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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [peripheral/] [gpio.c] - Rev 445

Go to most recent revision | Compare with Previous | Blame | View Log

/* gpio.h -- GPIO code simulation
   Copyright (C) 2001 Erez Volk, erez@mailandnews.comopencores.org
 
   This file is part of OpenRISC 1000 Architectural Simulator.
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
 
#include "gpio.h"
#include "gpio_i.h"
#include "sim-config.h"
#include "pic.h"
#include "vapi.h"
 
static struct gpio_device gpios[MAX_GPIOS];
 
static void gpio_vapi_read( unsigned long id, unsigned long data );
static unsigned long gpio_read32( unsigned long addr );
static void gpio_write32( unsigned long addr, unsigned long value );
 
static void gpio_external_clock( unsigned long value );
static void gpio_device_clock( struct gpio_device *gpio );
static int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr );
static struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which_vapi );
 
/* Initialize all parameters and state */
void gpio_reset( void )
{
  static int first_time = 1;
  unsigned i, j;
 
  if ( !config.ngpios )
    config.gpios_enabled = 0;
  if ( !config.gpios_enabled )
    return;
 
  if ( first_time ) {
    memset( gpios, 0, sizeof(gpios) );
    first_time = 0;
  }
 
 
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
    struct gpio_device *gpio = &(gpios[i]);
 
    gpio->gpio_number = i;
    gpio->baseaddr = config.gpios[i].baseaddr;
 
    if ( gpio->baseaddr != 0 ) {
      /* Get IRQ */
      gpio->irq = config.gpios[i].irq;
 
      /* Register memory range */
      register_memoryarea( gpio->baseaddr, GPIO_ADDR_SPACE, 4, gpio_read32, gpio_write32 );
 
      /* Possibly connect to VAPI */
      gpio->vapi_ids[GPIO_VAPI_DATA] = config.gpios[i].vapi_id;
      gpio->vapi_ids[GPIO_VAPI_AUX] = config.gpios[i].aux_vapi_id;
      gpio->vapi_ids[GPIO_VAPI_CLOCK] = config.gpios[i].clk_vapi_id;
      gpio->vapi_ids[GPIO_VAPI_CONFIG] = config.gpios[i].cfg_vapi_id;
      for ( j = 0; j < GPIO_NUM_VAPI_IDS; ++ j )
	if ( gpio->vapi_ids[j] )
	  vapi_install_handler( gpio->vapi_ids[j], gpio_vapi_read );
    }
  }
}
 
 
/* Dump status */
void gpio_status( void )
{
  unsigned i;
 
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
    struct gpio_device *gpio = &(gpios[i]);
 
    if ( gpio->baseaddr == 0 )
      continue;
 
    printf( "\nGPIO %u at 0x%08X:\n", i, gpio->baseaddr );
    printf( "RGPIO_IN     : 0x%08lX\n", gpio->curr.in );
    printf( "RGPIO_OUT    : 0x%08lX\n", gpio->curr.out );
    printf( "RGPIO_OE     : 0x%08lX\n", gpio->curr.oe );
    printf( "RGPIO_INTE   : 0x%08lX\n", gpio->curr.inte );
    printf( "RGPIO_PTRIG  : 0x%08lX\n", gpio->curr.ptrig );
    printf( "RGPIO_AUX    : 0x%08lX\n", gpio->curr.aux );
    printf( "RGPIO_CTRL   : 0x%08lX\n", gpio->curr.ctrl );
  }
}
 
 
/* Convert a memory address to a device struct and relative address.
 * Return nonzero on success */
int gpio_find_device( unsigned long addr, struct gpio_device **gpio, unsigned long *reladdr )
{
  unsigned i;
  *gpio = NULL;
 
  for ( i = 0; i < MAX_GPIOS && *gpio == NULL; ++ i ) {
    if ( (addr >= gpios[i].baseaddr) && (addr < gpios[i].baseaddr + GPIO_ADDR_SPACE) )
      *gpio = &(gpios[i]);
  }
 
  /* verify we found a device */
  if ( *gpio == NULL )
    return 0;
 
  /* Verify legal address */
  if ( (addr - (*gpio)->baseaddr) % 4 != 0 )
    return 0;
 
  *reladdr = addr - (*gpio)->baseaddr;
  return 1;
}
 
 
/* Find device by vapi id */
struct gpio_device *gpio_find_vapi_device( unsigned long id, unsigned *which )
{
  unsigned i, j;
 
  for ( i = 0; i < MAX_GPIOS; ++ i )
    for ( j = 0; j < GPIO_NUM_VAPI_IDS; ++ j )
      if ( id == gpios[i].vapi_ids[j] )
	return &(gpios[i]);
 
