sdio.c 35 KB

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