OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [peripheral/] [gpio.c] - Blame information for rev 1486

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 444 erez
/* gpio.h -- GPIO code simulation
2
   Copyright (C) 2001 Erez Volk, erez@mailandnews.comopencores.org
3
 
4
   This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
*/
20 1308 phoenix
#include <string.h>
21 444 erez
 
22 1350 nogj
#include "config.h"
23
 
24
#ifdef HAVE_INTTYPES_H
25
#include <inttypes.h>
26
#endif
27
 
28
#include "port.h"
29
#include "arch.h"
30 1308 phoenix
#include "abstract.h"
31 444 erez
#include "gpio.h"
32
#include "gpio_i.h"
33
#include "sim-config.h"
34
#include "pic.h"
35
#include "vapi.h"
36 1308 phoenix
#include "debug.h"
37 1374 nogj
#include "sched.h"
38 444 erez
 
39 1366 nogj
static void gpio_vapi_read( unsigned long id, unsigned long data, void *dat );
40 1359 nogj
static uint32_t gpio_read32( oraddr_t addr, void *dat );
41
static void gpio_write32( oraddr_t addr, uint32_t value, void *dat );
42 444 erez
 
43 1374 nogj
static void gpio_external_clock( unsigned long value, struct gpio_device *gpio );
44 444 erez
static void gpio_device_clock( struct gpio_device *gpio );
45
 
46
/* Initialize all parameters and state */
47 1374 nogj
void gpio_reset( void *dat )
48 444 erez
{
49 1374 nogj
  struct gpio_device *gpio = dat;
50 444 erez
 
51 1374 nogj
  if ( gpio->baseaddr != 0 ) {
52
    /* Possibly connect to VAPI */
53
    if ( gpio->base_vapi_id ) {
54
      vapi_install_multi_handler( gpio->base_vapi_id, GPIO_NUM_VAPI_IDS, gpio_vapi_read, dat );
55 444 erez
    }
56
  }
57
}
58
 
59
 
60
/* Dump status */
61 1374 nogj
void gpio_status( void *dat )
62 444 erez
{
63 1374 nogj
  struct gpio_device *gpio = dat;
64 444 erez
 
65 1374 nogj
  if ( gpio->baseaddr == 0 )
66
    return;
67 444 erez
 
68 1374 nogj
  PRINTF( "\nGPIO at 0x%"PRIxADDR":\n", gpio->baseaddr );
69
  PRINTF( "RGPIO_IN     : 0x%08lX\n", gpio->curr.in );
70
  PRINTF( "RGPIO_OUT    : 0x%08lX\n", gpio->curr.out );
71
  PRINTF( "RGPIO_OE     : 0x%08lX\n", gpio->curr.oe );
72
  PRINTF( "RGPIO_INTE   : 0x%08lX\n", gpio->curr.inte );
73
  PRINTF( "RGPIO_PTRIG  : 0x%08lX\n", gpio->curr.ptrig );
74
  PRINTF( "RGPIO_AUX    : 0x%08lX\n", gpio->curr.aux );
75
  PRINTF( "RGPIO_CTRL   : 0x%08lX\n", gpio->curr.ctrl );
76
  PRINTF( "RGPIO_INTS   : 0x%08lX\n", gpio->curr.ints );
77 444 erez
}
78
 
79
 
80
/* Wishbone read */
81 1359 nogj
uint32_t gpio_read32( oraddr_t addr, void *dat )
82 444 erez
{
83 1374 nogj
  struct gpio_device *gpio = dat;
84 444 erez
 
85
  switch( addr ) {
86 502 erez
  case RGPIO_IN: return gpio->curr.in | gpio->curr.out;
87 444 erez
  case RGPIO_OUT: return gpio->curr.out;
88
  case RGPIO_OE: return gpio->curr.oe;
89
  case RGPIO_INTE: return gpio->curr.inte;
90
  case RGPIO_PTRIG: return gpio->curr.ptrig;
91
  case RGPIO_AUX: return gpio->curr.aux;
92
  case RGPIO_CTRL: return gpio->curr.ctrl;
93 502 erez
  case RGPIO_INTS: return gpio->curr.ints;
94 444 erez
  }
95 1374 nogj
 
96
  return 0;
97 444 erez
}
98
 
99
 
