]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/cogent/serial.c
18a9dbf35cd7cf15fc65f648c8975aacf6b8f4a9
[karo-tx-uboot.git] / board / cogent / serial.c
1 /*
2  * Simple serial driver for Cogent motherboard serial ports
3  * for use during boot
4  */
5
6 #include <common.h>
7 #include <board/cogent/serial.h>
8 #include <serial.h>
9 #include <linux/compiler.h>
10
11 DECLARE_GLOBAL_DATA_PTR;
12
13 #if (CMA_MB_CAPS & CMA_MB_CAP_SERPAR)
14
15 #if (defined(CONFIG_8xx) && defined(CONFIG_8xx_CONS_NONE)) || \
16      (defined(CONFIG_8260) && defined(CONFIG_CONS_NONE))
17
18 #if CONFIG_CONS_INDEX == 1
19 #define CMA_MB_SERIAL_BASE      CMA_MB_SERIALA_BASE
20 #elif CONFIG_CONS_INDEX == 2
21 #define CMA_MB_SERIAL_BASE      CMA_MB_SERIALB_BASE
22 #elif CONFIG_CONS_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2)
23 #define CMA_MB_SERIAL_BASE      CMA_MB_SER2A_BASE
24 #elif CONFIG_CONS_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2)
25 #define CMA_MB_SERIAL_BASE      CMA_MB_SER2B_BASE
26 #else
27 #error CONFIG_CONS_INDEX must be configured for Cogent motherboard serial
28 #endif
29
30 static int cogent_serial_init(void)
31 {
32         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE;
33
34         cma_mb_reg_write (&mbsp->ser_ier, 0x00);        /* turn off interrupts */
35         serial_setbrg ();
36         cma_mb_reg_write (&mbsp->ser_lcr, 0x03);        /* 8 data, 1 stop, no parity */
37         cma_mb_reg_write (&mbsp->ser_mcr, 0x03);        /* RTS/DTR */
38         cma_mb_reg_write (&mbsp->ser_fcr, 0x07);        /* Clear & enable FIFOs */
39
40         return (0);
41 }
42
43 static void cogent_serial_setbrg(void)
44 {
45         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE;
46         unsigned int divisor;
47         unsigned char lcr;
48
49         if ((divisor = br_to_div (gd->baudrate)) == 0)
50                 divisor = DEFDIV;
51
52         lcr = cma_mb_reg_read (&mbsp->ser_lcr);
53         cma_mb_reg_write (&mbsp->ser_lcr, lcr | 0x80);  /* Access baud rate(set DLAB) */
54         cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff);
55         cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff);
56         cma_mb_reg_write (&mbsp->ser_lcr, lcr); /* unset DLAB */
57 }
58
59 static void cogent_serial_putc(const char c)
60 {
61         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE;
62
63         if (c == '\n')
64                 serial_putc ('\r');
65
66         while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0);
67
68         cma_mb_reg_write (&mbsp->ser_thr, c);
69 }
70
71 static void cogent_serial_puts(const char *s)
72 {
73         while (*s != '\0')
74                 serial_putc (*s++);
75 }
76
77 static int cogent_serial_getc(void)
78 {
79         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE;
80
81         while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0);
82
83         return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f);
84 }
85
86 static int cogent_serial_tstc(void)
87 {
88         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE;
89
90         return ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) != 0);
91 }
92
93 #ifdef CONFIG_SERIAL_MULTI
94 static struct serial_device cogent_serial_drv = {
95         .name   = "cogent_serial",
96         .start  = cogent_serial_init,
97         .stop   = NULL,
98         .setbrg = cogent_serial_setbrg,
99         .putc   = cogent_serial_putc,
100         .puts   = cogent_serial_puts,
101         .getc   = cogent_serial_getc,
102         .tstc   = cogent_serial_tstc,
103 };
104
105 void cogent_serial_initialize(void)
106 {
107         serial_register(&cogent_serial_drv);
108 }
109
110 __weak struct serial_device *default_serial_console(void)
111 {
112         return &cogent_serial_drv;
113 }
114 #else
115 int serial_init(void)
116 {
117         return cogent_serial_init();
118 }
119
120 void serial_setbrg(void)
121 {
122         cogent_serial_setbrg();
123 }
124
125 void serial_putc(const char c)
126 {
127         cogent_serial_putc(c);
128 }
129
130 void serial_puts(const char *s)
131 {
132         cogent_serial_puts(s);
133 }
134
135 int serial_getc(void)
136 {
137         return cogent_serial_getc();
138 }
139
140 int serial_tstc(void)
141 {
142         return cogent_serial_tstc();
143 }
144 #endif
145 #endif /* CONS_NONE */
146
147 #if defined(CONFIG_CMD_KGDB) && \
148     defined(CONFIG_KGDB_NONE)
149
150 #if CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX
151 #error Console and kgdb are on the same serial port - this is not supported
152 #endif
153
154 #if CONFIG_KGDB_INDEX == 1
155 #define CMA_MB_KGDB_SER_BASE    CMA_MB_SERIALA_BASE
156 #elif CONFIG_KGDB_INDEX == 2
157 #define CMA_MB_KGDB_SER_BASE    CMA_MB_SERIALB_BASE
158 #elif CONFIG_KGDB_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2)
159 #define CMA_MB_KGDB_SER_BASE    CMA_MB_SER2A_BASE
160 #elif CONFIG_KGDB_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2)
161 #define CMA_MB_KGDB_SER_BASE    CMA_MB_SER2B_BASE
162 #else
163 #error CONFIG_KGDB_INDEX must be configured for Cogent motherboard serial
164 #endif
165
166 void kgdb_serial_init (void)
167 {
168         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE;
169         unsigned int divisor;
170
171         if ((divisor = br_to_div (CONFIG_KGDB_BAUDRATE)) == 0)
172                 divisor = DEFDIV;
173
174         cma_mb_reg_write (&mbsp->ser_ier, 0x00);        /* turn off interrupts */
175         cma_mb_reg_write (&mbsp->ser_lcr, 0x80);        /* Access baud rate(set DLAB) */
176         cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff);
177         cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff);
178         cma_mb_reg_write (&mbsp->ser_lcr, 0x03);        /* 8 data, 1 stop, no parity */
179         cma_mb_reg_write (&mbsp->ser_mcr, 0x03);        /* RTS/DTR */
180         cma_mb_reg_write (&mbsp->ser_fcr, 0x07);        /* Clear & enable FIFOs */
181
182         printf ("[on cma10x serial port B] ");
183 }
184
185 void putDebugChar (int c)
186 {
187         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE;
188
189         while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0);
190
191         cma_mb_reg_write (&mbsp->ser_thr, c & 0xff);
192 }
193
194 void putDebugStr (const char *str)
195 {
196         while (*str != '\0') {
197                 if (*str == '\n')
198                         putDebugChar ('\r');
199                 putDebugChar (*str++);
200         }
201 }
202
203 int getDebugChar (void)
204 {
205         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE;
206
207         while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0);
208
209         return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f);
210 }
211
212 void kgdb_interruptible (int yes)
213 {
214         cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE;
215
216         if (yes == 1) {
217                 printf ("kgdb: turning serial ints on\n");
218                 cma_mb_reg_write (&mbsp->ser_ier, 0xf);
219         } else {
220                 printf ("kgdb: turning serial ints off\n");
221                 cma_mb_reg_write (&mbsp->ser_ier, 0x0);
222         }
223 }
224
225 #endif /* KGDB && KGDB_NONE */
226
227 #endif /* CAPS & SERPAR */