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 1489

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 1489 nogj
DEFAULT_DEBUG_CHANNEL(gpio);
40
 
41 1366 nogj
static void gpio_vapi_read( unsigned long id, unsigned long data, void *dat );
42 1359 nogj
static uint32_t gpio_read32( oraddr_t addr, void *dat );
43
static void gpio_write32( oraddr_t addr, uint32_t value, void *dat );
44 444 erez
 
45 1374 nogj
static void gpio_external_clock( unsigned long value, struct gpio_device *gpio );
46 444 erez
static void gpio_device_clock( struct gpio_device *gpio );
47
 
48
/* Initialize all parameters and state */
49 1374 nogj
void gpio_reset( void *dat )
50 444 erez
{
51 1374 nogj
  struct gpio_device *gpio = dat;
52 444 erez
 
53 1374 nogj
  if ( gpio->baseaddr != 0 ) {
54
    /* Possibly connect to VAPI */
55
    if ( gpio->base_vapi_id ) {
56
      vapi_install_multi_handler( gpio->base_vapi_id, GPIO_NUM_VAPI_IDS, gpio_vapi_read, dat );
57 444 erez
    }
58
  }
59
}
60
 
61
 
62
/* Dump status */
63 1374 nogj
void gpio_status( void *dat )
64 444 erez
{
65 1374 nogj
  struct gpio_device *gpio = dat;
66 444 erez
 
67 1374 nogj
  if ( gpio->baseaddr == 0 )
68
    return;
69 444 erez
 
70 1374 nogj
  PRINTF( "\nGPIO at 0x%"PRIxADDR":\n", gpio->baseaddr );
71
  PRINTF( "RGPIO_IN     : 0x%08lX\n", gpio->curr.in );
72
  PRINTF( "RGPIO_OUT    : 0x%08lX\n", gpio->curr.out );
73
  PRINTF( "RGPIO_OE     : 0x%08lX\n", gpio->curr.oe );
74
  PRINTF( "RGPIO_INTE   : 0x%08lX\n", gpio->curr.inte );
75
  PRINTF( "RGPIO_PTRIG  : 0x%08lX\n", gpio->curr.ptrig );
76
  PRINTF( "RGPIO_AUX    : 0x%08lX\n", gpio->curr.aux );
77
  PRINTF( "RGPIO_CTRL   : 0x%08lX\n", gpio->curr.ctrl );
78
  PRINTF( "RGPIO_INTS   : 0x%08lX\n", gpio->curr.ints );
79 444 erez
}
80
 
81
 
82
/* Wishbone read */
83 1359 nogj
uint32_t gpio_read32( oraddr_t addr, void *dat )
84 444 erez
{
85 1374 nogj
  struct gpio_device *gpio = dat;
86 444 erez
 
87
  switch( addr ) {
88 502 erez
  case RGPIO_IN: return gpio->curr.in | gpio->curr.out;
89 444 erez
  case RGPIO_OUT: return gpio->curr.out;
90
  case RGPIO_OE: return gpio->curr.oe;
91
  case RGPIO_INTE: return gpio->curr.inte;
92
  case RGPIO_PTRIG: return gpio->curr.ptrig;
93
  case RGPIO_AUX: return gpio->curr.aux;
94
  case RGPIO_CTRL: return gpio->curr.ctrl;
95 502 erez
  case RGPIO_INTS: return gpio->curr.ints;
96 444 erez
  }
97 1374 nogj
 
98
  return 0;
99 444 erez
}
100
 
101
 
102
/* Wishbone write */
103 1359 nogj
void gpio_write32( oraddr_t addr, uint32_t value, void *dat )
104 444 erez
{
105 1374 nogj
  struct gpio_device *gpio = dat;
106 444 erez
 
107
  switch( addr ) {
108 1489 nogj
  case RGPIO_IN: TRACE( "GPIO: Cannot write to RGPIO_IN\n" ); break;
109 444 erez
  case RGPIO_OUT: gpio->next.out = value; break;
110
  case RGPIO_OE: gpio->next.oe = value; break;
111
  case RGPIO_INTE: gpio->next.inte = value; break;
112
  case RGPIO_PTRIG: gpio->next.ptrig = value; break;
113
  case RGPIO_AUX: gpio->next.aux = value; break;
114
  case RGPIO_CTRL: gpio->next.ctrl = value; break;
115 502 erez
  case RGPIO_INTS: gpio->next.ints = value; break;
116 444 erez
  }
117
}
118
 
