[PATCH] orinoco: reduce differences between PCI drivers, create orinoco_pci.h
[powerpc.git] / drivers / net / wireless / orinoco_plx.c
1 /* orinoco_plx.c
2  *
3  * Driver for Prism II devices which would usually be driven by orinoco_cs,
4  * but are connected to the PCI bus by a PLX9052.
5  *
6  * Current maintainers (as of 29 September 2003) are:
7  *      Pavel Roskin <proski AT gnu.org>
8  * and  David Gibson <hermes AT gibson.dropbear.id.au>
9  *
10  * (C) Copyright David Gibson, IBM Corp. 2001-2003.
11  * Copyright (C) 2001 Daniel Barlow
12  *
13  * The contents of this file are subject to the Mozilla Public License
14  * Version 1.1 (the "License"); you may not use this file except in
15  * compliance with the License. You may obtain a copy of the License
16  * at http://www.mozilla.org/MPL/
17  *
18  * Software distributed under the License is distributed on an "AS IS"
19  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20  * the License for the specific language governing rights and
21  * limitations under the License.
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the MPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the MPL or the GPL.
33
34  * Caution: this is experimental and probably buggy.  For success and
35  * failure reports for different cards and adaptors, see
36  * orinoco_plx_id_table near the end of the file.  If you have a
37  * card we don't have the PCI id for, and looks like it should work,
38  * drop me mail with the id and "it works"/"it doesn't work".
39  *
40  * Note: if everything gets detected fine but it doesn't actually send
41  * or receive packets, your first port of call should probably be to
42  * try newer firmware in the card.  Especially if you're doing Ad-Hoc
43  * modes.
44  *
45  * The actual driving is done by orinoco.c, this is just resource
46  * allocation stuff.  The explanation below is courtesy of Ryan Niemi
47  * on the linux-wlan-ng list at
48  * http://archives.neohapsis.com/archives/dev/linux-wlan/2001-q1/0026.html
49  *
50  * The PLX9052-based cards (WL11000 and several others) are a
51  * different beast than the usual PCMCIA-based PRISM2 configuration
52  * expected by wlan-ng.  Here's the general details on how the WL11000
53  * PCI adapter works:
54  *
55  * - Two PCI I/O address spaces, one 0x80 long which contains the
56  * PLX9052 registers, and one that's 0x40 long mapped to the PCMCIA
57  * slot I/O address space.
58  *
59  * - One PCI memory address space, mapped to the PCMCIA memory space
60  * (containing the CIS).
61  *
62  * After identifying the I/O and memory space, you can read through
63  * the memory space to confirm the CIS's device ID or manufacturer ID
64  * to make sure it's the expected card.  qKeep in mind that the PCMCIA
65  * spec specifies the CIS as the lower 8 bits of each word read from
66  * the CIS, so to read the bytes of the CIS, read every other byte
67  * (0,2,4,...). Passing that test, you need to enable the I/O address
68  * space on the PCMCIA card via the PCMCIA COR register. This is the
69  * first byte following the CIS. In my case (which may not have any
70  * relation to what's on the PRISM2 cards), COR was at offset 0x800
71  * within the PCI memory space. Write 0x41 to the COR register to
72  * enable I/O mode and to select level triggered interrupts. To
73  * confirm you actually succeeded, read the COR register back and make
74  * sure it actually got set to 0x41, incase you have an unexpected
75  * card inserted.
76  *
77  * Following that, you can treat the second PCI I/O address space (the
78  * one that's not 0x80 in length) as the PCMCIA I/O space.
79  *
80  * Note that in the Eumitcom's source for their drivers, they register
81  * the interrupt as edge triggered when registering it with the
82  * Windows kernel. I don't recall how to register edge triggered on
83  * Linux (if it can be done at all). But in some experimentation, I
84  * don't see much operational difference between using either
85  * interrupt mode. Don't mess with the interrupt mode in the COR
86  * register though, as the PLX9052 wants level triggers with the way
87  * the serial EEPROM configures it on the WL11000.
88  *
89  * There's some other little quirks related to timing that I bumped
90  * into, but I don't recall right now. Also, there's two variants of
91  * the WL11000 I've seen, revision A1 and T2. These seem to differ
92  * slightly in the timings configured in the wait-state generator in
93  * the PLX9052. There have also been some comments from Eumitcom that
94  * cards shouldn't be hot swapped, apparently due to risk of cooking
95  * the PLX9052. I'm unsure why they believe this, as I can't see
96  * anything in the design that would really cause a problem, except
97  * for crashing drivers not written to expect it. And having developed
98  * drivers for the WL11000, I'd say it's quite tricky to write code
99  * that will successfully deal with a hot unplug. Very odd things
100  * happen on the I/O side of things. But anyway, be warned. Despite
101  * that, I've hot-swapped a number of times during debugging and
102  * driver development for various reasons (stuck WAIT# line after the
103  * radio card's firmware locks up).
104  *
105  * Hope this is enough info for someone to add PLX9052 support to the
106  * wlan-ng card. In the case of the WL11000, the PCI ID's are
107  * 0x1639/0x0200, with matching subsystem ID's. Other PLX9052-based
108  * manufacturers other than Eumitcom (or on cards other than the
109  * WL11000) may have different PCI ID's.
110  *
111  * If anyone needs any more specific info, let me know. I haven't had
112  * time to implement support myself yet, and with the way things are
113  * going, might not have time for a while..
114  */
115
116 #define DRIVER_NAME "orinoco_plx"
117 #define PFX DRIVER_NAME ": "
118
119 #include <linux/config.h>
120 #include <linux/module.h>
121 #include <linux/kernel.h>
122 #include <linux/init.h>
123 #include <linux/delay.h>
124 #include <linux/pci.h>
125 #include <pcmcia/cisreg.h>
126
127 #include "orinoco.h"
128 #include "orinoco_pci.h"
129
130 #define COR_OFFSET      (0x3e0) /* COR attribute offset of Prism2 PC card */
131 #define COR_VALUE       (COR_LEVEL_REQ | COR_FUNC_ENA) /* Enable PC card with interrupt in level trigger */
132 #define COR_RESET     (0x80)    /* reset bit in the COR register */
133 #define PLX_RESET_TIME  (500)   /* milliseconds */
134
135 #define PLX_INTCSR              0x4c /* Interrupt Control & Status Register */
136 #define PLX_INTCSR_INTEN        (1<<6) /* Interrupt Enable bit */
137
138 /*
139  * Do a soft reset of the card using the Configuration Option Register
140  */
141 static int orinoco_plx_cor_reset(struct orinoco_private *priv)
142 {
143         hermes_t *hw = &priv->hw;
144         struct orinoco_pci_card *card = priv->card;
145         unsigned long timeout;
146         u16 reg;
147
148         iowrite8(COR_VALUE | COR_RESET, card->attr_io + COR_OFFSET);
149         mdelay(1);
150
151         iowrite8(COR_VALUE, card->attr_io + COR_OFFSET);
152         mdelay(1);
153
154         /* Just in case, wait more until the card is no longer busy */
155         timeout = jiffies + (PLX_RESET_TIME * HZ / 1000);
156         reg = hermes_read_regn(hw, CMD);
157         while (time_before(jiffies, timeout) && (reg & HERMES_CMD_BUSY)) {
158                 mdelay(1);
159                 reg = hermes_read_regn(hw, CMD);
160         }
161
162         /* Still busy? */
163         if (reg & HERMES_CMD_BUSY) {
164                 printk(KERN_ERR PFX "Busy timeout\n");
165                 return -ETIMEDOUT;
166         }
167
168         return 0;
169 }
170
171 static int orinoco_plx_hw_init(struct orinoco_pci_card *card)
172 {
173         int i;
174         u32 csr_reg;
175         static const u8 cis_magic[] = {
176                 0x01, 0x03, 0x00, 0x00, 0xff, 0x17, 0x04, 0x67
177         };
178
179         printk(KERN_DEBUG PFX "CIS: ");
180         for (i = 0; i < 16; i++) {
181                 printk("%02X:", ioread8(card->attr_io + (i << 1)));
182         }
183         printk("\n");
184
185         /* Verify whether a supported PC card is present */
186         /* FIXME: we probably need to be smarted about this */
187         for (i = 0; i < sizeof(cis_magic); i++) {
188                 if (cis_magic[i] != ioread8(card->attr_io + (i << 1))) {
189                         printk(KERN_ERR PFX "The CIS value of Prism2 PC "
190                                "card is unexpected\n");
191                         return -ENODEV;
192                 }
193         }
194
195         /* bjoern: We need to tell the card to enable interrupts, in
196            case the serial eprom didn't do this already.  See the
197            PLX9052 data book, p8-1 and 8-24 for reference. */
198         csr_reg = ioread32(card->bridge_io + PLX_INTCSR);
199         if (!(csr_reg & PLX_INTCSR_INTEN)) {
200                 csr_reg |= PLX_INTCSR_INTEN;
201                 iowrite32(csr_reg, card->bridge_io + PLX_INTCSR);
202                 csr_reg = ioread32(card->bridge_io + PLX_INTCSR);
203                 if (!(csr_reg & PLX_INTCSR_INTEN)) {
204                         printk(KERN_ERR PFX "Cannot enable interrupts\n");
205                         return -EIO;
206                 }
207         }
208
209         return 0;
210 }
211
212 static int orinoco_plx_init_one(struct pci_dev *pdev,
213                                 const struct pci_device_id *ent)
214 {
215         int err;
216         struct orinoco_private *priv;
217         struct orinoco_pci_card *card;
218         struct net_device *dev;
219         void __iomem *hermes_io, *attr_io, *bridge_io;
220
221         err = pci_enable_device(pdev);
222         if (err) {
223                 printk(KERN_ERR PFX "Cannot enable PCI device\n");
224                 return err;
225         }
226
227         err = pci_request_regions(pdev, DRIVER_NAME);
228         if (err) {
229                 printk(KERN_ERR PFX "Cannot obtain PCI resources\n");
230                 goto fail_resources;
231         }
232
233         bridge_io = pci_iomap(pdev, 1, 0);
234         if (!bridge_io) {
235                 printk(KERN_ERR PFX "Cannot map bridge registers\n");
236                 err = -EIO;
237                 goto fail_map_bridge;
238         }
239
240         attr_io = pci_iomap(pdev, 2, 0);
241         if (!attr_io) {
242                 printk(KERN_ERR PFX "Cannot map PCMCIA attributes\n");
243                 err = -EIO;
244                 goto fail_map_attr;
245         }
246
247         hermes_io = pci_iomap(pdev, 3, 0);
248         if (!hermes_io) {
249                 printk(KERN_ERR PFX "Cannot map chipset registers\n");
250                 err = -EIO;
251                 goto fail_map_hermes;
252         }
253
254         /* Allocate network device */
255         dev = alloc_orinocodev(sizeof(*card), orinoco_plx_cor_reset);
256         if (!dev) {
257                 printk(KERN_ERR PFX "Cannot allocate network device\n");
258                 err = -ENOMEM;
259                 goto fail_alloc;
260         }
261
262         priv = netdev_priv(dev);
263         card = priv->card;
264         card->bridge_io = bridge_io;
265         card->attr_io = attr_io;
266         SET_MODULE_OWNER(dev);
267         SET_NETDEV_DEV(dev, &pdev->dev);
268
269         hermes_struct_init(&priv->hw, hermes_io, HERMES_16BIT_REGSPACING);
270
271         err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ,
272                           dev->name, dev);
273         if (err) {
274                 printk(KERN_ERR PFX "Cannot allocate IRQ %d\n", pdev->irq);
275                 err = -EBUSY;
276                 goto fail_irq;
277         }
278         orinoco_pci_setup_netdev(dev, pdev, 2);
279
280         err = orinoco_plx_hw_init(card);
281         if (err) {
282                 printk(KERN_ERR PFX "Hardware initialization failed\n");
283                 goto fail;
284         }
285
286         err = orinoco_plx_cor_reset(priv);
287         if (err) {
288                 printk(KERN_ERR PFX "Initial reset failed\n");
289                 goto fail;
290         }
291
292         err = register_netdev(dev);
293         if (err) {
294                 printk(KERN_ERR PFX "Cannot register network device\n");
295                 goto fail;
296         }
297
298         pci_set_drvdata(pdev, dev);
299
300         return 0;
301
302  fail:
303         free_irq(pdev->irq, dev);
304
305  fail_irq:
306         pci_set_drvdata(pdev, NULL);
307         free_orinocodev(dev);
308
309  fail_alloc:
310         pci_iounmap(pdev, hermes_io);
311
312  fail_map_hermes:
313         pci_iounmap(pdev, attr_io);
314
315  fail_map_attr:
316         pci_iounmap(pdev, bridge_io);
317
318  fail_map_bridge:
319         pci_release_regions(pdev);
320
321  fail_resources:
322         pci_disable_device(pdev);
323
324         return err;
325 }
326
327 static void __devexit orinoco_plx_remove_one(struct pci_dev *pdev)
328 {
329         struct net_device *dev = pci_get_drvdata(pdev);
330         struct orinoco_private *priv = netdev_priv(dev);
331         struct orinoco_pci_card *card = priv->card;
332
333         unregister_netdev(dev);
334         free_irq(dev->irq, dev);
335         pci_set_drvdata(pdev, NULL);
336         free_orinocodev(dev);
337         pci_iounmap(pdev, priv->hw.iobase);
338         pci_iounmap(pdev, card->attr_io);
339         pci_iounmap(pdev, card->bridge_io);
340         pci_release_regions(pdev);
341         pci_disable_device(pdev);
342 }
343
344 static struct pci_device_id orinoco_plx_id_table[] = {
345         {0x111a, 0x1023, PCI_ANY_ID, PCI_ANY_ID,},      /* Siemens SpeedStream SS1023 */
346         {0x1385, 0x4100, PCI_ANY_ID, PCI_ANY_ID,},      /* Netgear MA301 */
347         {0x15e8, 0x0130, PCI_ANY_ID, PCI_ANY_ID,},      /* Correga  - does this work? */
348         {0x1638, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* SMC EZConnect SMC2602W,
349                                                            Eumitcom PCI WL11000,
350                                                            Addtron AWA-100 */
351         {0x16ab, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* Global Sun Tech GL24110P */
352         {0x16ab, 0x1101, PCI_ANY_ID, PCI_ANY_ID,},      /* Reported working, but unknown */
353         {0x16ab, 0x1102, PCI_ANY_ID, PCI_ANY_ID,},      /* Linksys WDT11 */
354         {0x16ec, 0x3685, PCI_ANY_ID, PCI_ANY_ID,},      /* USR 2415 */
355         {0xec80, 0xec00, PCI_ANY_ID, PCI_ANY_ID,},      /* Belkin F5D6000 tested by
356                                                            Brendan W. McAdams <rit AT jacked-in.org> */
357         {0x10b7, 0x7770, PCI_ANY_ID, PCI_ANY_ID,},      /* 3Com AirConnect PCI tested by
358                                                            Damien Persohn <damien AT persohn.net> */
359         {0,},
360 };
361
362 MODULE_DEVICE_TABLE(pci, orinoco_plx_id_table);
363
364 static struct pci_driver orinoco_plx_driver = {
365         .name           = DRIVER_NAME,
366         .id_table       = orinoco_plx_id_table,
367         .probe          = orinoco_plx_init_one,
368         .remove         = __devexit_p(orinoco_plx_remove_one),
369         .suspend        = orinoco_pci_suspend,
370         .resume         = orinoco_pci_resume,
371 };
372
373 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
374         " (Pavel Roskin <proski@gnu.org>,"
375         " David Gibson <hermes@gibson.dropbear.id.au>,"
376         " Daniel Barlow <dan@telent.net>)";
377 MODULE_AUTHOR("Daniel Barlow <dan@telent.net>");
378 MODULE_DESCRIPTION("Driver for wireless LAN cards using the PLX9052 PCI bridge");
379 MODULE_LICENSE("Dual MPL/GPL");
380
381 static int __init orinoco_plx_init(void)
382 {
383         printk(KERN_DEBUG "%s\n", version);
384         return pci_module_init(&orinoco_plx_driver);
385 }
386
387 static void __exit orinoco_plx_exit(void)
388 {
389         pci_unregister_driver(&orinoco_plx_driver);
390 }
391
392 module_init(orinoco_plx_init);
393 module_exit(orinoco_plx_exit);
394
395 /*
396  * Local variables:
397  *  c-indent-level: 8
398  *  c-basic-offset: 8
399  *  tab-width: 8
400  * End:
401  */