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);
50 MODULE_FIRMWARE(FW_NAME(FW_API_VER));
53 * This workers main task is to wait for OP_OPR_ALIVE
54 * from TOP FW until ALIVE_MSG_TIMOUT timeout is elapsed.
55 * When OP_OPR_ALIVE received it will issue
56 * a call to "bus_rescan_devices".
58 static void iwmct_rescan_worker(struct work_struct *ws)
60 struct iwmct_priv *priv;
63 priv = container_of(ws, struct iwmct_priv, bus_rescan_worker);
65 LOG_INFO(priv, FW_MSG, "Calling bus_rescan\n");
67 ret = bus_rescan_devices(priv->func->dev.bus);
69 LOG_INFO(priv, INIT, "bus_rescan_devices FAILED!!!\n");
72 static void op_top_message(struct iwmct_priv *priv, struct top_msg *msg)
74 switch (msg->hdr.opcode) {
76 LOG_INFO(priv, FW_MSG, "Got ALIVE from device, wake rescan\n");
77 queue_work(priv->bus_rescan_wq, &priv->bus_rescan_worker);
80 LOG_INFO(priv, FW_MSG, "Received msg opcode 0x%X\n",
87 static void handle_top_message(struct iwmct_priv *priv, u8 *buf, int len)
91 msg = (struct top_msg *)buf;
93 if (msg->hdr.type != COMM_TYPE_D2H) {
94 LOG_ERROR(priv, FW_MSG,
95 "Message from TOP with invalid message type 0x%X\n",
100 if (len < sizeof(msg->hdr)) {
101 LOG_ERROR(priv, FW_MSG,
102 "Message from TOP is too short for message header "
103 "received %d bytes, expected at least %zd bytes\n",
104 len, sizeof(msg->hdr));
108 if (len < le16_to_cpu(msg->hdr.length) + sizeof(msg->hdr)) {
109 LOG_ERROR(priv, FW_MSG,
110 "Message length (%d bytes) is shorter than "
111 "in header (%d bytes)\n",
112 len, le16_to_cpu(msg->hdr.length));
116 switch (msg->hdr.category) {
117 case COMM_CATEGORY_OPERATIONAL:
118 op_top_message(priv, (struct top_msg *)buf);
121 case COMM_CATEGORY_DEBUG:
122 case COMM_CATEGORY_TESTABILITY:
123 case COMM_CATEGORY_DIAGNOSTICS:
124 iwmct_log_top_message(priv, buf, len);
128 LOG_ERROR(priv, FW_MSG,
129 "Message from TOP with unknown category 0x%X\n",
135 int iwmct_send_hcmd(struct iwmct_priv *priv, u8 *cmd, u16 len)
140 LOG_TRACE(priv, FW_MSG, "Sending hcmd:\n");
142 /* add padding to 256 for IWMC */
143 ((struct top_msg *)cmd)->hdr.flags |= CMD_FLAG_PADDING_256;
145 LOG_HEXDUMP(FW_MSG, cmd, len);
147 if (len > FW_HCMD_BLOCK_SIZE) {
148 LOG_ERROR(priv, FW_MSG, "size %d exceeded hcmd max size %d\n",
149 len, FW_HCMD_BLOCK_SIZE);
153 buf = kzalloc(FW_HCMD_BLOCK_SIZE, GFP_KERNEL);
155 LOG_ERROR(priv, FW_MSG, "kzalloc error, buf size %d\n",
160 memcpy(buf, cmd, len);
162 sdio_claim_host(priv->func);
163 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR, buf,
165 sdio_release_host(priv->func);
171 int iwmct_tx(struct iwmct_priv *priv, unsigned int addr,
172 void *src, int count)
176 sdio_claim_host(priv->func);
177 ret = sdio_memcpy_toio(priv->func, addr, src, count);
178 sdio_release_host(priv->func);
183 static void iwmct_irq_read_worker(struct work_struct *ws)
185 struct iwmct_priv *priv;
186 struct iwmct_work_struct *read_req;
193 priv = container_of(ws, struct iwmct_priv, isr_worker);
195 LOG_TRACE(priv, IRQ, "enter iwmct_irq_read_worker %p\n", ws);
197 /* --------------------- Handshake with device -------------------- */
198 sdio_claim_host(priv->func);
200 /* all list manipulations have to be protected by
201 * sdio_claim_host/sdio_release_host */
202 if (list_empty(&priv->read_req_list)) {
203 LOG_ERROR(priv, IRQ, "read_req_list empty in read worker\n");
207 read_req = list_entry(priv->read_req_list.next,
208 struct iwmct_work_struct, list);
210 list_del(&read_req->list);
211 iosize = read_req->iosize;
214 buf = kzalloc(iosize, GFP_KERNEL);
216 LOG_ERROR(priv, IRQ, "kzalloc error, buf size %d\n", iosize);
220 LOG_INFO(priv, IRQ, "iosize=%d, buf=%p, func=%d\n",
221 iosize, buf, priv->func->num);
223 /* read from device */
224 ret = sdio_memcpy_fromio(priv->func, buf, IWMC_SDIO_DATA_ADDR, iosize);
226 LOG_ERROR(priv, IRQ, "error %d reading buffer\n", ret);
230 LOG_HEXDUMP(IRQ, (u8 *)buf, iosize);
232 barker = le32_to_cpu(buf[0]);
234 /* Verify whether it's a barker and if not - treat as regular Rx */
235 if (barker == IWMC_BARKER_ACK ||
236 (barker & BARKER_DNLOAD_BARKER_MSK) == IWMC_BARKER_REBOOT) {
238 /* Valid Barker is equal on first 4 dwords */
239 is_barker = (buf[1] == buf[0]) &&
240 (buf[2] == buf[0]) &&
244 LOG_WARNING(priv, IRQ,
245 "Potentially inconsistent barker "
246 "%08X_%08X_%08X_%08X\n",
247 le32_to_cpu(buf[0]), le32_to_cpu(buf[1]),
248 le32_to_cpu(buf[2]), le32_to_cpu(buf[3]));
254 /* Handle Top CommHub message */
256 sdio_release_host(priv->func);
257 handle_top_message(priv, (u8 *)buf, iosize);
259 } else if (barker == IWMC_BARKER_ACK) { /* Handle barkers */
260 if (atomic_read(&priv->dev_sync) == 0) {
262 "ACK barker arrived out-of-sync\n");
266 /* Continuing to FW download (after Sync is completed)*/
267 atomic_set(&priv->dev_sync, 0);
268 LOG_INFO(priv, IRQ, "ACK barker arrived "
269 "- starting FW download\n");
270 } else { /* REBOOT barker */
271 LOG_INFO(priv, IRQ, "Recieved reboot barker: %x\n", barker);
272 priv->barker = barker;
274 if (barker & BARKER_DNLOAD_SYNC_MSK) {
275 /* Send the same barker back */
276 ret = sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR,
280 "error %d echoing barker\n", ret);
283 LOG_INFO(priv, IRQ, "Echoing barker to device\n");
284 atomic_set(&priv->dev_sync, 1);
288 /* Continuing to FW download (without Sync) */
289 LOG_INFO(priv, IRQ, "No sync requested "
290 "- starting FW download\n");
293 sdio_release_host(priv->func);
295 if (priv->dbg.fw_download)
298 LOG_ERROR(priv, IRQ, "FW download not allowed\n");
303 sdio_release_host(priv->func);
306 LOG_TRACE(priv, IRQ, "exit iwmct_irq_read_worker\n");
309 static void iwmct_irq(struct sdio_func *func)
311 struct iwmct_priv *priv;
314 int addr = IWMC_SDIO_INTR_GET_SIZE_ADDR;
315 struct iwmct_work_struct *read_req;
317 priv = sdio_get_drvdata(func);
319 LOG_TRACE(priv, IRQ, "enter iwmct_irq\n");
321 /* read the function's status register */
322 val = sdio_readb(func, IWMC_SDIO_INTR_STATUS_ADDR, &ret);
324 LOG_TRACE(priv, IRQ, "iir value = %d, ret=%d\n", val, ret);
327 LOG_ERROR(priv, IRQ, "iir = 0, exiting ISR\n");
328 goto exit_clear_intr;
333 * read 2 bytes of the transaction size
334 * IMPORTANT: sdio transaction size has to be read before clearing
337 val = sdio_readb(priv->func, addr++, &ret);
339 val = sdio_readb(priv->func, addr++, &ret);
342 LOG_INFO(priv, IRQ, "READ size %d\n", iosize);
345 LOG_ERROR(priv, IRQ, "READ size %d, exiting ISR\n", iosize);
346 goto exit_clear_intr;
349 /* allocate a work structure to pass iosize to the worker */
350 read_req = kzalloc(sizeof(struct iwmct_work_struct), GFP_KERNEL);
352 LOG_ERROR(priv, IRQ, "failed to allocate read_req, exit ISR\n");
353 goto exit_clear_intr;
356 INIT_LIST_HEAD(&read_req->list);
357 read_req->iosize = iosize;
359 list_add_tail(&priv->read_req_list, &read_req->list);
361 /* clear the function's interrupt request bit (write 1 to clear) */
362 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
364 queue_work(priv->wq, &priv->isr_worker);
366 LOG_TRACE(priv, IRQ, "exit iwmct_irq\n");
371 /* clear the function's interrupt request bit (write 1 to clear) */
372 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
377 module_param(blocks, int, 0604);
378 MODULE_PARM_DESC(blocks, "max_blocks_to_send");
381 module_param(dump, bool, 0604);
382 MODULE_PARM_DESC(dump, "dump_hex_content");
385 module_param(jump, bool, 0604);
387 static int direct = 1;
388 module_param(direct, bool, 0604);
390 static int checksum = 1;
391 module_param(checksum, bool, 0604);
393 static int fw_download = 1;
394 module_param(fw_download, bool, 0604);
396 static int block_size = IWMC_SDIO_BLK_SIZE;
397 module_param(block_size, int, 0404);
399 static int download_trans_blks = IWMC_DEFAULT_TR_BLK;
400 module_param(download_trans_blks, int, 0604);
402 static int rubbish_barker;
403 module_param(rubbish_barker, bool, 0604);
405 #ifdef CONFIG_IWMC3200TOP_DEBUG
406 static int log_level[LOG_SRC_MAX];
407 static unsigned int log_level_argc;
408 module_param_array(log_level, int, &log_level_argc, 0604);
409 MODULE_PARM_DESC(log_level, "log_level");
411 static int log_level_fw[FW_LOG_SRC_MAX];
412 static unsigned int log_level_fw_argc;
413 module_param_array(log_level_fw, int, &log_level_fw_argc, 0604);
414 MODULE_PARM_DESC(log_level_fw, "log_level_fw");
417 void iwmct_dbg_init_params(struct iwmct_priv *priv)
419 #ifdef CONFIG_IWMC3200TOP_DEBUG
422 for (i = 0; i < log_level_argc; i++) {
423 dev_notice(&priv->func->dev, "log_level[%d]=0x%X\n",
425 iwmct_log_set_filter((log_level[i] >> 8) & 0xFF,
426 log_level[i] & 0xFF);
428 for (i = 0; i < log_level_fw_argc; i++) {
429 dev_notice(&priv->func->dev, "log_level_fw[%d]=0x%X\n",
431 iwmct_log_set_fw_filter((log_level_fw[i] >> 8) & 0xFF,
432 log_level_fw[i] & 0xFF);
436 priv->dbg.blocks = blocks;
437 LOG_INFO(priv, INIT, "blocks=%d\n", blocks);
438 priv->dbg.dump = (bool)dump;
439 LOG_INFO(priv, INIT, "dump=%d\n", dump);
440 priv->dbg.jump = (bool)jump;
441 LOG_INFO(priv, INIT, "jump=%d\n", jump);
442 priv->dbg.direct = (bool)direct;
443 LOG_INFO(priv, INIT, "direct=%d\n", direct);
444 priv->dbg.checksum = (bool)checksum;
445 LOG_INFO(priv, INIT, "checksum=%d\n", checksum);
446 priv->dbg.fw_download = (bool)fw_download;
447 LOG_INFO(priv, INIT, "fw_download=%d\n", fw_download);
448 priv->dbg.block_size = block_size;
449 LOG_INFO(priv, INIT, "block_size=%d\n", block_size);
450 priv->dbg.download_trans_blks = download_trans_blks;
451 LOG_INFO(priv, INIT, "download_trans_blks=%d\n", download_trans_blks);
454 /*****************************************************************************
458 *****************************************************************************/
459 static ssize_t show_iwmct_fw_version(struct device *d,
460 struct device_attribute *attr, char *buf)
462 struct iwmct_priv *priv = dev_get_drvdata(d);
463 return sprintf(buf, "%s\n", priv->dbg.label_fw);
465 static DEVICE_ATTR(cc_label_fw, S_IRUGO, show_iwmct_fw_version, NULL);
467 #ifdef CONFIG_IWMC3200TOP_DEBUG
468 static DEVICE_ATTR(log_level, S_IWUSR | S_IRUGO,
469 show_iwmct_log_level, store_iwmct_log_level);
470 static DEVICE_ATTR(log_level_fw, S_IWUSR | S_IRUGO,
471 show_iwmct_log_level_fw, store_iwmct_log_level_fw);
474 static struct attribute *iwmct_sysfs_entries[] = {
475 &dev_attr_cc_label_fw.attr,
476 #ifdef CONFIG_IWMC3200TOP_DEBUG
477 &dev_attr_log_level.attr,
478 &dev_attr_log_level_fw.attr,
483 static struct attribute_group iwmct_attribute_group = {
484 .name = NULL, /* put in device directory */
485 .attrs = iwmct_sysfs_entries,
489 static int iwmct_probe(struct sdio_func *func,
490 const struct sdio_device_id *id)
492 struct iwmct_priv *priv;
495 int addr = IWMC_SDIO_INTR_ENABLE_ADDR;
497 dev_dbg(&func->dev, "enter iwmct_probe\n");
499 dev_dbg(&func->dev, "IRQ polling period id %u msecs, HZ is %d\n",
500 jiffies_to_msecs(2147483647), HZ);
502 priv = kzalloc(sizeof(struct iwmct_priv), GFP_KERNEL);
504 dev_err(&func->dev, "kzalloc error\n");
508 sdio_set_drvdata(func, priv);
511 /* create drivers work queue */
512 priv->wq = create_workqueue(DRV_NAME "_wq");
513 priv->bus_rescan_wq = create_workqueue(DRV_NAME "_rescan_wq");
514 INIT_WORK(&priv->bus_rescan_worker, iwmct_rescan_worker);
515 INIT_WORK(&priv->isr_worker, iwmct_irq_read_worker);
517 init_waitqueue_head(&priv->wait_q);
519 sdio_claim_host(func);
520 /* FIXME: Remove after it is fixed in the Boot ROM upgrade */
521 func->enable_timeout = 10;
523 /* In our HW, setting the block size also wakes up the boot rom. */
524 ret = sdio_set_block_size(func, priv->dbg.block_size);
526 LOG_ERROR(priv, INIT,
527 "sdio_set_block_size() failure: %d\n", ret);
528 goto error_sdio_enable;
531 ret = sdio_enable_func(func);
533 LOG_ERROR(priv, INIT, "sdio_enable_func() failure: %d\n", ret);
534 goto error_sdio_enable;
537 /* init reset and dev_sync states */
538 atomic_set(&priv->reset, 0);
539 atomic_set(&priv->dev_sync, 0);
541 /* init read req queue */
542 INIT_LIST_HEAD(&priv->read_req_list);
544 /* process configurable parameters */
545 iwmct_dbg_init_params(priv);
546 ret = sysfs_create_group(&func->dev.kobj, &iwmct_attribute_group);
548 LOG_ERROR(priv, INIT, "Failed to register attributes and "
549 "initialize module_params\n");
550 goto error_dev_attrs;
553 iwmct_dbgfs_register(priv, DRV_NAME);
555 if (!priv->dbg.direct && priv->dbg.download_trans_blks > 8) {
557 "Reducing transaction to 8 blocks = 2K (from %d)\n",
558 priv->dbg.download_trans_blks);
559 priv->dbg.download_trans_blks = 8;
561 priv->trans_len = priv->dbg.download_trans_blks * priv->dbg.block_size;
562 LOG_INFO(priv, INIT, "Transaction length = %d\n", priv->trans_len);
564 ret = sdio_claim_irq(func, iwmct_irq);
566 LOG_ERROR(priv, INIT, "sdio_claim_irq() failure: %d\n", ret);
567 goto error_claim_irq;
571 /* Enable function's interrupt */
572 sdio_writeb(priv->func, val, addr, &ret);
574 LOG_ERROR(priv, INIT, "Failure writing to "
575 "Interrupt Enable Register (%d): %d\n", addr, ret);
576 goto error_enable_int;
579 sdio_release_host(func);
581 LOG_INFO(priv, INIT, "exit iwmct_probe\n");
586 sdio_release_irq(func);
588 sdio_disable_func(func);
590 iwmct_dbgfs_unregister(priv->dbgfs);
591 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
593 sdio_release_host(func);
597 static void iwmct_remove(struct sdio_func *func)
599 struct iwmct_work_struct *read_req;
600 struct iwmct_priv *priv = sdio_get_drvdata(func);
602 priv = sdio_get_drvdata(func);
604 LOG_INFO(priv, INIT, "enter\n");
606 sdio_claim_host(func);
607 sdio_release_irq(func);
608 sdio_release_host(func);
610 /* Safely destroy osc workqueue */
611 destroy_workqueue(priv->bus_rescan_wq);
612 destroy_workqueue(priv->wq);
614 sdio_claim_host(func);
615 sdio_disable_func(func);
616 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
617 iwmct_dbgfs_unregister(priv->dbgfs);
618 sdio_release_host(func);
620 /* free read requests */
621 while (!list_empty(&priv->read_req_list)) {
622 read_req = list_entry(priv->read_req_list.next,
623 struct iwmct_work_struct, list);
625 list_del(&read_req->list);
633 static const struct sdio_device_id iwmct_ids[] = {
634 /* Intel Wireless MultiCom 3200 Top Driver */
635 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1404)},
636 { }, /* Terminating entry */
639 MODULE_DEVICE_TABLE(sdio, iwmct_ids);
641 static struct sdio_driver iwmct_driver = {
642 .probe = iwmct_probe,
643 .remove = iwmct_remove,
645 .id_table = iwmct_ids,
648 static int __init iwmct_init(void)
652 /* Default log filter settings */
653 iwmct_log_set_filter(LOG_SRC_ALL, LOG_SEV_FILTER_RUNTIME);
654 iwmct_log_set_filter(LOG_SRC_FW_MSG, LOG_SEV_FW_FILTER_ALL);
655 iwmct_log_set_fw_filter(LOG_SRC_ALL, FW_LOG_SEV_FILTER_RUNTIME);
657 rc = sdio_register_driver(&iwmct_driver);
662 static void __exit iwmct_exit(void)
664 sdio_unregister_driver(&iwmct_driver);
667 module_init(iwmct_init);
668 module_exit(iwmct_exit);