blob: 2c9dd6e10934ce4e1ebcff7899e705d647a7aa96 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/* Copyright (C) 2007-2008 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12#include "qemu_file.h"
13#include "arm_pic.h"
14#include "goldfish_device.h"
15
16#define PDEV_BUS_OP_DONE (0x00)
17#define PDEV_BUS_OP_REMOVE_DEV (0x04)
18#define PDEV_BUS_OP_ADD_DEV (0x08)
19
20#define PDEV_BUS_OP_INIT (0x00)
21
22#define PDEV_BUS_OP (0x00)
23#define PDEV_BUS_GET_NAME (0x04)
24#define PDEV_BUS_NAME_LEN (0x08)
25#define PDEV_BUS_ID (0x0c)
26#define PDEV_BUS_IO_BASE (0x10)
27#define PDEV_BUS_IO_SIZE (0x14)
28#define PDEV_BUS_IRQ (0x18)
29#define PDEV_BUS_IRQ_COUNT (0x1c)
30
31struct bus_state {
32 struct goldfish_device dev;
33 struct goldfish_device *current;
34};
35
36qemu_irq *goldfish_pic;
37static struct goldfish_device *first_device;
38static struct goldfish_device *last_device;
39uint32_t goldfish_free_base;
40uint32_t goldfish_free_irq;
41
42void goldfish_device_set_irq(struct goldfish_device *dev, int irq, int level)
43{
44 if(irq >= dev->irq_count)
45 cpu_abort (cpu_single_env, "goldfish_device_set_irq: Bad irq %d >= %d\n", irq, dev->irq_count);
46 else
47 qemu_set_irq(goldfish_pic[dev->irq + irq], level);
48}
49
50int goldfish_add_device_no_io(struct goldfish_device *dev)
51{
52 if(dev->base == 0) {
53 dev->base = goldfish_free_base;
54 goldfish_free_base += dev->size;
55 }
56 if(dev->irq == 0 && dev->irq_count > 0) {
57 dev->irq = goldfish_free_irq;
58 goldfish_free_irq += dev->irq_count;
59 }
60 //printf("goldfish_add_device: %s, base %x %x, irq %d %d\n",
61 // dev->name, dev->base, dev->size, dev->irq, dev->irq_count);
62 dev->next = NULL;
63 if(last_device) {
64 last_device->next = dev;
65 }
66 else {
67 first_device = dev;
68 }
69 last_device = dev;
70 return 0;
71}
72
73int goldfish_device_add(struct goldfish_device *dev,
74 CPUReadMemoryFunc **mem_read,
75 CPUWriteMemoryFunc **mem_write,
76 void *opaque)
77{
78 int iomemtype;
79 goldfish_add_device_no_io(dev);
80 iomemtype = cpu_register_io_memory(0, mem_read,
81 mem_write, opaque);
82 cpu_register_physical_memory(dev->base, dev->size, iomemtype);
83 return 0;
84}
85
86static uint32_t goldfish_bus_read(void *opaque, target_phys_addr_t offset)
87{
88 struct bus_state *s = (struct bus_state *)opaque;
89 offset -= s->dev.base;
90
91 switch (offset) {
92 case PDEV_BUS_OP:
93 if(s->current) {
94 s->current->reported_state = 1;
95 s->current = s->current->next;
96 }
97 else {
98 s->current = first_device;
99 }
100 while(s->current && s->current->reported_state == 1)
101 s->current = s->current->next;
102 if(s->current)
103 return PDEV_BUS_OP_ADD_DEV;
104 else {
105 goldfish_device_set_irq(&s->dev, 0, 0);
106 return PDEV_BUS_OP_DONE;
107 }
108
109 case PDEV_BUS_NAME_LEN:
110 return s->current ? strlen(s->current->name) : 0;
111 case PDEV_BUS_ID:
112 return s->current ? s->current->id : 0;
113 case PDEV_BUS_IO_BASE:
114 return s->current ? s->current->base : 0;
115 case PDEV_BUS_IO_SIZE:
116 return s->current ? s->current->size : 0;
117 case PDEV_BUS_IRQ:
118 return s->current ? s->current->irq : 0;
119 case PDEV_BUS_IRQ_COUNT:
120 return s->current ? s->current->irq_count : 0;
121 default:
122 cpu_abort (cpu_single_env, "goldfish_bus_read: Bad offset %x\n", offset);
123 return 0;
124 }
125}
126
127static void goldfish_bus_op_init(struct bus_state *s)
128{
129 struct goldfish_device *dev = first_device;
130 while(dev) {
131 dev->reported_state = 0;
132 dev = dev->next;
133 }
134 s->current = NULL;
135 goldfish_device_set_irq(&s->dev, 0, first_device != NULL);
136}
137
138static void goldfish_bus_write(void *opaque, target_phys_addr_t offset, uint32_t value)
139{
140 struct bus_state *s = (struct bus_state *)opaque;
141 offset -= s->dev.base;
142
143 switch(offset) {
144 case PDEV_BUS_OP:
145 switch(value) {
146 case PDEV_BUS_OP_INIT:
147 goldfish_bus_op_init(s);
148 break;
149 default:
150 cpu_abort (cpu_single_env, "goldfish_bus_write: Bad PDEV_BUS_OP value %x\n", value);
151 };
152 break;
153 case PDEV_BUS_GET_NAME:
154 if(s->current)
155 pmemcpy(value, s->current->name, strlen(s->current->name));
156 break;
157 default:
158 cpu_abort (cpu_single_env, "goldfish_bus_write: Bad offset %x\n", offset);
159 }
160}
161
162static CPUReadMemoryFunc *goldfish_bus_readfn[] = {
163 goldfish_bus_read,
164 goldfish_bus_read,
165 goldfish_bus_read
166};
167
168static CPUWriteMemoryFunc *goldfish_bus_writefn[] = {
169 goldfish_bus_write,
170 goldfish_bus_write,
171 goldfish_bus_write
172};
173
174
175static struct bus_state bus_state = {
176 .dev = {
177 .name = "goldfish_device_bus",
178 .id = -1,
179 .base = 0x10001000,
180 .size = 0x1000,
181 .irq = 1,
182 .irq_count = 1,
183 }
184};
185
186void goldfish_device_init(qemu_irq *pic, uint32_t base, uint32_t size, uint32_t irq, uint32_t irq_count)
187{
188 goldfish_pic = pic;
189 goldfish_free_base = base;
190 goldfish_free_irq = irq;
191}
192
193int goldfish_device_bus_init(uint32_t base, uint32_t irq)
194{
195 bus_state.dev.base = base;
196 bus_state.dev.irq = irq;
197
198 return goldfish_device_add(&bus_state.dev, goldfish_bus_readfn, goldfish_bus_writefn, &bus_state);
199}
200