blob: 83bf1b8d774485c6d1136fbb57493c0b64022509 [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{
557 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
558 add_timer(&port->break_timer);
559}
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/* Ensure that two consecutive samples find the break over. */
562static void sci_break_timer(unsigned long data)
563{
Paul Mundte108b2c2006-09-27 16:32:13 +0900564 struct sci_port *port = (struct sci_port *)data;
565
566 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900568 sci_schedule_break_timer(port);
569 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* break is over. */
571 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900572 sci_schedule_break_timer(port);
573 } else
574 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900577static int sci_handle_errors(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int copied = 0;
580 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700581 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Paul Mundte108b2c2006-09-27 16:32:13 +0900583 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900585 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800586 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900587
588 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Paul Mundte108b2c2006-09-27 16:32:13 +0900591 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (sci_rxd_in(port) == 0) {
593 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900594 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900595
596 if (!sci_port->break_flag) {
597 sci_port->break_flag = 1;
598 sci_schedule_break_timer(sci_port);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900601 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900603
604 dev_dbg(port->dev, "BREAK detected\n");
605
Paul Mundte108b2c2006-09-27 16:32:13 +0900606 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900607 copied++;
608 }
609
Paul Mundte108b2c2006-09-27 16:32:13 +0900610 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900612 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800613 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900614
615 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618
Paul Mundte108b2c2006-09-27 16:32:13 +0900619 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900621 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
622 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900623
624 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Alan Cox33f0f882006-01-09 20:54:13 -0800627 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return copied;
631}
632
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900633static int sci_handle_fifo_overrun(struct uart_port *port)
Paul Mundtd830fa42008-12-16 19:29:38 +0900634{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700635 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900636 int copied = 0;
637
638 if (port->type != PORT_SCIF)
639 return 0;
640
641 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
642 sci_out(port, SCLSR, 0);
643
644 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
645 tty_flip_buffer_push(tty);
646
647 dev_notice(port->dev, "overrun error\n");
648 copied++;
649 }
650
651 return copied;
652}
653
Paul Mundt94c8b6d2011-01-20 23:26:18 +0900654static int sci_handle_breaks(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int copied = 0;
657 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700658 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000659 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900661 if (uart_handle_break(port))
662 return 0;
663
Paul Mundtb7a76e42006-02-01 03:06:06 -0800664 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#if defined(CONFIG_CPU_SH3)
666 /* Debounce break */
667 s->break_flag = 1;
668#endif
669 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900670 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800671 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900672
673 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Alan Cox33f0f882006-01-09 20:54:13 -0800676 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900678
Paul Mundtd830fa42008-12-16 19:29:38 +0900679 copied += sci_handle_fifo_overrun(port);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return copied;
682}
683
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900684static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900686#ifdef CONFIG_SERIAL_SH_SCI_DMA
687 struct uart_port *port = ptr;
688 struct sci_port *s = to_sci_port(port);
689
690 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900691 u16 scr = sci_in(port, SCSCR);
692 u16 ssr = sci_in(port, SCxSR);
693
694 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000695 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000696 disable_irq_nosync(irq);
697 scr |= 0x4000;
698 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900699 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000700 }
701 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900702 /* Clear current interrupt */
703 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000704 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
705 jiffies, s->rx_timeout);
706 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900707
708 return IRQ_HANDLED;
709 }
710#endif
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* I think sci_receive_chars has to be called irrespective
713 * of whether the I_IXOFF is set, otherwise, how is the interrupt
714 * to be disabled?
715 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900716 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return IRQ_HANDLED;
719}
720
David Howells7d12e782006-10-05 14:55:46 +0100721static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900724 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Stuart Menefyfd78a762009-07-29 23:01:24 +0900726 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900728 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return IRQ_HANDLED;
731}
732
David Howells7d12e782006-10-05 14:55:46 +0100733static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct uart_port *port = ptr;
736
737 /* Handle errors */
738 if (port->type == PORT_SCI) {
739 if (sci_handle_errors(port)) {
740 /* discard character in rx buffer */
741 sci_in(port, SCxSR);
742 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
743 }
744 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900745 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100746 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
750
751 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100752 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 return IRQ_HANDLED;
755}
756
David Howells7d12e782006-10-05 14:55:46 +0100757static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct uart_port *port = ptr;
760
761 /* Handle BREAKs */
762 sci_handle_breaks(port);
763 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
764
765 return IRQ_HANDLED;
766}
767
Paul Mundtf43dc232011-01-13 15:06:28 +0900768static inline unsigned long port_rx_irq_mask(struct uart_port *port)
769{
770 /*
771 * Not all ports (such as SCIFA) will support REIE. Rather than
772 * special-casing the port type, we check the port initialization
773 * IRQ enable mask to see whether the IRQ is desired at all. If
774 * it's unset, it's logically inferred that there's no point in
775 * testing for it.
776 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900777 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900778}
779
David Howells7d12e782006-10-05 14:55:46 +0100780static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Magnus Damm44e18e92009-07-03 08:39:34 +0000782 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900783 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900784 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900785 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900787 ssr_status = sci_in(port, SCxSR);
788 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900789 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900792 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900793 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900794 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900795
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900796 /*
797 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
798 * DR flags
799 */
800 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900801 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900802 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000805 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900806 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000809 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900810 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Michael Trimarchia8884e32008-10-31 16:10:23 +0900812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/*
816 * Here we define a transistion notifier so that we can update all of our
817 * ports' baud rate when the peripheral clock changes.
818 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900819static int sci_notifier(struct notifier_block *self,
820 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Magnus Damme552de22009-01-21 15:13:42 +0000822 struct sci_port *sci_port;
823 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Paul Mundtd535a232011-01-19 17:19:35 +0900825 sci_port = container_of(self, struct sci_port, freq_transition);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000828 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900829 struct uart_port *port = &sci_port->port;
Paul Mundt073e84c2011-01-19 17:30:53 +0900830
Paul Mundtd535a232011-01-19 17:19:35 +0900831 spin_lock_irqsave(&port->lock, flags);
832 port->uartclk = clk_get_rate(sci_port->iclk);
833 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return NOTIFY_OK;
837}
Magnus Damm501b8252009-01-21 15:14:30 +0000838
839static void sci_clk_enable(struct uart_port *port)
840{
841 struct sci_port *sci_port = to_sci_port(port);
842
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900843 clk_enable(sci_port->iclk);
844 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
845 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000846}
847
848static void sci_clk_disable(struct uart_port *port)
849{
850 struct sci_port *sci_port = to_sci_port(port);
851
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900852 clk_disable(sci_port->fclk);
853 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000854}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856static int sci_request_irq(struct sci_port *port)
857{
858 int i;
David Howells7d12e782006-10-05 14:55:46 +0100859 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
861 sci_br_interrupt,
862 };
863 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
864 "SCI Transmit Data Empty", "SCI Break" };
865
Paul Mundtce6738b2011-01-19 15:24:40 +0900866 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
867 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900869
Paul Mundtce6738b2011-01-19 15:24:40 +0900870 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900871 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900872 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENODEV;
874 }
875 } else {
876 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900877 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900879
Paul Mundtce6738b2011-01-19 15:24:40 +0900880 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900881 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900882 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -ENODEV;
884 }
885 }
886 }
887
888 return 0;
889}
890
891static void sci_free_irq(struct sci_port *port)
892{
893 int i;
894
Paul Mundtce6738b2011-01-19 15:24:40 +0900895 if (port->cfg->irqs[0] == port->cfg->irqs[1])
896 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +0900897 else {
Paul Mundtce6738b2011-01-19 15:24:40 +0900898 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
899 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 continue;
901
Paul Mundtce6738b2011-01-19 15:24:40 +0900902 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 }
905}
906
907static unsigned int sci_tx_empty(struct uart_port *port)
908{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000909 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900910 unsigned short in_tx_fifo = scif_txfill(port);
911
912 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
916{
917 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
918 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
919 /* If you have signals for DTR and DCD, please implement here. */
920}
921
922static unsigned int sci_get_mctrl(struct uart_port *port)
923{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900924 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 and CTS/RTS */
926
927 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
928}
929
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900930#ifdef CONFIG_SERIAL_SH_SCI_DMA
931static void sci_dma_tx_complete(void *arg)
932{
933 struct sci_port *s = arg;
934 struct uart_port *port = &s->port;
935 struct circ_buf *xmit = &port->state->xmit;
936 unsigned long flags;
937
938 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
939
940 spin_lock_irqsave(&port->lock, flags);
941
Magnus Dammf354a382010-03-19 04:47:01 +0000942 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900943 xmit->tail &= UART_XMIT_SIZE - 1;
944
Magnus Dammf354a382010-03-19 04:47:01 +0000945 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900946
947 async_tx_ack(s->desc_tx);
948 s->cookie_tx = -EINVAL;
949 s->desc_tx = NULL;
950
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900951 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
952 uart_write_wakeup(port);
953
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000954 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900955 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000956 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000957 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900958 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000959 }
960
961 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900962}
963
964/* Locking: called with port lock held */
965static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
966 size_t count)
967{
968 struct uart_port *port = &s->port;
969 int i, active, room;
970
971 room = tty_buffer_request_room(tty, count);
972
973 if (s->active_rx == s->cookie_rx[0]) {
974 active = 0;
975 } else if (s->active_rx == s->cookie_rx[1]) {
976 active = 1;
977 } else {
978 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
979 return 0;
980 }
981
982 if (room < count)
983 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
984 count - room);
985 if (!room)
986 return room;
987
988 for (i = 0; i < room; i++)
989 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
990 TTY_NORMAL);
991
992 port->icount.rx += room;
993
994 return room;
995}
996
997static void sci_dma_rx_complete(void *arg)
998{
999 struct sci_port *s = arg;
1000 struct uart_port *port = &s->port;
1001 struct tty_struct *tty = port->state->port.tty;
1002 unsigned long flags;
1003 int count;
1004
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001005 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001006
1007 spin_lock_irqsave(&port->lock, flags);
1008
1009 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1010
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001011 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001012
1013 spin_unlock_irqrestore(&port->lock, flags);
1014
1015 if (count)
1016 tty_flip_buffer_push(tty);
1017
1018 schedule_work(&s->work_rx);
1019}
1020
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001021static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1022{
1023 struct dma_chan *chan = s->chan_rx;
1024 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001025
1026 s->chan_rx = NULL;
1027 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1028 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001029 if (sg_dma_address(&s->sg_rx[0]))
1030 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1031 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001032 if (enable_pio)
1033 sci_start_rx(port);
1034}
1035
1036static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1037{
1038 struct dma_chan *chan = s->chan_tx;
1039 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001040
1041 s->chan_tx = NULL;
1042 s->cookie_tx = -EINVAL;
1043 dma_release_channel(chan);
1044 if (enable_pio)
1045 sci_start_tx(port);
1046}
1047
1048static void sci_submit_rx(struct sci_port *s)
1049{
1050 struct dma_chan *chan = s->chan_rx;
1051 int i;
1052
1053 for (i = 0; i < 2; i++) {
1054 struct scatterlist *sg = &s->sg_rx[i];
1055 struct dma_async_tx_descriptor *desc;
1056
1057 desc = chan->device->device_prep_slave_sg(chan,
1058 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1059
1060 if (desc) {
1061 s->desc_rx[i] = desc;
1062 desc->callback = sci_dma_rx_complete;
1063 desc->callback_param = s;
1064 s->cookie_rx[i] = desc->tx_submit(desc);
1065 }
1066
1067 if (!desc || s->cookie_rx[i] < 0) {
1068 if (i) {
1069 async_tx_ack(s->desc_rx[0]);
1070 s->cookie_rx[0] = -EINVAL;
1071 }
1072 if (desc) {
1073 async_tx_ack(desc);
1074 s->cookie_rx[i] = -EINVAL;
1075 }
1076 dev_warn(s->port.dev,
1077 "failed to re-start DMA, using PIO\n");
1078 sci_rx_dma_release(s, true);
1079 return;
1080 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001081 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1082 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001083 }
1084
1085 s->active_rx = s->cookie_rx[0];
1086
1087 dma_async_issue_pending(chan);
1088}
1089
1090static void work_fn_rx(struct work_struct *work)
1091{
1092 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1093 struct uart_port *port = &s->port;
1094 struct dma_async_tx_descriptor *desc;
1095 int new;
1096
1097 if (s->active_rx == s->cookie_rx[0]) {
1098 new = 0;
1099 } else if (s->active_rx == s->cookie_rx[1]) {
1100 new = 1;
1101 } else {
1102 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1103 return;
1104 }
1105 desc = s->desc_rx[new];
1106
1107 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1108 DMA_SUCCESS) {
1109 /* Handle incomplete DMA receive */
1110 struct tty_struct *tty = port->state->port.tty;
1111 struct dma_chan *chan = s->chan_rx;
1112 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1113 async_tx);
1114 unsigned long flags;
1115 int count;
1116
Linus Walleij05827632010-05-17 16:30:42 -07001117 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001118 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1119 sh_desc->partial, sh_desc->cookie);
1120
1121 spin_lock_irqsave(&port->lock, flags);
1122 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1123 spin_unlock_irqrestore(&port->lock, flags);
1124
1125 if (count)
1126 tty_flip_buffer_push(tty);
1127
1128 sci_submit_rx(s);
1129
1130 return;
1131 }
1132
1133 s->cookie_rx[new] = desc->tx_submit(desc);
1134 if (s->cookie_rx[new] < 0) {
1135 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1136 sci_rx_dma_release(s, true);
1137 return;
1138 }
1139
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001140 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001141
1142 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1143 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001144}
1145
1146static void work_fn_tx(struct work_struct *work)
1147{
1148 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1149 struct dma_async_tx_descriptor *desc;
1150 struct dma_chan *chan = s->chan_tx;
1151 struct uart_port *port = &s->port;
1152 struct circ_buf *xmit = &port->state->xmit;
1153 struct scatterlist *sg = &s->sg_tx;
1154
1155 /*
1156 * DMA is idle now.
1157 * Port xmit buffer is already mapped, and it is one page... Just adjust
1158 * offsets and lengths. Since it is a circular buffer, we have to
1159 * transmit till the end, and then the rest. Take the port lock to get a
1160 * consistent xmit buffer state.
1161 */
1162 spin_lock_irq(&port->lock);
1163 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001164 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001165 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001166 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001167 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001168 spin_unlock_irq(&port->lock);
1169
Magnus Dammf354a382010-03-19 04:47:01 +00001170 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001171
1172 desc = chan->device->device_prep_slave_sg(chan,
1173 sg, s->sg_len_tx, DMA_TO_DEVICE,
1174 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1175 if (!desc) {
1176 /* switch to PIO */
1177 sci_tx_dma_release(s, true);
1178 return;
1179 }
1180
1181 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1182
1183 spin_lock_irq(&port->lock);
1184 s->desc_tx = desc;
1185 desc->callback = sci_dma_tx_complete;
1186 desc->callback_param = s;
1187 spin_unlock_irq(&port->lock);
1188 s->cookie_tx = desc->tx_submit(desc);
1189 if (s->cookie_tx < 0) {
1190 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1191 /* switch to PIO */
1192 sci_tx_dma_release(s, true);
1193 return;
1194 }
1195
1196 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1197 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1198
1199 dma_async_issue_pending(chan);
1200}
1201#endif
1202
Russell Kingb129a8c2005-08-31 10:12:14 +01001203static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001205 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001206 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001208#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001209 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001210 u16 new, scr = sci_in(port, SCSCR);
1211 if (s->chan_tx)
1212 new = scr | 0x8000;
1213 else
1214 new = scr & ~0x8000;
1215 if (new != scr)
1216 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001217 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001218
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001219 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1220 s->cookie_tx < 0)
1221 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001222#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001223
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001224 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001225 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1226 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001227 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Russell Kingb129a8c2005-08-31 10:12:14 +01001231static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned short ctrl;
1234
1235 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001237
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001238 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001239 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001240
Paul Mundt8e698612009-06-24 19:44:32 +09001241 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001246static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 unsigned short ctrl;
1249
Paul Mundtf43dc232011-01-13 15:06:28 +09001250 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1251
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001252 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001253 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258static void sci_stop_rx(struct uart_port *port)
1259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 unsigned short ctrl;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001263
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001264 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001265 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001266
1267 ctrl &= ~port_rx_irq_mask(port);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static void sci_enable_ms(struct uart_port *port)
1273{
1274 /* Nothing here yet .. */
1275}
1276
1277static void sci_break_ctl(struct uart_port *port, int break_state)
1278{
1279 /* Nothing here yet .. */
1280}
1281
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001282#ifdef CONFIG_SERIAL_SH_SCI_DMA
1283static bool filter(struct dma_chan *chan, void *slave)
1284{
1285 struct sh_dmae_slave *param = slave;
1286
1287 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1288 param->slave_id);
1289
1290 if (param->dma_dev == chan->device->dev) {
1291 chan->private = param;
1292 return true;
1293 } else {
1294 return false;
1295 }
1296}
1297
1298static void rx_timer_fn(unsigned long arg)
1299{
1300 struct sci_port *s = (struct sci_port *)arg;
1301 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001302 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001303
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001304 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001305 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001306 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001307 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001308 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001309 dev_dbg(port->dev, "DMA Rx timed out\n");
1310 schedule_work(&s->work_rx);
1311}
1312
1313static void sci_request_dma(struct uart_port *port)
1314{
1315 struct sci_port *s = to_sci_port(port);
1316 struct sh_dmae_slave *param;
1317 struct dma_chan *chan;
1318 dma_cap_mask_t mask;
1319 int nent;
1320
1321 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001322 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001323
Paul Mundtce6738b2011-01-19 15:24:40 +09001324 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001325 return;
1326
1327 dma_cap_zero(mask);
1328 dma_cap_set(DMA_SLAVE, mask);
1329
1330 param = &s->param_tx;
1331
1332 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001333 param->slave_id = s->cfg->dma_slave_tx;
1334 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001335
1336 s->cookie_tx = -EINVAL;
1337 chan = dma_request_channel(mask, filter, param);
1338 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1339 if (chan) {
1340 s->chan_tx = chan;
1341 sg_init_table(&s->sg_tx, 1);
1342 /* UART circular tx buffer is an aligned page. */
1343 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1344 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1345 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1346 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1347 if (!nent)
1348 sci_tx_dma_release(s, false);
1349 else
1350 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1351 sg_dma_len(&s->sg_tx),
1352 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1353
1354 s->sg_len_tx = nent;
1355
1356 INIT_WORK(&s->work_tx, work_fn_tx);
1357 }
1358
1359 param = &s->param_rx;
1360
1361 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001362 param->slave_id = s->cfg->dma_slave_rx;
1363 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001364
1365 chan = dma_request_channel(mask, filter, param);
1366 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1367 if (chan) {
1368 dma_addr_t dma[2];
1369 void *buf[2];
1370 int i;
1371
1372 s->chan_rx = chan;
1373
1374 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1375 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1376 &dma[0], GFP_KERNEL);
1377
1378 if (!buf[0]) {
1379 dev_warn(port->dev,
1380 "failed to allocate dma buffer, using PIO\n");
1381 sci_rx_dma_release(s, true);
1382 return;
1383 }
1384
1385 buf[1] = buf[0] + s->buf_len_rx;
1386 dma[1] = dma[0] + s->buf_len_rx;
1387
1388 for (i = 0; i < 2; i++) {
1389 struct scatterlist *sg = &s->sg_rx[i];
1390
1391 sg_init_table(sg, 1);
1392 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1393 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001394 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001395 }
1396
1397 INIT_WORK(&s->work_rx, work_fn_rx);
1398 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1399
1400 sci_submit_rx(s);
1401 }
1402}
1403
1404static void sci_free_dma(struct uart_port *port)
1405{
1406 struct sci_port *s = to_sci_port(port);
1407
Paul Mundtce6738b2011-01-19 15:24:40 +09001408 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001409 return;
1410
1411 if (s->chan_tx)
1412 sci_tx_dma_release(s, false);
1413 if (s->chan_rx)
1414 sci_rx_dma_release(s, false);
1415}
Paul Mundt27bd1072011-01-19 15:37:31 +09001416#else
1417static inline void sci_request_dma(struct uart_port *port)
1418{
1419}
1420
1421static inline void sci_free_dma(struct uart_port *port)
1422{
1423}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001424#endif
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static int sci_startup(struct uart_port *port)
1427{
Magnus Damma5660ad2009-01-21 15:14:38 +00001428 struct sci_port *s = to_sci_port(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001429 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001431 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1432
Paul Mundte108b2c2006-09-27 16:32:13 +09001433 if (s->enable)
1434 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Paul Mundt073e84c2011-01-19 17:30:53 +09001436 ret = sci_request_irq(s);
1437 if (unlikely(ret < 0))
1438 return ret;
1439
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001440 sci_request_dma(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001441
Yoshinori Satod6569012005-10-14 15:59:12 -07001442 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001443 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 return 0;
1446}
1447
1448static void sci_shutdown(struct uart_port *port)
1449{
Magnus Damma5660ad2009-01-21 15:14:38 +00001450 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001452 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001455 sci_stop_tx(port);
Paul Mundt073e84c2011-01-19 17:30:53 +09001456
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001457 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 sci_free_irq(s);
1459
Paul Mundte108b2c2006-09-27 16:32:13 +09001460 if (s->disable)
1461 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Paul Mundt26c92f32009-06-24 18:23:52 +09001464static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1465 unsigned long freq)
1466{
1467 switch (algo_id) {
1468 case SCBRR_ALGO_1:
1469 return ((freq + 16 * bps) / (16 * bps) - 1);
1470 case SCBRR_ALGO_2:
1471 return ((freq + 16 * bps) / (32 * bps) - 1);
1472 case SCBRR_ALGO_3:
1473 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1474 case SCBRR_ALGO_4:
1475 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1476 case SCBRR_ALGO_5:
1477 return (((freq * 1000 / 32) / bps) - 1);
1478 }
1479
1480 /* Warn, but use a safe default */
1481 WARN_ON(1);
Paul Mundte8183a62011-01-19 17:51:37 +09001482
Paul Mundt26c92f32009-06-24 18:23:52 +09001483 return ((freq + 16 * bps) / (32 * bps) - 1);
1484}
1485
Alan Cox606d0992006-12-08 02:38:45 -08001486static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1487 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Paul Mundt00b9de92009-06-24 17:53:33 +09001489 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001490 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001491 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001492 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Magnus Damm154280f2009-12-22 03:37:28 +00001494 /*
1495 * earlyprintk comes here early on with port->uartclk set to zero.
1496 * the clock framework is not up and running at this point so here
1497 * we assume that 115200 is the maximum baud rate. please note that
1498 * the baud rate is not programmed during earlyprintk - it is assumed
1499 * that the previous boot loader has enabled required clocks and
1500 * setup the baud rate generator hardware for us already.
1501 */
1502 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1503
1504 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1505 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001506 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Paul Mundte108b2c2006-09-27 16:32:13 +09001508 do {
1509 status = sci_in(port, SCxSR);
1510 } while (!(status & SCxSR_TEND(port)));
1511
1512 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1513
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001514 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001515 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001516
1517 smr_val = sci_in(port, SCSMR) & 3;
Paul Mundte8183a62011-01-19 17:51:37 +09001518
Paul Mundte108b2c2006-09-27 16:32:13 +09001519 if ((termios->c_cflag & CSIZE) == CS7)
1520 smr_val |= 0x40;
1521 if (termios->c_cflag & PARENB)
1522 smr_val |= 0x20;
1523 if (termios->c_cflag & PARODD)
1524 smr_val |= 0x30;
1525 if (termios->c_cflag & CSTOPB)
1526 smr_val |= 0x08;
1527
1528 uart_update_timeout(port, termios->c_cflag, baud);
1529
1530 sci_out(port, SCSMR, smr_val);
1531
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001532 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001533 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001536 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1538 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001539 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sci_out(port, SCBRR, t);
1543 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1544 }
1545
Paul Mundtd5701642008-12-16 20:07:27 +09001546 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001547 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001548
Paul Mundtce6738b2011-01-19 15:24:40 +09001549 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001551#ifdef CONFIG_SERIAL_SH_SCI_DMA
1552 /*
1553 * Calculate delay for 1.5 DMA buffers: see
1554 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1555 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1556 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1557 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1558 * sizes), but it has been found out experimentally, that this is not
1559 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1560 * as a minimum seem to work perfectly.
1561 */
1562 if (s->chan_rx) {
1563 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1564 port->fifosize / 2;
1565 dev_dbg(port->dev,
1566 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1567 s->rx_timeout * 1000 / HZ, port->timeout);
1568 if (s->rx_timeout < msecs_to_jiffies(20))
1569 s->rx_timeout = msecs_to_jiffies(20);
1570 }
1571#endif
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001574 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577static const char *sci_type(struct uart_port *port)
1578{
1579 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001580 case PORT_IRDA:
1581 return "irda";
1582 case PORT_SCI:
1583 return "sci";
1584 case PORT_SCIF:
1585 return "scif";
1586 case PORT_SCIFA:
1587 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001588 case PORT_SCIFB:
1589 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
Paul Mundtfa439722008-09-04 18:53:58 +09001592 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Paul Mundte2651642011-01-20 21:24:03 +09001595static inline unsigned long sci_port_size(struct uart_port *port)
1596{
1597 /*
1598 * Pick an arbitrary size that encapsulates all of the base
1599 * registers by default. This can be optimized later, or derived
1600 * from platform resource data at such a time that ports begin to
1601 * behave more erratically.
1602 */
1603 return 64;
1604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static void sci_release_port(struct uart_port *port)
1607{
Paul Mundte2651642011-01-20 21:24:03 +09001608 if (port->flags & UPF_IOREMAP) {
1609 iounmap(port->membase);
1610 port->membase = NULL;
1611 }
1612
1613 release_mem_region(port->mapbase, sci_port_size(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616static int sci_request_port(struct uart_port *port)
1617{
Paul Mundte2651642011-01-20 21:24:03 +09001618 unsigned long size = sci_port_size(port);
1619 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Paul Mundte2651642011-01-20 21:24:03 +09001621 res = request_mem_region(port->mapbase, size, sci_type(port));
1622 if (unlikely(res == NULL))
1623 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Magnus Damm08f8cb32009-01-21 15:14:22 +00001625 if (port->flags & UPF_IOREMAP) {
Paul Mundte2651642011-01-20 21:24:03 +09001626 port->membase = ioremap_nocache(port->mapbase, size);
1627 if (unlikely(!port->membase)) {
Magnus Damm08f8cb32009-01-21 15:14:22 +00001628 dev_err(port->dev, "can't remap port#%d\n", port->line);
Paul Mundte2651642011-01-20 21:24:03 +09001629 release_resource(res);
1630 return -ENXIO;
1631 }
Magnus Damm08f8cb32009-01-21 15:14:22 +00001632 } else {
1633 /*
1634 * For the simple (and majority of) cases where we don't
1635 * need to do any remapping, just cast the cookie
1636 * directly.
1637 */
1638 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001639 }
Paul Mundte2651642011-01-20 21:24:03 +09001640
1641 return 0;
1642}
1643
1644static void sci_config_port(struct uart_port *port, int flags)
1645{
1646 if (flags & UART_CONFIG_TYPE) {
1647 struct sci_port *sport = to_sci_port(port);
1648
1649 port->type = sport->cfg->type;
1650 sci_request_port(port);
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1655{
Magnus Damma5660ad2009-01-21 15:14:38 +00001656 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Paul Mundtce6738b2011-01-19 15:24:40 +09001658 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EINVAL;
1660 if (ser->baud_base < 2400)
1661 /* No paper tape reader for Mitch.. */
1662 return -EINVAL;
1663
1664 return 0;
1665}
1666
1667static struct uart_ops sci_uart_ops = {
1668 .tx_empty = sci_tx_empty,
1669 .set_mctrl = sci_set_mctrl,
1670 .get_mctrl = sci_get_mctrl,
1671 .start_tx = sci_start_tx,
1672 .stop_tx = sci_stop_tx,
1673 .stop_rx = sci_stop_rx,
1674 .enable_ms = sci_enable_ms,
1675 .break_ctl = sci_break_ctl,
1676 .startup = sci_startup,
1677 .shutdown = sci_shutdown,
1678 .set_termios = sci_set_termios,
1679 .type = sci_type,
1680 .release_port = sci_release_port,
1681 .request_port = sci_request_port,
1682 .config_port = sci_config_port,
1683 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001684#ifdef CONFIG_CONSOLE_POLL
1685 .poll_get_char = sci_poll_get_char,
1686 .poll_put_char = sci_poll_put_char,
1687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688};
1689
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001690static int __devinit sci_init_single(struct platform_device *dev,
1691 struct sci_port *sci_port,
1692 unsigned int index,
1693 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001694{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001695 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001696
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001697 port->ops = &sci_uart_ops;
1698 port->iotype = UPIO_MEM;
1699 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001700
1701 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001702 case PORT_SCIFB:
1703 port->fifosize = 256;
1704 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001705 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001706 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001707 break;
1708 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001709 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001710 break;
1711 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001712 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001713 break;
1714 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001715
1716 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001717 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1718 if (IS_ERR(sci_port->iclk)) {
1719 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1720 if (IS_ERR(sci_port->iclk)) {
1721 dev_err(&dev->dev, "can't get iclk\n");
1722 return PTR_ERR(sci_port->iclk);
1723 }
1724 }
1725
1726 /*
1727 * The function clock is optional, ignore it if we can't
1728 * find it.
1729 */
1730 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1731 if (IS_ERR(sci_port->fclk))
1732 sci_port->fclk = NULL;
1733
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001734 sci_port->enable = sci_clk_enable;
1735 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001736 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001737 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001738
Magnus Damm7ed7e072009-01-21 15:14:14 +00001739 sci_port->break_timer.data = (unsigned long)sci_port;
1740 sci_port->break_timer.function = sci_break_timer;
1741 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001742
Paul Mundtce6738b2011-01-19 15:24:40 +09001743 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001744
Paul Mundtce6738b2011-01-19 15:24:40 +09001745 port->mapbase = p->mapbase;
1746 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001747 port->flags = p->flags;
Paul Mundtce6738b2011-01-19 15:24:40 +09001748
1749 /*
1750 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1751 * for the multi-IRQ ports, which is where we are primarily
1752 * concerned with the shutdown path synchronization.
1753 *
1754 * For the muxed case there's nothing more to do.
1755 */
1756 port->irq = p->irqs[SCIx_TXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001757
Paul Mundtce6738b2011-01-19 15:24:40 +09001758 if (p->dma_dev)
1759 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1760 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001761
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001762 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001766static struct tty_driver *serial_console_device(struct console *co, int *index)
1767{
1768 struct uart_driver *p = &sci_uart_driver;
1769 *index = co->index;
1770 return p->tty_driver;
1771}
1772
1773static void serial_console_putchar(struct uart_port *port, int ch)
1774{
1775 sci_poll_put_char(port, ch);
1776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/*
1779 * Print a string to the serial port trying not to disturb
1780 * any possible real use of the port...
1781 */
1782static void serial_console_write(struct console *co, const char *s,
1783 unsigned count)
1784{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001785 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001786 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001787 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001788
Magnus Damm501b8252009-01-21 15:14:30 +00001789 if (sci_port->enable)
1790 sci_port->enable(port);
1791
1792 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001793
1794 /* wait until fifo is empty and last bit has been transmitted */
1795 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1796 while ((sci_in(port, SCxSR) & bits) != bits)
1797 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001798
Magnus Damm345e5a72009-11-05 14:34:57 +00001799 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001800 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001803static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001805 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct uart_port *port;
1807 int baud = 115200;
1808 int bits = 8;
1809 int parity = 'n';
1810 int flow = 'n';
1811 int ret;
1812
Paul Mundte108b2c2006-09-27 16:32:13 +09001813 /*
1814 * Check whether an invalid uart number has been specified, and
1815 * if so, search for the first available port that does have
1816 * console support.
1817 */
1818 if (co->index >= SCI_NPORTS)
1819 co->index = 0;
1820
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001821 if (co->data) {
1822 port = co->data;
1823 sci_port = to_sci_port(port);
1824 } else {
1825 sci_port = &sci_ports[co->index];
1826 port = &sci_port->port;
1827 co->data = port;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001831 * Also need to check port->type, we don't actually have any
1832 * UPIO_PORT ports, but uart_report_port() handily misreports
1833 * it anyways if we don't have a port available by the time this is
1834 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001836 if (!port->type)
1837 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001838
Magnus Damm08f8cb32009-01-21 15:14:22 +00001839 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001840
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001841 if (sci_port->enable)
1842 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (options)
1845 uart_parse_options(options, &baud, &parity, &bits, &flow);
1846
1847 ret = uart_set_options(port, co, baud, parity, bits, flow);
1848#if defined(__H8300H__) || defined(__H8300S__)
1849 /* disable rx interrupt */
1850 if (ret == 0)
1851 sci_stop_rx(port);
1852#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001853 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return ret;
1855}
1856
1857static struct console serial_console = {
1858 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001859 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .write = serial_console_write,
1861 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001862 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864};
1865
1866static int __init sci_console_init(void)
1867{
1868 register_console(&serial_console);
1869 return 0;
1870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001872
1873static struct sci_port early_serial_port;
1874static struct console early_serial_console = {
1875 .name = "early_ttySC",
1876 .write = serial_console_write,
1877 .flags = CON_PRINTBUFFER,
1878};
1879static char early_serial_buf[32];
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1882
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001883#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001884#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001886#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887#endif
1888
1889static char banner[] __initdata =
1890 KERN_INFO "SuperH SCI(F) driver initialized\n";
1891
1892static struct uart_driver sci_uart_driver = {
1893 .owner = THIS_MODULE,
1894 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 .dev_name = "ttySC",
1896 .major = SCI_MAJOR,
1897 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001898 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .cons = SCI_CONSOLE,
1900};
1901
Paul Mundt54507f62009-05-08 23:48:33 +09001902static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001903{
Paul Mundtd535a232011-01-19 17:19:35 +09001904 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001905
Paul Mundtd535a232011-01-19 17:19:35 +09001906 cpufreq_unregister_notifier(&port->freq_transition,
1907 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001908
Paul Mundtd535a232011-01-19 17:19:35 +09001909 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00001910
Paul Mundtd535a232011-01-19 17:19:35 +09001911 clk_put(port->iclk);
1912 clk_put(port->fclk);
1913
Magnus Damme552de22009-01-21 15:13:42 +00001914 return 0;
1915}
1916
Magnus Damm0ee70712009-01-21 15:13:50 +00001917static int __devinit sci_probe_single(struct platform_device *dev,
1918 unsigned int index,
1919 struct plat_sci_port *p,
1920 struct sci_port *sciport)
1921{
Magnus Damm0ee70712009-01-21 15:13:50 +00001922 int ret;
1923
1924 /* Sanity check */
1925 if (unlikely(index >= SCI_NPORTS)) {
1926 dev_notice(&dev->dev, "Attempting to register port "
1927 "%d when only %d are available.\n",
1928 index+1, SCI_NPORTS);
1929 dev_notice(&dev->dev, "Consider bumping "
1930 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1931 return 0;
1932 }
1933
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001934 ret = sci_init_single(dev, sciport, index, p);
1935 if (ret)
1936 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001937
Paul Mundtd535a232011-01-19 17:19:35 +09001938 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00001939}
1940
Paul Mundte108b2c2006-09-27 16:32:13 +09001941/*
1942 * Register a set of serial devices attached to a platform device. The
1943 * list is terminated with a zero flags entry, which means we expect
1944 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1945 * remapping (such as sh64) should also set UPF_IOREMAP.
1946 */
1947static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Paul Mundte108b2c2006-09-27 16:32:13 +09001949 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09001950 struct sci_port *sp = &sci_ports[dev->id];
1951 int ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001952
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001953#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1954 if (is_early_platform_device(dev)) {
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001955 early_serial_console.index = dev->id;
1956 early_serial_console.data = &early_serial_port.port;
Paul Mundtd535a232011-01-19 17:19:35 +09001957
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001958 sci_init_single(NULL, &early_serial_port, dev->id, p);
Paul Mundtd535a232011-01-19 17:19:35 +09001959
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001960 serial_console_setup(&early_serial_console, early_serial_buf);
Paul Mundtd535a232011-01-19 17:19:35 +09001961
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001962 if (!strstr(early_serial_buf, "keep"))
1963 early_serial_console.flags |= CON_BOOT;
Paul Mundtd535a232011-01-19 17:19:35 +09001964
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001965 register_console(&early_serial_console);
1966 return 0;
1967 }
1968#endif
1969
Paul Mundtd535a232011-01-19 17:19:35 +09001970 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00001971
Paul Mundtd535a232011-01-19 17:19:35 +09001972 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1973 if (ret)
1974 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001975
Paul Mundtd535a232011-01-19 17:19:35 +09001976 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Paul Mundtd535a232011-01-19 17:19:35 +09001978 ret = cpufreq_register_notifier(&sp->freq_transition,
1979 CPUFREQ_TRANSITION_NOTIFIER);
1980 if (unlikely(ret < 0))
1981 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983#ifdef CONFIG_SH_STANDARD_BIOS
1984 sh_bios_gdb_detach();
1985#endif
1986
Paul Mundte108b2c2006-09-27 16:32:13 +09001987 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001988
1989err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001990 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001991 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001992}
1993
Paul Mundt6daa79b2009-06-15 07:07:38 +09001994static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001995{
Paul Mundtd535a232011-01-19 17:19:35 +09001996 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09001997
Paul Mundtd535a232011-01-19 17:19:35 +09001998 if (sport)
1999 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002000
2001 return 0;
2002}
2003
Paul Mundt6daa79b2009-06-15 07:07:38 +09002004static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002005{
Paul Mundtd535a232011-01-19 17:19:35 +09002006 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09002007
Paul Mundtd535a232011-01-19 17:19:35 +09002008 if (sport)
2009 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09002010
2011 return 0;
2012}
2013
Alexey Dobriyan47145212009-12-14 18:00:08 -08002014static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002015 .suspend = sci_suspend,
2016 .resume = sci_resume,
2017};
2018
Paul Mundte108b2c2006-09-27 16:32:13 +09002019static struct platform_driver sci_driver = {
2020 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002021 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002022 .driver = {
2023 .name = "sh-sci",
2024 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002025 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002026 },
2027};
2028
2029static int __init sci_init(void)
2030{
2031 int ret;
2032
2033 printk(banner);
2034
Paul Mundte108b2c2006-09-27 16:32:13 +09002035 ret = uart_register_driver(&sci_uart_driver);
2036 if (likely(ret == 0)) {
2037 ret = platform_driver_register(&sci_driver);
2038 if (unlikely(ret))
2039 uart_unregister_driver(&sci_uart_driver);
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return ret;
2043}
2044
2045static void __exit sci_exit(void)
2046{
Paul Mundte108b2c2006-09-27 16:32:13 +09002047 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 uart_unregister_driver(&sci_uart_driver);
2049}
2050
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002051#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2052early_platform_init_buffer("earlyprintk", &sci_driver,
2053 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055module_init(sci_init);
2056module_exit(sci_exit);
2057
Paul Mundte108b2c2006-09-27 16:32:13 +09002058MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002059MODULE_ALIAS("platform:sh-sci");