1 //------------------------------------------------------------------------------
2 // <copyright file="hif.c" company="Atheros">
3 // Copyright (c) 2004-2010 Atheros Corporation. All rights reserved.
6 // Permission to use, copy, modify, and/or distribute this software for any
7 // purpose with or without fee is hereby granted, provided that the above
8 // copyright notice and this permission notice appear in all copies.
10 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // HIF layer reference implementation for Linux Native MMC stack
23 // Author(s): ="Atheros"
24 //==============================================================================
25 #include <linux/mmc/card.h>
26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/sdio_func.h>
29 #include <linux/mmc/sdio_ids.h>
30 #include <linux/mmc/sdio.h>
31 #include <linux/mmc/sd.h>
32 #include <linux/kthread.h>
34 /* by default setup a bounce buffer for the data packets, if the underlying host controller driver
35 does not use DMA you may be able to skip this step and save the memory allocation and transfer time */
36 #define HIF_USE_DMA_BOUNCE_BUFFER 1
37 #include "hif_internal.h"
38 #define ATH_MODULE_NAME hif
40 #include "AR6002/hw2.0/hw/mbox_host_reg.h"
42 #if HIF_USE_DMA_BOUNCE_BUFFER
43 /* macro to check if DMA buffer is WORD-aligned and DMA-able. Most host controllers assume the
44 * buffer is DMA'able and will bug-check otherwise (i.e. buffers on the stack).
45 * virt_addr_valid check fails on stack memory.
47 #define BUFFER_NEEDS_BOUNCE(buffer) (((unsigned long)(buffer) & 0x3) || !virt_addr_valid((buffer)))
49 #define BUFFER_NEEDS_BOUNCE(buffer) (false)
53 #if defined(CONFIG_PM)
54 #define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
55 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
56 #endif /* CONFIG_PM */
57 static void delHifDevice(struct hif_device * device);
58 static int Func0_CMD52WriteByte(struct mmc_card *card, unsigned int address, unsigned char byte);
59 static int Func0_CMD52ReadByte(struct mmc_card *card, unsigned int address, unsigned char *byte);
61 static int hifEnableFunc(struct hif_device *device, struct sdio_func *func);
62 static int hifDisableFunc(struct hif_device *device, struct sdio_func *func);
63 OSDRV_CALLBACKS osdrvCallbacks;
65 int reset_sdio_on_unload = 0;
66 module_param(reset_sdio_on_unload, int, 0644);
68 extern u32 nohifscattersupport;
70 static struct hif_device *ath6kl_alloc_hifdev(struct sdio_func *func)
72 struct hif_device *hifdevice;
74 hifdevice = kzalloc(sizeof(struct hif_device), GFP_KERNEL);
76 #if HIF_USE_DMA_BOUNCE_BUFFER
77 hifdevice->dma_buffer = kmalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
79 hifdevice->func = func;
80 hifdevice->powerConfig = HIF_DEVICE_POWER_UP;
81 sdio_set_drvdata(func, hifdevice);
86 static struct hif_device *ath6kl_get_hifdev(struct sdio_func *func)
88 return (struct hif_device *) sdio_get_drvdata(func);
91 static const struct sdio_device_id ath6kl_hifdev_ids[] = {
92 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x0)) },
93 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x1)) },
94 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0)) },
95 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1)) },
99 MODULE_DEVICE_TABLE(sdio, ath6kl_hifdev_ids);
101 static int ath6kl_hifdev_probe(struct sdio_func *func,
102 const struct sdio_device_id *id)
105 struct hif_device *device;
108 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
109 ("ath6kl: Function: 0x%X, Vendor ID: 0x%X, "
110 "Device ID: 0x%X, block size: 0x%X/0x%X\n",
111 func->num, func->vendor, func->device,
112 func->max_blksize, func->cur_blksize));
114 ath6kl_alloc_hifdev(func);
115 device = ath6kl_get_hifdev(func);
118 device->is_disabled = true;
120 spin_lock_init(&device->lock);
121 spin_lock_init(&device->asynclock);
123 DL_LIST_INIT(&device->ScatterReqHead);
125 /* Try to allow scatter unless globally overridden */
126 if (!nohifscattersupport)
127 device->scatter_enabled = true;
129 A_MEMZERO(device->busRequest, sizeof(device->busRequest));
131 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++) {
132 sema_init(&device->busRequest[count].sem_req, 0);
133 hifFreeBusRequest(device, &device->busRequest[count]);
136 sema_init(&device->sem_async, 0);
138 ret = hifEnableFunc(device, func);
143 static void ath6kl_hifdev_remove(struct sdio_func *func)
146 struct hif_device *device;
148 device = ath6kl_get_hifdev(func);
149 if (device->claimedContext != NULL)
150 status = osdrvCallbacks.
151 deviceRemovedHandler(device->claimedContext, device);
153 if (device->is_disabled)
154 device->is_disabled = false;
156 status = hifDisableFunc(device, func);
158 CleanupHIFScatterResources(device);
160 delHifDevice(device);
163 #if defined(CONFIG_PM)
164 static int ath6kl_hifdev_suspend(struct device *dev)
166 struct sdio_func *func = dev_to_sdio_func(dev);
168 struct hif_device *device;
170 device = ath6kl_get_hifdev(func);
172 if (device && device->claimedContext &&
173 osdrvCallbacks.deviceSuspendHandler) {
174 /* set true first for PowerStateChangeNotify(..) */
175 device->is_suspend = true;
176 status = osdrvCallbacks.
177 deviceSuspendHandler(device->claimedContext);
179 device->is_suspend = false;
182 CleanupHIFScatterResources(device);
188 /* Hack for kernel in order to support deep sleep and wow */
195 static int ath6kl_hifdev_resume(struct device *dev)
197 struct sdio_func *func = dev_to_sdio_func(dev);
199 struct hif_device *device;
201 device = ath6kl_get_hifdev(func);
202 if (device && device->claimedContext &&
203 osdrvCallbacks.deviceSuspendHandler) {
204 status = osdrvCallbacks.
205 deviceResumeHandler(device->claimedContext);
207 device->is_suspend = false;
213 static const struct dev_pm_ops ath6kl_hifdev_pmops = {
214 .suspend = ath6kl_hifdev_suspend,
215 .resume = ath6kl_hifdev_resume,
217 #endif /* CONFIG_PM */
219 static struct sdio_driver ath6kl_hifdev_driver = {
220 .name = "ath6kl_hifdev",
221 .id_table = ath6kl_hifdev_ids,
222 .probe = ath6kl_hifdev_probe,
223 .remove = ath6kl_hifdev_remove,
224 #if defined(CONFIG_PM)
226 .pm = &ath6kl_hifdev_pmops,
231 /* make sure we only unregister when registered. */
232 static int registered = 0;
234 extern u32 onebitmode;
235 extern u32 busspeedlow;
238 static void ResetAllCards(void);
242 ATH_DEBUG_INSTANTIATE_MODULE_VAR(hif,
244 "(Linux MMC) Host Interconnect Framework",
245 ATH_DEBUG_MASK_DEFAULTS,
252 /* ------ Functions ------ */
253 int HIFInit(OSDRV_CALLBACKS *callbacks)
256 AR_DEBUG_ASSERT(callbacks != NULL);
258 A_REGISTER_MODULE_DEBUG_INFO(hif);
260 /* store the callback handlers */
261 osdrvCallbacks = *callbacks;
263 /* Register with bus driver core */
266 r = sdio_register_driver(&ath6kl_hifdev_driver);
274 __HIFReadWrite(struct hif_device *device,
285 bool bounced = false;
287 AR_DEBUG_ASSERT(device != NULL);
288 AR_DEBUG_ASSERT(device->func != NULL);
290 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: 0x%p, buffer:0x%p (addr:0x%X)\n",
291 device, buffer, address));
294 if (request & HIF_EXTENDED_IO) {
295 //AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Command type: CMD53\n"));
297 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
298 ("AR6000: Invalid command type: 0x%08x\n", request));
303 if (request & HIF_BLOCK_BASIS) {
304 /* round to whole block length size */
305 length = (length / HIF_MBOX_BLOCK_SIZE) * HIF_MBOX_BLOCK_SIZE;
306 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
307 ("AR6000: Block mode (BlockLen: %d)\n",
309 } else if (request & HIF_BYTE_BASIS) {
310 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
311 ("AR6000: Byte mode (BlockLen: %d)\n",
314 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
315 ("AR6000: Invalid data mode: 0x%08x\n", request));
321 /* useful for checking register accesses */
323 A_PRINTF(KERN_ALERT"AR6000: HIF (%s) is not a multiple of 4 bytes, addr:0x%X, len:%d\n",
324 request & HIF_WRITE ? "write":"read", address, length);
328 if (request & HIF_WRITE) {
329 if ((address >= HIF_MBOX_START_ADDR(0)) &&
330 (address <= HIF_MBOX_END_ADDR(3)))
333 AR_DEBUG_ASSERT(length <= HIF_MBOX_WIDTH);
336 * Mailbox write. Adjust the address so that the last byte
337 * falls on the EOM address.
339 address += (HIF_MBOX_WIDTH - length);
343 if (request & HIF_FIXED_ADDRESS) {
344 opcode = CMD53_FIXED_ADDRESS;
345 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Address mode: Fixed 0x%X\n", address));
346 } else if (request & HIF_INCREMENTAL_ADDRESS) {
347 opcode = CMD53_INCR_ADDRESS;
348 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Address mode: Incremental 0x%X\n", address));
350 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
351 ("AR6000: Invalid address mode: 0x%08x\n", request));
356 if (request & HIF_WRITE) {
357 #if HIF_USE_DMA_BOUNCE_BUFFER
358 if (BUFFER_NEEDS_BOUNCE(buffer)) {
359 AR_DEBUG_ASSERT(device->dma_buffer != NULL);
360 tbuffer = device->dma_buffer;
361 /* copy the write data to the dma buffer */
362 AR_DEBUG_ASSERT(length <= HIF_DMA_BUFFER_SIZE);
363 memcpy(tbuffer, buffer, length);
371 if (opcode == CMD53_FIXED_ADDRESS) {
372 ret = sdio_writesb(device->func, address, tbuffer, length);
373 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: writesb ret=%d address: 0x%X, len: %d, 0x%X\n",
374 ret, address, length, *(int *)tbuffer));
376 ret = sdio_memcpy_toio(device->func, address, tbuffer, length);
377 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: writeio ret=%d address: 0x%X, len: %d, 0x%X\n",
378 ret, address, length, *(int *)tbuffer));
380 } else if (request & HIF_READ) {
381 #if HIF_USE_DMA_BOUNCE_BUFFER
382 if (BUFFER_NEEDS_BOUNCE(buffer)) {
383 AR_DEBUG_ASSERT(device->dma_buffer != NULL);
384 AR_DEBUG_ASSERT(length <= HIF_DMA_BUFFER_SIZE);
385 tbuffer = device->dma_buffer;
393 if (opcode == CMD53_FIXED_ADDRESS) {
394 ret = sdio_readsb(device->func, tbuffer, address, length);
395 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: readsb ret=%d address: 0x%X, len: %d, 0x%X\n",
396 ret, address, length, *(int *)tbuffer));
398 ret = sdio_memcpy_fromio(device->func, tbuffer, address, length);
399 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: readio ret=%d address: 0x%X, len: %d, 0x%X\n",
400 ret, address, length, *(int *)tbuffer));
402 #if HIF_USE_DMA_BOUNCE_BUFFER
404 /* copy the read data from the dma buffer */
405 memcpy(buffer, tbuffer, length);
409 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
410 ("AR6000: Invalid direction: 0x%08x\n", request));
416 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
417 ("AR6000: SDIO bus operation failed! MMC stack returned : %d \n", ret));
425 void AddToAsyncList(struct hif_device *device, BUS_REQUEST *busrequest)
431 spin_lock_irqsave(&device->asynclock, flags);
432 active = device->asyncreq;
433 if (active == NULL) {
434 device->asyncreq = busrequest;
435 device->asyncreq->inusenext = NULL;
437 for (async = device->asyncreq;
439 async = async->inusenext) {
442 active->inusenext = busrequest;
443 busrequest->inusenext = NULL;
445 spin_unlock_irqrestore(&device->asynclock, flags);
449 /* queue a read/write request */
451 HIFReadWrite(struct hif_device *device,
459 BUS_REQUEST *busrequest;
462 AR_DEBUG_ASSERT(device != NULL);
463 AR_DEBUG_ASSERT(device->func != NULL);
465 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: %p addr:0x%X\n", device,address));
468 if ((request & HIF_ASYNCHRONOUS) || (request & HIF_SYNCHRONOUS)){
469 /* serialize all requests through the async thread */
470 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Execution mode: %s\n",
471 (request & HIF_ASYNCHRONOUS)?"Async":"Synch"));
472 busrequest = hifAllocateBusRequest(device);
473 if (busrequest == NULL) {
474 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
475 ("AR6000: no async bus requests available (%s, addr:0x%X, len:%d) \n",
476 request & HIF_READ ? "READ":"WRITE", address, length));
479 busrequest->address = address;
480 busrequest->buffer = buffer;
481 busrequest->length = length;
482 busrequest->request = request;
483 busrequest->context = context;
485 AddToAsyncList(device, busrequest);
487 if (request & HIF_SYNCHRONOUS) {
488 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued sync req: 0x%lX\n", (unsigned long)busrequest));
490 /* wait for completion */
491 up(&device->sem_async);
492 if (down_interruptible(&busrequest->sem_req) != 0) {
493 /* interrupted, exit */
496 int status = busrequest->status;
497 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: sync return freeing 0x%lX: 0x%X\n",
498 (unsigned long)busrequest, busrequest->status));
499 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: freeing req: 0x%X\n", (unsigned int)request));
500 hifFreeBusRequest(device, busrequest);
504 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued async req: 0x%lX\n", (unsigned long)busrequest));
505 up(&device->sem_async);
509 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
510 ("AR6000: Invalid execution mode: 0x%08x\n", (unsigned int)request));
518 /* thread to serialize all requests, both sync and async */
519 static int async_task(void *param)
521 struct hif_device *device;
522 BUS_REQUEST *request;
526 device = (struct hif_device *)param;
527 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task\n"));
528 set_current_state(TASK_INTERRUPTIBLE);
529 while(!device->async_shutdown) {
531 if (down_interruptible(&device->sem_async) != 0) {
532 /* interrupted, exit */
533 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task interrupted\n"));
536 if (device->async_shutdown) {
537 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task stopping\n"));
540 /* we want to hold the host over multiple cmds if possible, but holding the host blocks card interrupts */
541 sdio_claim_host(device->func);
542 spin_lock_irqsave(&device->asynclock, flags);
543 /* pull the request to work on */
544 while (device->asyncreq != NULL) {
545 request = device->asyncreq;
546 if (request->inusenext != NULL) {
547 device->asyncreq = request->inusenext;
549 device->asyncreq = NULL;
551 spin_unlock_irqrestore(&device->asynclock, flags);
552 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task processing req: 0x%lX\n", (unsigned long)request));
554 if (request->pScatterReq != NULL) {
555 A_ASSERT(device->scatter_enabled);
556 /* this is a queued scatter request, pass the request to scatter routine which
557 * executes it synchronously, note, no need to free the request since scatter requests
558 * are maintained on a separate list */
559 status = DoHifReadWriteScatter(device,request);
561 /* call HIFReadWrite in sync mode to do the work */
562 status = __HIFReadWrite(device, request->address, request->buffer,
563 request->length, request->request & ~HIF_SYNCHRONOUS, NULL);
564 if (request->request & HIF_ASYNCHRONOUS) {
565 void *context = request->context;
566 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task freeing req: 0x%lX\n", (unsigned long)request));
567 hifFreeBusRequest(device, request);
568 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task completion routine req: 0x%lX\n", (unsigned long)request));
569 device->htcCallbacks.rwCompletionHandler(context, status);
571 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task upping req: 0x%lX\n", (unsigned long)request));
572 request->status = status;
573 up(&request->sem_req);
576 spin_lock_irqsave(&device->asynclock, flags);
578 spin_unlock_irqrestore(&device->asynclock, flags);
579 sdio_release_host(device->func);
582 complete_and_exit(&device->async_completion, 0);
586 static s32 IssueSDCommand(struct hif_device *device, u32 opcode, u32 arg, u32 flags, u32 *resp)
588 struct mmc_command cmd;
590 struct mmc_host *host;
591 struct sdio_func *func;
594 host = func->card->host;
596 memset(&cmd, 0, sizeof(struct mmc_command));
600 err = mmc_wait_for_cmd(host, &cmd, 3);
602 if ((!err) && (resp)) {
609 int ReinitSDIO(struct hif_device *device)
612 struct mmc_host *host;
613 struct mmc_card *card;
614 struct sdio_func *func;
622 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +ReinitSDIO \n"));
623 sdio_claim_host(func);
626 if (!device->is_suspend) {
630 int bit = fls(host->ocr_avail) - 1;
631 /* emulate the mmc_power_up(...) */
633 host->ios.chip_select = MMC_CS_DONTCARE;
634 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
635 host->ios.power_mode = MMC_POWER_UP;
636 host->ios.bus_width = MMC_BUS_WIDTH_1;
637 host->ios.timing = MMC_TIMING_LEGACY;
638 host->ops->set_ios(host, &host->ios);
640 * This delay should be sufficient to allow the power supply
641 * to reach the minimum voltage.
645 host->ios.clock = host->f_min;
646 host->ios.power_mode = MMC_POWER_ON;
647 host->ops->set_ios(host, &host->ios);
650 * This delay must be at least 74 clock sizes, or 1 ms, or the
651 * time required to reach a stable voltage.
655 /* Issue CMD0. Goto idle state */
656 host->ios.chip_select = MMC_CS_HIGH;
657 host->ops->set_ios(host, &host->ios);
659 err = IssueSDCommand(device, MMC_GO_IDLE_STATE, 0, (MMC_RSP_NONE | MMC_CMD_BC), NULL);
660 host->ios.chip_select = MMC_CS_DONTCARE;
661 host->ops->set_ios(host, &host->ios);
663 host->use_spi_crc = 0;
666 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD0 failed : %d \n",err));
671 /* Issue CMD5, arg = 0 */
672 err = IssueSDCommand(device, SD_IO_SEND_OP_COND, 0, (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
674 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));
680 /* Issue CMD5, arg = ocr. Wait till card is ready */
681 for (i=0;i<100;i++) {
682 err = IssueSDCommand(device, SD_IO_SEND_OP_COND, host->ocr, (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
684 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));
687 if (resp & MMC_CARD_BUSY) {
693 if ((i == 100) || (err)) {
694 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: card in not ready : %d %d \n",i,err));
698 /* Issue CMD3, get RCA */
699 err = IssueSDCommand(device, SD_SEND_RELATIVE_ADDR, 0, MMC_RSP_R6 | MMC_CMD_BCR, &resp);
701 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD3 failed : %d \n",err));
705 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
706 host->ops->set_ios(host, &host->ios);
708 /* Issue CMD7, select card */
709 err = IssueSDCommand(device, MMC_SELECT_CARD, (rca << 16), MMC_RSP_R1 | MMC_CMD_AC, NULL);
711 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD7 failed : %d \n",err));
716 /* Enable high speed */
717 if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
718 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("ReinitSDIO: Set high speed mode\n"));
719 err = Func0_CMD52ReadByte(card, SDIO_CCCR_SPEED, &cmd52_resp);
721 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 read to CCCR speed register failed : %d \n",err));
722 card->state &= ~MMC_STATE_HIGHSPEED;
723 /* no need to break */
725 err = Func0_CMD52WriteByte(card, SDIO_CCCR_SPEED, (cmd52_resp | SDIO_SPEED_EHS));
727 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 write to CCCR speed register failed : %d \n",err));
730 mmc_card_set_highspeed(card);
731 host->ios.timing = MMC_TIMING_SD_HS;
732 host->ops->set_ios(host, &host->ios);
737 if (mmc_card_highspeed(card)) {
740 clock = card->cis.max_dtr;
743 if (clock > host->f_max) {
746 host->ios.clock = clock;
747 host->ops->set_ios(host, &host->ios);
750 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
751 /* CMD52: Set bus width & disable card detect resistor */
752 err = Func0_CMD52WriteByte(card, SDIO_CCCR_IF, SDIO_BUS_CD_DISABLE | SDIO_BUS_WIDTH_4BIT);
754 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 to set bus mode failed : %d \n",err));
757 host->ios.bus_width = MMC_BUS_WIDTH_4;
758 host->ops->set_ios(host, &host->ios);
762 sdio_release_host(func);
763 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -ReinitSDIO \n"));
765 return (err) ? A_ERROR : 0;
769 PowerStateChangeNotify(struct hif_device *device, HIF_DEVICE_POWER_CHANGE_TYPE config)
772 #if defined(CONFIG_PM)
773 struct sdio_func *func = device->func;
775 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +PowerStateChangeNotify %d\n", config));
777 case HIF_DEVICE_POWER_DOWN:
778 case HIF_DEVICE_POWER_CUT:
779 old_reset_val = reset_sdio_on_unload;
780 reset_sdio_on_unload = 1;
781 status = hifDisableFunc(device, func);
782 reset_sdio_on_unload = old_reset_val;
783 if (!device->is_suspend) {
784 struct mmc_host *host = func->card->host;
787 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
788 host->ios.chip_select = MMC_CS_DONTCARE;
789 host->ios.power_mode = MMC_POWER_OFF;
790 host->ios.bus_width = MMC_BUS_WIDTH_1;
791 host->ios.timing = MMC_TIMING_LEGACY;
792 host->ops->set_ios(host, &host->ios);
795 case HIF_DEVICE_POWER_UP:
796 if (device->powerConfig == HIF_DEVICE_POWER_CUT) {
797 status = ReinitSDIO(device);
800 status = hifEnableFunc(device, func);
804 device->powerConfig = config;
806 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -PowerStateChangeNotify\n"));
812 HIFConfigureDevice(struct hif_device *device, HIF_DEVICE_CONFIG_OPCODE opcode,
813 void *config, u32 configLen)
819 case HIF_DEVICE_GET_MBOX_BLOCK_SIZE:
820 ((u32 *)config)[0] = HIF_MBOX0_BLOCK_SIZE;
821 ((u32 *)config)[1] = HIF_MBOX1_BLOCK_SIZE;
822 ((u32 *)config)[2] = HIF_MBOX2_BLOCK_SIZE;
823 ((u32 *)config)[3] = HIF_MBOX3_BLOCK_SIZE;
826 case HIF_DEVICE_GET_MBOX_ADDR:
827 for (count = 0; count < 4; count ++) {
828 ((u32 *)config)[count] = HIF_MBOX_START_ADDR(count);
831 if (configLen >= sizeof(struct hif_device_mbox_info)) {
832 SetExtendedMboxWindowInfo((u16)device->func->device,
833 (struct hif_device_mbox_info *)config);
837 case HIF_DEVICE_GET_IRQ_PROC_MODE:
838 *((HIF_DEVICE_IRQ_PROCESSING_MODE *)config) = HIF_DEVICE_IRQ_SYNC_ONLY;
840 case HIF_CONFIGURE_QUERY_SCATTER_REQUEST_SUPPORT:
841 if (!device->scatter_enabled) {
844 status = SetupHIFScatterSupport(device, (struct hif_device_scatter_support_info *)config);
846 device->scatter_enabled = false;
849 case HIF_DEVICE_GET_OS_DEVICE:
850 /* pass back a pointer to the SDIO function's "dev" struct */
851 ((struct hif_device_os_device_info *)config)->pOSDevice = &device->func->dev;
853 case HIF_DEVICE_POWER_STATE_CHANGE:
854 status = PowerStateChangeNotify(device, *(HIF_DEVICE_POWER_CHANGE_TYPE *)config);
857 AR_DEBUG_PRINTF(ATH_DEBUG_WARN,
858 ("AR6000: Unsupported configuration opcode: %d\n", opcode));
866 HIFShutDownDevice(struct hif_device *device)
868 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +HIFShutDownDevice\n"));
869 if (device != NULL) {
870 AR_DEBUG_ASSERT(device->func != NULL);
872 /* since we are unloading the driver anyways, reset all cards in case the SDIO card
873 * is externally powered and we are unloading the SDIO stack. This avoids the problem when
874 * the SDIO stack is reloaded and attempts are made to re-enumerate a card that is already
876 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFShutDownDevice, resetting\n"));
879 /* Unregister with bus driver core */
882 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
883 ("AR6000: Unregistering with the bus driver\n"));
884 sdio_unregister_driver(&ath6kl_hifdev_driver);
885 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
886 ("AR6000: Unregistered\n"));
889 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -HIFShutDownDevice\n"));
893 hifIRQHandler(struct sdio_func *func)
896 struct hif_device *device;
897 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifIRQHandler\n"));
899 device = ath6kl_get_hifdev(func);
900 atomic_set(&device->irqHandling, 1);
901 /* release the host during ints so we can pick it back up when we process cmds */
902 sdio_release_host(device->func);
903 status = device->htcCallbacks.dsrHandler(device->htcCallbacks.context);
904 sdio_claim_host(device->func);
905 atomic_set(&device->irqHandling, 0);
906 AR_DEBUG_ASSERT(status == 0 || status == A_ECANCELED);
907 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifIRQHandler\n"));
910 /* handle HTC startup via thread*/
911 static int startup_task(void *param)
913 struct hif_device *device;
915 device = (struct hif_device *)param;
916 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: call HTC from startup_task\n"));
917 /* start up inform DRV layer */
918 if ((osdrvCallbacks.deviceInsertedHandler(osdrvCallbacks.context,device)) != 0) {
919 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device rejected\n"));
924 #if defined(CONFIG_PM)
925 static int enable_task(void *param)
927 struct hif_device *device;
928 device = (struct hif_device *)param;
929 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: call from resume_task\n"));
931 /* start up inform DRV layer */
933 device->claimedContext &&
934 osdrvCallbacks.devicePowerChangeHandler &&
935 osdrvCallbacks.devicePowerChangeHandler(device->claimedContext, HIF_DEVICE_POWER_UP) != 0)
937 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device rejected\n"));
945 HIFAckInterrupt(struct hif_device *device)
947 AR_DEBUG_ASSERT(device != NULL);
949 /* Acknowledge our function IRQ */
953 HIFUnMaskInterrupt(struct hif_device *device)
957 AR_DEBUG_ASSERT(device != NULL);
958 AR_DEBUG_ASSERT(device->func != NULL);
960 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFUnMaskInterrupt\n"));
962 /* Register the IRQ Handler */
963 sdio_claim_host(device->func);
964 ret = sdio_claim_irq(device->func, hifIRQHandler);
965 sdio_release_host(device->func);
966 AR_DEBUG_ASSERT(ret == 0);
969 void HIFMaskInterrupt(struct hif_device *device)
972 AR_DEBUG_ASSERT(device != NULL);
973 AR_DEBUG_ASSERT(device->func != NULL);
975 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFMaskInterrupt\n"));
977 /* Mask our function IRQ */
978 sdio_claim_host(device->func);
979 while (atomic_read(&device->irqHandling)) {
980 sdio_release_host(device->func);
981 schedule_timeout(HZ/10);
982 sdio_claim_host(device->func);
984 ret = sdio_release_irq(device->func);
985 sdio_release_host(device->func);
986 AR_DEBUG_ASSERT(ret == 0);
989 BUS_REQUEST *hifAllocateBusRequest(struct hif_device *device)
991 BUS_REQUEST *busrequest;
995 spin_lock_irqsave(&device->lock, flag);
997 /* Remove first in list */
998 if((busrequest = device->s_busRequestFreeQueue) != NULL)
1000 device->s_busRequestFreeQueue = busrequest->next;
1003 spin_unlock_irqrestore(&device->lock, flag);
1004 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifAllocateBusRequest: 0x%p\n", busrequest));
1009 hifFreeBusRequest(struct hif_device *device, BUS_REQUEST *busrequest)
1013 AR_DEBUG_ASSERT(busrequest != NULL);
1014 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifFreeBusRequest: 0x%p\n", busrequest));
1016 spin_lock_irqsave(&device->lock, flag);
1019 /* Insert first in list */
1020 busrequest->next = device->s_busRequestFreeQueue;
1021 busrequest->inusenext = NULL;
1022 device->s_busRequestFreeQueue = busrequest;
1025 spin_unlock_irqrestore(&device->lock, flag);
1028 static int hifDisableFunc(struct hif_device *device, struct sdio_func *func)
1033 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifDisableFunc\n"));
1034 device = ath6kl_get_hifdev(func);
1035 if (!IS_ERR(device->async_task)) {
1036 init_completion(&device->async_completion);
1037 device->async_shutdown = 1;
1038 up(&device->sem_async);
1039 wait_for_completion(&device->async_completion);
1040 device->async_task = NULL;
1042 /* Disable the card */
1043 sdio_claim_host(device->func);
1044 ret = sdio_disable_func(device->func);
1049 if (reset_sdio_on_unload) {
1050 /* reset the SDIO interface. This is useful in automated testing where the card
1051 * does not need to be removed at the end of the test. It is expected that the user will
1052 * also unload/reload the host controller driver to force the bus driver to re-enumerate the slot */
1053 AR_DEBUG_PRINTF(ATH_DEBUG_WARN, ("AR6000: reseting SDIO card back to uninitialized state \n"));
1055 /* NOTE : sdio_f0_writeb() cannot be used here, that API only allows access
1056 * to undefined registers in the range of: 0xF0-0xFF */
1058 ret = Func0_CMD52WriteByte(device->func->card, SDIO_CCCR_ABORT, (1 << 3));
1061 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: reset failed : %d \n",ret));
1065 sdio_release_host(device->func);
1068 device->is_disabled = true;
1070 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDisableFunc\n"));
1075 static int hifEnableFunc(struct hif_device *device, struct sdio_func *func)
1077 struct task_struct* pTask;
1078 const char *taskName = NULL;
1079 int (*taskFunc)(void *) = NULL;
1082 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifEnableFunc\n"));
1083 device = ath6kl_get_hifdev(func);
1085 if (device->is_disabled) {
1086 /* enable the SDIO function */
1087 sdio_claim_host(func);
1089 if ((device->id->device & MANUFACTURER_ID_AR6K_BASE_MASK) >= MANUFACTURER_ID_AR6003_BASE) {
1090 /* enable 4-bit ASYNC interrupt on AR6003 or later devices */
1091 ret = Func0_CMD52WriteByte(func->card, CCCR_SDIO_IRQ_MODE_REG, SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
1093 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: failed to enable 4-bit ASYNC IRQ mode %d \n",ret));
1094 sdio_release_host(func);
1097 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: 4-bit ASYNC IRQ mode enabled\n"));
1099 /* give us some time to enable, in ms */
1100 func->enable_timeout = 100;
1101 ret = sdio_enable_func(func);
1103 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), Unable to enable AR6K: 0x%X\n",
1104 __FUNCTION__, ret));
1105 sdio_release_host(func);
1108 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
1109 sdio_release_host(func);
1111 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), Unable to set block size 0x%x AR6K: 0x%X\n",
1112 __FUNCTION__, HIF_MBOX_BLOCK_SIZE, ret));
1115 device->is_disabled = false;
1116 /* create async I/O thread */
1117 if (!device->async_task) {
1118 device->async_shutdown = 0;
1119 device->async_task = kthread_create(async_task,
1122 if (IS_ERR(device->async_task)) {
1123 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), to create async task\n", __FUNCTION__));
1126 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: start async task\n"));
1127 wake_up_process(device->async_task );
1131 if (!device->claimedContext) {
1132 taskFunc = startup_task;
1133 taskName = "AR6K startup";
1135 #if defined(CONFIG_PM)
1137 taskFunc = enable_task;
1138 taskName = "AR6K enable";
1140 #endif /* CONFIG_PM */
1142 /* create resume thread */
1143 pTask = kthread_create(taskFunc, (void *)device, taskName);
1144 if (IS_ERR(pTask)) {
1145 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), to create enabel task\n", __FUNCTION__));
1148 wake_up_process(pTask);
1149 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifEnableFunc\n"));
1151 /* task will call the enable func, indicate pending */
1156 * This should be moved to AR6K HTC layer.
1158 int hifWaitForPendingRecv(struct hif_device *device)
1165 while (atomic_read(&device->irqHandling)) {
1166 /* wait until irq handler finished all the jobs */
1167 schedule_timeout(HZ/10);
1169 /* check if there is any pending irq due to force done */
1170 host_int_status = 0;
1171 status = HIFReadWrite(device, HOST_INT_STATUS_ADDRESS,
1172 (u8 *)&host_int_status, sizeof(host_int_status),
1173 HIF_RD_SYNC_BYTE_INC, NULL);
1174 host_int_status = !status ? (host_int_status & (1 << 0)) : 0;
1175 if (host_int_status) {
1176 schedule(); /* schedule for next dsrHandler */
1178 } while (host_int_status && --cnt > 0);
1180 if (host_int_status && cnt == 0) {
1181 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
1182 ("AR6000: %s(), Unable clear up pending IRQ before the system suspended\n", __FUNCTION__));
1189 delHifDevice(struct hif_device * device)
1191 AR_DEBUG_ASSERT(device!= NULL);
1192 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: delHifDevice; 0x%p\n", device));
1193 kfree(device->dma_buffer);
1197 static void ResetAllCards(void)
1201 void HIFClaimDevice(struct hif_device *device, void *context)
1203 device->claimedContext = context;
1206 void HIFReleaseDevice(struct hif_device *device)
1208 device->claimedContext = NULL;
1211 int HIFAttachHTC(struct hif_device *device, HTC_CALLBACKS *callbacks)
1213 if (device->htcCallbacks.context != NULL) {
1214 /* already in use! */
1217 device->htcCallbacks = *callbacks;
1221 void HIFDetachHTC(struct hif_device *device)
1223 A_MEMZERO(&device->htcCallbacks,sizeof(device->htcCallbacks));
1226 #define SDIO_SET_CMD52_ARG(arg,rw,func,raw,address,writedata) \
1227 (arg) = (((rw) & 1) << 31) | \
1228 (((func) & 0x7) << 28) | \
1229 (((raw) & 1) << 27) | \
1231 (((address) & 0x1FFFF) << 9) | \
1233 ((writedata) & 0xFF)
1235 #define SDIO_SET_CMD52_READ_ARG(arg,func,address) \
1236 SDIO_SET_CMD52_ARG(arg,0,(func),0,address,0x00)
1237 #define SDIO_SET_CMD52_WRITE_ARG(arg,func,address,value) \
1238 SDIO_SET_CMD52_ARG(arg,1,(func),0,address,value)
1240 static int Func0_CMD52WriteByte(struct mmc_card *card, unsigned int address, unsigned char byte)
1242 struct mmc_command ioCmd;
1245 memset(&ioCmd,0,sizeof(ioCmd));
1246 SDIO_SET_CMD52_WRITE_ARG(arg,0,address,byte);
1247 ioCmd.opcode = SD_IO_RW_DIRECT;
1249 ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1251 return mmc_wait_for_cmd(card->host, &ioCmd, 0);
1254 static int Func0_CMD52ReadByte(struct mmc_card *card, unsigned int address, unsigned char *byte)
1256 struct mmc_command ioCmd;
1260 memset(&ioCmd,0,sizeof(ioCmd));
1261 SDIO_SET_CMD52_READ_ARG(arg,0,address);
1262 ioCmd.opcode = SD_IO_RW_DIRECT;
1264 ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1266 err = mmc_wait_for_cmd(card->host, &ioCmd, 0);
1268 if ((!err) && (byte)) {
1269 *byte = ioCmd.resp[0] & 0xFF;