2 * Copyright (C) 2008 Christian Lamparter <chunkeey@web.de>
3 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 * This driver is a port from stlc45xx:
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
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.
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.
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
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>
32 #include <linux/slab.h>
39 #ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
40 #include "p54spi_eeprom.h"
41 #endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
43 MODULE_FIRMWARE("3826.arm");
45 /* gpios should be handled in board files and provided via platform data,
46 * but because it's currently impossible for p54spi to have a header file
47 * in include/linux, let's use module paramaters for now
50 static int p54spi_gpio_power
= 97;
51 module_param(p54spi_gpio_power
, int, 0444);
52 MODULE_PARM_DESC(p54spi_gpio_power
, "gpio number for power line");
54 static int p54spi_gpio_irq
= 87;
55 module_param(p54spi_gpio_irq
, int, 0444);
56 MODULE_PARM_DESC(p54spi_gpio_irq
, "gpio number for irq line");
58 static void p54spi_spi_read(struct p54s_priv
*priv
, u8 address
,
59 void *buf
, size_t len
)
61 struct spi_transfer t
[2];
65 /* We first push the address */
66 addr
= cpu_to_le16(address
<< 8 | SPI_ADRS_READ_BIT_15
);
69 memset(t
, 0, sizeof(t
));
72 t
[0].len
= sizeof(addr
);
73 spi_message_add_tail(&t
[0], &m
);
77 spi_message_add_tail(&t
[1], &m
);
79 spi_sync(priv
->spi
, &m
);
83 static void p54spi_spi_write(struct p54s_priv
*priv
, u8 address
,
84 const void *buf
, size_t len
)
86 struct spi_transfer t
[3];
90 /* We first push the address */
91 addr
= cpu_to_le16(address
<< 8);
94 memset(t
, 0, sizeof(t
));
97 t
[0].len
= sizeof(addr
);
98 spi_message_add_tail(&t
[0], &m
);
102 spi_message_add_tail(&t
[1], &m
);
106 last_word
= cpu_to_le16(((u8
*)buf
)[len
- 1]);
108 t
[2].tx_buf
= &last_word
;
109 t
[2].len
= sizeof(last_word
);
110 spi_message_add_tail(&t
[2], &m
);
113 spi_sync(priv
->spi
, &m
);
116 static u32
p54spi_read32(struct p54s_priv
*priv
, u8 addr
)
120 p54spi_spi_read(priv
, addr
, &val
, sizeof(val
));
122 return le32_to_cpu(val
);
125 static inline void p54spi_write16(struct p54s_priv
*priv
, u8 addr
, __le16 val
)
127 p54spi_spi_write(priv
, addr
, &val
, sizeof(val
));
130 static inline void p54spi_write32(struct p54s_priv
*priv
, u8 addr
, __le32 val
)
132 p54spi_spi_write(priv
, addr
, &val
, sizeof(val
));
135 static int p54spi_wait_bit(struct p54s_priv
*priv
, u16 reg
, u32 bits
)
139 for (i
= 0; i
< 2000; i
++) {
140 u32 buffer
= p54spi_read32(priv
, reg
);
141 if ((buffer
& bits
) == bits
)
147 static int p54spi_spi_write_dma(struct p54s_priv
*priv
, __le32 base
,
148 const void *buf
, size_t len
)
150 if (!p54spi_wait_bit(priv
, SPI_ADRS_DMA_WRITE_CTRL
, HOST_ALLOWED
)) {
151 dev_err(&priv
->spi
->dev
, "spi_write_dma not allowed "
156 p54spi_write16(priv
, SPI_ADRS_DMA_WRITE_CTRL
,
157 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE
));
159 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
);
165 static int p54spi_request_firmware(struct ieee80211_hw
*dev
)
167 struct p54s_priv
*priv
= dev
->priv
;
170 /* FIXME: should driver use it's own struct device? */
171 ret
= request_firmware(&priv
->firmware
, "3826.arm", &priv
->spi
->dev
);
174 dev_err(&priv
->spi
->dev
, "request_firmware() failed: %d", ret
);
178 ret
= p54_parse_firmware(dev
, priv
->firmware
);
180 release_firmware(priv
->firmware
);
187 static int p54spi_request_eeprom(struct ieee80211_hw
*dev
)
189 struct p54s_priv
*priv
= dev
->priv
;
190 const struct firmware
*eeprom
;
193 /* allow users to customize their eeprom.
196 ret
= request_firmware_direct(&eeprom
, "3826.eeprom", &priv
->spi
->dev
);
198 #ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
199 dev_info(&priv
->spi
->dev
, "loading default eeprom...\n");
200 ret
= p54_parse_eeprom(dev
, (void *) p54spi_eeprom
,
201 sizeof(p54spi_eeprom
));
203 dev_err(&priv
->spi
->dev
, "Failed to request user eeprom\n");
204 #endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
206 dev_info(&priv
->spi
->dev
, "loading user eeprom...\n");
207 ret
= p54_parse_eeprom(dev
, (void *) eeprom
->data
,
209 release_firmware(eeprom
);
214 static int p54spi_upload_firmware(struct ieee80211_hw
*dev
)
216 struct p54s_priv
*priv
= dev
->priv
;
217 unsigned long fw_len
, _fw_len
;
218 unsigned int offset
= 0;
222 fw_len
= priv
->firmware
->size
;
223 fw
= kmemdup(priv
->firmware
->data
, fw_len
, GFP_KERNEL
);
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
));
232 msleep(TARGET_BOOT_SLEEP
);
234 p54spi_write16(priv
, SPI_ADRS_DEV_CTRL_STAT
, cpu_to_le16(
235 SPI_CTRL_STAT_HOST_OVERRIDE
|
236 SPI_CTRL_STAT_START_HALTED
));
238 msleep(TARGET_BOOT_SLEEP
);
241 _fw_len
= min_t(long, fw_len
, SPI_MAX_PACKET_SIZE
);
243 err
= p54spi_spi_write_dma(priv
, cpu_to_le32(
244 ISL38XX_DEV_FIRMWARE_ADDR
+ offset
),
245 (fw
+ offset
), _fw_len
);
255 /* enable host interrupts */
256 p54spi_write32(priv
, SPI_ADRS_HOST_INT_EN
,
257 cpu_to_le32(SPI_HOST_INTS_DEFAULT
));
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
));
264 msleep(TARGET_BOOT_SLEEP
);
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
);
275 static void p54spi_power_off(struct p54s_priv
*priv
)
277 disable_irq(gpio_to_irq(p54spi_gpio_irq
));
278 gpio_set_value(p54spi_gpio_power
, 0);
281 static void p54spi_power_on(struct p54s_priv
*priv
)
283 gpio_set_value(p54spi_gpio_power
, 1);
284 enable_irq(gpio_to_irq(p54spi_gpio_irq
));
286 /* need to wait a while before device can be accessed, the length
292 static inline void p54spi_int_ack(struct p54s_priv
*priv
, u32 val
)
294 p54spi_write32(priv
, SPI_ADRS_HOST_INT_ACK
, cpu_to_le32(val
));
297 static int p54spi_wakeup(struct p54s_priv
*priv
)
300 p54spi_write32(priv
, SPI_ADRS_ARM_INTERRUPTS
,
301 cpu_to_le32(SPI_TARGET_INT_WAKEUP
));
303 /* And wait for the READY interrupt */
304 if (!p54spi_wait_bit(priv
, SPI_ADRS_HOST_INTERRUPTS
,
305 SPI_HOST_INT_READY
)) {
306 dev_err(&priv
->spi
->dev
, "INT_READY timeout\n");
310 p54spi_int_ack(priv
, SPI_HOST_INT_READY
);
314 static inline void p54spi_sleep(struct p54s_priv
*priv
)
316 p54spi_write32(priv
, SPI_ADRS_ARM_INTERRUPTS
,
317 cpu_to_le32(SPI_TARGET_INT_SLEEP
));
320 static void p54spi_int_ready(struct p54s_priv
*priv
)
322 p54spi_write32(priv
, SPI_ADRS_HOST_INT_EN
, cpu_to_le32(
323 SPI_HOST_INT_UPDATE
| SPI_HOST_INT_SW_UPDATE
));
325 switch (priv
->fw_state
) {
326 case FW_STATE_BOOTING
:
327 priv
->fw_state
= FW_STATE_READY
;
328 complete(&priv
->fw_comp
);
330 case FW_STATE_RESETTING
:
331 priv
->fw_state
= FW_STATE_READY
;
332 /* TODO: reinitialize state */
339 static int p54spi_rx(struct p54s_priv
*priv
)
344 #define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
346 if (p54spi_wakeup(priv
) < 0)
349 /* 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.
353 p54spi_spi_read(priv
, SPI_ADRS_DMA_DATA
, rx_head
, sizeof(rx_head
));
358 dev_err(&priv
->spi
->dev
, "rx request of zero bytes\n");
362 /* 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
365 * past the end of allocated skb. Reserve extra 4 bytes for this case
367 skb
= dev_alloc_skb(len
+ 4);
370 dev_err(&priv
->spi
->dev
, "could not alloc skb");
374 if (len
<= READAHEAD_SZ
) {
375 skb_put_data(skb
, rx_head
+ 1, len
);
377 skb_put_data(skb
, rx_head
+ 1, READAHEAD_SZ
);
378 p54spi_spi_read(priv
, SPI_ADRS_DMA_DATA
,
379 skb_put(skb
, len
- READAHEAD_SZ
),
383 /* Put additional bytes to compensate for the possible
384 * alignment-caused truncation
388 if (p54_rx(priv
->hw
, skb
) == 0)
395 static irqreturn_t
p54spi_interrupt(int irq
, void *config
)
397 struct spi_device
*spi
= config
;
398 struct p54s_priv
*priv
= spi_get_drvdata(spi
);
400 ieee80211_queue_work(priv
->hw
, &priv
->work
);
405 static int p54spi_tx_frame(struct p54s_priv
*priv
, struct sk_buff
*skb
)
407 struct p54_hdr
*hdr
= (struct p54_hdr
*) skb
->data
;
410 if (p54spi_wakeup(priv
) < 0)
413 ret
= p54spi_spi_write_dma(priv
, hdr
->req_id
, skb
->data
, skb
->len
);
417 if (!p54spi_wait_bit(priv
, SPI_ADRS_HOST_INTERRUPTS
,
418 SPI_HOST_INT_WR_READY
)) {
419 dev_err(&priv
->spi
->dev
, "WR_READY timeout\n");
424 p54spi_int_ack(priv
, SPI_HOST_INT_WR_READY
);
426 if (FREE_AFTER_TX(skb
))
427 p54_free_skb(priv
->hw
, skb
);
433 static int p54spi_wq_tx(struct p54s_priv
*priv
)
435 struct p54s_tx_info
*entry
;
437 struct ieee80211_tx_info
*info
;
438 struct p54_tx_info
*minfo
;
439 struct p54s_tx_info
*dinfo
;
443 spin_lock_irqsave(&priv
->tx_lock
, flags
);
445 while (!list_empty(&priv
->tx_pending
)) {
446 entry
= list_entry(priv
->tx_pending
.next
,
447 struct p54s_tx_info
, tx_list
);
449 list_del_init(&entry
->tx_list
);
451 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
453 dinfo
= container_of((void *) entry
, struct p54s_tx_info
,
455 minfo
= container_of((void *) dinfo
, struct p54_tx_info
,
457 info
= container_of((void *) minfo
, struct ieee80211_tx_info
,
459 skb
= container_of((void *) info
, struct sk_buff
, cb
);
461 ret
= p54spi_tx_frame(priv
, skb
);
464 p54_free_skb(priv
->hw
, skb
);
468 spin_lock_irqsave(&priv
->tx_lock
, flags
);
470 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
474 static void p54spi_op_tx(struct ieee80211_hw
*dev
, struct sk_buff
*skb
)
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
;
482 BUILD_BUG_ON(sizeof(*di
) > sizeof((mi
->data
)));
484 spin_lock_irqsave(&priv
->tx_lock
, flags
);
485 list_add_tail(&di
->tx_list
, &priv
->tx_pending
);
486 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
488 ieee80211_queue_work(priv
->hw
, &priv
->work
);
491 static void p54spi_work(struct work_struct
*work
)
493 struct p54s_priv
*priv
= container_of(work
, struct p54s_priv
, work
);
497 mutex_lock(&priv
->mutex
);
499 if (priv
->fw_state
== FW_STATE_OFF
)
502 ints
= p54spi_read32(priv
, SPI_ADRS_HOST_INTERRUPTS
);
504 if (ints
& SPI_HOST_INT_READY
) {
505 p54spi_int_ready(priv
);
506 p54spi_int_ack(priv
, SPI_HOST_INT_READY
);
509 if (priv
->fw_state
!= FW_STATE_READY
)
512 if (ints
& SPI_HOST_INT_UPDATE
) {
513 p54spi_int_ack(priv
, SPI_HOST_INT_UPDATE
);
514 ret
= p54spi_rx(priv
);
518 if (ints
& SPI_HOST_INT_SW_UPDATE
) {
519 p54spi_int_ack(priv
, SPI_HOST_INT_SW_UPDATE
);
520 ret
= p54spi_rx(priv
);
525 ret
= p54spi_wq_tx(priv
);
527 mutex_unlock(&priv
->mutex
);
530 static int p54spi_op_start(struct ieee80211_hw
*dev
)
532 struct p54s_priv
*priv
= dev
->priv
;
533 unsigned long timeout
;
536 if (mutex_lock_interruptible(&priv
->mutex
)) {
541 priv
->fw_state
= FW_STATE_BOOTING
;
543 p54spi_power_on(priv
);
545 ret
= p54spi_upload_firmware(dev
);
547 p54spi_power_off(priv
);
551 mutex_unlock(&priv
->mutex
);
553 timeout
= msecs_to_jiffies(2000);
554 timeout
= wait_for_completion_interruptible_timeout(&priv
->fw_comp
,
557 dev_err(&priv
->spi
->dev
, "firmware boot failed");
558 p54spi_power_off(priv
);
563 if (mutex_lock_interruptible(&priv
->mutex
)) {
565 p54spi_power_off(priv
);
569 WARN_ON(priv
->fw_state
!= FW_STATE_READY
);
572 mutex_unlock(&priv
->mutex
);
578 static void p54spi_op_stop(struct ieee80211_hw
*dev
)
580 struct p54s_priv
*priv
= dev
->priv
;
583 mutex_lock(&priv
->mutex
);
584 WARN_ON(priv
->fw_state
!= FW_STATE_READY
);
586 p54spi_power_off(priv
);
587 spin_lock_irqsave(&priv
->tx_lock
, flags
);
588 INIT_LIST_HEAD(&priv
->tx_pending
);
589 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
591 priv
->fw_state
= FW_STATE_OFF
;
592 mutex_unlock(&priv
->mutex
);
594 cancel_work_sync(&priv
->work
);
597 static int p54spi_probe(struct spi_device
*spi
)
599 struct p54s_priv
*priv
= NULL
;
600 struct ieee80211_hw
*hw
;
603 hw
= p54_init_common(sizeof(*priv
));
605 dev_err(&spi
->dev
, "could not alloc ieee80211_hw");
611 spi_set_drvdata(spi
, priv
);
614 spi
->bits_per_word
= 16;
615 spi
->max_speed_hz
= 24000000;
617 ret
= spi_setup(spi
);
619 dev_err(&priv
->spi
->dev
, "spi_setup failed");
623 ret
= gpio_request(p54spi_gpio_power
, "p54spi power");
625 dev_err(&priv
->spi
->dev
, "power GPIO request failed: %d", ret
);
629 ret
= gpio_request(p54spi_gpio_irq
, "p54spi irq");
631 dev_err(&priv
->spi
->dev
, "irq GPIO request failed: %d", ret
);
632 goto err_free_gpio_power
;
635 gpio_direction_output(p54spi_gpio_power
, 0);
636 gpio_direction_input(p54spi_gpio_irq
);
638 ret
= request_irq(gpio_to_irq(p54spi_gpio_irq
),
639 p54spi_interrupt
, 0, "p54spi",
642 dev_err(&priv
->spi
->dev
, "request_irq() failed");
643 goto err_free_gpio_irq
;
646 irq_set_irq_type(gpio_to_irq(p54spi_gpio_irq
), IRQ_TYPE_EDGE_RISING
);
648 disable_irq(gpio_to_irq(p54spi_gpio_irq
));
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
);
654 spin_lock_init(&priv
->tx_lock
);
655 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
;
660 ret
= p54spi_request_firmware(hw
);
662 goto err_free_common
;
664 ret
= p54spi_request_eeprom(hw
);
666 goto err_free_common
;
668 ret
= p54_register_common(hw
, &priv
->spi
->dev
);
670 goto err_free_common
;
675 free_irq(gpio_to_irq(p54spi_gpio_irq
), spi
);
677 gpio_free(p54spi_gpio_irq
);
679 gpio_free(p54spi_gpio_power
);
681 p54_free_common(priv
->hw
);
685 static int p54spi_remove(struct spi_device
*spi
)
687 struct p54s_priv
*priv
= spi_get_drvdata(spi
);
689 p54_unregister_common(priv
->hw
);
691 free_irq(gpio_to_irq(p54spi_gpio_irq
), spi
);
693 gpio_free(p54spi_gpio_power
);
694 gpio_free(p54spi_gpio_irq
);
695 release_firmware(priv
->firmware
);
697 mutex_destroy(&priv
->mutex
);
699 p54_free_common(priv
->hw
);
705 static struct spi_driver p54spi_driver
= {
710 .probe
= p54spi_probe
,
711 .remove
= p54spi_remove
,
714 module_spi_driver(p54spi_driver
);
716 MODULE_LICENSE("GPL");
717 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
718 MODULE_ALIAS("spi:cx3110x");
719 MODULE_ALIAS("spi:p54spi");
720 MODULE_ALIAS("spi:stlc45xx");