include/qemu/osdep.h: Don't include qapi/error.h
[qemu/ar7.git] / hw / cris / axis_dev88.c
blob8cfa082acd8ca5f455d4e44fa5a5dc1712de9fb1
1 /*
2 * QEMU model for the AXIS devboard 88.
4 * Copyright (c) 2009 Edgar E. Iglesias, Axis Communications AB.
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "hw/sysbus.h"
28 #include "net/net.h"
29 #include "hw/block/flash.h"
30 #include "hw/boards.h"
31 #include "hw/cris/etraxfs.h"
32 #include "hw/loader.h"
33 #include "elf.h"
34 #include "boot.h"
35 #include "sysemu/block-backend.h"
36 #include "exec/address-spaces.h"
37 #include "sysemu/qtest.h"
39 #define D(x)
40 #define DNAND(x)
42 struct nand_state_t
44 DeviceState *nand;
45 MemoryRegion iomem;
46 unsigned int rdy:1;
47 unsigned int ale:1;
48 unsigned int cle:1;
49 unsigned int ce:1;
52 static struct nand_state_t nand_state;
53 static uint64_t nand_read(void *opaque, hwaddr addr, unsigned size)
55 struct nand_state_t *s = opaque;
56 uint32_t r;
57 int rdy;
59 r = nand_getio(s->nand);
60 nand_getpins(s->nand, &rdy);
61 s->rdy = rdy;
63 DNAND(printf("%s addr=%x r=%x\n", __func__, addr, r));
64 return r;
67 static void
68 nand_write(void *opaque, hwaddr addr, uint64_t value,
69 unsigned size)
71 struct nand_state_t *s = opaque;
72 int rdy;
74 DNAND(printf("%s addr=%x v=%x\n", __func__, addr, (unsigned)value));
75 nand_setpins(s->nand, s->cle, s->ale, s->ce, 1, 0);
76 nand_setio(s->nand, value);
77 nand_getpins(s->nand, &rdy);
78 s->rdy = rdy;
81 static const MemoryRegionOps nand_ops = {
82 .read = nand_read,
83 .write = nand_write,
84 .endianness = DEVICE_NATIVE_ENDIAN,
87 struct tempsensor_t
89 unsigned int shiftreg;
90 unsigned int count;
91 enum {
92 ST_OUT, ST_IN, ST_Z
93 } state;
95 uint16_t regs[3];
98 static void tempsensor_clkedge(struct tempsensor_t *s,
99 unsigned int clk, unsigned int data_in)
101 D(printf("%s clk=%d state=%d sr=%x\n", __func__,
102 clk, s->state, s->shiftreg));
103 if (s->count == 0) {
104 s->count = 16;
105 s->state = ST_OUT;
107 switch (s->state) {
108 case ST_OUT:
109 /* Output reg is clocked at negedge. */
110 if (!clk) {
111 s->count--;
112 s->shiftreg <<= 1;
113 if (s->count == 0) {
114 s->shiftreg = 0;
115 s->state = ST_IN;
116 s->count = 16;
119 break;
120 case ST_Z:
121 if (clk) {
122 s->count--;
123 if (s->count == 0) {
124 s->shiftreg = 0;
125 s->state = ST_OUT;
126 s->count = 16;
129 break;
130 case ST_IN:
131 /* Indata is sampled at posedge. */
132 if (clk) {
133 s->count--;
134 s->shiftreg <<= 1;
135 s->shiftreg |= data_in & 1;
136 if (s->count == 0) {
137 D(printf("%s cfgreg=%x\n", __func__, s->shiftreg));
138 s->regs[0] = s->shiftreg;
139 s->state = ST_OUT;
140 s->count = 16;
142 if ((s->regs[0] & 0xff) == 0) {
143 /* 25 degrees celsius. */
144 s->shiftreg = 0x0b9f;
145 } else if ((s->regs[0] & 0xff) == 0xff) {
146 /* Sensor ID, 0x8100 LM70. */
147 s->shiftreg = 0x8100;
148 } else
149 printf("Invalid tempsens state %x\n", s->regs[0]);
152 break;
157 #define RW_PA_DOUT 0x00
158 #define R_PA_DIN 0x01
159 #define RW_PA_OE 0x02
160 #define RW_PD_DOUT 0x10
161 #define R_PD_DIN 0x11
162 #define RW_PD_OE 0x12
164 static struct gpio_state_t
166 MemoryRegion iomem;
167 struct nand_state_t *nand;
168 struct tempsensor_t tempsensor;
169 uint32_t regs[0x5c / 4];
170 } gpio_state;
172 static uint64_t gpio_read(void *opaque, hwaddr addr, unsigned size)
174 struct gpio_state_t *s = opaque;
175 uint32_t r = 0;
177 addr >>= 2;
178 switch (addr)
180 case R_PA_DIN:
181 r = s->regs[RW_PA_DOUT] & s->regs[RW_PA_OE];
183 /* Encode pins from the nand. */
184 r |= s->nand->rdy << 7;
185 break;
186 case R_PD_DIN:
187 r = s->regs[RW_PD_DOUT] & s->regs[RW_PD_OE];
189 /* Encode temp sensor pins. */
190 r |= (!!(s->tempsensor.shiftreg & 0x10000)) << 4;
191 break;
193 default:
194 r = s->regs[addr];
195 break;
197 return r;
198 D(printf("%s %x=%x\n", __func__, addr, r));
201 static void gpio_write(void *opaque, hwaddr addr, uint64_t value,
202 unsigned size)
204 struct gpio_state_t *s = opaque;
205 D(printf("%s %x=%x\n", __func__, addr, (unsigned)value));
207 addr >>= 2;
208 switch (addr)
210 case RW_PA_DOUT:
211 /* Decode nand pins. */
212 s->nand->ale = !!(value & (1 << 6));
213 s->nand->cle = !!(value & (1 << 5));
214 s->nand->ce = !!(value & (1 << 4));
216 s->regs[addr] = value;
217 break;
219 case RW_PD_DOUT:
220 /* Temp sensor clk. */
221 if ((s->regs[addr] ^ value) & 2)
222 tempsensor_clkedge(&s->tempsensor, !!(value & 2),
223 !!(value & 16));
224 s->regs[addr] = value;
225 break;
227 default:
228 s->regs[addr] = value;
229 break;
233 static const MemoryRegionOps gpio_ops = {
234 .read = gpio_read,
235 .write = gpio_write,
236 .endianness = DEVICE_NATIVE_ENDIAN,
237 .valid = {
238 .min_access_size = 4,
239 .max_access_size = 4,
243 #define INTMEM_SIZE (128 * 1024)
245 static struct cris_load_info li;
247 static
248 void axisdev88_init(MachineState *machine)
250 ram_addr_t ram_size = machine->ram_size;
251 const char *cpu_model = machine->cpu_model;
252 const char *kernel_filename = machine->kernel_filename;
253 const char *kernel_cmdline = machine->kernel_cmdline;
254 CRISCPU *cpu;
255 CPUCRISState *env;
256 DeviceState *dev;
257 SysBusDevice *s;
258 DriveInfo *nand;
259 qemu_irq irq[30], nmi[2];
260 void *etraxfs_dmac;
261 struct etraxfs_dma_client *dma_eth;
262 int i;
263 MemoryRegion *address_space_mem = get_system_memory();
264 MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
265 MemoryRegion *phys_intmem = g_new(MemoryRegion, 1);
267 /* init CPUs */
268 if (cpu_model == NULL) {
269 cpu_model = "crisv32";
271 cpu = cpu_cris_init(cpu_model);
272 env = &cpu->env;
274 /* allocate RAM */
275 memory_region_allocate_system_memory(phys_ram, NULL, "axisdev88.ram",
276 ram_size);
277 memory_region_add_subregion(address_space_mem, 0x40000000, phys_ram);
279 /* The ETRAX-FS has 128Kb on chip ram, the docs refer to it as the
280 internal memory. */
281 memory_region_init_ram(phys_intmem, NULL, "axisdev88.chipram", INTMEM_SIZE,
282 &error_fatal);
283 vmstate_register_ram_global(phys_intmem);
284 memory_region_add_subregion(address_space_mem, 0x38000000, phys_intmem);
286 /* Attach a NAND flash to CS1. */
287 nand = drive_get(IF_MTD, 0, 0);
288 nand_state.nand = nand_init(nand ? blk_by_legacy_dinfo(nand) : NULL,
289 NAND_MFR_STMICRO, 0x39);
290 memory_region_init_io(&nand_state.iomem, NULL, &nand_ops, &nand_state,
291 "nand", 0x05000000);
292 memory_region_add_subregion(address_space_mem, 0x10000000,
293 &nand_state.iomem);
295 gpio_state.nand = &nand_state;
296 memory_region_init_io(&gpio_state.iomem, NULL, &gpio_ops, &gpio_state,
297 "gpio", 0x5c);
298 memory_region_add_subregion(address_space_mem, 0x3001a000,
299 &gpio_state.iomem);
302 dev = qdev_create(NULL, "etraxfs,pic");
303 /* FIXME: Is there a proper way to signal vectors to the CPU core? */
304 qdev_prop_set_ptr(dev, "interrupt_vector", &env->interrupt_vector);
305 qdev_init_nofail(dev);
306 s = SYS_BUS_DEVICE(dev);
307 sysbus_mmio_map(s, 0, 0x3001c000);
308 sysbus_connect_irq(s, 0, qdev_get_gpio_in(DEVICE(cpu), CRIS_CPU_IRQ));
309 sysbus_connect_irq(s, 1, qdev_get_gpio_in(DEVICE(cpu), CRIS_CPU_NMI));
310 for (i = 0; i < 30; i++) {
311 irq[i] = qdev_get_gpio_in(dev, i);
313 nmi[0] = qdev_get_gpio_in(dev, 30);
314 nmi[1] = qdev_get_gpio_in(dev, 31);
316 etraxfs_dmac = etraxfs_dmac_init(0x30000000, 10);
317 for (i = 0; i < 10; i++) {
318 /* On ETRAX, odd numbered channels are inputs. */
319 etraxfs_dmac_connect(etraxfs_dmac, i, irq + 7 + i, i & 1);
322 /* Add the two ethernet blocks. */
323 dma_eth = g_malloc0(sizeof dma_eth[0] * 4); /* Allocate 4 channels. */
324 etraxfs_eth_init(&nd_table[0], 0x30034000, 1, &dma_eth[0], &dma_eth[1]);
325 if (nb_nics > 1) {
326 etraxfs_eth_init(&nd_table[1], 0x30036000, 2, &dma_eth[2], &dma_eth[3]);
329 /* The DMA Connector block is missing, hardwire things for now. */
330 etraxfs_dmac_connect_client(etraxfs_dmac, 0, &dma_eth[0]);
331 etraxfs_dmac_connect_client(etraxfs_dmac, 1, &dma_eth[1]);
332 if (nb_nics > 1) {
333 etraxfs_dmac_connect_client(etraxfs_dmac, 6, &dma_eth[2]);
334 etraxfs_dmac_connect_client(etraxfs_dmac, 7, &dma_eth[3]);
337 /* 2 timers. */
338 sysbus_create_varargs("etraxfs,timer", 0x3001e000, irq[0x1b], nmi[1], NULL);
339 sysbus_create_varargs("etraxfs,timer", 0x3005e000, irq[0x1b], nmi[1], NULL);
341 for (i = 0; i < 4; i++) {
342 sysbus_create_simple("etraxfs,serial", 0x30026000 + i * 0x2000,
343 irq[0x14 + i]);
346 if (kernel_filename) {
347 li.image_filename = kernel_filename;
348 li.cmdline = kernel_cmdline;
349 cris_load_image(cpu, &li);
350 } else if (!qtest_enabled()) {
351 fprintf(stderr, "Kernel image must be specified\n");
352 exit(1);
356 static void axisdev88_machine_init(MachineClass *mc)
358 mc->desc = "AXIS devboard 88";
359 mc->init = axisdev88_init;
360 mc->is_default = 1;
363 DEFINE_MACHINE("axis-dev88", axisdev88_machine_init)