blob: 70f005f37de085f404e483b5e6daab8f2279b42b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
Paul Mundtf43dc232011-01-13 15:06:28 +09004 * Copyright (C) 2002 - 2011 Paul Mundt
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01005 * Copyright (C) 2015 Glider bvba
Markus Brunner3ea6bc32007-08-20 08:59:33 +09006 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 Dammd89ddd12007-07-25 11:42:56 +090015 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
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 Mundt0b3d4ef2007-03-14 13:22:37 +090021#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#undef DEBUG
26
Paul Mundt85f094e2008-04-25 16:04:20 +090027#include <linux/clk.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010028#include <linux/console.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090029#include <linux/ctype.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010030#include <linux/cpufreq.h>
31#include <linux/delay.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090032#include <linux/dmaengine.h>
Magnus Damm5beabc72011-08-02 09:42:54 +000033#include <linux/dma-mapping.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010034#include <linux/err.h>
35#include <linux/errno.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010036#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>
Bastian Hecht20bdcab2013-12-06 10:59:54 +010042#include <linux/of.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010043#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/scatterlist.h>
46#include <linux/serial.h>
47#include <linux/serial_sci.h>
48#include <linux/sh_dma.h>
49#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/sysrq.h>
52#include <linux/timer.h>
53#include <linux/tty.h>
54#include <linux/tty_flip.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090055
56#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090057#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "sh-sci.h"
61
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +010062/* Offsets into the sci_port->irqs array */
63enum {
64 SCIx_ERI_IRQ,
65 SCIx_RXI_IRQ,
66 SCIx_TXI_IRQ,
67 SCIx_BRI_IRQ,
68 SCIx_NR_IRQS,
69
70 SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
71};
72
73#define SCIx_IRQ_IS_MUXED(port) \
74 ((port)->irqs[SCIx_ERI_IRQ] == \
75 (port)->irqs[SCIx_RXI_IRQ]) || \
76 ((port)->irqs[SCIx_ERI_IRQ] && \
77 ((port)->irqs[SCIx_RXI_IRQ] < 0))
78
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +010079enum SCI_CLKS {
80 SCI_FCK, /* Functional Clock */
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +010081 SCI_SCK, /* Optional External Clock */
Geert Uytterhoeven1270f862015-11-18 11:25:53 +010082 SCI_BRG_INT, /* Optional BRG Internal Clock Source */
83 SCI_SCIF_CLK, /* Optional BRG External Clock Source */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +010084 SCI_NUM_CLKS
85};
86
Paul Mundte108b2c2006-09-27 16:32:13 +090087struct sci_port {
88 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Paul Mundtce6738b2011-01-19 15:24:40 +090090 /* Platform configuration */
91 struct plat_sci_port *cfg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +020092 unsigned int overrun_reg;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +020093 unsigned int overrun_mask;
Laurent Pinchart3ae988d2013-12-06 10:59:17 +010094 unsigned int error_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +020095 unsigned int error_clear;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +010096 unsigned int sampling_rate;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +090097 resource_size_t reg_size;
Paul Mundte108b2c2006-09-27 16:32:13 +090098
Paul Mundte108b2c2006-09-27 16:32:13 +090099 /* Break timer */
100 struct timer_list break_timer;
101 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900102
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100103 /* Clocks */
104 struct clk *clks[SCI_NUM_CLKS];
105 unsigned long clk_rates[SCI_NUM_CLKS];
Paul Mundtedad1f22009-11-25 16:23:35 +0900106
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +0100107 int irqs[SCIx_NR_IRQS];
Paul Mundt9174fc82011-06-28 15:25:36 +0900108 char *irqstr[SCIx_NR_IRQS];
109
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900110 struct dma_chan *chan_tx;
111 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +0900112
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900113#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900114 dma_cookie_t cookie_tx;
115 dma_cookie_t cookie_rx[2];
116 dma_cookie_t active_rx;
Geert Uytterhoeven79904422015-08-21 20:02:42 +0200117 dma_addr_t tx_dma_addr;
118 unsigned int tx_dma_len;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900119 struct scatterlist sg_rx[2];
Yoshihiro Shimoda7b39d902015-08-21 20:02:54 +0200120 void *rx_buf[2];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900121 size_t buf_len_rx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900122 struct work_struct work_tx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900123 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000124 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900125#endif
Paul Mundte108b2c2006-09-27 16:32:13 +0900126};
127
Paul Mundte108b2c2006-09-27 16:32:13 +0900128#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
129
130static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static struct uart_driver sci_uart_driver;
132
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900133static inline struct sci_port *
134to_sci_port(struct uart_port *uart)
135{
136 return container_of(uart, struct sci_port, port);
137}
138
Paul Mundt61a69762011-06-14 12:40:19 +0900139struct plat_sci_reg {
140 u8 offset, size;
141};
142
143/* Helper for invalidating specific entries of an inherited map. */
144#define sci_reg_invalid { .offset = 0, .size = 0 }
145
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200146static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
Paul Mundt61a69762011-06-14 12:40:19 +0900147 [SCIx_PROBE_REGTYPE] = {
148 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
149 },
150
151 /*
152 * Common SCI definitions, dependent on the port's regshift
153 * value.
154 */
155 [SCIx_SCI_REGTYPE] = {
156 [SCSMR] = { 0x00, 8 },
157 [SCBRR] = { 0x01, 8 },
158 [SCSCR] = { 0x02, 8 },
159 [SCxTDR] = { 0x03, 8 },
160 [SCxSR] = { 0x04, 8 },
161 [SCxRDR] = { 0x05, 8 },
162 [SCFCR] = sci_reg_invalid,
163 [SCFDR] = sci_reg_invalid,
164 [SCTFDR] = sci_reg_invalid,
165 [SCRFDR] = sci_reg_invalid,
166 [SCSPTR] = sci_reg_invalid,
167 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200168 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200169 [SCPCR] = sci_reg_invalid,
170 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100171 [SCDL] = sci_reg_invalid,
172 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900173 },
174
175 /*
176 * Common definitions for legacy IrDA ports, dependent on
177 * regshift value.
178 */
179 [SCIx_IRDA_REGTYPE] = {
180 [SCSMR] = { 0x00, 8 },
181 [SCBRR] = { 0x01, 8 },
182 [SCSCR] = { 0x02, 8 },
183 [SCxTDR] = { 0x03, 8 },
184 [SCxSR] = { 0x04, 8 },
185 [SCxRDR] = { 0x05, 8 },
186 [SCFCR] = { 0x06, 8 },
187 [SCFDR] = { 0x07, 16 },
188 [SCTFDR] = sci_reg_invalid,
189 [SCRFDR] = sci_reg_invalid,
190 [SCSPTR] = sci_reg_invalid,
191 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200192 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200193 [SCPCR] = sci_reg_invalid,
194 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100195 [SCDL] = sci_reg_invalid,
196 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900197 },
198
199 /*
200 * Common SCIFA definitions.
201 */
202 [SCIx_SCIFA_REGTYPE] = {
203 [SCSMR] = { 0x00, 16 },
204 [SCBRR] = { 0x04, 8 },
205 [SCSCR] = { 0x08, 16 },
206 [SCxTDR] = { 0x20, 8 },
207 [SCxSR] = { 0x14, 16 },
208 [SCxRDR] = { 0x24, 8 },
209 [SCFCR] = { 0x18, 16 },
210 [SCFDR] = { 0x1c, 16 },
211 [SCTFDR] = sci_reg_invalid,
212 [SCRFDR] = sci_reg_invalid,
213 [SCSPTR] = sci_reg_invalid,
214 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200215 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200216 [SCPCR] = { 0x30, 16 },
217 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100218 [SCDL] = sci_reg_invalid,
219 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900220 },
221
222 /*
223 * Common SCIFB definitions.
224 */
225 [SCIx_SCIFB_REGTYPE] = {
226 [SCSMR] = { 0x00, 16 },
227 [SCBRR] = { 0x04, 8 },
228 [SCSCR] = { 0x08, 16 },
229 [SCxTDR] = { 0x40, 8 },
230 [SCxSR] = { 0x14, 16 },
231 [SCxRDR] = { 0x60, 8 },
232 [SCFCR] = { 0x18, 16 },
Takashi Yoshii8c66d6d2012-11-16 10:53:31 +0900233 [SCFDR] = sci_reg_invalid,
234 [SCTFDR] = { 0x38, 16 },
235 [SCRFDR] = { 0x3c, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900236 [SCSPTR] = sci_reg_invalid,
237 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200238 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200239 [SCPCR] = { 0x30, 16 },
240 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100241 [SCDL] = sci_reg_invalid,
242 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900243 },
244
245 /*
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100246 * Common SH-2(A) SCIF definitions for ports with FIFO data
247 * count registers.
248 */
249 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
250 [SCSMR] = { 0x00, 16 },
251 [SCBRR] = { 0x04, 8 },
252 [SCSCR] = { 0x08, 16 },
253 [SCxTDR] = { 0x0c, 8 },
254 [SCxSR] = { 0x10, 16 },
255 [SCxRDR] = { 0x14, 8 },
256 [SCFCR] = { 0x18, 16 },
257 [SCFDR] = { 0x1c, 16 },
258 [SCTFDR] = sci_reg_invalid,
259 [SCRFDR] = sci_reg_invalid,
260 [SCSPTR] = { 0x20, 16 },
261 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200262 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200263 [SCPCR] = sci_reg_invalid,
264 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100265 [SCDL] = sci_reg_invalid,
266 [SCCKS] = sci_reg_invalid,
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100267 },
268
269 /*
Paul Mundt61a69762011-06-14 12:40:19 +0900270 * Common SH-3 SCIF definitions.
271 */
272 [SCIx_SH3_SCIF_REGTYPE] = {
273 [SCSMR] = { 0x00, 8 },
274 [SCBRR] = { 0x02, 8 },
275 [SCSCR] = { 0x04, 8 },
276 [SCxTDR] = { 0x06, 8 },
277 [SCxSR] = { 0x08, 16 },
278 [SCxRDR] = { 0x0a, 8 },
279 [SCFCR] = { 0x0c, 8 },
280 [SCFDR] = { 0x0e, 16 },
281 [SCTFDR] = sci_reg_invalid,
282 [SCRFDR] = sci_reg_invalid,
283 [SCSPTR] = sci_reg_invalid,
284 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200285 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200286 [SCPCR] = sci_reg_invalid,
287 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100288 [SCDL] = sci_reg_invalid,
289 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900290 },
291
292 /*
293 * Common SH-4(A) SCIF(B) definitions.
294 */
295 [SCIx_SH4_SCIF_REGTYPE] = {
296 [SCSMR] = { 0x00, 16 },
297 [SCBRR] = { 0x04, 8 },
298 [SCSCR] = { 0x08, 16 },
299 [SCxTDR] = { 0x0c, 8 },
300 [SCxSR] = { 0x10, 16 },
301 [SCxRDR] = { 0x14, 8 },
302 [SCFCR] = { 0x18, 16 },
303 [SCFDR] = { 0x1c, 16 },
304 [SCTFDR] = sci_reg_invalid,
305 [SCRFDR] = sci_reg_invalid,
306 [SCSPTR] = { 0x20, 16 },
307 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200308 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200309 [SCPCR] = sci_reg_invalid,
310 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100311 [SCDL] = sci_reg_invalid,
312 [SCCKS] = sci_reg_invalid,
313 },
314
315 /*
316 * Common SCIF definitions for ports with a Baud Rate Generator for
317 * External Clock (BRG).
318 */
319 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
320 [SCSMR] = { 0x00, 16 },
321 [SCBRR] = { 0x04, 8 },
322 [SCSCR] = { 0x08, 16 },
323 [SCxTDR] = { 0x0c, 8 },
324 [SCxSR] = { 0x10, 16 },
325 [SCxRDR] = { 0x14, 8 },
326 [SCFCR] = { 0x18, 16 },
327 [SCFDR] = { 0x1c, 16 },
328 [SCTFDR] = sci_reg_invalid,
329 [SCRFDR] = sci_reg_invalid,
330 [SCSPTR] = { 0x20, 16 },
331 [SCLSR] = { 0x24, 16 },
332 [HSSRR] = sci_reg_invalid,
333 [SCPCR] = sci_reg_invalid,
334 [SCPDR] = sci_reg_invalid,
335 [SCDL] = { 0x30, 16 },
336 [SCCKS] = { 0x34, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200337 },
338
339 /*
340 * Common HSCIF definitions.
341 */
342 [SCIx_HSCIF_REGTYPE] = {
343 [SCSMR] = { 0x00, 16 },
344 [SCBRR] = { 0x04, 8 },
345 [SCSCR] = { 0x08, 16 },
346 [SCxTDR] = { 0x0c, 8 },
347 [SCxSR] = { 0x10, 16 },
348 [SCxRDR] = { 0x14, 8 },
349 [SCFCR] = { 0x18, 16 },
350 [SCFDR] = { 0x1c, 16 },
351 [SCTFDR] = sci_reg_invalid,
352 [SCRFDR] = sci_reg_invalid,
353 [SCSPTR] = { 0x20, 16 },
354 [SCLSR] = { 0x24, 16 },
355 [HSSRR] = { 0x40, 16 },
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200356 [SCPCR] = sci_reg_invalid,
357 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100358 [SCDL] = { 0x30, 16 },
359 [SCCKS] = { 0x34, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900360 },
361
362 /*
363 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
364 * register.
365 */
366 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
367 [SCSMR] = { 0x00, 16 },
368 [SCBRR] = { 0x04, 8 },
369 [SCSCR] = { 0x08, 16 },
370 [SCxTDR] = { 0x0c, 8 },
371 [SCxSR] = { 0x10, 16 },
372 [SCxRDR] = { 0x14, 8 },
373 [SCFCR] = { 0x18, 16 },
374 [SCFDR] = { 0x1c, 16 },
375 [SCTFDR] = sci_reg_invalid,
376 [SCRFDR] = sci_reg_invalid,
377 [SCSPTR] = sci_reg_invalid,
378 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200379 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200380 [SCPCR] = sci_reg_invalid,
381 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100382 [SCDL] = sci_reg_invalid,
383 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900384 },
385
386 /*
387 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
388 * count registers.
389 */
390 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
391 [SCSMR] = { 0x00, 16 },
392 [SCBRR] = { 0x04, 8 },
393 [SCSCR] = { 0x08, 16 },
394 [SCxTDR] = { 0x0c, 8 },
395 [SCxSR] = { 0x10, 16 },
396 [SCxRDR] = { 0x14, 8 },
397 [SCFCR] = { 0x18, 16 },
398 [SCFDR] = { 0x1c, 16 },
399 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
400 [SCRFDR] = { 0x20, 16 },
401 [SCSPTR] = { 0x24, 16 },
402 [SCLSR] = { 0x28, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200403 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200404 [SCPCR] = sci_reg_invalid,
405 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100406 [SCDL] = sci_reg_invalid,
407 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900408 },
409
410 /*
411 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
412 * registers.
413 */
414 [SCIx_SH7705_SCIF_REGTYPE] = {
415 [SCSMR] = { 0x00, 16 },
416 [SCBRR] = { 0x04, 8 },
417 [SCSCR] = { 0x08, 16 },
418 [SCxTDR] = { 0x20, 8 },
419 [SCxSR] = { 0x14, 16 },
420 [SCxRDR] = { 0x24, 8 },
421 [SCFCR] = { 0x18, 16 },
422 [SCFDR] = { 0x1c, 16 },
423 [SCTFDR] = sci_reg_invalid,
424 [SCRFDR] = sci_reg_invalid,
425 [SCSPTR] = sci_reg_invalid,
426 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200427 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200428 [SCPCR] = sci_reg_invalid,
429 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100430 [SCDL] = sci_reg_invalid,
431 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900432 },
433};
434
Paul Mundt72b294c2011-06-14 17:38:19 +0900435#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
436
Paul Mundt61a69762011-06-14 12:40:19 +0900437/*
438 * The "offset" here is rather misleading, in that it refers to an enum
439 * value relative to the port mapping rather than the fixed offset
440 * itself, which needs to be manually retrieved from the platform's
441 * register map for the given port.
442 */
443static unsigned int sci_serial_in(struct uart_port *p, int offset)
444{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200445 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900446
447 if (reg->size == 8)
448 return ioread8(p->membase + (reg->offset << p->regshift));
449 else if (reg->size == 16)
450 return ioread16(p->membase + (reg->offset << p->regshift));
451 else
452 WARN(1, "Invalid register access\n");
453
454 return 0;
455}
456
457static void sci_serial_out(struct uart_port *p, int offset, int value)
458{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200459 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900460
461 if (reg->size == 8)
462 iowrite8(value, p->membase + (reg->offset << p->regshift));
463 else if (reg->size == 16)
464 iowrite16(value, p->membase + (reg->offset << p->regshift));
465 else
466 WARN(1, "Invalid register access\n");
467}
468
Paul Mundt61a69762011-06-14 12:40:19 +0900469static int sci_probe_regmap(struct plat_sci_port *cfg)
470{
471 switch (cfg->type) {
472 case PORT_SCI:
473 cfg->regtype = SCIx_SCI_REGTYPE;
474 break;
475 case PORT_IRDA:
476 cfg->regtype = SCIx_IRDA_REGTYPE;
477 break;
478 case PORT_SCIFA:
479 cfg->regtype = SCIx_SCIFA_REGTYPE;
480 break;
481 case PORT_SCIFB:
482 cfg->regtype = SCIx_SCIFB_REGTYPE;
483 break;
484 case PORT_SCIF:
485 /*
486 * The SH-4 is a bit of a misnomer here, although that's
487 * where this particular port layout originated. This
488 * configuration (or some slight variation thereof)
489 * remains the dominant model for all SCIFs.
490 */
491 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
492 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +0200493 case PORT_HSCIF:
494 cfg->regtype = SCIx_HSCIF_REGTYPE;
495 break;
Paul Mundt61a69762011-06-14 12:40:19 +0900496 default:
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +0100497 pr_err("Can't probe register map for given port\n");
Paul Mundt61a69762011-06-14 12:40:19 +0900498 return -EINVAL;
499 }
500
501 return 0;
502}
503
Paul Mundt23241d42011-06-28 13:55:31 +0900504static void sci_port_enable(struct sci_port *sci_port)
505{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100506 unsigned int i;
507
Paul Mundt23241d42011-06-28 13:55:31 +0900508 if (!sci_port->port.dev)
509 return;
510
511 pm_runtime_get_sync(sci_port->port.dev);
512
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100513 for (i = 0; i < SCI_NUM_CLKS; i++) {
514 clk_prepare_enable(sci_port->clks[i]);
515 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
516 }
517 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
Paul Mundt23241d42011-06-28 13:55:31 +0900518}
519
520static void sci_port_disable(struct sci_port *sci_port)
521{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100522 unsigned int i;
523
Paul Mundt23241d42011-06-28 13:55:31 +0900524 if (!sci_port->port.dev)
525 return;
526
Laurent Pinchartcaec7032013-11-28 18:11:45 +0100527 /* Cancel the break timer to ensure that the timer handler will not try
528 * to access the hardware with clocks and power disabled. Reset the
529 * break flag to make the break debouncing state machine ready for the
530 * next break.
531 */
532 del_timer_sync(&sci_port->break_timer);
533 sci_port->break_flag = 0;
534
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100535 for (i = SCI_NUM_CLKS; i-- > 0; )
536 clk_disable_unprepare(sci_port->clks[i]);
Paul Mundt23241d42011-06-28 13:55:31 +0900537
538 pm_runtime_put_sync(sci_port->port.dev);
539}
540
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +0200541static inline unsigned long port_rx_irq_mask(struct uart_port *port)
542{
543 /*
544 * Not all ports (such as SCIFA) will support REIE. Rather than
545 * special-casing the port type, we check the port initialization
546 * IRQ enable mask to see whether the IRQ is desired at all. If
547 * it's unset, it's logically inferred that there's no point in
548 * testing for it.
549 */
550 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
551}
552
553static void sci_start_tx(struct uart_port *port)
554{
555 struct sci_port *s = to_sci_port(port);
556 unsigned short ctrl;
557
558#ifdef CONFIG_SERIAL_SH_SCI_DMA
559 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
560 u16 new, scr = serial_port_in(port, SCSCR);
561 if (s->chan_tx)
562 new = scr | SCSCR_TDRQE;
563 else
564 new = scr & ~SCSCR_TDRQE;
565 if (new != scr)
566 serial_port_out(port, SCSCR, new);
567 }
568
569 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
570 dma_submit_error(s->cookie_tx)) {
571 s->cookie_tx = 0;
572 schedule_work(&s->work_tx);
573 }
574#endif
575
576 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
577 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
578 ctrl = serial_port_in(port, SCSCR);
579 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
580 }
581}
582
583static void sci_stop_tx(struct uart_port *port)
584{
585 unsigned short ctrl;
586
587 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
588 ctrl = serial_port_in(port, SCSCR);
589
590 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
591 ctrl &= ~SCSCR_TDRQE;
592
593 ctrl &= ~SCSCR_TIE;
594
595 serial_port_out(port, SCSCR, ctrl);
596}
597
598static void sci_start_rx(struct uart_port *port)
599{
600 unsigned short ctrl;
601
602 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
603
604 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
605 ctrl &= ~SCSCR_RDRQE;
606
607 serial_port_out(port, SCSCR, ctrl);
608}
609
610static void sci_stop_rx(struct uart_port *port)
611{
612 unsigned short ctrl;
613
614 ctrl = serial_port_in(port, SCSCR);
615
616 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
617 ctrl &= ~SCSCR_RDRQE;
618
619 ctrl &= ~port_rx_irq_mask(port);
620
621 serial_port_out(port, SCSCR, ctrl);
622}
623
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200624static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
625{
626 if (port->type == PORT_SCI) {
627 /* Just store the mask */
628 serial_port_out(port, SCxSR, mask);
629 } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
630 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
631 /* Only clear the status bits we want to clear */
632 serial_port_out(port, SCxSR,
633 serial_port_in(port, SCxSR) & mask);
634 } else {
635 /* Store the mask, clear parity/framing errors */
636 serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
637 }
638}
639
Yoshinori Sato0b0cced2015-12-24 11:24:48 +0100640#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
641 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900642
643#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900644static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned short status;
647 int c;
648
Paul Mundte108b2c2006-09-27 16:32:13 +0900649 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900650 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (status & SCxSR_ERRORS(port)) {
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200652 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500655 break;
656 } while (1);
657
658 if (!(status & SCxSR_RDxF(port)))
659 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900660
Paul Mundtb12bb292012-03-30 19:50:15 +0900661 c = serial_port_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900662
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900663 /* Dummy read */
Paul Mundtb12bb292012-03-30 19:50:15 +0900664 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200665 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return c;
668}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900671static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned short status;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900676 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 } while (!(status & SCxSR_TDxE(port)));
678
Paul Mundtb12bb292012-03-30 19:50:15 +0900679 serial_port_out(port, SCxTDR, c);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200680 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Yoshinori Sato0b0cced2015-12-24 11:24:48 +0100682#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
683 CONFIG_SERIAL_SH_SCI_EARLYCON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Paul Mundt61a69762011-06-14 12:40:19 +0900685static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900686{
Paul Mundt61a69762011-06-14 12:40:19 +0900687 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200688 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900689
Paul Mundt61a69762011-06-14 12:40:19 +0900690 /*
691 * Use port-specific handler if provided.
692 */
693 if (s->cfg->ops && s->cfg->ops->init_pins) {
694 s->cfg->ops->init_pins(port, cflag);
695 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Paul Mundt61a69762011-06-14 12:40:19 +0900698 /*
699 * For the generic path SCSPTR is necessary. Bail out if that's
700 * unavailable, too.
701 */
702 if (!reg->size)
703 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800704
Paul Mundtfaf02f82011-12-02 17:44:50 +0900705 if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
706 ((!(cflag & CRTSCTS)))) {
707 unsigned short status;
708
Paul Mundtb12bb292012-03-30 19:50:15 +0900709 status = serial_port_in(port, SCSPTR);
Paul Mundtfaf02f82011-12-02 17:44:50 +0900710 status &= ~SCSPTR_CTSIO;
711 status |= SCSPTR_RTSIO;
Paul Mundtb12bb292012-03-30 19:50:15 +0900712 serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
Paul Mundtfaf02f82011-12-02 17:44:50 +0900713 }
Paul Mundtd5701642008-12-16 20:07:27 +0900714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900716static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900717{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200718 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900719
720 reg = sci_getreg(port, SCTFDR);
721 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900722 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900723
724 reg = sci_getreg(port, SCFDR);
725 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900726 return serial_port_in(port, SCFDR) >> 8;
Paul Mundt72b294c2011-06-14 17:38:19 +0900727
Paul Mundtb12bb292012-03-30 19:50:15 +0900728 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900729}
730
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900731static int sci_txroom(struct uart_port *port)
732{
Paul Mundt72b294c2011-06-14 17:38:19 +0900733 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900734}
735
736static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900737{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200738 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900739
740 reg = sci_getreg(port, SCRFDR);
741 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900742 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900743
744 reg = sci_getreg(port, SCFDR);
745 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900746 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900747
Paul Mundtb12bb292012-03-30 19:50:15 +0900748 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900749}
750
Paul Mundt514820e2011-06-08 18:51:32 +0900751/*
752 * SCI helper for checking the state of the muxed port/RXD pins.
753 */
754static inline int sci_rxd_in(struct uart_port *port)
755{
756 struct sci_port *s = to_sci_port(port);
757
758 if (s->cfg->port_reg <= 0)
759 return 1;
760
Paul Mundt0dd4d5c2012-10-15 14:08:48 +0900761 /* Cast for ARM damage */
Laurent Pincharte2afca62013-12-11 13:40:31 +0100762 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
Paul Mundt514820e2011-06-08 18:51:32 +0900763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/* ********************************************************************** *
766 * the interrupt related routines *
767 * ********************************************************************** */
768
769static void sci_transmit_chars(struct uart_port *port)
770{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700771 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 unsigned short status;
774 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900775 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Paul Mundtb12bb292012-03-30 19:50:15 +0900777 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!(status & SCxSR_TDxE(port))) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900779 ctrl = serial_port_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900780 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900781 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900782 else
Paul Mundt8e698612009-06-24 19:44:32 +0900783 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900784 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return;
786 }
787
Paul Mundt72b294c2011-06-14 17:38:19 +0900788 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 do {
791 unsigned char c;
792
793 if (port->x_char) {
794 c = port->x_char;
795 port->x_char = 0;
796 } else if (!uart_circ_empty(xmit) && !stopped) {
797 c = xmit->buf[xmit->tail];
798 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
799 } else {
800 break;
801 }
802
Paul Mundtb12bb292012-03-30 19:50:15 +0900803 serial_port_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 port->icount.tx++;
806 } while (--count > 0);
807
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200808 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
811 uart_write_wakeup(port);
812 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100813 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900815 ctrl = serial_port_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900817 if (port->type != PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900818 serial_port_in(port, SCxSR); /* Dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200819 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Paul Mundt8e698612009-06-24 19:44:32 +0900822 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900823 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
827/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900828#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900830static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900832 struct sci_port *sci_port = to_sci_port(port);
Jiri Slaby227434f2013-01-03 15:53:01 +0100833 struct tty_port *tport = &port->state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int i, count, copied = 0;
835 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800836 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Paul Mundtb12bb292012-03-30 19:50:15 +0900838 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!(status & SCxSR_RDxF(port)))
840 return;
841
842 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Don't copy more bytes than there is room for in the buffer */
Jiri Slaby227434f2013-01-03 15:53:01 +0100844 count = tty_buffer_request_room(tport, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* If for any reason we can't copy more data, we're done! */
847 if (count == 0)
848 break;
849
850 if (port->type == PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900851 char c = serial_port_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900852 if (uart_handle_sysrq_char(port, c) ||
853 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900855 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100856 tty_insert_flip_char(tport, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900858 for (i = 0; i < count; i++) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900859 char c = serial_port_in(port, SCxRDR);
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900860
Paul Mundtb12bb292012-03-30 19:50:15 +0900861 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#if defined(CONFIG_CPU_SH3)
863 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900864 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if ((c == 0) &&
866 (status & SCxSR_FER(port))) {
867 count--; i--;
868 continue;
869 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900872 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900873 sci_port->break_flag = 0;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (STEPFN(c)) {
876 count--; i--;
877 continue;
878 }
879 }
880#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100881 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 count--; i--;
883 continue;
884 }
885
886 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900887 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800888 flag = TTY_FRAME;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900889 port->icount.frame++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900890 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900891 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800892 flag = TTY_PARITY;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900893 port->icount.parity++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900894 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800895 } else
896 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900897
Jiri Slaby92a19f92013-01-03 15:53:03 +0100898 tty_insert_flip_char(tport, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 }
901
Paul Mundtb12bb292012-03-30 19:50:15 +0900902 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200903 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 copied += count;
906 port->icount.rx += count;
907 }
908
909 if (copied) {
910 /* Tell the rest of the system the news. New characters! */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100911 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900913 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200914 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916}
917
918#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900919
920/*
921 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * 1 per millisecond or so during the break period, for 9600 baud.
923 * So dont bother disabling interrupts.
924 * But dont want more than 1 break event.
925 * Use a kernel timer to periodically poll the rx line until
926 * the break is finished.
927 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900928static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900930 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/* Ensure that two consecutive samples find the break over. */
934static void sci_break_timer(unsigned long data)
935{
Paul Mundte108b2c2006-09-27 16:32:13 +0900936 struct sci_port *port = (struct sci_port *)data;
937
938 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900940 sci_schedule_break_timer(port);
941 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* break is over. */
943 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900944 sci_schedule_break_timer(port);
945 } else
946 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900949static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900952 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100953 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900954 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100956 /* Handle overruns */
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +0200957 if (status & s->overrun_mask) {
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100958 port->icount.overrun++;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900959
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100960 /* overrun error */
961 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
962 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900963
Joe Perches9b971cd2014-03-11 10:10:46 -0700964 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Paul Mundte108b2c2006-09-27 16:32:13 +0900967 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (sci_rxd_in(port) == 0) {
969 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900970 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900971
972 if (!sci_port->break_flag) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900973 port->icount.brk++;
974
Paul Mundte108b2c2006-09-27 16:32:13 +0900975 sci_port->break_flag = 1;
976 sci_schedule_break_timer(sci_port);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900979 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900981
982 dev_dbg(port->dev, "BREAK detected\n");
983
Jiri Slaby92a19f92013-01-03 15:53:03 +0100984 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900985 copied++;
986 }
987
Paul Mundte108b2c2006-09-27 16:32:13 +0900988 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* frame error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900990 port->icount.frame++;
991
Jiri Slaby92a19f92013-01-03 15:53:03 +0100992 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800993 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900994
995 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998
Paul Mundte108b2c2006-09-27 16:32:13 +0900999 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* parity error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001001 port->icount.parity++;
1002
Jiri Slaby92a19f92013-01-03 15:53:03 +01001003 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
Paul Mundte108b2c2006-09-27 16:32:13 +09001004 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001005
Joe Perches9b971cd2014-03-11 10:10:46 -07001006 dev_notice(port->dev, "parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Alan Cox33f0f882006-01-09 20:54:13 -08001009 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001010 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 return copied;
1013}
1014
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001015static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +09001016{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001017 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +09001018 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001019 const struct plat_sci_reg *reg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001020 int copied = 0;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001021 u16 status;
Paul Mundtd830fa42008-12-16 19:29:38 +09001022
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001023 reg = sci_getreg(port, s->overrun_reg);
Paul Mundt4b8c59a2011-06-14 17:53:34 +09001024 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +09001025 return 0;
1026
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001027 status = serial_port_in(port, s->overrun_reg);
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001028 if (status & s->overrun_mask) {
1029 status &= ~s->overrun_mask;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001030 serial_port_out(port, s->overrun_reg, status);
Paul Mundtd830fa42008-12-16 19:29:38 +09001031
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001032 port->icount.overrun++;
1033
Jiri Slaby92a19f92013-01-03 15:53:03 +01001034 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001035 tty_flip_buffer_push(tport);
Paul Mundtd830fa42008-12-16 19:29:38 +09001036
Yoshihiro Kaneko51b31f12015-01-26 20:53:29 +09001037 dev_dbg(port->dev, "overrun error\n");
Paul Mundtd830fa42008-12-16 19:29:38 +09001038 copied++;
1039 }
1040
1041 return copied;
1042}
1043
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001044static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +09001047 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +01001048 struct tty_port *tport = &port->state->port;
Magnus Damma5660ad2009-01-21 15:14:38 +00001049 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Paul Mundt0b3d4ef2007-03-14 13:22:37 +09001051 if (uart_handle_break(port))
1052 return 0;
1053
Paul Mundtb7a76e42006-02-01 03:06:06 -08001054 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#if defined(CONFIG_CPU_SH3)
1056 /* Debounce break */
1057 s->break_flag = 1;
1058#endif
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001059
1060 port->icount.brk++;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Notify of BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001063 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -08001064 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001065
1066 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Alan Cox33f0f882006-01-09 20:54:13 -08001069 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001070 tty_flip_buffer_push(tport);
Paul Mundte108b2c2006-09-27 16:32:13 +09001071
Paul Mundtd830fa42008-12-16 19:29:38 +09001072 copied += sci_handle_fifo_overrun(port);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return copied;
1075}
1076
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001077#ifdef CONFIG_SERIAL_SH_SCI_DMA
1078static void sci_dma_tx_complete(void *arg)
1079{
1080 struct sci_port *s = arg;
1081 struct uart_port *port = &s->port;
1082 struct circ_buf *xmit = &port->state->xmit;
1083 unsigned long flags;
1084
1085 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1086
1087 spin_lock_irqsave(&port->lock, flags);
1088
1089 xmit->tail += s->tx_dma_len;
1090 xmit->tail &= UART_XMIT_SIZE - 1;
1091
1092 port->icount.tx += s->tx_dma_len;
1093
1094 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1095 uart_write_wakeup(port);
1096
1097 if (!uart_circ_empty(xmit)) {
1098 s->cookie_tx = 0;
1099 schedule_work(&s->work_tx);
1100 } else {
1101 s->cookie_tx = -EINVAL;
1102 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1103 u16 ctrl = serial_port_in(port, SCSCR);
1104 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1105 }
1106 }
1107
1108 spin_unlock_irqrestore(&port->lock, flags);
1109}
1110
1111/* Locking: called with port lock held */
1112static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1113{
1114 struct uart_port *port = &s->port;
1115 struct tty_port *tport = &port->state->port;
1116 int copied;
1117
1118 copied = tty_insert_flip_string(tport, buf, count);
1119 if (copied < count) {
1120 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
1121 count - copied);
1122 port->icount.buf_overrun++;
1123 }
1124
1125 port->icount.rx += copied;
1126
1127 return copied;
1128}
1129
1130static int sci_dma_rx_find_active(struct sci_port *s)
1131{
1132 unsigned int i;
1133
1134 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1135 if (s->active_rx == s->cookie_rx[i])
1136 return i;
1137
1138 dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
1139 s->active_rx);
1140 return -1;
1141}
1142
1143static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1144{
1145 struct dma_chan *chan = s->chan_rx;
1146 struct uart_port *port = &s->port;
1147 unsigned long flags;
1148
1149 spin_lock_irqsave(&port->lock, flags);
1150 s->chan_rx = NULL;
1151 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1152 spin_unlock_irqrestore(&port->lock, flags);
1153 dmaengine_terminate_all(chan);
1154 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1155 sg_dma_address(&s->sg_rx[0]));
1156 dma_release_channel(chan);
1157 if (enable_pio)
1158 sci_start_rx(port);
1159}
1160
1161static void sci_dma_rx_complete(void *arg)
1162{
1163 struct sci_port *s = arg;
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001164 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001165 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001166 struct dma_async_tx_descriptor *desc;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001167 unsigned long flags;
1168 int active, count = 0;
1169
1170 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1171 s->active_rx);
1172
1173 spin_lock_irqsave(&port->lock, flags);
1174
1175 active = sci_dma_rx_find_active(s);
1176 if (active >= 0)
1177 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1178
1179 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1180
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001181 if (count)
1182 tty_flip_buffer_push(&port->state->port);
1183
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001184 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1185 DMA_DEV_TO_MEM,
1186 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1187 if (!desc)
1188 goto fail;
1189
1190 desc->callback = sci_dma_rx_complete;
1191 desc->callback_param = s;
1192 s->cookie_rx[active] = dmaengine_submit(desc);
1193 if (dma_submit_error(s->cookie_rx[active]))
1194 goto fail;
1195
1196 s->active_rx = s->cookie_rx[!active];
1197
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001198 dma_async_issue_pending(chan);
1199
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001200 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1201 __func__, s->cookie_rx[active], active, s->active_rx);
1202 spin_unlock_irqrestore(&port->lock, flags);
1203 return;
1204
1205fail:
1206 spin_unlock_irqrestore(&port->lock, flags);
1207 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1208 sci_rx_dma_release(s, true);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001209}
1210
1211static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1212{
1213 struct dma_chan *chan = s->chan_tx;
1214 struct uart_port *port = &s->port;
1215 unsigned long flags;
1216
1217 spin_lock_irqsave(&port->lock, flags);
1218 s->chan_tx = NULL;
1219 s->cookie_tx = -EINVAL;
1220 spin_unlock_irqrestore(&port->lock, flags);
1221 dmaengine_terminate_all(chan);
1222 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1223 DMA_TO_DEVICE);
1224 dma_release_channel(chan);
1225 if (enable_pio)
1226 sci_start_tx(port);
1227}
1228
1229static void sci_submit_rx(struct sci_port *s)
1230{
1231 struct dma_chan *chan = s->chan_rx;
1232 int i;
1233
1234 for (i = 0; i < 2; i++) {
1235 struct scatterlist *sg = &s->sg_rx[i];
1236 struct dma_async_tx_descriptor *desc;
1237
1238 desc = dmaengine_prep_slave_sg(chan,
1239 sg, 1, DMA_DEV_TO_MEM,
1240 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1241 if (!desc)
1242 goto fail;
1243
1244 desc->callback = sci_dma_rx_complete;
1245 desc->callback_param = s;
1246 s->cookie_rx[i] = dmaengine_submit(desc);
1247 if (dma_submit_error(s->cookie_rx[i]))
1248 goto fail;
1249
1250 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1251 s->cookie_rx[i], i);
1252 }
1253
1254 s->active_rx = s->cookie_rx[0];
1255
1256 dma_async_issue_pending(chan);
1257 return;
1258
1259fail:
1260 if (i)
1261 dmaengine_terminate_all(chan);
1262 for (i = 0; i < 2; i++)
1263 s->cookie_rx[i] = -EINVAL;
1264 s->active_rx = -EINVAL;
1265 dev_warn(s->port.dev, "Failed to re-start Rx DMA, using PIO\n");
1266 sci_rx_dma_release(s, true);
1267}
1268
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001269static void work_fn_tx(struct work_struct *work)
1270{
1271 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1272 struct dma_async_tx_descriptor *desc;
1273 struct dma_chan *chan = s->chan_tx;
1274 struct uart_port *port = &s->port;
1275 struct circ_buf *xmit = &port->state->xmit;
1276 dma_addr_t buf;
1277
1278 /*
1279 * DMA is idle now.
1280 * Port xmit buffer is already mapped, and it is one page... Just adjust
1281 * offsets and lengths. Since it is a circular buffer, we have to
1282 * transmit till the end, and then the rest. Take the port lock to get a
1283 * consistent xmit buffer state.
1284 */
1285 spin_lock_irq(&port->lock);
1286 buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1));
1287 s->tx_dma_len = min_t(unsigned int,
1288 CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1289 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1290 spin_unlock_irq(&port->lock);
1291
1292 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1293 DMA_MEM_TO_DEV,
1294 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1295 if (!desc) {
1296 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1297 /* switch to PIO */
1298 sci_tx_dma_release(s, true);
1299 return;
1300 }
1301
1302 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1303 DMA_TO_DEVICE);
1304
1305 spin_lock_irq(&port->lock);
1306 desc->callback = sci_dma_tx_complete;
1307 desc->callback_param = s;
1308 spin_unlock_irq(&port->lock);
1309 s->cookie_tx = dmaengine_submit(desc);
1310 if (dma_submit_error(s->cookie_tx)) {
1311 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1312 /* switch to PIO */
1313 sci_tx_dma_release(s, true);
1314 return;
1315 }
1316
1317 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
1318 __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1319
1320 dma_async_issue_pending(chan);
1321}
1322
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001323static void rx_timer_fn(unsigned long arg)
1324{
1325 struct sci_port *s = (struct sci_port *)arg;
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001326 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001327 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001328 struct dma_tx_state state;
1329 enum dma_status status;
1330 unsigned long flags;
1331 unsigned int read;
1332 int active, count;
1333 u16 scr;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001334
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001335 spin_lock_irqsave(&port->lock, flags);
1336
1337 dev_dbg(port->dev, "DMA Rx timed out\n");
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001338
1339 active = sci_dma_rx_find_active(s);
1340 if (active < 0) {
1341 spin_unlock_irqrestore(&port->lock, flags);
1342 return;
1343 }
1344
1345 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001346 if (status == DMA_COMPLETE) {
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001347 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1348 s->active_rx, active);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001349 spin_unlock_irqrestore(&port->lock, flags);
1350
1351 /* Let packet complete handler take care of the packet */
1352 return;
1353 }
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001354
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001355 dmaengine_pause(chan);
1356
1357 /*
1358 * sometimes DMA transfer doesn't stop even if it is stopped and
1359 * data keeps on coming until transaction is complete so check
1360 * for DMA_COMPLETE again
1361 * Let packet complete handler take care of the packet
1362 */
1363 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1364 if (status == DMA_COMPLETE) {
1365 spin_unlock_irqrestore(&port->lock, flags);
1366 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1367 return;
1368 }
1369
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001370 /* Handle incomplete DMA receive */
1371 dmaengine_terminate_all(s->chan_rx);
1372 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1373 dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
1374 s->active_rx);
1375
1376 if (read) {
1377 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1378 if (count)
1379 tty_flip_buffer_push(&port->state->port);
1380 }
1381
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001382 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1383 sci_submit_rx(s);
Muhammad Hamza Farooq371cfed2015-09-18 13:08:29 +02001384
1385 /* Direct new serial port interrupts back to CPU */
1386 scr = serial_port_in(port, SCSCR);
1387 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1388 scr &= ~SCSCR_RDRQE;
1389 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1390 }
1391 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1392
1393 spin_unlock_irqrestore(&port->lock, flags);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001394}
1395
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001396static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1397 enum dma_transfer_direction dir,
1398 unsigned int id)
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001399{
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001400 dma_cap_mask_t mask;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001401 struct dma_chan *chan;
1402 struct dma_slave_config cfg;
1403 int ret;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001404
1405 dma_cap_zero(mask);
1406 dma_cap_set(DMA_SLAVE, mask);
1407
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001408 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1409 (void *)(unsigned long)id, port->dev,
1410 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1411 if (!chan) {
1412 dev_warn(port->dev,
1413 "dma_request_slave_channel_compat failed\n");
1414 return NULL;
1415 }
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001416
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001417 memset(&cfg, 0, sizeof(cfg));
1418 cfg.direction = dir;
1419 if (dir == DMA_MEM_TO_DEV) {
1420 cfg.dst_addr = port->mapbase +
1421 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1422 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1423 } else {
1424 cfg.src_addr = port->mapbase +
1425 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1426 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1427 }
1428
1429 ret = dmaengine_slave_config(chan, &cfg);
1430 if (ret) {
1431 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1432 dma_release_channel(chan);
1433 return NULL;
1434 }
1435
1436 return chan;
1437}
1438
1439static void sci_request_dma(struct uart_port *port)
1440{
1441 struct sci_port *s = to_sci_port(port);
1442 struct dma_chan *chan;
1443
1444 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1445
1446 if (!port->dev->of_node &&
1447 (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
1448 return;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001449
1450 s->cookie_tx = -EINVAL;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001451 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001452 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1453 if (chan) {
1454 s->chan_tx = chan;
1455 /* UART circular tx buffer is an aligned page. */
1456 s->tx_dma_addr = dma_map_single(chan->device->dev,
1457 port->state->xmit.buf,
1458 UART_XMIT_SIZE,
1459 DMA_TO_DEVICE);
1460 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1461 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1462 dma_release_channel(chan);
1463 s->chan_tx = NULL;
1464 } else {
1465 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1466 __func__, UART_XMIT_SIZE,
1467 port->state->xmit.buf, &s->tx_dma_addr);
1468 }
1469
1470 INIT_WORK(&s->work_tx, work_fn_tx);
1471 }
1472
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001473 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001474 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1475 if (chan) {
1476 unsigned int i;
1477 dma_addr_t dma;
1478 void *buf;
1479
1480 s->chan_rx = chan;
1481
1482 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1483 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1484 &dma, GFP_KERNEL);
1485 if (!buf) {
1486 dev_warn(port->dev,
1487 "Failed to allocate Rx dma buffer, using PIO\n");
1488 dma_release_channel(chan);
1489 s->chan_rx = NULL;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001490 return;
1491 }
1492
1493 for (i = 0; i < 2; i++) {
1494 struct scatterlist *sg = &s->sg_rx[i];
1495
1496 sg_init_table(sg, 1);
1497 s->rx_buf[i] = buf;
1498 sg_dma_address(sg) = dma;
Yoshihiro Shimodad09959e2015-12-04 15:21:19 +01001499 sg_dma_len(sg) = s->buf_len_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001500
1501 buf += s->buf_len_rx;
1502 dma += s->buf_len_rx;
1503 }
1504
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001505 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1506
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001507 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1508 sci_submit_rx(s);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001509 }
1510}
1511
1512static void sci_free_dma(struct uart_port *port)
1513{
1514 struct sci_port *s = to_sci_port(port);
1515
1516 if (s->chan_tx)
1517 sci_tx_dma_release(s, false);
1518 if (s->chan_rx)
1519 sci_rx_dma_release(s, false);
1520}
1521#else
1522static inline void sci_request_dma(struct uart_port *port)
1523{
1524}
1525
1526static inline void sci_free_dma(struct uart_port *port)
1527{
1528}
1529#endif
1530
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001531static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001533#ifdef CONFIG_SERIAL_SH_SCI_DMA
1534 struct uart_port *port = ptr;
1535 struct sci_port *s = to_sci_port(port);
1536
1537 if (s->chan_rx) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001538 u16 scr = serial_port_in(port, SCSCR);
1539 u16 ssr = serial_port_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001540
1541 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001542 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001543 disable_irq_nosync(irq);
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001544 scr |= SCSCR_RDRQE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001545 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +09001546 scr &= ~SCSCR_RIE;
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001547 sci_submit_rx(s);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001548 }
Paul Mundtb12bb292012-03-30 19:50:15 +09001549 serial_port_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001550 /* Clear current interrupt */
Geert Uytterhoeven54af5002015-08-21 20:02:28 +02001551 serial_port_out(port, SCxSR,
1552 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001553 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
1554 jiffies, s->rx_timeout);
1555 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001556
1557 return IRQ_HANDLED;
1558 }
1559#endif
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* I think sci_receive_chars has to be called irrespective
1562 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1563 * to be disabled?
1564 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001565 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 return IRQ_HANDLED;
1568}
1569
David Howells7d12e782006-10-05 14:55:46 +01001570static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +09001573 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Stuart Menefyfd78a762009-07-29 23:01:24 +09001575 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +09001577 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 return IRQ_HANDLED;
1580}
1581
David Howells7d12e782006-10-05 14:55:46 +01001582static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct uart_port *port = ptr;
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001585 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /* Handle errors */
1588 if (port->type == PORT_SCI) {
1589 if (sci_handle_errors(port)) {
1590 /* discard character in rx buffer */
Paul Mundtb12bb292012-03-30 19:50:15 +09001591 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001592 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +09001595 sci_handle_fifo_overrun(port);
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001596 if (!s->chan_rx)
1597 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001600 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* Kick the transmission */
Yoshihiro Shimoda8eadb562015-08-21 20:02:56 +02001603 if (!s->chan_tx)
1604 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 return IRQ_HANDLED;
1607}
1608
David Howells7d12e782006-10-05 14:55:46 +01001609static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 struct uart_port *port = ptr;
1612
1613 /* Handle BREAKs */
1614 sci_handle_breaks(port);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001615 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 return IRQ_HANDLED;
1618}
1619
David Howells7d12e782006-10-05 14:55:46 +01001620static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001622 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
Michael Trimarchia8884e32008-10-31 16:10:23 +09001623 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001624 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +09001625 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Paul Mundtb12bb292012-03-30 19:50:15 +09001627 ssr_status = serial_port_in(port, SCxSR);
1628 scr_status = serial_port_in(port, SCSCR);
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001629 if (s->overrun_reg == SCxSR)
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001630 orer_status = ssr_status;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001631 else {
1632 if (sci_getreg(port, s->overrun_reg)->size)
1633 orer_status = serial_port_in(port, s->overrun_reg);
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001634 }
1635
Paul Mundtf43dc232011-01-13 15:06:28 +09001636 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +09001639 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001640 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001641 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001642
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001643 /*
1644 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1645 * DR flags
1646 */
1647 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Geert Uytterhoevene0a12a22015-08-21 20:02:35 +02001648 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +09001649 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001652 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001653 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001656 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001657 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001659 /* Overrun Interrupt */
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001660 if (orer_status & s->overrun_mask) {
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001661 sci_handle_fifo_overrun(port);
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001662 ret = IRQ_HANDLED;
1663 }
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001664
Michael Trimarchia8884e32008-10-31 16:10:23 +09001665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001668static const struct sci_irq_desc {
Paul Mundt9174fc82011-06-28 15:25:36 +09001669 const char *desc;
1670 irq_handler_t handler;
1671} sci_irq_desc[] = {
1672 /*
1673 * Split out handlers, the default case.
1674 */
1675 [SCIx_ERI_IRQ] = {
1676 .desc = "rx err",
1677 .handler = sci_er_interrupt,
1678 },
1679
1680 [SCIx_RXI_IRQ] = {
1681 .desc = "rx full",
1682 .handler = sci_rx_interrupt,
1683 },
1684
1685 [SCIx_TXI_IRQ] = {
1686 .desc = "tx empty",
1687 .handler = sci_tx_interrupt,
1688 },
1689
1690 [SCIx_BRI_IRQ] = {
1691 .desc = "break",
1692 .handler = sci_br_interrupt,
1693 },
1694
1695 /*
1696 * Special muxed handler.
1697 */
1698 [SCIx_MUX_IRQ] = {
1699 .desc = "mux",
1700 .handler = sci_mpxed_interrupt,
1701 },
1702};
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704static int sci_request_irq(struct sci_port *port)
1705{
Paul Mundt9174fc82011-06-28 15:25:36 +09001706 struct uart_port *up = &port->port;
1707 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Paul Mundt9174fc82011-06-28 15:25:36 +09001709 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001710 const struct sci_irq_desc *desc;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001711 int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001712
Paul Mundt9174fc82011-06-28 15:25:36 +09001713 if (SCIx_IRQ_IS_MUXED(port)) {
1714 i = SCIx_MUX_IRQ;
1715 irq = up->irq;
Paul Mundt0e8963d2012-05-18 18:21:06 +09001716 } else {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001717 irq = port->irqs[i];
Paul Mundt9174fc82011-06-28 15:25:36 +09001718
Paul Mundt0e8963d2012-05-18 18:21:06 +09001719 /*
1720 * Certain port types won't support all of the
1721 * available interrupt sources.
1722 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001723 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001724 continue;
1725 }
1726
Paul Mundt9174fc82011-06-28 15:25:36 +09001727 desc = sci_irq_desc + i;
1728 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1729 dev_name(up->dev), desc->desc);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02001730 if (!port->irqstr[j])
Paul Mundt9174fc82011-06-28 15:25:36 +09001731 goto out_nomem;
Paul Mundt762c69e2008-12-16 18:55:26 +09001732
Paul Mundt9174fc82011-06-28 15:25:36 +09001733 ret = request_irq(irq, desc->handler, up->irqflags,
1734 port->irqstr[j], port);
1735 if (unlikely(ret)) {
1736 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1737 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 }
1740
1741 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001742
1743out_noirq:
1744 while (--i >= 0)
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001745 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001746
1747out_nomem:
1748 while (--j >= 0)
1749 kfree(port->irqstr[j]);
1750
1751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754static void sci_free_irq(struct sci_port *port)
1755{
1756 int i;
1757
Paul Mundt9174fc82011-06-28 15:25:36 +09001758 /*
1759 * Intentionally in reverse order so we iterate over the muxed
1760 * IRQ first.
1761 */
1762 for (i = 0; i < SCIx_NR_IRQS; i++) {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001763 int irq = port->irqs[i];
Paul Mundt0e8963d2012-05-18 18:21:06 +09001764
1765 /*
1766 * Certain port types won't support all of the available
1767 * interrupt sources.
1768 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001769 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001770 continue;
1771
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001772 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001773 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Paul Mundt9174fc82011-06-28 15:25:36 +09001775 if (SCIx_IRQ_IS_MUXED(port)) {
1776 /* If there's only one IRQ, we're done. */
1777 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779 }
1780}
1781
1782static unsigned int sci_tx_empty(struct uart_port *port)
1783{
Paul Mundtb12bb292012-03-30 19:50:15 +09001784 unsigned short status = serial_port_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001785 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001786
1787 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Paul Mundtcdf7c422011-11-24 20:18:32 +09001790/*
1791 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1792 * CTS/RTS is supported in hardware by at least one port and controlled
1793 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1794 * handled via the ->init_pins() op, which is a bit of a one-way street,
1795 * lacking any ability to defer pin control -- this will later be
1796 * converted over to the GPIO framework).
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001797 *
1798 * Other modes (such as loopback) are supported generically on certain
1799 * port types, but not others. For these it's sufficient to test for the
1800 * existence of the support register and simply ignore the port type.
Paul Mundtcdf7c422011-11-24 20:18:32 +09001801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1803{
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001804 if (mctrl & TIOCM_LOOP) {
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001805 const struct plat_sci_reg *reg;
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001806
1807 /*
1808 * Standard loopback mode for SCFCR ports.
1809 */
1810 reg = sci_getreg(port, SCFCR);
1811 if (reg->size)
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001812 serial_port_out(port, SCFCR,
1813 serial_port_in(port, SCFCR) |
1814 SCFCR_LOOP);
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818static unsigned int sci_get_mctrl(struct uart_port *port)
1819{
Paul Mundtcdf7c422011-11-24 20:18:32 +09001820 /*
1821 * CTS/RTS is handled in hardware when supported, while nothing
1822 * else is wired up. Keep it simple and simply assert DSR/CAR.
1823 */
1824 return TIOCM_DSR | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827static void sci_break_ctl(struct uart_port *port, int break_state)
1828{
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001829 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001830 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001831 unsigned short scscr, scsptr;
1832
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001833 /* check wheter the port has SCSPTR */
1834 if (!reg->size) {
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001835 /*
1836 * Not supported by hardware. Most parts couple break and rx
1837 * interrupts together, with break detection always enabled.
1838 */
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001839 return;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001840 }
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001841
1842 scsptr = serial_port_in(port, SCSPTR);
1843 scscr = serial_port_in(port, SCSCR);
1844
1845 if (break_state == -1) {
1846 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1847 scscr &= ~SCSCR_TE;
1848 } else {
1849 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1850 scscr |= SCSCR_TE;
1851 }
1852
1853 serial_port_out(port, SCSPTR, scsptr);
1854 serial_port_out(port, SCSCR, scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857static int sci_startup(struct uart_port *port)
1858{
Magnus Damma5660ad2009-01-21 15:14:38 +00001859 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001860 unsigned long flags;
Paul Mundt073e84c2011-01-19 17:30:53 +09001861 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001863 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1864
Paul Mundt073e84c2011-01-19 17:30:53 +09001865 ret = sci_request_irq(s);
1866 if (unlikely(ret < 0))
1867 return ret;
1868
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001869 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001870
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001871 spin_lock_irqsave(&port->lock, flags);
Yoshinori Satod6569012005-10-14 15:59:12 -07001872 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001873 sci_start_rx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001874 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 return 0;
1877}
1878
1879static void sci_shutdown(struct uart_port *port)
1880{
Magnus Damma5660ad2009-01-21 15:14:38 +00001881 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001882 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001884 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1885
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001886 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001888 sci_stop_tx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001889 spin_unlock_irqrestore(&port->lock, flags);
Paul Mundt073e84c2011-01-19 17:30:53 +09001890
Aleksandar Mitev9ab76552015-09-18 13:08:28 +02001891#ifdef CONFIG_SERIAL_SH_SCI_DMA
1892 if (s->chan_rx) {
1893 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
1894 port->line);
1895 del_timer_sync(&s->rx_timer);
1896 }
1897#endif
1898
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001899 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 sci_free_irq(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001903static int sci_sck_calc(struct sci_port *s, unsigned int bps,
1904 unsigned int *srr)
Paul Mundt26c92f32009-06-24 18:23:52 +09001905{
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001906 unsigned long freq = s->clk_rates[SCI_SCK];
1907 unsigned int min_sr, max_sr, sr;
1908 int err, min_err = INT_MAX;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01001909
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001910 if (s->port.type != PORT_HSCIF)
1911 freq *= 2;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001912 if (s->sampling_rate) {
1913 /* SCI(F) has a fixed sampling rate */
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001914 min_sr = max_sr = s->sampling_rate;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001915 } else {
1916 /* HSCIF has a variable 1/(8..32) sampling rate */
1917 min_sr = 8;
1918 max_sr = 32;
1919 }
Paul Mundte8183a62011-01-19 17:51:37 +09001920
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001921 for (sr = max_sr; sr >= min_sr; sr--) {
1922 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
1923 if (abs(err) >= abs(min_err))
1924 continue;
1925
1926 min_err = err;
1927 *srr = sr - 1;
1928
1929 if (!err)
1930 break;
1931 }
1932
1933 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
1934 *srr + 1);
1935 return min_err;
Paul Mundt26c92f32009-06-24 18:23:52 +09001936}
1937
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001938static int sci_brg_calc(struct sci_port *s, unsigned int bps,
1939 unsigned long freq, unsigned int *dlr,
1940 unsigned int *srr)
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001941{
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001942 unsigned int min_sr, max_sr, sr, dl;
1943 int err, min_err = INT_MAX;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001944
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001945 if (s->port.type != PORT_HSCIF)
1946 freq *= 2;
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001947 if (s->sampling_rate) {
1948 /* SCIF has a fixed sampling rate */
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001949 min_sr = max_sr = s->sampling_rate;
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001950 } else {
1951 /* HSCIF has a variable 1/(8..32) sampling rate */
1952 min_sr = 8;
1953 max_sr = 32;
1954 }
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001955
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001956 for (sr = max_sr; sr >= min_sr; sr--) {
1957 dl = DIV_ROUND_CLOSEST(freq, sr * bps);
1958 dl = clamp(dl, 1U, 65535U);
1959
1960 err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
1961 if (abs(err) >= abs(min_err))
1962 continue;
1963
1964 min_err = err;
1965 *dlr = dl;
1966 *srr = sr - 1;
1967
1968 if (!err)
1969 break;
1970 }
1971
1972 dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
1973 min_err, *dlr, *srr + 1);
1974 return min_err;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001975}
1976
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001977/* calculate sample rate, BRR, and clock select */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001978static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
1979 unsigned int *brr, unsigned int *srr,
1980 unsigned int *cks)
Ulrich Hechtf303b362013-05-31 17:57:01 +02001981{
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001982 unsigned int min_sr, max_sr, sr, br, prediv, scrate, c;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001983 unsigned long freq = s->clk_rates[SCI_FCK];
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001984 int err, min_err = INT_MAX;
Ulrich Hechtf303b362013-05-31 17:57:01 +02001985
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01001986 if (s->port.type != PORT_HSCIF)
1987 freq *= 2;
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001988 if (s->sampling_rate) {
1989 min_sr = max_sr = s->sampling_rate;
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001990 } else {
1991 /* HSCIF has a variable sample rate */
1992 min_sr = 8;
1993 max_sr = 32;
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001994 }
1995
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001996 /*
1997 * Find the combination of sample rate and clock select with the
1998 * smallest deviation from the desired baud rate.
1999 * Prefer high sample rates to maximise the receive margin.
2000 *
2001 * M: Receive margin (%)
2002 * N: Ratio of bit rate to clock (N = sampling rate)
2003 * D: Clock duty (D = 0 to 1.0)
2004 * L: Frame length (L = 9 to 12)
2005 * F: Absolute value of clock frequency deviation
2006 *
2007 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2008 * (|D - 0.5| / N * (1 + F))|
2009 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2010 */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002011 for (sr = max_sr; sr >= min_sr; sr--) {
Ulrich Hechtf303b362013-05-31 17:57:01 +02002012 for (c = 0; c <= 3; c++) {
2013 /* integerized formulas from HSCIF documentation */
Geert Uytterhoeven7b5c0c02016-01-04 14:45:20 +01002014 prediv = sr * (1 << (2 * c + 1));
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002015
2016 /*
2017 * We need to calculate:
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002018 *
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002019 * br = freq / (prediv * bps) clamped to [1..256]
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002020 * err = freq / (br * prediv) - bps
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002021 *
2022 * Watch out for overflow when calculating the desired
2023 * sampling clock rate!
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002024 */
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002025 if (bps > UINT_MAX / prediv)
2026 break;
2027
2028 scrate = prediv * bps;
2029 br = DIV_ROUND_CLOSEST(freq, scrate);
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002030 br = clamp(br, 1U, 256U);
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002031
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002032 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002033 if (abs(err) >= abs(min_err))
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002034 continue;
2035
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002036 min_err = err;
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002037 *brr = br - 1;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002038 *srr = sr - 1;
2039 *cks = c;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002040
2041 if (!err)
2042 goto found;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002043 }
2044 }
2045
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002046found:
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002047 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2048 min_err, *brr, *srr + 1, *cks);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002049 return min_err;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002050}
2051
Magnus Damm1ba76222011-08-03 03:47:36 +00002052static void sci_reset(struct uart_port *port)
2053{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002054 const struct plat_sci_reg *reg;
Magnus Damm1ba76222011-08-03 03:47:36 +00002055 unsigned int status;
2056
2057 do {
Paul Mundtb12bb292012-03-30 19:50:15 +09002058 status = serial_port_in(port, SCxSR);
Magnus Damm1ba76222011-08-03 03:47:36 +00002059 } while (!(status & SCxSR_TEND(port)));
2060
Paul Mundtb12bb292012-03-30 19:50:15 +09002061 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
Magnus Damm1ba76222011-08-03 03:47:36 +00002062
Paul Mundt0979e0e2011-11-24 18:35:49 +09002063 reg = sci_getreg(port, SCFCR);
2064 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09002065 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Magnus Damm1ba76222011-08-03 03:47:36 +00002066}
2067
Alan Cox606d0992006-12-08 02:38:45 -08002068static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2069 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Geert Uytterhoeven95ee05c2016-01-04 14:45:18 +01002071 unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i;
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002072 unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
2073 unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
Paul Mundt00b9de92009-06-24 17:53:33 +09002074 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002075 const struct plat_sci_reg *reg;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002076 int min_err = INT_MAX, err;
2077 unsigned long max_freq = 0;
2078 int best_clk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002080 if ((termios->c_cflag & CSIZE) == CS7)
2081 smr_val |= SCSMR_CHR;
2082 if (termios->c_cflag & PARENB)
2083 smr_val |= SCSMR_PE;
2084 if (termios->c_cflag & PARODD)
2085 smr_val |= SCSMR_PE | SCSMR_ODD;
2086 if (termios->c_cflag & CSTOPB)
2087 smr_val |= SCSMR_STOP;
2088
Magnus Damm154280f2009-12-22 03:37:28 +00002089 /*
2090 * earlyprintk comes here early on with port->uartclk set to zero.
2091 * the clock framework is not up and running at this point so here
2092 * we assume that 115200 is the maximum baud rate. please note that
2093 * the baud rate is not programmed during earlyprintk - it is assumed
2094 * that the previous boot loader has enabled required clocks and
2095 * setup the baud rate generator hardware for us already.
2096 */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002097 if (!port->uartclk) {
2098 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2099 goto done;
2100 }
Magnus Damm154280f2009-12-22 03:37:28 +00002101
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002102 for (i = 0; i < SCI_NUM_CLKS; i++)
2103 max_freq = max(max_freq, s->clk_rates[i]);
2104
2105 baud = uart_get_baud_rate(port, termios, old, 0,
2106 max_freq / max(s->sampling_rate, 8U));
2107 if (!baud)
2108 goto done;
2109
2110 /*
2111 * There can be multiple sources for the sampling clock. Find the one
2112 * that gives us the smallest deviation from the desired baud rate.
2113 */
2114
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002115 /* Optional Undivided External Clock */
2116 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2117 port->type != PORT_SCIFB) {
2118 err = sci_sck_calc(s, baud, &srr1);
2119 if (abs(err) < abs(min_err)) {
2120 best_clk = SCI_SCK;
2121 scr_val = SCSCR_CKE1;
2122 sccks = SCCKS_CKS;
2123 min_err = err;
2124 srr = srr1;
2125 if (!err)
2126 goto done;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002127 }
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002130 /* Optional BRG Frequency Divided External Clock */
2131 if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
2132 err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
2133 &srr1);
2134 if (abs(err) < abs(min_err)) {
2135 best_clk = SCI_SCIF_CLK;
2136 scr_val = SCSCR_CKE1;
2137 sccks = 0;
2138 min_err = err;
2139 dl = dl1;
2140 srr = srr1;
2141 if (!err)
2142 goto done;
2143 }
2144 }
2145
2146 /* Optional BRG Frequency Divided Internal Clock */
2147 if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
2148 err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
2149 &srr1);
2150 if (abs(err) < abs(min_err)) {
2151 best_clk = SCI_BRG_INT;
2152 scr_val = SCSCR_CKE1;
2153 sccks = SCCKS_XIN;
2154 min_err = err;
2155 dl = dl1;
2156 srr = srr1;
2157 if (!min_err)
2158 goto done;
2159 }
2160 }
2161
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002162 /* Divided Functional Clock using standard Bit Rate Register */
2163 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2164 if (abs(err) < abs(min_err)) {
2165 best_clk = SCI_FCK;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002166 scr_val = 0;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002167 min_err = err;
2168 brr = brr1;
2169 srr = srr1;
2170 cks = cks1;
2171 }
2172
2173done:
2174 if (best_clk >= 0)
2175 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2176 s->clks[best_clk], baud, min_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Paul Mundt23241d42011-06-28 13:55:31 +09002178 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00002179
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002180 /*
2181 * Program the optional External Baud Rate Generator (BRG) first.
2182 * It controls the mux to select (H)SCK or frequency divided clock.
2183 */
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002184 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
2185 serial_port_out(port, SCDL, dl);
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002186 serial_port_out(port, SCCKS, sccks);
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002187 }
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002188
Magnus Damm1ba76222011-08-03 03:47:36 +00002189 sci_reset(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002190
Paul Mundte108b2c2006-09-27 16:32:13 +09002191 uart_update_timeout(port, termios->c_cflag, baud);
2192
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002193 if (best_clk >= 0) {
2194 smr_val |= cks;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002195 dev_dbg(port->dev,
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002196 "SCR 0x%x SMR 0x%x BRR %u CKS 0x%x DL %u SRR %u\n",
2197 scr_val, smr_val, brr, sccks, dl, srr);
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002198 serial_port_out(port, SCSCR, scr_val);
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09002199 serial_port_out(port, SCSMR, smr_val);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002200 serial_port_out(port, SCBRR, brr);
2201 if (sci_getreg(port, HSSRR)->size)
2202 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
2203
2204 /* Wait one bit interval */
2205 udelay((1000000 + (baud - 1)) / baud);
2206 } else {
2207 /* Don't touch the bit rate configuration */
2208 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
Geert Uytterhoeven3a964ab2016-01-04 14:45:19 +01002209 smr_val |= serial_port_in(port, SCSMR) &
2210 (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002211 dev_dbg(port->dev, "SCR 0x%x SMR 0x%x\n", scr_val, smr_val);
2212 serial_port_out(port, SCSCR, scr_val);
2213 serial_port_out(port, SCSMR, smr_val);
2214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Paul Mundtd5701642008-12-16 20:07:27 +09002216 sci_init_pins(port, termios->c_cflag);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002217
Paul Mundt73c3d532011-12-02 19:02:06 +09002218 reg = sci_getreg(port, SCFCR);
2219 if (reg->size) {
Paul Mundtb12bb292012-03-30 19:50:15 +09002220 unsigned short ctrl = serial_port_in(port, SCFCR);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002221
Paul Mundt73c3d532011-12-02 19:02:06 +09002222 if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
Paul Mundtfaf02f82011-12-02 17:44:50 +09002223 if (termios->c_cflag & CRTSCTS)
2224 ctrl |= SCFCR_MCE;
2225 else
2226 ctrl &= ~SCFCR_MCE;
Paul Mundtfaf02f82011-12-02 17:44:50 +09002227 }
Paul Mundt73c3d532011-12-02 19:02:06 +09002228
2229 /*
2230 * As we've done a sci_reset() above, ensure we don't
2231 * interfere with the FIFOs while toggling MCE. As the
2232 * reset values could still be set, simply mask them out.
2233 */
2234 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2235
Paul Mundtb12bb292012-03-30 19:50:15 +09002236 serial_port_out(port, SCFCR, ctrl);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002237 }
Paul Mundtb7a76e42006-02-01 03:06:06 -08002238
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002239 scr_val |= s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0);
2240 dev_dbg(port->dev, "SCSCR 0x%x\n", scr_val);
2241 serial_port_out(port, SCSCR, scr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002243#ifdef CONFIG_SERIAL_SH_SCI_DMA
2244 /*
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002245 * Calculate delay for 2 DMA buffers (4 FIFO).
Geert Uytterhoevenf5835c12015-08-21 20:02:38 +02002246 * See serial_core.c::uart_update_timeout().
2247 * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
2248 * function calculates 1 jiffie for the data plus 5 jiffies for the
2249 * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
2250 * buffers (4 FIFO sizes), but when performing a faster transfer, the
2251 * value obtained by this formula is too small. Therefore, if the value
2252 * is smaller than 20ms, use 20ms as the timeout value for DMA.
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002253 */
2254 if (s->chan_rx) {
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002255 unsigned int bits;
2256
2257 /* byte size and parity */
2258 switch (termios->c_cflag & CSIZE) {
2259 case CS5:
2260 bits = 7;
2261 break;
2262 case CS6:
2263 bits = 8;
2264 break;
2265 case CS7:
2266 bits = 9;
2267 break;
2268 default:
2269 bits = 10;
2270 break;
2271 }
2272
2273 if (termios->c_cflag & CSTOPB)
2274 bits++;
2275 if (termios->c_cflag & PARENB)
2276 bits++;
2277 s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
2278 (baud / 10), 10);
Joe Perches9b971cd2014-03-11 10:10:46 -07002279 dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002280 s->rx_timeout * 1000 / HZ, port->timeout);
2281 if (s->rx_timeout < msecs_to_jiffies(20))
2282 s->rx_timeout = msecs_to_jiffies(20);
2283 }
2284#endif
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002287 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00002288
Paul Mundt23241d42011-06-28 13:55:31 +09002289 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002292static void sci_pm(struct uart_port *port, unsigned int state,
2293 unsigned int oldstate)
2294{
2295 struct sci_port *sci_port = to_sci_port(port);
2296
2297 switch (state) {
Geert Uytterhoevend3dfe5d2014-03-11 11:11:20 +01002298 case UART_PM_STATE_OFF:
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002299 sci_port_disable(sci_port);
2300 break;
2301 default:
2302 sci_port_enable(sci_port);
2303 break;
2304 }
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307static const char *sci_type(struct uart_port *port)
2308{
2309 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09002310 case PORT_IRDA:
2311 return "irda";
2312 case PORT_SCI:
2313 return "sci";
2314 case PORT_SCIF:
2315 return "scif";
2316 case PORT_SCIFA:
2317 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002318 case PORT_SCIFB:
2319 return "scifb";
Ulrich Hechtf303b362013-05-31 17:57:01 +02002320 case PORT_HSCIF:
2321 return "hscif";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323
Paul Mundtfa439722008-09-04 18:53:58 +09002324 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Paul Mundtf6e94952011-01-21 15:25:36 +09002327static int sci_remap_port(struct uart_port *port)
2328{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002329 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002330
2331 /*
2332 * Nothing to do if there's already an established membase.
2333 */
2334 if (port->membase)
2335 return 0;
2336
2337 if (port->flags & UPF_IOREMAP) {
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002338 port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
Paul Mundtf6e94952011-01-21 15:25:36 +09002339 if (unlikely(!port->membase)) {
2340 dev_err(port->dev, "can't remap port#%d\n", port->line);
2341 return -ENXIO;
2342 }
2343 } else {
2344 /*
2345 * For the simple (and majority of) cases where we don't
2346 * need to do any remapping, just cast the cookie
2347 * directly.
2348 */
Jingoo Han3af4e962014-02-05 09:56:37 +09002349 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
Paul Mundtf6e94952011-01-21 15:25:36 +09002350 }
2351
2352 return 0;
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355static void sci_release_port(struct uart_port *port)
2356{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002357 struct sci_port *sport = to_sci_port(port);
2358
Paul Mundte2651642011-01-20 21:24:03 +09002359 if (port->flags & UPF_IOREMAP) {
2360 iounmap(port->membase);
2361 port->membase = NULL;
2362 }
2363
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002364 release_mem_region(port->mapbase, sport->reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367static int sci_request_port(struct uart_port *port)
2368{
Paul Mundte2651642011-01-20 21:24:03 +09002369 struct resource *res;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002370 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002371 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002373 res = request_mem_region(port->mapbase, sport->reg_size,
2374 dev_name(port->dev));
2375 if (unlikely(res == NULL)) {
2376 dev_err(port->dev, "request_mem_region failed.");
Paul Mundte2651642011-01-20 21:24:03 +09002377 return -EBUSY;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Paul Mundtf6e94952011-01-21 15:25:36 +09002380 ret = sci_remap_port(port);
2381 if (unlikely(ret != 0)) {
2382 release_resource(res);
2383 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002384 }
Paul Mundte2651642011-01-20 21:24:03 +09002385
2386 return 0;
2387}
2388
2389static void sci_config_port(struct uart_port *port, int flags)
2390{
2391 if (flags & UART_CONFIG_TYPE) {
2392 struct sci_port *sport = to_sci_port(port);
2393
2394 port->type = sport->cfg->type;
2395 sci_request_port(port);
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (ser->baud_base < 2400)
2402 /* No paper tape reader for Mitch.. */
2403 return -EINVAL;
2404
2405 return 0;
2406}
2407
2408static struct uart_ops sci_uart_ops = {
2409 .tx_empty = sci_tx_empty,
2410 .set_mctrl = sci_set_mctrl,
2411 .get_mctrl = sci_get_mctrl,
2412 .start_tx = sci_start_tx,
2413 .stop_tx = sci_stop_tx,
2414 .stop_rx = sci_stop_rx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 .break_ctl = sci_break_ctl,
2416 .startup = sci_startup,
2417 .shutdown = sci_shutdown,
2418 .set_termios = sci_set_termios,
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002419 .pm = sci_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 .type = sci_type,
2421 .release_port = sci_release_port,
2422 .request_port = sci_request_port,
2423 .config_port = sci_config_port,
2424 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002425#ifdef CONFIG_CONSOLE_POLL
2426 .poll_get_char = sci_poll_get_char,
2427 .poll_put_char = sci_poll_put_char,
2428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429};
2430
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002431static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2432{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002433 const char *clk_names[] = {
2434 [SCI_FCK] = "fck",
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002435 [SCI_SCK] = "sck",
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002436 [SCI_BRG_INT] = "brg_int",
2437 [SCI_SCIF_CLK] = "scif_clk",
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002438 };
2439 struct clk *clk;
2440 unsigned int i;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002441
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002442 if (sci_port->cfg->type == PORT_HSCIF)
2443 clk_names[SCI_SCK] = "hsck";
2444
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002445 for (i = 0; i < SCI_NUM_CLKS; i++) {
2446 clk = devm_clk_get(dev, clk_names[i]);
2447 if (PTR_ERR(clk) == -EPROBE_DEFER)
2448 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002449
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002450 if (IS_ERR(clk) && i == SCI_FCK) {
2451 /*
2452 * "fck" used to be called "sci_ick", and we need to
2453 * maintain DT backward compatibility.
2454 */
2455 clk = devm_clk_get(dev, "sci_ick");
2456 if (PTR_ERR(clk) == -EPROBE_DEFER)
2457 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002458
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002459 if (!IS_ERR(clk))
2460 goto found;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002461
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002462 /*
2463 * Not all SH platforms declare a clock lookup entry
2464 * for SCI devices, in which case we need to get the
2465 * global "peripheral_clk" clock.
2466 */
2467 clk = devm_clk_get(dev, "peripheral_clk");
2468 if (!IS_ERR(clk))
2469 goto found;
2470
2471 dev_err(dev, "failed to get %s (%ld)\n", clk_names[i],
2472 PTR_ERR(clk));
2473 return PTR_ERR(clk);
2474 }
2475
2476found:
2477 if (IS_ERR(clk))
2478 dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i],
2479 PTR_ERR(clk));
2480 else
2481 dev_dbg(dev, "clk %s is %pC rate %pCr\n", clk_names[i],
2482 clk, clk);
2483 sci_port->clks[i] = IS_ERR(clk) ? NULL : clk;
2484 }
2485 return 0;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002486}
2487
Bill Pemberton9671f092012-11-19 13:21:50 -05002488static int sci_init_single(struct platform_device *dev,
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002489 struct sci_port *sci_port, unsigned int index,
2490 struct plat_sci_port *p, bool early)
Paul Mundte108b2c2006-09-27 16:32:13 +09002491{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002492 struct uart_port *port = &sci_port->port;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002493 const struct resource *res;
2494 unsigned int i;
Paul Mundt3127c6b2011-06-28 13:44:37 +09002495 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002496
Paul Mundt50f09592011-12-02 20:09:48 +09002497 sci_port->cfg = p;
2498
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002499 port->ops = &sci_uart_ops;
2500 port->iotype = UPIO_MEM;
2501 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09002502
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002503 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2504 if (res == NULL)
2505 return -ENOMEM;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002506
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002507 port->mapbase = res->start;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002508 sci_port->reg_size = resource_size(res);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002509
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002510 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2511 sci_port->irqs[i] = platform_get_irq(dev, i);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002512
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002513 /* The SCI generates several interrupts. They can be muxed together or
2514 * connected to different interrupt lines. In the muxed case only one
2515 * interrupt resource is specified. In the non-muxed case three or four
2516 * interrupt resources are specified, as the BRI interrupt is optional.
2517 */
2518 if (sci_port->irqs[0] < 0)
2519 return -ENXIO;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002520
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002521 if (sci_port->irqs[1] < 0) {
2522 sci_port->irqs[1] = sci_port->irqs[0];
2523 sci_port->irqs[2] = sci_port->irqs[0];
2524 sci_port->irqs[3] = sci_port->irqs[0];
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002525 }
2526
Paul Mundt3127c6b2011-06-28 13:44:37 +09002527 if (p->regtype == SCIx_PROBE_REGTYPE) {
2528 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02002529 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09002530 return ret;
2531 }
Paul Mundt61a69762011-06-14 12:40:19 +09002532
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002533 switch (p->type) {
2534 case PORT_SCIFB:
2535 port->fifosize = 256;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002536 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002537 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002538 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002539 break;
2540 case PORT_HSCIF:
2541 port->fifosize = 128;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002542 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002543 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002544 sci_port->sampling_rate = 0;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002545 break;
2546 case PORT_SCIFA:
2547 port->fifosize = 64;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002548 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002549 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002550 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002551 break;
2552 case PORT_SCIF:
2553 port->fifosize = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002554 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002555 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002556 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002557 sci_port->sampling_rate = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002558 } else {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002559 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002560 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002561 sci_port->sampling_rate = 32;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002562 }
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002563 break;
2564 default:
2565 port->fifosize = 1;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002566 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002567 sci_port->overrun_mask = SCI_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002568 sci_port->sampling_rate = 32;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002569 break;
2570 }
2571
Laurent Pinchart878fbb912013-12-06 10:59:51 +01002572 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
2573 * match the SoC datasheet, this should be investigated. Let platform
2574 * data override the sampling rate for now.
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002575 */
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002576 if (p->sampling_rate)
2577 sci_port->sampling_rate = p->sampling_rate;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002578
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002579 if (!early) {
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002580 ret = sci_init_clocks(sci_port, &dev->dev);
2581 if (ret < 0)
2582 return ret;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002583
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002584 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002585
2586 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002587 }
Paul Mundte108b2c2006-09-27 16:32:13 +09002588
Magnus Damm7ed7e072009-01-21 15:14:14 +00002589 sci_port->break_timer.data = (unsigned long)sci_port;
2590 sci_port->break_timer.function = sci_break_timer;
2591 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09002592
Paul Mundtdebf9502011-06-08 18:19:37 +09002593 /*
2594 * Establish some sensible defaults for the error detection.
2595 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002596 if (p->type == PORT_SCI) {
2597 sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
2598 sci_port->error_clear = SCI_ERROR_CLEAR;
2599 } else {
2600 sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
2601 sci_port->error_clear = SCIF_ERROR_CLEAR;
2602 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002603
2604 /*
Laurent Pinchart3ae988d2013-12-06 10:59:17 +01002605 * Make the error mask inclusive of overrun detection, if
2606 * supported.
2607 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002608 if (sci_port->overrun_reg == SCxSR) {
Geert Uytterhoevenafd66db2015-04-30 18:21:33 +02002609 sci_port->error_mask |= sci_port->overrun_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002610 sci_port->error_clear &= ~sci_port->overrun_mask;
2611 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002612
Paul Mundtce6738b2011-01-19 15:24:40 +09002613 port->type = p->type;
Laurent Pinchartb6e4a3f2013-12-06 10:59:14 +01002614 port->flags = UPF_FIXED_PORT | p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09002615 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09002616
2617 /*
Paul Mundt61a69762011-06-14 12:40:19 +09002618 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09002619 * for the multi-IRQ ports, which is where we are primarily
2620 * concerned with the shutdown path synchronization.
2621 *
2622 * For the muxed case there's nothing more to do.
2623 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002624 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08002625 port->irqflags = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002626
Paul Mundt61a69762011-06-14 12:40:19 +09002627 port->serial_in = sci_serial_in;
2628 port->serial_out = sci_serial_out;
2629
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02002630 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2631 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2632 p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002633
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002634 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002635}
2636
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002637static void sci_cleanup_single(struct sci_port *port)
2638{
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002639 pm_runtime_disable(port->port.dev);
2640}
2641
Yoshinori Sato0b0cced2015-12-24 11:24:48 +01002642#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
2643 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002644static void serial_console_putchar(struct uart_port *port, int ch)
2645{
2646 sci_poll_put_char(port, ch);
2647}
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649/*
2650 * Print a string to the serial port trying not to disturb
2651 * any possible real use of the port...
2652 */
2653static void serial_console_write(struct console *co, const char *s,
2654 unsigned count)
2655{
Paul Mundt906b17d2011-01-21 16:19:53 +09002656 struct sci_port *sci_port = &sci_ports[co->index];
2657 struct uart_port *port = &sci_port->port;
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002658 unsigned short bits, ctrl, ctrl_temp;
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002659 unsigned long flags;
2660 int locked = 1;
2661
2662 local_irq_save(flags);
Yoshinori Sato0b0cced2015-12-24 11:24:48 +01002663#if defined(SUPPORT_SYSRQ)
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002664 if (port->sysrq)
2665 locked = 0;
Yoshinori Sato0b0cced2015-12-24 11:24:48 +01002666 else
2667#endif
2668 if (oops_in_progress)
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002669 locked = spin_trylock(&port->lock);
2670 else
2671 spin_lock(&port->lock);
2672
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002673 /* first save SCSCR then disable interrupts, keep clock source */
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002674 ctrl = serial_port_in(port, SCSCR);
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002675 ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
2676 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
2677 serial_port_out(port, SCSCR, ctrl_temp);
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002678
Magnus Damm501b8252009-01-21 15:14:30 +00002679 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002680
2681 /* wait until fifo is empty and last bit has been transmitted */
2682 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
Paul Mundtb12bb292012-03-30 19:50:15 +09002683 while ((serial_port_in(port, SCxSR) & bits) != bits)
Magnus Damm973e5d52009-02-24 15:57:12 +09002684 cpu_relax();
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002685
2686 /* restore the SCSCR */
2687 serial_port_out(port, SCSCR, ctrl);
2688
2689 if (locked)
2690 spin_unlock(&port->lock);
2691 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
Bill Pemberton9671f092012-11-19 13:21:50 -05002694static int serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002696 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct uart_port *port;
2698 int baud = 115200;
2699 int bits = 8;
2700 int parity = 'n';
2701 int flow = 'n';
2702 int ret;
2703
Paul Mundte108b2c2006-09-27 16:32:13 +09002704 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002705 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002706 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002707 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002708 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002709
Paul Mundt906b17d2011-01-21 16:19:53 +09002710 sci_port = &sci_ports[co->index];
2711 port = &sci_port->port;
2712
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002713 /*
2714 * Refuse to handle uninitialized ports.
2715 */
2716 if (!port->ops)
2717 return -ENODEV;
2718
Paul Mundtf6e94952011-01-21 15:25:36 +09002719 ret = sci_remap_port(port);
2720 if (unlikely(ret != 0))
2721 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (options)
2724 uart_parse_options(options, &baud, &parity, &bits, &flow);
2725
Paul Mundtab7cfb52011-06-01 14:47:42 +09002726 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729static struct console serial_console = {
2730 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002731 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 .write = serial_console_write,
2733 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002734 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002736 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737};
2738
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002739static struct console early_serial_console = {
2740 .name = "early_ttySC",
2741 .write = serial_console_write,
2742 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002743 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002744};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002745
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002746static char early_serial_buf[32];
2747
Bill Pemberton9671f092012-11-19 13:21:50 -05002748static int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002749{
Jingoo Han574de552013-07-30 17:06:57 +09002750 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002751
2752 if (early_serial_console.data)
2753 return -EEXIST;
2754
2755 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002756
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002757 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002758
2759 serial_console_setup(&early_serial_console, early_serial_buf);
2760
2761 if (!strstr(early_serial_buf, "keep"))
2762 early_serial_console.flags |= CON_BOOT;
2763
2764 register_console(&early_serial_console);
2765 return 0;
2766}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002767
2768#define SCI_CONSOLE (&serial_console)
2769
Paul Mundtecdf8a42011-01-21 00:05:48 +09002770#else
Bill Pemberton9671f092012-11-19 13:21:50 -05002771static inline int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002772{
2773 return -EINVAL;
2774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002776#define SCI_CONSOLE NULL
2777
Yoshinori Sato0b0cced2015-12-24 11:24:48 +01002778#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002780static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782static struct uart_driver sci_uart_driver = {
2783 .owner = THIS_MODULE,
2784 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 .dev_name = "ttySC",
2786 .major = SCI_MAJOR,
2787 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002788 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 .cons = SCI_CONSOLE,
2790};
2791
Paul Mundt54507f62009-05-08 23:48:33 +09002792static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002793{
Paul Mundtd535a232011-01-19 17:19:35 +09002794 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002795
Paul Mundtd535a232011-01-19 17:19:35 +09002796 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002797
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002798 sci_cleanup_single(port);
Paul Mundtd535a232011-01-19 17:19:35 +09002799
Magnus Damme552de22009-01-21 15:13:42 +00002800 return 0;
2801}
2802
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002803
2804#define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
2805#define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
2806#define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002807
2808static const struct of_device_id of_sci_match[] = {
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002809 /* SoC-specific types */
2810 {
2811 .compatible = "renesas,scif-r7s72100",
2812 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
2813 },
Geert Uytterhoeven9ed44bb2015-11-10 18:57:23 +01002814 /* Family-specific types */
2815 {
2816 .compatible = "renesas,rcar-gen1-scif",
2817 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2818 }, {
2819 .compatible = "renesas,rcar-gen2-scif",
2820 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2821 }, {
2822 .compatible = "renesas,rcar-gen3-scif",
2823 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2824 },
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002825 /* Generic types */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002826 {
2827 .compatible = "renesas,scif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002828 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002829 }, {
2830 .compatible = "renesas,scifa",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002831 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002832 }, {
2833 .compatible = "renesas,scifb",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002834 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002835 }, {
2836 .compatible = "renesas,hscif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002837 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002838 }, {
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002839 .compatible = "renesas,sci",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002840 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002841 }, {
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002842 /* Terminator */
2843 },
2844};
2845MODULE_DEVICE_TABLE(of, of_sci_match);
2846
2847static struct plat_sci_port *
2848sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
2849{
2850 struct device_node *np = pdev->dev.of_node;
2851 const struct of_device_id *match;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002852 struct plat_sci_port *p;
2853 int id;
2854
2855 if (!IS_ENABLED(CONFIG_OF) || !np)
2856 return NULL;
2857
Geert Uytterhoeven495bb472015-12-10 16:02:17 +01002858 match = of_match_node(of_sci_match, np);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002859 if (!match)
2860 return NULL;
2861
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002862 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02002863 if (!p)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002864 return NULL;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002865
Geert Uytterhoeven2095fc72015-11-12 13:39:49 +01002866 /* Get the line number from the aliases node. */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002867 id = of_alias_get_id(np, "serial");
2868 if (id < 0) {
2869 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
2870 return NULL;
2871 }
2872
2873 *dev_id = id;
2874
2875 p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002876 p->type = SCI_OF_TYPE(match->data);
2877 p->regtype = SCI_OF_REGTYPE(match->data);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002878 p->scscr = SCSCR_RE | SCSCR_TE;
2879
2880 return p;
2881}
2882
Bill Pemberton9671f092012-11-19 13:21:50 -05002883static int sci_probe_single(struct platform_device *dev,
Magnus Damm0ee70712009-01-21 15:13:50 +00002884 unsigned int index,
2885 struct plat_sci_port *p,
2886 struct sci_port *sciport)
2887{
Magnus Damm0ee70712009-01-21 15:13:50 +00002888 int ret;
2889
2890 /* Sanity check */
2891 if (unlikely(index >= SCI_NPORTS)) {
Joe Perches9b971cd2014-03-11 10:10:46 -07002892 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
Magnus Damm0ee70712009-01-21 15:13:50 +00002893 index+1, SCI_NPORTS);
Joe Perches9b971cd2014-03-11 10:10:46 -07002894 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
Laurent Pinchartb6c5ef62012-06-13 00:28:24 +02002895 return -EINVAL;
Magnus Damm0ee70712009-01-21 15:13:50 +00002896 }
2897
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002898 ret = sci_init_single(dev, sciport, index, p, false);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002899 if (ret)
2900 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002901
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002902 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
2903 if (ret) {
2904 sci_cleanup_single(sciport);
2905 return ret;
2906 }
2907
2908 return 0;
Magnus Damm0ee70712009-01-21 15:13:50 +00002909}
2910
Bill Pemberton9671f092012-11-19 13:21:50 -05002911static int sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002913 struct plat_sci_port *p;
2914 struct sci_port *sp;
2915 unsigned int dev_id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002916 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002917
Paul Mundtecdf8a42011-01-21 00:05:48 +09002918 /*
2919 * If we've come here via earlyprintk initialization, head off to
2920 * the special early probe. We don't have sufficient device state
2921 * to make it beyond this yet.
2922 */
2923 if (is_early_platform_device(dev))
2924 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002925
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002926 if (dev->dev.of_node) {
2927 p = sci_parse_dt(dev, &dev_id);
2928 if (p == NULL)
2929 return -EINVAL;
2930 } else {
2931 p = dev->dev.platform_data;
2932 if (p == NULL) {
2933 dev_err(&dev->dev, "no platform data supplied\n");
2934 return -EINVAL;
2935 }
2936
2937 dev_id = dev->id;
2938 }
2939
2940 sp = &sci_ports[dev_id];
Paul Mundtd535a232011-01-19 17:19:35 +09002941 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002942
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002943 ret = sci_probe_single(dev, dev_id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002944 if (ret)
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002945 return ret;
Magnus Damme552de22009-01-21 15:13:42 +00002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947#ifdef CONFIG_SH_STANDARD_BIOS
2948 sh_bios_gdb_detach();
2949#endif
2950
Paul Mundte108b2c2006-09-27 16:32:13 +09002951 return 0;
2952}
2953
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002954static __maybe_unused int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002955{
Paul Mundtd535a232011-01-19 17:19:35 +09002956 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002957
Paul Mundtd535a232011-01-19 17:19:35 +09002958 if (sport)
2959 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002960
2961 return 0;
2962}
2963
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002964static __maybe_unused int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002965{
Paul Mundtd535a232011-01-19 17:19:35 +09002966 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002967
Paul Mundtd535a232011-01-19 17:19:35 +09002968 if (sport)
2969 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002970
2971 return 0;
2972}
2973
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002974static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
Paul Mundt6daa79b2009-06-15 07:07:38 +09002975
Paul Mundte108b2c2006-09-27 16:32:13 +09002976static struct platform_driver sci_driver = {
2977 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002978 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002979 .driver = {
2980 .name = "sh-sci",
Paul Mundt6daa79b2009-06-15 07:07:38 +09002981 .pm = &sci_dev_pm_ops,
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002982 .of_match_table = of_match_ptr(of_sci_match),
Paul Mundte108b2c2006-09-27 16:32:13 +09002983 },
2984};
2985
2986static int __init sci_init(void)
2987{
2988 int ret;
2989
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002990 pr_info("%s\n", banner);
Paul Mundte108b2c2006-09-27 16:32:13 +09002991
Paul Mundte108b2c2006-09-27 16:32:13 +09002992 ret = uart_register_driver(&sci_uart_driver);
2993 if (likely(ret == 0)) {
2994 ret = platform_driver_register(&sci_driver);
2995 if (unlikely(ret))
2996 uart_unregister_driver(&sci_uart_driver);
2997 }
2998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return ret;
3000}
3001
3002static void __exit sci_exit(void)
3003{
Paul Mundte108b2c2006-09-27 16:32:13 +09003004 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 uart_unregister_driver(&sci_uart_driver);
3006}
3007
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00003008#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
3009early_platform_init_buffer("earlyprintk", &sci_driver,
3010 early_serial_buf, ARRAY_SIZE(early_serial_buf));
3011#endif
Yoshinori Sato0b0cced2015-12-24 11:24:48 +01003012#ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
3013static struct __init plat_sci_port port_cfg;
3014
3015static int __init early_console_setup(struct earlycon_device *device,
3016 int type)
3017{
3018 if (!device->port.membase)
3019 return -ENODEV;
3020
3021 device->port.serial_in = sci_serial_in;
3022 device->port.serial_out = sci_serial_out;
3023 device->port.type = type;
3024 memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port));
3025 sci_ports[0].cfg = &port_cfg;
3026 sci_ports[0].cfg->type = type;
3027 sci_probe_regmap(sci_ports[0].cfg);
3028 port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR) |
3029 SCSCR_RE | SCSCR_TE;
3030 sci_serial_out(&sci_ports[0].port, SCSCR, port_cfg.scscr);
3031
3032 device->con->write = serial_console_write;
3033 return 0;
3034}
3035static int __init sci_early_console_setup(struct earlycon_device *device,
3036 const char *opt)
3037{
3038 return early_console_setup(device, PORT_SCI);
3039}
3040static int __init scif_early_console_setup(struct earlycon_device *device,
3041 const char *opt)
3042{
3043 return early_console_setup(device, PORT_SCIF);
3044}
3045static int __init scifa_early_console_setup(struct earlycon_device *device,
3046 const char *opt)
3047{
3048 return early_console_setup(device, PORT_SCIFA);
3049}
3050static int __init scifb_early_console_setup(struct earlycon_device *device,
3051 const char *opt)
3052{
3053 return early_console_setup(device, PORT_SCIFB);
3054}
3055static int __init hscif_early_console_setup(struct earlycon_device *device,
3056 const char *opt)
3057{
3058 return early_console_setup(device, PORT_HSCIF);
3059}
3060
3061EARLYCON_DECLARE(sci, sci_early_console_setup);
3062OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
3063EARLYCON_DECLARE(scif, scif_early_console_setup);
3064OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3065EARLYCON_DECLARE(scifa, scifa_early_console_setup);
3066OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
3067EARLYCON_DECLARE(scifb, scifb_early_console_setup);
3068OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
3069EARLYCON_DECLARE(hscif, hscif_early_console_setup);
3070OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
3071#endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073module_init(sci_init);
3074module_exit(sci_exit);
3075
Paul Mundte108b2c2006-09-27 16:32:13 +09003076MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07003077MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09003078MODULE_AUTHOR("Paul Mundt");
Ulrich Hechtf303b362013-05-31 17:57:01 +02003079MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");