sdio.c 37 KB

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