blob: 0f0878a0510de1ccff4132c9ded65b355b9a4386 [file] [log] [blame]
wdenk7ac16102004-08-01 22:48:16 +00001/*
2 * (C) Copyright 2002
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +02003 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenk7ac16102004-08-01 22:48:16 +00004 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8#include <common.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +00009#include <linux/compiler.h>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090010#include <asm/arch/s3c24x0_cpu.h>
wdenkc6097192002-11-03 00:24:07 +000011
Wolfgang Denk6405a152006-03-31 18:32:53 +020012DECLARE_GLOBAL_DATA_PTR;
13
wdenk7539dea2003-06-19 23:01:32 +000014#ifdef CONFIG_SERIAL1
15#define UART_NR S3C24X0_UART0
16
wdenkc35ba4e2004-03-14 22:25:36 +000017#elif defined(CONFIG_SERIAL2)
wdenk7539dea2003-06-19 23:01:32 +000018#define UART_NR S3C24X0_UART1
19
wdenkc35ba4e2004-03-14 22:25:36 +000020#elif defined(CONFIG_SERIAL3)
wdenk7539dea2003-06-19 23:01:32 +000021#define UART_NR S3C24X0_UART2
22
23#else
24#error "Bad: you didn't configure serial ..."
25#endif
wdenkc6097192002-11-03 00:24:07 +000026
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090027#include <asm/io.h>
Harald Welteda412622007-12-19 14:24:40 +010028#include <serial.h>
29
30/* Multi serial device functions */
31#define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090032 int s3serial##port##_init(void) \
33 { \
34 return serial_init_dev(port); \
35 } \
36 void s3serial##port##_setbrg(void) \
37 { \
38 serial_setbrg_dev(port); \
39 } \
40 int s3serial##port##_getc(void) \
41 { \
42 return serial_getc_dev(port); \
43 } \
44 int s3serial##port##_tstc(void) \
45 { \
46 return serial_tstc_dev(port); \
47 } \
48 void s3serial##port##_putc(const char c) \
49 { \
50 serial_putc_dev(port, c); \
51 } \
52 void s3serial##port##_puts(const char *s) \
53 { \
54 serial_puts_dev(port, s); \
55 }
Harald Welteda412622007-12-19 14:24:40 +010056
Marek Vasut5bcdf242012-09-09 18:48:28 +020057#define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
58 .name = __name, \
59 .start = s3serial##port##_init, \
60 .stop = NULL, \
61 .setbrg = s3serial##port##_setbrg, \
62 .getc = s3serial##port##_getc, \
63 .tstc = s3serial##port##_tstc, \
64 .putc = s3serial##port##_putc, \
65 .puts = s3serial##port##_puts, \
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090066}
Harald Welteda412622007-12-19 14:24:40 +010067
Masahiro Yamada4d5b9912014-10-23 22:26:05 +090068static void _serial_setbrg(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +000069{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090070 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +000071 unsigned int reg = 0;
Harald Welteda412622007-12-19 14:24:40 +010072 int i;
wdenkc6097192002-11-03 00:24:07 +000073
74 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
75 reg = get_PCLK() / (16 * gd->baudrate) - 1;
76
C Nauman383c43e2010-10-26 23:04:31 +090077 writel(reg, &uart->ubrdiv);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090078 for (i = 0; i < 100; i++)
79 /* Delay */ ;
Harald Welteda412622007-12-19 14:24:40 +010080}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090081
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090082static inline void serial_setbrg_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +010083{
84 _serial_setbrg(dev_index);
85}
Harald Welteda412622007-12-19 14:24:40 +010086
87/* Initialise the serial port. The settings are always 8 data bits, no parity,
88 * 1 stop bit, no start bits.
89 */
90static int serial_init_dev(const int dev_index)
91{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090092 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
93
wdenkc6097192002-11-03 00:24:07 +000094 /* FIFO enable, Tx/Rx FIFO clear */
C Nauman383c43e2010-10-26 23:04:31 +090095 writel(0x07, &uart->ufcon);
96 writel(0x0, &uart->umcon);
Harald Welteda412622007-12-19 14:24:40 +010097
wdenkc6097192002-11-03 00:24:07 +000098 /* Normal,No parity,1 stop,8 bit */
C Nauman383c43e2010-10-26 23:04:31 +090099 writel(0x3, &uart->ulcon);
wdenkc6097192002-11-03 00:24:07 +0000100 /*
101 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
102 * normal,interrupt or polling
103 */
C Nauman383c43e2010-10-26 23:04:31 +0900104 writel(0x245, &uart->ucon);
wdenkc6097192002-11-03 00:24:07 +0000105
Harald Welteda412622007-12-19 14:24:40 +0100106 _serial_setbrg(dev_index);
107
108 return (0);
wdenkc6097192002-11-03 00:24:07 +0000109}
wdenkc6097192002-11-03 00:24:07 +0000110
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 */
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900116static int _serial_getc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000117{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900118 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk57b2d802003-06-27 21:31:46 +0000119
C Nauman383c43e2010-10-26 23:04:31 +0900120 while (!(readl(&uart->utrstat) & 0x1))
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900121 /* wait for character to arrive */ ;
wdenkc6097192002-11-03 00:24:07 +0000122
C Nauman383c43e2010-10-26 23:04:31 +0900123 return readb(&uart->urxh) & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000124}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900125
Harald Welteda412622007-12-19 14:24:40 +0100126static inline int serial_getc_dev(unsigned int dev_index)
127{
128 return _serial_getc(dev_index);
129}
wdenkc6097192002-11-03 00:24:07 +0000130
wdenkc6097192002-11-03 00:24:07 +0000131/*
132 * Output a single byte to the serial port.
133 */
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900134static void _serial_putc(const char c, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000135{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900136 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000137
Alison Wang23e06b52016-03-02 11:00:37 +0800138 /* If \n, also do \r */
139 if (c == '\n')
140 serial_putc('\r');
141
C Nauman383c43e2010-10-26 23:04:31 +0900142 while (!(readl(&uart->utrstat) & 0x2))
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900143 /* wait for room in the tx FIFO */ ;
wdenkc6097192002-11-03 00:24:07 +0000144
C Nauman383c43e2010-10-26 23:04:31 +0900145 writeb(c, &uart->utxh);
wdenkc6097192002-11-03 00:24:07 +0000146}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900147
Harald Welteda412622007-12-19 14:24:40 +0100148static inline void serial_putc_dev(unsigned int dev_index, const char c)
149{
150 _serial_putc(c, dev_index);
151}
wdenkc6097192002-11-03 00:24:07 +0000152
153/*
154 * Test whether a character is in the RX buffer
155 */
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900156static int _serial_tstc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000157{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900158 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk7539dea2003-06-19 23:01:32 +0000159
C Nauman383c43e2010-10-26 23:04:31 +0900160 return readl(&uart->utrstat) & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000161}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900162
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900163static inline int serial_tstc_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +0100164{
165 return _serial_tstc(dev_index);
166}
wdenkc6097192002-11-03 00:24:07 +0000167
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900168static void _serial_puts(const char *s, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000169{
170 while (*s) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900171 _serial_putc(*s++, dev_index);
wdenkc6097192002-11-03 00:24:07 +0000172 }
173}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900174
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900175static inline void serial_puts_dev(int dev_index, const char *s)
Harald Welteda412622007-12-19 14:24:40 +0100176{
177 _serial_puts(s, dev_index);
178}
Harald Welteda412622007-12-19 14:24:40 +0100179
Harald Welteda412622007-12-19 14:24:40 +0100180DECLARE_S3C_SERIAL_FUNCTIONS(0);
181struct serial_device s3c24xx_serial0_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000182INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
Harald Welteda412622007-12-19 14:24:40 +0100183DECLARE_S3C_SERIAL_FUNCTIONS(1);
184struct serial_device s3c24xx_serial1_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000185INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
Harald Welteda412622007-12-19 14:24:40 +0100186DECLARE_S3C_SERIAL_FUNCTIONS(2);
187struct serial_device s3c24xx_serial2_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000188INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
Mike Frysingerf96c0422011-04-29 18:03:29 +0000189
190__weak struct serial_device *default_serial_console(void)
191{
192#if defined(CONFIG_SERIAL1)
193 return &s3c24xx_serial0_device;
194#elif defined(CONFIG_SERIAL2)
195 return &s3c24xx_serial1_device;
196#elif defined(CONFIG_SERIAL3)
197 return &s3c24xx_serial2_device;
198#else
199#error "CONFIG_SERIAL? missing."
200#endif
201}
Marek Vasutb3e48aa2012-09-12 16:01:16 +0200202
203void s3c24xx_serial_initialize(void)
204{
205 serial_register(&s3c24xx_serial0_device);
206 serial_register(&s3c24xx_serial1_device);
207 serial_register(&s3c24xx_serial2_device);
208}