rsi_91x_sdio_ops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <net/rsi_91x.h>
  19. #include "rsi_sdio.h"
  20. #include "rsi_common.h"
  21. /**
  22. * rsi_sdio_master_access_msword() - This function sets the AHB master access
  23. * MS word in the SDIO slave registers.
  24. * @adapter: Pointer to the adapter structure.
  25. * @ms_word: ms word need to be initialized.
  26. *
  27. * Return: status: 0 on success, -1 on failure.
  28. */
  29. int rsi_sdio_master_access_msword(struct rsi_hw *adapter, 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. void rsi_sdio_rx_thread(struct rsi_common *common)
  56. {
  57. struct rsi_hw *adapter = common->priv;
  58. struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
  59. struct sk_buff *skb;
  60. int status;
  61. do {
  62. rsi_wait_event(&sdev->rx_thread.event, EVENT_WAIT_FOREVER);
  63. rsi_reset_event(&sdev->rx_thread.event);
  64. while (true) {
  65. if (atomic_read(&sdev->rx_thread.thread_done))
  66. goto out;
  67. skb = skb_dequeue(&sdev->rx_q.head);
  68. if (!skb)
  69. break;
  70. if (sdev->rx_q.num_rx_pkts > 0)
  71. sdev->rx_q.num_rx_pkts--;
  72. status = rsi_read_pkt(common, skb->data, skb->len);
  73. if (status) {
  74. rsi_dbg(ERR_ZONE, "Failed to read the packet\n");
  75. dev_kfree_skb(skb);
  76. break;
  77. }
  78. dev_kfree_skb(skb);
  79. }
  80. } while (1);
  81. out:
  82. rsi_dbg(INFO_ZONE, "%s: Terminated SDIO RX thread\n", __func__);
  83. skb_queue_purge(&sdev->rx_q.head);
  84. atomic_inc(&sdev->rx_thread.thread_done);
  85. complete_and_exit(&sdev->rx_thread.completion, 0);
  86. }
  87. /**
  88. * rsi_process_pkt() - This Function reads rx_blocks register and figures out
  89. * the size of the rx pkt.
  90. * @common: Pointer to the driver private structure.
  91. *
  92. * Return: 0 on success, -1 on failure.
  93. */
  94. static int rsi_process_pkt(struct rsi_common *common)
  95. {
  96. struct rsi_hw *adapter = common->priv;
  97. struct rsi_91x_sdiodev *dev =
  98. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  99. u8 num_blks = 0;
  100. u32 rcv_pkt_len = 0;
  101. int status = 0;
  102. u8 value = 0;
  103. struct sk_buff *skb;
  104. if (dev->rx_q.num_rx_pkts >= RSI_MAX_RX_PKTS)
  105. return 0;
  106. num_blks = ((adapter->interrupt_status & 1) |
  107. ((adapter->interrupt_status >> RECV_NUM_BLOCKS) << 1));
  108. if (!num_blks) {
  109. status = rsi_sdio_read_register(adapter,
  110. SDIO_RX_NUM_BLOCKS_REG,
  111. &value);
  112. if (status) {
  113. rsi_dbg(ERR_ZONE,
  114. "%s: Failed to read pkt length from the card:\n",
  115. __func__);
  116. return status;
  117. }
  118. num_blks = value & 0x1f;
  119. }
  120. if (dev->write_fail == 2)
  121. rsi_sdio_ack_intr(common->priv, (1 << MSDU_PKT_PENDING));
  122. if (unlikely(!num_blks)) {
  123. dev->write_fail = 2;
  124. return -1;
  125. }
  126. rcv_pkt_len = (num_blks * 256);
  127. skb = dev_alloc_skb(rcv_pkt_len);
  128. if (!skb)
  129. return -ENOMEM;
  130. status = rsi_sdio_host_intf_read_pkt(adapter, skb->data, rcv_pkt_len);
  131. if (status) {
  132. rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n",
  133. __func__);
  134. dev_kfree_skb(skb);
  135. return status;
  136. }
  137. skb_put(skb, rcv_pkt_len);
  138. skb_queue_tail(&dev->rx_q.head, skb);
  139. dev->rx_q.num_rx_pkts++;
  140. rsi_set_event(&dev->rx_thread.event);
  141. return 0;
  142. }
  143. /**
  144. * rsi_init_sdio_slave_regs() - This function does the actual initialization
  145. * of SDBUS slave registers.
  146. * @adapter: Pointer to the adapter structure.
  147. *
  148. * Return: status: 0 on success, -1 on failure.
  149. */
  150. int rsi_init_sdio_slave_regs(struct rsi_hw *adapter)
  151. {
  152. struct rsi_91x_sdiodev *dev =
  153. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  154. u8 function = 0;
  155. u8 byte;
  156. int status = 0;
  157. if (dev->next_read_delay) {
  158. byte = dev->next_read_delay;
  159. status = rsi_sdio_write_register(adapter,
  160. function,
  161. SDIO_NXT_RD_DELAY2,
  162. &byte);
  163. if (status) {
  164. rsi_dbg(ERR_ZONE,
  165. "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
  166. __func__);
  167. return -1;
  168. }
  169. }
  170. if (dev->sdio_high_speed_enable) {
  171. rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__);
  172. byte = 0x3;
  173. status = rsi_sdio_write_register(adapter,
  174. function,
  175. SDIO_REG_HIGH_SPEED,
  176. &byte);
  177. if (status) {
  178. rsi_dbg(ERR_ZONE,
  179. "%s: Failed to enable SDIO high speed\n",
  180. __func__);
  181. return -1;
  182. }
  183. }
  184. /* This tells SDIO FIFO when to start read to host */
  185. rsi_dbg(INIT_ZONE, "%s: Initialzing SDIO read start level\n", __func__);
  186. byte = 0x24;
  187. status = rsi_sdio_write_register(adapter,
  188. function,
  189. SDIO_READ_START_LVL,
  190. &byte);
  191. if (status) {
  192. rsi_dbg(ERR_ZONE,
  193. "%s: Failed to write SDIO_READ_START_LVL\n", __func__);
  194. return -1;
  195. }
  196. rsi_dbg(INIT_ZONE, "%s: Initialzing FIFO ctrl registers\n", __func__);
  197. byte = (128 - 32);
  198. status = rsi_sdio_write_register(adapter,
  199. function,
  200. SDIO_READ_FIFO_CTL,
  201. &byte);
  202. if (status) {
  203. rsi_dbg(ERR_ZONE,
  204. "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__);
  205. return -1;
  206. }
  207. byte = 32;
  208. status = rsi_sdio_write_register(adapter,
  209. function,
  210. SDIO_WRITE_FIFO_CTL,
  211. &byte);
  212. if (status) {
  213. rsi_dbg(ERR_ZONE,
  214. "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__);
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * rsi_interrupt_handler() - This function read and process SDIO interrupts.
  221. * @adapter: Pointer to the adapter structure.
  222. *
  223. * Return: None.
  224. */
  225. void rsi_interrupt_handler(struct rsi_hw *adapter)
  226. {
  227. struct rsi_common *common = adapter->priv;
  228. struct rsi_91x_sdiodev *dev =
  229. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  230. int status;
  231. enum sdio_interrupt_type isr_type;
  232. u8 isr_status = 0;
  233. u8 fw_status = 0;
  234. dev->rx_info.sdio_int_counter++;
  235. do {
  236. mutex_lock(&common->rx_lock);
  237. status = rsi_sdio_read_register(common->priv,
  238. RSI_FN1_INT_REGISTER,
  239. &isr_status);
  240. if (status) {
  241. rsi_dbg(ERR_ZONE,
  242. "%s: Failed to Read Intr Status Register\n",
  243. __func__);
  244. mutex_unlock(&common->rx_lock);
  245. return;
  246. }
  247. adapter->interrupt_status = isr_status;
  248. if (isr_status == 0) {
  249. rsi_set_event(&common->tx_thread.event);
  250. dev->rx_info.sdio_intr_status_zero++;
  251. mutex_unlock(&common->rx_lock);
  252. return;
  253. }
  254. rsi_dbg(ISR_ZONE, "%s: Intr_status = %x %d %d\n",
  255. __func__, isr_status, (1 << MSDU_PKT_PENDING),
  256. (1 << FW_ASSERT_IND));
  257. do {
  258. RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type);
  259. switch (isr_type) {
  260. case BUFFER_AVAILABLE:
  261. status = rsi_sdio_check_buffer_status(adapter,
  262. 0);
  263. if (status < 0)
  264. rsi_dbg(ERR_ZONE,
  265. "%s: Failed to check buffer status\n",
  266. __func__);
  267. rsi_sdio_ack_intr(common->priv,
  268. (1 << PKT_BUFF_AVAILABLE));
  269. rsi_set_event(&common->tx_thread.event);
  270. rsi_dbg(ISR_ZONE,
  271. "%s: ==> BUFFER_AVAILABLE <==\n",
  272. __func__);
  273. dev->buff_status_updated = true;
  274. break;
  275. case FIRMWARE_ASSERT_IND:
  276. rsi_dbg(ERR_ZONE,
  277. "%s: ==> FIRMWARE Assert <==\n",
  278. __func__);
  279. status = rsi_sdio_read_register(common->priv,
  280. SDIO_FW_STATUS_REG,
  281. &fw_status);
  282. if (status) {
  283. rsi_dbg(ERR_ZONE,
  284. "%s: Failed to read f/w reg\n",
  285. __func__);
  286. } else {
  287. rsi_dbg(ERR_ZONE,
  288. "%s: Firmware Status is 0x%x\n",
  289. __func__ , fw_status);
  290. rsi_sdio_ack_intr(common->priv,
  291. (1 << FW_ASSERT_IND));
  292. }
  293. common->fsm_state = FSM_CARD_NOT_READY;
  294. break;
  295. case MSDU_PACKET_PENDING:
  296. rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n");
  297. dev->rx_info.total_sdio_msdu_pending_intr++;
  298. status = rsi_process_pkt(common);
  299. if (status) {
  300. rsi_dbg(ERR_ZONE,
  301. "%s: Failed to read pkt\n",
  302. __func__);
  303. mutex_unlock(&common->rx_lock);
  304. return;
  305. }
  306. break;
  307. default:
  308. rsi_sdio_ack_intr(common->priv, isr_status);
  309. dev->rx_info.total_sdio_unknown_intr++;
  310. isr_status = 0;
  311. rsi_dbg(ISR_ZONE,
  312. "Unknown Interrupt %x\n",
  313. isr_status);
  314. break;
  315. }
  316. isr_status ^= BIT(isr_type - 1);
  317. } while (isr_status);
  318. mutex_unlock(&common->rx_lock);
  319. } while (1);
  320. }
  321. /* This function is used to read buffer status register and
  322. * set relevant fields in rsi_91x_sdiodev struct.
  323. */
  324. int rsi_sdio_check_buffer_status(struct rsi_hw *adapter, u8 q_num)
  325. {
  326. struct rsi_common *common = adapter->priv;
  327. struct rsi_91x_sdiodev *dev =
  328. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  329. u8 buf_status = 0;
  330. int status = 0;
  331. static int counter = 4;
  332. if (!dev->buff_status_updated && counter) {
  333. counter--;
  334. goto out;
  335. }
  336. dev->buff_status_updated = false;
  337. status = rsi_sdio_read_register(common->priv,
  338. RSI_DEVICE_BUFFER_STATUS_REGISTER,
  339. &buf_status);
  340. if (status) {
  341. rsi_dbg(ERR_ZONE,
  342. "%s: Failed to read status register\n", __func__);
  343. return -1;
  344. }
  345. if (buf_status & (BIT(PKT_MGMT_BUFF_FULL))) {
  346. if (!dev->rx_info.mgmt_buffer_full)
  347. dev->rx_info.mgmt_buf_full_counter++;
  348. dev->rx_info.mgmt_buffer_full = true;
  349. } else {
  350. dev->rx_info.mgmt_buffer_full = false;
  351. }
  352. if (buf_status & (BIT(PKT_BUFF_FULL))) {
  353. if (!dev->rx_info.buffer_full)
  354. dev->rx_info.buf_full_counter++;
  355. dev->rx_info.buffer_full = true;
  356. } else {
  357. dev->rx_info.buffer_full = false;
  358. }
  359. if (buf_status & (BIT(PKT_BUFF_SEMI_FULL))) {
  360. if (!dev->rx_info.semi_buffer_full)
  361. dev->rx_info.buf_semi_full_counter++;
  362. dev->rx_info.semi_buffer_full = true;
  363. } else {
  364. dev->rx_info.semi_buffer_full = false;
  365. }
  366. if (dev->rx_info.mgmt_buffer_full || dev->rx_info.buf_full_counter)
  367. counter = 1;
  368. else
  369. counter = 4;
  370. out:
  371. if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full))
  372. return QUEUE_FULL;
  373. if ((q_num < MGMT_SOFT_Q) && (dev->rx_info.buffer_full))
  374. return QUEUE_FULL;
  375. return QUEUE_NOT_FULL;
  376. }
  377. /**
  378. * rsi_sdio_determine_event_timeout() - This Function determines the event
  379. * timeout duration.
  380. * @adapter: Pointer to the adapter structure.
  381. *
  382. * Return: timeout duration is returned.
  383. */
  384. int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter)
  385. {
  386. struct rsi_91x_sdiodev *dev =
  387. (struct rsi_91x_sdiodev *)adapter->rsi_dev;
  388. /* Once buffer full is seen, event timeout to occur every 2 msecs */
  389. if (dev->rx_info.buffer_full)
  390. return 2;
  391. return EVENT_WAIT_FOREVER;
  392. }