root / hw / pl031.c @ 4487fd34
History | View | Annotate | Download (5.3 kB)
1 | 7e1543c2 | pbrook | /*
|
---|---|---|---|
2 | 7e1543c2 | pbrook | * ARM AMBA PrimeCell PL031 RTC
|
3 | 7e1543c2 | pbrook | *
|
4 | 7e1543c2 | pbrook | * Copyright (c) 2007 CodeSourcery
|
5 | 7e1543c2 | pbrook | *
|
6 | 7e1543c2 | pbrook | * This file is free software; you can redistribute it and/or modify
|
7 | 7e1543c2 | pbrook | * it under the terms of the GNU General Public License version 2 as
|
8 | 7e1543c2 | pbrook | * published by the Free Software Foundation.
|
9 | 7e1543c2 | pbrook | *
|
10 | 7e1543c2 | pbrook | */
|
11 | 7e1543c2 | pbrook | |
12 | 87ecb68b | pbrook | #include "hw.h" |
13 | 87ecb68b | pbrook | #include "primecell.h" |
14 | 87ecb68b | pbrook | #include "qemu-timer.h" |
15 | 87ecb68b | pbrook | #include "sysemu.h" |
16 | 7e1543c2 | pbrook | |
17 | 7e1543c2 | pbrook | //#define DEBUG_PL031
|
18 | 7e1543c2 | pbrook | |
19 | 7e1543c2 | pbrook | #ifdef DEBUG_PL031
|
20 | 7e1543c2 | pbrook | #define DPRINTF(fmt, args...) \
|
21 | 7e1543c2 | pbrook | do { printf("pl031: " fmt , ##args); } while (0) |
22 | 7e1543c2 | pbrook | #else
|
23 | 7e1543c2 | pbrook | #define DPRINTF(fmt, args...) do {} while(0) |
24 | 7e1543c2 | pbrook | #endif
|
25 | 7e1543c2 | pbrook | |
26 | 7e1543c2 | pbrook | #define RTC_DR 0x00 /* Data read register */ |
27 | 7e1543c2 | pbrook | #define RTC_MR 0x04 /* Match register */ |
28 | 7e1543c2 | pbrook | #define RTC_LR 0x08 /* Data load register */ |
29 | 7e1543c2 | pbrook | #define RTC_CR 0x0c /* Control register */ |
30 | 7e1543c2 | pbrook | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
31 | 7e1543c2 | pbrook | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
32 | 7e1543c2 | pbrook | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
33 | 7e1543c2 | pbrook | #define RTC_ICR 0x1c /* Interrupt clear register */ |
34 | 7e1543c2 | pbrook | |
35 | 7e1543c2 | pbrook | typedef struct { |
36 | 7e1543c2 | pbrook | QEMUTimer *timer; |
37 | 7e1543c2 | pbrook | qemu_irq irq; |
38 | 7e1543c2 | pbrook | uint32_t base; |
39 | 7e1543c2 | pbrook | |
40 | 7e1543c2 | pbrook | uint64_t start_time; |
41 | 7e1543c2 | pbrook | uint32_t tick_offset; |
42 | 7e1543c2 | pbrook | |
43 | 7e1543c2 | pbrook | uint32_t mr; |
44 | 7e1543c2 | pbrook | uint32_t lr; |
45 | 7e1543c2 | pbrook | uint32_t cr; |
46 | 7e1543c2 | pbrook | uint32_t im; |
47 | 7e1543c2 | pbrook | uint32_t is; |
48 | 7e1543c2 | pbrook | } pl031_state; |
49 | 7e1543c2 | pbrook | |
50 | 7e1543c2 | pbrook | static const unsigned char pl031_id[] = { |
51 | 7e1543c2 | pbrook | 0x31, 0x10, 0x14, 0x00, /* Device ID */ |
52 | 7e1543c2 | pbrook | 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */ |
53 | 7e1543c2 | pbrook | }; |
54 | 7e1543c2 | pbrook | |
55 | 7e1543c2 | pbrook | static void pl031_update(pl031_state *s) |
56 | 7e1543c2 | pbrook | { |
57 | 7e1543c2 | pbrook | qemu_set_irq(s->irq, s->is & s->im); |
58 | 7e1543c2 | pbrook | } |
59 | 7e1543c2 | pbrook | |
60 | 7e1543c2 | pbrook | static void pl031_interrupt(void * opaque) |
61 | 7e1543c2 | pbrook | { |
62 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
63 | 7e1543c2 | pbrook | |
64 | 7e1543c2 | pbrook | s->im = 1;
|
65 | 7e1543c2 | pbrook | DPRINTF("Alarm raised\n");
|
66 | 7e1543c2 | pbrook | pl031_update(s); |
67 | 7e1543c2 | pbrook | } |
68 | 7e1543c2 | pbrook | |
69 | 7e1543c2 | pbrook | static uint32_t pl031_get_count(pl031_state *s)
|
70 | 7e1543c2 | pbrook | { |
71 | 7e1543c2 | pbrook | /* This assumes qemu_get_clock returns the time since the machine was
|
72 | 7e1543c2 | pbrook | created. */
|
73 | 7e1543c2 | pbrook | return s->tick_offset + qemu_get_clock(vm_clock) / ticks_per_sec;
|
74 | 7e1543c2 | pbrook | } |
75 | 7e1543c2 | pbrook | |
76 | 7e1543c2 | pbrook | static void pl031_set_alarm(pl031_state *s) |
77 | 7e1543c2 | pbrook | { |
78 | 7e1543c2 | pbrook | int64_t now; |
79 | 7e1543c2 | pbrook | uint32_t ticks; |
80 | 7e1543c2 | pbrook | |
81 | 7e1543c2 | pbrook | now = qemu_get_clock(vm_clock); |
82 | 7e1543c2 | pbrook | ticks = s->tick_offset + now / ticks_per_sec; |
83 | 7e1543c2 | pbrook | |
84 | 7e1543c2 | pbrook | /* The timer wraps around. This subtraction also wraps in the same way,
|
85 | 7e1543c2 | pbrook | and gives correct results when alarm < now_ticks. */
|
86 | 7e1543c2 | pbrook | ticks = s->mr - ticks; |
87 | 7e1543c2 | pbrook | DPRINTF("Alarm set in %ud ticks\n", ticks);
|
88 | 7e1543c2 | pbrook | if (ticks == 0) { |
89 | 7e1543c2 | pbrook | qemu_del_timer(s->timer); |
90 | 7e1543c2 | pbrook | pl031_interrupt(s); |
91 | 7e1543c2 | pbrook | } else {
|
92 | 7e1543c2 | pbrook | qemu_mod_timer(s->timer, now + (int64_t)ticks * ticks_per_sec); |
93 | 7e1543c2 | pbrook | } |
94 | 7e1543c2 | pbrook | } |
95 | 7e1543c2 | pbrook | |
96 | 7e1543c2 | pbrook | static uint32_t pl031_read(void *opaque, target_phys_addr_t offset) |
97 | 7e1543c2 | pbrook | { |
98 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
99 | 7e1543c2 | pbrook | |
100 | 7e1543c2 | pbrook | offset -= s->base; |
101 | 7e1543c2 | pbrook | |
102 | 7e1543c2 | pbrook | if (offset >= 0xfe0 && offset < 0x1000) |
103 | 7e1543c2 | pbrook | return pl031_id[(offset - 0xfe0) >> 2]; |
104 | 7e1543c2 | pbrook | |
105 | 7e1543c2 | pbrook | switch (offset) {
|
106 | 7e1543c2 | pbrook | case RTC_DR:
|
107 | 7e1543c2 | pbrook | return pl031_get_count(s);
|
108 | 7e1543c2 | pbrook | case RTC_MR:
|
109 | 7e1543c2 | pbrook | return s->mr;
|
110 | 7e1543c2 | pbrook | case RTC_IMSC:
|
111 | 7e1543c2 | pbrook | return s->im;
|
112 | 7e1543c2 | pbrook | case RTC_RIS:
|
113 | 7e1543c2 | pbrook | return s->is;
|
114 | 7e1543c2 | pbrook | case RTC_LR:
|
115 | 7e1543c2 | pbrook | return s->lr;
|
116 | 7e1543c2 | pbrook | case RTC_CR:
|
117 | 7e1543c2 | pbrook | /* RTC is permanently enabled. */
|
118 | 7e1543c2 | pbrook | return 1; |
119 | 7e1543c2 | pbrook | case RTC_MIS:
|
120 | 7e1543c2 | pbrook | return s->is & s->im;
|
121 | 7e1543c2 | pbrook | case RTC_ICR:
|
122 | 7e1543c2 | pbrook | fprintf(stderr, "qemu: pl031_read: Unexpected offset 0x%x\n",
|
123 | 7e1543c2 | pbrook | (int)offset);
|
124 | 7e1543c2 | pbrook | break;
|
125 | 7e1543c2 | pbrook | default:
|
126 | 7e1543c2 | pbrook | cpu_abort(cpu_single_env, "pl031_read: Bad offset 0x%x\n",
|
127 | 7e1543c2 | pbrook | (int)offset);
|
128 | 7e1543c2 | pbrook | break;
|
129 | 7e1543c2 | pbrook | } |
130 | 7e1543c2 | pbrook | |
131 | 7e1543c2 | pbrook | return 0; |
132 | 7e1543c2 | pbrook | } |
133 | 7e1543c2 | pbrook | |
134 | 7e1543c2 | pbrook | static void pl031_write(void * opaque, target_phys_addr_t offset, |
135 | 7e1543c2 | pbrook | uint32_t value) |
136 | 7e1543c2 | pbrook | { |
137 | 7e1543c2 | pbrook | pl031_state *s = (pl031_state *)opaque; |
138 | 7e1543c2 | pbrook | |
139 | 7e1543c2 | pbrook | offset -= s->base; |
140 | 7e1543c2 | pbrook | |
141 | 7e1543c2 | pbrook | switch (offset) {
|
142 | 7e1543c2 | pbrook | case RTC_LR:
|
143 | 7e1543c2 | pbrook | s->tick_offset += value - pl031_get_count(s); |
144 | 7e1543c2 | pbrook | pl031_set_alarm(s); |
145 | 7e1543c2 | pbrook | break;
|
146 | 7e1543c2 | pbrook | case RTC_MR:
|
147 | 7e1543c2 | pbrook | s->mr = value; |
148 | 7e1543c2 | pbrook | pl031_set_alarm(s); |
149 | 7e1543c2 | pbrook | break;
|
150 | 7e1543c2 | pbrook | case RTC_IMSC:
|
151 | 7e1543c2 | pbrook | s->im = value & 1;
|
152 | 7e1543c2 | pbrook | DPRINTF("Interrupt mask %d\n", s->im);
|
153 | 7e1543c2 | pbrook | pl031_update(s); |
154 | 7e1543c2 | pbrook | break;
|
155 | 7e1543c2 | pbrook | case RTC_ICR:
|
156 | 7e1543c2 | pbrook | /* The PL031 documentation (DDI0224B) states that the interupt is
|
157 | 7e1543c2 | pbrook | cleared when bit 0 of the written value is set. However the
|
158 | 7e1543c2 | pbrook | arm926e documentation (DDI0287B) states that the interrupt is
|
159 | 7e1543c2 | pbrook | cleared when any value is written. */
|
160 | 7e1543c2 | pbrook | DPRINTF("Interrupt cleared");
|
161 | 7e1543c2 | pbrook | s->is = 0;
|
162 | 7e1543c2 | pbrook | pl031_update(s); |
163 | 7e1543c2 | pbrook | break;
|
164 | 7e1543c2 | pbrook | case RTC_CR:
|
165 | 7e1543c2 | pbrook | /* Written value is ignored. */
|
166 | 7e1543c2 | pbrook | break;
|
167 | 7e1543c2 | pbrook | |
168 | 7e1543c2 | pbrook | case RTC_DR:
|
169 | 7e1543c2 | pbrook | case RTC_MIS:
|
170 | 7e1543c2 | pbrook | case RTC_RIS:
|
171 | 7e1543c2 | pbrook | fprintf(stderr, "qemu: pl031_write: Unexpected offset 0x%x\n",
|
172 | 7e1543c2 | pbrook | (int)offset);
|
173 | 7e1543c2 | pbrook | break;
|
174 | 7e1543c2 | pbrook | |
175 | 7e1543c2 | pbrook | default:
|
176 | 7e1543c2 | pbrook | cpu_abort(cpu_single_env, "pl031_write: Bad offset 0x%x\n",
|
177 | 7e1543c2 | pbrook | (int)offset);
|
178 | 7e1543c2 | pbrook | break;
|
179 | 7e1543c2 | pbrook | } |
180 | 7e1543c2 | pbrook | } |
181 | 7e1543c2 | pbrook | |
182 | 7e1543c2 | pbrook | static CPUWriteMemoryFunc * pl031_writefn[] = {
|
183 | 7e1543c2 | pbrook | pl031_write, |
184 | 7e1543c2 | pbrook | pl031_write, |
185 | 7e1543c2 | pbrook | pl031_write |
186 | 7e1543c2 | pbrook | }; |
187 | 7e1543c2 | pbrook | |
188 | 7e1543c2 | pbrook | static CPUReadMemoryFunc * pl031_readfn[] = {
|
189 | 7e1543c2 | pbrook | pl031_read, |
190 | 7e1543c2 | pbrook | pl031_read, |
191 | 7e1543c2 | pbrook | pl031_read |
192 | 7e1543c2 | pbrook | }; |
193 | 7e1543c2 | pbrook | |
194 | 7e1543c2 | pbrook | void pl031_init(uint32_t base, qemu_irq irq)
|
195 | 7e1543c2 | pbrook | { |
196 | 7e1543c2 | pbrook | int iomemtype;
|
197 | 7e1543c2 | pbrook | pl031_state *s; |
198 | f6503059 | balrog | struct tm tm;
|
199 | 7e1543c2 | pbrook | |
200 | 7e1543c2 | pbrook | s = qemu_mallocz(sizeof(pl031_state));
|
201 | 7e1543c2 | pbrook | if (!s)
|
202 | 7e1543c2 | pbrook | cpu_abort(cpu_single_env, "pl031_init: Out of memory\n");
|
203 | 7e1543c2 | pbrook | |
204 | 7e1543c2 | pbrook | iomemtype = cpu_register_io_memory(0, pl031_readfn, pl031_writefn, s);
|
205 | 7e1543c2 | pbrook | if (iomemtype == -1) |
206 | 7e1543c2 | pbrook | cpu_abort(cpu_single_env, "pl031_init: Can't register I/O memory\n");
|
207 | 7e1543c2 | pbrook | |
208 | 7e1543c2 | pbrook | cpu_register_physical_memory(base, 0x00001000, iomemtype);
|
209 | 7e1543c2 | pbrook | |
210 | 7e1543c2 | pbrook | s->base = base; |
211 | 7e1543c2 | pbrook | s->irq = irq; |
212 | 7e1543c2 | pbrook | /* ??? We assume vm_clock is zero at this point. */
|
213 | f6503059 | balrog | qemu_get_timedate(&tm, 0);
|
214 | f6503059 | balrog | s->tick_offset = mktime(&tm); |
215 | 7e1543c2 | pbrook | |
216 | 7e1543c2 | pbrook | s->timer = qemu_new_timer(vm_clock, pl031_interrupt, s); |
217 | 7e1543c2 | pbrook | } |