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#include <common.h>
27#include <asm/processor.h>
28#include <asm/leon.h>
29#include <ambapp.h>
30#include <serial.h>
31#include <linux/compiler.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
35ambapp_dev_apbuart *leon3_apbuart = NULL;
36
37static int leon3_serial_init(void)
38{
39 ambapp_apbdev apbdev;
40 unsigned int tmp;
41
42
43 if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) {
44
45 leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address;
46
47
48
49
50
51
52
53 leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER;
54
55
56 tmp = READ_WORD(leon3_apbuart->ctrl);
57
58 leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) |
59 LEON_REG_UART_CTRL_RE |
60 LEON_REG_UART_CTRL_TE);
61
62 return 0;
63 }
64 return -1;
65}
66
67static void leon3_serial_putc_raw(const char c)
68{
69 if (!leon3_apbuart)
70 return;
71
72
73 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ;
74
75
76 leon3_apbuart->data = c;
77
78#ifdef LEON_DEBUG
79
80 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ;
81#endif
82}
83
84static void leon3_serial_putc(const char c)
85{
86 if (c == '\n')
87 leon3_serial_putc_raw('\r');
88
89 leon3_serial_putc_raw(c);
90}
91
92static int leon3_serial_getc(void)
93{
94 if (!leon3_apbuart)
95 return 0;
96
97
98 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ;
99
100
101 return READ_WORD(leon3_apbuart->data);
102}
103
104static int leon3_serial_tstc(void)
105{
106 if (leon3_apbuart)
107 return (READ_WORD(leon3_apbuart->status) &
108 LEON_REG_UART_STATUS_DR);
109 return 0;
110}
111
112
113static void leon3_serial_setbrg(void)
114{
115
116 unsigned int scaler;
117 if (leon3_apbuart && (gd->baudrate > 0)) {
118 scaler =
119 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
120 5) / 10;
121 leon3_apbuart->scaler = scaler;
122 }
123 return;
124}
125
126static struct serial_device leon3_serial_drv = {
127 .name = "leon3_serial",
128 .start = leon3_serial_init,
129 .stop = NULL,
130 .setbrg = leon3_serial_setbrg,
131 .putc = leon3_serial_putc,
132 .puts = default_serial_puts,
133 .getc = leon3_serial_getc,
134 .tstc = leon3_serial_tstc,
135};
136
137void leon3_serial_initialize(void)
138{
139 serial_register(&leon3_serial_drv);
140}
141
142__weak struct serial_device *default_serial_console(void)
143{
144 return &leon3_serial_drv;
145}
146