blob: 5f90fcd7d107573312bbd44ab7be8839ab61b567 [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 Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 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>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090051#include <linux/dmaengine.h>
52#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090054
55#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090056#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Yoshinori Sato168f3622009-04-28 04:40:15 +000059#ifdef CONFIG_H8300
60#include <asm/gpio.h>
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "sh-sci.h"
64
Paul Mundte108b2c2006-09-27 16:32:13 +090065struct sci_port {
66 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Paul Mundte108b2c2006-09-27 16:32:13 +090068 /* Port type */
69 unsigned int type;
70
71 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090072 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090073
Paul Mundte108b2c2006-09-27 16:32:13 +090074 /* Port enable callback */
75 void (*enable)(struct uart_port *port);
76
77 /* Port disable callback */
78 void (*disable)(struct uart_port *port);
79
80 /* Break timer */
81 struct timer_list break_timer;
82 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090083
Magnus Damm501b8252009-01-21 15:14:30 +000084 /* Interface clock */
85 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090086 /* Function clock */
87 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090088
Magnus Damme552de22009-01-21 15:13:42 +000089 struct list_head node;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090090 struct dma_chan *chan_tx;
91 struct dma_chan *chan_rx;
92#ifdef CONFIG_SERIAL_SH_SCI_DMA
93 struct device *dma_dev;
Magnus Damm4bab9d42010-03-19 04:46:38 +000094 unsigned int slave_tx;
95 unsigned int slave_rx;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +090096 struct dma_async_tx_descriptor *desc_tx;
97 struct dma_async_tx_descriptor *desc_rx[2];
98 dma_cookie_t cookie_tx;
99 dma_cookie_t cookie_rx[2];
100 dma_cookie_t active_rx;
101 struct scatterlist sg_tx;
102 unsigned int sg_len_tx;
103 struct scatterlist sg_rx[2];
104 size_t buf_len_rx;
105 struct sh_dmae_slave param_tx;
106 struct sh_dmae_slave param_rx;
107 struct work_struct work_tx;
108 struct work_struct work_rx;
109 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000110 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900111#endif
Magnus Damme552de22009-01-21 15:13:42 +0000112};
113
114struct sh_sci_priv {
115 spinlock_t lock;
116 struct list_head ports;
Magnus Damme552de22009-01-21 15:13:42 +0000117 struct notifier_block clk_nb;
Paul Mundte108b2c2006-09-27 16:32:13 +0900118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100121static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Paul Mundte108b2c2006-09-27 16:32:13 +0900123#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
124
125static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static struct uart_driver sci_uart_driver;
127
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900128static inline struct sci_port *
129to_sci_port(struct uart_port *uart)
130{
131 return container_of(uart, struct sci_port, port);
132}
133
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900134#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900135
136#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900137static inline void handle_error(struct uart_port *port)
138{
139 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
141}
142
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900143static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned short status;
146 int c;
147
Paul Mundte108b2c2006-09-27 16:32:13 +0900148 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 status = sci_in(port, SCxSR);
150 if (status & SCxSR_ERRORS(port)) {
151 handle_error(port);
152 continue;
153 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500154 break;
155 } while (1);
156
157 if (!(status & SCxSR_RDxF(port)))
158 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900161
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900162 /* Dummy read */
163 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return c;
167}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900170static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned short status;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 do {
175 status = sci_in(port, SCxSR);
176 } while (!(status & SCxSR_TDxE(port)));
177
Paul Mundt272966c2008-11-13 17:46:06 +0900178 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000179 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900181#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Paul Mundt15c73aa2008-10-02 19:47:12 +0900183#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900184static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 int ch = (port->mapbase - SMR0) >> 3;
187
188 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900189 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
190 h8300_sci_pins[ch].rx,
191 H8300_GPIO_INPUT);
192 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
193 h8300_sci_pins[ch].tx,
194 H8300_GPIO_OUTPUT);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* tx mark output*/
197 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
198}
Paul Mundtd5701642008-12-16 20:07:27 +0900199#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
200static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900201{
Paul Mundtd5701642008-12-16 20:07:27 +0900202 if (port->mapbase == 0xA4400000) {
203 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
204 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
205 } else if (port->mapbase == 0xA4410000)
206 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900207}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900208#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900209static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900210{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900211 unsigned short data;
212
213 if (cflag & CRTSCTS) {
214 /* enable RTS/CTS */
215 if (port->mapbase == 0xa4430000) { /* SCIF0 */
216 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900217 data = __raw_readw(PORT_PTCR);
218 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
220 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900221 data = __raw_readw(PORT_PVCR);
222 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900223 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900224 } else {
225 if (port->mapbase == 0xa4430000) { /* SCIF0 */
226 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900227 data = __raw_readw(PORT_PTCR);
228 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900229 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
230 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900231 data = __raw_readw(PORT_PVCR);
232 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900233 }
234 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900235}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800236#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900237/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900238static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800240 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Paul Mundtb7a76e42006-02-01 03:06:06 -0800242 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900243 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800244 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900245 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800246
Paul Mundtd5701642008-12-16 20:07:27 +0900247 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900249 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Clear out SCP7MD1,0, SCP4MD1,0,
251 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900252 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Paul Mundt32b53072009-12-24 14:52:43 +0900254 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900256 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Paul Mundt41504c32006-12-11 20:28:03 +0900259#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900260static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900261{
Magnus Damm346b7462008-04-23 21:25:29 +0900262 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900263
Magnus Damm346b7462008-04-23 21:25:29 +0900264 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900265 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900266 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900267 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900268 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900269
Paul Mundtd5701642008-12-16 20:07:27 +0900270 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900271 }
Paul Mundt41504c32006-12-11 20:28:03 +0900272}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900273#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
274 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900275 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900276 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900277 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900278 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900279static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
280{
281 if (!(cflag & CRTSCTS))
282 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
283}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900284#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900285static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
286{
287 if (!(cflag & CRTSCTS))
288 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
289}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800290#else
Paul Mundtd5701642008-12-16 20:07:27 +0900291static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
292{
293 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Paul Mundte108b2c2006-09-27 16:32:13 +0900295#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Paul Mundt32351a22007-03-12 14:38:59 +0900297#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
298 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900299 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
300 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900301static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900302{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900303 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900304}
305
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900306static int scif_txroom(struct uart_port *port)
307{
308 return SCIF_TXROOM_MAX - scif_txfill(port);
309}
310
311static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900312{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900313 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900314}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900315#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900316static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900317{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900318 if (port->mapbase == 0xffe00000 ||
319 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900320 /* SCIF0/1*/
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900321 return sci_in(port, SCTFDR) & 0xff;
322 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900323 /* SCIF2 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900324 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900325}
326
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900327static int scif_txroom(struct uart_port *port)
328{
329 if (port->mapbase == 0xffe00000 ||
330 port->mapbase == 0xffe08000)
331 /* SCIF0/1*/
332 return SCIF_TXROOM_MAX - scif_txfill(port);
333 else
334 /* SCIF2 */
335 return SCIF2_TXROOM_MAX - scif_txfill(port);
336}
337
338static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900339{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900340 if ((port->mapbase == 0xffe00000) ||
341 (port->mapbase == 0xffe08000)) {
342 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900343 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900344 } else {
345 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900346 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900347 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900348}
Paul Mundte108b2c2006-09-27 16:32:13 +0900349#else
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900350static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900351{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900352 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900353}
354
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900355static int scif_txroom(struct uart_port *port)
356{
357 return SCIF_TXROOM_MAX - scif_txfill(port);
358}
359
360static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900361{
362 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900366static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900367{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900368 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900369}
370
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900371static int sci_txroom(struct uart_port *port)
372{
373 return !sci_txfill(port);
374}
375
376static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900377{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900378 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* ********************************************************************** *
382 * the interrupt related routines *
383 * ********************************************************************** */
384
385static void sci_transmit_chars(struct uart_port *port)
386{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700387 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned short status;
390 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900391 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 status = sci_in(port, SCxSR);
394 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900396 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900398 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
402 }
403
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900404 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900405 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900406 else
407 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 do {
410 unsigned char c;
411
412 if (port->x_char) {
413 c = port->x_char;
414 port->x_char = 0;
415 } else if (!uart_circ_empty(xmit) && !stopped) {
416 c = xmit->buf[xmit->tail];
417 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
418 } else {
419 break;
420 }
421
422 sci_out(port, SCxTDR, c);
423
424 port->icount.tx++;
425 } while (--count > 0);
426
427 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
428
429 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
430 uart_write_wakeup(port);
431 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100432 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ctrl = sci_in(port, SCSCR);
435
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900436 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sci_in(port, SCxSR); /* Dummy read */
438 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 ctrl |= SCI_CTRL_FLAGS_TIE;
442 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444}
445
446/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900447#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
David Howells7d12e782006-10-05 14:55:46 +0100449static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900451 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700452 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int i, count, copied = 0;
454 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800455 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 status = sci_in(port, SCxSR);
458 if (!(status & SCxSR_RDxF(port)))
459 return;
460
461 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900462 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900463 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900464 else
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900465 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800468 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* If for any reason we can't copy more data, we're done! */
471 if (count == 0)
472 break;
473
474 if (port->type == PORT_SCI) {
475 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900476 if (uart_handle_sysrq_char(port, c) ||
477 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900479 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900480 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900482 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 char c = sci_in(port, SCxRDR);
484 status = sci_in(port, SCxSR);
485#if defined(CONFIG_CPU_SH3)
486 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900487 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if ((c == 0) &&
489 (status & SCxSR_FER(port))) {
490 count--; i--;
491 continue;
492 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900495 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900496 sci_port->break_flag = 0;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (STEPFN(c)) {
499 count--; i--;
500 continue;
501 }
502 }
503#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100504 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 count--; i--;
506 continue;
507 }
508
509 /* Store data and status */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900510 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800511 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900512 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900513 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800514 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900515 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800516 } else
517 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900518
Alan Cox33f0f882006-01-09 20:54:13 -0800519 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 }
522
523 sci_in(port, SCxSR); /* dummy read */
524 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 copied += count;
527 port->icount.rx += count;
528 }
529
530 if (copied) {
531 /* Tell the rest of the system the news. New characters! */
532 tty_flip_buffer_push(tty);
533 } else {
534 sci_in(port, SCxSR); /* dummy read */
535 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
536 }
537}
538
539#define SCI_BREAK_JIFFIES (HZ/20)
540/* The sci generates interrupts during the break,
541 * 1 per millisecond or so during the break period, for 9600 baud.
542 * So dont bother disabling interrupts.
543 * But dont want more than 1 break event.
544 * Use a kernel timer to periodically poll the rx line until
545 * the break is finished.
546 */
547static void sci_schedule_break_timer(struct sci_port *port)
548{
549 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
550 add_timer(&port->break_timer);
551}
552/* Ensure that two consecutive samples find the break over. */
553static void sci_break_timer(unsigned long data)
554{
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 struct sci_port *port = (struct sci_port *)data;
556
557 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900559 sci_schedule_break_timer(port);
560 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* break is over. */
562 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900563 sci_schedule_break_timer(port);
564 } else
565 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static inline int sci_handle_errors(struct uart_port *port)
569{
570 int copied = 0;
571 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700572 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Paul Mundte108b2c2006-09-27 16:32:13 +0900574 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800577 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900578
579 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Paul Mundte108b2c2006-09-27 16:32:13 +0900582 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (sci_rxd_in(port) == 0) {
584 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900585 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900586
587 if (!sci_port->break_flag) {
588 sci_port->break_flag = 1;
589 sci_schedule_break_timer(sci_port);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900592 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900594
595 dev_dbg(port->dev, "BREAK detected\n");
596
Paul Mundte108b2c2006-09-27 16:32:13 +0900597 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900598 copied++;
599 }
600
Paul Mundte108b2c2006-09-27 16:32:13 +0900601 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900603 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800604 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900605
606 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 }
609
Paul Mundte108b2c2006-09-27 16:32:13 +0900610 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900612 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
613 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900614
615 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Alan Cox33f0f882006-01-09 20:54:13 -0800618 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return copied;
622}
623
Paul Mundtd830fa42008-12-16 19:29:38 +0900624static inline int sci_handle_fifo_overrun(struct uart_port *port)
625{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700626 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900627 int copied = 0;
628
629 if (port->type != PORT_SCIF)
630 return 0;
631
632 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
633 sci_out(port, SCLSR, 0);
634
635 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
636 tty_flip_buffer_push(tty);
637
638 dev_notice(port->dev, "overrun error\n");
639 copied++;
640 }
641
642 return copied;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static inline int sci_handle_breaks(struct uart_port *port)
646{
647 int copied = 0;
648 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700649 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000650 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900652 if (uart_handle_break(port))
653 return 0;
654
Paul Mundtb7a76e42006-02-01 03:06:06 -0800655 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#if defined(CONFIG_CPU_SH3)
657 /* Debounce break */
658 s->break_flag = 1;
659#endif
660 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900661 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800662 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900663
664 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Alan Cox33f0f882006-01-09 20:54:13 -0800667 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900669
Paul Mundtd830fa42008-12-16 19:29:38 +0900670 copied += sci_handle_fifo_overrun(port);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return copied;
673}
674
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900675static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900677#ifdef CONFIG_SERIAL_SH_SCI_DMA
678 struct uart_port *port = ptr;
679 struct sci_port *s = to_sci_port(port);
680
681 if (s->chan_rx) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900682 u16 scr = sci_in(port, SCSCR);
683 u16 ssr = sci_in(port, SCxSR);
684
685 /* Disable future Rx interrupts */
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000686 if (port->type == PORT_SCIFA) {
687 disable_irq_nosync(irq);
688 scr |= 0x4000;
689 } else {
690 scr &= ~SCI_CTRL_FLAGS_RIE;
691 }
692 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900693 /* Clear current interrupt */
694 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000695 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
696 jiffies, s->rx_timeout);
697 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900698
699 return IRQ_HANDLED;
700 }
701#endif
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* I think sci_receive_chars has to be called irrespective
704 * of whether the I_IXOFF is set, otherwise, how is the interrupt
705 * to be disabled?
706 */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900707 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 return IRQ_HANDLED;
710}
711
David Howells7d12e782006-10-05 14:55:46 +0100712static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900715 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Stuart Menefyfd78a762009-07-29 23:01:24 +0900717 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900719 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return IRQ_HANDLED;
722}
723
David Howells7d12e782006-10-05 14:55:46 +0100724static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct uart_port *port = ptr;
727
728 /* Handle errors */
729 if (port->type == PORT_SCI) {
730 if (sci_handle_errors(port)) {
731 /* discard character in rx buffer */
732 sci_in(port, SCxSR);
733 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
734 }
735 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900736 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100737 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
741
742 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100743 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 return IRQ_HANDLED;
746}
747
David Howells7d12e782006-10-05 14:55:46 +0100748static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct uart_port *port = ptr;
751
752 /* Handle BREAKs */
753 sci_handle_breaks(port);
754 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
755
756 return IRQ_HANDLED;
757}
758
David Howells7d12e782006-10-05 14:55:46 +0100759static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Magnus Damm44e18e92009-07-03 08:39:34 +0000761 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900762 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900763 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900764 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900766 ssr_status = sci_in(port, SCxSR);
767 scr_status = sci_in(port, SCSCR);
Magnus Damm44e18e92009-07-03 08:39:34 +0000768 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Tx Interrupt */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900771 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE) &&
772 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900773 ret = sci_tx_interrupt(irq, ptr);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900774 /*
775 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
776 * DR flags
777 */
778 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
779 (scr_status & SCI_CTRL_FLAGS_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900780 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000782 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900783 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000785 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900786 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Michael Trimarchia8884e32008-10-31 16:10:23 +0900788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * Here we define a transistion notifier so that we can update all of our
793 * ports' baud rate when the peripheral clock changes.
794 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900795static int sci_notifier(struct notifier_block *self,
796 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Magnus Damme552de22009-01-21 15:13:42 +0000798 struct sh_sci_priv *priv = container_of(self,
799 struct sh_sci_priv, clk_nb);
800 struct sci_port *sci_port;
801 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000804 (phase == CPUFREQ_RESUMECHANGE)) {
805 spin_lock_irqsave(&priv->lock, flags);
806 list_for_each_entry(sci_port, &priv->ports, node)
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900807 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
Magnus Damme552de22009-01-21 15:13:42 +0000808 spin_unlock_irqrestore(&priv->lock, flags);
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return NOTIFY_OK;
812}
Magnus Damm501b8252009-01-21 15:14:30 +0000813
814static void sci_clk_enable(struct uart_port *port)
815{
816 struct sci_port *sci_port = to_sci_port(port);
817
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900818 clk_enable(sci_port->iclk);
819 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
820 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000821}
822
823static void sci_clk_disable(struct uart_port *port)
824{
825 struct sci_port *sci_port = to_sci_port(port);
826
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900827 clk_disable(sci_port->fclk);
828 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831static int sci_request_irq(struct sci_port *port)
832{
833 int i;
David Howells7d12e782006-10-05 14:55:46 +0100834 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
836 sci_br_interrupt,
837 };
838 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
839 "SCI Transmit Data Empty", "SCI Break" };
840
841 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900842 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900844
845 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900846 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900847 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENODEV;
849 }
850 } else {
851 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900852 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900854
Paul Mundte108b2c2006-09-27 16:32:13 +0900855 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900856 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900857 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENODEV;
859 }
860 }
861 }
862
863 return 0;
864}
865
866static void sci_free_irq(struct sci_port *port)
867{
868 int i;
869
Paul Mundt762c69e2008-12-16 18:55:26 +0900870 if (port->irqs[0] == port->irqs[1])
871 free_irq(port->irqs[0], port);
872 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
874 if (!port->irqs[i])
875 continue;
876
877 free_irq(port->irqs[i], port);
878 }
879 }
880}
881
882static unsigned int sci_tx_empty(struct uart_port *port)
883{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000884 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900885 unsigned short in_tx_fifo = scif_txfill(port);
886
887 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
891{
892 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
893 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
894 /* If you have signals for DTR and DCD, please implement here. */
895}
896
897static unsigned int sci_get_mctrl(struct uart_port *port)
898{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900899 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 and CTS/RTS */
901
902 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
903}
904
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900905#ifdef CONFIG_SERIAL_SH_SCI_DMA
906static void sci_dma_tx_complete(void *arg)
907{
908 struct sci_port *s = arg;
909 struct uart_port *port = &s->port;
910 struct circ_buf *xmit = &port->state->xmit;
911 unsigned long flags;
912
913 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
914
915 spin_lock_irqsave(&port->lock, flags);
916
Magnus Dammf354a382010-03-19 04:47:01 +0000917 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900918 xmit->tail &= UART_XMIT_SIZE - 1;
919
Magnus Dammf354a382010-03-19 04:47:01 +0000920 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900921
922 async_tx_ack(s->desc_tx);
923 s->cookie_tx = -EINVAL;
924 s->desc_tx = NULL;
925
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900926 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
927 uart_write_wakeup(port);
928
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000929 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900930 schedule_work(&s->work_tx);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000931 } else if (port->type == PORT_SCIFA) {
932 u16 ctrl = sci_in(port, SCSCR);
933 sci_out(port, SCSCR, ctrl & ~SCI_CTRL_FLAGS_TIE);
934 }
935
936 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900937}
938
939/* Locking: called with port lock held */
940static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
941 size_t count)
942{
943 struct uart_port *port = &s->port;
944 int i, active, room;
945
946 room = tty_buffer_request_room(tty, count);
947
948 if (s->active_rx == s->cookie_rx[0]) {
949 active = 0;
950 } else if (s->active_rx == s->cookie_rx[1]) {
951 active = 1;
952 } else {
953 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
954 return 0;
955 }
956
957 if (room < count)
958 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
959 count - room);
960 if (!room)
961 return room;
962
963 for (i = 0; i < room; i++)
964 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
965 TTY_NORMAL);
966
967 port->icount.rx += room;
968
969 return room;
970}
971
972static void sci_dma_rx_complete(void *arg)
973{
974 struct sci_port *s = arg;
975 struct uart_port *port = &s->port;
976 struct tty_struct *tty = port->state->port.tty;
977 unsigned long flags;
978 int count;
979
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000980 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900981
982 spin_lock_irqsave(&port->lock, flags);
983
984 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
985
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000986 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +0900987
988 spin_unlock_irqrestore(&port->lock, flags);
989
990 if (count)
991 tty_flip_buffer_push(tty);
992
993 schedule_work(&s->work_rx);
994}
995
996static void sci_start_rx(struct uart_port *port);
997static void sci_start_tx(struct uart_port *port);
998
999static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1000{
1001 struct dma_chan *chan = s->chan_rx;
1002 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001003
1004 s->chan_rx = NULL;
1005 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1006 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001007 if (sg_dma_address(&s->sg_rx[0]))
1008 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1009 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001010 if (enable_pio)
1011 sci_start_rx(port);
1012}
1013
1014static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1015{
1016 struct dma_chan *chan = s->chan_tx;
1017 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001018
1019 s->chan_tx = NULL;
1020 s->cookie_tx = -EINVAL;
1021 dma_release_channel(chan);
1022 if (enable_pio)
1023 sci_start_tx(port);
1024}
1025
1026static void sci_submit_rx(struct sci_port *s)
1027{
1028 struct dma_chan *chan = s->chan_rx;
1029 int i;
1030
1031 for (i = 0; i < 2; i++) {
1032 struct scatterlist *sg = &s->sg_rx[i];
1033 struct dma_async_tx_descriptor *desc;
1034
1035 desc = chan->device->device_prep_slave_sg(chan,
1036 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1037
1038 if (desc) {
1039 s->desc_rx[i] = desc;
1040 desc->callback = sci_dma_rx_complete;
1041 desc->callback_param = s;
1042 s->cookie_rx[i] = desc->tx_submit(desc);
1043 }
1044
1045 if (!desc || s->cookie_rx[i] < 0) {
1046 if (i) {
1047 async_tx_ack(s->desc_rx[0]);
1048 s->cookie_rx[0] = -EINVAL;
1049 }
1050 if (desc) {
1051 async_tx_ack(desc);
1052 s->cookie_rx[i] = -EINVAL;
1053 }
1054 dev_warn(s->port.dev,
1055 "failed to re-start DMA, using PIO\n");
1056 sci_rx_dma_release(s, true);
1057 return;
1058 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001059 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1060 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001061 }
1062
1063 s->active_rx = s->cookie_rx[0];
1064
1065 dma_async_issue_pending(chan);
1066}
1067
1068static void work_fn_rx(struct work_struct *work)
1069{
1070 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1071 struct uart_port *port = &s->port;
1072 struct dma_async_tx_descriptor *desc;
1073 int new;
1074
1075 if (s->active_rx == s->cookie_rx[0]) {
1076 new = 0;
1077 } else if (s->active_rx == s->cookie_rx[1]) {
1078 new = 1;
1079 } else {
1080 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1081 return;
1082 }
1083 desc = s->desc_rx[new];
1084
1085 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1086 DMA_SUCCESS) {
1087 /* Handle incomplete DMA receive */
1088 struct tty_struct *tty = port->state->port.tty;
1089 struct dma_chan *chan = s->chan_rx;
1090 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1091 async_tx);
1092 unsigned long flags;
1093 int count;
1094
Linus Walleij05827632010-05-17 16:30:42 -07001095 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001096 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1097 sh_desc->partial, sh_desc->cookie);
1098
1099 spin_lock_irqsave(&port->lock, flags);
1100 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1101 spin_unlock_irqrestore(&port->lock, flags);
1102
1103 if (count)
1104 tty_flip_buffer_push(tty);
1105
1106 sci_submit_rx(s);
1107
1108 return;
1109 }
1110
1111 s->cookie_rx[new] = desc->tx_submit(desc);
1112 if (s->cookie_rx[new] < 0) {
1113 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1114 sci_rx_dma_release(s, true);
1115 return;
1116 }
1117
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001118 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001119
1120 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1121 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001122}
1123
1124static void work_fn_tx(struct work_struct *work)
1125{
1126 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1127 struct dma_async_tx_descriptor *desc;
1128 struct dma_chan *chan = s->chan_tx;
1129 struct uart_port *port = &s->port;
1130 struct circ_buf *xmit = &port->state->xmit;
1131 struct scatterlist *sg = &s->sg_tx;
1132
1133 /*
1134 * DMA is idle now.
1135 * Port xmit buffer is already mapped, and it is one page... Just adjust
1136 * offsets and lengths. Since it is a circular buffer, we have to
1137 * transmit till the end, and then the rest. Take the port lock to get a
1138 * consistent xmit buffer state.
1139 */
1140 spin_lock_irq(&port->lock);
1141 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001142 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001143 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001144 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001145 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001146 spin_unlock_irq(&port->lock);
1147
Magnus Dammf354a382010-03-19 04:47:01 +00001148 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001149
1150 desc = chan->device->device_prep_slave_sg(chan,
1151 sg, s->sg_len_tx, DMA_TO_DEVICE,
1152 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1153 if (!desc) {
1154 /* switch to PIO */
1155 sci_tx_dma_release(s, true);
1156 return;
1157 }
1158
1159 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1160
1161 spin_lock_irq(&port->lock);
1162 s->desc_tx = desc;
1163 desc->callback = sci_dma_tx_complete;
1164 desc->callback_param = s;
1165 spin_unlock_irq(&port->lock);
1166 s->cookie_tx = desc->tx_submit(desc);
1167 if (s->cookie_tx < 0) {
1168 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1169 /* switch to PIO */
1170 sci_tx_dma_release(s, true);
1171 return;
1172 }
1173
1174 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1175 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1176
1177 dma_async_issue_pending(chan);
1178}
1179#endif
1180
Russell Kingb129a8c2005-08-31 10:12:14 +01001181static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001183 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001184 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001186#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001187 if (port->type == PORT_SCIFA) {
1188 u16 new, scr = sci_in(port, SCSCR);
1189 if (s->chan_tx)
1190 new = scr | 0x8000;
1191 else
1192 new = scr & ~0x8000;
1193 if (new != scr)
1194 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001195 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001196 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1197 s->cookie_tx < 0)
1198 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001199#endif
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001200 if (!s->chan_tx || port->type == PORT_SCIFA) {
1201 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1202 ctrl = sci_in(port, SCSCR);
1203 sci_out(port, SCSCR, ctrl | SCI_CTRL_FLAGS_TIE);
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Russell Kingb129a8c2005-08-31 10:12:14 +01001207static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unsigned short ctrl;
1210
1211 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001213 if (port->type == PORT_SCIFA)
1214 ctrl &= ~0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1216 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001219static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001221 unsigned short ctrl = SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001224 ctrl |= sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001225 if (port->type == PORT_SCIFA)
1226 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static void sci_stop_rx(struct uart_port *port)
1231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned short ctrl;
1233
1234 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001236 if (port->type == PORT_SCIFA)
1237 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
1239 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242static void sci_enable_ms(struct uart_port *port)
1243{
1244 /* Nothing here yet .. */
1245}
1246
1247static void sci_break_ctl(struct uart_port *port, int break_state)
1248{
1249 /* Nothing here yet .. */
1250}
1251
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001252#ifdef CONFIG_SERIAL_SH_SCI_DMA
1253static bool filter(struct dma_chan *chan, void *slave)
1254{
1255 struct sh_dmae_slave *param = slave;
1256
1257 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1258 param->slave_id);
1259
1260 if (param->dma_dev == chan->device->dev) {
1261 chan->private = param;
1262 return true;
1263 } else {
1264 return false;
1265 }
1266}
1267
1268static void rx_timer_fn(unsigned long arg)
1269{
1270 struct sci_port *s = (struct sci_port *)arg;
1271 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001272 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001273
1274 if (port->type == PORT_SCIFA) {
1275 scr &= ~0x4000;
1276 enable_irq(s->irqs[1]);
1277 }
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001278 sci_out(port, SCSCR, scr | SCI_CTRL_FLAGS_RIE);
1279 dev_dbg(port->dev, "DMA Rx timed out\n");
1280 schedule_work(&s->work_rx);
1281}
1282
1283static void sci_request_dma(struct uart_port *port)
1284{
1285 struct sci_port *s = to_sci_port(port);
1286 struct sh_dmae_slave *param;
1287 struct dma_chan *chan;
1288 dma_cap_mask_t mask;
1289 int nent;
1290
1291 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1292 port->line, s->dma_dev);
1293
1294 if (!s->dma_dev)
1295 return;
1296
1297 dma_cap_zero(mask);
1298 dma_cap_set(DMA_SLAVE, mask);
1299
1300 param = &s->param_tx;
1301
1302 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1303 param->slave_id = s->slave_tx;
1304 param->dma_dev = s->dma_dev;
1305
1306 s->cookie_tx = -EINVAL;
1307 chan = dma_request_channel(mask, filter, param);
1308 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1309 if (chan) {
1310 s->chan_tx = chan;
1311 sg_init_table(&s->sg_tx, 1);
1312 /* UART circular tx buffer is an aligned page. */
1313 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1314 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1315 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1316 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1317 if (!nent)
1318 sci_tx_dma_release(s, false);
1319 else
1320 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1321 sg_dma_len(&s->sg_tx),
1322 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1323
1324 s->sg_len_tx = nent;
1325
1326 INIT_WORK(&s->work_tx, work_fn_tx);
1327 }
1328
1329 param = &s->param_rx;
1330
1331 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1332 param->slave_id = s->slave_rx;
1333 param->dma_dev = s->dma_dev;
1334
1335 chan = dma_request_channel(mask, filter, param);
1336 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1337 if (chan) {
1338 dma_addr_t dma[2];
1339 void *buf[2];
1340 int i;
1341
1342 s->chan_rx = chan;
1343
1344 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1345 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1346 &dma[0], GFP_KERNEL);
1347
1348 if (!buf[0]) {
1349 dev_warn(port->dev,
1350 "failed to allocate dma buffer, using PIO\n");
1351 sci_rx_dma_release(s, true);
1352 return;
1353 }
1354
1355 buf[1] = buf[0] + s->buf_len_rx;
1356 dma[1] = dma[0] + s->buf_len_rx;
1357
1358 for (i = 0; i < 2; i++) {
1359 struct scatterlist *sg = &s->sg_rx[i];
1360
1361 sg_init_table(sg, 1);
1362 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1363 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001364 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001365 }
1366
1367 INIT_WORK(&s->work_rx, work_fn_rx);
1368 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1369
1370 sci_submit_rx(s);
1371 }
1372}
1373
1374static void sci_free_dma(struct uart_port *port)
1375{
1376 struct sci_port *s = to_sci_port(port);
1377
1378 if (!s->dma_dev)
1379 return;
1380
1381 if (s->chan_tx)
1382 sci_tx_dma_release(s, false);
1383 if (s->chan_rx)
1384 sci_rx_dma_release(s, false);
1385}
1386#endif
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388static int sci_startup(struct uart_port *port)
1389{
Magnus Damma5660ad2009-01-21 15:14:38 +00001390 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001392 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1393
Paul Mundte108b2c2006-09-27 16:32:13 +09001394 if (s->enable)
1395 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 sci_request_irq(s);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001398#ifdef CONFIG_SERIAL_SH_SCI_DMA
1399 sci_request_dma(port);
1400#endif
Yoshinori Satod6569012005-10-14 15:59:12 -07001401 sci_start_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001402 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 0;
1405}
1406
1407static void sci_shutdown(struct uart_port *port)
1408{
Magnus Damma5660ad2009-01-21 15:14:38 +00001409 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001411 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001414 sci_stop_tx(port);
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001415#ifdef CONFIG_SERIAL_SH_SCI_DMA
1416 sci_free_dma(port);
1417#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 sci_free_irq(s);
1419
Paul Mundte108b2c2006-09-27 16:32:13 +09001420 if (s->disable)
1421 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Alan Cox606d0992006-12-08 02:38:45 -08001424static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1425 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001427#ifdef CONFIG_SERIAL_SH_SCI_DMA
1428 struct sci_port *s = to_sci_port(port);
1429#endif
Magnus Damm154280f2009-12-22 03:37:28 +00001430 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001431 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001432 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Magnus Damm154280f2009-12-22 03:37:28 +00001434 /*
1435 * earlyprintk comes here early on with port->uartclk set to zero.
1436 * the clock framework is not up and running at this point so here
1437 * we assume that 115200 is the maximum baud rate. please note that
1438 * the baud rate is not programmed during earlyprintk - it is assumed
1439 * that the previous boot loader has enabled required clocks and
1440 * setup the baud rate generator hardware for us already.
1441 */
1442 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1443
1444 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1445 if (likely(baud && port->uartclk))
Paul Mundta2159b52008-10-02 19:09:13 +09001446 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Paul Mundte108b2c2006-09-27 16:32:13 +09001448 do {
1449 status = sci_in(port, SCxSR);
1450 } while (!(status & SCxSR_TEND(port)));
1451
1452 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1453
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001454 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001455 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001456
1457 smr_val = sci_in(port, SCSMR) & 3;
1458 if ((termios->c_cflag & CSIZE) == CS7)
1459 smr_val |= 0x40;
1460 if (termios->c_cflag & PARENB)
1461 smr_val |= 0x20;
1462 if (termios->c_cflag & PARODD)
1463 smr_val |= 0x30;
1464 if (termios->c_cflag & CSTOPB)
1465 smr_val |= 0x08;
1466
1467 uart_update_timeout(port, termios->c_cflag, baud);
1468
1469 sci_out(port, SCSMR, smr_val);
1470
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001471 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1472 SCSCR_INIT(port));
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001475 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1477 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001478 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 sci_out(port, SCBRR, t);
1482 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1483 }
1484
Paul Mundtd5701642008-12-16 20:07:27 +09001485 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001486 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 sci_out(port, SCSCR, SCSCR_INIT(port));
1489
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001490#ifdef CONFIG_SERIAL_SH_SCI_DMA
1491 /*
1492 * Calculate delay for 1.5 DMA buffers: see
1493 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1494 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1495 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1496 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1497 * sizes), but it has been found out experimentally, that this is not
1498 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1499 * as a minimum seem to work perfectly.
1500 */
1501 if (s->chan_rx) {
1502 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1503 port->fifosize / 2;
1504 dev_dbg(port->dev,
1505 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1506 s->rx_timeout * 1000 / HZ, port->timeout);
1507 if (s->rx_timeout < msecs_to_jiffies(20))
1508 s->rx_timeout = msecs_to_jiffies(20);
1509 }
1510#endif
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001513 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516static const char *sci_type(struct uart_port *port)
1517{
1518 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001519 case PORT_IRDA:
1520 return "irda";
1521 case PORT_SCI:
1522 return "sci";
1523 case PORT_SCIF:
1524 return "scif";
1525 case PORT_SCIFA:
1526 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Paul Mundtfa439722008-09-04 18:53:58 +09001529 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532static void sci_release_port(struct uart_port *port)
1533{
1534 /* Nothing here yet .. */
1535}
1536
1537static int sci_request_port(struct uart_port *port)
1538{
1539 /* Nothing here yet .. */
1540 return 0;
1541}
1542
1543static void sci_config_port(struct uart_port *port, int flags)
1544{
Magnus Damma5660ad2009-01-21 15:14:38 +00001545 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 port->type = s->type;
1548
Magnus Damm08f8cb32009-01-21 15:14:22 +00001549 if (port->membase)
1550 return;
1551
1552 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001553 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001554
1555 if (IS_ERR(port->membase))
1556 dev_err(port->dev, "can't remap port#%d\n", port->line);
1557 } else {
1558 /*
1559 * For the simple (and majority of) cases where we don't
1560 * need to do any remapping, just cast the cookie
1561 * directly.
1562 */
1563 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1568{
Magnus Damma5660ad2009-01-21 15:14:38 +00001569 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Yinghai Lua62c4132008-08-19 20:49:55 -07001571 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return -EINVAL;
1573 if (ser->baud_base < 2400)
1574 /* No paper tape reader for Mitch.. */
1575 return -EINVAL;
1576
1577 return 0;
1578}
1579
1580static struct uart_ops sci_uart_ops = {
1581 .tx_empty = sci_tx_empty,
1582 .set_mctrl = sci_set_mctrl,
1583 .get_mctrl = sci_get_mctrl,
1584 .start_tx = sci_start_tx,
1585 .stop_tx = sci_stop_tx,
1586 .stop_rx = sci_stop_rx,
1587 .enable_ms = sci_enable_ms,
1588 .break_ctl = sci_break_ctl,
1589 .startup = sci_startup,
1590 .shutdown = sci_shutdown,
1591 .set_termios = sci_set_termios,
1592 .type = sci_type,
1593 .release_port = sci_release_port,
1594 .request_port = sci_request_port,
1595 .config_port = sci_config_port,
1596 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001597#ifdef CONFIG_CONSOLE_POLL
1598 .poll_get_char = sci_poll_get_char,
1599 .poll_put_char = sci_poll_put_char,
1600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601};
1602
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001603static int __devinit sci_init_single(struct platform_device *dev,
1604 struct sci_port *sci_port,
1605 unsigned int index,
1606 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001607{
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001608 struct uart_port *port = &sci_port->port;
1609
1610 port->ops = &sci_uart_ops;
1611 port->iotype = UPIO_MEM;
1612 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001613
1614 switch (p->type) {
1615 case PORT_SCIFA:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001616 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001617 break;
1618 case PORT_SCIF:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001619 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001620 break;
1621 default:
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001622 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001623 break;
1624 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001625
1626 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001627 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1628 if (IS_ERR(sci_port->iclk)) {
1629 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1630 if (IS_ERR(sci_port->iclk)) {
1631 dev_err(&dev->dev, "can't get iclk\n");
1632 return PTR_ERR(sci_port->iclk);
1633 }
1634 }
1635
1636 /*
1637 * The function clock is optional, ignore it if we can't
1638 * find it.
1639 */
1640 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1641 if (IS_ERR(sci_port->fclk))
1642 sci_port->fclk = NULL;
1643
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001644 sci_port->enable = sci_clk_enable;
1645 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001646 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001647 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001648
Magnus Damm7ed7e072009-01-21 15:14:14 +00001649 sci_port->break_timer.data = (unsigned long)sci_port;
1650 sci_port->break_timer.function = sci_break_timer;
1651 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001652
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001653 port->mapbase = p->mapbase;
1654 port->membase = p->membase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001655
Guennadi Liakhovetski73a19e4c2010-03-02 11:39:15 +09001656 port->irq = p->irqs[SCIx_TXI_IRQ];
1657 port->flags = p->flags;
1658 sci_port->type = port->type = p->type;
1659
1660#ifdef CONFIG_SERIAL_SH_SCI_DMA
1661 sci_port->dma_dev = p->dma_dev;
1662 sci_port->slave_tx = p->dma_slave_tx;
1663 sci_port->slave_rx = p->dma_slave_rx;
1664
1665 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1666 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1667#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001668
1669 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001670 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001674static struct tty_driver *serial_console_device(struct console *co, int *index)
1675{
1676 struct uart_driver *p = &sci_uart_driver;
1677 *index = co->index;
1678 return p->tty_driver;
1679}
1680
1681static void serial_console_putchar(struct uart_port *port, int ch)
1682{
1683 sci_poll_put_char(port, ch);
1684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686/*
1687 * Print a string to the serial port trying not to disturb
1688 * any possible real use of the port...
1689 */
1690static void serial_console_write(struct console *co, const char *s,
1691 unsigned count)
1692{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001693 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001694 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001695 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001696
Magnus Damm501b8252009-01-21 15:14:30 +00001697 if (sci_port->enable)
1698 sci_port->enable(port);
1699
1700 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001701
1702 /* wait until fifo is empty and last bit has been transmitted */
1703 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1704 while ((sci_in(port, SCxSR) & bits) != bits)
1705 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001706
Magnus Damm345e5a72009-11-05 14:34:57 +00001707 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001708 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001711static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001713 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct uart_port *port;
1715 int baud = 115200;
1716 int bits = 8;
1717 int parity = 'n';
1718 int flow = 'n';
1719 int ret;
1720
Paul Mundte108b2c2006-09-27 16:32:13 +09001721 /*
1722 * Check whether an invalid uart number has been specified, and
1723 * if so, search for the first available port that does have
1724 * console support.
1725 */
1726 if (co->index >= SCI_NPORTS)
1727 co->index = 0;
1728
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001729 if (co->data) {
1730 port = co->data;
1731 sci_port = to_sci_port(port);
1732 } else {
1733 sci_port = &sci_ports[co->index];
1734 port = &sci_port->port;
1735 co->data = port;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001739 * Also need to check port->type, we don't actually have any
1740 * UPIO_PORT ports, but uart_report_port() handily misreports
1741 * it anyways if we don't have a port available by the time this is
1742 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001744 if (!port->type)
1745 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001746
Magnus Damm08f8cb32009-01-21 15:14:22 +00001747 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001748
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001749 if (sci_port->enable)
1750 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (options)
1753 uart_parse_options(options, &baud, &parity, &bits, &flow);
1754
1755 ret = uart_set_options(port, co, baud, parity, bits, flow);
1756#if defined(__H8300H__) || defined(__H8300S__)
1757 /* disable rx interrupt */
1758 if (ret == 0)
1759 sci_stop_rx(port);
1760#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001761 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return ret;
1763}
1764
1765static struct console serial_console = {
1766 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001767 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .write = serial_console_write,
1769 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001770 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772};
1773
1774static int __init sci_console_init(void)
1775{
1776 register_console(&serial_console);
1777 return 0;
1778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001780
1781static struct sci_port early_serial_port;
1782static struct console early_serial_console = {
1783 .name = "early_ttySC",
1784 .write = serial_console_write,
1785 .flags = CON_PRINTBUFFER,
1786};
1787static char early_serial_buf[32];
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1790
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001791#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001792#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001794#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#endif
1796
1797static char banner[] __initdata =
1798 KERN_INFO "SuperH SCI(F) driver initialized\n";
1799
1800static struct uart_driver sci_uart_driver = {
1801 .owner = THIS_MODULE,
1802 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 .dev_name = "ttySC",
1804 .major = SCI_MAJOR,
1805 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001806 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 .cons = SCI_CONSOLE,
1808};
1809
Magnus Damme552de22009-01-21 15:13:42 +00001810
Paul Mundt54507f62009-05-08 23:48:33 +09001811static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001812{
1813 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1814 struct sci_port *p;
1815 unsigned long flags;
1816
Magnus Damme552de22009-01-21 15:13:42 +00001817 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001818
1819 spin_lock_irqsave(&priv->lock, flags);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001820 list_for_each_entry(p, &priv->ports, node) {
Magnus Damme552de22009-01-21 15:13:42 +00001821 uart_remove_one_port(&sci_uart_driver, &p->port);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001822 clk_put(p->iclk);
1823 clk_put(p->fclk);
1824 }
Magnus Damme552de22009-01-21 15:13:42 +00001825 spin_unlock_irqrestore(&priv->lock, flags);
1826
1827 kfree(priv);
1828 return 0;
1829}
1830
Magnus Damm0ee70712009-01-21 15:13:50 +00001831static int __devinit sci_probe_single(struct platform_device *dev,
1832 unsigned int index,
1833 struct plat_sci_port *p,
1834 struct sci_port *sciport)
1835{
1836 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1837 unsigned long flags;
1838 int ret;
1839
1840 /* Sanity check */
1841 if (unlikely(index >= SCI_NPORTS)) {
1842 dev_notice(&dev->dev, "Attempting to register port "
1843 "%d when only %d are available.\n",
1844 index+1, SCI_NPORTS);
1845 dev_notice(&dev->dev, "Consider bumping "
1846 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1847 return 0;
1848 }
1849
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001850 ret = sci_init_single(dev, sciport, index, p);
1851 if (ret)
1852 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001853
1854 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001855 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001856 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001857
1858 INIT_LIST_HEAD(&sciport->node);
1859
1860 spin_lock_irqsave(&priv->lock, flags);
1861 list_add(&sciport->node, &priv->ports);
1862 spin_unlock_irqrestore(&priv->lock, flags);
1863
1864 return 0;
1865}
1866
Paul Mundte108b2c2006-09-27 16:32:13 +09001867/*
1868 * Register a set of serial devices attached to a platform device. The
1869 * list is terminated with a zero flags entry, which means we expect
1870 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1871 * remapping (such as sh64) should also set UPF_IOREMAP.
1872 */
1873static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Paul Mundte108b2c2006-09-27 16:32:13 +09001875 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001876 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001877 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001878
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001879#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1880 if (is_early_platform_device(dev)) {
1881 if (dev->id == -1)
1882 return -ENOTSUPP;
1883 early_serial_console.index = dev->id;
1884 early_serial_console.data = &early_serial_port.port;
1885 sci_init_single(NULL, &early_serial_port, dev->id, p);
1886 serial_console_setup(&early_serial_console, early_serial_buf);
1887 if (!strstr(early_serial_buf, "keep"))
1888 early_serial_console.flags |= CON_BOOT;
1889 register_console(&early_serial_console);
1890 return 0;
1891 }
1892#endif
1893
Magnus Damme552de22009-01-21 15:13:42 +00001894 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1895 if (!priv)
1896 return -ENOMEM;
1897
1898 INIT_LIST_HEAD(&priv->ports);
1899 spin_lock_init(&priv->lock);
1900 platform_set_drvdata(dev, priv);
1901
Magnus Damme552de22009-01-21 15:13:42 +00001902 priv->clk_nb.notifier_call = sci_notifier;
1903 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Magnus Damm0ee70712009-01-21 15:13:50 +00001905 if (dev->id != -1) {
1906 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1907 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001908 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001909 } else {
1910 for (i = 0; p && p->flags != 0; p++, i++) {
1911 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1912 if (ret)
1913 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001914 }
Magnus Damme552de22009-01-21 15:13:42 +00001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917#ifdef CONFIG_SH_STANDARD_BIOS
1918 sh_bios_gdb_detach();
1919#endif
1920
Paul Mundte108b2c2006-09-27 16:32:13 +09001921 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001922
1923err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001924 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001925 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001926}
1927
Paul Mundt6daa79b2009-06-15 07:07:38 +09001928static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001929{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001930 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001931 struct sci_port *p;
1932 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001933
Magnus Damme552de22009-01-21 15:13:42 +00001934 spin_lock_irqsave(&priv->lock, flags);
1935 list_for_each_entry(p, &priv->ports, node)
1936 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001937 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001938
1939 return 0;
1940}
1941
Paul Mundt6daa79b2009-06-15 07:07:38 +09001942static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001943{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001944 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001945 struct sci_port *p;
1946 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001947
Magnus Damme552de22009-01-21 15:13:42 +00001948 spin_lock_irqsave(&priv->lock, flags);
1949 list_for_each_entry(p, &priv->ports, node)
1950 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001951 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001952
1953 return 0;
1954}
1955
Alexey Dobriyan47145212009-12-14 18:00:08 -08001956static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001957 .suspend = sci_suspend,
1958 .resume = sci_resume,
1959};
1960
Paul Mundte108b2c2006-09-27 16:32:13 +09001961static struct platform_driver sci_driver = {
1962 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001963 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001964 .driver = {
1965 .name = "sh-sci",
1966 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001967 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001968 },
1969};
1970
1971static int __init sci_init(void)
1972{
1973 int ret;
1974
1975 printk(banner);
1976
Paul Mundte108b2c2006-09-27 16:32:13 +09001977 ret = uart_register_driver(&sci_uart_driver);
1978 if (likely(ret == 0)) {
1979 ret = platform_driver_register(&sci_driver);
1980 if (unlikely(ret))
1981 uart_unregister_driver(&sci_uart_driver);
1982 }
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return ret;
1985}
1986
1987static void __exit sci_exit(void)
1988{
Paul Mundte108b2c2006-09-27 16:32:13 +09001989 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 uart_unregister_driver(&sci_uart_driver);
1991}
1992
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001993#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1994early_platform_init_buffer("earlyprintk", &sci_driver,
1995 early_serial_buf, ARRAY_SIZE(early_serial_buf));
1996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997module_init(sci_init);
1998module_exit(sci_exit);
1999
Paul Mundte108b2c2006-09-27 16:32:13 +09002000MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002001MODULE_ALIAS("platform:sh-sci");