root / hw / pl061.c @ c75a823c
History | View | Annotate | Download (6.2 kB)
1 | 9ee6e8bb | pbrook | /*
|
---|---|---|---|
2 | 9ee6e8bb | pbrook | * Arm PrimeCell PL061 General Purpose IO with additional
|
3 | 9ee6e8bb | pbrook | * Luminary Micro Stellaris bits.
|
4 | 9ee6e8bb | pbrook | *
|
5 | 9ee6e8bb | pbrook | * Copyright (c) 2007 CodeSourcery.
|
6 | 9ee6e8bb | pbrook | * Written by Paul Brook
|
7 | 9ee6e8bb | pbrook | *
|
8 | 9ee6e8bb | pbrook | * This code is licenced under the GPL.
|
9 | 9ee6e8bb | pbrook | */
|
10 | 9ee6e8bb | pbrook | |
11 | 87ecb68b | pbrook | #include "hw.h" |
12 | 87ecb68b | pbrook | #include "primecell.h" |
13 | 9ee6e8bb | pbrook | |
14 | 9ee6e8bb | pbrook | //#define DEBUG_PL061 1
|
15 | 9ee6e8bb | pbrook | |
16 | 9ee6e8bb | pbrook | #ifdef DEBUG_PL061
|
17 | 9ee6e8bb | pbrook | #define DPRINTF(fmt, args...) \
|
18 | 9ee6e8bb | pbrook | do { printf("pl061: " fmt , ##args); } while (0) |
19 | 9ee6e8bb | pbrook | #define BADF(fmt, args...) \
|
20 | 9ee6e8bb | pbrook | do { fprintf(stderr, "pl061: error: " fmt , ##args); exit(1);} while (0) |
21 | 9ee6e8bb | pbrook | #else
|
22 | 9ee6e8bb | pbrook | #define DPRINTF(fmt, args...) do {} while(0) |
23 | 9ee6e8bb | pbrook | #define BADF(fmt, args...) \
|
24 | 9ee6e8bb | pbrook | do { fprintf(stderr, "pl061: error: " fmt , ##args);} while (0) |
25 | 9ee6e8bb | pbrook | #endif
|
26 | 9ee6e8bb | pbrook | |
27 | 9ee6e8bb | pbrook | static const uint8_t pl061_id[12] = |
28 | 9ee6e8bb | pbrook | { 0x00, 0x00, 0x00, 0x00, 0x61, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 }; |
29 | 9ee6e8bb | pbrook | |
30 | 9ee6e8bb | pbrook | typedef struct { |
31 | 9ee6e8bb | pbrook | uint32_t base; |
32 | 9ee6e8bb | pbrook | int locked;
|
33 | 9ee6e8bb | pbrook | uint8_t data; |
34 | 9ee6e8bb | pbrook | uint8_t old_data; |
35 | 9ee6e8bb | pbrook | uint8_t dir; |
36 | 9ee6e8bb | pbrook | uint8_t isense; |
37 | 9ee6e8bb | pbrook | uint8_t ibe; |
38 | 9ee6e8bb | pbrook | uint8_t iev; |
39 | 9ee6e8bb | pbrook | uint8_t im; |
40 | 9ee6e8bb | pbrook | uint8_t istate; |
41 | 9ee6e8bb | pbrook | uint8_t afsel; |
42 | 9ee6e8bb | pbrook | uint8_t dr2r; |
43 | 9ee6e8bb | pbrook | uint8_t dr4r; |
44 | 9ee6e8bb | pbrook | uint8_t dr8r; |
45 | 9ee6e8bb | pbrook | uint8_t odr; |
46 | 9ee6e8bb | pbrook | uint8_t pur; |
47 | 9ee6e8bb | pbrook | uint8_t pdr; |
48 | 9ee6e8bb | pbrook | uint8_t slr; |
49 | 9ee6e8bb | pbrook | uint8_t den; |
50 | 9ee6e8bb | pbrook | uint8_t cr; |
51 | 775616c3 | pbrook | uint8_t float_high; |
52 | 9ee6e8bb | pbrook | qemu_irq irq; |
53 | 9ee6e8bb | pbrook | qemu_irq out[8];
|
54 | 9ee6e8bb | pbrook | } pl061_state; |
55 | 9ee6e8bb | pbrook | |
56 | 9ee6e8bb | pbrook | static void pl061_update(pl061_state *s) |
57 | 9ee6e8bb | pbrook | { |
58 | 9ee6e8bb | pbrook | uint8_t changed; |
59 | 9ee6e8bb | pbrook | uint8_t mask; |
60 | 775616c3 | pbrook | uint8_t out; |
61 | 9ee6e8bb | pbrook | int i;
|
62 | 9ee6e8bb | pbrook | |
63 | 775616c3 | pbrook | /* Outputs float high. */
|
64 | 775616c3 | pbrook | /* FIXME: This is board dependent. */
|
65 | 775616c3 | pbrook | out = (s->data & s->dir) | ~s->dir; |
66 | 775616c3 | pbrook | changed = s->old_data ^ out; |
67 | 9ee6e8bb | pbrook | if (!changed)
|
68 | 9ee6e8bb | pbrook | return;
|
69 | 9ee6e8bb | pbrook | |
70 | 775616c3 | pbrook | s->old_data = out; |
71 | 9ee6e8bb | pbrook | for (i = 0; i < 8; i++) { |
72 | 9ee6e8bb | pbrook | mask = 1 << i;
|
73 | 775616c3 | pbrook | if ((changed & mask) && s->out) {
|
74 | 775616c3 | pbrook | DPRINTF("Set output %d = %d\n", i, (out & mask) != 0); |
75 | 775616c3 | pbrook | qemu_set_irq(s->out[i], (out & mask) != 0);
|
76 | 9ee6e8bb | pbrook | } |
77 | 9ee6e8bb | pbrook | } |
78 | 9ee6e8bb | pbrook | |
79 | 9ee6e8bb | pbrook | /* FIXME: Implement input interrupts. */
|
80 | 9ee6e8bb | pbrook | } |
81 | 9ee6e8bb | pbrook | |
82 | 9ee6e8bb | pbrook | static uint32_t pl061_read(void *opaque, target_phys_addr_t offset) |
83 | 9ee6e8bb | pbrook | { |
84 | 9ee6e8bb | pbrook | pl061_state *s = (pl061_state *)opaque; |
85 | 9ee6e8bb | pbrook | |
86 | 9ee6e8bb | pbrook | offset -= s->base; |
87 | 9ee6e8bb | pbrook | if (offset >= 0xfd0 && offset < 0x1000) { |
88 | 9ee6e8bb | pbrook | return pl061_id[(offset - 0xfd0) >> 2]; |
89 | 9ee6e8bb | pbrook | } |
90 | 9ee6e8bb | pbrook | if (offset < 0x400) { |
91 | 9ee6e8bb | pbrook | return s->data & (offset >> 2); |
92 | 9ee6e8bb | pbrook | } |
93 | 9ee6e8bb | pbrook | switch (offset) {
|
94 | 9ee6e8bb | pbrook | case 0x400: /* Direction */ |
95 | 9ee6e8bb | pbrook | return s->dir;
|
96 | 9ee6e8bb | pbrook | case 0x404: /* Interrupt sense */ |
97 | 9ee6e8bb | pbrook | return s->isense;
|
98 | 9ee6e8bb | pbrook | case 0x408: /* Interrupt both edges */ |
99 | 9ee6e8bb | pbrook | return s->ibe;
|
100 | 9ee6e8bb | pbrook | case 0x40c: /* Interupt event */ |
101 | 9ee6e8bb | pbrook | return s->iev;
|
102 | 9ee6e8bb | pbrook | case 0x410: /* Interrupt mask */ |
103 | 9ee6e8bb | pbrook | return s->im;
|
104 | 9ee6e8bb | pbrook | case 0x414: /* Raw interrupt status */ |
105 | 9ee6e8bb | pbrook | return s->istate;
|
106 | 9ee6e8bb | pbrook | case 0x418: /* Masked interrupt status */ |
107 | 9ee6e8bb | pbrook | return s->istate | s->im;
|
108 | 9ee6e8bb | pbrook | case 0x420: /* Alternate function select */ |
109 | 9ee6e8bb | pbrook | return s->afsel;
|
110 | 9ee6e8bb | pbrook | case 0x500: /* 2mA drive */ |
111 | 9ee6e8bb | pbrook | return s->dr2r;
|
112 | 9ee6e8bb | pbrook | case 0x504: /* 4mA drive */ |
113 | 9ee6e8bb | pbrook | return s->dr4r;
|
114 | 9ee6e8bb | pbrook | case 0x508: /* 8mA drive */ |
115 | 9ee6e8bb | pbrook | return s->dr8r;
|
116 | 9ee6e8bb | pbrook | case 0x50c: /* Open drain */ |
117 | 9ee6e8bb | pbrook | return s->odr;
|
118 | 9ee6e8bb | pbrook | case 0x510: /* Pull-up */ |
119 | 9ee6e8bb | pbrook | return s->pur;
|
120 | 9ee6e8bb | pbrook | case 0x514: /* Pull-down */ |
121 | 9ee6e8bb | pbrook | return s->pdr;
|
122 | 9ee6e8bb | pbrook | case 0x518: /* Slew rate control */ |
123 | 9ee6e8bb | pbrook | return s->slr;
|
124 | 9ee6e8bb | pbrook | case 0x51c: /* Digital enable */ |
125 | 9ee6e8bb | pbrook | return s->den;
|
126 | 9ee6e8bb | pbrook | case 0x520: /* Lock */ |
127 | 9ee6e8bb | pbrook | return s->locked;
|
128 | 9ee6e8bb | pbrook | case 0x524: /* Commit */ |
129 | 9ee6e8bb | pbrook | return s->cr;
|
130 | 9ee6e8bb | pbrook | default:
|
131 | 9ee6e8bb | pbrook | cpu_abort (cpu_single_env, "pl061_read: Bad offset %x\n",
|
132 | 9ee6e8bb | pbrook | (int)offset);
|
133 | 9ee6e8bb | pbrook | return 0; |
134 | 9ee6e8bb | pbrook | } |
135 | 9ee6e8bb | pbrook | } |
136 | 9ee6e8bb | pbrook | |
137 | 9ee6e8bb | pbrook | static void pl061_write(void *opaque, target_phys_addr_t offset, |
138 | 9ee6e8bb | pbrook | uint32_t value) |
139 | 9ee6e8bb | pbrook | { |
140 | 9ee6e8bb | pbrook | pl061_state *s = (pl061_state *)opaque; |
141 | 9ee6e8bb | pbrook | uint8_t mask; |
142 | 9ee6e8bb | pbrook | |
143 | 9ee6e8bb | pbrook | offset -= s->base; |
144 | 9ee6e8bb | pbrook | if (offset < 0x400) { |
145 | 9ee6e8bb | pbrook | mask = (offset >> 2) & s->dir;
|
146 | 9ee6e8bb | pbrook | s->data = (s->data & ~mask) | (value & mask); |
147 | 9ee6e8bb | pbrook | pl061_update(s); |
148 | 9ee6e8bb | pbrook | return;
|
149 | 9ee6e8bb | pbrook | } |
150 | 9ee6e8bb | pbrook | switch (offset) {
|
151 | 9ee6e8bb | pbrook | case 0x400: /* Direction */ |
152 | 9ee6e8bb | pbrook | s->dir = value; |
153 | 9ee6e8bb | pbrook | break;
|
154 | 9ee6e8bb | pbrook | case 0x404: /* Interrupt sense */ |
155 | 9ee6e8bb | pbrook | s->isense = value; |
156 | 9ee6e8bb | pbrook | break;
|
157 | 9ee6e8bb | pbrook | case 0x408: /* Interrupt both edges */ |
158 | 9ee6e8bb | pbrook | s->ibe = value; |
159 | 9ee6e8bb | pbrook | break;
|
160 | 9ee6e8bb | pbrook | case 0x40c: /* Interupt event */ |
161 | 9ee6e8bb | pbrook | s->iev = value; |
162 | 9ee6e8bb | pbrook | break;
|
163 | 9ee6e8bb | pbrook | case 0x410: /* Interrupt mask */ |
164 | 9ee6e8bb | pbrook | s->im = value; |
165 | 9ee6e8bb | pbrook | break;
|
166 | 9ee6e8bb | pbrook | case 0x41c: /* Interrupt clear */ |
167 | 9ee6e8bb | pbrook | s->istate &= ~value; |
168 | 9ee6e8bb | pbrook | break;
|
169 | 9ee6e8bb | pbrook | case 0x420: /* Alternate function select */ |
170 | 9ee6e8bb | pbrook | mask = s->cr; |
171 | 9ee6e8bb | pbrook | s->afsel = (s->afsel & ~mask) | (value & mask); |
172 | 9ee6e8bb | pbrook | break;
|
173 | 9ee6e8bb | pbrook | case 0x500: /* 2mA drive */ |
174 | 9ee6e8bb | pbrook | s->dr2r = value; |
175 | 9ee6e8bb | pbrook | break;
|
176 | 9ee6e8bb | pbrook | case 0x504: /* 4mA drive */ |
177 | 9ee6e8bb | pbrook | s->dr4r = value; |
178 | 9ee6e8bb | pbrook | break;
|
179 | 9ee6e8bb | pbrook | case 0x508: /* 8mA drive */ |
180 | 9ee6e8bb | pbrook | s->dr8r = value; |
181 | 9ee6e8bb | pbrook | break;
|
182 | 9ee6e8bb | pbrook | case 0x50c: /* Open drain */ |
183 | 9ee6e8bb | pbrook | s->odr = value; |
184 | 9ee6e8bb | pbrook | break;
|
185 | 9ee6e8bb | pbrook | case 0x510: /* Pull-up */ |
186 | 9ee6e8bb | pbrook | s->pur = value; |
187 | 9ee6e8bb | pbrook | break;
|
188 | 9ee6e8bb | pbrook | case 0x514: /* Pull-down */ |
189 | 9ee6e8bb | pbrook | s->pdr = value; |
190 | 9ee6e8bb | pbrook | break;
|
191 | 9ee6e8bb | pbrook | case 0x518: /* Slew rate control */ |
192 | 9ee6e8bb | pbrook | s->slr = value; |
193 | 9ee6e8bb | pbrook | break;
|
194 | 9ee6e8bb | pbrook | case 0x51c: /* Digital enable */ |
195 | 9ee6e8bb | pbrook | s->den = value; |
196 | 9ee6e8bb | pbrook | break;
|
197 | 9ee6e8bb | pbrook | case 0x520: /* Lock */ |
198 | 9ee6e8bb | pbrook | s->locked = (value != 0xacce551);
|
199 | 9ee6e8bb | pbrook | break;
|
200 | 9ee6e8bb | pbrook | case 0x524: /* Commit */ |
201 | 9ee6e8bb | pbrook | if (!s->locked)
|
202 | 9ee6e8bb | pbrook | s->cr = value; |
203 | 9ee6e8bb | pbrook | break;
|
204 | 9ee6e8bb | pbrook | default:
|
205 | 9ee6e8bb | pbrook | cpu_abort (cpu_single_env, "pl061_write: Bad offset %x\n",
|
206 | 9ee6e8bb | pbrook | (int)offset);
|
207 | 9ee6e8bb | pbrook | } |
208 | 9ee6e8bb | pbrook | pl061_update(s); |
209 | 9ee6e8bb | pbrook | } |
210 | 9ee6e8bb | pbrook | |
211 | 9ee6e8bb | pbrook | static void pl061_reset(pl061_state *s) |
212 | 9ee6e8bb | pbrook | { |
213 | 9ee6e8bb | pbrook | s->locked = 1;
|
214 | 9ee6e8bb | pbrook | s->cr = 0xff;
|
215 | 9ee6e8bb | pbrook | } |
216 | 9ee6e8bb | pbrook | |
217 | 9596ebb7 | pbrook | static void pl061_set_irq(void * opaque, int irq, int level) |
218 | 9ee6e8bb | pbrook | { |
219 | 9ee6e8bb | pbrook | pl061_state *s = (pl061_state *)opaque; |
220 | 9ee6e8bb | pbrook | uint8_t mask; |
221 | 9ee6e8bb | pbrook | |
222 | 9ee6e8bb | pbrook | mask = 1 << irq;
|
223 | 9ee6e8bb | pbrook | if ((s->dir & mask) == 0) { |
224 | 9ee6e8bb | pbrook | s->data &= ~mask; |
225 | 9ee6e8bb | pbrook | if (level)
|
226 | 9ee6e8bb | pbrook | s->data |= mask; |
227 | 9ee6e8bb | pbrook | pl061_update(s); |
228 | 9ee6e8bb | pbrook | } |
229 | 9ee6e8bb | pbrook | } |
230 | 9ee6e8bb | pbrook | |
231 | 9ee6e8bb | pbrook | static CPUReadMemoryFunc *pl061_readfn[] = {
|
232 | 9ee6e8bb | pbrook | pl061_read, |
233 | 9ee6e8bb | pbrook | pl061_read, |
234 | 9ee6e8bb | pbrook | pl061_read |
235 | 9ee6e8bb | pbrook | }; |
236 | 9ee6e8bb | pbrook | |
237 | 9ee6e8bb | pbrook | static CPUWriteMemoryFunc *pl061_writefn[] = {
|
238 | 9ee6e8bb | pbrook | pl061_write, |
239 | 9ee6e8bb | pbrook | pl061_write, |
240 | 9ee6e8bb | pbrook | pl061_write |
241 | 9ee6e8bb | pbrook | }; |
242 | 9ee6e8bb | pbrook | |
243 | 9ee6e8bb | pbrook | /* Returns an array of inputs. */
|
244 | 9ee6e8bb | pbrook | qemu_irq *pl061_init(uint32_t base, qemu_irq irq, qemu_irq **out) |
245 | 9ee6e8bb | pbrook | { |
246 | 9ee6e8bb | pbrook | int iomemtype;
|
247 | 9ee6e8bb | pbrook | pl061_state *s; |
248 | 9ee6e8bb | pbrook | |
249 | 9ee6e8bb | pbrook | s = (pl061_state *)qemu_mallocz(sizeof(pl061_state));
|
250 | 9ee6e8bb | pbrook | iomemtype = cpu_register_io_memory(0, pl061_readfn,
|
251 | 9ee6e8bb | pbrook | pl061_writefn, s); |
252 | 9ee6e8bb | pbrook | cpu_register_physical_memory(base, 0x00001000, iomemtype);
|
253 | 9ee6e8bb | pbrook | s->base = base; |
254 | 9ee6e8bb | pbrook | s->irq = irq; |
255 | 9ee6e8bb | pbrook | pl061_reset(s); |
256 | 9ee6e8bb | pbrook | if (out)
|
257 | 9ee6e8bb | pbrook | *out = s->out; |
258 | 9ee6e8bb | pbrook | |
259 | 9ee6e8bb | pbrook | /* ??? Save/restore. */
|
260 | 9ee6e8bb | pbrook | return qemu_allocate_irqs(pl061_set_irq, s, 8); |
261 | 9ee6e8bb | pbrook | } |