at91_pio.c
16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* AT91 Parallel I/O Controller
*
* Copyright (c) 2009 Filip Navara
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* TODO: Glitch-filter, multi-driver (ie. open drain) */
#include "sysbus.h"
//#define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
#define DPRINTF(fmt, ...) do { } while (0)
/* Segment Display */
#define SEGTOP // displayed at the top of the terminal window
#define SEGPUTCHAR(com, pin, offset, symb) \
if (!(s->odsr & (1 << com)) && (s->odsr & (1 << pin))) segline[offset] = symb
#ifdef SEGTOP
#define SEGPRINT printf("\x1B[%d;60H%s", ln++, segline)
#else
#define SEGPRINT printf("\x1B[20C%s\n", segline)
#endif
/*
* Input/Output GPIO pins:
* 32x PIO device
* 32x peripheral A
* 32x peripheral B
*/
#define PIO_SIZE 0x200
#define PIO_PINS 32
#define PIO_PER 0x00 /* PIO Enable Register */
#define PIO_PDR 0x04 /* PIO Disable Register */
#define PIO_PSR 0x08 /* PIO Status Register */
#define PIO_OER 0x10 /* Output Enable Register */
#define PIO_ODR 0x14 /* Output Disable Register */
#define PIO_OSR 0x18 /* Output Status Register */
#define PIO_IFER 0x20 /* Input Filter Enable Register */
#define PIO_IFDR 0x24 /* Input Filter Disable Register */
#define PIO_IFSR 0x28 /* Input Filter Status Register */
#define PIO_SODR 0x30 /* Set Output Data Register */
#define PIO_CODR 0x34 /* Clear Output Data Register */
#define PIO_ODSR 0x38 /* Output Data Status Register */
#define PIO_PDSR 0x3c /* Pin Data Status Register */
#define PIO_IER 0x40 /* Interrupt Enable Register */
#define PIO_IDR 0x44 /* Interrupt Disable Register */
#define PIO_IMR 0x48 /* Interrupt Mask Register */
#define PIO_ISR 0x4c /* Interrupt Status Register */
#define PIO_MDER 0x50 /* Multi-driver Enable Register */
#define PIO_MDDR 0x54 /* Multi-driver Disable Register */
#define PIO_MDSR 0x58 /* Multi-driver Status Register */
#define PIO_PPUDR 0x60 /* Pull-up Disable Register */
#define PIO_PPUER 0x64 /* Pull-up Enable Register */
#define PIO_PPUSR 0x68 /* Pull-up Status Register */
#define PIO_ASR 0x70 /* Select A Register */
#define PIO_BSR 0x74 /* Select B Register */
#define PIO_ABSR 0x78 /* AB Select Status Register */
#define PIO_OWER 0xa0 /* Output Write Enable Register */
#define PIO_OWDR 0xa4 /* Output Write Disable Register */
#define PIO_OWSR 0xa8 /* Output Write Status Register */
typedef struct PIOState {
SysBusDevice busdev;
qemu_irq out[PIO_PINS * 3];
qemu_irq parent_irq;
uint32_t psr;
uint32_t osr;
uint32_t ifsr;
uint32_t odsr;
uint32_t pdsr;
uint32_t imr;
uint32_t isr;
uint32_t mdsr;
uint32_t ppusr;
uint32_t absr;
uint32_t owsr;
/* Mask of unknown state of PIO pins, needed for pull-up resistor
implementation */
uint32_t unknown_state;
int portid;
} PIOState;
void at91_pio_set_pin(void *opaque, int pin, int level)
{
DPRINTF("In at91_pio_set_pin, opaque=%p, pin=%d, level=%d\n", opaque, pin, level);
PIOState *s = opaque;
int mask = 1 << (pin % PIO_PINS);
int input_set = pin / PIO_PINS;
int output_set = !!(s->absr & mask);
uint32_t saved_pdsr;
if (input_set == 0) {
/* PIO pin -> Peripheral / IO */
DPRINTF("In at91_pio_set_pin, s->osr=%08x, s->psr=%08x, mask=%08x\n",s->osr, s->psr, mask);
/* Skip input if output mode is enabled for the pin */
if (s->osr & mask)
return;
if (s->psr & mask) {
saved_pdsr = s->pdsr;
s->pdsr &= ~mask;
s->unknown_state &= ~mask;
if (level == -1) {
s->unknown_state |= mask;
} else if (level) {
DPRINTF("In at91_pio_set_pin, setting level\n");
s->unknown_state &= ~mask;
s->pdsr |= mask;
}
if (saved_pdsr != s->pdsr) {
s->isr |= mask;
qemu_set_irq(s->parent_irq, !!(s->isr & s->imr));
}
} else {
qemu_set_irq(s->out[PIO_PINS + (output_set * PIO_PINS)], level);
}
DPRINTF("In at91_pio_set_pin, s->pdsr=%08x\n",s->pdsr);
} else {
/* Peripheral -> PIO pin */
if ((~s->psr & mask) && input_set == output_set) {
qemu_set_irq(s->out[pin & PIO_PINS], level);
}
}
}
static uint32_t at91_pio_mem_read(void *opaque, target_phys_addr_t offset)
{
PIOState *s = opaque;
int isr;
offset &= PIO_SIZE - 1;
switch (offset) {
case PIO_PSR:
return s->psr;
case PIO_OSR:
return s->osr;
case PIO_IFSR:
return s->ifsr;
case PIO_ODSR:
return s->odsr;
case PIO_PDSR:
DPRINTF("In at91_pio_mem_read, reading PDSR, s->pdsr=%08x, s->unknown_state=%08x, retval=%08x\n",
s->pdsr, s->unknown_state,
(s->pdsr & ~s->unknown_state) |
(s->ppusr & s->unknown_state));
return
(s->pdsr & ~s->unknown_state) |
(s->ppusr & s->unknown_state);
case PIO_IMR:
return s->imr;
case PIO_ISR:
isr = s->isr;
s->isr = 0;
qemu_set_irq(s->parent_irq, 0);
return isr;
case PIO_MDSR:
return s->mdsr;
case PIO_PPUSR:
return s->ppusr;
case PIO_ABSR:
return s->absr;
case PIO_OWSR:
return s->owsr;
default:
return 0;
}
}
static void at91_pio_mem_write(void *opaque, target_phys_addr_t offset,
uint32_t value)
{
PIOState *s = opaque;
int i;
DPRINTF("Writing PIO: portid=%d, offset=0x%08lx, value=0x%08x\n", s->portid, offset, value);
offset &= PIO_SIZE - 1;
uint32_t prev;
int segdisp_refresh = 0;
switch (offset) {
case PIO_PER:
s->psr |= value; //we should also detect LED state change in some cases
//printf("enabling, psr=0x%08x\n", s->psr);
break;
case PIO_PDR:
s->psr &= ~value;
//printf("disabling, psr=0x%08x\n", s->psr);
break;
case PIO_OER:
s->osr |= value;
break;
case PIO_ODR:
s->osr &= ~value;
break;
case PIO_IFER:
s->ifsr |= value;
break;
case PIO_IFDR:
s->ifsr &= ~value;
break;
case PIO_SODR:
prev = s->odsr;
s->odsr |= value;
for (i = 0; i < PIO_PINS; i++)
if (value & (1 << i) & s->osr & s->psr)
{
qemu_set_irq(s->out[i], 1);
if ((prev & (1 << i)) != (s->odsr & (1 << i)))
{
DPRINTF("Port %c%d set\n",'A' + s->portid, i);
if (
(s->portid == 1) && (i >= 20) && (i <= 27)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) )
&& !(s->odsr & (1 << 31))
)
segdisp_refresh = 1;
else if (
((s->portid == 1) && ((i == 28) || (i == 30))
&& !(s->odsr & (1 << 31)))
|| ((s->portid == 1) && (i == 31)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) ))
)
segdisp_refresh = 1;
else if ((s->portid == 1) && (i == 8))
printf("LED1 off\n");
else if ((s->portid == 2) && (i == 29))
printf("LED2 off\n");
}
}
break;
case PIO_CODR:
prev = s->odsr;
s->odsr &= ~value;
for (i = 0; i < PIO_PINS; i++)
if (value & (1 << i) & s->osr & s->psr)
{
qemu_set_irq(s->out[i], 0);
if ((prev & (1 << i)) != (s->odsr & (1 << i)))
{
DPRINTF("Port %c%d reset\n",'A' + s->portid, i);
if (
(s->portid == 1) && (i >= 20) && (i <= 27)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) )
&& !(s->odsr & (1 << 31))
)
segdisp_refresh = 1;
else if (
((s->portid == 1) && ((i == 28) || (i == 30))
&& !(s->odsr & (1 << 31)))
|| ((s->portid == 1) && (i == 31)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) ))
)
segdisp_refresh = 1;
else if ((s->portid == 1) && (i == 8))
printf("LED1 on\n");
else if ((s->portid == 2) && (i == 29))
printf("LED2 on\n");
}
}
break;
case PIO_ODSR:
prev = s->odsr;
s->odsr = (s->odsr & ~s->owsr) | (value & s->owsr);
for (i = 0; i < PIO_PINS; i++)
if (s->owsr & (1 << i))
{
qemu_set_irq(s->out[i], !!(value & (1 << i)));
if ((prev & (1 << i)) != (s->odsr & (1 << i)))
{
DPRINTF("Port %c%d written\n",'A' + s->portid, i);
if (
(s->portid == 1) && (i >= 20) && (i <= 27)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) )
&& !(s->odsr & (1 << 31))
)
segdisp_refresh = 1;
else if (
((s->portid == 1) && ((i == 28) || (i == 30))
&& !(s->odsr & (1 << 31)))
|| ((s->portid == 1) && (i == 31)
&& ( !(s->odsr & (1 << 28)) || !(s->odsr & (1 << 30)) ))
)
segdisp_refresh = 1;
else if ((s->portid == 1) && (i == 8))
printf("LED1 %s\n", ((s->odsr & (1 << i)) ? "off" : "on"));
else if ((s->portid == 2) && (i == 29))
printf("LED2 %s\n", ((s->odsr & (1 << i)) ? "off" : "on"));
}
}
break;
case PIO_IER:
s->imr |= value;
break;
case PIO_IDR:
s->imr &= ~value;
break;
case PIO_MDER:
s->mdsr |= value;
break;
case PIO_MDDR:
s->mdsr &= ~value;
break;
case PIO_PPUER:
s->ppusr |= value;
break;
case PIO_PPUDR:
s->ppusr &= ~value;
break;
case PIO_ASR:
s->absr &= ~value;
break;
case PIO_BSR:
s->absr |= value;
break;
case PIO_OWER:
s->owsr |= value;
break;
case PIO_OWDR:
s->owsr &= ~value;
break;
default:
return;
}
if (segdisp_refresh)
{
// printf("Segment display changed\n");
char segline[12];
int ln;
#ifdef SEGTOP
printf("\x1B[s" "\x1B""7");
#else
printf("\n");
#endif
printf("\x1B[91m");
strcpy(segline, " ");
ln = 1;
SEGPRINT;
SEGPRINT;
if (s->odsr & (1 << 31))
{
while (ln <= 7)
SEGPRINT;
}
else
{
strcpy(segline, " ");
SEGPUTCHAR(28,25,0+1,'-');
SEGPUTCHAR(28,25,0+2,'-');
SEGPUTCHAR(30,25,6+1,'-');
SEGPUTCHAR(30,25,6+2,'-');
SEGPRINT;
strcpy(segline, " ");
SEGPUTCHAR(28,27,0+0,'|');
SEGPUTCHAR(28,24,0+3,'|');
SEGPUTCHAR(30,27,6+0,'|');
SEGPUTCHAR(30,24,6+3,'|');
SEGPRINT;
strcpy(segline, " ");
SEGPUTCHAR(28,26,0+1,'-');
SEGPUTCHAR(28,26,0+2,'-');
SEGPUTCHAR(30,26,6+1,'-');
SEGPUTCHAR(30,26,6+2,'-');
SEGPRINT;
strcpy(segline, " ");
SEGPUTCHAR(28,20,0+0,'|');
SEGPUTCHAR(28,22,0+3,'|');
SEGPUTCHAR(30,20,6+0,'|');
SEGPUTCHAR(30,22,6+3,'|');
SEGPRINT;
strcpy(segline, " ");
SEGPUTCHAR(28,21,0+1,'-');
SEGPUTCHAR(28,21,0+2,'-');
SEGPUTCHAR(30,21,6+1,'-');
SEGPUTCHAR(30,21,6+2,'-');
SEGPUTCHAR(28,23,0+4,'.');
SEGPUTCHAR(30,23,6+4,'.');
SEGPRINT;
}
#ifdef SEGTOP
printf("\x1B""8" "\x1B[u");
#else
printf("\x1B[8A");
#endif
printf("\x1B[0m");
fflush(stdout);
}
}
static CPUReadMemoryFunc *at91_pio_readfn[] = {
at91_pio_mem_read,
at91_pio_mem_read,
at91_pio_mem_read,
};
static CPUWriteMemoryFunc *at91_pio_writefn[] = {
at91_pio_mem_write,
at91_pio_mem_write,
at91_pio_mem_write,
};
static void at91_pio_save(QEMUFile *f, void *opaque)
{
PIOState *s = opaque;
qemu_put_be32(f, s->psr);
qemu_put_be32(f, s->osr);
qemu_put_be32(f, s->ifsr);
qemu_put_be32(f, s->odsr);
qemu_put_be32(f, s->pdsr);
qemu_put_be32(f, s->imr);
qemu_put_be32(f, s->isr);
qemu_put_be32(f, s->mdsr);
qemu_put_be32(f, s->ppusr);
qemu_put_be32(f, s->absr);
qemu_put_be32(f, s->owsr);
qemu_put_be32(f, s->unknown_state);
}
static int at91_pio_load(QEMUFile *f, void *opaque, int version_id)
{
PIOState *s = opaque;
if (version_id != 1)
return -EINVAL;
s->psr = qemu_get_be32(f);
s->osr = qemu_get_be32(f);
s->ifsr = qemu_get_be32(f);
s->odsr = qemu_get_be32(f);
s->pdsr = qemu_get_be32(f);
s->imr = qemu_get_be32(f);
s->isr = qemu_get_be32(f);
s->mdsr = qemu_get_be32(f);
s->ppusr = qemu_get_be32(f);
s->absr = qemu_get_be32(f);
s->owsr = qemu_get_be32(f);
s->unknown_state = qemu_get_be32(f);
return 0;
}
static void at91_pio_reset(void *opaque)
{
PIOState *s = opaque;
s->psr = 0xffffffff;
s->osr = 0;
s->ifsr = 0;
s->odsr = 0;
s->pdsr = 0;
s->imr = 0;
s->isr = 0;
s->mdsr = 0;
s->ppusr = 0;
s->absr = 0;
s->owsr = 0;
s->unknown_state = 0xffffffff;
}
void* at91_io_state[5];
int at91_io_cnt = 0;
static void at91_pio_init(SysBusDevice *dev)
{
DPRINTF("at91_pio_init called\n");
PIOState *s = FROM_SYSBUS(typeof (*s), dev);
s->portid = at91_io_cnt;
at91_io_state[at91_io_cnt++] = s;
int pio_regs;
sysbus_init_irq(dev, &s->parent_irq);
qdev_init_gpio_in(&dev->qdev, at91_pio_set_pin, PIO_PINS * 3);
qdev_init_gpio_out(&dev->qdev, s->out, PIO_PINS * 3);
pio_regs = cpu_register_io_memory(at91_pio_readfn, at91_pio_writefn, s);
sysbus_init_mmio(dev, PIO_SIZE, pio_regs);
at91_pio_reset(s);
qemu_register_reset(at91_pio_reset, s);
register_savevm("at91_pio", -1, 1, at91_pio_save, at91_pio_load, s);
}
static void at91_pio_register(void)
{
sysbus_register_dev("at91,pio", sizeof(PIOState), at91_pio_init);
}
device_init(at91_pio_register)