blob: e08bdcadc9c72edd7bd47a0b0ae49773b586200a [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010014#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090015#include <asm/processor.h>
Marek Vasut904d3d72012-09-14 22:40:08 +020016#include <serial.h>
17#include <linux/compiler.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090018#include <dm/platform_data/serial_sh.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090020#include "serial_sh.h"
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090021
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +090022DECLARE_GLOBAL_DATA_PTR;
23
Marek Vasut39df77a2019-05-07 22:31:23 +020024#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090025static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090026{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090027 return sci_in(port, SCRFDR) & 0xff;
28}
29#elif defined(CONFIG_CPU_SH7763)
30static int scif_rxfill(struct uart_port *port)
31{
32 if ((port->mapbase == 0xffe00000) ||
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090033 (port->mapbase == 0xffe08000)) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090034 /* SCIF0/1*/
35 return sci_in(port, SCRFDR) & 0xff;
36 } else {
37 /* SCIF2 */
38 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
39 }
40}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090041#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090042static int scif_rxfill(struct uart_port *port)
43{
44 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
45}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090046#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090047
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090048static void sh_serial_init_generic(struct uart_port *port)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090049{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090050 sci_out(port, SCSCR , SCSCR_INIT(port));
51 sci_out(port, SCSCR , SCSCR_INIT(port));
52 sci_out(port, SCSMR, 0);
53 sci_out(port, SCSMR, 0);
54 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
55 sci_in(port, SCFCR);
56 sci_out(port, SCFCR, 0);
Marek Vasut2d2e3ff2019-05-01 18:20:00 +020057#if defined(CONFIG_RZA1)
58 sci_out(port, SCSPTR, 0x0003);
59#endif
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090060}
61
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090062static void
63sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000064{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090065 if (port->clk_mode == EXT_CLK) {
66 unsigned short dl = DL_VALUE(baudrate, clk);
67 sci_out(port, DL, dl);
Nobuhiro Iwamatsu17861752014-12-10 14:42:05 +090068 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090069 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
70 } else {
71 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
72 }
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000073}
74
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090075static void handle_error(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090076{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090077 sci_in(port, SCxSR);
78 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
79 sci_in(port, SCLSR);
80 sci_out(port, SCLSR, 0x00);
81}
82
83static int serial_raw_putc(struct uart_port *port, const char c)
84{
85 /* Tx fifo is empty */
86 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
87 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090088
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090089 sci_out(port, SCxTDR, c);
90 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
91
92 return 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090093}
94
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090095static int serial_rx_fifo_level(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090096{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090097 return scif_rxfill(port);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090098}
99
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900100static int sh_serial_tstc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900101{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900102 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
103 handle_error(port);
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000104 return 0;
105 }
106
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900107 return serial_rx_fifo_level(port) ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900108}
109
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900110static int serial_getc_check(struct uart_port *port)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900111{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900112 unsigned short status;
113
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900114 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900115
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900116 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900117 handle_error(port);
118 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
119 handle_error(port);
Marek Vasutd88686b2020-05-09 22:30:05 +0200120 status &= (SCIF_DR | SCxSR_RDxF(port));
121 if (status)
122 return status;
123 return scif_rxfill(port);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900124}
125
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900126static int sh_serial_getc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900127{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900128 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900129 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900130
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900131 if (!serial_getc_check(port))
132 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900133
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900134 ch = sci_in(port, SCxRDR);
135 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900136
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900137 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900138
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900139 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900140 handle_error(port);
141
142 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
143 handle_error(port);
144
145 return ch;
146}
147
Marek Vasut0dfa9912018-02-16 01:33:27 +0100148#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900149
150static int sh_serial_pending(struct udevice *dev, bool input)
151{
152 struct uart_port *priv = dev_get_priv(dev);
153
154 return sh_serial_tstc_generic(priv);
155}
156
157static int sh_serial_putc(struct udevice *dev, const char ch)
158{
159 struct uart_port *priv = dev_get_priv(dev);
160
161 return serial_raw_putc(priv, ch);
162}
163
164static int sh_serial_getc(struct udevice *dev)
165{
166 struct uart_port *priv = dev_get_priv(dev);
167
168 return sh_serial_getc_generic(priv);
169}
170
171static int sh_serial_setbrg(struct udevice *dev, int baudrate)
172{
Simon Glassb75b15b2020-12-03 16:55:23 -0700173 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900174 struct uart_port *priv = dev_get_priv(dev);
175
176 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
177
178 return 0;
179}
180
181static int sh_serial_probe(struct udevice *dev)
182{
Simon Glassb75b15b2020-12-03 16:55:23 -0700183 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900184 struct uart_port *priv = dev_get_priv(dev);
185
186 priv->membase = (unsigned char *)plat->base;
187 priv->mapbase = plat->base;
188 priv->type = plat->type;
189 priv->clk_mode = plat->clk_mode;
190
191 sh_serial_init_generic(priv);
192
193 return 0;
194}
195
196static const struct dm_serial_ops sh_serial_ops = {
197 .putc = sh_serial_putc,
198 .pending = sh_serial_pending,
199 .getc = sh_serial_getc,
200 .setbrg = sh_serial_setbrg,
201};
202
Marek Vasut0dfa9912018-02-16 01:33:27 +0100203#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900204static const struct udevice_id sh_serial_id[] ={
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900205 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900206 {.compatible = "renesas,scif", .data = PORT_SCIF},
207 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
208 {}
209};
210
Simon Glassaad29ae2020-12-03 16:55:21 -0700211static int sh_serial_of_to_plat(struct udevice *dev)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900212{
Simon Glassb75b15b2020-12-03 16:55:23 -0700213 struct sh_serial_plat *plat = dev_get_plat(dev);
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200214 struct clk sh_serial_clk;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900215 fdt_addr_t addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200216 int ret;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900217
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900218 addr = dev_read_addr(dev);
Marek Vasut48db7762018-01-17 22:36:37 +0100219 if (!addr)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900220 return -EINVAL;
221
222 plat->base = addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200223
224 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200225 if (!ret) {
226 ret = clk_enable(&sh_serial_clk);
227 if (!ret)
228 plat->clk = clk_get_rate(&sh_serial_clk);
229 } else {
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200230 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
231 "clock", 1);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200232 }
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200233
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900234 plat->type = dev_get_driver_data(dev);
235 return 0;
236}
237#endif
238
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900239U_BOOT_DRIVER(serial_sh) = {
240 .name = "serial_sh",
241 .id = UCLASS_SERIAL,
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900242 .of_match = of_match_ptr(sh_serial_id),
Simon Glassaad29ae2020-12-03 16:55:21 -0700243 .of_to_plat = of_match_ptr(sh_serial_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700244 .plat_auto = sizeof(struct sh_serial_plat),
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900245 .probe = sh_serial_probe,
246 .ops = &sh_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700247#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900248 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700249#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700250 .priv_auto = sizeof(struct uart_port),
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900251};
Marek Vasut98ae2812023-02-28 22:17:22 +0100252#endif
253
254#if !CONFIG_IS_ENABLED(DM_SERIAL) || IS_ENABLED(CONFIG_DEBUG_UART_SCIF)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900255
Marek Vasut98ae2812023-02-28 22:17:22 +0100256#if defined(CFG_SCIF_A)
257 #define SCIF_BASE_PORT PORT_SCIFA
258#elif defined(CFG_SCI)
259 #define SCIF_BASE_PORT PORT_SCI
260#else
261 #define SCIF_BASE_PORT PORT_SCIF
262#endif
263
264static void sh_serial_init_nodm(struct uart_port *port)
265{
266 sh_serial_init_generic(port);
267 serial_setbrg();
268}
269
270static void sh_serial_putc_nondm(struct uart_port *port, const char c)
271{
272 if (c == '\n') {
273 while (1) {
274 if (serial_raw_putc(port, '\r') != -EAGAIN)
275 break;
276 }
277 }
278 while (1) {
279 if (serial_raw_putc(port, c) != -EAGAIN)
280 break;
281 }
282}
283#endif
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900284
Marek Vasut98ae2812023-02-28 22:17:22 +0100285#if !CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900286#if defined(CONFIG_CONS_SCIF0)
287# define SCIF_BASE SCIF0_BASE
288#elif defined(CONFIG_CONS_SCIF1)
289# define SCIF_BASE SCIF1_BASE
290#elif defined(CONFIG_CONS_SCIF2)
291# define SCIF_BASE SCIF2_BASE
292#elif defined(CONFIG_CONS_SCIF3)
293# define SCIF_BASE SCIF3_BASE
294#elif defined(CONFIG_CONS_SCIF4)
295# define SCIF_BASE SCIF4_BASE
296#elif defined(CONFIG_CONS_SCIF5)
297# define SCIF_BASE SCIF5_BASE
298#elif defined(CONFIG_CONS_SCIF6)
299# define SCIF_BASE SCIF6_BASE
300#elif defined(CONFIG_CONS_SCIF7)
301# define SCIF_BASE SCIF7_BASE
Marek Vasut1d9756b2018-04-12 15:23:46 +0200302#elif defined(CONFIG_CONS_SCIFA0)
303# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900304#else
305# error "Default SCIF doesn't set....."
306#endif
307
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900308static struct uart_port sh_sci = {
309 .membase = (unsigned char *)SCIF_BASE,
310 .mapbase = SCIF_BASE,
311 .type = SCIF_BASE_PORT,
Marek Vasutb811f9b2023-02-28 22:17:21 +0100312#ifdef CFG_SCIF_USE_EXT_CLK
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900313 .clk_mode = EXT_CLK,
314#endif
315};
316
317static void sh_serial_setbrg(void)
318{
319 DECLARE_GLOBAL_DATA_PTR;
320 struct uart_port *port = &sh_sci;
321
322 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
323}
324
325static int sh_serial_init(void)
326{
Marek Vasut98ae2812023-02-28 22:17:22 +0100327 sh_serial_init_nodm(&sh_sci);
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900328
329 return 0;
330}
331
332static void sh_serial_putc(const char c)
333{
Marek Vasut98ae2812023-02-28 22:17:22 +0100334 sh_serial_putc_nondm(&sh_sci, c);
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900335}
336
337static int sh_serial_tstc(void)
338{
339 struct uart_port *port = &sh_sci;
340
341 return sh_serial_tstc_generic(port);
342}
343
344static int sh_serial_getc(void)
345{
346 struct uart_port *port = &sh_sci;
347 int ch;
348
349 while (1) {
350 ch = sh_serial_getc_generic(port);
351 if (ch != -EAGAIN)
352 break;
353 }
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900354
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900355 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900356}
Marek Vasut904d3d72012-09-14 22:40:08 +0200357
Marek Vasut904d3d72012-09-14 22:40:08 +0200358static struct serial_device sh_serial_drv = {
359 .name = "sh_serial",
360 .start = sh_serial_init,
361 .stop = NULL,
362 .setbrg = sh_serial_setbrg,
363 .putc = sh_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000364 .puts = default_serial_puts,
Marek Vasut904d3d72012-09-14 22:40:08 +0200365 .getc = sh_serial_getc,
366 .tstc = sh_serial_tstc,
367};
368
369void sh_serial_initialize(void)
370{
371 serial_register(&sh_serial_drv);
372}
373
374__weak struct serial_device *default_serial_console(void)
375{
376 return &sh_serial_drv;
377}
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900378#endif /* CONFIG_DM_SERIAL */
Marek Vasut98ae2812023-02-28 22:17:22 +0100379
380#ifdef CONFIG_DEBUG_UART_SCIF
381#include <debug_uart.h>
382
383static struct uart_port debug_uart_sci = {
384 .membase = (unsigned char *)CONFIG_DEBUG_UART_BASE,
385 .mapbase = CONFIG_DEBUG_UART_BASE,
386 .type = SCIF_BASE_PORT,
387#ifdef CFG_SCIF_USE_EXT_CLK
388 .clk_mode = EXT_CLK,
389#endif
390};
391
392static inline void _debug_uart_init(void)
393{
394 sh_serial_init_nodm(&debug_uart_sci);
395}
396
397static inline void _debug_uart_putc(int c)
398{
399 sh_serial_putc_nondm(&debug_uart_sci, c);
400}
401
402DEBUG_UART_FUNCS
403
404#endif