rsi_91x_sdio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. /**
  21. * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
  22. * @rw: Read/write
  23. * @func: function number
  24. * @raw: indicates whether to perform read after write
  25. * @address: address to which to read/write
  26. * @writedata: data to write
  27. *
  28. * Return: argument
  29. */
  30. static u32 rsi_sdio_set_cmd52_arg(bool rw,
  31. u8 func,
  32. u8 raw,
  33. u32 address,
  34. u8 writedata)
  35. {
  36. return ((rw & 1) << 31) | ((func & 0x7) << 28) |
  37. ((raw & 1) << 27) | (1 << 26) |
  38. ((address & 0x1FFFF) << 9) | (1 << 8) |
  39. (writedata & 0xFF);
  40. }
  41. /**
  42. * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
  43. * @card: Pointer to the mmc_card.
  44. * @address: Address to write.
  45. * @byte: Data to write.
  46. *
  47. * Return: Write status.
  48. */
  49. static int rsi_cmd52writebyte(struct mmc_card *card,
  50. u32 address,
  51. u8 byte)
  52. {
  53. struct mmc_command io_cmd;
  54. u32 arg;
  55. memset(&io_cmd, 0, sizeof(io_cmd));
  56. arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
  57. io_cmd.opcode = SD_IO_RW_DIRECT;
  58. io_cmd.arg = arg;
  59. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  60. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  61. }
  62. /**
  63. * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
  64. * @card: Pointer to the mmc_card.
  65. * @address: Address to read from.
  66. * @byte: Variable to store read value.
  67. *
  68. * Return: Read status.
  69. */
  70. static int rsi_cmd52readbyte(struct mmc_card *card,
  71. u32 address,
  72. u8 *byte)
  73. {
  74. struct mmc_command io_cmd;
  75. u32 arg;
  76. int err;
  77. memset(&io_cmd, 0, sizeof(io_cmd));
  78. arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
  79. io_cmd.opcode = SD_IO_RW_DIRECT;
  80. io_cmd.arg = arg;
  81. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  82. err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  83. if ((!err) && (byte))
  84. *byte = io_cmd.resp[0] & 0xFF;
  85. return err;
  86. }
  87. /**
  88. * rsi_issue_sdiocommand() - This function issues sdio commands.
  89. * @func: Pointer to the sdio_func structure.
  90. * @opcode: Opcode value.
  91. * @arg: Arguments to pass.
  92. * @flags: Flags which are set.
  93. * @resp: Pointer to store response.
  94. *
  95. * Return: err: command status as 0 or -1.
  96. */
  97. static int rsi_issue_sdiocommand(struct sdio_func *func,
  98. u32 opcode,
  99. u32 arg,
  100. u32 flags,
  101. u32 *resp)
  102. {
  103. struct mmc_command cmd;
  104. struct mmc_host *host;
  105. int err;
  106. host = func->card->host;
  107. memset(&cmd, 0, sizeof(struct mmc_command));
  108. cmd.opcode = opcode;
  109. cmd.arg = arg;
  110. cmd.flags = flags;
  111. err = mmc_wait_for_cmd(host, &cmd, 3);
  112. if ((!err) && (resp))
  113. *resp = cmd.resp[0];
  114. return err;
  115. }
  116. /**
  117. * rsi_handle_interrupt() - This function is called upon the occurence
  118. * of an interrupt.
  119. * @function: Pointer to the sdio_func structure.
  120. *
  121. * Return: None.
  122. */
  123. static void rsi_handle_interrupt(struct sdio_func *function)
  124. {
  125. struct rsi_hw *adapter = sdio_get_drvdata(function);
  126. sdio_release_host(function);
  127. rsi_interrupt_handler(adapter);
  128. sdio_claim_host(function);
  129. }
  130. /**
  131. * rsi_reset_card() - This function resets and re-initializes the card.
  132. * @pfunction: Pointer to the sdio_func structure.
  133. *
  134. * Return: None.
  135. */
  136. static void rsi_reset_card(struct sdio_func *pfunction)
  137. {
  138. int ret = 0;
  139. int err;
  140. struct mmc_card *card = pfunction->card;
  141. struct mmc_host *host = card->host;
  142. u8 cmd52_resp;
  143. u32 clock, resp, i;
  144. u16 rca;
  145. /* Reset 9110 chip */
  146. ret = rsi_cmd52writebyte(pfunction->card,
  147. SDIO_CCCR_ABORT,
  148. (1 << 3));
  149. /* Card will not send any response as it is getting reset immediately
  150. * Hence expect a timeout status from host controller
  151. */
  152. if (ret != -ETIMEDOUT)
  153. rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
  154. /* Wait for few milli seconds to get rid of residue charges if any */
  155. msleep(20);
  156. /* Initialize the SDIO card */
  157. host->ios.chip_select = MMC_CS_DONTCARE;
  158. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  159. host->ios.power_mode = MMC_POWER_UP;
  160. host->ios.bus_width = MMC_BUS_WIDTH_1;
  161. host->ios.timing = MMC_TIMING_LEGACY;
  162. host->ops->set_ios(host, &host->ios);
  163. /*
  164. * This delay should be sufficient to allow the power supply
  165. * to reach the minimum voltage.
  166. */
  167. msleep(20);
  168. host->ios.clock = host->f_min;
  169. host->ios.power_mode = MMC_POWER_ON;
  170. host->ops->set_ios(host, &host->ios);
  171. /*
  172. * This delay must be at least 74 clock sizes, or 1 ms, or the
  173. * time required to reach a stable voltage.
  174. */
  175. msleep(20);
  176. /* Issue CMD0. Goto idle state */
  177. host->ios.chip_select = MMC_CS_HIGH;
  178. host->ops->set_ios(host, &host->ios);
  179. msleep(20);
  180. err = rsi_issue_sdiocommand(pfunction,
  181. MMC_GO_IDLE_STATE,
  182. 0,
  183. (MMC_RSP_NONE | MMC_CMD_BC),
  184. NULL);
  185. host->ios.chip_select = MMC_CS_DONTCARE;
  186. host->ops->set_ios(host, &host->ios);
  187. msleep(20);
  188. host->use_spi_crc = 0;
  189. if (err)
  190. rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
  191. if (!host->ocr_avail) {
  192. /* Issue CMD5, arg = 0 */
  193. err = rsi_issue_sdiocommand(pfunction,
  194. SD_IO_SEND_OP_COND,
  195. 0,
  196. (MMC_RSP_R4 | MMC_CMD_BCR),
  197. &resp);
  198. if (err)
  199. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
  200. __func__, err);
  201. host->ocr_avail = resp;
  202. }
  203. /* Issue CMD5, arg = ocr. Wait till card is ready */
  204. for (i = 0; i < 100; i++) {
  205. err = rsi_issue_sdiocommand(pfunction,
  206. SD_IO_SEND_OP_COND,
  207. host->ocr_avail,
  208. (MMC_RSP_R4 | MMC_CMD_BCR),
  209. &resp);
  210. if (err) {
  211. rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
  212. __func__, err);
  213. break;
  214. }
  215. if (resp & MMC_CARD_BUSY)
  216. break;
  217. msleep(20);
  218. }
  219. if ((i == 100) || (err)) {
  220. rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
  221. __func__, i, err);
  222. return;
  223. }
  224. /* Issue CMD3, get RCA */
  225. err = rsi_issue_sdiocommand(pfunction,
  226. SD_SEND_RELATIVE_ADDR,
  227. 0,
  228. (MMC_RSP_R6 | MMC_CMD_BCR),
  229. &resp);
  230. if (err) {
  231. rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
  232. return;
  233. }
  234. rca = resp >> 16;
  235. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  236. host->ops->set_ios(host, &host->ios);
  237. /* Issue CMD7, select card */
  238. err = rsi_issue_sdiocommand(pfunction,
  239. MMC_SELECT_CARD,
  240. (rca << 16),
  241. (MMC_RSP_R1 | MMC_CMD_AC),
  242. NULL);
  243. if (err) {
  244. rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
  245. return;
  246. }
  247. /* Enable high speed */
  248. if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
  249. rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
  250. err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
  251. if (err) {
  252. rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
  253. __func__, err);
  254. } else {
  255. err = rsi_cmd52writebyte(card,
  256. SDIO_CCCR_SPEED,
  257. (cmd52_resp | SDIO_SPEED_EHS));
  258. if (err) {
  259. rsi_dbg(ERR_ZONE,
  260. "%s: CCR speed regwrite failed %d\n",
  261. __func__, err);
  262. return;
  263. }
  264. host->ios.timing = MMC_TIMING_SD_HS;
  265. host->ops->set_ios(host, &host->ios);
  266. }
  267. }
  268. /* Set clock */
  269. if (mmc_card_hs(card))
  270. clock = 50000000;
  271. else
  272. clock = card->cis.max_dtr;
  273. if (clock > host->f_max)
  274. clock = host->f_max;
  275. host->ios.clock = clock;
  276. host->ops->set_ios(host, &host->ios);
  277. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  278. /* CMD52: Set bus width & disable card detect resistor */
  279. err = rsi_cmd52writebyte(card,
  280. SDIO_CCCR_IF,
  281. (SDIO_BUS_CD_DISABLE |
  282. SDIO_BUS_WIDTH_4BIT));
  283. if (err) {
  284. rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
  285. __func__, err);
  286. return;
  287. }
  288. host->ios.bus_width = MMC_BUS_WIDTH_4;
  289. host->ops->set_ios(host, &host->ios);
  290. }
  291. }
  292. /**
  293. * rsi_setclock() - This function sets the clock frequency.
  294. * @adapter: Pointer to the adapter structure.
  295. * @freq: Clock frequency.
  296. *
  297. * Return: None.
  298. */
  299. static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
  300. {
  301. struct rsi_91x_sdiodev *dev =
  302. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  303. struct mmc_host *host = dev->pfunction->card->host;
  304. u32 clock;
  305. clock = freq * 1000;
  306. if (clock > host->f_max)
  307. clock = host->f_max;
  308. host->ios.clock = clock;
  309. host->ops->set_ios(host, &host->ios);
  310. }
  311. /**
  312. * rsi_setblocklength() - This function sets the host block length.
  313. * @adapter: Pointer to the adapter structure.
  314. * @length: Block length to be set.
  315. *
  316. * Return: status: 0 on success, -1 on failure.
  317. */
  318. static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
  319. {
  320. struct rsi_91x_sdiodev *dev =
  321. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  322. int status;
  323. rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
  324. status = sdio_set_block_size(dev->pfunction, length);
  325. dev->pfunction->max_blksize = 256;
  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. sdio_claim_host(dev->pfunction);
  367. if (fun_num == 0)
  368. *data = sdio_f0_readb(dev->pfunction, addr, &status);
  369. else
  370. *data = sdio_readb(dev->pfunction, addr, &status);
  371. sdio_release_host(dev->pfunction);
  372. return status;
  373. }
  374. /**
  375. * rsi_sdio_write_register() - This function writes one byte of information
  376. * into a register.
  377. * @adapter: Pointer to the adapter structure.
  378. * @function: Function Number.
  379. * @addr: Address of the register.
  380. * @data: Pointer to the data tha has to be written.
  381. *
  382. * Return: 0 on success, -1 on failure.
  383. */
  384. int rsi_sdio_write_register(struct rsi_hw *adapter,
  385. u8 function,
  386. u32 addr,
  387. u8 *data)
  388. {
  389. struct rsi_91x_sdiodev *dev =
  390. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  391. int status = 0;
  392. sdio_claim_host(dev->pfunction);
  393. if (function == 0)
  394. sdio_f0_writeb(dev->pfunction, *data, addr, &status);
  395. else
  396. sdio_writeb(dev->pfunction, *data, addr, &status);
  397. sdio_release_host(dev->pfunction);
  398. return status;
  399. }
  400. /**
  401. * rsi_sdio_ack_intr() - This function acks the interrupt received.
  402. * @adapter: Pointer to the adapter structure.
  403. * @int_bit: Interrupt bit to write into register.
  404. *
  405. * Return: None.
  406. */
  407. void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
  408. {
  409. int status;
  410. status = rsi_sdio_write_register(adapter,
  411. 1,
  412. (SDIO_FUN1_INTR_CLR_REG |
  413. RSI_SD_REQUEST_MASTER),
  414. &int_bit);
  415. if (status)
  416. rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
  417. }
  418. /**
  419. * rsi_sdio_read_register_multiple() - This function read multiple bytes of
  420. * information from the SD card.
  421. * @adapter: Pointer to the adapter structure.
  422. * @addr: Address of the register.
  423. * @count: Number of multiple bytes to be read.
  424. * @data: Pointer to the read data.
  425. *
  426. * Return: 0 on success, -1 on failure.
  427. */
  428. static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
  429. u32 addr,
  430. u32 count,
  431. u8 *data)
  432. {
  433. struct rsi_91x_sdiodev *dev =
  434. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  435. u32 status;
  436. sdio_claim_host(dev->pfunction);
  437. status = sdio_readsb(dev->pfunction, data, addr, count);
  438. sdio_release_host(dev->pfunction);
  439. if (status != 0)
  440. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
  441. return status;
  442. }
  443. /**
  444. * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
  445. * information to the SD card.
  446. * @adapter: Pointer to the adapter structure.
  447. * @addr: Address of the register.
  448. * @data: Pointer to the data that has to be written.
  449. * @count: Number of multiple bytes to be written.
  450. *
  451. * Return: 0 on success, -1 on failure.
  452. */
  453. int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
  454. u32 addr,
  455. u8 *data,
  456. u32 count)
  457. {
  458. struct rsi_91x_sdiodev *dev =
  459. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  460. int status;
  461. if (dev->write_fail > 1) {
  462. rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
  463. return 0;
  464. } else if (dev->write_fail == 1) {
  465. /**
  466. * Assuming it is a CRC failure, we want to allow another
  467. * card write
  468. */
  469. rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
  470. dev->write_fail++;
  471. }
  472. sdio_claim_host(dev->pfunction);
  473. status = sdio_writesb(dev->pfunction, addr, data, count);
  474. sdio_release_host(dev->pfunction);
  475. if (status) {
  476. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
  477. __func__, status);
  478. dev->write_fail = 2;
  479. } else {
  480. memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
  481. }
  482. return status;
  483. }
  484. /**
  485. * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
  486. * @adapter: Pointer to the adapter structure.
  487. * @pkt: Pointer to the data to be written on to the device.
  488. * @len: length of the data to be written on to the device.
  489. *
  490. * Return: 0 on success, -1 on failure.
  491. */
  492. static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
  493. u8 *pkt,
  494. u32 len)
  495. {
  496. struct rsi_91x_sdiodev *dev =
  497. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  498. u32 block_size = dev->tx_blk_size;
  499. u32 num_blocks, address, length;
  500. u32 queueno;
  501. int status;
  502. queueno = ((pkt[1] >> 4) & 0xf);
  503. num_blocks = len / block_size;
  504. if (len % block_size)
  505. num_blocks++;
  506. address = (num_blocks * block_size | (queueno << 12));
  507. length = num_blocks * block_size;
  508. status = rsi_sdio_write_register_multiple(adapter,
  509. address,
  510. (u8 *)pkt,
  511. length);
  512. if (status)
  513. rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
  514. __func__, status);
  515. rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
  516. return status;
  517. }
  518. /**
  519. * rsi_sdio_host_intf_read_pkt() - This function reads the packet
  520. from the device.
  521. * @adapter: Pointer to the adapter data structure.
  522. * @pkt: Pointer to the packet data to be read from the the device.
  523. * @length: Length of the data to be read from the device.
  524. *
  525. * Return: 0 on success, -1 on failure.
  526. */
  527. int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
  528. u8 *pkt,
  529. u32 length)
  530. {
  531. int status = -EINVAL;
  532. if (!length) {
  533. rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
  534. return status;
  535. }
  536. status = rsi_sdio_read_register_multiple(adapter,
  537. length,
  538. length, /*num of bytes*/
  539. (u8 *)pkt);
  540. if (status)
  541. rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
  542. status);
  543. return status;
  544. }
  545. /**
  546. * rsi_init_sdio_interface() - This function does init specific to SDIO.
  547. *
  548. * @adapter: Pointer to the adapter data structure.
  549. * @pkt: Pointer to the packet data to be read from the the device.
  550. *
  551. * Return: 0 on success, -1 on failure.
  552. */
  553. static int rsi_init_sdio_interface(struct rsi_hw *adapter,
  554. struct sdio_func *pfunction)
  555. {
  556. struct rsi_91x_sdiodev *rsi_91x_dev;
  557. int status = -ENOMEM;
  558. rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
  559. if (!rsi_91x_dev)
  560. return status;
  561. adapter->rsi_dev = rsi_91x_dev;
  562. sdio_claim_host(pfunction);
  563. pfunction->enable_timeout = 100;
  564. status = sdio_enable_func(pfunction);
  565. if (status) {
  566. rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
  567. sdio_release_host(pfunction);
  568. return status;
  569. }
  570. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  571. rsi_91x_dev->pfunction = pfunction;
  572. adapter->device = &pfunction->dev;
  573. sdio_set_drvdata(pfunction, adapter);
  574. status = rsi_setupcard(adapter);
  575. if (status) {
  576. rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
  577. goto fail;
  578. }
  579. rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
  580. status = rsi_init_sdio_slave_regs(adapter);
  581. if (status) {
  582. rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
  583. goto fail;
  584. }
  585. sdio_release_host(pfunction);
  586. adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
  587. adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
  588. adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
  589. adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
  590. #ifdef CONFIG_RSI_DEBUGFS
  591. adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
  592. #endif
  593. return status;
  594. fail:
  595. sdio_disable_func(pfunction);
  596. sdio_release_host(pfunction);
  597. return status;
  598. }
  599. /**
  600. * rsi_probe() - This function is called by kernel when the driver provided
  601. * Vendor and device IDs are matched. All the initialization
  602. * work is done here.
  603. * @pfunction: Pointer to the sdio_func structure.
  604. * @id: Pointer to sdio_device_id structure.
  605. *
  606. * Return: 0 on success, 1 on failure.
  607. */
  608. static int rsi_probe(struct sdio_func *pfunction,
  609. const struct sdio_device_id *id)
  610. {
  611. struct rsi_hw *adapter;
  612. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  613. adapter = rsi_91x_init();
  614. if (!adapter) {
  615. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  616. __func__);
  617. return 1;
  618. }
  619. if (rsi_init_sdio_interface(adapter, pfunction)) {
  620. rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
  621. __func__);
  622. goto fail;
  623. }
  624. if (rsi_sdio_device_init(adapter->priv)) {
  625. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
  626. sdio_claim_host(pfunction);
  627. sdio_disable_func(pfunction);
  628. sdio_release_host(pfunction);
  629. goto fail;
  630. }
  631. sdio_claim_host(pfunction);
  632. if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
  633. rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
  634. sdio_release_host(pfunction);
  635. goto fail;
  636. }
  637. sdio_release_host(pfunction);
  638. rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
  639. return 0;
  640. fail:
  641. rsi_91x_deinit(adapter);
  642. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  643. return 1;
  644. }
  645. /**
  646. * rsi_disconnect() - This function performs the reverse of the probe function.
  647. * @pfunction: Pointer to the sdio_func structure.
  648. *
  649. * Return: void.
  650. */
  651. static void rsi_disconnect(struct sdio_func *pfunction)
  652. {
  653. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  654. struct rsi_91x_sdiodev *dev;
  655. if (!adapter)
  656. return;
  657. dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  658. dev->write_fail = 2;
  659. rsi_mac80211_detach(adapter);
  660. sdio_claim_host(pfunction);
  661. sdio_release_irq(pfunction);
  662. sdio_disable_func(pfunction);
  663. rsi_91x_deinit(adapter);
  664. /* Resetting to take care of the case, where-in driver is re-loaded */
  665. rsi_reset_card(pfunction);
  666. sdio_release_host(pfunction);
  667. }
  668. #ifdef CONFIG_PM
  669. static int rsi_suspend(struct device *dev)
  670. {
  671. /* Not yet implemented */
  672. return -ENOSYS;
  673. }
  674. static int rsi_resume(struct device *dev)
  675. {
  676. /* Not yet implemented */
  677. return -ENOSYS;
  678. }
  679. static const struct dev_pm_ops rsi_pm_ops = {
  680. .suspend = rsi_suspend,
  681. .resume = rsi_resume,
  682. };
  683. #endif
  684. static const struct sdio_device_id rsi_dev_table[] = {
  685. { SDIO_DEVICE(0x303, 0x100) },
  686. { SDIO_DEVICE(0x041B, 0x0301) },
  687. { SDIO_DEVICE(0x041B, 0x0201) },
  688. { SDIO_DEVICE(0x041B, 0x9330) },
  689. { /* Blank */},
  690. };
  691. static struct sdio_driver rsi_driver = {
  692. .name = "RSI-SDIO WLAN",
  693. .probe = rsi_probe,
  694. .remove = rsi_disconnect,
  695. .id_table = rsi_dev_table,
  696. #ifdef CONFIG_PM
  697. .drv = {
  698. .pm = &rsi_pm_ops,
  699. }
  700. #endif
  701. };
  702. /**
  703. * rsi_module_init() - This function registers the sdio module.
  704. * @void: Void.
  705. *
  706. * Return: 0 on success.
  707. */
  708. static int rsi_module_init(void)
  709. {
  710. int ret;
  711. ret = sdio_register_driver(&rsi_driver);
  712. rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
  713. return ret;
  714. }
  715. /**
  716. * rsi_module_exit() - This function unregisters the sdio module.
  717. * @void: Void.
  718. *
  719. * Return: None.
  720. */
  721. static void rsi_module_exit(void)
  722. {
  723. sdio_unregister_driver(&rsi_driver);
  724. rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
  725. }
  726. module_init(rsi_module_init);
  727. module_exit(rsi_module_exit);
  728. MODULE_AUTHOR("Redpine Signals Inc");
  729. MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
  730. MODULE_SUPPORTED_DEVICE("RSI-91x");
  731. MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
  732. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  733. MODULE_VERSION("0.1");
  734. MODULE_LICENSE("Dual BSD/GPL");