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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [peripheral/] [gpio.c] - Blame information for rev 499

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
 
21
#include "gpio.h"
22
#include "gpio_i.h"
23
#include "sim-config.h"
24
#include "pic.h"
25
#include "vapi.h"
26
 
27
static struct gpio_device gpios[MAX_GPIOS];
28
 
29
static void gpio_vapi_read( unsigned long id, unsigned long data );
30
static unsigned long gpio_read32( unsigned long addr );
31
static void gpio_write32( unsigned long addr, unsigned long value );
32
 
33
static void gpio_external_clock( unsigned long value );
34
static void gpio_device_clock( struct gpio_device *gpio );
35
static int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr );
36
static struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which_vapi );
37
 
38
/* Initialize all parameters and state */
39
void gpio_reset( void )
40
{
41
  static int first_time = 1;
42
  unsigned i, j;
43
 
44
  if ( !config.ngpios )
45
    config.gpios_enabled = 0;
46
  if ( !config.gpios_enabled )
47
    return;
48
 
49
  if ( first_time ) {
50
    memset( gpios, 0, sizeof(gpios) );
51
    first_time = 0;
52
  }
53
 
54
 
55 477 erez
  for ( i = 0; i < config.ngpios; ++ i ) {
56 444 erez
    struct gpio_device *gpio = &(gpios[i]);
57
 
58
    gpio->gpio_number = i;
59
    gpio->baseaddr = config.gpios[i].baseaddr;
60
 
61
    if ( gpio->baseaddr != 0 ) {
62
      /* Get IRQ */
63
      gpio->irq = config.gpios[i].irq;
64
 
65
      /* Register memory range */
66
      register_memoryarea( gpio->baseaddr, GPIO_ADDR_SPACE, 4, gpio_read32, gpio_write32 );
67
 
68
      /* Possibly connect to VAPI */
69 477 erez
      if ( config.gpios[i].base_vapi_id ) {
70
        gpio->base_vapi_id = config.gpios[i].base_vapi_id;
71
        vapi_install_multi_handler( gpio->base_vapi_id, GPIO_NUM_VAPI_IDS, gpio_vapi_read );
72
      }
73 444 erez
    }
74
  }
75
}
76
 
77
 
78
/* Dump status */
79
void gpio_status( void )
80
{
81
  unsigned i;
82
 
83 477 erez
  for ( i = 0; i < config.ngpios; ++ i ) {
84 444 erez
    struct gpio_device *gpio = &(gpios[i]);
85
 
86
    if ( gpio->baseaddr == 0 )
87
      continue;
88
 
89
    printf( "\nGPIO %u at 0x%08X:\n", i, gpio->baseaddr );
90
    printf( "RGPIO_IN     : 0x%08lX\n", gpio->curr.in );
91
    printf( "RGPIO_OUT    : 0x%08lX\n", gpio->curr.out );
92
    printf( "RGPIO_OE     : 0x%08lX\n", gpio->curr.oe );
93
    printf( "RGPIO_INTE   : 0x%08lX\n", gpio->curr.inte );
94
    printf( "RGPIO_PTRIG  : 0x%08lX\n", gpio->curr.ptrig );
95
    printf( "RGPIO_AUX    : 0x%08lX\n", gpio->curr.aux );
96
    printf( "RGPIO_CTRL   : 0x%08lX\n", gpio->curr.ctrl );
97
  }
98
}
99
 
100
 
101
/* Convert a memory address to a device struct and relative address.
102
 * Return nonzero on success */
103
int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr )
104
{
105
  unsigned i;
106
  *gpio = NULL;
107
 
108 477 erez
  for ( i = 0; i < config.ngpios && *gpio == NULL; ++ i ) {
109 444 erez
    if ( (addr >= gpios[i].baseaddr) && (addr < gpios[i].baseaddr + GPIO_ADDR_SPACE) )
110
      *gpio = &(gpios[i]);
111
  }
112
 
113
  /* verify we found a device */
114
  if ( *gpio == NULL )
115
    return 0;
116
 
117
  /* Verify legal address */
118
  if ( (addr - (*gpio)->baseaddr) % 4 != 0 )
119
    return 0;
120
 
121
  *reladdr = addr - (*gpio)->baseaddr;
122
  return 1;
123
}
124
 
125
 
126
/* Find device by vapi id */
127
struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which )
128
{
129
  unsigned i, j;
130
 
131 477 erez
  for ( i = 0; i < config.ngpios; ++ i )
132
    if ( (id >= gpios[i].base_vapi_id) && (id < gpios[i].base_vapi_id + GPIO_NUM_VAPI_IDS) ) {
133
      *which = id - gpios[i].base_vapi_id;
134
      return &(gpios[i]);
135
    }
136 444 erez
 
137
  return NULL;
138
}
139
 
140
 
141
/* Wishbone read */
142
unsigned long gpio_read32( unsigned long addr )
143
{
144
  struct gpio_device *gpio;
145
  if ( !gpio_find_device( addr, &gpio, &addr ) )        {
146 483 erez
    debug( 2, "gpio_read32( 0x%08lX ): Not in registered range(s)\n", addr );
147 444 erez
    return 0;
148
  }
149
 
150
  switch( addr ) {
151 477 erez
  case RGPIO_IN:
152
    gpio->next.ctrl &= ~RGPIO_CTRL_INT;
153
    return gpio->curr.in | gpio->curr.out;
154 444 erez
  case RGPIO_OUT: return gpio->curr.out;
155
  case RGPIO_OE: return gpio->curr.oe;
156
  case RGPIO_INTE: return gpio->curr.inte;
157
  case RGPIO_PTRIG: return gpio->curr.ptrig;
158
  case RGPIO_AUX: return gpio->curr.aux;
159
  case RGPIO_CTRL: return gpio->curr.ctrl;
160
  }
161
}
162
 
