Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/serial/sh-sci.c |
| 3 | * |
| 4 | * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO) |
| 5 | * |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 6 | * Copyright (C) 2002 - 2008 Paul Mundt |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 7 | * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Damm | d89ddd1 | 2007-07-25 11:42:56 +0900 | [diff] [blame] | 16 | * Removed SH7300 support (Jul 2007). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 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 Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 22 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 23 | #define SUPPORT_SYSRQ |
| 24 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #undef DEBUG |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/ioport.h> |
| 39 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/init.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/console.h> |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 43 | #include <linux/platform_device.h> |
Paul Mundt | 96de1a8 | 2008-02-26 14:52:45 +0900 | [diff] [blame] | 44 | #include <linux/serial_sci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/notifier.h> |
| 46 | #include <linux/cpufreq.h> |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 47 | #include <linux/clk.h> |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 48 | #include <linux/ctype.h> |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 49 | #include <linux/err.h> |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 50 | #include <linux/list.h> |
Paul Mundt | 85f094e | 2008-04-25 16:04:20 +0900 | [diff] [blame] | 51 | |
| 52 | #ifdef CONFIG_SUPERH |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 53 | #include <asm/clock.h> |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 54 | #include <asm/sh_bios.h> |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 55 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include "sh-sci.h" |
| 58 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 59 | struct sci_port { |
| 60 | struct uart_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 62 | /* Port type */ |
| 63 | unsigned int type; |
| 64 | |
| 65 | /* Port IRQs: ERI, RXI, TXI, BRI (optional) */ |
Paul Mundt | 32351a2 | 2007-03-12 14:38:59 +0900 | [diff] [blame] | 66 | unsigned int irqs[SCIx_NR_IRQS]; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 67 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 68 | /* Port enable callback */ |
| 69 | void (*enable)(struct uart_port *port); |
| 70 | |
| 71 | /* Port disable callback */ |
| 72 | void (*disable)(struct uart_port *port); |
| 73 | |
| 74 | /* Break timer */ |
| 75 | struct timer_list break_timer; |
| 76 | int break_flag; |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 77 | |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 78 | #ifdef CONFIG_HAVE_CLK |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 79 | /* Port clock */ |
| 80 | struct clk *clk; |
Paul Mundt | 005a336 | 2007-04-26 11:45:32 +0900 | [diff] [blame] | 81 | #endif |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 82 | struct list_head node; |
| 83 | }; |
| 84 | |
| 85 | struct sh_sci_priv { |
| 86 | spinlock_t lock; |
| 87 | struct list_head ports; |
| 88 | |
| 89 | #ifdef CONFIG_HAVE_CLK |
| 90 | struct notifier_block clk_nb; |
| 91 | #endif |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* Function prototypes */ |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 95 | static void sci_stop_tx(struct uart_port *port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 97 | #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS |
| 98 | |
| 99 | static struct sci_port sci_ports[SCI_NPORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static struct uart_driver sci_uart_driver; |
| 101 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 102 | static inline struct sci_port * |
| 103 | to_sci_port(struct uart_port *uart) |
| 104 | { |
| 105 | return container_of(uart, struct sci_port, port); |
| 106 | } |
| 107 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 108 | #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 109 | |
| 110 | #ifdef CONFIG_CONSOLE_POLL |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 111 | static inline void handle_error(struct uart_port *port) |
| 112 | { |
| 113 | /* Clear error flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 115 | } |
| 116 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 117 | static int sci_poll_get_char(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | unsigned short status; |
| 120 | int c; |
| 121 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 122 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | status = sci_in(port, SCxSR); |
| 124 | if (status & SCxSR_ERRORS(port)) { |
| 125 | handle_error(port); |
| 126 | continue; |
| 127 | } |
| 128 | } while (!(status & SCxSR_RDxF(port))); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | c = sci_in(port, SCxRDR); |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 131 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 132 | /* Dummy read */ |
| 133 | sci_in(port, SCxSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | return c; |
| 137 | } |
Paul Mundt | 1f6fd5c | 2008-12-17 14:53:24 +0900 | [diff] [blame] | 138 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 140 | static void sci_poll_put_char(struct uart_port *port, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | unsigned short status; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | do { |
| 145 | status = sci_in(port, SCxSR); |
| 146 | } while (!(status & SCxSR_TDxE(port))); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | sci_in(port, SCxSR); /* Dummy read */ |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 149 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port)); |
Paul Mundt | 272966c | 2008-11-13 17:46:06 +0900 | [diff] [blame] | 150 | sci_out(port, SCxTDR, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 152 | #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | #if defined(__H8300S__) |
| 155 | enum { sci_disable, sci_enable }; |
| 156 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 157 | static void h8300_sci_config(struct uart_port *port, unsigned int ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 159 | volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | int ch = (port->mapbase - SMR0) >> 3; |
| 161 | unsigned char mask = 1 << (ch+1); |
| 162 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 163 | if (ctrl == sci_disable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | *mstpcrl |= mask; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 165 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | *mstpcrl &= ~mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 168 | |
| 169 | static inline void h8300_sci_enable(struct uart_port *port) |
| 170 | { |
| 171 | h8300_sci_config(port, sci_enable); |
| 172 | } |
| 173 | |
| 174 | static inline void h8300_sci_disable(struct uart_port *port) |
| 175 | { |
| 176 | h8300_sci_config(port, sci_disable); |
| 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | #endif |
| 179 | |
Paul Mundt | 15c73aa | 2008-10-02 19:47:12 +0900 | [diff] [blame] | 180 | #if defined(__H8300H__) || defined(__H8300S__) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 181 | static void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | int ch = (port->mapbase - SMR0) >> 3; |
| 184 | |
| 185 | /* set DDR regs */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 186 | H8300_GPIO_DDR(h8300_sci_pins[ch].port, |
| 187 | h8300_sci_pins[ch].rx, |
| 188 | H8300_GPIO_INPUT); |
| 189 | H8300_GPIO_DDR(h8300_sci_pins[ch].port, |
| 190 | h8300_sci_pins[ch].tx, |
| 191 | H8300_GPIO_OUTPUT); |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* tx mark output*/ |
| 194 | H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx; |
| 195 | } |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 196 | #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712) |
| 197 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 198 | { |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 199 | if (port->mapbase == 0xA4400000) { |
| 200 | __raw_writew(__raw_readw(PACR) & 0xffc0, PACR); |
| 201 | __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR); |
| 202 | } else if (port->mapbase == 0xA4410000) |
| 203 | __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR); |
Nobuhiro Iwamatsu | 9465a54 | 2007-03-27 18:13:51 +0900 | [diff] [blame] | 204 | } |
Yoshihiro Shimoda | 31a49c4 | 2007-12-26 11:45:06 +0900 | [diff] [blame] | 205 | #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 206 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 207 | { |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 208 | unsigned short data; |
| 209 | |
| 210 | if (cflag & CRTSCTS) { |
| 211 | /* enable RTS/CTS */ |
| 212 | if (port->mapbase == 0xa4430000) { /* SCIF0 */ |
| 213 | /* Clear PTCR bit 9-2; enable all scif pins but sck */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 214 | data = __raw_readw(PORT_PTCR); |
| 215 | __raw_writew((data & 0xfc03), PORT_PTCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 216 | } else if (port->mapbase == 0xa4438000) { /* SCIF1 */ |
| 217 | /* Clear PVCR bit 9-2 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 218 | data = __raw_readw(PORT_PVCR); |
| 219 | __raw_writew((data & 0xfc03), PORT_PVCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 220 | } |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 221 | } else { |
| 222 | if (port->mapbase == 0xa4430000) { /* SCIF0 */ |
| 223 | /* Clear PTCR bit 5-2; enable only tx and rx */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 224 | data = __raw_readw(PORT_PTCR); |
| 225 | __raw_writew((data & 0xffc3), PORT_PTCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 226 | } else if (port->mapbase == 0xa4438000) { /* SCIF1 */ |
| 227 | /* Clear PVCR bit 5-2 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 228 | data = __raw_readw(PORT_PVCR); |
| 229 | __raw_writew((data & 0xffc3), PORT_PVCR); |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 230 | } |
| 231 | } |
Markus Brunner | 3ea6bc3 | 2007-08-20 08:59:33 +0900 | [diff] [blame] | 232 | } |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 233 | #elif defined(CONFIG_CPU_SH3) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 234 | /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 235 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 237 | unsigned short data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 239 | /* We need to set SCPCR to enable RTS/CTS */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 240 | data = __raw_readw(SCPCR); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 241 | /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 242 | __raw_writew(data & 0x0fcf, SCPCR); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 243 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 244 | if (!(cflag & CRTSCTS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* We need to set SCPCR to enable RTS/CTS */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 246 | data = __raw_readw(SCPCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* Clear out SCP7MD1,0, SCP4MD1,0, |
| 248 | Set SCP6MD1,0 = {01} (output) */ |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 249 | __raw_writew((data & 0x0fcf) | 0x1000, SCPCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | data = ctrl_inb(SCPDR); |
| 252 | /* Set /RTS2 (bit6) = 0 */ |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 253 | ctrl_outb(data & 0xbf, SCPDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 256 | #elif defined(CONFIG_CPU_SUBTYPE_SH7722) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 257 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 258 | { |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 259 | unsigned short data; |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 260 | |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 261 | if (port->mapbase == 0xffe00000) { |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 262 | data = __raw_readw(PSCR); |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 263 | data &= ~0x03cf; |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 264 | if (!(cflag & CRTSCTS)) |
Magnus Damm | 346b746 | 2008-04-23 21:25:29 +0900 | [diff] [blame] | 265 | data |= 0x0340; |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 266 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 267 | __raw_writew(data, PSCR); |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 268 | } |
Paul Mundt | 41504c3 | 2006-12-11 20:28:03 +0900 | [diff] [blame] | 269 | } |
Yoshihiro Shimoda | 7d740a0 | 2008-01-07 14:40:07 +0900 | [diff] [blame] | 270 | #elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \ |
| 271 | defined(CONFIG_CPU_SUBTYPE_SH7780) || \ |
Paul Mundt | 2b1bd1a | 2007-06-20 18:27:10 +0900 | [diff] [blame] | 272 | defined(CONFIG_CPU_SUBTYPE_SH7785) || \ |
Kuninori Morimoto | 55ba99e | 2009-03-03 15:40:25 +0900 | [diff] [blame] | 273 | defined(CONFIG_CPU_SUBTYPE_SH7786) || \ |
Paul Mundt | 2b1bd1a | 2007-06-20 18:27:10 +0900 | [diff] [blame] | 274 | defined(CONFIG_CPU_SUBTYPE_SHX3) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 275 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 276 | { |
| 277 | if (!(cflag & CRTSCTS)) |
| 278 | __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */ |
| 279 | } |
Paul Mundt | b0c50ad | 2008-12-22 03:40:10 +0900 | [diff] [blame] | 280 | #elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A) |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 281 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 282 | { |
| 283 | if (!(cflag & CRTSCTS)) |
| 284 | __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */ |
| 285 | } |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 286 | #else |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 287 | static inline void sci_init_pins(struct uart_port *port, unsigned int cflag) |
| 288 | { |
| 289 | /* Nothing to do */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 291 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Paul Mundt | 32351a2 | 2007-03-12 14:38:59 +0900 | [diff] [blame] | 293 | #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \ |
| 294 | defined(CONFIG_CPU_SUBTYPE_SH7780) || \ |
Kuninori Morimoto | 55ba99e | 2009-03-03 15:40:25 +0900 | [diff] [blame] | 295 | defined(CONFIG_CPU_SUBTYPE_SH7785) || \ |
| 296 | defined(CONFIG_CPU_SUBTYPE_SH7786) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 297 | static inline int scif_txroom(struct uart_port *port) |
| 298 | { |
Yutaro Ebihara | cae167d | 2008-03-11 13:58:50 +0900 | [diff] [blame] | 299 | return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static inline int scif_rxroom(struct uart_port *port) |
| 303 | { |
Yutaro Ebihara | cae167d | 2008-03-11 13:58:50 +0900 | [diff] [blame] | 304 | return sci_in(port, SCRFDR) & 0xff; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 305 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 306 | #elif defined(CONFIG_CPU_SUBTYPE_SH7763) |
| 307 | static inline int scif_txroom(struct uart_port *port) |
| 308 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 309 | if ((port->mapbase == 0xffe00000) || |
| 310 | (port->mapbase == 0xffe08000)) { |
| 311 | /* SCIF0/1*/ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 312 | return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 313 | } else { |
| 314 | /* SCIF2 */ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 315 | return SCIF2_TXROOM_MAX - (sci_in(port, SCFDR) >> 8); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 316 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static inline int scif_rxroom(struct uart_port *port) |
| 320 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 321 | if ((port->mapbase == 0xffe00000) || |
| 322 | (port->mapbase == 0xffe08000)) { |
| 323 | /* SCIF0/1*/ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 324 | return sci_in(port, SCRFDR) & 0xff; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 325 | } else { |
| 326 | /* SCIF2 */ |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 327 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 328 | } |
Nobuhiro Iwamatsu | c63847a | 2008-06-06 17:04:08 +0900 | [diff] [blame] | 329 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 330 | #else |
| 331 | static inline int scif_txroom(struct uart_port *port) |
| 332 | { |
| 333 | return SCIF_TXROOM_MAX - (sci_in(port, SCFDR) >> 8); |
| 334 | } |
| 335 | |
| 336 | static inline int scif_rxroom(struct uart_port *port) |
| 337 | { |
| 338 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 342 | static inline int sci_txroom(struct uart_port *port) |
| 343 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 344 | return (sci_in(port, SCxSR) & SCI_TDRE) != 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static inline int sci_rxroom(struct uart_port *port) |
| 348 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 349 | return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /* ********************************************************************** * |
| 353 | * the interrupt related routines * |
| 354 | * ********************************************************************** */ |
| 355 | |
| 356 | static void sci_transmit_chars(struct uart_port *port) |
| 357 | { |
| 358 | struct circ_buf *xmit = &port->info->xmit; |
| 359 | unsigned int stopped = uart_tx_stopped(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | unsigned short status; |
| 361 | unsigned short ctrl; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 362 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | status = sci_in(port, SCxSR); |
| 365 | if (!(status & SCxSR_TDxE(port))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | ctrl = sci_in(port, SCSCR); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 367 | if (uart_circ_empty(xmit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | ctrl &= ~SCI_CTRL_FLAGS_TIE; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 369 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | ctrl |= SCI_CTRL_FLAGS_TIE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 375 | if (port->type == PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 376 | count = sci_txroom(port); |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 377 | else |
| 378 | count = scif_txroom(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | do { |
| 381 | unsigned char c; |
| 382 | |
| 383 | if (port->x_char) { |
| 384 | c = port->x_char; |
| 385 | port->x_char = 0; |
| 386 | } else if (!uart_circ_empty(xmit) && !stopped) { |
| 387 | c = xmit->buf[xmit->tail]; |
| 388 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 389 | } else { |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | sci_out(port, SCxTDR, c); |
| 394 | |
| 395 | port->icount.tx++; |
| 396 | } while (--count > 0); |
| 397 | |
| 398 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port)); |
| 399 | |
| 400 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 401 | uart_write_wakeup(port); |
| 402 | if (uart_circ_empty(xmit)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 403 | sci_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ctrl = sci_in(port, SCSCR); |
| 406 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 407 | if (port->type != PORT_SCI) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | sci_in(port, SCxSR); /* Dummy read */ |
| 409 | sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port)); |
| 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | ctrl |= SCI_CTRL_FLAGS_TIE; |
| 413 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | /* On SH3, SCIF may read end-of-break as a space->mark char */ |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 418 | #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 420 | static inline void sci_receive_chars(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 422 | struct sci_port *sci_port = to_sci_port(port); |
Takashi Iwai | a88487c | 2008-07-16 21:54:42 +0100 | [diff] [blame] | 423 | struct tty_struct *tty = port->info->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | int i, count, copied = 0; |
| 425 | unsigned short status; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 426 | unsigned char flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | status = sci_in(port, SCxSR); |
| 429 | if (!(status & SCxSR_RDxF(port))) |
| 430 | return; |
| 431 | |
| 432 | while (1) { |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 433 | if (port->type == PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 434 | count = sci_rxroom(port); |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 435 | else |
| 436 | count = scif_rxroom(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* Don't copy more bytes than there is room for in the buffer */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 439 | count = tty_buffer_request_room(tty, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | /* If for any reason we can't copy more data, we're done! */ |
| 442 | if (count == 0) |
| 443 | break; |
| 444 | |
| 445 | if (port->type == PORT_SCI) { |
| 446 | char c = sci_in(port, SCxRDR); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 447 | if (uart_handle_sysrq_char(port, c) || |
| 448 | sci_port->break_flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | count = 0; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 450 | else |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 451 | tty_insert_flip_char(tty, c, TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } else { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 453 | for (i = 0; i < count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | char c = sci_in(port, SCxRDR); |
| 455 | status = sci_in(port, SCxSR); |
| 456 | #if defined(CONFIG_CPU_SH3) |
| 457 | /* Skip "chars" during break */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 458 | if (sci_port->break_flag) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | if ((c == 0) && |
| 460 | (status & SCxSR_FER(port))) { |
| 461 | count--; i--; |
| 462 | continue; |
| 463 | } |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* Nonzero => end-of-break */ |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 466 | dev_dbg(port->dev, "debounce<%02x>\n", c); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 467 | sci_port->break_flag = 0; |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (STEPFN(c)) { |
| 470 | count--; i--; |
| 471 | continue; |
| 472 | } |
| 473 | } |
| 474 | #endif /* CONFIG_CPU_SH3 */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 475 | if (uart_handle_sysrq_char(port, c)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | count--; i--; |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | /* Store data and status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (status&SCxSR_FER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 482 | flag = TTY_FRAME; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 483 | dev_notice(port->dev, "frame error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } else if (status&SCxSR_PER(port)) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 485 | flag = TTY_PARITY; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 486 | dev_notice(port->dev, "parity error\n"); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 487 | } else |
| 488 | flag = TTY_NORMAL; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 489 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 490 | tty_insert_flip_char(tty, c, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | sci_in(port, SCxSR); /* dummy read */ |
| 495 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | copied += count; |
| 498 | port->icount.rx += count; |
| 499 | } |
| 500 | |
| 501 | if (copied) { |
| 502 | /* Tell the rest of the system the news. New characters! */ |
| 503 | tty_flip_buffer_push(tty); |
| 504 | } else { |
| 505 | sci_in(port, SCxSR); /* dummy read */ |
| 506 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | #define SCI_BREAK_JIFFIES (HZ/20) |
| 511 | /* The sci generates interrupts during the break, |
| 512 | * 1 per millisecond or so during the break period, for 9600 baud. |
| 513 | * So dont bother disabling interrupts. |
| 514 | * But dont want more than 1 break event. |
| 515 | * Use a kernel timer to periodically poll the rx line until |
| 516 | * the break is finished. |
| 517 | */ |
| 518 | static void sci_schedule_break_timer(struct sci_port *port) |
| 519 | { |
| 520 | port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES; |
| 521 | add_timer(&port->break_timer); |
| 522 | } |
| 523 | /* Ensure that two consecutive samples find the break over. */ |
| 524 | static void sci_break_timer(unsigned long data) |
| 525 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 526 | struct sci_port *port = (struct sci_port *)data; |
| 527 | |
| 528 | if (sci_rxd_in(&port->port) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | port->break_flag = 1; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 530 | sci_schedule_break_timer(port); |
| 531 | } else if (port->break_flag == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* break is over. */ |
| 533 | port->break_flag = 2; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 534 | sci_schedule_break_timer(port); |
| 535 | } else |
| 536 | port->break_flag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static inline int sci_handle_errors(struct uart_port *port) |
| 540 | { |
| 541 | int copied = 0; |
| 542 | unsigned short status = sci_in(port, SCxSR); |
Takashi Iwai | a88487c | 2008-07-16 21:54:42 +0100 | [diff] [blame] | 543 | struct tty_struct *tty = port->info->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 545 | if (status & SCxSR_ORER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* overrun error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 547 | if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 548 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 549 | |
| 550 | dev_notice(port->dev, "overrun error"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 553 | if (status & SCxSR_FER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (sci_rxd_in(port) == 0) { |
| 555 | /* Notify of BREAK */ |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 556 | struct sci_port *sci_port = to_sci_port(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 557 | |
| 558 | if (!sci_port->break_flag) { |
| 559 | sci_port->break_flag = 1; |
| 560 | sci_schedule_break_timer(sci_port); |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* Do sysrq handling. */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 563 | if (uart_handle_break(port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return 0; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 565 | |
| 566 | dev_dbg(port->dev, "BREAK detected\n"); |
| 567 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 568 | if (tty_insert_flip_char(tty, 0, TTY_BREAK)) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 569 | copied++; |
| 570 | } |
| 571 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 572 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | /* frame error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 574 | if (tty_insert_flip_char(tty, 0, TTY_FRAME)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 575 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 576 | |
| 577 | dev_notice(port->dev, "frame error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 581 | if (status & SCxSR_PER(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | /* parity error */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 583 | if (tty_insert_flip_char(tty, 0, TTY_PARITY)) |
| 584 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 585 | |
| 586 | dev_notice(port->dev, "parity error"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 589 | if (copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | return copied; |
| 593 | } |
| 594 | |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 595 | static inline int sci_handle_fifo_overrun(struct uart_port *port) |
| 596 | { |
| 597 | struct tty_struct *tty = port->info->port.tty; |
| 598 | int copied = 0; |
| 599 | |
| 600 | if (port->type != PORT_SCIF) |
| 601 | return 0; |
| 602 | |
| 603 | if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) { |
| 604 | sci_out(port, SCLSR, 0); |
| 605 | |
| 606 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 607 | tty_flip_buffer_push(tty); |
| 608 | |
| 609 | dev_notice(port->dev, "overrun error\n"); |
| 610 | copied++; |
| 611 | } |
| 612 | |
| 613 | return copied; |
| 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | static inline int sci_handle_breaks(struct uart_port *port) |
| 617 | { |
| 618 | int copied = 0; |
| 619 | unsigned short status = sci_in(port, SCxSR); |
Takashi Iwai | a88487c | 2008-07-16 21:54:42 +0100 | [diff] [blame] | 620 | struct tty_struct *tty = port->info->port.tty; |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 621 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Paul Mundt | 0b3d4ef | 2007-03-14 13:22:37 +0900 | [diff] [blame] | 623 | if (uart_handle_break(port)) |
| 624 | return 0; |
| 625 | |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 626 | if (!s->break_flag && status & SCxSR_BRK(port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | #if defined(CONFIG_CPU_SH3) |
| 628 | /* Debounce break */ |
| 629 | s->break_flag = 1; |
| 630 | #endif |
| 631 | /* Notify of BREAK */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 632 | if (tty_insert_flip_char(tty, 0, TTY_BREAK)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 633 | copied++; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 634 | |
| 635 | dev_dbg(port->dev, "BREAK detected\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 638 | if (copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | tty_flip_buffer_push(tty); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 640 | |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 641 | copied += sci_handle_fifo_overrun(port); |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | return copied; |
| 644 | } |
| 645 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 646 | static irqreturn_t sci_rx_interrupt(int irq, void *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* I think sci_receive_chars has to be called irrespective |
| 649 | * of whether the I_IXOFF is set, otherwise, how is the interrupt |
| 650 | * to be disabled? |
| 651 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 652 | sci_receive_chars(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | return IRQ_HANDLED; |
| 655 | } |
| 656 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 657 | static irqreturn_t sci_tx_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
| 659 | struct uart_port *port = ptr; |
| 660 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 661 | spin_lock_irq(&port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | sci_transmit_chars(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 663 | spin_unlock_irq(&port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | return IRQ_HANDLED; |
| 666 | } |
| 667 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 668 | static irqreturn_t sci_er_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
| 670 | struct uart_port *port = ptr; |
| 671 | |
| 672 | /* Handle errors */ |
| 673 | if (port->type == PORT_SCI) { |
| 674 | if (sci_handle_errors(port)) { |
| 675 | /* discard character in rx buffer */ |
| 676 | sci_in(port, SCxSR); |
| 677 | sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port)); |
| 678 | } |
| 679 | } else { |
Paul Mundt | d830fa4 | 2008-12-16 19:29:38 +0900 | [diff] [blame] | 680 | sci_handle_fifo_overrun(port); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 681 | sci_rx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); |
| 685 | |
| 686 | /* Kick the transmission */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 687 | sci_tx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | return IRQ_HANDLED; |
| 690 | } |
| 691 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 692 | static irqreturn_t sci_br_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | struct uart_port *port = ptr; |
| 695 | |
| 696 | /* Handle BREAKs */ |
| 697 | sci_handle_breaks(port); |
| 698 | sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port)); |
| 699 | |
| 700 | return IRQ_HANDLED; |
| 701 | } |
| 702 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 703 | static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 705 | unsigned short ssr_status, scr_status; |
| 706 | struct uart_port *port = ptr; |
| 707 | irqreturn_t ret = IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 709 | ssr_status = sci_in(port, SCxSR); |
| 710 | scr_status = sci_in(port, SCSCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | /* Tx Interrupt */ |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 713 | if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE)) |
| 714 | ret = sci_tx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* Rx Interrupt */ |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 716 | if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE)) |
| 717 | ret = sci_rx_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | /* Error Interrupt */ |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 719 | if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE)) |
| 720 | ret = sci_er_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | /* Break Interrupt */ |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 722 | if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE)) |
| 723 | ret = sci_br_interrupt(irq, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Michael Trimarchi | a8884e3 | 2008-10-31 16:10:23 +0900 | [diff] [blame] | 725 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Paul Mundt | 027e687 | 2008-12-16 18:36:16 +0900 | [diff] [blame] | 728 | #ifdef CONFIG_HAVE_CLK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
| 730 | * Here we define a transistion notifier so that we can update all of our |
| 731 | * ports' baud rate when the peripheral clock changes. |
| 732 | */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 733 | static int sci_notifier(struct notifier_block *self, |
| 734 | unsigned long phase, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 736 | struct sh_sci_priv *priv = container_of(self, |
| 737 | struct sh_sci_priv, clk_nb); |
| 738 | struct sci_port *sci_port; |
| 739 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | if ((phase == CPUFREQ_POSTCHANGE) || |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 742 | (phase == CPUFREQ_RESUMECHANGE)) { |
| 743 | spin_lock_irqsave(&priv->lock, flags); |
| 744 | list_for_each_entry(sci_port, &priv->ports, node) |
| 745 | sci_port->port.uartclk = clk_get_rate(sci_port->clk); |
| 746 | |
| 747 | spin_unlock_irqrestore(&priv->lock, flags); |
| 748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return NOTIFY_OK; |
| 751 | } |
Paul Mundt | 027e687 | 2008-12-16 18:36:16 +0900 | [diff] [blame] | 752 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | static int sci_request_irq(struct sci_port *port) |
| 755 | { |
| 756 | int i; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 757 | irqreturn_t (*handlers[4])(int irq, void *ptr) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt, |
| 759 | sci_br_interrupt, |
| 760 | }; |
| 761 | const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full", |
| 762 | "SCI Transmit Data Empty", "SCI Break" }; |
| 763 | |
| 764 | if (port->irqs[0] == port->irqs[1]) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 765 | if (unlikely(!port->irqs[0])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return -ENODEV; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 767 | |
| 768 | if (request_irq(port->irqs[0], sci_mpxed_interrupt, |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 769 | IRQF_DISABLED, "sci", port)) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 770 | dev_err(port->port.dev, "Can't allocate IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return -ENODEV; |
| 772 | } |
| 773 | } else { |
| 774 | for (i = 0; i < ARRAY_SIZE(handlers); i++) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 775 | if (unlikely(!port->irqs[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | continue; |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 777 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 778 | if (request_irq(port->irqs[i], handlers[i], |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 779 | IRQF_DISABLED, desc[i], port)) { |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 780 | dev_err(port->port.dev, "Can't allocate IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return -ENODEV; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static void sci_free_irq(struct sci_port *port) |
| 790 | { |
| 791 | int i; |
| 792 | |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 793 | if (port->irqs[0] == port->irqs[1]) |
| 794 | free_irq(port->irqs[0], port); |
| 795 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | for (i = 0; i < ARRAY_SIZE(port->irqs); i++) { |
| 797 | if (!port->irqs[i]) |
| 798 | continue; |
| 799 | |
| 800 | free_irq(port->irqs[i], port); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | static unsigned int sci_tx_empty(struct uart_port *port) |
| 806 | { |
| 807 | /* Can't detect */ |
| 808 | return TIOCSER_TEMT; |
| 809 | } |
| 810 | |
| 811 | static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 812 | { |
| 813 | /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */ |
| 814 | /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */ |
| 815 | /* If you have signals for DTR and DCD, please implement here. */ |
| 816 | } |
| 817 | |
| 818 | static unsigned int sci_get_mctrl(struct uart_port *port) |
| 819 | { |
| 820 | /* This routine is used for geting signals of: DTR, DCD, DSR, RI, |
| 821 | and CTS/RTS */ |
| 822 | |
| 823 | return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR; |
| 824 | } |
| 825 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 826 | static void sci_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 828 | unsigned short ctrl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 830 | /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */ |
| 831 | ctrl = sci_in(port, SCSCR); |
| 832 | ctrl |= SCI_CTRL_FLAGS_TIE; |
| 833 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 836 | static void sci_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | unsigned short ctrl; |
| 839 | |
| 840 | /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | ctrl = sci_in(port, SCSCR); |
| 842 | ctrl &= ~SCI_CTRL_FLAGS_TIE; |
| 843 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static void sci_start_rx(struct uart_port *port, unsigned int tty_start) |
| 847 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | unsigned short ctrl; |
| 849 | |
| 850 | /* Set RIE (Receive Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | ctrl = sci_in(port, SCSCR); |
| 852 | ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE; |
| 853 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void sci_stop_rx(struct uart_port *port) |
| 857 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | unsigned short ctrl; |
| 859 | |
| 860 | /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | ctrl = sci_in(port, SCSCR); |
| 862 | ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE); |
| 863 | sci_out(port, SCSCR, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void sci_enable_ms(struct uart_port *port) |
| 867 | { |
| 868 | /* Nothing here yet .. */ |
| 869 | } |
| 870 | |
| 871 | static void sci_break_ctl(struct uart_port *port, int break_state) |
| 872 | { |
| 873 | /* Nothing here yet .. */ |
| 874 | } |
| 875 | |
| 876 | static int sci_startup(struct uart_port *port) |
| 877 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 878 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 880 | if (s->enable) |
| 881 | s->enable(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 883 | #ifdef CONFIG_HAVE_CLK |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 884 | s->clk = clk_get(NULL, "module_clk"); |
Paul Mundt | 005a336 | 2007-04-26 11:45:32 +0900 | [diff] [blame] | 885 | #endif |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | sci_request_irq(s); |
Yoshinori Sato | d656901 | 2005-10-14 15:59:12 -0700 | [diff] [blame] | 888 | sci_start_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | sci_start_rx(port, 1); |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static void sci_shutdown(struct uart_port *port) |
| 895 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 896 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
| 898 | sci_stop_rx(port); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 899 | sci_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sci_free_irq(s); |
| 901 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 902 | if (s->disable) |
| 903 | s->disable(port); |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 904 | |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 905 | #ifdef CONFIG_HAVE_CLK |
dmitry pervushin | 1534a3b | 2007-04-24 13:41:12 +0900 | [diff] [blame] | 906 | clk_put(s->clk); |
| 907 | s->clk = NULL; |
Paul Mundt | 005a336 | 2007-04-26 11:45:32 +0900 | [diff] [blame] | 908 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 911 | static void sci_set_termios(struct uart_port *port, struct ktermios *termios, |
| 912 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | unsigned int status, baud, smr_val; |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 915 | int t = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 918 | if (likely(baud)) |
| 919 | t = SCBRR_VALUE(baud, port->uartclk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 921 | do { |
| 922 | status = sci_in(port, SCxSR); |
| 923 | } while (!(status & SCxSR_TEND(port))); |
| 924 | |
| 925 | sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */ |
| 926 | |
Yoshihiro Shimoda | 1a22f08 | 2008-11-11 12:19:05 +0900 | [diff] [blame] | 927 | if (port->type != PORT_SCI) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 928 | sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 929 | |
| 930 | smr_val = sci_in(port, SCSMR) & 3; |
| 931 | if ((termios->c_cflag & CSIZE) == CS7) |
| 932 | smr_val |= 0x40; |
| 933 | if (termios->c_cflag & PARENB) |
| 934 | smr_val |= 0x20; |
| 935 | if (termios->c_cflag & PARODD) |
| 936 | smr_val |= 0x30; |
| 937 | if (termios->c_cflag & CSTOPB) |
| 938 | smr_val |= 0x08; |
| 939 | |
| 940 | uart_update_timeout(port, termios->c_cflag, baud); |
| 941 | |
| 942 | sci_out(port, SCSMR, smr_val); |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (t > 0) { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 945 | if (t >= 256) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1); |
| 947 | t >>= 2; |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 948 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3); |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | sci_out(port, SCBRR, t); |
| 952 | udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */ |
| 953 | } |
| 954 | |
Paul Mundt | d570164 | 2008-12-16 20:07:27 +0900 | [diff] [blame] | 955 | sci_init_pins(port, termios->c_cflag); |
| 956 | sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0); |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | sci_out(port, SCSCR, SCSCR_INIT(port)); |
| 959 | |
| 960 | if ((termios->c_cflag & CREAD) != 0) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 961 | sci_start_rx(port, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static const char *sci_type(struct uart_port *port) |
| 965 | { |
| 966 | switch (port->type) { |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 967 | case PORT_IRDA: |
| 968 | return "irda"; |
| 969 | case PORT_SCI: |
| 970 | return "sci"; |
| 971 | case PORT_SCIF: |
| 972 | return "scif"; |
| 973 | case PORT_SCIFA: |
| 974 | return "scifa"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Paul Mundt | fa43972 | 2008-09-04 18:53:58 +0900 | [diff] [blame] | 977 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static void sci_release_port(struct uart_port *port) |
| 981 | { |
| 982 | /* Nothing here yet .. */ |
| 983 | } |
| 984 | |
| 985 | static int sci_request_port(struct uart_port *port) |
| 986 | { |
| 987 | /* Nothing here yet .. */ |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | static void sci_config_port(struct uart_port *port, int flags) |
| 992 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 993 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | port->type = s->type; |
| 996 | |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 997 | if (port->flags & UPF_IOREMAP && !port->membase) { |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 998 | port->membase = ioremap_nocache(port->mapbase, 0x40); |
Paul Mundt | 762c69e | 2008-12-16 18:55:26 +0900 | [diff] [blame] | 999 | dev_err(port->dev, "can't remap port#%d\n", port->line); |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static int sci_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 1004 | { |
Magnus Damm | a5660ad | 2009-01-21 15:14:38 +0000 | [diff] [blame] | 1005 | struct sci_port *s = to_sci_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 1007 | if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return -EINVAL; |
| 1009 | if (ser->baud_base < 2400) |
| 1010 | /* No paper tape reader for Mitch.. */ |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static struct uart_ops sci_uart_ops = { |
| 1017 | .tx_empty = sci_tx_empty, |
| 1018 | .set_mctrl = sci_set_mctrl, |
| 1019 | .get_mctrl = sci_get_mctrl, |
| 1020 | .start_tx = sci_start_tx, |
| 1021 | .stop_tx = sci_stop_tx, |
| 1022 | .stop_rx = sci_stop_rx, |
| 1023 | .enable_ms = sci_enable_ms, |
| 1024 | .break_ctl = sci_break_ctl, |
| 1025 | .startup = sci_startup, |
| 1026 | .shutdown = sci_shutdown, |
| 1027 | .set_termios = sci_set_termios, |
| 1028 | .type = sci_type, |
| 1029 | .release_port = sci_release_port, |
| 1030 | .request_port = sci_request_port, |
| 1031 | .config_port = sci_config_port, |
| 1032 | .verify_port = sci_verify_port, |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1033 | #ifdef CONFIG_CONSOLE_POLL |
| 1034 | .poll_get_char = sci_poll_get_char, |
| 1035 | .poll_put_char = sci_poll_put_char, |
| 1036 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | }; |
| 1038 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1039 | static void __init sci_init_ports(void) |
| 1040 | { |
| 1041 | static int first = 1; |
| 1042 | int i; |
| 1043 | |
| 1044 | if (!first) |
| 1045 | return; |
| 1046 | |
| 1047 | first = 0; |
| 1048 | |
| 1049 | for (i = 0; i < SCI_NPORTS; i++) { |
| 1050 | sci_ports[i].port.ops = &sci_uart_ops; |
| 1051 | sci_ports[i].port.iotype = UPIO_MEM; |
| 1052 | sci_ports[i].port.line = i; |
| 1053 | sci_ports[i].port.fifosize = 1; |
| 1054 | |
| 1055 | #if defined(__H8300H__) || defined(__H8300S__) |
| 1056 | #ifdef __H8300S__ |
| 1057 | sci_ports[i].enable = h8300_sci_enable; |
| 1058 | sci_ports[i].disable = h8300_sci_disable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | #endif |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1060 | sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK; |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 1061 | #elif defined(CONFIG_HAVE_CLK) |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1062 | /* |
| 1063 | * XXX: We should use a proper SCI/SCIF clock |
| 1064 | */ |
| 1065 | { |
Paul Mundt | 1d11856 | 2006-12-01 13:15:14 +0900 | [diff] [blame] | 1066 | struct clk *clk = clk_get(NULL, "module_clk"); |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 1067 | sci_ports[i].port.uartclk = clk_get_rate(clk); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1068 | clk_put(clk); |
| 1069 | } |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 1070 | #else |
| 1071 | #error "Need a valid uartclk" |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1072 | #endif |
| 1073 | |
| 1074 | sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i]; |
| 1075 | sci_ports[i].break_timer.function = sci_break_timer; |
| 1076 | |
| 1077 | init_timer(&sci_ports[i].break_timer); |
| 1078 | } |
| 1079 | } |
| 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1082 | static struct tty_driver *serial_console_device(struct console *co, int *index) |
| 1083 | { |
| 1084 | struct uart_driver *p = &sci_uart_driver; |
| 1085 | *index = co->index; |
| 1086 | return p->tty_driver; |
| 1087 | } |
| 1088 | |
| 1089 | static void serial_console_putchar(struct uart_port *port, int ch) |
| 1090 | { |
| 1091 | sci_poll_put_char(port, ch); |
| 1092 | } |
| 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * Print a string to the serial port trying not to disturb |
| 1096 | * any possible real use of the port... |
| 1097 | */ |
| 1098 | static void serial_console_write(struct console *co, const char *s, |
| 1099 | unsigned count) |
| 1100 | { |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1101 | struct uart_port *port = co->data; |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 1102 | unsigned short bits; |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1103 | |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1104 | uart_console_write(co->data, s, count, serial_console_putchar); |
Magnus Damm | 973e5d5 | 2009-02-24 15:57:12 +0900 | [diff] [blame] | 1105 | |
| 1106 | /* wait until fifo is empty and last bit has been transmitted */ |
| 1107 | bits = SCxSR_TDxE(port) | SCxSR_TEND(port); |
| 1108 | while ((sci_in(port, SCxSR) & bits) != bits) |
| 1109 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | static int __init serial_console_setup(struct console *co, char *options) |
| 1113 | { |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1114 | struct sci_port *sci_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | struct uart_port *port; |
| 1116 | int baud = 115200; |
| 1117 | int bits = 8; |
| 1118 | int parity = 'n'; |
| 1119 | int flow = 'n'; |
| 1120 | int ret; |
| 1121 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1122 | /* |
| 1123 | * Check whether an invalid uart number has been specified, and |
| 1124 | * if so, search for the first available port that does have |
| 1125 | * console support. |
| 1126 | */ |
| 1127 | if (co->index >= SCI_NPORTS) |
| 1128 | co->index = 0; |
| 1129 | |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1130 | sci_port = &sci_ports[co->index]; |
| 1131 | port = &sci_port->port; |
| 1132 | co->data = port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | /* |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1135 | * Also need to check port->type, we don't actually have any |
| 1136 | * UPIO_PORT ports, but uart_report_port() handily misreports |
| 1137 | * it anyways if we don't have a port available by the time this is |
| 1138 | * called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | */ |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1140 | if (!port->type) |
| 1141 | return -ENODEV; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1142 | |
Paul Mundt | a2159b5 | 2008-10-02 19:09:13 +0900 | [diff] [blame] | 1143 | #ifdef CONFIG_HAVE_CLK |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1144 | if (!sci_port->clk) |
| 1145 | sci_port->clk = clk_get(NULL, "module_clk"); |
Paul Mundt | 005a336 | 2007-04-26 11:45:32 +0900 | [diff] [blame] | 1146 | #endif |
| 1147 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1148 | if (port->flags & UPF_IOREMAP) |
| 1149 | sci_config_port(port, 0); |
| 1150 | |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1151 | if (sci_port->enable) |
| 1152 | sci_port->enable(port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | if (options) |
| 1155 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1156 | |
| 1157 | ret = uart_set_options(port, co, baud, parity, bits, flow); |
| 1158 | #if defined(__H8300H__) || defined(__H8300S__) |
| 1159 | /* disable rx interrupt */ |
| 1160 | if (ret == 0) |
| 1161 | sci_stop_rx(port); |
| 1162 | #endif |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | static struct console serial_console = { |
| 1167 | .name = "ttySC", |
Magnus Damm | dc8e6f5 | 2009-01-21 15:14:06 +0000 | [diff] [blame] | 1168 | .device = serial_console_device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | .write = serial_console_write, |
| 1170 | .setup = serial_console_setup, |
Paul Mundt | fa5da2f | 2007-03-08 17:27:37 +0900 | [diff] [blame] | 1171 | .flags = CON_PRINTBUFFER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | .index = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | }; |
| 1174 | |
| 1175 | static int __init sci_console_init(void) |
| 1176 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1177 | sci_init_ports(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | register_console(&serial_console); |
| 1179 | return 0; |
| 1180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | console_initcall(sci_console_init); |
| 1182 | #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */ |
| 1183 | |
Paul Mundt | 07d2a1a | 2008-12-11 19:06:43 +0900 | [diff] [blame] | 1184 | #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) |
Michael Trimarchi | e7c98dc | 2008-11-13 18:18:35 +0900 | [diff] [blame] | 1185 | #define SCI_CONSOLE (&serial_console) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | #else |
Paul Mundt | b7a76e4 | 2006-02-01 03:06:06 -0800 | [diff] [blame] | 1187 | #define SCI_CONSOLE 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | #endif |
| 1189 | |
| 1190 | static char banner[] __initdata = |
| 1191 | KERN_INFO "SuperH SCI(F) driver initialized\n"; |
| 1192 | |
| 1193 | static struct uart_driver sci_uart_driver = { |
| 1194 | .owner = THIS_MODULE, |
| 1195 | .driver_name = "sci", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | .dev_name = "ttySC", |
| 1197 | .major = SCI_MAJOR, |
| 1198 | .minor = SCI_MINOR_START, |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1199 | .nr = SCI_NPORTS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | .cons = SCI_CONSOLE, |
| 1201 | }; |
| 1202 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1203 | |
| 1204 | static int __devexit sci_remove(struct platform_device *dev) |
| 1205 | { |
| 1206 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1207 | struct sci_port *p; |
| 1208 | unsigned long flags; |
| 1209 | |
| 1210 | #ifdef CONFIG_HAVE_CLK |
| 1211 | cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER); |
| 1212 | #endif |
| 1213 | |
| 1214 | spin_lock_irqsave(&priv->lock, flags); |
| 1215 | list_for_each_entry(p, &priv->ports, node) |
| 1216 | uart_remove_one_port(&sci_uart_driver, &p->port); |
| 1217 | |
| 1218 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1219 | |
| 1220 | kfree(priv); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame^] | 1224 | static int __devinit sci_probe_single(struct platform_device *dev, |
| 1225 | unsigned int index, |
| 1226 | struct plat_sci_port *p, |
| 1227 | struct sci_port *sciport) |
| 1228 | { |
| 1229 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1230 | unsigned long flags; |
| 1231 | int ret; |
| 1232 | |
| 1233 | /* Sanity check */ |
| 1234 | if (unlikely(index >= SCI_NPORTS)) { |
| 1235 | dev_notice(&dev->dev, "Attempting to register port " |
| 1236 | "%d when only %d are available.\n", |
| 1237 | index+1, SCI_NPORTS); |
| 1238 | dev_notice(&dev->dev, "Consider bumping " |
| 1239 | "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n"); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | sciport->port.mapbase = p->mapbase; |
| 1244 | |
| 1245 | if (p->mapbase && !p->membase) { |
| 1246 | if (p->flags & UPF_IOREMAP) { |
| 1247 | p->membase = ioremap_nocache(p->mapbase, 0x40); |
| 1248 | if (IS_ERR(p->membase)) |
| 1249 | return PTR_ERR(p->membase); |
| 1250 | } else { |
| 1251 | /* |
| 1252 | * For the simple (and majority of) cases |
| 1253 | * where we don't need to do any remapping, |
| 1254 | * just cast the cookie directly. |
| 1255 | */ |
| 1256 | p->membase = (void __iomem *)p->mapbase; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | sciport->port.membase = p->membase; |
| 1261 | |
| 1262 | sciport->port.irq = p->irqs[SCIx_TXI_IRQ]; |
| 1263 | sciport->port.flags = p->flags; |
| 1264 | sciport->port.dev = &dev->dev; |
| 1265 | |
| 1266 | sciport->type = sciport->port.type = p->type; |
| 1267 | |
| 1268 | memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs)); |
| 1269 | |
| 1270 | ret = uart_add_one_port(&sci_uart_driver, &sciport->port); |
| 1271 | |
| 1272 | if (ret) { |
| 1273 | if (p->flags & UPF_IOREMAP) |
| 1274 | iounmap(p->membase); |
| 1275 | |
| 1276 | return ret; |
| 1277 | } |
| 1278 | |
| 1279 | INIT_LIST_HEAD(&sciport->node); |
| 1280 | |
| 1281 | spin_lock_irqsave(&priv->lock, flags); |
| 1282 | list_add(&sciport->node, &priv->ports); |
| 1283 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1288 | /* |
| 1289 | * Register a set of serial devices attached to a platform device. The |
| 1290 | * list is terminated with a zero flags entry, which means we expect |
| 1291 | * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need |
| 1292 | * remapping (such as sh64) should also set UPF_IOREMAP. |
| 1293 | */ |
| 1294 | static int __devinit sci_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1296 | struct plat_sci_port *p = dev->dev.platform_data; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1297 | struct sh_sci_priv *priv; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1298 | int i, ret = -EINVAL; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1299 | |
| 1300 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 1301 | if (!priv) |
| 1302 | return -ENOMEM; |
| 1303 | |
| 1304 | INIT_LIST_HEAD(&priv->ports); |
| 1305 | spin_lock_init(&priv->lock); |
| 1306 | platform_set_drvdata(dev, priv); |
| 1307 | |
| 1308 | #ifdef CONFIG_HAVE_CLK |
| 1309 | priv->clk_nb.notifier_call = sci_notifier; |
| 1310 | cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER); |
| 1311 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame^] | 1313 | if (dev->id != -1) { |
| 1314 | ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]); |
| 1315 | if (ret) |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1316 | goto err_unreg; |
Magnus Damm | 0ee7071 | 2009-01-21 15:13:50 +0000 | [diff] [blame^] | 1317 | } else { |
| 1318 | for (i = 0; p && p->flags != 0; p++, i++) { |
| 1319 | ret = sci_probe_single(dev, i, p, &sci_ports[i]); |
| 1320 | if (ret) |
| 1321 | goto err_unreg; |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1322 | } |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
| 1325 | #ifdef CONFIG_SH_STANDARD_BIOS |
| 1326 | sh_bios_gdb_detach(); |
| 1327 | #endif |
| 1328 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1329 | return 0; |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1330 | |
| 1331 | err_unreg: |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1332 | sci_remove(dev); |
Paul Mundt | 7ff731a | 2008-10-01 15:46:58 +0900 | [diff] [blame] | 1333 | return ret; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1334 | } |
| 1335 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1336 | static int sci_suspend(struct platform_device *dev, pm_message_t state) |
| 1337 | { |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1338 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1339 | struct sci_port *p; |
| 1340 | unsigned long flags; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1341 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1342 | spin_lock_irqsave(&priv->lock, flags); |
| 1343 | list_for_each_entry(p, &priv->ports, node) |
| 1344 | uart_suspend_port(&sci_uart_driver, &p->port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1345 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1346 | spin_unlock_irqrestore(&priv->lock, flags); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static int sci_resume(struct platform_device *dev) |
| 1352 | { |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1353 | struct sh_sci_priv *priv = platform_get_drvdata(dev); |
| 1354 | struct sci_port *p; |
| 1355 | unsigned long flags; |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1356 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1357 | spin_lock_irqsave(&priv->lock, flags); |
| 1358 | list_for_each_entry(p, &priv->ports, node) |
| 1359 | uart_resume_port(&sci_uart_driver, &p->port); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1360 | |
Magnus Damm | e552de2 | 2009-01-21 15:13:42 +0000 | [diff] [blame] | 1361 | spin_unlock_irqrestore(&priv->lock, flags); |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1362 | |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static struct platform_driver sci_driver = { |
| 1367 | .probe = sci_probe, |
| 1368 | .remove = __devexit_p(sci_remove), |
| 1369 | .suspend = sci_suspend, |
| 1370 | .resume = sci_resume, |
| 1371 | .driver = { |
| 1372 | .name = "sh-sci", |
| 1373 | .owner = THIS_MODULE, |
| 1374 | }, |
| 1375 | }; |
| 1376 | |
| 1377 | static int __init sci_init(void) |
| 1378 | { |
| 1379 | int ret; |
| 1380 | |
| 1381 | printk(banner); |
| 1382 | |
| 1383 | sci_init_ports(); |
| 1384 | |
| 1385 | ret = uart_register_driver(&sci_uart_driver); |
| 1386 | if (likely(ret == 0)) { |
| 1387 | ret = platform_driver_register(&sci_driver); |
| 1388 | if (unlikely(ret)) |
| 1389 | uart_unregister_driver(&sci_uart_driver); |
| 1390 | } |
| 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | static void __exit sci_exit(void) |
| 1396 | { |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1397 | platform_driver_unregister(&sci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | uart_unregister_driver(&sci_uart_driver); |
| 1399 | } |
| 1400 | |
| 1401 | module_init(sci_init); |
| 1402 | module_exit(sci_exit); |
| 1403 | |
Paul Mundt | e108b2c | 2006-09-27 16:32:13 +0900 | [diff] [blame] | 1404 | MODULE_LICENSE("GPL"); |
Kay Sievers | e169c13 | 2008-04-15 14:34:35 -0700 | [diff] [blame] | 1405 | MODULE_ALIAS("platform:sh-sci"); |