blob: 37f0de9dd9ce555caff819a93b97148db4d61798 [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>
Paul Mundt85f094e2008-04-25 16:04:20 +090051
52#ifdef CONFIG_SUPERH
Paul Mundte108b2c2006-09-27 16:32:13 +090053#include <asm/sh_bios.h>
Paul Mundtb7a76e42006-02-01 03:06:06 -080054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Yoshinori Sato168f3622009-04-28 04:40:15 +000056#ifdef CONFIG_H8300
57#include <asm/gpio.h>
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "sh-sci.h"
61
Paul Mundte108b2c2006-09-27 16:32:13 +090062struct sci_port {
63 struct uart_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Paul Mundte108b2c2006-09-27 16:32:13 +090065 /* Port type */
66 unsigned int type;
67
68 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
Paul Mundt32351a22007-03-12 14:38:59 +090069 unsigned int irqs[SCIx_NR_IRQS];
Paul Mundte108b2c2006-09-27 16:32:13 +090070
Paul Mundte108b2c2006-09-27 16:32:13 +090071 /* Port enable callback */
72 void (*enable)(struct uart_port *port);
73
74 /* Port disable callback */
75 void (*disable)(struct uart_port *port);
76
77 /* Break timer */
78 struct timer_list break_timer;
79 int break_flag;
dmitry pervushin1534a3b2007-04-24 13:41:12 +090080
Magnus Damm501b8252009-01-21 15:14:30 +000081 /* Interface clock */
82 struct clk *iclk;
83 /* Data clock */
84 struct clk *dclk;
Paul Mundtedad1f22009-11-25 16:23:35 +090085
Magnus Damme552de22009-01-21 15:13:42 +000086 struct list_head node;
87};
88
89struct sh_sci_priv {
90 spinlock_t lock;
91 struct list_head ports;
Magnus Damme552de22009-01-21 15:13:42 +000092 struct notifier_block clk_nb;
Paul Mundte108b2c2006-09-27 16:32:13 +090093};
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* Function prototypes */
Russell Kingb129a8c2005-08-31 10:12:14 +010096static void sci_stop_tx(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Paul Mundte108b2c2006-09-27 16:32:13 +090098#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
99
100static struct sci_port sci_ports[SCI_NPORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct uart_driver sci_uart_driver;
102
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900103static inline struct sci_port *
104to_sci_port(struct uart_port *uart)
105{
106 return container_of(uart, struct sci_port, port);
107}
108
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900109#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900110
111#ifdef CONFIG_CONSOLE_POLL
Paul Mundte108b2c2006-09-27 16:32:13 +0900112static inline void handle_error(struct uart_port *port)
113{
114 /* Clear error flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
116}
117
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900118static int sci_poll_get_char(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned short status;
121 int c;
122
Paul Mundte108b2c2006-09-27 16:32:13 +0900123 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 status = sci_in(port, SCxSR);
125 if (status & SCxSR_ERRORS(port)) {
126 handle_error(port);
127 continue;
128 }
129 } while (!(status & SCxSR_RDxF(port)));
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 c = sci_in(port, SCxRDR);
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900132
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900133 /* Dummy read */
134 sci_in(port, SCxSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return c;
138}
Paul Mundt1f6fd5c2008-12-17 14:53:24 +0900139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900141static void sci_poll_put_char(struct uart_port *port, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned short status;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 do {
146 status = sci_in(port, SCxSR);
147 } while (!(status & SCxSR_TDxE(port)));
148
Paul Mundt272966c2008-11-13 17:46:06 +0900149 sci_out(port, SCxTDR, c);
SUGIOKA Toshinobudd0a3e72009-06-01 03:53:41 +0000150 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Paul Mundt07d2a1a2008-12-11 19:06:43 +0900152#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Paul Mundt15c73aa2008-10-02 19:47:12 +0900154#if defined(__H8300H__) || defined(__H8300S__)
Paul Mundtd5701642008-12-16 20:07:27 +0900155static void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int ch = (port->mapbase - SMR0) >> 3;
158
159 /* set DDR regs */
Paul Mundte108b2c2006-09-27 16:32:13 +0900160 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
161 h8300_sci_pins[ch].rx,
162 H8300_GPIO_INPUT);
163 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
164 h8300_sci_pins[ch].tx,
165 H8300_GPIO_OUTPUT);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* tx mark output*/
168 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
169}
Paul Mundtd5701642008-12-16 20:07:27 +0900170#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
171static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundte108b2c2006-09-27 16:32:13 +0900172{
Paul Mundtd5701642008-12-16 20:07:27 +0900173 if (port->mapbase == 0xA4400000) {
174 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
175 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
176 } else if (port->mapbase == 0xA4410000)
177 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
Nobuhiro Iwamatsu9465a542007-03-27 18:13:51 +0900178}
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900179#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
Paul Mundtd5701642008-12-16 20:07:27 +0900180static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900181{
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900182 unsigned short data;
183
184 if (cflag & CRTSCTS) {
185 /* enable RTS/CTS */
186 if (port->mapbase == 0xa4430000) { /* SCIF0 */
187 /* Clear PTCR bit 9-2; enable all scif pins but sck */
Paul Mundtd5701642008-12-16 20:07:27 +0900188 data = __raw_readw(PORT_PTCR);
189 __raw_writew((data & 0xfc03), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900190 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
191 /* Clear PVCR bit 9-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900192 data = __raw_readw(PORT_PVCR);
193 __raw_writew((data & 0xfc03), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900194 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900195 } else {
196 if (port->mapbase == 0xa4430000) { /* SCIF0 */
197 /* Clear PTCR bit 5-2; enable only tx and rx */
Paul Mundtd5701642008-12-16 20:07:27 +0900198 data = __raw_readw(PORT_PTCR);
199 __raw_writew((data & 0xffc3), PORT_PTCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900200 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
201 /* Clear PVCR bit 5-2 */
Paul Mundtd5701642008-12-16 20:07:27 +0900202 data = __raw_readw(PORT_PVCR);
203 __raw_writew((data & 0xffc3), PORT_PVCR);
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900204 }
205 }
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900206}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800207#elif defined(CONFIG_CPU_SH3)
Paul Mundte108b2c2006-09-27 16:32:13 +0900208/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
Paul Mundtd5701642008-12-16 20:07:27 +0900209static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Paul Mundtb7a76e42006-02-01 03:06:06 -0800211 unsigned short data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Paul Mundtb7a76e42006-02-01 03:06:06 -0800213 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900214 data = __raw_readw(SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800215 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
Paul Mundtd5701642008-12-16 20:07:27 +0900216 __raw_writew(data & 0x0fcf, SCPCR);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800217
Paul Mundtd5701642008-12-16 20:07:27 +0900218 if (!(cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* We need to set SCPCR to enable RTS/CTS */
Paul Mundtd5701642008-12-16 20:07:27 +0900220 data = __raw_readw(SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Clear out SCP7MD1,0, SCP4MD1,0,
222 Set SCP6MD1,0 = {01} (output) */
Paul Mundtd5701642008-12-16 20:07:27 +0900223 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Paul Mundt32b53072009-12-24 14:52:43 +0900225 data = __raw_readb(SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Set /RTS2 (bit6) = 0 */
Paul Mundt32b53072009-12-24 14:52:43 +0900227 __raw_writeb(data & 0xbf, SCPDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Paul Mundt41504c32006-12-11 20:28:03 +0900230#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
Paul Mundtd5701642008-12-16 20:07:27 +0900231static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
Paul Mundt41504c32006-12-11 20:28:03 +0900232{
Magnus Damm346b7462008-04-23 21:25:29 +0900233 unsigned short data;
Paul Mundt41504c32006-12-11 20:28:03 +0900234
Magnus Damm346b7462008-04-23 21:25:29 +0900235 if (port->mapbase == 0xffe00000) {
Paul Mundtd5701642008-12-16 20:07:27 +0900236 data = __raw_readw(PSCR);
Magnus Damm346b7462008-04-23 21:25:29 +0900237 data &= ~0x03cf;
Paul Mundtd5701642008-12-16 20:07:27 +0900238 if (!(cflag & CRTSCTS))
Magnus Damm346b7462008-04-23 21:25:29 +0900239 data |= 0x0340;
Paul Mundt41504c32006-12-11 20:28:03 +0900240
Paul Mundtd5701642008-12-16 20:07:27 +0900241 __raw_writew(data, PSCR);
Paul Mundt41504c32006-12-11 20:28:03 +0900242 }
Paul Mundt41504c32006-12-11 20:28:03 +0900243}
Yoshihiro Shimodac01f0f12009-08-21 16:30:28 +0900244#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
245 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Yoshihiro Shimoda7d740a02008-01-07 14:40:07 +0900246 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900247 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900248 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
Paul Mundt2b1bd1a2007-06-20 18:27:10 +0900249 defined(CONFIG_CPU_SUBTYPE_SHX3)
Paul Mundtd5701642008-12-16 20:07:27 +0900250static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
251{
252 if (!(cflag & CRTSCTS))
253 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
254}
Paul Mundtb0c50ad2008-12-22 03:40:10 +0900255#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
Paul Mundtd5701642008-12-16 20:07:27 +0900256static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
257{
258 if (!(cflag & CRTSCTS))
259 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
260}
Paul Mundtb7a76e42006-02-01 03:06:06 -0800261#else
Paul Mundtd5701642008-12-16 20:07:27 +0900262static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
263{
264 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Paul Mundte108b2c2006-09-27 16:32:13 +0900266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Paul Mundt32351a22007-03-12 14:38:59 +0900268#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
269 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
Kuninori Morimoto55ba99e2009-03-03 15:40:25 +0900270 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
271 defined(CONFIG_CPU_SUBTYPE_SH7786)
Paul Mundte108b2c2006-09-27 16:32:13 +0900272static inline int scif_txroom(struct uart_port *port)
273{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900274 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Paul Mundte108b2c2006-09-27 16:32:13 +0900275}
276
277static inline int scif_rxroom(struct uart_port *port)
278{
Yutaro Ebiharacae167d2008-03-11 13:58:50 +0900279 return sci_in(port, SCRFDR) & 0xff;
Paul Mundte108b2c2006-09-27 16:32:13 +0900280}
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900281#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
282static inline int scif_txroom(struct uart_port *port)
283{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900284 if ((port->mapbase == 0xffe00000) ||
285 (port->mapbase == 0xffe08000)) {
286 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900287 return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900288 } else {
289 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900290 return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900291 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900292}
293
294static inline int scif_rxroom(struct uart_port *port)
295{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900296 if ((port->mapbase == 0xffe00000) ||
297 (port->mapbase == 0xffe08000)) {
298 /* SCIF0/1*/
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900299 return sci_in(port, SCRFDR) & 0xff;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900300 } else {
301 /* SCIF2 */
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900302 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900303 }
Nobuhiro Iwamatsuc63847a2008-06-06 17:04:08 +0900304}
Paul Mundte108b2c2006-09-27 16:32:13 +0900305#else
306static inline int scif_txroom(struct uart_port *port)
307{
308 return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8);
309}
310
311static inline int scif_rxroom(struct uart_port *port)
312{
313 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Paul Mundte108b2c2006-09-27 16:32:13 +0900317static inline int sci_txroom(struct uart_port *port)
318{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900319 return (sci_in(port, SCxSR) & SCI_TDRE) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900320}
321
322static inline int sci_rxroom(struct uart_port *port)
323{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900324 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
Paul Mundte108b2c2006-09-27 16:32:13 +0900325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* ********************************************************************** *
328 * the interrupt related routines *
329 * ********************************************************************** */
330
331static void sci_transmit_chars(struct uart_port *port)
332{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700333 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned int stopped = uart_tx_stopped(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unsigned short status;
336 unsigned short ctrl;
Paul Mundte108b2c2006-09-27 16:32:13 +0900337 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 status = sci_in(port, SCxSR);
340 if (!(status & SCxSR_TDxE(port))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ctrl = sci_in(port, SCSCR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900342 if (uart_circ_empty(xmit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ctrl &= ~SCI_CTRL_FLAGS_TIE;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900344 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ctrl |= SCI_CTRL_FLAGS_TIE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
348 }
349
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900350 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900351 count = sci_txroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900352 else
353 count = scif_txroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 do {
356 unsigned char c;
357
358 if (port->x_char) {
359 c = port->x_char;
360 port->x_char = 0;
361 } else if (!uart_circ_empty(xmit) && !stopped) {
362 c = xmit->buf[xmit->tail];
363 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
364 } else {
365 break;
366 }
367
368 sci_out(port, SCxTDR, c);
369
370 port->icount.tx++;
371 } while (--count > 0);
372
373 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
374
375 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
376 uart_write_wakeup(port);
377 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100378 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ctrl = sci_in(port, SCSCR);
381
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900382 if (port->type != PORT_SCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 sci_in(port, SCxSR); /* Dummy read */
384 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 ctrl |= SCI_CTRL_FLAGS_TIE;
388 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390}
391
392/* On SH3, SCIF may read end-of-break as a space->mark char */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900393#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Howells7d12e782006-10-05 14:55:46 +0100395static inline void sci_receive_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900397 struct sci_port *sci_port = to_sci_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700398 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int i, count, copied = 0;
400 unsigned short status;
Alan Cox33f0f882006-01-09 20:54:13 -0800401 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 status = sci_in(port, SCxSR);
404 if (!(status & SCxSR_RDxF(port)))
405 return;
406
407 while (1) {
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900408 if (port->type == PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900409 count = sci_rxroom(port);
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900410 else
411 count = scif_rxroom(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Don't copy more bytes than there is room for in the buffer */
Alan Cox33f0f882006-01-09 20:54:13 -0800414 count = tty_buffer_request_room(tty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* If for any reason we can't copy more data, we're done! */
417 if (count == 0)
418 break;
419
420 if (port->type == PORT_SCI) {
421 char c = sci_in(port, SCxRDR);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900422 if (uart_handle_sysrq_char(port, c) ||
423 sci_port->break_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 count = 0;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900425 else
Paul Mundte108b2c2006-09-27 16:32:13 +0900426 tty_insert_flip_char(tty, c, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900428 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 char c = sci_in(port, SCxRDR);
430 status = sci_in(port, SCxSR);
431#if defined(CONFIG_CPU_SH3)
432 /* Skip "chars" during break */
Paul Mundte108b2c2006-09-27 16:32:13 +0900433 if (sci_port->break_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if ((c == 0) &&
435 (status & SCxSR_FER(port))) {
436 count--; i--;
437 continue;
438 }
Paul Mundte108b2c2006-09-27 16:32:13 +0900439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Nonzero => end-of-break */
Paul Mundt762c69e2008-12-16 18:55:26 +0900441 dev_dbg(port->dev, "debounce<%02x>\n", c);
Paul Mundte108b2c2006-09-27 16:32:13 +0900442 sci_port->break_flag = 0;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (STEPFN(c)) {
445 count--; i--;
446 continue;
447 }
448 }
449#endif /* CONFIG_CPU_SH3 */
David Howells7d12e782006-10-05 14:55:46 +0100450 if (uart_handle_sysrq_char(port, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 count--; i--;
452 continue;
453 }
454
455 /* Store data and status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (status&SCxSR_FER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800457 flag = TTY_FRAME;
Paul Mundt762c69e2008-12-16 18:55:26 +0900458 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else if (status&SCxSR_PER(port)) {
Alan Cox33f0f882006-01-09 20:54:13 -0800460 flag = TTY_PARITY;
Paul Mundt762c69e2008-12-16 18:55:26 +0900461 dev_notice(port->dev, "parity error\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800462 } else
463 flag = TTY_NORMAL;
Paul Mundt762c69e2008-12-16 18:55:26 +0900464
Alan Cox33f0f882006-01-09 20:54:13 -0800465 tty_insert_flip_char(tty, c, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 }
468
469 sci_in(port, SCxSR); /* dummy read */
470 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 copied += count;
473 port->icount.rx += count;
474 }
475
476 if (copied) {
477 /* Tell the rest of the system the news. New characters! */
478 tty_flip_buffer_push(tty);
479 } else {
480 sci_in(port, SCxSR); /* dummy read */
481 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
482 }
483}
484
485#define SCI_BREAK_JIFFIES (HZ/20)
486/* The sci generates interrupts during the break,
487 * 1 per millisecond or so during the break period, for 9600 baud.
488 * So dont bother disabling interrupts.
489 * But dont want more than 1 break event.
490 * Use a kernel timer to periodically poll the rx line until
491 * the break is finished.
492 */
493static void sci_schedule_break_timer(struct sci_port *port)
494{
495 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
496 add_timer(&port->break_timer);
497}
498/* Ensure that two consecutive samples find the break over. */
499static void sci_break_timer(unsigned long data)
500{
Paul Mundte108b2c2006-09-27 16:32:13 +0900501 struct sci_port *port = (struct sci_port *)data;
502
503 if (sci_rxd_in(&port->port) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 port->break_flag = 1;
Paul Mundte108b2c2006-09-27 16:32:13 +0900505 sci_schedule_break_timer(port);
506 } else if (port->break_flag == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* break is over. */
508 port->break_flag = 2;
Paul Mundte108b2c2006-09-27 16:32:13 +0900509 sci_schedule_break_timer(port);
510 } else
511 port->break_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514static inline int sci_handle_errors(struct uart_port *port)
515{
516 int copied = 0;
517 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700518 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Paul Mundte108b2c2006-09-27 16:32:13 +0900520 if (status & SCxSR_ORER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* overrun error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900522 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
Alan Cox33f0f882006-01-09 20:54:13 -0800523 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900524
525 dev_notice(port->dev, "overrun error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
Paul Mundte108b2c2006-09-27 16:32:13 +0900528 if (status & SCxSR_FER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (sci_rxd_in(port) == 0) {
530 /* Notify of BREAK */
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900531 struct sci_port *sci_port = to_sci_port(port);
Paul Mundte108b2c2006-09-27 16:32:13 +0900532
533 if (!sci_port->break_flag) {
534 sci_port->break_flag = 1;
535 sci_schedule_break_timer(sci_port);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* Do sysrq handling. */
Paul Mundte108b2c2006-09-27 16:32:13 +0900538 if (uart_handle_break(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
Paul Mundt762c69e2008-12-16 18:55:26 +0900540
541 dev_dbg(port->dev, "BREAK detected\n");
542
Paul Mundte108b2c2006-09-27 16:32:13 +0900543 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900544 copied++;
545 }
546
Paul Mundte108b2c2006-09-27 16:32:13 +0900547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* frame error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900549 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
Alan Cox33f0f882006-01-09 20:54:13 -0800550 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900551
552 dev_notice(port->dev, "frame error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 }
555
Paul Mundte108b2c2006-09-27 16:32:13 +0900556 if (status & SCxSR_PER(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* parity error */
Paul Mundte108b2c2006-09-27 16:32:13 +0900558 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
559 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900560
561 dev_notice(port->dev, "parity error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Alan Cox33f0f882006-01-09 20:54:13 -0800564 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return copied;
568}
569
Paul Mundtd830fa42008-12-16 19:29:38 +0900570static inline int sci_handle_fifo_overrun(struct uart_port *port)
571{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700572 struct tty_struct *tty = port->state->port.tty;
Paul Mundtd830fa42008-12-16 19:29:38 +0900573 int copied = 0;
574
575 if (port->type != PORT_SCIF)
576 return 0;
577
578 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
579 sci_out(port, SCLSR, 0);
580
581 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
582 tty_flip_buffer_push(tty);
583
584 dev_notice(port->dev, "overrun error\n");
585 copied++;
586 }
587
588 return copied;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static inline int sci_handle_breaks(struct uart_port *port)
592{
593 int copied = 0;
594 unsigned short status = sci_in(port, SCxSR);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700595 struct tty_struct *tty = port->state->port.tty;
Magnus Damma5660ad2009-01-21 15:14:38 +0000596 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Paul Mundt0b3d4ef2007-03-14 13:22:37 +0900598 if (uart_handle_break(port))
599 return 0;
600
Paul Mundtb7a76e42006-02-01 03:06:06 -0800601 if (!s->break_flag && status & SCxSR_BRK(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#if defined(CONFIG_CPU_SH3)
603 /* Debounce break */
604 s->break_flag = 1;
605#endif
606 /* Notify of BREAK */
Paul Mundte108b2c2006-09-27 16:32:13 +0900607 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
Alan Cox33f0f882006-01-09 20:54:13 -0800608 copied++;
Paul Mundt762c69e2008-12-16 18:55:26 +0900609
610 dev_dbg(port->dev, "BREAK detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Alan Cox33f0f882006-01-09 20:54:13 -0800613 if (copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 tty_flip_buffer_push(tty);
Paul Mundte108b2c2006-09-27 16:32:13 +0900615
Paul Mundtd830fa42008-12-16 19:29:38 +0900616 copied += sci_handle_fifo_overrun(port);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return copied;
619}
620
David Howells7d12e782006-10-05 14:55:46 +0100621static irqreturn_t sci_rx_interrupt(int irq, void *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* I think sci_receive_chars has to be called irrespective
624 * of whether the I_IXOFF is set, otherwise, how is the interrupt
625 * to be disabled?
626 */
David Howells7d12e782006-10-05 14:55:46 +0100627 sci_receive_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return IRQ_HANDLED;
630}
631
David Howells7d12e782006-10-05 14:55:46 +0100632static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct uart_port *port = ptr;
Stuart Menefyfd78a762009-07-29 23:01:24 +0900635 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Stuart Menefyfd78a762009-07-29 23:01:24 +0900637 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 sci_transmit_chars(port);
Stuart Menefyfd78a762009-07-29 23:01:24 +0900639 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return IRQ_HANDLED;
642}
643
David Howells7d12e782006-10-05 14:55:46 +0100644static irqreturn_t sci_er_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct uart_port *port = ptr;
647
648 /* Handle errors */
649 if (port->type == PORT_SCI) {
650 if (sci_handle_errors(port)) {
651 /* discard character in rx buffer */
652 sci_in(port, SCxSR);
653 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
654 }
655 } else {
Paul Mundtd830fa42008-12-16 19:29:38 +0900656 sci_handle_fifo_overrun(port);
David Howells7d12e782006-10-05 14:55:46 +0100657 sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
660 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
661
662 /* Kick the transmission */
David Howells7d12e782006-10-05 14:55:46 +0100663 sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return IRQ_HANDLED;
666}
667
David Howells7d12e782006-10-05 14:55:46 +0100668static irqreturn_t sci_br_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct uart_port *port = ptr;
671
672 /* Handle BREAKs */
673 sci_handle_breaks(port);
674 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
675
676 return IRQ_HANDLED;
677}
678
David Howells7d12e782006-10-05 14:55:46 +0100679static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Magnus Damm44e18e92009-07-03 08:39:34 +0000681 unsigned short ssr_status, scr_status, err_enabled;
Michael Trimarchia8884e32008-10-31 16:10:23 +0900682 struct uart_port *port = ptr;
683 irqreturn_t ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900685 ssr_status = sci_in(port, SCxSR);
686 scr_status = sci_in(port, SCSCR);
Magnus Damm44e18e92009-07-03 08:39:34 +0000687 err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* Tx Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000690 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900691 ret = sci_tx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Rx Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000693 if ((ssr_status & SCxSR_RDxF(port)) && (scr_status & SCI_CTRL_FLAGS_RIE))
Michael Trimarchia8884e32008-10-31 16:10:23 +0900694 ret = sci_rx_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Error Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000696 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900697 ret = sci_er_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* Break Interrupt */
SUGIOKA Toshinobudd4da3a2009-07-07 05:32:07 +0000699 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
Michael Trimarchia8884e32008-10-31 16:10:23 +0900700 ret = sci_br_interrupt(irq, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Michael Trimarchia8884e32008-10-31 16:10:23 +0900702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * Here we define a transistion notifier so that we can update all of our
707 * ports' baud rate when the peripheral clock changes.
708 */
Paul Mundte108b2c2006-09-27 16:32:13 +0900709static int sci_notifier(struct notifier_block *self,
710 unsigned long phase, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Magnus Damme552de22009-01-21 15:13:42 +0000712 struct sh_sci_priv *priv = container_of(self,
713 struct sh_sci_priv, clk_nb);
714 struct sci_port *sci_port;
715 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if ((phase == CPUFREQ_POSTCHANGE) ||
Magnus Damme552de22009-01-21 15:13:42 +0000718 (phase == CPUFREQ_RESUMECHANGE)) {
719 spin_lock_irqsave(&priv->lock, flags);
720 list_for_each_entry(sci_port, &priv->ports, node)
Magnus Damm501b8252009-01-21 15:14:30 +0000721 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
Magnus Damme552de22009-01-21 15:13:42 +0000722 spin_unlock_irqrestore(&priv->lock, flags);
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return NOTIFY_OK;
726}
Magnus Damm501b8252009-01-21 15:14:30 +0000727
728static void sci_clk_enable(struct uart_port *port)
729{
730 struct sci_port *sci_port = to_sci_port(port);
731
732 clk_enable(sci_port->dclk);
733 sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
734
735 if (sci_port->iclk)
736 clk_enable(sci_port->iclk);
737}
738
739static void sci_clk_disable(struct uart_port *port)
740{
741 struct sci_port *sci_port = to_sci_port(port);
742
743 if (sci_port->iclk)
744 clk_disable(sci_port->iclk);
745
746 clk_disable(sci_port->dclk);
747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749static int sci_request_irq(struct sci_port *port)
750{
751 int i;
David Howells7d12e782006-10-05 14:55:46 +0100752 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
754 sci_br_interrupt,
755 };
756 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
757 "SCI Transmit Data Empty", "SCI Break" };
758
759 if (port->irqs[0] == port->irqs[1]) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900760 if (unlikely(!port->irqs[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +0900762
763 if (request_irq(port->irqs[0], sci_mpxed_interrupt,
Paul Mundt35f3c512006-10-06 15:31:16 +0900764 IRQF_DISABLED, "sci", port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900765 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -ENODEV;
767 }
768 } else {
769 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900770 if (unlikely(!port->irqs[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 continue;
Paul Mundt762c69e2008-12-16 18:55:26 +0900772
Paul Mundte108b2c2006-09-27 16:32:13 +0900773 if (request_irq(port->irqs[i], handlers[i],
Paul Mundt35f3c512006-10-06 15:31:16 +0900774 IRQF_DISABLED, desc[i], port)) {
Paul Mundt762c69e2008-12-16 18:55:26 +0900775 dev_err(port->port.dev, "Can't allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENODEV;
777 }
778 }
779 }
780
781 return 0;
782}
783
784static void sci_free_irq(struct sci_port *port)
785{
786 int i;
787
Paul Mundt762c69e2008-12-16 18:55:26 +0900788 if (port->irqs[0] == port->irqs[1])
789 free_irq(port->irqs[0], port);
790 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
792 if (!port->irqs[i])
793 continue;
794
795 free_irq(port->irqs[i], port);
796 }
797 }
798}
799
800static unsigned int sci_tx_empty(struct uart_port *port)
801{
Guennadi Liakhovetskib1516802009-12-01 09:54:46 +0000802 unsigned short status = sci_in(port, SCxSR);
803 return status & SCxSR_TEND(port) ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
807{
808 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
809 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
810 /* If you have signals for DTR and DCD, please implement here. */
811}
812
813static unsigned int sci_get_mctrl(struct uart_port *port)
814{
815 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
816 and CTS/RTS */
817
818 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
819}
820
Russell Kingb129a8c2005-08-31 10:12:14 +0100821static void sci_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Paul Mundte108b2c2006-09-27 16:32:13 +0900823 unsigned short ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Paul Mundte108b2c2006-09-27 16:32:13 +0900825 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
826 ctrl = sci_in(port, SCSCR);
827 ctrl |= SCI_CTRL_FLAGS_TIE;
828 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Russell Kingb129a8c2005-08-31 10:12:14 +0100831static void sci_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 unsigned short ctrl;
834
835 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ctrl = sci_in(port, SCSCR);
837 ctrl &= ~SCI_CTRL_FLAGS_TIE;
838 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 unsigned short ctrl;
844
845 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ctrl = sci_in(port, SCSCR);
847 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
848 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851static void sci_stop_rx(struct uart_port *port)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned short ctrl;
854
855 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ctrl = sci_in(port, SCSCR);
857 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
858 sci_out(port, SCSCR, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static void sci_enable_ms(struct uart_port *port)
862{
863 /* Nothing here yet .. */
864}
865
866static void sci_break_ctl(struct uart_port *port, int break_state)
867{
868 /* Nothing here yet .. */
869}
870
871static int sci_startup(struct uart_port *port)
872{
Magnus Damma5660ad2009-01-21 15:14:38 +0000873 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Paul Mundte108b2c2006-09-27 16:32:13 +0900875 if (s->enable)
876 s->enable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 sci_request_irq(s);
Yoshinori Satod6569012005-10-14 15:59:12 -0700879 sci_start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sci_start_rx(port, 1);
881
882 return 0;
883}
884
885static void sci_shutdown(struct uart_port *port)
886{
Magnus Damma5660ad2009-01-21 15:14:38 +0000887 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 sci_stop_rx(port);
Russell Kingb129a8c2005-08-31 10:12:14 +0100890 sci_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 sci_free_irq(s);
892
Paul Mundte108b2c2006-09-27 16:32:13 +0900893 if (s->disable)
894 s->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Alan Cox606d0992006-12-08 02:38:45 -0800897static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
898 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Magnus Damm154280f2009-12-22 03:37:28 +0000900 unsigned int status, baud, smr_val, max_baud;
Paul Mundta2159b52008-10-02 19:09:13 +0900901 int t = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Magnus Damm154280f2009-12-22 03:37:28 +0000903 /*
904 * earlyprintk comes here early on with port->uartclk set to zero.
905 * the clock framework is not up and running at this point so here
906 * we assume that 115200 is the maximum baud rate. please note that
907 * the baud rate is not programmed during earlyprintk - it is assumed
908 * that the previous boot loader has enabled required clocks and
909 * setup the baud rate generator hardware for us already.
910 */
911 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
912
913 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
914 if (likely(baud && port->uartclk))
Paul Mundta2159b52008-10-02 19:09:13 +0900915 t = SCBRR_VALUE(baud, port->uartclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Paul Mundte108b2c2006-09-27 16:32:13 +0900917 do {
918 status = sci_in(port, SCxSR);
919 } while (!(status & SCxSR_TEND(port)));
920
921 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
922
Yoshihiro Shimoda1a22f082008-11-11 12:19:05 +0900923 if (port->type != PORT_SCI)
Paul Mundte108b2c2006-09-27 16:32:13 +0900924 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
Paul Mundte108b2c2006-09-27 16:32:13 +0900925
926 smr_val = sci_in(port, SCSMR) & 3;
927 if ((termios->c_cflag & CSIZE) == CS7)
928 smr_val |= 0x40;
929 if (termios->c_cflag & PARENB)
930 smr_val |= 0x20;
931 if (termios->c_cflag & PARODD)
932 smr_val |= 0x30;
933 if (termios->c_cflag & CSTOPB)
934 smr_val |= 0x08;
935
936 uart_update_timeout(port, termios->c_cflag, baud);
937
938 sci_out(port, SCSMR, smr_val);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (t > 0) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900941 if (t >= 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
943 t >>= 2;
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900944 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sci_out(port, SCBRR, t);
948 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
949 }
950
Paul Mundtd5701642008-12-16 20:07:27 +0900951 sci_init_pins(port, termios->c_cflag);
952 sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
Paul Mundtb7a76e42006-02-01 03:06:06 -0800953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sci_out(port, SCSCR, SCSCR_INIT(port));
955
956 if ((termios->c_cflag & CREAD) != 0)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900957 sci_start_rx(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static const char *sci_type(struct uart_port *port)
961{
962 switch (port->type) {
Michael Trimarchie7c98dc2008-11-13 18:18:35 +0900963 case PORT_IRDA:
964 return "irda";
965 case PORT_SCI:
966 return "sci";
967 case PORT_SCIF:
968 return "scif";
969 case PORT_SCIFA:
970 return "scifa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Paul Mundtfa439722008-09-04 18:53:58 +0900973 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static void sci_release_port(struct uart_port *port)
977{
978 /* Nothing here yet .. */
979}
980
981static int sci_request_port(struct uart_port *port)
982{
983 /* Nothing here yet .. */
984 return 0;
985}
986
987static void sci_config_port(struct uart_port *port, int flags)
988{
Magnus Damma5660ad2009-01-21 15:14:38 +0000989 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 port->type = s->type;
992
Magnus Damm08f8cb32009-01-21 15:14:22 +0000993 if (port->membase)
994 return;
995
996 if (port->flags & UPF_IOREMAP) {
Paul Mundt7ff731a2008-10-01 15:46:58 +0900997 port->membase = ioremap_nocache(port->mapbase, 0x40);
Magnus Damm08f8cb32009-01-21 15:14:22 +0000998
999 if (IS_ERR(port->membase))
1000 dev_err(port->dev, "can't remap port#%d\n", port->line);
1001 } else {
1002 /*
1003 * For the simple (and majority of) cases where we don't
1004 * need to do any remapping, just cast the cookie
1005 * directly.
1006 */
1007 port->membase = (void __iomem *)port->mapbase;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1012{
Magnus Damma5660ad2009-01-21 15:14:38 +00001013 struct sci_port *s = to_sci_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Yinghai Lua62c4132008-08-19 20:49:55 -07001015 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EINVAL;
1017 if (ser->baud_base < 2400)
1018 /* No paper tape reader for Mitch.. */
1019 return -EINVAL;
1020
1021 return 0;
1022}
1023
1024static struct uart_ops sci_uart_ops = {
1025 .tx_empty = sci_tx_empty,
1026 .set_mctrl = sci_set_mctrl,
1027 .get_mctrl = sci_get_mctrl,
1028 .start_tx = sci_start_tx,
1029 .stop_tx = sci_stop_tx,
1030 .stop_rx = sci_stop_rx,
1031 .enable_ms = sci_enable_ms,
1032 .break_ctl = sci_break_ctl,
1033 .startup = sci_startup,
1034 .shutdown = sci_shutdown,
1035 .set_termios = sci_set_termios,
1036 .type = sci_type,
1037 .release_port = sci_release_port,
1038 .request_port = sci_request_port,
1039 .config_port = sci_config_port,
1040 .verify_port = sci_verify_port,
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001041#ifdef CONFIG_CONSOLE_POLL
1042 .poll_get_char = sci_poll_get_char,
1043 .poll_put_char = sci_poll_put_char,
1044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
1046
Magnus Damm501b8252009-01-21 15:14:30 +00001047static void __devinit sci_init_single(struct platform_device *dev,
1048 struct sci_port *sci_port,
Magnus Damm08f8cb32009-01-21 15:14:22 +00001049 unsigned int index,
1050 struct plat_sci_port *p)
Paul Mundte108b2c2006-09-27 16:32:13 +09001051{
Magnus Damm7ed7e072009-01-21 15:14:14 +00001052 sci_port->port.ops = &sci_uart_ops;
1053 sci_port->port.iotype = UPIO_MEM;
1054 sci_port->port.line = index;
1055 sci_port->port.fifosize = 1;
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001056
1057 if (dev) {
1058 sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
1059 sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
1060 sci_port->enable = sci_clk_enable;
1061 sci_port->disable = sci_clk_disable;
1062 sci_port->port.dev = &dev->dev;
1063 }
Paul Mundte108b2c2006-09-27 16:32:13 +09001064
Magnus Damm7ed7e072009-01-21 15:14:14 +00001065 sci_port->break_timer.data = (unsigned long)sci_port;
1066 sci_port->break_timer.function = sci_break_timer;
1067 init_timer(&sci_port->break_timer);
Paul Mundte108b2c2006-09-27 16:32:13 +09001068
Magnus Damm7ed7e072009-01-21 15:14:14 +00001069 sci_port->port.mapbase = p->mapbase;
Magnus Damm7ed7e072009-01-21 15:14:14 +00001070 sci_port->port.membase = p->membase;
1071
1072 sci_port->port.irq = p->irqs[SCIx_TXI_IRQ];
1073 sci_port->port.flags = p->flags;
1074 sci_port->type = sci_port->port.type = p->type;
1075
1076 memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
Paul Mundte108b2c2006-09-27 16:32:13 +09001077}
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001080static struct tty_driver *serial_console_device(struct console *co, int *index)
1081{
1082 struct uart_driver *p = &sci_uart_driver;
1083 *index = co->index;
1084 return p->tty_driver;
1085}
1086
1087static void serial_console_putchar(struct uart_port *port, int ch)
1088{
1089 sci_poll_put_char(port, ch);
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/*
1093 * Print a string to the serial port trying not to disturb
1094 * any possible real use of the port...
1095 */
1096static void serial_console_write(struct console *co, const char *s,
1097 unsigned count)
1098{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001099 struct uart_port *port = co->data;
Magnus Damm501b8252009-01-21 15:14:30 +00001100 struct sci_port *sci_port = to_sci_port(port);
Magnus Damm973e5d52009-02-24 15:57:12 +09001101 unsigned short bits;
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001102
Magnus Damm501b8252009-01-21 15:14:30 +00001103 if (sci_port->enable)
1104 sci_port->enable(port);
1105
1106 uart_console_write(port, s, count, serial_console_putchar);
Magnus Damm973e5d52009-02-24 15:57:12 +09001107
1108 /* wait until fifo is empty and last bit has been transmitted */
1109 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1110 while ((sci_in(port, SCxSR) & bits) != bits)
1111 cpu_relax();
Magnus Damm501b8252009-01-21 15:14:30 +00001112
Magnus Damm345e5a72009-11-05 14:34:57 +00001113 if (sci_port->disable)
Magnus Damm501b8252009-01-21 15:14:30 +00001114 sci_port->disable(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001117static int __devinit serial_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001119 struct sci_port *sci_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct uart_port *port;
1121 int baud = 115200;
1122 int bits = 8;
1123 int parity = 'n';
1124 int flow = 'n';
1125 int ret;
1126
Paul Mundte108b2c2006-09-27 16:32:13 +09001127 /*
1128 * Check whether an invalid uart number has been specified, and
1129 * if so, search for the first available port that does have
1130 * console support.
1131 */
1132 if (co->index >= SCI_NPORTS)
1133 co->index = 0;
1134
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001135 if (co->data) {
1136 port = co->data;
1137 sci_port = to_sci_port(port);
1138 } else {
1139 sci_port = &sci_ports[co->index];
1140 port = &sci_port->port;
1141 co->data = port;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /*
Paul Mundte108b2c2006-09-27 16:32:13 +09001145 * Also need to check port->type, we don't actually have any
1146 * UPIO_PORT ports, but uart_report_port() handily misreports
1147 * it anyways if we don't have a port available by the time this is
1148 * called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Paul Mundte108b2c2006-09-27 16:32:13 +09001150 if (!port->type)
1151 return -ENODEV;
Paul Mundte108b2c2006-09-27 16:32:13 +09001152
Magnus Damm08f8cb32009-01-21 15:14:22 +00001153 sci_config_port(port, 0);
Paul Mundte108b2c2006-09-27 16:32:13 +09001154
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001155 if (sci_port->enable)
1156 sci_port->enable(port);
Paul Mundte108b2c2006-09-27 16:32:13 +09001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (options)
1159 uart_parse_options(options, &baud, &parity, &bits, &flow);
1160
1161 ret = uart_set_options(port, co, baud, parity, bits, flow);
1162#if defined(__H8300H__) || defined(__H8300S__)
1163 /* disable rx interrupt */
1164 if (ret == 0)
1165 sci_stop_rx(port);
1166#endif
Magnus Damm501b8252009-01-21 15:14:30 +00001167 /* TODO: disable clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return ret;
1169}
1170
1171static struct console serial_console = {
1172 .name = "ttySC",
Magnus Dammdc8e6f52009-01-21 15:14:06 +00001173 .device = serial_console_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 .write = serial_console_write,
1175 .setup = serial_console_setup,
Paul Mundtfa5da2f2007-03-08 17:27:37 +09001176 .flags = CON_PRINTBUFFER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 .index = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178};
1179
1180static int __init sci_console_init(void)
1181{
1182 register_console(&serial_console);
1183 return 0;
1184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185console_initcall(sci_console_init);
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001186
1187static struct sci_port early_serial_port;
1188static struct console early_serial_console = {
1189 .name = "early_ttySC",
1190 .write = serial_console_write,
1191 .flags = CON_PRINTBUFFER,
1192};
1193static char early_serial_buf[32];
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1196
Paul Mundt07d2a1a2008-12-11 19:06:43 +09001197#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
Michael Trimarchie7c98dc2008-11-13 18:18:35 +09001198#define SCI_CONSOLE (&serial_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#else
Paul Mundtb7a76e42006-02-01 03:06:06 -08001200#define SCI_CONSOLE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif
1202
1203static char banner[] __initdata =
1204 KERN_INFO "SuperH SCI(F) driver initialized\n";
1205
1206static struct uart_driver sci_uart_driver = {
1207 .owner = THIS_MODULE,
1208 .driver_name = "sci",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 .dev_name = "ttySC",
1210 .major = SCI_MAJOR,
1211 .minor = SCI_MINOR_START,
Paul Mundte108b2c2006-09-27 16:32:13 +09001212 .nr = SCI_NPORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 .cons = SCI_CONSOLE,
1214};
1215
Magnus Damme552de22009-01-21 15:13:42 +00001216
Paul Mundt54507f62009-05-08 23:48:33 +09001217static int sci_remove(struct platform_device *dev)
Magnus Damme552de22009-01-21 15:13:42 +00001218{
1219 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1220 struct sci_port *p;
1221 unsigned long flags;
1222
Magnus Damme552de22009-01-21 15:13:42 +00001223 cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Magnus Damme552de22009-01-21 15:13:42 +00001224
1225 spin_lock_irqsave(&priv->lock, flags);
1226 list_for_each_entry(p, &priv->ports, node)
1227 uart_remove_one_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001228 spin_unlock_irqrestore(&priv->lock, flags);
1229
1230 kfree(priv);
1231 return 0;
1232}
1233
Magnus Damm0ee70712009-01-21 15:13:50 +00001234static int __devinit sci_probe_single(struct platform_device *dev,
1235 unsigned int index,
1236 struct plat_sci_port *p,
1237 struct sci_port *sciport)
1238{
1239 struct sh_sci_priv *priv = platform_get_drvdata(dev);
1240 unsigned long flags;
1241 int ret;
1242
1243 /* Sanity check */
1244 if (unlikely(index >= SCI_NPORTS)) {
1245 dev_notice(&dev->dev, "Attempting to register port "
1246 "%d when only %d are available.\n",
1247 index+1, SCI_NPORTS);
1248 dev_notice(&dev->dev, "Consider bumping "
1249 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1250 return 0;
1251 }
1252
Magnus Damm501b8252009-01-21 15:14:30 +00001253 sci_init_single(dev, sciport, index, p);
Magnus Damm0ee70712009-01-21 15:13:50 +00001254
1255 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
Magnus Damm08f8cb32009-01-21 15:14:22 +00001256 if (ret)
Magnus Damm0ee70712009-01-21 15:13:50 +00001257 return ret;
Magnus Damm0ee70712009-01-21 15:13:50 +00001258
1259 INIT_LIST_HEAD(&sciport->node);
1260
1261 spin_lock_irqsave(&priv->lock, flags);
1262 list_add(&sciport->node, &priv->ports);
1263 spin_unlock_irqrestore(&priv->lock, flags);
1264
1265 return 0;
1266}
1267
Paul Mundte108b2c2006-09-27 16:32:13 +09001268/*
1269 * Register a set of serial devices attached to a platform device. The
1270 * list is terminated with a zero flags entry, which means we expect
1271 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1272 * remapping (such as sh64) should also set UPF_IOREMAP.
1273 */
1274static int __devinit sci_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Paul Mundte108b2c2006-09-27 16:32:13 +09001276 struct plat_sci_port *p = dev->dev.platform_data;
Magnus Damme552de22009-01-21 15:13:42 +00001277 struct sh_sci_priv *priv;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001278 int i, ret = -EINVAL;
Magnus Damme552de22009-01-21 15:13:42 +00001279
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001280#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1281 if (is_early_platform_device(dev)) {
1282 if (dev->id == -1)
1283 return -ENOTSUPP;
1284 early_serial_console.index = dev->id;
1285 early_serial_console.data = &early_serial_port.port;
1286 sci_init_single(NULL, &early_serial_port, dev->id, p);
1287 serial_console_setup(&early_serial_console, early_serial_buf);
1288 if (!strstr(early_serial_buf, "keep"))
1289 early_serial_console.flags |= CON_BOOT;
1290 register_console(&early_serial_console);
1291 return 0;
1292 }
1293#endif
1294
Magnus Damme552de22009-01-21 15:13:42 +00001295 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1296 if (!priv)
1297 return -ENOMEM;
1298
1299 INIT_LIST_HEAD(&priv->ports);
1300 spin_lock_init(&priv->lock);
1301 platform_set_drvdata(dev, priv);
1302
Magnus Damme552de22009-01-21 15:13:42 +00001303 priv->clk_nb.notifier_call = sci_notifier;
1304 cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Magnus Damm0ee70712009-01-21 15:13:50 +00001306 if (dev->id != -1) {
1307 ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
1308 if (ret)
Magnus Damme552de22009-01-21 15:13:42 +00001309 goto err_unreg;
Magnus Damm0ee70712009-01-21 15:13:50 +00001310 } else {
1311 for (i = 0; p && p->flags != 0; p++, i++) {
1312 ret = sci_probe_single(dev, i, p, &sci_ports[i]);
1313 if (ret)
1314 goto err_unreg;
Magnus Damme552de22009-01-21 15:13:42 +00001315 }
Magnus Damme552de22009-01-21 15:13:42 +00001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318#ifdef CONFIG_SH_STANDARD_BIOS
1319 sh_bios_gdb_detach();
1320#endif
1321
Paul Mundte108b2c2006-09-27 16:32:13 +09001322 return 0;
Paul Mundt7ff731a2008-10-01 15:46:58 +09001323
1324err_unreg:
Magnus Damme552de22009-01-21 15:13:42 +00001325 sci_remove(dev);
Paul Mundt7ff731a2008-10-01 15:46:58 +09001326 return ret;
Paul Mundte108b2c2006-09-27 16:32:13 +09001327}
1328
Paul Mundt6daa79b2009-06-15 07:07:38 +09001329static int sci_suspend(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001330{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001331 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001332 struct sci_port *p;
1333 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001334
Magnus Damme552de22009-01-21 15:13:42 +00001335 spin_lock_irqsave(&priv->lock, flags);
1336 list_for_each_entry(p, &priv->ports, node)
1337 uart_suspend_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001338 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001339
1340 return 0;
1341}
1342
Paul Mundt6daa79b2009-06-15 07:07:38 +09001343static int sci_resume(struct device *dev)
Paul Mundte108b2c2006-09-27 16:32:13 +09001344{
Paul Mundt6daa79b2009-06-15 07:07:38 +09001345 struct sh_sci_priv *priv = dev_get_drvdata(dev);
Magnus Damme552de22009-01-21 15:13:42 +00001346 struct sci_port *p;
1347 unsigned long flags;
Paul Mundte108b2c2006-09-27 16:32:13 +09001348
Magnus Damme552de22009-01-21 15:13:42 +00001349 spin_lock_irqsave(&priv->lock, flags);
1350 list_for_each_entry(p, &priv->ports, node)
1351 uart_resume_port(&sci_uart_driver, &p->port);
Magnus Damme552de22009-01-21 15:13:42 +00001352 spin_unlock_irqrestore(&priv->lock, flags);
Paul Mundte108b2c2006-09-27 16:32:13 +09001353
1354 return 0;
1355}
1356
Alexey Dobriyan47145212009-12-14 18:00:08 -08001357static const struct dev_pm_ops sci_dev_pm_ops = {
Paul Mundt6daa79b2009-06-15 07:07:38 +09001358 .suspend = sci_suspend,
1359 .resume = sci_resume,
1360};
1361
Paul Mundte108b2c2006-09-27 16:32:13 +09001362static struct platform_driver sci_driver = {
1363 .probe = sci_probe,
Uwe Kleine-Königb9e39c82009-11-24 22:07:32 +01001364 .remove = sci_remove,
Paul Mundte108b2c2006-09-27 16:32:13 +09001365 .driver = {
1366 .name = "sh-sci",
1367 .owner = THIS_MODULE,
Paul Mundt6daa79b2009-06-15 07:07:38 +09001368 .pm = &sci_dev_pm_ops,
Paul Mundte108b2c2006-09-27 16:32:13 +09001369 },
1370};
1371
1372static int __init sci_init(void)
1373{
1374 int ret;
1375
1376 printk(banner);
1377
Paul Mundte108b2c2006-09-27 16:32:13 +09001378 ret = uart_register_driver(&sci_uart_driver);
1379 if (likely(ret == 0)) {
1380 ret = platform_driver_register(&sci_driver);
1381 if (unlikely(ret))
1382 uart_unregister_driver(&sci_uart_driver);
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return ret;
1386}
1387
1388static void __exit sci_exit(void)
1389{
Paul Mundte108b2c2006-09-27 16:32:13 +09001390 platform_driver_unregister(&sci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 uart_unregister_driver(&sci_uart_driver);
1392}
1393
Magnus Damm7b6fd3b2009-12-14 10:24:42 +00001394#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1395early_platform_init_buffer("earlyprintk", &sci_driver,
1396 early_serial_buf, ARRAY_SIZE(early_serial_buf));
1397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398module_init(sci_init);
1399module_exit(sci_exit);
1400
Paul Mundte108b2c2006-09-27 16:32:13 +09001401MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001402MODULE_ALIAS("platform:sh-sci");