blob: acfcc2954a9e60ff81bece443b450d788e529fa2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09002/*
3 * SuperH SCIF device driver.
Nobuhiro Iwamatsu788b73f2013-07-23 13:58:20 +09004 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +09005 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09007 */
8
9#include <common.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090010#include <errno.h>
Marek Vasut8fe2ffc2017-07-21 23:19:18 +020011#include <clk.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090012#include <dm.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010013#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090014#include <asm/processor.h>
Marek Vasut904d3d72012-09-14 22:40:08 +020015#include <serial.h>
16#include <linux/compiler.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090017#include <dm/platform_data/serial_sh.h>
18#include "serial_sh.h"
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090019
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +090020DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasut39df77a2019-05-07 22:31:23 +020022#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090023static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090024{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090025 return sci_in(port, SCRFDR) & 0xff;
26}
27#elif defined(CONFIG_CPU_SH7763)
28static int scif_rxfill(struct uart_port *port)
29{
30 if ((port->mapbase == 0xffe00000) ||
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090031 (port->mapbase == 0xffe08000)) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090032 /* SCIF0/1*/
33 return sci_in(port, SCRFDR) & 0xff;
34 } else {
35 /* SCIF2 */
36 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
37 }
38}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090039#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090040static int scif_rxfill(struct uart_port *port)
41{
42 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
43}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090044#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090045
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090046static void sh_serial_init_generic(struct uart_port *port)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090047{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090048 sci_out(port, SCSCR , SCSCR_INIT(port));
49 sci_out(port, SCSCR , SCSCR_INIT(port));
50 sci_out(port, SCSMR, 0);
51 sci_out(port, SCSMR, 0);
52 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
53 sci_in(port, SCFCR);
54 sci_out(port, SCFCR, 0);
Marek Vasut2d2e3ff2019-05-01 18:20:00 +020055#if defined(CONFIG_RZA1)
56 sci_out(port, SCSPTR, 0x0003);
57#endif
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090058}
59
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090060static void
61sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000062{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090063 if (port->clk_mode == EXT_CLK) {
64 unsigned short dl = DL_VALUE(baudrate, clk);
65 sci_out(port, DL, dl);
Nobuhiro Iwamatsu17861752014-12-10 14:42:05 +090066 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090067 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
68 } else {
69 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
70 }
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000071}
72
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090073static void handle_error(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090074{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090075 sci_in(port, SCxSR);
76 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
77 sci_in(port, SCLSR);
78 sci_out(port, SCLSR, 0x00);
79}
80
81static int serial_raw_putc(struct uart_port *port, const char c)
82{
83 /* Tx fifo is empty */
84 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
85 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090086
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090087 sci_out(port, SCxTDR, c);
88 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
89
90 return 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090091}
92
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090093static int serial_rx_fifo_level(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090094{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090095 return scif_rxfill(port);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090096}
97
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090098static int sh_serial_tstc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090099{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900100 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
101 handle_error(port);
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000102 return 0;
103 }
104
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900105 return serial_rx_fifo_level(port) ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900106}
107
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900108static int serial_getc_check(struct uart_port *port)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900109{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900110 unsigned short status;
111
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900112 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900113
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900114 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900115 handle_error(port);
116 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
117 handle_error(port);
118 return status & (SCIF_DR | SCxSR_RDxF(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900119}
120
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900121static int sh_serial_getc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900122{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900123 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900124 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900125
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900126 if (!serial_getc_check(port))
127 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900128
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900129 ch = sci_in(port, SCxRDR);
130 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900131
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900132 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900133
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900134 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900135 handle_error(port);
136
137 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
138 handle_error(port);
139
140 return ch;
141}
142
Marek Vasut0dfa9912018-02-16 01:33:27 +0100143#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900144
145static int sh_serial_pending(struct udevice *dev, bool input)
146{
147 struct uart_port *priv = dev_get_priv(dev);
148
149 return sh_serial_tstc_generic(priv);
150}
151
152static int sh_serial_putc(struct udevice *dev, const char ch)
153{
154 struct uart_port *priv = dev_get_priv(dev);
155
156 return serial_raw_putc(priv, ch);
157}
158
159static int sh_serial_getc(struct udevice *dev)
160{
161 struct uart_port *priv = dev_get_priv(dev);
162
163 return sh_serial_getc_generic(priv);
164}
165
166static int sh_serial_setbrg(struct udevice *dev, int baudrate)
167{
168 struct sh_serial_platdata *plat = dev_get_platdata(dev);
169 struct uart_port *priv = dev_get_priv(dev);
170
171 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
172
173 return 0;
174}
175
176static int sh_serial_probe(struct udevice *dev)
177{
178 struct sh_serial_platdata *plat = dev_get_platdata(dev);
179 struct uart_port *priv = dev_get_priv(dev);
180
181 priv->membase = (unsigned char *)plat->base;
182 priv->mapbase = plat->base;
183 priv->type = plat->type;
184 priv->clk_mode = plat->clk_mode;
185
186 sh_serial_init_generic(priv);
187
188 return 0;
189}
190
191static const struct dm_serial_ops sh_serial_ops = {
192 .putc = sh_serial_putc,
193 .pending = sh_serial_pending,
194 .getc = sh_serial_getc,
195 .setbrg = sh_serial_setbrg,
196};
197
Marek Vasut0dfa9912018-02-16 01:33:27 +0100198#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900199static const struct udevice_id sh_serial_id[] ={
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900200 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900201 {.compatible = "renesas,scif", .data = PORT_SCIF},
202 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
203 {}
204};
205
206static int sh_serial_ofdata_to_platdata(struct udevice *dev)
207{
208 struct sh_serial_platdata *plat = dev_get_platdata(dev);
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200209 struct clk sh_serial_clk;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900210 fdt_addr_t addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200211 int ret;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900212
Marek Vasut48db7762018-01-17 22:36:37 +0100213 addr = devfdt_get_addr(dev);
214 if (!addr)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900215 return -EINVAL;
216
217 plat->base = addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200218
219 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200220 if (!ret) {
221 ret = clk_enable(&sh_serial_clk);
222 if (!ret)
223 plat->clk = clk_get_rate(&sh_serial_clk);
224 } else {
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200225 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
226 "clock", 1);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200227 }
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200228
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900229 plat->type = dev_get_driver_data(dev);
230 return 0;
231}
232#endif
233
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900234U_BOOT_DRIVER(serial_sh) = {
235 .name = "serial_sh",
236 .id = UCLASS_SERIAL,
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900237 .of_match = of_match_ptr(sh_serial_id),
238 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
239 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900240 .probe = sh_serial_probe,
241 .ops = &sh_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700242#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900243 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700244#endif
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900245 .priv_auto_alloc_size = sizeof(struct uart_port),
246};
247
248#else /* CONFIG_DM_SERIAL */
249
250#if defined(CONFIG_CONS_SCIF0)
251# define SCIF_BASE SCIF0_BASE
252#elif defined(CONFIG_CONS_SCIF1)
253# define SCIF_BASE SCIF1_BASE
254#elif defined(CONFIG_CONS_SCIF2)
255# define SCIF_BASE SCIF2_BASE
256#elif defined(CONFIG_CONS_SCIF3)
257# define SCIF_BASE SCIF3_BASE
258#elif defined(CONFIG_CONS_SCIF4)
259# define SCIF_BASE SCIF4_BASE
260#elif defined(CONFIG_CONS_SCIF5)
261# define SCIF_BASE SCIF5_BASE
262#elif defined(CONFIG_CONS_SCIF6)
263# define SCIF_BASE SCIF6_BASE
264#elif defined(CONFIG_CONS_SCIF7)
265# define SCIF_BASE SCIF7_BASE
Marek Vasut1d9756b2018-04-12 15:23:46 +0200266#elif defined(CONFIG_CONS_SCIFA0)
267# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900268#else
269# error "Default SCIF doesn't set....."
270#endif
271
272#if defined(CONFIG_SCIF_A)
273 #define SCIF_BASE_PORT PORT_SCIFA
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900274#elif defined(CONFIG_SCI)
275 #define SCIF_BASE_PORT PORT_SCI
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900276#else
277 #define SCIF_BASE_PORT PORT_SCIF
278#endif
279
280static struct uart_port sh_sci = {
281 .membase = (unsigned char *)SCIF_BASE,
282 .mapbase = SCIF_BASE,
283 .type = SCIF_BASE_PORT,
284#ifdef CONFIG_SCIF_USE_EXT_CLK
285 .clk_mode = EXT_CLK,
286#endif
287};
288
289static void sh_serial_setbrg(void)
290{
291 DECLARE_GLOBAL_DATA_PTR;
292 struct uart_port *port = &sh_sci;
293
294 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
295}
296
297static int sh_serial_init(void)
298{
299 struct uart_port *port = &sh_sci;
300
301 sh_serial_init_generic(port);
302 serial_setbrg();
303
304 return 0;
305}
306
307static void sh_serial_putc(const char c)
308{
309 struct uart_port *port = &sh_sci;
310
311 if (c == '\n') {
312 while (1) {
313 if (serial_raw_putc(port, '\r') != -EAGAIN)
314 break;
315 }
316 }
317 while (1) {
318 if (serial_raw_putc(port, c) != -EAGAIN)
319 break;
320 }
321}
322
323static int sh_serial_tstc(void)
324{
325 struct uart_port *port = &sh_sci;
326
327 return sh_serial_tstc_generic(port);
328}
329
330static int sh_serial_getc(void)
331{
332 struct uart_port *port = &sh_sci;
333 int ch;
334
335 while (1) {
336 ch = sh_serial_getc_generic(port);
337 if (ch != -EAGAIN)
338 break;
339 }
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900340
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900341 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900342}
Marek Vasut904d3d72012-09-14 22:40:08 +0200343
Marek Vasut904d3d72012-09-14 22:40:08 +0200344static struct serial_device sh_serial_drv = {
345 .name = "sh_serial",
346 .start = sh_serial_init,
347 .stop = NULL,
348 .setbrg = sh_serial_setbrg,
349 .putc = sh_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000350 .puts = default_serial_puts,
Marek Vasut904d3d72012-09-14 22:40:08 +0200351 .getc = sh_serial_getc,
352 .tstc = sh_serial_tstc,
353};
354
355void sh_serial_initialize(void)
356{
357 serial_register(&sh_serial_drv);
358}
359
360__weak struct serial_device *default_serial_console(void)
361{
362 return &sh_serial_drv;
363}
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900364#endif /* CONFIG_DM_SERIAL */