9b6282a1422562be8e4c1eb68e87c8f6609dfcfb
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / sdio.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/module.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/sdio_func.h>
23 #include <linux/mmc/sdio_ids.h>
24 #include <linux/mmc/sdio.h>
25 #include <linux/mmc/sd.h>
26 #include "hif.h"
27 #include "hif-ops.h"
28 #include "target.h"
29 #include "debug.h"
30 #include "cfg80211.h"
31
32 struct ath6kl_sdio {
33         struct sdio_func *func;
34
35         spinlock_t lock;
36
37         /* free list */
38         struct list_head bus_req_freeq;
39
40         /* available bus requests */
41         struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
42
43         struct ath6kl *ar;
44
45         u8 *dma_buffer;
46
47         /* protects access to dma_buffer */
48         struct mutex dma_buffer_mutex;
49
50         /* scatter request list head */
51         struct list_head scat_req;
52
53         atomic_t irq_handling;
54         wait_queue_head_t irq_wq;
55
56         spinlock_t scat_lock;
57         bool scatter_enabled;
58
59         bool is_disabled;
60         const struct sdio_device_id *id;
61         struct work_struct wr_async_work;
62         struct list_head wr_asyncq;
63         spinlock_t wr_async_lock;
64 };
65
66 #define CMD53_ARG_READ          0
67 #define CMD53_ARG_WRITE         1
68 #define CMD53_ARG_BLOCK_BASIS   1
69 #define CMD53_ARG_FIXED_ADDRESS 0
70 #define CMD53_ARG_INCR_ADDRESS  1
71
72 static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
73 {
74         return ar->hif_priv;
75 }
76
77 /*
78  * Macro to check if DMA buffer is WORD-aligned and DMA-able.
79  * Most host controllers assume the buffer is DMA'able and will
80  * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
81  * check fails on stack memory.
82  */
83 static inline bool buf_needs_bounce(u8 *buf)
84 {
85         return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
86 }
87
88 static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
89 {
90         struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
91
92         /* EP1 has an extended range */
93         mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
94         mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
95         mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
96         mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
97         mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
98         mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
99 }
100
101 static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
102                                              u8 mode, u8 opcode, u32 addr,
103                                              u16 blksz)
104 {
105         *arg = (((rw & 1) << 31) |
106                 ((func & 0x7) << 28) |
107                 ((mode & 1) << 27) |
108                 ((opcode & 1) << 26) |
109                 ((addr & 0x1FFFF) << 9) |
110                 (blksz & 0x1FF));
111 }
112
113 static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
114                                              unsigned int address,
115                                              unsigned char val)
116 {
117         const u8 func = 0;
118
119         *arg = ((write & 1) << 31) |
120                ((func & 0x7) << 28) |
121                ((raw & 1) << 27) |
122                (1 << 26) |
123                ((address & 0x1FFFF) << 9) |
124                (1 << 8) |
125                (val & 0xFF);
126 }
127
128 static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
129                                            unsigned int address,
130                                            unsigned char byte)
131 {
132         struct mmc_command io_cmd;
133
134         memset(&io_cmd, 0, sizeof(io_cmd));
135         ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
136         io_cmd.opcode = SD_IO_RW_DIRECT;
137         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
138
139         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
140 }
141
142 static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
143                           u8 *buf, u32 len)
144 {
145         int ret = 0;
146
147         sdio_claim_host(func);
148
149         if (request & HIF_WRITE) {
150                 /* FIXME: looks like ugly workaround for something */
151                 if (addr >= HIF_MBOX_BASE_ADDR &&
152                     addr <= HIF_MBOX_END_ADDR)
153                         addr += (HIF_MBOX_WIDTH - len);
154
155                 /* FIXME: this also looks like ugly workaround */
156                 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
157                         addr += HIF_MBOX0_EXT_WIDTH - len;
158
159                 if (request & HIF_FIXED_ADDRESS)
160                         ret = sdio_writesb(func, addr, buf, len);
161                 else
162                         ret = sdio_memcpy_toio(func, addr, buf, len);
163         } else {
164                 if (request & HIF_FIXED_ADDRESS)
165                         ret = sdio_readsb(func, buf, addr, len);
166                 else
167                         ret = sdio_memcpy_fromio(func, buf, addr, len);
168         }
169
170         sdio_release_host(func);
171
172         ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
173                    request & HIF_WRITE ? "wr" : "rd", addr,
174                    request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
175         ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
176
177         return ret;
178 }
179
180 static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
181 {
182         struct bus_request *bus_req;
183
184         spin_lock_bh(&ar_sdio->lock);
185
186         if (list_empty(&ar_sdio->bus_req_freeq)) {
187                 spin_unlock_bh(&ar_sdio->lock);
188                 return NULL;
189         }
190
191         bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
192                                    struct bus_request, list);
193         list_del(&bus_req->list);
194
195         spin_unlock_bh(&ar_sdio->lock);
196         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
197                    __func__, bus_req);
198
199         return bus_req;
200 }
201
202 static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
203                                      struct bus_request *bus_req)
204 {
205         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
206                    __func__, bus_req);
207
208         spin_lock_bh(&ar_sdio->lock);
209         list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
210         spin_unlock_bh(&ar_sdio->lock);
211 }
212
213 static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
214                                         struct mmc_data *data)
215 {
216         struct scatterlist *sg;
217         int i;
218
219         data->blksz = HIF_MBOX_BLOCK_SIZE;
220         data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
221
222         ath6kl_dbg(ATH6KL_DBG_SCATTER,
223                    "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
224                    (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
225                    data->blksz, data->blocks, scat_req->len,
226                    scat_req->scat_entries);
227
228         data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
229                                                     MMC_DATA_READ;
230
231         /* fill SG entries */
232         sg = scat_req->sgentries;
233         sg_init_table(sg, scat_req->scat_entries);
234
235         /* assemble SG list */
236         for (i = 0; i < scat_req->scat_entries; i++, sg++) {
237                 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
238                            i, scat_req->scat_list[i].buf,
239                            scat_req->scat_list[i].len);
240
241                 sg_set_buf(sg, scat_req->scat_list[i].buf,
242                            scat_req->scat_list[i].len);
243         }
244
245         /* set scatter-gather table for request */
246         data->sg = scat_req->sgentries;
247         data->sg_len = scat_req->scat_entries;
248 }
249
250 static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
251                                struct bus_request *req)
252 {
253         struct mmc_request mmc_req;
254         struct mmc_command cmd;
255         struct mmc_data data;
256         struct hif_scatter_req *scat_req;
257         u8 opcode, rw;
258         int status, len;
259
260         scat_req = req->scat_req;
261
262         if (scat_req->virt_scat) {
263                 len = scat_req->len;
264                 if (scat_req->req & HIF_BLOCK_BASIS)
265                         len = round_down(len, HIF_MBOX_BLOCK_SIZE);
266
267                 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
268                                         scat_req->addr, scat_req->virt_dma_buf,
269                                         len);
270                 goto scat_complete;
271         }
272
273         memset(&mmc_req, 0, sizeof(struct mmc_request));
274         memset(&cmd, 0, sizeof(struct mmc_command));
275         memset(&data, 0, sizeof(struct mmc_data));
276
277         ath6kl_sdio_setup_scat_data(scat_req, &data);
278
279         opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
280                   CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
281
282         rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
283
284         /* Fixup the address so that the last byte will fall on MBOX EOM */
285         if (scat_req->req & HIF_WRITE) {
286                 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
287                         scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
288                 else
289                         /* Uses extended address range */
290                         scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
291         }
292
293         /* set command argument */
294         ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
295                                   CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
296                                   data.blocks);
297
298         cmd.opcode = SD_IO_RW_EXTENDED;
299         cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
300
301         mmc_req.cmd = &cmd;
302         mmc_req.data = &data;
303
304         sdio_claim_host(ar_sdio->func);
305
306         mmc_set_data_timeout(&data, ar_sdio->func->card);
307         /* synchronous call to process request */
308         mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
309
310         sdio_release_host(ar_sdio->func);
311
312         status = cmd.error ? cmd.error : data.error;
313
314 scat_complete:
315         scat_req->status = status;
316
317         if (scat_req->status)
318                 ath6kl_err("Scatter write request failed:%d\n",
319                            scat_req->status);
320
321         if (scat_req->req & HIF_ASYNCHRONOUS)
322                 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
323
324         return status;
325 }
326
327 static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
328                                            int n_scat_entry, int n_scat_req,
329                                            bool virt_scat)
330 {
331         struct hif_scatter_req *s_req;
332         struct bus_request *bus_req;
333         int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
334         u8 *virt_buf;
335
336         scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
337         scat_req_sz = sizeof(*s_req) + scat_list_sz;
338
339         if (!virt_scat)
340                 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
341         else
342                 buf_sz =  2 * L1_CACHE_BYTES +
343                           ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
344
345         for (i = 0; i < n_scat_req; i++) {
346                 /* allocate the scatter request */
347                 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
348                 if (!s_req)
349                         return -ENOMEM;
350
351                 if (virt_scat) {
352                         virt_buf = kzalloc(buf_sz, GFP_KERNEL);
353                         if (!virt_buf) {
354                                 kfree(s_req);
355                                 return -ENOMEM;
356                         }
357
358                         s_req->virt_dma_buf =
359                                 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
360                 } else {
361                         /* allocate sglist */
362                         s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
363
364                         if (!s_req->sgentries) {
365                                 kfree(s_req);
366                                 return -ENOMEM;
367                         }
368                 }
369
370                 /* allocate a bus request for this scatter request */
371                 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
372                 if (!bus_req) {
373                         kfree(s_req->sgentries);
374                         kfree(s_req->virt_dma_buf);
375                         kfree(s_req);
376                         return -ENOMEM;
377                 }
378
379                 /* assign the scatter request to this bus request */
380                 bus_req->scat_req = s_req;
381                 s_req->busrequest = bus_req;
382
383                 s_req->virt_scat = virt_scat;
384
385                 /* add it to the scatter pool */
386                 hif_scatter_req_add(ar_sdio->ar, s_req);
387         }
388
389         return 0;
390 }
391
392 static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
393                                        u32 len, u32 request)
394 {
395         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
396         u8  *tbuf = NULL;
397         int ret;
398         bool bounced = false;
399
400         if (request & HIF_BLOCK_BASIS)
401                 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
402
403         if (buf_needs_bounce(buf)) {
404                 if (!ar_sdio->dma_buffer)
405                         return -ENOMEM;
406                 mutex_lock(&ar_sdio->dma_buffer_mutex);
407                 tbuf = ar_sdio->dma_buffer;
408
409                 if (request & HIF_WRITE)
410                         memcpy(tbuf, buf, len);
411
412                 bounced = true;
413         } else
414                 tbuf = buf;
415
416         ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
417         if ((request & HIF_READ) && bounced)
418                 memcpy(buf, tbuf, len);
419
420         if (bounced)
421                 mutex_unlock(&ar_sdio->dma_buffer_mutex);
422
423         return ret;
424 }
425
426 static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
427                                       struct bus_request *req)
428 {
429         if (req->scat_req)
430                 ath6kl_sdio_scat_rw(ar_sdio, req);
431         else {
432                 void *context;
433                 int status;
434
435                 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
436                                                      req->buffer, req->length,
437                                                      req->request);
438                 context = req->packet;
439                 ath6kl_sdio_free_bus_req(ar_sdio, req);
440                 ath6kl_hif_rw_comp_handler(context, status);
441         }
442 }
443
444 static void ath6kl_sdio_write_async_work(struct work_struct *work)
445 {
446         struct ath6kl_sdio *ar_sdio;
447         struct bus_request *req, *tmp_req;
448
449         ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
450
451         spin_lock_bh(&ar_sdio->wr_async_lock);
452         list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
453                 list_del(&req->list);
454                 spin_unlock_bh(&ar_sdio->wr_async_lock);
455                 __ath6kl_sdio_write_async(ar_sdio, req);
456                 spin_lock_bh(&ar_sdio->wr_async_lock);
457         }
458         spin_unlock_bh(&ar_sdio->wr_async_lock);
459 }
460
461 static void ath6kl_sdio_irq_handler(struct sdio_func *func)
462 {
463         int status;
464         struct ath6kl_sdio *ar_sdio;
465
466         ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
467
468         ar_sdio = sdio_get_drvdata(func);
469         atomic_set(&ar_sdio->irq_handling, 1);
470         /*
471          * Release the host during interrups so we can pick it back up when
472          * we process commands.
473          */
474         sdio_release_host(ar_sdio->func);
475
476         status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
477         sdio_claim_host(ar_sdio->func);
478
479         atomic_set(&ar_sdio->irq_handling, 0);
480         wake_up(&ar_sdio->irq_wq);
481
482         WARN_ON(status && status != -ECANCELED);
483 }
484
485 static int ath6kl_sdio_power_on(struct ath6kl *ar)
486 {
487         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
488         struct sdio_func *func = ar_sdio->func;
489         int ret = 0;
490
491         if (!ar_sdio->is_disabled)
492                 return 0;
493
494         ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
495
496         sdio_claim_host(func);
497
498         ret = sdio_enable_func(func);
499         if (ret) {
500                 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
501                 sdio_release_host(func);
502                 return ret;
503         }
504
505         sdio_release_host(func);
506
507         /*
508          * Wait for hardware to initialise. It should take a lot less than
509          * 10 ms but let's be conservative here.
510          */
511         msleep(10);
512
513         ar_sdio->is_disabled = false;
514
515         return ret;
516 }
517
518 static int ath6kl_sdio_power_off(struct ath6kl *ar)
519 {
520         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
521         int ret;
522
523         if (ar_sdio->is_disabled)
524                 return 0;
525
526         ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
527
528         /* Disable the card */
529         sdio_claim_host(ar_sdio->func);
530         ret = sdio_disable_func(ar_sdio->func);
531         sdio_release_host(ar_sdio->func);
532
533         if (ret)
534                 return ret;
535
536         ar_sdio->is_disabled = true;
537
538         return ret;
539 }
540
541 static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
542                                    u32 length, u32 request,
543                                    struct htc_packet *packet)
544 {
545         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
546         struct bus_request *bus_req;
547
548         bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
549
550         if (!bus_req)
551                 return -ENOMEM;
552
553         bus_req->address = address;
554         bus_req->buffer = buffer;
555         bus_req->length = length;
556         bus_req->request = request;
557         bus_req->packet = packet;
558
559         spin_lock_bh(&ar_sdio->wr_async_lock);
560         list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
561         spin_unlock_bh(&ar_sdio->wr_async_lock);
562         queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
563
564         return 0;
565 }
566
567 static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
568 {
569         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
570         int ret;
571
572         sdio_claim_host(ar_sdio->func);
573
574         /* Register the isr */
575         ret =  sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
576         if (ret)
577                 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
578
579         sdio_release_host(ar_sdio->func);
580 }
581
582 static bool ath6kl_sdio_is_on_irq(struct ath6kl *ar)
583 {
584         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
585
586         return !atomic_read(&ar_sdio->irq_handling);
587 }
588
589 static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
590 {
591         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
592         int ret;
593
594         sdio_claim_host(ar_sdio->func);
595
596         if (atomic_read(&ar_sdio->irq_handling)) {
597                 sdio_release_host(ar_sdio->func);
598
599                 ret = wait_event_interruptible(ar_sdio->irq_wq,
600                                                ath6kl_sdio_is_on_irq(ar));
601                 if (ret)
602                         return;
603
604                 sdio_claim_host(ar_sdio->func);
605         }
606
607         ret = sdio_release_irq(ar_sdio->func);
608         if (ret)
609                 ath6kl_err("Failed to release sdio irq: %d\n", ret);
610
611         sdio_release_host(ar_sdio->func);
612 }
613
614 static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
615 {
616         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
617         struct hif_scatter_req *node = NULL;
618
619         spin_lock_bh(&ar_sdio->scat_lock);
620
621         if (!list_empty(&ar_sdio->scat_req)) {
622                 node = list_first_entry(&ar_sdio->scat_req,
623                                         struct hif_scatter_req, list);
624                 list_del(&node->list);
625
626                 node->scat_q_depth = get_queue_depth(&ar_sdio->scat_req);
627         }
628
629         spin_unlock_bh(&ar_sdio->scat_lock);
630
631         return node;
632 }
633
634 static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
635                                         struct hif_scatter_req *s_req)
636 {
637         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
638
639         spin_lock_bh(&ar_sdio->scat_lock);
640
641         list_add_tail(&s_req->list, &ar_sdio->scat_req);
642
643         spin_unlock_bh(&ar_sdio->scat_lock);
644
645 }
646
647 /* scatter gather read write request */
648 static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
649                                         struct hif_scatter_req *scat_req)
650 {
651         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
652         u32 request = scat_req->req;
653         int status = 0;
654
655         if (!scat_req->len)
656                 return -EINVAL;
657
658         ath6kl_dbg(ATH6KL_DBG_SCATTER,
659                 "hif-scatter: total len: %d scatter entries: %d\n",
660                 scat_req->len, scat_req->scat_entries);
661
662         if (request & HIF_SYNCHRONOUS)
663                 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
664         else {
665                 spin_lock_bh(&ar_sdio->wr_async_lock);
666                 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
667                 spin_unlock_bh(&ar_sdio->wr_async_lock);
668                 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
669         }
670
671         return status;
672 }
673
674 /* clean up scatter support */
675 static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
676 {
677         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
678         struct hif_scatter_req *s_req, *tmp_req;
679
680         /* empty the free list */
681         spin_lock_bh(&ar_sdio->scat_lock);
682         list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
683                 list_del(&s_req->list);
684                 spin_unlock_bh(&ar_sdio->scat_lock);
685
686                 /*
687                  * FIXME: should we also call completion handler with
688                  * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
689                  * that the packet is properly freed?
690                  */
691                 if (s_req->busrequest)
692                         ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
693                 kfree(s_req->virt_dma_buf);
694                 kfree(s_req->sgentries);
695                 kfree(s_req);
696
697                 spin_lock_bh(&ar_sdio->scat_lock);
698         }
699         spin_unlock_bh(&ar_sdio->scat_lock);
700 }
701
702 /* setup of HIF scatter resources */
703 static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
704 {
705         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
706         struct htc_target *target = ar->htc_target;
707         int ret;
708         bool virt_scat = false;
709
710         if (ar_sdio->scatter_enabled)
711                 return 0;
712
713         ar_sdio->scatter_enabled = true;
714
715         /* check if host supports scatter and it meets our requirements */
716         if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
717                 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
718                            ar_sdio->func->card->host->max_segs,
719                            MAX_SCATTER_ENTRIES_PER_REQ);
720                 virt_scat = true;
721         }
722
723         if (!virt_scat) {
724                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
725                                 MAX_SCATTER_ENTRIES_PER_REQ,
726                                 MAX_SCATTER_REQUESTS, virt_scat);
727
728                 if (!ret) {
729                         ath6kl_dbg(ATH6KL_DBG_BOOT,
730                                    "hif-scatter enabled requests %d entries %d\n",
731                                    MAX_SCATTER_REQUESTS,
732                                    MAX_SCATTER_ENTRIES_PER_REQ);
733
734                         target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
735                         target->max_xfer_szper_scatreq =
736                                                 MAX_SCATTER_REQ_TRANSFER_SIZE;
737                 } else {
738                         ath6kl_sdio_cleanup_scatter(ar);
739                         ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
740                 }
741         }
742
743         if (virt_scat || ret) {
744                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
745                                 ATH6KL_SCATTER_ENTRIES_PER_REQ,
746                                 ATH6KL_SCATTER_REQS, virt_scat);
747
748                 if (ret) {
749                         ath6kl_err("failed to alloc virtual scatter resources !\n");
750                         ath6kl_sdio_cleanup_scatter(ar);
751                         return ret;
752                 }
753
754                 ath6kl_dbg(ATH6KL_DBG_BOOT,
755                            "virtual scatter enabled requests %d entries %d\n",
756                            ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
757
758                 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
759                 target->max_xfer_szper_scatreq =
760                                         ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
761         }
762
763         return 0;
764 }
765
766 static int ath6kl_sdio_config(struct ath6kl *ar)
767 {
768         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
769         struct sdio_func *func = ar_sdio->func;
770         int ret;
771
772         sdio_claim_host(func);
773
774         if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
775             MANUFACTURER_ID_AR6003_BASE) {
776                 /* enable 4-bit ASYNC interrupt on AR6003 or later */
777                 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
778                                                 CCCR_SDIO_IRQ_MODE_REG,
779                                                 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
780                 if (ret) {
781                         ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
782                                    ret);
783                         goto out;
784                 }
785
786                 ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
787         }
788
789         /* give us some time to enable, in ms */
790         func->enable_timeout = 100;
791
792         ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
793         if (ret) {
794                 ath6kl_err("Set sdio block size %d failed: %d)\n",
795                            HIF_MBOX_BLOCK_SIZE, ret);
796                 goto out;
797         }
798
799 out:
800         sdio_release_host(func);
801
802         return ret;
803 }
804
805 static int ath6kl_set_sdio_pm_caps(struct ath6kl *ar)
806 {
807         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
808         struct sdio_func *func = ar_sdio->func;
809         mmc_pm_flag_t flags;
810         int ret;
811
812         flags = sdio_get_host_pm_caps(func);
813
814         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
815
816         if (!(flags & MMC_PM_WAKE_SDIO_IRQ) ||
817             !(flags & MMC_PM_KEEP_POWER))
818                 return -EINVAL;
819
820         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
821         if (ret) {
822                 ath6kl_err("set sdio keep pwr flag failed: %d\n", ret);
823                 return ret;
824         }
825
826         /* sdio irq wakes up host */
827         ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
828         if (ret)
829                 ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
830
831         return ret;
832 }
833
834 static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
835 {
836         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
837         struct sdio_func *func = ar_sdio->func;
838         mmc_pm_flag_t flags;
839         bool try_deepsleep = false;
840         int ret;
841
842         if (ar->state == ATH6KL_STATE_SCHED_SCAN) {
843                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sched scan is in progress\n");
844
845                 ret = ath6kl_set_sdio_pm_caps(ar);
846                 if (ret)
847                         goto cut_pwr;
848
849                 ret =  ath6kl_cfg80211_suspend(ar,
850                                                ATH6KL_CFG_SUSPEND_SCHED_SCAN,
851                                                NULL);
852                 if (ret)
853                         goto cut_pwr;
854
855                 return 0;
856         }
857
858         if (ar->suspend_mode == WLAN_POWER_STATE_WOW ||
859             (!ar->suspend_mode && wow)) {
860
861                 ret = ath6kl_set_sdio_pm_caps(ar);
862                 if (ret)
863                         goto cut_pwr;
864
865                 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
866                 if (ret && ret != -ENOTCONN)
867                         ath6kl_err("wow suspend failed: %d\n", ret);
868
869                 if (ret && (!ar->wow_suspend_mode ||
870                     ar->wow_suspend_mode == WLAN_POWER_STATE_DEEP_SLEEP))
871                                 try_deepsleep = true;
872                 else if (ret &&
873                          ar->wow_suspend_mode == WLAN_POWER_STATE_CUT_PWR)
874                                 goto cut_pwr;
875                 if (!ret)
876                         return 0;
877         }
878
879         if (ar->suspend_mode == WLAN_POWER_STATE_DEEP_SLEEP ||
880             !ar->suspend_mode || try_deepsleep) {
881
882                 flags = sdio_get_host_pm_caps(func);
883                 if (!(flags & MMC_PM_KEEP_POWER))
884                         goto cut_pwr;
885
886                 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
887                 if (ret)
888                         goto cut_pwr;
889
890                 /*
891                  * Workaround to support Deep Sleep with MSM, set the host pm
892                  * flag as MMC_PM_WAKE_SDIO_IRQ to allow SDCC deiver to disable
893                  * the sdc2_clock and internally allows MSM to enter
894                  * TCXO shutdown properly.
895                  */
896                 if ((flags & MMC_PM_WAKE_SDIO_IRQ)) {
897                         ret = sdio_set_host_pm_flags(func,
898                                                 MMC_PM_WAKE_SDIO_IRQ);
899                         if (ret)
900                                 goto cut_pwr;
901                 }
902
903                 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP,
904                                               NULL);
905                 if (ret)
906                         goto cut_pwr;
907
908                 return 0;
909         }
910
911 cut_pwr:
912         return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER, NULL);
913 }
914
915 static int ath6kl_sdio_resume(struct ath6kl *ar)
916 {
917         switch (ar->state) {
918         case ATH6KL_STATE_OFF:
919         case ATH6KL_STATE_CUTPOWER:
920                 ath6kl_dbg(ATH6KL_DBG_SUSPEND,
921                            "sdio resume configuring sdio\n");
922
923                 /* need to set sdio settings after power is cut from sdio */
924                 ath6kl_sdio_config(ar);
925                 break;
926
927         case ATH6KL_STATE_ON:
928                 break;
929
930         case ATH6KL_STATE_DEEPSLEEP:
931                 break;
932
933         case ATH6KL_STATE_WOW:
934                 break;
935         case ATH6KL_STATE_SCHED_SCAN:
936                 break;
937         }
938
939         ath6kl_cfg80211_resume(ar);
940
941         return 0;
942 }
943
944 /* set the window address register (using 4-byte register access ). */
945 static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
946 {
947         int status;
948         u8 addr_val[4];
949         s32 i;
950
951         /*
952          * Write bytes 1,2,3 of the register to set the upper address bytes,
953          * the LSB is written last to initiate the access cycle
954          */
955
956         for (i = 1; i <= 3; i++) {
957                 /*
958                  * Fill the buffer with the address byte value we want to
959                  * hit 4 times.
960                  */
961                 memset(addr_val, ((u8 *)&addr)[i], 4);
962
963                 /*
964                  * Hit each byte of the register address with a 4-byte
965                  * write operation to the same address, this is a harmless
966                  * operation.
967                  */
968                 status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
969                                              4, HIF_WR_SYNC_BYTE_FIX);
970                 if (status)
971                         break;
972         }
973
974         if (status) {
975                 ath6kl_err("%s: failed to write initial bytes of 0x%x "
976                            "to window reg: 0x%X\n", __func__,
977                            addr, reg_addr);
978                 return status;
979         }
980
981         /*
982          * Write the address register again, this time write the whole
983          * 4-byte value. The effect here is that the LSB write causes the
984          * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
985          * effect since we are writing the same values again
986          */
987         status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
988                                      4, HIF_WR_SYNC_BYTE_INC);
989
990         if (status) {
991                 ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
992                            __func__, addr, reg_addr);
993                 return status;
994         }
995
996         return 0;
997 }
998
999 static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
1000 {
1001         int status;
1002
1003         /* set window register to start read cycle */
1004         status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
1005                                         address);
1006
1007         if (status)
1008                 return status;
1009
1010         /* read the data */
1011         status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
1012                                 (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
1013         if (status) {
1014                 ath6kl_err("%s: failed to read from window data addr\n",
1015                         __func__);
1016                 return status;
1017         }
1018
1019         return status;
1020 }
1021
1022 static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
1023                                     __le32 data)
1024 {
1025         int status;
1026         u32 val = (__force u32) data;
1027
1028         /* set write data */
1029         status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
1030                                 (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
1031         if (status) {
1032                 ath6kl_err("%s: failed to write 0x%x to window data addr\n",
1033                            __func__, data);
1034                 return status;
1035         }
1036
1037         /* set window register, which starts the write cycle */
1038         return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
1039                                       address);
1040 }
1041
1042 static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
1043 {
1044         u32 addr;
1045         unsigned long timeout;
1046         int ret;
1047
1048         ar->bmi.cmd_credits = 0;
1049
1050         /* Read the counter register to get the command credits */
1051         addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
1052
1053         timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
1054         while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
1055
1056                 /*
1057                  * Hit the credit counter with a 4-byte access, the first byte
1058                  * read will hit the counter and cause a decrement, while the
1059                  * remaining 3 bytes has no effect. The rationale behind this
1060                  * is to make all HIF accesses 4-byte aligned.
1061                  */
1062                 ret = ath6kl_sdio_read_write_sync(ar, addr,
1063                                          (u8 *)&ar->bmi.cmd_credits, 4,
1064                                          HIF_RD_SYNC_BYTE_INC);
1065                 if (ret) {
1066                         ath6kl_err("Unable to decrement the command credit "
1067                                                 "count register: %d\n", ret);
1068                         return ret;
1069                 }
1070
1071                 /* The counter is only 8 bits.
1072                  * Ignore anything in the upper 3 bytes
1073                  */
1074                 ar->bmi.cmd_credits &= 0xFF;
1075         }
1076
1077         if (!ar->bmi.cmd_credits) {
1078                 ath6kl_err("bmi communication timeout\n");
1079                 return -ETIMEDOUT;
1080         }
1081
1082         return 0;
1083 }
1084
1085 static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
1086 {
1087         unsigned long timeout;
1088         u32 rx_word = 0;
1089         int ret = 0;
1090
1091         timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
1092         while ((time_before(jiffies, timeout)) && !rx_word) {
1093                 ret = ath6kl_sdio_read_write_sync(ar,
1094                                         RX_LOOKAHEAD_VALID_ADDRESS,
1095                                         (u8 *)&rx_word, sizeof(rx_word),
1096                                         HIF_RD_SYNC_BYTE_INC);
1097                 if (ret) {
1098                         ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
1099                         return ret;
1100                 }
1101
1102                  /* all we really want is one bit */
1103                 rx_word &= (1 << ENDPOINT1);
1104         }
1105
1106         if (!rx_word) {
1107                 ath6kl_err("bmi_recv_buf FIFO empty\n");
1108                 return -EINVAL;
1109         }
1110
1111         return ret;
1112 }
1113
1114 static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
1115 {
1116         int ret;
1117         u32 addr;
1118
1119         ret = ath6kl_sdio_bmi_credits(ar);
1120         if (ret)
1121                 return ret;
1122
1123         addr = ar->mbox_info.htc_addr;
1124
1125         ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1126                                           HIF_WR_SYNC_BYTE_INC);
1127         if (ret)
1128                 ath6kl_err("unable to send the bmi data to the device\n");
1129
1130         return ret;
1131 }
1132
1133 static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
1134 {
1135         int ret;
1136         u32 addr;
1137
1138         /*
1139          * During normal bootup, small reads may be required.
1140          * Rather than issue an HIF Read and then wait as the Target
1141          * adds successive bytes to the FIFO, we wait here until
1142          * we know that response data is available.
1143          *
1144          * This allows us to cleanly timeout on an unexpected
1145          * Target failure rather than risk problems at the HIF level.
1146          * In particular, this avoids SDIO timeouts and possibly garbage
1147          * data on some host controllers.  And on an interconnect
1148          * such as Compact Flash (as well as some SDIO masters) which
1149          * does not provide any indication on data timeout, it avoids
1150          * a potential hang or garbage response.
1151          *
1152          * Synchronization is more difficult for reads larger than the
1153          * size of the MBOX FIFO (128B), because the Target is unable
1154          * to push the 129th byte of data until AFTER the Host posts an
1155          * HIF Read and removes some FIFO data.  So for large reads the
1156          * Host proceeds to post an HIF Read BEFORE all the data is
1157          * actually available to read.  Fortunately, large BMI reads do
1158          * not occur in practice -- they're supported for debug/development.
1159          *
1160          * So Host/Target BMI synchronization is divided into these cases:
1161          *  CASE 1: length < 4
1162          *        Should not happen
1163          *
1164          *  CASE 2: 4 <= length <= 128
1165          *        Wait for first 4 bytes to be in FIFO
1166          *        If CONSERVATIVE_BMI_READ is enabled, also wait for
1167          *        a BMI command credit, which indicates that the ENTIRE
1168          *        response is available in the the FIFO
1169          *
1170          *  CASE 3: length > 128
1171          *        Wait for the first 4 bytes to be in FIFO
1172          *
1173          * For most uses, a small timeout should be sufficient and we will
1174          * usually see a response quickly; but there may be some unusual
1175          * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1176          * For now, we use an unbounded busy loop while waiting for
1177          * BMI_EXECUTE.
1178          *
1179          * If BMI_EXECUTE ever needs to support longer-latency execution,
1180          * especially in production, this code needs to be enhanced to sleep
1181          * and yield.  Also note that BMI_COMMUNICATION_TIMEOUT is currently
1182          * a function of Host processor speed.
1183          */
1184         if (len >= 4) { /* NB: Currently, always true */
1185                 ret = ath6kl_bmi_get_rx_lkahd(ar);
1186                 if (ret)
1187                         return ret;
1188         }
1189
1190         addr = ar->mbox_info.htc_addr;
1191         ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1192                                   HIF_RD_SYNC_BYTE_INC);
1193         if (ret) {
1194                 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1195                            ret);
1196                 return ret;
1197         }
1198
1199         return 0;
1200 }
1201
1202 static void ath6kl_sdio_stop(struct ath6kl *ar)
1203 {
1204         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
1205         struct bus_request *req, *tmp_req;
1206         void *context;
1207
1208         /* FIXME: make sure that wq is not queued again */
1209
1210         cancel_work_sync(&ar_sdio->wr_async_work);
1211
1212         spin_lock_bh(&ar_sdio->wr_async_lock);
1213
1214         list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1215                 list_del(&req->list);
1216
1217                 if (req->scat_req) {
1218                         /* this is a scatter gather request */
1219                         req->scat_req->status = -ECANCELED;
1220                         req->scat_req->complete(ar_sdio->ar->htc_target,
1221                                                 req->scat_req);
1222                 } else {
1223                         context = req->packet;
1224                         ath6kl_sdio_free_bus_req(ar_sdio, req);
1225                         ath6kl_hif_rw_comp_handler(context, -ECANCELED);
1226                 }
1227         }
1228
1229         spin_unlock_bh(&ar_sdio->wr_async_lock);
1230
1231         WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
1232 }
1233
1234 static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
1235         .read_write_sync = ath6kl_sdio_read_write_sync,
1236         .write_async = ath6kl_sdio_write_async,
1237         .irq_enable = ath6kl_sdio_irq_enable,
1238         .irq_disable = ath6kl_sdio_irq_disable,
1239         .scatter_req_get = ath6kl_sdio_scatter_req_get,
1240         .scatter_req_add = ath6kl_sdio_scatter_req_add,
1241         .enable_scatter = ath6kl_sdio_enable_scatter,
1242         .scat_req_rw = ath6kl_sdio_async_rw_scatter,
1243         .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
1244         .suspend = ath6kl_sdio_suspend,
1245         .resume = ath6kl_sdio_resume,
1246         .diag_read32 = ath6kl_sdio_diag_read32,
1247         .diag_write32 = ath6kl_sdio_diag_write32,
1248         .bmi_read = ath6kl_sdio_bmi_read,
1249         .bmi_write = ath6kl_sdio_bmi_write,
1250         .power_on = ath6kl_sdio_power_on,
1251         .power_off = ath6kl_sdio_power_off,
1252         .stop = ath6kl_sdio_stop,
1253 };
1254
1255 #ifdef CONFIG_PM_SLEEP
1256
1257 /*
1258  * Empty handlers so that mmc subsystem doesn't remove us entirely during
1259  * suspend. We instead follow cfg80211 suspend/resume handlers.
1260  */
1261 static int ath6kl_sdio_pm_suspend(struct device *device)
1262 {
1263         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
1264
1265         return 0;
1266 }
1267
1268 static int ath6kl_sdio_pm_resume(struct device *device)
1269 {
1270         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
1271
1272         return 0;
1273 }
1274
1275 static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
1276                          ath6kl_sdio_pm_resume);
1277
1278 #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
1279
1280 #else
1281
1282 #define ATH6KL_SDIO_PM_OPS NULL
1283
1284 #endif /* CONFIG_PM_SLEEP */
1285
1286 static int ath6kl_sdio_probe(struct sdio_func *func,
1287                              const struct sdio_device_id *id)
1288 {
1289         int ret;
1290         struct ath6kl_sdio *ar_sdio;
1291         struct ath6kl *ar;
1292         int count;
1293
1294         ath6kl_dbg(ATH6KL_DBG_BOOT,
1295                    "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
1296                    func->num, func->vendor, func->device,
1297                    func->max_blksize, func->cur_blksize);
1298
1299         ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
1300         if (!ar_sdio)
1301                 return -ENOMEM;
1302
1303         ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
1304         if (!ar_sdio->dma_buffer) {
1305                 ret = -ENOMEM;
1306                 goto err_hif;
1307         }
1308
1309         ar_sdio->func = func;
1310         sdio_set_drvdata(func, ar_sdio);
1311
1312         ar_sdio->id = id;
1313         ar_sdio->is_disabled = true;
1314
1315         spin_lock_init(&ar_sdio->lock);
1316         spin_lock_init(&ar_sdio->scat_lock);
1317         spin_lock_init(&ar_sdio->wr_async_lock);
1318         mutex_init(&ar_sdio->dma_buffer_mutex);
1319
1320         INIT_LIST_HEAD(&ar_sdio->scat_req);
1321         INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
1322         INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
1323
1324         INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
1325
1326         init_waitqueue_head(&ar_sdio->irq_wq);
1327
1328         for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
1329                 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
1330
1331         ar = ath6kl_core_create(&ar_sdio->func->dev);
1332         if (!ar) {
1333                 ath6kl_err("Failed to alloc ath6kl core\n");
1334                 ret = -ENOMEM;
1335                 goto err_dma;
1336         }
1337
1338         ar_sdio->ar = ar;
1339         ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
1340         ar->hif_priv = ar_sdio;
1341         ar->hif_ops = &ath6kl_sdio_ops;
1342         ar->bmi.max_data_size = 256;
1343
1344         ath6kl_sdio_set_mbox_info(ar);
1345
1346         ret = ath6kl_sdio_config(ar);
1347         if (ret) {
1348                 ath6kl_err("Failed to config sdio: %d\n", ret);
1349                 goto err_core_alloc;
1350         }
1351
1352         ret = ath6kl_core_init(ar);
1353         if (ret) {
1354                 ath6kl_err("Failed to init ath6kl core\n");
1355                 goto err_core_alloc;
1356         }
1357
1358         return ret;
1359
1360 err_core_alloc:
1361         ath6kl_core_destroy(ar_sdio->ar);
1362 err_dma:
1363         kfree(ar_sdio->dma_buffer);
1364 err_hif:
1365         kfree(ar_sdio);
1366
1367         return ret;
1368 }
1369
1370 static void ath6kl_sdio_remove(struct sdio_func *func)
1371 {
1372         struct ath6kl_sdio *ar_sdio;
1373
1374         ath6kl_dbg(ATH6KL_DBG_BOOT,
1375                    "sdio removed func %d vendor 0x%x device 0x%x\n",
1376                    func->num, func->vendor, func->device);
1377
1378         ar_sdio = sdio_get_drvdata(func);
1379
1380         ath6kl_stop_txrx(ar_sdio->ar);
1381         cancel_work_sync(&ar_sdio->wr_async_work);
1382
1383         ath6kl_core_cleanup(ar_sdio->ar);
1384         ath6kl_core_destroy(ar_sdio->ar);
1385
1386         kfree(ar_sdio->dma_buffer);
1387         kfree(ar_sdio);
1388 }
1389
1390 static const struct sdio_device_id ath6kl_sdio_devices[] = {
1391         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
1392         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
1393         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
1394         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
1395         {},
1396 };
1397
1398 MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
1399
1400 static struct sdio_driver ath6kl_sdio_driver = {
1401         .name = "ath6kl_sdio",
1402         .id_table = ath6kl_sdio_devices,
1403         .probe = ath6kl_sdio_probe,
1404         .remove = ath6kl_sdio_remove,
1405         .drv.pm = ATH6KL_SDIO_PM_OPS,
1406 };
1407
1408 static int __init ath6kl_sdio_init(void)
1409 {
1410         int ret;
1411
1412         ret = sdio_register_driver(&ath6kl_sdio_driver);
1413         if (ret)
1414                 ath6kl_err("sdio driver registration failed: %d\n", ret);
1415
1416         return ret;
1417 }
1418
1419 static void __exit ath6kl_sdio_exit(void)
1420 {
1421         sdio_unregister_driver(&ath6kl_sdio_driver);
1422 }
1423
1424 module_init(ath6kl_sdio_init);
1425 module_exit(ath6kl_sdio_exit);
1426
1427 MODULE_AUTHOR("Atheros Communications, Inc.");
1428 MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
1429 MODULE_LICENSE("Dual BSD/GPL");
1430
1431 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_OTP_FILE);
1432 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_FIRMWARE_FILE);
1433 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_PATCH_FILE);
1434 MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
1435 MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
1436 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_OTP_FILE);
1437 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_FIRMWARE_FILE);
1438 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_PATCH_FILE);
1439 MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
1440 MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);
1441 MODULE_FIRMWARE(AR6004_HW_1_0_FW_DIR "/" AR6004_HW_1_0_FIRMWARE_FILE);
1442 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
1443 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
1444 MODULE_FIRMWARE(AR6004_HW_1_1_FW_DIR "/" AR6004_HW_1_1_FIRMWARE_FILE);
1445 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
1446 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);