drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / net / wireless / intersil / p54 / p54spi.c
blob27f44a9f0bc1f90e6c43d71e792ee95ccd4ce1b9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2008 Christian Lamparter <chunkeey@web.de>
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6 * This driver is a port from stlc45xx:
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
8 */
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/interrupt.h>
13 #include <linux/firmware.h>
14 #include <linux/delay.h>
15 #include <linux/irq.h>
16 #include <linux/spi/spi.h>
17 #include <linux/etherdevice.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
21 #include "p54spi.h"
22 #include "p54.h"
24 #include "lmac.h"
26 #ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
27 #include "p54spi_eeprom.h"
28 #endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
30 MODULE_FIRMWARE("3826.arm");
31 MODULE_FIRMWARE("3826.eeprom");
33 /* gpios should be handled in board files and provided via platform data,
34 * but because it's currently impossible for p54spi to have a header file
35 * in include/linux, let's use module paramaters for now
38 static int p54spi_gpio_power = 97;
39 module_param(p54spi_gpio_power, int, 0444);
40 MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
42 static int p54spi_gpio_irq = 87;
43 module_param(p54spi_gpio_irq, int, 0444);
44 MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
46 static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
47 void *buf, size_t len)
49 struct spi_transfer t[2];
50 struct spi_message m;
51 __le16 addr;
53 /* We first push the address */
54 addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
56 spi_message_init(&m);
57 memset(t, 0, sizeof(t));
59 t[0].tx_buf = &addr;
60 t[0].len = sizeof(addr);
61 spi_message_add_tail(&t[0], &m);
63 t[1].rx_buf = buf;
64 t[1].len = len;
65 spi_message_add_tail(&t[1], &m);
67 spi_sync(priv->spi, &m);
71 static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
72 const void *buf, size_t len)
74 struct spi_transfer t[3];
75 struct spi_message m;
76 __le16 addr;
78 /* We first push the address */
79 addr = cpu_to_le16(address << 8);
81 spi_message_init(&m);
82 memset(t, 0, sizeof(t));
84 t[0].tx_buf = &addr;
85 t[0].len = sizeof(addr);
86 spi_message_add_tail(&t[0], &m);
88 t[1].tx_buf = buf;
89 t[1].len = len & ~1;
90 spi_message_add_tail(&t[1], &m);
92 if (len % 2) {
93 __le16 last_word;
94 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
96 t[2].tx_buf = &last_word;
97 t[2].len = sizeof(last_word);
98 spi_message_add_tail(&t[2], &m);
101 spi_sync(priv->spi, &m);
104 static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
106 __le32 val;
108 p54spi_spi_read(priv, addr, &val, sizeof(val));
110 return le32_to_cpu(val);
113 static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
115 p54spi_spi_write(priv, addr, &val, sizeof(val));
118 static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
120 p54spi_spi_write(priv, addr, &val, sizeof(val));
123 static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, u32 bits)
125 int i;
127 for (i = 0; i < 2000; i++) {
128 u32 buffer = p54spi_read32(priv, reg);
129 if ((buffer & bits) == bits)
130 return 1;
132 return 0;
135 static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base,
136 const void *buf, size_t len)
138 if (!p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL, HOST_ALLOWED)) {
139 dev_err(&priv->spi->dev, "spi_write_dma not allowed "
140 "to DMA write.\n");
141 return -EAGAIN;
144 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
145 cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
147 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len));
148 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base);
149 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len);
150 return 0;
153 static int p54spi_request_firmware(struct ieee80211_hw *dev)
155 struct p54s_priv *priv = dev->priv;
156 int ret;
158 /* FIXME: should driver use it's own struct device? */
159 ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
161 if (ret < 0) {
162 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
163 return ret;
166 ret = p54_parse_firmware(dev, priv->firmware);
167 if (ret) {
168 /* the firmware is released by the caller */
169 return ret;
172 return 0;
175 static int p54spi_request_eeprom(struct ieee80211_hw *dev)
177 struct p54s_priv *priv = dev->priv;
178 const struct firmware *eeprom;
179 int ret;
181 /* allow users to customize their eeprom.
184 ret = request_firmware_direct(&eeprom, "3826.eeprom", &priv->spi->dev);
185 if (ret < 0) {
186 #ifdef CONFIG_P54_SPI_DEFAULT_EEPROM
187 dev_info(&priv->spi->dev, "loading default eeprom...\n");
188 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
189 sizeof(p54spi_eeprom));
190 #else
191 dev_err(&priv->spi->dev, "Failed to request user eeprom\n");
192 #endif /* CONFIG_P54_SPI_DEFAULT_EEPROM */
193 } else {
194 dev_info(&priv->spi->dev, "loading user eeprom...\n");
195 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
196 (int)eeprom->size);
197 release_firmware(eeprom);
199 return ret;
202 static int p54spi_upload_firmware(struct ieee80211_hw *dev)
204 struct p54s_priv *priv = dev->priv;
205 unsigned long fw_len, _fw_len;
206 unsigned int offset = 0;
207 int err = 0;
208 u8 *fw;
210 fw_len = priv->firmware->size;
211 fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
212 if (!fw)
213 return -ENOMEM;
215 /* stop the device */
216 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
217 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
218 SPI_CTRL_STAT_START_HALTED));
220 msleep(TARGET_BOOT_SLEEP);
222 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
223 SPI_CTRL_STAT_HOST_OVERRIDE |
224 SPI_CTRL_STAT_START_HALTED));
226 msleep(TARGET_BOOT_SLEEP);
228 while (fw_len > 0) {
229 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
231 err = p54spi_spi_write_dma(priv, cpu_to_le32(
232 ISL38XX_DEV_FIRMWARE_ADDR + offset),
233 (fw + offset), _fw_len);
234 if (err < 0)
235 goto out;
237 fw_len -= _fw_len;
238 offset += _fw_len;
241 BUG_ON(fw_len != 0);
243 /* enable host interrupts */
244 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
245 cpu_to_le32(SPI_HOST_INTS_DEFAULT));
247 /* boot the device */
248 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
249 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
250 SPI_CTRL_STAT_RAM_BOOT));
252 msleep(TARGET_BOOT_SLEEP);
254 p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
255 SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
256 msleep(TARGET_BOOT_SLEEP);
258 out:
259 kfree(fw);
260 return err;
263 static void p54spi_power_off(struct p54s_priv *priv)
265 disable_irq(gpio_to_irq(p54spi_gpio_irq));
266 gpio_set_value(p54spi_gpio_power, 0);
269 static void p54spi_power_on(struct p54s_priv *priv)
271 gpio_set_value(p54spi_gpio_power, 1);
272 enable_irq(gpio_to_irq(p54spi_gpio_irq));
274 /* need to wait a while before device can be accessed, the length
275 * is just a guess
277 msleep(10);
280 static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
282 p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
285 static int p54spi_wakeup(struct p54s_priv *priv)
287 /* wake the chip */
288 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
289 cpu_to_le32(SPI_TARGET_INT_WAKEUP));
291 /* And wait for the READY interrupt */
292 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
293 SPI_HOST_INT_READY)) {
294 dev_err(&priv->spi->dev, "INT_READY timeout\n");
295 return -EBUSY;
298 p54spi_int_ack(priv, SPI_HOST_INT_READY);
299 return 0;
302 static inline void p54spi_sleep(struct p54s_priv *priv)
304 p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
305 cpu_to_le32(SPI_TARGET_INT_SLEEP));
308 static void p54spi_int_ready(struct p54s_priv *priv)
310 p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
311 SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
313 switch (priv->fw_state) {
314 case FW_STATE_BOOTING:
315 priv->fw_state = FW_STATE_READY;
316 complete(&priv->fw_comp);
317 break;
318 case FW_STATE_RESETTING:
319 priv->fw_state = FW_STATE_READY;
320 /* TODO: reinitialize state */
321 break;
322 default:
323 break;
327 static int p54spi_rx(struct p54s_priv *priv)
329 struct sk_buff *skb;
330 u16 len;
331 u16 rx_head[2];
332 #define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
334 if (p54spi_wakeup(priv) < 0)
335 return -EBUSY;
337 /* Read data size and first data word in one SPI transaction
338 * This is workaround for firmware/DMA bug,
339 * when first data word gets lost under high load.
341 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
342 len = rx_head[0];
344 if (len == 0) {
345 p54spi_sleep(priv);
346 dev_err(&priv->spi->dev, "rx request of zero bytes\n");
347 return 0;
350 /* Firmware may insert up to 4 padding bytes after the lmac header,
351 * but it does not amend the size of SPI data transfer.
352 * Such packets has correct data size in header, thus referencing
353 * past the end of allocated skb. Reserve extra 4 bytes for this case
355 skb = dev_alloc_skb(len + 4);
356 if (!skb) {
357 p54spi_sleep(priv);
358 dev_err(&priv->spi->dev, "could not alloc skb");
359 return -ENOMEM;
362 if (len <= READAHEAD_SZ) {
363 skb_put_data(skb, rx_head + 1, len);
364 } else {
365 skb_put_data(skb, rx_head + 1, READAHEAD_SZ);
366 p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
367 skb_put(skb, len - READAHEAD_SZ),
368 len - READAHEAD_SZ);
370 p54spi_sleep(priv);
371 /* Put additional bytes to compensate for the possible
372 * alignment-caused truncation
374 skb_put(skb, 4);
376 if (p54_rx(priv->hw, skb) == 0)
377 dev_kfree_skb(skb);
379 return 0;
383 static irqreturn_t p54spi_interrupt(int irq, void *config)
385 struct spi_device *spi = config;
386 struct p54s_priv *priv = spi_get_drvdata(spi);
388 ieee80211_queue_work(priv->hw, &priv->work);
390 return IRQ_HANDLED;
393 static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
395 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
396 int ret = 0;
398 if (p54spi_wakeup(priv) < 0)
399 return -EBUSY;
401 ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len);
402 if (ret < 0)
403 goto out;
405 if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
406 SPI_HOST_INT_WR_READY)) {
407 dev_err(&priv->spi->dev, "WR_READY timeout\n");
408 ret = -EAGAIN;
409 goto out;
412 p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
414 if (FREE_AFTER_TX(skb))
415 p54_free_skb(priv->hw, skb);
416 out:
417 p54spi_sleep(priv);
418 return ret;
421 static int p54spi_wq_tx(struct p54s_priv *priv)
423 struct p54s_tx_info *entry;
424 struct sk_buff *skb;
425 struct ieee80211_tx_info *info;
426 struct p54_tx_info *minfo;
427 struct p54s_tx_info *dinfo;
428 unsigned long flags;
429 int ret = 0;
431 spin_lock_irqsave(&priv->tx_lock, flags);
433 while (!list_empty(&priv->tx_pending)) {
434 entry = list_entry(priv->tx_pending.next,
435 struct p54s_tx_info, tx_list);
437 list_del_init(&entry->tx_list);
439 spin_unlock_irqrestore(&priv->tx_lock, flags);
441 dinfo = container_of((void *) entry, struct p54s_tx_info,
442 tx_list);
443 minfo = container_of((void *) dinfo, struct p54_tx_info,
444 data);
445 info = container_of((void *) minfo, struct ieee80211_tx_info,
446 rate_driver_data);
447 skb = container_of((void *) info, struct sk_buff, cb);
449 ret = p54spi_tx_frame(priv, skb);
451 if (ret < 0) {
452 p54_free_skb(priv->hw, skb);
453 return ret;
456 spin_lock_irqsave(&priv->tx_lock, flags);
458 spin_unlock_irqrestore(&priv->tx_lock, flags);
459 return ret;
462 static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
464 struct p54s_priv *priv = dev->priv;
465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
466 struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
467 struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
468 unsigned long flags;
470 BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
472 spin_lock_irqsave(&priv->tx_lock, flags);
473 list_add_tail(&di->tx_list, &priv->tx_pending);
474 spin_unlock_irqrestore(&priv->tx_lock, flags);
476 ieee80211_queue_work(priv->hw, &priv->work);
479 static void p54spi_work(struct work_struct *work)
481 struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
482 u32 ints;
483 int ret;
485 mutex_lock(&priv->mutex);
487 if (priv->fw_state == FW_STATE_OFF)
488 goto out;
490 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
492 if (ints & SPI_HOST_INT_READY) {
493 p54spi_int_ready(priv);
494 p54spi_int_ack(priv, SPI_HOST_INT_READY);
497 if (priv->fw_state != FW_STATE_READY)
498 goto out;
500 if (ints & SPI_HOST_INT_UPDATE) {
501 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
502 ret = p54spi_rx(priv);
503 if (ret < 0)
504 goto out;
506 if (ints & SPI_HOST_INT_SW_UPDATE) {
507 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
508 ret = p54spi_rx(priv);
509 if (ret < 0)
510 goto out;
513 ret = p54spi_wq_tx(priv);
514 out:
515 mutex_unlock(&priv->mutex);
518 static int p54spi_op_start(struct ieee80211_hw *dev)
520 struct p54s_priv *priv = dev->priv;
521 long time_left;
522 int ret = 0;
524 if (mutex_lock_interruptible(&priv->mutex)) {
525 ret = -EINTR;
526 goto out;
529 priv->fw_state = FW_STATE_BOOTING;
531 p54spi_power_on(priv);
533 ret = p54spi_upload_firmware(dev);
534 if (ret < 0) {
535 p54spi_power_off(priv);
536 goto out_unlock;
539 mutex_unlock(&priv->mutex);
541 time_left = msecs_to_jiffies(2000);
542 time_left = wait_for_completion_interruptible_timeout(&priv->fw_comp,
543 time_left);
544 if (!time_left) {
545 dev_err(&priv->spi->dev, "firmware boot failed");
546 p54spi_power_off(priv);
547 ret = -1;
548 goto out;
551 if (mutex_lock_interruptible(&priv->mutex)) {
552 ret = -EINTR;
553 p54spi_power_off(priv);
554 goto out;
557 WARN_ON(priv->fw_state != FW_STATE_READY);
559 out_unlock:
560 mutex_unlock(&priv->mutex);
562 out:
563 return ret;
566 static void p54spi_op_stop(struct ieee80211_hw *dev)
568 struct p54s_priv *priv = dev->priv;
569 unsigned long flags;
571 mutex_lock(&priv->mutex);
572 WARN_ON(priv->fw_state != FW_STATE_READY);
574 p54spi_power_off(priv);
575 spin_lock_irqsave(&priv->tx_lock, flags);
576 INIT_LIST_HEAD(&priv->tx_pending);
577 spin_unlock_irqrestore(&priv->tx_lock, flags);
579 priv->fw_state = FW_STATE_OFF;
580 mutex_unlock(&priv->mutex);
582 cancel_work_sync(&priv->work);
585 static int p54spi_probe(struct spi_device *spi)
587 struct p54s_priv *priv = NULL;
588 struct ieee80211_hw *hw;
589 int ret = -EINVAL;
591 hw = p54_init_common(sizeof(*priv));
592 if (!hw) {
593 dev_err(&spi->dev, "could not alloc ieee80211_hw");
594 return -ENOMEM;
597 priv = hw->priv;
598 priv->hw = hw;
599 spi_set_drvdata(spi, priv);
600 priv->spi = spi;
602 spi->bits_per_word = 16;
603 spi->max_speed_hz = 24000000;
605 ret = spi_setup(spi);
606 if (ret < 0) {
607 dev_err(&priv->spi->dev, "spi_setup failed");
608 goto err_free;
611 ret = gpio_request(p54spi_gpio_power, "p54spi power");
612 if (ret < 0) {
613 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
614 goto err_free;
617 ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
618 if (ret < 0) {
619 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
620 goto err_free_gpio_power;
623 gpio_direction_output(p54spi_gpio_power, 0);
624 gpio_direction_input(p54spi_gpio_irq);
626 ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
627 p54spi_interrupt, IRQF_NO_AUTOEN, "p54spi",
628 priv->spi);
629 if (ret < 0) {
630 dev_err(&priv->spi->dev, "request_irq() failed");
631 goto err_free_gpio_irq;
634 irq_set_irq_type(gpio_to_irq(p54spi_gpio_irq), IRQ_TYPE_EDGE_RISING);
636 INIT_WORK(&priv->work, p54spi_work);
637 init_completion(&priv->fw_comp);
638 INIT_LIST_HEAD(&priv->tx_pending);
639 mutex_init(&priv->mutex);
640 spin_lock_init(&priv->tx_lock);
641 SET_IEEE80211_DEV(hw, &spi->dev);
642 priv->common.open = p54spi_op_start;
643 priv->common.stop = p54spi_op_stop;
644 priv->common.tx = p54spi_op_tx;
646 ret = p54spi_request_firmware(hw);
647 if (ret < 0)
648 goto err_free_common;
650 ret = p54spi_request_eeprom(hw);
651 if (ret)
652 goto err_free_common;
654 ret = p54_register_common(hw, &priv->spi->dev);
655 if (ret)
656 goto err_free_common;
658 return 0;
660 err_free_common:
661 release_firmware(priv->firmware);
662 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
663 err_free_gpio_irq:
664 gpio_free(p54spi_gpio_irq);
665 err_free_gpio_power:
666 gpio_free(p54spi_gpio_power);
667 err_free:
668 p54_free_common(priv->hw);
669 return ret;
672 static void p54spi_remove(struct spi_device *spi)
674 struct p54s_priv *priv = spi_get_drvdata(spi);
676 p54_unregister_common(priv->hw);
678 free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
680 gpio_free(p54spi_gpio_power);
681 gpio_free(p54spi_gpio_irq);
682 release_firmware(priv->firmware);
684 mutex_destroy(&priv->mutex);
686 p54_free_common(priv->hw);
690 static struct spi_driver p54spi_driver = {
691 .driver = {
692 .name = "p54spi",
695 .probe = p54spi_probe,
696 .remove = p54spi_remove,
699 module_spi_driver(p54spi_driver);
701 MODULE_DESCRIPTION("Prism54 SPI wireless driver");
702 MODULE_LICENSE("GPL");
703 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
704 MODULE_ALIAS("spi:cx3110x");
705 MODULE_ALIAS("spi:p54spi");
706 MODULE_ALIAS("spi:stlc45xx");