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