blob: 6d9204fef90b2aa902b7fb1f68f86eff4d88cc42 [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));
Michael Büschf4bbf922010-09-05 00:55:38 +0200205#else
206 dev_err(&priv->spi->dev, "Failed to request user eeprom\n");
Christian Lamparterd7065c32010-08-22 00:00:14 +0200207#endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100208 } else {
209 dev_info(&priv->spi->dev, "loading user eeprom...\n");
210 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
211 (int)eeprom->size);
212 release_firmware(eeprom);
213 }
214 return ret;
215}
216
217static int p54spi_upload_firmware(struct ieee80211_hw *dev)
218{
219 struct p54s_priv *priv = dev->priv;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100220 unsigned long fw_len, _fw_len;
221 unsigned int offset = 0;
222 int err = 0;
223 u8 *fw;
224
225 fw_len = priv->firmware->size;
226 fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
227 if (!fw)
228 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100229
230 /* stop the device */
231 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
232 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
233 SPI_CTRL_STAT_START_HALTED));
234
235 msleep(TARGET_BOOT_SLEEP);
236
237 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
238 SPI_CTRL_STAT_HOST_OVERRIDE |
239 SPI_CTRL_STAT_START_HALTED));
240
241 msleep(TARGET_BOOT_SLEEP);
242
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100243 while (fw_len > 0) {
244 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
245
Max Filippov4f5cab92009-03-26 06:38:25 +0300246 err = p54spi_spi_write_dma(priv, cpu_to_le32(
247 ISL38XX_DEV_FIRMWARE_ADDR + offset),
248 (fw + offset), _fw_len);
249 if (err < 0)
Max Filippov5e3af1d2009-03-25 13:45:01 +0100250 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100251
252 fw_len -= _fw_len;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100253 offset += _fw_len;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100254 }
255
256 BUG_ON(fw_len != 0);
257
258 /* enable host interrupts */
259 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
260 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
261
262 /* boot the device */
263 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
264 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
265 SPI_CTRL_STAT_RAM_BOOT));
266
267 msleep(TARGET_BOOT_SLEEP);
268
269 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
270 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
271 msleep(TARGET_BOOT_SLEEP);
Max Filippov5e3af1d2009-03-25 13:45:01 +0100272
273out:
274 kfree(fw);
275 return err;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100276}
277
278static void p54spi_power_off(struct p54s_priv *priv)
279{
Christian Lampartera2116992009-01-16 22:34:15 +0100280 disable_irq(gpio_to_irq(p54spi_gpio_irq));
281 gpio_set_value(p54spi_gpio_power, 0);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100282}
283
284static void p54spi_power_on(struct p54s_priv *priv)
285{
Christian Lampartera2116992009-01-16 22:34:15 +0100286 gpio_set_value(p54spi_gpio_power, 1);
287 enable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100288
289 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300290 * need to wait a while before device can be accessed, the length
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100291 * is just a guess
292 */
293 msleep(10);
294}
295
296static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
297{
298 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
299}
300
Max Filippov465b6352009-05-18 03:02:33 +0400301static int p54spi_wakeup(struct p54s_priv *priv)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100302{
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100303 /* wake the chip */
304 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
305 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
306
307 /* And wait for the READY interrupt */
Max Filippov87cbfd02009-04-06 01:03:09 +0400308 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
Christian Lampartera7eee062009-07-03 21:01:15 +0200309 SPI_HOST_INT_READY)) {
Max Filippov87cbfd02009-04-06 01:03:09 +0400310 dev_err(&priv->spi->dev, "INT_READY timeout\n");
Max Filippov465b6352009-05-18 03:02:33 +0400311 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100312 }
313
314 p54spi_int_ack(priv, SPI_HOST_INT_READY);
Max Filippov465b6352009-05-18 03:02:33 +0400315 return 0;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100316}
317
318static inline void p54spi_sleep(struct p54s_priv *priv)
319{
320 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
321 cpu_to_le32(SPI_TARGET_INT_SLEEP));
322}
323
324static void p54spi_int_ready(struct p54s_priv *priv)
325{
326 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
327 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
328
329 switch (priv->fw_state) {
330 case FW_STATE_BOOTING:
331 priv->fw_state = FW_STATE_READY;
332 complete(&priv->fw_comp);
333 break;
334 case FW_STATE_RESETTING:
335 priv->fw_state = FW_STATE_READY;
336 /* TODO: reinitialize state */
337 break;
338 default:
339 break;
340 }
341}
342
343static int p54spi_rx(struct p54s_priv *priv)
344{
345 struct sk_buff *skb;
346 u16 len;
Max Filippovff561ac2009-05-18 03:02:35 +0400347 u16 rx_head[2];
348#define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100349
Max Filippov465b6352009-05-18 03:02:33 +0400350 if (p54spi_wakeup(priv) < 0)
351 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100352
Max Filippovff561ac2009-05-18 03:02:35 +0400353 /* Read data size and first data word in one SPI transaction
354 * This is workaround for firmware/DMA bug,
355 * when first data word gets lost under high load.
356 */
357 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
358 len = rx_head[0];
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100359
360 if (len == 0) {
Max Filippovff561ac2009-05-18 03:02:35 +0400361 p54spi_sleep(priv);
362 dev_err(&priv->spi->dev, "rx request of zero bytes\n");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100363 return 0;
364 }
365
Max Filippov9f201a82009-03-27 07:50:53 +0300366 /* Firmware may insert up to 4 padding bytes after the lmac header,
367 * but it does not amend the size of SPI data transfer.
368 * Such packets has correct data size in header, thus referencing
369 * past the end of allocated skb. Reserve extra 4 bytes for this case */
370 skb = dev_alloc_skb(len + 4);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100371 if (!skb) {
Max Filippovff561ac2009-05-18 03:02:35 +0400372 p54spi_sleep(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100373 dev_err(&priv->spi->dev, "could not alloc skb");
Max Filippovff561ac2009-05-18 03:02:35 +0400374 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100375 }
376
Max Filippovff561ac2009-05-18 03:02:35 +0400377 if (len <= READAHEAD_SZ) {
378 memcpy(skb_put(skb, len), rx_head + 1, len);
379 } else {
380 memcpy(skb_put(skb, READAHEAD_SZ), rx_head + 1, READAHEAD_SZ);
381 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
382 skb_put(skb, len - READAHEAD_SZ),
383 len - READAHEAD_SZ);
384 }
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100385 p54spi_sleep(priv);
Max Filippov9f201a82009-03-27 07:50:53 +0300386 /* Put additional bytes to compensate for the possible
387 * alignment-caused truncation */
388 skb_put(skb, 4);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100389
390 if (p54_rx(priv->hw, skb) == 0)
391 dev_kfree_skb(skb);
392
393 return 0;
394}
395
396
397static irqreturn_t p54spi_interrupt(int irq, void *config)
398{
399 struct spi_device *spi = config;
400 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
401
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400402 ieee80211_queue_work(priv->hw, &priv->work);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100403
404 return IRQ_HANDLED;
405}
406
407static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
408{
409 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100410 int ret = 0;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100411
Max Filippov465b6352009-05-18 03:02:33 +0400412 if (p54spi_wakeup(priv) < 0)
413 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100414
Max Filippov4f5cab92009-03-26 06:38:25 +0300415 ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len);
416 if (ret < 0)
417 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100418
Max Filippov87cbfd02009-04-06 01:03:09 +0400419 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
Christian Lampartera7eee062009-07-03 21:01:15 +0200420 SPI_HOST_INT_WR_READY)) {
Max Filippov87cbfd02009-04-06 01:03:09 +0400421 dev_err(&priv->spi->dev, "WR_READY timeout\n");
Max Filippov6edf5342009-05-18 03:02:34 +0400422 ret = -EAGAIN;
Max Filippov87cbfd02009-04-06 01:03:09 +0400423 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100424 }
425
426 p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100427
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100428 if (FREE_AFTER_TX(skb))
429 p54_free_skb(priv->hw, skb);
Max Filippov4f5cab92009-03-26 06:38:25 +0300430out:
Max Filippov6edf5342009-05-18 03:02:34 +0400431 p54spi_sleep(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100432 return ret;
433}
434
435static int p54spi_wq_tx(struct p54s_priv *priv)
436{
437 struct p54s_tx_info *entry;
438 struct sk_buff *skb;
439 struct ieee80211_tx_info *info;
440 struct p54_tx_info *minfo;
441 struct p54s_tx_info *dinfo;
Christian Lamparter731c6532009-03-30 15:55:24 +0200442 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100443 int ret = 0;
444
Christian Lamparter731c6532009-03-30 15:55:24 +0200445 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100446
447 while (!list_empty(&priv->tx_pending)) {
448 entry = list_entry(priv->tx_pending.next,
449 struct p54s_tx_info, tx_list);
450
451 list_del_init(&entry->tx_list);
452
Christian Lamparter731c6532009-03-30 15:55:24 +0200453 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100454
455 dinfo = container_of((void *) entry, struct p54s_tx_info,
456 tx_list);
457 minfo = container_of((void *) dinfo, struct p54_tx_info,
458 data);
459 info = container_of((void *) minfo, struct ieee80211_tx_info,
460 rate_driver_data);
461 skb = container_of((void *) info, struct sk_buff, cb);
462
463 ret = p54spi_tx_frame(priv, skb);
464
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100465 if (ret < 0) {
466 p54_free_skb(priv->hw, skb);
Christian Lamparter731c6532009-03-30 15:55:24 +0200467 return ret;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100468 }
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100469
Christian Lamparter731c6532009-03-30 15:55:24 +0200470 spin_lock_irqsave(&priv->tx_lock, flags);
471 }
472 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100473 return ret;
474}
475
476static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
477{
478 struct p54s_priv *priv = dev->priv;
479 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
480 struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
481 struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
Christian Lamparter731c6532009-03-30 15:55:24 +0200482 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100483
484 BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
485
Christian Lamparter731c6532009-03-30 15:55:24 +0200486 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100487 list_add_tail(&di->tx_list, &priv->tx_pending);
Christian Lamparter731c6532009-03-30 15:55:24 +0200488 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100489
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400490 ieee80211_queue_work(priv->hw, &priv->work);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100491}
492
493static void p54spi_work(struct work_struct *work)
494{
495 struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
496 u32 ints;
497 int ret;
498
499 mutex_lock(&priv->mutex);
500
Max Filippov4de2dc72009-05-18 21:28:55 +0400501 if (priv->fw_state == FW_STATE_OFF)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100502 goto out;
503
504 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
505
506 if (ints & SPI_HOST_INT_READY) {
507 p54spi_int_ready(priv);
508 p54spi_int_ack(priv, SPI_HOST_INT_READY);
509 }
510
511 if (priv->fw_state != FW_STATE_READY)
512 goto out;
513
514 if (ints & SPI_HOST_INT_UPDATE) {
515 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
516 ret = p54spi_rx(priv);
517 if (ret < 0)
518 goto out;
519 }
520 if (ints & SPI_HOST_INT_SW_UPDATE) {
521 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
522 ret = p54spi_rx(priv);
523 if (ret < 0)
524 goto out;
525 }
526
527 ret = p54spi_wq_tx(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100528out:
529 mutex_unlock(&priv->mutex);
530}
531
532static int p54spi_op_start(struct ieee80211_hw *dev)
533{
534 struct p54s_priv *priv = dev->priv;
535 unsigned long timeout;
536 int ret = 0;
537
538 if (mutex_lock_interruptible(&priv->mutex)) {
539 ret = -EINTR;
540 goto out;
541 }
542
543 priv->fw_state = FW_STATE_BOOTING;
544
545 p54spi_power_on(priv);
546
547 ret = p54spi_upload_firmware(dev);
548 if (ret < 0) {
549 p54spi_power_off(priv);
550 goto out_unlock;
551 }
552
553 mutex_unlock(&priv->mutex);
554
555 timeout = msecs_to_jiffies(2000);
556 timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
557 timeout);
558 if (!timeout) {
559 dev_err(&priv->spi->dev, "firmware boot failed");
560 p54spi_power_off(priv);
561 ret = -1;
562 goto out;
563 }
564
565 if (mutex_lock_interruptible(&priv->mutex)) {
566 ret = -EINTR;
567 p54spi_power_off(priv);
568 goto out;
569 }
570
571 WARN_ON(priv->fw_state != FW_STATE_READY);
572
573out_unlock:
574 mutex_unlock(&priv->mutex);
575
576out:
577 return ret;
578}
579
580static void p54spi_op_stop(struct ieee80211_hw *dev)
581{
582 struct p54s_priv *priv = dev->priv;
Christian Lamparter731c6532009-03-30 15:55:24 +0200583 unsigned long flags;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100584
585 if (mutex_lock_interruptible(&priv->mutex)) {
586 /* FIXME: how to handle this error? */
587 return;
588 }
589
590 WARN_ON(priv->fw_state != FW_STATE_READY);
591
592 cancel_work_sync(&priv->work);
593
594 p54spi_power_off(priv);
Christian Lamparter731c6532009-03-30 15:55:24 +0200595 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100596 INIT_LIST_HEAD(&priv->tx_pending);
Christian Lamparter731c6532009-03-30 15:55:24 +0200597 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100598
599 priv->fw_state = FW_STATE_OFF;
600 mutex_unlock(&priv->mutex);
601}
602
603static int __devinit p54spi_probe(struct spi_device *spi)
604{
605 struct p54s_priv *priv = NULL;
606 struct ieee80211_hw *hw;
607 int ret = -EINVAL;
608
609 hw = p54_init_common(sizeof(*priv));
610 if (!hw) {
Dan Carpenterbfa99bf2009-07-19 21:26:13 +0200611 dev_err(&spi->dev, "could not alloc ieee80211_hw");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100612 return -ENOMEM;
613 }
614
615 priv = hw->priv;
616 priv->hw = hw;
617 dev_set_drvdata(&spi->dev, priv);
618 priv->spi = spi;
619
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100620 spi->bits_per_word = 16;
621 spi->max_speed_hz = 24000000;
622
623 ret = spi_setup(spi);
624 if (ret < 0) {
625 dev_err(&priv->spi->dev, "spi_setup failed");
626 goto err_free_common;
627 }
628
Christian Lampartera2116992009-01-16 22:34:15 +0100629 ret = gpio_request(p54spi_gpio_power, "p54spi power");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100630 if (ret < 0) {
631 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
632 goto err_free_common;
633 }
634
Christian Lampartera2116992009-01-16 22:34:15 +0100635 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100636 if (ret < 0) {
637 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
638 goto err_free_common;
639 }
640
Christian Lampartera2116992009-01-16 22:34:15 +0100641 gpio_direction_output(p54spi_gpio_power, 0);
642 gpio_direction_input(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100643
Christian Lampartera2116992009-01-16 22:34:15 +0100644 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100645 p54spi_interrupt, IRQF_DISABLED, "p54spi",
646 priv->spi);
647 if (ret < 0) {
648 dev_err(&priv->spi->dev, "request_irq() failed");
649 goto err_free_common;
650 }
651
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200652 irq_set_irq_type(gpio_to_irq(p54spi_gpio_irq), IRQ_TYPE_EDGE_RISING);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100653
Christian Lampartera2116992009-01-16 22:34:15 +0100654 disable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100655
656 INIT_WORK(&priv->work, p54spi_work);
657 init_completion(&priv->fw_comp);
658 INIT_LIST_HEAD(&priv->tx_pending);
659 mutex_init(&priv->mutex);
660 SET_IEEE80211_DEV(hw, &spi->dev);
661 priv->common.open = p54spi_op_start;
662 priv->common.stop = p54spi_op_stop;
663 priv->common.tx = p54spi_op_tx;
664
665 ret = p54spi_request_firmware(hw);
666 if (ret < 0)
667 goto err_free_common;
668
669 ret = p54spi_request_eeprom(hw);
670 if (ret)
671 goto err_free_common;
672
Christian Lamparter2ac71072009-03-05 21:30:10 +0100673 ret = p54_register_common(hw, &priv->spi->dev);
674 if (ret)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100675 goto err_free_common;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100676
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100677 return 0;
678
679err_free_common:
680 p54_free_common(priv->hw);
681 return ret;
682}
683
684static int __devexit p54spi_remove(struct spi_device *spi)
685{
686 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
687
Christian Lamparterd8c92102009-06-23 10:39:45 -0500688 p54_unregister_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100689
Christian Lampartera2116992009-01-16 22:34:15 +0100690 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100691
Christian Lampartera2116992009-01-16 22:34:15 +0100692 gpio_free(p54spi_gpio_power);
693 gpio_free(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100694 release_firmware(priv->firmware);
695
696 mutex_destroy(&priv->mutex);
697
698 p54_free_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100699
700 return 0;
701}
702
703
704static struct spi_driver p54spi_driver = {
705 .driver = {
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500706 .name = "p54spi",
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100707 .bus = &spi_bus_type,
708 .owner = THIS_MODULE,
709 },
710
711 .probe = p54spi_probe,
712 .remove = __devexit_p(p54spi_remove),
713};
714
715static int __init p54spi_init(void)
716{
717 int ret;
718
719 ret = spi_register_driver(&p54spi_driver);
720 if (ret < 0) {
721 printk(KERN_ERR "failed to register SPI driver: %d", ret);
722 goto out;
723 }
724
725out:
726 return ret;
727}
728
729static void __exit p54spi_exit(void)
730{
731 spi_unregister_driver(&p54spi_driver);
732}
733
734module_init(p54spi_init);
735module_exit(p54spi_exit);
736
737MODULE_LICENSE("GPL");
738MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700739MODULE_ALIAS("spi:cx3110x");
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500740MODULE_ALIAS("spi:p54spi");