sdio-fw.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Firmware uploader's SDIO specifics
  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. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  37. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  38. * - Initial implementation
  39. *
  40. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  41. * - Bus generic/specific split for USB
  42. *
  43. * Dirk Brandewie <dirk.j.brandewie@intel.com>
  44. * - Initial implementation for SDIO
  45. *
  46. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  47. * - SDIO rehash for changes in the bus-driver model
  48. *
  49. * THE PROCEDURE
  50. *
  51. * See fw.c for the generic description of this procedure.
  52. *
  53. * This file implements only the SDIO specifics. It boils down to how
  54. * to send a command and waiting for an acknowledgement from the
  55. * device. We do polled reads.
  56. *
  57. * COMMAND EXECUTION
  58. *
  59. * THe generic firmware upload code will call i2400m_bus_bm_cmd_send()
  60. * to send commands.
  61. *
  62. * The SDIO devices expects things in 256 byte blocks, so it will pad
  63. * it, compute the checksum (if needed) and pass it to SDIO.
  64. *
  65. * ACK RECEPTION
  66. *
  67. * This works in polling mode -- the fw loader says when to wait for
  68. * data and for that it calls i2400ms_bus_bm_wait_for_ack().
  69. *
  70. * This will poll the device for data until it is received. We need to
  71. * receive at least as much bytes as where asked for (although it'll
  72. * always be a multiple of 256 bytes).
  73. */
  74. #include <linux/mmc/sdio_func.h>
  75. #include "i2400m-sdio.h"
  76. #define D_SUBMODULE fw
  77. #include "sdio-debug-levels.h"
  78. /*
  79. * Send a boot-mode command to the SDIO function
  80. *
  81. * We use a bounce buffer (i2400m->bm_cmd_buf) because we need to
  82. * touch the header if the RAW flag is not set.
  83. *
  84. * @flags: pass thru from i2400m_bm_cmd()
  85. * @return: cmd_size if ok, < 0 errno code on error.
  86. *
  87. * Note the command is padded to the SDIO block size for the device.
  88. */
  89. ssize_t i2400ms_bus_bm_cmd_send(struct i2400m *i2400m,
  90. const struct i2400m_bootrom_header *_cmd,
  91. size_t cmd_size, int flags)
  92. {
  93. ssize_t result;
  94. struct device *dev = i2400m_dev(i2400m);
  95. struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
  96. int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
  97. struct i2400m_bootrom_header *cmd;
  98. /* SDIO restriction */
  99. size_t cmd_size_a = ALIGN(cmd_size, I2400MS_BLK_SIZE);
  100. d_fnstart(5, dev, "(i2400m %p cmd %p size %zu)\n",
  101. i2400m, _cmd, cmd_size);
  102. result = -E2BIG;
  103. if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
  104. goto error_too_big;
  105. memcpy(i2400m->bm_cmd_buf, _cmd, cmd_size); /* Prep command */
  106. cmd = i2400m->bm_cmd_buf;
  107. if (cmd_size_a > cmd_size) /* Zero pad space */
  108. memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
  109. if ((flags & I2400M_BM_CMD_RAW) == 0) {
  110. if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
  111. dev_warn(dev, "SW BUG: response_required == 0\n");
  112. i2400m_bm_cmd_prepare(cmd);
  113. }
  114. d_printf(4, dev, "BM cmd %d: %zu bytes (%zu padded)\n",
  115. opcode, cmd_size, cmd_size_a);
  116. d_dump(5, dev, cmd, cmd_size);
  117. sdio_claim_host(i2400ms->func); /* Send & check */
  118. result = sdio_memcpy_toio(i2400ms->func, I2400MS_DATA_ADDR,
  119. i2400m->bm_cmd_buf, cmd_size_a);
  120. sdio_release_host(i2400ms->func);
  121. if (result < 0) {
  122. dev_err(dev, "BM cmd %d: cannot send: %ld\n",
  123. opcode, (long) result);
  124. goto error_cmd_send;
  125. }
  126. result = cmd_size;
  127. error_cmd_send:
  128. error_too_big:
  129. d_fnend(5, dev, "(i2400m %p cmd %p size %zu) = %d\n",
  130. i2400m, _cmd, cmd_size, (int) result);
  131. return result;
  132. }
  133. /*
  134. * Read an ack from the device's boot-mode (polling)
  135. *
  136. * @i2400m:
  137. * @_ack: pointer to where to store the read data
  138. * @ack_size: how many bytes we should read
  139. *
  140. * Returns: < 0 errno code on error; otherwise, amount of received bytes.
  141. *
  142. * The ACK for a BM command is always at least sizeof(*ack) bytes, so
  143. * check for that. We don't need to check for device reboots
  144. *
  145. * NOTE: We do an artificial timeout of 1 sec over the SDIO timeout;
  146. * this way we have control over it...there is no way that I know
  147. * of setting an SDIO transaction timeout.
  148. */
  149. ssize_t i2400ms_bus_bm_wait_for_ack(struct i2400m *i2400m,
  150. struct i2400m_bootrom_header *ack,
  151. size_t ack_size)
  152. {
  153. int result;
  154. ssize_t rx_size;
  155. u64 timeout;
  156. struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
  157. struct sdio_func *func = i2400ms->func;
  158. struct device *dev = &func->dev;
  159. BUG_ON(sizeof(*ack) > ack_size);
  160. d_fnstart(5, dev, "(i2400m %p ack %p size %zu)\n",
  161. i2400m, ack, ack_size);
  162. timeout = get_jiffies_64() + 2 * HZ;
  163. sdio_claim_host(func);
  164. while (1) {
  165. if (time_after64(get_jiffies_64(), timeout)) {
  166. rx_size = -ETIMEDOUT;
  167. dev_err(dev, "timeout waiting for ack data\n");
  168. goto error_timedout;
  169. }
  170. /* Find the RX size, check if it fits or not -- it if
  171. * doesn't fit, fail, as we have no way to dispose of
  172. * the extra data. */
  173. rx_size = __i2400ms_rx_get_size(i2400ms);
  174. if (rx_size < 0)
  175. goto error_rx_get_size;
  176. result = -ENOSPC; /* Check it fits */
  177. if (rx_size < sizeof(*ack)) {
  178. rx_size = -EIO;
  179. dev_err(dev, "HW BUG? received is too small (%zu vs "
  180. "%zu needed)\n", sizeof(*ack), rx_size);
  181. goto error_too_small;
  182. }
  183. if (rx_size > I2400M_BM_ACK_BUF_SIZE) {
  184. dev_err(dev, "SW BUG? BM_ACK_BUF is too small (%u vs "
  185. "%zu needed)\n", I2400M_BM_ACK_BUF_SIZE,
  186. rx_size);
  187. goto error_too_small;
  188. }
  189. /* Read it */
  190. result = sdio_memcpy_fromio(func, i2400m->bm_ack_buf,
  191. I2400MS_DATA_ADDR, rx_size);
  192. if (result == -ETIMEDOUT || result == -ETIME)
  193. continue;
  194. if (result < 0) {
  195. dev_err(dev, "BM SDIO receive (%zu B) failed: %d\n",
  196. rx_size, result);
  197. goto error_read;
  198. } else
  199. break;
  200. }
  201. rx_size = min((ssize_t)ack_size, rx_size);
  202. memcpy(ack, i2400m->bm_ack_buf, rx_size);
  203. error_read:
  204. error_too_small:
  205. error_rx_get_size:
  206. error_timedout:
  207. sdio_release_host(func);
  208. d_fnend(5, dev, "(i2400m %p ack %p size %zu) = %ld\n",
  209. i2400m, ack, ack_size, (long) rx_size);
  210. return rx_size;
  211. }