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/pci.h>
17 #include <linux/slab.h>
18 #include <linux/firmware.h>
19 #include <linux/etherdevice.h>
20 #include <linux/delay.h>
21 #include <linux/completion.h>
22 #include <linux/module.h>
23 #include <net/mac80211.h>
29 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
30 MODULE_DESCRIPTION("Prism54 PCI wireless driver");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("prism54pci");
33 MODULE_FIRMWARE("isl3886pci");
35 static const struct pci_device_id p54p_table
[] = {
36 /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
37 { PCI_DEVICE(0x1260, 0x3890) },
38 /* 3COM 3CRWE154G72 Wireless LAN adapter */
39 { PCI_DEVICE(0x10b7, 0x6001) },
40 /* Intersil PRISM Indigo Wireless LAN adapter */
41 { PCI_DEVICE(0x1260, 0x3877) },
42 /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
43 { PCI_DEVICE(0x1260, 0x3886) },
44 /* Intersil PRISM Xbow Wireless LAN adapter (Symbol AP-300) */
45 { PCI_DEVICE(0x1260, 0xffff) },
49 MODULE_DEVICE_TABLE(pci
, p54p_table
);
51 static int p54p_upload_firmware(struct ieee80211_hw
*dev
)
53 struct p54p_priv
*priv
= dev
->priv
;
57 u32 remains
, left
, device_addr
;
59 P54P_WRITE(int_enable
, cpu_to_le32(0));
60 P54P_READ(int_enable
);
63 reg
= P54P_READ(ctrl_stat
);
64 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
65 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT
);
66 P54P_WRITE(ctrl_stat
, reg
);
70 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RESET
);
71 P54P_WRITE(ctrl_stat
, reg
);
75 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
76 P54P_WRITE(ctrl_stat
, reg
);
79 /* wait for the firmware to reset properly */
82 err
= p54_parse_firmware(dev
, priv
->firmware
);
86 if (priv
->common
.fw_interface
!= FW_LM86
) {
87 dev_err(&priv
->pdev
->dev
, "wrong firmware, "
88 "please get a LM86(PCI) firmware a try again.\n");
92 data
= (__le32
*) priv
->firmware
->data
;
93 remains
= priv
->firmware
->size
;
94 device_addr
= ISL38XX_DEV_FIRMWARE_ADDR
;
97 left
= min((u32
)0x1000, remains
);
98 P54P_WRITE(direct_mem_base
, cpu_to_le32(device_addr
));
99 P54P_READ(int_enable
);
101 device_addr
+= 0x1000;
103 P54P_WRITE(direct_mem_win
[i
], *data
++);
108 P54P_READ(int_enable
);
111 reg
= P54P_READ(ctrl_stat
);
112 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN
);
113 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
114 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT
);
115 P54P_WRITE(ctrl_stat
, reg
);
116 P54P_READ(ctrl_stat
);
119 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RESET
);
120 P54P_WRITE(ctrl_stat
, reg
);
124 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
125 P54P_WRITE(ctrl_stat
, reg
);
129 /* wait for the firmware to boot properly */
135 static void p54p_refill_rx_ring(struct ieee80211_hw
*dev
,
136 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
137 struct sk_buff
**rx_buf
, u32 index
)
139 struct p54p_priv
*priv
= dev
->priv
;
140 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
143 idx
= le32_to_cpu(ring_control
->host_idx
[ring_index
]);
146 limit
= ring_limit
- limit
;
148 i
= idx
% ring_limit
;
149 while (limit
-- > 1) {
150 struct p54p_desc
*desc
= &ring
[i
];
152 if (!desc
->host_addr
) {
155 skb
= dev_alloc_skb(priv
->common
.rx_mtu
+ 32);
159 mapping
= pci_map_single(priv
->pdev
,
160 skb_tail_pointer(skb
),
161 priv
->common
.rx_mtu
+ 32,
164 if (pci_dma_mapping_error(priv
->pdev
, mapping
)) {
165 dev_kfree_skb_any(skb
);
166 dev_err(&priv
->pdev
->dev
,
167 "RX DMA Mapping error\n");
171 desc
->host_addr
= cpu_to_le32(mapping
);
172 desc
->device_addr
= 0; // FIXME: necessary?
173 desc
->len
= cpu_to_le16(priv
->common
.rx_mtu
+ 32);
184 ring_control
->host_idx
[ring_index
] = cpu_to_le32(idx
);
187 static void p54p_check_rx_ring(struct ieee80211_hw
*dev
, u32
*index
,
188 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
189 struct sk_buff
**rx_buf
)
191 struct p54p_priv
*priv
= dev
->priv
;
192 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
193 struct p54p_desc
*desc
;
196 i
= (*index
) % ring_limit
;
197 (*index
) = idx
= le32_to_cpu(ring_control
->device_idx
[ring_index
]);
204 len
= le16_to_cpu(desc
->len
);
213 if (unlikely(len
> priv
->common
.rx_mtu
)) {
215 dev_err(&priv
->pdev
->dev
, "rx'd frame size "
216 "exceeds length threshold.\n");
218 len
= priv
->common
.rx_mtu
;
220 dma_addr
= le32_to_cpu(desc
->host_addr
);
221 pci_dma_sync_single_for_cpu(priv
->pdev
, dma_addr
,
222 priv
->common
.rx_mtu
+ 32, PCI_DMA_FROMDEVICE
);
225 if (p54_rx(dev
, skb
)) {
226 pci_unmap_single(priv
->pdev
, dma_addr
,
227 priv
->common
.rx_mtu
+ 32, PCI_DMA_FROMDEVICE
);
229 desc
->host_addr
= cpu_to_le32(0);
232 pci_dma_sync_single_for_device(priv
->pdev
, dma_addr
,
233 priv
->common
.rx_mtu
+ 32, PCI_DMA_FROMDEVICE
);
234 desc
->len
= cpu_to_le16(priv
->common
.rx_mtu
+ 32);
241 p54p_refill_rx_ring(dev
, ring_index
, ring
, ring_limit
, rx_buf
, *index
);
244 static void p54p_check_tx_ring(struct ieee80211_hw
*dev
, u32
*index
,
245 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
246 struct sk_buff
**tx_buf
)
248 struct p54p_priv
*priv
= dev
->priv
;
249 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
250 struct p54p_desc
*desc
;
254 i
= (*index
) % ring_limit
;
255 (*index
) = idx
= le32_to_cpu(ring_control
->device_idx
[ring_index
]);
264 pci_unmap_single(priv
->pdev
, le32_to_cpu(desc
->host_addr
),
265 le16_to_cpu(desc
->len
), PCI_DMA_TODEVICE
);
268 desc
->device_addr
= 0;
272 if (skb
&& FREE_AFTER_TX(skb
))
273 p54_free_skb(dev
, skb
);
280 static void p54p_tasklet(unsigned long dev_id
)
282 struct ieee80211_hw
*dev
= (struct ieee80211_hw
*)dev_id
;
283 struct p54p_priv
*priv
= dev
->priv
;
284 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
286 p54p_check_tx_ring(dev
, &priv
->tx_idx_mgmt
, 3, ring_control
->tx_mgmt
,
287 ARRAY_SIZE(ring_control
->tx_mgmt
),
290 p54p_check_tx_ring(dev
, &priv
->tx_idx_data
, 1, ring_control
->tx_data
,
291 ARRAY_SIZE(ring_control
->tx_data
),
294 p54p_check_rx_ring(dev
, &priv
->rx_idx_mgmt
, 2, ring_control
->rx_mgmt
,
295 ARRAY_SIZE(ring_control
->rx_mgmt
), priv
->rx_buf_mgmt
);
297 p54p_check_rx_ring(dev
, &priv
->rx_idx_data
, 0, ring_control
->rx_data
,
298 ARRAY_SIZE(ring_control
->rx_data
), priv
->rx_buf_data
);
301 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
304 static irqreturn_t
p54p_interrupt(int irq
, void *dev_id
)
306 struct ieee80211_hw
*dev
= dev_id
;
307 struct p54p_priv
*priv
= dev
->priv
;
310 reg
= P54P_READ(int_ident
);
311 if (unlikely(reg
== cpu_to_le32(0xFFFFFFFF))) {
314 P54P_WRITE(int_ack
, reg
);
316 reg
&= P54P_READ(int_enable
);
318 if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
))
319 tasklet_schedule(&priv
->tasklet
);
320 else if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_INIT
))
321 complete(&priv
->boot_comp
);
324 return reg
? IRQ_HANDLED
: IRQ_NONE
;
327 static void p54p_tx(struct ieee80211_hw
*dev
, struct sk_buff
*skb
)
330 struct p54p_priv
*priv
= dev
->priv
;
331 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
332 struct p54p_desc
*desc
;
336 spin_lock_irqsave(&priv
->lock
, flags
);
337 idx
= le32_to_cpu(ring_control
->host_idx
[1]);
338 i
= idx
% ARRAY_SIZE(ring_control
->tx_data
);
340 mapping
= pci_map_single(priv
->pdev
, skb
->data
, skb
->len
,
342 if (pci_dma_mapping_error(priv
->pdev
, mapping
)) {
343 spin_unlock_irqrestore(&priv
->lock
, flags
);
344 p54_free_skb(dev
, skb
);
345 dev_err(&priv
->pdev
->dev
, "TX DMA mapping error\n");
348 priv
->tx_buf_data
[i
] = skb
;
350 desc
= &ring_control
->tx_data
[i
];
351 desc
->host_addr
= cpu_to_le32(mapping
);
352 desc
->device_addr
= ((struct p54_hdr
*)skb
->data
)->req_id
;
353 desc
->len
= cpu_to_le16(skb
->len
);
357 ring_control
->host_idx
[1] = cpu_to_le32(idx
+ 1);
358 spin_unlock_irqrestore(&priv
->lock
, flags
);
360 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
364 static void p54p_stop(struct ieee80211_hw
*dev
)
366 struct p54p_priv
*priv
= dev
->priv
;
367 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
369 struct p54p_desc
*desc
;
371 P54P_WRITE(int_enable
, cpu_to_le32(0));
372 P54P_READ(int_enable
);
375 free_irq(priv
->pdev
->irq
, dev
);
377 tasklet_kill(&priv
->tasklet
);
379 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
381 for (i
= 0; i
< ARRAY_SIZE(priv
->rx_buf_data
); i
++) {
382 desc
= &ring_control
->rx_data
[i
];
384 pci_unmap_single(priv
->pdev
,
385 le32_to_cpu(desc
->host_addr
),
386 priv
->common
.rx_mtu
+ 32,
388 kfree_skb(priv
->rx_buf_data
[i
]);
389 priv
->rx_buf_data
[i
] = NULL
;
392 for (i
= 0; i
< ARRAY_SIZE(priv
->rx_buf_mgmt
); i
++) {
393 desc
= &ring_control
->rx_mgmt
[i
];
395 pci_unmap_single(priv
->pdev
,
396 le32_to_cpu(desc
->host_addr
),
397 priv
->common
.rx_mtu
+ 32,
399 kfree_skb(priv
->rx_buf_mgmt
[i
]);
400 priv
->rx_buf_mgmt
[i
] = NULL
;
403 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_buf_data
); i
++) {
404 desc
= &ring_control
->tx_data
[i
];
406 pci_unmap_single(priv
->pdev
,
407 le32_to_cpu(desc
->host_addr
),
408 le16_to_cpu(desc
->len
),
411 p54_free_skb(dev
, priv
->tx_buf_data
[i
]);
412 priv
->tx_buf_data
[i
] = NULL
;
415 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_buf_mgmt
); i
++) {
416 desc
= &ring_control
->tx_mgmt
[i
];
418 pci_unmap_single(priv
->pdev
,
419 le32_to_cpu(desc
->host_addr
),
420 le16_to_cpu(desc
->len
),
423 p54_free_skb(dev
, priv
->tx_buf_mgmt
[i
]);
424 priv
->tx_buf_mgmt
[i
] = NULL
;
427 memset(ring_control
, 0, sizeof(*ring_control
));
430 static int p54p_open(struct ieee80211_hw
*dev
)
432 struct p54p_priv
*priv
= dev
->priv
;
436 init_completion(&priv
->boot_comp
);
437 err
= request_irq(priv
->pdev
->irq
, p54p_interrupt
,
438 IRQF_SHARED
, "p54pci", dev
);
440 dev_err(&priv
->pdev
->dev
, "failed to register IRQ handler\n");
444 memset(priv
->ring_control
, 0, sizeof(*priv
->ring_control
));
445 err
= p54p_upload_firmware(dev
);
447 free_irq(priv
->pdev
->irq
, dev
);
450 priv
->rx_idx_data
= priv
->tx_idx_data
= 0;
451 priv
->rx_idx_mgmt
= priv
->tx_idx_mgmt
= 0;
453 p54p_refill_rx_ring(dev
, 0, priv
->ring_control
->rx_data
,
454 ARRAY_SIZE(priv
->ring_control
->rx_data
), priv
->rx_buf_data
, 0);
456 p54p_refill_rx_ring(dev
, 2, priv
->ring_control
->rx_mgmt
,
457 ARRAY_SIZE(priv
->ring_control
->rx_mgmt
), priv
->rx_buf_mgmt
, 0);
459 P54P_WRITE(ring_control_base
, cpu_to_le32(priv
->ring_control_dma
));
460 P54P_READ(ring_control_base
);
464 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_INIT
));
465 P54P_READ(int_enable
);
469 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
472 timeout
= wait_for_completion_interruptible_timeout(
473 &priv
->boot_comp
, HZ
);
475 wiphy_err(dev
->wiphy
, "Cannot boot firmware!\n");
477 return timeout
? -ERESTARTSYS
: -ETIMEDOUT
;
480 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
));
481 P54P_READ(int_enable
);
485 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
493 static void p54p_firmware_step2(const struct firmware
*fw
,
496 struct p54p_priv
*priv
= context
;
497 struct ieee80211_hw
*dev
= priv
->common
.hw
;
498 struct pci_dev
*pdev
= priv
->pdev
;
502 dev_err(&pdev
->dev
, "Cannot find firmware (isl3886pci)\n");
509 err
= p54p_open(dev
);
512 err
= p54_read_eeprom(dev
);
517 err
= p54_register_common(dev
, &pdev
->dev
);
523 complete(&priv
->fw_loaded
);
526 struct device
*parent
= pdev
->dev
.parent
;
532 * This will indirectly result in a call to p54p_remove.
533 * Hence, we don't need to bother with freeing any
534 * allocated ressources at all.
536 device_release_driver(&pdev
->dev
);
539 device_unlock(parent
);
545 static int p54p_probe(struct pci_dev
*pdev
,
546 const struct pci_device_id
*id
)
548 struct p54p_priv
*priv
;
549 struct ieee80211_hw
*dev
;
550 unsigned long mem_addr
, mem_len
;
554 err
= pci_enable_device(pdev
);
556 dev_err(&pdev
->dev
, "Cannot enable new PCI device\n");
560 mem_addr
= pci_resource_start(pdev
, 0);
561 mem_len
= pci_resource_len(pdev
, 0);
562 if (mem_len
< sizeof(struct p54p_csr
)) {
563 dev_err(&pdev
->dev
, "Too short PCI resources\n");
565 goto err_disable_dev
;
568 err
= pci_request_regions(pdev
, "p54pci");
570 dev_err(&pdev
->dev
, "Cannot obtain PCI resources\n");
571 goto err_disable_dev
;
574 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
576 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
578 dev_err(&pdev
->dev
, "No suitable DMA available\n");
582 pci_set_master(pdev
);
583 pci_try_set_mwi(pdev
);
585 pci_write_config_byte(pdev
, 0x40, 0);
586 pci_write_config_byte(pdev
, 0x41, 0);
588 dev
= p54_init_common(sizeof(*priv
));
590 dev_err(&pdev
->dev
, "ieee80211 alloc failed\n");
598 init_completion(&priv
->fw_loaded
);
599 SET_IEEE80211_DEV(dev
, &pdev
->dev
);
600 pci_set_drvdata(pdev
, dev
);
602 priv
->map
= ioremap(mem_addr
, mem_len
);
604 dev_err(&pdev
->dev
, "Cannot map device memory\n");
609 priv
->ring_control
= pci_alloc_consistent(pdev
, sizeof(*priv
->ring_control
),
610 &priv
->ring_control_dma
);
611 if (!priv
->ring_control
) {
612 dev_err(&pdev
->dev
, "Cannot allocate rings\n");
616 priv
->common
.open
= p54p_open
;
617 priv
->common
.stop
= p54p_stop
;
618 priv
->common
.tx
= p54p_tx
;
620 spin_lock_init(&priv
->lock
);
621 tasklet_init(&priv
->tasklet
, p54p_tasklet
, (unsigned long)dev
);
623 err
= request_firmware_nowait(THIS_MODULE
, 1, "isl3886pci",
624 &priv
->pdev
->dev
, GFP_KERNEL
,
625 priv
, p54p_firmware_step2
);
629 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
630 priv
->ring_control
, priv
->ring_control_dma
);
636 p54_free_common(dev
);
639 pci_release_regions(pdev
);
641 pci_disable_device(pdev
);
646 static void p54p_remove(struct pci_dev
*pdev
)
648 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
649 struct p54p_priv
*priv
;
655 wait_for_completion(&priv
->fw_loaded
);
656 p54_unregister_common(dev
);
657 release_firmware(priv
->firmware
);
658 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
659 priv
->ring_control
, priv
->ring_control_dma
);
661 pci_release_regions(pdev
);
662 pci_disable_device(pdev
);
663 p54_free_common(dev
);
666 #ifdef CONFIG_PM_SLEEP
667 static int p54p_suspend(struct device
*device
)
669 struct pci_dev
*pdev
= to_pci_dev(device
);
671 pci_save_state(pdev
);
672 pci_set_power_state(pdev
, PCI_D3hot
);
673 pci_disable_device(pdev
);
677 static int p54p_resume(struct device
*device
)
679 struct pci_dev
*pdev
= to_pci_dev(device
);
682 err
= pci_reenable_device(pdev
);
685 return pci_set_power_state(pdev
, PCI_D0
);
688 static SIMPLE_DEV_PM_OPS(p54pci_pm_ops
, p54p_suspend
, p54p_resume
);
690 #define P54P_PM_OPS (&p54pci_pm_ops)
692 #define P54P_PM_OPS (NULL)
693 #endif /* CONFIG_PM_SLEEP */
695 static struct pci_driver p54p_driver
= {
697 .id_table
= p54p_table
,
699 .remove
= p54p_remove
,
700 .driver
.pm
= P54P_PM_OPS
,
703 module_pci_driver(p54p_driver
);