isa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * TODO: clean-up
  8. */
  9. #include <common.h>
  10. #include <asm/processor.h>
  11. #include <stdio_dev.h>
  12. #include "isa.h"
  13. #include "piix4_pci.h"
  14. #include "kbd.h"
  15. #include "video.h"
  16. #undef ISA_DEBUG
  17. #ifdef ISA_DEBUG
  18. #define PRINTF(fmt,args...) printf (fmt ,##args)
  19. #else
  20. #define PRINTF(fmt,args...)
  21. #endif
  22. #if defined(CONFIG_PIP405)
  23. extern int drv_isa_kbd_init (void);
  24. /* fdc (logical device 0) */
  25. const SIO_LOGDEV_TABLE sio_fdc[] = {
  26. {0x60, 3}, /* set IO to FDPort (3F0) */
  27. {0x61, 0xF0}, /* set IO to FDPort (3F0) */
  28. {0x70, 06}, /* set IRQ 6 for FDPort */
  29. {0x74, 02}, /* set DMA 2 for FDPort */
  30. {0xF0, 0x05}, /* set to PS2 type */
  31. {0xF1, 0x00}, /* default value */
  32. {0x30, 1}, /* and activate the device */
  33. {0xFF, 0} /* end of device table */
  34. };
  35. /* paralell port (logical device 3) */
  36. const SIO_LOGDEV_TABLE sio_pport[] = {
  37. {0x60, 3}, /* set IO to PPort (378) */
  38. {0x61, 0x78}, /* set IO to PPort (378) */
  39. {0x70, 07}, /* set IRQ 7 for PPort */
  40. {0xF1, 00}, /* set PPort to normal */
  41. {0x30, 1}, /* and activate the device */
  42. {0xFF, 0} /* end of device table */
  43. };
  44. /* paralell port (logical device 3) Floppy assigned to lpt */
  45. const SIO_LOGDEV_TABLE sio_pport_fdc[] = {
  46. {0x60, 3}, /* set IO to PPort (378) */
  47. {0x61, 0x78}, /* set IO to PPort (378) */
  48. {0x70, 07}, /* set IRQ 7 for PPort */
  49. {0xF1, 02}, /* set PPort to Floppy */
  50. {0x30, 1}, /* and activate the device */
  51. {0xFF, 0} /* end of device table */
  52. };
  53. /* uart 1 (logical device 4) */
  54. const SIO_LOGDEV_TABLE sio_com1[] = {
  55. {0x60, 3}, /* set IO to COM1 (3F8) */
  56. {0x61, 0xF8}, /* set IO to COM1 (3F8) */
  57. {0x70, 04}, /* set IRQ 4 for COM1 */
  58. {0x30, 1}, /* and activate the device */
  59. {0xFF, 0} /* end of device table */
  60. };
  61. /* uart 2 (logical device 5) */
  62. const SIO_LOGDEV_TABLE sio_com2[] = {
  63. {0x60, 2}, /* set IO to COM2 (2F8) */
  64. {0x61, 0xF8}, /* set IO to COM2 (2F8) */
  65. {0x70, 03}, /* set IRQ 3 for COM2 */
  66. {0x30, 1}, /* and activate the device */
  67. {0xFF, 0} /* end of device table */
  68. };
  69. /* keyboard controller (logical device 7) */
  70. const SIO_LOGDEV_TABLE sio_keyboard[] = {
  71. {0x70, 1}, /* set IRQ 1 for keyboard */
  72. {0x72, 12}, /* set IRQ 12 for mouse */
  73. {0xF0, 0}, /* disable Port92 (this is a PowerPC!!) */
  74. {0x30, 1}, /* and activate the device */
  75. {0xFF, 0} /* end of device table */
  76. };
  77. /*******************************************************************************
  78. * Config SuperIO FDC37C672
  79. ********************************************************************************/
  80. unsigned char open_cfg_super_IO(int address)
  81. {
  82. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,0x55); /* open config */
  83. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,0x20); /* set address to DEV ID */
  84. if(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address | 0x1)==0x40) /* ok Device ID is correct */
  85. return true;
  86. else
  87. return false;
  88. }
  89. void close_cfg_super_IO(int address)
  90. {
  91. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,0xAA); /* close config */
  92. }
  93. unsigned char read_cfg_super_IO(int address, unsigned char function, unsigned char regaddr)
  94. {
  95. /* assuming config reg is open */
  96. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,0x7); /* points to the function reg */
  97. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address | 1,function); /* set the function no */
  98. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,regaddr); /* sets the address in the function */
  99. return in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address | 1);
  100. }
  101. void write_cfg_super_IO(int address, unsigned char function, unsigned char regaddr, unsigned char data)
  102. {
  103. /* assuming config reg is open */
  104. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,0x7); /* points to the function reg */
  105. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address | 1,function); /* set the function no */
  106. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address,regaddr); /* sets the address in the function */
  107. out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS | address | 1,data); /* writes the data */
  108. }
  109. void isa_write_table(SIO_LOGDEV_TABLE *ldt,unsigned char ldev)
  110. {
  111. while (ldt->index != 0xFF) {
  112. write_cfg_super_IO(SIO_CFG_PORT, ldev, ldt->index, ldt->val);
  113. ldt++;
  114. } /* endwhile */
  115. }
  116. void isa_sio_loadtable(void)
  117. {
  118. char *s = getenv("floppy");
  119. /* setup Floppy device 0*/
  120. isa_write_table((SIO_LOGDEV_TABLE *)&sio_fdc,0);
  121. /* setup parallel port device 3 */
  122. if(s && !strncmp(s, "lpt", 3)) {
  123. printf("SIO: Floppy assigned to LPT\n");
  124. /* floppy is assigned to the LPT */
  125. isa_write_table((SIO_LOGDEV_TABLE *)&sio_pport_fdc,3);
  126. }
  127. else {
  128. /*printf("Floppy assigned to internal port\n");*/
  129. isa_write_table((SIO_LOGDEV_TABLE *)&sio_pport,3);
  130. }
  131. /* setup Com1 port device 4 */
  132. isa_write_table((SIO_LOGDEV_TABLE *)&sio_com1,4);
  133. /* setup Com2 port device 5 */
  134. isa_write_table((SIO_LOGDEV_TABLE *)&sio_com2,5);
  135. /* setup keyboards device 7 */
  136. isa_write_table((SIO_LOGDEV_TABLE *)&sio_keyboard,7);
  137. }
  138. void isa_sio_setup(void)
  139. {
  140. if (open_cfg_super_IO(SIO_CFG_PORT) == true)
  141. {
  142. isa_sio_loadtable();
  143. close_cfg_super_IO(0x3F0);
  144. }
  145. }
  146. #endif
  147. /******************************************************************************
  148. * IRQ Controller
  149. * we use the Vector mode
  150. */
  151. struct isa_irq_action {
  152. interrupt_handler_t *handler;
  153. void *arg;
  154. int count;
  155. };
  156. static struct isa_irq_action isa_irqs[16];
  157. /*
  158. * This contains the irq mask for both 8259A irq controllers,
  159. */
  160. static unsigned int cached_irq_mask = 0xfff9;
  161. #define cached_imr1 (unsigned char)cached_irq_mask
  162. #define cached_imr2 (unsigned char)(cached_irq_mask>>8)
  163. #define IMR_1 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT1_OCW1
  164. #define IMR_2 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT2_OCW1
  165. #define ICW1_1 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT1_ICW1
  166. #define ICW1_2 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT2_ICW1
  167. #define ICW2_1 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT1_ICW2
  168. #define ICW2_2 CONFIG_SYS_ISA_IO_BASE_ADDRESS + PIIX4_ISA_INT2_ICW2
  169. #define ICW3_1 ICW2_1
  170. #define ICW3_2 ICW2_2
  171. #define ICW4_1 ICW2_1
  172. #define ICW4_2 ICW2_2
  173. #define ISR_1 ICW1_1
  174. #define ISR_2 ICW1_2
  175. void disable_8259A_irq(unsigned int irq)
  176. {
  177. unsigned int mask = 1 << irq;
  178. cached_irq_mask |= mask;
  179. if (irq & 8)
  180. out8(IMR_2,cached_imr2);
  181. else
  182. out8(IMR_1,cached_imr1);
  183. }
  184. void enable_8259A_irq(unsigned int irq)
  185. {
  186. unsigned int mask = ~(1 << irq);
  187. cached_irq_mask &= mask;
  188. if (irq & 8)
  189. out8(IMR_2,cached_imr2);
  190. else
  191. out8(IMR_1,cached_imr1);
  192. }
  193. /*
  194. int i8259A_irq_pending(unsigned int irq)
  195. {
  196. unsigned int mask = 1<<irq;
  197. int ret;
  198. if (irq < 8)
  199. ret = inb(0x20) & mask;
  200. else
  201. ret = inb(0xA0) & (mask >> 8);
  202. spin_unlock_irqrestore(&i8259A_lock, flags);
  203. return ret;
  204. }
  205. */
  206. /*
  207. * This function assumes to be called rarely. Switching between
  208. * 8259A registers is slow.
  209. */
  210. int i8259A_irq_real(unsigned int irq)
  211. {
  212. int value;
  213. int irqmask = 1<<irq;
  214. if (irq < 8) {
  215. out8(ISR_1,0x0B); /* ISR register */
  216. value = in8(ISR_1) & irqmask;
  217. out8(ISR_1,0x0A); /* back to the IRR register */
  218. return value;
  219. }
  220. out8(ISR_2,0x0B); /* ISR register */
  221. value = in8(ISR_2) & (irqmask >> 8);
  222. out8(ISR_2,0x0A); /* back to the IRR register */
  223. return value;
  224. }
  225. /*
  226. * Careful! The 8259A is a fragile beast, it pretty
  227. * much _has_ to be done exactly like this (mask it
  228. * first, _then_ send the EOI, and the order of EOI
  229. * to the two 8259s is important!
  230. */
  231. void mask_and_ack_8259A(unsigned int irq)
  232. {
  233. unsigned int irqmask = 1 << irq;
  234. unsigned int temp_irqmask = cached_irq_mask;
  235. /*
  236. * Lightweight spurious IRQ detection. We do not want
  237. * to overdo spurious IRQ handling - it's usually a sign
  238. * of hardware problems, so we only do the checks we can
  239. * do without slowing down good hardware unnecesserily.
  240. *
  241. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  242. * usually resulting from the 8259A-1|2 PICs) occur
  243. * even if the IRQ is masked in the 8259A. Thus we
  244. * can check spurious 8259A IRQs without doing the
  245. * quite slow i8259A_irq_real() call for every IRQ.
  246. * This does not cover 100% of spurious interrupts,
  247. * but should be enough to warn the user that there
  248. * is something bad going on ...
  249. */
  250. if (temp_irqmask & irqmask)
  251. goto spurious_8259A_irq;
  252. temp_irqmask |= irqmask;
  253. handle_real_irq:
  254. if (irq & 8) {
  255. in8(IMR_2); /* DUMMY - (do we need this?) */
  256. out8(IMR_2,(unsigned char)(temp_irqmask>>8));
  257. out8(ISR_2,0x60+(irq&7));/* 'Specific EOI' to slave */
  258. out8(ISR_1,0x62); /* 'Specific EOI' to master-IRQ2 */
  259. out8(IMR_2,cached_imr2); /* turn it on again */
  260. } else {
  261. in8(IMR_1); /* DUMMY - (do we need this?) */
  262. out8(IMR_1,(unsigned char)temp_irqmask);
  263. out8(ISR_1,0x60+irq); /* 'Specific EOI' to master */
  264. out8(IMR_1,cached_imr1); /* turn it on again */
  265. }
  266. return;
  267. spurious_8259A_irq:
  268. /*
  269. * this is the slow path - should happen rarely.
  270. */
  271. if (i8259A_irq_real(irq))
  272. /*
  273. * oops, the IRQ _is_ in service according to the
  274. * 8259A - not spurious, go handle it.
  275. */
  276. goto handle_real_irq;
  277. {
  278. static int spurious_irq_mask;
  279. /*
  280. * At this point we can be sure the IRQ is spurious,
  281. * lets ACK and report it. [once per IRQ]
  282. */
  283. if (!(spurious_irq_mask & irqmask)) {
  284. PRINTF("spurious 8259A interrupt: IRQ%d.\n", irq);
  285. spurious_irq_mask |= irqmask;
  286. }
  287. /* irq_err_count++; */
  288. /*
  289. * Theoretically we do not have to handle this IRQ,
  290. * but in Linux this does not cause problems and is
  291. * simpler for us.
  292. */
  293. goto handle_real_irq;
  294. }
  295. }
  296. void init_8259A(void)
  297. {
  298. out8(IMR_1,0xff); /* mask all of 8259A-1 */
  299. out8(IMR_2,0xff); /* mask all of 8259A-2 */
  300. out8(ICW1_1,0x11); /* ICW1: select 8259A-1 init */
  301. out8(ICW2_1,0x20 + 0); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
  302. out8(ICW3_1,0x04); /* 8259A-1 (the master) has a slave on IR2 */
  303. out8(ICW4_1,0x01); /* master expects normal EOI */
  304. out8(ICW1_2,0x11); /* ICW2: select 8259A-2 init */
  305. out8(ICW2_2,0x20 + 8); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
  306. out8(ICW3_2,0x02); /* 8259A-2 is a slave on master's IR2 */
  307. out8(ICW4_2,0x01); /* (slave's support for AEOI in flat mode
  308. is to be investigated) */
  309. udelay(10000); /* wait for 8259A to initialize */
  310. out8(IMR_1,cached_imr1); /* restore master IRQ mask */
  311. udelay(10000); /* wait for 8259A to initialize */
  312. out8(IMR_2,cached_imr2); /* restore slave IRQ mask */
  313. }
  314. #define PCI_INT_ACK_ADDR 0xEED00000
  315. int handle_isa_int(void)
  316. {
  317. unsigned long irqack;
  318. unsigned char irq;
  319. /* first we acknokledge the int via the PCI bus */
  320. irqack=in32(PCI_INT_ACK_ADDR);
  321. /* now we get the ISRs */
  322. in8(ISR_2);
  323. in8(ISR_1);
  324. irq=(unsigned char)irqack;
  325. irq-=32;
  326. /* if((irq==7)&&((isr1&0x80)==0)) {
  327. PRINTF("IRQ7 detected but not in ISR\n");
  328. }
  329. else {
  330. */ /* we should handle cascaded interrupts here also */
  331. {
  332. /* printf("ISA Irq %d\n",irq); */
  333. isa_irqs[irq].count++;
  334. if(irq!=2) { /* just swallow the cascade irq 2 */
  335. if (isa_irqs[irq].handler != NULL)
  336. (*isa_irqs[irq].handler)(isa_irqs[irq].arg); /* call isr */
  337. else {
  338. PRINTF ("bogus interrupt vector 0x%x\n", irq);
  339. }
  340. }
  341. }
  342. /* issue EOI instruction to clear the IRQ */
  343. mask_and_ack_8259A(irq);
  344. return 0;
  345. }
  346. /******************************************************************
  347. * Install and free an ISA interrupt handler.
  348. */
  349. void isa_irq_install_handler(int vec, interrupt_handler_t *handler, void *arg)
  350. {
  351. if (isa_irqs[vec].handler != NULL) {
  352. printf ("ISA Interrupt vector %d: handler 0x%x replacing 0x%x\n",
  353. vec, (uint)handler, (uint)isa_irqs[vec].handler);
  354. }
  355. isa_irqs[vec].handler = handler;
  356. isa_irqs[vec].arg = arg;
  357. enable_8259A_irq(vec);
  358. PRINTF ("Install ISA IRQ %d ==> %p, @ %p mask=%04x\n", vec, handler, &isa_irqs[vec].handler,cached_irq_mask);
  359. }
  360. void isa_irq_free_handler(int vec)
  361. {
  362. disable_8259A_irq(vec);
  363. isa_irqs[vec].handler = NULL;
  364. isa_irqs[vec].arg = NULL;
  365. PRINTF ("Free ISA IRQ %d mask=%04x\n", vec, cached_irq_mask);
  366. }
  367. /****************************************************************************/
  368. void isa_init_irq_contr(void)
  369. {
  370. int i;
  371. /* disable all Interrupts */
  372. /* first write icws controller 1 */
  373. for(i=0;i<16;i++)
  374. {
  375. isa_irqs[i].handler=NULL;
  376. isa_irqs[i].arg=NULL;
  377. isa_irqs[i].count=0;
  378. }
  379. init_8259A();
  380. out8(IMR_2,0xFF);
  381. }
  382. /*************************************************************************/
  383. void isa_show_irq(void)
  384. {
  385. int vec;
  386. printf ("\nISA Interrupt-Information:\n");
  387. printf ("Nr Routine Arg Count\n");
  388. for (vec=0; vec<16; vec++) {
  389. if (isa_irqs[vec].handler != NULL) {
  390. printf ("%02d %08lx %08lx %d\n",
  391. vec,
  392. (ulong)isa_irqs[vec].handler,
  393. (ulong)isa_irqs[vec].arg,
  394. isa_irqs[vec].count);
  395. }
  396. }
  397. }
  398. int isa_irq_get_count(int vec)
  399. {
  400. return(isa_irqs[vec].count);
  401. }
  402. /******************************************************************
  403. * Init the ISA bus and devices.
  404. */
  405. #if defined(CONFIG_PIP405)
  406. int isa_init(void)
  407. {
  408. isa_sio_setup();
  409. isa_init_irq_contr();
  410. drv_isa_kbd_init();
  411. return 0;
  412. }
  413. #endif