sdio.c 36 KB

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