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