sdio.c 37 KB

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