blob: 291bc08e2e8406f30f7fb635df49749cf35d2beb [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 Liakhovetski73a19e42010-03-02 11:39:15 +090051#include <linux/dmaengine.h>
52#include <linux/scatterlist.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 Mundte108b2c2006-09-27 16:32:13 +090067 /* Port type */
68 unsigned int type;
69
70 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090071 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090072
Paul Mundte108b2c2006-09-27 16:32:13 +090073 /* Port enable callback */
74 void (*enable)(struct uart_port *port);
75
76 /* Port disable callback */
77 void (*disable)(struct uart_port *port);
78
79 /* Break timer */
80 struct timer_list break_timer;
81 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090082
Magnus Damm501b8252009-01-21 15:14:30 +000083 /* Interface clock */
84 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090085 /* Function clock */
86 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090087
Magnus Damme552de22009-01-21 15:13:42 +000088 struct list_head node;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090089 struct dma_chan *chan_tx;
90 struct dma_chan *chan_rx;
91#ifdef CONFIG_SERIAL_SH_SCI_DMA
92 struct device *dma_dev;
Magnus Damm4bab9d42010-03-19 04:46:38 +000093 unsigned int slave_tx;
94 unsigned int slave_rx;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090095 struct dma_async_tx_descriptor *desc_tx;
96 struct dma_async_tx_descriptor *desc_rx[2];
97 dma_cookie_t cookie_tx;
98 dma_cookie_t cookie_rx[2];
99 dma_cookie_t active_rx;
100 struct scatterlist sg_tx;
101 unsigned int sg_len_tx;
102 struct scatterlist sg_rx[2];
103 size_t buf_len_rx;
104 struct sh_dmae_slave param_tx;
105 struct sh_dmae_slave param_rx;
106 struct work_struct work_tx;
107 struct work_struct work_rx;
108 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000109 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900110#endif
Magnus Damme552de22009-01-21 15:13:42 +0000111};
112
113struct sh_sci_priv {
114 spinlock_t lock;
115 struct list_head ports;
Magnus Damme552de22009-01-21 15:13:42 +0000116 struct notifier_block clk_nb;
Paul Mundte108b2c2006-09-27 16:32:13 +0900117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100120static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Paul Mundte108b2c2006-09-27 16:32:13 +0900122#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
123
124static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct uart_driver sci_uart_driver;
126
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900127static inline struct sci_port *
128to_sci_port(struct uart_port *uart)
129{
130 return container_of(uart, struct sci_port, port);
131}
132
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900133#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900134
135#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900136static inline void handle_error(struct uart_port *port)
137{
138 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
140}
141
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900142static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned short status;
145 int c;
146
Paul Mundte108b2c2006-09-27 16:32:13 +0900147 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 status = sci_in(port, SCxSR);
149 if (status & SCxSR_ERRORS(port)) {
150 handle_error(port);
151 continue;
152 }
153 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900156
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900157 /* Dummy read */
158 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return c;
162}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900165static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned short status;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 do {
170 status = sci_in(port, SCxSR);
171 } while (!(status & SCxSR_TDxE(port)));
172
Paul Mundt272966c2008-11-13 17:46:06 +0900173 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000174 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900176#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Paul Mundt15c73aa2008-10-02 19:47:12 +0900178#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900179static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int ch = (port->mapbase - SMR0) >> 3;
182
183 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900184 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
185 h8300_sci_pins[ch].rx,
186 H8300_GPIO_INPUT);
187 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
188 h8300_sci_pins[ch].tx,
189 H8300_GPIO_OUTPUT);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* tx mark output*/
192 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
193}
Paul Mundtd5701642008-12-16 20:07:27 +0900194#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
195static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900196{
Paul Mundtd5701642008-12-16 20:07:27 +0900197 if (port->mapbase == 0xA4400000) {
198 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
199 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
200 } else if (port->mapbase == 0xA4410000)
201 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900202}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900203#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900204static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900205{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900206 unsigned short data;
207
208 if (cflag & CRTSCTS) {
209 /* enable RTS/CTS */
210 if (port->mapbase == 0xa4430000) { /* SCIF0 */
211 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900212 data = __raw_readw(PORT_PTCR);
213 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900214 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
215 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900216 data = __raw_readw(PORT_PVCR);
217 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 } else {
220 if (port->mapbase == 0xa4430000) { /* SCIF0 */
221 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900222 data = __raw_readw(PORT_PTCR);
223 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900224 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
225 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900226 data = __raw_readw(PORT_PVCR);
227 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900228 }
229 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900230}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800231#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900232/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900233static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800235 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Paul Mundtb7a76e42006-02-01 03:06:06 -0800237 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900238 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800239 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900240 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800241
Paul Mundtd5701642008-12-16 20:07:27 +0900242 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900244 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Clear out SCP7MD1,0, SCP4MD1,0,
246 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900247 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Paul Mundt32b53072009-12-24 14:52:43 +0900249 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900251 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Paul Mundt41504c32006-12-11 20:28:03 +0900254#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900255static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900256{
Magnus Damm346b7462008-04-23 21:25:29 +0900257 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900258
Magnus Damm346b7462008-04-23 21:25:29 +0900259 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900260 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900261 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900262 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900263 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900264
Paul Mundtd5701642008-12-16 20:07:27 +0900265 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900266 }
Paul Mundt41504c32006-12-11 20:28:03 +0900267}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900268#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
269 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900270 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900271 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900272 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900273 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900274static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
275{
276 if (!(cflag & CRTSCTS))
277 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
278}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900279#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900280static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
281{
282 if (!(cflag & CRTSCTS))
283 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
284}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800285#else
Paul Mundtd5701642008-12-16 20:07:27 +0900286static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
287{
288 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Paul Mundte108b2c2006-09-27 16:32:13 +0900290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Paul Mundt32351a22007-03-12 14:38:59 +0900292#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
293 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900294 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
295 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900296static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900297{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900298 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900299}
300
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900301static int scif_txroom(struct uart_port *port)
302{
303 return SCIF_TXROOM_MAX - scif_txfill(port);
304}
305
306static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900307{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900308 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900309}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900310#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900311static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900312{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900313 if (port->mapbase == 0xffe00000 ||
314 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900315 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900316 return sci_in(port, SCTFDR) & 0xff;
317 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900318 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900319 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900320}
321
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900322static int scif_txroom(struct uart_port *port)
323{
324 if (port->mapbase == 0xffe00000 ||
325 port->mapbase == 0xffe08000)
326 /* SCIF0/1*/
327 return SCIF_TXROOM_MAX - scif_txfill(port);
328 else
329 /* SCIF2 */
330 return SCIF2_TXROOM_MAX - scif_txfill(port);
331}
332
333static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900334{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900335 if ((port->mapbase == 0xffe00000) ||
336 (port->mapbase == 0xffe08000)) {
337 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900338 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900339 } else {
340 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900341 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900342 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900343}
Paul Mundte108b2c2006-09-27 16:32:13 +0900344#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900345static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900346{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900347 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900348}
349
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900350static int scif_txroom(struct uart_port *port)
351{
352 return SCIF_TXROOM_MAX - scif_txfill(port);
353}
354
355static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900356{
357 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900361static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900362{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900363 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900364}
365
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900366static int sci_txroom(struct uart_port *port)
367{
368 return !sci_txfill(port);
369}
370
371static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900372{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900373 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* ********************************************************************** *
377 * the interrupt related routines *
378 * ********************************************************************** */
379
380static void sci_transmit_chars(struct uart_port *port)
381{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700382 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned short status;
385 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900386 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 status = sci_in(port, SCxSR);
389 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900391 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900393 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return;
397 }
398
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900399 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900400 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900401 else
402 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 do {
405 unsigned char c;
406
407 if (port->x_char) {
408 c = port->x_char;
409 port->x_char = 0;
410 } else if (!uart_circ_empty(xmit) && !stopped) {
411 c = xmit->buf[xmit->tail];
412 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
413 } else {
414 break;
415 }
416
417 sci_out(port, SCxTDR, c);
418
419 port->icount.tx++;
420 } while (--count > 0);
421
422 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
423
424 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
425 uart_write_wakeup(port);
426 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100427 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 ctrl = sci_in(port, SCSCR);
430
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900431 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sci_in(port, SCxSR); /* Dummy read */
433 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 ctrl |= SCI_CTRL_FLAGS_TIE;
437 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439}
440
441/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900442#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Howells7d12e782006-10-05 14:55:46 +0100444static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900446 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700447 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int i, count, copied = 0;
449 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800450 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 status = sci_in(port, SCxSR);
453 if (!(status & SCxSR_RDxF(port)))
454 return;
455
456 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900457 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900458 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900459 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900460 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800463 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* If for any reason we can't copy more data, we're done! */
466 if (count == 0)
467 break;
468
469 if (port->type == PORT_SCI) {
470 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900471 if (uart_handle_sysrq_char(port, c) ||
472 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900474 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900475 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900477 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 char c = sci_in(port, SCxRDR);
479 status = sci_in(port, SCxSR);
480#if defined(CONFIG_CPU_SH3)
481 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900482 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((c == 0) &&
484 (status & SCxSR_FER(port))) {
485 count--; i--;
486 continue;
487 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900490 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900491 sci_port->break_flag = 0;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (STEPFN(c)) {
494 count--; i--;
495 continue;
496 }
497 }
498#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100499 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 count--; i--;
501 continue;
502 }
503
504 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900505 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800506 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900507 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900508 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800509 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900510 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800511 } else
512 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900513
Alan Cox33f0f882006-01-09 20:54:13 -0800514 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 }
517
518 sci_in(port, SCxSR); /* dummy read */
519 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 copied += count;
522 port->icount.rx += count;
523 }
524
525 if (copied) {
526 /* Tell the rest of the system the news. New characters! */
527 tty_flip_buffer_push(tty);
528 } else {
529 sci_in(port, SCxSR); /* dummy read */
530 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
531 }
532}
533
534#define SCI_BREAK_JIFFIES (HZ/20)
535/* The sci generates interrupts during the break,
536 * 1 per millisecond or so during the break period, for 9600 baud.
537 * So dont bother disabling interrupts.
538 * But dont want more than 1 break event.
539 * Use a kernel timer to periodically poll the rx line until
540 * the break is finished.
541 */
542static void sci_schedule_break_timer(struct sci_port *port)
543{
544 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
545 add_timer(&port->break_timer);
546}
547/* Ensure that two consecutive samples find the break over. */
548static void sci_break_timer(unsigned long data)
549{
Paul Mundte108b2c2006-09-27 16:32:13 +0900550 struct sci_port *port = (struct sci_port *)data;
551
552 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900554 sci_schedule_break_timer(port);
555 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* break is over. */
557 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900558 sci_schedule_break_timer(port);
559 } else
560 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563static inline int sci_handle_errors(struct uart_port *port)
564{
565 int copied = 0;
566 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700567 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Paul Mundte108b2c2006-09-27 16:32:13 +0900569 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900571 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800572 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900573
574 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Paul Mundte108b2c2006-09-27 16:32:13 +0900577 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (sci_rxd_in(port) == 0) {
579 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900580 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900581
582 if (!sci_port->break_flag) {
583 sci_port->break_flag = 1;
584 sci_schedule_break_timer(sci_port);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900587 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900589
590 dev_dbg(port->dev, "BREAK detected\n");
591
Paul Mundte108b2c2006-09-27 16:32:13 +0900592 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900593 copied++;
594 }
595
Paul Mundte108b2c2006-09-27 16:32:13 +0900596 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900598 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800599 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900600
601 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900607 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
608 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900609
610 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Alan Cox33f0f882006-01-09 20:54:13 -0800613 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return copied;
617}
618
Paul Mundtd830fa42008-12-16 19:29:38 +0900619static inline int sci_handle_fifo_overrun(struct uart_port *port)
620{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700621 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900622 int copied = 0;
623
624 if (port->type != PORT_SCIF)
625 return 0;
626
627 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
628 sci_out(port, SCLSR, 0);
629
630 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
631 tty_flip_buffer_push(tty);
632
633 dev_notice(port->dev, "overrun error\n");
634 copied++;
635 }
636
637 return copied;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static inline int sci_handle_breaks(struct uart_port *port)
641{
642 int copied = 0;
643 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700644 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000645 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900647 if (uart_handle_break(port))
648 return 0;
649
Paul Mundtb7a76e42006-02-01 03:06:06 -0800650 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#if defined(CONFIG_CPU_SH3)
652 /* Debounce break */
653 s->break_flag = 1;
654#endif
655 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900656 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800657 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900658
659 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Alan Cox33f0f882006-01-09 20:54:13 -0800662 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900664
Paul Mundtd830fa42008-12-16 19:29:38 +0900665 copied += sci_handle_fifo_overrun(port);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return copied;
668}
669
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900670static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900672#ifdef CONFIG_SERIAL_SH_SCI_DMA
673 struct uart_port *port = ptr;
674 struct sci_port *s = to_sci_port(port);
675
676 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900677 u16 scr = sci_in(port, SCSCR);
678 u16 ssr = sci_in(port, SCxSR);
679
680 /* Disable future Rx interrupts */
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000681 if (port->type == PORT_SCIFA) {
682 disable_irq_nosync(irq);
683 scr |= 0x4000;
684 } else {
685 scr &= ~SCI_CTRL_FLAGS_RIE;
686 }
687 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900688 /* Clear current interrupt */
689 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000690 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
691 jiffies, s->rx_timeout);
692 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900693
694 return IRQ_HANDLED;
695 }
696#endif
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* I think sci_receive_chars has to be called irrespective
699 * of whether the I_IXOFF is set, otherwise, how is the interrupt
700 * to be disabled?
701 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900702 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return IRQ_HANDLED;
705}
706
David Howells7d12e782006-10-05 14:55:46 +0100707static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900710 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Stuart Menefyfd78a762009-07-29 23:01:24 +0900712 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900714 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return IRQ_HANDLED;
717}
718
David Howells7d12e782006-10-05 14:55:46 +0100719static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct uart_port *port = ptr;
722
723 /* Handle errors */
724 if (port->type == PORT_SCI) {
725 if (sci_handle_errors(port)) {
726 /* discard character in rx buffer */
727 sci_in(port, SCxSR);
728 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
729 }
730 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900731 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100732 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
736
737 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100738 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 return IRQ_HANDLED;
741}
742
David Howells7d12e782006-10-05 14:55:46 +0100743static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct uart_port *port = ptr;
746
747 /* Handle BREAKs */
748 sci_handle_breaks(port);
749 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
750
751 return IRQ_HANDLED;
752}
753
David Howells7d12e782006-10-05 14:55:46 +0100754static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Magnus Damm44e18e92009-07-03 08:39:34 +0000756 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900757 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900758 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900759 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900761 ssr_status = sci_in(port, SCxSR);
762 scr_status = sci_in(port, SCSCR);
Magnus Damm44e18e92009-07-03 08:39:34 +0000763 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* Tx Interrupt */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900766 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE) &&
767 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900768 ret = sci_tx_interrupt(irq, ptr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900769 /*
770 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
771 * DR flags
772 */
773 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
774 (scr_status & SCI_CTRL_FLAGS_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900775 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000777 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900778 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000780 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900781 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Michael Trimarchia8884e32008-10-31 16:10:23 +0900783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * Here we define a transistion notifier so that we can update all of our
788 * ports' baud rate when the peripheral clock changes.
789 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900790static int sci_notifier(struct notifier_block *self,
791 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Magnus Damme552de22009-01-21 15:13:42 +0000793 struct sh_sci_priv *priv = container_of(self,
794 struct sh_sci_priv, clk_nb);
795 struct sci_port *sci_port;
796 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000799 (phase == CPUFREQ_RESUMECHANGE)) {
800 spin_lock_irqsave(&priv->lock, flags);
801 list_for_each_entry(sci_port, &priv->ports, node)
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900802 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
Magnus Damme552de22009-01-21 15:13:42 +0000803 spin_unlock_irqrestore(&priv->lock, flags);
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return NOTIFY_OK;
807}
Magnus Damm501b8252009-01-21 15:14:30 +0000808
809static void sci_clk_enable(struct uart_port *port)
810{
811 struct sci_port *sci_port = to_sci_port(port);
812
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900813 clk_enable(sci_port->iclk);
814 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
815 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000816}
817
818static void sci_clk_disable(struct uart_port *port)
819{
820 struct sci_port *sci_port = to_sci_port(port);
821
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900822 clk_disable(sci_port->fclk);
823 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000824}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826static int sci_request_irq(struct sci_port *port)
827{
828 int i;
David Howells7d12e782006-10-05 14:55:46 +0100829 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
831 sci_br_interrupt,
832 };
833 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
834 "SCI Transmit Data Empty", "SCI Break" };
835
836 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900837 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900839
840 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900841 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900842 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -ENODEV;
844 }
845 } else {
846 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900847 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900849
Paul Mundte108b2c2006-09-27 16:32:13 +0900850 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900851 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900852 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -ENODEV;
854 }
855 }
856 }
857
858 return 0;
859}
860
861static void sci_free_irq(struct sci_port *port)
862{
863 int i;
864
Paul Mundt762c69e2008-12-16 18:55:26 +0900865 if (port->irqs[0] == port->irqs[1])
866 free_irq(port->irqs[0], port);
867 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
869 if (!port->irqs[i])
870 continue;
871
872 free_irq(port->irqs[i], port);
873 }
874 }
875}
876
877static unsigned int sci_tx_empty(struct uart_port *port)
878{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000879 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900880 unsigned short in_tx_fifo = scif_txfill(port);
881
882 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
886{
887 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
888 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
889 /* If you have signals for DTR and DCD, please implement here. */
890}
891
892static unsigned int sci_get_mctrl(struct uart_port *port)
893{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900894 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 and CTS/RTS */
896
897 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
898}
899
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900900#ifdef CONFIG_SERIAL_SH_SCI_DMA
901static void sci_dma_tx_complete(void *arg)
902{
903 struct sci_port *s = arg;
904 struct uart_port *port = &s->port;
905 struct circ_buf *xmit = &port->state->xmit;
906 unsigned long flags;
907
908 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
909
910 spin_lock_irqsave(&port->lock, flags);
911
Magnus Dammf354a382010-03-19 04:47:01 +0000912 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900913 xmit->tail &= UART_XMIT_SIZE - 1;
914
Magnus Dammf354a382010-03-19 04:47:01 +0000915 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900916
917 async_tx_ack(s->desc_tx);
918 s->cookie_tx = -EINVAL;
919 s->desc_tx = NULL;
920
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900921 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
922 uart_write_wakeup(port);
923
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000924 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900925 schedule_work(&s->work_tx);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000926 } else if (port->type == PORT_SCIFA) {
927 u16 ctrl = sci_in(port, SCSCR);
928 sci_out(port, SCSCR, ctrl & ~SCI_CTRL_FLAGS_TIE);
929 }
930
931 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900932}
933
934/* Locking: called with port lock held */
935static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
936 size_t count)
937{
938 struct uart_port *port = &s->port;
939 int i, active, room;
940
941 room = tty_buffer_request_room(tty, count);
942
943 if (s->active_rx == s->cookie_rx[0]) {
944 active = 0;
945 } else if (s->active_rx == s->cookie_rx[1]) {
946 active = 1;
947 } else {
948 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
949 return 0;
950 }
951
952 if (room < count)
953 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
954 count - room);
955 if (!room)
956 return room;
957
958 for (i = 0; i < room; i++)
959 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
960 TTY_NORMAL);
961
962 port->icount.rx += room;
963
964 return room;
965}
966
967static void sci_dma_rx_complete(void *arg)
968{
969 struct sci_port *s = arg;
970 struct uart_port *port = &s->port;
971 struct tty_struct *tty = port->state->port.tty;
972 unsigned long flags;
973 int count;
974
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000975 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900976
977 spin_lock_irqsave(&port->lock, flags);
978
979 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
980
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000981 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900982
983 spin_unlock_irqrestore(&port->lock, flags);
984
985 if (count)
986 tty_flip_buffer_push(tty);
987
988 schedule_work(&s->work_rx);
989}
990
991static void sci_start_rx(struct uart_port *port);
992static void sci_start_tx(struct uart_port *port);
993
994static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
995{
996 struct dma_chan *chan = s->chan_rx;
997 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900998
999 s->chan_rx = NULL;
1000 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1001 dma_release_channel(chan);
1002 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1003 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1004 if (enable_pio)
1005 sci_start_rx(port);
1006}
1007
1008static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1009{
1010 struct dma_chan *chan = s->chan_tx;
1011 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001012
1013 s->chan_tx = NULL;
1014 s->cookie_tx = -EINVAL;
1015 dma_release_channel(chan);
1016 if (enable_pio)
1017 sci_start_tx(port);
1018}
1019
1020static void sci_submit_rx(struct sci_port *s)
1021{
1022 struct dma_chan *chan = s->chan_rx;
1023 int i;
1024
1025 for (i = 0; i < 2; i++) {
1026 struct scatterlist *sg = &s->sg_rx[i];
1027 struct dma_async_tx_descriptor *desc;
1028
1029 desc = chan->device->device_prep_slave_sg(chan,
1030 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1031
1032 if (desc) {
1033 s->desc_rx[i] = desc;
1034 desc->callback = sci_dma_rx_complete;
1035 desc->callback_param = s;
1036 s->cookie_rx[i] = desc->tx_submit(desc);
1037 }
1038
1039 if (!desc || s->cookie_rx[i] < 0) {
1040 if (i) {
1041 async_tx_ack(s->desc_rx[0]);
1042 s->cookie_rx[0] = -EINVAL;
1043 }
1044 if (desc) {
1045 async_tx_ack(desc);
1046 s->cookie_rx[i] = -EINVAL;
1047 }
1048 dev_warn(s->port.dev,
1049 "failed to re-start DMA, using PIO\n");
1050 sci_rx_dma_release(s, true);
1051 return;
1052 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001053 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1054 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001055 }
1056
1057 s->active_rx = s->cookie_rx[0];
1058
1059 dma_async_issue_pending(chan);
1060}
1061
1062static void work_fn_rx(struct work_struct *work)
1063{
1064 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1065 struct uart_port *port = &s->port;
1066 struct dma_async_tx_descriptor *desc;
1067 int new;
1068
1069 if (s->active_rx == s->cookie_rx[0]) {
1070 new = 0;
1071 } else if (s->active_rx == s->cookie_rx[1]) {
1072 new = 1;
1073 } else {
1074 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1075 return;
1076 }
1077 desc = s->desc_rx[new];
1078
1079 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1080 DMA_SUCCESS) {
1081 /* Handle incomplete DMA receive */
1082 struct tty_struct *tty = port->state->port.tty;
1083 struct dma_chan *chan = s->chan_rx;
1084 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1085 async_tx);
1086 unsigned long flags;
1087 int count;
1088
1089 chan->device->device_terminate_all(chan);
1090 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1091 sh_desc->partial, sh_desc->cookie);
1092
1093 spin_lock_irqsave(&port->lock, flags);
1094 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1095 spin_unlock_irqrestore(&port->lock, flags);
1096
1097 if (count)
1098 tty_flip_buffer_push(tty);
1099
1100 sci_submit_rx(s);
1101
1102 return;
1103 }
1104
1105 s->cookie_rx[new] = desc->tx_submit(desc);
1106 if (s->cookie_rx[new] < 0) {
1107 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1108 sci_rx_dma_release(s, true);
1109 return;
1110 }
1111
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001112 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001113
1114 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1115 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001116}
1117
1118static void work_fn_tx(struct work_struct *work)
1119{
1120 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1121 struct dma_async_tx_descriptor *desc;
1122 struct dma_chan *chan = s->chan_tx;
1123 struct uart_port *port = &s->port;
1124 struct circ_buf *xmit = &port->state->xmit;
1125 struct scatterlist *sg = &s->sg_tx;
1126
1127 /*
1128 * DMA is idle now.
1129 * Port xmit buffer is already mapped, and it is one page... Just adjust
1130 * offsets and lengths. Since it is a circular buffer, we have to
1131 * transmit till the end, and then the rest. Take the port lock to get a
1132 * consistent xmit buffer state.
1133 */
1134 spin_lock_irq(&port->lock);
1135 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001136 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001137 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001138 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001139 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001140 spin_unlock_irq(&port->lock);
1141
Magnus Dammf354a382010-03-19 04:47:01 +00001142 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001143
1144 desc = chan->device->device_prep_slave_sg(chan,
1145 sg, s->sg_len_tx, DMA_TO_DEVICE,
1146 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1147 if (!desc) {
1148 /* switch to PIO */
1149 sci_tx_dma_release(s, true);
1150 return;
1151 }
1152
1153 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1154
1155 spin_lock_irq(&port->lock);
1156 s->desc_tx = desc;
1157 desc->callback = sci_dma_tx_complete;
1158 desc->callback_param = s;
1159 spin_unlock_irq(&port->lock);
1160 s->cookie_tx = desc->tx_submit(desc);
1161 if (s->cookie_tx < 0) {
1162 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1163 /* switch to PIO */
1164 sci_tx_dma_release(s, true);
1165 return;
1166 }
1167
1168 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1169 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1170
1171 dma_async_issue_pending(chan);
1172}
1173#endif
1174
Russell Kingb129a8c2005-08-31 10:12:14 +01001175static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001177 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001178 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001180#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001181 if (port->type == PORT_SCIFA) {
1182 u16 new, scr = sci_in(port, SCSCR);
1183 if (s->chan_tx)
1184 new = scr | 0x8000;
1185 else
1186 new = scr & ~0x8000;
1187 if (new != scr)
1188 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001189 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001190 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1191 s->cookie_tx < 0)
1192 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001193#endif
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001194 if (!s->chan_tx || port->type == PORT_SCIFA) {
1195 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1196 ctrl = sci_in(port, SCSCR);
1197 sci_out(port, SCSCR, ctrl | SCI_CTRL_FLAGS_TIE);
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Russell Kingb129a8c2005-08-31 10:12:14 +01001201static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unsigned short ctrl;
1204
1205 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001207 if (port->type == PORT_SCIFA)
1208 ctrl &= ~0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1210 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001213static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001215 unsigned short ctrl = SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001218 ctrl |= sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001219 if (port->type == PORT_SCIFA)
1220 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224static void sci_stop_rx(struct uart_port *port)
1225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 unsigned short ctrl;
1227
1228 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001230 if (port->type == PORT_SCIFA)
1231 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
1233 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static void sci_enable_ms(struct uart_port *port)
1237{
1238 /* Nothing here yet .. */
1239}
1240
1241static void sci_break_ctl(struct uart_port *port, int break_state)
1242{
1243 /* Nothing here yet .. */
1244}
1245
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001246#ifdef CONFIG_SERIAL_SH_SCI_DMA
1247static bool filter(struct dma_chan *chan, void *slave)
1248{
1249 struct sh_dmae_slave *param = slave;
1250
1251 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1252 param->slave_id);
1253
1254 if (param->dma_dev == chan->device->dev) {
1255 chan->private = param;
1256 return true;
1257 } else {
1258 return false;
1259 }
1260}
1261
1262static void rx_timer_fn(unsigned long arg)
1263{
1264 struct sci_port *s = (struct sci_port *)arg;
1265 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001266 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001267
1268 if (port->type == PORT_SCIFA) {
1269 scr &= ~0x4000;
1270 enable_irq(s->irqs[1]);
1271 }
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001272 sci_out(port, SCSCR, scr | SCI_CTRL_FLAGS_RIE);
1273 dev_dbg(port->dev, "DMA Rx timed out\n");
1274 schedule_work(&s->work_rx);
1275}
1276
1277static void sci_request_dma(struct uart_port *port)
1278{
1279 struct sci_port *s = to_sci_port(port);
1280 struct sh_dmae_slave *param;
1281 struct dma_chan *chan;
1282 dma_cap_mask_t mask;
1283 int nent;
1284
1285 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1286 port->line, s->dma_dev);
1287
1288 if (!s->dma_dev)
1289 return;
1290
1291 dma_cap_zero(mask);
1292 dma_cap_set(DMA_SLAVE, mask);
1293
1294 param = &s->param_tx;
1295
1296 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1297 param->slave_id = s->slave_tx;
1298 param->dma_dev = s->dma_dev;
1299
1300 s->cookie_tx = -EINVAL;
1301 chan = dma_request_channel(mask, filter, param);
1302 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1303 if (chan) {
1304 s->chan_tx = chan;
1305 sg_init_table(&s->sg_tx, 1);
1306 /* UART circular tx buffer is an aligned page. */
1307 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1308 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1309 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1310 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1311 if (!nent)
1312 sci_tx_dma_release(s, false);
1313 else
1314 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1315 sg_dma_len(&s->sg_tx),
1316 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1317
1318 s->sg_len_tx = nent;
1319
1320 INIT_WORK(&s->work_tx, work_fn_tx);
1321 }
1322
1323 param = &s->param_rx;
1324
1325 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1326 param->slave_id = s->slave_rx;
1327 param->dma_dev = s->dma_dev;
1328
1329 chan = dma_request_channel(mask, filter, param);
1330 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1331 if (chan) {
1332 dma_addr_t dma[2];
1333 void *buf[2];
1334 int i;
1335
1336 s->chan_rx = chan;
1337
1338 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1339 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1340 &dma[0], GFP_KERNEL);
1341
1342 if (!buf[0]) {
1343 dev_warn(port->dev,
1344 "failed to allocate dma buffer, using PIO\n");
1345 sci_rx_dma_release(s, true);
1346 return;
1347 }
1348
1349 buf[1] = buf[0] + s->buf_len_rx;
1350 dma[1] = dma[0] + s->buf_len_rx;
1351
1352 for (i = 0; i < 2; i++) {
1353 struct scatterlist *sg = &s->sg_rx[i];
1354
1355 sg_init_table(sg, 1);
1356 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1357 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001358 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001359 }
1360
1361 INIT_WORK(&s->work_rx, work_fn_rx);
1362 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1363
1364 sci_submit_rx(s);
1365 }
1366}
1367
1368static void sci_free_dma(struct uart_port *port)
1369{
1370 struct sci_port *s = to_sci_port(port);
1371
1372 if (!s->dma_dev)
1373 return;
1374
1375 if (s->chan_tx)
1376 sci_tx_dma_release(s, false);
1377 if (s->chan_rx)
1378 sci_rx_dma_release(s, false);
1379}
1380#endif
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static int sci_startup(struct uart_port *port)
1383{
Magnus Damma5660ad2009-01-21 15:14:38 +00001384 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001386 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1387
Paul Mundte108b2c2006-09-27 16:32:13 +09001388 if (s->enable)
1389 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 sci_request_irq(s);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001392#ifdef CONFIG_SERIAL_SH_SCI_DMA
1393 sci_request_dma(port);
1394#endif
Yoshinori Satod6569012005-10-14 15:59:12 -07001395 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001396 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 return 0;
1399}
1400
1401static void sci_shutdown(struct uart_port *port)
1402{
Magnus Damma5660ad2009-01-21 15:14:38 +00001403 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001405 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001408 sci_stop_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001409#ifdef CONFIG_SERIAL_SH_SCI_DMA
1410 sci_free_dma(port);
1411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 sci_free_irq(s);
1413
Paul Mundte108b2c2006-09-27 16:32:13 +09001414 if (s->disable)
1415 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Alan Cox606d0992006-12-08 02:38:45 -08001418static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1419 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001421#ifdef CONFIG_SERIAL_SH_SCI_DMA
1422 struct sci_port *s = to_sci_port(port);
1423#endif
Magnus Damm154280f2009-12-22 03:37:28 +00001424 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001425 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001426 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Magnus Damm154280f2009-12-22 03:37:28 +00001428 /*
1429 * earlyprintk comes here early on with port->uartclk set to zero.
1430 * the clock framework is not up and running at this point so here
1431 * we assume that 115200 is the maximum baud rate. please note that
1432 * the baud rate is not programmed during earlyprintk - it is assumed
1433 * that the previous boot loader has enabled required clocks and
1434 * setup the baud rate generator hardware for us already.
1435 */
1436 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1437
1438 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1439 if (likely(baud && port->uartclk))
Paul Mundta2159b52008-10-02 19:09:13 +09001440 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Paul Mundte108b2c2006-09-27 16:32:13 +09001442 do {
1443 status = sci_in(port, SCxSR);
1444 } while (!(status & SCxSR_TEND(port)));
1445
1446 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1447
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001448 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001449 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001450
1451 smr_val = sci_in(port, SCSMR) & 3;
1452 if ((termios->c_cflag & CSIZE) == CS7)
1453 smr_val |= 0x40;
1454 if (termios->c_cflag & PARENB)
1455 smr_val |= 0x20;
1456 if (termios->c_cflag & PARODD)
1457 smr_val |= 0x30;
1458 if (termios->c_cflag & CSTOPB)
1459 smr_val |= 0x08;
1460
1461 uart_update_timeout(port, termios->c_cflag, baud);
1462
1463 sci_out(port, SCSMR, smr_val);
1464
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001465 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1466 SCSCR_INIT(port));
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001469 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1471 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001472 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 sci_out(port, SCBRR, t);
1476 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1477 }
1478
Paul Mundtd5701642008-12-16 20:07:27 +09001479 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001480 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 sci_out(port, SCSCR, SCSCR_INIT(port));
1483
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001484#ifdef CONFIG_SERIAL_SH_SCI_DMA
1485 /*
1486 * Calculate delay for 1.5 DMA buffers: see
1487 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1488 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1489 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1490 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1491 * sizes), but it has been found out experimentally, that this is not
1492 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1493 * as a minimum seem to work perfectly.
1494 */
1495 if (s->chan_rx) {
1496 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1497 port->fifosize / 2;
1498 dev_dbg(port->dev,
1499 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1500 s->rx_timeout * 1000 / HZ, port->timeout);
1501 if (s->rx_timeout < msecs_to_jiffies(20))
1502 s->rx_timeout = msecs_to_jiffies(20);
1503 }
1504#endif
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001507 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
1510static const char *sci_type(struct uart_port *port)
1511{
1512 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001513 case PORT_IRDA:
1514 return "irda";
1515 case PORT_SCI:
1516 return "sci";
1517 case PORT_SCIF:
1518 return "scif";
1519 case PORT_SCIFA:
1520 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Paul Mundtfa439722008-09-04 18:53:58 +09001523 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526static void sci_release_port(struct uart_port *port)
1527{
1528 /* Nothing here yet .. */
1529}
1530
1531static int sci_request_port(struct uart_port *port)
1532{
1533 /* Nothing here yet .. */
1534 return 0;
1535}
1536
1537static void sci_config_port(struct uart_port *port, int flags)
1538{
Magnus Damma5660ad2009-01-21 15:14:38 +00001539 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 port->type = s->type;
1542
Magnus Damm08f8cb32009-01-21 15:14:22 +00001543 if (port->membase)
1544 return;
1545
1546 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001547 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001548
1549 if (IS_ERR(port->membase))
1550 dev_err(port->dev, "can't remap port#%d\n", port->line);
1551 } else {
1552 /*
1553 * For the simple (and majority of) cases where we don't
1554 * need to do any remapping, just cast the cookie
1555 * directly.
1556 */
1557 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1562{
Magnus Damma5660ad2009-01-21 15:14:38 +00001563 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Yinghai Lua62c4132008-08-19 20:49:55 -07001565 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EINVAL;
1567 if (ser->baud_base < 2400)
1568 /* No paper tape reader for Mitch.. */
1569 return -EINVAL;
1570
1571 return 0;
1572}
1573
1574static struct uart_ops sci_uart_ops = {
1575 .tx_empty = sci_tx_empty,
1576 .set_mctrl = sci_set_mctrl,
1577 .get_mctrl = sci_get_mctrl,
1578 .start_tx = sci_start_tx,
1579 .stop_tx = sci_stop_tx,
1580 .stop_rx = sci_stop_rx,
1581 .enable_ms = sci_enable_ms,
1582 .break_ctl = sci_break_ctl,
1583 .startup = sci_startup,
1584 .shutdown = sci_shutdown,
1585 .set_termios = sci_set_termios,
1586 .type = sci_type,
1587 .release_port = sci_release_port,
1588 .request_port = sci_request_port,
1589 .config_port = sci_config_port,
1590 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001591#ifdef CONFIG_CONSOLE_POLL
1592 .poll_get_char = sci_poll_get_char,
1593 .poll_put_char = sci_poll_put_char,
1594#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595};
1596
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001597static int __devinit sci_init_single(struct platform_device *dev,
1598 struct sci_port *sci_port,
1599 unsigned int index,
1600 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001601{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001602 struct uart_port *port = &sci_port->port;
1603
1604 port->ops = &sci_uart_ops;
1605 port->iotype = UPIO_MEM;
1606 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001607
1608 switch (p->type) {
1609 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001610 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001611 break;
1612 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001613 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001614 break;
1615 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001616 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001617 break;
1618 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001619
1620 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001621 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1622 if (IS_ERR(sci_port->iclk)) {
1623 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1624 if (IS_ERR(sci_port->iclk)) {
1625 dev_err(&dev->dev, "can't get iclk\n");
1626 return PTR_ERR(sci_port->iclk);
1627 }
1628 }
1629
1630 /*
1631 * The function clock is optional, ignore it if we can't
1632 * find it.
1633 */
1634 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1635 if (IS_ERR(sci_port->fclk))
1636 sci_port->fclk = NULL;
1637
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001638 sci_port->enable = sci_clk_enable;
1639 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001640 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001641 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001642
Magnus Damm7ed7e072009-01-21 15:14:14 +00001643 sci_port->break_timer.data = (unsigned long)sci_port;
1644 sci_port->break_timer.function = sci_break_timer;
1645 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001646
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001647 port->mapbase = p->mapbase;
1648 port->membase = p->membase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001649
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001650 port->irq = p->irqs[SCIx_TXI_IRQ];
1651 port->flags = p->flags;
1652 sci_port->type = port->type = p->type;
1653
1654#ifdef CONFIG_SERIAL_SH_SCI_DMA
1655 sci_port->dma_dev = p->dma_dev;
1656 sci_port->slave_tx = p->dma_slave_tx;
1657 sci_port->slave_rx = p->dma_slave_rx;
1658
1659 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1660 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1661#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001662
1663 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001664 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001668static struct tty_driver *serial_console_device(struct console *co, int *index)
1669{
1670 struct uart_driver *p = &sci_uart_driver;
1671 *index = co->index;
1672 return p->tty_driver;
1673}
1674
1675static void serial_console_putchar(struct uart_port *port, int ch)
1676{
1677 sci_poll_put_char(port, ch);
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
1681 * Print a string to the serial port trying not to disturb
1682 * any possible real use of the port...
1683 */
1684static void serial_console_write(struct console *co, const char *s,
1685 unsigned count)
1686{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001687 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001688 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001689 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001690
Magnus Damm501b8252009-01-21 15:14:30 +00001691 if (sci_port->enable)
1692 sci_port->enable(port);
1693
1694 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001695
1696 /* wait until fifo is empty and last bit has been transmitted */
1697 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1698 while ((sci_in(port, SCxSR) & bits) != bits)
1699 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001700
Magnus Damm345e5a72009-11-05 14:34:57 +00001701 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001702 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001705static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001707 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct uart_port *port;
1709 int baud = 115200;
1710 int bits = 8;
1711 int parity = 'n';
1712 int flow = 'n';
1713 int ret;
1714
Paul Mundte108b2c2006-09-27 16:32:13 +09001715 /*
1716 * Check whether an invalid uart number has been specified, and
1717 * if so, search for the first available port that does have
1718 * console support.
1719 */
1720 if (co->index >= SCI_NPORTS)
1721 co->index = 0;
1722
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001723 if (co->data) {
1724 port = co->data;
1725 sci_port = to_sci_port(port);
1726 } else {
1727 sci_port = &sci_ports[co->index];
1728 port = &sci_port->port;
1729 co->data = port;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001733 * Also need to check port->type, we don't actually have any
1734 * UPIO_PORT ports, but uart_report_port() handily misreports
1735 * it anyways if we don't have a port available by the time this is
1736 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001738 if (!port->type)
1739 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001740
Magnus Damm08f8cb32009-01-21 15:14:22 +00001741 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001742
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001743 if (sci_port->enable)
1744 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (options)
1747 uart_parse_options(options, &baud, &parity, &bits, &flow);
1748
1749 ret = uart_set_options(port, co, baud, parity, bits, flow);
1750#if defined(__H8300H__) || defined(__H8300S__)
1751 /* disable rx interrupt */
1752 if (ret == 0)
1753 sci_stop_rx(port);
1754#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001755 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return ret;
1757}
1758
1759static struct console serial_console = {
1760 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001761 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 .write = serial_console_write,
1763 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001764 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766};
1767
1768static int __init sci_console_init(void)
1769{
1770 register_console(&serial_console);
1771 return 0;
1772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001774
1775static struct sci_port early_serial_port;
1776static struct console early_serial_console = {
1777 .name = "early_ttySC",
1778 .write = serial_console_write,
1779 .flags = CON_PRINTBUFFER,
1780};
1781static char early_serial_buf[32];
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1784
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001785#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001786#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001788#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#endif
1790
1791static char banner[] __initdata =
1792 KERN_INFO "SuperH SCI(F) driver initialized\n";
1793
1794static struct uart_driver sci_uart_driver = {
1795 .owner = THIS_MODULE,
1796 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 .dev_name = "ttySC",
1798 .major = SCI_MAJOR,
1799 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001800 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 .cons = SCI_CONSOLE,
1802};
1803
Magnus Damme552de22009-01-21 15:13:42 +00001804
Paul Mundt54507f62009-05-08 23:48:33 +09001805static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001806{
1807 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1808 struct sci_port *p;
1809 unsigned long flags;
1810
Magnus Damme552de22009-01-21 15:13:42 +00001811 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001812
1813 spin_lock_irqsave(&priv->lock, flags);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001814 list_for_each_entry(p, &priv->ports, node) {
Magnus Damme552de22009-01-21 15:13:42 +00001815 uart_remove_one_port(&sci_uart_driver, &p->port);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001816 clk_put(p->iclk);
1817 clk_put(p->fclk);
1818 }
Magnus Damme552de22009-01-21 15:13:42 +00001819 spin_unlock_irqrestore(&priv->lock, flags);
1820
1821 kfree(priv);
1822 return 0;
1823}
1824
Magnus Damm0ee70712009-01-21 15:13:50 +00001825static int __devinit sci_probe_single(struct platform_device *dev,
1826 unsigned int index,
1827 struct plat_sci_port *p,
1828 struct sci_port *sciport)
1829{
1830 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1831 unsigned long flags;
1832 int ret;
1833
1834 /* Sanity check */
1835 if (unlikely(index >= SCI_NPORTS)) {
1836 dev_notice(&dev->dev, "Attempting to register port "
1837 "%d when only %d are available.\n",
1838 index+1, SCI_NPORTS);
1839 dev_notice(&dev->dev, "Consider bumping "
1840 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1841 return 0;
1842 }
1843
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001844 ret = sci_init_single(dev, sciport, index, p);
1845 if (ret)
1846 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001847
1848 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001849 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001850 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001851
1852 INIT_LIST_HEAD(&sciport->node);
1853
1854 spin_lock_irqsave(&priv->lock, flags);
1855 list_add(&sciport->node, &priv->ports);
1856 spin_unlock_irqrestore(&priv->lock, flags);
1857
1858 return 0;
1859}
1860
Paul Mundte108b2c2006-09-27 16:32:13 +09001861/*
1862 * Register a set of serial devices attached to a platform device. The
1863 * list is terminated with a zero flags entry, which means we expect
1864 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1865 * remapping (such as sh64) should also set UPF_IOREMAP.
1866 */
1867static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Paul Mundte108b2c2006-09-27 16:32:13 +09001869 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001870 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001871 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001872
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001873#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1874 if (is_early_platform_device(dev)) {
1875 if (dev->id == -1)
1876 return -ENOTSUPP;
1877 early_serial_console.index = dev->id;
1878 early_serial_console.data = &early_serial_port.port;
1879 sci_init_single(NULL, &early_serial_port, dev->id, p);
1880 serial_console_setup(&early_serial_console, early_serial_buf);
1881 if (!strstr(early_serial_buf, "keep"))
1882 early_serial_console.flags |= CON_BOOT;
1883 register_console(&early_serial_console);
1884 return 0;
1885 }
1886#endif
1887
Magnus Damme552de22009-01-21 15:13:42 +00001888 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1889 if (!priv)
1890 return -ENOMEM;
1891
1892 INIT_LIST_HEAD(&priv->ports);
1893 spin_lock_init(&priv->lock);
1894 platform_set_drvdata(dev, priv);
1895
Magnus Damme552de22009-01-21 15:13:42 +00001896 priv->clk_nb.notifier_call = sci_notifier;
1897 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Magnus Damm0ee70712009-01-21 15:13:50 +00001899 if (dev->id != -1) {
1900 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1901 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001902 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001903 } else {
1904 for (i = 0; p && p->flags != 0; p++, i++) {
1905 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1906 if (ret)
1907 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001908 }
Magnus Damme552de22009-01-21 15:13:42 +00001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911#ifdef CONFIG_SH_STANDARD_BIOS
1912 sh_bios_gdb_detach();
1913#endif
1914
Paul Mundte108b2c2006-09-27 16:32:13 +09001915 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001916
1917err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001918 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001919 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001920}
1921
Paul Mundt6daa79b2009-06-15 07:07:38 +09001922static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001923{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001924 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001925 struct sci_port *p;
1926 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001927
Magnus Damme552de22009-01-21 15:13:42 +00001928 spin_lock_irqsave(&priv->lock, flags);
1929 list_for_each_entry(p, &priv->ports, node)
1930 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001931 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001932
1933 return 0;
1934}
1935
Paul Mundt6daa79b2009-06-15 07:07:38 +09001936static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001937{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001938 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001939 struct sci_port *p;
1940 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001941
Magnus Damme552de22009-01-21 15:13:42 +00001942 spin_lock_irqsave(&priv->lock, flags);
1943 list_for_each_entry(p, &priv->ports, node)
1944 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001945 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001946
1947 return 0;
1948}
1949
Alexey Dobriyan47145212009-12-14 18:00:08 -08001950static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001951 .suspend = sci_suspend,
1952 .resume = sci_resume,
1953};
1954
Paul Mundte108b2c2006-09-27 16:32:13 +09001955static struct platform_driver sci_driver = {
1956 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001957 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001958 .driver = {
1959 .name = "sh-sci",
1960 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001961 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001962 },
1963};
1964
1965static int __init sci_init(void)
1966{
1967 int ret;
1968
1969 printk(banner);
1970
Paul Mundte108b2c2006-09-27 16:32:13 +09001971 ret = uart_register_driver(&sci_uart_driver);
1972 if (likely(ret == 0)) {
1973 ret = platform_driver_register(&sci_driver);
1974 if (unlikely(ret))
1975 uart_unregister_driver(&sci_uart_driver);
1976 }
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return ret;
1979}
1980
1981static void __exit sci_exit(void)
1982{
Paul Mundte108b2c2006-09-27 16:32:13 +09001983 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 uart_unregister_driver(&sci_uart_driver);
1985}
1986
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001987#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1988early_platform_init_buffer("earlyprintk", &sci_driver,
1989 early_serial_buf, ARRAY_SIZE(early_serial_buf));
1990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991module_init(sci_init);
1992module_exit(sci_exit);
1993
Paul Mundte108b2c2006-09-27 16:32:13 +09001994MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001995MODULE_ALIAS("platform:sh-sci");