Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | struct 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 | |
| 61 | static 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 | */ |
| 72 | static inline bool buf_needs_bounce(u8 *buf) |
| 73 | { |
| 74 | return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf); |
| 75 | } |
| 76 | |
| 77 | static 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 | |
| 90 | static 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 | |
| 102 | static 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 | |
| 117 | static 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 | |
| 131 | static 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 | |
| 153 | static 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 | |
| 165 | static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req, |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 166 | 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 Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 184 | sg = scat_req->sgentries; |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 185 | 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 Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 208 | data->sg = scat_req->sgentries; |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 209 | data->sg_len = scat_req->scat_entries; |
| 210 | } |
| 211 | |
| 212 | static 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 Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 228 | ath6kl_sdio_setup_scat_data(scat_req, &data); |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 229 | |
| 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) |
Vasanthakumar Thiagarajan | e041c7f | 2011-07-16 20:29:09 +0530 | [diff] [blame] | 267 | scat_req->complete(ar_sdio->ar->htc_target, scat_req); |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 268 | |
| 269 | return status; |
| 270 | } |
| 271 | |
Vasanthakumar Thiagarajan | 3df505a | 2011-07-16 20:29:10 +0530 | [diff] [blame] | 272 | static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio, |
| 273 | int n_scat_entry, int n_scat_req, |
| 274 | bool virt_scat) |
| 275 | { |
| 276 | struct hif_scatter_req *s_req; |
| 277 | struct bus_request *bus_req; |
| 278 | int i, scat_req_sz, scat_list_sz, sg_sz = 0; |
| 279 | |
| 280 | scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item); |
| 281 | scat_req_sz = sizeof(*s_req) + scat_list_sz; |
| 282 | |
| 283 | if (!virt_scat) |
| 284 | sg_sz = sizeof(struct scatterlist) * n_scat_entry; |
| 285 | |
| 286 | for (i = 0; i < n_scat_req; i++) { |
| 287 | /* allocate the scatter request */ |
| 288 | s_req = kzalloc(scat_req_sz, GFP_KERNEL); |
| 289 | if (!s_req) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | if (sg_sz) { |
| 293 | /* allocate sglist */ |
| 294 | s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL); |
| 295 | |
| 296 | if (!s_req->sgentries) { |
| 297 | kfree(s_req); |
| 298 | return -ENOMEM; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* allocate a bus request for this scatter request */ |
| 303 | bus_req = ath6kl_sdio_alloc_busreq(ar_sdio); |
| 304 | if (!bus_req) { |
| 305 | kfree(s_req->sgentries); |
| 306 | kfree(s_req); |
| 307 | return -ENOMEM; |
| 308 | } |
| 309 | |
| 310 | /* assign the scatter request to this bus request */ |
| 311 | bus_req->scat_req = s_req; |
| 312 | s_req->busrequest = bus_req; |
| 313 | |
| 314 | /* add it to the scatter pool */ |
| 315 | hif_scatter_req_add(ar_sdio->ar, s_req); |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 321 | /* clean up scatter support */ |
| 322 | static void ath6kl_sdio_cleanup_scat_resource(struct ath6kl_sdio *ar_sdio) |
| 323 | { |
| 324 | struct hif_scatter_req *s_req, *tmp_req; |
| 325 | unsigned long flag; |
| 326 | |
| 327 | /* empty the free list */ |
| 328 | spin_lock_irqsave(&ar_sdio->scat_lock, flag); |
| 329 | list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) { |
| 330 | list_del(&s_req->list); |
| 331 | spin_unlock_irqrestore(&ar_sdio->scat_lock, flag); |
| 332 | |
Vasanthakumar Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 333 | if (s_req->busrequest) |
| 334 | ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest); |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 335 | kfree(s_req->virt_dma_buf); |
Vasanthakumar Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 336 | kfree(s_req->sgentries); |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 337 | kfree(s_req); |
| 338 | |
| 339 | spin_lock_irqsave(&ar_sdio->scat_lock, flag); |
| 340 | } |
| 341 | spin_unlock_irqrestore(&ar_sdio->scat_lock, flag); |
| 342 | } |
| 343 | |
| 344 | /* setup of HIF scatter resources */ |
Vasanthakumar Thiagarajan | def85c0 | 2011-07-16 20:29:11 +0530 | [diff] [blame^] | 345 | static int ath6kl_sdio_enable_scatter(struct ath6kl *ar, |
| 346 | struct hif_dev_scat_sup_info *pinfo) |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 347 | { |
Vasanthakumar Thiagarajan | def85c0 | 2011-07-16 20:29:11 +0530 | [diff] [blame^] | 348 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
Vasanthakumar Thiagarajan | 3df505a | 2011-07-16 20:29:10 +0530 | [diff] [blame] | 349 | int ret = 0; |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 350 | |
| 351 | /* check if host supports scatter and it meets our requirements */ |
| 352 | if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) { |
| 353 | ath6kl_err("hif-scatter: host only supports scatter of : %d entries, need: %d\n", |
| 354 | ar_sdio->func->card->host->max_segs, |
| 355 | MAX_SCATTER_ENTRIES_PER_REQ); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
| 359 | ath6kl_dbg(ATH6KL_DBG_ANY, |
| 360 | "hif-scatter enabled: max scatter req : %d entries: %d\n", |
| 361 | MAX_SCATTER_REQUESTS, MAX_SCATTER_ENTRIES_PER_REQ); |
| 362 | |
Vasanthakumar Thiagarajan | 3df505a | 2011-07-16 20:29:10 +0530 | [diff] [blame] | 363 | ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio, |
| 364 | MAX_SCATTER_ENTRIES_PER_REQ, |
| 365 | MAX_SCATTER_REQUESTS, 0); |
| 366 | if (ret) { |
| 367 | ath6kl_err("hif-scatter: failed to alloc scatter resources !\n"); |
| 368 | ath6kl_sdio_cleanup_scat_resource(ar_sdio); |
| 369 | return ret; |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 370 | } |
| 371 | |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 372 | pinfo->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ; |
| 373 | pinfo->max_xfer_szper_scatreq = MAX_SCATTER_REQ_TRANSFER_SIZE; |
| 374 | |
| 375 | return 0; |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf, |
| 379 | u32 len, u32 request) |
| 380 | { |
| 381 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 382 | u8 *tbuf = NULL; |
| 383 | int ret; |
| 384 | bool bounced = false; |
| 385 | |
| 386 | if (request & HIF_BLOCK_BASIS) |
| 387 | len = round_down(len, HIF_MBOX_BLOCK_SIZE); |
| 388 | |
| 389 | if (buf_needs_bounce(buf)) { |
| 390 | if (!ar_sdio->dma_buffer) |
| 391 | return -ENOMEM; |
| 392 | tbuf = ar_sdio->dma_buffer; |
| 393 | memcpy(tbuf, buf, len); |
| 394 | bounced = true; |
| 395 | } else |
| 396 | tbuf = buf; |
| 397 | |
| 398 | sdio_claim_host(ar_sdio->func); |
| 399 | if (request & HIF_WRITE) { |
| 400 | if (addr >= HIF_MBOX_BASE_ADDR && |
| 401 | addr <= HIF_MBOX_END_ADDR) |
| 402 | addr += (HIF_MBOX_WIDTH - len); |
| 403 | |
| 404 | if (addr == HIF_MBOX0_EXT_BASE_ADDR) |
| 405 | addr += HIF_MBOX0_EXT_WIDTH - len; |
| 406 | |
| 407 | if (request & HIF_FIXED_ADDRESS) |
| 408 | ret = sdio_writesb(ar_sdio->func, addr, tbuf, len); |
| 409 | else |
| 410 | ret = sdio_memcpy_toio(ar_sdio->func, addr, tbuf, len); |
| 411 | } else { |
| 412 | if (request & HIF_FIXED_ADDRESS) |
| 413 | ret = sdio_readsb(ar_sdio->func, tbuf, addr, len); |
| 414 | else |
| 415 | ret = sdio_memcpy_fromio(ar_sdio->func, tbuf, |
| 416 | addr, len); |
| 417 | if (bounced) |
| 418 | memcpy(buf, tbuf, len); |
| 419 | } |
| 420 | sdio_release_host(ar_sdio->func); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio, |
| 426 | struct bus_request *req) |
| 427 | { |
| 428 | if (req->scat_req) |
| 429 | ath6kl_sdio_scat_rw(ar_sdio, req); |
| 430 | else { |
| 431 | void *context; |
| 432 | int status; |
| 433 | |
| 434 | status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address, |
| 435 | req->buffer, req->length, |
| 436 | req->request); |
| 437 | context = req->packet; |
| 438 | ath6kl_sdio_free_bus_req(ar_sdio, req); |
| 439 | ath6kldev_rw_comp_handler(context, status); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static void ath6kl_sdio_write_async_work(struct work_struct *work) |
| 444 | { |
| 445 | struct ath6kl_sdio *ar_sdio; |
| 446 | unsigned long flags; |
| 447 | struct bus_request *req, *tmp_req; |
| 448 | |
| 449 | ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work); |
| 450 | sdio_claim_host(ar_sdio->func); |
| 451 | |
| 452 | spin_lock_irqsave(&ar_sdio->wr_async_lock, flags); |
| 453 | list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) { |
| 454 | list_del(&req->list); |
| 455 | spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags); |
| 456 | __ath6kl_sdio_write_async(ar_sdio, req); |
| 457 | spin_lock_irqsave(&ar_sdio->wr_async_lock, flags); |
| 458 | } |
| 459 | spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags); |
| 460 | |
| 461 | sdio_release_host(ar_sdio->func); |
| 462 | } |
| 463 | |
| 464 | static void ath6kl_sdio_irq_handler(struct sdio_func *func) |
| 465 | { |
| 466 | int status; |
| 467 | struct ath6kl_sdio *ar_sdio; |
| 468 | |
| 469 | ar_sdio = sdio_get_drvdata(func); |
| 470 | atomic_set(&ar_sdio->irq_handling, 1); |
| 471 | |
| 472 | /* |
| 473 | * Release the host during interrups so we can pick it back up when |
| 474 | * we process commands. |
| 475 | */ |
| 476 | sdio_release_host(ar_sdio->func); |
| 477 | |
| 478 | status = ath6kldev_intr_bh_handler(ar_sdio->ar); |
| 479 | sdio_claim_host(ar_sdio->func); |
| 480 | atomic_set(&ar_sdio->irq_handling, 0); |
| 481 | WARN_ON(status && status != -ECANCELED); |
| 482 | } |
| 483 | |
| 484 | static int ath6kl_sdio_power_on(struct ath6kl_sdio *ar_sdio) |
| 485 | { |
| 486 | struct sdio_func *func = ar_sdio->func; |
| 487 | int ret = 0; |
| 488 | |
| 489 | if (!ar_sdio->is_disabled) |
| 490 | return 0; |
| 491 | |
| 492 | sdio_claim_host(func); |
| 493 | |
| 494 | ret = sdio_enable_func(func); |
| 495 | if (ret) { |
| 496 | ath6kl_err("Unable to enable sdio func: %d)\n", ret); |
| 497 | sdio_release_host(func); |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | sdio_release_host(func); |
| 502 | |
| 503 | /* |
| 504 | * Wait for hardware to initialise. It should take a lot less than |
| 505 | * 10 ms but let's be conservative here. |
| 506 | */ |
| 507 | msleep(10); |
| 508 | |
| 509 | ar_sdio->is_disabled = false; |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static int ath6kl_sdio_power_off(struct ath6kl_sdio *ar_sdio) |
| 515 | { |
| 516 | int ret; |
| 517 | |
| 518 | if (ar_sdio->is_disabled) |
| 519 | return 0; |
| 520 | |
| 521 | /* Disable the card */ |
| 522 | sdio_claim_host(ar_sdio->func); |
| 523 | ret = sdio_disable_func(ar_sdio->func); |
| 524 | sdio_release_host(ar_sdio->func); |
| 525 | |
| 526 | if (ret) |
| 527 | return ret; |
| 528 | |
| 529 | ar_sdio->is_disabled = true; |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer, |
| 535 | u32 length, u32 request, |
| 536 | struct htc_packet *packet) |
| 537 | { |
| 538 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 539 | struct bus_request *bus_req; |
| 540 | unsigned long flags; |
| 541 | |
| 542 | bus_req = ath6kl_sdio_alloc_busreq(ar_sdio); |
| 543 | |
| 544 | if (!bus_req) |
| 545 | return -ENOMEM; |
| 546 | |
| 547 | bus_req->address = address; |
| 548 | bus_req->buffer = buffer; |
| 549 | bus_req->length = length; |
| 550 | bus_req->request = request; |
| 551 | bus_req->packet = packet; |
| 552 | |
| 553 | spin_lock_irqsave(&ar_sdio->wr_async_lock, flags); |
| 554 | list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq); |
| 555 | spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags); |
| 556 | queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static void ath6kl_sdio_irq_enable(struct ath6kl *ar) |
| 562 | { |
| 563 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 564 | int ret; |
| 565 | |
| 566 | sdio_claim_host(ar_sdio->func); |
| 567 | |
| 568 | /* Register the isr */ |
| 569 | ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler); |
| 570 | if (ret) |
| 571 | ath6kl_err("Failed to claim sdio irq: %d\n", ret); |
| 572 | |
| 573 | sdio_release_host(ar_sdio->func); |
| 574 | } |
| 575 | |
| 576 | static void ath6kl_sdio_irq_disable(struct ath6kl *ar) |
| 577 | { |
| 578 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 579 | int ret; |
| 580 | |
| 581 | sdio_claim_host(ar_sdio->func); |
| 582 | |
| 583 | /* Mask our function IRQ */ |
| 584 | while (atomic_read(&ar_sdio->irq_handling)) { |
| 585 | sdio_release_host(ar_sdio->func); |
| 586 | schedule_timeout(HZ / 10); |
| 587 | sdio_claim_host(ar_sdio->func); |
| 588 | } |
| 589 | |
| 590 | ret = sdio_release_irq(ar_sdio->func); |
| 591 | if (ret) |
| 592 | ath6kl_err("Failed to release sdio irq: %d\n", ret); |
| 593 | |
| 594 | sdio_release_host(ar_sdio->func); |
| 595 | } |
| 596 | |
| 597 | static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar) |
| 598 | { |
| 599 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 600 | struct hif_scatter_req *node = NULL; |
| 601 | unsigned long flag; |
| 602 | |
| 603 | spin_lock_irqsave(&ar_sdio->scat_lock, flag); |
| 604 | |
| 605 | if (!list_empty(&ar_sdio->scat_req)) { |
| 606 | node = list_first_entry(&ar_sdio->scat_req, |
| 607 | struct hif_scatter_req, list); |
| 608 | list_del(&node->list); |
| 609 | } |
| 610 | |
| 611 | spin_unlock_irqrestore(&ar_sdio->scat_lock, flag); |
| 612 | |
| 613 | return node; |
| 614 | } |
| 615 | |
| 616 | static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar, |
| 617 | struct hif_scatter_req *s_req) |
| 618 | { |
| 619 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 620 | unsigned long flag; |
| 621 | |
| 622 | spin_lock_irqsave(&ar_sdio->scat_lock, flag); |
| 623 | |
| 624 | list_add_tail(&s_req->list, &ar_sdio->scat_req); |
| 625 | |
| 626 | spin_unlock_irqrestore(&ar_sdio->scat_lock, flag); |
| 627 | |
| 628 | } |
| 629 | |
Vasanthakumar Thiagarajan | c630d18 | 2011-07-16 20:29:06 +0530 | [diff] [blame] | 630 | /* scatter gather read write request */ |
| 631 | static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar, |
| 632 | struct hif_scatter_req *scat_req) |
| 633 | { |
| 634 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
Vasanthakumar Thiagarajan | c630d18 | 2011-07-16 20:29:06 +0530 | [diff] [blame] | 635 | u32 request = scat_req->req; |
| 636 | int status = 0; |
| 637 | unsigned long flags; |
| 638 | |
| 639 | if (!scat_req->len) |
| 640 | return -EINVAL; |
| 641 | |
| 642 | ath6kl_dbg(ATH6KL_DBG_SCATTER, |
| 643 | "hif-scatter: total len: %d scatter entries: %d\n", |
| 644 | scat_req->len, scat_req->scat_entries); |
| 645 | |
| 646 | if (request & HIF_SYNCHRONOUS) { |
| 647 | sdio_claim_host(ar_sdio->func); |
Vasanthakumar Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 648 | status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest); |
Vasanthakumar Thiagarajan | c630d18 | 2011-07-16 20:29:06 +0530 | [diff] [blame] | 649 | sdio_release_host(ar_sdio->func); |
| 650 | } else { |
| 651 | spin_lock_irqsave(&ar_sdio->wr_async_lock, flags); |
Vasanthakumar Thiagarajan | d4df789 | 2011-07-16 20:29:07 +0530 | [diff] [blame] | 652 | list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq); |
Vasanthakumar Thiagarajan | c630d18 | 2011-07-16 20:29:06 +0530 | [diff] [blame] | 653 | spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags); |
| 654 | queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work); |
| 655 | } |
| 656 | |
| 657 | return status; |
| 658 | } |
| 659 | |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 660 | static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar) |
| 661 | { |
| 662 | struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar); |
| 663 | |
| 664 | ath6kl_sdio_cleanup_scat_resource(ar_sdio); |
| 665 | } |
| 666 | |
| 667 | static const struct ath6kl_hif_ops ath6kl_sdio_ops = { |
| 668 | .read_write_sync = ath6kl_sdio_read_write_sync, |
| 669 | .write_async = ath6kl_sdio_write_async, |
| 670 | .irq_enable = ath6kl_sdio_irq_enable, |
| 671 | .irq_disable = ath6kl_sdio_irq_disable, |
| 672 | .scatter_req_get = ath6kl_sdio_scatter_req_get, |
| 673 | .scatter_req_add = ath6kl_sdio_scatter_req_add, |
| 674 | .enable_scatter = ath6kl_sdio_enable_scatter, |
Vasanthakumar Thiagarajan | f74a736 | 2011-07-16 20:29:05 +0530 | [diff] [blame] | 675 | .scat_req_rw = ath6kl_sdio_async_rw_scatter, |
Kalle Valo | bdcd817 | 2011-07-18 00:22:30 +0300 | [diff] [blame] | 676 | .cleanup_scatter = ath6kl_sdio_cleanup_scatter, |
| 677 | }; |
| 678 | |
| 679 | static int ath6kl_sdio_probe(struct sdio_func *func, |
| 680 | const struct sdio_device_id *id) |
| 681 | { |
| 682 | int ret; |
| 683 | struct ath6kl_sdio *ar_sdio; |
| 684 | struct ath6kl *ar; |
| 685 | int count; |
| 686 | |
| 687 | ath6kl_dbg(ATH6KL_DBG_TRC, |
| 688 | "%s: func: 0x%X, vendor id: 0x%X, dev id: 0x%X, block size: 0x%X/0x%X\n", |
| 689 | __func__, func->num, func->vendor, |
| 690 | func->device, func->max_blksize, func->cur_blksize); |
| 691 | |
| 692 | ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL); |
| 693 | if (!ar_sdio) |
| 694 | return -ENOMEM; |
| 695 | |
| 696 | ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL); |
| 697 | if (!ar_sdio->dma_buffer) { |
| 698 | ret = -ENOMEM; |
| 699 | goto err_hif; |
| 700 | } |
| 701 | |
| 702 | ar_sdio->func = func; |
| 703 | sdio_set_drvdata(func, ar_sdio); |
| 704 | |
| 705 | ar_sdio->id = id; |
| 706 | ar_sdio->is_disabled = true; |
| 707 | |
| 708 | spin_lock_init(&ar_sdio->lock); |
| 709 | spin_lock_init(&ar_sdio->scat_lock); |
| 710 | spin_lock_init(&ar_sdio->wr_async_lock); |
| 711 | |
| 712 | INIT_LIST_HEAD(&ar_sdio->scat_req); |
| 713 | INIT_LIST_HEAD(&ar_sdio->bus_req_freeq); |
| 714 | INIT_LIST_HEAD(&ar_sdio->wr_asyncq); |
| 715 | |
| 716 | INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work); |
| 717 | |
| 718 | for (count = 0; count < BUS_REQUEST_MAX_NUM; count++) |
| 719 | ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]); |
| 720 | |
| 721 | ar = ath6kl_core_alloc(&ar_sdio->func->dev); |
| 722 | if (!ar) { |
| 723 | ath6kl_err("Failed to alloc ath6kl core\n"); |
| 724 | ret = -ENOMEM; |
| 725 | goto err_dma; |
| 726 | } |
| 727 | |
| 728 | ar_sdio->ar = ar; |
| 729 | ar->hif_priv = ar_sdio; |
| 730 | ar->hif_ops = &ath6kl_sdio_ops; |
| 731 | |
| 732 | ath6kl_sdio_set_mbox_info(ar); |
| 733 | |
| 734 | sdio_claim_host(func); |
| 735 | |
| 736 | if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >= |
| 737 | MANUFACTURER_ID_AR6003_BASE) { |
| 738 | /* enable 4-bit ASYNC interrupt on AR6003 or later */ |
| 739 | ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card, |
| 740 | CCCR_SDIO_IRQ_MODE_REG, |
| 741 | SDIO_IRQ_MODE_ASYNC_4BIT_IRQ); |
| 742 | if (ret) { |
| 743 | ath6kl_err("Failed to enable 4-bit async irq mode %d\n", |
| 744 | ret); |
| 745 | sdio_release_host(func); |
| 746 | goto err_dma; |
| 747 | } |
| 748 | |
| 749 | ath6kl_dbg(ATH6KL_DBG_TRC, "4-bit async irq mode enabled\n"); |
| 750 | } |
| 751 | |
| 752 | /* give us some time to enable, in ms */ |
| 753 | func->enable_timeout = 100; |
| 754 | |
| 755 | sdio_release_host(func); |
| 756 | |
| 757 | ret = ath6kl_sdio_power_on(ar_sdio); |
| 758 | if (ret) |
| 759 | goto err_dma; |
| 760 | |
| 761 | sdio_claim_host(func); |
| 762 | |
| 763 | ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE); |
| 764 | if (ret) { |
| 765 | ath6kl_err("Set sdio block size %d failed: %d)\n", |
| 766 | HIF_MBOX_BLOCK_SIZE, ret); |
| 767 | sdio_release_host(func); |
| 768 | goto err_off; |
| 769 | } |
| 770 | |
| 771 | sdio_release_host(func); |
| 772 | |
| 773 | ret = ath6kl_core_init(ar); |
| 774 | if (ret) { |
| 775 | ath6kl_err("Failed to init ath6kl core\n"); |
| 776 | goto err_off; |
| 777 | } |
| 778 | |
| 779 | return ret; |
| 780 | |
| 781 | err_off: |
| 782 | ath6kl_sdio_power_off(ar_sdio); |
| 783 | err_dma: |
| 784 | kfree(ar_sdio->dma_buffer); |
| 785 | err_hif: |
| 786 | kfree(ar_sdio); |
| 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | static void ath6kl_sdio_remove(struct sdio_func *func) |
| 792 | { |
| 793 | struct ath6kl_sdio *ar_sdio; |
| 794 | |
| 795 | ar_sdio = sdio_get_drvdata(func); |
| 796 | |
| 797 | ath6kl_stop_txrx(ar_sdio->ar); |
| 798 | cancel_work_sync(&ar_sdio->wr_async_work); |
| 799 | |
| 800 | ath6kl_unavail_ev(ar_sdio->ar); |
| 801 | |
| 802 | ath6kl_sdio_power_off(ar_sdio); |
| 803 | |
| 804 | kfree(ar_sdio->dma_buffer); |
| 805 | kfree(ar_sdio); |
| 806 | } |
| 807 | |
| 808 | static const struct sdio_device_id ath6kl_sdio_devices[] = { |
| 809 | {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))}, |
| 810 | {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))}, |
| 811 | {}, |
| 812 | }; |
| 813 | |
| 814 | MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices); |
| 815 | |
| 816 | static struct sdio_driver ath6kl_sdio_driver = { |
| 817 | .name = "ath6kl_sdio", |
| 818 | .id_table = ath6kl_sdio_devices, |
| 819 | .probe = ath6kl_sdio_probe, |
| 820 | .remove = ath6kl_sdio_remove, |
| 821 | }; |
| 822 | |
| 823 | static int __init ath6kl_sdio_init(void) |
| 824 | { |
| 825 | int ret; |
| 826 | |
| 827 | ret = sdio_register_driver(&ath6kl_sdio_driver); |
| 828 | if (ret) |
| 829 | ath6kl_err("sdio driver registration failed: %d\n", ret); |
| 830 | |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static void __exit ath6kl_sdio_exit(void) |
| 835 | { |
| 836 | sdio_unregister_driver(&ath6kl_sdio_driver); |
| 837 | } |
| 838 | |
| 839 | module_init(ath6kl_sdio_init); |
| 840 | module_exit(ath6kl_sdio_exit); |
| 841 | |
| 842 | MODULE_AUTHOR("Atheros Communications, Inc."); |
| 843 | MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices"); |
| 844 | MODULE_LICENSE("Dual BSD/GPL"); |
| 845 | |
| 846 | MODULE_FIRMWARE(AR6003_REV2_OTP_FILE); |
| 847 | MODULE_FIRMWARE(AR6003_REV2_FIRMWARE_FILE); |
| 848 | MODULE_FIRMWARE(AR6003_REV2_PATCH_FILE); |
| 849 | MODULE_FIRMWARE(AR6003_REV2_BOARD_DATA_FILE); |
| 850 | MODULE_FIRMWARE(AR6003_REV2_DEFAULT_BOARD_DATA_FILE); |
| 851 | MODULE_FIRMWARE(AR6003_REV3_OTP_FILE); |
| 852 | MODULE_FIRMWARE(AR6003_REV3_FIRMWARE_FILE); |
| 853 | MODULE_FIRMWARE(AR6003_REV3_PATCH_FILE); |
| 854 | MODULE_FIRMWARE(AR6003_REV3_BOARD_DATA_FILE); |
| 855 | MODULE_FIRMWARE(AR6003_REV3_DEFAULT_BOARD_DATA_FILE); |