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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [peripheral/] [gpio.c] - Blame information for rev 502

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 502 erez
    printf( "RGPIO_INTS   : 0x%08lX\n", gpio->curr.ints );
98 444 erez
  }
99
}
100
 
101
 
102
/* Convert a memory address to a device struct and relative address.
103
 * Return nonzero on success */
104
int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr )
105
{
106
  unsigned i;
107
  *gpio = NULL;
108
 
109 477 erez
  for ( i = 0; i < config.ngpios && *gpio == NULL; ++ i ) {
110 444 erez
    if ( (addr >= gpios[i].baseaddr) && (addr < gpios[i].baseaddr + GPIO_ADDR_SPACE) )
111
      *gpio = &(gpios[i]);
112
  }
113
 
114
  /* verify we found a device */
115
  if ( *gpio == NULL )
116
    return 0;
117
 
118
  /* Verify legal address */
119
  if ( (addr - (*gpio)->baseaddr) % 4 != 0 )
120
    return 0;
121
 
122
  *reladdr = addr - (*gpio)->baseaddr;
123
  return 1;
124
}
125
 
126
 
127
/* Find device by vapi id */
128
struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which )
129
{
130
  unsigned i, j;
131
 
132 477 erez
  for ( i = 0; i < config.ngpios; ++ i )
133
    if ( (id >= gpios[i].base_vapi_id) && (id < gpios[i].base_vapi_id + GPIO_NUM_VAPI_IDS) ) {
134
      *which = id - gpios[i].base_vapi_id;
135
      return &(gpios[i]);
136
    }
137 444 erez
 
138
  return NULL;
139
}
140
 
141
 
142
/* Wishbone read */
143
unsigned long gpio_read32( unsigned long addr )
144
{
145
  struct gpio_device *gpio;
146
  if ( !gpio_find_device( addr, &gpio, &addr ) )        {
147 483 erez
    debug( 2, "gpio_read32( 0x%08lX ): Not in registered range(s)\n", addr );
148 444 erez
    return 0;
149
  }
150
 
151
  switch( addr ) {
152 502 erez
  case RGPIO_IN: return gpio->curr.in | gpio->curr.out;
153 444 erez
  case RGPIO_OUT: return gpio->curr.out;
154
  case RGPIO_OE: return gpio->curr.oe;
155
  case RGPIO_INTE: return gpio->curr.inte;
156
  case RGPIO_PTRIG: return gpio->curr.ptrig;
157
  case RGPIO_AUX: return gpio->curr.aux;
158
  case RGPIO_CTRL: return gpio->curr.ctrl;
159 502 erez
  case RGPIO_INTS: return gpio->curr.ints;
160 444 erez
  }
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 502 erez
  case RGPIO_INTS: gpio->next.ints = value; break;
182 444 erez
  }
183
}
184
 
185
 
186
/* Input from "outside world" */
187
void gpio_vapi_read( unsigned long id, unsigned long data )
188
{
189
  unsigned which;
190
  struct gpio_device *gpio = gpio_find_vapi_device( id, &which );
191
 
192 477 erez
  debug( 5, "GPIO: id %08x, data %08x\n", id, data );
193 444 erez
 
194
  if ( !gpio ) {
195
    debug( 1, "GPIO: VAPI ID %08x is not ours!\n", id );
196
    return;
197
  }
198
 
199
  switch( which ) {
200 477 erez
  case GPIO_VAPI_DATA:
201 483 erez
    debug( 4, "GPIO: Next input from VAPI = 0x%08x (RGPIO_OE = 0x%08x)\n", data, gpio->next.oe );
202 477 erez
    gpio->next.in = data;
203
    break;
204
  case GPIO_VAPI_AUX:
205
    gpio->auxiliary_inputs = data;
206
    break;
207
  case GPIO_VAPI_RGPIO_OE:
208
    gpio->next.oe = data;
209
    break;
210
  case GPIO_VAPI_RGPIO_INTE:
211
    gpio->next.inte = data;
212
    break;
213
  case GPIO_VAPI_RGPIO_PTRIG:
214
    gpio->next.ptrig = data;
215
    break;
216
  case GPIO_VAPI_RGPIO_AUX:
217
    gpio->next.aux = data;
218
    break;
219
  case GPIO_VAPI_RGPIO_CTRL:
220
    gpio->next.ctrl = data;
221
    break;
222
  case GPIO_VAPI_CLOCK:
223
    gpio_external_clock( data );
224
    break;
225 444 erez
  }
226
}
227
 
228
/* System Clock. */
229
void gpio_clock( void )
230
{
231
  unsigned i;
232
 
233 477 erez
  for ( i = 0; i < config.ngpios; ++ i )
234 444 erez
    if ( !(gpios[i].curr.ctrl & RGPIO_CTRL_ECLK) )
235
      gpio_device_clock( &(gpios[i]) );
236
}
237
 
238
/* External Clock. */
239
void gpio_external_clock( unsigned long value )
240
{
241
  unsigned i;
242
 
243 477 erez
  /* "Normalize" clock value */
244 444 erez
  value = (value != 0);
245
 
246 477 erez
  for ( i = 0; i < config.ngpios; ++ i ) {
247 444 erez
    struct gpio_device *gpio = &(gpios[i]);
248
 
249 477 erez
    int use_external_clock = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
250
    int negative_edge = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
251 444 erez
 
252
    gpio->next.external_clock = value;
253
 
254 477 erez
    if ( use_external_clock && (gpio->next.external_clock != gpio->curr.external_clock) && (value != negative_edge) )
255 444 erez
      gpio_device_clock( gpio );
256
  }
257
}
258
 
259 477 erez
 
260 444 erez
/* Clock as handld by one device. */
261
void gpio_device_clock( struct gpio_device *gpio )
262
{
263 445 erez
  /* Calculate new inputs and outputs */
264
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
265
  /* Replace requested output bits with aux input */
266
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
267
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
268
 
269 444 erez
  /* If any outputs changed, notify the world (i.e. vapi) */
270 477 erez
  if ( gpio->next.out != gpio->curr.out ) {
271 483 erez
    debug( 4, "GPIO: New output 0x%08x, RGPIO_OE = 0x%08x\n", gpio->next.out, gpio->next.oe );
272 477 erez
    if ( gpio->base_vapi_id )
273
      vapi_send( gpio->base_vapi_id + GPIO_VAPI_DATA, gpio->next.out );
274
  }
275 444 erez
 
276 445 erez
  /* If any inputs changed and interrupt enabled, generate interrupt */
277 477 erez
  if ( gpio->next.in != gpio->curr.in ) {
278 483 erez
    debug( 4, "GPIO: New input 0x%08x\n", gpio->next.in );
279 477 erez
 
280
    if ( gpio->next.ctrl & RGPIO_CTRL_INTE ) {
281
      unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
282
      unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
283
      unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
284
      unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
285
 
286
      if ( relevant_bits & gpio->next.inte ) {
287 499 erez
        debug( 3, "GPIO: Reporting interrupt %d\n", gpio->irq );
288 477 erez
        report_interrupt( gpio->irq );
289 502 erez
        gpio->next.ctrl |= RGPIO_CTRL_INTS;
290
        gpio->next.ints |= relevant_bits & gpio->next.inte;
291 477 erez
      }
292 444 erez
    }
293
  }
294
 
295
  /* Switch to values for next clock */
296
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
297
}

powered by: WebSVN 2.1.0

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