rsi_91x_sdio.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. /**
  2. * Copyright (c) 2014 Redpine Signals 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. */
  17. #include <linux/module.h>
  18. #include "rsi_sdio.h"
  19. #include "rsi_common.h"
  20. #include "rsi_coex.h"
  21. #include "rsi_hal.h"
  22. /* Default operating mode is wlan STA + BT */
  23. static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
  24. module_param(dev_oper_mode, ushort, 0444);
  25. MODULE_PARM_DESC(dev_oper_mode,
  26. "1[Wi-Fi], 4[BT], 8[BT LE], 5[Wi-Fi STA + BT classic]\n"
  27. "9[Wi-Fi STA + BT LE], 13[Wi-Fi STA + BT classic + BT LE]\n"
  28. "6[AP + BT classic], 14[AP + BT classic + BT LE]");
  29. /**
  30. * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
  31. * @rw: Read/write
  32. * @func: function number
  33. * @raw: indicates whether to perform read after write
  34. * @address: address to which to read/write
  35. * @writedata: data to write
  36. *
  37. * Return: argument
  38. */
  39. static u32 rsi_sdio_set_cmd52_arg(bool rw,
  40. u8 func,
  41. u8 raw,
  42. u32 address,
  43. u8 writedata)
  44. {
  45. return ((rw & 1) << 31) | ((func & 0x7) << 28) |
  46. ((raw & 1) << 27) | (1 << 26) |
  47. ((address & 0x1FFFF) << 9) | (1 << 8) |
  48. (writedata & 0xFF);
  49. }
  50. /**
  51. * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
  52. * @card: Pointer to the mmc_card.
  53. * @address: Address to write.
  54. * @byte: Data to write.
  55. *
  56. * Return: Write status.
  57. */
  58. static int rsi_cmd52writebyte(struct mmc_card *card,
  59. u32 address,
  60. u8 byte)
  61. {
  62. struct mmc_command io_cmd;
  63. u32 arg;
  64. memset(&io_cmd, 0, sizeof(io_cmd));
  65. arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
  66. io_cmd.opcode = SD_IO_RW_DIRECT;
  67. io_cmd.arg = arg;
  68. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  69. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  70. }
  71. /**
  72. * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
  73. * @card: Pointer to the mmc_card.
  74. * @address: Address to read from.
  75. * @byte: Variable to store read value.
  76. *
  77. * Return: Read status.
  78. */
  79. static int rsi_cmd52readbyte(struct mmc_card *card,
  80. u32 address,
  81. u8 *byte)
  82. {
  83. struct mmc_command io_cmd;
  84. u32 arg;
  85. int err;
  86. memset(&io_cmd, 0, sizeof(io_cmd));
  87. arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
  88. io_cmd.opcode = SD_IO_RW_DIRECT;
  89. io_cmd.arg = arg;
  90. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  91. err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  92. if ((!err) && (byte))
  93. *byte = io_cmd.resp[0] & 0xFF;
  94. return err;
  95. }
  96. /**
  97. * rsi_issue_sdiocommand() - This function issues sdio commands.
  98. * @func: Pointer to the sdio_func structure.
  99. * @opcode: Opcode value.
  100. * @arg: Arguments to pass.
  101. * @flags: Flags which are set.
  102. * @resp: Pointer to store response.
  103. *
  104. * Return: err: command status as 0 or -1.
  105. */
  106. static int rsi_issue_sdiocommand(struct sdio_func *func,
  107. u32 opcode,
  108. u32 arg,
  109. u32 flags,
  110. u32 *resp)
  111. {
  112. struct mmc_command cmd;
  113. struct mmc_host *host;
  114. int err;
  115. host = func->card->host;
  116. memset(&cmd, 0, sizeof(struct mmc_command));
  117. cmd.opcode = opcode;
  118. cmd.arg = arg;
  119. cmd.flags = flags;
  120. err = mmc_wait_for_cmd(host, &cmd, 3);
  121. if ((!err) && (resp))
  122. *resp = cmd.resp[0];
  123. return err;
  124. }
  125. /**
  126. * rsi_handle_interrupt() - This function is called upon the occurrence
  127. * of an interrupt.
  128. * @function: Pointer to the sdio_func structure.
  129. *
  130. * Return: None.
  131. */
  132. static void rsi_handle_interrupt(struct sdio_func *function)
  133. {
  134. struct rsi_hw *adapter = sdio_get_drvdata(function);
  135. struct rsi_91x_sdiodev *dev =
  136. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  137. if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
  138. return;
  139. rsi_set_event(&dev->rx_thread.event);
  140. }
  141. /**
  142. * rsi_reset_card() - This function resets and re-initializes the card.
  143. * @pfunction: Pointer to the sdio_func structure.
  144. *
  145. * Return: None.
  146. */
  147. static void rsi_reset_card(struct sdio_func *pfunction)
  148. {
  149. int ret = 0;
  150. int err;
  151. struct mmc_card *card = pfunction->card;
  152. struct mmc_host *host = card->host;
  153. u8 cmd52_resp;
  154. u32 clock, resp, i;
  155. u16 rca;
  156. /* Reset 9110 chip */
  157. ret = rsi_cmd52writebyte(pfunction->card,
  158. SDIO_CCCR_ABORT,
  159. (1 << 3));
  160. /* Card will not send any response as it is getting reset immediately
  161. * Hence expect a timeout status from host controller
  162. */
  163. if (ret != -ETIMEDOUT)
  164. rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
  165. /* Wait for few milli seconds to get rid of residue charges if any */
  166. msleep(20);
  167. /* Initialize the SDIO card */
  168. host->ios.chip_select = MMC_CS_DONTCARE;
  169. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  170. host->ios.power_mode = MMC_POWER_UP;
  171. host->ios.bus_width = MMC_BUS_WIDTH_1;
  172. host->ios.timing = MMC_TIMING_LEGACY;
  173. host->ops->set_ios(host, &host->ios);
  174. /*
  175. * This delay should be sufficient to allow the power supply
  176. * to reach the minimum voltage.
  177. */
  178. msleep(20);
  179. host->ios.clock = host->f_min;
  180. host->ios.power_mode = MMC_POWER_ON;
  181. host->ops->set_ios(host, &host->ios);
  182. /*
  183. * This delay must be at least 74 clock sizes, or 1 ms, or the
  184. * time required to reach a stable voltage.
  185. */
  186. msleep(20);
  187. /* Issue CMD0. Goto idle state */
  188. host->ios.chip_select = MMC_CS_HIGH;
  189. host->ops->set_ios(host, &host->ios);
  190. msleep(20);
  191. err = rsi_issue_sdiocommand(pfunction,
  192. MMC_GO_IDLE_STATE,
  193. 0,
  194. (MMC_RSP_NONE | MMC_CMD_BC),
  195. NULL);
  196. host->ios.chip_select = MMC_CS_DONTCARE;
  197. host->ops->set_ios(host, &host->ios);
  198. msleep(20);
  199. host->use_spi_crc = 0;
  200. if (err)
  201. rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
  202. /* Issue CMD5, arg = 0 */
  203. err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND, 0,
  204. (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
  205. if (err)
  206. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
  207. __func__, err);
  208. card->ocr = resp;
  209. /* Issue CMD5, arg = ocr. Wait till card is ready */
  210. for (i = 0; i < 100; i++) {
  211. err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
  212. card->ocr,
  213. (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
  214. if (err) {
  215. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
  216. __func__, err);
  217. break;
  218. }
  219. if (resp & MMC_CARD_BUSY)
  220. break;
  221. msleep(20);
  222. }
  223. if ((i == 100) || (err)) {
  224. rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
  225. __func__, i, err);
  226. return;
  227. }
  228. /* Issue CMD3, get RCA */
  229. err = rsi_issue_sdiocommand(pfunction,
  230. SD_SEND_RELATIVE_ADDR,
  231. 0,
  232. (MMC_RSP_R6 | MMC_CMD_BCR),
  233. &resp);
  234. if (err) {
  235. rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
  236. return;
  237. }
  238. rca = resp >> 16;
  239. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  240. host->ops->set_ios(host, &host->ios);
  241. /* Issue CMD7, select card */
  242. err = rsi_issue_sdiocommand(pfunction,
  243. MMC_SELECT_CARD,
  244. (rca << 16),
  245. (MMC_RSP_R1 | MMC_CMD_AC),
  246. NULL);
  247. if (err) {
  248. rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
  249. return;
  250. }
  251. /* Enable high speed */
  252. if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
  253. rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
  254. err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
  255. if (err) {
  256. rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
  257. __func__, err);
  258. } else {
  259. err = rsi_cmd52writebyte(card,
  260. SDIO_CCCR_SPEED,
  261. (cmd52_resp | SDIO_SPEED_EHS));
  262. if (err) {
  263. rsi_dbg(ERR_ZONE,
  264. "%s: CCR speed regwrite failed %d\n",
  265. __func__, err);
  266. return;
  267. }
  268. host->ios.timing = MMC_TIMING_SD_HS;
  269. host->ops->set_ios(host, &host->ios);
  270. }
  271. }
  272. /* Set clock */
  273. if (mmc_card_hs(card))
  274. clock = 50000000;
  275. else
  276. clock = card->cis.max_dtr;
  277. if (clock > host->f_max)
  278. clock = host->f_max;
  279. host->ios.clock = clock;
  280. host->ops->set_ios(host, &host->ios);
  281. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  282. /* CMD52: Set bus width & disable card detect resistor */
  283. err = rsi_cmd52writebyte(card,
  284. SDIO_CCCR_IF,
  285. (SDIO_BUS_CD_DISABLE |
  286. SDIO_BUS_WIDTH_4BIT));
  287. if (err) {
  288. rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
  289. __func__, err);
  290. return;
  291. }
  292. host->ios.bus_width = MMC_BUS_WIDTH_4;
  293. host->ops->set_ios(host, &host->ios);
  294. }
  295. }
  296. /**
  297. * rsi_setclock() - This function sets the clock frequency.
  298. * @adapter: Pointer to the adapter structure.
  299. * @freq: Clock frequency.
  300. *
  301. * Return: None.
  302. */
  303. static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
  304. {
  305. struct rsi_91x_sdiodev *dev =
  306. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  307. struct mmc_host *host = dev->pfunction->card->host;
  308. u32 clock;
  309. clock = freq * 1000;
  310. if (clock > host->f_max)
  311. clock = host->f_max;
  312. host->ios.clock = clock;
  313. host->ops->set_ios(host, &host->ios);
  314. }
  315. /**
  316. * rsi_setblocklength() - This function sets the host block length.
  317. * @adapter: Pointer to the adapter structure.
  318. * @length: Block length to be set.
  319. *
  320. * Return: status: 0 on success, -1 on failure.
  321. */
  322. static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
  323. {
  324. struct rsi_91x_sdiodev *dev =
  325. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  326. int status;
  327. rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
  328. status = sdio_set_block_size(dev->pfunction, length);
  329. dev->pfunction->max_blksize = 256;
  330. adapter->block_size = dev->pfunction->max_blksize;
  331. rsi_dbg(INFO_ZONE,
  332. "%s: Operational blk length is %d\n", __func__, length);
  333. return status;
  334. }
  335. /**
  336. * rsi_setupcard() - This function queries and sets the card's features.
  337. * @adapter: Pointer to the adapter structure.
  338. *
  339. * Return: status: 0 on success, -1 on failure.
  340. */
  341. static int rsi_setupcard(struct rsi_hw *adapter)
  342. {
  343. struct rsi_91x_sdiodev *dev =
  344. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  345. int status = 0;
  346. rsi_setclock(adapter, 50000);
  347. dev->tx_blk_size = 256;
  348. status = rsi_setblocklength(adapter, dev->tx_blk_size);
  349. if (status)
  350. rsi_dbg(ERR_ZONE,
  351. "%s: Unable to set block length\n", __func__);
  352. return status;
  353. }
  354. /**
  355. * rsi_sdio_read_register() - This function reads one byte of information
  356. * from a register.
  357. * @adapter: Pointer to the adapter structure.
  358. * @addr: Address of the register.
  359. * @data: Pointer to the data that stores the data read.
  360. *
  361. * Return: 0 on success, -1 on failure.
  362. */
  363. int rsi_sdio_read_register(struct rsi_hw *adapter,
  364. u32 addr,
  365. u8 *data)
  366. {
  367. struct rsi_91x_sdiodev *dev =
  368. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  369. u8 fun_num = 0;
  370. int status;
  371. if (likely(dev->sdio_irq_task != current))
  372. sdio_claim_host(dev->pfunction);
  373. if (fun_num == 0)
  374. *data = sdio_f0_readb(dev->pfunction, addr, &status);
  375. else
  376. *data = sdio_readb(dev->pfunction, addr, &status);
  377. if (likely(dev->sdio_irq_task != current))
  378. sdio_release_host(dev->pfunction);
  379. return status;
  380. }
  381. /**
  382. * rsi_sdio_write_register() - This function writes one byte of information
  383. * into a register.
  384. * @adapter: Pointer to the adapter structure.
  385. * @function: Function Number.
  386. * @addr: Address of the register.
  387. * @data: Pointer to the data tha has to be written.
  388. *
  389. * Return: 0 on success, -1 on failure.
  390. */
  391. int rsi_sdio_write_register(struct rsi_hw *adapter,
  392. u8 function,
  393. u32 addr,
  394. u8 *data)
  395. {
  396. struct rsi_91x_sdiodev *dev =
  397. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  398. int status = 0;
  399. if (likely(dev->sdio_irq_task != current))
  400. sdio_claim_host(dev->pfunction);
  401. if (function == 0)
  402. sdio_f0_writeb(dev->pfunction, *data, addr, &status);
  403. else
  404. sdio_writeb(dev->pfunction, *data, addr, &status);
  405. if (likely(dev->sdio_irq_task != current))
  406. sdio_release_host(dev->pfunction);
  407. return status;
  408. }
  409. /**
  410. * rsi_sdio_ack_intr() - This function acks the interrupt received.
  411. * @adapter: Pointer to the adapter structure.
  412. * @int_bit: Interrupt bit to write into register.
  413. *
  414. * Return: None.
  415. */
  416. void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
  417. {
  418. int status;
  419. status = rsi_sdio_write_register(adapter,
  420. 1,
  421. (SDIO_FUN1_INTR_CLR_REG |
  422. RSI_SD_REQUEST_MASTER),
  423. &int_bit);
  424. if (status)
  425. rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
  426. }
  427. /**
  428. * rsi_sdio_read_register_multiple() - This function read multiple bytes of
  429. * information from the SD card.
  430. * @adapter: Pointer to the adapter structure.
  431. * @addr: Address of the register.
  432. * @count: Number of multiple bytes to be read.
  433. * @data: Pointer to the read data.
  434. *
  435. * Return: 0 on success, -1 on failure.
  436. */
  437. static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
  438. u32 addr,
  439. u8 *data,
  440. u16 count)
  441. {
  442. struct rsi_91x_sdiodev *dev =
  443. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  444. u32 status;
  445. if (likely(dev->sdio_irq_task != current))
  446. sdio_claim_host(dev->pfunction);
  447. status = sdio_readsb(dev->pfunction, data, addr, count);
  448. if (likely(dev->sdio_irq_task != current))
  449. sdio_release_host(dev->pfunction);
  450. if (status != 0)
  451. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
  452. return status;
  453. }
  454. /**
  455. * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
  456. * information to the SD card.
  457. * @adapter: Pointer to the adapter structure.
  458. * @addr: Address of the register.
  459. * @data: Pointer to the data that has to be written.
  460. * @count: Number of multiple bytes to be written.
  461. *
  462. * Return: 0 on success, -1 on failure.
  463. */
  464. int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
  465. u32 addr,
  466. u8 *data,
  467. u16 count)
  468. {
  469. struct rsi_91x_sdiodev *dev =
  470. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  471. int status;
  472. if (dev->write_fail > 1) {
  473. rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
  474. return 0;
  475. } else if (dev->write_fail == 1) {
  476. /**
  477. * Assuming it is a CRC failure, we want to allow another
  478. * card write
  479. */
  480. rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
  481. dev->write_fail++;
  482. }
  483. if (likely(dev->sdio_irq_task != current))
  484. sdio_claim_host(dev->pfunction);
  485. status = sdio_writesb(dev->pfunction, addr, data, count);
  486. if (likely(dev->sdio_irq_task != current))
  487. sdio_release_host(dev->pfunction);
  488. if (status) {
  489. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
  490. __func__, status);
  491. dev->write_fail = 2;
  492. } else {
  493. memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
  494. }
  495. return status;
  496. }
  497. static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
  498. u32 base_address,
  499. u32 instructions_sz,
  500. u16 block_size,
  501. u8 *ta_firmware)
  502. {
  503. u32 num_blocks, offset, i;
  504. u16 msb_address, lsb_address;
  505. u8 *temp_buf;
  506. int status;
  507. num_blocks = instructions_sz / block_size;
  508. msb_address = base_address >> 16;
  509. rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
  510. instructions_sz, num_blocks);
  511. temp_buf = kmalloc(block_size, GFP_KERNEL);
  512. if (!temp_buf)
  513. return -ENOMEM;
  514. /* Loading DM ms word in the sdio slave */
  515. status = rsi_sdio_master_access_msword(adapter, msb_address);
  516. if (status < 0) {
  517. rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
  518. goto out_free;
  519. }
  520. for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
  521. memcpy(temp_buf, ta_firmware + offset, block_size);
  522. lsb_address = (u16)base_address;
  523. status = rsi_sdio_write_register_multiple
  524. (adapter,
  525. lsb_address | RSI_SD_REQUEST_MASTER,
  526. temp_buf, block_size);
  527. if (status < 0) {
  528. rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
  529. goto out_free;
  530. }
  531. rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
  532. base_address += block_size;
  533. if ((base_address >> 16) != msb_address) {
  534. msb_address += 1;
  535. /* Loading DM ms word in the sdio slave */
  536. status = rsi_sdio_master_access_msword(adapter,
  537. msb_address);
  538. if (status < 0) {
  539. rsi_dbg(ERR_ZONE,
  540. "%s: Unable to set ms word reg\n",
  541. __func__);
  542. goto out_free;
  543. }
  544. }
  545. }
  546. if (instructions_sz % block_size) {
  547. memset(temp_buf, 0, block_size);
  548. memcpy(temp_buf, ta_firmware + offset,
  549. instructions_sz % block_size);
  550. lsb_address = (u16)base_address;
  551. status = rsi_sdio_write_register_multiple
  552. (adapter,
  553. lsb_address | RSI_SD_REQUEST_MASTER,
  554. temp_buf,
  555. instructions_sz % block_size);
  556. if (status < 0)
  557. goto out_free;
  558. rsi_dbg(INFO_ZONE,
  559. "Written Last Block in Address 0x%x Successfully\n",
  560. offset | RSI_SD_REQUEST_MASTER);
  561. }
  562. status = 0;
  563. out_free:
  564. kfree(temp_buf);
  565. return status;
  566. }
  567. #define FLASH_SIZE_ADDR 0x04000016
  568. static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
  569. u32 *read_buf, u16 size)
  570. {
  571. u32 addr_on_bus, *data;
  572. u16 ms_addr;
  573. int status;
  574. data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
  575. if (!data)
  576. return -ENOMEM;
  577. ms_addr = (addr >> 16);
  578. status = rsi_sdio_master_access_msword(adapter, ms_addr);
  579. if (status < 0) {
  580. rsi_dbg(ERR_ZONE,
  581. "%s: Unable to set ms word to common reg\n",
  582. __func__);
  583. goto err;
  584. }
  585. addr &= 0xFFFF;
  586. addr_on_bus = (addr & 0xFF000000);
  587. if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
  588. (addr_on_bus == 0x0))
  589. addr_on_bus = (addr & ~(0x3));
  590. else
  591. addr_on_bus = addr;
  592. /* Bring TA out of reset */
  593. status = rsi_sdio_read_register_multiple
  594. (adapter,
  595. (addr_on_bus | RSI_SD_REQUEST_MASTER),
  596. (u8 *)data, 4);
  597. if (status < 0) {
  598. rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
  599. goto err;
  600. }
  601. if (size == 2) {
  602. if ((addr & 0x3) == 0)
  603. *read_buf = *data;
  604. else
  605. *read_buf = (*data >> 16);
  606. *read_buf = (*read_buf & 0xFFFF);
  607. } else if (size == 1) {
  608. if ((addr & 0x3) == 0)
  609. *read_buf = *data;
  610. else if ((addr & 0x3) == 1)
  611. *read_buf = (*data >> 8);
  612. else if ((addr & 0x3) == 2)
  613. *read_buf = (*data >> 16);
  614. else
  615. *read_buf = (*data >> 24);
  616. *read_buf = (*read_buf & 0xFF);
  617. } else {
  618. *read_buf = *data;
  619. }
  620. err:
  621. kfree(data);
  622. return status;
  623. }
  624. static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
  625. unsigned long addr,
  626. unsigned long data, u16 size)
  627. {
  628. unsigned long *data_aligned;
  629. int status;
  630. data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
  631. if (!data_aligned)
  632. return -ENOMEM;
  633. if (size == 2) {
  634. *data_aligned = ((data << 16) | (data & 0xFFFF));
  635. } else if (size == 1) {
  636. u32 temp_data = data & 0xFF;
  637. *data_aligned = ((temp_data << 24) | (temp_data << 16) |
  638. (temp_data << 8) | temp_data);
  639. } else {
  640. *data_aligned = data;
  641. }
  642. size = 4;
  643. status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
  644. if (status < 0) {
  645. rsi_dbg(ERR_ZONE,
  646. "%s: Unable to set ms word to common reg\n",
  647. __func__);
  648. kfree(data_aligned);
  649. return -EIO;
  650. }
  651. addr = addr & 0xFFFF;
  652. /* Bring TA out of reset */
  653. status = rsi_sdio_write_register_multiple
  654. (adapter,
  655. (addr | RSI_SD_REQUEST_MASTER),
  656. (u8 *)data_aligned, size);
  657. if (status < 0)
  658. rsi_dbg(ERR_ZONE,
  659. "%s: Unable to do AHB reg write\n", __func__);
  660. kfree(data_aligned);
  661. return status;
  662. }
  663. /**
  664. * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
  665. * @adapter: Pointer to the adapter structure.
  666. * @pkt: Pointer to the data to be written on to the device.
  667. * @len: length of the data to be written on to the device.
  668. *
  669. * Return: 0 on success, -1 on failure.
  670. */
  671. static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
  672. u8 *pkt,
  673. u32 len)
  674. {
  675. struct rsi_91x_sdiodev *dev =
  676. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  677. u32 block_size = dev->tx_blk_size;
  678. u32 num_blocks, address, length;
  679. u32 queueno;
  680. int status;
  681. queueno = ((pkt[1] >> 4) & 0xf);
  682. if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
  683. queueno = RSI_BT_Q;
  684. num_blocks = len / block_size;
  685. if (len % block_size)
  686. num_blocks++;
  687. address = (num_blocks * block_size | (queueno << 12));
  688. length = num_blocks * block_size;
  689. status = rsi_sdio_write_register_multiple(adapter,
  690. address,
  691. (u8 *)pkt,
  692. length);
  693. if (status)
  694. rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
  695. __func__, status);
  696. rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
  697. return status;
  698. }
  699. /**
  700. * rsi_sdio_host_intf_read_pkt() - This function reads the packet
  701. from the device.
  702. * @adapter: Pointer to the adapter data structure.
  703. * @pkt: Pointer to the packet data to be read from the the device.
  704. * @length: Length of the data to be read from the device.
  705. *
  706. * Return: 0 on success, -1 on failure.
  707. */
  708. int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
  709. u8 *pkt,
  710. u32 length)
  711. {
  712. int status = -EINVAL;
  713. if (!length) {
  714. rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
  715. return status;
  716. }
  717. status = rsi_sdio_read_register_multiple(adapter,
  718. length,
  719. (u8 *)pkt,
  720. length); /*num of bytes*/
  721. if (status)
  722. rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
  723. status);
  724. return status;
  725. }
  726. /**
  727. * rsi_init_sdio_interface() - This function does init specific to SDIO.
  728. *
  729. * @adapter: Pointer to the adapter data structure.
  730. * @pkt: Pointer to the packet data to be read from the the device.
  731. *
  732. * Return: 0 on success, -1 on failure.
  733. */
  734. static int rsi_init_sdio_interface(struct rsi_hw *adapter,
  735. struct sdio_func *pfunction)
  736. {
  737. struct rsi_91x_sdiodev *rsi_91x_dev;
  738. int status;
  739. rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
  740. if (!rsi_91x_dev)
  741. return -ENOMEM;
  742. adapter->rsi_dev = rsi_91x_dev;
  743. sdio_claim_host(pfunction);
  744. pfunction->enable_timeout = 100;
  745. status = sdio_enable_func(pfunction);
  746. if (status) {
  747. rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
  748. sdio_release_host(pfunction);
  749. return status;
  750. }
  751. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  752. rsi_91x_dev->pfunction = pfunction;
  753. adapter->device = &pfunction->dev;
  754. sdio_set_drvdata(pfunction, adapter);
  755. status = rsi_setupcard(adapter);
  756. if (status) {
  757. rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
  758. goto fail;
  759. }
  760. rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
  761. status = rsi_init_sdio_slave_regs(adapter);
  762. if (status) {
  763. rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
  764. goto fail;
  765. }
  766. sdio_release_host(pfunction);
  767. adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
  768. adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
  769. #ifdef CONFIG_RSI_DEBUGFS
  770. adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
  771. #endif
  772. return 0;
  773. fail:
  774. sdio_disable_func(pfunction);
  775. sdio_release_host(pfunction);
  776. return status;
  777. }
  778. static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
  779. {
  780. struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
  781. struct sdio_func *pfunction = sdev->pfunction;
  782. int ii;
  783. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  784. skb_queue_purge(&adapter->priv->tx_queue[ii]);
  785. /* Initialize device again */
  786. sdio_claim_host(pfunction);
  787. sdio_release_irq(pfunction);
  788. rsi_reset_card(pfunction);
  789. sdio_enable_func(pfunction);
  790. rsi_setupcard(adapter);
  791. rsi_init_sdio_slave_regs(adapter);
  792. sdio_claim_irq(pfunction, rsi_handle_interrupt);
  793. rsi_hal_device_init(adapter);
  794. sdio_release_host(pfunction);
  795. return 0;
  796. }
  797. static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
  798. {
  799. int status;
  800. u32 addr;
  801. u8 *data;
  802. data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
  803. if (!data)
  804. return -ENOMEM;
  805. status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
  806. if (status < 0) {
  807. rsi_dbg(ERR_ZONE,
  808. "Unable to set ms word to common reg\n");
  809. goto err;
  810. }
  811. rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
  812. put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
  813. addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
  814. status = rsi_sdio_write_register_multiple(adapter, addr,
  815. (u8 *)data,
  816. RSI_9116_REG_SIZE);
  817. if (status < 0) {
  818. rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
  819. goto err;
  820. }
  821. put_unaligned_le32(TA_SOFT_RST_CLR, data);
  822. addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
  823. status = rsi_sdio_write_register_multiple(adapter, addr,
  824. (u8 *)data,
  825. RSI_9116_REG_SIZE);
  826. if (status < 0) {
  827. rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
  828. goto err;
  829. }
  830. put_unaligned_le32(TA_PC_ZERO, data);
  831. addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
  832. status = rsi_sdio_write_register_multiple(adapter, addr,
  833. (u8 *)data,
  834. RSI_9116_REG_SIZE);
  835. if (status < 0) {
  836. rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
  837. status = -EINVAL;
  838. goto err;
  839. }
  840. put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
  841. addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
  842. status = rsi_sdio_write_register_multiple(adapter, addr,
  843. (u8 *)data,
  844. RSI_9116_REG_SIZE);
  845. if (status < 0) {
  846. rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
  847. goto err;
  848. }
  849. status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
  850. if (status < 0) {
  851. rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
  852. goto err;
  853. }
  854. rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
  855. err:
  856. kfree(data);
  857. return status;
  858. }
  859. static struct rsi_host_intf_ops sdio_host_intf_ops = {
  860. .write_pkt = rsi_sdio_host_intf_write_pkt,
  861. .read_pkt = rsi_sdio_host_intf_read_pkt,
  862. .master_access_msword = rsi_sdio_master_access_msword,
  863. .read_reg_multiple = rsi_sdio_read_register_multiple,
  864. .write_reg_multiple = rsi_sdio_write_register_multiple,
  865. .master_reg_read = rsi_sdio_master_reg_read,
  866. .master_reg_write = rsi_sdio_master_reg_write,
  867. .load_data_master_write = rsi_sdio_load_data_master_write,
  868. .reinit_device = rsi_sdio_reinit_device,
  869. .ta_reset = rsi_sdio_ta_reset,
  870. };
  871. /**
  872. * rsi_probe() - This function is called by kernel when the driver provided
  873. * Vendor and device IDs are matched. All the initialization
  874. * work is done here.
  875. * @pfunction: Pointer to the sdio_func structure.
  876. * @id: Pointer to sdio_device_id structure.
  877. *
  878. * Return: 0 on success, 1 on failure.
  879. */
  880. static int rsi_probe(struct sdio_func *pfunction,
  881. const struct sdio_device_id *id)
  882. {
  883. struct rsi_hw *adapter;
  884. struct rsi_91x_sdiodev *sdev;
  885. int status = -EINVAL;
  886. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  887. adapter = rsi_91x_init(dev_oper_mode);
  888. if (!adapter) {
  889. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  890. __func__);
  891. return -EINVAL;
  892. }
  893. adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
  894. adapter->host_intf_ops = &sdio_host_intf_ops;
  895. if (rsi_init_sdio_interface(adapter, pfunction)) {
  896. rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
  897. __func__);
  898. status = -EIO;
  899. goto fail_free_adapter;
  900. }
  901. if (pfunction->device == RSI_SDIO_PID_9113) {
  902. rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
  903. adapter->device_model = RSI_DEV_9113;
  904. } else if (pfunction->device == RSI_SDIO_PID_9116) {
  905. rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
  906. adapter->device_model = RSI_DEV_9116;
  907. } else {
  908. rsi_dbg(ERR_ZONE,
  909. "%s: Unsupported RSI device id 0x%x\n", __func__,
  910. pfunction->device);
  911. goto fail_free_adapter;
  912. }
  913. sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  914. rsi_init_event(&sdev->rx_thread.event);
  915. status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
  916. rsi_sdio_rx_thread, "SDIO-RX-Thread");
  917. if (status) {
  918. rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
  919. goto fail_kill_thread;
  920. }
  921. sdio_claim_host(pfunction);
  922. if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
  923. rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
  924. sdio_release_host(pfunction);
  925. status = -EIO;
  926. goto fail_claim_irq;
  927. }
  928. sdio_release_host(pfunction);
  929. rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
  930. if (rsi_hal_device_init(adapter)) {
  931. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
  932. status = -EINVAL;
  933. goto fail_dev_init;
  934. }
  935. rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
  936. if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
  937. rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
  938. status = -EIO;
  939. goto fail_dev_init;
  940. }
  941. adapter->priv->hibernate_resume = false;
  942. adapter->priv->reinit_hw = false;
  943. return 0;
  944. fail_dev_init:
  945. sdio_claim_host(pfunction);
  946. sdio_release_irq(pfunction);
  947. sdio_release_host(pfunction);
  948. fail_claim_irq:
  949. rsi_kill_thread(&sdev->rx_thread);
  950. fail_kill_thread:
  951. sdio_claim_host(pfunction);
  952. sdio_disable_func(pfunction);
  953. sdio_release_host(pfunction);
  954. fail_free_adapter:
  955. rsi_91x_deinit(adapter);
  956. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  957. return status;
  958. }
  959. static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
  960. u16 len_in_bits)
  961. {
  962. rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
  963. ((addr << 6) | ((data >> 16) & 0xffff)), 2);
  964. rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
  965. (data & 0xffff), 2);
  966. rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
  967. RSI_GSPI_CTRL_REG0_VALUE, 2);
  968. rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
  969. ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
  970. msleep(20);
  971. }
  972. /*This function resets and re-initializes the chip.*/
  973. static void rsi_reset_chip(struct rsi_hw *adapter)
  974. {
  975. u8 *data;
  976. u8 sdio_interrupt_status = 0;
  977. u8 request = 1;
  978. int ret;
  979. data = kzalloc(sizeof(u32), GFP_KERNEL);
  980. if (!data)
  981. return;
  982. rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
  983. ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
  984. if (ret < 0) {
  985. rsi_dbg(ERR_ZONE,
  986. "%s: Failed to write SDIO wakeup register\n", __func__);
  987. goto err;
  988. }
  989. msleep(20);
  990. ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
  991. &sdio_interrupt_status);
  992. if (ret < 0) {
  993. rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
  994. __func__);
  995. goto err;
  996. }
  997. rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
  998. __func__, sdio_interrupt_status);
  999. /* Put Thread-Arch processor on hold */
  1000. if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
  1001. rsi_dbg(ERR_ZONE,
  1002. "%s: Unable to set ms word to common reg\n",
  1003. __func__);
  1004. goto err;
  1005. }
  1006. put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
  1007. if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
  1008. RSI_SD_REQUEST_MASTER,
  1009. data, 4)) {
  1010. rsi_dbg(ERR_ZONE,
  1011. "%s: Unable to hold Thread-Arch processor threads\n",
  1012. __func__);
  1013. goto err;
  1014. }
  1015. /* This msleep will ensure Thread-Arch processor to go to hold
  1016. * and any pending dma transfers to rf spi in device to finish.
  1017. */
  1018. msleep(100);
  1019. if (adapter->device_model != RSI_DEV_9116) {
  1020. ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
  1021. ulp_read_write(adapter,
  1022. RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
  1023. ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
  1024. 32);
  1025. ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
  1026. RSI_ULP_WRITE_50, 32);
  1027. ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
  1028. RSI_ULP_WRITE_0, 32);
  1029. ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
  1030. RSI_ULP_TIMER_ENABLE, 32);
  1031. } else {
  1032. if ((rsi_sdio_master_reg_write(adapter,
  1033. NWP_WWD_INTERRUPT_TIMER,
  1034. NWP_WWD_INT_TIMER_CLKS,
  1035. RSI_9116_REG_SIZE)) < 0) {
  1036. rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
  1037. }
  1038. if ((rsi_sdio_master_reg_write(adapter,
  1039. NWP_WWD_SYSTEM_RESET_TIMER,
  1040. NWP_WWD_SYS_RESET_TIMER_CLKS,
  1041. RSI_9116_REG_SIZE)) < 0) {
  1042. rsi_dbg(ERR_ZONE,
  1043. "Failed to write to system reset timer\n");
  1044. }
  1045. if ((rsi_sdio_master_reg_write(adapter,
  1046. NWP_WWD_MODE_AND_RSTART,
  1047. NWP_WWD_TIMER_DISABLE,
  1048. RSI_9116_REG_SIZE)) < 0) {
  1049. rsi_dbg(ERR_ZONE,
  1050. "Failed to write to mode and restart\n");
  1051. }
  1052. rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
  1053. }
  1054. /* This msleep will be sufficient for the ulp
  1055. * read write operations to complete for chip reset.
  1056. */
  1057. msleep(500);
  1058. err:
  1059. kfree(data);
  1060. return;
  1061. }
  1062. /**
  1063. * rsi_disconnect() - This function performs the reverse of the probe function.
  1064. * @pfunction: Pointer to the sdio_func structure.
  1065. *
  1066. * Return: void.
  1067. */
  1068. static void rsi_disconnect(struct sdio_func *pfunction)
  1069. {
  1070. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1071. struct rsi_91x_sdiodev *dev;
  1072. if (!adapter)
  1073. return;
  1074. dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  1075. rsi_kill_thread(&dev->rx_thread);
  1076. sdio_claim_host(pfunction);
  1077. sdio_release_irq(pfunction);
  1078. sdio_release_host(pfunction);
  1079. mdelay(10);
  1080. rsi_mac80211_detach(adapter);
  1081. mdelay(10);
  1082. if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
  1083. adapter->priv->bt_adapter) {
  1084. rsi_bt_ops.detach(adapter->priv->bt_adapter);
  1085. adapter->priv->bt_adapter = NULL;
  1086. }
  1087. /* Reset Chip */
  1088. rsi_reset_chip(adapter);
  1089. /* Resetting to take care of the case, where-in driver is re-loaded */
  1090. sdio_claim_host(pfunction);
  1091. rsi_reset_card(pfunction);
  1092. sdio_disable_func(pfunction);
  1093. sdio_release_host(pfunction);
  1094. dev->write_fail = 2;
  1095. rsi_91x_deinit(adapter);
  1096. rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
  1097. }
  1098. #ifdef CONFIG_PM
  1099. static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
  1100. {
  1101. struct rsi_91x_sdiodev *dev =
  1102. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  1103. struct sdio_func *func = dev->pfunction;
  1104. int ret;
  1105. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  1106. if (ret)
  1107. rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
  1108. return ret;
  1109. }
  1110. static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
  1111. {
  1112. struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
  1113. u8 isr_status = 0, data = 0;
  1114. int ret;
  1115. unsigned long t1;
  1116. rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
  1117. t1 = jiffies;
  1118. do {
  1119. rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
  1120. &isr_status);
  1121. rsi_dbg(INFO_ZONE, ".");
  1122. } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
  1123. rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
  1124. sdio_claim_host(pfunc);
  1125. ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
  1126. if (ret < 0) {
  1127. rsi_dbg(ERR_ZONE,
  1128. "%s: Failed to read int enable register\n",
  1129. __func__);
  1130. goto done;
  1131. }
  1132. data &= RSI_INT_ENABLE_MASK;
  1133. ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
  1134. if (ret < 0) {
  1135. rsi_dbg(ERR_ZONE,
  1136. "%s: Failed to write to int enable register\n",
  1137. __func__);
  1138. goto done;
  1139. }
  1140. ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
  1141. if (ret < 0) {
  1142. rsi_dbg(ERR_ZONE,
  1143. "%s: Failed to read int enable register\n",
  1144. __func__);
  1145. goto done;
  1146. }
  1147. rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
  1148. done:
  1149. sdio_release_host(pfunc);
  1150. return ret;
  1151. }
  1152. static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
  1153. {
  1154. u8 data;
  1155. int ret;
  1156. struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
  1157. struct rsi_common *common = adapter->priv;
  1158. sdio_claim_host(pfunc);
  1159. ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
  1160. if (ret < 0) {
  1161. rsi_dbg(ERR_ZONE,
  1162. "%s: Failed to read int enable register\n", __func__);
  1163. goto done;
  1164. }
  1165. data |= ~RSI_INT_ENABLE_MASK & 0xff;
  1166. ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
  1167. if (ret < 0) {
  1168. rsi_dbg(ERR_ZONE,
  1169. "%s: Failed to write to int enable register\n",
  1170. __func__);
  1171. goto done;
  1172. }
  1173. if ((common->wow_flags & RSI_WOW_ENABLED) &&
  1174. (common->wow_flags & RSI_WOW_NO_CONNECTION))
  1175. rsi_dbg(ERR_ZONE,
  1176. "##### Device can not wake up through WLAN\n");
  1177. ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
  1178. if (ret < 0) {
  1179. rsi_dbg(ERR_ZONE,
  1180. "%s: Failed to read int enable register\n", __func__);
  1181. goto done;
  1182. }
  1183. rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
  1184. done:
  1185. sdio_release_host(pfunc);
  1186. return ret;
  1187. }
  1188. static int rsi_suspend(struct device *dev)
  1189. {
  1190. int ret;
  1191. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1192. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1193. struct rsi_common *common;
  1194. if (!adapter) {
  1195. rsi_dbg(ERR_ZONE, "Device is not ready\n");
  1196. return -ENODEV;
  1197. }
  1198. common = adapter->priv;
  1199. rsi_sdio_disable_interrupts(pfunction);
  1200. ret = rsi_set_sdio_pm_caps(adapter);
  1201. if (ret)
  1202. rsi_dbg(INFO_ZONE,
  1203. "Setting power management caps failed\n");
  1204. common->fsm_state = FSM_CARD_NOT_READY;
  1205. return 0;
  1206. }
  1207. static int rsi_resume(struct device *dev)
  1208. {
  1209. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1210. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1211. struct rsi_common *common = adapter->priv;
  1212. common->fsm_state = FSM_MAC_INIT_DONE;
  1213. rsi_sdio_enable_interrupts(pfunction);
  1214. return 0;
  1215. }
  1216. static int rsi_freeze(struct device *dev)
  1217. {
  1218. int ret;
  1219. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1220. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1221. struct rsi_common *common;
  1222. struct rsi_91x_sdiodev *sdev;
  1223. rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
  1224. if (!adapter) {
  1225. rsi_dbg(ERR_ZONE, "Device is not ready\n");
  1226. return -ENODEV;
  1227. }
  1228. common = adapter->priv;
  1229. sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  1230. if ((common->wow_flags & RSI_WOW_ENABLED) &&
  1231. (common->wow_flags & RSI_WOW_NO_CONNECTION))
  1232. rsi_dbg(ERR_ZONE,
  1233. "##### Device can not wake up through WLAN\n");
  1234. if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
  1235. common->bt_adapter) {
  1236. rsi_bt_ops.detach(common->bt_adapter);
  1237. common->bt_adapter = NULL;
  1238. }
  1239. ret = rsi_sdio_disable_interrupts(pfunction);
  1240. if (sdev->write_fail)
  1241. rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
  1242. ret = rsi_set_sdio_pm_caps(adapter);
  1243. if (ret)
  1244. rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
  1245. rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
  1246. return 0;
  1247. }
  1248. static int rsi_thaw(struct device *dev)
  1249. {
  1250. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1251. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1252. struct rsi_common *common = adapter->priv;
  1253. rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
  1254. common->hibernate_resume = true;
  1255. common->fsm_state = FSM_CARD_NOT_READY;
  1256. common->iface_down = true;
  1257. rsi_sdio_enable_interrupts(pfunction);
  1258. rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
  1259. return 0;
  1260. }
  1261. static void rsi_shutdown(struct device *dev)
  1262. {
  1263. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1264. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1265. struct rsi_91x_sdiodev *sdev =
  1266. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  1267. struct ieee80211_hw *hw = adapter->hw;
  1268. struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
  1269. rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
  1270. if (rsi_config_wowlan(adapter, wowlan))
  1271. rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
  1272. if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
  1273. adapter->priv->bt_adapter) {
  1274. rsi_bt_ops.detach(adapter->priv->bt_adapter);
  1275. adapter->priv->bt_adapter = NULL;
  1276. }
  1277. rsi_sdio_disable_interrupts(sdev->pfunction);
  1278. if (sdev->write_fail)
  1279. rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
  1280. if (rsi_set_sdio_pm_caps(adapter))
  1281. rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
  1282. rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
  1283. }
  1284. static int rsi_restore(struct device *dev)
  1285. {
  1286. struct sdio_func *pfunction = dev_to_sdio_func(dev);
  1287. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  1288. struct rsi_common *common = adapter->priv;
  1289. rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
  1290. common->hibernate_resume = true;
  1291. common->fsm_state = FSM_FW_NOT_LOADED;
  1292. common->iface_down = true;
  1293. adapter->sc_nvifs = 0;
  1294. adapter->ps_state = PS_NONE;
  1295. common->wow_flags = 0;
  1296. common->iface_down = false;
  1297. rsi_dbg(INFO_ZONE, "RSI module restored\n");
  1298. return 0;
  1299. }
  1300. static const struct dev_pm_ops rsi_pm_ops = {
  1301. .suspend = rsi_suspend,
  1302. .resume_noirq = rsi_resume,
  1303. .freeze = rsi_freeze,
  1304. .thaw = rsi_thaw,
  1305. .restore = rsi_restore,
  1306. };
  1307. #endif
  1308. static const struct sdio_device_id rsi_dev_table[] = {
  1309. { SDIO_DEVICE(RSI_SDIO_VENDOR_ID, RSI_SDIO_PID_9113) },
  1310. { SDIO_DEVICE(RSI_SDIO_VENDOR_ID, RSI_SDIO_PID_9116) },
  1311. { /* Blank */},
  1312. };
  1313. static struct sdio_driver rsi_driver = {
  1314. .name = "RSI-SDIO WLAN",
  1315. .probe = rsi_probe,
  1316. .remove = rsi_disconnect,
  1317. .id_table = rsi_dev_table,
  1318. #ifdef CONFIG_PM
  1319. .drv = {
  1320. .pm = &rsi_pm_ops,
  1321. .shutdown = rsi_shutdown,
  1322. }
  1323. #endif
  1324. };
  1325. /**
  1326. * rsi_module_init() - This function registers the sdio module.
  1327. * @void: Void.
  1328. *
  1329. * Return: 0 on success.
  1330. */
  1331. static int rsi_module_init(void)
  1332. {
  1333. int ret;
  1334. ret = sdio_register_driver(&rsi_driver);
  1335. rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
  1336. return ret;
  1337. }
  1338. /**
  1339. * rsi_module_exit() - This function unregisters the sdio module.
  1340. * @void: Void.
  1341. *
  1342. * Return: None.
  1343. */
  1344. static void rsi_module_exit(void)
  1345. {
  1346. sdio_unregister_driver(&rsi_driver);
  1347. rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
  1348. }
  1349. module_init(rsi_module_init);
  1350. module_exit(rsi_module_exit);
  1351. MODULE_AUTHOR("Redpine Signals Inc");
  1352. MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
  1353. MODULE_SUPPORTED_DEVICE("RSI-91x");
  1354. MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
  1355. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  1356. MODULE_VERSION("0.1");
  1357. MODULE_LICENSE("Dual BSD/GPL");