blob: 0826d59ab2adbba604665b520278898130e9fa59 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu48ca8822013-07-23 13:58:20 +09003 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +09004 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
5 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk61fb15c52007-12-27 01:52:50 +01006 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09008 */
9
10#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010011#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090012#include <asm/processor.h>
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090013#include "serial_sh.h"
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020014#include <serial.h>
15#include <linux/compiler.h>
John Rigby29565322010-12-20 18:27:51 -070016
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090017#if defined(CONFIG_CONS_SCIF0)
18# define SCIF_BASE SCIF0_BASE
19#elif defined(CONFIG_CONS_SCIF1)
20# define SCIF_BASE SCIF1_BASE
21#elif defined(CONFIG_CONS_SCIF2)
22# define SCIF_BASE SCIF2_BASE
23#elif defined(CONFIG_CONS_SCIF3)
24# define SCIF_BASE SCIF3_BASE
25#elif defined(CONFIG_CONS_SCIF4)
26# define SCIF_BASE SCIF4_BASE
27#elif defined(CONFIG_CONS_SCIF5)
28# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +000029#elif defined(CONFIG_CONS_SCIF6)
30# define SCIF_BASE SCIF6_BASE
31#elif defined(CONFIG_CONS_SCIF7)
32# define SCIF_BASE SCIF7_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090033#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090034# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090035#endif
36
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090037#if defined(CONFIG_SCIF_A)
38 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090039#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090040 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090041#endif
42
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090043static struct uart_port sh_sci = {
44 .membase = (unsigned char*)SCIF_BASE,
45 .mapbase = SCIF_BASE,
46 .type = SCIF_BASE_PORT,
47};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090048
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020049static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090050{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090051 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsuf8b7d9e2013-08-20 10:31:53 +090052
53 sci_out(&sh_sci, SCBRR,
54 SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090055}
56
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020057static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090058{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090059 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
60 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
61 sci_out(&sh_sci, SCSMR, 0);
62 sci_out(&sh_sci, SCSMR, 0);
63 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
64 sci_in(&sh_sci, SCFCR);
65 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090066
67 serial_setbrg();
68 return 0;
69}
70
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090071#if defined(CONFIG_CPU_SH7760) || \
72 defined(CONFIG_CPU_SH7780) || \
73 defined(CONFIG_CPU_SH7785) || \
74 defined(CONFIG_CPU_SH7786)
75static int scif_rxfill(struct uart_port *port)
76{
77 return sci_in(port, SCRFDR) & 0xff;
78}
79#elif defined(CONFIG_CPU_SH7763)
80static int scif_rxfill(struct uart_port *port)
81{
82 if ((port->mapbase == 0xffe00000) ||
83 (port->mapbase == 0xffe08000)) {
84 /* SCIF0/1*/
85 return sci_in(port, SCRFDR) & 0xff;
86 } else {
87 /* SCIF2 */
88 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
89 }
90}
91#elif defined(CONFIG_ARCH_SH7372)
92static int scif_rxfill(struct uart_port *port)
93{
94 if (port->type == PORT_SCIFA)
95 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
96 else
97 return sci_in(port, SCRFDR);
98}
99#else
100static int scif_rxfill(struct uart_port *port)
101{
102 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
103}
104#endif
105
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900106static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900107{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900108 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900109}
110
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000111static void handle_error(void)
112{
113 sci_in(&sh_sci, SCxSR);
114 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
115 sci_in(&sh_sci, SCLSR);
116 sci_out(&sh_sci, SCLSR, 0x00);
117}
118
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900119void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900120{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900121 while (1) {
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900122 /* Tx fifo is empty */
123 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900124 break;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900125 }
126
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900127 sci_out(&sh_sci, SCxTDR, c);
128 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900129}
130
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200131static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900132{
133 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900134 serial_raw_putc('\r');
135 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900136}
137
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200138static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900139{
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000140 if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
141 handle_error();
142 return 0;
143 }
144
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900145 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900146}
147
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900148
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900149int serial_getc_check(void)
150{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900151 unsigned short status;
152
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900153 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900154
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900155 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900156 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900157 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900158 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900159 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900160}
161
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200162static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900163{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900164 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900165 char ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900166
167 while (!serial_getc_check())
168 ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900169
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900170 ch = sci_in(&sh_sci, SCxRDR);
171 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900172
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900173 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900174
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900175 if (status & SCIF_ERRORS)
176 handle_error();
177
178 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900179 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900180 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900181}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200182
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200183static struct serial_device sh_serial_drv = {
184 .name = "sh_serial",
185 .start = sh_serial_init,
186 .stop = NULL,
187 .setbrg = sh_serial_setbrg,
188 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000189 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200190 .getc = sh_serial_getc,
191 .tstc = sh_serial_tstc,
192};
193
194void sh_serial_initialize(void)
195{
196 serial_register(&sh_serial_drv);
197}
198
199__weak struct serial_device *default_serial_console(void)
200{
201 return &sh_serial_drv;
202}