ath6kl: propagate errors on module setup
[pandora-kernel.git] / drivers / staging / ath6kl / hif / sdio / linux_sdio / src / hif.c
1 //------------------------------------------------------------------------------
2 // <copyright file="hif.c" company="Atheros">
3 //    Copyright (c) 2004-2010 Atheros Corporation.  All rights reserved.
4 // 
5 //
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.
9 //
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.
17 //
18 //
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // HIF layer reference implementation for Linux Native MMC stack
22 //
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>
33
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
39 #include "a_debug.h"
40 #include "AR6002/hw2.0/hw/mbox_host_reg.h"
41
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.  
46  */
47 #define BUFFER_NEEDS_BOUNCE(buffer)  (((unsigned long)(buffer) & 0x3) || !virt_addr_valid((buffer)))
48 #else
49 #define BUFFER_NEEDS_BOUNCE(buffer)   (false)
50 #endif
51
52 /* ATHENV */
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);
66
67 int reset_sdio_on_unload = 0;
68 module_param(reset_sdio_on_unload, int, 0644);
69
70 extern u32 nohifscattersupport;
71
72
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))  },
79     { /* null */                                         },
80 };
81 MODULE_DEVICE_TABLE(sdio, ar6k_id_table);
82
83 static struct sdio_driver ar6k_driver = {
84         .name = "ar6k_wlan",
85         .id_table = ar6k_id_table,
86         .probe = hifDeviceInserted,
87         .remove = hifDeviceRemoved,
88 };
89
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
94      */
95 static struct dev_pm_ops ar6k_device_pm_ops = {
96         .suspend = hifDeviceSuspend,
97         .resume = hifDeviceResume,
98 };
99 #endif /* CONFIG_PM */
100
101 /* make sure we only unregister when registered. */
102 static int registered = 0;
103
104 OSDRV_CALLBACKS osdrvCallbacks;
105 extern u32 onebitmode;
106 extern u32 busspeedlow;
107 extern u32 debughif;
108
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);
112
113 #ifdef DEBUG
114
115 ATH_DEBUG_INSTANTIATE_MODULE_VAR(hif,
116                                  "hif",
117                                  "(Linux MMC) Host Interconnect Framework",
118                                  ATH_DEBUG_MASK_DEFAULTS,
119                                  0,
120                                  NULL);
121                                  
122 #endif
123
124
125 /* ------ Functions ------ */
126 int HIFInit(OSDRV_CALLBACKS *callbacks)
127 {
128         int r;
129         AR_DEBUG_ASSERT(callbacks != NULL);
130
131         A_REGISTER_MODULE_DEBUG_INFO(hif);
132
133         /* store the callback handlers */
134         osdrvCallbacks = *callbacks;
135
136         /* Register with bus driver core */
137         registered = 1;
138
139 #if defined(CONFIG_PM)
140         if (callbacks->deviceSuspendHandler && callbacks->deviceResumeHandler)
141                 ar6k_driver.drv.pm = &ar6k_device_pm_ops;
142 #endif /* CONFIG_PM */
143
144         r = sdio_register_driver(&ar6k_driver);
145         if (r < 0)
146                 return r;
147
148         return 0;
149 }
150
151 static int
152 __HIFReadWrite(struct hif_device *device,
153              u32 address,
154              u8 *buffer,
155              u32 length,
156              u32 request,
157              void *context)
158 {
159     u8 opcode;
160     int    status = 0;
161     int     ret;
162     u8 *tbuffer;
163     bool   bounced = false;
164
165     AR_DEBUG_ASSERT(device != NULL);
166     AR_DEBUG_ASSERT(device->func != NULL);
167
168     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: 0x%p, buffer:0x%p (addr:0x%X)\n", 
169                     device, buffer, address));
170
171     do {
172         if (request & HIF_EXTENDED_IO) {
173             //AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Command type: CMD53\n"));
174         } else {
175             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
176                             ("AR6000: Invalid command type: 0x%08x\n", request));
177             status = A_EINVAL;
178             break;
179         }
180
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",
186                             length));
187         } else if (request & HIF_BYTE_BASIS) {
188             AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
189                             ("AR6000: Byte mode (BlockLen: %d)\n",
190                             length));
191         } else {
192             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
193                             ("AR6000: Invalid data mode: 0x%08x\n", request));
194             status = A_EINVAL;
195             break;
196         }
197
198 #if 0
199         /* useful for checking register accesses */
200         if (length & 0x3) {
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);
203         }
204 #endif
205
206         if (request & HIF_WRITE) {
207             if ((address >= HIF_MBOX_START_ADDR(0)) &&
208                 (address <= HIF_MBOX_END_ADDR(3)))
209             {
210     
211                 AR_DEBUG_ASSERT(length <= HIF_MBOX_WIDTH);
212     
213                 /*
214                  * Mailbox write. Adjust the address so that the last byte
215                  * falls on the EOM address.
216                  */
217                 address += (HIF_MBOX_WIDTH - length);
218             }
219         }
220
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));
227         } else {
228             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
229                             ("AR6000: Invalid address mode: 0x%08x\n", request));
230             status = A_EINVAL;
231             break;
232         }
233
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);
242                 bounced = true;
243             } else {
244                 tbuffer = buffer;    
245             }
246 #else
247                 tbuffer = buffer;
248 #endif
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));
253             } else {
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));
257             }
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;
264                 bounced = true;
265             } else {
266                 tbuffer = buffer;    
267             }
268 #else
269             tbuffer = buffer;
270 #endif
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));
275             } else {
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));
279             }
280 #if HIF_USE_DMA_BOUNCE_BUFFER
281             if (bounced) {
282                    /* copy the read data from the dma buffer */
283                 memcpy(buffer, tbuffer, length);
284             }
285 #endif
286         } else {
287             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
288                             ("AR6000: Invalid direction: 0x%08x\n", request));
289             status = A_EINVAL;
290             break;
291         }
292
293         if (ret) {
294             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
295                             ("AR6000: SDIO bus operation failed! MMC stack returned : %d \n", ret));
296             status = A_ERROR;
297         }
298     } while (false);
299
300     return status;
301 }
302
303 void AddToAsyncList(struct hif_device *device, BUS_REQUEST *busrequest)
304 {
305     unsigned long flags;
306     BUS_REQUEST *async;
307     BUS_REQUEST *active;
308     
309     spin_lock_irqsave(&device->asynclock, flags);
310     active = device->asyncreq;
311     if (active == NULL) {
312         device->asyncreq = busrequest;
313         device->asyncreq->inusenext = NULL;
314     } else {
315         for (async = device->asyncreq;
316              async != NULL;
317              async = async->inusenext) {
318              active =  async;
319         }
320         active->inusenext = busrequest;
321         busrequest->inusenext = NULL;
322     }
323     spin_unlock_irqrestore(&device->asynclock, flags);
324 }
325
326
327 /* queue a read/write request */
328 int
329 HIFReadWrite(struct hif_device *device,
330              u32 address,
331              u8 *buffer,
332              u32 length,
333              u32 request,
334              void *context)
335 {
336     int    status = 0;
337     BUS_REQUEST *busrequest;
338
339
340     AR_DEBUG_ASSERT(device != NULL);
341     AR_DEBUG_ASSERT(device->func != NULL);
342
343     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device: %p addr:0x%X\n", device,address));
344
345     do {            
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));
355                 return A_ERROR;
356             }
357             busrequest->address = address;
358             busrequest->buffer = buffer;
359             busrequest->length = length;
360             busrequest->request = request;
361             busrequest->context = context;
362             
363             AddToAsyncList(device, busrequest);
364             
365             if (request & HIF_SYNCHRONOUS) {
366                 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued sync req: 0x%lX\n", (unsigned long)busrequest));
367
368                 /* wait for completion */
369                 up(&device->sem_async);
370                 if (down_interruptible(&busrequest->sem_req) != 0) {
371                     /* interrupted, exit */
372                     return A_ERROR;
373                 } else {
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);
379                     return status;
380                 }
381             } else {
382                 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: queued async req: 0x%lX\n", (unsigned long)busrequest));
383                 up(&device->sem_async);
384                 return A_PENDING;
385             }
386         } else {
387             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
388                             ("AR6000: Invalid execution mode: 0x%08x\n", (unsigned int)request));
389             status = A_EINVAL;
390             break;
391         }
392     } while(0);
393
394     return status;
395 }
396 /* thread to serialize all requests, both sync and async */
397 static int async_task(void *param)
398  {
399     struct hif_device *device;
400     BUS_REQUEST *request;
401     int status;
402     unsigned long flags;
403
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) {
408         /* wait for work */
409         if (down_interruptible(&device->sem_async) != 0) {
410             /* interrupted, exit */
411             AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task interrupted\n"));
412             break;
413         }
414         if (device->async_shutdown) {
415             AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: async task stopping\n"));
416             break;
417         }
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;
426             } else {
427                 device->asyncreq = NULL;
428             }
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));
431             
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);
438             } else {                
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);
448                 } else {
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);
452                 }
453             }
454             spin_lock_irqsave(&device->asynclock, flags);
455         }
456         spin_unlock_irqrestore(&device->asynclock, flags);
457         sdio_release_host(device->func);
458     }
459
460     complete_and_exit(&device->async_completion, 0);
461     return 0;
462 }
463
464 static s32 IssueSDCommand(struct hif_device *device, u32 opcode, u32 arg, u32 flags, u32 *resp)
465 {
466     struct mmc_command cmd;
467     s32 err;
468     struct mmc_host *host;
469     struct sdio_func *func;
470
471     func = device->func;
472     host = func->card->host;
473
474     memset(&cmd, 0, sizeof(struct mmc_command)); 
475     cmd.opcode = opcode;
476     cmd.arg = arg;
477     cmd.flags = flags;
478     err = mmc_wait_for_cmd(host, &cmd, 3);
479
480     if ((!err) && (resp)) {
481         *resp = cmd.resp[0];
482     }
483
484     return err;
485 }
486
487 int ReinitSDIO(struct hif_device *device)
488 {
489     s32 err;
490     struct mmc_host *host;
491     struct mmc_card *card;
492         struct sdio_func *func;
493     u8 cmd52_resp;
494     u32 clock;
495
496     func = device->func;
497     card = func->card;
498     host = card->host;
499    
500     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +ReinitSDIO \n"));
501     sdio_claim_host(func);
502
503     do {
504         if (!device->is_suspend) {
505             u32 resp;
506             u16 rca;
507             u32 i;
508             int bit = fls(host->ocr_avail) - 1;
509             /* emulate the mmc_power_up(...) */
510             host->ios.vdd = bit;
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);
517             /*
518              * This delay should be sufficient to allow the power supply
519              * to reach the minimum voltage.
520              */
521             msleep(2);
522
523             host->ios.clock = host->f_min;
524             host->ios.power_mode = MMC_POWER_ON;
525             host->ops->set_ios(host, &host->ios);
526
527             /*
528              * This delay must be at least 74 clock sizes, or 1 ms, or the
529              * time required to reach a stable voltage.
530              */
531             msleep(2);
532
533             /* Issue CMD0. Goto idle state */
534                 host->ios.chip_select = MMC_CS_HIGH;
535             host->ops->set_ios(host, &host->ios);
536                 msleep(1);
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);
540                 msleep(1);
541             host->use_spi_crc = 0;
542
543             if (err) {
544                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD0 failed : %d \n",err));    
545                 break;
546             }        
547
548             if (!host->ocr) {
549                 /* Issue CMD5, arg = 0 */
550                 err = IssueSDCommand(device, SD_IO_SEND_OP_COND, 0, (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
551                 if (err) {
552                     AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));    
553                     break;
554                 }
555                 host->ocr = resp;
556             }
557
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);
561                 if (err) {
562                     AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD5 failed : %d \n",err));    
563                     break;
564                 }
565                 if (resp & MMC_CARD_BUSY) {
566                     break;
567                 }
568                 msleep(10);
569             }
570
571             if ((i == 100) || (err)) {
572                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: card in not ready : %d %d \n",i,err));    
573                 break;
574             }
575
576             /* Issue CMD3, get RCA */
577             err = IssueSDCommand(device, SD_SEND_RELATIVE_ADDR, 0, MMC_RSP_R6 | MMC_CMD_BCR, &resp);
578             if (err) {
579                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD3 failed : %d \n",err));    
580                 break;
581             }
582             rca = resp >> 16;
583             host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
584             host->ops->set_ios(host, &host->ios);
585
586             /* Issue CMD7, select card  */
587             err = IssueSDCommand(device, MMC_SELECT_CARD, (rca << 16), MMC_RSP_R1 | MMC_CMD_AC, NULL);
588             if (err) {
589                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD7 failed : %d \n",err));    
590                 break;
591             }
592         }
593         
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);
598             if (err) {
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 */
602             } else {
603                 err = Func0_CMD52WriteByte(card, SDIO_CCCR_SPEED, (cmd52_resp | SDIO_SPEED_EHS));
604                 if (err) {
605                     AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 write to CCCR speed register failed  : %d \n",err));    
606                     break;
607                 }
608                 mmc_card_set_highspeed(card);
609                 host->ios.timing = MMC_TIMING_SD_HS;
610                 host->ops->set_ios(host, &host->ios);
611             }
612         }
613
614         /* Set clock */
615         if (mmc_card_highspeed(card)) {
616             clock = 50000000;
617         } else {
618             clock = card->cis.max_dtr;
619         }
620         
621         if (clock > host->f_max) {
622             clock = host->f_max;
623         }
624         host->ios.clock = clock;
625         host->ops->set_ios(host, &host->ios);
626         
627
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);
631             if (err) {
632                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("ReinitSDIO: CMD52 to set bus mode failed : %d \n",err));    
633                 break;
634             }
635             host->ios.bus_width = MMC_BUS_WIDTH_4;
636             host->ops->set_ios(host, &host->ios);
637         }
638     } while (0);
639
640     sdio_release_host(func);
641     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -ReinitSDIO \n"));
642
643     return (err) ? A_ERROR : 0;
644 }
645
646 int
647 PowerStateChangeNotify(struct hif_device *device, HIF_DEVICE_POWER_CHANGE_TYPE config)
648 {
649     int status = 0;
650 #if defined(CONFIG_PM)
651         struct sdio_func *func = device->func;
652     int old_reset_val;
653     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +PowerStateChangeNotify %d\n", config));
654     switch (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;
663                     host->ios.clock = 0;
664                     host->ios.vdd = 0;
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);
671             }
672             break;
673        case HIF_DEVICE_POWER_UP:
674             if (device->powerConfig == HIF_DEVICE_POWER_CUT) {
675                 status = ReinitSDIO(device);
676             }
677             if (status == 0) {
678                 status = hifEnableFunc(device, func);
679             }
680             break;
681     } 
682     device->powerConfig = config;
683
684     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -PowerStateChangeNotify\n"));
685 #endif
686     return status;
687 }
688
689 int
690 HIFConfigureDevice(struct hif_device *device, HIF_DEVICE_CONFIG_OPCODE opcode,
691                    void *config, u32 configLen)
692 {
693     u32 count;
694     int status = 0;
695     
696     switch(opcode) {
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;
702             break;
703
704         case HIF_DEVICE_GET_MBOX_ADDR:
705             for (count = 0; count < 4; count ++) {
706                 ((u32 *)config)[count] = HIF_MBOX_START_ADDR(count);
707             }
708             
709             if (configLen >= sizeof(struct hif_device_mbox_info)) {    
710                 SetExtendedMboxWindowInfo((u16)device->func->device,
711                                           (struct hif_device_mbox_info *)config);
712             }
713                         
714             break;
715         case HIF_DEVICE_GET_IRQ_PROC_MODE:
716             *((HIF_DEVICE_IRQ_PROCESSING_MODE *)config) = HIF_DEVICE_IRQ_SYNC_ONLY;
717             break;
718        case HIF_CONFIGURE_QUERY_SCATTER_REQUEST_SUPPORT:
719             if (!device->scatter_enabled) {
720                 return A_ENOTSUP;    
721             }
722             status = SetupHIFScatterSupport(device, (struct hif_device_scatter_support_info *)config);
723             if (status) {
724                 device->scatter_enabled = false;
725             }
726             break; 
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;
730             break; 
731         case HIF_DEVICE_POWER_STATE_CHANGE:
732             status = PowerStateChangeNotify(device, *(HIF_DEVICE_POWER_CHANGE_TYPE *)config);
733             break;
734         default:
735             AR_DEBUG_PRINTF(ATH_DEBUG_WARN,
736                             ("AR6000: Unsupported configuration opcode: %d\n", opcode));
737             status = A_ERROR;
738     }
739
740     return status;
741 }
742
743 void
744 HIFShutDownDevice(struct hif_device *device)
745 {
746     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +HIFShutDownDevice\n"));
747     if (device != NULL) {
748         AR_DEBUG_ASSERT(device->func != NULL);
749     } else {
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
753              * enumerated */
754         AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFShutDownDevice, resetting\n"));
755         ResetAllCards();
756
757         /* Unregister with bus driver core */
758         if (registered) {
759             registered = 0;
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"));
765         }
766     }
767     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -HIFShutDownDevice\n"));
768 }
769
770 static void
771 hifIRQHandler(struct sdio_func *func)
772 {
773     int status;
774     struct hif_device *device;
775     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifIRQHandler\n"));
776
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"));
786 }
787
788 /* handle HTC startup via thread*/
789 static int startup_task(void *param)
790 {
791     struct hif_device *device;
792
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"));
798     }
799     return 0;
800 }
801
802 #if defined(CONFIG_PM)
803 static int enable_task(void *param)
804 {
805     struct hif_device *device;
806     device = (struct hif_device *)param;
807     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: call  from resume_task\n"));
808
809         /* start  up inform DRV layer */
810     if (device && 
811         device->claimedContext && 
812         osdrvCallbacks.devicePowerChangeHandler &&
813         osdrvCallbacks.devicePowerChangeHandler(device->claimedContext, HIF_DEVICE_POWER_UP) != 0)
814     {
815         AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: Device rejected\n"));
816     }
817
818     return 0;
819 }
820 #endif
821
822 static int hifDeviceInserted(struct sdio_func *func, const struct sdio_device_id *id)
823 {
824     int ret;
825     struct hif_device * device;
826     int count;
827
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));
831
832     addHifDevice(func);
833     device = getHifDevice(func);
834
835     device->id = id;
836     device->is_disabled = true;
837
838     spin_lock_init(&device->lock);
839
840     spin_lock_init(&device->asynclock);
841     
842     DL_LIST_INIT(&device->ScatterReqHead);
843     
844     if (!nohifscattersupport) {
845             /* try to allow scatter operation on all instances,
846              * unless globally overridden */
847         device->scatter_enabled = true;
848     }
849
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]);
855     }
856     sema_init(&device->sem_async, 0);
857     
858     ret  = hifEnableFunc(device, func);
859
860     return ret;
861 }
862
863
864 void
865 HIFAckInterrupt(struct hif_device *device)
866 {
867     AR_DEBUG_ASSERT(device != NULL);
868
869     /* Acknowledge our function IRQ */
870 }
871
872 void
873 HIFUnMaskInterrupt(struct hif_device *device)
874 {
875     int ret;
876
877     AR_DEBUG_ASSERT(device != NULL);
878     AR_DEBUG_ASSERT(device->func != NULL);
879
880     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFUnMaskInterrupt\n"));
881
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);
887 }
888
889 void HIFMaskInterrupt(struct hif_device *device)
890 {
891     int ret;
892     AR_DEBUG_ASSERT(device != NULL);
893     AR_DEBUG_ASSERT(device->func != NULL);
894
895     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: HIFMaskInterrupt\n"));
896
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);
903     }
904     ret = sdio_release_irq(device->func);
905     sdio_release_host(device->func);
906     AR_DEBUG_ASSERT(ret == 0);
907 }
908
909 BUS_REQUEST *hifAllocateBusRequest(struct hif_device *device)
910 {
911     BUS_REQUEST *busrequest;
912     unsigned long flag;
913
914     /* Acquire lock */
915     spin_lock_irqsave(&device->lock, flag);
916
917     /* Remove first in list */
918     if((busrequest = device->s_busRequestFreeQueue) != NULL)
919     {
920         device->s_busRequestFreeQueue = busrequest->next;
921     }
922     /* Release lock */
923     spin_unlock_irqrestore(&device->lock, flag);
924     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifAllocateBusRequest: 0x%p\n", busrequest));
925     return busrequest;
926 }
927
928 void
929 hifFreeBusRequest(struct hif_device *device, BUS_REQUEST *busrequest)
930 {
931     unsigned long flag;
932
933     AR_DEBUG_ASSERT(busrequest != NULL);
934     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: hifFreeBusRequest: 0x%p\n", busrequest));
935     /* Acquire lock */
936     spin_lock_irqsave(&device->lock, flag);
937
938
939     /* Insert first in list */
940     busrequest->next = device->s_busRequestFreeQueue;
941     busrequest->inusenext = NULL;
942     device->s_busRequestFreeQueue = busrequest;
943
944     /* Release lock */
945     spin_unlock_irqrestore(&device->lock, flag);
946 }
947
948 static int hifDisableFunc(struct hif_device *device, struct sdio_func *func)
949 {
950     int ret;
951     int status = 0;
952
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;
961     }
962     /* Disable the card */
963     sdio_claim_host(device->func);
964     ret = sdio_disable_func(device->func);
965     if (ret) {
966         status = A_ERROR;
967     } 
968
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"));
974         
975         /* NOTE : sdio_f0_writeb() cannot be used here, that API only allows access
976          *        to undefined registers in the range of: 0xF0-0xFF */
977          
978         ret = Func0_CMD52WriteByte(device->func->card, SDIO_CCCR_ABORT, (1 << 3)); 
979         if (ret) {
980             status = A_ERROR;
981             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: reset failed : %d \n",ret));    
982         }
983     }
984
985     sdio_release_host(device->func);
986
987     if (status == 0) {
988         device->is_disabled = true;
989     }
990     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDisableFunc\n"));
991
992     return status;
993 }
994
995 static int hifEnableFunc(struct hif_device *device, struct sdio_func *func)
996 {
997     struct task_struct* pTask;
998     const char *taskName = NULL;
999     int (*taskFunc)(void *) = NULL;
1000     int ret = 0;
1001     
1002     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: +hifEnableFunc\n"));
1003     device = getHifDevice(func);
1004
1005     if (device->is_disabled) {
1006        /* enable the SDIO function */
1007         sdio_claim_host(func);
1008
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);
1012             if (ret) {
1013                 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("AR6000: failed to enable 4-bit ASYNC IRQ mode %d \n",ret));
1014                 sdio_release_host(func);
1015                 return A_ERROR;
1016             }
1017             AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: 4-bit ASYNC IRQ mode enabled\n"));
1018         }
1019         /* give us some time to enable, in ms */
1020         func->enable_timeout = 100;
1021         ret = sdio_enable_func(func);
1022         if (ret) {
1023             AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), Unable to enable AR6K: 0x%X\n",
1024                                           __FUNCTION__, ret));
1025             sdio_release_host(func);
1026             return A_ERROR;
1027         }
1028         ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
1029         sdio_release_host(func);
1030         if (ret) {
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));
1033             return A_ERROR;
1034         }
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,
1040                                            (void *)device,
1041                                            "AR6K Async");
1042            if (IS_ERR(device->async_task)) {
1043                AR_DEBUG_PRINTF(ATH_DEBUG_ERROR, ("AR6000: %s(), to create async task\n", __FUNCTION__));
1044                 return A_ERROR;
1045            }
1046            AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: start async task\n"));
1047            wake_up_process(device->async_task );    
1048         }
1049     }
1050
1051     if (!device->claimedContext) {
1052         taskFunc = startup_task;
1053         taskName = "AR6K startup";
1054         ret = 0;
1055 #if defined(CONFIG_PM)
1056     } else {
1057         taskFunc = enable_task;
1058         taskName = "AR6K enable";
1059         ret = A_PENDING;
1060 #endif /* CONFIG_PM */
1061     }
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__));
1066         return A_ERROR;
1067     }
1068     wake_up_process(pTask);
1069     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifEnableFunc\n"));
1070
1071     /* task will call the enable func, indicate pending */
1072     return ret;
1073 }
1074
1075 #if defined(CONFIG_PM)
1076 static int hifDeviceSuspend(struct device *dev)
1077 {
1078     struct sdio_func *func=dev_to_sdio_func(dev);
1079     int status = 0;
1080     struct hif_device *device;   
1081
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);
1087         if (status) {
1088             device->is_suspend = false;
1089         }
1090     }
1091     CleanupHIFScatterResources(device);
1092     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceSuspend\n"));
1093
1094     switch (status) {
1095     case 0:
1096         return 0;
1097     case A_EBUSY:
1098         return -EBUSY; /* Hack for kernel in order to support deep sleep and wow */
1099     default:
1100         return -1;
1101     }
1102 }
1103
1104 static int hifDeviceResume(struct device *dev)
1105 {
1106     struct sdio_func *func=dev_to_sdio_func(dev);
1107     int status = 0;
1108     struct hif_device *device;   
1109
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);
1114         if (status == 0) {
1115             device->is_suspend = false;
1116         }
1117     }
1118     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceResume\n"));
1119
1120     return status;
1121 }
1122 #endif /* CONFIG_PM */
1123
1124 static void hifDeviceRemoved(struct sdio_func *func)
1125 {
1126     int status = 0;
1127     struct hif_device *device;
1128     AR_DEBUG_ASSERT(func != NULL);
1129
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);
1134     }
1135
1136     if (device->is_disabled) {
1137         device->is_disabled = false;
1138     } else {
1139         status = hifDisableFunc(device, func);
1140     }
1141     CleanupHIFScatterResources(device);
1142      
1143     delHifDevice(device);
1144     AR_DEBUG_ASSERT(status == 0);
1145     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: -hifDeviceRemoved\n"));
1146 }
1147
1148 /*
1149  * This should be moved to AR6K HTC layer.
1150  */
1151 int hifWaitForPendingRecv(struct hif_device *device)
1152 {
1153     s32 cnt = 10;
1154     u8 host_int_status;
1155     int status = 0;
1156
1157     do {                            
1158         while (atomic_read(&device->irqHandling)) {
1159                 /* wait until irq handler finished all the jobs */
1160                         schedule_timeout(HZ/10);
1161             }
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 */
1170                 }
1171         } while (host_int_status && --cnt > 0);
1172
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__));
1176      }
1177
1178     return 0;
1179 }
1180     
1181
1182 static struct hif_device *
1183 addHifDevice(struct sdio_func *func)
1184 {
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);
1193 #endif
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));
1198     return hifdevice;
1199 }
1200
1201 static struct hif_device *
1202 getHifDevice(struct sdio_func *func)
1203 {
1204     AR_DEBUG_ASSERT(func != NULL);
1205     return (struct hif_device *)sdio_get_drvdata(func);
1206 }
1207
1208 static void
1209 delHifDevice(struct hif_device * device)
1210 {
1211     AR_DEBUG_ASSERT(device!= NULL);
1212     AR_DEBUG_PRINTF(ATH_DEBUG_TRACE, ("AR6000: delHifDevice; 0x%p\n", device));
1213     kfree(device->dma_buffer);
1214     kfree(device);
1215 }
1216
1217 static void ResetAllCards(void)
1218 {
1219 }
1220
1221 void HIFClaimDevice(struct hif_device  *device, void *context)
1222 {
1223     device->claimedContext = context;
1224 }
1225
1226 void HIFReleaseDevice(struct hif_device  *device)
1227 {
1228     device->claimedContext = NULL;
1229 }
1230
1231 int HIFAttachHTC(struct hif_device *device, HTC_CALLBACKS *callbacks)
1232 {
1233     if (device->htcCallbacks.context != NULL) {
1234             /* already in use! */
1235         return A_ERROR;
1236     }
1237     device->htcCallbacks = *callbacks;
1238     return 0;
1239 }
1240
1241 void HIFDetachHTC(struct hif_device *device)
1242 {
1243     A_MEMZERO(&device->htcCallbacks,sizeof(device->htcCallbacks));
1244 }
1245
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)          | \
1250             (1 << 26)                    | \
1251             (((address) & 0x1FFFF) << 9) | \
1252             (1 << 8)                     | \
1253             ((writedata) & 0xFF)
1254             
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)
1259     
1260 static int Func0_CMD52WriteByte(struct mmc_card *card, unsigned int address, unsigned char byte)
1261 {
1262     struct mmc_command ioCmd;
1263     unsigned long      arg;
1264     
1265     memset(&ioCmd,0,sizeof(ioCmd));
1266     SDIO_SET_CMD52_WRITE_ARG(arg,0,address,byte);
1267     ioCmd.opcode = SD_IO_RW_DIRECT;
1268     ioCmd.arg = arg;
1269     ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1270     
1271     return mmc_wait_for_cmd(card->host, &ioCmd, 0);
1272 }
1273
1274 static int Func0_CMD52ReadByte(struct mmc_card *card, unsigned int address, unsigned char *byte)
1275 {
1276     struct mmc_command ioCmd;
1277     unsigned long      arg;
1278     s32 err;
1279     
1280     memset(&ioCmd,0,sizeof(ioCmd));
1281     SDIO_SET_CMD52_READ_ARG(arg,0,address);
1282     ioCmd.opcode = SD_IO_RW_DIRECT;
1283     ioCmd.arg = arg;
1284     ioCmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1285
1286     err = mmc_wait_for_cmd(card->host, &ioCmd, 0);
1287
1288     if ((!err) && (byte)) {
1289         *byte =  ioCmd.resp[0] & 0xFF;
1290     }
1291
1292     return err;
1293 }