isa.c 13 KB

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