]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1251_spi.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1251_spi.c
CommitLineData
2f01a1f5 1/*
80301cdc 2 * This file is part of wl1251
2f01a1f5
KV
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
2f01a1f5
KV
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
8e639c06 22#include <linux/irq.h>
2f01a1f5 23#include <linux/module.h>
5a0e3ad6 24#include <linux/slab.h>
2f01a1f5
KV
25#include <linux/crc7.h>
26#include <linux/spi/spi.h>
c1f9a095 27#include <linux/wl12xx.h>
2f01a1f5 28
13674118 29#include "wl1251.h"
29d904c4 30#include "wl1251_reg.h"
ef2f8d45 31#include "wl1251_spi.h"
2f01a1f5 32
b5ed9c1b
BC
33static irqreturn_t wl1251_irq(int irq, void *cookie)
34{
35 struct wl1251 *wl;
36
37 wl1251_debug(DEBUG_IRQ, "IRQ");
38
39 wl = cookie;
40
16e711f9 41 ieee80211_queue_work(wl->hw, &wl->irq_work);
b5ed9c1b
BC
42
43 return IRQ_HANDLED;
44}
45
af8c78eb
BC
46static struct spi_device *wl_to_spi(struct wl1251 *wl)
47{
48 return wl->if_priv;
49}
50
08d9f572 51static void wl1251_spi_reset(struct wl1251 *wl)
2f01a1f5
KV
52{
53 u8 *cmd;
54 struct spi_transfer t;
55 struct spi_message m;
56
57 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
58 if (!cmd) {
80301cdc 59 wl1251_error("could not allocate cmd for spi reset");
2f01a1f5
KV
60 return;
61 }
62
63 memset(&t, 0, sizeof(t));
64 spi_message_init(&m);
65
66 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
67
68 t.tx_buf = cmd;
69 t.len = WSPI_INIT_CMD_LEN;
70 spi_message_add_tail(&t, &m);
71
af8c78eb 72 spi_sync(wl_to_spi(wl), &m);
2f01a1f5 73
80301cdc 74 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
2f01a1f5
KV
75}
76
8e639c06 77static void wl1251_spi_wake(struct wl1251 *wl)
2f01a1f5
KV
78{
79 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
80 struct spi_transfer t;
81 struct spi_message m;
82
83 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
84 if (!cmd) {
80301cdc 85 wl1251_error("could not allocate cmd for spi init");
2f01a1f5
KV
86 return;
87 }
88
89 memset(crc, 0, sizeof(crc));
90 memset(&t, 0, sizeof(t));
91 spi_message_init(&m);
92
93 /*
94 * Set WSPI_INIT_COMMAND
95 * the data is being send from the MSB to LSB
96 */
97 cmd[2] = 0xff;
98 cmd[3] = 0xff;
99 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
100 cmd[0] = 0;
101 cmd[7] = 0;
102 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
103 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
104
105 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
106 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
107 else
108 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
109
110 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
111 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
112
113 crc[0] = cmd[1];
114 crc[1] = cmd[0];
115 crc[2] = cmd[7];
116 crc[3] = cmd[6];
117 crc[4] = cmd[5];
118
119 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
120 cmd[4] |= WSPI_INIT_CMD_END;
121
122 t.tx_buf = cmd;
123 t.len = WSPI_INIT_CMD_LEN;
124 spi_message_add_tail(&t, &m);
125
af8c78eb 126 spi_sync(wl_to_spi(wl), &m);
2f01a1f5 127
80301cdc 128 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
2f01a1f5
KV
129}
130
08d9f572
BC
131static void wl1251_spi_reset_wake(struct wl1251 *wl)
132{
133 wl1251_spi_reset(wl);
8e639c06 134 wl1251_spi_wake(wl);
08d9f572
BC
135}
136
08d9f572
BC
137static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
138 size_t len)
2f01a1f5
KV
139{
140 struct spi_transfer t[3];
141 struct spi_message m;
5262c12d 142 u8 *busy_buf;
56343a3c 143 u32 *cmd;
2f01a1f5 144
56343a3c 145 cmd = &wl->buffer_cmd;
5262c12d 146 busy_buf = wl->buffer_busyword;
56343a3c
KV
147
148 *cmd = 0;
149 *cmd |= WSPI_CMD_READ;
150 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
151 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
2f01a1f5
KV
152
153 spi_message_init(&m);
154 memset(t, 0, sizeof(t));
155
56343a3c 156 t[0].tx_buf = cmd;
2f01a1f5
KV
157 t[0].len = 4;
158 spi_message_add_tail(&t[0], &m);
159
160 /* Busy and non busy words read */
161 t[1].rx_buf = busy_buf;
80301cdc 162 t[1].len = WL1251_BUSY_WORD_LEN;
2f01a1f5
KV
163 spi_message_add_tail(&t[1], &m);
164
165 t[2].rx_buf = buf;
166 t[2].len = len;
167 spi_message_add_tail(&t[2], &m);
168
af8c78eb 169 spi_sync(wl_to_spi(wl), &m);
2f01a1f5
KV
170
171 /* FIXME: check busy words */
172
80301cdc
KV
173 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
174 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
2f01a1f5
KV
175}
176
08d9f572
BC
177static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
178 size_t len)
2f01a1f5
KV
179{
180 struct spi_transfer t[2];
181 struct spi_message m;
56343a3c 182 u32 *cmd;
2f01a1f5 183
56343a3c
KV
184 cmd = &wl->buffer_cmd;
185
186 *cmd = 0;
187 *cmd |= WSPI_CMD_WRITE;
188 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
189 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
2f01a1f5
KV
190
191 spi_message_init(&m);
192 memset(t, 0, sizeof(t));
193
56343a3c
KV
194 t[0].tx_buf = cmd;
195 t[0].len = sizeof(*cmd);
2f01a1f5
KV
196 spi_message_add_tail(&t[0], &m);
197
198 t[1].tx_buf = buf;
199 t[1].len = len;
200 spi_message_add_tail(&t[1], &m);
201
af8c78eb 202 spi_sync(wl_to_spi(wl), &m);
2f01a1f5 203
80301cdc
KV
204 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
205 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
2f01a1f5 206}
08d9f572 207
b5ed9c1b
BC
208static void wl1251_spi_enable_irq(struct wl1251 *wl)
209{
210 return enable_irq(wl->irq);
211}
212
213static void wl1251_spi_disable_irq(struct wl1251 *wl)
214{
215 return disable_irq(wl->irq);
216}
217
af8c78eb 218static const struct wl1251_if_operations wl1251_spi_ops = {
08d9f572
BC
219 .read = wl1251_spi_read,
220 .write = wl1251_spi_write,
221 .reset = wl1251_spi_reset_wake,
b5ed9c1b
BC
222 .enable_irq = wl1251_spi_enable_irq,
223 .disable_irq = wl1251_spi_disable_irq,
08d9f572 224};
8e639c06
BC
225
226static int __devinit wl1251_spi_probe(struct spi_device *spi)
227{
228 struct wl12xx_platform_data *pdata;
229 struct ieee80211_hw *hw;
230 struct wl1251 *wl;
231 int ret;
232
233 pdata = spi->dev.platform_data;
234 if (!pdata) {
235 wl1251_error("no platform data");
236 return -ENODEV;
237 }
238
239 hw = wl1251_alloc_hw();
240 if (IS_ERR(hw))
241 return PTR_ERR(hw);
242
243 wl = hw->priv;
244
245 SET_IEEE80211_DEV(hw, &spi->dev);
246 dev_set_drvdata(&spi->dev, wl);
af8c78eb 247 wl->if_priv = spi;
8e639c06
BC
248 wl->if_ops = &wl1251_spi_ops;
249
250 /* This is the only SPI value that we need to set here, the rest
251 * comes from the board-peripherals file */
252 spi->bits_per_word = 32;
253
254 ret = spi_setup(spi);
255 if (ret < 0) {
256 wl1251_error("spi_setup failed");
257 goto out_free;
258 }
259
260 wl->set_power = pdata->set_power;
261 if (!wl->set_power) {
262 wl1251_error("set power function missing in platform data");
263 return -ENODEV;
264 }
265
266 wl->irq = spi->irq;
267 if (wl->irq < 0) {
268 wl1251_error("irq missing in platform data");
269 return -ENODEV;
270 }
271
c95cf3d0
DJW
272 wl->use_eeprom = pdata->use_eeprom;
273
8e639c06
BC
274 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
275 if (ret < 0) {
276 wl1251_error("request_irq() failed: %d", ret);
277 goto out_free;
278 }
279
280 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
281
282 disable_irq(wl->irq);
283
284 ret = wl1251_init_ieee80211(wl);
285 if (ret)
286 goto out_irq;
287
288 return 0;
289
290 out_irq:
291 free_irq(wl->irq, wl);
292
293 out_free:
294 ieee80211_free_hw(hw);
295
296 return ret;
297}
298
299static int __devexit wl1251_spi_remove(struct spi_device *spi)
300{
301 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
302
b5ed9c1b 303 free_irq(wl->irq, wl);
8e639c06
BC
304 wl1251_free_hw(wl);
305
306 return 0;
307}
308
309static struct spi_driver wl1251_spi_driver = {
310 .driver = {
7590a550 311 .name = DRIVER_NAME,
8e639c06
BC
312 .bus = &spi_bus_type,
313 .owner = THIS_MODULE,
314 },
315
316 .probe = wl1251_spi_probe,
317 .remove = __devexit_p(wl1251_spi_remove),
318};
319
320static int __init wl1251_spi_init(void)
321{
322 int ret;
323
324 ret = spi_register_driver(&wl1251_spi_driver);
325 if (ret < 0) {
326 wl1251_error("failed to register spi driver: %d", ret);
327 goto out;
328 }
329
330out:
331 return ret;
332}
333
334static void __exit wl1251_spi_exit(void)
335{
336 spi_unregister_driver(&wl1251_spi_driver);
337
338 wl1251_notice("unloaded");
339}
340
341module_init(wl1251_spi_init);
342module_exit(wl1251_spi_exit);
343
344MODULE_LICENSE("GPL");
31c726f0 345MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
f148cfdd 346MODULE_ALIAS("spi:wl1251");