blob: a2a8d94e0602dcf7b7fae44c5bf3a5bb9005768f [file] [log] [blame]
wdenk0db5bca2003-03-31 17:27:09 +00001/*
2 * (C) Copyright 2003
3 * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk0db5bca2003-03-31 17:27:09 +00006 */
7
8/*
9 * File: serial.c
wdenk8bde7f72003-06-27 21:31:46 +000010 *
11 * Discription: Serial interface driver for SCI1 and SCI2.
Wolfgang Denk53677ef2008-05-20 16:00:29 +020012 * Since this code will be called from ROM use
13 * only non-static local variables.
wdenk0db5bca2003-03-31 17:27:09 +000014 *
15 */
16
17#include <common.h>
18#include <watchdog.h>
19#include <command.h>
20#include <mpc5xx.h>
Marek Vasutb57c6522012-09-13 01:26:42 +020021#include <serial.h>
22#include <linux/compiler.h>
wdenk0db5bca2003-03-31 17:27:09 +000023
Wolfgang Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
wdenk0db5bca2003-03-31 17:27:09 +000025
26/*
Marek Vasutb57c6522012-09-13 01:26:42 +020027 * Local functions
wdenk0db5bca2003-03-31 17:27:09 +000028 */
29
Marek Vasutb57c6522012-09-13 01:26:42 +020030static int ready_to_send(void)
31{
32 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
33 volatile short status;
34
35 do {
36#if defined(CONFIG_5xx_CONS_SCI1)
37 status = immr->im_qsmcm.qsmcm_sc1sr;
38#else
39 status = immr->im_qsmcm.qsmcm_sc2sr;
40#endif
41
42#if defined(CONFIG_WATCHDOG)
43 reset_5xx_watchdog (immr);
44#endif
45 } while ((status & SCI_TDRE) == 0);
46 return 1;
47
48}
wdenk0db5bca2003-03-31 17:27:09 +000049
50/*
51 * Minimal global serial functions needed to use one of the SCI modules.
52 */
53
Marek Vasutb57c6522012-09-13 01:26:42 +020054static int mpc5xx_serial_init(void)
wdenk0db5bca2003-03-31 17:27:09 +000055{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk0db5bca2003-03-31 17:27:09 +000057
58 serial_setbrg();
59
60#if defined(CONFIG_5xx_CONS_SCI1)
61 /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */
62 immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10;
wdenk8bde7f72003-06-27 21:31:46 +000063 immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE;
wdenk0db5bca2003-03-31 17:27:09 +000064#else
wdenk8bde7f72003-06-27 21:31:46 +000065 immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10;
wdenk0db5bca2003-03-31 17:27:09 +000066 immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE;
67#endif
68 return 0;
69}
70
Marek Vasutb57c6522012-09-13 01:26:42 +020071static void mpc5xx_serial_putc(const char c)
wdenk8bde7f72003-06-27 21:31:46 +000072{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk8bde7f72003-06-27 21:31:46 +000074
wdenk0db5bca2003-03-31 17:27:09 +000075 /* Test for completition */
76 if(ready_to_send()) {
77#if defined(CONFIG_5xx_CONS_SCI1)
wdenk8bde7f72003-06-27 21:31:46 +000078 immr->im_qsmcm.qsmcm_sc1dr = (short)c;
wdenk0db5bca2003-03-31 17:27:09 +000079#else
80 immr->im_qsmcm.qsmcm_sc2dr = (short)c;
wdenk8bde7f72003-06-27 21:31:46 +000081#endif
wdenk0db5bca2003-03-31 17:27:09 +000082 if(c == '\n') {
83 if(ready_to_send());
84#if defined(CONFIG_5xx_CONS_SCI1)
85 immr->im_qsmcm.qsmcm_sc1dr = (short)'\r';
86#else
87 immr->im_qsmcm.qsmcm_sc2dr = (short)'\r';
88#endif
89 }
90 }
91}
92
Marek Vasutb57c6522012-09-13 01:26:42 +020093static int mpc5xx_serial_getc(void)
wdenk8bde7f72003-06-27 21:31:46 +000094{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk0db5bca2003-03-31 17:27:09 +000096 volatile short status;
97 unsigned char tmp;
wdenk8bde7f72003-06-27 21:31:46 +000098
wdenk0db5bca2003-03-31 17:27:09 +000099 /* New data ? */
100 do {
101#if defined(CONFIG_5xx_CONS_SCI1)
wdenk8bde7f72003-06-27 21:31:46 +0000102 status = immr->im_qsmcm.qsmcm_sc1sr;
wdenk0db5bca2003-03-31 17:27:09 +0000103#else
104 status = immr->im_qsmcm.qsmcm_sc2sr;
105#endif
106
107#if defined(CONFIG_WATCHDOG)
wdenk8bde7f72003-06-27 21:31:46 +0000108 reset_5xx_watchdog (immr);
wdenk0db5bca2003-03-31 17:27:09 +0000109#endif
wdenk8bde7f72003-06-27 21:31:46 +0000110 } while ((status & SCI_RDRF) == 0);
111
wdenk0db5bca2003-03-31 17:27:09 +0000112 /* Read data */
113#if defined(CONFIG_5xx_CONS_SCI1)
wdenk8bde7f72003-06-27 21:31:46 +0000114 tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK);
wdenk0db5bca2003-03-31 17:27:09 +0000115#else
116 tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK);
117#endif
118 return tmp;
119}
120
Marek Vasutb57c6522012-09-13 01:26:42 +0200121static int mpc5xx_serial_tstc(void)
wdenk0db5bca2003-03-31 17:27:09 +0000122{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200123 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk8bde7f72003-06-27 21:31:46 +0000124 short status;
wdenk0db5bca2003-03-31 17:27:09 +0000125
126 /* New data character ? */
127#if defined(CONFIG_5xx_CONS_SCI1)
wdenk8bde7f72003-06-27 21:31:46 +0000128 status = immr->im_qsmcm.qsmcm_sc1sr;
wdenk0db5bca2003-03-31 17:27:09 +0000129#else
130 status = immr->im_qsmcm.qsmcm_sc2sr;
131#endif
wdenk8bde7f72003-06-27 21:31:46 +0000132 return (status & SCI_RDRF);
wdenk0db5bca2003-03-31 17:27:09 +0000133}
134
Marek Vasutb57c6522012-09-13 01:26:42 +0200135static void mpc5xx_serial_setbrg(void)
wdenk0db5bca2003-03-31 17:27:09 +0000136{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200137 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenk0db5bca2003-03-31 17:27:09 +0000138 short scxbr;
139
140 /* Set baudrate */
141 scxbr = (gd->cpu_clk / (32 * gd->baudrate));
142#if defined(CONFIG_5xx_CONS_SCI1)
wdenk8bde7f72003-06-27 21:31:46 +0000143 immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK);
wdenk0db5bca2003-03-31 17:27:09 +0000144#else
145 immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK);
146#endif
147}
148
Marek Vasutb57c6522012-09-13 01:26:42 +0200149static struct serial_device mpc5xx_serial_drv = {
150 .name = "mpc5xx_serial",
151 .start = mpc5xx_serial_init,
152 .stop = NULL,
153 .setbrg = mpc5xx_serial_setbrg,
154 .putc = mpc5xx_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000155 .puts = default_serial_puts,
Marek Vasutb57c6522012-09-13 01:26:42 +0200156 .getc = mpc5xx_serial_getc,
157 .tstc = mpc5xx_serial_tstc,
158};
159
160void mpc5xx_serial_initialize(void)
wdenk0db5bca2003-03-31 17:27:09 +0000161{
Marek Vasutb57c6522012-09-13 01:26:42 +0200162 serial_register(&mpc5xx_serial_drv);
wdenk0db5bca2003-03-31 17:27:09 +0000163}
Marek Vasutb57c6522012-09-13 01:26:42 +0200164
165__weak struct serial_device *default_serial_console(void)
166{
167 return &mpc5xx_serial_drv;
168}