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