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
, cpu_to_le32(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
= (__le32
*) 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
;
144 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
146 struct p54_control_hdr
*hdr
;
148 dma_addr_t rx_mapping
, tx_mapping
;
151 init_completion(&priv
->boot_comp
);
152 err
= request_irq(priv
->pdev
->irq
, &p54p_simple_interrupt
,
153 IRQF_SHARED
, "prism54pci", priv
);
155 printk(KERN_ERR
"%s (prism54pci): failed to register IRQ handler\n",
156 pci_name(priv
->pdev
));
160 eeprom
= kmalloc(0x2010 + EEPROM_READBACK_LEN
, GFP_KERNEL
);
162 printk(KERN_ERR
"%s (prism54pci): no memory for eeprom!\n",
163 pci_name(priv
->pdev
));
168 memset(ring_control
, 0, sizeof(*ring_control
));
169 P54P_WRITE(ring_control_base
, cpu_to_le32(priv
->ring_control_dma
));
170 P54P_READ(ring_control_base
);
173 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_INIT
));
174 P54P_READ(int_enable
);
177 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
179 if (!wait_for_completion_interruptible_timeout(&priv
->boot_comp
, HZ
)) {
180 printk(KERN_ERR
"%s (prism54pci): Cannot boot firmware!\n",
181 pci_name(priv
->pdev
));
186 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
));
187 P54P_READ(int_enable
);
189 hdr
= eeprom
+ 0x2010;
190 p54_fill_eeprom_readback(hdr
);
191 hdr
->req_id
= cpu_to_le32(priv
->common
.rx_start
);
193 rx_mapping
= pci_map_single(priv
->pdev
, eeprom
,
194 0x2010, PCI_DMA_FROMDEVICE
);
195 tx_mapping
= pci_map_single(priv
->pdev
, (void *)hdr
,
196 EEPROM_READBACK_LEN
, PCI_DMA_TODEVICE
);
198 ring_control
->rx_mgmt
[0].host_addr
= cpu_to_le32(rx_mapping
);
199 ring_control
->rx_mgmt
[0].len
= cpu_to_le16(0x2010);
200 ring_control
->tx_data
[0].host_addr
= cpu_to_le32(tx_mapping
);
201 ring_control
->tx_data
[0].device_addr
= hdr
->req_id
;
202 ring_control
->tx_data
[0].len
= cpu_to_le16(EEPROM_READBACK_LEN
);
204 ring_control
->host_idx
[2] = cpu_to_le32(1);
205 ring_control
->host_idx
[1] = cpu_to_le32(1);
209 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
211 wait_for_completion_interruptible_timeout(&priv
->boot_comp
, HZ
);
212 wait_for_completion_interruptible_timeout(&priv
->boot_comp
, HZ
);
214 pci_unmap_single(priv
->pdev
, tx_mapping
,
215 EEPROM_READBACK_LEN
, PCI_DMA_TODEVICE
);
216 pci_unmap_single(priv
->pdev
, rx_mapping
,
217 0x2010, PCI_DMA_FROMDEVICE
);
219 alen
= le16_to_cpu(ring_control
->rx_mgmt
[0].len
);
220 if (le32_to_cpu(ring_control
->device_idx
[2]) != 1 ||
222 printk(KERN_ERR
"%s (prism54pci): Cannot read eeprom!\n",
223 pci_name(priv
->pdev
));
228 p54_parse_eeprom(dev
, (u8
*)eeprom
+ 0x10, alen
- 0x10);
232 P54P_WRITE(int_enable
, cpu_to_le32(0));
233 P54P_READ(int_enable
);
235 free_irq(priv
->pdev
->irq
, priv
);
236 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
240 static void p54p_refill_rx_ring(struct ieee80211_hw
*dev
)
242 struct p54p_priv
*priv
= dev
->priv
;
243 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
244 u32 limit
, host_idx
, idx
;
246 host_idx
= le32_to_cpu(ring_control
->host_idx
[0]);
248 limit
-= le32_to_cpu(ring_control
->device_idx
[0]);
249 limit
= ARRAY_SIZE(ring_control
->rx_data
) - limit
;
251 idx
= host_idx
% ARRAY_SIZE(ring_control
->rx_data
);
252 while (limit
-- > 1) {
253 struct p54p_desc
*desc
= &ring_control
->rx_data
[idx
];
255 if (!desc
->host_addr
) {
258 skb
= dev_alloc_skb(MAX_RX_SIZE
);
262 mapping
= pci_map_single(priv
->pdev
,
263 skb_tail_pointer(skb
),
266 desc
->host_addr
= cpu_to_le32(mapping
);
267 desc
->device_addr
= 0; // FIXME: necessary?
268 desc
->len
= cpu_to_le16(MAX_RX_SIZE
);
270 priv
->rx_buf
[idx
] = skb
;
275 idx
%= ARRAY_SIZE(ring_control
->rx_data
);
279 ring_control
->host_idx
[0] = cpu_to_le32(host_idx
);
282 static irqreturn_t
p54p_interrupt(int irq
, void *dev_id
)
284 struct ieee80211_hw
*dev
= dev_id
;
285 struct p54p_priv
*priv
= dev
->priv
;
286 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
289 spin_lock(&priv
->lock
);
290 reg
= P54P_READ(int_ident
);
291 if (unlikely(reg
== cpu_to_le32(0xFFFFFFFF))) {
292 spin_unlock(&priv
->lock
);
296 P54P_WRITE(int_ack
, reg
);
298 reg
&= P54P_READ(int_enable
);
300 if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
)) {
301 struct p54p_desc
*desc
;
304 i
%= ARRAY_SIZE(ring_control
->tx_data
);
305 priv
->tx_idx
= idx
= le32_to_cpu(ring_control
->device_idx
[1]);
306 idx
%= ARRAY_SIZE(ring_control
->tx_data
);
309 desc
= &ring_control
->tx_data
[i
];
310 if (priv
->tx_buf
[i
]) {
311 kfree(priv
->tx_buf
[i
]);
312 priv
->tx_buf
[i
] = NULL
;
315 pci_unmap_single(priv
->pdev
, le32_to_cpu(desc
->host_addr
),
316 le16_to_cpu(desc
->len
), PCI_DMA_TODEVICE
);
319 desc
->device_addr
= 0;
324 i
%= ARRAY_SIZE(ring_control
->tx_data
);
328 i
%= ARRAY_SIZE(ring_control
->rx_data
);
329 priv
->rx_idx
= idx
= le32_to_cpu(ring_control
->device_idx
[0]);
330 idx
%= ARRAY_SIZE(ring_control
->rx_data
);
334 desc
= &ring_control
->rx_data
[i
];
335 len
= le16_to_cpu(desc
->len
);
336 skb
= priv
->rx_buf
[i
];
340 if (p54_rx(dev
, skb
)) {
341 pci_unmap_single(priv
->pdev
,
342 le32_to_cpu(desc
->host_addr
),
343 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
345 priv
->rx_buf
[i
] = NULL
;
349 desc
->len
= cpu_to_le16(MAX_RX_SIZE
);
353 i
%= ARRAY_SIZE(ring_control
->rx_data
);
356 p54p_refill_rx_ring(dev
);
359 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
360 } else if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_INIT
))
361 complete(&priv
->boot_comp
);
363 spin_unlock(&priv
->lock
);
365 return reg
? IRQ_HANDLED
: IRQ_NONE
;
368 static void p54p_tx(struct ieee80211_hw
*dev
, struct p54_control_hdr
*data
,
369 size_t len
, int free_on_tx
)
371 struct p54p_priv
*priv
= dev
->priv
;
372 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
374 struct p54p_desc
*desc
;
376 u32 device_idx
, idx
, i
;
378 spin_lock_irqsave(&priv
->lock
, flags
);
380 device_idx
= le32_to_cpu(ring_control
->device_idx
[1]);
381 idx
= le32_to_cpu(ring_control
->host_idx
[1]);
382 i
= idx
% ARRAY_SIZE(ring_control
->tx_data
);
384 mapping
= pci_map_single(priv
->pdev
, data
, len
, PCI_DMA_TODEVICE
);
385 desc
= &ring_control
->tx_data
[i
];
386 desc
->host_addr
= cpu_to_le32(mapping
);
387 desc
->device_addr
= data
->req_id
;
388 desc
->len
= cpu_to_le16(len
);
392 ring_control
->host_idx
[1] = cpu_to_le32(idx
+ 1);
395 priv
->tx_buf
[i
] = data
;
397 spin_unlock_irqrestore(&priv
->lock
, flags
);
399 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
402 /* FIXME: unlikely to happen because the device usually runs out of
403 memory before we fill the ring up, but we can make it impossible */
404 if (idx
- device_idx
> ARRAY_SIZE(ring_control
->tx_data
) - 2)
405 printk(KERN_INFO
"%s: tx overflow.\n", wiphy_name(dev
->wiphy
));
408 static int p54p_open(struct ieee80211_hw
*dev
)
410 struct p54p_priv
*priv
= dev
->priv
;
413 init_completion(&priv
->boot_comp
);
414 err
= request_irq(priv
->pdev
->irq
, &p54p_interrupt
,
415 IRQF_SHARED
, "prism54pci", dev
);
417 printk(KERN_ERR
"%s: failed to register IRQ handler\n",
418 wiphy_name(dev
->wiphy
));
422 memset(priv
->ring_control
, 0, sizeof(*priv
->ring_control
));
423 priv
->rx_idx
= priv
->tx_idx
= 0;
424 p54p_refill_rx_ring(dev
);
426 p54p_upload_firmware(dev
);
428 P54P_WRITE(ring_control_base
, cpu_to_le32(priv
->ring_control_dma
));
429 P54P_READ(ring_control_base
);
433 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_INIT
));
434 P54P_READ(int_enable
);
438 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
441 if (!wait_for_completion_interruptible_timeout(&priv
->boot_comp
, HZ
)) {
442 printk(KERN_ERR
"%s: Cannot boot firmware!\n",
443 wiphy_name(dev
->wiphy
));
444 free_irq(priv
->pdev
->irq
, dev
);
448 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
));
449 P54P_READ(int_enable
);
453 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
461 static void p54p_stop(struct ieee80211_hw
*dev
)
463 struct p54p_priv
*priv
= dev
->priv
;
464 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
466 struct p54p_desc
*desc
;
468 P54P_WRITE(int_enable
, cpu_to_le32(0));
469 P54P_READ(int_enable
);
472 free_irq(priv
->pdev
->irq
, dev
);
474 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
476 for (i
= 0; i
< ARRAY_SIZE(priv
->rx_buf
); i
++) {
477 desc
= &ring_control
->rx_data
[i
];
479 pci_unmap_single(priv
->pdev
, le32_to_cpu(desc
->host_addr
),
480 MAX_RX_SIZE
, PCI_DMA_FROMDEVICE
);
481 kfree_skb(priv
->rx_buf
[i
]);
482 priv
->rx_buf
[i
] = NULL
;
485 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_buf
); i
++) {
486 desc
= &ring_control
->tx_data
[i
];
488 pci_unmap_single(priv
->pdev
, le32_to_cpu(desc
->host_addr
),
489 le16_to_cpu(desc
->len
), PCI_DMA_TODEVICE
);
491 kfree(priv
->tx_buf
[i
]);
492 priv
->tx_buf
[i
] = NULL
;
495 memset(ring_control
, 0, sizeof(ring_control
));
498 static int __devinit
p54p_probe(struct pci_dev
*pdev
,
499 const struct pci_device_id
*id
)
501 struct p54p_priv
*priv
;
502 struct ieee80211_hw
*dev
;
503 unsigned long mem_addr
, mem_len
;
505 DECLARE_MAC_BUF(mac
);
507 err
= pci_enable_device(pdev
);
509 printk(KERN_ERR
"%s (prism54pci): Cannot enable new PCI device\n",
514 mem_addr
= pci_resource_start(pdev
, 0);
515 mem_len
= pci_resource_len(pdev
, 0);
516 if (mem_len
< sizeof(struct p54p_csr
)) {
517 printk(KERN_ERR
"%s (prism54pci): Too short PCI resources\n",
519 pci_disable_device(pdev
);
523 err
= pci_request_regions(pdev
, "prism54pci");
525 printk(KERN_ERR
"%s (prism54pci): Cannot obtain PCI resources\n",
530 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) ||
531 pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
)) {
532 printk(KERN_ERR
"%s (prism54pci): No suitable DMA available\n",
537 pci_set_master(pdev
);
538 pci_try_set_mwi(pdev
);
540 pci_write_config_byte(pdev
, 0x40, 0);
541 pci_write_config_byte(pdev
, 0x41, 0);
543 dev
= p54_init_common(sizeof(*priv
));
545 printk(KERN_ERR
"%s (prism54pci): ieee80211 alloc failed\n",
554 SET_IEEE80211_DEV(dev
, &pdev
->dev
);
555 pci_set_drvdata(pdev
, dev
);
557 priv
->map
= ioremap(mem_addr
, mem_len
);
559 printk(KERN_ERR
"%s (prism54pci): Cannot map device memory\n",
561 err
= -EINVAL
; // TODO: use a better error code?
565 priv
->ring_control
= pci_alloc_consistent(pdev
, sizeof(*priv
->ring_control
),
566 &priv
->ring_control_dma
);
567 if (!priv
->ring_control
) {
568 printk(KERN_ERR
"%s (prism54pci): Cannot allocate rings\n",
573 memset(priv
->ring_control
, 0, sizeof(*priv
->ring_control
));
575 err
= p54p_upload_firmware(dev
);
579 err
= p54p_read_eeprom(dev
);
583 priv
->common
.open
= p54p_open
;
584 priv
->common
.stop
= p54p_stop
;
585 priv
->common
.tx
= p54p_tx
;
587 spin_lock_init(&priv
->lock
);
589 err
= ieee80211_register_hw(dev
);
591 printk(KERN_ERR
"%s (prism54pci): Cannot register netdevice\n",
593 goto err_free_common
;
596 printk(KERN_INFO
"%s: hwaddr %s, isl38%02x\n",
597 wiphy_name(dev
->wiphy
),
598 print_mac(mac
, dev
->wiphy
->perm_addr
),
599 priv
->common
.version
);
604 p54_free_common(dev
);
607 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
608 priv
->ring_control
, priv
->ring_control_dma
);
614 pci_set_drvdata(pdev
, NULL
);
615 ieee80211_free_hw(dev
);
618 pci_release_regions(pdev
);
619 pci_disable_device(pdev
);
623 static void __devexit
p54p_remove(struct pci_dev
*pdev
)
625 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
626 struct p54p_priv
*priv
;
631 ieee80211_unregister_hw(dev
);
633 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
634 priv
->ring_control
, priv
->ring_control_dma
);
635 p54_free_common(dev
);
637 pci_release_regions(pdev
);
638 pci_disable_device(pdev
);
639 ieee80211_free_hw(dev
);
643 static int p54p_suspend(struct pci_dev
*pdev
, pm_message_t state
)
645 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
646 struct p54p_priv
*priv
= dev
->priv
;
648 if (priv
->common
.mode
!= IEEE80211_IF_TYPE_INVALID
) {
649 ieee80211_stop_queues(dev
);
653 pci_save_state(pdev
);
654 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
658 static int p54p_resume(struct pci_dev
*pdev
)
660 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
661 struct p54p_priv
*priv
= dev
->priv
;
663 pci_set_power_state(pdev
, PCI_D0
);
664 pci_restore_state(pdev
);
666 if (priv
->common
.mode
!= IEEE80211_IF_TYPE_INVALID
) {
668 ieee80211_start_queues(dev
);
673 #endif /* CONFIG_PM */
675 static struct pci_driver p54p_driver
= {
676 .name
= "prism54pci",
677 .id_table
= p54p_table
,
679 .remove
= __devexit_p(p54p_remove
),
681 .suspend
= p54p_suspend
,
682 .resume
= p54p_resume
,
683 #endif /* CONFIG_PM */
686 static int __init
p54p_init(void)
688 return pci_register_driver(&p54p_driver
);
691 static void __exit
p54p_exit(void)
693 pci_unregister_driver(&p54p_driver
);
696 module_init(p54p_init
);
697 module_exit(p54p_exit
);