sdio.c 36 KB

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