sdio.c 24 KB

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