blob: 13c6abe9d842c0a8a0a2dc9a6b3d4003cb4b2103 [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
Markus Brunner3ea6bc32007-08-20 08:59:33 +09005 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090014 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090020#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#undef DEBUG
25
Paul Mundt85f094e2008-04-25 16:04:20 +090026#include <linux/clk.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010027#include <linux/console.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090028#include <linux/ctype.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010029#include <linux/cpufreq.h>
30#include <linux/delay.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090031#include <linux/dmaengine.h>
Magnus Damm5beabc72011-08-02 09:42:54 +000032#include <linux/dma-mapping.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010033#include <linux/err.h>
34#include <linux/errno.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010035#include <linux/init.h>
36#include <linux/interrupt.h>
37#include <linux/ioport.h>
38#include <linux/major.h>
39#include <linux/module.h>
40#include <linux/mm.h>
41#include <linux/notifier.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
Paul Mundte108b2c2006-09-27 16:32:13 +090079struct sci_port {
80 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Paul Mundtce6738b2011-01-19 15:24:40 +090082 /* Platform configuration */
83 struct plat_sci_port *cfg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +020084 unsigned int overrun_reg;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +020085 unsigned int overrun_mask;
Laurent Pinchart3ae988d2013-12-06 10:59:17 +010086 unsigned int error_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +020087 unsigned int error_clear;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +010088 unsigned int sampling_rate;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +090089 resource_size_t reg_size;
Paul Mundte108b2c2006-09-27 16:32:13 +090090
Paul Mundte108b2c2006-09-27 16:32:13 +090091 /* Break timer */
92 struct timer_list break_timer;
93 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090094
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090095 /* Function clock */
96 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090097
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +010098 int irqs[SCIx_NR_IRQS];
Paul Mundt9174fc82011-06-28 15:25:36 +090099 char *irqstr[SCIx_NR_IRQS];
100
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900101 struct dma_chan *chan_tx;
102 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +0900103
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900104#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900105 dma_cookie_t cookie_tx;
106 dma_cookie_t cookie_rx[2];
107 dma_cookie_t active_rx;
Geert Uytterhoeven79904422015-08-21 20:02:42 +0200108 dma_addr_t tx_dma_addr;
109 unsigned int tx_dma_len;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900110 struct scatterlist sg_rx[2];
Yoshihiro Shimoda7b39d902015-08-21 20:02:54 +0200111 void *rx_buf[2];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900112 size_t buf_len_rx;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900113 struct work_struct work_tx;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900114 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000115 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900116#endif
Magnus Damme552de22009-01-21 15:13:42 +0000117
Paul Mundtd535a232011-01-19 17:19:35 +0900118 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900119};
120
Paul Mundte108b2c2006-09-27 16:32:13 +0900121#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
122
123static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static struct uart_driver sci_uart_driver;
125
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900126static inline struct sci_port *
127to_sci_port(struct uart_port *uart)
128{
129 return container_of(uart, struct sci_port, port);
130}
131
Paul Mundt61a69762011-06-14 12:40:19 +0900132struct plat_sci_reg {
133 u8 offset, size;
134};
135
136/* Helper for invalidating specific entries of an inherited map. */
137#define sci_reg_invalid { .offset = 0, .size = 0 }
138
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200139static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
Paul Mundt61a69762011-06-14 12:40:19 +0900140 [SCIx_PROBE_REGTYPE] = {
141 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
142 },
143
144 /*
145 * Common SCI definitions, dependent on the port's regshift
146 * value.
147 */
148 [SCIx_SCI_REGTYPE] = {
149 [SCSMR] = { 0x00, 8 },
150 [SCBRR] = { 0x01, 8 },
151 [SCSCR] = { 0x02, 8 },
152 [SCxTDR] = { 0x03, 8 },
153 [SCxSR] = { 0x04, 8 },
154 [SCxRDR] = { 0x05, 8 },
155 [SCFCR] = sci_reg_invalid,
156 [SCFDR] = sci_reg_invalid,
157 [SCTFDR] = sci_reg_invalid,
158 [SCRFDR] = sci_reg_invalid,
159 [SCSPTR] = sci_reg_invalid,
160 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200161 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200162 [SCPCR] = sci_reg_invalid,
163 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100164 [SCDL] = sci_reg_invalid,
165 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900166 },
167
168 /*
169 * Common definitions for legacy IrDA ports, dependent on
170 * regshift value.
171 */
172 [SCIx_IRDA_REGTYPE] = {
173 [SCSMR] = { 0x00, 8 },
174 [SCBRR] = { 0x01, 8 },
175 [SCSCR] = { 0x02, 8 },
176 [SCxTDR] = { 0x03, 8 },
177 [SCxSR] = { 0x04, 8 },
178 [SCxRDR] = { 0x05, 8 },
179 [SCFCR] = { 0x06, 8 },
180 [SCFDR] = { 0x07, 16 },
181 [SCTFDR] = sci_reg_invalid,
182 [SCRFDR] = sci_reg_invalid,
183 [SCSPTR] = sci_reg_invalid,
184 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200185 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200186 [SCPCR] = sci_reg_invalid,
187 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100188 [SCDL] = sci_reg_invalid,
189 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900190 },
191
192 /*
193 * Common SCIFA definitions.
194 */
195 [SCIx_SCIFA_REGTYPE] = {
196 [SCSMR] = { 0x00, 16 },
197 [SCBRR] = { 0x04, 8 },
198 [SCSCR] = { 0x08, 16 },
199 [SCxTDR] = { 0x20, 8 },
200 [SCxSR] = { 0x14, 16 },
201 [SCxRDR] = { 0x24, 8 },
202 [SCFCR] = { 0x18, 16 },
203 [SCFDR] = { 0x1c, 16 },
204 [SCTFDR] = sci_reg_invalid,
205 [SCRFDR] = sci_reg_invalid,
206 [SCSPTR] = sci_reg_invalid,
207 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200208 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200209 [SCPCR] = { 0x30, 16 },
210 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100211 [SCDL] = sci_reg_invalid,
212 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900213 },
214
215 /*
216 * Common SCIFB definitions.
217 */
218 [SCIx_SCIFB_REGTYPE] = {
219 [SCSMR] = { 0x00, 16 },
220 [SCBRR] = { 0x04, 8 },
221 [SCSCR] = { 0x08, 16 },
222 [SCxTDR] = { 0x40, 8 },
223 [SCxSR] = { 0x14, 16 },
224 [SCxRDR] = { 0x60, 8 },
225 [SCFCR] = { 0x18, 16 },
Takashi Yoshii8c66d6d2012-11-16 10:53:31 +0900226 [SCFDR] = sci_reg_invalid,
227 [SCTFDR] = { 0x38, 16 },
228 [SCRFDR] = { 0x3c, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900229 [SCSPTR] = sci_reg_invalid,
230 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200231 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200232 [SCPCR] = { 0x30, 16 },
233 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100234 [SCDL] = sci_reg_invalid,
235 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900236 },
237
238 /*
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100239 * Common SH-2(A) SCIF definitions for ports with FIFO data
240 * count registers.
241 */
242 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
243 [SCSMR] = { 0x00, 16 },
244 [SCBRR] = { 0x04, 8 },
245 [SCSCR] = { 0x08, 16 },
246 [SCxTDR] = { 0x0c, 8 },
247 [SCxSR] = { 0x10, 16 },
248 [SCxRDR] = { 0x14, 8 },
249 [SCFCR] = { 0x18, 16 },
250 [SCFDR] = { 0x1c, 16 },
251 [SCTFDR] = sci_reg_invalid,
252 [SCRFDR] = sci_reg_invalid,
253 [SCSPTR] = { 0x20, 16 },
254 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200255 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200256 [SCPCR] = sci_reg_invalid,
257 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100258 [SCDL] = sci_reg_invalid,
259 [SCCKS] = sci_reg_invalid,
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100260 },
261
262 /*
Paul Mundt61a69762011-06-14 12:40:19 +0900263 * Common SH-3 SCIF definitions.
264 */
265 [SCIx_SH3_SCIF_REGTYPE] = {
266 [SCSMR] = { 0x00, 8 },
267 [SCBRR] = { 0x02, 8 },
268 [SCSCR] = { 0x04, 8 },
269 [SCxTDR] = { 0x06, 8 },
270 [SCxSR] = { 0x08, 16 },
271 [SCxRDR] = { 0x0a, 8 },
272 [SCFCR] = { 0x0c, 8 },
273 [SCFDR] = { 0x0e, 16 },
274 [SCTFDR] = sci_reg_invalid,
275 [SCRFDR] = sci_reg_invalid,
276 [SCSPTR] = sci_reg_invalid,
277 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200278 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200279 [SCPCR] = sci_reg_invalid,
280 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100281 [SCDL] = sci_reg_invalid,
282 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900283 },
284
285 /*
286 * Common SH-4(A) SCIF(B) definitions.
287 */
288 [SCIx_SH4_SCIF_REGTYPE] = {
289 [SCSMR] = { 0x00, 16 },
290 [SCBRR] = { 0x04, 8 },
291 [SCSCR] = { 0x08, 16 },
292 [SCxTDR] = { 0x0c, 8 },
293 [SCxSR] = { 0x10, 16 },
294 [SCxRDR] = { 0x14, 8 },
295 [SCFCR] = { 0x18, 16 },
296 [SCFDR] = { 0x1c, 16 },
297 [SCTFDR] = sci_reg_invalid,
298 [SCRFDR] = sci_reg_invalid,
299 [SCSPTR] = { 0x20, 16 },
300 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200301 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200302 [SCPCR] = sci_reg_invalid,
303 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100304 [SCDL] = sci_reg_invalid,
305 [SCCKS] = sci_reg_invalid,
306 },
307
308 /*
309 * Common SCIF definitions for ports with a Baud Rate Generator for
310 * External Clock (BRG).
311 */
312 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
313 [SCSMR] = { 0x00, 16 },
314 [SCBRR] = { 0x04, 8 },
315 [SCSCR] = { 0x08, 16 },
316 [SCxTDR] = { 0x0c, 8 },
317 [SCxSR] = { 0x10, 16 },
318 [SCxRDR] = { 0x14, 8 },
319 [SCFCR] = { 0x18, 16 },
320 [SCFDR] = { 0x1c, 16 },
321 [SCTFDR] = sci_reg_invalid,
322 [SCRFDR] = sci_reg_invalid,
323 [SCSPTR] = { 0x20, 16 },
324 [SCLSR] = { 0x24, 16 },
325 [HSSRR] = sci_reg_invalid,
326 [SCPCR] = sci_reg_invalid,
327 [SCPDR] = sci_reg_invalid,
328 [SCDL] = { 0x30, 16 },
329 [SCCKS] = { 0x34, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200330 },
331
332 /*
333 * Common HSCIF definitions.
334 */
335 [SCIx_HSCIF_REGTYPE] = {
336 [SCSMR] = { 0x00, 16 },
337 [SCBRR] = { 0x04, 8 },
338 [SCSCR] = { 0x08, 16 },
339 [SCxTDR] = { 0x0c, 8 },
340 [SCxSR] = { 0x10, 16 },
341 [SCxRDR] = { 0x14, 8 },
342 [SCFCR] = { 0x18, 16 },
343 [SCFDR] = { 0x1c, 16 },
344 [SCTFDR] = sci_reg_invalid,
345 [SCRFDR] = sci_reg_invalid,
346 [SCSPTR] = { 0x20, 16 },
347 [SCLSR] = { 0x24, 16 },
348 [HSSRR] = { 0x40, 16 },
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200349 [SCPCR] = sci_reg_invalid,
350 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100351 [SCDL] = { 0x30, 16 },
352 [SCCKS] = { 0x34, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900353 },
354
355 /*
356 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
357 * register.
358 */
359 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
360 [SCSMR] = { 0x00, 16 },
361 [SCBRR] = { 0x04, 8 },
362 [SCSCR] = { 0x08, 16 },
363 [SCxTDR] = { 0x0c, 8 },
364 [SCxSR] = { 0x10, 16 },
365 [SCxRDR] = { 0x14, 8 },
366 [SCFCR] = { 0x18, 16 },
367 [SCFDR] = { 0x1c, 16 },
368 [SCTFDR] = sci_reg_invalid,
369 [SCRFDR] = sci_reg_invalid,
370 [SCSPTR] = sci_reg_invalid,
371 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200372 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200373 [SCPCR] = sci_reg_invalid,
374 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100375 [SCDL] = sci_reg_invalid,
376 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900377 },
378
379 /*
380 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
381 * count registers.
382 */
383 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
384 [SCSMR] = { 0x00, 16 },
385 [SCBRR] = { 0x04, 8 },
386 [SCSCR] = { 0x08, 16 },
387 [SCxTDR] = { 0x0c, 8 },
388 [SCxSR] = { 0x10, 16 },
389 [SCxRDR] = { 0x14, 8 },
390 [SCFCR] = { 0x18, 16 },
391 [SCFDR] = { 0x1c, 16 },
392 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
393 [SCRFDR] = { 0x20, 16 },
394 [SCSPTR] = { 0x24, 16 },
395 [SCLSR] = { 0x28, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200396 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200397 [SCPCR] = sci_reg_invalid,
398 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100399 [SCDL] = sci_reg_invalid,
400 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900401 },
402
403 /*
404 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
405 * registers.
406 */
407 [SCIx_SH7705_SCIF_REGTYPE] = {
408 [SCSMR] = { 0x00, 16 },
409 [SCBRR] = { 0x04, 8 },
410 [SCSCR] = { 0x08, 16 },
411 [SCxTDR] = { 0x20, 8 },
412 [SCxSR] = { 0x14, 16 },
413 [SCxRDR] = { 0x24, 8 },
414 [SCFCR] = { 0x18, 16 },
415 [SCFDR] = { 0x1c, 16 },
416 [SCTFDR] = sci_reg_invalid,
417 [SCRFDR] = sci_reg_invalid,
418 [SCSPTR] = sci_reg_invalid,
419 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200420 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200421 [SCPCR] = sci_reg_invalid,
422 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100423 [SCDL] = sci_reg_invalid,
424 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900425 },
426};
427
Paul Mundt72b294c2011-06-14 17:38:19 +0900428#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
429
Paul Mundt61a69762011-06-14 12:40:19 +0900430/*
431 * The "offset" here is rather misleading, in that it refers to an enum
432 * value relative to the port mapping rather than the fixed offset
433 * itself, which needs to be manually retrieved from the platform's
434 * register map for the given port.
435 */
436static unsigned int sci_serial_in(struct uart_port *p, int offset)
437{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200438 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900439
440 if (reg->size == 8)
441 return ioread8(p->membase + (reg->offset << p->regshift));
442 else if (reg->size == 16)
443 return ioread16(p->membase + (reg->offset << p->regshift));
444 else
445 WARN(1, "Invalid register access\n");
446
447 return 0;
448}
449
450static void sci_serial_out(struct uart_port *p, int offset, int value)
451{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200452 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900453
454 if (reg->size == 8)
455 iowrite8(value, p->membase + (reg->offset << p->regshift));
456 else if (reg->size == 16)
457 iowrite16(value, p->membase + (reg->offset << p->regshift));
458 else
459 WARN(1, "Invalid register access\n");
460}
461
Paul Mundt61a69762011-06-14 12:40:19 +0900462static int sci_probe_regmap(struct plat_sci_port *cfg)
463{
464 switch (cfg->type) {
465 case PORT_SCI:
466 cfg->regtype = SCIx_SCI_REGTYPE;
467 break;
468 case PORT_IRDA:
469 cfg->regtype = SCIx_IRDA_REGTYPE;
470 break;
471 case PORT_SCIFA:
472 cfg->regtype = SCIx_SCIFA_REGTYPE;
473 break;
474 case PORT_SCIFB:
475 cfg->regtype = SCIx_SCIFB_REGTYPE;
476 break;
477 case PORT_SCIF:
478 /*
479 * The SH-4 is a bit of a misnomer here, although that's
480 * where this particular port layout originated. This
481 * configuration (or some slight variation thereof)
482 * remains the dominant model for all SCIFs.
483 */
484 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
485 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +0200486 case PORT_HSCIF:
487 cfg->regtype = SCIx_HSCIF_REGTYPE;
488 break;
Paul Mundt61a69762011-06-14 12:40:19 +0900489 default:
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +0100490 pr_err("Can't probe register map for given port\n");
Paul Mundt61a69762011-06-14 12:40:19 +0900491 return -EINVAL;
492 }
493
494 return 0;
495}
496
Paul Mundt23241d42011-06-28 13:55:31 +0900497static void sci_port_enable(struct sci_port *sci_port)
498{
499 if (!sci_port->port.dev)
500 return;
501
502 pm_runtime_get_sync(sci_port->port.dev);
503
Laurent Pinchartb016b642013-11-28 18:11:46 +0100504 clk_prepare_enable(sci_port->fclk);
Laurent Pincharta9ec81f2015-09-14 15:14:23 +0300505 sci_port->port.uartclk = clk_get_rate(sci_port->fclk);
Paul Mundt23241d42011-06-28 13:55:31 +0900506}
507
508static void sci_port_disable(struct sci_port *sci_port)
509{
510 if (!sci_port->port.dev)
511 return;
512
Laurent Pinchartcaec7032013-11-28 18:11:45 +0100513 /* Cancel the break timer to ensure that the timer handler will not try
514 * to access the hardware with clocks and power disabled. Reset the
515 * break flag to make the break debouncing state machine ready for the
516 * next break.
517 */
518 del_timer_sync(&sci_port->break_timer);
519 sci_port->break_flag = 0;
520
Laurent Pinchartb016b642013-11-28 18:11:46 +0100521 clk_disable_unprepare(sci_port->fclk);
Paul Mundt23241d42011-06-28 13:55:31 +0900522
523 pm_runtime_put_sync(sci_port->port.dev);
524}
525
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +0200526static inline unsigned long port_rx_irq_mask(struct uart_port *port)
527{
528 /*
529 * Not all ports (such as SCIFA) will support REIE. Rather than
530 * special-casing the port type, we check the port initialization
531 * IRQ enable mask to see whether the IRQ is desired at all. If
532 * it's unset, it's logically inferred that there's no point in
533 * testing for it.
534 */
535 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
536}
537
538static void sci_start_tx(struct uart_port *port)
539{
540 struct sci_port *s = to_sci_port(port);
541 unsigned short ctrl;
542
543#ifdef CONFIG_SERIAL_SH_SCI_DMA
544 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
545 u16 new, scr = serial_port_in(port, SCSCR);
546 if (s->chan_tx)
547 new = scr | SCSCR_TDRQE;
548 else
549 new = scr & ~SCSCR_TDRQE;
550 if (new != scr)
551 serial_port_out(port, SCSCR, new);
552 }
553
554 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
555 dma_submit_error(s->cookie_tx)) {
556 s->cookie_tx = 0;
557 schedule_work(&s->work_tx);
558 }
559#endif
560
561 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
562 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
563 ctrl = serial_port_in(port, SCSCR);
564 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
565 }
566}
567
568static void sci_stop_tx(struct uart_port *port)
569{
570 unsigned short ctrl;
571
572 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
573 ctrl = serial_port_in(port, SCSCR);
574
575 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
576 ctrl &= ~SCSCR_TDRQE;
577
578 ctrl &= ~SCSCR_TIE;
579
580 serial_port_out(port, SCSCR, ctrl);
581}
582
583static void sci_start_rx(struct uart_port *port)
584{
585 unsigned short ctrl;
586
587 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
588
589 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
590 ctrl &= ~SCSCR_RDRQE;
591
592 serial_port_out(port, SCSCR, ctrl);
593}
594
595static void sci_stop_rx(struct uart_port *port)
596{
597 unsigned short ctrl;
598
599 ctrl = serial_port_in(port, SCSCR);
600
601 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
602 ctrl &= ~SCSCR_RDRQE;
603
604 ctrl &= ~port_rx_irq_mask(port);
605
606 serial_port_out(port, SCSCR, ctrl);
607}
608
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200609static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
610{
611 if (port->type == PORT_SCI) {
612 /* Just store the mask */
613 serial_port_out(port, SCxSR, mask);
614 } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
615 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
616 /* Only clear the status bits we want to clear */
617 serial_port_out(port, SCxSR,
618 serial_port_in(port, SCxSR) & mask);
619 } else {
620 /* Store the mask, clear parity/framing errors */
621 serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
622 }
623}
624
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900625#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900626
627#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900628static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned short status;
631 int c;
632
Paul Mundte108b2c2006-09-27 16:32:13 +0900633 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900634 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (status & SCxSR_ERRORS(port)) {
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200636 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 continue;
638 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500639 break;
640 } while (1);
641
642 if (!(status & SCxSR_RDxF(port)))
643 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900644
Paul Mundtb12bb292012-03-30 19:50:15 +0900645 c = serial_port_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900646
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900647 /* Dummy read */
Paul Mundtb12bb292012-03-30 19:50:15 +0900648 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200649 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return c;
652}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900655static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned short status;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900660 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 } while (!(status & SCxSR_TDxE(port)));
662
Paul Mundtb12bb292012-03-30 19:50:15 +0900663 serial_port_out(port, SCxTDR, c);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200664 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900666#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Paul Mundt61a69762011-06-14 12:40:19 +0900668static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900669{
Paul Mundt61a69762011-06-14 12:40:19 +0900670 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200671 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900672
Paul Mundt61a69762011-06-14 12:40:19 +0900673 /*
674 * Use port-specific handler if provided.
675 */
676 if (s->cfg->ops && s->cfg->ops->init_pins) {
677 s->cfg->ops->init_pins(port, cflag);
678 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Paul Mundt61a69762011-06-14 12:40:19 +0900681 /*
682 * For the generic path SCSPTR is necessary. Bail out if that's
683 * unavailable, too.
684 */
685 if (!reg->size)
686 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800687
Paul Mundtfaf02f82011-12-02 17:44:50 +0900688 if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
689 ((!(cflag & CRTSCTS)))) {
690 unsigned short status;
691
Paul Mundtb12bb292012-03-30 19:50:15 +0900692 status = serial_port_in(port, SCSPTR);
Paul Mundtfaf02f82011-12-02 17:44:50 +0900693 status &= ~SCSPTR_CTSIO;
694 status |= SCSPTR_RTSIO;
Paul Mundtb12bb292012-03-30 19:50:15 +0900695 serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
Paul Mundtfaf02f82011-12-02 17:44:50 +0900696 }
Paul Mundtd5701642008-12-16 20:07:27 +0900697}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900699static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900700{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200701 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900702
703 reg = sci_getreg(port, SCTFDR);
704 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900705 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900706
707 reg = sci_getreg(port, SCFDR);
708 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900709 return serial_port_in(port, SCFDR) >> 8;
Paul Mundt72b294c2011-06-14 17:38:19 +0900710
Paul Mundtb12bb292012-03-30 19:50:15 +0900711 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900712}
713
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900714static int sci_txroom(struct uart_port *port)
715{
Paul Mundt72b294c2011-06-14 17:38:19 +0900716 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900717}
718
719static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900720{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200721 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900722
723 reg = sci_getreg(port, SCRFDR);
724 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900725 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900726
727 reg = sci_getreg(port, SCFDR);
728 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900729 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900730
Paul Mundtb12bb292012-03-30 19:50:15 +0900731 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900732}
733
Paul Mundt514820e2011-06-08 18:51:32 +0900734/*
735 * SCI helper for checking the state of the muxed port/RXD pins.
736 */
737static inline int sci_rxd_in(struct uart_port *port)
738{
739 struct sci_port *s = to_sci_port(port);
740
741 if (s->cfg->port_reg <= 0)
742 return 1;
743
Paul Mundt0dd4d5c2012-10-15 14:08:48 +0900744 /* Cast for ARM damage */
Laurent Pincharte2afca62013-12-11 13:40:31 +0100745 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
Paul Mundt514820e2011-06-08 18:51:32 +0900746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* ********************************************************************** *
749 * the interrupt related routines *
750 * ********************************************************************** */
751
752static void sci_transmit_chars(struct uart_port *port)
753{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700754 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 unsigned short status;
757 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900758 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Paul Mundtb12bb292012-03-30 19:50:15 +0900760 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!(status & SCxSR_TDxE(port))) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900762 ctrl = serial_port_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900763 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900764 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900765 else
Paul Mundt8e698612009-06-24 19:44:32 +0900766 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900767 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return;
769 }
770
Paul Mundt72b294c2011-06-14 17:38:19 +0900771 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 do {
774 unsigned char c;
775
776 if (port->x_char) {
777 c = port->x_char;
778 port->x_char = 0;
779 } else if (!uart_circ_empty(xmit) && !stopped) {
780 c = xmit->buf[xmit->tail];
781 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
782 } else {
783 break;
784 }
785
Paul Mundtb12bb292012-03-30 19:50:15 +0900786 serial_port_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 port->icount.tx++;
789 } while (--count > 0);
790
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200791 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
794 uart_write_wakeup(port);
795 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100796 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900798 ctrl = serial_port_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900800 if (port->type != PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900801 serial_port_in(port, SCxSR); /* Dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200802 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Paul Mundt8e698612009-06-24 19:44:32 +0900805 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900806 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808}
809
810/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900811#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900813static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900815 struct sci_port *sci_port = to_sci_port(port);
Jiri Slaby227434f2013-01-03 15:53:01 +0100816 struct tty_port *tport = &port->state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int i, count, copied = 0;
818 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800819 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Paul Mundtb12bb292012-03-30 19:50:15 +0900821 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!(status & SCxSR_RDxF(port)))
823 return;
824
825 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Don't copy more bytes than there is room for in the buffer */
Jiri Slaby227434f2013-01-03 15:53:01 +0100827 count = tty_buffer_request_room(tport, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* If for any reason we can't copy more data, we're done! */
830 if (count == 0)
831 break;
832
833 if (port->type == PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900834 char c = serial_port_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900835 if (uart_handle_sysrq_char(port, c) ||
836 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900838 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100839 tty_insert_flip_char(tport, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900841 for (i = 0; i < count; i++) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900842 char c = serial_port_in(port, SCxRDR);
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900843
Paul Mundtb12bb292012-03-30 19:50:15 +0900844 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#if defined(CONFIG_CPU_SH3)
846 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900847 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if ((c == 0) &&
849 (status & SCxSR_FER(port))) {
850 count--; i--;
851 continue;
852 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900855 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900856 sci_port->break_flag = 0;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (STEPFN(c)) {
859 count--; i--;
860 continue;
861 }
862 }
863#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100864 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 count--; i--;
866 continue;
867 }
868
869 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900870 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800871 flag = TTY_FRAME;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900872 port->icount.frame++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900873 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900874 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800875 flag = TTY_PARITY;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900876 port->icount.parity++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900877 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800878 } else
879 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900880
Jiri Slaby92a19f92013-01-03 15:53:03 +0100881 tty_insert_flip_char(tport, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883 }
884
Paul Mundtb12bb292012-03-30 19:50:15 +0900885 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200886 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 copied += count;
889 port->icount.rx += count;
890 }
891
892 if (copied) {
893 /* Tell the rest of the system the news. New characters! */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100894 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900896 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200897 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899}
900
901#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900902
903/*
904 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * 1 per millisecond or so during the break period, for 9600 baud.
906 * So dont bother disabling interrupts.
907 * But dont want more than 1 break event.
908 * Use a kernel timer to periodically poll the rx line until
909 * the break is finished.
910 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900911static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900913 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/* Ensure that two consecutive samples find the break over. */
917static void sci_break_timer(unsigned long data)
918{
Paul Mundte108b2c2006-09-27 16:32:13 +0900919 struct sci_port *port = (struct sci_port *)data;
920
921 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900923 sci_schedule_break_timer(port);
924 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* break is over. */
926 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900927 sci_schedule_break_timer(port);
928 } else
929 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900932static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900935 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100936 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900937 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100939 /* Handle overruns */
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +0200940 if (status & s->overrun_mask) {
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100941 port->icount.overrun++;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900942
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100943 /* overrun error */
944 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
945 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900946
Joe Perches9b971cd2014-03-11 10:10:46 -0700947 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Paul Mundte108b2c2006-09-27 16:32:13 +0900950 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (sci_rxd_in(port) == 0) {
952 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900953 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900954
955 if (!sci_port->break_flag) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900956 port->icount.brk++;
957
Paul Mundte108b2c2006-09-27 16:32:13 +0900958 sci_port->break_flag = 1;
959 sci_schedule_break_timer(sci_port);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900962 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900964
965 dev_dbg(port->dev, "BREAK detected\n");
966
Jiri Slaby92a19f92013-01-03 15:53:03 +0100967 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900968 copied++;
969 }
970
Paul Mundte108b2c2006-09-27 16:32:13 +0900971 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* frame error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900973 port->icount.frame++;
974
Jiri Slaby92a19f92013-01-03 15:53:03 +0100975 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800976 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900977
978 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
981
Paul Mundte108b2c2006-09-27 16:32:13 +0900982 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* parity error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900984 port->icount.parity++;
985
Jiri Slaby92a19f92013-01-03 15:53:03 +0100986 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
Paul Mundte108b2c2006-09-27 16:32:13 +0900987 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900988
Joe Perches9b971cd2014-03-11 10:10:46 -0700989 dev_notice(port->dev, "parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Alan Cox33f0f882006-01-09 20:54:13 -0800992 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100993 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 return copied;
996}
997
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900998static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900999{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001000 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +09001001 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001002 const struct plat_sci_reg *reg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001003 int copied = 0;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001004 u16 status;
Paul Mundtd830fa42008-12-16 19:29:38 +09001005
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001006 reg = sci_getreg(port, s->overrun_reg);
Paul Mundt4b8c59a2011-06-14 17:53:34 +09001007 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +09001008 return 0;
1009
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001010 status = serial_port_in(port, s->overrun_reg);
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001011 if (status & s->overrun_mask) {
1012 status &= ~s->overrun_mask;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001013 serial_port_out(port, s->overrun_reg, status);
Paul Mundtd830fa42008-12-16 19:29:38 +09001014
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001015 port->icount.overrun++;
1016
Jiri Slaby92a19f92013-01-03 15:53:03 +01001017 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001018 tty_flip_buffer_push(tport);
Paul Mundtd830fa42008-12-16 19:29:38 +09001019
Yoshihiro Kaneko51b31f12015-01-26 20:53:29 +09001020 dev_dbg(port->dev, "overrun error\n");
Paul Mundtd830fa42008-12-16 19:29:38 +09001021 copied++;
1022 }
1023
1024 return copied;
1025}
1026
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001027static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +09001030 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +01001031 struct tty_port *tport = &port->state->port;
Magnus Damma5660ad2009-01-21 15:14:38 +00001032 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Paul Mundt0b3d4ef2007-03-14 13:22:37 +09001034 if (uart_handle_break(port))
1035 return 0;
1036
Paul Mundtb7a76e42006-02-01 03:06:06 -08001037 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038#if defined(CONFIG_CPU_SH3)
1039 /* Debounce break */
1040 s->break_flag = 1;
1041#endif
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001042
1043 port->icount.brk++;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Notify of BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001046 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -08001047 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001048
1049 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Alan Cox33f0f882006-01-09 20:54:13 -08001052 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001053 tty_flip_buffer_push(tport);
Paul Mundte108b2c2006-09-27 16:32:13 +09001054
Paul Mundtd830fa42008-12-16 19:29:38 +09001055 copied += sci_handle_fifo_overrun(port);
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return copied;
1058}
1059
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001060#ifdef CONFIG_SERIAL_SH_SCI_DMA
1061static void sci_dma_tx_complete(void *arg)
1062{
1063 struct sci_port *s = arg;
1064 struct uart_port *port = &s->port;
1065 struct circ_buf *xmit = &port->state->xmit;
1066 unsigned long flags;
1067
1068 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1069
1070 spin_lock_irqsave(&port->lock, flags);
1071
1072 xmit->tail += s->tx_dma_len;
1073 xmit->tail &= UART_XMIT_SIZE - 1;
1074
1075 port->icount.tx += s->tx_dma_len;
1076
1077 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1078 uart_write_wakeup(port);
1079
1080 if (!uart_circ_empty(xmit)) {
1081 s->cookie_tx = 0;
1082 schedule_work(&s->work_tx);
1083 } else {
1084 s->cookie_tx = -EINVAL;
1085 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1086 u16 ctrl = serial_port_in(port, SCSCR);
1087 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1088 }
1089 }
1090
1091 spin_unlock_irqrestore(&port->lock, flags);
1092}
1093
1094/* Locking: called with port lock held */
1095static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1096{
1097 struct uart_port *port = &s->port;
1098 struct tty_port *tport = &port->state->port;
1099 int copied;
1100
1101 copied = tty_insert_flip_string(tport, buf, count);
1102 if (copied < count) {
1103 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
1104 count - copied);
1105 port->icount.buf_overrun++;
1106 }
1107
1108 port->icount.rx += copied;
1109
1110 return copied;
1111}
1112
1113static int sci_dma_rx_find_active(struct sci_port *s)
1114{
1115 unsigned int i;
1116
1117 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1118 if (s->active_rx == s->cookie_rx[i])
1119 return i;
1120
1121 dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
1122 s->active_rx);
1123 return -1;
1124}
1125
1126static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1127{
1128 struct dma_chan *chan = s->chan_rx;
1129 struct uart_port *port = &s->port;
1130 unsigned long flags;
1131
1132 spin_lock_irqsave(&port->lock, flags);
1133 s->chan_rx = NULL;
1134 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1135 spin_unlock_irqrestore(&port->lock, flags);
1136 dmaengine_terminate_all(chan);
1137 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1138 sg_dma_address(&s->sg_rx[0]));
1139 dma_release_channel(chan);
1140 if (enable_pio)
1141 sci_start_rx(port);
1142}
1143
1144static void sci_dma_rx_complete(void *arg)
1145{
1146 struct sci_port *s = arg;
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001147 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001148 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001149 struct dma_async_tx_descriptor *desc;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001150 unsigned long flags;
1151 int active, count = 0;
1152
1153 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1154 s->active_rx);
1155
1156 spin_lock_irqsave(&port->lock, flags);
1157
1158 active = sci_dma_rx_find_active(s);
1159 if (active >= 0)
1160 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1161
1162 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1163
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001164 if (count)
1165 tty_flip_buffer_push(&port->state->port);
1166
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001167 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1168 DMA_DEV_TO_MEM,
1169 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1170 if (!desc)
1171 goto fail;
1172
1173 desc->callback = sci_dma_rx_complete;
1174 desc->callback_param = s;
1175 s->cookie_rx[active] = dmaengine_submit(desc);
1176 if (dma_submit_error(s->cookie_rx[active]))
1177 goto fail;
1178
1179 s->active_rx = s->cookie_rx[!active];
1180
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001181 dma_async_issue_pending(chan);
1182
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001183 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1184 __func__, s->cookie_rx[active], active, s->active_rx);
1185 spin_unlock_irqrestore(&port->lock, flags);
1186 return;
1187
1188fail:
1189 spin_unlock_irqrestore(&port->lock, flags);
1190 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1191 sci_rx_dma_release(s, true);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001192}
1193
1194static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1195{
1196 struct dma_chan *chan = s->chan_tx;
1197 struct uart_port *port = &s->port;
1198 unsigned long flags;
1199
1200 spin_lock_irqsave(&port->lock, flags);
1201 s->chan_tx = NULL;
1202 s->cookie_tx = -EINVAL;
1203 spin_unlock_irqrestore(&port->lock, flags);
1204 dmaengine_terminate_all(chan);
1205 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1206 DMA_TO_DEVICE);
1207 dma_release_channel(chan);
1208 if (enable_pio)
1209 sci_start_tx(port);
1210}
1211
1212static void sci_submit_rx(struct sci_port *s)
1213{
1214 struct dma_chan *chan = s->chan_rx;
1215 int i;
1216
1217 for (i = 0; i < 2; i++) {
1218 struct scatterlist *sg = &s->sg_rx[i];
1219 struct dma_async_tx_descriptor *desc;
1220
1221 desc = dmaengine_prep_slave_sg(chan,
1222 sg, 1, DMA_DEV_TO_MEM,
1223 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1224 if (!desc)
1225 goto fail;
1226
1227 desc->callback = sci_dma_rx_complete;
1228 desc->callback_param = s;
1229 s->cookie_rx[i] = dmaengine_submit(desc);
1230 if (dma_submit_error(s->cookie_rx[i]))
1231 goto fail;
1232
1233 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1234 s->cookie_rx[i], i);
1235 }
1236
1237 s->active_rx = s->cookie_rx[0];
1238
1239 dma_async_issue_pending(chan);
1240 return;
1241
1242fail:
1243 if (i)
1244 dmaengine_terminate_all(chan);
1245 for (i = 0; i < 2; i++)
1246 s->cookie_rx[i] = -EINVAL;
1247 s->active_rx = -EINVAL;
1248 dev_warn(s->port.dev, "Failed to re-start Rx DMA, using PIO\n");
1249 sci_rx_dma_release(s, true);
1250}
1251
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001252static void work_fn_tx(struct work_struct *work)
1253{
1254 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1255 struct dma_async_tx_descriptor *desc;
1256 struct dma_chan *chan = s->chan_tx;
1257 struct uart_port *port = &s->port;
1258 struct circ_buf *xmit = &port->state->xmit;
1259 dma_addr_t buf;
1260
1261 /*
1262 * DMA is idle now.
1263 * Port xmit buffer is already mapped, and it is one page... Just adjust
1264 * offsets and lengths. Since it is a circular buffer, we have to
1265 * transmit till the end, and then the rest. Take the port lock to get a
1266 * consistent xmit buffer state.
1267 */
1268 spin_lock_irq(&port->lock);
1269 buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1));
1270 s->tx_dma_len = min_t(unsigned int,
1271 CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1272 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1273 spin_unlock_irq(&port->lock);
1274
1275 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1276 DMA_MEM_TO_DEV,
1277 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1278 if (!desc) {
1279 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1280 /* switch to PIO */
1281 sci_tx_dma_release(s, true);
1282 return;
1283 }
1284
1285 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1286 DMA_TO_DEVICE);
1287
1288 spin_lock_irq(&port->lock);
1289 desc->callback = sci_dma_tx_complete;
1290 desc->callback_param = s;
1291 spin_unlock_irq(&port->lock);
1292 s->cookie_tx = dmaengine_submit(desc);
1293 if (dma_submit_error(s->cookie_tx)) {
1294 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1295 /* switch to PIO */
1296 sci_tx_dma_release(s, true);
1297 return;
1298 }
1299
1300 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
1301 __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1302
1303 dma_async_issue_pending(chan);
1304}
1305
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001306static void rx_timer_fn(unsigned long arg)
1307{
1308 struct sci_port *s = (struct sci_port *)arg;
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001309 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001310 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001311 struct dma_tx_state state;
1312 enum dma_status status;
1313 unsigned long flags;
1314 unsigned int read;
1315 int active, count;
1316 u16 scr;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001317
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001318 spin_lock_irqsave(&port->lock, flags);
1319
1320 dev_dbg(port->dev, "DMA Rx timed out\n");
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001321
1322 active = sci_dma_rx_find_active(s);
1323 if (active < 0) {
1324 spin_unlock_irqrestore(&port->lock, flags);
1325 return;
1326 }
1327
1328 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001329 if (status == DMA_COMPLETE) {
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001330 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1331 s->active_rx, active);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001332 spin_unlock_irqrestore(&port->lock, flags);
1333
1334 /* Let packet complete handler take care of the packet */
1335 return;
1336 }
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001337
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001338 dmaengine_pause(chan);
1339
1340 /*
1341 * sometimes DMA transfer doesn't stop even if it is stopped and
1342 * data keeps on coming until transaction is complete so check
1343 * for DMA_COMPLETE again
1344 * Let packet complete handler take care of the packet
1345 */
1346 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1347 if (status == DMA_COMPLETE) {
1348 spin_unlock_irqrestore(&port->lock, flags);
1349 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1350 return;
1351 }
1352
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001353 /* Handle incomplete DMA receive */
1354 dmaengine_terminate_all(s->chan_rx);
1355 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1356 dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
1357 s->active_rx);
1358
1359 if (read) {
1360 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1361 if (count)
1362 tty_flip_buffer_push(&port->state->port);
1363 }
1364
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001365 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1366 sci_submit_rx(s);
Muhammad Hamza Farooq371cfed2015-09-18 13:08:29 +02001367
1368 /* Direct new serial port interrupts back to CPU */
1369 scr = serial_port_in(port, SCSCR);
1370 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1371 scr &= ~SCSCR_RDRQE;
1372 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1373 }
1374 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1375
1376 spin_unlock_irqrestore(&port->lock, flags);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001377}
1378
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001379static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1380 enum dma_transfer_direction dir,
1381 unsigned int id)
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001382{
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001383 dma_cap_mask_t mask;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001384 struct dma_chan *chan;
1385 struct dma_slave_config cfg;
1386 int ret;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001387
1388 dma_cap_zero(mask);
1389 dma_cap_set(DMA_SLAVE, mask);
1390
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001391 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1392 (void *)(unsigned long)id, port->dev,
1393 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1394 if (!chan) {
1395 dev_warn(port->dev,
1396 "dma_request_slave_channel_compat failed\n");
1397 return NULL;
1398 }
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001399
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001400 memset(&cfg, 0, sizeof(cfg));
1401 cfg.direction = dir;
1402 if (dir == DMA_MEM_TO_DEV) {
1403 cfg.dst_addr = port->mapbase +
1404 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1405 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1406 } else {
1407 cfg.src_addr = port->mapbase +
1408 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1409 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1410 }
1411
1412 ret = dmaengine_slave_config(chan, &cfg);
1413 if (ret) {
1414 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1415 dma_release_channel(chan);
1416 return NULL;
1417 }
1418
1419 return chan;
1420}
1421
1422static void sci_request_dma(struct uart_port *port)
1423{
1424 struct sci_port *s = to_sci_port(port);
1425 struct dma_chan *chan;
1426
1427 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1428
1429 if (!port->dev->of_node &&
1430 (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
1431 return;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001432
1433 s->cookie_tx = -EINVAL;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001434 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001435 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1436 if (chan) {
1437 s->chan_tx = chan;
1438 /* UART circular tx buffer is an aligned page. */
1439 s->tx_dma_addr = dma_map_single(chan->device->dev,
1440 port->state->xmit.buf,
1441 UART_XMIT_SIZE,
1442 DMA_TO_DEVICE);
1443 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1444 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1445 dma_release_channel(chan);
1446 s->chan_tx = NULL;
1447 } else {
1448 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1449 __func__, UART_XMIT_SIZE,
1450 port->state->xmit.buf, &s->tx_dma_addr);
1451 }
1452
1453 INIT_WORK(&s->work_tx, work_fn_tx);
1454 }
1455
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001456 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001457 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1458 if (chan) {
1459 unsigned int i;
1460 dma_addr_t dma;
1461 void *buf;
1462
1463 s->chan_rx = chan;
1464
1465 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1466 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1467 &dma, GFP_KERNEL);
1468 if (!buf) {
1469 dev_warn(port->dev,
1470 "Failed to allocate Rx dma buffer, using PIO\n");
1471 dma_release_channel(chan);
1472 s->chan_rx = NULL;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001473 return;
1474 }
1475
1476 for (i = 0; i < 2; i++) {
1477 struct scatterlist *sg = &s->sg_rx[i];
1478
1479 sg_init_table(sg, 1);
1480 s->rx_buf[i] = buf;
1481 sg_dma_address(sg) = dma;
1482 sg->length = s->buf_len_rx;
1483
1484 buf += s->buf_len_rx;
1485 dma += s->buf_len_rx;
1486 }
1487
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001488 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1489
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001490 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1491 sci_submit_rx(s);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001492 }
1493}
1494
1495static void sci_free_dma(struct uart_port *port)
1496{
1497 struct sci_port *s = to_sci_port(port);
1498
1499 if (s->chan_tx)
1500 sci_tx_dma_release(s, false);
1501 if (s->chan_rx)
1502 sci_rx_dma_release(s, false);
1503}
1504#else
1505static inline void sci_request_dma(struct uart_port *port)
1506{
1507}
1508
1509static inline void sci_free_dma(struct uart_port *port)
1510{
1511}
1512#endif
1513
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001514static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001516#ifdef CONFIG_SERIAL_SH_SCI_DMA
1517 struct uart_port *port = ptr;
1518 struct sci_port *s = to_sci_port(port);
1519
1520 if (s->chan_rx) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001521 u16 scr = serial_port_in(port, SCSCR);
1522 u16 ssr = serial_port_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001523
1524 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001525 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001526 disable_irq_nosync(irq);
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001527 scr |= SCSCR_RDRQE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001528 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +09001529 scr &= ~SCSCR_RIE;
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001530 sci_submit_rx(s);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001531 }
Paul Mundtb12bb292012-03-30 19:50:15 +09001532 serial_port_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001533 /* Clear current interrupt */
Geert Uytterhoeven54af5002015-08-21 20:02:28 +02001534 serial_port_out(port, SCxSR,
1535 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001536 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
1537 jiffies, s->rx_timeout);
1538 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001539
1540 return IRQ_HANDLED;
1541 }
1542#endif
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* I think sci_receive_chars has to be called irrespective
1545 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1546 * to be disabled?
1547 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001548 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 return IRQ_HANDLED;
1551}
1552
David Howells7d12e782006-10-05 14:55:46 +01001553static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +09001556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Stuart Menefyfd78a762009-07-29 23:01:24 +09001558 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +09001560 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return IRQ_HANDLED;
1563}
1564
David Howells7d12e782006-10-05 14:55:46 +01001565static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct uart_port *port = ptr;
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001568 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Handle errors */
1571 if (port->type == PORT_SCI) {
1572 if (sci_handle_errors(port)) {
1573 /* discard character in rx buffer */
Paul Mundtb12bb292012-03-30 19:50:15 +09001574 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001575 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +09001578 sci_handle_fifo_overrun(port);
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001579 if (!s->chan_rx)
1580 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001583 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /* Kick the transmission */
Yoshihiro Shimoda8eadb562015-08-21 20:02:56 +02001586 if (!s->chan_tx)
1587 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 return IRQ_HANDLED;
1590}
1591
David Howells7d12e782006-10-05 14:55:46 +01001592static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct uart_port *port = ptr;
1595
1596 /* Handle BREAKs */
1597 sci_handle_breaks(port);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001598 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return IRQ_HANDLED;
1601}
1602
David Howells7d12e782006-10-05 14:55:46 +01001603static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001605 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
Michael Trimarchia8884e32008-10-31 16:10:23 +09001606 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001607 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +09001608 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Paul Mundtb12bb292012-03-30 19:50:15 +09001610 ssr_status = serial_port_in(port, SCxSR);
1611 scr_status = serial_port_in(port, SCSCR);
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001612 if (s->overrun_reg == SCxSR)
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001613 orer_status = ssr_status;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001614 else {
1615 if (sci_getreg(port, s->overrun_reg)->size)
1616 orer_status = serial_port_in(port, s->overrun_reg);
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001617 }
1618
Paul Mundtf43dc232011-01-13 15:06:28 +09001619 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +09001622 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001623 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001624 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001625
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001626 /*
1627 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1628 * DR flags
1629 */
1630 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Geert Uytterhoevene0a12a22015-08-21 20:02:35 +02001631 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +09001632 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001635 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001636 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001639 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001640 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001642 /* Overrun Interrupt */
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001643 if (orer_status & s->overrun_mask) {
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001644 sci_handle_fifo_overrun(port);
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001645 ret = IRQ_HANDLED;
1646 }
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001647
Michael Trimarchia8884e32008-10-31 16:10:23 +09001648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001652 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * ports' baud rate when the peripheral clock changes.
1654 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001655static int sci_notifier(struct notifier_block *self,
1656 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Magnus Damme552de22009-01-21 15:13:42 +00001658 struct sci_port *sci_port;
1659 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Paul Mundtd535a232011-01-19 17:19:35 +09001661 sci_port = container_of(self, struct sci_port, freq_transition);
1662
Viresh Kumar0b443ea2014-03-19 11:24:58 +05301663 if (phase == CPUFREQ_POSTCHANGE) {
Paul Mundtd535a232011-01-19 17:19:35 +09001664 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +09001665
Paul Mundtd535a232011-01-19 17:19:35 +09001666 spin_lock_irqsave(&port->lock, flags);
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03001667 port->uartclk = clk_get_rate(sci_port->fclk);
Paul Mundtd535a232011-01-19 17:19:35 +09001668 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +00001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return NOTIFY_OK;
1672}
Magnus Damm501b8252009-01-21 15:14:30 +00001673
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001674static const struct sci_irq_desc {
Paul Mundt9174fc82011-06-28 15:25:36 +09001675 const char *desc;
1676 irq_handler_t handler;
1677} sci_irq_desc[] = {
1678 /*
1679 * Split out handlers, the default case.
1680 */
1681 [SCIx_ERI_IRQ] = {
1682 .desc = "rx err",
1683 .handler = sci_er_interrupt,
1684 },
1685
1686 [SCIx_RXI_IRQ] = {
1687 .desc = "rx full",
1688 .handler = sci_rx_interrupt,
1689 },
1690
1691 [SCIx_TXI_IRQ] = {
1692 .desc = "tx empty",
1693 .handler = sci_tx_interrupt,
1694 },
1695
1696 [SCIx_BRI_IRQ] = {
1697 .desc = "break",
1698 .handler = sci_br_interrupt,
1699 },
1700
1701 /*
1702 * Special muxed handler.
1703 */
1704 [SCIx_MUX_IRQ] = {
1705 .desc = "mux",
1706 .handler = sci_mpxed_interrupt,
1707 },
1708};
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static int sci_request_irq(struct sci_port *port)
1711{
Paul Mundt9174fc82011-06-28 15:25:36 +09001712 struct uart_port *up = &port->port;
1713 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Paul Mundt9174fc82011-06-28 15:25:36 +09001715 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001716 const struct sci_irq_desc *desc;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001717 int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001718
Paul Mundt9174fc82011-06-28 15:25:36 +09001719 if (SCIx_IRQ_IS_MUXED(port)) {
1720 i = SCIx_MUX_IRQ;
1721 irq = up->irq;
Paul Mundt0e8963d2012-05-18 18:21:06 +09001722 } else {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001723 irq = port->irqs[i];
Paul Mundt9174fc82011-06-28 15:25:36 +09001724
Paul Mundt0e8963d2012-05-18 18:21:06 +09001725 /*
1726 * Certain port types won't support all of the
1727 * available interrupt sources.
1728 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001729 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001730 continue;
1731 }
1732
Paul Mundt9174fc82011-06-28 15:25:36 +09001733 desc = sci_irq_desc + i;
1734 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1735 dev_name(up->dev), desc->desc);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02001736 if (!port->irqstr[j])
Paul Mundt9174fc82011-06-28 15:25:36 +09001737 goto out_nomem;
Paul Mundt762c69e2008-12-16 18:55:26 +09001738
Paul Mundt9174fc82011-06-28 15:25:36 +09001739 ret = request_irq(irq, desc->handler, up->irqflags,
1740 port->irqstr[j], port);
1741 if (unlikely(ret)) {
1742 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1743 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 }
1746
1747 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001748
1749out_noirq:
1750 while (--i >= 0)
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001751 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001752
1753out_nomem:
1754 while (--j >= 0)
1755 kfree(port->irqstr[j]);
1756
1757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760static void sci_free_irq(struct sci_port *port)
1761{
1762 int i;
1763
Paul Mundt9174fc82011-06-28 15:25:36 +09001764 /*
1765 * Intentionally in reverse order so we iterate over the muxed
1766 * IRQ first.
1767 */
1768 for (i = 0; i < SCIx_NR_IRQS; i++) {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001769 int irq = port->irqs[i];
Paul Mundt0e8963d2012-05-18 18:21:06 +09001770
1771 /*
1772 * Certain port types won't support all of the available
1773 * interrupt sources.
1774 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001775 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001776 continue;
1777
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001778 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001779 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Paul Mundt9174fc82011-06-28 15:25:36 +09001781 if (SCIx_IRQ_IS_MUXED(port)) {
1782 /* If there's only one IRQ, we're done. */
1783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 }
1786}
1787
1788static unsigned int sci_tx_empty(struct uart_port *port)
1789{
Paul Mundtb12bb292012-03-30 19:50:15 +09001790 unsigned short status = serial_port_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001791 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001792
1793 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Paul Mundtcdf7c422011-11-24 20:18:32 +09001796/*
1797 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1798 * CTS/RTS is supported in hardware by at least one port and controlled
1799 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1800 * handled via the ->init_pins() op, which is a bit of a one-way street,
1801 * lacking any ability to defer pin control -- this will later be
1802 * converted over to the GPIO framework).
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001803 *
1804 * Other modes (such as loopback) are supported generically on certain
1805 * port types, but not others. For these it's sufficient to test for the
1806 * existence of the support register and simply ignore the port type.
Paul Mundtcdf7c422011-11-24 20:18:32 +09001807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1809{
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001810 if (mctrl & TIOCM_LOOP) {
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001811 const struct plat_sci_reg *reg;
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001812
1813 /*
1814 * Standard loopback mode for SCFCR ports.
1815 */
1816 reg = sci_getreg(port, SCFCR);
1817 if (reg->size)
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001818 serial_port_out(port, SCFCR,
1819 serial_port_in(port, SCFCR) |
1820 SCFCR_LOOP);
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824static unsigned int sci_get_mctrl(struct uart_port *port)
1825{
Paul Mundtcdf7c422011-11-24 20:18:32 +09001826 /*
1827 * CTS/RTS is handled in hardware when supported, while nothing
1828 * else is wired up. Keep it simple and simply assert DSR/CAR.
1829 */
1830 return TIOCM_DSR | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833static void sci_break_ctl(struct uart_port *port, int break_state)
1834{
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001835 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001836 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001837 unsigned short scscr, scsptr;
1838
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001839 /* check wheter the port has SCSPTR */
1840 if (!reg->size) {
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001841 /*
1842 * Not supported by hardware. Most parts couple break and rx
1843 * interrupts together, with break detection always enabled.
1844 */
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001845 return;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001846 }
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001847
1848 scsptr = serial_port_in(port, SCSPTR);
1849 scscr = serial_port_in(port, SCSCR);
1850
1851 if (break_state == -1) {
1852 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1853 scscr &= ~SCSCR_TE;
1854 } else {
1855 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1856 scscr |= SCSCR_TE;
1857 }
1858
1859 serial_port_out(port, SCSPTR, scsptr);
1860 serial_port_out(port, SCSCR, scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863static int sci_startup(struct uart_port *port)
1864{
Magnus Damma5660ad2009-01-21 15:14:38 +00001865 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001866 unsigned long flags;
Paul Mundt073e84c2011-01-19 17:30:53 +09001867 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001869 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1870
Paul Mundt073e84c2011-01-19 17:30:53 +09001871 ret = sci_request_irq(s);
1872 if (unlikely(ret < 0))
1873 return ret;
1874
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001875 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001876
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001877 spin_lock_irqsave(&port->lock, flags);
Yoshinori Satod6569012005-10-14 15:59:12 -07001878 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001879 sci_start_rx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001880 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 return 0;
1883}
1884
1885static void sci_shutdown(struct uart_port *port)
1886{
Magnus Damma5660ad2009-01-21 15:14:38 +00001887 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001888 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001890 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1891
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001892 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001894 sci_stop_tx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001895 spin_unlock_irqrestore(&port->lock, flags);
Paul Mundt073e84c2011-01-19 17:30:53 +09001896
Aleksandar Mitev9ab76552015-09-18 13:08:28 +02001897#ifdef CONFIG_SERIAL_SH_SCI_DMA
1898 if (s->chan_rx) {
1899 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
1900 port->line);
1901 del_timer_sync(&s->rx_timer);
1902 }
1903#endif
1904
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001905 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 sci_free_irq(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001909/* calculate sample rate, BRR, and clock select */
1910static void sci_scbrr_calc(struct sci_port *s, unsigned int bps,
1911 unsigned long freq, int *brr, unsigned int *srr,
1912 unsigned int *cks)
Paul Mundt26c92f32009-06-24 18:23:52 +09001913{
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001914 unsigned int min_sr, max_sr, shift, sr, br, prediv, scrate, c;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001915 int err, min_err = INT_MAX;
Ulrich Hechtf303b362013-05-31 17:57:01 +02001916
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001917 if (s->sampling_rate) {
1918 min_sr = max_sr = s->sampling_rate;
1919 shift = 0;
1920 } else {
1921 /* HSCIF has a variable sample rate */
1922 min_sr = 8;
1923 max_sr = 32;
1924 shift = 1;
1925 }
1926
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001927 /*
1928 * Find the combination of sample rate and clock select with the
1929 * smallest deviation from the desired baud rate.
1930 * Prefer high sample rates to maximise the receive margin.
1931 *
1932 * M: Receive margin (%)
1933 * N: Ratio of bit rate to clock (N = sampling rate)
1934 * D: Clock duty (D = 0 to 1.0)
1935 * L: Frame length (L = 9 to 12)
1936 * F: Absolute value of clock frequency deviation
1937 *
1938 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
1939 * (|D - 0.5| / N * (1 + F))|
1940 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
1941 */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001942 for (sr = max_sr; sr >= min_sr; sr--) {
Ulrich Hechtf303b362013-05-31 17:57:01 +02001943 for (c = 0; c <= 3; c++) {
1944 /* integerized formulas from HSCIF documentation */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001945 prediv = sr * (1 << (2 * c + shift));
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01001946
1947 /*
1948 * We need to calculate:
1949 *
1950 * br = freq / (prediv * bps) clamped to [1..256]
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01001951 * err = freq / (br * prediv) - bps
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01001952 *
1953 * Watch out for overflow when calculating the desired
1954 * sampling clock rate!
1955 */
1956 if (bps > UINT_MAX / prediv)
1957 break;
1958
1959 scrate = prediv * bps;
1960 br = DIV_ROUND_CLOSEST(freq, scrate);
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01001961 br = clamp(br, 1U, 256U);
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001962
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01001963 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001964 if (abs(err) >= abs(min_err))
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001965 continue;
1966
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001967 min_err = err;
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01001968 *brr = br - 1;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09001969 *srr = sr - 1;
1970 *cks = c;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001971
1972 if (!err)
1973 goto found;
Ulrich Hechtf303b362013-05-31 17:57:01 +02001974 }
1975 }
1976
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001977found:
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01001978 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
1979 min_err, *brr, *srr + 1, *cks);
Ulrich Hechtf303b362013-05-31 17:57:01 +02001980}
1981
Magnus Damm1ba76222011-08-03 03:47:36 +00001982static void sci_reset(struct uart_port *port)
1983{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001984 const struct plat_sci_reg *reg;
Magnus Damm1ba76222011-08-03 03:47:36 +00001985 unsigned int status;
1986
1987 do {
Paul Mundtb12bb292012-03-30 19:50:15 +09001988 status = serial_port_in(port, SCxSR);
Magnus Damm1ba76222011-08-03 03:47:36 +00001989 } while (!(status & SCxSR_TEND(port)));
1990
Paul Mundtb12bb292012-03-30 19:50:15 +09001991 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
Magnus Damm1ba76222011-08-03 03:47:36 +00001992
Paul Mundt0979e0e2011-11-24 18:35:49 +09001993 reg = sci_getreg(port, SCFCR);
1994 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09001995 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Magnus Damm1ba76222011-08-03 03:47:36 +00001996}
1997
Alan Cox606d0992006-12-08 02:38:45 -08001998static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1999 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Paul Mundt00b9de92009-06-24 17:53:33 +09002001 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002002 const struct plat_sci_reg *reg;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002003 unsigned int baud, smr_val = 0, max_baud, cks = 0;
Paul Mundta2159b52008-10-02 19:09:13 +09002004 int t = -1;
Simon Hormand4759de2013-06-20 21:09:45 +09002005 unsigned int srr = 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002007 if ((termios->c_cflag & CSIZE) == CS7)
2008 smr_val |= SCSMR_CHR;
2009 if (termios->c_cflag & PARENB)
2010 smr_val |= SCSMR_PE;
2011 if (termios->c_cflag & PARODD)
2012 smr_val |= SCSMR_PE | SCSMR_ODD;
2013 if (termios->c_cflag & CSTOPB)
2014 smr_val |= SCSMR_STOP;
2015
Magnus Damm154280f2009-12-22 03:37:28 +00002016 /*
2017 * earlyprintk comes here early on with port->uartclk set to zero.
2018 * the clock framework is not up and running at this point so here
2019 * we assume that 115200 is the maximum baud rate. please note that
2020 * the baud rate is not programmed during earlyprintk - it is assumed
2021 * that the previous boot loader has enabled required clocks and
2022 * setup the baud rate generator hardware for us already.
2023 */
Geert Uytterhoevenff8b2752015-11-19 14:35:09 +01002024 if (port->uartclk)
2025 max_baud = port->uartclk / max(s->sampling_rate, 8U);
2026 else
2027 max_baud = 115200;
Magnus Damm154280f2009-12-22 03:37:28 +00002028
2029 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002030 if (likely(baud && port->uartclk))
2031 sci_scbrr_calc(s, baud, port->uartclk, &t, &srr, &cks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Paul Mundt23241d42011-06-28 13:55:31 +09002033 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00002034
Magnus Damm1ba76222011-08-03 03:47:36 +00002035 sci_reset(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002036
Geert Uytterhoeven2944a332015-08-21 20:02:29 +02002037 smr_val |= serial_port_in(port, SCSMR) & SCSMR_CKS;
Paul Mundte108b2c2006-09-27 16:32:13 +09002038
2039 uart_update_timeout(port, termios->c_cflag, baud);
2040
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09002041 dev_dbg(port->dev, "%s: SMR %x, cks %x, t %x, SCSCR %x\n",
2042 __func__, smr_val, cks, t, s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002043
Takashi Yoshii4ffc3cd2012-11-16 10:52:22 +09002044 if (t >= 0) {
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01002045 serial_port_out(port, SCSMR, (smr_val & ~SCSMR_CKS) | cks);
Paul Mundtb12bb292012-03-30 19:50:15 +09002046 serial_port_out(port, SCBRR, t);
Ulrich Hechtf303b362013-05-31 17:57:01 +02002047 reg = sci_getreg(port, HSSRR);
2048 if (reg->size)
2049 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09002051 } else
2052 serial_port_out(port, SCSMR, smr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Paul Mundtd5701642008-12-16 20:07:27 +09002054 sci_init_pins(port, termios->c_cflag);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002055
Paul Mundt73c3d532011-12-02 19:02:06 +09002056 reg = sci_getreg(port, SCFCR);
2057 if (reg->size) {
Paul Mundtb12bb292012-03-30 19:50:15 +09002058 unsigned short ctrl = serial_port_in(port, SCFCR);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002059
Paul Mundt73c3d532011-12-02 19:02:06 +09002060 if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
Paul Mundtfaf02f82011-12-02 17:44:50 +09002061 if (termios->c_cflag & CRTSCTS)
2062 ctrl |= SCFCR_MCE;
2063 else
2064 ctrl &= ~SCFCR_MCE;
Paul Mundtfaf02f82011-12-02 17:44:50 +09002065 }
Paul Mundt73c3d532011-12-02 19:02:06 +09002066
2067 /*
2068 * As we've done a sci_reset() above, ensure we don't
2069 * interfere with the FIFOs while toggling MCE. As the
2070 * reset values could still be set, simply mask them out.
2071 */
2072 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2073
Paul Mundtb12bb292012-03-30 19:50:15 +09002074 serial_port_out(port, SCFCR, ctrl);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002075 }
Paul Mundtb7a76e42006-02-01 03:06:06 -08002076
Paul Mundtb12bb292012-03-30 19:50:15 +09002077 serial_port_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002079#ifdef CONFIG_SERIAL_SH_SCI_DMA
2080 /*
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002081 * Calculate delay for 2 DMA buffers (4 FIFO).
Geert Uytterhoevenf5835c12015-08-21 20:02:38 +02002082 * See serial_core.c::uart_update_timeout().
2083 * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
2084 * function calculates 1 jiffie for the data plus 5 jiffies for the
2085 * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
2086 * buffers (4 FIFO sizes), but when performing a faster transfer, the
2087 * value obtained by this formula is too small. Therefore, if the value
2088 * is smaller than 20ms, use 20ms as the timeout value for DMA.
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002089 */
2090 if (s->chan_rx) {
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002091 unsigned int bits;
2092
2093 /* byte size and parity */
2094 switch (termios->c_cflag & CSIZE) {
2095 case CS5:
2096 bits = 7;
2097 break;
2098 case CS6:
2099 bits = 8;
2100 break;
2101 case CS7:
2102 bits = 9;
2103 break;
2104 default:
2105 bits = 10;
2106 break;
2107 }
2108
2109 if (termios->c_cflag & CSTOPB)
2110 bits++;
2111 if (termios->c_cflag & PARENB)
2112 bits++;
2113 s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
2114 (baud / 10), 10);
Joe Perches9b971cd2014-03-11 10:10:46 -07002115 dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002116 s->rx_timeout * 1000 / HZ, port->timeout);
2117 if (s->rx_timeout < msecs_to_jiffies(20))
2118 s->rx_timeout = msecs_to_jiffies(20);
2119 }
2120#endif
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002123 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00002124
Paul Mundt23241d42011-06-28 13:55:31 +09002125 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002128static void sci_pm(struct uart_port *port, unsigned int state,
2129 unsigned int oldstate)
2130{
2131 struct sci_port *sci_port = to_sci_port(port);
2132
2133 switch (state) {
Geert Uytterhoevend3dfe5d2014-03-11 11:11:20 +01002134 case UART_PM_STATE_OFF:
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002135 sci_port_disable(sci_port);
2136 break;
2137 default:
2138 sci_port_enable(sci_port);
2139 break;
2140 }
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143static const char *sci_type(struct uart_port *port)
2144{
2145 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09002146 case PORT_IRDA:
2147 return "irda";
2148 case PORT_SCI:
2149 return "sci";
2150 case PORT_SCIF:
2151 return "scif";
2152 case PORT_SCIFA:
2153 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002154 case PORT_SCIFB:
2155 return "scifb";
Ulrich Hechtf303b362013-05-31 17:57:01 +02002156 case PORT_HSCIF:
2157 return "hscif";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
Paul Mundtfa439722008-09-04 18:53:58 +09002160 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Paul Mundtf6e94952011-01-21 15:25:36 +09002163static int sci_remap_port(struct uart_port *port)
2164{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002165 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002166
2167 /*
2168 * Nothing to do if there's already an established membase.
2169 */
2170 if (port->membase)
2171 return 0;
2172
2173 if (port->flags & UPF_IOREMAP) {
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002174 port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
Paul Mundtf6e94952011-01-21 15:25:36 +09002175 if (unlikely(!port->membase)) {
2176 dev_err(port->dev, "can't remap port#%d\n", port->line);
2177 return -ENXIO;
2178 }
2179 } else {
2180 /*
2181 * For the simple (and majority of) cases where we don't
2182 * need to do any remapping, just cast the cookie
2183 * directly.
2184 */
Jingoo Han3af4e962014-02-05 09:56:37 +09002185 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
Paul Mundtf6e94952011-01-21 15:25:36 +09002186 }
2187
2188 return 0;
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191static void sci_release_port(struct uart_port *port)
2192{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002193 struct sci_port *sport = to_sci_port(port);
2194
Paul Mundte2651642011-01-20 21:24:03 +09002195 if (port->flags & UPF_IOREMAP) {
2196 iounmap(port->membase);
2197 port->membase = NULL;
2198 }
2199
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002200 release_mem_region(port->mapbase, sport->reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203static int sci_request_port(struct uart_port *port)
2204{
Paul Mundte2651642011-01-20 21:24:03 +09002205 struct resource *res;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002206 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002209 res = request_mem_region(port->mapbase, sport->reg_size,
2210 dev_name(port->dev));
2211 if (unlikely(res == NULL)) {
2212 dev_err(port->dev, "request_mem_region failed.");
Paul Mundte2651642011-01-20 21:24:03 +09002213 return -EBUSY;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Paul Mundtf6e94952011-01-21 15:25:36 +09002216 ret = sci_remap_port(port);
2217 if (unlikely(ret != 0)) {
2218 release_resource(res);
2219 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002220 }
Paul Mundte2651642011-01-20 21:24:03 +09002221
2222 return 0;
2223}
2224
2225static void sci_config_port(struct uart_port *port, int flags)
2226{
2227 if (flags & UART_CONFIG_TYPE) {
2228 struct sci_port *sport = to_sci_port(port);
2229
2230 port->type = sport->cfg->type;
2231 sci_request_port(port);
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (ser->baud_base < 2400)
2238 /* No paper tape reader for Mitch.. */
2239 return -EINVAL;
2240
2241 return 0;
2242}
2243
2244static struct uart_ops sci_uart_ops = {
2245 .tx_empty = sci_tx_empty,
2246 .set_mctrl = sci_set_mctrl,
2247 .get_mctrl = sci_get_mctrl,
2248 .start_tx = sci_start_tx,
2249 .stop_tx = sci_stop_tx,
2250 .stop_rx = sci_stop_rx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 .break_ctl = sci_break_ctl,
2252 .startup = sci_startup,
2253 .shutdown = sci_shutdown,
2254 .set_termios = sci_set_termios,
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002255 .pm = sci_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 .type = sci_type,
2257 .release_port = sci_release_port,
2258 .request_port = sci_request_port,
2259 .config_port = sci_config_port,
2260 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002261#ifdef CONFIG_CONSOLE_POLL
2262 .poll_get_char = sci_poll_get_char,
2263 .poll_put_char = sci_poll_put_char,
2264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265};
2266
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002267static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2268{
2269 /* Get the SCI functional clock. It's called "fck" on ARM. */
Geert Uytterhoevenf4de4722015-10-26 09:56:20 +01002270 sci_port->fclk = devm_clk_get(dev, "fck");
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002271 if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
2272 return -EPROBE_DEFER;
2273 if (!IS_ERR(sci_port->fclk))
2274 return 0;
2275
2276 /*
2277 * But it used to be called "sci_ick", and we need to maintain DT
2278 * backward compatibility.
2279 */
Geert Uytterhoevenf4de4722015-10-26 09:56:20 +01002280 sci_port->fclk = devm_clk_get(dev, "sci_ick");
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002281 if (PTR_ERR(sci_port->fclk) == -EPROBE_DEFER)
2282 return -EPROBE_DEFER;
2283 if (!IS_ERR(sci_port->fclk))
2284 return 0;
2285
2286 /* SH has historically named the clock "sci_fck". */
Geert Uytterhoevenf4de4722015-10-26 09:56:20 +01002287 sci_port->fclk = devm_clk_get(dev, "sci_fck");
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002288 if (!IS_ERR(sci_port->fclk))
2289 return 0;
2290
2291 /*
2292 * Not all SH platforms declare a clock lookup entry for SCI devices,
2293 * in which case we need to get the global "peripheral_clk" clock.
2294 */
Geert Uytterhoevenf4de4722015-10-26 09:56:20 +01002295 sci_port->fclk = devm_clk_get(dev, "peripheral_clk");
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002296 if (!IS_ERR(sci_port->fclk))
2297 return 0;
2298
2299 dev_err(dev, "failed to get functional clock\n");
2300 return PTR_ERR(sci_port->fclk);
2301}
2302
Bill Pemberton9671f092012-11-19 13:21:50 -05002303static int sci_init_single(struct platform_device *dev,
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002304 struct sci_port *sci_port, unsigned int index,
2305 struct plat_sci_port *p, bool early)
Paul Mundte108b2c2006-09-27 16:32:13 +09002306{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002307 struct uart_port *port = &sci_port->port;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002308 const struct resource *res;
2309 unsigned int i;
Paul Mundt3127c6b2011-06-28 13:44:37 +09002310 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002311
Paul Mundt50f09592011-12-02 20:09:48 +09002312 sci_port->cfg = p;
2313
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002314 port->ops = &sci_uart_ops;
2315 port->iotype = UPIO_MEM;
2316 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09002317
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002318 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2319 if (res == NULL)
2320 return -ENOMEM;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002321
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002322 port->mapbase = res->start;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002323 sci_port->reg_size = resource_size(res);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002324
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002325 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2326 sci_port->irqs[i] = platform_get_irq(dev, i);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002327
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002328 /* The SCI generates several interrupts. They can be muxed together or
2329 * connected to different interrupt lines. In the muxed case only one
2330 * interrupt resource is specified. In the non-muxed case three or four
2331 * interrupt resources are specified, as the BRI interrupt is optional.
2332 */
2333 if (sci_port->irqs[0] < 0)
2334 return -ENXIO;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002335
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002336 if (sci_port->irqs[1] < 0) {
2337 sci_port->irqs[1] = sci_port->irqs[0];
2338 sci_port->irqs[2] = sci_port->irqs[0];
2339 sci_port->irqs[3] = sci_port->irqs[0];
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002340 }
2341
Paul Mundt3127c6b2011-06-28 13:44:37 +09002342 if (p->regtype == SCIx_PROBE_REGTYPE) {
2343 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02002344 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09002345 return ret;
2346 }
Paul Mundt61a69762011-06-14 12:40:19 +09002347
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002348 switch (p->type) {
2349 case PORT_SCIFB:
2350 port->fifosize = 256;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002351 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002352 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002353 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002354 break;
2355 case PORT_HSCIF:
2356 port->fifosize = 128;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002357 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002358 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002359 sci_port->sampling_rate = 0;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002360 break;
2361 case PORT_SCIFA:
2362 port->fifosize = 64;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002363 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002364 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002365 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002366 break;
2367 case PORT_SCIF:
2368 port->fifosize = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002369 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002370 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002371 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002372 sci_port->sampling_rate = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002373 } else {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002374 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002375 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002376 sci_port->sampling_rate = 32;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002377 }
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002378 break;
2379 default:
2380 port->fifosize = 1;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002381 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002382 sci_port->overrun_mask = SCI_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002383 sci_port->sampling_rate = 32;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002384 break;
2385 }
2386
Laurent Pinchart878fbb912013-12-06 10:59:51 +01002387 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
2388 * match the SoC datasheet, this should be investigated. Let platform
2389 * data override the sampling rate for now.
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002390 */
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002391 if (p->sampling_rate)
2392 sci_port->sampling_rate = p->sampling_rate;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002393
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002394 if (!early) {
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002395 ret = sci_init_clocks(sci_port, &dev->dev);
2396 if (ret < 0)
2397 return ret;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002398
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002399 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002400
2401 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002402 }
Paul Mundte108b2c2006-09-27 16:32:13 +09002403
Magnus Damm7ed7e072009-01-21 15:14:14 +00002404 sci_port->break_timer.data = (unsigned long)sci_port;
2405 sci_port->break_timer.function = sci_break_timer;
2406 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09002407
Paul Mundtdebf9502011-06-08 18:19:37 +09002408 /*
2409 * Establish some sensible defaults for the error detection.
2410 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002411 if (p->type == PORT_SCI) {
2412 sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
2413 sci_port->error_clear = SCI_ERROR_CLEAR;
2414 } else {
2415 sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
2416 sci_port->error_clear = SCIF_ERROR_CLEAR;
2417 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002418
2419 /*
Laurent Pinchart3ae988d2013-12-06 10:59:17 +01002420 * Make the error mask inclusive of overrun detection, if
2421 * supported.
2422 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002423 if (sci_port->overrun_reg == SCxSR) {
Geert Uytterhoevenafd66db2015-04-30 18:21:33 +02002424 sci_port->error_mask |= sci_port->overrun_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002425 sci_port->error_clear &= ~sci_port->overrun_mask;
2426 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002427
Paul Mundtce6738b2011-01-19 15:24:40 +09002428 port->type = p->type;
Laurent Pinchartb6e4a3f2013-12-06 10:59:14 +01002429 port->flags = UPF_FIXED_PORT | p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09002430 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09002431
2432 /*
Paul Mundt61a69762011-06-14 12:40:19 +09002433 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09002434 * for the multi-IRQ ports, which is where we are primarily
2435 * concerned with the shutdown path synchronization.
2436 *
2437 * For the muxed case there's nothing more to do.
2438 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002439 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08002440 port->irqflags = 0;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09002441
Paul Mundt61a69762011-06-14 12:40:19 +09002442 port->serial_in = sci_serial_in;
2443 port->serial_out = sci_serial_out;
2444
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02002445 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2446 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2447 p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002448
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002449 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002450}
2451
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002452static void sci_cleanup_single(struct sci_port *port)
2453{
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002454 pm_runtime_disable(port->port.dev);
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002458static void serial_console_putchar(struct uart_port *port, int ch)
2459{
2460 sci_poll_put_char(port, ch);
2461}
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463/*
2464 * Print a string to the serial port trying not to disturb
2465 * any possible real use of the port...
2466 */
2467static void serial_console_write(struct console *co, const char *s,
2468 unsigned count)
2469{
Paul Mundt906b17d2011-01-21 16:19:53 +09002470 struct sci_port *sci_port = &sci_ports[co->index];
2471 struct uart_port *port = &sci_port->port;
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002472 unsigned short bits, ctrl, ctrl_temp;
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002473 unsigned long flags;
2474 int locked = 1;
2475
2476 local_irq_save(flags);
2477 if (port->sysrq)
2478 locked = 0;
2479 else if (oops_in_progress)
2480 locked = spin_trylock(&port->lock);
2481 else
2482 spin_lock(&port->lock);
2483
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002484 /* first save SCSCR then disable interrupts, keep clock source */
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002485 ctrl = serial_port_in(port, SCSCR);
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002486 ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
2487 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
2488 serial_port_out(port, SCSCR, ctrl_temp);
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002489
Magnus Damm501b8252009-01-21 15:14:30 +00002490 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002491
2492 /* wait until fifo is empty and last bit has been transmitted */
2493 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
Paul Mundtb12bb292012-03-30 19:50:15 +09002494 while ((serial_port_in(port, SCxSR) & bits) != bits)
Magnus Damm973e5d52009-02-24 15:57:12 +09002495 cpu_relax();
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002496
2497 /* restore the SCSCR */
2498 serial_port_out(port, SCSCR, ctrl);
2499
2500 if (locked)
2501 spin_unlock(&port->lock);
2502 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Bill Pemberton9671f092012-11-19 13:21:50 -05002505static int serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002507 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct uart_port *port;
2509 int baud = 115200;
2510 int bits = 8;
2511 int parity = 'n';
2512 int flow = 'n';
2513 int ret;
2514
Paul Mundte108b2c2006-09-27 16:32:13 +09002515 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002516 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002517 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002518 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002519 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002520
Paul Mundt906b17d2011-01-21 16:19:53 +09002521 sci_port = &sci_ports[co->index];
2522 port = &sci_port->port;
2523
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002524 /*
2525 * Refuse to handle uninitialized ports.
2526 */
2527 if (!port->ops)
2528 return -ENODEV;
2529
Paul Mundtf6e94952011-01-21 15:25:36 +09002530 ret = sci_remap_port(port);
2531 if (unlikely(ret != 0))
2532 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (options)
2535 uart_parse_options(options, &baud, &parity, &bits, &flow);
2536
Paul Mundtab7cfb52011-06-01 14:47:42 +09002537 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540static struct console serial_console = {
2541 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002542 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 .write = serial_console_write,
2544 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002545 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002547 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548};
2549
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002550static struct console early_serial_console = {
2551 .name = "early_ttySC",
2552 .write = serial_console_write,
2553 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002554 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002555};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002556
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002557static char early_serial_buf[32];
2558
Bill Pemberton9671f092012-11-19 13:21:50 -05002559static int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002560{
Jingoo Han574de552013-07-30 17:06:57 +09002561 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002562
2563 if (early_serial_console.data)
2564 return -EEXIST;
2565
2566 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002567
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002568 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002569
2570 serial_console_setup(&early_serial_console, early_serial_buf);
2571
2572 if (!strstr(early_serial_buf, "keep"))
2573 early_serial_console.flags |= CON_BOOT;
2574
2575 register_console(&early_serial_console);
2576 return 0;
2577}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002578
2579#define SCI_CONSOLE (&serial_console)
2580
Paul Mundtecdf8a42011-01-21 00:05:48 +09002581#else
Bill Pemberton9671f092012-11-19 13:21:50 -05002582static inline int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002583{
2584 return -EINVAL;
2585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002587#define SCI_CONSOLE NULL
2588
2589#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002591static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593static struct uart_driver sci_uart_driver = {
2594 .owner = THIS_MODULE,
2595 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 .dev_name = "ttySC",
2597 .major = SCI_MAJOR,
2598 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002599 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 .cons = SCI_CONSOLE,
2601};
2602
Paul Mundt54507f62009-05-08 23:48:33 +09002603static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002604{
Paul Mundtd535a232011-01-19 17:19:35 +09002605 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002606
Paul Mundtd535a232011-01-19 17:19:35 +09002607 cpufreq_unregister_notifier(&port->freq_transition,
2608 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002609
Paul Mundtd535a232011-01-19 17:19:35 +09002610 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002611
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002612 sci_cleanup_single(port);
Paul Mundtd535a232011-01-19 17:19:35 +09002613
Magnus Damme552de22009-01-21 15:13:42 +00002614 return 0;
2615}
2616
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002617
2618#define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
2619#define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
2620#define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002621
2622static const struct of_device_id of_sci_match[] = {
2623 {
2624 .compatible = "renesas,scif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002625 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002626 }, {
2627 .compatible = "renesas,scifa",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002628 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002629 }, {
2630 .compatible = "renesas,scifb",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002631 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002632 }, {
2633 .compatible = "renesas,hscif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002634 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002635 }, {
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002636 .compatible = "renesas,sci",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002637 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002638 }, {
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002639 /* Terminator */
2640 },
2641};
2642MODULE_DEVICE_TABLE(of, of_sci_match);
2643
2644static struct plat_sci_port *
2645sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
2646{
2647 struct device_node *np = pdev->dev.of_node;
2648 const struct of_device_id *match;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002649 struct plat_sci_port *p;
2650 int id;
2651
2652 if (!IS_ENABLED(CONFIG_OF) || !np)
2653 return NULL;
2654
Geert Uytterhoeven495bb472015-12-10 16:02:17 +01002655 match = of_match_node(of_sci_match, np);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002656 if (!match)
2657 return NULL;
2658
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002659 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02002660 if (!p)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002661 return NULL;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002662
Geert Uytterhoeven2095fc72015-11-12 13:39:49 +01002663 /* Get the line number from the aliases node. */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002664 id = of_alias_get_id(np, "serial");
2665 if (id < 0) {
2666 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
2667 return NULL;
2668 }
2669
2670 *dev_id = id;
2671
2672 p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002673 p->type = SCI_OF_TYPE(match->data);
2674 p->regtype = SCI_OF_REGTYPE(match->data);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002675 p->scscr = SCSCR_RE | SCSCR_TE;
2676
2677 return p;
2678}
2679
Bill Pemberton9671f092012-11-19 13:21:50 -05002680static int sci_probe_single(struct platform_device *dev,
Magnus Damm0ee70712009-01-21 15:13:50 +00002681 unsigned int index,
2682 struct plat_sci_port *p,
2683 struct sci_port *sciport)
2684{
Magnus Damm0ee70712009-01-21 15:13:50 +00002685 int ret;
2686
2687 /* Sanity check */
2688 if (unlikely(index >= SCI_NPORTS)) {
Joe Perches9b971cd2014-03-11 10:10:46 -07002689 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
Magnus Damm0ee70712009-01-21 15:13:50 +00002690 index+1, SCI_NPORTS);
Joe Perches9b971cd2014-03-11 10:10:46 -07002691 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
Laurent Pinchartb6c5ef62012-06-13 00:28:24 +02002692 return -EINVAL;
Magnus Damm0ee70712009-01-21 15:13:50 +00002693 }
2694
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002695 ret = sci_init_single(dev, sciport, index, p, false);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002696 if (ret)
2697 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002698
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002699 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
2700 if (ret) {
2701 sci_cleanup_single(sciport);
2702 return ret;
2703 }
2704
2705 return 0;
Magnus Damm0ee70712009-01-21 15:13:50 +00002706}
2707
Bill Pemberton9671f092012-11-19 13:21:50 -05002708static int sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002710 struct plat_sci_port *p;
2711 struct sci_port *sp;
2712 unsigned int dev_id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002713 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002714
Paul Mundtecdf8a42011-01-21 00:05:48 +09002715 /*
2716 * If we've come here via earlyprintk initialization, head off to
2717 * the special early probe. We don't have sufficient device state
2718 * to make it beyond this yet.
2719 */
2720 if (is_early_platform_device(dev))
2721 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002722
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002723 if (dev->dev.of_node) {
2724 p = sci_parse_dt(dev, &dev_id);
2725 if (p == NULL)
2726 return -EINVAL;
2727 } else {
2728 p = dev->dev.platform_data;
2729 if (p == NULL) {
2730 dev_err(&dev->dev, "no platform data supplied\n");
2731 return -EINVAL;
2732 }
2733
2734 dev_id = dev->id;
2735 }
2736
2737 sp = &sci_ports[dev_id];
Paul Mundtd535a232011-01-19 17:19:35 +09002738 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002739
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002740 ret = sci_probe_single(dev, dev_id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002741 if (ret)
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002742 return ret;
Magnus Damme552de22009-01-21 15:13:42 +00002743
Paul Mundtd535a232011-01-19 17:19:35 +09002744 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Paul Mundtd535a232011-01-19 17:19:35 +09002746 ret = cpufreq_register_notifier(&sp->freq_transition,
2747 CPUFREQ_TRANSITION_NOTIFIER);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002748 if (unlikely(ret < 0)) {
Geert Uytterhoevenbf13c9a2014-02-28 14:21:33 +01002749 uart_remove_one_port(&sci_uart_driver, &sp->port);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002750 sci_cleanup_single(sp);
2751 return ret;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754#ifdef CONFIG_SH_STANDARD_BIOS
2755 sh_bios_gdb_detach();
2756#endif
2757
Paul Mundte108b2c2006-09-27 16:32:13 +09002758 return 0;
2759}
2760
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002761static __maybe_unused int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002762{
Paul Mundtd535a232011-01-19 17:19:35 +09002763 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002764
Paul Mundtd535a232011-01-19 17:19:35 +09002765 if (sport)
2766 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002767
2768 return 0;
2769}
2770
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002771static __maybe_unused int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002772{
Paul Mundtd535a232011-01-19 17:19:35 +09002773 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002774
Paul Mundtd535a232011-01-19 17:19:35 +09002775 if (sport)
2776 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002777
2778 return 0;
2779}
2780
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002781static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
Paul Mundt6daa79b2009-06-15 07:07:38 +09002782
Paul Mundte108b2c2006-09-27 16:32:13 +09002783static struct platform_driver sci_driver = {
2784 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002785 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002786 .driver = {
2787 .name = "sh-sci",
Paul Mundt6daa79b2009-06-15 07:07:38 +09002788 .pm = &sci_dev_pm_ops,
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002789 .of_match_table = of_match_ptr(of_sci_match),
Paul Mundte108b2c2006-09-27 16:32:13 +09002790 },
2791};
2792
2793static int __init sci_init(void)
2794{
2795 int ret;
2796
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002797 pr_info("%s\n", banner);
Paul Mundte108b2c2006-09-27 16:32:13 +09002798
Paul Mundte108b2c2006-09-27 16:32:13 +09002799 ret = uart_register_driver(&sci_uart_driver);
2800 if (likely(ret == 0)) {
2801 ret = platform_driver_register(&sci_driver);
2802 if (unlikely(ret))
2803 uart_unregister_driver(&sci_uart_driver);
2804 }
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return ret;
2807}
2808
2809static void __exit sci_exit(void)
2810{
Paul Mundte108b2c2006-09-27 16:32:13 +09002811 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 uart_unregister_driver(&sci_uart_driver);
2813}
2814
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002815#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2816early_platform_init_buffer("earlyprintk", &sci_driver,
2817 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2818#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819module_init(sci_init);
2820module_exit(sci_exit);
2821
Paul Mundte108b2c2006-09-27 16:32:13 +09002822MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002823MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09002824MODULE_AUTHOR("Paul Mundt");
Ulrich Hechtf303b362013-05-31 17:57:01 +02002825MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");