if_sdio.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /*
  2. * linux/drivers/net/wireless/libertas/if_sdio.c
  3. *
  4. * Copyright 2007-2008 Pierre Ossman
  5. *
  6. * Inspired by if_cs.c, Copyright 2007 Holger Schurig
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This hardware has more or less no CMD53 support, so all registers
  14. * must be accessed using sdio_readb()/sdio_writeb().
  15. *
  16. * Transfers must be in one transaction or the firmware goes bonkers.
  17. * This means that the transfer must either be small enough to do a
  18. * byte based transfer or it must be padded to a multiple of the
  19. * current block size.
  20. *
  21. * As SDIO is still new to the kernel, it is unfortunately common with
  22. * bugs in the host controllers related to that. One such bug is that
  23. * controllers cannot do transfers that aren't a multiple of 4 bytes.
  24. * If you don't have time to fix the host controller driver, you can
  25. * work around the problem by modifying if_sdio_host_to_card() and
  26. * if_sdio_card_to_host() to pad the data.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/firmware.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/delay.h>
  35. #include <linux/mmc/card.h>
  36. #include <linux/mmc/sdio_func.h>
  37. #include <linux/mmc/sdio_ids.h>
  38. #include <linux/mmc/sdio.h>
  39. #include <linux/mmc/host.h>
  40. #include <linux/pm_runtime.h>
  41. #include "host.h"
  42. #include "decl.h"
  43. #include "defs.h"
  44. #include "dev.h"
  45. #include "cmd.h"
  46. #include "if_sdio.h"
  47. static void if_sdio_interrupt(struct sdio_func *func);
  48. /* The if_sdio_remove() callback function is called when
  49. * user removes this module from kernel space or ejects
  50. * the card from the slot. The driver handles these 2 cases
  51. * differently for SD8688 combo chip.
  52. * If the user is removing the module, the FUNC_SHUTDOWN
  53. * command for SD8688 is sent to the firmware.
  54. * If the card is removed, there is no need to send this command.
  55. *
  56. * The variable 'user_rmmod' is used to distinguish these two
  57. * scenarios. This flag is initialized as FALSE in case the card
  58. * is removed, and will be set to TRUE for module removal when
  59. * module_exit function is called.
  60. */
  61. static u8 user_rmmod;
  62. static const struct sdio_device_id if_sdio_ids[] = {
  63. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  64. SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
  65. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  66. SDIO_DEVICE_ID_MARVELL_8688WLAN) },
  67. { /* end: all zeroes */ },
  68. };
  69. MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
  70. #define MODEL_8385 0x04
  71. #define MODEL_8686 0x0b
  72. #define MODEL_8688 0x10
  73. static const struct lbs_fw_table fw_table[] = {
  74. { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
  75. { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
  76. { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
  77. { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
  78. { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
  79. { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
  80. { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
  81. { 0, NULL, NULL }
  82. };
  83. MODULE_FIRMWARE("libertas/sd8385_helper.bin");
  84. MODULE_FIRMWARE("libertas/sd8385.bin");
  85. MODULE_FIRMWARE("sd8385_helper.bin");
  86. MODULE_FIRMWARE("sd8385.bin");
  87. MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
  88. MODULE_FIRMWARE("libertas/sd8686_v9.bin");
  89. MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
  90. MODULE_FIRMWARE("libertas/sd8686_v8.bin");
  91. MODULE_FIRMWARE("sd8686_helper.bin");
  92. MODULE_FIRMWARE("sd8686.bin");
  93. MODULE_FIRMWARE("libertas/sd8688_helper.bin");
  94. MODULE_FIRMWARE("libertas/sd8688.bin");
  95. MODULE_FIRMWARE("sd8688_helper.bin");
  96. MODULE_FIRMWARE("sd8688.bin");
  97. struct if_sdio_packet {
  98. struct if_sdio_packet *next;
  99. u16 nb;
  100. u8 buffer[0] __attribute__((aligned(4)));
  101. };
  102. struct if_sdio_card {
  103. struct sdio_func *func;
  104. struct lbs_private *priv;
  105. int model;
  106. unsigned long ioport;
  107. unsigned int scratch_reg;
  108. bool started;
  109. wait_queue_head_t pwron_waitq;
  110. u8 buffer[65536] __attribute__((aligned(4)));
  111. spinlock_t lock;
  112. struct if_sdio_packet *packets;
  113. struct workqueue_struct *workqueue;
  114. struct work_struct packet_worker;
  115. u8 rx_unit;
  116. };
  117. static void if_sdio_finish_power_on(struct if_sdio_card *card);
  118. static int if_sdio_power_off(struct if_sdio_card *card);
  119. /********************************************************************/
  120. /* I/O */
  121. /********************************************************************/
  122. /*
  123. * For SD8385/SD8686, this function reads firmware status after
  124. * the image is downloaded, or reads RX packet length when
  125. * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
  126. * For SD8688, this function reads firmware status only.
  127. */
  128. static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
  129. {
  130. int ret;
  131. u16 scratch;
  132. scratch = sdio_readb(card->func, card->scratch_reg, &ret);
  133. if (!ret)
  134. scratch |= sdio_readb(card->func, card->scratch_reg + 1,
  135. &ret) << 8;
  136. if (err)
  137. *err = ret;
  138. if (ret)
  139. return 0xffff;
  140. return scratch;
  141. }
  142. static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
  143. {
  144. int ret;
  145. u8 rx_unit;
  146. rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
  147. if (ret)
  148. rx_unit = 0;
  149. return rx_unit;
  150. }
  151. static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
  152. {
  153. int ret;
  154. u16 rx_len;
  155. switch (card->model) {
  156. case MODEL_8385:
  157. case MODEL_8686:
  158. rx_len = if_sdio_read_scratch(card, &ret);
  159. break;
  160. case MODEL_8688:
  161. default: /* for newer chipsets */
  162. rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
  163. if (!ret)
  164. rx_len <<= card->rx_unit;
  165. else
  166. rx_len = 0xffff; /* invalid length */
  167. break;
  168. }
  169. if (err)
  170. *err = ret;
  171. return rx_len;
  172. }
  173. static int if_sdio_handle_cmd(struct if_sdio_card *card,
  174. u8 *buffer, unsigned size)
  175. {
  176. struct lbs_private *priv = card->priv;
  177. int ret;
  178. unsigned long flags;
  179. u8 i;
  180. if (size > LBS_CMD_BUFFER_SIZE) {
  181. lbs_deb_sdio("response packet too large (%d bytes)\n",
  182. (int)size);
  183. ret = -E2BIG;
  184. goto out;
  185. }
  186. spin_lock_irqsave(&priv->driver_lock, flags);
  187. i = (priv->resp_idx == 0) ? 1 : 0;
  188. BUG_ON(priv->resp_len[i]);
  189. priv->resp_len[i] = size;
  190. memcpy(priv->resp_buf[i], buffer, size);
  191. lbs_notify_command_response(priv, i);
  192. spin_unlock_irqrestore(&priv->driver_lock, flags);
  193. ret = 0;
  194. out:
  195. return ret;
  196. }
  197. static int if_sdio_handle_data(struct if_sdio_card *card,
  198. u8 *buffer, unsigned size)
  199. {
  200. int ret;
  201. struct sk_buff *skb;
  202. if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  203. lbs_deb_sdio("response packet too large (%d bytes)\n",
  204. (int)size);
  205. ret = -E2BIG;
  206. goto out;
  207. }
  208. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
  209. if (!skb) {
  210. ret = -ENOMEM;
  211. goto out;
  212. }
  213. skb_reserve(skb, NET_IP_ALIGN);
  214. skb_put_data(skb, buffer, size);
  215. lbs_process_rxed_packet(card->priv, skb);
  216. ret = 0;
  217. out:
  218. return ret;
  219. }
  220. static int if_sdio_handle_event(struct if_sdio_card *card,
  221. u8 *buffer, unsigned size)
  222. {
  223. int ret;
  224. u32 event;
  225. if (card->model == MODEL_8385) {
  226. event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
  227. if (ret)
  228. goto out;
  229. /* right shift 3 bits to get the event id */
  230. event >>= 3;
  231. } else {
  232. if (size < 4) {
  233. lbs_deb_sdio("event packet too small (%d bytes)\n",
  234. (int)size);
  235. ret = -EINVAL;
  236. goto out;
  237. }
  238. event = buffer[3] << 24;
  239. event |= buffer[2] << 16;
  240. event |= buffer[1] << 8;
  241. event |= buffer[0] << 0;
  242. }
  243. lbs_queue_event(card->priv, event & 0xFF);
  244. ret = 0;
  245. out:
  246. return ret;
  247. }
  248. static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
  249. {
  250. u8 status;
  251. unsigned long timeout;
  252. int ret = 0;
  253. timeout = jiffies + HZ;
  254. while (1) {
  255. status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
  256. if (ret)
  257. return ret;
  258. if ((status & condition) == condition)
  259. break;
  260. if (time_after(jiffies, timeout))
  261. return -ETIMEDOUT;
  262. mdelay(1);
  263. }
  264. return ret;
  265. }
  266. static int if_sdio_card_to_host(struct if_sdio_card *card)
  267. {
  268. int ret;
  269. u16 size, type, chunk;
  270. size = if_sdio_read_rx_len(card, &ret);
  271. if (ret)
  272. goto out;
  273. if (size < 4) {
  274. lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
  275. (int)size);
  276. ret = -EINVAL;
  277. goto out;
  278. }
  279. ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
  280. if (ret)
  281. goto out;
  282. /*
  283. * The transfer must be in one transaction or the firmware
  284. * goes suicidal. There's no way to guarantee that for all
  285. * controllers, but we can at least try.
  286. */
  287. chunk = sdio_align_size(card->func, size);
  288. ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
  289. if (ret)
  290. goto out;
  291. chunk = card->buffer[0] | (card->buffer[1] << 8);
  292. type = card->buffer[2] | (card->buffer[3] << 8);
  293. lbs_deb_sdio("packet of type %d and size %d bytes\n",
  294. (int)type, (int)chunk);
  295. if (chunk > size) {
  296. lbs_deb_sdio("packet fragment (%d > %d)\n",
  297. (int)chunk, (int)size);
  298. ret = -EINVAL;
  299. goto out;
  300. }
  301. if (chunk < size) {
  302. lbs_deb_sdio("packet fragment (%d < %d)\n",
  303. (int)chunk, (int)size);
  304. }
  305. switch (type) {
  306. case MVMS_CMD:
  307. ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
  308. if (ret)
  309. goto out;
  310. break;
  311. case MVMS_DAT:
  312. ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
  313. if (ret)
  314. goto out;
  315. break;
  316. case MVMS_EVENT:
  317. ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
  318. if (ret)
  319. goto out;
  320. break;
  321. default:
  322. lbs_deb_sdio("invalid type (%d) from firmware\n",
  323. (int)type);
  324. ret = -EINVAL;
  325. goto out;
  326. }
  327. out:
  328. if (ret)
  329. pr_err("problem fetching packet from firmware\n");
  330. return ret;
  331. }
  332. static void if_sdio_host_to_card_worker(struct work_struct *work)
  333. {
  334. struct if_sdio_card *card;
  335. struct if_sdio_packet *packet;
  336. int ret;
  337. unsigned long flags;
  338. card = container_of(work, struct if_sdio_card, packet_worker);
  339. while (1) {
  340. spin_lock_irqsave(&card->lock, flags);
  341. packet = card->packets;
  342. if (packet)
  343. card->packets = packet->next;
  344. spin_unlock_irqrestore(&card->lock, flags);
  345. if (!packet)
  346. break;
  347. sdio_claim_host(card->func);
  348. ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
  349. if (ret == 0) {
  350. ret = sdio_writesb(card->func, card->ioport,
  351. packet->buffer, packet->nb);
  352. }
  353. if (ret)
  354. pr_err("error %d sending packet to firmware\n", ret);
  355. sdio_release_host(card->func);
  356. kfree(packet);
  357. }
  358. }
  359. /********************************************************************/
  360. /* Firmware */
  361. /********************************************************************/
  362. #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
  363. static int if_sdio_prog_helper(struct if_sdio_card *card,
  364. const struct firmware *fw)
  365. {
  366. int ret;
  367. unsigned long timeout;
  368. u8 *chunk_buffer;
  369. u32 chunk_size;
  370. const u8 *firmware;
  371. size_t size;
  372. chunk_buffer = kzalloc(64, GFP_KERNEL);
  373. if (!chunk_buffer) {
  374. ret = -ENOMEM;
  375. goto out;
  376. }
  377. sdio_claim_host(card->func);
  378. ret = sdio_set_block_size(card->func, 32);
  379. if (ret)
  380. goto release;
  381. firmware = fw->data;
  382. size = fw->size;
  383. while (size) {
  384. ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
  385. if (ret)
  386. goto release;
  387. /* On some platforms (like Davinci) the chip needs more time
  388. * between helper blocks.
  389. */
  390. mdelay(2);
  391. chunk_size = min_t(size_t, size, 60);
  392. *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
  393. memcpy(chunk_buffer + 4, firmware, chunk_size);
  394. /*
  395. lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
  396. */
  397. ret = sdio_writesb(card->func, card->ioport,
  398. chunk_buffer, 64);
  399. if (ret)
  400. goto release;
  401. firmware += chunk_size;
  402. size -= chunk_size;
  403. }
  404. /* an empty block marks the end of the transfer */
  405. memset(chunk_buffer, 0, 4);
  406. ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
  407. if (ret)
  408. goto release;
  409. lbs_deb_sdio("waiting for helper to boot...\n");
  410. /* wait for the helper to boot by looking at the size register */
  411. timeout = jiffies + HZ;
  412. while (1) {
  413. u16 req_size;
  414. req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
  415. if (ret)
  416. goto release;
  417. req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
  418. if (ret)
  419. goto release;
  420. if (req_size != 0)
  421. break;
  422. if (time_after(jiffies, timeout)) {
  423. ret = -ETIMEDOUT;
  424. goto release;
  425. }
  426. msleep(10);
  427. }
  428. ret = 0;
  429. release:
  430. sdio_release_host(card->func);
  431. kfree(chunk_buffer);
  432. out:
  433. if (ret)
  434. pr_err("failed to load helper firmware\n");
  435. return ret;
  436. }
  437. static int if_sdio_prog_real(struct if_sdio_card *card,
  438. const struct firmware *fw)
  439. {
  440. int ret;
  441. unsigned long timeout;
  442. u8 *chunk_buffer;
  443. u32 chunk_size;
  444. const u8 *firmware;
  445. size_t size, req_size;
  446. chunk_buffer = kzalloc(512, GFP_KERNEL);
  447. if (!chunk_buffer) {
  448. ret = -ENOMEM;
  449. goto out;
  450. }
  451. sdio_claim_host(card->func);
  452. ret = sdio_set_block_size(card->func, 32);
  453. if (ret)
  454. goto release;
  455. firmware = fw->data;
  456. size = fw->size;
  457. while (size) {
  458. timeout = jiffies + HZ;
  459. while (1) {
  460. ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
  461. if (ret)
  462. goto release;
  463. req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
  464. &ret);
  465. if (ret)
  466. goto release;
  467. req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
  468. &ret) << 8;
  469. if (ret)
  470. goto release;
  471. /*
  472. * For SD8688 wait until the length is not 0, 1 or 2
  473. * before downloading the first FW block,
  474. * since BOOT code writes the register to indicate the
  475. * helper/FW download winner,
  476. * the value could be 1 or 2 (Func1 or Func2).
  477. */
  478. if ((size != fw->size) || (req_size > 2))
  479. break;
  480. if (time_after(jiffies, timeout)) {
  481. ret = -ETIMEDOUT;
  482. goto release;
  483. }
  484. mdelay(1);
  485. }
  486. /*
  487. lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
  488. */
  489. if (req_size == 0) {
  490. lbs_deb_sdio("firmware helper gave up early\n");
  491. ret = -EIO;
  492. goto release;
  493. }
  494. if (req_size & 0x01) {
  495. lbs_deb_sdio("firmware helper signalled error\n");
  496. ret = -EIO;
  497. goto release;
  498. }
  499. if (req_size > size)
  500. req_size = size;
  501. while (req_size) {
  502. chunk_size = min_t(size_t, req_size, 512);
  503. memcpy(chunk_buffer, firmware, chunk_size);
  504. /*
  505. lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
  506. chunk_size, (chunk_size + 31) / 32 * 32);
  507. */
  508. ret = sdio_writesb(card->func, card->ioport,
  509. chunk_buffer, roundup(chunk_size, 32));
  510. if (ret)
  511. goto release;
  512. firmware += chunk_size;
  513. size -= chunk_size;
  514. req_size -= chunk_size;
  515. }
  516. }
  517. ret = 0;
  518. lbs_deb_sdio("waiting for firmware to boot...\n");
  519. /* wait for the firmware to boot */
  520. timeout = jiffies + HZ;
  521. while (1) {
  522. u16 scratch;
  523. scratch = if_sdio_read_scratch(card, &ret);
  524. if (ret)
  525. goto release;
  526. if (scratch == IF_SDIO_FIRMWARE_OK)
  527. break;
  528. if (time_after(jiffies, timeout)) {
  529. ret = -ETIMEDOUT;
  530. goto release;
  531. }
  532. msleep(10);
  533. }
  534. ret = 0;
  535. release:
  536. sdio_release_host(card->func);
  537. kfree(chunk_buffer);
  538. out:
  539. if (ret)
  540. pr_err("failed to load firmware\n");
  541. return ret;
  542. }
  543. static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
  544. const struct firmware *helper,
  545. const struct firmware *mainfw)
  546. {
  547. struct if_sdio_card *card = priv->card;
  548. if (ret) {
  549. pr_err("failed to find firmware (%d)\n", ret);
  550. return;
  551. }
  552. ret = if_sdio_prog_helper(card, helper);
  553. if (ret)
  554. return;
  555. lbs_deb_sdio("Helper firmware loaded\n");
  556. ret = if_sdio_prog_real(card, mainfw);
  557. if (ret)
  558. return;
  559. lbs_deb_sdio("Firmware loaded\n");
  560. if_sdio_finish_power_on(card);
  561. }
  562. static int if_sdio_prog_firmware(struct if_sdio_card *card)
  563. {
  564. int ret;
  565. u16 scratch;
  566. /*
  567. * Disable interrupts
  568. */
  569. sdio_claim_host(card->func);
  570. sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
  571. sdio_release_host(card->func);
  572. sdio_claim_host(card->func);
  573. scratch = if_sdio_read_scratch(card, &ret);
  574. sdio_release_host(card->func);
  575. lbs_deb_sdio("firmware status = %#x\n", scratch);
  576. lbs_deb_sdio("scratch ret = %d\n", ret);
  577. if (ret)
  578. goto out;
  579. /*
  580. * The manual clearly describes that FEDC is the right code to use
  581. * to detect firmware presence, but for SD8686 it is not that simple.
  582. * Scratch is also used to store the RX packet length, so we lose
  583. * the FEDC value early on. So we use a non-zero check in order
  584. * to validate firmware presence.
  585. * Additionally, the SD8686 in the Gumstix always has the high scratch
  586. * bit set, even when the firmware is not loaded. So we have to
  587. * exclude that from the test.
  588. */
  589. if (scratch == IF_SDIO_FIRMWARE_OK) {
  590. lbs_deb_sdio("firmware already loaded\n");
  591. if_sdio_finish_power_on(card);
  592. return 0;
  593. } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
  594. lbs_deb_sdio("firmware may be running\n");
  595. if_sdio_finish_power_on(card);
  596. return 0;
  597. }
  598. ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
  599. fw_table, if_sdio_do_prog_firmware);
  600. out:
  601. return ret;
  602. }
  603. /********************************************************************/
  604. /* Power management */
  605. /********************************************************************/
  606. /* Finish power on sequence (after firmware is loaded) */
  607. static void if_sdio_finish_power_on(struct if_sdio_card *card)
  608. {
  609. struct sdio_func *func = card->func;
  610. struct lbs_private *priv = card->priv;
  611. int ret;
  612. sdio_claim_host(func);
  613. sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
  614. /*
  615. * Get rx_unit if the chip is SD8688 or newer.
  616. * SD8385 & SD8686 do not have rx_unit.
  617. */
  618. if ((card->model != MODEL_8385)
  619. && (card->model != MODEL_8686))
  620. card->rx_unit = if_sdio_read_rx_unit(card);
  621. else
  622. card->rx_unit = 0;
  623. /*
  624. * Set up the interrupt handler late.
  625. *
  626. * If we set it up earlier, the (buggy) hardware generates a spurious
  627. * interrupt, even before the interrupt has been enabled, with
  628. * CCCR_INTx = 0.
  629. *
  630. * We register the interrupt handler late so that we can handle any
  631. * spurious interrupts, and also to avoid generation of that known
  632. * spurious interrupt in the first place.
  633. */
  634. ret = sdio_claim_irq(func, if_sdio_interrupt);
  635. if (ret)
  636. goto release;
  637. /*
  638. * Enable interrupts now that everything is set up
  639. */
  640. sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
  641. if (ret)
  642. goto release_irq;
  643. sdio_release_host(func);
  644. /* Set fw_ready before queuing any commands so that
  645. * lbs_thread won't block from sending them to firmware.
  646. */
  647. priv->fw_ready = 1;
  648. /*
  649. * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
  650. */
  651. if (card->model == MODEL_8688) {
  652. struct cmd_header cmd;
  653. memset(&cmd, 0, sizeof(cmd));
  654. lbs_deb_sdio("send function INIT command\n");
  655. if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
  656. lbs_cmd_copyback, (unsigned long) &cmd))
  657. netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
  658. }
  659. wake_up(&card->pwron_waitq);
  660. if (!card->started) {
  661. ret = lbs_start_card(priv);
  662. if_sdio_power_off(card);
  663. if (ret == 0) {
  664. card->started = true;
  665. /* Tell PM core that we don't need the card to be
  666. * powered now */
  667. pm_runtime_put(&func->dev);
  668. }
  669. }
  670. return;
  671. release_irq:
  672. sdio_release_irq(func);
  673. release:
  674. sdio_release_host(func);
  675. }
  676. static int if_sdio_power_on(struct if_sdio_card *card)
  677. {
  678. struct sdio_func *func = card->func;
  679. struct mmc_host *host = func->card->host;
  680. int ret;
  681. sdio_claim_host(func);
  682. ret = sdio_enable_func(func);
  683. if (ret)
  684. goto release;
  685. /* For 1-bit transfers to the 8686 model, we need to enable the
  686. * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
  687. * bit to allow access to non-vendor registers. */
  688. if ((card->model == MODEL_8686) &&
  689. (host->caps & MMC_CAP_SDIO_IRQ) &&
  690. (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
  691. u8 reg;
  692. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  693. reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
  694. if (ret)
  695. goto disable;
  696. reg |= SDIO_BUS_ECSI;
  697. sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
  698. if (ret)
  699. goto disable;
  700. }
  701. card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
  702. if (ret)
  703. goto disable;
  704. card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
  705. if (ret)
  706. goto disable;
  707. card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
  708. if (ret)
  709. goto disable;
  710. sdio_release_host(func);
  711. ret = if_sdio_prog_firmware(card);
  712. if (ret) {
  713. sdio_claim_host(func);
  714. goto disable;
  715. }
  716. return 0;
  717. disable:
  718. sdio_disable_func(func);
  719. release:
  720. sdio_release_host(func);
  721. return ret;
  722. }
  723. static int if_sdio_power_off(struct if_sdio_card *card)
  724. {
  725. struct sdio_func *func = card->func;
  726. struct lbs_private *priv = card->priv;
  727. priv->fw_ready = 0;
  728. sdio_claim_host(func);
  729. sdio_release_irq(func);
  730. sdio_disable_func(func);
  731. sdio_release_host(func);
  732. return 0;
  733. }
  734. /*******************************************************************/
  735. /* Libertas callbacks */
  736. /*******************************************************************/
  737. static int if_sdio_host_to_card(struct lbs_private *priv,
  738. u8 type, u8 *buf, u16 nb)
  739. {
  740. int ret;
  741. struct if_sdio_card *card;
  742. struct if_sdio_packet *packet, *cur;
  743. u16 size;
  744. unsigned long flags;
  745. card = priv->card;
  746. if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
  747. ret = -EINVAL;
  748. goto out;
  749. }
  750. /*
  751. * The transfer must be in one transaction or the firmware
  752. * goes suicidal. There's no way to guarantee that for all
  753. * controllers, but we can at least try.
  754. */
  755. size = sdio_align_size(card->func, nb + 4);
  756. packet = kzalloc(sizeof(struct if_sdio_packet) + size,
  757. GFP_ATOMIC);
  758. if (!packet) {
  759. ret = -ENOMEM;
  760. goto out;
  761. }
  762. packet->next = NULL;
  763. packet->nb = size;
  764. /*
  765. * SDIO specific header.
  766. */
  767. packet->buffer[0] = (nb + 4) & 0xff;
  768. packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
  769. packet->buffer[2] = type;
  770. packet->buffer[3] = 0;
  771. memcpy(packet->buffer + 4, buf, nb);
  772. spin_lock_irqsave(&card->lock, flags);
  773. if (!card->packets)
  774. card->packets = packet;
  775. else {
  776. cur = card->packets;
  777. while (cur->next)
  778. cur = cur->next;
  779. cur->next = packet;
  780. }
  781. switch (type) {
  782. case MVMS_CMD:
  783. priv->dnld_sent = DNLD_CMD_SENT;
  784. break;
  785. case MVMS_DAT:
  786. priv->dnld_sent = DNLD_DATA_SENT;
  787. break;
  788. default:
  789. lbs_deb_sdio("unknown packet type %d\n", (int)type);
  790. }
  791. spin_unlock_irqrestore(&card->lock, flags);
  792. queue_work(card->workqueue, &card->packet_worker);
  793. ret = 0;
  794. out:
  795. return ret;
  796. }
  797. static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
  798. {
  799. int ret = -1;
  800. struct cmd_header cmd;
  801. memset(&cmd, 0, sizeof(cmd));
  802. lbs_deb_sdio("send DEEP_SLEEP command\n");
  803. ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
  804. lbs_cmd_copyback, (unsigned long) &cmd);
  805. if (ret)
  806. netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
  807. mdelay(200);
  808. return ret;
  809. }
  810. static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
  811. {
  812. struct if_sdio_card *card = priv->card;
  813. int ret = -1;
  814. sdio_claim_host(card->func);
  815. sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
  816. if (ret)
  817. netdev_err(priv->dev, "sdio_writeb failed!\n");
  818. sdio_release_host(card->func);
  819. return ret;
  820. }
  821. static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
  822. {
  823. struct if_sdio_card *card = priv->card;
  824. int ret = -1;
  825. sdio_claim_host(card->func);
  826. sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
  827. if (ret)
  828. netdev_err(priv->dev, "sdio_writeb failed!\n");
  829. sdio_release_host(card->func);
  830. return ret;
  831. }
  832. static struct mmc_host *reset_host;
  833. static void if_sdio_reset_card_worker(struct work_struct *work)
  834. {
  835. /*
  836. * The actual reset operation must be run outside of lbs_thread. This
  837. * is because mmc_remove_host() will cause the device to be instantly
  838. * destroyed, and the libertas driver then needs to end lbs_thread,
  839. * leading to a deadlock.
  840. *
  841. * We run it in a workqueue totally independent from the if_sdio_card
  842. * instance for that reason.
  843. */
  844. pr_info("Resetting card...");
  845. mmc_remove_host(reset_host);
  846. mmc_add_host(reset_host);
  847. }
  848. static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
  849. static void if_sdio_reset_card(struct lbs_private *priv)
  850. {
  851. struct if_sdio_card *card = priv->card;
  852. if (work_pending(&card_reset_work))
  853. return;
  854. reset_host = card->func->card->host;
  855. schedule_work(&card_reset_work);
  856. }
  857. static int if_sdio_power_save(struct lbs_private *priv)
  858. {
  859. struct if_sdio_card *card = priv->card;
  860. int ret;
  861. flush_workqueue(card->workqueue);
  862. ret = if_sdio_power_off(card);
  863. /* Let runtime PM know the card is powered off */
  864. pm_runtime_put_sync(&card->func->dev);
  865. return ret;
  866. }
  867. static int if_sdio_power_restore(struct lbs_private *priv)
  868. {
  869. struct if_sdio_card *card = priv->card;
  870. int r;
  871. /* Make sure the card will not be powered off by runtime PM */
  872. pm_runtime_get_sync(&card->func->dev);
  873. r = if_sdio_power_on(card);
  874. if (r)
  875. return r;
  876. wait_event(card->pwron_waitq, priv->fw_ready);
  877. return 0;
  878. }
  879. /*******************************************************************/
  880. /* SDIO callbacks */
  881. /*******************************************************************/
  882. static void if_sdio_interrupt(struct sdio_func *func)
  883. {
  884. int ret;
  885. struct if_sdio_card *card;
  886. u8 cause;
  887. card = sdio_get_drvdata(func);
  888. cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
  889. if (ret || !cause)
  890. return;
  891. lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
  892. sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
  893. if (ret)
  894. return;
  895. /*
  896. * Ignore the define name, this really means the card has
  897. * successfully received the command.
  898. */
  899. card->priv->is_activity_detected = 1;
  900. if (cause & IF_SDIO_H_INT_DNLD)
  901. lbs_host_to_card_done(card->priv);
  902. if (cause & IF_SDIO_H_INT_UPLD) {
  903. ret = if_sdio_card_to_host(card);
  904. if (ret)
  905. return;
  906. }
  907. }
  908. static int if_sdio_probe(struct sdio_func *func,
  909. const struct sdio_device_id *id)
  910. {
  911. struct if_sdio_card *card;
  912. struct lbs_private *priv;
  913. int ret, i;
  914. unsigned int model;
  915. struct if_sdio_packet *packet;
  916. for (i = 0;i < func->card->num_info;i++) {
  917. if (sscanf(func->card->info[i],
  918. "802.11 SDIO ID: %x", &model) == 1)
  919. break;
  920. if (sscanf(func->card->info[i],
  921. "ID: %x", &model) == 1)
  922. break;
  923. if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
  924. model = MODEL_8385;
  925. break;
  926. }
  927. }
  928. if (i == func->card->num_info) {
  929. pr_err("unable to identify card model\n");
  930. return -ENODEV;
  931. }
  932. card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
  933. if (!card)
  934. return -ENOMEM;
  935. card->func = func;
  936. card->model = model;
  937. switch (card->model) {
  938. case MODEL_8385:
  939. card->scratch_reg = IF_SDIO_SCRATCH_OLD;
  940. break;
  941. case MODEL_8686:
  942. card->scratch_reg = IF_SDIO_SCRATCH;
  943. break;
  944. case MODEL_8688:
  945. default: /* for newer chipsets */
  946. card->scratch_reg = IF_SDIO_FW_STATUS;
  947. break;
  948. }
  949. spin_lock_init(&card->lock);
  950. card->workqueue = alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM, 0);
  951. if (unlikely(!card->workqueue)) {
  952. ret = -ENOMEM;
  953. goto err_queue;
  954. }
  955. INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
  956. init_waitqueue_head(&card->pwron_waitq);
  957. /* Check if we support this card */
  958. for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
  959. if (card->model == fw_table[i].model)
  960. break;
  961. }
  962. if (i == ARRAY_SIZE(fw_table)) {
  963. pr_err("unknown card model 0x%x\n", card->model);
  964. ret = -ENODEV;
  965. goto free;
  966. }
  967. sdio_set_drvdata(func, card);
  968. lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
  969. "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
  970. func->class, func->vendor, func->device,
  971. model, (unsigned)card->ioport);
  972. priv = lbs_add_card(card, &func->dev);
  973. if (!priv) {
  974. ret = -ENOMEM;
  975. goto free;
  976. }
  977. card->priv = priv;
  978. priv->card = card;
  979. priv->hw_host_to_card = if_sdio_host_to_card;
  980. priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
  981. priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
  982. priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
  983. priv->reset_card = if_sdio_reset_card;
  984. priv->power_save = if_sdio_power_save;
  985. priv->power_restore = if_sdio_power_restore;
  986. priv->is_polling = !(func->card->host->caps & MMC_CAP_SDIO_IRQ);
  987. ret = if_sdio_power_on(card);
  988. if (ret)
  989. goto err_activate_card;
  990. out:
  991. return ret;
  992. err_activate_card:
  993. flush_workqueue(card->workqueue);
  994. lbs_remove_card(priv);
  995. free:
  996. destroy_workqueue(card->workqueue);
  997. err_queue:
  998. while (card->packets) {
  999. packet = card->packets;
  1000. card->packets = card->packets->next;
  1001. kfree(packet);
  1002. }
  1003. kfree(card);
  1004. goto out;
  1005. }
  1006. static void if_sdio_remove(struct sdio_func *func)
  1007. {
  1008. struct if_sdio_card *card;
  1009. struct if_sdio_packet *packet;
  1010. card = sdio_get_drvdata(func);
  1011. /* Undo decrement done above in if_sdio_probe */
  1012. pm_runtime_get_noresume(&func->dev);
  1013. if (user_rmmod && (card->model == MODEL_8688)) {
  1014. /*
  1015. * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
  1016. * multiple functions
  1017. */
  1018. struct cmd_header cmd;
  1019. memset(&cmd, 0, sizeof(cmd));
  1020. lbs_deb_sdio("send function SHUTDOWN command\n");
  1021. if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
  1022. &cmd, sizeof(cmd), lbs_cmd_copyback,
  1023. (unsigned long) &cmd))
  1024. pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
  1025. }
  1026. lbs_deb_sdio("call remove card\n");
  1027. lbs_stop_card(card->priv);
  1028. lbs_remove_card(card->priv);
  1029. destroy_workqueue(card->workqueue);
  1030. while (card->packets) {
  1031. packet = card->packets;
  1032. card->packets = card->packets->next;
  1033. kfree(packet);
  1034. }
  1035. kfree(card);
  1036. }
  1037. static int if_sdio_suspend(struct device *dev)
  1038. {
  1039. struct sdio_func *func = dev_to_sdio_func(dev);
  1040. struct if_sdio_card *card = sdio_get_drvdata(func);
  1041. struct lbs_private *priv = card->priv;
  1042. int ret;
  1043. mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
  1044. priv->power_up_on_resume = false;
  1045. /* If we're powered off anyway, just let the mmc layer remove the
  1046. * card. */
  1047. if (!lbs_iface_active(priv)) {
  1048. if (priv->fw_ready) {
  1049. priv->power_up_on_resume = true;
  1050. if_sdio_power_off(card);
  1051. }
  1052. return 0;
  1053. }
  1054. dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
  1055. sdio_func_id(func), flags);
  1056. /* If we aren't being asked to wake on anything, we should bail out
  1057. * and let the SD stack power down the card.
  1058. */
  1059. if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
  1060. dev_info(dev, "Suspend without wake params -- powering down card\n");
  1061. if (priv->fw_ready) {
  1062. ret = lbs_suspend(priv);
  1063. if (ret)
  1064. return ret;
  1065. priv->power_up_on_resume = true;
  1066. if_sdio_power_off(card);
  1067. }
  1068. return 0;
  1069. }
  1070. if (!(flags & MMC_PM_KEEP_POWER)) {
  1071. dev_err(dev, "%s: cannot remain alive while host is suspended\n",
  1072. sdio_func_id(func));
  1073. return -ENOSYS;
  1074. }
  1075. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  1076. if (ret)
  1077. return ret;
  1078. ret = lbs_suspend(priv);
  1079. if (ret)
  1080. return ret;
  1081. return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
  1082. }
  1083. static int if_sdio_resume(struct device *dev)
  1084. {
  1085. struct sdio_func *func = dev_to_sdio_func(dev);
  1086. struct if_sdio_card *card = sdio_get_drvdata(func);
  1087. int ret;
  1088. dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
  1089. if (card->priv->power_up_on_resume) {
  1090. if_sdio_power_on(card);
  1091. wait_event(card->pwron_waitq, card->priv->fw_ready);
  1092. }
  1093. ret = lbs_resume(card->priv);
  1094. return ret;
  1095. }
  1096. static const struct dev_pm_ops if_sdio_pm_ops = {
  1097. .suspend = if_sdio_suspend,
  1098. .resume = if_sdio_resume,
  1099. };
  1100. static struct sdio_driver if_sdio_driver = {
  1101. .name = "libertas_sdio",
  1102. .id_table = if_sdio_ids,
  1103. .probe = if_sdio_probe,
  1104. .remove = if_sdio_remove,
  1105. .drv = {
  1106. .pm = &if_sdio_pm_ops,
  1107. },
  1108. };
  1109. /*******************************************************************/
  1110. /* Module functions */
  1111. /*******************************************************************/
  1112. static int __init if_sdio_init_module(void)
  1113. {
  1114. int ret = 0;
  1115. printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
  1116. printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
  1117. ret = sdio_register_driver(&if_sdio_driver);
  1118. /* Clear the flag in case user removes the card. */
  1119. user_rmmod = 0;
  1120. return ret;
  1121. }
  1122. static void __exit if_sdio_exit_module(void)
  1123. {
  1124. /* Set the flag as user is removing this module. */
  1125. user_rmmod = 1;
  1126. cancel_work_sync(&card_reset_work);
  1127. sdio_unregister_driver(&if_sdio_driver);
  1128. }
  1129. module_init(if_sdio_init_module);
  1130. module_exit(if_sdio_exit_module);
  1131. MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
  1132. MODULE_AUTHOR("Pierre Ossman");
  1133. MODULE_LICENSE("GPL");