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>
35 #include "p54spi_eeprom.h"
40 MODULE_FIRMWARE("3826.arm");
41 MODULE_ALIAS("stlc45xx");
44 * gpios should be handled in board files and provided via platform data,
45 * but because it's currently impossible for p54spi to have a header file
46 * in include/linux, let's use module paramaters for now
49 static int p54spi_gpio_power = 97;
50 module_param(p54spi_gpio_power, int, 0444);
51 MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
53 static int p54spi_gpio_irq = 87;
54 module_param(p54spi_gpio_irq, int, 0444);
55 MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
57 static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
58 void *buf, size_t len)
60 struct spi_transfer t[2];
64 /* We first push the address */
65 addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
68 memset(t, 0, sizeof(t));
71 t[0].len = sizeof(addr);
72 spi_message_add_tail(&t[0], &m);
76 spi_message_add_tail(&t[1], &m);
78 spi_sync(priv->spi, &m);
82 static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
83 const void *buf, size_t len)
85 struct spi_transfer t[3];
89 /* We first push the address */
90 addr = cpu_to_le16(address << 8);
93 memset(t, 0, sizeof(t));
96 t[0].len = sizeof(addr);
97 spi_message_add_tail(&t[0], &m);
101 spi_message_add_tail(&t[1], &m);
105 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
107 t[2].tx_buf = &last_word;
108 t[2].len = sizeof(last_word);
109 spi_message_add_tail(&t[2], &m);
112 spi_sync(priv->spi, &m);
115 static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
119 p54spi_spi_read(priv, addr, &val, sizeof(val));
121 return le32_to_cpu(val);
124 static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
126 p54spi_spi_write(priv, addr, &val, sizeof(val));
129 static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
131 p54spi_spi_write(priv, addr, &val, sizeof(val));
134 static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, u32 bits)
138 for (i = 0; i < 2000; i++) {
139 u32 buffer = p54spi_read32(priv, reg);
140 if ((buffer & bits) == bits)
146 static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base,
147 const void *buf, size_t len)
149 if (!p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL, HOST_ALLOWED)) {
150 dev_err(&priv->spi->dev, "spi_write_dma not allowed "
155 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
156 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
158 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len));
159 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base);
160 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len);
164 static int p54spi_request_firmware(struct ieee80211_hw *dev)
166 struct p54s_priv *priv = dev->priv;
169 /* FIXME: should driver use it's own struct device? */
170 ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
173 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
177 ret = p54_parse_firmware(dev, priv->firmware);
179 release_firmware(priv->firmware);
186 static int p54spi_request_eeprom(struct ieee80211_hw *dev)
188 struct p54s_priv *priv = dev->priv;
189 const struct firmware *eeprom;
193 * allow users to customize their eeprom.
196 ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev);
198 dev_info(&priv->spi->dev, "loading default eeprom...\n");
199 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
200 sizeof(p54spi_eeprom));
202 dev_info(&priv->spi->dev, "loading user eeprom...\n");
203 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
205 release_firmware(eeprom);
210 static int p54spi_upload_firmware(struct ieee80211_hw *dev)
212 struct p54s_priv *priv = dev->priv;
213 unsigned long fw_len, _fw_len;
214 unsigned int offset = 0;
218 fw_len = priv->firmware->size;
219 fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
223 /* stop the device */
224 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
225 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
226 SPI_CTRL_STAT_START_HALTED));
228 msleep(TARGET_BOOT_SLEEP);
230 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
231 SPI_CTRL_STAT_HOST_OVERRIDE |
232 SPI_CTRL_STAT_START_HALTED));
234 msleep(TARGET_BOOT_SLEEP);
237 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
239 err = p54spi_spi_write_dma(priv, cpu_to_le32(
240 ISL38XX_DEV_FIRMWARE_ADDR + offset),
241 (fw + offset), _fw_len);
251 /* enable host interrupts */
252 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
253 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
255 /* boot the device */
256 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
257 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
258 SPI_CTRL_STAT_RAM_BOOT));
260 msleep(TARGET_BOOT_SLEEP);
262 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
263 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
264 msleep(TARGET_BOOT_SLEEP);
271 static void p54spi_power_off(struct p54s_priv *priv)
273 disable_irq(gpio_to_irq(p54spi_gpio_irq));
274 gpio_set_value(p54spi_gpio_power, 0);
277 static void p54spi_power_on(struct p54s_priv *priv)
279 gpio_set_value(p54spi_gpio_power, 1);
280 enable_irq(gpio_to_irq(p54spi_gpio_irq));
283 * need to wait a while before device can be accessed, the lenght
289 static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
291 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
294 static int p54spi_wakeup(struct p54s_priv *priv)
297 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
298 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
300 /* And wait for the READY interrupt */
301 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
302 SPI_HOST_INT_READY)) {
303 dev_err(&priv->spi->dev, "INT_READY timeout\n");
307 p54spi_int_ack(priv, SPI_HOST_INT_READY);
311 static inline void p54spi_sleep(struct p54s_priv *priv)
313 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
314 cpu_to_le32(SPI_TARGET_INT_SLEEP));
317 static void p54spi_int_ready(struct p54s_priv *priv)
319 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
320 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
322 switch (priv->fw_state) {
323 case FW_STATE_BOOTING:
324 priv->fw_state = FW_STATE_READY;
325 complete(&priv->fw_comp);
327 case FW_STATE_RESETTING:
328 priv->fw_state = FW_STATE_READY;
329 /* TODO: reinitialize state */
336 static int p54spi_rx(struct p54s_priv *priv)
341 #define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
343 if (p54spi_wakeup(priv) < 0)
346 /* Read data size and first data word in one SPI transaction
347 * This is workaround for firmware/DMA bug,
348 * when first data word gets lost under high load.
350 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
355 dev_err(&priv->spi->dev, "rx request of zero bytes\n");
359 /* Firmware may insert up to 4 padding bytes after the lmac header,
360 * but it does not amend the size of SPI data transfer.
361 * Such packets has correct data size in header, thus referencing
362 * past the end of allocated skb. Reserve extra 4 bytes for this case */
363 skb = dev_alloc_skb(len + 4);
366 dev_err(&priv->spi->dev, "could not alloc skb");
370 if (len <= READAHEAD_SZ) {
371 memcpy(skb_put(skb, len), rx_head + 1, len);
373 memcpy(skb_put(skb, READAHEAD_SZ), rx_head + 1, READAHEAD_SZ);
374 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
375 skb_put(skb, len - READAHEAD_SZ),
379 /* Put additional bytes to compensate for the possible
380 * alignment-caused truncation */
383 if (p54_rx(priv->hw, skb) == 0)
390 static irqreturn_t p54spi_interrupt(int irq, void *config)
392 struct spi_device *spi = config;
393 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
395 ieee80211_queue_work(priv->hw, &priv->work);
400 static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
402 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
405 if (p54spi_wakeup(priv) < 0)
408 ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len);
412 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
413 SPI_HOST_INT_WR_READY)) {
414 dev_err(&priv->spi->dev, "WR_READY timeout\n");
419 p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
421 if (FREE_AFTER_TX(skb))
422 p54_free_skb(priv->hw, skb);
428 static int p54spi_wq_tx(struct p54s_priv *priv)
430 struct p54s_tx_info *entry;
432 struct ieee80211_tx_info *info;
433 struct p54_tx_info *minfo;
434 struct p54s_tx_info *dinfo;
438 spin_lock_irqsave(&priv->tx_lock, flags);
440 while (!list_empty(&priv->tx_pending)) {
441 entry = list_entry(priv->tx_pending.next,
442 struct p54s_tx_info, tx_list);
444 list_del_init(&entry->tx_list);
446 spin_unlock_irqrestore(&priv->tx_lock, flags);
448 dinfo = container_of((void *) entry, struct p54s_tx_info,
450 minfo = container_of((void *) dinfo, struct p54_tx_info,
452 info = container_of((void *) minfo, struct ieee80211_tx_info,
454 skb = container_of((void *) info, struct sk_buff, cb);
456 ret = p54spi_tx_frame(priv, skb);
459 p54_free_skb(priv->hw, skb);
463 spin_lock_irqsave(&priv->tx_lock, flags);
465 spin_unlock_irqrestore(&priv->tx_lock, flags);
469 static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
471 struct p54s_priv *priv = dev->priv;
472 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
473 struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
474 struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
477 BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
479 spin_lock_irqsave(&priv->tx_lock, flags);
480 list_add_tail(&di->tx_list, &priv->tx_pending);
481 spin_unlock_irqrestore(&priv->tx_lock, flags);
483 ieee80211_queue_work(priv->hw, &priv->work);
486 static void p54spi_work(struct work_struct *work)
488 struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
492 mutex_lock(&priv->mutex);
494 if (priv->fw_state == FW_STATE_OFF)
497 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
499 if (ints & SPI_HOST_INT_READY) {
500 p54spi_int_ready(priv);
501 p54spi_int_ack(priv, SPI_HOST_INT_READY);
504 if (priv->fw_state != FW_STATE_READY)
507 if (ints & SPI_HOST_INT_UPDATE) {
508 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
509 ret = p54spi_rx(priv);
513 if (ints & SPI_HOST_INT_SW_UPDATE) {
514 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
515 ret = p54spi_rx(priv);
520 ret = p54spi_wq_tx(priv);
522 mutex_unlock(&priv->mutex);
525 static int p54spi_op_start(struct ieee80211_hw *dev)
527 struct p54s_priv *priv = dev->priv;
528 unsigned long timeout;
531 if (mutex_lock_interruptible(&priv->mutex)) {
536 priv->fw_state = FW_STATE_BOOTING;
538 p54spi_power_on(priv);
540 ret = p54spi_upload_firmware(dev);
542 p54spi_power_off(priv);
546 mutex_unlock(&priv->mutex);
548 timeout = msecs_to_jiffies(2000);
549 timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
552 dev_err(&priv->spi->dev, "firmware boot failed");
553 p54spi_power_off(priv);
558 if (mutex_lock_interruptible(&priv->mutex)) {
560 p54spi_power_off(priv);
564 WARN_ON(priv->fw_state != FW_STATE_READY);
567 mutex_unlock(&priv->mutex);
573 static void p54spi_op_stop(struct ieee80211_hw *dev)
575 struct p54s_priv *priv = dev->priv;
578 if (mutex_lock_interruptible(&priv->mutex)) {
579 /* FIXME: how to handle this error? */
583 WARN_ON(priv->fw_state != FW_STATE_READY);
585 cancel_work_sync(&priv->work);
587 p54spi_power_off(priv);
588 spin_lock_irqsave(&priv->tx_lock, flags);
589 INIT_LIST_HEAD(&priv->tx_pending);
590 spin_unlock_irqrestore(&priv->tx_lock, flags);
592 priv->fw_state = FW_STATE_OFF;
593 mutex_unlock(&priv->mutex);
596 static int __devinit p54spi_probe(struct spi_device *spi)
598 struct p54s_priv *priv = NULL;
599 struct ieee80211_hw *hw;
602 hw = p54_init_common(sizeof(*priv));
604 dev_err(&spi->dev, "could not alloc ieee80211_hw");
610 dev_set_drvdata(&spi->dev, priv);
613 spi->bits_per_word = 16;
614 spi->max_speed_hz = 24000000;
616 ret = spi_setup(spi);
618 dev_err(&priv->spi->dev, "spi_setup failed");
619 goto err_free_common;
622 ret = gpio_request(p54spi_gpio_power, "p54spi power");
624 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
625 goto err_free_common;
628 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
630 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
631 goto err_free_common;
634 gpio_direction_output(p54spi_gpio_power, 0);
635 gpio_direction_input(p54spi_gpio_irq);
637 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
638 p54spi_interrupt, IRQF_DISABLED, "p54spi",
641 dev_err(&priv->spi->dev, "request_irq() failed");
642 goto err_free_common;
645 set_irq_type(gpio_to_irq(p54spi_gpio_irq),
646 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 SET_IEEE80211_DEV(hw, &spi->dev);
655 priv->common.open = p54spi_op_start;
656 priv->common.stop = p54spi_op_stop;
657 priv->common.tx = p54spi_op_tx;
659 ret = p54spi_request_firmware(hw);
661 goto err_free_common;
663 ret = p54spi_request_eeprom(hw);
665 goto err_free_common;
667 ret = p54_register_common(hw, &priv->spi->dev);
669 goto err_free_common;
674 p54_free_common(priv->hw);
678 static int __devexit p54spi_remove(struct spi_device *spi)
680 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
682 p54_unregister_common(priv->hw);
684 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
686 gpio_free(p54spi_gpio_power);
687 gpio_free(p54spi_gpio_irq);
688 release_firmware(priv->firmware);
690 mutex_destroy(&priv->mutex);
692 p54_free_common(priv->hw);
698 static struct spi_driver p54spi_driver = {
701 .bus = &spi_bus_type,
702 .owner = THIS_MODULE,
705 .probe = p54spi_probe,
706 .remove = __devexit_p(p54spi_remove),
709 static int __init p54spi_init(void)
713 ret = spi_register_driver(&p54spi_driver);
715 printk(KERN_ERR "failed to register SPI driver: %d", ret);
723 static void __exit p54spi_exit(void)
725 spi_unregister_driver(&p54spi_driver);
728 module_init(p54spi_init);
729 module_exit(p54spi_exit);
731 MODULE_LICENSE("GPL");
732 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
733 MODULE_ALIAS("spi:cx3110x");
734 MODULE_ALIAS("spi:p54spi");