]>
Commit | Line | Data |
---|---|---|
83fa1010 TS |
1 | /* |
2 | * QEMU ETRAX System Emulator | |
3 | * | |
4 | * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB. | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
83c9f4ca | 25 | #include "hw/sysbus.h" |
927d4878 | 26 | #include "char/char.h" |
1de7afc9 | 27 | #include "qemu/log.h" |
83fa1010 | 28 | |
bbaf29c7 EI |
29 | #define D(x) |
30 | ||
72af9170 EI |
31 | #define RW_TR_CTRL (0x00 / 4) |
32 | #define RW_TR_DMA_EN (0x04 / 4) | |
33 | #define RW_REC_CTRL (0x08 / 4) | |
34 | #define RW_DOUT (0x1c / 4) | |
35 | #define RS_STAT_DIN (0x20 / 4) | |
36 | #define R_STAT_DIN (0x24 / 4) | |
37 | #define RW_INTR_MASK (0x2c / 4) | |
38 | #define RW_ACK_INTR (0x30 / 4) | |
39 | #define R_INTR (0x34 / 4) | |
40 | #define R_MASKED_INTR (0x38 / 4) | |
41 | #define R_MAX (0x3c / 4) | |
83fa1010 | 42 | |
f062058f EI |
43 | #define STAT_DAV 16 |
44 | #define STAT_TR_IDLE 22 | |
45 | #define STAT_TR_RDY 24 | |
46 | ||
f2964260 | 47 | struct etrax_serial |
83fa1010 | 48 | { |
2a9859e7 | 49 | SysBusDevice busdev; |
dbfb57f3 | 50 | MemoryRegion mmio; |
2a9859e7 EI |
51 | CharDriverState *chr; |
52 | qemu_irq irq; | |
f062058f | 53 | |
2a9859e7 | 54 | int pending_tx; |
f062058f | 55 | |
f2fcffbb EI |
56 | uint8_t rx_fifo[16]; |
57 | unsigned int rx_fifo_pos; | |
58 | unsigned int rx_fifo_len; | |
59 | ||
2a9859e7 EI |
60 | /* Control registers. */ |
61 | uint32_t regs[R_MAX]; | |
f062058f EI |
62 | }; |
63 | ||
f2964260 | 64 | static void ser_update_irq(struct etrax_serial *s) |
f062058f | 65 | { |
72af9170 | 66 | |
f2fcffbb EI |
67 | if (s->rx_fifo_len) { |
68 | s->regs[R_INTR] |= 8; | |
69 | } else { | |
70 | s->regs[R_INTR] &= ~8; | |
71 | } | |
72 | ||
73 | s->regs[R_MASKED_INTR] = s->regs[R_INTR] & s->regs[RW_INTR_MASK]; | |
2a9859e7 | 74 | qemu_set_irq(s->irq, !!s->regs[R_MASKED_INTR]); |
83fa1010 | 75 | } |
f062058f | 76 | |
dbfb57f3 | 77 | static uint64_t |
a8170e5e | 78 | ser_read(void *opaque, hwaddr addr, unsigned int size) |
83fa1010 | 79 | { |
2a9859e7 | 80 | struct etrax_serial *s = opaque; |
2a9859e7 EI |
81 | uint32_t r = 0; |
82 | ||
83 | addr >>= 2; | |
84 | switch (addr) | |
85 | { | |
86 | case R_STAT_DIN: | |
f2fcffbb EI |
87 | r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 15]; |
88 | if (s->rx_fifo_len) { | |
89 | r |= 1 << STAT_DAV; | |
90 | } | |
91 | r |= 1 << STAT_TR_RDY; | |
92 | r |= 1 << STAT_TR_IDLE; | |
2a9859e7 EI |
93 | break; |
94 | case RS_STAT_DIN: | |
f2fcffbb EI |
95 | r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 15]; |
96 | if (s->rx_fifo_len) { | |
97 | r |= 1 << STAT_DAV; | |
98 | s->rx_fifo_len--; | |
99 | } | |
100 | r |= 1 << STAT_TR_RDY; | |
101 | r |= 1 << STAT_TR_IDLE; | |
2a9859e7 EI |
102 | break; |
103 | default: | |
104 | r = s->regs[addr]; | |
8cc7c395 | 105 | D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr, r)); |
2a9859e7 EI |
106 | break; |
107 | } | |
108 | return r; | |
83fa1010 TS |
109 | } |
110 | ||
83fa1010 | 111 | static void |
a8170e5e | 112 | ser_write(void *opaque, hwaddr addr, |
dbfb57f3 | 113 | uint64_t val64, unsigned int size) |
83fa1010 | 114 | { |
2a9859e7 | 115 | struct etrax_serial *s = opaque; |
dbfb57f3 EI |
116 | uint32_t value = val64; |
117 | unsigned char ch = val64; | |
2a9859e7 | 118 | |
8cc7c395 | 119 | D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr, value)); |
2a9859e7 EI |
120 | addr >>= 2; |
121 | switch (addr) | |
122 | { | |
123 | case RW_DOUT: | |
2cc6e0a1 | 124 | qemu_chr_fe_write(s->chr, &ch, 1); |
f2fcffbb | 125 | s->regs[R_INTR] |= 3; |
2a9859e7 EI |
126 | s->pending_tx = 1; |
127 | s->regs[addr] = value; | |
128 | break; | |
129 | case RW_ACK_INTR: | |
f2fcffbb EI |
130 | if (s->pending_tx) { |
131 | value &= ~1; | |
2a9859e7 | 132 | s->pending_tx = 0; |
8cc7c395 EI |
133 | D(qemu_log("fixedup value=%x r_intr=%x\n", |
134 | value, s->regs[R_INTR])); | |
2a9859e7 | 135 | } |
f2fcffbb EI |
136 | s->regs[addr] = value; |
137 | s->regs[R_INTR] &= ~value; | |
138 | D(printf("r_intr=%x\n", s->regs[R_INTR])); | |
2a9859e7 EI |
139 | break; |
140 | default: | |
141 | s->regs[addr] = value; | |
142 | break; | |
143 | } | |
144 | ser_update_irq(s); | |
83fa1010 TS |
145 | } |
146 | ||
dbfb57f3 EI |
147 | static const MemoryRegionOps ser_ops = { |
148 | .read = ser_read, | |
149 | .write = ser_write, | |
150 | .endianness = DEVICE_NATIVE_ENDIAN, | |
151 | .valid = { | |
152 | .min_access_size = 4, | |
153 | .max_access_size = 4 | |
154 | } | |
83fa1010 TS |
155 | }; |
156 | ||
f062058f | 157 | static void serial_receive(void *opaque, const uint8_t *buf, int size) |
83fa1010 | 158 | { |
2a9859e7 | 159 | struct etrax_serial *s = opaque; |
f2fcffbb EI |
160 | int i; |
161 | ||
162 | /* Got a byte. */ | |
163 | if (s->rx_fifo_len >= 16) { | |
8cc7c395 | 164 | qemu_log("WARNING: UART dropped char.\n"); |
f2fcffbb EI |
165 | return; |
166 | } | |
167 | ||
168 | for (i = 0; i < size; i++) { | |
169 | s->rx_fifo[s->rx_fifo_pos] = buf[i]; | |
170 | s->rx_fifo_pos++; | |
171 | s->rx_fifo_pos &= 15; | |
172 | s->rx_fifo_len++; | |
173 | } | |
f062058f | 174 | |
2a9859e7 | 175 | ser_update_irq(s); |
f062058f EI |
176 | } |
177 | ||
178 | static int serial_can_receive(void *opaque) | |
179 | { | |
2a9859e7 EI |
180 | struct etrax_serial *s = opaque; |
181 | int r; | |
f062058f | 182 | |
2a9859e7 | 183 | /* Is the receiver enabled? */ |
f2fcffbb EI |
184 | if (!(s->regs[RW_REC_CTRL] & (1 << 3))) { |
185 | return 0; | |
186 | } | |
f062058f | 187 | |
f2fcffbb | 188 | r = sizeof(s->rx_fifo) - s->rx_fifo_len; |
2a9859e7 | 189 | return r; |
f062058f EI |
190 | } |
191 | ||
192 | static void serial_event(void *opaque, int event) | |
193 | { | |
194 | ||
195 | } | |
196 | ||
20be39de | 197 | static void etraxfs_ser_reset(DeviceState *d) |
f062058f | 198 | { |
20be39de | 199 | struct etrax_serial *s = container_of(d, typeof(*s), busdev.qdev); |
2a9859e7 EI |
200 | |
201 | /* transmitter begins ready and idle. */ | |
202 | s->regs[RS_STAT_DIN] |= (1 << STAT_TR_RDY); | |
203 | s->regs[RS_STAT_DIN] |= (1 << STAT_TR_IDLE); | |
204 | ||
20be39de EI |
205 | s->regs[RW_REC_CTRL] = 0x10000; |
206 | ||
207 | } | |
208 | ||
209 | static int etraxfs_ser_init(SysBusDevice *dev) | |
210 | { | |
211 | struct etrax_serial *s = FROM_SYSBUS(typeof (*s), dev); | |
20be39de | 212 | |
2a9859e7 | 213 | sysbus_init_irq(dev, &s->irq); |
dbfb57f3 | 214 | memory_region_init_io(&s->mmio, &ser_ops, s, "etraxfs-serial", R_MAX * 4); |
750ecd44 | 215 | sysbus_init_mmio(dev, &s->mmio); |
dbfb57f3 | 216 | |
0beb4942 | 217 | s->chr = qemu_char_get_next_serial(); |
2a9859e7 EI |
218 | if (s->chr) |
219 | qemu_chr_add_handlers(s->chr, | |
220 | serial_can_receive, serial_receive, | |
221 | serial_event, s); | |
81a322d4 | 222 | return 0; |
83fa1010 | 223 | } |
4b816985 | 224 | |
999e12bb AL |
225 | static void etraxfs_ser_class_init(ObjectClass *klass, void *data) |
226 | { | |
39bffca2 | 227 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
228 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
229 | ||
230 | k->init = etraxfs_ser_init; | |
39bffca2 | 231 | dc->reset = etraxfs_ser_reset; |
999e12bb AL |
232 | } |
233 | ||
8c43a6f0 | 234 | static const TypeInfo etraxfs_ser_info = { |
39bffca2 AL |
235 | .name = "etraxfs,serial", |
236 | .parent = TYPE_SYS_BUS_DEVICE, | |
237 | .instance_size = sizeof(struct etrax_serial), | |
238 | .class_init = etraxfs_ser_class_init, | |
20be39de EI |
239 | }; |
240 | ||
83f7d43a | 241 | static void etraxfs_serial_register_types(void) |
4b816985 | 242 | { |
39bffca2 | 243 | type_register_static(&etraxfs_ser_info); |
4b816985 EI |
244 | } |
245 | ||
83f7d43a | 246 | type_init(etraxfs_serial_register_types) |