rsi_91x_sdio.c 37 KB

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