rsi_91x_sdio.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_hal.h"
  21. /**
  22. * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
  23. * @rw: Read/write
  24. * @func: function number
  25. * @raw: indicates whether to perform read after write
  26. * @address: address to which to read/write
  27. * @writedata: data to write
  28. *
  29. * Return: argument
  30. */
  31. static u32 rsi_sdio_set_cmd52_arg(bool rw,
  32. u8 func,
  33. u8 raw,
  34. u32 address,
  35. u8 writedata)
  36. {
  37. return ((rw & 1) << 31) | ((func & 0x7) << 28) |
  38. ((raw & 1) << 27) | (1 << 26) |
  39. ((address & 0x1FFFF) << 9) | (1 << 8) |
  40. (writedata & 0xFF);
  41. }
  42. /**
  43. * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
  44. * @card: Pointer to the mmc_card.
  45. * @address: Address to write.
  46. * @byte: Data to write.
  47. *
  48. * Return: Write status.
  49. */
  50. static int rsi_cmd52writebyte(struct mmc_card *card,
  51. u32 address,
  52. u8 byte)
  53. {
  54. struct mmc_command io_cmd;
  55. u32 arg;
  56. memset(&io_cmd, 0, sizeof(io_cmd));
  57. arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
  58. io_cmd.opcode = SD_IO_RW_DIRECT;
  59. io_cmd.arg = arg;
  60. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  61. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  62. }
  63. /**
  64. * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
  65. * @card: Pointer to the mmc_card.
  66. * @address: Address to read from.
  67. * @byte: Variable to store read value.
  68. *
  69. * Return: Read status.
  70. */
  71. static int rsi_cmd52readbyte(struct mmc_card *card,
  72. u32 address,
  73. u8 *byte)
  74. {
  75. struct mmc_command io_cmd;
  76. u32 arg;
  77. int err;
  78. memset(&io_cmd, 0, sizeof(io_cmd));
  79. arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
  80. io_cmd.opcode = SD_IO_RW_DIRECT;
  81. io_cmd.arg = arg;
  82. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  83. err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  84. if ((!err) && (byte))
  85. *byte = io_cmd.resp[0] & 0xFF;
  86. return err;
  87. }
  88. /**
  89. * rsi_issue_sdiocommand() - This function issues sdio commands.
  90. * @func: Pointer to the sdio_func structure.
  91. * @opcode: Opcode value.
  92. * @arg: Arguments to pass.
  93. * @flags: Flags which are set.
  94. * @resp: Pointer to store response.
  95. *
  96. * Return: err: command status as 0 or -1.
  97. */
  98. static int rsi_issue_sdiocommand(struct sdio_func *func,
  99. u32 opcode,
  100. u32 arg,
  101. u32 flags,
  102. u32 *resp)
  103. {
  104. struct mmc_command cmd;
  105. struct mmc_host *host;
  106. int err;
  107. host = func->card->host;
  108. memset(&cmd, 0, sizeof(struct mmc_command));
  109. cmd.opcode = opcode;
  110. cmd.arg = arg;
  111. cmd.flags = flags;
  112. err = mmc_wait_for_cmd(host, &cmd, 3);
  113. if ((!err) && (resp))
  114. *resp = cmd.resp[0];
  115. return err;
  116. }
  117. /**
  118. * rsi_handle_interrupt() - This function is called upon the occurence
  119. * of an interrupt.
  120. * @function: Pointer to the sdio_func structure.
  121. *
  122. * Return: None.
  123. */
  124. static void rsi_handle_interrupt(struct sdio_func *function)
  125. {
  126. struct rsi_hw *adapter = sdio_get_drvdata(function);
  127. struct rsi_91x_sdiodev *dev =
  128. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  129. if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
  130. return;
  131. dev->sdio_irq_task = current;
  132. rsi_interrupt_handler(adapter);
  133. dev->sdio_irq_task = NULL;
  134. }
  135. /**
  136. * rsi_reset_card() - This function resets and re-initializes the card.
  137. * @pfunction: Pointer to the sdio_func structure.
  138. *
  139. * Return: None.
  140. */
  141. static void rsi_reset_card(struct sdio_func *pfunction)
  142. {
  143. int ret = 0;
  144. int err;
  145. struct mmc_card *card = pfunction->card;
  146. struct mmc_host *host = card->host;
  147. s32 bit = (fls(host->ocr_avail) - 1);
  148. u8 cmd52_resp;
  149. u32 clock, resp, i;
  150. u16 rca;
  151. /* Reset 9110 chip */
  152. ret = rsi_cmd52writebyte(pfunction->card,
  153. SDIO_CCCR_ABORT,
  154. (1 << 3));
  155. /* Card will not send any response as it is getting reset immediately
  156. * Hence expect a timeout status from host controller
  157. */
  158. if (ret != -ETIMEDOUT)
  159. rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
  160. /* Wait for few milli seconds to get rid of residue charges if any */
  161. msleep(20);
  162. /* Initialize the SDIO card */
  163. host->ios.vdd = bit;
  164. host->ios.chip_select = MMC_CS_DONTCARE;
  165. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  166. host->ios.power_mode = MMC_POWER_UP;
  167. host->ios.bus_width = MMC_BUS_WIDTH_1;
  168. host->ios.timing = MMC_TIMING_LEGACY;
  169. host->ops->set_ios(host, &host->ios);
  170. /*
  171. * This delay should be sufficient to allow the power supply
  172. * to reach the minimum voltage.
  173. */
  174. msleep(20);
  175. host->ios.clock = host->f_min;
  176. host->ios.power_mode = MMC_POWER_ON;
  177. host->ops->set_ios(host, &host->ios);
  178. /*
  179. * This delay must be at least 74 clock sizes, or 1 ms, or the
  180. * time required to reach a stable voltage.
  181. */
  182. msleep(20);
  183. /* Issue CMD0. Goto idle state */
  184. host->ios.chip_select = MMC_CS_HIGH;
  185. host->ops->set_ios(host, &host->ios);
  186. msleep(20);
  187. err = rsi_issue_sdiocommand(pfunction,
  188. MMC_GO_IDLE_STATE,
  189. 0,
  190. (MMC_RSP_NONE | MMC_CMD_BC),
  191. NULL);
  192. host->ios.chip_select = MMC_CS_DONTCARE;
  193. host->ops->set_ios(host, &host->ios);
  194. msleep(20);
  195. host->use_spi_crc = 0;
  196. if (err)
  197. rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
  198. /* Issue CMD5, arg = 0 */
  199. err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND, 0,
  200. (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
  201. if (err)
  202. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n", __func__, err);
  203. card->ocr = resp;
  204. /* Issue CMD5, arg = ocr. Wait till card is ready */
  205. for (i = 0; i < 100; i++) {
  206. err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
  207. card->ocr,
  208. (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
  209. if (err) {
  210. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
  211. __func__, err);
  212. break;
  213. }
  214. if (resp & MMC_CARD_BUSY)
  215. break;
  216. msleep(20);
  217. }
  218. if ((i == 100) || (err)) {
  219. rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
  220. __func__, i, err);
  221. return;
  222. }
  223. /* Issue CMD3, get RCA */
  224. err = rsi_issue_sdiocommand(pfunction,
  225. SD_SEND_RELATIVE_ADDR,
  226. 0,
  227. (MMC_RSP_R6 | MMC_CMD_BCR),
  228. &resp);
  229. if (err) {
  230. rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
  231. return;
  232. }
  233. rca = resp >> 16;
  234. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  235. host->ops->set_ios(host, &host->ios);
  236. /* Issue CMD7, select card */
  237. err = rsi_issue_sdiocommand(pfunction,
  238. MMC_SELECT_CARD,
  239. (rca << 16),
  240. (MMC_RSP_R1 | MMC_CMD_AC),
  241. NULL);
  242. if (err) {
  243. rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
  244. return;
  245. }
  246. /* Enable high speed */
  247. if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
  248. rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
  249. err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
  250. if (err) {
  251. rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
  252. __func__, err);
  253. } else {
  254. err = rsi_cmd52writebyte(card,
  255. SDIO_CCCR_SPEED,
  256. (cmd52_resp | SDIO_SPEED_EHS));
  257. if (err) {
  258. rsi_dbg(ERR_ZONE,
  259. "%s: CCR speed regwrite failed %d\n",
  260. __func__, err);
  261. return;
  262. }
  263. host->ios.timing = MMC_TIMING_SD_HS;
  264. host->ops->set_ios(host, &host->ios);
  265. }
  266. }
  267. /* Set clock */
  268. if (mmc_card_hs(card))
  269. clock = 50000000;
  270. else
  271. clock = card->cis.max_dtr;
  272. if (clock > host->f_max)
  273. clock = host->f_max;
  274. host->ios.clock = clock;
  275. host->ops->set_ios(host, &host->ios);
  276. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  277. /* CMD52: Set bus width & disable card detect resistor */
  278. err = rsi_cmd52writebyte(card,
  279. SDIO_CCCR_IF,
  280. (SDIO_BUS_CD_DISABLE |
  281. SDIO_BUS_WIDTH_4BIT));
  282. if (err) {
  283. rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
  284. __func__, err);
  285. return;
  286. }
  287. host->ios.bus_width = MMC_BUS_WIDTH_4;
  288. host->ops->set_ios(host, &host->ios);
  289. }
  290. }
  291. /**
  292. * rsi_setclock() - This function sets the clock frequency.
  293. * @adapter: Pointer to the adapter structure.
  294. * @freq: Clock frequency.
  295. *
  296. * Return: None.
  297. */
  298. static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
  299. {
  300. struct rsi_91x_sdiodev *dev =
  301. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  302. struct mmc_host *host = dev->pfunction->card->host;
  303. u32 clock;
  304. clock = freq * 1000;
  305. if (clock > host->f_max)
  306. clock = host->f_max;
  307. host->ios.clock = clock;
  308. host->ops->set_ios(host, &host->ios);
  309. }
  310. /**
  311. * rsi_setblocklength() - This function sets the host block length.
  312. * @adapter: Pointer to the adapter structure.
  313. * @length: Block length to be set.
  314. *
  315. * Return: status: 0 on success, -1 on failure.
  316. */
  317. static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
  318. {
  319. struct rsi_91x_sdiodev *dev =
  320. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  321. int status;
  322. rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
  323. status = sdio_set_block_size(dev->pfunction, length);
  324. dev->pfunction->max_blksize = 256;
  325. adapter->block_size = dev->pfunction->max_blksize;
  326. rsi_dbg(INFO_ZONE,
  327. "%s: Operational blk length is %d\n", __func__, length);
  328. return status;
  329. }
  330. /**
  331. * rsi_setupcard() - This function queries and sets the card's features.
  332. * @adapter: Pointer to the adapter structure.
  333. *
  334. * Return: status: 0 on success, -1 on failure.
  335. */
  336. static int rsi_setupcard(struct rsi_hw *adapter)
  337. {
  338. struct rsi_91x_sdiodev *dev =
  339. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  340. int status = 0;
  341. rsi_setclock(adapter, 50000);
  342. dev->tx_blk_size = 256;
  343. status = rsi_setblocklength(adapter, dev->tx_blk_size);
  344. if (status)
  345. rsi_dbg(ERR_ZONE,
  346. "%s: Unable to set block length\n", __func__);
  347. return status;
  348. }
  349. /**
  350. * rsi_sdio_read_register() - This function reads one byte of information
  351. * from a register.
  352. * @adapter: Pointer to the adapter structure.
  353. * @addr: Address of the register.
  354. * @data: Pointer to the data that stores the data read.
  355. *
  356. * Return: 0 on success, -1 on failure.
  357. */
  358. int rsi_sdio_read_register(struct rsi_hw *adapter,
  359. u32 addr,
  360. u8 *data)
  361. {
  362. struct rsi_91x_sdiodev *dev =
  363. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  364. u8 fun_num = 0;
  365. int status;
  366. if (likely(dev->sdio_irq_task != current))
  367. sdio_claim_host(dev->pfunction);
  368. if (fun_num == 0)
  369. *data = sdio_f0_readb(dev->pfunction, addr, &status);
  370. else
  371. *data = sdio_readb(dev->pfunction, addr, &status);
  372. if (likely(dev->sdio_irq_task != current))
  373. sdio_release_host(dev->pfunction);
  374. return status;
  375. }
  376. /**
  377. * rsi_sdio_write_register() - This function writes one byte of information
  378. * into a register.
  379. * @adapter: Pointer to the adapter structure.
  380. * @function: Function Number.
  381. * @addr: Address of the register.
  382. * @data: Pointer to the data tha has to be written.
  383. *
  384. * Return: 0 on success, -1 on failure.
  385. */
  386. int rsi_sdio_write_register(struct rsi_hw *adapter,
  387. u8 function,
  388. u32 addr,
  389. u8 *data)
  390. {
  391. struct rsi_91x_sdiodev *dev =
  392. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  393. int status = 0;
  394. if (likely(dev->sdio_irq_task != current))
  395. sdio_claim_host(dev->pfunction);
  396. if (function == 0)
  397. sdio_f0_writeb(dev->pfunction, *data, addr, &status);
  398. else
  399. sdio_writeb(dev->pfunction, *data, addr, &status);
  400. if (likely(dev->sdio_irq_task != current))
  401. sdio_release_host(dev->pfunction);
  402. return status;
  403. }
  404. /**
  405. * rsi_sdio_ack_intr() - This function acks the interrupt received.
  406. * @adapter: Pointer to the adapter structure.
  407. * @int_bit: Interrupt bit to write into register.
  408. *
  409. * Return: None.
  410. */
  411. void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
  412. {
  413. int status;
  414. status = rsi_sdio_write_register(adapter,
  415. 1,
  416. (SDIO_FUN1_INTR_CLR_REG |
  417. RSI_SD_REQUEST_MASTER),
  418. &int_bit);
  419. if (status)
  420. rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
  421. }
  422. /**
  423. * rsi_sdio_read_register_multiple() - This function read multiple bytes of
  424. * information from the SD card.
  425. * @adapter: Pointer to the adapter structure.
  426. * @addr: Address of the register.
  427. * @count: Number of multiple bytes to be read.
  428. * @data: Pointer to the read data.
  429. *
  430. * Return: 0 on success, -1 on failure.
  431. */
  432. static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
  433. u32 addr,
  434. u8 *data,
  435. u16 count)
  436. {
  437. struct rsi_91x_sdiodev *dev =
  438. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  439. u32 status;
  440. if (likely(dev->sdio_irq_task != current))
  441. sdio_claim_host(dev->pfunction);
  442. status = sdio_readsb(dev->pfunction, data, addr, count);
  443. if (likely(dev->sdio_irq_task != current))
  444. sdio_release_host(dev->pfunction);
  445. if (status != 0)
  446. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
  447. return status;
  448. }
  449. /**
  450. * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
  451. * information to the SD card.
  452. * @adapter: Pointer to the adapter structure.
  453. * @addr: Address of the register.
  454. * @data: Pointer to the data that has to be written.
  455. * @count: Number of multiple bytes to be written.
  456. *
  457. * Return: 0 on success, -1 on failure.
  458. */
  459. int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
  460. u32 addr,
  461. u8 *data,
  462. u16 count)
  463. {
  464. struct rsi_91x_sdiodev *dev =
  465. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  466. int status;
  467. if (dev->write_fail > 1) {
  468. rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
  469. return 0;
  470. } else if (dev->write_fail == 1) {
  471. /**
  472. * Assuming it is a CRC failure, we want to allow another
  473. * card write
  474. */
  475. rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
  476. dev->write_fail++;
  477. }
  478. if (likely(dev->sdio_irq_task != current))
  479. sdio_claim_host(dev->pfunction);
  480. status = sdio_writesb(dev->pfunction, addr, data, count);
  481. if (likely(dev->sdio_irq_task != current))
  482. sdio_release_host(dev->pfunction);
  483. if (status) {
  484. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
  485. __func__, status);
  486. dev->write_fail = 2;
  487. } else {
  488. memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
  489. }
  490. return status;
  491. }
  492. static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
  493. u32 base_address,
  494. u32 instructions_sz,
  495. u16 block_size,
  496. u8 *ta_firmware)
  497. {
  498. u32 num_blocks, offset, i;
  499. u16 msb_address, lsb_address;
  500. u8 temp_buf[block_size];
  501. int status;
  502. num_blocks = instructions_sz / block_size;
  503. msb_address = base_address >> 16;
  504. rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
  505. instructions_sz, num_blocks);
  506. /* Loading DM ms word in the sdio slave */
  507. status = rsi_sdio_master_access_msword(adapter, msb_address);
  508. if (status < 0) {
  509. rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
  510. return status;
  511. }
  512. for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
  513. memcpy(temp_buf, ta_firmware + offset, block_size);
  514. lsb_address = (u16)base_address;
  515. status = rsi_sdio_write_register_multiple
  516. (adapter,
  517. lsb_address | RSI_SD_REQUEST_MASTER,
  518. temp_buf, block_size);
  519. if (status < 0) {
  520. rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
  521. return status;
  522. }
  523. rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
  524. base_address += block_size;
  525. if ((base_address >> 16) != msb_address) {
  526. msb_address += 1;
  527. /* Loading DM ms word in the sdio slave */
  528. status = rsi_sdio_master_access_msword(adapter,
  529. msb_address);
  530. if (status < 0) {
  531. rsi_dbg(ERR_ZONE,
  532. "%s: Unable to set ms word reg\n",
  533. __func__);
  534. return status;
  535. }
  536. }
  537. }
  538. if (instructions_sz % block_size) {
  539. memset(temp_buf, 0, block_size);
  540. memcpy(temp_buf, ta_firmware + offset,
  541. instructions_sz % block_size);
  542. lsb_address = (u16)base_address;
  543. status = rsi_sdio_write_register_multiple
  544. (adapter,
  545. lsb_address | RSI_SD_REQUEST_MASTER,
  546. temp_buf,
  547. instructions_sz % block_size);
  548. if (status < 0)
  549. return status;
  550. rsi_dbg(INFO_ZONE,
  551. "Written Last Block in Address 0x%x Successfully\n",
  552. offset | RSI_SD_REQUEST_MASTER);
  553. }
  554. return 0;
  555. }
  556. #define FLASH_SIZE_ADDR 0x04000016
  557. static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
  558. u32 *read_buf, u16 size)
  559. {
  560. u32 addr_on_bus, *data;
  561. u32 align[2] = {};
  562. u16 ms_addr;
  563. int status;
  564. data = PTR_ALIGN(&align[0], 8);
  565. ms_addr = (addr >> 16);
  566. status = rsi_sdio_master_access_msword(adapter, ms_addr);
  567. if (status < 0) {
  568. rsi_dbg(ERR_ZONE,
  569. "%s: Unable to set ms word to common reg\n",
  570. __func__);
  571. return status;
  572. }
  573. addr &= 0xFFFF;
  574. addr_on_bus = (addr & 0xFF000000);
  575. if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
  576. (addr_on_bus == 0x0))
  577. addr_on_bus = (addr & ~(0x3));
  578. else
  579. addr_on_bus = addr;
  580. /* Bring TA out of reset */
  581. status = rsi_sdio_read_register_multiple
  582. (adapter,
  583. (addr_on_bus | RSI_SD_REQUEST_MASTER),
  584. (u8 *)data, 4);
  585. if (status < 0) {
  586. rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
  587. return status;
  588. }
  589. if (size == 2) {
  590. if ((addr & 0x3) == 0)
  591. *read_buf = *data;
  592. else
  593. *read_buf = (*data >> 16);
  594. *read_buf = (*read_buf & 0xFFFF);
  595. } else if (size == 1) {
  596. if ((addr & 0x3) == 0)
  597. *read_buf = *data;
  598. else if ((addr & 0x3) == 1)
  599. *read_buf = (*data >> 8);
  600. else if ((addr & 0x3) == 2)
  601. *read_buf = (*data >> 16);
  602. else
  603. *read_buf = (*data >> 24);
  604. *read_buf = (*read_buf & 0xFF);
  605. } else {
  606. *read_buf = *data;
  607. }
  608. return 0;
  609. }
  610. static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
  611. unsigned long addr,
  612. unsigned long data, u16 size)
  613. {
  614. unsigned long data1[2], *data_aligned;
  615. int status;
  616. data_aligned = PTR_ALIGN(&data1[0], 8);
  617. if (size == 2) {
  618. *data_aligned = ((data << 16) | (data & 0xFFFF));
  619. } else if (size == 1) {
  620. u32 temp_data = data & 0xFF;
  621. *data_aligned = ((temp_data << 24) | (temp_data << 16) |
  622. (temp_data << 8) | temp_data);
  623. } else {
  624. *data_aligned = data;
  625. }
  626. size = 4;
  627. status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
  628. if (status < 0) {
  629. rsi_dbg(ERR_ZONE,
  630. "%s: Unable to set ms word to common reg\n",
  631. __func__);
  632. return -EIO;
  633. }
  634. addr = addr & 0xFFFF;
  635. /* Bring TA out of reset */
  636. status = rsi_sdio_write_register_multiple
  637. (adapter,
  638. (addr | RSI_SD_REQUEST_MASTER),
  639. (u8 *)data_aligned, size);
  640. if (status < 0) {
  641. rsi_dbg(ERR_ZONE,
  642. "%s: Unable to do AHB reg write\n", __func__);
  643. return status;
  644. }
  645. return 0;
  646. }
  647. /**
  648. * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
  649. * @adapter: Pointer to the adapter structure.
  650. * @pkt: Pointer to the data to be written on to the device.
  651. * @len: length of the data to be written on to the device.
  652. *
  653. * Return: 0 on success, -1 on failure.
  654. */
  655. static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
  656. u8 *pkt,
  657. u32 len)
  658. {
  659. struct rsi_91x_sdiodev *dev =
  660. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  661. u32 block_size = dev->tx_blk_size;
  662. u32 num_blocks, address, length;
  663. u32 queueno;
  664. int status;
  665. queueno = ((pkt[1] >> 4) & 0xf);
  666. num_blocks = len / block_size;
  667. if (len % block_size)
  668. num_blocks++;
  669. address = (num_blocks * block_size | (queueno << 12));
  670. length = num_blocks * block_size;
  671. status = rsi_sdio_write_register_multiple(adapter,
  672. address,
  673. (u8 *)pkt,
  674. length);
  675. if (status)
  676. rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
  677. __func__, status);
  678. rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
  679. return status;
  680. }
  681. /**
  682. * rsi_sdio_host_intf_read_pkt() - This function reads the packet
  683. from the device.
  684. * @adapter: Pointer to the adapter data structure.
  685. * @pkt: Pointer to the packet data to be read from the the device.
  686. * @length: Length of the data to be read from the device.
  687. *
  688. * Return: 0 on success, -1 on failure.
  689. */
  690. int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
  691. u8 *pkt,
  692. u32 length)
  693. {
  694. int status = -EINVAL;
  695. if (!length) {
  696. rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
  697. return status;
  698. }
  699. status = rsi_sdio_read_register_multiple(adapter,
  700. length,
  701. (u8 *)pkt,
  702. length); /*num of bytes*/
  703. if (status)
  704. rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
  705. status);
  706. return status;
  707. }
  708. /**
  709. * rsi_init_sdio_interface() - This function does init specific to SDIO.
  710. *
  711. * @adapter: Pointer to the adapter data structure.
  712. * @pkt: Pointer to the packet data to be read from the the device.
  713. *
  714. * Return: 0 on success, -1 on failure.
  715. */
  716. static int rsi_init_sdio_interface(struct rsi_hw *adapter,
  717. struct sdio_func *pfunction)
  718. {
  719. struct rsi_91x_sdiodev *rsi_91x_dev;
  720. int status = -ENOMEM;
  721. rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
  722. if (!rsi_91x_dev)
  723. return status;
  724. adapter->rsi_dev = rsi_91x_dev;
  725. sdio_claim_host(pfunction);
  726. pfunction->enable_timeout = 100;
  727. status = sdio_enable_func(pfunction);
  728. if (status) {
  729. rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
  730. sdio_release_host(pfunction);
  731. return status;
  732. }
  733. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  734. rsi_91x_dev->pfunction = pfunction;
  735. adapter->device = &pfunction->dev;
  736. sdio_set_drvdata(pfunction, adapter);
  737. status = rsi_setupcard(adapter);
  738. if (status) {
  739. rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
  740. goto fail;
  741. }
  742. rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
  743. status = rsi_init_sdio_slave_regs(adapter);
  744. if (status) {
  745. rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
  746. goto fail;
  747. }
  748. sdio_release_host(pfunction);
  749. adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
  750. adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
  751. #ifdef CONFIG_RSI_DEBUGFS
  752. adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
  753. #endif
  754. return status;
  755. fail:
  756. sdio_disable_func(pfunction);
  757. sdio_release_host(pfunction);
  758. return status;
  759. }
  760. static struct rsi_host_intf_ops sdio_host_intf_ops = {
  761. .write_pkt = rsi_sdio_host_intf_write_pkt,
  762. .read_pkt = rsi_sdio_host_intf_read_pkt,
  763. .master_access_msword = rsi_sdio_master_access_msword,
  764. .read_reg_multiple = rsi_sdio_read_register_multiple,
  765. .write_reg_multiple = rsi_sdio_write_register_multiple,
  766. .master_reg_read = rsi_sdio_master_reg_read,
  767. .master_reg_write = rsi_sdio_master_reg_write,
  768. .load_data_master_write = rsi_sdio_load_data_master_write,
  769. };
  770. /**
  771. * rsi_probe() - This function is called by kernel when the driver provided
  772. * Vendor and device IDs are matched. All the initialization
  773. * work is done here.
  774. * @pfunction: Pointer to the sdio_func structure.
  775. * @id: Pointer to sdio_device_id structure.
  776. *
  777. * Return: 0 on success, 1 on failure.
  778. */
  779. static int rsi_probe(struct sdio_func *pfunction,
  780. const struct sdio_device_id *id)
  781. {
  782. struct rsi_hw *adapter;
  783. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  784. adapter = rsi_91x_init();
  785. if (!adapter) {
  786. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  787. __func__);
  788. return 1;
  789. }
  790. adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
  791. adapter->host_intf_ops = &sdio_host_intf_ops;
  792. if (rsi_init_sdio_interface(adapter, pfunction)) {
  793. rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
  794. __func__);
  795. goto fail;
  796. }
  797. sdio_claim_host(pfunction);
  798. if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
  799. rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
  800. sdio_release_host(pfunction);
  801. goto fail;
  802. }
  803. sdio_release_host(pfunction);
  804. rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
  805. if (rsi_hal_device_init(adapter)) {
  806. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
  807. sdio_claim_host(pfunction);
  808. sdio_release_irq(pfunction);
  809. sdio_disable_func(pfunction);
  810. sdio_release_host(pfunction);
  811. goto fail;
  812. }
  813. rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
  814. if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
  815. rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
  816. return -EIO;
  817. }
  818. return 0;
  819. fail:
  820. rsi_91x_deinit(adapter);
  821. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  822. return 1;
  823. }
  824. static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
  825. u16 len_in_bits)
  826. {
  827. rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
  828. ((addr << 6) | ((data >> 16) & 0xffff)), 2);
  829. rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
  830. (data & 0xffff), 2);
  831. rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
  832. RSI_GSPI_CTRL_REG0_VALUE, 2);
  833. rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
  834. ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
  835. msleep(20);
  836. }
  837. /*This function resets and re-initializes the chip.*/
  838. static void rsi_reset_chip(struct rsi_hw *adapter)
  839. {
  840. __le32 data;
  841. u8 sdio_interrupt_status = 0;
  842. u8 request = 1;
  843. int ret;
  844. rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
  845. ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
  846. if (ret < 0) {
  847. rsi_dbg(ERR_ZONE,
  848. "%s: Failed to write SDIO wakeup register\n", __func__);
  849. return;
  850. }
  851. msleep(20);
  852. ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
  853. &sdio_interrupt_status);
  854. if (ret < 0) {
  855. rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
  856. __func__);
  857. return;
  858. }
  859. rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
  860. __func__, sdio_interrupt_status);
  861. /* Put Thread-Arch processor on hold */
  862. if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
  863. rsi_dbg(ERR_ZONE,
  864. "%s: Unable to set ms word to common reg\n",
  865. __func__);
  866. return;
  867. }
  868. data = TA_HOLD_THREAD_VALUE;
  869. if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
  870. RSI_SD_REQUEST_MASTER,
  871. (u8 *)&data, 4)) {
  872. rsi_dbg(ERR_ZONE,
  873. "%s: Unable to hold Thread-Arch processor threads\n",
  874. __func__);
  875. return;
  876. }
  877. /* This msleep will ensure Thread-Arch processor to go to hold
  878. * and any pending dma transfers to rf spi in device to finish.
  879. */
  880. msleep(100);
  881. ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
  882. ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
  883. ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0, 32);
  884. ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1, RSI_ULP_WRITE_50,
  885. 32);
  886. ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2, RSI_ULP_WRITE_0,
  887. 32);
  888. ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
  889. RSI_ULP_TIMER_ENABLE, 32);
  890. /* This msleep will be sufficient for the ulp
  891. * read write operations to complete for chip reset.
  892. */
  893. msleep(500);
  894. }
  895. /**
  896. * rsi_disconnect() - This function performs the reverse of the probe function.
  897. * @pfunction: Pointer to the sdio_func structure.
  898. *
  899. * Return: void.
  900. */
  901. static void rsi_disconnect(struct sdio_func *pfunction)
  902. {
  903. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  904. struct rsi_91x_sdiodev *dev;
  905. if (!adapter)
  906. return;
  907. dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  908. sdio_claim_host(pfunction);
  909. sdio_release_irq(pfunction);
  910. sdio_release_host(pfunction);
  911. mdelay(10);
  912. rsi_mac80211_detach(adapter);
  913. mdelay(10);
  914. /* Reset Chip */
  915. rsi_reset_chip(adapter);
  916. /* Resetting to take care of the case, where-in driver is re-loaded */
  917. sdio_claim_host(pfunction);
  918. rsi_reset_card(pfunction);
  919. sdio_disable_func(pfunction);
  920. sdio_release_host(pfunction);
  921. dev->write_fail = 2;
  922. rsi_91x_deinit(adapter);
  923. rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
  924. }
  925. #ifdef CONFIG_PM
  926. static int rsi_suspend(struct device *dev)
  927. {
  928. /* Not yet implemented */
  929. return -ENOSYS;
  930. }
  931. static int rsi_resume(struct device *dev)
  932. {
  933. /* Not yet implemented */
  934. return -ENOSYS;
  935. }
  936. static const struct dev_pm_ops rsi_pm_ops = {
  937. .suspend = rsi_suspend,
  938. .resume = rsi_resume,
  939. };
  940. #endif
  941. static const struct sdio_device_id rsi_dev_table[] = {
  942. { SDIO_DEVICE(0x303, 0x100) },
  943. { SDIO_DEVICE(0x041B, 0x0301) },
  944. { SDIO_DEVICE(0x041B, 0x0201) },
  945. { SDIO_DEVICE(0x041B, 0x9330) },
  946. { /* Blank */},
  947. };
  948. static struct sdio_driver rsi_driver = {
  949. .name = "RSI-SDIO WLAN",
  950. .probe = rsi_probe,
  951. .remove = rsi_disconnect,
  952. .id_table = rsi_dev_table,
  953. #ifdef CONFIG_PM
  954. .drv = {
  955. .pm = &rsi_pm_ops,
  956. }
  957. #endif
  958. };
  959. /**
  960. * rsi_module_init() - This function registers the sdio module.
  961. * @void: Void.
  962. *
  963. * Return: 0 on success.
  964. */
  965. static int rsi_module_init(void)
  966. {
  967. int ret;
  968. ret = sdio_register_driver(&rsi_driver);
  969. rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
  970. return ret;
  971. }
  972. /**
  973. * rsi_module_exit() - This function unregisters the sdio module.
  974. * @void: Void.
  975. *
  976. * Return: None.
  977. */
  978. static void rsi_module_exit(void)
  979. {
  980. sdio_unregister_driver(&rsi_driver);
  981. rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
  982. }
  983. module_init(rsi_module_init);
  984. module_exit(rsi_module_exit);
  985. MODULE_AUTHOR("Redpine Signals Inc");
  986. MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
  987. MODULE_SUPPORTED_DEVICE("RSI-91x");
  988. MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
  989. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  990. MODULE_VERSION("0.1");
  991. MODULE_LICENSE("Dual BSD/GPL");