Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SuperH SCIF device driver. |
Nobuhiro Iwamatsu | 788b73f | 2013-07-23 13:58:20 +0900 | [diff] [blame] | 4 | * Copyright (C) 2013 Renesas Electronics Corporation |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 5 | * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 6 | * Copyright (C) 2002 - 2008 Paul Mundt |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 10 | #include <errno.h> |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 11 | #include <clk.h> |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Jean-Christophe PLAGNIOL-VILLARD | b27a8e3 | 2009-01-11 16:35:16 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 15 | #include <asm/processor.h> |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 16 | #include <serial.h> |
| 17 | #include <linux/compiler.h> |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 18 | #include <dm/platform_data/serial_sh.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 20 | #include "serial_sh.h" |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 21 | |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Marek Vasut | 39df77a | 2019-05-07 22:31:23 +0200 | [diff] [blame] | 24 | #if defined(CONFIG_CPU_SH7780) |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 25 | static int scif_rxfill(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 26 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 27 | return sci_in(port, SCRFDR) & 0xff; |
| 28 | } |
| 29 | #elif defined(CONFIG_CPU_SH7763) |
| 30 | static int scif_rxfill(struct uart_port *port) |
| 31 | { |
| 32 | if ((port->mapbase == 0xffe00000) || |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 33 | (port->mapbase == 0xffe08000)) { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 34 | /* 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 Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 41 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 42 | static int scif_rxfill(struct uart_port *port) |
| 43 | { |
| 44 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 45 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 46 | #endif |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 47 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 48 | static void sh_serial_init_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 49 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 50 | 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 Vasut | 2d2e3ff | 2019-05-01 18:20:00 +0200 | [diff] [blame] | 57 | #if defined(CONFIG_RZA1) |
| 58 | sci_out(port, SCSPTR, 0x0003); |
| 59 | #endif |
Hai Pham | 19923d8 | 2023-02-28 22:29:19 +0100 | [diff] [blame] | 60 | |
| 61 | if (port->type == PORT_HSCIF) |
| 62 | sci_out(port, HSSRR, HSSRR_SRE | HSSRR_SRCYC8); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 65 | static void |
| 66 | sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate) |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 67 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 68 | if (port->clk_mode == EXT_CLK) { |
| 69 | unsigned short dl = DL_VALUE(baudrate, clk); |
| 70 | sci_out(port, DL, dl); |
Nobuhiro Iwamatsu | 1786175 | 2014-12-10 14:42:05 +0900 | [diff] [blame] | 71 | /* Need wait: Clock * 1/dl * 1/16 */ |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 72 | udelay((1000000 * dl * 16 / clk) * 1000 + 1); |
| 73 | } else { |
| 74 | sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk)); |
| 75 | } |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 78 | static void handle_error(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 79 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 80 | sci_in(port, SCxSR); |
| 81 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 82 | sci_in(port, SCLSR); |
| 83 | sci_out(port, SCLSR, 0x00); |
| 84 | } |
| 85 | |
| 86 | static int serial_raw_putc(struct uart_port *port, const char c) |
| 87 | { |
| 88 | /* Tx fifo is empty */ |
| 89 | if (!(sci_in(port, SCxSR) & SCxSR_TEND(port))) |
| 90 | return -EAGAIN; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 91 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 92 | sci_out(port, SCxTDR, c); |
| 93 | sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port)); |
| 94 | |
| 95 | return 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 98 | static int serial_rx_fifo_level(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 99 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 100 | return scif_rxfill(port); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 103 | static int sh_serial_tstc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 104 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 105 | if (sci_in(port, SCxSR) & SCIF_ERRORS) { |
| 106 | handle_error(port); |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 110 | return serial_rx_fifo_level(port) ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 111 | } |
| 112 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 113 | static int serial_getc_check(struct uart_port *port) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 114 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 115 | unsigned short status; |
| 116 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 117 | status = sci_in(port, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 118 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 119 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 120 | handle_error(port); |
| 121 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 122 | handle_error(port); |
Marek Vasut | d88686b | 2020-05-09 22:30:05 +0200 | [diff] [blame] | 123 | status &= (SCIF_DR | SCxSR_RDxF(port)); |
| 124 | if (status) |
| 125 | return status; |
| 126 | return scif_rxfill(port); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 129 | static int sh_serial_getc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 130 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 131 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 132 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 133 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 134 | if (!serial_getc_check(port)) |
| 135 | return -EAGAIN; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 136 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 137 | ch = sci_in(port, SCxRDR); |
| 138 | status = sci_in(port, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 139 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 140 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 141 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 142 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 143 | handle_error(port); |
| 144 | |
| 145 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 146 | handle_error(port); |
| 147 | |
| 148 | return ch; |
| 149 | } |
| 150 | |
Marek Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 151 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 152 | |
| 153 | static int sh_serial_pending(struct udevice *dev, bool input) |
| 154 | { |
| 155 | struct uart_port *priv = dev_get_priv(dev); |
| 156 | |
| 157 | return sh_serial_tstc_generic(priv); |
| 158 | } |
| 159 | |
| 160 | static int sh_serial_putc(struct udevice *dev, const char ch) |
| 161 | { |
| 162 | struct uart_port *priv = dev_get_priv(dev); |
| 163 | |
| 164 | return serial_raw_putc(priv, ch); |
| 165 | } |
| 166 | |
| 167 | static int sh_serial_getc(struct udevice *dev) |
| 168 | { |
| 169 | struct uart_port *priv = dev_get_priv(dev); |
| 170 | |
| 171 | return sh_serial_getc_generic(priv); |
| 172 | } |
| 173 | |
| 174 | static int sh_serial_setbrg(struct udevice *dev, int baudrate) |
| 175 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 176 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 177 | struct uart_port *priv = dev_get_priv(dev); |
| 178 | |
| 179 | sh_serial_setbrg_generic(priv, plat->clk, baudrate); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int sh_serial_probe(struct udevice *dev) |
| 185 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 186 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 187 | struct uart_port *priv = dev_get_priv(dev); |
| 188 | |
| 189 | priv->membase = (unsigned char *)plat->base; |
| 190 | priv->mapbase = plat->base; |
| 191 | priv->type = plat->type; |
| 192 | priv->clk_mode = plat->clk_mode; |
| 193 | |
| 194 | sh_serial_init_generic(priv); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static const struct dm_serial_ops sh_serial_ops = { |
| 200 | .putc = sh_serial_putc, |
| 201 | .pending = sh_serial_pending, |
| 202 | .getc = sh_serial_getc, |
| 203 | .setbrg = sh_serial_setbrg, |
| 204 | }; |
| 205 | |
Marek Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 206 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 207 | static const struct udevice_id sh_serial_id[] ={ |
Yoshinori Sato | e5669a3 | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 208 | {.compatible = "renesas,sci", .data = PORT_SCI}, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 209 | {.compatible = "renesas,scif", .data = PORT_SCIF}, |
| 210 | {.compatible = "renesas,scifa", .data = PORT_SCIFA}, |
Hai Pham | 19923d8 | 2023-02-28 22:29:19 +0100 | [diff] [blame] | 211 | {.compatible = "renesas,hscif", .data = PORT_HSCIF}, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 212 | {} |
| 213 | }; |
| 214 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 215 | static int sh_serial_of_to_plat(struct udevice *dev) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 216 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 217 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 218 | struct clk sh_serial_clk; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 219 | fdt_addr_t addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 220 | int ret; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 221 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 222 | addr = dev_read_addr(dev); |
Marek Vasut | 48db776 | 2018-01-17 22:36:37 +0100 | [diff] [blame] | 223 | if (!addr) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 224 | return -EINVAL; |
| 225 | |
| 226 | plat->base = addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 227 | |
| 228 | ret = clk_get_by_name(dev, "fck", &sh_serial_clk); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 229 | if (!ret) { |
| 230 | ret = clk_enable(&sh_serial_clk); |
| 231 | if (!ret) |
| 232 | plat->clk = clk_get_rate(&sh_serial_clk); |
| 233 | } else { |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 234 | plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 235 | "clock", 1); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 236 | } |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 237 | |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 238 | plat->type = dev_get_driver_data(dev); |
| 239 | return 0; |
| 240 | } |
| 241 | #endif |
| 242 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 243 | U_BOOT_DRIVER(serial_sh) = { |
| 244 | .name = "serial_sh", |
| 245 | .id = UCLASS_SERIAL, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 246 | .of_match = of_match_ptr(sh_serial_id), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 247 | .of_to_plat = of_match_ptr(sh_serial_of_to_plat), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 248 | .plat_auto = sizeof(struct sh_serial_plat), |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 249 | .probe = sh_serial_probe, |
| 250 | .ops = &sh_serial_ops, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 251 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 252 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 253 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 254 | .priv_auto = sizeof(struct uart_port), |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 255 | }; |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 256 | #endif |
| 257 | |
| 258 | #if !CONFIG_IS_ENABLED(DM_SERIAL) || IS_ENABLED(CONFIG_DEBUG_UART_SCIF) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 259 | |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 260 | #if defined(CFG_SCIF_A) |
| 261 | #define SCIF_BASE_PORT PORT_SCIFA |
| 262 | #elif defined(CFG_SCI) |
| 263 | #define SCIF_BASE_PORT PORT_SCI |
Hai Pham | 19923d8 | 2023-02-28 22:29:19 +0100 | [diff] [blame] | 264 | #elif defined(CFG_HSCIF) |
| 265 | #define SCIF_BASE_PORT PORT_HSCIF |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 266 | #else |
| 267 | #define SCIF_BASE_PORT PORT_SCIF |
| 268 | #endif |
| 269 | |
| 270 | static void sh_serial_init_nodm(struct uart_port *port) |
| 271 | { |
| 272 | sh_serial_init_generic(port); |
| 273 | serial_setbrg(); |
| 274 | } |
| 275 | |
| 276 | static void sh_serial_putc_nondm(struct uart_port *port, const char c) |
| 277 | { |
| 278 | if (c == '\n') { |
| 279 | while (1) { |
| 280 | if (serial_raw_putc(port, '\r') != -EAGAIN) |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | while (1) { |
| 285 | if (serial_raw_putc(port, c) != -EAGAIN) |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | #endif |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 290 | |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 291 | #if !CONFIG_IS_ENABLED(DM_SERIAL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 292 | #if defined(CONFIG_CONS_SCIF0) |
| 293 | # define SCIF_BASE SCIF0_BASE |
| 294 | #elif defined(CONFIG_CONS_SCIF1) |
| 295 | # define SCIF_BASE SCIF1_BASE |
| 296 | #elif defined(CONFIG_CONS_SCIF2) |
| 297 | # define SCIF_BASE SCIF2_BASE |
| 298 | #elif defined(CONFIG_CONS_SCIF3) |
| 299 | # define SCIF_BASE SCIF3_BASE |
| 300 | #elif defined(CONFIG_CONS_SCIF4) |
| 301 | # define SCIF_BASE SCIF4_BASE |
| 302 | #elif defined(CONFIG_CONS_SCIF5) |
| 303 | # define SCIF_BASE SCIF5_BASE |
| 304 | #elif defined(CONFIG_CONS_SCIF6) |
| 305 | # define SCIF_BASE SCIF6_BASE |
| 306 | #elif defined(CONFIG_CONS_SCIF7) |
| 307 | # define SCIF_BASE SCIF7_BASE |
Marek Vasut | 1d9756b | 2018-04-12 15:23:46 +0200 | [diff] [blame] | 308 | #elif defined(CONFIG_CONS_SCIFA0) |
| 309 | # define SCIF_BASE SCIFA0_BASE |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 310 | #else |
| 311 | # error "Default SCIF doesn't set....." |
| 312 | #endif |
| 313 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 314 | static struct uart_port sh_sci = { |
| 315 | .membase = (unsigned char *)SCIF_BASE, |
| 316 | .mapbase = SCIF_BASE, |
| 317 | .type = SCIF_BASE_PORT, |
Marek Vasut | b811f9b | 2023-02-28 22:17:21 +0100 | [diff] [blame] | 318 | #ifdef CFG_SCIF_USE_EXT_CLK |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 319 | .clk_mode = EXT_CLK, |
| 320 | #endif |
| 321 | }; |
| 322 | |
| 323 | static void sh_serial_setbrg(void) |
| 324 | { |
| 325 | DECLARE_GLOBAL_DATA_PTR; |
| 326 | struct uart_port *port = &sh_sci; |
| 327 | |
| 328 | sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate); |
| 329 | } |
| 330 | |
| 331 | static int sh_serial_init(void) |
| 332 | { |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 333 | sh_serial_init_nodm(&sh_sci); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static void sh_serial_putc(const char c) |
| 339 | { |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 340 | sh_serial_putc_nondm(&sh_sci, c); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int sh_serial_tstc(void) |
| 344 | { |
| 345 | struct uart_port *port = &sh_sci; |
| 346 | |
| 347 | return sh_serial_tstc_generic(port); |
| 348 | } |
| 349 | |
| 350 | static int sh_serial_getc(void) |
| 351 | { |
| 352 | struct uart_port *port = &sh_sci; |
| 353 | int ch; |
| 354 | |
| 355 | while (1) { |
| 356 | ch = sh_serial_getc_generic(port); |
| 357 | if (ch != -EAGAIN) |
| 358 | break; |
| 359 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 360 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 361 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 362 | } |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 363 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 364 | static struct serial_device sh_serial_drv = { |
| 365 | .name = "sh_serial", |
| 366 | .start = sh_serial_init, |
| 367 | .stop = NULL, |
| 368 | .setbrg = sh_serial_setbrg, |
| 369 | .putc = sh_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 370 | .puts = default_serial_puts, |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 371 | .getc = sh_serial_getc, |
| 372 | .tstc = sh_serial_tstc, |
| 373 | }; |
| 374 | |
| 375 | void sh_serial_initialize(void) |
| 376 | { |
| 377 | serial_register(&sh_serial_drv); |
| 378 | } |
| 379 | |
| 380 | __weak struct serial_device *default_serial_console(void) |
| 381 | { |
| 382 | return &sh_serial_drv; |
| 383 | } |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 384 | #endif /* CONFIG_DM_SERIAL */ |
Marek Vasut | 98ae281 | 2023-02-28 22:17:22 +0100 | [diff] [blame] | 385 | |
| 386 | #ifdef CONFIG_DEBUG_UART_SCIF |
| 387 | #include <debug_uart.h> |
| 388 | |
| 389 | static struct uart_port debug_uart_sci = { |
| 390 | .membase = (unsigned char *)CONFIG_DEBUG_UART_BASE, |
| 391 | .mapbase = CONFIG_DEBUG_UART_BASE, |
| 392 | .type = SCIF_BASE_PORT, |
| 393 | #ifdef CFG_SCIF_USE_EXT_CLK |
| 394 | .clk_mode = EXT_CLK, |
| 395 | #endif |
| 396 | }; |
| 397 | |
| 398 | static inline void _debug_uart_init(void) |
| 399 | { |
| 400 | sh_serial_init_nodm(&debug_uart_sci); |
| 401 | } |
| 402 | |
| 403 | static inline void _debug_uart_putc(int c) |
| 404 | { |
| 405 | sh_serial_putc_nondm(&debug_uart_sci, c); |
| 406 | } |
| 407 | |
| 408 | DEBUG_UART_FUNCS |
| 409 | |
| 410 | #endif |