Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU model of the Xilinx timer block. |
| 3 | * |
| 4 | * Copyright (c) 2009 Edgar E. Iglesias. |
| 5 | * |
| 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: |
| 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 | |
| 25 | #include "sysbus.h" |
Paolo Bonzini | 49d4d9b6 | 2012-01-13 17:07:19 +0100 | [diff] [blame] | 26 | #include "ptimer.h" |
Chris Wulff | 8354cd7 | 2012-09-09 20:20:07 -0400 | [diff] [blame] | 27 | #include "qemu-log.h" |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 28 | |
| 29 | #define D(x) |
| 30 | |
| 31 | #define R_TCSR 0 |
| 32 | #define R_TLR 1 |
| 33 | #define R_TCR 2 |
| 34 | #define R_MAX 4 |
| 35 | |
| 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) |
| 47 | |
| 48 | struct xlx_timer |
| 49 | { |
| 50 | QEMUBH *bh; |
| 51 | ptimer_state *ptimer; |
| 52 | void *parent; |
| 53 | int nr; /* for debug. */ |
| 54 | |
| 55 | unsigned long timer_div; |
| 56 | |
| 57 | uint32_t regs[R_MAX]; |
| 58 | }; |
| 59 | |
| 60 | struct timerblock |
| 61 | { |
| 62 | SysBusDevice busdev; |
Edgar E. Iglesias | 010f3f5 | 2011-08-26 00:13:47 +0200 | [diff] [blame] | 63 | MemoryRegion mmio; |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 64 | qemu_irq irq; |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 65 | uint8_t one_timer_only; |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 66 | uint32_t freq_hz; |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 67 | struct xlx_timer *timers; |
| 68 | }; |
| 69 | |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 70 | static inline unsigned int num_timers(struct timerblock *t) |
| 71 | { |
| 72 | return 2 - t->one_timer_only; |
| 73 | } |
| 74 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 75 | static inline unsigned int timer_from_addr(target_phys_addr_t addr) |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 76 | { |
| 77 | /* Timers get a 4x32bit control reg area each. */ |
| 78 | return addr >> 2; |
| 79 | } |
| 80 | |
| 81 | static void timer_update_irq(struct timerblock *t) |
| 82 | { |
| 83 | unsigned int i, irq = 0; |
| 84 | uint32_t csr; |
| 85 | |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 86 | for (i = 0; i < num_timers(t); i++) { |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 87 | csr = t->timers[i].regs[R_TCSR]; |
| 88 | irq |= (csr & TCSR_TINT) && (csr & TCSR_ENIT); |
| 89 | } |
| 90 | |
| 91 | /* All timers within the same slave share a single IRQ line. */ |
| 92 | qemu_set_irq(t->irq, !!irq); |
| 93 | } |
| 94 | |
Edgar E. Iglesias | 010f3f5 | 2011-08-26 00:13:47 +0200 | [diff] [blame] | 95 | static uint64_t |
| 96 | timer_read(void *opaque, target_phys_addr_t addr, unsigned int size) |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 97 | { |
| 98 | struct timerblock *t = opaque; |
| 99 | struct xlx_timer *xt; |
| 100 | uint32_t r = 0; |
| 101 | unsigned int timer; |
| 102 | |
| 103 | addr >>= 2; |
| 104 | timer = timer_from_addr(addr); |
| 105 | xt = &t->timers[timer]; |
| 106 | /* Further decoding to address a specific timers reg. */ |
| 107 | addr &= 0x3; |
| 108 | switch (addr) |
| 109 | { |
| 110 | case R_TCR: |
| 111 | r = ptimer_get_count(xt->ptimer); |
| 112 | if (!(xt->regs[R_TCSR] & TCSR_UDT)) |
| 113 | r = ~r; |
| 114 | D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n", |
| 115 | timer, r, xt->regs[R_TCSR] & TCSR_UDT)); |
| 116 | break; |
| 117 | default: |
| 118 | if (addr < ARRAY_SIZE(xt->regs)) |
| 119 | r = xt->regs[addr]; |
| 120 | break; |
| 121 | |
| 122 | } |
Peter A. G. Crosthwaite | e03377a | 2012-06-28 16:28:03 +1000 | [diff] [blame] | 123 | D(fprintf(stderr, "%s timer=%d %x=%x\n", __func__, timer, addr * 4, r)); |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 124 | return r; |
| 125 | } |
| 126 | |
| 127 | static void timer_enable(struct xlx_timer *xt) |
| 128 | { |
| 129 | uint64_t count; |
| 130 | |
Peter A. G. Crosthwaite | e03377a | 2012-06-28 16:28:03 +1000 | [diff] [blame] | 131 | D(fprintf(stderr, "%s timer=%d down=%d\n", __func__, |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 132 | xt->nr, xt->regs[R_TCSR] & TCSR_UDT)); |
| 133 | |
| 134 | ptimer_stop(xt->ptimer); |
| 135 | |
| 136 | if (xt->regs[R_TCSR] & TCSR_UDT) |
| 137 | count = xt->regs[R_TLR]; |
| 138 | else |
| 139 | count = ~0 - xt->regs[R_TLR]; |
Peter A. G. Crosthwaite | 7798a88 | 2012-06-16 15:20:59 +1000 | [diff] [blame] | 140 | ptimer_set_limit(xt->ptimer, count, 1); |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 141 | ptimer_run(xt->ptimer, 1); |
| 142 | } |
| 143 | |
| 144 | static void |
Edgar E. Iglesias | 010f3f5 | 2011-08-26 00:13:47 +0200 | [diff] [blame] | 145 | timer_write(void *opaque, target_phys_addr_t addr, |
| 146 | uint64_t val64, unsigned int size) |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 147 | { |
| 148 | struct timerblock *t = opaque; |
| 149 | struct xlx_timer *xt; |
| 150 | unsigned int timer; |
Edgar E. Iglesias | 010f3f5 | 2011-08-26 00:13:47 +0200 | [diff] [blame] | 151 | uint32_t value = val64; |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 152 | |
| 153 | addr >>= 2; |
| 154 | timer = timer_from_addr(addr); |
| 155 | xt = &t->timers[timer]; |
Peter A. G. Crosthwaite | e03377a | 2012-06-28 16:28:03 +1000 | [diff] [blame] | 156 | D(fprintf(stderr, "%s addr=%x val=%x (timer=%d off=%d)\n", |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 157 | __func__, addr * 4, value, timer, addr & 3)); |
| 158 | /* Further decoding to address a specific timers reg. */ |
| 159 | addr &= 3; |
| 160 | switch (addr) |
| 161 | { |
| 162 | case R_TCSR: |
| 163 | if (value & TCSR_TINT) |
| 164 | value &= ~TCSR_TINT; |
| 165 | |
| 166 | xt->regs[addr] = value; |
| 167 | if (value & TCSR_ENT) |
| 168 | timer_enable(xt); |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | if (addr < ARRAY_SIZE(xt->regs)) |
| 173 | xt->regs[addr] = value; |
| 174 | break; |
| 175 | } |
| 176 | timer_update_irq(t); |
| 177 | } |
| 178 | |
Edgar E. Iglesias | 010f3f5 | 2011-08-26 00:13:47 +0200 | [diff] [blame] | 179 | static const MemoryRegionOps timer_ops = { |
| 180 | .read = timer_read, |
| 181 | .write = timer_write, |
| 182 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 183 | .valid = { |
| 184 | .min_access_size = 4, |
| 185 | .max_access_size = 4 |
| 186 | } |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | static void timer_hit(void *opaque) |
| 190 | { |
| 191 | struct xlx_timer *xt = opaque; |
| 192 | struct timerblock *t = xt->parent; |
Chris Wulff | 8354cd7 | 2012-09-09 20:20:07 -0400 | [diff] [blame] | 193 | D(fprintf(stderr, "%s %d\n", __func__, xt->nr)); |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 194 | xt->regs[R_TCSR] |= TCSR_TINT; |
| 195 | |
| 196 | if (xt->regs[R_TCSR] & TCSR_ARHT) |
| 197 | timer_enable(xt); |
| 198 | timer_update_irq(t); |
| 199 | } |
| 200 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 201 | static int xilinx_timer_init(SysBusDevice *dev) |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 202 | { |
| 203 | struct timerblock *t = FROM_SYSBUS(typeof (*t), dev); |
| 204 | unsigned int i; |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 205 | |
| 206 | /* All timers share a single irq line. */ |
| 207 | sysbus_init_irq(dev, &t->irq); |
| 208 | |
| 209 | /* Init all the ptimers. */ |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 210 | t->timers = g_malloc0(sizeof t->timers[0] * num_timers(t)); |
| 211 | for (i = 0; i < num_timers(t); i++) { |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 212 | struct xlx_timer *xt = &t->timers[i]; |
| 213 | |
| 214 | xt->parent = t; |
| 215 | xt->nr = i; |
| 216 | xt->bh = qemu_bh_new(timer_hit, xt); |
| 217 | xt->ptimer = ptimer_init(xt->bh); |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 218 | ptimer_set_freq(xt->ptimer, t->freq_hz); |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Peter A. G. Crosthwaite | c0a1dcb | 2012-06-28 12:52:23 +1000 | [diff] [blame] | 221 | memory_region_init_io(&t->mmio, &timer_ops, t, "xlnx.xps-timer", |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 222 | R_MAX * 4 * num_timers(t)); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 223 | sysbus_init_mmio(dev, &t->mmio); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 224 | return 0; |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 227 | static Property xilinx_timer_properties[] = { |
Peter A. G. Crosthwaite | 919f89f | 2012-06-29 13:20:46 +1000 | [diff] [blame] | 228 | DEFINE_PROP_UINT32("clock-frequency", struct timerblock, freq_hz, |
| 229 | 62 * 1000000), |
Peter A. G. Crosthwaite | abe098e | 2012-06-13 14:46:43 +1000 | [diff] [blame] | 230 | DEFINE_PROP_UINT8("one-timer-only", struct timerblock, one_timer_only, 0), |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 231 | DEFINE_PROP_END_OF_LIST(), |
| 232 | }; |
| 233 | |
| 234 | static void xilinx_timer_class_init(ObjectClass *klass, void *data) |
| 235 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 236 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 237 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
| 238 | |
| 239 | k->init = xilinx_timer_init; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 240 | dc->props = xilinx_timer_properties; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 241 | } |
| 242 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 243 | static TypeInfo xilinx_timer_info = { |
Peter A. G. Crosthwaite | c0a1dcb | 2012-06-28 12:52:23 +1000 | [diff] [blame] | 244 | .name = "xlnx.xps-timer", |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 245 | .parent = TYPE_SYS_BUS_DEVICE, |
| 246 | .instance_size = sizeof(struct timerblock), |
| 247 | .class_init = xilinx_timer_class_init, |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 248 | }; |
| 249 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 250 | static void xilinx_timer_register_types(void) |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 251 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 252 | type_register_static(&xilinx_timer_info); |
Edgar E. Iglesias | 388f60b | 2009-05-20 20:11:44 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 255 | type_init(xilinx_timer_register_types) |