sdio.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 (WARN_ON_ONCE(!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. if (func->card && func->card->host)
  721. func->card->host->pm_flags &= ~MMC_PM_KEEP_POWER;
  722. return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER, NULL);
  723. }
  724. static int ath6kl_sdio_resume(struct ath6kl *ar)
  725. {
  726. switch (ar->state) {
  727. case ATH6KL_STATE_OFF:
  728. case ATH6KL_STATE_CUTPOWER:
  729. ath6kl_dbg(ATH6KL_DBG_SUSPEND,
  730. "sdio resume configuring sdio\n");
  731. /* need to set sdio settings after power is cut from sdio */
  732. ath6kl_sdio_config(ar);
  733. break;
  734. case ATH6KL_STATE_ON:
  735. break;
  736. case ATH6KL_STATE_DEEPSLEEP:
  737. break;
  738. case ATH6KL_STATE_WOW:
  739. break;
  740. case ATH6KL_STATE_SCHED_SCAN:
  741. break;
  742. case ATH6KL_STATE_SUSPENDING:
  743. break;
  744. case ATH6KL_STATE_RESUMING:
  745. break;
  746. }
  747. ath6kl_cfg80211_resume(ar);
  748. return 0;
  749. }
  750. /* set the window address register (using 4-byte register access ). */
  751. static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
  752. {
  753. int status;
  754. u8 addr_val[4];
  755. s32 i;
  756. /*
  757. * Write bytes 1,2,3 of the register to set the upper address bytes,
  758. * the LSB is written last to initiate the access cycle
  759. */
  760. for (i = 1; i <= 3; i++) {
  761. /*
  762. * Fill the buffer with the address byte value we want to
  763. * hit 4 times.
  764. */
  765. memset(addr_val, ((u8 *)&addr)[i], 4);
  766. /*
  767. * Hit each byte of the register address with a 4-byte
  768. * write operation to the same address, this is a harmless
  769. * operation.
  770. */
  771. status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
  772. 4, HIF_WR_SYNC_BYTE_FIX);
  773. if (status)
  774. break;
  775. }
  776. if (status) {
  777. ath6kl_err("%s: failed to write initial bytes of 0x%x to window reg: 0x%X\n",
  778. __func__, addr, reg_addr);
  779. return status;
  780. }
  781. /*
  782. * Write the address register again, this time write the whole
  783. * 4-byte value. The effect here is that the LSB write causes the
  784. * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
  785. * effect since we are writing the same values again
  786. */
  787. status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
  788. 4, HIF_WR_SYNC_BYTE_INC);
  789. if (status) {
  790. ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
  791. __func__, addr, reg_addr);
  792. return status;
  793. }
  794. return 0;
  795. }
  796. static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
  797. {
  798. int status;
  799. /* set window register to start read cycle */
  800. status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
  801. address);
  802. if (status)
  803. return status;
  804. /* read the data */
  805. status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
  806. (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
  807. if (status) {
  808. ath6kl_err("%s: failed to read from window data addr\n",
  809. __func__);
  810. return status;
  811. }
  812. return status;
  813. }
  814. static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
  815. __le32 data)
  816. {
  817. int status;
  818. u32 val = (__force u32) data;
  819. /* set write data */
  820. status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
  821. (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
  822. if (status) {
  823. ath6kl_err("%s: failed to write 0x%x to window data addr\n",
  824. __func__, data);
  825. return status;
  826. }
  827. /* set window register, which starts the write cycle */
  828. return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
  829. address);
  830. }
  831. static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
  832. {
  833. u32 addr;
  834. unsigned long timeout;
  835. int ret;
  836. ar->bmi.cmd_credits = 0;
  837. /* Read the counter register to get the command credits */
  838. addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
  839. timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
  840. while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
  841. /*
  842. * Hit the credit counter with a 4-byte access, the first byte
  843. * read will hit the counter and cause a decrement, while the
  844. * remaining 3 bytes has no effect. The rationale behind this
  845. * is to make all HIF accesses 4-byte aligned.
  846. */
  847. ret = ath6kl_sdio_read_write_sync(ar, addr,
  848. (u8 *)&ar->bmi.cmd_credits, 4,
  849. HIF_RD_SYNC_BYTE_INC);
  850. if (ret) {
  851. ath6kl_err("Unable to decrement the command credit count register: %d\n",
  852. ret);
  853. return ret;
  854. }
  855. /* The counter is only 8 bits.
  856. * Ignore anything in the upper 3 bytes
  857. */
  858. ar->bmi.cmd_credits &= 0xFF;
  859. }
  860. if (!ar->bmi.cmd_credits) {
  861. ath6kl_err("bmi communication timeout\n");
  862. return -ETIMEDOUT;
  863. }
  864. return 0;
  865. }
  866. static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
  867. {
  868. unsigned long timeout;
  869. u32 rx_word = 0;
  870. int ret = 0;
  871. timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
  872. while ((time_before(jiffies, timeout)) && !rx_word) {
  873. ret = ath6kl_sdio_read_write_sync(ar,
  874. RX_LOOKAHEAD_VALID_ADDRESS,
  875. (u8 *)&rx_word, sizeof(rx_word),
  876. HIF_RD_SYNC_BYTE_INC);
  877. if (ret) {
  878. ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
  879. return ret;
  880. }
  881. /* all we really want is one bit */
  882. rx_word &= (1 << ENDPOINT1);
  883. }
  884. if (!rx_word) {
  885. ath6kl_err("bmi_recv_buf FIFO empty\n");
  886. return -EINVAL;
  887. }
  888. return ret;
  889. }
  890. static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
  891. {
  892. int ret;
  893. u32 addr;
  894. ret = ath6kl_sdio_bmi_credits(ar);
  895. if (ret)
  896. return ret;
  897. addr = ar->mbox_info.htc_addr;
  898. ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
  899. HIF_WR_SYNC_BYTE_INC);
  900. if (ret)
  901. ath6kl_err("unable to send the bmi data to the device\n");
  902. return ret;
  903. }
  904. static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
  905. {
  906. int ret;
  907. u32 addr;
  908. /*
  909. * During normal bootup, small reads may be required.
  910. * Rather than issue an HIF Read and then wait as the Target
  911. * adds successive bytes to the FIFO, we wait here until
  912. * we know that response data is available.
  913. *
  914. * This allows us to cleanly timeout on an unexpected
  915. * Target failure rather than risk problems at the HIF level.
  916. * In particular, this avoids SDIO timeouts and possibly garbage
  917. * data on some host controllers. And on an interconnect
  918. * such as Compact Flash (as well as some SDIO masters) which
  919. * does not provide any indication on data timeout, it avoids
  920. * a potential hang or garbage response.
  921. *
  922. * Synchronization is more difficult for reads larger than the
  923. * size of the MBOX FIFO (128B), because the Target is unable
  924. * to push the 129th byte of data until AFTER the Host posts an
  925. * HIF Read and removes some FIFO data. So for large reads the
  926. * Host proceeds to post an HIF Read BEFORE all the data is
  927. * actually available to read. Fortunately, large BMI reads do
  928. * not occur in practice -- they're supported for debug/development.
  929. *
  930. * So Host/Target BMI synchronization is divided into these cases:
  931. * CASE 1: length < 4
  932. * Should not happen
  933. *
  934. * CASE 2: 4 <= length <= 128
  935. * Wait for first 4 bytes to be in FIFO
  936. * If CONSERVATIVE_BMI_READ is enabled, also wait for
  937. * a BMI command credit, which indicates that the ENTIRE
  938. * response is available in the the FIFO
  939. *
  940. * CASE 3: length > 128
  941. * Wait for the first 4 bytes to be in FIFO
  942. *
  943. * For most uses, a small timeout should be sufficient and we will
  944. * usually see a response quickly; but there may be some unusual
  945. * (debug) cases of BMI_EXECUTE where we want an larger timeout.
  946. * For now, we use an unbounded busy loop while waiting for
  947. * BMI_EXECUTE.
  948. *
  949. * If BMI_EXECUTE ever needs to support longer-latency execution,
  950. * especially in production, this code needs to be enhanced to sleep
  951. * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
  952. * a function of Host processor speed.
  953. */
  954. if (len >= 4) { /* NB: Currently, always true */
  955. ret = ath6kl_bmi_get_rx_lkahd(ar);
  956. if (ret)
  957. return ret;
  958. }
  959. addr = ar->mbox_info.htc_addr;
  960. ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
  961. HIF_RD_SYNC_BYTE_INC);
  962. if (ret) {
  963. ath6kl_err("Unable to read the bmi data from the device: %d\n",
  964. ret);
  965. return ret;
  966. }
  967. return 0;
  968. }
  969. static void ath6kl_sdio_stop(struct ath6kl *ar)
  970. {
  971. struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
  972. struct bus_request *req, *tmp_req;
  973. void *context;
  974. /* FIXME: make sure that wq is not queued again */
  975. cancel_work_sync(&ar_sdio->wr_async_work);
  976. spin_lock_bh(&ar_sdio->wr_async_lock);
  977. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  978. list_del(&req->list);
  979. if (req->scat_req) {
  980. /* this is a scatter gather request */
  981. req->scat_req->status = -ECANCELED;
  982. req->scat_req->complete(ar_sdio->ar->htc_target,
  983. req->scat_req);
  984. } else {
  985. context = req->packet;
  986. ath6kl_sdio_free_bus_req(ar_sdio, req);
  987. ath6kl_hif_rw_comp_handler(context, -ECANCELED);
  988. }
  989. }
  990. spin_unlock_bh(&ar_sdio->wr_async_lock);
  991. WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
  992. }
  993. static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
  994. .read_write_sync = ath6kl_sdio_read_write_sync,
  995. .write_async = ath6kl_sdio_write_async,
  996. .irq_enable = ath6kl_sdio_irq_enable,
  997. .irq_disable = ath6kl_sdio_irq_disable,
  998. .scatter_req_get = ath6kl_sdio_scatter_req_get,
  999. .scatter_req_add = ath6kl_sdio_scatter_req_add,
  1000. .enable_scatter = ath6kl_sdio_enable_scatter,
  1001. .scat_req_rw = ath6kl_sdio_async_rw_scatter,
  1002. .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
  1003. .suspend = ath6kl_sdio_suspend,
  1004. .resume = ath6kl_sdio_resume,
  1005. .diag_read32 = ath6kl_sdio_diag_read32,
  1006. .diag_write32 = ath6kl_sdio_diag_write32,
  1007. .bmi_read = ath6kl_sdio_bmi_read,
  1008. .bmi_write = ath6kl_sdio_bmi_write,
  1009. .power_on = ath6kl_sdio_power_on,
  1010. .power_off = ath6kl_sdio_power_off,
  1011. .stop = ath6kl_sdio_stop,
  1012. };
  1013. #ifdef CONFIG_PM_SLEEP
  1014. /*
  1015. * Empty handlers so that mmc subsystem doesn't remove us entirely during
  1016. * suspend. We instead follow cfg80211 suspend/resume handlers.
  1017. */
  1018. static int ath6kl_sdio_pm_suspend(struct device *device)
  1019. {
  1020. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
  1021. return 0;
  1022. }
  1023. static int ath6kl_sdio_pm_resume(struct device *device)
  1024. {
  1025. ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
  1026. return 0;
  1027. }
  1028. static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
  1029. ath6kl_sdio_pm_resume);
  1030. #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
  1031. #else
  1032. #define ATH6KL_SDIO_PM_OPS NULL
  1033. #endif /* CONFIG_PM_SLEEP */
  1034. static int ath6kl_sdio_probe(struct sdio_func *func,
  1035. const struct sdio_device_id *id)
  1036. {
  1037. int ret;
  1038. struct ath6kl_sdio *ar_sdio;
  1039. struct ath6kl *ar;
  1040. int count;
  1041. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1042. "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
  1043. func->num, func->vendor, func->device,
  1044. func->max_blksize, func->cur_blksize);
  1045. ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
  1046. if (!ar_sdio)
  1047. return -ENOMEM;
  1048. ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
  1049. if (!ar_sdio->dma_buffer) {
  1050. ret = -ENOMEM;
  1051. goto err_hif;
  1052. }
  1053. ar_sdio->func = func;
  1054. sdio_set_drvdata(func, ar_sdio);
  1055. ar_sdio->id = id;
  1056. ar_sdio->is_disabled = true;
  1057. spin_lock_init(&ar_sdio->lock);
  1058. spin_lock_init(&ar_sdio->scat_lock);
  1059. spin_lock_init(&ar_sdio->wr_async_lock);
  1060. mutex_init(&ar_sdio->dma_buffer_mutex);
  1061. INIT_LIST_HEAD(&ar_sdio->scat_req);
  1062. INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
  1063. INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
  1064. INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
  1065. init_waitqueue_head(&ar_sdio->irq_wq);
  1066. for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
  1067. ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
  1068. ar = ath6kl_core_create(&ar_sdio->func->dev);
  1069. if (!ar) {
  1070. ath6kl_err("Failed to alloc ath6kl core\n");
  1071. ret = -ENOMEM;
  1072. goto err_dma;
  1073. }
  1074. ar_sdio->ar = ar;
  1075. ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
  1076. ar->hif_priv = ar_sdio;
  1077. ar->hif_ops = &ath6kl_sdio_ops;
  1078. ar->bmi.max_data_size = 256;
  1079. ath6kl_sdio_set_mbox_info(ar);
  1080. ret = ath6kl_sdio_config(ar);
  1081. if (ret) {
  1082. ath6kl_err("Failed to config sdio: %d\n", ret);
  1083. goto err_core_alloc;
  1084. }
  1085. ret = ath6kl_core_init(ar, ATH6KL_HTC_TYPE_MBOX);
  1086. if (ret) {
  1087. ath6kl_err("Failed to init ath6kl core\n");
  1088. goto err_core_alloc;
  1089. }
  1090. return ret;
  1091. err_core_alloc:
  1092. ath6kl_core_destroy(ar_sdio->ar);
  1093. err_dma:
  1094. kfree(ar_sdio->dma_buffer);
  1095. err_hif:
  1096. kfree(ar_sdio);
  1097. return ret;
  1098. }
  1099. static void ath6kl_sdio_remove(struct sdio_func *func)
  1100. {
  1101. struct ath6kl_sdio *ar_sdio;
  1102. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1103. "sdio removed func %d vendor 0x%x device 0x%x\n",
  1104. func->num, func->vendor, func->device);
  1105. ar_sdio = sdio_get_drvdata(func);
  1106. ath6kl_stop_txrx(ar_sdio->ar);
  1107. cancel_work_sync(&ar_sdio->wr_async_work);
  1108. ath6kl_core_cleanup(ar_sdio->ar);
  1109. ath6kl_core_destroy(ar_sdio->ar);
  1110. kfree(ar_sdio->dma_buffer);
  1111. kfree(ar_sdio);
  1112. }
  1113. static const struct sdio_device_id ath6kl_sdio_devices[] = {
  1114. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
  1115. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
  1116. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
  1117. {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
  1118. {},
  1119. };
  1120. MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
  1121. static struct sdio_driver ath6kl_sdio_driver = {
  1122. .name = "ath6kl_sdio",
  1123. .id_table = ath6kl_sdio_devices,
  1124. .probe = ath6kl_sdio_probe,
  1125. .remove = ath6kl_sdio_remove,
  1126. .drv.pm = ATH6KL_SDIO_PM_OPS,
  1127. };
  1128. static int __init ath6kl_sdio_init(void)
  1129. {
  1130. int ret;
  1131. ret = sdio_register_driver(&ath6kl_sdio_driver);
  1132. if (ret)
  1133. ath6kl_err("sdio driver registration failed: %d\n", ret);
  1134. return ret;
  1135. }
  1136. static void __exit ath6kl_sdio_exit(void)
  1137. {
  1138. sdio_unregister_driver(&ath6kl_sdio_driver);
  1139. }
  1140. module_init(ath6kl_sdio_init);
  1141. module_exit(ath6kl_sdio_exit);
  1142. MODULE_AUTHOR("Atheros Communications, Inc.");
  1143. MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
  1144. MODULE_LICENSE("Dual BSD/GPL");
  1145. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_OTP_FILE);
  1146. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_FIRMWARE_FILE);
  1147. MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_PATCH_FILE);
  1148. MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
  1149. MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
  1150. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_OTP_FILE);
  1151. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_FIRMWARE_FILE);
  1152. MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_PATCH_FILE);
  1153. MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
  1154. MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);
  1155. MODULE_FIRMWARE(AR6004_HW_1_0_FW_DIR "/" AR6004_HW_1_0_FIRMWARE_FILE);
  1156. MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
  1157. MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
  1158. MODULE_FIRMWARE(AR6004_HW_1_1_FW_DIR "/" AR6004_HW_1_1_FIRMWARE_FILE);
  1159. MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
  1160. MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);
  1161. MODULE_FIRMWARE(AR6004_HW_1_2_FW_DIR "/" AR6004_HW_1_2_FIRMWARE_FILE);
  1162. MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE);
  1163. MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE);