2 * iwmc3200top - Intel Wireless MultiCom 3200 Top Driver
3 * drivers/misc/iwmc3200top/main.c
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * Author Name: Maxim Grabarnik <maxim.grabarnink@intel.com>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/debugfs.h>
31 #include <linux/mmc/sdio_ids.h>
32 #include <linux/mmc/sdio_func.h>
33 #include <linux/mmc/sdio.h>
35 #include "iwmc3200top.h"
41 #define DRIVER_DESCRIPTION "Intel(R) IWMC 3200 Top Driver"
42 #define DRIVER_COPYRIGHT "Copyright (c) 2008 Intel Corporation."
44 #define IWMCT_VERSION "0.1.62"
46 #ifdef REPOSITORY_LABEL
47 #define RL REPOSITORY_LABEL
52 #ifdef CONFIG_IWMC3200TOP_DEBUG
58 #define DRIVER_VERSION IWMCT_VERSION "-" __stringify(RL) VD
60 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
61 MODULE_VERSION(DRIVER_VERSION);
62 MODULE_LICENSE("GPL");
63 MODULE_AUTHOR(DRIVER_COPYRIGHT);
66 * This workers main task is to wait for OP_OPR_ALIVE
67 * from TOP FW until ALIVE_MSG_TIMOUT timeout is elapsed.
68 * When OP_OPR_ALIVE received it will issue
69 * a call to "bus_rescan_devices".
71 static void iwmct_rescan_worker(struct work_struct *ws)
73 struct iwmct_priv *priv;
76 priv = container_of(ws, struct iwmct_priv, bus_rescan_worker);
78 LOG_INFO(priv, FW_MSG, "Calling bus_rescan\n");
80 ret = bus_rescan_devices(priv->func->dev.bus);
82 LOG_INFO(priv, FW_DOWNLOAD, "bus_rescan_devices FAILED!!!\n");
85 static void op_top_message(struct iwmct_priv *priv, struct top_msg *msg)
87 switch (msg->hdr.opcode) {
89 LOG_INFO(priv, FW_MSG, "Got ALIVE from device, wake rescan\n");
90 queue_work(priv->bus_rescan_wq, &priv->bus_rescan_worker);
93 LOG_INFO(priv, FW_MSG, "Received msg opcode 0x%X\n",
100 static void handle_top_message(struct iwmct_priv *priv, u8 *buf, int len)
104 msg = (struct top_msg *)buf;
106 if (msg->hdr.type != COMM_TYPE_D2H) {
107 LOG_ERROR(priv, FW_MSG,
108 "Message from TOP with invalid message type 0x%X\n",
113 if (len < sizeof(msg->hdr)) {
114 LOG_ERROR(priv, FW_MSG,
115 "Message from TOP is too short for message header "
116 "received %d bytes, expected at least %zd bytes\n",
117 len, sizeof(msg->hdr));
121 if (len < le16_to_cpu(msg->hdr.length) + sizeof(msg->hdr)) {
122 LOG_ERROR(priv, FW_MSG,
123 "Message length (%d bytes) is shorter than "
124 "in header (%d bytes)\n",
125 len, le16_to_cpu(msg->hdr.length));
129 switch (msg->hdr.category) {
130 case COMM_CATEGORY_OPERATIONAL:
131 op_top_message(priv, (struct top_msg *)buf);
134 case COMM_CATEGORY_DEBUG:
135 case COMM_CATEGORY_TESTABILITY:
136 case COMM_CATEGORY_DIAGNOSTICS:
137 iwmct_log_top_message(priv, buf, len);
141 LOG_ERROR(priv, FW_MSG,
142 "Message from TOP with unknown category 0x%X\n",
148 int iwmct_send_hcmd(struct iwmct_priv *priv, u8 *cmd, u16 len)
153 LOG_INFOEX(priv, FW_MSG, "Sending hcmd:\n");
155 /* add padding to 256 for IWMC */
156 ((struct top_msg *)cmd)->hdr.flags |= CMD_FLAG_PADDING_256;
158 LOG_HEXDUMP(FW_MSG, cmd, len);
160 if (len > FW_HCMD_BLOCK_SIZE) {
161 LOG_ERROR(priv, FW_MSG, "size %d exceeded hcmd max size %d\n",
162 len, FW_HCMD_BLOCK_SIZE);
166 buf = kzalloc(FW_HCMD_BLOCK_SIZE, GFP_KERNEL);
168 LOG_ERROR(priv, FW_MSG, "kzalloc error, buf size %d\n",
173 memcpy(buf, cmd, len);
175 sdio_claim_host(priv->func);
176 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR, buf,
178 sdio_release_host(priv->func);
184 int iwmct_tx(struct iwmct_priv *priv, unsigned int addr,
185 void *src, int count)
189 sdio_claim_host(priv->func);
190 ret = sdio_memcpy_toio(priv->func, addr, src, count);
191 sdio_release_host(priv->func);
196 static void iwmct_irq_read_worker(struct work_struct *ws)
198 struct iwmct_priv *priv;
199 struct iwmct_work_struct *read_req;
206 priv = container_of(ws, struct iwmct_priv, isr_worker);
208 LOG_INFO(priv, IRQ, "enter iwmct_irq_read_worker %p\n", ws);
210 /* --------------------- Handshake with device -------------------- */
211 sdio_claim_host(priv->func);
213 /* all list manipulations have to be protected by
214 * sdio_claim_host/sdio_release_host */
215 if (list_empty(&priv->read_req_list)) {
216 LOG_ERROR(priv, IRQ, "read_req_list empty in read worker\n");
220 read_req = list_entry(priv->read_req_list.next,
221 struct iwmct_work_struct, list);
223 list_del(&read_req->list);
224 iosize = read_req->iosize;
227 buf = kzalloc(iosize, GFP_KERNEL);
229 LOG_ERROR(priv, IRQ, "kzalloc error, buf size %d\n", iosize);
233 LOG_INFO(priv, IRQ, "iosize=%d, buf=%p, func=%d\n",
234 iosize, buf, priv->func->num);
236 /* read from device */
237 ret = sdio_memcpy_fromio(priv->func, buf, IWMC_SDIO_DATA_ADDR, iosize);
239 LOG_ERROR(priv, IRQ, "error %d reading buffer\n", ret);
243 LOG_HEXDUMP(IRQ, (u8 *)buf, iosize);
245 barker = le32_to_cpu(buf[0]);
247 /* Verify whether it's a barker and if not - treat as regular Rx */
248 if (barker == IWMC_BARKER_ACK ||
249 (barker & BARKER_DNLOAD_BARKER_MSK) == IWMC_BARKER_REBOOT) {
251 /* Valid Barker is equal on first 4 dwords */
252 is_barker = (buf[1] == buf[0]) &&
253 (buf[2] == buf[0]) &&
257 LOG_WARNING(priv, IRQ,
258 "Potentially inconsistent barker "
259 "%08X_%08X_%08X_%08X\n",
260 le32_to_cpu(buf[0]), le32_to_cpu(buf[1]),
261 le32_to_cpu(buf[2]), le32_to_cpu(buf[3]));
267 /* Handle Top CommHub message */
269 sdio_release_host(priv->func);
270 handle_top_message(priv, (u8 *)buf, iosize);
272 } else if (barker == IWMC_BARKER_ACK) { /* Handle barkers */
273 if (atomic_read(&priv->dev_sync) == 0) {
275 "ACK barker arrived out-of-sync\n");
279 /* Continuing to FW download (after Sync is completed)*/
280 atomic_set(&priv->dev_sync, 0);
281 LOG_INFO(priv, IRQ, "ACK barker arrived "
282 "- starting FW download\n");
283 } else { /* REBOOT barker */
284 LOG_INFO(priv, IRQ, "Recieved reboot barker: %x\n", barker);
285 priv->barker = barker;
287 if (barker & BARKER_DNLOAD_SYNC_MSK) {
288 /* Send the same barker back */
289 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR,
293 "error %d echoing barker\n", ret);
296 LOG_INFO(priv, IRQ, "Echoing barker to device\n");
297 atomic_set(&priv->dev_sync, 1);
301 /* Continuing to FW download (without Sync) */
302 LOG_INFO(priv, IRQ, "No sync requested "
303 "- starting FW download\n");
306 sdio_release_host(priv->func);
309 LOG_INFO(priv, IRQ, "barker download request 0x%x is:\n", priv->barker);
310 LOG_INFO(priv, IRQ, "******* Top FW %s requested ********\n",
311 (priv->barker & BARKER_DNLOAD_TOP_MSK) ? "was" : "not");
312 LOG_INFO(priv, IRQ, "******* GPS FW %s requested ********\n",
313 (priv->barker & BARKER_DNLOAD_GPS_MSK) ? "was" : "not");
314 LOG_INFO(priv, IRQ, "******* BT FW %s requested ********\n",
315 (priv->barker & BARKER_DNLOAD_BT_MSK) ? "was" : "not");
317 if (priv->dbg.fw_download)
320 LOG_ERROR(priv, IRQ, "FW download not allowed\n");
325 sdio_release_host(priv->func);
328 LOG_INFO(priv, IRQ, "exit iwmct_irq_read_worker\n");
331 static void iwmct_irq(struct sdio_func *func)
333 struct iwmct_priv *priv;
336 int addr = IWMC_SDIO_INTR_GET_SIZE_ADDR;
337 struct iwmct_work_struct *read_req;
339 priv = sdio_get_drvdata(func);
341 LOG_INFO(priv, IRQ, "enter iwmct_irq\n");
343 /* read the function's status register */
344 val = sdio_readb(func, IWMC_SDIO_INTR_STATUS_ADDR, &ret);
346 LOG_INFO(priv, IRQ, "iir value = %d, ret=%d\n", val, ret);
349 LOG_ERROR(priv, IRQ, "iir = 0, exiting ISR\n");
350 goto exit_clear_intr;
355 * read 2 bytes of the transaction size
356 * IMPORTANT: sdio transaction size has to be read before clearing
359 val = sdio_readb(priv->func, addr++, &ret);
361 val = sdio_readb(priv->func, addr++, &ret);
364 LOG_INFO(priv, IRQ, "READ size %d\n", iosize);
367 LOG_ERROR(priv, IRQ, "READ size %d, exiting ISR\n", iosize);
368 goto exit_clear_intr;
371 /* allocate a work structure to pass iosize to the worker */
372 read_req = kzalloc(sizeof(struct iwmct_work_struct), GFP_KERNEL);
374 LOG_ERROR(priv, IRQ, "failed to allocate read_req, exit ISR\n");
375 goto exit_clear_intr;
378 INIT_LIST_HEAD(&read_req->list);
379 read_req->iosize = iosize;
381 list_add_tail(&priv->read_req_list, &read_req->list);
383 /* clear the function's interrupt request bit (write 1 to clear) */
384 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
386 queue_work(priv->wq, &priv->isr_worker);
388 LOG_INFO(priv, IRQ, "exit iwmct_irq\n");
393 /* clear the function's interrupt request bit (write 1 to clear) */
394 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
399 module_param(blocks, int, 0604);
400 MODULE_PARM_DESC(blocks, "max_blocks_to_send");
403 module_param(dump, bool, 0604);
404 MODULE_PARM_DESC(dump, "dump_hex_content");
407 module_param(jump, bool, 0604);
409 static int direct = 1;
410 module_param(direct, bool, 0604);
412 static int checksum = 1;
413 module_param(checksum, bool, 0604);
415 static int fw_download = 1;
416 module_param(fw_download, bool, 0604);
418 static int block_size = IWMC_SDIO_BLK_SIZE;
419 module_param(block_size, int, 0404);
421 static int download_trans_blks = IWMC_DEFAULT_TR_BLK;
422 module_param(download_trans_blks, int, 0604);
424 static int rubbish_barker;
425 module_param(rubbish_barker, bool, 0604);
427 #ifdef CONFIG_IWMC3200TOP_DEBUG
428 static int log_level[LOG_SRC_MAX];
429 static unsigned int log_level_argc;
430 module_param_array(log_level, int, &log_level_argc, 0604);
431 MODULE_PARM_DESC(log_level, "log_level");
433 static int log_level_fw[FW_LOG_SRC_MAX];
434 static unsigned int log_level_fw_argc;
435 module_param_array(log_level_fw, int, &log_level_fw_argc, 0604);
436 MODULE_PARM_DESC(log_level_fw, "log_level_fw");
439 void iwmct_dbg_init_params(struct iwmct_priv *priv)
441 #ifdef CONFIG_IWMC3200TOP_DEBUG
444 for (i = 0; i < log_level_argc; i++) {
445 dev_notice(&priv->func->dev, "log_level[%d]=0x%X\n",
447 iwmct_log_set_filter((log_level[i] >> 8) & 0xFF,
448 log_level[i] & 0xFF);
450 for (i = 0; i < log_level_fw_argc; i++) {
451 dev_notice(&priv->func->dev, "log_level_fw[%d]=0x%X\n",
453 iwmct_log_set_fw_filter((log_level_fw[i] >> 8) & 0xFF,
454 log_level_fw[i] & 0xFF);
458 priv->dbg.blocks = blocks;
459 LOG_INFO(priv, INIT, "blocks=%d\n", blocks);
460 priv->dbg.dump = (bool)dump;
461 LOG_INFO(priv, INIT, "dump=%d\n", dump);
462 priv->dbg.jump = (bool)jump;
463 LOG_INFO(priv, INIT, "jump=%d\n", jump);
464 priv->dbg.direct = (bool)direct;
465 LOG_INFO(priv, INIT, "direct=%d\n", direct);
466 priv->dbg.checksum = (bool)checksum;
467 LOG_INFO(priv, INIT, "checksum=%d\n", checksum);
468 priv->dbg.fw_download = (bool)fw_download;
469 LOG_INFO(priv, INIT, "fw_download=%d\n", fw_download);
470 priv->dbg.block_size = block_size;
471 LOG_INFO(priv, INIT, "block_size=%d\n", block_size);
472 priv->dbg.download_trans_blks = download_trans_blks;
473 LOG_INFO(priv, INIT, "download_trans_blks=%d\n", download_trans_blks);
476 /*****************************************************************************
480 *****************************************************************************/
481 static ssize_t show_iwmct_fw_version(struct device *d,
482 struct device_attribute *attr, char *buf)
484 struct iwmct_priv *priv = dev_get_drvdata(d);
485 return sprintf(buf, "%s\n", priv->dbg.label_fw);
487 static DEVICE_ATTR(cc_label_fw, S_IRUGO, show_iwmct_fw_version, NULL);
489 #ifdef CONFIG_IWMC3200TOP_DEBUG
490 static DEVICE_ATTR(log_level, S_IWUSR | S_IRUGO,
491 show_iwmct_log_level, store_iwmct_log_level);
492 static DEVICE_ATTR(log_level_fw, S_IWUSR | S_IRUGO,
493 show_iwmct_log_level_fw, store_iwmct_log_level_fw);
496 static struct attribute *iwmct_sysfs_entries[] = {
497 &dev_attr_cc_label_fw.attr,
498 #ifdef CONFIG_IWMC3200TOP_DEBUG
499 &dev_attr_log_level.attr,
500 &dev_attr_log_level_fw.attr,
505 static struct attribute_group iwmct_attribute_group = {
506 .name = NULL, /* put in device directory */
507 .attrs = iwmct_sysfs_entries,
511 static int iwmct_probe(struct sdio_func *func,
512 const struct sdio_device_id *id)
514 struct iwmct_priv *priv;
517 int addr = IWMC_SDIO_INTR_ENABLE_ADDR;
519 dev_dbg(&func->dev, "enter iwmct_probe\n");
521 dev_dbg(&func->dev, "IRQ polling period id %u msecs, HZ is %d\n",
522 jiffies_to_msecs(2147483647), HZ);
524 priv = kzalloc(sizeof(struct iwmct_priv), GFP_KERNEL);
526 dev_err(&func->dev, "kzalloc error\n");
530 sdio_set_drvdata(func, priv);
533 /* create drivers work queue */
534 priv->wq = create_workqueue(DRV_NAME "_wq");
535 priv->bus_rescan_wq = create_workqueue(DRV_NAME "_rescan_wq");
536 INIT_WORK(&priv->bus_rescan_worker, iwmct_rescan_worker);
537 INIT_WORK(&priv->isr_worker, iwmct_irq_read_worker);
539 init_waitqueue_head(&priv->wait_q);
541 sdio_claim_host(func);
542 /* FIXME: Remove after it is fixed in the Boot ROM upgrade */
543 func->enable_timeout = 10;
545 /* In our HW, setting the block size also wakes up the boot rom. */
546 ret = sdio_set_block_size(func, priv->dbg.block_size);
548 LOG_ERROR(priv, INIT,
549 "sdio_set_block_size() failure: %d\n", ret);
550 goto error_sdio_enable;
553 ret = sdio_enable_func(func);
555 LOG_ERROR(priv, INIT, "sdio_enable_func() failure: %d\n", ret);
556 goto error_sdio_enable;
559 /* init reset and dev_sync states */
560 atomic_set(&priv->reset, 0);
561 atomic_set(&priv->dev_sync, 0);
563 /* init read req queue */
564 INIT_LIST_HEAD(&priv->read_req_list);
566 /* process configurable parameters */
567 iwmct_dbg_init_params(priv);
568 ret = sysfs_create_group(&func->dev.kobj, &iwmct_attribute_group);
570 LOG_ERROR(priv, INIT, "Failed to register attributes and "
571 "initialize module_params\n");
572 goto error_dev_attrs;
575 iwmct_dbgfs_register(priv, DRV_NAME);
577 if (!priv->dbg.direct && priv->dbg.download_trans_blks > 8) {
579 "Reducing transaction to 8 blocks = 2K (from %d)\n",
580 priv->dbg.download_trans_blks);
581 priv->dbg.download_trans_blks = 8;
583 priv->trans_len = priv->dbg.download_trans_blks * priv->dbg.block_size;
584 LOG_INFO(priv, INIT, "Transaction length = %d\n", priv->trans_len);
586 ret = sdio_claim_irq(func, iwmct_irq);
588 LOG_ERROR(priv, INIT, "sdio_claim_irq() failure: %d\n", ret);
589 goto error_claim_irq;
593 /* Enable function's interrupt */
594 sdio_writeb(priv->func, val, addr, &ret);
596 LOG_ERROR(priv, INIT, "Failure writing to "
597 "Interrupt Enable Register (%d): %d\n", addr, ret);
598 goto error_enable_int;
601 sdio_release_host(func);
603 LOG_INFO(priv, INIT, "exit iwmct_probe\n");
608 sdio_release_irq(func);
610 sdio_disable_func(func);
612 iwmct_dbgfs_unregister(priv->dbgfs);
613 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
615 sdio_release_host(func);
619 static void iwmct_remove(struct sdio_func *func)
621 struct iwmct_work_struct *read_req;
622 struct iwmct_priv *priv = sdio_get_drvdata(func);
624 priv = sdio_get_drvdata(func);
626 LOG_INFO(priv, INIT, "enter\n");
628 sdio_claim_host(func);
629 sdio_release_irq(func);
630 sdio_release_host(func);
632 /* Safely destroy osc workqueue */
633 destroy_workqueue(priv->bus_rescan_wq);
634 destroy_workqueue(priv->wq);
636 sdio_claim_host(func);
637 sdio_disable_func(func);
638 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
639 iwmct_dbgfs_unregister(priv->dbgfs);
640 sdio_release_host(func);
642 /* free read requests */
643 while (!list_empty(&priv->read_req_list)) {
644 read_req = list_entry(priv->read_req_list.next,
645 struct iwmct_work_struct, list);
647 list_del(&read_req->list);
655 static const struct sdio_device_id iwmct_ids[] = {
656 /* Intel Wireless MultiCom 3200 Top Driver */
657 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1404)},
658 { }, /* Terminating entry */
661 MODULE_DEVICE_TABLE(sdio, iwmct_ids);
663 static struct sdio_driver iwmct_driver = {
664 .probe = iwmct_probe,
665 .remove = iwmct_remove,
667 .id_table = iwmct_ids,
670 static int __init iwmct_init(void)
674 /* Default log filter settings */
675 iwmct_log_set_filter(LOG_SRC_ALL, LOG_SEV_FILTER_RUNTIME);
676 iwmct_log_set_filter(LOG_SRC_FW_MSG, LOG_SEV_FILTER_ALL);
677 iwmct_log_set_fw_filter(LOG_SRC_ALL, FW_LOG_SEV_FILTER_RUNTIME);
679 rc = sdio_register_driver(&iwmct_driver);
684 static void __exit iwmct_exit(void)
686 sdio_unregister_driver(&iwmct_driver);
689 module_init(iwmct_init);
690 module_exit(iwmct_exit);