100
/* Wishbone write */
101 1359 nogj
void gpio_write32( oraddr_t addr, uint32_t value, void *dat )
102 444 erez
{
103 1374 nogj
  struct gpio_device *gpio = dat;
104 444 erez
 
105
  switch( addr ) {
106 483 erez
  case RGPIO_IN: debug( 5, "GPIO: Cannot write to RGPIO_IN\n" ); break;
107 444 erez
  case RGPIO_OUT: gpio->next.out = value; break;
108
  case RGPIO_OE: gpio->next.oe = value; break;
109
  case RGPIO_INTE: gpio->next.inte = value; break;
110
  case RGPIO_PTRIG: gpio->next.ptrig = value; break;
111
  case RGPIO_AUX: gpio->next.aux = value; break;
112
  case RGPIO_CTRL: gpio->next.ctrl = value; break;
113 502 erez
  case RGPIO_INTS: gpio->next.ints = value; break;
114 444 erez
  }
115
}
116
 
117
 
118
/* Input from "outside world" */
119 1366 nogj
void gpio_vapi_read( unsigned long id, unsigned long data, void *dat )
120 444 erez
{
121
  unsigned which;
122 1374 nogj
  struct gpio_device *gpio = dat;
123 444 erez
 
124 1350 nogj
  debug( 5, "GPIO: id %08lx, data %08lx\n", id, data );
125 444 erez
 
126 1374 nogj
  which = id - gpio->base_vapi_id;
127 444 erez
 
128
  switch( which ) {
129 477 erez
  case GPIO_VAPI_DATA:
130 1350 nogj
    debug( 4, "GPIO: Next input from VAPI = 0x%08lx (RGPIO_OE = 0x%08lx)\n",
131
           data, gpio->next.oe );
132 477 erez
    gpio->next.in = data;
133
    break;
134
  case GPIO_VAPI_AUX:
135
    gpio->auxiliary_inputs = data;
136
    break;
137
  case GPIO_VAPI_RGPIO_OE:
138
    gpio->next.oe = data;
139
    break;
140
  case GPIO_VAPI_RGPIO_INTE:
141
    gpio->next.inte = data;
142
    break;
143
  case GPIO_VAPI_RGPIO_PTRIG:
144
    gpio->next.ptrig = data;
145
    break;
146
  case GPIO_VAPI_RGPIO_AUX:
147
    gpio->next.aux = data;
148
    break;
149
  case GPIO_VAPI_RGPIO_CTRL:
150
    gpio->next.ctrl = data;
151
    break;
152
  case GPIO_VAPI_CLOCK:
153 1374 nogj
    gpio_external_clock( data, gpio );
154 477 erez
    break;
155 444 erez
  }
156
 
157 1374 nogj
  /* Clock the device */
158
  if ( !(gpio->curr.ctrl & RGPIO_CTRL_ECLK) )
159
    gpio_device_clock( gpio );
160 444 erez
}
161
 
162
/* External Clock. */
163 1374 nogj
static void gpio_external_clock( unsigned long value, struct gpio_device *gpio )
164 444 erez
{
165 1374 nogj
  int use_external_clock = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
166
  int negative_edge = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
167 444 erez
 
168 477 erez
  /* "Normalize" clock value */
169 444 erez
  value = (value != 0);
170
 
171 1374 nogj
  gpio->next.external_clock = value;
172 444 erez
 
173 1374 nogj
  if ( use_external_clock && (gpio->next.external_clock != gpio->curr.external_clock) && (value != negative_edge) )
174
    /* Make sure that in vapi_read, we don't clock the device */
175
    if ( gpio->curr.ctrl & RGPIO_CTRL_ECLK )
176 444 erez
      gpio_device_clock( gpio );
177
}
178
 
179 1374 nogj
/* Report an interrupt to the sim */
180
void gpio_do_int( void *dat )
181
{
182
  struct gpio_device *gpio = dat;
183 477 erez
 
184 1374 nogj
  report_interrupt( gpio->irq );
185
}
186
 
