patch-2_6_7-vs1_9_1_12
[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         extern void serial8250_console_init(void); /* drivers/serial/8250.c */
423         
424         if (!sio_dev.irq_region)
425                 return; /* superio not present */
426
427         if (!serial) {
428                 printk(KERN_WARNING "SuperIO: Could not get memory for serial struct.\n");
429                 return;
430         }
431
432         serial[0].iobase = sio_dev.sp1_base;
433         serial[0].irq = sio_dev.irq_region->data.irqbase + SP1_IRQ;
434
435         retval = early_serial_setup(&serial[0]);
436         if (retval < 0) {
437                 printk(KERN_WARNING "SuperIO: Register Serial #0 failed.\n");
438                 return;
439         }
440
441         serial8250_console_init();
442
443         serial[1].iobase = sio_dev.sp2_base;
444         serial[1].irq = sio_dev.irq_region->data.irqbase + SP2_IRQ;
445         retval = early_serial_setup(&serial[1]);
446
447         if (retval < 0)
448                 printk(KERN_WARNING "SuperIO: Register Serial #1 failed.\n");
449 #endif /* CONFIG_SERIAL_8250 */
450 }
451
452
453 static void __devinit
454 superio_parport_init(void)
455 {
456 #ifdef CONFIG_PARPORT_PC
457         if (!parport_pc_probe_port(sio_dev.pp_base,
458                         0 /*base_hi*/,
459                         sio_dev.irq_region->data.irqbase + PAR_IRQ, 
460                         PARPORT_DMA_NONE /* dma */,
461                         NULL /*struct pci_dev* */) )
462
463                 printk(KERN_WARNING "SuperIO: Probing parallel port failed.\n");
464 #endif  /* CONFIG_PARPORT_PC */
465 }
466
467
468 static u8 superio_ide_inb (unsigned long port);
469 static unsigned long superio_ide_status[2];
470 static unsigned long superio_ide_select[2];
471 static unsigned long superio_ide_dma_status[2];
472
473 void superio_fixup_pci(struct pci_dev *pdev)
474 {
475         u8 prog;
476
477         pdev->class |= 0x5;
478         pci_write_config_byte(pdev, PCI_CLASS_PROG, pdev->class);
479
480         pci_read_config_byte(pdev, PCI_CLASS_PROG, &prog);
481         printk("PCI: Enabled native mode for NS87415 (pif=0x%x)\n", prog);
482 }
483
484 /* Because of a defect in Super I/O, all reads of the PCI DMA status 
485  * registers, IDE status register and the IDE select register need to be 
486  * retried
487  */
488 static u8 superio_ide_inb (unsigned long port)
489 {
490         if (port == superio_ide_status[0] ||
491             port == superio_ide_status[1] ||
492             port == superio_ide_select[0] ||
493             port == superio_ide_select[1] ||
494             port == superio_ide_dma_status[0] ||
495             port == superio_ide_dma_status[1]) {
496                 u8 tmp;
497                 int retries = SUPERIO_IDE_MAX_RETRIES;
498
499                 /* printk(" [ reading port 0x%x with retry ] ", port); */
500
501                 do {
502                         tmp = inb(port);
503                         if (tmp == 0)
504                                 udelay(50);
505                 } while (tmp == 0 && retries-- > 0);
506
507                 return tmp;
508         }
509
510         return inb(port);
511 }
512
513 void __init superio_ide_init_iops (struct hwif_s *hwif)
514 {
515         u32 base, dmabase;
516         u8 tmp;
517         struct pci_dev *pdev = hwif->pci_dev;
518         u8 port = hwif->channel;
519
520         base = pci_resource_start(pdev, port * 2) & ~3;
521         dmabase = pci_resource_start(pdev, 4) & ~3;
522
523         superio_ide_status[port] = base + IDE_STATUS_OFFSET;
524         superio_ide_select[port] = base + IDE_SELECT_OFFSET;
525         superio_ide_dma_status[port] = dmabase + (!port ? 2 : 0xa);
526         
527         /* Clear error/interrupt, enable dma */
528         tmp = superio_ide_inb(superio_ide_dma_status[port]);
529         outb(tmp | 0x66, superio_ide_dma_status[port]);
530
531         /* We need to override inb to workaround a SuperIO errata */
532         hwif->INB = superio_ide_inb;
533 }
534
535 static int __devinit superio_probe(struct pci_dev *dev, const struct pci_device_id *id)
536 {
537
538         /*
539         ** superio_probe(00:0e.0) ven 0x100b dev 0x2 sv 0x0 sd 0x0 class 0x1018a
540         ** superio_probe(00:0e.1) ven 0x100b dev 0xe sv 0x0 sd 0x0 class 0x68000
541         ** superio_probe(00:0e.2) ven 0x100b dev 0x12 sv 0x0 sd 0x0 class 0xc0310
542         */
543         DBG_INIT("superio_probe(%s) ven 0x%x dev 0x%x sv 0x%x sd 0x%x class 0x%x\n",
544                 pci_name(dev),
545                 dev->vendor, dev->device,
546                 dev->subsystem_vendor, dev->subsystem_device,
547                 dev->class);
548
549         superio_init(&sio_dev);
550
551         if (dev->device == PCI_DEVICE_ID_NS_87560_LIO) {        /* Function 1 */
552                 superio_parport_init();
553                 superio_serial_init();
554                 /* REVISIT XXX : superio_fdc_init() ? */
555                 return 0;
556         } else if (dev->device == PCI_DEVICE_ID_NS_87415) {     /* Function 0 */
557                 DBG_INIT("superio_probe: ignoring IDE 87415\n");
558         } else if (dev->device == PCI_DEVICE_ID_NS_87560_USB) { /* Function 2 */
559                 DBG_INIT("superio_probe: ignoring USB OHCI controller\n");
560         } else {
561                 DBG_INIT("superio_probe: WTF? Fire Extinguisher?\n");
562         }
563
564         /* Let appropriate other driver claim this device. */ 
565         return -ENODEV;
566 }
567
568 static struct pci_device_id superio_tbl[] = {
569         { PCI_VENDOR_ID_NS, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
570         { 0, }
571 };
572
573 static struct pci_driver superio_driver = {
574         .name =         "SuperIO",
575         .id_table =     superio_tbl,
576         .probe =        superio_probe,
577 };
578
579 static int __init superio_modinit(void)
580 {
581         return pci_module_init(&superio_driver);
582 }
583
584 static void __exit superio_exit(void)
585 {
586         pci_unregister_driver(&superio_driver);
587 }
588
589
590 module_init(superio_modinit);
591 module_exit(superio_exit);