forked from datajerk/x49gp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
s3c2410_uart.c
478 lines (401 loc) · 11.3 KB
/
s3c2410_uart.c
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
/* $Id: s3c2410_uart.c,v 1.4 2008/12/11 12:18:17 ecd Exp $
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <x49gp.h>
#include <s3c2410.h>
#include <s3c2410_intc.h>
typedef struct {
uint32_t ulcon;
uint32_t ucon;
uint32_t ufcon;
uint32_t umcon;
uint32_t utrstat;
uint32_t uerstat;
uint32_t ufstat;
uint32_t umstat;
uint32_t utxh;
uint32_t urxh;
uint32_t ubrdiv;
int int_err;
int int_txd;
int int_rxd;
unsigned int nr_regs;
s3c2410_offset_t *regs;
x49gp_t *x49gp;
} s3c2410_uart_reg_t;
typedef struct {
s3c2410_uart_reg_t uart[3];
} s3c2410_uart_t;
static int
s3c2410_uart_data_init(s3c2410_uart_t *uart)
{
s3c2410_offset_t regs0[] = {
S3C2410_OFFSET(UART0, ULCON, 0x00000000, uart->uart[0].ulcon),
S3C2410_OFFSET(UART0, UCON, 0x00000000, uart->uart[0].ucon),
S3C2410_OFFSET(UART0, UFCON, 0x00000000, uart->uart[0].ufcon),
S3C2410_OFFSET(UART0, UMCON, 0x00000000, uart->uart[0].umcon),
S3C2410_OFFSET(UART0, UTRSTAT, 0x00000006, uart->uart[0].utrstat),
S3C2410_OFFSET(UART0, UERSTAT, 0x00000000, uart->uart[0].uerstat),
S3C2410_OFFSET(UART0, UFSTAT, 0x00000000, uart->uart[0].ufstat),
S3C2410_OFFSET(UART0, UMSTAT, 0x00000000, uart->uart[0].umstat),
S3C2410_OFFSET(UART0, UTXH, 0, uart->uart[0].utxh),
S3C2410_OFFSET(UART0, URXH, 0, uart->uart[0].urxh),
S3C2410_OFFSET(UART0, UBRDIV, 0, uart->uart[0].ubrdiv)
};
s3c2410_offset_t regs1[] = {
S3C2410_OFFSET(UART1, ULCON, 0x00000000, uart->uart[1].ulcon),
S3C2410_OFFSET(UART1, UCON, 0x00000000, uart->uart[1].ucon),
S3C2410_OFFSET(UART1, UFCON, 0x00000000, uart->uart[1].ufcon),
S3C2410_OFFSET(UART1, UMCON, 0x00000000, uart->uart[1].umcon),
S3C2410_OFFSET(UART1, UTRSTAT, 0x00000006, uart->uart[1].utrstat),
S3C2410_OFFSET(UART1, UERSTAT, 0x00000000, uart->uart[1].uerstat),
S3C2410_OFFSET(UART1, UFSTAT, 0x00000000, uart->uart[1].ufstat),
S3C2410_OFFSET(UART1, UMSTAT, 0x00000000, uart->uart[1].umstat),
S3C2410_OFFSET(UART1, UTXH, 0, uart->uart[1].utxh),
S3C2410_OFFSET(UART1, URXH, 0, uart->uart[1].urxh),
S3C2410_OFFSET(UART1, UBRDIV, 0, uart->uart[1].ubrdiv)
};
s3c2410_offset_t regs2[] = {
S3C2410_OFFSET(UART2, ULCON, 0x00000000, uart->uart[2].ulcon),
S3C2410_OFFSET(UART2, UCON, 0x00000000, uart->uart[2].ucon),
S3C2410_OFFSET(UART2, UFCON, 0x00000000, uart->uart[2].ufcon),
S3C2410_OFFSET(UART2, UTRSTAT, 0x00000006, uart->uart[2].utrstat),
S3C2410_OFFSET(UART2, UERSTAT, 0x00000000, uart->uart[2].uerstat),
S3C2410_OFFSET(UART2, UFSTAT, 0x00000000, uart->uart[2].ufstat),
S3C2410_OFFSET(UART2, UTXH, 0, uart->uart[2].utxh),
S3C2410_OFFSET(UART2, URXH, 0, uart->uart[2].urxh),
S3C2410_OFFSET(UART2, UBRDIV, 0, uart->uart[2].ubrdiv)
};
uart->uart[0].regs = malloc(sizeof(regs0));
if (NULL == uart->uart[0].regs) {
fprintf(stderr, "%s:%u: Out of memory\n",
__FUNCTION__, __LINE__);
return -ENOMEM;
}
uart->uart[1].regs = malloc(sizeof(regs1));
if (NULL == uart->uart[1].regs) {
fprintf(stderr, "%s:%u: Out of memory\n",
__FUNCTION__, __LINE__);
free(uart->uart[0].regs);
return -ENOMEM;
}
uart->uart[2].regs = malloc(sizeof(regs2));
if (NULL == uart->uart[2].regs) {
fprintf(stderr, "%s:%u: Out of memory\n",
__FUNCTION__, __LINE__);
free(uart->uart[0].regs);
free(uart->uart[1].regs);
return -ENOMEM;
}
memcpy(uart->uart[0].regs, regs0, sizeof(regs0));
uart->uart[0].nr_regs = sizeof(regs0) / sizeof(regs0[0]);
uart->uart[0].int_err = SUB_INT_ERR0;
uart->uart[0].int_txd = SUB_INT_TXD0;
uart->uart[0].int_rxd = SUB_INT_RXD0;
memcpy(uart->uart[1].regs, regs1, sizeof(regs1));
uart->uart[1].nr_regs = sizeof(regs1) / sizeof(regs1[0]);
uart->uart[1].int_err = SUB_INT_ERR1;
uart->uart[1].int_txd = SUB_INT_TXD1;
uart->uart[1].int_rxd = SUB_INT_RXD1;
memcpy(uart->uart[2].regs, regs2, sizeof(regs2));
uart->uart[2].nr_regs = sizeof(regs2) / sizeof(regs2[0]);
uart->uart[2].int_err = SUB_INT_ERR2;
uart->uart[2].int_txd = SUB_INT_TXD2;
uart->uart[2].int_rxd = SUB_INT_RXD2;
return 0;
}
static uint32_t
s3c2410_uart_read(void *opaque, target_phys_addr_t offset)
{
s3c2410_uart_reg_t *uart_regs = opaque;
x49gp_t *x49gp = uart_regs->x49gp;
s3c2410_offset_t *reg;
#ifdef DEBUG_S3C2410_UART
const char *module;
uint32_t mod_offset;
uint32_t base;
base = (offset & 0x0000c000) >> 14;
switch (base) {
case 0:
module = "s3c2410-uart0";
mod_offset = S3C2410_UART0_BASE;
break;
case 1:
module = "s3c2410-uart1";
mod_offset = S3C2410_UART1_BASE;
break;
case 2:
module = "s3c2410-uart2";
mod_offset = S3C2410_UART2_BASE;
break;
default:
return ~(0);
}
#endif
offset &= ~(0xffffc000);
if (! S3C2410_OFFSET_OK(uart_regs, offset)) {
return ~(0);
}
reg = S3C2410_OFFSET_ENTRY(uart_regs, offset);
#ifdef DEBUG_S3C2410_UART
printf("read %s [%08x] %s [%08lx] data %08x\n",
module, mod_offset, reg->name, (unsigned long) offset, *(reg->datap));
#endif
switch (offset) {
case S3C2410_UART0_URXH:
uart_regs->utrstat &= ~(1 << 0);
if (uart_regs->ucon & (1 << 8)) {
s3c2410_intc_sub_deassert(x49gp, uart_regs->int_rxd);
}
break;
}
return *(reg->datap);
}
static void
s3c2410_uart_write(void *opaque, target_phys_addr_t offset, uint32_t data)
{
s3c2410_uart_reg_t *uart_regs = opaque;
x49gp_t *x49gp = uart_regs->x49gp;
s3c2410_offset_t *reg;
uint32_t base;
#ifdef DEBUG_S3C2410_UART
const char *module;
uint32_t mod_offset, ubrdivn, baud;
#endif
base = (offset & 0x0000c000) >> 14;
#ifdef DEBUG_S3C2410_UART
switch (base) {
case 0:
module = "s3c2410-uart0";
mod_offset = S3C2410_UART0_BASE;
break;
case 1:
module = "s3c2410-uart1";
mod_offset = S3C2410_UART1_BASE;
break;
case 2:
module = "s3c2410-uart2";
mod_offset = S3C2410_UART2_BASE;
break;
default:
return;
}
#endif
offset &= ~(0xffffc000);
if (! S3C2410_OFFSET_OK(uart_regs, offset)) {
return;
}
reg = S3C2410_OFFSET_ENTRY(uart_regs, offset);
#ifdef DEBUG_S3C2410_UART
printf("write %s [%08x] %s [%08lx] data %08x\n",
module, mod_offset, reg->name, (unsigned long) offset, data);
#endif
*(reg->datap) = data;
switch (offset) {
case S3C2410_UART0_UCON:
if (*(reg->datap) & (1 << 9))
s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 1);
if (*(reg->datap) & (1 << 8))
s3c2410_intc_sub_deassert(x49gp, uart_regs->int_rxd);
break;
case S3C2410_UART0_UBRDIV:
#ifdef DEBUG_S3C2410_UART
ubrdivn = (data >> 0) & 0xffff;
if (uart_regs->ucon & (1 << 10)) {
baud = x49gp->UCLK / 16 / (ubrdivn + 1);
printf("%s: UEXTCLK %u, ubrdivn %u, baud %u\n",
module, x49gp->UCLK, ubrdivn, baud);
} else {
baud = x49gp->PCLK / 16 / (ubrdivn + 1);
printf("%s: PCLK %u, ubrdivn %u, baud %u\n",
module, x49gp->PCLK, ubrdivn, baud);
}
#endif
break;
case S3C2410_UART0_UTXH:
if (uart_regs->ucon & (1 << 9))
s3c2410_intc_sub_deassert(x49gp, uart_regs->int_txd);
uart_regs->utrstat |= (1 << 2) | (1 << 1);
if (uart_regs->ucon & (1 << 9))
s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 1);
else
s3c2410_intc_sub_assert(x49gp, uart_regs->int_txd, 0);
if (uart_regs->ucon & (1 << 5)) {
uart_regs->urxh = data;
uart_regs->utrstat |= (1 << 0);
if (uart_regs->ucon & (1 << 8))
s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 1);
else
s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 0);
} else if (base == 2) {
uart_regs->urxh = data;
uart_regs->utrstat |= (1 << 0);
if (uart_regs->ucon & (1 << 8))
s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 1);
else
s3c2410_intc_sub_assert(x49gp, uart_regs->int_rxd, 0);
}
break;
}
}
static int
s3c2410_uart_load(x49gp_module_t *module, GKeyFile *key)
{
s3c2410_uart_reg_t *uart_regs = module->user_data;
s3c2410_offset_t *reg;
int error = 0;
int i;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
for (i = 0; i < uart_regs->nr_regs; i++) {
reg = &uart_regs->regs[i];
if (NULL == reg->name)
continue;
if (x49gp_module_get_u32(module, key, reg->name,
reg->reset, reg->datap))
error = -EAGAIN;
}
return error;
}
static int
s3c2410_uart_save(x49gp_module_t *module, GKeyFile *key)
{
s3c2410_uart_reg_t *uart_regs = module->user_data;
s3c2410_offset_t *reg;
int i;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
for (i = 0; i < uart_regs->nr_regs; i++) {
reg = &uart_regs->regs[i];
if (NULL == reg->name)
continue;
x49gp_module_set_u32(module, key, reg->name, *(reg->datap));
}
return 0;
}
static int
s3c2410_uart_reset(x49gp_module_t *module, x49gp_reset_t reset)
{
s3c2410_uart_reg_t *uart_regs = module->user_data;
s3c2410_offset_t *reg;
int i;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
for (i = 0; i < uart_regs->nr_regs; i++) {
reg = &uart_regs->regs[i];
if (NULL == reg->name)
continue;
*(reg->datap) = reg->reset;
}
return 0;
}
static CPUReadMemoryFunc *s3c2410_uart_readfn[] =
{
s3c2410_uart_read,
s3c2410_uart_read,
s3c2410_uart_read
};
static CPUWriteMemoryFunc *s3c2410_uart_writefn[] =
{
s3c2410_uart_write,
s3c2410_uart_write,
s3c2410_uart_write
};
static int
s3c2410_uart_init(x49gp_module_t *module)
{
s3c2410_uart_reg_t *uart_regs = module->user_data;
int iotype;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
iotype = cpu_register_io_memory(s3c2410_uart_readfn,
s3c2410_uart_writefn, uart_regs);
#ifdef DEBUG_S3C2410_UART
printf("%s: iotype %08x\n", __FUNCTION__, iotype);
#endif
cpu_register_physical_memory(S3C2410_UART0_BASE, S3C2410_MAP_SIZE, iotype);
return 0;
}
static int
s3c2410_uart_exit(x49gp_module_t *module)
{
s3c2410_uart_reg_t *uart_regs;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
if (module->user_data) {
uart_regs = module->user_data;
if (uart_regs->regs)
free(uart_regs->regs);
}
x49gp_module_unregister(module);
free(module);
return 0;
}
int
x49gp_s3c2410_uart_init(x49gp_t *x49gp)
{
s3c2410_uart_t *uart;
x49gp_module_t *module;
uart = malloc(sizeof(s3c2410_uart_t));
if (NULL == uart) {
fprintf(stderr, "%s:%u: Out of memory\n",
__FUNCTION__, __LINE__);
return -ENOMEM;
}
memset(uart, 0, sizeof(s3c2410_uart_t));
if (s3c2410_uart_data_init(uart)) {
free(uart);
return -ENOMEM;
}
uart->uart[0].x49gp = x49gp;
uart->uart[1].x49gp = x49gp;
uart->uart[2].x49gp = x49gp;
if (x49gp_module_init(x49gp, "s3c2410-uart0",
s3c2410_uart_init,
s3c2410_uart_exit,
s3c2410_uart_reset,
s3c2410_uart_load,
s3c2410_uart_save,
&uart->uart[0], &module)) {
return -1;
}
if (x49gp_module_register(module)) {
return -1;
}
if (x49gp_module_init(x49gp, "s3c2410-uart1",
s3c2410_uart_init,
s3c2410_uart_exit,
s3c2410_uart_reset,
s3c2410_uart_load,
s3c2410_uart_save,
&uart->uart[1], &module)) {
return -1;
}
if (x49gp_module_register(module)) {
return -1;
}
if (x49gp_module_init(x49gp, "s3c2410-uart2",
s3c2410_uart_init,
s3c2410_uart_exit,
s3c2410_uart_reset,
s3c2410_uart_load,
s3c2410_uart_save,
&uart->uart[2], &module)) {
return -1;
}
if (x49gp_module_register(module)) {
return -1;
}
return 0;
}