blob: 545c81aa4237d1f25607853aac5b3b56528e864e [file] [log] [blame]
wdenk983fda82004-10-28 00:09:35 +00001/*
2 * (C) Copyright 2004, Freescale, Inc
3 * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 */
24
25/*
26 * Minimal serial functions needed to use one of the PSC ports
27 * as serial console interface.
28 */
29
30#include <common.h>
31#include <mpc8220.h>
Marek Vasut2063a542012-09-13 01:29:31 +020032#include <serial.h>
33#include <linux/compiler.h>
wdenk983fda82004-10-28 00:09:35 +000034
Wolfgang Denkd87080b2006-03-31 18:32:53 +020035DECLARE_GLOBAL_DATA_PTR;
36
wdenk983fda82004-10-28 00:09:35 +000037#define PSC_BASE MMAP_PSC1
38
39#if defined(CONFIG_PSC_CONSOLE)
Marek Vasut2063a542012-09-13 01:29:31 +020040static int mpc8220_serial_init(void)
wdenk983fda82004-10-28 00:09:35 +000041{
wdenk983fda82004-10-28 00:09:35 +000042 volatile psc8220_t *psc = (psc8220_t *) PSC_BASE;
43 u32 counter;
44
45 /* write to SICR: SIM2 = uart mode,dcd does not affect rx */
46 psc->cr = 0;
47 psc->ipcr_acr = 0;
48 psc->isr_imr = 0;
49
50 /* write to CSR: RX/TX baud rate from timers */
51 psc->sr_csr = 0xdd000000;
52
wdenk3c2b3d42005-04-05 23:32:21 +000053 psc->mr1_2 = PSC_MR1_BITS_CHAR_8 | PSC_MR1_NO_PARITY | PSC_MR2_STOP_BITS_1;
wdenk983fda82004-10-28 00:09:35 +000054
55 /* Setting up BaudRate */
56 counter = ((gd->bus_clk / gd->baudrate)) >> 5;
57 counter++;
58
59 /* write to CTUR: divide counter upper byte */
60 psc->ctur = ((counter & 0xff00) << 16);
61 /* write to CTLR: divide counter lower byte */
62 psc->ctlr = ((counter & 0x00ff) << 24);
63
64 psc->cr = PSC_CR_RST_RX_CMD;
65 psc->cr = PSC_CR_RST_TX_CMD;
66 psc->cr = PSC_CR_RST_ERR_STS_CMD;
67 psc->cr = PSC_CR_RST_BRK_INT_CMD;
68 psc->cr = PSC_CR_RST_MR_PTR_CMD;
69
70 psc->cr = PSC_CR_RX_ENABLE | PSC_CR_TX_ENABLE;
71 return (0);
72}
73
Marek Vasut2063a542012-09-13 01:29:31 +020074static void mpc8220_serial_putc(const char c)
wdenk983fda82004-10-28 00:09:35 +000075{
76 volatile psc8220_t *psc = (psc8220_t *) PSC_BASE;
77
78 if (c == '\n')
79 serial_putc ('\r');
80
81 /* Wait for last character to go. */
wdenk12b43d52005-04-05 21:57:18 +000082 while (!(psc->sr_csr & PSC_SR_TXRDY));
wdenk983fda82004-10-28 00:09:35 +000083
84 psc->xmitbuf[0] = c;
85}
86
Marek Vasut2063a542012-09-13 01:29:31 +020087static void mpc8220_serial_puts(const char *s)
wdenk983fda82004-10-28 00:09:35 +000088{
89 while (*s) {
90 serial_putc (*s++);
91 }
92}
93
Marek Vasut2063a542012-09-13 01:29:31 +020094static int mpc8220_serial_getc(void)
wdenk983fda82004-10-28 00:09:35 +000095{
96 volatile psc8220_t *psc = (psc8220_t *) PSC_BASE;
97
98 /* Wait for a character to arrive. */
99 while (!(psc->sr_csr & PSC_SR_RXRDY));
100 return psc->xmitbuf[2];
101}
102
Marek Vasut2063a542012-09-13 01:29:31 +0200103static int mpc8220_serial_tstc(void)
wdenk983fda82004-10-28 00:09:35 +0000104{
105 volatile psc8220_t *psc = (psc8220_t *) PSC_BASE;
106
107 return (psc->sr_csr & PSC_SR_RXRDY);
108}
109
Marek Vasut2063a542012-09-13 01:29:31 +0200110static void mpc8220_serial_setbrg(void)
wdenk983fda82004-10-28 00:09:35 +0000111{
wdenk983fda82004-10-28 00:09:35 +0000112 volatile psc8220_t *psc = (psc8220_t *) PSC_BASE;
113 u32 counter;
114
115 counter = ((gd->bus_clk / gd->baudrate)) >> 5;
116 counter++;
117
118 /* write to CTUR: divide counter upper byte */
119 psc->ctur = ((counter & 0xff00) << 16);
120 /* write to CTLR: divide counter lower byte */
121 psc->ctlr = ((counter & 0x00ff) << 24);
122
123 psc->cr = PSC_CR_RST_RX_CMD;
124 psc->cr = PSC_CR_RST_TX_CMD;
125
126 psc->cr = PSC_CR_RX_ENABLE | PSC_CR_TX_ENABLE;
127}
Marek Vasut2063a542012-09-13 01:29:31 +0200128
129#ifdef CONFIG_SERIAL_MULTI
130static struct serial_device mpc8220_serial_drv = {
131 .name = "mpc8220_serial",
132 .start = mpc8220_serial_init,
133 .stop = NULL,
134 .setbrg = mpc8220_serial_setbrg,
135 .putc = mpc8220_serial_putc,
136 .puts = mpc8220_serial_puts,
137 .getc = mpc8220_serial_getc,
138 .tstc = mpc8220_serial_tstc,
139};
140
141void mpc8220_serial_initialize(void)
142{
143 serial_register(&mpc8220_serial_drv);
144}
145
146__weak struct serial_device *default_serial_console(void)
147{
148 return &mpc8220_serial_drv;
149}
150#else
151int serial_init(void)
152{
153 return mpc8220_serial_init();
154}
155
156void serial_setbrg(void)
157{
158 mpc8220_serial_setbrg();
159}
160
161void serial_putc(const char c)
162{
163 mpc8220_serial_putc(c);
164}
165
166void serial_puts(const char *s)
167{
168 mpc8220_serial_puts(s);
169}
170
171int serial_getc(void)
172{
173 return mpc8220_serial_getc();
174}
175
176int serial_tstc(void)
177{
178 return mpc8220_serial_tstc();
179}
180#endif
wdenk983fda82004-10-28 00:09:35 +0000181#endif /* CONFIG_PSC_CONSOLE */