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