summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/mips/shared/irq/i8259.c
blob: 90bfaf52640ca3bd34290b304f0042b4f108ce11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 *  @file
 *  
 *  This file was based upon the powerpc and the i386.
 */

/*
 *  COPYRIGHT (c) 1989-2012.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

/*
 *  Copyright (C) 1998, 1999 valette@crf.canon.fr
 */

#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/i8259.h>
#include <bsp/pci.h>
#include <bsp/irq-generic.h>


#define DEBUG_8259      1

#define ValidateIrqLine( _irq ) \
   if ( ((int)_irq < 0) ||((int)_irq > 16)) return 1;

/*-------------------------------------------------------------------------+
| Cache for 1st and 2nd PIC IRQ line's status (enabled or disabled) register.
+--------------------------------------------------------------------------*/
/*
 * lower byte is interrupt mask on the master PIC.
 * while upper bits are interrupt on the slave PIC.
 */
volatile rtems_i8259_masks i8259s_cache = 0xfffb;

/*-------------------------------------------------------------------------+
|         Function:  BSP_irq_disable_at_i8259s
|      Description: Mask IRQ line in appropriate PIC chip.
| Global Variables: i8259s_cache
|        Arguments: vector_offset - number of IRQ line to mask.
|          Returns: original state or -1 on error.
+--------------------------------------------------------------------------*/
int BSP_irq_disable_at_i8259s    (const rtems_irq_number irqLine)
{
  unsigned short        mask;
  rtems_interrupt_level level;
  int                   rval;

  ValidateIrqLine(irqLine);

  rtems_interrupt_disable(level);

  /* Recalculate the value */
  mask = 1 << irqLine;
  rval = i8259s_cache & mask ? 0 : 1;
  i8259s_cache |= mask;

  /* Determine which chip and write the value. */
  if (irqLine < 8) {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_IMR_IO_PORT, 
      i8259s_cache & 0xff
    );
  } else {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_SLAVE_IMR_IO_PORT, 
      ((i8259s_cache & 0xff00) >> 8)
    );
  }

  rtems_interrupt_enable(level);

  return rval;
}

/*-------------------------------------------------------------------------+
|         Function:  BSP_irq_enable_at_i8259s
|      Description: Unmask IRQ line in appropriate PIC chip.
| Global Variables: i8259s_cache
|        Arguments: irqLine - number of IRQ line to mask.
|          Returns: Nothing.
+--------------------------------------------------------------------------*/
int BSP_irq_enable_at_i8259s    (const rtems_irq_number irqLine)
{
  unsigned short        mask;
  rtems_interrupt_level level;
  
  ValidateIrqLine( irqLine );

  rtems_interrupt_disable(level);

  /* Calculate the value */
  mask = ~(1 << irqLine);
  i8259s_cache &= mask;

  /* Determine which chip and write the value */
  if (irqLine < 8) {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_IMR_IO_PORT, 
      i8259s_cache & 0xff
    );
  } else {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_SLAVE_IMR_IO_PORT, 
      ((i8259s_cache & 0xff00) >> 8)
    );
  }

  rtems_interrupt_enable(level);

  return 0;
} /* mask_irq */


int BSP_irq_enabled_at_i8259s(const rtems_irq_number irqLine)
{
  unsigned short mask;

  ValidateIrqLine( irqLine );

  mask = (1 << irqLine);

  return  (~(i8259s_cache & mask));
}

/*-------------------------------------------------------------------------+
|         Function: BSP_irq_ack_at_i8259s
|      Description: Signal generic End Of Interrupt (EOI) to appropriate PIC.
| Global Variables: None.
|        Arguments: irqLine - number of IRQ line to acknowledge.
|          Returns: Nothing.
+--------------------------------------------------------------------------*/
int BSP_irq_ack_at_i8259s (const rtems_irq_number irqLine)
{
  if (irqLine >= 8) {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_COMMAND_IO_PORT, 
      SLAVE_PIC_EOSI
    );
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_SLAVE_COMMAND_IO_PORT, 
      (PIC_EOSI | (irqLine - 8))
    );
  }else {
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_COMMAND_IO_PORT, 
      (PIC_EOSI | irqLine)
    );
  }

  return 0;

} /* ackIRQ */

