sdio.c 37 KB

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