blob: 8d993c4cceac6e92d8aed9a465e956a1139ae3ba [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>
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 Liakhovetski73a19e42010-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 Liakhovetski73a19e42010-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 Liakhovetski73a19e42010-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 }
154 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900157
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900158 /* Dummy read */
159 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return c;
163}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900166static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned short status;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 do {
171 status = sci_in(port, SCxSR);
172 } while (!(status & SCxSR_TDxE(port)));
173
Paul Mundt272966c2008-11-13 17:46:06 +0900174 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000175 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900177#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Paul Mundt15c73aa2008-10-02 19:47:12 +0900179#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900180static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int ch = (port->mapbase - SMR0) >> 3;
183
184 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900185 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
186 h8300_sci_pins[ch].rx,
187 H8300_GPIO_INPUT);
188 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
189 h8300_sci_pins[ch].tx,
190 H8300_GPIO_OUTPUT);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* tx mark output*/
193 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
194}
Paul Mundtd5701642008-12-16 20:07:27 +0900195#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
196static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900197{
Paul Mundtd5701642008-12-16 20:07:27 +0900198 if (port->mapbase == 0xA4400000) {
199 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
200 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
201 } else if (port->mapbase == 0xA4410000)
202 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900203}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900204#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900205static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900206{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900207 unsigned short data;
208
209 if (cflag & CRTSCTS) {
210 /* enable RTS/CTS */
211 if (port->mapbase == 0xa4430000) { /* SCIF0 */
212 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900213 data = __raw_readw(PORT_PTCR);
214 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900215 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
216 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900217 data = __raw_readw(PORT_PVCR);
218 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900220 } else {
221 if (port->mapbase == 0xa4430000) { /* SCIF0 */
222 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900223 data = __raw_readw(PORT_PTCR);
224 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900225 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
226 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900227 data = __raw_readw(PORT_PVCR);
228 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900229 }
230 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900231}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800232#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900233/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900234static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800236 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Paul Mundtb7a76e42006-02-01 03:06:06 -0800238 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900239 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800240 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900241 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800242
Paul Mundtd5701642008-12-16 20:07:27 +0900243 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900245 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* Clear out SCP7MD1,0, SCP4MD1,0,
247 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900248 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Paul Mundt32b53072009-12-24 14:52:43 +0900250 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900252 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
Paul Mundt41504c32006-12-11 20:28:03 +0900255#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900256static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900257{
Magnus Damm346b7462008-04-23 21:25:29 +0900258 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900259
Magnus Damm346b7462008-04-23 21:25:29 +0900260 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900261 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900262 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900263 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900264 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900265
Paul Mundtd5701642008-12-16 20:07:27 +0900266 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900267 }
Paul Mundt41504c32006-12-11 20:28:03 +0900268}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900269#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
270 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900271 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900272 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900273 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900274 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900275static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
276{
277 if (!(cflag & CRTSCTS))
278 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
279}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900280#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900281static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
282{
283 if (!(cflag & CRTSCTS))
284 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
285}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800286#else
Paul Mundtd5701642008-12-16 20:07:27 +0900287static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
288{
289 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Paul Mundte108b2c2006-09-27 16:32:13 +0900291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Paul Mundt32351a22007-03-12 14:38:59 +0900293#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
294 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900295 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
296 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900297static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900298{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900299 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900300}
301
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900302static int scif_txroom(struct uart_port *port)
303{
304 return SCIF_TXROOM_MAX - scif_txfill(port);
305}
306
307static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900308{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900309 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900310}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900311#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900312static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900313{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900314 if (port->mapbase == 0xffe00000 ||
315 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900316 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900317 return sci_in(port, SCTFDR) & 0xff;
318 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900319 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900320 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900321}
322
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900323static int scif_txroom(struct uart_port *port)
324{
325 if (port->mapbase == 0xffe00000 ||
326 port->mapbase == 0xffe08000)
327 /* SCIF0/1*/
328 return SCIF_TXROOM_MAX - scif_txfill(port);
329 else
330 /* SCIF2 */
331 return SCIF2_TXROOM_MAX - scif_txfill(port);
332}
333
334static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900335{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900336 if ((port->mapbase == 0xffe00000) ||
337 (port->mapbase == 0xffe08000)) {
338 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900339 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900340 } else {
341 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900342 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900343 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900344}
Paul Mundte108b2c2006-09-27 16:32:13 +0900345#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900346static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900347{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900348 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900349}
350
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900351static int scif_txroom(struct uart_port *port)
352{
353 return SCIF_TXROOM_MAX - scif_txfill(port);
354}
355
356static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900357{
358 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900362static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900363{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900364 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900365}
366
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900367static int sci_txroom(struct uart_port *port)
368{
369 return !sci_txfill(port);
370}
371
372static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900373{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900374 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/* ********************************************************************** *
378 * the interrupt related routines *
379 * ********************************************************************** */
380
381static void sci_transmit_chars(struct uart_port *port)
382{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700383 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned short status;
386 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900387 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 status = sci_in(port, SCxSR);
390 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900392 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return;
398 }
399
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900400 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900401 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900402 else
403 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 do {
406 unsigned char c;
407
408 if (port->x_char) {
409 c = port->x_char;
410 port->x_char = 0;
411 } else if (!uart_circ_empty(xmit) && !stopped) {
412 c = xmit->buf[xmit->tail];
413 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
414 } else {
415 break;
416 }
417
418 sci_out(port, SCxTDR, c);
419
420 port->icount.tx++;
421 } while (--count > 0);
422
423 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
424
425 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
426 uart_write_wakeup(port);
427 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100428 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ctrl = sci_in(port, SCSCR);
431
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900432 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sci_in(port, SCxSR); /* Dummy read */
434 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 ctrl |= SCI_CTRL_FLAGS_TIE;
438 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440}
441
442/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900443#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells7d12e782006-10-05 14:55:46 +0100445static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900447 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700448 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int i, count, copied = 0;
450 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800451 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 status = sci_in(port, SCxSR);
454 if (!(status & SCxSR_RDxF(port)))
455 return;
456
457 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900458 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900459 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900460 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900461 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800464 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* If for any reason we can't copy more data, we're done! */
467 if (count == 0)
468 break;
469
470 if (port->type == PORT_SCI) {
471 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900472 if (uart_handle_sysrq_char(port, c) ||
473 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900475 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900476 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900478 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 char c = sci_in(port, SCxRDR);
480 status = sci_in(port, SCxSR);
481#if defined(CONFIG_CPU_SH3)
482 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900483 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if ((c == 0) &&
485 (status & SCxSR_FER(port))) {
486 count--; i--;
487 continue;
488 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900491 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900492 sci_port->break_flag = 0;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (STEPFN(c)) {
495 count--; i--;
496 continue;
497 }
498 }
499#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100500 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 count--; i--;
502 continue;
503 }
504
505 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900506 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800507 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900508 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900509 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800510 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900511 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800512 } else
513 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900514
Alan Cox33f0f882006-01-09 20:54:13 -0800515 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518
519 sci_in(port, SCxSR); /* dummy read */
520 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 copied += count;
523 port->icount.rx += count;
524 }
525
526 if (copied) {
527 /* Tell the rest of the system the news. New characters! */
528 tty_flip_buffer_push(tty);
529 } else {
530 sci_in(port, SCxSR); /* dummy read */
531 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
532 }
533}
534
535#define SCI_BREAK_JIFFIES (HZ/20)
536/* The sci generates interrupts during the break,
537 * 1 per millisecond or so during the break period, for 9600 baud.
538 * So dont bother disabling interrupts.
539 * But dont want more than 1 break event.
540 * Use a kernel timer to periodically poll the rx line until
541 * the break is finished.
542 */
543static void sci_schedule_break_timer(struct sci_port *port)
544{
545 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
546 add_timer(&port->break_timer);
547}
548/* Ensure that two consecutive samples find the break over. */
549static void sci_break_timer(unsigned long data)
550{
Paul Mundte108b2c2006-09-27 16:32:13 +0900551 struct sci_port *port = (struct sci_port *)data;
552
553 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900555 sci_schedule_break_timer(port);
556 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* break is over. */
558 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900559 sci_schedule_break_timer(port);
560 } else
561 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static inline int sci_handle_errors(struct uart_port *port)
565{
566 int copied = 0;
567 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700568 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Paul Mundte108b2c2006-09-27 16:32:13 +0900570 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900572 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800573 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900574
575 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Paul Mundte108b2c2006-09-27 16:32:13 +0900578 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (sci_rxd_in(port) == 0) {
580 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900581 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900582
583 if (!sci_port->break_flag) {
584 sci_port->break_flag = 1;
585 sci_schedule_break_timer(sci_port);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900588 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900590
591 dev_dbg(port->dev, "BREAK detected\n");
592
Paul Mundte108b2c2006-09-27 16:32:13 +0900593 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900594 copied++;
595 }
596
Paul Mundte108b2c2006-09-27 16:32:13 +0900597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900599 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800600 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900601
602 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 }
605
Paul Mundte108b2c2006-09-27 16:32:13 +0900606 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900608 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
609 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900610
611 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
Alan Cox33f0f882006-01-09 20:54:13 -0800614 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return copied;
618}
619
Paul Mundtd830fa42008-12-16 19:29:38 +0900620static inline int sci_handle_fifo_overrun(struct uart_port *port)
621{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700622 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900623 int copied = 0;
624
625 if (port->type != PORT_SCIF)
626 return 0;
627
628 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
629 sci_out(port, SCLSR, 0);
630
631 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
632 tty_flip_buffer_push(tty);
633
634 dev_notice(port->dev, "overrun error\n");
635 copied++;
636 }
637
638 return copied;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static inline int sci_handle_breaks(struct uart_port *port)
642{
643 int copied = 0;
644 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700645 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000646 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900648 if (uart_handle_break(port))
649 return 0;
650
Paul Mundtb7a76e42006-02-01 03:06:06 -0800651 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#if defined(CONFIG_CPU_SH3)
653 /* Debounce break */
654 s->break_flag = 1;
655#endif
656 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900657 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800658 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900659
660 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Alan Cox33f0f882006-01-09 20:54:13 -0800663 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900665
Paul Mundtd830fa42008-12-16 19:29:38 +0900666 copied += sci_handle_fifo_overrun(port);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return copied;
669}
670
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900671static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900673#ifdef CONFIG_SERIAL_SH_SCI_DMA
674 struct uart_port *port = ptr;
675 struct sci_port *s = to_sci_port(port);
676
677 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900678 u16 scr = sci_in(port, SCSCR);
679 u16 ssr = sci_in(port, SCxSR);
680
681 /* Disable future Rx interrupts */
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000682 if (port->type == PORT_SCIFA) {
683 disable_irq_nosync(irq);
684 scr |= 0x4000;
685 } else {
686 scr &= ~SCI_CTRL_FLAGS_RIE;
687 }
688 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900689 /* Clear current interrupt */
690 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000691 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
692 jiffies, s->rx_timeout);
693 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900694
695 return IRQ_HANDLED;
696 }
697#endif
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* I think sci_receive_chars has to be called irrespective
700 * of whether the I_IXOFF is set, otherwise, how is the interrupt
701 * to be disabled?
702 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900703 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return IRQ_HANDLED;
706}
707
David Howells7d12e782006-10-05 14:55:46 +0100708static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900711 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stuart Menefyfd78a762009-07-29 23:01:24 +0900713 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900715 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return IRQ_HANDLED;
718}
719
David Howells7d12e782006-10-05 14:55:46 +0100720static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct uart_port *port = ptr;
723
724 /* Handle errors */
725 if (port->type == PORT_SCI) {
726 if (sci_handle_errors(port)) {
727 /* discard character in rx buffer */
728 sci_in(port, SCxSR);
729 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
730 }
731 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900732 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100733 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
736 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
737
738 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100739 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 return IRQ_HANDLED;
742}
743
David Howells7d12e782006-10-05 14:55:46 +0100744static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct uart_port *port = ptr;
747
748 /* Handle BREAKs */
749 sci_handle_breaks(port);
750 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
751
752 return IRQ_HANDLED;
753}
754
David Howells7d12e782006-10-05 14:55:46 +0100755static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Magnus Damm44e18e92009-07-03 08:39:34 +0000757 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900758 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900759 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900760 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900762 ssr_status = sci_in(port, SCxSR);
763 scr_status = sci_in(port, SCSCR);
Magnus Damm44e18e92009-07-03 08:39:34 +0000764 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* Tx Interrupt */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900767 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE) &&
768 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900769 ret = sci_tx_interrupt(irq, ptr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900770 /*
771 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
772 * DR flags
773 */
774 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
775 (scr_status & SCI_CTRL_FLAGS_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900776 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000778 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900779 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000781 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900782 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Michael Trimarchia8884e32008-10-31 16:10:23 +0900784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
788 * Here we define a transistion notifier so that we can update all of our
789 * ports' baud rate when the peripheral clock changes.
790 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900791static int sci_notifier(struct notifier_block *self,
792 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Magnus Damme552de22009-01-21 15:13:42 +0000794 struct sh_sci_priv *priv = container_of(self,
795 struct sh_sci_priv, clk_nb);
796 struct sci_port *sci_port;
797 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000800 (phase == CPUFREQ_RESUMECHANGE)) {
801 spin_lock_irqsave(&priv->lock, flags);
802 list_for_each_entry(sci_port, &priv->ports, node)
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900803 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
Magnus Damme552de22009-01-21 15:13:42 +0000804 spin_unlock_irqrestore(&priv->lock, flags);
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return NOTIFY_OK;
808}
Magnus Damm501b8252009-01-21 15:14:30 +0000809
810static void sci_clk_enable(struct uart_port *port)
811{
812 struct sci_port *sci_port = to_sci_port(port);
813
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900814 clk_enable(sci_port->iclk);
815 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
816 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000817}
818
819static void sci_clk_disable(struct uart_port *port)
820{
821 struct sci_port *sci_port = to_sci_port(port);
822
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900823 clk_disable(sci_port->fclk);
824 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827static int sci_request_irq(struct sci_port *port)
828{
829 int i;
David Howells7d12e782006-10-05 14:55:46 +0100830 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
832 sci_br_interrupt,
833 };
834 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
835 "SCI Transmit Data Empty", "SCI Break" };
836
837 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900838 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900840
841 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900842 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900843 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -ENODEV;
845 }
846 } else {
847 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900848 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900850
Paul Mundte108b2c2006-09-27 16:32:13 +0900851 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900852 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900853 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -ENODEV;
855 }
856 }
857 }
858
859 return 0;
860}
861
862static void sci_free_irq(struct sci_port *port)
863{
864 int i;
865
Paul Mundt762c69e2008-12-16 18:55:26 +0900866 if (port->irqs[0] == port->irqs[1])
867 free_irq(port->irqs[0], port);
868 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
870 if (!port->irqs[i])
871 continue;
872
873 free_irq(port->irqs[i], port);
874 }
875 }
876}
877
878static unsigned int sci_tx_empty(struct uart_port *port)
879{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000880 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900881 unsigned short in_tx_fifo = scif_txfill(port);
882
883 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
887{
888 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
889 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
890 /* If you have signals for DTR and DCD, please implement here. */
891}
892
893static unsigned int sci_get_mctrl(struct uart_port *port)
894{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900895 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 and CTS/RTS */
897
898 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
899}
900
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900901#ifdef CONFIG_SERIAL_SH_SCI_DMA
902static void sci_dma_tx_complete(void *arg)
903{
904 struct sci_port *s = arg;
905 struct uart_port *port = &s->port;
906 struct circ_buf *xmit = &port->state->xmit;
907 unsigned long flags;
908
909 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
910
911 spin_lock_irqsave(&port->lock, flags);
912
Magnus Dammf354a382010-03-19 04:47:01 +0000913 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900914 xmit->tail &= UART_XMIT_SIZE - 1;
915
Magnus Dammf354a382010-03-19 04:47:01 +0000916 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900917
918 async_tx_ack(s->desc_tx);
919 s->cookie_tx = -EINVAL;
920 s->desc_tx = NULL;
921
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900922 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
923 uart_write_wakeup(port);
924
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000925 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900926 schedule_work(&s->work_tx);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000927 } else if (port->type == PORT_SCIFA) {
928 u16 ctrl = sci_in(port, SCSCR);
929 sci_out(port, SCSCR, ctrl & ~SCI_CTRL_FLAGS_TIE);
930 }
931
932 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900933}
934
935/* Locking: called with port lock held */
936static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
937 size_t count)
938{
939 struct uart_port *port = &s->port;
940 int i, active, room;
941
942 room = tty_buffer_request_room(tty, count);
943
944 if (s->active_rx == s->cookie_rx[0]) {
945 active = 0;
946 } else if (s->active_rx == s->cookie_rx[1]) {
947 active = 1;
948 } else {
949 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
950 return 0;
951 }
952
953 if (room < count)
954 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
955 count - room);
956 if (!room)
957 return room;
958
959 for (i = 0; i < room; i++)
960 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
961 TTY_NORMAL);
962
963 port->icount.rx += room;
964
965 return room;
966}
967
968static void sci_dma_rx_complete(void *arg)
969{
970 struct sci_port *s = arg;
971 struct uart_port *port = &s->port;
972 struct tty_struct *tty = port->state->port.tty;
973 unsigned long flags;
974 int count;
975
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000976 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900977
978 spin_lock_irqsave(&port->lock, flags);
979
980 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
981
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000982 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900983
984 spin_unlock_irqrestore(&port->lock, flags);
985
986 if (count)
987 tty_flip_buffer_push(tty);
988
989 schedule_work(&s->work_rx);
990}
991
992static void sci_start_rx(struct uart_port *port);
993static void sci_start_tx(struct uart_port *port);
994
995static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
996{
997 struct dma_chan *chan = s->chan_rx;
998 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900999
1000 s->chan_rx = NULL;
1001 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1002 dma_release_channel(chan);
1003 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1004 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1005 if (enable_pio)
1006 sci_start_rx(port);
1007}
1008
1009static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1010{
1011 struct dma_chan *chan = s->chan_tx;
1012 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001013
1014 s->chan_tx = NULL;
1015 s->cookie_tx = -EINVAL;
1016 dma_release_channel(chan);
1017 if (enable_pio)
1018 sci_start_tx(port);
1019}
1020
1021static void sci_submit_rx(struct sci_port *s)
1022{
1023 struct dma_chan *chan = s->chan_rx;
1024 int i;
1025
1026 for (i = 0; i < 2; i++) {
1027 struct scatterlist *sg = &s->sg_rx[i];
1028 struct dma_async_tx_descriptor *desc;
1029
1030 desc = chan->device->device_prep_slave_sg(chan,
1031 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1032
1033 if (desc) {
1034 s->desc_rx[i] = desc;
1035 desc->callback = sci_dma_rx_complete;
1036 desc->callback_param = s;
1037 s->cookie_rx[i] = desc->tx_submit(desc);
1038 }
1039
1040 if (!desc || s->cookie_rx[i] < 0) {
1041 if (i) {
1042 async_tx_ack(s->desc_rx[0]);
1043 s->cookie_rx[0] = -EINVAL;
1044 }
1045 if (desc) {
1046 async_tx_ack(desc);
1047 s->cookie_rx[i] = -EINVAL;
1048 }
1049 dev_warn(s->port.dev,
1050 "failed to re-start DMA, using PIO\n");
1051 sci_rx_dma_release(s, true);
1052 return;
1053 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001054 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1055 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001056 }
1057
1058 s->active_rx = s->cookie_rx[0];
1059
1060 dma_async_issue_pending(chan);
1061}
1062
1063static void work_fn_rx(struct work_struct *work)
1064{
1065 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1066 struct uart_port *port = &s->port;
1067 struct dma_async_tx_descriptor *desc;
1068 int new;
1069
1070 if (s->active_rx == s->cookie_rx[0]) {
1071 new = 0;
1072 } else if (s->active_rx == s->cookie_rx[1]) {
1073 new = 1;
1074 } else {
1075 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1076 return;
1077 }
1078 desc = s->desc_rx[new];
1079
1080 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1081 DMA_SUCCESS) {
1082 /* Handle incomplete DMA receive */
1083 struct tty_struct *tty = port->state->port.tty;
1084 struct dma_chan *chan = s->chan_rx;
1085 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1086 async_tx);
1087 unsigned long flags;
1088 int count;
1089
1090 chan->device->device_terminate_all(chan);
1091 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1092 sh_desc->partial, sh_desc->cookie);
1093
1094 spin_lock_irqsave(&port->lock, flags);
1095 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1096 spin_unlock_irqrestore(&port->lock, flags);
1097
1098 if (count)
1099 tty_flip_buffer_push(tty);
1100
1101 sci_submit_rx(s);
1102
1103 return;
1104 }
1105
1106 s->cookie_rx[new] = desc->tx_submit(desc);
1107 if (s->cookie_rx[new] < 0) {
1108 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1109 sci_rx_dma_release(s, true);
1110 return;
1111 }
1112
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001113 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001114
1115 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1116 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001117}
1118
1119static void work_fn_tx(struct work_struct *work)
1120{
1121 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1122 struct dma_async_tx_descriptor *desc;
1123 struct dma_chan *chan = s->chan_tx;
1124 struct uart_port *port = &s->port;
1125 struct circ_buf *xmit = &port->state->xmit;
1126 struct scatterlist *sg = &s->sg_tx;
1127
1128 /*
1129 * DMA is idle now.
1130 * Port xmit buffer is already mapped, and it is one page... Just adjust
1131 * offsets and lengths. Since it is a circular buffer, we have to
1132 * transmit till the end, and then the rest. Take the port lock to get a
1133 * consistent xmit buffer state.
1134 */
1135 spin_lock_irq(&port->lock);
1136 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001137 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001138 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001139 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001140 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001141 spin_unlock_irq(&port->lock);
1142
Magnus Dammf354a382010-03-19 04:47:01 +00001143 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001144
1145 desc = chan->device->device_prep_slave_sg(chan,
1146 sg, s->sg_len_tx, DMA_TO_DEVICE,
1147 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1148 if (!desc) {
1149 /* switch to PIO */
1150 sci_tx_dma_release(s, true);
1151 return;
1152 }
1153
1154 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1155
1156 spin_lock_irq(&port->lock);
1157 s->desc_tx = desc;
1158 desc->callback = sci_dma_tx_complete;
1159 desc->callback_param = s;
1160 spin_unlock_irq(&port->lock);
1161 s->cookie_tx = desc->tx_submit(desc);
1162 if (s->cookie_tx < 0) {
1163 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1164 /* switch to PIO */
1165 sci_tx_dma_release(s, true);
1166 return;
1167 }
1168
1169 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1170 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1171
1172 dma_async_issue_pending(chan);
1173}
1174#endif
1175
Russell Kingb129a8c2005-08-31 10:12:14 +01001176static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001178 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001179 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001181#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001182 if (port->type == PORT_SCIFA) {
1183 u16 new, scr = sci_in(port, SCSCR);
1184 if (s->chan_tx)
1185 new = scr | 0x8000;
1186 else
1187 new = scr & ~0x8000;
1188 if (new != scr)
1189 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001190 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001191 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1192 s->cookie_tx < 0)
1193 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001194#endif
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001195 if (!s->chan_tx || port->type == PORT_SCIFA) {
1196 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1197 ctrl = sci_in(port, SCSCR);
1198 sci_out(port, SCSCR, ctrl | SCI_CTRL_FLAGS_TIE);
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Russell Kingb129a8c2005-08-31 10:12:14 +01001202static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 unsigned short ctrl;
1205
1206 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001208 if (port->type == PORT_SCIFA)
1209 ctrl &= ~0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1211 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001214static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001216 unsigned short ctrl = SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001219 ctrl |= sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001220 if (port->type == PORT_SCIFA)
1221 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static void sci_stop_rx(struct uart_port *port)
1226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 unsigned short ctrl;
1228
1229 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 ctrl = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001231 if (port->type == PORT_SCIFA)
1232 ctrl &= ~0x4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
1234 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static void sci_enable_ms(struct uart_port *port)
1238{
1239 /* Nothing here yet .. */
1240}
1241
1242static void sci_break_ctl(struct uart_port *port, int break_state)
1243{
1244 /* Nothing here yet .. */
1245}
1246
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001247#ifdef CONFIG_SERIAL_SH_SCI_DMA
1248static bool filter(struct dma_chan *chan, void *slave)
1249{
1250 struct sh_dmae_slave *param = slave;
1251
1252 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1253 param->slave_id);
1254
1255 if (param->dma_dev == chan->device->dev) {
1256 chan->private = param;
1257 return true;
1258 } else {
1259 return false;
1260 }
1261}
1262
1263static void rx_timer_fn(unsigned long arg)
1264{
1265 struct sci_port *s = (struct sci_port *)arg;
1266 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001267 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001268
1269 if (port->type == PORT_SCIFA) {
1270 scr &= ~0x4000;
1271 enable_irq(s->irqs[1]);
1272 }
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001273 sci_out(port, SCSCR, scr | SCI_CTRL_FLAGS_RIE);
1274 dev_dbg(port->dev, "DMA Rx timed out\n");
1275 schedule_work(&s->work_rx);
1276}
1277
1278static void sci_request_dma(struct uart_port *port)
1279{
1280 struct sci_port *s = to_sci_port(port);
1281 struct sh_dmae_slave *param;
1282 struct dma_chan *chan;
1283 dma_cap_mask_t mask;
1284 int nent;
1285
1286 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1287 port->line, s->dma_dev);
1288
1289 if (!s->dma_dev)
1290 return;
1291
1292 dma_cap_zero(mask);
1293 dma_cap_set(DMA_SLAVE, mask);
1294
1295 param = &s->param_tx;
1296
1297 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1298 param->slave_id = s->slave_tx;
1299 param->dma_dev = s->dma_dev;
1300
1301 s->cookie_tx = -EINVAL;
1302 chan = dma_request_channel(mask, filter, param);
1303 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1304 if (chan) {
1305 s->chan_tx = chan;
1306 sg_init_table(&s->sg_tx, 1);
1307 /* UART circular tx buffer is an aligned page. */
1308 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1309 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1310 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1311 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1312 if (!nent)
1313 sci_tx_dma_release(s, false);
1314 else
1315 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1316 sg_dma_len(&s->sg_tx),
1317 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1318
1319 s->sg_len_tx = nent;
1320
1321 INIT_WORK(&s->work_tx, work_fn_tx);
1322 }
1323
1324 param = &s->param_rx;
1325
1326 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1327 param->slave_id = s->slave_rx;
1328 param->dma_dev = s->dma_dev;
1329
1330 chan = dma_request_channel(mask, filter, param);
1331 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1332 if (chan) {
1333 dma_addr_t dma[2];
1334 void *buf[2];
1335 int i;
1336
1337 s->chan_rx = chan;
1338
1339 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1340 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1341 &dma[0], GFP_KERNEL);
1342
1343 if (!buf[0]) {
1344 dev_warn(port->dev,
1345 "failed to allocate dma buffer, using PIO\n");
1346 sci_rx_dma_release(s, true);
1347 return;
1348 }
1349
1350 buf[1] = buf[0] + s->buf_len_rx;
1351 dma[1] = dma[0] + s->buf_len_rx;
1352
1353 for (i = 0; i < 2; i++) {
1354 struct scatterlist *sg = &s->sg_rx[i];
1355
1356 sg_init_table(sg, 1);
1357 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1358 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001359 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001360 }
1361
1362 INIT_WORK(&s->work_rx, work_fn_rx);
1363 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1364
1365 sci_submit_rx(s);
1366 }
1367}
1368
1369static void sci_free_dma(struct uart_port *port)
1370{
1371 struct sci_port *s = to_sci_port(port);
1372
1373 if (!s->dma_dev)
1374 return;
1375
1376 if (s->chan_tx)
1377 sci_tx_dma_release(s, false);
1378 if (s->chan_rx)
1379 sci_rx_dma_release(s, false);
1380}
1381#endif
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383static int sci_startup(struct uart_port *port)
1384{
Magnus Damma5660ad2009-01-21 15:14:38 +00001385 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001387 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1388
Paul Mundte108b2c2006-09-27 16:32:13 +09001389 if (s->enable)
1390 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 sci_request_irq(s);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001393#ifdef CONFIG_SERIAL_SH_SCI_DMA
1394 sci_request_dma(port);
1395#endif
Yoshinori Satod6569012005-10-14 15:59:12 -07001396 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001397 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return 0;
1400}
1401
1402static void sci_shutdown(struct uart_port *port)
1403{
Magnus Damma5660ad2009-01-21 15:14:38 +00001404 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001406 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001409 sci_stop_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001410#ifdef CONFIG_SERIAL_SH_SCI_DMA
1411 sci_free_dma(port);
1412#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 sci_free_irq(s);
1414
Paul Mundte108b2c2006-09-27 16:32:13 +09001415 if (s->disable)
1416 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Alan Cox606d0992006-12-08 02:38:45 -08001419static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1420 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001422#ifdef CONFIG_SERIAL_SH_SCI_DMA
1423 struct sci_port *s = to_sci_port(port);
1424#endif
Magnus Damm154280f2009-12-22 03:37:28 +00001425 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001426 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001427 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Magnus Damm154280f2009-12-22 03:37:28 +00001429 /*
1430 * earlyprintk comes here early on with port->uartclk set to zero.
1431 * the clock framework is not up and running at this point so here
1432 * we assume that 115200 is the maximum baud rate. please note that
1433 * the baud rate is not programmed during earlyprintk - it is assumed
1434 * that the previous boot loader has enabled required clocks and
1435 * setup the baud rate generator hardware for us already.
1436 */
1437 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1438
1439 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1440 if (likely(baud && port->uartclk))
Paul Mundta2159b52008-10-02 19:09:13 +09001441 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Paul Mundte108b2c2006-09-27 16:32:13 +09001443 do {
1444 status = sci_in(port, SCxSR);
1445 } while (!(status & SCxSR_TEND(port)));
1446
1447 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1448
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001449 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001450 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001451
1452 smr_val = sci_in(port, SCSMR) & 3;
1453 if ((termios->c_cflag & CSIZE) == CS7)
1454 smr_val |= 0x40;
1455 if (termios->c_cflag & PARENB)
1456 smr_val |= 0x20;
1457 if (termios->c_cflag & PARODD)
1458 smr_val |= 0x30;
1459 if (termios->c_cflag & CSTOPB)
1460 smr_val |= 0x08;
1461
1462 uart_update_timeout(port, termios->c_cflag, baud);
1463
1464 sci_out(port, SCSMR, smr_val);
1465
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001466 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1467 SCSCR_INIT(port));
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001470 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1472 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001473 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 sci_out(port, SCBRR, t);
1477 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1478 }
1479
Paul Mundtd5701642008-12-16 20:07:27 +09001480 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001481 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 sci_out(port, SCSCR, SCSCR_INIT(port));
1484
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001485#ifdef CONFIG_SERIAL_SH_SCI_DMA
1486 /*
1487 * Calculate delay for 1.5 DMA buffers: see
1488 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1489 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1490 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1491 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1492 * sizes), but it has been found out experimentally, that this is not
1493 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1494 * as a minimum seem to work perfectly.
1495 */
1496 if (s->chan_rx) {
1497 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1498 port->fifosize / 2;
1499 dev_dbg(port->dev,
1500 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1501 s->rx_timeout * 1000 / HZ, port->timeout);
1502 if (s->rx_timeout < msecs_to_jiffies(20))
1503 s->rx_timeout = msecs_to_jiffies(20);
1504 }
1505#endif
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001508 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511static const char *sci_type(struct uart_port *port)
1512{
1513 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001514 case PORT_IRDA:
1515 return "irda";
1516 case PORT_SCI:
1517 return "sci";
1518 case PORT_SCIF:
1519 return "scif";
1520 case PORT_SCIFA:
1521 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
Paul Mundtfa439722008-09-04 18:53:58 +09001524 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527static void sci_release_port(struct uart_port *port)
1528{
1529 /* Nothing here yet .. */
1530}
1531
1532static int sci_request_port(struct uart_port *port)
1533{
1534 /* Nothing here yet .. */
1535 return 0;
1536}
1537
1538static void sci_config_port(struct uart_port *port, int flags)
1539{
Magnus Damma5660ad2009-01-21 15:14:38 +00001540 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 port->type = s->type;
1543
Magnus Damm08f8cb32009-01-21 15:14:22 +00001544 if (port->membase)
1545 return;
1546
1547 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001548 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001549
1550 if (IS_ERR(port->membase))
1551 dev_err(port->dev, "can't remap port#%d\n", port->line);
1552 } else {
1553 /*
1554 * For the simple (and majority of) cases where we don't
1555 * need to do any remapping, just cast the cookie
1556 * directly.
1557 */
1558 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1563{
Magnus Damma5660ad2009-01-21 15:14:38 +00001564 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Yinghai Lua62c4132008-08-19 20:49:55 -07001566 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EINVAL;
1568 if (ser->baud_base < 2400)
1569 /* No paper tape reader for Mitch.. */
1570 return -EINVAL;
1571
1572 return 0;
1573}
1574
1575static struct uart_ops sci_uart_ops = {
1576 .tx_empty = sci_tx_empty,
1577 .set_mctrl = sci_set_mctrl,
1578 .get_mctrl = sci_get_mctrl,
1579 .start_tx = sci_start_tx,
1580 .stop_tx = sci_stop_tx,
1581 .stop_rx = sci_stop_rx,
1582 .enable_ms = sci_enable_ms,
1583 .break_ctl = sci_break_ctl,
1584 .startup = sci_startup,
1585 .shutdown = sci_shutdown,
1586 .set_termios = sci_set_termios,
1587 .type = sci_type,
1588 .release_port = sci_release_port,
1589 .request_port = sci_request_port,
1590 .config_port = sci_config_port,
1591 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001592#ifdef CONFIG_CONSOLE_POLL
1593 .poll_get_char = sci_poll_get_char,
1594 .poll_put_char = sci_poll_put_char,
1595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596};
1597
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001598static int __devinit sci_init_single(struct platform_device *dev,
1599 struct sci_port *sci_port,
1600 unsigned int index,
1601 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001602{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001603 struct uart_port *port = &sci_port->port;
1604
1605 port->ops = &sci_uart_ops;
1606 port->iotype = UPIO_MEM;
1607 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001608
1609 switch (p->type) {
1610 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001611 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001612 break;
1613 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001614 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001615 break;
1616 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001617 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001618 break;
1619 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001620
1621 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001622 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1623 if (IS_ERR(sci_port->iclk)) {
1624 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1625 if (IS_ERR(sci_port->iclk)) {
1626 dev_err(&dev->dev, "can't get iclk\n");
1627 return PTR_ERR(sci_port->iclk);
1628 }
1629 }
1630
1631 /*
1632 * The function clock is optional, ignore it if we can't
1633 * find it.
1634 */
1635 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1636 if (IS_ERR(sci_port->fclk))
1637 sci_port->fclk = NULL;
1638
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001639 sci_port->enable = sci_clk_enable;
1640 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001641 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001642 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001643
Magnus Damm7ed7e072009-01-21 15:14:14 +00001644 sci_port->break_timer.data = (unsigned long)sci_port;
1645 sci_port->break_timer.function = sci_break_timer;
1646 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001647
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001648 port->mapbase = p->mapbase;
1649 port->membase = p->membase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001650
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001651 port->irq = p->irqs[SCIx_TXI_IRQ];
1652 port->flags = p->flags;
1653 sci_port->type = port->type = p->type;
1654
1655#ifdef CONFIG_SERIAL_SH_SCI_DMA
1656 sci_port->dma_dev = p->dma_dev;
1657 sci_port->slave_tx = p->dma_slave_tx;
1658 sci_port->slave_rx = p->dma_slave_rx;
1659
1660 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1661 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1662#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001663
1664 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001665 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001669static struct tty_driver *serial_console_device(struct console *co, int *index)
1670{
1671 struct uart_driver *p = &sci_uart_driver;
1672 *index = co->index;
1673 return p->tty_driver;
1674}
1675
1676static void serial_console_putchar(struct uart_port *port, int ch)
1677{
1678 sci_poll_put_char(port, ch);
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * Print a string to the serial port trying not to disturb
1683 * any possible real use of the port...
1684 */
1685static void serial_console_write(struct console *co, const char *s,
1686 unsigned count)
1687{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001688 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001689 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001690 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001691
Magnus Damm501b8252009-01-21 15:14:30 +00001692 if (sci_port->enable)
1693 sci_port->enable(port);
1694
1695 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001696
1697 /* wait until fifo is empty and last bit has been transmitted */
1698 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1699 while ((sci_in(port, SCxSR) & bits) != bits)
1700 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001701
Magnus Damm345e5a72009-11-05 14:34:57 +00001702 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001703 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001706static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001708 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct uart_port *port;
1710 int baud = 115200;
1711 int bits = 8;
1712 int parity = 'n';
1713 int flow = 'n';
1714 int ret;
1715
Paul Mundte108b2c2006-09-27 16:32:13 +09001716 /*
1717 * Check whether an invalid uart number has been specified, and
1718 * if so, search for the first available port that does have
1719 * console support.
1720 */
1721 if (co->index >= SCI_NPORTS)
1722 co->index = 0;
1723
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001724 if (co->data) {
1725 port = co->data;
1726 sci_port = to_sci_port(port);
1727 } else {
1728 sci_port = &sci_ports[co->index];
1729 port = &sci_port->port;
1730 co->data = port;
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001734 * Also need to check port->type, we don't actually have any
1735 * UPIO_PORT ports, but uart_report_port() handily misreports
1736 * it anyways if we don't have a port available by the time this is
1737 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001739 if (!port->type)
1740 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001741
Magnus Damm08f8cb32009-01-21 15:14:22 +00001742 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001743
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001744 if (sci_port->enable)
1745 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (options)
1748 uart_parse_options(options, &baud, &parity, &bits, &flow);
1749
1750 ret = uart_set_options(port, co, baud, parity, bits, flow);
1751#if defined(__H8300H__) || defined(__H8300S__)
1752 /* disable rx interrupt */
1753 if (ret == 0)
1754 sci_stop_rx(port);
1755#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001756 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return ret;
1758}
1759
1760static struct console serial_console = {
1761 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001762 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 .write = serial_console_write,
1764 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001765 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767};
1768
1769static int __init sci_console_init(void)
1770{
1771 register_console(&serial_console);
1772 return 0;
1773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001775
1776static struct sci_port early_serial_port;
1777static struct console early_serial_console = {
1778 .name = "early_ttySC",
1779 .write = serial_console_write,
1780 .flags = CON_PRINTBUFFER,
1781};
1782static char early_serial_buf[32];
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1785
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001786#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001787#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001789#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790#endif
1791
1792static char banner[] __initdata =
1793 KERN_INFO "SuperH SCI(F) driver initialized\n";
1794
1795static struct uart_driver sci_uart_driver = {
1796 .owner = THIS_MODULE,
1797 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 .dev_name = "ttySC",
1799 .major = SCI_MAJOR,
1800 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001801 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 .cons = SCI_CONSOLE,
1803};
1804
Magnus Damme552de22009-01-21 15:13:42 +00001805
Paul Mundt54507f62009-05-08 23:48:33 +09001806static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001807{
1808 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1809 struct sci_port *p;
1810 unsigned long flags;
1811
Magnus Damme552de22009-01-21 15:13:42 +00001812 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001813
1814 spin_lock_irqsave(&priv->lock, flags);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001815 list_for_each_entry(p, &priv->ports, node) {
Magnus Damme552de22009-01-21 15:13:42 +00001816 uart_remove_one_port(&sci_uart_driver, &p->port);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001817 clk_put(p->iclk);
1818 clk_put(p->fclk);
1819 }
Magnus Damme552de22009-01-21 15:13:42 +00001820 spin_unlock_irqrestore(&priv->lock, flags);
1821
1822 kfree(priv);
1823 return 0;
1824}
1825
Magnus Damm0ee70712009-01-21 15:13:50 +00001826static int __devinit sci_probe_single(struct platform_device *dev,
1827 unsigned int index,
1828 struct plat_sci_port *p,
1829 struct sci_port *sciport)
1830{
1831 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1832 unsigned long flags;
1833 int ret;
1834
1835 /* Sanity check */
1836 if (unlikely(index >= SCI_NPORTS)) {
1837 dev_notice(&dev->dev, "Attempting to register port "
1838 "%d when only %d are available.\n",
1839 index+1, SCI_NPORTS);
1840 dev_notice(&dev->dev, "Consider bumping "
1841 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1842 return 0;
1843 }
1844
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001845 ret = sci_init_single(dev, sciport, index, p);
1846 if (ret)
1847 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001848
1849 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001850 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001851 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001852
1853 INIT_LIST_HEAD(&sciport->node);
1854
1855 spin_lock_irqsave(&priv->lock, flags);
1856 list_add(&sciport->node, &priv->ports);
1857 spin_unlock_irqrestore(&priv->lock, flags);
1858
1859 return 0;
1860}
1861
Paul Mundte108b2c2006-09-27 16:32:13 +09001862/*
1863 * Register a set of serial devices attached to a platform device. The
1864 * list is terminated with a zero flags entry, which means we expect
1865 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1866 * remapping (such as sh64) should also set UPF_IOREMAP.
1867 */
1868static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Paul Mundte108b2c2006-09-27 16:32:13 +09001870 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001871 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001872 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001873
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001874#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1875 if (is_early_platform_device(dev)) {
1876 if (dev->id == -1)
1877 return -ENOTSUPP;
1878 early_serial_console.index = dev->id;
1879 early_serial_console.data = &early_serial_port.port;
1880 sci_init_single(NULL, &early_serial_port, dev->id, p);
1881 serial_console_setup(&early_serial_console, early_serial_buf);
1882 if (!strstr(early_serial_buf, "keep"))
1883 early_serial_console.flags |= CON_BOOT;
1884 register_console(&early_serial_console);
1885 return 0;
1886 }
1887#endif
1888
Magnus Damme552de22009-01-21 15:13:42 +00001889 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1890 if (!priv)
1891 return -ENOMEM;
1892
1893 INIT_LIST_HEAD(&priv->ports);
1894 spin_lock_init(&priv->lock);
1895 platform_set_drvdata(dev, priv);
1896
Magnus Damme552de22009-01-21 15:13:42 +00001897 priv->clk_nb.notifier_call = sci_notifier;
1898 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Magnus Damm0ee70712009-01-21 15:13:50 +00001900 if (dev->id != -1) {
1901 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1902 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001903 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001904 } else {
1905 for (i = 0; p && p->flags != 0; p++, i++) {
1906 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1907 if (ret)
1908 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001909 }
Magnus Damme552de22009-01-21 15:13:42 +00001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912#ifdef CONFIG_SH_STANDARD_BIOS
1913 sh_bios_gdb_detach();
1914#endif
1915
Paul Mundte108b2c2006-09-27 16:32:13 +09001916 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001917
1918err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001919 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001920 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001921}
1922
Paul Mundt6daa79b2009-06-15 07:07:38 +09001923static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001924{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001925 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001926 struct sci_port *p;
1927 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001928
Magnus Damme552de22009-01-21 15:13:42 +00001929 spin_lock_irqsave(&priv->lock, flags);
1930 list_for_each_entry(p, &priv->ports, node)
1931 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001932 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001933
1934 return 0;
1935}
1936
Paul Mundt6daa79b2009-06-15 07:07:38 +09001937static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001938{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001939 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001940 struct sci_port *p;
1941 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001942
Magnus Damme552de22009-01-21 15:13:42 +00001943 spin_lock_irqsave(&priv->lock, flags);
1944 list_for_each_entry(p, &priv->ports, node)
1945 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001946 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001947
1948 return 0;
1949}
1950
Alexey Dobriyan47145212009-12-14 18:00:08 -08001951static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001952 .suspend = sci_suspend,
1953 .resume = sci_resume,
1954};
1955
Paul Mundte108b2c2006-09-27 16:32:13 +09001956static struct platform_driver sci_driver = {
1957 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001958 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001959 .driver = {
1960 .name = "sh-sci",
1961 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001962 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001963 },
1964};
1965
1966static int __init sci_init(void)
1967{
1968 int ret;
1969
1970 printk(banner);
1971
Paul Mundte108b2c2006-09-27 16:32:13 +09001972 ret = uart_register_driver(&sci_uart_driver);
1973 if (likely(ret == 0)) {
1974 ret = platform_driver_register(&sci_driver);
1975 if (unlikely(ret))
1976 uart_unregister_driver(&sci_uart_driver);
1977 }
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return ret;
1980}
1981
1982static void __exit sci_exit(void)
1983{
Paul Mundte108b2c2006-09-27 16:32:13 +09001984 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 uart_unregister_driver(&sci_uart_driver);
1986}
1987
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001988#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1989early_platform_init_buffer("earlyprintk", &sci_driver,
1990 early_serial_buf, ARRAY_SIZE(early_serial_buf));
1991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992module_init(sci_init);
1993module_exit(sci_exit);
1994
Paul Mundte108b2c2006-09-27 16:32:13 +09001995MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001996MODULE_ALIAS("platform:sh-sci");