Merge commit 'origin/master' into next
[pandora-kernel.git] / drivers / net / wimax / i2400m / sdio-fw.c
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
87
88 #define D_SUBMODULE fw
89 #include "sdio-debug-levels.h"
90
91
92 /*
93  * Send a boot-mode command to the SDIO function
94  *
95  * We use a bounce buffer (i2400m->bm_cmd_buf) because we need to
96  * touch the header if the RAW flag is not set.
97  *
98  * @flags: pass thru from i2400m_bm_cmd()
99  * @return: cmd_size if ok, < 0 errno code on error.
100  *
101  * Note the command is padded to the SDIO block size for the device.
102  */
103 ssize_t i2400ms_bus_bm_cmd_send(struct i2400m *i2400m,
104                                 const struct i2400m_bootrom_header *_cmd,
105                                 size_t cmd_size, int flags)
106 {
107         ssize_t result;
108         struct device *dev = i2400m_dev(i2400m);
109         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
110         int opcode = _cmd == NULL ? -1 : i2400m_brh_get_opcode(_cmd);
111         struct i2400m_bootrom_header *cmd;
112         /* SDIO restriction */
113         size_t cmd_size_a = ALIGN(cmd_size, I2400MS_BLK_SIZE);
114
115         d_fnstart(5, dev, "(i2400m %p cmd %p size %zu)\n",
116                   i2400m, _cmd, cmd_size);
117         result = -E2BIG;
118         if (cmd_size > I2400M_BM_CMD_BUF_SIZE)
119                 goto error_too_big;
120
121         memcpy(i2400m->bm_cmd_buf, _cmd, cmd_size);     /* Prep command */
122         cmd = i2400m->bm_cmd_buf;
123         if (cmd_size_a > cmd_size)                      /* Zero pad space */
124                 memset(i2400m->bm_cmd_buf + cmd_size, 0, cmd_size_a - cmd_size);
125         if ((flags & I2400M_BM_CMD_RAW) == 0) {
126                 if (WARN_ON(i2400m_brh_get_response_required(cmd) == 0))
127                         dev_warn(dev, "SW BUG: response_required == 0\n");
128                 i2400m_bm_cmd_prepare(cmd);
129         }
130         d_printf(4, dev, "BM cmd %d: %zu bytes (%zu padded)\n",
131                  opcode, cmd_size, cmd_size_a);
132         d_dump(5, dev, cmd, cmd_size);
133
134         sdio_claim_host(i2400ms->func);                 /* Send & check */
135         result = sdio_memcpy_toio(i2400ms->func, I2400MS_DATA_ADDR,
136                                   i2400m->bm_cmd_buf, cmd_size_a);
137         sdio_release_host(i2400ms->func);
138         if (result < 0) {
139                 dev_err(dev, "BM cmd %d: cannot send: %ld\n",
140                         opcode, (long) result);
141                 goto error_cmd_send;
142         }
143         result = cmd_size;
144 error_cmd_send:
145 error_too_big:
146         d_fnend(5, dev, "(i2400m %p cmd %p size %zu) = %d\n",
147                 i2400m, _cmd, cmd_size, (int) result);
148         return result;
149 }
150
151
152 /*
153  * Read an ack from the device's boot-mode
154  *
155  * @i2400m:
156  * @_ack: pointer to where to store the read data
157  * @ack_size: how many bytes we should read
158  *
159  * Returns: < 0 errno code on error; otherwise, amount of received bytes.
160  *
161  * The ACK for a BM command is always at least sizeof(*ack) bytes, so
162  * check for that. We don't need to check for device reboots
163  *
164  */
165 ssize_t i2400ms_bus_bm_wait_for_ack(struct i2400m *i2400m,
166                                     struct i2400m_bootrom_header *ack,
167                                     size_t ack_size)
168 {
169         ssize_t result;
170         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
171         struct sdio_func *func = i2400ms->func;
172         struct device *dev = &func->dev;
173         int size;
174
175         BUG_ON(sizeof(*ack) > ack_size);
176
177         d_fnstart(5, dev, "(i2400m %p ack %p size %zu)\n",
178                   i2400m, ack, ack_size);
179
180         spin_lock(&i2400m->rx_lock);
181         i2400ms->bm_ack_size = -EINPROGRESS;
182         spin_unlock(&i2400m->rx_lock);
183
184         result = wait_event_timeout(i2400ms->bm_wfa_wq,
185                                     i2400ms->bm_ack_size != -EINPROGRESS,
186                                     2 * HZ);
187         if (result == 0) {
188                 result = -ETIMEDOUT;
189                 dev_err(dev, "BM: error waiting for an ack\n");
190                 goto error_timeout;
191         }
192
193         spin_lock(&i2400m->rx_lock);
194         result = i2400ms->bm_ack_size;
195         BUG_ON(result == -EINPROGRESS);
196         if (result < 0)        /* so we exit when rx_release() is called */
197                 dev_err(dev, "BM: %s failed: %zd\n", __func__, result);
198         else {
199                 size = min(ack_size, i2400ms->bm_ack_size);
200                 memcpy(ack, i2400m->bm_ack_buf, size);
201         }
202         i2400ms->bm_ack_size = -EINPROGRESS;
203         spin_unlock(&i2400m->rx_lock);
204
205 error_timeout:
206         d_fnend(5, dev, "(i2400m %p ack %p size %zu) = %zd\n",
207                 i2400m, ack, ack_size, result);
208         return result;
209 }