isa.c 13 KB

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