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 |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 62 | static void |
| 63 | sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate) |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 64 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 65 | if (port->clk_mode == EXT_CLK) { |
| 66 | unsigned short dl = DL_VALUE(baudrate, clk); |
| 67 | sci_out(port, DL, dl); |
Nobuhiro Iwamatsu | 1786175 | 2014-12-10 14:42:05 +0900 | [diff] [blame] | 68 | /* Need wait: Clock * 1/dl * 1/16 */ |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 69 | udelay((1000000 * dl * 16 / clk) * 1000 + 1); |
| 70 | } else { |
| 71 | sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk)); |
| 72 | } |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 75 | static void handle_error(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 76 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 77 | 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 | |
| 83 | static 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 Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 88 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 89 | sci_out(port, SCxTDR, c); |
| 90 | sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port)); |
| 91 | |
| 92 | return 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 95 | static int serial_rx_fifo_level(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 96 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 97 | return scif_rxfill(port); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 100 | static int sh_serial_tstc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 101 | { |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 102 | if (sci_in(port, SCxSR) & SCIF_ERRORS) { |
| 103 | handle_error(port); |
Tetsuyuki Kobayashi | 5d2b5a2 | 2012-11-19 21:37:38 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 107 | return serial_rx_fifo_level(port) ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 110 | static int serial_getc_check(struct uart_port *port) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 111 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 112 | unsigned short status; |
| 113 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 114 | status = sci_in(port, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 115 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 116 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 117 | handle_error(port); |
| 118 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 119 | handle_error(port); |
Marek Vasut | d88686b | 2020-05-09 22:30:05 +0200 | [diff] [blame] | 120 | status &= (SCIF_DR | SCxSR_RDxF(port)); |
| 121 | if (status) |
| 122 | return status; |
| 123 | return scif_rxfill(port); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 126 | static int sh_serial_getc_generic(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 127 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 128 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 129 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 130 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 131 | if (!serial_getc_check(port)) |
| 132 | return -EAGAIN; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 133 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 134 | ch = sci_in(port, SCxRDR); |
| 135 | status = sci_in(port, SCxSR); |
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 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 138 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame] | 139 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 140 | handle_error(port); |
| 141 | |
| 142 | if (sci_in(port, SCLSR) & SCxSR_ORER(port)) |
| 143 | handle_error(port); |
| 144 | |
| 145 | return ch; |
| 146 | } |
| 147 | |
Marek Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 148 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 149 | |
| 150 | static 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 | |
| 157 | static 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 | |
| 164 | static 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 | |
| 171 | static int sh_serial_setbrg(struct udevice *dev, int baudrate) |
| 172 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 173 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 174 | struct uart_port *priv = dev_get_priv(dev); |
| 175 | |
| 176 | sh_serial_setbrg_generic(priv, plat->clk, baudrate); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int sh_serial_probe(struct udevice *dev) |
| 182 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 183 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 184 | 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 | |
| 196 | static 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 Vasut | 0dfa991 | 2018-02-16 01:33:27 +0100 | [diff] [blame] | 203 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 204 | static const struct udevice_id sh_serial_id[] ={ |
Yoshinori Sato | e5669a3 | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 205 | {.compatible = "renesas,sci", .data = PORT_SCI}, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 206 | {.compatible = "renesas,scif", .data = PORT_SCIF}, |
| 207 | {.compatible = "renesas,scifa", .data = PORT_SCIFA}, |
| 208 | {} |
| 209 | }; |
| 210 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 211 | static int sh_serial_of_to_plat(struct udevice *dev) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 212 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 213 | struct sh_serial_plat *plat = dev_get_plat(dev); |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 214 | struct clk sh_serial_clk; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 215 | fdt_addr_t addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 216 | int ret; |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 217 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 218 | addr = dev_read_addr(dev); |
Marek Vasut | 48db776 | 2018-01-17 22:36:37 +0100 | [diff] [blame] | 219 | if (!addr) |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | |
| 222 | plat->base = addr; |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 223 | |
| 224 | ret = clk_get_by_name(dev, "fck", &sh_serial_clk); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 225 | if (!ret) { |
| 226 | ret = clk_enable(&sh_serial_clk); |
| 227 | if (!ret) |
| 228 | plat->clk = clk_get_rate(&sh_serial_clk); |
| 229 | } else { |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 230 | plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 231 | "clock", 1); |
Marek Vasut | fd6a4a7 | 2017-09-15 21:11:27 +0200 | [diff] [blame] | 232 | } |
Marek Vasut | 8fe2ffc | 2017-07-21 23:19:18 +0200 | [diff] [blame] | 233 | |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 234 | plat->type = dev_get_driver_data(dev); |
| 235 | return 0; |
| 236 | } |
| 237 | #endif |
| 238 | |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 239 | U_BOOT_DRIVER(serial_sh) = { |
| 240 | .name = "serial_sh", |
| 241 | .id = UCLASS_SERIAL, |
Yoshinori Sato | a6eed2b | 2016-04-18 16:51:04 +0900 | [diff] [blame] | 242 | .of_match = of_match_ptr(sh_serial_id), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 243 | .of_to_plat = of_match_ptr(sh_serial_of_to_plat), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 244 | .plat_auto = sizeof(struct sh_serial_plat), |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 245 | .probe = sh_serial_probe, |
| 246 | .ops = &sh_serial_ops, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 247 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 248 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 249 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 250 | .priv_auto = sizeof(struct uart_port), |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | #else /* CONFIG_DM_SERIAL */ |
| 254 | |
| 255 | #if defined(CONFIG_CONS_SCIF0) |
| 256 | # define SCIF_BASE SCIF0_BASE |
| 257 | #elif defined(CONFIG_CONS_SCIF1) |
| 258 | # define SCIF_BASE SCIF1_BASE |
| 259 | #elif defined(CONFIG_CONS_SCIF2) |
| 260 | # define SCIF_BASE SCIF2_BASE |
| 261 | #elif defined(CONFIG_CONS_SCIF3) |
| 262 | # define SCIF_BASE SCIF3_BASE |
| 263 | #elif defined(CONFIG_CONS_SCIF4) |
| 264 | # define SCIF_BASE SCIF4_BASE |
| 265 | #elif defined(CONFIG_CONS_SCIF5) |
| 266 | # define SCIF_BASE SCIF5_BASE |
| 267 | #elif defined(CONFIG_CONS_SCIF6) |
| 268 | # define SCIF_BASE SCIF6_BASE |
| 269 | #elif defined(CONFIG_CONS_SCIF7) |
| 270 | # define SCIF_BASE SCIF7_BASE |
Marek Vasut | 1d9756b | 2018-04-12 15:23:46 +0200 | [diff] [blame] | 271 | #elif defined(CONFIG_CONS_SCIFA0) |
| 272 | # define SCIF_BASE SCIFA0_BASE |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 273 | #else |
| 274 | # error "Default SCIF doesn't set....." |
| 275 | #endif |
| 276 | |
Tom Rini | 31b8b4d | 2022-12-04 10:13:43 -0500 | [diff] [blame] | 277 | #if defined(CFG_SCIF_A) |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 278 | #define SCIF_BASE_PORT PORT_SCIFA |
Yoshinori Sato | e5669a3 | 2016-04-18 16:51:05 +0900 | [diff] [blame] | 279 | #elif defined(CONFIG_SCI) |
| 280 | #define SCIF_BASE_PORT PORT_SCI |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 281 | #else |
| 282 | #define SCIF_BASE_PORT PORT_SCIF |
| 283 | #endif |
| 284 | |
| 285 | static struct uart_port sh_sci = { |
| 286 | .membase = (unsigned char *)SCIF_BASE, |
| 287 | .mapbase = SCIF_BASE, |
| 288 | .type = SCIF_BASE_PORT, |
| 289 | #ifdef CONFIG_SCIF_USE_EXT_CLK |
| 290 | .clk_mode = EXT_CLK, |
| 291 | #endif |
| 292 | }; |
| 293 | |
| 294 | static void sh_serial_setbrg(void) |
| 295 | { |
| 296 | DECLARE_GLOBAL_DATA_PTR; |
| 297 | struct uart_port *port = &sh_sci; |
| 298 | |
| 299 | sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate); |
| 300 | } |
| 301 | |
| 302 | static int sh_serial_init(void) |
| 303 | { |
| 304 | struct uart_port *port = &sh_sci; |
| 305 | |
| 306 | sh_serial_init_generic(port); |
| 307 | serial_setbrg(); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static void sh_serial_putc(const char c) |
| 313 | { |
| 314 | struct uart_port *port = &sh_sci; |
| 315 | |
| 316 | if (c == '\n') { |
| 317 | while (1) { |
| 318 | if (serial_raw_putc(port, '\r') != -EAGAIN) |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | while (1) { |
| 323 | if (serial_raw_putc(port, c) != -EAGAIN) |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static int sh_serial_tstc(void) |
| 329 | { |
| 330 | struct uart_port *port = &sh_sci; |
| 331 | |
| 332 | return sh_serial_tstc_generic(port); |
| 333 | } |
| 334 | |
| 335 | static int sh_serial_getc(void) |
| 336 | { |
| 337 | struct uart_port *port = &sh_sci; |
| 338 | int ch; |
| 339 | |
| 340 | while (1) { |
| 341 | ch = sh_serial_getc_generic(port); |
| 342 | if (ch != -EAGAIN) |
| 343 | break; |
| 344 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 345 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 346 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 347 | } |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 348 | |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 349 | static struct serial_device sh_serial_drv = { |
| 350 | .name = "sh_serial", |
| 351 | .start = sh_serial_init, |
| 352 | .stop = NULL, |
| 353 | .setbrg = sh_serial_setbrg, |
| 354 | .putc = sh_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 355 | .puts = default_serial_puts, |
Marek Vasut | 904d3d7 | 2012-09-14 22:40:08 +0200 | [diff] [blame] | 356 | .getc = sh_serial_getc, |
| 357 | .tstc = sh_serial_tstc, |
| 358 | }; |
| 359 | |
| 360 | void sh_serial_initialize(void) |
| 361 | { |
| 362 | serial_register(&sh_serial_drv); |
| 363 | } |
| 364 | |
| 365 | __weak struct serial_device *default_serial_console(void) |
| 366 | { |
| 367 | return &sh_serial_drv; |
| 368 | } |
Nobuhiro Iwamatsu | 6d02035 | 2015-02-12 13:48:04 +0900 | [diff] [blame] | 369 | #endif /* CONFIG_DM_SERIAL */ |