Linux 2.6.34-rc3
[pohmelfs.git] / drivers / misc / iwmc3200top / main.c
blob3b7292a5cea95916cdf4574393c6664a38bc648b
1 /*
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
19 * 02110-1301, USA.
22 * Author Name: Maxim Grabarnik <maxim.grabarnink@intel.com>
23 * -
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"
36 #include "log.h"
37 #include "fw-msg.h"
38 #include "debugfs.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 static inline int __iwmct_tx(struct iwmct_priv *priv, void *src, int count)
55 return sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR, src, count);
58 int iwmct_tx(struct iwmct_priv *priv, void *src, int count)
60 int ret;
61 sdio_claim_host(priv->func);
62 ret = __iwmct_tx(priv, src, count);
63 sdio_release_host(priv->func);
64 return ret;
67 * This workers main task is to wait for OP_OPR_ALIVE
68 * from TOP FW until ALIVE_MSG_TIMOUT timeout is elapsed.
69 * When OP_OPR_ALIVE received it will issue
70 * a call to "bus_rescan_devices".
72 static void iwmct_rescan_worker(struct work_struct *ws)
74 struct iwmct_priv *priv;
75 int ret;
77 priv = container_of(ws, struct iwmct_priv, bus_rescan_worker);
79 LOG_INFO(priv, FW_MSG, "Calling bus_rescan\n");
81 ret = bus_rescan_devices(priv->func->dev.bus);
82 if (ret < 0)
83 LOG_INFO(priv, INIT, "bus_rescan_devices FAILED!!!\n");
86 static void op_top_message(struct iwmct_priv *priv, struct top_msg *msg)
88 switch (msg->hdr.opcode) {
89 case OP_OPR_ALIVE:
90 LOG_INFO(priv, FW_MSG, "Got ALIVE from device, wake rescan\n");
91 queue_work(priv->bus_rescan_wq, &priv->bus_rescan_worker);
92 break;
93 default:
94 LOG_INFO(priv, FW_MSG, "Received msg opcode 0x%X\n",
95 msg->hdr.opcode);
96 break;
101 static void handle_top_message(struct iwmct_priv *priv, u8 *buf, int len)
103 struct top_msg *msg;
105 msg = (struct top_msg *)buf;
107 if (msg->hdr.type != COMM_TYPE_D2H) {
108 LOG_ERROR(priv, FW_MSG,
109 "Message from TOP with invalid message type 0x%X\n",
110 msg->hdr.type);
111 return;
114 if (len < sizeof(msg->hdr)) {
115 LOG_ERROR(priv, FW_MSG,
116 "Message from TOP is too short for message header "
117 "received %d bytes, expected at least %zd bytes\n",
118 len, sizeof(msg->hdr));
119 return;
122 if (len < le16_to_cpu(msg->hdr.length) + sizeof(msg->hdr)) {
123 LOG_ERROR(priv, FW_MSG,
124 "Message length (%d bytes) is shorter than "
125 "in header (%d bytes)\n",
126 len, le16_to_cpu(msg->hdr.length));
127 return;
130 switch (msg->hdr.category) {
131 case COMM_CATEGORY_OPERATIONAL:
132 op_top_message(priv, (struct top_msg *)buf);
133 break;
135 case COMM_CATEGORY_DEBUG:
136 case COMM_CATEGORY_TESTABILITY:
137 case COMM_CATEGORY_DIAGNOSTICS:
138 iwmct_log_top_message(priv, buf, len);
139 break;
141 default:
142 LOG_ERROR(priv, FW_MSG,
143 "Message from TOP with unknown category 0x%X\n",
144 msg->hdr.category);
145 break;
149 int iwmct_send_hcmd(struct iwmct_priv *priv, u8 *cmd, u16 len)
151 int ret;
152 u8 *buf;
154 LOG_TRACE(priv, FW_MSG, "Sending hcmd:\n");
156 /* add padding to 256 for IWMC */
157 ((struct top_msg *)cmd)->hdr.flags |= CMD_FLAG_PADDING_256;
159 LOG_HEXDUMP(FW_MSG, cmd, len);
161 if (len > FW_HCMD_BLOCK_SIZE) {
162 LOG_ERROR(priv, FW_MSG, "size %d exceeded hcmd max size %d\n",
163 len, FW_HCMD_BLOCK_SIZE);
164 return -1;
167 buf = kzalloc(FW_HCMD_BLOCK_SIZE, GFP_KERNEL);
168 if (!buf) {
169 LOG_ERROR(priv, FW_MSG, "kzalloc error, buf size %d\n",
170 FW_HCMD_BLOCK_SIZE);
171 return -1;
174 memcpy(buf, cmd, len);
175 ret = iwmct_tx(priv, buf, FW_HCMD_BLOCK_SIZE);
177 kfree(buf);
178 return ret;
182 static void iwmct_irq_read_worker(struct work_struct *ws)
184 struct iwmct_priv *priv;
185 struct iwmct_work_struct *read_req;
186 __le32 *buf = NULL;
187 int ret;
188 int iosize;
189 u32 barker;
190 bool is_barker;
192 priv = container_of(ws, struct iwmct_priv, isr_worker);
194 LOG_TRACE(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");
203 goto exit_release;
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;
211 kfree(read_req);
213 buf = kzalloc(iosize, GFP_KERNEL);
214 if (!buf) {
215 LOG_ERROR(priv, IRQ, "kzalloc error, buf size %d\n", iosize);
216 goto exit_release;
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);
224 if (ret) {
225 LOG_ERROR(priv, IRQ, "error %d reading buffer\n", ret);
226 goto exit_release;
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]) &&
240 (buf[3] == buf[0]);
242 if (!is_barker) {
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]));
249 } else {
250 is_barker = false;
253 /* Handle Top CommHub message */
254 if (!is_barker) {
255 sdio_release_host(priv->func);
256 handle_top_message(priv, (u8 *)buf, iosize);
257 goto exit;
258 } else if (barker == IWMC_BARKER_ACK) { /* Handle barkers */
259 if (atomic_read(&priv->dev_sync) == 0) {
260 LOG_ERROR(priv, IRQ,
261 "ACK barker arrived out-of-sync\n");
262 goto exit_release;
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 = __iwmct_tx(priv, buf, iosize);
276 if (ret) {
277 LOG_ERROR(priv, IRQ,
278 "error %d echoing barker\n", ret);
279 goto exit_release;
281 LOG_INFO(priv, IRQ, "Echoing barker to device\n");
282 atomic_set(&priv->dev_sync, 1);
283 goto exit_release;
286 /* Continuing to FW download (without Sync) */
287 LOG_INFO(priv, IRQ, "No sync requested "
288 "- starting FW download\n");
291 sdio_release_host(priv->func);
293 if (priv->dbg.fw_download)
294 iwmct_fw_load(priv);
295 else
296 LOG_ERROR(priv, IRQ, "FW download not allowed\n");
298 goto exit;
300 exit_release:
301 sdio_release_host(priv->func);
302 exit:
303 kfree(buf);
304 LOG_TRACE(priv, IRQ, "exit iwmct_irq_read_worker\n");
307 static void iwmct_irq(struct sdio_func *func)
309 struct iwmct_priv *priv;
310 int val, ret;
311 int iosize;
312 int addr = IWMC_SDIO_INTR_GET_SIZE_ADDR;
313 struct iwmct_work_struct *read_req;
315 priv = sdio_get_drvdata(func);
317 LOG_TRACE(priv, IRQ, "enter iwmct_irq\n");
319 /* read the function's status register */
320 val = sdio_readb(func, IWMC_SDIO_INTR_STATUS_ADDR, &ret);
322 LOG_TRACE(priv, IRQ, "iir value = %d, ret=%d\n", val, ret);
324 if (!val) {
325 LOG_ERROR(priv, IRQ, "iir = 0, exiting ISR\n");
326 goto exit_clear_intr;
331 * read 2 bytes of the transaction size
332 * IMPORTANT: sdio transaction size has to be read before clearing
333 * sdio interrupt!!!
335 val = sdio_readb(priv->func, addr++, &ret);
336 iosize = val;
337 val = sdio_readb(priv->func, addr++, &ret);
338 iosize += val << 8;
340 LOG_INFO(priv, IRQ, "READ size %d\n", iosize);
342 if (iosize == 0) {
343 LOG_ERROR(priv, IRQ, "READ size %d, exiting ISR\n", iosize);
344 goto exit_clear_intr;
347 /* allocate a work structure to pass iosize to the worker */
348 read_req = kzalloc(sizeof(struct iwmct_work_struct), GFP_KERNEL);
349 if (!read_req) {
350 LOG_ERROR(priv, IRQ, "failed to allocate read_req, exit ISR\n");
351 goto exit_clear_intr;
354 INIT_LIST_HEAD(&read_req->list);
355 read_req->iosize = iosize;
357 list_add_tail(&priv->read_req_list, &read_req->list);
359 /* clear the function's interrupt request bit (write 1 to clear) */
360 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
362 queue_work(priv->wq, &priv->isr_worker);
364 LOG_TRACE(priv, IRQ, "exit iwmct_irq\n");
366 return;
368 exit_clear_intr:
369 /* clear the function's interrupt request bit (write 1 to clear) */
370 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
374 static int blocks;
375 module_param(blocks, int, 0604);
376 MODULE_PARM_DESC(blocks, "max_blocks_to_send");
378 static int dump;
379 module_param(dump, bool, 0604);
380 MODULE_PARM_DESC(dump, "dump_hex_content");
382 static int jump = 1;
383 module_param(jump, bool, 0604);
385 static int direct = 1;
386 module_param(direct, bool, 0604);
388 static int checksum = 1;
389 module_param(checksum, bool, 0604);
391 static int fw_download = 1;
392 module_param(fw_download, bool, 0604);
394 static int block_size = IWMC_SDIO_BLK_SIZE;
395 module_param(block_size, int, 0404);
397 static int download_trans_blks = IWMC_DEFAULT_TR_BLK;
398 module_param(download_trans_blks, int, 0604);
400 static int rubbish_barker;
401 module_param(rubbish_barker, bool, 0604);
403 #ifdef CONFIG_IWMC3200TOP_DEBUG
404 static int log_level[LOG_SRC_MAX];
405 static unsigned int log_level_argc;
406 module_param_array(log_level, int, &log_level_argc, 0604);
407 MODULE_PARM_DESC(log_level, "log_level");
409 static int log_level_fw[FW_LOG_SRC_MAX];
410 static unsigned int log_level_fw_argc;
411 module_param_array(log_level_fw, int, &log_level_fw_argc, 0604);
412 MODULE_PARM_DESC(log_level_fw, "log_level_fw");
413 #endif
415 void iwmct_dbg_init_params(struct iwmct_priv *priv)
417 #ifdef CONFIG_IWMC3200TOP_DEBUG
418 int i;
420 for (i = 0; i < log_level_argc; i++) {
421 dev_notice(&priv->func->dev, "log_level[%d]=0x%X\n",
422 i, log_level[i]);
423 iwmct_log_set_filter((log_level[i] >> 8) & 0xFF,
424 log_level[i] & 0xFF);
426 for (i = 0; i < log_level_fw_argc; i++) {
427 dev_notice(&priv->func->dev, "log_level_fw[%d]=0x%X\n",
428 i, log_level_fw[i]);
429 iwmct_log_set_fw_filter((log_level_fw[i] >> 8) & 0xFF,
430 log_level_fw[i] & 0xFF);
432 #endif
434 priv->dbg.blocks = blocks;
435 LOG_INFO(priv, INIT, "blocks=%d\n", blocks);
436 priv->dbg.dump = (bool)dump;
437 LOG_INFO(priv, INIT, "dump=%d\n", dump);
438 priv->dbg.jump = (bool)jump;
439 LOG_INFO(priv, INIT, "jump=%d\n", jump);
440 priv->dbg.direct = (bool)direct;
441 LOG_INFO(priv, INIT, "direct=%d\n", direct);
442 priv->dbg.checksum = (bool)checksum;
443 LOG_INFO(priv, INIT, "checksum=%d\n", checksum);
444 priv->dbg.fw_download = (bool)fw_download;
445 LOG_INFO(priv, INIT, "fw_download=%d\n", fw_download);
446 priv->dbg.block_size = block_size;
447 LOG_INFO(priv, INIT, "block_size=%d\n", block_size);
448 priv->dbg.download_trans_blks = download_trans_blks;
449 LOG_INFO(priv, INIT, "download_trans_blks=%d\n", download_trans_blks);
452 /*****************************************************************************
454 * sysfs attributes
456 *****************************************************************************/
457 static ssize_t show_iwmct_fw_version(struct device *d,
458 struct device_attribute *attr, char *buf)
460 struct iwmct_priv *priv = dev_get_drvdata(d);
461 return sprintf(buf, "%s\n", priv->dbg.label_fw);
463 static DEVICE_ATTR(cc_label_fw, S_IRUGO, show_iwmct_fw_version, NULL);
465 #ifdef CONFIG_IWMC3200TOP_DEBUG
466 static DEVICE_ATTR(log_level, S_IWUSR | S_IRUGO,
467 show_iwmct_log_level, store_iwmct_log_level);
468 static DEVICE_ATTR(log_level_fw, S_IWUSR | S_IRUGO,
469 show_iwmct_log_level_fw, store_iwmct_log_level_fw);
470 #endif
472 static struct attribute *iwmct_sysfs_entries[] = {
473 &dev_attr_cc_label_fw.attr,
474 #ifdef CONFIG_IWMC3200TOP_DEBUG
475 &dev_attr_log_level.attr,
476 &dev_attr_log_level_fw.attr,
477 #endif
478 NULL
481 static struct attribute_group iwmct_attribute_group = {
482 .name = NULL, /* put in device directory */
483 .attrs = iwmct_sysfs_entries,
487 static int iwmct_probe(struct sdio_func *func,
488 const struct sdio_device_id *id)
490 struct iwmct_priv *priv;
491 int ret;
492 int val = 1;
493 int addr = IWMC_SDIO_INTR_ENABLE_ADDR;
495 dev_dbg(&func->dev, "enter iwmct_probe\n");
497 dev_dbg(&func->dev, "IRQ polling period id %u msecs, HZ is %d\n",
498 jiffies_to_msecs(2147483647), HZ);
500 priv = kzalloc(sizeof(struct iwmct_priv), GFP_KERNEL);
501 if (!priv) {
502 dev_err(&func->dev, "kzalloc error\n");
503 return -ENOMEM;
505 priv->func = func;
506 sdio_set_drvdata(func, priv);
509 /* create drivers work queue */
510 priv->wq = create_workqueue(DRV_NAME "_wq");
511 priv->bus_rescan_wq = create_workqueue(DRV_NAME "_rescan_wq");
512 INIT_WORK(&priv->bus_rescan_worker, iwmct_rescan_worker);
513 INIT_WORK(&priv->isr_worker, iwmct_irq_read_worker);
515 init_waitqueue_head(&priv->wait_q);
517 sdio_claim_host(func);
518 /* FIXME: Remove after it is fixed in the Boot ROM upgrade */
519 func->enable_timeout = 10;
521 /* In our HW, setting the block size also wakes up the boot rom. */
522 ret = sdio_set_block_size(func, priv->dbg.block_size);
523 if (ret) {
524 LOG_ERROR(priv, INIT,
525 "sdio_set_block_size() failure: %d\n", ret);
526 goto error_sdio_enable;
529 ret = sdio_enable_func(func);
530 if (ret) {
531 LOG_ERROR(priv, INIT, "sdio_enable_func() failure: %d\n", ret);
532 goto error_sdio_enable;
535 /* init reset and dev_sync states */
536 atomic_set(&priv->reset, 0);
537 atomic_set(&priv->dev_sync, 0);
539 /* init read req queue */
540 INIT_LIST_HEAD(&priv->read_req_list);
542 /* process configurable parameters */
543 iwmct_dbg_init_params(priv);
544 ret = sysfs_create_group(&func->dev.kobj, &iwmct_attribute_group);
545 if (ret) {
546 LOG_ERROR(priv, INIT, "Failed to register attributes and "
547 "initialize module_params\n");
548 goto error_dev_attrs;
551 iwmct_dbgfs_register(priv, DRV_NAME);
553 if (!priv->dbg.direct && priv->dbg.download_trans_blks > 8) {
554 LOG_INFO(priv, INIT,
555 "Reducing transaction to 8 blocks = 2K (from %d)\n",
556 priv->dbg.download_trans_blks);
557 priv->dbg.download_trans_blks = 8;
559 priv->trans_len = priv->dbg.download_trans_blks * priv->dbg.block_size;
560 LOG_INFO(priv, INIT, "Transaction length = %d\n", priv->trans_len);
562 ret = sdio_claim_irq(func, iwmct_irq);
563 if (ret) {
564 LOG_ERROR(priv, INIT, "sdio_claim_irq() failure: %d\n", ret);
565 goto error_claim_irq;
569 /* Enable function's interrupt */
570 sdio_writeb(priv->func, val, addr, &ret);
571 if (ret) {
572 LOG_ERROR(priv, INIT, "Failure writing to "
573 "Interrupt Enable Register (%d): %d\n", addr, ret);
574 goto error_enable_int;
577 sdio_release_host(func);
579 LOG_INFO(priv, INIT, "exit iwmct_probe\n");
581 return ret;
583 error_enable_int:
584 sdio_release_irq(func);
585 error_claim_irq:
586 sdio_disable_func(func);
587 error_dev_attrs:
588 iwmct_dbgfs_unregister(priv->dbgfs);
589 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
590 error_sdio_enable:
591 sdio_release_host(func);
592 return ret;
595 static void iwmct_remove(struct sdio_func *func)
597 struct iwmct_work_struct *read_req;
598 struct iwmct_priv *priv = sdio_get_drvdata(func);
600 LOG_INFO(priv, INIT, "enter\n");
602 sdio_claim_host(func);
603 sdio_release_irq(func);
604 sdio_release_host(func);
606 /* Safely destroy osc workqueue */
607 destroy_workqueue(priv->bus_rescan_wq);
608 destroy_workqueue(priv->wq);
610 sdio_claim_host(func);
611 sdio_disable_func(func);
612 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
613 iwmct_dbgfs_unregister(priv->dbgfs);
614 sdio_release_host(func);
616 /* free read requests */
617 while (!list_empty(&priv->read_req_list)) {
618 read_req = list_entry(priv->read_req_list.next,
619 struct iwmct_work_struct, list);
621 list_del(&read_req->list);
622 kfree(read_req);
625 kfree(priv);
629 static const struct sdio_device_id iwmct_ids[] = {
630 /* Intel Wireless MultiCom 3200 Top Driver */
631 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1404)},
632 { }, /* Terminating entry */
635 MODULE_DEVICE_TABLE(sdio, iwmct_ids);
637 static struct sdio_driver iwmct_driver = {
638 .probe = iwmct_probe,
639 .remove = iwmct_remove,
640 .name = DRV_NAME,
641 .id_table = iwmct_ids,
644 static int __init iwmct_init(void)
646 int rc;
648 /* Default log filter settings */
649 iwmct_log_set_filter(LOG_SRC_ALL, LOG_SEV_FILTER_RUNTIME);
650 iwmct_log_set_filter(LOG_SRC_FW_MSG, LOG_SEV_FW_FILTER_ALL);
651 iwmct_log_set_fw_filter(LOG_SRC_ALL, FW_LOG_SEV_FILTER_RUNTIME);
653 rc = sdio_register_driver(&iwmct_driver);
655 return rc;
658 static void __exit iwmct_exit(void)
660 sdio_unregister_driver(&iwmct_driver);
663 module_init(iwmct_init);
664 module_exit(iwmct_exit);