blob: 732856a5c57106ddc6e2e4e09b6e41680bbffa56 [file] [log] [blame]
wdenk359733b2003-03-31 17:27:09 +00001/*
2 * (C) Copyright 2003
3 * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
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
wdenk57b2d802003-06-27 21:31:46 +000020 * Foundation,
wdenk359733b2003-03-31 17:27:09 +000021 */
22
23/*
24 * File: serial.c
wdenk57b2d802003-06-27 21:31:46 +000025 *
26 * Discription: Serial interface driver for SCI1 and SCI2.
Wolfgang Denka1be4762008-05-20 16:00:29 +020027 * Since this code will be called from ROM use
28 * only non-static local variables.
wdenk359733b2003-03-31 17:27:09 +000029 *
30 */
31
32#include <common.h>
33#include <watchdog.h>
34#include <command.h>
35#include <mpc5xx.h>
Marek Vasut58698e72012-09-13 01:26:42 +020036#include <serial.h>
37#include <linux/compiler.h>
wdenk359733b2003-03-31 17:27:09 +000038
Wolfgang Denk6405a152006-03-31 18:32:53 +020039DECLARE_GLOBAL_DATA_PTR;
wdenk359733b2003-03-31 17:27:09 +000040
41/*
Marek Vasut58698e72012-09-13 01:26:42 +020042 * Local functions
wdenk359733b2003-03-31 17:27:09 +000043 */
44
Marek Vasut58698e72012-09-13 01:26:42 +020045static int ready_to_send(void)
46{
47 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
48 volatile short status;
49
50 do {
51#if defined(CONFIG_5xx_CONS_SCI1)
52 status = immr->im_qsmcm.qsmcm_sc1sr;
53#else
54 status = immr->im_qsmcm.qsmcm_sc2sr;
55#endif
56
57#if defined(CONFIG_WATCHDOG)
58 reset_5xx_watchdog (immr);
59#endif
60 } while ((status & SCI_TDRE) == 0);
61 return 1;
62
63}
wdenk359733b2003-03-31 17:27:09 +000064
65/*
66 * Minimal global serial functions needed to use one of the SCI modules.
67 */
68
Marek Vasut58698e72012-09-13 01:26:42 +020069static int mpc5xx_serial_init(void)
wdenk359733b2003-03-31 17:27:09 +000070{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk359733b2003-03-31 17:27:09 +000072
73 serial_setbrg();
74
75#if defined(CONFIG_5xx_CONS_SCI1)
76 /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */
77 immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10;
wdenk57b2d802003-06-27 21:31:46 +000078 immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE;
wdenk359733b2003-03-31 17:27:09 +000079#else
wdenk57b2d802003-06-27 21:31:46 +000080 immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10;
wdenk359733b2003-03-31 17:27:09 +000081 immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE;
82#endif
83 return 0;
84}
85
Marek Vasut58698e72012-09-13 01:26:42 +020086static void mpc5xx_serial_putc(const char c)
wdenk57b2d802003-06-27 21:31:46 +000087{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020088 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk57b2d802003-06-27 21:31:46 +000089
wdenk359733b2003-03-31 17:27:09 +000090 /* Test for completition */
91 if(ready_to_send()) {
92#if defined(CONFIG_5xx_CONS_SCI1)
wdenk57b2d802003-06-27 21:31:46 +000093 immr->im_qsmcm.qsmcm_sc1dr = (short)c;
wdenk359733b2003-03-31 17:27:09 +000094#else
95 immr->im_qsmcm.qsmcm_sc2dr = (short)c;
wdenk57b2d802003-06-27 21:31:46 +000096#endif
wdenk359733b2003-03-31 17:27:09 +000097 if(c == '\n') {
98 if(ready_to_send());
99#if defined(CONFIG_5xx_CONS_SCI1)
100 immr->im_qsmcm.qsmcm_sc1dr = (short)'\r';
101#else
102 immr->im_qsmcm.qsmcm_sc2dr = (short)'\r';
103#endif
104 }
105 }
106}
107
Marek Vasut58698e72012-09-13 01:26:42 +0200108static int mpc5xx_serial_getc(void)
wdenk57b2d802003-06-27 21:31:46 +0000109{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200110 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk359733b2003-03-31 17:27:09 +0000111 volatile short status;
112 unsigned char tmp;
wdenk57b2d802003-06-27 21:31:46 +0000113
wdenk359733b2003-03-31 17:27:09 +0000114 /* New data ? */
115 do {
116#if defined(CONFIG_5xx_CONS_SCI1)
wdenk57b2d802003-06-27 21:31:46 +0000117 status = immr->im_qsmcm.qsmcm_sc1sr;
wdenk359733b2003-03-31 17:27:09 +0000118#else
119 status = immr->im_qsmcm.qsmcm_sc2sr;
120#endif
121
122#if defined(CONFIG_WATCHDOG)
wdenk57b2d802003-06-27 21:31:46 +0000123 reset_5xx_watchdog (immr);
wdenk359733b2003-03-31 17:27:09 +0000124#endif
wdenk57b2d802003-06-27 21:31:46 +0000125 } while ((status & SCI_RDRF) == 0);
126
wdenk359733b2003-03-31 17:27:09 +0000127 /* Read data */
128#if defined(CONFIG_5xx_CONS_SCI1)
wdenk57b2d802003-06-27 21:31:46 +0000129 tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK);
wdenk359733b2003-03-31 17:27:09 +0000130#else
131 tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK);
132#endif
133 return tmp;
134}
135
Marek Vasut58698e72012-09-13 01:26:42 +0200136static int mpc5xx_serial_tstc(void)
wdenk359733b2003-03-31 17:27:09 +0000137{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200138 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk57b2d802003-06-27 21:31:46 +0000139 short status;
wdenk359733b2003-03-31 17:27:09 +0000140
141 /* New data character ? */
142#if defined(CONFIG_5xx_CONS_SCI1)
wdenk57b2d802003-06-27 21:31:46 +0000143 status = immr->im_qsmcm.qsmcm_sc1sr;
wdenk359733b2003-03-31 17:27:09 +0000144#else
145 status = immr->im_qsmcm.qsmcm_sc2sr;
146#endif
wdenk57b2d802003-06-27 21:31:46 +0000147 return (status & SCI_RDRF);
wdenk359733b2003-03-31 17:27:09 +0000148}
149
Marek Vasut58698e72012-09-13 01:26:42 +0200150static void mpc5xx_serial_setbrg(void)
wdenk359733b2003-03-31 17:27:09 +0000151{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200152 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk359733b2003-03-31 17:27:09 +0000153 short scxbr;
154
155 /* Set baudrate */
156 scxbr = (gd->cpu_clk / (32 * gd->baudrate));
157#if defined(CONFIG_5xx_CONS_SCI1)
wdenk57b2d802003-06-27 21:31:46 +0000158 immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK);
wdenk359733b2003-03-31 17:27:09 +0000159#else
160 immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK);
161#endif
162}
163
Marek Vasut58698e72012-09-13 01:26:42 +0200164static struct serial_device mpc5xx_serial_drv = {
165 .name = "mpc5xx_serial",
166 .start = mpc5xx_serial_init,
167 .stop = NULL,
168 .setbrg = mpc5xx_serial_setbrg,
169 .putc = mpc5xx_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000170 .puts = default_serial_puts,
Marek Vasut58698e72012-09-13 01:26:42 +0200171 .getc = mpc5xx_serial_getc,
172 .tstc = mpc5xx_serial_tstc,
173};
174
175void mpc5xx_serial_initialize(void)
wdenk359733b2003-03-31 17:27:09 +0000176{
Marek Vasut58698e72012-09-13 01:26:42 +0200177 serial_register(&mpc5xx_serial_drv);
178}
wdenk359733b2003-03-31 17:27:09 +0000179
Marek Vasut58698e72012-09-13 01:26:42 +0200180__weak struct serial_device *default_serial_console(void)
181{
182 return &mpc5xx_serial_drv;
183}