blob: cb5b0bfe9b0d3eeb18e69ad3ae7627b145ddfe12 [file] [log] [blame]
wdenk21136db2003-07-16 21:53:01 +00001/*
2 * (C) Copyright 2000 - 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk21136db2003-07-16 21:53:01 +00006 *
7 * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with
Stefan Roese88fbf932010-04-15 16:07:28 +02008 * changes based on the file arch/powerpc/mbxboot/m8260_tty.c from the
wdenk21136db2003-07-16 21:53:01 +00009 * Linux/PPC sources (m8260_tty.c had no copyright info in it).
Wolfgang Denk16c3a452006-06-16 17:04:45 +020010 *
11 * Martin Krause, 8 Jun 2006
Marek Vasutf35317c2012-09-14 23:44:09 +020012 * Added SERIAL_MULTI support
wdenk21136db2003-07-16 21:53:01 +000013 */
14
15/*
16 * Minimal serial functions needed to use one of the PSC ports
17 * as serial console interface.
18 */
19
20#include <common.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +000021#include <linux/compiler.h>
wdenk21136db2003-07-16 21:53:01 +000022#include <mpc5xxx.h>
Wolfgang Denk16c3a452006-06-16 17:04:45 +020023#include <serial.h>
Wolfgang Denk16c3a452006-06-16 17:04:45 +020024
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
wdenk21136db2003-07-16 21:53:01 +000027#if defined(CONFIG_PSC_CONSOLE)
28
29#if CONFIG_PSC_CONSOLE == 1
30#define PSC_BASE MPC5XXX_PSC1
31#elif CONFIG_PSC_CONSOLE == 2
32#define PSC_BASE MPC5XXX_PSC2
33#elif CONFIG_PSC_CONSOLE == 3
34#define PSC_BASE MPC5XXX_PSC3
wdenk21136db2003-07-16 21:53:01 +000035#elif CONFIG_PSC_CONSOLE == 4
36#define PSC_BASE MPC5XXX_PSC4
37#elif CONFIG_PSC_CONSOLE == 5
38#define PSC_BASE MPC5XXX_PSC5
39#elif CONFIG_PSC_CONSOLE == 6
40#define PSC_BASE MPC5XXX_PSC6
41#else
42#error CONFIG_PSC_CONSOLE must be in 1 ... 6
43#endif
44
Marek Vasutf35317c2012-09-14 23:44:09 +020045#if defined(CONFIG_PSC_CONSOLE2)
Wolfgang Denk16c3a452006-06-16 17:04:45 +020046
Wolfgang Denk16c3a452006-06-16 17:04:45 +020047#if CONFIG_PSC_CONSOLE2 == 1
48#define PSC_BASE2 MPC5XXX_PSC1
49#elif CONFIG_PSC_CONSOLE2 == 2
50#define PSC_BASE2 MPC5XXX_PSC2
51#elif CONFIG_PSC_CONSOLE2 == 3
52#define PSC_BASE2 MPC5XXX_PSC3
Wolfgang Denk16c3a452006-06-16 17:04:45 +020053#elif CONFIG_PSC_CONSOLE2 == 4
54#define PSC_BASE2 MPC5XXX_PSC4
55#elif CONFIG_PSC_CONSOLE2 == 5
56#define PSC_BASE2 MPC5XXX_PSC5
57#elif CONFIG_PSC_CONSOLE2 == 6
58#define PSC_BASE2 MPC5XXX_PSC6
59#else
60#error CONFIG_PSC_CONSOLE2 must be in 1 ... 6
61#endif
Wolfgang Denk16c3a452006-06-16 17:04:45 +020062
Wolfgang Denk16c3a452006-06-16 17:04:45 +020063#endif
Marek Vasutf35317c2012-09-14 23:44:09 +020064
65int serial_init_dev (unsigned long dev_base)
wdenk21136db2003-07-16 21:53:01 +000066{
Wolfgang Denk16c3a452006-06-16 17:04:45 +020067 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
wdenk21136db2003-07-16 21:53:01 +000068 unsigned long baseclk;
69 int div;
70
71 /* reset PSC */
72 psc->command = PSC_SEL_MODE_REG_1;
73
74 /* select clock sources */
wdenk21136db2003-07-16 21:53:01 +000075 psc->psc_clock_select = 0;
Simon Glass4f8c5f02012-12-13 20:48:53 +000076 baseclk = (gd->arch.ipb_clk + 16) / 32;
wdenk21136db2003-07-16 21:53:01 +000077
78 /* switch to UART mode */
79 psc->sicr = 0;
80
81 /* configure parity, bit length and so on */
wdenk21136db2003-07-16 21:53:01 +000082 psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
wdenk21136db2003-07-16 21:53:01 +000083 psc->mode = PSC_MODE_ONE_STOP;
84
85 /* set up UART divisor */
wdenkca262162004-02-09 20:51:26 +000086 div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
87 psc->ctur = (div >> 8) & 0xff;
wdenk21136db2003-07-16 21:53:01 +000088 psc->ctlr = div & 0xff;
89
90 /* disable all interrupts */
91 psc->psc_imr = 0;
92
93 /* reset and enable Rx/Tx */
94 psc->command = PSC_RST_RX;
95 psc->command = PSC_RST_TX;
96 psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
97
98 return (0);
99}
100
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200101void serial_putc_dev (unsigned long dev_base, const char c)
wdenk21136db2003-07-16 21:53:01 +0000102{
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200103 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
wdenk21136db2003-07-16 21:53:01 +0000104
105 if (c == '\n')
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200106 serial_putc_dev (dev_base, '\r');
wdenk21136db2003-07-16 21:53:01 +0000107
108 /* Wait for last character to go. */
109 while (!(psc->psc_status & PSC_SR_TXEMP))
110 ;
111
112 psc->psc_buffer_8 = c;
113}
114
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200115void serial_putc_raw_dev(unsigned long dev_base, const char c)
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200116{
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200117 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200118 /* Wait for last character to go. */
119 while (!(psc->psc_status & PSC_SR_TXEMP))
120 ;
121
122 psc->psc_buffer_8 = c;
123}
124
125
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200126void serial_puts_dev (unsigned long dev_base, const char *s)
wdenk21136db2003-07-16 21:53:01 +0000127{
128 while (*s) {
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200129 serial_putc_dev (dev_base, *s++);
wdenk21136db2003-07-16 21:53:01 +0000130 }
131}
132
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200133int serial_getc_dev (unsigned long dev_base)
wdenk21136db2003-07-16 21:53:01 +0000134{
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200135 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
wdenk21136db2003-07-16 21:53:01 +0000136
137 /* Wait for a character to arrive. */
138 while (!(psc->psc_status & PSC_SR_RXRDY))
139 ;
140
141 return psc->psc_buffer_8;
142}
143
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200144int serial_tstc_dev (unsigned long dev_base)
wdenk21136db2003-07-16 21:53:01 +0000145{
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200146 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
wdenk21136db2003-07-16 21:53:01 +0000147
148 return (psc->psc_status & PSC_SR_RXRDY);
149}
150
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200151void serial_setbrg_dev (unsigned long dev_base)
wdenk21136db2003-07-16 21:53:01 +0000152{
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200153 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
wdenk21136db2003-07-16 21:53:01 +0000154 unsigned long baseclk, div;
155
Simon Glass4f8c5f02012-12-13 20:48:53 +0000156 baseclk = (gd->arch.ipb_clk + 16) / 32;
wdenk21136db2003-07-16 21:53:01 +0000157
158 /* set up UART divisor */
wdenka2b932d2005-06-27 13:30:03 +0000159 div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
Wolfgang Denk42a14192005-09-25 01:09:58 +0200160 psc->ctur = (div >> 8) & 0xFF;
161 psc->ctlr = div & 0xff;
wdenk21136db2003-07-16 21:53:01 +0000162}
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200163
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200164void serial_setrts_dev (unsigned long dev_base, int s)
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200165{
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200166 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200167
168 if (s) {
169 /* Assert RTS (become LOW) */
170 psc->op1 = 0x1;
171 }
172 else {
173 /* Negate RTS (become HIGH) */
174 psc->op0 = 0x1;
175 }
176}
177
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200178int serial_getcts_dev (unsigned long dev_base)
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200179{
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200180 volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
Wolfgang Denkd4321d32006-08-30 23:09:00 +0200181
182 return (psc->ip & 0x1) ? 0 : 1;
183}
184
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200185int serial0_init(void)
186{
187 return (serial_init_dev(PSC_BASE));
188}
189
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200190void serial0_setbrg (void)
191{
192 serial_setbrg_dev(PSC_BASE);
193}
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200194
195void serial0_putc(const char c)
196{
197 serial_putc_dev(PSC_BASE,c);
198}
199
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200200void serial0_puts(const char *s)
201{
202 serial_puts_dev(PSC_BASE, s);
203}
204
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200205int serial0_getc(void)
206{
207 return(serial_getc_dev(PSC_BASE));
208}
209
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200210int serial0_tstc(void)
211{
212 return (serial_tstc_dev(PSC_BASE));
213}
214
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200215struct serial_device serial0_device =
216{
Marek Vasut5bcdf242012-09-09 18:48:28 +0200217 .name = "serial0",
218 .start = serial0_init,
219 .stop = NULL,
220 .setbrg = serial0_setbrg,
221 .getc = serial0_getc,
222 .tstc = serial0_tstc,
223 .putc = serial0_putc,
224 .puts = serial0_puts,
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200225};
226
Mike Frysingerf96c0422011-04-29 18:03:29 +0000227__weak struct serial_device *default_serial_console(void)
228{
229 return &serial0_device;
230}
231
Marek Vasutf35317c2012-09-14 23:44:09 +0200232#ifdef CONFIG_PSC_CONSOLE2
233int serial1_init(void)
234{
235 return serial_init_dev(PSC_BASE2);
236}
237
238void serial1_setbrg(void)
239{
240 serial_setbrg_dev(PSC_BASE2);
241}
242
243void serial1_putc(const char c)
244{
245 serial_putc_dev(PSC_BASE2, c);
246}
247
248void serial1_puts(const char *s)
249{
250 serial_puts_dev(PSC_BASE2, s);
251}
252
253int serial1_getc(void)
254{
255 return serial_getc_dev(PSC_BASE2);
256}
257
258int serial1_tstc(void)
259{
260 return serial_tstc_dev(PSC_BASE2);
261}
262
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200263struct serial_device serial1_device =
264{
Marek Vasut5bcdf242012-09-09 18:48:28 +0200265 .name = "serial1",
266 .start = serial1_init,
267 .stop = NULL,
268 .setbrg = serial1_setbrg,
269 .getc = serial1_getc,
270 .tstc = serial1_tstc,
271 .putc = serial1_putc,
272 .puts = serial1_puts,
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200273};
Marek Vasutf35317c2012-09-14 23:44:09 +0200274#endif /* CONFIG_PSC_CONSOLE2 */
Wolfgang Denk16c3a452006-06-16 17:04:45 +0200275
wdenk21136db2003-07-16 21:53:01 +0000276#endif /* CONFIG_PSC_CONSOLE */