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