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