  return NULL;
}
 
 
/* Wishbone read */
unsigned long gpio_read32( unsigned long addr )
{
  struct gpio_device *gpio;
  if ( !gpio_find_device( addr, &gpio, &addr ) )	{
    printf( "gpio_read32( 0x%08lX ): Not in registered range(s)\n", addr );
    return 0;
  }		
 
  switch( addr ) {
  case RGPIO_IN: gpio->next.ctrl &= ~RGPIO_CTRL_INT; return gpio->curr.in | gpio->curr.out;
  case RGPIO_OUT: return gpio->curr.out;
  case RGPIO_OE: return gpio->curr.oe;
  case RGPIO_INTE: return gpio->curr.inte;
  case RGPIO_PTRIG: return gpio->curr.ptrig;
  case RGPIO_AUX: return gpio->curr.aux;
  case RGPIO_CTRL: return gpio->curr.ctrl;
  }
}
 
 
/* Wishbone write */
void gpio_write32( unsigned long addr, unsigned long value )
{
  struct gpio_device *gpio;
  if ( !gpio_find_device( addr, &gpio, &addr ) )	{
    printf( "gpio_write32( 0x%08lX ): Not in registered range(s)\n", addr );
    return;
  }
 
  switch( addr ) {
  case RGPIO_IN: debug( 3, "GPIO: Cannot write to RGPIO_IN\n" ); break;
  case RGPIO_OUT: gpio->next.out = value; break;
  case RGPIO_OE: gpio->next.oe = value; break;
  case RGPIO_INTE: gpio->next.inte = value; break;
  case RGPIO_PTRIG: gpio->next.ptrig = value; break;
  case RGPIO_AUX: gpio->next.aux = value; break;
  case RGPIO_CTRL: gpio->next.ctrl = value; break;
  }
}
 
 
/* Input from "outside world" */
void gpio_vapi_read( unsigned long id, unsigned long data )
{
  unsigned which;
  struct gpio_device *gpio = gpio_find_vapi_device( id, &which );
 
  debug( 4, "GPIO: id %08x, data %08x\n", id, data );
 
  if ( !gpio ) {
    debug( 1, "GPIO: VAPI ID %08x is not ours!\n", id );
    return;
  }
 
  switch( which ) {
  case GPIO_VAPI_DATA: gpio->next.in = data; break;
  case GPIO_VAPI_AUX: gpio->auxiliary_inputs = data; break;
  case GPIO_VAPI_CLOCK: gpio_external_clock( data ); break;
  case GPIO_VAPI_CONFIG: /* Currently no configuration commands supported */ break;
  }
}
 
/* System Clock. */
void gpio_clock( void )
{
  unsigned i;
 
  for ( i = 0; i < MAX_GPIOS; ++ i )
    if ( !(gpios[i].curr.ctrl & RGPIO_CTRL_ECLK) )
      gpio_device_clock( &(gpios[i]) );
}
 
/* External Clock. */
void gpio_external_clock( unsigned long value )
{
  unsigned i;
 
  value = (value != 0);
 
  for ( i = 0; i < MAX_GPIOS; ++ i ) {
    struct gpio_device *gpio = &(gpios[i]);
 
    int eclk = ((gpio->curr.ctrl & RGPIO_CTRL_ECLK) == RGPIO_CTRL_ECLK);
    int nec = ((gpio->curr.ctrl & RGPIO_CTRL_NEC) == RGPIO_CTRL_NEC);
 
    gpio->next.external_clock = value;
 
    if ( eclk && (gpio->next.external_clock != gpio->curr.external_clock) && (value != nec) )
      gpio_device_clock( gpio );
  }
}
 
/* Clock as handld by one device. */
void gpio_device_clock( struct gpio_device *gpio )
{
  /* Calculate new inputs and outputs */
  gpio->next.in &= ~gpio->next.oe; /* Only input bits */
  /* Replace requested output bits with aux input */
  gpio->next.out = (gpio->next.out & ~gpio->next.aux) | (gpio->auxiliary_inputs & gpio->next.aux);
  gpio->next.out &= gpio->next.oe; /* Only output-enabled bits */
 
  /* If any outputs changed, notify the world (i.e. vapi) */
  if ( gpio->next.out != gpio->curr.out )
    if ( gpio->vapi_ids[GPIO_VAPI_DATA] != 0 )
      vapi_send( gpio->vapi_ids[GPIO_VAPI_DATA], gpio->next.out );
 
  /* If any inputs changed and interrupt enabled, generate interrupt */
  if ( (gpio->next.in != gpio->curr.in) && (gpio->next.ctrl & RGPIO_CTRL_INTE) ) {
    unsigned changed_bits = gpio->next.in ^ gpio->curr.in; /* inputs that have changed */
    unsigned set_bits = changed_bits & gpio->next.in; /* inputs that have been set */
    unsigned cleared_bits = changed_bits & gpio->curr.in; /* inputs that have been cleared */
    unsigned relevant_bits = (gpio->next.ptrig & set_bits) | (~gpio->next.ptrig & cleared_bits);
    if ( relevant_bits & gpio->next.inte ) {
      report_interrupt( gpio->irq );
      gpio->next.ctrl |= RGPIO_CTRL_INT;
    }
  }
 
  /* Switch to values for next clock */
  memcpy( &(gpio->curr), &(gpio->next), sizeof(gpio->curr) );
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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