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>
27 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
28 MODULE_DESCRIPTION("Prism54 PCI wireless driver");
29 MODULE_LICENSE("GPL");
30 MODULE_ALIAS("prism54pci");
32 static struct pci_device_id p54p_table[] __devinitdata = {
33 /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
34 { PCI_DEVICE(0x1260, 0x3890) },
35 /* 3COM 3CRWE154G72 Wireless LAN adapter */
36 { PCI_DEVICE(0x10b7, 0x6001) },
37 /* Intersil PRISM Indigo Wireless LAN adapter */
38 { PCI_DEVICE(0x1260, 0x3877) },
39 /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
40 { PCI_DEVICE(0x1260, 0x3886) },
44 MODULE_DEVICE_TABLE(pci, p54p_table);
46 static int p54p_upload_firmware(struct ieee80211_hw *dev)
48 struct p54p_priv *priv = dev->priv;
49 const struct firmware *fw_entry = NULL;
53 u32 remains, left, device_addr;
55 P54P_WRITE(int_enable, cpu_to_le32(0));
56 P54P_READ(int_enable);
59 reg = P54P_READ(ctrl_stat);
60 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
61 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
62 P54P_WRITE(ctrl_stat, reg);
66 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
67 P54P_WRITE(ctrl_stat, reg);
71 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
72 P54P_WRITE(ctrl_stat, reg);
77 err = request_firmware(&fw_entry, "isl3886", &priv->pdev->dev);
79 printk(KERN_ERR "%s (p54pci): cannot find firmware "
80 "(isl3886)\n", pci_name(priv->pdev));
84 p54_parse_firmware(dev, fw_entry);
86 data = (__le32 *) fw_entry->data;
87 remains = fw_entry->size;
88 device_addr = ISL38XX_DEV_FIRMWARE_ADDR;
91 left = min((u32)0x1000, remains);
92 P54P_WRITE(direct_mem_base, cpu_to_le32(device_addr));
93 P54P_READ(int_enable);
95 device_addr += 0x1000;
97 P54P_WRITE(direct_mem_win[i], *data++);
102 P54P_READ(int_enable);
105 release_firmware(fw_entry);
107 reg = P54P_READ(ctrl_stat);
108 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
109 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
110 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
111 P54P_WRITE(ctrl_stat, reg);
112 P54P_READ(ctrl_stat);
115 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
116 P54P_WRITE(ctrl_stat, reg);
120 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
121 P54P_WRITE(ctrl_stat, reg);
128 static irqreturn_t p54p_simple_interrupt(int irq, void *dev_id)
130 struct p54p_priv *priv = (struct p54p_priv *) dev_id;
133 reg = P54P_READ(int_ident);
134 P54P_WRITE(int_ack, reg);
136 if (reg & P54P_READ(int_enable))
137 complete(&priv->boot_comp);
142 static int p54p_read_eeprom(struct ieee80211_hw *dev)
144 struct p54p_priv *priv = dev->priv;
145 struct p54p_ring_control *ring_control = priv->ring_control;
147 struct p54_control_hdr *hdr;
149 dma_addr_t rx_mapping, tx_mapping;
152 init_completion(&priv->boot_comp);
153 err = request_irq(priv->pdev->irq, &p54p_simple_interrupt,
154 IRQF_SHARED, "p54pci", priv);
156 printk(KERN_ERR "%s (p54pci): failed to register IRQ handler\n",
157 pci_name(priv->pdev));
161 eeprom = kmalloc(0x2010 + EEPROM_READBACK_LEN, GFP_KERNEL);
163 printk(KERN_ERR "%s (p54pci): no memory for eeprom!\n",
164 pci_name(priv->pdev));
169 memset(ring_control, 0, sizeof(*ring_control));
170 P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
171 P54P_READ(ring_control_base);
174 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
175 P54P_READ(int_enable);
178 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
180 if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
181 printk(KERN_ERR "%s (p54pci): Cannot boot firmware!\n",
182 pci_name(priv->pdev));
187 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
188 P54P_READ(int_enable);
190 hdr = eeprom + 0x2010;
191 p54_fill_eeprom_readback(hdr);
192 hdr->req_id = cpu_to_le32(priv->common.rx_start);
194 rx_mapping = pci_map_single(priv->pdev, eeprom,
195 0x2010, PCI_DMA_FROMDEVICE);
196 tx_mapping = pci_map_single(priv->pdev, (void *)hdr,
197 EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);
199 ring_control->rx_mgmt[0].host_addr = cpu_to_le32(rx_mapping);
200 ring_control->rx_mgmt[0].len = cpu_to_le16(0x2010);
201 ring_control->tx_data[0].host_addr = cpu_to_le32(tx_mapping);
202 ring_control->tx_data[0].device_addr = hdr->req_id;
203 ring_control->tx_data[0].len = cpu_to_le16(EEPROM_READBACK_LEN);
205 ring_control->host_idx[2] = cpu_to_le32(1);
206 ring_control->host_idx[1] = cpu_to_le32(1);
210 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
212 wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);
213 wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);
215 pci_unmap_single(priv->pdev, tx_mapping,
216 EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);
217 pci_unmap_single(priv->pdev, rx_mapping,
218 0x2010, PCI_DMA_FROMDEVICE);
220 alen = le16_to_cpu(ring_control->rx_mgmt[0].len);
221 if (le32_to_cpu(ring_control->device_idx[2]) != 1 ||
223 printk(KERN_ERR "%s (p54pci): Cannot read eeprom!\n",
224 pci_name(priv->pdev));
229 p54_parse_eeprom(dev, (u8 *)eeprom + 0x10, alen - 0x10);
233 P54P_WRITE(int_enable, cpu_to_le32(0));
234 P54P_READ(int_enable);
236 free_irq(priv->pdev->irq, priv);
237 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
241 static void p54p_refill_rx_ring(struct ieee80211_hw *dev,
242 int ring_index, struct p54p_desc *ring, u32 ring_limit,
243 struct sk_buff **rx_buf)
245 struct p54p_priv *priv = dev->priv;
246 struct p54p_ring_control *ring_control = priv->ring_control;
249 idx = le32_to_cpu(ring_control->host_idx[ring_index]);
251 limit -= le32_to_cpu(ring_control->device_idx[ring_index]);
252 limit = ring_limit - limit;
254 i = idx % ring_limit;
255 while (limit-- > 1) {
256 struct p54p_desc *desc = &ring[i];
258 if (!desc->host_addr) {
261 skb = dev_alloc_skb(MAX_RX_SIZE);
265 mapping = pci_map_single(priv->pdev,
266 skb_tail_pointer(skb),
269 desc->host_addr = cpu_to_le32(mapping);
270 desc->device_addr = 0; // FIXME: necessary?
271 desc->len = cpu_to_le16(MAX_RX_SIZE);
282 ring_control->host_idx[ring_index] = cpu_to_le32(idx);
285 static void p54p_check_rx_ring(struct ieee80211_hw *dev, u32 *index,
286 int ring_index, struct p54p_desc *ring, u32 ring_limit,
287 struct sk_buff **rx_buf)
289 struct p54p_priv *priv = dev->priv;
290 struct p54p_ring_control *ring_control = priv->ring_control;
291 struct p54p_desc *desc;
294 i = (*index) % ring_limit;
295 (*index) = idx = le32_to_cpu(ring_control->device_idx[ring_index]);
301 len = le16_to_cpu(desc->len);
309 if (p54_rx(dev, skb)) {
310 pci_unmap_single(priv->pdev,
311 le32_to_cpu(desc->host_addr),
312 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
317 desc->len = cpu_to_le16(MAX_RX_SIZE);
324 p54p_refill_rx_ring(dev, ring_index, ring, ring_limit, rx_buf);
327 /* caller must hold priv->lock */
328 static void p54p_check_tx_ring(struct ieee80211_hw *dev, u32 *index,
329 int ring_index, struct p54p_desc *ring, u32 ring_limit,
332 struct p54p_priv *priv = dev->priv;
333 struct p54p_ring_control *ring_control = priv->ring_control;
334 struct p54p_desc *desc;
337 i = (*index) % ring_limit;
338 (*index) = idx = le32_to_cpu(ring_control->device_idx[1]);
346 pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
347 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);
350 desc->device_addr = 0;
359 static void p54p_rx_tasklet(unsigned long dev_id)
361 struct ieee80211_hw *dev = (struct ieee80211_hw *)dev_id;
362 struct p54p_priv *priv = dev->priv;
363 struct p54p_ring_control *ring_control = priv->ring_control;
365 p54p_check_rx_ring(dev, &priv->rx_idx_mgmt, 2, ring_control->rx_mgmt,
366 ARRAY_SIZE(ring_control->rx_mgmt), priv->rx_buf_mgmt);
368 p54p_check_rx_ring(dev, &priv->rx_idx_data, 0, ring_control->rx_data,
369 ARRAY_SIZE(ring_control->rx_data), priv->rx_buf_data);
372 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
375 static irqreturn_t p54p_interrupt(int irq, void *dev_id)
377 struct ieee80211_hw *dev = dev_id;
378 struct p54p_priv *priv = dev->priv;
379 struct p54p_ring_control *ring_control = priv->ring_control;
382 spin_lock(&priv->lock);
383 reg = P54P_READ(int_ident);
384 if (unlikely(reg == cpu_to_le32(0xFFFFFFFF))) {
385 spin_unlock(&priv->lock);
389 P54P_WRITE(int_ack, reg);
391 reg &= P54P_READ(int_enable);
393 if (reg & cpu_to_le32(ISL38XX_INT_IDENT_UPDATE)) {
394 p54p_check_tx_ring(dev, &priv->tx_idx_mgmt,
395 3, ring_control->tx_mgmt,
396 ARRAY_SIZE(ring_control->tx_mgmt),
399 p54p_check_tx_ring(dev, &priv->tx_idx_data,
400 1, ring_control->tx_data,
401 ARRAY_SIZE(ring_control->tx_data),
404 tasklet_schedule(&priv->rx_tasklet);
406 } else if (reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT))
407 complete(&priv->boot_comp);
409 spin_unlock(&priv->lock);
411 return reg ? IRQ_HANDLED : IRQ_NONE;
414 static void p54p_tx(struct ieee80211_hw *dev, struct p54_control_hdr *data,
415 size_t len, int free_on_tx)
417 struct p54p_priv *priv = dev->priv;
418 struct p54p_ring_control *ring_control = priv->ring_control;
420 struct p54p_desc *desc;
422 u32 device_idx, idx, i;
424 spin_lock_irqsave(&priv->lock, flags);
426 device_idx = le32_to_cpu(ring_control->device_idx[1]);
427 idx = le32_to_cpu(ring_control->host_idx[1]);
428 i = idx % ARRAY_SIZE(ring_control->tx_data);
430 mapping = pci_map_single(priv->pdev, data, len, PCI_DMA_TODEVICE);
431 desc = &ring_control->tx_data[i];
432 desc->host_addr = cpu_to_le32(mapping);
433 desc->device_addr = data->req_id;
434 desc->len = cpu_to_le16(len);
438 ring_control->host_idx[1] = cpu_to_le32(idx + 1);
441 priv->tx_buf_data[i] = data;
443 spin_unlock_irqrestore(&priv->lock, flags);
445 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
448 /* FIXME: unlikely to happen because the device usually runs out of
449 memory before we fill the ring up, but we can make it impossible */
450 if (idx - device_idx > ARRAY_SIZE(ring_control->tx_data) - 2)
451 printk(KERN_INFO "%s: tx overflow.\n", wiphy_name(dev->wiphy));
454 static int p54p_open(struct ieee80211_hw *dev)
456 struct p54p_priv *priv = dev->priv;
459 init_completion(&priv->boot_comp);
460 err = request_irq(priv->pdev->irq, &p54p_interrupt,
461 IRQF_SHARED, "p54pci", dev);
463 printk(KERN_ERR "%s: failed to register IRQ handler\n",
464 wiphy_name(dev->wiphy));
468 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
469 priv->rx_idx_data = priv->tx_idx_data = 0;
470 priv->rx_idx_mgmt = priv->tx_idx_mgmt = 0;
472 p54p_refill_rx_ring(dev, 0, priv->ring_control->rx_data,
473 ARRAY_SIZE(priv->ring_control->rx_data), priv->rx_buf_data);
475 p54p_refill_rx_ring(dev, 2, priv->ring_control->rx_mgmt,
476 ARRAY_SIZE(priv->ring_control->rx_mgmt), priv->rx_buf_mgmt);
478 p54p_upload_firmware(dev);
480 P54P_WRITE(ring_control_base, cpu_to_le32(priv->ring_control_dma));
481 P54P_READ(ring_control_base);
485 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
486 P54P_READ(int_enable);
490 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
493 if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
494 printk(KERN_ERR "%s: Cannot boot firmware!\n",
495 wiphy_name(dev->wiphy));
496 free_irq(priv->pdev->irq, dev);
500 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
501 P54P_READ(int_enable);
505 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
513 static void p54p_stop(struct ieee80211_hw *dev)
515 struct p54p_priv *priv = dev->priv;
516 struct p54p_ring_control *ring_control = priv->ring_control;
518 struct p54p_desc *desc;
520 tasklet_kill(&priv->rx_tasklet);
522 P54P_WRITE(int_enable, cpu_to_le32(0));
523 P54P_READ(int_enable);
526 free_irq(priv->pdev->irq, dev);
528 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
530 for (i = 0; i < ARRAY_SIZE(priv->rx_buf_data); i++) {
531 desc = &ring_control->rx_data[i];
533 pci_unmap_single(priv->pdev,
534 le32_to_cpu(desc->host_addr),
535 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
536 kfree_skb(priv->rx_buf_data[i]);
537 priv->rx_buf_data[i] = NULL;
540 for (i = 0; i < ARRAY_SIZE(priv->rx_buf_mgmt); i++) {
541 desc = &ring_control->rx_mgmt[i];
543 pci_unmap_single(priv->pdev,
544 le32_to_cpu(desc->host_addr),
545 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
546 kfree_skb(priv->rx_buf_mgmt[i]);
547 priv->rx_buf_mgmt[i] = NULL;
550 for (i = 0; i < ARRAY_SIZE(priv->tx_buf_data); i++) {
551 desc = &ring_control->tx_data[i];
553 pci_unmap_single(priv->pdev,
554 le32_to_cpu(desc->host_addr),
555 le16_to_cpu(desc->len),
558 kfree(priv->tx_buf_data[i]);
559 priv->tx_buf_data[i] = NULL;
562 for (i = 0; i < ARRAY_SIZE(priv->tx_buf_mgmt); i++) {
563 desc = &ring_control->tx_mgmt[i];
565 pci_unmap_single(priv->pdev,
566 le32_to_cpu(desc->host_addr),
567 le16_to_cpu(desc->len),
570 kfree(priv->tx_buf_mgmt[i]);
571 priv->tx_buf_mgmt[i] = NULL;
574 memset(ring_control, 0, sizeof(*ring_control));
577 static int __devinit p54p_probe(struct pci_dev *pdev,
578 const struct pci_device_id *id)
580 struct p54p_priv *priv;
581 struct ieee80211_hw *dev;
582 unsigned long mem_addr, mem_len;
584 DECLARE_MAC_BUF(mac);
586 err = pci_enable_device(pdev);
588 printk(KERN_ERR "%s (p54pci): Cannot enable new PCI device\n",
593 mem_addr = pci_resource_start(pdev, 0);
594 mem_len = pci_resource_len(pdev, 0);
595 if (mem_len < sizeof(struct p54p_csr)) {
596 printk(KERN_ERR "%s (p54pci): Too short PCI resources\n",
598 pci_disable_device(pdev);
602 err = pci_request_regions(pdev, "p54pci");
604 printk(KERN_ERR "%s (p54pci): Cannot obtain PCI resources\n",
609 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) ||
610 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
611 printk(KERN_ERR "%s (p54pci): No suitable DMA available\n",
616 pci_set_master(pdev);
617 pci_try_set_mwi(pdev);
619 pci_write_config_byte(pdev, 0x40, 0);
620 pci_write_config_byte(pdev, 0x41, 0);
622 dev = p54_init_common(sizeof(*priv));
624 printk(KERN_ERR "%s (p54pci): ieee80211 alloc failed\n",
633 SET_IEEE80211_DEV(dev, &pdev->dev);
634 pci_set_drvdata(pdev, dev);
636 priv->map = ioremap(mem_addr, mem_len);
638 printk(KERN_ERR "%s (p54pci): Cannot map device memory\n",
640 err = -EINVAL; // TODO: use a better error code?
644 priv->ring_control = pci_alloc_consistent(pdev, sizeof(*priv->ring_control),
645 &priv->ring_control_dma);
646 if (!priv->ring_control) {
647 printk(KERN_ERR "%s (p54pci): Cannot allocate rings\n",
652 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
654 err = p54p_upload_firmware(dev);
658 err = p54p_read_eeprom(dev);
662 priv->common.open = p54p_open;
663 priv->common.stop = p54p_stop;
664 priv->common.tx = p54p_tx;
666 spin_lock_init(&priv->lock);
667 tasklet_init(&priv->rx_tasklet, p54p_rx_tasklet, (unsigned long)dev);
669 err = ieee80211_register_hw(dev);
671 printk(KERN_ERR "%s (p54pci): Cannot register netdevice\n",
673 goto err_free_common;
676 printk(KERN_INFO "%s: hwaddr %s, isl38%02x\n",
677 wiphy_name(dev->wiphy),
678 print_mac(mac, dev->wiphy->perm_addr),
679 priv->common.version);
684 p54_free_common(dev);
687 pci_free_consistent(pdev, sizeof(*priv->ring_control),
688 priv->ring_control, priv->ring_control_dma);
694 pci_set_drvdata(pdev, NULL);
695 ieee80211_free_hw(dev);
698 pci_release_regions(pdev);
699 pci_disable_device(pdev);
703 static void __devexit p54p_remove(struct pci_dev *pdev)
705 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
706 struct p54p_priv *priv;
711 ieee80211_unregister_hw(dev);
713 pci_free_consistent(pdev, sizeof(*priv->ring_control),
714 priv->ring_control, priv->ring_control_dma);
715 p54_free_common(dev);
717 pci_release_regions(pdev);
718 pci_disable_device(pdev);
719 ieee80211_free_hw(dev);
723 static int p54p_suspend(struct pci_dev *pdev, pm_message_t state)
725 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
726 struct p54p_priv *priv = dev->priv;
728 if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
729 ieee80211_stop_queues(dev);
733 pci_save_state(pdev);
734 pci_set_power_state(pdev, pci_choose_state(pdev, state));
738 static int p54p_resume(struct pci_dev *pdev)
740 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
741 struct p54p_priv *priv = dev->priv;
743 pci_set_power_state(pdev, PCI_D0);
744 pci_restore_state(pdev);
746 if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
748 ieee80211_wake_queues(dev);
753 #endif /* CONFIG_PM */
755 static struct pci_driver p54p_driver = {
757 .id_table = p54p_table,
759 .remove = __devexit_p(p54p_remove),
761 .suspend = p54p_suspend,
762 .resume = p54p_resume,
763 #endif /* CONFIG_PM */
766 static int __init p54p_init(void)
768 return pci_register_driver(&p54p_driver);
771 static void __exit p54p_exit(void)
773 pci_unregister_driver(&p54p_driver);
776 module_init(p54p_init);
777 module_exit(p54p_exit);