xref: /netbsd-src/external/gpl3/gdb/dist/sim/bfin/dv-bfin_gptimer.c (revision 1580a27b92f58fcdcb23fdfbc04a7c2b54a0b7c8)
1 /* Blackfin General Purpose Timers (GPtimer) model
2 
3    Copyright (C) 2010-2016 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 #include "config.h"
22 
23 #include "sim-main.h"
24 #include "devices.h"
25 #include "dv-bfin_gptimer.h"
26 
27 /* XXX: This is merely a stub.  */
28 
29 struct bfin_gptimer
30 {
31   /* This top portion matches common dv_bfin struct.  */
32   bu32 base;
33   struct hw *dma_master;
34   bool acked;
35 
36   struct hw_event *handler;
37   char saved_byte;
38   int saved_count;
39 
40   /* Order after here is important -- matches hardware MMR layout.  */
41   bu16 BFIN_MMR_16(config);
42   bu32 counter, period, width;
43 };
44 #define mmr_base()      offsetof(struct bfin_gptimer, config)
45 #define mmr_offset(mmr) (offsetof(struct bfin_gptimer, mmr) - mmr_base())
46 
47 static const char * const mmr_names[] =
48 {
49   "TIMER_CONFIG", "TIMER_COUNTER", "TIMER_PERIOD", "TIMER_WIDTH",
50 };
51 #define mmr_name(off) mmr_names[(off) / 4]
52 
53 static unsigned
54 bfin_gptimer_io_write_buffer (struct hw *me, const void *source, int space,
55 			      address_word addr, unsigned nr_bytes)
56 {
57   struct bfin_gptimer *gptimer = hw_data (me);
58   bu32 mmr_off;
59   bu32 value;
60   bu16 *value16p;
61   bu32 *value32p;
62   void *valuep;
63 
64   /* Invalid access mode is higher priority than missing register.  */
65   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
66     return 0;
67 
68   if (nr_bytes == 4)
69     value = dv_load_4 (source);
70   else
71     value = dv_load_2 (source);
72 
73   mmr_off = addr - gptimer->base;
74   valuep = (void *)((unsigned long)gptimer + mmr_base() + mmr_off);
75   value16p = valuep;
76   value32p = valuep;
77 
78   HW_TRACE_WRITE ();
79 
80   switch (mmr_off)
81     {
82     case mmr_offset(config):
83       if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
84 	return 0;
85       *value16p = value;
86       break;
87     case mmr_offset(counter):
88     case mmr_offset(period):
89     case mmr_offset(width):
90       if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
91 	return 0;
92       *value32p = value;
93       break;
94     default:
95       dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
96       return 0;
97     }
98 
99   return nr_bytes;
100 }
101 
102 static unsigned
103 bfin_gptimer_io_read_buffer (struct hw *me, void *dest, int space,
104 			     address_word addr, unsigned nr_bytes)
105 {
106   struct bfin_gptimer *gptimer = hw_data (me);
107   bu32 mmr_off;
108   bu16 *value16p;
109   bu32 *value32p;
110   void *valuep;
111 
112   /* Invalid access mode is higher priority than missing register.  */
113   if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
114     return 0;
115 
116   mmr_off = addr - gptimer->base;
117   valuep = (void *)((unsigned long)gptimer + mmr_base() + mmr_off);
118   value16p = valuep;
119   value32p = valuep;
120 
121   HW_TRACE_READ ();
122 
123   switch (mmr_off)
124     {
125     case mmr_offset(config):
126       if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
127 	return 0;
128       dv_store_2 (dest, *value16p);
129       break;
130     case mmr_offset(counter):
131     case mmr_offset(period):
132     case mmr_offset(width):
133       if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
134 	return 0;
135       dv_store_4 (dest, *value32p);
136       break;
137     default:
138       dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
139       return 0;
140     }
141 
142   return nr_bytes;
143 }
144 
145 static const struct hw_port_descriptor bfin_gptimer_ports[] =
146 {
147   { "stat", 0, 0, output_port, },
148   { NULL, 0, 0, 0, },
149 };
150 
151 static void
152 attach_bfin_gptimer_regs (struct hw *me, struct bfin_gptimer *gptimer)
153 {
154   address_word attach_address;
155   int attach_space;
156   unsigned attach_size;
157   reg_property_spec reg;
158 
159   if (hw_find_property (me, "reg") == NULL)
160     hw_abort (me, "Missing \"reg\" property");
161 
162   if (!hw_find_reg_array_property (me, "reg", 0, &reg))
163     hw_abort (me, "\"reg\" property must contain three addr/size entries");
164 
165   hw_unit_address_to_attach_address (hw_parent (me),
166 				     &reg.address,
167 				     &attach_space, &attach_address, me);
168   hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
169 
170   if (attach_size != BFIN_MMR_GPTIMER_SIZE)
171     hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_GPTIMER_SIZE);
172 
173   hw_attach_address (hw_parent (me),
174 		     0, attach_space, attach_address, attach_size, me);
175 
176   gptimer->base = attach_address;
177 }
178 
179 static void
180 bfin_gptimer_finish (struct hw *me)
181 {
182   struct bfin_gptimer *gptimer;
183 
184   gptimer = HW_ZALLOC (me, struct bfin_gptimer);
185 
186   set_hw_data (me, gptimer);
187   set_hw_io_read_buffer (me, bfin_gptimer_io_read_buffer);
188   set_hw_io_write_buffer (me, bfin_gptimer_io_write_buffer);
189   set_hw_ports (me, bfin_gptimer_ports);
190 
191   attach_bfin_gptimer_regs (me, gptimer);
192 }
193 
194 const struct hw_descriptor dv_bfin_gptimer_descriptor[] =
195 {
196   {"bfin_gptimer", bfin_gptimer_finish,},
197   {NULL, NULL},
198 };
199