sdio.c 35 KB

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