void BSP_i8259s_init(void)
{
  volatile uint32_t i;

  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_IMR_IO_PORT, 0xff );
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT,  0xff );
  

  /*
   * init master 8259 interrupt controller
   */

  /* Start init sequence */
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_MASTER_COMMAND_IO_PORT, 
    0x11
  );
  /* Vector base  = 0 */
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_MASTER_IMR_IO_PORT, 
    0x00
  );

  /* edge tiggered, Cascade (slave) on IRQ2 */
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_MASTER_IMR_IO_PORT, 
    0x04
  );

  /* Select 8086 mode */
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_MASTER_IMR_IO_PORT, 
    0x01
  );

  /*
   * init slave  interrupt controller
   */

  /* Start init sequence */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_COMMAND_IO_PORT, 0x11); 

  /* Vector base  = 8 */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0x08);

  /* edge triggered, Cascade (slave) on IRQ2 */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0x02);

  /* Select 8086 mode */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0x01); 

  /* Mask all except cascade */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_IMR_IO_PORT, 0xFB); 

  /* Mask all */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0xFF); 

  /*
   * Enable all interrupts in debug mode.
   */

  if ( DEBUG_8259 ) {
     i8259s_cache = 0x0101;
  }

  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_MASTER_IMR_IO_PORT, 
    i8259s_cache & 0xff
  );
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_SLAVE_IMR_IO_PORT, 
    ((i8259s_cache & 0xff00) >> 8)
  );

  for (i=0; i<10000; i++);
}

#define PCI__GEN(bus, off, num)		(((off)^((bus) << 7))+((num) << 4))
#define PCI_INTR_ACK(bus)		PCI__GEN(bus, 0x0c34, 0)

volatile uint8_t  master;
volatile uint8_t  slave;
volatile uint8_t  temp;

void bsp_show_interrupt_regs(void);
void bsp_show_interrupt_regs() {
  unsigned int sr;
  unsigned int cause;
  unsigned int pending;

  mips_get_sr( sr ); 
  mips_get_cause( cause );
  pending = (cause & sr & 0xff00) >> CAUSE_IPSHIFT;

  master = simple_in_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_COMMAND_IO_PORT);
  slave  = simple_in_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_COMMAND_IO_PORT);

  printk("sr: 0x%x cause: 0x%x pending: 0x%x  master: 0x%x slave: 0x%x\n", 
    sr, cause, pending, master, slave 
  );
}

int BSP_i8259s_int_process()
{
  uint8_t           irq;
  volatile uint32_t temp;

  /* Get the Interrupt */
  irq = simple_in_le32(BSP_PCI_BASE_ADDRESS, PCI_INTR_ACK(0) );

  /*
   *  Mask interrupts
   *   + Mask all except cascade on master
   *   + Mask all on slave
   */
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_IMR_IO_PORT, 0xFB);
  simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0xFF);

  /* Call the Handler */
  temp = irq + MALTA_SB_IRQ_0;
  bsp_interrupt_handler_dispatch( temp );

  /* Reset the interrupt on the 8259 either the master or the slave chip */
  if (irq & 8) {
    temp = simple_in_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT);

    /* Mask all */
    simple_out_8(BSP_8259_BASE_ADDRESS, PIC_SLAVE_IMR_IO_PORT, 0xFF); 
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_SLAVE_COMMAND_IO_PORT, 
      (PIC_EOSI + (irq&7))
    );
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_COMMAND_IO_PORT, 
      SLAVE_PIC_EOSI 
    );
  } else {
    temp = simple_in_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_IMR_IO_PORT);
    /* Mask all except cascade */
    simple_out_8(BSP_8259_BASE_ADDRESS, PIC_MASTER_IMR_IO_PORT, 0xFB);
    simple_out_8(
      BSP_8259_BASE_ADDRESS, 
      PIC_MASTER_COMMAND_IO_PORT, 
      (PIC_EOSI+irq)
    );
  }

  /* Restore the interrupts */
  simple_out_8(BSP_8259_BASE_ADDRESS,PIC_MASTER_IMR_IO_PORT,i8259s_cache&0xff);
  simple_out_8(
    BSP_8259_BASE_ADDRESS, 
    PIC_SLAVE_IMR_IO_PORT, 
    ((i8259s_cache & 0xff00) >> 8)
  );

  return 0;
}