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>
28 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
29 MODULE_DESCRIPTION("Prism54 PCI wireless driver");
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS("prism54pci");
32 MODULE_FIRMWARE("isl3886pci");
34 static DEFINE_PCI_DEVICE_TABLE(p54p_table
) = {
35 /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
36 { PCI_DEVICE(0x1260, 0x3890) },
37 /* 3COM 3CRWE154G72 Wireless LAN adapter */
38 { PCI_DEVICE(0x10b7, 0x6001) },
39 /* Intersil PRISM Indigo Wireless LAN adapter */
40 { PCI_DEVICE(0x1260, 0x3877) },
41 /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
42 { PCI_DEVICE(0x1260, 0x3886) },
46 MODULE_DEVICE_TABLE(pci
, p54p_table
);
48 static int p54p_upload_firmware(struct ieee80211_hw
*dev
)
50 struct p54p_priv
*priv
= dev
->priv
;
54 u32 remains
, left
, device_addr
;
56 P54P_WRITE(int_enable
, cpu_to_le32(0));
57 P54P_READ(int_enable
);
60 reg
= P54P_READ(ctrl_stat
);
61 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
62 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT
);
63 P54P_WRITE(ctrl_stat
, reg
);
67 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RESET
);
68 P54P_WRITE(ctrl_stat
, reg
);
72 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
73 P54P_WRITE(ctrl_stat
, reg
);
76 /* wait for the firmware to reset properly */
79 err
= p54_parse_firmware(dev
, priv
->firmware
);
83 if (priv
->common
.fw_interface
!= FW_LM86
) {
84 dev_err(&priv
->pdev
->dev
, "wrong firmware, "
85 "please get a LM86(PCI) firmware a try again.\n");
89 data
= (__le32
*) priv
->firmware
->data
;
90 remains
= priv
->firmware
->size
;
91 device_addr
= ISL38XX_DEV_FIRMWARE_ADDR
;
94 left
= min((u32
)0x1000, remains
);
95 P54P_WRITE(direct_mem_base
, cpu_to_le32(device_addr
));
96 P54P_READ(int_enable
);
98 device_addr
+= 0x1000;
100 P54P_WRITE(direct_mem_win
[i
], *data
++);
105 P54P_READ(int_enable
);
108 reg
= P54P_READ(ctrl_stat
);
109 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN
);
110 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
111 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT
);
112 P54P_WRITE(ctrl_stat
, reg
);
113 P54P_READ(ctrl_stat
);
116 reg
|= cpu_to_le32(ISL38XX_CTRL_STAT_RESET
);
117 P54P_WRITE(ctrl_stat
, reg
);
121 reg
&= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET
);
122 P54P_WRITE(ctrl_stat
, reg
);
126 /* wait for the firmware to boot properly */
132 static void p54p_refill_rx_ring(struct ieee80211_hw
*dev
,
133 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
134 struct sk_buff
**rx_buf
)
136 struct p54p_priv
*priv
= dev
->priv
;
137 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
140 idx
= le32_to_cpu(ring_control
->host_idx
[ring_index
]);
142 limit
-= le32_to_cpu(ring_control
->device_idx
[ring_index
]);
143 limit
= ring_limit
- limit
;
145 i
= idx
% ring_limit
;
146 while (limit
-- > 1) {
147 struct p54p_desc
*desc
= &ring
[i
];
149 if (!desc
->host_addr
) {
152 skb
= dev_alloc_skb(priv
->common
.rx_mtu
+ 32);
156 mapping
= pci_map_single(priv
->pdev
,
157 skb_tail_pointer(skb
),
158 priv
->common
.rx_mtu
+ 32,
160 desc
->host_addr
= cpu_to_le32(mapping
);
161 desc
->device_addr
= 0; // FIXME: necessary?
162 desc
->len
= cpu_to_le16(priv
->common
.rx_mtu
+ 32);
173 ring_control
->host_idx
[ring_index
] = cpu_to_le32(idx
);
176 static void p54p_check_rx_ring(struct ieee80211_hw
*dev
, u32
*index
,
177 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
178 struct sk_buff
**rx_buf
)
180 struct p54p_priv
*priv
= dev
->priv
;
181 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
182 struct p54p_desc
*desc
;
185 i
= (*index
) % ring_limit
;
186 (*index
) = idx
= le32_to_cpu(ring_control
->device_idx
[ring_index
]);
192 len
= le16_to_cpu(desc
->len
);
202 if (p54_rx(dev
, skb
)) {
203 pci_unmap_single(priv
->pdev
,
204 le32_to_cpu(desc
->host_addr
),
205 priv
->common
.rx_mtu
+ 32,
211 desc
->len
= cpu_to_le16(priv
->common
.rx_mtu
+ 32);
218 p54p_refill_rx_ring(dev
, ring_index
, ring
, ring_limit
, rx_buf
);
221 /* caller must hold priv->lock */
222 static void p54p_check_tx_ring(struct ieee80211_hw
*dev
, u32
*index
,
223 int ring_index
, struct p54p_desc
*ring
, u32 ring_limit
,
226 struct p54p_priv
*priv
= dev
->priv
;
227 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
228 struct p54p_desc
*desc
;
231 i
= (*index
) % ring_limit
;
232 (*index
) = idx
= le32_to_cpu(ring_control
->device_idx
[1]);
238 if (FREE_AFTER_TX((struct sk_buff
*) tx_buf
[i
]))
239 p54_free_skb(dev
, tx_buf
[i
]);
242 pci_unmap_single(priv
->pdev
, le32_to_cpu(desc
->host_addr
),
243 le16_to_cpu(desc
->len
), PCI_DMA_TODEVICE
);
246 desc
->device_addr
= 0;
255 static void p54p_rx_tasklet(unsigned long dev_id
)
257 struct ieee80211_hw
*dev
= (struct ieee80211_hw
*)dev_id
;
258 struct p54p_priv
*priv
= dev
->priv
;
259 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
261 p54p_check_rx_ring(dev
, &priv
->rx_idx_mgmt
, 2, ring_control
->rx_mgmt
,
262 ARRAY_SIZE(ring_control
->rx_mgmt
), priv
->rx_buf_mgmt
);
264 p54p_check_rx_ring(dev
, &priv
->rx_idx_data
, 0, ring_control
->rx_data
,
265 ARRAY_SIZE(ring_control
->rx_data
), priv
->rx_buf_data
);
268 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
271 static irqreturn_t
p54p_interrupt(int irq
, void *dev_id
)
273 struct ieee80211_hw
*dev
= dev_id
;
274 struct p54p_priv
*priv
= dev
->priv
;
275 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
278 spin_lock(&priv
->lock
);
279 reg
= P54P_READ(int_ident
);
280 if (unlikely(reg
== cpu_to_le32(0xFFFFFFFF))) {
281 spin_unlock(&priv
->lock
);
285 P54P_WRITE(int_ack
, reg
);
287 reg
&= P54P_READ(int_enable
);
289 if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
)) {
290 p54p_check_tx_ring(dev
, &priv
->tx_idx_mgmt
,
291 3, ring_control
->tx_mgmt
,
292 ARRAY_SIZE(ring_control
->tx_mgmt
),
295 p54p_check_tx_ring(dev
, &priv
->tx_idx_data
,
296 1, ring_control
->tx_data
,
297 ARRAY_SIZE(ring_control
->tx_data
),
300 tasklet_schedule(&priv
->rx_tasklet
);
302 } else if (reg
& cpu_to_le32(ISL38XX_INT_IDENT_INIT
))
303 complete(&priv
->boot_comp
);
305 spin_unlock(&priv
->lock
);
307 return reg
? IRQ_HANDLED
: IRQ_NONE
;
310 static void p54p_tx(struct ieee80211_hw
*dev
, struct sk_buff
*skb
)
312 struct p54p_priv
*priv
= dev
->priv
;
313 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
315 struct p54p_desc
*desc
;
317 u32 device_idx
, idx
, i
;
319 spin_lock_irqsave(&priv
->lock
, flags
);
321 device_idx
= le32_to_cpu(ring_control
->device_idx
[1]);
322 idx
= le32_to_cpu(ring_control
->host_idx
[1]);
323 i
= idx
% ARRAY_SIZE(ring_control
->tx_data
);
325 priv
->tx_buf_data
[i
] = skb
;
326 mapping
= pci_map_single(priv
->pdev
, skb
->data
, skb
->len
,
328 desc
= &ring_control
->tx_data
[i
];
329 desc
->host_addr
= cpu_to_le32(mapping
);
330 desc
->device_addr
= ((struct p54_hdr
*)skb
->data
)->req_id
;
331 desc
->len
= cpu_to_le16(skb
->len
);
335 ring_control
->host_idx
[1] = cpu_to_le32(idx
+ 1);
336 spin_unlock_irqrestore(&priv
->lock
, flags
);
338 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
342 static void p54p_stop(struct ieee80211_hw
*dev
)
344 struct p54p_priv
*priv
= dev
->priv
;
345 struct p54p_ring_control
*ring_control
= priv
->ring_control
;
347 struct p54p_desc
*desc
;
349 tasklet_kill(&priv
->rx_tasklet
);
351 P54P_WRITE(int_enable
, cpu_to_le32(0));
352 P54P_READ(int_enable
);
355 free_irq(priv
->pdev
->irq
, dev
);
357 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
359 for (i
= 0; i
< ARRAY_SIZE(priv
->rx_buf_data
); i
++) {
360 desc
= &ring_control
->rx_data
[i
];
362 pci_unmap_single(priv
->pdev
,
363 le32_to_cpu(desc
->host_addr
),
364 priv
->common
.rx_mtu
+ 32,
366 kfree_skb(priv
->rx_buf_data
[i
]);
367 priv
->rx_buf_data
[i
] = NULL
;
370 for (i
= 0; i
< ARRAY_SIZE(priv
->rx_buf_mgmt
); i
++) {
371 desc
= &ring_control
->rx_mgmt
[i
];
373 pci_unmap_single(priv
->pdev
,
374 le32_to_cpu(desc
->host_addr
),
375 priv
->common
.rx_mtu
+ 32,
377 kfree_skb(priv
->rx_buf_mgmt
[i
]);
378 priv
->rx_buf_mgmt
[i
] = NULL
;
381 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_buf_data
); i
++) {
382 desc
= &ring_control
->tx_data
[i
];
384 pci_unmap_single(priv
->pdev
,
385 le32_to_cpu(desc
->host_addr
),
386 le16_to_cpu(desc
->len
),
389 p54_free_skb(dev
, priv
->tx_buf_data
[i
]);
390 priv
->tx_buf_data
[i
] = NULL
;
393 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_buf_mgmt
); i
++) {
394 desc
= &ring_control
->tx_mgmt
[i
];
396 pci_unmap_single(priv
->pdev
,
397 le32_to_cpu(desc
->host_addr
),
398 le16_to_cpu(desc
->len
),
401 p54_free_skb(dev
, priv
->tx_buf_mgmt
[i
]);
402 priv
->tx_buf_mgmt
[i
] = NULL
;
405 memset(ring_control
, 0, sizeof(*ring_control
));
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
, "p54pci", dev
);
417 dev_err(&priv
->pdev
->dev
, "failed to register IRQ handler\n");
421 memset(priv
->ring_control
, 0, sizeof(*priv
->ring_control
));
422 err
= p54p_upload_firmware(dev
);
424 free_irq(priv
->pdev
->irq
, dev
);
427 priv
->rx_idx_data
= priv
->tx_idx_data
= 0;
428 priv
->rx_idx_mgmt
= priv
->tx_idx_mgmt
= 0;
430 p54p_refill_rx_ring(dev
, 0, priv
->ring_control
->rx_data
,
431 ARRAY_SIZE(priv
->ring_control
->rx_data
), priv
->rx_buf_data
);
433 p54p_refill_rx_ring(dev
, 2, priv
->ring_control
->rx_mgmt
,
434 ARRAY_SIZE(priv
->ring_control
->rx_mgmt
), priv
->rx_buf_mgmt
);
436 P54P_WRITE(ring_control_base
, cpu_to_le32(priv
->ring_control_dma
));
437 P54P_READ(ring_control_base
);
441 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_INIT
));
442 P54P_READ(int_enable
);
446 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_RESET
));
449 if (!wait_for_completion_interruptible_timeout(&priv
->boot_comp
, HZ
)) {
450 printk(KERN_ERR
"%s: Cannot boot firmware!\n",
451 wiphy_name(dev
->wiphy
));
456 P54P_WRITE(int_enable
, cpu_to_le32(ISL38XX_INT_IDENT_UPDATE
));
457 P54P_READ(int_enable
);
461 P54P_WRITE(dev_int
, cpu_to_le32(ISL38XX_DEV_INT_UPDATE
));
469 static int __devinit
p54p_probe(struct pci_dev
*pdev
,
470 const struct pci_device_id
*id
)
472 struct p54p_priv
*priv
;
473 struct ieee80211_hw
*dev
;
474 unsigned long mem_addr
, mem_len
;
477 err
= pci_enable_device(pdev
);
479 dev_err(&pdev
->dev
, "Cannot enable new PCI device\n");
483 mem_addr
= pci_resource_start(pdev
, 0);
484 mem_len
= pci_resource_len(pdev
, 0);
485 if (mem_len
< sizeof(struct p54p_csr
)) {
486 dev_err(&pdev
->dev
, "Too short PCI resources\n");
487 goto err_disable_dev
;
490 err
= pci_request_regions(pdev
, "p54pci");
492 dev_err(&pdev
->dev
, "Cannot obtain PCI resources\n");
493 goto err_disable_dev
;
496 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) ||
497 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32))) {
498 dev_err(&pdev
->dev
, "No suitable DMA available\n");
502 pci_set_master(pdev
);
503 pci_try_set_mwi(pdev
);
505 pci_write_config_byte(pdev
, 0x40, 0);
506 pci_write_config_byte(pdev
, 0x41, 0);
508 dev
= p54_init_common(sizeof(*priv
));
510 dev_err(&pdev
->dev
, "ieee80211 alloc failed\n");
518 SET_IEEE80211_DEV(dev
, &pdev
->dev
);
519 pci_set_drvdata(pdev
, dev
);
521 priv
->map
= ioremap(mem_addr
, mem_len
);
523 dev_err(&pdev
->dev
, "Cannot map device memory\n");
528 priv
->ring_control
= pci_alloc_consistent(pdev
, sizeof(*priv
->ring_control
),
529 &priv
->ring_control_dma
);
530 if (!priv
->ring_control
) {
531 dev_err(&pdev
->dev
, "Cannot allocate rings\n");
535 priv
->common
.open
= p54p_open
;
536 priv
->common
.stop
= p54p_stop
;
537 priv
->common
.tx
= p54p_tx
;
539 spin_lock_init(&priv
->lock
);
540 tasklet_init(&priv
->rx_tasklet
, p54p_rx_tasklet
, (unsigned long)dev
);
542 err
= request_firmware(&priv
->firmware
, "isl3886pci",
545 dev_err(&pdev
->dev
, "Cannot find firmware (isl3886pci)\n");
546 err
= request_firmware(&priv
->firmware
, "isl3886",
549 goto err_free_common
;
552 err
= p54p_open(dev
);
554 goto err_free_common
;
555 err
= p54_read_eeprom(dev
);
558 goto err_free_common
;
560 err
= p54_register_common(dev
, &pdev
->dev
);
562 goto err_free_common
;
567 release_firmware(priv
->firmware
);
568 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
569 priv
->ring_control
, priv
->ring_control_dma
);
575 pci_set_drvdata(pdev
, NULL
);
576 p54_free_common(dev
);
579 pci_release_regions(pdev
);
581 pci_disable_device(pdev
);
585 static void __devexit
p54p_remove(struct pci_dev
*pdev
)
587 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
588 struct p54p_priv
*priv
;
593 p54_unregister_common(dev
);
595 release_firmware(priv
->firmware
);
596 pci_free_consistent(pdev
, sizeof(*priv
->ring_control
),
597 priv
->ring_control
, priv
->ring_control_dma
);
599 pci_release_regions(pdev
);
600 pci_disable_device(pdev
);
601 p54_free_common(dev
);
605 static int p54p_suspend(struct pci_dev
*pdev
, pm_message_t state
)
607 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
608 struct p54p_priv
*priv
= dev
->priv
;
610 if (priv
->common
.mode
!= NL80211_IFTYPE_UNSPECIFIED
) {
611 ieee80211_stop_queues(dev
);
615 pci_save_state(pdev
);
616 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
620 static int p54p_resume(struct pci_dev
*pdev
)
622 struct ieee80211_hw
*dev
= pci_get_drvdata(pdev
);
623 struct p54p_priv
*priv
= dev
->priv
;
625 pci_set_power_state(pdev
, PCI_D0
);
626 pci_restore_state(pdev
);
628 if (priv
->common
.mode
!= NL80211_IFTYPE_UNSPECIFIED
) {
630 ieee80211_wake_queues(dev
);
635 #endif /* CONFIG_PM */
637 static struct pci_driver p54p_driver
= {
639 .id_table
= p54p_table
,
641 .remove
= __devexit_p(p54p_remove
),
643 .suspend
= p54p_suspend
,
644 .resume
= p54p_resume
,
645 #endif /* CONFIG_PM */
648 static int __init
p54p_init(void)
650 return pci_register_driver(&p54p_driver
);
653 static void __exit
p54p_exit(void)
655 pci_unregister_driver(&p54p_driver
);
658 module_init(p54p_init
);
659 module_exit(p54p_exit
);