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