blob: 524970840d72cc0cf0de283538addb121801dfc0 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
bellardb5ff1b32005-11-26 10:38:39 +00002 * ARM Integrator CP System emulation.
3 *
pbrooka1bb27b2007-04-06 16:49:48 +00004 * Copyright (c) 2005-2007 CodeSourcery.
bellardb5ff1b32005-11-26 10:38:39 +00005 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL
bellardb5ff1b32005-11-26 10:38:39 +00008 */
9
Peter Maydell12b16722015-12-07 16:23:45 +000010#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010011#include "qapi/error.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010012#include "cpu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010013#include "hw/sysbus.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020014#include "migration/vmstate.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010015#include "hw/boards.h"
Peter Maydell12ec8bd2019-05-23 14:47:43 +010016#include "hw/arm/boot.h"
Alex Bennéeb8616052013-10-22 15:16:06 +010017#include "hw/misc/arm_integrator_debug.h"
Philippe Mathieu-Daudé437cc272019-04-12 18:54:16 +020018#include "hw/net/smc91c111.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020019#include "net/net.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010020#include "exec/address-spaces.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020021#include "sysemu/runstate.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
Greg Bellows223a72f2014-12-15 17:09:50 -060023#include "qemu/error-report.h"
xiaoqiang zhaof0d1d2c2016-06-06 16:59:31 +010024#include "hw/char/pl011.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020025#include "hw/hw.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020026#include "hw/irq.h"
bellardb5ff1b32005-11-26 10:38:39 +000027
Andreas Färber257ec282013-07-24 01:00:27 +020028#define TYPE_INTEGRATOR_CM "integrator_core"
29#define INTEGRATOR_CM(obj) \
30 OBJECT_CHECK(IntegratorCMState, (obj), TYPE_INTEGRATOR_CM)
31
32typedef struct IntegratorCMState {
33 /*< private >*/
34 SysBusDevice parent_obj;
35 /*< public >*/
36
Benoît Canet71d9bc52011-10-17 17:28:26 +020037 MemoryRegion iomem;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +020038 uint32_t memsz;
Avi Kivity211adf42011-07-25 15:03:19 +030039 MemoryRegion flash;
bellardb5ff1b32005-11-26 10:38:39 +000040 uint32_t cm_osc;
41 uint32_t cm_ctrl;
42 uint32_t cm_lock;
43 uint32_t cm_auxosc;
44 uint32_t cm_sdram;
45 uint32_t cm_init;
46 uint32_t cm_flags;
47 uint32_t cm_nvflags;
Jan Petrousf53977f2013-12-10 13:24:51 +000048 uint32_t cm_refcnt_offset;
bellardb5ff1b32005-11-26 10:38:39 +000049 uint32_t int_level;
50 uint32_t irq_enabled;
51 uint32_t fiq_enabled;
Andreas Färber257ec282013-07-24 01:00:27 +020052} IntegratorCMState;
bellardb5ff1b32005-11-26 10:38:39 +000053
54static uint8_t integrator_spd[128] = {
55 128, 8, 4, 11, 9, 1, 64, 0, 2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
56 0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
57};
58
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +000059static const VMStateDescription vmstate_integratorcm = {
60 .name = "integratorcm",
61 .version_id = 1,
62 .minimum_version_id = 1,
63 .fields = (VMStateField[]) {
64 VMSTATE_UINT32(cm_osc, IntegratorCMState),
65 VMSTATE_UINT32(cm_ctrl, IntegratorCMState),
66 VMSTATE_UINT32(cm_lock, IntegratorCMState),
67 VMSTATE_UINT32(cm_auxosc, IntegratorCMState),
68 VMSTATE_UINT32(cm_sdram, IntegratorCMState),
69 VMSTATE_UINT32(cm_init, IntegratorCMState),
70 VMSTATE_UINT32(cm_flags, IntegratorCMState),
71 VMSTATE_UINT32(cm_nvflags, IntegratorCMState),
72 VMSTATE_UINT32(int_level, IntegratorCMState),
73 VMSTATE_UINT32(irq_enabled, IntegratorCMState),
74 VMSTATE_UINT32(fiq_enabled, IntegratorCMState),
75 VMSTATE_END_OF_LIST()
76 }
77};
78
Avi Kivitya8170e52012-10-23 12:30:10 +020079static uint64_t integratorcm_read(void *opaque, hwaddr offset,
Benoît Canet71d9bc52011-10-17 17:28:26 +020080 unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +000081{
Andreas Färber257ec282013-07-24 01:00:27 +020082 IntegratorCMState *s = opaque;
bellardb5ff1b32005-11-26 10:38:39 +000083 if (offset >= 0x100 && offset < 0x200) {
84 /* CM_SPD */
85 if (offset >= 0x180)
86 return 0;
87 return integrator_spd[offset >> 2];
88 }
89 switch (offset >> 2) {
90 case 0: /* CM_ID */
91 return 0x411a3001;
92 case 1: /* CM_PROC */
93 return 0;
94 case 2: /* CM_OSC */
95 return s->cm_osc;
96 case 3: /* CM_CTRL */
97 return s->cm_ctrl;
98 case 4: /* CM_STAT */
99 return 0x00100000;
100 case 5: /* CM_LOCK */
101 if (s->cm_lock == 0xa05f) {
102 return 0x1a05f;
103 } else {
104 return s->cm_lock;
105 }
106 case 6: /* CM_LMBUSCNT */
107 /* ??? High frequency timer. */
Paul Brook2ac71172009-05-08 02:35:15 +0100108 hw_error("integratorcm_read: CM_LMBUSCNT");
bellardb5ff1b32005-11-26 10:38:39 +0000109 case 7: /* CM_AUXOSC */
110 return s->cm_auxosc;
111 case 8: /* CM_SDRAM */
112 return s->cm_sdram;
113 case 9: /* CM_INIT */
114 return s->cm_init;
Jan Petrousf53977f2013-12-10 13:24:51 +0000115 case 10: /* CM_REFCNT */
116 /* This register, CM_REFCNT, provides a 32-bit count value.
117 * The count increments at the fixed reference clock frequency of 24MHz
118 * and can be used as a real-time counter.
119 */
120 return (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 24,
121 1000) - s->cm_refcnt_offset;
bellardb5ff1b32005-11-26 10:38:39 +0000122 case 12: /* CM_FLAGS */
123 return s->cm_flags;
124 case 14: /* CM_NVFLAGS */
125 return s->cm_nvflags;
126 case 16: /* CM_IRQ_STAT */
127 return s->int_level & s->irq_enabled;
128 case 17: /* CM_IRQ_RSTAT */
129 return s->int_level;
130 case 18: /* CM_IRQ_ENSET */
131 return s->irq_enabled;
132 case 20: /* CM_SOFT_INTSET */
133 return s->int_level & 1;
134 case 24: /* CM_FIQ_STAT */
135 return s->int_level & s->fiq_enabled;
136 case 25: /* CM_FIQ_RSTAT */
137 return s->int_level;
138 case 26: /* CM_FIQ_ENSET */
139 return s->fiq_enabled;
140 case 32: /* CM_VOLTAGE_CTL0 */
141 case 33: /* CM_VOLTAGE_CTL1 */
142 case 34: /* CM_VOLTAGE_CTL2 */
143 case 35: /* CM_VOLTAGE_CTL3 */
144 /* ??? Voltage control unimplemented. */
145 return 0;
146 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100147 hw_error("integratorcm_read: Unimplemented offset 0x%x\n",
148 (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000149 return 0;
150 }
151}
152
Andreas Färber257ec282013-07-24 01:00:27 +0200153static void integratorcm_do_remap(IntegratorCMState *s)
bellardb5ff1b32005-11-26 10:38:39 +0000154{
Peter Maydell563c2bf2012-01-06 18:58:28 +0000155 /* Sync memory region state with CM_CTRL REMAP bit:
156 * bit 0 => flash at address 0; bit 1 => RAM
157 */
158 memory_region_set_enabled(&s->flash, !(s->cm_ctrl & 4));
bellardb5ff1b32005-11-26 10:38:39 +0000159}
160
Andreas Färber257ec282013-07-24 01:00:27 +0200161static void integratorcm_set_ctrl(IntegratorCMState *s, uint32_t value)
bellardb5ff1b32005-11-26 10:38:39 +0000162{
163 if (value & 8) {
Eric Blakecf83f142017-05-15 16:41:13 -0500164 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
bellardb5ff1b32005-11-26 10:38:39 +0000165 }
Peter Maydelldf3f4572011-09-12 15:43:31 +0100166 if ((s->cm_ctrl ^ value) & 1) {
167 /* (value & 1) != 0 means the green "MISC LED" is lit.
168 * We don't have any nice place to display LEDs. printf is a bad
169 * idea because Linux uses the LED as a heartbeat and the output
170 * will swamp anything else on the terminal.
171 */
bellardb5ff1b32005-11-26 10:38:39 +0000172 }
Peter Maydelldf3f4572011-09-12 15:43:31 +0100173 /* Note that the RESET bit [3] always reads as zero */
174 s->cm_ctrl = (s->cm_ctrl & ~5) | (value & 5);
Peter Maydell563c2bf2012-01-06 18:58:28 +0000175 integratorcm_do_remap(s);
bellardb5ff1b32005-11-26 10:38:39 +0000176}
177
Andreas Färber257ec282013-07-24 01:00:27 +0200178static void integratorcm_update(IntegratorCMState *s)
bellardb5ff1b32005-11-26 10:38:39 +0000179{
180 /* ??? The CPU irq/fiq is raised when either the core module or base PIC
181 are active. */
182 if (s->int_level & (s->irq_enabled | s->fiq_enabled))
Paul Brook2ac71172009-05-08 02:35:15 +0100183 hw_error("Core module interrupt\n");
bellardb5ff1b32005-11-26 10:38:39 +0000184}
185
Avi Kivitya8170e52012-10-23 12:30:10 +0200186static void integratorcm_write(void *opaque, hwaddr offset,
Benoît Canet71d9bc52011-10-17 17:28:26 +0200187 uint64_t value, unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +0000188{
Andreas Färber257ec282013-07-24 01:00:27 +0200189 IntegratorCMState *s = opaque;
bellardb5ff1b32005-11-26 10:38:39 +0000190 switch (offset >> 2) {
191 case 2: /* CM_OSC */
192 if (s->cm_lock == 0xa05f)
193 s->cm_osc = value;
194 break;
195 case 3: /* CM_CTRL */
196 integratorcm_set_ctrl(s, value);
197 break;
198 case 5: /* CM_LOCK */
199 s->cm_lock = value & 0xffff;
200 break;
201 case 7: /* CM_AUXOSC */
202 if (s->cm_lock == 0xa05f)
203 s->cm_auxosc = value;
204 break;
205 case 8: /* CM_SDRAM */
206 s->cm_sdram = value;
207 break;
208 case 9: /* CM_INIT */
209 /* ??? This can change the memory bus frequency. */
210 s->cm_init = value;
211 break;
212 case 12: /* CM_FLAGSS */
213 s->cm_flags |= value;
214 break;
215 case 13: /* CM_FLAGSC */
216 s->cm_flags &= ~value;
217 break;
218 case 14: /* CM_NVFLAGSS */
219 s->cm_nvflags |= value;
220 break;
221 case 15: /* CM_NVFLAGSS */
222 s->cm_nvflags &= ~value;
223 break;
224 case 18: /* CM_IRQ_ENSET */
225 s->irq_enabled |= value;
226 integratorcm_update(s);
227 break;
228 case 19: /* CM_IRQ_ENCLR */
229 s->irq_enabled &= ~value;
230 integratorcm_update(s);
231 break;
232 case 20: /* CM_SOFT_INTSET */
233 s->int_level |= (value & 1);
234 integratorcm_update(s);
235 break;
236 case 21: /* CM_SOFT_INTCLR */
237 s->int_level &= ~(value & 1);
238 integratorcm_update(s);
239 break;
240 case 26: /* CM_FIQ_ENSET */
241 s->fiq_enabled |= value;
242 integratorcm_update(s);
243 break;
244 case 27: /* CM_FIQ_ENCLR */
245 s->fiq_enabled &= ~value;
246 integratorcm_update(s);
247 break;
248 case 32: /* CM_VOLTAGE_CTL0 */
249 case 33: /* CM_VOLTAGE_CTL1 */
250 case 34: /* CM_VOLTAGE_CTL2 */
251 case 35: /* CM_VOLTAGE_CTL3 */
252 /* ??? Voltage control unimplemented. */
253 break;
254 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100255 hw_error("integratorcm_write: Unimplemented offset 0x%x\n",
256 (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000257 break;
258 }
259}
260
261/* Integrator/CM control registers. */
262
Benoît Canet71d9bc52011-10-17 17:28:26 +0200263static const MemoryRegionOps integratorcm_ops = {
264 .read = integratorcm_read,
265 .write = integratorcm_write,
266 .endianness = DEVICE_NATIVE_ENDIAN,
bellardb5ff1b32005-11-26 10:38:39 +0000267};
268
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800269static void integratorcm_init(Object *obj)
bellardb5ff1b32005-11-26 10:38:39 +0000270{
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800271 IntegratorCMState *s = INTEGRATOR_CM(obj);
bellardb5ff1b32005-11-26 10:38:39 +0000272
bellardb5ff1b32005-11-26 10:38:39 +0000273 s->cm_osc = 0x01000048;
274 /* ??? What should the high bits of this value be? */
275 s->cm_auxosc = 0x0007feff;
276 s->cm_sdram = 0x00011122;
Jakub Jermare9d9ee22016-10-04 13:28:08 +0100277 memcpy(integrator_spd + 73, "QEMU-MEMORY", 11);
278 s->cm_init = 0x00000112;
279 s->cm_refcnt_offset = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 24,
280 1000);
Jakub Jermare9d9ee22016-10-04 13:28:08 +0100281
Jakub Jermare9d9ee22016-10-04 13:28:08 +0100282 /* ??? Save/restore. */
283}
284
285static void integratorcm_realize(DeviceState *d, Error **errp)
286{
287 IntegratorCMState *s = INTEGRATOR_CM(d);
Thomas Huth8720daa2018-04-10 13:02:24 +0100288 SysBusDevice *dev = SYS_BUS_DEVICE(d);
289 Error *local_err = NULL;
290
291 memory_region_init_ram(&s->flash, OBJECT(d), "integrator.flash", 0x100000,
292 &local_err);
293 if (local_err) {
294 error_propagate(errp, local_err);
295 return;
296 }
297
298 memory_region_init_io(&s->iomem, OBJECT(d), &integratorcm_ops, s,
299 "integratorcm", 0x00800000);
300 sysbus_init_mmio(dev, &s->iomem);
301
302 integratorcm_do_remap(s);
Jakub Jermare9d9ee22016-10-04 13:28:08 +0100303
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200304 if (s->memsz >= 256) {
bellardb5ff1b32005-11-26 10:38:39 +0000305 integrator_spd[31] = 64;
306 s->cm_sdram |= 0x10;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200307 } else if (s->memsz >= 128) {
bellardb5ff1b32005-11-26 10:38:39 +0000308 integrator_spd[31] = 32;
309 s->cm_sdram |= 0x0c;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200310 } else if (s->memsz >= 64) {
bellardb5ff1b32005-11-26 10:38:39 +0000311 integrator_spd[31] = 16;
312 s->cm_sdram |= 0x08;
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200313 } else if (s->memsz >= 32) {
bellardb5ff1b32005-11-26 10:38:39 +0000314 integrator_spd[31] = 4;
315 s->cm_sdram |= 0x04;
316 } else {
317 integrator_spd[31] = 2;
318 }
bellardb5ff1b32005-11-26 10:38:39 +0000319}
320
321/* Integrator/CP hardware emulation. */
322/* Primary interrupt controller. */
323
Andreas Färber91b64622013-07-24 01:08:01 +0200324#define TYPE_INTEGRATOR_PIC "integrator_pic"
325#define INTEGRATOR_PIC(obj) \
326 OBJECT_CHECK(icp_pic_state, (obj), TYPE_INTEGRATOR_PIC)
327
328typedef struct icp_pic_state {
329 /*< private >*/
330 SysBusDevice parent_obj;
331 /*< public >*/
332
333 MemoryRegion iomem;
334 uint32_t level;
335 uint32_t irq_enabled;
336 uint32_t fiq_enabled;
337 qemu_irq parent_irq;
338 qemu_irq parent_fiq;
bellardb5ff1b32005-11-26 10:38:39 +0000339} icp_pic_state;
340
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +0000341static const VMStateDescription vmstate_icp_pic = {
342 .name = "icp_pic",
343 .version_id = 1,
344 .minimum_version_id = 1,
345 .fields = (VMStateField[]) {
346 VMSTATE_UINT32(level, icp_pic_state),
347 VMSTATE_UINT32(irq_enabled, icp_pic_state),
348 VMSTATE_UINT32(fiq_enabled, icp_pic_state),
349 VMSTATE_END_OF_LIST()
350 }
351};
352
bellardb5ff1b32005-11-26 10:38:39 +0000353static void icp_pic_update(icp_pic_state *s)
354{
pbrookcdbdb642006-04-09 01:32:52 +0000355 uint32_t flags;
bellardb5ff1b32005-11-26 10:38:39 +0000356
pbrookd537cf62007-04-07 18:14:41 +0000357 flags = (s->level & s->irq_enabled);
358 qemu_set_irq(s->parent_irq, flags != 0);
359 flags = (s->level & s->fiq_enabled);
360 qemu_set_irq(s->parent_fiq, flags != 0);
bellardb5ff1b32005-11-26 10:38:39 +0000361}
362
pbrookcdbdb642006-04-09 01:32:52 +0000363static void icp_pic_set_irq(void *opaque, int irq, int level)
bellardb5ff1b32005-11-26 10:38:39 +0000364{
bellard80337b62005-12-04 18:54:21 +0000365 icp_pic_state *s = (icp_pic_state *)opaque;
bellardb5ff1b32005-11-26 10:38:39 +0000366 if (level)
bellard80337b62005-12-04 18:54:21 +0000367 s->level |= 1 << irq;
bellardb5ff1b32005-11-26 10:38:39 +0000368 else
bellard80337b62005-12-04 18:54:21 +0000369 s->level &= ~(1 << irq);
bellardb5ff1b32005-11-26 10:38:39 +0000370 icp_pic_update(s);
371}
372
Avi Kivitya8170e52012-10-23 12:30:10 +0200373static uint64_t icp_pic_read(void *opaque, hwaddr offset,
Benoît Canet61074e42011-10-17 17:28:27 +0200374 unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +0000375{
376 icp_pic_state *s = (icp_pic_state *)opaque;
377
bellardb5ff1b32005-11-26 10:38:39 +0000378 switch (offset >> 2) {
379 case 0: /* IRQ_STATUS */
380 return s->level & s->irq_enabled;
381 case 1: /* IRQ_RAWSTAT */
382 return s->level;
383 case 2: /* IRQ_ENABLESET */
384 return s->irq_enabled;
385 case 4: /* INT_SOFTSET */
386 return s->level & 1;
387 case 8: /* FRQ_STATUS */
388 return s->level & s->fiq_enabled;
389 case 9: /* FRQ_RAWSTAT */
390 return s->level;
391 case 10: /* FRQ_ENABLESET */
392 return s->fiq_enabled;
393 case 3: /* IRQ_ENABLECLR */
394 case 5: /* INT_SOFTCLR */
395 case 11: /* FRQ_ENABLECLR */
396 default:
pbrook29bfb112006-11-19 23:07:17 +0000397 printf ("icp_pic_read: Bad register offset 0x%x\n", (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000398 return 0;
399 }
400}
401
Avi Kivitya8170e52012-10-23 12:30:10 +0200402static void icp_pic_write(void *opaque, hwaddr offset,
Benoît Canet61074e42011-10-17 17:28:27 +0200403 uint64_t value, unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +0000404{
405 icp_pic_state *s = (icp_pic_state *)opaque;
bellardb5ff1b32005-11-26 10:38:39 +0000406
407 switch (offset >> 2) {
408 case 2: /* IRQ_ENABLESET */
409 s->irq_enabled |= value;
410 break;
411 case 3: /* IRQ_ENABLECLR */
412 s->irq_enabled &= ~value;
413 break;
414 case 4: /* INT_SOFTSET */
415 if (value & 1)
pbrookd537cf62007-04-07 18:14:41 +0000416 icp_pic_set_irq(s, 0, 1);
bellardb5ff1b32005-11-26 10:38:39 +0000417 break;
418 case 5: /* INT_SOFTCLR */
419 if (value & 1)
pbrookd537cf62007-04-07 18:14:41 +0000420 icp_pic_set_irq(s, 0, 0);
bellardb5ff1b32005-11-26 10:38:39 +0000421 break;
422 case 10: /* FRQ_ENABLESET */
423 s->fiq_enabled |= value;
424 break;
425 case 11: /* FRQ_ENABLECLR */
426 s->fiq_enabled &= ~value;
427 break;
428 case 0: /* IRQ_STATUS */
429 case 1: /* IRQ_RAWSTAT */
430 case 8: /* FRQ_STATUS */
431 case 9: /* FRQ_RAWSTAT */
432 default:
pbrook29bfb112006-11-19 23:07:17 +0000433 printf ("icp_pic_write: Bad register offset 0x%x\n", (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000434 return;
435 }
436 icp_pic_update(s);
437}
438
Benoît Canet61074e42011-10-17 17:28:27 +0200439static const MemoryRegionOps icp_pic_ops = {
440 .read = icp_pic_read,
441 .write = icp_pic_write,
442 .endianness = DEVICE_NATIVE_ENDIAN,
bellardb5ff1b32005-11-26 10:38:39 +0000443};
444
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800445static void icp_pic_init(Object *obj)
bellardb5ff1b32005-11-26 10:38:39 +0000446{
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800447 DeviceState *dev = DEVICE(obj);
448 icp_pic_state *s = INTEGRATOR_PIC(obj);
449 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
bellardb5ff1b32005-11-26 10:38:39 +0000450
Andreas Färber91b64622013-07-24 01:08:01 +0200451 qdev_init_gpio_in(dev, icp_pic_set_irq, 32);
452 sysbus_init_irq(sbd, &s->parent_irq);
453 sysbus_init_irq(sbd, &s->parent_fiq);
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800454 memory_region_init_io(&s->iomem, obj, &icp_pic_ops, s,
Paolo Bonzini64bde0f2013-06-06 21:25:08 -0400455 "icp-pic", 0x00800000);
Andreas Färber91b64622013-07-24 01:08:01 +0200456 sysbus_init_mmio(sbd, &s->iomem);
bellardb5ff1b32005-11-26 10:38:39 +0000457}
458
bellardb5ff1b32005-11-26 10:38:39 +0000459/* CP control registers. */
Benoît Canet0c364932011-10-17 17:28:28 +0200460
Jan Kiszkaffc85422015-03-11 13:21:06 +0000461#define TYPE_ICP_CONTROL_REGS "icp-ctrl-regs"
462#define ICP_CONTROL_REGS(obj) \
463 OBJECT_CHECK(ICPCtrlRegsState, (obj), TYPE_ICP_CONTROL_REGS)
464
465typedef struct ICPCtrlRegsState {
466 /*< private >*/
467 SysBusDevice parent_obj;
468 /*< public >*/
469
470 MemoryRegion iomem;
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000471
472 qemu_irq mmc_irq;
473 uint32_t intreg_state;
Jan Kiszkaffc85422015-03-11 13:21:06 +0000474} ICPCtrlRegsState;
475
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000476#define ICP_GPIO_MMC_WPROT "mmc-wprot"
477#define ICP_GPIO_MMC_CARDIN "mmc-cardin"
478
479#define ICP_INTREG_WPROT (1 << 0)
480#define ICP_INTREG_CARDIN (1 << 3)
481
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +0000482static const VMStateDescription vmstate_icp_control = {
483 .name = "icp_control",
484 .version_id = 1,
485 .minimum_version_id = 1,
486 .fields = (VMStateField[]) {
487 VMSTATE_UINT32(intreg_state, ICPCtrlRegsState),
488 VMSTATE_END_OF_LIST()
489 }
490};
491
Avi Kivitya8170e52012-10-23 12:30:10 +0200492static uint64_t icp_control_read(void *opaque, hwaddr offset,
Benoît Canet0c364932011-10-17 17:28:28 +0200493 unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +0000494{
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000495 ICPCtrlRegsState *s = opaque;
496
bellardb5ff1b32005-11-26 10:38:39 +0000497 switch (offset >> 2) {
498 case 0: /* CP_IDFIELD */
499 return 0x41034003;
500 case 1: /* CP_FLASHPROG */
501 return 0;
502 case 2: /* CP_INTREG */
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000503 return s->intreg_state;
bellardb5ff1b32005-11-26 10:38:39 +0000504 case 3: /* CP_DECODE */
505 return 0x11;
506 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100507 hw_error("icp_control_read: Bad offset %x\n", (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000508 return 0;
509 }
510}
511
Avi Kivitya8170e52012-10-23 12:30:10 +0200512static void icp_control_write(void *opaque, hwaddr offset,
Benoît Canet0c364932011-10-17 17:28:28 +0200513 uint64_t value, unsigned size)
bellardb5ff1b32005-11-26 10:38:39 +0000514{
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000515 ICPCtrlRegsState *s = opaque;
516
bellardb5ff1b32005-11-26 10:38:39 +0000517 switch (offset >> 2) {
bellardb5ff1b32005-11-26 10:38:39 +0000518 case 2: /* CP_INTREG */
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000519 s->intreg_state &= ~(value & ICP_INTREG_CARDIN);
520 qemu_set_irq(s->mmc_irq, !!(s->intreg_state & ICP_INTREG_CARDIN));
521 break;
522 case 1: /* CP_FLASHPROG */
bellardb5ff1b32005-11-26 10:38:39 +0000523 case 3: /* CP_DECODE */
524 /* Nothing interesting implemented yet. */
525 break;
526 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100527 hw_error("icp_control_write: Bad offset %x\n", (int)offset);
bellardb5ff1b32005-11-26 10:38:39 +0000528 }
529}
Benoît Canet0c364932011-10-17 17:28:28 +0200530
531static const MemoryRegionOps icp_control_ops = {
532 .read = icp_control_read,
533 .write = icp_control_write,
534 .endianness = DEVICE_NATIVE_ENDIAN,
bellardb5ff1b32005-11-26 10:38:39 +0000535};
536
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000537static void icp_control_mmc_wprot(void *opaque, int line, int level)
538{
539 ICPCtrlRegsState *s = opaque;
540
541 s->intreg_state &= ~ICP_INTREG_WPROT;
542 if (level) {
543 s->intreg_state |= ICP_INTREG_WPROT;
544 }
545}
546
547static void icp_control_mmc_cardin(void *opaque, int line, int level)
548{
549 ICPCtrlRegsState *s = opaque;
550
551 /* line is released by writing to CP_INTREG */
552 if (level) {
553 s->intreg_state |= ICP_INTREG_CARDIN;
554 qemu_set_irq(s->mmc_irq, 1);
555 }
556}
557
Jan Kiszkaffc85422015-03-11 13:21:06 +0000558static void icp_control_init(Object *obj)
bellardb5ff1b32005-11-26 10:38:39 +0000559{
Jan Kiszkaffc85422015-03-11 13:21:06 +0000560 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
561 ICPCtrlRegsState *s = ICP_CONTROL_REGS(obj);
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000562 DeviceState *dev = DEVICE(obj);
bellardb5ff1b32005-11-26 10:38:39 +0000563
Jan Kiszkaffc85422015-03-11 13:21:06 +0000564 memory_region_init_io(&s->iomem, OBJECT(s), &icp_control_ops, s,
565 "icp_ctrl_regs", 0x00800000);
566 sysbus_init_mmio(sbd, &s->iomem);
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000567
568 qdev_init_gpio_in_named(dev, icp_control_mmc_wprot, ICP_GPIO_MMC_WPROT, 1);
569 qdev_init_gpio_in_named(dev, icp_control_mmc_cardin,
570 ICP_GPIO_MMC_CARDIN, 1);
571 sysbus_init_irq(sbd, &s->mmc_irq);
bellardb5ff1b32005-11-26 10:38:39 +0000572}
573
574
bellardb5ff1b32005-11-26 10:38:39 +0000575/* Board init. */
576
balrogf93eb9f2008-04-14 20:27:51 +0000577static struct arm_boot_info integrator_binfo = {
578 .loader_start = 0x0,
579 .board_id = 0x113,
580};
581
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300582static void integratorcp_init(MachineState *machine)
bellardb5ff1b32005-11-26 10:38:39 +0000583{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300584 ram_addr_t ram_size = machine->ram_size;
Greg Bellows223a72f2014-12-15 17:09:50 -0600585 Object *cpuobj;
Andreas Färber393a9ea2012-05-14 01:51:01 +0200586 ARMCPU *cpu;
Avi Kivity211adf42011-07-25 15:03:19 +0300587 MemoryRegion *address_space_mem = get_system_memory();
588 MemoryRegion *ram = g_new(MemoryRegion, 1);
589 MemoryRegion *ram_alias = g_new(MemoryRegion, 1);
Paul Brooka7086882009-05-14 22:35:07 +0100590 qemu_irq pic[32];
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000591 DeviceState *dev, *sic, *icp;
Paul Brooka7086882009-05-14 22:35:07 +0100592 int i;
bellardb5ff1b32005-11-26 10:38:39 +0000593
Igor Mammedovba1ba5c2017-09-13 18:04:57 +0200594 cpuobj = object_new(machine->cpu_type);
Greg Bellows223a72f2014-12-15 17:09:50 -0600595
Greg Bellows61e2f352014-12-15 17:09:51 -0600596 /* By default ARM1176 CPUs have EL3 enabled. This board does not
597 * currently support EL3 so the CPU EL3 property is disabled before
598 * realization.
599 */
600 if (object_property_find(cpuobj, "has_el3", NULL)) {
Markus Armbruster007b0652015-09-11 15:04:45 +0200601 object_property_set_bool(cpuobj, false, "has_el3", &error_fatal);
Greg Bellows61e2f352014-12-15 17:09:51 -0600602 }
603
Markus Armbruster007b0652015-09-11 15:04:45 +0200604 object_property_set_bool(cpuobj, true, "realized", &error_fatal);
Greg Bellows223a72f2014-12-15 17:09:50 -0600605
606 cpu = ARM_CPU(cpuobj);
607
Dirk Müllerc8623c02015-04-04 14:24:38 +0200608 memory_region_allocate_system_memory(ram, NULL, "integrator.ram",
609 ram_size);
bellardb5ff1b32005-11-26 10:38:39 +0000610 /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash. */
ths1235fc02008-06-03 19:51:57 +0000611 /* ??? RAM should repeat to fill physical memory space. */
bellardb5ff1b32005-11-26 10:38:39 +0000612 /* SDRAM at address zero*/
Avi Kivity211adf42011-07-25 15:03:19 +0300613 memory_region_add_subregion(address_space_mem, 0, ram);
bellardb5ff1b32005-11-26 10:38:39 +0000614 /* And again at address 0x80000000 */
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400615 memory_region_init_alias(ram_alias, NULL, "ram.alias", ram, 0, ram_size);
Avi Kivity211adf42011-07-25 15:03:19 +0300616 memory_region_add_subregion(address_space_mem, 0x80000000, ram_alias);
bellardb5ff1b32005-11-26 10:38:39 +0000617
Andreas Färber257ec282013-07-24 01:00:27 +0200618 dev = qdev_create(NULL, TYPE_INTEGRATOR_CM);
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200619 qdev_prop_set_uint32(dev, "memsz", ram_size >> 20);
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200620 qdev_init_nofail(dev);
Paul Brooka7086882009-05-14 22:35:07 +0100621 sysbus_mmio_map((SysBusDevice *)dev, 0, 0x10000000);
622
Andreas Färber91b64622013-07-24 01:08:01 +0200623 dev = sysbus_create_varargs(TYPE_INTEGRATOR_PIC, 0x14000000,
Peter Maydell99d228d2013-08-20 14:54:29 +0100624 qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_IRQ),
625 qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_FIQ),
626 NULL);
Paul Brooka7086882009-05-14 22:35:07 +0100627 for (i = 0; i < 32; i++) {
Paul Brook067a3dd2009-05-26 14:56:11 +0100628 pic[i] = qdev_get_gpio_in(dev, i);
Paul Brooka7086882009-05-14 22:35:07 +0100629 }
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000630 sic = sysbus_create_simple(TYPE_INTEGRATOR_PIC, 0xca000000, pic[26]);
Paul Brook6a824ec2009-05-14 22:35:07 +0100631 sysbus_create_varargs("integrator_pit", 0x13000000,
632 pic[5], pic[6], pic[7], NULL);
Paul Brooka63bdb32009-05-14 22:35:07 +0100633 sysbus_create_simple("pl031", 0x15000000, pic[8]);
Peter Maydell9bca0ed2018-04-20 15:52:43 +0100634 pl011_create(0x16000000, pic[1], serial_hd(0));
635 pl011_create(0x17000000, pic[2], serial_hd(1));
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000636 icp = sysbus_create_simple(TYPE_ICP_CONTROL_REGS, 0xcb000000,
637 qdev_get_gpio_in(sic, 3));
Paul Brook86394e92009-05-14 22:35:07 +0100638 sysbus_create_simple("pl050_keyboard", 0x18000000, pic[3]);
639 sysbus_create_simple("pl050_mouse", 0x19000000, pic[4]);
Alex Bennéeb8616052013-10-22 15:16:06 +0100640 sysbus_create_simple(TYPE_INTEGRATOR_DEBUG, 0x1a000000, 0);
Jan Kiszka83d0cf82015-03-11 13:21:06 +0000641
642 dev = sysbus_create_varargs("pl181", 0x1c000000, pic[23], pic[24], NULL);
643 qdev_connect_gpio_out(dev, 0,
644 qdev_get_gpio_in_named(icp, ICP_GPIO_MMC_WPROT, 0));
645 qdev_connect_gpio_out(dev, 1,
646 qdev_get_gpio_in_named(icp, ICP_GPIO_MMC_CARDIN, 0));
647
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100648 if (nd_table[0].used)
aliguori0ae18ce2009-01-13 19:39:36 +0000649 smc91c111_init(&nd_table[0], 0xc8000000, pic[27]);
Paul Brook2e9bdce2009-05-14 22:35:07 +0100650
651 sysbus_create_simple("pl110", 0xc0000000, pic[22]);
bellardb5ff1b32005-11-26 10:38:39 +0000652
balrogf93eb9f2008-04-14 20:27:51 +0000653 integrator_binfo.ram_size = ram_size;
Tao Xu2744ece2019-08-09 14:57:21 +0800654 arm_load_kernel(cpu, machine, &integrator_binfo);
bellardb5ff1b32005-11-26 10:38:39 +0000655}
656
Eduardo Habkoste264d292015-09-04 15:37:08 -0300657static void integratorcp_machine_init(MachineClass *mc)
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500658{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300659 mc->desc = "ARM Integrator/CP (ARM926EJ-S)";
660 mc->init = integratorcp_init;
Peter Maydell4672cbd2017-09-07 13:54:54 +0100661 mc->ignore_memory_transaction_failures = true;
Igor Mammedovba1ba5c2017-09-13 18:04:57 +0200662 mc->default_cpu_type = ARM_CPU_TYPE_NAME("arm926");
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500663}
664
Eduardo Habkoste264d292015-09-04 15:37:08 -0300665DEFINE_MACHINE("integratorcp", integratorcp_machine_init)
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500666
Anthony Liguori999e12b2012-01-24 13:12:29 -0600667static Property core_properties[] = {
Andreas Färber257ec282013-07-24 01:00:27 +0200668 DEFINE_PROP_UINT32("memsz", IntegratorCMState, memsz, 0),
Anthony Liguori999e12b2012-01-24 13:12:29 -0600669 DEFINE_PROP_END_OF_LIST(),
670};
671
672static void core_class_init(ObjectClass *klass, void *data)
673{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600674 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600675
Anthony Liguori39bffca2011-12-07 21:34:16 -0600676 dc->props = core_properties;
Jakub Jermare9d9ee22016-10-04 13:28:08 +0100677 dc->realize = integratorcm_realize;
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +0000678 dc->vmsd = &vmstate_integratorcm;
679}
680
681static void icp_pic_class_init(ObjectClass *klass, void *data)
682{
683 DeviceClass *dc = DEVICE_CLASS(klass);
684
685 dc->vmsd = &vmstate_icp_pic;
686}
687
688static void icp_control_class_init(ObjectClass *klass, void *data)
689{
690 DeviceClass *dc = DEVICE_CLASS(klass);
691
692 dc->vmsd = &vmstate_icp_control;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600693}
694
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100695static const TypeInfo core_info = {
Andreas Färber257ec282013-07-24 01:00:27 +0200696 .name = TYPE_INTEGRATOR_CM,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600697 .parent = TYPE_SYS_BUS_DEVICE,
Andreas Färber257ec282013-07-24 01:00:27 +0200698 .instance_size = sizeof(IntegratorCMState),
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800699 .instance_init = integratorcm_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600700 .class_init = core_class_init,
Anthony Liguori999e12b2012-01-24 13:12:29 -0600701};
702
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100703static const TypeInfo icp_pic_info = {
Andreas Färber91b64622013-07-24 01:08:01 +0200704 .name = TYPE_INTEGRATOR_PIC,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600705 .parent = TYPE_SYS_BUS_DEVICE,
706 .instance_size = sizeof(icp_pic_state),
xiaoqiang.zhaoa1f42e02016-03-07 15:05:44 +0800707 .instance_init = icp_pic_init,
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +0000708 .class_init = icp_pic_class_init,
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200709};
710
Jan Kiszkaffc85422015-03-11 13:21:06 +0000711static const TypeInfo icp_ctrl_regs_info = {
712 .name = TYPE_ICP_CONTROL_REGS,
713 .parent = TYPE_SYS_BUS_DEVICE,
714 .instance_size = sizeof(ICPCtrlRegsState),
715 .instance_init = icp_control_init,
Pavel Dovgalyuk26d32022017-02-07 18:29:58 +0000716 .class_init = icp_control_class_init,
Jan Kiszkaffc85422015-03-11 13:21:06 +0000717};
718
Andreas Färber83f7d432012-02-09 15:20:55 +0100719static void integratorcp_register_types(void)
Paul Brooka7086882009-05-14 22:35:07 +0100720{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600721 type_register_static(&icp_pic_info);
722 type_register_static(&core_info);
Jan Kiszkaffc85422015-03-11 13:21:06 +0000723 type_register_static(&icp_ctrl_regs_info);
Paul Brooka7086882009-05-14 22:35:07 +0100724}
725
Andreas Färber83f7d432012-02-09 15:20:55 +0100726type_init(integratorcp_register_types)