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. card->state &= ~MMC_STATE_HIGHSPEED;
  257. } else {
  258. err = rsi_cmd52writebyte(card,
  259. SDIO_CCCR_SPEED,
  260. (cmd52_resp | SDIO_SPEED_EHS));
  261. if (err) {
  262. rsi_dbg(ERR_ZONE,
  263. "%s: CCR speed regwrite failed %d\n",
  264. __func__, err);
  265. return;
  266. }
  267. mmc_card_set_highspeed(card);
  268. host->ios.timing = MMC_TIMING_SD_HS;
  269. host->ops->set_ios(host, &host->ios);
  270. }
  271. }
  272. /* Set clock */
  273. if (mmc_card_highspeed(card))
  274. clock = 50000000;
  275. else
  276. clock = card->cis.max_dtr;
  277. if (clock > host->f_max)
  278. clock = host->f_max;
  279. host->ios.clock = clock;
  280. host->ops->set_ios(host, &host->ios);
  281. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  282. /* CMD52: Set bus width & disable card detect resistor */
  283. err = rsi_cmd52writebyte(card,
  284. SDIO_CCCR_IF,
  285. (SDIO_BUS_CD_DISABLE |
  286. SDIO_BUS_WIDTH_4BIT));
  287. if (err) {
  288. rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
  289. __func__, err);
  290. return;
  291. }
  292. host->ios.bus_width = MMC_BUS_WIDTH_4;
  293. host->ops->set_ios(host, &host->ios);
  294. }
  295. }
  296. /**
  297. * rsi_setclock() - This function sets the clock frequency.
  298. * @adapter: Pointer to the adapter structure.
  299. * @freq: Clock frequency.
  300. *
  301. * Return: None.
  302. */
  303. static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
  304. {
  305. struct rsi_91x_sdiodev *dev =
  306. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  307. struct mmc_host *host = dev->pfunction->card->host;
  308. u32 clock;
  309. clock = freq * 1000;
  310. if (clock > host->f_max)
  311. clock = host->f_max;
  312. host->ios.clock = clock;
  313. host->ops->set_ios(host, &host->ios);
  314. }
  315. /**
  316. * rsi_setblocklength() - This function sets the host block length.
  317. * @adapter: Pointer to the adapter structure.
  318. * @length: Block length to be set.
  319. *
  320. * Return: status: 0 on success, -1 on failure.
  321. */
  322. static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
  323. {
  324. struct rsi_91x_sdiodev *dev =
  325. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  326. int status;
  327. rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
  328. status = sdio_set_block_size(dev->pfunction, length);
  329. dev->pfunction->max_blksize = 256;
  330. rsi_dbg(INFO_ZONE,
  331. "%s: Operational blk length is %d\n", __func__, length);
  332. return status;
  333. }
  334. /**
  335. * rsi_setupcard() - This function queries and sets the card's features.
  336. * @adapter: Pointer to the adapter structure.
  337. *
  338. * Return: status: 0 on success, -1 on failure.
  339. */
  340. static int rsi_setupcard(struct rsi_hw *adapter)
  341. {
  342. struct rsi_91x_sdiodev *dev =
  343. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  344. int status = 0;
  345. rsi_setclock(adapter, 50000);
  346. dev->tx_blk_size = 256;
  347. status = rsi_setblocklength(adapter, dev->tx_blk_size);
  348. if (status)
  349. rsi_dbg(ERR_ZONE,
  350. "%s: Unable to set block length\n", __func__);
  351. return status;
  352. }
  353. /**
  354. * rsi_sdio_read_register() - This function reads one byte of information
  355. * from a register.
  356. * @adapter: Pointer to the adapter structure.
  357. * @addr: Address of the register.
  358. * @data: Pointer to the data that stores the data read.
  359. *
  360. * Return: 0 on success, -1 on failure.
  361. */
  362. int rsi_sdio_read_register(struct rsi_hw *adapter,
  363. u32 addr,
  364. u8 *data)
  365. {
  366. struct rsi_91x_sdiodev *dev =
  367. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  368. u8 fun_num = 0;
  369. int status;
  370. sdio_claim_host(dev->pfunction);
  371. if (fun_num == 0)
  372. *data = sdio_f0_readb(dev->pfunction, addr, &status);
  373. else
  374. *data = sdio_readb(dev->pfunction, addr, &status);
  375. sdio_release_host(dev->pfunction);
  376. return status;
  377. }
  378. /**
  379. * rsi_sdio_write_register() - This function writes one byte of information
  380. * into a register.
  381. * @adapter: Pointer to the adapter structure.
  382. * @function: Function Number.
  383. * @addr: Address of the register.
  384. * @data: Pointer to the data tha has to be written.
  385. *
  386. * Return: 0 on success, -1 on failure.
  387. */
  388. int rsi_sdio_write_register(struct rsi_hw *adapter,
  389. u8 function,
  390. u32 addr,
  391. u8 *data)
  392. {
  393. struct rsi_91x_sdiodev *dev =
  394. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  395. int status = 0;
  396. sdio_claim_host(dev->pfunction);
  397. if (function == 0)
  398. sdio_f0_writeb(dev->pfunction, *data, addr, &status);
  399. else
  400. sdio_writeb(dev->pfunction, *data, addr, &status);
  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. u32 count,
  435. u8 *data)
  436. {
  437. struct rsi_91x_sdiodev *dev =
  438. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  439. u32 status;
  440. sdio_claim_host(dev->pfunction);
  441. status = sdio_readsb(dev->pfunction, data, addr, count);
  442. sdio_release_host(dev->pfunction);
  443. if (status != 0)
  444. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
  445. return status;
  446. }
  447. /**
  448. * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
  449. * information to the SD card.
  450. * @adapter: Pointer to the adapter structure.
  451. * @addr: Address of the register.
  452. * @data: Pointer to the data that has to be written.
  453. * @count: Number of multiple bytes to be written.
  454. *
  455. * Return: 0 on success, -1 on failure.
  456. */
  457. int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
  458. u32 addr,
  459. u8 *data,
  460. u32 count)
  461. {
  462. struct rsi_91x_sdiodev *dev =
  463. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  464. int status;
  465. if (dev->write_fail > 1) {
  466. rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
  467. return 0;
  468. } else if (dev->write_fail == 1) {
  469. /**
  470. * Assuming it is a CRC failure, we want to allow another
  471. * card write
  472. */
  473. rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
  474. dev->write_fail++;
  475. }
  476. sdio_claim_host(dev->pfunction);
  477. status = sdio_writesb(dev->pfunction, addr, data, count);
  478. sdio_release_host(dev->pfunction);
  479. if (status) {
  480. rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
  481. __func__, status);
  482. dev->write_fail = 2;
  483. } else {
  484. memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
  485. }
  486. return status;
  487. }
  488. /**
  489. * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
  490. * @adapter: Pointer to the adapter structure.
  491. * @pkt: Pointer to the data to be written on to the device.
  492. * @len: length of the data to be written on to the device.
  493. *
  494. * Return: 0 on success, -1 on failure.
  495. */
  496. static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
  497. u8 *pkt,
  498. u32 len)
  499. {
  500. struct rsi_91x_sdiodev *dev =
  501. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  502. u32 block_size = dev->tx_blk_size;
  503. u32 num_blocks, address, length;
  504. u32 queueno;
  505. int status;
  506. queueno = ((pkt[1] >> 4) & 0xf);
  507. num_blocks = len / block_size;
  508. if (len % block_size)
  509. num_blocks++;
  510. address = (num_blocks * block_size | (queueno << 12));
  511. length = num_blocks * block_size;
  512. status = rsi_sdio_write_register_multiple(adapter,
  513. address,
  514. (u8 *)pkt,
  515. length);
  516. if (status)
  517. rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
  518. __func__, status);
  519. rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
  520. return status;
  521. }
  522. /**
  523. * rsi_sdio_host_intf_read_pkt() - This function reads the packet
  524. from the device.
  525. * @adapter: Pointer to the adapter data structure.
  526. * @pkt: Pointer to the packet data to be read from the the device.
  527. * @length: Length of the data to be read from the device.
  528. *
  529. * Return: 0 on success, -1 on failure.
  530. */
  531. int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
  532. u8 *pkt,
  533. u32 length)
  534. {
  535. int status = -EINVAL;
  536. if (!length) {
  537. rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
  538. return status;
  539. }
  540. status = rsi_sdio_read_register_multiple(adapter,
  541. length,
  542. length, /*num of bytes*/
  543. (u8 *)pkt);
  544. if (status)
  545. rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
  546. status);
  547. return status;
  548. }
  549. /**
  550. * rsi_init_sdio_interface() - This function does init specific to SDIO.
  551. *
  552. * @adapter: Pointer to the adapter data structure.
  553. * @pkt: Pointer to the packet data to be read from the the device.
  554. *
  555. * Return: 0 on success, -1 on failure.
  556. */
  557. static int rsi_init_sdio_interface(struct rsi_hw *adapter,
  558. struct sdio_func *pfunction)
  559. {
  560. struct rsi_91x_sdiodev *rsi_91x_dev;
  561. int status = -ENOMEM;
  562. rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
  563. if (!rsi_91x_dev)
  564. return status;
  565. adapter->rsi_dev = rsi_91x_dev;
  566. sdio_claim_host(pfunction);
  567. pfunction->enable_timeout = 100;
  568. status = sdio_enable_func(pfunction);
  569. if (status) {
  570. rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
  571. sdio_release_host(pfunction);
  572. return status;
  573. }
  574. rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
  575. rsi_91x_dev->pfunction = pfunction;
  576. adapter->device = &pfunction->dev;
  577. sdio_set_drvdata(pfunction, adapter);
  578. status = rsi_setupcard(adapter);
  579. if (status) {
  580. rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
  581. goto fail;
  582. }
  583. rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
  584. status = rsi_init_sdio_slave_regs(adapter);
  585. if (status) {
  586. rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
  587. goto fail;
  588. }
  589. sdio_release_host(pfunction);
  590. adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
  591. adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
  592. adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
  593. adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
  594. #ifdef CONFIG_RSI_DEBUGFS
  595. adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
  596. #endif
  597. return status;
  598. fail:
  599. sdio_disable_func(pfunction);
  600. sdio_release_host(pfunction);
  601. return status;
  602. }
  603. /**
  604. * rsi_probe() - This function is called by kernel when the driver provided
  605. * Vendor and device IDs are matched. All the initialization
  606. * work is done here.
  607. * @pfunction: Pointer to the sdio_func structure.
  608. * @id: Pointer to sdio_device_id structure.
  609. *
  610. * Return: 0 on success, 1 on failure.
  611. */
  612. static int rsi_probe(struct sdio_func *pfunction,
  613. const struct sdio_device_id *id)
  614. {
  615. struct rsi_hw *adapter;
  616. rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
  617. adapter = rsi_91x_init();
  618. if (!adapter) {
  619. rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
  620. __func__);
  621. return 1;
  622. }
  623. if (rsi_init_sdio_interface(adapter, pfunction)) {
  624. rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
  625. __func__);
  626. goto fail;
  627. }
  628. if (rsi_sdio_device_init(adapter->priv)) {
  629. rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
  630. sdio_claim_host(pfunction);
  631. sdio_disable_func(pfunction);
  632. sdio_release_host(pfunction);
  633. goto fail;
  634. }
  635. sdio_claim_host(pfunction);
  636. if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
  637. rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
  638. sdio_release_host(pfunction);
  639. goto fail;
  640. }
  641. sdio_release_host(pfunction);
  642. rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
  643. return 0;
  644. fail:
  645. rsi_91x_deinit(adapter);
  646. rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
  647. return 1;
  648. }
  649. /**
  650. * rsi_disconnect() - This function performs the reverse of the probe function.
  651. * @pfunction: Pointer to the sdio_func structure.
  652. *
  653. * Return: void.
  654. */
  655. static void rsi_disconnect(struct sdio_func *pfunction)
  656. {
  657. struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
  658. struct rsi_91x_sdiodev *dev;
  659. if (!adapter)
  660. return;
  661. dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  662. dev->write_fail = 2;
  663. rsi_mac80211_detach(adapter);
  664. sdio_claim_host(pfunction);
  665. sdio_release_irq(pfunction);
  666. sdio_disable_func(pfunction);
  667. rsi_91x_deinit(adapter);
  668. /* Resetting to take care of the case, where-in driver is re-loaded */
  669. rsi_reset_card(pfunction);
  670. sdio_release_host(pfunction);
  671. }
  672. #ifdef CONFIG_PM
  673. static int rsi_suspend(struct device *dev)
  674. {
  675. /* Not yet implemented */
  676. return -ENOSYS;
  677. }
  678. static int rsi_resume(struct device *dev)
  679. {
  680. /* Not yet implemented */
  681. return -ENOSYS;
  682. }
  683. static const struct dev_pm_ops rsi_pm_ops = {
  684. .suspend = rsi_suspend,
  685. .resume = rsi_resume,
  686. };
  687. #endif
  688. static const struct sdio_device_id rsi_dev_table[] = {
  689. { SDIO_DEVICE(0x303, 0x100) },
  690. { SDIO_DEVICE(0x041B, 0x0301) },
  691. { SDIO_DEVICE(0x041B, 0x0201) },
  692. { SDIO_DEVICE(0x041B, 0x9330) },
  693. { /* Blank */},
  694. };
  695. static struct sdio_driver rsi_driver = {
  696. .name = "RSI-SDIO WLAN",
  697. .probe = rsi_probe,
  698. .remove = rsi_disconnect,
  699. .id_table = rsi_dev_table,
  700. #ifdef CONFIG_PM
  701. .drv = {
  702. .pm = &rsi_pm_ops,
  703. }
  704. #endif
  705. };
  706. /**
  707. * rsi_module_init() - This function registers the sdio module.
  708. * @void: Void.
  709. *
  710. * Return: 0 on success.
  711. */
  712. static int rsi_module_init(void)
  713. {
  714. sdio_register_driver(&rsi_driver);
  715. rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
  716. return 0;
  717. }
  718. /**
  719. * rsi_module_exit() - This function unregisters the sdio module.
  720. * @void: Void.
  721. *
  722. * Return: None.
  723. */
  724. static void rsi_module_exit(void)
  725. {
  726. sdio_unregister_driver(&rsi_driver);
  727. rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
  728. }
  729. module_init(rsi_module_init);
  730. module_exit(rsi_module_exit);
  731. MODULE_AUTHOR("Redpine Signals Inc");
  732. MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
  733. MODULE_SUPPORTED_DEVICE("RSI-91x");
  734. MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
  735. MODULE_FIRMWARE(FIRMWARE_RSI9113);
  736. MODULE_VERSION("0.1");
  737. MODULE_LICENSE("Dual BSD/GPL");