blob: 3248ddaa889df448ee5cc6820f80d1cf2187a1e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
Paul Mundtf43dc232011-01-13 15:06:28 +09004 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09005 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090014 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090020#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#undef DEBUG
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090041#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090042#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/notifier.h>
Magnus Damm5e50d2d2011-04-19 10:38:25 +000044#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090046#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090047#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090048#include <linux/err.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090049#include <linux/dmaengine.h>
50#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090052
53#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090054#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "sh-sci.h"
58
Paul Mundte108b2c2006-09-27 16:32:13 +090059struct sci_port {
60 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Paul Mundtce6738b2011-01-19 15:24:40 +090062 /* Platform configuration */
63 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090064
Paul Mundte108b2c2006-09-27 16:32:13 +090065 /* Port enable callback */
66 void (*enable)(struct uart_port *port);
67
68 /* Port disable callback */
69 void (*disable)(struct uart_port *port);
70
71 /* Break timer */
72 struct timer_list break_timer;
73 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090074
Magnus Damm501b8252009-01-21 15:14:30 +000075 /* Interface clock */
76 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090077 /* Function clock */
78 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090079
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090080 struct dma_chan *chan_tx;
81 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090082
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090083#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090084 struct dma_async_tx_descriptor *desc_tx;
85 struct dma_async_tx_descriptor *desc_rx[2];
86 dma_cookie_t cookie_tx;
87 dma_cookie_t cookie_rx[2];
88 dma_cookie_t active_rx;
89 struct scatterlist sg_tx;
90 unsigned int sg_len_tx;
91 struct scatterlist sg_rx[2];
92 size_t buf_len_rx;
93 struct sh_dmae_slave param_tx;
94 struct sh_dmae_slave param_rx;
95 struct work_struct work_tx;
96 struct work_struct work_rx;
97 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +000098 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090099#endif
Magnus Damme552de22009-01-21 15:13:42 +0000100
Paul Mundtd535a232011-01-19 17:19:35 +0900101 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900105static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100106static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900107static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Paul Mundte108b2c2006-09-27 16:32:13 +0900109#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
110
111static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct uart_driver sci_uart_driver;
113
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900114static inline struct sci_port *
115to_sci_port(struct uart_port *uart)
116{
117 return container_of(uart, struct sci_port, port);
118}
119
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900120#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900121
122#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900123static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned short status;
126 int c;
127
Paul Mundte108b2c2006-09-27 16:32:13 +0900128 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 status = sci_in(port, SCxSR);
130 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900131 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 continue;
133 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500134 break;
135 } while (1);
136
137 if (!(status & SCxSR_RDxF(port)))
138 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900141
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900142 /* Dummy read */
143 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return c;
147}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900150static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned short status;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 do {
155 status = sci_in(port, SCxSR);
156 } while (!(status & SCxSR_TDxE(port)));
157
Paul Mundt272966c2008-11-13 17:46:06 +0900158 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000159 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900161#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Paul Mundtab7cfb52011-06-01 14:47:42 +0900163#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
Paul Mundtd5701642008-12-16 20:07:27 +0900164static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900165{
Paul Mundtd5701642008-12-16 20:07:27 +0900166 if (port->mapbase == 0xA4400000) {
167 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
168 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
169 } else if (port->mapbase == 0xA4410000)
170 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900171}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900172#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900173static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900174{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900175 unsigned short data;
176
177 if (cflag & CRTSCTS) {
178 /* enable RTS/CTS */
179 if (port->mapbase == 0xa4430000) { /* SCIF0 */
180 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900181 data = __raw_readw(PORT_PTCR);
182 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900183 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
184 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900185 data = __raw_readw(PORT_PVCR);
186 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900187 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900188 } else {
189 if (port->mapbase == 0xa4430000) { /* SCIF0 */
190 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900191 data = __raw_readw(PORT_PTCR);
192 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900193 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
194 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900195 data = __raw_readw(PORT_PVCR);
196 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900197 }
198 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900199}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800200#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900201/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900202static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800204 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Paul Mundtb7a76e42006-02-01 03:06:06 -0800206 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900207 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800208 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900209 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800210
Paul Mundtd5701642008-12-16 20:07:27 +0900211 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900213 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Clear out SCP7MD1,0, SCP4MD1,0,
215 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900216 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Paul Mundt32b53072009-12-24 14:52:43 +0900218 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900220 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
Paul Mundt41504c32006-12-11 20:28:03 +0900223#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900224static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900225{
Magnus Damm346b7462008-04-23 21:25:29 +0900226 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900227
Magnus Damm346b7462008-04-23 21:25:29 +0900228 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900229 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900230 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900231 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900232 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900233
Paul Mundtd5701642008-12-16 20:07:27 +0900234 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900235 }
Paul Mundt41504c32006-12-11 20:28:03 +0900236}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900237#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
238 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900239 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900240 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900241 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900242 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900243static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
244{
245 if (!(cflag & CRTSCTS))
246 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
247}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900248#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900249static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
250{
251 if (!(cflag & CRTSCTS))
252 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
253}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800254#else
Paul Mundtd5701642008-12-16 20:07:27 +0900255static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
256{
257 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Paul Mundte108b2c2006-09-27 16:32:13 +0900259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Paul Mundt32351a22007-03-12 14:38:59 +0900261#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
262 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900263 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
264 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900265static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900266{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900267 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900268}
269
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900270static int scif_txroom(struct uart_port *port)
271{
272 return SCIF_TXROOM_MAX - scif_txfill(port);
273}
274
275static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900276{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900277 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900278}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900279#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900280static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900281{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900282 if (port->mapbase == 0xffe00000 ||
283 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900284 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900285 return sci_in(port, SCTFDR) & 0xff;
286 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900287 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900288 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900289}
290
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900291static int scif_txroom(struct uart_port *port)
292{
293 if (port->mapbase == 0xffe00000 ||
294 port->mapbase == 0xffe08000)
295 /* SCIF0/1*/
296 return SCIF_TXROOM_MAX - scif_txfill(port);
297 else
298 /* SCIF2 */
299 return SCIF2_TXROOM_MAX - scif_txfill(port);
300}
301
302static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900303{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900304 if ((port->mapbase == 0xffe00000) ||
305 (port->mapbase == 0xffe08000)) {
306 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900307 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900308 } else {
309 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900310 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900311 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900312}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000313#elif defined(CONFIG_ARCH_SH7372)
314static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900315{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000316 if (port->type == PORT_SCIFA)
317 return sci_in(port, SCFDR) >> 8;
318 else
319 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900320}
321
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000322static int scif_txroom(struct uart_port *port)
323{
324 return port->fifosize - scif_txfill(port);
325}
326
327static int scif_rxfill(struct uart_port *port)
328{
329 if (port->type == PORT_SCIFA)
330 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
331 else
332 return sci_in(port, SCRFDR);
333}
Paul Mundte108b2c2006-09-27 16:32:13 +0900334#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900335static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900336{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900337 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900338}
339
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900340static int scif_txroom(struct uart_port *port)
341{
342 return SCIF_TXROOM_MAX - scif_txfill(port);
343}
344
345static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900346{
347 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900351static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900352{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900353 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900354}
355
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900356static int sci_txroom(struct uart_port *port)
357{
358 return !sci_txfill(port);
359}
360
361static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900362{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900363 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900364}
365
Paul Mundt514820e2011-06-08 18:51:32 +0900366/*
367 * SCI helper for checking the state of the muxed port/RXD pins.
368 */
369static inline int sci_rxd_in(struct uart_port *port)
370{
371 struct sci_port *s = to_sci_port(port);
372
373 if (s->cfg->port_reg <= 0)
374 return 1;
375
376 return !!__raw_readb(s->cfg->port_reg);
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/* ********************************************************************** *
380 * the interrupt related routines *
381 * ********************************************************************** */
382
383static void sci_transmit_chars(struct uart_port *port)
384{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700385 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned short status;
388 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900389 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 status = sci_in(port, SCxSR);
392 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900394 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900395 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900396 else
Paul Mundt8e698612009-06-24 19:44:32 +0900397 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return;
400 }
401
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900402 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900403 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900404 else
405 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 do {
408 unsigned char c;
409
410 if (port->x_char) {
411 c = port->x_char;
412 port->x_char = 0;
413 } else if (!uart_circ_empty(xmit) && !stopped) {
414 c = xmit->buf[xmit->tail];
415 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
416 } else {
417 break;
418 }
419
420 sci_out(port, SCxTDR, c);
421
422 port->icount.tx++;
423 } while (--count > 0);
424
425 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
426
427 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
428 uart_write_wakeup(port);
429 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100430 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ctrl = sci_in(port, SCSCR);
433
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900434 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sci_in(port, SCxSR); /* Dummy read */
436 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Paul Mundt8e698612009-06-24 19:44:32 +0900439 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442}
443
444/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900445#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900447static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900449 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700450 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int i, count, copied = 0;
452 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800453 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 status = sci_in(port, SCxSR);
456 if (!(status & SCxSR_RDxF(port)))
457 return;
458
459 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900460 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900461 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900462 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900463 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800466 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* If for any reason we can't copy more data, we're done! */
469 if (count == 0)
470 break;
471
472 if (port->type == PORT_SCI) {
473 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900474 if (uart_handle_sysrq_char(port, c) ||
475 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900477 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900478 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900480 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 char c = sci_in(port, SCxRDR);
482 status = sci_in(port, SCxSR);
483#if defined(CONFIG_CPU_SH3)
484 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900485 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if ((c == 0) &&
487 (status & SCxSR_FER(port))) {
488 count--; i--;
489 continue;
490 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900493 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900494 sci_port->break_flag = 0;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (STEPFN(c)) {
497 count--; i--;
498 continue;
499 }
500 }
501#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100502 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 count--; i--;
504 continue;
505 }
506
507 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900508 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800509 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900510 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900511 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800512 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900513 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800514 } else
515 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900516
Alan Cox33f0f882006-01-09 20:54:13 -0800517 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 }
520
521 sci_in(port, SCxSR); /* dummy read */
522 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 copied += count;
525 port->icount.rx += count;
526 }
527
528 if (copied) {
529 /* Tell the rest of the system the news. New characters! */
530 tty_flip_buffer_push(tty);
531 } else {
532 sci_in(port, SCxSR); /* dummy read */
533 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
534 }
535}
536
537#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900538
539/*
540 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * 1 per millisecond or so during the break period, for 9600 baud.
542 * So dont bother disabling interrupts.
543 * But dont want more than 1 break event.
544 * Use a kernel timer to periodically poll the rx line until
545 * the break is finished.
546 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900547static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900549 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/* Ensure that two consecutive samples find the break over. */
553static void sci_break_timer(unsigned long data)
554{
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 struct sci_port *port = (struct sci_port *)data;
556
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000557 if (port->enable)
558 port->enable(&port->port);
559
Paul Mundte108b2c2006-09-27 16:32:13 +0900560 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900562 sci_schedule_break_timer(port);
563 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* break is over. */
565 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900566 sci_schedule_break_timer(port);
567 } else
568 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000569
570 if (port->disable)
571 port->disable(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900574static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 int copied = 0;
577 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700578 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900579 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Paul Mundtdebf9502011-06-08 18:19:37 +0900581 /*
582 * Handle overruns, if supported.
583 */
584 if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
585 if (status & (1 << s->cfg->overrun_bit)) {
586 /* overrun error */
587 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
588 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900589
Paul Mundtdebf9502011-06-08 18:19:37 +0900590 dev_notice(port->dev, "overrun error");
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Paul Mundte108b2c2006-09-27 16:32:13 +0900594 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (sci_rxd_in(port) == 0) {
596 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900597 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900598
599 if (!sci_port->break_flag) {
600 sci_port->break_flag = 1;
601 sci_schedule_break_timer(sci_port);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900604 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900606
607 dev_dbg(port->dev, "BREAK detected\n");
608
Paul Mundte108b2c2006-09-27 16:32:13 +0900609 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900610 copied++;
611 }
612
Paul Mundte108b2c2006-09-27 16:32:13 +0900613 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900615 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800616 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900617
618 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
621
Paul Mundte108b2c2006-09-27 16:32:13 +0900622 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900624 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
625 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900626
627 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
Alan Cox33f0f882006-01-09 20:54:13 -0800630 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return copied;
634}
635
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900636static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900637{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700638 struct tty_struct *tty = port->state->port.tty;
Paul Mundtdebf9502011-06-08 18:19:37 +0900639 struct sci_port *s = to_sci_port(port);
Paul Mundtd830fa42008-12-16 19:29:38 +0900640 int copied = 0;
641
Paul Mundtdebf9502011-06-08 18:19:37 +0900642 /*
643 * XXX: Technically not limited to non-SCIFs, it's simply the
644 * SCLSR check that is for the moment SCIF-specific. This
645 * probably wants to be revisited for SCIFA/B as well as for
646 * factoring in SCI overrun detection.
647 */
Paul Mundtd830fa42008-12-16 19:29:38 +0900648 if (port->type != PORT_SCIF)
649 return 0;
650
Paul Mundtdebf9502011-06-08 18:19:37 +0900651 if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
Paul Mundtd830fa42008-12-16 19:29:38 +0900652 sci_out(port, SCLSR, 0);
653
654 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
655 tty_flip_buffer_push(tty);
656
657 dev_notice(port->dev, "overrun error\n");
658 copied++;
659 }
660
661 return copied;
662}
663
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900664static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int copied = 0;
667 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700668 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000669 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900671 if (uart_handle_break(port))
672 return 0;
673
Paul Mundtb7a76e42006-02-01 03:06:06 -0800674 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675#if defined(CONFIG_CPU_SH3)
676 /* Debounce break */
677 s->break_flag = 1;
678#endif
679 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900680 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800681 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900682
683 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Alan Cox33f0f882006-01-09 20:54:13 -0800686 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900688
Paul Mundtd830fa42008-12-16 19:29:38 +0900689 copied += sci_handle_fifo_overrun(port);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return copied;
692}
693
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900694static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900696#ifdef CONFIG_SERIAL_SH_SCI_DMA
697 struct uart_port *port = ptr;
698 struct sci_port *s = to_sci_port(port);
699
700 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900701 u16 scr = sci_in(port, SCSCR);
702 u16 ssr = sci_in(port, SCxSR);
703
704 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000705 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000706 disable_irq_nosync(irq);
707 scr |= 0x4000;
708 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900709 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000710 }
711 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900712 /* Clear current interrupt */
713 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000714 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
715 jiffies, s->rx_timeout);
716 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900717
718 return IRQ_HANDLED;
719 }
720#endif
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* I think sci_receive_chars has to be called irrespective
723 * of whether the I_IXOFF is set, otherwise, how is the interrupt
724 * to be disabled?
725 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900726 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 return IRQ_HANDLED;
729}
730
David Howells7d12e782006-10-05 14:55:46 +0100731static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900734 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Stuart Menefyfd78a762009-07-29 23:01:24 +0900736 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900738 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 return IRQ_HANDLED;
741}
742
David Howells7d12e782006-10-05 14:55:46 +0100743static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct uart_port *port = ptr;
746
747 /* Handle errors */
748 if (port->type == PORT_SCI) {
749 if (sci_handle_errors(port)) {
750 /* discard character in rx buffer */
751 sci_in(port, SCxSR);
752 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
753 }
754 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900755 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100756 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
760
761 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100762 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return IRQ_HANDLED;
765}
766
David Howells7d12e782006-10-05 14:55:46 +0100767static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct uart_port *port = ptr;
770
771 /* Handle BREAKs */
772 sci_handle_breaks(port);
773 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
774
775 return IRQ_HANDLED;
776}
777
Paul Mundtf43dc232011-01-13 15:06:28 +0900778static inline unsigned long port_rx_irq_mask(struct uart_port *port)
779{
780 /*
781 * Not all ports (such as SCIFA) will support REIE. Rather than
782 * special-casing the port type, we check the port initialization
783 * IRQ enable mask to see whether the IRQ is desired at all. If
784 * it's unset, it's logically inferred that there's no point in
785 * testing for it.
786 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900787 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900788}
789
David Howells7d12e782006-10-05 14:55:46 +0100790static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Magnus Damm44e18e92009-07-03 08:39:34 +0000792 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900793 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900794 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900795 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900797 ssr_status = sci_in(port, SCxSR);
798 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900799 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900802 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900803 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900804 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900805
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900806 /*
807 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
808 * DR flags
809 */
810 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900811 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900812 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000815 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900816 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000819 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900820 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Michael Trimarchia8884e32008-10-31 16:10:23 +0900822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300826 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * ports' baud rate when the peripheral clock changes.
828 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900829static int sci_notifier(struct notifier_block *self,
830 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Magnus Damme552de22009-01-21 15:13:42 +0000832 struct sci_port *sci_port;
833 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Paul Mundtd535a232011-01-19 17:19:35 +0900835 sci_port = container_of(self, struct sci_port, freq_transition);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000838 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900839 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900840
Paul Mundtd535a232011-01-19 17:19:35 +0900841 spin_lock_irqsave(&port->lock, flags);
842 port->uartclk = clk_get_rate(sci_port->iclk);
843 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return NOTIFY_OK;
847}
Magnus Damm501b8252009-01-21 15:14:30 +0000848
849static void sci_clk_enable(struct uart_port *port)
850{
851 struct sci_port *sci_port = to_sci_port(port);
852
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000853 pm_runtime_get_sync(port->dev);
854
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900855 clk_enable(sci_port->iclk);
856 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
857 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000858}
859
860static void sci_clk_disable(struct uart_port *port)
861{
862 struct sci_port *sci_port = to_sci_port(port);
863
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900864 clk_disable(sci_port->fclk);
865 clk_disable(sci_port->iclk);
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000866
867 pm_runtime_put_sync(port->dev);
Magnus Damm501b8252009-01-21 15:14:30 +0000868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870static int sci_request_irq(struct sci_port *port)
871{
872 int i;
David Howells7d12e782006-10-05 14:55:46 +0100873 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
875 sci_br_interrupt,
876 };
877 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
878 "SCI Transmit Data Empty", "SCI Break" };
879
Paul Mundtce6738b2011-01-19 15:24:40 +0900880 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
881 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900883
Paul Mundtce6738b2011-01-19 15:24:40 +0900884 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900885 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900886 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -ENODEV;
888 }
889 } else {
890 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900891 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900893
Paul Mundtce6738b2011-01-19 15:24:40 +0900894 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900895 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900896 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -ENODEV;
898 }
899 }
900 }
901
902 return 0;
903}
904
905static void sci_free_irq(struct sci_port *port)
906{
907 int i;
908
Paul Mundtce6738b2011-01-19 15:24:40 +0900909 if (port->cfg->irqs[0] == port->cfg->irqs[1])
910 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +0900911 else {
Paul Mundtce6738b2011-01-19 15:24:40 +0900912 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
913 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 continue;
915
Paul Mundtce6738b2011-01-19 15:24:40 +0900916 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919}
920
921static unsigned int sci_tx_empty(struct uart_port *port)
922{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000923 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900924 unsigned short in_tx_fifo = scif_txfill(port);
925
926 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
930{
931 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
932 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
933 /* If you have signals for DTR and DCD, please implement here. */
934}
935
936static unsigned int sci_get_mctrl(struct uart_port *port)
937{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900938 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 and CTS/RTS */
940
941 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
942}
943
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900944#ifdef CONFIG_SERIAL_SH_SCI_DMA
945static void sci_dma_tx_complete(void *arg)
946{
947 struct sci_port *s = arg;
948 struct uart_port *port = &s->port;
949 struct circ_buf *xmit = &port->state->xmit;
950 unsigned long flags;
951
952 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
953
954 spin_lock_irqsave(&port->lock, flags);
955
Magnus Dammf354a382010-03-19 04:47:01 +0000956 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900957 xmit->tail &= UART_XMIT_SIZE - 1;
958
Magnus Dammf354a382010-03-19 04:47:01 +0000959 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900960
961 async_tx_ack(s->desc_tx);
962 s->cookie_tx = -EINVAL;
963 s->desc_tx = NULL;
964
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900965 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
966 uart_write_wakeup(port);
967
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000968 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900969 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000970 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000971 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900972 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000973 }
974
975 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900976}
977
978/* Locking: called with port lock held */
979static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
980 size_t count)
981{
982 struct uart_port *port = &s->port;
983 int i, active, room;
984
985 room = tty_buffer_request_room(tty, count);
986
987 if (s->active_rx == s->cookie_rx[0]) {
988 active = 0;
989 } else if (s->active_rx == s->cookie_rx[1]) {
990 active = 1;
991 } else {
992 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
993 return 0;
994 }
995
996 if (room < count)
997 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
998 count - room);
999 if (!room)
1000 return room;
1001
1002 for (i = 0; i < room; i++)
1003 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1004 TTY_NORMAL);
1005
1006 port->icount.rx += room;
1007
1008 return room;
1009}
1010
1011static void sci_dma_rx_complete(void *arg)
1012{
1013 struct sci_port *s = arg;
1014 struct uart_port *port = &s->port;
1015 struct tty_struct *tty = port->state->port.tty;
1016 unsigned long flags;
1017 int count;
1018
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001019 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001020
1021 spin_lock_irqsave(&port->lock, flags);
1022
1023 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1024
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001025 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001026
1027 spin_unlock_irqrestore(&port->lock, flags);
1028
1029 if (count)
1030 tty_flip_buffer_push(tty);
1031
1032 schedule_work(&s->work_rx);
1033}
1034
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001035static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1036{
1037 struct dma_chan *chan = s->chan_rx;
1038 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001039
1040 s->chan_rx = NULL;
1041 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1042 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001043 if (sg_dma_address(&s->sg_rx[0]))
1044 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1045 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001046 if (enable_pio)
1047 sci_start_rx(port);
1048}
1049
1050static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1051{
1052 struct dma_chan *chan = s->chan_tx;
1053 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001054
1055 s->chan_tx = NULL;
1056 s->cookie_tx = -EINVAL;
1057 dma_release_channel(chan);
1058 if (enable_pio)
1059 sci_start_tx(port);
1060}
1061
1062static void sci_submit_rx(struct sci_port *s)
1063{
1064 struct dma_chan *chan = s->chan_rx;
1065 int i;
1066
1067 for (i = 0; i < 2; i++) {
1068 struct scatterlist *sg = &s->sg_rx[i];
1069 struct dma_async_tx_descriptor *desc;
1070
1071 desc = chan->device->device_prep_slave_sg(chan,
1072 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1073
1074 if (desc) {
1075 s->desc_rx[i] = desc;
1076 desc->callback = sci_dma_rx_complete;
1077 desc->callback_param = s;
1078 s->cookie_rx[i] = desc->tx_submit(desc);
1079 }
1080
1081 if (!desc || s->cookie_rx[i] < 0) {
1082 if (i) {
1083 async_tx_ack(s->desc_rx[0]);
1084 s->cookie_rx[0] = -EINVAL;
1085 }
1086 if (desc) {
1087 async_tx_ack(desc);
1088 s->cookie_rx[i] = -EINVAL;
1089 }
1090 dev_warn(s->port.dev,
1091 "failed to re-start DMA, using PIO\n");
1092 sci_rx_dma_release(s, true);
1093 return;
1094 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001095 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1096 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001097 }
1098
1099 s->active_rx = s->cookie_rx[0];
1100
1101 dma_async_issue_pending(chan);
1102}
1103
1104static void work_fn_rx(struct work_struct *work)
1105{
1106 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1107 struct uart_port *port = &s->port;
1108 struct dma_async_tx_descriptor *desc;
1109 int new;
1110
1111 if (s->active_rx == s->cookie_rx[0]) {
1112 new = 0;
1113 } else if (s->active_rx == s->cookie_rx[1]) {
1114 new = 1;
1115 } else {
1116 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1117 return;
1118 }
1119 desc = s->desc_rx[new];
1120
1121 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1122 DMA_SUCCESS) {
1123 /* Handle incomplete DMA receive */
1124 struct tty_struct *tty = port->state->port.tty;
1125 struct dma_chan *chan = s->chan_rx;
1126 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1127 async_tx);
1128 unsigned long flags;
1129 int count;
1130
Linus Walleij05827632010-05-17 16:30:42 -07001131 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001132 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1133 sh_desc->partial, sh_desc->cookie);
1134
1135 spin_lock_irqsave(&port->lock, flags);
1136 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1137 spin_unlock_irqrestore(&port->lock, flags);
1138
1139 if (count)
1140 tty_flip_buffer_push(tty);
1141
1142 sci_submit_rx(s);
1143
1144 return;
1145 }
1146
1147 s->cookie_rx[new] = desc->tx_submit(desc);
1148 if (s->cookie_rx[new] < 0) {
1149 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1150 sci_rx_dma_release(s, true);
1151 return;
1152 }
1153
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001154 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001155
1156 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1157 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001158}
1159
1160static void work_fn_tx(struct work_struct *work)
1161{
1162 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1163 struct dma_async_tx_descriptor *desc;
1164 struct dma_chan *chan = s->chan_tx;
1165 struct uart_port *port = &s->port;
1166 struct circ_buf *xmit = &port->state->xmit;
1167 struct scatterlist *sg = &s->sg_tx;
1168
1169 /*
1170 * DMA is idle now.
1171 * Port xmit buffer is already mapped, and it is one page... Just adjust
1172 * offsets and lengths. Since it is a circular buffer, we have to
1173 * transmit till the end, and then the rest. Take the port lock to get a
1174 * consistent xmit buffer state.
1175 */
1176 spin_lock_irq(&port->lock);
1177 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001178 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001179 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001180 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001181 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001182 spin_unlock_irq(&port->lock);
1183
Magnus Dammf354a382010-03-19 04:47:01 +00001184 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001185
1186 desc = chan->device->device_prep_slave_sg(chan,
1187 sg, s->sg_len_tx, DMA_TO_DEVICE,
1188 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1189 if (!desc) {
1190 /* switch to PIO */
1191 sci_tx_dma_release(s, true);
1192 return;
1193 }
1194
1195 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1196
1197 spin_lock_irq(&port->lock);
1198 s->desc_tx = desc;
1199 desc->callback = sci_dma_tx_complete;
1200 desc->callback_param = s;
1201 spin_unlock_irq(&port->lock);
1202 s->cookie_tx = desc->tx_submit(desc);
1203 if (s->cookie_tx < 0) {
1204 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1205 /* switch to PIO */
1206 sci_tx_dma_release(s, true);
1207 return;
1208 }
1209
1210 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1211 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1212
1213 dma_async_issue_pending(chan);
1214}
1215#endif
1216
Russell Kingb129a8c2005-08-31 10:12:14 +01001217static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001219 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001220 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001222#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001223 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001224 u16 new, scr = sci_in(port, SCSCR);
1225 if (s->chan_tx)
1226 new = scr | 0x8000;
1227 else
1228 new = scr & ~0x8000;
1229 if (new != scr)
1230 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001231 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001232
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001233 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1234 s->cookie_tx < 0)
1235 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001236#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001237
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001238 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001239 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1240 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001241 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Russell Kingb129a8c2005-08-31 10:12:14 +01001245static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned short ctrl;
1248
1249 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001251
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001252 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001253 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001254
Paul Mundt8e698612009-06-24 19:44:32 +09001255 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001260static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 unsigned short ctrl;
1263
Paul Mundtf43dc232011-01-13 15:06:28 +09001264 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1265
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001266 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001267 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static void sci_stop_rx(struct uart_port *port)
1273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 unsigned short ctrl;
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001277
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001278 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001279 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001280
1281 ctrl &= ~port_rx_irq_mask(port);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286static void sci_enable_ms(struct uart_port *port)
1287{
1288 /* Nothing here yet .. */
1289}
1290
1291static void sci_break_ctl(struct uart_port *port, int break_state)
1292{
1293 /* Nothing here yet .. */
1294}
1295
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001296#ifdef CONFIG_SERIAL_SH_SCI_DMA
1297static bool filter(struct dma_chan *chan, void *slave)
1298{
1299 struct sh_dmae_slave *param = slave;
1300
1301 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1302 param->slave_id);
1303
1304 if (param->dma_dev == chan->device->dev) {
1305 chan->private = param;
1306 return true;
1307 } else {
1308 return false;
1309 }
1310}
1311
1312static void rx_timer_fn(unsigned long arg)
1313{
1314 struct sci_port *s = (struct sci_port *)arg;
1315 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001316 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001317
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001318 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001319 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001320 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001321 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001322 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001323 dev_dbg(port->dev, "DMA Rx timed out\n");
1324 schedule_work(&s->work_rx);
1325}
1326
1327static void sci_request_dma(struct uart_port *port)
1328{
1329 struct sci_port *s = to_sci_port(port);
1330 struct sh_dmae_slave *param;
1331 struct dma_chan *chan;
1332 dma_cap_mask_t mask;
1333 int nent;
1334
1335 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001336 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001337
Paul Mundtce6738b2011-01-19 15:24:40 +09001338 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001339 return;
1340
1341 dma_cap_zero(mask);
1342 dma_cap_set(DMA_SLAVE, mask);
1343
1344 param = &s->param_tx;
1345
1346 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001347 param->slave_id = s->cfg->dma_slave_tx;
1348 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001349
1350 s->cookie_tx = -EINVAL;
1351 chan = dma_request_channel(mask, filter, param);
1352 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1353 if (chan) {
1354 s->chan_tx = chan;
1355 sg_init_table(&s->sg_tx, 1);
1356 /* UART circular tx buffer is an aligned page. */
1357 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1358 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1359 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1360 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1361 if (!nent)
1362 sci_tx_dma_release(s, false);
1363 else
1364 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1365 sg_dma_len(&s->sg_tx),
1366 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1367
1368 s->sg_len_tx = nent;
1369
1370 INIT_WORK(&s->work_tx, work_fn_tx);
1371 }
1372
1373 param = &s->param_rx;
1374
1375 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001376 param->slave_id = s->cfg->dma_slave_rx;
1377 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001378
1379 chan = dma_request_channel(mask, filter, param);
1380 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1381 if (chan) {
1382 dma_addr_t dma[2];
1383 void *buf[2];
1384 int i;
1385
1386 s->chan_rx = chan;
1387
1388 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1389 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1390 &dma[0], GFP_KERNEL);
1391
1392 if (!buf[0]) {
1393 dev_warn(port->dev,
1394 "failed to allocate dma buffer, using PIO\n");
1395 sci_rx_dma_release(s, true);
1396 return;
1397 }
1398
1399 buf[1] = buf[0] + s->buf_len_rx;
1400 dma[1] = dma[0] + s->buf_len_rx;
1401
1402 for (i = 0; i < 2; i++) {
1403 struct scatterlist *sg = &s->sg_rx[i];
1404
1405 sg_init_table(sg, 1);
1406 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1407 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001408 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001409 }
1410
1411 INIT_WORK(&s->work_rx, work_fn_rx);
1412 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1413
1414 sci_submit_rx(s);
1415 }
1416}
1417
1418static void sci_free_dma(struct uart_port *port)
1419{
1420 struct sci_port *s = to_sci_port(port);
1421
Paul Mundtce6738b2011-01-19 15:24:40 +09001422 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001423 return;
1424
1425 if (s->chan_tx)
1426 sci_tx_dma_release(s, false);
1427 if (s->chan_rx)
1428 sci_rx_dma_release(s, false);
1429}
Paul Mundt27bd1072011-01-19 15:37:31 +09001430#else
1431static inline void sci_request_dma(struct uart_port *port)
1432{
1433}
1434
1435static inline void sci_free_dma(struct uart_port *port)
1436{
1437}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001438#endif
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static int sci_startup(struct uart_port *port)
1441{
Magnus Damma5660ad2009-01-21 15:14:38 +00001442 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001443 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001445 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1446
Paul Mundte108b2c2006-09-27 16:32:13 +09001447 if (s->enable)
1448 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Paul Mundt073e84c2011-01-19 17:30:53 +09001450 ret = sci_request_irq(s);
1451 if (unlikely(ret < 0))
1452 return ret;
1453
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001454 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001455
Yoshinori Satod6569012005-10-14 15:59:12 -07001456 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001457 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 return 0;
1460}
1461
1462static void sci_shutdown(struct uart_port *port)
1463{
Magnus Damma5660ad2009-01-21 15:14:38 +00001464 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001466 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001469 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001470
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001471 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 sci_free_irq(s);
1473
Paul Mundte108b2c2006-09-27 16:32:13 +09001474 if (s->disable)
1475 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Paul Mundt26c92f32009-06-24 18:23:52 +09001478static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1479 unsigned long freq)
1480{
1481 switch (algo_id) {
1482 case SCBRR_ALGO_1:
1483 return ((freq + 16 * bps) / (16 * bps) - 1);
1484 case SCBRR_ALGO_2:
1485 return ((freq + 16 * bps) / (32 * bps) - 1);
1486 case SCBRR_ALGO_3:
1487 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1488 case SCBRR_ALGO_4:
1489 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1490 case SCBRR_ALGO_5:
1491 return (((freq * 1000 / 32) / bps) - 1);
1492 }
1493
1494 /* Warn, but use a safe default */
1495 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001496
Paul Mundt26c92f32009-06-24 18:23:52 +09001497 return ((freq + 16 * bps) / (32 * bps) - 1);
1498}
1499
Alan Cox606d0992006-12-08 02:38:45 -08001500static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1501 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Paul Mundt00b9de92009-06-24 17:53:33 +09001503 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001504 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001505 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001506 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Magnus Damm154280f2009-12-22 03:37:28 +00001508 /*
1509 * earlyprintk comes here early on with port->uartclk set to zero.
1510 * the clock framework is not up and running at this point so here
1511 * we assume that 115200 is the maximum baud rate. please note that
1512 * the baud rate is not programmed during earlyprintk - it is assumed
1513 * that the previous boot loader has enabled required clocks and
1514 * setup the baud rate generator hardware for us already.
1515 */
1516 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1517
1518 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1519 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001520 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Alexandre Courbot36003382011-03-03 08:04:42 +00001522 if (s->enable)
1523 s->enable(port);
1524
Paul Mundte108b2c2006-09-27 16:32:13 +09001525 do {
1526 status = sci_in(port, SCxSR);
1527 } while (!(status & SCxSR_TEND(port)));
1528
1529 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1530
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001531 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001532 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001533
1534 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001535
Paul Mundte108b2c2006-09-27 16:32:13 +09001536 if ((termios->c_cflag & CSIZE) == CS7)
1537 smr_val |= 0x40;
1538 if (termios->c_cflag & PARENB)
1539 smr_val |= 0x20;
1540 if (termios->c_cflag & PARODD)
1541 smr_val |= 0x30;
1542 if (termios->c_cflag & CSTOPB)
1543 smr_val |= 0x08;
1544
1545 uart_update_timeout(port, termios->c_cflag, baud);
1546
1547 sci_out(port, SCSMR, smr_val);
1548
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001549 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001550 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001553 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1555 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001556 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 sci_out(port, SCBRR, t);
1560 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1561 }
1562
Paul Mundtd5701642008-12-16 20:07:27 +09001563 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001564 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001565
Paul Mundtce6738b2011-01-19 15:24:40 +09001566 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001568#ifdef CONFIG_SERIAL_SH_SCI_DMA
1569 /*
1570 * Calculate delay for 1.5 DMA buffers: see
1571 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1572 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1573 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1574 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1575 * sizes), but it has been found out experimentally, that this is not
1576 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1577 * as a minimum seem to work perfectly.
1578 */
1579 if (s->chan_rx) {
1580 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1581 port->fifosize / 2;
1582 dev_dbg(port->dev,
1583 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1584 s->rx_timeout * 1000 / HZ, port->timeout);
1585 if (s->rx_timeout < msecs_to_jiffies(20))
1586 s->rx_timeout = msecs_to_jiffies(20);
1587 }
1588#endif
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001591 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001592
1593 if (s->disable)
1594 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597static const char *sci_type(struct uart_port *port)
1598{
1599 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001600 case PORT_IRDA:
1601 return "irda";
1602 case PORT_SCI:
1603 return "sci";
1604 case PORT_SCIF:
1605 return "scif";
1606 case PORT_SCIFA:
1607 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001608 case PORT_SCIFB:
1609 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
Paul Mundtfa439722008-09-04 18:53:58 +09001612 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Paul Mundte2651642011-01-20 21:24:03 +09001615static inline unsigned long sci_port_size(struct uart_port *port)
1616{
1617 /*
1618 * Pick an arbitrary size that encapsulates all of the base
1619 * registers by default. This can be optimized later, or derived
1620 * from platform resource data at such a time that ports begin to
1621 * behave more erratically.
1622 */
1623 return 64;
1624}
1625
Paul Mundtf6e94952011-01-21 15:25:36 +09001626static int sci_remap_port(struct uart_port *port)
1627{
1628 unsigned long size = sci_port_size(port);
1629
1630 /*
1631 * Nothing to do if there's already an established membase.
1632 */
1633 if (port->membase)
1634 return 0;
1635
1636 if (port->flags & UPF_IOREMAP) {
1637 port->membase = ioremap_nocache(port->mapbase, size);
1638 if (unlikely(!port->membase)) {
1639 dev_err(port->dev, "can't remap port#%d\n", port->line);
1640 return -ENXIO;
1641 }
1642 } else {
1643 /*
1644 * For the simple (and majority of) cases where we don't
1645 * need to do any remapping, just cast the cookie
1646 * directly.
1647 */
1648 port->membase = (void __iomem *)port->mapbase;
1649 }
1650
1651 return 0;
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static void sci_release_port(struct uart_port *port)
1655{
Paul Mundte2651642011-01-20 21:24:03 +09001656 if (port->flags & UPF_IOREMAP) {
1657 iounmap(port->membase);
1658 port->membase = NULL;
1659 }
1660
1661 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664static int sci_request_port(struct uart_port *port)
1665{
Paul Mundte2651642011-01-20 21:24:03 +09001666 unsigned long size = sci_port_size(port);
1667 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001668 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Paul Mundt10205202011-01-21 16:00:31 +09001670 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001671 if (unlikely(res == NULL))
1672 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Paul Mundtf6e94952011-01-21 15:25:36 +09001674 ret = sci_remap_port(port);
1675 if (unlikely(ret != 0)) {
1676 release_resource(res);
1677 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001678 }
Paul Mundte2651642011-01-20 21:24:03 +09001679
1680 return 0;
1681}
1682
1683static void sci_config_port(struct uart_port *port, int flags)
1684{
1685 if (flags & UART_CONFIG_TYPE) {
1686 struct sci_port *sport = to_sci_port(port);
1687
1688 port->type = sport->cfg->type;
1689 sci_request_port(port);
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1694{
Magnus Damma5660ad2009-01-21 15:14:38 +00001695 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Paul Mundtce6738b2011-01-19 15:24:40 +09001697 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return -EINVAL;
1699 if (ser->baud_base < 2400)
1700 /* No paper tape reader for Mitch.. */
1701 return -EINVAL;
1702
1703 return 0;
1704}
1705
1706static struct uart_ops sci_uart_ops = {
1707 .tx_empty = sci_tx_empty,
1708 .set_mctrl = sci_set_mctrl,
1709 .get_mctrl = sci_get_mctrl,
1710 .start_tx = sci_start_tx,
1711 .stop_tx = sci_stop_tx,
1712 .stop_rx = sci_stop_rx,
1713 .enable_ms = sci_enable_ms,
1714 .break_ctl = sci_break_ctl,
1715 .startup = sci_startup,
1716 .shutdown = sci_shutdown,
1717 .set_termios = sci_set_termios,
1718 .type = sci_type,
1719 .release_port = sci_release_port,
1720 .request_port = sci_request_port,
1721 .config_port = sci_config_port,
1722 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001723#ifdef CONFIG_CONSOLE_POLL
1724 .poll_get_char = sci_poll_get_char,
1725 .poll_put_char = sci_poll_put_char,
1726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727};
1728
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001729static int __devinit sci_init_single(struct platform_device *dev,
1730 struct sci_port *sci_port,
1731 unsigned int index,
1732 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001733{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001734 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001735
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001736 port->ops = &sci_uart_ops;
1737 port->iotype = UPIO_MEM;
1738 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001739
1740 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001741 case PORT_SCIFB:
1742 port->fifosize = 256;
1743 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001744 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001745 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001746 break;
1747 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001748 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001749 break;
1750 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001751 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001752 break;
1753 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001754
1755 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001756 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1757 if (IS_ERR(sci_port->iclk)) {
1758 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1759 if (IS_ERR(sci_port->iclk)) {
1760 dev_err(&dev->dev, "can't get iclk\n");
1761 return PTR_ERR(sci_port->iclk);
1762 }
1763 }
1764
1765 /*
1766 * The function clock is optional, ignore it if we can't
1767 * find it.
1768 */
1769 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1770 if (IS_ERR(sci_port->fclk))
1771 sci_port->fclk = NULL;
1772
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001773 sci_port->enable = sci_clk_enable;
1774 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001775 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001776
1777 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001778 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001779
Magnus Damm7ed7e072009-01-21 15:14:14 +00001780 sci_port->break_timer.data = (unsigned long)sci_port;
1781 sci_port->break_timer.function = sci_break_timer;
1782 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001783
Paul Mundtdebf9502011-06-08 18:19:37 +09001784 /*
1785 * Establish some sensible defaults for the error detection.
1786 */
1787 if (!p->error_mask)
1788 p->error_mask = (p->type == PORT_SCI) ?
1789 SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1790
1791 /*
1792 * Establish sensible defaults for the overrun detection, unless
1793 * the part has explicitly disabled support for it.
1794 */
1795 if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1796 if (p->type == PORT_SCI)
1797 p->overrun_bit = 5;
1798 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1799 p->overrun_bit = 9;
1800 else
1801 p->overrun_bit = 0;
1802
1803 /*
1804 * Make the error mask inclusive of overrun detection, if
1805 * supported.
1806 */
1807 p->error_mask |= (1 << p->overrun_bit);
1808 }
1809
Paul Mundtce6738b2011-01-19 15:24:40 +09001810 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001811
Paul Mundtce6738b2011-01-19 15:24:40 +09001812 port->mapbase = p->mapbase;
1813 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001814 port->flags = p->flags;
Paul Mundtce6738b2011-01-19 15:24:40 +09001815
1816 /*
1817 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1818 * for the multi-IRQ ports, which is where we are primarily
1819 * concerned with the shutdown path synchronization.
1820 *
1821 * For the muxed case there's nothing more to do.
1822 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00001823 port->irq = p->irqs[SCIx_RXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001824
Paul Mundtce6738b2011-01-19 15:24:40 +09001825 if (p->dma_dev)
1826 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1827 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001828
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001829 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001833static void serial_console_putchar(struct uart_port *port, int ch)
1834{
1835 sci_poll_put_char(port, ch);
1836}
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/*
1839 * Print a string to the serial port trying not to disturb
1840 * any possible real use of the port...
1841 */
1842static void serial_console_write(struct console *co, const char *s,
1843 unsigned count)
1844{
Paul Mundt906b17d2011-01-21 16:19:53 +09001845 struct sci_port *sci_port = &sci_ports[co->index];
1846 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001847 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001848
Magnus Damm501b8252009-01-21 15:14:30 +00001849 if (sci_port->enable)
1850 sci_port->enable(port);
1851
1852 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001853
1854 /* wait until fifo is empty and last bit has been transmitted */
1855 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1856 while ((sci_in(port, SCxSR) & bits) != bits)
1857 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001858
Magnus Damm345e5a72009-11-05 14:34:57 +00001859 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001860 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001863static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001865 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct uart_port *port;
1867 int baud = 115200;
1868 int bits = 8;
1869 int parity = 'n';
1870 int flow = 'n';
1871 int ret;
1872
Paul Mundte108b2c2006-09-27 16:32:13 +09001873 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09001874 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09001875 */
Paul Mundt906b17d2011-01-21 16:19:53 +09001876 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09001877 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001878
Paul Mundt906b17d2011-01-21 16:19:53 +09001879 sci_port = &sci_ports[co->index];
1880 port = &sci_port->port;
1881
Alexandre Courbotb2267a62011-02-09 03:18:46 +00001882 /*
1883 * Refuse to handle uninitialized ports.
1884 */
1885 if (!port->ops)
1886 return -ENODEV;
1887
Paul Mundtf6e94952011-01-21 15:25:36 +09001888 ret = sci_remap_port(port);
1889 if (unlikely(ret != 0))
1890 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001891
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001892 if (sci_port->enable)
1893 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (options)
1896 uart_parse_options(options, &baud, &parity, &bits, &flow);
1897
Magnus Damm501b8252009-01-21 15:14:30 +00001898 /* TODO: disable clock */
Paul Mundtab7cfb52011-06-01 14:47:42 +09001899 return uart_set_options(port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902static struct console serial_console = {
1903 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09001904 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 .write = serial_console_write,
1906 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001907 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09001909 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001912static struct console early_serial_console = {
1913 .name = "early_ttySC",
1914 .write = serial_console_write,
1915 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09001916 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001917};
Paul Mundtecdf8a42011-01-21 00:05:48 +09001918
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001919static char early_serial_buf[32];
1920
Paul Mundtecdf8a42011-01-21 00:05:48 +09001921static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1922{
1923 struct plat_sci_port *cfg = pdev->dev.platform_data;
1924
1925 if (early_serial_console.data)
1926 return -EEXIST;
1927
1928 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09001929
Paul Mundt906b17d2011-01-21 16:19:53 +09001930 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09001931
1932 serial_console_setup(&early_serial_console, early_serial_buf);
1933
1934 if (!strstr(early_serial_buf, "keep"))
1935 early_serial_console.flags |= CON_BOOT;
1936
1937 register_console(&early_serial_console);
1938 return 0;
1939}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00001940
1941#define SCI_CONSOLE (&serial_console)
1942
Paul Mundtecdf8a42011-01-21 00:05:48 +09001943#else
1944static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1945{
1946 return -EINVAL;
1947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00001949#define SCI_CONSOLE NULL
1950
1951#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953static char banner[] __initdata =
1954 KERN_INFO "SuperH SCI(F) driver initialized\n";
1955
1956static struct uart_driver sci_uart_driver = {
1957 .owner = THIS_MODULE,
1958 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 .dev_name = "ttySC",
1960 .major = SCI_MAJOR,
1961 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001962 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 .cons = SCI_CONSOLE,
1964};
1965
Paul Mundt54507f62009-05-08 23:48:33 +09001966static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001967{
Paul Mundtd535a232011-01-19 17:19:35 +09001968 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001969
Paul Mundtd535a232011-01-19 17:19:35 +09001970 cpufreq_unregister_notifier(&port->freq_transition,
1971 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001972
Paul Mundtd535a232011-01-19 17:19:35 +09001973 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00001974
Paul Mundtd535a232011-01-19 17:19:35 +09001975 clk_put(port->iclk);
1976 clk_put(port->fclk);
1977
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001978 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00001979 return 0;
1980}
1981
Magnus Damm0ee70712009-01-21 15:13:50 +00001982static int __devinit sci_probe_single(struct platform_device *dev,
1983 unsigned int index,
1984 struct plat_sci_port *p,
1985 struct sci_port *sciport)
1986{
Magnus Damm0ee70712009-01-21 15:13:50 +00001987 int ret;
1988
1989 /* Sanity check */
1990 if (unlikely(index >= SCI_NPORTS)) {
1991 dev_notice(&dev->dev, "Attempting to register port "
1992 "%d when only %d are available.\n",
1993 index+1, SCI_NPORTS);
1994 dev_notice(&dev->dev, "Consider bumping "
1995 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1996 return 0;
1997 }
1998
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001999 ret = sci_init_single(dev, sciport, index, p);
2000 if (ret)
2001 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00002002
Paul Mundtd535a232011-01-19 17:19:35 +09002003 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00002004}
2005
Paul Mundte108b2c2006-09-27 16:32:13 +09002006static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Paul Mundte108b2c2006-09-27 16:32:13 +09002008 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09002009 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09002010 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00002011
Paul Mundtecdf8a42011-01-21 00:05:48 +09002012 /*
2013 * If we've come here via earlyprintk initialization, head off to
2014 * the special early probe. We don't have sufficient device state
2015 * to make it beyond this yet.
2016 */
2017 if (is_early_platform_device(dev))
2018 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002019
Paul Mundtd535a232011-01-19 17:19:35 +09002020 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00002021
Paul Mundt906b17d2011-01-21 16:19:53 +09002022 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09002023 if (ret)
2024 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00002025
Paul Mundtd535a232011-01-19 17:19:35 +09002026 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Paul Mundtd535a232011-01-19 17:19:35 +09002028 ret = cpufreq_register_notifier(&sp->freq_transition,
2029 CPUFREQ_TRANSITION_NOTIFIER);
2030 if (unlikely(ret < 0))
2031 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033#ifdef CONFIG_SH_STANDARD_BIOS
2034 sh_bios_gdb_detach();
2035#endif
2036
Paul Mundte108b2c2006-09-27 16:32:13 +09002037 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002038
2039err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002040 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002041 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002042}
2043
Paul Mundt6daa79b2009-06-15 07:07:38 +09002044static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002045{
Paul Mundtd535a232011-01-19 17:19:35 +09002046 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002047
Paul Mundtd535a232011-01-19 17:19:35 +09002048 if (sport)
2049 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002050
2051 return 0;
2052}
2053
Paul Mundt6daa79b2009-06-15 07:07:38 +09002054static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002055{
Paul Mundtd535a232011-01-19 17:19:35 +09002056 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002057
Paul Mundtd535a232011-01-19 17:19:35 +09002058 if (sport)
2059 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002060
2061 return 0;
2062}
2063
Alexey Dobriyan47145212009-12-14 18:00:08 -08002064static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002065 .suspend = sci_suspend,
2066 .resume = sci_resume,
2067};
2068
Paul Mundte108b2c2006-09-27 16:32:13 +09002069static struct platform_driver sci_driver = {
2070 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002071 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002072 .driver = {
2073 .name = "sh-sci",
2074 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002075 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002076 },
2077};
2078
2079static int __init sci_init(void)
2080{
2081 int ret;
2082
2083 printk(banner);
2084
Paul Mundte108b2c2006-09-27 16:32:13 +09002085 ret = uart_register_driver(&sci_uart_driver);
2086 if (likely(ret == 0)) {
2087 ret = platform_driver_register(&sci_driver);
2088 if (unlikely(ret))
2089 uart_unregister_driver(&sci_uart_driver);
2090 }
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return ret;
2093}
2094
2095static void __exit sci_exit(void)
2096{
Paul Mundte108b2c2006-09-27 16:32:13 +09002097 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 uart_unregister_driver(&sci_uart_driver);
2099}
2100
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002101#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2102early_platform_init_buffer("earlyprintk", &sci_driver,
2103 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105module_init(sci_init);
2106module_exit(sci_exit);
2107
Paul Mundte108b2c2006-09-27 16:32:13 +09002108MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002109MODULE_ALIAS("platform:sh-sci");