Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO) |
| 4 | * |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 5 | * Copyright (C) 2002 - 2011 Paul Mundt |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 6 | * Copyright (C) 2015 Glider bvba |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 7 | * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * based off of the old drivers/char/sh-sci.c by: |
| 10 | * |
| 11 | * Copyright (C) 1999, 2000 Niibe Yutaka |
| 12 | * Copyright (C) 2000 Sugioka Toshinobu |
| 13 | * Modified to support multiple serial ports. Stuart Menefy (May 2000). |
| 14 | * Modified to support SecureEdge. David McCullough (2002) |
| 15 | * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003). |
Magnus Damm | d89ddd1 | 2007-07-25 11:42:56 +0900 | [diff] [blame] | 16 | * Removed SH7300 support (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Paul Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 18 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 19 | #define SUPPORT_SYSRQ |
| 20 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #undef DEBUG |
| 23 | |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 24 | #include <linux/clk.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 25 | #include <linux/console.h> |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 26 | #include <linux/ctype.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 27 | #include <linux/cpufreq.h> |
| 28 | #include <linux/delay.h> |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 29 | #include <linux/dmaengine.h> |
Magnus Damm | 5beabc7 | 2011-08-02 09:42:54 +0000 | [diff] [blame] | 30 | #include <linux/dma-mapping.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 31 | #include <linux/err.h> |
| 32 | #include <linux/errno.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 33 | #include <linux/init.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/ioport.h> |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 36 | #include <linux/ktime.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 37 | #include <linux/major.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/mm.h> |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 40 | #include <linux/of.h> |
Geert Uytterhoeven | 6e605a0 | 2017-10-04 14:21:56 +0200 | [diff] [blame] | 41 | #include <linux/of_device.h> |
Laurent Pinchart | 8fb9631 | 2013-12-06 10:59:10 +0100 | [diff] [blame] | 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/pm_runtime.h> |
| 44 | #include <linux/scatterlist.h> |
| 45 | #include <linux/serial.h> |
| 46 | #include <linux/serial_sci.h> |
| 47 | #include <linux/sh_dma.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/string.h> |
| 50 | #include <linux/sysrq.h> |
| 51 | #include <linux/timer.h> |
| 52 | #include <linux/tty.h> |
| 53 | #include <linux/tty_flip.h> |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 54 | |
| 55 | #ifdef CONFIG_SUPERH |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 56 | #include <asm/sh_bios.h> |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 57 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 59 | #include "serial_mctrl_gpio.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include "sh-sci.h" |
| 61 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 62 | /* Offsets into the sci_port->irqs array */ |
| 63 | enum { |
| 64 | SCIx_ERI_IRQ, |
| 65 | SCIx_RXI_IRQ, |
| 66 | SCIx_TXI_IRQ, |
| 67 | SCIx_BRI_IRQ, |
| 68 | SCIx_NR_IRQS, |
| 69 | |
| 70 | SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */ |
| 71 | }; |
| 72 | |
| 73 | #define SCIx_IRQ_IS_MUXED(port) \ |
| 74 | ((port)->irqs[SCIx_ERI_IRQ] == \ |
| 75 | (port)->irqs[SCIx_RXI_IRQ]) || \ |
| 76 | ((port)->irqs[SCIx_ERI_IRQ] && \ |
| 77 | ((port)->irqs[SCIx_RXI_IRQ] < 0)) |
| 78 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 79 | enum SCI_CLKS { |
| 80 | SCI_FCK, /* Functional Clock */ |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 81 | SCI_SCK, /* Optional External Clock */ |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 82 | SCI_BRG_INT, /* Optional BRG Internal Clock Source */ |
| 83 | SCI_SCIF_CLK, /* Optional BRG External Clock Source */ |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 84 | SCI_NUM_CLKS |
| 85 | }; |
| 86 | |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 87 | /* Bit x set means sampling rate x + 1 is supported */ |
| 88 | #define SCI_SR(x) BIT((x) - 1) |
| 89 | #define SCI_SR_RANGE(x, y) GENMASK((y) - 1, (x) - 1) |
| 90 | |
Geert Uytterhoeven | 92a0574 | 2016-01-04 14:45:22 +0100 | [diff] [blame] | 91 | #define SCI_SR_SCIFAB SCI_SR(5) | SCI_SR(7) | SCI_SR(11) | \ |
| 92 | SCI_SR(13) | SCI_SR(16) | SCI_SR(17) | \ |
| 93 | SCI_SR(19) | SCI_SR(27) |
| 94 | |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 95 | #define min_sr(_port) ffs((_port)->sampling_rate_mask) |
| 96 | #define max_sr(_port) fls((_port)->sampling_rate_mask) |
| 97 | |
| 98 | /* Iterate over all supported sampling rates, from high to low */ |
| 99 | #define for_each_sr(_sr, _port) \ |
| 100 | for ((_sr) = max_sr(_port); (_sr) >= min_sr(_port); (_sr)--) \ |
| 101 | if ((_port)->sampling_rate_mask & SCI_SR((_sr))) |
| 102 | |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 103 | struct plat_sci_reg { |
| 104 | u8 offset, size; |
| 105 | }; |
| 106 | |
| 107 | struct sci_port_params { |
| 108 | const struct plat_sci_reg regs[SCIx_NR_REGS]; |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 109 | unsigned int fifosize; |
| 110 | unsigned int overrun_reg; |
| 111 | unsigned int overrun_mask; |
| 112 | unsigned int sampling_rate_mask; |
| 113 | unsigned int error_mask; |
| 114 | unsigned int error_clear; |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 117 | struct sci_port { |
| 118 | struct uart_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Paul Mundt | ce6738b | 2011-01-19 15:24:40 +0900 | [diff] [blame] | 120 | /* Platform configuration */ |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 121 | const struct sci_port_params *params; |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 122 | const struct plat_sci_port *cfg; |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 123 | unsigned int sampling_rate_mask; |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 124 | resource_size_t reg_size; |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 125 | struct mctrl_gpios *gpios; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 126 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 127 | /* Clocks */ |
| 128 | struct clk *clks[SCI_NUM_CLKS]; |
| 129 | unsigned long clk_rates[SCI_NUM_CLKS]; |
Paul Mundt | edad1f2 | 2009-11-25 16:23:35 +0900 | [diff] [blame] | 130 | |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 131 | int irqs[SCIx_NR_IRQS]; |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 132 | char *irqstr[SCIx_NR_IRQS]; |
| 133 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 134 | struct dma_chan *chan_tx; |
| 135 | struct dma_chan *chan_rx; |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 136 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 137 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 138 | dma_cookie_t cookie_tx; |
| 139 | dma_cookie_t cookie_rx[2]; |
| 140 | dma_cookie_t active_rx; |
Geert Uytterhoeven | 7990442 | 2015-08-21 20:02:42 +0200 | [diff] [blame] | 141 | dma_addr_t tx_dma_addr; |
| 142 | unsigned int tx_dma_len; |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 143 | struct scatterlist sg_rx[2]; |
Yoshihiro Shimoda | 7b39d90 | 2015-08-21 20:02:54 +0200 | [diff] [blame] | 144 | void *rx_buf[2]; |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 145 | size_t buf_len_rx; |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 146 | struct work_struct work_tx; |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 147 | struct hrtimer rx_timer; |
| 148 | unsigned int rx_timeout; /* microseconds */ |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 149 | #endif |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 150 | unsigned int rx_frame; |
Ulrich Hecht | 18e8cf1 | 2017-02-03 11:38:17 +0100 | [diff] [blame] | 151 | int rx_trigger; |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 152 | struct timer_list rx_fifo_timer; |
| 153 | int rx_fifo_timeout; |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 154 | u16 hscif_tot; |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 155 | |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 156 | bool has_rtscts; |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 157 | bool autorts; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 158 | }; |
| 159 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 160 | #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS |
| 161 | |
| 162 | static struct sci_port sci_ports[SCI_NPORTS]; |
Geert Uytterhoeven | 7678f4c | 2018-03-05 18:17:40 +0100 | [diff] [blame] | 163 | static unsigned long sci_ports_in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static struct uart_driver sci_uart_driver; |
| 165 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 166 | static inline struct sci_port * |
| 167 | to_sci_port(struct uart_port *uart) |
| 168 | { |
| 169 | return container_of(uart, struct sci_port, port); |
| 170 | } |
| 171 | |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 172 | static const struct sci_port_params sci_port_params[SCIx_NR_REGTYPES] = { |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 173 | /* |
| 174 | * Common SCI definitions, dependent on the port's regshift |
| 175 | * value. |
| 176 | */ |
| 177 | [SCIx_SCI_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 178 | .regs = { |
| 179 | [SCSMR] = { 0x00, 8 }, |
| 180 | [SCBRR] = { 0x01, 8 }, |
| 181 | [SCSCR] = { 0x02, 8 }, |
| 182 | [SCxTDR] = { 0x03, 8 }, |
| 183 | [SCxSR] = { 0x04, 8 }, |
| 184 | [SCxRDR] = { 0x05, 8 }, |
| 185 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 186 | .fifosize = 1, |
| 187 | .overrun_reg = SCxSR, |
| 188 | .overrun_mask = SCI_ORER, |
| 189 | .sampling_rate_mask = SCI_SR(32), |
| 190 | .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER, |
| 191 | .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 192 | }, |
| 193 | |
| 194 | /* |
Laurent Pinchart | a752ba1 | 2017-01-11 16:43:32 +0200 | [diff] [blame] | 195 | * Common definitions for legacy IrDA ports. |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 196 | */ |
| 197 | [SCIx_IRDA_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 198 | .regs = { |
| 199 | [SCSMR] = { 0x00, 8 }, |
| 200 | [SCBRR] = { 0x02, 8 }, |
| 201 | [SCSCR] = { 0x04, 8 }, |
| 202 | [SCxTDR] = { 0x06, 8 }, |
| 203 | [SCxSR] = { 0x08, 16 }, |
| 204 | [SCxRDR] = { 0x0a, 8 }, |
| 205 | [SCFCR] = { 0x0c, 8 }, |
| 206 | [SCFDR] = { 0x0e, 16 }, |
| 207 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 208 | .fifosize = 1, |
| 209 | .overrun_reg = SCxSR, |
| 210 | .overrun_mask = SCI_ORER, |
| 211 | .sampling_rate_mask = SCI_SR(32), |
| 212 | .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER, |
| 213 | .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 214 | }, |
| 215 | |
| 216 | /* |
| 217 | * Common SCIFA definitions. |
| 218 | */ |
| 219 | [SCIx_SCIFA_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 220 | .regs = { |
| 221 | [SCSMR] = { 0x00, 16 }, |
| 222 | [SCBRR] = { 0x04, 8 }, |
| 223 | [SCSCR] = { 0x08, 16 }, |
| 224 | [SCxTDR] = { 0x20, 8 }, |
| 225 | [SCxSR] = { 0x14, 16 }, |
| 226 | [SCxRDR] = { 0x24, 8 }, |
| 227 | [SCFCR] = { 0x18, 16 }, |
| 228 | [SCFDR] = { 0x1c, 16 }, |
| 229 | [SCPCR] = { 0x30, 16 }, |
| 230 | [SCPDR] = { 0x34, 16 }, |
| 231 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 232 | .fifosize = 64, |
| 233 | .overrun_reg = SCxSR, |
| 234 | .overrun_mask = SCIFA_ORER, |
| 235 | .sampling_rate_mask = SCI_SR_SCIFAB, |
| 236 | .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER, |
| 237 | .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 238 | }, |
| 239 | |
| 240 | /* |
| 241 | * Common SCIFB definitions. |
| 242 | */ |
| 243 | [SCIx_SCIFB_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 244 | .regs = { |
| 245 | [SCSMR] = { 0x00, 16 }, |
| 246 | [SCBRR] = { 0x04, 8 }, |
| 247 | [SCSCR] = { 0x08, 16 }, |
| 248 | [SCxTDR] = { 0x40, 8 }, |
| 249 | [SCxSR] = { 0x14, 16 }, |
| 250 | [SCxRDR] = { 0x60, 8 }, |
| 251 | [SCFCR] = { 0x18, 16 }, |
| 252 | [SCTFDR] = { 0x38, 16 }, |
| 253 | [SCRFDR] = { 0x3c, 16 }, |
| 254 | [SCPCR] = { 0x30, 16 }, |
| 255 | [SCPDR] = { 0x34, 16 }, |
| 256 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 257 | .fifosize = 256, |
| 258 | .overrun_reg = SCxSR, |
| 259 | .overrun_mask = SCIFA_ORER, |
| 260 | .sampling_rate_mask = SCI_SR_SCIFAB, |
| 261 | .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER, |
| 262 | .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 263 | }, |
| 264 | |
| 265 | /* |
Phil Edworthy | 3af1f8a | 2011-10-03 15:16:47 +0100 | [diff] [blame] | 266 | * Common SH-2(A) SCIF definitions for ports with FIFO data |
| 267 | * count registers. |
| 268 | */ |
| 269 | [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 270 | .regs = { |
| 271 | [SCSMR] = { 0x00, 16 }, |
| 272 | [SCBRR] = { 0x04, 8 }, |
| 273 | [SCSCR] = { 0x08, 16 }, |
| 274 | [SCxTDR] = { 0x0c, 8 }, |
| 275 | [SCxSR] = { 0x10, 16 }, |
| 276 | [SCxRDR] = { 0x14, 8 }, |
| 277 | [SCFCR] = { 0x18, 16 }, |
| 278 | [SCFDR] = { 0x1c, 16 }, |
| 279 | [SCSPTR] = { 0x20, 16 }, |
| 280 | [SCLSR] = { 0x24, 16 }, |
| 281 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 282 | .fifosize = 16, |
| 283 | .overrun_reg = SCLSR, |
| 284 | .overrun_mask = SCLSR_ORER, |
| 285 | .sampling_rate_mask = SCI_SR(32), |
| 286 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 287 | .error_clear = SCIF_ERROR_CLEAR, |
Phil Edworthy | 3af1f8a | 2011-10-03 15:16:47 +0100 | [diff] [blame] | 288 | }, |
| 289 | |
| 290 | /* |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 291 | * Common SH-3 SCIF definitions. |
| 292 | */ |
| 293 | [SCIx_SH3_SCIF_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 294 | .regs = { |
| 295 | [SCSMR] = { 0x00, 8 }, |
| 296 | [SCBRR] = { 0x02, 8 }, |
| 297 | [SCSCR] = { 0x04, 8 }, |
| 298 | [SCxTDR] = { 0x06, 8 }, |
| 299 | [SCxSR] = { 0x08, 16 }, |
| 300 | [SCxRDR] = { 0x0a, 8 }, |
| 301 | [SCFCR] = { 0x0c, 8 }, |
| 302 | [SCFDR] = { 0x0e, 16 }, |
| 303 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 304 | .fifosize = 16, |
| 305 | .overrun_reg = SCLSR, |
| 306 | .overrun_mask = SCLSR_ORER, |
| 307 | .sampling_rate_mask = SCI_SR(32), |
| 308 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 309 | .error_clear = SCIF_ERROR_CLEAR, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 310 | }, |
| 311 | |
| 312 | /* |
| 313 | * Common SH-4(A) SCIF(B) definitions. |
| 314 | */ |
| 315 | [SCIx_SH4_SCIF_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 316 | .regs = { |
| 317 | [SCSMR] = { 0x00, 16 }, |
| 318 | [SCBRR] = { 0x04, 8 }, |
| 319 | [SCSCR] = { 0x08, 16 }, |
| 320 | [SCxTDR] = { 0x0c, 8 }, |
| 321 | [SCxSR] = { 0x10, 16 }, |
| 322 | [SCxRDR] = { 0x14, 8 }, |
| 323 | [SCFCR] = { 0x18, 16 }, |
| 324 | [SCFDR] = { 0x1c, 16 }, |
| 325 | [SCSPTR] = { 0x20, 16 }, |
| 326 | [SCLSR] = { 0x24, 16 }, |
| 327 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 328 | .fifosize = 16, |
| 329 | .overrun_reg = SCLSR, |
| 330 | .overrun_mask = SCLSR_ORER, |
| 331 | .sampling_rate_mask = SCI_SR(32), |
| 332 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 333 | .error_clear = SCIF_ERROR_CLEAR, |
Geert Uytterhoeven | b8bbd6b | 2015-11-12 13:36:06 +0100 | [diff] [blame] | 334 | }, |
| 335 | |
| 336 | /* |
| 337 | * Common SCIF definitions for ports with a Baud Rate Generator for |
| 338 | * External Clock (BRG). |
| 339 | */ |
| 340 | [SCIx_SH4_SCIF_BRG_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 341 | .regs = { |
| 342 | [SCSMR] = { 0x00, 16 }, |
| 343 | [SCBRR] = { 0x04, 8 }, |
| 344 | [SCSCR] = { 0x08, 16 }, |
| 345 | [SCxTDR] = { 0x0c, 8 }, |
| 346 | [SCxSR] = { 0x10, 16 }, |
| 347 | [SCxRDR] = { 0x14, 8 }, |
| 348 | [SCFCR] = { 0x18, 16 }, |
| 349 | [SCFDR] = { 0x1c, 16 }, |
| 350 | [SCSPTR] = { 0x20, 16 }, |
| 351 | [SCLSR] = { 0x24, 16 }, |
| 352 | [SCDL] = { 0x30, 16 }, |
| 353 | [SCCKS] = { 0x34, 16 }, |
| 354 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 355 | .fifosize = 16, |
| 356 | .overrun_reg = SCLSR, |
| 357 | .overrun_mask = SCLSR_ORER, |
| 358 | .sampling_rate_mask = SCI_SR(32), |
| 359 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 360 | .error_clear = SCIF_ERROR_CLEAR, |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 361 | }, |
| 362 | |
| 363 | /* |
| 364 | * Common HSCIF definitions. |
| 365 | */ |
| 366 | [SCIx_HSCIF_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 367 | .regs = { |
| 368 | [SCSMR] = { 0x00, 16 }, |
| 369 | [SCBRR] = { 0x04, 8 }, |
| 370 | [SCSCR] = { 0x08, 16 }, |
| 371 | [SCxTDR] = { 0x0c, 8 }, |
| 372 | [SCxSR] = { 0x10, 16 }, |
| 373 | [SCxRDR] = { 0x14, 8 }, |
| 374 | [SCFCR] = { 0x18, 16 }, |
| 375 | [SCFDR] = { 0x1c, 16 }, |
| 376 | [SCSPTR] = { 0x20, 16 }, |
| 377 | [SCLSR] = { 0x24, 16 }, |
| 378 | [HSSRR] = { 0x40, 16 }, |
| 379 | [SCDL] = { 0x30, 16 }, |
| 380 | [SCCKS] = { 0x34, 16 }, |
Ulrich Hecht | 54e14ae | 2017-02-02 18:10:14 +0100 | [diff] [blame] | 381 | [HSRTRGR] = { 0x54, 16 }, |
| 382 | [HSTTRGR] = { 0x58, 16 }, |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 383 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 384 | .fifosize = 128, |
| 385 | .overrun_reg = SCLSR, |
| 386 | .overrun_mask = SCLSR_ORER, |
| 387 | .sampling_rate_mask = SCI_SR_RANGE(8, 32), |
| 388 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 389 | .error_clear = SCIF_ERROR_CLEAR, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 390 | }, |
| 391 | |
| 392 | /* |
| 393 | * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR |
| 394 | * register. |
| 395 | */ |
| 396 | [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 397 | .regs = { |
| 398 | [SCSMR] = { 0x00, 16 }, |
| 399 | [SCBRR] = { 0x04, 8 }, |
| 400 | [SCSCR] = { 0x08, 16 }, |
| 401 | [SCxTDR] = { 0x0c, 8 }, |
| 402 | [SCxSR] = { 0x10, 16 }, |
| 403 | [SCxRDR] = { 0x14, 8 }, |
| 404 | [SCFCR] = { 0x18, 16 }, |
| 405 | [SCFDR] = { 0x1c, 16 }, |
| 406 | [SCLSR] = { 0x24, 16 }, |
| 407 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 408 | .fifosize = 16, |
| 409 | .overrun_reg = SCLSR, |
| 410 | .overrun_mask = SCLSR_ORER, |
| 411 | .sampling_rate_mask = SCI_SR(32), |
| 412 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 413 | .error_clear = SCIF_ERROR_CLEAR, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 414 | }, |
| 415 | |
| 416 | /* |
| 417 | * Common SH-4(A) SCIF(B) definitions for ports with FIFO data |
| 418 | * count registers. |
| 419 | */ |
| 420 | [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 421 | .regs = { |
| 422 | [SCSMR] = { 0x00, 16 }, |
| 423 | [SCBRR] = { 0x04, 8 }, |
| 424 | [SCSCR] = { 0x08, 16 }, |
| 425 | [SCxTDR] = { 0x0c, 8 }, |
| 426 | [SCxSR] = { 0x10, 16 }, |
| 427 | [SCxRDR] = { 0x14, 8 }, |
| 428 | [SCFCR] = { 0x18, 16 }, |
| 429 | [SCFDR] = { 0x1c, 16 }, |
| 430 | [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */ |
| 431 | [SCRFDR] = { 0x20, 16 }, |
| 432 | [SCSPTR] = { 0x24, 16 }, |
| 433 | [SCLSR] = { 0x28, 16 }, |
| 434 | }, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 435 | .fifosize = 16, |
| 436 | .overrun_reg = SCLSR, |
| 437 | .overrun_mask = SCLSR_ORER, |
| 438 | .sampling_rate_mask = SCI_SR(32), |
| 439 | .error_mask = SCIF_DEFAULT_ERROR_MASK, |
| 440 | .error_clear = SCIF_ERROR_CLEAR, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 441 | }, |
| 442 | |
| 443 | /* |
| 444 | * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR |
| 445 | * registers. |
| 446 | */ |
| 447 | [SCIx_SH7705_SCIF_REGTYPE] = { |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 448 | .regs = { |
| 449 | [SCSMR] = { 0x00, 16 }, |
| 450 | [SCBRR] = { 0x04, 8 }, |
| 451 | [SCSCR] = { 0x08, 16 }, |
| 452 | [SCxTDR] = { 0x20, 8 }, |
| 453 | [SCxSR] = { 0x14, 16 }, |
| 454 | [SCxRDR] = { 0x24, 8 }, |
| 455 | [SCFCR] = { 0x18, 16 }, |
| 456 | [SCFDR] = { 0x1c, 16 }, |
| 457 | }, |
Ulrich Hecht | 18e8cf1 | 2017-02-03 11:38:17 +0100 | [diff] [blame] | 458 | .fifosize = 64, |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 459 | .overrun_reg = SCxSR, |
| 460 | .overrun_mask = SCIFA_ORER, |
| 461 | .sampling_rate_mask = SCI_SR(16), |
| 462 | .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER, |
| 463 | .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER, |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 464 | }, |
| 465 | }; |
| 466 | |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 467 | #define sci_getreg(up, offset) (&to_sci_port(up)->params->regs[offset]) |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 468 | |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 469 | /* |
| 470 | * The "offset" here is rather misleading, in that it refers to an enum |
| 471 | * value relative to the port mapping rather than the fixed offset |
| 472 | * itself, which needs to be manually retrieved from the platform's |
| 473 | * register map for the given port. |
| 474 | */ |
| 475 | static unsigned int sci_serial_in(struct uart_port *p, int offset) |
| 476 | { |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 477 | const struct plat_sci_reg *reg = sci_getreg(p, offset); |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 478 | |
| 479 | if (reg->size == 8) |
| 480 | return ioread8(p->membase + (reg->offset << p->regshift)); |
| 481 | else if (reg->size == 16) |
| 482 | return ioread16(p->membase + (reg->offset << p->regshift)); |
| 483 | else |
| 484 | WARN(1, "Invalid register access\n"); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static void sci_serial_out(struct uart_port *p, int offset, int value) |
| 490 | { |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 491 | const struct plat_sci_reg *reg = sci_getreg(p, offset); |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 492 | |
| 493 | if (reg->size == 8) |
| 494 | iowrite8(value, p->membase + (reg->offset << p->regshift)); |
| 495 | else if (reg->size == 16) |
| 496 | iowrite16(value, p->membase + (reg->offset << p->regshift)); |
| 497 | else |
| 498 | WARN(1, "Invalid register access\n"); |
| 499 | } |
| 500 | |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 501 | static void sci_port_enable(struct sci_port *sci_port) |
| 502 | { |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 503 | unsigned int i; |
| 504 | |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 505 | if (!sci_port->port.dev) |
| 506 | return; |
| 507 | |
| 508 | pm_runtime_get_sync(sci_port->port.dev); |
| 509 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 510 | for (i = 0; i < SCI_NUM_CLKS; i++) { |
| 511 | clk_prepare_enable(sci_port->clks[i]); |
| 512 | sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]); |
| 513 | } |
| 514 | sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK]; |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void sci_port_disable(struct sci_port *sci_port) |
| 518 | { |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 519 | unsigned int i; |
| 520 | |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 521 | if (!sci_port->port.dev) |
| 522 | return; |
| 523 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 524 | for (i = SCI_NUM_CLKS; i-- > 0; ) |
| 525 | clk_disable_unprepare(sci_port->clks[i]); |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 526 | |
| 527 | pm_runtime_put_sync(sci_port->port.dev); |
| 528 | } |
| 529 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 530 | static inline unsigned long port_rx_irq_mask(struct uart_port *port) |
| 531 | { |
| 532 | /* |
| 533 | * Not all ports (such as SCIFA) will support REIE. Rather than |
| 534 | * special-casing the port type, we check the port initialization |
| 535 | * IRQ enable mask to see whether the IRQ is desired at all. If |
| 536 | * it's unset, it's logically inferred that there's no point in |
| 537 | * testing for it. |
| 538 | */ |
| 539 | return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE); |
| 540 | } |
| 541 | |
| 542 | static void sci_start_tx(struct uart_port *port) |
| 543 | { |
| 544 | struct sci_port *s = to_sci_port(port); |
| 545 | unsigned short ctrl; |
| 546 | |
| 547 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
| 548 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 549 | u16 new, scr = serial_port_in(port, SCSCR); |
| 550 | if (s->chan_tx) |
| 551 | new = scr | SCSCR_TDRQE; |
| 552 | else |
| 553 | new = scr & ~SCSCR_TDRQE; |
| 554 | if (new != scr) |
| 555 | serial_port_out(port, SCSCR, new); |
| 556 | } |
| 557 | |
| 558 | if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) && |
| 559 | dma_submit_error(s->cookie_tx)) { |
| 560 | s->cookie_tx = 0; |
| 561 | schedule_work(&s->work_tx); |
| 562 | } |
| 563 | #endif |
| 564 | |
| 565 | if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 566 | /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */ |
| 567 | ctrl = serial_port_in(port, SCSCR); |
| 568 | serial_port_out(port, SCSCR, ctrl | SCSCR_TIE); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | static void sci_stop_tx(struct uart_port *port) |
| 573 | { |
| 574 | unsigned short ctrl; |
| 575 | |
| 576 | /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */ |
| 577 | ctrl = serial_port_in(port, SCSCR); |
| 578 | |
| 579 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 580 | ctrl &= ~SCSCR_TDRQE; |
| 581 | |
| 582 | ctrl &= ~SCSCR_TIE; |
| 583 | |
| 584 | serial_port_out(port, SCSCR, ctrl); |
| 585 | } |
| 586 | |
| 587 | static void sci_start_rx(struct uart_port *port) |
| 588 | { |
| 589 | unsigned short ctrl; |
| 590 | |
| 591 | ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port); |
| 592 | |
| 593 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 594 | ctrl &= ~SCSCR_RDRQE; |
| 595 | |
| 596 | serial_port_out(port, SCSCR, ctrl); |
| 597 | } |
| 598 | |
| 599 | static void sci_stop_rx(struct uart_port *port) |
| 600 | { |
| 601 | unsigned short ctrl; |
| 602 | |
| 603 | ctrl = serial_port_in(port, SCSCR); |
| 604 | |
| 605 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 606 | ctrl &= ~SCSCR_RDRQE; |
| 607 | |
| 608 | ctrl &= ~port_rx_irq_mask(port); |
| 609 | |
| 610 | serial_port_out(port, SCSCR, ctrl); |
| 611 | } |
| 612 | |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 613 | static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask) |
| 614 | { |
| 615 | if (port->type == PORT_SCI) { |
| 616 | /* Just store the mask */ |
| 617 | serial_port_out(port, SCxSR, mask); |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 618 | } else if (to_sci_port(port)->params->overrun_mask == SCIFA_ORER) { |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 619 | /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */ |
| 620 | /* Only clear the status bits we want to clear */ |
| 621 | serial_port_out(port, SCxSR, |
| 622 | serial_port_in(port, SCxSR) & mask); |
| 623 | } else { |
| 624 | /* Store the mask, clear parity/framing errors */ |
| 625 | serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC)); |
| 626 | } |
| 627 | } |
| 628 | |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 629 | #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \ |
| 630 | defined(CONFIG_SERIAL_SH_SCI_EARLYCON) |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 631 | |
| 632 | #ifdef CONFIG_CONSOLE_POLL |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 633 | static int sci_poll_get_char(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | unsigned short status; |
| 636 | int c; |
| 637 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 638 | do { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 639 | status = serial_port_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | if (status & SCxSR_ERRORS(port)) { |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 641 | sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | continue; |
| 643 | } |
Jason Wessel | 3f255eb | 2010-05-20 21:04:23 -0500 | [diff] [blame] | 644 | break; |
| 645 | } while (1); |
| 646 | |
| 647 | if (!(status & SCxSR_RDxF(port))) |
| 648 | return NO_POLL_CHAR; |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 649 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 650 | c = serial_port_in(port, SCxRDR); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 651 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 652 | /* Dummy read */ |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 653 | serial_port_in(port, SCxSR); |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 654 | sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | return c; |
| 657 | } |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 658 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 660 | static void sci_poll_put_char(struct uart_port *port, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | unsigned short status; |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | do { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 665 | status = serial_port_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } while (!(status & SCxSR_TDxE(port))); |
| 667 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 668 | serial_port_out(port, SCxTDR, c); |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 669 | sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 671 | #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE || |
| 672 | CONFIG_SERIAL_SH_SCI_EARLYCON */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 674 | static void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 675 | { |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 676 | struct sci_port *s = to_sci_port(port); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 677 | |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 678 | /* |
| 679 | * Use port-specific handler if provided. |
| 680 | */ |
| 681 | if (s->cfg->ops && s->cfg->ops->init_pins) { |
| 682 | s->cfg->ops->init_pins(port, cflag); |
| 683 | return; |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Geert Uytterhoeven | e9d7a45 | 2016-06-03 12:00:09 +0200 | [diff] [blame] | 686 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
Geert Uytterhoeven | cfa6eb2 | 2017-03-28 11:13:46 +0200 | [diff] [blame] | 687 | u16 data = serial_port_in(port, SCPDR); |
Geert Uytterhoeven | e9d7a45 | 2016-06-03 12:00:09 +0200 | [diff] [blame] | 688 | u16 ctrl = serial_port_in(port, SCPCR); |
| 689 | |
| 690 | /* Enable RXD and TXD pin functions */ |
| 691 | ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC); |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 692 | if (to_sci_port(port)->has_rtscts) { |
Geert Uytterhoeven | cfa6eb2 | 2017-03-28 11:13:46 +0200 | [diff] [blame] | 693 | /* RTS# is output, active low, unless autorts */ |
| 694 | if (!(port->mctrl & TIOCM_RTS)) { |
| 695 | ctrl |= SCPCR_RTSC; |
| 696 | data |= SCPDR_RTSD; |
| 697 | } else if (!s->autorts) { |
| 698 | ctrl |= SCPCR_RTSC; |
| 699 | data &= ~SCPDR_RTSD; |
| 700 | } else { |
| 701 | /* Enable RTS# pin function */ |
| 702 | ctrl &= ~SCPCR_RTSC; |
| 703 | } |
Geert Uytterhoeven | e9d7a45 | 2016-06-03 12:00:09 +0200 | [diff] [blame] | 704 | /* Enable CTS# pin function */ |
| 705 | ctrl &= ~SCPCR_CTSC; |
| 706 | } |
Geert Uytterhoeven | cfa6eb2 | 2017-03-28 11:13:46 +0200 | [diff] [blame] | 707 | serial_port_out(port, SCPDR, data); |
Geert Uytterhoeven | e9d7a45 | 2016-06-03 12:00:09 +0200 | [diff] [blame] | 708 | serial_port_out(port, SCPCR, ctrl); |
| 709 | } else if (sci_getreg(port, SCSPTR)->size) { |
Geert Uytterhoeven | d2b9775 | 2016-06-03 12:00:08 +0200 | [diff] [blame] | 710 | u16 status = serial_port_in(port, SCSPTR); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 711 | |
Geert Uytterhoeven | cfa6eb2 | 2017-03-28 11:13:46 +0200 | [diff] [blame] | 712 | /* RTS# is always output; and active low, unless autorts */ |
| 713 | status |= SCSPTR_RTSIO; |
| 714 | if (!(port->mctrl & TIOCM_RTS)) |
| 715 | status |= SCSPTR_RTSDT; |
| 716 | else if (!s->autorts) |
| 717 | status &= ~SCSPTR_RTSDT; |
Geert Uytterhoeven | d2b9775 | 2016-06-03 12:00:08 +0200 | [diff] [blame] | 718 | /* CTS# and SCK are inputs */ |
| 719 | status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO); |
| 720 | serial_port_out(port, SCSPTR, status); |
Paul Mundt | faf02f8 | 2011-12-02 17:44:50 +0900 | [diff] [blame] | 721 | } |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 724 | static int sci_txfill(struct uart_port *port) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 725 | { |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 726 | struct sci_port *s = to_sci_port(port); |
| 727 | unsigned int fifo_mask = (s->params->fifosize << 1) - 1; |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 728 | const struct plat_sci_reg *reg; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 729 | |
| 730 | reg = sci_getreg(port, SCTFDR); |
| 731 | if (reg->size) |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 732 | return serial_port_in(port, SCTFDR) & fifo_mask; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 733 | |
| 734 | reg = sci_getreg(port, SCFDR); |
| 735 | if (reg->size) |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 736 | return serial_port_in(port, SCFDR) >> 8; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 737 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 738 | return !(serial_port_in(port, SCxSR) & SCI_TDRE); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 739 | } |
| 740 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 741 | static int sci_txroom(struct uart_port *port) |
| 742 | { |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 743 | return port->fifosize - sci_txfill(port); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static int sci_rxfill(struct uart_port *port) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 747 | { |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 748 | struct sci_port *s = to_sci_port(port); |
| 749 | unsigned int fifo_mask = (s->params->fifosize << 1) - 1; |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 750 | const struct plat_sci_reg *reg; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 751 | |
| 752 | reg = sci_getreg(port, SCRFDR); |
| 753 | if (reg->size) |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 754 | return serial_port_in(port, SCRFDR) & fifo_mask; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 755 | |
| 756 | reg = sci_getreg(port, SCFDR); |
| 757 | if (reg->size) |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 758 | return serial_port_in(port, SCFDR) & fifo_mask; |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 759 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 760 | return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* ********************************************************************** * |
| 764 | * the interrupt related routines * |
| 765 | * ********************************************************************** */ |
| 766 | |
| 767 | static void sci_transmit_chars(struct uart_port *port) |
| 768 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 769 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | unsigned int stopped = uart_tx_stopped(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | unsigned short status; |
| 772 | unsigned short ctrl; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 773 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 775 | status = serial_port_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | if (!(status & SCxSR_TDxE(port))) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 777 | ctrl = serial_port_in(port, SCSCR); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 778 | if (uart_circ_empty(xmit)) |
Paul Mundt | 8e69861 | 2009-06-24 19:44:32 +0900 | [diff] [blame] | 779 | ctrl &= ~SCSCR_TIE; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 780 | else |
Paul Mundt | 8e69861 | 2009-06-24 19:44:32 +0900 | [diff] [blame] | 781 | ctrl |= SCSCR_TIE; |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 782 | serial_port_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 786 | count = sci_txroom(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
| 788 | do { |
| 789 | unsigned char c; |
| 790 | |
| 791 | if (port->x_char) { |
| 792 | c = port->x_char; |
| 793 | port->x_char = 0; |
| 794 | } else if (!uart_circ_empty(xmit) && !stopped) { |
| 795 | c = xmit->buf[xmit->tail]; |
| 796 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 797 | } else { |
| 798 | break; |
| 799 | } |
| 800 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 801 | serial_port_out(port, SCxTDR, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | port->icount.tx++; |
| 804 | } while (--count > 0); |
| 805 | |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 806 | sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 809 | uart_write_wakeup(port); |
| 810 | if (uart_circ_empty(xmit)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 811 | sci_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } else { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 813 | ctrl = serial_port_in(port, SCSCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 815 | if (port->type != PORT_SCI) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 816 | serial_port_in(port, SCxSR); /* Dummy read */ |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 817 | sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Paul Mundt | 8e69861 | 2009-06-24 19:44:32 +0900 | [diff] [blame] | 820 | ctrl |= SCSCR_TIE; |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 821 | serial_port_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | /* On SH3, SCIF may read end-of-break as a space->mark char */ |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 826 | #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Paul Mundt | 94c8b6d | 2011-01-20 23:26:18 +0900 | [diff] [blame] | 828 | static void sci_receive_chars(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | { |
Jiri Slaby | 227434f | 2013-01-03 15:53:01 +0100 | [diff] [blame] | 830 | struct tty_port *tport = &port->state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | int i, count, copied = 0; |
| 832 | unsigned short status; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 833 | unsigned char flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 835 | status = serial_port_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | if (!(status & SCxSR_RDxF(port))) |
| 837 | return; |
| 838 | |
| 839 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* Don't copy more bytes than there is room for in the buffer */ |
Jiri Slaby | 227434f | 2013-01-03 15:53:01 +0100 | [diff] [blame] | 841 | count = tty_buffer_request_room(tport, sci_rxfill(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | /* If for any reason we can't copy more data, we're done! */ |
| 844 | if (count == 0) |
| 845 | break; |
| 846 | |
| 847 | if (port->type == PORT_SCI) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 848 | char c = serial_port_in(port, SCxRDR); |
Laurent Pinchart | d5cb131 | 2017-01-11 16:43:38 +0200 | [diff] [blame] | 849 | if (uart_handle_sysrq_char(port, c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | count = 0; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 851 | else |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 852 | tty_insert_flip_char(tport, c, TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } else { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 854 | for (i = 0; i < count; i++) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 855 | char c = serial_port_in(port, SCxRDR); |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 856 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 857 | status = serial_port_in(port, SCxSR); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 858 | if (uart_handle_sysrq_char(port, c)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | count--; i--; |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | /* Store data and status */ |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 864 | if (status & SCxSR_FER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 865 | flag = TTY_FRAME; |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 866 | port->icount.frame++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 867 | dev_notice(port->dev, "frame error\n"); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 868 | } else if (status & SCxSR_PER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 869 | flag = TTY_PARITY; |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 870 | port->icount.parity++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 871 | dev_notice(port->dev, "parity error\n"); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 872 | } else |
| 873 | flag = TTY_NORMAL; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 874 | |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 875 | tty_insert_flip_char(tport, c, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 879 | serial_port_in(port, SCxSR); /* dummy read */ |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 880 | sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | copied += count; |
| 883 | port->icount.rx += count; |
| 884 | } |
| 885 | |
| 886 | if (copied) { |
| 887 | /* Tell the rest of the system the news. New characters! */ |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 888 | tty_flip_buffer_push(tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } else { |
Ulrich Hecht | 7842055 | 2018-02-15 13:02:27 +0100 | [diff] [blame] | 890 | /* TTY buffers full; read from RX reg to prevent lockup */ |
| 891 | serial_port_in(port, SCxRDR); |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 892 | serial_port_in(port, SCxSR); /* dummy read */ |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 893 | sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Paul Mundt | 94c8b6d | 2011-01-20 23:26:18 +0900 | [diff] [blame] | 897 | static int sci_handle_errors(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
| 899 | int copied = 0; |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 900 | unsigned short status = serial_port_in(port, SCxSR); |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 901 | struct tty_port *tport = &port->state->port; |
Paul Mundt | debf950 | 2011-06-08 18:19:37 +0900 | [diff] [blame] | 902 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Laurent Pinchart | 3ae988d | 2013-12-06 10:59:17 +0100 | [diff] [blame] | 904 | /* Handle overruns */ |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 905 | if (status & s->params->overrun_mask) { |
Laurent Pinchart | 3ae988d | 2013-12-06 10:59:17 +0100 | [diff] [blame] | 906 | port->icount.overrun++; |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 907 | |
Laurent Pinchart | 3ae988d | 2013-12-06 10:59:17 +0100 | [diff] [blame] | 908 | /* overrun error */ |
| 909 | if (tty_insert_flip_char(tport, 0, TTY_OVERRUN)) |
| 910 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 911 | |
Joe Perches | 9b971cd | 2014-03-11 10:10:46 -0700 | [diff] [blame] | 912 | dev_notice(port->dev, "overrun error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 915 | if (status & SCxSR_FER(port)) { |
Laurent Pinchart | d5cb131 | 2017-01-11 16:43:38 +0200 | [diff] [blame] | 916 | /* frame error */ |
| 917 | port->icount.frame++; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 918 | |
Laurent Pinchart | d5cb131 | 2017-01-11 16:43:38 +0200 | [diff] [blame] | 919 | if (tty_insert_flip_char(tport, 0, TTY_FRAME)) |
| 920 | copied++; |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 921 | |
Laurent Pinchart | d5cb131 | 2017-01-11 16:43:38 +0200 | [diff] [blame] | 922 | dev_notice(port->dev, "frame error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 925 | if (status & SCxSR_PER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | /* parity error */ |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 927 | port->icount.parity++; |
| 928 | |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 929 | if (tty_insert_flip_char(tport, 0, TTY_PARITY)) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 930 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 931 | |
Joe Perches | 9b971cd | 2014-03-11 10:10:46 -0700 | [diff] [blame] | 932 | dev_notice(port->dev, "parity error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 935 | if (copied) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 936 | tty_flip_buffer_push(tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | return copied; |
| 939 | } |
| 940 | |
Paul Mundt | 94c8b6d | 2011-01-20 23:26:18 +0900 | [diff] [blame] | 941 | static int sci_handle_fifo_overrun(struct uart_port *port) |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 942 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 943 | struct tty_port *tport = &port->state->port; |
Paul Mundt | debf950 | 2011-06-08 18:19:37 +0900 | [diff] [blame] | 944 | struct sci_port *s = to_sci_port(port); |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 945 | const struct plat_sci_reg *reg; |
Geert Uytterhoeven | 2e0842a | 2015-04-30 18:21:32 +0200 | [diff] [blame] | 946 | int copied = 0; |
Geert Uytterhoeven | 75c249f | 2015-04-30 18:21:31 +0200 | [diff] [blame] | 947 | u16 status; |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 948 | |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 949 | reg = sci_getreg(port, s->params->overrun_reg); |
Paul Mundt | 4b8c59a | 2011-06-14 17:53:34 +0900 | [diff] [blame] | 950 | if (!reg->size) |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 951 | return 0; |
| 952 | |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 953 | status = serial_port_in(port, s->params->overrun_reg); |
| 954 | if (status & s->params->overrun_mask) { |
| 955 | status &= ~s->params->overrun_mask; |
| 956 | serial_port_out(port, s->params->overrun_reg, status); |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 957 | |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 958 | port->icount.overrun++; |
| 959 | |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 960 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 961 | tty_flip_buffer_push(tport); |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 962 | |
Yoshihiro Kaneko | 51b31f1 | 2015-01-26 20:53:29 +0900 | [diff] [blame] | 963 | dev_dbg(port->dev, "overrun error\n"); |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 964 | copied++; |
| 965 | } |
| 966 | |
| 967 | return copied; |
| 968 | } |
| 969 | |
Paul Mundt | 94c8b6d | 2011-01-20 23:26:18 +0900 | [diff] [blame] | 970 | static int sci_handle_breaks(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | { |
| 972 | int copied = 0; |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 973 | unsigned short status = serial_port_in(port, SCxSR); |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 974 | struct tty_port *tport = &port->state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
Paul Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 976 | if (uart_handle_break(port)) |
| 977 | return 0; |
| 978 | |
Laurent Pinchart | d5cb131 | 2017-01-11 16:43:38 +0200 | [diff] [blame] | 979 | if (status & SCxSR_BRK(port)) { |
Paul Mundt | d97fbbe | 2011-11-24 19:15:06 +0900 | [diff] [blame] | 980 | port->icount.brk++; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | /* Notify of BREAK */ |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 983 | if (tty_insert_flip_char(tport, 0, TTY_BREAK)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 984 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 985 | |
| 986 | dev_dbg(port->dev, "BREAK detected\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 989 | if (copied) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 990 | tty_flip_buffer_push(tport); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 991 | |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 992 | copied += sci_handle_fifo_overrun(port); |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | return copied; |
| 995 | } |
| 996 | |
Ulrich Hecht | a380ed4 | 2017-02-02 18:10:16 +0100 | [diff] [blame] | 997 | static int scif_set_rtrg(struct uart_port *port, int rx_trig) |
| 998 | { |
| 999 | unsigned int bits; |
| 1000 | |
| 1001 | if (rx_trig < 1) |
| 1002 | rx_trig = 1; |
| 1003 | if (rx_trig >= port->fifosize) |
| 1004 | rx_trig = port->fifosize; |
| 1005 | |
| 1006 | /* HSCIF can be set to an arbitrary level. */ |
| 1007 | if (sci_getreg(port, HSRTRGR)->size) { |
| 1008 | serial_port_out(port, HSRTRGR, rx_trig); |
| 1009 | return rx_trig; |
| 1010 | } |
| 1011 | |
| 1012 | switch (port->type) { |
| 1013 | case PORT_SCIF: |
| 1014 | if (rx_trig < 4) { |
| 1015 | bits = 0; |
| 1016 | rx_trig = 1; |
| 1017 | } else if (rx_trig < 8) { |
| 1018 | bits = SCFCR_RTRG0; |
| 1019 | rx_trig = 4; |
| 1020 | } else if (rx_trig < 14) { |
| 1021 | bits = SCFCR_RTRG1; |
| 1022 | rx_trig = 8; |
| 1023 | } else { |
| 1024 | bits = SCFCR_RTRG0 | SCFCR_RTRG1; |
| 1025 | rx_trig = 14; |
| 1026 | } |
| 1027 | break; |
| 1028 | case PORT_SCIFA: |
| 1029 | case PORT_SCIFB: |
| 1030 | if (rx_trig < 16) { |
| 1031 | bits = 0; |
| 1032 | rx_trig = 1; |
| 1033 | } else if (rx_trig < 32) { |
| 1034 | bits = SCFCR_RTRG0; |
| 1035 | rx_trig = 16; |
| 1036 | } else if (rx_trig < 48) { |
| 1037 | bits = SCFCR_RTRG1; |
| 1038 | rx_trig = 32; |
| 1039 | } else { |
| 1040 | bits = SCFCR_RTRG0 | SCFCR_RTRG1; |
| 1041 | rx_trig = 48; |
| 1042 | } |
| 1043 | break; |
| 1044 | default: |
| 1045 | WARN(1, "unknown FIFO configuration"); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
| 1049 | serial_port_out(port, SCFCR, |
| 1050 | (serial_port_in(port, SCFCR) & |
| 1051 | ~(SCFCR_RTRG1 | SCFCR_RTRG0)) | bits); |
| 1052 | |
| 1053 | return rx_trig; |
| 1054 | } |
| 1055 | |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1056 | static int scif_rtrg_enabled(struct uart_port *port) |
| 1057 | { |
| 1058 | if (sci_getreg(port, HSRTRGR)->size) |
| 1059 | return serial_port_in(port, HSRTRGR) != 0; |
| 1060 | else |
| 1061 | return (serial_port_in(port, SCFCR) & |
| 1062 | (SCFCR_RTRG0 | SCFCR_RTRG1)) != 0; |
| 1063 | } |
| 1064 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1065 | static void rx_fifo_timer_fn(struct timer_list *t) |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1066 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1067 | struct sci_port *s = from_timer(s, t, rx_fifo_timer); |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1068 | struct uart_port *port = &s->port; |
| 1069 | |
| 1070 | dev_dbg(port->dev, "Rx timed out\n"); |
| 1071 | scif_set_rtrg(port, 1); |
| 1072 | } |
| 1073 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1074 | static ssize_t rx_trigger_show(struct device *dev, |
| 1075 | struct device_attribute *attr, |
| 1076 | char *buf) |
| 1077 | { |
| 1078 | struct uart_port *port = dev_get_drvdata(dev); |
| 1079 | struct sci_port *sci = to_sci_port(port); |
| 1080 | |
| 1081 | return sprintf(buf, "%d\n", sci->rx_trigger); |
| 1082 | } |
| 1083 | |
| 1084 | static ssize_t rx_trigger_store(struct device *dev, |
| 1085 | struct device_attribute *attr, |
| 1086 | const char *buf, |
| 1087 | size_t count) |
| 1088 | { |
| 1089 | struct uart_port *port = dev_get_drvdata(dev); |
| 1090 | struct sci_port *sci = to_sci_port(port); |
Dan Carpenter | 4ab3c51 | 2017-07-17 11:34:23 +0300 | [diff] [blame] | 1091 | int ret; |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1092 | long r; |
| 1093 | |
Dan Carpenter | 4ab3c51 | 2017-07-17 11:34:23 +0300 | [diff] [blame] | 1094 | ret = kstrtol(buf, 0, &r); |
| 1095 | if (ret) |
| 1096 | return ret; |
Ulrich Hecht | 90afa52 | 2017-02-08 18:31:14 +0100 | [diff] [blame] | 1097 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1098 | sci->rx_trigger = scif_set_rtrg(port, r); |
Ulrich Hecht | 90afa52 | 2017-02-08 18:31:14 +0100 | [diff] [blame] | 1099 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 1100 | scif_set_rtrg(port, 1); |
| 1101 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1102 | return count; |
| 1103 | } |
| 1104 | |
| 1105 | static DEVICE_ATTR(rx_fifo_trigger, 0644, rx_trigger_show, rx_trigger_store); |
| 1106 | |
| 1107 | static ssize_t rx_fifo_timeout_show(struct device *dev, |
| 1108 | struct device_attribute *attr, |
| 1109 | char *buf) |
| 1110 | { |
| 1111 | struct uart_port *port = dev_get_drvdata(dev); |
| 1112 | struct sci_port *sci = to_sci_port(port); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 1113 | int v; |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1114 | |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 1115 | if (port->type == PORT_HSCIF) |
| 1116 | v = sci->hscif_tot >> HSSCR_TOT_SHIFT; |
| 1117 | else |
| 1118 | v = sci->rx_fifo_timeout; |
| 1119 | |
| 1120 | return sprintf(buf, "%d\n", v); |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static ssize_t rx_fifo_timeout_store(struct device *dev, |
| 1124 | struct device_attribute *attr, |
| 1125 | const char *buf, |
| 1126 | size_t count) |
| 1127 | { |
| 1128 | struct uart_port *port = dev_get_drvdata(dev); |
| 1129 | struct sci_port *sci = to_sci_port(port); |
Dan Carpenter | 4ab3c51 | 2017-07-17 11:34:23 +0300 | [diff] [blame] | 1130 | int ret; |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1131 | long r; |
| 1132 | |
Dan Carpenter | 4ab3c51 | 2017-07-17 11:34:23 +0300 | [diff] [blame] | 1133 | ret = kstrtol(buf, 0, &r); |
| 1134 | if (ret) |
| 1135 | return ret; |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 1136 | |
| 1137 | if (port->type == PORT_HSCIF) { |
| 1138 | if (r < 0 || r > 3) |
| 1139 | return -EINVAL; |
| 1140 | sci->hscif_tot = r << HSSCR_TOT_SHIFT; |
| 1141 | } else { |
| 1142 | sci->rx_fifo_timeout = r; |
| 1143 | scif_set_rtrg(port, 1); |
| 1144 | if (r > 0) |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1145 | timer_setup(&sci->rx_fifo_timer, rx_fifo_timer_fn, 0); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1148 | return count; |
| 1149 | } |
| 1150 | |
Joe Perches | b6b996b | 2017-12-19 10:15:07 -0800 | [diff] [blame] | 1151 | static DEVICE_ATTR_RW(rx_fifo_timeout); |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 1152 | |
| 1153 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1154 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
| 1155 | static void sci_dma_tx_complete(void *arg) |
| 1156 | { |
| 1157 | struct sci_port *s = arg; |
| 1158 | struct uart_port *port = &s->port; |
| 1159 | struct circ_buf *xmit = &port->state->xmit; |
| 1160 | unsigned long flags; |
| 1161 | |
| 1162 | dev_dbg(port->dev, "%s(%d)\n", __func__, port->line); |
| 1163 | |
| 1164 | spin_lock_irqsave(&port->lock, flags); |
| 1165 | |
| 1166 | xmit->tail += s->tx_dma_len; |
| 1167 | xmit->tail &= UART_XMIT_SIZE - 1; |
| 1168 | |
| 1169 | port->icount.tx += s->tx_dma_len; |
| 1170 | |
| 1171 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 1172 | uart_write_wakeup(port); |
| 1173 | |
| 1174 | if (!uart_circ_empty(xmit)) { |
| 1175 | s->cookie_tx = 0; |
| 1176 | schedule_work(&s->work_tx); |
| 1177 | } else { |
| 1178 | s->cookie_tx = -EINVAL; |
| 1179 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 1180 | u16 ctrl = serial_port_in(port, SCSCR); |
| 1181 | serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | spin_unlock_irqrestore(&port->lock, flags); |
| 1186 | } |
| 1187 | |
| 1188 | /* Locking: called with port lock held */ |
| 1189 | static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count) |
| 1190 | { |
| 1191 | struct uart_port *port = &s->port; |
| 1192 | struct tty_port *tport = &port->state->port; |
| 1193 | int copied; |
| 1194 | |
| 1195 | copied = tty_insert_flip_string(tport, buf, count); |
Takatoshi Akiyama | 6fc5a52 | 2016-11-07 16:56:50 +0100 | [diff] [blame] | 1196 | if (copied < count) |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1197 | port->icount.buf_overrun++; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1198 | |
| 1199 | port->icount.rx += copied; |
| 1200 | |
| 1201 | return copied; |
| 1202 | } |
| 1203 | |
| 1204 | static int sci_dma_rx_find_active(struct sci_port *s) |
| 1205 | { |
| 1206 | unsigned int i; |
| 1207 | |
| 1208 | for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++) |
| 1209 | if (s->active_rx == s->cookie_rx[i]) |
| 1210 | return i; |
| 1211 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | static void sci_rx_dma_release(struct sci_port *s, bool enable_pio) |
| 1216 | { |
| 1217 | struct dma_chan *chan = s->chan_rx; |
| 1218 | struct uart_port *port = &s->port; |
| 1219 | unsigned long flags; |
| 1220 | |
| 1221 | spin_lock_irqsave(&port->lock, flags); |
| 1222 | s->chan_rx = NULL; |
| 1223 | s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL; |
| 1224 | spin_unlock_irqrestore(&port->lock, flags); |
| 1225 | dmaengine_terminate_all(chan); |
| 1226 | dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0], |
| 1227 | sg_dma_address(&s->sg_rx[0])); |
| 1228 | dma_release_channel(chan); |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 1229 | if (enable_pio) { |
| 1230 | spin_lock_irqsave(&port->lock, flags); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1231 | sci_start_rx(port); |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 1232 | spin_unlock_irqrestore(&port->lock, flags); |
| 1233 | } |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1236 | static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec) |
| 1237 | { |
| 1238 | long sec = usec / 1000000; |
| 1239 | long nsec = (usec % 1000000) * 1000; |
| 1240 | ktime_t t = ktime_set(sec, nsec); |
| 1241 | |
| 1242 | hrtimer_start(hrt, t, HRTIMER_MODE_REL); |
| 1243 | } |
| 1244 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1245 | static void sci_dma_rx_complete(void *arg) |
| 1246 | { |
| 1247 | struct sci_port *s = arg; |
Muhammad Hamza Farooq | 1d3db60 | 2015-09-18 13:08:30 +0200 | [diff] [blame] | 1248 | struct dma_chan *chan = s->chan_rx; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1249 | struct uart_port *port = &s->port; |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1250 | struct dma_async_tx_descriptor *desc; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1251 | unsigned long flags; |
| 1252 | int active, count = 0; |
| 1253 | |
| 1254 | dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line, |
| 1255 | s->active_rx); |
| 1256 | |
| 1257 | spin_lock_irqsave(&port->lock, flags); |
| 1258 | |
| 1259 | active = sci_dma_rx_find_active(s); |
| 1260 | if (active >= 0) |
| 1261 | count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx); |
| 1262 | |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1263 | start_hrtimer_us(&s->rx_timer, s->rx_timeout); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1264 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1265 | if (count) |
| 1266 | tty_flip_buffer_push(&port->state->port); |
| 1267 | |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1268 | desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1, |
| 1269 | DMA_DEV_TO_MEM, |
| 1270 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1271 | if (!desc) |
| 1272 | goto fail; |
| 1273 | |
| 1274 | desc->callback = sci_dma_rx_complete; |
| 1275 | desc->callback_param = s; |
| 1276 | s->cookie_rx[active] = dmaengine_submit(desc); |
| 1277 | if (dma_submit_error(s->cookie_rx[active])) |
| 1278 | goto fail; |
| 1279 | |
| 1280 | s->active_rx = s->cookie_rx[!active]; |
| 1281 | |
Muhammad Hamza Farooq | 1d3db60 | 2015-09-18 13:08:30 +0200 | [diff] [blame] | 1282 | dma_async_issue_pending(chan); |
| 1283 | |
Takatoshi Akiyama | 6fc5a52 | 2016-11-07 16:56:50 +0100 | [diff] [blame] | 1284 | spin_unlock_irqrestore(&port->lock, flags); |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1285 | dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n", |
| 1286 | __func__, s->cookie_rx[active], active, s->active_rx); |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1287 | return; |
| 1288 | |
| 1289 | fail: |
| 1290 | spin_unlock_irqrestore(&port->lock, flags); |
| 1291 | dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n"); |
| 1292 | sci_rx_dma_release(s, true); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | static void sci_tx_dma_release(struct sci_port *s, bool enable_pio) |
| 1296 | { |
| 1297 | struct dma_chan *chan = s->chan_tx; |
| 1298 | struct uart_port *port = &s->port; |
| 1299 | unsigned long flags; |
| 1300 | |
| 1301 | spin_lock_irqsave(&port->lock, flags); |
| 1302 | s->chan_tx = NULL; |
| 1303 | s->cookie_tx = -EINVAL; |
| 1304 | spin_unlock_irqrestore(&port->lock, flags); |
| 1305 | dmaengine_terminate_all(chan); |
| 1306 | dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE, |
| 1307 | DMA_TO_DEVICE); |
| 1308 | dma_release_channel(chan); |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 1309 | if (enable_pio) { |
| 1310 | spin_lock_irqsave(&port->lock, flags); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1311 | sci_start_tx(port); |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 1312 | spin_unlock_irqrestore(&port->lock, flags); |
| 1313 | } |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static void sci_submit_rx(struct sci_port *s) |
| 1317 | { |
| 1318 | struct dma_chan *chan = s->chan_rx; |
| 1319 | int i; |
| 1320 | |
| 1321 | for (i = 0; i < 2; i++) { |
| 1322 | struct scatterlist *sg = &s->sg_rx[i]; |
| 1323 | struct dma_async_tx_descriptor *desc; |
| 1324 | |
| 1325 | desc = dmaengine_prep_slave_sg(chan, |
| 1326 | sg, 1, DMA_DEV_TO_MEM, |
| 1327 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1328 | if (!desc) |
| 1329 | goto fail; |
| 1330 | |
| 1331 | desc->callback = sci_dma_rx_complete; |
| 1332 | desc->callback_param = s; |
| 1333 | s->cookie_rx[i] = dmaengine_submit(desc); |
| 1334 | if (dma_submit_error(s->cookie_rx[i])) |
| 1335 | goto fail; |
| 1336 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | s->active_rx = s->cookie_rx[0]; |
| 1340 | |
| 1341 | dma_async_issue_pending(chan); |
| 1342 | return; |
| 1343 | |
| 1344 | fail: |
| 1345 | if (i) |
| 1346 | dmaengine_terminate_all(chan); |
| 1347 | for (i = 0; i < 2; i++) |
| 1348 | s->cookie_rx[i] = -EINVAL; |
| 1349 | s->active_rx = -EINVAL; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1350 | sci_rx_dma_release(s, true); |
| 1351 | } |
| 1352 | |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1353 | static void work_fn_tx(struct work_struct *work) |
| 1354 | { |
| 1355 | struct sci_port *s = container_of(work, struct sci_port, work_tx); |
| 1356 | struct dma_async_tx_descriptor *desc; |
| 1357 | struct dma_chan *chan = s->chan_tx; |
| 1358 | struct uart_port *port = &s->port; |
| 1359 | struct circ_buf *xmit = &port->state->xmit; |
| 1360 | dma_addr_t buf; |
| 1361 | |
| 1362 | /* |
| 1363 | * DMA is idle now. |
| 1364 | * Port xmit buffer is already mapped, and it is one page... Just adjust |
| 1365 | * offsets and lengths. Since it is a circular buffer, we have to |
| 1366 | * transmit till the end, and then the rest. Take the port lock to get a |
| 1367 | * consistent xmit buffer state. |
| 1368 | */ |
| 1369 | spin_lock_irq(&port->lock); |
| 1370 | buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1)); |
| 1371 | s->tx_dma_len = min_t(unsigned int, |
| 1372 | CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE), |
| 1373 | CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE)); |
| 1374 | spin_unlock_irq(&port->lock); |
| 1375 | |
| 1376 | desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len, |
| 1377 | DMA_MEM_TO_DEV, |
| 1378 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1379 | if (!desc) { |
| 1380 | dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n"); |
| 1381 | /* switch to PIO */ |
| 1382 | sci_tx_dma_release(s, true); |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len, |
| 1387 | DMA_TO_DEVICE); |
| 1388 | |
| 1389 | spin_lock_irq(&port->lock); |
| 1390 | desc->callback = sci_dma_tx_complete; |
| 1391 | desc->callback_param = s; |
| 1392 | spin_unlock_irq(&port->lock); |
| 1393 | s->cookie_tx = dmaengine_submit(desc); |
| 1394 | if (dma_submit_error(s->cookie_tx)) { |
| 1395 | dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n"); |
| 1396 | /* switch to PIO */ |
| 1397 | sci_tx_dma_release(s, true); |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| 1401 | dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", |
| 1402 | __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx); |
| 1403 | |
| 1404 | dma_async_issue_pending(chan); |
| 1405 | } |
| 1406 | |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1407 | static enum hrtimer_restart rx_timer_fn(struct hrtimer *t) |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1408 | { |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1409 | struct sci_port *s = container_of(t, struct sci_port, rx_timer); |
Muhammad Hamza Farooq | e7327c0 | 2015-09-18 13:08:32 +0200 | [diff] [blame] | 1410 | struct dma_chan *chan = s->chan_rx; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1411 | struct uart_port *port = &s->port; |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1412 | struct dma_tx_state state; |
| 1413 | enum dma_status status; |
| 1414 | unsigned long flags; |
| 1415 | unsigned int read; |
| 1416 | int active, count; |
| 1417 | u16 scr; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1418 | |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1419 | dev_dbg(port->dev, "DMA Rx timed out\n"); |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1420 | |
Takatoshi Akiyama | 6fc5a52 | 2016-11-07 16:56:50 +0100 | [diff] [blame] | 1421 | spin_lock_irqsave(&port->lock, flags); |
| 1422 | |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1423 | active = sci_dma_rx_find_active(s); |
| 1424 | if (active < 0) { |
| 1425 | spin_unlock_irqrestore(&port->lock, flags); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1426 | return HRTIMER_NORESTART; |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state); |
Muhammad Hamza Farooq | 3b96304 | 2015-09-18 13:08:31 +0200 | [diff] [blame] | 1430 | if (status == DMA_COMPLETE) { |
Takatoshi Akiyama | 6fc5a52 | 2016-11-07 16:56:50 +0100 | [diff] [blame] | 1431 | spin_unlock_irqrestore(&port->lock, flags); |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1432 | dev_dbg(port->dev, "Cookie %d #%d has already completed\n", |
| 1433 | s->active_rx, active); |
Muhammad Hamza Farooq | 3b96304 | 2015-09-18 13:08:31 +0200 | [diff] [blame] | 1434 | |
| 1435 | /* Let packet complete handler take care of the packet */ |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1436 | return HRTIMER_NORESTART; |
Muhammad Hamza Farooq | 3b96304 | 2015-09-18 13:08:31 +0200 | [diff] [blame] | 1437 | } |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1438 | |
Muhammad Hamza Farooq | e7327c0 | 2015-09-18 13:08:32 +0200 | [diff] [blame] | 1439 | dmaengine_pause(chan); |
| 1440 | |
| 1441 | /* |
| 1442 | * sometimes DMA transfer doesn't stop even if it is stopped and |
| 1443 | * data keeps on coming until transaction is complete so check |
| 1444 | * for DMA_COMPLETE again |
| 1445 | * Let packet complete handler take care of the packet |
| 1446 | */ |
| 1447 | status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state); |
| 1448 | if (status == DMA_COMPLETE) { |
| 1449 | spin_unlock_irqrestore(&port->lock, flags); |
| 1450 | dev_dbg(port->dev, "Transaction complete after DMA engine was stopped"); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1451 | return HRTIMER_NORESTART; |
Muhammad Hamza Farooq | e7327c0 | 2015-09-18 13:08:32 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1454 | /* Handle incomplete DMA receive */ |
| 1455 | dmaengine_terminate_all(s->chan_rx); |
| 1456 | read = sg_dma_len(&s->sg_rx[active]) - state.residue; |
Geert Uytterhoeven | 67f462b0 | 2015-09-18 13:08:25 +0200 | [diff] [blame] | 1457 | |
| 1458 | if (read) { |
| 1459 | count = sci_dma_rx_push(s, s->rx_buf[active], read); |
| 1460 | if (count) |
| 1461 | tty_flip_buffer_push(&port->state->port); |
| 1462 | } |
| 1463 | |
Geert Uytterhoeven | 756981b | 2015-09-18 13:08:26 +0200 | [diff] [blame] | 1464 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 1465 | sci_submit_rx(s); |
Muhammad Hamza Farooq | 371cfed | 2015-09-18 13:08:29 +0200 | [diff] [blame] | 1466 | |
| 1467 | /* Direct new serial port interrupts back to CPU */ |
| 1468 | scr = serial_port_in(port, SCSCR); |
| 1469 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 1470 | scr &= ~SCSCR_RDRQE; |
| 1471 | enable_irq(s->irqs[SCIx_RXI_IRQ]); |
| 1472 | } |
| 1473 | serial_port_out(port, SCSCR, scr | SCSCR_RIE); |
| 1474 | |
| 1475 | spin_unlock_irqrestore(&port->lock, flags); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1476 | |
| 1477 | return HRTIMER_NORESTART; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1480 | static struct dma_chan *sci_request_dma_chan(struct uart_port *port, |
Laurent Pinchart | 219fb0c | 2017-01-11 16:43:37 +0200 | [diff] [blame] | 1481 | enum dma_transfer_direction dir) |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1482 | { |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1483 | struct dma_chan *chan; |
| 1484 | struct dma_slave_config cfg; |
| 1485 | int ret; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1486 | |
Laurent Pinchart | 219fb0c | 2017-01-11 16:43:37 +0200 | [diff] [blame] | 1487 | chan = dma_request_slave_channel(port->dev, |
| 1488 | dir == DMA_MEM_TO_DEV ? "tx" : "rx"); |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1489 | if (!chan) { |
Geert Uytterhoeven | 9b7becf | 2017-05-22 15:15:02 +0200 | [diff] [blame] | 1490 | dev_warn(port->dev, "dma_request_slave_channel failed\n"); |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1491 | return NULL; |
| 1492 | } |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1493 | |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1494 | memset(&cfg, 0, sizeof(cfg)); |
| 1495 | cfg.direction = dir; |
| 1496 | if (dir == DMA_MEM_TO_DEV) { |
| 1497 | cfg.dst_addr = port->mapbase + |
| 1498 | (sci_getreg(port, SCxTDR)->offset << port->regshift); |
| 1499 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1500 | } else { |
| 1501 | cfg.src_addr = port->mapbase + |
| 1502 | (sci_getreg(port, SCxRDR)->offset << port->regshift); |
| 1503 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1504 | } |
| 1505 | |
| 1506 | ret = dmaengine_slave_config(chan, &cfg); |
| 1507 | if (ret) { |
| 1508 | dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret); |
| 1509 | dma_release_channel(chan); |
| 1510 | return NULL; |
| 1511 | } |
| 1512 | |
| 1513 | return chan; |
| 1514 | } |
| 1515 | |
| 1516 | static void sci_request_dma(struct uart_port *port) |
| 1517 | { |
| 1518 | struct sci_port *s = to_sci_port(port); |
| 1519 | struct dma_chan *chan; |
| 1520 | |
| 1521 | dev_dbg(port->dev, "%s: port %d\n", __func__, port->line); |
| 1522 | |
Laurent Pinchart | 219fb0c | 2017-01-11 16:43:37 +0200 | [diff] [blame] | 1523 | if (!port->dev->of_node) |
Geert Uytterhoeven | ff44112 | 2015-09-18 13:08:33 +0200 | [diff] [blame] | 1524 | return; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1525 | |
| 1526 | s->cookie_tx = -EINVAL; |
Andy Lowe | 7464779 | 2017-09-22 20:29:30 +0200 | [diff] [blame] | 1527 | |
| 1528 | /* |
| 1529 | * Don't request a dma channel if no channel was specified |
| 1530 | * in the device tree. |
| 1531 | */ |
| 1532 | if (!of_find_property(port->dev->of_node, "dmas", NULL)) |
| 1533 | return; |
| 1534 | |
Laurent Pinchart | 219fb0c | 2017-01-11 16:43:37 +0200 | [diff] [blame] | 1535 | chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1536 | dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan); |
| 1537 | if (chan) { |
| 1538 | s->chan_tx = chan; |
| 1539 | /* UART circular tx buffer is an aligned page. */ |
| 1540 | s->tx_dma_addr = dma_map_single(chan->device->dev, |
| 1541 | port->state->xmit.buf, |
| 1542 | UART_XMIT_SIZE, |
| 1543 | DMA_TO_DEVICE); |
| 1544 | if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) { |
| 1545 | dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n"); |
| 1546 | dma_release_channel(chan); |
| 1547 | s->chan_tx = NULL; |
| 1548 | } else { |
| 1549 | dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n", |
| 1550 | __func__, UART_XMIT_SIZE, |
| 1551 | port->state->xmit.buf, &s->tx_dma_addr); |
| 1552 | } |
| 1553 | |
| 1554 | INIT_WORK(&s->work_tx, work_fn_tx); |
| 1555 | } |
| 1556 | |
Laurent Pinchart | 219fb0c | 2017-01-11 16:43:37 +0200 | [diff] [blame] | 1557 | chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1558 | dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan); |
| 1559 | if (chan) { |
| 1560 | unsigned int i; |
| 1561 | dma_addr_t dma; |
| 1562 | void *buf; |
| 1563 | |
| 1564 | s->chan_rx = chan; |
| 1565 | |
| 1566 | s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize); |
| 1567 | buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2, |
| 1568 | &dma, GFP_KERNEL); |
| 1569 | if (!buf) { |
| 1570 | dev_warn(port->dev, |
| 1571 | "Failed to allocate Rx dma buffer, using PIO\n"); |
| 1572 | dma_release_channel(chan); |
| 1573 | s->chan_rx = NULL; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1574 | return; |
| 1575 | } |
| 1576 | |
| 1577 | for (i = 0; i < 2; i++) { |
| 1578 | struct scatterlist *sg = &s->sg_rx[i]; |
| 1579 | |
| 1580 | sg_init_table(sg, 1); |
| 1581 | s->rx_buf[i] = buf; |
| 1582 | sg_dma_address(sg) = dma; |
Yoshihiro Shimoda | d09959e | 2015-12-04 15:21:19 +0100 | [diff] [blame] | 1583 | sg_dma_len(sg) = s->buf_len_rx; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1584 | |
| 1585 | buf += s->buf_len_rx; |
| 1586 | dma += s->buf_len_rx; |
| 1587 | } |
| 1588 | |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1589 | hrtimer_init(&s->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1590 | s->rx_timer.function = rx_timer_fn; |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1591 | |
Geert Uytterhoeven | 756981b | 2015-09-18 13:08:26 +0200 | [diff] [blame] | 1592 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 1593 | sci_submit_rx(s); |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | static void sci_free_dma(struct uart_port *port) |
| 1598 | { |
| 1599 | struct sci_port *s = to_sci_port(port); |
| 1600 | |
| 1601 | if (s->chan_tx) |
| 1602 | sci_tx_dma_release(s, false); |
| 1603 | if (s->chan_rx) |
| 1604 | sci_rx_dma_release(s, false); |
| 1605 | } |
Geert Uytterhoeven | 1cf4a7e | 2017-04-25 20:15:35 +0200 | [diff] [blame] | 1606 | |
| 1607 | static void sci_flush_buffer(struct uart_port *port) |
| 1608 | { |
| 1609 | /* |
| 1610 | * In uart_flush_buffer(), the xmit circular buffer has just been |
| 1611 | * cleared, so we have to reset tx_dma_len accordingly. |
| 1612 | */ |
| 1613 | to_sci_port(port)->tx_dma_len = 0; |
| 1614 | } |
| 1615 | #else /* !CONFIG_SERIAL_SH_SCI_DMA */ |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1616 | static inline void sci_request_dma(struct uart_port *port) |
| 1617 | { |
| 1618 | } |
| 1619 | |
| 1620 | static inline void sci_free_dma(struct uart_port *port) |
| 1621 | { |
| 1622 | } |
Geert Uytterhoeven | 1cf4a7e | 2017-04-25 20:15:35 +0200 | [diff] [blame] | 1623 | |
| 1624 | #define sci_flush_buffer NULL |
| 1625 | #endif /* !CONFIG_SERIAL_SH_SCI_DMA */ |
Geert Uytterhoeven | e1910fc | 2015-09-18 13:08:24 +0200 | [diff] [blame] | 1626 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1627 | static irqreturn_t sci_rx_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1629 | struct uart_port *port = ptr; |
| 1630 | struct sci_port *s = to_sci_port(port); |
| 1631 | |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1632 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1633 | if (s->chan_rx) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 1634 | u16 scr = serial_port_in(port, SCSCR); |
| 1635 | u16 ssr = serial_port_in(port, SCxSR); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1636 | |
| 1637 | /* Disable future Rx interrupts */ |
Guennadi Liakhovetski | d1d4b10 | 2010-05-23 16:39:09 +0000 | [diff] [blame] | 1638 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 1639 | disable_irq_nosync(irq); |
Geert Uytterhoeven | 26de4f1 | 2014-03-11 11:11:19 +0100 | [diff] [blame] | 1640 | scr |= SCSCR_RDRQE; |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 1641 | } else { |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1642 | scr &= ~SCSCR_RIE; |
Geert Uytterhoeven | 756981b | 2015-09-18 13:08:26 +0200 | [diff] [blame] | 1643 | sci_submit_rx(s); |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 1644 | } |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 1645 | serial_port_out(port, SCSCR, scr); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1646 | /* Clear current interrupt */ |
Geert Uytterhoeven | 54af500 | 2015-08-21 20:02:28 +0200 | [diff] [blame] | 1647 | serial_port_out(port, SCxSR, |
| 1648 | ssr & ~(SCIF_DR | SCxSR_RDxF(port))); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1649 | dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u us\n", |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 1650 | jiffies, s->rx_timeout); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1651 | start_hrtimer_us(&s->rx_timer, s->rx_timeout); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1652 | |
| 1653 | return IRQ_HANDLED; |
| 1654 | } |
| 1655 | #endif |
| 1656 | |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1657 | if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0) { |
| 1658 | if (!scif_rtrg_enabled(port)) |
| 1659 | scif_set_rtrg(port, s->rx_trigger); |
| 1660 | |
| 1661 | mod_timer(&s->rx_fifo_timer, jiffies + DIV_ROUND_UP( |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 1662 | s->rx_frame * HZ * s->rx_fifo_timeout, 1000000)); |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | /* I think sci_receive_chars has to be called irrespective |
| 1666 | * of whether the I_IXOFF is set, otherwise, how is the interrupt |
| 1667 | * to be disabled? |
| 1668 | */ |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1669 | sci_receive_chars(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
| 1671 | return IRQ_HANDLED; |
| 1672 | } |
| 1673 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1674 | static irqreturn_t sci_tx_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | struct uart_port *port = ptr; |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 1677 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 1679 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | sci_transmit_chars(port); |
Stuart Menefy | fd78a76 | 2009-07-29 23:01:24 +0900 | [diff] [blame] | 1681 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
| 1683 | return IRQ_HANDLED; |
| 1684 | } |
| 1685 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1686 | static irqreturn_t sci_er_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | { |
| 1688 | struct uart_port *port = ptr; |
Geert Uytterhoeven | e6403c1 | 2015-08-21 20:02:55 +0200 | [diff] [blame] | 1689 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
| 1691 | /* Handle errors */ |
| 1692 | if (port->type == PORT_SCI) { |
| 1693 | if (sci_handle_errors(port)) { |
| 1694 | /* discard character in rx buffer */ |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 1695 | serial_port_in(port, SCxSR); |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 1696 | sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
| 1698 | } else { |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 1699 | sci_handle_fifo_overrun(port); |
Geert Uytterhoeven | e6403c1 | 2015-08-21 20:02:55 +0200 | [diff] [blame] | 1700 | if (!s->chan_rx) |
| 1701 | sci_receive_chars(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 1704 | sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
| 1706 | /* Kick the transmission */ |
Yoshihiro Shimoda | 8eadb56 | 2015-08-21 20:02:56 +0200 | [diff] [blame] | 1707 | if (!s->chan_tx) |
| 1708 | sci_tx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
| 1710 | return IRQ_HANDLED; |
| 1711 | } |
| 1712 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1713 | static irqreturn_t sci_br_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | { |
| 1715 | struct uart_port *port = ptr; |
| 1716 | |
| 1717 | /* Handle BREAKs */ |
| 1718 | sci_handle_breaks(port); |
Geert Uytterhoeven | a1b5b43 | 2015-08-21 20:02:25 +0200 | [diff] [blame] | 1719 | sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
| 1721 | return IRQ_HANDLED; |
| 1722 | } |
| 1723 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1724 | static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | { |
Nobuhiro Iwamatsu | cb772fe7 | 2015-03-17 01:19:19 +0900 | [diff] [blame] | 1726 | unsigned short ssr_status, scr_status, err_enabled, orer_status = 0; |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1727 | struct uart_port *port = ptr; |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1728 | struct sci_port *s = to_sci_port(port); |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1729 | irqreturn_t ret = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 1731 | ssr_status = serial_port_in(port, SCxSR); |
| 1732 | scr_status = serial_port_in(port, SCSCR); |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 1733 | if (s->params->overrun_reg == SCxSR) |
Nobuhiro Iwamatsu | cb772fe7 | 2015-03-17 01:19:19 +0900 | [diff] [blame] | 1734 | orer_status = ssr_status; |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 1735 | else if (sci_getreg(port, s->params->overrun_reg)->size) |
| 1736 | orer_status = serial_port_in(port, s->params->overrun_reg); |
Nobuhiro Iwamatsu | cb772fe7 | 2015-03-17 01:19:19 +0900 | [diff] [blame] | 1737 | |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1738 | err_enabled = scr_status & port_rx_irq_mask(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
| 1740 | /* Tx Interrupt */ |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1741 | if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) && |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1742 | !s->chan_tx) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1743 | ret = sci_tx_interrupt(irq, ptr); |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1744 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1745 | /* |
| 1746 | * Rx Interrupt: if we're using DMA, the DMA controller clears RDF / |
| 1747 | * DR flags |
| 1748 | */ |
| 1749 | if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) && |
Geert Uytterhoeven | e0a12a2 | 2015-08-21 20:02:35 +0200 | [diff] [blame] | 1750 | (scr_status & SCSCR_RIE)) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1751 | ret = sci_rx_interrupt(irq, ptr); |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | /* Error Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 1754 | if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1755 | ret = sci_er_interrupt(irq, ptr); |
Paul Mundt | f43dc23 | 2011-01-13 15:06:28 +0900 | [diff] [blame] | 1756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | /* Break Interrupt */ |
SUGIOKA Toshinobu | dd4da3a | 2009-07-07 05:32:07 +0000 | [diff] [blame] | 1758 | if ((ssr_status & SCxSR_BRK(port)) && err_enabled) |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1759 | ret = sci_br_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
Hisashi Nakamura | 8b6ff84 | 2015-01-26 21:25:48 +0900 | [diff] [blame] | 1761 | /* Overrun Interrupt */ |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 1762 | if (orer_status & s->params->overrun_mask) { |
Nobuhiro Iwamatsu | cb772fe7 | 2015-03-17 01:19:19 +0900 | [diff] [blame] | 1763 | sci_handle_fifo_overrun(port); |
Yoshihiro Shimoda | 9080307 | 2015-08-21 20:02:36 +0200 | [diff] [blame] | 1764 | ret = IRQ_HANDLED; |
| 1765 | } |
Hisashi Nakamura | 8b6ff84 | 2015-01-26 21:25:48 +0900 | [diff] [blame] | 1766 | |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 1767 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Geert Uytterhoeven | d56a91e | 2015-08-21 20:02:32 +0200 | [diff] [blame] | 1770 | static const struct sci_irq_desc { |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1771 | const char *desc; |
| 1772 | irq_handler_t handler; |
| 1773 | } sci_irq_desc[] = { |
| 1774 | /* |
| 1775 | * Split out handlers, the default case. |
| 1776 | */ |
| 1777 | [SCIx_ERI_IRQ] = { |
| 1778 | .desc = "rx err", |
| 1779 | .handler = sci_er_interrupt, |
| 1780 | }, |
| 1781 | |
| 1782 | [SCIx_RXI_IRQ] = { |
| 1783 | .desc = "rx full", |
| 1784 | .handler = sci_rx_interrupt, |
| 1785 | }, |
| 1786 | |
| 1787 | [SCIx_TXI_IRQ] = { |
| 1788 | .desc = "tx empty", |
| 1789 | .handler = sci_tx_interrupt, |
| 1790 | }, |
| 1791 | |
| 1792 | [SCIx_BRI_IRQ] = { |
| 1793 | .desc = "break", |
| 1794 | .handler = sci_br_interrupt, |
| 1795 | }, |
| 1796 | |
| 1797 | /* |
| 1798 | * Special muxed handler. |
| 1799 | */ |
| 1800 | [SCIx_MUX_IRQ] = { |
| 1801 | .desc = "mux", |
| 1802 | .handler = sci_mpxed_interrupt, |
| 1803 | }, |
| 1804 | }; |
| 1805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | static int sci_request_irq(struct sci_port *port) |
| 1807 | { |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1808 | struct uart_port *up = &port->port; |
| 1809 | int i, j, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1811 | for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) { |
Geert Uytterhoeven | d56a91e | 2015-08-21 20:02:32 +0200 | [diff] [blame] | 1812 | const struct sci_irq_desc *desc; |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1813 | int irq; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1814 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1815 | if (SCIx_IRQ_IS_MUXED(port)) { |
| 1816 | i = SCIx_MUX_IRQ; |
| 1817 | irq = up->irq; |
Paul Mundt | 0e8963d | 2012-05-18 18:21:06 +0900 | [diff] [blame] | 1818 | } else { |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1819 | irq = port->irqs[i]; |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1820 | |
Paul Mundt | 0e8963d | 2012-05-18 18:21:06 +0900 | [diff] [blame] | 1821 | /* |
| 1822 | * Certain port types won't support all of the |
| 1823 | * available interrupt sources. |
| 1824 | */ |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1825 | if (unlikely(irq < 0)) |
Paul Mundt | 0e8963d | 2012-05-18 18:21:06 +0900 | [diff] [blame] | 1826 | continue; |
| 1827 | } |
| 1828 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1829 | desc = sci_irq_desc + i; |
| 1830 | port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s", |
| 1831 | dev_name(up->dev), desc->desc); |
Pan Bian | 623ac1d | 2016-12-03 18:40:25 +0800 | [diff] [blame] | 1832 | if (!port->irqstr[j]) { |
| 1833 | ret = -ENOMEM; |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1834 | goto out_nomem; |
Pan Bian | 623ac1d | 2016-12-03 18:40:25 +0800 | [diff] [blame] | 1835 | } |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 1836 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1837 | ret = request_irq(irq, desc->handler, up->irqflags, |
| 1838 | port->irqstr[j], port); |
| 1839 | if (unlikely(ret)) { |
| 1840 | dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc); |
| 1841 | goto out_noirq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | return 0; |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1846 | |
| 1847 | out_noirq: |
| 1848 | while (--i >= 0) |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1849 | free_irq(port->irqs[i], port); |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1850 | |
| 1851 | out_nomem: |
| 1852 | while (--j >= 0) |
| 1853 | kfree(port->irqstr[j]); |
| 1854 | |
| 1855 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | static void sci_free_irq(struct sci_port *port) |
| 1859 | { |
| 1860 | int i; |
| 1861 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1862 | /* |
| 1863 | * Intentionally in reverse order so we iterate over the muxed |
| 1864 | * IRQ first. |
| 1865 | */ |
| 1866 | for (i = 0; i < SCIx_NR_IRQS; i++) { |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1867 | int irq = port->irqs[i]; |
Paul Mundt | 0e8963d | 2012-05-18 18:21:06 +0900 | [diff] [blame] | 1868 | |
| 1869 | /* |
| 1870 | * Certain port types won't support all of the available |
| 1871 | * interrupt sources. |
| 1872 | */ |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1873 | if (unlikely(irq < 0)) |
Paul Mundt | 0e8963d | 2012-05-18 18:21:06 +0900 | [diff] [blame] | 1874 | continue; |
| 1875 | |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 1876 | free_irq(port->irqs[i], port); |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1877 | kfree(port->irqstr[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
Paul Mundt | 9174fc8 | 2011-06-28 15:25:36 +0900 | [diff] [blame] | 1879 | if (SCIx_IRQ_IS_MUXED(port)) { |
| 1880 | /* If there's only one IRQ, we're done. */ |
| 1881 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | static unsigned int sci_tx_empty(struct uart_port *port) |
| 1887 | { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 1888 | unsigned short status = serial_port_in(port, SCxSR); |
Paul Mundt | 72b294c | 2011-06-14 17:38:19 +0900 | [diff] [blame] | 1889 | unsigned short in_tx_fifo = sci_txfill(port); |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 1890 | |
| 1891 | return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 1894 | static void sci_set_rts(struct uart_port *port, bool state) |
| 1895 | { |
| 1896 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 1897 | u16 data = serial_port_in(port, SCPDR); |
| 1898 | |
| 1899 | /* Active low */ |
| 1900 | if (state) |
| 1901 | data &= ~SCPDR_RTSD; |
| 1902 | else |
| 1903 | data |= SCPDR_RTSD; |
| 1904 | serial_port_out(port, SCPDR, data); |
| 1905 | |
| 1906 | /* RTS# is output */ |
| 1907 | serial_port_out(port, SCPCR, |
| 1908 | serial_port_in(port, SCPCR) | SCPCR_RTSC); |
| 1909 | } else if (sci_getreg(port, SCSPTR)->size) { |
| 1910 | u16 ctrl = serial_port_in(port, SCSPTR); |
| 1911 | |
| 1912 | /* Active low */ |
| 1913 | if (state) |
| 1914 | ctrl &= ~SCSPTR_RTSDT; |
| 1915 | else |
| 1916 | ctrl |= SCSPTR_RTSDT; |
| 1917 | serial_port_out(port, SCSPTR, ctrl); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | static bool sci_get_cts(struct uart_port *port) |
| 1922 | { |
| 1923 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 1924 | /* Active low */ |
| 1925 | return !(serial_port_in(port, SCPDR) & SCPDR_CTSD); |
| 1926 | } else if (sci_getreg(port, SCSPTR)->size) { |
| 1927 | /* Active low */ |
| 1928 | return !(serial_port_in(port, SCSPTR) & SCSPTR_CTSDT); |
| 1929 | } |
| 1930 | |
| 1931 | return true; |
| 1932 | } |
| 1933 | |
Paul Mundt | cdf7c42 | 2011-11-24 20:18:32 +0900 | [diff] [blame] | 1934 | /* |
| 1935 | * Modem control is a bit of a mixed bag for SCI(F) ports. Generally |
| 1936 | * CTS/RTS is supported in hardware by at least one port and controlled |
| 1937 | * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently |
| 1938 | * handled via the ->init_pins() op, which is a bit of a one-way street, |
| 1939 | * lacking any ability to defer pin control -- this will later be |
| 1940 | * converted over to the GPIO framework). |
Paul Mundt | dc7e3ef | 2011-11-24 20:20:53 +0900 | [diff] [blame] | 1941 | * |
| 1942 | * Other modes (such as loopback) are supported generically on certain |
| 1943 | * port types, but not others. For these it's sufficient to test for the |
| 1944 | * existence of the support register and simply ignore the port type. |
Paul Mundt | cdf7c42 | 2011-11-24 20:18:32 +0900 | [diff] [blame] | 1945 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 1947 | { |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 1948 | struct sci_port *s = to_sci_port(port); |
| 1949 | |
Paul Mundt | dc7e3ef | 2011-11-24 20:20:53 +0900 | [diff] [blame] | 1950 | if (mctrl & TIOCM_LOOP) { |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 1951 | const struct plat_sci_reg *reg; |
Paul Mundt | dc7e3ef | 2011-11-24 20:20:53 +0900 | [diff] [blame] | 1952 | |
| 1953 | /* |
| 1954 | * Standard loopback mode for SCFCR ports. |
| 1955 | */ |
| 1956 | reg = sci_getreg(port, SCFCR); |
| 1957 | if (reg->size) |
Geert Uytterhoeven | 26de4f1 | 2014-03-11 11:11:19 +0100 | [diff] [blame] | 1958 | serial_port_out(port, SCFCR, |
| 1959 | serial_port_in(port, SCFCR) | |
| 1960 | SCFCR_LOOP); |
Paul Mundt | dc7e3ef | 2011-11-24 20:20:53 +0900 | [diff] [blame] | 1961 | } |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 1962 | |
| 1963 | mctrl_gpio_set(s->gpios, mctrl); |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 1964 | |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 1965 | if (!s->has_rtscts) |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 1966 | return; |
| 1967 | |
| 1968 | if (!(mctrl & TIOCM_RTS)) { |
| 1969 | /* Disable Auto RTS */ |
| 1970 | serial_port_out(port, SCFCR, |
| 1971 | serial_port_in(port, SCFCR) & ~SCFCR_MCE); |
| 1972 | |
| 1973 | /* Clear RTS */ |
| 1974 | sci_set_rts(port, 0); |
| 1975 | } else if (s->autorts) { |
| 1976 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) { |
| 1977 | /* Enable RTS# pin function */ |
| 1978 | serial_port_out(port, SCPCR, |
| 1979 | serial_port_in(port, SCPCR) & ~SCPCR_RTSC); |
| 1980 | } |
| 1981 | |
| 1982 | /* Enable Auto RTS */ |
| 1983 | serial_port_out(port, SCFCR, |
| 1984 | serial_port_in(port, SCFCR) | SCFCR_MCE); |
| 1985 | } else { |
| 1986 | /* Set RTS */ |
| 1987 | sci_set_rts(port, 1); |
| 1988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static unsigned int sci_get_mctrl(struct uart_port *port) |
| 1992 | { |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 1993 | struct sci_port *s = to_sci_port(port); |
| 1994 | struct mctrl_gpios *gpios = s->gpios; |
| 1995 | unsigned int mctrl = 0; |
| 1996 | |
| 1997 | mctrl_gpio_get(gpios, &mctrl); |
| 1998 | |
Paul Mundt | cdf7c42 | 2011-11-24 20:18:32 +0900 | [diff] [blame] | 1999 | /* |
| 2000 | * CTS/RTS is handled in hardware when supported, while nothing |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2001 | * else is wired up. |
Paul Mundt | cdf7c42 | 2011-11-24 20:18:32 +0900 | [diff] [blame] | 2002 | */ |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2003 | if (s->autorts) { |
| 2004 | if (sci_get_cts(port)) |
| 2005 | mctrl |= TIOCM_CTS; |
| 2006 | } else if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS))) { |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 2007 | mctrl |= TIOCM_CTS; |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2008 | } |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 2009 | if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))) |
| 2010 | mctrl |= TIOCM_DSR; |
| 2011 | if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))) |
| 2012 | mctrl |= TIOCM_CAR; |
| 2013 | |
| 2014 | return mctrl; |
| 2015 | } |
| 2016 | |
| 2017 | static void sci_enable_ms(struct uart_port *port) |
| 2018 | { |
| 2019 | mctrl_gpio_enable_ms(to_sci_port(port)->gpios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | static void sci_break_ctl(struct uart_port *port, int break_state) |
| 2023 | { |
Shimoda, Yoshihiro | bbb4ce5 | 2012-04-06 09:59:14 +0900 | [diff] [blame] | 2024 | unsigned short scscr, scsptr; |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2025 | unsigned long flags; |
Shimoda, Yoshihiro | bbb4ce5 | 2012-04-06 09:59:14 +0900 | [diff] [blame] | 2026 | |
Shimoda, Yoshihiro | a4e02f6 | 2012-04-12 19:19:21 +0900 | [diff] [blame] | 2027 | /* check wheter the port has SCSPTR */ |
Geert Uytterhoeven | abbf121 | 2016-06-03 12:00:05 +0200 | [diff] [blame] | 2028 | if (!sci_getreg(port, SCSPTR)->size) { |
Shimoda, Yoshihiro | bbb4ce5 | 2012-04-06 09:59:14 +0900 | [diff] [blame] | 2029 | /* |
| 2030 | * Not supported by hardware. Most parts couple break and rx |
| 2031 | * interrupts together, with break detection always enabled. |
| 2032 | */ |
Shimoda, Yoshihiro | a4e02f6 | 2012-04-12 19:19:21 +0900 | [diff] [blame] | 2033 | return; |
Shimoda, Yoshihiro | bbb4ce5 | 2012-04-06 09:59:14 +0900 | [diff] [blame] | 2034 | } |
Shimoda, Yoshihiro | a4e02f6 | 2012-04-12 19:19:21 +0900 | [diff] [blame] | 2035 | |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2036 | spin_lock_irqsave(&port->lock, flags); |
Shimoda, Yoshihiro | a4e02f6 | 2012-04-12 19:19:21 +0900 | [diff] [blame] | 2037 | scsptr = serial_port_in(port, SCSPTR); |
| 2038 | scscr = serial_port_in(port, SCSCR); |
| 2039 | |
| 2040 | if (break_state == -1) { |
| 2041 | scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT; |
| 2042 | scscr &= ~SCSCR_TE; |
| 2043 | } else { |
| 2044 | scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO; |
| 2045 | scscr |= SCSCR_TE; |
| 2046 | } |
| 2047 | |
| 2048 | serial_port_out(port, SCSPTR, scsptr); |
| 2049 | serial_port_out(port, SCSCR, scscr); |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2050 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | static int sci_startup(struct uart_port *port) |
| 2054 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 2055 | struct sci_port *s = to_sci_port(port); |
Paul Mundt | 073e84c | 2011-01-19 17:30:53 +0900 | [diff] [blame] | 2056 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2058 | dev_dbg(port->dev, "%s(%d)\n", __func__, port->line); |
| 2059 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2060 | sci_request_dma(port); |
Paul Mundt | 073e84c | 2011-01-19 17:30:53 +0900 | [diff] [blame] | 2061 | |
Takatoshi Akiyama | 3c91017 | 2017-02-27 15:56:31 +0900 | [diff] [blame] | 2062 | ret = sci_request_irq(s); |
| 2063 | if (unlikely(ret < 0)) { |
| 2064 | sci_free_dma(port); |
| 2065 | return ret; |
| 2066 | } |
| 2067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | static void sci_shutdown(struct uart_port *port) |
| 2072 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 2073 | struct sci_port *s = to_sci_port(port); |
Shinya Kuribayashi | 33b48e1 | 2012-11-16 10:54:49 +0900 | [diff] [blame] | 2074 | unsigned long flags; |
Geert Uytterhoeven | 5fd2b6e | 2016-06-26 11:20:21 +0200 | [diff] [blame] | 2075 | u16 scr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2077 | dev_dbg(port->dev, "%s(%d)\n", __func__, port->line); |
| 2078 | |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2079 | s->autorts = false; |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 2080 | mctrl_gpio_disable_ms(to_sci_port(port)->gpios); |
| 2081 | |
Shinya Kuribayashi | 33b48e1 | 2012-11-16 10:54:49 +0900 | [diff] [blame] | 2082 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | sci_stop_rx(port); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 2084 | sci_stop_tx(port); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2085 | /* |
| 2086 | * Stop RX and TX, disable related interrupts, keep clock source |
| 2087 | * and HSCIF TOT bits |
| 2088 | */ |
Geert Uytterhoeven | 5fd2b6e | 2016-06-26 11:20:21 +0200 | [diff] [blame] | 2089 | scr = serial_port_in(port, SCSCR); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2090 | serial_port_out(port, SCSCR, scr & |
| 2091 | (SCSCR_CKE1 | SCSCR_CKE0 | s->hscif_tot)); |
Shinya Kuribayashi | 33b48e1 | 2012-11-16 10:54:49 +0900 | [diff] [blame] | 2092 | spin_unlock_irqrestore(&port->lock, flags); |
Paul Mundt | 073e84c | 2011-01-19 17:30:53 +0900 | [diff] [blame] | 2093 | |
Aleksandar Mitev | 9ab7655 | 2015-09-18 13:08:28 +0200 | [diff] [blame] | 2094 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
| 2095 | if (s->chan_rx) { |
| 2096 | dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__, |
| 2097 | port->line); |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 2098 | hrtimer_cancel(&s->rx_timer); |
Aleksandar Mitev | 9ab7655 | 2015-09-18 13:08:28 +0200 | [diff] [blame] | 2099 | } |
| 2100 | #endif |
| 2101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | sci_free_irq(s); |
Takatoshi Akiyama | 3c91017 | 2017-02-27 15:56:31 +0900 | [diff] [blame] | 2103 | sci_free_dma(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2106 | static int sci_sck_calc(struct sci_port *s, unsigned int bps, |
| 2107 | unsigned int *srr) |
Paul Mundt | 26c92f3 | 2009-06-24 18:23:52 +0900 | [diff] [blame] | 2108 | { |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2109 | unsigned long freq = s->clk_rates[SCI_SCK]; |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2110 | int err, min_err = INT_MAX; |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2111 | unsigned int sr; |
Laurent Pinchart | ec09c5e | 2013-12-06 10:59:20 +0100 | [diff] [blame] | 2112 | |
Geert Uytterhoeven | 7b5c0c0 | 2016-01-04 14:45:20 +0100 | [diff] [blame] | 2113 | if (s->port.type != PORT_HSCIF) |
| 2114 | freq *= 2; |
Paul Mundt | e8183a6 | 2011-01-19 17:51:37 +0900 | [diff] [blame] | 2115 | |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2116 | for_each_sr(sr, s) { |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2117 | err = DIV_ROUND_CLOSEST(freq, sr) - bps; |
| 2118 | if (abs(err) >= abs(min_err)) |
| 2119 | continue; |
| 2120 | |
| 2121 | min_err = err; |
| 2122 | *srr = sr - 1; |
| 2123 | |
| 2124 | if (!err) |
| 2125 | break; |
| 2126 | } |
| 2127 | |
| 2128 | dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err, |
| 2129 | *srr + 1); |
| 2130 | return min_err; |
Paul Mundt | 26c92f3 | 2009-06-24 18:23:52 +0900 | [diff] [blame] | 2131 | } |
| 2132 | |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2133 | static int sci_brg_calc(struct sci_port *s, unsigned int bps, |
| 2134 | unsigned long freq, unsigned int *dlr, |
| 2135 | unsigned int *srr) |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2136 | { |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2137 | int err, min_err = INT_MAX; |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2138 | unsigned int sr, dl; |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2139 | |
Geert Uytterhoeven | 7b5c0c0 | 2016-01-04 14:45:20 +0100 | [diff] [blame] | 2140 | if (s->port.type != PORT_HSCIF) |
| 2141 | freq *= 2; |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2142 | |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2143 | for_each_sr(sr, s) { |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2144 | dl = DIV_ROUND_CLOSEST(freq, sr * bps); |
| 2145 | dl = clamp(dl, 1U, 65535U); |
| 2146 | |
| 2147 | err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps; |
| 2148 | if (abs(err) >= abs(min_err)) |
| 2149 | continue; |
| 2150 | |
| 2151 | min_err = err; |
| 2152 | *dlr = dl; |
| 2153 | *srr = sr - 1; |
| 2154 | |
| 2155 | if (!err) |
| 2156 | break; |
| 2157 | } |
| 2158 | |
| 2159 | dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps, |
| 2160 | min_err, *dlr, *srr + 1); |
| 2161 | return min_err; |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2162 | } |
| 2163 | |
Geert Uytterhoeven | b4a5c45 | 2015-11-16 17:22:16 +0100 | [diff] [blame] | 2164 | /* calculate sample rate, BRR, and clock select */ |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2165 | static int sci_scbrr_calc(struct sci_port *s, unsigned int bps, |
| 2166 | unsigned int *brr, unsigned int *srr, |
| 2167 | unsigned int *cks) |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2168 | { |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2169 | unsigned long freq = s->clk_rates[SCI_FCK]; |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2170 | unsigned int sr, br, prediv, scrate, c; |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2171 | int err, min_err = INT_MAX; |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2172 | |
Geert Uytterhoeven | 7b5c0c0 | 2016-01-04 14:45:20 +0100 | [diff] [blame] | 2173 | if (s->port.type != PORT_HSCIF) |
| 2174 | freq *= 2; |
Geert Uytterhoeven | b4a5c45 | 2015-11-16 17:22:16 +0100 | [diff] [blame] | 2175 | |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2176 | /* |
| 2177 | * Find the combination of sample rate and clock select with the |
| 2178 | * smallest deviation from the desired baud rate. |
| 2179 | * Prefer high sample rates to maximise the receive margin. |
| 2180 | * |
| 2181 | * M: Receive margin (%) |
| 2182 | * N: Ratio of bit rate to clock (N = sampling rate) |
| 2183 | * D: Clock duty (D = 0 to 1.0) |
| 2184 | * L: Frame length (L = 9 to 12) |
| 2185 | * F: Absolute value of clock frequency deviation |
| 2186 | * |
| 2187 | * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) - |
| 2188 | * (|D - 0.5| / N * (1 + F))| |
| 2189 | * NOTE: Usually, treat D for 0.5, F is 0 by this calculation. |
| 2190 | */ |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2191 | for_each_sr(sr, s) { |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2192 | for (c = 0; c <= 3; c++) { |
| 2193 | /* integerized formulas from HSCIF documentation */ |
Geert Uytterhoeven | 7b5c0c0 | 2016-01-04 14:45:20 +0100 | [diff] [blame] | 2194 | prediv = sr * (1 << (2 * c + 1)); |
Geert Uytterhoeven | de01e6c | 2015-11-13 17:04:56 +0100 | [diff] [blame] | 2195 | |
| 2196 | /* |
| 2197 | * We need to calculate: |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2198 | * |
Geert Uytterhoeven | de01e6c | 2015-11-13 17:04:56 +0100 | [diff] [blame] | 2199 | * br = freq / (prediv * bps) clamped to [1..256] |
Geert Uytterhoeven | 881a748 | 2015-11-16 15:54:47 +0100 | [diff] [blame] | 2200 | * err = freq / (br * prediv) - bps |
Geert Uytterhoeven | de01e6c | 2015-11-13 17:04:56 +0100 | [diff] [blame] | 2201 | * |
| 2202 | * Watch out for overflow when calculating the desired |
| 2203 | * sampling clock rate! |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2204 | */ |
Geert Uytterhoeven | de01e6c | 2015-11-13 17:04:56 +0100 | [diff] [blame] | 2205 | if (bps > UINT_MAX / prediv) |
| 2206 | break; |
| 2207 | |
| 2208 | scrate = prediv * bps; |
| 2209 | br = DIV_ROUND_CLOSEST(freq, scrate); |
Geert Uytterhoeven | 95a2703 | 2015-11-13 16:56:08 +0100 | [diff] [blame] | 2210 | br = clamp(br, 1U, 256U); |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2211 | |
Geert Uytterhoeven | 881a748 | 2015-11-16 15:54:47 +0100 | [diff] [blame] | 2212 | err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps; |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2213 | if (abs(err) >= abs(min_err)) |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2214 | continue; |
| 2215 | |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2216 | min_err = err; |
Geert Uytterhoeven | 95a2703 | 2015-11-13 16:56:08 +0100 | [diff] [blame] | 2217 | *brr = br - 1; |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2218 | *srr = sr - 1; |
| 2219 | *cks = c; |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2220 | |
| 2221 | if (!err) |
| 2222 | goto found; |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Geert Uytterhoeven | 6c51332 | 2015-11-16 16:33:22 +0100 | [diff] [blame] | 2226 | found: |
Geert Uytterhoeven | 881a748 | 2015-11-16 15:54:47 +0100 | [diff] [blame] | 2227 | dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps, |
| 2228 | min_err, *brr, *srr + 1, *cks); |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2229 | return min_err; |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2232 | static void sci_reset(struct uart_port *port) |
| 2233 | { |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 2234 | const struct plat_sci_reg *reg; |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2235 | unsigned int status; |
Ulrich Hecht | 18e8cf1 | 2017-02-03 11:38:17 +0100 | [diff] [blame] | 2236 | struct sci_port *s = to_sci_port(port); |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2237 | |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2238 | serial_port_out(port, SCSCR, s->hscif_tot); /* TE=0, RE=0, CKE1=0 */ |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2239 | |
Paul Mundt | 0979e0e | 2011-11-24 18:35:49 +0900 | [diff] [blame] | 2240 | reg = sci_getreg(port, SCFCR); |
| 2241 | if (reg->size) |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 2242 | serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST); |
Geert Uytterhoeven | 2768cf4 | 2016-06-24 16:59:15 +0200 | [diff] [blame] | 2243 | |
| 2244 | sci_clear_SCxSR(port, |
| 2245 | SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) & |
| 2246 | SCxSR_BREAK_CLEAR(port)); |
Geert Uytterhoeven | fc2af33 | 2016-06-24 16:59:16 +0200 | [diff] [blame] | 2247 | if (sci_getreg(port, SCLSR)->size) { |
| 2248 | status = serial_port_in(port, SCLSR); |
| 2249 | status &= ~(SCLSR_TO | SCLSR_ORER); |
| 2250 | serial_port_out(port, SCLSR, status); |
| 2251 | } |
Ulrich Hecht | 18e8cf1 | 2017-02-03 11:38:17 +0100 | [diff] [blame] | 2252 | |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2253 | if (s->rx_trigger > 1) { |
| 2254 | if (s->rx_fifo_timeout) { |
| 2255 | scif_set_rtrg(port, 1); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 2256 | timer_setup(&s->rx_fifo_timer, rx_fifo_timer_fn, 0); |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2257 | } else { |
Ulrich Hecht | 90afa52 | 2017-02-08 18:31:14 +0100 | [diff] [blame] | 2258 | if (port->type == PORT_SCIFA || |
| 2259 | port->type == PORT_SCIFB) |
| 2260 | scif_set_rtrg(port, 1); |
| 2261 | else |
| 2262 | scif_set_rtrg(port, s->rx_trigger); |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2263 | } |
| 2264 | } |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2267 | static void sci_set_termios(struct uart_port *port, struct ktermios *termios, |
| 2268 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | { |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2270 | unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i, bits; |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2271 | unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0; |
| 2272 | unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0; |
Paul Mundt | 00b9de9 | 2009-06-24 17:53:33 +0900 | [diff] [blame] | 2273 | struct sci_port *s = to_sci_port(port); |
Geert Uytterhoeven | d3184e6 | 2015-08-21 20:02:33 +0200 | [diff] [blame] | 2274 | const struct plat_sci_reg *reg; |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2275 | int min_err = INT_MAX, err; |
| 2276 | unsigned long max_freq = 0; |
| 2277 | int best_clk = -1; |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2278 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
Nobuhiro Iwamatsu | 730c4e7 | 2014-07-14 16:10:00 +0900 | [diff] [blame] | 2280 | if ((termios->c_cflag & CSIZE) == CS7) |
| 2281 | smr_val |= SCSMR_CHR; |
| 2282 | if (termios->c_cflag & PARENB) |
| 2283 | smr_val |= SCSMR_PE; |
| 2284 | if (termios->c_cflag & PARODD) |
| 2285 | smr_val |= SCSMR_PE | SCSMR_ODD; |
| 2286 | if (termios->c_cflag & CSTOPB) |
| 2287 | smr_val |= SCSMR_STOP; |
| 2288 | |
Magnus Damm | 154280f | 2009-12-22 03:37:28 +0000 | [diff] [blame] | 2289 | /* |
| 2290 | * earlyprintk comes here early on with port->uartclk set to zero. |
| 2291 | * the clock framework is not up and running at this point so here |
| 2292 | * we assume that 115200 is the maximum baud rate. please note that |
| 2293 | * the baud rate is not programmed during earlyprintk - it is assumed |
| 2294 | * that the previous boot loader has enabled required clocks and |
| 2295 | * setup the baud rate generator hardware for us already. |
| 2296 | */ |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2297 | if (!port->uartclk) { |
| 2298 | baud = uart_get_baud_rate(port, termios, old, 0, 115200); |
| 2299 | goto done; |
| 2300 | } |
Magnus Damm | 154280f | 2009-12-22 03:37:28 +0000 | [diff] [blame] | 2301 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2302 | for (i = 0; i < SCI_NUM_CLKS; i++) |
| 2303 | max_freq = max(max_freq, s->clk_rates[i]); |
| 2304 | |
Geert Uytterhoeven | 69eee8e | 2016-01-04 14:45:21 +0100 | [diff] [blame] | 2305 | baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s)); |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2306 | if (!baud) |
| 2307 | goto done; |
| 2308 | |
| 2309 | /* |
| 2310 | * There can be multiple sources for the sampling clock. Find the one |
| 2311 | * that gives us the smallest deviation from the desired baud rate. |
| 2312 | */ |
| 2313 | |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2314 | /* Optional Undivided External Clock */ |
| 2315 | if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA && |
| 2316 | port->type != PORT_SCIFB) { |
| 2317 | err = sci_sck_calc(s, baud, &srr1); |
| 2318 | if (abs(err) < abs(min_err)) { |
| 2319 | best_clk = SCI_SCK; |
| 2320 | scr_val = SCSCR_CKE1; |
| 2321 | sccks = SCCKS_CKS; |
| 2322 | min_err = err; |
| 2323 | srr = srr1; |
| 2324 | if (!err) |
| 2325 | goto done; |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2326 | } |
| 2327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2329 | /* Optional BRG Frequency Divided External Clock */ |
| 2330 | if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) { |
| 2331 | err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1, |
| 2332 | &srr1); |
| 2333 | if (abs(err) < abs(min_err)) { |
| 2334 | best_clk = SCI_SCIF_CLK; |
| 2335 | scr_val = SCSCR_CKE1; |
| 2336 | sccks = 0; |
| 2337 | min_err = err; |
| 2338 | dl = dl1; |
| 2339 | srr = srr1; |
| 2340 | if (!err) |
| 2341 | goto done; |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | /* Optional BRG Frequency Divided Internal Clock */ |
| 2346 | if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) { |
| 2347 | err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1, |
| 2348 | &srr1); |
| 2349 | if (abs(err) < abs(min_err)) { |
| 2350 | best_clk = SCI_BRG_INT; |
| 2351 | scr_val = SCSCR_CKE1; |
| 2352 | sccks = SCCKS_XIN; |
| 2353 | min_err = err; |
| 2354 | dl = dl1; |
| 2355 | srr = srr1; |
| 2356 | if (!min_err) |
| 2357 | goto done; |
| 2358 | } |
| 2359 | } |
| 2360 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2361 | /* Divided Functional Clock using standard Bit Rate Register */ |
| 2362 | err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1); |
| 2363 | if (abs(err) < abs(min_err)) { |
| 2364 | best_clk = SCI_FCK; |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2365 | scr_val = 0; |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2366 | min_err = err; |
| 2367 | brr = brr1; |
| 2368 | srr = srr1; |
| 2369 | cks = cks1; |
| 2370 | } |
| 2371 | |
| 2372 | done: |
| 2373 | if (best_clk >= 0) |
| 2374 | dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n", |
| 2375 | s->clks[best_clk], baud, min_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 2377 | sci_port_enable(s); |
Alexandre Courbot | 3600338 | 2011-03-03 08:04:42 +0000 | [diff] [blame] | 2378 | |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2379 | /* |
| 2380 | * Program the optional External Baud Rate Generator (BRG) first. |
| 2381 | * It controls the mux to select (H)SCK or frequency divided clock. |
| 2382 | */ |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2383 | if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) { |
| 2384 | serial_port_out(port, SCDL, dl); |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2385 | serial_port_out(port, SCCKS, sccks); |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2386 | } |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2387 | |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2388 | spin_lock_irqsave(&port->lock, flags); |
| 2389 | |
Magnus Damm | 1ba7622 | 2011-08-03 03:47:36 +0000 | [diff] [blame] | 2390 | sci_reset(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2391 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2392 | uart_update_timeout(port, termios->c_cflag, baud); |
| 2393 | |
Ulrich Hecht | 63ba1e0 | 2018-04-04 17:48:51 +0200 | [diff] [blame^] | 2394 | /* byte size and parity */ |
| 2395 | switch (termios->c_cflag & CSIZE) { |
| 2396 | case CS5: |
| 2397 | bits = 7; |
| 2398 | break; |
| 2399 | case CS6: |
| 2400 | bits = 8; |
| 2401 | break; |
| 2402 | case CS7: |
| 2403 | bits = 9; |
| 2404 | break; |
| 2405 | default: |
| 2406 | bits = 10; |
| 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | if (termios->c_cflag & CSTOPB) |
| 2411 | bits++; |
| 2412 | if (termios->c_cflag & PARENB) |
| 2413 | bits++; |
| 2414 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2415 | if (best_clk >= 0) { |
Geert Uytterhoeven | 92a0574 | 2016-01-04 14:45:22 +0100 | [diff] [blame] | 2416 | if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) |
| 2417 | switch (srr + 1) { |
| 2418 | case 5: smr_val |= SCSMR_SRC_5; break; |
| 2419 | case 7: smr_val |= SCSMR_SRC_7; break; |
| 2420 | case 11: smr_val |= SCSMR_SRC_11; break; |
| 2421 | case 13: smr_val |= SCSMR_SRC_13; break; |
| 2422 | case 16: smr_val |= SCSMR_SRC_16; break; |
| 2423 | case 17: smr_val |= SCSMR_SRC_17; break; |
| 2424 | case 19: smr_val |= SCSMR_SRC_19; break; |
| 2425 | case 27: smr_val |= SCSMR_SRC_27; break; |
| 2426 | } |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2427 | smr_val |= cks; |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2428 | serial_port_out(port, SCSCR, scr_val | s->hscif_tot); |
Takashi Yoshii | 9d482cc | 2012-11-16 10:52:49 +0900 | [diff] [blame] | 2429 | serial_port_out(port, SCSMR, smr_val); |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2430 | serial_port_out(port, SCBRR, brr); |
Ulrich Hecht | 63ba1e0 | 2018-04-04 17:48:51 +0200 | [diff] [blame^] | 2431 | if (sci_getreg(port, HSSRR)->size) { |
| 2432 | unsigned int hssrr = srr | HSCIF_SRE; |
| 2433 | /* Calculate deviation from intended rate at the |
| 2434 | * center of the last stop bit in sampling clocks. |
| 2435 | */ |
| 2436 | int last_stop = bits * 2 - 1; |
| 2437 | int deviation = min_err * srr * last_stop / 2 / baud; |
| 2438 | |
| 2439 | if (abs(deviation) >= 2) { |
| 2440 | /* At least two sampling clocks off at the |
| 2441 | * last stop bit; we can increase the error |
| 2442 | * margin by shifting the sampling point. |
| 2443 | */ |
| 2444 | int shift = min(-8, max(7, deviation / 2)); |
| 2445 | |
| 2446 | hssrr |= (shift << HSCIF_SRHP_SHIFT) & |
| 2447 | HSCIF_SRHP_MASK; |
| 2448 | hssrr |= HSCIF_SRDE; |
| 2449 | } |
| 2450 | serial_port_out(port, HSSRR, hssrr); |
| 2451 | } |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2452 | |
| 2453 | /* Wait one bit interval */ |
| 2454 | udelay((1000000 + (baud - 1)) / baud); |
| 2455 | } else { |
| 2456 | /* Don't touch the bit rate configuration */ |
| 2457 | scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0); |
Geert Uytterhoeven | 3a964ab | 2016-01-04 14:45:19 +0100 | [diff] [blame] | 2458 | smr_val |= serial_port_in(port, SCSMR) & |
| 2459 | (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2460 | serial_port_out(port, SCSCR, scr_val | s->hscif_tot); |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2461 | serial_port_out(port, SCSMR, smr_val); |
| 2462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 2464 | sci_init_pins(port, termios->c_cflag); |
Paul Mundt | 0979e0e | 2011-11-24 18:35:49 +0900 | [diff] [blame] | 2465 | |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2466 | port->status &= ~UPSTAT_AUTOCTS; |
| 2467 | s->autorts = false; |
Paul Mundt | 73c3d53 | 2011-12-02 19:02:06 +0900 | [diff] [blame] | 2468 | reg = sci_getreg(port, SCFCR); |
| 2469 | if (reg->size) { |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 2470 | unsigned short ctrl = serial_port_in(port, SCFCR); |
Paul Mundt | 0979e0e | 2011-11-24 18:35:49 +0900 | [diff] [blame] | 2471 | |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 2472 | if ((port->flags & UPF_HARD_FLOW) && |
| 2473 | (termios->c_cflag & CRTSCTS)) { |
| 2474 | /* There is no CTS interrupt to restart the hardware */ |
| 2475 | port->status |= UPSTAT_AUTOCTS; |
| 2476 | /* MCE is enabled when RTS is raised */ |
| 2477 | s->autorts = true; |
Paul Mundt | faf02f8 | 2011-12-02 17:44:50 +0900 | [diff] [blame] | 2478 | } |
Paul Mundt | 73c3d53 | 2011-12-02 19:02:06 +0900 | [diff] [blame] | 2479 | |
| 2480 | /* |
| 2481 | * As we've done a sci_reset() above, ensure we don't |
| 2482 | * interfere with the FIFOs while toggling MCE. As the |
| 2483 | * reset values could still be set, simply mask them out. |
| 2484 | */ |
| 2485 | ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST); |
| 2486 | |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 2487 | serial_port_out(port, SCFCR, ctrl); |
Paul Mundt | 0979e0e | 2011-11-24 18:35:49 +0900 | [diff] [blame] | 2488 | } |
Geert Uytterhoeven | 5f76895 | 2017-03-28 11:13:45 +0200 | [diff] [blame] | 2489 | if (port->flags & UPF_HARD_FLOW) { |
| 2490 | /* Refresh (Auto) RTS */ |
| 2491 | sci_set_mctrl(port, port->mctrl); |
| 2492 | } |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 2493 | |
Laurent Pinchart | 9f8325b | 2017-01-11 16:43:23 +0200 | [diff] [blame] | 2494 | scr_val |= SCSCR_RE | SCSCR_TE | |
| 2495 | (s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2496 | serial_port_out(port, SCSCR, scr_val | s->hscif_tot); |
Geert Uytterhoeven | 92a0574 | 2016-01-04 14:45:22 +0100 | [diff] [blame] | 2497 | if ((srr + 1 == 5) && |
| 2498 | (port->type == PORT_SCIFA || port->type == PORT_SCIFB)) { |
| 2499 | /* |
| 2500 | * In asynchronous mode, when the sampling rate is 1/5, first |
| 2501 | * received data may become invalid on some SCIFA and SCIFB. |
| 2502 | * To avoid this problem wait more than 1 serial data time (1 |
| 2503 | * bit time x serial data number) after setting SCSCR.RE = 1. |
| 2504 | */ |
| 2505 | udelay(DIV_ROUND_UP(10 * 1000000, baud)); |
| 2506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 2508 | /* |
Nobuhiro Iwamatsu | 5f6d851 | 2015-03-17 01:19:54 +0900 | [diff] [blame] | 2509 | * Calculate delay for 2 DMA buffers (4 FIFO). |
Geert Uytterhoeven | f5835c1 | 2015-08-21 20:02:38 +0200 | [diff] [blame] | 2510 | * See serial_core.c::uart_update_timeout(). |
| 2511 | * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above |
| 2512 | * function calculates 1 jiffie for the data plus 5 jiffies for the |
| 2513 | * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA |
| 2514 | * buffers (4 FIFO sizes), but when performing a faster transfer, the |
| 2515 | * value obtained by this formula is too small. Therefore, if the value |
| 2516 | * is smaller than 20ms, use 20ms as the timeout value for DMA. |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 2517 | */ |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 2518 | s->rx_frame = (10000 * bits) / (baud / 100); |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2519 | #ifdef CONFIG_SERIAL_SH_SCI_DMA |
Ulrich Hecht | b96408b | 2018-02-15 13:02:41 +0100 | [diff] [blame] | 2520 | s->rx_timeout = s->buf_len_rx * 2 * s->rx_frame; |
| 2521 | if (s->rx_timeout < 20) |
| 2522 | s->rx_timeout = 20; |
Guennadi Liakhovetski | 3089f38 | 2010-03-19 13:53:04 +0000 | [diff] [blame] | 2523 | #endif |
| 2524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | if ((termios->c_cflag & CREAD) != 0) |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2526 | sci_start_rx(port); |
Alexandre Courbot | 3600338 | 2011-03-03 08:04:42 +0000 | [diff] [blame] | 2527 | |
Takatoshi Akiyama | 1be2266 | 2017-11-02 11:14:55 +0100 | [diff] [blame] | 2528 | spin_unlock_irqrestore(&port->lock, flags); |
| 2529 | |
Paul Mundt | 23241d4 | 2011-06-28 13:55:31 +0900 | [diff] [blame] | 2530 | sci_port_disable(s); |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 2531 | |
| 2532 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 2533 | sci_enable_ms(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
Teppei Kamijou | 0174e5c | 2012-11-16 10:51:55 +0900 | [diff] [blame] | 2536 | static void sci_pm(struct uart_port *port, unsigned int state, |
| 2537 | unsigned int oldstate) |
| 2538 | { |
| 2539 | struct sci_port *sci_port = to_sci_port(port); |
| 2540 | |
| 2541 | switch (state) { |
Geert Uytterhoeven | d3dfe5d | 2014-03-11 11:11:20 +0100 | [diff] [blame] | 2542 | case UART_PM_STATE_OFF: |
Teppei Kamijou | 0174e5c | 2012-11-16 10:51:55 +0900 | [diff] [blame] | 2543 | sci_port_disable(sci_port); |
| 2544 | break; |
| 2545 | default: |
| 2546 | sci_port_enable(sci_port); |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | static const char *sci_type(struct uart_port *port) |
| 2552 | { |
| 2553 | switch (port->type) { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 2554 | case PORT_IRDA: |
| 2555 | return "irda"; |
| 2556 | case PORT_SCI: |
| 2557 | return "sci"; |
| 2558 | case PORT_SCIF: |
| 2559 | return "scif"; |
| 2560 | case PORT_SCIFA: |
| 2561 | return "scifa"; |
Guennadi Liakhovetski | d1d4b10 | 2010-05-23 16:39:09 +0000 | [diff] [blame] | 2562 | case PORT_SCIFB: |
| 2563 | return "scifb"; |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 2564 | case PORT_HSCIF: |
| 2565 | return "hscif"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 2568 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2571 | static int sci_remap_port(struct uart_port *port) |
| 2572 | { |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2573 | struct sci_port *sport = to_sci_port(port); |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2574 | |
| 2575 | /* |
| 2576 | * Nothing to do if there's already an established membase. |
| 2577 | */ |
| 2578 | if (port->membase) |
| 2579 | return 0; |
| 2580 | |
Laurent Pinchart | 3d73f32 | 2017-01-11 16:43:24 +0200 | [diff] [blame] | 2581 | if (port->dev->of_node || (port->flags & UPF_IOREMAP)) { |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2582 | port->membase = ioremap_nocache(port->mapbase, sport->reg_size); |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2583 | if (unlikely(!port->membase)) { |
| 2584 | dev_err(port->dev, "can't remap port#%d\n", port->line); |
| 2585 | return -ENXIO; |
| 2586 | } |
| 2587 | } else { |
| 2588 | /* |
| 2589 | * For the simple (and majority of) cases where we don't |
| 2590 | * need to do any remapping, just cast the cookie |
| 2591 | * directly. |
| 2592 | */ |
Jingoo Han | 3af4e96 | 2014-02-05 09:56:37 +0900 | [diff] [blame] | 2593 | port->membase = (void __iomem *)(uintptr_t)port->mapbase; |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | static void sci_release_port(struct uart_port *port) |
| 2600 | { |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2601 | struct sci_port *sport = to_sci_port(port); |
| 2602 | |
Laurent Pinchart | 3d73f32 | 2017-01-11 16:43:24 +0200 | [diff] [blame] | 2603 | if (port->dev->of_node || (port->flags & UPF_IOREMAP)) { |
Paul Mundt | e265164 | 2011-01-20 21:24:03 +0900 | [diff] [blame] | 2604 | iounmap(port->membase); |
| 2605 | port->membase = NULL; |
| 2606 | } |
| 2607 | |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2608 | release_mem_region(port->mapbase, sport->reg_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | static int sci_request_port(struct uart_port *port) |
| 2612 | { |
Paul Mundt | e265164 | 2011-01-20 21:24:03 +0900 | [diff] [blame] | 2613 | struct resource *res; |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2614 | struct sci_port *sport = to_sci_port(port); |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2615 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2617 | res = request_mem_region(port->mapbase, sport->reg_size, |
| 2618 | dev_name(port->dev)); |
| 2619 | if (unlikely(res == NULL)) { |
| 2620 | dev_err(port->dev, "request_mem_region failed."); |
Paul Mundt | e265164 | 2011-01-20 21:24:03 +0900 | [diff] [blame] | 2621 | return -EBUSY; |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2624 | ret = sci_remap_port(port); |
| 2625 | if (unlikely(ret != 0)) { |
| 2626 | release_resource(res); |
| 2627 | return ret; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 2628 | } |
Paul Mundt | e265164 | 2011-01-20 21:24:03 +0900 | [diff] [blame] | 2629 | |
| 2630 | return 0; |
| 2631 | } |
| 2632 | |
| 2633 | static void sci_config_port(struct uart_port *port, int flags) |
| 2634 | { |
| 2635 | if (flags & UART_CONFIG_TYPE) { |
| 2636 | struct sci_port *sport = to_sci_port(port); |
| 2637 | |
| 2638 | port->type = sport->cfg->type; |
| 2639 | sci_request_port(port); |
| 2640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | static int sci_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 2644 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | if (ser->baud_base < 2400) |
| 2646 | /* No paper tape reader for Mitch.. */ |
| 2647 | return -EINVAL; |
| 2648 | |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
Julia Lawall | 069a47e | 2016-09-01 19:51:35 +0200 | [diff] [blame] | 2652 | static const struct uart_ops sci_uart_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | .tx_empty = sci_tx_empty, |
| 2654 | .set_mctrl = sci_set_mctrl, |
| 2655 | .get_mctrl = sci_get_mctrl, |
| 2656 | .start_tx = sci_start_tx, |
| 2657 | .stop_tx = sci_stop_tx, |
| 2658 | .stop_rx = sci_stop_rx, |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 2659 | .enable_ms = sci_enable_ms, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | .break_ctl = sci_break_ctl, |
| 2661 | .startup = sci_startup, |
| 2662 | .shutdown = sci_shutdown, |
Geert Uytterhoeven | 1cf4a7e | 2017-04-25 20:15:35 +0200 | [diff] [blame] | 2663 | .flush_buffer = sci_flush_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | .set_termios = sci_set_termios, |
Teppei Kamijou | 0174e5c | 2012-11-16 10:51:55 +0900 | [diff] [blame] | 2665 | .pm = sci_pm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | .type = sci_type, |
| 2667 | .release_port = sci_release_port, |
| 2668 | .request_port = sci_request_port, |
| 2669 | .config_port = sci_config_port, |
| 2670 | .verify_port = sci_verify_port, |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 2671 | #ifdef CONFIG_CONSOLE_POLL |
| 2672 | .poll_get_char = sci_poll_get_char, |
| 2673 | .poll_put_char = sci_poll_put_char, |
| 2674 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | }; |
| 2676 | |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2677 | static int sci_init_clocks(struct sci_port *sci_port, struct device *dev) |
| 2678 | { |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2679 | const char *clk_names[] = { |
| 2680 | [SCI_FCK] = "fck", |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2681 | [SCI_SCK] = "sck", |
Geert Uytterhoeven | 1270f86 | 2015-11-18 11:25:53 +0100 | [diff] [blame] | 2682 | [SCI_BRG_INT] = "brg_int", |
| 2683 | [SCI_SCIF_CLK] = "scif_clk", |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2684 | }; |
| 2685 | struct clk *clk; |
| 2686 | unsigned int i; |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2687 | |
Geert Uytterhoeven | 6af27bf | 2015-11-18 11:12:26 +0100 | [diff] [blame] | 2688 | if (sci_port->cfg->type == PORT_HSCIF) |
| 2689 | clk_names[SCI_SCK] = "hsck"; |
| 2690 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2691 | for (i = 0; i < SCI_NUM_CLKS; i++) { |
| 2692 | clk = devm_clk_get(dev, clk_names[i]); |
| 2693 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 2694 | return -EPROBE_DEFER; |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2695 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2696 | if (IS_ERR(clk) && i == SCI_FCK) { |
| 2697 | /* |
| 2698 | * "fck" used to be called "sci_ick", and we need to |
| 2699 | * maintain DT backward compatibility. |
| 2700 | */ |
| 2701 | clk = devm_clk_get(dev, "sci_ick"); |
| 2702 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 2703 | return -EPROBE_DEFER; |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2704 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2705 | if (!IS_ERR(clk)) |
| 2706 | goto found; |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2707 | |
Geert Uytterhoeven | f4998e5 | 2015-10-26 09:58:16 +0100 | [diff] [blame] | 2708 | /* |
| 2709 | * Not all SH platforms declare a clock lookup entry |
| 2710 | * for SCI devices, in which case we need to get the |
| 2711 | * global "peripheral_clk" clock. |
| 2712 | */ |
| 2713 | clk = devm_clk_get(dev, "peripheral_clk"); |
| 2714 | if (!IS_ERR(clk)) |
| 2715 | goto found; |
| 2716 | |
| 2717 | dev_err(dev, "failed to get %s (%ld)\n", clk_names[i], |
| 2718 | PTR_ERR(clk)); |
| 2719 | return PTR_ERR(clk); |
| 2720 | } |
| 2721 | |
| 2722 | found: |
| 2723 | if (IS_ERR(clk)) |
| 2724 | dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i], |
| 2725 | PTR_ERR(clk)); |
| 2726 | else |
| 2727 | dev_dbg(dev, "clk %s is %pC rate %pCr\n", clk_names[i], |
| 2728 | clk, clk); |
| 2729 | sci_port->clks[i] = IS_ERR(clk) ? NULL : clk; |
| 2730 | } |
| 2731 | return 0; |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2732 | } |
| 2733 | |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 2734 | static const struct sci_port_params * |
| 2735 | sci_probe_regmap(const struct plat_sci_port *cfg) |
| 2736 | { |
| 2737 | unsigned int regtype; |
| 2738 | |
| 2739 | if (cfg->regtype != SCIx_PROBE_REGTYPE) |
| 2740 | return &sci_port_params[cfg->regtype]; |
| 2741 | |
| 2742 | switch (cfg->type) { |
| 2743 | case PORT_SCI: |
| 2744 | regtype = SCIx_SCI_REGTYPE; |
| 2745 | break; |
| 2746 | case PORT_IRDA: |
| 2747 | regtype = SCIx_IRDA_REGTYPE; |
| 2748 | break; |
| 2749 | case PORT_SCIFA: |
| 2750 | regtype = SCIx_SCIFA_REGTYPE; |
| 2751 | break; |
| 2752 | case PORT_SCIFB: |
| 2753 | regtype = SCIx_SCIFB_REGTYPE; |
| 2754 | break; |
| 2755 | case PORT_SCIF: |
| 2756 | /* |
| 2757 | * The SH-4 is a bit of a misnomer here, although that's |
| 2758 | * where this particular port layout originated. This |
| 2759 | * configuration (or some slight variation thereof) |
| 2760 | * remains the dominant model for all SCIFs. |
| 2761 | */ |
| 2762 | regtype = SCIx_SH4_SCIF_REGTYPE; |
| 2763 | break; |
| 2764 | case PORT_HSCIF: |
| 2765 | regtype = SCIx_HSCIF_REGTYPE; |
| 2766 | break; |
| 2767 | default: |
| 2768 | pr_err("Can't probe register map for given port\n"); |
| 2769 | return NULL; |
| 2770 | } |
| 2771 | |
| 2772 | return &sci_port_params[regtype]; |
| 2773 | } |
| 2774 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 2775 | static int sci_init_single(struct platform_device *dev, |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2776 | struct sci_port *sci_port, unsigned int index, |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 2777 | const struct plat_sci_port *p, bool early) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2778 | { |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2779 | struct uart_port *port = &sci_port->port; |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2780 | const struct resource *res; |
| 2781 | unsigned int i; |
Paul Mundt | 3127c6b | 2011-06-28 13:44:37 +0900 | [diff] [blame] | 2782 | int ret; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2783 | |
Paul Mundt | 50f0959 | 2011-12-02 20:09:48 +0900 | [diff] [blame] | 2784 | sci_port->cfg = p; |
| 2785 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2786 | port->ops = &sci_uart_ops; |
| 2787 | port->iotype = UPIO_MEM; |
| 2788 | port->line = index; |
Markus Pietrek | 75136d4 | 2010-01-15 08:33:20 +0900 | [diff] [blame] | 2789 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 2790 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 2791 | if (res == NULL) |
| 2792 | return -ENOMEM; |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2793 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 2794 | port->mapbase = res->start; |
Yoshinori Sato | e4d6f91 | 2015-05-16 23:57:31 +0900 | [diff] [blame] | 2795 | sci_port->reg_size = resource_size(res); |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2796 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 2797 | for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) |
| 2798 | sci_port->irqs[i] = platform_get_irq(dev, i); |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2799 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 2800 | /* The SCI generates several interrupts. They can be muxed together or |
| 2801 | * connected to different interrupt lines. In the muxed case only one |
| 2802 | * interrupt resource is specified. In the non-muxed case three or four |
| 2803 | * interrupt resources are specified, as the BRI interrupt is optional. |
| 2804 | */ |
| 2805 | if (sci_port->irqs[0] < 0) |
| 2806 | return -ENXIO; |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2807 | |
Laurent Pinchart | 89b5c1a | 2013-12-06 10:59:52 +0100 | [diff] [blame] | 2808 | if (sci_port->irqs[1] < 0) { |
| 2809 | sci_port->irqs[1] = sci_port->irqs[0]; |
| 2810 | sci_port->irqs[2] = sci_port->irqs[0]; |
| 2811 | sci_port->irqs[3] = sci_port->irqs[0]; |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2812 | } |
| 2813 | |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 2814 | sci_port->params = sci_probe_regmap(p); |
| 2815 | if (unlikely(sci_port->params == NULL)) |
| 2816 | return -EINVAL; |
Laurent Pinchart | e095ee6 | 2017-01-11 16:43:34 +0200 | [diff] [blame] | 2817 | |
Ulrich Hecht | 18e8cf1 | 2017-02-03 11:38:17 +0100 | [diff] [blame] | 2818 | switch (p->type) { |
| 2819 | case PORT_SCIFB: |
| 2820 | sci_port->rx_trigger = 48; |
| 2821 | break; |
| 2822 | case PORT_HSCIF: |
| 2823 | sci_port->rx_trigger = 64; |
| 2824 | break; |
| 2825 | case PORT_SCIFA: |
| 2826 | sci_port->rx_trigger = 32; |
| 2827 | break; |
| 2828 | case PORT_SCIF: |
| 2829 | if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) |
| 2830 | /* RX triggering not implemented for this IP */ |
| 2831 | sci_port->rx_trigger = 1; |
| 2832 | else |
| 2833 | sci_port->rx_trigger = 8; |
| 2834 | break; |
| 2835 | default: |
| 2836 | sci_port->rx_trigger = 1; |
| 2837 | break; |
| 2838 | } |
| 2839 | |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2840 | sci_port->rx_fifo_timeout = 0; |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2841 | sci_port->hscif_tot = 0; |
Ulrich Hecht | 0394037 | 2017-02-03 11:38:18 +0100 | [diff] [blame] | 2842 | |
Laurent Pinchart | 878fbb91 | 2013-12-06 10:59:51 +0100 | [diff] [blame] | 2843 | /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't |
| 2844 | * match the SoC datasheet, this should be investigated. Let platform |
| 2845 | * data override the sampling rate for now. |
Laurent Pinchart | ec09c5e | 2013-12-06 10:59:20 +0100 | [diff] [blame] | 2846 | */ |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 2847 | sci_port->sampling_rate_mask = p->sampling_rate |
| 2848 | ? SCI_SR(p->sampling_rate) |
| 2849 | : sci_port->params->sampling_rate_mask; |
Laurent Pinchart | ec09c5e | 2013-12-06 10:59:20 +0100 | [diff] [blame] | 2850 | |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2851 | if (!early) { |
Laurent Pinchart | a9ec81f | 2015-09-14 15:14:23 +0300 | [diff] [blame] | 2852 | ret = sci_init_clocks(sci_port, &dev->dev); |
| 2853 | if (ret < 0) |
| 2854 | return ret; |
Paul Mundt | c7ed1ab | 2010-03-10 18:35:14 +0900 | [diff] [blame] | 2855 | |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2856 | port->dev = &dev->dev; |
Magnus Damm | 5e50d2d | 2011-04-19 10:38:25 +0000 | [diff] [blame] | 2857 | |
| 2858 | pm_runtime_enable(&dev->dev); |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 2859 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2860 | |
Paul Mundt | ce6738b | 2011-01-19 15:24:40 +0900 | [diff] [blame] | 2861 | port->type = p->type; |
Laurent Pinchart | 3d73f32 | 2017-01-11 16:43:24 +0200 | [diff] [blame] | 2862 | port->flags = UPF_FIXED_PORT | UPF_BOOT_AUTOCONF | p->flags; |
Laurent Pinchart | b2f20ed | 2017-01-11 16:43:36 +0200 | [diff] [blame] | 2863 | port->fifosize = sci_port->params->fifosize; |
Paul Mundt | ce6738b | 2011-01-19 15:24:40 +0900 | [diff] [blame] | 2864 | |
Laurent Pinchart | dfc8038 | 2017-01-11 16:43:40 +0200 | [diff] [blame] | 2865 | if (port->type == PORT_SCI) { |
| 2866 | if (sci_port->reg_size >= 0x20) |
| 2867 | port->regshift = 2; |
| 2868 | else |
| 2869 | port->regshift = 1; |
| 2870 | } |
| 2871 | |
Paul Mundt | ce6738b | 2011-01-19 15:24:40 +0900 | [diff] [blame] | 2872 | /* |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 2873 | * The UART port needs an IRQ value, so we peg this to the RX IRQ |
Paul Mundt | ce6738b | 2011-01-19 15:24:40 +0900 | [diff] [blame] | 2874 | * for the multi-IRQ ports, which is where we are primarily |
| 2875 | * concerned with the shutdown path synchronization. |
| 2876 | * |
| 2877 | * For the muxed case there's nothing more to do. |
| 2878 | */ |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 2879 | port->irq = sci_port->irqs[SCIx_RXI_IRQ]; |
Yong Zhang | 9cfb5c0 | 2011-09-22 16:59:15 +0800 | [diff] [blame] | 2880 | port->irqflags = 0; |
Guennadi Liakhovetski | 73a19e4c | 2010-03-02 11:39:15 +0900 | [diff] [blame] | 2881 | |
Paul Mundt | 61a6976 | 2011-06-14 12:40:19 +0900 | [diff] [blame] | 2882 | port->serial_in = sci_serial_in; |
| 2883 | port->serial_out = sci_serial_out; |
| 2884 | |
Paul Mundt | c7ed1ab | 2010-03-10 18:35:14 +0900 | [diff] [blame] | 2885 | return 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2886 | } |
| 2887 | |
Laurent Pinchart | 6dae142 | 2012-06-13 00:28:23 +0200 | [diff] [blame] | 2888 | static void sci_cleanup_single(struct sci_port *port) |
| 2889 | { |
Laurent Pinchart | 6dae142 | 2012-06-13 00:28:23 +0200 | [diff] [blame] | 2890 | pm_runtime_disable(port->port.dev); |
| 2891 | } |
| 2892 | |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 2893 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \ |
| 2894 | defined(CONFIG_SERIAL_SH_SCI_EARLYCON) |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 2895 | static void serial_console_putchar(struct uart_port *port, int ch) |
| 2896 | { |
| 2897 | sci_poll_put_char(port, ch); |
| 2898 | } |
| 2899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | /* |
| 2901 | * Print a string to the serial port trying not to disturb |
| 2902 | * any possible real use of the port... |
| 2903 | */ |
| 2904 | static void serial_console_write(struct console *co, const char *s, |
| 2905 | unsigned count) |
| 2906 | { |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2907 | struct sci_port *sci_port = &sci_ports[co->index]; |
| 2908 | struct uart_port *port = &sci_port->port; |
Geert Uytterhoeven | a67969b | 2015-11-18 16:20:44 +0100 | [diff] [blame] | 2909 | unsigned short bits, ctrl, ctrl_temp; |
Shinya Kuribayashi | 40f70c0 | 2012-11-16 10:54:15 +0900 | [diff] [blame] | 2910 | unsigned long flags; |
| 2911 | int locked = 1; |
| 2912 | |
| 2913 | local_irq_save(flags); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 2914 | #if defined(SUPPORT_SYSRQ) |
Shinya Kuribayashi | 40f70c0 | 2012-11-16 10:54:15 +0900 | [diff] [blame] | 2915 | if (port->sysrq) |
| 2916 | locked = 0; |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 2917 | else |
| 2918 | #endif |
| 2919 | if (oops_in_progress) |
Shinya Kuribayashi | 40f70c0 | 2012-11-16 10:54:15 +0900 | [diff] [blame] | 2920 | locked = spin_trylock(&port->lock); |
| 2921 | else |
| 2922 | spin_lock(&port->lock); |
| 2923 | |
Geert Uytterhoeven | a67969b | 2015-11-18 16:20:44 +0100 | [diff] [blame] | 2924 | /* first save SCSCR then disable interrupts, keep clock source */ |
Shinya Kuribayashi | 40f70c0 | 2012-11-16 10:54:15 +0900 | [diff] [blame] | 2925 | ctrl = serial_port_in(port, SCSCR); |
Laurent Pinchart | 9f8325b | 2017-01-11 16:43:23 +0200 | [diff] [blame] | 2926 | ctrl_temp = SCSCR_RE | SCSCR_TE | |
| 2927 | (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) | |
Geert Uytterhoeven | a67969b | 2015-11-18 16:20:44 +0100 | [diff] [blame] | 2928 | (ctrl & (SCSCR_CKE1 | SCSCR_CKE0)); |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 2929 | serial_port_out(port, SCSCR, ctrl_temp | sci_port->hscif_tot); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 2930 | |
Magnus Damm | 501b825 | 2009-01-21 15:14:30 +0000 | [diff] [blame] | 2931 | uart_console_write(port, s, count, serial_console_putchar); |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 2932 | |
| 2933 | /* wait until fifo is empty and last bit has been transmitted */ |
| 2934 | bits = SCxSR_TDxE(port) | SCxSR_TEND(port); |
Paul Mundt | b12bb29 | 2012-03-30 19:50:15 +0900 | [diff] [blame] | 2935 | while ((serial_port_in(port, SCxSR) & bits) != bits) |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 2936 | cpu_relax(); |
Shinya Kuribayashi | 40f70c0 | 2012-11-16 10:54:15 +0900 | [diff] [blame] | 2937 | |
| 2938 | /* restore the SCSCR */ |
| 2939 | serial_port_out(port, SCSCR, ctrl); |
| 2940 | |
| 2941 | if (locked) |
| 2942 | spin_unlock(&port->lock); |
| 2943 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 2946 | static int serial_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | { |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 2948 | struct sci_port *sci_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | struct uart_port *port; |
| 2950 | int baud = 115200; |
| 2951 | int bits = 8; |
| 2952 | int parity = 'n'; |
| 2953 | int flow = 'n'; |
| 2954 | int ret; |
| 2955 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2956 | /* |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2957 | * Refuse to handle any bogus ports. |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2958 | */ |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2959 | if (co->index < 0 || co->index >= SCI_NPORTS) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2960 | return -ENODEV; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2961 | |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2962 | sci_port = &sci_ports[co->index]; |
| 2963 | port = &sci_port->port; |
| 2964 | |
Alexandre Courbot | b2267a6 | 2011-02-09 03:18:46 +0000 | [diff] [blame] | 2965 | /* |
| 2966 | * Refuse to handle uninitialized ports. |
| 2967 | */ |
| 2968 | if (!port->ops) |
| 2969 | return -ENODEV; |
| 2970 | |
Paul Mundt | f6e9495 | 2011-01-21 15:25:36 +0900 | [diff] [blame] | 2971 | ret = sci_remap_port(port); |
| 2972 | if (unlikely(ret != 0)) |
| 2973 | return ret; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 2974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | if (options) |
| 2976 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2977 | |
Paul Mundt | ab7cfb5 | 2011-06-01 14:47:42 +0900 | [diff] [blame] | 2978 | return uart_set_options(port, co, baud, parity, bits, flow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | static struct console serial_console = { |
| 2982 | .name = "ttySC", |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2983 | .device = uart_console_device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | .write = serial_console_write, |
| 2985 | .setup = serial_console_setup, |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 2986 | .flags = CON_PRINTBUFFER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | .index = -1, |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2988 | .data = &sci_uart_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | }; |
| 2990 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 2991 | static struct console early_serial_console = { |
| 2992 | .name = "early_ttySC", |
| 2993 | .write = serial_console_write, |
| 2994 | .flags = CON_PRINTBUFFER, |
Paul Mundt | 906b17d | 2011-01-21 16:19:53 +0900 | [diff] [blame] | 2995 | .index = -1, |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 2996 | }; |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 2997 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 2998 | static char early_serial_buf[32]; |
| 2999 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 3000 | static int sci_probe_earlyprintk(struct platform_device *pdev) |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3001 | { |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 3002 | const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev); |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3003 | |
| 3004 | if (early_serial_console.data) |
| 3005 | return -EEXIST; |
| 3006 | |
| 3007 | early_serial_console.index = pdev->id; |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3008 | |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 3009 | sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true); |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3010 | |
| 3011 | serial_console_setup(&early_serial_console, early_serial_buf); |
| 3012 | |
| 3013 | if (!strstr(early_serial_buf, "keep")) |
| 3014 | early_serial_console.flags |= CON_BOOT; |
| 3015 | |
| 3016 | register_console(&early_serial_console); |
| 3017 | return 0; |
| 3018 | } |
Nobuhiro Iwamatsu | 6a8c979 | 2011-03-24 02:20:56 +0000 | [diff] [blame] | 3019 | |
| 3020 | #define SCI_CONSOLE (&serial_console) |
| 3021 | |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3022 | #else |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 3023 | static inline int sci_probe_earlyprintk(struct platform_device *pdev) |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3024 | { |
| 3025 | return -EINVAL; |
| 3026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | |
Nobuhiro Iwamatsu | 6a8c979 | 2011-03-24 02:20:56 +0000 | [diff] [blame] | 3028 | #define SCI_CONSOLE NULL |
| 3029 | |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3030 | #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | |
Geert Uytterhoeven | 6c13d5d | 2014-03-11 11:11:17 +0100 | [diff] [blame] | 3032 | static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | |
Sjoerd Simons | 352b926 | 2017-04-20 14:13:01 +0200 | [diff] [blame] | 3034 | static DEFINE_MUTEX(sci_uart_registration_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | static struct uart_driver sci_uart_driver = { |
| 3036 | .owner = THIS_MODULE, |
| 3037 | .driver_name = "sci", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | .dev_name = "ttySC", |
| 3039 | .major = SCI_MAJOR, |
| 3040 | .minor = SCI_MINOR_START, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3041 | .nr = SCI_NPORTS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | .cons = SCI_CONSOLE, |
| 3043 | }; |
| 3044 | |
Paul Mundt | 54507f6 | 2009-05-08 23:48:33 +0900 | [diff] [blame] | 3045 | static int sci_remove(struct platform_device *dev) |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3046 | { |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3047 | struct sci_port *port = platform_get_drvdata(dev); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3048 | |
Geert Uytterhoeven | 7678f4c | 2018-03-05 18:17:40 +0100 | [diff] [blame] | 3049 | sci_ports_in_use &= ~BIT(port->port.line); |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3050 | uart_remove_one_port(&sci_uart_driver, &port->port); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3051 | |
Laurent Pinchart | 6dae142 | 2012-06-13 00:28:23 +0200 | [diff] [blame] | 3052 | sci_cleanup_single(port); |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3053 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 3054 | if (port->port.fifosize > 1) { |
| 3055 | sysfs_remove_file(&dev->dev.kobj, |
| 3056 | &dev_attr_rx_fifo_trigger.attr); |
| 3057 | } |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 3058 | if (port->port.type == PORT_SCIFA || port->port.type == PORT_SCIFB || |
| 3059 | port->port.type == PORT_HSCIF) { |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 3060 | sysfs_remove_file(&dev->dev.kobj, |
| 3061 | &dev_attr_rx_fifo_timeout.attr); |
| 3062 | } |
| 3063 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3064 | return 0; |
| 3065 | } |
| 3066 | |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3067 | |
| 3068 | #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype)) |
| 3069 | #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16) |
| 3070 | #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff) |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3071 | |
| 3072 | static const struct of_device_id of_sci_match[] = { |
Geert Uytterhoeven | f443ff8 | 2015-11-10 16:16:54 +0100 | [diff] [blame] | 3073 | /* SoC-specific types */ |
| 3074 | { |
| 3075 | .compatible = "renesas,scif-r7s72100", |
| 3076 | .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE), |
| 3077 | }, |
Geert Uytterhoeven | 9ed44bb | 2015-11-10 18:57:23 +0100 | [diff] [blame] | 3078 | /* Family-specific types */ |
| 3079 | { |
| 3080 | .compatible = "renesas,rcar-gen1-scif", |
| 3081 | .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE), |
| 3082 | }, { |
| 3083 | .compatible = "renesas,rcar-gen2-scif", |
| 3084 | .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE), |
| 3085 | }, { |
| 3086 | .compatible = "renesas,rcar-gen3-scif", |
| 3087 | .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE), |
| 3088 | }, |
Geert Uytterhoeven | f443ff8 | 2015-11-10 16:16:54 +0100 | [diff] [blame] | 3089 | /* Generic types */ |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3090 | { |
| 3091 | .compatible = "renesas,scif", |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3092 | .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE), |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3093 | }, { |
| 3094 | .compatible = "renesas,scifa", |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3095 | .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE), |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3096 | }, { |
| 3097 | .compatible = "renesas,scifb", |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3098 | .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE), |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3099 | }, { |
| 3100 | .compatible = "renesas,hscif", |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3101 | .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE), |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3102 | }, { |
Yoshinori Sato | e1d0be6 | 2015-01-28 02:53:55 +0900 | [diff] [blame] | 3103 | .compatible = "renesas,sci", |
Geert Uytterhoeven | bd2238f | 2015-11-10 16:09:23 +0100 | [diff] [blame] | 3104 | .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE), |
Yoshinori Sato | e1d0be6 | 2015-01-28 02:53:55 +0900 | [diff] [blame] | 3105 | }, { |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3106 | /* Terminator */ |
| 3107 | }, |
| 3108 | }; |
| 3109 | MODULE_DEVICE_TABLE(of, of_sci_match); |
| 3110 | |
Geert Uytterhoeven | 54b12c4 | 2017-01-25 15:55:49 +0100 | [diff] [blame] | 3111 | static struct plat_sci_port *sci_parse_dt(struct platform_device *pdev, |
| 3112 | unsigned int *dev_id) |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3113 | { |
| 3114 | struct device_node *np = pdev->dev.of_node; |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3115 | struct plat_sci_port *p; |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 3116 | struct sci_port *sp; |
Geert Uytterhoeven | 6e605a0 | 2017-10-04 14:21:56 +0200 | [diff] [blame] | 3117 | const void *data; |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3118 | int id; |
| 3119 | |
| 3120 | if (!IS_ENABLED(CONFIG_OF) || !np) |
| 3121 | return NULL; |
| 3122 | |
Geert Uytterhoeven | 6e605a0 | 2017-10-04 14:21:56 +0200 | [diff] [blame] | 3123 | data = of_device_get_match_data(&pdev->dev); |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3124 | |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3125 | p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL); |
Geert Uytterhoeven | 4205463 | 2015-08-21 20:02:34 +0200 | [diff] [blame] | 3126 | if (!p) |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3127 | return NULL; |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3128 | |
Geert Uytterhoeven | 2095fc7 | 2015-11-12 13:39:49 +0100 | [diff] [blame] | 3129 | /* Get the line number from the aliases node. */ |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3130 | id = of_alias_get_id(np, "serial"); |
Geert Uytterhoeven | 7678f4c | 2018-03-05 18:17:40 +0100 | [diff] [blame] | 3131 | if (id < 0 && ~sci_ports_in_use) |
| 3132 | id = ffz(sci_ports_in_use); |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3133 | if (id < 0) { |
| 3134 | dev_err(&pdev->dev, "failed to get alias id (%d)\n", id); |
| 3135 | return NULL; |
| 3136 | } |
Geert Uytterhoeven | 090fa4b | 2018-02-23 14:38:35 +0100 | [diff] [blame] | 3137 | if (id >= ARRAY_SIZE(sci_ports)) { |
| 3138 | dev_err(&pdev->dev, "serial%d out of range\n", id); |
| 3139 | return NULL; |
| 3140 | } |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3141 | |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 3142 | sp = &sci_ports[id]; |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3143 | *dev_id = id; |
| 3144 | |
Geert Uytterhoeven | 6e605a0 | 2017-10-04 14:21:56 +0200 | [diff] [blame] | 3145 | p->type = SCI_OF_TYPE(data); |
| 3146 | p->regtype = SCI_OF_REGTYPE(data); |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3147 | |
Sergei Shtylyov | 43c6128 | 2017-08-13 22:11:24 +0300 | [diff] [blame] | 3148 | sp->has_rtscts = of_property_read_bool(np, "uart-has-rtscts"); |
Geert Uytterhoeven | 861a70a | 2016-06-03 12:00:11 +0200 | [diff] [blame] | 3149 | |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3150 | return p; |
| 3151 | } |
| 3152 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 3153 | static int sci_probe_single(struct platform_device *dev, |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3154 | unsigned int index, |
| 3155 | struct plat_sci_port *p, |
| 3156 | struct sci_port *sciport) |
| 3157 | { |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3158 | int ret; |
| 3159 | |
| 3160 | /* Sanity check */ |
| 3161 | if (unlikely(index >= SCI_NPORTS)) { |
Joe Perches | 9b971cd | 2014-03-11 10:10:46 -0700 | [diff] [blame] | 3162 | dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n", |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3163 | index+1, SCI_NPORTS); |
Joe Perches | 9b971cd | 2014-03-11 10:10:46 -0700 | [diff] [blame] | 3164 | dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n"); |
Laurent Pinchart | b6c5ef6 | 2012-06-13 00:28:24 +0200 | [diff] [blame] | 3165 | return -EINVAL; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3166 | } |
Geert Uytterhoeven | 7678f4c | 2018-03-05 18:17:40 +0100 | [diff] [blame] | 3167 | BUILD_BUG_ON(SCI_NPORTS > sizeof(sci_ports_in_use) * 8); |
| 3168 | if (sci_ports_in_use & BIT(index)) |
| 3169 | return -EBUSY; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3170 | |
Sjoerd Simons | 352b926 | 2017-04-20 14:13:01 +0200 | [diff] [blame] | 3171 | mutex_lock(&sci_uart_registration_lock); |
| 3172 | if (!sci_uart_driver.state) { |
| 3173 | ret = uart_register_driver(&sci_uart_driver); |
| 3174 | if (ret) { |
| 3175 | mutex_unlock(&sci_uart_registration_lock); |
| 3176 | return ret; |
| 3177 | } |
| 3178 | } |
| 3179 | mutex_unlock(&sci_uart_registration_lock); |
| 3180 | |
Laurent Pinchart | 1fcc91a | 2013-12-06 10:59:16 +0100 | [diff] [blame] | 3181 | ret = sci_init_single(dev, sciport, index, p, false); |
Paul Mundt | c7ed1ab | 2010-03-10 18:35:14 +0900 | [diff] [blame] | 3182 | if (ret) |
| 3183 | return ret; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3184 | |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 3185 | sciport->gpios = mctrl_gpio_init(&sciport->port, 0); |
| 3186 | if (IS_ERR(sciport->gpios) && PTR_ERR(sciport->gpios) != -ENOSYS) |
| 3187 | return PTR_ERR(sciport->gpios); |
| 3188 | |
Laurent Pinchart | 97ed979 | 2017-01-11 16:43:39 +0200 | [diff] [blame] | 3189 | if (sciport->has_rtscts) { |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 3190 | if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios, |
| 3191 | UART_GPIO_CTS)) || |
| 3192 | !IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios, |
| 3193 | UART_GPIO_RTS))) { |
| 3194 | dev_err(&dev->dev, "Conflicting RTS/CTS config\n"); |
| 3195 | return -EINVAL; |
| 3196 | } |
Geert Uytterhoeven | 33f50ff | 2016-06-03 12:00:10 +0200 | [diff] [blame] | 3197 | sciport->port.flags |= UPF_HARD_FLOW; |
Geert Uytterhoeven | f907c9e | 2016-06-03 12:00:04 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
Laurent Pinchart | 6dae142 | 2012-06-13 00:28:23 +0200 | [diff] [blame] | 3200 | ret = uart_add_one_port(&sci_uart_driver, &sciport->port); |
| 3201 | if (ret) { |
| 3202 | sci_cleanup_single(sciport); |
| 3203 | return ret; |
| 3204 | } |
| 3205 | |
| 3206 | return 0; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 3209 | static int sci_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | { |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3211 | struct plat_sci_port *p; |
| 3212 | struct sci_port *sp; |
| 3213 | unsigned int dev_id; |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3214 | int ret; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3215 | |
Paul Mundt | ecdf8a4 | 2011-01-21 00:05:48 +0900 | [diff] [blame] | 3216 | /* |
| 3217 | * If we've come here via earlyprintk initialization, head off to |
| 3218 | * the special early probe. We don't have sufficient device state |
| 3219 | * to make it beyond this yet. |
| 3220 | */ |
| 3221 | if (is_early_platform_device(dev)) |
| 3222 | return sci_probe_earlyprintk(dev); |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 3223 | |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3224 | if (dev->dev.of_node) { |
| 3225 | p = sci_parse_dt(dev, &dev_id); |
| 3226 | if (p == NULL) |
| 3227 | return -EINVAL; |
| 3228 | } else { |
| 3229 | p = dev->dev.platform_data; |
| 3230 | if (p == NULL) { |
| 3231 | dev_err(&dev->dev, "no platform data supplied\n"); |
| 3232 | return -EINVAL; |
| 3233 | } |
| 3234 | |
| 3235 | dev_id = dev->id; |
| 3236 | } |
| 3237 | |
| 3238 | sp = &sci_ports[dev_id]; |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3239 | platform_set_drvdata(dev, sp); |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3240 | |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3241 | ret = sci_probe_single(dev, dev_id, p, sp); |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3242 | if (ret) |
Laurent Pinchart | 6dae142 | 2012-06-13 00:28:23 +0200 | [diff] [blame] | 3243 | return ret; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 3244 | |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 3245 | if (sp->port.fifosize > 1) { |
| 3246 | ret = sysfs_create_file(&dev->dev.kobj, |
| 3247 | &dev_attr_rx_fifo_trigger.attr); |
| 3248 | if (ret) |
| 3249 | return ret; |
| 3250 | } |
Ulrich Hecht | fa2abb0 | 2017-09-29 15:08:53 +0200 | [diff] [blame] | 3251 | if (sp->port.type == PORT_SCIFA || sp->port.type == PORT_SCIFB || |
| 3252 | sp->port.type == PORT_HSCIF) { |
Ulrich Hecht | 5d23188 | 2017-02-03 11:38:19 +0100 | [diff] [blame] | 3253 | ret = sysfs_create_file(&dev->dev.kobj, |
| 3254 | &dev_attr_rx_fifo_timeout.attr); |
| 3255 | if (ret) { |
| 3256 | if (sp->port.fifosize > 1) { |
| 3257 | sysfs_remove_file(&dev->dev.kobj, |
| 3258 | &dev_attr_rx_fifo_trigger.attr); |
| 3259 | } |
| 3260 | return ret; |
| 3261 | } |
| 3262 | } |
| 3263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | #ifdef CONFIG_SH_STANDARD_BIOS |
| 3265 | sh_bios_gdb_detach(); |
| 3266 | #endif |
| 3267 | |
Geert Uytterhoeven | 7678f4c | 2018-03-05 18:17:40 +0100 | [diff] [blame] | 3268 | sci_ports_in_use |= BIT(dev_id); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3269 | return 0; |
| 3270 | } |
| 3271 | |
Sergei Shtylyov | cb87634 | 2015-01-16 13:56:02 -0800 | [diff] [blame] | 3272 | static __maybe_unused int sci_suspend(struct device *dev) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3273 | { |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3274 | struct sci_port *sport = dev_get_drvdata(dev); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3275 | |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3276 | if (sport) |
| 3277 | uart_suspend_port(&sci_uart_driver, &sport->port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3278 | |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
Sergei Shtylyov | cb87634 | 2015-01-16 13:56:02 -0800 | [diff] [blame] | 3282 | static __maybe_unused int sci_resume(struct device *dev) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3283 | { |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3284 | struct sci_port *sport = dev_get_drvdata(dev); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3285 | |
Paul Mundt | d535a23 | 2011-01-19 17:19:35 +0900 | [diff] [blame] | 3286 | if (sport) |
| 3287 | uart_resume_port(&sci_uart_driver, &sport->port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3288 | |
| 3289 | return 0; |
| 3290 | } |
| 3291 | |
Sergei Shtylyov | cb87634 | 2015-01-16 13:56:02 -0800 | [diff] [blame] | 3292 | static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume); |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 3293 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3294 | static struct platform_driver sci_driver = { |
| 3295 | .probe = sci_probe, |
Uwe Kleine-König | b9e39c8 | 2009-11-24 22:07:32 +0100 | [diff] [blame] | 3296 | .remove = sci_remove, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3297 | .driver = { |
| 3298 | .name = "sh-sci", |
Paul Mundt | 6daa79b | 2009-06-15 07:07:38 +0900 | [diff] [blame] | 3299 | .pm = &sci_dev_pm_ops, |
Bastian Hecht | 20bdcab | 2013-12-06 10:59:54 +0100 | [diff] [blame] | 3300 | .of_match_table = of_match_ptr(of_sci_match), |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3301 | }, |
| 3302 | }; |
| 3303 | |
| 3304 | static int __init sci_init(void) |
| 3305 | { |
Geert Uytterhoeven | 6c13d5d | 2014-03-11 11:11:17 +0100 | [diff] [blame] | 3306 | pr_info("%s\n", banner); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3307 | |
Sjoerd Simons | 352b926 | 2017-04-20 14:13:01 +0200 | [diff] [blame] | 3308 | return platform_driver_register(&sci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | static void __exit sci_exit(void) |
| 3312 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3313 | platform_driver_unregister(&sci_driver); |
Sjoerd Simons | 352b926 | 2017-04-20 14:13:01 +0200 | [diff] [blame] | 3314 | |
| 3315 | if (sci_uart_driver.state) |
| 3316 | uart_unregister_driver(&sci_uart_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | } |
| 3318 | |
Magnus Damm | 7b6fd3b | 2009-12-14 10:24:42 +0000 | [diff] [blame] | 3319 | #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE |
| 3320 | early_platform_init_buffer("earlyprintk", &sci_driver, |
| 3321 | early_serial_buf, ARRAY_SIZE(early_serial_buf)); |
| 3322 | #endif |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3323 | #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON |
Matthias Kaehlcke | dd076cf | 2017-10-09 18:26:22 -0700 | [diff] [blame] | 3324 | static struct plat_sci_port port_cfg __initdata; |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3325 | |
| 3326 | static int __init early_console_setup(struct earlycon_device *device, |
| 3327 | int type) |
| 3328 | { |
| 3329 | if (!device->port.membase) |
| 3330 | return -ENODEV; |
| 3331 | |
| 3332 | device->port.serial_in = sci_serial_in; |
| 3333 | device->port.serial_out = sci_serial_out; |
| 3334 | device->port.type = type; |
| 3335 | memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port)); |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 3336 | port_cfg.type = type; |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3337 | sci_ports[0].cfg = &port_cfg; |
Laurent Pinchart | daf5a89 | 2017-01-11 16:43:35 +0200 | [diff] [blame] | 3338 | sci_ports[0].params = sci_probe_regmap(&port_cfg); |
Laurent Pinchart | 9f8325b | 2017-01-11 16:43:23 +0200 | [diff] [blame] | 3339 | port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR); |
| 3340 | sci_serial_out(&sci_ports[0].port, SCSCR, |
| 3341 | SCSCR_RE | SCSCR_TE | port_cfg.scscr); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3342 | |
| 3343 | device->con->write = serial_console_write; |
| 3344 | return 0; |
| 3345 | } |
| 3346 | static int __init sci_early_console_setup(struct earlycon_device *device, |
| 3347 | const char *opt) |
| 3348 | { |
| 3349 | return early_console_setup(device, PORT_SCI); |
| 3350 | } |
| 3351 | static int __init scif_early_console_setup(struct earlycon_device *device, |
| 3352 | const char *opt) |
| 3353 | { |
| 3354 | return early_console_setup(device, PORT_SCIF); |
| 3355 | } |
| 3356 | static int __init scifa_early_console_setup(struct earlycon_device *device, |
| 3357 | const char *opt) |
| 3358 | { |
| 3359 | return early_console_setup(device, PORT_SCIFA); |
| 3360 | } |
| 3361 | static int __init scifb_early_console_setup(struct earlycon_device *device, |
| 3362 | const char *opt) |
| 3363 | { |
| 3364 | return early_console_setup(device, PORT_SCIFB); |
| 3365 | } |
| 3366 | static int __init hscif_early_console_setup(struct earlycon_device *device, |
| 3367 | const char *opt) |
| 3368 | { |
| 3369 | return early_console_setup(device, PORT_HSCIF); |
| 3370 | } |
| 3371 | |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3372 | OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3373 | OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3374 | OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3375 | OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup); |
Yoshinori Sato | 0b0cced | 2015-12-24 11:24:48 +0100 | [diff] [blame] | 3376 | OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup); |
| 3377 | #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */ |
| 3378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | module_init(sci_init); |
| 3380 | module_exit(sci_exit); |
| 3381 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 3382 | MODULE_LICENSE("GPL"); |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 3383 | MODULE_ALIAS("platform:sh-sci"); |
Paul Mundt | 7f405f9 | 2011-06-28 13:47:40 +0900 | [diff] [blame] | 3384 | MODULE_AUTHOR("Paul Mundt"); |
Ulrich Hecht | f303b36 | 2013-05-31 17:57:01 +0200 | [diff] [blame] | 3385 | MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver"); |