sdio.c 37 KB

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