Initial checkin
[pandora-x-loader.git] / drivers / serial.c
1 /*
2  * (C) Copyright 2000
3  * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25
26 #ifdef CFG_PRINTF
27 #ifdef CFG_NS16550_SERIAL
28
29 #include <ns16550.h>
30 #ifdef CFG_NS87308
31 #include <ns87308.h>
32 #endif
33
34 #if CONFIG_CONS_INDEX == 1
35 static NS16550_t console = (NS16550_t) CFG_NS16550_COM1;
36 #elif CONFIG_CONS_INDEX == 2
37 static NS16550_t console = (NS16550_t) CFG_NS16550_COM2;
38 #elif CONFIG_CONS_INDEX == 3
39 static NS16550_t console = (NS16550_t) CFG_NS16550_COM3;
40 #elif CONFIG_CONS_INDEX == 4
41 static NS16550_t console = (NS16550_t) CFG_NS16550_COM4;
42 #else
43 #error no valid console defined
44 #endif
45
46 static int calc_divisor (void)
47 {
48 //      DECLARE_GLOBAL_DATA_PTR;
49 #ifdef CONFIG_OMAP1510
50         /* If can't cleanly clock 115200 set div to 1 */
51         if ((CFG_NS16550_CLK == 12000000) && (CONFIG_BAUDRATE == 115200)) {
52                 console->osc_12m_sel = OSC_12M_SEL;     /* enable 6.5 * divisor */
53                 return (1);                             /* return 1 for base divisor */
54         }
55         console->osc_12m_sel = 0;                       /* clear if previsouly set */
56 #endif
57 #if defined(CONFIG_OMAP1610) || defined(CONFIG_OMAP1710)
58         /* If can't cleanly clock 115200 set div to 1 */
59         if ((CFG_NS16550_CLK == 48000000) && (CONFIG_BAUDRATE == 115200)) {
60                 return (26);            /* return 26 for base divisor */
61         }
62 #endif
63         return (CFG_NS16550_CLK / 16 / CONFIG_BAUDRATE);
64 }
65
66 int serial_init (void)
67 {
68         int clock_divisor = calc_divisor();
69
70 #ifdef CFG_NS87308
71         initialise_ns87308();
72 #endif
73
74         NS16550_init(console, clock_divisor);
75
76         return (0);
77 }
78
79 void
80 serial_putc(const char c)
81 {
82         if (c == '\n')
83                 NS16550_putc(console, '\r');
84
85         NS16550_putc(console, c);
86 }
87
88 void
89 serial_puts (const char *s)
90 {
91         while (*s) {
92                 serial_putc (*s++);
93         }
94 }
95
96
97 int
98 serial_getc(void)
99 {
100         return NS16550_getc(console);
101 }
102
103 int
104 serial_tstc(void)
105 {
106         return NS16550_tstc(console);
107 }
108
109 void
110 serial_setbrg (void)
111 {
112         int clock_divisor;
113
114     clock_divisor = calc_divisor();
115         NS16550_reinit(console, clock_divisor);
116 }
117
118 #endif
119 #endif