2 * QEMU model of the Xilinx timer block.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * 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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
36 #define TCSR_MDT (1<<0)
37 #define TCSR_UDT (1<<1)
38 #define TCSR_GENT (1<<2)
39 #define TCSR_CAPT (1<<3)
40 #define TCSR_ARHT (1<<4)
41 #define TCSR_LOAD (1<<5)
42 #define TCSR_ENIT (1<<6)
43 #define TCSR_ENT (1<<7)
44 #define TCSR_TINT (1<<8)
45 #define TCSR_PWMA (1<<9)
46 #define TCSR_ENALL (1<<10)
53 int nr
; /* for debug. */
55 unsigned long timer_div
;
65 uint8_t one_timer_only
;
67 struct xlx_timer
*timers
;
70 static inline unsigned int num_timers(struct timerblock
*t
)
72 return 2 - t
->one_timer_only
;
75 static inline unsigned int timer_from_addr(target_phys_addr_t addr
)
77 /* Timers get a 4x32bit control reg area each. */
81 static void timer_update_irq(struct timerblock
*t
)
83 unsigned int i
, irq
= 0;
86 for (i
= 0; i
< num_timers(t
); i
++) {
87 csr
= t
->timers
[i
].regs
[R_TCSR
];
88 irq
|= (csr
& TCSR_TINT
) && (csr
& TCSR_ENIT
);
91 /* All timers within the same slave share a single IRQ line. */
92 qemu_set_irq(t
->irq
, !!irq
);
96 timer_read(void *opaque
, target_phys_addr_t addr
, unsigned int size
)
98 struct timerblock
*t
= opaque
;
104 timer
= timer_from_addr(addr
);
105 xt
= &t
->timers
[timer
];
106 /* Further decoding to address a specific timers reg. */
111 r
= ptimer_get_count(xt
->ptimer
);
112 if (!(xt
->regs
[R_TCSR
] & TCSR_UDT
))
114 D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n",
115 timer
, r
, xt
->regs
[R_TCSR
] & TCSR_UDT
));
118 if (addr
< ARRAY_SIZE(xt
->regs
))
123 D(fprintf(stderr
, "%s timer=%d %x=%x\n", __func__
, timer
, addr
* 4, r
));
127 static void timer_enable(struct xlx_timer
*xt
)
131 D(fprintf(stderr
, "%s timer=%d down=%d\n", __func__
,
132 xt
->nr
, xt
->regs
[R_TCSR
] & TCSR_UDT
));
134 ptimer_stop(xt
->ptimer
);
136 if (xt
->regs
[R_TCSR
] & TCSR_UDT
)
137 count
= xt
->regs
[R_TLR
];
139 count
= ~0 - xt
->regs
[R_TLR
];
140 ptimer_set_limit(xt
->ptimer
, count
, 1);
141 ptimer_run(xt
->ptimer
, 1);
145 timer_write(void *opaque
, target_phys_addr_t addr
,
146 uint64_t val64
, unsigned int size
)
148 struct timerblock
*t
= opaque
;
149 struct xlx_timer
*xt
;
151 uint32_t value
= val64
;
154 timer
= timer_from_addr(addr
);
155 xt
= &t
->timers
[timer
];
156 D(fprintf(stderr
, "%s addr=%x val=%x (timer=%d off=%d)\n",
157 __func__
, addr
* 4, value
, timer
, addr
& 3));
158 /* Further decoding to address a specific timers reg. */
163 if (value
& TCSR_TINT
)
166 xt
->regs
[addr
] = value
;
167 if (value
& TCSR_ENT
)
172 if (addr
< ARRAY_SIZE(xt
->regs
))
173 xt
->regs
[addr
] = value
;
179 static const MemoryRegionOps timer_ops
= {
181 .write
= timer_write
,
182 .endianness
= DEVICE_NATIVE_ENDIAN
,
184 .min_access_size
= 4,
189 static void timer_hit(void *opaque
)
191 struct xlx_timer
*xt
= opaque
;
192 struct timerblock
*t
= xt
->parent
;
193 D(fprintf(stderr
, "%s %d\n", __func__
, xt
->nr
));
194 xt
->regs
[R_TCSR
] |= TCSR_TINT
;
196 if (xt
->regs
[R_TCSR
] & TCSR_ARHT
)
201 static int xilinx_timer_init(SysBusDevice
*dev
)
203 struct timerblock
*t
= FROM_SYSBUS(typeof (*t
), dev
);
206 /* All timers share a single irq line. */
207 sysbus_init_irq(dev
, &t
->irq
);
209 /* Init all the ptimers. */
210 t
->timers
= g_malloc0(sizeof t
->timers
[0] * num_timers(t
));
211 for (i
= 0; i
< num_timers(t
); i
++) {
212 struct xlx_timer
*xt
= &t
->timers
[i
];
216 xt
->bh
= qemu_bh_new(timer_hit
, xt
);
217 xt
->ptimer
= ptimer_init(xt
->bh
);
218 ptimer_set_freq(xt
->ptimer
, t
->freq_hz
);
221 memory_region_init_io(&t
->mmio
, &timer_ops
, t
, "xlnx.xps-timer",
222 R_MAX
* 4 * num_timers(t
));
223 sysbus_init_mmio(dev
, &t
->mmio
);
227 static Property xilinx_timer_properties
[] = {
228 DEFINE_PROP_UINT32("clock-frequency", struct timerblock
, freq_hz
,
230 DEFINE_PROP_UINT8("one-timer-only", struct timerblock
, one_timer_only
, 0),
231 DEFINE_PROP_END_OF_LIST(),
234 static void xilinx_timer_class_init(ObjectClass
*klass
, void *data
)
236 DeviceClass
*dc
= DEVICE_CLASS(klass
);
237 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
239 k
->init
= xilinx_timer_init
;
240 dc
->props
= xilinx_timer_properties
;
243 static TypeInfo xilinx_timer_info
= {
244 .name
= "xlnx.xps-timer",
245 .parent
= TYPE_SYS_BUS_DEVICE
,
246 .instance_size
= sizeof(struct timerblock
),
247 .class_init
= xilinx_timer_class_init
,
250 static void xilinx_timer_register_types(void)
252 type_register_static(&xilinx_timer_info
);
255 type_init(xilinx_timer_register_types
)