2 * Copyright (c) 2004-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/module.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/mmc.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/sdio_func.h>
22 #include <linux/mmc/sdio_ids.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/sd.h>
32 struct sdio_func *func;
37 struct list_head bus_req_freeq;
39 /* available bus requests */
40 struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
45 /* scatter request list head */
46 struct list_head scat_req;
52 atomic_t irq_handling;
53 const struct sdio_device_id *id;
54 struct work_struct wr_async_work;
55 struct list_head wr_asyncq;
56 spinlock_t wr_async_lock;
59 #define CMD53_ARG_READ 0
60 #define CMD53_ARG_WRITE 1
61 #define CMD53_ARG_BLOCK_BASIS 1
62 #define CMD53_ARG_FIXED_ADDRESS 0
63 #define CMD53_ARG_INCR_ADDRESS 1
65 static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
71 * Macro to check if DMA buffer is WORD-aligned and DMA-able.
72 * Most host controllers assume the buffer is DMA'able and will
73 * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
74 * check fails on stack memory.
76 static inline bool buf_needs_bounce(u8 *buf)
78 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
81 static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
83 struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
85 /* EP1 has an extended range */
86 mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
87 mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
88 mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
89 mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
90 mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
91 mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
94 static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
95 u8 mode, u8 opcode, u32 addr,
98 *arg = (((rw & 1) << 31) |
99 ((func & 0x7) << 28) |
101 ((opcode & 1) << 26) |
102 ((addr & 0x1FFFF) << 9) |
106 static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
107 unsigned int address,
112 *arg = ((write & 1) << 31) |
113 ((func & 0x7) << 28) |
116 ((address & 0x1FFFF) << 9) |
121 static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
122 unsigned int address,
125 struct mmc_command io_cmd;
127 memset(&io_cmd, 0, sizeof(io_cmd));
128 ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
129 io_cmd.opcode = SD_IO_RW_DIRECT;
130 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
132 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
135 static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
140 sdio_claim_host(func);
142 if (request & HIF_WRITE) {
143 /* FIXME: looks like ugly workaround for something */
144 if (addr >= HIF_MBOX_BASE_ADDR &&
145 addr <= HIF_MBOX_END_ADDR)
146 addr += (HIF_MBOX_WIDTH - len);
148 /* FIXME: this also looks like ugly workaround */
149 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
150 addr += HIF_MBOX0_EXT_WIDTH - len;
152 if (request & HIF_FIXED_ADDRESS)
153 ret = sdio_writesb(func, addr, buf, len);
155 ret = sdio_memcpy_toio(func, addr, buf, len);
157 if (request & HIF_FIXED_ADDRESS)
158 ret = sdio_readsb(func, buf, addr, len);
160 ret = sdio_memcpy_fromio(func, buf, addr, len);
163 sdio_release_host(func);
165 ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
166 request & HIF_WRITE ? "wr" : "rd", addr,
167 request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
168 ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
173 static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
175 struct bus_request *bus_req;
177 spin_lock_bh(&ar_sdio->lock);
179 if (list_empty(&ar_sdio->bus_req_freeq)) {
180 spin_unlock_bh(&ar_sdio->lock);
184 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
185 struct bus_request, list);
186 list_del(&bus_req->list);
188 spin_unlock_bh(&ar_sdio->lock);
189 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
195 static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
196 struct bus_request *bus_req)
198 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
201 spin_lock_bh(&ar_sdio->lock);
202 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
203 spin_unlock_bh(&ar_sdio->lock);
206 static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
207 struct mmc_data *data)
209 struct scatterlist *sg;
212 data->blksz = HIF_MBOX_BLOCK_SIZE;
213 data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
215 ath6kl_dbg(ATH6KL_DBG_SCATTER,
216 "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
217 (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
218 data->blksz, data->blocks, scat_req->len,
219 scat_req->scat_entries);
221 data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
224 /* fill SG entries */
225 sg = scat_req->sgentries;
226 sg_init_table(sg, scat_req->scat_entries);
228 /* assemble SG list */
229 for (i = 0; i < scat_req->scat_entries; i++, sg++) {
230 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
231 i, scat_req->scat_list[i].buf,
232 scat_req->scat_list[i].len);
234 sg_set_buf(sg, scat_req->scat_list[i].buf,
235 scat_req->scat_list[i].len);
238 /* set scatter-gather table for request */
239 data->sg = scat_req->sgentries;
240 data->sg_len = scat_req->scat_entries;
243 static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
244 struct bus_request *req)
246 struct mmc_request mmc_req;
247 struct mmc_command cmd;
248 struct mmc_data data;
249 struct hif_scatter_req *scat_req;
253 scat_req = req->scat_req;
255 if (scat_req->virt_scat) {
257 if (scat_req->req & HIF_BLOCK_BASIS)
258 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
260 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
261 scat_req->addr, scat_req->virt_dma_buf,
266 memset(&mmc_req, 0, sizeof(struct mmc_request));
267 memset(&cmd, 0, sizeof(struct mmc_command));
268 memset(&data, 0, sizeof(struct mmc_data));
270 ath6kl_sdio_setup_scat_data(scat_req, &data);
272 opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
273 CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
275 rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
277 /* Fixup the address so that the last byte will fall on MBOX EOM */
278 if (scat_req->req & HIF_WRITE) {
279 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
280 scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
282 /* Uses extended address range */
283 scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
286 /* set command argument */
287 ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
288 CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
291 cmd.opcode = SD_IO_RW_EXTENDED;
292 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
295 mmc_req.data = &data;
297 sdio_claim_host(ar_sdio->func);
299 mmc_set_data_timeout(&data, ar_sdio->func->card);
300 /* synchronous call to process request */
301 mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
303 sdio_release_host(ar_sdio->func);
305 status = cmd.error ? cmd.error : data.error;
308 scat_req->status = status;
310 if (scat_req->status)
311 ath6kl_err("Scatter write request failed:%d\n",
314 if (scat_req->req & HIF_ASYNCHRONOUS)
315 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
320 static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
321 int n_scat_entry, int n_scat_req,
324 struct hif_scatter_req *s_req;
325 struct bus_request *bus_req;
326 int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
329 scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
330 scat_req_sz = sizeof(*s_req) + scat_list_sz;
333 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
335 buf_sz = 2 * L1_CACHE_BYTES +
336 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
338 for (i = 0; i < n_scat_req; i++) {
339 /* allocate the scatter request */
340 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
345 virt_buf = kzalloc(buf_sz, GFP_KERNEL);
351 s_req->virt_dma_buf =
352 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
354 /* allocate sglist */
355 s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
357 if (!s_req->sgentries) {
363 /* allocate a bus request for this scatter request */
364 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
366 kfree(s_req->sgentries);
367 kfree(s_req->virt_dma_buf);
372 /* assign the scatter request to this bus request */
373 bus_req->scat_req = s_req;
374 s_req->busrequest = bus_req;
376 s_req->virt_scat = virt_scat;
378 /* add it to the scatter pool */
379 hif_scatter_req_add(ar_sdio->ar, s_req);
385 static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
386 u32 len, u32 request)
388 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
391 bool bounced = false;
393 if (request & HIF_BLOCK_BASIS)
394 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
396 if (buf_needs_bounce(buf)) {
397 if (!ar_sdio->dma_buffer)
399 tbuf = ar_sdio->dma_buffer;
400 memcpy(tbuf, buf, len);
405 ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
406 if ((request & HIF_READ) && bounced)
407 memcpy(buf, tbuf, len);
412 static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
413 struct bus_request *req)
416 ath6kl_sdio_scat_rw(ar_sdio, req);
421 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
422 req->buffer, req->length,
424 context = req->packet;
425 ath6kl_sdio_free_bus_req(ar_sdio, req);
426 ath6kl_hif_rw_comp_handler(context, status);
430 static void ath6kl_sdio_write_async_work(struct work_struct *work)
432 struct ath6kl_sdio *ar_sdio;
433 struct bus_request *req, *tmp_req;
435 ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
437 spin_lock_bh(&ar_sdio->wr_async_lock);
438 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
439 list_del(&req->list);
440 spin_unlock_bh(&ar_sdio->wr_async_lock);
441 __ath6kl_sdio_write_async(ar_sdio, req);
442 spin_lock_bh(&ar_sdio->wr_async_lock);
444 spin_unlock_bh(&ar_sdio->wr_async_lock);
447 static void ath6kl_sdio_irq_handler(struct sdio_func *func)
450 struct ath6kl_sdio *ar_sdio;
452 ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
454 ar_sdio = sdio_get_drvdata(func);
455 atomic_set(&ar_sdio->irq_handling, 1);
458 * Release the host during interrups so we can pick it back up when
459 * we process commands.
461 sdio_release_host(ar_sdio->func);
463 status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
464 sdio_claim_host(ar_sdio->func);
465 atomic_set(&ar_sdio->irq_handling, 0);
466 WARN_ON(status && status != -ECANCELED);
469 static int ath6kl_sdio_power_on(struct ath6kl *ar)
471 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
472 struct sdio_func *func = ar_sdio->func;
475 if (!ar_sdio->is_disabled)
478 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
480 sdio_claim_host(func);
482 ret = sdio_enable_func(func);
484 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
485 sdio_release_host(func);
489 sdio_release_host(func);
492 * Wait for hardware to initialise. It should take a lot less than
493 * 10 ms but let's be conservative here.
497 ar_sdio->is_disabled = false;
502 static int ath6kl_sdio_power_off(struct ath6kl *ar)
504 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
507 if (ar_sdio->is_disabled)
510 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
512 /* Disable the card */
513 sdio_claim_host(ar_sdio->func);
514 ret = sdio_disable_func(ar_sdio->func);
515 sdio_release_host(ar_sdio->func);
520 ar_sdio->is_disabled = true;
525 static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
526 u32 length, u32 request,
527 struct htc_packet *packet)
529 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
530 struct bus_request *bus_req;
532 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
537 bus_req->address = address;
538 bus_req->buffer = buffer;
539 bus_req->length = length;
540 bus_req->request = request;
541 bus_req->packet = packet;
543 spin_lock_bh(&ar_sdio->wr_async_lock);
544 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
545 spin_unlock_bh(&ar_sdio->wr_async_lock);
546 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
551 static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
553 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
556 sdio_claim_host(ar_sdio->func);
558 /* Register the isr */
559 ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
561 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
563 sdio_release_host(ar_sdio->func);
566 static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
568 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
571 sdio_claim_host(ar_sdio->func);
573 /* Mask our function IRQ */
574 while (atomic_read(&ar_sdio->irq_handling)) {
575 sdio_release_host(ar_sdio->func);
576 schedule_timeout(HZ / 10);
577 sdio_claim_host(ar_sdio->func);
580 ret = sdio_release_irq(ar_sdio->func);
582 ath6kl_err("Failed to release sdio irq: %d\n", ret);
584 sdio_release_host(ar_sdio->func);
587 static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
589 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
590 struct hif_scatter_req *node = NULL;
592 spin_lock_bh(&ar_sdio->scat_lock);
594 if (!list_empty(&ar_sdio->scat_req)) {
595 node = list_first_entry(&ar_sdio->scat_req,
596 struct hif_scatter_req, list);
597 list_del(&node->list);
600 spin_unlock_bh(&ar_sdio->scat_lock);
605 static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
606 struct hif_scatter_req *s_req)
608 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
610 spin_lock_bh(&ar_sdio->scat_lock);
612 list_add_tail(&s_req->list, &ar_sdio->scat_req);
614 spin_unlock_bh(&ar_sdio->scat_lock);
618 /* scatter gather read write request */
619 static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
620 struct hif_scatter_req *scat_req)
622 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
623 u32 request = scat_req->req;
629 ath6kl_dbg(ATH6KL_DBG_SCATTER,
630 "hif-scatter: total len: %d scatter entries: %d\n",
631 scat_req->len, scat_req->scat_entries);
633 if (request & HIF_SYNCHRONOUS)
634 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
636 spin_lock_bh(&ar_sdio->wr_async_lock);
637 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
638 spin_unlock_bh(&ar_sdio->wr_async_lock);
639 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
645 /* clean up scatter support */
646 static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
648 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
649 struct hif_scatter_req *s_req, *tmp_req;
651 /* empty the free list */
652 spin_lock_bh(&ar_sdio->scat_lock);
653 list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
654 list_del(&s_req->list);
655 spin_unlock_bh(&ar_sdio->scat_lock);
658 * FIXME: should we also call completion handler with
659 * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
660 * that the packet is properly freed?
662 if (s_req->busrequest)
663 ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
664 kfree(s_req->virt_dma_buf);
665 kfree(s_req->sgentries);
668 spin_lock_bh(&ar_sdio->scat_lock);
670 spin_unlock_bh(&ar_sdio->scat_lock);
673 /* setup of HIF scatter resources */
674 static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
676 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
677 struct htc_target *target = ar->htc_target;
679 bool virt_scat = false;
681 if (ar_sdio->scatter_enabled)
684 ar_sdio->scatter_enabled = true;
686 /* check if host supports scatter and it meets our requirements */
687 if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
688 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
689 ar_sdio->func->card->host->max_segs,
690 MAX_SCATTER_ENTRIES_PER_REQ);
695 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
696 MAX_SCATTER_ENTRIES_PER_REQ,
697 MAX_SCATTER_REQUESTS, virt_scat);
700 ath6kl_dbg(ATH6KL_DBG_BOOT,
701 "hif-scatter enabled requests %d entries %d\n",
702 MAX_SCATTER_REQUESTS,
703 MAX_SCATTER_ENTRIES_PER_REQ);
705 target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
706 target->max_xfer_szper_scatreq =
707 MAX_SCATTER_REQ_TRANSFER_SIZE;
709 ath6kl_sdio_cleanup_scatter(ar);
710 ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
714 if (virt_scat || ret) {
715 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
716 ATH6KL_SCATTER_ENTRIES_PER_REQ,
717 ATH6KL_SCATTER_REQS, virt_scat);
720 ath6kl_err("failed to alloc virtual scatter resources !\n");
721 ath6kl_sdio_cleanup_scatter(ar);
725 ath6kl_dbg(ATH6KL_DBG_BOOT,
726 "virtual scatter enabled requests %d entries %d\n",
727 ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
729 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
730 target->max_xfer_szper_scatreq =
731 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
737 static int ath6kl_sdio_config(struct ath6kl *ar)
739 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
740 struct sdio_func *func = ar_sdio->func;
743 sdio_claim_host(func);
745 if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
746 MANUFACTURER_ID_AR6003_BASE) {
747 /* enable 4-bit ASYNC interrupt on AR6003 or later */
748 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
749 CCCR_SDIO_IRQ_MODE_REG,
750 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
752 ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
757 ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
760 /* give us some time to enable, in ms */
761 func->enable_timeout = 100;
763 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
765 ath6kl_err("Set sdio block size %d failed: %d)\n",
766 HIF_MBOX_BLOCK_SIZE, ret);
767 sdio_release_host(func);
772 sdio_release_host(func);
777 static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
779 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
780 struct sdio_func *func = ar_sdio->func;
784 flags = sdio_get_host_pm_caps(func);
786 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
788 if (!(flags & MMC_PM_KEEP_POWER) ||
789 (ar->conf_flags & ATH6KL_CONF_SUSPEND_CUTPOWER)) {
790 /* as host doesn't support keep power we need to cut power */
791 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER,
795 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
797 printk(KERN_ERR "ath6kl: set sdio pm flags failed: %d\n",
802 if ((flags & MMC_PM_WAKE_SDIO_IRQ) && wow) {
804 * The host sdio controller is capable of keep power and
805 * sdio irq wake up at this point. It's fine to continue
806 * wow suspend operation.
808 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
812 ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
814 ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
819 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP, NULL);
822 static int ath6kl_sdio_resume(struct ath6kl *ar)
825 case ATH6KL_STATE_OFF:
826 case ATH6KL_STATE_CUTPOWER:
827 ath6kl_dbg(ATH6KL_DBG_SUSPEND,
828 "sdio resume configuring sdio\n");
830 /* need to set sdio settings after power is cut from sdio */
831 ath6kl_sdio_config(ar);
834 case ATH6KL_STATE_ON:
837 case ATH6KL_STATE_DEEPSLEEP:
840 case ATH6KL_STATE_WOW:
844 ath6kl_cfg80211_resume(ar);
849 static void ath6kl_sdio_stop(struct ath6kl *ar)
851 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
852 struct bus_request *req, *tmp_req;
855 /* FIXME: make sure that wq is not queued again */
857 cancel_work_sync(&ar_sdio->wr_async_work);
859 spin_lock_bh(&ar_sdio->wr_async_lock);
861 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
862 list_del(&req->list);
865 /* this is a scatter gather request */
866 req->scat_req->status = -ECANCELED;
867 req->scat_req->complete(ar_sdio->ar->htc_target,
870 context = req->packet;
871 ath6kl_sdio_free_bus_req(ar_sdio, req);
872 ath6kl_hif_rw_comp_handler(context, -ECANCELED);
876 spin_unlock_bh(&ar_sdio->wr_async_lock);
878 WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
881 static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
882 .read_write_sync = ath6kl_sdio_read_write_sync,
883 .write_async = ath6kl_sdio_write_async,
884 .irq_enable = ath6kl_sdio_irq_enable,
885 .irq_disable = ath6kl_sdio_irq_disable,
886 .scatter_req_get = ath6kl_sdio_scatter_req_get,
887 .scatter_req_add = ath6kl_sdio_scatter_req_add,
888 .enable_scatter = ath6kl_sdio_enable_scatter,
889 .scat_req_rw = ath6kl_sdio_async_rw_scatter,
890 .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
891 .suspend = ath6kl_sdio_suspend,
892 .resume = ath6kl_sdio_resume,
893 .power_on = ath6kl_sdio_power_on,
894 .power_off = ath6kl_sdio_power_off,
895 .stop = ath6kl_sdio_stop,
898 #ifdef CONFIG_PM_SLEEP
901 * Empty handlers so that mmc subsystem doesn't remove us entirely during
902 * suspend. We instead follow cfg80211 suspend/resume handlers.
904 static int ath6kl_sdio_pm_suspend(struct device *device)
906 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
911 static int ath6kl_sdio_pm_resume(struct device *device)
913 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
918 static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
919 ath6kl_sdio_pm_resume);
921 #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
925 #define ATH6KL_SDIO_PM_OPS NULL
927 #endif /* CONFIG_PM_SLEEP */
929 static int ath6kl_sdio_probe(struct sdio_func *func,
930 const struct sdio_device_id *id)
933 struct ath6kl_sdio *ar_sdio;
937 ath6kl_dbg(ATH6KL_DBG_BOOT,
938 "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
939 func->num, func->vendor, func->device,
940 func->max_blksize, func->cur_blksize);
942 ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
946 ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
947 if (!ar_sdio->dma_buffer) {
952 ar_sdio->func = func;
953 sdio_set_drvdata(func, ar_sdio);
956 ar_sdio->is_disabled = true;
958 spin_lock_init(&ar_sdio->lock);
959 spin_lock_init(&ar_sdio->scat_lock);
960 spin_lock_init(&ar_sdio->wr_async_lock);
962 INIT_LIST_HEAD(&ar_sdio->scat_req);
963 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
964 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
966 INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
968 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
969 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
971 ar = ath6kl_core_alloc(&ar_sdio->func->dev);
973 ath6kl_err("Failed to alloc ath6kl core\n");
979 ar->hif_priv = ar_sdio;
980 ar->hif_ops = &ath6kl_sdio_ops;
982 ath6kl_sdio_set_mbox_info(ar);
984 ret = ath6kl_sdio_config(ar);
986 ath6kl_err("Failed to config sdio: %d\n", ret);
990 ret = ath6kl_core_init(ar);
992 ath6kl_err("Failed to init ath6kl core\n");
999 ath6kl_core_free(ar_sdio->ar);
1001 kfree(ar_sdio->dma_buffer);
1008 static void ath6kl_sdio_remove(struct sdio_func *func)
1010 struct ath6kl_sdio *ar_sdio;
1012 ath6kl_dbg(ATH6KL_DBG_BOOT,
1013 "sdio removed func %d vendor 0x%x device 0x%x\n",
1014 func->num, func->vendor, func->device);
1016 ar_sdio = sdio_get_drvdata(func);
1018 ath6kl_stop_txrx(ar_sdio->ar);
1019 cancel_work_sync(&ar_sdio->wr_async_work);
1021 ath6kl_core_cleanup(ar_sdio->ar);
1023 kfree(ar_sdio->dma_buffer);
1027 static const struct sdio_device_id ath6kl_sdio_devices[] = {
1028 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
1029 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
1033 MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
1035 static struct sdio_driver ath6kl_sdio_driver = {
1037 .id_table = ath6kl_sdio_devices,
1038 .probe = ath6kl_sdio_probe,
1039 .remove = ath6kl_sdio_remove,
1040 .drv.pm = ATH6KL_SDIO_PM_OPS,
1043 static int __init ath6kl_sdio_init(void)
1047 ret = sdio_register_driver(&ath6kl_sdio_driver);
1049 ath6kl_err("sdio driver registration failed: %d\n", ret);
1054 static void __exit ath6kl_sdio_exit(void)
1056 sdio_unregister_driver(&ath6kl_sdio_driver);
1059 module_init(ath6kl_sdio_init);
1060 module_exit(ath6kl_sdio_exit);
1062 MODULE_AUTHOR("Atheros Communications, Inc.");
1063 MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
1064 MODULE_LICENSE("Dual BSD/GPL");
1066 MODULE_FIRMWARE(AR6003_REV2_OTP_FILE);
1067 MODULE_FIRMWARE(AR6003_REV2_FIRMWARE_FILE);
1068 MODULE_FIRMWARE(AR6003_REV2_PATCH_FILE);
1069 MODULE_FIRMWARE(AR6003_REV2_BOARD_DATA_FILE);
1070 MODULE_FIRMWARE(AR6003_REV2_DEFAULT_BOARD_DATA_FILE);
1071 MODULE_FIRMWARE(AR6003_REV3_OTP_FILE);
1072 MODULE_FIRMWARE(AR6003_REV3_FIRMWARE_FILE);
1073 MODULE_FIRMWARE(AR6003_REV3_PATCH_FILE);
1074 MODULE_FIRMWARE(AR6003_REV3_BOARD_DATA_FILE);
1075 MODULE_FIRMWARE(AR6003_REV3_DEFAULT_BOARD_DATA_FILE);