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 DRIVER_VERSION "0.1.62"
46 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
47 MODULE_VERSION(DRIVER_VERSION);
48 MODULE_LICENSE("GPL");
49 MODULE_AUTHOR(DRIVER_COPYRIGHT);
52 * This workers main task is to wait for OP_OPR_ALIVE
53 * from TOP FW until ALIVE_MSG_TIMOUT timeout is elapsed.
54 * When OP_OPR_ALIVE received it will issue
55 * a call to "bus_rescan_devices".
57 static void iwmct_rescan_worker(struct work_struct *ws)
59 struct iwmct_priv *priv;
62 priv = container_of(ws, struct iwmct_priv, bus_rescan_worker);
64 LOG_INFO(priv, FW_MSG, "Calling bus_rescan\n");
66 ret = bus_rescan_devices(priv->func->dev.bus);
68 LOG_INFO(priv, FW_DOWNLOAD, "bus_rescan_devices FAILED!!!\n");
71 static void op_top_message(struct iwmct_priv *priv, struct top_msg *msg)
73 switch (msg->hdr.opcode) {
75 LOG_INFO(priv, FW_MSG, "Got ALIVE from device, wake rescan\n");
76 queue_work(priv->bus_rescan_wq, &priv->bus_rescan_worker);
79 LOG_INFO(priv, FW_MSG, "Received msg opcode 0x%X\n",
86 static void handle_top_message(struct iwmct_priv *priv, u8 *buf, int len)
90 msg = (struct top_msg *)buf;
92 if (msg->hdr.type != COMM_TYPE_D2H) {
93 LOG_ERROR(priv, FW_MSG,
94 "Message from TOP with invalid message type 0x%X\n",
99 if (len < sizeof(msg->hdr)) {
100 LOG_ERROR(priv, FW_MSG,
101 "Message from TOP is too short for message header "
102 "received %d bytes, expected at least %zd bytes\n",
103 len, sizeof(msg->hdr));
107 if (len < le16_to_cpu(msg->hdr.length) + sizeof(msg->hdr)) {
108 LOG_ERROR(priv, FW_MSG,
109 "Message length (%d bytes) is shorter than "
110 "in header (%d bytes)\n",
111 len, le16_to_cpu(msg->hdr.length));
115 switch (msg->hdr.category) {
116 case COMM_CATEGORY_OPERATIONAL:
117 op_top_message(priv, (struct top_msg *)buf);
120 case COMM_CATEGORY_DEBUG:
121 case COMM_CATEGORY_TESTABILITY:
122 case COMM_CATEGORY_DIAGNOSTICS:
123 iwmct_log_top_message(priv, buf, len);
127 LOG_ERROR(priv, FW_MSG,
128 "Message from TOP with unknown category 0x%X\n",
134 int iwmct_send_hcmd(struct iwmct_priv *priv, u8 *cmd, u16 len)
139 LOG_INFOEX(priv, FW_MSG, "Sending hcmd:\n");
141 /* add padding to 256 for IWMC */
142 ((struct top_msg *)cmd)->hdr.flags |= CMD_FLAG_PADDING_256;
144 LOG_HEXDUMP(FW_MSG, cmd, len);
146 if (len > FW_HCMD_BLOCK_SIZE) {
147 LOG_ERROR(priv, FW_MSG, "size %d exceeded hcmd max size %d\n",
148 len, FW_HCMD_BLOCK_SIZE);
152 buf = kzalloc(FW_HCMD_BLOCK_SIZE, GFP_KERNEL);
154 LOG_ERROR(priv, FW_MSG, "kzalloc error, buf size %d\n",
159 memcpy(buf, cmd, len);
161 sdio_claim_host(priv->func);
162 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR, buf,
164 sdio_release_host(priv->func);
170 int iwmct_tx(struct iwmct_priv *priv, unsigned int addr,
171 void *src, int count)
175 sdio_claim_host(priv->func);
176 ret = sdio_memcpy_toio(priv->func, addr, src, count);
177 sdio_release_host(priv->func);
182 static void iwmct_irq_read_worker(struct work_struct *ws)
184 struct iwmct_priv *priv;
185 struct iwmct_work_struct *read_req;
192 priv = container_of(ws, struct iwmct_priv, isr_worker);
194 LOG_INFO(priv, IRQ, "enter iwmct_irq_read_worker %p\n", ws);
196 /* --------------------- Handshake with device -------------------- */
197 sdio_claim_host(priv->func);
199 /* all list manipulations have to be protected by
200 * sdio_claim_host/sdio_release_host */
201 if (list_empty(&priv->read_req_list)) {
202 LOG_ERROR(priv, IRQ, "read_req_list empty in read worker\n");
206 read_req = list_entry(priv->read_req_list.next,
207 struct iwmct_work_struct, list);
209 list_del(&read_req->list);
210 iosize = read_req->iosize;
213 buf = kzalloc(iosize, GFP_KERNEL);
215 LOG_ERROR(priv, IRQ, "kzalloc error, buf size %d\n", iosize);
219 LOG_INFO(priv, IRQ, "iosize=%d, buf=%p, func=%d\n",
220 iosize, buf, priv->func->num);
222 /* read from device */
223 ret = sdio_memcpy_fromio(priv->func, buf, IWMC_SDIO_DATA_ADDR, iosize);
225 LOG_ERROR(priv, IRQ, "error %d reading buffer\n", ret);
229 LOG_HEXDUMP(IRQ, (u8 *)buf, iosize);
231 barker = le32_to_cpu(buf[0]);
233 /* Verify whether it's a barker and if not - treat as regular Rx */
234 if (barker == IWMC_BARKER_ACK ||
235 (barker & BARKER_DNLOAD_BARKER_MSK) == IWMC_BARKER_REBOOT) {
237 /* Valid Barker is equal on first 4 dwords */
238 is_barker = (buf[1] == buf[0]) &&
239 (buf[2] == buf[0]) &&
243 LOG_WARNING(priv, IRQ,
244 "Potentially inconsistent barker "
245 "%08X_%08X_%08X_%08X\n",
246 le32_to_cpu(buf[0]), le32_to_cpu(buf[1]),
247 le32_to_cpu(buf[2]), le32_to_cpu(buf[3]));
253 /* Handle Top CommHub message */
255 sdio_release_host(priv->func);
256 handle_top_message(priv, (u8 *)buf, iosize);
258 } else if (barker == IWMC_BARKER_ACK) { /* Handle barkers */
259 if (atomic_read(&priv->dev_sync) == 0) {
261 "ACK barker arrived out-of-sync\n");
265 /* Continuing to FW download (after Sync is completed)*/
266 atomic_set(&priv->dev_sync, 0);
267 LOG_INFO(priv, IRQ, "ACK barker arrived "
268 "- starting FW download\n");
269 } else { /* REBOOT barker */
270 LOG_INFO(priv, IRQ, "Recieved reboot barker: %x\n", barker);
271 priv->barker = barker;
273 if (barker & BARKER_DNLOAD_SYNC_MSK) {
274 /* Send the same barker back */
275 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR,
279 "error %d echoing barker\n", ret);
282 LOG_INFO(priv, IRQ, "Echoing barker to device\n");
283 atomic_set(&priv->dev_sync, 1);
287 /* Continuing to FW download (without Sync) */
288 LOG_INFO(priv, IRQ, "No sync requested "
289 "- starting FW download\n");
292 sdio_release_host(priv->func);
295 LOG_INFO(priv, IRQ, "barker download request 0x%x is:\n", priv->barker);
296 LOG_INFO(priv, IRQ, "******* Top FW %s requested ********\n",
297 (priv->barker & BARKER_DNLOAD_TOP_MSK) ? "was" : "not");
298 LOG_INFO(priv, IRQ, "******* GPS FW %s requested ********\n",
299 (priv->barker & BARKER_DNLOAD_GPS_MSK) ? "was" : "not");
300 LOG_INFO(priv, IRQ, "******* BT FW %s requested ********\n",
301 (priv->barker & BARKER_DNLOAD_BT_MSK) ? "was" : "not");
303 if (priv->dbg.fw_download)
306 LOG_ERROR(priv, IRQ, "FW download not allowed\n");
311 sdio_release_host(priv->func);
314 LOG_INFO(priv, IRQ, "exit iwmct_irq_read_worker\n");
317 static void iwmct_irq(struct sdio_func *func)
319 struct iwmct_priv *priv;
322 int addr = IWMC_SDIO_INTR_GET_SIZE_ADDR;
323 struct iwmct_work_struct *read_req;
325 priv = sdio_get_drvdata(func);
327 LOG_INFO(priv, IRQ, "enter iwmct_irq\n");
329 /* read the function's status register */
330 val = sdio_readb(func, IWMC_SDIO_INTR_STATUS_ADDR, &ret);
332 LOG_INFO(priv, IRQ, "iir value = %d, ret=%d\n", val, ret);
335 LOG_ERROR(priv, IRQ, "iir = 0, exiting ISR\n");
336 goto exit_clear_intr;
341 * read 2 bytes of the transaction size
342 * IMPORTANT: sdio transaction size has to be read before clearing
345 val = sdio_readb(priv->func, addr++, &ret);
347 val = sdio_readb(priv->func, addr++, &ret);
350 LOG_INFO(priv, IRQ, "READ size %d\n", iosize);
353 LOG_ERROR(priv, IRQ, "READ size %d, exiting ISR\n", iosize);
354 goto exit_clear_intr;
357 /* allocate a work structure to pass iosize to the worker */
358 read_req = kzalloc(sizeof(struct iwmct_work_struct), GFP_KERNEL);
360 LOG_ERROR(priv, IRQ, "failed to allocate read_req, exit ISR\n");
361 goto exit_clear_intr;
364 INIT_LIST_HEAD(&read_req->list);
365 read_req->iosize = iosize;
367 list_add_tail(&priv->read_req_list, &read_req->list);
369 /* clear the function's interrupt request bit (write 1 to clear) */
370 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
372 queue_work(priv->wq, &priv->isr_worker);
374 LOG_INFO(priv, IRQ, "exit iwmct_irq\n");
379 /* clear the function's interrupt request bit (write 1 to clear) */
380 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
385 module_param(blocks, int, 0604);
386 MODULE_PARM_DESC(blocks, "max_blocks_to_send");
389 module_param(dump, bool, 0604);
390 MODULE_PARM_DESC(dump, "dump_hex_content");
393 module_param(jump, bool, 0604);
395 static int direct = 1;
396 module_param(direct, bool, 0604);
398 static int checksum = 1;
399 module_param(checksum, bool, 0604);
401 static int fw_download = 1;
402 module_param(fw_download, bool, 0604);
404 static int block_size = IWMC_SDIO_BLK_SIZE;
405 module_param(block_size, int, 0404);
407 static int download_trans_blks = IWMC_DEFAULT_TR_BLK;
408 module_param(download_trans_blks, int, 0604);
410 static int rubbish_barker;
411 module_param(rubbish_barker, bool, 0604);
413 #ifdef CONFIG_IWMC3200TOP_DEBUG
414 static int log_level[LOG_SRC_MAX];
415 static unsigned int log_level_argc;
416 module_param_array(log_level, int, &log_level_argc, 0604);
417 MODULE_PARM_DESC(log_level, "log_level");
419 static int log_level_fw[FW_LOG_SRC_MAX];
420 static unsigned int log_level_fw_argc;
421 module_param_array(log_level_fw, int, &log_level_fw_argc, 0604);
422 MODULE_PARM_DESC(log_level_fw, "log_level_fw");
425 void iwmct_dbg_init_params(struct iwmct_priv *priv)
427 #ifdef CONFIG_IWMC3200TOP_DEBUG
430 for (i = 0; i < log_level_argc; i++) {
431 dev_notice(&priv->func->dev, "log_level[%d]=0x%X\n",
433 iwmct_log_set_filter((log_level[i] >> 8) & 0xFF,
434 log_level[i] & 0xFF);
436 for (i = 0; i < log_level_fw_argc; i++) {
437 dev_notice(&priv->func->dev, "log_level_fw[%d]=0x%X\n",
439 iwmct_log_set_fw_filter((log_level_fw[i] >> 8) & 0xFF,
440 log_level_fw[i] & 0xFF);
444 priv->dbg.blocks = blocks;
445 LOG_INFO(priv, INIT, "blocks=%d\n", blocks);
446 priv->dbg.dump = (bool)dump;
447 LOG_INFO(priv, INIT, "dump=%d\n", dump);
448 priv->dbg.jump = (bool)jump;
449 LOG_INFO(priv, INIT, "jump=%d\n", jump);
450 priv->dbg.direct = (bool)direct;
451 LOG_INFO(priv, INIT, "direct=%d\n", direct);
452 priv->dbg.checksum = (bool)checksum;
453 LOG_INFO(priv, INIT, "checksum=%d\n", checksum);
454 priv->dbg.fw_download = (bool)fw_download;
455 LOG_INFO(priv, INIT, "fw_download=%d\n", fw_download);
456 priv->dbg.block_size = block_size;
457 LOG_INFO(priv, INIT, "block_size=%d\n", block_size);
458 priv->dbg.download_trans_blks = download_trans_blks;
459 LOG_INFO(priv, INIT, "download_trans_blks=%d\n", download_trans_blks);
462 /*****************************************************************************
466 *****************************************************************************/
467 static ssize_t show_iwmct_fw_version(struct device *d,
468 struct device_attribute *attr, char *buf)
470 struct iwmct_priv *priv = dev_get_drvdata(d);
471 return sprintf(buf, "%s\n", priv->dbg.label_fw);
473 static DEVICE_ATTR(cc_label_fw, S_IRUGO, show_iwmct_fw_version, NULL);
475 #ifdef CONFIG_IWMC3200TOP_DEBUG
476 static DEVICE_ATTR(log_level, S_IWUSR | S_IRUGO,
477 show_iwmct_log_level, store_iwmct_log_level);
478 static DEVICE_ATTR(log_level_fw, S_IWUSR | S_IRUGO,
479 show_iwmct_log_level_fw, store_iwmct_log_level_fw);
482 static struct attribute *iwmct_sysfs_entries[] = {
483 &dev_attr_cc_label_fw.attr,
484 #ifdef CONFIG_IWMC3200TOP_DEBUG
485 &dev_attr_log_level.attr,
486 &dev_attr_log_level_fw.attr,
491 static struct attribute_group iwmct_attribute_group = {
492 .name = NULL, /* put in device directory */
493 .attrs = iwmct_sysfs_entries,
497 static int iwmct_probe(struct sdio_func *func,
498 const struct sdio_device_id *id)
500 struct iwmct_priv *priv;
503 int addr = IWMC_SDIO_INTR_ENABLE_ADDR;
505 dev_dbg(&func->dev, "enter iwmct_probe\n");
507 dev_dbg(&func->dev, "IRQ polling period id %u msecs, HZ is %d\n",
508 jiffies_to_msecs(2147483647), HZ);
510 priv = kzalloc(sizeof(struct iwmct_priv), GFP_KERNEL);
512 dev_err(&func->dev, "kzalloc error\n");
516 sdio_set_drvdata(func, priv);
519 /* create drivers work queue */
520 priv->wq = create_workqueue(DRV_NAME "_wq");
521 priv->bus_rescan_wq = create_workqueue(DRV_NAME "_rescan_wq");
522 INIT_WORK(&priv->bus_rescan_worker, iwmct_rescan_worker);
523 INIT_WORK(&priv->isr_worker, iwmct_irq_read_worker);
525 init_waitqueue_head(&priv->wait_q);
527 sdio_claim_host(func);
528 /* FIXME: Remove after it is fixed in the Boot ROM upgrade */
529 func->enable_timeout = 10;
531 /* In our HW, setting the block size also wakes up the boot rom. */
532 ret = sdio_set_block_size(func, priv->dbg.block_size);
534 LOG_ERROR(priv, INIT,
535 "sdio_set_block_size() failure: %d\n", ret);
536 goto error_sdio_enable;
539 ret = sdio_enable_func(func);
541 LOG_ERROR(priv, INIT, "sdio_enable_func() failure: %d\n", ret);
542 goto error_sdio_enable;
545 /* init reset and dev_sync states */
546 atomic_set(&priv->reset, 0);
547 atomic_set(&priv->dev_sync, 0);
549 /* init read req queue */
550 INIT_LIST_HEAD(&priv->read_req_list);
552 /* process configurable parameters */
553 iwmct_dbg_init_params(priv);
554 ret = sysfs_create_group(&func->dev.kobj, &iwmct_attribute_group);
556 LOG_ERROR(priv, INIT, "Failed to register attributes and "
557 "initialize module_params\n");
558 goto error_dev_attrs;
561 iwmct_dbgfs_register(priv, DRV_NAME);
563 if (!priv->dbg.direct && priv->dbg.download_trans_blks > 8) {
565 "Reducing transaction to 8 blocks = 2K (from %d)\n",
566 priv->dbg.download_trans_blks);
567 priv->dbg.download_trans_blks = 8;
569 priv->trans_len = priv->dbg.download_trans_blks * priv->dbg.block_size;
570 LOG_INFO(priv, INIT, "Transaction length = %d\n", priv->trans_len);
572 ret = sdio_claim_irq(func, iwmct_irq);
574 LOG_ERROR(priv, INIT, "sdio_claim_irq() failure: %d\n", ret);
575 goto error_claim_irq;
579 /* Enable function's interrupt */
580 sdio_writeb(priv->func, val, addr, &ret);
582 LOG_ERROR(priv, INIT, "Failure writing to "
583 "Interrupt Enable Register (%d): %d\n", addr, ret);
584 goto error_enable_int;
587 sdio_release_host(func);
589 LOG_INFO(priv, INIT, "exit iwmct_probe\n");
594 sdio_release_irq(func);
596 sdio_disable_func(func);
598 iwmct_dbgfs_unregister(priv->dbgfs);
599 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
601 sdio_release_host(func);
605 static void iwmct_remove(struct sdio_func *func)
607 struct iwmct_work_struct *read_req;
608 struct iwmct_priv *priv = sdio_get_drvdata(func);
610 priv = sdio_get_drvdata(func);
612 LOG_INFO(priv, INIT, "enter\n");
614 sdio_claim_host(func);
615 sdio_release_irq(func);
616 sdio_release_host(func);
618 /* Safely destroy osc workqueue */
619 destroy_workqueue(priv->bus_rescan_wq);
620 destroy_workqueue(priv->wq);
622 sdio_claim_host(func);
623 sdio_disable_func(func);
624 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
625 iwmct_dbgfs_unregister(priv->dbgfs);
626 sdio_release_host(func);
628 /* free read requests */
629 while (!list_empty(&priv->read_req_list)) {
630 read_req = list_entry(priv->read_req_list.next,
631 struct iwmct_work_struct, list);
633 list_del(&read_req->list);
641 static const struct sdio_device_id iwmct_ids[] = {
642 /* Intel Wireless MultiCom 3200 Top Driver */
643 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1404)},
644 { }, /* Terminating entry */
647 MODULE_DEVICE_TABLE(sdio, iwmct_ids);
649 static struct sdio_driver iwmct_driver = {
650 .probe = iwmct_probe,
651 .remove = iwmct_remove,
653 .id_table = iwmct_ids,
656 static int __init iwmct_init(void)
660 /* Default log filter settings */
661 iwmct_log_set_filter(LOG_SRC_ALL, LOG_SEV_FILTER_RUNTIME);
662 iwmct_log_set_filter(LOG_SRC_FW_MSG, LOG_SEV_FILTER_ALL);
663 iwmct_log_set_fw_filter(LOG_SRC_ALL, FW_LOG_SEV_FILTER_RUNTIME);
665 rc = sdio_register_driver(&iwmct_driver);
670 static void __exit iwmct_exit(void)
672 sdio_unregister_driver(&iwmct_driver);
675 module_init(iwmct_init);
676 module_exit(iwmct_exit);