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
;
501 err
= pci_enable_device(pdev
);
503 printk(KERN_ERR
"%s (prism54pci): Cannot enable new PCI device\n",
508 mem_addr
= pci_resource_start(pdev
, 0);
509 mem_len
= pci_resource_len(pdev
, 0);
510 if (mem_len
< sizeof(struct p54p_csr
)) {
511 printk(KERN_ERR
"%s (prism54pci): Too short PCI resources\n",
513 pci_disable_device(pdev
);
517 err
= pci_request_regions(pdev
, "prism54pci");
519 printk(KERN_ERR
"%s (prism54pci): Cannot obtain PCI resources\n",
524 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) ||
525 pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
)) {
526 printk(KERN_ERR
"%s (prism54pci): No suitable DMA available\n",
531 pci_set_master(pdev
);
532 pci_try_set_mwi(pdev
);
534 pci_write_config_byte(pdev
, 0x40, 0);
535 pci_write_config_byte(pdev
, 0x41, 0);
537 dev
= p54_init_common(sizeof(*priv
));
539 printk(KERN_ERR
"%s (prism54pci): ieee80211 alloc failed\n",
548 SET_IEEE80211_DEV(dev
, &pdev
->dev
);
549 pci_set_drvdata(pdev
, dev
);
551 priv
->map
= ioremap(mem_addr
, mem_len
);
553 printk(KERN_ERR
"%s (prism54pci): Cannot map device memory\n",
555 err
= -EINVAL
; // TODO: use a better error code?
559 priv
->ring_control
= pci_alloc_consistent(pdev
, sizeof(*priv
->ring_control
),
560 &priv
->ring_control_dma
);
561 if (!priv
->ring_control
) {
562 printk(KERN_ERR
"%s (prism54pci): Cannot allocate rings\n",
567 memset(priv
->ring_control
, 0, sizeof(*priv
->ring_control
));
569 err
= p54p_upload_firmware(dev
);
573 err
= p54p_read_eeprom(dev
);
577 priv
->common
.open
= p54p_open
;
578 priv
->common
.stop
= p54p_stop
;
579 priv
->common
.tx
= p54p_tx
;
581 spin_lock_init(&priv
->lock
);
583 err
= ieee80211_register_hw(dev
);
585 printk(KERN_ERR
"%s (prism54pci): Cannot register netdevice\n",
587 goto err_free_common
;
590 printk(KERN_INFO
"%s: hwaddr " MAC_FMT
", isl38%02x\n",
591 wiphy_name(dev
->wiphy
), MAC_ARG(dev
->wiphy
->perm_addr
),
592 priv
->common
.version
);
597 p54_free_common(dev
);
600 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
601 priv
->ring_control
, priv
->ring_control_dma
);
607 pci_set_drvdata(pdev
, NULL
);
608 ieee80211_free_hw(dev
);
611 pci_release_regions(pdev
);
612 pci_disable_device(pdev
);
616 static void __devexit
p54p_remove(struct pci_dev
*pdev
)
618 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
619 struct p54p_priv
*priv
;
624 ieee80211_unregister_hw(dev
);
626 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
627 priv
->ring_control
, priv
->ring_control_dma
);
628 p54_free_common(dev
);
630 pci_release_regions(pdev
);
631 pci_disable_device(pdev
);
632 ieee80211_free_hw(dev
);
636 static int p54p_suspend(struct pci_dev
*pdev
, pm_message_t state
)
638 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
639 struct p54p_priv
*priv
= dev
->priv
;
641 if (priv
->common
.mode
!= IEEE80211_IF_TYPE_INVALID
) {
642 ieee80211_stop_queues(dev
);
646 pci_save_state(pdev
);
647 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
651 static int p54p_resume(struct pci_dev
*pdev
)
653 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
654 struct p54p_priv
*priv
= dev
->priv
;
656 pci_set_power_state(pdev
, PCI_D0
);
657 pci_restore_state(pdev
);
659 if (priv
->common
.mode
!= IEEE80211_IF_TYPE_INVALID
) {
661 ieee80211_start_queues(dev
);
666 #endif /* CONFIG_PM */
668 static struct pci_driver p54p_driver
= {
669 .name
= "prism54pci",
670 .id_table
= p54p_table
,
672 .remove
= __devexit_p(p54p_remove
),
674 .suspend
= p54p_suspend
,
675 .resume
= p54p_resume
,
676 #endif /* CONFIG_PM */
679 static int __init
p54p_init(void)
681 return pci_register_driver(&p54p_driver
);
684 static void __exit
p54p_exit(void)
686 pci_unregister_driver(&p54p_driver
);
689 module_init(p54p_init
);
690 module_exit(p54p_exit
);