119
 
120
/* Input from "outside world" */
121 1366 nogj
void gpio_vapi_read( unsigned long id, unsigned long data, void *dat )
122 444 erez
{
123
  unsigned which;
124 1374 nogj
  struct gpio_device *gpio = dat;
125 444 erez
 
126 1489 nogj
  TRACE( "GPIO: id %08lx, data %08lx\n", id, data );
127 444 erez
 
128 1374 nogj
  which = id - gpio->base_vapi_id;
129 444 erez
 
130
  switch( which ) {
131 477 erez
  case GPIO_VAPI_DATA:
132 1489 nogj
    TRACE( "GPIO: Next input from VAPI = 0x%08lx (RGPIO_OE = 0x%08lx)\n",
133 1350 nogj
           data, gpio->next.oe );
134 477 erez
    gpio->next.in = data;
135
    break;
136
  case GPIO_VAPI_AUX:
137
    gpio->auxiliary_inputs = data;
138
    break;
139
  case GPIO_VAPI_RGPIO_OE:
140
    gpio->next.oe = data;
141
    break;
142
  case GPIO_VAPI_RGPIO_INTE:
143
    gpio->next.inte = data;
144
    break;
145
  case GPIO_VAPI_RGPIO_PTRIG:
146
    gpio->next.ptrig = data;
147
    break;
148
  case GPIO_VAPI_RGPIO_AUX:
149
    gpio->next.aux = data;
150
    break;
151
  case GPIO_VAPI_RGPIO_CTRL:
152
    gpio->next.ctrl = data;
153
    break;
154
  case GPIO_VAPI_CLOCK:
155 1374 nogj
    gpio_external_clock( data, gpio );
156 477 erez
    break;
157 444 erez
  }
158
 
159 1374 nogj
  /* Clock the device */
160
  if ( !(gpio->curr.ctrl & RGPIO_CTRL_ECLK) )
161
    gpio_device_clock( gpio );
162 444 erez
}
163
 
164
/* External Clock. */
165 1374 nogj
static void gpio_external_clock( unsigned long value, struct gpio_device *gpio )
166 444 erez
{
167 1374 nogj
  int use_external_clock = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
168
  int negative_edge = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
169 444 erez
 
170 477 erez
  /* "Normalize" clock value */
171 444 erez
  value = (value != 0);
172
 
173 1374 nogj
  gpio->next.external_clock = value;
174 444 erez
 
175 1374 nogj
  if ( use_external_clock && (gpio->next.external_clock != gpio->curr.external_clock) && (value != negative_edge) )
176
    /* Make sure that in vapi_read, we don't clock the device */
177
    if ( gpio->curr.ctrl & RGPIO_CTRL_ECLK )
178 444 erez
      gpio_device_clock( gpio );
179
}
180
 
181 1374 nogj
/* Report an interrupt to the sim */
182
void gpio_do_int( void *dat )
183
{
184
  struct gpio_device *gpio = dat;
185 477 erez
 
186 1374 nogj
  report_interrupt( gpio->irq );
187
}
188
 
189 444 erez
/* Clock as handld by one device. */
190 1374 nogj
static void gpio_device_clock( struct gpio_device *gpio )
191 444 erez
{
192 445 erez
  /* Calculate new inputs and outputs */
193
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
194
  /* Replace requested output bits with aux input */
195
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
196
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
197
 
198 444 erez
  /* If any outputs changed, notify the world (i.e. vapi) */
199 477 erez
  if ( gpio->next.out != gpio->curr.out ) {
200 1489 nogj
    TRACE( "GPIO: New output 0x%08lx, RGPIO_OE = 0x%08lx\n", gpio->next.out,
201 1350 nogj
           gpio->next.oe );
202 477 erez
    if ( gpio->base_vapi_id )
203
      vapi_send( gpio->base_vapi_id + GPIO_VAPI_DATA, gpio->next.out );
204
  }
205 444 erez
 
206 445 erez
  /* If any inputs changed and interrupt enabled, generate interrupt */
207 477 erez
  if ( gpio->next.in != gpio->curr.in ) {
208 1489 nogj
    TRACE( "GPIO: New input 0x%08lx\n", gpio->next.in );
209 477 erez
 
210
    if ( gpio->next.ctrl & RGPIO_CTRL_INTE ) {
211
      unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
212
      unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
213
      unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
214
      unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
215
 
216
      if ( relevant_bits & gpio->next.inte ) {
217 1489 nogj
        TRACE( "GPIO: Reporting interrupt %d\n", gpio->irq );
218 502 erez
        gpio->next.ctrl |= RGPIO_CTRL_INTS;
219
        gpio->next.ints |= relevant_bits & gpio->next.inte;
220 1374 nogj
        /* Since we can't report an interrupt during a readmem/writemem
221
         * schedule the scheduler to do it.  Read the comment above
222
         * report_interrupt in pic/pic.c */
223 1390 nogj
        SCHED_ADD( gpio_do_int, gpio, 1 );
224 477 erez
      }
225 444 erez
    }
226
  }
227
 
228
  /* Switch to values for next clock */
229
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
230
}
231 1358 nogj
 
