blob: 86e682d44d4aa788a081275895b43b5b2d5b0b1e [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <common.h>
32#include <SA-1100.h>
Marek Vasuta30a4222012-09-14 22:39:44 +020033#include <serial.h>
34#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +000035
Wolfgang Denkd87080b2006-03-31 18:32:53 +020036DECLARE_GLOBAL_DATA_PTR;
37
Marek Vasuta30a4222012-09-14 22:39:44 +020038static void sa1100_serial_setbrg(void)
wdenkfe8c2802002-11-03 00:38:21 +000039{
wdenkfe8c2802002-11-03 00:38:21 +000040 unsigned int reg = 0;
41
42 if (gd->baudrate == 1200)
43 reg = 191;
44 else if (gd->baudrate == 9600)
45 reg = 23;
46 else if (gd->baudrate == 19200)
47 reg = 11;
48 else if (gd->baudrate == 38400)
49 reg = 5;
50 else if (gd->baudrate == 57600)
51 reg = 3;
52 else if (gd->baudrate == 115200)
53 reg = 1;
54 else
55 hang ();
56
57#ifdef CONFIG_SERIAL1
wdenkdc7c9a12003-03-26 06:55:25 +000058 /* SA1110 uart function */
59 Ser1SDCR0 |= SDCR0_SUS;
60
wdenkfe8c2802002-11-03 00:38:21 +000061 /* Wait until port is ready ... */
wdenkdc7c9a12003-03-26 06:55:25 +000062 while(Ser1UTSR1 & UTSR1_TBY) {}
wdenkfe8c2802002-11-03 00:38:21 +000063
64 /* init serial serial 1 */
65 Ser1UTCR3 = 0x00;
66 Ser1UTSR0 = 0xff;
wdenkdc7c9a12003-03-26 06:55:25 +000067 Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData );
wdenkfe8c2802002-11-03 00:38:21 +000068 Ser1UTCR1 = 0;
wdenkdc7c9a12003-03-26 06:55:25 +000069 Ser1UTCR2 = (u32)reg;
70 Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE );
wdenk42dfe7a2004-03-14 22:25:36 +000071#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +000072 /* Wait until port is ready ... */
73 while (Ser3UTSR1 & UTSR1_TBY) {
74 }
75
76 /* init serial serial 3 */
77 Ser3UTCR3 = 0x00;
78 Ser3UTSR0 = 0xff;
79 Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData);
80 Ser3UTCR1 = 0;
81 Ser3UTCR2 = (u32) reg;
82 Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE);
83#else
84#error "Bad: you didn't configured serial ..."
85#endif
86}
87
88
89/*
90 * Initialise the serial port with the given baudrate. The settings
91 * are always 8 data bits, no parity, 1 stop bit, no start bits.
92 *
93 */
Marek Vasuta30a4222012-09-14 22:39:44 +020094static int sa1100_serial_init(void)
wdenkfe8c2802002-11-03 00:38:21 +000095{
96 serial_setbrg ();
97
98 return (0);
99}
100
101
102/*
103 * Output a single byte to the serial port.
104 */
Marek Vasuta30a4222012-09-14 22:39:44 +0200105static void sa1100_serial_putc(const char c)
wdenkfe8c2802002-11-03 00:38:21 +0000106{
107#ifdef CONFIG_SERIAL1
108 /* wait for room in the tx FIFO on SERIAL1 */
109 while ((Ser1UTSR0 & UTSR0_TFS) == 0);
110
111 Ser1UTDR = c;
wdenk42dfe7a2004-03-14 22:25:36 +0000112#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000113 /* wait for room in the tx FIFO on SERIAL3 */
114 while ((Ser3UTSR0 & UTSR0_TFS) == 0);
115
116 Ser3UTDR = c;
117#endif
118
119 /* If \n, also do \r */
120 if (c == '\n')
121 serial_putc ('\r');
122}
123
124/*
125 * Read a single byte from the serial port. Returns 1 on success, 0
126 * otherwise. When the function is succesfull, the character read is
127 * written into its argument c.
128 */
Marek Vasuta30a4222012-09-14 22:39:44 +0200129static int sa1100_serial_tstc(void)
wdenkfe8c2802002-11-03 00:38:21 +0000130{
131#ifdef CONFIG_SERIAL1
132 return Ser1UTSR1 & UTSR1_RNE;
wdenk42dfe7a2004-03-14 22:25:36 +0000133#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000134 return Ser3UTSR1 & UTSR1_RNE;
135#endif
136}
137
138/*
139 * Read a single byte from the serial port. Returns 1 on success, 0
140 * otherwise. When the function is succesfull, the character read is
141 * written into its argument c.
142 */
Marek Vasuta30a4222012-09-14 22:39:44 +0200143static int sa1100_serial_getc(void)
wdenkfe8c2802002-11-03 00:38:21 +0000144{
145#ifdef CONFIG_SERIAL1
146 while (!(Ser1UTSR1 & UTSR1_RNE));
147
148 return (char) Ser1UTDR & 0xff;
wdenk42dfe7a2004-03-14 22:25:36 +0000149#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000150 while (!(Ser3UTSR1 & UTSR1_RNE));
151
152 return (char) Ser3UTDR & 0xff;
153#endif
154}
155
Marek Vasuta30a4222012-09-14 22:39:44 +0200156static void sa1100_serial_puts(const char *s)
wdenkfe8c2802002-11-03 00:38:21 +0000157{
158 while (*s) {
159 serial_putc (*s++);
160 }
161}
Marek Vasuta30a4222012-09-14 22:39:44 +0200162
163#ifdef CONFIG_SERIAL_MULTI
164static struct serial_device sa1100_serial_drv = {
165 .name = "sa1100_serial",
166 .start = sa1100_serial_init,
167 .stop = NULL,
168 .setbrg = sa1100_serial_setbrg,
169 .putc = sa1100_serial_putc,
170 .puts = sa1100_serial_puts,
171 .getc = sa1100_serial_getc,
172 .tstc = sa1100_serial_tstc,
173};
174
175void sa1100_serial_initialize(void)
176{
177 serial_register(&sa1100_serial_drv);
178}
179
180__weak struct serial_device *default_serial_console(void)
181{
182 return &sa1100_serial_drv;
183}
184#else
185int serial_init(void)
186{
187 return sa1100_serial_init();
188}
189
190void serial_setbrg(void)
191{
192 sa1100_serial_setbrg();
193}
194
195void serial_putc(const char c)
196{
197 sa1100_serial_putc(c);
198}
199
200void serial_puts(const char *s)
201{
202 sa1100_serial_puts(s);
203}
204
205int serial_getc(void)
206{
207 return sa1100_serial_getc();
208}
209
210int serial_tstc(void)
211{
212 return sa1100_serial_tstc();
213}
214#endif