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