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 static int hifDeviceSuspend(struct device *dev);
57 static int hifDeviceResume(struct device *dev);
58 #endif /* CONFIG_PM */
59 static int hifDeviceInserted(struct sdio_func *func, const struct sdio_device_id *id);
60 static void hifDeviceRemoved(struct sdio_func *func);
61 static struct hif_device *addHifDevice(struct sdio_func *func);
62 static struct hif_device *getHifDevice(struct sdio_func *func);
63 static void delHifDevice(struct hif_device * device);
64 static int Func0_CMD52WriteByte(struct mmc_card *card, unsigned int address, unsigned char byte);
65 static int Func0_CMD52ReadByte(struct mmc_card *card, unsigned int address, unsigned char *byte);
67 int reset_sdio_on_unload = 0;
68 module_param(reset_sdio_on_unload, int, 0644);
70 extern u32 nohifscattersupport;
73 /* ------ Static Variables ------ */
74 static const struct sdio_device_id ar6k_id_table[] = {
75 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x0)) },
76 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x1)) },
77 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0)) },
78 { SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1)) },
81 MODULE_DEVICE_TABLE(sdio, ar6k_id_table);
83 static struct sdio_driver ar6k_driver = {
85 .id_table = ar6k_id_table,
86 .probe = hifDeviceInserted,
87 .remove = hifDeviceRemoved,
90 #if defined(CONFIG_PM)
91 /* New suspend/resume based on linux-2.6.32
92 * Need to patch linux-2.6.32 with mmc2.6.32_suspend.patch
93 * Need to patch with msmsdcc2.6.29_suspend.patch for msm_sdcc host
95 static struct dev_pm_ops ar6k_device_pm_ops = {
96 .suspend = hifDeviceSuspend,
97 .resume = hifDeviceResume,
99 #endif /* CONFIG_PM */
101 /* make sure we only unregister when registered. */
102 static int registered = 0;
104 OSDRV_CALLBACKS osdrvCallbacks;
105 extern u32 onebitmode;
106 extern u32 busspeedlow;
109 static void ResetAllCards(void);
110 static int hifDisableFunc(struct hif_device *device, struct sdio_func *func);
111 static int hifEnableFunc(struct hif_device *device, struct sdio_func *func);
115 ATH_DEBUG_INSTANTIATE_MODULE_VAR(hif,
117 "(Linux MMC) Host Interconnect Framework",
118 ATH_DEBUG_MASK_DEFAULTS,
125 /* ------ Functions ------ */
126 int HIFInit(OSDRV_CALLBACKS *callbacks)
129 AR_DEBUG_ASSERT(callbacks != NULL);
131 A_REGISTER_MODULE_DEBUG_INFO(hif);
133 /* store the callback handlers */
134 osdrvCallbacks = *callbacks;
136 /* Register with bus driver core */
139 #if defined(CONFIG_PM)
140 if (callbacks->deviceSuspendHandler && callbacks->deviceResumeHandler)
141 ar6k_driver.drv.pm = &ar6k_device_pm_ops;
142 #endif /* CONFIG_PM */
144 r = sdio_register_driver(&ar6k_driver);
152 __HIFReadWrite(struct hif_device *device,
163 bool bounced = false;
165 AR_DEBUG_ASSERT(device != NULL);
166 AR_DEBUG_ASSERT(device->func != NULL);
168 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: 0x%p, buffer:0x%p (addr:0x%X)\n",
169 device, buffer, address));
172 if (request & HIF_EXTENDED_IO) {
173 //AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Command type: CMD53\n"));
175 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
176 ("AR6000: Invalid command type: 0x%08x\n", request));
181 if (request & HIF_BLOCK_BASIS) {
182 /* round to whole block length size */
183 length = (length / HIF_MBOX_BLOCK_SIZE) * HIF_MBOX_BLOCK_SIZE;
184 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
185 ("AR6000: Block mode (BlockLen: %d)\n",
187 } else if (request & HIF_BYTE_BASIS) {
188 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
189 ("AR6000: Byte mode (BlockLen: %d)\n",
192 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
193 ("AR6000: Invalid data mode: 0x%08x\n", request));
199 /* useful for checking register accesses */
201 A_PRINTF(KERN_ALERT"AR6000: HIF (%s) is not a multiple of 4 bytes, addr:0x%X, len:%d\n",
202 request & HIF_WRITE ? "write":"read", address, length);
206 if (request & HIF_WRITE) {
207 if ((address >= HIF_MBOX_START_ADDR(0)) &&
208 (address <= HIF_MBOX_END_ADDR(3)))
211 AR_DEBUG_ASSERT(length <= HIF_MBOX_WIDTH);
214 * Mailbox write. Adjust the address so that the last byte
215 * falls on the EOM address.
217 address += (HIF_MBOX_WIDTH - length);
221 if (request & HIF_FIXED_ADDRESS) {
222 opcode = CMD53_FIXED_ADDRESS;
223 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Address mode: Fixed 0x%X\n", address));
224 } else if (request & HIF_INCREMENTAL_ADDRESS) {
225 opcode = CMD53_INCR_ADDRESS;
226 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Address mode: Incremental 0x%X\n", address));
228 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
229 ("AR6000: Invalid address mode: 0x%08x\n", request));
234 if (request & HIF_WRITE) {
235 #if HIF_USE_DMA_BOUNCE_BUFFER
236 if (BUFFER_NEEDS_BOUNCE(buffer)) {
237 AR_DEBUG_ASSERT(device->dma_buffer != NULL);
238 tbuffer = device->dma_buffer;
239 /* copy the write data to the dma buffer */
240 AR_DEBUG_ASSERT(length <= HIF_DMA_BUFFER_SIZE);
241 memcpy(tbuffer, buffer, length);
249 if (opcode == CMD53_FIXED_ADDRESS) {
250 ret = sdio_writesb(device->func, address, tbuffer, length);
251 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: writesb ret=%d address: 0x%X, len: %d, 0x%X\n",
252 ret, address, length, *(int *)tbuffer));
254 ret = sdio_memcpy_toio(device->func, address, tbuffer, length);
255 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: writeio ret=%d address: 0x%X, len: %d, 0x%X\n",
256 ret, address, length, *(int *)tbuffer));
258 } else if (request & HIF_READ) {
259 #if HIF_USE_DMA_BOUNCE_BUFFER
260 if (BUFFER_NEEDS_BOUNCE(buffer)) {
261 AR_DEBUG_ASSERT(device->dma_buffer != NULL);
262 AR_DEBUG_ASSERT(length <= HIF_DMA_BUFFER_SIZE);
263 tbuffer = device->dma_buffer;
271 if (opcode == CMD53_FIXED_ADDRESS) {
272 ret = sdio_readsb(device->func, tbuffer, address, length);
273 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: readsb ret=%d address: 0x%X, len: %d, 0x%X\n",
274 ret, address, length, *(int *)tbuffer));
276 ret = sdio_memcpy_fromio(device->func, tbuffer, address, length);
277 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: readio ret=%d address: 0x%X, len: %d, 0x%X\n",
278 ret, address, length, *(int *)tbuffer));
280 #if HIF_USE_DMA_BOUNCE_BUFFER
282 /* copy the read data from the dma buffer */
283 memcpy(buffer, tbuffer, length);
287 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
288 ("AR6000: Invalid direction: 0x%08x\n", request));
294 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
295 ("AR6000: SDIO bus operation failed! MMC stack returned : %d \n", ret));
303 void AddToAsyncList(struct hif_device *device, BUS_REQUEST *busrequest)
309 spin_lock_irqsave(&device->asynclock, flags);
310 active = device->asyncreq;
311 if (active == NULL) {
312 device->asyncreq = busrequest;
313 device->asyncreq->inusenext = NULL;
315 for (async = device->asyncreq;
317 async = async->inusenext) {
320 active->inusenext = busrequest;
321 busrequest->inusenext = NULL;
323 spin_unlock_irqrestore(&device->asynclock, flags);
327 /* queue a read/write request */
329 HIFReadWrite(struct hif_device *device,
337 BUS_REQUEST *busrequest;
340 AR_DEBUG_ASSERT(device != NULL);
341 AR_DEBUG_ASSERT(device->func != NULL);
343 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: %p addr:0x%X\n", device,address));
346 if ((request & HIF_ASYNCHRONOUS) || (request & HIF_SYNCHRONOUS)){
347 /* serialize all requests through the async thread */
348 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Execution mode: %s\n",
349 (request & HIF_ASYNCHRONOUS)?"Async":"Synch"));
350 busrequest = hifAllocateBusRequest(device);
351 if (busrequest == NULL) {
352 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
353 ("AR6000: no async bus requests available (%s, addr:0x%X, len:%d) \n",
354 request & HIF_READ ? "READ":"WRITE", address, length));
357 busrequest->address = address;
358 busrequest->buffer = buffer;
359 busrequest->length = length;
360 busrequest->request = request;
361 busrequest->context = context;
363 AddToAsyncList(device, busrequest);
365 if (request & HIF_SYNCHRONOUS) {
366 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued sync req: 0x%lX\n", (unsigned long)busrequest));
368 /* wait for completion */
369 up(&device->sem_async);
370 if (down_interruptible(&busrequest->sem_req) != 0) {
371 /* interrupted, exit */
374 int status = busrequest->status;
375 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: sync return freeing 0x%lX: 0x%X\n",
376 (unsigned long)busrequest, busrequest->status));
377 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: freeing req: 0x%X\n", (unsigned int)request));
378 hifFreeBusRequest(device, busrequest);
382 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued async req: 0x%lX\n", (unsigned long)busrequest));
383 up(&device->sem_async);
387 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
388 ("AR6000: Invalid execution mode: 0x%08x\n", (unsigned int)request));
396 /* thread to serialize all requests, both sync and async */
397 static int async_task(void *param)
399 struct hif_device *device;
400 BUS_REQUEST *request;
404 device = (struct hif_device *)param;
405 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task\n"));
406 set_current_state(TASK_INTERRUPTIBLE);
407 while(!device->async_shutdown) {
409 if (down_interruptible(&device->sem_async) != 0) {
410 /* interrupted, exit */
411 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task interrupted\n"));
414 if (device->async_shutdown) {
415 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task stopping\n"));
418 /* we want to hold the host over multiple cmds if possible, but holding the host blocks card interrupts */
419 sdio_claim_host(device->func);
420 spin_lock_irqsave(&device->asynclock, flags);
421 /* pull the request to work on */
422 while (device->asyncreq != NULL) {
423 request = device->asyncreq;
424 if (request->inusenext != NULL) {
425 device->asyncreq = request->inusenext;
427 device->asyncreq = NULL;
429 spin_unlock_irqrestore(&device->asynclock, flags);
430 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task processing req: 0x%lX\n", (unsigned long)request));
432 if (request->pScatterReq != NULL) {
433 A_ASSERT(device->scatter_enabled);
434 /* this is a queued scatter request, pass the request to scatter routine which
435 * executes it synchronously, note, no need to free the request since scatter requests
436 * are maintained on a separate list */
437 status = DoHifReadWriteScatter(device,request);
439 /* call HIFReadWrite in sync mode to do the work */
440 status = __HIFReadWrite(device, request->address, request->buffer,
441 request->length, request->request & ~HIF_SYNCHRONOUS, NULL);
442 if (request->request & HIF_ASYNCHRONOUS) {
443 void *context = request->context;
444 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task freeing req: 0x%lX\n", (unsigned long)request));
445 hifFreeBusRequest(device, request);
446 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task completion routine req: 0x%lX\n", (unsigned long)request));
447 device->htcCallbacks.rwCompletionHandler(context, status);
449 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async_task upping req: 0x%lX\n", (unsigned long)request));
450 request->status = status;
451 up(&request->sem_req);
454 spin_lock_irqsave(&device->asynclock, flags);
456 spin_unlock_irqrestore(&device->asynclock, flags);
457 sdio_release_host(device->func);
460 complete_and_exit(&device->async_completion, 0);
464 static s32 IssueSDCommand(struct hif_device *device, u32 opcode, u32 arg, u32 flags, u32 *resp)
466 struct mmc_command cmd;
468 struct mmc_host *host;
469 struct sdio_func *func;
472 host = func->card->host;
474 memset(&cmd, 0, sizeof(struct mmc_command));
478 err = mmc_wait_for_cmd(host, &cmd, 3);
480 if ((!err) && (resp)) {
487 int ReinitSDIO(struct hif_device *device)
490 struct mmc_host *host;
491 struct mmc_card *card;
492 struct sdio_func *func;
500 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +ReinitSDIO \n"));
501 sdio_claim_host(func);
504 if (!device->is_suspend) {
508 int bit = fls(host->ocr_avail) - 1;
509 /* emulate the mmc_power_up(...) */
511 host->ios.chip_select = MMC_CS_DONTCARE;
512 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
513 host->ios.power_mode = MMC_POWER_UP;
514 host->ios.bus_width = MMC_BUS_WIDTH_1;
515 host->ios.timing = MMC_TIMING_LEGACY;
516 host->ops->set_ios(host, &host->ios);
518 * This delay should be sufficient to allow the power supply
519 * to reach the minimum voltage.
523 host->ios.clock = host->f_min;
524 host->ios.power_mode = MMC_POWER_ON;
525 host->ops->set_ios(host, &host->ios);
528 * This delay must be at least 74 clock sizes, or 1 ms, or the
529 * time required to reach a stable voltage.
533 /* Issue CMD0. Goto idle state */
534 host->ios.chip_select = MMC_CS_HIGH;
535 host->ops->set_ios(host, &host->ios);
537 err = IssueSDCommand(device, MMC_GO_IDLE_STATE, 0, (MMC_RSP_NONE | MMC_CMD_BC), NULL);
538 host->ios.chip_select = MMC_CS_DONTCARE;
539 host->ops->set_ios(host, &host->ios);
541 host->use_spi_crc = 0;
544 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD0 failed : %d \n",err));
549 /* Issue CMD5, arg = 0 */
550 err = IssueSDCommand(device, SD_IO_SEND_OP_COND, 0, (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
552 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));
558 /* Issue CMD5, arg = ocr. Wait till card is ready */
559 for (i=0;i<100;i++) {
560 err = IssueSDCommand(device, SD_IO_SEND_OP_COND, host->ocr, (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
562 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));
565 if (resp & MMC_CARD_BUSY) {
571 if ((i == 100) || (err)) {
572 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: card in not ready : %d %d \n",i,err));
576 /* Issue CMD3, get RCA */
577 err = IssueSDCommand(device, SD_SEND_RELATIVE_ADDR, 0, MMC_RSP_R6 | MMC_CMD_BCR, &resp);
579 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD3 failed : %d \n",err));
583 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
584 host->ops->set_ios(host, &host->ios);
586 /* Issue CMD7, select card */
587 err = IssueSDCommand(device, MMC_SELECT_CARD, (rca << 16), MMC_RSP_R1 | MMC_CMD_AC, NULL);
589 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD7 failed : %d \n",err));
594 /* Enable high speed */
595 if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
596 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("ReinitSDIO: Set high speed mode\n"));
597 err = Func0_CMD52ReadByte(card, SDIO_CCCR_SPEED, &cmd52_resp);
599 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 read to CCCR speed register failed : %d \n",err));
600 card->state &= ~MMC_STATE_HIGHSPEED;
601 /* no need to break */
603 err = Func0_CMD52WriteByte(card, SDIO_CCCR_SPEED, (cmd52_resp | SDIO_SPEED_EHS));
605 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 write to CCCR speed register failed : %d \n",err));
608 mmc_card_set_highspeed(card);
609 host->ios.timing = MMC_TIMING_SD_HS;
610 host->ops->set_ios(host, &host->ios);
615 if (mmc_card_highspeed(card)) {
618 clock = card->cis.max_dtr;
621 if (clock > host->f_max) {
624 host->ios.clock = clock;
625 host->ops->set_ios(host, &host->ios);
628 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
629 /* CMD52: Set bus width & disable card detect resistor */
630 err = Func0_CMD52WriteByte(card, SDIO_CCCR_IF, SDIO_BUS_CD_DISABLE | SDIO_BUS_WIDTH_4BIT);
632 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 to set bus mode failed : %d \n",err));
635 host->ios.bus_width = MMC_BUS_WIDTH_4;
636 host->ops->set_ios(host, &host->ios);
640 sdio_release_host(func);
641 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -ReinitSDIO \n"));
643 return (err) ? A_ERROR : 0;
647 PowerStateChangeNotify(struct hif_device *device, HIF_DEVICE_POWER_CHANGE_TYPE config)
650 #if defined(CONFIG_PM)
651 struct sdio_func *func = device->func;
653 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +PowerStateChangeNotify %d\n", config));
655 case HIF_DEVICE_POWER_DOWN:
656 case HIF_DEVICE_POWER_CUT:
657 old_reset_val = reset_sdio_on_unload;
658 reset_sdio_on_unload = 1;
659 status = hifDisableFunc(device, func);
660 reset_sdio_on_unload = old_reset_val;
661 if (!device->is_suspend) {
662 struct mmc_host *host = func->card->host;
665 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
666 host->ios.chip_select = MMC_CS_DONTCARE;
667 host->ios.power_mode = MMC_POWER_OFF;
668 host->ios.bus_width = MMC_BUS_WIDTH_1;
669 host->ios.timing = MMC_TIMING_LEGACY;
670 host->ops->set_ios(host, &host->ios);
673 case HIF_DEVICE_POWER_UP:
674 if (device->powerConfig == HIF_DEVICE_POWER_CUT) {
675 status = ReinitSDIO(device);
678 status = hifEnableFunc(device, func);
682 device->powerConfig = config;
684 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -PowerStateChangeNotify\n"));
690 HIFConfigureDevice(struct hif_device *device, HIF_DEVICE_CONFIG_OPCODE opcode,
691 void *config, u32 configLen)
697 case HIF_DEVICE_GET_MBOX_BLOCK_SIZE:
698 ((u32 *)config)[0] = HIF_MBOX0_BLOCK_SIZE;
699 ((u32 *)config)[1] = HIF_MBOX1_BLOCK_SIZE;
700 ((u32 *)config)[2] = HIF_MBOX2_BLOCK_SIZE;
701 ((u32 *)config)[3] = HIF_MBOX3_BLOCK_SIZE;
704 case HIF_DEVICE_GET_MBOX_ADDR:
705 for (count = 0; count < 4; count ++) {
706 ((u32 *)config)[count] = HIF_MBOX_START_ADDR(count);
709 if (configLen >= sizeof(struct hif_device_mbox_info)) {
710 SetExtendedMboxWindowInfo((u16)device->func->device,
711 (struct hif_device_mbox_info *)config);
715 case HIF_DEVICE_GET_IRQ_PROC_MODE:
716 *((HIF_DEVICE_IRQ_PROCESSING_MODE *)config) = HIF_DEVICE_IRQ_SYNC_ONLY;
718 case HIF_CONFIGURE_QUERY_SCATTER_REQUEST_SUPPORT:
719 if (!device->scatter_enabled) {
722 status = SetupHIFScatterSupport(device, (struct hif_device_scatter_support_info *)config);
724 device->scatter_enabled = false;
727 case HIF_DEVICE_GET_OS_DEVICE:
728 /* pass back a pointer to the SDIO function's "dev" struct */
729 ((struct hif_device_os_device_info *)config)->pOSDevice = &device->func->dev;
731 case HIF_DEVICE_POWER_STATE_CHANGE:
732 status = PowerStateChangeNotify(device, *(HIF_DEVICE_POWER_CHANGE_TYPE *)config);
735 AR_DEBUG_PRINTF(ATH_DEBUG_WARN,
736 ("AR6000: Unsupported configuration opcode: %d\n", opcode));
744 HIFShutDownDevice(struct hif_device *device)
746 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +HIFShutDownDevice\n"));
747 if (device != NULL) {
748 AR_DEBUG_ASSERT(device->func != NULL);
750 /* since we are unloading the driver anyways, reset all cards in case the SDIO card
751 * is externally powered and we are unloading the SDIO stack. This avoids the problem when
752 * the SDIO stack is reloaded and attempts are made to re-enumerate a card that is already
754 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFShutDownDevice, resetting\n"));
757 /* Unregister with bus driver core */
760 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
761 ("AR6000: Unregistering with the bus driver\n"));
762 sdio_unregister_driver(&ar6k_driver);
763 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
764 ("AR6000: Unregistered\n"));
767 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -HIFShutDownDevice\n"));
771 hifIRQHandler(struct sdio_func *func)
774 struct hif_device *device;
775 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifIRQHandler\n"));
777 device = getHifDevice(func);
778 atomic_set(&device->irqHandling, 1);
779 /* release the host during ints so we can pick it back up when we process cmds */
780 sdio_release_host(device->func);
781 status = device->htcCallbacks.dsrHandler(device->htcCallbacks.context);
782 sdio_claim_host(device->func);
783 atomic_set(&device->irqHandling, 0);
784 AR_DEBUG_ASSERT(status == 0 || status == A_ECANCELED);
785 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifIRQHandler\n"));
788 /* handle HTC startup via thread*/
789 static int startup_task(void *param)
791 struct hif_device *device;
793 device = (struct hif_device *)param;
794 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: call HTC from startup_task\n"));
795 /* start up inform DRV layer */
796 if ((osdrvCallbacks.deviceInsertedHandler(osdrvCallbacks.context,device)) != 0) {
797 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device rejected\n"));
802 #if defined(CONFIG_PM)
803 static int enable_task(void *param)
805 struct hif_device *device;
806 device = (struct hif_device *)param;
807 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: call from resume_task\n"));
809 /* start up inform DRV layer */
811 device->claimedContext &&
812 osdrvCallbacks.devicePowerChangeHandler &&
813 osdrvCallbacks.devicePowerChangeHandler(device->claimedContext, HIF_DEVICE_POWER_UP) != 0)
815 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device rejected\n"));
822 static int hifDeviceInserted(struct sdio_func *func, const struct sdio_device_id *id)
825 struct hif_device * device;
828 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
829 ("AR6000: hifDeviceInserted, Function: 0x%X, Vendor ID: 0x%X, Device ID: 0x%X, block size: 0x%X/0x%X\n",
830 func->num, func->vendor, func->device, func->max_blksize, func->cur_blksize));
833 device = getHifDevice(func);
836 device->is_disabled = true;
838 spin_lock_init(&device->lock);
840 spin_lock_init(&device->asynclock);
842 DL_LIST_INIT(&device->ScatterReqHead);
844 if (!nohifscattersupport) {
845 /* try to allow scatter operation on all instances,
846 * unless globally overridden */
847 device->scatter_enabled = true;
850 /* Initialize the bus requests to be used later */
851 A_MEMZERO(device->busRequest, sizeof(device->busRequest));
852 for (count = 0; count < BUS_REQUEST_MAX_NUM; count ++) {
853 sema_init(&device->busRequest[count].sem_req, 0);
854 hifFreeBusRequest(device, &device->busRequest[count]);
856 sema_init(&device->sem_async, 0);
858 ret = hifEnableFunc(device, func);
865 HIFAckInterrupt(struct hif_device *device)
867 AR_DEBUG_ASSERT(device != NULL);
869 /* Acknowledge our function IRQ */
873 HIFUnMaskInterrupt(struct hif_device *device)
877 AR_DEBUG_ASSERT(device != NULL);
878 AR_DEBUG_ASSERT(device->func != NULL);
880 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFUnMaskInterrupt\n"));
882 /* Register the IRQ Handler */
883 sdio_claim_host(device->func);
884 ret = sdio_claim_irq(device->func, hifIRQHandler);
885 sdio_release_host(device->func);
886 AR_DEBUG_ASSERT(ret == 0);
889 void HIFMaskInterrupt(struct hif_device *device)
892 AR_DEBUG_ASSERT(device != NULL);
893 AR_DEBUG_ASSERT(device->func != NULL);
895 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFMaskInterrupt\n"));
897 /* Mask our function IRQ */
898 sdio_claim_host(device->func);
899 while (atomic_read(&device->irqHandling)) {
900 sdio_release_host(device->func);
901 schedule_timeout(HZ/10);
902 sdio_claim_host(device->func);
904 ret = sdio_release_irq(device->func);
905 sdio_release_host(device->func);
906 AR_DEBUG_ASSERT(ret == 0);
909 BUS_REQUEST *hifAllocateBusRequest(struct hif_device *device)
911 BUS_REQUEST *busrequest;
915 spin_lock_irqsave(&device->lock, flag);
917 /* Remove first in list */
918 if((busrequest = device->s_busRequestFreeQueue) != NULL)
920 device->s_busRequestFreeQueue = busrequest->next;
923 spin_unlock_irqrestore(&device->lock, flag);
924 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifAllocateBusRequest: 0x%p\n", busrequest));
929 hifFreeBusRequest(struct hif_device *device, BUS_REQUEST *busrequest)
933 AR_DEBUG_ASSERT(busrequest != NULL);
934 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifFreeBusRequest: 0x%p\n", busrequest));
936 spin_lock_irqsave(&device->lock, flag);
939 /* Insert first in list */
940 busrequest->next = device->s_busRequestFreeQueue;
941 busrequest->inusenext = NULL;
942 device->s_busRequestFreeQueue = busrequest;
945 spin_unlock_irqrestore(&device->lock, flag);
948 static int hifDisableFunc(struct hif_device *device, struct sdio_func *func)
953 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifDisableFunc\n"));
954 device = getHifDevice(func);
955 if (!IS_ERR(device->async_task)) {
956 init_completion(&device->async_completion);
957 device->async_shutdown = 1;
958 up(&device->sem_async);
959 wait_for_completion(&device->async_completion);
960 device->async_task = NULL;
962 /* Disable the card */
963 sdio_claim_host(device->func);
964 ret = sdio_disable_func(device->func);
969 if (reset_sdio_on_unload) {
970 /* reset the SDIO interface. This is useful in automated testing where the card
971 * does not need to be removed at the end of the test. It is expected that the user will
972 * also unload/reload the host controller driver to force the bus driver to re-enumerate the slot */
973 AR_DEBUG_PRINTF(ATH_DEBUG_WARN, ("AR6000: reseting SDIO card back to uninitialized state \n"));
975 /* NOTE : sdio_f0_writeb() cannot be used here, that API only allows access
976 * to undefined registers in the range of: 0xF0-0xFF */
978 ret = Func0_CMD52WriteByte(device->func->card, SDIO_CCCR_ABORT, (1 << 3));
981 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: reset failed : %d \n",ret));
985 sdio_release_host(device->func);
988 device->is_disabled = true;
990 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDisableFunc\n"));
995 static int hifEnableFunc(struct hif_device *device, struct sdio_func *func)
997 struct task_struct* pTask;
998 const char *taskName = NULL;
999 int (*taskFunc)(void *) = NULL;
1002 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifEnableFunc\n"));
1003 device = getHifDevice(func);
1005 if (device->is_disabled) {
1006 /* enable the SDIO function */
1007 sdio_claim_host(func);
1009 if ((device->id->device & MANUFACTURER_ID_AR6K_BASE_MASK) >= MANUFACTURER_ID_AR6003_BASE) {
1010 /* enable 4-bit ASYNC interrupt on AR6003 or later devices */
1011 ret = Func0_CMD52WriteByte(func->card, CCCR_SDIO_IRQ_MODE_REG, SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
1013 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: failed to enable 4-bit ASYNC IRQ mode %d \n",ret));
1014 sdio_release_host(func);
1017 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: 4-bit ASYNC IRQ mode enabled\n"));
1019 /* give us some time to enable, in ms */
1020 func->enable_timeout = 100;
1021 ret = sdio_enable_func(func);
1023 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), Unable to enable AR6K: 0x%X\n",
1024 __FUNCTION__, ret));
1025 sdio_release_host(func);
1028 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
1029 sdio_release_host(func);
1031 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), Unable to set block size 0x%x AR6K: 0x%X\n",
1032 __FUNCTION__, HIF_MBOX_BLOCK_SIZE, ret));
1035 device->is_disabled = false;
1036 /* create async I/O thread */
1037 if (!device->async_task) {
1038 device->async_shutdown = 0;
1039 device->async_task = kthread_create(async_task,
1042 if (IS_ERR(device->async_task)) {
1043 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), to create async task\n", __FUNCTION__));
1046 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: start async task\n"));
1047 wake_up_process(device->async_task );
1051 if (!device->claimedContext) {
1052 taskFunc = startup_task;
1053 taskName = "AR6K startup";
1055 #if defined(CONFIG_PM)
1057 taskFunc = enable_task;
1058 taskName = "AR6K enable";
1060 #endif /* CONFIG_PM */
1062 /* create resume thread */
1063 pTask = kthread_create(taskFunc, (void *)device, taskName);
1064 if (IS_ERR(pTask)) {
1065 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), to create enabel task\n", __FUNCTION__));
1068 wake_up_process(pTask);
1069 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifEnableFunc\n"));
1071 /* task will call the enable func, indicate pending */
1075 #if defined(CONFIG_PM)
1076 static int hifDeviceSuspend(struct device *dev)
1078 struct sdio_func *func=dev_to_sdio_func(dev);
1080 struct hif_device *device;
1082 device = getHifDevice(func);
1083 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifDeviceSuspend\n"));
1084 if (device && device->claimedContext && osdrvCallbacks.deviceSuspendHandler) {
1085 device->is_suspend = true; /* set true first for PowerStateChangeNotify(..) */
1086 status = osdrvCallbacks.deviceSuspendHandler(device->claimedContext);
1088 device->is_suspend = false;
1091 CleanupHIFScatterResources(device);
1092 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceSuspend\n"));
1098 return -EBUSY; /* Hack for kernel in order to support deep sleep and wow */
1104 static int hifDeviceResume(struct device *dev)
1106 struct sdio_func *func=dev_to_sdio_func(dev);
1108 struct hif_device *device;
1110 device = getHifDevice(func);
1111 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifDeviceResume\n"));
1112 if (device && device->claimedContext && osdrvCallbacks.deviceSuspendHandler) {
1113 status = osdrvCallbacks.deviceResumeHandler(device->claimedContext);
1115 device->is_suspend = false;
1118 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceResume\n"));
1122 #endif /* CONFIG_PM */
1124 static void hifDeviceRemoved(struct sdio_func *func)
1127 struct hif_device *device;
1128 AR_DEBUG_ASSERT(func != NULL);
1130 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifDeviceRemoved\n"));
1131 device = getHifDevice(func);
1132 if (device->claimedContext != NULL) {
1133 status = osdrvCallbacks.deviceRemovedHandler(device->claimedContext, device);
1136 if (device->is_disabled) {
1137 device->is_disabled = false;
1139 status = hifDisableFunc(device, func);
1141 CleanupHIFScatterResources(device);
1143 delHifDevice(device);
1144 AR_DEBUG_ASSERT(status == 0);
1145 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceRemoved\n"));
1149 * This should be moved to AR6K HTC layer.
1151 int hifWaitForPendingRecv(struct hif_device *device)
1158 while (atomic_read(&device->irqHandling)) {
1159 /* wait until irq handler finished all the jobs */
1160 schedule_timeout(HZ/10);
1162 /* check if there is any pending irq due to force done */
1163 host_int_status = 0;
1164 status = HIFReadWrite(device, HOST_INT_STATUS_ADDRESS,
1165 (u8 *)&host_int_status, sizeof(host_int_status),
1166 HIF_RD_SYNC_BYTE_INC, NULL);
1167 host_int_status = !status ? (host_int_status & (1 << 0)) : 0;
1168 if (host_int_status) {
1169 schedule(); /* schedule for next dsrHandler */
1171 } while (host_int_status && --cnt > 0);
1173 if (host_int_status && cnt == 0) {
1174 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
1175 ("AR6000: %s(), Unable clear up pending IRQ before the system suspended\n", __FUNCTION__));
1182 static struct hif_device *
1183 addHifDevice(struct sdio_func *func)
1185 struct hif_device *hifdevice;
1186 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: addHifDevice\n"));
1187 AR_DEBUG_ASSERT(func != NULL);
1188 hifdevice = kzalloc(sizeof(struct hif_device), GFP_KERNEL);
1189 AR_DEBUG_ASSERT(hifdevice != NULL);
1190 #if HIF_USE_DMA_BOUNCE_BUFFER
1191 hifdevice->dma_buffer = kmalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
1192 AR_DEBUG_ASSERT(hifdevice->dma_buffer != NULL);
1194 hifdevice->func = func;
1195 hifdevice->powerConfig = HIF_DEVICE_POWER_UP;
1196 sdio_set_drvdata(func, hifdevice);
1197 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: addHifDevice; 0x%p\n", hifdevice));
1201 static struct hif_device *
1202 getHifDevice(struct sdio_func *func)
1204 AR_DEBUG_ASSERT(func != NULL);
1205 return (struct hif_device *)sdio_get_drvdata(func);
1209 delHifDevice(struct hif_device * device)
1211 AR_DEBUG_ASSERT(device!= NULL);
1212 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: delHifDevice; 0x%p\n", device));
1213 kfree(device->dma_buffer);
1217 static void ResetAllCards(void)
1221 void HIFClaimDevice(struct hif_device *device, void *context)
1223 device->claimedContext = context;
1226 void HIFReleaseDevice(struct hif_device *device)
1228 device->claimedContext = NULL;
1231 int HIFAttachHTC(struct hif_device *device, HTC_CALLBACKS *callbacks)
1233 if (device->htcCallbacks.context != NULL) {
1234 /* already in use! */
1237 device->htcCallbacks = *callbacks;
1241 void HIFDetachHTC(struct hif_device *device)
1243 A_MEMZERO(&device->htcCallbacks,sizeof(device->htcCallbacks));
1246 #define SDIO_SET_CMD52_ARG(arg,rw,func,raw,address,writedata) \
1247 (arg) = (((rw) & 1) << 31) | \
1248 (((func) & 0x7) << 28) | \
1249 (((raw) & 1) << 27) | \
1251 (((address) & 0x1FFFF) << 9) | \
1253 ((writedata) & 0xFF)
1255 #define SDIO_SET_CMD52_READ_ARG(arg,func,address) \
1256 SDIO_SET_CMD52_ARG(arg,0,(func),0,address,0x00)
1257 #define SDIO_SET_CMD52_WRITE_ARG(arg,func,address,value) \
1258 SDIO_SET_CMD52_ARG(arg,1,(func),0,address,value)
1260 static int Func0_CMD52WriteByte(struct mmc_card *card, unsigned int address, unsigned char byte)
1262 struct mmc_command ioCmd;
1265 memset(&ioCmd,0,sizeof(ioCmd));
1266 SDIO_SET_CMD52_WRITE_ARG(arg,0,address,byte);
1267 ioCmd.opcode = SD_IO_RW_DIRECT;
1269 ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1271 return mmc_wait_for_cmd(card->host, &ioCmd, 0);
1274 static int Func0_CMD52ReadByte(struct mmc_card *card, unsigned int address, unsigned char *byte)
1276 struct mmc_command ioCmd;
1280 memset(&ioCmd,0,sizeof(ioCmd));
1281 SDIO_SET_CMD52_READ_ARG(arg,0,address);
1282 ioCmd.opcode = SD_IO_RW_DIRECT;
1284 ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1286 err = mmc_wait_for_cmd(card->host, &ioCmd, 0);
1288 if ((!err) && (byte)) {
1289 *byte = ioCmd.resp[0] & 0xFF;