blob: e27360652d9b6b6e09976b5fda3ff495ed3f9838 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Serial driver for the amiga builtin port.
4 *
5 * This code was created by taking serial.c version 4.30 from kernel
6 * release 2.3.22, replacing all hardware related stuff with the
7 * corresponding amiga hardware actions, and removing all irrelevant
8 * code. As a consequence, it uses many of the constants and names
9 * associated with the registers and bits of 16550 compatible UARTS -
10 * but only to keep track of status, etc in the state variables. It
11 * was done this was to make it easier to keep the code in line with
12 * (non hardware specific) changes to serial.c.
13 *
14 * The port is registered with the tty driver as minor device 64, and
Jason Wang014482b2022-07-15 13:44:01 +080015 * therefore other ports should only use 65 upwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * Richard Lucock 28/12/99
18 *
19 * Copyright (C) 1991, 1992 Linus Torvalds
20 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
21 * 1998, 1999 Theodore Ts'o
22 *
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Set of debugging defines */
26
27#undef SERIAL_DEBUG_INTR
28#undef SERIAL_DEBUG_OPEN
29#undef SERIAL_DEBUG_FLOW
30#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * End of serial driver configuration section.
34 */
35
Jiri Slabyf3d788b2021-07-14 11:13:12 +020036#include <linux/bitops.h>
Jiri Slaby6fe18d22012-03-05 14:52:45 +010037#include <linux/circ_buf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/console.h>
Jiri Slabyf3d788b2021-07-14 11:13:12 +020039#include <linux/delay.h>
40#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
Jiri Slabyf3d788b2021-07-14 11:13:12 +020043#include <linux/interrupt.h>
44#include <linux/ioport.h>
45#include <linux/kernel.h>
46#include <linux/major.h>
47#include <linux/mm.h>
48#include <linux/module.h>
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +020049#include <linux/platform_device.h>
Jiri Slabyf3d788b2021-07-14 11:13:12 +020050#include <linux/ptrace.h>
51#include <linux/seq_file.h>
52#include <linux/serial.h>
53#include <linux/serial_reg.h>
Ilpo Järvineneb47b592022-06-24 23:54:23 +030054#include <linux/serial_core.h>
Jiri Slabyf3d788b2021-07-14 11:13:12 +020055#include <linux/sched.h>
56#include <linux/signal.h>
57#include <linux/slab.h>
58#include <linux/string.h>
59#include <linux/timer.h>
60#include <linux/tty_flip.h>
61#include <linux/tty.h>
62#include <linux/types.h>
63#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <asm/amigahw.h>
66#include <asm/amigaints.h>
Jiri Slabyf3d788b2021-07-14 11:13:12 +020067#include <asm/irq.h>
68#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jiri Slaby6fe18d22012-03-05 14:52:45 +010070struct serial_state {
71 struct tty_port tport;
72 struct circ_buf xmit;
73 struct async_icount icount;
74
75 unsigned long port;
76 int baud_base;
Jiri Slaby6fe18d22012-03-05 14:52:45 +010077 int custom_divisor;
78 int read_status_mask;
79 int ignore_status_mask;
80 int timeout;
81 int quot;
82 int IER; /* Interrupt Enable Register */
83 int MCR; /* Modem control register */
Jiri Slaby (SUSE)fbdeead2023-12-06 08:36:51 +010084 u8 x_char; /* xon/xoff character */
Jiri Slaby6fe18d22012-03-05 14:52:45 +010085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct tty_driver *serial_driver;
88
89/* number of characters left in xmit buffer before we ask for more */
90#define WAKEUP_CHARS 256
91
Jiri Slaby5a7c7a62021-07-14 11:13:07 +020092#define XMIT_FIFO_SIZE 1
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned char current_ctl_bits;
95
Jiri Slaby588993d2012-03-05 14:52:22 +010096static void change_speed(struct tty_struct *tty, struct serial_state *info,
Ilpo Järvinena8c11c12022-08-16 14:57:39 +030097 const struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
99
100
Jiri Slaby6cc7bda2021-07-14 11:13:13 +0200101static struct serial_state serial_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* some serial hardware definitions */
104#define SDR_OVRUN (1<<15)
105#define SDR_RBF (1<<14)
106#define SDR_TBE (1<<13)
107#define SDR_TSRE (1<<12)
108
109#define SERPER_PARENB (1<<15)
110
111#define AC_SETCLR (1<<15)
112#define AC_UARTBRK (1<<11)
113
114#define SER_DTR (1<<7)
115#define SER_RTS (1<<6)
116#define SER_DCD (1<<5)
117#define SER_CTS (1<<4)
118#define SER_DSR (1<<3)
119
120static __inline__ void rtsdtr_ctrl(int bits)
121{
122 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
123}
124
125/*
126 * ------------------------------------------------------------
127 * rs_stop() and rs_start()
128 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200129 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * They enable or disable transmitter interrupts, as necessary.
131 * ------------------------------------------------------------
132 */
133static void rs_stop(struct tty_struct *tty)
134{
Jiri Slaby916b7652012-03-05 14:52:20 +0100135 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long flags;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 local_irq_save(flags);
139 if (info->IER & UART_IER_THRI) {
140 info->IER &= ~UART_IER_THRI;
141 /* disable Tx interrupt and remove any pending interrupts */
Jiri Slaby816807022021-07-14 11:13:11 +0200142 amiga_custom.intena = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 mb();
Jiri Slaby816807022021-07-14 11:13:11 +0200144 amiga_custom.intreq = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 mb();
146 }
147 local_irq_restore(flags);
148}
149
150static void rs_start(struct tty_struct *tty)
151{
Jiri Slaby916b7652012-03-05 14:52:20 +0100152 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long flags;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 local_irq_save(flags);
156 if (info->xmit.head != info->xmit.tail
157 && info->xmit.buf
158 && !(info->IER & UART_IER_THRI)) {
159 info->IER |= UART_IER_THRI;
Jiri Slaby816807022021-07-14 11:13:11 +0200160 amiga_custom.intena = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mb();
162 /* set a pending Tx Interrupt, transmitter should restart now */
Jiri Slaby816807022021-07-14 11:13:11 +0200163 amiga_custom.intreq = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 mb();
165 }
166 local_irq_restore(flags);
167}
168
169/*
170 * ----------------------------------------------------------------------
171 *
Jiri Slabyb4420692021-07-14 11:13:06 +0200172 * Here start the interrupt handling routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * -----------------------------------------------------------------------
175 */
176
Jiri Slaby916b7652012-03-05 14:52:20 +0100177static void receive_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int status;
180 int serdatr;
Jiri Slaby (SUSE)fbdeead2023-12-06 08:36:51 +0100181 u8 ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct async_icount *icount;
Jiri Slaby (SUSE)73b2ed32023-11-21 10:22:47 +0100183 bool overrun = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jiri Slaby916b7652012-03-05 14:52:20 +0100185 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 status = UART_LSR_DR; /* We obviously have a character! */
Jiri Slaby816807022021-07-14 11:13:11 +0200188 serdatr = amiga_custom.serdatr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 mb();
Jiri Slaby816807022021-07-14 11:13:11 +0200190 amiga_custom.intreq = IF_RBF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mb();
192
193 if((serdatr & 0x1ff) == 0)
194 status |= UART_LSR_BI;
195 if(serdatr & SDR_OVRUN)
196 status |= UART_LSR_OE;
197
198 ch = serdatr & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 icount->rx++;
200
201#ifdef SERIAL_DEBUG_INTR
202 printk("DR%02x:%02x...", ch, status);
203#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800204 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
207 * We don't handle parity or frame errors - but I have left
208 * the code in, since I'm not sure that the errors can't be
209 * detected.
210 */
211
212 if (status & (UART_LSR_BI | UART_LSR_PE |
213 UART_LSR_FE | UART_LSR_OE)) {
214 /*
215 * For statistics only
216 */
217 if (status & UART_LSR_BI) {
218 status &= ~(UART_LSR_FE | UART_LSR_PE);
219 icount->brk++;
220 } else if (status & UART_LSR_PE)
221 icount->parity++;
222 else if (status & UART_LSR_FE)
223 icount->frame++;
224 if (status & UART_LSR_OE)
225 icount->overrun++;
226
227 /*
228 * Now check to see if character should be
229 * ignored, and mask off conditions which
230 * should be ignored.
231 */
232 if (status & info->ignore_status_mask)
Jiri Slaby (SUSE)c35e6ec2023-11-21 10:22:46 +0100233 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 status &= info->read_status_mask;
236
237 if (status & (UART_LSR_BI)) {
238#ifdef SERIAL_DEBUG_INTR
239 printk("handling break....");
240#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800241 flag = TTY_BREAK;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100242 if (info->tport.flags & ASYNC_SAK)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100243 do_SAK(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else if (status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800245 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 else if (status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800247 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (status & UART_LSR_OE) {
249 /*
250 * Overrun is special, since it's
251 * reported immediately, and doesn't
252 * affect the current character
253 */
Jiri Slaby (SUSE)73b2ed32023-11-21 10:22:47 +0100254 overrun = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 }
Jiri Slaby92a19f92013-01-03 15:53:03 +0100257 tty_insert_flip_char(&info->tport, ch, flag);
Jiri Slaby (SUSE)73b2ed32023-11-21 10:22:47 +0100258 if (overrun)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100259 tty_insert_flip_char(&info->tport, 0, TTY_OVERRUN);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100260 tty_flip_buffer_push(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Jiri Slaby916b7652012-03-05 14:52:20 +0100263static void transmit_chars(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jiri Slaby816807022021-07-14 11:13:11 +0200265 amiga_custom.intreq = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 mb();
267 if (info->x_char) {
Jiri Slaby816807022021-07-14 11:13:11 +0200268 amiga_custom.serdat = info->x_char | 0x100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 mb();
Jiri Slaby916b7652012-03-05 14:52:20 +0100270 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 info->x_char = 0;
272 return;
273 }
274 if (info->xmit.head == info->xmit.tail
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200275 || info->tport.tty->flow.stopped
Jiri Slaby87758792012-03-05 14:52:24 +0100276 || info->tport.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 info->IER &= ~UART_IER_THRI;
Jiri Slaby816807022021-07-14 11:13:11 +0200278 amiga_custom.intena = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mb();
280 return;
281 }
282
Jiri Slaby816807022021-07-14 11:13:11 +0200283 amiga_custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 mb();
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300285 info->xmit.tail = info->xmit.tail & (UART_XMIT_SIZE - 1);
Jiri Slaby916b7652012-03-05 14:52:20 +0100286 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (CIRC_CNT(info->xmit.head,
289 info->xmit.tail,
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300290 UART_XMIT_SIZE) < WAKEUP_CHARS)
Jiri Slaby87758792012-03-05 14:52:24 +0100291 tty_wakeup(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293#ifdef SERIAL_DEBUG_INTR
294 printk("THRE...");
295#endif
296 if (info->xmit.head == info->xmit.tail) {
Jiri Slaby816807022021-07-14 11:13:11 +0200297 amiga_custom.intena = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 mb();
299 info->IER &= ~UART_IER_THRI;
300 }
301}
302
Jiri Slaby916b7652012-03-05 14:52:20 +0100303static void check_modem_status(struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Jiri Slaby7188dc22012-03-05 14:52:43 +0100305 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
307 unsigned char dstatus;
308 struct async_icount *icount;
309
310 /* Determine bits that have changed */
311 dstatus = status ^ current_ctl_bits;
312 current_ctl_bits = status;
313
314 if (dstatus) {
Jiri Slaby916b7652012-03-05 14:52:20 +0100315 icount = &info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* update input line counters */
317 if (dstatus & SER_DSR)
318 icount->dsr++;
319 if (dstatus & SER_DCD) {
320 icount->dcd++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 if (dstatus & SER_CTS)
323 icount->cts++;
Jiri Slaby7188dc22012-03-05 14:52:43 +0100324 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Peter Hurley2d686552016-04-09 17:53:23 -0700327 if (tty_port_check_carrier(port) && (dstatus & SER_DCD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
329 printk("ttyS%d CD now %s...", info->line,
330 (!(status & SER_DCD)) ? "on" : "off");
331#endif
332 if (!(status & SER_DCD))
Jiri Slaby7188dc22012-03-05 14:52:43 +0100333 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 else {
335#ifdef SERIAL_DEBUG_OPEN
336 printk("doing serial hangup...");
337#endif
Jiri Slaby7188dc22012-03-05 14:52:43 +0100338 if (port->tty)
339 tty_hangup(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
Huang Shijief21ec3d2012-08-22 22:13:36 -0400342 if (tty_port_cts_enabled(port)) {
Jiri Slaby7188dc22012-03-05 14:52:43 +0100343 if (port->tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!(status & SER_CTS)) {
345#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
346 printk("CTS tx start...");
347#endif
Ilpo Järvinen035173c2023-03-09 10:20:35 +0200348 port->tty->hw_stopped = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 info->IER |= UART_IER_THRI;
Jiri Slaby816807022021-07-14 11:13:11 +0200350 amiga_custom.intena = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mb();
352 /* set a pending Tx Interrupt, transmitter should restart now */
Jiri Slaby816807022021-07-14 11:13:11 +0200353 amiga_custom.intreq = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 mb();
Jiri Slaby7188dc22012-03-05 14:52:43 +0100355 tty_wakeup(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357 }
358 } else {
359 if ((status & SER_CTS)) {
360#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
361 printk("CTS tx stop...");
362#endif
Ilpo Järvinen035173c2023-03-09 10:20:35 +0200363 port->tty->hw_stopped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 info->IER &= ~UART_IER_THRI;
365 /* disable Tx interrupt and remove any pending interrupts */
Jiri Slaby816807022021-07-14 11:13:11 +0200366 amiga_custom.intena = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 mb();
Jiri Slaby816807022021-07-14 11:13:11 +0200368 amiga_custom.intreq = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 mb();
370 }
371 }
372 }
373}
374
David Howells7d12e782006-10-05 14:55:46 +0100375static irqreturn_t ser_vbl_int( int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 /* vbl is just a periodic interrupt we tie into to update modem status */
Jiri Slaby916b7652012-03-05 14:52:20 +0100378 struct serial_state *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * TBD - is it better to unregister from this interrupt or to
381 * ignore it if MSI is clear ?
382 */
383 if(info->IER & UART_IER_MSI)
384 check_modem_status(info);
385 return IRQ_HANDLED;
386}
387
David Howells7d12e782006-10-05 14:55:46 +0100388static irqreturn_t ser_rx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jiri Slaby916b7652012-03-05 14:52:20 +0100390 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392#ifdef SERIAL_DEBUG_INTR
393 printk("ser_rx_int...");
394#endif
395
Jiri Slaby87758792012-03-05 14:52:24 +0100396 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return IRQ_NONE;
398
399 receive_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#ifdef SERIAL_DEBUG_INTR
401 printk("end.\n");
402#endif
403 return IRQ_HANDLED;
404}
405
David Howells7d12e782006-10-05 14:55:46 +0100406static irqreturn_t ser_tx_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jiri Slaby916b7652012-03-05 14:52:20 +0100408 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jiri Slaby816807022021-07-14 11:13:11 +0200410 if (amiga_custom.serdatr & SDR_TBE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#ifdef SERIAL_DEBUG_INTR
412 printk("ser_tx_int...");
413#endif
414
Jiri Slaby87758792012-03-05 14:52:24 +0100415 if (!info->tport.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return IRQ_NONE;
417
418 transmit_chars(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#ifdef SERIAL_DEBUG_INTR
420 printk("end.\n");
421#endif
422 }
423 return IRQ_HANDLED;
424}
425
426/*
427 * -------------------------------------------------------------------
428 * Here ends the serial interrupt routines.
429 * -------------------------------------------------------------------
430 */
431
432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * ---------------------------------------------------------------
434 * Low level utility subroutines for the serial driver: routines to
435 * figure out the appropriate timeout for an interrupt chain, routines
436 * to initialize and startup a serial port, and routines to shutdown a
437 * serial port. Useful stuff like that.
438 * ---------------------------------------------------------------
439 */
440
Jiri Slaby588993d2012-03-05 14:52:22 +0100441static int startup(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100443 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
445 int retval=0;
446 unsigned long page;
447
448 page = get_zeroed_page(GFP_KERNEL);
449 if (!page)
450 return -ENOMEM;
451
452 local_irq_save(flags);
453
Peter Hurleyd41861c2016-04-09 17:53:25 -0700454 if (tty_port_initialized(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 free_page(page);
456 goto errout;
457 }
458
459 if (info->xmit.buf)
460 free_page(page);
461 else
462 info->xmit.buf = (unsigned char *) page;
463
464#ifdef SERIAL_DEBUG_OPEN
465 printk("starting up ttys%d ...", info->line);
466#endif
467
468 /* Clear anything in the input buffer */
469
Jiri Slaby816807022021-07-14 11:13:11 +0200470 amiga_custom.intreq = IF_RBF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 mb();
472
473 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
474 if (retval) {
Jiri Slaby93525612021-07-14 11:13:10 +0200475 if (capable(CAP_SYS_ADMIN)) {
476 set_bit(TTY_IO_ERROR, &tty->flags);
477 retval = 0;
478 }
479 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 /* enable both Rx and Tx interrupts */
Jiri Slaby816807022021-07-14 11:13:11 +0200483 amiga_custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 mb();
485 info->IER = UART_IER_MSI;
486
487 /* remember current state of the DCD and CTS bits */
488 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 info->MCR = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100491 if (C_BAUD(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 info->MCR = SER_DTR | SER_RTS;
493 rtsdtr_ctrl(info->MCR);
494
Jiri Slaby588993d2012-03-05 14:52:22 +0100495 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 info->xmit.head = info->xmit.tail = 0;
497
498 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * and set the speed of the serial port
500 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100501 change_speed(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Ilpo Järvinen515be7ba2023-01-17 11:03:47 +0200503 tty_port_set_initialized(port, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 local_irq_restore(flags);
505 return 0;
506
507errout:
508 local_irq_restore(flags);
509 return retval;
510}
511
512/*
513 * This routine will shutdown a serial port; interrupts are disabled, and
514 * DTR is dropped if the hangup on close termio flag is on.
515 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100516static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Peter Hurleyd41861c2016-04-09 17:53:25 -0700520 if (!tty_port_initialized(&info->tport))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#ifdef SERIAL_DEBUG_OPEN
524 printk("Shutting down serial port %d ....\n", info->line);
525#endif
526
527 local_irq_save(flags); /* Disable interrupts */
528
529 /*
530 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
531 * here so the queue might never be waken up
532 */
Jiri Slaby87758792012-03-05 14:52:24 +0100533 wake_up_interruptible(&info->tport.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Free the IRQ, if necessary
537 */
538 free_irq(IRQ_AMIGA_VERTB, info);
539
Andy Shevchenkoa5e3faf2022-02-02 18:56:55 +0200540 free_page((unsigned long)info->xmit.buf);
541 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 info->IER = 0;
Jiri Slaby816807022021-07-14 11:13:11 +0200544 amiga_custom.intena = IF_RBF | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 mb();
546
547 /* disable break condition */
Jiri Slaby816807022021-07-14 11:13:11 +0200548 amiga_custom.adkcon = AC_UARTBRK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 mb();
550
Peter Hurley9db276f2016-01-10 20:36:15 -0800551 if (C_HUPCL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 info->MCR &= ~(SER_DTR|SER_RTS);
553 rtsdtr_ctrl(info->MCR);
554
Jiri Slaby588993d2012-03-05 14:52:22 +0100555 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Ilpo Järvinen515be7ba2023-01-17 11:03:47 +0200557 tty_port_set_initialized(&info->tport, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 local_irq_restore(flags);
559}
560
561
562/*
563 * This routine is called to set the UART divisor registers to match
564 * the specified baud rate for a serial port.
565 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100566static void change_speed(struct tty_struct *tty, struct serial_state *info,
Ilpo Järvinena8c11c12022-08-16 14:57:39 +0300567 const struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100569 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int quot = 0, baud_base, baud;
571 unsigned cflag, cval = 0;
572 int bits;
573 unsigned long flags;
574
Alan Coxadc8d742012-07-14 15:31:47 +0100575 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Byte size is always 8 bits plus parity bit if requested */
578
579 cval = 3; bits = 10;
580 if (cflag & CSTOPB) {
581 cval |= 0x04;
582 bits++;
583 }
584 if (cflag & PARENB) {
585 cval |= UART_LCR_PARITY;
586 bits++;
587 }
588 if (!(cflag & PARODD))
589 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (cflag & CMSPAR)
591 cval |= UART_LCR_SPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* Determine divisor based on baud rate */
Jiri Slaby588993d2012-03-05 14:52:22 +0100594 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!baud)
596 baud = 9600; /* B0 transition handled in rs_set_termios */
Jiri Slaby916b7652012-03-05 14:52:20 +0100597 baud_base = info->baud_base;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100598 if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
Jiri Slaby916b7652012-03-05 14:52:20 +0100599 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 else {
601 if (baud == 134)
602 /* Special case since 134 is really 134.5 */
603 quot = (2*baud_base / 269);
604 else if (baud)
605 quot = baud_base / baud;
606 }
607 /* If the quotient is zero refuse the change */
608 if (!quot && old_termios) {
Alan Coxdb0ef082007-07-15 23:41:47 -0700609 /* FIXME: Will need updating for new tty in the end */
Alan Coxadc8d742012-07-14 15:31:47 +0100610 tty->termios.c_cflag &= ~CBAUD;
611 tty->termios.c_cflag |= (old_termios->c_cflag & CBAUD);
Jiri Slaby588993d2012-03-05 14:52:22 +0100612 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!baud)
614 baud = 9600;
615 if (baud == 38400 &&
Jiri Slaby01bd7302012-03-05 14:52:27 +0100616 (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
Jiri Slaby916b7652012-03-05 14:52:20 +0100617 quot = info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else {
619 if (baud == 134)
620 /* Special case since 134 is really 134.5 */
621 quot = (2*baud_base / 269);
622 else if (baud)
623 quot = baud_base / baud;
624 }
625 }
626 /* As a last resort, if the quotient is zero, default to 9600 bps */
627 if (!quot)
628 quot = baud_base / 9600;
629 info->quot = quot;
Jiri Slaby5a7c7a62021-07-14 11:13:07 +0200630 info->timeout = (XMIT_FIFO_SIZE*HZ*bits*quot) / baud_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 info->timeout += HZ/50; /* Add .02 seconds of slop */
632
633 /* CTS flow control flag and modem status interrupts */
634 info->IER &= ~UART_IER_MSI;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100635 if (port->flags & ASYNC_HARDPPS_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 info->IER |= UART_IER_MSI;
Peter Hurley5604a982016-04-09 17:53:21 -0700637 tty_port_set_cts_flow(port, cflag & CRTSCTS);
638 if (cflag & CRTSCTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 info->IER |= UART_IER_MSI;
Peter Hurley2d686552016-04-09 17:53:23 -0700640 tty_port_set_check_carrier(port, ~cflag & CLOCAL);
641 if (~cflag & CLOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 info->IER |= UART_IER_MSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* TBD:
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300644 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646
647 /*
648 * Set up parity check flag
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
Jiri Slaby588993d2012-03-05 14:52:22 +0100652 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100654 if (I_BRKINT(tty) || I_PARMRK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 info->read_status_mask |= UART_LSR_BI;
656
657 /*
658 * Characters to ignore
659 */
660 info->ignore_status_mask = 0;
Jiri Slaby588993d2012-03-05 14:52:22 +0100661 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
Jiri Slaby588993d2012-03-05 14:52:22 +0100663 if (I_IGNBRK(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 info->ignore_status_mask |= UART_LSR_BI;
665 /*
666 * If we're ignore parity and break indicators, ignore
667 * overruns too. (For real raw support).
668 */
Jiri Slaby588993d2012-03-05 14:52:22 +0100669 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 info->ignore_status_mask |= UART_LSR_OE;
671 }
672 /*
673 * !!! ignore all characters if CREAD is not set
674 */
675 if ((cflag & CREAD) == 0)
676 info->ignore_status_mask |= UART_LSR_DR;
677 local_irq_save(flags);
678
679 {
680 short serper;
681
682 /* Set up the baud rate */
683 serper = quot - 1;
684
685 /* Enable or disable parity bit */
686
687 if(cval & UART_LCR_PARITY)
688 serper |= (SERPER_PARENB);
689
Jiri Slaby816807022021-07-14 11:13:11 +0200690 amiga_custom.serper = serper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 mb();
692 }
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 local_irq_restore(flags);
695}
696
Jiri Slaby (SUSE)dcaafbe2023-08-10 11:15:02 +0200697static int rs_put_char(struct tty_struct *tty, u8 ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Jiri Slaby916b7652012-03-05 14:52:20 +0100699 struct serial_state *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned long flags;
701
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700702 info = tty->driver_data;
703
TINNES Julien RD-MAPS-ISSd9ad7ef2005-06-23 00:10:08 -0700704 if (!info->xmit.buf)
Alan Cox257afa32008-04-30 00:54:02 -0700705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 local_irq_save(flags);
708 if (CIRC_SPACE(info->xmit.head,
709 info->xmit.tail,
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300710 UART_XMIT_SIZE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 info->xmit.buf[info->xmit.head++] = ch;
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300716 info->xmit.head &= UART_XMIT_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 local_irq_restore(flags);
Alan Cox257afa32008-04-30 00:54:02 -0700718 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static void rs_flush_chars(struct tty_struct *tty)
722{
Jiri Slaby916b7652012-03-05 14:52:20 +0100723 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long flags;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (info->xmit.head == info->xmit.tail
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200727 || tty->flow.stopped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 || tty->hw_stopped
729 || !info->xmit.buf)
730 return;
731
732 local_irq_save(flags);
733 info->IER |= UART_IER_THRI;
Jiri Slaby816807022021-07-14 11:13:11 +0200734 amiga_custom.intena = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mb();
736 /* set a pending Tx Interrupt, transmitter should restart now */
Jiri Slaby816807022021-07-14 11:13:11 +0200737 amiga_custom.intreq = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mb();
739 local_irq_restore(flags);
740}
741
Jiri Slaby (SUSE)95713962023-08-10 11:15:03 +0200742static ssize_t rs_write(struct tty_struct * tty, const u8 *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 int c, ret = 0;
Jiri Slaby916b7652012-03-05 14:52:20 +0100745 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 unsigned long flags;
747
Jiri Slabyb3218a72006-10-04 02:15:27 -0700748 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750
Jiri Kosina3abf3be2007-02-10 01:46:48 -0800751 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 while (1) {
753 c = CIRC_SPACE_TO_END(info->xmit.head,
754 info->xmit.tail,
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300755 UART_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (count < c)
757 c = count;
758 if (c <= 0) {
759 break;
760 }
761 memcpy(info->xmit.buf + info->xmit.head, buf, c);
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300762 info->xmit.head = (info->xmit.head + c) & (UART_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 buf += c;
764 count -= c;
765 ret += c;
766 }
767 local_irq_restore(flags);
768
769 if (info->xmit.head != info->xmit.tail
Jiri Slaby6e94dbc2021-05-05 11:19:05 +0200770 && !tty->flow.stopped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 && !tty->hw_stopped
772 && !(info->IER & UART_IER_THRI)) {
773 info->IER |= UART_IER_THRI;
774 local_irq_disable();
Jiri Slaby816807022021-07-14 11:13:11 +0200775 amiga_custom.intena = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 mb();
777 /* set a pending Tx Interrupt, transmitter should restart now */
Jiri Slaby816807022021-07-14 11:13:11 +0200778 amiga_custom.intreq = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 mb();
780 local_irq_restore(flags);
781 }
782 return ret;
783}
784
Jiri Slaby03b3b1a2021-05-05 11:19:15 +0200785static unsigned int rs_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jiri Slaby916b7652012-03-05 14:52:20 +0100787 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300789 return CIRC_SPACE(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200792static unsigned int rs_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Jiri Slaby916b7652012-03-05 14:52:20 +0100794 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Ilpo Järvineneb47b592022-06-24 23:54:23 +0300796 return CIRC_CNT(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static void rs_flush_buffer(struct tty_struct *tty)
800{
Jiri Slaby916b7652012-03-05 14:52:20 +0100801 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned long flags;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 local_irq_save(flags);
805 info->xmit.head = info->xmit.tail = 0;
806 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 tty_wakeup(tty);
808}
809
810/*
811 * This function is used to send a high-priority XON/XOFF character to
812 * the device
813 */
Jiri Slaby (SUSE)3a00da02023-12-06 08:36:49 +0100814static void rs_send_xchar(struct tty_struct *tty, u8 ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jiri Slaby916b7652012-03-05 14:52:20 +0100816 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unsigned long flags;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 info->x_char = ch;
820 if (ch) {
821 /* Make sure transmit interrupts are on */
822
823 /* Check this ! */
824 local_irq_save(flags);
Jiri Slaby816807022021-07-14 11:13:11 +0200825 if(!(amiga_custom.intenar & IF_TBE)) {
826 amiga_custom.intena = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 mb();
828 /* set a pending Tx Interrupt, transmitter should restart now */
Jiri Slaby816807022021-07-14 11:13:11 +0200829 amiga_custom.intreq = IF_SETCLR | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 mb();
831 }
832 local_irq_restore(flags);
833
834 info->IER |= UART_IER_THRI;
835 }
836}
837
838/*
839 * ------------------------------------------------------------
840 * rs_throttle()
841 *
842 * This routine is called by the upper-layer tty layer to signal that
843 * incoming characters should be throttled.
844 * ------------------------------------------------------------
845 */
846static void rs_throttle(struct tty_struct * tty)
847{
Jiri Slaby916b7652012-03-05 14:52:20 +0100848 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned long flags;
850#ifdef SERIAL_DEBUG_THROTTLE
Peter Hurleyfdfb7192016-01-10 22:40:54 -0800851 printk("throttle %s ....\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#endif
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (I_IXOFF(tty))
855 rs_send_xchar(tty, STOP_CHAR(tty));
856
Peter Hurley9db276f2016-01-10 20:36:15 -0800857 if (C_CRTSCTS(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 info->MCR &= ~SER_RTS;
859
860 local_irq_save(flags);
861 rtsdtr_ctrl(info->MCR);
862 local_irq_restore(flags);
863}
864
865static void rs_unthrottle(struct tty_struct * tty)
866{
Jiri Slaby916b7652012-03-05 14:52:20 +0100867 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned long flags;
869#ifdef SERIAL_DEBUG_THROTTLE
Peter Hurleyfdfb7192016-01-10 22:40:54 -0800870 printk("unthrottle %s ....\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#endif
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (I_IXOFF(tty)) {
874 if (info->x_char)
875 info->x_char = 0;
876 else
877 rs_send_xchar(tty, START_CHAR(tty));
878 }
Peter Hurley9db276f2016-01-10 20:36:15 -0800879 if (C_CRTSCTS(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 info->MCR |= SER_RTS;
881 local_irq_save(flags);
882 rtsdtr_ctrl(info->MCR);
883 local_irq_restore(flags);
884}
885
886/*
887 * ------------------------------------------------------------
888 * rs_ioctl() and friends
889 * ------------------------------------------------------------
890 */
891
Al Virob129cbc2018-09-11 21:59:13 -0400892static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Al Virob129cbc2018-09-11 21:59:13 -0400894 struct serial_state *state = tty->driver_data;
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200895 unsigned int close_delay, closing_wait;
Al Virob129cbc2018-09-11 21:59:13 -0400896
Alan Cox89c8d912012-08-08 16:30:13 +0100897 tty_lock(tty);
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200898 close_delay = jiffies_to_msecs(state->tport.close_delay) / 10;
899 closing_wait = state->tport.closing_wait;
900 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
901 closing_wait = jiffies_to_msecs(closing_wait) / 10;
902
Al Virob129cbc2018-09-11 21:59:13 -0400903 ss->line = tty->index;
904 ss->port = state->port;
905 ss->flags = state->tport.flags;
Jiri Slaby5a7c7a62021-07-14 11:13:07 +0200906 ss->xmit_fifo_size = XMIT_FIFO_SIZE;
Al Virob129cbc2018-09-11 21:59:13 -0400907 ss->baud_base = state->baud_base;
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200908 ss->close_delay = close_delay;
909 ss->closing_wait = closing_wait;
Al Virob129cbc2018-09-11 21:59:13 -0400910 ss->custom_divisor = state->custom_divisor;
Alan Cox89c8d912012-08-08 16:30:13 +0100911 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913}
914
Al Virob129cbc2018-09-11 21:59:13 -0400915static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Al Virob129cbc2018-09-11 21:59:13 -0400917 struct serial_state *state = tty->driver_data;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100918 struct tty_port *port = &state->tport;
Jiri Slaby0f9b9682012-03-05 14:52:21 +0100919 bool change_spd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int retval = 0;
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200921 unsigned int close_delay, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alan Cox89c8d912012-08-08 16:30:13 +0100923 tty_lock(tty);
Al Virob129cbc2018-09-11 21:59:13 -0400924 change_spd = ((ss->flags ^ port->flags) & ASYNC_SPD_MASK) ||
925 ss->custom_divisor != state->custom_divisor;
926 if (ss->irq || ss->port != state->port ||
Jiri Slaby5a7c7a62021-07-14 11:13:07 +0200927 ss->xmit_fifo_size != XMIT_FIFO_SIZE) {
Alan Cox89c8d912012-08-08 16:30:13 +0100928 tty_unlock(tty);
Jiri Slaby0f9b9682012-03-05 14:52:21 +0100929 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -0700930 }
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200931
932 close_delay = msecs_to_jiffies(ss->close_delay * 10);
933 closing_wait = ss->closing_wait;
934 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
935 closing_wait = msecs_to_jiffies(closing_wait * 10);
936
Jiri Slaby93525612021-07-14 11:13:10 +0200937 if (!capable(CAP_SYS_ADMIN)) {
Al Virob129cbc2018-09-11 21:59:13 -0400938 if ((ss->baud_base != state->baud_base) ||
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200939 (close_delay != port->close_delay) ||
940 (closing_wait != port->closing_wait) ||
Al Virob129cbc2018-09-11 21:59:13 -0400941 ((ss->flags & ~ASYNC_USR_MASK) !=
Julia Lawalld3a7b832012-04-19 18:12:40 +0200942 (port->flags & ~ASYNC_USR_MASK))) {
Alan Cox89c8d912012-08-08 16:30:13 +0100943 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -EPERM;
Julia Lawalld3a7b832012-04-19 18:12:40 +0200945 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100946 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Al Virob129cbc2018-09-11 21:59:13 -0400947 (ss->flags & ASYNC_USR_MASK));
948 state->custom_divisor = ss->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto check_and_exit;
950 }
951
Al Virob129cbc2018-09-11 21:59:13 -0400952 if (ss->baud_base < 9600) {
Alan Cox89c8d912012-08-08 16:30:13 +0100953 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return -EINVAL;
Alan Coxe18ce492008-04-30 00:53:16 -0700955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /*
958 * OK, past this point, all the error checking has been done.
959 * At this point, we start making changes.....
960 */
961
Al Virob129cbc2018-09-11 21:59:13 -0400962 state->baud_base = ss->baud_base;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100963 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Virob129cbc2018-09-11 21:59:13 -0400964 (ss->flags & ASYNC_FLAGS));
965 state->custom_divisor = ss->custom_divisor;
Johan Hovoldc33a63e2021-04-07 12:23:27 +0200966 port->close_delay = close_delay;
967 port->closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969check_and_exit:
Peter Hurleyd41861c2016-04-09 17:53:25 -0700970 if (tty_port_initialized(port)) {
Jiri Slaby0f9b9682012-03-05 14:52:21 +0100971 if (change_spd) {
Johan Hovold60863802017-06-06 12:54:37 +0200972 /* warn about deprecation unless clearing */
Al Virob129cbc2018-09-11 21:59:13 -0400973 if (ss->flags & ASYNC_SPD_MASK)
Johan Hovold60863802017-06-06 12:54:37 +0200974 dev_warn_ratelimited(tty->dev, "use of SPD flags is deprecated\n");
Jiri Slaby588993d2012-03-05 14:52:22 +0100975 change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 } else
Jiri Slaby588993d2012-03-05 14:52:22 +0100978 retval = startup(tty, state);
Alan Cox89c8d912012-08-08 16:30:13 +0100979 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return retval;
981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/*
984 * get_lsr_info - get line status register info
985 *
986 * Purpose: Let user call ioctl() to get info when the UART physically
987 * is emptied. On bus types like RS485, the transmitter must
988 * release the bus after transmitting. This must be done when
989 * the transmit shift register is empty, not be done when the
990 * transmit holding register is empty. This functionality
991 * allows an RS485 driver to be written in user space.
992 */
Jiri Slaby916b7652012-03-05 14:52:20 +0100993static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 unsigned char status;
996 unsigned int result;
997 unsigned long flags;
998
999 local_irq_save(flags);
Jiri Slaby816807022021-07-14 11:13:11 +02001000 status = amiga_custom.serdatr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 mb();
1002 local_irq_restore(flags);
1003 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1004 if (copy_to_user(value, &result, sizeof(int)))
1005 return -EFAULT;
1006 return 0;
1007}
1008
1009
Alan Cox60b33c12011-02-14 16:26:14 +00001010static int rs_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Jiri Slaby916b7652012-03-05 14:52:20 +01001012 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned char control, status;
1014 unsigned long flags;
1015
Peter Hurley18900ca2016-04-09 17:06:48 -07001016 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -EIO;
1018
1019 control = info->MCR;
1020 local_irq_save(flags);
1021 status = ciab.pra;
1022 local_irq_restore(flags);
1023 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1024 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1025 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1026 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1027 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1028}
1029
Alan Cox20b9d172011-02-14 16:26:50 +00001030static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1031 unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jiri Slaby916b7652012-03-05 14:52:20 +01001033 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned long flags;
1035
Peter Hurley18900ca2016-04-09 17:06:48 -07001036 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -EIO;
1038
1039 local_irq_save(flags);
1040 if (set & TIOCM_RTS)
1041 info->MCR |= SER_RTS;
1042 if (set & TIOCM_DTR)
1043 info->MCR |= SER_DTR;
1044 if (clear & TIOCM_RTS)
1045 info->MCR &= ~SER_RTS;
1046 if (clear & TIOCM_DTR)
1047 info->MCR &= ~SER_DTR;
1048 rtsdtr_ctrl(info->MCR);
1049 local_irq_restore(flags);
1050 return 0;
1051}
1052
1053/*
1054 * rs_break() --- routine which turns the break handling on or off
1055 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001056static int rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 unsigned long flags;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 local_irq_save(flags);
1061 if (break_state == -1)
Jiri Slaby816807022021-07-14 11:13:11 +02001062 amiga_custom.adkcon = AC_SETCLR | AC_UARTBRK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 else
Jiri Slaby816807022021-07-14 11:13:11 +02001064 amiga_custom.adkcon = AC_UARTBRK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mb();
1066 local_irq_restore(flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01001067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Alan Cox05871022010-09-16 18:21:52 +01001070/*
1071 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1072 * Return: write counters to the user passed counter struct
1073 * NB: both 1->0 and 0->1 transitions are counted except for
1074 * RI where only 0->1 is counted.
1075 */
1076static int rs_get_icount(struct tty_struct *tty,
1077 struct serial_icounter_struct *icount)
1078{
Jiri Slaby916b7652012-03-05 14:52:20 +01001079 struct serial_state *info = tty->driver_data;
Alan Cox05871022010-09-16 18:21:52 +01001080 struct async_icount cnow;
1081 unsigned long flags;
1082
1083 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001084 cnow = info->icount;
Alan Cox05871022010-09-16 18:21:52 +01001085 local_irq_restore(flags);
1086 icount->cts = cnow.cts;
1087 icount->dsr = cnow.dsr;
1088 icount->rng = cnow.rng;
1089 icount->dcd = cnow.dcd;
1090 icount->rx = cnow.rx;
1091 icount->tx = cnow.tx;
1092 icount->frame = cnow.frame;
1093 icount->overrun = cnow.overrun;
1094 icount->parity = cnow.parity;
1095 icount->brk = cnow.brk;
1096 icount->buf_overrun = cnow.buf_overrun;
1097
1098 return 0;
1099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Alan Cox6caa76b2011-02-14 16:27:22 +00001101static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unsigned int cmd, unsigned long arg)
1103{
Jiri Slaby916b7652012-03-05 14:52:20 +01001104 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct async_icount cprev, cnow; /* kernel counter temps */
Al Viroab14cae2006-01-12 01:06:30 -08001106 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned long flags;
Arnd Bergmann591cee02014-01-02 13:07:38 +01001108 DEFINE_WAIT(wait);
1109 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Al Viroce5a9832018-09-14 14:46:18 -04001111 if ((cmd != TIOCSERCONFIG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
Peter Hurley18900ca2016-04-09 17:06:48 -07001113 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EIO;
1115 }
1116
1117 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 case TIOCSERCONFIG:
1119 return 0;
1120
1121 case TIOCSERGETLSR: /* Get line status register */
Al Viroab14cae2006-01-12 01:06:30 -08001122 return get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1126 * - mask passed in arg for lines of interest
1127 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1128 * Caller should use TIOCGICOUNT to see which one it was
1129 */
1130 case TIOCMIWAIT:
1131 local_irq_save(flags);
1132 /* note the counters on entry */
Jiri Slaby916b7652012-03-05 14:52:20 +01001133 cprev = info->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 local_irq_restore(flags);
1135 while (1) {
Arnd Bergmann591cee02014-01-02 13:07:38 +01001136 prepare_to_wait(&info->tport.delta_msr_wait,
1137 &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 local_irq_save(flags);
Jiri Slaby916b7652012-03-05 14:52:20 +01001139 cnow = info->icount; /* atomic copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 local_irq_restore(flags);
1141 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
Arnd Bergmann591cee02014-01-02 13:07:38 +01001142 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
1143 ret = -EIO; /* no change => error */
1144 break;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1147 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1148 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1149 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
Arnd Bergmann591cee02014-01-02 13:07:38 +01001150 ret = 0;
1151 break;
1152 }
1153 schedule();
1154 /* see if a signal did it */
1155 if (signal_pending(current)) {
1156 ret = -ERESTARTSYS;
1157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 cprev = cnow;
1160 }
Arnd Bergmann591cee02014-01-02 13:07:38 +01001161 finish_wait(&info->tport.delta_msr_wait, &wait);
1162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 default:
1165 return -ENOIOCTLCMD;
1166 }
1167 return 0;
1168}
1169
Ilpo Järvinena8c11c12022-08-16 14:57:39 +03001170static void rs_set_termios(struct tty_struct *tty, const struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Jiri Slaby916b7652012-03-05 14:52:20 +01001172 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long flags;
Alan Coxadc8d742012-07-14 15:31:47 +01001174 unsigned int cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jiri Slaby588993d2012-03-05 14:52:22 +01001176 change_speed(tty, info, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* Handle transition to B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -08001179 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 info->MCR &= ~(SER_DTR|SER_RTS);
1181 local_irq_save(flags);
1182 rtsdtr_ctrl(info->MCR);
1183 local_irq_restore(flags);
1184 }
1185
1186 /* Handle transition away from B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -08001187 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 info->MCR |= SER_DTR;
Peter Hurley97ef38b2016-04-09 17:11:36 -07001189 if (!C_CRTSCTS(tty) || !tty_throttled(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 info->MCR |= SER_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 local_irq_save(flags);
1192 rtsdtr_ctrl(info->MCR);
1193 local_irq_restore(flags);
1194 }
1195
1196 /* Handle turning off CRTSCTS */
Peter Hurley9db276f2016-01-10 20:36:15 -08001197 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Ilpo Järvinen035173c2023-03-09 10:20:35 +02001198 tty->hw_stopped = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 rs_start(tty);
1200 }
1201
1202#if 0
1203 /*
1204 * No need to wake up processes in open wait, since they
1205 * sample the CLOCAL flag once, and don't recheck it.
1206 * XXX It's not clear whether the current behavior is correct
1207 * or not. Hence, this may change.....
1208 */
Peter Hurley9db276f2016-01-10 20:36:15 -08001209 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 wake_up_interruptible(&info->open_wait);
1211#endif
1212}
1213
1214/*
1215 * ------------------------------------------------------------
1216 * rs_close()
1217 *
1218 * This routine is called when the serial port gets closed. First, we
1219 * wait for the last remaining data to be sent. Then, we unlink its
1220 * async structure from the interrupt chain if necessary, and we free
1221 * that IRQ if nothing is left in the chain.
1222 * ------------------------------------------------------------
1223 */
1224static void rs_close(struct tty_struct *tty, struct file * filp)
1225{
Jiri Slaby916b7652012-03-05 14:52:20 +01001226 struct serial_state *state = tty->driver_data;
Jiri Slaby7188dc22012-03-05 14:52:43 +01001227 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Jiri Slaby9b937422012-03-05 14:52:49 +01001229 if (tty_port_close_start(port, tty, filp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * At this point we stop accepting input. To do this, we
1234 * disable the receive line status interrupts, and tell the
1235 * interrupt driver to stop checking the data ready bit in the
1236 * line status register.
1237 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001238 state->read_status_mask &= ~UART_LSR_DR;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001239 if (tty_port_initialized(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* disable receive interrupts */
Jiri Slaby816807022021-07-14 11:13:11 +02001241 amiga_custom.intena = IF_RBF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 mb();
1243 /* clear any pending receive interrupt */
Jiri Slaby816807022021-07-14 11:13:11 +02001244 amiga_custom.intreq = IF_RBF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 mb();
1246
1247 /*
1248 * Before we drop DTR, make sure the UART transmitter
1249 * has completely drained; this is especially
1250 * important if there is a transmit FIFO!
1251 */
Jiri Slaby916b7652012-03-05 14:52:20 +01001252 rs_wait_until_sent(tty, state->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Jiri Slaby588993d2012-03-05 14:52:22 +01001254 shutdown(tty, state);
Alan Cox978e5952008-04-30 00:53:59 -07001255 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 tty_ldisc_flush(tty);
Jiri Slaby7188dc22012-03-05 14:52:43 +01001258 port->tty = NULL;
Jiri Slabyb8edebe2012-03-05 14:52:48 +01001259
1260 tty_port_close_end(port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263/*
1264 * rs_wait_until_sent() --- wait until the transmitter is empty
1265 */
1266static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1267{
Jiri Slaby916b7652012-03-05 14:52:20 +01001268 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unsigned long orig_jiffies, char_time;
1270 int lsr;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 orig_jiffies = jiffies;
Alan Cox978e5952008-04-30 00:53:59 -07001273
Arnd Bergmann20365212010-06-01 22:53:07 +02001274 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * Set the check interval to be 1/5 of the estimated time to
1276 * send a single character, and make it at least 1. The check
1277 * interval should also be less than the timeout.
1278 *
1279 * Note: we have to use pretty tight timings here to satisfy
1280 * the NIST-PCTS.
1281 */
Jiri Slaby5a7c7a62021-07-14 11:13:07 +02001282 char_time = (info->timeout - HZ/50) / XMIT_FIFO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 char_time = char_time / 5;
1284 if (char_time == 0)
1285 char_time = 1;
1286 if (timeout)
1287 char_time = min_t(unsigned long, char_time, timeout);
1288 /*
1289 * If the transmitter hasn't cleared in twice the approximate
1290 * amount of time to send the entire FIFO, it probably won't
1291 * ever clear. This assumes the UART isn't doing flow
1292 * control, which is currently the case. Hence, if it ever
1293 * takes longer than info->timeout, this is probably due to a
1294 * UART bug of some kind. So, we clamp the timeout parameter at
1295 * 2*info->timeout.
1296 */
1297 if (!timeout || timeout > 2*info->timeout)
1298 timeout = 2*info->timeout;
1299#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1300 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1301 printk("jiff=%lu...", jiffies);
1302#endif
Jiri Slaby816807022021-07-14 11:13:11 +02001303 while(!((lsr = amiga_custom.serdatr) & SDR_TSRE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1305 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1306#endif
1307 msleep_interruptible(jiffies_to_msecs(char_time));
1308 if (signal_pending(current))
1309 break;
1310 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1311 break;
1312 }
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001313 __set_current_state(TASK_RUNNING);
Jiri Slabyeff4b0b2011-07-14 14:35:13 +02001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1316 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1317#endif
1318}
1319
1320/*
1321 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1322 */
1323static void rs_hangup(struct tty_struct *tty)
1324{
Jiri Slaby916b7652012-03-05 14:52:20 +01001325 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 rs_flush_buffer(tty);
Jiri Slaby588993d2012-03-05 14:52:22 +01001328 shutdown(tty, info);
Jiri Slaby12c80352012-03-05 14:52:26 +01001329 info->tport.count = 0;
Ilpo Järvinen9b5aa542023-01-17 11:03:49 +02001330 tty_port_set_active(&info->tport, false);
Jiri Slaby87758792012-03-05 14:52:24 +01001331 info->tport.tty = NULL;
1332 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * This routine is called whenever a serial port is opened. It
1337 * enables interrupts for a serial port, linking in its async structure into
1338 * the IRQ chain. It also performs the serial-specific
1339 * initialization for the tty structure.
1340 */
1341static int rs_open(struct tty_struct *tty, struct file * filp)
1342{
Jiri Slaby7ec31142021-07-14 11:13:08 +02001343 struct tty_port *port = tty->port;
1344 struct serial_state *info = container_of(port, struct serial_state,
1345 tport);
Jiri Slaby410235f2012-03-05 14:52:01 +01001346 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jiri Slaby7188dc22012-03-05 14:52:43 +01001348 port->count++;
1349 port->tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +01001350 tty->driver_data = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Jiri Slaby588993d2012-03-05 14:52:22 +01001352 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (retval) {
1354 return retval;
1355 }
1356
Jiri Slaby6e1aeb02012-03-05 14:52:47 +01001357 return tty_port_block_til_ready(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360/*
1361 * /proc fs routines....
1362 */
1363
Jiri Slabyff169e52012-03-05 14:52:44 +01001364static inline void line_info(struct seq_file *m, int line,
1365 struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 char stat_buf[30], control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 unsigned long flags;
1369
Jiri Slabyff169e52012-03-05 14:52:44 +01001370 seq_printf(m, "%d: uart:amiga_builtin", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 local_irq_save(flags);
1373 status = ciab.pra;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001374 control = tty_port_initialized(&state->tport) ? state->MCR : status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 local_irq_restore(flags);
1376
1377 stat_buf[0] = 0;
1378 stat_buf[1] = 0;
1379 if(!(control & SER_RTS))
1380 strcat(stat_buf, "|RTS");
1381 if(!(status & SER_CTS))
1382 strcat(stat_buf, "|CTS");
1383 if(!(control & SER_DTR))
1384 strcat(stat_buf, "|DTR");
1385 if(!(status & SER_DSR))
1386 strcat(stat_buf, "|DSR");
1387 if(!(status & SER_DCD))
1388 strcat(stat_buf, "|CD");
1389
Jiri Slaby916b7652012-03-05 14:52:20 +01001390 if (state->quot)
1391 seq_printf(m, " baud:%d", state->baud_base / state->quot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alexey Dobriyand5940272009-03-31 15:19:23 -07001393 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (state->icount.frame)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001396 seq_printf(m, " fe:%d", state->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (state->icount.parity)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001399 seq_printf(m, " pe:%d", state->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (state->icount.brk)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001402 seq_printf(m, " brk:%d", state->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (state->icount.overrun)
Alexey Dobriyand5940272009-03-31 15:19:23 -07001405 seq_printf(m, " oe:%d", state->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
1408 * Last thing is the RS-232 status lines
1409 */
Alexey Dobriyand5940272009-03-31 15:19:23 -07001410 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Alexey Dobriyand5940272009-03-31 15:19:23 -07001413static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Jiri Slaby1cd25472021-07-14 11:13:05 +02001415 seq_printf(m, "serinfo:1.0 driver:4.30\n");
Jiri Slaby6cc7bda2021-07-14 11:13:13 +02001416 line_info(m, 0, &serial_state);
Alexey Dobriyand5940272009-03-31 15:19:23 -07001417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420/*
1421 * ---------------------------------------------------------------------
1422 * rs_init() and friends
1423 *
1424 * rs_init() is called at boot-time to initialize the serial driver.
1425 * ---------------------------------------------------------------------
1426 */
1427
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001428static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 .open = rs_open,
1430 .close = rs_close,
1431 .write = rs_write,
1432 .put_char = rs_put_char,
1433 .flush_chars = rs_flush_chars,
1434 .write_room = rs_write_room,
1435 .chars_in_buffer = rs_chars_in_buffer,
1436 .flush_buffer = rs_flush_buffer,
1437 .ioctl = rs_ioctl,
1438 .throttle = rs_throttle,
1439 .unthrottle = rs_unthrottle,
1440 .set_termios = rs_set_termios,
1441 .stop = rs_stop,
1442 .start = rs_start,
1443 .hangup = rs_hangup,
1444 .break_ctl = rs_break,
1445 .send_xchar = rs_send_xchar,
1446 .wait_until_sent = rs_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .tiocmget = rs_tiocmget,
1448 .tiocmset = rs_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001449 .get_icount = rs_get_icount,
Al Virob129cbc2018-09-11 21:59:13 -04001450 .set_serial = set_serial_info,
1451 .get_serial = get_serial_info,
Christoph Hellwig8a8dcab2018-04-13 21:04:45 +02001452 .proc_show = rs_proc_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453};
1454
Ilpo Järvinenb300fb22023-01-17 11:03:52 +02001455static bool amiga_carrier_raised(struct tty_port *port)
Jiri Slabyf1166602012-03-05 14:52:46 +01001456{
1457 return !(ciab.pra & SER_DCD);
1458}
1459
Ilpo Järvinen5701cb82023-01-17 11:03:57 +02001460static void amiga_dtr_rts(struct tty_port *port, bool active)
Jiri Slabyf1166602012-03-05 14:52:46 +01001461{
1462 struct serial_state *info = container_of(port, struct serial_state,
1463 tport);
1464 unsigned long flags;
1465
Ilpo Järvinen5701cb82023-01-17 11:03:57 +02001466 if (active)
Jiri Slabyf1166602012-03-05 14:52:46 +01001467 info->MCR |= SER_DTR|SER_RTS;
1468 else
1469 info->MCR &= ~(SER_DTR|SER_RTS);
1470
1471 local_irq_save(flags);
1472 rtsdtr_ctrl(info->MCR);
1473 local_irq_restore(flags);
1474}
1475
1476static const struct tty_port_operations amiga_port_ops = {
1477 .carrier_raised = amiga_carrier_raised,
1478 .dtr_rts = amiga_dtr_rts,
1479};
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481/*
1482 * The serial driver boot-time initialization code!
1483 */
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001484static int __init amiga_serial_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Jiri Slaby6cc7bda2021-07-14 11:13:13 +02001486 struct serial_state *state = &serial_state;
Jiri Slaby0524513a2021-07-23 09:43:12 +02001487 struct tty_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned long flags;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001489 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jiri Slaby39b7b422021-07-23 09:43:13 +02001491 driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
1492 if (IS_ERR(driver))
1493 return PTR_ERR(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /* Initialize the tty_driver structure */
1496
Jiri Slaby0524513a2021-07-23 09:43:12 +02001497 driver->driver_name = "amiserial";
1498 driver->name = "ttyS";
1499 driver->major = TTY_MAJOR;
1500 driver->minor_start = 64;
1501 driver->type = TTY_DRIVER_TYPE_SERIAL;
1502 driver->subtype = SERIAL_TYPE_NORMAL;
1503 driver->init_termios = tty_std_termios;
1504 driver->init_termios.c_cflag =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Jiri Slaby0524513a2021-07-23 09:43:12 +02001506 tty_set_operations(driver, &serial_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Jiri Slaby5d4317a2021-07-14 11:13:09 +02001508 memset(state, 0, sizeof(*state));
Jiri Slaby816807022021-07-14 11:13:11 +02001509 state->port = (int)&amiga_custom.serdatr; /* Just to give it a value */
Jiri Slaby87758792012-03-05 14:52:24 +01001510 tty_port_init(&state->tport);
Jiri Slabyf1166602012-03-05 14:52:46 +01001511 state->tport.ops = &amiga_port_ops;
Jiri Slaby0524513a2021-07-23 09:43:12 +02001512 tty_port_link_device(&state->tport, driver, 0);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001513
Jiri Slaby0524513a2021-07-23 09:43:12 +02001514 error = tty_register_driver(driver);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001515 if (error)
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02001516 goto fail_tty_driver_kref_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Jiri Slabyff169e52012-03-05 14:52:44 +01001518 printk(KERN_INFO "ttyS0 is the amiga builtin serial port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* Hardware set up */
1521
1522 state->baud_base = amiga_colorclock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* set ISRs, and then disable the rx interrupts */
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001525 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1526 if (error)
1527 goto fail_unregister;
1528
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001529 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001530 "serial RX", state);
1531 if (error)
1532 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Julia Lawallc70c0362010-04-06 14:34:46 -07001534 local_irq_save(flags);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* turn off Rx and Tx interrupts */
Jiri Slaby816807022021-07-14 11:13:11 +02001537 amiga_custom.intena = IF_RBF | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 mb();
1539
1540 /* clear any pending interrupt */
Jiri Slaby816807022021-07-14 11:13:11 +02001541 amiga_custom.intreq = IF_RBF | IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mb();
1543
1544 local_irq_restore(flags);
1545
1546 /*
1547 * set the appropriate directions for the modem control flags,
1548 * and clear RTS and DTR
1549 */
1550 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1551 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1552
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001553 platform_set_drvdata(pdev, state);
1554
Jiri Slaby0524513a2021-07-23 09:43:12 +02001555 serial_driver = driver;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001558
1559fail_free_irq:
1560 free_irq(IRQ_AMIGA_TBE, state);
1561fail_unregister:
Jiri Slaby0524513a2021-07-23 09:43:12 +02001562 tty_unregister_driver(driver);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02001563fail_tty_driver_kref_put:
Jiri Slaby191c5f12012-11-15 09:49:56 +01001564 tty_port_destroy(&state->tport);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02001565 tty_driver_kref_put(driver);
Geert Uytterhoeven5edc3042008-12-30 14:13:41 +01001566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Uwe Kleine-Königd4c22ec2024-02-18 09:53:54 +01001569static void __exit amiga_serial_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001571 struct serial_state *state = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Jiri Slaby6c2e6312021-03-02 07:22:04 +01001573 tty_unregister_driver(serial_driver);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02001574 tty_driver_kref_put(serial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001575 tty_port_destroy(&state->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jiri Slaby916b7652012-03-05 14:52:20 +01001577 free_irq(IRQ_AMIGA_TBE, state);
1578 free_irq(IRQ_AMIGA_RBF, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001581static struct platform_driver amiga_serial_driver = {
Uwe Kleine-Königd4c22ec2024-02-18 09:53:54 +01001582 .remove_new = __exit_p(amiga_serial_remove),
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001583 .driver = {
1584 .name = "amiga-serial",
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001585 },
1586};
1587
Jingoo Hanfec6bee2013-03-05 12:29:20 +09001588module_platform_driver_probe(amiga_serial_driver, amiga_serial_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001591#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593/*
1594 * ------------------------------------------------------------
1595 * Serial console driver
1596 * ------------------------------------------------------------
1597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599static void amiga_serial_putc(char c)
1600{
Jiri Slaby816807022021-07-14 11:13:11 +02001601 amiga_custom.serdat = (unsigned char)c | 0x100;
1602 while (!(amiga_custom.serdatr & 0x2000))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 barrier();
1604}
1605
1606/*
1607 * Print a string to the serial port trying not to disturb
1608 * any possible real use of the port...
1609 *
1610 * The console must be locked when we get here.
1611 */
1612static void serial_console_write(struct console *co, const char *s,
1613 unsigned count)
1614{
Jiri Slaby816807022021-07-14 11:13:11 +02001615 unsigned short intena = amiga_custom.intenar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Jiri Slaby816807022021-07-14 11:13:11 +02001617 amiga_custom.intena = IF_TBE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 while (count--) {
1620 if (*s == '\n')
1621 amiga_serial_putc('\r');
1622 amiga_serial_putc(*s++);
1623 }
1624
Jiri Slaby816807022021-07-14 11:13:11 +02001625 amiga_custom.intena = IF_SETCLR | (intena & IF_TBE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628static struct tty_driver *serial_console_device(struct console *c, int *index)
1629{
1630 *index = 0;
1631 return serial_driver;
1632}
1633
1634static struct console sercons = {
1635 .name = "ttyS",
1636 .write = serial_console_write,
1637 .device = serial_console_device,
1638 .flags = CON_PRINTBUFFER,
1639 .index = -1,
1640};
1641
1642/*
1643 * Register console.
1644 */
1645static int __init amiserial_console_init(void)
1646{
Geert Uytterhoeven3dcf3442013-11-22 16:47:27 +01001647 if (!MACH_IS_AMIGA)
1648 return -ENODEV;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 register_console(&sercons);
1651 return 0;
1652}
1653console_initcall(amiserial_console_init);
Geert Uytterhoevend1a35e42008-10-26 19:51:14 +01001654
1655#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657MODULE_LICENSE("GPL");
Geert Uytterhoeven826e8c82009-04-05 13:12:30 +02001658MODULE_ALIAS("platform:amiga-serial");