VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / parisc / superio.c
1 /*      National Semiconductor NS87560UBD Super I/O controller used in
2  *      HP [BCJ]x000 workstations.
3  *
4  *      This chip is a horrid piece of engineering, and National
5  *      denies any knowledge of its existence. Thus no datasheet is
6  *      available off www.national.com. 
7  *
8  *      (C) Copyright 2000 Linuxcare, Inc.
9  *      (C) Copyright 2000 Linuxcare Canada, Inc.
10  *      (C) Copyright 2000 Martin K. Petersen <mkp@linuxcare.com>
11  *      (C) Copyright 2000 Alex deVries <alex@linuxcare.com>
12  *      (C) Copyright 2001 John Marvin <jsm fc hp com>
13  *      (C) Copyright 2003 Grant Grundler <grundler parisc-linux org>
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License as
17  *      published by the Free Software Foundation; either version 2 of
18  *      the License, or (at your option) any later version.  
19  *
20  *      The initial version of this is by Martin Peterson.  Alex deVries
21  *      has spent a bit of time trying to coax it into working.
22  *
23  *      Major changes to get basic interrupt infrastructure working to
24  *      hopefully be able to support all SuperIO devices. Currently
25  *      works with serial. -- John Marvin <jsm@fc.hp.com>
26  */
27
28
29 /* NOTES:
30  * 
31  * Function 0 is an IDE controller. It is identical to a PC87415 IDE
32  * controller (and identifies itself as such).
33  *
34  * Function 1 is a "Legacy I/O" controller. Under this function is a
35  * whole mess of legacy I/O peripherals. Of course, HP hasn't enabled
36  * all the functionality in hardware, but the following is available:
37  *
38  *      Two 16550A compatible serial controllers
39  *      An IEEE 1284 compatible parallel port
40  *      A floppy disk controller
41  *
42  * Function 2 is a USB controller.
43  *
44  * We must be incredibly careful during initialization.  Since all
45  * interrupts are routed through function 1 (which is not allowed by
46  * the PCI spec), we need to program the PICs on the legacy I/O port
47  * *before* we attempt to set up IDE and USB.  @#$!&
48  *
49  * According to HP, devices are only enabled by firmware if they have
50  * a physical device connected.
51  *
52  * Configuration register bits:
53  *     0x5A: FDC, SP1, IDE1, SP2, IDE2, PAR, Reserved, P92
54  *     0x5B: RTC, 8259, 8254, DMA1, DMA2, KBC, P61, APM
55  *
56  */
57
58 #include <linux/errno.h>
59 #include <linux/init.h>
60 #include <linux/module.h>
61 #include <linux/types.h>
62 #include <linux/interrupt.h>
63 #include <linux/ioport.h>
64 #include <linux/serial.h>
65 #include <linux/pci.h>
66 #include <linux/parport.h>
67 #include <linux/parport_pc.h>
68 #include <linux/termios.h>
69 #include <linux/tty.h>
70 #include <linux/serial_core.h>
71 #include <linux/delay.h>
72 #include <linux/ide.h>
73
74 #include <asm/io.h>
75 #include <asm/hardware.h>
76 #include <asm/irq.h>
77 #include <asm/superio.h>
78
79 #define SUPERIO_IDE_MAX_RETRIES 25
80
81 static struct superio_device sio_dev;
82
83
84 #undef DEBUG_SUPERIO_INIT
85
86 #ifdef DEBUG_SUPERIO_INIT
87 #define DBG_INIT(x...)  printk(x)
88 #else
89 #define DBG_INIT(x...)
90 #endif
91
92 static irqreturn_t
93 superio_interrupt(int irq, void *devp, struct pt_regs *regs)
94 {
95         struct superio_device *sio = (struct superio_device *)devp;
96         u8 results;
97         u8 local_irq;
98
99         /* Poll the 8259 to see if there's an interrupt. */
100         outb (OCW3_POLL,IC_PIC1+0);
101
102         results = inb(IC_PIC1+0);
103
104         if ((results & 0x80) == 0) {
105 #ifndef CONFIG_SMP
106                 /* HACK: need to investigate why this happens if SMP enabled */
107                 BUG(); /* This shouldn't happen */
108 #endif
109                 return IRQ_HANDLED;
110         }
111
112         /* Check to see which device is interrupting */
113
114         local_irq = results & 0x0f;
115
116         if (local_irq == 2 || local_irq > 7) {
117                 printk(KERN_ERR "SuperIO: slave interrupted!\n");
118                 BUG();
119                 return IRQ_HANDLED;
120         }
121
122         if (local_irq == 7) {
123
124                 /* Could be spurious. Check in service bits */
125
126                 outb(OCW3_ISR,IC_PIC1+0);
127                 results = inb(IC_PIC1+0);
128                 if ((results & 0x80) == 0) { /* if ISR7 not set: spurious */
129                         printk(KERN_WARNING "SuperIO: spurious interrupt!\n");
130                         return IRQ_HANDLED;
131                 }
132         }
133
134         /* Call the appropriate device's interrupt */
135         do_irq(&sio->irq_region->action[local_irq],
136                 sio->irq_region->data.irqbase + local_irq,
137                 regs);
138
139         /* set EOI */
140
141         outb((OCW2_SEOI|local_irq),IC_PIC1 + 0);
142         return IRQ_HANDLED;
143 }
144
145 /* Initialize Super I/O device */
146
147 static void __devinit
148 superio_init(struct superio_device *sio)
149 {
150         struct pci_dev *pdev = sio->lio_pdev;
151         u16 word;
152
153         if (sio->suckyio_irq_enabled)                                       
154                 return;
155
156         if (!pdev) BUG();
157         if (!sio->usb_pdev) BUG();
158
159         /* use the IRQ iosapic found for USB INT D... */
160         pdev->irq = sio->usb_pdev->irq;
161
162         /* ...then properly fixup the USB to point at suckyio PIC */
163         sio->usb_pdev->irq = superio_fixup_irq(sio->usb_pdev);
164
165         printk (KERN_INFO "SuperIO: Found NS87560 Legacy I/O device at %s (IRQ %i) \n",
166                 pci_name(pdev),pdev->irq);
167
168         pci_read_config_dword (pdev, SIO_SP1BAR, &sio->sp1_base);
169         sio->sp1_base &= ~1;
170         printk (KERN_INFO "SuperIO: Serial port 1 at 0x%x\n", sio->sp1_base);
171
172         pci_read_config_dword (pdev, SIO_SP2BAR, &sio->sp2_base);
173         sio->sp2_base &= ~1;
174         printk (KERN_INFO "SuperIO: Serial port 2 at 0x%x\n", sio->sp2_base);
175
176         pci_read_config_dword (pdev, SIO_PPBAR, &sio->pp_base);
177         sio->pp_base &= ~1;
178         printk (KERN_INFO "SuperIO: Parallel port at 0x%x\n", sio->pp_base);
179
180         pci_read_config_dword (pdev, SIO_FDCBAR, &sio->fdc_base);
181         sio->fdc_base &= ~1;
182         printk (KERN_INFO "SuperIO: Floppy controller at 0x%x\n", sio->fdc_base);
183         pci_read_config_dword (pdev, SIO_ACPIBAR, &sio->acpi_base);
184         sio->acpi_base &= ~1;
185         printk (KERN_INFO "SuperIO: ACPI at 0x%x\n", sio->acpi_base);
186
187         request_region (IC_PIC1, 0x1f, "pic1");
188         request_region (IC_PIC2, 0x1f, "pic2");
189         request_region (sio->acpi_base, 0x1f, "acpi");
190
191         /* Enable the legacy I/O function */
192         pci_read_config_word (pdev, PCI_COMMAND, &word);
193         word |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_IO;
194         pci_write_config_word (pdev, PCI_COMMAND, word);
195
196         pci_set_master (pdev);
197         pci_enable_device(pdev);
198
199         /*
200          * Next project is programming the onboard interrupt controllers.
201          * PDC hasn't done this for us, since it's using polled I/O.
202          *
203          * XXX Use dword writes to avoid bugs in Elroy or Suckyio Config
204          *     space access.  PCI is by nature a 32-bit bus and config
205          *     space can be sensitive to that.
206          */
207
208         /* 0x64 - 0x67 :
209                 DMA Rtg 2
210                 DMA Rtg 3
211                 DMA Chan Ctl
212                 TRIGGER_1    == 0x82   USB & IDE level triggered, rest to edge
213         */
214         pci_write_config_dword (pdev, 0x64,         0x82000000U);
215
216         /* 0x68 - 0x6b :
217                 TRIGGER_2    == 0x00   all edge triggered (not used)
218                 CFG_IR_SER   == 0x43   SerPort1 = IRQ3, SerPort2 = IRQ4
219                 CFG_IR_PF    == 0x65   ParPort  = IRQ5, FloppyCtlr = IRQ6
220                 CFG_IR_IDE   == 0x07   IDE1 = IRQ7, reserved
221         */
222         pci_write_config_dword (pdev, TRIGGER_2,    0x07654300U);
223
224         /* 0x6c - 0x6f :
225                 CFG_IR_INTAB == 0x00
226                 CFG_IR_INTCD == 0x10   USB = IRQ1
227                 CFG_IR_PS2   == 0x00
228                 CFG_IR_FXBUS == 0x00
229         */
230         pci_write_config_dword (pdev, CFG_IR_INTAB, 0x00001000U);
231
232         /* 0x70 - 0x73 :
233                 CFG_IR_USB   == 0x00  not used. USB is connected to INTD.
234                 CFG_IR_ACPI  == 0x00  not used.
235                 DMA Priority == 0x4c88  Power on default value. NFC.
236         */
237         pci_write_config_dword (pdev, CFG_IR_USB, 0x4c880000U);
238
239         /* PIC1 Initialization Command Word register programming */
240         outb (0x11,IC_PIC1+0);  /* ICW1: ICW4 write req | ICW1 */
241         outb (0x00,IC_PIC1+1);  /* ICW2: interrupt vector table - not used */
242         outb (0x04,IC_PIC1+1);  /* ICW3: Cascade */
243         outb (0x01,IC_PIC1+1);  /* ICW4: x86 mode */
244
245         /* PIC1 Program Operational Control Words */
246         outb (0xff,IC_PIC1+1);  /* OCW1: Mask all interrupts */
247         outb (0xc2,IC_PIC1+0);  /* OCW2: priority (3-7,0-2) */
248
249         /* PIC2 Initialization Command Word register programming */
250         outb (0x11,IC_PIC2+0);  /* ICW1: ICW4 write req | ICW1 */
251         outb (0x00,IC_PIC2+1);  /* ICW2: N/A */
252         outb (0x02,IC_PIC2+1);  /* ICW3: Slave ID code */
253         outb (0x01,IC_PIC2+1);  /* ICW4: x86 mode */
254                 
255         /* Program Operational Control Words */
256         outb (0xff,IC_PIC1+1);  /* OCW1: Mask all interrupts */
257         outb (0x68,IC_PIC1+0);  /* OCW3: OCW3 select | ESMM | SMM */
258
259         /* Write master mask reg */
260         outb (0xff,IC_PIC1+1);
261
262         /* Setup USB power regulation */
263         outb(1, sio->acpi_base + USB_REG_CR);
264         if (inb(sio->acpi_base + USB_REG_CR) & 1)
265                 printk(KERN_INFO "SuperIO: USB regulator enabled\n");
266         else
267                 printk(KERN_ERR "USB regulator not initialized!\n");
268
269         if (request_irq(pdev->irq, superio_interrupt, SA_INTERRUPT,
270                         "SuperIO", (void *)sio)) {
271
272                 printk(KERN_ERR "SuperIO: could not get irq\n");
273                 BUG();
274                 return;
275         }
276
277         sio->suckyio_irq_enabled = 1;
278 }
279
280
281 static void
282 superio_disable_irq(void *dev, int local_irq)
283 {
284         u8 r8;
285
286         if ((local_irq < 1) || (local_irq == 2) || (local_irq > 7)) {
287             printk(KERN_ERR "SuperIO: Illegal irq number.\n");
288             BUG();
289             return;
290         }
291
292         /* Mask interrupt */
293
294         r8 = inb(IC_PIC1+1);
295         r8 |= (1 << local_irq);
296         outb (r8,IC_PIC1+1);
297 }
298
299 static void
300 superio_enable_irq(void *dev, int local_irq)
301 {
302         u8 r8;
303
304         if ((local_irq < 1) || (local_irq == 2) || (local_irq > 7)) {
305             printk(KERN_ERR "SuperIO: Illegal irq number (%d).\n", local_irq);
306             BUG();
307             return;
308         }
309
310         /* Unmask interrupt */
311         r8 = inb(IC_PIC1+1);
312         r8 &= ~(1 << local_irq);
313         outb (r8,IC_PIC1+1);
314 }
315
316
317 static void
318 superio_mask_irq(void *dev, int local_irq)
319 {
320         BUG();
321 }
322
323 static void
324 superio_unmask_irq(void *dev, int local_irq)
325 {
326         BUG();
327 }
328
329 static struct irq_region_ops superio_irq_ops = {
330         .disable_irq =  superio_disable_irq,
331         .enable_irq =   superio_enable_irq,
332         .mask_irq =     superio_mask_irq,
333         .unmask_irq =   superio_unmask_irq
334 };
335
336 #ifdef DEBUG_SUPERIO_INIT
337 static unsigned short expected_device[3] = {
338         PCI_DEVICE_ID_NS_87415,
339         PCI_DEVICE_ID_NS_87560_LIO,
340         PCI_DEVICE_ID_NS_87560_USB
341 };
342 #endif
343
344 int superio_fixup_irq(struct pci_dev *pcidev)
345 {
346         int local_irq;
347
348 #ifdef DEBUG_SUPERIO_INIT
349         int fn;
350         fn = PCI_FUNC(pcidev->devfn);
351
352         /* Verify the function number matches the expected device id. */
353         if (expected_device[fn] != pcidev->device) {
354                 BUG();
355                 return -1;
356         }
357         printk("superio_fixup_irq(%s) ven 0x%x dev 0x%x from %p\n",
358                 pci_name(pcidev),
359                 pcidev->vendor, pcidev->device,
360                 __builtin_return_address(0));
361 #endif
362
363         if (!sio_dev.irq_region) {
364                 /* Allocate an irq region for SuperIO devices */
365                 sio_dev.irq_region = alloc_irq_region(SUPERIO_NIRQS,
366                                                 &superio_irq_ops,
367                                                 "SuperIO", (void *) &sio_dev);
368                 if (!sio_dev.irq_region) {
369                         printk(KERN_WARNING "SuperIO: alloc_irq_region failed\n");
370                         return -1;
371                 }
372         }
373
374         /*
375          * We don't allocate a SuperIO irq for the legacy IO function,
376          * since it is a "bridge". Instead, we will allocate irq's for
377          * each legacy device as they are initialized.
378          */
379
380         switch(pcidev->device) {
381         case PCI_DEVICE_ID_NS_87415:            /* Function 0 */
382                 local_irq = IDE_IRQ;
383                 break;
384         case PCI_DEVICE_ID_NS_87560_LIO:        /* Function 1 */
385                 sio_dev.lio_pdev = pcidev;      /* save for superio_init() */
386                 return -1;
387         case PCI_DEVICE_ID_NS_87560_USB:        /* Function 2 */
388                 sio_dev.usb_pdev = pcidev;      /* save for superio_init() */
389                 local_irq = USB_IRQ;
390                 break;
391         default:
392                 local_irq = -1;
393                 BUG();
394                 break;
395         }
396
397         return(sio_dev.irq_region->data.irqbase + local_irq);
398 }
399
400 static struct uart_port serial[] = {
401         {
402                 .iotype         = UPIO_PORT,
403                 .line           = 0,
404                 .type           = PORT_16550A,
405                 .uartclk        = 115200*16,
406                 .fifosize       = 16,
407         },
408         {
409                 .iotype         = UPIO_PORT,
410                 .line           = 1,
411                 .type           = PORT_16550A,
412                 .uartclk        = 115200*16,
413                 .fifosize       = 16,
414         }
415 };
416
417 void __devinit
418 superio_serial_init(void)
419 {
420 #ifdef CONFIG_SERIAL_8250
421         int retval;
422 #ifdef CONFIG_SERIAL_8250_CONSOLE
423         extern void serial8250_console_init(void); /* drivers/serial/8250.c */
424 #endif        
425         
426         if (!sio_dev.irq_region)
427                 return; /* superio not present */
428
429         if (!serial) {
430                 printk(KERN_WARNING "SuperIO: Could not get memory for serial struct.\n");
431                 return;
432         }
433
434         serial[0].iobase = sio_dev.sp1_base;
435         serial[0].irq = sio_dev.irq_region->data.irqbase + SP1_IRQ;
436
437         retval = early_serial_setup(&serial[0]);
438         if (retval < 0) {
439                 printk(KERN_WARNING "SuperIO: Register Serial #0 failed.\n");
440                 return;
441         }
442
443 #ifdef CONFIG_SERIAL_8250_CONSOLE
444         serial8250_console_init();
445 #endif
446         
447         serial[1].iobase = sio_dev.sp2_base;
448         serial[1].irq = sio_dev.irq_region->data.irqbase + SP2_IRQ;
449         retval = early_serial_setup(&serial[1]);
450
451         if (retval < 0)
452                 printk(KERN_WARNING "SuperIO: Register Serial #1 failed.\n");
453 #endif /* CONFIG_SERIAL_8250 */
454 }
455
456
457 static void __devinit
458 superio_parport_init(void)
459 {
460 #ifdef CONFIG_PARPORT_PC
461         if (!parport_pc_probe_port(sio_dev.pp_base,
462                         0 /*base_hi*/,
463                         sio_dev.irq_region->data.irqbase + PAR_IRQ, 
464                         PARPORT_DMA_NONE /* dma */,
465                         NULL /*struct pci_dev* */) )
466
467                 printk(KERN_WARNING "SuperIO: Probing parallel port failed.\n");
468 #endif  /* CONFIG_PARPORT_PC */
469 }
470
471
472 static u8 superio_ide_inb (unsigned long port);
473 static unsigned long superio_ide_status[2];
474 static unsigned long superio_ide_select[2];
475 static unsigned long superio_ide_dma_status[2];
476
477 void superio_fixup_pci(struct pci_dev *pdev)
478 {
479         u8 prog;
480
481         pdev->class |= 0x5;
482         pci_write_config_byte(pdev, PCI_CLASS_PROG, pdev->class);
483
484         pci_read_config_byte(pdev, PCI_CLASS_PROG, &prog);
485         printk("PCI: Enabled native mode for NS87415 (pif=0x%x)\n", prog);
486 }
487
488 /* Because of a defect in Super I/O, all reads of the PCI DMA status 
489  * registers, IDE status register and the IDE select register need to be 
490  * retried
491  */
492 static u8 superio_ide_inb (unsigned long port)
493 {
494         if (port == superio_ide_status[0] ||
495             port == superio_ide_status[1] ||
496             port == superio_ide_select[0] ||
497             port == superio_ide_select[1] ||
498             port == superio_ide_dma_status[0] ||
499             port == superio_ide_dma_status[1]) {
500                 u8 tmp;
501                 int retries = SUPERIO_IDE_MAX_RETRIES;
502
503                 /* printk(" [ reading port 0x%x with retry ] ", port); */
504
505                 do {
506                         tmp = inb(port);
507                         if (tmp == 0)
508                                 udelay(50);
509                 } while (tmp == 0 && retries-- > 0);
510
511                 return tmp;
512         }
513
514         return inb(port);
515 }
516
517 void __init superio_ide_init_iops (struct hwif_s *hwif)
518 {
519         u32 base, dmabase;
520         u8 tmp;
521         struct pci_dev *pdev = hwif->pci_dev;
522         u8 port = hwif->channel;
523
524         base = pci_resource_start(pdev, port * 2) & ~3;
525         dmabase = pci_resource_start(pdev, 4) & ~3;
526
527         superio_ide_status[port] = base + IDE_STATUS_OFFSET;
528         superio_ide_select[port] = base + IDE_SELECT_OFFSET;
529         superio_ide_dma_status[port] = dmabase + (!port ? 2 : 0xa);
530         
531         /* Clear error/interrupt, enable dma */
532         tmp = superio_ide_inb(superio_ide_dma_status[port]);
533         outb(tmp | 0x66, superio_ide_dma_status[port]);
534
535         /* We need to override inb to workaround a SuperIO errata */
536         hwif->INB = superio_ide_inb;
537 }
538
539 static int __devinit superio_probe(struct pci_dev *dev, const struct pci_device_id *id)
540 {
541
542         /*
543         ** superio_probe(00:0e.0) ven 0x100b dev 0x2 sv 0x0 sd 0x0 class 0x1018a
544         ** superio_probe(00:0e.1) ven 0x100b dev 0xe sv 0x0 sd 0x0 class 0x68000
545         ** superio_probe(00:0e.2) ven 0x100b dev 0x12 sv 0x0 sd 0x0 class 0xc0310
546         */
547         DBG_INIT("superio_probe(%s) ven 0x%x dev 0x%x sv 0x%x sd 0x%x class 0x%x\n",
548                 pci_name(dev),
549                 dev->vendor, dev->device,
550                 dev->subsystem_vendor, dev->subsystem_device,
551                 dev->class);
552
553         superio_init(&sio_dev);
554
555         if (dev->device == PCI_DEVICE_ID_NS_87560_LIO) {        /* Function 1 */
556                 superio_parport_init();
557                 superio_serial_init();
558                 /* REVISIT XXX : superio_fdc_init() ? */
559                 return 0;
560         } else if (dev->device == PCI_DEVICE_ID_NS_87415) {     /* Function 0 */
561                 DBG_INIT("superio_probe: ignoring IDE 87415\n");
562         } else if (dev->device == PCI_DEVICE_ID_NS_87560_USB) { /* Function 2 */
563                 DBG_INIT("superio_probe: ignoring USB OHCI controller\n");
564         } else {
565                 DBG_INIT("superio_probe: WTF? Fire Extinguisher?\n");
566         }
567
568         /* Let appropriate other driver claim this device. */ 
569         return -ENODEV;
570 }
571
572 static struct pci_device_id superio_tbl[] = {
573         { PCI_VENDOR_ID_NS, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
574         { 0, }
575 };
576
577 static struct pci_driver superio_driver = {
578         .name =         "SuperIO",
579         .id_table =     superio_tbl,
580         .probe =        superio_probe,
581 };
582
583 static int __init superio_modinit(void)
584 {
585         return pci_module_init(&superio_driver);
586 }
587
588 static void __exit superio_exit(void)
589 {
590         pci_unregister_driver(&superio_driver);
591 }
592
593
594 module_init(superio_modinit);
595 module_exit(superio_exit);