sdio.c 37 KB

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