163
 
164
/* Wishbone write */
165
void gpio_write32( unsigned long addr, unsigned long value )
166
{
167
  struct gpio_device *gpio;
168
  if ( !gpio_find_device( addr, &gpio, &addr ) )        {
169 483 erez
    debug( 2, "gpio_write32( 0x%08lX ): Not in registered range(s)\n", addr );
170 444 erez
    return;
171
  }
172
 
173
  switch( addr ) {
174 483 erez
  case RGPIO_IN: debug( 5, "GPIO: Cannot write to RGPIO_IN\n" ); break;
175 444 erez
  case RGPIO_OUT: gpio->next.out = value; break;
176
  case RGPIO_OE: gpio->next.oe = value; break;
177
  case RGPIO_INTE: gpio->next.inte = value; break;
178
  case RGPIO_PTRIG: gpio->next.ptrig = value; break;
179
  case RGPIO_AUX: gpio->next.aux = value; break;
180
  case RGPIO_CTRL: gpio->next.ctrl = value; break;
181
  }
182
}
183
 
184
 
185
/* Input from "outside world" */
186
void gpio_vapi_read( unsigned long id, unsigned long data )
187
{
188
  unsigned which;
189
  struct gpio_device *gpio = gpio_find_vapi_device( id, &which );
190
 
191 477 erez
  debug( 5, "GPIO: id %08x, data %08x\n", id, data );
192 444 erez
 
193
  if ( !gpio ) {
194
    debug( 1, "GPIO: VAPI ID %08x is not ours!\n", id );
195
    return;
196
  }
197
 
198
  switch( which ) {
199 477 erez
  case GPIO_VAPI_DATA:
200 483 erez
    debug( 4, "GPIO: Next input from VAPI = 0x%08x (RGPIO_OE = 0x%08x)\n", data, gpio->next.oe );
201 477 erez
    gpio->next.in = data;
202
    break;
203
  case GPIO_VAPI_AUX:
204
    gpio->auxiliary_inputs = data;
205
    break;
206
  case GPIO_VAPI_RGPIO_OE:
207
    gpio->next.oe = data;
208
    break;
209
  case GPIO_VAPI_RGPIO_INTE:
210
    gpio->next.inte = data;
211
    break;
212
  case GPIO_VAPI_RGPIO_PTRIG:
213
    gpio->next.ptrig = data;
214
    break;
215
  case GPIO_VAPI_RGPIO_AUX:
216
    gpio->next.aux = data;
217
    break;
218
  case GPIO_VAPI_RGPIO_CTRL:
219
    gpio->next.ctrl = data;
220
    break;
221
  case GPIO_VAPI_CLOCK:
222
    gpio_external_clock( data );
223
    break;
224 444 erez
  }
225
}
226
 
227
/* System Clock. */
228
void gpio_clock( void )
229
{
230
  unsigned i;
231
 
232 477 erez
  for ( i = 0; i < config.ngpios; ++ i )
233 444 erez
    if ( !(gpios[i].curr.ctrl & RGPIO_CTRL_ECLK) )
234
      gpio_device_clock( &(gpios[i]) );
235
}
236
 
237
/* External Clock. */
238
void gpio_external_clock( unsigned long value )
239
{
240
  unsigned i;
241
 
242 477 erez
  /* "Normalize" clock value */
243 444 erez
  value = (value != 0);
244
 
245 477 erez
  for ( i = 0; i < config.ngpios; ++ i ) {
246 444 erez
    struct gpio_device *gpio = &(gpios[i]);
247
 
248 477 erez
    int use_external_clock = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
249
    int negative_edge = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
250 444 erez
 
251
    gpio->next.external_clock = value;
252
 
253 477 erez
    if ( use_external_clock && (gpio->next.external_clock != gpio->curr.external_clock) && (value != negative_edge) )
254 444 erez
      gpio_device_clock( gpio );
255
  }
256
}
257
 
258 477 erez
 
259 444 erez
/* Clock as handld by one device. */
260
void gpio_device_clock( struct gpio_device *gpio )
261
{
262 445 erez
  /* Calculate new inputs and outputs */
263
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
264
  /* Replace requested output bits with aux input */
265
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
266
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
267
 
268 444 erez
  /* If any outputs changed, notify the world (i.e. vapi) */
269 477 erez
  if ( gpio->next.out != gpio->curr.out ) {
270 483 erez
    debug( 4, "GPIO: New output 0x%08x, RGPIO_OE = 0x%08x\n", gpio->next.out, gpio->next.oe );
271 477 erez
    if ( gpio->base_vapi_id )
272
      vapi_send( gpio->base_vapi_id + GPIO_VAPI_DATA, gpio->next.out );
273
  }
274 444 erez
 
275 445 erez
  /* If any inputs changed and interrupt enabled, generate interrupt */
276 477 erez
  if ( gpio->next.in != gpio->curr.in ) {
277 483 erez
    debug( 4, "GPIO: New input 0x%08x\n", gpio->next.in );
278 477 erez
 
279
    if ( gpio->next.ctrl & RGPIO_CTRL_INTE ) {
280
      unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
281
      unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
282
      unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
283
      unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
284
 
285
      if ( relevant_bits & gpio->next.inte ) {
286 499 erez
        debug( 3, "GPIO: Reporting interrupt %d\n", gpio->irq );
287 477 erez
        report_interrupt( gpio->irq );
288
        gpio->next.ctrl |= RGPIO_CTRL_INT;
289
      }
290 444 erez
    }
291
  }
292
 
293
  /* Switch to values for next clock */
294
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
295
}

powered by: WebSVN 2.1.0

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