rsi_91x_sdio_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/firmware.h>
  18. #include "rsi_sdio.h"
  19. #include "rsi_common.h"
  20. /**
  21. * rsi_sdio_master_access_msword() - This function sets the AHB master access
  22. * MS word in the SDIO slave registers.
  23. * @adapter: Pointer to the adapter structure.
  24. * @ms_word: ms word need to be initialized.
  25. *
  26. * Return: status: 0 on success, -1 on failure.
  27. */
  28. static int rsi_sdio_master_access_msword(struct rsi_hw *adapter,
  29. u16 ms_word)
  30. {
  31. u8 byte;
  32. u8 function = 0;
  33. int status = 0;
  34. byte = (u8)(ms_word & 0x00FF);
  35. rsi_dbg(INIT_ZONE,
  36. "%s: MASTER_ACCESS_MSBYTE:0x%x\n", __func__, byte);
  37. status = rsi_sdio_write_register(adapter,
  38. function,
  39. SDIO_MASTER_ACCESS_MSBYTE,
  40. &byte);
  41. if (status) {
  42. rsi_dbg(ERR_ZONE,
  43. "%s: fail to access MASTER_ACCESS_MSBYTE\n",
  44. __func__);
  45. return -1;
  46. }
  47. byte = (u8)(ms_word >> 8);
  48. rsi_dbg(INIT_ZONE, "%s:MASTER_ACCESS_LSBYTE:0x%x\n", __func__, byte);
  49. status = rsi_sdio_write_register(adapter,
  50. function,
  51. SDIO_MASTER_ACCESS_LSBYTE,
  52. &byte);
  53. return status;
  54. }
  55. /**
  56. * rsi_copy_to_card() - This function includes the actual funtionality of
  57. * copying the TA firmware to the card.Basically this
  58. * function includes opening the TA file,reading the
  59. * TA file and writing their values in blocks of data.
  60. * @common: Pointer to the driver private structure.
  61. * @fw: Pointer to the firmware value to be written.
  62. * @len: length of firmware file.
  63. * @num_blocks: Number of blocks to be written to the card.
  64. *
  65. * Return: 0 on success and -1 on failure.
  66. */
  67. static int rsi_copy_to_card(struct rsi_common *common,
  68. const u8 *fw,
  69. u32 len,
  70. u32 num_blocks)
  71. {
  72. struct rsi_hw *adapter = common->priv;
  73. struct rsi_91x_sdiodev *dev =
  74. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  75. u32 indx, ii;
  76. u32 block_size = dev->tx_blk_size;
  77. u32 lsb_address;
  78. __le32 data[] = { TA_HOLD_THREAD_VALUE, TA_SOFT_RST_CLR,
  79. TA_PC_ZERO, TA_RELEASE_THREAD_VALUE };
  80. u32 address[] = { TA_HOLD_THREAD_REG, TA_SOFT_RESET_REG,
  81. TA_TH0_PC_REG, TA_RELEASE_THREAD_REG };
  82. u32 base_address;
  83. u16 msb_address;
  84. base_address = TA_LOAD_ADDRESS;
  85. msb_address = base_address >> 16;
  86. for (indx = 0, ii = 0; ii < num_blocks; ii++, indx += block_size) {
  87. lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
  88. if (rsi_sdio_write_register_multiple(adapter,
  89. lsb_address,
  90. (u8 *)(fw + indx),
  91. block_size)) {
  92. rsi_dbg(ERR_ZONE,
  93. "%s: Unable to load %s blk\n", __func__,
  94. FIRMWARE_RSI9113);
  95. return -1;
  96. }
  97. rsi_dbg(INIT_ZONE, "%s: loading block: %d\n", __func__, ii);
  98. base_address += block_size;
  99. if ((base_address >> 16) != msb_address) {
  100. msb_address += 1;
  101. if (rsi_sdio_master_access_msword(adapter,
  102. msb_address)) {
  103. rsi_dbg(ERR_ZONE,
  104. "%s: Unable to set ms word reg\n",
  105. __func__);
  106. return -1;
  107. }
  108. }
  109. }
  110. if (len % block_size) {
  111. lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
  112. if (rsi_sdio_write_register_multiple(adapter,
  113. lsb_address,
  114. (u8 *)(fw + indx),
  115. len % block_size)) {
  116. rsi_dbg(ERR_ZONE,
  117. "%s: Unable to load f/w\n", __func__);
  118. return -1;
  119. }
  120. }
  121. rsi_dbg(INIT_ZONE,
  122. "%s: Succesfully loaded TA instructions\n", __func__);
  123. if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
  124. rsi_dbg(ERR_ZONE,
  125. "%s: Unable to set ms word to common reg\n",
  126. __func__);
  127. return -1;
  128. }
  129. for (ii = 0; ii < ARRAY_SIZE(data); ii++) {
  130. /* Bringing TA out of reset */
  131. if (rsi_sdio_write_register_multiple(adapter,
  132. (address[ii] |
  133. RSI_SD_REQUEST_MASTER),
  134. (u8 *)&data[ii],
  135. 4)) {
  136. rsi_dbg(ERR_ZONE,
  137. "%s: Unable to hold TA threads\n", __func__);
  138. return -1;
  139. }
  140. }
  141. rsi_dbg(INIT_ZONE, "%s: loaded firmware\n", __func__);
  142. return 0;
  143. }
  144. /**
  145. * rsi_load_ta_instructions() - This function includes the actual funtionality
  146. * of loading the TA firmware.This function also
  147. * includes opening the TA file,reading the TA
  148. * file and writing their value in blocks of data.
  149. * @common: Pointer to the driver private structure.
  150. *
  151. * Return: status: 0 on success, -1 on failure.
  152. */
  153. static int rsi_load_ta_instructions(struct rsi_common *common)
  154. {
  155. struct rsi_hw *adapter = common->priv;
  156. struct rsi_91x_sdiodev *dev =
  157. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  158. u32 len;
  159. u32 num_blocks;
  160. const u8 *fw;
  161. const struct firmware *fw_entry = NULL;
  162. u32 block_size = dev->tx_blk_size;
  163. int status = 0;
  164. u32 base_address;
  165. u16 msb_address;
  166. if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
  167. rsi_dbg(ERR_ZONE,
  168. "%s: Unable to set ms word to common reg\n",
  169. __func__);
  170. return -1;
  171. }
  172. base_address = TA_LOAD_ADDRESS;
  173. msb_address = (base_address >> 16);
  174. if (rsi_sdio_master_access_msword(adapter, msb_address)) {
  175. rsi_dbg(ERR_ZONE,
  176. "%s: Unable to set ms word reg\n", __func__);
  177. return -1;
  178. }
  179. status = request_firmware(&fw_entry, FIRMWARE_RSI9113, adapter->device);
  180. if (status < 0) {
  181. rsi_dbg(ERR_ZONE, "%s Firmware file %s not found\n",
  182. __func__, FIRMWARE_RSI9113);
  183. return status;
  184. }
  185. /* Copy firmware into DMA-accessible memory */
  186. fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  187. if (!fw) {
  188. status = -ENOMEM;
  189. goto out;
  190. }
  191. len = fw_entry->size;
  192. if (len % 4)
  193. len += (4 - (len % 4));
  194. num_blocks = (len / block_size);
  195. rsi_dbg(INIT_ZONE, "%s: Instruction size:%d\n", __func__, len);
  196. rsi_dbg(INIT_ZONE, "%s: num blocks: %d\n", __func__, num_blocks);
  197. status = rsi_copy_to_card(common, fw, len, num_blocks);
  198. kfree(fw);
  199. out:
  200. release_firmware(fw_entry);
  201. return status;
  202. }
  203. /**
  204. * rsi_process_pkt() - This Function reads rx_blocks register and figures out
  205. * the size of the rx pkt.
  206. * @common: Pointer to the driver private structure.
  207. *
  208. * Return: 0 on success, -1 on failure.
  209. */
  210. static int rsi_process_pkt(struct rsi_common *common)
  211. {
  212. struct rsi_hw *adapter = common->priv;
  213. u8 num_blks = 0;
  214. u32 rcv_pkt_len = 0;
  215. int status = 0;
  216. status = rsi_sdio_read_register(adapter,
  217. SDIO_RX_NUM_BLOCKS_REG,
  218. &num_blks);
  219. if (status) {
  220. rsi_dbg(ERR_ZONE,
  221. "%s: Failed to read pkt length from the card:\n",
  222. __func__);
  223. return status;
  224. }
  225. rcv_pkt_len = (num_blks * 256);
  226. common->rx_data_pkt = kmalloc(rcv_pkt_len, GFP_KERNEL);
  227. if (!common->rx_data_pkt) {
  228. rsi_dbg(ERR_ZONE, "%s: Failed in memory allocation\n",
  229. __func__);
  230. return -ENOMEM;
  231. }
  232. status = rsi_sdio_host_intf_read_pkt(adapter,
  233. common->rx_data_pkt,
  234. rcv_pkt_len);
  235. if (status) {
  236. rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n",
  237. __func__);
  238. goto fail;
  239. }
  240. status = rsi_read_pkt(common, rcv_pkt_len);
  241. fail:
  242. kfree(common->rx_data_pkt);
  243. return status;
  244. }
  245. /**
  246. * rsi_init_sdio_slave_regs() - This function does the actual initialization
  247. * of SDBUS slave registers.
  248. * @adapter: Pointer to the adapter structure.
  249. *
  250. * Return: status: 0 on success, -1 on failure.
  251. */
  252. int rsi_init_sdio_slave_regs(struct rsi_hw *adapter)
  253. {
  254. struct rsi_91x_sdiodev *dev =
  255. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  256. u8 function = 0;
  257. u8 byte;
  258. int status = 0;
  259. if (dev->next_read_delay) {
  260. byte = dev->next_read_delay;
  261. status = rsi_sdio_write_register(adapter,
  262. function,
  263. SDIO_NXT_RD_DELAY2,
  264. &byte);
  265. if (status) {
  266. rsi_dbg(ERR_ZONE,
  267. "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
  268. __func__);
  269. return -1;
  270. }
  271. }
  272. if (dev->sdio_high_speed_enable) {
  273. rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__);
  274. byte = 0x3;
  275. status = rsi_sdio_write_register(adapter,
  276. function,
  277. SDIO_REG_HIGH_SPEED,
  278. &byte);
  279. if (status) {
  280. rsi_dbg(ERR_ZONE,
  281. "%s: Failed to enable SDIO high speed\n",
  282. __func__);
  283. return -1;
  284. }
  285. }
  286. /* This tells SDIO FIFO when to start read to host */
  287. rsi_dbg(INIT_ZONE, "%s: Initialzing SDIO read start level\n", __func__);
  288. byte = 0x24;
  289. status = rsi_sdio_write_register(adapter,
  290. function,
  291. SDIO_READ_START_LVL,
  292. &byte);
  293. if (status) {
  294. rsi_dbg(ERR_ZONE,
  295. "%s: Failed to write SDIO_READ_START_LVL\n", __func__);
  296. return -1;
  297. }
  298. rsi_dbg(INIT_ZONE, "%s: Initialzing FIFO ctrl registers\n", __func__);
  299. byte = (128 - 32);
  300. status = rsi_sdio_write_register(adapter,
  301. function,
  302. SDIO_READ_FIFO_CTL,
  303. &byte);
  304. if (status) {
  305. rsi_dbg(ERR_ZONE,
  306. "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__);
  307. return -1;
  308. }
  309. byte = 32;
  310. status = rsi_sdio_write_register(adapter,
  311. function,
  312. SDIO_WRITE_FIFO_CTL,
  313. &byte);
  314. if (status) {
  315. rsi_dbg(ERR_ZONE,
  316. "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__);
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. /**
  322. * rsi_interrupt_handler() - This function read and process SDIO interrupts.
  323. * @adapter: Pointer to the adapter structure.
  324. *
  325. * Return: None.
  326. */
  327. void rsi_interrupt_handler(struct rsi_hw *adapter)
  328. {
  329. struct rsi_common *common = adapter->priv;
  330. struct rsi_91x_sdiodev *dev =
  331. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  332. int status;
  333. enum sdio_interrupt_type isr_type;
  334. u8 isr_status = 0;
  335. u8 fw_status = 0;
  336. dev->rx_info.sdio_int_counter++;
  337. do {
  338. mutex_lock(&common->tx_rxlock);
  339. status = rsi_sdio_read_register(common->priv,
  340. RSI_FN1_INT_REGISTER,
  341. &isr_status);
  342. if (status) {
  343. rsi_dbg(ERR_ZONE,
  344. "%s: Failed to Read Intr Status Register\n",
  345. __func__);
  346. mutex_unlock(&common->tx_rxlock);
  347. return;
  348. }
  349. if (isr_status == 0) {
  350. rsi_set_event(&common->tx_thread.event);
  351. dev->rx_info.sdio_intr_status_zero++;
  352. mutex_unlock(&common->tx_rxlock);
  353. return;
  354. }
  355. rsi_dbg(ISR_ZONE, "%s: Intr_status = %x %d %d\n",
  356. __func__, isr_status, (1 << MSDU_PKT_PENDING),
  357. (1 << FW_ASSERT_IND));
  358. do {
  359. RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type);
  360. switch (isr_type) {
  361. case BUFFER_AVAILABLE:
  362. dev->rx_info.watch_bufferfull_count = 0;
  363. dev->rx_info.buffer_full = false;
  364. dev->rx_info.semi_buffer_full = false;
  365. dev->rx_info.mgmt_buffer_full = false;
  366. rsi_sdio_ack_intr(common->priv,
  367. (1 << PKT_BUFF_AVAILABLE));
  368. rsi_set_event(&common->tx_thread.event);
  369. rsi_dbg(ISR_ZONE,
  370. "%s: ==> BUFFER_AVAILABLE <==\n",
  371. __func__);
  372. dev->rx_info.buf_available_counter++;
  373. break;
  374. case FIRMWARE_ASSERT_IND:
  375. rsi_dbg(ERR_ZONE,
  376. "%s: ==> FIRMWARE Assert <==\n",
  377. __func__);
  378. status = rsi_sdio_read_register(common->priv,
  379. SDIO_FW_STATUS_REG,
  380. &fw_status);
  381. if (status) {
  382. rsi_dbg(ERR_ZONE,
  383. "%s: Failed to read f/w reg\n",
  384. __func__);
  385. } else {
  386. rsi_dbg(ERR_ZONE,
  387. "%s: Firmware Status is 0x%x\n",
  388. __func__ , fw_status);
  389. rsi_sdio_ack_intr(common->priv,
  390. (1 << FW_ASSERT_IND));
  391. }
  392. common->fsm_state = FSM_CARD_NOT_READY;
  393. break;
  394. case MSDU_PACKET_PENDING:
  395. rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n");
  396. dev->rx_info.total_sdio_msdu_pending_intr++;
  397. status = rsi_process_pkt(common);
  398. if (status) {
  399. rsi_dbg(ERR_ZONE,
  400. "%s: Failed to read pkt\n",
  401. __func__);
  402. mutex_unlock(&common->tx_rxlock);
  403. return;
  404. }
  405. break;
  406. default:
  407. rsi_sdio_ack_intr(common->priv, isr_status);
  408. dev->rx_info.total_sdio_unknown_intr++;
  409. isr_status = 0;
  410. rsi_dbg(ISR_ZONE,
  411. "Unknown Interrupt %x\n",
  412. isr_status);
  413. break;
  414. }
  415. isr_status ^= BIT(isr_type - 1);
  416. } while (isr_status);
  417. mutex_unlock(&common->tx_rxlock);
  418. } while (1);
  419. }
  420. /**
  421. * rsi_device_init() - This Function Initializes The HAL.
  422. * @common: Pointer to the driver private structure.
  423. *
  424. * Return: 0 on success, -1 on failure.
  425. */
  426. int rsi_sdio_device_init(struct rsi_common *common)
  427. {
  428. if (rsi_load_ta_instructions(common))
  429. return -1;
  430. if (rsi_sdio_master_access_msword(common->priv, MISC_CFG_BASE_ADDR)) {
  431. rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n",
  432. __func__);
  433. return -1;
  434. }
  435. rsi_dbg(INIT_ZONE,
  436. "%s: Setting ms word to 0x41050000\n", __func__);
  437. return 0;
  438. }
  439. /**
  440. * rsi_sdio_read_buffer_status_register() - This function is used to the read
  441. * buffer status register and set
  442. * relevant fields in
  443. * rsi_91x_sdiodev struct.
  444. * @adapter: Pointer to the driver hw structure.
  445. * @q_num: The Q number whose status is to be found.
  446. *
  447. * Return: status: -1 on failure or else queue full/stop is indicated.
  448. */
  449. int rsi_sdio_read_buffer_status_register(struct rsi_hw *adapter, u8 q_num)
  450. {
  451. struct rsi_common *common = adapter->priv;
  452. struct rsi_91x_sdiodev *dev =
  453. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  454. u8 buf_status = 0;
  455. int status = 0;
  456. status = rsi_sdio_read_register(common->priv,
  457. RSI_DEVICE_BUFFER_STATUS_REGISTER,
  458. &buf_status);
  459. if (status) {
  460. rsi_dbg(ERR_ZONE,
  461. "%s: Failed to read status register\n", __func__);
  462. return -1;
  463. }
  464. if (buf_status & (BIT(PKT_MGMT_BUFF_FULL))) {
  465. if (!dev->rx_info.mgmt_buffer_full)
  466. dev->rx_info.mgmt_buf_full_counter++;
  467. dev->rx_info.mgmt_buffer_full = true;
  468. } else {
  469. dev->rx_info.mgmt_buffer_full = false;
  470. }
  471. if (buf_status & (BIT(PKT_BUFF_FULL))) {
  472. if (!dev->rx_info.buffer_full)
  473. dev->rx_info.buf_full_counter++;
  474. dev->rx_info.buffer_full = true;
  475. } else {
  476. dev->rx_info.buffer_full = false;
  477. }
  478. if (buf_status & (BIT(PKT_BUFF_SEMI_FULL))) {
  479. if (!dev->rx_info.semi_buffer_full)
  480. dev->rx_info.buf_semi_full_counter++;
  481. dev->rx_info.semi_buffer_full = true;
  482. } else {
  483. dev->rx_info.semi_buffer_full = false;
  484. }
  485. if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full))
  486. return QUEUE_FULL;
  487. if (dev->rx_info.buffer_full)
  488. return QUEUE_FULL;
  489. return QUEUE_NOT_FULL;
  490. }
  491. /**
  492. * rsi_sdio_determine_event_timeout() - This Function determines the event
  493. * timeout duration.
  494. * @adapter: Pointer to the adapter structure.
  495. *
  496. * Return: timeout duration is returned.
  497. */
  498. int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter)
  499. {
  500. struct rsi_91x_sdiodev *dev =
  501. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  502. /* Once buffer full is seen, event timeout to occur every 2 msecs */
  503. if (dev->rx_info.buffer_full)
  504. return 2;
  505. return EVENT_WAIT_FOREVER;
  506. }