blob: 229162481fd67b9d3522ed3a341609b866cbc3ac [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 Uytterhoevenf4998e52015-10-26 09:58:16 +010083 SCI_NUM_CLKS
84};
85
Paul Mundte108b2c2006-09-27 16:32:13 +090086struct sci_port {
87 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Paul Mundtce6738b2011-01-19 15:24:40 +090089 /* Platform configuration */
90 struct plat_sci_port *cfg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +020091 unsigned int overrun_reg;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +020092 unsigned int overrun_mask;
Laurent Pinchart3ae988d2013-12-06 10:59:17 +010093 unsigned int error_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +020094 unsigned int error_clear;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +010095 unsigned int sampling_rate;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +090096 resource_size_t reg_size;
Paul Mundte108b2c2006-09-27 16:32:13 +090097
Paul Mundte108b2c2006-09-27 16:32:13 +090098 /* Break timer */
99 struct timer_list break_timer;
100 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +0900101
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100102 /* Clocks */
103 struct clk *clks[SCI_NUM_CLKS];
104 unsigned long clk_rates[SCI_NUM_CLKS];
Paul Mundtedad1f22009-11-25 16:23:35 +0900105
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +0100106 int irqs[SCIx_NR_IRQS];
Paul Mundt9174fc82011-06-28 15:25:36 +0900107 char *irqstr[SCIx_NR_IRQS];
108
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900109 struct dma_chan *chan_tx;
110 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +0900111
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900112#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900113 dma_cookie_t cookie_tx;
114 dma_cookie_t cookie_rx[2];
115 dma_cookie_t active_rx;
Geert Uytterhoeven79904422015-08-21 20:02:42 +0200116 dma_addr_t tx_dma_addr;
117 unsigned int tx_dma_len;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900118 struct scatterlist sg_rx[2];
Yoshihiro Shimoda7b39d902015-08-21 20:02:54 +0200119 void *rx_buf[2];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900120 size_t buf_len_rx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900121 struct work_struct work_tx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900122 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000123 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900124#endif
Magnus Damme552de22009-01-21 15:13:42 +0000125
Paul Mundtd535a232011-01-19 17:19:35 +0900126 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900127};
128
Paul Mundte108b2c2006-09-27 16:32:13 +0900129#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
130
131static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct uart_driver sci_uart_driver;
133
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900134static inline struct sci_port *
135to_sci_port(struct uart_port *uart)
136{
137 return container_of(uart, struct sci_port, port);
138}
139
Paul Mundt61a69762011-06-14 12:40:19 +0900140struct plat_sci_reg {
141 u8 offset, size;
142};
143
144/* Helper for invalidating specific entries of an inherited map. */
145#define sci_reg_invalid { .offset = 0, .size = 0 }
146
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200147static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
Paul Mundt61a69762011-06-14 12:40:19 +0900148 [SCIx_PROBE_REGTYPE] = {
149 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
150 },
151
152 /*
153 * Common SCI definitions, dependent on the port's regshift
154 * value.
155 */
156 [SCIx_SCI_REGTYPE] = {
157 [SCSMR] = { 0x00, 8 },
158 [SCBRR] = { 0x01, 8 },
159 [SCSCR] = { 0x02, 8 },
160 [SCxTDR] = { 0x03, 8 },
161 [SCxSR] = { 0x04, 8 },
162 [SCxRDR] = { 0x05, 8 },
163 [SCFCR] = sci_reg_invalid,
164 [SCFDR] = sci_reg_invalid,
165 [SCTFDR] = sci_reg_invalid,
166 [SCRFDR] = sci_reg_invalid,
167 [SCSPTR] = sci_reg_invalid,
168 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200169 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200170 [SCPCR] = sci_reg_invalid,
171 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100172 [SCDL] = sci_reg_invalid,
173 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900174 },
175
176 /*
177 * Common definitions for legacy IrDA ports, dependent on
178 * regshift value.
179 */
180 [SCIx_IRDA_REGTYPE] = {
181 [SCSMR] = { 0x00, 8 },
182 [SCBRR] = { 0x01, 8 },
183 [SCSCR] = { 0x02, 8 },
184 [SCxTDR] = { 0x03, 8 },
185 [SCxSR] = { 0x04, 8 },
186 [SCxRDR] = { 0x05, 8 },
187 [SCFCR] = { 0x06, 8 },
188 [SCFDR] = { 0x07, 16 },
189 [SCTFDR] = sci_reg_invalid,
190 [SCRFDR] = sci_reg_invalid,
191 [SCSPTR] = sci_reg_invalid,
192 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200193 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200194 [SCPCR] = sci_reg_invalid,
195 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100196 [SCDL] = sci_reg_invalid,
197 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900198 },
199
200 /*
201 * Common SCIFA definitions.
202 */
203 [SCIx_SCIFA_REGTYPE] = {
204 [SCSMR] = { 0x00, 16 },
205 [SCBRR] = { 0x04, 8 },
206 [SCSCR] = { 0x08, 16 },
207 [SCxTDR] = { 0x20, 8 },
208 [SCxSR] = { 0x14, 16 },
209 [SCxRDR] = { 0x24, 8 },
210 [SCFCR] = { 0x18, 16 },
211 [SCFDR] = { 0x1c, 16 },
212 [SCTFDR] = sci_reg_invalid,
213 [SCRFDR] = sci_reg_invalid,
214 [SCSPTR] = sci_reg_invalid,
215 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200216 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200217 [SCPCR] = { 0x30, 16 },
218 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100219 [SCDL] = sci_reg_invalid,
220 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900221 },
222
223 /*
224 * Common SCIFB definitions.
225 */
226 [SCIx_SCIFB_REGTYPE] = {
227 [SCSMR] = { 0x00, 16 },
228 [SCBRR] = { 0x04, 8 },
229 [SCSCR] = { 0x08, 16 },
230 [SCxTDR] = { 0x40, 8 },
231 [SCxSR] = { 0x14, 16 },
232 [SCxRDR] = { 0x60, 8 },
233 [SCFCR] = { 0x18, 16 },
Takashi Yoshii8c66d6d2012-11-16 10:53:31 +0900234 [SCFDR] = sci_reg_invalid,
235 [SCTFDR] = { 0x38, 16 },
236 [SCRFDR] = { 0x3c, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900237 [SCSPTR] = sci_reg_invalid,
238 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200239 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200240 [SCPCR] = { 0x30, 16 },
241 [SCPDR] = { 0x34, 16 },
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100242 [SCDL] = sci_reg_invalid,
243 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900244 },
245
246 /*
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100247 * Common SH-2(A) SCIF definitions for ports with FIFO data
248 * count registers.
249 */
250 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
251 [SCSMR] = { 0x00, 16 },
252 [SCBRR] = { 0x04, 8 },
253 [SCSCR] = { 0x08, 16 },
254 [SCxTDR] = { 0x0c, 8 },
255 [SCxSR] = { 0x10, 16 },
256 [SCxRDR] = { 0x14, 8 },
257 [SCFCR] = { 0x18, 16 },
258 [SCFDR] = { 0x1c, 16 },
259 [SCTFDR] = sci_reg_invalid,
260 [SCRFDR] = sci_reg_invalid,
261 [SCSPTR] = { 0x20, 16 },
262 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200263 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200264 [SCPCR] = sci_reg_invalid,
265 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100266 [SCDL] = sci_reg_invalid,
267 [SCCKS] = sci_reg_invalid,
Phil Edworthy3af1f8a2011-10-03 15:16:47 +0100268 },
269
270 /*
Paul Mundt61a69762011-06-14 12:40:19 +0900271 * Common SH-3 SCIF definitions.
272 */
273 [SCIx_SH3_SCIF_REGTYPE] = {
274 [SCSMR] = { 0x00, 8 },
275 [SCBRR] = { 0x02, 8 },
276 [SCSCR] = { 0x04, 8 },
277 [SCxTDR] = { 0x06, 8 },
278 [SCxSR] = { 0x08, 16 },
279 [SCxRDR] = { 0x0a, 8 },
280 [SCFCR] = { 0x0c, 8 },
281 [SCFDR] = { 0x0e, 16 },
282 [SCTFDR] = sci_reg_invalid,
283 [SCRFDR] = sci_reg_invalid,
284 [SCSPTR] = sci_reg_invalid,
285 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200286 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200287 [SCPCR] = sci_reg_invalid,
288 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100289 [SCDL] = sci_reg_invalid,
290 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900291 },
292
293 /*
294 * Common SH-4(A) SCIF(B) definitions.
295 */
296 [SCIx_SH4_SCIF_REGTYPE] = {
297 [SCSMR] = { 0x00, 16 },
298 [SCBRR] = { 0x04, 8 },
299 [SCSCR] = { 0x08, 16 },
300 [SCxTDR] = { 0x0c, 8 },
301 [SCxSR] = { 0x10, 16 },
302 [SCxRDR] = { 0x14, 8 },
303 [SCFCR] = { 0x18, 16 },
304 [SCFDR] = { 0x1c, 16 },
305 [SCTFDR] = sci_reg_invalid,
306 [SCRFDR] = sci_reg_invalid,
307 [SCSPTR] = { 0x20, 16 },
308 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200309 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200310 [SCPCR] = sci_reg_invalid,
311 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100312 [SCDL] = sci_reg_invalid,
313 [SCCKS] = sci_reg_invalid,
314 },
315
316 /*
317 * Common SCIF definitions for ports with a Baud Rate Generator for
318 * External Clock (BRG).
319 */
320 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
321 [SCSMR] = { 0x00, 16 },
322 [SCBRR] = { 0x04, 8 },
323 [SCSCR] = { 0x08, 16 },
324 [SCxTDR] = { 0x0c, 8 },
325 [SCxSR] = { 0x10, 16 },
326 [SCxRDR] = { 0x14, 8 },
327 [SCFCR] = { 0x18, 16 },
328 [SCFDR] = { 0x1c, 16 },
329 [SCTFDR] = sci_reg_invalid,
330 [SCRFDR] = sci_reg_invalid,
331 [SCSPTR] = { 0x20, 16 },
332 [SCLSR] = { 0x24, 16 },
333 [HSSRR] = sci_reg_invalid,
334 [SCPCR] = sci_reg_invalid,
335 [SCPDR] = sci_reg_invalid,
336 [SCDL] = { 0x30, 16 },
337 [SCCKS] = { 0x34, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200338 },
339
340 /*
341 * Common HSCIF definitions.
342 */
343 [SCIx_HSCIF_REGTYPE] = {
344 [SCSMR] = { 0x00, 16 },
345 [SCBRR] = { 0x04, 8 },
346 [SCSCR] = { 0x08, 16 },
347 [SCxTDR] = { 0x0c, 8 },
348 [SCxSR] = { 0x10, 16 },
349 [SCxRDR] = { 0x14, 8 },
350 [SCFCR] = { 0x18, 16 },
351 [SCFDR] = { 0x1c, 16 },
352 [SCTFDR] = sci_reg_invalid,
353 [SCRFDR] = sci_reg_invalid,
354 [SCSPTR] = { 0x20, 16 },
355 [SCLSR] = { 0x24, 16 },
356 [HSSRR] = { 0x40, 16 },
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200357 [SCPCR] = sci_reg_invalid,
358 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100359 [SCDL] = { 0x30, 16 },
360 [SCCKS] = { 0x34, 16 },
Paul Mundt61a69762011-06-14 12:40:19 +0900361 },
362
363 /*
364 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
365 * register.
366 */
367 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
368 [SCSMR] = { 0x00, 16 },
369 [SCBRR] = { 0x04, 8 },
370 [SCSCR] = { 0x08, 16 },
371 [SCxTDR] = { 0x0c, 8 },
372 [SCxSR] = { 0x10, 16 },
373 [SCxRDR] = { 0x14, 8 },
374 [SCFCR] = { 0x18, 16 },
375 [SCFDR] = { 0x1c, 16 },
376 [SCTFDR] = sci_reg_invalid,
377 [SCRFDR] = sci_reg_invalid,
378 [SCSPTR] = sci_reg_invalid,
379 [SCLSR] = { 0x24, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200380 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200381 [SCPCR] = sci_reg_invalid,
382 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100383 [SCDL] = sci_reg_invalid,
384 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900385 },
386
387 /*
388 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
389 * count registers.
390 */
391 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
392 [SCSMR] = { 0x00, 16 },
393 [SCBRR] = { 0x04, 8 },
394 [SCSCR] = { 0x08, 16 },
395 [SCxTDR] = { 0x0c, 8 },
396 [SCxSR] = { 0x10, 16 },
397 [SCxRDR] = { 0x14, 8 },
398 [SCFCR] = { 0x18, 16 },
399 [SCFDR] = { 0x1c, 16 },
400 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
401 [SCRFDR] = { 0x20, 16 },
402 [SCSPTR] = { 0x24, 16 },
403 [SCLSR] = { 0x28, 16 },
Ulrich Hechtf303b362013-05-31 17:57:01 +0200404 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200405 [SCPCR] = sci_reg_invalid,
406 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100407 [SCDL] = sci_reg_invalid,
408 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900409 },
410
411 /*
412 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
413 * registers.
414 */
415 [SCIx_SH7705_SCIF_REGTYPE] = {
416 [SCSMR] = { 0x00, 16 },
417 [SCBRR] = { 0x04, 8 },
418 [SCSCR] = { 0x08, 16 },
419 [SCxTDR] = { 0x20, 8 },
420 [SCxSR] = { 0x14, 16 },
421 [SCxRDR] = { 0x24, 8 },
422 [SCFCR] = { 0x18, 16 },
423 [SCFDR] = { 0x1c, 16 },
424 [SCTFDR] = sci_reg_invalid,
425 [SCRFDR] = sci_reg_invalid,
426 [SCSPTR] = sci_reg_invalid,
427 [SCLSR] = sci_reg_invalid,
Ulrich Hechtf303b362013-05-31 17:57:01 +0200428 [HSSRR] = sci_reg_invalid,
Geert Uytterhoevenc097abc2015-04-30 18:21:27 +0200429 [SCPCR] = sci_reg_invalid,
430 [SCPDR] = sci_reg_invalid,
Geert Uytterhoevenb8bbd6b2015-11-12 13:36:06 +0100431 [SCDL] = sci_reg_invalid,
432 [SCCKS] = sci_reg_invalid,
Paul Mundt61a69762011-06-14 12:40:19 +0900433 },
434};
435
Paul Mundt72b294c2011-06-14 17:38:19 +0900436#define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
437
Paul Mundt61a69762011-06-14 12:40:19 +0900438/*
439 * The "offset" here is rather misleading, in that it refers to an enum
440 * value relative to the port mapping rather than the fixed offset
441 * itself, which needs to be manually retrieved from the platform's
442 * register map for the given port.
443 */
444static unsigned int sci_serial_in(struct uart_port *p, int offset)
445{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200446 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900447
448 if (reg->size == 8)
449 return ioread8(p->membase + (reg->offset << p->regshift));
450 else if (reg->size == 16)
451 return ioread16(p->membase + (reg->offset << p->regshift));
452 else
453 WARN(1, "Invalid register access\n");
454
455 return 0;
456}
457
458static void sci_serial_out(struct uart_port *p, int offset, int value)
459{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200460 const struct plat_sci_reg *reg = sci_getreg(p, offset);
Paul Mundt61a69762011-06-14 12:40:19 +0900461
462 if (reg->size == 8)
463 iowrite8(value, p->membase + (reg->offset << p->regshift));
464 else if (reg->size == 16)
465 iowrite16(value, p->membase + (reg->offset << p->regshift));
466 else
467 WARN(1, "Invalid register access\n");
468}
469
Paul Mundt61a69762011-06-14 12:40:19 +0900470static int sci_probe_regmap(struct plat_sci_port *cfg)
471{
472 switch (cfg->type) {
473 case PORT_SCI:
474 cfg->regtype = SCIx_SCI_REGTYPE;
475 break;
476 case PORT_IRDA:
477 cfg->regtype = SCIx_IRDA_REGTYPE;
478 break;
479 case PORT_SCIFA:
480 cfg->regtype = SCIx_SCIFA_REGTYPE;
481 break;
482 case PORT_SCIFB:
483 cfg->regtype = SCIx_SCIFB_REGTYPE;
484 break;
485 case PORT_SCIF:
486 /*
487 * The SH-4 is a bit of a misnomer here, although that's
488 * where this particular port layout originated. This
489 * configuration (or some slight variation thereof)
490 * remains the dominant model for all SCIFs.
491 */
492 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
493 break;
Ulrich Hechtf303b362013-05-31 17:57:01 +0200494 case PORT_HSCIF:
495 cfg->regtype = SCIx_HSCIF_REGTYPE;
496 break;
Paul Mundt61a69762011-06-14 12:40:19 +0900497 default:
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +0100498 pr_err("Can't probe register map for given port\n");
Paul Mundt61a69762011-06-14 12:40:19 +0900499 return -EINVAL;
500 }
501
502 return 0;
503}
504
Paul Mundt23241d42011-06-28 13:55:31 +0900505static void sci_port_enable(struct sci_port *sci_port)
506{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100507 unsigned int i;
508
Paul Mundt23241d42011-06-28 13:55:31 +0900509 if (!sci_port->port.dev)
510 return;
511
512 pm_runtime_get_sync(sci_port->port.dev);
513
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100514 for (i = 0; i < SCI_NUM_CLKS; i++) {
515 clk_prepare_enable(sci_port->clks[i]);
516 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
517 }
518 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
Paul Mundt23241d42011-06-28 13:55:31 +0900519}
520
521static void sci_port_disable(struct sci_port *sci_port)
522{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100523 unsigned int i;
524
Paul Mundt23241d42011-06-28 13:55:31 +0900525 if (!sci_port->port.dev)
526 return;
527
Laurent Pinchartcaec7032013-11-28 18:11:45 +0100528 /* Cancel the break timer to ensure that the timer handler will not try
529 * to access the hardware with clocks and power disabled. Reset the
530 * break flag to make the break debouncing state machine ready for the
531 * next break.
532 */
533 del_timer_sync(&sci_port->break_timer);
534 sci_port->break_flag = 0;
535
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +0100536 for (i = SCI_NUM_CLKS; i-- > 0; )
537 clk_disable_unprepare(sci_port->clks[i]);
Paul Mundt23241d42011-06-28 13:55:31 +0900538
539 pm_runtime_put_sync(sci_port->port.dev);
540}
541
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +0200542static inline unsigned long port_rx_irq_mask(struct uart_port *port)
543{
544 /*
545 * Not all ports (such as SCIFA) will support REIE. Rather than
546 * special-casing the port type, we check the port initialization
547 * IRQ enable mask to see whether the IRQ is desired at all. If
548 * it's unset, it's logically inferred that there's no point in
549 * testing for it.
550 */
551 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
552}
553
554static void sci_start_tx(struct uart_port *port)
555{
556 struct sci_port *s = to_sci_port(port);
557 unsigned short ctrl;
558
559#ifdef CONFIG_SERIAL_SH_SCI_DMA
560 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
561 u16 new, scr = serial_port_in(port, SCSCR);
562 if (s->chan_tx)
563 new = scr | SCSCR_TDRQE;
564 else
565 new = scr & ~SCSCR_TDRQE;
566 if (new != scr)
567 serial_port_out(port, SCSCR, new);
568 }
569
570 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
571 dma_submit_error(s->cookie_tx)) {
572 s->cookie_tx = 0;
573 schedule_work(&s->work_tx);
574 }
575#endif
576
577 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
578 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
579 ctrl = serial_port_in(port, SCSCR);
580 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
581 }
582}
583
584static void sci_stop_tx(struct uart_port *port)
585{
586 unsigned short ctrl;
587
588 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
589 ctrl = serial_port_in(port, SCSCR);
590
591 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
592 ctrl &= ~SCSCR_TDRQE;
593
594 ctrl &= ~SCSCR_TIE;
595
596 serial_port_out(port, SCSCR, ctrl);
597}
598
599static void sci_start_rx(struct uart_port *port)
600{
601 unsigned short ctrl;
602
603 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
604
605 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
606 ctrl &= ~SCSCR_RDRQE;
607
608 serial_port_out(port, SCSCR, ctrl);
609}
610
611static void sci_stop_rx(struct uart_port *port)
612{
613 unsigned short ctrl;
614
615 ctrl = serial_port_in(port, SCSCR);
616
617 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
618 ctrl &= ~SCSCR_RDRQE;
619
620 ctrl &= ~port_rx_irq_mask(port);
621
622 serial_port_out(port, SCSCR, ctrl);
623}
624
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200625static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
626{
627 if (port->type == PORT_SCI) {
628 /* Just store the mask */
629 serial_port_out(port, SCxSR, mask);
630 } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
631 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
632 /* Only clear the status bits we want to clear */
633 serial_port_out(port, SCxSR,
634 serial_port_in(port, SCxSR) & mask);
635 } else {
636 /* Store the mask, clear parity/framing errors */
637 serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
638 }
639}
640
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900641#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900642
643#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900644static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned short status;
647 int c;
648
Paul Mundte108b2c2006-09-27 16:32:13 +0900649 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900650 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (status & SCxSR_ERRORS(port)) {
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200652 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500655 break;
656 } while (1);
657
658 if (!(status & SCxSR_RDxF(port)))
659 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900660
Paul Mundtb12bb292012-03-30 19:50:15 +0900661 c = serial_port_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900662
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900663 /* Dummy read */
Paul Mundtb12bb292012-03-30 19:50:15 +0900664 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200665 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return c;
668}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900671static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned short status;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 do {
Paul Mundtb12bb292012-03-30 19:50:15 +0900676 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 } while (!(status & SCxSR_TDxE(port)));
678
Paul Mundtb12bb292012-03-30 19:50:15 +0900679 serial_port_out(port, SCxTDR, c);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200680 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900682#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Paul Mundt61a69762011-06-14 12:40:19 +0900684static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900685{
Paul Mundt61a69762011-06-14 12:40:19 +0900686 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200687 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900688
Paul Mundt61a69762011-06-14 12:40:19 +0900689 /*
690 * Use port-specific handler if provided.
691 */
692 if (s->cfg->ops && s->cfg->ops->init_pins) {
693 s->cfg->ops->init_pins(port, cflag);
694 return;
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Paul Mundt61a69762011-06-14 12:40:19 +0900697 /*
698 * For the generic path SCSPTR is necessary. Bail out if that's
699 * unavailable, too.
700 */
701 if (!reg->size)
702 return;
Paul Mundtb7a76e42006-02-01 03:06:06 -0800703
Paul Mundtfaf02f82011-12-02 17:44:50 +0900704 if ((s->cfg->capabilities & SCIx_HAVE_RTSCTS) &&
705 ((!(cflag & CRTSCTS)))) {
706 unsigned short status;
707
Paul Mundtb12bb292012-03-30 19:50:15 +0900708 status = serial_port_in(port, SCSPTR);
Paul Mundtfaf02f82011-12-02 17:44:50 +0900709 status &= ~SCSPTR_CTSIO;
710 status |= SCSPTR_RTSIO;
Paul Mundtb12bb292012-03-30 19:50:15 +0900711 serial_port_out(port, SCSPTR, status); /* Set RTS = 1 */
Paul Mundtfaf02f82011-12-02 17:44:50 +0900712 }
Paul Mundtd5701642008-12-16 20:07:27 +0900713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900715static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900716{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200717 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900718
719 reg = sci_getreg(port, SCTFDR);
720 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900721 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900722
723 reg = sci_getreg(port, SCFDR);
724 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900725 return serial_port_in(port, SCFDR) >> 8;
Paul Mundt72b294c2011-06-14 17:38:19 +0900726
Paul Mundtb12bb292012-03-30 19:50:15 +0900727 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900728}
729
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900730static int sci_txroom(struct uart_port *port)
731{
Paul Mundt72b294c2011-06-14 17:38:19 +0900732 return port->fifosize - sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900733}
734
735static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900736{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +0200737 const struct plat_sci_reg *reg;
Paul Mundt72b294c2011-06-14 17:38:19 +0900738
739 reg = sci_getreg(port, SCRFDR);
740 if (reg->size)
Takashi Yoshii63f7ad12012-11-16 10:53:11 +0900741 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900742
743 reg = sci_getreg(port, SCFDR);
744 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +0900745 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
Paul Mundt72b294c2011-06-14 17:38:19 +0900746
Paul Mundtb12bb292012-03-30 19:50:15 +0900747 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900748}
749
Paul Mundt514820e2011-06-08 18:51:32 +0900750/*
751 * SCI helper for checking the state of the muxed port/RXD pins.
752 */
753static inline int sci_rxd_in(struct uart_port *port)
754{
755 struct sci_port *s = to_sci_port(port);
756
757 if (s->cfg->port_reg <= 0)
758 return 1;
759
Paul Mundt0dd4d5c2012-10-15 14:08:48 +0900760 /* Cast for ARM damage */
Laurent Pincharte2afca62013-12-11 13:40:31 +0100761 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
Paul Mundt514820e2011-06-08 18:51:32 +0900762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/* ********************************************************************** *
765 * the interrupt related routines *
766 * ********************************************************************** */
767
768static void sci_transmit_chars(struct uart_port *port)
769{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700770 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned short status;
773 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900774 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Paul Mundtb12bb292012-03-30 19:50:15 +0900776 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!(status & SCxSR_TDxE(port))) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900778 ctrl = serial_port_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900779 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900780 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900781 else
Paul Mundt8e698612009-06-24 19:44:32 +0900782 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900783 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return;
785 }
786
Paul Mundt72b294c2011-06-14 17:38:19 +0900787 count = sci_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 do {
790 unsigned char c;
791
792 if (port->x_char) {
793 c = port->x_char;
794 port->x_char = 0;
795 } else if (!uart_circ_empty(xmit) && !stopped) {
796 c = xmit->buf[xmit->tail];
797 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
798 } else {
799 break;
800 }
801
Paul Mundtb12bb292012-03-30 19:50:15 +0900802 serial_port_out(port, SCxTDR, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 port->icount.tx++;
805 } while (--count > 0);
806
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200807 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
810 uart_write_wakeup(port);
811 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100812 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900814 ctrl = serial_port_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900816 if (port->type != PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900817 serial_port_in(port, SCxSR); /* Dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200818 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Paul Mundt8e698612009-06-24 19:44:32 +0900821 ctrl |= SCSCR_TIE;
Paul Mundtb12bb292012-03-30 19:50:15 +0900822 serial_port_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824}
825
826/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900827#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900829static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900831 struct sci_port *sci_port = to_sci_port(port);
Jiri Slaby227434f2013-01-03 15:53:01 +0100832 struct tty_port *tport = &port->state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int i, count, copied = 0;
834 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800835 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Paul Mundtb12bb292012-03-30 19:50:15 +0900837 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!(status & SCxSR_RDxF(port)))
839 return;
840
841 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Don't copy more bytes than there is room for in the buffer */
Jiri Slaby227434f2013-01-03 15:53:01 +0100843 count = tty_buffer_request_room(tport, sci_rxfill(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* If for any reason we can't copy more data, we're done! */
846 if (count == 0)
847 break;
848
849 if (port->type == PORT_SCI) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900850 char c = serial_port_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900851 if (uart_handle_sysrq_char(port, c) ||
852 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900854 else
Jiri Slaby92a19f92013-01-03 15:53:03 +0100855 tty_insert_flip_char(tport, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900857 for (i = 0; i < count; i++) {
Paul Mundtb12bb292012-03-30 19:50:15 +0900858 char c = serial_port_in(port, SCxRDR);
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900859
Paul Mundtb12bb292012-03-30 19:50:15 +0900860 status = serial_port_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861#if defined(CONFIG_CPU_SH3)
862 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900863 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if ((c == 0) &&
865 (status & SCxSR_FER(port))) {
866 count--; i--;
867 continue;
868 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900871 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900872 sci_port->break_flag = 0;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (STEPFN(c)) {
875 count--; i--;
876 continue;
877 }
878 }
879#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100880 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 count--; i--;
882 continue;
883 }
884
885 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900886 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800887 flag = TTY_FRAME;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900888 port->icount.frame++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900889 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900890 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800891 flag = TTY_PARITY;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900892 port->icount.parity++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900893 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800894 } else
895 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900896
Jiri Slaby92a19f92013-01-03 15:53:03 +0100897 tty_insert_flip_char(tport, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 }
900
Paul Mundtb12bb292012-03-30 19:50:15 +0900901 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200902 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 copied += count;
905 port->icount.rx += count;
906 }
907
908 if (copied) {
909 /* Tell the rest of the system the news. New characters! */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100910 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 } else {
Paul Mundtb12bb292012-03-30 19:50:15 +0900912 serial_port_in(port, SCxSR); /* dummy read */
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +0200913 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915}
916
917#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900918
919/*
920 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * 1 per millisecond or so during the break period, for 9600 baud.
922 * So dont bother disabling interrupts.
923 * But dont want more than 1 break event.
924 * Use a kernel timer to periodically poll the rx line until
925 * the break is finished.
926 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900927static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900929 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* Ensure that two consecutive samples find the break over. */
933static void sci_break_timer(unsigned long data)
934{
Paul Mundte108b2c2006-09-27 16:32:13 +0900935 struct sci_port *port = (struct sci_port *)data;
936
937 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900939 sci_schedule_break_timer(port);
940 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* break is over. */
942 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900943 sci_schedule_break_timer(port);
944 } else
945 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900948static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +0900951 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100952 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +0900953 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100955 /* Handle overruns */
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +0200956 if (status & s->overrun_mask) {
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100957 port->icount.overrun++;
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900958
Laurent Pinchart3ae988d2013-12-06 10:59:17 +0100959 /* overrun error */
960 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
961 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900962
Joe Perches9b971cd2014-03-11 10:10:46 -0700963 dev_notice(port->dev, "overrun error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
Paul Mundte108b2c2006-09-27 16:32:13 +0900966 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (sci_rxd_in(port) == 0) {
968 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900969 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900970
971 if (!sci_port->break_flag) {
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900972 port->icount.brk++;
973
Paul Mundte108b2c2006-09-27 16:32:13 +0900974 sci_port->break_flag = 1;
975 sci_schedule_break_timer(sci_port);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900978 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900980
981 dev_dbg(port->dev, "BREAK detected\n");
982
Jiri Slaby92a19f92013-01-03 15:53:03 +0100983 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900984 copied++;
985 }
986
Paul Mundte108b2c2006-09-27 16:32:13 +0900987 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* frame error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +0900989 port->icount.frame++;
990
Jiri Slaby92a19f92013-01-03 15:53:03 +0100991 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800992 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900993
994 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 }
997
Paul Mundte108b2c2006-09-27 16:32:13 +0900998 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* parity error */
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001000 port->icount.parity++;
1001
Jiri Slaby92a19f92013-01-03 15:53:03 +01001002 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
Paul Mundte108b2c2006-09-27 16:32:13 +09001003 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001004
Joe Perches9b971cd2014-03-11 10:10:46 -07001005 dev_notice(port->dev, "parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Alan Cox33f0f882006-01-09 20:54:13 -08001008 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001009 tty_flip_buffer_push(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 return copied;
1012}
1013
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001014static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +09001015{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001016 struct tty_port *tport = &port->state->port;
Paul Mundtdebf9502011-06-08 18:19:37 +09001017 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001018 const struct plat_sci_reg *reg;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001019 int copied = 0;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001020 u16 status;
Paul Mundtd830fa42008-12-16 19:29:38 +09001021
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001022 reg = sci_getreg(port, s->overrun_reg);
Paul Mundt4b8c59a2011-06-14 17:53:34 +09001023 if (!reg->size)
Paul Mundtd830fa42008-12-16 19:29:38 +09001024 return 0;
1025
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001026 status = serial_port_in(port, s->overrun_reg);
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02001027 if (status & s->overrun_mask) {
1028 status &= ~s->overrun_mask;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001029 serial_port_out(port, s->overrun_reg, status);
Paul Mundtd830fa42008-12-16 19:29:38 +09001030
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001031 port->icount.overrun++;
1032
Jiri Slaby92a19f92013-01-03 15:53:03 +01001033 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001034 tty_flip_buffer_push(tport);
Paul Mundtd830fa42008-12-16 19:29:38 +09001035
Yoshihiro Kaneko51b31f12015-01-26 20:53:29 +09001036 dev_dbg(port->dev, "overrun error\n");
Paul Mundtd830fa42008-12-16 19:29:38 +09001037 copied++;
1038 }
1039
1040 return copied;
1041}
1042
Paul Mundt94c8b6d2011-01-20 23:26:18 +09001043static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 int copied = 0;
Paul Mundtb12bb292012-03-30 19:50:15 +09001046 unsigned short status = serial_port_in(port, SCxSR);
Jiri Slaby92a19f92013-01-03 15:53:03 +01001047 struct tty_port *tport = &port->state->port;
Magnus Damma5660ad2009-01-21 15:14:38 +00001048 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Paul Mundt0b3d4ef2007-03-14 13:22:37 +09001050 if (uart_handle_break(port))
1051 return 0;
1052
Paul Mundtb7a76e42006-02-01 03:06:06 -08001053 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#if defined(CONFIG_CPU_SH3)
1055 /* Debounce break */
1056 s->break_flag = 1;
1057#endif
Paul Mundtd97fbbe2011-11-24 19:15:06 +09001058
1059 port->icount.brk++;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Notify of BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001062 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -08001063 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +09001064
1065 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Alan Cox33f0f882006-01-09 20:54:13 -08001068 if (copied)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001069 tty_flip_buffer_push(tport);
Paul Mundte108b2c2006-09-27 16:32:13 +09001070
Paul Mundtd830fa42008-12-16 19:29:38 +09001071 copied += sci_handle_fifo_overrun(port);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return copied;
1074}
1075
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001076#ifdef CONFIG_SERIAL_SH_SCI_DMA
1077static void sci_dma_tx_complete(void *arg)
1078{
1079 struct sci_port *s = arg;
1080 struct uart_port *port = &s->port;
1081 struct circ_buf *xmit = &port->state->xmit;
1082 unsigned long flags;
1083
1084 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1085
1086 spin_lock_irqsave(&port->lock, flags);
1087
1088 xmit->tail += s->tx_dma_len;
1089 xmit->tail &= UART_XMIT_SIZE - 1;
1090
1091 port->icount.tx += s->tx_dma_len;
1092
1093 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1094 uart_write_wakeup(port);
1095
1096 if (!uart_circ_empty(xmit)) {
1097 s->cookie_tx = 0;
1098 schedule_work(&s->work_tx);
1099 } else {
1100 s->cookie_tx = -EINVAL;
1101 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1102 u16 ctrl = serial_port_in(port, SCSCR);
1103 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1104 }
1105 }
1106
1107 spin_unlock_irqrestore(&port->lock, flags);
1108}
1109
1110/* Locking: called with port lock held */
1111static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1112{
1113 struct uart_port *port = &s->port;
1114 struct tty_port *tport = &port->state->port;
1115 int copied;
1116
1117 copied = tty_insert_flip_string(tport, buf, count);
1118 if (copied < count) {
1119 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
1120 count - copied);
1121 port->icount.buf_overrun++;
1122 }
1123
1124 port->icount.rx += copied;
1125
1126 return copied;
1127}
1128
1129static int sci_dma_rx_find_active(struct sci_port *s)
1130{
1131 unsigned int i;
1132
1133 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1134 if (s->active_rx == s->cookie_rx[i])
1135 return i;
1136
1137 dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
1138 s->active_rx);
1139 return -1;
1140}
1141
1142static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1143{
1144 struct dma_chan *chan = s->chan_rx;
1145 struct uart_port *port = &s->port;
1146 unsigned long flags;
1147
1148 spin_lock_irqsave(&port->lock, flags);
1149 s->chan_rx = NULL;
1150 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1151 spin_unlock_irqrestore(&port->lock, flags);
1152 dmaengine_terminate_all(chan);
1153 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1154 sg_dma_address(&s->sg_rx[0]));
1155 dma_release_channel(chan);
1156 if (enable_pio)
1157 sci_start_rx(port);
1158}
1159
1160static void sci_dma_rx_complete(void *arg)
1161{
1162 struct sci_port *s = arg;
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001163 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001164 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001165 struct dma_async_tx_descriptor *desc;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001166 unsigned long flags;
1167 int active, count = 0;
1168
1169 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1170 s->active_rx);
1171
1172 spin_lock_irqsave(&port->lock, flags);
1173
1174 active = sci_dma_rx_find_active(s);
1175 if (active >= 0)
1176 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1177
1178 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1179
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001180 if (count)
1181 tty_flip_buffer_push(&port->state->port);
1182
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001183 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1184 DMA_DEV_TO_MEM,
1185 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1186 if (!desc)
1187 goto fail;
1188
1189 desc->callback = sci_dma_rx_complete;
1190 desc->callback_param = s;
1191 s->cookie_rx[active] = dmaengine_submit(desc);
1192 if (dma_submit_error(s->cookie_rx[active]))
1193 goto fail;
1194
1195 s->active_rx = s->cookie_rx[!active];
1196
Muhammad Hamza Farooq1d3db602015-09-18 13:08:30 +02001197 dma_async_issue_pending(chan);
1198
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001199 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1200 __func__, s->cookie_rx[active], active, s->active_rx);
1201 spin_unlock_irqrestore(&port->lock, flags);
1202 return;
1203
1204fail:
1205 spin_unlock_irqrestore(&port->lock, flags);
1206 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1207 sci_rx_dma_release(s, true);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001208}
1209
1210static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1211{
1212 struct dma_chan *chan = s->chan_tx;
1213 struct uart_port *port = &s->port;
1214 unsigned long flags;
1215
1216 spin_lock_irqsave(&port->lock, flags);
1217 s->chan_tx = NULL;
1218 s->cookie_tx = -EINVAL;
1219 spin_unlock_irqrestore(&port->lock, flags);
1220 dmaengine_terminate_all(chan);
1221 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1222 DMA_TO_DEVICE);
1223 dma_release_channel(chan);
1224 if (enable_pio)
1225 sci_start_tx(port);
1226}
1227
1228static void sci_submit_rx(struct sci_port *s)
1229{
1230 struct dma_chan *chan = s->chan_rx;
1231 int i;
1232
1233 for (i = 0; i < 2; i++) {
1234 struct scatterlist *sg = &s->sg_rx[i];
1235 struct dma_async_tx_descriptor *desc;
1236
1237 desc = dmaengine_prep_slave_sg(chan,
1238 sg, 1, DMA_DEV_TO_MEM,
1239 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1240 if (!desc)
1241 goto fail;
1242
1243 desc->callback = sci_dma_rx_complete;
1244 desc->callback_param = s;
1245 s->cookie_rx[i] = dmaengine_submit(desc);
1246 if (dma_submit_error(s->cookie_rx[i]))
1247 goto fail;
1248
1249 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1250 s->cookie_rx[i], i);
1251 }
1252
1253 s->active_rx = s->cookie_rx[0];
1254
1255 dma_async_issue_pending(chan);
1256 return;
1257
1258fail:
1259 if (i)
1260 dmaengine_terminate_all(chan);
1261 for (i = 0; i < 2; i++)
1262 s->cookie_rx[i] = -EINVAL;
1263 s->active_rx = -EINVAL;
1264 dev_warn(s->port.dev, "Failed to re-start Rx DMA, using PIO\n");
1265 sci_rx_dma_release(s, true);
1266}
1267
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001268static void work_fn_tx(struct work_struct *work)
1269{
1270 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1271 struct dma_async_tx_descriptor *desc;
1272 struct dma_chan *chan = s->chan_tx;
1273 struct uart_port *port = &s->port;
1274 struct circ_buf *xmit = &port->state->xmit;
1275 dma_addr_t buf;
1276
1277 /*
1278 * DMA is idle now.
1279 * Port xmit buffer is already mapped, and it is one page... Just adjust
1280 * offsets and lengths. Since it is a circular buffer, we have to
1281 * transmit till the end, and then the rest. Take the port lock to get a
1282 * consistent xmit buffer state.
1283 */
1284 spin_lock_irq(&port->lock);
1285 buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1));
1286 s->tx_dma_len = min_t(unsigned int,
1287 CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1288 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1289 spin_unlock_irq(&port->lock);
1290
1291 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1292 DMA_MEM_TO_DEV,
1293 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1294 if (!desc) {
1295 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1296 /* switch to PIO */
1297 sci_tx_dma_release(s, true);
1298 return;
1299 }
1300
1301 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1302 DMA_TO_DEVICE);
1303
1304 spin_lock_irq(&port->lock);
1305 desc->callback = sci_dma_tx_complete;
1306 desc->callback_param = s;
1307 spin_unlock_irq(&port->lock);
1308 s->cookie_tx = dmaengine_submit(desc);
1309 if (dma_submit_error(s->cookie_tx)) {
1310 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1311 /* switch to PIO */
1312 sci_tx_dma_release(s, true);
1313 return;
1314 }
1315
1316 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
1317 __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1318
1319 dma_async_issue_pending(chan);
1320}
1321
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001322static void rx_timer_fn(unsigned long arg)
1323{
1324 struct sci_port *s = (struct sci_port *)arg;
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001325 struct dma_chan *chan = s->chan_rx;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001326 struct uart_port *port = &s->port;
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001327 struct dma_tx_state state;
1328 enum dma_status status;
1329 unsigned long flags;
1330 unsigned int read;
1331 int active, count;
1332 u16 scr;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001333
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001334 spin_lock_irqsave(&port->lock, flags);
1335
1336 dev_dbg(port->dev, "DMA Rx timed out\n");
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001337
1338 active = sci_dma_rx_find_active(s);
1339 if (active < 0) {
1340 spin_unlock_irqrestore(&port->lock, flags);
1341 return;
1342 }
1343
1344 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001345 if (status == DMA_COMPLETE) {
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001346 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1347 s->active_rx, active);
Muhammad Hamza Farooq3b963042015-09-18 13:08:31 +02001348 spin_unlock_irqrestore(&port->lock, flags);
1349
1350 /* Let packet complete handler take care of the packet */
1351 return;
1352 }
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001353
Muhammad Hamza Farooqe7327c02015-09-18 13:08:32 +02001354 dmaengine_pause(chan);
1355
1356 /*
1357 * sometimes DMA transfer doesn't stop even if it is stopped and
1358 * data keeps on coming until transaction is complete so check
1359 * for DMA_COMPLETE again
1360 * Let packet complete handler take care of the packet
1361 */
1362 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1363 if (status == DMA_COMPLETE) {
1364 spin_unlock_irqrestore(&port->lock, flags);
1365 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1366 return;
1367 }
1368
Geert Uytterhoeven67f462b02015-09-18 13:08:25 +02001369 /* Handle incomplete DMA receive */
1370 dmaengine_terminate_all(s->chan_rx);
1371 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1372 dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
1373 s->active_rx);
1374
1375 if (read) {
1376 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1377 if (count)
1378 tty_flip_buffer_push(&port->state->port);
1379 }
1380
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001381 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1382 sci_submit_rx(s);
Muhammad Hamza Farooq371cfed2015-09-18 13:08:29 +02001383
1384 /* Direct new serial port interrupts back to CPU */
1385 scr = serial_port_in(port, SCSCR);
1386 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1387 scr &= ~SCSCR_RDRQE;
1388 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1389 }
1390 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1391
1392 spin_unlock_irqrestore(&port->lock, flags);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001393}
1394
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001395static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1396 enum dma_transfer_direction dir,
1397 unsigned int id)
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001398{
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001399 dma_cap_mask_t mask;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001400 struct dma_chan *chan;
1401 struct dma_slave_config cfg;
1402 int ret;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001403
1404 dma_cap_zero(mask);
1405 dma_cap_set(DMA_SLAVE, mask);
1406
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001407 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1408 (void *)(unsigned long)id, port->dev,
1409 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1410 if (!chan) {
1411 dev_warn(port->dev,
1412 "dma_request_slave_channel_compat failed\n");
1413 return NULL;
1414 }
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001415
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001416 memset(&cfg, 0, sizeof(cfg));
1417 cfg.direction = dir;
1418 if (dir == DMA_MEM_TO_DEV) {
1419 cfg.dst_addr = port->mapbase +
1420 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1421 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1422 } else {
1423 cfg.src_addr = port->mapbase +
1424 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1425 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1426 }
1427
1428 ret = dmaengine_slave_config(chan, &cfg);
1429 if (ret) {
1430 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1431 dma_release_channel(chan);
1432 return NULL;
1433 }
1434
1435 return chan;
1436}
1437
1438static void sci_request_dma(struct uart_port *port)
1439{
1440 struct sci_port *s = to_sci_port(port);
1441 struct dma_chan *chan;
1442
1443 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1444
1445 if (!port->dev->of_node &&
1446 (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
1447 return;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001448
1449 s->cookie_tx = -EINVAL;
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001450 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001451 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1452 if (chan) {
1453 s->chan_tx = chan;
1454 /* UART circular tx buffer is an aligned page. */
1455 s->tx_dma_addr = dma_map_single(chan->device->dev,
1456 port->state->xmit.buf,
1457 UART_XMIT_SIZE,
1458 DMA_TO_DEVICE);
1459 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1460 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1461 dma_release_channel(chan);
1462 s->chan_tx = NULL;
1463 } else {
1464 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1465 __func__, UART_XMIT_SIZE,
1466 port->state->xmit.buf, &s->tx_dma_addr);
1467 }
1468
1469 INIT_WORK(&s->work_tx, work_fn_tx);
1470 }
1471
Geert Uytterhoevenff441122015-09-18 13:08:33 +02001472 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001473 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1474 if (chan) {
1475 unsigned int i;
1476 dma_addr_t dma;
1477 void *buf;
1478
1479 s->chan_rx = chan;
1480
1481 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1482 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1483 &dma, GFP_KERNEL);
1484 if (!buf) {
1485 dev_warn(port->dev,
1486 "Failed to allocate Rx dma buffer, using PIO\n");
1487 dma_release_channel(chan);
1488 s->chan_rx = NULL;
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001489 return;
1490 }
1491
1492 for (i = 0; i < 2; i++) {
1493 struct scatterlist *sg = &s->sg_rx[i];
1494
1495 sg_init_table(sg, 1);
1496 s->rx_buf[i] = buf;
1497 sg_dma_address(sg) = dma;
1498 sg->length = s->buf_len_rx;
1499
1500 buf += s->buf_len_rx;
1501 dma += s->buf_len_rx;
1502 }
1503
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001504 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1505
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001506 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1507 sci_submit_rx(s);
Geert Uytterhoevene1910fc2015-09-18 13:08:24 +02001508 }
1509}
1510
1511static void sci_free_dma(struct uart_port *port)
1512{
1513 struct sci_port *s = to_sci_port(port);
1514
1515 if (s->chan_tx)
1516 sci_tx_dma_release(s, false);
1517 if (s->chan_rx)
1518 sci_rx_dma_release(s, false);
1519}
1520#else
1521static inline void sci_request_dma(struct uart_port *port)
1522{
1523}
1524
1525static inline void sci_free_dma(struct uart_port *port)
1526{
1527}
1528#endif
1529
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001530static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001532#ifdef CONFIG_SERIAL_SH_SCI_DMA
1533 struct uart_port *port = ptr;
1534 struct sci_port *s = to_sci_port(port);
1535
1536 if (s->chan_rx) {
Paul Mundtb12bb292012-03-30 19:50:15 +09001537 u16 scr = serial_port_in(port, SCSCR);
1538 u16 ssr = serial_port_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001539
1540 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001541 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001542 disable_irq_nosync(irq);
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001543 scr |= SCSCR_RDRQE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001544 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +09001545 scr &= ~SCSCR_RIE;
Geert Uytterhoeven756981b2015-09-18 13:08:26 +02001546 sci_submit_rx(s);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001547 }
Paul Mundtb12bb292012-03-30 19:50:15 +09001548 serial_port_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001549 /* Clear current interrupt */
Geert Uytterhoeven54af5002015-08-21 20:02:28 +02001550 serial_port_out(port, SCxSR,
1551 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001552 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
1553 jiffies, s->rx_timeout);
1554 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001555
1556 return IRQ_HANDLED;
1557 }
1558#endif
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* I think sci_receive_chars has to be called irrespective
1561 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1562 * to be disabled?
1563 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001564 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 return IRQ_HANDLED;
1567}
1568
David Howells7d12e782006-10-05 14:55:46 +01001569static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +09001572 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Stuart Menefyfd78a762009-07-29 23:01:24 +09001574 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +09001576 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 return IRQ_HANDLED;
1579}
1580
David Howells7d12e782006-10-05 14:55:46 +01001581static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct uart_port *port = ptr;
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001584 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* Handle errors */
1587 if (port->type == PORT_SCI) {
1588 if (sci_handle_errors(port)) {
1589 /* discard character in rx buffer */
Paul Mundtb12bb292012-03-30 19:50:15 +09001590 serial_port_in(port, SCxSR);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001591 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +09001594 sci_handle_fifo_overrun(port);
Geert Uytterhoevene6403c12015-08-21 20:02:55 +02001595 if (!s->chan_rx)
1596 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001599 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* Kick the transmission */
Yoshihiro Shimoda8eadb562015-08-21 20:02:56 +02001602 if (!s->chan_tx)
1603 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 return IRQ_HANDLED;
1606}
1607
David Howells7d12e782006-10-05 14:55:46 +01001608static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
1610 struct uart_port *port = ptr;
1611
1612 /* Handle BREAKs */
1613 sci_handle_breaks(port);
Geert Uytterhoevena1b5b432015-08-21 20:02:25 +02001614 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 return IRQ_HANDLED;
1617}
1618
David Howells7d12e782006-10-05 14:55:46 +01001619static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001621 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
Michael Trimarchia8884e32008-10-31 16:10:23 +09001622 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001623 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +09001624 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Paul Mundtb12bb292012-03-30 19:50:15 +09001626 ssr_status = serial_port_in(port, SCxSR);
1627 scr_status = serial_port_in(port, SCSCR);
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001628 if (s->overrun_reg == SCxSR)
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001629 orer_status = ssr_status;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02001630 else {
1631 if (sci_getreg(port, s->overrun_reg)->size)
1632 orer_status = serial_port_in(port, s->overrun_reg);
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001633 }
1634
Paul Mundtf43dc232011-01-13 15:06:28 +09001635 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +09001638 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001639 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001640 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001641
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001642 /*
1643 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1644 * DR flags
1645 */
1646 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Geert Uytterhoevene0a12a22015-08-21 20:02:35 +02001647 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +09001648 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001651 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001652 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +09001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +00001655 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +09001656 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001658 /* Overrun Interrupt */
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001659 if (orer_status & s->overrun_mask) {
Nobuhiro Iwamatsucb772fe2015-03-17 01:19:19 +09001660 sci_handle_fifo_overrun(port);
Yoshihiro Shimoda90803072015-08-21 20:02:36 +02001661 ret = IRQ_HANDLED;
1662 }
Hisashi Nakamura8b6ff842015-01-26 21:25:48 +09001663
Michael Trimarchia8884e32008-10-31 16:10:23 +09001664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001668 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * ports' baud rate when the peripheral clock changes.
1670 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001671static int sci_notifier(struct notifier_block *self,
1672 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Magnus Damme552de22009-01-21 15:13:42 +00001674 struct sci_port *sci_port;
1675 unsigned long flags;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001676 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Paul Mundtd535a232011-01-19 17:19:35 +09001678 sci_port = container_of(self, struct sci_port, freq_transition);
1679
Viresh Kumar0b443ea2014-03-19 11:24:58 +05301680 if (phase == CPUFREQ_POSTCHANGE) {
Paul Mundtd535a232011-01-19 17:19:35 +09001681 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +09001682
Paul Mundtd535a232011-01-19 17:19:35 +09001683 spin_lock_irqsave(&port->lock, flags);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001684 for (i = 0; i < SCI_NUM_CLKS; i++)
1685 sci_port->clk_rates[i] =
1686 clk_get_rate(sci_port->clks[i]);
Paul Mundtd535a232011-01-19 17:19:35 +09001687 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +00001688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return NOTIFY_OK;
1691}
Magnus Damm501b8252009-01-21 15:14:30 +00001692
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001693static const struct sci_irq_desc {
Paul Mundt9174fc82011-06-28 15:25:36 +09001694 const char *desc;
1695 irq_handler_t handler;
1696} sci_irq_desc[] = {
1697 /*
1698 * Split out handlers, the default case.
1699 */
1700 [SCIx_ERI_IRQ] = {
1701 .desc = "rx err",
1702 .handler = sci_er_interrupt,
1703 },
1704
1705 [SCIx_RXI_IRQ] = {
1706 .desc = "rx full",
1707 .handler = sci_rx_interrupt,
1708 },
1709
1710 [SCIx_TXI_IRQ] = {
1711 .desc = "tx empty",
1712 .handler = sci_tx_interrupt,
1713 },
1714
1715 [SCIx_BRI_IRQ] = {
1716 .desc = "break",
1717 .handler = sci_br_interrupt,
1718 },
1719
1720 /*
1721 * Special muxed handler.
1722 */
1723 [SCIx_MUX_IRQ] = {
1724 .desc = "mux",
1725 .handler = sci_mpxed_interrupt,
1726 },
1727};
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729static int sci_request_irq(struct sci_port *port)
1730{
Paul Mundt9174fc82011-06-28 15:25:36 +09001731 struct uart_port *up = &port->port;
1732 int i, j, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Paul Mundt9174fc82011-06-28 15:25:36 +09001734 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
Geert Uytterhoevend56a91e2015-08-21 20:02:32 +02001735 const struct sci_irq_desc *desc;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001736 int irq;
Paul Mundte108b2c2006-09-27 16:32:13 +09001737
Paul Mundt9174fc82011-06-28 15:25:36 +09001738 if (SCIx_IRQ_IS_MUXED(port)) {
1739 i = SCIx_MUX_IRQ;
1740 irq = up->irq;
Paul Mundt0e8963d2012-05-18 18:21:06 +09001741 } else {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001742 irq = port->irqs[i];
Paul Mundt9174fc82011-06-28 15:25:36 +09001743
Paul Mundt0e8963d2012-05-18 18:21:06 +09001744 /*
1745 * Certain port types won't support all of the
1746 * available interrupt sources.
1747 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001748 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001749 continue;
1750 }
1751
Paul Mundt9174fc82011-06-28 15:25:36 +09001752 desc = sci_irq_desc + i;
1753 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1754 dev_name(up->dev), desc->desc);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02001755 if (!port->irqstr[j])
Paul Mundt9174fc82011-06-28 15:25:36 +09001756 goto out_nomem;
Paul Mundt762c69e2008-12-16 18:55:26 +09001757
Paul Mundt9174fc82011-06-28 15:25:36 +09001758 ret = request_irq(irq, desc->handler, up->irqflags,
1759 port->irqstr[j], port);
1760 if (unlikely(ret)) {
1761 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1762 goto out_noirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764 }
1765
1766 return 0;
Paul Mundt9174fc82011-06-28 15:25:36 +09001767
1768out_noirq:
1769 while (--i >= 0)
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001770 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001771
1772out_nomem:
1773 while (--j >= 0)
1774 kfree(port->irqstr[j]);
1775
1776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779static void sci_free_irq(struct sci_port *port)
1780{
1781 int i;
1782
Paul Mundt9174fc82011-06-28 15:25:36 +09001783 /*
1784 * Intentionally in reverse order so we iterate over the muxed
1785 * IRQ first.
1786 */
1787 for (i = 0; i < SCIx_NR_IRQS; i++) {
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001788 int irq = port->irqs[i];
Paul Mundt0e8963d2012-05-18 18:21:06 +09001789
1790 /*
1791 * Certain port types won't support all of the available
1792 * interrupt sources.
1793 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001794 if (unlikely(irq < 0))
Paul Mundt0e8963d2012-05-18 18:21:06 +09001795 continue;
1796
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01001797 free_irq(port->irqs[i], port);
Paul Mundt9174fc82011-06-28 15:25:36 +09001798 kfree(port->irqstr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Paul Mundt9174fc82011-06-28 15:25:36 +09001800 if (SCIx_IRQ_IS_MUXED(port)) {
1801 /* If there's only one IRQ, we're done. */
1802 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804 }
1805}
1806
1807static unsigned int sci_tx_empty(struct uart_port *port)
1808{
Paul Mundtb12bb292012-03-30 19:50:15 +09001809 unsigned short status = serial_port_in(port, SCxSR);
Paul Mundt72b294c2011-06-14 17:38:19 +09001810 unsigned short in_tx_fifo = sci_txfill(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001811
1812 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Paul Mundtcdf7c422011-11-24 20:18:32 +09001815/*
1816 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1817 * CTS/RTS is supported in hardware by at least one port and controlled
1818 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1819 * handled via the ->init_pins() op, which is a bit of a one-way street,
1820 * lacking any ability to defer pin control -- this will later be
1821 * converted over to the GPIO framework).
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001822 *
1823 * Other modes (such as loopback) are supported generically on certain
1824 * port types, but not others. For these it's sufficient to test for the
1825 * existence of the support register and simply ignore the port type.
Paul Mundtcdf7c422011-11-24 20:18:32 +09001826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1828{
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001829 if (mctrl & TIOCM_LOOP) {
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001830 const struct plat_sci_reg *reg;
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001831
1832 /*
1833 * Standard loopback mode for SCFCR ports.
1834 */
1835 reg = sci_getreg(port, SCFCR);
1836 if (reg->size)
Geert Uytterhoeven26de4f12014-03-11 11:11:19 +01001837 serial_port_out(port, SCFCR,
1838 serial_port_in(port, SCFCR) |
1839 SCFCR_LOOP);
Paul Mundtdc7e3ef2011-11-24 20:20:53 +09001840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843static unsigned int sci_get_mctrl(struct uart_port *port)
1844{
Paul Mundtcdf7c422011-11-24 20:18:32 +09001845 /*
1846 * CTS/RTS is handled in hardware when supported, while nothing
1847 * else is wired up. Keep it simple and simply assert DSR/CAR.
1848 */
1849 return TIOCM_DSR | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static void sci_break_ctl(struct uart_port *port, int break_state)
1853{
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001854 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02001855 const struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001856 unsigned short scscr, scsptr;
1857
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001858 /* check wheter the port has SCSPTR */
1859 if (!reg->size) {
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001860 /*
1861 * Not supported by hardware. Most parts couple break and rx
1862 * interrupts together, with break detection always enabled.
1863 */
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001864 return;
Shimoda, Yoshihirobbb4ce52012-04-06 09:59:14 +09001865 }
Shimoda, Yoshihiroa4e02f62012-04-12 19:19:21 +09001866
1867 scsptr = serial_port_in(port, SCSPTR);
1868 scscr = serial_port_in(port, SCSCR);
1869
1870 if (break_state == -1) {
1871 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1872 scscr &= ~SCSCR_TE;
1873 } else {
1874 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1875 scscr |= SCSCR_TE;
1876 }
1877
1878 serial_port_out(port, SCSPTR, scsptr);
1879 serial_port_out(port, SCSCR, scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882static int sci_startup(struct uart_port *port)
1883{
Magnus Damma5660ad2009-01-21 15:14:38 +00001884 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001885 unsigned long flags;
Paul Mundt073e84c2011-01-19 17:30:53 +09001886 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001888 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1889
Paul Mundt073e84c2011-01-19 17:30:53 +09001890 ret = sci_request_irq(s);
1891 if (unlikely(ret < 0))
1892 return ret;
1893
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001894 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001895
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001896 spin_lock_irqsave(&port->lock, flags);
Yoshinori Satod6569012005-10-14 15:59:12 -07001897 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001898 sci_start_rx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001899 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 return 0;
1902}
1903
1904static void sci_shutdown(struct uart_port *port)
1905{
Magnus Damma5660ad2009-01-21 15:14:38 +00001906 struct sci_port *s = to_sci_port(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001907 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001909 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1910
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001911 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001913 sci_stop_tx(port);
Shinya Kuribayashi33b48e12012-11-16 10:54:49 +09001914 spin_unlock_irqrestore(&port->lock, flags);
Paul Mundt073e84c2011-01-19 17:30:53 +09001915
Aleksandar Mitev9ab76552015-09-18 13:08:28 +02001916#ifdef CONFIG_SERIAL_SH_SCI_DMA
1917 if (s->chan_rx) {
1918 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
1919 port->line);
1920 del_timer_sync(&s->rx_timer);
1921 }
1922#endif
1923
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001924 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 sci_free_irq(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01001928static int sci_sck_calc(struct sci_port *s, unsigned int bps,
1929 unsigned int *srr)
1930{
1931 unsigned long freq = s->clk_rates[SCI_SCK];
1932 unsigned int min_sr, max_sr, sr;
1933 int err, min_err = INT_MAX;
1934
1935 if (s->sampling_rate) {
1936 /* SCI(F) has a fixed sampling rate */
1937 min_sr = max_sr = s->sampling_rate / 2;
1938 } else {
1939 /* HSCIF has a variable 1/(8..32) sampling rate */
1940 min_sr = 8;
1941 max_sr = 32;
1942 }
1943
1944 for (sr = max_sr; sr >= min_sr; sr--) {
1945 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
1946 if (abs(err) >= abs(min_err))
1947 continue;
1948
1949 min_err = err;
1950 *srr = sr - 1;
1951
1952 if (!err)
1953 break;
1954 }
1955
1956 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
1957 *srr + 1);
1958 return min_err;
1959}
1960
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001961/* calculate sample rate, BRR, and clock select */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001962static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
1963 unsigned int *brr, unsigned int *srr,
1964 unsigned int *cks)
Paul Mundt26c92f32009-06-24 18:23:52 +09001965{
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001966 unsigned int min_sr, max_sr, shift, sr, br, prediv, scrate, c;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01001967 unsigned long freq = s->clk_rates[SCI_FCK];
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001968 int err, min_err = INT_MAX;
Ulrich Hechtf303b362013-05-31 17:57:01 +02001969
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001970 if (s->sampling_rate) {
1971 min_sr = max_sr = s->sampling_rate;
1972 shift = 0;
1973 } else {
1974 /* HSCIF has a variable sample rate */
1975 min_sr = 8;
1976 max_sr = 32;
1977 shift = 1;
1978 }
1979
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01001980 /*
1981 * Find the combination of sample rate and clock select with the
1982 * smallest deviation from the desired baud rate.
1983 * Prefer high sample rates to maximise the receive margin.
1984 *
1985 * M: Receive margin (%)
1986 * N: Ratio of bit rate to clock (N = sampling rate)
1987 * D: Clock duty (D = 0 to 1.0)
1988 * L: Frame length (L = 9 to 12)
1989 * F: Absolute value of clock frequency deviation
1990 *
1991 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
1992 * (|D - 0.5| / N * (1 + F))|
1993 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
1994 */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001995 for (sr = max_sr; sr >= min_sr; sr--) {
Ulrich Hechtf303b362013-05-31 17:57:01 +02001996 for (c = 0; c <= 3; c++) {
1997 /* integerized formulas from HSCIF documentation */
Geert Uytterhoevenb4a5c452015-11-16 17:22:16 +01001998 prediv = sr * (1 << (2 * c + shift));
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01001999
2000 /*
2001 * We need to calculate:
2002 *
2003 * br = freq / (prediv * bps) clamped to [1..256]
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002004 * err = freq / (br * prediv) - bps
Geert Uytterhoevende01e6c2015-11-13 17:04:56 +01002005 *
2006 * Watch out for overflow when calculating the desired
2007 * sampling clock rate!
2008 */
2009 if (bps > UINT_MAX / prediv)
2010 break;
2011
2012 scrate = prediv * bps;
2013 br = DIV_ROUND_CLOSEST(freq, scrate);
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002014 br = clamp(br, 1U, 256U);
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002015
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002016 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002017 if (abs(err) >= abs(min_err))
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002018 continue;
2019
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002020 min_err = err;
Geert Uytterhoeven95a27032015-11-13 16:56:08 +01002021 *brr = br - 1;
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002022 *srr = sr - 1;
2023 *cks = c;
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002024
2025 if (!err)
2026 goto found;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002027 }
2028 }
2029
Geert Uytterhoeven6c513322015-11-16 16:33:22 +01002030found:
Geert Uytterhoeven881a7482015-11-16 15:54:47 +01002031 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2032 min_err, *brr, *srr + 1, *cks);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002033 return min_err;
Ulrich Hechtf303b362013-05-31 17:57:01 +02002034}
2035
Magnus Damm1ba76222011-08-03 03:47:36 +00002036static void sci_reset(struct uart_port *port)
2037{
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002038 const struct plat_sci_reg *reg;
Magnus Damm1ba76222011-08-03 03:47:36 +00002039 unsigned int status;
2040
2041 do {
Paul Mundtb12bb292012-03-30 19:50:15 +09002042 status = serial_port_in(port, SCxSR);
Magnus Damm1ba76222011-08-03 03:47:36 +00002043 } while (!(status & SCxSR_TEND(port)));
2044
Paul Mundtb12bb292012-03-30 19:50:15 +09002045 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
Magnus Damm1ba76222011-08-03 03:47:36 +00002046
Paul Mundt0979e0e2011-11-24 18:35:49 +09002047 reg = sci_getreg(port, SCFCR);
2048 if (reg->size)
Paul Mundtb12bb292012-03-30 19:50:15 +09002049 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Magnus Damm1ba76222011-08-03 03:47:36 +00002050}
2051
Alan Cox606d0992006-12-08 02:38:45 -08002052static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2053 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002055 unsigned int baud, smr_val = 0, scr_val = 0, i;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002056 unsigned int brr = 255, cks = 0, srr = 15, sccks = 0;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002057 unsigned int brr1 = 255, cks1 = 0, srr1 = 15;
Paul Mundt00b9de92009-06-24 17:53:33 +09002058 struct sci_port *s = to_sci_port(port);
Geert Uytterhoevend3184e62015-08-21 20:02:33 +02002059 const struct plat_sci_reg *reg;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002060 int min_err = INT_MAX, err;
2061 unsigned long max_freq = 0;
2062 int best_clk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Nobuhiro Iwamatsu730c4e72014-07-14 16:10:00 +09002064 if ((termios->c_cflag & CSIZE) == CS7)
2065 smr_val |= SCSMR_CHR;
2066 if (termios->c_cflag & PARENB)
2067 smr_val |= SCSMR_PE;
2068 if (termios->c_cflag & PARODD)
2069 smr_val |= SCSMR_PE | SCSMR_ODD;
2070 if (termios->c_cflag & CSTOPB)
2071 smr_val |= SCSMR_STOP;
2072
Magnus Damm154280f2009-12-22 03:37:28 +00002073 /*
2074 * earlyprintk comes here early on with port->uartclk set to zero.
2075 * the clock framework is not up and running at this point so here
2076 * we assume that 115200 is the maximum baud rate. please note that
2077 * the baud rate is not programmed during earlyprintk - it is assumed
2078 * that the previous boot loader has enabled required clocks and
2079 * setup the baud rate generator hardware for us already.
2080 */
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002081 if (!port->uartclk) {
2082 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2083 goto done;
2084 }
Magnus Damm154280f2009-12-22 03:37:28 +00002085
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002086 for (i = 0; i < SCI_NUM_CLKS; i++)
2087 max_freq = max(max_freq, s->clk_rates[i]);
2088
2089 baud = uart_get_baud_rate(port, termios, old, 0,
2090 max_freq / max(s->sampling_rate, 8U));
2091 if (!baud)
2092 goto done;
2093
2094 /*
2095 * There can be multiple sources for the sampling clock. Find the one
2096 * that gives us the smallest deviation from the desired baud rate.
2097 */
2098
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002099 /* Optional Undivided External Clock */
2100 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2101 port->type != PORT_SCIFB) {
2102 err = sci_sck_calc(s, baud, &srr1);
2103 if (abs(err) < abs(min_err)) {
2104 best_clk = SCI_SCK;
2105 scr_val = SCSCR_CKE1;
2106 sccks = SCCKS_CKS;
2107 min_err = err;
2108 srr = srr1;
2109 if (!err)
2110 goto done;
2111 }
2112 }
2113
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002114 /* Divided Functional Clock using standard Bit Rate Register */
2115 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2116 if (abs(err) < abs(min_err)) {
2117 best_clk = SCI_FCK;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002118 scr_val = 0;
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002119 min_err = err;
2120 brr = brr1;
2121 srr = srr1;
2122 cks = cks1;
2123 }
2124
2125done:
2126 if (best_clk >= 0)
2127 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2128 s->clks[best_clk], baud, min_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Paul Mundt23241d42011-06-28 13:55:31 +09002130 sci_port_enable(s);
Alexandre Courbot36003382011-03-03 08:04:42 +00002131
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002132 /*
2133 * Program the optional External Baud Rate Generator (BRG) first.
2134 * It controls the mux to select (H)SCK or frequency divided clock.
2135 */
2136 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size)
2137 serial_port_out(port, SCCKS, sccks);
2138
Magnus Damm1ba76222011-08-03 03:47:36 +00002139 sci_reset(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002140
Paul Mundte108b2c2006-09-27 16:32:13 +09002141 uart_update_timeout(port, termios->c_cflag, baud);
2142
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002143 if (best_clk >= 0) {
2144 smr_val |= cks;
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002145 dev_dbg(port->dev,
2146 "SCR 0x%x SMR 0x%x BRR %u CKS 0x%x SRR %u\n",
2147 scr_val, smr_val, brr, sccks, srr);
2148 serial_port_out(port, SCSCR, scr_val);
Takashi Yoshii9d482cc2012-11-16 10:52:49 +09002149 serial_port_out(port, SCSMR, smr_val);
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002150 serial_port_out(port, SCBRR, brr);
2151 if (sci_getreg(port, HSSRR)->size)
2152 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
2153
2154 /* Wait one bit interval */
2155 udelay((1000000 + (baud - 1)) / baud);
2156 } else {
2157 /* Don't touch the bit rate configuration */
2158 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
2159 smr_val |= serial_port_in(port, SCSMR) & SCSMR_CKS;
2160 dev_dbg(port->dev, "SCR 0x%x SMR 0x%x\n", scr_val, smr_val);
2161 serial_port_out(port, SCSCR, scr_val);
2162 serial_port_out(port, SCSMR, smr_val);
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Paul Mundtd5701642008-12-16 20:07:27 +09002165 sci_init_pins(port, termios->c_cflag);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002166
Paul Mundt73c3d532011-12-02 19:02:06 +09002167 reg = sci_getreg(port, SCFCR);
2168 if (reg->size) {
Paul Mundtb12bb292012-03-30 19:50:15 +09002169 unsigned short ctrl = serial_port_in(port, SCFCR);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002170
Paul Mundt73c3d532011-12-02 19:02:06 +09002171 if (s->cfg->capabilities & SCIx_HAVE_RTSCTS) {
Paul Mundtfaf02f82011-12-02 17:44:50 +09002172 if (termios->c_cflag & CRTSCTS)
2173 ctrl |= SCFCR_MCE;
2174 else
2175 ctrl &= ~SCFCR_MCE;
Paul Mundtfaf02f82011-12-02 17:44:50 +09002176 }
Paul Mundt73c3d532011-12-02 19:02:06 +09002177
2178 /*
2179 * As we've done a sci_reset() above, ensure we don't
2180 * interfere with the FIFOs while toggling MCE. As the
2181 * reset values could still be set, simply mask them out.
2182 */
2183 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2184
Paul Mundtb12bb292012-03-30 19:50:15 +09002185 serial_port_out(port, SCFCR, ctrl);
Paul Mundt0979e0e2011-11-24 18:35:49 +09002186 }
Paul Mundtb7a76e42006-02-01 03:06:06 -08002187
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002188 scr_val |= s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0);
2189 dev_dbg(port->dev, "SCSCR 0x%x\n", scr_val);
2190 serial_port_out(port, SCSCR, scr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002192#ifdef CONFIG_SERIAL_SH_SCI_DMA
2193 /*
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002194 * Calculate delay for 2 DMA buffers (4 FIFO).
Geert Uytterhoevenf5835c12015-08-21 20:02:38 +02002195 * See serial_core.c::uart_update_timeout().
2196 * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
2197 * function calculates 1 jiffie for the data plus 5 jiffies for the
2198 * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
2199 * buffers (4 FIFO sizes), but when performing a faster transfer, the
2200 * value obtained by this formula is too small. Therefore, if the value
2201 * is smaller than 20ms, use 20ms as the timeout value for DMA.
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002202 */
2203 if (s->chan_rx) {
Nobuhiro Iwamatsu5f6d8512015-03-17 01:19:54 +09002204 unsigned int bits;
2205
2206 /* byte size and parity */
2207 switch (termios->c_cflag & CSIZE) {
2208 case CS5:
2209 bits = 7;
2210 break;
2211 case CS6:
2212 bits = 8;
2213 break;
2214 case CS7:
2215 bits = 9;
2216 break;
2217 default:
2218 bits = 10;
2219 break;
2220 }
2221
2222 if (termios->c_cflag & CSTOPB)
2223 bits++;
2224 if (termios->c_cflag & PARENB)
2225 bits++;
2226 s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
2227 (baud / 10), 10);
Joe Perches9b971cd2014-03-11 10:10:46 -07002228 dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00002229 s->rx_timeout * 1000 / HZ, port->timeout);
2230 if (s->rx_timeout < msecs_to_jiffies(20))
2231 s->rx_timeout = msecs_to_jiffies(20);
2232 }
2233#endif
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002236 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00002237
Paul Mundt23241d42011-06-28 13:55:31 +09002238 sci_port_disable(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002241static void sci_pm(struct uart_port *port, unsigned int state,
2242 unsigned int oldstate)
2243{
2244 struct sci_port *sci_port = to_sci_port(port);
2245
2246 switch (state) {
Geert Uytterhoevend3dfe5d2014-03-11 11:11:20 +01002247 case UART_PM_STATE_OFF:
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002248 sci_port_disable(sci_port);
2249 break;
2250 default:
2251 sci_port_enable(sci_port);
2252 break;
2253 }
2254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static const char *sci_type(struct uart_port *port)
2257{
2258 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09002259 case PORT_IRDA:
2260 return "irda";
2261 case PORT_SCI:
2262 return "sci";
2263 case PORT_SCIF:
2264 return "scif";
2265 case PORT_SCIFA:
2266 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00002267 case PORT_SCIFB:
2268 return "scifb";
Ulrich Hechtf303b362013-05-31 17:57:01 +02002269 case PORT_HSCIF:
2270 return "hscif";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272
Paul Mundtfa439722008-09-04 18:53:58 +09002273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Paul Mundtf6e94952011-01-21 15:25:36 +09002276static int sci_remap_port(struct uart_port *port)
2277{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002278 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002279
2280 /*
2281 * Nothing to do if there's already an established membase.
2282 */
2283 if (port->membase)
2284 return 0;
2285
2286 if (port->flags & UPF_IOREMAP) {
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002287 port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
Paul Mundtf6e94952011-01-21 15:25:36 +09002288 if (unlikely(!port->membase)) {
2289 dev_err(port->dev, "can't remap port#%d\n", port->line);
2290 return -ENXIO;
2291 }
2292 } else {
2293 /*
2294 * For the simple (and majority of) cases where we don't
2295 * need to do any remapping, just cast the cookie
2296 * directly.
2297 */
Jingoo Han3af4e962014-02-05 09:56:37 +09002298 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
Paul Mundtf6e94952011-01-21 15:25:36 +09002299 }
2300
2301 return 0;
2302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static void sci_release_port(struct uart_port *port)
2305{
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002306 struct sci_port *sport = to_sci_port(port);
2307
Paul Mundte2651642011-01-20 21:24:03 +09002308 if (port->flags & UPF_IOREMAP) {
2309 iounmap(port->membase);
2310 port->membase = NULL;
2311 }
2312
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002313 release_mem_region(port->mapbase, sport->reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
2316static int sci_request_port(struct uart_port *port)
2317{
Paul Mundte2651642011-01-20 21:24:03 +09002318 struct resource *res;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002319 struct sci_port *sport = to_sci_port(port);
Paul Mundtf6e94952011-01-21 15:25:36 +09002320 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002322 res = request_mem_region(port->mapbase, sport->reg_size,
2323 dev_name(port->dev));
2324 if (unlikely(res == NULL)) {
2325 dev_err(port->dev, "request_mem_region failed.");
Paul Mundte2651642011-01-20 21:24:03 +09002326 return -EBUSY;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Paul Mundtf6e94952011-01-21 15:25:36 +09002329 ret = sci_remap_port(port);
2330 if (unlikely(ret != 0)) {
2331 release_resource(res);
2332 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002333 }
Paul Mundte2651642011-01-20 21:24:03 +09002334
2335 return 0;
2336}
2337
2338static void sci_config_port(struct uart_port *port, int flags)
2339{
2340 if (flags & UART_CONFIG_TYPE) {
2341 struct sci_port *sport = to_sci_port(port);
2342
2343 port->type = sport->cfg->type;
2344 sci_request_port(port);
2345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (ser->baud_base < 2400)
2351 /* No paper tape reader for Mitch.. */
2352 return -EINVAL;
2353
2354 return 0;
2355}
2356
2357static struct uart_ops sci_uart_ops = {
2358 .tx_empty = sci_tx_empty,
2359 .set_mctrl = sci_set_mctrl,
2360 .get_mctrl = sci_get_mctrl,
2361 .start_tx = sci_start_tx,
2362 .stop_tx = sci_stop_tx,
2363 .stop_rx = sci_stop_rx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .break_ctl = sci_break_ctl,
2365 .startup = sci_startup,
2366 .shutdown = sci_shutdown,
2367 .set_termios = sci_set_termios,
Teppei Kamijou0174e5c2012-11-16 10:51:55 +09002368 .pm = sci_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .type = sci_type,
2370 .release_port = sci_release_port,
2371 .request_port = sci_request_port,
2372 .config_port = sci_config_port,
2373 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002374#ifdef CONFIG_CONSOLE_POLL
2375 .poll_get_char = sci_poll_get_char,
2376 .poll_put_char = sci_poll_put_char,
2377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378};
2379
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002380static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2381{
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002382 const char *clk_names[] = {
2383 [SCI_FCK] = "fck",
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002384 [SCI_SCK] = "sck",
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002385 };
2386 struct clk *clk;
2387 unsigned int i;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002388
Geert Uytterhoeven6af27bf2015-11-18 11:12:26 +01002389 if (sci_port->cfg->type == PORT_HSCIF)
2390 clk_names[SCI_SCK] = "hsck";
2391
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002392 for (i = 0; i < SCI_NUM_CLKS; i++) {
2393 clk = devm_clk_get(dev, clk_names[i]);
2394 if (PTR_ERR(clk) == -EPROBE_DEFER)
2395 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002396
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002397 if (IS_ERR(clk) && i == SCI_FCK) {
2398 /*
2399 * "fck" used to be called "sci_ick", and we need to
2400 * maintain DT backward compatibility.
2401 */
2402 clk = devm_clk_get(dev, "sci_ick");
2403 if (PTR_ERR(clk) == -EPROBE_DEFER)
2404 return -EPROBE_DEFER;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002405
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002406 if (!IS_ERR(clk))
2407 goto found;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002408
Geert Uytterhoevenf4998e52015-10-26 09:58:16 +01002409 /* SH has historically named the clock "sci_fck". */
2410 clk = devm_clk_get(dev, "sci_fck");
2411 if (!IS_ERR(clk))
2412 goto found;
2413
2414 /*
2415 * Not all SH platforms declare a clock lookup entry
2416 * for SCI devices, in which case we need to get the
2417 * global "peripheral_clk" clock.
2418 */
2419 clk = devm_clk_get(dev, "peripheral_clk");
2420 if (!IS_ERR(clk))
2421 goto found;
2422
2423 dev_err(dev, "failed to get %s (%ld)\n", clk_names[i],
2424 PTR_ERR(clk));
2425 return PTR_ERR(clk);
2426 }
2427
2428found:
2429 if (IS_ERR(clk))
2430 dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i],
2431 PTR_ERR(clk));
2432 else
2433 dev_dbg(dev, "clk %s is %pC rate %pCr\n", clk_names[i],
2434 clk, clk);
2435 sci_port->clks[i] = IS_ERR(clk) ? NULL : clk;
2436 }
2437 return 0;
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002438}
2439
Bill Pemberton9671f092012-11-19 13:21:50 -05002440static int sci_init_single(struct platform_device *dev,
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002441 struct sci_port *sci_port, unsigned int index,
2442 struct plat_sci_port *p, bool early)
Paul Mundte108b2c2006-09-27 16:32:13 +09002443{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002444 struct uart_port *port = &sci_port->port;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002445 const struct resource *res;
2446 unsigned int i;
Paul Mundt3127c6b2011-06-28 13:44:37 +09002447 int ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002448
Paul Mundt50f09592011-12-02 20:09:48 +09002449 sci_port->cfg = p;
2450
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002451 port->ops = &sci_uart_ops;
2452 port->iotype = UPIO_MEM;
2453 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09002454
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002455 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2456 if (res == NULL)
2457 return -ENOMEM;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002458
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002459 port->mapbase = res->start;
Yoshinori Satoe4d6f912015-05-16 23:57:31 +09002460 sci_port->reg_size = resource_size(res);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002461
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002462 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2463 sci_port->irqs[i] = platform_get_irq(dev, i);
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002464
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002465 /* The SCI generates several interrupts. They can be muxed together or
2466 * connected to different interrupt lines. In the muxed case only one
2467 * interrupt resource is specified. In the non-muxed case three or four
2468 * interrupt resources are specified, as the BRI interrupt is optional.
2469 */
2470 if (sci_port->irqs[0] < 0)
2471 return -ENXIO;
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002472
Laurent Pinchart89b5c1a2013-12-06 10:59:52 +01002473 if (sci_port->irqs[1] < 0) {
2474 sci_port->irqs[1] = sci_port->irqs[0];
2475 sci_port->irqs[2] = sci_port->irqs[0];
2476 sci_port->irqs[3] = sci_port->irqs[0];
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002477 }
2478
Paul Mundt3127c6b2011-06-28 13:44:37 +09002479 if (p->regtype == SCIx_PROBE_REGTYPE) {
2480 ret = sci_probe_regmap(p);
Rafael J. Wysockifc971142011-08-08 00:26:50 +02002481 if (unlikely(ret))
Paul Mundt3127c6b2011-06-28 13:44:37 +09002482 return ret;
2483 }
Paul Mundt61a69762011-06-14 12:40:19 +09002484
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002485 switch (p->type) {
2486 case PORT_SCIFB:
2487 port->fifosize = 256;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002488 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002489 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002490 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002491 break;
2492 case PORT_HSCIF:
2493 port->fifosize = 128;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002494 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002495 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002496 sci_port->sampling_rate = 0;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002497 break;
2498 case PORT_SCIFA:
2499 port->fifosize = 64;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002500 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002501 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002502 sci_port->sampling_rate = 16;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002503 break;
2504 case PORT_SCIF:
2505 port->fifosize = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002506 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002507 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002508 sci_port->overrun_mask = SCIFA_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002509 sci_port->sampling_rate = 16;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002510 } else {
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002511 sci_port->overrun_reg = SCLSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002512 sci_port->overrun_mask = SCLSR_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002513 sci_port->sampling_rate = 32;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002514 }
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002515 break;
2516 default:
2517 port->fifosize = 1;
Geert Uytterhoeven2e0842a2015-04-30 18:21:32 +02002518 sci_port->overrun_reg = SCxSR;
Geert Uytterhoeven75c249f2015-04-30 18:21:31 +02002519 sci_port->overrun_mask = SCI_ORER;
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002520 sci_port->sampling_rate = 32;
Laurent Pinchartb545e4f2013-12-06 10:59:19 +01002521 break;
2522 }
2523
Laurent Pinchart878fbb912013-12-06 10:59:51 +01002524 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
2525 * match the SoC datasheet, this should be investigated. Let platform
2526 * data override the sampling rate for now.
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002527 */
Geert Uytterhoevenf84b6bd2015-08-21 20:02:31 +02002528 if (p->sampling_rate)
2529 sci_port->sampling_rate = p->sampling_rate;
Laurent Pinchartec09c5e2013-12-06 10:59:20 +01002530
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002531 if (!early) {
Laurent Pincharta9ec81f2015-09-14 15:14:23 +03002532 ret = sci_init_clocks(sci_port, &dev->dev);
2533 if (ret < 0)
2534 return ret;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002535
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002536 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00002537
2538 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002539 }
Paul Mundte108b2c2006-09-27 16:32:13 +09002540
Magnus Damm7ed7e072009-01-21 15:14:14 +00002541 sci_port->break_timer.data = (unsigned long)sci_port;
2542 sci_port->break_timer.function = sci_break_timer;
2543 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09002544
Paul Mundtdebf9502011-06-08 18:19:37 +09002545 /*
2546 * Establish some sensible defaults for the error detection.
2547 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002548 if (p->type == PORT_SCI) {
2549 sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
2550 sci_port->error_clear = SCI_ERROR_CLEAR;
2551 } else {
2552 sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
2553 sci_port->error_clear = SCIF_ERROR_CLEAR;
2554 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002555
2556 /*
Laurent Pinchart3ae988d2013-12-06 10:59:17 +01002557 * Make the error mask inclusive of overrun detection, if
2558 * supported.
2559 */
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002560 if (sci_port->overrun_reg == SCxSR) {
Geert Uytterhoevenafd66db2015-04-30 18:21:33 +02002561 sci_port->error_mask |= sci_port->overrun_mask;
Geert Uytterhoeven5da0f462015-08-21 20:02:27 +02002562 sci_port->error_clear &= ~sci_port->overrun_mask;
2563 }
Paul Mundtdebf9502011-06-08 18:19:37 +09002564
Paul Mundtce6738b2011-01-19 15:24:40 +09002565 port->type = p->type;
Laurent Pinchartb6e4a3f2013-12-06 10:59:14 +01002566 port->flags = UPF_FIXED_PORT | p->flags;
Paul Mundt61a69762011-06-14 12:40:19 +09002567 port->regshift = p->regshift;
Paul Mundtce6738b2011-01-19 15:24:40 +09002568
2569 /*
Paul Mundt61a69762011-06-14 12:40:19 +09002570 * The UART port needs an IRQ value, so we peg this to the RX IRQ
Paul Mundtce6738b2011-01-19 15:24:40 +09002571 * for the multi-IRQ ports, which is where we are primarily
2572 * concerned with the shutdown path synchronization.
2573 *
2574 * For the muxed case there's nothing more to do.
2575 */
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002576 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
Yong Zhang9cfb5c02011-09-22 16:59:15 +08002577 port->irqflags = 0;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09002578
Paul Mundt61a69762011-06-14 12:40:19 +09002579 port->serial_in = sci_serial_in;
2580 port->serial_out = sci_serial_out;
2581
Guennadi Liakhovetski937bb6e2011-06-24 13:56:15 +02002582 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2583 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2584 p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00002585
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002586 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09002587}
2588
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002589static void sci_cleanup_single(struct sci_port *port)
2590{
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002591 pm_runtime_disable(port->port.dev);
2592}
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002595static void serial_console_putchar(struct uart_port *port, int ch)
2596{
2597 sci_poll_put_char(port, ch);
2598}
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600/*
2601 * Print a string to the serial port trying not to disturb
2602 * any possible real use of the port...
2603 */
2604static void serial_console_write(struct console *co, const char *s,
2605 unsigned count)
2606{
Paul Mundt906b17d2011-01-21 16:19:53 +09002607 struct sci_port *sci_port = &sci_ports[co->index];
2608 struct uart_port *port = &sci_port->port;
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002609 unsigned short bits, ctrl, ctrl_temp;
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002610 unsigned long flags;
2611 int locked = 1;
2612
2613 local_irq_save(flags);
2614 if (port->sysrq)
2615 locked = 0;
2616 else if (oops_in_progress)
2617 locked = spin_trylock(&port->lock);
2618 else
2619 spin_lock(&port->lock);
2620
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002621 /* first save SCSCR then disable interrupts, keep clock source */
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002622 ctrl = serial_port_in(port, SCSCR);
Geert Uytterhoevena67969b2015-11-18 16:20:44 +01002623 ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
2624 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
2625 serial_port_out(port, SCSCR, ctrl_temp);
Paul Mundt07d2a1a2008-12-11 19:06:43 +09002626
Magnus Damm501b8252009-01-21 15:14:30 +00002627 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09002628
2629 /* wait until fifo is empty and last bit has been transmitted */
2630 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
Paul Mundtb12bb292012-03-30 19:50:15 +09002631 while ((serial_port_in(port, SCxSR) & bits) != bits)
Magnus Damm973e5d52009-02-24 15:57:12 +09002632 cpu_relax();
Shinya Kuribayashi40f70c02012-11-16 10:54:15 +09002633
2634 /* restore the SCSCR */
2635 serial_port_out(port, SCSCR, ctrl);
2636
2637 if (locked)
2638 spin_unlock(&port->lock);
2639 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
Bill Pemberton9671f092012-11-19 13:21:50 -05002642static int serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00002644 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 struct uart_port *port;
2646 int baud = 115200;
2647 int bits = 8;
2648 int parity = 'n';
2649 int flow = 'n';
2650 int ret;
2651
Paul Mundte108b2c2006-09-27 16:32:13 +09002652 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09002653 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09002654 */
Paul Mundt906b17d2011-01-21 16:19:53 +09002655 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09002656 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09002657
Paul Mundt906b17d2011-01-21 16:19:53 +09002658 sci_port = &sci_ports[co->index];
2659 port = &sci_port->port;
2660
Alexandre Courbotb2267a62011-02-09 03:18:46 +00002661 /*
2662 * Refuse to handle uninitialized ports.
2663 */
2664 if (!port->ops)
2665 return -ENODEV;
2666
Paul Mundtf6e94952011-01-21 15:25:36 +09002667 ret = sci_remap_port(port);
2668 if (unlikely(ret != 0))
2669 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (options)
2672 uart_parse_options(options, &baud, &parity, &bits, &flow);
2673
Paul Mundtab7cfb52011-06-01 14:47:42 +09002674 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677static struct console serial_console = {
2678 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09002679 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .write = serial_console_write,
2681 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09002682 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09002684 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685};
2686
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002687static struct console early_serial_console = {
2688 .name = "early_ttySC",
2689 .write = serial_console_write,
2690 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09002691 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002692};
Paul Mundtecdf8a42011-01-21 00:05:48 +09002693
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002694static char early_serial_buf[32];
2695
Bill Pemberton9671f092012-11-19 13:21:50 -05002696static int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002697{
Jingoo Han574de552013-07-30 17:06:57 +09002698 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002699
2700 if (early_serial_console.data)
2701 return -EEXIST;
2702
2703 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002704
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002705 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
Paul Mundtecdf8a42011-01-21 00:05:48 +09002706
2707 serial_console_setup(&early_serial_console, early_serial_buf);
2708
2709 if (!strstr(early_serial_buf, "keep"))
2710 early_serial_console.flags |= CON_BOOT;
2711
2712 register_console(&early_serial_console);
2713 return 0;
2714}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002715
2716#define SCI_CONSOLE (&serial_console)
2717
Paul Mundtecdf8a42011-01-21 00:05:48 +09002718#else
Bill Pemberton9671f092012-11-19 13:21:50 -05002719static inline int sci_probe_earlyprintk(struct platform_device *pdev)
Paul Mundtecdf8a42011-01-21 00:05:48 +09002720{
2721 return -EINVAL;
2722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00002724#define SCI_CONSOLE NULL
2725
2726#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002728static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730static struct uart_driver sci_uart_driver = {
2731 .owner = THIS_MODULE,
2732 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .dev_name = "ttySC",
2734 .major = SCI_MAJOR,
2735 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09002736 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .cons = SCI_CONSOLE,
2738};
2739
Paul Mundt54507f62009-05-08 23:48:33 +09002740static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00002741{
Paul Mundtd535a232011-01-19 17:19:35 +09002742 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002743
Paul Mundtd535a232011-01-19 17:19:35 +09002744 cpufreq_unregister_notifier(&port->freq_transition,
2745 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00002746
Paul Mundtd535a232011-01-19 17:19:35 +09002747 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00002748
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002749 sci_cleanup_single(port);
Paul Mundtd535a232011-01-19 17:19:35 +09002750
Magnus Damme552de22009-01-21 15:13:42 +00002751 return 0;
2752}
2753
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002754
2755#define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
2756#define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
2757#define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002758
2759static const struct of_device_id of_sci_match[] = {
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002760 /* SoC-specific types */
2761 {
2762 .compatible = "renesas,scif-r7s72100",
2763 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
2764 },
Geert Uytterhoeven9ed44bb2015-11-10 18:57:23 +01002765 /* Family-specific types */
2766 {
2767 .compatible = "renesas,rcar-gen1-scif",
2768 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2769 }, {
2770 .compatible = "renesas,rcar-gen2-scif",
2771 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2772 }, {
2773 .compatible = "renesas,rcar-gen3-scif",
2774 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2775 },
Geert Uytterhoevenf443ff82015-11-10 16:16:54 +01002776 /* Generic types */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002777 {
2778 .compatible = "renesas,scif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002779 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002780 }, {
2781 .compatible = "renesas,scifa",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002782 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002783 }, {
2784 .compatible = "renesas,scifb",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002785 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002786 }, {
2787 .compatible = "renesas,hscif",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002788 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002789 }, {
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002790 .compatible = "renesas,sci",
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002791 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
Yoshinori Satoe1d0be62015-01-28 02:53:55 +09002792 }, {
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002793 /* Terminator */
2794 },
2795};
2796MODULE_DEVICE_TABLE(of, of_sci_match);
2797
2798static struct plat_sci_port *
2799sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
2800{
2801 struct device_node *np = pdev->dev.of_node;
2802 const struct of_device_id *match;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002803 struct plat_sci_port *p;
2804 int id;
2805
2806 if (!IS_ENABLED(CONFIG_OF) || !np)
2807 return NULL;
2808
Geert Uytterhoeven495bb472015-12-10 16:02:17 +01002809 match = of_match_node(of_sci_match, np);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002810 if (!match)
2811 return NULL;
2812
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002813 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
Geert Uytterhoeven42054632015-08-21 20:02:34 +02002814 if (!p)
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002815 return NULL;
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002816
Geert Uytterhoeven2095fc72015-11-12 13:39:49 +01002817 /* Get the line number from the aliases node. */
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002818 id = of_alias_get_id(np, "serial");
2819 if (id < 0) {
2820 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
2821 return NULL;
2822 }
2823
2824 *dev_id = id;
2825
2826 p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Geert Uytterhoevenbd2238f2015-11-10 16:09:23 +01002827 p->type = SCI_OF_TYPE(match->data);
2828 p->regtype = SCI_OF_REGTYPE(match->data);
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002829 p->scscr = SCSCR_RE | SCSCR_TE;
2830
2831 return p;
2832}
2833
Bill Pemberton9671f092012-11-19 13:21:50 -05002834static int sci_probe_single(struct platform_device *dev,
Magnus Damm0ee70712009-01-21 15:13:50 +00002835 unsigned int index,
2836 struct plat_sci_port *p,
2837 struct sci_port *sciport)
2838{
Magnus Damm0ee70712009-01-21 15:13:50 +00002839 int ret;
2840
2841 /* Sanity check */
2842 if (unlikely(index >= SCI_NPORTS)) {
Joe Perches9b971cd2014-03-11 10:10:46 -07002843 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
Magnus Damm0ee70712009-01-21 15:13:50 +00002844 index+1, SCI_NPORTS);
Joe Perches9b971cd2014-03-11 10:10:46 -07002845 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
Laurent Pinchartb6c5ef62012-06-13 00:28:24 +02002846 return -EINVAL;
Magnus Damm0ee70712009-01-21 15:13:50 +00002847 }
2848
Laurent Pinchart1fcc91a2013-12-06 10:59:16 +01002849 ret = sci_init_single(dev, sciport, index, p, false);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09002850 if (ret)
2851 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002852
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002853 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
2854 if (ret) {
2855 sci_cleanup_single(sciport);
2856 return ret;
2857 }
2858
2859 return 0;
Magnus Damm0ee70712009-01-21 15:13:50 +00002860}
2861
Bill Pemberton9671f092012-11-19 13:21:50 -05002862static int sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002864 struct plat_sci_port *p;
2865 struct sci_port *sp;
2866 unsigned int dev_id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09002867 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002868
Paul Mundtecdf8a42011-01-21 00:05:48 +09002869 /*
2870 * If we've come here via earlyprintk initialization, head off to
2871 * the special early probe. We don't have sufficient device state
2872 * to make it beyond this yet.
2873 */
2874 if (is_early_platform_device(dev))
2875 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002876
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002877 if (dev->dev.of_node) {
2878 p = sci_parse_dt(dev, &dev_id);
2879 if (p == NULL)
2880 return -EINVAL;
2881 } else {
2882 p = dev->dev.platform_data;
2883 if (p == NULL) {
2884 dev_err(&dev->dev, "no platform data supplied\n");
2885 return -EINVAL;
2886 }
2887
2888 dev_id = dev->id;
2889 }
2890
2891 sp = &sci_ports[dev_id];
Paul Mundtd535a232011-01-19 17:19:35 +09002892 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002893
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002894 ret = sci_probe_single(dev, dev_id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002895 if (ret)
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002896 return ret;
Magnus Damme552de22009-01-21 15:13:42 +00002897
Paul Mundtd535a232011-01-19 17:19:35 +09002898 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Paul Mundtd535a232011-01-19 17:19:35 +09002900 ret = cpufreq_register_notifier(&sp->freq_transition,
2901 CPUFREQ_TRANSITION_NOTIFIER);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002902 if (unlikely(ret < 0)) {
Geert Uytterhoevenbf13c9a2014-02-28 14:21:33 +01002903 uart_remove_one_port(&sci_uart_driver, &sp->port);
Laurent Pinchart6dae1422012-06-13 00:28:23 +02002904 sci_cleanup_single(sp);
2905 return ret;
2906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908#ifdef CONFIG_SH_STANDARD_BIOS
2909 sh_bios_gdb_detach();
2910#endif
2911
Paul Mundte108b2c2006-09-27 16:32:13 +09002912 return 0;
2913}
2914
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002915static __maybe_unused int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002916{
Paul Mundtd535a232011-01-19 17:19:35 +09002917 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002918
Paul Mundtd535a232011-01-19 17:19:35 +09002919 if (sport)
2920 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002921
2922 return 0;
2923}
2924
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002925static __maybe_unused int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002926{
Paul Mundtd535a232011-01-19 17:19:35 +09002927 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002928
Paul Mundtd535a232011-01-19 17:19:35 +09002929 if (sport)
2930 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002931
2932 return 0;
2933}
2934
Sergei Shtylyovcb876342015-01-16 13:56:02 -08002935static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
Paul Mundt6daa79b2009-06-15 07:07:38 +09002936
Paul Mundte108b2c2006-09-27 16:32:13 +09002937static struct platform_driver sci_driver = {
2938 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002939 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002940 .driver = {
2941 .name = "sh-sci",
Paul Mundt6daa79b2009-06-15 07:07:38 +09002942 .pm = &sci_dev_pm_ops,
Bastian Hecht20bdcab2013-12-06 10:59:54 +01002943 .of_match_table = of_match_ptr(of_sci_match),
Paul Mundte108b2c2006-09-27 16:32:13 +09002944 },
2945};
2946
2947static int __init sci_init(void)
2948{
2949 int ret;
2950
Geert Uytterhoeven6c13d5d2014-03-11 11:11:17 +01002951 pr_info("%s\n", banner);
Paul Mundte108b2c2006-09-27 16:32:13 +09002952
Paul Mundte108b2c2006-09-27 16:32:13 +09002953 ret = uart_register_driver(&sci_uart_driver);
2954 if (likely(ret == 0)) {
2955 ret = platform_driver_register(&sci_driver);
2956 if (unlikely(ret))
2957 uart_unregister_driver(&sci_uart_driver);
2958 }
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return ret;
2961}
2962
2963static void __exit sci_exit(void)
2964{
Paul Mundte108b2c2006-09-27 16:32:13 +09002965 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 uart_unregister_driver(&sci_uart_driver);
2967}
2968
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002969#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2970early_platform_init_buffer("earlyprintk", &sci_driver,
2971 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973module_init(sci_init);
2974module_exit(sci_exit);
2975
Paul Mundte108b2c2006-09-27 16:32:13 +09002976MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002977MODULE_ALIAS("platform:sh-sci");
Paul Mundt7f405f92011-06-28 13:47:40 +09002978MODULE_AUTHOR("Paul Mundt");
Ulrich Hechtf303b362013-05-31 17:57:01 +02002979MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");