2 * hif2.c - HIF layer re-implementation for the Linux SDIO stack
4 * Copyright (C) 2008, 2009 by OpenMoko, Inc.
5 * Written by Werner Almesberger <werner@openmoko.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
14 * @abstract: HIF layer reference implementation for Atheros SDIO stack
15 * @notice: Copyright (c) 2004-2006 Atheros Communications Inc.
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/mmc/sdio.h>
28 #include <linux/mmc/sdio_ids.h>
35 /* @@@ Hack - this wants cleaning up */
37 #ifdef CONFIG_MACH_NEO1973_GTA02
39 #include <mach/gta02-pm-wlan.h>
41 #else /* CONFIG_MACH_NEO1973_GTA02 */
43 #define gta02_wlan_query_rfkill_lock() 1
44 #define gta02_wlan_set_rfkill_cb(cb, hif) ((void) cb)
45 #define gta02_wlan_query_rfkill_unlock()
46 #define gta02_wlan_clear_rfkill_cb()
48 #endif /* !CONFIG_MACH_NEO1973_GTA02 */
54 * - HIF_DEVICE_IRQ_ASYNC_SYNC doesn't work yet (gets MMC errors)
55 * - latency can reach hundreds of ms, probably because of scheduling delays
56 * - packets go through about three queues before finally hitting the network
60 * Differences from Atheros' HIFs:
62 * - synchronous and asynchronous requests may get reordered with respect to
63 * each other, e.g., if HIFReadWrite returns for an asynchronous request and
64 * then HIFReadWrite is called for a synchronous request, the synchronous
65 * request may be executed before the asynchronous request.
67 * - request queue locking seems unnecessarily complex in the Atheros HIFs.
69 * - Atheros mask interrupts by calling sdio_claim_irq/sdio_release_irq, which
70 * can cause quite a bit of overhead. This HIF has its own light-weight
73 * - Atheros call deviceInsertedHandler from a thread spawned off the probe or
74 * device insertion function. The original explanation for the Atheros SDIO
75 * stack said that this is done because a delay is needed to let the chip
76 * complete initialization. There is indeed a one second delay in the thread.
78 * The Atheros Linux SDIO HIF removes the delay and only retains the thread.
79 * Experimentally removing the thread didn't show any conflicts, so let's get
82 * - The Atheros SDIO stack with Samuel's driver sets SDIO_CCCR_POWER in
83 * SDIO_POWER_EMPC. Atheros' Linux SDIO code apparently doesn't. We don't
84 * either, and this seems to work fine.
85 * @@@ Need to check this with Atheros.
91 #define HIF_MBOX_BLOCK_SIZE 128
92 #define HIF_MBOX_BASE_ADDR 0x800
93 #define HIF_MBOX_WIDTH 0x800
94 #define HIF_MBOX_START_ADDR(mbox) \
95 (HIF_MBOX_BASE_ADDR+(mbox)*HIF_MBOX_WIDTH)
100 struct sdio_func *func;
103 * @@@ our sweet little bit of bogosity - the mechanism that lets us
104 * use the SDIO stack from softirqs. This really wants to use skbs.
106 struct list_head queue;
107 spinlock_t queue_lock;
108 struct task_struct *io_task;
109 wait_queue_head_t wait;
112 * activate_lock protects "active" and the activation/deactivation
115 * Relation to other locks: The SDIO function can be claimed while
116 * activate_lock is being held, but trying to acquire activate_lock
117 * while having ownership of the SDIO function could cause a deadlock.
120 struct mutex activate_lock;
124 struct list_head list;
125 struct sdio_func *func;
126 int (*read)(struct sdio_func *func,
127 void *dst, unsigned int addr, int count);
128 int (*write)(struct sdio_func *func,
129 unsigned int addr, void *src, int count);
133 A_STATUS (*completion)(void *context, A_STATUS status);
138 static HTC_CALLBACKS htcCallbacks;
141 * shutdown_lock prevents recursion through HIFShutDownDevice
143 static DEFINE_MUTEX(shutdown_lock);
146 /* ----- Request processing ------------------------------------------------ */
149 static A_STATUS process_request(struct hif_request *req)
154 dev_dbg(&req->func->dev, "process_request(req %p)\n", req);
155 sdio_claim_host(req->func);
157 ret = req->read(req->func, req->buf, req->addr, req->len);
159 ret = req->write(req->func, req->addr, req->buf, req->len);
161 sdio_release_host(req->func);
162 status = ret ? A_ERROR : A_OK;
164 req->completion(req->context, status);
170 static void enqueue_request(struct hif_device *hif, struct hif_request *req)
174 dev_dbg(&req->func->dev, "enqueue_request(req %p)\n", req);
175 spin_lock_irqsave(&hif->queue_lock, flags);
176 list_add_tail(&req->list, &hif->queue);
177 spin_unlock_irqrestore(&hif->queue_lock, flags);
182 static struct hif_request *dequeue_request(struct hif_device *hif)
184 struct hif_request *req;
187 spin_lock_irqsave(&hif->queue_lock, flags);
188 if (list_empty(&hif->queue))
191 req = list_first_entry(&hif->queue,
192 struct hif_request, list);
193 list_del(&req->list);
195 spin_unlock_irqrestore(&hif->queue_lock, flags);
200 static void wait_queue_empty(struct hif_device *hif)
206 spin_lock_irqsave(&hif->queue_lock, flags);
207 empty = list_empty(&hif->queue);
208 spin_unlock_irqrestore(&hif->queue_lock, flags);
217 static int io(void *data)
219 struct hif_device *hif = data;
220 struct sched_param param = { .sched_priority = 2 };
221 /* one priority level slower than ksdioirqd (which is at 1) */
223 struct hif_request *req;
225 sched_setscheduler(current, SCHED_FIFO, ¶m);
230 * Since we never use signals here, one might think
231 * that this ought to be TASK_UNINTERRUPTIBLE. However,
232 * such a task would increase the load average and,
233 * worse, it would trigger the softlockup check.
235 prepare_to_wait(&hif->wait, &wait, TASK_INTERRUPTIBLE);
236 if (kthread_should_stop()) {
237 finish_wait(&hif->wait, &wait);
240 req = dequeue_request(hif);
245 finish_wait(&hif->wait, &wait);
247 (void) process_request(req);
253 A_STATUS HIFReadWrite(HIF_DEVICE *hif, A_UINT32 address, A_UCHAR *buffer,
254 A_UINT32 length, A_UINT32 request, void *context)
256 struct device *dev = HIFGetOSDevice(hif);
257 struct hif_request *req;
259 dev_dbg(dev, "HIFReadWrite(device %p, address 0x%x, buffer %p, "
260 "length %d, request 0x%x, context %p)\n",
261 hif, address, buffer, length, request, context);
263 BUG_ON(!(request & (HIF_SYNCHRONOUS | HIF_ASYNCHRONOUS)));
264 BUG_ON(!(request & (HIF_BYTE_BASIS | HIF_BLOCK_BASIS)));
265 BUG_ON(!(request & (HIF_READ | HIF_WRITE)));
266 BUG_ON(!(request & HIF_EXTENDED_IO));
268 if (address >= HIF_MBOX_START_ADDR(0) &&
269 address < HIF_MBOX_START_ADDR(MBOXES+1)) {
270 BUG_ON(length > HIF_MBOX_WIDTH);
271 /* Adjust the address so that the last byte falls on the EOM
273 address += HIF_MBOX_WIDTH-length;
276 req = kzalloc(sizeof(*req), GFP_ATOMIC);
278 if (request & HIF_ASYNCHRONOUS)
279 htcCallbacks.rwCompletionHandler(context, A_ERROR);
283 req->func = hif->func;
288 if (request & HIF_READ) {
289 if (request & HIF_FIXED_ADDRESS)
290 req->read = sdio_readsb;
292 req->read = sdio_memcpy_fromio;
294 if (request & HIF_FIXED_ADDRESS)
295 req->write = sdio_writesb;
297 req->write = sdio_memcpy_toio;
300 if (!(request & HIF_ASYNCHRONOUS))
301 return process_request(req);
303 req->completion = htcCallbacks.rwCompletionHandler;
304 req->context = context;
305 enqueue_request(hif, req);
311 /* ----- Interrupt handling ------------------------------------------------ */
314 * Volatile ought to be good enough to make gcc do the right thing on S3C24xx.
315 * No need to use atomic or put barriers, keeping the code more readable.
317 * Warning: this story changes if going SMP/SMT.
320 static volatile int masked = 1;
321 static volatile int pending;
322 static volatile int in_interrupt;
325 static void ar6000_do_irq(struct sdio_func *func)
327 HIF_DEVICE *hif = sdio_get_drvdata(func);
328 struct device *dev = HIFGetOSDevice(hif);
331 dev_dbg(dev, "ar6000_do_irq -> %p\n", htcCallbacks.dsrHandler);
333 status = htcCallbacks.dsrHandler(hif->htc_handle);
334 BUG_ON(status != A_OK);
338 static void sdio_ar6000_irq(struct sdio_func *func)
340 HIF_DEVICE *hif = sdio_get_drvdata(func);
341 struct device *dev = HIFGetOSDevice(hif);
343 dev_dbg(dev, "sdio_ar6000_irq\n");
352 * @@@ This is ugly. If we don't drop the lock, we'll deadlock when
353 * the handler tries to do SDIO. So there are four choices:
355 * 1) Break the call chain by calling the callback from a workqueue.
357 * 2) Make process_request aware that we already have the lock.
358 * 3) Drop the lock. Which is ugly but should be safe as long as we're
359 * making sure the device doesn't go away.
360 * 4) Change the AR6k driver such that it only issues asynchronous
361 * quests when called from an interrupt.
363 * Solution 2) is probably the best for now. Will try it later.
365 sdio_release_host(func);
367 sdio_claim_host(func);
372 void HIFAckInterrupt(HIF_DEVICE *hif)
374 struct device *dev = HIFGetOSDevice(hif);
376 dev_dbg(dev, "HIFAckInterrupt\n");
381 void HIFUnMaskInterrupt(HIF_DEVICE *hif)
383 struct device *dev = HIFGetOSDevice(hif);
385 dev_dbg(dev, "HIFUnMaskInterrupt\n");
390 ar6000_do_irq(hif->func);
391 /* We may take an interrupt before unmasking and thus
392 get it pending. In this case, we just loop back. */
400 void HIFMaskInterrupt(HIF_DEVICE *hif)
402 struct device *dev = HIFGetOSDevice(hif);
404 dev_dbg(dev, "HIFMaskInterrupt\n");
406 * Since sdio_ar6000_irq can also be called from a process context, we
407 * may conceivably end up racing with it. Thus, we need to wait until
408 * we can be sure that no concurrent interrupt processing is going on
411 * Note: this may be a bit on the paranoid side - the callers may
412 * actually be nice enough to disable scheduling. Check later.
420 /* ----- HIF API glue functions -------------------------------------------- */
423 struct device *HIFGetOSDevice(HIF_DEVICE *hif)
425 return &hif->func->dev;
429 void HIFSetHandle(void *hif_handle, void *handle)
431 HIF_DEVICE *hif = (HIF_DEVICE *) hif_handle;
433 hif->htc_handle = handle;
437 /* ----- Device configuration (HIF side) ----------------------------------- */
440 A_STATUS HIFConfigureDevice(HIF_DEVICE *hif,
441 HIF_DEVICE_CONFIG_OPCODE opcode, void *config, A_UINT32 configLen)
443 struct device *dev = HIFGetOSDevice(hif);
444 HIF_DEVICE_IRQ_PROCESSING_MODE *ipm_cfg = config;
445 A_UINT32 *mbs_cfg = config;
448 dev_dbg(dev, "HIFConfigureDevice\n");
451 case HIF_DEVICE_GET_MBOX_BLOCK_SIZE:
452 for (i = 0; i != MBOXES; i++)
453 mbs_cfg[i] = HIF_MBOX_BLOCK_SIZE;
455 case HIF_DEVICE_GET_MBOX_ADDR:
456 for (i = 0; i != MBOXES; i++)
457 mbs_cfg[i] = HIF_MBOX_START_ADDR(i);
459 case HIF_DEVICE_GET_IRQ_PROC_MODE:
460 *ipm_cfg = HIF_DEVICE_IRQ_SYNC_ONLY;
461 // *ipm_cfg = HIF_DEVICE_IRQ_ASYNC_SYNC;
470 /* ----- Device probe and removal (Linux side) ----------------------------- */
473 static int ar6000_do_activate(struct hif_device *hif)
475 struct sdio_func *func = hif->func;
476 struct device *dev = &func->dev;
479 dev_dbg(dev, "ar6000_do_activate\n");
481 sdio_claim_host(func);
482 sdio_enable_func(func);
484 INIT_LIST_HEAD(&hif->queue);
485 init_waitqueue_head(&hif->wait);
486 spin_lock_init(&hif->queue_lock);
488 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
490 dev_err(dev, "sdio_set_block_size returns %d\n", ret);
493 ret = sdio_claim_irq(func, sdio_ar6000_irq);
495 dev_err(dev, "sdio_claim_irq returns %d\n", ret);
498 /* Set SDIO_BUS_CD_DISABLE in SDIO_CCCR_IF ? */
500 sdio_f0_writeb(func, SDIO_CCCR_CAP_E4MI, SDIO_CCCR_CAPS, &ret);
502 dev_err(dev, "sdio_f0_writeb(SDIO_CCCR_CAPS) returns %d\n",
507 if (0) /* avoid warning */
511 sdio_release_host(func);
513 hif->io_task = kthread_run(io, hif, "ar6000_io");
514 ret = IS_ERR(hif->io_task);
516 dev_err(dev, "kthread_run(ar6000_io): %d\n", ret);
520 ret = htcCallbacks.deviceInsertedHandler(hif);
524 dev_err(dev, "deviceInsertedHandler: %d\n", ret);
526 ret = kthread_stop(hif->io_task);
528 dev_err(dev, "kthread_stop (ar6000_io): %d\n", ret);
531 sdio_claim_host(func);
534 sdio_release_irq(func);
537 sdio_disable_func(func);
538 sdio_release_host(func);
544 static void ar6000_do_deactivate(struct hif_device *hif)
546 struct sdio_func *func = hif->func;
547 struct device *dev = &func->dev;
550 dev_dbg(dev, "ar6000_do_deactivate\n");
554 if (mutex_trylock(&shutdown_lock)) {
556 * Funny, Atheros' HIF does this call, but this just puts us in
557 * a recursion through HTCShutDown/HIFShutDown if unloading the
560 * However, we need it for suspend/resume. See the comment at
561 * HIFShutDown, below.
563 ret = htcCallbacks.deviceRemovedHandler(hif->htc_handle, A_OK);
565 dev_err(dev, "deviceRemovedHandler: %d\n", ret);
566 mutex_unlock(&shutdown_lock);
568 wait_queue_empty(hif);
569 ret = kthread_stop(hif->io_task);
571 dev_err(dev, "kthread_stop (ar6000_io): %d\n", ret);
572 sdio_claim_host(func);
573 sdio_release_irq(func);
574 sdio_disable_func(func);
575 sdio_release_host(func);
579 static int ar6000_activate(struct hif_device *hif)
583 dev_dbg(&hif->func->dev, "ar6000_activate\n");
584 mutex_lock(&hif->activate_lock);
586 ret = ar6000_do_activate(hif);
588 printk(KERN_ERR "%s: Failed to activate %d\n",
595 mutex_unlock(&hif->activate_lock);
600 static void ar6000_deactivate(struct hif_device *hif)
602 dev_dbg(&hif->func->dev, "ar6000_deactivate\n");
603 mutex_lock(&hif->activate_lock);
605 ar6000_do_deactivate(hif);
608 mutex_unlock(&hif->activate_lock);
612 static int ar6000_rfkill_cb(void *data, int on)
614 struct hif_device *hif = data;
615 struct sdio_func *func = hif->func;
616 struct device *dev = &func->dev;
618 dev_dbg(dev, "ar6000_rfkill_cb: on %d\n", on);
620 return ar6000_activate(hif);
621 ar6000_deactivate(hif);
626 static int sdio_ar6000_probe(struct sdio_func *func,
627 const struct sdio_device_id *id)
629 struct device *dev = &func->dev;
630 struct hif_device *hif;
633 dev_dbg(dev, "sdio_ar6000_probe\n");
634 BUG_ON(!htcCallbacks.deviceInsertedHandler);
636 hif = kzalloc(sizeof(*hif), GFP_KERNEL);
640 sdio_set_drvdata(func, hif);
642 mutex_init(&hif->activate_lock);
645 if (gta02_wlan_query_rfkill_lock())
646 ret = ar6000_activate(hif);
648 gta02_wlan_set_rfkill_cb(ar6000_rfkill_cb, hif);
651 gta02_wlan_query_rfkill_unlock();
652 sdio_set_drvdata(func, NULL);
658 static void sdio_ar6000_remove(struct sdio_func *func)
660 struct device *dev = &func->dev;
661 HIF_DEVICE *hif = sdio_get_drvdata(func);
663 dev_dbg(dev, "sdio_ar6000_remove\n");
664 gta02_wlan_clear_rfkill_cb();
665 ar6000_deactivate(hif);
666 sdio_set_drvdata(func, NULL);
671 /* ----- Device registration/unregistration (called by HIF) ---------------- */
674 #define ATHEROS_SDIO_DEVICE(id, offset) \
675 SDIO_DEVICE(SDIO_VENDOR_ID_ATHEROS, SDIO_DEVICE_ID_ATHEROS_##id | (offset))
677 static const struct sdio_device_id sdio_ar6000_ids[] = {
678 { ATHEROS_SDIO_DEVICE(AR6002, 0) },
679 { ATHEROS_SDIO_DEVICE(AR6002, 0x1) },
680 { ATHEROS_SDIO_DEVICE(AR6001, 0x8) },
681 { ATHEROS_SDIO_DEVICE(AR6001, 0x9) },
682 { ATHEROS_SDIO_DEVICE(AR6001, 0xa) },
683 { ATHEROS_SDIO_DEVICE(AR6001, 0xb) },
684 { /* end: all zeroes */ },
687 MODULE_DEVICE_TABLE(sdio, sdio_ar6000_ids);
690 static struct sdio_driver sdio_ar6000_driver = {
691 .probe = sdio_ar6000_probe,
692 .remove = sdio_ar6000_remove,
693 .name = "sdio_ar6000",
694 .id_table = sdio_ar6000_ids,
698 int HIFInit(HTC_CALLBACKS *callbacks)
704 printk(KERN_DEBUG "HIFInit\n");
705 htcCallbacks = *callbacks;
707 ret = sdio_register_driver(&sdio_ar6000_driver);
710 "sdio_register_driver(sdio_ar6000_driver): %d\n", ret);
719 * We have four possible call chains here:
721 * System shutdown/reboot:
723 * kernel_restart_prepare ...> device_shutdown ... > s3cmci_shutdown ->
724 * mmc_remove_host ..> sdio_bus_remove -> sdio_ar6000_remove ->
725 * ar6000_deactivate -> ar6000_do_deactivate ->
726 * deviceRemovedHandler (HTCTargetRemovedHandler) -> HIFShutDownDevice
728 * This is roughly the same sequence as suspend, described below.
732 * sys_delete_module -> ar6000_cleanup_module -> HTCShutDown ->
733 * HIFShutDownDevice -> sdio_unregister_driver ...> sdio_bus_remove ->
734 * sdio_ar6000_remove -> ar6000_deactivate -> ar6000_do_deactivate
736 * In this case, HIFShutDownDevice must call sdio_unregister_driver to
737 * notify the driver about its removal. ar6000_do_deactivate must not call
738 * deviceRemovedHandler, because that would loop back into HIFShutDownDevice.
742 * device_suspend ...> s3cmci_suspend ...> sdio_bus_remove ->
743 * sdio_ar6000_remove -> ar6000_deactivate -> ar6000_do_deactivate ->
744 * deviceRemovedHandler (HTCTargetRemovedHandler) -> HIFShutDownDevice
746 * We must call deviceRemovedHandler to inform the ar6k stack that the device
747 * has been removed. Since HTCTargetRemovedHandler calls back into
748 * HIFShutDownDevice, we must also prevent the call to
749 * sdio_unregister_driver, or we'd end up recursing into the SDIO stack,
750 * eventually deadlocking somewhere.
754 * rfkill_state_store -> rfkill_toggle_radio -> gta02_wlan_toggle_radio ->
755 * ar6000_rfkill_cb -> ar6000_deactivate -> ar6000_do_deactivate ->
756 * deviceRemovedHandler (HTCTargetRemovedHandler) -> HIFShutDownDevice
758 * This is similar to suspend - only the entry point changes.
761 void HIFShutDownDevice(HIF_DEVICE *hif)
763 /* Beware, HTCShutDown calls us with hif == NULL ! */
764 if (mutex_trylock(&shutdown_lock)) {
765 sdio_unregister_driver(&sdio_ar6000_driver);
766 mutex_unlock(&shutdown_lock);