blob: 1b17a3d3729ef1e9226cc93c0f04131d36ba188e [file] [log] [blame]
bellarde80cfcf2004-12-19 23:18:01 +00001/*
2 * QEMU Sparc SLAVIO timer controller emulation
3 *
bellard66321a12005-04-06 20:47:48 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellarde80cfcf2004-12-19 23:18:01 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
26//#define DEBUG_TIMER
27
bellard66321a12005-04-06 20:47:48 +000028#ifdef DEBUG_TIMER
29#define DPRINTF(fmt, args...) \
30do { printf("TIMER: " fmt , ##args); } while (0)
31#else
32#define DPRINTF(fmt, args...)
33#endif
34
bellarde80cfcf2004-12-19 23:18:01 +000035/*
36 * Registers of hardware timer in sun4m.
37 *
38 * This is the timer/counter part of chip STP2001 (Slave I/O), also
39 * produced as NCR89C105. See
40 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
ths5fafdf22007-09-16 21:08:06 +000041 *
bellarde80cfcf2004-12-19 23:18:01 +000042 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
43 * are zero. Bit 31 is 1 when count has been reached.
44 *
bellardba3c64f2005-12-05 20:31:52 +000045 * Per-CPU timers interrupt local CPU, system timer uses normal
46 * interrupt routing.
47 *
bellarde80cfcf2004-12-19 23:18:01 +000048 */
49
blueswir181732d12007-10-06 11:25:43 +000050#define MAX_CPUS 16
51
bellarde80cfcf2004-12-19 23:18:01 +000052typedef struct SLAVIO_TIMERState {
blueswir1d7edfd22007-05-27 16:37:49 +000053 qemu_irq irq;
blueswir18d05ea82007-05-24 19:48:41 +000054 ptimer_state *timer;
55 uint32_t count, counthigh, reached;
56 uint64_t limit;
blueswir1115646b2007-10-07 10:00:55 +000057 // processor only
58 int running;
59 struct SLAVIO_TIMERState *master;
60 int slave_index;
61 // system only
blueswir181732d12007-10-06 11:25:43 +000062 struct SLAVIO_TIMERState *slave[MAX_CPUS];
63 uint32_t slave_mode;
bellarde80cfcf2004-12-19 23:18:01 +000064} SLAVIO_TIMERState;
65
66#define TIMER_MAXADDR 0x1f
blueswir1115646b2007-10-07 10:00:55 +000067#define SYS_TIMER_SIZE 0x14
blueswir181732d12007-10-06 11:25:43 +000068#define CPU_TIMER_SIZE 0x10
bellarde80cfcf2004-12-19 23:18:01 +000069
blueswir1115646b2007-10-07 10:00:55 +000070static int slavio_timer_is_user(SLAVIO_TIMERState *s)
71{
72 return s->master && (s->master->slave_mode & (1 << s->slave_index));
73}
74
bellarde80cfcf2004-12-19 23:18:01 +000075// Update count, set irq, update expire_time
blueswir18d05ea82007-05-24 19:48:41 +000076// Convert from ptimer countdown units
bellarde80cfcf2004-12-19 23:18:01 +000077static void slavio_timer_get_out(SLAVIO_TIMERState *s)
78{
blueswir18d05ea82007-05-24 19:48:41 +000079 uint64_t count;
bellarde80cfcf2004-12-19 23:18:01 +000080
blueswir18d05ea82007-05-24 19:48:41 +000081 count = s->limit - (ptimer_get_count(s->timer) << 9);
82 DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit, s->counthigh,
83 s->count);
84 s->count = count & 0xfffffe00;
85 s->counthigh = count >> 32;
bellarde80cfcf2004-12-19 23:18:01 +000086}
87
88// timer callback
89static void slavio_timer_irq(void *opaque)
90{
91 SLAVIO_TIMERState *s = opaque;
92
bellarde80cfcf2004-12-19 23:18:01 +000093 slavio_timer_get_out(s);
blueswir18d05ea82007-05-24 19:48:41 +000094 DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
blueswir1115646b2007-10-07 10:00:55 +000095 if (!slavio_timer_is_user(s)) {
96 s->reached = 0x80000000;
blueswir1f930d072007-10-06 11:28:21 +000097 qemu_irq_raise(s->irq);
blueswir1115646b2007-10-07 10:00:55 +000098 }
bellarde80cfcf2004-12-19 23:18:01 +000099}
100
101static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
102{
103 SLAVIO_TIMERState *s = opaque;
blueswir18d05ea82007-05-24 19:48:41 +0000104 uint32_t saddr, ret;
bellarde80cfcf2004-12-19 23:18:01 +0000105
106 saddr = (addr & TIMER_MAXADDR) >> 2;
107 switch (saddr) {
108 case 0:
blueswir1f930d072007-10-06 11:28:21 +0000109 // read limit (system counter mode) or read most signifying
110 // part of counter (user mode)
blueswir1115646b2007-10-07 10:00:55 +0000111 if (slavio_timer_is_user(s)) {
112 // read user timer MSW
113 slavio_timer_get_out(s);
114 ret = s->counthigh;
115 } else {
116 // read limit
blueswir1f930d072007-10-06 11:28:21 +0000117 // clear irq
blueswir1d7edfd22007-05-27 16:37:49 +0000118 qemu_irq_lower(s->irq);
blueswir1f930d072007-10-06 11:28:21 +0000119 s->reached = 0;
blueswir18d05ea82007-05-24 19:48:41 +0000120 ret = s->limit & 0x7fffffff;
blueswir1f930d072007-10-06 11:28:21 +0000121 }
blueswir18d05ea82007-05-24 19:48:41 +0000122 break;
bellarde80cfcf2004-12-19 23:18:01 +0000123 case 1:
blueswir1f930d072007-10-06 11:28:21 +0000124 // read counter and reached bit (system mode) or read lsbits
125 // of counter (user mode)
126 slavio_timer_get_out(s);
blueswir1115646b2007-10-07 10:00:55 +0000127 if (slavio_timer_is_user(s)) // read user timer LSW
128 ret = s->count & 0xffffffe00;
129 else // read limit
130 ret = (s->count & 0x7ffffe00) | s->reached;
blueswir18d05ea82007-05-24 19:48:41 +0000131 break;
bellarde80cfcf2004-12-19 23:18:01 +0000132 case 3:
blueswir1115646b2007-10-07 10:00:55 +0000133 // only available in processor counter/timer
blueswir1f930d072007-10-06 11:28:21 +0000134 // read start/stop status
blueswir1115646b2007-10-07 10:00:55 +0000135 ret = s->running;
blueswir18d05ea82007-05-24 19:48:41 +0000136 break;
bellarde80cfcf2004-12-19 23:18:01 +0000137 case 4:
blueswir1115646b2007-10-07 10:00:55 +0000138 // only available in system counter
blueswir1f930d072007-10-06 11:28:21 +0000139 // read user/system mode
blueswir181732d12007-10-06 11:25:43 +0000140 ret = s->slave_mode;
blueswir18d05ea82007-05-24 19:48:41 +0000141 break;
bellarde80cfcf2004-12-19 23:18:01 +0000142 default:
blueswir1115646b2007-10-07 10:00:55 +0000143 DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
blueswir18d05ea82007-05-24 19:48:41 +0000144 ret = 0;
145 break;
bellarde80cfcf2004-12-19 23:18:01 +0000146 }
blueswir18d05ea82007-05-24 19:48:41 +0000147 DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
148
149 return ret;
bellarde80cfcf2004-12-19 23:18:01 +0000150}
151
152static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
153{
154 SLAVIO_TIMERState *s = opaque;
155 uint32_t saddr;
blueswir18d05ea82007-05-24 19:48:41 +0000156 int reload = 0;
bellarde80cfcf2004-12-19 23:18:01 +0000157
blueswir18d05ea82007-05-24 19:48:41 +0000158 DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
bellarde80cfcf2004-12-19 23:18:01 +0000159 saddr = (addr & TIMER_MAXADDR) >> 2;
160 switch (saddr) {
161 case 0:
blueswir1115646b2007-10-07 10:00:55 +0000162 if (slavio_timer_is_user(s)) {
163 // set user counter MSW, reset counter
blueswir181732d12007-10-06 11:25:43 +0000164 qemu_irq_lower(s->irq);
blueswir1115646b2007-10-07 10:00:55 +0000165 s->limit = 0x7ffffffffffffe00ULL;
166 DPRINTF("processor %d user timer reset\n", s->slave_index);
blueswir181732d12007-10-06 11:25:43 +0000167 ptimer_set_limit(s->timer, s->limit >> 9, 1);
blueswir1115646b2007-10-07 10:00:55 +0000168 } else {
169 // set limit, reset counter
170 qemu_irq_lower(s->irq);
171 s->limit = val & 0x7ffffe00ULL;
172 if (!s->limit)
173 s->limit = 0x7ffffe00ULL;
174 ptimer_set_limit(s->timer, s->limit >> 9, 1);
blueswir181732d12007-10-06 11:25:43 +0000175 }
blueswir1115646b2007-10-07 10:00:55 +0000176 break;
177 case 1:
178 if (slavio_timer_is_user(s)) {
179 // set user counter LSW, reset counter
180 qemu_irq_lower(s->irq);
181 s->limit = 0x7ffffffffffffe00ULL;
182 DPRINTF("processor %d user timer reset\n", s->slave_index);
183 ptimer_set_limit(s->timer, s->limit >> 9, 1);
184 } else
185 DPRINTF("not user timer\n");
186 break;
bellarde80cfcf2004-12-19 23:18:01 +0000187 case 2:
blueswir1f930d072007-10-06 11:28:21 +0000188 // set limit without resetting counter
blueswir18d05ea82007-05-24 19:48:41 +0000189 s->limit = val & 0x7ffffe00ULL;
190 if (!s->limit)
191 s->limit = 0x7ffffe00ULL;
192 ptimer_set_limit(s->timer, s->limit >> 9, reload);
blueswir1f930d072007-10-06 11:28:21 +0000193 break;
bellarde80cfcf2004-12-19 23:18:01 +0000194 case 3:
blueswir1115646b2007-10-07 10:00:55 +0000195 if (slavio_timer_is_user(s)) {
196 // start/stop user counter
197 if ((val & 1) && !s->running) {
198 DPRINTF("processor %d user timer started\n", s->slave_index);
blueswir18d05ea82007-05-24 19:48:41 +0000199 ptimer_run(s->timer, 0);
blueswir1115646b2007-10-07 10:00:55 +0000200 s->running = 1;
201 } else if (!(val & 1) && s->running) {
202 DPRINTF("processor %d user timer stopped\n", s->slave_index);
203 ptimer_stop(s->timer);
204 s->running = 0;
blueswir1f930d072007-10-06 11:28:21 +0000205 }
206 }
207 break;
bellarde80cfcf2004-12-19 23:18:01 +0000208 case 4:
blueswir1115646b2007-10-07 10:00:55 +0000209 if (s->master == NULL) {
blueswir181732d12007-10-06 11:25:43 +0000210 unsigned int i;
211
212 for (i = 0; i < MAX_CPUS; i++) {
213 if (val & (1 << i)) {
214 qemu_irq_lower(s->slave[i]->irq);
215 s->slave[i]->limit = -1ULL;
blueswir181732d12007-10-06 11:25:43 +0000216 }
blueswir1115646b2007-10-07 10:00:55 +0000217 if ((val & (1 << i)) != (s->slave_mode & (1 << i))) {
218 ptimer_stop(s->slave[i]->timer);
219 ptimer_set_limit(s->slave[i]->timer, s->slave[i]->limit >> 9, 1);
220 DPRINTF("processor %d timer changed\n", s->slave[i]->slave_index);
221 ptimer_run(s->slave[i]->timer, 0);
222 }
blueswir181732d12007-10-06 11:25:43 +0000223 }
224 s->slave_mode = val & ((1 << MAX_CPUS) - 1);
blueswir1115646b2007-10-07 10:00:55 +0000225 } else
226 DPRINTF("not system timer\n");
blueswir1f930d072007-10-06 11:28:21 +0000227 break;
bellarde80cfcf2004-12-19 23:18:01 +0000228 default:
blueswir1115646b2007-10-07 10:00:55 +0000229 DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
blueswir1f930d072007-10-06 11:28:21 +0000230 break;
bellarde80cfcf2004-12-19 23:18:01 +0000231 }
232}
233
234static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
235 slavio_timer_mem_readl,
236 slavio_timer_mem_readl,
237 slavio_timer_mem_readl,
238};
239
240static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
241 slavio_timer_mem_writel,
242 slavio_timer_mem_writel,
243 slavio_timer_mem_writel,
244};
245
246static void slavio_timer_save(QEMUFile *f, void *opaque)
247{
248 SLAVIO_TIMERState *s = opaque;
249
blueswir18d05ea82007-05-24 19:48:41 +0000250 qemu_put_be64s(f, &s->limit);
bellarde80cfcf2004-12-19 23:18:01 +0000251 qemu_put_be32s(f, &s->count);
252 qemu_put_be32s(f, &s->counthigh);
blueswir1d7edfd22007-05-27 16:37:49 +0000253 qemu_put_be32(f, 0); // Was irq
bellarde80cfcf2004-12-19 23:18:01 +0000254 qemu_put_be32s(f, &s->reached);
blueswir1115646b2007-10-07 10:00:55 +0000255 qemu_put_be32s(f, &s->running);
256 qemu_put_be32s(f, 0); // Was mode
blueswir18d05ea82007-05-24 19:48:41 +0000257 qemu_put_ptimer(f, s->timer);
bellarde80cfcf2004-12-19 23:18:01 +0000258}
259
260static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
261{
262 SLAVIO_TIMERState *s = opaque;
blueswir1d7edfd22007-05-27 16:37:49 +0000263 uint32_t tmp;
ths3b46e622007-09-17 08:09:54 +0000264
blueswir18d05ea82007-05-24 19:48:41 +0000265 if (version_id != 2)
bellarde80cfcf2004-12-19 23:18:01 +0000266 return -EINVAL;
267
blueswir18d05ea82007-05-24 19:48:41 +0000268 qemu_get_be64s(f, &s->limit);
bellarde80cfcf2004-12-19 23:18:01 +0000269 qemu_get_be32s(f, &s->count);
270 qemu_get_be32s(f, &s->counthigh);
blueswir1d7edfd22007-05-27 16:37:49 +0000271 qemu_get_be32s(f, &tmp); // Was irq
bellarde80cfcf2004-12-19 23:18:01 +0000272 qemu_get_be32s(f, &s->reached);
blueswir1115646b2007-10-07 10:00:55 +0000273 qemu_get_be32s(f, &s->running);
274 qemu_get_be32s(f, &tmp); // Was mode
blueswir18d05ea82007-05-24 19:48:41 +0000275 qemu_get_ptimer(f, s->timer);
276
bellarde80cfcf2004-12-19 23:18:01 +0000277 return 0;
278}
279
280static void slavio_timer_reset(void *opaque)
281{
282 SLAVIO_TIMERState *s = opaque;
283
blueswir1115646b2007-10-07 10:00:55 +0000284 if (slavio_timer_is_user(s))
285 s->limit = 0x7ffffffffffffe00ULL;
286 else
287 s->limit = 0x7ffffe00ULL;
bellarde80cfcf2004-12-19 23:18:01 +0000288 s->count = 0;
bellarde80cfcf2004-12-19 23:18:01 +0000289 s->reached = 0;
blueswir18d05ea82007-05-24 19:48:41 +0000290 ptimer_set_limit(s->timer, s->limit >> 9, 1);
291 ptimer_run(s->timer, 0);
blueswir1115646b2007-10-07 10:00:55 +0000292 s->running = 1;
blueswir1d7edfd22007-05-27 16:37:49 +0000293 qemu_irq_lower(s->irq);
bellarde80cfcf2004-12-19 23:18:01 +0000294}
295
blueswir181732d12007-10-06 11:25:43 +0000296static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
blueswir1115646b2007-10-07 10:00:55 +0000297 qemu_irq irq,
298 SLAVIO_TIMERState *master,
299 int slave_index)
bellarde80cfcf2004-12-19 23:18:01 +0000300{
301 int slavio_timer_io_memory;
302 SLAVIO_TIMERState *s;
blueswir18d05ea82007-05-24 19:48:41 +0000303 QEMUBH *bh;
bellarde80cfcf2004-12-19 23:18:01 +0000304
305 s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
306 if (!s)
blueswir181732d12007-10-06 11:25:43 +0000307 return s;
bellarde80cfcf2004-12-19 23:18:01 +0000308 s->irq = irq;
blueswir1115646b2007-10-07 10:00:55 +0000309 s->master = master;
310 s->slave_index = slave_index;
blueswir18d05ea82007-05-24 19:48:41 +0000311 bh = qemu_bh_new(slavio_timer_irq, s);
312 s->timer = ptimer_init(bh);
313 ptimer_set_period(s->timer, 500ULL);
bellarde80cfcf2004-12-19 23:18:01 +0000314
315 slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
blueswir1f930d072007-10-06 11:28:21 +0000316 slavio_timer_mem_write, s);
blueswir1115646b2007-10-07 10:00:55 +0000317 if (master)
blueswir181732d12007-10-06 11:25:43 +0000318 cpu_register_physical_memory(addr, CPU_TIMER_SIZE, slavio_timer_io_memory);
319 else
blueswir1115646b2007-10-07 10:00:55 +0000320 cpu_register_physical_memory(addr, SYS_TIMER_SIZE, slavio_timer_io_memory);
blueswir18d05ea82007-05-24 19:48:41 +0000321 register_savevm("slavio_timer", addr, 2, slavio_timer_save, slavio_timer_load, s);
bellarde80cfcf2004-12-19 23:18:01 +0000322 qemu_register_reset(slavio_timer_reset, s);
323 slavio_timer_reset(s);
blueswir181732d12007-10-06 11:25:43 +0000324
325 return s;
326}
327
328void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
329 qemu_irq *cpu_irqs)
330{
331 SLAVIO_TIMERState *master;
332 unsigned int i;
333
blueswir1115646b2007-10-07 10:00:55 +0000334 master = slavio_timer_init(base + 0x10000ULL, master_irq, NULL, 0);
blueswir181732d12007-10-06 11:25:43 +0000335
336 for (i = 0; i < MAX_CPUS; i++) {
337 master->slave[i] = slavio_timer_init(base + (target_phys_addr_t)
338 (i * TARGET_PAGE_SIZE),
blueswir1115646b2007-10-07 10:00:55 +0000339 cpu_irqs[i], master, i);
blueswir181732d12007-10-06 11:25:43 +0000340 }
bellarde80cfcf2004-12-19 23:18:01 +0000341}