superio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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@onefishtwo.ca>
  12. * (C) Copyright 2001 John Marvin <jsm fc hp com>
  13. * (C) Copyright 2003 Grant Grundler <grundler parisc-linux org>
  14. * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * The initial version of this is by Martin Peterson. Alex deVries
  22. * has spent a bit of time trying to coax it into working.
  23. *
  24. * Major changes to get basic interrupt infrastructure working to
  25. * hopefully be able to support all SuperIO devices. Currently
  26. * works with serial. -- John Marvin <jsm@fc.hp.com>
  27. *
  28. * Converted superio_init() to be a PCI_FIXUP_FINAL callee.
  29. * -- Kyle McMartin <kyle@parisc-linux.org>
  30. */
  31. /* NOTES:
  32. *
  33. * Function 0 is an IDE controller. It is identical to a PC87415 IDE
  34. * controller (and identifies itself as such).
  35. *
  36. * Function 1 is a "Legacy I/O" controller. Under this function is a
  37. * whole mess of legacy I/O peripherals. Of course, HP hasn't enabled
  38. * all the functionality in hardware, but the following is available:
  39. *
  40. * Two 16550A compatible serial controllers
  41. * An IEEE 1284 compatible parallel port
  42. * A floppy disk controller
  43. *
  44. * Function 2 is a USB controller.
  45. *
  46. * We must be incredibly careful during initialization. Since all
  47. * interrupts are routed through function 1 (which is not allowed by
  48. * the PCI spec), we need to program the PICs on the legacy I/O port
  49. * *before* we attempt to set up IDE and USB. @#$!&
  50. *
  51. * According to HP, devices are only enabled by firmware if they have
  52. * a physical device connected.
  53. *
  54. * Configuration register bits:
  55. * 0x5A: FDC, SP1, IDE1, SP2, IDE2, PAR, Reserved, P92
  56. * 0x5B: RTC, 8259, 8254, DMA1, DMA2, KBC, P61, APM
  57. *
  58. */
  59. #include <linux/errno.h>
  60. #include <linux/init.h>
  61. #include <linux/module.h>
  62. #include <linux/types.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/ioport.h>
  65. #include <linux/serial.h>
  66. #include <linux/pci.h>
  67. #include <linux/parport.h>
  68. #include <linux/parport_pc.h>
  69. #include <linux/termios.h>
  70. #include <linux/tty.h>
  71. #include <linux/serial_core.h>
  72. #include <linux/delay.h>
  73. #include <asm/io.h>
  74. #include <asm/hardware.h>
  75. #include <asm/superio.h>
  76. static struct superio_device sio_dev;
  77. #undef DEBUG_SUPERIO_INIT
  78. #ifdef DEBUG_SUPERIO_INIT
  79. #define DBG_INIT(x...) printk(x)
  80. #else
  81. #define DBG_INIT(x...)
  82. #endif
  83. #define SUPERIO "SuperIO"
  84. #define PFX SUPERIO ": "
  85. static irqreturn_t
  86. superio_interrupt(int parent_irq, void *devp, struct pt_regs *regs)
  87. {
  88. u8 results;
  89. u8 local_irq;
  90. /* Poll the 8259 to see if there's an interrupt. */
  91. outb (OCW3_POLL,IC_PIC1+0);
  92. results = inb(IC_PIC1+0);
  93. /*
  94. * Bit 7: 1 = active Interrupt; 0 = no Interrupt pending
  95. * Bits 6-3: zero
  96. * Bits 2-0: highest priority, active requesting interrupt ID (0-7)
  97. */
  98. if ((results & 0x80) == 0) {
  99. /* I suspect "spurious" interrupts are from unmasking an IRQ.
  100. * We don't know if an interrupt was/is pending and thus
  101. * just call the handler for that IRQ as if it were pending.
  102. */
  103. return IRQ_NONE;
  104. }
  105. /* Check to see which device is interrupting */
  106. local_irq = results & 0x0f;
  107. if (local_irq == 2 || local_irq > 7) {
  108. printk(KERN_ERR PFX "slave interrupted!\n");
  109. return IRQ_HANDLED;
  110. }
  111. if (local_irq == 7) {
  112. /* Could be spurious. Check in service bits */
  113. outb(OCW3_ISR,IC_PIC1+0);
  114. results = inb(IC_PIC1+0);
  115. if ((results & 0x80) == 0) { /* if ISR7 not set: spurious */
  116. printk(KERN_WARNING PFX "spurious interrupt!\n");
  117. return IRQ_HANDLED;
  118. }
  119. }
  120. /* Call the appropriate device's interrupt */
  121. __do_IRQ(local_irq, regs);
  122. /* set EOI - forces a new interrupt if a lower priority device
  123. * still needs service.
  124. */
  125. outb((OCW2_SEOI|local_irq),IC_PIC1 + 0);
  126. return IRQ_HANDLED;
  127. }
  128. /* Initialize Super I/O device */
  129. static void
  130. superio_init(struct pci_dev *pcidev)
  131. {
  132. struct superio_device *sio = &sio_dev;
  133. struct pci_dev *pdev = sio->lio_pdev;
  134. u16 word;
  135. if (sio->suckyio_irq_enabled)
  136. return;
  137. if (!pdev) BUG();
  138. if (!sio->usb_pdev) BUG();
  139. /* use the IRQ iosapic found for USB INT D... */
  140. pdev->irq = sio->usb_pdev->irq;
  141. /* ...then properly fixup the USB to point at suckyio PIC */
  142. sio->usb_pdev->irq = superio_fixup_irq(sio->usb_pdev);
  143. printk(KERN_INFO PFX "Found NS87560 Legacy I/O device at %s (IRQ %i) \n",
  144. pci_name(pdev), pdev->irq);
  145. pci_read_config_dword (pdev, SIO_SP1BAR, &sio->sp1_base);
  146. sio->sp1_base &= ~1;
  147. printk(KERN_INFO PFX "Serial port 1 at 0x%x\n", sio->sp1_base);
  148. pci_read_config_dword (pdev, SIO_SP2BAR, &sio->sp2_base);
  149. sio->sp2_base &= ~1;
  150. printk(KERN_INFO PFX "Serial port 2 at 0x%x\n", sio->sp2_base);
  151. pci_read_config_dword (pdev, SIO_PPBAR, &sio->pp_base);
  152. sio->pp_base &= ~1;
  153. printk(KERN_INFO PFX "Parallel port at 0x%x\n", sio->pp_base);
  154. pci_read_config_dword (pdev, SIO_FDCBAR, &sio->fdc_base);
  155. sio->fdc_base &= ~1;
  156. printk(KERN_INFO PFX "Floppy controller at 0x%x\n", sio->fdc_base);
  157. pci_read_config_dword (pdev, SIO_ACPIBAR, &sio->acpi_base);
  158. sio->acpi_base &= ~1;
  159. printk(KERN_INFO PFX "ACPI at 0x%x\n", sio->acpi_base);
  160. request_region (IC_PIC1, 0x1f, "pic1");
  161. request_region (IC_PIC2, 0x1f, "pic2");
  162. request_region (sio->acpi_base, 0x1f, "acpi");
  163. /* Enable the legacy I/O function */
  164. pci_read_config_word (pdev, PCI_COMMAND, &word);
  165. word |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_IO;
  166. pci_write_config_word (pdev, PCI_COMMAND, word);
  167. pci_set_master (pdev);
  168. pci_enable_device(pdev);
  169. /*
  170. * Next project is programming the onboard interrupt controllers.
  171. * PDC hasn't done this for us, since it's using polled I/O.
  172. *
  173. * XXX Use dword writes to avoid bugs in Elroy or Suckyio Config
  174. * space access. PCI is by nature a 32-bit bus and config
  175. * space can be sensitive to that.
  176. */
  177. /* 0x64 - 0x67 :
  178. DMA Rtg 2
  179. DMA Rtg 3
  180. DMA Chan Ctl
  181. TRIGGER_1 == 0x82 USB & IDE level triggered, rest to edge
  182. */
  183. pci_write_config_dword (pdev, 0x64, 0x82000000U);
  184. /* 0x68 - 0x6b :
  185. TRIGGER_2 == 0x00 all edge triggered (not used)
  186. CFG_IR_SER == 0x43 SerPort1 = IRQ3, SerPort2 = IRQ4
  187. CFG_IR_PF == 0x65 ParPort = IRQ5, FloppyCtlr = IRQ6
  188. CFG_IR_IDE == 0x07 IDE1 = IRQ7, reserved
  189. */
  190. pci_write_config_dword (pdev, TRIGGER_2, 0x07654300U);
  191. /* 0x6c - 0x6f :
  192. CFG_IR_INTAB == 0x00
  193. CFG_IR_INTCD == 0x10 USB = IRQ1
  194. CFG_IR_PS2 == 0x00
  195. CFG_IR_FXBUS == 0x00
  196. */
  197. pci_write_config_dword (pdev, CFG_IR_INTAB, 0x00001000U);
  198. /* 0x70 - 0x73 :
  199. CFG_IR_USB == 0x00 not used. USB is connected to INTD.
  200. CFG_IR_ACPI == 0x00 not used.
  201. DMA Priority == 0x4c88 Power on default value. NFC.
  202. */
  203. pci_write_config_dword (pdev, CFG_IR_USB, 0x4c880000U);
  204. /* PIC1 Initialization Command Word register programming */
  205. outb (0x11,IC_PIC1+0); /* ICW1: ICW4 write req | ICW1 */
  206. outb (0x00,IC_PIC1+1); /* ICW2: interrupt vector table - not used */
  207. outb (0x04,IC_PIC1+1); /* ICW3: Cascade */
  208. outb (0x01,IC_PIC1+1); /* ICW4: x86 mode */
  209. /* PIC1 Program Operational Control Words */
  210. outb (0xff,IC_PIC1+1); /* OCW1: Mask all interrupts */
  211. outb (0xc2,IC_PIC1+0); /* OCW2: priority (3-7,0-2) */
  212. /* PIC2 Initialization Command Word register programming */
  213. outb (0x11,IC_PIC2+0); /* ICW1: ICW4 write req | ICW1 */
  214. outb (0x00,IC_PIC2+1); /* ICW2: N/A */
  215. outb (0x02,IC_PIC2+1); /* ICW3: Slave ID code */
  216. outb (0x01,IC_PIC2+1); /* ICW4: x86 mode */
  217. /* Program Operational Control Words */
  218. outb (0xff,IC_PIC1+1); /* OCW1: Mask all interrupts */
  219. outb (0x68,IC_PIC1+0); /* OCW3: OCW3 select | ESMM | SMM */
  220. /* Write master mask reg */
  221. outb (0xff,IC_PIC1+1);
  222. /* Setup USB power regulation */
  223. outb(1, sio->acpi_base + USB_REG_CR);
  224. if (inb(sio->acpi_base + USB_REG_CR) & 1)
  225. printk(KERN_INFO PFX "USB regulator enabled\n");
  226. else
  227. printk(KERN_ERR PFX "USB regulator not initialized!\n");
  228. if (request_irq(pdev->irq, superio_interrupt, SA_INTERRUPT,
  229. SUPERIO, (void *)sio)) {
  230. printk(KERN_ERR PFX "could not get irq\n");
  231. BUG();
  232. return;
  233. }
  234. sio->suckyio_irq_enabled = 1;
  235. }
  236. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87560_LIO, superio_init);
  237. static void superio_disable_irq(unsigned int irq)
  238. {
  239. u8 r8;
  240. if ((irq < 1) || (irq == 2) || (irq > 7)) {
  241. printk(KERN_ERR PFX "Illegal irq number.\n");
  242. BUG();
  243. return;
  244. }
  245. /* Mask interrupt */
  246. r8 = inb(IC_PIC1+1);
  247. r8 |= (1 << irq);
  248. outb (r8,IC_PIC1+1);
  249. }
  250. static void superio_enable_irq(unsigned int irq)
  251. {
  252. u8 r8;
  253. if ((irq < 1) || (irq == 2) || (irq > 7)) {
  254. printk(KERN_ERR PFX "Illegal irq number (%d).\n", irq);
  255. BUG();
  256. return;
  257. }
  258. /* Unmask interrupt */
  259. r8 = inb(IC_PIC1+1);
  260. r8 &= ~(1 << irq);
  261. outb (r8,IC_PIC1+1);
  262. }
  263. static unsigned int superio_startup_irq(unsigned int irq)
  264. {
  265. superio_enable_irq(irq);
  266. return 0;
  267. }
  268. static struct hw_interrupt_type superio_interrupt_type = {
  269. .typename = SUPERIO,
  270. .startup = superio_startup_irq,
  271. .shutdown = superio_disable_irq,
  272. .enable = superio_enable_irq,
  273. .disable = superio_disable_irq,
  274. .ack = no_ack_irq,
  275. .end = no_end_irq,
  276. };
  277. #ifdef DEBUG_SUPERIO_INIT
  278. static unsigned short expected_device[3] = {
  279. PCI_DEVICE_ID_NS_87415,
  280. PCI_DEVICE_ID_NS_87560_LIO,
  281. PCI_DEVICE_ID_NS_87560_USB
  282. };
  283. #endif
  284. int superio_fixup_irq(struct pci_dev *pcidev)
  285. {
  286. int local_irq, i;
  287. #ifdef DEBUG_SUPERIO_INIT
  288. int fn;
  289. fn = PCI_FUNC(pcidev->devfn);
  290. /* Verify the function number matches the expected device id. */
  291. if (expected_device[fn] != pcidev->device) {
  292. BUG();
  293. return -1;
  294. }
  295. printk("superio_fixup_irq(%s) ven 0x%x dev 0x%x from %p\n",
  296. pci_name(pcidev),
  297. pcidev->vendor, pcidev->device,
  298. __builtin_return_address(0));
  299. #endif
  300. for (i = 0; i < 16; i++) {
  301. irq_desc[i].handler = &superio_interrupt_type;
  302. }
  303. /*
  304. * We don't allocate a SuperIO irq for the legacy IO function,
  305. * since it is a "bridge". Instead, we will allocate irq's for
  306. * each legacy device as they are initialized.
  307. */
  308. switch(pcidev->device) {
  309. case PCI_DEVICE_ID_NS_87415: /* Function 0 */
  310. local_irq = IDE_IRQ;
  311. break;
  312. case PCI_DEVICE_ID_NS_87560_LIO: /* Function 1 */
  313. sio_dev.lio_pdev = pcidev; /* save for superio_init() */
  314. return -1;
  315. case PCI_DEVICE_ID_NS_87560_USB: /* Function 2 */
  316. sio_dev.usb_pdev = pcidev; /* save for superio_init() */
  317. local_irq = USB_IRQ;
  318. break;
  319. default:
  320. local_irq = -1;
  321. BUG();
  322. break;
  323. }
  324. return local_irq;
  325. }
  326. static struct uart_port serial[] = {
  327. {
  328. .iotype = UPIO_PORT,
  329. .line = 0,
  330. .type = PORT_16550A,
  331. .uartclk = 115200*16,
  332. .fifosize = 16,
  333. },
  334. {
  335. .iotype = UPIO_PORT,
  336. .line = 1,
  337. .type = PORT_16550A,
  338. .uartclk = 115200*16,
  339. .fifosize = 16,
  340. }
  341. };
  342. static void __devinit superio_serial_init(void)
  343. {
  344. #ifdef CONFIG_SERIAL_8250
  345. int retval;
  346. serial[0].iobase = sio_dev.sp1_base;
  347. serial[0].irq = SP1_IRQ;
  348. spin_lock_init(&serial[0].lock);
  349. retval = early_serial_setup(&serial[0]);
  350. if (retval < 0) {
  351. printk(KERN_WARNING PFX "Register Serial #0 failed.\n");
  352. return;
  353. }
  354. serial[1].iobase = sio_dev.sp2_base;
  355. serial[1].irq = SP2_IRQ;
  356. spin_lock_init(&serial[1].lock);
  357. retval = early_serial_setup(&serial[1]);
  358. if (retval < 0)
  359. printk(KERN_WARNING PFX "Register Serial #1 failed.\n");
  360. #endif /* CONFIG_SERIAL_8250 */
  361. }
  362. static void __devinit superio_parport_init(void)
  363. {
  364. #ifdef CONFIG_PARPORT_PC
  365. if (!parport_pc_probe_port(sio_dev.pp_base,
  366. 0 /*base_hi*/,
  367. PAR_IRQ,
  368. PARPORT_DMA_NONE /* dma */,
  369. NULL /*struct pci_dev* */) )
  370. printk(KERN_WARNING PFX "Probing parallel port failed.\n");
  371. #endif /* CONFIG_PARPORT_PC */
  372. }
  373. static void superio_fixup_pci(struct pci_dev *pdev)
  374. {
  375. u8 prog;
  376. pdev->class |= 0x5;
  377. pci_write_config_byte(pdev, PCI_CLASS_PROG, pdev->class);
  378. pci_read_config_byte(pdev, PCI_CLASS_PROG, &prog);
  379. printk("PCI: Enabled native mode for NS87415 (pif=0x%x)\n", prog);
  380. }
  381. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415, superio_fixup_pci);
  382. static int __devinit
  383. superio_probe(struct pci_dev *dev, const struct pci_device_id *id)
  384. {
  385. struct superio_device *sio = &sio_dev;
  386. /*
  387. ** superio_probe(00:0e.0) ven 0x100b dev 0x2 sv 0x0 sd 0x0 class 0x1018a
  388. ** superio_probe(00:0e.1) ven 0x100b dev 0xe sv 0x0 sd 0x0 class 0x68000
  389. ** superio_probe(00:0e.2) ven 0x100b dev 0x12 sv 0x0 sd 0x0 class 0xc0310
  390. */
  391. DBG_INIT("superio_probe(%s) ven 0x%x dev 0x%x sv 0x%x sd 0x%x class 0x%x\n",
  392. pci_name(dev),
  393. dev->vendor, dev->device,
  394. dev->subsystem_vendor, dev->subsystem_device,
  395. dev->class);
  396. if (!sio->suckyio_irq_enabled)
  397. BUG(); /* Enabled by PCI_FIXUP_FINAL */
  398. if (dev->device == PCI_DEVICE_ID_NS_87560_LIO) { /* Function 1 */
  399. superio_parport_init();
  400. superio_serial_init();
  401. /* REVISIT XXX : superio_fdc_init() ? */
  402. return 0;
  403. } else if (dev->device == PCI_DEVICE_ID_NS_87415) { /* Function 0 */
  404. DBG_INIT("superio_probe: ignoring IDE 87415\n");
  405. } else if (dev->device == PCI_DEVICE_ID_NS_87560_USB) { /* Function 2 */
  406. DBG_INIT("superio_probe: ignoring USB OHCI controller\n");
  407. } else {
  408. DBG_INIT("superio_probe: WTF? Fire Extinguisher?\n");
  409. }
  410. /* Let appropriate other driver claim this device. */
  411. return -ENODEV;
  412. }
  413. static struct pci_device_id superio_tbl[] = {
  414. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87560_LIO) },
  415. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87560_USB) },
  416. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415) },
  417. { 0, }
  418. };
  419. static struct pci_driver superio_driver = {
  420. .name = SUPERIO,
  421. .id_table = superio_tbl,
  422. .probe = superio_probe,
  423. };
  424. static int __init superio_modinit(void)
  425. {
  426. return pci_register_driver(&superio_driver);
  427. }
  428. static void __exit superio_exit(void)
  429. {
  430. pci_unregister_driver(&superio_driver);
  431. }
  432. module_init(superio_modinit);
  433. module_exit(superio_exit);