blob: eecf88c43d0c5771ab905a50105b8501e6ea9f81 [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>
Kalle Valo2e1cb232011-10-05 12:23:49 +030024#include "hif.h"
Kalle Valobdcd8172011-07-18 00:22:30 +030025#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;
Raja Manifdb28582011-11-21 12:26:51 +053042
Kalle Valobdcd8172011-07-18 00:22:30 +030043 u8 *dma_buffer;
44
Raja Manifdb28582011-11-21 12:26:51 +053045 /* protects access to dma_buffer */
46 struct mutex dma_buffer_mutex;
47
Kalle Valobdcd8172011-07-18 00:22:30 +030048 /* scatter request list head */
49 struct list_head scat_req;
50
51 spinlock_t scat_lock;
Kalle Valo32a07e42011-10-30 21:15:57 +020052 bool scatter_enabled;
53
Kalle Valobdcd8172011-07-18 00:22:30 +030054 bool is_disabled;
55 atomic_t irq_handling;
56 const struct sdio_device_id *id;
57 struct work_struct wr_async_work;
58 struct list_head wr_asyncq;
59 spinlock_t wr_async_lock;
60};
61
62#define CMD53_ARG_READ 0
63#define CMD53_ARG_WRITE 1
64#define CMD53_ARG_BLOCK_BASIS 1
65#define CMD53_ARG_FIXED_ADDRESS 0
66#define CMD53_ARG_INCR_ADDRESS 1
67
68static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
69{
70 return ar->hif_priv;
71}
72
73/*
74 * Macro to check if DMA buffer is WORD-aligned and DMA-able.
75 * Most host controllers assume the buffer is DMA'able and will
76 * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
77 * check fails on stack memory.
78 */
79static inline bool buf_needs_bounce(u8 *buf)
80{
81 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
82}
83
84static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
85{
86 struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
87
88 /* EP1 has an extended range */
89 mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
90 mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
91 mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
92 mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
93 mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
94 mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
95}
96
97static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
98 u8 mode, u8 opcode, u32 addr,
99 u16 blksz)
100{
101 *arg = (((rw & 1) << 31) |
102 ((func & 0x7) << 28) |
103 ((mode & 1) << 27) |
104 ((opcode & 1) << 26) |
105 ((addr & 0x1FFFF) << 9) |
106 (blksz & 0x1FF));
107}
108
109static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
110 unsigned int address,
111 unsigned char val)
112{
113 const u8 func = 0;
114
115 *arg = ((write & 1) << 31) |
116 ((func & 0x7) << 28) |
117 ((raw & 1) << 27) |
118 (1 << 26) |
119 ((address & 0x1FFFF) << 9) |
120 (1 << 8) |
121 (val & 0xFF);
122}
123
124static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
125 unsigned int address,
126 unsigned char byte)
127{
128 struct mmc_command io_cmd;
129
130 memset(&io_cmd, 0, sizeof(io_cmd));
131 ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
132 io_cmd.opcode = SD_IO_RW_DIRECT;
133 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
134
135 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
136}
137
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530138static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
139 u8 *buf, u32 len)
140{
141 int ret = 0;
142
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530143 sdio_claim_host(func);
144
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530145 if (request & HIF_WRITE) {
Kalle Valof7325b82011-09-27 14:30:58 +0300146 /* FIXME: looks like ugly workaround for something */
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530147 if (addr >= HIF_MBOX_BASE_ADDR &&
148 addr <= HIF_MBOX_END_ADDR)
149 addr += (HIF_MBOX_WIDTH - len);
150
Kalle Valof7325b82011-09-27 14:30:58 +0300151 /* FIXME: this also looks like ugly workaround */
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530152 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
153 addr += HIF_MBOX0_EXT_WIDTH - len;
154
155 if (request & HIF_FIXED_ADDRESS)
156 ret = sdio_writesb(func, addr, buf, len);
157 else
158 ret = sdio_memcpy_toio(func, addr, buf, len);
159 } else {
160 if (request & HIF_FIXED_ADDRESS)
161 ret = sdio_readsb(func, buf, addr, len);
162 else
163 ret = sdio_memcpy_fromio(func, buf, addr, len);
164 }
165
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530166 sdio_release_host(func);
167
Kalle Valof7325b82011-09-27 14:30:58 +0300168 ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
169 request & HIF_WRITE ? "wr" : "rd", addr,
170 request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
171 ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
172
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530173 return ret;
174}
175
Kalle Valobdcd8172011-07-18 00:22:30 +0300176static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
177{
178 struct bus_request *bus_req;
Kalle Valobdcd8172011-07-18 00:22:30 +0300179
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530180 spin_lock_bh(&ar_sdio->lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300181
182 if (list_empty(&ar_sdio->bus_req_freeq)) {
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530183 spin_unlock_bh(&ar_sdio->lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300184 return NULL;
185 }
186
187 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
188 struct bus_request, list);
189 list_del(&bus_req->list);
190
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530191 spin_unlock_bh(&ar_sdio->lock);
Kalle Valof7325b82011-09-27 14:30:58 +0300192 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
193 __func__, bus_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300194
195 return bus_req;
196}
197
198static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
199 struct bus_request *bus_req)
200{
Kalle Valof7325b82011-09-27 14:30:58 +0300201 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
202 __func__, bus_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300203
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530204 spin_lock_bh(&ar_sdio->lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300205 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530206 spin_unlock_bh(&ar_sdio->lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300207}
208
209static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
Kalle Valobdcd8172011-07-18 00:22:30 +0300210 struct mmc_data *data)
211{
212 struct scatterlist *sg;
213 int i;
214
215 data->blksz = HIF_MBOX_BLOCK_SIZE;
216 data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
217
218 ath6kl_dbg(ATH6KL_DBG_SCATTER,
219 "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
220 (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
221 data->blksz, data->blocks, scat_req->len,
222 scat_req->scat_entries);
223
224 data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
225 MMC_DATA_READ;
226
227 /* fill SG entries */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530228 sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300229 sg_init_table(sg, scat_req->scat_entries);
230
231 /* assemble SG list */
232 for (i = 0; i < scat_req->scat_entries; i++, sg++) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300233 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
234 i, scat_req->scat_list[i].buf,
235 scat_req->scat_list[i].len);
236
237 sg_set_buf(sg, scat_req->scat_list[i].buf,
238 scat_req->scat_list[i].len);
239 }
240
241 /* set scatter-gather table for request */
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530242 data->sg = scat_req->sgentries;
Kalle Valobdcd8172011-07-18 00:22:30 +0300243 data->sg_len = scat_req->scat_entries;
244}
245
246static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
247 struct bus_request *req)
248{
249 struct mmc_request mmc_req;
250 struct mmc_command cmd;
251 struct mmc_data data;
252 struct hif_scatter_req *scat_req;
253 u8 opcode, rw;
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530254 int status, len;
Kalle Valobdcd8172011-07-18 00:22:30 +0300255
256 scat_req = req->scat_req;
257
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530258 if (scat_req->virt_scat) {
259 len = scat_req->len;
260 if (scat_req->req & HIF_BLOCK_BASIS)
261 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
262
263 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
264 scat_req->addr, scat_req->virt_dma_buf,
265 len);
266 goto scat_complete;
267 }
268
Kalle Valobdcd8172011-07-18 00:22:30 +0300269 memset(&mmc_req, 0, sizeof(struct mmc_request));
270 memset(&cmd, 0, sizeof(struct mmc_command));
271 memset(&data, 0, sizeof(struct mmc_data));
272
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530273 ath6kl_sdio_setup_scat_data(scat_req, &data);
Kalle Valobdcd8172011-07-18 00:22:30 +0300274
275 opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
276 CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
277
278 rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
279
280 /* Fixup the address so that the last byte will fall on MBOX EOM */
281 if (scat_req->req & HIF_WRITE) {
282 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
283 scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
284 else
285 /* Uses extended address range */
286 scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
287 }
288
289 /* set command argument */
290 ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
291 CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
292 data.blocks);
293
294 cmd.opcode = SD_IO_RW_EXTENDED;
295 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
296
297 mmc_req.cmd = &cmd;
298 mmc_req.data = &data;
299
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530300 sdio_claim_host(ar_sdio->func);
301
Kalle Valobdcd8172011-07-18 00:22:30 +0300302 mmc_set_data_timeout(&data, ar_sdio->func->card);
303 /* synchronous call to process request */
304 mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
305
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530306 sdio_release_host(ar_sdio->func);
307
Kalle Valobdcd8172011-07-18 00:22:30 +0300308 status = cmd.error ? cmd.error : data.error;
Vasanthakumar Thiagarajan348a8fb2011-07-16 20:29:17 +0530309
310scat_complete:
Kalle Valobdcd8172011-07-18 00:22:30 +0300311 scat_req->status = status;
312
313 if (scat_req->status)
314 ath6kl_err("Scatter write request failed:%d\n",
315 scat_req->status);
316
317 if (scat_req->req & HIF_ASYNCHRONOUS)
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530318 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
Kalle Valobdcd8172011-07-18 00:22:30 +0300319
320 return status;
321}
322
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530323static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
324 int n_scat_entry, int n_scat_req,
325 bool virt_scat)
326{
327 struct hif_scatter_req *s_req;
328 struct bus_request *bus_req;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530329 int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
330 u8 *virt_buf;
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530331
332 scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
333 scat_req_sz = sizeof(*s_req) + scat_list_sz;
334
335 if (!virt_scat)
336 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530337 else
338 buf_sz = 2 * L1_CACHE_BYTES +
339 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530340
341 for (i = 0; i < n_scat_req; i++) {
342 /* allocate the scatter request */
343 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
344 if (!s_req)
345 return -ENOMEM;
346
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530347 if (virt_scat) {
348 virt_buf = kzalloc(buf_sz, GFP_KERNEL);
349 if (!virt_buf) {
350 kfree(s_req);
351 return -ENOMEM;
352 }
353
354 s_req->virt_dma_buf =
355 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
356 } else {
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530357 /* allocate sglist */
358 s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
359
360 if (!s_req->sgentries) {
361 kfree(s_req);
362 return -ENOMEM;
363 }
364 }
365
366 /* allocate a bus request for this scatter request */
367 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
368 if (!bus_req) {
369 kfree(s_req->sgentries);
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530370 kfree(s_req->virt_dma_buf);
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530371 kfree(s_req);
372 return -ENOMEM;
373 }
374
375 /* assign the scatter request to this bus request */
376 bus_req->scat_req = s_req;
377 s_req->busrequest = bus_req;
378
Vasanthakumar Thiagarajan4a005c32011-07-16 20:29:15 +0530379 s_req->virt_scat = virt_scat;
380
Vasanthakumar Thiagarajan3df505a2011-07-16 20:29:10 +0530381 /* add it to the scatter pool */
382 hif_scatter_req_add(ar_sdio->ar, s_req);
383 }
384
385 return 0;
386}
387
Kalle Valobdcd8172011-07-18 00:22:30 +0300388static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
389 u32 len, u32 request)
390{
391 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
392 u8 *tbuf = NULL;
393 int ret;
394 bool bounced = false;
395
396 if (request & HIF_BLOCK_BASIS)
397 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
398
399 if (buf_needs_bounce(buf)) {
400 if (!ar_sdio->dma_buffer)
401 return -ENOMEM;
Raja Manifdb28582011-11-21 12:26:51 +0530402 mutex_lock(&ar_sdio->dma_buffer_mutex);
Kalle Valobdcd8172011-07-18 00:22:30 +0300403 tbuf = ar_sdio->dma_buffer;
404 memcpy(tbuf, buf, len);
405 bounced = true;
406 } else
407 tbuf = buf;
408
Vasanthakumar Thiagarajanda220692011-07-16 20:29:16 +0530409 ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
410 if ((request & HIF_READ) && bounced)
411 memcpy(buf, tbuf, len);
Kalle Valobdcd8172011-07-18 00:22:30 +0300412
Raja Manifdb28582011-11-21 12:26:51 +0530413 if (bounced)
414 mutex_unlock(&ar_sdio->dma_buffer_mutex);
415
Kalle Valobdcd8172011-07-18 00:22:30 +0300416 return ret;
417}
418
419static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
420 struct bus_request *req)
421{
422 if (req->scat_req)
423 ath6kl_sdio_scat_rw(ar_sdio, req);
424 else {
425 void *context;
426 int status;
427
428 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
429 req->buffer, req->length,
430 req->request);
431 context = req->packet;
432 ath6kl_sdio_free_bus_req(ar_sdio, req);
Kalle Valo8e8ddb22011-10-05 12:23:33 +0300433 ath6kl_hif_rw_comp_handler(context, status);
Kalle Valobdcd8172011-07-18 00:22:30 +0300434 }
435}
436
437static void ath6kl_sdio_write_async_work(struct work_struct *work)
438{
439 struct ath6kl_sdio *ar_sdio;
Kalle Valobdcd8172011-07-18 00:22:30 +0300440 struct bus_request *req, *tmp_req;
441
442 ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
Kalle Valobdcd8172011-07-18 00:22:30 +0300443
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530444 spin_lock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300445 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
446 list_del(&req->list);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530447 spin_unlock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300448 __ath6kl_sdio_write_async(ar_sdio, req);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530449 spin_lock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300450 }
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530451 spin_unlock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300452}
453
454static void ath6kl_sdio_irq_handler(struct sdio_func *func)
455{
456 int status;
457 struct ath6kl_sdio *ar_sdio;
458
Kalle Valof7325b82011-09-27 14:30:58 +0300459 ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
460
Kalle Valobdcd8172011-07-18 00:22:30 +0300461 ar_sdio = sdio_get_drvdata(func);
462 atomic_set(&ar_sdio->irq_handling, 1);
463
464 /*
465 * Release the host during interrups so we can pick it back up when
466 * we process commands.
467 */
468 sdio_release_host(ar_sdio->func);
469
Kalle Valo8e8ddb22011-10-05 12:23:33 +0300470 status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300471 sdio_claim_host(ar_sdio->func);
472 atomic_set(&ar_sdio->irq_handling, 0);
473 WARN_ON(status && status != -ECANCELED);
474}
475
Kalle Valob2e75692011-10-27 18:48:14 +0300476static int ath6kl_sdio_power_on(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300477{
Kalle Valob2e75692011-10-27 18:48:14 +0300478 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300479 struct sdio_func *func = ar_sdio->func;
480 int ret = 0;
481
482 if (!ar_sdio->is_disabled)
483 return 0;
484
Kalle Valo3ef987b2011-10-24 12:18:07 +0300485 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
486
Kalle Valobdcd8172011-07-18 00:22:30 +0300487 sdio_claim_host(func);
488
489 ret = sdio_enable_func(func);
490 if (ret) {
491 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
492 sdio_release_host(func);
493 return ret;
494 }
495
496 sdio_release_host(func);
497
498 /*
499 * Wait for hardware to initialise. It should take a lot less than
500 * 10 ms but let's be conservative here.
501 */
502 msleep(10);
503
504 ar_sdio->is_disabled = false;
505
506 return ret;
507}
508
Kalle Valob2e75692011-10-27 18:48:14 +0300509static int ath6kl_sdio_power_off(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +0300510{
Kalle Valob2e75692011-10-27 18:48:14 +0300511 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300512 int ret;
513
514 if (ar_sdio->is_disabled)
515 return 0;
516
Kalle Valo3ef987b2011-10-24 12:18:07 +0300517 ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
518
Kalle Valobdcd8172011-07-18 00:22:30 +0300519 /* Disable the card */
520 sdio_claim_host(ar_sdio->func);
521 ret = sdio_disable_func(ar_sdio->func);
522 sdio_release_host(ar_sdio->func);
523
524 if (ret)
525 return ret;
526
527 ar_sdio->is_disabled = true;
528
529 return ret;
530}
531
532static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
533 u32 length, u32 request,
534 struct htc_packet *packet)
535{
536 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
537 struct bus_request *bus_req;
Kalle Valobdcd8172011-07-18 00:22:30 +0300538
539 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
540
541 if (!bus_req)
542 return -ENOMEM;
543
544 bus_req->address = address;
545 bus_req->buffer = buffer;
546 bus_req->length = length;
547 bus_req->request = request;
548 bus_req->packet = packet;
549
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530550 spin_lock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300551 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530552 spin_unlock_bh(&ar_sdio->wr_async_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300553 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
554
555 return 0;
556}
557
558static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
559{
560 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
561 int ret;
562
563 sdio_claim_host(ar_sdio->func);
564
565 /* Register the isr */
566 ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
567 if (ret)
568 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
569
570 sdio_release_host(ar_sdio->func);
571}
572
573static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
574{
575 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
576 int ret;
577
578 sdio_claim_host(ar_sdio->func);
579
580 /* Mask our function IRQ */
581 while (atomic_read(&ar_sdio->irq_handling)) {
582 sdio_release_host(ar_sdio->func);
583 schedule_timeout(HZ / 10);
584 sdio_claim_host(ar_sdio->func);
585 }
586
587 ret = sdio_release_irq(ar_sdio->func);
588 if (ret)
589 ath6kl_err("Failed to release sdio irq: %d\n", ret);
590
591 sdio_release_host(ar_sdio->func);
592}
593
594static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
595{
596 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
597 struct hif_scatter_req *node = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +0300598
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530599 spin_lock_bh(&ar_sdio->scat_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300600
601 if (!list_empty(&ar_sdio->scat_req)) {
602 node = list_first_entry(&ar_sdio->scat_req,
603 struct hif_scatter_req, list);
604 list_del(&node->list);
605 }
606
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530607 spin_unlock_bh(&ar_sdio->scat_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300608
609 return node;
610}
611
612static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
613 struct hif_scatter_req *s_req)
614{
615 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +0300616
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530617 spin_lock_bh(&ar_sdio->scat_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300618
619 list_add_tail(&s_req->list, &ar_sdio->scat_req);
620
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530621 spin_unlock_bh(&ar_sdio->scat_lock);
Kalle Valobdcd8172011-07-18 00:22:30 +0300622
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;
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530632
633 if (!scat_req->len)
634 return -EINVAL;
635
636 ath6kl_dbg(ATH6KL_DBG_SCATTER,
637 "hif-scatter: total len: %d scatter entries: %d\n",
638 scat_req->len, scat_req->scat_entries);
639
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530640 if (request & HIF_SYNCHRONOUS)
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530641 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
Vasanthakumar Thiagarajan861dd052011-09-30 21:46:59 +0530642 else {
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530643 spin_lock_bh(&ar_sdio->wr_async_lock);
Vasanthakumar Thiagarajand4df7892011-07-16 20:29:07 +0530644 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530645 spin_unlock_bh(&ar_sdio->wr_async_lock);
Vasanthakumar Thiagarajanc630d182011-07-16 20:29:06 +0530646 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
647 }
648
649 return status;
650}
651
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530652/* clean up scatter support */
653static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
654{
655 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
656 struct hif_scatter_req *s_req, *tmp_req;
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530657
658 /* empty the free list */
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530659 spin_lock_bh(&ar_sdio->scat_lock);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530660 list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
661 list_del(&s_req->list);
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530662 spin_unlock_bh(&ar_sdio->scat_lock);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530663
Kalle Valo32a07e42011-10-30 21:15:57 +0200664 /*
665 * FIXME: should we also call completion handler with
666 * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
667 * that the packet is properly freed?
668 */
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530669 if (s_req->busrequest)
670 ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
671 kfree(s_req->virt_dma_buf);
672 kfree(s_req->sgentries);
673 kfree(s_req);
674
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530675 spin_lock_bh(&ar_sdio->scat_lock);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530676 }
Vasanthakumar Thiagarajan151bd302011-09-30 19:18:43 +0530677 spin_unlock_bh(&ar_sdio->scat_lock);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530678}
679
680/* setup of HIF scatter resources */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530681static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530682{
683 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530684 struct htc_target *target = ar->htc_target;
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530685 int ret;
686 bool virt_scat = false;
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530687
Kalle Valo32a07e42011-10-30 21:15:57 +0200688 if (ar_sdio->scatter_enabled)
689 return 0;
690
691 ar_sdio->scatter_enabled = true;
692
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530693 /* check if host supports scatter and it meets our requirements */
694 if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530695 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530696 ar_sdio->func->card->host->max_segs,
697 MAX_SCATTER_ENTRIES_PER_REQ);
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530698 virt_scat = true;
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530699 }
700
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530701 if (!virt_scat) {
702 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
703 MAX_SCATTER_ENTRIES_PER_REQ,
704 MAX_SCATTER_REQUESTS, virt_scat);
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530705
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530706 if (!ret) {
Kalle Valo3ef987b2011-10-24 12:18:07 +0300707 ath6kl_dbg(ATH6KL_DBG_BOOT,
708 "hif-scatter enabled requests %d entries %d\n",
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530709 MAX_SCATTER_REQUESTS,
710 MAX_SCATTER_ENTRIES_PER_REQ);
711
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530712 target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
713 target->max_xfer_szper_scatreq =
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530714 MAX_SCATTER_REQ_TRANSFER_SIZE;
715 } else {
716 ath6kl_sdio_cleanup_scatter(ar);
717 ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
718 }
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530719 }
720
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530721 if (virt_scat || ret) {
722 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
723 ATH6KL_SCATTER_ENTRIES_PER_REQ,
724 ATH6KL_SCATTER_REQS, virt_scat);
725
726 if (ret) {
727 ath6kl_err("failed to alloc virtual scatter resources !\n");
728 ath6kl_sdio_cleanup_scatter(ar);
729 return ret;
730 }
731
Kalle Valo3ef987b2011-10-24 12:18:07 +0300732 ath6kl_dbg(ATH6KL_DBG_BOOT,
733 "virtual scatter enabled requests %d entries %d\n",
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530734 ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
735
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +0530736 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
737 target->max_xfer_szper_scatreq =
Vasanthakumar Thiagarajancfeab102011-07-16 20:29:14 +0530738 ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
739 }
740
Vasanthakumar Thiagarajan18a0f932011-07-16 20:29:13 +0530741 return 0;
742}
743
Kalle Valoe28e8102011-11-01 08:44:36 +0200744static int ath6kl_sdio_config(struct ath6kl *ar)
745{
746 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
747 struct sdio_func *func = ar_sdio->func;
748 int ret;
749
750 sdio_claim_host(func);
751
752 if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
753 MANUFACTURER_ID_AR6003_BASE) {
754 /* enable 4-bit ASYNC interrupt on AR6003 or later */
755 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
756 CCCR_SDIO_IRQ_MODE_REG,
757 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
758 if (ret) {
759 ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
760 ret);
761 goto out;
762 }
763
764 ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
765 }
766
767 /* give us some time to enable, in ms */
768 func->enable_timeout = 100;
769
770 ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
771 if (ret) {
772 ath6kl_err("Set sdio block size %d failed: %d)\n",
773 HIF_MBOX_BLOCK_SIZE, ret);
774 sdio_release_host(func);
775 goto out;
776 }
777
778out:
779 sdio_release_host(func);
780
781 return ret;
782}
783
Raja Mani0f60e9f2011-11-07 22:52:45 +0200784static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
Kalle Valoabcb3442011-07-22 08:26:20 +0300785{
786 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
787 struct sdio_func *func = ar_sdio->func;
788 mmc_pm_flag_t flags;
789 int ret;
790
791 flags = sdio_get_host_pm_caps(func);
792
Kalle Valob4b2a0b2011-11-01 08:44:44 +0200793 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
794
Kalle Valo8277de12011-11-03 12:18:31 +0200795 if (!(flags & MMC_PM_KEEP_POWER) ||
796 (ar->conf_flags & ATH6KL_CONF_SUSPEND_CUTPOWER)) {
Kalle Valob4b2a0b2011-11-01 08:44:44 +0200797 /* as host doesn't support keep power we need to cut power */
Raja Mani0f60e9f2011-11-07 22:52:45 +0200798 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER,
799 NULL);
Sam Leffler17380852011-10-13 13:20:32 +0300800 }
Kalle Valoabcb3442011-07-22 08:26:20 +0300801
802 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
803 if (ret) {
804 printk(KERN_ERR "ath6kl: set sdio pm flags failed: %d\n",
805 ret);
806 return ret;
807 }
808
Raja Manid7c44e02011-11-07 22:52:46 +0200809 if ((flags & MMC_PM_WAKE_SDIO_IRQ) && wow) {
810 /*
811 * The host sdio controller is capable of keep power and
812 * sdio irq wake up at this point. It's fine to continue
813 * wow suspend operation.
814 */
815 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
816 if (ret)
817 return ret;
818
819 ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
820 if (ret)
821 ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
822
823 return ret;
824 }
825
Raja Mani0f60e9f2011-11-07 22:52:45 +0200826 return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP, NULL);
Kalle Valoabcb3442011-07-22 08:26:20 +0300827}
828
Chilam Ngaa6cffc2011-10-05 10:12:52 +0300829static int ath6kl_sdio_resume(struct ath6kl *ar)
830{
Kalle Valob4b2a0b2011-11-01 08:44:44 +0200831 switch (ar->state) {
832 case ATH6KL_STATE_OFF:
833 case ATH6KL_STATE_CUTPOWER:
834 ath6kl_dbg(ATH6KL_DBG_SUSPEND,
835 "sdio resume configuring sdio\n");
836
837 /* need to set sdio settings after power is cut from sdio */
838 ath6kl_sdio_config(ar);
839 break;
840
841 case ATH6KL_STATE_ON:
Kalle Valob4b2a0b2011-11-01 08:44:44 +0200842 break;
843
844 case ATH6KL_STATE_DEEPSLEEP:
845 break;
Raja Manid7c44e02011-11-07 22:52:46 +0200846
847 case ATH6KL_STATE_WOW:
848 break;
Kalle Valob4b2a0b2011-11-01 08:44:44 +0200849 }
850
Kalle Valo52d81a62011-11-01 08:44:21 +0200851 ath6kl_cfg80211_resume(ar);
Chilam Ngaa6cffc2011-10-05 10:12:52 +0300852
853 return 0;
854}
855
Kalle Valoc7111492011-11-11 12:17:51 +0200856/* set the window address register (using 4-byte register access ). */
857static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
858{
859 int status;
860 u8 addr_val[4];
861 s32 i;
862
863 /*
864 * Write bytes 1,2,3 of the register to set the upper address bytes,
865 * the LSB is written last to initiate the access cycle
866 */
867
868 for (i = 1; i <= 3; i++) {
869 /*
870 * Fill the buffer with the address byte value we want to
871 * hit 4 times.
872 */
873 memset(addr_val, ((u8 *)&addr)[i], 4);
874
875 /*
876 * Hit each byte of the register address with a 4-byte
877 * write operation to the same address, this is a harmless
878 * operation.
879 */
880 status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
881 4, HIF_WR_SYNC_BYTE_FIX);
882 if (status)
883 break;
884 }
885
886 if (status) {
887 ath6kl_err("%s: failed to write initial bytes of 0x%x "
888 "to window reg: 0x%X\n", __func__,
889 addr, reg_addr);
890 return status;
891 }
892
893 /*
894 * Write the address register again, this time write the whole
895 * 4-byte value. The effect here is that the LSB write causes the
896 * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
897 * effect since we are writing the same values again
898 */
899 status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
900 4, HIF_WR_SYNC_BYTE_INC);
901
902 if (status) {
903 ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
904 __func__, addr, reg_addr);
905 return status;
906 }
907
908 return 0;
909}
910
911static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
912{
913 int status;
914
915 /* set window register to start read cycle */
916 status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
917 address);
918
919 if (status)
920 return status;
921
922 /* read the data */
923 status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
924 (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
925 if (status) {
926 ath6kl_err("%s: failed to read from window data addr\n",
927 __func__);
928 return status;
929 }
930
931 return status;
932}
933
934static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
935 __le32 data)
936{
937 int status;
938 u32 val = (__force u32) data;
939
940 /* set write data */
941 status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
942 (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
943 if (status) {
944 ath6kl_err("%s: failed to write 0x%x to window data addr\n",
945 __func__, data);
946 return status;
947 }
948
949 /* set window register, which starts the write cycle */
950 return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
951 address);
952}
953
Kalle Valo66b693c2011-11-11 12:17:33 +0200954static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
955{
956 u32 addr;
957 unsigned long timeout;
958 int ret;
959
960 ar->bmi.cmd_credits = 0;
961
962 /* Read the counter register to get the command credits */
963 addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
964
965 timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
966 while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
967
968 /*
969 * Hit the credit counter with a 4-byte access, the first byte
970 * read will hit the counter and cause a decrement, while the
971 * remaining 3 bytes has no effect. The rationale behind this
972 * is to make all HIF accesses 4-byte aligned.
973 */
974 ret = ath6kl_sdio_read_write_sync(ar, addr,
975 (u8 *)&ar->bmi.cmd_credits, 4,
976 HIF_RD_SYNC_BYTE_INC);
977 if (ret) {
978 ath6kl_err("Unable to decrement the command credit "
979 "count register: %d\n", ret);
980 return ret;
981 }
982
983 /* The counter is only 8 bits.
984 * Ignore anything in the upper 3 bytes
985 */
986 ar->bmi.cmd_credits &= 0xFF;
987 }
988
989 if (!ar->bmi.cmd_credits) {
990 ath6kl_err("bmi communication timeout\n");
991 return -ETIMEDOUT;
992 }
993
994 return 0;
995}
996
997static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
998{
999 unsigned long timeout;
1000 u32 rx_word = 0;
1001 int ret = 0;
1002
1003 timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
1004 while ((time_before(jiffies, timeout)) && !rx_word) {
1005 ret = ath6kl_sdio_read_write_sync(ar,
1006 RX_LOOKAHEAD_VALID_ADDRESS,
1007 (u8 *)&rx_word, sizeof(rx_word),
1008 HIF_RD_SYNC_BYTE_INC);
1009 if (ret) {
1010 ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
1011 return ret;
1012 }
1013
1014 /* all we really want is one bit */
1015 rx_word &= (1 << ENDPOINT1);
1016 }
1017
1018 if (!rx_word) {
1019 ath6kl_err("bmi_recv_buf FIFO empty\n");
1020 return -EINVAL;
1021 }
1022
1023 return ret;
1024}
1025
1026static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
1027{
1028 int ret;
1029 u32 addr;
1030
1031 ret = ath6kl_sdio_bmi_credits(ar);
1032 if (ret)
1033 return ret;
1034
1035 addr = ar->mbox_info.htc_addr;
1036
1037 ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1038 HIF_WR_SYNC_BYTE_INC);
1039 if (ret)
1040 ath6kl_err("unable to send the bmi data to the device\n");
1041
1042 return ret;
1043}
1044
1045static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
1046{
1047 int ret;
1048 u32 addr;
1049
1050 /*
1051 * During normal bootup, small reads may be required.
1052 * Rather than issue an HIF Read and then wait as the Target
1053 * adds successive bytes to the FIFO, we wait here until
1054 * we know that response data is available.
1055 *
1056 * This allows us to cleanly timeout on an unexpected
1057 * Target failure rather than risk problems at the HIF level.
1058 * In particular, this avoids SDIO timeouts and possibly garbage
1059 * data on some host controllers. And on an interconnect
1060 * such as Compact Flash (as well as some SDIO masters) which
1061 * does not provide any indication on data timeout, it avoids
1062 * a potential hang or garbage response.
1063 *
1064 * Synchronization is more difficult for reads larger than the
1065 * size of the MBOX FIFO (128B), because the Target is unable
1066 * to push the 129th byte of data until AFTER the Host posts an
1067 * HIF Read and removes some FIFO data. So for large reads the
1068 * Host proceeds to post an HIF Read BEFORE all the data is
1069 * actually available to read. Fortunately, large BMI reads do
1070 * not occur in practice -- they're supported for debug/development.
1071 *
1072 * So Host/Target BMI synchronization is divided into these cases:
1073 * CASE 1: length < 4
1074 * Should not happen
1075 *
1076 * CASE 2: 4 <= length <= 128
1077 * Wait for first 4 bytes to be in FIFO
1078 * If CONSERVATIVE_BMI_READ is enabled, also wait for
1079 * a BMI command credit, which indicates that the ENTIRE
1080 * response is available in the the FIFO
1081 *
1082 * CASE 3: length > 128
1083 * Wait for the first 4 bytes to be in FIFO
1084 *
1085 * For most uses, a small timeout should be sufficient and we will
1086 * usually see a response quickly; but there may be some unusual
1087 * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1088 * For now, we use an unbounded busy loop while waiting for
1089 * BMI_EXECUTE.
1090 *
1091 * If BMI_EXECUTE ever needs to support longer-latency execution,
1092 * especially in production, this code needs to be enhanced to sleep
1093 * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
1094 * a function of Host processor speed.
1095 */
1096 if (len >= 4) { /* NB: Currently, always true */
1097 ret = ath6kl_bmi_get_rx_lkahd(ar);
1098 if (ret)
1099 return ret;
1100 }
1101
1102 addr = ar->mbox_info.htc_addr;
1103 ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1104 HIF_RD_SYNC_BYTE_INC);
1105 if (ret) {
1106 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1107 ret);
1108 return ret;
1109 }
1110
1111 return 0;
1112}
1113
Kalle Valo32a07e42011-10-30 21:15:57 +02001114static void ath6kl_sdio_stop(struct ath6kl *ar)
1115{
1116 struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
1117 struct bus_request *req, *tmp_req;
1118 void *context;
1119
1120 /* FIXME: make sure that wq is not queued again */
1121
1122 cancel_work_sync(&ar_sdio->wr_async_work);
1123
1124 spin_lock_bh(&ar_sdio->wr_async_lock);
1125
1126 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1127 list_del(&req->list);
1128
1129 if (req->scat_req) {
1130 /* this is a scatter gather request */
1131 req->scat_req->status = -ECANCELED;
1132 req->scat_req->complete(ar_sdio->ar->htc_target,
1133 req->scat_req);
1134 } else {
1135 context = req->packet;
1136 ath6kl_sdio_free_bus_req(ar_sdio, req);
1137 ath6kl_hif_rw_comp_handler(context, -ECANCELED);
1138 }
1139 }
1140
1141 spin_unlock_bh(&ar_sdio->wr_async_lock);
1142
1143 WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
1144}
1145
Kalle Valobdcd8172011-07-18 00:22:30 +03001146static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
1147 .read_write_sync = ath6kl_sdio_read_write_sync,
1148 .write_async = ath6kl_sdio_write_async,
1149 .irq_enable = ath6kl_sdio_irq_enable,
1150 .irq_disable = ath6kl_sdio_irq_disable,
1151 .scatter_req_get = ath6kl_sdio_scatter_req_get,
1152 .scatter_req_add = ath6kl_sdio_scatter_req_add,
1153 .enable_scatter = ath6kl_sdio_enable_scatter,
Vasanthakumar Thiagarajanf74a7362011-07-16 20:29:05 +05301154 .scat_req_rw = ath6kl_sdio_async_rw_scatter,
Kalle Valobdcd8172011-07-18 00:22:30 +03001155 .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
Kalle Valoabcb3442011-07-22 08:26:20 +03001156 .suspend = ath6kl_sdio_suspend,
Chilam Ngaa6cffc2011-10-05 10:12:52 +03001157 .resume = ath6kl_sdio_resume,
Kalle Valoc7111492011-11-11 12:17:51 +02001158 .diag_read32 = ath6kl_sdio_diag_read32,
1159 .diag_write32 = ath6kl_sdio_diag_write32,
Kalle Valo66b693c2011-11-11 12:17:33 +02001160 .bmi_read = ath6kl_sdio_bmi_read,
1161 .bmi_write = ath6kl_sdio_bmi_write,
Kalle Valob2e75692011-10-27 18:48:14 +03001162 .power_on = ath6kl_sdio_power_on,
1163 .power_off = ath6kl_sdio_power_off,
Kalle Valo32a07e42011-10-30 21:15:57 +02001164 .stop = ath6kl_sdio_stop,
Kalle Valobdcd8172011-07-18 00:22:30 +03001165};
1166
Kalle Valob4b2a0b2011-11-01 08:44:44 +02001167#ifdef CONFIG_PM_SLEEP
1168
1169/*
1170 * Empty handlers so that mmc subsystem doesn't remove us entirely during
1171 * suspend. We instead follow cfg80211 suspend/resume handlers.
1172 */
1173static int ath6kl_sdio_pm_suspend(struct device *device)
1174{
1175 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
1176
1177 return 0;
1178}
1179
1180static int ath6kl_sdio_pm_resume(struct device *device)
1181{
1182 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
1183
1184 return 0;
1185}
1186
1187static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
1188 ath6kl_sdio_pm_resume);
1189
1190#define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
1191
1192#else
1193
1194#define ATH6KL_SDIO_PM_OPS NULL
1195
1196#endif /* CONFIG_PM_SLEEP */
1197
Kalle Valobdcd8172011-07-18 00:22:30 +03001198static int ath6kl_sdio_probe(struct sdio_func *func,
1199 const struct sdio_device_id *id)
1200{
1201 int ret;
1202 struct ath6kl_sdio *ar_sdio;
1203 struct ath6kl *ar;
1204 int count;
1205
Kalle Valo3ef987b2011-10-24 12:18:07 +03001206 ath6kl_dbg(ATH6KL_DBG_BOOT,
1207 "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
Kalle Valof7325b82011-09-27 14:30:58 +03001208 func->num, func->vendor, func->device,
1209 func->max_blksize, func->cur_blksize);
Kalle Valobdcd8172011-07-18 00:22:30 +03001210
1211 ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
1212 if (!ar_sdio)
1213 return -ENOMEM;
1214
1215 ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
1216 if (!ar_sdio->dma_buffer) {
1217 ret = -ENOMEM;
1218 goto err_hif;
1219 }
1220
1221 ar_sdio->func = func;
1222 sdio_set_drvdata(func, ar_sdio);
1223
1224 ar_sdio->id = id;
1225 ar_sdio->is_disabled = true;
1226
1227 spin_lock_init(&ar_sdio->lock);
1228 spin_lock_init(&ar_sdio->scat_lock);
1229 spin_lock_init(&ar_sdio->wr_async_lock);
Raja Manifdb28582011-11-21 12:26:51 +05301230 mutex_init(&ar_sdio->dma_buffer_mutex);
Kalle Valobdcd8172011-07-18 00:22:30 +03001231
1232 INIT_LIST_HEAD(&ar_sdio->scat_req);
1233 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
1234 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
1235
1236 INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
1237
1238 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
1239 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
1240
1241 ar = ath6kl_core_alloc(&ar_sdio->func->dev);
1242 if (!ar) {
1243 ath6kl_err("Failed to alloc ath6kl core\n");
1244 ret = -ENOMEM;
1245 goto err_dma;
1246 }
1247
1248 ar_sdio->ar = ar;
Kalle Valo77eab1e2011-11-11 12:18:22 +02001249 ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
Kalle Valobdcd8172011-07-18 00:22:30 +03001250 ar->hif_priv = ar_sdio;
1251 ar->hif_ops = &ath6kl_sdio_ops;
Kalle Valo1f4c8942011-11-11 12:17:42 +02001252 ar->bmi.max_data_size = 256;
Kalle Valobdcd8172011-07-18 00:22:30 +03001253
1254 ath6kl_sdio_set_mbox_info(ar);
1255
Kalle Valoe28e8102011-11-01 08:44:36 +02001256 ret = ath6kl_sdio_config(ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001257 if (ret) {
Kalle Valoe28e8102011-11-01 08:44:36 +02001258 ath6kl_err("Failed to config sdio: %d\n", ret);
1259 goto err_core_alloc;
Kalle Valobdcd8172011-07-18 00:22:30 +03001260 }
1261
Kalle Valobdcd8172011-07-18 00:22:30 +03001262 ret = ath6kl_core_init(ar);
1263 if (ret) {
1264 ath6kl_err("Failed to init ath6kl core\n");
Kalle Valoe28e8102011-11-01 08:44:36 +02001265 goto err_core_alloc;
Kalle Valobdcd8172011-07-18 00:22:30 +03001266 }
1267
1268 return ret;
1269
Vasanthakumar Thiagarajan8dafb702011-10-25 19:33:58 +05301270err_core_alloc:
1271 ath6kl_core_free(ar_sdio->ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001272err_dma:
1273 kfree(ar_sdio->dma_buffer);
1274err_hif:
1275 kfree(ar_sdio);
1276
1277 return ret;
1278}
1279
1280static void ath6kl_sdio_remove(struct sdio_func *func)
1281{
1282 struct ath6kl_sdio *ar_sdio;
1283
Kalle Valo3ef987b2011-10-24 12:18:07 +03001284 ath6kl_dbg(ATH6KL_DBG_BOOT,
1285 "sdio removed func %d vendor 0x%x device 0x%x\n",
Kalle Valof7325b82011-09-27 14:30:58 +03001286 func->num, func->vendor, func->device);
1287
Kalle Valobdcd8172011-07-18 00:22:30 +03001288 ar_sdio = sdio_get_drvdata(func);
1289
1290 ath6kl_stop_txrx(ar_sdio->ar);
1291 cancel_work_sync(&ar_sdio->wr_async_work);
1292
Vasanthakumar Thiagarajan6db8fa52011-10-25 19:34:16 +05301293 ath6kl_core_cleanup(ar_sdio->ar);
Kalle Valobdcd8172011-07-18 00:22:30 +03001294
Kalle Valobdcd8172011-07-18 00:22:30 +03001295 kfree(ar_sdio->dma_buffer);
1296 kfree(ar_sdio);
1297}
1298
1299static const struct sdio_device_id ath6kl_sdio_devices[] = {
1300 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
1301 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
Naveen Gangadharand93e2c22011-11-11 12:18:14 +02001302 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
1303 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
Kalle Valobdcd8172011-07-18 00:22:30 +03001304 {},
1305};
1306
1307MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
1308
1309static struct sdio_driver ath6kl_sdio_driver = {
Kalle Valofde57762011-11-11 12:18:45 +02001310 .name = "ath6kl_sdio",
Kalle Valobdcd8172011-07-18 00:22:30 +03001311 .id_table = ath6kl_sdio_devices,
1312 .probe = ath6kl_sdio_probe,
1313 .remove = ath6kl_sdio_remove,
Kalle Valob4b2a0b2011-11-01 08:44:44 +02001314 .drv.pm = ATH6KL_SDIO_PM_OPS,
Kalle Valobdcd8172011-07-18 00:22:30 +03001315};
1316
1317static int __init ath6kl_sdio_init(void)
1318{
1319 int ret;
1320
1321 ret = sdio_register_driver(&ath6kl_sdio_driver);
1322 if (ret)
1323 ath6kl_err("sdio driver registration failed: %d\n", ret);
1324
1325 return ret;
1326}
1327
1328static void __exit ath6kl_sdio_exit(void)
1329{
1330 sdio_unregister_driver(&ath6kl_sdio_driver);
1331}
1332
1333module_init(ath6kl_sdio_init);
1334module_exit(ath6kl_sdio_exit);
1335
1336MODULE_AUTHOR("Atheros Communications, Inc.");
1337MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
1338MODULE_LICENSE("Dual BSD/GPL");
1339
Kalle Valo0d0192b2011-11-14 19:31:07 +02001340MODULE_FIRMWARE(AR6003_HW_2_0_OTP_FILE);
1341MODULE_FIRMWARE(AR6003_HW_2_0_FIRMWARE_FILE);
1342MODULE_FIRMWARE(AR6003_HW_2_0_PATCH_FILE);
1343MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
1344MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
1345MODULE_FIRMWARE(AR6003_HW_2_1_1_OTP_FILE);
1346MODULE_FIRMWARE(AR6003_HW_2_1_1_FIRMWARE_FILE);
1347MODULE_FIRMWARE(AR6003_HW_2_1_1_PATCH_FILE);
1348MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
1349MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);
Kalle Valof0ea5d52011-11-14 19:31:15 +02001350MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE);
1351MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
1352MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
1353MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE);
1354MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
1355MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);