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>
34 #include "p54spi_eeprom.h"
37 #include "p54common.h"
39 MODULE_FIRMWARE("3826.arm");
40 MODULE_ALIAS("stlc45xx");
43 * gpios should be handled in board files and provided via platform data,
44 * but because it's currently impossible for p54spi to have a header file
45 * in include/linux, let's use module paramaters for now
48 static int p54spi_gpio_power = 97;
49 module_param(p54spi_gpio_power, int, 0444);
50 MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
52 static int p54spi_gpio_irq = 87;
53 module_param(p54spi_gpio_irq, int, 0444);
54 MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
56 static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
57 void *buf, size_t len)
59 struct spi_transfer t[2];
63 /* We first push the address */
64 addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
67 memset(t, 0, sizeof(t));
70 t[0].len = sizeof(addr);
71 spi_message_add_tail(&t[0], &m);
75 spi_message_add_tail(&t[1], &m);
77 spi_sync(priv->spi, &m);
81 static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
82 const void *buf, size_t len)
84 struct spi_transfer t[3];
88 /* We first push the address */
89 addr = cpu_to_le16(address << 8);
92 memset(t, 0, sizeof(t));
95 t[0].len = sizeof(addr);
96 spi_message_add_tail(&t[0], &m);
100 spi_message_add_tail(&t[1], &m);
104 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
106 t[2].tx_buf = &last_word;
107 t[2].len = sizeof(last_word);
108 spi_message_add_tail(&t[2], &m);
111 spi_sync(priv->spi, &m);
114 static u16 p54spi_read16(struct p54s_priv *priv, u8 addr)
118 p54spi_spi_read(priv, addr, &val, sizeof(val));
120 return le16_to_cpu(val);
123 static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
127 p54spi_spi_read(priv, addr, &val, sizeof(val));
129 return le32_to_cpu(val);
132 static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
134 p54spi_spi_write(priv, addr, &val, sizeof(val));
137 static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
139 p54spi_spi_write(priv, addr, &val, sizeof(val));
142 struct p54spi_spi_reg {
143 u16 address; /* __le16 ? */
148 static const struct p54spi_spi_reg p54spi_registers_array[] =
150 { SPI_ADRS_ARM_INTERRUPTS, 32, "ARM_INT " },
151 { SPI_ADRS_ARM_INT_EN, 32, "ARM_INT_ENA " },
152 { SPI_ADRS_HOST_INTERRUPTS, 32, "HOST_INT " },
153 { SPI_ADRS_HOST_INT_EN, 32, "HOST_INT_ENA" },
154 { SPI_ADRS_HOST_INT_ACK, 32, "HOST_INT_ACK" },
155 { SPI_ADRS_GEN_PURP_1, 32, "GP1_COMM " },
156 { SPI_ADRS_GEN_PURP_2, 32, "GP2_COMM " },
157 { SPI_ADRS_DEV_CTRL_STAT, 32, "DEV_CTRL_STA" },
158 { SPI_ADRS_DMA_DATA, 16, "DMA_DATA " },
159 { SPI_ADRS_DMA_WRITE_CTRL, 16, "DMA_WR_CTRL " },
160 { SPI_ADRS_DMA_WRITE_LEN, 16, "DMA_WR_LEN " },
161 { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_WR_BASE " },
162 { SPI_ADRS_DMA_READ_CTRL, 16, "DMA_RD_CTRL " },
163 { SPI_ADRS_DMA_READ_LEN, 16, "DMA_RD_LEN " },
164 { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_RD_BASE " }
167 static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, __le32 bits)
172 for (i = 0; i < 2000; i++) {
173 p54spi_spi_read(priv, reg, &buffer, sizeof(buffer));
182 static int p54spi_request_firmware(struct ieee80211_hw *dev)
184 struct p54s_priv *priv = dev->priv;
187 /* FIXME: should driver use it's own struct device? */
188 ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
191 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
195 ret = p54_parse_firmware(dev, priv->firmware);
197 release_firmware(priv->firmware);
204 static int p54spi_request_eeprom(struct ieee80211_hw *dev)
206 struct p54s_priv *priv = dev->priv;
207 const struct firmware *eeprom;
211 * allow users to customize their eeprom.
214 ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev);
216 dev_info(&priv->spi->dev, "loading default eeprom...\n");
217 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
218 sizeof(p54spi_eeprom));
220 dev_info(&priv->spi->dev, "loading user eeprom...\n");
221 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
223 release_firmware(eeprom);
228 static int p54spi_upload_firmware(struct ieee80211_hw *dev)
230 struct p54s_priv *priv = dev->priv;
231 unsigned long fw_len, fw_addr;
234 /* stop the device */
235 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
236 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
237 SPI_CTRL_STAT_START_HALTED));
239 msleep(TARGET_BOOT_SLEEP);
241 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
242 SPI_CTRL_STAT_HOST_OVERRIDE |
243 SPI_CTRL_STAT_START_HALTED));
245 msleep(TARGET_BOOT_SLEEP);
247 fw_addr = ISL38XX_DEV_FIRMWARE_ADDR;
248 fw_len = priv->firmware->size;
251 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
253 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
254 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
256 if (p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL,
257 cpu_to_le32(HOST_ALLOWED)) == 0) {
258 dev_err(&priv->spi->dev, "fw_upload not allowed "
263 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN,
264 cpu_to_le16(_fw_len));
265 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE,
266 cpu_to_le32(fw_addr));
268 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA,
269 &priv->firmware->data, _fw_len);
274 /* FIXME: I think this doesn't work if firmware is large,
275 * this loop goes to second round. fw->data is not
276 * increased at all! */
281 /* enable host interrupts */
282 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
283 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
285 /* boot the device */
286 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
287 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
288 SPI_CTRL_STAT_RAM_BOOT));
290 msleep(TARGET_BOOT_SLEEP);
292 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
293 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
294 msleep(TARGET_BOOT_SLEEP);
298 static void p54spi_power_off(struct p54s_priv *priv)
300 disable_irq(gpio_to_irq(p54spi_gpio_irq));
301 gpio_set_value(p54spi_gpio_power, 0);
304 static void p54spi_power_on(struct p54s_priv *priv)
306 gpio_set_value(p54spi_gpio_power, 1);
307 enable_irq(gpio_to_irq(p54spi_gpio_irq));
310 * need to wait a while before device can be accessed, the lenght
316 static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
318 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
321 static void p54spi_wakeup(struct p54s_priv *priv)
323 unsigned long timeout;
327 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
328 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
330 /* And wait for the READY interrupt */
331 timeout = jiffies + HZ;
333 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
334 while (!(ints & SPI_HOST_INT_READY)) {
335 if (time_after(jiffies, timeout))
337 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
340 p54spi_int_ack(priv, SPI_HOST_INT_READY);
346 static inline void p54spi_sleep(struct p54s_priv *priv)
348 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
349 cpu_to_le32(SPI_TARGET_INT_SLEEP));
352 static void p54spi_int_ready(struct p54s_priv *priv)
354 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
355 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
357 switch (priv->fw_state) {
358 case FW_STATE_BOOTING:
359 priv->fw_state = FW_STATE_READY;
360 complete(&priv->fw_comp);
362 case FW_STATE_RESETTING:
363 priv->fw_state = FW_STATE_READY;
364 /* TODO: reinitialize state */
371 static int p54spi_rx(struct p54s_priv *priv)
378 /* dummy read to flush SPI DMA controller bug */
379 p54spi_read16(priv, SPI_ADRS_GEN_PURP_1);
381 len = p54spi_read16(priv, SPI_ADRS_DMA_DATA);
384 dev_err(&priv->spi->dev, "rx request of zero bytes");
388 skb = dev_alloc_skb(len);
390 dev_err(&priv->spi->dev, "could not alloc skb");
394 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, skb_put(skb, len), len);
397 if (p54_rx(priv->hw, skb) == 0)
404 static irqreturn_t p54spi_interrupt(int irq, void *config)
406 struct spi_device *spi = config;
407 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
409 queue_work(priv->hw->workqueue, &priv->work);
414 static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
416 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
417 struct p54s_dma_regs dma_regs;
418 unsigned long timeout;
424 dma_regs.cmd = cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE);
425 dma_regs.len = cpu_to_le16(skb->len);
426 dma_regs.addr = hdr->req_id;
428 p54spi_spi_write(priv, SPI_ADRS_DMA_WRITE_CTRL, &dma_regs,
431 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, skb->data, skb->len);
433 timeout = jiffies + 2 * HZ;
434 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
435 while (!(ints & SPI_HOST_INT_WR_READY)) {
436 if (time_after(jiffies, timeout)) {
437 dev_err(&priv->spi->dev, "WR_READY timeout");
441 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
444 p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
448 if (FREE_AFTER_TX(skb))
449 p54_free_skb(priv->hw, skb);
453 static int p54spi_wq_tx(struct p54s_priv *priv)
455 struct p54s_tx_info *entry;
457 struct ieee80211_tx_info *info;
458 struct p54_tx_info *minfo;
459 struct p54s_tx_info *dinfo;
463 spin_lock_irqsave(&priv->tx_lock, flags);
465 while (!list_empty(&priv->tx_pending)) {
466 entry = list_entry(priv->tx_pending.next,
467 struct p54s_tx_info, tx_list);
469 list_del_init(&entry->tx_list);
471 spin_unlock_irqrestore(&priv->tx_lock, flags);
473 dinfo = container_of((void *) entry, struct p54s_tx_info,
475 minfo = container_of((void *) dinfo, struct p54_tx_info,
477 info = container_of((void *) minfo, struct ieee80211_tx_info,
479 skb = container_of((void *) info, struct sk_buff, cb);
481 ret = p54spi_tx_frame(priv, skb);
484 p54_free_skb(priv->hw, skb);
488 spin_lock_irqsave(&priv->tx_lock, flags);
490 spin_unlock_irqrestore(&priv->tx_lock, flags);
494 static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
496 struct p54s_priv *priv = dev->priv;
497 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
498 struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
499 struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
502 BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
504 spin_lock_irqsave(&priv->tx_lock, flags);
505 list_add_tail(&di->tx_list, &priv->tx_pending);
506 spin_unlock_irqrestore(&priv->tx_lock, flags);
508 queue_work(priv->hw->workqueue, &priv->work);
511 static void p54spi_work(struct work_struct *work)
513 struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
517 mutex_lock(&priv->mutex);
519 if (priv->fw_state == FW_STATE_OFF &&
520 priv->fw_state == FW_STATE_RESET)
523 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
525 if (ints & SPI_HOST_INT_READY) {
526 p54spi_int_ready(priv);
527 p54spi_int_ack(priv, SPI_HOST_INT_READY);
530 if (priv->fw_state != FW_STATE_READY)
533 if (ints & SPI_HOST_INT_UPDATE) {
534 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
535 ret = p54spi_rx(priv);
539 if (ints & SPI_HOST_INT_SW_UPDATE) {
540 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
541 ret = p54spi_rx(priv);
546 ret = p54spi_wq_tx(priv);
550 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
553 mutex_unlock(&priv->mutex);
556 static int p54spi_op_start(struct ieee80211_hw *dev)
558 struct p54s_priv *priv = dev->priv;
559 unsigned long timeout;
562 if (mutex_lock_interruptible(&priv->mutex)) {
567 priv->fw_state = FW_STATE_BOOTING;
569 p54spi_power_on(priv);
571 ret = p54spi_upload_firmware(dev);
573 p54spi_power_off(priv);
577 mutex_unlock(&priv->mutex);
579 timeout = msecs_to_jiffies(2000);
580 timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
583 dev_err(&priv->spi->dev, "firmware boot failed");
584 p54spi_power_off(priv);
589 if (mutex_lock_interruptible(&priv->mutex)) {
591 p54spi_power_off(priv);
595 WARN_ON(priv->fw_state != FW_STATE_READY);
598 mutex_unlock(&priv->mutex);
604 static void p54spi_op_stop(struct ieee80211_hw *dev)
606 struct p54s_priv *priv = dev->priv;
609 if (mutex_lock_interruptible(&priv->mutex)) {
610 /* FIXME: how to handle this error? */
614 WARN_ON(priv->fw_state != FW_STATE_READY);
616 cancel_work_sync(&priv->work);
618 p54spi_power_off(priv);
619 spin_lock_irqsave(&priv->tx_lock, flags);
620 INIT_LIST_HEAD(&priv->tx_pending);
621 spin_unlock_irqrestore(&priv->tx_lock, flags);
623 priv->fw_state = FW_STATE_OFF;
624 mutex_unlock(&priv->mutex);
627 static int __devinit p54spi_probe(struct spi_device *spi)
629 struct p54s_priv *priv = NULL;
630 struct ieee80211_hw *hw;
633 hw = p54_init_common(sizeof(*priv));
635 dev_err(&priv->spi->dev, "could not alloc ieee80211_hw");
641 dev_set_drvdata(&spi->dev, priv);
644 spi->bits_per_word = 16;
645 spi->max_speed_hz = 24000000;
647 ret = spi_setup(spi);
649 dev_err(&priv->spi->dev, "spi_setup failed");
650 goto err_free_common;
653 ret = gpio_request(p54spi_gpio_power, "p54spi power");
655 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
656 goto err_free_common;
659 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
661 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
662 goto err_free_common;
665 gpio_direction_output(p54spi_gpio_power, 0);
666 gpio_direction_input(p54spi_gpio_irq);
668 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
669 p54spi_interrupt, IRQF_DISABLED, "p54spi",
672 dev_err(&priv->spi->dev, "request_irq() failed");
673 goto err_free_common;
676 set_irq_type(gpio_to_irq(p54spi_gpio_irq),
677 IRQ_TYPE_EDGE_RISING);
679 disable_irq(gpio_to_irq(p54spi_gpio_irq));
681 INIT_WORK(&priv->work, p54spi_work);
682 init_completion(&priv->fw_comp);
683 INIT_LIST_HEAD(&priv->tx_pending);
684 mutex_init(&priv->mutex);
685 SET_IEEE80211_DEV(hw, &spi->dev);
686 priv->common.open = p54spi_op_start;
687 priv->common.stop = p54spi_op_stop;
688 priv->common.tx = p54spi_op_tx;
690 ret = p54spi_request_firmware(hw);
692 goto err_free_common;
694 ret = p54spi_request_eeprom(hw);
696 goto err_free_common;
698 ret = p54_register_common(hw, &priv->spi->dev);
700 goto err_free_common;
705 p54_free_common(priv->hw);
709 static int __devexit p54spi_remove(struct spi_device *spi)
711 struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
713 ieee80211_unregister_hw(priv->hw);
715 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
717 gpio_free(p54spi_gpio_power);
718 gpio_free(p54spi_gpio_irq);
719 release_firmware(priv->firmware);
721 mutex_destroy(&priv->mutex);
723 p54_free_common(priv->hw);
724 ieee80211_free_hw(priv->hw);
730 static struct spi_driver p54spi_driver = {
732 /* use cx3110x name because board-n800.c uses that for the
735 .bus = &spi_bus_type,
736 .owner = THIS_MODULE,
739 .probe = p54spi_probe,
740 .remove = __devexit_p(p54spi_remove),
743 static int __init p54spi_init(void)
747 ret = spi_register_driver(&p54spi_driver);
749 printk(KERN_ERR "failed to register SPI driver: %d", ret);
757 static void __exit p54spi_exit(void)
759 spi_unregister_driver(&p54spi_driver);
762 module_init(p54spi_init);
763 module_exit(p54spi_exit);
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");