blob: 251c08c55ae0f902a78b121fe63e42bed79280f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundtf43dc232011-01-13 15:06:28 +09006 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
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
Paul Mundt00b9de92009-06-24 17:53:33 +090084 /* SCSCR initialization */
85 unsigned int scscr;
86
Paul Mundt26c92f32009-06-24 18:23:52 +090087 /* SCBRR calculation algo */
88 unsigned int scbrr_algo_id;
89
Magnus Damm501b8252009-01-21 15:14:30 +000090 /* Interface clock */
91 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090092 /* Function clock */
93 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090094
Magnus Damme552de22009-01-21 15:13:42 +000095 struct list_head node;
Paul Mundtf43dc232011-01-13 15:06:28 +090096
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090097 struct dma_chan *chan_tx;
98 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090099
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900100#ifdef CONFIG_SERIAL_SH_SCI_DMA
101 struct device *dma_dev;
Magnus Damm4bab9d42010-03-19 04:46:38 +0000102 unsigned int slave_tx;
103 unsigned int slave_rx;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900104 struct dma_async_tx_descriptor *desc_tx;
105 struct dma_async_tx_descriptor *desc_rx[2];
106 dma_cookie_t cookie_tx;
107 dma_cookie_t cookie_rx[2];
108 dma_cookie_t active_rx;
109 struct scatterlist sg_tx;
110 unsigned int sg_len_tx;
111 struct scatterlist sg_rx[2];
112 size_t buf_len_rx;
113 struct sh_dmae_slave param_tx;
114 struct sh_dmae_slave param_rx;
115 struct work_struct work_tx;
116 struct work_struct work_rx;
117 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000118 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900119#endif
Magnus Damme552de22009-01-21 15:13:42 +0000120};
121
122struct sh_sci_priv {
123 spinlock_t lock;
124 struct list_head ports;
Magnus Damme552de22009-01-21 15:13:42 +0000125 struct notifier_block clk_nb;
Paul Mundte108b2c2006-09-27 16:32:13 +0900126};
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100129static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Paul Mundte108b2c2006-09-27 16:32:13 +0900131#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
132
133static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct uart_driver sci_uart_driver;
135
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900136static inline struct sci_port *
137to_sci_port(struct uart_port *uart)
138{
139 return container_of(uart, struct sci_port, port);
140}
141
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900142#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900143
144#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900145static inline void handle_error(struct uart_port *port)
146{
147 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
149}
150
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900151static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned short status;
154 int c;
155
Paul Mundte108b2c2006-09-27 16:32:13 +0900156 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 status = sci_in(port, SCxSR);
158 if (status & SCxSR_ERRORS(port)) {
159 handle_error(port);
160 continue;
161 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500162 break;
163 } while (1);
164
165 if (!(status & SCxSR_RDxF(port)))
166 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900169
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900170 /* Dummy read */
171 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return c;
175}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900178static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned short status;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 do {
183 status = sci_in(port, SCxSR);
184 } while (!(status & SCxSR_TDxE(port)));
185
Paul Mundt272966c2008-11-13 17:46:06 +0900186 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000187 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900189#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Paul Mundt15c73aa2008-10-02 19:47:12 +0900191#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900192static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int ch = (port->mapbase - SMR0) >> 3;
195
196 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900197 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
198 h8300_sci_pins[ch].rx,
199 H8300_GPIO_INPUT);
200 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
201 h8300_sci_pins[ch].tx,
202 H8300_GPIO_OUTPUT);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* tx mark output*/
205 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
206}
Paul Mundtd5701642008-12-16 20:07:27 +0900207#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
208static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900209{
Paul Mundtd5701642008-12-16 20:07:27 +0900210 if (port->mapbase == 0xA4400000) {
211 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
212 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
213 } else if (port->mapbase == 0xA4410000)
214 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900215}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900216#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900217static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900219 unsigned short data;
220
221 if (cflag & CRTSCTS) {
222 /* enable RTS/CTS */
223 if (port->mapbase == 0xa4430000) { /* SCIF0 */
224 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900225 data = __raw_readw(PORT_PTCR);
226 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900227 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
228 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900229 data = __raw_readw(PORT_PVCR);
230 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900231 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900232 } else {
233 if (port->mapbase == 0xa4430000) { /* SCIF0 */
234 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900235 data = __raw_readw(PORT_PTCR);
236 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900237 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
238 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900239 data = __raw_readw(PORT_PVCR);
240 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900241 }
242 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900243}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800244#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900245/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900246static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800248 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Paul Mundtb7a76e42006-02-01 03:06:06 -0800250 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900251 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800252 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900253 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800254
Paul Mundtd5701642008-12-16 20:07:27 +0900255 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900257 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Clear out SCP7MD1,0, SCP4MD1,0,
259 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900260 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Paul Mundt32b53072009-12-24 14:52:43 +0900262 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900264 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Paul Mundt41504c32006-12-11 20:28:03 +0900267#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900268static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900269{
Magnus Damm346b7462008-04-23 21:25:29 +0900270 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900271
Magnus Damm346b7462008-04-23 21:25:29 +0900272 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900273 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900274 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900275 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900276 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900277
Paul Mundtd5701642008-12-16 20:07:27 +0900278 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900279 }
Paul Mundt41504c32006-12-11 20:28:03 +0900280}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900281#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900283 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900284 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900285 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900286 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900287static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
288{
289 if (!(cflag & CRTSCTS))
290 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
291}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900292#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900293static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
294{
295 if (!(cflag & CRTSCTS))
296 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
297}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800298#else
Paul Mundtd5701642008-12-16 20:07:27 +0900299static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
300{
301 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
Paul Mundte108b2c2006-09-27 16:32:13 +0900303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Paul Mundt32351a22007-03-12 14:38:59 +0900305#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
306 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900307 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
308 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900309static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900310{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900311 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900312}
313
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900314static int scif_txroom(struct uart_port *port)
315{
316 return SCIF_TXROOM_MAX - scif_txfill(port);
317}
318
319static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900320{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900321 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900322}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900323#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900324static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900325{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900326 if (port->mapbase == 0xffe00000 ||
327 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900328 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900329 return sci_in(port, SCTFDR) & 0xff;
330 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900331 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900332 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900333}
334
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900335static int scif_txroom(struct uart_port *port)
336{
337 if (port->mapbase == 0xffe00000 ||
338 port->mapbase == 0xffe08000)
339 /* SCIF0/1*/
340 return SCIF_TXROOM_MAX - scif_txfill(port);
341 else
342 /* SCIF2 */
343 return SCIF2_TXROOM_MAX - scif_txfill(port);
344}
345
346static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900347{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900348 if ((port->mapbase == 0xffe00000) ||
349 (port->mapbase == 0xffe08000)) {
350 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900351 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900352 } else {
353 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900354 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900355 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900356}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000357#elif defined(CONFIG_ARCH_SH7372)
358static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900359{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000360 if (port->type == PORT_SCIFA)
361 return sci_in(port, SCFDR) >> 8;
362 else
363 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900364}
365
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000366static int scif_txroom(struct uart_port *port)
367{
368 return port->fifosize - scif_txfill(port);
369}
370
371static int scif_rxfill(struct uart_port *port)
372{
373 if (port->type == PORT_SCIFA)
374 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
375 else
376 return sci_in(port, SCRFDR);
377}
Paul Mundte108b2c2006-09-27 16:32:13 +0900378#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900379static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900380{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900381 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900382}
383
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900384static int scif_txroom(struct uart_port *port)
385{
386 return SCIF_TXROOM_MAX - scif_txfill(port);
387}
388
389static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900390{
391 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900395static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900396{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900397 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900398}
399
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900400static int sci_txroom(struct uart_port *port)
401{
402 return !sci_txfill(port);
403}
404
405static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900406{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900407 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* ********************************************************************** *
411 * the interrupt related routines *
412 * ********************************************************************** */
413
414static void sci_transmit_chars(struct uart_port *port)
415{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700416 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned short status;
419 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900420 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 status = sci_in(port, SCxSR);
423 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900425 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900426 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900427 else
Paul Mundt8e698612009-06-24 19:44:32 +0900428 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return;
431 }
432
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900433 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900434 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900435 else
436 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 do {
439 unsigned char c;
440
441 if (port->x_char) {
442 c = port->x_char;
443 port->x_char = 0;
444 } else if (!uart_circ_empty(xmit) && !stopped) {
445 c = xmit->buf[xmit->tail];
446 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
447 } else {
448 break;
449 }
450
451 sci_out(port, SCxTDR, c);
452
453 port->icount.tx++;
454 } while (--count > 0);
455
456 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
457
458 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
459 uart_write_wakeup(port);
460 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100461 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ctrl = sci_in(port, SCSCR);
464
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900465 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sci_in(port, SCxSR); /* Dummy read */
467 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Paul Mundt8e698612009-06-24 19:44:32 +0900470 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473}
474
475/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900476#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David Howells7d12e782006-10-05 14:55:46 +0100478static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900480 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700481 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int i, count, copied = 0;
483 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800484 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 status = sci_in(port, SCxSR);
487 if (!(status & SCxSR_RDxF(port)))
488 return;
489
490 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900491 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900492 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900493 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900494 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800497 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* If for any reason we can't copy more data, we're done! */
500 if (count == 0)
501 break;
502
503 if (port->type == PORT_SCI) {
504 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900505 if (uart_handle_sysrq_char(port, c) ||
506 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900508 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900509 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900511 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 char c = sci_in(port, SCxRDR);
513 status = sci_in(port, SCxSR);
514#if defined(CONFIG_CPU_SH3)
515 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900516 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if ((c == 0) &&
518 (status & SCxSR_FER(port))) {
519 count--; i--;
520 continue;
521 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900524 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900525 sci_port->break_flag = 0;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (STEPFN(c)) {
528 count--; i--;
529 continue;
530 }
531 }
532#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100533 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 count--; i--;
535 continue;
536 }
537
538 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900539 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800540 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900541 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900542 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800543 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900544 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800545 } else
546 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900547
Alan Cox33f0f882006-01-09 20:54:13 -0800548 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 }
551
552 sci_in(port, SCxSR); /* dummy read */
553 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 copied += count;
556 port->icount.rx += count;
557 }
558
559 if (copied) {
560 /* Tell the rest of the system the news. New characters! */
561 tty_flip_buffer_push(tty);
562 } else {
563 sci_in(port, SCxSR); /* dummy read */
564 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
565 }
566}
567
568#define SCI_BREAK_JIFFIES (HZ/20)
569/* The sci generates interrupts during the break,
570 * 1 per millisecond or so during the break period, for 9600 baud.
571 * So dont bother disabling interrupts.
572 * But dont want more than 1 break event.
573 * Use a kernel timer to periodically poll the rx line until
574 * the break is finished.
575 */
576static void sci_schedule_break_timer(struct sci_port *port)
577{
578 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
579 add_timer(&port->break_timer);
580}
581/* Ensure that two consecutive samples find the break over. */
582static void sci_break_timer(unsigned long data)
583{
Paul Mundte108b2c2006-09-27 16:32:13 +0900584 struct sci_port *port = (struct sci_port *)data;
585
586 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900588 sci_schedule_break_timer(port);
589 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* break is over. */
591 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900592 sci_schedule_break_timer(port);
593 } else
594 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597static inline int sci_handle_errors(struct uart_port *port)
598{
599 int copied = 0;
600 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700601 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Paul Mundte108b2c2006-09-27 16:32:13 +0900603 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900605 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800606 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900607
608 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Paul Mundte108b2c2006-09-27 16:32:13 +0900611 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (sci_rxd_in(port) == 0) {
613 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900614 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900615
616 if (!sci_port->break_flag) {
617 sci_port->break_flag = 1;
618 sci_schedule_break_timer(sci_port);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900621 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900623
624 dev_dbg(port->dev, "BREAK detected\n");
625
Paul Mundte108b2c2006-09-27 16:32:13 +0900626 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900627 copied++;
628 }
629
Paul Mundte108b2c2006-09-27 16:32:13 +0900630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900632 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800633 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900634
635 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 }
638
Paul Mundte108b2c2006-09-27 16:32:13 +0900639 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900641 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
642 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900643
644 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Alan Cox33f0f882006-01-09 20:54:13 -0800647 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return copied;
651}
652
Paul Mundtd830fa42008-12-16 19:29:38 +0900653static inline int sci_handle_fifo_overrun(struct uart_port *port)
654{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700655 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900656 int copied = 0;
657
658 if (port->type != PORT_SCIF)
659 return 0;
660
661 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
662 sci_out(port, SCLSR, 0);
663
664 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
665 tty_flip_buffer_push(tty);
666
667 dev_notice(port->dev, "overrun error\n");
668 copied++;
669 }
670
671 return copied;
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static inline int sci_handle_breaks(struct uart_port *port)
675{
676 int copied = 0;
677 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700678 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000679 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900681 if (uart_handle_break(port))
682 return 0;
683
Paul Mundtb7a76e42006-02-01 03:06:06 -0800684 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#if defined(CONFIG_CPU_SH3)
686 /* Debounce break */
687 s->break_flag = 1;
688#endif
689 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900690 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800691 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900692
693 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Alan Cox33f0f882006-01-09 20:54:13 -0800696 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900698
Paul Mundtd830fa42008-12-16 19:29:38 +0900699 copied += sci_handle_fifo_overrun(port);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return copied;
702}
703
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900704static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900706#ifdef CONFIG_SERIAL_SH_SCI_DMA
707 struct uart_port *port = ptr;
708 struct sci_port *s = to_sci_port(port);
709
710 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900711 u16 scr = sci_in(port, SCSCR);
712 u16 ssr = sci_in(port, SCxSR);
713
714 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000715 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000716 disable_irq_nosync(irq);
717 scr |= 0x4000;
718 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900719 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000720 }
721 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900722 /* Clear current interrupt */
723 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000724 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
725 jiffies, s->rx_timeout);
726 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900727
728 return IRQ_HANDLED;
729 }
730#endif
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* I think sci_receive_chars has to be called irrespective
733 * of whether the I_IXOFF is set, otherwise, how is the interrupt
734 * to be disabled?
735 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900736 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 return IRQ_HANDLED;
739}
740
David Howells7d12e782006-10-05 14:55:46 +0100741static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900744 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Stuart Menefyfd78a762009-07-29 23:01:24 +0900746 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900748 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return IRQ_HANDLED;
751}
752
David Howells7d12e782006-10-05 14:55:46 +0100753static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct uart_port *port = ptr;
756
757 /* Handle errors */
758 if (port->type == PORT_SCI) {
759 if (sci_handle_errors(port)) {
760 /* discard character in rx buffer */
761 sci_in(port, SCxSR);
762 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
763 }
764 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900765 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100766 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
770
771 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100772 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return IRQ_HANDLED;
775}
776
David Howells7d12e782006-10-05 14:55:46 +0100777static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct uart_port *port = ptr;
780
781 /* Handle BREAKs */
782 sci_handle_breaks(port);
783 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
784
785 return IRQ_HANDLED;
786}
787
Paul Mundtf43dc232011-01-13 15:06:28 +0900788static inline unsigned long port_rx_irq_mask(struct uart_port *port)
789{
790 /*
791 * Not all ports (such as SCIFA) will support REIE. Rather than
792 * special-casing the port type, we check the port initialization
793 * IRQ enable mask to see whether the IRQ is desired at all. If
794 * it's unset, it's logically inferred that there's no point in
795 * testing for it.
796 */
797 return SCSCR_RIE | (to_sci_port(port)->scscr & SCSR_REIE);
798}
799
David Howells7d12e782006-10-05 14:55:46 +0100800static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Magnus Damm44e18e92009-07-03 08:39:34 +0000802 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900803 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900804 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900805 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900807 ssr_status = sci_in(port, SCxSR);
808 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900809 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900812 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900813 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900814 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900815
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900816 /*
817 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
818 * DR flags
819 */
820 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900821 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900822 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000825 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900826 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000829 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900830 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Michael Trimarchia8884e32008-10-31 16:10:23 +0900832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/*
836 * Here we define a transistion notifier so that we can update all of our
837 * ports' baud rate when the peripheral clock changes.
838 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900839static int sci_notifier(struct notifier_block *self,
840 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Magnus Damme552de22009-01-21 15:13:42 +0000842 struct sh_sci_priv *priv = container_of(self,
843 struct sh_sci_priv, clk_nb);
844 struct sci_port *sci_port;
845 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000848 (phase == CPUFREQ_RESUMECHANGE)) {
849 spin_lock_irqsave(&priv->lock, flags);
850 list_for_each_entry(sci_port, &priv->ports, node)
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900851 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
Magnus Damme552de22009-01-21 15:13:42 +0000852 spin_unlock_irqrestore(&priv->lock, flags);
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return NOTIFY_OK;
856}
Magnus Damm501b8252009-01-21 15:14:30 +0000857
858static void sci_clk_enable(struct uart_port *port)
859{
860 struct sci_port *sci_port = to_sci_port(port);
861
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900862 clk_enable(sci_port->iclk);
863 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
864 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000865}
866
867static void sci_clk_disable(struct uart_port *port)
868{
869 struct sci_port *sci_port = to_sci_port(port);
870
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900871 clk_disable(sci_port->fclk);
872 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000873}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875static int sci_request_irq(struct sci_port *port)
876{
877 int i;
David Howells7d12e782006-10-05 14:55:46 +0100878 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
880 sci_br_interrupt,
881 };
882 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
883 "SCI Transmit Data Empty", "SCI Break" };
884
885 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900886 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900888
889 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900890 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900891 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENODEV;
893 }
894 } else {
895 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900896 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900898
Paul Mundte108b2c2006-09-27 16:32:13 +0900899 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900900 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900901 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -ENODEV;
903 }
904 }
905 }
906
907 return 0;
908}
909
910static void sci_free_irq(struct sci_port *port)
911{
912 int i;
913
Paul Mundt762c69e2008-12-16 18:55:26 +0900914 if (port->irqs[0] == port->irqs[1])
915 free_irq(port->irqs[0], port);
916 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
918 if (!port->irqs[i])
919 continue;
920
921 free_irq(port->irqs[i], port);
922 }
923 }
924}
925
926static unsigned int sci_tx_empty(struct uart_port *port)
927{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000928 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900929 unsigned short in_tx_fifo = scif_txfill(port);
930
931 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
935{
936 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
937 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
938 /* If you have signals for DTR and DCD, please implement here. */
939}
940
941static unsigned int sci_get_mctrl(struct uart_port *port)
942{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900943 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 and CTS/RTS */
945
946 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
947}
948
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900949#ifdef CONFIG_SERIAL_SH_SCI_DMA
950static void sci_dma_tx_complete(void *arg)
951{
952 struct sci_port *s = arg;
953 struct uart_port *port = &s->port;
954 struct circ_buf *xmit = &port->state->xmit;
955 unsigned long flags;
956
957 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
958
959 spin_lock_irqsave(&port->lock, flags);
960
Magnus Dammf354a382010-03-19 04:47:01 +0000961 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900962 xmit->tail &= UART_XMIT_SIZE - 1;
963
Magnus Dammf354a382010-03-19 04:47:01 +0000964 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900965
966 async_tx_ack(s->desc_tx);
967 s->cookie_tx = -EINVAL;
968 s->desc_tx = NULL;
969
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900970 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
971 uart_write_wakeup(port);
972
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000973 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900974 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000975 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000976 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900977 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000978 }
979
980 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900981}
982
983/* Locking: called with port lock held */
984static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
985 size_t count)
986{
987 struct uart_port *port = &s->port;
988 int i, active, room;
989
990 room = tty_buffer_request_room(tty, count);
991
992 if (s->active_rx == s->cookie_rx[0]) {
993 active = 0;
994 } else if (s->active_rx == s->cookie_rx[1]) {
995 active = 1;
996 } else {
997 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
998 return 0;
999 }
1000
1001 if (room < count)
1002 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1003 count - room);
1004 if (!room)
1005 return room;
1006
1007 for (i = 0; i < room; i++)
1008 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1009 TTY_NORMAL);
1010
1011 port->icount.rx += room;
1012
1013 return room;
1014}
1015
1016static void sci_dma_rx_complete(void *arg)
1017{
1018 struct sci_port *s = arg;
1019 struct uart_port *port = &s->port;
1020 struct tty_struct *tty = port->state->port.tty;
1021 unsigned long flags;
1022 int count;
1023
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001024 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001025
1026 spin_lock_irqsave(&port->lock, flags);
1027
1028 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1029
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001030 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001031
1032 spin_unlock_irqrestore(&port->lock, flags);
1033
1034 if (count)
1035 tty_flip_buffer_push(tty);
1036
1037 schedule_work(&s->work_rx);
1038}
1039
1040static void sci_start_rx(struct uart_port *port);
1041static void sci_start_tx(struct uart_port *port);
1042
1043static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1044{
1045 struct dma_chan *chan = s->chan_rx;
1046 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001047
1048 s->chan_rx = NULL;
1049 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1050 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001051 if (sg_dma_address(&s->sg_rx[0]))
1052 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1053 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001054 if (enable_pio)
1055 sci_start_rx(port);
1056}
1057
1058static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1059{
1060 struct dma_chan *chan = s->chan_tx;
1061 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001062
1063 s->chan_tx = NULL;
1064 s->cookie_tx = -EINVAL;
1065 dma_release_channel(chan);
1066 if (enable_pio)
1067 sci_start_tx(port);
1068}
1069
1070static void sci_submit_rx(struct sci_port *s)
1071{
1072 struct dma_chan *chan = s->chan_rx;
1073 int i;
1074
1075 for (i = 0; i < 2; i++) {
1076 struct scatterlist *sg = &s->sg_rx[i];
1077 struct dma_async_tx_descriptor *desc;
1078
1079 desc = chan->device->device_prep_slave_sg(chan,
1080 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1081
1082 if (desc) {
1083 s->desc_rx[i] = desc;
1084 desc->callback = sci_dma_rx_complete;
1085 desc->callback_param = s;
1086 s->cookie_rx[i] = desc->tx_submit(desc);
1087 }
1088
1089 if (!desc || s->cookie_rx[i] < 0) {
1090 if (i) {
1091 async_tx_ack(s->desc_rx[0]);
1092 s->cookie_rx[0] = -EINVAL;
1093 }
1094 if (desc) {
1095 async_tx_ack(desc);
1096 s->cookie_rx[i] = -EINVAL;
1097 }
1098 dev_warn(s->port.dev,
1099 "failed to re-start DMA, using PIO\n");
1100 sci_rx_dma_release(s, true);
1101 return;
1102 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001103 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1104 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001105 }
1106
1107 s->active_rx = s->cookie_rx[0];
1108
1109 dma_async_issue_pending(chan);
1110}
1111
1112static void work_fn_rx(struct work_struct *work)
1113{
1114 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1115 struct uart_port *port = &s->port;
1116 struct dma_async_tx_descriptor *desc;
1117 int new;
1118
1119 if (s->active_rx == s->cookie_rx[0]) {
1120 new = 0;
1121 } else if (s->active_rx == s->cookie_rx[1]) {
1122 new = 1;
1123 } else {
1124 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1125 return;
1126 }
1127 desc = s->desc_rx[new];
1128
1129 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1130 DMA_SUCCESS) {
1131 /* Handle incomplete DMA receive */
1132 struct tty_struct *tty = port->state->port.tty;
1133 struct dma_chan *chan = s->chan_rx;
1134 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1135 async_tx);
1136 unsigned long flags;
1137 int count;
1138
Linus Walleij05827632010-05-17 16:30:42 -07001139 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001140 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1141 sh_desc->partial, sh_desc->cookie);
1142
1143 spin_lock_irqsave(&port->lock, flags);
1144 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1145 spin_unlock_irqrestore(&port->lock, flags);
1146
1147 if (count)
1148 tty_flip_buffer_push(tty);
1149
1150 sci_submit_rx(s);
1151
1152 return;
1153 }
1154
1155 s->cookie_rx[new] = desc->tx_submit(desc);
1156 if (s->cookie_rx[new] < 0) {
1157 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1158 sci_rx_dma_release(s, true);
1159 return;
1160 }
1161
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001162 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001163
1164 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1165 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001166}
1167
1168static void work_fn_tx(struct work_struct *work)
1169{
1170 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1171 struct dma_async_tx_descriptor *desc;
1172 struct dma_chan *chan = s->chan_tx;
1173 struct uart_port *port = &s->port;
1174 struct circ_buf *xmit = &port->state->xmit;
1175 struct scatterlist *sg = &s->sg_tx;
1176
1177 /*
1178 * DMA is idle now.
1179 * Port xmit buffer is already mapped, and it is one page... Just adjust
1180 * offsets and lengths. Since it is a circular buffer, we have to
1181 * transmit till the end, and then the rest. Take the port lock to get a
1182 * consistent xmit buffer state.
1183 */
1184 spin_lock_irq(&port->lock);
1185 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001186 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001187 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001188 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001189 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001190 spin_unlock_irq(&port->lock);
1191
Magnus Dammf354a382010-03-19 04:47:01 +00001192 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001193
1194 desc = chan->device->device_prep_slave_sg(chan,
1195 sg, s->sg_len_tx, DMA_TO_DEVICE,
1196 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1197 if (!desc) {
1198 /* switch to PIO */
1199 sci_tx_dma_release(s, true);
1200 return;
1201 }
1202
1203 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1204
1205 spin_lock_irq(&port->lock);
1206 s->desc_tx = desc;
1207 desc->callback = sci_dma_tx_complete;
1208 desc->callback_param = s;
1209 spin_unlock_irq(&port->lock);
1210 s->cookie_tx = desc->tx_submit(desc);
1211 if (s->cookie_tx < 0) {
1212 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1213 /* switch to PIO */
1214 sci_tx_dma_release(s, true);
1215 return;
1216 }
1217
1218 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1219 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1220
1221 dma_async_issue_pending(chan);
1222}
1223#endif
1224
Russell Kingb129a8c2005-08-31 10:12:14 +01001225static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001227 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001228 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001230#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001231 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001232 u16 new, scr = sci_in(port, SCSCR);
1233 if (s->chan_tx)
1234 new = scr | 0x8000;
1235 else
1236 new = scr & ~0x8000;
1237 if (new != scr)
1238 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001239 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001240
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001241 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1242 s->cookie_tx < 0)
1243 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001244#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001245
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001246 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001247 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1248 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001249 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Russell Kingb129a8c2005-08-31 10:12:14 +01001253static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned short ctrl;
1256
1257 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001259
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001260 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001261 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001262
Paul Mundt8e698612009-06-24 19:44:32 +09001263 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001268static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned short ctrl;
1271
Paul Mundtf43dc232011-01-13 15:06:28 +09001272 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1273
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001274 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001275 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void sci_stop_rx(struct uart_port *port)
1281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 unsigned short ctrl;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001285
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001286 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001287 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001288
1289 ctrl &= ~port_rx_irq_mask(port);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static void sci_enable_ms(struct uart_port *port)
1295{
1296 /* Nothing here yet .. */
1297}
1298
1299static void sci_break_ctl(struct uart_port *port, int break_state)
1300{
1301 /* Nothing here yet .. */
1302}
1303
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001304#ifdef CONFIG_SERIAL_SH_SCI_DMA
1305static bool filter(struct dma_chan *chan, void *slave)
1306{
1307 struct sh_dmae_slave *param = slave;
1308
1309 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1310 param->slave_id);
1311
1312 if (param->dma_dev == chan->device->dev) {
1313 chan->private = param;
1314 return true;
1315 } else {
1316 return false;
1317 }
1318}
1319
1320static void rx_timer_fn(unsigned long arg)
1321{
1322 struct sci_port *s = (struct sci_port *)arg;
1323 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001324 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001325
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001326 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001327 scr &= ~0x4000;
1328 enable_irq(s->irqs[1]);
1329 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001330 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001331 dev_dbg(port->dev, "DMA Rx timed out\n");
1332 schedule_work(&s->work_rx);
1333}
1334
1335static void sci_request_dma(struct uart_port *port)
1336{
1337 struct sci_port *s = to_sci_port(port);
1338 struct sh_dmae_slave *param;
1339 struct dma_chan *chan;
1340 dma_cap_mask_t mask;
1341 int nent;
1342
1343 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1344 port->line, s->dma_dev);
1345
1346 if (!s->dma_dev)
1347 return;
1348
1349 dma_cap_zero(mask);
1350 dma_cap_set(DMA_SLAVE, mask);
1351
1352 param = &s->param_tx;
1353
1354 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1355 param->slave_id = s->slave_tx;
1356 param->dma_dev = s->dma_dev;
1357
1358 s->cookie_tx = -EINVAL;
1359 chan = dma_request_channel(mask, filter, param);
1360 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1361 if (chan) {
1362 s->chan_tx = chan;
1363 sg_init_table(&s->sg_tx, 1);
1364 /* UART circular tx buffer is an aligned page. */
1365 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1366 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1367 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1368 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1369 if (!nent)
1370 sci_tx_dma_release(s, false);
1371 else
1372 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1373 sg_dma_len(&s->sg_tx),
1374 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1375
1376 s->sg_len_tx = nent;
1377
1378 INIT_WORK(&s->work_tx, work_fn_tx);
1379 }
1380
1381 param = &s->param_rx;
1382
1383 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1384 param->slave_id = s->slave_rx;
1385 param->dma_dev = s->dma_dev;
1386
1387 chan = dma_request_channel(mask, filter, param);
1388 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1389 if (chan) {
1390 dma_addr_t dma[2];
1391 void *buf[2];
1392 int i;
1393
1394 s->chan_rx = chan;
1395
1396 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1397 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1398 &dma[0], GFP_KERNEL);
1399
1400 if (!buf[0]) {
1401 dev_warn(port->dev,
1402 "failed to allocate dma buffer, using PIO\n");
1403 sci_rx_dma_release(s, true);
1404 return;
1405 }
1406
1407 buf[1] = buf[0] + s->buf_len_rx;
1408 dma[1] = dma[0] + s->buf_len_rx;
1409
1410 for (i = 0; i < 2; i++) {
1411 struct scatterlist *sg = &s->sg_rx[i];
1412
1413 sg_init_table(sg, 1);
1414 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1415 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001416 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001417 }
1418
1419 INIT_WORK(&s->work_rx, work_fn_rx);
1420 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1421
1422 sci_submit_rx(s);
1423 }
1424}
1425
1426static void sci_free_dma(struct uart_port *port)
1427{
1428 struct sci_port *s = to_sci_port(port);
1429
1430 if (!s->dma_dev)
1431 return;
1432
1433 if (s->chan_tx)
1434 sci_tx_dma_release(s, false);
1435 if (s->chan_rx)
1436 sci_rx_dma_release(s, false);
1437}
1438#endif
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static int sci_startup(struct uart_port *port)
1441{
Magnus Damma5660ad2009-01-21 15:14:38 +00001442 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001444 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1445
Paul Mundte108b2c2006-09-27 16:32:13 +09001446 if (s->enable)
1447 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 sci_request_irq(s);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001450#ifdef CONFIG_SERIAL_SH_SCI_DMA
1451 sci_request_dma(port);
1452#endif
Yoshinori Satod6569012005-10-14 15:59:12 -07001453 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001454 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 return 0;
1457}
1458
1459static void sci_shutdown(struct uart_port *port)
1460{
Magnus Damma5660ad2009-01-21 15:14:38 +00001461 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001463 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001466 sci_stop_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001467#ifdef CONFIG_SERIAL_SH_SCI_DMA
1468 sci_free_dma(port);
1469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 sci_free_irq(s);
1471
Paul Mundte108b2c2006-09-27 16:32:13 +09001472 if (s->disable)
1473 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Paul Mundt26c92f32009-06-24 18:23:52 +09001476static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1477 unsigned long freq)
1478{
1479 switch (algo_id) {
1480 case SCBRR_ALGO_1:
1481 return ((freq + 16 * bps) / (16 * bps) - 1);
1482 case SCBRR_ALGO_2:
1483 return ((freq + 16 * bps) / (32 * bps) - 1);
1484 case SCBRR_ALGO_3:
1485 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1486 case SCBRR_ALGO_4:
1487 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1488 case SCBRR_ALGO_5:
1489 return (((freq * 1000 / 32) / bps) - 1);
1490 }
1491
1492 /* Warn, but use a safe default */
1493 WARN_ON(1);
1494 return ((freq + 16 * bps) / (32 * bps) - 1);
1495}
1496
Alan Cox606d0992006-12-08 02:38:45 -08001497static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1498 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Paul Mundt00b9de92009-06-24 17:53:33 +09001500 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001501 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001502 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001503 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Magnus Damm154280f2009-12-22 03:37:28 +00001505 /*
1506 * earlyprintk comes here early on with port->uartclk set to zero.
1507 * the clock framework is not up and running at this point so here
1508 * we assume that 115200 is the maximum baud rate. please note that
1509 * the baud rate is not programmed during earlyprintk - it is assumed
1510 * that the previous boot loader has enabled required clocks and
1511 * setup the baud rate generator hardware for us already.
1512 */
1513 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1514
1515 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1516 if (likely(baud && port->uartclk))
Paul Mundt26c92f32009-06-24 18:23:52 +09001517 t = sci_scbrr_calc(s->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Paul Mundte108b2c2006-09-27 16:32:13 +09001519 do {
1520 status = sci_in(port, SCxSR);
1521 } while (!(status & SCxSR_TEND(port)));
1522
1523 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1524
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001525 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001526 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001527
1528 smr_val = sci_in(port, SCSMR) & 3;
1529 if ((termios->c_cflag & CSIZE) == CS7)
1530 smr_val |= 0x40;
1531 if (termios->c_cflag & PARENB)
1532 smr_val |= 0x20;
1533 if (termios->c_cflag & PARODD)
1534 smr_val |= 0x30;
1535 if (termios->c_cflag & CSTOPB)
1536 smr_val |= 0x08;
1537
1538 uart_update_timeout(port, termios->c_cflag, baud);
1539
1540 sci_out(port, SCSMR, smr_val);
1541
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001542 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1543 SCSCR_INIT(port));
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001546 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1548 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001549 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 sci_out(port, SCBRR, t);
1553 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1554 }
1555
Paul Mundtd5701642008-12-16 20:07:27 +09001556 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001557 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001558
Paul Mundt00b9de92009-06-24 17:53:33 +09001559 sci_out(port, SCSCR, s->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001561#ifdef CONFIG_SERIAL_SH_SCI_DMA
1562 /*
1563 * Calculate delay for 1.5 DMA buffers: see
1564 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1565 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1566 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1567 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1568 * sizes), but it has been found out experimentally, that this is not
1569 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1570 * as a minimum seem to work perfectly.
1571 */
1572 if (s->chan_rx) {
1573 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1574 port->fifosize / 2;
1575 dev_dbg(port->dev,
1576 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1577 s->rx_timeout * 1000 / HZ, port->timeout);
1578 if (s->rx_timeout < msecs_to_jiffies(20))
1579 s->rx_timeout = msecs_to_jiffies(20);
1580 }
1581#endif
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001584 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static const char *sci_type(struct uart_port *port)
1588{
1589 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001590 case PORT_IRDA:
1591 return "irda";
1592 case PORT_SCI:
1593 return "sci";
1594 case PORT_SCIF:
1595 return "scif";
1596 case PORT_SCIFA:
1597 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001598 case PORT_SCIFB:
1599 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
Paul Mundtfa439722008-09-04 18:53:58 +09001602 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605static void sci_release_port(struct uart_port *port)
1606{
1607 /* Nothing here yet .. */
1608}
1609
1610static int sci_request_port(struct uart_port *port)
1611{
1612 /* Nothing here yet .. */
1613 return 0;
1614}
1615
1616static void sci_config_port(struct uart_port *port, int flags)
1617{
Magnus Damma5660ad2009-01-21 15:14:38 +00001618 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 port->type = s->type;
1621
Magnus Damm08f8cb32009-01-21 15:14:22 +00001622 if (port->membase)
1623 return;
1624
1625 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001626 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001627
1628 if (IS_ERR(port->membase))
1629 dev_err(port->dev, "can't remap port#%d\n", port->line);
1630 } else {
1631 /*
1632 * For the simple (and majority of) cases where we don't
1633 * need to do any remapping, just cast the cookie
1634 * directly.
1635 */
1636 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1641{
Magnus Damma5660ad2009-01-21 15:14:38 +00001642 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Yinghai Lua62c4132008-08-19 20:49:55 -07001644 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -EINVAL;
1646 if (ser->baud_base < 2400)
1647 /* No paper tape reader for Mitch.. */
1648 return -EINVAL;
1649
1650 return 0;
1651}
1652
1653static struct uart_ops sci_uart_ops = {
1654 .tx_empty = sci_tx_empty,
1655 .set_mctrl = sci_set_mctrl,
1656 .get_mctrl = sci_get_mctrl,
1657 .start_tx = sci_start_tx,
1658 .stop_tx = sci_stop_tx,
1659 .stop_rx = sci_stop_rx,
1660 .enable_ms = sci_enable_ms,
1661 .break_ctl = sci_break_ctl,
1662 .startup = sci_startup,
1663 .shutdown = sci_shutdown,
1664 .set_termios = sci_set_termios,
1665 .type = sci_type,
1666 .release_port = sci_release_port,
1667 .request_port = sci_request_port,
1668 .config_port = sci_config_port,
1669 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001670#ifdef CONFIG_CONSOLE_POLL
1671 .poll_get_char = sci_poll_get_char,
1672 .poll_put_char = sci_poll_put_char,
1673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674};
1675
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001676static int __devinit sci_init_single(struct platform_device *dev,
1677 struct sci_port *sci_port,
1678 unsigned int index,
1679 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001680{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001681 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001682
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001683 port->ops = &sci_uart_ops;
1684 port->iotype = UPIO_MEM;
1685 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001686
1687 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001688 case PORT_SCIFB:
1689 port->fifosize = 256;
1690 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001691 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001692 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001693 break;
1694 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001695 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001696 break;
1697 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001698 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001699 break;
1700 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001701
1702 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001703 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1704 if (IS_ERR(sci_port->iclk)) {
1705 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1706 if (IS_ERR(sci_port->iclk)) {
1707 dev_err(&dev->dev, "can't get iclk\n");
1708 return PTR_ERR(sci_port->iclk);
1709 }
1710 }
1711
1712 /*
1713 * The function clock is optional, ignore it if we can't
1714 * find it.
1715 */
1716 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1717 if (IS_ERR(sci_port->fclk))
1718 sci_port->fclk = NULL;
1719
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001720 sci_port->enable = sci_clk_enable;
1721 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001722 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001723 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001724
Magnus Damm7ed7e072009-01-21 15:14:14 +00001725 sci_port->break_timer.data = (unsigned long)sci_port;
1726 sci_port->break_timer.function = sci_break_timer;
1727 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001728
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001729 port->mapbase = p->mapbase;
1730 port->membase = p->membase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001731
Paul Mundtf43dc232011-01-13 15:06:28 +09001732 port->irq = p->irqs[SCIx_TXI_IRQ];
1733 port->flags = p->flags;
1734 sci_port->type = port->type = p->type;
Paul Mundt00b9de92009-06-24 17:53:33 +09001735 sci_port->scscr = p->scscr;
Paul Mundtf43dc232011-01-13 15:06:28 +09001736 sci_port->scbrr_algo_id = p->scbrr_algo_id;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001737
1738#ifdef CONFIG_SERIAL_SH_SCI_DMA
1739 sci_port->dma_dev = p->dma_dev;
1740 sci_port->slave_tx = p->dma_slave_tx;
1741 sci_port->slave_rx = p->dma_slave_rx;
1742
1743 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1744 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1745#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001746
1747 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001748 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001752static struct tty_driver *serial_console_device(struct console *co, int *index)
1753{
1754 struct uart_driver *p = &sci_uart_driver;
1755 *index = co->index;
1756 return p->tty_driver;
1757}
1758
1759static void serial_console_putchar(struct uart_port *port, int ch)
1760{
1761 sci_poll_put_char(port, ch);
1762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
1765 * Print a string to the serial port trying not to disturb
1766 * any possible real use of the port...
1767 */
1768static void serial_console_write(struct console *co, const char *s,
1769 unsigned count)
1770{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001771 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001772 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001773 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001774
Magnus Damm501b8252009-01-21 15:14:30 +00001775 if (sci_port->enable)
1776 sci_port->enable(port);
1777
1778 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001779
1780 /* wait until fifo is empty and last bit has been transmitted */
1781 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1782 while ((sci_in(port, SCxSR) & bits) != bits)
1783 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001784
Magnus Damm345e5a72009-11-05 14:34:57 +00001785 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001786 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001789static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001791 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct uart_port *port;
1793 int baud = 115200;
1794 int bits = 8;
1795 int parity = 'n';
1796 int flow = 'n';
1797 int ret;
1798
Paul Mundte108b2c2006-09-27 16:32:13 +09001799 /*
1800 * Check whether an invalid uart number has been specified, and
1801 * if so, search for the first available port that does have
1802 * console support.
1803 */
1804 if (co->index >= SCI_NPORTS)
1805 co->index = 0;
1806
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001807 if (co->data) {
1808 port = co->data;
1809 sci_port = to_sci_port(port);
1810 } else {
1811 sci_port = &sci_ports[co->index];
1812 port = &sci_port->port;
1813 co->data = port;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001817 * Also need to check port->type, we don't actually have any
1818 * UPIO_PORT ports, but uart_report_port() handily misreports
1819 * it anyways if we don't have a port available by the time this is
1820 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001822 if (!port->type)
1823 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001824
Magnus Damm08f8cb32009-01-21 15:14:22 +00001825 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001826
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001827 if (sci_port->enable)
1828 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (options)
1831 uart_parse_options(options, &baud, &parity, &bits, &flow);
1832
1833 ret = uart_set_options(port, co, baud, parity, bits, flow);
1834#if defined(__H8300H__) || defined(__H8300S__)
1835 /* disable rx interrupt */
1836 if (ret == 0)
1837 sci_stop_rx(port);
1838#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001839 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return ret;
1841}
1842
1843static struct console serial_console = {
1844 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001845 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 .write = serial_console_write,
1847 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001848 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850};
1851
1852static int __init sci_console_init(void)
1853{
1854 register_console(&serial_console);
1855 return 0;
1856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001858
1859static struct sci_port early_serial_port;
1860static struct console early_serial_console = {
1861 .name = "early_ttySC",
1862 .write = serial_console_write,
1863 .flags = CON_PRINTBUFFER,
1864};
1865static char early_serial_buf[32];
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1868
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001869#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001870#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001872#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873#endif
1874
1875static char banner[] __initdata =
1876 KERN_INFO "SuperH SCI(F) driver initialized\n";
1877
1878static struct uart_driver sci_uart_driver = {
1879 .owner = THIS_MODULE,
1880 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 .dev_name = "ttySC",
1882 .major = SCI_MAJOR,
1883 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001884 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .cons = SCI_CONSOLE,
1886};
1887
Magnus Damme552de22009-01-21 15:13:42 +00001888
Paul Mundt54507f62009-05-08 23:48:33 +09001889static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001890{
1891 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1892 struct sci_port *p;
1893 unsigned long flags;
1894
Magnus Damme552de22009-01-21 15:13:42 +00001895 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001896
1897 spin_lock_irqsave(&priv->lock, flags);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001898 list_for_each_entry(p, &priv->ports, node) {
Magnus Damme552de22009-01-21 15:13:42 +00001899 uart_remove_one_port(&sci_uart_driver, &p->port);
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001900 clk_put(p->iclk);
1901 clk_put(p->fclk);
1902 }
Magnus Damme552de22009-01-21 15:13:42 +00001903 spin_unlock_irqrestore(&priv->lock, flags);
1904
1905 kfree(priv);
1906 return 0;
1907}
1908
Magnus Damm0ee70712009-01-21 15:13:50 +00001909static int __devinit sci_probe_single(struct platform_device *dev,
1910 unsigned int index,
1911 struct plat_sci_port *p,
1912 struct sci_port *sciport)
1913{
1914 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1915 unsigned long flags;
1916 int ret;
1917
1918 /* Sanity check */
1919 if (unlikely(index >= SCI_NPORTS)) {
1920 dev_notice(&dev->dev, "Attempting to register port "
1921 "%d when only %d are available.\n",
1922 index+1, SCI_NPORTS);
1923 dev_notice(&dev->dev, "Consider bumping "
1924 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1925 return 0;
1926 }
1927
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001928 ret = sci_init_single(dev, sciport, index, p);
1929 if (ret)
1930 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001931
1932 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001933 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001934 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001935
1936 INIT_LIST_HEAD(&sciport->node);
1937
1938 spin_lock_irqsave(&priv->lock, flags);
1939 list_add(&sciport->node, &priv->ports);
1940 spin_unlock_irqrestore(&priv->lock, flags);
1941
1942 return 0;
1943}
1944
Paul Mundte108b2c2006-09-27 16:32:13 +09001945/*
1946 * Register a set of serial devices attached to a platform device. The
1947 * list is terminated with a zero flags entry, which means we expect
1948 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1949 * remapping (such as sh64) should also set UPF_IOREMAP.
1950 */
1951static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Paul Mundte108b2c2006-09-27 16:32:13 +09001953 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001954 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001955 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001956
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001957#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1958 if (is_early_platform_device(dev)) {
1959 if (dev->id == -1)
1960 return -ENOTSUPP;
1961 early_serial_console.index = dev->id;
1962 early_serial_console.data = &early_serial_port.port;
1963 sci_init_single(NULL, &early_serial_port, dev->id, p);
1964 serial_console_setup(&early_serial_console, early_serial_buf);
1965 if (!strstr(early_serial_buf, "keep"))
1966 early_serial_console.flags |= CON_BOOT;
1967 register_console(&early_serial_console);
1968 return 0;
1969 }
1970#endif
1971
Magnus Damme552de22009-01-21 15:13:42 +00001972 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1973 if (!priv)
1974 return -ENOMEM;
1975
1976 INIT_LIST_HEAD(&priv->ports);
1977 spin_lock_init(&priv->lock);
1978 platform_set_drvdata(dev, priv);
1979
Magnus Damme552de22009-01-21 15:13:42 +00001980 priv->clk_nb.notifier_call = sci_notifier;
1981 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Magnus Damm0ee70712009-01-21 15:13:50 +00001983 if (dev->id != -1) {
1984 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1985 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001986 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001987 } else {
1988 for (i = 0; p && p->flags != 0; p++, i++) {
1989 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1990 if (ret)
1991 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001992 }
Magnus Damme552de22009-01-21 15:13:42 +00001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995#ifdef CONFIG_SH_STANDARD_BIOS
1996 sh_bios_gdb_detach();
1997#endif
1998
Paul Mundte108b2c2006-09-27 16:32:13 +09001999 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09002000
2001err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00002002 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09002003 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09002004}
2005
Paul Mundt6daa79b2009-06-15 07:07:38 +09002006static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002007{
Paul Mundt6daa79b2009-06-15 07:07:38 +09002008 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002009 struct sci_port *p;
2010 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09002011
Magnus Damme552de22009-01-21 15:13:42 +00002012 spin_lock_irqsave(&priv->lock, flags);
2013 list_for_each_entry(p, &priv->ports, node)
2014 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00002015 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09002016
2017 return 0;
2018}
2019
Paul Mundt6daa79b2009-06-15 07:07:38 +09002020static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09002021{
Paul Mundt6daa79b2009-06-15 07:07:38 +09002022 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00002023 struct sci_port *p;
2024 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09002025
Magnus Damme552de22009-01-21 15:13:42 +00002026 spin_lock_irqsave(&priv->lock, flags);
2027 list_for_each_entry(p, &priv->ports, node)
2028 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00002029 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09002030
2031 return 0;
2032}
2033
Alexey Dobriyan47145212009-12-14 18:00:08 -08002034static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09002035 .suspend = sci_suspend,
2036 .resume = sci_resume,
2037};
2038
Paul Mundte108b2c2006-09-27 16:32:13 +09002039static struct platform_driver sci_driver = {
2040 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01002041 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09002042 .driver = {
2043 .name = "sh-sci",
2044 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09002045 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09002046 },
2047};
2048
2049static int __init sci_init(void)
2050{
2051 int ret;
2052
2053 printk(banner);
2054
Paul Mundte108b2c2006-09-27 16:32:13 +09002055 ret = uart_register_driver(&sci_uart_driver);
2056 if (likely(ret == 0)) {
2057 ret = platform_driver_register(&sci_driver);
2058 if (unlikely(ret))
2059 uart_unregister_driver(&sci_uart_driver);
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return ret;
2063}
2064
2065static void __exit sci_exit(void)
2066{
Paul Mundte108b2c2006-09-27 16:32:13 +09002067 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 uart_unregister_driver(&sci_uart_driver);
2069}
2070
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002071#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2072early_platform_init_buffer("earlyprintk", &sci_driver,
2073 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075module_init(sci_init);
2076module_exit(sci_exit);
2077
Paul Mundte108b2c2006-09-27 16:32:13 +09002078MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002079MODULE_ALIAS("platform:sh-sci");