1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
64 #include <linux/slab.h>
65 #include <linux/string.h>
66 #include <linux/export.h>
68 #include "iwl-phy-db.h"
69 #include "iwl-debug.h"
70 #include "iwl-op-mode.h"
71 #include "iwl-trans.h"
73 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
74 #define IWL_NUM_PAPD_CH_GROUPS 4
75 #define IWL_NUM_TXP_CH_GROUPS 9
77 struct iwl_phy_db_entry {
83 * struct iwl_phy_db - stores phy configuration and calibration data.
85 * @cfg: phy configuration.
86 * @calib_nch: non channel specific calibration data.
87 * @calib_ch: channel specific calibration data.
88 * @calib_ch_group_papd: calibration data related to papd channel group.
89 * @calib_ch_group_txp: calibration data related to tx power chanel group.
92 struct iwl_phy_db_entry cfg;
93 struct iwl_phy_db_entry calib_nch;
94 struct iwl_phy_db_entry calib_ch;
95 struct iwl_phy_db_entry calib_ch_group_papd[IWL_NUM_PAPD_CH_GROUPS];
96 struct iwl_phy_db_entry calib_ch_group_txp[IWL_NUM_TXP_CH_GROUPS];
101 struct iwl_trans *trans;
104 enum iwl_phy_db_section_type {
106 IWL_PHY_DB_CALIB_NCH,
108 IWL_PHY_DB_CALIB_CHG_PAPD,
109 IWL_PHY_DB_CALIB_CHG_TXP,
113 #define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */
116 * phy db - configure operational ucode
118 struct iwl_phy_db_cmd {
124 /* for parsing of tx power channel group data that comes from the firmware*/
125 struct iwl_phy_db_chg_txp {
127 __le16 max_channel_idx;
131 * phy db - Receieve phy db chunk after calibrations
133 struct iwl_calib_res_notif_phy_db {
139 #define IWL_PHY_DB_STATIC_PIC cpu_to_le32(0x21436587)
140 static inline void iwl_phy_db_test_pic(__le32 pic)
142 WARN_ON(IWL_PHY_DB_STATIC_PIC != pic);
145 struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
147 struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
153 phy_db->trans = trans;
155 /* TODO: add default values of the phy db. */
158 EXPORT_SYMBOL(iwl_phy_db_init);
161 * get phy db section: returns a pointer to a phy db section specified by
162 * type and channel group id.
164 static struct iwl_phy_db_entry *
165 iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
166 enum iwl_phy_db_section_type type,
169 if (!phy_db || type >= IWL_PHY_DB_MAX)
175 case IWL_PHY_DB_CALIB_NCH:
176 return &phy_db->calib_nch;
177 case IWL_PHY_DB_CALIB_CH:
178 return &phy_db->calib_ch;
179 case IWL_PHY_DB_CALIB_CHG_PAPD:
180 if (chg_id >= IWL_NUM_PAPD_CH_GROUPS)
182 return &phy_db->calib_ch_group_papd[chg_id];
183 case IWL_PHY_DB_CALIB_CHG_TXP:
184 if (chg_id >= IWL_NUM_TXP_CH_GROUPS)
186 return &phy_db->calib_ch_group_txp[chg_id];
193 static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
194 enum iwl_phy_db_section_type type,
197 struct iwl_phy_db_entry *entry =
198 iwl_phy_db_get_section(phy_db, type, chg_id);
207 void iwl_phy_db_free(struct iwl_phy_db *phy_db)
214 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
215 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
216 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CH, 0);
217 for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
218 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
219 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
220 iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
224 EXPORT_SYMBOL(iwl_phy_db_free);
226 int iwl_phy_db_set_section(struct iwl_phy_db *phy_db, struct iwl_rx_packet *pkt,
229 struct iwl_calib_res_notif_phy_db *phy_db_notif =
230 (struct iwl_calib_res_notif_phy_db *)pkt->data;
231 enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
232 u16 size = le16_to_cpu(phy_db_notif->length);
233 struct iwl_phy_db_entry *entry;
239 if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
240 type == IWL_PHY_DB_CALIB_CHG_TXP)
241 chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
243 entry = iwl_phy_db_get_section(phy_db, type, chg_id);
248 entry->data = kmemdup(phy_db_notif->data, size, alloc_ctx);
256 if (type == IWL_PHY_DB_CALIB_CH) {
257 phy_db->channel_num =
258 le32_to_cpup((__le32 *)phy_db_notif->data);
259 phy_db->channel_size =
260 (size - CHANNEL_NUM_SIZE) / phy_db->channel_num;
264 if (type != IWL_PHY_DB_CFG)
265 iwl_phy_db_test_pic(*(((__le32 *)phy_db_notif->data) +
266 (size / sizeof(__le32)) - 1));
268 IWL_DEBUG_INFO(phy_db->trans,
269 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
270 __func__, __LINE__, type, size);
274 EXPORT_SYMBOL(iwl_phy_db_set_section);
276 static int is_valid_channel(u16 ch_id)
279 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
280 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
281 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
286 static u8 ch_id_to_ch_index(u16 ch_id)
288 if (WARN_ON(!is_valid_channel(ch_id)))
294 return (ch_id + 20) / 4;
296 return (ch_id - 12) / 4;
297 return (ch_id - 13) / 4;
301 static u16 channel_id_to_papd(u16 ch_id)
303 if (WARN_ON(!is_valid_channel(ch_id)))
306 if (1 <= ch_id && ch_id <= 14)
308 if (36 <= ch_id && ch_id <= 64)
310 if (100 <= ch_id && ch_id <= 140)
315 static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
317 struct iwl_phy_db_chg_txp *txp_chg;
319 u8 ch_index = ch_id_to_ch_index(ch_id);
320 if (ch_index == 0xff)
323 for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
324 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
328 * Looking for the first channel group that its max channel is
329 * higher then wanted channel.
331 if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
337 int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
338 u32 type, u8 **data, u16 *size, u16 ch_id)
340 struct iwl_phy_db_entry *entry;
349 /* find wanted channel group */
350 if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
351 ch_group_id = channel_id_to_papd(ch_id);
352 else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
353 ch_group_id = channel_id_to_txp(phy_db, ch_id);
355 entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
359 if (type == IWL_PHY_DB_CALIB_CH) {
360 index = ch_id_to_ch_index(ch_id);
361 channel_num = phy_db->channel_num;
362 channel_size = phy_db->channel_size;
363 if (index >= channel_num) {
364 IWL_ERR(phy_db->trans, "Wrong channel number %d\n",
368 *data = entry->data + CHANNEL_NUM_SIZE + index * channel_size;
369 *size = channel_size;
376 if (type != IWL_PHY_DB_CFG)
377 iwl_phy_db_test_pic(*(((__le32 *)*data) +
378 (*size / sizeof(__le32)) - 1));
380 IWL_DEBUG_INFO(phy_db->trans,
381 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
382 __func__, __LINE__, type, *size);
387 static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
388 u16 length, void *data)
390 struct iwl_phy_db_cmd phy_db_cmd;
391 struct iwl_host_cmd cmd = {
396 IWL_DEBUG_INFO(phy_db->trans,
397 "Sending PHY-DB hcmd of type %d, of length %d\n",
400 /* Set phy db cmd variables */
401 phy_db_cmd.type = cpu_to_le16(type);
402 phy_db_cmd.length = cpu_to_le16(length);
404 /* Set hcmd variables */
405 cmd.data[0] = &phy_db_cmd;
406 cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
409 cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
411 return iwl_trans_send_cmd(phy_db->trans, &cmd);
414 static int iwl_phy_db_send_all_channel_groups(
415 struct iwl_phy_db *phy_db,
416 enum iwl_phy_db_section_type type,
421 struct iwl_phy_db_entry *entry;
423 /* Send all the channel specific groups to operational fw */
424 for (i = 0; i < max_ch_groups; i++) {
425 entry = iwl_phy_db_get_section(phy_db,
431 /* Send the requested PHY DB section */
432 err = iwl_send_phy_db_cmd(phy_db,
437 IWL_ERR(phy_db->trans,
438 "Can't SEND phy_db section %d (%d), err %d",
443 IWL_DEBUG_INFO(phy_db->trans,
444 "Sent PHY_DB HCMD, type = %d num = %d",
451 int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
457 IWL_DEBUG_INFO(phy_db->trans,
458 "Sending phy db data and configuration to runtime image\n");
460 /* Send PHY DB CFG section */
461 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
464 IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
468 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
470 IWL_ERR(phy_db->trans,
471 "Cannot send HCMD of Phy DB cfg section\n");
475 err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
478 IWL_ERR(phy_db->trans,
479 "Cannot get Phy DB non specific channel section\n");
483 err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
485 IWL_ERR(phy_db->trans,
486 "Cannot send HCMD of Phy DB non specific channel section\n");
490 /* Send all the TXP channel specific data */
491 err = iwl_phy_db_send_all_channel_groups(phy_db,
492 IWL_PHY_DB_CALIB_CHG_PAPD,
493 IWL_NUM_PAPD_CH_GROUPS);
495 IWL_ERR(phy_db->trans,
496 "Cannot send channel specific PAPD groups");
500 /* Send all the TXP channel specific data */
501 err = iwl_phy_db_send_all_channel_groups(phy_db,
502 IWL_PHY_DB_CALIB_CHG_TXP,
503 IWL_NUM_TXP_CH_GROUPS);
505 IWL_ERR(phy_db->trans,
506 "Cannot send channel specific TX power groups");
510 IWL_DEBUG_INFO(phy_db->trans,
511 "Finished sending phy db non channel data\n");
514 EXPORT_SYMBOL(iwl_send_phy_db_data);