3 * Linux device driver for PCI based Prism54
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6 * Copyright (c) 2008, Christian Lamparter <chunkeey@web.de>
8 * Based on the islsm (softmac prism54) driver, which is:
9 * Copyright 2004-2006 Jean-Baptiste Note <jean-baptiste.note@m4x.org>, et al.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/firmware.h>
19 #include <linux/etherdevice.h>
20 #include <linux/delay.h>
21 #include <linux/completion.h>
22 #include <net/mac80211.h>
28 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
29 MODULE_DESCRIPTION("Prism54 PCI wireless driver");
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS("prism54pci");
32 MODULE_FIRMWARE("isl3886pci");
34 static DEFINE_PCI_DEVICE_TABLE(p54p_table) = {
35 /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
36 { PCI_DEVICE(0x1260, 0x3890) },
37 /* 3COM 3CRWE154G72 Wireless LAN adapter */
38 { PCI_DEVICE(0x10b7, 0x6001) },
39 /* Intersil PRISM Indigo Wireless LAN adapter */
40 { PCI_DEVICE(0x1260, 0x3877) },
41 /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
42 { PCI_DEVICE(0x1260, 0x3886) },
46 MODULE_DEVICE_TABLE(pci, p54p_table);
48 static int p54p_upload_firmware(struct ieee80211_hw *dev)
50 struct p54p_priv *priv = dev->priv;
54 u32 remains, left, device_addr;
56 P54P_WRITE(int_enable, cpu_to_le32(0));
57 P54P_READ(int_enable);
60 reg = P54P_READ(ctrl_stat);
61 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
62 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
63 P54P_WRITE(ctrl_stat, reg);
67 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
68 P54P_WRITE(ctrl_stat, reg);
72 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
73 P54P_WRITE(ctrl_stat, reg);
76 /* wait for the firmware to reset properly */
79 err = p54_parse_firmware(dev, priv->firmware);
83 if (priv->common.fw_interface != FW_LM86) {
84 dev_err(&priv->pdev->dev, "wrong firmware, "
85 "please get a LM86(PCI) firmware a try again.\n");
89 data = (__le32 *) priv->firmware->data;
90 remains = priv->firmware->size;
91 device_addr = ISL38XX_DEV_FIRMWARE_ADDR;
94 left = min((u32)0x1000, remains);
95 P54P_WRITE(direct_mem_base, cpu_to_le32(device_addr));
96 P54P_READ(int_enable);
98 device_addr += 0x1000;
100 P54P_WRITE(direct_mem_win[i], *data++);
105 P54P_READ(int_enable);
108 reg = P54P_READ(ctrl_stat);
109 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
110 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
111 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
112 P54P_WRITE(ctrl_stat, reg);
113 P54P_READ(ctrl_stat);
116 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
117 P54P_WRITE(ctrl_stat, reg);
121 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
122 P54P_WRITE(ctrl_stat, reg);
126 /* wait for the firmware to boot properly */
132 static void p54p_refill_rx_ring(struct ieee80211_hw *dev,
133 int ring_index, struct p54p_desc *ring, u32 ring_limit,
134 struct sk_buff **rx_buf, u32 index)
136 struct p54p_priv *priv = dev->priv;
137 struct p54p_ring_control *ring_control = priv->ring_control;
140 idx = le32_to_cpu(ring_control->host_idx[ring_index]);
143 limit = ring_limit - limit;
145 i = idx % ring_limit;
146 while (limit-- > 1) {
147 struct p54p_desc *desc = &ring[i];
149 if (!desc->host_addr) {
152 skb = dev_alloc_skb(priv->common.rx_mtu + 32);
156 mapping = pci_map_single(priv->pdev,
157 skb_tail_pointer(skb),
158 priv->common.rx_mtu + 32,
161 if (pci_dma_mapping_error(priv->pdev, mapping)) {
162 dev_kfree_skb_any(skb);
163 dev_err(&priv->pdev->dev,
164 "RX DMA Mapping error\n");
168 desc->host_addr = cpu_to_le32(mapping);
169 desc->device_addr = 0; // FIXME: necessary?
170 desc->len = cpu_to_le16(priv->common.rx_mtu + 32);
181 ring_control->host_idx[ring_index] = cpu_to_le32(idx);
184 static void p54p_check_rx_ring(struct ieee80211_hw *dev, u32 *index,
185 int ring_index, struct p54p_desc *ring, u32 ring_limit,
186 struct sk_buff **rx_buf)
188 struct p54p_priv *priv = dev->priv;
189 struct p54p_ring_control *ring_control = priv->ring_control;
190 struct p54p_desc *desc;
193 i = (*index) % ring_limit;
194 (*index) = idx = le32_to_cpu(ring_control->device_idx[ring_index]);
200 len = le16_to_cpu(desc->len);
209 if (unlikely(len > priv->common.rx_mtu)) {
211 dev_err(&priv->pdev->dev, "rx'd frame size "
212 "exceeds length threshold.\n");
214 len = priv->common.rx_mtu;
218 if (p54_rx(dev, skb)) {
219 pci_unmap_single(priv->pdev,
220 le32_to_cpu(desc->host_addr),
221 priv->common.rx_mtu + 32,
227 desc->len = cpu_to_le16(priv->common.rx_mtu + 32);
234 p54p_refill_rx_ring(dev, ring_index, ring, ring_limit, rx_buf, *index);
237 static void p54p_check_tx_ring(struct ieee80211_hw *dev, u32 *index,
238 int ring_index, struct p54p_desc *ring, u32 ring_limit,
239 struct sk_buff **tx_buf)
241 struct p54p_priv *priv = dev->priv;
242 struct p54p_ring_control *ring_control = priv->ring_control;
243 struct p54p_desc *desc;
247 i = (*index) % ring_limit;
248 (*index) = idx = le32_to_cpu(ring_control->device_idx[ring_index]);
257 pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
258 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);
261 desc->device_addr = 0;
265 if (skb && FREE_AFTER_TX(skb))
266 p54_free_skb(dev, skb);
273 static void p54p_tasklet(unsigned long dev_id)
275 struct ieee80211_hw *dev = (struct ieee80211_hw *)dev_id;
276 struct p54p_priv *priv = dev->priv;
277 struct p54p_ring_control *ring_control = priv->ring_control;
279 p54p_check_tx_ring(dev, &priv->tx_idx_mgmt, 3, ring_control->tx_mgmt,
280 ARRAY_SIZE(ring_control->tx_mgmt),
283 p54p_check_tx_ring(dev, &priv->tx_idx_data, 1, ring_control->tx_data,
284 ARRAY_SIZE(ring_control->tx_data),
287 p54p_check_rx_ring(dev, &priv->rx_idx_mgmt, 2, ring_control->rx_mgmt,
288 ARRAY_SIZE(ring_control->rx_mgmt), priv->rx_buf_mgmt);
290 p54p_check_rx_ring(dev, &priv->rx_idx_data, 0, ring_control->rx_data,
291 ARRAY_SIZE(ring_control->rx_data), priv->rx_buf_data);
294 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
297 static irqreturn_t p54p_interrupt(int irq, void *dev_id)
299 struct ieee80211_hw *dev = dev_id;
300 struct p54p_priv *priv = dev->priv;
303 reg = P54P_READ(int_ident);
304 if (unlikely(reg == cpu_to_le32(0xFFFFFFFF))) {
307 P54P_WRITE(int_ack, reg);
309 reg &= P54P_READ(int_enable);
311 if (reg & cpu_to_le32(ISL38XX_INT_IDENT_UPDATE))
312 tasklet_schedule(&priv->tasklet);
313 else if (reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT))
314 complete(&priv->boot_comp);
317 return reg ? IRQ_HANDLED : IRQ_NONE;
320 static void p54p_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
323 struct p54p_priv *priv = dev->priv;
324 struct p54p_ring_control *ring_control = priv->ring_control;
325 struct p54p_desc *desc;
327 u32 device_idx, idx, i;
329 spin_lock_irqsave(&priv->lock, flags);
330 device_idx = le32_to_cpu(ring_control->device_idx[1]);
331 idx = le32_to_cpu(ring_control->host_idx[1]);
332 i = idx % ARRAY_SIZE(ring_control->tx_data);
334 mapping = pci_map_single(priv->pdev, skb->data, skb->len,
336 if (pci_dma_mapping_error(priv->pdev, mapping)) {
337 spin_unlock_irqrestore(&priv->lock, flags);
338 p54_free_skb(dev, skb);
339 dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
342 priv->tx_buf_data[i] = skb;
344 desc = &ring_control->tx_data[i];
345 desc->host_addr = cpu_to_le32(mapping);
346 desc->device_addr = ((struct p54_hdr *)skb->data)->req_id;
347 desc->len = cpu_to_le16(skb->len);
351 ring_control->host_idx[1] = cpu_to_le32(idx + 1);
352 spin_unlock_irqrestore(&priv->lock, flags);
354 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
358 static void p54p_stop(struct ieee80211_hw *dev)
360 struct p54p_priv *priv = dev->priv;
361 struct p54p_ring_control *ring_control = priv->ring_control;
363 struct p54p_desc *desc;
365 P54P_WRITE(int_enable, cpu_to_le32(0));
366 P54P_READ(int_enable);
369 free_irq(priv->pdev->irq, dev);
371 tasklet_kill(&priv->tasklet);
373 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
375 for (i = 0; i < ARRAY_SIZE(priv->rx_buf_data); i++) {
376 desc = &ring_control->rx_data[i];
378 pci_unmap_single(priv->pdev,
379 le32_to_cpu(desc->host_addr),
380 priv->common.rx_mtu + 32,
382 kfree_skb(priv->rx_buf_data[i]);
383 priv->rx_buf_data[i] = NULL;
386 for (i = 0; i < ARRAY_SIZE(priv->rx_buf_mgmt); i++) {
387 desc = &ring_control->rx_mgmt[i];
389 pci_unmap_single(priv->pdev,
390 le32_to_cpu(desc->host_addr),
391 priv->common.rx_mtu + 32,
393 kfree_skb(priv->rx_buf_mgmt[i]);
394 priv->rx_buf_mgmt[i] = NULL;
397 for (i = 0; i < ARRAY_SIZE(priv->tx_buf_data); i++) {
398 desc = &ring_control->tx_data[i];
400 pci_unmap_single(priv->pdev,
401 le32_to_cpu(desc->host_addr),
402 le16_to_cpu(desc->len),
405 p54_free_skb(dev, priv->tx_buf_data[i]);
406 priv->tx_buf_data[i] = NULL;
409 for (i = 0; i < ARRAY_SIZE(priv->tx_buf_mgmt); i++) {
410 desc = &ring_control->tx_mgmt[i];
412 pci_unmap_single(priv->pdev,
413 le32_to_cpu(desc->host_addr),
414 le16_to_cpu(desc->len),
417 p54_free_skb(dev, priv->tx_buf_mgmt[i]);
418 priv->tx_buf_mgmt[i] = NULL;
421 memset(ring_control, 0, sizeof(*ring_control));
424 static int p54p_open(struct ieee80211_hw *dev)
426 struct p54p_priv *priv = dev->priv;
429 init_completion(&priv->boot_comp);
430 err = request_irq(priv->pdev->irq, p54p_interrupt,
431 IRQF_SHARED, "p54pci", dev);
433 dev_err(&priv->pdev->dev, "failed to register IRQ handler\n");
437 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
438 err = p54p_upload_firmware(dev);
440 free_irq(priv->pdev->irq, dev);
443 priv->rx_idx_data = priv->tx_idx_data = 0;
444 priv->rx_idx_mgmt = priv->tx_idx_mgmt = 0;
446 p54p_refill_rx_ring(dev, 0, priv->ring_control->rx_data,
447 ARRAY_SIZE(priv->ring_control->rx_data), priv->rx_buf_data, 0);
449 p54p_refill_rx_ring(dev, 2, priv->ring_control->rx_mgmt,
450 ARRAY_SIZE(priv->ring_control->rx_mgmt), priv->rx_buf_mgmt, 0);
452 P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
453 P54P_READ(ring_control_base);
457 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
458 P54P_READ(int_enable);
462 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
465 if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
466 printk(KERN_ERR "%s: Cannot boot firmware!\n",
467 wiphy_name(dev->wiphy));
472 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
473 P54P_READ(int_enable);
477 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
485 static int __devinit p54p_probe(struct pci_dev *pdev,
486 const struct pci_device_id *id)
488 struct p54p_priv *priv;
489 struct ieee80211_hw *dev;
490 unsigned long mem_addr, mem_len;
493 err = pci_enable_device(pdev);
495 dev_err(&pdev->dev, "Cannot enable new PCI device\n");
499 mem_addr = pci_resource_start(pdev, 0);
500 mem_len = pci_resource_len(pdev, 0);
501 if (mem_len < sizeof(struct p54p_csr)) {
502 dev_err(&pdev->dev, "Too short PCI resources\n");
503 goto err_disable_dev;
506 err = pci_request_regions(pdev, "p54pci");
508 dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
509 goto err_disable_dev;
512 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) ||
513 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
514 dev_err(&pdev->dev, "No suitable DMA available\n");
518 pci_set_master(pdev);
519 pci_try_set_mwi(pdev);
521 pci_write_config_byte(pdev, 0x40, 0);
522 pci_write_config_byte(pdev, 0x41, 0);
524 dev = p54_init_common(sizeof(*priv));
526 dev_err(&pdev->dev, "ieee80211 alloc failed\n");
534 SET_IEEE80211_DEV(dev, &pdev->dev);
535 pci_set_drvdata(pdev, dev);
537 priv->map = ioremap(mem_addr, mem_len);
539 dev_err(&pdev->dev, "Cannot map device memory\n");
544 priv->ring_control = pci_alloc_consistent(pdev, sizeof(*priv->ring_control),
545 &priv->ring_control_dma);
546 if (!priv->ring_control) {
547 dev_err(&pdev->dev, "Cannot allocate rings\n");
551 priv->common.open = p54p_open;
552 priv->common.stop = p54p_stop;
553 priv->common.tx = p54p_tx;
555 spin_lock_init(&priv->lock);
556 tasklet_init(&priv->tasklet, p54p_tasklet, (unsigned long)dev);
558 err = request_firmware(&priv->firmware, "isl3886pci",
561 dev_err(&pdev->dev, "Cannot find firmware (isl3886pci)\n");
562 err = request_firmware(&priv->firmware, "isl3886",
565 goto err_free_common;
568 err = p54p_open(dev);
570 goto err_free_common;
571 err = p54_read_eeprom(dev);
574 goto err_free_common;
576 err = p54_register_common(dev, &pdev->dev);
578 goto err_free_common;
583 release_firmware(priv->firmware);
584 pci_free_consistent(pdev, sizeof(*priv->ring_control),
585 priv->ring_control, priv->ring_control_dma);
591 pci_set_drvdata(pdev, NULL);
592 p54_free_common(dev);
595 pci_release_regions(pdev);
597 pci_disable_device(pdev);
601 static void __devexit p54p_remove(struct pci_dev *pdev)
603 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
604 struct p54p_priv *priv;
609 p54_unregister_common(dev);
611 release_firmware(priv->firmware);
612 pci_free_consistent(pdev, sizeof(*priv->ring_control),
613 priv->ring_control, priv->ring_control_dma);
615 pci_release_regions(pdev);
616 pci_disable_device(pdev);
617 p54_free_common(dev);
621 static int p54p_suspend(struct pci_dev *pdev, pm_message_t state)
623 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
624 struct p54p_priv *priv = dev->priv;
626 if (priv->common.mode != NL80211_IFTYPE_UNSPECIFIED) {
627 ieee80211_stop_queues(dev);
631 pci_save_state(pdev);
632 pci_set_power_state(pdev, pci_choose_state(pdev, state));
636 static int p54p_resume(struct pci_dev *pdev)
638 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
639 struct p54p_priv *priv = dev->priv;
641 pci_set_power_state(pdev, PCI_D0);
642 pci_restore_state(pdev);
644 if (priv->common.mode != NL80211_IFTYPE_UNSPECIFIED) {
646 ieee80211_wake_queues(dev);
651 #endif /* CONFIG_PM */
653 static struct pci_driver p54p_driver = {
655 .id_table = p54p_table,
657 .remove = __devexit_p(p54p_remove),
659 .suspend = p54p_suspend,
660 .resume = p54p_resume,
661 #endif /* CONFIG_PM */
664 static int __init p54p_init(void)
666 return pci_register_driver(&p54p_driver);
669 static void __exit p54p_exit(void)
671 pci_unregister_driver(&p54p_driver);
674 module_init(p54p_init);
675 module_exit(p54p_exit);