blob: da60738324bed4ac18d1aeabf719a2ddf1ccd695 [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"
28
29struct ath6kl_sdio {
30 struct sdio_func *func;
31
32 spinlock_t lock;
33
34 /* free list */
35 struct list_head bus_req_freeq;
36
37 /* available bus requests */
38 struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
39
40 struct ath6kl *ar;
41 u8 *dma_buffer;
42
43 /* scatter request list head */
44 struct list_head scat_req;
45
46 spinlock_t scat_lock;
47 bool is_disabled;
48 atomic_t irq_handling;
49 const struct sdio_device_id *id;
50 struct work_struct wr_async_work;
51 struct list_head wr_asyncq;
52 spinlock_t wr_async_lock;
53};
54
55#define CMD53_ARG_READ 0
56#define CMD53_ARG_WRITE 1
57#define CMD53_ARG_BLOCK_BASIS 1
58#define CMD53_ARG_FIXED_ADDRESS 0
59#define CMD53_ARG_INCR_ADDRESS 1
60
61static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
62{
63 return ar->hif_priv;
64}
65
66/*
67 * Macro to check if DMA buffer is WORD-aligned and DMA-able.
68 * Most host controllers assume the buffer is DMA'able and will
69 * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
70 * check fails on stack memory.
71 */
72static inline bool buf_needs_bounce(u8 *buf)
73{
74 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
75}
76
77static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
78{
79 struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
80
81 /* EP1 has an extended range */
82 mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
83 mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
84 mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
85 mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
86 mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
87 mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
88}
89
90static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
91 u8 mode, u8 opcode, u32 addr,
92 u16 blksz)
93{
94 *arg = (((rw & 1) << 31) |
95 ((func & 0x7) << 28) |
96 ((mode & 1) << 27) |
97 ((opcode & 1) << 26) |
98 ((addr & 0x1FFFF) << 9) |
99 (blksz & 0x1FF));
100}
101
102static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
103 unsigned int address,
104 unsigned char val)
105{
106 const u8 func = 0;
107
108 *arg = ((write & 1) << 31) |
109 ((func & 0x7) << 28) |
110 ((raw & 1) << 27) |
111 (1 << 26) |
112 ((address & 0x1FFFF) << 9) |
113 (1 << 8) |
114 (val & 0xFF);
115}
116
117static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
118 unsigned int address,
119 unsigned char byte)
120{
121 struct mmc_command io_cmd;
122
123 memset(&io_cmd, 0, sizeof(io_cmd));
124 ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
125 io_cmd.opcode = SD_IO_RW_DIRECT;
126 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
127
128 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
129}
130
131static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
132{
133 struct bus_request *bus_req;
134 unsigned long flag;
135
136 spin_lock_irqsave(&ar_sdio->lock, flag);
137
138 if (list_empty(&ar_sdio->bus_req_freeq)) {
139 spin_unlock_irqrestore(&ar_sdio->lock, flag);
140 return NULL;
141 }
142
143 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
144 struct bus_request, list);
145 list_del(&bus_req->list);
146
147 spin_unlock_irqrestore(&ar_sdio->lock, flag);
148 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: bus request 0x%p\n", __func__, bus_req);
149
150 return bus_req;
151}
152
153static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
154 struct bus_request *bus_req)
155{
156 unsigned long flag;
157
158 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: bus request 0x%p\n", __func__, bus_req);
159
160 spin_lock_irqsave(&ar_sdio->lock, flag);
161 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
162 spin_unlock_irqrestore(&ar_sdio->lock, flag);
163}
164
165static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
Kalle Valobdcd8172011-07-18 00:22:30 +0300166 struct mmc_data *data)
167{
168 struct scatterlist *sg;
169 int i;
170
171 data->blksz = HIF_MBOX_BLOCK_SIZE;
172 data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
173
174 ath6kl_dbg(ATH6KL_DBG_SCATTER,
175 "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
176 (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
177 data->blksz, data->blocks, scat_req->len,
178 scat_req->scat_entries);
179
180 data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
181 MMC_DATA_READ;
182
183 /* fill SG entries */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530184 sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300185 sg_init_table(sg, scat_req->scat_entries);
186
187 /* assemble SG list */
188 for (i = 0; i < scat_req->scat_entries; i++, sg++) {
189 if ((unsigned long)scat_req->scat_list[i].buf & 0x3)
190 /*
191 * Some scatter engines can handle unaligned
192 * buffers, print this as informational only.
193 */
194 ath6kl_dbg(ATH6KL_DBG_SCATTER,
195 "(%s) scatter buffer is unaligned 0x%p\n",
196 scat_req->req & HIF_WRITE ? "WR" : "RD",
197 scat_req->scat_list[i].buf);
198
199 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
200 i, scat_req->scat_list[i].buf,
201 scat_req->scat_list[i].len);
202
203 sg_set_buf(sg, scat_req->scat_list[i].buf,
204 scat_req->scat_list[i].len);
205 }
206
207 /* set scatter-gather table for request */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530208 data->sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300209 data->sg_len = scat_req->scat_entries;
210}
211
212static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
213 struct bus_request *req)
214{
215 struct mmc_request mmc_req;
216 struct mmc_command cmd;
217 struct mmc_data data;
218 struct hif_scatter_req *scat_req;
219 u8 opcode, rw;
220 int status;
221
222 scat_req = req->scat_req;
223
224 memset(&mmc_req, 0, sizeof(struct mmc_request));
225 memset(&cmd, 0, sizeof(struct mmc_command));
226 memset(&data, 0, sizeof(struct mmc_data));
227
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530228 ath6kl_sdio_setup_scat_data(scat_req, &data);
Kalle Valobdcd8172011-07-18 00:22:30 +0300229
230 opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
231 CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
232
233 rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
234
235 /* Fixup the address so that the last byte will fall on MBOX EOM */
236 if (scat_req->req & HIF_WRITE) {
237 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
238 scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
239 else
240 /* Uses extended address range */
241 scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
242 }
243
244 /* set command argument */
245 ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
246 CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
247 data.blocks);
248
249 cmd.opcode = SD_IO_RW_EXTENDED;
250 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
251
252 mmc_req.cmd = &cmd;
253 mmc_req.data = &data;
254
255 mmc_set_data_timeout(&data, ar_sdio->func->card);
256 /* synchronous call to process request */
257 mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
258
259 status = cmd.error ? cmd.error : data.error;
260 scat_req->status = status;
261
262 if (scat_req->status)
263 ath6kl_err("Scatter write request failed:%d\n",
264 scat_req->status);
265
266 if (scat_req->req & HIF_ASYNCHRONOUS)
267 scat_req->complete(scat_req);
268
269 return status;
270}
271
Kalle Valobdcd8172011-07-18 00:22:30 +0300272/* clean up scatter support */
273static void ath6kl_sdio_cleanup_scat_resource(struct ath6kl_sdio *ar_sdio)
274{
275 struct hif_scatter_req *s_req, *tmp_req;
276 unsigned long flag;
277
278 /* empty the free list */
279 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
280 list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
281 list_del(&s_req->list);
282 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
283
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530284 if (s_req->busrequest)
285 ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
Kalle Valobdcd8172011-07-18 00:22:30 +0300286 kfree(s_req->virt_dma_buf);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530287 kfree(s_req->sgentries);
Kalle Valobdcd8172011-07-18 00:22:30 +0300288 kfree(s_req);
289
290 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
291 }
292 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
293}
294
295/* setup of HIF scatter resources */
296static int ath6kl_sdio_setup_scat_resource(struct ath6kl_sdio *ar_sdio,
297 struct hif_dev_scat_sup_info *pinfo)
298{
299 struct hif_scatter_req *s_req;
300 struct bus_request *bus_req;
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530301 int i, scat_req_sz, scat_list_sz, sg_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +0300302
303 /* check if host supports scatter and it meets our requirements */
304 if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
305 ath6kl_err("hif-scatter: host only supports scatter of : %d entries, need: %d\n",
306 ar_sdio->func->card->host->max_segs,
307 MAX_SCATTER_ENTRIES_PER_REQ);
308 return -EINVAL;
309 }
310
311 ath6kl_dbg(ATH6KL_DBG_ANY,
312 "hif-scatter enabled: max scatter req : %d entries: %d\n",
313 MAX_SCATTER_REQUESTS, MAX_SCATTER_ENTRIES_PER_REQ);
314
315 scat_list_sz = (MAX_SCATTER_ENTRIES_PER_REQ - 1) *
316 sizeof(struct hif_scatter_item);
317 scat_req_sz = sizeof(*s_req) + scat_list_sz;
318
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530319 sg_sz = sizeof(struct scatterlist) * MAX_SCATTER_ENTRIES_PER_REQ;
320
Kalle Valobdcd8172011-07-18 00:22:30 +0300321 for (i = 0; i < MAX_SCATTER_REQUESTS; i++) {
322 /* allocate the scatter request */
323 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
324 if (!s_req)
325 goto fail_setup_scat;
326
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530327 /* allocate sglist */
328 s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
Kalle Valobdcd8172011-07-18 00:22:30 +0300329
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530330 if (!s_req->sgentries) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300331 kfree(s_req);
332 goto fail_setup_scat;
333 }
334
335 /* allocate a bus request for this scatter request */
336 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
337 if (!bus_req) {
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530338 kfree(s_req->sgentries);
Kalle Valobdcd8172011-07-18 00:22:30 +0300339 kfree(s_req);
340 goto fail_setup_scat;
341 }
342
343 /* assign the scatter request to this bus request */
344 bus_req->scat_req = s_req;
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530345 s_req->busrequest = bus_req;
Kalle Valobdcd8172011-07-18 00:22:30 +0300346 /* add it to the scatter pool */
347 hif_scatter_req_add(ar_sdio->ar, s_req);
348 }
349
Kalle Valobdcd8172011-07-18 00:22:30 +0300350 pinfo->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
351 pinfo->max_xfer_szper_scatreq = MAX_SCATTER_REQ_TRANSFER_SIZE;
352
353 return 0;
354
355fail_setup_scat:
356 ath6kl_err("hif-scatter: failed to alloc scatter resources !\n");
357 ath6kl_sdio_cleanup_scat_resource(ar_sdio);
358
359 return -ENOMEM;
360}
361
362static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
363 u32 len, u32 request)
364{
365 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
366 u8 *tbuf = NULL;
367 int ret;
368 bool bounced = false;
369
370 if (request & HIF_BLOCK_BASIS)
371 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
372
373 if (buf_needs_bounce(buf)) {
374 if (!ar_sdio->dma_buffer)
375 return -ENOMEM;
376 tbuf = ar_sdio->dma_buffer;
377 memcpy(tbuf, buf, len);
378 bounced = true;
379 } else
380 tbuf = buf;
381
382 sdio_claim_host(ar_sdio->func);
383 if (request & HIF_WRITE) {
384 if (addr >= HIF_MBOX_BASE_ADDR &&
385 addr <= HIF_MBOX_END_ADDR)
386 addr += (HIF_MBOX_WIDTH - len);
387
388 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
389 addr += HIF_MBOX0_EXT_WIDTH - len;
390
391 if (request & HIF_FIXED_ADDRESS)
392 ret = sdio_writesb(ar_sdio->func, addr, tbuf, len);
393 else
394 ret = sdio_memcpy_toio(ar_sdio->func, addr, tbuf, len);
395 } else {
396 if (request & HIF_FIXED_ADDRESS)
397 ret = sdio_readsb(ar_sdio->func, tbuf, addr, len);
398 else
399 ret = sdio_memcpy_fromio(ar_sdio->func, tbuf,
400 addr, len);
401 if (bounced)
402 memcpy(buf, tbuf, len);
403 }
404 sdio_release_host(ar_sdio->func);
405
406 return ret;
407}
408
409static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
410 struct bus_request *req)
411{
412 if (req->scat_req)
413 ath6kl_sdio_scat_rw(ar_sdio, req);
414 else {
415 void *context;
416 int status;
417
418 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
419 req->buffer, req->length,
420 req->request);
421 context = req->packet;
422 ath6kl_sdio_free_bus_req(ar_sdio, req);
423 ath6kldev_rw_comp_handler(context, status);
424 }
425}
426
427static void ath6kl_sdio_write_async_work(struct work_struct *work)
428{
429 struct ath6kl_sdio *ar_sdio;
430 unsigned long flags;
431 struct bus_request *req, *tmp_req;
432
433 ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
434 sdio_claim_host(ar_sdio->func);
435
436 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
437 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
438 list_del(&req->list);
439 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
440 __ath6kl_sdio_write_async(ar_sdio, req);
441 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
442 }
443 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
444
445 sdio_release_host(ar_sdio->func);
446}
447
448static void ath6kl_sdio_irq_handler(struct sdio_func *func)
449{
450 int status;
451 struct ath6kl_sdio *ar_sdio;
452
453 ar_sdio = sdio_get_drvdata(func);
454 atomic_set(&ar_sdio->irq_handling, 1);
455
456 /*
457 * Release the host during interrups so we can pick it back up when
458 * we process commands.
459 */
460 sdio_release_host(ar_sdio->func);
461
462 status = ath6kldev_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);
466}
467
468static int ath6kl_sdio_power_on(struct ath6kl_sdio *ar_sdio)
469{
470 struct sdio_func *func = ar_sdio->func;
471 int ret = 0;
472
473 if (!ar_sdio->is_disabled)
474 return 0;
475
476 sdio_claim_host(func);
477
478 ret = sdio_enable_func(func);
479 if (ret) {
480 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
481 sdio_release_host(func);
482 return ret;
483 }
484
485 sdio_release_host(func);
486
487 /*
488 * Wait for hardware to initialise. It should take a lot less than
489 * 10 ms but let's be conservative here.
490 */
491 msleep(10);
492
493 ar_sdio->is_disabled = false;
494
495 return ret;
496}
497
498static int ath6kl_sdio_power_off(struct ath6kl_sdio *ar_sdio)
499{
500 int ret;
501
502 if (ar_sdio->is_disabled)
503 return 0;
504
505 /* Disable the card */
506 sdio_claim_host(ar_sdio->func);
507 ret = sdio_disable_func(ar_sdio->func);
508 sdio_release_host(ar_sdio->func);
509
510 if (ret)
511 return ret;
512
513 ar_sdio->is_disabled = true;
514
515 return ret;
516}
517
518static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
519 u32 length, u32 request,
520 struct htc_packet *packet)
521{
522 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
523 struct bus_request *bus_req;
524 unsigned long flags;
525
526 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
527
528 if (!bus_req)
529 return -ENOMEM;
530
531 bus_req->address = address;
532 bus_req->buffer = buffer;
533 bus_req->length = length;
534 bus_req->request = request;
535 bus_req->packet = packet;
536
537 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
538 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
539 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
540 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
541
542 return 0;
543}
544
545static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
546{
547 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
548 int ret;
549
550 sdio_claim_host(ar_sdio->func);
551
552 /* Register the isr */
553 ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
554 if (ret)
555 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
556
557 sdio_release_host(ar_sdio->func);
558}
559
560static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
561{
562 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
563 int ret;
564
565 sdio_claim_host(ar_sdio->func);
566
567 /* Mask our function IRQ */
568 while (atomic_read(&ar_sdio->irq_handling)) {
569 sdio_release_host(ar_sdio->func);
570 schedule_timeout(HZ / 10);
571 sdio_claim_host(ar_sdio->func);
572 }
573
574 ret = sdio_release_irq(ar_sdio->func);
575 if (ret)
576 ath6kl_err("Failed to release sdio irq: %d\n", ret);
577
578 sdio_release_host(ar_sdio->func);
579}
580
581static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
582{
583 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
584 struct hif_scatter_req *node = NULL;
585 unsigned long flag;
586
587 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
588
589 if (!list_empty(&ar_sdio->scat_req)) {
590 node = list_first_entry(&ar_sdio->scat_req,
591 struct hif_scatter_req, list);
592 list_del(&node->list);
593 }
594
595 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
596
597 return node;
598}
599
600static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
601 struct hif_scatter_req *s_req)
602{
603 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
604 unsigned long flag;
605
606 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
607
608 list_add_tail(&s_req->list, &ar_sdio->scat_req);
609
610 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
611
612}
613
614static int ath6kl_sdio_enable_scatter(struct ath6kl *ar,
615 struct hif_dev_scat_sup_info *info)
616{
617 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
618 int ret;
619
620 ret = ath6kl_sdio_setup_scat_resource(ar_sdio, info);
621
622 return ret;
623}
624
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530625/* scatter gather read write request */
626static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
627 struct hif_scatter_req *scat_req)
628{
629 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530630 u32 request = scat_req->req;
631 int status = 0;
632 unsigned long flags;
633
634 if (!scat_req->len)
635 return -EINVAL;
636
637 ath6kl_dbg(ATH6KL_DBG_SCATTER,
638 "hif-scatter: total len: %d scatter entries: %d\n",
639 scat_req->len, scat_req->scat_entries);
640
641 if (request & HIF_SYNCHRONOUS) {
642 sdio_claim_host(ar_sdio->func);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530643 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530644 sdio_release_host(ar_sdio->func);
645 } else {
646 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530647 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530648 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
649 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
650 }
651
652 return status;
653}
654
Kalle Valobdcd8172011-07-18 00:22:30 +0300655static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
656{
657 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
658
659 ath6kl_sdio_cleanup_scat_resource(ar_sdio);
660}
661
662static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
663 .read_write_sync = ath6kl_sdio_read_write_sync,
664 .write_async = ath6kl_sdio_write_async,
665 .irq_enable = ath6kl_sdio_irq_enable,
666 .irq_disable = ath6kl_sdio_irq_disable,
667 .scatter_req_get = ath6kl_sdio_scatter_req_get,
668 .scatter_req_add = ath6kl_sdio_scatter_req_add,
669 .enable_scatter = ath6kl_sdio_enable_scatter,
Vasanthakumar Thiagarajanf74a7362011-07-16 20:29:05 +0530670 .scat_req_rw = ath6kl_sdio_async_rw_scatter,
Kalle Valobdcd8172011-07-18 00:22:30 +0300671 .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
672};
673
674static int ath6kl_sdio_probe(struct sdio_func *func,
675 const struct sdio_device_id *id)
676{
677 int ret;
678 struct ath6kl_sdio *ar_sdio;
679 struct ath6kl *ar;
680 int count;
681
682 ath6kl_dbg(ATH6KL_DBG_TRC,
683 "%s: func: 0x%X, vendor id: 0x%X, dev id: 0x%X, block size: 0x%X/0x%X\n",
684 __func__, func->num, func->vendor,
685 func->device, func->max_blksize, func->cur_blksize);
686
687 ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
688 if (!ar_sdio)
689 return -ENOMEM;
690
691 ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
692 if (!ar_sdio->dma_buffer) {
693 ret = -ENOMEM;
694 goto err_hif;
695 }
696
697 ar_sdio->func = func;
698 sdio_set_drvdata(func, ar_sdio);
699
700 ar_sdio->id = id;
701 ar_sdio->is_disabled = true;
702
703 spin_lock_init(&ar_sdio->lock);
704 spin_lock_init(&ar_sdio->scat_lock);
705 spin_lock_init(&ar_sdio->wr_async_lock);
706
707 INIT_LIST_HEAD(&ar_sdio->scat_req);
708 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
709 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
710
711 INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
712
713 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
714 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
715
716 ar = ath6kl_core_alloc(&ar_sdio->func->dev);
717 if (!ar) {
718 ath6kl_err("Failed to alloc ath6kl core\n");
719 ret = -ENOMEM;
720 goto err_dma;
721 }
722
723 ar_sdio->ar = ar;
724 ar->hif_priv = ar_sdio;
725 ar->hif_ops = &ath6kl_sdio_ops;
726
727 ath6kl_sdio_set_mbox_info(ar);
728
729 sdio_claim_host(func);
730
731 if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
732 MANUFACTURER_ID_AR6003_BASE) {
733 /* enable 4-bit ASYNC interrupt on AR6003 or later */
734 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
735 CCCR_SDIO_IRQ_MODE_REG,
736 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
737 if (ret) {
738 ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
739 ret);
740 sdio_release_host(func);
741 goto err_dma;
742 }
743
744 ath6kl_dbg(ATH6KL_DBG_TRC, "4-bit async irq mode enabled\n");
745 }
746
747 /* give us some time to enable, in ms */
748 func->enable_timeout = 100;
749
750 sdio_release_host(func);
751
752 ret = ath6kl_sdio_power_on(ar_sdio);
753 if (ret)
754 goto err_dma;
755
756 sdio_claim_host(func);
757
758 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
759 if (ret) {
760 ath6kl_err("Set sdio block size %d failed: %d)\n",
761 HIF_MBOX_BLOCK_SIZE, ret);
762 sdio_release_host(func);
763 goto err_off;
764 }
765
766 sdio_release_host(func);
767
768 ret = ath6kl_core_init(ar);
769 if (ret) {
770 ath6kl_err("Failed to init ath6kl core\n");
771 goto err_off;
772 }
773
774 return ret;
775
776err_off:
777 ath6kl_sdio_power_off(ar_sdio);
778err_dma:
779 kfree(ar_sdio->dma_buffer);
780err_hif:
781 kfree(ar_sdio);
782
783 return ret;
784}
785
786static void ath6kl_sdio_remove(struct sdio_func *func)
787{
788 struct ath6kl_sdio *ar_sdio;
789
790 ar_sdio = sdio_get_drvdata(func);
791
792 ath6kl_stop_txrx(ar_sdio->ar);
793 cancel_work_sync(&ar_sdio->wr_async_work);
794
795 ath6kl_unavail_ev(ar_sdio->ar);
796
797 ath6kl_sdio_power_off(ar_sdio);
798
799 kfree(ar_sdio->dma_buffer);
800 kfree(ar_sdio);
801}
802
803static const struct sdio_device_id ath6kl_sdio_devices[] = {
804 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
805 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
806 {},
807};
808
809MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
810
811static struct sdio_driver ath6kl_sdio_driver = {
812 .name = "ath6kl_sdio",
813 .id_table = ath6kl_sdio_devices,
814 .probe = ath6kl_sdio_probe,
815 .remove = ath6kl_sdio_remove,
816};
817
818static int __init ath6kl_sdio_init(void)
819{
820 int ret;
821
822 ret = sdio_register_driver(&ath6kl_sdio_driver);
823 if (ret)
824 ath6kl_err("sdio driver registration failed: %d\n", ret);
825
826 return ret;
827}
828
829static void __exit ath6kl_sdio_exit(void)
830{
831 sdio_unregister_driver(&ath6kl_sdio_driver);
832}
833
834module_init(ath6kl_sdio_init);
835module_exit(ath6kl_sdio_exit);
836
837MODULE_AUTHOR("Atheros Communications, Inc.");
838MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
839MODULE_LICENSE("Dual BSD/GPL");
840
841MODULE_FIRMWARE(AR6003_REV2_OTP_FILE);
842MODULE_FIRMWARE(AR6003_REV2_FIRMWARE_FILE);
843MODULE_FIRMWARE(AR6003_REV2_PATCH_FILE);
844MODULE_FIRMWARE(AR6003_REV2_BOARD_DATA_FILE);
845MODULE_FIRMWARE(AR6003_REV2_DEFAULT_BOARD_DATA_FILE);
846MODULE_FIRMWARE(AR6003_REV3_OTP_FILE);
847MODULE_FIRMWARE(AR6003_REV3_FIRMWARE_FILE);
848MODULE_FIRMWARE(AR6003_REV3_PATCH_FILE);
849MODULE_FIRMWARE(AR6003_REV3_BOARD_DATA_FILE);
850MODULE_FIRMWARE(AR6003_REV3_DEFAULT_BOARD_DATA_FILE);