rsi_91x_sdio.c 21 KB

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