blob: 78f241d8508f38d80a82f1aa9f63065a449de51b [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020015 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +000016 */
17
18#include <common.h>
19#include <SA-1100.h>
Marek Vasut0d19c022012-09-14 22:39:44 +020020#include <serial.h>
21#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +000022
Wolfgang Denk6405a152006-03-31 18:32:53 +020023DECLARE_GLOBAL_DATA_PTR;
24
Marek Vasut0d19c022012-09-14 22:39:44 +020025static void sa1100_serial_setbrg(void)
wdenkfe8c2802002-11-03 00:38:21 +000026{
wdenkfe8c2802002-11-03 00:38:21 +000027 unsigned int reg = 0;
28
29 if (gd->baudrate == 1200)
30 reg = 191;
31 else if (gd->baudrate == 9600)
32 reg = 23;
33 else if (gd->baudrate == 19200)
34 reg = 11;
35 else if (gd->baudrate == 38400)
36 reg = 5;
37 else if (gd->baudrate == 57600)
38 reg = 3;
39 else if (gd->baudrate == 115200)
40 reg = 1;
41 else
42 hang ();
43
44#ifdef CONFIG_SERIAL1
wdenkc8434db2003-03-26 06:55:25 +000045 /* SA1110 uart function */
46 Ser1SDCR0 |= SDCR0_SUS;
47
wdenkfe8c2802002-11-03 00:38:21 +000048 /* Wait until port is ready ... */
wdenkc8434db2003-03-26 06:55:25 +000049 while(Ser1UTSR1 & UTSR1_TBY) {}
wdenkfe8c2802002-11-03 00:38:21 +000050
51 /* init serial serial 1 */
52 Ser1UTCR3 = 0x00;
53 Ser1UTSR0 = 0xff;
wdenkc8434db2003-03-26 06:55:25 +000054 Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData );
wdenkfe8c2802002-11-03 00:38:21 +000055 Ser1UTCR1 = 0;
wdenkc8434db2003-03-26 06:55:25 +000056 Ser1UTCR2 = (u32)reg;
57 Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE );
wdenkc35ba4e2004-03-14 22:25:36 +000058#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +000059 /* Wait until port is ready ... */
60 while (Ser3UTSR1 & UTSR1_TBY) {
61 }
62
63 /* init serial serial 3 */
64 Ser3UTCR3 = 0x00;
65 Ser3UTSR0 = 0xff;
66 Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData);
67 Ser3UTCR1 = 0;
68 Ser3UTCR2 = (u32) reg;
69 Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE);
70#else
71#error "Bad: you didn't configured serial ..."
72#endif
73}
74
75
76/*
77 * Initialise the serial port with the given baudrate. The settings
78 * are always 8 data bits, no parity, 1 stop bit, no start bits.
79 *
80 */
Marek Vasut0d19c022012-09-14 22:39:44 +020081static int sa1100_serial_init(void)
wdenkfe8c2802002-11-03 00:38:21 +000082{
83 serial_setbrg ();
84
85 return (0);
86}
87
88
89/*
90 * Output a single byte to the serial port.
91 */
Marek Vasut0d19c022012-09-14 22:39:44 +020092static void sa1100_serial_putc(const char c)
wdenkfe8c2802002-11-03 00:38:21 +000093{
94#ifdef CONFIG_SERIAL1
95 /* wait for room in the tx FIFO on SERIAL1 */
96 while ((Ser1UTSR0 & UTSR0_TFS) == 0);
97
98 Ser1UTDR = c;
wdenkc35ba4e2004-03-14 22:25:36 +000099#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000100 /* wait for room in the tx FIFO on SERIAL3 */
101 while ((Ser3UTSR0 & UTSR0_TFS) == 0);
102
103 Ser3UTDR = c;
104#endif
105
106 /* If \n, also do \r */
107 if (c == '\n')
108 serial_putc ('\r');
109}
110
111/*
112 * Read a single byte from the serial port. Returns 1 on success, 0
113 * otherwise. When the function is succesfull, the character read is
114 * written into its argument c.
115 */
Marek Vasut0d19c022012-09-14 22:39:44 +0200116static int sa1100_serial_tstc(void)
wdenkfe8c2802002-11-03 00:38:21 +0000117{
118#ifdef CONFIG_SERIAL1
119 return Ser1UTSR1 & UTSR1_RNE;
wdenkc35ba4e2004-03-14 22:25:36 +0000120#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000121 return Ser3UTSR1 & UTSR1_RNE;
122#endif
123}
124
125/*
126 * Read a single byte from the serial port. Returns 1 on success, 0
127 * otherwise. When the function is succesfull, the character read is
128 * written into its argument c.
129 */
Marek Vasut0d19c022012-09-14 22:39:44 +0200130static int sa1100_serial_getc(void)
wdenkfe8c2802002-11-03 00:38:21 +0000131{
132#ifdef CONFIG_SERIAL1
133 while (!(Ser1UTSR1 & UTSR1_RNE));
134
135 return (char) Ser1UTDR & 0xff;
wdenkc35ba4e2004-03-14 22:25:36 +0000136#elif defined(CONFIG_SERIAL3)
wdenkfe8c2802002-11-03 00:38:21 +0000137 while (!(Ser3UTSR1 & UTSR1_RNE));
138
139 return (char) Ser3UTDR & 0xff;
140#endif
141}
142
Marek Vasut0d19c022012-09-14 22:39:44 +0200143static struct serial_device sa1100_serial_drv = {
144 .name = "sa1100_serial",
145 .start = sa1100_serial_init,
146 .stop = NULL,
147 .setbrg = sa1100_serial_setbrg,
148 .putc = sa1100_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000149 .puts = default_serial_puts,
Marek Vasut0d19c022012-09-14 22:39:44 +0200150 .getc = sa1100_serial_getc,
151 .tstc = sa1100_serial_tstc,
152};
153
154void sa1100_serial_initialize(void)
155{
156 serial_register(&sa1100_serial_drv);
157}
158
159__weak struct serial_device *default_serial_console(void)
160{
161 return &sa1100_serial_drv;
162}