blob: 156e57dbd2cfa858df969ab3e98c8476f173ee89 [file] [log] [blame]
Christian Lampartercd8d3d32009-01-11 01:18:38 +01001/*
2 * Copyright (C) 2008 Christian Lamparter <chunkeey@web.de>
3 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
4 *
5 * This driver is a port from stlc45xx:
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/interrupt.h>
26#include <linux/firmware.h>
27#include <linux/delay.h>
28#include <linux/irq.h>
29#include <linux/spi/spi.h>
30#include <linux/etherdevice.h>
31#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Christian Lampartercd8d3d32009-01-11 01:18:38 +010033
34#include "p54spi.h"
Christian Lampartercd8d3d32009-01-11 01:18:38 +010035#include "p54.h"
36
Christian Lamparterd8c92102009-06-23 10:39:45 -050037#include "lmac.h"
Christian Lampartercd8d3d32009-01-11 01:18:38 +010038
Christian Lamparterd7065c32010-08-22 00:00:14 +020039#ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
40#include "p54spi_eeprom.h"
41#endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
42
Christian Lampartercd8d3d32009-01-11 01:18:38 +010043MODULE_FIRMWARE("3826.arm");
44MODULE_ALIAS("stlc45xx");
45
Christian Lampartera2116992009-01-16 22:34:15 +010046/*
47 * gpios should be handled in board files and provided via platform data,
48 * but because it's currently impossible for p54spi to have a header file
49 * in include/linux, let's use module paramaters for now
50 */
51
52static int p54spi_gpio_power = 97;
53module_param(p54spi_gpio_power, int, 0444);
54MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
55
56static int p54spi_gpio_irq = 87;
57module_param(p54spi_gpio_irq, int, 0444);
58MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
59
Christian Lampartercd8d3d32009-01-11 01:18:38 +010060static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
61 void *buf, size_t len)
62{
63 struct spi_transfer t[2];
64 struct spi_message m;
65 __le16 addr;
66
67 /* We first push the address */
68 addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
69
70 spi_message_init(&m);
71 memset(t, 0, sizeof(t));
72
73 t[0].tx_buf = &addr;
74 t[0].len = sizeof(addr);
75 spi_message_add_tail(&t[0], &m);
76
77 t[1].rx_buf = buf;
78 t[1].len = len;
79 spi_message_add_tail(&t[1], &m);
80
81 spi_sync(priv->spi, &m);
82}
83
84
85static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
86 const void *buf, size_t len)
87{
88 struct spi_transfer t[3];
89 struct spi_message m;
90 __le16 addr;
91
92 /* We first push the address */
93 addr = cpu_to_le16(address << 8);
94
95 spi_message_init(&m);
96 memset(t, 0, sizeof(t));
97
98 t[0].tx_buf = &addr;
99 t[0].len = sizeof(addr);
100 spi_message_add_tail(&t[0], &m);
101
102 t[1].tx_buf = buf;
Max Filippov69712e92009-05-18 03:02:32 +0400103 t[1].len = len & ~1;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100104 spi_message_add_tail(&t[1], &m);
105
106 if (len % 2) {
107 __le16 last_word;
108 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
109
110 t[2].tx_buf = &last_word;
111 t[2].len = sizeof(last_word);
112 spi_message_add_tail(&t[2], &m);
113 }
114
115 spi_sync(priv->spi, &m);
116}
117
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100118static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
119{
120 __le32 val;
121
122 p54spi_spi_read(priv, addr, &val, sizeof(val));
123
124 return le32_to_cpu(val);
125}
126
127static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
128{
129 p54spi_spi_write(priv, addr, &val, sizeof(val));
130}
131
132static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
133{
134 p54spi_spi_write(priv, addr, &val, sizeof(val));
135}
136
Christian Lampartera7eee062009-07-03 21:01:15 +0200137static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, u32 bits)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100138{
139 int i;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100140
141 for (i = 0; i < 2000; i++) {
Christian Lampartera7eee062009-07-03 21:01:15 +0200142 u32 buffer = p54spi_read32(priv, reg);
Max Filippovf74d0f52009-03-25 08:30:15 +0300143 if ((buffer & bits) == bits)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100144 return 1;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100145 }
146 return 0;
147}
148
Max Filippov4f5cab92009-03-26 06:38:25 +0300149static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base,
150 const void *buf, size_t len)
151{
Christian Lampartera7eee062009-07-03 21:01:15 +0200152 if (!p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL, HOST_ALLOWED)) {
Max Filippov4f5cab92009-03-26 06:38:25 +0300153 dev_err(&priv->spi->dev, "spi_write_dma not allowed "
Max Filippov87cbfd02009-04-06 01:03:09 +0400154 "to DMA write.\n");
Max Filippov4f5cab92009-03-26 06:38:25 +0300155 return -EAGAIN;
156 }
157
Max Filippov210dd1b2009-05-18 03:02:31 +0400158 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
159 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
160
Max Filippov4f5cab92009-03-26 06:38:25 +0300161 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len));
162 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base);
163 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len);
164 return 0;
165}
166
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100167static int p54spi_request_firmware(struct ieee80211_hw *dev)
168{
169 struct p54s_priv *priv = dev->priv;
170 int ret;
171
172 /* FIXME: should driver use it's own struct device? */
173 ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
174
175 if (ret < 0) {
176 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
177 return ret;
178 }
179
180 ret = p54_parse_firmware(dev, priv->firmware);
181 if (ret) {
182 release_firmware(priv->firmware);
183 return ret;
184 }
185
186 return 0;
187}
188
189static int p54spi_request_eeprom(struct ieee80211_hw *dev)
190{
191 struct p54s_priv *priv = dev->priv;
192 const struct firmware *eeprom;
193 int ret;
194
195 /*
196 * allow users to customize their eeprom.
197 */
198
199 ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev);
200 if (ret < 0) {
Christian Lamparterd7065c32010-08-22 00:00:14 +0200201#ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100202 dev_info(&priv->spi->dev, "loading default eeprom...\n");
203 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
204 sizeof(p54spi_eeprom));
Christian Lamparterd7065c32010-08-22 00:00:14 +0200205#endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100206 } else {
207 dev_info(&priv->spi->dev, "loading user eeprom...\n");
208 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
209 (int)eeprom->size);
210 release_firmware(eeprom);
211 }
212 return ret;
213}
214
215static int p54spi_upload_firmware(struct ieee80211_hw *dev)
216{
217 struct p54s_priv *priv = dev->priv;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100218 unsigned long fw_len, _fw_len;
219 unsigned int offset = 0;
220 int err = 0;
221 u8 *fw;
222
223 fw_len = priv->firmware->size;
224 fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
225 if (!fw)
226 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100227
228 /* stop the device */
229 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
230 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
231 SPI_CTRL_STAT_START_HALTED));
232
233 msleep(TARGET_BOOT_SLEEP);
234
235 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
236 SPI_CTRL_STAT_HOST_OVERRIDE |
237 SPI_CTRL_STAT_START_HALTED));
238
239 msleep(TARGET_BOOT_SLEEP);
240
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100241 while (fw_len > 0) {
242 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
243
Max Filippov4f5cab92009-03-26 06:38:25 +0300244 err = p54spi_spi_write_dma(priv, cpu_to_le32(
245 ISL38XX_DEV_FIRMWARE_ADDR + offset),
246 (fw + offset), _fw_len);
247 if (err < 0)
Max Filippov5e3af1d2009-03-25 13:45:01 +0100248 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100249
250 fw_len -= _fw_len;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100251 offset += _fw_len;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100252 }
253
254 BUG_ON(fw_len != 0);
255
256 /* enable host interrupts */
257 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
258 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
259
260 /* boot the device */
261 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
262 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
263 SPI_CTRL_STAT_RAM_BOOT));
264
265 msleep(TARGET_BOOT_SLEEP);
266
267 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
268 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
269 msleep(TARGET_BOOT_SLEEP);
Max Filippov5e3af1d2009-03-25 13:45:01 +0100270
271out:
272 kfree(fw);
273 return err;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100274}
275
276static void p54spi_power_off(struct p54s_priv *priv)
277{
Christian Lampartera2116992009-01-16 22:34:15 +0100278 disable_irq(gpio_to_irq(p54spi_gpio_irq));
279 gpio_set_value(p54spi_gpio_power, 0);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100280}
281
282static void p54spi_power_on(struct p54s_priv *priv)
283{
Christian Lampartera2116992009-01-16 22:34:15 +0100284 gpio_set_value(p54spi_gpio_power, 1);
285 enable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100286
287 /*
288 * need to wait a while before device can be accessed, the lenght
289 * is just a guess
290 */
291 msleep(10);
292}
293
294static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
295{
296 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
297}
298
Max Filippov465b6352009-05-18 03:02:33 +0400299static int p54spi_wakeup(struct p54s_priv *priv)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100300{
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100301 /* wake the chip */
302 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
303 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
304
305 /* And wait for the READY interrupt */
Max Filippov87cbfd02009-04-06 01:03:09 +0400306 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
Christian Lampartera7eee062009-07-03 21:01:15 +0200307 SPI_HOST_INT_READY)) {
Max Filippov87cbfd02009-04-06 01:03:09 +0400308 dev_err(&priv->spi->dev, "INT_READY timeout\n");
Max Filippov465b6352009-05-18 03:02:33 +0400309 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100310 }
311
312 p54spi_int_ack(priv, SPI_HOST_INT_READY);
Max Filippov465b6352009-05-18 03:02:33 +0400313 return 0;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100314}
315
316static inline void p54spi_sleep(struct p54s_priv *priv)
317{
318 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
319 cpu_to_le32(SPI_TARGET_INT_SLEEP));
320}
321
322static void p54spi_int_ready(struct p54s_priv *priv)
323{
324 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
325 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
326
327 switch (priv->fw_state) {
328 case FW_STATE_BOOTING:
329 priv->fw_state = FW_STATE_READY;
330 complete(&priv->fw_comp);
331 break;
332 case FW_STATE_RESETTING:
333 priv->fw_state = FW_STATE_READY;
334 /* TODO: reinitialize state */
335 break;
336 default:
337 break;
338 }
339}
340
341static int p54spi_rx(struct p54s_priv *priv)
342{
343 struct sk_buff *skb;
344 u16 len;
Max Filippovff561ac2009-05-18 03:02:35 +0400345 u16 rx_head[2];
346#define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100347
Max Filippov465b6352009-05-18 03:02:33 +0400348 if (p54spi_wakeup(priv) < 0)
349 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100350
Max Filippovff561ac2009-05-18 03:02:35 +0400351 /* Read data size and first data word in one SPI transaction
352 * This is workaround for firmware/DMA bug,
353 * when first data word gets lost under high load.
354 */
355 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
356 len = rx_head[0];
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100357
358 if (len == 0) {
Max Filippovff561ac2009-05-18 03:02:35 +0400359 p54spi_sleep(priv);
360 dev_err(&priv->spi->dev, "rx request of zero bytes\n");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100361 return 0;
362 }
363
Max Filippov9f201a82009-03-27 07:50:53 +0300364 /* Firmware may insert up to 4 padding bytes after the lmac header,
365 * but it does not amend the size of SPI data transfer.
366 * Such packets has correct data size in header, thus referencing
367 * past the end of allocated skb. Reserve extra 4 bytes for this case */
368 skb = dev_alloc_skb(len + 4);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100369 if (!skb) {
Max Filippovff561ac2009-05-18 03:02:35 +0400370 p54spi_sleep(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100371 dev_err(&priv->spi->dev, "could not alloc skb");
Max Filippovff561ac2009-05-18 03:02:35 +0400372 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100373 }
374
Max Filippovff561ac2009-05-18 03:02:35 +0400375 if (len <= READAHEAD_SZ) {
376 memcpy(skb_put(skb, len), rx_head + 1, len);
377 } else {
378 memcpy(skb_put(skb, READAHEAD_SZ), rx_head + 1, READAHEAD_SZ);
379 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
380 skb_put(skb, len - READAHEAD_SZ),
381 len - READAHEAD_SZ);
382 }
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100383 p54spi_sleep(priv);
Max Filippov9f201a82009-03-27 07:50:53 +0300384 /* Put additional bytes to compensate for the possible
385 * alignment-caused truncation */
386 skb_put(skb, 4);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100387
388 if (p54_rx(priv->hw, skb) == 0)
389 dev_kfree_skb(skb);
390
391 return 0;
392}
393
394
395static irqreturn_t p54spi_interrupt(int irq, void *config)
396{
397 struct spi_device *spi = config;
398 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
399
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400400 ieee80211_queue_work(priv->hw, &priv->work);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100401
402 return IRQ_HANDLED;
403}
404
405static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
406{
407 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100408 int ret = 0;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100409
Max Filippov465b6352009-05-18 03:02:33 +0400410 if (p54spi_wakeup(priv) < 0)
411 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100412
Max Filippov4f5cab92009-03-26 06:38:25 +0300413 ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len);
414 if (ret < 0)
415 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100416
Max Filippov87cbfd02009-04-06 01:03:09 +0400417 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
Christian Lampartera7eee062009-07-03 21:01:15 +0200418 SPI_HOST_INT_WR_READY)) {
Max Filippov87cbfd02009-04-06 01:03:09 +0400419 dev_err(&priv->spi->dev, "WR_READY timeout\n");
Max Filippov6edf5342009-05-18 03:02:34 +0400420 ret = -EAGAIN;
Max Filippov87cbfd02009-04-06 01:03:09 +0400421 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100422 }
423
424 p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100425
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100426 if (FREE_AFTER_TX(skb))
427 p54_free_skb(priv->hw, skb);
Max Filippov4f5cab92009-03-26 06:38:25 +0300428out:
Max Filippov6edf5342009-05-18 03:02:34 +0400429 p54spi_sleep(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100430 return ret;
431}
432
433static int p54spi_wq_tx(struct p54s_priv *priv)
434{
435 struct p54s_tx_info *entry;
436 struct sk_buff *skb;
437 struct ieee80211_tx_info *info;
438 struct p54_tx_info *minfo;
439 struct p54s_tx_info *dinfo;
Christian Lamparter731c6532009-03-30 15:55:24 +0200440 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100441 int ret = 0;
442
Christian Lamparter731c6532009-03-30 15:55:24 +0200443 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100444
445 while (!list_empty(&priv->tx_pending)) {
446 entry = list_entry(priv->tx_pending.next,
447 struct p54s_tx_info, tx_list);
448
449 list_del_init(&entry->tx_list);
450
Christian Lamparter731c6532009-03-30 15:55:24 +0200451 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100452
453 dinfo = container_of((void *) entry, struct p54s_tx_info,
454 tx_list);
455 minfo = container_of((void *) dinfo, struct p54_tx_info,
456 data);
457 info = container_of((void *) minfo, struct ieee80211_tx_info,
458 rate_driver_data);
459 skb = container_of((void *) info, struct sk_buff, cb);
460
461 ret = p54spi_tx_frame(priv, skb);
462
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100463 if (ret < 0) {
464 p54_free_skb(priv->hw, skb);
Christian Lamparter731c6532009-03-30 15:55:24 +0200465 return ret;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100466 }
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100467
Christian Lamparter731c6532009-03-30 15:55:24 +0200468 spin_lock_irqsave(&priv->tx_lock, flags);
469 }
470 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100471 return ret;
472}
473
474static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
475{
476 struct p54s_priv *priv = dev->priv;
477 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
478 struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
479 struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
Christian Lamparter731c6532009-03-30 15:55:24 +0200480 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100481
482 BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
483
Christian Lamparter731c6532009-03-30 15:55:24 +0200484 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100485 list_add_tail(&di->tx_list, &priv->tx_pending);
Christian Lamparter731c6532009-03-30 15:55:24 +0200486 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100487
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400488 ieee80211_queue_work(priv->hw, &priv->work);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100489}
490
491static void p54spi_work(struct work_struct *work)
492{
493 struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
494 u32 ints;
495 int ret;
496
497 mutex_lock(&priv->mutex);
498
Max Filippov4de2dc72009-05-18 21:28:55 +0400499 if (priv->fw_state == FW_STATE_OFF)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100500 goto out;
501
502 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
503
504 if (ints & SPI_HOST_INT_READY) {
505 p54spi_int_ready(priv);
506 p54spi_int_ack(priv, SPI_HOST_INT_READY);
507 }
508
509 if (priv->fw_state != FW_STATE_READY)
510 goto out;
511
512 if (ints & SPI_HOST_INT_UPDATE) {
513 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
514 ret = p54spi_rx(priv);
515 if (ret < 0)
516 goto out;
517 }
518 if (ints & SPI_HOST_INT_SW_UPDATE) {
519 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
520 ret = p54spi_rx(priv);
521 if (ret < 0)
522 goto out;
523 }
524
525 ret = p54spi_wq_tx(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100526out:
527 mutex_unlock(&priv->mutex);
528}
529
530static int p54spi_op_start(struct ieee80211_hw *dev)
531{
532 struct p54s_priv *priv = dev->priv;
533 unsigned long timeout;
534 int ret = 0;
535
536 if (mutex_lock_interruptible(&priv->mutex)) {
537 ret = -EINTR;
538 goto out;
539 }
540
541 priv->fw_state = FW_STATE_BOOTING;
542
543 p54spi_power_on(priv);
544
545 ret = p54spi_upload_firmware(dev);
546 if (ret < 0) {
547 p54spi_power_off(priv);
548 goto out_unlock;
549 }
550
551 mutex_unlock(&priv->mutex);
552
553 timeout = msecs_to_jiffies(2000);
554 timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
555 timeout);
556 if (!timeout) {
557 dev_err(&priv->spi->dev, "firmware boot failed");
558 p54spi_power_off(priv);
559 ret = -1;
560 goto out;
561 }
562
563 if (mutex_lock_interruptible(&priv->mutex)) {
564 ret = -EINTR;
565 p54spi_power_off(priv);
566 goto out;
567 }
568
569 WARN_ON(priv->fw_state != FW_STATE_READY);
570
571out_unlock:
572 mutex_unlock(&priv->mutex);
573
574out:
575 return ret;
576}
577
578static void p54spi_op_stop(struct ieee80211_hw *dev)
579{
580 struct p54s_priv *priv = dev->priv;
Christian Lamparter731c6532009-03-30 15:55:24 +0200581 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100582
583 if (mutex_lock_interruptible(&priv->mutex)) {
584 /* FIXME: how to handle this error? */
585 return;
586 }
587
588 WARN_ON(priv->fw_state != FW_STATE_READY);
589
590 cancel_work_sync(&priv->work);
591
592 p54spi_power_off(priv);
Christian Lamparter731c6532009-03-30 15:55:24 +0200593 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100594 INIT_LIST_HEAD(&priv->tx_pending);
Christian Lamparter731c6532009-03-30 15:55:24 +0200595 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100596
597 priv->fw_state = FW_STATE_OFF;
598 mutex_unlock(&priv->mutex);
599}
600
601static int __devinit p54spi_probe(struct spi_device *spi)
602{
603 struct p54s_priv *priv = NULL;
604 struct ieee80211_hw *hw;
605 int ret = -EINVAL;
606
607 hw = p54_init_common(sizeof(*priv));
608 if (!hw) {
Dan Carpenterbfa99bf2009-07-19 21:26:13 +0200609 dev_err(&spi->dev, "could not alloc ieee80211_hw");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100610 return -ENOMEM;
611 }
612
613 priv = hw->priv;
614 priv->hw = hw;
615 dev_set_drvdata(&spi->dev, priv);
616 priv->spi = spi;
617
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100618 spi->bits_per_word = 16;
619 spi->max_speed_hz = 24000000;
620
621 ret = spi_setup(spi);
622 if (ret < 0) {
623 dev_err(&priv->spi->dev, "spi_setup failed");
624 goto err_free_common;
625 }
626
Christian Lampartera2116992009-01-16 22:34:15 +0100627 ret = gpio_request(p54spi_gpio_power, "p54spi power");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100628 if (ret < 0) {
629 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
630 goto err_free_common;
631 }
632
Christian Lampartera2116992009-01-16 22:34:15 +0100633 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100634 if (ret < 0) {
635 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
636 goto err_free_common;
637 }
638
Christian Lampartera2116992009-01-16 22:34:15 +0100639 gpio_direction_output(p54spi_gpio_power, 0);
640 gpio_direction_input(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100641
Christian Lampartera2116992009-01-16 22:34:15 +0100642 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100643 p54spi_interrupt, IRQF_DISABLED, "p54spi",
644 priv->spi);
645 if (ret < 0) {
646 dev_err(&priv->spi->dev, "request_irq() failed");
647 goto err_free_common;
648 }
649
Christian Lampartera2116992009-01-16 22:34:15 +0100650 set_irq_type(gpio_to_irq(p54spi_gpio_irq),
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100651 IRQ_TYPE_EDGE_RISING);
652
Christian Lampartera2116992009-01-16 22:34:15 +0100653 disable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100654
655 INIT_WORK(&priv->work, p54spi_work);
656 init_completion(&priv->fw_comp);
657 INIT_LIST_HEAD(&priv->tx_pending);
658 mutex_init(&priv->mutex);
659 SET_IEEE80211_DEV(hw, &spi->dev);
660 priv->common.open = p54spi_op_start;
661 priv->common.stop = p54spi_op_stop;
662 priv->common.tx = p54spi_op_tx;
663
664 ret = p54spi_request_firmware(hw);
665 if (ret < 0)
666 goto err_free_common;
667
668 ret = p54spi_request_eeprom(hw);
669 if (ret)
670 goto err_free_common;
671
Christian Lamparter2ac71072009-03-05 21:30:10 +0100672 ret = p54_register_common(hw, &priv->spi->dev);
673 if (ret)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100674 goto err_free_common;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100675
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100676 return 0;
677
678err_free_common:
679 p54_free_common(priv->hw);
680 return ret;
681}
682
683static int __devexit p54spi_remove(struct spi_device *spi)
684{
685 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
686
Christian Lamparterd8c92102009-06-23 10:39:45 -0500687 p54_unregister_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100688
Christian Lampartera2116992009-01-16 22:34:15 +0100689 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100690
Christian Lampartera2116992009-01-16 22:34:15 +0100691 gpio_free(p54spi_gpio_power);
692 gpio_free(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100693 release_firmware(priv->firmware);
694
695 mutex_destroy(&priv->mutex);
696
697 p54_free_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100698
699 return 0;
700}
701
702
703static struct spi_driver p54spi_driver = {
704 .driver = {
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500705 .name = "p54spi",
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100706 .bus = &spi_bus_type,
707 .owner = THIS_MODULE,
708 },
709
710 .probe = p54spi_probe,
711 .remove = __devexit_p(p54spi_remove),
712};
713
714static int __init p54spi_init(void)
715{
716 int ret;
717
718 ret = spi_register_driver(&p54spi_driver);
719 if (ret < 0) {
720 printk(KERN_ERR "failed to register SPI driver: %d", ret);
721 goto out;
722 }
723
724out:
725 return ret;
726}
727
728static void __exit p54spi_exit(void)
729{
730 spi_unregister_driver(&p54spi_driver);
731}
732
733module_init(p54spi_init);
734module_exit(p54spi_exit);
735
736MODULE_LICENSE("GPL");
737MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700738MODULE_ALIAS("spi:cx3110x");
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500739MODULE_ALIAS("spi:p54spi");