blob: 4ff5d0cf812688ff0176ccd8b26604fba395aba3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
Paul Mundtf43dc232011-01-13 15:06:28 +09004 * Copyright (C) 2002 - 2011 Paul Mundt
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01005 * Copyright (C) 2015 Glider bvba
Markus Brunner3ea6bc32007-08-20 08:59:33 +09006 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * based off of the old drivers/char/sh-sci.c by:
9 *
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090015 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
20 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090021#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22#define SUPPORT_SYSRQ
23#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#undef DEBUG
26
Paul Mundt85f094e2008-04-25 16:04:20 +090027#include <linux/clk.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010028#include <linux/console.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090029#include <linux/ctype.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010030#include <linux/cpufreq.h>
31#include <linux/delay.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090032#include <linux/dmaengine.h>
Magnus Damm5beabc72011-08-02 09:42:54 +000033#include <linux/dma-mapping.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010034#include <linux/err.h>
35#include <linux/errno.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010036#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/ioport.h>
39#include <linux/major.h>
40#include <linux/module.h>
41#include <linux/mm.h>
42#include <linux/notifier.h>
Bastian Hecht20bdcab2013-12-06 10:59:54 +010043#include <linux/of.h>
Laurent Pinchart8fb96312013-12-06 10:59:10 +010044#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/scatterlist.h>
47#include <linux/serial.h>
48#include <linux/serial_sci.h>
49#include <linux/sh_dma.h>
50#include <linux/slab.h>
51#include <linux/string.h>
52#include <linux/sysrq.h>
53#include <linux/timer.h>
54#include <linux/tty.h>
55#include <linux/tty_flip.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090056
57#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090058#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "sh-sci.h"
62
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +010063/* Offsets into the sci_port->irqs array */
64enum {
65 SCIx_ERI_IRQ,
66 SCIx_RXI_IRQ,
67 SCIx_TXI_IRQ,
68 SCIx_BRI_IRQ,
69 SCIx_NR_IRQS,
70
71 SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
72};
73
74#define SCIx_IRQ_IS_MUXED(port) \
75 ((port)->irqs[SCIx_ERI_IRQ] == \
76 (port)->irqs[SCIx_RXI_IRQ]) || \
77 ((port)->irqs[SCIx_ERI_IRQ] && \
78 ((port)->irqs[SCIx_RXI_IRQ] < 0))
79
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +010080enum SCI_CLKS {
81 SCI_FCK, /* Functional Clock */
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +010082 SCI_SCK, /* Optional External Clock */
Geert Uytterhoeven1270f862015-11-18 11:25:53 +010083 SCI_BRG_INT, /* Optional BRG Internal Clock Source */
84 SCI_SCIF_CLK, /* Optional BRG External Clock Source */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +010085 SCI_NUM_CLKS
86};
87
Paul Mundte108b2c2006-09-27 16:32:13 +090088struct sci_port {
89 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Paul Mundtce6738b2011-01-19 15:24:40 +090091 /* Platform configuration */
92 struct plat_sci_port *cfg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +020093 unsigned int overrun_reg;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +020094 unsigned int overrun_mask;
Laurent Pinchart3ae988d2013-12-06 10:59:17 +010095 unsigned int error_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +020096 unsigned int error_clear;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +010097 unsigned int sampling_rate;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +090098 resource_size_t reg_size;
Paul Mundte108b2c2006-09-27 16:32:13 +090099
Paul Mundte108b2c2006-09-27 16:32:13 +0900100 /* Break timer */
101 struct timer_list break_timer;
102 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900103
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100104 /* Clocks */
105 struct clk *clks[SCI_NUM_CLKS];
106 unsigned long clk_rates[SCI_NUM_CLKS];
Paul Mundtedad1f22009-11-25 16:23:35 +0900107
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +0100108 int irqs[SCIx_NR_IRQS];
Paul Mundt9174fc82011-06-28 15:25:36 +0900109 char *irqstr[SCIx_NR_IRQS];
110
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900111 struct dma_chan *chan_tx;
112 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +0900113
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900114#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900115 dma_cookie_t cookie_tx;
116 dma_cookie_t cookie_rx[2];
117 dma_cookie_t active_rx;
Geert Uytterhoeven79904422015-08-21 20:02:42 +0200118 dma_addr_t tx_dma_addr;
119 unsigned int tx_dma_len;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900120 struct scatterlist sg_rx[2];
Yoshihiro Shimoda7b39d902015-08-21 20:02:54 +0200121 void *rx_buf[2];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900122 size_t buf_len_rx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900123 struct work_struct work_tx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900124 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000125 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900126#endif
Magnus Damme552de22009-01-21 15:13:42 +0000127
Paul Mundtd535a232011-01-19 17:19:35 +0900128 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900129};
130
Paul Mundte108b2c2006-09-27 16:32:13 +0900131#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
132
133static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct uart_driver sci_uart_driver;
135
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900136static inline struct sci_port *
137to_sci_port(struct uart_port *uart)
138{
139 return container_of(uart, struct sci_port, port);
140}
141
Paul Mundt61a69762011-06-14 12:40:19 +0900142struct plat_sci_reg {
143 u8 offset, size;
144};
145
146/* Helper for invalidating specific entries of an inherited map. */
147#define sci_reg_invalid { .offset = 0, .size = 0 }
148
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200149static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
Paul Mundt61a69762011-06-14 12:40:19 +0900150 [SCIx_PROBE_REGTYPE] = {
151 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
152 },
153
154 /*
155 * Common SCI definitions, dependent on the port's regshift
156 * value.
157 */
158 [SCIx_SCI_REGTYPE] = {
159 [SCSMR] = { 0x00, 8 },
160 [SCBRR] = { 0x01, 8 },
161 [SCSCR] = { 0x02, 8 },
162 [SCxTDR] = { 0x03, 8 },
163 [SCxSR] = { 0x04, 8 },
164 [SCxRDR] = { 0x05, 8 },
165 [SCFCR] = sci_reg_invalid,
166 [SCFDR] = sci_reg_invalid,
167 [SCTFDR] = sci_reg_invalid,
168 [SCRFDR] = sci_reg_invalid,
169 [SCSPTR] = sci_reg_invalid,
170 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200171 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200172 [SCPCR] = sci_reg_invalid,
173 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100174 [SCDL] = sci_reg_invalid,
175 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900176 },
177
178 /*
179 * Common definitions for legacy IrDA ports, dependent on
180 * regshift value.
181 */
182 [SCIx_IRDA_REGTYPE] = {
183 [SCSMR] = { 0x00, 8 },
184 [SCBRR] = { 0x01, 8 },
185 [SCSCR] = { 0x02, 8 },
186 [SCxTDR] = { 0x03, 8 },
187 [SCxSR] = { 0x04, 8 },
188 [SCxRDR] = { 0x05, 8 },
189 [SCFCR] = { 0x06, 8 },
190 [SCFDR] = { 0x07, 16 },
191 [SCTFDR] = sci_reg_invalid,
192 [SCRFDR] = sci_reg_invalid,
193 [SCSPTR] = sci_reg_invalid,
194 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200195 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200196 [SCPCR] = sci_reg_invalid,
197 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100198 [SCDL] = sci_reg_invalid,
199 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900200 },
201
202 /*
203 * Common SCIFA definitions.
204 */
205 [SCIx_SCIFA_REGTYPE] = {
206 [SCSMR] = { 0x00, 16 },
207 [SCBRR] = { 0x04, 8 },
208 [SCSCR] = { 0x08, 16 },
209 [SCxTDR] = { 0x20, 8 },
210 [SCxSR] = { 0x14, 16 },
211 [SCxRDR] = { 0x24, 8 },
212 [SCFCR] = { 0x18, 16 },
213 [SCFDR] = { 0x1c, 16 },
214 [SCTFDR] = sci_reg_invalid,
215 [SCRFDR] = sci_reg_invalid,
216 [SCSPTR] = sci_reg_invalid,
217 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200218 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200219 [SCPCR] = { 0x30, 16 },
220 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100221 [SCDL] = sci_reg_invalid,
222 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900223 },
224
225 /*
226 * Common SCIFB definitions.
227 */
228 [SCIx_SCIFB_REGTYPE] = {
229 [SCSMR] = { 0x00, 16 },
230 [SCBRR] = { 0x04, 8 },
231 [SCSCR] = { 0x08, 16 },
232 [SCxTDR] = { 0x40, 8 },
233 [SCxSR] = { 0x14, 16 },
234 [SCxRDR] = { 0x60, 8 },
235 [SCFCR] = { 0x18, 16 },
Takashi Yoshii8c66d6d2012-11-16 10:53:31 +0900236 [SCFDR] = sci_reg_invalid,
237 [SCTFDR] = { 0x38, 16 },
238 [SCRFDR] = { 0x3c, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900239 [SCSPTR] = sci_reg_invalid,
240 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200241 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200242 [SCPCR] = { 0x30, 16 },
243 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100244 [SCDL] = sci_reg_invalid,
245 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900246 },
247
248 /*
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100249 * Common SH-2(A) SCIF definitions for ports with FIFO data
250 * count registers.
251 */
252 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
253 [SCSMR] = { 0x00, 16 },
254 [SCBRR] = { 0x04, 8 },
255 [SCSCR] = { 0x08, 16 },
256 [SCxTDR] = { 0x0c, 8 },
257 [SCxSR] = { 0x10, 16 },
258 [SCxRDR] = { 0x14, 8 },
259 [SCFCR] = { 0x18, 16 },
260 [SCFDR] = { 0x1c, 16 },
261 [SCTFDR] = sci_reg_invalid,
262 [SCRFDR] = sci_reg_invalid,
263 [SCSPTR] = { 0x20, 16 },
264 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200265 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200266 [SCPCR] = sci_reg_invalid,
267 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100268 [SCDL] = sci_reg_invalid,
269 [SCCKS] = sci_reg_invalid,
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100270 },
271
272 /*
Paul Mundt61a69762011-06-14 12:40:19 +0900273 * Common SH-3 SCIF definitions.
274 */
275 [SCIx_SH3_SCIF_REGTYPE] = {
276 [SCSMR] = { 0x00, 8 },
277 [SCBRR] = { 0x02, 8 },
278 [SCSCR] = { 0x04, 8 },
279 [SCxTDR] = { 0x06, 8 },
280 [SCxSR] = { 0x08, 16 },
281 [SCxRDR] = { 0x0a, 8 },
282 [SCFCR] = { 0x0c, 8 },
283 [SCFDR] = { 0x0e, 16 },
284 [SCTFDR] = sci_reg_invalid,
285 [SCRFDR] = sci_reg_invalid,
286 [SCSPTR] = sci_reg_invalid,
287 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200288 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200289 [SCPCR] = sci_reg_invalid,
290 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100291 [SCDL] = sci_reg_invalid,
292 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900293 },
294
295 /*
296 * Common SH-4(A) SCIF(B) definitions.
297 */
298 [SCIx_SH4_SCIF_REGTYPE] = {
299 [SCSMR] = { 0x00, 16 },
300 [SCBRR] = { 0x04, 8 },
301 [SCSCR] = { 0x08, 16 },
302 [SCxTDR] = { 0x0c, 8 },
303 [SCxSR] = { 0x10, 16 },
304 [SCxRDR] = { 0x14, 8 },
305 [SCFCR] = { 0x18, 16 },
306 [SCFDR] = { 0x1c, 16 },
307 [SCTFDR] = sci_reg_invalid,
308 [SCRFDR] = sci_reg_invalid,
309 [SCSPTR] = { 0x20, 16 },
310 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200311 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200312 [SCPCR] = sci_reg_invalid,
313 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100314 [SCDL] = sci_reg_invalid,
315 [SCCKS] = sci_reg_invalid,
316 },
317
318 /*
319 * Common SCIF definitions for ports with a Baud Rate Generator for
320 * External Clock (BRG).
321 */
322 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
323 [SCSMR] = { 0x00, 16 },
324 [SCBRR] = { 0x04, 8 },
325 [SCSCR] = { 0x08, 16 },
326 [SCxTDR] = { 0x0c, 8 },
327 [SCxSR] = { 0x10, 16 },
328 [SCxRDR] = { 0x14, 8 },
329 [SCFCR] = { 0x18, 16 },
330 [SCFDR] = { 0x1c, 16 },
331 [SCTFDR] = sci_reg_invalid,
332 [SCRFDR] = sci_reg_invalid,
333 [SCSPTR] = { 0x20, 16 },
334 [SCLSR] = { 0x24, 16 },
335 [HSSRR] = sci_reg_invalid,
336 [SCPCR] = sci_reg_invalid,
337 [SCPDR] = sci_reg_invalid,
338 [SCDL] = { 0x30, 16 },
339 [SCCKS] = { 0x34, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200340 },
341
342 /*
343 * Common HSCIF definitions.
344 */
345 [SCIx_HSCIF_REGTYPE] = {
346 [SCSMR] = { 0x00, 16 },
347 [SCBRR] = { 0x04, 8 },
348 [SCSCR] = { 0x08, 16 },
349 [SCxTDR] = { 0x0c, 8 },
350 [SCxSR] = { 0x10, 16 },
351 [SCxRDR] = { 0x14, 8 },
352 [SCFCR] = { 0x18, 16 },
353 [SCFDR] = { 0x1c, 16 },
354 [SCTFDR] = sci_reg_invalid,
355 [SCRFDR] = sci_reg_invalid,
356 [SCSPTR] = { 0x20, 16 },
357 [SCLSR] = { 0x24, 16 },
358 [HSSRR] = { 0x40, 16 },
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200359 [SCPCR] = sci_reg_invalid,
360 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100361 [SCDL] = { 0x30, 16 },
362 [SCCKS] = { 0x34, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900363 },
364
365 /*
366 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
367 * register.
368 */
369 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
370 [SCSMR] = { 0x00, 16 },
371 [SCBRR] = { 0x04, 8 },
372 [SCSCR] = { 0x08, 16 },
373 [SCxTDR] = { 0x0c, 8 },
374 [SCxSR] = { 0x10, 16 },
375 [SCxRDR] = { 0x14, 8 },
376 [SCFCR] = { 0x18, 16 },
377 [SCFDR] = { 0x1c, 16 },
378 [SCTFDR] = sci_reg_invalid,
379 [SCRFDR] = sci_reg_invalid,
380 [SCSPTR] = sci_reg_invalid,
381 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200382 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200383 [SCPCR] = sci_reg_invalid,
384 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100385 [SCDL] = sci_reg_invalid,
386 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900387 },
388
389 /*
390 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
391 * count registers.
392 */
393 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
394 [SCSMR] = { 0x00, 16 },
395 [SCBRR] = { 0x04, 8 },
396 [SCSCR] = { 0x08, 16 },
397 [SCxTDR] = { 0x0c, 8 },
398 [SCxSR] = { 0x10, 16 },
399 [SCxRDR] = { 0x14, 8 },
400 [SCFCR] = { 0x18, 16 },
401 [SCFDR] = { 0x1c, 16 },
402 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
403 [SCRFDR] = { 0x20, 16 },
404 [SCSPTR] = { 0x24, 16 },
405 [SCLSR] = { 0x28, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200406 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200407 [SCPCR] = sci_reg_invalid,
408 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100409 [SCDL] = sci_reg_invalid,
410 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900411 },
412
413 /*
414 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
415 * registers.
416 */
417 [SCIx_SH7705_SCIF_REGTYPE] = {
418 [SCSMR] = { 0x00, 16 },
419 [SCBRR] = { 0x04, 8 },
420 [SCSCR] = { 0x08, 16 },
421 [SCxTDR] = { 0x20, 8 },
422 [SCxSR] = { 0x14, 16 },
423 [SCxRDR] = { 0x24, 8 },
424 [SCFCR] = { 0x18, 16 },
425 [SCFDR] = { 0x1c, 16 },
426 [SCTFDR] = sci_reg_invalid,
427 [SCRFDR] = sci_reg_invalid,
428 [SCSPTR] = sci_reg_invalid,
429 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200430 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200431 [SCPCR] = sci_reg_invalid,
432 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100433 [SCDL] = sci_reg_invalid,
434 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900435 },
436};
437
Paul Mundt72b294c2011-06-14 17:38:19 +0900438#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
439
Paul Mundt61a69762011-06-14 12:40:19 +0900440/*
441 * The "offset" here is rather misleading, in that it refers to an enum
442 * value relative to the port mapping rather than the fixed offset
443 * itself, which needs to be manually retrieved from the platform's
444 * register map for the given port.
445 */
446static unsigned int sci_serial_in(struct uart_port *p, int offset)
447{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200448 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900449
450 if (reg->size == 8)
451 return ioread8(p->membase + (reg->offset << p->regshift));
452 else if (reg->size == 16)
453 return ioread16(p->membase + (reg->offset << p->regshift));
454 else
455 WARN(1, "Invalid register access\n");
456
457 return 0;
458}
459
460static void sci_serial_out(struct uart_port *p, int offset, int value)
461{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200462 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900463
464 if (reg->size == 8)
465 iowrite8(value, p->membase + (reg->offset << p->regshift));
466 else if (reg->size == 16)
467 iowrite16(value, p->membase + (reg->offset << p->regshift));
468 else
469 WARN(1, "Invalid register access\n");
470}
471
Paul Mundt61a69762011-06-14 12:40:19 +0900472static int sci_probe_regmap(struct plat_sci_port *cfg)
473{
474 switch (cfg->type) {
475 case PORT_SCI:
476 cfg->regtype = SCIx_SCI_REGTYPE;
477 break;
478 case PORT_IRDA:
479 cfg->regtype = SCIx_IRDA_REGTYPE;
480 break;
481 case PORT_SCIFA:
482 cfg->regtype = SCIx_SCIFA_REGTYPE;
483 break;
484 case PORT_SCIFB:
485 cfg->regtype = SCIx_SCIFB_REGTYPE;
486 break;
487 case PORT_SCIF:
488 /*
489 * The SH-4 is a bit of a misnomer here, although that's
490 * where this particular port layout originated. This
491 * configuration (or some slight variation thereof)
492 * remains the dominant model for all SCIFs.
493 */
494 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
495 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +0200496 case PORT_HSCIF:
497 cfg->regtype = SCIx_HSCIF_REGTYPE;
498 break;
Paul Mundt61a69762011-06-14 12:40:19 +0900499 default:
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +0100500 pr_err("Can't probe register map for given port\n");
Paul Mundt61a69762011-06-14 12:40:19 +0900501 return -EINVAL;
502 }
503
504 return 0;
505}
506
Paul Mundt23241d42011-06-28 13:55:31 +0900507static void sci_port_enable(struct sci_port *sci_port)
508{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100509 unsigned int i;
510
Paul Mundt23241d42011-06-28 13:55:31 +0900511 if (!sci_port->port.dev)
512 return;
513
514 pm_runtime_get_sync(sci_port->port.dev);
515
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100516 for (i = 0; i < SCI_NUM_CLKS; i++) {
517 clk_prepare_enable(sci_port->clks[i]);
518 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
519 }
520 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
Paul Mundt23241d42011-06-28 13:55:31 +0900521}
522
523static void sci_port_disable(struct sci_port *sci_port)
524{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100525 unsigned int i;
526
Paul Mundt23241d42011-06-28 13:55:31 +0900527 if (!sci_port->port.dev)
528 return;
529
Laurent Pinchartcaec7032013-11-28 18:11:45 +0100530 /* Cancel the break timer to ensure that the timer handler will not try
531 * to access the hardware with clocks and power disabled. Reset the
532 * break flag to make the break debouncing state machine ready for the
533 * next break.
534 */
535 del_timer_sync(&sci_port->break_timer);
536 sci_port->break_flag = 0;
537
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100538 for (i = SCI_NUM_CLKS; i-- > 0; )
539 clk_disable_unprepare(sci_port->clks[i]);
Paul Mundt23241d42011-06-28 13:55:31 +0900540
541 pm_runtime_put_sync(sci_port->port.dev);
542}
543
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +0200544static inline unsigned long port_rx_irq_mask(struct uart_port *port)
545{
546 /*
547 * Not all ports (such as SCIFA) will support REIE. Rather than
548 * special-casing the port type, we check the port initialization
549 * IRQ enable mask to see whether the IRQ is desired at all. If
550 * it's unset, it's logically inferred that there's no point in
551 * testing for it.
552 */
553 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
554}
555
556static void sci_start_tx(struct uart_port *port)
557{
558 struct sci_port *s = to_sci_port(port);
559 unsigned short ctrl;
560
561#ifdef CONFIG_SERIAL_SH_SCI_DMA
562 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
563 u16 new, scr = serial_port_in(port, SCSCR);
564 if (s->chan_tx)
565 new = scr | SCSCR_TDRQE;
566 else
567 new = scr & ~SCSCR_TDRQE;
568 if (new != scr)
569 serial_port_out(port, SCSCR, new);
570 }
571
572 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
573 dma_submit_error(s->cookie_tx)) {
574 s->cookie_tx = 0;
575 schedule_work(&s->work_tx);
576 }
577#endif
578
579 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
580 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
581 ctrl = serial_port_in(port, SCSCR);
582 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
583 }
584}
585
586static void sci_stop_tx(struct uart_port *port)
587{
588 unsigned short ctrl;
589
590 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
591 ctrl = serial_port_in(port, SCSCR);
592
593 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
594 ctrl &= ~SCSCR_TDRQE;
595
596 ctrl &= ~SCSCR_TIE;
597
598 serial_port_out(port, SCSCR, ctrl);
599}
600
601static void sci_start_rx(struct uart_port *port)
602{
603 unsigned short ctrl;
604
605 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
606
607 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
608 ctrl &= ~SCSCR_RDRQE;
609
610 serial_port_out(port, SCSCR, ctrl);
611}
612
613static void sci_stop_rx(struct uart_port *port)
614{
615 unsigned short ctrl;
616
617 ctrl = serial_port_in(port, SCSCR);
618
619 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
620 ctrl &= ~SCSCR_RDRQE;
621
622 ctrl &= ~port_rx_irq_mask(port);
623
624 serial_port_out(port, SCSCR, ctrl);
625}
626
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200627static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
628{
629 if (port->type == PORT_SCI) {
630 /* Just store the mask */
631 serial_port_out(port, SCxSR, mask);
632 } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
633 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
634 /* Only clear the status bits we want to clear */
635 serial_port_out(port, SCxSR,
636 serial_port_in(port, SCxSR) & mask);
637 } else {
638 /* Store the mask, clear parity/framing errors */
639 serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
640 }
641}
642
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900643#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900644
645#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900646static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned short status;
649 int c;
650
Paul Mundte108b2c2006-09-27 16:32:13 +0900651 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900652 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (status & SCxSR_ERRORS(port)) {
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200654 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 continue;
656 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500657 break;
658 } while (1);
659
660 if (!(status & SCxSR_RDxF(port)))
661 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900662
Paul Mundtb12bb292012-03-30 19:50:15 +0900663 c = serial_port_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900664
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900665 /* Dummy read */
Paul Mundtb12bb292012-03-30 19:50:15 +0900666 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200667 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return c;
670}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900673static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned short status;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900678 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 } while (!(status & SCxSR_TDxE(port)));
680
Paul Mundtb12bb292012-03-30 19:50:15 +0900681 serial_port_out(port, SCxTDR, c);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200682 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900684#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Paul Mundt61a69762011-06-14 12:40:19 +0900686static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900687{
Paul Mundt61a69762011-06-14 12:40:19 +0900688 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200689 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900690
Paul Mundt61a69762011-06-14 12:40:19 +0900691 /*
692 * Use port-specific handler if provided.
693 */
694 if (s->cfg->ops && s->cfg->ops->init_pins) {
695 s->cfg->ops->init_pins(port, cflag);
696 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Paul Mundt61a69762011-06-14 12:40:19 +0900699 /*
700 * For the generic path SCSPTR is necessary. Bail out if that's
701 * unavailable, too.
702 */
703 if (!reg->size)
704 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800705
Paul Mundtfaf02f82011-12-02 17:44:50 +0900706 if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
707 ((!(cflag & CRTSCTS)))) {
708 unsigned short status;
709
Paul Mundtb12bb292012-03-30 19:50:15 +0900710 status = serial_port_in(port, SCSPTR);
Paul Mundtfaf02f82011-12-02 17:44:50 +0900711 status &= ~SCSPTR_CTSIO;
712 status |= SCSPTR_RTSIO;
Paul Mundtb12bb292012-03-30 19:50:15 +0900713 serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
Paul Mundtfaf02f82011-12-02 17:44:50 +0900714 }
Paul Mundtd5701642008-12-16 20:07:27 +0900715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900717static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900718{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200719 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900720
721 reg = sci_getreg(port, SCTFDR);
722 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900723 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900724
725 reg = sci_getreg(port, SCFDR);
726 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900727 return serial_port_in(port, SCFDR) >> 8;
Paul Mundt72b294c2011-06-14 17:38:19 +0900728
Paul Mundtb12bb292012-03-30 19:50:15 +0900729 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900730}
731
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900732static int sci_txroom(struct uart_port *port)
733{
Paul Mundt72b294c2011-06-14 17:38:19 +0900734 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900735}
736
737static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900738{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200739 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900740
741 reg = sci_getreg(port, SCRFDR);
742 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900743 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900744
745 reg = sci_getreg(port, SCFDR);
746 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900747 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900748
Paul Mundtb12bb292012-03-30 19:50:15 +0900749 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900750}
751
Paul Mundt514820e2011-06-08 18:51:32 +0900752/*
753 * SCI helper for checking the state of the muxed port/RXD pins.
754 */
755static inline int sci_rxd_in(struct uart_port *port)
756{
757 struct sci_port *s = to_sci_port(port);
758
759 if (s->cfg->port_reg <= 0)
760 return 1;
761
Paul Mundt0dd4d5c2012-10-15 14:08:48 +0900762 /* Cast for ARM damage */
Laurent Pincharte2afca62013-12-11 13:40:31 +0100763 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
Paul Mundt514820e2011-06-08 18:51:32 +0900764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/* ********************************************************************** *
767 * the interrupt related routines *
768 * ********************************************************************** */
769
770static void sci_transmit_chars(struct uart_port *port)
771{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700772 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 unsigned short status;
775 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900776 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Paul Mundtb12bb292012-03-30 19:50:15 +0900778 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!(status & SCxSR_TDxE(port))) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900780 ctrl = serial_port_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900781 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900782 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900783 else
Paul Mundt8e698612009-06-24 19:44:32 +0900784 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900785 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return;
787 }
788
Paul Mundt72b294c2011-06-14 17:38:19 +0900789 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 do {
792 unsigned char c;
793
794 if (port->x_char) {
795 c = port->x_char;
796 port->x_char = 0;
797 } else if (!uart_circ_empty(xmit) && !stopped) {
798 c = xmit->buf[xmit->tail];
799 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
800 } else {
801 break;
802 }
803
Paul Mundtb12bb292012-03-30 19:50:15 +0900804 serial_port_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 port->icount.tx++;
807 } while (--count > 0);
808
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200809 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
812 uart_write_wakeup(port);
813 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100814 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900816 ctrl = serial_port_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900818 if (port->type != PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900819 serial_port_in(port, SCxSR); /* Dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200820 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Paul Mundt8e698612009-06-24 19:44:32 +0900823 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900824 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826}
827
828/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900829#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900831static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900833 struct sci_port *sci_port = to_sci_port(port);
Jiri Slaby227434f2013-01-03 15:53:01 +0100834 struct tty_port *tport = &port->state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int i, count, copied = 0;
836 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800837 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Paul Mundtb12bb292012-03-30 19:50:15 +0900839 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!(status & SCxSR_RDxF(port)))
841 return;
842
843 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Don't copy more bytes than there is room for in the buffer */
Jiri Slaby227434f2013-01-03 15:53:01 +0100845 count = tty_buffer_request_room(tport, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* If for any reason we can't copy more data, we're done! */
848 if (count == 0)
849 break;
850
851 if (port->type == PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900852 char c = serial_port_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900853 if (uart_handle_sysrq_char(port, c) ||
854 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900856 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100857 tty_insert_flip_char(tport, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900859 for (i = 0; i < count; i++) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900860 char c = serial_port_in(port, SCxRDR);
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900861
Paul Mundtb12bb292012-03-30 19:50:15 +0900862 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#if defined(CONFIG_CPU_SH3)
864 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900865 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if ((c == 0) &&
867 (status & SCxSR_FER(port))) {
868 count--; i--;
869 continue;
870 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900873 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900874 sci_port->break_flag = 0;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (STEPFN(c)) {
877 count--; i--;
878 continue;
879 }
880 }
881#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100882 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 count--; i--;
884 continue;
885 }
886
887 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900888 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800889 flag = TTY_FRAME;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900890 port->icount.frame++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900891 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900892 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800893 flag = TTY_PARITY;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900894 port->icount.parity++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900895 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800896 } else
897 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900898
Jiri Slaby92a19f92013-01-03 15:53:03 +0100899 tty_insert_flip_char(tport, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901 }
902
Paul Mundtb12bb292012-03-30 19:50:15 +0900903 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200904 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 copied += count;
907 port->icount.rx += count;
908 }
909
910 if (copied) {
911 /* Tell the rest of the system the news. New characters! */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100912 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900914 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200915 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917}
918
919#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900920
921/*
922 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * 1 per millisecond or so during the break period, for 9600 baud.
924 * So dont bother disabling interrupts.
925 * But dont want more than 1 break event.
926 * Use a kernel timer to periodically poll the rx line until
927 * the break is finished.
928 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900929static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900931 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/* Ensure that two consecutive samples find the break over. */
935static void sci_break_timer(unsigned long data)
936{
Paul Mundte108b2c2006-09-27 16:32:13 +0900937 struct sci_port *port = (struct sci_port *)data;
938
939 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900941 sci_schedule_break_timer(port);
942 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* break is over. */
944 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900945 sci_schedule_break_timer(port);
946 } else
947 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900950static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900953 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100954 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900955 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100957 /* Handle overruns */
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +0200958 if (status & s->overrun_mask) {
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100959 port->icount.overrun++;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900960
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100961 /* overrun error */
962 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
963 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900964
Joe Perches9b971cd2014-03-11 10:10:46 -0700965 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Paul Mundte108b2c2006-09-27 16:32:13 +0900968 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (sci_rxd_in(port) == 0) {
970 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900971 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900972
973 if (!sci_port->break_flag) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900974 port->icount.brk++;
975
Paul Mundte108b2c2006-09-27 16:32:13 +0900976 sci_port->break_flag = 1;
977 sci_schedule_break_timer(sci_port);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900980 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900982
983 dev_dbg(port->dev, "BREAK detected\n");
984
Jiri Slaby92a19f92013-01-03 15:53:03 +0100985 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900986 copied++;
987 }
988
Paul Mundte108b2c2006-09-27 16:32:13 +0900989 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* frame error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900991 port->icount.frame++;
992
Jiri Slaby92a19f92013-01-03 15:53:03 +0100993 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800994 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900995
996 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 }
999
Paul Mundte108b2c2006-09-27 16:32:13 +09001000 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* parity error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001002 port->icount.parity++;
1003
Jiri Slaby92a19f92013-01-03 15:53:03 +01001004 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
Paul Mundte108b2c2006-09-27 16:32:13 +09001005 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001006
Joe Perches9b971cd2014-03-11 10:10:46 -07001007 dev_notice(port->dev, "parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Alan Cox33f0f882006-01-09 20:54:13 -08001010 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001011 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return copied;
1014}
1015
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001016static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +09001017{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001018 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +09001019 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001020 const struct plat_sci_reg *reg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001021 int copied = 0;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001022 u16 status;
Paul Mundtd830fa42008-12-16 19:29:38 +09001023
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001024 reg = sci_getreg(port, s->overrun_reg);
Paul Mundt4b8c59a2011-06-14 17:53:34 +09001025 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +09001026 return 0;
1027
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001028 status = serial_port_in(port, s->overrun_reg);
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001029 if (status & s->overrun_mask) {
1030 status &= ~s->overrun_mask;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001031 serial_port_out(port, s->overrun_reg, status);
Paul Mundtd830fa42008-12-16 19:29:38 +09001032
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001033 port->icount.overrun++;
1034
Jiri Slaby92a19f92013-01-03 15:53:03 +01001035 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001036 tty_flip_buffer_push(tport);
Paul Mundtd830fa42008-12-16 19:29:38 +09001037
Yoshihiro Kaneko51b31f12015-01-26 20:53:29 +09001038 dev_dbg(port->dev, "overrun error\n");
Paul Mundtd830fa42008-12-16 19:29:38 +09001039 copied++;
1040 }
1041
1042 return copied;
1043}
1044
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001045static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +09001048 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +01001049 struct tty_port *tport = &port->state->port;
Magnus Damma5660ad2009-01-21 15:14:38 +00001050 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Paul Mundt0b3d4ef2007-03-14 13:22:37 +09001052 if (uart_handle_break(port))
1053 return 0;
1054
Paul Mundtb7a76e42006-02-01 03:06:06 -08001055 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#if defined(CONFIG_CPU_SH3)
1057 /* Debounce break */
1058 s->break_flag = 1;
1059#endif
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001060
1061 port->icount.brk++;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Notify of BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001064 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -08001065 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001066
1067 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Alan Cox33f0f882006-01-09 20:54:13 -08001070 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001071 tty_flip_buffer_push(tport);
Paul Mundte108b2c2006-09-27 16:32:13 +09001072
Paul Mundtd830fa42008-12-16 19:29:38 +09001073 copied += sci_handle_fifo_overrun(port);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return copied;
1076}
1077
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001078#ifdef CONFIG_SERIAL_SH_SCI_DMA
1079static void sci_dma_tx_complete(void *arg)
1080{
1081 struct sci_port *s = arg;
1082 struct uart_port *port = &s->port;
1083 struct circ_buf *xmit = &port->state->xmit;
1084 unsigned long flags;
1085
1086 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1087
1088 spin_lock_irqsave(&port->lock, flags);
1089
1090 xmit->tail += s->tx_dma_len;
1091 xmit->tail &= UART_XMIT_SIZE - 1;
1092
1093 port->icount.tx += s->tx_dma_len;
1094
1095 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1096 uart_write_wakeup(port);
1097
1098 if (!uart_circ_empty(xmit)) {
1099 s->cookie_tx = 0;
1100 schedule_work(&s->work_tx);
1101 } else {
1102 s->cookie_tx = -EINVAL;
1103 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1104 u16 ctrl = serial_port_in(port, SCSCR);
1105 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1106 }
1107 }
1108
1109 spin_unlock_irqrestore(&port->lock, flags);
1110}
1111
1112/* Locking: called with port lock held */
1113static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1114{
1115 struct uart_port *port = &s->port;
1116 struct tty_port *tport = &port->state->port;
1117 int copied;
1118
1119 copied = tty_insert_flip_string(tport, buf, count);
1120 if (copied < count) {
1121 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
1122 count - copied);
1123 port->icount.buf_overrun++;
1124 }
1125
1126 port->icount.rx += copied;
1127
1128 return copied;
1129}
1130
1131static int sci_dma_rx_find_active(struct sci_port *s)
1132{
1133 unsigned int i;
1134
1135 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1136 if (s->active_rx == s->cookie_rx[i])
1137 return i;
1138
1139 dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
1140 s->active_rx);
1141 return -1;
1142}
1143
1144static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1145{
1146 struct dma_chan *chan = s->chan_rx;
1147 struct uart_port *port = &s->port;
1148 unsigned long flags;
1149
1150 spin_lock_irqsave(&port->lock, flags);
1151 s->chan_rx = NULL;
1152 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1153 spin_unlock_irqrestore(&port->lock, flags);
1154 dmaengine_terminate_all(chan);
1155 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1156 sg_dma_address(&s->sg_rx[0]));
1157 dma_release_channel(chan);
1158 if (enable_pio)
1159 sci_start_rx(port);
1160}
1161
1162static void sci_dma_rx_complete(void *arg)
1163{
1164 struct sci_port *s = arg;
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001165 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001166 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001167 struct dma_async_tx_descriptor *desc;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001168 unsigned long flags;
1169 int active, count = 0;
1170
1171 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1172 s->active_rx);
1173
1174 spin_lock_irqsave(&port->lock, flags);
1175
1176 active = sci_dma_rx_find_active(s);
1177 if (active >= 0)
1178 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1179
1180 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1181
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001182 if (count)
1183 tty_flip_buffer_push(&port->state->port);
1184
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001185 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1186 DMA_DEV_TO_MEM,
1187 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1188 if (!desc)
1189 goto fail;
1190
1191 desc->callback = sci_dma_rx_complete;
1192 desc->callback_param = s;
1193 s->cookie_rx[active] = dmaengine_submit(desc);
1194 if (dma_submit_error(s->cookie_rx[active]))
1195 goto fail;
1196
1197 s->active_rx = s->cookie_rx[!active];
1198
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001199 dma_async_issue_pending(chan);
1200
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001201 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1202 __func__, s->cookie_rx[active], active, s->active_rx);
1203 spin_unlock_irqrestore(&port->lock, flags);
1204 return;
1205
1206fail:
1207 spin_unlock_irqrestore(&port->lock, flags);
1208 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1209 sci_rx_dma_release(s, true);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001210}
1211
1212static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1213{
1214 struct dma_chan *chan = s->chan_tx;
1215 struct uart_port *port = &s->port;
1216 unsigned long flags;
1217
1218 spin_lock_irqsave(&port->lock, flags);
1219 s->chan_tx = NULL;
1220 s->cookie_tx = -EINVAL;
1221 spin_unlock_irqrestore(&port->lock, flags);
1222 dmaengine_terminate_all(chan);
1223 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1224 DMA_TO_DEVICE);
1225 dma_release_channel(chan);
1226 if (enable_pio)
1227 sci_start_tx(port);
1228}
1229
1230static void sci_submit_rx(struct sci_port *s)
1231{
1232 struct dma_chan *chan = s->chan_rx;
1233 int i;
1234
1235 for (i = 0; i < 2; i++) {
1236 struct scatterlist *sg = &s->sg_rx[i];
1237 struct dma_async_tx_descriptor *desc;
1238
1239 desc = dmaengine_prep_slave_sg(chan,
1240 sg, 1, DMA_DEV_TO_MEM,
1241 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1242 if (!desc)
1243 goto fail;
1244
1245 desc->callback = sci_dma_rx_complete;
1246 desc->callback_param = s;
1247 s->cookie_rx[i] = dmaengine_submit(desc);
1248 if (dma_submit_error(s->cookie_rx[i]))
1249 goto fail;
1250
1251 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1252 s->cookie_rx[i], i);
1253 }
1254
1255 s->active_rx = s->cookie_rx[0];
1256
1257 dma_async_issue_pending(chan);
1258 return;
1259
1260fail:
1261 if (i)
1262 dmaengine_terminate_all(chan);
1263 for (i = 0; i < 2; i++)
1264 s->cookie_rx[i] = -EINVAL;
1265 s->active_rx = -EINVAL;
1266 dev_warn(s->port.dev, "Failed to re-start Rx DMA, using PIO\n");
1267 sci_rx_dma_release(s, true);
1268}
1269
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001270static void work_fn_tx(struct work_struct *work)
1271{
1272 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1273 struct dma_async_tx_descriptor *desc;
1274 struct dma_chan *chan = s->chan_tx;
1275 struct uart_port *port = &s->port;
1276 struct circ_buf *xmit = &port->state->xmit;
1277 dma_addr_t buf;
1278
1279 /*
1280 * DMA is idle now.
1281 * Port xmit buffer is already mapped, and it is one page... Just adjust
1282 * offsets and lengths. Since it is a circular buffer, we have to
1283 * transmit till the end, and then the rest. Take the port lock to get a
1284 * consistent xmit buffer state.
1285 */
1286 spin_lock_irq(&port->lock);
1287 buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1));
1288 s->tx_dma_len = min_t(unsigned int,
1289 CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1290 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1291 spin_unlock_irq(&port->lock);
1292
1293 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1294 DMA_MEM_TO_DEV,
1295 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1296 if (!desc) {
1297 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1298 /* switch to PIO */
1299 sci_tx_dma_release(s, true);
1300 return;
1301 }
1302
1303 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1304 DMA_TO_DEVICE);
1305
1306 spin_lock_irq(&port->lock);
1307 desc->callback = sci_dma_tx_complete;
1308 desc->callback_param = s;
1309 spin_unlock_irq(&port->lock);
1310 s->cookie_tx = dmaengine_submit(desc);
1311 if (dma_submit_error(s->cookie_tx)) {
1312 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1313 /* switch to PIO */
1314 sci_tx_dma_release(s, true);
1315 return;
1316 }
1317
1318 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
1319 __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1320
1321 dma_async_issue_pending(chan);
1322}
1323
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001324static void rx_timer_fn(unsigned long arg)
1325{
1326 struct sci_port *s = (struct sci_port *)arg;
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001327 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001328 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001329 struct dma_tx_state state;
1330 enum dma_status status;
1331 unsigned long flags;
1332 unsigned int read;
1333 int active, count;
1334 u16 scr;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001335
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001336 spin_lock_irqsave(&port->lock, flags);
1337
1338 dev_dbg(port->dev, "DMA Rx timed out\n");
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001339
1340 active = sci_dma_rx_find_active(s);
1341 if (active < 0) {
1342 spin_unlock_irqrestore(&port->lock, flags);
1343 return;
1344 }
1345
1346 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001347 if (status == DMA_COMPLETE) {
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001348 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1349 s->active_rx, active);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001350 spin_unlock_irqrestore(&port->lock, flags);
1351
1352 /* Let packet complete handler take care of the packet */
1353 return;
1354 }
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001355
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001356 dmaengine_pause(chan);
1357
1358 /*
1359 * sometimes DMA transfer doesn't stop even if it is stopped and
1360 * data keeps on coming until transaction is complete so check
1361 * for DMA_COMPLETE again
1362 * Let packet complete handler take care of the packet
1363 */
1364 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1365 if (status == DMA_COMPLETE) {
1366 spin_unlock_irqrestore(&port->lock, flags);
1367 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1368 return;
1369 }
1370
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001371 /* Handle incomplete DMA receive */
1372 dmaengine_terminate_all(s->chan_rx);
1373 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1374 dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
1375 s->active_rx);
1376
1377 if (read) {
1378 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1379 if (count)
1380 tty_flip_buffer_push(&port->state->port);
1381 }
1382
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001383 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1384 sci_submit_rx(s);
Muhammad Hamza Farooq371cfed2015-09-18 13:08:29 +02001385
1386 /* Direct new serial port interrupts back to CPU */
1387 scr = serial_port_in(port, SCSCR);
1388 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1389 scr &= ~SCSCR_RDRQE;
1390 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1391 }
1392 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1393
1394 spin_unlock_irqrestore(&port->lock, flags);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001395}
1396
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001397static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1398 enum dma_transfer_direction dir,
1399 unsigned int id)
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001400{
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001401 dma_cap_mask_t mask;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001402 struct dma_chan *chan;
1403 struct dma_slave_config cfg;
1404 int ret;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001405
1406 dma_cap_zero(mask);
1407 dma_cap_set(DMA_SLAVE, mask);
1408
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001409 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1410 (void *)(unsigned long)id, port->dev,
1411 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1412 if (!chan) {
1413 dev_warn(port->dev,
1414 "dma_request_slave_channel_compat failed\n");
1415 return NULL;
1416 }
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001417
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001418 memset(&cfg, 0, sizeof(cfg));
1419 cfg.direction = dir;
1420 if (dir == DMA_MEM_TO_DEV) {
1421 cfg.dst_addr = port->mapbase +
1422 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1423 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1424 } else {
1425 cfg.src_addr = port->mapbase +
1426 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1427 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1428 }
1429
1430 ret = dmaengine_slave_config(chan, &cfg);
1431 if (ret) {
1432 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1433 dma_release_channel(chan);
1434 return NULL;
1435 }
1436
1437 return chan;
1438}
1439
1440static void sci_request_dma(struct uart_port *port)
1441{
1442 struct sci_port *s = to_sci_port(port);
1443 struct dma_chan *chan;
1444
1445 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1446
1447 if (!port->dev->of_node &&
1448 (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
1449 return;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001450
1451 s->cookie_tx = -EINVAL;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001452 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001453 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1454 if (chan) {
1455 s->chan_tx = chan;
1456 /* UART circular tx buffer is an aligned page. */
1457 s->tx_dma_addr = dma_map_single(chan->device->dev,
1458 port->state->xmit.buf,
1459 UART_XMIT_SIZE,
1460 DMA_TO_DEVICE);
1461 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1462 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1463 dma_release_channel(chan);
1464 s->chan_tx = NULL;
1465 } else {
1466 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1467 __func__, UART_XMIT_SIZE,
1468 port->state->xmit.buf, &s->tx_dma_addr);
1469 }
1470
1471 INIT_WORK(&s->work_tx, work_fn_tx);
1472 }
1473
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001474 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001475 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1476 if (chan) {
1477 unsigned int i;
1478 dma_addr_t dma;
1479 void *buf;
1480
1481 s->chan_rx = chan;
1482
1483 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1484 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1485 &dma, GFP_KERNEL);
1486 if (!buf) {
1487 dev_warn(port->dev,
1488 "Failed to allocate Rx dma buffer, using PIO\n");
1489 dma_release_channel(chan);
1490 s->chan_rx = NULL;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001491 return;
1492 }
1493
1494 for (i = 0; i < 2; i++) {
1495 struct scatterlist *sg = &s->sg_rx[i];
1496
1497 sg_init_table(sg, 1);
1498 s->rx_buf[i] = buf;
1499 sg_dma_address(sg) = dma;
1500 sg->length = s->buf_len_rx;
1501
1502 buf += s->buf_len_rx;
1503 dma += s->buf_len_rx;
1504 }
1505
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001506 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1507
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001508 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1509 sci_submit_rx(s);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001510 }
1511}
1512
1513static void sci_free_dma(struct uart_port *port)
1514{
1515 struct sci_port *s = to_sci_port(port);
1516
1517 if (s->chan_tx)
1518 sci_tx_dma_release(s, false);
1519 if (s->chan_rx)
1520 sci_rx_dma_release(s, false);
1521}
1522#else
1523static inline void sci_request_dma(struct uart_port *port)
1524{
1525}
1526
1527static inline void sci_free_dma(struct uart_port *port)
1528{
1529}
1530#endif
1531
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001532static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001534#ifdef CONFIG_SERIAL_SH_SCI_DMA
1535 struct uart_port *port = ptr;
1536 struct sci_port *s = to_sci_port(port);
1537
1538 if (s->chan_rx) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001539 u16 scr = serial_port_in(port, SCSCR);
1540 u16 ssr = serial_port_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001541
1542 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001543 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001544 disable_irq_nosync(irq);
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001545 scr |= SCSCR_RDRQE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001546 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +09001547 scr &= ~SCSCR_RIE;
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001548 sci_submit_rx(s);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001549 }
Paul Mundtb12bb292012-03-30 19:50:15 +09001550 serial_port_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001551 /* Clear current interrupt */
Geert Uytterhoeven54af5002015-08-21 20:02:28 +02001552 serial_port_out(port, SCxSR,
1553 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001554 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
1555 jiffies, s->rx_timeout);
1556 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001557
1558 return IRQ_HANDLED;
1559 }
1560#endif
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /* I think sci_receive_chars has to be called irrespective
1563 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1564 * to be disabled?
1565 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001566 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 return IRQ_HANDLED;
1569}
1570
David Howells7d12e782006-10-05 14:55:46 +01001571static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +09001574 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Stuart Menefyfd78a762009-07-29 23:01:24 +09001576 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +09001578 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 return IRQ_HANDLED;
1581}
1582
David Howells7d12e782006-10-05 14:55:46 +01001583static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct uart_port *port = ptr;
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001586 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* Handle errors */
1589 if (port->type == PORT_SCI) {
1590 if (sci_handle_errors(port)) {
1591 /* discard character in rx buffer */
Paul Mundtb12bb292012-03-30 19:50:15 +09001592 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001593 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +09001596 sci_handle_fifo_overrun(port);
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001597 if (!s->chan_rx)
1598 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001601 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* Kick the transmission */
Yoshihiro Shimoda8eadb562015-08-21 20:02:56 +02001604 if (!s->chan_tx)
1605 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return IRQ_HANDLED;
1608}
1609
David Howells7d12e782006-10-05 14:55:46 +01001610static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct uart_port *port = ptr;
1613
1614 /* Handle BREAKs */
1615 sci_handle_breaks(port);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001616 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 return IRQ_HANDLED;
1619}
1620
David Howells7d12e782006-10-05 14:55:46 +01001621static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Nobuhiro Iwamatsucb772fe72015-03-17 01:19:19 +09001623 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
Michael Trimarchia8884e32008-10-31 16:10:23 +09001624 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001625 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +09001626 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Paul Mundtb12bb292012-03-30 19:50:15 +09001628 ssr_status = serial_port_in(port, SCxSR);
1629 scr_status = serial_port_in(port, SCSCR);
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001630 if (s->overrun_reg == SCxSR)
Nobuhiro Iwamatsucb772fe72015-03-17 01:19:19 +09001631 orer_status = ssr_status;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001632 else {
1633 if (sci_getreg(port, s->overrun_reg)->size)
1634 orer_status = serial_port_in(port, s->overrun_reg);
Nobuhiro Iwamatsucb772fe72015-03-17 01:19:19 +09001635 }
1636
Paul Mundtf43dc232011-01-13 15:06:28 +09001637 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +09001640 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001641 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001642 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001643
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001644 /*
1645 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1646 * DR flags
1647 */
1648 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Geert Uytterhoevene0a12a22015-08-21 20:02:35 +02001649 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +09001650 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001653 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001654 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001657 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001658 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001660 /* Overrun Interrupt */
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001661 if (orer_status & s->overrun_mask) {
Nobuhiro Iwamatsucb772fe72015-03-17 01:19:19 +09001662 sci_handle_fifo_overrun(port);
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001663 ret = IRQ_HANDLED;
1664 }
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001665
Michael Trimarchia8884e32008-10-31 16:10:23 +09001666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001670 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * ports' baud rate when the peripheral clock changes.
1672 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001673static int sci_notifier(struct notifier_block *self,
1674 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Magnus Damme552de22009-01-21 15:13:42 +00001676 struct sci_port *sci_port;
1677 unsigned long flags;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001678 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Paul Mundtd535a232011-01-19 17:19:35 +09001680 sci_port = container_of(self, struct sci_port, freq_transition);
1681
Viresh Kumar0b443ea2014-03-19 11:24:58 +05301682 if (phase == CPUFREQ_POSTCHANGE) {
Paul Mundtd535a232011-01-19 17:19:35 +09001683 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +09001684
Paul Mundtd535a232011-01-19 17:19:35 +09001685 spin_lock_irqsave(&port->lock, flags);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001686 for (i = 0; i < SCI_NUM_CLKS; i++)
1687 sci_port->clk_rates[i] =
1688 clk_get_rate(sci_port->clks[i]);
Paul Mundtd535a232011-01-19 17:19:35 +09001689 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +00001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return NOTIFY_OK;
1693}
Magnus Damm501b8252009-01-21 15:14:30 +00001694
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001695static const struct sci_irq_desc {
Paul Mundt9174fc82011-06-28 15:25:36 +09001696 const char *desc;
1697 irq_handler_t handler;
1698} sci_irq_desc[] = {
1699 /*
1700 * Split out handlers, the default case.
1701 */
1702 [SCIx_ERI_IRQ] = {
1703 .desc = "rx err",
1704 .handler = sci_er_interrupt,
1705 },
1706
1707 [SCIx_RXI_IRQ] = {
1708 .desc = "rx full",
1709 .handler = sci_rx_interrupt,
1710 },
1711
1712 [SCIx_TXI_IRQ] = {
1713 .desc = "tx empty",
1714 .handler = sci_tx_interrupt,
1715 },
1716
1717 [SCIx_BRI_IRQ] = {
1718 .desc = "break",
1719 .handler = sci_br_interrupt,
1720 },
1721
1722 /*
1723 * Special muxed handler.
1724 */
1725 [SCIx_MUX_IRQ] = {
1726 .desc = "mux",
1727 .handler = sci_mpxed_interrupt,
1728 },
1729};
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731static int sci_request_irq(struct sci_port *port)
1732{
Paul Mundt9174fc82011-06-28 15:25:36 +09001733 struct uart_port *up = &port->port;
1734 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Paul Mundt9174fc82011-06-28 15:25:36 +09001736 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001737 const struct sci_irq_desc *desc;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001738 int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001739
Paul Mundt9174fc82011-06-28 15:25:36 +09001740 if (SCIx_IRQ_IS_MUXED(port)) {
1741 i = SCIx_MUX_IRQ;
1742 irq = up->irq;
Paul Mundt0e8963d2012-05-18 18:21:06 +09001743 } else {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001744 irq = port->irqs[i];
Paul Mundt9174fc82011-06-28 15:25:36 +09001745
Paul Mundt0e8963d2012-05-18 18:21:06 +09001746 /*
1747 * Certain port types won't support all of the
1748 * available interrupt sources.
1749 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001750 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001751 continue;
1752 }
1753
Paul Mundt9174fc82011-06-28 15:25:36 +09001754 desc = sci_irq_desc + i;
1755 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1756 dev_name(up->dev), desc->desc);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02001757 if (!port->irqstr[j])
Paul Mundt9174fc82011-06-28 15:25:36 +09001758 goto out_nomem;
Paul Mundt762c69e2008-12-16 18:55:26 +09001759
Paul Mundt9174fc82011-06-28 15:25:36 +09001760 ret = request_irq(irq, desc->handler, up->irqflags,
1761 port->irqstr[j], port);
1762 if (unlikely(ret)) {
1763 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1764 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 }
1767
1768 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001769
1770out_noirq:
1771 while (--i >= 0)
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001772 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001773
1774out_nomem:
1775 while (--j >= 0)
1776 kfree(port->irqstr[j]);
1777
1778 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
1781static void sci_free_irq(struct sci_port *port)
1782{
1783 int i;
1784
Paul Mundt9174fc82011-06-28 15:25:36 +09001785 /*
1786 * Intentionally in reverse order so we iterate over the muxed
1787 * IRQ first.
1788 */
1789 for (i = 0; i < SCIx_NR_IRQS; i++) {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001790 int irq = port->irqs[i];
Paul Mundt0e8963d2012-05-18 18:21:06 +09001791
1792 /*
1793 * Certain port types won't support all of the available
1794 * interrupt sources.
1795 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001796 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001797 continue;
1798
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001799 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001800 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Paul Mundt9174fc82011-06-28 15:25:36 +09001802 if (SCIx_IRQ_IS_MUXED(port)) {
1803 /* If there's only one IRQ, we're done. */
1804 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806 }
1807}
1808
1809static unsigned int sci_tx_empty(struct uart_port *port)
1810{
Paul Mundtb12bb292012-03-30 19:50:15 +09001811 unsigned short status = serial_port_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001812 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001813
1814 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Paul Mundtcdf7c422011-11-24 20:18:32 +09001817/*
1818 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1819 * CTS/RTS is supported in hardware by at least one port and controlled
1820 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1821 * handled via the ->init_pins() op, which is a bit of a one-way street,
1822 * lacking any ability to defer pin control -- this will later be
1823 * converted over to the GPIO framework).
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001824 *
1825 * Other modes (such as loopback) are supported generically on certain
1826 * port types, but not others. For these it's sufficient to test for the
1827 * existence of the support register and simply ignore the port type.
Paul Mundtcdf7c422011-11-24 20:18:32 +09001828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1830{
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001831 if (mctrl & TIOCM_LOOP) {
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001832 const struct plat_sci_reg *reg;
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001833
1834 /*
1835 * Standard loopback mode for SCFCR ports.
1836 */
1837 reg = sci_getreg(port, SCFCR);
1838 if (reg->size)
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001839 serial_port_out(port, SCFCR,
1840 serial_port_in(port, SCFCR) |
1841 SCFCR_LOOP);
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845static unsigned int sci_get_mctrl(struct uart_port *port)
1846{
Paul Mundtcdf7c422011-11-24 20:18:32 +09001847 /*
1848 * CTS/RTS is handled in hardware when supported, while nothing
1849 * else is wired up. Keep it simple and simply assert DSR/CAR.
1850 */
1851 return TIOCM_DSR | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static void sci_break_ctl(struct uart_port *port, int break_state)
1855{
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001856 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001857 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001858 unsigned short scscr, scsptr;
1859
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001860 /* check wheter the port has SCSPTR */
1861 if (!reg->size) {
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001862 /*
1863 * Not supported by hardware. Most parts couple break and rx
1864 * interrupts together, with break detection always enabled.
1865 */
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001866 return;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001867 }
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001868
1869 scsptr = serial_port_in(port, SCSPTR);
1870 scscr = serial_port_in(port, SCSCR);
1871
1872 if (break_state == -1) {
1873 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1874 scscr &= ~SCSCR_TE;
1875 } else {
1876 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1877 scscr |= SCSCR_TE;
1878 }
1879
1880 serial_port_out(port, SCSPTR, scsptr);
1881 serial_port_out(port, SCSCR, scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884static int sci_startup(struct uart_port *port)
1885{
Magnus Damma5660ad2009-01-21 15:14:38 +00001886 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001887 unsigned long flags;
Paul Mundt073e84c2011-01-19 17:30:53 +09001888 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001890 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1891
Paul Mundt073e84c2011-01-19 17:30:53 +09001892 ret = sci_request_irq(s);
1893 if (unlikely(ret < 0))
1894 return ret;
1895
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001896 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001897
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001898 spin_lock_irqsave(&port->lock, flags);
Yoshinori Satod6569012005-10-14 15:59:12 -07001899 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001900 sci_start_rx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001901 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 return 0;
1904}
1905
1906static void sci_shutdown(struct uart_port *port)
1907{
Magnus Damma5660ad2009-01-21 15:14:38 +00001908 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001909 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001911 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1912
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001913 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001915 sci_stop_tx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001916 spin_unlock_irqrestore(&port->lock, flags);
Paul Mundt073e84c2011-01-19 17:30:53 +09001917
Aleksandar Mitev9ab76552015-09-18 13:08:28 +02001918#ifdef CONFIG_SERIAL_SH_SCI_DMA
1919 if (s->chan_rx) {
1920 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
1921 port->line);
1922 del_timer_sync(&s->rx_timer);
1923 }
1924#endif
1925
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001926 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 sci_free_irq(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001930static int sci_sck_calc(struct sci_port *s, unsigned int bps,
1931 unsigned int *srr)
1932{
1933 unsigned long freq = s->clk_rates[SCI_SCK];
1934 unsigned int min_sr, max_sr, sr;
1935 int err, min_err = INT_MAX;
1936
1937 if (s->sampling_rate) {
1938 /* SCI(F) has a fixed sampling rate */
1939 min_sr = max_sr = s->sampling_rate / 2;
1940 } else {
1941 /* HSCIF has a variable 1/(8..32) sampling rate */
1942 min_sr = 8;
1943 max_sr = 32;
1944 }
1945
1946 for (sr = max_sr; sr >= min_sr; sr--) {
1947 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
1948 if (abs(err) >= abs(min_err))
1949 continue;
1950
1951 min_err = err;
1952 *srr = sr - 1;
1953
1954 if (!err)
1955 break;
1956 }
1957
1958 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
1959 *srr + 1);
1960 return min_err;
1961}
1962
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01001963static int sci_brg_calc(struct sci_port *s, unsigned int bps,
1964 unsigned long freq, unsigned int *dlr,
1965 unsigned int *srr)
1966{
1967 unsigned int min_sr, max_sr, sr, dl;
1968 int err, min_err = INT_MAX;
1969
1970 if (s->sampling_rate) {
1971 /* SCIF has a fixed sampling rate */
1972 min_sr = max_sr = s->sampling_rate / 2;
1973 } else {
1974 /* HSCIF has a variable 1/(8..32) sampling rate */
1975 min_sr = 8;
1976 max_sr = 32;
1977 }
1978
1979 for (sr = max_sr; sr >= min_sr; sr--) {
1980 dl = DIV_ROUND_CLOSEST(freq, sr * bps);
1981 dl = clamp(dl, 1U, 65535U);
1982
1983 err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
1984 if (abs(err) >= abs(min_err))
1985 continue;
1986
1987 min_err = err;
1988 *dlr = dl;
1989 *srr = sr - 1;
1990
1991 if (!err)
1992 break;
1993 }
1994
1995 dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
1996 min_err, *dlr, *srr + 1);
1997 return min_err;
1998}
1999
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002000/* calculate sample rate, BRR, and clock select */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002001static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
2002 unsigned int *brr, unsigned int *srr,
2003 unsigned int *cks)
Paul Mundt26c92f32009-06-24 18:23:52 +09002004{
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002005 unsigned int min_sr, max_sr, shift, sr, br, prediv, scrate, c;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002006 unsigned long freq = s->clk_rates[SCI_FCK];
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002007 int err, min_err = INT_MAX;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002008
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002009 if (s->sampling_rate) {
2010 min_sr = max_sr = s->sampling_rate;
2011 shift = 0;
2012 } else {
2013 /* HSCIF has a variable sample rate */
2014 min_sr = 8;
2015 max_sr = 32;
2016 shift = 1;
2017 }
2018
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002019 /*
2020 * Find the combination of sample rate and clock select with the
2021 * smallest deviation from the desired baud rate.
2022 * Prefer high sample rates to maximise the receive margin.
2023 *
2024 * M: Receive margin (%)
2025 * N: Ratio of bit rate to clock (N = sampling rate)
2026 * D: Clock duty (D = 0 to 1.0)
2027 * L: Frame length (L = 9 to 12)
2028 * F: Absolute value of clock frequency deviation
2029 *
2030 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2031 * (|D - 0.5| / N * (1 + F))|
2032 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2033 */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002034 for (sr = max_sr; sr >= min_sr; sr--) {
Ulrich Hechtf303b362013-05-31 17:57:01 +02002035 for (c = 0; c <= 3; c++) {
2036 /* integerized formulas from HSCIF documentation */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01002037 prediv = sr * (1 << (2 * c + shift));
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002038
2039 /*
2040 * We need to calculate:
2041 *
2042 * br = freq / (prediv * bps) clamped to [1..256]
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002043 * err = freq / (br * prediv) - bps
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002044 *
2045 * Watch out for overflow when calculating the desired
2046 * sampling clock rate!
2047 */
2048 if (bps > UINT_MAX / prediv)
2049 break;
2050
2051 scrate = prediv * bps;
2052 br = DIV_ROUND_CLOSEST(freq, scrate);
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002053 br = clamp(br, 1U, 256U);
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002054
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002055 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002056 if (abs(err) >= abs(min_err))
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002057 continue;
2058
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002059 min_err = err;
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002060 *brr = br - 1;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002061 *srr = sr - 1;
2062 *cks = c;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002063
2064 if (!err)
2065 goto found;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002066 }
2067 }
2068
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002069found:
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002070 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2071 min_err, *brr, *srr + 1, *cks);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002072 return min_err;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002073}
2074
Magnus Damm1ba76222011-08-03 03:47:36 +00002075static void sci_reset(struct uart_port *port)
2076{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002077 const struct plat_sci_reg *reg;
Magnus Damm1ba76222011-08-03 03:47:36 +00002078 unsigned int status;
2079
2080 do {
Paul Mundtb12bb292012-03-30 19:50:15 +09002081 status = serial_port_in(port, SCxSR);
Magnus Damm1ba76222011-08-03 03:47:36 +00002082 } while (!(status & SCxSR_TEND(port)));
2083
Paul Mundtb12bb292012-03-30 19:50:15 +09002084 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
Magnus Damm1ba76222011-08-03 03:47:36 +00002085
Paul Mundt0979e0e2011-11-24 18:35:49 +09002086 reg = sci_getreg(port, SCFCR);
2087 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09002088 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Magnus Damm1ba76222011-08-03 03:47:36 +00002089}
2090
Alan Cox606d0992006-12-08 02:38:45 -08002091static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2092 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002094 unsigned int baud, smr_val = 0, scr_val = 0, i;
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002095 unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
2096 unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
Paul Mundt00b9de92009-06-24 17:53:33 +09002097 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002098 const struct plat_sci_reg *reg;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002099 int min_err = INT_MAX, err;
2100 unsigned long max_freq = 0;
2101 int best_clk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002103 if ((termios->c_cflag & CSIZE) == CS7)
2104 smr_val |= SCSMR_CHR;
2105 if (termios->c_cflag & PARENB)
2106 smr_val |= SCSMR_PE;
2107 if (termios->c_cflag & PARODD)
2108 smr_val |= SCSMR_PE | SCSMR_ODD;
2109 if (termios->c_cflag & CSTOPB)
2110 smr_val |= SCSMR_STOP;
2111
Magnus Damm154280f2009-12-22 03:37:28 +00002112 /*
2113 * earlyprintk comes here early on with port->uartclk set to zero.
2114 * the clock framework is not up and running at this point so here
2115 * we assume that 115200 is the maximum baud rate. please note that
2116 * the baud rate is not programmed during earlyprintk - it is assumed
2117 * that the previous boot loader has enabled required clocks and
2118 * setup the baud rate generator hardware for us already.
2119 */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002120 if (!port->uartclk) {
2121 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2122 goto done;
2123 }
Magnus Damm154280f2009-12-22 03:37:28 +00002124
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002125 for (i = 0; i < SCI_NUM_CLKS; i++)
2126 max_freq = max(max_freq, s->clk_rates[i]);
2127
2128 baud = uart_get_baud_rate(port, termios, old, 0,
2129 max_freq / max(s->sampling_rate, 8U));
2130 if (!baud)
2131 goto done;
2132
2133 /*
2134 * There can be multiple sources for the sampling clock. Find the one
2135 * that gives us the smallest deviation from the desired baud rate.
2136 */
2137
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002138 /* Optional Undivided External Clock */
2139 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2140 port->type != PORT_SCIFB) {
2141 err = sci_sck_calc(s, baud, &srr1);
2142 if (abs(err) < abs(min_err)) {
2143 best_clk = SCI_SCK;
2144 scr_val = SCSCR_CKE1;
2145 sccks = SCCKS_CKS;
2146 min_err = err;
2147 srr = srr1;
2148 if (!err)
2149 goto done;
2150 }
2151 }
2152
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002153 /* Optional BRG Frequency Divided External Clock */
2154 if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
2155 err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
2156 &srr1);
2157 if (abs(err) < abs(min_err)) {
2158 best_clk = SCI_SCIF_CLK;
2159 scr_val = SCSCR_CKE1;
2160 sccks = 0;
2161 min_err = err;
2162 dl = dl1;
2163 srr = srr1;
2164 if (!err)
2165 goto done;
2166 }
2167 }
2168
2169 /* Optional BRG Frequency Divided Internal Clock */
2170 if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
2171 err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
2172 &srr1);
2173 if (abs(err) < abs(min_err)) {
2174 best_clk = SCI_BRG_INT;
2175 scr_val = SCSCR_CKE1;
2176 sccks = SCCKS_XIN;
2177 min_err = err;
2178 dl = dl1;
2179 srr = srr1;
2180 if (!min_err)
2181 goto done;
2182 }
2183 }
2184
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002185 /* Divided Functional Clock using standard Bit Rate Register */
2186 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2187 if (abs(err) < abs(min_err)) {
2188 best_clk = SCI_FCK;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002189 scr_val = 0;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002190 min_err = err;
2191 brr = brr1;
2192 srr = srr1;
2193 cks = cks1;
2194 }
2195
2196done:
2197 if (best_clk >= 0)
2198 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2199 s->clks[best_clk], baud, min_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Paul Mundt23241d42011-06-28 13:55:31 +09002201 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00002202
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002203 /*
2204 * Program the optional External Baud Rate Generator (BRG) first.
2205 * It controls the mux to select (H)SCK or frequency divided clock.
2206 */
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002207 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
2208 serial_port_out(port, SCDL, dl);
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002209 serial_port_out(port, SCCKS, sccks);
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002210 }
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002211
Magnus Damm1ba76222011-08-03 03:47:36 +00002212 sci_reset(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002213
Paul Mundte108b2c2006-09-27 16:32:13 +09002214 uart_update_timeout(port, termios->c_cflag, baud);
2215
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002216 if (best_clk >= 0) {
2217 smr_val |= cks;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002218 dev_dbg(port->dev,
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002219 "SCR 0x%x SMR 0x%x BRR %u CKS 0x%x DL %u SRR %u\n",
2220 scr_val, smr_val, brr, sccks, dl, srr);
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002221 serial_port_out(port, SCSCR, scr_val);
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09002222 serial_port_out(port, SCSMR, smr_val);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002223 serial_port_out(port, SCBRR, brr);
2224 if (sci_getreg(port, HSSRR)->size)
2225 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
2226
2227 /* Wait one bit interval */
2228 udelay((1000000 + (baud - 1)) / baud);
2229 } else {
2230 /* Don't touch the bit rate configuration */
2231 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
2232 smr_val |= serial_port_in(port, SCSMR) & SCSMR_CKS;
2233 dev_dbg(port->dev, "SCR 0x%x SMR 0x%x\n", scr_val, smr_val);
2234 serial_port_out(port, SCSCR, scr_val);
2235 serial_port_out(port, SCSMR, smr_val);
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Paul Mundtd5701642008-12-16 20:07:27 +09002238 sci_init_pins(port, termios->c_cflag);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002239
Paul Mundt73c3d532011-12-02 19:02:06 +09002240 reg = sci_getreg(port, SCFCR);
2241 if (reg->size) {
Paul Mundtb12bb292012-03-30 19:50:15 +09002242 unsigned short ctrl = serial_port_in(port, SCFCR);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002243
Paul Mundt73c3d532011-12-02 19:02:06 +09002244 if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
Paul Mundtfaf02f82011-12-02 17:44:50 +09002245 if (termios->c_cflag & CRTSCTS)
2246 ctrl |= SCFCR_MCE;
2247 else
2248 ctrl &= ~SCFCR_MCE;
Paul Mundtfaf02f82011-12-02 17:44:50 +09002249 }
Paul Mundt73c3d532011-12-02 19:02:06 +09002250
2251 /*
2252 * As we've done a sci_reset() above, ensure we don't
2253 * interfere with the FIFOs while toggling MCE. As the
2254 * reset values could still be set, simply mask them out.
2255 */
2256 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2257
Paul Mundtb12bb292012-03-30 19:50:15 +09002258 serial_port_out(port, SCFCR, ctrl);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002259 }
Paul Mundtb7a76e42006-02-01 03:06:06 -08002260
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002261 scr_val |= s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0);
2262 dev_dbg(port->dev, "SCSCR 0x%x\n", scr_val);
2263 serial_port_out(port, SCSCR, scr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002265#ifdef CONFIG_SERIAL_SH_SCI_DMA
2266 /*
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002267 * Calculate delay for 2 DMA buffers (4 FIFO).
Geert Uytterhoevenf5835c12015-08-21 20:02:38 +02002268 * See serial_core.c::uart_update_timeout().
2269 * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
2270 * function calculates 1 jiffie for the data plus 5 jiffies for the
2271 * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
2272 * buffers (4 FIFO sizes), but when performing a faster transfer, the
2273 * value obtained by this formula is too small. Therefore, if the value
2274 * is smaller than 20ms, use 20ms as the timeout value for DMA.
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002275 */
2276 if (s->chan_rx) {
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002277 unsigned int bits;
2278
2279 /* byte size and parity */
2280 switch (termios->c_cflag & CSIZE) {
2281 case CS5:
2282 bits = 7;
2283 break;
2284 case CS6:
2285 bits = 8;
2286 break;
2287 case CS7:
2288 bits = 9;
2289 break;
2290 default:
2291 bits = 10;
2292 break;
2293 }
2294
2295 if (termios->c_cflag & CSTOPB)
2296 bits++;
2297 if (termios->c_cflag & PARENB)
2298 bits++;
2299 s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
2300 (baud / 10), 10);
Joe Perches9b971cd2014-03-11 10:10:46 -07002301 dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002302 s->rx_timeout * 1000 / HZ, port->timeout);
2303 if (s->rx_timeout < msecs_to_jiffies(20))
2304 s->rx_timeout = msecs_to_jiffies(20);
2305 }
2306#endif
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002309 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00002310
Paul Mundt23241d42011-06-28 13:55:31 +09002311 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002314static void sci_pm(struct uart_port *port, unsigned int state,
2315 unsigned int oldstate)
2316{
2317 struct sci_port *sci_port = to_sci_port(port);
2318
2319 switch (state) {
Geert Uytterhoevend3dfe5d2014-03-11 11:11:20 +01002320 case UART_PM_STATE_OFF:
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002321 sci_port_disable(sci_port);
2322 break;
2323 default:
2324 sci_port_enable(sci_port);
2325 break;
2326 }
2327}
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329static const char *sci_type(struct uart_port *port)
2330{
2331 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09002332 case PORT_IRDA:
2333 return "irda";
2334 case PORT_SCI:
2335 return "sci";
2336 case PORT_SCIF:
2337 return "scif";
2338 case PORT_SCIFA:
2339 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002340 case PORT_SCIFB:
2341 return "scifb";
Ulrich Hechtf303b362013-05-31 17:57:01 +02002342 case PORT_HSCIF:
2343 return "hscif";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345
Paul Mundtfa439722008-09-04 18:53:58 +09002346 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Paul Mundtf6e94952011-01-21 15:25:36 +09002349static int sci_remap_port(struct uart_port *port)
2350{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002351 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002352
2353 /*
2354 * Nothing to do if there's already an established membase.
2355 */
2356 if (port->membase)
2357 return 0;
2358
2359 if (port->flags & UPF_IOREMAP) {
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002360 port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
Paul Mundtf6e94952011-01-21 15:25:36 +09002361 if (unlikely(!port->membase)) {
2362 dev_err(port->dev, "can't remap port#%d\n", port->line);
2363 return -ENXIO;
2364 }
2365 } else {
2366 /*
2367 * For the simple (and majority of) cases where we don't
2368 * need to do any remapping, just cast the cookie
2369 * directly.
2370 */
Jingoo Han3af4e962014-02-05 09:56:37 +09002371 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
Paul Mundtf6e94952011-01-21 15:25:36 +09002372 }
2373
2374 return 0;
2375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377static void sci_release_port(struct uart_port *port)
2378{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002379 struct sci_port *sport = to_sci_port(port);
2380
Paul Mundte2651642011-01-20 21:24:03 +09002381 if (port->flags & UPF_IOREMAP) {
2382 iounmap(port->membase);
2383 port->membase = NULL;
2384 }
2385
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002386 release_mem_region(port->mapbase, sport->reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
2389static int sci_request_port(struct uart_port *port)
2390{
Paul Mundte2651642011-01-20 21:24:03 +09002391 struct resource *res;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002392 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002393 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002395 res = request_mem_region(port->mapbase, sport->reg_size,
2396 dev_name(port->dev));
2397 if (unlikely(res == NULL)) {
2398 dev_err(port->dev, "request_mem_region failed.");
Paul Mundte2651642011-01-20 21:24:03 +09002399 return -EBUSY;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Paul Mundtf6e94952011-01-21 15:25:36 +09002402 ret = sci_remap_port(port);
2403 if (unlikely(ret != 0)) {
2404 release_resource(res);
2405 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002406 }
Paul Mundte2651642011-01-20 21:24:03 +09002407
2408 return 0;
2409}
2410
2411static void sci_config_port(struct uart_port *port, int flags)
2412{
2413 if (flags & UART_CONFIG_TYPE) {
2414 struct sci_port *sport = to_sci_port(port);
2415
2416 port->type = sport->cfg->type;
2417 sci_request_port(port);
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
2421static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (ser->baud_base < 2400)
2424 /* No paper tape reader for Mitch.. */
2425 return -EINVAL;
2426
2427 return 0;
2428}
2429
2430static struct uart_ops sci_uart_ops = {
2431 .tx_empty = sci_tx_empty,
2432 .set_mctrl = sci_set_mctrl,
2433 .get_mctrl = sci_get_mctrl,
2434 .start_tx = sci_start_tx,
2435 .stop_tx = sci_stop_tx,
2436 .stop_rx = sci_stop_rx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 .break_ctl = sci_break_ctl,
2438 .startup = sci_startup,
2439 .shutdown = sci_shutdown,
2440 .set_termios = sci_set_termios,
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002441 .pm = sci_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 .type = sci_type,
2443 .release_port = sci_release_port,
2444 .request_port = sci_request_port,
2445 .config_port = sci_config_port,
2446 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002447#ifdef CONFIG_CONSOLE_POLL
2448 .poll_get_char = sci_poll_get_char,
2449 .poll_put_char = sci_poll_put_char,
2450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451};
2452
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002453static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2454{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002455 const char *clk_names[] = {
2456 [SCI_FCK] = "fck",
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002457 [SCI_SCK] = "sck",
Geert Uytterhoeven1270f862015-11-18 11:25:53 +01002458 [SCI_BRG_INT] = "brg_int",
2459 [SCI_SCIF_CLK] = "scif_clk",
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002460 };
2461 struct clk *clk;
2462 unsigned int i;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002463
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002464 if (sci_port->cfg->type == PORT_HSCIF)
2465 clk_names[SCI_SCK] = "hsck";
2466
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002467 for (i = 0; i < SCI_NUM_CLKS; i++) {
2468 clk = devm_clk_get(dev, clk_names[i]);
2469 if (PTR_ERR(clk) == -EPROBE_DEFER)
2470 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002471
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002472 if (IS_ERR(clk) && i == SCI_FCK) {
2473 /*
2474 * "fck" used to be called "sci_ick", and we need to
2475 * maintain DT backward compatibility.
2476 */
2477 clk = devm_clk_get(dev, "sci_ick");
2478 if (PTR_ERR(clk) == -EPROBE_DEFER)
2479 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002480
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002481 if (!IS_ERR(clk))
2482 goto found;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002483
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002484 /* SH has historically named the clock "sci_fck". */
2485 clk = devm_clk_get(dev, "sci_fck");
2486 if (!IS_ERR(clk))
2487 goto found;
2488
2489 /*
2490 * Not all SH platforms declare a clock lookup entry
2491 * for SCI devices, in which case we need to get the
2492 * global "peripheral_clk" clock.
2493 */
2494 clk = devm_clk_get(dev, "peripheral_clk");
2495 if (!IS_ERR(clk))
2496 goto found;
2497
2498 dev_err(dev, "failed to get %s (%ld)\n", clk_names[i],
2499 PTR_ERR(clk));
2500 return PTR_ERR(clk);
2501 }
2502
2503found:
2504 if (IS_ERR(clk))
2505 dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i],
2506 PTR_ERR(clk));
2507 else
2508 dev_dbg(dev, "clk %s is %pC rate %pCr\n", clk_names[i],
2509 clk, clk);
2510 sci_port->clks[i] = IS_ERR(clk) ? NULL : clk;
2511 }
2512 return 0;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002513}
2514
Bill Pemberton9671f092012-11-19 13:21:50 -05002515static int sci_init_single(struct platform_device *dev,
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002516 struct sci_port *sci_port, unsigned int index,
2517 struct plat_sci_port *p, bool early)
Paul Mundte108b2c2006-09-27 16:32:13 +09002518{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002519 struct uart_port *port = &sci_port->port;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002520 const struct resource *res;
2521 unsigned int i;
Paul Mundt3127c6b2011-06-28 13:44:37 +09002522 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002523
Paul Mundt50f09592011-12-02 20:09:48 +09002524 sci_port->cfg = p;
2525
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002526 port->ops = &sci_uart_ops;
2527 port->iotype = UPIO_MEM;
2528 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09002529
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002530 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2531 if (res == NULL)
2532 return -ENOMEM;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002533
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002534 port->mapbase = res->start;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002535 sci_port->reg_size = resource_size(res);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002536
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002537 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2538 sci_port->irqs[i] = platform_get_irq(dev, i);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002539
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002540 /* The SCI generates several interrupts. They can be muxed together or
2541 * connected to different interrupt lines. In the muxed case only one
2542 * interrupt resource is specified. In the non-muxed case three or four
2543 * interrupt resources are specified, as the BRI interrupt is optional.
2544 */
2545 if (sci_port->irqs[0] < 0)
2546 return -ENXIO;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002547
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002548 if (sci_port->irqs[1] < 0) {
2549 sci_port->irqs[1] = sci_port->irqs[0];
2550 sci_port->irqs[2] = sci_port->irqs[0];
2551 sci_port->irqs[3] = sci_port->irqs[0];
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002552 }
2553
Paul Mundt3127c6b2011-06-28 13:44:37 +09002554 if (p->regtype == SCIx_PROBE_REGTYPE) {
2555 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02002556 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09002557 return ret;
2558 }
Paul Mundt61a69762011-06-14 12:40:19 +09002559
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002560 switch (p->type) {
2561 case PORT_SCIFB:
2562 port->fifosize = 256;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002563 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002564 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002565 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002566 break;
2567 case PORT_HSCIF:
2568 port->fifosize = 128;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002569 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002570 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002571 sci_port->sampling_rate = 0;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002572 break;
2573 case PORT_SCIFA:
2574 port->fifosize = 64;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002575 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002576 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002577 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002578 break;
2579 case PORT_SCIF:
2580 port->fifosize = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002581 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002582 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002583 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002584 sci_port->sampling_rate = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002585 } else {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002586 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002587 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002588 sci_port->sampling_rate = 32;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002589 }
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002590 break;
2591 default:
2592 port->fifosize = 1;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002593 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002594 sci_port->overrun_mask = SCI_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002595 sci_port->sampling_rate = 32;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002596 break;
2597 }
2598
Laurent Pinchart878fbb912013-12-06 10:59:51 +01002599 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
2600 * match the SoC datasheet, this should be investigated. Let platform
2601 * data override the sampling rate for now.
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002602 */
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002603 if (p->sampling_rate)
2604 sci_port->sampling_rate = p->sampling_rate;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002605
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002606 if (!early) {
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002607 ret = sci_init_clocks(sci_port, &dev->dev);
2608 if (ret < 0)
2609 return ret;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002610
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002611 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002612
2613 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002614 }
Paul Mundte108b2c2006-09-27 16:32:13 +09002615
Magnus Damm7ed7e072009-01-21 15:14:14 +00002616 sci_port->break_timer.data = (unsigned long)sci_port;
2617 sci_port->break_timer.function = sci_break_timer;
2618 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09002619
Paul Mundtdebf9502011-06-08 18:19:37 +09002620 /*
2621 * Establish some sensible defaults for the error detection.
2622 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002623 if (p->type == PORT_SCI) {
2624 sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
2625 sci_port->error_clear = SCI_ERROR_CLEAR;
2626 } else {
2627 sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
2628 sci_port->error_clear = SCIF_ERROR_CLEAR;
2629 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002630
2631 /*
Laurent Pinchart3ae988d2013-12-06 10:59:17 +01002632 * Make the error mask inclusive of overrun detection, if
2633 * supported.
2634 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002635 if (sci_port->overrun_reg == SCxSR) {
Geert Uytterhoevenafd66db2015-04-30 18:21:33 +02002636 sci_port->error_mask |= sci_port->overrun_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002637 sci_port->error_clear &= ~sci_port->overrun_mask;
2638 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002639
Paul Mundtce6738b2011-01-19 15:24:40 +09002640 port->type = p->type;
Laurent Pinchartb6e4a3f2013-12-06 10:59:14 +01002641 port->flags = UPF_FIXED_PORT | p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09002642 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09002643
2644 /*
Paul Mundt61a69762011-06-14 12:40:19 +09002645 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09002646 * for the multi-IRQ ports, which is where we are primarily
2647 * concerned with the shutdown path synchronization.
2648 *
2649 * For the muxed case there's nothing more to do.
2650 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002651 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08002652 port->irqflags = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002653
Paul Mundt61a69762011-06-14 12:40:19 +09002654 port->serial_in = sci_serial_in;
2655 port->serial_out = sci_serial_out;
2656
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02002657 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2658 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2659 p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002660
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002661 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002662}
2663
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002664static void sci_cleanup_single(struct sci_port *port)
2665{
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002666 pm_runtime_disable(port->port.dev);
2667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002670static void serial_console_putchar(struct uart_port *port, int ch)
2671{
2672 sci_poll_put_char(port, ch);
2673}
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675/*
2676 * Print a string to the serial port trying not to disturb
2677 * any possible real use of the port...
2678 */
2679static void serial_console_write(struct console *co, const char *s,
2680 unsigned count)
2681{
Paul Mundt906b17d2011-01-21 16:19:53 +09002682 struct sci_port *sci_port = &sci_ports[co->index];
2683 struct uart_port *port = &sci_port->port;
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002684 unsigned short bits, ctrl, ctrl_temp;
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002685 unsigned long flags;
2686 int locked = 1;
2687
2688 local_irq_save(flags);
2689 if (port->sysrq)
2690 locked = 0;
2691 else if (oops_in_progress)
2692 locked = spin_trylock(&port->lock);
2693 else
2694 spin_lock(&port->lock);
2695
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002696 /* first save SCSCR then disable interrupts, keep clock source */
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002697 ctrl = serial_port_in(port, SCSCR);
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002698 ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
2699 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
2700 serial_port_out(port, SCSCR, ctrl_temp);
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002701
Magnus Damm501b8252009-01-21 15:14:30 +00002702 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002703
2704 /* wait until fifo is empty and last bit has been transmitted */
2705 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
Paul Mundtb12bb292012-03-30 19:50:15 +09002706 while ((serial_port_in(port, SCxSR) & bits) != bits)
Magnus Damm973e5d52009-02-24 15:57:12 +09002707 cpu_relax();
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002708
2709 /* restore the SCSCR */
2710 serial_port_out(port, SCSCR, ctrl);
2711
2712 if (locked)
2713 spin_unlock(&port->lock);
2714 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Bill Pemberton9671f092012-11-19 13:21:50 -05002717static int serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002719 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct uart_port *port;
2721 int baud = 115200;
2722 int bits = 8;
2723 int parity = 'n';
2724 int flow = 'n';
2725 int ret;
2726
Paul Mundte108b2c2006-09-27 16:32:13 +09002727 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002728 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002729 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002730 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002731 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002732
Paul Mundt906b17d2011-01-21 16:19:53 +09002733 sci_port = &sci_ports[co->index];
2734 port = &sci_port->port;
2735
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002736 /*
2737 * Refuse to handle uninitialized ports.
2738 */
2739 if (!port->ops)
2740 return -ENODEV;
2741
Paul Mundtf6e94952011-01-21 15:25:36 +09002742 ret = sci_remap_port(port);
2743 if (unlikely(ret != 0))
2744 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (options)
2747 uart_parse_options(options, &baud, &parity, &bits, &flow);
2748
Paul Mundtab7cfb52011-06-01 14:47:42 +09002749 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
2752static struct console serial_console = {
2753 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002754 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 .write = serial_console_write,
2756 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002757 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002759 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760};
2761
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002762static struct console early_serial_console = {
2763 .name = "early_ttySC",
2764 .write = serial_console_write,
2765 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002766 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002767};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002768
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002769static char early_serial_buf[32];
2770
Bill Pemberton9671f092012-11-19 13:21:50 -05002771static int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002772{
Jingoo Han574de552013-07-30 17:06:57 +09002773 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002774
2775 if (early_serial_console.data)
2776 return -EEXIST;
2777
2778 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002779
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002780 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002781
2782 serial_console_setup(&early_serial_console, early_serial_buf);
2783
2784 if (!strstr(early_serial_buf, "keep"))
2785 early_serial_console.flags |= CON_BOOT;
2786
2787 register_console(&early_serial_console);
2788 return 0;
2789}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002790
2791#define SCI_CONSOLE (&serial_console)
2792
Paul Mundtecdf8a42011-01-21 00:05:48 +09002793#else
Bill Pemberton9671f092012-11-19 13:21:50 -05002794static inline int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002795{
2796 return -EINVAL;
2797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002799#define SCI_CONSOLE NULL
2800
2801#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002803static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805static struct uart_driver sci_uart_driver = {
2806 .owner = THIS_MODULE,
2807 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .dev_name = "ttySC",
2809 .major = SCI_MAJOR,
2810 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002811 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 .cons = SCI_CONSOLE,
2813};
2814
Paul Mundt54507f62009-05-08 23:48:33 +09002815static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002816{
Paul Mundtd535a232011-01-19 17:19:35 +09002817 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002818
Paul Mundtd535a232011-01-19 17:19:35 +09002819 cpufreq_unregister_notifier(&port->freq_transition,
2820 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002821
Paul Mundtd535a232011-01-19 17:19:35 +09002822 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002823
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002824 sci_cleanup_single(port);
Paul Mundtd535a232011-01-19 17:19:35 +09002825
Magnus Damme552de22009-01-21 15:13:42 +00002826 return 0;
2827}
2828
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002829
2830#define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
2831#define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
2832#define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002833
2834static const struct of_device_id of_sci_match[] = {
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002835 /* SoC-specific types */
2836 {
2837 .compatible = "renesas,scif-r7s72100",
2838 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
2839 },
Geert Uytterhoeven9ed44bb2015-11-10 18:57:23 +01002840 /* Family-specific types */
2841 {
2842 .compatible = "renesas,rcar-gen1-scif",
2843 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2844 }, {
2845 .compatible = "renesas,rcar-gen2-scif",
2846 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2847 }, {
2848 .compatible = "renesas,rcar-gen3-scif",
2849 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2850 },
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002851 /* Generic types */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002852 {
2853 .compatible = "renesas,scif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002854 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002855 }, {
2856 .compatible = "renesas,scifa",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002857 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002858 }, {
2859 .compatible = "renesas,scifb",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002860 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002861 }, {
2862 .compatible = "renesas,hscif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002863 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002864 }, {
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002865 .compatible = "renesas,sci",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002866 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002867 }, {
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002868 /* Terminator */
2869 },
2870};
2871MODULE_DEVICE_TABLE(of, of_sci_match);
2872
2873static struct plat_sci_port *
2874sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
2875{
2876 struct device_node *np = pdev->dev.of_node;
2877 const struct of_device_id *match;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002878 struct plat_sci_port *p;
2879 int id;
2880
2881 if (!IS_ENABLED(CONFIG_OF) || !np)
2882 return NULL;
2883
Geert Uytterhoeven495bb472015-12-10 16:02:17 +01002884 match = of_match_node(of_sci_match, np);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002885 if (!match)
2886 return NULL;
2887
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002888 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02002889 if (!p)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002890 return NULL;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002891
Geert Uytterhoeven2095fc72015-11-12 13:39:49 +01002892 /* Get the line number from the aliases node. */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002893 id = of_alias_get_id(np, "serial");
2894 if (id < 0) {
2895 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
2896 return NULL;
2897 }
2898
2899 *dev_id = id;
2900
2901 p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002902 p->type = SCI_OF_TYPE(match->data);
2903 p->regtype = SCI_OF_REGTYPE(match->data);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002904 p->scscr = SCSCR_RE | SCSCR_TE;
2905
2906 return p;
2907}
2908
Bill Pemberton9671f092012-11-19 13:21:50 -05002909static int sci_probe_single(struct platform_device *dev,
Magnus Damm0ee70712009-01-21 15:13:50 +00002910 unsigned int index,
2911 struct plat_sci_port *p,
2912 struct sci_port *sciport)
2913{
Magnus Damm0ee70712009-01-21 15:13:50 +00002914 int ret;
2915
2916 /* Sanity check */
2917 if (unlikely(index >= SCI_NPORTS)) {
Joe Perches9b971cd2014-03-11 10:10:46 -07002918 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
Magnus Damm0ee70712009-01-21 15:13:50 +00002919 index+1, SCI_NPORTS);
Joe Perches9b971cd2014-03-11 10:10:46 -07002920 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
Laurent Pinchartb6c5ef62012-06-13 00:28:24 +02002921 return -EINVAL;
Magnus Damm0ee70712009-01-21 15:13:50 +00002922 }
2923
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002924 ret = sci_init_single(dev, sciport, index, p, false);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002925 if (ret)
2926 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002927
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002928 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
2929 if (ret) {
2930 sci_cleanup_single(sciport);
2931 return ret;
2932 }
2933
2934 return 0;
Magnus Damm0ee70712009-01-21 15:13:50 +00002935}
2936
Bill Pemberton9671f092012-11-19 13:21:50 -05002937static int sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002939 struct plat_sci_port *p;
2940 struct sci_port *sp;
2941 unsigned int dev_id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002942 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002943
Paul Mundtecdf8a42011-01-21 00:05:48 +09002944 /*
2945 * If we've come here via earlyprintk initialization, head off to
2946 * the special early probe. We don't have sufficient device state
2947 * to make it beyond this yet.
2948 */
2949 if (is_early_platform_device(dev))
2950 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002951
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002952 if (dev->dev.of_node) {
2953 p = sci_parse_dt(dev, &dev_id);
2954 if (p == NULL)
2955 return -EINVAL;
2956 } else {
2957 p = dev->dev.platform_data;
2958 if (p == NULL) {
2959 dev_err(&dev->dev, "no platform data supplied\n");
2960 return -EINVAL;
2961 }
2962
2963 dev_id = dev->id;
2964 }
2965
2966 sp = &sci_ports[dev_id];
Paul Mundtd535a232011-01-19 17:19:35 +09002967 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002968
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002969 ret = sci_probe_single(dev, dev_id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002970 if (ret)
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002971 return ret;
Magnus Damme552de22009-01-21 15:13:42 +00002972
Paul Mundtd535a232011-01-19 17:19:35 +09002973 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Paul Mundtd535a232011-01-19 17:19:35 +09002975 ret = cpufreq_register_notifier(&sp->freq_transition,
2976 CPUFREQ_TRANSITION_NOTIFIER);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002977 if (unlikely(ret < 0)) {
Geert Uytterhoevenbf13c9a2014-02-28 14:21:33 +01002978 uart_remove_one_port(&sci_uart_driver, &sp->port);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002979 sci_cleanup_single(sp);
2980 return ret;
2981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983#ifdef CONFIG_SH_STANDARD_BIOS
2984 sh_bios_gdb_detach();
2985#endif
2986
Paul Mundte108b2c2006-09-27 16:32:13 +09002987 return 0;
2988}
2989
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002990static __maybe_unused int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002991{
Paul Mundtd535a232011-01-19 17:19:35 +09002992 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002993
Paul Mundtd535a232011-01-19 17:19:35 +09002994 if (sport)
2995 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002996
2997 return 0;
2998}
2999
Sergei Shtylyovcb876342015-01-16 13:56:02 -08003000static __maybe_unused int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09003001{
Paul Mundtd535a232011-01-19 17:19:35 +09003002 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09003003
Paul Mundtd535a232011-01-19 17:19:35 +09003004 if (sport)
3005 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09003006
3007 return 0;
3008}
3009
Sergei Shtylyovcb876342015-01-16 13:56:02 -08003010static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
Paul Mundt6daa79b2009-06-15 07:07:38 +09003011
Paul Mundte108b2c2006-09-27 16:32:13 +09003012static struct platform_driver sci_driver = {
3013 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01003014 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09003015 .driver = {
3016 .name = "sh-sci",
Paul Mundt6daa79b2009-06-15 07:07:38 +09003017 .pm = &sci_dev_pm_ops,
Bastian Hecht20bdcab2013-12-06 10:59:54 +01003018 .of_match_table = of_match_ptr(of_sci_match),
Paul Mundte108b2c2006-09-27 16:32:13 +09003019 },
3020};
3021
3022static int __init sci_init(void)
3023{
3024 int ret;
3025
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01003026 pr_info("%s\n", banner);
Paul Mundte108b2c2006-09-27 16:32:13 +09003027
Paul Mundte108b2c2006-09-27 16:32:13 +09003028 ret = uart_register_driver(&sci_uart_driver);
3029 if (likely(ret == 0)) {
3030 ret = platform_driver_register(&sci_driver);
3031 if (unlikely(ret))
3032 uart_unregister_driver(&sci_uart_driver);
3033 }
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return ret;
3036}
3037
3038static void __exit sci_exit(void)
3039{
Paul Mundte108b2c2006-09-27 16:32:13 +09003040 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 uart_unregister_driver(&sci_uart_driver);
3042}
3043
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00003044#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
3045early_platform_init_buffer("earlyprintk", &sci_driver,
3046 early_serial_buf, ARRAY_SIZE(early_serial_buf));
3047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048module_init(sci_init);
3049module_exit(sci_exit);
3050
Paul Mundte108b2c2006-09-27 16:32:13 +09003051MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07003052MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09003053MODULE_AUTHOR("Paul Mundt");
Ulrich Hechtf303b362013-05-31 17:57:01 +02003054MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");