blob: f1dc311ee0c7b041bdbcac42b5bc29b867e7cdd4 [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001/*
2 * Copyright (c) 2004-2011 Atheros Communications Inc.
3 *
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.
7 *
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.
15 */
16
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>
24#include "htc_hif.h"
25#include "hif-ops.h"
26#include "target.h"
27#include "debug.h"
Vivek Natarajan9df337a2011-09-15 20:30:43 +053028#include "cfg80211.h"
Kalle Valobdcd8172011-07-18 00:22:30 +030029
30struct ath6kl_sdio {
31 struct sdio_func *func;
32
33 spinlock_t lock;
34
35 /* free list */
36 struct list_head bus_req_freeq;
37
38 /* available bus requests */
39 struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
40
41 struct ath6kl *ar;
42 u8 *dma_buffer;
43
44 /* scatter request list head */
45 struct list_head scat_req;
46
47 spinlock_t scat_lock;
48 bool is_disabled;
49 atomic_t irq_handling;
50 const struct sdio_device_id *id;
51 struct work_struct wr_async_work;
52 struct list_head wr_asyncq;
53 spinlock_t wr_async_lock;
54};
55
56#define CMD53_ARG_READ 0
57#define CMD53_ARG_WRITE 1
58#define CMD53_ARG_BLOCK_BASIS 1
59#define CMD53_ARG_FIXED_ADDRESS 0
60#define CMD53_ARG_INCR_ADDRESS 1
61
62static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
63{
64 return ar->hif_priv;
65}
66
67/*
68 * Macro to check if DMA buffer is WORD-aligned and DMA-able.
69 * Most host controllers assume the buffer is DMA'able and will
70 * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
71 * check fails on stack memory.
72 */
73static inline bool buf_needs_bounce(u8 *buf)
74{
75 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
76}
77
78static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
79{
80 struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
81
82 /* EP1 has an extended range */
83 mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
84 mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
85 mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
86 mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
87 mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
88 mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
89}
90
91static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
92 u8 mode, u8 opcode, u32 addr,
93 u16 blksz)
94{
95 *arg = (((rw & 1) << 31) |
96 ((func & 0x7) << 28) |
97 ((mode & 1) << 27) |
98 ((opcode & 1) << 26) |
99 ((addr & 0x1FFFF) << 9) |
100 (blksz & 0x1FF));
101}
102
103static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
104 unsigned int address,
105 unsigned char val)
106{
107 const u8 func = 0;
108
109 *arg = ((write & 1) << 31) |
110 ((func & 0x7) << 28) |
111 ((raw & 1) << 27) |
112 (1 << 26) |
113 ((address & 0x1FFFF) << 9) |
114 (1 << 8) |
115 (val & 0xFF);
116}
117
118static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
119 unsigned int address,
120 unsigned char byte)
121{
122 struct mmc_command io_cmd;
123
124 memset(&io_cmd, 0, sizeof(io_cmd));
125 ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
126 io_cmd.opcode = SD_IO_RW_DIRECT;
127 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
128
129 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
130}
131
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530132static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
133 u8 *buf, u32 len)
134{
135 int ret = 0;
136
137 if (request & HIF_WRITE) {
Kalle Valof7325b82011-09-27 14:30:58 +0300138 /* FIXME: looks like ugly workaround for something */
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530139 if (addr >= HIF_MBOX_BASE_ADDR &&
140 addr <= HIF_MBOX_END_ADDR)
141 addr += (HIF_MBOX_WIDTH - len);
142
Kalle Valof7325b82011-09-27 14:30:58 +0300143 /* FIXME: this also looks like ugly workaround */
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530144 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
145 addr += HIF_MBOX0_EXT_WIDTH - len;
146
147 if (request & HIF_FIXED_ADDRESS)
148 ret = sdio_writesb(func, addr, buf, len);
149 else
150 ret = sdio_memcpy_toio(func, addr, buf, len);
151 } else {
152 if (request & HIF_FIXED_ADDRESS)
153 ret = sdio_readsb(func, buf, addr, len);
154 else
155 ret = sdio_memcpy_fromio(func, buf, addr, len);
156 }
157
Kalle Valof7325b82011-09-27 14:30:58 +0300158 ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
159 request & HIF_WRITE ? "wr" : "rd", addr,
160 request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
161 ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
162
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530163 return ret;
164}
165
Kalle Valobdcd8172011-07-18 00:22:30 +0300166static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
167{
168 struct bus_request *bus_req;
169 unsigned long flag;
170
171 spin_lock_irqsave(&ar_sdio->lock, flag);
172
173 if (list_empty(&ar_sdio->bus_req_freeq)) {
174 spin_unlock_irqrestore(&ar_sdio->lock, flag);
175 return NULL;
176 }
177
178 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
179 struct bus_request, list);
180 list_del(&bus_req->list);
181
182 spin_unlock_irqrestore(&ar_sdio->lock, flag);
Kalle Valof7325b82011-09-27 14:30:58 +0300183 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
184 __func__, bus_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300185
186 return bus_req;
187}
188
189static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
190 struct bus_request *bus_req)
191{
192 unsigned long flag;
193
Kalle Valof7325b82011-09-27 14:30:58 +0300194 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
195 __func__, bus_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300196
197 spin_lock_irqsave(&ar_sdio->lock, flag);
198 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
199 spin_unlock_irqrestore(&ar_sdio->lock, flag);
200}
201
202static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
Kalle Valobdcd8172011-07-18 00:22:30 +0300203 struct mmc_data *data)
204{
205 struct scatterlist *sg;
206 int i;
207
208 data->blksz = HIF_MBOX_BLOCK_SIZE;
209 data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
210
211 ath6kl_dbg(ATH6KL_DBG_SCATTER,
212 "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
213 (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
214 data->blksz, data->blocks, scat_req->len,
215 scat_req->scat_entries);
216
217 data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
218 MMC_DATA_READ;
219
220 /* fill SG entries */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530221 sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300222 sg_init_table(sg, scat_req->scat_entries);
223
224 /* assemble SG list */
225 for (i = 0; i < scat_req->scat_entries; i++, sg++) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300226 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
227 i, scat_req->scat_list[i].buf,
228 scat_req->scat_list[i].len);
229
230 sg_set_buf(sg, scat_req->scat_list[i].buf,
231 scat_req->scat_list[i].len);
232 }
233
234 /* set scatter-gather table for request */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530235 data->sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300236 data->sg_len = scat_req->scat_entries;
237}
238
239static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
240 struct bus_request *req)
241{
242 struct mmc_request mmc_req;
243 struct mmc_command cmd;
244 struct mmc_data data;
245 struct hif_scatter_req *scat_req;
246 u8 opcode, rw;
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530247 int status, len;
Kalle Valobdcd8172011-07-18 00:22:30 +0300248
249 scat_req = req->scat_req;
250
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530251 if (scat_req->virt_scat) {
252 len = scat_req->len;
253 if (scat_req->req & HIF_BLOCK_BASIS)
254 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
255
256 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
257 scat_req->addr, scat_req->virt_dma_buf,
258 len);
259 goto scat_complete;
260 }
261
Kalle Valobdcd8172011-07-18 00:22:30 +0300262 memset(&mmc_req, 0, sizeof(struct mmc_request));
263 memset(&cmd, 0, sizeof(struct mmc_command));
264 memset(&data, 0, sizeof(struct mmc_data));
265
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530266 ath6kl_sdio_setup_scat_data(scat_req, &data);
Kalle Valobdcd8172011-07-18 00:22:30 +0300267
268 opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
269 CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
270
271 rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
272
273 /* Fixup the address so that the last byte will fall on MBOX EOM */
274 if (scat_req->req & HIF_WRITE) {
275 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
276 scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
277 else
278 /* Uses extended address range */
279 scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
280 }
281
282 /* set command argument */
283 ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
284 CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
285 data.blocks);
286
287 cmd.opcode = SD_IO_RW_EXTENDED;
288 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
289
290 mmc_req.cmd = &cmd;
291 mmc_req.data = &data;
292
293 mmc_set_data_timeout(&data, ar_sdio->func->card);
294 /* synchronous call to process request */
295 mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
296
297 status = cmd.error ? cmd.error : data.error;
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530298
299scat_complete:
Kalle Valobdcd8172011-07-18 00:22:30 +0300300 scat_req->status = status;
301
302 if (scat_req->status)
303 ath6kl_err("Scatter write request failed:%d\n",
304 scat_req->status);
305
306 if (scat_req->req & HIF_ASYNCHRONOUS)
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530307 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300308
309 return status;
310}
311
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530312static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
313 int n_scat_entry, int n_scat_req,
314 bool virt_scat)
315{
316 struct hif_scatter_req *s_req;
317 struct bus_request *bus_req;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530318 int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
319 u8 *virt_buf;
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530320
321 scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
322 scat_req_sz = sizeof(*s_req) + scat_list_sz;
323
324 if (!virt_scat)
325 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530326 else
327 buf_sz = 2 * L1_CACHE_BYTES +
328 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530329
330 for (i = 0; i < n_scat_req; i++) {
331 /* allocate the scatter request */
332 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
333 if (!s_req)
334 return -ENOMEM;
335
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530336 if (virt_scat) {
337 virt_buf = kzalloc(buf_sz, GFP_KERNEL);
338 if (!virt_buf) {
339 kfree(s_req);
340 return -ENOMEM;
341 }
342
343 s_req->virt_dma_buf =
344 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
345 } else {
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530346 /* allocate sglist */
347 s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
348
349 if (!s_req->sgentries) {
350 kfree(s_req);
351 return -ENOMEM;
352 }
353 }
354
355 /* allocate a bus request for this scatter request */
356 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
357 if (!bus_req) {
358 kfree(s_req->sgentries);
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530359 kfree(s_req->virt_dma_buf);
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530360 kfree(s_req);
361 return -ENOMEM;
362 }
363
364 /* assign the scatter request to this bus request */
365 bus_req->scat_req = s_req;
366 s_req->busrequest = bus_req;
367
Vasanthakumar Thiagarajan4a005c32011-07-16 20:29:15 +0530368 s_req->virt_scat = virt_scat;
369
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530370 /* add it to the scatter pool */
371 hif_scatter_req_add(ar_sdio->ar, s_req);
372 }
373
374 return 0;
375}
376
Kalle Valobdcd8172011-07-18 00:22:30 +0300377static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
378 u32 len, u32 request)
379{
380 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
381 u8 *tbuf = NULL;
382 int ret;
383 bool bounced = false;
384
385 if (request & HIF_BLOCK_BASIS)
386 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
387
388 if (buf_needs_bounce(buf)) {
389 if (!ar_sdio->dma_buffer)
390 return -ENOMEM;
391 tbuf = ar_sdio->dma_buffer;
392 memcpy(tbuf, buf, len);
393 bounced = true;
394 } else
395 tbuf = buf;
396
397 sdio_claim_host(ar_sdio->func);
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530398 ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
399 if ((request & HIF_READ) && bounced)
400 memcpy(buf, tbuf, len);
Kalle Valobdcd8172011-07-18 00:22:30 +0300401 sdio_release_host(ar_sdio->func);
402
403 return ret;
404}
405
406static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
407 struct bus_request *req)
408{
409 if (req->scat_req)
410 ath6kl_sdio_scat_rw(ar_sdio, req);
411 else {
412 void *context;
413 int status;
414
415 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
416 req->buffer, req->length,
417 req->request);
418 context = req->packet;
419 ath6kl_sdio_free_bus_req(ar_sdio, req);
420 ath6kldev_rw_comp_handler(context, status);
421 }
422}
423
424static void ath6kl_sdio_write_async_work(struct work_struct *work)
425{
426 struct ath6kl_sdio *ar_sdio;
427 unsigned long flags;
428 struct bus_request *req, *tmp_req;
429
430 ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
431 sdio_claim_host(ar_sdio->func);
432
433 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
434 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
435 list_del(&req->list);
436 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
437 __ath6kl_sdio_write_async(ar_sdio, req);
438 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
439 }
440 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
441
442 sdio_release_host(ar_sdio->func);
443}
444
445static void ath6kl_sdio_irq_handler(struct sdio_func *func)
446{
447 int status;
448 struct ath6kl_sdio *ar_sdio;
449
Kalle Valof7325b82011-09-27 14:30:58 +0300450 ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
451
Kalle Valobdcd8172011-07-18 00:22:30 +0300452 ar_sdio = sdio_get_drvdata(func);
453 atomic_set(&ar_sdio->irq_handling, 1);
454
455 /*
456 * Release the host during interrups so we can pick it back up when
457 * we process commands.
458 */
459 sdio_release_host(ar_sdio->func);
460
461 status = ath6kldev_intr_bh_handler(ar_sdio->ar);
462 sdio_claim_host(ar_sdio->func);
463 atomic_set(&ar_sdio->irq_handling, 0);
464 WARN_ON(status && status != -ECANCELED);
465}
466
467static int ath6kl_sdio_power_on(struct ath6kl_sdio *ar_sdio)
468{
469 struct sdio_func *func = ar_sdio->func;
470 int ret = 0;
471
472 if (!ar_sdio->is_disabled)
473 return 0;
474
475 sdio_claim_host(func);
476
477 ret = sdio_enable_func(func);
478 if (ret) {
479 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
480 sdio_release_host(func);
481 return ret;
482 }
483
484 sdio_release_host(func);
485
486 /*
487 * Wait for hardware to initialise. It should take a lot less than
488 * 10 ms but let's be conservative here.
489 */
490 msleep(10);
491
492 ar_sdio->is_disabled = false;
493
494 return ret;
495}
496
497static int ath6kl_sdio_power_off(struct ath6kl_sdio *ar_sdio)
498{
499 int ret;
500
501 if (ar_sdio->is_disabled)
502 return 0;
503
504 /* Disable the card */
505 sdio_claim_host(ar_sdio->func);
506 ret = sdio_disable_func(ar_sdio->func);
507 sdio_release_host(ar_sdio->func);
508
509 if (ret)
510 return ret;
511
512 ar_sdio->is_disabled = true;
513
514 return ret;
515}
516
517static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
518 u32 length, u32 request,
519 struct htc_packet *packet)
520{
521 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
522 struct bus_request *bus_req;
523 unsigned long flags;
524
525 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
526
527 if (!bus_req)
528 return -ENOMEM;
529
530 bus_req->address = address;
531 bus_req->buffer = buffer;
532 bus_req->length = length;
533 bus_req->request = request;
534 bus_req->packet = packet;
535
536 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
537 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
538 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
539 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
540
541 return 0;
542}
543
544static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
545{
546 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
547 int ret;
548
549 sdio_claim_host(ar_sdio->func);
550
551 /* Register the isr */
552 ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
553 if (ret)
554 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
555
556 sdio_release_host(ar_sdio->func);
557}
558
559static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
560{
561 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
562 int ret;
563
564 sdio_claim_host(ar_sdio->func);
565
566 /* Mask our function IRQ */
567 while (atomic_read(&ar_sdio->irq_handling)) {
568 sdio_release_host(ar_sdio->func);
569 schedule_timeout(HZ / 10);
570 sdio_claim_host(ar_sdio->func);
571 }
572
573 ret = sdio_release_irq(ar_sdio->func);
574 if (ret)
575 ath6kl_err("Failed to release sdio irq: %d\n", ret);
576
577 sdio_release_host(ar_sdio->func);
578}
579
580static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
581{
582 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
583 struct hif_scatter_req *node = NULL;
584 unsigned long flag;
585
586 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
587
588 if (!list_empty(&ar_sdio->scat_req)) {
589 node = list_first_entry(&ar_sdio->scat_req,
590 struct hif_scatter_req, list);
591 list_del(&node->list);
592 }
593
594 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
595
596 return node;
597}
598
599static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
600 struct hif_scatter_req *s_req)
601{
602 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
603 unsigned long flag;
604
605 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
606
607 list_add_tail(&s_req->list, &ar_sdio->scat_req);
608
609 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
610
611}
612
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530613/* scatter gather read write request */
614static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
615 struct hif_scatter_req *scat_req)
616{
617 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530618 u32 request = scat_req->req;
619 int status = 0;
620 unsigned long flags;
621
622 if (!scat_req->len)
623 return -EINVAL;
624
625 ath6kl_dbg(ATH6KL_DBG_SCATTER,
626 "hif-scatter: total len: %d scatter entries: %d\n",
627 scat_req->len, scat_req->scat_entries);
628
629 if (request & HIF_SYNCHRONOUS) {
630 sdio_claim_host(ar_sdio->func);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530631 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530632 sdio_release_host(ar_sdio->func);
633 } else {
634 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530635 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530636 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
637 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
638 }
639
640 return status;
641}
642
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530643/* clean up scatter support */
644static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
645{
646 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
647 struct hif_scatter_req *s_req, *tmp_req;
648 unsigned long flag;
649
650 /* empty the free list */
651 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
652 list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
653 list_del(&s_req->list);
654 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
655
656 if (s_req->busrequest)
657 ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
658 kfree(s_req->virt_dma_buf);
659 kfree(s_req->sgentries);
660 kfree(s_req);
661
662 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
663 }
664 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
665}
666
667/* setup of HIF scatter resources */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530668static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530669{
670 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530671 struct htc_target *target = ar->htc_target;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530672 int ret;
673 bool virt_scat = false;
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530674
675 /* check if host supports scatter and it meets our requirements */
676 if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530677 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530678 ar_sdio->func->card->host->max_segs,
679 MAX_SCATTER_ENTRIES_PER_REQ);
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530680 virt_scat = true;
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530681 }
682
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530683 if (!virt_scat) {
684 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
685 MAX_SCATTER_ENTRIES_PER_REQ,
686 MAX_SCATTER_REQUESTS, virt_scat);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530687
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530688 if (!ret) {
Kalle Valof7325b82011-09-27 14:30:58 +0300689 ath6kl_dbg(ATH6KL_DBG_SCATTER,
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530690 "hif-scatter enabled: max scatter req : %d entries: %d\n",
691 MAX_SCATTER_REQUESTS,
692 MAX_SCATTER_ENTRIES_PER_REQ);
693
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530694 target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
695 target->max_xfer_szper_scatreq =
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530696 MAX_SCATTER_REQ_TRANSFER_SIZE;
697 } else {
698 ath6kl_sdio_cleanup_scatter(ar);
699 ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
700 }
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530701 }
702
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530703 if (virt_scat || ret) {
704 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
705 ATH6KL_SCATTER_ENTRIES_PER_REQ,
706 ATH6KL_SCATTER_REQS, virt_scat);
707
708 if (ret) {
709 ath6kl_err("failed to alloc virtual scatter resources !\n");
710 ath6kl_sdio_cleanup_scatter(ar);
711 return ret;
712 }
713
Kalle Valof7325b82011-09-27 14:30:58 +0300714 ath6kl_dbg(ATH6KL_DBG_SCATTER,
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530715 "Vitual scatter enabled, max_scat_req:%d, entries:%d\n",
716 ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
717
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530718 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
719 target->max_xfer_szper_scatreq =
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530720 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
721 }
722
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530723 return 0;
724}
725
Kalle Valoabcb3442011-07-22 08:26:20 +0300726static int ath6kl_sdio_suspend(struct ath6kl *ar)
727{
728 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
729 struct sdio_func *func = ar_sdio->func;
730 mmc_pm_flag_t flags;
731 int ret;
732
733 flags = sdio_get_host_pm_caps(func);
734
735 if (!(flags & MMC_PM_KEEP_POWER))
736 /* as host doesn't support keep power we need to bail out */
Kalle Valof7325b82011-09-27 14:30:58 +0300737 ath6kl_dbg(ATH6KL_DBG_SDIO,
738 "func %d doesn't support MMC_PM_KEEP_POWER\n",
739 func->num);
Kalle Valoabcb3442011-07-22 08:26:20 +0300740 return -EINVAL;
741
742 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
743 if (ret) {
744 printk(KERN_ERR "ath6kl: set sdio pm flags failed: %d\n",
745 ret);
746 return ret;
747 }
748
749 ath6kl_deep_sleep_enable(ar);
750
751 return 0;
752}
753
Kalle Valobdcd8172011-07-18 00:22:30 +0300754static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
755 .read_write_sync = ath6kl_sdio_read_write_sync,
756 .write_async = ath6kl_sdio_write_async,
757 .irq_enable = ath6kl_sdio_irq_enable,
758 .irq_disable = ath6kl_sdio_irq_disable,
759 .scatter_req_get = ath6kl_sdio_scatter_req_get,
760 .scatter_req_add = ath6kl_sdio_scatter_req_add,
761 .enable_scatter = ath6kl_sdio_enable_scatter,
Vasanthakumar Thiagarajanf74a7362011-07-16 20:29:05 +0530762 .scat_req_rw = ath6kl_sdio_async_rw_scatter,
Kalle Valobdcd8172011-07-18 00:22:30 +0300763 .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
Kalle Valoabcb3442011-07-22 08:26:20 +0300764 .suspend = ath6kl_sdio_suspend,
Kalle Valobdcd8172011-07-18 00:22:30 +0300765};
766
767static int ath6kl_sdio_probe(struct sdio_func *func,
768 const struct sdio_device_id *id)
769{
770 int ret;
771 struct ath6kl_sdio *ar_sdio;
772 struct ath6kl *ar;
773 int count;
774
Kalle Valof7325b82011-09-27 14:30:58 +0300775 ath6kl_dbg(ATH6KL_DBG_SDIO,
776 "new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
777 func->num, func->vendor, func->device,
778 func->max_blksize, func->cur_blksize);
Kalle Valobdcd8172011-07-18 00:22:30 +0300779
780 ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
781 if (!ar_sdio)
782 return -ENOMEM;
783
784 ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
785 if (!ar_sdio->dma_buffer) {
786 ret = -ENOMEM;
787 goto err_hif;
788 }
789
790 ar_sdio->func = func;
791 sdio_set_drvdata(func, ar_sdio);
792
793 ar_sdio->id = id;
794 ar_sdio->is_disabled = true;
795
796 spin_lock_init(&ar_sdio->lock);
797 spin_lock_init(&ar_sdio->scat_lock);
798 spin_lock_init(&ar_sdio->wr_async_lock);
799
800 INIT_LIST_HEAD(&ar_sdio->scat_req);
801 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
802 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
803
804 INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
805
806 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
807 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
808
809 ar = ath6kl_core_alloc(&ar_sdio->func->dev);
810 if (!ar) {
811 ath6kl_err("Failed to alloc ath6kl core\n");
812 ret = -ENOMEM;
813 goto err_dma;
814 }
815
816 ar_sdio->ar = ar;
817 ar->hif_priv = ar_sdio;
818 ar->hif_ops = &ath6kl_sdio_ops;
819
820 ath6kl_sdio_set_mbox_info(ar);
821
822 sdio_claim_host(func);
823
824 if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
825 MANUFACTURER_ID_AR6003_BASE) {
826 /* enable 4-bit ASYNC interrupt on AR6003 or later */
827 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
828 CCCR_SDIO_IRQ_MODE_REG,
829 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
830 if (ret) {
831 ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
832 ret);
833 sdio_release_host(func);
Vivek Natarajan9df337a2011-09-15 20:30:43 +0530834 goto err_cfg80211;
Kalle Valobdcd8172011-07-18 00:22:30 +0300835 }
836
Kalle Valof7325b82011-09-27 14:30:58 +0300837 ath6kl_dbg(ATH6KL_DBG_SDIO, "4-bit async irq mode enabled\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300838 }
839
840 /* give us some time to enable, in ms */
841 func->enable_timeout = 100;
842
843 sdio_release_host(func);
844
845 ret = ath6kl_sdio_power_on(ar_sdio);
846 if (ret)
Vivek Natarajan9df337a2011-09-15 20:30:43 +0530847 goto err_cfg80211;
Kalle Valobdcd8172011-07-18 00:22:30 +0300848
849 sdio_claim_host(func);
850
851 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
852 if (ret) {
853 ath6kl_err("Set sdio block size %d failed: %d)\n",
854 HIF_MBOX_BLOCK_SIZE, ret);
855 sdio_release_host(func);
856 goto err_off;
857 }
858
859 sdio_release_host(func);
860
861 ret = ath6kl_core_init(ar);
862 if (ret) {
863 ath6kl_err("Failed to init ath6kl core\n");
864 goto err_off;
865 }
866
867 return ret;
868
869err_off:
870 ath6kl_sdio_power_off(ar_sdio);
Vivek Natarajan9df337a2011-09-15 20:30:43 +0530871err_cfg80211:
872 ath6kl_cfg80211_deinit(ar_sdio->ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300873err_dma:
874 kfree(ar_sdio->dma_buffer);
875err_hif:
876 kfree(ar_sdio);
877
878 return ret;
879}
880
881static void ath6kl_sdio_remove(struct sdio_func *func)
882{
883 struct ath6kl_sdio *ar_sdio;
884
Kalle Valof7325b82011-09-27 14:30:58 +0300885 ath6kl_dbg(ATH6KL_DBG_SDIO,
886 "removed func %d vendor 0x%x device 0x%x\n",
887 func->num, func->vendor, func->device);
888
Kalle Valobdcd8172011-07-18 00:22:30 +0300889 ar_sdio = sdio_get_drvdata(func);
890
891 ath6kl_stop_txrx(ar_sdio->ar);
892 cancel_work_sync(&ar_sdio->wr_async_work);
893
894 ath6kl_unavail_ev(ar_sdio->ar);
895
896 ath6kl_sdio_power_off(ar_sdio);
897
898 kfree(ar_sdio->dma_buffer);
899 kfree(ar_sdio);
900}
901
902static const struct sdio_device_id ath6kl_sdio_devices[] = {
903 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
904 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
905 {},
906};
907
908MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
909
910static struct sdio_driver ath6kl_sdio_driver = {
911 .name = "ath6kl_sdio",
912 .id_table = ath6kl_sdio_devices,
913 .probe = ath6kl_sdio_probe,
914 .remove = ath6kl_sdio_remove,
915};
916
917static int __init ath6kl_sdio_init(void)
918{
919 int ret;
920
921 ret = sdio_register_driver(&ath6kl_sdio_driver);
922 if (ret)
923 ath6kl_err("sdio driver registration failed: %d\n", ret);
924
925 return ret;
926}
927
928static void __exit ath6kl_sdio_exit(void)
929{
930 sdio_unregister_driver(&ath6kl_sdio_driver);
931}
932
933module_init(ath6kl_sdio_init);
934module_exit(ath6kl_sdio_exit);
935
936MODULE_AUTHOR("Atheros Communications, Inc.");
937MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
938MODULE_LICENSE("Dual BSD/GPL");
939
940MODULE_FIRMWARE(AR6003_REV2_OTP_FILE);
941MODULE_FIRMWARE(AR6003_REV2_FIRMWARE_FILE);
942MODULE_FIRMWARE(AR6003_REV2_PATCH_FILE);
943MODULE_FIRMWARE(AR6003_REV2_BOARD_DATA_FILE);
944MODULE_FIRMWARE(AR6003_REV2_DEFAULT_BOARD_DATA_FILE);
945MODULE_FIRMWARE(AR6003_REV3_OTP_FILE);
946MODULE_FIRMWARE(AR6003_REV3_FIRMWARE_FILE);
947MODULE_FIRMWARE(AR6003_REV3_PATCH_FILE);
948MODULE_FIRMWARE(AR6003_REV3_BOARD_DATA_FILE);
949MODULE_FIRMWARE(AR6003_REV3_DEFAULT_BOARD_DATA_FILE);