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