sdio-rx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_rx()
  57. *
  58. * i2400ms_rx_setup()
  59. *
  60. * i2400ms_rx_release()
  61. */
  62. #include <linux/workqueue.h>
  63. #include <linux/wait.h>
  64. #include <linux/skbuff.h>
  65. #include <linux/mmc/sdio.h>
  66. #include <linux/mmc/sdio_func.h>
  67. #include "i2400m-sdio.h"
  68. #define D_SUBMODULE rx
  69. #include "sdio-debug-levels.h"
  70. /*
  71. * Read and return the amount of bytes available for RX
  72. *
  73. * The RX size has to be read like this: byte reads of three
  74. * sequential locations; then glue'em together.
  75. *
  76. * sdio_readl() doesn't work.
  77. */
  78. ssize_t __i2400ms_rx_get_size(struct i2400ms *i2400ms)
  79. {
  80. int ret, cnt, val;
  81. ssize_t rx_size;
  82. unsigned xfer_size_addr;
  83. struct sdio_func *func = i2400ms->func;
  84. struct device *dev = &i2400ms->func->dev;
  85. d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
  86. xfer_size_addr = I2400MS_INTR_GET_SIZE_ADDR;
  87. rx_size = 0;
  88. for (cnt = 0; cnt < 3; cnt++) {
  89. val = sdio_readb(func, xfer_size_addr + cnt, &ret);
  90. if (ret < 0) {
  91. dev_err(dev, "RX: Can't read byte %d of RX size from "
  92. "0x%08x: %d\n", cnt, xfer_size_addr + cnt, ret);
  93. rx_size = ret;
  94. goto error_read;
  95. }
  96. rx_size = rx_size << 8 | (val & 0xff);
  97. }
  98. d_printf(6, dev, "RX: rx_size is %ld\n", (long) rx_size);
  99. error_read:
  100. d_fnend(7, dev, "(i2400ms %p) = %ld\n", i2400ms, (long) rx_size);
  101. return rx_size;
  102. }
  103. /*
  104. * Read data from the device (when in normal)
  105. *
  106. * Allocate an SKB of the right size, read the data in and then
  107. * deliver it to the generic layer.
  108. *
  109. * We also check for a reboot barker. That means the device died and
  110. * we have to reboot it.
  111. */
  112. static
  113. void i2400ms_rx(struct i2400ms *i2400ms)
  114. {
  115. int ret;
  116. struct sdio_func *func = i2400ms->func;
  117. struct device *dev = &func->dev;
  118. struct i2400m *i2400m = &i2400ms->i2400m;
  119. struct sk_buff *skb;
  120. ssize_t rx_size;
  121. d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
  122. rx_size = __i2400ms_rx_get_size(i2400ms);
  123. if (rx_size < 0) {
  124. ret = rx_size;
  125. goto error_get_size;
  126. }
  127. ret = -ENOMEM;
  128. skb = alloc_skb(rx_size, GFP_ATOMIC);
  129. if (NULL == skb) {
  130. dev_err(dev, "RX: unable to alloc skb\n");
  131. goto error_alloc_skb;
  132. }
  133. ret = sdio_memcpy_fromio(func, skb->data,
  134. I2400MS_DATA_ADDR, rx_size);
  135. if (ret < 0) {
  136. dev_err(dev, "RX: SDIO data read failed: %d\n", ret);
  137. goto error_memcpy_fromio;
  138. }
  139. /* Check if device has reset */
  140. if (!memcmp(skb->data, i2400m_NBOOT_BARKER,
  141. sizeof(i2400m_NBOOT_BARKER))
  142. || !memcmp(skb->data, i2400m_SBOOT_BARKER,
  143. sizeof(i2400m_SBOOT_BARKER))) {
  144. ret = i2400m_dev_reset_handle(i2400m);
  145. kfree_skb(skb);
  146. } else {
  147. skb_put(skb, rx_size);
  148. i2400m_rx(i2400m, skb);
  149. }
  150. d_fnend(7, dev, "(i2400ms %p) = void\n", i2400ms);
  151. return;
  152. error_memcpy_fromio:
  153. kfree_skb(skb);
  154. error_alloc_skb:
  155. error_get_size:
  156. d_fnend(7, dev, "(i2400ms %p) = %d\n", i2400ms, ret);
  157. return;
  158. }
  159. /*
  160. * Process an interrupt from the SDIO card
  161. *
  162. * FIXME: need to process other events that are not just ready-to-read
  163. *
  164. * Checks there is data ready and then proceeds to read it.
  165. */
  166. static
  167. void i2400ms_irq(struct sdio_func *func)
  168. {
  169. int ret;
  170. struct i2400ms *i2400ms = sdio_get_drvdata(func);
  171. struct i2400m *i2400m = &i2400ms->i2400m;
  172. struct device *dev = &func->dev;
  173. int val;
  174. d_fnstart(6, dev, "(i2400ms %p)\n", i2400ms);
  175. val = sdio_readb(func, I2400MS_INTR_STATUS_ADDR, &ret);
  176. if (ret < 0) {
  177. dev_err(dev, "RX: Can't read interrupt status: %d\n", ret);
  178. goto error_no_irq;
  179. }
  180. if (!val) {
  181. dev_err(dev, "RX: BUG? got IRQ but no interrupt ready?\n");
  182. goto error_no_irq;
  183. }
  184. sdio_writeb(func, 1, I2400MS_INTR_CLEAR_ADDR, &ret);
  185. if (WARN_ON(i2400m->boot_mode != 0))
  186. dev_err(dev, "RX: SW BUG? boot mode and IRQ is up?\n");
  187. else
  188. i2400ms_rx(i2400ms);
  189. error_no_irq:
  190. d_fnend(6, dev, "(i2400ms %p) = void\n", i2400ms);
  191. return;
  192. }
  193. /*
  194. * Setup SDIO RX
  195. *
  196. * Hooks up the IRQ handler and then enables IRQs.
  197. */
  198. int i2400ms_rx_setup(struct i2400ms *i2400ms)
  199. {
  200. int result;
  201. struct sdio_func *func = i2400ms->func;
  202. struct device *dev = &func->dev;
  203. d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
  204. sdio_claim_host(func);
  205. result = sdio_claim_irq(func, i2400ms_irq);
  206. if (result < 0) {
  207. dev_err(dev, "Cannot claim IRQ: %d\n", result);
  208. goto error_irq_claim;
  209. }
  210. result = 0;
  211. sdio_writeb(func, 1, I2400MS_INTR_ENABLE_ADDR, &result);
  212. if (result < 0) {
  213. sdio_release_irq(func);
  214. dev_err(dev, "Failed to enable interrupts %d\n", result);
  215. }
  216. error_irq_claim:
  217. sdio_release_host(func);
  218. d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
  219. return result;
  220. }
  221. /*
  222. * Tear down SDIO RX
  223. *
  224. * Disables IRQs in the device and removes the IRQ handler.
  225. */
  226. void i2400ms_rx_release(struct i2400ms *i2400ms)
  227. {
  228. int result;
  229. struct sdio_func *func = i2400ms->func;
  230. struct device *dev = &func->dev;
  231. d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
  232. sdio_claim_host(func);
  233. sdio_writeb(func, 0, I2400MS_INTR_ENABLE_ADDR, &result);
  234. sdio_release_irq(func);
  235. sdio_release_host(func);
  236. d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
  237. }