blob: cd52dd64edf0bf4568e9c142c47a9b2b94e5f388 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/sh-sci.c
3 *
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 *
Paul Mundt7ff731a2008-10-01 15:46:58 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Markus Brunner3ea6bc32007-08-20 08:59:33 +09007 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
Magnus Dammd89ddd12007-07-25 11:42:56 +090016 * Removed SH7300 support (Jul 2007).
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
20 * for more details.
21 */
Paul Mundt0b3d4ef2007-03-14 13:22:37 +090022#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#undef DEBUG
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/serial.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/console.h>
Paul Mundte108b2c2006-09-27 16:32:13 +090043#include <linux/platform_device.h>
Paul Mundt96de1a82008-02-26 14:52:45 +090044#include <linux/serial_sci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/notifier.h>
46#include <linux/cpufreq.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090047#include <linux/clk.h>
Paul Mundtfa5da2f2007-03-08 17:27:37 +090048#include <linux/ctype.h>
Paul Mundt7ff731a2008-10-01 15:46:58 +090049#include <linux/err.h>
Magnus Damme552de22009-01-21 15:13:42 +000050#include <linux/list.h>
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090051#include <linux/dmaengine.h>
52#include <linux/scatterlist.h>
Paul Mundt85f094e2008-04-25 16:04:20 +090053
54#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090055#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yoshinori Sato168f3622009-04-28 04:40:15 +000058#ifdef CONFIG_H8300
59#include <asm/gpio.h>
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "sh-sci.h"
63
Paul Mundte108b2c2006-09-27 16:32:13 +090064struct sci_port {
65 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Paul Mundte108b2c2006-09-27 16:32:13 +090067 /* Port type */
68 unsigned int type;
69
70 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090071 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090072
Paul Mundte108b2c2006-09-27 16:32:13 +090073 /* Port enable callback */
74 void (*enable)(struct uart_port *port);
75
76 /* Port disable callback */
77 void (*disable)(struct uart_port *port);
78
79 /* Break timer */
80 struct timer_list break_timer;
81 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090082
Magnus Damm501b8252009-01-21 15:14:30 +000083 /* Interface clock */
84 struct clk *iclk;
85 /* Data clock */
86 struct clk *dclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090087
Magnus Damme552de22009-01-21 15:13:42 +000088 struct list_head node;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090089 struct dma_chan *chan_tx;
90 struct dma_chan *chan_rx;
91#ifdef CONFIG_SERIAL_SH_SCI_DMA
92 struct device *dma_dev;
Magnus Damm4bab9d42010-03-19 04:46:38 +000093 unsigned int slave_tx;
94 unsigned int slave_rx;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +090095 struct dma_async_tx_descriptor *desc_tx;
96 struct dma_async_tx_descriptor *desc_rx[2];
97 dma_cookie_t cookie_tx;
98 dma_cookie_t cookie_rx[2];
99 dma_cookie_t active_rx;
100 struct scatterlist sg_tx;
101 unsigned int sg_len_tx;
102 struct scatterlist sg_rx[2];
103 size_t buf_len_rx;
104 struct sh_dmae_slave param_tx;
105 struct sh_dmae_slave param_rx;
106 struct work_struct work_tx;
107 struct work_struct work_rx;
108 struct timer_list rx_timer;
109#endif
Magnus Damme552de22009-01-21 15:13:42 +0000110};
111
112struct sh_sci_priv {
113 spinlock_t lock;
114 struct list_head ports;
Magnus Damme552de22009-01-21 15:13:42 +0000115 struct notifier_block clk_nb;
Paul Mundte108b2c2006-09-27 16:32:13 +0900116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +0100119static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Paul Mundte108b2c2006-09-27 16:32:13 +0900121#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
122
123static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static struct uart_driver sci_uart_driver;
125
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900126static inline struct sci_port *
127to_sci_port(struct uart_port *uart)
128{
129 return container_of(uart, struct sci_port, port);
130}
131
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900132#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900133
134#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900135static inline void handle_error(struct uart_port *port)
136{
137 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
139}
140
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900141static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned short status;
144 int c;
145
Paul Mundte108b2c2006-09-27 16:32:13 +0900146 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 status = sci_in(port, SCxSR);
148 if (status & SCxSR_ERRORS(port)) {
149 handle_error(port);
150 continue;
151 }
152 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900155
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900156 /* Dummy read */
157 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return c;
161}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900164static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned short status;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 do {
169 status = sci_in(port, SCxSR);
170 } while (!(status & SCxSR_TDxE(port)));
171
Paul Mundt272966c2008-11-13 17:46:06 +0900172 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000173 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900175#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Paul Mundt15c73aa2008-10-02 19:47:12 +0900177#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900178static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int ch = (port->mapbase - SMR0) >> 3;
181
182 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900183 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
184 h8300_sci_pins[ch].rx,
185 H8300_GPIO_INPUT);
186 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
187 h8300_sci_pins[ch].tx,
188 H8300_GPIO_OUTPUT);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* tx mark output*/
191 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
192}
Paul Mundtd5701642008-12-16 20:07:27 +0900193#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
194static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900195{
Paul Mundtd5701642008-12-16 20:07:27 +0900196 if (port->mapbase == 0xA4400000) {
197 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
198 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
199 } else if (port->mapbase == 0xA4410000)
200 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900201}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900202#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900203static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900204{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900205 unsigned short data;
206
207 if (cflag & CRTSCTS) {
208 /* enable RTS/CTS */
209 if (port->mapbase == 0xa4430000) { /* SCIF0 */
210 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900211 data = __raw_readw(PORT_PTCR);
212 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900213 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
214 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900215 data = __raw_readw(PORT_PVCR);
216 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900217 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900218 } else {
219 if (port->mapbase == 0xa4430000) { /* SCIF0 */
220 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900221 data = __raw_readw(PORT_PTCR);
222 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900223 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
224 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900225 data = __raw_readw(PORT_PVCR);
226 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900227 }
228 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900229}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800230#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900231/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900232static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800234 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Paul Mundtb7a76e42006-02-01 03:06:06 -0800236 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900237 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800238 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900239 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800240
Paul Mundtd5701642008-12-16 20:07:27 +0900241 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900243 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Clear out SCP7MD1,0, SCP4MD1,0,
245 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900246 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Paul Mundt32b53072009-12-24 14:52:43 +0900248 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900250 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Paul Mundt41504c32006-12-11 20:28:03 +0900253#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900254static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900255{
Magnus Damm346b7462008-04-23 21:25:29 +0900256 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900257
Magnus Damm346b7462008-04-23 21:25:29 +0900258 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900259 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900260 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900261 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900262 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900263
Paul Mundtd5701642008-12-16 20:07:27 +0900264 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900265 }
Paul Mundt41504c32006-12-11 20:28:03 +0900266}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900267#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
268 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900269 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900270 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900271 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900272 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900273static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
274{
275 if (!(cflag & CRTSCTS))
276 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
277}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900278#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900279static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
280{
281 if (!(cflag & CRTSCTS))
282 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
283}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800284#else
Paul Mundtd5701642008-12-16 20:07:27 +0900285static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
286{
287 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
Paul Mundte108b2c2006-09-27 16:32:13 +0900289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Paul Mundt32351a22007-03-12 14:38:59 +0900291#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
292 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900293 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
294 defined(CONFIG_CPU_SUBTYPE_SH7786)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900295static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900296{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900297 return sci_in(port, SCTFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900298}
299
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900300static int scif_txroom(struct uart_port *port)
301{
302 return SCIF_TXROOM_MAX - scif_txfill(port);
303}
304
305static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900306{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900307 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900308}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900309#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900310static int scif_txfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900311{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900312 if (port->mapbase == 0xffe00000 ||
313 port->mapbase == 0xffe08000)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900314 /* SCIF0/1*/
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900315 return sci_in(port, SCTFDR) & 0xff;
316 else
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900317 /* SCIF2 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900318 return sci_in(port, SCFDR) >> 8;
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900319}
320
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900321static int scif_txroom(struct uart_port *port)
322{
323 if (port->mapbase == 0xffe00000 ||
324 port->mapbase == 0xffe08000)
325 /* SCIF0/1*/
326 return SCIF_TXROOM_MAX - scif_txfill(port);
327 else
328 /* SCIF2 */
329 return SCIF2_TXROOM_MAX - scif_txfill(port);
330}
331
332static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900333{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900334 if ((port->mapbase == 0xffe00000) ||
335 (port->mapbase == 0xffe08000)) {
336 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900337 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900338 } else {
339 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900340 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900341 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900342}
Paul Mundte108b2c2006-09-27 16:32:13 +0900343#else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900344static int scif_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900345{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900346 return sci_in(port, SCFDR) >> 8;
Paul Mundte108b2c2006-09-27 16:32:13 +0900347}
348
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900349static int scif_txroom(struct uart_port *port)
350{
351 return SCIF_TXROOM_MAX - scif_txfill(port);
352}
353
354static int scif_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900355{
356 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900360static int sci_txfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900361{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900362 return !(sci_in(port, SCxSR) & SCI_TDRE);
Paul Mundte108b2c2006-09-27 16:32:13 +0900363}
364
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900365static int sci_txroom(struct uart_port *port)
366{
367 return !sci_txfill(port);
368}
369
370static int sci_rxfill(struct uart_port *port)
Paul Mundte108b2c2006-09-27 16:32:13 +0900371{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900372 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/* ********************************************************************** *
376 * the interrupt related routines *
377 * ********************************************************************** */
378
379static void sci_transmit_chars(struct uart_port *port)
380{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700381 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned short status;
384 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900385 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 status = sci_in(port, SCxSR);
388 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900390 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900392 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
396 }
397
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900398 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900399 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900400 else
401 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 do {
404 unsigned char c;
405
406 if (port->x_char) {
407 c = port->x_char;
408 port->x_char = 0;
409 } else if (!uart_circ_empty(xmit) && !stopped) {
410 c = xmit->buf[xmit->tail];
411 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
412 } else {
413 break;
414 }
415
416 sci_out(port, SCxTDR, c);
417
418 port->icount.tx++;
419 } while (--count > 0);
420
421 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
422
423 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
424 uart_write_wakeup(port);
425 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100426 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ctrl = sci_in(port, SCSCR);
429
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900430 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 sci_in(port, SCxSR); /* Dummy read */
432 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 ctrl |= SCI_CTRL_FLAGS_TIE;
436 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438}
439
440/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900441#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howells7d12e782006-10-05 14:55:46 +0100443static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900445 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700446 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int i, count, copied = 0;
448 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800449 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 status = sci_in(port, SCxSR);
452 if (!(status & SCxSR_RDxF(port)))
453 return;
454
455 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900456 if (port->type == PORT_SCI)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900457 count = sci_rxfill(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900458 else
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900459 count = scif_rxfill(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800462 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* If for any reason we can't copy more data, we're done! */
465 if (count == 0)
466 break;
467
468 if (port->type == PORT_SCI) {
469 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900470 if (uart_handle_sysrq_char(port, c) ||
471 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900473 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900474 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900476 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 char c = sci_in(port, SCxRDR);
478 status = sci_in(port, SCxSR);
479#if defined(CONFIG_CPU_SH3)
480 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900481 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if ((c == 0) &&
483 (status & SCxSR_FER(port))) {
484 count--; i--;
485 continue;
486 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900489 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900490 sci_port->break_flag = 0;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (STEPFN(c)) {
493 count--; i--;
494 continue;
495 }
496 }
497#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100498 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 count--; i--;
500 continue;
501 }
502
503 /* Store data and status */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900504 if (status & SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800505 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900506 dev_notice(port->dev, "frame error\n");
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900507 } else if (status & SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800508 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900509 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800510 } else
511 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900512
Alan Cox33f0f882006-01-09 20:54:13 -0800513 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 }
516
517 sci_in(port, SCxSR); /* dummy read */
518 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 copied += count;
521 port->icount.rx += count;
522 }
523
524 if (copied) {
525 /* Tell the rest of the system the news. New characters! */
526 tty_flip_buffer_push(tty);
527 } else {
528 sci_in(port, SCxSR); /* dummy read */
529 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
530 }
531}
532
533#define SCI_BREAK_JIFFIES (HZ/20)
534/* The sci generates interrupts during the break,
535 * 1 per millisecond or so during the break period, for 9600 baud.
536 * So dont bother disabling interrupts.
537 * But dont want more than 1 break event.
538 * Use a kernel timer to periodically poll the rx line until
539 * the break is finished.
540 */
541static void sci_schedule_break_timer(struct sci_port *port)
542{
543 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
544 add_timer(&port->break_timer);
545}
546/* Ensure that two consecutive samples find the break over. */
547static void sci_break_timer(unsigned long data)
548{
Paul Mundte108b2c2006-09-27 16:32:13 +0900549 struct sci_port *port = (struct sci_port *)data;
550
551 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900553 sci_schedule_break_timer(port);
554 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* break is over. */
556 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900557 sci_schedule_break_timer(port);
558 } else
559 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562static inline int sci_handle_errors(struct uart_port *port)
563{
564 int copied = 0;
565 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700566 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Paul Mundte108b2c2006-09-27 16:32:13 +0900568 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900570 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800571 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900572
573 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Paul Mundte108b2c2006-09-27 16:32:13 +0900576 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (sci_rxd_in(port) == 0) {
578 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900579 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900580
581 if (!sci_port->break_flag) {
582 sci_port->break_flag = 1;
583 sci_schedule_break_timer(sci_port);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900586 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900588
589 dev_dbg(port->dev, "BREAK detected\n");
590
Paul Mundte108b2c2006-09-27 16:32:13 +0900591 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900592 copied++;
593 }
594
Paul Mundte108b2c2006-09-27 16:32:13 +0900595 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900597 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800598 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900599
600 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 }
603
Paul Mundte108b2c2006-09-27 16:32:13 +0900604 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900606 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
607 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900608
609 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Alan Cox33f0f882006-01-09 20:54:13 -0800612 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 return copied;
616}
617
Paul Mundtd830fa42008-12-16 19:29:38 +0900618static inline int sci_handle_fifo_overrun(struct uart_port *port)
619{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700620 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900621 int copied = 0;
622
623 if (port->type != PORT_SCIF)
624 return 0;
625
626 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
627 sci_out(port, SCLSR, 0);
628
629 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
630 tty_flip_buffer_push(tty);
631
632 dev_notice(port->dev, "overrun error\n");
633 copied++;
634 }
635
636 return copied;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static inline int sci_handle_breaks(struct uart_port *port)
640{
641 int copied = 0;
642 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700643 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000644 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900646 if (uart_handle_break(port))
647 return 0;
648
Paul Mundtb7a76e42006-02-01 03:06:06 -0800649 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#if defined(CONFIG_CPU_SH3)
651 /* Debounce break */
652 s->break_flag = 1;
653#endif
654 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900655 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800656 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900657
658 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Alan Cox33f0f882006-01-09 20:54:13 -0800661 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900663
Paul Mundtd830fa42008-12-16 19:29:38 +0900664 copied += sci_handle_fifo_overrun(port);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return copied;
667}
668
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900669static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900671#ifdef CONFIG_SERIAL_SH_SCI_DMA
672 struct uart_port *port = ptr;
673 struct sci_port *s = to_sci_port(port);
674
675 if (s->chan_rx) {
676 unsigned long tout;
677 u16 scr = sci_in(port, SCSCR);
678 u16 ssr = sci_in(port, SCxSR);
679
680 /* Disable future Rx interrupts */
681 sci_out(port, SCSCR, scr & ~SCI_CTRL_FLAGS_RIE);
682 /* Clear current interrupt */
683 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
684 /* Calculate delay for 1.5 DMA buffers */
685 tout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
686 port->fifosize / 2;
Paul Mundtc6efd462010-03-02 11:53:54 +0900687 dev_dbg(port->dev, "Rx IRQ: setup timeout in %lu ms\n",
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900688 tout * 1000 / HZ);
689 if (tout < 2)
690 tout = 2;
691 mod_timer(&s->rx_timer, jiffies + tout);
692
693 return IRQ_HANDLED;
694 }
695#endif
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* I think sci_receive_chars has to be called irrespective
698 * of whether the I_IXOFF is set, otherwise, how is the interrupt
699 * to be disabled?
700 */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900701 sci_receive_chars(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return IRQ_HANDLED;
704}
705
David Howells7d12e782006-10-05 14:55:46 +0100706static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900709 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Stuart Menefyfd78a762009-07-29 23:01:24 +0900711 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900713 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 return IRQ_HANDLED;
716}
717
David Howells7d12e782006-10-05 14:55:46 +0100718static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct uart_port *port = ptr;
721
722 /* Handle errors */
723 if (port->type == PORT_SCI) {
724 if (sci_handle_errors(port)) {
725 /* discard character in rx buffer */
726 sci_in(port, SCxSR);
727 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
728 }
729 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900730 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100731 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
734 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
735
736 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100737 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 return IRQ_HANDLED;
740}
741
David Howells7d12e782006-10-05 14:55:46 +0100742static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct uart_port *port = ptr;
745
746 /* Handle BREAKs */
747 sci_handle_breaks(port);
748 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
749
750 return IRQ_HANDLED;
751}
752
David Howells7d12e782006-10-05 14:55:46 +0100753static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Magnus Damm44e18e92009-07-03 08:39:34 +0000755 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900756 struct uart_port *port = ptr;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900757 struct sci_port *s = to_sci_port(port);
Michael Trimarchia8884e32008-10-31 16:10:23 +0900758 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900760 ssr_status = sci_in(port, SCxSR);
761 scr_status = sci_in(port, SCSCR);
Magnus Damm44e18e92009-07-03 08:39:34 +0000762 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Tx Interrupt */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900765 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE) &&
766 !s->chan_tx)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900767 ret = sci_tx_interrupt(irq, ptr);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900768 /*
769 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
770 * DR flags
771 */
772 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
773 (scr_status & SCI_CTRL_FLAGS_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900774 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000776 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900777 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000779 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900780 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900782 WARN_ONCE(ret == IRQ_NONE,
783 "%s: %d IRQ %d, status %x, control %x\n", __func__,
784 irq, port->line, ssr_status, scr_status);
785
Michael Trimarchia8884e32008-10-31 16:10:23 +0900786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/*
790 * Here we define a transistion notifier so that we can update all of our
791 * ports' baud rate when the peripheral clock changes.
792 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900793static int sci_notifier(struct notifier_block *self,
794 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Magnus Damme552de22009-01-21 15:13:42 +0000796 struct sh_sci_priv *priv = container_of(self,
797 struct sh_sci_priv, clk_nb);
798 struct sci_port *sci_port;
799 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000802 (phase == CPUFREQ_RESUMECHANGE)) {
803 spin_lock_irqsave(&priv->lock, flags);
804 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000805 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000806 spin_unlock_irqrestore(&priv->lock, flags);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return NOTIFY_OK;
810}
Magnus Damm501b8252009-01-21 15:14:30 +0000811
812static void sci_clk_enable(struct uart_port *port)
813{
814 struct sci_port *sci_port = to_sci_port(port);
815
816 clk_enable(sci_port->dclk);
817 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
818
819 if (sci_port->iclk)
820 clk_enable(sci_port->iclk);
821}
822
823static void sci_clk_disable(struct uart_port *port)
824{
825 struct sci_port *sci_port = to_sci_port(port);
826
827 if (sci_port->iclk)
828 clk_disable(sci_port->iclk);
829
830 clk_disable(sci_port->dclk);
831}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833static int sci_request_irq(struct sci_port *port)
834{
835 int i;
David Howells7d12e782006-10-05 14:55:46 +0100836 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
838 sci_br_interrupt,
839 };
840 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
841 "SCI Transmit Data Empty", "SCI Break" };
842
843 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900844 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900846
847 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900848 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900849 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -ENODEV;
851 }
852 } else {
853 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900854 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900856
Paul Mundte108b2c2006-09-27 16:32:13 +0900857 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900858 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900859 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -ENODEV;
861 }
862 }
863 }
864
865 return 0;
866}
867
868static void sci_free_irq(struct sci_port *port)
869{
870 int i;
871
Paul Mundt762c69e2008-12-16 18:55:26 +0900872 if (port->irqs[0] == port->irqs[1])
873 free_irq(port->irqs[0], port);
874 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
876 if (!port->irqs[i])
877 continue;
878
879 free_irq(port->irqs[i], port);
880 }
881 }
882}
883
884static unsigned int sci_tx_empty(struct uart_port *port)
885{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000886 unsigned short status = sci_in(port, SCxSR);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900887 unsigned short in_tx_fifo = scif_txfill(port);
888
889 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
893{
894 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
895 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
896 /* If you have signals for DTR and DCD, please implement here. */
897}
898
899static unsigned int sci_get_mctrl(struct uart_port *port)
900{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900901 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 and CTS/RTS */
903
904 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
905}
906
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900907#ifdef CONFIG_SERIAL_SH_SCI_DMA
908static void sci_dma_tx_complete(void *arg)
909{
910 struct sci_port *s = arg;
911 struct uart_port *port = &s->port;
912 struct circ_buf *xmit = &port->state->xmit;
913 unsigned long flags;
914
915 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
916
917 spin_lock_irqsave(&port->lock, flags);
918
Magnus Dammf354a382010-03-19 04:47:01 +0000919 xmit->tail += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900920 xmit->tail &= UART_XMIT_SIZE - 1;
921
Magnus Dammf354a382010-03-19 04:47:01 +0000922 port->icount.tx += sg_dma_len(&s->sg_tx);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +0900923
924 async_tx_ack(s->desc_tx);
925 s->cookie_tx = -EINVAL;
926 s->desc_tx = NULL;
927
928 spin_unlock_irqrestore(&port->lock, flags);
929
930 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
931 uart_write_wakeup(port);
932
933 if (uart_circ_chars_pending(xmit))
934 schedule_work(&s->work_tx);
935}
936
937/* Locking: called with port lock held */
938static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
939 size_t count)
940{
941 struct uart_port *port = &s->port;
942 int i, active, room;
943
944 room = tty_buffer_request_room(tty, count);
945
946 if (s->active_rx == s->cookie_rx[0]) {
947 active = 0;
948 } else if (s->active_rx == s->cookie_rx[1]) {
949 active = 1;
950 } else {
951 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
952 return 0;
953 }
954
955 if (room < count)
956 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
957 count - room);
958 if (!room)
959 return room;
960
961 for (i = 0; i < room; i++)
962 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
963 TTY_NORMAL);
964
965 port->icount.rx += room;
966
967 return room;
968}
969
970static void sci_dma_rx_complete(void *arg)
971{
972 struct sci_port *s = arg;
973 struct uart_port *port = &s->port;
974 struct tty_struct *tty = port->state->port.tty;
975 unsigned long flags;
976 int count;
977
978 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
979
980 spin_lock_irqsave(&port->lock, flags);
981
982 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
983
984 mod_timer(&s->rx_timer, jiffies + msecs_to_jiffies(5));
985
986 spin_unlock_irqrestore(&port->lock, flags);
987
988 if (count)
989 tty_flip_buffer_push(tty);
990
991 schedule_work(&s->work_rx);
992}
993
994static void sci_start_rx(struct uart_port *port);
995static void sci_start_tx(struct uart_port *port);
996
997static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
998{
999 struct dma_chan *chan = s->chan_rx;
1000 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001001
1002 s->chan_rx = NULL;
1003 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1004 dma_release_channel(chan);
1005 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1006 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1007 if (enable_pio)
1008 sci_start_rx(port);
1009}
1010
1011static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1012{
1013 struct dma_chan *chan = s->chan_tx;
1014 struct uart_port *port = &s->port;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001015
1016 s->chan_tx = NULL;
1017 s->cookie_tx = -EINVAL;
1018 dma_release_channel(chan);
1019 if (enable_pio)
1020 sci_start_tx(port);
1021}
1022
1023static void sci_submit_rx(struct sci_port *s)
1024{
1025 struct dma_chan *chan = s->chan_rx;
1026 int i;
1027
1028 for (i = 0; i < 2; i++) {
1029 struct scatterlist *sg = &s->sg_rx[i];
1030 struct dma_async_tx_descriptor *desc;
1031
1032 desc = chan->device->device_prep_slave_sg(chan,
1033 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1034
1035 if (desc) {
1036 s->desc_rx[i] = desc;
1037 desc->callback = sci_dma_rx_complete;
1038 desc->callback_param = s;
1039 s->cookie_rx[i] = desc->tx_submit(desc);
1040 }
1041
1042 if (!desc || s->cookie_rx[i] < 0) {
1043 if (i) {
1044 async_tx_ack(s->desc_rx[0]);
1045 s->cookie_rx[0] = -EINVAL;
1046 }
1047 if (desc) {
1048 async_tx_ack(desc);
1049 s->cookie_rx[i] = -EINVAL;
1050 }
1051 dev_warn(s->port.dev,
1052 "failed to re-start DMA, using PIO\n");
1053 sci_rx_dma_release(s, true);
1054 return;
1055 }
1056 }
1057
1058 s->active_rx = s->cookie_rx[0];
1059
1060 dma_async_issue_pending(chan);
1061}
1062
1063static void work_fn_rx(struct work_struct *work)
1064{
1065 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1066 struct uart_port *port = &s->port;
1067 struct dma_async_tx_descriptor *desc;
1068 int new;
1069
1070 if (s->active_rx == s->cookie_rx[0]) {
1071 new = 0;
1072 } else if (s->active_rx == s->cookie_rx[1]) {
1073 new = 1;
1074 } else {
1075 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1076 return;
1077 }
1078 desc = s->desc_rx[new];
1079
1080 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1081 DMA_SUCCESS) {
1082 /* Handle incomplete DMA receive */
1083 struct tty_struct *tty = port->state->port.tty;
1084 struct dma_chan *chan = s->chan_rx;
1085 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1086 async_tx);
1087 unsigned long flags;
1088 int count;
1089
1090 chan->device->device_terminate_all(chan);
1091 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1092 sh_desc->partial, sh_desc->cookie);
1093
1094 spin_lock_irqsave(&port->lock, flags);
1095 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1096 spin_unlock_irqrestore(&port->lock, flags);
1097
1098 if (count)
1099 tty_flip_buffer_push(tty);
1100
1101 sci_submit_rx(s);
1102
1103 return;
1104 }
1105
1106 s->cookie_rx[new] = desc->tx_submit(desc);
1107 if (s->cookie_rx[new] < 0) {
1108 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1109 sci_rx_dma_release(s, true);
1110 return;
1111 }
1112
1113 dev_dbg(port->dev, "%s: cookie %d #%d\n", __func__,
1114 s->cookie_rx[new], new);
1115
1116 s->active_rx = s->cookie_rx[!new];
1117}
1118
1119static void work_fn_tx(struct work_struct *work)
1120{
1121 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1122 struct dma_async_tx_descriptor *desc;
1123 struct dma_chan *chan = s->chan_tx;
1124 struct uart_port *port = &s->port;
1125 struct circ_buf *xmit = &port->state->xmit;
1126 struct scatterlist *sg = &s->sg_tx;
1127
1128 /*
1129 * DMA is idle now.
1130 * Port xmit buffer is already mapped, and it is one page... Just adjust
1131 * offsets and lengths. Since it is a circular buffer, we have to
1132 * transmit till the end, and then the rest. Take the port lock to get a
1133 * consistent xmit buffer state.
1134 */
1135 spin_lock_irq(&port->lock);
1136 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
Magnus Dammf354a382010-03-19 04:47:01 +00001137 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001138 sg->offset;
Magnus Dammf354a382010-03-19 04:47:01 +00001139 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001140 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001141 spin_unlock_irq(&port->lock);
1142
Magnus Dammf354a382010-03-19 04:47:01 +00001143 BUG_ON(!sg_dma_len(sg));
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001144
1145 desc = chan->device->device_prep_slave_sg(chan,
1146 sg, s->sg_len_tx, DMA_TO_DEVICE,
1147 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1148 if (!desc) {
1149 /* switch to PIO */
1150 sci_tx_dma_release(s, true);
1151 return;
1152 }
1153
1154 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1155
1156 spin_lock_irq(&port->lock);
1157 s->desc_tx = desc;
1158 desc->callback = sci_dma_tx_complete;
1159 desc->callback_param = s;
1160 spin_unlock_irq(&port->lock);
1161 s->cookie_tx = desc->tx_submit(desc);
1162 if (s->cookie_tx < 0) {
1163 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1164 /* switch to PIO */
1165 sci_tx_dma_release(s, true);
1166 return;
1167 }
1168
1169 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1170 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1171
1172 dma_async_issue_pending(chan);
1173}
1174#endif
1175
Russell Kingb129a8c2005-08-31 10:12:14 +01001176static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Paul Mundte108b2c2006-09-27 16:32:13 +09001178 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001180#ifdef CONFIG_SERIAL_SH_SCI_DMA
1181 struct sci_port *s = to_sci_port(port);
1182
1183 if (s->chan_tx) {
1184 if (!uart_circ_empty(&s->port.state->xmit) && s->cookie_tx < 0)
1185 schedule_work(&s->work_tx);
1186
1187 return;
1188 }
1189#endif
1190
Paul Mundte108b2c2006-09-27 16:32:13 +09001191 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1192 ctrl = sci_in(port, SCSCR);
1193 ctrl |= SCI_CTRL_FLAGS_TIE;
1194 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Russell Kingb129a8c2005-08-31 10:12:14 +01001197static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unsigned short ctrl;
1200
1201 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ctrl = sci_in(port, SCSCR);
1203 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1204 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001207static void sci_start_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001209 unsigned short ctrl = SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001212 ctrl |= sci_in(port, SCSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static void sci_stop_rx(struct uart_port *port)
1217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 unsigned short ctrl;
1219
1220 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ctrl = sci_in(port, SCSCR);
1222 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
1223 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static void sci_enable_ms(struct uart_port *port)
1227{
1228 /* Nothing here yet .. */
1229}
1230
1231static void sci_break_ctl(struct uart_port *port, int break_state)
1232{
1233 /* Nothing here yet .. */
1234}
1235
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001236#ifdef CONFIG_SERIAL_SH_SCI_DMA
1237static bool filter(struct dma_chan *chan, void *slave)
1238{
1239 struct sh_dmae_slave *param = slave;
1240
1241 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1242 param->slave_id);
1243
1244 if (param->dma_dev == chan->device->dev) {
1245 chan->private = param;
1246 return true;
1247 } else {
1248 return false;
1249 }
1250}
1251
1252static void rx_timer_fn(unsigned long arg)
1253{
1254 struct sci_port *s = (struct sci_port *)arg;
1255 struct uart_port *port = &s->port;
1256
1257 u16 scr = sci_in(port, SCSCR);
1258 sci_out(port, SCSCR, scr | SCI_CTRL_FLAGS_RIE);
1259 dev_dbg(port->dev, "DMA Rx timed out\n");
1260 schedule_work(&s->work_rx);
1261}
1262
1263static void sci_request_dma(struct uart_port *port)
1264{
1265 struct sci_port *s = to_sci_port(port);
1266 struct sh_dmae_slave *param;
1267 struct dma_chan *chan;
1268 dma_cap_mask_t mask;
1269 int nent;
1270
1271 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1272 port->line, s->dma_dev);
1273
1274 if (!s->dma_dev)
1275 return;
1276
1277 dma_cap_zero(mask);
1278 dma_cap_set(DMA_SLAVE, mask);
1279
1280 param = &s->param_tx;
1281
1282 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1283 param->slave_id = s->slave_tx;
1284 param->dma_dev = s->dma_dev;
1285
1286 s->cookie_tx = -EINVAL;
1287 chan = dma_request_channel(mask, filter, param);
1288 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1289 if (chan) {
1290 s->chan_tx = chan;
1291 sg_init_table(&s->sg_tx, 1);
1292 /* UART circular tx buffer is an aligned page. */
1293 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1294 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1295 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1296 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1297 if (!nent)
1298 sci_tx_dma_release(s, false);
1299 else
1300 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1301 sg_dma_len(&s->sg_tx),
1302 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1303
1304 s->sg_len_tx = nent;
1305
1306 INIT_WORK(&s->work_tx, work_fn_tx);
1307 }
1308
1309 param = &s->param_rx;
1310
1311 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1312 param->slave_id = s->slave_rx;
1313 param->dma_dev = s->dma_dev;
1314
1315 chan = dma_request_channel(mask, filter, param);
1316 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1317 if (chan) {
1318 dma_addr_t dma[2];
1319 void *buf[2];
1320 int i;
1321
1322 s->chan_rx = chan;
1323
1324 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1325 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1326 &dma[0], GFP_KERNEL);
1327
1328 if (!buf[0]) {
1329 dev_warn(port->dev,
1330 "failed to allocate dma buffer, using PIO\n");
1331 sci_rx_dma_release(s, true);
1332 return;
1333 }
1334
1335 buf[1] = buf[0] + s->buf_len_rx;
1336 dma[1] = dma[0] + s->buf_len_rx;
1337
1338 for (i = 0; i < 2; i++) {
1339 struct scatterlist *sg = &s->sg_rx[i];
1340
1341 sg_init_table(sg, 1);
1342 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1343 (int)buf[i] & ~PAGE_MASK);
Magnus Dammf354a382010-03-19 04:47:01 +00001344 sg_dma_address(sg) = dma[i];
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001345 }
1346
1347 INIT_WORK(&s->work_rx, work_fn_rx);
1348 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1349
1350 sci_submit_rx(s);
1351 }
1352}
1353
1354static void sci_free_dma(struct uart_port *port)
1355{
1356 struct sci_port *s = to_sci_port(port);
1357
1358 if (!s->dma_dev)
1359 return;
1360
1361 if (s->chan_tx)
1362 sci_tx_dma_release(s, false);
1363 if (s->chan_rx)
1364 sci_rx_dma_release(s, false);
1365}
1366#endif
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368static int sci_startup(struct uart_port *port)
1369{
Magnus Damma5660ad2009-01-21 15:14:38 +00001370 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001372 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1373
Paul Mundte108b2c2006-09-27 16:32:13 +09001374 if (s->enable)
1375 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 sci_request_irq(s);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001378#ifdef CONFIG_SERIAL_SH_SCI_DMA
1379 sci_request_dma(port);
1380#endif
Yoshinori Satod6569012005-10-14 15:59:12 -07001381 sci_start_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001382 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 return 0;
1385}
1386
1387static void sci_shutdown(struct uart_port *port)
1388{
Magnus Damma5660ad2009-01-21 15:14:38 +00001389 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001391 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +01001394 sci_stop_tx(port);
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001395#ifdef CONFIG_SERIAL_SH_SCI_DMA
1396 sci_free_dma(port);
1397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 sci_free_irq(s);
1399
Paul Mundte108b2c2006-09-27 16:32:13 +09001400 if (s->disable)
1401 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Alan Cox606d0992006-12-08 02:38:45 -08001404static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1405 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Magnus Damm154280f2009-12-22 03:37:28 +00001407 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +09001408 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Magnus Damm154280f2009-12-22 03:37:28 +00001410 /*
1411 * earlyprintk comes here early on with port->uartclk set to zero.
1412 * the clock framework is not up and running at this point so here
1413 * we assume that 115200 is the maximum baud rate. please note that
1414 * the baud rate is not programmed during earlyprintk - it is assumed
1415 * that the previous boot loader has enabled required clocks and
1416 * setup the baud rate generator hardware for us already.
1417 */
1418 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1419
1420 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1421 if (likely(baud && port->uartclk))
Paul Mundta2159b52008-10-02 19:09:13 +09001422 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Paul Mundte108b2c2006-09-27 16:32:13 +09001424 do {
1425 status = sci_in(port, SCxSR);
1426 } while (!(status & SCxSR_TEND(port)));
1427
1428 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1429
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +09001430 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +09001431 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +09001432
1433 smr_val = sci_in(port, SCSMR) & 3;
1434 if ((termios->c_cflag & CSIZE) == CS7)
1435 smr_val |= 0x40;
1436 if (termios->c_cflag & PARENB)
1437 smr_val |= 0x20;
1438 if (termios->c_cflag & PARODD)
1439 smr_val |= 0x30;
1440 if (termios->c_cflag & CSTOPB)
1441 smr_val |= 0x08;
1442
1443 uart_update_timeout(port, termios->c_cflag, baud);
1444
1445 sci_out(port, SCSMR, smr_val);
1446
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001447 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1448 SCSCR_INIT(port));
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001451 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1453 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001454 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 sci_out(port, SCBRR, t);
1458 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1459 }
1460
Paul Mundtd5701642008-12-16 20:07:27 +09001461 sci_init_pins(port, termios->c_cflag);
1462 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -08001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 sci_out(port, SCSCR, SCSCR_INIT(port));
1465
1466 if ((termios->c_cflag & CREAD) != 0)
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001467 sci_start_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static const char *sci_type(struct uart_port *port)
1471{
1472 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001473 case PORT_IRDA:
1474 return "irda";
1475 case PORT_SCI:
1476 return "sci";
1477 case PORT_SCIF:
1478 return "scif";
1479 case PORT_SCIFA:
1480 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Paul Mundtfa439722008-09-04 18:53:58 +09001483 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486static void sci_release_port(struct uart_port *port)
1487{
1488 /* Nothing here yet .. */
1489}
1490
1491static int sci_request_port(struct uart_port *port)
1492{
1493 /* Nothing here yet .. */
1494 return 0;
1495}
1496
1497static void sci_config_port(struct uart_port *port, int flags)
1498{
Magnus Damma5660ad2009-01-21 15:14:38 +00001499 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 port->type = s->type;
1502
Magnus Damm08f8cb32009-01-21 15:14:22 +00001503 if (port->membase)
1504 return;
1505
1506 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +09001507 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001508
1509 if (IS_ERR(port->membase))
1510 dev_err(port->dev, "can't remap port#%d\n", port->line);
1511 } else {
1512 /*
1513 * For the simple (and majority of) cases where we don't
1514 * need to do any remapping, just cast the cookie
1515 * directly.
1516 */
1517 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1522{
Magnus Damma5660ad2009-01-21 15:14:38 +00001523 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Yinghai Lua62c4132008-08-19 20:49:55 -07001525 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EINVAL;
1527 if (ser->baud_base < 2400)
1528 /* No paper tape reader for Mitch.. */
1529 return -EINVAL;
1530
1531 return 0;
1532}
1533
1534static struct uart_ops sci_uart_ops = {
1535 .tx_empty = sci_tx_empty,
1536 .set_mctrl = sci_set_mctrl,
1537 .get_mctrl = sci_get_mctrl,
1538 .start_tx = sci_start_tx,
1539 .stop_tx = sci_stop_tx,
1540 .stop_rx = sci_stop_rx,
1541 .enable_ms = sci_enable_ms,
1542 .break_ctl = sci_break_ctl,
1543 .startup = sci_startup,
1544 .shutdown = sci_shutdown,
1545 .set_termios = sci_set_termios,
1546 .type = sci_type,
1547 .release_port = sci_release_port,
1548 .request_port = sci_request_port,
1549 .config_port = sci_config_port,
1550 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001551#ifdef CONFIG_CONSOLE_POLL
1552 .poll_get_char = sci_poll_get_char,
1553 .poll_put_char = sci_poll_put_char,
1554#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555};
1556
Magnus Damm501b8252009-01-21 15:14:30 +00001557static void __devinit sci_init_single(struct platform_device *dev,
1558 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001559 unsigned int index,
1560 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001561{
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001562 struct uart_port *port = &sci_port->port;
1563
1564 port->ops = &sci_uart_ops;
1565 port->iotype = UPIO_MEM;
1566 port->line = index;
Markus Pietrek75136d42010-01-15 08:33:20 +09001567
1568 switch (p->type) {
1569 case PORT_SCIFA:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001570 port->fifosize = 64;
Markus Pietrek75136d42010-01-15 08:33:20 +09001571 break;
1572 case PORT_SCIF:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001573 port->fifosize = 16;
Markus Pietrek75136d42010-01-15 08:33:20 +09001574 break;
1575 default:
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001576 port->fifosize = 1;
Markus Pietrek75136d42010-01-15 08:33:20 +09001577 break;
1578 }
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001579
1580 if (dev) {
1581 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
1582 sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
1583 sci_port->enable = sci_clk_enable;
1584 sci_port->disable = sci_clk_disable;
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001585 port->dev = &dev->dev;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001586 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001587
Magnus Damm7ed7e072009-01-21 15:14:14 +00001588 sci_port->break_timer.data = (unsigned long)sci_port;
1589 sci_port->break_timer.function = sci_break_timer;
1590 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001591
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001592 port->mapbase = p->mapbase;
1593 port->membase = p->membase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001594
Guennadi Liakhovetski73a19e42010-03-02 11:39:15 +09001595 port->irq = p->irqs[SCIx_TXI_IRQ];
1596 port->flags = p->flags;
1597 sci_port->type = port->type = p->type;
1598
1599#ifdef CONFIG_SERIAL_SH_SCI_DMA
1600 sci_port->dma_dev = p->dma_dev;
1601 sci_port->slave_tx = p->dma_slave_tx;
1602 sci_port->slave_rx = p->dma_slave_rx;
1603
1604 dev_dbg(port->dev, "%s: DMA device %p, tx %d, rx %d\n", __func__,
1605 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1606#endif
Magnus Damm7ed7e072009-01-21 15:14:14 +00001607
1608 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundte108b2c2006-09-27 16:32:13 +09001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001612static struct tty_driver *serial_console_device(struct console *co, int *index)
1613{
1614 struct uart_driver *p = &sci_uart_driver;
1615 *index = co->index;
1616 return p->tty_driver;
1617}
1618
1619static void serial_console_putchar(struct uart_port *port, int ch)
1620{
1621 sci_poll_put_char(port, ch);
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624/*
1625 * Print a string to the serial port trying not to disturb
1626 * any possible real use of the port...
1627 */
1628static void serial_console_write(struct console *co, const char *s,
1629 unsigned count)
1630{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001631 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001632 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001633 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001634
Magnus Damm501b8252009-01-21 15:14:30 +00001635 if (sci_port->enable)
1636 sci_port->enable(port);
1637
1638 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001639
1640 /* wait until fifo is empty and last bit has been transmitted */
1641 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1642 while ((sci_in(port, SCxSR) & bits) != bits)
1643 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001644
Magnus Damm345e5a72009-11-05 14:34:57 +00001645 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001646 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001649static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001651 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct uart_port *port;
1653 int baud = 115200;
1654 int bits = 8;
1655 int parity = 'n';
1656 int flow = 'n';
1657 int ret;
1658
Paul Mundte108b2c2006-09-27 16:32:13 +09001659 /*
1660 * Check whether an invalid uart number has been specified, and
1661 * if so, search for the first available port that does have
1662 * console support.
1663 */
1664 if (co->index >= SCI_NPORTS)
1665 co->index = 0;
1666
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001667 if (co->data) {
1668 port = co->data;
1669 sci_port = to_sci_port(port);
1670 } else {
1671 sci_port = &sci_ports[co->index];
1672 port = &sci_port->port;
1673 co->data = port;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001677 * Also need to check port->type, we don't actually have any
1678 * UPIO_PORT ports, but uart_report_port() handily misreports
1679 * it anyways if we don't have a port available by the time this is
1680 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001682 if (!port->type)
1683 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001684
Magnus Damm08f8cb32009-01-21 15:14:22 +00001685 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001686
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001687 if (sci_port->enable)
1688 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (options)
1691 uart_parse_options(options, &baud, &parity, &bits, &flow);
1692
1693 ret = uart_set_options(port, co, baud, parity, bits, flow);
1694#if defined(__H8300H__) || defined(__H8300S__)
1695 /* disable rx interrupt */
1696 if (ret == 0)
1697 sci_stop_rx(port);
1698#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001699 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return ret;
1701}
1702
1703static struct console serial_console = {
1704 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001705 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 .write = serial_console_write,
1707 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001708 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710};
1711
1712static int __init sci_console_init(void)
1713{
1714 register_console(&serial_console);
1715 return 0;
1716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001718
1719static struct sci_port early_serial_port;
1720static struct console early_serial_console = {
1721 .name = "early_ttySC",
1722 .write = serial_console_write,
1723 .flags = CON_PRINTBUFFER,
1724};
1725static char early_serial_buf[32];
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1728
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001729#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001730#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001732#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733#endif
1734
1735static char banner[] __initdata =
1736 KERN_INFO "SuperH SCI(F) driver initialized\n";
1737
1738static struct uart_driver sci_uart_driver = {
1739 .owner = THIS_MODULE,
1740 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 .dev_name = "ttySC",
1742 .major = SCI_MAJOR,
1743 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001744 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .cons = SCI_CONSOLE,
1746};
1747
Magnus Damme552de22009-01-21 15:13:42 +00001748
Paul Mundt54507f62009-05-08 23:48:33 +09001749static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001750{
1751 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1752 struct sci_port *p;
1753 unsigned long flags;
1754
Magnus Damme552de22009-01-21 15:13:42 +00001755 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001756
1757 spin_lock_irqsave(&priv->lock, flags);
1758 list_for_each_entry(p, &priv->ports, node)
1759 uart_remove_one_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001760 spin_unlock_irqrestore(&priv->lock, flags);
1761
1762 kfree(priv);
1763 return 0;
1764}
1765
Magnus Damm0ee70712009-01-21 15:13:50 +00001766static int __devinit sci_probe_single(struct platform_device *dev,
1767 unsigned int index,
1768 struct plat_sci_port *p,
1769 struct sci_port *sciport)
1770{
1771 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1772 unsigned long flags;
1773 int ret;
1774
1775 /* Sanity check */
1776 if (unlikely(index >= SCI_NPORTS)) {
1777 dev_notice(&dev->dev, "Attempting to register port "
1778 "%d when only %d are available.\n",
1779 index+1, SCI_NPORTS);
1780 dev_notice(&dev->dev, "Consider bumping "
1781 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1782 return 0;
1783 }
1784
Magnus Damm501b8252009-01-21 15:14:30 +00001785 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001786
1787 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001788 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001789 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001790
1791 INIT_LIST_HEAD(&sciport->node);
1792
1793 spin_lock_irqsave(&priv->lock, flags);
1794 list_add(&sciport->node, &priv->ports);
1795 spin_unlock_irqrestore(&priv->lock, flags);
1796
1797 return 0;
1798}
1799
Paul Mundte108b2c2006-09-27 16:32:13 +09001800/*
1801 * Register a set of serial devices attached to a platform device. The
1802 * list is terminated with a zero flags entry, which means we expect
1803 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1804 * remapping (such as sh64) should also set UPF_IOREMAP.
1805 */
1806static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Paul Mundte108b2c2006-09-27 16:32:13 +09001808 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001809 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001810 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001811
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001812#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1813 if (is_early_platform_device(dev)) {
1814 if (dev->id == -1)
1815 return -ENOTSUPP;
1816 early_serial_console.index = dev->id;
1817 early_serial_console.data = &early_serial_port.port;
1818 sci_init_single(NULL, &early_serial_port, dev->id, p);
1819 serial_console_setup(&early_serial_console, early_serial_buf);
1820 if (!strstr(early_serial_buf, "keep"))
1821 early_serial_console.flags |= CON_BOOT;
1822 register_console(&early_serial_console);
1823 return 0;
1824 }
1825#endif
1826
Magnus Damme552de22009-01-21 15:13:42 +00001827 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1828 if (!priv)
1829 return -ENOMEM;
1830
1831 INIT_LIST_HEAD(&priv->ports);
1832 spin_lock_init(&priv->lock);
1833 platform_set_drvdata(dev, priv);
1834
Magnus Damme552de22009-01-21 15:13:42 +00001835 priv->clk_nb.notifier_call = sci_notifier;
1836 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Magnus Damm0ee70712009-01-21 15:13:50 +00001838 if (dev->id != -1) {
1839 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1840 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001841 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001842 } else {
1843 for (i = 0; p && p->flags != 0; p++, i++) {
1844 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1845 if (ret)
1846 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001847 }
Magnus Damme552de22009-01-21 15:13:42 +00001848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850#ifdef CONFIG_SH_STANDARD_BIOS
1851 sh_bios_gdb_detach();
1852#endif
1853
Paul Mundte108b2c2006-09-27 16:32:13 +09001854 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001855
1856err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001857 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001858 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001859}
1860
Paul Mundt6daa79b2009-06-15 07:07:38 +09001861static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001862{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001863 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001864 struct sci_port *p;
1865 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001866
Magnus Damme552de22009-01-21 15:13:42 +00001867 spin_lock_irqsave(&priv->lock, flags);
1868 list_for_each_entry(p, &priv->ports, node)
1869 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001870 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001871
1872 return 0;
1873}
1874
Paul Mundt6daa79b2009-06-15 07:07:38 +09001875static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001876{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001877 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001878 struct sci_port *p;
1879 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001880
Magnus Damme552de22009-01-21 15:13:42 +00001881 spin_lock_irqsave(&priv->lock, flags);
1882 list_for_each_entry(p, &priv->ports, node)
1883 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001884 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001885
1886 return 0;
1887}
1888
Alexey Dobriyan47145212009-12-14 18:00:08 -08001889static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001890 .suspend = sci_suspend,
1891 .resume = sci_resume,
1892};
1893
Paul Mundte108b2c2006-09-27 16:32:13 +09001894static struct platform_driver sci_driver = {
1895 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001896 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001897 .driver = {
1898 .name = "sh-sci",
1899 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001900 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001901 },
1902};
1903
1904static int __init sci_init(void)
1905{
1906 int ret;
1907
1908 printk(banner);
1909
Paul Mundte108b2c2006-09-27 16:32:13 +09001910 ret = uart_register_driver(&sci_uart_driver);
1911 if (likely(ret == 0)) {
1912 ret = platform_driver_register(&sci_driver);
1913 if (unlikely(ret))
1914 uart_unregister_driver(&sci_uart_driver);
1915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return ret;
1918}
1919
1920static void __exit sci_exit(void)
1921{
Paul Mundte108b2c2006-09-27 16:32:13 +09001922 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 uart_unregister_driver(&sci_uart_driver);
1924}
1925
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001926#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1927early_platform_init_buffer("earlyprintk", &sci_driver,
1928 early_serial_buf, ARRAY_SIZE(early_serial_buf));
1929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930module_init(sci_init);
1931module_exit(sci_exit);
1932
Paul Mundte108b2c2006-09-27 16:32:13 +09001933MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001934MODULE_ALIAS("platform:sh-sci");