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/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
22 #include <linux/mmc/sdio.h>
23 #include <linux/mmc/sd.h>
31 struct sdio_func *func;
36 struct list_head bus_req_freeq;
38 /* available bus requests */
39 struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
44 /* scatter request list head */
45 struct list_head scat_req;
51 atomic_t irq_handling;
52 const struct sdio_device_id *id;
53 struct work_struct wr_async_work;
54 struct list_head wr_asyncq;
55 spinlock_t wr_async_lock;
58 #define CMD53_ARG_READ 0
59 #define CMD53_ARG_WRITE 1
60 #define CMD53_ARG_BLOCK_BASIS 1
61 #define CMD53_ARG_FIXED_ADDRESS 0
62 #define CMD53_ARG_INCR_ADDRESS 1
64 static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
70 * Macro to check if DMA buffer is WORD-aligned and DMA-able.
71 * Most host controllers assume the buffer is DMA'able and will
72 * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
73 * check fails on stack memory.
75 static inline bool buf_needs_bounce(u8 *buf)
77 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
80 static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
82 struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
84 /* EP1 has an extended range */
85 mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
86 mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
87 mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
88 mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
89 mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
90 mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
93 static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
94 u8 mode, u8 opcode, u32 addr,
97 *arg = (((rw & 1) << 31) |
98 ((func & 0x7) << 28) |
100 ((opcode & 1) << 26) |
101 ((addr & 0x1FFFF) << 9) |
105 static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
106 unsigned int address,
111 *arg = ((write & 1) << 31) |
112 ((func & 0x7) << 28) |
115 ((address & 0x1FFFF) << 9) |
120 static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
121 unsigned int address,
124 struct mmc_command io_cmd;
126 memset(&io_cmd, 0, sizeof(io_cmd));
127 ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
128 io_cmd.opcode = SD_IO_RW_DIRECT;
129 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
131 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
134 static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
139 sdio_claim_host(func);
141 if (request & HIF_WRITE) {
142 /* FIXME: looks like ugly workaround for something */
143 if (addr >= HIF_MBOX_BASE_ADDR &&
144 addr <= HIF_MBOX_END_ADDR)
145 addr += (HIF_MBOX_WIDTH - len);
147 /* FIXME: this also looks like ugly workaround */
148 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
149 addr += HIF_MBOX0_EXT_WIDTH - len;
151 if (request & HIF_FIXED_ADDRESS)
152 ret = sdio_writesb(func, addr, buf, len);
154 ret = sdio_memcpy_toio(func, addr, buf, len);
156 if (request & HIF_FIXED_ADDRESS)
157 ret = sdio_readsb(func, buf, addr, len);
159 ret = sdio_memcpy_fromio(func, buf, addr, len);
162 sdio_release_host(func);
164 ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
165 request & HIF_WRITE ? "wr" : "rd", addr,
166 request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
167 ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
172 static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
174 struct bus_request *bus_req;
176 spin_lock_bh(&ar_sdio->lock);
178 if (list_empty(&ar_sdio->bus_req_freeq)) {
179 spin_unlock_bh(&ar_sdio->lock);
183 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
184 struct bus_request, list);
185 list_del(&bus_req->list);
187 spin_unlock_bh(&ar_sdio->lock);
188 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
194 static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
195 struct bus_request *bus_req)
197 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
200 spin_lock_bh(&ar_sdio->lock);
201 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
202 spin_unlock_bh(&ar_sdio->lock);
205 static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
206 struct mmc_data *data)
208 struct scatterlist *sg;
211 data->blksz = HIF_MBOX_BLOCK_SIZE;
212 data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
214 ath6kl_dbg(ATH6KL_DBG_SCATTER,
215 "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
216 (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
217 data->blksz, data->blocks, scat_req->len,
218 scat_req->scat_entries);
220 data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
223 /* fill SG entries */
224 sg = scat_req->sgentries;
225 sg_init_table(sg, scat_req->scat_entries);
227 /* assemble SG list */
228 for (i = 0; i < scat_req->scat_entries; i++, sg++) {
229 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
230 i, scat_req->scat_list[i].buf,
231 scat_req->scat_list[i].len);
233 sg_set_buf(sg, scat_req->scat_list[i].buf,
234 scat_req->scat_list[i].len);
237 /* set scatter-gather table for request */
238 data->sg = scat_req->sgentries;
239 data->sg_len = scat_req->scat_entries;
242 static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
243 struct bus_request *req)
245 struct mmc_request mmc_req;
246 struct mmc_command cmd;
247 struct mmc_data data;
248 struct hif_scatter_req *scat_req;
252 scat_req = req->scat_req;
254 if (scat_req->virt_scat) {
256 if (scat_req->req & HIF_BLOCK_BASIS)
257 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
259 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
260 scat_req->addr, scat_req->virt_dma_buf,
265 memset(&mmc_req, 0, sizeof(struct mmc_request));
266 memset(&cmd, 0, sizeof(struct mmc_command));
267 memset(&data, 0, sizeof(struct mmc_data));
269 ath6kl_sdio_setup_scat_data(scat_req, &data);
271 opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
272 CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
274 rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
276 /* Fixup the address so that the last byte will fall on MBOX EOM */
277 if (scat_req->req & HIF_WRITE) {
278 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
279 scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
281 /* Uses extended address range */
282 scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
285 /* set command argument */
286 ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
287 CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
290 cmd.opcode = SD_IO_RW_EXTENDED;
291 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
294 mmc_req.data = &data;
296 sdio_claim_host(ar_sdio->func);
298 mmc_set_data_timeout(&data, ar_sdio->func->card);
299 /* synchronous call to process request */
300 mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
302 sdio_release_host(ar_sdio->func);
304 status = cmd.error ? cmd.error : data.error;
307 scat_req->status = status;
309 if (scat_req->status)
310 ath6kl_err("Scatter write request failed:%d\n",
313 if (scat_req->req & HIF_ASYNCHRONOUS)
314 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
319 static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
320 int n_scat_entry, int n_scat_req,
323 struct hif_scatter_req *s_req;
324 struct bus_request *bus_req;
325 int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
328 scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
329 scat_req_sz = sizeof(*s_req) + scat_list_sz;
332 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
334 buf_sz = 2 * L1_CACHE_BYTES +
335 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
337 for (i = 0; i < n_scat_req; i++) {
338 /* allocate the scatter request */
339 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
344 virt_buf = kzalloc(buf_sz, GFP_KERNEL);
350 s_req->virt_dma_buf =
351 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
353 /* allocate sglist */
354 s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
356 if (!s_req->sgentries) {
362 /* allocate a bus request for this scatter request */
363 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
365 kfree(s_req->sgentries);
366 kfree(s_req->virt_dma_buf);
371 /* assign the scatter request to this bus request */
372 bus_req->scat_req = s_req;
373 s_req->busrequest = bus_req;
375 s_req->virt_scat = virt_scat;
377 /* add it to the scatter pool */
378 hif_scatter_req_add(ar_sdio->ar, s_req);
384 static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
385 u32 len, u32 request)
387 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
390 bool bounced = false;
392 if (request & HIF_BLOCK_BASIS)
393 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
395 if (buf_needs_bounce(buf)) {
396 if (!ar_sdio->dma_buffer)
398 tbuf = ar_sdio->dma_buffer;
399 memcpy(tbuf, buf, len);
404 ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
405 if ((request & HIF_READ) && bounced)
406 memcpy(buf, tbuf, len);
411 static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
412 struct bus_request *req)
415 ath6kl_sdio_scat_rw(ar_sdio, req);
420 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
421 req->buffer, req->length,
423 context = req->packet;
424 ath6kl_sdio_free_bus_req(ar_sdio, req);
425 ath6kl_hif_rw_comp_handler(context, status);
429 static void ath6kl_sdio_write_async_work(struct work_struct *work)
431 struct ath6kl_sdio *ar_sdio;
432 struct bus_request *req, *tmp_req;
434 ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
436 spin_lock_bh(&ar_sdio->wr_async_lock);
437 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
438 list_del(&req->list);
439 spin_unlock_bh(&ar_sdio->wr_async_lock);
440 __ath6kl_sdio_write_async(ar_sdio, req);
441 spin_lock_bh(&ar_sdio->wr_async_lock);
443 spin_unlock_bh(&ar_sdio->wr_async_lock);
446 static void ath6kl_sdio_irq_handler(struct sdio_func *func)
449 struct ath6kl_sdio *ar_sdio;
451 ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
453 ar_sdio = sdio_get_drvdata(func);
454 atomic_set(&ar_sdio->irq_handling, 1);
457 * Release the host during interrups so we can pick it back up when
458 * we process commands.
460 sdio_release_host(ar_sdio->func);
462 status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
463 sdio_claim_host(ar_sdio->func);
464 atomic_set(&ar_sdio->irq_handling, 0);
465 WARN_ON(status && status != -ECANCELED);
468 static int ath6kl_sdio_power_on(struct ath6kl *ar)
470 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
471 struct sdio_func *func = ar_sdio->func;
474 if (!ar_sdio->is_disabled)
477 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
479 sdio_claim_host(func);
481 ret = sdio_enable_func(func);
483 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
484 sdio_release_host(func);
488 sdio_release_host(func);
491 * Wait for hardware to initialise. It should take a lot less than
492 * 10 ms but let's be conservative here.
496 ar_sdio->is_disabled = false;
501 static int ath6kl_sdio_power_off(struct ath6kl *ar)
503 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
506 if (ar_sdio->is_disabled)
509 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
511 /* Disable the card */
512 sdio_claim_host(ar_sdio->func);
513 ret = sdio_disable_func(ar_sdio->func);
514 sdio_release_host(ar_sdio->func);
519 ar_sdio->is_disabled = true;
524 static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
525 u32 length, u32 request,
526 struct htc_packet *packet)
528 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
529 struct bus_request *bus_req;
531 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
536 bus_req->address = address;
537 bus_req->buffer = buffer;
538 bus_req->length = length;
539 bus_req->request = request;
540 bus_req->packet = packet;
542 spin_lock_bh(&ar_sdio->wr_async_lock);
543 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
544 spin_unlock_bh(&ar_sdio->wr_async_lock);
545 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
550 static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
552 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
555 sdio_claim_host(ar_sdio->func);
557 /* Register the isr */
558 ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
560 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
562 sdio_release_host(ar_sdio->func);
565 static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
567 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
570 sdio_claim_host(ar_sdio->func);
572 /* Mask our function IRQ */
573 while (atomic_read(&ar_sdio->irq_handling)) {
574 sdio_release_host(ar_sdio->func);
575 schedule_timeout(HZ / 10);
576 sdio_claim_host(ar_sdio->func);
579 ret = sdio_release_irq(ar_sdio->func);
581 ath6kl_err("Failed to release sdio irq: %d\n", ret);
583 sdio_release_host(ar_sdio->func);
586 static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
588 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
589 struct hif_scatter_req *node = NULL;
591 spin_lock_bh(&ar_sdio->scat_lock);
593 if (!list_empty(&ar_sdio->scat_req)) {
594 node = list_first_entry(&ar_sdio->scat_req,
595 struct hif_scatter_req, list);
596 list_del(&node->list);
599 spin_unlock_bh(&ar_sdio->scat_lock);
604 static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
605 struct hif_scatter_req *s_req)
607 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
609 spin_lock_bh(&ar_sdio->scat_lock);
611 list_add_tail(&s_req->list, &ar_sdio->scat_req);
613 spin_unlock_bh(&ar_sdio->scat_lock);
617 /* scatter gather read write request */
618 static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
619 struct hif_scatter_req *scat_req)
621 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
622 u32 request = scat_req->req;
628 ath6kl_dbg(ATH6KL_DBG_SCATTER,
629 "hif-scatter: total len: %d scatter entries: %d\n",
630 scat_req->len, scat_req->scat_entries);
632 if (request & HIF_SYNCHRONOUS)
633 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
635 spin_lock_bh(&ar_sdio->wr_async_lock);
636 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
637 spin_unlock_bh(&ar_sdio->wr_async_lock);
638 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
644 /* clean up scatter support */
645 static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
647 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
648 struct hif_scatter_req *s_req, *tmp_req;
650 /* empty the free list */
651 spin_lock_bh(&ar_sdio->scat_lock);
652 list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
653 list_del(&s_req->list);
654 spin_unlock_bh(&ar_sdio->scat_lock);
657 * FIXME: should we also call completion handler with
658 * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
659 * that the packet is properly freed?
661 if (s_req->busrequest)
662 ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
663 kfree(s_req->virt_dma_buf);
664 kfree(s_req->sgentries);
667 spin_lock_bh(&ar_sdio->scat_lock);
669 spin_unlock_bh(&ar_sdio->scat_lock);
672 /* setup of HIF scatter resources */
673 static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
675 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
676 struct htc_target *target = ar->htc_target;
678 bool virt_scat = false;
680 if (ar_sdio->scatter_enabled)
683 ar_sdio->scatter_enabled = true;
685 /* check if host supports scatter and it meets our requirements */
686 if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
687 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
688 ar_sdio->func->card->host->max_segs,
689 MAX_SCATTER_ENTRIES_PER_REQ);
694 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
695 MAX_SCATTER_ENTRIES_PER_REQ,
696 MAX_SCATTER_REQUESTS, virt_scat);
699 ath6kl_dbg(ATH6KL_DBG_BOOT,
700 "hif-scatter enabled requests %d entries %d\n",
701 MAX_SCATTER_REQUESTS,
702 MAX_SCATTER_ENTRIES_PER_REQ);
704 target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
705 target->max_xfer_szper_scatreq =
706 MAX_SCATTER_REQ_TRANSFER_SIZE;
708 ath6kl_sdio_cleanup_scatter(ar);
709 ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
713 if (virt_scat || ret) {
714 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
715 ATH6KL_SCATTER_ENTRIES_PER_REQ,
716 ATH6KL_SCATTER_REQS, virt_scat);
719 ath6kl_err("failed to alloc virtual scatter resources !\n");
720 ath6kl_sdio_cleanup_scatter(ar);
724 ath6kl_dbg(ATH6KL_DBG_BOOT,
725 "virtual scatter enabled requests %d entries %d\n",
726 ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
728 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
729 target->max_xfer_szper_scatreq =
730 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
736 static int ath6kl_sdio_config(struct ath6kl *ar)
738 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
739 struct sdio_func *func = ar_sdio->func;
742 sdio_claim_host(func);
744 if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
745 MANUFACTURER_ID_AR6003_BASE) {
746 /* enable 4-bit ASYNC interrupt on AR6003 or later */
747 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
748 CCCR_SDIO_IRQ_MODE_REG,
749 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
751 ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
756 ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
759 /* give us some time to enable, in ms */
760 func->enable_timeout = 100;
762 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
764 ath6kl_err("Set sdio block size %d failed: %d)\n",
765 HIF_MBOX_BLOCK_SIZE, ret);
766 sdio_release_host(func);
771 sdio_release_host(func);
776 static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
778 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
779 struct sdio_func *func = ar_sdio->func;
783 flags = sdio_get_host_pm_caps(func);
785 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
787 if (!(flags & MMC_PM_KEEP_POWER) ||
788 (ar->conf_flags & ATH6KL_CONF_SUSPEND_CUTPOWER)) {
789 /* as host doesn't support keep power we need to cut power */
790 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER,
794 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
796 printk(KERN_ERR "ath6kl: set sdio pm flags failed: %d\n",
801 if ((flags & MMC_PM_WAKE_SDIO_IRQ) && wow) {
803 * The host sdio controller is capable of keep power and
804 * sdio irq wake up at this point. It's fine to continue
805 * wow suspend operation.
807 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
811 ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
813 ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
818 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP, NULL);
821 static int ath6kl_sdio_resume(struct ath6kl *ar)
824 case ATH6KL_STATE_OFF:
825 case ATH6KL_STATE_CUTPOWER:
826 ath6kl_dbg(ATH6KL_DBG_SUSPEND,
827 "sdio resume configuring sdio\n");
829 /* need to set sdio settings after power is cut from sdio */
830 ath6kl_sdio_config(ar);
833 case ATH6KL_STATE_ON:
836 case ATH6KL_STATE_DEEPSLEEP:
839 case ATH6KL_STATE_WOW:
843 ath6kl_cfg80211_resume(ar);
848 /* set the window address register (using 4-byte register access ). */
849 static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
856 * Write bytes 1,2,3 of the register to set the upper address bytes,
857 * the LSB is written last to initiate the access cycle
860 for (i = 1; i <= 3; i++) {
862 * Fill the buffer with the address byte value we want to
865 memset(addr_val, ((u8 *)&addr)[i], 4);
868 * Hit each byte of the register address with a 4-byte
869 * write operation to the same address, this is a harmless
872 status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
873 4, HIF_WR_SYNC_BYTE_FIX);
879 ath6kl_err("%s: failed to write initial bytes of 0x%x "
880 "to window reg: 0x%X\n", __func__,
886 * Write the address register again, this time write the whole
887 * 4-byte value. The effect here is that the LSB write causes the
888 * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
889 * effect since we are writing the same values again
891 status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
892 4, HIF_WR_SYNC_BYTE_INC);
895 ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
896 __func__, addr, reg_addr);
903 static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
907 /* set window register to start read cycle */
908 status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
915 status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
916 (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
918 ath6kl_err("%s: failed to read from window data addr\n",
926 static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
930 u32 val = (__force u32) data;
933 status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
934 (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
936 ath6kl_err("%s: failed to write 0x%x to window data addr\n",
941 /* set window register, which starts the write cycle */
942 return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
946 static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
949 unsigned long timeout;
952 ar->bmi.cmd_credits = 0;
954 /* Read the counter register to get the command credits */
955 addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
957 timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
958 while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
961 * Hit the credit counter with a 4-byte access, the first byte
962 * read will hit the counter and cause a decrement, while the
963 * remaining 3 bytes has no effect. The rationale behind this
964 * is to make all HIF accesses 4-byte aligned.
966 ret = ath6kl_sdio_read_write_sync(ar, addr,
967 (u8 *)&ar->bmi.cmd_credits, 4,
968 HIF_RD_SYNC_BYTE_INC);
970 ath6kl_err("Unable to decrement the command credit "
971 "count register: %d\n", ret);
975 /* The counter is only 8 bits.
976 * Ignore anything in the upper 3 bytes
978 ar->bmi.cmd_credits &= 0xFF;
981 if (!ar->bmi.cmd_credits) {
982 ath6kl_err("bmi communication timeout\n");
989 static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
991 unsigned long timeout;
995 timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
996 while ((time_before(jiffies, timeout)) && !rx_word) {
997 ret = ath6kl_sdio_read_write_sync(ar,
998 RX_LOOKAHEAD_VALID_ADDRESS,
999 (u8 *)&rx_word, sizeof(rx_word),
1000 HIF_RD_SYNC_BYTE_INC);
1002 ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
1006 /* all we really want is one bit */
1007 rx_word &= (1 << ENDPOINT1);
1011 ath6kl_err("bmi_recv_buf FIFO empty\n");
1018 static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
1023 ret = ath6kl_sdio_bmi_credits(ar);
1027 addr = ar->mbox_info.htc_addr;
1029 ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1030 HIF_WR_SYNC_BYTE_INC);
1032 ath6kl_err("unable to send the bmi data to the device\n");
1037 static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
1043 * During normal bootup, small reads may be required.
1044 * Rather than issue an HIF Read and then wait as the Target
1045 * adds successive bytes to the FIFO, we wait here until
1046 * we know that response data is available.
1048 * This allows us to cleanly timeout on an unexpected
1049 * Target failure rather than risk problems at the HIF level.
1050 * In particular, this avoids SDIO timeouts and possibly garbage
1051 * data on some host controllers. And on an interconnect
1052 * such as Compact Flash (as well as some SDIO masters) which
1053 * does not provide any indication on data timeout, it avoids
1054 * a potential hang or garbage response.
1056 * Synchronization is more difficult for reads larger than the
1057 * size of the MBOX FIFO (128B), because the Target is unable
1058 * to push the 129th byte of data until AFTER the Host posts an
1059 * HIF Read and removes some FIFO data. So for large reads the
1060 * Host proceeds to post an HIF Read BEFORE all the data is
1061 * actually available to read. Fortunately, large BMI reads do
1062 * not occur in practice -- they're supported for debug/development.
1064 * So Host/Target BMI synchronization is divided into these cases:
1065 * CASE 1: length < 4
1068 * CASE 2: 4 <= length <= 128
1069 * Wait for first 4 bytes to be in FIFO
1070 * If CONSERVATIVE_BMI_READ is enabled, also wait for
1071 * a BMI command credit, which indicates that the ENTIRE
1072 * response is available in the the FIFO
1074 * CASE 3: length > 128
1075 * Wait for the first 4 bytes to be in FIFO
1077 * For most uses, a small timeout should be sufficient and we will
1078 * usually see a response quickly; but there may be some unusual
1079 * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1080 * For now, we use an unbounded busy loop while waiting for
1083 * If BMI_EXECUTE ever needs to support longer-latency execution,
1084 * especially in production, this code needs to be enhanced to sleep
1085 * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
1086 * a function of Host processor speed.
1088 if (len >= 4) { /* NB: Currently, always true */
1089 ret = ath6kl_bmi_get_rx_lkahd(ar);
1094 addr = ar->mbox_info.htc_addr;
1095 ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1096 HIF_RD_SYNC_BYTE_INC);
1098 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1106 static void ath6kl_sdio_stop(struct ath6kl *ar)
1108 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
1109 struct bus_request *req, *tmp_req;
1112 /* FIXME: make sure that wq is not queued again */
1114 cancel_work_sync(&ar_sdio->wr_async_work);
1116 spin_lock_bh(&ar_sdio->wr_async_lock);
1118 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1119 list_del(&req->list);
1121 if (req->scat_req) {
1122 /* this is a scatter gather request */
1123 req->scat_req->status = -ECANCELED;
1124 req->scat_req->complete(ar_sdio->ar->htc_target,
1127 context = req->packet;
1128 ath6kl_sdio_free_bus_req(ar_sdio, req);
1129 ath6kl_hif_rw_comp_handler(context, -ECANCELED);
1133 spin_unlock_bh(&ar_sdio->wr_async_lock);
1135 WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
1138 static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
1139 .read_write_sync = ath6kl_sdio_read_write_sync,
1140 .write_async = ath6kl_sdio_write_async,
1141 .irq_enable = ath6kl_sdio_irq_enable,
1142 .irq_disable = ath6kl_sdio_irq_disable,
1143 .scatter_req_get = ath6kl_sdio_scatter_req_get,
1144 .scatter_req_add = ath6kl_sdio_scatter_req_add,
1145 .enable_scatter = ath6kl_sdio_enable_scatter,
1146 .scat_req_rw = ath6kl_sdio_async_rw_scatter,
1147 .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
1148 .suspend = ath6kl_sdio_suspend,
1149 .resume = ath6kl_sdio_resume,
1150 .diag_read32 = ath6kl_sdio_diag_read32,
1151 .diag_write32 = ath6kl_sdio_diag_write32,
1152 .bmi_read = ath6kl_sdio_bmi_read,
1153 .bmi_write = ath6kl_sdio_bmi_write,
1154 .power_on = ath6kl_sdio_power_on,
1155 .power_off = ath6kl_sdio_power_off,
1156 .stop = ath6kl_sdio_stop,
1159 #ifdef CONFIG_PM_SLEEP
1162 * Empty handlers so that mmc subsystem doesn't remove us entirely during
1163 * suspend. We instead follow cfg80211 suspend/resume handlers.
1165 static int ath6kl_sdio_pm_suspend(struct device *device)
1167 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
1172 static int ath6kl_sdio_pm_resume(struct device *device)
1174 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
1179 static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
1180 ath6kl_sdio_pm_resume);
1182 #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
1186 #define ATH6KL_SDIO_PM_OPS NULL
1188 #endif /* CONFIG_PM_SLEEP */
1190 static int ath6kl_sdio_probe(struct sdio_func *func,
1191 const struct sdio_device_id *id)
1194 struct ath6kl_sdio *ar_sdio;
1198 ath6kl_dbg(ATH6KL_DBG_BOOT,
1199 "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
1200 func->num, func->vendor, func->device,
1201 func->max_blksize, func->cur_blksize);
1203 ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
1207 ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
1208 if (!ar_sdio->dma_buffer) {
1213 ar_sdio->func = func;
1214 sdio_set_drvdata(func, ar_sdio);
1217 ar_sdio->is_disabled = true;
1219 spin_lock_init(&ar_sdio->lock);
1220 spin_lock_init(&ar_sdio->scat_lock);
1221 spin_lock_init(&ar_sdio->wr_async_lock);
1223 INIT_LIST_HEAD(&ar_sdio->scat_req);
1224 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
1225 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
1227 INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
1229 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
1230 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
1232 ar = ath6kl_core_alloc(&ar_sdio->func->dev);
1234 ath6kl_err("Failed to alloc ath6kl core\n");
1240 ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
1241 ar->hif_priv = ar_sdio;
1242 ar->hif_ops = &ath6kl_sdio_ops;
1243 ar->bmi.max_data_size = 256;
1245 ath6kl_sdio_set_mbox_info(ar);
1247 ret = ath6kl_sdio_config(ar);
1249 ath6kl_err("Failed to config sdio: %d\n", ret);
1250 goto err_core_alloc;
1253 ret = ath6kl_core_init(ar);
1255 ath6kl_err("Failed to init ath6kl core\n");
1256 goto err_core_alloc;
1262 ath6kl_core_free(ar_sdio->ar);
1264 kfree(ar_sdio->dma_buffer);
1271 static void ath6kl_sdio_remove(struct sdio_func *func)
1273 struct ath6kl_sdio *ar_sdio;
1275 ath6kl_dbg(ATH6KL_DBG_BOOT,
1276 "sdio removed func %d vendor 0x%x device 0x%x\n",
1277 func->num, func->vendor, func->device);
1279 ar_sdio = sdio_get_drvdata(func);
1281 ath6kl_stop_txrx(ar_sdio->ar);
1282 cancel_work_sync(&ar_sdio->wr_async_work);
1284 ath6kl_core_cleanup(ar_sdio->ar);
1286 kfree(ar_sdio->dma_buffer);
1290 static const struct sdio_device_id ath6kl_sdio_devices[] = {
1291 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
1292 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
1293 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
1294 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
1298 MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
1300 static struct sdio_driver ath6kl_sdio_driver = {
1301 .name = "ath6kl_sdio",
1302 .id_table = ath6kl_sdio_devices,
1303 .probe = ath6kl_sdio_probe,
1304 .remove = ath6kl_sdio_remove,
1305 .drv.pm = ATH6KL_SDIO_PM_OPS,
1308 static int __init ath6kl_sdio_init(void)
1312 ret = sdio_register_driver(&ath6kl_sdio_driver);
1314 ath6kl_err("sdio driver registration failed: %d\n", ret);
1319 static void __exit ath6kl_sdio_exit(void)
1321 sdio_unregister_driver(&ath6kl_sdio_driver);
1324 module_init(ath6kl_sdio_init);
1325 module_exit(ath6kl_sdio_exit);
1327 MODULE_AUTHOR("Atheros Communications, Inc.");
1328 MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
1329 MODULE_LICENSE("Dual BSD/GPL");
1331 MODULE_FIRMWARE(AR6003_HW_2_0_OTP_FILE);
1332 MODULE_FIRMWARE(AR6003_HW_2_0_FIRMWARE_FILE);
1333 MODULE_FIRMWARE(AR6003_HW_2_0_PATCH_FILE);
1334 MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
1335 MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
1336 MODULE_FIRMWARE(AR6003_HW_2_1_1_OTP_FILE);
1337 MODULE_FIRMWARE(AR6003_HW_2_1_1_FIRMWARE_FILE);
1338 MODULE_FIRMWARE(AR6003_HW_2_1_1_PATCH_FILE);
1339 MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
1340 MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);