blob: 7ab2f43ab42512e7da0c87c37574b8ed534f2b08 [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");
Christian Lampartercd8d3d32009-01-11 01:18:38 +010044
Sachin Kamatacba7bb2013-05-29 15:31:41 +053045/* gpios should be handled in board files and provided via platform data,
Christian Lampartera2116992009-01-16 22:34:15 +010046 * but because it's currently impossible for p54spi to have a header file
47 * in include/linux, let's use module paramaters for now
48 */
49
50static int p54spi_gpio_power = 97;
51module_param(p54spi_gpio_power, int, 0444);
52MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
53
54static int p54spi_gpio_irq = 87;
55module_param(p54spi_gpio_irq, int, 0444);
56MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
57
Christian Lampartercd8d3d32009-01-11 01:18:38 +010058static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
59 void *buf, size_t len)
60{
61 struct spi_transfer t[2];
62 struct spi_message m;
63 __le16 addr;
64
65 /* We first push the address */
66 addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
67
68 spi_message_init(&m);
69 memset(t, 0, sizeof(t));
70
71 t[0].tx_buf = &addr;
72 t[0].len = sizeof(addr);
73 spi_message_add_tail(&t[0], &m);
74
75 t[1].rx_buf = buf;
76 t[1].len = len;
77 spi_message_add_tail(&t[1], &m);
78
79 spi_sync(priv->spi, &m);
80}
81
82
83static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
84 const void *buf, size_t len)
85{
86 struct spi_transfer t[3];
87 struct spi_message m;
88 __le16 addr;
89
90 /* We first push the address */
91 addr = cpu_to_le16(address << 8);
92
93 spi_message_init(&m);
94 memset(t, 0, sizeof(t));
95
96 t[0].tx_buf = &addr;
97 t[0].len = sizeof(addr);
98 spi_message_add_tail(&t[0], &m);
99
100 t[1].tx_buf = buf;
Max Filippov69712e92009-05-18 03:02:32 +0400101 t[1].len = len & ~1;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100102 spi_message_add_tail(&t[1], &m);
103
104 if (len % 2) {
105 __le16 last_word;
106 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
107
108 t[2].tx_buf = &last_word;
109 t[2].len = sizeof(last_word);
110 spi_message_add_tail(&t[2], &m);
111 }
112
113 spi_sync(priv->spi, &m);
114}
115
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100116static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
117{
118 __le32 val;
119
120 p54spi_spi_read(priv, addr, &val, sizeof(val));
121
122 return le32_to_cpu(val);
123}
124
125static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
126{
127 p54spi_spi_write(priv, addr, &val, sizeof(val));
128}
129
130static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
131{
132 p54spi_spi_write(priv, addr, &val, sizeof(val));
133}
134
Christian Lampartera7eee062009-07-03 21:01:15 +0200135static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, u32 bits)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100136{
137 int i;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100138
139 for (i = 0; i < 2000; i++) {
Christian Lampartera7eee062009-07-03 21:01:15 +0200140 u32 buffer = p54spi_read32(priv, reg);
Max Filippovf74d0f52009-03-25 08:30:15 +0300141 if ((buffer & bits) == bits)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100142 return 1;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100143 }
144 return 0;
145}
146
Max Filippov4f5cab92009-03-26 06:38:25 +0300147static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base,
148 const void *buf, size_t len)
149{
Christian Lampartera7eee062009-07-03 21:01:15 +0200150 if (!p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL, HOST_ALLOWED)) {
Max Filippov4f5cab92009-03-26 06:38:25 +0300151 dev_err(&priv->spi->dev, "spi_write_dma not allowed "
Max Filippov87cbfd02009-04-06 01:03:09 +0400152 "to DMA write.\n");
Max Filippov4f5cab92009-03-26 06:38:25 +0300153 return -EAGAIN;
154 }
155
Max Filippov210dd1b2009-05-18 03:02:31 +0400156 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
157 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
158
Max Filippov4f5cab92009-03-26 06:38:25 +0300159 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len));
160 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base);
161 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len);
162 return 0;
163}
164
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100165static int p54spi_request_firmware(struct ieee80211_hw *dev)
166{
167 struct p54s_priv *priv = dev->priv;
168 int ret;
169
170 /* FIXME: should driver use it's own struct device? */
171 ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
172
173 if (ret < 0) {
174 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
175 return ret;
176 }
177
178 ret = p54_parse_firmware(dev, priv->firmware);
179 if (ret) {
180 release_firmware(priv->firmware);
181 return ret;
182 }
183
184 return 0;
185}
186
187static int p54spi_request_eeprom(struct ieee80211_hw *dev)
188{
189 struct p54s_priv *priv = dev->priv;
190 const struct firmware *eeprom;
191 int ret;
192
Sachin Kamatacba7bb2013-05-29 15:31:41 +0530193 /* allow users to customize their eeprom.
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100194 */
195
Luis R. Rodriguez80140b72014-06-24 15:39:43 -0700196 ret = request_firmware_direct(&eeprom, "3826.eeprom", &priv->spi->dev);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100197 if (ret < 0) {
Christian Lamparterd7065c32010-08-22 00:00:14 +0200198#ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100199 dev_info(&priv->spi->dev, "loading default eeprom...\n");
200 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
201 sizeof(p54spi_eeprom));
Michael Büschf4bbf922010-09-05 00:55:38 +0200202#else
203 dev_err(&priv->spi->dev, "Failed to request user eeprom\n");
Christian Lamparterd7065c32010-08-22 00:00:14 +0200204#endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100205 } else {
206 dev_info(&priv->spi->dev, "loading user eeprom...\n");
207 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
208 (int)eeprom->size);
209 release_firmware(eeprom);
210 }
211 return ret;
212}
213
214static int p54spi_upload_firmware(struct ieee80211_hw *dev)
215{
216 struct p54s_priv *priv = dev->priv;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100217 unsigned long fw_len, _fw_len;
218 unsigned int offset = 0;
219 int err = 0;
220 u8 *fw;
221
222 fw_len = priv->firmware->size;
223 fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
224 if (!fw)
225 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100226
227 /* stop the device */
228 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
229 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
230 SPI_CTRL_STAT_START_HALTED));
231
232 msleep(TARGET_BOOT_SLEEP);
233
234 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
235 SPI_CTRL_STAT_HOST_OVERRIDE |
236 SPI_CTRL_STAT_START_HALTED));
237
238 msleep(TARGET_BOOT_SLEEP);
239
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100240 while (fw_len > 0) {
241 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
242
Max Filippov4f5cab92009-03-26 06:38:25 +0300243 err = p54spi_spi_write_dma(priv, cpu_to_le32(
244 ISL38XX_DEV_FIRMWARE_ADDR + offset),
245 (fw + offset), _fw_len);
246 if (err < 0)
Max Filippov5e3af1d2009-03-25 13:45:01 +0100247 goto out;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100248
249 fw_len -= _fw_len;
Max Filippov5e3af1d2009-03-25 13:45:01 +0100250 offset += _fw_len;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100251 }
252
253 BUG_ON(fw_len != 0);
254
255 /* enable host interrupts */
256 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
257 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
258
259 /* boot the device */
260 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
261 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
262 SPI_CTRL_STAT_RAM_BOOT));
263
264 msleep(TARGET_BOOT_SLEEP);
265
266 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
267 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
268 msleep(TARGET_BOOT_SLEEP);
Max Filippov5e3af1d2009-03-25 13:45:01 +0100269
270out:
271 kfree(fw);
272 return err;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100273}
274
275static void p54spi_power_off(struct p54s_priv *priv)
276{
Christian Lampartera2116992009-01-16 22:34:15 +0100277 disable_irq(gpio_to_irq(p54spi_gpio_irq));
278 gpio_set_value(p54spi_gpio_power, 0);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100279}
280
281static void p54spi_power_on(struct p54s_priv *priv)
282{
Christian Lampartera2116992009-01-16 22:34:15 +0100283 gpio_set_value(p54spi_gpio_power, 1);
284 enable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100285
Sachin Kamatacba7bb2013-05-29 15:31:41 +0530286 /* need to wait a while before device can be accessed, the length
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100287 * is just a guess
288 */
289 msleep(10);
290}
291
292static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
293{
294 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
295}
296
Max Filippov465b6352009-05-18 03:02:33 +0400297static int p54spi_wakeup(struct p54s_priv *priv)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100298{
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100299 /* wake the chip */
300 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
301 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
302
303 /* And wait for the READY interrupt */
Max Filippov87cbfd02009-04-06 01:03:09 +0400304 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
Christian Lampartera7eee062009-07-03 21:01:15 +0200305 SPI_HOST_INT_READY)) {
Max Filippov87cbfd02009-04-06 01:03:09 +0400306 dev_err(&priv->spi->dev, "INT_READY timeout\n");
Max Filippov465b6352009-05-18 03:02:33 +0400307 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100308 }
309
310 p54spi_int_ack(priv, SPI_HOST_INT_READY);
Max Filippov465b6352009-05-18 03:02:33 +0400311 return 0;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100312}
313
314static inline void p54spi_sleep(struct p54s_priv *priv)
315{
316 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
317 cpu_to_le32(SPI_TARGET_INT_SLEEP));
318}
319
320static void p54spi_int_ready(struct p54s_priv *priv)
321{
322 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
323 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
324
325 switch (priv->fw_state) {
326 case FW_STATE_BOOTING:
327 priv->fw_state = FW_STATE_READY;
328 complete(&priv->fw_comp);
329 break;
330 case FW_STATE_RESETTING:
331 priv->fw_state = FW_STATE_READY;
332 /* TODO: reinitialize state */
333 break;
334 default:
335 break;
336 }
337}
338
339static int p54spi_rx(struct p54s_priv *priv)
340{
341 struct sk_buff *skb;
342 u16 len;
Max Filippovff561ac2009-05-18 03:02:35 +0400343 u16 rx_head[2];
344#define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100345
Max Filippov465b6352009-05-18 03:02:33 +0400346 if (p54spi_wakeup(priv) < 0)
347 return -EBUSY;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100348
Max Filippovff561ac2009-05-18 03:02:35 +0400349 /* Read data size and first data word in one SPI transaction
350 * This is workaround for firmware/DMA bug,
351 * when first data word gets lost under high load.
352 */
353 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
354 len = rx_head[0];
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100355
356 if (len == 0) {
Max Filippovff561ac2009-05-18 03:02:35 +0400357 p54spi_sleep(priv);
358 dev_err(&priv->spi->dev, "rx request of zero bytes\n");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100359 return 0;
360 }
361
Max Filippov9f201a82009-03-27 07:50:53 +0300362 /* Firmware may insert up to 4 padding bytes after the lmac header,
363 * but it does not amend the size of SPI data transfer.
364 * Such packets has correct data size in header, thus referencing
Sachin Kamatacba7bb2013-05-29 15:31:41 +0530365 * past the end of allocated skb. Reserve extra 4 bytes for this case
366 */
Max Filippov9f201a82009-03-27 07:50:53 +0300367 skb = dev_alloc_skb(len + 4);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100368 if (!skb) {
Max Filippovff561ac2009-05-18 03:02:35 +0400369 p54spi_sleep(priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100370 dev_err(&priv->spi->dev, "could not alloc skb");
Max Filippovff561ac2009-05-18 03:02:35 +0400371 return -ENOMEM;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100372 }
373
Max Filippovff561ac2009-05-18 03:02:35 +0400374 if (len <= READAHEAD_SZ) {
375 memcpy(skb_put(skb, len), rx_head + 1, len);
376 } else {
377 memcpy(skb_put(skb, READAHEAD_SZ), rx_head + 1, READAHEAD_SZ);
378 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
379 skb_put(skb, len - READAHEAD_SZ),
380 len - READAHEAD_SZ);
381 }
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100382 p54spi_sleep(priv);
Max Filippov9f201a82009-03-27 07:50:53 +0300383 /* Put additional bytes to compensate for the possible
Sachin Kamatacba7bb2013-05-29 15:31:41 +0530384 * alignment-caused truncation
385 */
Max Filippov9f201a82009-03-27 07:50:53 +0300386 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;
Jingoo Hand60c5d22013-04-05 20:36:47 +0000398 struct p54s_priv *priv = spi_get_drvdata(spi);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100399
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
Michael Büsch7adb92f2011-11-16 23:51:20 +0100583 mutex_lock(&priv->mutex);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100584 WARN_ON(priv->fw_state != FW_STATE_READY);
585
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100586 p54spi_power_off(priv);
Christian Lamparter731c6532009-03-30 15:55:24 +0200587 spin_lock_irqsave(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100588 INIT_LIST_HEAD(&priv->tx_pending);
Christian Lamparter731c6532009-03-30 15:55:24 +0200589 spin_unlock_irqrestore(&priv->tx_lock, flags);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100590
591 priv->fw_state = FW_STATE_OFF;
592 mutex_unlock(&priv->mutex);
Michael Büsch2d161812011-11-16 23:55:46 +0100593
594 cancel_work_sync(&priv->work);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100595}
596
Bill Pemberton337b5632012-12-03 09:56:38 -0500597static int p54spi_probe(struct spi_device *spi)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100598{
599 struct p54s_priv *priv = NULL;
600 struct ieee80211_hw *hw;
601 int ret = -EINVAL;
602
603 hw = p54_init_common(sizeof(*priv));
604 if (!hw) {
Dan Carpenterbfa99bf2009-07-19 21:26:13 +0200605 dev_err(&spi->dev, "could not alloc ieee80211_hw");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100606 return -ENOMEM;
607 }
608
609 priv = hw->priv;
610 priv->hw = hw;
Jingoo Hand60c5d22013-04-05 20:36:47 +0000611 spi_set_drvdata(spi, priv);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100612 priv->spi = spi;
613
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100614 spi->bits_per_word = 16;
615 spi->max_speed_hz = 24000000;
616
617 ret = spi_setup(spi);
618 if (ret < 0) {
619 dev_err(&priv->spi->dev, "spi_setup failed");
Max Filippov62ebeed2012-03-01 00:40:08 +0400620 goto err_free;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100621 }
622
Christian Lampartera2116992009-01-16 22:34:15 +0100623 ret = gpio_request(p54spi_gpio_power, "p54spi power");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100624 if (ret < 0) {
625 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
Max Filippov62ebeed2012-03-01 00:40:08 +0400626 goto err_free;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100627 }
628
Christian Lampartera2116992009-01-16 22:34:15 +0100629 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100630 if (ret < 0) {
631 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
Max Filippov62ebeed2012-03-01 00:40:08 +0400632 goto err_free_gpio_power;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100633 }
634
Christian Lampartera2116992009-01-16 22:34:15 +0100635 gpio_direction_output(p54spi_gpio_power, 0);
636 gpio_direction_input(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100637
Christian Lampartera2116992009-01-16 22:34:15 +0100638 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
Michael Opdenackera9b1d9a2013-10-06 07:04:06 +0200639 p54spi_interrupt, 0, "p54spi",
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100640 priv->spi);
641 if (ret < 0) {
642 dev_err(&priv->spi->dev, "request_irq() failed");
Max Filippov62ebeed2012-03-01 00:40:08 +0400643 goto err_free_gpio_irq;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100644 }
645
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200646 irq_set_irq_type(gpio_to_irq(p54spi_gpio_irq), IRQ_TYPE_EDGE_RISING);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100647
Christian Lampartera2116992009-01-16 22:34:15 +0100648 disable_irq(gpio_to_irq(p54spi_gpio_irq));
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100649
650 INIT_WORK(&priv->work, p54spi_work);
651 init_completion(&priv->fw_comp);
652 INIT_LIST_HEAD(&priv->tx_pending);
653 mutex_init(&priv->mutex);
Michael Büsch32d3a392011-11-16 23:48:31 +0100654 spin_lock_init(&priv->tx_lock);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100655 SET_IEEE80211_DEV(hw, &spi->dev);
656 priv->common.open = p54spi_op_start;
657 priv->common.stop = p54spi_op_stop;
658 priv->common.tx = p54spi_op_tx;
659
660 ret = p54spi_request_firmware(hw);
661 if (ret < 0)
662 goto err_free_common;
663
664 ret = p54spi_request_eeprom(hw);
665 if (ret)
666 goto err_free_common;
667
Christian Lamparter2ac71072009-03-05 21:30:10 +0100668 ret = p54_register_common(hw, &priv->spi->dev);
669 if (ret)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100670 goto err_free_common;
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100671
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100672 return 0;
673
674err_free_common:
Max Filippov62ebeed2012-03-01 00:40:08 +0400675 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
676err_free_gpio_irq:
677 gpio_free(p54spi_gpio_irq);
678err_free_gpio_power:
679 gpio_free(p54spi_gpio_power);
680err_free:
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100681 p54_free_common(priv->hw);
682 return ret;
683}
684
Bill Pemberton337b5632012-12-03 09:56:38 -0500685static int p54spi_remove(struct spi_device *spi)
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100686{
Jingoo Hand60c5d22013-04-05 20:36:47 +0000687 struct p54s_priv *priv = spi_get_drvdata(spi);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100688
Christian Lamparterd8c92102009-06-23 10:39:45 -0500689 p54_unregister_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100690
Christian Lampartera2116992009-01-16 22:34:15 +0100691 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100692
Christian Lampartera2116992009-01-16 22:34:15 +0100693 gpio_free(p54spi_gpio_power);
694 gpio_free(p54spi_gpio_irq);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100695 release_firmware(priv->firmware);
696
697 mutex_destroy(&priv->mutex);
698
699 p54_free_common(priv->hw);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100700
701 return 0;
702}
703
704
705static struct spi_driver p54spi_driver = {
706 .driver = {
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500707 .name = "p54spi",
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100708 },
709
710 .probe = p54spi_probe,
Bill Pemberton337b5632012-12-03 09:56:38 -0500711 .remove = p54spi_remove,
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100712};
713
Sachin Kamatb6c32f82013-05-29 15:31:40 +0530714module_spi_driver(p54spi_driver);
Christian Lampartercd8d3d32009-01-11 01:18:38 +0100715
716MODULE_LICENSE("GPL");
717MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700718MODULE_ALIAS("spi:cx3110x");
Luke-Jr5f1e83d2010-06-01 21:16:53 -0500719MODULE_ALIAS("spi:p54spi");
Axel Lin1eb85d632011-08-26 14:34:59 +0800720MODULE_ALIAS("spi:stlc45xx");