3 * Linux device driver for PCI based Prism54
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
7 * Based on the islsm (softmac prism54) driver, which is:
8 * Copyright 2004-2006 Jean-Baptiste Note <jean-baptiste.note@m4x.org>, et al.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/pci.h>
17 #include <linux/firmware.h>
18 #include <linux/etherdevice.h>
19 #include <linux/delay.h>
20 #include <linux/completion.h>
21 #include <net/mac80211.h>
26 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
27 MODULE_DESCRIPTION("Prism54 PCI wireless driver");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS("prism54pci");
31 static struct pci_device_id p54p_table[] __devinitdata = {
32 /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
33 { PCI_DEVICE(0x1260, 0x3890) },
34 /* 3COM 3CRWE154G72 Wireless LAN adapter */
35 { PCI_DEVICE(0x10b7, 0x6001) },
36 /* Intersil PRISM Indigo Wireless LAN adapter */
37 { PCI_DEVICE(0x1260, 0x3877) },
38 /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
39 { PCI_DEVICE(0x1260, 0x3886) },
43 MODULE_DEVICE_TABLE(pci, p54p_table);
45 static int p54p_upload_firmware(struct ieee80211_hw *dev)
47 struct p54p_priv *priv = dev->priv;
48 const struct firmware *fw_entry = NULL;
52 u32 remains, left, device_addr;
54 P54P_WRITE(int_enable, 0);
55 P54P_READ(int_enable);
58 reg = P54P_READ(ctrl_stat);
59 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
60 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
61 P54P_WRITE(ctrl_stat, reg);
65 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
66 P54P_WRITE(ctrl_stat, reg);
70 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
71 P54P_WRITE(ctrl_stat, reg);
76 err = request_firmware(&fw_entry, "isl3886", &priv->pdev->dev);
78 printk(KERN_ERR "%s (prism54pci): cannot find firmware "
79 "(isl3886)\n", pci_name(priv->pdev));
83 p54_parse_firmware(dev, fw_entry);
85 data = (u32 *) fw_entry->data;
86 remains = fw_entry->size;
87 device_addr = ISL38XX_DEV_FIRMWARE_ADDR;
90 left = min((u32)0x1000, remains);
91 P54P_WRITE(direct_mem_base, cpu_to_le32(device_addr));
92 P54P_READ(int_enable);
94 device_addr += 0x1000;
96 P54P_WRITE(direct_mem_win[i], *data++);
101 P54P_READ(int_enable);
104 release_firmware(fw_entry);
106 reg = P54P_READ(ctrl_stat);
107 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
108 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
109 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
110 P54P_WRITE(ctrl_stat, reg);
111 P54P_READ(ctrl_stat);
114 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
115 P54P_WRITE(ctrl_stat, reg);
119 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
120 P54P_WRITE(ctrl_stat, reg);
127 static irqreturn_t p54p_simple_interrupt(int irq, void *dev_id)
129 struct p54p_priv *priv = (struct p54p_priv *) dev_id;
132 reg = P54P_READ(int_ident);
133 P54P_WRITE(int_ack, reg);
135 if (reg & P54P_READ(int_enable))
136 complete(&priv->boot_comp);
141 static int p54p_read_eeprom(struct ieee80211_hw *dev)
143 struct p54p_priv *priv = dev->priv;
145 struct p54_control_hdr *hdr;
147 dma_addr_t rx_mapping, tx_mapping;
150 init_completion(&priv->boot_comp);
151 err = request_irq(priv->pdev->irq, &p54p_simple_interrupt,
152 IRQF_SHARED, "prism54pci", priv);
154 printk(KERN_ERR "%s (prism54pci): failed to register IRQ handler\n",
155 pci_name(priv->pdev));
159 eeprom = kmalloc(0x2010 + EEPROM_READBACK_LEN, GFP_KERNEL);
161 printk(KERN_ERR "%s (prism54pci): no memory for eeprom!\n",
162 pci_name(priv->pdev));
167 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
168 P54P_WRITE(ring_control_base, priv->ring_control_dma);
169 P54P_READ(ring_control_base);
172 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
173 P54P_READ(int_enable);
176 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
178 if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
179 printk(KERN_ERR "%s (prism54pci): Cannot boot firmware!\n",
180 pci_name(priv->pdev));
185 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
186 P54P_READ(int_enable);
188 hdr = eeprom + 0x2010;
189 p54_fill_eeprom_readback(hdr);
190 hdr->req_id = cpu_to_le32(priv->common.rx_start);
192 rx_mapping = pci_map_single(priv->pdev, eeprom,
193 0x2010, PCI_DMA_FROMDEVICE);
194 tx_mapping = pci_map_single(priv->pdev, (void *)hdr,
195 EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);
197 priv->ring_control->rx_mgmt[0].host_addr = cpu_to_le32(rx_mapping);
198 priv->ring_control->rx_mgmt[0].len = cpu_to_le16(0x2010);
199 priv->ring_control->tx_data[0].host_addr = cpu_to_le32(tx_mapping);
200 priv->ring_control->tx_data[0].device_addr = hdr->req_id;
201 priv->ring_control->tx_data[0].len = cpu_to_le16(EEPROM_READBACK_LEN);
203 priv->ring_control->host_idx[2] = cpu_to_le32(1);
204 priv->ring_control->host_idx[1] = cpu_to_le32(1);
208 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
210 wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);
211 wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ);
213 pci_unmap_single(priv->pdev, tx_mapping,
214 EEPROM_READBACK_LEN, PCI_DMA_TODEVICE);
215 pci_unmap_single(priv->pdev, rx_mapping,
216 0x2010, PCI_DMA_FROMDEVICE);
218 alen = le16_to_cpu(priv->ring_control->rx_mgmt[0].len);
219 if (le32_to_cpu(priv->ring_control->device_idx[2]) != 1 ||
221 printk(KERN_ERR "%s (prism54pci): Cannot read eeprom!\n",
222 pci_name(priv->pdev));
227 p54_parse_eeprom(dev, (u8 *)eeprom + 0x10, alen - 0x10);
231 P54P_WRITE(int_enable, 0);
232 P54P_READ(int_enable);
234 free_irq(priv->pdev->irq, priv);
235 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
239 static void p54p_refill_rx_ring(struct ieee80211_hw *dev)
241 struct p54p_priv *priv = dev->priv;
242 u32 limit, host_idx, idx;
244 host_idx = le32_to_cpu(priv->ring_control->host_idx[0]);
246 limit -= le32_to_cpu(priv->ring_control->device_idx[0]);
247 limit = ARRAY_SIZE(priv->ring_control->rx_data) - limit;
249 idx = host_idx % ARRAY_SIZE(priv->ring_control->rx_data);
250 while (limit-- > 1) {
251 struct p54p_desc *desc = &priv->ring_control->rx_data[idx];
253 if (!desc->host_addr) {
256 skb = dev_alloc_skb(MAX_RX_SIZE);
260 mapping = pci_map_single(priv->pdev,
261 skb_tail_pointer(skb),
264 desc->host_addr = cpu_to_le32(mapping);
265 desc->device_addr = 0; // FIXME: necessary?
266 desc->len = cpu_to_le16(MAX_RX_SIZE);
268 priv->rx_buf[idx] = skb;
273 idx %= ARRAY_SIZE(priv->ring_control->rx_data);
277 priv->ring_control->host_idx[0] = cpu_to_le32(host_idx);
280 static irqreturn_t p54p_interrupt(int irq, void *dev_id)
282 struct ieee80211_hw *dev = dev_id;
283 struct p54p_priv *priv = dev->priv;
286 spin_lock(&priv->lock);
287 reg = P54P_READ(int_ident);
288 if (unlikely(reg == 0xFFFFFFFF)) {
289 spin_unlock(&priv->lock);
293 P54P_WRITE(int_ack, reg);
295 reg &= P54P_READ(int_enable);
297 if (reg & cpu_to_le32(ISL38XX_INT_IDENT_UPDATE)) {
298 struct p54p_desc *desc;
301 i %= ARRAY_SIZE(priv->ring_control->tx_data);
302 priv->tx_idx = idx = le32_to_cpu(priv->ring_control->device_idx[1]);
303 idx %= ARRAY_SIZE(priv->ring_control->tx_data);
306 desc = &priv->ring_control->tx_data[i];
307 if (priv->tx_buf[i]) {
308 kfree(priv->tx_buf[i]);
309 priv->tx_buf[i] = NULL;
312 pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
313 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);
316 desc->device_addr = 0;
321 i %= ARRAY_SIZE(priv->ring_control->tx_data);
325 i %= ARRAY_SIZE(priv->ring_control->rx_data);
326 priv->rx_idx = idx = le32_to_cpu(priv->ring_control->device_idx[0]);
327 idx %= ARRAY_SIZE(priv->ring_control->rx_data);
331 desc = &priv->ring_control->rx_data[i];
332 len = le16_to_cpu(desc->len);
333 skb = priv->rx_buf[i];
337 if (p54_rx(dev, skb)) {
338 pci_unmap_single(priv->pdev,
339 le32_to_cpu(desc->host_addr),
340 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
342 priv->rx_buf[i] = NULL;
346 desc->len = cpu_to_le16(MAX_RX_SIZE);
350 i %= ARRAY_SIZE(priv->ring_control->rx_data);
353 p54p_refill_rx_ring(dev);
356 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
357 } else if (reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT))
358 complete(&priv->boot_comp);
360 spin_unlock(&priv->lock);
362 return reg ? IRQ_HANDLED : IRQ_NONE;
365 static void p54p_tx(struct ieee80211_hw *dev, struct p54_control_hdr *data,
366 size_t len, int free_on_tx)
368 struct p54p_priv *priv = dev->priv;
370 struct p54p_desc *desc;
372 u32 device_idx, idx, i;
374 spin_lock_irqsave(&priv->lock, flags);
376 device_idx = le32_to_cpu(priv->ring_control->device_idx[1]);
377 idx = le32_to_cpu(priv->ring_control->host_idx[1]);
378 i = idx % ARRAY_SIZE(priv->ring_control->tx_data);
380 mapping = pci_map_single(priv->pdev, data, len, PCI_DMA_TODEVICE);
381 desc = &priv->ring_control->tx_data[i];
382 desc->host_addr = cpu_to_le32(mapping);
383 desc->device_addr = data->req_id;
384 desc->len = cpu_to_le16(len);
388 priv->ring_control->host_idx[1] = cpu_to_le32(idx + 1);
391 priv->tx_buf[i] = data;
393 spin_unlock_irqrestore(&priv->lock, flags);
395 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
398 /* FIXME: unlikely to happen because the device usually runs out of
399 memory before we fill the ring up, but we can make it impossible */
400 if (idx - device_idx > ARRAY_SIZE(priv->ring_control->tx_data) - 2)
401 printk(KERN_INFO "%s: tx overflow.\n", wiphy_name(dev->wiphy));
404 static int p54p_open(struct ieee80211_hw *dev)
406 struct p54p_priv *priv = dev->priv;
409 init_completion(&priv->boot_comp);
410 err = request_irq(priv->pdev->irq, &p54p_interrupt,
411 IRQF_SHARED, "prism54pci", dev);
413 printk(KERN_ERR "%s: failed to register IRQ handler\n",
414 wiphy_name(dev->wiphy));
418 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
419 priv->rx_idx = priv->tx_idx = 0;
420 p54p_refill_rx_ring(dev);
422 p54p_upload_firmware(dev);
424 P54P_WRITE(ring_control_base, priv->ring_control_dma);
425 P54P_READ(ring_control_base);
429 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_INIT));
430 P54P_READ(int_enable);
434 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
437 if (!wait_for_completion_interruptible_timeout(&priv->boot_comp, HZ)) {
438 printk(KERN_ERR "%s: Cannot boot firmware!\n",
439 wiphy_name(dev->wiphy));
440 free_irq(priv->pdev->irq, dev);
444 P54P_WRITE(int_enable, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE));
445 P54P_READ(int_enable);
449 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_UPDATE));
457 static void p54p_stop(struct ieee80211_hw *dev)
459 struct p54p_priv *priv = dev->priv;
461 struct p54p_desc *desc;
463 P54P_WRITE(int_enable, 0);
464 P54P_READ(int_enable);
467 free_irq(priv->pdev->irq, dev);
469 P54P_WRITE(dev_int, cpu_to_le32(ISL38XX_DEV_INT_RESET));
471 for (i = 0; i < ARRAY_SIZE(priv->rx_buf); i++) {
472 desc = &priv->ring_control->rx_data[i];
474 pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
475 MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
476 kfree_skb(priv->rx_buf[i]);
477 priv->rx_buf[i] = NULL;
480 for (i = 0; i < ARRAY_SIZE(priv->tx_buf); i++) {
481 desc = &priv->ring_control->tx_data[i];
483 pci_unmap_single(priv->pdev, le32_to_cpu(desc->host_addr),
484 le16_to_cpu(desc->len), PCI_DMA_TODEVICE);
486 kfree(priv->tx_buf[i]);
487 priv->tx_buf[i] = NULL;
490 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
493 static int __devinit p54p_probe(struct pci_dev *pdev,
494 const struct pci_device_id *id)
496 struct p54p_priv *priv;
497 struct ieee80211_hw *dev;
498 unsigned long mem_addr, mem_len;
500 DECLARE_MAC_BUF(mac);
502 err = pci_enable_device(pdev);
504 printk(KERN_ERR "%s (prism54pci): Cannot enable new PCI device\n",
509 mem_addr = pci_resource_start(pdev, 0);
510 mem_len = pci_resource_len(pdev, 0);
511 if (mem_len < sizeof(struct p54p_csr)) {
512 printk(KERN_ERR "%s (prism54pci): Too short PCI resources\n",
514 pci_disable_device(pdev);
518 err = pci_request_regions(pdev, "prism54pci");
520 printk(KERN_ERR "%s (prism54pci): Cannot obtain PCI resources\n",
525 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) ||
526 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
527 printk(KERN_ERR "%s (prism54pci): No suitable DMA available\n",
532 pci_set_master(pdev);
533 pci_try_set_mwi(pdev);
535 pci_write_config_byte(pdev, 0x40, 0);
536 pci_write_config_byte(pdev, 0x41, 0);
538 dev = p54_init_common(sizeof(*priv));
540 printk(KERN_ERR "%s (prism54pci): ieee80211 alloc failed\n",
549 SET_IEEE80211_DEV(dev, &pdev->dev);
550 pci_set_drvdata(pdev, dev);
552 priv->map = ioremap(mem_addr, mem_len);
554 printk(KERN_ERR "%s (prism54pci): Cannot map device memory\n",
556 err = -EINVAL; // TODO: use a better error code?
560 priv->ring_control = pci_alloc_consistent(pdev, sizeof(*priv->ring_control),
561 &priv->ring_control_dma);
562 if (!priv->ring_control) {
563 printk(KERN_ERR "%s (prism54pci): Cannot allocate rings\n",
568 memset(priv->ring_control, 0, sizeof(*priv->ring_control));
570 err = p54p_upload_firmware(dev);
574 err = p54p_read_eeprom(dev);
578 priv->common.open = p54p_open;
579 priv->common.stop = p54p_stop;
580 priv->common.tx = p54p_tx;
582 spin_lock_init(&priv->lock);
584 err = ieee80211_register_hw(dev);
586 printk(KERN_ERR "%s (prism54pci): Cannot register netdevice\n",
588 goto err_free_common;
591 printk(KERN_INFO "%s: hwaddr %s, isl38%02x\n",
592 wiphy_name(dev->wiphy),
593 print_mac(mac, dev->wiphy->perm_addr),
594 priv->common.version);
599 p54_free_common(dev);
602 pci_free_consistent(pdev, sizeof(*priv->ring_control),
603 priv->ring_control, priv->ring_control_dma);
609 pci_set_drvdata(pdev, NULL);
610 ieee80211_free_hw(dev);
613 pci_release_regions(pdev);
614 pci_disable_device(pdev);
618 static void __devexit p54p_remove(struct pci_dev *pdev)
620 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
621 struct p54p_priv *priv;
626 ieee80211_unregister_hw(dev);
628 pci_free_consistent(pdev, sizeof(*priv->ring_control),
629 priv->ring_control, priv->ring_control_dma);
630 p54_free_common(dev);
632 pci_release_regions(pdev);
633 pci_disable_device(pdev);
634 ieee80211_free_hw(dev);
638 static int p54p_suspend(struct pci_dev *pdev, pm_message_t state)
640 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
641 struct p54p_priv *priv = dev->priv;
643 if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
644 ieee80211_stop_queues(dev);
648 pci_save_state(pdev);
649 pci_set_power_state(pdev, pci_choose_state(pdev, state));
653 static int p54p_resume(struct pci_dev *pdev)
655 struct ieee80211_hw *dev = pci_get_drvdata(pdev);
656 struct p54p_priv *priv = dev->priv;
658 pci_set_power_state(pdev, PCI_D0);
659 pci_restore_state(pdev);
661 if (priv->common.mode != IEEE80211_IF_TYPE_INVALID) {
663 ieee80211_start_queues(dev);
668 #endif /* CONFIG_PM */
670 static struct pci_driver p54p_driver = {
671 .name = "prism54pci",
672 .id_table = p54p_table,
674 .remove = __devexit_p(p54p_remove),
676 .suspend = p54p_suspend,
677 .resume = p54p_resume,
678 #endif /* CONFIG_PM */
681 static int __init p54p_init(void)
683 return pci_register_driver(&p54p_driver);
686 static void __exit p54p_exit(void)
688 pci_unregister_driver(&p54p_driver);
691 module_init(p54p_init);
692 module_exit(p54p_exit);