sdio.c 37 KB

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