187 444 erez
/* Clock as handld by one device. */
188 1374 nogj
static void gpio_device_clock( struct gpio_device *gpio )
189 444 erez
{
190 445 erez
  /* Calculate new inputs and outputs */
191
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
192
  /* Replace requested output bits with aux input */
193
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
194
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
195
 
196 444 erez
  /* If any outputs changed, notify the world (i.e. vapi) */
197 477 erez
  if ( gpio->next.out != gpio->curr.out ) {
198 1350 nogj
    debug( 4, "GPIO: New output 0x%08lx, RGPIO_OE = 0x%08lx\n", gpio->next.out,
199
           gpio->next.oe );
200 477 erez
    if ( gpio->base_vapi_id )
201
      vapi_send( gpio->base_vapi_id + GPIO_VAPI_DATA, gpio->next.out );
202
  }
203 444 erez
 
204 445 erez
  /* If any inputs changed and interrupt enabled, generate interrupt */
205 477 erez
  if ( gpio->next.in != gpio->curr.in ) {
206 1350 nogj
    debug( 4, "GPIO: New input 0x%08lx\n", gpio->next.in );
207 477 erez
 
208
    if ( gpio->next.ctrl & RGPIO_CTRL_INTE ) {
209
      unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
210
      unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
211
      unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
212
      unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
213
 
214
      if ( relevant_bits & gpio->next.inte ) {
215 499 erez
        debug( 3, "GPIO: Reporting interrupt %d\n", gpio->irq );
216 502 erez
        gpio->next.ctrl |= RGPIO_CTRL_INTS;
217
        gpio->next.ints |= relevant_bits & gpio->next.inte;
218 1374 nogj
        /* Since we can't report an interrupt during a readmem/writemem
219
         * schedule the scheduler to do it.  Read the comment above
220
         * report_interrupt in pic/pic.c */
221 1390 nogj
        SCHED_ADD( gpio_do_int, gpio, 1 );
222 477 erez
      }
223 444 erez
    }
224
  }
225
 
226
  /* Switch to values for next clock */
227
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
228
}
229 1358 nogj
 
230
/*---------------------------------------------------[ GPIO configuration ]---*/
231
void gpio_baseaddr(union param_val val, void *dat)
232
{
233 1374 nogj
  struct gpio_device *gpio = dat;
234
  gpio->baseaddr = val.addr_val;
235 1358 nogj
}
236
 
237
void gpio_irq(union param_val val, void *dat)
238
{
239 1374 nogj
  struct gpio_device *gpio = dat;
240
  gpio->irq = val.int_val;
241 1358 nogj
}
242
 
243
void gpio_base_vapi_id(union param_val val, void *dat)
244
{
245 1374 nogj
  struct gpio_device *gpio = dat;
246
  gpio->base_vapi_id = val.int_val;
247 1358 nogj
}
248
 
249 1461 nogj
void gpio_enabled(union param_val val, void *dat)
250
{
251
  struct gpio_device *gpio = dat;
252
  gpio->enabled = val.int_val;
253
}
254
 
255 1374 nogj
void *gpio_sec_start(void)
256
{
257
  struct gpio_device *new = malloc(sizeof(struct gpio_device));
258
 
259
  if(!new) {
260 1383 nogj
    fprintf(stderr, "Peripheral gpio: Run out of memory\n");
261 1374 nogj
    exit(-1);
262
  }
263
 
264
  new->auxiliary_inputs = 0;
265
  memset(&new->curr, 0, sizeof(new->curr));
266
  memset(&new->next, 0, sizeof(new->next));
267
 
268 1461 nogj
  new->enabled = 1;
269
 
270 1374 nogj
  return new;
271
}
272
 
273
void gpio_sec_end(void *dat)
274
{
275
  struct gpio_device *gpio = dat;
276 1486 nogj
  struct mem_ops ops;
277 1374 nogj
 
278 1461 nogj
  if(!gpio->enabled) {
279
    free(dat);
280
    return;
281
  }
282
 
283 1486 nogj
  memset(&ops, 0, sizeof(struct mem_ops));
284
 
285
  ops.readfunc32 = gpio_read32;
286
  ops.writefunc32 = gpio_write32;
287
  ops.write_dat32 = dat;
288
  ops.read_dat32 = dat;
289
 
290
  /* FIXME: Correct delays? */
291
  ops.delayr = 2;
292
  ops.delayw = 2;
293
 
294 1374 nogj
  /* Register memory range */
295 1486 nogj
  reg_mem_area( gpio->baseaddr, GPIO_ADDR_SPACE, 0, &ops );
296 1374 nogj
 
297
  reg_sim_reset(gpio_reset, dat);
298
  reg_sim_stat(gpio_status, dat);
299
}
300
 
301 1358 nogj
void reg_gpio_sec(void)
302
{
303 1383 nogj
  struct config_section *sec = reg_config_sec("gpio", gpio_sec_start, gpio_sec_end);
304 1358 nogj
 
305 1461 nogj
  reg_config_param(sec, "enabled", paramt_int, gpio_enabled);
306 1358 nogj
  reg_config_param(sec, "baseaddr", paramt_addr, gpio_baseaddr);
307
  reg_config_param(sec, "irq", paramt_int, gpio_irq);
308
  reg_config_param(sec, "base_vapi_id", paramt_int, gpio_base_vapi_id);
309
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.