blob: fcf69abd2544e978563138b36b24a77454587ad1 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +09003 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
4 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk0a5c2142007-12-27 01:52:50 +01005 *
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010022#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090023#include <asm/processor.h>
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090024#include "serial_sh.h"
John Rigby0d21ed02010-12-20 18:27:51 -070025
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090026#if defined(CONFIG_CONS_SCIF0)
27# define SCIF_BASE SCIF0_BASE
28#elif defined(CONFIG_CONS_SCIF1)
29# define SCIF_BASE SCIF1_BASE
30#elif defined(CONFIG_CONS_SCIF2)
31# define SCIF_BASE SCIF2_BASE
32#elif defined(CONFIG_CONS_SCIF3)
33# define SCIF_BASE SCIF3_BASE
34#elif defined(CONFIG_CONS_SCIF4)
35# define SCIF_BASE SCIF4_BASE
36#elif defined(CONFIG_CONS_SCIF5)
37# define SCIF_BASE SCIF5_BASE
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090038#else
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090039# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090040#endif
41
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090042#if defined(CONFIG_SCIF_A)
43 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090044#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090045 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090046#endif
47
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090048static struct uart_port sh_sci = {
49 .membase = (unsigned char*)SCIF_BASE,
50 .mapbase = SCIF_BASE,
51 .type = SCIF_BASE_PORT,
52};
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090053
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090054void serial_setbrg(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090055{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090056 DECLARE_GLOBAL_DATA_PTR;
57 sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090058}
59
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090060int serial_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090061{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090062 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
63 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
64 sci_out(&sh_sci, SCSMR, 0);
65 sci_out(&sh_sci, SCSMR, 0);
66 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
67 sci_in(&sh_sci, SCFCR);
68 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090069
70 serial_setbrg();
71 return 0;
72}
73
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090074#if defined(CONFIG_CPU_SH7760) || \
75 defined(CONFIG_CPU_SH7780) || \
76 defined(CONFIG_CPU_SH7785) || \
77 defined(CONFIG_CPU_SH7786)
78static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090079{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090080 return sci_in(port, SCRFDR) & 0xff;
81}
82#elif defined(CONFIG_CPU_SH7763)
83static int scif_rxfill(struct uart_port *port)
84{
85 if ((port->mapbase == 0xffe00000) ||
86 (port->mapbase == 0xffe08000)) {
87 /* SCIF0/1*/
88 return sci_in(port, SCRFDR) & 0xff;
89 } else {
90 /* SCIF2 */
91 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
92 }
93}
94#elif defined(CONFIG_ARCH_SH7372)
95static int scif_rxfill(struct uart_port *port)
96{
97 if (port->type == PORT_SCIFA)
98 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
99 else
100 return sci_in(port, SCRFDR);
101}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900102#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900103static int scif_rxfill(struct uart_port *port)
104{
105 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
106}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900107#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900108
109static int serial_rx_fifo_level(void)
110{
111 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900112}
113
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900114void serial_raw_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900115{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900116 while (1) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900117 /* Tx fifo is empty */
118 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900119 break;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900120 }
121
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900122 sci_out(&sh_sci, SCxTDR, c);
123 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900124}
125
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900126void serial_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900127{
128 if (c == '\n')
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900129 serial_raw_putc('\r');
130 serial_raw_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900131}
132
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900133void serial_puts(const char *s)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900134{
135 char c;
136 while ((c = *s++) != 0)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900137 serial_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900138}
139
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900140int serial_tstc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900141{
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900142 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900143}
144
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900145void handle_error(void)
146{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900147 sci_in(&sh_sci, SCxSR);
148 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
149 sci_in(&sh_sci, SCLSR);
150 sci_out(&sh_sci, SCLSR, 0x00);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900151}
152
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900153int serial_getc_check(void)
154{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900155 unsigned short status;
156
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900157 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900158
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900159 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900160 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900161 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900162 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900163 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900164}
165
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900166int serial_getc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900167{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900168 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900169 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900170
171 while (!serial_getc_check())
172 ;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900173
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900174 ch = sci_in(&sh_sci, SCxRDR);
175 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900176
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900177 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900178
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900179 if (status & SCIF_ERRORS)
180 handle_error();
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900181
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900182 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900183 handle_error();
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900184 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900185}