blob: b9d0f5bfa5208e3382f3cf6dc2abbd4102caf008 [file] [log] [blame]
wdenkbd1575f2003-10-14 19:43:55 +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+
wdenkbd1575f2003-10-14 19:43:55 +000016 */
17
18#include <common.h>
19#include <asm/arch/ixp425.h>
Michael Schwingen8d15c692011-05-22 23:59:59 +020020#include <watchdog.h>
Marek Vasutdde37342012-09-14 22:35:14 +020021#include <serial.h>
22#include <linux/compiler.h>
wdenkbd1575f2003-10-14 19:43:55 +000023
Jean-Christophe PLAGNIOL-VILLARD69fb9fa2007-12-08 16:34:08 +010024/*
25 * 14.7456 MHz
26 * Baud Rate = --------------
27 * 16 x Divisor
28 */
Wolfgang Denk635a2c72008-01-09 13:43:38 +010029#define SERIAL_CLOCK 921600
Jean-Christophe PLAGNIOL-VILLARD69fb9fa2007-12-08 16:34:08 +010030
Wolfgang Denk6405a152006-03-31 18:32:53 +020031DECLARE_GLOBAL_DATA_PTR;
32
Marek Vasutdde37342012-09-14 22:35:14 +020033static void ixp_serial_setbrg(void)
wdenkbd1575f2003-10-14 19:43:55 +000034{
wdenkbd1575f2003-10-14 19:43:55 +000035 unsigned int quot = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036 int uart = CONFIG_SYS_IXP425_CONSOLE;
Wolfgang Denk635a2c72008-01-09 13:43:38 +010037
Jean-Christophe PLAGNIOL-VILLARD69fb9fa2007-12-08 16:34:08 +010038 if ((gd->baudrate <= SERIAL_CLOCK) && (SERIAL_CLOCK % gd->baudrate == 0))
39 quot = SERIAL_CLOCK / gd->baudrate;
wdenkbd1575f2003-10-14 19:43:55 +000040 else
41 hang ();
42
43 IER(uart) = 0; /* Disable for now */
44 FCR(uart) = 0; /* No fifos enabled */
45
46 /* set baud rate */
47 LCR(uart) = LCR_WLS0 | LCR_WLS1 | LCR_DLAB;
48 DLL(uart) = quot & 0xff;
49 DLH(uart) = quot >> 8;
50 LCR(uart) = LCR_WLS0 | LCR_WLS1;
Michael Schwingenfbe18f92008-01-10 14:59:46 +010051#ifdef CONFIG_SERIAL_RTS_ACTIVE
52 MCR(uart) = MCR_RTS; /* set RTS active */
53#else
54 MCR(uart) = 0; /* set RTS inactive */
55#endif
wdenkbd1575f2003-10-14 19:43:55 +000056 IER(uart) = IER_UUE;
57}
58
wdenkbd1575f2003-10-14 19:43:55 +000059/*
60 * Initialise the serial port with the given baudrate. The settings
61 * are always 8 data bits, no parity, 1 stop bit, no start bits.
62 *
63 */
Marek Vasutdde37342012-09-14 22:35:14 +020064static int ixp_serial_init(void)
wdenkbd1575f2003-10-14 19:43:55 +000065{
66 serial_setbrg ();
67
68 return (0);
69}
70
71
72/*
73 * Output a single byte to the serial port.
74 */
Marek Vasutdde37342012-09-14 22:35:14 +020075static void ixp_serial_putc(const char c)
wdenkbd1575f2003-10-14 19:43:55 +000076{
77 /* wait for room in the tx FIFO on UART */
Michael Schwingen8d15c692011-05-22 23:59:59 +020078 while ((LSR(CONFIG_SYS_IXP425_CONSOLE) & LSR_TEMT) == 0)
79 WATCHDOG_RESET(); /* Reset HW Watchdog, if needed */
wdenkbd1575f2003-10-14 19:43:55 +000080
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081 THR(CONFIG_SYS_IXP425_CONSOLE) = c;
wdenkbd1575f2003-10-14 19:43:55 +000082
83 /* If \n, also do \r */
84 if (c == '\n')
85 serial_putc ('\r');
86}
87
88/*
89 * Read a single byte from the serial port. Returns 1 on success, 0
90 * otherwise. When the function is succesfull, the character read is
91 * written into its argument c.
92 */
Marek Vasutdde37342012-09-14 22:35:14 +020093static int ixp_serial_tstc(void)
wdenkbd1575f2003-10-14 19:43:55 +000094{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095 return LSR(CONFIG_SYS_IXP425_CONSOLE) & LSR_DR;
wdenkbd1575f2003-10-14 19:43:55 +000096}
97
98/*
99 * Read a single byte from the serial port. Returns 1 on success, 0
100 * otherwise. When the function is succesfull, the character read is
101 * written into its argument c.
102 */
Marek Vasutdde37342012-09-14 22:35:14 +0200103static int ixp_serial_getc(void)
wdenkbd1575f2003-10-14 19:43:55 +0000104{
Michael Schwingen8d15c692011-05-22 23:59:59 +0200105 while (!(LSR(CONFIG_SYS_IXP425_CONSOLE) & LSR_DR))
106 WATCHDOG_RESET(); /* Reset HW Watchdog, if needed */
wdenkbd1575f2003-10-14 19:43:55 +0000107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108 return (char) RBR(CONFIG_SYS_IXP425_CONSOLE) & 0xff;
wdenkbd1575f2003-10-14 19:43:55 +0000109}
110
Marek Vasutdde37342012-09-14 22:35:14 +0200111static struct serial_device ixp_serial_drv = {
112 .name = "ixp_serial",
113 .start = ixp_serial_init,
114 .stop = NULL,
115 .setbrg = ixp_serial_setbrg,
116 .putc = ixp_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000117 .puts = default_serial_puts,
Marek Vasutdde37342012-09-14 22:35:14 +0200118 .getc = ixp_serial_getc,
119 .tstc = ixp_serial_tstc,
120};
121
122void ixp_serial_initialize(void)
123{
124 serial_register(&ixp_serial_drv);
125}
126
127__weak struct serial_device *default_serial_console(void)
128{
129 return &ixp_serial_drv;
130}