sdio.c 23 KB

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