blob: 1c9046914bd17aadfd225a0ad9c5c0078f52082d [file] [log] [blame]
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -08001/*
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
69#define D_SUBMODULE rx
70#include "sdio-debug-levels.h"
71
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -070072static const __le32 i2400m_ACK_BARKER[4] = {
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 __constant_cpu_to_le32(I2400M_ACK_BARKER)
77};
78
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -080079
80/*
81 * Read and return the amount of bytes available for RX
82 *
83 * The RX size has to be read like this: byte reads of three
84 * sequential locations; then glue'em together.
85 *
86 * sdio_readl() doesn't work.
87 */
88ssize_t __i2400ms_rx_get_size(struct i2400ms *i2400ms)
89{
90 int ret, cnt, val;
91 ssize_t rx_size;
92 unsigned xfer_size_addr;
93 struct sdio_func *func = i2400ms->func;
94 struct device *dev = &i2400ms->func->dev;
95
96 d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
97 xfer_size_addr = I2400MS_INTR_GET_SIZE_ADDR;
98 rx_size = 0;
99 for (cnt = 0; cnt < 3; cnt++) {
100 val = sdio_readb(func, xfer_size_addr + cnt, &ret);
101 if (ret < 0) {
102 dev_err(dev, "RX: Can't read byte %d of RX size from "
103 "0x%08x: %d\n", cnt, xfer_size_addr + cnt, ret);
104 rx_size = ret;
105 goto error_read;
106 }
107 rx_size = rx_size << 8 | (val & 0xff);
108 }
109 d_printf(6, dev, "RX: rx_size is %ld\n", (long) rx_size);
110error_read:
111 d_fnend(7, dev, "(i2400ms %p) = %ld\n", i2400ms, (long) rx_size);
112 return rx_size;
113}
114
115
116/*
117 * Read data from the device (when in normal)
118 *
119 * Allocate an SKB of the right size, read the data in and then
120 * deliver it to the generic layer.
121 *
122 * We also check for a reboot barker. That means the device died and
123 * we have to reboot it.
124 */
125static
126void i2400ms_rx(struct i2400ms *i2400ms)
127{
128 int ret;
129 struct sdio_func *func = i2400ms->func;
130 struct device *dev = &func->dev;
131 struct i2400m *i2400m = &i2400ms->i2400m;
132 struct sk_buff *skb;
133 ssize_t rx_size;
134
135 d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
136 rx_size = __i2400ms_rx_get_size(i2400ms);
137 if (rx_size < 0) {
138 ret = rx_size;
139 goto error_get_size;
140 }
Cindy H Kao339ccc32009-08-10 18:36:15 -0700141 /*
142 * Hardware quirk: make sure to clear the INTR status register
143 * AFTER getting the data transfer size.
144 */
145 sdio_writeb(func, 1, I2400MS_INTR_CLEAR_ADDR, &ret);
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700146
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800147 ret = -ENOMEM;
148 skb = alloc_skb(rx_size, GFP_ATOMIC);
149 if (NULL == skb) {
150 dev_err(dev, "RX: unable to alloc skb\n");
151 goto error_alloc_skb;
152 }
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800153 ret = sdio_memcpy_fromio(func, skb->data,
154 I2400MS_DATA_ADDR, rx_size);
155 if (ret < 0) {
156 dev_err(dev, "RX: SDIO data read failed: %d\n", ret);
157 goto error_memcpy_fromio;
158 }
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700159
160 rmb(); /* make sure we get boot_mode from dev_reset_handle */
161 if (i2400m->boot_mode == 1) {
162 spin_lock(&i2400m->rx_lock);
163 i2400ms->bm_ack_size = rx_size;
164 spin_unlock(&i2400m->rx_lock);
165 memcpy(i2400m->bm_ack_buf, skb->data, rx_size);
166 wake_up(&i2400ms->bm_wfa_wq);
167 dev_err(dev, "RX: SDIO boot mode message\n");
168 kfree_skb(skb);
169 } else if (unlikely(!memcmp(skb->data, i2400m_NBOOT_BARKER,
170 sizeof(i2400m_NBOOT_BARKER))
171 || !memcmp(skb->data, i2400m_SBOOT_BARKER,
172 sizeof(i2400m_SBOOT_BARKER)))) {
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800173 ret = i2400m_dev_reset_handle(i2400m);
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700174 dev_err(dev, "RX: SDIO reboot barker\n");
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800175 kfree_skb(skb);
176 } else {
177 skb_put(skb, rx_size);
178 i2400m_rx(i2400m, skb);
179 }
180 d_fnend(7, dev, "(i2400ms %p) = void\n", i2400ms);
181 return;
182
183error_memcpy_fromio:
184 kfree_skb(skb);
185error_alloc_skb:
186error_get_size:
187 d_fnend(7, dev, "(i2400ms %p) = %d\n", i2400ms, ret);
188 return;
189}
190
191
192/*
193 * Process an interrupt from the SDIO card
194 *
195 * FIXME: need to process other events that are not just ready-to-read
196 *
197 * Checks there is data ready and then proceeds to read it.
198 */
199static
200void i2400ms_irq(struct sdio_func *func)
201{
202 int ret;
203 struct i2400ms *i2400ms = sdio_get_drvdata(func);
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800204 struct device *dev = &func->dev;
205 int val;
206
207 d_fnstart(6, dev, "(i2400ms %p)\n", i2400ms);
208 val = sdio_readb(func, I2400MS_INTR_STATUS_ADDR, &ret);
209 if (ret < 0) {
210 dev_err(dev, "RX: Can't read interrupt status: %d\n", ret);
211 goto error_no_irq;
212 }
213 if (!val) {
214 dev_err(dev, "RX: BUG? got IRQ but no interrupt ready?\n");
215 goto error_no_irq;
216 }
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700217 i2400ms_rx(i2400ms);
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800218error_no_irq:
219 d_fnend(6, dev, "(i2400ms %p) = void\n", i2400ms);
220 return;
221}
222
223
224/*
225 * Setup SDIO RX
226 *
227 * Hooks up the IRQ handler and then enables IRQs.
228 */
229int i2400ms_rx_setup(struct i2400ms *i2400ms)
230{
231 int result;
232 struct sdio_func *func = i2400ms->func;
233 struct device *dev = &func->dev;
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700234 struct i2400m *i2400m = &i2400ms->i2400m;
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800235
236 d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700237
238 init_waitqueue_head(&i2400ms->bm_wfa_wq);
239 spin_lock(&i2400m->rx_lock);
240 i2400ms->bm_wait_result = -EINPROGRESS;
Cindy H Kao5b45bfe2009-08-27 15:25:12 -0700241 /*
242 * Before we are about to enable the RX interrupt, make sure
243 * bm_ack_size is cleared to -EINPROGRESS which indicates
244 * no RX interrupt happened yet or the previous interrupt
245 * has been handled, we are ready to take the new interrupt
246 */
247 i2400ms->bm_ack_size = -EINPROGRESS;
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700248 spin_unlock(&i2400m->rx_lock);
249
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800250 sdio_claim_host(func);
251 result = sdio_claim_irq(func, i2400ms_irq);
252 if (result < 0) {
253 dev_err(dev, "Cannot claim IRQ: %d\n", result);
254 goto error_irq_claim;
255 }
256 result = 0;
257 sdio_writeb(func, 1, I2400MS_INTR_ENABLE_ADDR, &result);
258 if (result < 0) {
259 sdio_release_irq(func);
260 dev_err(dev, "Failed to enable interrupts %d\n", result);
261 }
262error_irq_claim:
263 sdio_release_host(func);
264 d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
265 return result;
266}
267
268
269/*
270 * Tear down SDIO RX
271 *
272 * Disables IRQs in the device and removes the IRQ handler.
273 */
274void i2400ms_rx_release(struct i2400ms *i2400ms)
275{
276 int result;
277 struct sdio_func *func = i2400ms->func;
278 struct device *dev = &func->dev;
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700279 struct i2400m *i2400m = &i2400ms->i2400m;
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800280
281 d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
Inaky Perez-Gonzalez16820c12009-05-07 01:02:39 -0700282 spin_lock(&i2400m->rx_lock);
283 i2400ms->bm_ack_size = -EINTR;
284 spin_unlock(&i2400m->rx_lock);
285 wake_up_all(&i2400ms->bm_wfa_wq);
Inaky Perez-Gonzalez514ec712008-12-20 16:57:57 -0800286 sdio_claim_host(func);
287 sdio_writeb(func, 0, I2400MS_INTR_ENABLE_ADDR, &result);
288 sdio_release_irq(func);
289 sdio_release_host(func);
290 d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
291}