blob: 3fd1577a162fa829aaec90616697eda762b698df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundtf43dc232011-01-13 15:06:28 +09006 * Copyright (C) 2002 - 2011 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090050#include <linux/dmaengine.h>
51#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090053
54#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090055#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yoshinori Sato168f3622009-04-28 04:40:15 +000058#ifdef CONFIG_H8300
59#include <asm/gpio.h>
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "sh-sci.h"
63
Paul Mundte108b2c2006-09-27 16:32:13 +090064struct sci_port {
65 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Paul Mundtce6738b2011-01-19 15:24:40 +090067 /* Platform configuration */
68 struct plat_sci_port *cfg;
Paul Mundte108b2c2006-09-27 16:32:13 +090069
Paul Mundte108b2c2006-09-27 16:32:13 +090070 /* Port enable callback */
71 void (*enable)(struct uart_port *port);
72
73 /* Port disable callback */
74 void (*disable)(struct uart_port *port);
75
76 /* Break timer */
77 struct timer_list break_timer;
78 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090079
Magnus Damm501b8252009-01-21 15:14:30 +000080 /* Interface clock */
81 struct clk *iclk;
Paul Mundtc7ed1ab2010-03-10 18:35:14 +090082 /* Function clock */
83 struct clk *fclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090084
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090085 struct dma_chan *chan_tx;
86 struct dma_chan *chan_rx;
Paul Mundtf43dc232011-01-13 15:06:28 +090087
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090088#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090089 struct dma_async_tx_descriptor *desc_tx;
90 struct dma_async_tx_descriptor *desc_rx[2];
91 dma_cookie_t cookie_tx;
92 dma_cookie_t cookie_rx[2];
93 dma_cookie_t active_rx;
94 struct scatterlist sg_tx;
95 unsigned int sg_len_tx;
96 struct scatterlist sg_rx[2];
97 size_t buf_len_rx;
98 struct sh_dmae_slave param_tx;
99 struct sh_dmae_slave param_rx;
100 struct work_struct work_tx;
101 struct work_struct work_rx;
102 struct timer_list rx_timer;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000103 unsigned int rx_timeout;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900104#endif
Magnus Damme552de22009-01-21 15:13:42 +0000105
Paul Mundtd535a232011-01-19 17:19:35 +0900106 struct notifier_block freq_transition;
Paul Mundte108b2c2006-09-27 16:32:13 +0900107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Function prototypes */
Paul Mundtd535a232011-01-19 17:19:35 +0900110static void sci_start_tx(struct uart_port *port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100111static void sci_stop_tx(struct uart_port *port);
Paul Mundtd535a232011-01-19 17:19:35 +0900112static void sci_start_rx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Paul Mundte108b2c2006-09-27 16:32:13 +0900114#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
115
116static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static struct uart_driver sci_uart_driver;
118
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900119static inline struct sci_port *
120to_sci_port(struct uart_port *uart)
121{
122 return container_of(uart, struct sci_port, port);
123}
124
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900125#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900126
127#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900128static inline void handle_error(struct uart_port *port)
129{
130 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
132}
133
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900134static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned short status;
137 int c;
138
Paul Mundte108b2c2006-09-27 16:32:13 +0900139 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 status = sci_in(port, SCxSR);
141 if (status & SCxSR_ERRORS(port)) {
142 handle_error(port);
143 continue;
144 }
Jason Wessel3f255eb2010-05-20 21:04:23 -0500145 break;
146 } while (1);
147
148 if (!(status & SCxSR_RDxF(port)))
149 return NO_POLL_CHAR;
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900152
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900153 /* Dummy read */
154 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return c;
158}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900161static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned short status;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 do {
166 status = sci_in(port, SCxSR);
167 } while (!(status & SCxSR_TDxE(port)));
168
Paul Mundt272966c2008-11-13 17:46:06 +0900169 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000170 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900172#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Paul Mundt15c73aa2008-10-02 19:47:12 +0900174#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900175static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int ch = (port->mapbase - SMR0) >> 3;
178
179 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900180 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
181 h8300_sci_pins[ch].rx,
182 H8300_GPIO_INPUT);
183 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
184 h8300_sci_pins[ch].tx,
185 H8300_GPIO_OUTPUT);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* tx mark output*/
188 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
189}
Paul Mundtd5701642008-12-16 20:07:27 +0900190#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
191static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900192{
Paul Mundtd5701642008-12-16 20:07:27 +0900193 if (port->mapbase == 0xA4400000) {
194 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
195 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
196 } else if (port->mapbase == 0xA4410000)
197 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900198}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900199#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900200static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900201{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900202 unsigned short data;
203
204 if (cflag & CRTSCTS) {
205 /* enable RTS/CTS */
206 if (port->mapbase == 0xa4430000) { /* SCIF0 */
207 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900208 data = __raw_readw(PORT_PTCR);
209 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900210 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
211 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900212 data = __raw_readw(PORT_PVCR);
213 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900214 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900215 } else {
216 if (port->mapbase == 0xa4430000) { /* SCIF0 */
217 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900218 data = __raw_readw(PORT_PTCR);
219 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900220 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
221 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900222 data = __raw_readw(PORT_PVCR);
223 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900224 }
225 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900226}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800227#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900228/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900229static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800231 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Paul Mundtb7a76e42006-02-01 03:06:06 -0800233 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900234 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800235 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900236 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800237
Paul Mundtd5701642008-12-16 20:07:27 +0900238 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900240 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Clear out SCP7MD1,0, SCP4MD1,0,
242 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900243 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Paul Mundt32b53072009-12-24 14:52:43 +0900245 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900247 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Paul Mundt41504c32006-12-11 20:28:03 +0900250#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900251static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900252{
Magnus Damm346b7462008-04-23 21:25:29 +0900253 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900254
Magnus Damm346b7462008-04-23 21:25:29 +0900255 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900256 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900257 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900258 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900259 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900260
Paul Mundtd5701642008-12-16 20:07:27 +0900261 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900262 }
Paul Mundt41504c32006-12-11 20:28:03 +0900263}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900264#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
265 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900266 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900267 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900268 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900269 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900270static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
271{
272 if (!(cflag & CRTSCTS))
273 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
274}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900275#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900276static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
277{
278 if (!(cflag & CRTSCTS))
279 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
280}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800281#else
Paul Mundtd5701642008-12-16 20:07:27 +0900282static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
283{
284 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
Paul Mundte108b2c2006-09-27 16:32:13 +0900286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Paul Mundt32351a22007-03-12 14:38:59 +0900288#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
289 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900290 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
291 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900292static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900293{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900294 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900295}
296
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900297static int scif_txroom(struct uart_port *port)
298{
299 return SCIF_TXROOM_MAX - scif_txfill(port);
300}
301
302static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900303{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900304 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900305}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900306#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900307static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900308{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900309 if (port->mapbase == 0xffe00000 ||
310 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900311 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900312 return sci_in(port, SCTFDR) & 0xff;
313 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900314 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900315 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900316}
317
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900318static int scif_txroom(struct uart_port *port)
319{
320 if (port->mapbase == 0xffe00000 ||
321 port->mapbase == 0xffe08000)
322 /* SCIF0/1*/
323 return SCIF_TXROOM_MAX - scif_txfill(port);
324 else
325 /* SCIF2 */
326 return SCIF2_TXROOM_MAX - scif_txfill(port);
327}
328
329static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900330{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900331 if ((port->mapbase == 0xffe00000) ||
332 (port->mapbase == 0xffe08000)) {
333 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900334 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900335 } else {
336 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900337 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900338 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900339}
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000340#elif defined(CONFIG_ARCH_SH7372)
341static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900342{
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000343 if (port->type == PORT_SCIFA)
344 return sci_in(port, SCFDR) >> 8;
345 else
346 return sci_in(port, SCTFDR);
Paul Mundte108b2c2006-09-27 16:32:13 +0900347}
348
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000349static int scif_txroom(struct uart_port *port)
350{
351 return port->fifosize - scif_txfill(port);
352}
353
354static int scif_rxfill(struct uart_port *port)
355{
356 if (port->type == PORT_SCIFA)
357 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
358 else
359 return sci_in(port, SCRFDR);
360}
Paul Mundte108b2c2006-09-27 16:32:13 +0900361#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900362static int scif_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, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900365}
366
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900367static int scif_txroom(struct uart_port *port)
368{
369 return SCIF_TXROOM_MAX - scif_txfill(port);
370}
371
372static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900373{
374 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900378static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900379{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900380 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900381}
382
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900383static int sci_txroom(struct uart_port *port)
384{
385 return !sci_txfill(port);
386}
387
388static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900389{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900390 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/* ********************************************************************** *
394 * the interrupt related routines *
395 * ********************************************************************** */
396
397static void sci_transmit_chars(struct uart_port *port)
398{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700399 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned short status;
402 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900403 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 status = sci_in(port, SCxSR);
406 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900408 if (uart_circ_empty(xmit))
Paul Mundt8e698612009-06-24 19:44:32 +0900409 ctrl &= ~SCSCR_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900410 else
Paul Mundt8e698612009-06-24 19:44:32 +0900411 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return;
414 }
415
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900416 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900417 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900418 else
419 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 do {
422 unsigned char c;
423
424 if (port->x_char) {
425 c = port->x_char;
426 port->x_char = 0;
427 } else if (!uart_circ_empty(xmit) && !stopped) {
428 c = xmit->buf[xmit->tail];
429 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
430 } else {
431 break;
432 }
433
434 sci_out(port, SCxTDR, c);
435
436 port->icount.tx++;
437 } while (--count > 0);
438
439 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
440
441 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
442 uart_write_wakeup(port);
443 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100444 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ctrl = sci_in(port, SCSCR);
447
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900448 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sci_in(port, SCxSR); /* Dummy read */
450 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Paul Mundt8e698612009-06-24 19:44:32 +0900453 ctrl |= SCSCR_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456}
457
458/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900459#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
David Howells7d12e782006-10-05 14:55:46 +0100461static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900463 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700464 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int i, count, copied = 0;
466 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800467 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 status = sci_in(port, SCxSR);
470 if (!(status & SCxSR_RDxF(port)))
471 return;
472
473 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900474 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900475 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900476 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900477 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800480 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* If for any reason we can't copy more data, we're done! */
483 if (count == 0)
484 break;
485
486 if (port->type == PORT_SCI) {
487 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900488 if (uart_handle_sysrq_char(port, c) ||
489 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900491 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900492 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900494 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 char c = sci_in(port, SCxRDR);
496 status = sci_in(port, SCxSR);
497#if defined(CONFIG_CPU_SH3)
498 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900499 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((c == 0) &&
501 (status & SCxSR_FER(port))) {
502 count--; i--;
503 continue;
504 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900507 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900508 sci_port->break_flag = 0;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (STEPFN(c)) {
511 count--; i--;
512 continue;
513 }
514 }
515#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100516 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 count--; i--;
518 continue;
519 }
520
521 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900522 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800523 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900524 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900525 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800526 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900527 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800528 } else
529 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900530
Alan Cox33f0f882006-01-09 20:54:13 -0800531 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
534
535 sci_in(port, SCxSR); /* dummy read */
536 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 copied += count;
539 port->icount.rx += count;
540 }
541
542 if (copied) {
543 /* Tell the rest of the system the news. New characters! */
544 tty_flip_buffer_push(tty);
545 } else {
546 sci_in(port, SCxSR); /* dummy read */
547 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
548 }
549}
550
551#define SCI_BREAK_JIFFIES (HZ/20)
552/* The sci generates interrupts during the break,
553 * 1 per millisecond or so during the break period, for 9600 baud.
554 * So dont bother disabling interrupts.
555 * But dont want more than 1 break event.
556 * Use a kernel timer to periodically poll the rx line until
557 * the break is finished.
558 */
559static void sci_schedule_break_timer(struct sci_port *port)
560{
561 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
562 add_timer(&port->break_timer);
563}
564/* Ensure that two consecutive samples find the break over. */
565static void sci_break_timer(unsigned long data)
566{
Paul Mundte108b2c2006-09-27 16:32:13 +0900567 struct sci_port *port = (struct sci_port *)data;
568
569 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900571 sci_schedule_break_timer(port);
572 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* break is over. */
574 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900575 sci_schedule_break_timer(port);
576 } else
577 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580static inline int sci_handle_errors(struct uart_port *port)
581{
582 int copied = 0;
583 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700584 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Paul Mundte108b2c2006-09-27 16:32:13 +0900586 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900588 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800589 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900590
591 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Paul Mundte108b2c2006-09-27 16:32:13 +0900594 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (sci_rxd_in(port) == 0) {
596 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900597 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900598
599 if (!sci_port->break_flag) {
600 sci_port->break_flag = 1;
601 sci_schedule_break_timer(sci_port);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900604 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900606
607 dev_dbg(port->dev, "BREAK detected\n");
608
Paul Mundte108b2c2006-09-27 16:32:13 +0900609 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900610 copied++;
611 }
612
Paul Mundte108b2c2006-09-27 16:32:13 +0900613 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900615 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800616 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900617
618 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
621
Paul Mundte108b2c2006-09-27 16:32:13 +0900622 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900624 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
625 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900626
627 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
Alan Cox33f0f882006-01-09 20:54:13 -0800630 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return copied;
634}
635
Paul Mundtd830fa42008-12-16 19:29:38 +0900636static inline int sci_handle_fifo_overrun(struct uart_port *port)
637{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700638 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900639 int copied = 0;
640
641 if (port->type != PORT_SCIF)
642 return 0;
643
644 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
645 sci_out(port, SCLSR, 0);
646
647 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
648 tty_flip_buffer_push(tty);
649
650 dev_notice(port->dev, "overrun error\n");
651 copied++;
652 }
653
654 return copied;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static inline int sci_handle_breaks(struct uart_port *port)
658{
659 int copied = 0;
660 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700661 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000662 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900664 if (uart_handle_break(port))
665 return 0;
666
Paul Mundtb7a76e42006-02-01 03:06:06 -0800667 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#if defined(CONFIG_CPU_SH3)
669 /* Debounce break */
670 s->break_flag = 1;
671#endif
672 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900673 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800674 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900675
676 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Alan Cox33f0f882006-01-09 20:54:13 -0800679 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900681
Paul Mundtd830fa42008-12-16 19:29:38 +0900682 copied += sci_handle_fifo_overrun(port);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return copied;
685}
686
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900687static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900689#ifdef CONFIG_SERIAL_SH_SCI_DMA
690 struct uart_port *port = ptr;
691 struct sci_port *s = to_sci_port(port);
692
693 if (s->chan_rx) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900694 u16 scr = sci_in(port, SCSCR);
695 u16 ssr = sci_in(port, SCxSR);
696
697 /* Disable future Rx interrupts */
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000698 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000699 disable_irq_nosync(irq);
700 scr |= 0x4000;
701 } else {
Paul Mundtf43dc232011-01-13 15:06:28 +0900702 scr &= ~SCSCR_RIE;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000703 }
704 sci_out(port, SCSCR, scr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900705 /* Clear current interrupt */
706 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000707 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
708 jiffies, s->rx_timeout);
709 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900710
711 return IRQ_HANDLED;
712 }
713#endif
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* I think sci_receive_chars has to be called irrespective
716 * of whether the I_IXOFF is set, otherwise, how is the interrupt
717 * to be disabled?
718 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900719 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return IRQ_HANDLED;
722}
723
David Howells7d12e782006-10-05 14:55:46 +0100724static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900727 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Stuart Menefyfd78a762009-07-29 23:01:24 +0900729 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900731 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 return IRQ_HANDLED;
734}
735
David Howells7d12e782006-10-05 14:55:46 +0100736static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct uart_port *port = ptr;
739
740 /* Handle errors */
741 if (port->type == PORT_SCI) {
742 if (sci_handle_errors(port)) {
743 /* discard character in rx buffer */
744 sci_in(port, SCxSR);
745 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
746 }
747 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900748 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100749 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
752 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
753
754 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100755 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 return IRQ_HANDLED;
758}
759
David Howells7d12e782006-10-05 14:55:46 +0100760static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct uart_port *port = ptr;
763
764 /* Handle BREAKs */
765 sci_handle_breaks(port);
766 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
767
768 return IRQ_HANDLED;
769}
770
Paul Mundtf43dc232011-01-13 15:06:28 +0900771static inline unsigned long port_rx_irq_mask(struct uart_port *port)
772{
773 /*
774 * Not all ports (such as SCIFA) will support REIE. Rather than
775 * special-casing the port type, we check the port initialization
776 * IRQ enable mask to see whether the IRQ is desired at all. If
777 * it's unset, it's logically inferred that there's no point in
778 * testing for it.
779 */
Paul Mundtce6738b2011-01-19 15:24:40 +0900780 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
Paul Mundtf43dc232011-01-13 15:06:28 +0900781}
782
David Howells7d12e782006-10-05 14:55:46 +0100783static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Magnus Damm44e18e92009-07-03 08:39:34 +0000785 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900786 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900787 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900788 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900790 ssr_status = sci_in(port, SCxSR);
791 scr_status = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900792 err_enabled = scr_status & port_rx_irq_mask(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* Tx Interrupt */
Paul Mundtf43dc232011-01-13 15:06:28 +0900795 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900796 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900797 ret = sci_tx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900798
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900799 /*
800 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
801 * DR flags
802 */
803 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
Paul Mundtf43dc232011-01-13 15:06:28 +0900804 (scr_status & SCSCR_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900805 ret = sci_rx_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000808 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900809 ret = sci_er_interrupt(irq, ptr);
Paul Mundtf43dc232011-01-13 15:06:28 +0900810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000812 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900813 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Michael Trimarchia8884e32008-10-31 16:10:23 +0900815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * Here we define a transistion notifier so that we can update all of our
820 * ports' baud rate when the peripheral clock changes.
821 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900822static int sci_notifier(struct notifier_block *self,
823 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Magnus Damme552de22009-01-21 15:13:42 +0000825 struct sci_port *sci_port;
826 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Paul Mundtd535a232011-01-19 17:19:35 +0900828 sci_port = container_of(self, struct sci_port, freq_transition);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000831 (phase == CPUFREQ_RESUMECHANGE)) {
Paul Mundtd535a232011-01-19 17:19:35 +0900832 struct uart_port *port = &sci_port->port;
833 spin_lock_irqsave(&port->lock, flags);
834 port->uartclk = clk_get_rate(sci_port->iclk);
835 spin_unlock_irqrestore(&port->lock, flags);
Magnus Damme552de22009-01-21 15:13:42 +0000836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return NOTIFY_OK;
839}
Magnus Damm501b8252009-01-21 15:14:30 +0000840
841static void sci_clk_enable(struct uart_port *port)
842{
843 struct sci_port *sci_port = to_sci_port(port);
844
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900845 clk_enable(sci_port->iclk);
846 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
847 clk_enable(sci_port->fclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000848}
849
850static void sci_clk_disable(struct uart_port *port)
851{
852 struct sci_port *sci_port = to_sci_port(port);
853
Paul Mundtc7ed1ab2010-03-10 18:35:14 +0900854 clk_disable(sci_port->fclk);
855 clk_disable(sci_port->iclk);
Magnus Damm501b8252009-01-21 15:14:30 +0000856}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858static int sci_request_irq(struct sci_port *port)
859{
860 int i;
David Howells7d12e782006-10-05 14:55:46 +0100861 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
863 sci_br_interrupt,
864 };
865 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
866 "SCI Transmit Data Empty", "SCI Break" };
867
Paul Mundtce6738b2011-01-19 15:24:40 +0900868 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
869 if (unlikely(!port->cfg->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900871
Paul Mundtce6738b2011-01-19 15:24:40 +0900872 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900873 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900874 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -ENODEV;
876 }
877 } else {
878 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundtce6738b2011-01-19 15:24:40 +0900879 if (unlikely(!port->cfg->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900881
Paul Mundtce6738b2011-01-19 15:24:40 +0900882 if (request_irq(port->cfg->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900883 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900884 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -ENODEV;
886 }
887 }
888 }
889
890 return 0;
891}
892
893static void sci_free_irq(struct sci_port *port)
894{
895 int i;
896
Paul Mundtce6738b2011-01-19 15:24:40 +0900897 if (port->cfg->irqs[0] == port->cfg->irqs[1])
898 free_irq(port->cfg->irqs[0], port);
Paul Mundt762c69e2008-12-16 18:55:26 +0900899 else {
Paul Mundtce6738b2011-01-19 15:24:40 +0900900 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
901 if (!port->cfg->irqs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 continue;
903
Paul Mundtce6738b2011-01-19 15:24:40 +0900904 free_irq(port->cfg->irqs[i], port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 }
907}
908
909static unsigned int sci_tx_empty(struct uart_port *port)
910{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000911 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900912 unsigned short in_tx_fifo = scif_txfill(port);
913
914 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
918{
919 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
920 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
921 /* If you have signals for DTR and DCD, please implement here. */
922}
923
924static unsigned int sci_get_mctrl(struct uart_port *port)
925{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900926 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 and CTS/RTS */
928
929 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
930}
931
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900932#ifdef CONFIG_SERIAL_SH_SCI_DMA
933static void sci_dma_tx_complete(void *arg)
934{
935 struct sci_port *s = arg;
936 struct uart_port *port = &s->port;
937 struct circ_buf *xmit = &port->state->xmit;
938 unsigned long flags;
939
940 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
941
942 spin_lock_irqsave(&port->lock, flags);
943
Magnus Dammf354a382010-03-19 04:47:01 +0000944 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900945 xmit->tail &= UART_XMIT_SIZE - 1;
946
Magnus Dammf354a382010-03-19 04:47:01 +0000947 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900948
949 async_tx_ack(s->desc_tx);
950 s->cookie_tx = -EINVAL;
951 s->desc_tx = NULL;
952
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900953 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
954 uart_write_wakeup(port);
955
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000956 if (!uart_circ_empty(xmit)) {
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900957 schedule_work(&s->work_tx);
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +0000958 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000959 u16 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +0900960 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +0000961 }
962
963 spin_unlock_irqrestore(&port->lock, flags);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900964}
965
966/* Locking: called with port lock held */
967static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
968 size_t count)
969{
970 struct uart_port *port = &s->port;
971 int i, active, room;
972
973 room = tty_buffer_request_room(tty, count);
974
975 if (s->active_rx == s->cookie_rx[0]) {
976 active = 0;
977 } else if (s->active_rx == s->cookie_rx[1]) {
978 active = 1;
979 } else {
980 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
981 return 0;
982 }
983
984 if (room < count)
985 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
986 count - room);
987 if (!room)
988 return room;
989
990 for (i = 0; i < room; i++)
991 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
992 TTY_NORMAL);
993
994 port->icount.rx += room;
995
996 return room;
997}
998
999static void sci_dma_rx_complete(void *arg)
1000{
1001 struct sci_port *s = arg;
1002 struct uart_port *port = &s->port;
1003 struct tty_struct *tty = port->state->port.tty;
1004 unsigned long flags;
1005 int count;
1006
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001007 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001008
1009 spin_lock_irqsave(&port->lock, flags);
1010
1011 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1012
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001013 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001014
1015 spin_unlock_irqrestore(&port->lock, flags);
1016
1017 if (count)
1018 tty_flip_buffer_push(tty);
1019
1020 schedule_work(&s->work_rx);
1021}
1022
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001023static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1024{
1025 struct dma_chan *chan = s->chan_rx;
1026 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001027
1028 s->chan_rx = NULL;
1029 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1030 dma_release_channel(chan);
Guennadi Liakhovetski85b8e3f2010-05-21 15:22:40 +00001031 if (sg_dma_address(&s->sg_rx[0]))
1032 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1033 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001034 if (enable_pio)
1035 sci_start_rx(port);
1036}
1037
1038static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1039{
1040 struct dma_chan *chan = s->chan_tx;
1041 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001042
1043 s->chan_tx = NULL;
1044 s->cookie_tx = -EINVAL;
1045 dma_release_channel(chan);
1046 if (enable_pio)
1047 sci_start_tx(port);
1048}
1049
1050static void sci_submit_rx(struct sci_port *s)
1051{
1052 struct dma_chan *chan = s->chan_rx;
1053 int i;
1054
1055 for (i = 0; i < 2; i++) {
1056 struct scatterlist *sg = &s->sg_rx[i];
1057 struct dma_async_tx_descriptor *desc;
1058
1059 desc = chan->device->device_prep_slave_sg(chan,
1060 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1061
1062 if (desc) {
1063 s->desc_rx[i] = desc;
1064 desc->callback = sci_dma_rx_complete;
1065 desc->callback_param = s;
1066 s->cookie_rx[i] = desc->tx_submit(desc);
1067 }
1068
1069 if (!desc || s->cookie_rx[i] < 0) {
1070 if (i) {
1071 async_tx_ack(s->desc_rx[0]);
1072 s->cookie_rx[0] = -EINVAL;
1073 }
1074 if (desc) {
1075 async_tx_ack(desc);
1076 s->cookie_rx[i] = -EINVAL;
1077 }
1078 dev_warn(s->port.dev,
1079 "failed to re-start DMA, using PIO\n");
1080 sci_rx_dma_release(s, true);
1081 return;
1082 }
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001083 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1084 s->cookie_rx[i], i);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001085 }
1086
1087 s->active_rx = s->cookie_rx[0];
1088
1089 dma_async_issue_pending(chan);
1090}
1091
1092static void work_fn_rx(struct work_struct *work)
1093{
1094 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1095 struct uart_port *port = &s->port;
1096 struct dma_async_tx_descriptor *desc;
1097 int new;
1098
1099 if (s->active_rx == s->cookie_rx[0]) {
1100 new = 0;
1101 } else if (s->active_rx == s->cookie_rx[1]) {
1102 new = 1;
1103 } else {
1104 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1105 return;
1106 }
1107 desc = s->desc_rx[new];
1108
1109 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1110 DMA_SUCCESS) {
1111 /* Handle incomplete DMA receive */
1112 struct tty_struct *tty = port->state->port.tty;
1113 struct dma_chan *chan = s->chan_rx;
1114 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1115 async_tx);
1116 unsigned long flags;
1117 int count;
1118
Linus Walleij05827632010-05-17 16:30:42 -07001119 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001120 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1121 sh_desc->partial, sh_desc->cookie);
1122
1123 spin_lock_irqsave(&port->lock, flags);
1124 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1125 spin_unlock_irqrestore(&port->lock, flags);
1126
1127 if (count)
1128 tty_flip_buffer_push(tty);
1129
1130 sci_submit_rx(s);
1131
1132 return;
1133 }
1134
1135 s->cookie_rx[new] = desc->tx_submit(desc);
1136 if (s->cookie_rx[new] < 0) {
1137 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1138 sci_rx_dma_release(s, true);
1139 return;
1140 }
1141
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001142 s->active_rx = s->cookie_rx[!new];
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001143
1144 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1145 s->cookie_rx[new], new, s->active_rx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001146}
1147
1148static void work_fn_tx(struct work_struct *work)
1149{
1150 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1151 struct dma_async_tx_descriptor *desc;
1152 struct dma_chan *chan = s->chan_tx;
1153 struct uart_port *port = &s->port;
1154 struct circ_buf *xmit = &port->state->xmit;
1155 struct scatterlist *sg = &s->sg_tx;
1156
1157 /*
1158 * DMA is idle now.
1159 * Port xmit buffer is already mapped, and it is one page... Just adjust
1160 * offsets and lengths. Since it is a circular buffer, we have to
1161 * transmit till the end, and then the rest. Take the port lock to get a
1162 * consistent xmit buffer state.
1163 */
1164 spin_lock_irq(&port->lock);
1165 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001166 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001167 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001168 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001169 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001170 spin_unlock_irq(&port->lock);
1171
Magnus Dammf354a382010-03-19 04:47:01 +00001172 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001173
1174 desc = chan->device->device_prep_slave_sg(chan,
1175 sg, s->sg_len_tx, DMA_TO_DEVICE,
1176 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1177 if (!desc) {
1178 /* switch to PIO */
1179 sci_tx_dma_release(s, true);
1180 return;
1181 }
1182
1183 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1184
1185 spin_lock_irq(&port->lock);
1186 s->desc_tx = desc;
1187 desc->callback = sci_dma_tx_complete;
1188 desc->callback_param = s;
1189 spin_unlock_irq(&port->lock);
1190 s->cookie_tx = desc->tx_submit(desc);
1191 if (s->cookie_tx < 0) {
1192 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1193 /* switch to PIO */
1194 sci_tx_dma_release(s, true);
1195 return;
1196 }
1197
1198 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1199 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1200
1201 dma_async_issue_pending(chan);
1202}
1203#endif
1204
Russell Kingb129a8c2005-08-31 10:12:14 +01001205static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001207 struct sci_port *s = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001208 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001210#ifdef CONFIG_SERIAL_SH_SCI_DMA
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001211 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001212 u16 new, scr = sci_in(port, SCSCR);
1213 if (s->chan_tx)
1214 new = scr | 0x8000;
1215 else
1216 new = scr & ~0x8000;
1217 if (new != scr)
1218 sci_out(port, SCSCR, new);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001219 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001220
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001221 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1222 s->cookie_tx < 0)
1223 schedule_work(&s->work_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001224#endif
Paul Mundtf43dc232011-01-13 15:06:28 +09001225
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001226 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001227 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1228 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001229 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Russell Kingb129a8c2005-08-31 10:12:14 +01001233static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 unsigned short ctrl;
1236
1237 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001239
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001240 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001241 ctrl &= ~0x8000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001242
Paul Mundt8e698612009-06-24 19:44:32 +09001243 ctrl &= ~SCSCR_TIE;
Paul Mundtf43dc232011-01-13 15:06:28 +09001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001248static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 unsigned short ctrl;
1251
Paul Mundtf43dc232011-01-13 15:06:28 +09001252 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1253
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001254 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001255 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static void sci_stop_rx(struct uart_port *port)
1261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 unsigned short ctrl;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ctrl = sci_in(port, SCSCR);
Paul Mundtf43dc232011-01-13 15:06:28 +09001265
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001266 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001267 ctrl &= ~0x4000;
Paul Mundtf43dc232011-01-13 15:06:28 +09001268
1269 ctrl &= ~port_rx_irq_mask(port);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274static void sci_enable_ms(struct uart_port *port)
1275{
1276 /* Nothing here yet .. */
1277}
1278
1279static void sci_break_ctl(struct uart_port *port, int break_state)
1280{
1281 /* Nothing here yet .. */
1282}
1283
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001284#ifdef CONFIG_SERIAL_SH_SCI_DMA
1285static bool filter(struct dma_chan *chan, void *slave)
1286{
1287 struct sh_dmae_slave *param = slave;
1288
1289 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1290 param->slave_id);
1291
1292 if (param->dma_dev == chan->device->dev) {
1293 chan->private = param;
1294 return true;
1295 } else {
1296 return false;
1297 }
1298}
1299
1300static void rx_timer_fn(unsigned long arg)
1301{
1302 struct sci_port *s = (struct sci_port *)arg;
1303 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001304 u16 scr = sci_in(port, SCSCR);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001305
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001306 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001307 scr &= ~0x4000;
Paul Mundtce6738b2011-01-19 15:24:40 +09001308 enable_irq(s->cfg->irqs[1]);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001309 }
Paul Mundtf43dc232011-01-13 15:06:28 +09001310 sci_out(port, SCSCR, scr | SCSCR_RIE);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001311 dev_dbg(port->dev, "DMA Rx timed out\n");
1312 schedule_work(&s->work_rx);
1313}
1314
1315static void sci_request_dma(struct uart_port *port)
1316{
1317 struct sci_port *s = to_sci_port(port);
1318 struct sh_dmae_slave *param;
1319 struct dma_chan *chan;
1320 dma_cap_mask_t mask;
1321 int nent;
1322
1323 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
Paul Mundtce6738b2011-01-19 15:24:40 +09001324 port->line, s->cfg->dma_dev);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001325
Paul Mundtce6738b2011-01-19 15:24:40 +09001326 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001327 return;
1328
1329 dma_cap_zero(mask);
1330 dma_cap_set(DMA_SLAVE, mask);
1331
1332 param = &s->param_tx;
1333
1334 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001335 param->slave_id = s->cfg->dma_slave_tx;
1336 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001337
1338 s->cookie_tx = -EINVAL;
1339 chan = dma_request_channel(mask, filter, param);
1340 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1341 if (chan) {
1342 s->chan_tx = chan;
1343 sg_init_table(&s->sg_tx, 1);
1344 /* UART circular tx buffer is an aligned page. */
1345 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1346 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1347 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1348 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1349 if (!nent)
1350 sci_tx_dma_release(s, false);
1351 else
1352 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1353 sg_dma_len(&s->sg_tx),
1354 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1355
1356 s->sg_len_tx = nent;
1357
1358 INIT_WORK(&s->work_tx, work_fn_tx);
1359 }
1360
1361 param = &s->param_rx;
1362
1363 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
Paul Mundtce6738b2011-01-19 15:24:40 +09001364 param->slave_id = s->cfg->dma_slave_rx;
1365 param->dma_dev = s->cfg->dma_dev;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001366
1367 chan = dma_request_channel(mask, filter, param);
1368 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1369 if (chan) {
1370 dma_addr_t dma[2];
1371 void *buf[2];
1372 int i;
1373
1374 s->chan_rx = chan;
1375
1376 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1377 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1378 &dma[0], GFP_KERNEL);
1379
1380 if (!buf[0]) {
1381 dev_warn(port->dev,
1382 "failed to allocate dma buffer, using PIO\n");
1383 sci_rx_dma_release(s, true);
1384 return;
1385 }
1386
1387 buf[1] = buf[0] + s->buf_len_rx;
1388 dma[1] = dma[0] + s->buf_len_rx;
1389
1390 for (i = 0; i < 2; i++) {
1391 struct scatterlist *sg = &s->sg_rx[i];
1392
1393 sg_init_table(sg, 1);
1394 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1395 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001396 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001397 }
1398
1399 INIT_WORK(&s->work_rx, work_fn_rx);
1400 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1401
1402 sci_submit_rx(s);
1403 }
1404}
1405
1406static void sci_free_dma(struct uart_port *port)
1407{
1408 struct sci_port *s = to_sci_port(port);
1409
Paul Mundtce6738b2011-01-19 15:24:40 +09001410 if (!s->cfg->dma_dev)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001411 return;
1412
1413 if (s->chan_tx)
1414 sci_tx_dma_release(s, false);
1415 if (s->chan_rx)
1416 sci_rx_dma_release(s, false);
1417}
Paul Mundt27bd1072011-01-19 15:37:31 +09001418#else
1419static inline void sci_request_dma(struct uart_port *port)
1420{
1421}
1422
1423static inline void sci_free_dma(struct uart_port *port)
1424{
1425}
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001426#endif
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static int sci_startup(struct uart_port *port)
1429{
Magnus Damma5660ad2009-01-21 15:14:38 +00001430 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001432 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1433
Paul Mundte108b2c2006-09-27 16:32:13 +09001434 if (s->enable)
1435 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 sci_request_irq(s);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001438 sci_request_dma(port);
Yoshinori Satod6569012005-10-14 15:59:12 -07001439 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001440 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 return 0;
1443}
1444
1445static void sci_shutdown(struct uart_port *port)
1446{
Magnus Damma5660ad2009-01-21 15:14:38 +00001447 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001449 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001452 sci_stop_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001453 sci_free_dma(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 sci_free_irq(s);
1455
Paul Mundte108b2c2006-09-27 16:32:13 +09001456 if (s->disable)
1457 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Paul Mundt26c92f32009-06-24 18:23:52 +09001460static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1461 unsigned long freq)
1462{
1463 switch (algo_id) {
1464 case SCBRR_ALGO_1:
1465 return ((freq + 16 * bps) / (16 * bps) - 1);
1466 case SCBRR_ALGO_2:
1467 return ((freq + 16 * bps) / (32 * bps) - 1);
1468 case SCBRR_ALGO_3:
1469 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1470 case SCBRR_ALGO_4:
1471 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1472 case SCBRR_ALGO_5:
1473 return (((freq * 1000 / 32) / bps) - 1);
1474 }
1475
1476 /* Warn, but use a safe default */
1477 WARN_ON(1);
1478 return ((freq + 16 * bps) / (32 * bps) - 1);
1479}
1480
Alan Cox606d0992006-12-08 02:38:45 -08001481static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1482 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Paul Mundt00b9de92009-06-24 17:53:33 +09001484 struct sci_port *s = to_sci_port(port);
Magnus Damm154280f2009-12-22 03:37:28 +00001485 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001486 int t = -1;
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001487 u16 scfcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Magnus Damm154280f2009-12-22 03:37:28 +00001489 /*
1490 * earlyprintk comes here early on with port->uartclk set to zero.
1491 * the clock framework is not up and running at this point so here
1492 * we assume that 115200 is the maximum baud rate. please note that
1493 * the baud rate is not programmed during earlyprintk - it is assumed
1494 * that the previous boot loader has enabled required clocks and
1495 * setup the baud rate generator hardware for us already.
1496 */
1497 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1498
1499 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1500 if (likely(baud && port->uartclk))
Paul Mundtce6738b2011-01-19 15:24:40 +09001501 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Paul Mundte108b2c2006-09-27 16:32:13 +09001503 do {
1504 status = sci_in(port, SCxSR);
1505 } while (!(status & SCxSR_TEND(port)));
1506
1507 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1508
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001509 if (port->type != PORT_SCI)
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001510 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001511
1512 smr_val = sci_in(port, SCSMR) & 3;
1513 if ((termios->c_cflag & CSIZE) == CS7)
1514 smr_val |= 0x40;
1515 if (termios->c_cflag & PARENB)
1516 smr_val |= 0x20;
1517 if (termios->c_cflag & PARODD)
1518 smr_val |= 0x30;
1519 if (termios->c_cflag & CSTOPB)
1520 smr_val |= 0x08;
1521
1522 uart_update_timeout(port, termios->c_cflag, baud);
1523
1524 sci_out(port, SCSMR, smr_val);
1525
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001526 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
Paul Mundtce6738b2011-01-19 15:24:40 +09001527 s->cfg->scscr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001530 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1532 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001533 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 sci_out(port, SCBRR, t);
1537 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1538 }
1539
Paul Mundtd5701642008-12-16 20:07:27 +09001540 sci_init_pins(port, termios->c_cflag);
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001541 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
Paul Mundtb7a76e42006-02-01 03:06:06 -08001542
Paul Mundtce6738b2011-01-19 15:24:40 +09001543 sci_out(port, SCSCR, s->cfg->scscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Guennadi Liakhovetski3089f382010-03-19 13:53:04 +00001545#ifdef CONFIG_SERIAL_SH_SCI_DMA
1546 /*
1547 * Calculate delay for 1.5 DMA buffers: see
1548 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1549 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1550 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1551 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1552 * sizes), but it has been found out experimentally, that this is not
1553 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1554 * as a minimum seem to work perfectly.
1555 */
1556 if (s->chan_rx) {
1557 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1558 port->fifosize / 2;
1559 dev_dbg(port->dev,
1560 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1561 s->rx_timeout * 1000 / HZ, port->timeout);
1562 if (s->rx_timeout < msecs_to_jiffies(20))
1563 s->rx_timeout = msecs_to_jiffies(20);
1564 }
1565#endif
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001568 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571static const char *sci_type(struct uart_port *port)
1572{
1573 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001574 case PORT_IRDA:
1575 return "irda";
1576 case PORT_SCI:
1577 return "sci";
1578 case PORT_SCIF:
1579 return "scif";
1580 case PORT_SCIFA:
1581 return "scifa";
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001582 case PORT_SCIFB:
1583 return "scifb";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
Paul Mundtfa439722008-09-04 18:53:58 +09001586 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589static void sci_release_port(struct uart_port *port)
1590{
1591 /* Nothing here yet .. */
1592}
1593
1594static int sci_request_port(struct uart_port *port)
1595{
1596 /* Nothing here yet .. */
1597 return 0;
1598}
1599
1600static void sci_config_port(struct uart_port *port, int flags)
1601{
Magnus Damma5660ad2009-01-21 15:14:38 +00001602 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Paul Mundtce6738b2011-01-19 15:24:40 +09001604 port->type = s->cfg->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Magnus Damm08f8cb32009-01-21 15:14:22 +00001606 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001607 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001608
1609 if (IS_ERR(port->membase))
1610 dev_err(port->dev, "can't remap port#%d\n", port->line);
1611 } else {
1612 /*
1613 * For the simple (and majority of) cases where we don't
1614 * need to do any remapping, just cast the cookie
1615 * directly.
1616 */
1617 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1622{
Magnus Damma5660ad2009-01-21 15:14:38 +00001623 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Paul Mundtce6738b2011-01-19 15:24:40 +09001625 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EINVAL;
1627 if (ser->baud_base < 2400)
1628 /* No paper tape reader for Mitch.. */
1629 return -EINVAL;
1630
1631 return 0;
1632}
1633
1634static struct uart_ops sci_uart_ops = {
1635 .tx_empty = sci_tx_empty,
1636 .set_mctrl = sci_set_mctrl,
1637 .get_mctrl = sci_get_mctrl,
1638 .start_tx = sci_start_tx,
1639 .stop_tx = sci_stop_tx,
1640 .stop_rx = sci_stop_rx,
1641 .enable_ms = sci_enable_ms,
1642 .break_ctl = sci_break_ctl,
1643 .startup = sci_startup,
1644 .shutdown = sci_shutdown,
1645 .set_termios = sci_set_termios,
1646 .type = sci_type,
1647 .release_port = sci_release_port,
1648 .request_port = sci_request_port,
1649 .config_port = sci_config_port,
1650 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001651#ifdef CONFIG_CONSOLE_POLL
1652 .poll_get_char = sci_poll_get_char,
1653 .poll_put_char = sci_poll_put_char,
1654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655};
1656
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001657static int __devinit sci_init_single(struct platform_device *dev,
1658 struct sci_port *sci_port,
1659 unsigned int index,
1660 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001661{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001662 struct uart_port *port = &sci_port->port;
Paul Mundte108b2c2006-09-27 16:32:13 +09001663
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001664 port->ops = &sci_uart_ops;
1665 port->iotype = UPIO_MEM;
1666 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001667
1668 switch (p->type) {
Guennadi Liakhovetskid1d4b102010-05-23 16:39:09 +00001669 case PORT_SCIFB:
1670 port->fifosize = 256;
1671 break;
Markus Pietrek75136d42010-01-15 08:33:20 +09001672 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001673 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001674 break;
1675 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001676 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001677 break;
1678 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001679 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001680 break;
1681 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001682
1683 if (dev) {
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001684 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1685 if (IS_ERR(sci_port->iclk)) {
1686 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1687 if (IS_ERR(sci_port->iclk)) {
1688 dev_err(&dev->dev, "can't get iclk\n");
1689 return PTR_ERR(sci_port->iclk);
1690 }
1691 }
1692
1693 /*
1694 * The function clock is optional, ignore it if we can't
1695 * find it.
1696 */
1697 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1698 if (IS_ERR(sci_port->fclk))
1699 sci_port->fclk = NULL;
1700
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001701 sci_port->enable = sci_clk_enable;
1702 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001703 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001704 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001705
Magnus Damm7ed7e072009-01-21 15:14:14 +00001706 sci_port->break_timer.data = (unsigned long)sci_port;
1707 sci_port->break_timer.function = sci_break_timer;
1708 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001709
Paul Mundtce6738b2011-01-19 15:24:40 +09001710 sci_port->cfg = p;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001711
Paul Mundtce6738b2011-01-19 15:24:40 +09001712 port->mapbase = p->mapbase;
1713 port->type = p->type;
Paul Mundtf43dc232011-01-13 15:06:28 +09001714 port->flags = p->flags;
Paul Mundtce6738b2011-01-19 15:24:40 +09001715
1716 /*
1717 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1718 * for the multi-IRQ ports, which is where we are primarily
1719 * concerned with the shutdown path synchronization.
1720 *
1721 * For the muxed case there's nothing more to do.
1722 */
1723 port->irq = p->irqs[SCIx_TXI_IRQ];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001724
Paul Mundtce6738b2011-01-19 15:24:40 +09001725 if (p->dma_dev)
1726 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1727 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
Magnus Damm7ed7e072009-01-21 15:14:14 +00001728
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001729 return 0;
Paul Mundte108b2c2006-09-27 16:32:13 +09001730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001733static struct tty_driver *serial_console_device(struct console *co, int *index)
1734{
1735 struct uart_driver *p = &sci_uart_driver;
1736 *index = co->index;
1737 return p->tty_driver;
1738}
1739
1740static void serial_console_putchar(struct uart_port *port, int ch)
1741{
1742 sci_poll_put_char(port, ch);
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745/*
1746 * Print a string to the serial port trying not to disturb
1747 * any possible real use of the port...
1748 */
1749static void serial_console_write(struct console *co, const char *s,
1750 unsigned count)
1751{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001752 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001753 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001754 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001755
Magnus Damm501b8252009-01-21 15:14:30 +00001756 if (sci_port->enable)
1757 sci_port->enable(port);
1758
1759 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001760
1761 /* wait until fifo is empty and last bit has been transmitted */
1762 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1763 while ((sci_in(port, SCxSR) & bits) != bits)
1764 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001765
Magnus Damm345e5a72009-11-05 14:34:57 +00001766 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001767 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001770static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001772 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct uart_port *port;
1774 int baud = 115200;
1775 int bits = 8;
1776 int parity = 'n';
1777 int flow = 'n';
1778 int ret;
1779
Paul Mundte108b2c2006-09-27 16:32:13 +09001780 /*
1781 * Check whether an invalid uart number has been specified, and
1782 * if so, search for the first available port that does have
1783 * console support.
1784 */
1785 if (co->index >= SCI_NPORTS)
1786 co->index = 0;
1787
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001788 if (co->data) {
1789 port = co->data;
1790 sci_port = to_sci_port(port);
1791 } else {
1792 sci_port = &sci_ports[co->index];
1793 port = &sci_port->port;
1794 co->data = port;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001798 * Also need to check port->type, we don't actually have any
1799 * UPIO_PORT ports, but uart_report_port() handily misreports
1800 * it anyways if we don't have a port available by the time this is
1801 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001803 if (!port->type)
1804 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001805
Magnus Damm08f8cb32009-01-21 15:14:22 +00001806 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001807
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001808 if (sci_port->enable)
1809 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (options)
1812 uart_parse_options(options, &baud, &parity, &bits, &flow);
1813
1814 ret = uart_set_options(port, co, baud, parity, bits, flow);
1815#if defined(__H8300H__) || defined(__H8300S__)
1816 /* disable rx interrupt */
1817 if (ret == 0)
1818 sci_stop_rx(port);
1819#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001820 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return ret;
1822}
1823
1824static struct console serial_console = {
1825 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001826 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 .write = serial_console_write,
1828 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001829 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831};
1832
1833static int __init sci_console_init(void)
1834{
1835 register_console(&serial_console);
1836 return 0;
1837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001839
1840static struct sci_port early_serial_port;
1841static struct console early_serial_console = {
1842 .name = "early_ttySC",
1843 .write = serial_console_write,
1844 .flags = CON_PRINTBUFFER,
1845};
1846static char early_serial_buf[32];
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1849
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001850#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001851#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001853#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854#endif
1855
1856static char banner[] __initdata =
1857 KERN_INFO "SuperH SCI(F) driver initialized\n";
1858
1859static struct uart_driver sci_uart_driver = {
1860 .owner = THIS_MODULE,
1861 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .dev_name = "ttySC",
1863 .major = SCI_MAJOR,
1864 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001865 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 .cons = SCI_CONSOLE,
1867};
1868
Paul Mundt54507f62009-05-08 23:48:33 +09001869static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001870{
Paul Mundtd535a232011-01-19 17:19:35 +09001871 struct sci_port *port = platform_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001872
Paul Mundtd535a232011-01-19 17:19:35 +09001873 cpufreq_unregister_notifier(&port->freq_transition,
1874 CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001875
Paul Mundtd535a232011-01-19 17:19:35 +09001876 uart_remove_one_port(&sci_uart_driver, &port->port);
Magnus Damme552de22009-01-21 15:13:42 +00001877
Paul Mundtd535a232011-01-19 17:19:35 +09001878 clk_put(port->iclk);
1879 clk_put(port->fclk);
1880
Magnus Damme552de22009-01-21 15:13:42 +00001881 return 0;
1882}
1883
Magnus Damm0ee70712009-01-21 15:13:50 +00001884static int __devinit sci_probe_single(struct platform_device *dev,
1885 unsigned int index,
1886 struct plat_sci_port *p,
1887 struct sci_port *sciport)
1888{
Magnus Damm0ee70712009-01-21 15:13:50 +00001889 int ret;
1890
1891 /* Sanity check */
1892 if (unlikely(index >= SCI_NPORTS)) {
1893 dev_notice(&dev->dev, "Attempting to register port "
1894 "%d when only %d are available.\n",
1895 index+1, SCI_NPORTS);
1896 dev_notice(&dev->dev, "Consider bumping "
1897 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1898 return 0;
1899 }
1900
Paul Mundtc7ed1ab2010-03-10 18:35:14 +09001901 ret = sci_init_single(dev, sciport, index, p);
1902 if (ret)
1903 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001904
Paul Mundtd535a232011-01-19 17:19:35 +09001905 return uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm0ee70712009-01-21 15:13:50 +00001906}
1907
Paul Mundte108b2c2006-09-27 16:32:13 +09001908/*
1909 * Register a set of serial devices attached to a platform device. The
1910 * list is terminated with a zero flags entry, which means we expect
1911 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1912 * remapping (such as sh64) should also set UPF_IOREMAP.
1913 */
1914static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Paul Mundte108b2c2006-09-27 16:32:13 +09001916 struct plat_sci_port *p = dev->dev.platform_data;
Paul Mundtd535a232011-01-19 17:19:35 +09001917 struct sci_port *sp = &sci_ports[dev->id];
1918 int ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001919
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001920#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1921 if (is_early_platform_device(dev)) {
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001922 early_serial_console.index = dev->id;
1923 early_serial_console.data = &early_serial_port.port;
Paul Mundtd535a232011-01-19 17:19:35 +09001924
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001925 sci_init_single(NULL, &early_serial_port, dev->id, p);
Paul Mundtd535a232011-01-19 17:19:35 +09001926
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001927 serial_console_setup(&early_serial_console, early_serial_buf);
Paul Mundtd535a232011-01-19 17:19:35 +09001928
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001929 if (!strstr(early_serial_buf, "keep"))
1930 early_serial_console.flags |= CON_BOOT;
Paul Mundtd535a232011-01-19 17:19:35 +09001931
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001932 register_console(&early_serial_console);
1933 return 0;
1934 }
1935#endif
1936
Paul Mundtd535a232011-01-19 17:19:35 +09001937 platform_set_drvdata(dev, sp);
Magnus Damme552de22009-01-21 15:13:42 +00001938
Paul Mundtd535a232011-01-19 17:19:35 +09001939 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1940 if (ret)
1941 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001942
Paul Mundtd535a232011-01-19 17:19:35 +09001943 sp->freq_transition.notifier_call = sci_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Paul Mundtd535a232011-01-19 17:19:35 +09001945 ret = cpufreq_register_notifier(&sp->freq_transition,
1946 CPUFREQ_TRANSITION_NOTIFIER);
1947 if (unlikely(ret < 0))
1948 goto err_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950#ifdef CONFIG_SH_STANDARD_BIOS
1951 sh_bios_gdb_detach();
1952#endif
1953
Paul Mundte108b2c2006-09-27 16:32:13 +09001954 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001955
1956err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001957 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001958 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001959}
1960
Paul Mundt6daa79b2009-06-15 07:07:38 +09001961static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001962{
Paul Mundtd535a232011-01-19 17:19:35 +09001963 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09001964
Paul Mundtd535a232011-01-19 17:19:35 +09001965 if (sport)
1966 uart_suspend_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001967
1968 return 0;
1969}
1970
Paul Mundt6daa79b2009-06-15 07:07:38 +09001971static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001972{
Paul Mundtd535a232011-01-19 17:19:35 +09001973 struct sci_port *sport = dev_get_drvdata(dev);
Paul Mundte108b2c2006-09-27 16:32:13 +09001974
Paul Mundtd535a232011-01-19 17:19:35 +09001975 if (sport)
1976 uart_resume_port(&sci_uart_driver, &sport->port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001977
1978 return 0;
1979}
1980
Alexey Dobriyan47145212009-12-14 18:00:08 -08001981static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001982 .suspend = sci_suspend,
1983 .resume = sci_resume,
1984};
1985
Paul Mundte108b2c2006-09-27 16:32:13 +09001986static struct platform_driver sci_driver = {
1987 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001988 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001989 .driver = {
1990 .name = "sh-sci",
1991 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001992 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001993 },
1994};
1995
1996static int __init sci_init(void)
1997{
1998 int ret;
1999
2000 printk(banner);
2001
Paul Mundte108b2c2006-09-27 16:32:13 +09002002 ret = uart_register_driver(&sci_uart_driver);
2003 if (likely(ret == 0)) {
2004 ret = platform_driver_register(&sci_driver);
2005 if (unlikely(ret))
2006 uart_unregister_driver(&sci_uart_driver);
2007 }
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return ret;
2010}
2011
2012static void __exit sci_exit(void)
2013{
Paul Mundte108b2c2006-09-27 16:32:13 +09002014 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 uart_unregister_driver(&sci_uart_driver);
2016}
2017
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00002018#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2019early_platform_init_buffer("earlyprintk", &sci_driver,
2020 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022module_init(sci_init);
2023module_exit(sci_exit);
2024
Paul Mundte108b2c2006-09-27 16:32:13 +09002025MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002026MODULE_ALIAS("platform:sh-sci");