sdio-fw.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * Dirk Brandewie <dirk.j.brandewie@intel.com>
  50. * - Make it IRQ based, not polling
  51. *
  52. * THE PROCEDURE
  53. *
  54. * See fw.c for the generic description of this procedure.
  55. *
  56. * This file implements only the SDIO specifics. It boils down to how
  57. * to send a command and waiting for an acknowledgement from the
  58. * device.
  59. *
  60. * All this code is sequential -- all i2400ms_bus_bm_*() functions are
  61. * executed in the same thread, except i2400ms_bm_irq() [on its own by
  62. * the SDIO driver]. This makes it possible to avoid locking.
  63. *
  64. * COMMAND EXECUTION
  65. *
  66. * The generic firmware upload code will call i2400m_bus_bm_cmd_send()
  67. * to send commands.
  68. *
  69. * The SDIO devices expects things in 256 byte blocks, so it will pad
  70. * it, compute the checksum (if needed) and pass it to SDIO.
  71. *
  72. * ACK RECEPTION
  73. *
  74. * This works in IRQ mode -- the fw loader says when to wait for data
  75. * and for that it calls i2400ms_bus_bm_wait_for_ack().
  76. *
  77. * This checks if there is any data available (RX size > 0); if not,
  78. * waits for the IRQ handler to notify about it. Once there is data,
  79. * it is read and passed to the caller. Doing it this way we don't
  80. * need much coordination/locking, and it makes it much more difficult
  81. * for an interrupt to be lost and the wait_for_ack() function getting
  82. * stuck even when data is pending.
  83. */
  84. #include <linux/mmc/sdio_func.h>
  85. #include "i2400m-sdio.h"
  86. #define D_SUBMODULE fw
  87. #include "sdio-debug-levels.h"
  88. /*
  89. * Send a boot-mode command to the SDIO function
  90. *
  91. * We use a bounce buffer (i2400m->bm_cmd_buf) because we need to
  92. * touch the header if the RAW flag is not set.
  93. *
  94. * @flags: pass thru from i2400m_bm_cmd()
  95. * @return: cmd_size if ok, < 0 errno code on error.
  96. *
  97. * Note the command is padded to the SDIO block size for the device.
  98. */
  99. ssize_t i2400ms_bus_bm_cmd_send(struct i2400m *i2400m,
  100. const struct i2400m_bootrom_header *_cmd,
  101. size_t cmd_size, int flags)
  102. {
  103. ssize_t result;
  104. struct device *dev = i2400m_dev(i2400m);
  105. struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
  106. int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
  107. struct i2400m_bootrom_header *cmd;
  108. /* SDIO restriction */
  109. size_t cmd_size_a = ALIGN(cmd_size, I2400MS_BLK_SIZE);
  110. d_fnstart(5, dev, "(i2400m %p cmd %p size %zu)\n",
  111. i2400m, _cmd, cmd_size);
  112. result = -E2BIG;
  113. if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
  114. goto error_too_big;
  115. if (_cmd != i2400m->bm_cmd_buf)
  116. memmove(i2400m->bm_cmd_buf, _cmd, cmd_size);
  117. cmd = i2400m->bm_cmd_buf;
  118. if (cmd_size_a > cmd_size) /* Zero pad space */
  119. memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
  120. if ((flags & I2400M_BM_CMD_RAW) == 0) {
  121. if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
  122. dev_warn(dev, "SW BUG: response_required == 0\n");
  123. i2400m_bm_cmd_prepare(cmd);
  124. }
  125. d_printf(4, dev, "BM cmd %d: %zu bytes (%zu padded)\n",
  126. opcode, cmd_size, cmd_size_a);
  127. d_dump(5, dev, cmd, cmd_size);
  128. sdio_claim_host(i2400ms->func); /* Send & check */
  129. result = sdio_memcpy_toio(i2400ms->func, I2400MS_DATA_ADDR,
  130. i2400m->bm_cmd_buf, cmd_size_a);
  131. sdio_release_host(i2400ms->func);
  132. if (result < 0) {
  133. dev_err(dev, "BM cmd %d: cannot send: %ld\n",
  134. opcode, (long) result);
  135. goto error_cmd_send;
  136. }
  137. result = cmd_size;
  138. error_cmd_send:
  139. error_too_big:
  140. d_fnend(5, dev, "(i2400m %p cmd %p size %zu) = %d\n",
  141. i2400m, _cmd, cmd_size, (int) result);
  142. return result;
  143. }
  144. /*
  145. * Read an ack from the device's boot-mode
  146. *
  147. * @i2400m:
  148. * @_ack: pointer to where to store the read data
  149. * @ack_size: how many bytes we should read
  150. *
  151. * Returns: < 0 errno code on error; otherwise, amount of received bytes.
  152. *
  153. * The ACK for a BM command is always at least sizeof(*ack) bytes, so
  154. * check for that. We don't need to check for device reboots
  155. *
  156. */
  157. ssize_t i2400ms_bus_bm_wait_for_ack(struct i2400m *i2400m,
  158. struct i2400m_bootrom_header *ack,
  159. size_t ack_size)
  160. {
  161. ssize_t result;
  162. struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
  163. struct sdio_func *func = i2400ms->func;
  164. struct device *dev = &func->dev;
  165. int size;
  166. BUG_ON(sizeof(*ack) > ack_size);
  167. d_fnstart(5, dev, "(i2400m %p ack %p size %zu)\n",
  168. i2400m, ack, ack_size);
  169. result = wait_event_timeout(i2400ms->bm_wfa_wq,
  170. i2400ms->bm_ack_size != -EINPROGRESS,
  171. 2 * HZ);
  172. if (result == 0) {
  173. result = -ETIMEDOUT;
  174. dev_err(dev, "BM: error waiting for an ack\n");
  175. goto error_timeout;
  176. }
  177. spin_lock(&i2400m->rx_lock);
  178. result = i2400ms->bm_ack_size;
  179. BUG_ON(result == -EINPROGRESS);
  180. if (result < 0) /* so we exit when rx_release() is called */
  181. dev_err(dev, "BM: %s failed: %zd\n", __func__, result);
  182. else {
  183. size = min(ack_size, i2400ms->bm_ack_size);
  184. memcpy(ack, i2400m->bm_ack_buf, size);
  185. }
  186. /*
  187. * Remember always to clear the bm_ack_size to -EINPROGRESS
  188. * after the RX data is processed
  189. */
  190. i2400ms->bm_ack_size = -EINPROGRESS;
  191. spin_unlock(&i2400m->rx_lock);
  192. error_timeout:
  193. d_fnend(5, dev, "(i2400m %p ack %p size %zu) = %zd\n",
  194. i2400m, ack, ack_size, result);
  195. return result;
  196. }