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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_39/] [or1ksim/] [peripheral/] [gpio.c] - Blame information for rev 445

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
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
56
    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
      gpio->vapi_ids[GPIO_VAPI_DATA] = config.gpios[i].vapi_id;
70
      gpio->vapi_ids[GPIO_VAPI_AUX] = config.gpios[i].aux_vapi_id;
71
      gpio->vapi_ids[GPIO_VAPI_CLOCK] = config.gpios[i].clk_vapi_id;
72
      gpio->vapi_ids[GPIO_VAPI_CONFIG] = config.gpios[i].cfg_vapi_id;
73
      for ( j = 0; j < GPIO_NUM_VAPI_IDS; ++ j )
74
        if ( gpio->vapi_ids[j] )
75
          vapi_install_handler( gpio->vapi_ids[j], gpio_vapi_read );
76
    }
77
  }
78
}
79
 
80
 
81
/* Dump status */
82
void gpio_status( void )
83
{
84
  unsigned i;
85
 
86
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
87
    struct gpio_device *gpio = &(gpios[i]);
88
 
89
    if ( gpio->baseaddr == 0 )
90
      continue;
91
 
92
    printf( "\nGPIO %u at 0x%08X:\n", i, gpio->baseaddr );
93
    printf( "RGPIO_IN     : 0x%08lX\n", gpio->curr.in );
94
    printf( "RGPIO_OUT    : 0x%08lX\n", gpio->curr.out );
95
    printf( "RGPIO_OE     : 0x%08lX\n", gpio->curr.oe );
96
    printf( "RGPIO_INTE   : 0x%08lX\n", gpio->curr.inte );
97
    printf( "RGPIO_PTRIG  : 0x%08lX\n", gpio->curr.ptrig );
98
    printf( "RGPIO_AUX    : 0x%08lX\n", gpio->curr.aux );
99
    printf( "RGPIO_CTRL   : 0x%08lX\n", gpio->curr.ctrl );
100
  }
101
}
102
 
103
 
104
/* Convert a memory address to a device struct and relative address.
105
 * Return nonzero on success */
106
int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr )
107
{
108
  unsigned i;
109
  *gpio = NULL;
110
 
111
  for ( i = 0; i < MAX_GPIOS && *gpio == NULL; ++ i ) {
112
    if ( (addr >= gpios[i].baseaddr) && (addr < gpios[i].baseaddr + GPIO_ADDR_SPACE) )
113
      *gpio = &(gpios[i]);
114
  }
115
 
116
  /* verify we found a device */
117
  if ( *gpio == NULL )
118
    return 0;
119
 
120
  /* Verify legal address */
121
  if ( (addr - (*gpio)->baseaddr) % 4 != 0 )
122
    return 0;
123
 
124
  *reladdr = addr - (*gpio)->baseaddr;
125
  return 1;
126
}
127
 
128
 
129
/* Find device by vapi id */
130
struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which )
131
{
132
  unsigned i, j;
133
 
134
  for ( i = 0; i < MAX_GPIOS; ++ i )
135
    for ( j = 0; j < GPIO_NUM_VAPI_IDS; ++ j )
136
      if ( id == gpios[i].vapi_ids[j] )
137
        return &(gpios[i]);
138
 
139
  return NULL;
140
}
141
 
142
 
143
/* Wishbone read */
144
unsigned long gpio_read32( unsigned long addr )
145
{
146
  struct gpio_device *gpio;
147
  if ( !gpio_find_device( addr, &gpio, &addr ) )        {
148
    printf( "gpio_read32( 0x%08lX ): Not in registered range(s)\n", addr );
149
    return 0;
150
  }
151
 
152
  switch( addr ) {
153 445 erez
  case RGPIO_IN: gpio->next.ctrl &= ~RGPIO_CTRL_INT; 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
    printf( "gpio_write32( 0x%08lX ): Not in registered range(s)\n", addr );
170
    return;
171
  }
172
 
173
  switch( addr ) {
174
  case RGPIO_IN: debug( 3, "GPIO: Cannot write to RGPIO_IN\n" ); break;
175
  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
  debug( 4, "GPIO: id %08x, data %08x\n", id, data );
192
 
193
  if ( !gpio ) {
194
    debug( 1, "GPIO: VAPI ID %08x is not ours!\n", id );
195
    return;
196
  }
197
 
198
  switch( which ) {
199
  case GPIO_VAPI_DATA: gpio->next.in = data; break;
200
  case GPIO_VAPI_AUX: gpio->auxiliary_inputs = data; break;
201
  case GPIO_VAPI_CLOCK: gpio_external_clock( data ); break;
202
  case GPIO_VAPI_CONFIG: /* Currently no configuration commands supported */ break;
203
  }
204
}
205
 
206
/* System Clock. */
207
void gpio_clock( void )
208
{
209
  unsigned i;
210
 
211
  for ( i = 0; i < MAX_GPIOS; ++ i )
212
    if ( !(gpios[i].curr.ctrl & RGPIO_CTRL_ECLK) )
213
      gpio_device_clock( &(gpios[i]) );
214
}
215
 
216
/* External Clock. */
217
void gpio_external_clock( unsigned long value )
218
{
219
  unsigned i;
220
 
221
  value = (value != 0);
222
 
223
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
224
    struct gpio_device *gpio = &(gpios[i]);
225
 
226
    int eclk = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
227
    int nec = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
228
 
229
    gpio->next.external_clock = value;
230
 
231
    if ( eclk && (gpio->next.external_clock != gpio->curr.external_clock) && (value != nec) )
232
      gpio_device_clock( gpio );
233
  }
234
}
235
 
236
/* Clock as handld by one device. */
237
void gpio_device_clock( struct gpio_device *gpio )
238
{
239 445 erez
  /* Calculate new inputs and outputs */
240
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
241
  /* Replace requested output bits with aux input */
242
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
243
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
244
 
245 444 erez
  /* If any outputs changed, notify the world (i.e. vapi) */
246
  if ( gpio->next.out != gpio->curr.out )
247
    if ( gpio->vapi_ids[GPIO_VAPI_DATA] != 0 )
248
      vapi_send( gpio->vapi_ids[GPIO_VAPI_DATA], gpio->next.out );
249
 
250 445 erez
  /* If any inputs changed and interrupt enabled, generate interrupt */
251 444 erez
  if ( (gpio->next.in != gpio->curr.in) && (gpio->next.ctrl & RGPIO_CTRL_INTE) ) {
252
    unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
253
    unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
254
    unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
255
    unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
256
    if ( relevant_bits & gpio->next.inte ) {
257
      report_interrupt( gpio->irq );
258
      gpio->next.ctrl |= RGPIO_CTRL_INT;
259
    }
260
  }
261
 
262
  /* Switch to values for next clock */
263
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
264
}

powered by: WebSVN 2.1.0

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