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  *
14  *	This program is free software; you can redistribute it and/or
15  *	modify it under the terms of the GNU General Public License as
16  *	published by the Free Software Foundation; either version 2 of
17  *	the License, or (at your option) any later version.
18  *
19  *	The initial version of this is by Martin Peterson.  Alex deVries
20  *	has spent a bit of time trying to coax it into working.
21  *
22  *      Major changes to get basic interrupt infrastructure working to
23  *      hopefully be able to support all SuperIO devices. Currently
24  *      works with serial. -- John Marvin <jsm@fc.hp.com>
25  */
26 
27 
28 /* NOTES:
29  *
30  * Function 0 is an IDE controller. It is identical to a PC87415 IDE
31  * controller (and identifies itself as such).
32  *
33  * Function 1 is a "Legacy I/O" controller. Under this function is a
34  * whole mess of legacy I/O peripherals. Of course, HP hasn't enabled
35  * all the functionality in hardware, but the following is available:
36  *
37  *      Two 16550A compatible serial controllers
38  *      An IEEE 1284 compatible parallel port
39  *      A floppy disk controller
40  *
41  * Function 2 is a USB controller.
42  *
43  * We must be incredibly careful during initialization.  Since all
44  * interrupts are routed through function 1 (which is not allowed by
45  * the PCI spec), we need to program the PICs on the legacy I/O port
46  * *before* we attempt to set up IDE and USB.  @#$!&
47  *
48  * According to HP, devices are only enabled by firmware if they have
49  * a physical device connected.
50  *
51  * Configuration register bits:
52  *     0x5A: FDC, SP1, IDE1, SP2, IDE2, PAR, Reserved, P92
53  *     0x5B: RTC, 8259, 8254, DMA1, DMA2, KBC, P61, APM
54  *
55  */
56 
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/module.h>
60 #include <linux/types.h>
61 #include <linux/interrupt.h>
62 #include <linux/ioport.h>
63 #include <linux/serial.h>
64 #include <linux/pci.h>
65 #include <linux/ioport.h>
66 #include <linux/parport.h>
67 #include <linux/parport_pc.h>
68 #include <linux/serial_reg.h>
69 #include <asm/io.h>
70 #include <asm/hardware.h>
71 #include <asm/gsc.h>
72 #include <asm/irq.h>
73 #include <asm/superio.h>
74 
75 static struct superio_device sio_dev = {
76 	iosapic_irq: -1
77 };
78 
79 
80 #undef DEBUG_INIT
81 
82 void
superio_inform_irq(int irq)83 superio_inform_irq(int irq)
84 {
85     if (sio_dev.iosapic_irq != -1) {
86 	printk(KERN_ERR "SuperIO: superio_inform_irq called twice! (more than one SuperIO?)\n");
87 	BUG();
88 	return;
89     }
90 
91     sio_dev.iosapic_irq = irq;
92 }
93 
94 static void
superio_interrupt(int irq,void * devp,struct pt_regs * regs)95 superio_interrupt(int irq, void *devp, struct pt_regs *regs)
96 {
97 	struct superio_device *sio = (struct superio_device *)devp;
98 	u8 results;
99 	u8 local_irq;
100 
101 	/* Poll the 8259 to see if there's an interrupt. */
102 	outb (OCW3_POLL,IC_PIC1+0);
103 
104 	results = inb(IC_PIC1+0);
105 
106 	if ((results & 0x80) == 0) {
107 #ifndef CONFIG_SMP
108 		/* HACK: need to investigate why this happens if SMP enabled */
109 		BUG(); /* This shouldn't happen */
110 #endif
111 		return;
112 	}
113 
114 	/* Check to see which device is interrupting */
115 
116 	local_irq = results & 0x0f;
117 
118 	if (local_irq == 2 || local_irq > 7) {
119 		printk(KERN_ERR "SuperIO: slave interrupted!\n");
120 		BUG();
121 		return;
122 	}
123 
124 	if (local_irq == 7) {
125 
126 		/* Could be spurious. Check in service bits */
127 
128 		outb(OCW3_ISR,IC_PIC1+0);
129 		results = inb(IC_PIC1+0);
130 		if ((results & 0x80) == 0) { /* if ISR7 not set: spurious */
131 			printk(KERN_WARNING "SuperIO: spurious interrupt!\n");
132 			return;
133 		}
134 	}
135 
136 	/* Call the appropriate device's interrupt */
137 
138 	do_irq(&sio->irq_region->action[local_irq],
139 		sio->irq_region->data.irqbase + local_irq,
140 		regs);
141 
142 	/* set EOI */
143 
144 	outb((OCW2_SEOI|local_irq),IC_PIC1 + 0);
145 	return;
146 }
147 
148 /* Initialize Super I/O device */
149 
150 static void __devinit
superio_init(struct superio_device * sio)151 superio_init(struct superio_device *sio)
152 {
153 	struct pci_dev *pdev = sio->lio_pdev;
154 	u16 word;
155 	u8  i;
156 
157 	if (!pdev || sio->iosapic_irq == -1) {
158 		printk(KERN_ERR "All SuperIO functions not found!\n");
159 		BUG();
160 		return;
161 	}
162 
163 	printk (KERN_INFO "SuperIO: Found NS87560 Legacy I/O device at %s (IRQ %i) \n",
164 		pdev->slot_name,sio->iosapic_irq);
165 
166 	/* Find our I/O devices */
167 	pci_read_config_word (pdev, SIO_SP1BAR, &sio->sp1_base);
168 	sio->sp1_base &= ~1;
169 	printk (KERN_INFO "SuperIO: Serial port 1 at 0x%x\n", sio->sp1_base);
170 
171 	pci_read_config_word (pdev, SIO_SP2BAR, &sio->sp2_base);
172 	sio->sp2_base &= ~1;
173 	printk (KERN_INFO "SuperIO: Serial port 2 at 0x%x\n", sio->sp2_base);
174 
175 	pci_read_config_word (pdev, SIO_PPBAR, &sio->pp_base);
176 	sio->pp_base &= ~1;
177 	printk (KERN_INFO "SuperIO: Parallel port at 0x%x\n", sio->pp_base);
178 
179 	pci_read_config_word (pdev, SIO_FDCBAR, &sio->fdc_base);
180 	sio->fdc_base &= ~1;
181 	printk (KERN_INFO "SuperIO: Floppy controller at 0x%x\n", sio->fdc_base);
182 	pci_read_config_word (pdev, SIO_ACPIBAR, &sio->acpi_base);
183 	sio->acpi_base &= ~1;
184 	printk (KERN_INFO "SuperIO: ACPI at 0x%x\n", sio->acpi_base);
185 
186 	request_region (IC_PIC1, 0x1f, "pic1");
187 	request_region (IC_PIC2, 0x1f, "pic2");
188 	request_region (sio->acpi_base, 0x1f, "acpi");
189 
190 	/* Enable the legacy I/O function */
191         pci_read_config_word (pdev, PCI_COMMAND, &word);
192 	word |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_IO;
193 	pci_write_config_word (pdev, PCI_COMMAND, word);
194 	pci_set_master (pdev);
195 
196 	/* Next project is programming the onboard interrupt
197 	 * controllers.  PDC hasn't done this for us, since it's using
198 	 * polled I/O.
199 	 */
200 
201 	/* Set PIC interrupts to edge triggered */
202 	pci_write_config_byte (pdev, TRIGGER_1, 0x0);
203 	pci_write_config_byte (pdev, TRIGGER_2, 0x0);
204 
205 	/* Disable all interrupt routing */
206 	for (i = IR_LOW ; i < IR_HIGH ; i++)
207 		pci_write_config_byte (pdev, i, 0x0);
208 
209 	/* PIC1 Initialization Command Word register programming */
210 	outb (0x11,IC_PIC1+0);	/* ICW1: ICW4 write req | ICW1 */
211 	outb (0x00,IC_PIC1+1);	/* ICW2: N/A */
212 	outb (0x04,IC_PIC1+1);	/* ICW3: Cascade */
213 	outb (0x01,IC_PIC1+1);	/* ICW4: x86 mode */
214 
215 	/* PIC1 Program Operational Control Words */
216 	outb (0xff,IC_PIC1+1);	/* OCW1: Mask all interrupts */
217 	outb (0xc2,IC_PIC1+0);  /* OCW2: priority (3-7,0-2) */
218 
219 	/* PIC2 Initialization Command Word register programming */
220 	outb (0x11,IC_PIC2+0);	/* ICW1: ICW4 write req | ICW1 */
221 	outb (0x00,IC_PIC2+1);	/* ICW2: N/A */
222 	outb (0x02,IC_PIC2+1);	/* ICW3: Slave ID code */
223 	outb (0x01,IC_PIC2+1);	/* ICW4: x86 mode */
224 
225 	/* Program Operational Control Words */
226 	outb (0xff,IC_PIC1+1);	/* OCW1: Mask all interrupts */
227 	outb (0x68,IC_PIC1+0);	/* OCW3: OCW3 select | ESMM | SMM */
228 
229 	/* Write master mask reg */
230 
231 	outb (0xff,IC_PIC1+1);
232 
233 	/* Set up interrupt routing */
234 
235 	pci_write_config_byte (pdev, IR_USB, 0x10); /* USB on IRQ1 */
236 	pci_write_config_byte (pdev, IR_SER, 0x43); /* SP1 on IRQ3, SP2 on IRQ4 */
237 	pci_write_config_byte (pdev, IR_PFD, 0x65); /* PAR on IRQ5, FDC on IRQ6 */
238 	pci_write_config_byte (pdev, IR_IDE, 0x07); /* IDE1 on IRQ7 */
239 
240 	/* Set USB and IDE to level triggered interrupts, rest to edge */
241 	pci_write_config_byte (pdev, TRIGGER_1, 0x82); /* IRQ 1 and 7 */
242 
243 	/* Setup USB power regulation */
244 	outb(1, sio->acpi_base + USB_REG_CR);
245 	if (inb(sio->acpi_base + USB_REG_CR) & 1)
246 		printk(KERN_INFO "SuperIO: USB regulator enabled\n");
247 	else
248 		printk(KERN_ERR "USB regulator not initialized!\n");
249 
250 	pci_enable_device(pdev);
251 
252 	if (request_irq(sio->iosapic_irq,superio_interrupt,SA_INTERRUPT,
253 			"SuperIO",(void *)sio)) {
254 
255 		printk(KERN_ERR "SuperIO: could not get irq\n");
256 		BUG();
257 		return;
258 	}
259 
260 	sio->iosapic_irq_enabled = 1;
261 
262 }
263 
264 static void
superio_disable_irq(void * dev,int local_irq)265 superio_disable_irq(void *dev, int local_irq)
266 {
267 	u8 r8;
268 
269 	if ((local_irq < 1) || (local_irq == 2) || (local_irq > 7)) {
270 	    printk(KERN_ERR "SuperIO: Illegal irq number.\n");
271 	    BUG();
272 	    return;
273 	}
274 
275 	/* Mask interrupt */
276 
277 	r8 = inb(IC_PIC1+1);
278 	r8 |= (1 << local_irq);
279 	outb (r8,IC_PIC1+1);
280 }
281 
282 static void
superio_enable_irq(void * dev,int local_irq)283 superio_enable_irq(void *dev, int local_irq)
284 {
285 	struct superio_device *sio = (struct superio_device *)dev;
286 	u8 r8;
287 
288 	if ((local_irq < 1) || (local_irq == 2) || (local_irq > 7)) {
289 	    printk(KERN_ERR "SuperIO: Illegal irq number.\n");
290 	    BUG();
291 	    return;
292 	}
293 
294 	/*
295 	 * It's possible that we haven't initialized the legacy IO
296 	 * function yet. If not, do it now.
297 	 */
298 
299 	if (!sio->iosapic_irq_enabled)
300 		superio_init(sio);
301 
302 	/* Unmask interrupt */
303 
304 	r8 = inb(IC_PIC1+1);
305 	r8 &= ~(1 << local_irq);
306 	outb (r8,IC_PIC1+1);
307 }
308 
309 static void
superio_mask_irq(void * dev,int local_irq)310 superio_mask_irq(void *dev, int local_irq)
311 {
312 	BUG();
313 }
314 
315 static void
superio_unmask_irq(void * dev,int local_irq)316 superio_unmask_irq(void *dev, int local_irq)
317 {
318 	BUG();
319 }
320 
321 static struct irq_region_ops superio_irq_ops = {
322 	disable_irq:	superio_disable_irq,
323 	enable_irq:	superio_enable_irq,
324 	mask_irq:	superio_mask_irq,
325 	unmask_irq:	superio_unmask_irq
326 };
327 
328 #ifdef DEBUG_INIT
329 static unsigned short expected_device[3] = {
330 	PCI_DEVICE_ID_NS_87415,
331 	PCI_DEVICE_ID_NS_87560_LIO,
332 	PCI_DEVICE_ID_NS_87560_USB
333 };
334 #endif
335 
superio_fixup_irq(struct pci_dev * pcidev)336 int superio_fixup_irq(struct pci_dev *pcidev)
337 {
338 	int local_irq;
339 
340 #ifdef DEBUG_INIT
341 	int fn;
342 	fn = PCI_FUNC(pcidev->devfn);
343 
344 	/* Verify the function number matches the expected device id. */
345 	if (expected_device[fn] != pcidev->device) {
346 		BUG();
347 		return -1;
348 	}
349 	printk("superio_fixup_irq(%s) ven 0x%x dev 0x%x from %p\n",
350 		pcidev->slot_name,
351 		pcidev->vendor, pcidev->device,
352 		__builtin_return_address(0));
353 #endif
354 
355 	if (!sio_dev.irq_region) {
356 		/* Allocate an irq region for SuperIO devices */
357 		sio_dev.irq_region = alloc_irq_region(SUPERIO_NIRQS,
358 						&superio_irq_ops,
359 						"SuperIO", (void *) &sio_dev);
360 		if (!sio_dev.irq_region) {
361 			printk(KERN_WARNING "SuperIO: alloc_irq_region failed\n");
362 			return -1;
363 		}
364 	}
365 
366 	/*
367 	 * We don't allocate a SuperIO irq for the legacy IO function,
368 	 * since it is a "bridge". Instead, we will allocate irq's for
369 	 * each legacy device as they are initialized.
370 	 */
371 
372 	switch(pcidev->device) {
373 	case PCI_DEVICE_ID_NS_87415:		/* Function 0 */
374 		local_irq = IDE_IRQ;
375 		break;
376 	case PCI_DEVICE_ID_NS_87560_LIO:	/* Function 1 */
377 		sio_dev.lio_pdev = pcidev; /* save for later initialization */
378 		return -1;
379 	case PCI_DEVICE_ID_NS_87560_USB:	/* Function 2 */
380 		local_irq = USB_IRQ;
381 		break;
382 	default:
383 		local_irq = -1;
384 		BUG();
385 		break;
386 	}
387 
388 	return(sio_dev.irq_region->data.irqbase + local_irq);
389 }
390 
391 void __devinit
superio_serial_init(void)392 superio_serial_init(void)
393 {
394 #ifdef CONFIG_SERIAL
395 	struct serial_struct *serial;
396 	int retval;
397 
398 	if (!sio_dev.irq_region)
399 		return; /* superio not present */
400 
401 	if (!sio_dev.iosapic_irq_enabled)
402 		superio_init(&sio_dev);
403 
404 	serial = kmalloc(2 * sizeof (struct serial_struct), GFP_KERNEL);
405 
406 	if (!serial) {
407 		printk(KERN_WARNING "SuperIO: Could not get memory for serial struct.\n");
408 		return;
409 	}
410 
411 	memset(serial, 0, 2 * sizeof (struct serial_struct));
412 
413 	serial->type = PORT_16550A;
414 	serial->line = 0;
415 	serial->port = sio_dev.sp1_base;
416 	serial->port_high = 0;
417 	serial->irq = sio_dev.irq_region->data.irqbase + SP1_IRQ;
418 	serial->io_type = SERIAL_IO_PORT;
419 	serial->flags = 0;
420 	serial->xmit_fifo_size = 16;
421 	serial->custom_divisor = 0;
422 	serial->baud_base = 115200;
423 
424 	retval = register_serial(serial);
425 	if (retval < 0) {
426 		printk(KERN_WARNING "SuperIO: Register Serial #0 failed.\n");
427 		kfree (serial);
428 		return;
429 	}
430 
431 	serial++;
432 
433 	serial->type = PORT_16550A;
434 	serial->line = 1;
435 	serial->port = sio_dev.sp2_base;
436 	serial->port_high = 0;
437 	serial->irq = sio_dev.irq_region->data.irqbase + SP2_IRQ;
438 	serial->io_type = SERIAL_IO_PORT;
439 	serial->flags = 0;
440 	serial->xmit_fifo_size = 16;
441 	serial->custom_divisor = 0;
442 	serial->baud_base = 115200;
443 
444 	retval = register_serial(serial);
445 	if (retval < 0)
446 		printk(KERN_WARNING "SuperIO: Register Serial #1 failed.\n");
447 #endif /* CONFIG_SERIAL */
448 }
449 
450 EXPORT_SYMBOL(superio_serial_init);
451 
452 
453 #ifdef CONFIG_PARPORT_PC
454 void __devinit
superio_parport_init(void)455 superio_parport_init(void)
456 {
457 	if (!sio_dev.irq_region)
458 		return; /* superio not present */
459 
460 	if (!sio_dev.iosapic_irq_enabled)
461 		superio_init(&sio_dev);
462 
463 	if (!parport_pc_probe_port(sio_dev.pp_base,
464 		0 /*base_hi*/,
465 		sio_dev.irq_region->data.irqbase + PAR_IRQ,
466 		PARPORT_DMA_NONE /* dma */,
467 		NULL /*struct pci_dev* */))
468 
469 		printk(KERN_WARNING "SuperIO: Probing parallel port failed.\n");
470 }
471 
472 EXPORT_SYMBOL(superio_parport_init);
473 #endif	/* CONFIG_PARPORT_PC */
474 
475 
476 int
superio_get_ide_irq(void)477 superio_get_ide_irq(void)
478 {
479 	if (sio_dev.irq_region)
480 		return sio_dev.irq_region->data.irqbase + IDE_IRQ;
481 	else
482 		return 0;
483 }
484 
485 EXPORT_SYMBOL(superio_get_ide_irq);
486 
superio_probe(struct pci_dev * dev,const struct pci_device_id * id)487 static int __devinit superio_probe(struct pci_dev *dev, const struct pci_device_id *id)
488 {
489 #ifdef DEBUG_INIT
490 	printk("superio_probe(%s) ven 0x%x dev 0x%x sv 0x%x sd 0x%x class 0x%x\n",
491 		dev->slot_name,
492 		dev->vendor, dev->device,
493 		dev->subsystem_vendor, dev->subsystem_device,
494 		dev->class);
495 /*
496 ** superio_probe(00:0e.0) ven 0x100b dev 0x2 sv 0x0 sd 0x0 class 0x1018a
497 ** superio_probe(00:0e.1) ven 0x100b dev 0xe sv 0x0 sd 0x0 class 0x68000
498 ** superio_probe(00:0e.2) ven 0x100b dev 0x12 sv 0x0 sd 0x0 class 0xc0310
499 */
500 #endif
501 
502 	/* superio_fixup_irq(dev); */
503 
504 	if (dev->device == PCI_DEVICE_ID_NS_87560_LIO) {
505 #ifdef CONFIG_PARPORT_PC
506 		superio_parport_init();
507 #endif
508 		/* Don't call superio_serial_init() - see rs_init() */
509 		/* REVISIT : superio_fdc_init() ? */
510 		return 0;
511 	}
512 	else
513 	{
514 		/* don't claim this device; let whatever either driver
515 		 * do it
516 		 */
517 		return -1;
518 	}
519 }
520 
521 static struct pci_device_id superio_tbl[] __devinitdata = {
522 	{ PCI_VENDOR_ID_NS, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
523 	{ 0, }
524 };
525 
526 static struct pci_driver superio_driver = {
527 	name:		"SuperIO",
528 	id_table:	superio_tbl,
529 	probe:		superio_probe,
530 };
531 
superio_modinit(void)532 static int __init superio_modinit(void)
533 {
534 	return pci_module_init(&superio_driver);
535 }
536 
superio_exit(void)537 static void __exit superio_exit(void)
538 {
539 	pci_unregister_driver(&superio_driver);
540 }
541 
542 module_init(superio_modinit);
543 module_exit(superio_exit);
544