sdio-rx.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * SDIO RX handling
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *
  35. * Intel Corporation <linux-wimax@intel.com>
  36. * Dirk Brandewie <dirk.j.brandewie@intel.com>
  37. * - Initial implementation
  38. *
  39. *
  40. * This handles the RX path on SDIO.
  41. *
  42. * The SDIO bus driver calls the "irq" routine when data is available.
  43. * This is not a traditional interrupt routine since the SDIO bus
  44. * driver calls us from its irq thread context. Because of this
  45. * sleeping in the SDIO RX IRQ routine is okay.
  46. *
  47. * From there on, we obtain the size of the data that is available,
  48. * allocate an skb, copy it and then pass it to the generic driver's
  49. * RX routine [i2400m_rx()].
  50. *
  51. * ROADMAP
  52. *
  53. * i2400ms_irq()
  54. * i2400ms_rx()
  55. * __i2400ms_rx_get_size()
  56. * i2400m_is_boot_barker()
  57. * i2400m_rx()
  58. *
  59. * i2400ms_rx_setup()
  60. *
  61. * i2400ms_rx_release()
  62. */
  63. #include <linux/workqueue.h>
  64. #include <linux/wait.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/mmc/sdio.h>
  67. #include <linux/mmc/sdio_func.h>
  68. #include "i2400m-sdio.h"
  69. #define D_SUBMODULE rx
  70. #include "sdio-debug-levels.h"
  71. static const __le32 i2400m_ACK_BARKER[4] = {
  72. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  73. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  74. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  75. __constant_cpu_to_le32(I2400M_ACK_BARKER)
  76. };
  77. /*
  78. * Read and return the amount of bytes available for RX
  79. *
  80. * The RX size has to be read like this: byte reads of three
  81. * sequential locations; then glue'em together.
  82. *
  83. * sdio_readl() doesn't work.
  84. */
  85. ssize_t __i2400ms_rx_get_size(struct i2400ms *i2400ms)
  86. {
  87. int ret, cnt, val;
  88. ssize_t rx_size;
  89. unsigned xfer_size_addr;
  90. struct sdio_func *func = i2400ms->func;
  91. struct device *dev = &i2400ms->func->dev;
  92. d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
  93. xfer_size_addr = I2400MS_INTR_GET_SIZE_ADDR;
  94. rx_size = 0;
  95. for (cnt = 0; cnt < 3; cnt++) {
  96. val = sdio_readb(func, xfer_size_addr + cnt, &ret);
  97. if (ret < 0) {
  98. dev_err(dev, "RX: Can't read byte %d of RX size from "
  99. "0x%08x: %d\n", cnt, xfer_size_addr + cnt, ret);
  100. rx_size = ret;
  101. goto error_read;
  102. }
  103. rx_size = rx_size << 8 | (val & 0xff);
  104. }
  105. d_printf(6, dev, "RX: rx_size is %ld\n", (long) rx_size);
  106. error_read:
  107. d_fnend(7, dev, "(i2400ms %p) = %ld\n", i2400ms, (long) rx_size);
  108. return rx_size;
  109. }
  110. /*
  111. * Read data from the device (when in normal)
  112. *
  113. * Allocate an SKB of the right size, read the data in and then
  114. * deliver it to the generic layer.
  115. *
  116. * We also check for a reboot barker. That means the device died and
  117. * we have to reboot it.
  118. */
  119. static
  120. void i2400ms_rx(struct i2400ms *i2400ms)
  121. {
  122. int ret;
  123. struct sdio_func *func = i2400ms->func;
  124. struct device *dev = &func->dev;
  125. struct i2400m *i2400m = &i2400ms->i2400m;
  126. struct sk_buff *skb;
  127. ssize_t rx_size;
  128. d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
  129. rx_size = __i2400ms_rx_get_size(i2400ms);
  130. if (rx_size < 0) {
  131. ret = rx_size;
  132. goto error_get_size;
  133. }
  134. /*
  135. * Hardware quirk: make sure to clear the INTR status register
  136. * AFTER getting the data transfer size.
  137. */
  138. sdio_writeb(func, 1, I2400MS_INTR_CLEAR_ADDR, &ret);
  139. ret = -ENOMEM;
  140. skb = alloc_skb(rx_size, GFP_ATOMIC);
  141. if (NULL == skb) {
  142. dev_err(dev, "RX: unable to alloc skb\n");
  143. goto error_alloc_skb;
  144. }
  145. ret = sdio_memcpy_fromio(func, skb->data,
  146. I2400MS_DATA_ADDR, rx_size);
  147. if (ret < 0) {
  148. dev_err(dev, "RX: SDIO data read failed: %d\n", ret);
  149. goto error_memcpy_fromio;
  150. }
  151. rmb(); /* make sure we get boot_mode from dev_reset_handle */
  152. if (unlikely(i2400m->boot_mode == 1)) {
  153. spin_lock(&i2400m->rx_lock);
  154. i2400ms->bm_ack_size = rx_size;
  155. spin_unlock(&i2400m->rx_lock);
  156. memcpy(i2400m->bm_ack_buf, skb->data, rx_size);
  157. wake_up(&i2400ms->bm_wfa_wq);
  158. d_printf(5, dev, "RX: SDIO boot mode message\n");
  159. kfree_skb(skb);
  160. goto out;
  161. }
  162. ret = -EIO;
  163. if (unlikely(rx_size < sizeof(__le32))) {
  164. dev_err(dev, "HW BUG? only %zu bytes received\n", rx_size);
  165. goto error_bad_size;
  166. }
  167. if (likely(i2400m_is_d2h_barker(skb->data))) {
  168. skb_put(skb, rx_size);
  169. i2400m_rx(i2400m, skb);
  170. } else if (unlikely(i2400m_is_boot_barker(i2400m,
  171. skb->data, rx_size))) {
  172. ret = i2400m_dev_reset_handle(i2400m, "device rebooted");
  173. dev_err(dev, "RX: SDIO reboot barker\n");
  174. kfree_skb(skb);
  175. } else {
  176. i2400m_unknown_barker(i2400m, skb->data, rx_size);
  177. kfree_skb(skb);
  178. }
  179. out:
  180. d_fnend(7, dev, "(i2400ms %p) = void\n", i2400ms);
  181. return;
  182. error_memcpy_fromio:
  183. kfree_skb(skb);
  184. error_alloc_skb:
  185. error_get_size:
  186. error_bad_size:
  187. d_fnend(7, dev, "(i2400ms %p) = %d\n", i2400ms, ret);
  188. return;
  189. }
  190. /*
  191. * Process an interrupt from the SDIO card
  192. *
  193. * FIXME: need to process other events that are not just ready-to-read
  194. *
  195. * Checks there is data ready and then proceeds to read it.
  196. */
  197. static
  198. void i2400ms_irq(struct sdio_func *func)
  199. {
  200. int ret;
  201. struct i2400ms *i2400ms = sdio_get_drvdata(func);
  202. struct device *dev = &func->dev;
  203. int val;
  204. d_fnstart(6, dev, "(i2400ms %p)\n", i2400ms);
  205. val = sdio_readb(func, I2400MS_INTR_STATUS_ADDR, &ret);
  206. if (ret < 0) {
  207. dev_err(dev, "RX: Can't read interrupt status: %d\n", ret);
  208. goto error_no_irq;
  209. }
  210. if (!val) {
  211. dev_err(dev, "RX: BUG? got IRQ but no interrupt ready?\n");
  212. goto error_no_irq;
  213. }
  214. i2400ms_rx(i2400ms);
  215. error_no_irq:
  216. d_fnend(6, dev, "(i2400ms %p) = void\n", i2400ms);
  217. return;
  218. }
  219. /*
  220. * Setup SDIO RX
  221. *
  222. * Hooks up the IRQ handler and then enables IRQs.
  223. */
  224. int i2400ms_rx_setup(struct i2400ms *i2400ms)
  225. {
  226. int result;
  227. struct sdio_func *func = i2400ms->func;
  228. struct device *dev = &func->dev;
  229. struct i2400m *i2400m = &i2400ms->i2400m;
  230. d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
  231. init_waitqueue_head(&i2400ms->bm_wfa_wq);
  232. spin_lock(&i2400m->rx_lock);
  233. i2400ms->bm_wait_result = -EINPROGRESS;
  234. /*
  235. * Before we are about to enable the RX interrupt, make sure
  236. * bm_ack_size is cleared to -EINPROGRESS which indicates
  237. * no RX interrupt happened yet or the previous interrupt
  238. * has been handled, we are ready to take the new interrupt
  239. */
  240. i2400ms->bm_ack_size = -EINPROGRESS;
  241. spin_unlock(&i2400m->rx_lock);
  242. sdio_claim_host(func);
  243. result = sdio_claim_irq(func, i2400ms_irq);
  244. if (result < 0) {
  245. dev_err(dev, "Cannot claim IRQ: %d\n", result);
  246. goto error_irq_claim;
  247. }
  248. result = 0;
  249. sdio_writeb(func, 1, I2400MS_INTR_ENABLE_ADDR, &result);
  250. if (result < 0) {
  251. sdio_release_irq(func);
  252. dev_err(dev, "Failed to enable interrupts %d\n", result);
  253. }
  254. error_irq_claim:
  255. sdio_release_host(func);
  256. d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
  257. return result;
  258. }
  259. /*
  260. * Tear down SDIO RX
  261. *
  262. * Disables IRQs in the device and removes the IRQ handler.
  263. */
  264. void i2400ms_rx_release(struct i2400ms *i2400ms)
  265. {
  266. int result;
  267. struct sdio_func *func = i2400ms->func;
  268. struct device *dev = &func->dev;
  269. struct i2400m *i2400m = &i2400ms->i2400m;
  270. d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
  271. spin_lock(&i2400m->rx_lock);
  272. i2400ms->bm_ack_size = -EINTR;
  273. spin_unlock(&i2400m->rx_lock);
  274. wake_up_all(&i2400ms->bm_wfa_wq);
  275. sdio_claim_host(func);
  276. sdio_writeb(func, 0, I2400MS_INTR_ENABLE_ADDR, &result);
  277. sdio_release_irq(func);
  278. sdio_release_host(func);
  279. d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
  280. }