blob: 13919c623baa2501aea7027db67d1bfe984755ef [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +09003 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
4 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk61fb15c52007-12-27 01:52:50 +01005 *
Nobuhiro Iwamatsu0b135cf2007-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-VILLARDfc83c922009-01-11 16:35:16 +010022#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090023#include <asm/processor.h>
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090024#include "serial_sh.h"
John Rigby29565322010-12-20 18:27:51 -070025
Nobuhiro Iwamatsuab09f432008-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
Phil Edworthy99744b72012-05-15 22:15:51 +000038#elif defined(CONFIG_CONS_SCIF6)
39# define SCIF_BASE SCIF6_BASE
40#elif defined(CONFIG_CONS_SCIF7)
41# define SCIF_BASE SCIF7_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090042#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090043# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090044#endif
45
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090046#if defined(CONFIG_SCIF_A)
47 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090048#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090049 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090050#endif
51
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090052static struct uart_port sh_sci = {
53 .membase = (unsigned char*)SCIF_BASE,
54 .mapbase = SCIF_BASE,
55 .type = SCIF_BASE_PORT,
56};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090057
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090058void serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090059{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090060 DECLARE_GLOBAL_DATA_PTR;
61 sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090062}
63
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090064int serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090065{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090066 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
67 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
68 sci_out(&sh_sci, SCSMR, 0);
69 sci_out(&sh_sci, SCSMR, 0);
70 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
71 sci_in(&sh_sci, SCFCR);
72 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090073
74 serial_setbrg();
75 return 0;
76}
77
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090078#if defined(CONFIG_CPU_SH7760) || \
79 defined(CONFIG_CPU_SH7780) || \
80 defined(CONFIG_CPU_SH7785) || \
81 defined(CONFIG_CPU_SH7786)
82static int scif_rxfill(struct uart_port *port)
83{
84 return sci_in(port, SCRFDR) & 0xff;
85}
86#elif defined(CONFIG_CPU_SH7763)
87static int scif_rxfill(struct uart_port *port)
88{
89 if ((port->mapbase == 0xffe00000) ||
90 (port->mapbase == 0xffe08000)) {
91 /* SCIF0/1*/
92 return sci_in(port, SCRFDR) & 0xff;
93 } else {
94 /* SCIF2 */
95 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
96 }
97}
98#elif defined(CONFIG_ARCH_SH7372)
99static int scif_rxfill(struct uart_port *port)
100{
101 if (port->type == PORT_SCIFA)
102 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
103 else
104 return sci_in(port, SCRFDR);
105}
106#else
107static int scif_rxfill(struct uart_port *port)
108{
109 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
110}
111#endif
112
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900113static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900114{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900115 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900116}
117
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900118void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900119{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900120 while (1) {
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900121 /* Tx fifo is empty */
122 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900123 break;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900124 }
125
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900126 sci_out(&sh_sci, SCxTDR, c);
127 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900128}
129
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900130void serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900131{
132 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900133 serial_raw_putc('\r');
134 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900135}
136
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900137void serial_puts(const char *s)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900138{
139 char c;
140 while ((c = *s++) != 0)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900141 serial_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900142}
143
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900144int serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900145{
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900146 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900147}
148
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900149void handle_error(void)
150{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900151 sci_in(&sh_sci, SCxSR);
152 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
153 sci_in(&sh_sci, SCLSR);
154 sci_out(&sh_sci, SCLSR, 0x00);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900155}
156
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900157int serial_getc_check(void)
158{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900159 unsigned short status;
160
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900161 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900162
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900163 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900164 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900165 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900166 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900167 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900168}
169
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900170int serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900171{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900172 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900173 char ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900174
175 while (!serial_getc_check())
176 ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900177
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900178 ch = sci_in(&sh_sci, SCxRDR);
179 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900180
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900181 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900182
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900183 if (status & SCIF_ERRORS)
184 handle_error();
185
186 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900187 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900188 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900189}