isa.c 13 KB

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