blob: eb7958c675a80c7989ffeaced1fe65ef37ce0f6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundtf43dc232011-01-13 15:06:28 +09006 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090050#include <linux/dmaengine.h>
51#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090053
54#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090055#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yoshinori Sato168f3622009-04-28 04:40:15 +000058#ifdef CONFIG_H8300
59#include <asm/gpio.h>
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "sh-sci.h"
63
Paul Mundte108b2c2006-09-27 16:32:13 +090064struct sci_port {
65 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Paul Mundtce6738b2011-01-19 15:24:40 +090067 /* Platform configuration */
68 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090069
Paul Mundte108b2c2006-09-27 16:32:13 +090070 /* Port enable callback */
71 void (*enable)(struct uart_port *port);
72
73 /* Port disable callback */
74 void (*disable)(struct uart_port *port);
75
76 /* Break timer */
77 struct timer_list break_timer;
78 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090079
Magnus Damm501b8252009-01-21 15:14:30 +000080 /* Interface clock */
81 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090082 /* Function clock */
83 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090084
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090085 struct dma_chan *chan_tx;
86 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090087
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090088#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090089 struct dma_async_tx_descriptor *desc_tx;
90 struct dma_async_tx_descriptor *desc_rx[2];
91 dma_cookie_t cookie_tx;
92 dma_cookie_t cookie_rx[2];
93 dma_cookie_t active_rx;
94 struct scatterlist sg_tx;
95 unsigned int sg_len_tx;
96 struct scatterlist sg_rx[2];
97 size_t buf_len_rx;
98 struct sh_dmae_slave param_tx;
99 struct sh_dmae_slave param_rx;
100 struct work_struct work_tx;
101 struct work_struct work_rx;
102 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000103 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900104#endif
Magnus Damme552de22009-01-21 15:13:42 +0000105
Paul Mundtd535a232011-01-19 17:19:35 +0900106 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900110static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100111static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900112static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Paul Mundte108b2c2006-09-27 16:32:13 +0900114#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
115
116static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static struct uart_driver sci_uart_driver;
118
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900119static inline struct sci_port *
120to_sci_port(struct uart_port *uart)
121{
122 return container_of(uart, struct sci_port, port);
123}
124
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900125#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900126
127#ifdef CONFIG_CONSOLE_POLL
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900128static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned short status;
131 int c;
132
Paul Mundte108b2c2006-09-27 16:32:13 +0900133 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 status = sci_in(port, SCxSR);
135 if (status & SCxSR_ERRORS(port)) {
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900136 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 continue;
138 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500139 break;
140 } while (1);
141
142 if (!(status & SCxSR_RDxF(port)))
143 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900146
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900147 /* Dummy read */
148 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return c;
152}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900155static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 unsigned short status;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 do {
160 status = sci_in(port, SCxSR);
161 } while (!(status & SCxSR_TDxE(port)));
162
Paul Mundt272966c2008-11-13 17:46:06 +0900163 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000164 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900166#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Paul Mundt15c73aa2008-10-02 19:47:12 +0900168#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900169static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int ch = (port->mapbase - SMR0) >> 3;
172
173 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900174 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
175 h8300_sci_pins[ch].rx,
176 H8300_GPIO_INPUT);
177 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
178 h8300_sci_pins[ch].tx,
179 H8300_GPIO_OUTPUT);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* tx mark output*/
182 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
183}
Paul Mundtd5701642008-12-16 20:07:27 +0900184#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
185static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900186{
Paul Mundtd5701642008-12-16 20:07:27 +0900187 if (port->mapbase == 0xA4400000) {
188 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
189 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
190 } else if (port->mapbase == 0xA4410000)
191 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900192}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900193#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900194static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900195{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900196 unsigned short data;
197
198 if (cflag & CRTSCTS) {
199 /* enable RTS/CTS */
200 if (port->mapbase == 0xa4430000) { /* SCIF0 */
201 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900202 data = __raw_readw(PORT_PTCR);
203 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900204 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
205 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900206 data = __raw_readw(PORT_PVCR);
207 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900208 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900209 } else {
210 if (port->mapbase == 0xa4430000) { /* SCIF0 */
211 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900212 data = __raw_readw(PORT_PTCR);
213 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900214 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
215 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900216 data = __raw_readw(PORT_PVCR);
217 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218 }
219 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900220}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800221#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900222/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900223static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800225 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Paul Mundtb7a76e42006-02-01 03:06:06 -0800227 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900228 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800229 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900230 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800231
Paul Mundtd5701642008-12-16 20:07:27 +0900232 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900234 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Clear out SCP7MD1,0, SCP4MD1,0,
236 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900237 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paul Mundt32b53072009-12-24 14:52:43 +0900239 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900241 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Paul Mundt41504c32006-12-11 20:28:03 +0900244#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900245static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900246{
Magnus Damm346b7462008-04-23 21:25:29 +0900247 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900248
Magnus Damm346b7462008-04-23 21:25:29 +0900249 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900250 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900251 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900252 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900253 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900254
Paul Mundtd5701642008-12-16 20:07:27 +0900255 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900256 }
Paul Mundt41504c32006-12-11 20:28:03 +0900257}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900258#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
259 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900260 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900261 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900262 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900263 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900264static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
265{
266 if (!(cflag & CRTSCTS))
267 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
268}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900269#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900270static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
271{
272 if (!(cflag & CRTSCTS))
273 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
274}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800275#else
Paul Mundtd5701642008-12-16 20:07:27 +0900276static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
277{
278 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
Paul Mundte108b2c2006-09-27 16:32:13 +0900280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Paul Mundt32351a22007-03-12 14:38:59 +0900282#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
283 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900284 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
285 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900286static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900287{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900288 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900289}
290
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900291static int scif_txroom(struct uart_port *port)
292{
293 return SCIF_TXROOM_MAX - scif_txfill(port);
294}
295
296static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900297{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900298 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900299}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900300#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900301static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900302{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900303 if (port->mapbase == 0xffe00000 ||
304 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900305 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900306 return sci_in(port, SCTFDR) & 0xff;
307 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900308 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900309 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900310}
311
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900312static int scif_txroom(struct uart_port *port)
313{
314 if (port->mapbase == 0xffe00000 ||
315 port->mapbase == 0xffe08000)
316 /* SCIF0/1*/
317 return SCIF_TXROOM_MAX - scif_txfill(port);
318 else
319 /* SCIF2 */
320 return SCIF2_TXROOM_MAX - scif_txfill(port);
321}
322
323static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900324{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900325 if ((port->mapbase == 0xffe00000) ||
326 (port->mapbase == 0xffe08000)) {
327 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900328 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900329 } else {
330 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900331 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900332 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900333}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000334#elif defined(CONFIG_ARCH_SH7372)
335static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900336{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000337 if (port->type == PORT_SCIFA)
338 return sci_in(port, SCFDR) >> 8;
339 else
340 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900341}
342
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000343static int scif_txroom(struct uart_port *port)
344{
345 return port->fifosize - scif_txfill(port);
346}
347
348static int scif_rxfill(struct uart_port *port)
349{
350 if (port->type == PORT_SCIFA)
351 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
352 else
353 return sci_in(port, SCRFDR);
354}
Paul Mundte108b2c2006-09-27 16:32:13 +0900355#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900356static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900357{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900358 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900359}
360
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900361static int scif_txroom(struct uart_port *port)
362{
363 return SCIF_TXROOM_MAX - scif_txfill(port);
364}
365
366static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900367{
368 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900372static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900373{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900374 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900375}
376
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900377static int sci_txroom(struct uart_port *port)
378{
379 return !sci_txfill(port);
380}
381
382static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900383{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900384 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/* ********************************************************************** *
388 * the interrupt related routines *
389 * ********************************************************************** */
390
391static void sci_transmit_chars(struct uart_port *port)
392{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700393 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned short status;
396 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900397 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 status = sci_in(port, SCxSR);
400 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900402 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900403 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900404 else
Paul Mundt8e698612009-06-24 19:44:32 +0900405 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return;
408 }
409
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900410 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900411 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900412 else
413 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 do {
416 unsigned char c;
417
418 if (port->x_char) {
419 c = port->x_char;
420 port->x_char = 0;
421 } else if (!uart_circ_empty(xmit) && !stopped) {
422 c = xmit->buf[xmit->tail];
423 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
424 } else {
425 break;
426 }
427
428 sci_out(port, SCxTDR, c);
429
430 port->icount.tx++;
431 } while (--count > 0);
432
433 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
434
435 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
436 uart_write_wakeup(port);
437 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100438 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ctrl = sci_in(port, SCSCR);
441
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900442 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sci_in(port, SCxSR); /* Dummy read */
444 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Paul Mundt8e698612009-06-24 19:44:32 +0900447 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450}
451
452/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900453#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900455static void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900457 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700458 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int i, count, copied = 0;
460 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800461 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 status = sci_in(port, SCxSR);
464 if (!(status & SCxSR_RDxF(port)))
465 return;
466
467 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900468 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900469 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900470 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900471 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800474 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* If for any reason we can't copy more data, we're done! */
477 if (count == 0)
478 break;
479
480 if (port->type == PORT_SCI) {
481 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900482 if (uart_handle_sysrq_char(port, c) ||
483 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900485 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900486 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900488 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 char c = sci_in(port, SCxRDR);
490 status = sci_in(port, SCxSR);
491#if defined(CONFIG_CPU_SH3)
492 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900493 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if ((c == 0) &&
495 (status & SCxSR_FER(port))) {
496 count--; i--;
497 continue;
498 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900501 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900502 sci_port->break_flag = 0;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (STEPFN(c)) {
505 count--; i--;
506 continue;
507 }
508 }
509#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100510 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 count--; i--;
512 continue;
513 }
514
515 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900516 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800517 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900518 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900519 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800520 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900521 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800522 } else
523 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900524
Alan Cox33f0f882006-01-09 20:54:13 -0800525 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 }
528
529 sci_in(port, SCxSR); /* dummy read */
530 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 copied += count;
533 port->icount.rx += count;
534 }
535
536 if (copied) {
537 /* Tell the rest of the system the news. New characters! */
538 tty_flip_buffer_push(tty);
539 } else {
540 sci_in(port, SCxSR); /* dummy read */
541 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
542 }
543}
544
545#define SCI_BREAK_JIFFIES (HZ/20)
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900546
547/*
548 * The sci generates interrupts during the break,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * 1 per millisecond or so during the break period, for 9600 baud.
550 * So dont bother disabling interrupts.
551 * But dont want more than 1 break event.
552 * Use a kernel timer to periodically poll the rx line until
553 * the break is finished.
554 */
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900555static inline void sci_schedule_break_timer(struct sci_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Paul Mundtbc9b3f52011-01-20 23:30:19 +0900557 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/* Ensure that two consecutive samples find the break over. */
561static void sci_break_timer(unsigned long data)
562{
Paul Mundte108b2c2006-09-27 16:32:13 +0900563 struct sci_port *port = (struct sci_port *)data;
564
565 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 sci_schedule_break_timer(port);
568 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* break is over. */
570 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900571 sci_schedule_break_timer(port);
572 } else
573 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900576static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int copied = 0;
579 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700580 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Paul Mundte108b2c2006-09-27 16:32:13 +0900582 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900584 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800585 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900586
587 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Paul Mundte108b2c2006-09-27 16:32:13 +0900590 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (sci_rxd_in(port) == 0) {
592 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900593 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900594
595 if (!sci_port->break_flag) {
596 sci_port->break_flag = 1;
597 sci_schedule_break_timer(sci_port);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900600 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900602
603 dev_dbg(port->dev, "BREAK detected\n");
604
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900606 copied++;
607 }
608
Paul Mundte108b2c2006-09-27 16:32:13 +0900609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900611 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800612 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900613
614 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 }
617
Paul Mundte108b2c2006-09-27 16:32:13 +0900618 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900620 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
621 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900622
623 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Alan Cox33f0f882006-01-09 20:54:13 -0800626 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return copied;
630}
631
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900632static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900633{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700634 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900635 int copied = 0;
636
637 if (port->type != PORT_SCIF)
638 return 0;
639
640 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
641 sci_out(port, SCLSR, 0);
642
643 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
644 tty_flip_buffer_push(tty);
645
646 dev_notice(port->dev, "overrun error\n");
647 copied++;
648 }
649
650 return copied;
651}
652
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900653static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 int copied = 0;
656 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700657 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000658 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900660 if (uart_handle_break(port))
661 return 0;
662
Paul Mundtb7a76e42006-02-01 03:06:06 -0800663 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#if defined(CONFIG_CPU_SH3)
665 /* Debounce break */
666 s->break_flag = 1;
667#endif
668 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900669 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800670 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900671
672 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Alan Cox33f0f882006-01-09 20:54:13 -0800675 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900677
Paul Mundtd830fa42008-12-16 19:29:38 +0900678 copied += sci_handle_fifo_overrun(port);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return copied;
681}
682
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900683static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900685#ifdef CONFIG_SERIAL_SH_SCI_DMA
686 struct uart_port *port = ptr;
687 struct sci_port *s = to_sci_port(port);
688
689 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900690 u16 scr = sci_in(port, SCSCR);
691 u16 ssr = sci_in(port, SCxSR);
692
693 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000694 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000695 disable_irq_nosync(irq);
696 scr |= 0x4000;
697 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900698 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000699 }
700 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900701 /* Clear current interrupt */
702 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000703 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
704 jiffies, s->rx_timeout);
705 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900706
707 return IRQ_HANDLED;
708 }
709#endif
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* I think sci_receive_chars has to be called irrespective
712 * of whether the I_IXOFF is set, otherwise, how is the interrupt
713 * to be disabled?
714 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900715 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return IRQ_HANDLED;
718}
719
David Howells7d12e782006-10-05 14:55:46 +0100720static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900723 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Stuart Menefyfd78a762009-07-29 23:01:24 +0900725 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900727 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 return IRQ_HANDLED;
730}
731
David Howells7d12e782006-10-05 14:55:46 +0100732static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct uart_port *port = ptr;
735
736 /* Handle errors */
737 if (port->type == PORT_SCI) {
738 if (sci_handle_errors(port)) {
739 /* discard character in rx buffer */
740 sci_in(port, SCxSR);
741 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
742 }
743 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900744 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100745 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
749
750 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100751 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 return IRQ_HANDLED;
754}
755
David Howells7d12e782006-10-05 14:55:46 +0100756static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct uart_port *port = ptr;
759
760 /* Handle BREAKs */
761 sci_handle_breaks(port);
762 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
763
764 return IRQ_HANDLED;
765}
766
Paul Mundtf43dc232011-01-13 15:06:28 +0900767static inline unsigned long port_rx_irq_mask(struct uart_port *port)
768{
769 /*
770 * Not all ports (such as SCIFA) will support REIE. Rather than
771 * special-casing the port type, we check the port initialization
772 * IRQ enable mask to see whether the IRQ is desired at all. If
773 * it's unset, it's logically inferred that there's no point in
774 * testing for it.
775 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900776 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900777}
778
David Howells7d12e782006-10-05 14:55:46 +0100779static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Magnus Damm44e18e92009-07-03 08:39:34 +0000781 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900782 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900783 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900784 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900786 ssr_status = sci_in(port, SCxSR);
787 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900788 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900791 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900792 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900793 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900794
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900795 /*
796 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
797 * DR flags
798 */
799 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900800 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900801 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000804 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900805 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000808 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900809 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Michael Trimarchia8884e32008-10-31 16:10:23 +0900811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
815 * Here we define a transistion notifier so that we can update all of our
816 * ports' baud rate when the peripheral clock changes.
817 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900818static int sci_notifier(struct notifier_block *self,
819 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Magnus Damme552de22009-01-21 15:13:42 +0000821 struct sci_port *sci_port;
822 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Paul Mundtd535a232011-01-19 17:19:35 +0900824 sci_port = container_of(self, struct sci_port, freq_transition);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000827 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900828 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900829
Paul Mundtd535a232011-01-19 17:19:35 +0900830 spin_lock_irqsave(&port->lock, flags);
831 port->uartclk = clk_get_rate(sci_port->iclk);
832 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return NOTIFY_OK;
836}
Magnus Damm501b8252009-01-21 15:14:30 +0000837
838static void sci_clk_enable(struct uart_port *port)
839{
840 struct sci_port *sci_port = to_sci_port(port);
841
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900842 clk_enable(sci_port->iclk);
843 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
844 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000845}
846
847static void sci_clk_disable(struct uart_port *port)
848{
849 struct sci_port *sci_port = to_sci_port(port);
850
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900851 clk_disable(sci_port->fclk);
852 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000853}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855static int sci_request_irq(struct sci_port *port)
856{
857 int i;
David Howells7d12e782006-10-05 14:55:46 +0100858 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
860 sci_br_interrupt,
861 };
862 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
863 "SCI Transmit Data Empty", "SCI Break" };
864
Paul Mundtce6738b2011-01-19 15:24:40 +0900865 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
866 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900868
Paul Mundtce6738b2011-01-19 15:24:40 +0900869 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900870 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900871 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -ENODEV;
873 }
874 } else {
875 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900876 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900878
Paul Mundtce6738b2011-01-19 15:24:40 +0900879 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900880 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900881 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -ENODEV;
883 }
884 }
885 }
886
887 return 0;
888}
889
890static void sci_free_irq(struct sci_port *port)
891{
892 int i;
893
Paul Mundtce6738b2011-01-19 15:24:40 +0900894 if (port->cfg->irqs[0] == port->cfg->irqs[1])
895 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +0900896 else {
Paul Mundtce6738b2011-01-19 15:24:40 +0900897 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
898 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 continue;
900
Paul Mundtce6738b2011-01-19 15:24:40 +0900901 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 }
904}
905
906static unsigned int sci_tx_empty(struct uart_port *port)
907{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000908 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900909 unsigned short in_tx_fifo = scif_txfill(port);
910
911 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
915{
916 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
917 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
918 /* If you have signals for DTR and DCD, please implement here. */
919}
920
921static unsigned int sci_get_mctrl(struct uart_port *port)
922{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900923 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 and CTS/RTS */
925
926 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
927}
928
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900929#ifdef CONFIG_SERIAL_SH_SCI_DMA
930static void sci_dma_tx_complete(void *arg)
931{
932 struct sci_port *s = arg;
933 struct uart_port *port = &s->port;
934 struct circ_buf *xmit = &port->state->xmit;
935 unsigned long flags;
936
937 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
938
939 spin_lock_irqsave(&port->lock, flags);
940
Magnus Dammf354a382010-03-19 04:47:01 +0000941 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900942 xmit->tail &= UART_XMIT_SIZE - 1;
943
Magnus Dammf354a382010-03-19 04:47:01 +0000944 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900945
946 async_tx_ack(s->desc_tx);
947 s->cookie_tx = -EINVAL;
948 s->desc_tx = NULL;
949
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900950 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
951 uart_write_wakeup(port);
952
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000953 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900954 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000955 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000956 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900957 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000958 }
959
960 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900961}
962
963/* Locking: called with port lock held */
964static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
965 size_t count)
966{
967 struct uart_port *port = &s->port;
968 int i, active, room;
969
970 room = tty_buffer_request_room(tty, count);
971
972 if (s->active_rx == s->cookie_rx[0]) {
973 active = 0;
974 } else if (s->active_rx == s->cookie_rx[1]) {
975 active = 1;
976 } else {
977 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
978 return 0;
979 }
980
981 if (room < count)
982 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
983 count - room);
984 if (!room)
985 return room;
986
987 for (i = 0; i < room; i++)
988 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
989 TTY_NORMAL);
990
991 port->icount.rx += room;
992
993 return room;
994}
995
996static void sci_dma_rx_complete(void *arg)
997{
998 struct sci_port *s = arg;
999 struct uart_port *port = &s->port;
1000 struct tty_struct *tty = port->state->port.tty;
1001 unsigned long flags;
1002 int count;
1003
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001004 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001005
1006 spin_lock_irqsave(&port->lock, flags);
1007
1008 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1009
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001010 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001011
1012 spin_unlock_irqrestore(&port->lock, flags);
1013
1014 if (count)
1015 tty_flip_buffer_push(tty);
1016
1017 schedule_work(&s->work_rx);
1018}
1019
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001020static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1021{
1022 struct dma_chan *chan = s->chan_rx;
1023 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001024
1025 s->chan_rx = NULL;
1026 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1027 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001028 if (sg_dma_address(&s->sg_rx[0]))
1029 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1030 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001031 if (enable_pio)
1032 sci_start_rx(port);
1033}
1034
1035static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1036{
1037 struct dma_chan *chan = s->chan_tx;
1038 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001039
1040 s->chan_tx = NULL;
1041 s->cookie_tx = -EINVAL;
1042 dma_release_channel(chan);
1043 if (enable_pio)
1044 sci_start_tx(port);
1045}
1046
1047static void sci_submit_rx(struct sci_port *s)
1048{
1049 struct dma_chan *chan = s->chan_rx;
1050 int i;
1051
1052 for (i = 0; i < 2; i++) {
1053 struct scatterlist *sg = &s->sg_rx[i];
1054 struct dma_async_tx_descriptor *desc;
1055
1056 desc = chan->device->device_prep_slave_sg(chan,
1057 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1058
1059 if (desc) {
1060 s->desc_rx[i] = desc;
1061 desc->callback = sci_dma_rx_complete;
1062 desc->callback_param = s;
1063 s->cookie_rx[i] = desc->tx_submit(desc);
1064 }
1065
1066 if (!desc || s->cookie_rx[i] < 0) {
1067 if (i) {
1068 async_tx_ack(s->desc_rx[0]);
1069 s->cookie_rx[0] = -EINVAL;
1070 }
1071 if (desc) {
1072 async_tx_ack(desc);
1073 s->cookie_rx[i] = -EINVAL;
1074 }
1075 dev_warn(s->port.dev,
1076 "failed to re-start DMA, using PIO\n");
1077 sci_rx_dma_release(s, true);
1078 return;
1079 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001080 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1081 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001082 }
1083
1084 s->active_rx = s->cookie_rx[0];
1085
1086 dma_async_issue_pending(chan);
1087}
1088
1089static void work_fn_rx(struct work_struct *work)
1090{
1091 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1092 struct uart_port *port = &s->port;
1093 struct dma_async_tx_descriptor *desc;
1094 int new;
1095
1096 if (s->active_rx == s->cookie_rx[0]) {
1097 new = 0;
1098 } else if (s->active_rx == s->cookie_rx[1]) {
1099 new = 1;
1100 } else {
1101 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1102 return;
1103 }
1104 desc = s->desc_rx[new];
1105
1106 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1107 DMA_SUCCESS) {
1108 /* Handle incomplete DMA receive */
1109 struct tty_struct *tty = port->state->port.tty;
1110 struct dma_chan *chan = s->chan_rx;
1111 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1112 async_tx);
1113 unsigned long flags;
1114 int count;
1115
Linus Walleij05827632010-05-17 16:30:42 -07001116 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001117 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1118 sh_desc->partial, sh_desc->cookie);
1119
1120 spin_lock_irqsave(&port->lock, flags);
1121 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1122 spin_unlock_irqrestore(&port->lock, flags);
1123
1124 if (count)
1125 tty_flip_buffer_push(tty);
1126
1127 sci_submit_rx(s);
1128
1129 return;
1130 }
1131
1132 s->cookie_rx[new] = desc->tx_submit(desc);
1133 if (s->cookie_rx[new] < 0) {
1134 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1135 sci_rx_dma_release(s, true);
1136 return;
1137 }
1138
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001139 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001140
1141 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1142 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001143}
1144
1145static void work_fn_tx(struct work_struct *work)
1146{
1147 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1148 struct dma_async_tx_descriptor *desc;
1149 struct dma_chan *chan = s->chan_tx;
1150 struct uart_port *port = &s->port;
1151 struct circ_buf *xmit = &port->state->xmit;
1152 struct scatterlist *sg = &s->sg_tx;
1153
1154 /*
1155 * DMA is idle now.
1156 * Port xmit buffer is already mapped, and it is one page... Just adjust
1157 * offsets and lengths. Since it is a circular buffer, we have to
1158 * transmit till the end, and then the rest. Take the port lock to get a
1159 * consistent xmit buffer state.
1160 */
1161 spin_lock_irq(&port->lock);
1162 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001163 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001164 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001165 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001166 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001167 spin_unlock_irq(&port->lock);
1168
Magnus Dammf354a382010-03-19 04:47:01 +00001169 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001170
1171 desc = chan->device->device_prep_slave_sg(chan,
1172 sg, s->sg_len_tx, DMA_TO_DEVICE,
1173 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1174 if (!desc) {
1175 /* switch to PIO */
1176 sci_tx_dma_release(s, true);
1177 return;
1178 }
1179
1180 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1181
1182 spin_lock_irq(&port->lock);
1183 s->desc_tx = desc;
1184 desc->callback = sci_dma_tx_complete;
1185 desc->callback_param = s;
1186 spin_unlock_irq(&port->lock);
1187 s->cookie_tx = desc->tx_submit(desc);
1188 if (s->cookie_tx < 0) {
1189 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1190 /* switch to PIO */
1191 sci_tx_dma_release(s, true);
1192 return;
1193 }
1194
1195 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1196 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1197
1198 dma_async_issue_pending(chan);
1199}
1200#endif
1201
Russell Kingb129a8c2005-08-31 10:12:14 +01001202static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001204 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001205 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001207#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001208 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001209 u16 new, scr = sci_in(port, SCSCR);
1210 if (s->chan_tx)
1211 new = scr | 0x8000;
1212 else
1213 new = scr & ~0x8000;
1214 if (new != scr)
1215 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001216 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001217
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001218 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1219 s->cookie_tx < 0)
1220 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001221#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001222
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001223 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001224 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1225 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001226 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Russell Kingb129a8c2005-08-31 10:12:14 +01001230static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned short ctrl;
1233
1234 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001236
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001237 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001238 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001239
Paul Mundt8e698612009-06-24 19:44:32 +09001240 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001245static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned short ctrl;
1248
Paul Mundtf43dc232011-01-13 15:06:28 +09001249 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1250
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001251 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001252 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static void sci_stop_rx(struct uart_port *port)
1258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 unsigned short ctrl;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001262
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001263 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001264 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001265
1266 ctrl &= ~port_rx_irq_mask(port);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271static void sci_enable_ms(struct uart_port *port)
1272{
1273 /* Nothing here yet .. */
1274}
1275
1276static void sci_break_ctl(struct uart_port *port, int break_state)
1277{
1278 /* Nothing here yet .. */
1279}
1280
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001281#ifdef CONFIG_SERIAL_SH_SCI_DMA
1282static bool filter(struct dma_chan *chan, void *slave)
1283{
1284 struct sh_dmae_slave *param = slave;
1285
1286 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1287 param->slave_id);
1288
1289 if (param->dma_dev == chan->device->dev) {
1290 chan->private = param;
1291 return true;
1292 } else {
1293 return false;
1294 }
1295}
1296
1297static void rx_timer_fn(unsigned long arg)
1298{
1299 struct sci_port *s = (struct sci_port *)arg;
1300 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001301 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001302
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001303 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001304 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001305 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001306 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001307 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001308 dev_dbg(port->dev, "DMA Rx timed out\n");
1309 schedule_work(&s->work_rx);
1310}
1311
1312static void sci_request_dma(struct uart_port *port)
1313{
1314 struct sci_port *s = to_sci_port(port);
1315 struct sh_dmae_slave *param;
1316 struct dma_chan *chan;
1317 dma_cap_mask_t mask;
1318 int nent;
1319
1320 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001321 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001322
Paul Mundtce6738b2011-01-19 15:24:40 +09001323 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001324 return;
1325
1326 dma_cap_zero(mask);
1327 dma_cap_set(DMA_SLAVE, mask);
1328
1329 param = &s->param_tx;
1330
1331 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001332 param->slave_id = s->cfg->dma_slave_tx;
1333 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001334
1335 s->cookie_tx = -EINVAL;
1336 chan = dma_request_channel(mask, filter, param);
1337 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1338 if (chan) {
1339 s->chan_tx = chan;
1340 sg_init_table(&s->sg_tx, 1);
1341 /* UART circular tx buffer is an aligned page. */
1342 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1343 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1344 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1345 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1346 if (!nent)
1347 sci_tx_dma_release(s, false);
1348 else
1349 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1350 sg_dma_len(&s->sg_tx),
1351 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1352
1353 s->sg_len_tx = nent;
1354
1355 INIT_WORK(&s->work_tx, work_fn_tx);
1356 }
1357
1358 param = &s->param_rx;
1359
1360 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001361 param->slave_id = s->cfg->dma_slave_rx;
1362 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001363
1364 chan = dma_request_channel(mask, filter, param);
1365 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1366 if (chan) {
1367 dma_addr_t dma[2];
1368 void *buf[2];
1369 int i;
1370
1371 s->chan_rx = chan;
1372
1373 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1374 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1375 &dma[0], GFP_KERNEL);
1376
1377 if (!buf[0]) {
1378 dev_warn(port->dev,
1379 "failed to allocate dma buffer, using PIO\n");
1380 sci_rx_dma_release(s, true);
1381 return;
1382 }
1383
1384 buf[1] = buf[0] + s->buf_len_rx;
1385 dma[1] = dma[0] + s->buf_len_rx;
1386
1387 for (i = 0; i < 2; i++) {
1388 struct scatterlist *sg = &s->sg_rx[i];
1389
1390 sg_init_table(sg, 1);
1391 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1392 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001393 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001394 }
1395
1396 INIT_WORK(&s->work_rx, work_fn_rx);
1397 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1398
1399 sci_submit_rx(s);
1400 }
1401}
1402
1403static void sci_free_dma(struct uart_port *port)
1404{
1405 struct sci_port *s = to_sci_port(port);
1406
Paul Mundtce6738b2011-01-19 15:24:40 +09001407 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001408 return;
1409
1410 if (s->chan_tx)
1411 sci_tx_dma_release(s, false);
1412 if (s->chan_rx)
1413 sci_rx_dma_release(s, false);
1414}
Paul Mundt27bd1072011-01-19 15:37:31 +09001415#else
1416static inline void sci_request_dma(struct uart_port *port)
1417{
1418}
1419
1420static inline void sci_free_dma(struct uart_port *port)
1421{
1422}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001423#endif
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static int sci_startup(struct uart_port *port)
1426{
Magnus Damma5660ad2009-01-21 15:14:38 +00001427 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001428 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001430 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1431
Paul Mundte108b2c2006-09-27 16:32:13 +09001432 if (s->enable)
1433 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Paul Mundt073e84c2011-01-19 17:30:53 +09001435 ret = sci_request_irq(s);
1436 if (unlikely(ret < 0))
1437 return ret;
1438
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001439 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001440
Yoshinori Satod6569012005-10-14 15:59:12 -07001441 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001442 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 return 0;
1445}
1446
1447static void sci_shutdown(struct uart_port *port)
1448{
Magnus Damma5660ad2009-01-21 15:14:38 +00001449 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001451 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001454 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001455
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001456 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 sci_free_irq(s);
1458
Paul Mundte108b2c2006-09-27 16:32:13 +09001459 if (s->disable)
1460 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Paul Mundt26c92f32009-06-24 18:23:52 +09001463static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1464 unsigned long freq)
1465{
1466 switch (algo_id) {
1467 case SCBRR_ALGO_1:
1468 return ((freq + 16 * bps) / (16 * bps) - 1);
1469 case SCBRR_ALGO_2:
1470 return ((freq + 16 * bps) / (32 * bps) - 1);
1471 case SCBRR_ALGO_3:
1472 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1473 case SCBRR_ALGO_4:
1474 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1475 case SCBRR_ALGO_5:
1476 return (((freq * 1000 / 32) / bps) - 1);
1477 }
1478
1479 /* Warn, but use a safe default */
1480 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001481
Paul Mundt26c92f32009-06-24 18:23:52 +09001482 return ((freq + 16 * bps) / (32 * bps) - 1);
1483}
1484
Alan Cox606d0992006-12-08 02:38:45 -08001485static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1486 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Paul Mundt00b9de92009-06-24 17:53:33 +09001488 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001489 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001490 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001491 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Magnus Damm154280f2009-12-22 03:37:28 +00001493 /*
1494 * earlyprintk comes here early on with port->uartclk set to zero.
1495 * the clock framework is not up and running at this point so here
1496 * we assume that 115200 is the maximum baud rate. please note that
1497 * the baud rate is not programmed during earlyprintk - it is assumed
1498 * that the previous boot loader has enabled required clocks and
1499 * setup the baud rate generator hardware for us already.
1500 */
1501 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1502
1503 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1504 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001505 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Alexandre Courbot36003382011-03-03 08:04:42 +00001507 if (s->enable)
1508 s->enable(port);
1509
Paul Mundte108b2c2006-09-27 16:32:13 +09001510 do {
1511 status = sci_in(port, SCxSR);
1512 } while (!(status & SCxSR_TEND(port)));
1513
1514 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1515
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001516 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001517 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001518
1519 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001520
Paul Mundte108b2c2006-09-27 16:32:13 +09001521 if ((termios->c_cflag & CSIZE) == CS7)
1522 smr_val |= 0x40;
1523 if (termios->c_cflag & PARENB)
1524 smr_val |= 0x20;
1525 if (termios->c_cflag & PARODD)
1526 smr_val |= 0x30;
1527 if (termios->c_cflag & CSTOPB)
1528 smr_val |= 0x08;
1529
1530 uart_update_timeout(port, termios->c_cflag, baud);
1531
1532 sci_out(port, SCSMR, smr_val);
1533
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001534 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001535 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001538 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1540 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001541 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 sci_out(port, SCBRR, t);
1545 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1546 }
1547
Paul Mundtd5701642008-12-16 20:07:27 +09001548 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001549 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001550
Paul Mundtce6738b2011-01-19 15:24:40 +09001551 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001553#ifdef CONFIG_SERIAL_SH_SCI_DMA
1554 /*
1555 * Calculate delay for 1.5 DMA buffers: see
1556 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1557 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1558 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1559 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1560 * sizes), but it has been found out experimentally, that this is not
1561 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1562 * as a minimum seem to work perfectly.
1563 */
1564 if (s->chan_rx) {
1565 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1566 port->fifosize / 2;
1567 dev_dbg(port->dev,
1568 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1569 s->rx_timeout * 1000 / HZ, port->timeout);
1570 if (s->rx_timeout < msecs_to_jiffies(20))
1571 s->rx_timeout = msecs_to_jiffies(20);
1572 }
1573#endif
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001576 sci_start_rx(port);
Alexandre Courbot36003382011-03-03 08:04:42 +00001577
1578 if (s->disable)
1579 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582static const char *sci_type(struct uart_port *port)
1583{
1584 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001585 case PORT_IRDA:
1586 return "irda";
1587 case PORT_SCI:
1588 return "sci";
1589 case PORT_SCIF:
1590 return "scif";
1591 case PORT_SCIFA:
1592 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001593 case PORT_SCIFB:
1594 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
Paul Mundtfa439722008-09-04 18:53:58 +09001597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Paul Mundte2651642011-01-20 21:24:03 +09001600static inline unsigned long sci_port_size(struct uart_port *port)
1601{
1602 /*
1603 * Pick an arbitrary size that encapsulates all of the base
1604 * registers by default. This can be optimized later, or derived
1605 * from platform resource data at such a time that ports begin to
1606 * behave more erratically.
1607 */
1608 return 64;
1609}
1610
Paul Mundtf6e94952011-01-21 15:25:36 +09001611static int sci_remap_port(struct uart_port *port)
1612{
1613 unsigned long size = sci_port_size(port);
1614
1615 /*
1616 * Nothing to do if there's already an established membase.
1617 */
1618 if (port->membase)
1619 return 0;
1620
1621 if (port->flags & UPF_IOREMAP) {
1622 port->membase = ioremap_nocache(port->mapbase, size);
1623 if (unlikely(!port->membase)) {
1624 dev_err(port->dev, "can't remap port#%d\n", port->line);
1625 return -ENXIO;
1626 }
1627 } else {
1628 /*
1629 * For the simple (and majority of) cases where we don't
1630 * need to do any remapping, just cast the cookie
1631 * directly.
1632 */
1633 port->membase = (void __iomem *)port->mapbase;
1634 }
1635
1636 return 0;
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639static void sci_release_port(struct uart_port *port)
1640{
Paul Mundte2651642011-01-20 21:24:03 +09001641 if (port->flags & UPF_IOREMAP) {
1642 iounmap(port->membase);
1643 port->membase = NULL;
1644 }
1645
1646 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649static int sci_request_port(struct uart_port *port)
1650{
Paul Mundte2651642011-01-20 21:24:03 +09001651 unsigned long size = sci_port_size(port);
1652 struct resource *res;
Paul Mundtf6e94952011-01-21 15:25:36 +09001653 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Paul Mundt10205202011-01-21 16:00:31 +09001655 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
Paul Mundte2651642011-01-20 21:24:03 +09001656 if (unlikely(res == NULL))
1657 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Paul Mundtf6e94952011-01-21 15:25:36 +09001659 ret = sci_remap_port(port);
1660 if (unlikely(ret != 0)) {
1661 release_resource(res);
1662 return ret;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001663 }
Paul Mundte2651642011-01-20 21:24:03 +09001664
1665 return 0;
1666}
1667
1668static void sci_config_port(struct uart_port *port, int flags)
1669{
1670 if (flags & UART_CONFIG_TYPE) {
1671 struct sci_port *sport = to_sci_port(port);
1672
1673 port->type = sport->cfg->type;
1674 sci_request_port(port);
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1679{
Magnus Damma5660ad2009-01-21 15:14:38 +00001680 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Paul Mundtce6738b2011-01-19 15:24:40 +09001682 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EINVAL;
1684 if (ser->baud_base < 2400)
1685 /* No paper tape reader for Mitch.. */
1686 return -EINVAL;
1687
1688 return 0;
1689}
1690
1691static struct uart_ops sci_uart_ops = {
1692 .tx_empty = sci_tx_empty,
1693 .set_mctrl = sci_set_mctrl,
1694 .get_mctrl = sci_get_mctrl,
1695 .start_tx = sci_start_tx,
1696 .stop_tx = sci_stop_tx,
1697 .stop_rx = sci_stop_rx,
1698 .enable_ms = sci_enable_ms,
1699 .break_ctl = sci_break_ctl,
1700 .startup = sci_startup,
1701 .shutdown = sci_shutdown,
1702 .set_termios = sci_set_termios,
1703 .type = sci_type,
1704 .release_port = sci_release_port,
1705 .request_port = sci_request_port,
1706 .config_port = sci_config_port,
1707 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001708#ifdef CONFIG_CONSOLE_POLL
1709 .poll_get_char = sci_poll_get_char,
1710 .poll_put_char = sci_poll_put_char,
1711#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712};
1713
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001714static int __devinit sci_init_single(struct platform_device *dev,
1715 struct sci_port *sci_port,
1716 unsigned int index,
1717 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001718{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001719 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001720
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001721 port->ops = &sci_uart_ops;
1722 port->iotype = UPIO_MEM;
1723 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001724
1725 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001726 case PORT_SCIFB:
1727 port->fifosize = 256;
1728 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001729 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001730 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001731 break;
1732 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001733 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001734 break;
1735 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001736 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001737 break;
1738 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001739
1740 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001741 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1742 if (IS_ERR(sci_port->iclk)) {
1743 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1744 if (IS_ERR(sci_port->iclk)) {
1745 dev_err(&dev->dev, "can't get iclk\n");
1746 return PTR_ERR(sci_port->iclk);
1747 }
1748 }
1749
1750 /*
1751 * The function clock is optional, ignore it if we can't
1752 * find it.
1753 */
1754 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1755 if (IS_ERR(sci_port->fclk))
1756 sci_port->fclk = NULL;
1757
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001758 sci_port->enable = sci_clk_enable;
1759 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001760 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001761 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001762
Magnus Damm7ed7e072009-01-21 15:14:14 +00001763 sci_port->break_timer.data = (unsigned long)sci_port;
1764 sci_port->break_timer.function = sci_break_timer;
1765 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001766
Paul Mundtce6738b2011-01-19 15:24:40 +09001767 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001768
Paul Mundtce6738b2011-01-19 15:24:40 +09001769 port->mapbase = p->mapbase;
1770 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001771 port->flags = p->flags;
Paul Mundtce6738b2011-01-19 15:24:40 +09001772
1773 /*
1774 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1775 * for the multi-IRQ ports, which is where we are primarily
1776 * concerned with the shutdown path synchronization.
1777 *
1778 * For the muxed case there's nothing more to do.
1779 */
1780 port->irq = p->irqs[SCIx_TXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001781
Paul Mundtce6738b2011-01-19 15:24:40 +09001782 if (p->dma_dev)
1783 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1784 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001785
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001786 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001790static void serial_console_putchar(struct uart_port *port, int ch)
1791{
1792 sci_poll_put_char(port, ch);
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
1796 * Print a string to the serial port trying not to disturb
1797 * any possible real use of the port...
1798 */
1799static void serial_console_write(struct console *co, const char *s,
1800 unsigned count)
1801{
Paul Mundt906b17d2011-01-21 16:19:53 +09001802 struct sci_port *sci_port = &sci_ports[co->index];
1803 struct uart_port *port = &sci_port->port;
Magnus Damm973e5d52009-02-24 15:57:12 +09001804 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001805
Magnus Damm501b8252009-01-21 15:14:30 +00001806 if (sci_port->enable)
1807 sci_port->enable(port);
1808
1809 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001810
1811 /* wait until fifo is empty and last bit has been transmitted */
1812 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1813 while ((sci_in(port, SCxSR) & bits) != bits)
1814 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001815
Magnus Damm345e5a72009-11-05 14:34:57 +00001816 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001817 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001820static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001822 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct uart_port *port;
1824 int baud = 115200;
1825 int bits = 8;
1826 int parity = 'n';
1827 int flow = 'n';
1828 int ret;
1829
Paul Mundte108b2c2006-09-27 16:32:13 +09001830 /*
Paul Mundt906b17d2011-01-21 16:19:53 +09001831 * Refuse to handle any bogus ports.
Paul Mundte108b2c2006-09-27 16:32:13 +09001832 */
Paul Mundt906b17d2011-01-21 16:19:53 +09001833 if (co->index < 0 || co->index >= SCI_NPORTS)
Paul Mundte108b2c2006-09-27 16:32:13 +09001834 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001835
Paul Mundt906b17d2011-01-21 16:19:53 +09001836 sci_port = &sci_ports[co->index];
1837 port = &sci_port->port;
1838
Paul Mundtf6e94952011-01-21 15:25:36 +09001839 ret = sci_remap_port(port);
1840 if (unlikely(ret != 0))
1841 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001842
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001843 if (sci_port->enable)
1844 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (options)
1847 uart_parse_options(options, &baud, &parity, &bits, &flow);
1848
1849 ret = uart_set_options(port, co, baud, parity, bits, flow);
1850#if defined(__H8300H__) || defined(__H8300S__)
1851 /* disable rx interrupt */
1852 if (ret == 0)
1853 sci_stop_rx(port);
1854#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001855 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return ret;
1857}
1858
1859static struct console serial_console = {
1860 .name = "ttySC",
Paul Mundt906b17d2011-01-21 16:19:53 +09001861 .device = uart_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .write = serial_console_write,
1863 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001864 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .index = -1,
Paul Mundt906b17d2011-01-21 16:19:53 +09001866 .data = &sci_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867};
1868
1869static int __init sci_console_init(void)
1870{
1871 register_console(&serial_console);
1872 return 0;
1873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001875
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001876static struct console early_serial_console = {
1877 .name = "early_ttySC",
1878 .write = serial_console_write,
1879 .flags = CON_PRINTBUFFER,
Paul Mundt906b17d2011-01-21 16:19:53 +09001880 .index = -1,
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001881};
Paul Mundtecdf8a42011-01-21 00:05:48 +09001882
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001883static char early_serial_buf[32];
1884
Paul Mundtecdf8a42011-01-21 00:05:48 +09001885static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1886{
1887 struct plat_sci_port *cfg = pdev->dev.platform_data;
1888
1889 if (early_serial_console.data)
1890 return -EEXIST;
1891
1892 early_serial_console.index = pdev->id;
Paul Mundtecdf8a42011-01-21 00:05:48 +09001893
Paul Mundt906b17d2011-01-21 16:19:53 +09001894 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
Paul Mundtecdf8a42011-01-21 00:05:48 +09001895
1896 serial_console_setup(&early_serial_console, early_serial_buf);
1897
1898 if (!strstr(early_serial_buf, "keep"))
1899 early_serial_console.flags |= CON_BOOT;
1900
1901 register_console(&early_serial_console);
1902 return 0;
1903}
1904#else
1905static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1906{
1907 return -EINVAL;
1908}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1910
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001911#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001912#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001914#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915#endif
1916
1917static char banner[] __initdata =
1918 KERN_INFO "SuperH SCI(F) driver initialized\n";
1919
1920static struct uart_driver sci_uart_driver = {
1921 .owner = THIS_MODULE,
1922 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 .dev_name = "ttySC",
1924 .major = SCI_MAJOR,
1925 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001926 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 .cons = SCI_CONSOLE,
1928};
1929
Paul Mundt54507f62009-05-08 23:48:33 +09001930static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001931{
Paul Mundtd535a232011-01-19 17:19:35 +09001932 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001933
Paul Mundtd535a232011-01-19 17:19:35 +09001934 cpufreq_unregister_notifier(&port->freq_transition,
1935 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001936
Paul Mundtd535a232011-01-19 17:19:35 +09001937 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00001938
Paul Mundtd535a232011-01-19 17:19:35 +09001939 clk_put(port->iclk);
1940 clk_put(port->fclk);
1941
Magnus Damme552de22009-01-21 15:13:42 +00001942 return 0;
1943}
1944
Magnus Damm0ee70712009-01-21 15:13:50 +00001945static int __devinit sci_probe_single(struct platform_device *dev,
1946 unsigned int index,
1947 struct plat_sci_port *p,
1948 struct sci_port *sciport)
1949{
Magnus Damm0ee70712009-01-21 15:13:50 +00001950 int ret;
1951
1952 /* Sanity check */
1953 if (unlikely(index >= SCI_NPORTS)) {
1954 dev_notice(&dev->dev, "Attempting to register port "
1955 "%d when only %d are available.\n",
1956 index+1, SCI_NPORTS);
1957 dev_notice(&dev->dev, "Consider bumping "
1958 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1959 return 0;
1960 }
1961
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001962 ret = sci_init_single(dev, sciport, index, p);
1963 if (ret)
1964 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001965
Paul Mundtd535a232011-01-19 17:19:35 +09001966 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00001967}
1968
Paul Mundte108b2c2006-09-27 16:32:13 +09001969static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Paul Mundte108b2c2006-09-27 16:32:13 +09001971 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09001972 struct sci_port *sp = &sci_ports[dev->id];
Paul Mundtecdf8a42011-01-21 00:05:48 +09001973 int ret;
Magnus Damme552de22009-01-21 15:13:42 +00001974
Paul Mundtecdf8a42011-01-21 00:05:48 +09001975 /*
1976 * If we've come here via earlyprintk initialization, head off to
1977 * the special early probe. We don't have sufficient device state
1978 * to make it beyond this yet.
1979 */
1980 if (is_early_platform_device(dev))
1981 return sci_probe_earlyprintk(dev);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001982
Paul Mundtd535a232011-01-19 17:19:35 +09001983 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00001984
Paul Mundt906b17d2011-01-21 16:19:53 +09001985 ret = sci_probe_single(dev, dev->id, p, sp);
Paul Mundtd535a232011-01-19 17:19:35 +09001986 if (ret)
1987 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001988
Paul Mundtd535a232011-01-19 17:19:35 +09001989 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Paul Mundtd535a232011-01-19 17:19:35 +09001991 ret = cpufreq_register_notifier(&sp->freq_transition,
1992 CPUFREQ_TRANSITION_NOTIFIER);
1993 if (unlikely(ret < 0))
1994 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996#ifdef CONFIG_SH_STANDARD_BIOS
1997 sh_bios_gdb_detach();
1998#endif
1999
Paul Mundte108b2c2006-09-27 16:32:13 +09002000 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002001
2002err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002003 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002004 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002005}
2006
Paul Mundt6daa79b2009-06-15 07:07:38 +09002007static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002008{
Paul Mundtd535a232011-01-19 17:19:35 +09002009 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002010
Paul Mundtd535a232011-01-19 17:19:35 +09002011 if (sport)
2012 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002013
2014 return 0;
2015}
2016
Paul Mundt6daa79b2009-06-15 07:07:38 +09002017static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002018{
Paul Mundtd535a232011-01-19 17:19:35 +09002019 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002020
Paul Mundtd535a232011-01-19 17:19:35 +09002021 if (sport)
2022 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002023
2024 return 0;
2025}
2026
Alexey Dobriyan47145212009-12-14 18:00:08 -08002027static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002028 .suspend = sci_suspend,
2029 .resume = sci_resume,
2030};
2031
Paul Mundte108b2c2006-09-27 16:32:13 +09002032static struct platform_driver sci_driver = {
2033 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002034 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002035 .driver = {
2036 .name = "sh-sci",
2037 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002038 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002039 },
2040};
2041
2042static int __init sci_init(void)
2043{
2044 int ret;
2045
2046 printk(banner);
2047
Paul Mundte108b2c2006-09-27 16:32:13 +09002048 ret = uart_register_driver(&sci_uart_driver);
2049 if (likely(ret == 0)) {
2050 ret = platform_driver_register(&sci_driver);
2051 if (unlikely(ret))
2052 uart_unregister_driver(&sci_uart_driver);
2053 }
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return ret;
2056}
2057
2058static void __exit sci_exit(void)
2059{
Paul Mundte108b2c2006-09-27 16:32:13 +09002060 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 uart_unregister_driver(&sci_uart_driver);
2062}
2063
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002064#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2065early_platform_init_buffer("earlyprintk", &sci_driver,
2066 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068module_init(sci_init);
2069module_exit(sci_exit);
2070
Paul Mundte108b2c2006-09-27 16:32:13 +09002071MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002072MODULE_ALIAS("platform:sh-sci");