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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [peripheral/] [gpio.c] - Blame information for rev 1308

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

powered by: WebSVN 2.1.0

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