sdio.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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 "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. /* protects access to dma_buffer */
  39. struct mutex dma_buffer_mutex;
  40. /* scatter request list head */
  41. struct list_head scat_req;
  42. /* Avoids disabling irq while the interrupts being handled */
  43. struct mutex mtx_irq;
  44. spinlock_t scat_lock;
  45. bool scatter_enabled;
  46. bool is_disabled;
  47. const struct sdio_device_id *id;
  48. struct work_struct wr_async_work;
  49. struct list_head wr_asyncq;
  50. spinlock_t wr_async_lock;
  51. };
  52. #define CMD53_ARG_READ 0
  53. #define CMD53_ARG_WRITE 1
  54. #define CMD53_ARG_BLOCK_BASIS 1
  55. #define CMD53_ARG_FIXED_ADDRESS 0
  56. #define CMD53_ARG_INCR_ADDRESS 1
  57. static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
  58. {
  59. return ar->hif_priv;
  60. }
  61. /*
  62. * Macro to check if DMA buffer is WORD-aligned and DMA-able.
  63. * Most host controllers assume the buffer is DMA'able and will
  64. * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
  65. * check fails on stack memory.
  66. */
  67. static inline bool buf_needs_bounce(u8 *buf)
  68. {
  69. return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
  70. }
  71. static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
  72. {
  73. struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
  74. /* EP1 has an extended range */
  75. mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
  76. mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
  77. mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
  78. mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
  79. mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
  80. mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
  81. }
  82. static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
  83. u8 mode, u8 opcode, u32 addr,
  84. u16 blksz)
  85. {
  86. *arg = (((rw & 1) << 31) |
  87. ((func & 0x7) << 28) |
  88. ((mode & 1) << 27) |
  89. ((opcode & 1) << 26) |
  90. ((addr & 0x1FFFF) << 9) |
  91. (blksz & 0x1FF));
  92. }
  93. static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
  94. unsigned int address,
  95. unsigned char val)
  96. {
  97. const u8 func = 0;
  98. *arg = ((write & 1) << 31) |
  99. ((func & 0x7) << 28) |
  100. ((raw & 1) << 27) |
  101. (1 << 26) |
  102. ((address & 0x1FFFF) << 9) |
  103. (1 << 8) |
  104. (val & 0xFF);
  105. }
  106. static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
  107. unsigned int address,
  108. unsigned char byte)
  109. {
  110. struct mmc_command io_cmd;
  111. memset(&io_cmd, 0, sizeof(io_cmd));
  112. ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
  113. io_cmd.opcode = SD_IO_RW_DIRECT;
  114. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  115. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  116. }
  117. static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
  118. u8 *buf, u32 len)
  119. {
  120. int ret = 0;
  121. sdio_claim_host(func);
  122. if (request & HIF_WRITE) {
  123. /* FIXME: looks like ugly workaround for something */
  124. if (addr >= HIF_MBOX_BASE_ADDR &&
  125. addr <= HIF_MBOX_END_ADDR)
  126. addr += (HIF_MBOX_WIDTH - len);
  127. /* FIXME: this also looks like ugly workaround */
  128. if (addr == HIF_MBOX0_EXT_BASE_ADDR)
  129. addr += HIF_MBOX0_EXT_WIDTH - len;
  130. if (request & HIF_FIXED_ADDRESS)
  131. ret = sdio_writesb(func, addr, buf, len);
  132. else
  133. ret = sdio_memcpy_toio(func, addr, buf, len);
  134. } else {
  135. if (request & HIF_FIXED_ADDRESS)
  136. ret = sdio_readsb(func, buf, addr, len);
  137. else
  138. ret = sdio_memcpy_fromio(func, buf, addr, len);
  139. }
  140. sdio_release_host(func);
  141. ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
  142. request & HIF_WRITE ? "wr" : "rd", addr,
  143. request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
  144. ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
  145. return ret;
  146. }
  147. static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
  148. {
  149. struct bus_request *bus_req;
  150. spin_lock_bh(&ar_sdio->lock);
  151. if (list_empty(&ar_sdio->bus_req_freeq)) {
  152. spin_unlock_bh(&ar_sdio->lock);
  153. return NULL;
  154. }
  155. bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
  156. struct bus_request, list);
  157. list_del(&bus_req->list);
  158. spin_unlock_bh(&ar_sdio->lock);
  159. ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
  160. __func__, bus_req);
  161. return bus_req;
  162. }
  163. static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
  164. struct bus_request *bus_req)
  165. {
  166. ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
  167. __func__, bus_req);
  168. spin_lock_bh(&ar_sdio->lock);
  169. list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
  170. spin_unlock_bh(&ar_sdio->lock);
  171. }
  172. static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
  173. struct mmc_data *data)
  174. {
  175. struct scatterlist *sg;
  176. int i;
  177. data->blksz = HIF_MBOX_BLOCK_SIZE;
  178. data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
  179. ath6kl_dbg(ATH6KL_DBG_SCATTER,
  180. "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
  181. (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
  182. data->blksz, data->blocks, scat_req->len,
  183. scat_req->scat_entries);
  184. data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
  185. MMC_DATA_READ;
  186. /* fill SG entries */
  187. sg = scat_req->sgentries;
  188. sg_init_table(sg, scat_req->scat_entries);
  189. /* assemble SG list */
  190. for (i = 0; i < scat_req->scat_entries; i++, sg++) {
  191. ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
  192. i, scat_req->scat_list[i].buf,
  193. scat_req->scat_list[i].len);
  194. sg_set_buf(sg, scat_req->scat_list[i].buf,
  195. scat_req->scat_list[i].len);
  196. }
  197. /* set scatter-gather table for request */
  198. data->sg = scat_req->sgentries;
  199. data->sg_len = scat_req->scat_entries;
  200. }
  201. static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
  202. struct bus_request *req)
  203. {
  204. struct mmc_request mmc_req;
  205. struct mmc_command cmd;
  206. struct mmc_data data;
  207. struct hif_scatter_req *scat_req;
  208. u8 opcode, rw;
  209. int status, len;
  210. scat_req = req->scat_req;
  211. if (scat_req->virt_scat) {
  212. len = scat_req->len;
  213. if (scat_req->req & HIF_BLOCK_BASIS)
  214. len = round_down(len, HIF_MBOX_BLOCK_SIZE);
  215. status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
  216. scat_req->addr, scat_req->virt_dma_buf,
  217. len);
  218. goto scat_complete;
  219. }
  220. memset(&mmc_req, 0, sizeof(struct mmc_request));
  221. memset(&cmd, 0, sizeof(struct mmc_command));
  222. memset(&data, 0, sizeof(struct mmc_data));
  223. ath6kl_sdio_setup_scat_data(scat_req, &data);
  224. opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
  225. CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
  226. rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
  227. /* Fixup the address so that the last byte will fall on MBOX EOM */
  228. if (scat_req->req & HIF_WRITE) {
  229. if (scat_req->addr == HIF_MBOX_BASE_ADDR)
  230. scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
  231. else
  232. /* Uses extended address range */
  233. scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
  234. }
  235. /* set command argument */
  236. ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
  237. CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
  238. data.blocks);
  239. cmd.opcode = SD_IO_RW_EXTENDED;
  240. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
  241. mmc_req.cmd = &cmd;
  242. mmc_req.data = &data;
  243. sdio_claim_host(ar_sdio->func);
  244. mmc_set_data_timeout(&data, ar_sdio->func->card);
  245. /* synchronous call to process request */
  246. mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
  247. sdio_release_host(ar_sdio->func);
  248. status = cmd.error ? cmd.error : data.error;
  249. scat_complete:
  250. scat_req->status = status;
  251. if (scat_req->status)
  252. ath6kl_err("Scatter write request failed:%d\n",
  253. scat_req->status);
  254. if (scat_req->req & HIF_ASYNCHRONOUS)
  255. scat_req->complete(ar_sdio->ar->htc_target, scat_req);
  256. return status;
  257. }
  258. static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
  259. int n_scat_entry, int n_scat_req,
  260. bool virt_scat)
  261. {
  262. struct hif_scatter_req *s_req;
  263. struct bus_request *bus_req;
  264. int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
  265. u8 *virt_buf;
  266. scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
  267. scat_req_sz = sizeof(*s_req) + scat_list_sz;
  268. if (!virt_scat)
  269. sg_sz = sizeof(struct scatterlist) * n_scat_entry;
  270. else
  271. buf_sz = 2 * L1_CACHE_BYTES +
  272. ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
  273. for (i = 0; i < n_scat_req; i++) {
  274. /* allocate the scatter request */
  275. s_req = kzalloc(scat_req_sz, GFP_KERNEL);
  276. if (!s_req)
  277. return -ENOMEM;
  278. if (virt_scat) {
  279. virt_buf = kzalloc(buf_sz, GFP_KERNEL);
  280. if (!virt_buf) {
  281. kfree(s_req);
  282. return -ENOMEM;
  283. }
  284. s_req->virt_dma_buf =
  285. (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
  286. } else {
  287. /* allocate sglist */
  288. s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
  289. if (!s_req->sgentries) {
  290. kfree(s_req);
  291. return -ENOMEM;
  292. }
  293. }
  294. /* allocate a bus request for this scatter request */
  295. bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
  296. if (!bus_req) {
  297. kfree(s_req->sgentries);
  298. kfree(s_req->virt_dma_buf);
  299. kfree(s_req);
  300. return -ENOMEM;
  301. }
  302. /* assign the scatter request to this bus request */
  303. bus_req->scat_req = s_req;
  304. s_req->busrequest = bus_req;
  305. s_req->virt_scat = virt_scat;
  306. /* add it to the scatter pool */
  307. hif_scatter_req_add(ar_sdio->ar, s_req);
  308. }
  309. return 0;
  310. }
  311. static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
  312. u32 len, u32 request)
  313. {
  314. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  315. u8 *tbuf = NULL;
  316. int ret;
  317. bool bounced = false;
  318. if (request & HIF_BLOCK_BASIS)
  319. len = round_down(len, HIF_MBOX_BLOCK_SIZE);
  320. if (buf_needs_bounce(buf)) {
  321. if (!ar_sdio->dma_buffer)
  322. return -ENOMEM;
  323. mutex_lock(&ar_sdio->dma_buffer_mutex);
  324. tbuf = ar_sdio->dma_buffer;
  325. memcpy(tbuf, buf, len);
  326. bounced = true;
  327. } else
  328. tbuf = buf;
  329. ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
  330. if ((request & HIF_READ) && bounced)
  331. memcpy(buf, tbuf, len);
  332. if (bounced)
  333. mutex_unlock(&ar_sdio->dma_buffer_mutex);
  334. return ret;
  335. }
  336. static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
  337. struct bus_request *req)
  338. {
  339. if (req->scat_req)
  340. ath6kl_sdio_scat_rw(ar_sdio, req);
  341. else {
  342. void *context;
  343. int status;
  344. status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
  345. req->buffer, req->length,
  346. req->request);
  347. context = req->packet;
  348. ath6kl_sdio_free_bus_req(ar_sdio, req);
  349. ath6kl_hif_rw_comp_handler(context, status);
  350. }
  351. }
  352. static void ath6kl_sdio_write_async_work(struct work_struct *work)
  353. {
  354. struct ath6kl_sdio *ar_sdio;
  355. struct bus_request *req, *tmp_req;
  356. ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
  357. spin_lock_bh(&ar_sdio->wr_async_lock);
  358. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  359. list_del(&req->list);
  360. spin_unlock_bh(&ar_sdio->wr_async_lock);
  361. __ath6kl_sdio_write_async(ar_sdio, req);
  362. spin_lock_bh(&ar_sdio->wr_async_lock);
  363. }
  364. spin_unlock_bh(&ar_sdio->wr_async_lock);
  365. }
  366. static void ath6kl_sdio_irq_handler(struct sdio_func *func)
  367. {
  368. int status;
  369. struct ath6kl_sdio *ar_sdio;
  370. ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
  371. ar_sdio = sdio_get_drvdata(func);
  372. mutex_lock(&ar_sdio->mtx_irq);
  373. /*
  374. * Release the host during interrups so we can pick it back up when
  375. * we process commands.
  376. */
  377. sdio_release_host(ar_sdio->func);
  378. status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
  379. sdio_claim_host(ar_sdio->func);
  380. mutex_unlock(&ar_sdio->mtx_irq);
  381. WARN_ON(status && status != -ECANCELED);
  382. }
  383. static int ath6kl_sdio_power_on(struct ath6kl *ar)
  384. {
  385. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  386. struct sdio_func *func = ar_sdio->func;
  387. int ret = 0;
  388. if (!ar_sdio->is_disabled)
  389. return 0;
  390. ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
  391. sdio_claim_host(func);
  392. ret = sdio_enable_func(func);
  393. if (ret) {
  394. ath6kl_err("Unable to enable sdio func: %d)\n", ret);
  395. sdio_release_host(func);
  396. return ret;
  397. }
  398. sdio_release_host(func);
  399. /*
  400. * Wait for hardware to initialise. It should take a lot less than
  401. * 10 ms but let's be conservative here.
  402. */
  403. msleep(10);
  404. ar_sdio->is_disabled = false;
  405. return ret;
  406. }
  407. static int ath6kl_sdio_power_off(struct ath6kl *ar)
  408. {
  409. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  410. int ret;
  411. if (ar_sdio->is_disabled)
  412. return 0;
  413. ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
  414. /* Disable the card */
  415. sdio_claim_host(ar_sdio->func);
  416. ret = sdio_disable_func(ar_sdio->func);
  417. sdio_release_host(ar_sdio->func);
  418. if (ret)
  419. return ret;
  420. ar_sdio->is_disabled = true;
  421. return ret;
  422. }
  423. static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
  424. u32 length, u32 request,
  425. struct htc_packet *packet)
  426. {
  427. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  428. struct bus_request *bus_req;
  429. bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
  430. if (!bus_req)
  431. return -ENOMEM;
  432. bus_req->address = address;
  433. bus_req->buffer = buffer;
  434. bus_req->length = length;
  435. bus_req->request = request;
  436. bus_req->packet = packet;
  437. spin_lock_bh(&ar_sdio->wr_async_lock);
  438. list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
  439. spin_unlock_bh(&ar_sdio->wr_async_lock);
  440. queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
  441. return 0;
  442. }
  443. static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
  444. {
  445. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  446. int ret;
  447. sdio_claim_host(ar_sdio->func);
  448. /* Register the isr */
  449. ret = sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
  450. if (ret)
  451. ath6kl_err("Failed to claim sdio irq: %d\n", ret);
  452. sdio_release_host(ar_sdio->func);
  453. }
  454. static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
  455. {
  456. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  457. int ret;
  458. sdio_claim_host(ar_sdio->func);
  459. mutex_lock(&ar_sdio->mtx_irq);
  460. ret = sdio_release_irq(ar_sdio->func);
  461. if (ret)
  462. ath6kl_err("Failed to release sdio irq: %d\n", ret);
  463. mutex_unlock(&ar_sdio->mtx_irq);
  464. sdio_release_host(ar_sdio->func);
  465. }
  466. static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
  467. {
  468. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  469. struct hif_scatter_req *node = NULL;
  470. spin_lock_bh(&ar_sdio->scat_lock);
  471. if (!list_empty(&ar_sdio->scat_req)) {
  472. node = list_first_entry(&ar_sdio->scat_req,
  473. struct hif_scatter_req, list);
  474. list_del(&node->list);
  475. }
  476. spin_unlock_bh(&ar_sdio->scat_lock);
  477. return node;
  478. }
  479. static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
  480. struct hif_scatter_req *s_req)
  481. {
  482. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  483. spin_lock_bh(&ar_sdio->scat_lock);
  484. list_add_tail(&s_req->list, &ar_sdio->scat_req);
  485. spin_unlock_bh(&ar_sdio->scat_lock);
  486. }
  487. /* scatter gather read write request */
  488. static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
  489. struct hif_scatter_req *scat_req)
  490. {
  491. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  492. u32 request = scat_req->req;
  493. int status = 0;
  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. status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
  501. else {
  502. spin_lock_bh(&ar_sdio->wr_async_lock);
  503. list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
  504. spin_unlock_bh(&ar_sdio->wr_async_lock);
  505. queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
  506. }
  507. return status;
  508. }
  509. /* clean up scatter support */
  510. static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
  511. {
  512. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  513. struct hif_scatter_req *s_req, *tmp_req;
  514. /* empty the free list */
  515. spin_lock_bh(&ar_sdio->scat_lock);
  516. list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
  517. list_del(&s_req->list);
  518. spin_unlock_bh(&ar_sdio->scat_lock);
  519. /*
  520. * FIXME: should we also call completion handler with
  521. * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
  522. * that the packet is properly freed?
  523. */
  524. if (s_req->busrequest)
  525. ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
  526. kfree(s_req->virt_dma_buf);
  527. kfree(s_req->sgentries);
  528. kfree(s_req);
  529. spin_lock_bh(&ar_sdio->scat_lock);
  530. }
  531. spin_unlock_bh(&ar_sdio->scat_lock);
  532. }
  533. /* setup of HIF scatter resources */
  534. static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
  535. {
  536. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  537. struct htc_target *target = ar->htc_target;
  538. int ret;
  539. bool virt_scat = false;
  540. if (ar_sdio->scatter_enabled)
  541. return 0;
  542. ar_sdio->scatter_enabled = true;
  543. /* check if host supports scatter and it meets our requirements */
  544. if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
  545. ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
  546. ar_sdio->func->card->host->max_segs,
  547. MAX_SCATTER_ENTRIES_PER_REQ);
  548. virt_scat = true;
  549. }
  550. if (!virt_scat) {
  551. ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
  552. MAX_SCATTER_ENTRIES_PER_REQ,
  553. MAX_SCATTER_REQUESTS, virt_scat);
  554. if (!ret) {
  555. ath6kl_dbg(ATH6KL_DBG_BOOT,
  556. "hif-scatter enabled requests %d entries %d\n",
  557. MAX_SCATTER_REQUESTS,
  558. MAX_SCATTER_ENTRIES_PER_REQ);
  559. target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
  560. target->max_xfer_szper_scatreq =
  561. MAX_SCATTER_REQ_TRANSFER_SIZE;
  562. } else {
  563. ath6kl_sdio_cleanup_scatter(ar);
  564. ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
  565. }
  566. }
  567. if (virt_scat || ret) {
  568. ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
  569. ATH6KL_SCATTER_ENTRIES_PER_REQ,
  570. ATH6KL_SCATTER_REQS, virt_scat);
  571. if (ret) {
  572. ath6kl_err("failed to alloc virtual scatter resources !\n");
  573. ath6kl_sdio_cleanup_scatter(ar);
  574. return ret;
  575. }
  576. ath6kl_dbg(ATH6KL_DBG_BOOT,
  577. "virtual scatter enabled requests %d entries %d\n",
  578. ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
  579. target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
  580. target->max_xfer_szper_scatreq =
  581. ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
  582. }
  583. return 0;
  584. }
  585. static int ath6kl_sdio_config(struct ath6kl *ar)
  586. {
  587. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  588. struct sdio_func *func = ar_sdio->func;
  589. int ret;
  590. sdio_claim_host(func);
  591. if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
  592. MANUFACTURER_ID_AR6003_BASE) {
  593. /* enable 4-bit ASYNC interrupt on AR6003 or later */
  594. ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
  595. CCCR_SDIO_IRQ_MODE_REG,
  596. SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
  597. if (ret) {
  598. ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
  599. ret);
  600. goto out;
  601. }
  602. ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
  603. }
  604. /* give us some time to enable, in ms */
  605. func->enable_timeout = 100;
  606. ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
  607. if (ret) {
  608. ath6kl_err("Set sdio block size %d failed: %d)\n",
  609. HIF_MBOX_BLOCK_SIZE, ret);
  610. goto out;
  611. }
  612. out:
  613. sdio_release_host(func);
  614. return ret;
  615. }
  616. static int ath6kl_set_sdio_pm_caps(struct ath6kl *ar)
  617. {
  618. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  619. struct sdio_func *func = ar_sdio->func;
  620. mmc_pm_flag_t flags;
  621. int ret;
  622. flags = sdio_get_host_pm_caps(func);
  623. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
  624. if (!(flags & MMC_PM_WAKE_SDIO_IRQ) ||
  625. !(flags & MMC_PM_KEEP_POWER))
  626. return -EINVAL;
  627. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  628. if (ret) {
  629. ath6kl_err("set sdio keep pwr flag failed: %d\n", ret);
  630. return ret;
  631. }
  632. /* sdio irq wakes up host */
  633. ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
  634. if (ret)
  635. ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
  636. return ret;
  637. }
  638. static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
  639. {
  640. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  641. struct sdio_func *func = ar_sdio->func;
  642. mmc_pm_flag_t flags;
  643. int ret;
  644. if (ar->state == ATH6KL_STATE_SCHED_SCAN) {
  645. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sched scan is in progress\n");
  646. ret = ath6kl_set_sdio_pm_caps(ar);
  647. if (ret)
  648. goto cut_pwr;
  649. ret = ath6kl_cfg80211_suspend(ar,
  650. ATH6KL_CFG_SUSPEND_SCHED_SCAN,
  651. NULL);
  652. if (ret)
  653. goto cut_pwr;
  654. return 0;
  655. }
  656. if (ar->suspend_mode == WLAN_POWER_STATE_WOW ||
  657. (!ar->suspend_mode && wow)) {
  658. ret = ath6kl_set_sdio_pm_caps(ar);
  659. if (ret)
  660. goto cut_pwr;
  661. ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
  662. if (ret)
  663. goto cut_pwr;
  664. return 0;
  665. }
  666. if (ar->suspend_mode == WLAN_POWER_STATE_DEEP_SLEEP ||
  667. !ar->suspend_mode) {
  668. flags = sdio_get_host_pm_caps(func);
  669. if (!(flags & MMC_PM_KEEP_POWER))
  670. goto cut_pwr;
  671. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  672. if (ret)
  673. goto cut_pwr;
  674. /*
  675. * Workaround to support Deep Sleep with MSM, set the host pm
  676. * flag as MMC_PM_WAKE_SDIO_IRQ to allow SDCC deiver to disable
  677. * the sdc2_clock and internally allows MSM to enter
  678. * TCXO shutdown properly.
  679. */
  680. if ((flags & MMC_PM_WAKE_SDIO_IRQ)) {
  681. ret = sdio_set_host_pm_flags(func,
  682. MMC_PM_WAKE_SDIO_IRQ);
  683. if (ret)
  684. goto cut_pwr;
  685. }
  686. ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP,
  687. NULL);
  688. if (ret)
  689. goto cut_pwr;
  690. return 0;
  691. }
  692. cut_pwr:
  693. return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER, NULL);
  694. }
  695. static int ath6kl_sdio_resume(struct ath6kl *ar)
  696. {
  697. switch (ar->state) {
  698. case ATH6KL_STATE_OFF:
  699. case ATH6KL_STATE_CUTPOWER:
  700. ath6kl_dbg(ATH6KL_DBG_SUSPEND,
  701. "sdio resume configuring sdio\n");
  702. /* need to set sdio settings after power is cut from sdio */
  703. ath6kl_sdio_config(ar);
  704. break;
  705. case ATH6KL_STATE_ON:
  706. break;
  707. case ATH6KL_STATE_DEEPSLEEP:
  708. break;
  709. case ATH6KL_STATE_WOW:
  710. break;
  711. case ATH6KL_STATE_SCHED_SCAN:
  712. break;
  713. }
  714. ath6kl_cfg80211_resume(ar);
  715. return 0;
  716. }
  717. /* set the window address register (using 4-byte register access ). */
  718. static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
  719. {
  720. int status;
  721. u8 addr_val[4];
  722. s32 i;
  723. /*
  724. * Write bytes 1,2,3 of the register to set the upper address bytes,
  725. * the LSB is written last to initiate the access cycle
  726. */
  727. for (i = 1; i <= 3; i++) {
  728. /*
  729. * Fill the buffer with the address byte value we want to
  730. * hit 4 times.
  731. */
  732. memset(addr_val, ((u8 *)&addr)[i], 4);
  733. /*
  734. * Hit each byte of the register address with a 4-byte
  735. * write operation to the same address, this is a harmless
  736. * operation.
  737. */
  738. status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
  739. 4, HIF_WR_SYNC_BYTE_FIX);
  740. if (status)
  741. break;
  742. }
  743. if (status) {
  744. ath6kl_err("%s: failed to write initial bytes of 0x%x "
  745. "to window reg: 0x%X\n", __func__,
  746. addr, reg_addr);
  747. return status;
  748. }
  749. /*
  750. * Write the address register again, this time write the whole
  751. * 4-byte value. The effect here is that the LSB write causes the
  752. * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
  753. * effect since we are writing the same values again
  754. */
  755. status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
  756. 4, HIF_WR_SYNC_BYTE_INC);
  757. if (status) {
  758. ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
  759. __func__, addr, reg_addr);
  760. return status;
  761. }
  762. return 0;
  763. }
  764. static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
  765. {
  766. int status;
  767. /* set window register to start read cycle */
  768. status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
  769. address);
  770. if (status)
  771. return status;
  772. /* read the data */
  773. status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
  774. (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
  775. if (status) {
  776. ath6kl_err("%s: failed to read from window data addr\n",
  777. __func__);
  778. return status;
  779. }
  780. return status;
  781. }
  782. static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
  783. __le32 data)
  784. {
  785. int status;
  786. u32 val = (__force u32) data;
  787. /* set write data */
  788. status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
  789. (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
  790. if (status) {
  791. ath6kl_err("%s: failed to write 0x%x to window data addr\n",
  792. __func__, data);
  793. return status;
  794. }
  795. /* set window register, which starts the write cycle */
  796. return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
  797. address);
  798. }
  799. static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
  800. {
  801. u32 addr;
  802. unsigned long timeout;
  803. int ret;
  804. ar->bmi.cmd_credits = 0;
  805. /* Read the counter register to get the command credits */
  806. addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
  807. timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
  808. while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
  809. /*
  810. * Hit the credit counter with a 4-byte access, the first byte
  811. * read will hit the counter and cause a decrement, while the
  812. * remaining 3 bytes has no effect. The rationale behind this
  813. * is to make all HIF accesses 4-byte aligned.
  814. */
  815. ret = ath6kl_sdio_read_write_sync(ar, addr,
  816. (u8 *)&ar->bmi.cmd_credits, 4,
  817. HIF_RD_SYNC_BYTE_INC);
  818. if (ret) {
  819. ath6kl_err("Unable to decrement the command credit "
  820. "count register: %d\n", ret);
  821. return ret;
  822. }
  823. /* The counter is only 8 bits.
  824. * Ignore anything in the upper 3 bytes
  825. */
  826. ar->bmi.cmd_credits &= 0xFF;
  827. }
  828. if (!ar->bmi.cmd_credits) {
  829. ath6kl_err("bmi communication timeout\n");
  830. return -ETIMEDOUT;
  831. }
  832. return 0;
  833. }
  834. static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
  835. {
  836. unsigned long timeout;
  837. u32 rx_word = 0;
  838. int ret = 0;
  839. timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
  840. while ((time_before(jiffies, timeout)) && !rx_word) {
  841. ret = ath6kl_sdio_read_write_sync(ar,
  842. RX_LOOKAHEAD_VALID_ADDRESS,
  843. (u8 *)&rx_word, sizeof(rx_word),
  844. HIF_RD_SYNC_BYTE_INC);
  845. if (ret) {
  846. ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
  847. return ret;
  848. }
  849. /* all we really want is one bit */
  850. rx_word &= (1 << ENDPOINT1);
  851. }
  852. if (!rx_word) {
  853. ath6kl_err("bmi_recv_buf FIFO empty\n");
  854. return -EINVAL;
  855. }
  856. return ret;
  857. }
  858. static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
  859. {
  860. int ret;
  861. u32 addr;
  862. ret = ath6kl_sdio_bmi_credits(ar);
  863. if (ret)
  864. return ret;
  865. addr = ar->mbox_info.htc_addr;
  866. ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
  867. HIF_WR_SYNC_BYTE_INC);
  868. if (ret)
  869. ath6kl_err("unable to send the bmi data to the device\n");
  870. return ret;
  871. }
  872. static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
  873. {
  874. int ret;
  875. u32 addr;
  876. /*
  877. * During normal bootup, small reads may be required.
  878. * Rather than issue an HIF Read and then wait as the Target
  879. * adds successive bytes to the FIFO, we wait here until
  880. * we know that response data is available.
  881. *
  882. * This allows us to cleanly timeout on an unexpected
  883. * Target failure rather than risk problems at the HIF level.
  884. * In particular, this avoids SDIO timeouts and possibly garbage
  885. * data on some host controllers. And on an interconnect
  886. * such as Compact Flash (as well as some SDIO masters) which
  887. * does not provide any indication on data timeout, it avoids
  888. * a potential hang or garbage response.
  889. *
  890. * Synchronization is more difficult for reads larger than the
  891. * size of the MBOX FIFO (128B), because the Target is unable
  892. * to push the 129th byte of data until AFTER the Host posts an
  893. * HIF Read and removes some FIFO data. So for large reads the
  894. * Host proceeds to post an HIF Read BEFORE all the data is
  895. * actually available to read. Fortunately, large BMI reads do
  896. * not occur in practice -- they're supported for debug/development.
  897. *
  898. * So Host/Target BMI synchronization is divided into these cases:
  899. * CASE 1: length < 4
  900. * Should not happen
  901. *
  902. * CASE 2: 4 <= length <= 128
  903. * Wait for first 4 bytes to be in FIFO
  904. * If CONSERVATIVE_BMI_READ is enabled, also wait for
  905. * a BMI command credit, which indicates that the ENTIRE
  906. * response is available in the the FIFO
  907. *
  908. * CASE 3: length > 128
  909. * Wait for the first 4 bytes to be in FIFO
  910. *
  911. * For most uses, a small timeout should be sufficient and we will
  912. * usually see a response quickly; but there may be some unusual
  913. * (debug) cases of BMI_EXECUTE where we want an larger timeout.
  914. * For now, we use an unbounded busy loop while waiting for
  915. * BMI_EXECUTE.
  916. *
  917. * If BMI_EXECUTE ever needs to support longer-latency execution,
  918. * especially in production, this code needs to be enhanced to sleep
  919. * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
  920. * a function of Host processor speed.
  921. */
  922. if (len >= 4) { /* NB: Currently, always true */
  923. ret = ath6kl_bmi_get_rx_lkahd(ar);
  924. if (ret)
  925. return ret;
  926. }
  927. addr = ar->mbox_info.htc_addr;
  928. ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
  929. HIF_RD_SYNC_BYTE_INC);
  930. if (ret) {
  931. ath6kl_err("Unable to read the bmi data from the device: %d\n",
  932. ret);
  933. return ret;
  934. }
  935. return 0;
  936. }
  937. static void ath6kl_sdio_stop(struct ath6kl *ar)
  938. {
  939. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  940. struct bus_request *req, *tmp_req;
  941. void *context;
  942. /* FIXME: make sure that wq is not queued again */
  943. cancel_work_sync(&ar_sdio->wr_async_work);
  944. spin_lock_bh(&ar_sdio->wr_async_lock);
  945. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  946. list_del(&req->list);
  947. if (req->scat_req) {
  948. /* this is a scatter gather request */
  949. req->scat_req->status = -ECANCELED;
  950. req->scat_req->complete(ar_sdio->ar->htc_target,
  951. req->scat_req);
  952. } else {
  953. context = req->packet;
  954. ath6kl_sdio_free_bus_req(ar_sdio, req);
  955. ath6kl_hif_rw_comp_handler(context, -ECANCELED);
  956. }
  957. }
  958. spin_unlock_bh(&ar_sdio->wr_async_lock);
  959. WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
  960. }
  961. static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
  962. .read_write_sync = ath6kl_sdio_read_write_sync,
  963. .write_async = ath6kl_sdio_write_async,
  964. .irq_enable = ath6kl_sdio_irq_enable,
  965. .irq_disable = ath6kl_sdio_irq_disable,
  966. .scatter_req_get = ath6kl_sdio_scatter_req_get,
  967. .scatter_req_add = ath6kl_sdio_scatter_req_add,
  968. .enable_scatter = ath6kl_sdio_enable_scatter,
  969. .scat_req_rw = ath6kl_sdio_async_rw_scatter,
  970. .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
  971. .suspend = ath6kl_sdio_suspend,
  972. .resume = ath6kl_sdio_resume,
  973. .diag_read32 = ath6kl_sdio_diag_read32,
  974. .diag_write32 = ath6kl_sdio_diag_write32,
  975. .bmi_read = ath6kl_sdio_bmi_read,
  976. .bmi_write = ath6kl_sdio_bmi_write,
  977. .power_on = ath6kl_sdio_power_on,
  978. .power_off = ath6kl_sdio_power_off,
  979. .stop = ath6kl_sdio_stop,
  980. };
  981. #ifdef CONFIG_PM_SLEEP
  982. /*
  983. * Empty handlers so that mmc subsystem doesn't remove us entirely during
  984. * suspend. We instead follow cfg80211 suspend/resume handlers.
  985. */
  986. static int ath6kl_sdio_pm_suspend(struct device *device)
  987. {
  988. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
  989. return 0;
  990. }
  991. static int ath6kl_sdio_pm_resume(struct device *device)
  992. {
  993. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
  994. return 0;
  995. }
  996. static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
  997. ath6kl_sdio_pm_resume);
  998. #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
  999. #else
  1000. #define ATH6KL_SDIO_PM_OPS NULL
  1001. #endif /* CONFIG_PM_SLEEP */
  1002. static int ath6kl_sdio_probe(struct sdio_func *func,
  1003. const struct sdio_device_id *id)
  1004. {
  1005. int ret;
  1006. struct ath6kl_sdio *ar_sdio;
  1007. struct ath6kl *ar;
  1008. int count;
  1009. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1010. "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
  1011. func->num, func->vendor, func->device,
  1012. func->max_blksize, func->cur_blksize);
  1013. ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
  1014. if (!ar_sdio)
  1015. return -ENOMEM;
  1016. ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
  1017. if (!ar_sdio->dma_buffer) {
  1018. ret = -ENOMEM;
  1019. goto err_hif;
  1020. }
  1021. ar_sdio->func = func;
  1022. sdio_set_drvdata(func, ar_sdio);
  1023. ar_sdio->id = id;
  1024. ar_sdio->is_disabled = true;
  1025. spin_lock_init(&ar_sdio->lock);
  1026. spin_lock_init(&ar_sdio->scat_lock);
  1027. spin_lock_init(&ar_sdio->wr_async_lock);
  1028. mutex_init(&ar_sdio->dma_buffer_mutex);
  1029. mutex_init(&ar_sdio->mtx_irq);
  1030. INIT_LIST_HEAD(&ar_sdio->scat_req);
  1031. INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
  1032. INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
  1033. INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
  1034. for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
  1035. ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
  1036. ar = ath6kl_core_create(&ar_sdio->func->dev);
  1037. if (!ar) {
  1038. ath6kl_err("Failed to alloc ath6kl core\n");
  1039. ret = -ENOMEM;
  1040. goto err_dma;
  1041. }
  1042. ar_sdio->ar = ar;
  1043. ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
  1044. ar->hif_priv = ar_sdio;
  1045. ar->hif_ops = &ath6kl_sdio_ops;
  1046. ar->bmi.max_data_size = 256;
  1047. ath6kl_sdio_set_mbox_info(ar);
  1048. ret = ath6kl_sdio_config(ar);
  1049. if (ret) {
  1050. ath6kl_err("Failed to config sdio: %d\n", ret);
  1051. goto err_core_alloc;
  1052. }
  1053. ret = ath6kl_core_init(ar);
  1054. if (ret) {
  1055. ath6kl_err("Failed to init ath6kl core\n");
  1056. goto err_core_alloc;
  1057. }
  1058. return ret;
  1059. err_core_alloc:
  1060. ath6kl_core_destroy(ar_sdio->ar);
  1061. err_dma:
  1062. kfree(ar_sdio->dma_buffer);
  1063. err_hif:
  1064. kfree(ar_sdio);
  1065. return ret;
  1066. }
  1067. static void ath6kl_sdio_remove(struct sdio_func *func)
  1068. {
  1069. struct ath6kl_sdio *ar_sdio;
  1070. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1071. "sdio removed func %d vendor 0x%x device 0x%x\n",
  1072. func->num, func->vendor, func->device);
  1073. ar_sdio = sdio_get_drvdata(func);
  1074. ath6kl_stop_txrx(ar_sdio->ar);
  1075. cancel_work_sync(&ar_sdio->wr_async_work);
  1076. ath6kl_core_cleanup(ar_sdio->ar);
  1077. ath6kl_core_destroy(ar_sdio->ar);
  1078. kfree(ar_sdio->dma_buffer);
  1079. kfree(ar_sdio);
  1080. }
  1081. static const struct sdio_device_id ath6kl_sdio_devices[] = {
  1082. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
  1083. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
  1084. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
  1085. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
  1086. {},
  1087. };
  1088. MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
  1089. static struct sdio_driver ath6kl_sdio_driver = {
  1090. .name = "ath6kl_sdio",
  1091. .id_table = ath6kl_sdio_devices,
  1092. .probe = ath6kl_sdio_probe,
  1093. .remove = ath6kl_sdio_remove,
  1094. .drv.pm = ATH6KL_SDIO_PM_OPS,
  1095. };
  1096. static int __init ath6kl_sdio_init(void)
  1097. {
  1098. int ret;
  1099. ret = sdio_register_driver(&ath6kl_sdio_driver);
  1100. if (ret)
  1101. ath6kl_err("sdio driver registration failed: %d\n", ret);
  1102. return ret;
  1103. }
  1104. static void __exit ath6kl_sdio_exit(void)
  1105. {
  1106. sdio_unregister_driver(&ath6kl_sdio_driver);
  1107. }
  1108. module_init(ath6kl_sdio_init);
  1109. module_exit(ath6kl_sdio_exit);
  1110. MODULE_AUTHOR("Atheros Communications, Inc.");
  1111. MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
  1112. MODULE_LICENSE("Dual BSD/GPL");
  1113. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_OTP_FILE);
  1114. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_FIRMWARE_FILE);
  1115. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_PATCH_FILE);
  1116. MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
  1117. MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
  1118. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_OTP_FILE);
  1119. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_FIRMWARE_FILE);
  1120. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_PATCH_FILE);
  1121. MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
  1122. MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);
  1123. MODULE_FIRMWARE(AR6004_HW_1_0_FW_DIR "/" AR6004_HW_1_0_FIRMWARE_FILE);
  1124. MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
  1125. MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
  1126. MODULE_FIRMWARE(AR6004_HW_1_1_FW_DIR "/" AR6004_HW_1_1_FIRMWARE_FILE);
  1127. MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
  1128. MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);