Home | History | Annotate | Line # | Download | only in bfin
      1 /* Blackfin General Purpose Timers (GPtimer) model
      2 
      3    Copyright (C) 2010-2024 Free Software Foundation, Inc.
      4    Contributed by Analog Devices, Inc.
      5 
      6    This file is part of simulators.
      7 
      8    This program is free software; you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation; either version 3 of the License, or
     11    (at your option) any later version.
     12 
     13    This program is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20 
     21 /* This must come before any other includes.  */
     22 #include "defs.h"
     23 
     24 #include "sim-main.h"
     25 #include "devices.h"
     26 #include "dv-bfin_gptimer.h"
     27 
     28 /* XXX: This is merely a stub.  */
     29 
     30 struct bfin_gptimer
     31 {
     32   /* This top portion matches common dv_bfin struct.  */
     33   bu32 base;
     34   struct hw *dma_master;
     35   bool acked;
     36 
     37   struct hw_event *handler;
     38   char saved_byte;
     39   int saved_count;
     40 
     41   /* Order after here is important -- matches hardware MMR layout.  */
     42   bu16 BFIN_MMR_16(config);
     43   bu32 counter, period, width;
     44 };
     45 #define mmr_base()      offsetof(struct bfin_gptimer, config)
     46 #define mmr_offset(mmr) (offsetof(struct bfin_gptimer, mmr) - mmr_base())
     47 
     48 static const char * const mmr_names[] =
     49 {
     50   "TIMER_CONFIG", "TIMER_COUNTER", "TIMER_PERIOD", "TIMER_WIDTH",
     51 };
     52 #define mmr_name(off) mmr_names[(off) / 4]
     53 
     54 static unsigned
     55 bfin_gptimer_io_write_buffer (struct hw *me, const void *source, int space,
     56 			      address_word addr, unsigned nr_bytes)
     57 {
     58   struct bfin_gptimer *gptimer = hw_data (me);
     59   bu32 mmr_off;
     60   bu32 value;
     61   bu16 *value16p;
     62   bu32 *value32p;
     63   void *valuep;
     64 
     65   /* Invalid access mode is higher priority than missing register.  */
     66   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
     67     return 0;
     68 
     69   if (nr_bytes == 4)
     70     value = dv_load_4 (source);
     71   else
     72     value = dv_load_2 (source);
     73 
     74   mmr_off = addr - gptimer->base;
     75   valuep = (void *)((uintptr_t)gptimer + mmr_base() + mmr_off);
     76   value16p = valuep;
     77   value32p = valuep;
     78 
     79   HW_TRACE_WRITE ();
     80 
     81   switch (mmr_off)
     82     {
     83     case mmr_offset(config):
     84       if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
     85 	return 0;
     86       *value16p = value;
     87       break;
     88     case mmr_offset(counter):
     89     case mmr_offset(period):
     90     case mmr_offset(width):
     91       if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
     92 	return 0;
     93       *value32p = value;
     94       break;
     95     default:
     96       dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
     97       return 0;
     98     }
     99 
    100   return nr_bytes;
    101 }
    102 
    103 static unsigned
    104 bfin_gptimer_io_read_buffer (struct hw *me, void *dest, int space,
    105 			     address_word addr, unsigned nr_bytes)
    106 {
    107   struct bfin_gptimer *gptimer = hw_data (me);
    108   bu32 mmr_off;
    109   bu16 *value16p;
    110   bu32 *value32p;
    111   void *valuep;
    112 
    113   /* Invalid access mode is higher priority than missing register.  */
    114   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
    115     return 0;
    116 
    117   mmr_off = addr - gptimer->base;
    118   valuep = (void *)((uintptr_t)gptimer + mmr_base() + mmr_off);
    119   value16p = valuep;
    120   value32p = valuep;
    121 
    122   HW_TRACE_READ ();
    123 
    124   switch (mmr_off)
    125     {
    126     case mmr_offset(config):
    127       if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
    128 	return 0;
    129       dv_store_2 (dest, *value16p);
    130       break;
    131     case mmr_offset(counter):
    132     case mmr_offset(period):
    133     case mmr_offset(width):
    134       if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
    135 	return 0;
    136       dv_store_4 (dest, *value32p);
    137       break;
    138     default:
    139       dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
    140       return 0;
    141     }
    142 
    143   return nr_bytes;
    144 }
    145 
    146 static const struct hw_port_descriptor bfin_gptimer_ports[] =
    147 {
    148   { "stat", 0, 0, output_port, },
    149   { NULL, 0, 0, 0, },
    150 };
    151 
    152 static void
    153 attach_bfin_gptimer_regs (struct hw *me, struct bfin_gptimer *gptimer)
    154 {
    155   address_word attach_address;
    156   int attach_space;
    157   unsigned attach_size;
    158   reg_property_spec reg;
    159 
    160   if (hw_find_property (me, "reg") == NULL)
    161     hw_abort (me, "Missing \"reg\" property");
    162 
    163   if (!hw_find_reg_array_property (me, "reg", 0, &reg))
    164     hw_abort (me, "\"reg\" property must contain three addr/size entries");
    165 
    166   hw_unit_address_to_attach_address (hw_parent (me),
    167 				     &reg.address,
    168 				     &attach_space, &attach_address, me);
    169   hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
    170 
    171   if (attach_size != BFIN_MMR_GPTIMER_SIZE)
    172     hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_GPTIMER_SIZE);
    173 
    174   hw_attach_address (hw_parent (me),
    175 		     0, attach_space, attach_address, attach_size, me);
    176 
    177   gptimer->base = attach_address;
    178 }
    179 
    180 static void
    181 bfin_gptimer_finish (struct hw *me)
    182 {
    183   struct bfin_gptimer *gptimer;
    184 
    185   gptimer = HW_ZALLOC (me, struct bfin_gptimer);
    186 
    187   set_hw_data (me, gptimer);
    188   set_hw_io_read_buffer (me, bfin_gptimer_io_read_buffer);
    189   set_hw_io_write_buffer (me, bfin_gptimer_io_write_buffer);
    190   set_hw_ports (me, bfin_gptimer_ports);
    191 
    192   attach_bfin_gptimer_regs (me, gptimer);
    193 }
    194 
    195 const struct hw_descriptor dv_bfin_gptimer_descriptor[] =
    196 {
    197   {"bfin_gptimer", bfin_gptimer_finish,},
    198   {NULL, NULL},
    199 };
    200