blob: ebd8629c108ddc06f6b731831dbe962442a1868f [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
Yoshinori Sato168f3622009-04-28 04:40:15 +000057#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "sh-sci.h"
62
Paul Mundte108b2c2006-09-27 16:32:13 +090063struct sci_port {
64 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Paul Mundtce6738b2011-01-19 15:24:40 +090066 /* Platform configuration */
67 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090068
Paul Mundte108b2c2006-09-27 16:32:13 +090069 /* Port enable callback */
70 void (*enable)(struct uart_port *port);
71
72 /* Port disable callback */
73 void (*disable)(struct uart_port *port);
74
75 /* Break timer */
76 struct timer_list break_timer;
77 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090078
Magnus Damm501b8252009-01-21 15:14:30 +000079 /* Interface clock */
80 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090081 /* Function clock */
82 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090083
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090084 struct dma_chan *chan_tx;
85 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090086
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090087#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090088 struct dma_async_tx_descriptor *desc_tx;
89 struct dma_async_tx_descriptor *desc_rx[2];
90 dma_cookie_t cookie_tx;
91 dma_cookie_t cookie_rx[2];
92 dma_cookie_t active_rx;
93 struct scatterlist sg_tx;
94 unsigned int sg_len_tx;
95 struct scatterlist sg_rx[2];
96 size_t buf_len_rx;
97 struct sh_dmae_slave param_tx;
98 struct sh_dmae_slave param_rx;
99 struct work_struct work_tx;
100 struct work_struct work_rx;
101 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000102 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900103#endif
Magnus Damme552de22009-01-21 15:13:42 +0000104
Paul Mundtd535a232011-01-19 17:19:35 +0900105 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900109static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100110static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900111static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Paul Mundte108b2c2006-09-27 16:32:13 +0900113#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
114
115static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static struct uart_driver sci_uart_driver;
117
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900118static inline struct sci_port *
119to_sci_port(struct uart_port *uart)
120{
121 return container_of(uart, struct sci_port, port);
122}
123
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900124#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900125
126#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900127static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned short status;
130 int c;
131
Paul Mundte108b2c2006-09-27 16:32:13 +0900132 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 status = sci_in(port, SCxSR);
134 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900135 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 continue;
137 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500138 break;
139 } while (1);
140
141 if (!(status & SCxSR_RDxF(port)))
142 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900145
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900146 /* Dummy read */
147 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return c;
151}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900154static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned short status;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 do {
159 status = sci_in(port, SCxSR);
160 } while (!(status & SCxSR_TDxE(port)));
161
Paul Mundt272966c2008-11-13 17:46:06 +0900162 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000163 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900165#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Paul Mundt15c73aa2008-10-02 19:47:12 +0900167#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900168static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 int ch = (port->mapbase - SMR0) >> 3;
171
172 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900173 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
174 h8300_sci_pins[ch].rx,
175 H8300_GPIO_INPUT);
176 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
177 h8300_sci_pins[ch].tx,
178 H8300_GPIO_OUTPUT);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* tx mark output*/
181 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
182}
Paul Mundtd5701642008-12-16 20:07:27 +0900183#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
184static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900185{
Paul Mundtd5701642008-12-16 20:07:27 +0900186 if (port->mapbase == 0xA4400000) {
187 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
188 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
189 } else if (port->mapbase == 0xA4410000)
190 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900191}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900192#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900193static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900194{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900195 unsigned short data;
196
197 if (cflag & CRTSCTS) {
198 /* enable RTS/CTS */
199 if (port->mapbase == 0xa4430000) { /* SCIF0 */
200 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900201 data = __raw_readw(PORT_PTCR);
202 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900203 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
204 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900205 data = __raw_readw(PORT_PVCR);
206 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900207 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900208 } else {
209 if (port->mapbase == 0xa4430000) { /* SCIF0 */
210 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900211 data = __raw_readw(PORT_PTCR);
212 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900213 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
214 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900215 data = __raw_readw(PORT_PVCR);
216 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900217 }
218 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800220#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900221/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900222static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800224 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Paul Mundtb7a76e42006-02-01 03:06:06 -0800226 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900227 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800228 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900229 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800230
Paul Mundtd5701642008-12-16 20:07:27 +0900231 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900233 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Clear out SCP7MD1,0, SCP4MD1,0,
235 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900236 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Paul Mundt32b53072009-12-24 14:52:43 +0900238 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900240 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Paul Mundt41504c32006-12-11 20:28:03 +0900243#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900244static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900245{
Magnus Damm346b7462008-04-23 21:25:29 +0900246 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900247
Magnus Damm346b7462008-04-23 21:25:29 +0900248 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900249 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900250 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900251 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900252 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900253
Paul Mundtd5701642008-12-16 20:07:27 +0900254 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900255 }
Paul Mundt41504c32006-12-11 20:28:03 +0900256}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900257#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
258 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900259 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900260 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900261 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900262 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900263static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
264{
265 if (!(cflag & CRTSCTS))
266 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
267}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900268#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900269static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
270{
271 if (!(cflag & CRTSCTS))
272 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
273}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800274#else
Paul Mundtd5701642008-12-16 20:07:27 +0900275static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
276{
277 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Paul Mundte108b2c2006-09-27 16:32:13 +0900279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Paul Mundt32351a22007-03-12 14:38:59 +0900281#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900283 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
284 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900285static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900286{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900287 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900288}
289
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900290static int scif_txroom(struct uart_port *port)
291{
292 return SCIF_TXROOM_MAX - scif_txfill(port);
293}
294
295static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900296{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900297 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900298}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900299#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900300static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900301{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900302 if (port->mapbase == 0xffe00000 ||
303 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900304 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900305 return sci_in(port, SCTFDR) & 0xff;
306 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900307 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900308 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900309}
310
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900311static int scif_txroom(struct uart_port *port)
312{
313 if (port->mapbase == 0xffe00000 ||
314 port->mapbase == 0xffe08000)
315 /* SCIF0/1*/
316 return SCIF_TXROOM_MAX - scif_txfill(port);
317 else
318 /* SCIF2 */
319 return SCIF2_TXROOM_MAX - scif_txfill(port);
320}
321
322static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900323{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900324 if ((port->mapbase == 0xffe00000) ||
325 (port->mapbase == 0xffe08000)) {
326 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900327 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900328 } else {
329 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900330 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900331 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900332}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000333#elif defined(CONFIG_ARCH_SH7372)
334static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900335{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000336 if (port->type == PORT_SCIFA)
337 return sci_in(port, SCFDR) >> 8;
338 else
339 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900340}
341
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000342static int scif_txroom(struct uart_port *port)
343{
344 return port->fifosize - scif_txfill(port);
345}
346
347static int scif_rxfill(struct uart_port *port)
348{
349 if (port->type == PORT_SCIFA)
350 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
351 else
352 return sci_in(port, SCRFDR);
353}
Paul Mundte108b2c2006-09-27 16:32:13 +0900354#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900355static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900356{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900357 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900358}
359
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900360static int scif_txroom(struct uart_port *port)
361{
362 return SCIF_TXROOM_MAX - scif_txfill(port);
363}
364
365static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900366{
367 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900371static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900372{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900373 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900374}
375
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900376static int sci_txroom(struct uart_port *port)
377{
378 return !sci_txfill(port);
379}
380
381static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900382{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900383 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/* ********************************************************************** *
387 * the interrupt related routines *
388 * ********************************************************************** */
389
390static void sci_transmit_chars(struct uart_port *port)
391{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700392 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned short status;
395 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900396 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 status = sci_in(port, SCxSR);
399 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900401 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900402 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900403 else
Paul Mundt8e698612009-06-24 19:44:32 +0900404 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return;
407 }
408
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900409 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900410 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900411 else
412 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 do {
415 unsigned char c;
416
417 if (port->x_char) {
418 c = port->x_char;
419 port->x_char = 0;
420 } else if (!uart_circ_empty(xmit) && !stopped) {
421 c = xmit->buf[xmit->tail];
422 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
423 } else {
424 break;
425 }
426
427 sci_out(port, SCxTDR, c);
428
429 port->icount.tx++;
430 } while (--count > 0);
431
432 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
433
434 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
435 uart_write_wakeup(port);
436 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100437 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ctrl = sci_in(port, SCSCR);
440
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900441 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sci_in(port, SCxSR); /* Dummy read */
443 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Paul Mundt8e698612009-06-24 19:44:32 +0900446 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449}
450
451/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900452#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900454static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900456 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700457 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int i, count, copied = 0;
459 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800460 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 status = sci_in(port, SCxSR);
463 if (!(status & SCxSR_RDxF(port)))
464 return;
465
466 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900467 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900468 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900469 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900470 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800473 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* If for any reason we can't copy more data, we're done! */
476 if (count == 0)
477 break;
478
479 if (port->type == PORT_SCI) {
480 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900481 if (uart_handle_sysrq_char(port, c) ||
482 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900484 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900485 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900487 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 char c = sci_in(port, SCxRDR);
489 status = sci_in(port, SCxSR);
490#if defined(CONFIG_CPU_SH3)
491 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900492 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((c == 0) &&
494 (status & SCxSR_FER(port))) {
495 count--; i--;
496 continue;
497 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900500 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900501 sci_port->break_flag = 0;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (STEPFN(c)) {
504 count--; i--;
505 continue;
506 }
507 }
508#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100509 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 count--; i--;
511 continue;
512 }
513
514 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900515 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800516 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900517 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900518 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800519 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900520 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800521 } else
522 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900523
Alan Cox33f0f882006-01-09 20:54:13 -0800524 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 }
527
528 sci_in(port, SCxSR); /* dummy read */
529 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 copied += count;
532 port->icount.rx += count;
533 }
534
535 if (copied) {
536 /* Tell the rest of the system the news. New characters! */
537 tty_flip_buffer_push(tty);
538 } else {
539 sci_in(port, SCxSR); /* dummy read */
540 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
541 }
542}
543
544#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900545
546/*
547 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * 1 per millisecond or so during the break period, for 9600 baud.
549 * So dont bother disabling interrupts.
550 * But dont want more than 1 break event.
551 * Use a kernel timer to periodically poll the rx line until
552 * the break is finished.
553 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900554static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900556 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/* Ensure that two consecutive samples find the break over. */
560static void sci_break_timer(unsigned long data)
561{
Paul Mundte108b2c2006-09-27 16:32:13 +0900562 struct sci_port *port = (struct sci_port *)data;
563
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000564 if (port->enable)
565 port->enable(&port->port);
566
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900569 sci_schedule_break_timer(port);
570 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* break is over. */
572 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900573 sci_schedule_break_timer(port);
574 } else
575 port->break_flag = 0;
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000576
577 if (port->disable)
578 port->disable(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900581static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int copied = 0;
584 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700585 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Paul Mundte108b2c2006-09-27 16:32:13 +0900587 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900589 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800590 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900591
592 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Paul Mundte108b2c2006-09-27 16:32:13 +0900595 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (sci_rxd_in(port) == 0) {
597 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900598 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900599
600 if (!sci_port->break_flag) {
601 sci_port->break_flag = 1;
602 sci_schedule_break_timer(sci_port);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900607
608 dev_dbg(port->dev, "BREAK detected\n");
609
Paul Mundte108b2c2006-09-27 16:32:13 +0900610 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900611 copied++;
612 }
613
Paul Mundte108b2c2006-09-27 16:32:13 +0900614 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900616 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800617 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900618
619 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 }
622
Paul Mundte108b2c2006-09-27 16:32:13 +0900623 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900625 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
626 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900627
628 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Alan Cox33f0f882006-01-09 20:54:13 -0800631 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return copied;
635}
636
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900637static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900638{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700639 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900640 int copied = 0;
641
642 if (port->type != PORT_SCIF)
643 return 0;
644
645 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
646 sci_out(port, SCLSR, 0);
647
648 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
649 tty_flip_buffer_push(tty);
650
651 dev_notice(port->dev, "overrun error\n");
652 copied++;
653 }
654
655 return copied;
656}
657
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900658static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int copied = 0;
661 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700662 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000663 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900665 if (uart_handle_break(port))
666 return 0;
667
Paul Mundtb7a76e42006-02-01 03:06:06 -0800668 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#if defined(CONFIG_CPU_SH3)
670 /* Debounce break */
671 s->break_flag = 1;
672#endif
673 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900674 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800675 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900676
677 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Alan Cox33f0f882006-01-09 20:54:13 -0800680 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900682
Paul Mundtd830fa42008-12-16 19:29:38 +0900683 copied += sci_handle_fifo_overrun(port);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return copied;
686}
687
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900688static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900690#ifdef CONFIG_SERIAL_SH_SCI_DMA
691 struct uart_port *port = ptr;
692 struct sci_port *s = to_sci_port(port);
693
694 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900695 u16 scr = sci_in(port, SCSCR);
696 u16 ssr = sci_in(port, SCxSR);
697
698 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000699 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000700 disable_irq_nosync(irq);
701 scr |= 0x4000;
702 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900703 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000704 }
705 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900706 /* Clear current interrupt */
707 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000708 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
709 jiffies, s->rx_timeout);
710 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900711
712 return IRQ_HANDLED;
713 }
714#endif
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* I think sci_receive_chars has to be called irrespective
717 * of whether the I_IXOFF is set, otherwise, how is the interrupt
718 * to be disabled?
719 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900720 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 return IRQ_HANDLED;
723}
724
David Howells7d12e782006-10-05 14:55:46 +0100725static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900728 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Stuart Menefyfd78a762009-07-29 23:01:24 +0900730 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900732 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 return IRQ_HANDLED;
735}
736
David Howells7d12e782006-10-05 14:55:46 +0100737static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct uart_port *port = ptr;
740
741 /* Handle errors */
742 if (port->type == PORT_SCI) {
743 if (sci_handle_errors(port)) {
744 /* discard character in rx buffer */
745 sci_in(port, SCxSR);
746 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
747 }
748 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900749 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100750 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
754
755 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100756 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return IRQ_HANDLED;
759}
760
David Howells7d12e782006-10-05 14:55:46 +0100761static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct uart_port *port = ptr;
764
765 /* Handle BREAKs */
766 sci_handle_breaks(port);
767 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
768
769 return IRQ_HANDLED;
770}
771
Paul Mundtf43dc232011-01-13 15:06:28 +0900772static inline unsigned long port_rx_irq_mask(struct uart_port *port)
773{
774 /*
775 * Not all ports (such as SCIFA) will support REIE. Rather than
776 * special-casing the port type, we check the port initialization
777 * IRQ enable mask to see whether the IRQ is desired at all. If
778 * it's unset, it's logically inferred that there's no point in
779 * testing for it.
780 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900781 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900782}
783
David Howells7d12e782006-10-05 14:55:46 +0100784static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Magnus Damm44e18e92009-07-03 08:39:34 +0000786 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900787 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900788 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900789 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900791 ssr_status = sci_in(port, SCxSR);
792 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900793 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900796 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900797 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900798 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900799
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900800 /*
801 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
802 * DR flags
803 */
804 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900805 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900806 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000809 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900810 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000813 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900814 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Michael Trimarchia8884e32008-10-31 16:10:23 +0900816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300820 * Here we define a transition notifier so that we can update all of our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * ports' baud rate when the peripheral clock changes.
822 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900823static int sci_notifier(struct notifier_block *self,
824 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Magnus Damme552de22009-01-21 15:13:42 +0000826 struct sci_port *sci_port;
827 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Paul Mundtd535a232011-01-19 17:19:35 +0900829 sci_port = container_of(self, struct sci_port, freq_transition);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000832 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900833 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900834
Paul Mundtd535a232011-01-19 17:19:35 +0900835 spin_lock_irqsave(&port->lock, flags);
836 port->uartclk = clk_get_rate(sci_port->iclk);
837 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return NOTIFY_OK;
841}
Magnus Damm501b8252009-01-21 15:14:30 +0000842
843static void sci_clk_enable(struct uart_port *port)
844{
845 struct sci_port *sci_port = to_sci_port(port);
846
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000847 pm_runtime_get_sync(port->dev);
848
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900849 clk_enable(sci_port->iclk);
850 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
851 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000852}
853
854static void sci_clk_disable(struct uart_port *port)
855{
856 struct sci_port *sci_port = to_sci_port(port);
857
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900858 clk_disable(sci_port->fclk);
859 clk_disable(sci_port->iclk);
Magnus Damm5e50d2d2011-04-19 10:38:25 +0000860
861 pm_runtime_put_sync(port->dev);
Magnus Damm501b8252009-01-21 15:14:30 +0000862}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864static int sci_request_irq(struct sci_port *port)
865{
866 int i;
David Howells7d12e782006-10-05 14:55:46 +0100867 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
869 sci_br_interrupt,
870 };
871 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
872 "SCI Transmit Data Empty", "SCI Break" };
873
Paul Mundtce6738b2011-01-19 15:24:40 +0900874 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
875 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900877
Paul Mundtce6738b2011-01-19 15:24:40 +0900878 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900879 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900880 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENODEV;
882 }
883 } else {
884 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900885 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900887
Paul Mundtce6738b2011-01-19 15:24:40 +0900888 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900889 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900890 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENODEV;
892 }
893 }
894 }
895
896 return 0;
897}
898
899static void sci_free_irq(struct sci_port *port)
900{
901 int i;
902
Paul Mundtce6738b2011-01-19 15:24:40 +0900903 if (port->cfg->irqs[0] == port->cfg->irqs[1])
904 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +0900905 else {
Paul Mundtce6738b2011-01-19 15:24:40 +0900906 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
907 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 continue;
909
Paul Mundtce6738b2011-01-19 15:24:40 +0900910 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 }
913}
914
915static unsigned int sci_tx_empty(struct uart_port *port)
916{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000917 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900918 unsigned short in_tx_fifo = scif_txfill(port);
919
920 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
924{
925 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
926 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
927 /* If you have signals for DTR and DCD, please implement here. */
928}
929
930static unsigned int sci_get_mctrl(struct uart_port *port)
931{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900932 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 and CTS/RTS */
934
935 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
936}
937
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900938#ifdef CONFIG_SERIAL_SH_SCI_DMA
939static void sci_dma_tx_complete(void *arg)
940{
941 struct sci_port *s = arg;
942 struct uart_port *port = &s->port;
943 struct circ_buf *xmit = &port->state->xmit;
944 unsigned long flags;
945
946 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
947
948 spin_lock_irqsave(&port->lock, flags);
949
Magnus Dammf354a382010-03-19 04:47:01 +0000950 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900951 xmit->tail &= UART_XMIT_SIZE - 1;
952
Magnus Dammf354a382010-03-19 04:47:01 +0000953 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900954
955 async_tx_ack(s->desc_tx);
956 s->cookie_tx = -EINVAL;
957 s->desc_tx = NULL;
958
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900959 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
960 uart_write_wakeup(port);
961
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000962 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900963 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000964 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000965 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900966 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000967 }
968
969 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900970}
971
972/* Locking: called with port lock held */
973static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
974 size_t count)
975{
976 struct uart_port *port = &s->port;
977 int i, active, room;
978
979 room = tty_buffer_request_room(tty, count);
980
981 if (s->active_rx == s->cookie_rx[0]) {
982 active = 0;
983 } else if (s->active_rx == s->cookie_rx[1]) {
984 active = 1;
985 } else {
986 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
987 return 0;
988 }
989
990 if (room < count)
991 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
992 count - room);
993 if (!room)
994 return room;
995
996 for (i = 0; i < room; i++)
997 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
998 TTY_NORMAL);
999
1000 port->icount.rx += room;
1001
1002 return room;
1003}
1004
1005static void sci_dma_rx_complete(void *arg)
1006{
1007 struct sci_port *s = arg;
1008 struct uart_port *port = &s->port;
1009 struct tty_struct *tty = port->state->port.tty;
1010 unsigned long flags;
1011 int count;
1012
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001013 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001014
1015 spin_lock_irqsave(&port->lock, flags);
1016
1017 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1018
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001019 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001020
1021 spin_unlock_irqrestore(&port->lock, flags);
1022
1023 if (count)
1024 tty_flip_buffer_push(tty);
1025
1026 schedule_work(&s->work_rx);
1027}
1028
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001029static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1030{
1031 struct dma_chan *chan = s->chan_rx;
1032 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001033
1034 s->chan_rx = NULL;
1035 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1036 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001037 if (sg_dma_address(&s->sg_rx[0]))
1038 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1039 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001040 if (enable_pio)
1041 sci_start_rx(port);
1042}
1043
1044static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1045{
1046 struct dma_chan *chan = s->chan_tx;
1047 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001048
1049 s->chan_tx = NULL;
1050 s->cookie_tx = -EINVAL;
1051 dma_release_channel(chan);
1052 if (enable_pio)
1053 sci_start_tx(port);
1054}
1055
1056static void sci_submit_rx(struct sci_port *s)
1057{
1058 struct dma_chan *chan = s->chan_rx;
1059 int i;
1060
1061 for (i = 0; i < 2; i++) {
1062 struct scatterlist *sg = &s->sg_rx[i];
1063 struct dma_async_tx_descriptor *desc;
1064
1065 desc = chan->device->device_prep_slave_sg(chan,
1066 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1067
1068 if (desc) {
1069 s->desc_rx[i] = desc;
1070 desc->callback = sci_dma_rx_complete;
1071 desc->callback_param = s;
1072 s->cookie_rx[i] = desc->tx_submit(desc);
1073 }
1074
1075 if (!desc || s->cookie_rx[i] < 0) {
1076 if (i) {
1077 async_tx_ack(s->desc_rx[0]);
1078 s->cookie_rx[0] = -EINVAL;
1079 }
1080 if (desc) {
1081 async_tx_ack(desc);
1082 s->cookie_rx[i] = -EINVAL;
1083 }
1084 dev_warn(s->port.dev,
1085 "failed to re-start DMA, using PIO\n");
1086 sci_rx_dma_release(s, true);
1087 return;
1088 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001089 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1090 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001091 }
1092
1093 s->active_rx = s->cookie_rx[0];
1094
1095 dma_async_issue_pending(chan);
1096}
1097
1098static void work_fn_rx(struct work_struct *work)
1099{
1100 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1101 struct uart_port *port = &s->port;
1102 struct dma_async_tx_descriptor *desc;
1103 int new;
1104
1105 if (s->active_rx == s->cookie_rx[0]) {
1106 new = 0;
1107 } else if (s->active_rx == s->cookie_rx[1]) {
1108 new = 1;
1109 } else {
1110 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1111 return;
1112 }
1113 desc = s->desc_rx[new];
1114
1115 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1116 DMA_SUCCESS) {
1117 /* Handle incomplete DMA receive */
1118 struct tty_struct *tty = port->state->port.tty;
1119 struct dma_chan *chan = s->chan_rx;
1120 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1121 async_tx);
1122 unsigned long flags;
1123 int count;
1124
Linus Walleij05827632010-05-17 16:30:42 -07001125 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001126 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1127 sh_desc->partial, sh_desc->cookie);
1128
1129 spin_lock_irqsave(&port->lock, flags);
1130 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1131 spin_unlock_irqrestore(&port->lock, flags);
1132
1133 if (count)
1134 tty_flip_buffer_push(tty);
1135
1136 sci_submit_rx(s);
1137
1138 return;
1139 }
1140
1141 s->cookie_rx[new] = desc->tx_submit(desc);
1142 if (s->cookie_rx[new] < 0) {
1143 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1144 sci_rx_dma_release(s, true);
1145 return;
1146 }
1147
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001148 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001149
1150 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1151 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001152}
1153
1154static void work_fn_tx(struct work_struct *work)
1155{
1156 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1157 struct dma_async_tx_descriptor *desc;
1158 struct dma_chan *chan = s->chan_tx;
1159 struct uart_port *port = &s->port;
1160 struct circ_buf *xmit = &port->state->xmit;
1161 struct scatterlist *sg = &s->sg_tx;
1162
1163 /*
1164 * DMA is idle now.
1165 * Port xmit buffer is already mapped, and it is one page... Just adjust
1166 * offsets and lengths. Since it is a circular buffer, we have to
1167 * transmit till the end, and then the rest. Take the port lock to get a
1168 * consistent xmit buffer state.
1169 */
1170 spin_lock_irq(&port->lock);
1171 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001172 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001173 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001174 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001175 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001176 spin_unlock_irq(&port->lock);
1177
Magnus Dammf354a382010-03-19 04:47:01 +00001178 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001179
1180 desc = chan->device->device_prep_slave_sg(chan,
1181 sg, s->sg_len_tx, DMA_TO_DEVICE,
1182 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1183 if (!desc) {
1184 /* switch to PIO */
1185 sci_tx_dma_release(s, true);
1186 return;
1187 }
1188
1189 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1190
1191 spin_lock_irq(&port->lock);
1192 s->desc_tx = desc;
1193 desc->callback = sci_dma_tx_complete;
1194 desc->callback_param = s;
1195 spin_unlock_irq(&port->lock);
1196 s->cookie_tx = desc->tx_submit(desc);
1197 if (s->cookie_tx < 0) {
1198 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1199 /* switch to PIO */
1200 sci_tx_dma_release(s, true);
1201 return;
1202 }
1203
1204 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1205 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1206
1207 dma_async_issue_pending(chan);
1208}
1209#endif
1210
Russell Kingb129a8c2005-08-31 10:12:14 +01001211static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001213 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001214 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001216#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001217 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001218 u16 new, scr = sci_in(port, SCSCR);
1219 if (s->chan_tx)
1220 new = scr | 0x8000;
1221 else
1222 new = scr & ~0x8000;
1223 if (new != scr)
1224 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001225 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001226
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001227 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1228 s->cookie_tx < 0)
1229 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001230#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001231
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001232 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001233 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1234 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001235 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Russell Kingb129a8c2005-08-31 10:12:14 +01001239static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned short ctrl;
1242
1243 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001245
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001246 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001247 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001248
Paul Mundt8e698612009-06-24 19:44:32 +09001249 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001254static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 unsigned short ctrl;
1257
Paul Mundtf43dc232011-01-13 15:06:28 +09001258 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1259
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001260 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001261 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static void sci_stop_rx(struct uart_port *port)
1267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned short ctrl;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001271
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001272 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001273 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001274
1275 ctrl &= ~port_rx_irq_mask(port);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void sci_enable_ms(struct uart_port *port)
1281{
1282 /* Nothing here yet .. */
1283}
1284
1285static void sci_break_ctl(struct uart_port *port, int break_state)
1286{
1287 /* Nothing here yet .. */
1288}
1289
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001290#ifdef CONFIG_SERIAL_SH_SCI_DMA
1291static bool filter(struct dma_chan *chan, void *slave)
1292{
1293 struct sh_dmae_slave *param = slave;
1294
1295 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1296 param->slave_id);
1297
1298 if (param->dma_dev == chan->device->dev) {
1299 chan->private = param;
1300 return true;
1301 } else {
1302 return false;
1303 }
1304}
1305
1306static void rx_timer_fn(unsigned long arg)
1307{
1308 struct sci_port *s = (struct sci_port *)arg;
1309 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001310 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001311
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001312 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001313 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001314 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001315 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001316 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001317 dev_dbg(port->dev, "DMA Rx timed out\n");
1318 schedule_work(&s->work_rx);
1319}
1320
1321static void sci_request_dma(struct uart_port *port)
1322{
1323 struct sci_port *s = to_sci_port(port);
1324 struct sh_dmae_slave *param;
1325 struct dma_chan *chan;
1326 dma_cap_mask_t mask;
1327 int nent;
1328
1329 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001330 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001331
Paul Mundtce6738b2011-01-19 15:24:40 +09001332 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001333 return;
1334
1335 dma_cap_zero(mask);
1336 dma_cap_set(DMA_SLAVE, mask);
1337
1338 param = &s->param_tx;
1339
1340 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001341 param->slave_id = s->cfg->dma_slave_tx;
1342 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001343
1344 s->cookie_tx = -EINVAL;
1345 chan = dma_request_channel(mask, filter, param);
1346 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1347 if (chan) {
1348 s->chan_tx = chan;
1349 sg_init_table(&s->sg_tx, 1);
1350 /* UART circular tx buffer is an aligned page. */
1351 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1352 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1353 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1354 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1355 if (!nent)
1356 sci_tx_dma_release(s, false);
1357 else
1358 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1359 sg_dma_len(&s->sg_tx),
1360 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1361
1362 s->sg_len_tx = nent;
1363
1364 INIT_WORK(&s->work_tx, work_fn_tx);
1365 }
1366
1367 param = &s->param_rx;
1368
1369 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001370 param->slave_id = s->cfg->dma_slave_rx;
1371 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001372
1373 chan = dma_request_channel(mask, filter, param);
1374 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1375 if (chan) {
1376 dma_addr_t dma[2];
1377 void *buf[2];
1378 int i;
1379
1380 s->chan_rx = chan;
1381
1382 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1383 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1384 &dma[0], GFP_KERNEL);
1385
1386 if (!buf[0]) {
1387 dev_warn(port->dev,
1388 "failed to allocate dma buffer, using PIO\n");
1389 sci_rx_dma_release(s, true);
1390 return;
1391 }
1392
1393 buf[1] = buf[0] + s->buf_len_rx;
1394 dma[1] = dma[0] + s->buf_len_rx;
1395
1396 for (i = 0; i < 2; i++) {
1397 struct scatterlist *sg = &s->sg_rx[i];
1398
1399 sg_init_table(sg, 1);
1400 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1401 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001402 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001403 }
1404
1405 INIT_WORK(&s->work_rx, work_fn_rx);
1406 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1407
1408 sci_submit_rx(s);
1409 }
1410}
1411
1412static void sci_free_dma(struct uart_port *port)
1413{
1414 struct sci_port *s = to_sci_port(port);
1415
Paul Mundtce6738b2011-01-19 15:24:40 +09001416 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001417 return;
1418
1419 if (s->chan_tx)
1420 sci_tx_dma_release(s, false);
1421 if (s->chan_rx)
1422 sci_rx_dma_release(s, false);
1423}
Paul Mundt27bd1072011-01-19 15:37:31 +09001424#else
1425static inline void sci_request_dma(struct uart_port *port)
1426{
1427}
1428
1429static inline void sci_free_dma(struct uart_port *port)
1430{
1431}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001432#endif
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434static int sci_startup(struct uart_port *port)
1435{
Magnus Damma5660ad2009-01-21 15:14:38 +00001436 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001437 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001439 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1440
Paul Mundte108b2c2006-09-27 16:32:13 +09001441 if (s->enable)
1442 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Paul Mundt073e84c2011-01-19 17:30:53 +09001444 ret = sci_request_irq(s);
1445 if (unlikely(ret < 0))
1446 return ret;
1447
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001448 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001449
Yoshinori Satod6569012005-10-14 15:59:12 -07001450 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001451 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 return 0;
1454}
1455
1456static void sci_shutdown(struct uart_port *port)
1457{
Magnus Damma5660ad2009-01-21 15:14:38 +00001458 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001460 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001463 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001464
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001465 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 sci_free_irq(s);
1467
Paul Mundte108b2c2006-09-27 16:32:13 +09001468 if (s->disable)
1469 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Paul Mundt26c92f32009-06-24 18:23:52 +09001472static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1473 unsigned long freq)
1474{
1475 switch (algo_id) {
1476 case SCBRR_ALGO_1:
1477 return ((freq + 16 * bps) / (16 * bps) - 1);
1478 case SCBRR_ALGO_2:
1479 return ((freq + 16 * bps) / (32 * bps) - 1);
1480 case SCBRR_ALGO_3:
1481 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1482 case SCBRR_ALGO_4:
1483 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1484 case SCBRR_ALGO_5:
1485 return (((freq * 1000 / 32) / bps) - 1);
1486 }
1487
1488 /* Warn, but use a safe default */
1489 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001490
Paul Mundt26c92f32009-06-24 18:23:52 +09001491 return ((freq + 16 * bps) / (32 * bps) - 1);
1492}
1493
Alan Cox606d0992006-12-08 02:38:45 -08001494static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1495 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Paul Mundt00b9de92009-06-24 17:53:33 +09001497 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001498 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001499 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001500 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Magnus Damm154280f2009-12-22 03:37:28 +00001502 /*
1503 * earlyprintk comes here early on with port->uartclk set to zero.
1504 * the clock framework is not up and running at this point so here
1505 * we assume that 115200 is the maximum baud rate. please note that
1506 * the baud rate is not programmed during earlyprintk - it is assumed
1507 * that the previous boot loader has enabled required clocks and
1508 * setup the baud rate generator hardware for us already.
1509 */
1510 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1511
1512 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1513 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001514 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Alexandre Courbot36003382011-03-03 08:04:42 +00001516 if (s->enable)
1517 s->enable(port);
1518
Paul Mundte108b2c2006-09-27 16:32:13 +09001519 do {
1520 status = sci_in(port, SCxSR);
1521 } while (!(status & SCxSR_TEND(port)));
1522
1523 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1524
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001525 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001526 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001527
1528 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001529
Paul Mundte108b2c2006-09-27 16:32:13 +09001530 if ((termios->c_cflag & CSIZE) == CS7)
1531 smr_val |= 0x40;
1532 if (termios->c_cflag & PARENB)
1533 smr_val |= 0x20;
1534 if (termios->c_cflag & PARODD)
1535 smr_val |= 0x30;
1536 if (termios->c_cflag & CSTOPB)
1537 smr_val |= 0x08;
1538
1539 uart_update_timeout(port, termios->c_cflag, baud);
1540
1541 sci_out(port, SCSMR, smr_val);
1542
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001543 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001544 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001547 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1549 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001550 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 sci_out(port, SCBRR, t);
1554 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1555 }
1556
Paul Mundtd5701642008-12-16 20:07:27 +09001557 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001558 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001559
Paul Mundtce6738b2011-01-19 15:24:40 +09001560 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001562#ifdef CONFIG_SERIAL_SH_SCI_DMA
1563 /*
1564 * Calculate delay for 1.5 DMA buffers: see
1565 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1566 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1567 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1568 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1569 * sizes), but it has been found out experimentally, that this is not
1570 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1571 * as a minimum seem to work perfectly.
1572 */
1573 if (s->chan_rx) {
1574 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1575 port->fifosize / 2;
1576 dev_dbg(port->dev,
1577 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1578 s->rx_timeout * 1000 / HZ, port->timeout);
1579 if (s->rx_timeout < msecs_to_jiffies(20))
1580 s->rx_timeout = msecs_to_jiffies(20);
1581 }
1582#endif
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001585 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001586
1587 if (s->disable)
1588 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591static const char *sci_type(struct uart_port *port)
1592{
1593 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001594 case PORT_IRDA:
1595 return "irda";
1596 case PORT_SCI:
1597 return "sci";
1598 case PORT_SCIF:
1599 return "scif";
1600 case PORT_SCIFA:
1601 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001602 case PORT_SCIFB:
1603 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
Paul Mundtfa439722008-09-04 18:53:58 +09001606 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Paul Mundte2651642011-01-20 21:24:03 +09001609static inline unsigned long sci_port_size(struct uart_port *port)
1610{
1611 /*
1612 * Pick an arbitrary size that encapsulates all of the base
1613 * registers by default. This can be optimized later, or derived
1614 * from platform resource data at such a time that ports begin to
1615 * behave more erratically.
1616 */
1617 return 64;
1618}
1619
Paul Mundtf6e94952011-01-21 15:25:36 +09001620static int sci_remap_port(struct uart_port *port)
1621{
1622 unsigned long size = sci_port_size(port);
1623
1624 /*
1625 * Nothing to do if there's already an established membase.
1626 */
1627 if (port->membase)
1628 return 0;
1629
1630 if (port->flags & UPF_IOREMAP) {
1631 port->membase = ioremap_nocache(port->mapbase, size);
1632 if (unlikely(!port->membase)) {
1633 dev_err(port->dev, "can't remap port#%d\n", port->line);
1634 return -ENXIO;
1635 }
1636 } else {
1637 /*
1638 * For the simple (and majority of) cases where we don't
1639 * need to do any remapping, just cast the cookie
1640 * directly.
1641 */
1642 port->membase = (void __iomem *)port->mapbase;
1643 }
1644
1645 return 0;
1646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648static void sci_release_port(struct uart_port *port)
1649{
Paul Mundte2651642011-01-20 21:24:03 +09001650 if (port->flags & UPF_IOREMAP) {
1651 iounmap(port->membase);
1652 port->membase = NULL;
1653 }
1654
1655 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658static int sci_request_port(struct uart_port *port)
1659{
Paul Mundte2651642011-01-20 21:24:03 +09001660 unsigned long size = sci_port_size(port);
1661 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001662 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Paul Mundt10205202011-01-21 16:00:31 +09001664 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001665 if (unlikely(res == NULL))
1666 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Paul Mundtf6e94952011-01-21 15:25:36 +09001668 ret = sci_remap_port(port);
1669 if (unlikely(ret != 0)) {
1670 release_resource(res);
1671 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001672 }
Paul Mundte2651642011-01-20 21:24:03 +09001673
1674 return 0;
1675}
1676
1677static void sci_config_port(struct uart_port *port, int flags)
1678{
1679 if (flags & UART_CONFIG_TYPE) {
1680 struct sci_port *sport = to_sci_port(port);
1681
1682 port->type = sport->cfg->type;
1683 sci_request_port(port);
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1688{
Magnus Damma5660ad2009-01-21 15:14:38 +00001689 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Paul Mundtce6738b2011-01-19 15:24:40 +09001691 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return -EINVAL;
1693 if (ser->baud_base < 2400)
1694 /* No paper tape reader for Mitch.. */
1695 return -EINVAL;
1696
1697 return 0;
1698}
1699
1700static struct uart_ops sci_uart_ops = {
1701 .tx_empty = sci_tx_empty,
1702 .set_mctrl = sci_set_mctrl,
1703 .get_mctrl = sci_get_mctrl,
1704 .start_tx = sci_start_tx,
1705 .stop_tx = sci_stop_tx,
1706 .stop_rx = sci_stop_rx,
1707 .enable_ms = sci_enable_ms,
1708 .break_ctl = sci_break_ctl,
1709 .startup = sci_startup,
1710 .shutdown = sci_shutdown,
1711 .set_termios = sci_set_termios,
1712 .type = sci_type,
1713 .release_port = sci_release_port,
1714 .request_port = sci_request_port,
1715 .config_port = sci_config_port,
1716 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001717#ifdef CONFIG_CONSOLE_POLL
1718 .poll_get_char = sci_poll_get_char,
1719 .poll_put_char = sci_poll_put_char,
1720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721};
1722
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001723static int __devinit sci_init_single(struct platform_device *dev,
1724 struct sci_port *sci_port,
1725 unsigned int index,
1726 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001727{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001728 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001729
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001730 port->ops = &sci_uart_ops;
1731 port->iotype = UPIO_MEM;
1732 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001733
1734 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001735 case PORT_SCIFB:
1736 port->fifosize = 256;
1737 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001738 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001739 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001740 break;
1741 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001742 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001743 break;
1744 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001745 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001746 break;
1747 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001748
1749 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001750 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1751 if (IS_ERR(sci_port->iclk)) {
1752 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1753 if (IS_ERR(sci_port->iclk)) {
1754 dev_err(&dev->dev, "can't get iclk\n");
1755 return PTR_ERR(sci_port->iclk);
1756 }
1757 }
1758
1759 /*
1760 * The function clock is optional, ignore it if we can't
1761 * find it.
1762 */
1763 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1764 if (IS_ERR(sci_port->fclk))
1765 sci_port->fclk = NULL;
1766
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001767 sci_port->enable = sci_clk_enable;
1768 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001769 port->dev = &dev->dev;
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001770
1771 pm_runtime_enable(&dev->dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001772 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001773
Magnus Damm7ed7e072009-01-21 15:14:14 +00001774 sci_port->break_timer.data = (unsigned long)sci_port;
1775 sci_port->break_timer.function = sci_break_timer;
1776 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001777
Paul Mundtce6738b2011-01-19 15:24:40 +09001778 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001779
Paul Mundtce6738b2011-01-19 15:24:40 +09001780 port->mapbase = p->mapbase;
1781 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001782 port->flags = p->flags;
Paul Mundtce6738b2011-01-19 15:24:40 +09001783
1784 /*
1785 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1786 * for the multi-IRQ ports, which is where we are primarily
1787 * concerned with the shutdown path synchronization.
1788 *
1789 * For the muxed case there's nothing more to do.
1790 */
Magnus Damm54aa89e2011-04-21 13:08:46 +00001791 port->irq = p->irqs[SCIx_RXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001792
Paul Mundtce6738b2011-01-19 15:24:40 +09001793 if (p->dma_dev)
1794 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1795 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001796
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001797 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001801static void serial_console_putchar(struct uart_port *port, int ch)
1802{
1803 sci_poll_put_char(port, ch);
1804}
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806/*
1807 * Print a string to the serial port trying not to disturb
1808 * any possible real use of the port...
1809 */
1810static void serial_console_write(struct console *co, const char *s,
1811 unsigned count)
1812{
Paul Mundt906b17d2011-01-21 16:19:53 +09001813 struct sci_port *sci_port = &sci_ports[co->index];
1814 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001815 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001816
Magnus Damm501b8252009-01-21 15:14:30 +00001817 if (sci_port->enable)
1818 sci_port->enable(port);
1819
1820 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001821
1822 /* wait until fifo is empty and last bit has been transmitted */
1823 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1824 while ((sci_in(port, SCxSR) & bits) != bits)
1825 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001826
Magnus Damm345e5a72009-11-05 14:34:57 +00001827 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001828 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001831static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001833 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct uart_port *port;
1835 int baud = 115200;
1836 int bits = 8;
1837 int parity = 'n';
1838 int flow = 'n';
1839 int ret;
1840
Paul Mundte108b2c2006-09-27 16:32:13 +09001841 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09001842 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09001843 */
Paul Mundt906b17d2011-01-21 16:19:53 +09001844 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09001845 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001846
Paul Mundt906b17d2011-01-21 16:19:53 +09001847 sci_port = &sci_ports[co->index];
1848 port = &sci_port->port;
1849
Alexandre Courbotb2267a62011-02-09 03:18:46 +00001850 /*
1851 * Refuse to handle uninitialized ports.
1852 */
1853 if (!port->ops)
1854 return -ENODEV;
1855
Paul Mundtf6e94952011-01-21 15:25:36 +09001856 ret = sci_remap_port(port);
1857 if (unlikely(ret != 0))
1858 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001859
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001860 if (sci_port->enable)
1861 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (options)
1864 uart_parse_options(options, &baud, &parity, &bits, &flow);
1865
1866 ret = uart_set_options(port, co, baud, parity, bits, flow);
1867#if defined(__H8300H__) || defined(__H8300S__)
1868 /* disable rx interrupt */
1869 if (ret == 0)
1870 sci_stop_rx(port);
1871#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001872 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return ret;
1874}
1875
1876static struct console serial_console = {
1877 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09001878 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .write = serial_console_write,
1880 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001881 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09001883 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884};
1885
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001886static struct console early_serial_console = {
1887 .name = "early_ttySC",
1888 .write = serial_console_write,
1889 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09001890 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001891};
Paul Mundtecdf8a42011-01-21 00:05:48 +09001892
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001893static char early_serial_buf[32];
1894
Paul Mundtecdf8a42011-01-21 00:05:48 +09001895static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1896{
1897 struct plat_sci_port *cfg = pdev->dev.platform_data;
1898
1899 if (early_serial_console.data)
1900 return -EEXIST;
1901
1902 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09001903
Paul Mundt906b17d2011-01-21 16:19:53 +09001904 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09001905
1906 serial_console_setup(&early_serial_console, early_serial_buf);
1907
1908 if (!strstr(early_serial_buf, "keep"))
1909 early_serial_console.flags |= CON_BOOT;
1910
1911 register_console(&early_serial_console);
1912 return 0;
1913}
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00001914
1915#define SCI_CONSOLE (&serial_console)
1916
Paul Mundtecdf8a42011-01-21 00:05:48 +09001917#else
1918static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1919{
1920 return -EINVAL;
1921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Nobuhiro Iwamatsu6a8c9792011-03-24 02:20:56 +00001923#define SCI_CONSOLE NULL
1924
1925#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927static char banner[] __initdata =
1928 KERN_INFO "SuperH SCI(F) driver initialized\n";
1929
1930static struct uart_driver sci_uart_driver = {
1931 .owner = THIS_MODULE,
1932 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 .dev_name = "ttySC",
1934 .major = SCI_MAJOR,
1935 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001936 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 .cons = SCI_CONSOLE,
1938};
1939
Paul Mundt54507f62009-05-08 23:48:33 +09001940static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001941{
Paul Mundtd535a232011-01-19 17:19:35 +09001942 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001943
Paul Mundtd535a232011-01-19 17:19:35 +09001944 cpufreq_unregister_notifier(&port->freq_transition,
1945 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001946
Paul Mundtd535a232011-01-19 17:19:35 +09001947 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00001948
Paul Mundtd535a232011-01-19 17:19:35 +09001949 clk_put(port->iclk);
1950 clk_put(port->fclk);
1951
Magnus Damm5e50d2d2011-04-19 10:38:25 +00001952 pm_runtime_disable(&dev->dev);
Magnus Damme552de22009-01-21 15:13:42 +00001953 return 0;
1954}
1955
Magnus Damm0ee70712009-01-21 15:13:50 +00001956static int __devinit sci_probe_single(struct platform_device *dev,
1957 unsigned int index,
1958 struct plat_sci_port *p,
1959 struct sci_port *sciport)
1960{
Magnus Damm0ee70712009-01-21 15:13:50 +00001961 int ret;
1962
1963 /* Sanity check */
1964 if (unlikely(index >= SCI_NPORTS)) {
1965 dev_notice(&dev->dev, "Attempting to register port "
1966 "%d when only %d are available.\n",
1967 index+1, SCI_NPORTS);
1968 dev_notice(&dev->dev, "Consider bumping "
1969 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1970 return 0;
1971 }
1972
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001973 ret = sci_init_single(dev, sciport, index, p);
1974 if (ret)
1975 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001976
Paul Mundtd535a232011-01-19 17:19:35 +09001977 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00001978}
1979
Paul Mundte108b2c2006-09-27 16:32:13 +09001980static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Paul Mundte108b2c2006-09-27 16:32:13 +09001982 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09001983 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09001984 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00001985
Paul Mundtecdf8a42011-01-21 00:05:48 +09001986 /*
1987 * If we've come here via earlyprintk initialization, head off to
1988 * the special early probe. We don't have sufficient device state
1989 * to make it beyond this yet.
1990 */
1991 if (is_early_platform_device(dev))
1992 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001993
Paul Mundtd535a232011-01-19 17:19:35 +09001994 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00001995
Paul Mundt906b17d2011-01-21 16:19:53 +09001996 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09001997 if (ret)
1998 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001999
Paul Mundtd535a232011-01-19 17:19:35 +09002000 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Paul Mundtd535a232011-01-19 17:19:35 +09002002 ret = cpufreq_register_notifier(&sp->freq_transition,
2003 CPUFREQ_TRANSITION_NOTIFIER);
2004 if (unlikely(ret < 0))
2005 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007#ifdef CONFIG_SH_STANDARD_BIOS
2008 sh_bios_gdb_detach();
2009#endif
2010
Paul Mundte108b2c2006-09-27 16:32:13 +09002011 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002012
2013err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002014 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002015 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002016}
2017
Paul Mundt6daa79b2009-06-15 07:07:38 +09002018static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002019{
Paul Mundtd535a232011-01-19 17:19:35 +09002020 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002021
Paul Mundtd535a232011-01-19 17:19:35 +09002022 if (sport)
2023 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002024
2025 return 0;
2026}
2027
Paul Mundt6daa79b2009-06-15 07:07:38 +09002028static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002029{
Paul Mundtd535a232011-01-19 17:19:35 +09002030 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002031
Paul Mundtd535a232011-01-19 17:19:35 +09002032 if (sport)
2033 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002034
2035 return 0;
2036}
2037
Alexey Dobriyan47145212009-12-14 18:00:08 -08002038static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002039 .suspend = sci_suspend,
2040 .resume = sci_resume,
2041};
2042
Paul Mundte108b2c2006-09-27 16:32:13 +09002043static struct platform_driver sci_driver = {
2044 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002045 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002046 .driver = {
2047 .name = "sh-sci",
2048 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002049 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002050 },
2051};
2052
2053static int __init sci_init(void)
2054{
2055 int ret;
2056
2057 printk(banner);
2058
Paul Mundte108b2c2006-09-27 16:32:13 +09002059 ret = uart_register_driver(&sci_uart_driver);
2060 if (likely(ret == 0)) {
2061 ret = platform_driver_register(&sci_driver);
2062 if (unlikely(ret))
2063 uart_unregister_driver(&sci_uart_driver);
2064 }
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return ret;
2067}
2068
2069static void __exit sci_exit(void)
2070{
Paul Mundte108b2c2006-09-27 16:32:13 +09002071 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 uart_unregister_driver(&sci_uart_driver);
2073}
2074
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002075#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2076early_platform_init_buffer("earlyprintk", &sci_driver,
2077 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2078#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079module_init(sci_init);
2080module_exit(sci_exit);
2081
Paul Mundte108b2c2006-09-27 16:32:13 +09002082MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002083MODULE_ALIAS("platform:sh-sci");