Search
lxdream.org :: lxdream/src/mmio.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/mmio.h
changeset 55:96323c198da3
prev31:495e480360d7
next136:f07efd59604f
author nkeynes
date Mon Mar 13 12:37:06 2006 +0000 (18 years ago)
permissions -rw-r--r--
last change Add sh4addr_t type, need to start propagating it instead of uint32_t
view annotate diff log raw
     1 /**
     2  * $Id: mmio.h,v 1.3 2006-01-01 08:09:17 nkeynes Exp $
     3  *
     4  * mmio.h defines a complicated batch of macros used to build up the 
     5  * memory-mapped I/O regions in a reasonably readable fashion.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    19 #ifndef dream_mmio_H
    20 #define dream_mmio_H 1
    22 #ifdef __cplusplus
    23 extern "C" {
    24 #if 0
    25 }
    26 #endif
    27 #endif
    29 #include <stdint.h>
    30 #include <stdlib.h>
    32 #define PAGE_TABLE_ENTRIES 128*1024
    33 #define PAGE_SIZE 4096
    34 #define PAGE_BITS 12
    36 #define PORT_R 1
    37 #define PORT_W 2
    38 #define PORT_MEM 4 /* store written value */
    39 #define PORT_RW 3
    40 #define PORT_MR 5
    41 #define PORT_MRW 7
    42 #define UNDEFINED 0xDEADBEEF /* This has to be a value that nothing inits to */
    44 struct mmio_region {
    45     char *id, *desc;
    46     uint32_t base;
    47     int32_t (*io_read)(uint32_t addr);
    48     void (*io_write)(uint32_t addr, uint32_t val);
    49     char *mem;
    50     char *save_mem; /* Used to compare for gui updates */
    51     struct mmio_port {
    52         char *id, *desc;
    53         int width;
    54         uint32_t offset;
    55         uint32_t def_val;
    56         int flags;
    57         uint32_t *val;
    58     } ports[80];
    59     struct mmio_port **index; /* reverse lookup by address */
    60     int trace_flag; /* set to 1 to enable transfer traces */
    61 };
    63 void register_io_region( struct mmio_region *mmio );
    64 void register_io_regions( struct mmio_region **mmiolist );
    66 extern struct mmio_region *io_rgn[];
    67 extern int num_io_rgns;
    69 #define MMIO_READ( id, r ) *((int32_t *)(mmio_region_##id.mem + (r)))
    70 #define MMIO_WRITE( id, r, v ) *((int32_t *)(mmio_region_##id.mem + (r))) = (v)
    71 #define MMIO_REG( id, r ) ((int32_t *)(mmio_region_##id.mem + (r)))
    72 #define MMIO_REGID( mid, r ) (mmio_region_##mid.index[(r)>>2] != NULL ? \
    73             mmio_region_##mid.index[(r)>>2]->id : "<UNDEF>" )
    74 #define MMIO_REGDESC( mid, r) (mmio_region_##mid.index[(r)>>2] != NULL ? \
    75             mmio_region_##mid.index[(r)>>2]->desc : "Undefined register" )
    76 #define MMIO_TRACE( mid ) mmio_region_##mid.trace_flag = 1
    77 #define MMIO_NOTRACE( mid ) mmio_region_##mid.trace_flag = 0
    79 #define MMIO_REGID_BYNUM( mid, r ) (io_rgn[mid]->index[(r)>>2] != NULL ? \
    80             io_rgn[mid]->index[(r)>>2]->id : "<UNDEF>" )
    81 #define MMIO_REGDESC_BYNUM( mid, r ) (io_rgn[mid]->index[(r)>>2] != NULL ? \
    82             io_rgn[mid]->index[(r)>>2]->desc : "Undefined register" )
    83 #define MMIO_NAME_BYNUM( mid ) (io_rgn[mid]->id)
    85 #ifdef __cplusplus
    86 }
    87 #endif
    89 #endif
    91 #ifdef MMIO_IMPL
    93 #ifndef MMIO_IMPL_INCLUDED
    94 #define MMIO_IMPL_INCLUDED
    95 #undef MMIO_REGION_BEGIN
    96 #undef LONG_PORT
    97 #undef WORD_PORT
    98 #undef BYTE_PORT
    99 #undef MMIO_REGION_END
   100 #undef MMIO_REGION_LIST_BEGIN
   101 #undef MMIO_REGION
   102 #undef MMIO_REGION_LIST_END
   103 #define MMIO_REGION_BEGIN(b,id,d) struct mmio_region mmio_region_##id = { #id, d, b, mmio_region_##id##_read, mmio_region_##id##_write, 0, 0, {
   104 #define LONG_PORT( o,id,f,def,d ) { #id, d, 32, o, def, f },
   105 #define WORD_PORT( o,id,f,def,d ) { #id, d, 16, o, def, f },
   106 #define BYTE_PORT( o,id,f,def,d ) { #id, d, 8, o, def, f },
   107 #define MMIO_REGION_END {NULL, NULL, 0, 0, 0, 0} } };
   108 #define MMIO_REGION_LIST_BEGIN(id) struct mmio_region *mmio_list_##id[] = {
   109 #define MMIO_REGION( id ) &mmio_region_##id,
   110 #define MMIO_REGION_LIST_END NULL};
   112 /* Stub defines for modules we haven't got to yet, or ones which don't
   113  * actually need any direct code on read and/or write
   114  */
   115 #define MMIO_REGION_READ_STUBFN( id ) \
   116 int32_t mmio_region_##id##_read( uint32_t reg ) { \
   117     int32_t val = MMIO_READ( id, reg ); \
   118     WARN( "Read from unimplemented module %s (%03X => %08X) [%s: %s]",\
   119           #id, reg, val, MMIO_REGID(id,reg), MMIO_REGDESC(id,reg) ); \
   120     return val; \
   121 }
   122 #define MMIO_REGION_WRITE_STUBFN( id ) \
   123 void mmio_region_##id##_write( uint32_t reg, uint32_t val ) { \
   124     WARN( "Write to unimplemented module %s (%03X <= %08X) [%s: %s]", \
   125           #id, reg, val, MMIO_REGID(id,reg), MMIO_REGDESC(id,reg) ); \
   126     MMIO_WRITE( id, reg, val ); \
   127 }
   128 #define MMIO_REGION_STUBFNS( id ) \
   129     MMIO_REGION_READ_STUBFN( id ) \
   130     MMIO_REGION_WRITE_STUBFN( id )
   131 #define MMIO_REGION_READ_DEFFN( id ) \
   132 int32_t mmio_region_##id##_read( uint32_t reg ) { \
   133     return MMIO_READ( id, reg ); \
   134 }
   135 #define MMIO_REGION_WRITE_DEFFN( id ) \
   136 void mmio_region_##id##_write( uint32_t reg, uint32_t val ) { \
   137     MMIO_WRITE( id, reg, val ); \
   138 }
   139 #define MMIO_REGION_DEFFNS( id ) \
   140     MMIO_REGION_READ_DEFFN( id ) \
   141     MMIO_REGION_WRITE_DEFFN( id )
   142 #endif
   144 #define MMIO_REGION_WRITE_FN( id, reg, val ) \
   145 void mmio_region_##id##_write( uint32_t reg, uint32_t val )
   147 #define MMIO_REGION_READ_FN( id, reg ) \
   148 int32_t mmio_region_##id##_read( uint32_t reg )
   150 #else
   152 #ifndef MMIO_IFACE_INCLUDED
   153 #define MMIO_IFACE_INCLUDED
   154 #define MMIO_REGION_BEGIN(b,id,d) \
   155 extern struct mmio_region mmio_region_##id; \
   156 int32_t mmio_region_##id##_read(uint32_t); \
   157 void mmio_region_##id##_write(uint32_t, uint32_t); \
   158 enum mmio_region_##id##_port_t {
   159 #define LONG_PORT( o,id,f,def,d ) id = o,
   160 #define WORD_PORT( o,id,f,def,d ) id = o,
   161 #define BYTE_PORT( o,id,f,def,d ) id = o,
   162 #define MMIO_REGION_END };
   163 #define MMIO_REGION_LIST_BEGIN(id) extern struct mmio_region *mmio_list_##id[];
   164 #define MMIO_REGION( id )
   165 #define MMIO_REGION_LIST_END
   166 #endif
   168 #endif
.