serial/serial_arc: set registers address during compilation
[people/ms/u-boot.git] / drivers / serial / serial_arc.c
CommitLineData
22a240c3
AB
1/*
2 * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
10#include <common.h>
11#include <serial.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15struct arc_serial_regs {
16 unsigned int id0;
17 unsigned int id1;
18 unsigned int id2;
19 unsigned int id3;
20 unsigned int data;
21 unsigned int status;
22 unsigned int baudl;
23 unsigned int baudh;
24};
25
26/* Bit definitions of STATUS register */
27#define UART_RXEMPTY (1 << 5)
28#define UART_OVERFLOW_ERR (1 << 1)
29#define UART_TXEMPTY (1 << 7)
30
36d68668 31struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_ARC_UART_BASE;
22a240c3
AB
32
33static void arc_serial_setbrg(void)
34{
35 int arc_console_baud;
36
37 if (!gd->baudrate)
38 gd->baudrate = CONFIG_BAUDRATE;
39
40 arc_console_baud = gd->cpu_clk / (gd->baudrate * 4) - 1;
94b5400e 41 writeb(arc_console_baud & 0xff, &regs->baudl);
1d568c76
AB
42
43#ifdef CONFIG_ARC
44 /*
45 * UART ISS(Instruction Set simulator) emulation has a subtle bug:
46 * A existing value of Baudh = 0 is used as a indication to startup
47 * it's internal state machine.
48 * Thus if baudh is set to 0, 2 times, it chokes.
49 * This happens with BAUD=115200 and the formaula above
50 * Until that is fixed, when running on ISS, we will set baudh to !0
51 */
52 if (gd->arch.running_on_hw)
94b5400e 53 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
1d568c76 54 else
94b5400e 55 writeb(1, &regs->baudh);
1d568c76 56#else
94b5400e 57 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
1d568c76 58#endif
22a240c3
AB
59}
60
61static int arc_serial_init(void)
62{
22a240c3
AB
63 serial_setbrg();
64 return 0;
65}
66
67static void arc_serial_putc(const char c)
68{
69 if (c == '\n')
70 arc_serial_putc('\r');
71
94b5400e 72 while (!(readb(&regs->status) & UART_TXEMPTY))
22a240c3
AB
73 ;
74
94b5400e 75 writeb(c, &regs->data);
22a240c3
AB
76}
77
78static int arc_serial_tstc(void)
79{
94b5400e 80 return !(readb(&regs->status) & UART_RXEMPTY);
22a240c3
AB
81}
82
83static int arc_serial_getc(void)
84{
85 while (!arc_serial_tstc())
86 ;
87
88 /* Check for overflow errors */
94b5400e 89 if (readb(&regs->status) & UART_OVERFLOW_ERR)
22a240c3
AB
90 return 0;
91
94b5400e 92 return readb(&regs->data) & 0xFF;
22a240c3
AB
93}
94
22a240c3
AB
95static struct serial_device arc_serial_drv = {
96 .name = "arc_serial",
97 .start = arc_serial_init,
98 .stop = NULL,
99 .setbrg = arc_serial_setbrg,
100 .putc = arc_serial_putc,
1cb8393a 101 .puts = default_serial_puts,
22a240c3
AB
102 .getc = arc_serial_getc,
103 .tstc = arc_serial_tstc,
104};
105
106void arc_serial_initialize(void)
107{
108 serial_register(&arc_serial_drv);
109}
110
111__weak struct serial_device *default_serial_console(void)
112{
113 return &arc_serial_drv;
114}