ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / parisc / eisa.c
1 /*
2  * eisa.c - provide support for EISA adapters in PA-RISC machines
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  * Copyright (c) 2001 Matthew Wilcox for Hewlett Packard
10  * Copyright (c) 2001 Daniel Engstrom <5116@telia.com>
11  *
12  * There are two distinct EISA adapters.  Mongoose is found in machines
13  * before the 712; then the Wax ASIC is used.  To complicate matters, the
14  * Wax ASIC also includes a PS/2 and RS-232 controller, but those are
15  * dealt with elsewhere; this file is concerned only with the EISA portions
16  * of Wax.
17  * 
18  * 
19  * HINT:
20  * -----
21  * To allow an ISA card to work properly in the EISA slot you need to
22  * set an edge trigger level. This may be done on the palo command line 
23  * by adding the kernel parameter "eisa_irq_edge=n,n2,[...]]", with 
24  * n and n2 as the irq levels you want to use.
25  * 
26  * Example: "eisa_irq_edge=10,11" allows ISA cards to operate at 
27  * irq levels 10 and 11.
28  */
29
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/irq.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/sched.h>
37 #include <linux/spinlock.h>
38 #include <linux/eisa.h>
39
40 #include <asm/byteorder.h>
41 #include <asm/io.h>
42 #include <asm/hardware.h>
43 #include <asm/processor.h>
44 #include <asm/parisc-device.h>
45 #include <asm/delay.h>
46 #include <asm/eisa_bus.h>
47
48 #if 0
49 #define EISA_DBG(msg, arg... ) printk(KERN_DEBUG "eisa: " msg , ## arg )
50 #else
51 #define EISA_DBG(msg, arg... )  
52 #endif
53
54 #define SNAKES_EEPROM_BASE_ADDR 0xF0810400
55 #define MIRAGE_EEPROM_BASE_ADDR 0xF00C0400
56
57 static spinlock_t eisa_irq_lock = SPIN_LOCK_UNLOCKED;
58
59 /* We can only have one EISA adapter in the system because neither
60  * implementation can be flexed.
61  */
62 static struct eisa_ba {
63         struct pci_hba_data     hba;
64         unsigned long eeprom_addr;
65         struct eisa_root_device root;
66 } eisa_dev;
67
68 /* Port ops */
69
70 static inline unsigned long eisa_permute(unsigned short port)
71 {
72         if (port & 0x300) {
73                 return 0xfc000000 | ((port & 0xfc00) >> 6)
74                         | ((port & 0x3f8) << 9) | (port & 7);
75         } else {
76                 return 0xfc000000 | port;
77         }
78 }
79
80 unsigned char eisa_in8(unsigned short port)
81 {
82         if (EISA_bus)
83                 return gsc_readb(eisa_permute(port));
84         return 0xff;
85 }
86
87 unsigned short eisa_in16(unsigned short port)
88 {
89         if (EISA_bus)
90                 return le16_to_cpu(gsc_readw(eisa_permute(port)));
91         return 0xffff;
92 }
93
94 unsigned int eisa_in32(unsigned short port)
95 {
96         if (EISA_bus)
97                 return le32_to_cpu(gsc_readl(eisa_permute(port)));
98         return 0xffffffff;
99 }
100
101 void eisa_out8(unsigned char data, unsigned short port)
102 {
103         if (EISA_bus)
104                 gsc_writeb(data, eisa_permute(port));
105 }
106
107 void eisa_out16(unsigned short data, unsigned short port)
108 {
109         if (EISA_bus)   
110                 gsc_writew(cpu_to_le16(data), eisa_permute(port));
111 }
112
113 void eisa_out32(unsigned int data, unsigned short port)
114 {
115         if (EISA_bus)
116                 gsc_writel(cpu_to_le32(data), eisa_permute(port));
117 }
118
119 #ifndef CONFIG_PCI
120 /* We call these directly without PCI.  See asm/io.h. */
121 EXPORT_SYMBOL(eisa_in8);
122 EXPORT_SYMBOL(eisa_in16);
123 EXPORT_SYMBOL(eisa_in32);
124 EXPORT_SYMBOL(eisa_out8);
125 EXPORT_SYMBOL(eisa_out16);
126 EXPORT_SYMBOL(eisa_out32);
127 #endif
128
129 /* Interrupt handling */
130
131 /* cached interrupt mask registers */
132 static int master_mask;
133 static int slave_mask;
134
135 /* the trig level can be set with the
136  * eisa_irq_edge=n,n,n commandline parameter 
137  * We should really read this from the EEPROM 
138  * in the furure. 
139  */
140 /* irq 13,8,2,1,0 must be edge */
141 static unsigned int eisa_irq_level; /* default to edge triggered */
142
143
144 /* called by free irq */
145 static void eisa_disable_irq(void *irq_dev, int irq)
146 {
147         unsigned long flags;
148
149         EISA_DBG("disable irq %d\n", irq);
150         /* just mask for now */
151         spin_lock_irqsave(&eisa_irq_lock, flags);
152         if (irq & 8) {
153                 slave_mask |= (1 << (irq&7));
154                 eisa_out8(slave_mask, 0xa1);
155         } else {
156                 master_mask |= (1 << (irq&7));
157                 eisa_out8(master_mask, 0x21);
158         }
159         spin_unlock_irqrestore(&eisa_irq_lock, flags);
160         EISA_DBG("pic0 mask %02x\n", eisa_in8(0x21));
161         EISA_DBG("pic1 mask %02x\n", eisa_in8(0xa1));
162 }
163
164 /* called by request irq */
165 static void eisa_enable_irq(void *irq_dev, int irq)
166 {
167         unsigned long flags;
168         EISA_DBG("enable irq %d\n", irq);
169                 
170         spin_lock_irqsave(&eisa_irq_lock, flags);
171         if (irq & 8) {
172                 slave_mask &= ~(1 << (irq&7));
173                 eisa_out8(slave_mask, 0xa1);
174         } else {
175                 master_mask &= ~(1 << (irq&7));
176                 eisa_out8(master_mask, 0x21);
177         }
178         spin_unlock_irqrestore(&eisa_irq_lock, flags);
179         EISA_DBG("pic0 mask %02x\n", eisa_in8(0x21));
180         EISA_DBG("pic1 mask %02x\n", eisa_in8(0xa1));
181 }
182
183 static void eisa_mask_irq(void *irq_dev, int irq)
184 {
185         unsigned long flags;
186         EISA_DBG("mask irq %d\n", irq);
187         
188         /* mask irq */
189         spin_lock_irqsave(&eisa_irq_lock, flags);
190         if (irq & 8) {
191                 slave_mask |= (1 << (irq&7));
192                 eisa_out8(slave_mask, 0xa1);
193         } else {
194                 master_mask |= (1 << (irq&7));
195                 eisa_out8(master_mask, 0x21);
196         }
197         spin_unlock_irqrestore(&eisa_irq_lock, flags);
198 }
199
200 static void eisa_unmask_irq(void *irq_dev, int irq)
201 {
202         unsigned long flags;
203         EISA_DBG("unmask irq %d\n", irq);
204         
205         /* unmask */
206         spin_lock_irqsave(&eisa_irq_lock, flags);
207         if (irq & 8) {
208                 slave_mask &= ~(1 << (irq&7));
209                 eisa_out8(slave_mask, 0xa1);
210         } else {
211                 master_mask &= ~(1 << (irq&7));
212                 eisa_out8(master_mask, 0x21);
213         }
214         spin_unlock_irqrestore(&eisa_irq_lock, flags);
215 }
216
217 static struct irqaction action[IRQ_PER_REGION];
218
219 /* EISA needs to be fixed at IRQ region #0 (EISA_IRQ_REGION) */
220 static struct irq_region eisa_irq_region = {
221         .ops    = { eisa_disable_irq, eisa_enable_irq, eisa_mask_irq, eisa_unmask_irq },
222         .data   = { .name = "EISA", .irqbase = 0 },
223         .action = action,
224 };
225
226 static irqreturn_t eisa_irq(int _, void *intr_dev, struct pt_regs *regs)
227 {
228         extern void do_irq(struct irqaction *a, int i, struct pt_regs *p);
229         int irq = gsc_readb(0xfc01f000); /* EISA supports 16 irqs */
230         unsigned long flags;
231         
232         spin_lock_irqsave(&eisa_irq_lock, flags);
233         /* read IRR command */
234         eisa_out8(0x0a, 0x20);
235         eisa_out8(0x0a, 0xa0);
236
237         EISA_DBG("irq IAR %02x 8259-1 irr %02x 8259-2 irr %02x\n",
238                    irq, eisa_in8(0x20), eisa_in8(0xa0));
239    
240         /* read ISR command */
241         eisa_out8(0x0a, 0x20);
242         eisa_out8(0x0a, 0xa0);
243         EISA_DBG("irq 8259-1 isr %02x imr %02x 8259-2 isr %02x imr %02x\n",
244                  eisa_in8(0x20), eisa_in8(0x21), eisa_in8(0xa0), eisa_in8(0xa1));
245         
246         irq &= 0xf;
247         
248         /* mask irq and write eoi */
249         if (irq & 8) {
250                 slave_mask |= (1 << (irq&7));
251                 eisa_out8(slave_mask, 0xa1);
252                 eisa_out8(0x60 | (irq&7),0xa0);/* 'Specific EOI' to slave */
253                 eisa_out8(0x62,0x20);   /* 'Specific EOI' to master-IRQ2 */
254                 
255         } else {
256                 master_mask |= (1 << (irq&7));
257                 eisa_out8(master_mask, 0x21);
258                 eisa_out8(0x60|irq,0x20);       /* 'Specific EOI' to master */
259         }
260         spin_unlock_irqrestore(&eisa_irq_lock, flags);
261
262    
263         do_irq(&eisa_irq_region.action[irq], EISA_IRQ_REGION + irq, regs);
264    
265         spin_lock_irqsave(&eisa_irq_lock, flags);
266         /* unmask */
267         if (irq & 8) {
268                 slave_mask &= ~(1 << (irq&7));
269                 eisa_out8(slave_mask, 0xa1);
270         } else {
271                 master_mask &= ~(1 << (irq&7));
272                 eisa_out8(master_mask, 0x21);
273         }
274         spin_unlock_irqrestore(&eisa_irq_lock, flags);
275         return IRQ_HANDLED;
276 }
277
278 static irqreturn_t dummy_irq2_handler(int _, void *dev, struct pt_regs *regs)
279 {
280         printk(KERN_ALERT "eisa: uhh, irq2?\n");
281         return IRQ_HANDLED;
282 }
283
284 static void init_eisa_pic(void)
285 {
286         unsigned long flags;
287         
288         spin_lock_irqsave(&eisa_irq_lock, flags);
289
290         eisa_out8(0xff, 0x21); /* mask during init */
291         eisa_out8(0xff, 0xa1); /* mask during init */
292         
293         /* master pic */
294         eisa_out8(0x11,0x20); /* ICW1 */   
295         eisa_out8(0x00,0x21); /* ICW2 */   
296         eisa_out8(0x04,0x21); /* ICW3 */   
297         eisa_out8(0x01,0x21); /* ICW4 */   
298         eisa_out8(0x40,0x20); /* OCW2 */   
299         
300         /* slave pic */
301         eisa_out8(0x11,0xa0); /* ICW1 */   
302         eisa_out8(0x08,0xa1); /* ICW2 */   
303         eisa_out8(0x02,0xa1); /* ICW3 */   
304         eisa_out8(0x01,0xa1); /* ICW4 */   
305         eisa_out8(0x40,0xa0); /* OCW2 */   
306         
307         udelay(100);
308         
309         slave_mask = 0xff; 
310         master_mask = 0xfb; 
311         eisa_out8(slave_mask, 0xa1); /* OCW1 */
312         eisa_out8(master_mask, 0x21); /* OCW1 */
313         
314         /* setup trig level */
315         EISA_DBG("EISA edge/level %04x\n", eisa_irq_level);
316         
317         eisa_out8(eisa_irq_level&0xff, 0x4d0); /* Set all irq's to edge  */
318         eisa_out8((eisa_irq_level >> 8) & 0xff, 0x4d1); 
319         
320         EISA_DBG("pic0 mask %02x\n", eisa_in8(0x21));
321         EISA_DBG("pic1 mask %02x\n", eisa_in8(0xa1));
322         EISA_DBG("pic0 edge/level %02x\n", eisa_in8(0x4d0));
323         EISA_DBG("pic1 edge/level %02x\n", eisa_in8(0x4d1));
324         
325         spin_unlock_irqrestore(&eisa_irq_lock, flags);
326 }
327
328 /* Device initialisation */
329
330 #define is_mongoose(dev) (dev->id.sversion == 0x00076)
331
332 static int __devinit eisa_probe(struct parisc_device *dev)
333 {
334         int result;
335
336         char *name = is_mongoose(dev) ? "Mongoose" : "Wax";
337
338         printk(KERN_INFO "%s EISA Adapter found at 0x%08lx\n", 
339                 name, dev->hpa);
340
341         eisa_dev.hba.dev = dev;
342         eisa_dev.hba.iommu = ccio_get_iommu(dev);
343
344         eisa_dev.hba.lmmio_space.name = "EISA";
345         eisa_dev.hba.lmmio_space.start = F_EXTEND(0xfc000000);
346         eisa_dev.hba.lmmio_space.end = F_EXTEND(0xffbfffff);
347         eisa_dev.hba.lmmio_space.flags = IORESOURCE_MEM;
348         result = ccio_request_resource(dev, &eisa_dev.hba.lmmio_space);
349         if (result < 0) {
350                 printk(KERN_ERR "EISA: failed to claim EISA Bus address space!\n");
351                 return result;
352         }
353         eisa_dev.hba.io_space.name = "EISA";
354         eisa_dev.hba.io_space.start = 0;
355         eisa_dev.hba.io_space.end = 0xffff;
356         eisa_dev.hba.lmmio_space.flags = IORESOURCE_IO;
357         result = request_resource(&ioport_resource, &eisa_dev.hba.io_space);
358         if (result < 0) {
359                 printk(KERN_ERR "EISA: failed to claim EISA Bus port space!\n");
360                 return result;
361         }
362         pcibios_register_hba(&eisa_dev.hba);
363
364         result = request_irq(dev->irq, eisa_irq, SA_SHIRQ, "EISA", NULL);
365         if (result) {
366                 printk(KERN_ERR "EISA: request_irq failed!\n");
367                 return result;
368         }
369         
370         /* Reserve IRQ2 */
371         action[2].handler = dummy_irq2_handler;
372         action[2].name = "cascade";
373         
374         eisa_irq_region.data.dev = dev;
375         irq_region[0] = &eisa_irq_region;
376         
377         EISA_bus = 1;
378         if (dev->num_addrs) {
379                 /* newer firmware hand out the eeprom address */
380                 eisa_dev.eeprom_addr = dev->addr[0];
381         } else {
382                 /* old firmware, need to figure out the box */
383                 if (is_mongoose(dev)) {
384                         eisa_dev.eeprom_addr = SNAKES_EEPROM_BASE_ADDR;
385                 } else {
386                         eisa_dev.eeprom_addr = MIRAGE_EEPROM_BASE_ADDR;
387                 }
388         }
389         eisa_eeprom_init(eisa_dev.eeprom_addr);
390         result = eisa_enumerator(eisa_dev.eeprom_addr, &eisa_dev.hba.io_space, &eisa_dev.hba.lmmio_space);
391         init_eisa_pic();
392
393         if (result >= 0) {
394                 /* FIXME : Don't enumerate the bus twice. */
395                 eisa_dev.root.dev = &dev->dev;
396                 dev->dev.driver_data = &eisa_dev.root;
397                 eisa_dev.root.bus_base_addr = 0;
398                 eisa_dev.root.res = &eisa_dev.hba.io_space;
399                 eisa_dev.root.slots = result;
400                 eisa_dev.root.dma_mask = 0xffffffff; /* wild guess */
401                 if (eisa_root_register (&eisa_dev.root)) {
402                         printk(KERN_ERR "EISA: Failed to register EISA root\n");
403                         return -1;
404                 }
405         }
406         
407         return 0;
408 }
409
410 static struct parisc_device_id eisa_tbl[] = {
411         { HPHW_BA, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00076 }, /* Mongoose */
412         { HPHW_BA, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00090 }, /* Wax EISA */
413         { 0, }
414 };
415
416 MODULE_DEVICE_TABLE(parisc, eisa_tbl);
417
418 static struct parisc_driver eisa_driver = {
419         .name =         "EISA Bus Adapter",
420         .id_table =     eisa_tbl,
421         .probe =        eisa_probe,
422 };
423
424 void __init eisa_init(void)
425 {
426         register_parisc_driver(&eisa_driver);
427 }
428
429
430 static unsigned int eisa_irq_configured;
431 void eisa_make_irq_level(int num)
432 {
433         if (eisa_irq_configured& (1<<num)) {
434                 printk(KERN_WARNING
435                        "IRQ %d polarity configured twice (last to level)\n", 
436                        num);
437         }
438         eisa_irq_level |= (1<<num); /* set the corresponding bit */
439         eisa_irq_configured |= (1<<num); /* set the corresponding bit */
440 }
441
442 void eisa_make_irq_edge(int num)
443 {
444         if (eisa_irq_configured& (1<<num)) {
445                 printk(KERN_WARNING 
446                        "IRQ %d polarity configured twice (last to edge)\n",
447                        num);
448         }
449         eisa_irq_level &= ~(1<<num); /* clear the corresponding bit */
450         eisa_irq_configured |= (1<<num); /* set the corresponding bit */
451 }
452
453 static int __init eisa_irq_setup(char *str)
454 {
455         char *cur = str;
456         int val;
457
458         EISA_DBG("IRQ setup\n");
459         while (cur != NULL) {
460                 char *pe;
461                 
462                 val = (int) simple_strtoul(cur, &pe, 0);
463                 if (val > 15 || val < 0) {
464                         printk(KERN_ERR "eisa: EISA irq value are 0-15\n");
465                         continue;
466                 }
467                 if (val == 2) { 
468                         val = 9;
469                 }
470                 eisa_make_irq_edge(val); /* clear the corresponding bit */
471                 EISA_DBG("setting IRQ %d to edge-triggered mode\n", val);
472                 
473                 if ((cur = strchr(cur, ','))) {
474                         cur++;
475                 } else {
476                         break;
477                 }
478         }
479         return 1;
480 }
481
482 __setup("eisa_irq_edge=", eisa_irq_setup);
483