232
/*---------------------------------------------------[ GPIO configuration ]---*/
233
void gpio_baseaddr(union param_val val, void *dat)
234
{
235 1374 nogj
  struct gpio_device *gpio = dat;
236
  gpio->baseaddr = val.addr_val;
237 1358 nogj
}
238
 
239
void gpio_irq(union param_val val, void *dat)
240
{
241 1374 nogj
  struct gpio_device *gpio = dat;
242
  gpio->irq = val.int_val;
243 1358 nogj
}
244
 
245
void gpio_base_vapi_id(union param_val val, void *dat)
246
{
247 1374 nogj
  struct gpio_device *gpio = dat;
248
  gpio->base_vapi_id = val.int_val;
249 1358 nogj
}
250
 
251 1461 nogj
void gpio_enabled(union param_val val, void *dat)
252
{
253
  struct gpio_device *gpio = dat;
254
  gpio->enabled = val.int_val;
255
}
256
 
257 1374 nogj
void *gpio_sec_start(void)
258
{
259
  struct gpio_device *new = malloc(sizeof(struct gpio_device));
260
 
261
  if(!new) {
262 1383 nogj
    fprintf(stderr, "Peripheral gpio: Run out of memory\n");
263 1374 nogj
    exit(-1);
264
  }
265
 
266
  new->auxiliary_inputs = 0;
267
  memset(&new->curr, 0, sizeof(new->curr));
268
  memset(&new->next, 0, sizeof(new->next));
269
 
270 1461 nogj
  new->enabled = 1;
271
 
272 1374 nogj
  return new;
273
}
274
 
275
void gpio_sec_end(void *dat)
276
{
277
  struct gpio_device *gpio = dat;
278 1486 nogj
  struct mem_ops ops;
279 1374 nogj
 
280 1461 nogj
  if(!gpio->enabled) {
281
    free(dat);
282
    return;
283
  }
284
 
285 1486 nogj
  memset(&ops, 0, sizeof(struct mem_ops));
286
 
287
  ops.readfunc32 = gpio_read32;
288
  ops.writefunc32 = gpio_write32;
289
  ops.write_dat32 = dat;
290
  ops.read_dat32 = dat;
291
 
292
  /* FIXME: Correct delays? */
293
  ops.delayr = 2;
294
  ops.delayw = 2;
295
 
296 1374 nogj
  /* Register memory range */
297 1486 nogj
  reg_mem_area( gpio->baseaddr, GPIO_ADDR_SPACE, 0, &ops );
298 1374 nogj
 
299
  reg_sim_reset(gpio_reset, dat);
300
  reg_sim_stat(gpio_status, dat);
301
}
302
 
303 1358 nogj
void reg_gpio_sec(void)
304
{
305 1383 nogj
  struct config_section *sec = reg_config_sec("gpio", gpio_sec_start, gpio_sec_end);
306 1358 nogj
 
307 1461 nogj
  reg_config_param(sec, "enabled", paramt_int, gpio_enabled);
308 1358 nogj
  reg_config_param(sec, "baseaddr", paramt_addr, gpio_baseaddr);
309
  reg_config_param(sec, "irq", paramt_int, gpio_irq);
310
  reg_config_param(sec, "base_vapi_id", paramt_int, gpio_base_vapi_id);
311
}

powered by: WebSVN 2.1.0

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