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 997

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

powered by: WebSVN 2.1.0

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