blob: 3c0f4c59fb47e8c0f8bf354e0090745cb43a8356 [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 Vasut0d19c022012-09-14 22:39:44 +020033#include <serial.h>
34#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +000035
Wolfgang Denk6405a152006-03-31 18:32:53 +020036DECLARE_GLOBAL_DATA_PTR;
37
Marek Vasut0d19c022012-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
wdenkc8434db2003-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 ... */
wdenkc8434db2003-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;
wdenkc8434db2003-03-26 06:55:25 +000067 Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData );
wdenkfe8c2802002-11-03 00:38:21 +000068 Ser1UTCR1 = 0;
wdenkc8434db2003-03-26 06:55:25 +000069 Ser1UTCR2 = (u32)reg;
70 Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE );
wdenkc35ba4e2004-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 Vasut0d19c022012-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 Vasut0d19c022012-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;
wdenkc35ba4e2004-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 Vasut0d19c022012-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;
wdenkc35ba4e2004-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 Vasut0d19c022012-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;
wdenkc35ba4e2004-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 Vasut0d19c022012-09-14 22:39:44 +0200156static struct serial_device sa1100_serial_drv = {
157 .name = "sa1100_serial",
158 .start = sa1100_serial_init,
159 .stop = NULL,
160 .setbrg = sa1100_serial_setbrg,
161 .putc = sa1100_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000162 .puts = default_serial_puts,
Marek Vasut0d19c022012-09-14 22:39:44 +0200163 .getc = sa1100_serial_getc,
164 .tstc = sa1100_serial_tstc,
165};
166
167void sa1100_serial_initialize(void)
168{
169 serial_register(&sa1100_serial_drv);
170}
171
172__weak struct serial_device *default_serial_console(void)
173{
174 return &sa1100_serial_drv;
175}