blob: e98aef7970657c9060c3f57f0e02f1035666d2ea [file] [log] [blame]
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Serial port driver for the ETRAX 100LX chip
3 *
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004 * Copyright (C) 1998-2007 Axis Communications AB
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Many, many authors. Based once upon a time on serial.c for 16x50.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10static char *serial_version = "$Revision: 1.25 $";
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/interrupt.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/major.h>
21#include <linux/string.h>
22#include <linux/fcntl.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
25#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000027#include <linux/mutex.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070028#include <linux/bitops.h>
Jesper Nilsson9e040a32009-04-30 15:08:13 -070029#include <linux/seq_file.h>
30#include <linux/delay.h>
31#include <linux/module.h>
32#include <linux/uaccess.h>
33#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/irq.h>
Jesper Nilsson77accbf2007-11-14 17:01:15 -080036#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jesper Nilsson556dcee2008-10-21 17:45:58 +020038#include <arch/svinto.h>
David Howellsb1a154d2012-03-28 18:30:02 +010039#include <arch/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* non-arch dependent serial structures are in linux/serial.h */
42#include <linux/serial.h>
43/* while we keep our own stuff (struct e100_serial) in a local .h file */
Jesper Nilsson77accbf2007-11-14 17:01:15 -080044#include "crisv10.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/fasttimer.h>
Jesper Nilsson556dcee2008-10-21 17:45:58 +020046#include <arch/io_interface_mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
49#ifndef CONFIG_ETRAX_FAST_TIMER
50#error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
51#endif
52#endif
53
54#if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
55 (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
56#error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
57#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * All of the compatibilty code so we can compile serial.c against
61 * older kernels is hidden in serial_compat.h
62 */
63#if defined(LOCAL_HEADERS)
64#include "serial_compat.h"
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067struct tty_driver *serial_driver;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* number of characters left in xmit buffer before we ask for more */
70#define WAKEUP_CHARS 256
71
72//#define SERIAL_DEBUG_INTR
73//#define SERIAL_DEBUG_OPEN
74//#define SERIAL_DEBUG_FLOW
75//#define SERIAL_DEBUG_DATA
76//#define SERIAL_DEBUG_THROTTLE
77//#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
78//#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
79
80/* Enable this to use serial interrupts to handle when you
81 expect the first received event on the serial port to
82 be an error, break or similar. Used to be able to flash IRMA
83 from eLinux */
84#define SERIAL_HANDLE_EARLY_ERRORS
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Currently 16 descriptors x 128 bytes = 2048 bytes */
87#define SERIAL_DESCR_BUF_SIZE 256
88
89#define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
90#define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
91
92/* We don't want to load the system with massive fast timer interrupt
93 * on high baudrates so limit it to 250 us (4kHz) */
94#define MIN_FLUSH_TIME_USEC 250
95
96/* Add an x here to log a lot of timer stuff */
97#define TIMERD(x)
98/* Debug details of interrupt handling */
99#define DINTR1(x) /* irq on/off, errors */
100#define DINTR2(x) /* tx and rx */
101/* Debug flip buffer stuff */
102#define DFLIP(x)
103/* Debug flow control and overview of data flow */
104#define DFLOW(x)
105#define DBAUD(x)
106#define DLOG_INT_TRIG(x)
107
108//#define DEBUG_LOG_INCLUDED
109#ifndef DEBUG_LOG_INCLUDED
110#define DEBUG_LOG(line, string, value)
111#else
112struct debug_log_info
113{
114 unsigned long time;
115 unsigned long timer_data;
116// int line;
117 const char *string;
118 int value;
119};
120#define DEBUG_LOG_SIZE 4096
121
122struct debug_log_info debug_log[DEBUG_LOG_SIZE];
123int debug_log_pos = 0;
124
125#define DEBUG_LOG(_line, _string, _value) do { \
126 if ((_line) == SERIAL_DEBUG_LINE) {\
127 debug_log_func(_line, _string, _value); \
128 }\
129}while(0)
130
131void debug_log_func(int line, const char *string, int value)
132{
133 if (debug_log_pos < DEBUG_LOG_SIZE) {
134 debug_log[debug_log_pos].time = jiffies;
135 debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
136// debug_log[debug_log_pos].line = line;
137 debug_log[debug_log_pos].string = string;
138 debug_log[debug_log_pos].value = value;
139 debug_log_pos++;
140 }
141 /*printk(string, value);*/
142}
143#endif
144
145#ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
146/* Default number of timer ticks before flushing rx fifo
147 * When using "little data, low latency applications: use 0
148 * When using "much data applications (PPP)" use ~5
149 */
150#define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
151#endif
152
153unsigned long timer_data_to_ns(unsigned long timer_data);
154
155static void change_speed(struct e100_serial *info);
156static void rs_throttle(struct tty_struct * tty);
157static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800158static int rs_write(struct tty_struct *tty,
159 const unsigned char *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#ifdef CONFIG_ETRAX_RS485
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800161static int e100_write_rs485(struct tty_struct *tty,
162 const unsigned char *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800164static int get_lsr_info(struct e100_serial *info, unsigned int *value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166
167#define DEF_BAUD 115200 /* 115.2 kbit/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
169/* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
170#define DEF_TX 0x80 /* or SERIAL_CTRL_B */
171
172/* offsets from R_SERIALx_CTRL */
173
174#define REG_DATA 0
175#define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
176#define REG_TR_DATA 0
177#define REG_STATUS 1
178#define REG_TR_CTRL 1
179#define REG_REC_CTRL 2
180#define REG_BAUD 3
181#define REG_XOFF 4 /* this is a 32 bit register */
182
183/* The bitfields are the same for all serial ports */
184#define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
185#define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
186#define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
187#define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
188#define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
189
190#define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
191
192/* Values for info->errorcode */
193#define ERRCODE_SET_BREAK (TTY_BREAK)
194#define ERRCODE_INSERT 0x100
195#define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
196
197#define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
198
199/*
200 * General note regarding the use of IO_* macros in this file:
201 *
202 * We will use the bits defined for DMA channel 6 when using various
203 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
204 * the same for all channels (which of course they are).
205 *
206 * We will also use the bits defined for serial port 0 when writing commands
207 * to the different ports, as these bits too are the same for all ports.
208 */
209
210
211/* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
212static const unsigned long e100_ser_int_mask = 0
213#ifdef CONFIG_ETRAX_SERIAL_PORT0
214| IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
215#endif
216#ifdef CONFIG_ETRAX_SERIAL_PORT1
217| IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
218#endif
219#ifdef CONFIG_ETRAX_SERIAL_PORT2
220| IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
221#endif
222#ifdef CONFIG_ETRAX_SERIAL_PORT3
223| IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
224#endif
225;
226unsigned long r_alt_ser_baudrate_shadow = 0;
227
228/* this is the data for the four serial ports in the etrax100 */
229/* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
230/* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
231
232static struct e100_serial rs_table[] = {
233 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100234 .ioport = (unsigned char *)R_SERIAL0_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .irq = 1U << 12, /* uses DMA 6 and 7 */
236 .oclrintradr = R_DMA_CH6_CLR_INTR,
237 .ofirstadr = R_DMA_CH6_FIRST,
238 .ocmdadr = R_DMA_CH6_CMD,
239 .ostatusadr = R_DMA_CH6_STATUS,
240 .iclrintradr = R_DMA_CH7_CLR_INTR,
241 .ifirstadr = R_DMA_CH7_FIRST,
242 .icmdadr = R_DMA_CH7_CMD,
243 .idescradr = R_DMA_CH7_DESCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .rx_ctrl = DEF_RX,
245 .tx_ctrl = DEF_TX,
246 .iseteop = 2,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800247 .dma_owner = dma_ser0,
248 .io_if = if_serial_0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#ifdef CONFIG_ETRAX_SERIAL_PORT0
250 .enabled = 1,
251#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
252 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800253 .dma_out_nbr = SER0_TX_DMA_NBR,
254 .dma_out_irq_nbr = SER0_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800255 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800256 .dma_out_irq_description = "serial 0 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#else
258 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800259 .dma_out_nbr = UINT_MAX,
260 .dma_out_irq_nbr = 0,
261 .dma_out_irq_flags = 0,
262 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif
264#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
265 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800266 .dma_in_nbr = SER0_RX_DMA_NBR,
267 .dma_in_irq_nbr = SER0_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800268 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800269 .dma_in_irq_description = "serial 0 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800271 .dma_in_enabled = 0,
272 .dma_in_nbr = UINT_MAX,
273 .dma_in_irq_nbr = 0,
274 .dma_in_irq_flags = 0,
275 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif
277#else
278 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800279 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .dma_out_enabled = 0,
281 .dma_in_enabled = 0
282#endif
283
284}, /* ttyS0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100286 .ioport = (unsigned char *)R_SERIAL1_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .irq = 1U << 16, /* uses DMA 8 and 9 */
288 .oclrintradr = R_DMA_CH8_CLR_INTR,
289 .ofirstadr = R_DMA_CH8_FIRST,
290 .ocmdadr = R_DMA_CH8_CMD,
291 .ostatusadr = R_DMA_CH8_STATUS,
292 .iclrintradr = R_DMA_CH9_CLR_INTR,
293 .ifirstadr = R_DMA_CH9_FIRST,
294 .icmdadr = R_DMA_CH9_CMD,
295 .idescradr = R_DMA_CH9_DESCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .rx_ctrl = DEF_RX,
297 .tx_ctrl = DEF_TX,
298 .iseteop = 3,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800299 .dma_owner = dma_ser1,
300 .io_if = if_serial_1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifdef CONFIG_ETRAX_SERIAL_PORT1
302 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800303 .io_if_description = "ser1",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
305 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800306 .dma_out_nbr = SER1_TX_DMA_NBR,
307 .dma_out_irq_nbr = SER1_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800308 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800309 .dma_out_irq_description = "serial 1 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#else
311 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800312 .dma_out_nbr = UINT_MAX,
313 .dma_out_irq_nbr = 0,
314 .dma_out_irq_flags = 0,
315 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#endif
317#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
318 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800319 .dma_in_nbr = SER1_RX_DMA_NBR,
320 .dma_in_irq_nbr = SER1_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800321 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800322 .dma_in_irq_description = "serial 1 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800324 .dma_in_enabled = 0,
325 .dma_in_enabled = 0,
326 .dma_in_nbr = UINT_MAX,
327 .dma_in_irq_nbr = 0,
328 .dma_in_irq_flags = 0,
329 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#endif
331#else
332 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800333 .io_if_description = NULL,
334 .dma_in_irq_nbr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 .dma_out_enabled = 0,
336 .dma_in_enabled = 0
337#endif
338}, /* ttyS1 */
339
340 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100341 .ioport = (unsigned char *)R_SERIAL2_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .irq = 1U << 4, /* uses DMA 2 and 3 */
343 .oclrintradr = R_DMA_CH2_CLR_INTR,
344 .ofirstadr = R_DMA_CH2_FIRST,
345 .ocmdadr = R_DMA_CH2_CMD,
346 .ostatusadr = R_DMA_CH2_STATUS,
347 .iclrintradr = R_DMA_CH3_CLR_INTR,
348 .ifirstadr = R_DMA_CH3_FIRST,
349 .icmdadr = R_DMA_CH3_CMD,
350 .idescradr = R_DMA_CH3_DESCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 .rx_ctrl = DEF_RX,
352 .tx_ctrl = DEF_TX,
353 .iseteop = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800354 .dma_owner = dma_ser2,
355 .io_if = if_serial_2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#ifdef CONFIG_ETRAX_SERIAL_PORT2
357 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800358 .io_if_description = "ser2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
360 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800361 .dma_out_nbr = SER2_TX_DMA_NBR,
362 .dma_out_irq_nbr = SER2_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800363 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800364 .dma_out_irq_description = "serial 2 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#else
366 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800367 .dma_out_nbr = UINT_MAX,
368 .dma_out_irq_nbr = 0,
369 .dma_out_irq_flags = 0,
370 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#endif
372#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
373 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800374 .dma_in_nbr = SER2_RX_DMA_NBR,
375 .dma_in_irq_nbr = SER2_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800376 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800377 .dma_in_irq_description = "serial 2 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800379 .dma_in_enabled = 0,
380 .dma_in_nbr = UINT_MAX,
381 .dma_in_irq_nbr = 0,
382 .dma_in_irq_flags = 0,
383 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#endif
385#else
386 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800387 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .dma_out_enabled = 0,
389 .dma_in_enabled = 0
390#endif
391 }, /* ttyS2 */
392
393 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100394 .ioport = (unsigned char *)R_SERIAL3_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .irq = 1U << 8, /* uses DMA 4 and 5 */
396 .oclrintradr = R_DMA_CH4_CLR_INTR,
397 .ofirstadr = R_DMA_CH4_FIRST,
398 .ocmdadr = R_DMA_CH4_CMD,
399 .ostatusadr = R_DMA_CH4_STATUS,
400 .iclrintradr = R_DMA_CH5_CLR_INTR,
401 .ifirstadr = R_DMA_CH5_FIRST,
402 .icmdadr = R_DMA_CH5_CMD,
403 .idescradr = R_DMA_CH5_DESCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .rx_ctrl = DEF_RX,
405 .tx_ctrl = DEF_TX,
406 .iseteop = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800407 .dma_owner = dma_ser3,
408 .io_if = if_serial_3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#ifdef CONFIG_ETRAX_SERIAL_PORT3
410 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800411 .io_if_description = "ser3",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
413 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800414 .dma_out_nbr = SER3_TX_DMA_NBR,
415 .dma_out_irq_nbr = SER3_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800416 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800417 .dma_out_irq_description = "serial 3 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#else
419 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800420 .dma_out_nbr = UINT_MAX,
421 .dma_out_irq_nbr = 0,
422 .dma_out_irq_flags = 0,
423 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
425#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
426 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800427 .dma_in_nbr = SER3_RX_DMA_NBR,
428 .dma_in_irq_nbr = SER3_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800429 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800430 .dma_in_irq_description = "serial 3 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800432 .dma_in_enabled = 0,
433 .dma_in_nbr = UINT_MAX,
434 .dma_in_irq_nbr = 0,
435 .dma_in_irq_flags = 0,
436 .dma_in_irq_description = NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#endif
438#else
439 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800440 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .dma_out_enabled = 0,
442 .dma_in_enabled = 0
443#endif
444 } /* ttyS3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
447
448#define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
451static struct fast_timer fast_timers[NR_PORTS];
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* RS-485 */
455#if defined(CONFIG_ETRAX_RS485)
456#ifdef CONFIG_ETRAX_FAST_TIMER
457static struct fast_timer fast_timers_rs485[NR_PORTS];
458#endif
459#if defined(CONFIG_ETRAX_RS485_ON_PA)
460static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
463
464/* Info and macros needed for each ports extra control/status signals. */
465#define E100_STRUCT_PORT(line, pinname) \
466 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
467 (R_PORT_PA_DATA): ( \
468 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
469 (R_PORT_PB_DATA):&dummy_ser[line]))
470
471#define E100_STRUCT_SHADOW(line, pinname) \
472 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
473 (&port_pa_data_shadow): ( \
474 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
475 (&port_pb_data_shadow):&dummy_ser[line]))
476#define E100_STRUCT_MASK(line, pinname) \
477 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
478 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
479 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
480 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
481
482#define DUMMY_DTR_MASK 1
483#define DUMMY_RI_MASK 2
484#define DUMMY_DSR_MASK 4
485#define DUMMY_CD_MASK 8
486static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
487
488/* If not all status pins are used or disabled, use mixed mode */
489#ifdef CONFIG_ETRAX_SERIAL_PORT0
490
491#define SER0_PA_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PA_BIT+CONFIG_ETRAX_SER0_RI_ON_PA_BIT+CONFIG_ETRAX_SER0_DSR_ON_PA_BIT+CONFIG_ETRAX_SER0_CD_ON_PA_BIT)
492
493#if SER0_PA_BITSUM != -4
494# if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
495# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
496# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
497# endif
498# endif
499# if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
500# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
501# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
502# endif
503# endif
504# if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
505# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
506# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
507# endif
508# endif
509# if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
510# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
511# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
512# endif
513# endif
514#endif
515
516#define SER0_PB_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PB_BIT+CONFIG_ETRAX_SER0_RI_ON_PB_BIT+CONFIG_ETRAX_SER0_DSR_ON_PB_BIT+CONFIG_ETRAX_SER0_CD_ON_PB_BIT)
517
518#if SER0_PB_BITSUM != -4
519# if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
520# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
521# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
522# endif
523# endif
524# if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
525# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
526# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
527# endif
528# endif
529# if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
530# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
531# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
532# endif
533# endif
534# if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
535# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
536# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
537# endif
538# endif
539#endif
540
541#endif /* PORT0 */
542
543
544#ifdef CONFIG_ETRAX_SERIAL_PORT1
545
546#define SER1_PA_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PA_BIT+CONFIG_ETRAX_SER1_RI_ON_PA_BIT+CONFIG_ETRAX_SER1_DSR_ON_PA_BIT+CONFIG_ETRAX_SER1_CD_ON_PA_BIT)
547
548#if SER1_PA_BITSUM != -4
549# if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
550# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
551# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
552# endif
553# endif
554# if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
555# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
556# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
557# endif
558# endif
559# if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
560# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
561# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
562# endif
563# endif
564# if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
565# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
566# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
567# endif
568# endif
569#endif
570
571#define SER1_PB_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PB_BIT+CONFIG_ETRAX_SER1_RI_ON_PB_BIT+CONFIG_ETRAX_SER1_DSR_ON_PB_BIT+CONFIG_ETRAX_SER1_CD_ON_PB_BIT)
572
573#if SER1_PB_BITSUM != -4
574# if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
575# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
576# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
577# endif
578# endif
579# if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
580# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
581# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
582# endif
583# endif
584# if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
585# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
586# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
587# endif
588# endif
589# if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
590# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
591# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
592# endif
593# endif
594#endif
595
596#endif /* PORT1 */
597
598#ifdef CONFIG_ETRAX_SERIAL_PORT2
599
600#define SER2_PA_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PA_BIT+CONFIG_ETRAX_SER2_RI_ON_PA_BIT+CONFIG_ETRAX_SER2_DSR_ON_PA_BIT+CONFIG_ETRAX_SER2_CD_ON_PA_BIT)
601
602#if SER2_PA_BITSUM != -4
603# if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
604# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
605# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
606# endif
607# endif
608# if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
609# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
610# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
611# endif
612# endif
613# if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
614# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
615# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
616# endif
617# endif
618# if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
619# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
620# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
621# endif
622# endif
623#endif
624
625#define SER2_PB_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PB_BIT+CONFIG_ETRAX_SER2_RI_ON_PB_BIT+CONFIG_ETRAX_SER2_DSR_ON_PB_BIT+CONFIG_ETRAX_SER2_CD_ON_PB_BIT)
626
627#if SER2_PB_BITSUM != -4
628# if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
629# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
630# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
631# endif
632# endif
633# if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
634# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
635# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
636# endif
637# endif
638# if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
639# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
640# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
641# endif
642# endif
643# if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
644# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
645# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
646# endif
647# endif
648#endif
649
650#endif /* PORT2 */
651
652#ifdef CONFIG_ETRAX_SERIAL_PORT3
653
654#define SER3_PA_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PA_BIT+CONFIG_ETRAX_SER3_RI_ON_PA_BIT+CONFIG_ETRAX_SER3_DSR_ON_PA_BIT+CONFIG_ETRAX_SER3_CD_ON_PA_BIT)
655
656#if SER3_PA_BITSUM != -4
657# if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
658# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
659# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
660# endif
661# endif
662# if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
663# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
664# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
665# endif
666# endif
667# if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
668# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
669# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
670# endif
671# endif
672# if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
673# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
674# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
675# endif
676# endif
677#endif
678
679#define SER3_PB_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PB_BIT+CONFIG_ETRAX_SER3_RI_ON_PB_BIT+CONFIG_ETRAX_SER3_DSR_ON_PB_BIT+CONFIG_ETRAX_SER3_CD_ON_PB_BIT)
680
681#if SER3_PB_BITSUM != -4
682# if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
683# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
684# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
685# endif
686# endif
687# if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
688# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
689# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
690# endif
691# endif
692# if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
693# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
694# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
695# endif
696# endif
697# if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
698# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
699# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
700# endif
701# endif
702#endif
703
704#endif /* PORT3 */
705
706
707#if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
708 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
709 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
710 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
Valentin Rothberg74a76082015-04-11 15:56:03 +0200711#define ETRAX_SERX_DTR_RI_DSR_CD_MIXED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712#endif
713
Valentin Rothberg74a76082015-04-11 15:56:03 +0200714#ifdef ETRAX_SERX_DTR_RI_DSR_CD_MIXED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/* The pins can be mixed on PA and PB */
716#define CONTROL_PINS_PORT_NOT_USED(line) \
717 &dummy_ser[line], &dummy_ser[line], \
718 &dummy_ser[line], &dummy_ser[line], \
719 &dummy_ser[line], &dummy_ser[line], \
720 &dummy_ser[line], &dummy_ser[line], \
721 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
722
723
724struct control_pins
725{
726 volatile unsigned char *dtr_port;
727 unsigned char *dtr_shadow;
728 volatile unsigned char *ri_port;
729 unsigned char *ri_shadow;
730 volatile unsigned char *dsr_port;
731 unsigned char *dsr_shadow;
732 volatile unsigned char *cd_port;
733 unsigned char *cd_shadow;
734
735 unsigned char dtr_mask;
736 unsigned char ri_mask;
737 unsigned char dsr_mask;
738 unsigned char cd_mask;
739};
740
741static const struct control_pins e100_modem_pins[NR_PORTS] =
742{
743 /* Ser 0 */
744 {
745#ifdef CONFIG_ETRAX_SERIAL_PORT0
746 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
747 E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
748 E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
749 E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
750 E100_STRUCT_MASK(0,DTR),
751 E100_STRUCT_MASK(0,RI),
752 E100_STRUCT_MASK(0,DSR),
753 E100_STRUCT_MASK(0,CD)
754#else
755 CONTROL_PINS_PORT_NOT_USED(0)
756#endif
757 },
758
759 /* Ser 1 */
760 {
761#ifdef CONFIG_ETRAX_SERIAL_PORT1
762 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
763 E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
764 E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
765 E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
766 E100_STRUCT_MASK(1,DTR),
767 E100_STRUCT_MASK(1,RI),
768 E100_STRUCT_MASK(1,DSR),
769 E100_STRUCT_MASK(1,CD)
770#else
771 CONTROL_PINS_PORT_NOT_USED(1)
772#endif
773 },
774
775 /* Ser 2 */
776 {
777#ifdef CONFIG_ETRAX_SERIAL_PORT2
778 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
779 E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
780 E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
781 E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
782 E100_STRUCT_MASK(2,DTR),
783 E100_STRUCT_MASK(2,RI),
784 E100_STRUCT_MASK(2,DSR),
785 E100_STRUCT_MASK(2,CD)
786#else
787 CONTROL_PINS_PORT_NOT_USED(2)
788#endif
789 },
790
791 /* Ser 3 */
792 {
793#ifdef CONFIG_ETRAX_SERIAL_PORT3
794 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
795 E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
796 E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
797 E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
798 E100_STRUCT_MASK(3,DTR),
799 E100_STRUCT_MASK(3,RI),
800 E100_STRUCT_MASK(3,DSR),
801 E100_STRUCT_MASK(3,CD)
802#else
803 CONTROL_PINS_PORT_NOT_USED(3)
804#endif
805 }
806};
Valentin Rothberg74a76082015-04-11 15:56:03 +0200807#else /* ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/* All pins are on either PA or PB for each serial port */
810#define CONTROL_PINS_PORT_NOT_USED(line) \
811 &dummy_ser[line], &dummy_ser[line], \
812 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
813
814
815struct control_pins
816{
817 volatile unsigned char *port;
818 unsigned char *shadow;
819
820 unsigned char dtr_mask;
821 unsigned char ri_mask;
822 unsigned char dsr_mask;
823 unsigned char cd_mask;
824};
825
826#define dtr_port port
827#define dtr_shadow shadow
828#define ri_port port
829#define ri_shadow shadow
830#define dsr_port port
831#define dsr_shadow shadow
832#define cd_port port
833#define cd_shadow shadow
834
835static const struct control_pins e100_modem_pins[NR_PORTS] =
836{
837 /* Ser 0 */
838 {
839#ifdef CONFIG_ETRAX_SERIAL_PORT0
840 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
841 E100_STRUCT_MASK(0,DTR),
842 E100_STRUCT_MASK(0,RI),
843 E100_STRUCT_MASK(0,DSR),
844 E100_STRUCT_MASK(0,CD)
845#else
846 CONTROL_PINS_PORT_NOT_USED(0)
847#endif
848 },
849
850 /* Ser 1 */
851 {
852#ifdef CONFIG_ETRAX_SERIAL_PORT1
853 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
854 E100_STRUCT_MASK(1,DTR),
855 E100_STRUCT_MASK(1,RI),
856 E100_STRUCT_MASK(1,DSR),
857 E100_STRUCT_MASK(1,CD)
858#else
859 CONTROL_PINS_PORT_NOT_USED(1)
860#endif
861 },
862
863 /* Ser 2 */
864 {
865#ifdef CONFIG_ETRAX_SERIAL_PORT2
866 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
867 E100_STRUCT_MASK(2,DTR),
868 E100_STRUCT_MASK(2,RI),
869 E100_STRUCT_MASK(2,DSR),
870 E100_STRUCT_MASK(2,CD)
871#else
872 CONTROL_PINS_PORT_NOT_USED(2)
873#endif
874 },
875
876 /* Ser 3 */
877 {
878#ifdef CONFIG_ETRAX_SERIAL_PORT3
879 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
880 E100_STRUCT_MASK(3,DTR),
881 E100_STRUCT_MASK(3,RI),
882 E100_STRUCT_MASK(3,DSR),
883 E100_STRUCT_MASK(3,CD)
884#else
885 CONTROL_PINS_PORT_NOT_USED(3)
886#endif
887 }
888};
Valentin Rothberg74a76082015-04-11 15:56:03 +0200889#endif /* !ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891#define E100_RTS_MASK 0x20
892#define E100_CTS_MASK 0x40
893
894/* All serial port signals are active low:
895 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
896 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
897 *
898 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
899 */
900
901/* Output */
902#define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
903/* Input */
Alan Coxd7283352008-08-04 17:21:18 +0100904#define E100_CTS_GET(info) ((info)->ioport[REG_STATUS] & E100_CTS_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906/* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
907/* Is an output */
908#define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
909
910/* Normally inputs */
911#define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
912#define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
913
914/* Input */
915#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/* Calculate the chartime depending on baudrate, numbor of bits etc. */
918static void update_char_time(struct e100_serial * info)
919{
Alan Coxadc8d742012-07-14 15:31:47 +0100920 tcflag_t cflags = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int bits;
922
923 /* calc. number of bits / data byte */
924 /* databits + startbit and 1 stopbit */
925 if ((cflags & CSIZE) == CS7)
926 bits = 9;
927 else
928 bits = 10;
929
930 if (cflags & CSTOPB) /* 2 stopbits ? */
931 bits++;
932
933 if (cflags & PARENB) /* parity bit ? */
934 bits++;
935
936 /* calc timeout */
937 info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
938 info->flush_time_usec = 4*info->char_time_usec;
939 if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
940 info->flush_time_usec = MIN_FLUSH_TIME_USEC;
941
942}
943
944/*
945 * This function maps from the Bxxxx defines in asm/termbits.h into real
946 * baud rates.
947 */
948
949static int
950cflag_to_baud(unsigned int cflag)
951{
952 static int baud_table[] = {
953 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
954 4800, 9600, 19200, 38400 };
955
956 static int ext_baud_table[] = {
957 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
958 0, 0, 0, 0, 0, 0, 0, 0 };
959
960 if (cflag & CBAUDEX)
961 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
962 else
963 return baud_table[cflag & CBAUD];
964}
965
966/* and this maps to an etrax100 hardware baud constant */
967
968static unsigned char
969cflag_to_etrax_baud(unsigned int cflag)
970{
971 char retval;
972
973 static char baud_table[] = {
974 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
975
976 static char ext_baud_table[] = {
977 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
978
979 if (cflag & CBAUDEX)
980 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
981 else
982 retval = baud_table[cflag & CBAUD];
983
984 if (retval < 0) {
985 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
986 retval = 5; /* choose default 9600 instead */
987 }
988
989 return retval | (retval << 4); /* choose same for both TX and RX */
990}
991
992
993/* Various static support functions */
994
995/* Functions to set or clear DTR/RTS on the requested line */
996/* It is complicated by the fact that RTS is a serial port register, while
997 * DTR might not be implemented in the HW at all, and if it is, it can be on
998 * any general port.
999 */
1000
1001
1002static inline void
1003e100_dtr(struct e100_serial *info, int set)
1004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1006
1007#ifdef SERIAL_DEBUG_IO
1008 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1009 printk("ser%i shadow before 0x%02X get: %i\n",
1010 info->line, *e100_modem_pins[info->line].dtr_shadow,
1011 E100_DTR_GET(info));
1012#endif
1013 /* DTR is active low */
1014 {
1015 unsigned long flags;
1016
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001017 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1019 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
1020 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001021 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
1024#ifdef SERIAL_DEBUG_IO
1025 printk("ser%i shadow after 0x%02X get: %i\n",
1026 info->line, *e100_modem_pins[info->line].dtr_shadow,
1027 E100_DTR_GET(info));
1028#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1032 * 0=0V , 1=3.3V
1033 */
1034static inline void
1035e100_rts(struct e100_serial *info, int set)
1036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001038 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 info->rx_ctrl &= ~E100_RTS_MASK;
1040 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
Alan Coxd7283352008-08-04 17:21:18 +01001041 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001042 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#ifdef SERIAL_DEBUG_IO
1044 printk("ser%i rts %i\n", info->line, set);
1045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048
1049/* If this behaves as a modem, RI and CD is an output */
1050static inline void
1051e100_ri_out(struct e100_serial *info, int set)
1052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* RI is active low */
1054 {
1055 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1056 unsigned long flags;
1057
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001058 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1060 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
1061 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001062 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065static inline void
1066e100_cd_out(struct e100_serial *info, int set)
1067{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* CD is active low */
1069 {
1070 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1071 unsigned long flags;
1072
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001073 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1075 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
1076 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001077 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static inline void
1082e100_disable_rx(struct e100_serial *info)
1083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* disable the receiver */
Alan Coxd7283352008-08-04 17:21:18 +01001085 info->ioport[REG_REC_CTRL] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static inline void
1090e100_enable_rx(struct e100_serial *info)
1091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* enable the receiver */
Alan Coxd7283352008-08-04 17:21:18 +01001093 info->ioport[REG_REC_CTRL] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097/* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1098
1099static inline void
1100e100_disable_rxdma_irq(struct e100_serial *info)
1101{
1102#ifdef SERIAL_DEBUG_INTR
1103 printk("rxdma_irq(%d): 0\n",info->line);
1104#endif
1105 DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
1106 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1107}
1108
1109static inline void
1110e100_enable_rxdma_irq(struct e100_serial *info)
1111{
1112#ifdef SERIAL_DEBUG_INTR
1113 printk("rxdma_irq(%d): 1\n",info->line);
1114#endif
1115 DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
1116 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1117}
1118
1119/* the tx DMA uses only dma_descr interrupt */
1120
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001121static void e100_disable_txdma_irq(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123#ifdef SERIAL_DEBUG_INTR
1124 printk("txdma_irq(%d): 0\n",info->line);
1125#endif
1126 DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
1127 *R_IRQ_MASK2_CLR = info->irq;
1128}
1129
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001130static void e100_enable_txdma_irq(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132#ifdef SERIAL_DEBUG_INTR
1133 printk("txdma_irq(%d): 1\n",info->line);
1134#endif
1135 DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
1136 *R_IRQ_MASK2_SET = info->irq;
1137}
1138
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001139static void e100_disable_txdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 unsigned long flags;
1142
1143 /* Disable output DMA channel for the serial port in question
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001144 * ( set to something other than serialX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001146 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
1148 if (info->line == 0) {
1149 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
1150 IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
1151 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1152 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1153 }
1154 } else if (info->line == 1) {
1155 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
1156 IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
1157 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1158 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1159 }
1160 } else if (info->line == 2) {
1161 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
1162 IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
1163 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1164 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1165 }
1166 } else if (info->line == 3) {
1167 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
1168 IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
1169 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1170 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1171 }
1172 }
1173 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001174 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001178static void e100_enable_txdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 unsigned long flags;
1181
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001182 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
1184 /* Enable output DMA channel for the serial port in question */
1185 if (info->line == 0) {
1186 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1187 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1188 } else if (info->line == 1) {
1189 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1190 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1191 } else if (info->line == 2) {
1192 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1193 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1194 } else if (info->line == 3) {
1195 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1196 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1197 }
1198 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001199 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001202static void e100_disable_rxdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 unsigned long flags;
1205
1206 /* Disable input DMA channel for the serial port in question
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001207 * ( set to something other than serialX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001209 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (info->line == 0) {
1211 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
1212 IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
1213 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1214 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
1215 }
1216 } else if (info->line == 1) {
1217 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
1218 IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
1219 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1220 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
1221 }
1222 } else if (info->line == 2) {
1223 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
1224 IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
1225 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1226 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
1227 }
1228 } else if (info->line == 3) {
1229 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
1230 IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
1231 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1232 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
1233 }
1234 }
1235 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001236 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001240static void e100_enable_rxdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 unsigned long flags;
1243
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001244 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* Enable input DMA channel for the serial port in question */
1246 if (info->line == 0) {
1247 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1248 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
1249 } else if (info->line == 1) {
1250 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1251 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
1252 } else if (info->line == 2) {
1253 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1254 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
1255 } else if (info->line == 3) {
1256 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1257 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
1258 }
1259 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001260 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263#ifdef SERIAL_HANDLE_EARLY_ERRORS
1264/* in order to detect and fix errors on the first byte
1265 we have to use the serial interrupts as well. */
1266
1267static inline void
1268e100_disable_serial_data_irq(struct e100_serial *info)
1269{
1270#ifdef SERIAL_DEBUG_INTR
1271 printk("ser_irq(%d): 0\n",info->line);
1272#endif
1273 DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
1274 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1275}
1276
1277static inline void
1278e100_enable_serial_data_irq(struct e100_serial *info)
1279{
1280#ifdef SERIAL_DEBUG_INTR
1281 printk("ser_irq(%d): 1\n",info->line);
1282 printk("**** %d = %d\n",
1283 (8+2*info->line),
1284 (1U << (8+2*info->line)));
1285#endif
1286 DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
1287 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1288}
1289#endif
1290
1291static inline void
1292e100_disable_serial_tx_ready_irq(struct e100_serial *info)
1293{
1294#ifdef SERIAL_DEBUG_INTR
1295 printk("ser_tx_irq(%d): 0\n",info->line);
1296#endif
1297 DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
1298 *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
1299}
1300
1301static inline void
1302e100_enable_serial_tx_ready_irq(struct e100_serial *info)
1303{
1304#ifdef SERIAL_DEBUG_INTR
1305 printk("ser_tx_irq(%d): 1\n",info->line);
1306 printk("**** %d = %d\n",
1307 (8+1+2*info->line),
1308 (1U << (8+1+2*info->line)));
1309#endif
1310 DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
1311 *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
1312}
1313
1314static inline void e100_enable_rx_irq(struct e100_serial *info)
1315{
1316 if (info->uses_dma_in)
1317 e100_enable_rxdma_irq(info);
1318 else
1319 e100_enable_serial_data_irq(info);
1320}
1321static inline void e100_disable_rx_irq(struct e100_serial *info)
1322{
1323 if (info->uses_dma_in)
1324 e100_disable_rxdma_irq(info);
1325 else
1326 e100_disable_serial_data_irq(info);
1327}
1328
1329#if defined(CONFIG_ETRAX_RS485)
1330/* Enable RS-485 mode on selected port. This is UGLY. */
1331static int
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001332e100_enable_rs485(struct tty_struct *tty, struct serial_rs485 *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1335
1336#if defined(CONFIG_ETRAX_RS485_ON_PA)
1337 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02001340 info->rs485 = *r;
1341
1342 /* Maximum delay before RTS equal to 1000 */
1343 if (info->rs485.delay_rts_before_send >= 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 info->rs485.delay_rts_before_send = 1000;
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/* printk("rts: on send = %i, after = %i, enabled = %i",
1347 info->rs485.rts_on_send,
1348 info->rs485.rts_after_sent,
1349 info->rs485.enabled
1350 );
1351*/
1352 return 0;
1353}
1354
1355static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001356e100_write_rs485(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 const unsigned char *buf, int count)
1358{
1359 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001360 int old_value = (info->rs485.flags) & SER_RS485_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* rs485 is always implicitly enabled if we're using the ioctl()
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001363 * but it doesn't have to be set in the serial_rs485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * (to be backward compatible with old apps)
1365 * So we store, set and restore it.
1366 */
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001367 info->rs485.flags |= SER_RS485_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /* rs_write now deals with RS485 if enabled */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001369 count = rs_write(tty, buf, count);
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001370 if (!old_value)
1371 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return count;
1373}
1374
1375#ifdef CONFIG_ETRAX_FAST_TIMER
1376/* Timer function to toggle RTS when using FAST_TIMER */
1377static void rs485_toggle_rts_timer_function(unsigned long data)
1378{
1379 struct e100_serial *info = (struct e100_serial *)data;
1380
1381 fast_timers_rs485[info->line].function = NULL;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001382 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1384 e100_enable_rx(info);
1385 e100_enable_rx_irq(info);
1386#endif
1387}
1388#endif
1389#endif /* CONFIG_ETRAX_RS485 */
1390
1391/*
1392 * ------------------------------------------------------------
1393 * rs_stop() and rs_start()
1394 *
1395 * This routines are called before setting or resetting tty->stopped.
1396 * They enable or disable transmitter using the XOFF registers, as necessary.
1397 * ------------------------------------------------------------
1398 */
1399
1400static void
1401rs_stop(struct tty_struct *tty)
1402{
1403 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1404 if (info) {
1405 unsigned long flags;
1406 unsigned long xoff;
1407
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001408 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
1410 CIRC_CNT(info->xmit.head,
1411 info->xmit.tail,SERIAL_XMIT_SIZE)));
1412
Takashi Iwaia88487c2008-07-16 21:54:42 +01001413 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char,
1414 STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
Alan Coxadc8d742012-07-14 15:31:47 +01001416 if (tty->termios.c_iflag & IXON ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1418 }
1419
Alan Coxd7283352008-08-04 17:21:18 +01001420 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001421 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
1425static void
1426rs_start(struct tty_struct *tty)
1427{
1428 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1429 if (info) {
1430 unsigned long flags;
1431 unsigned long xoff;
1432
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001433 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
1435 CIRC_CNT(info->xmit.head,
1436 info->xmit.tail,SERIAL_XMIT_SIZE)));
1437 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1438 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
Alan Coxadc8d742012-07-14 15:31:47 +01001439 if (tty->termios.c_iflag & IXON ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1441 }
1442
Alan Coxd7283352008-08-04 17:21:18 +01001443 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!info->uses_dma_out &&
1445 info->xmit.head != info->xmit.tail && info->xmit.buf)
1446 e100_enable_serial_tx_ready_irq(info);
1447
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001448 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450}
1451
1452/*
1453 * ----------------------------------------------------------------------
1454 *
1455 * Here starts the interrupt handling routines. All of the following
1456 * subroutines are declared as inline and are folded into
1457 * rs_interrupt(). They were separated out for readability's sake.
1458 *
1459 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1460 * runs with interrupts turned off. People who may want to modify
1461 * rs_interrupt() should try to keep the interrupt handler as fast as
1462 * possible. After you are done making modifications, it is not a bad
1463 * idea to do:
1464 *
1465 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1466 *
1467 * and look at the resulting assemble code in serial.s.
1468 *
1469 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1470 * -----------------------------------------------------------------------
1471 */
1472
1473/*
1474 * This routine is used by the interrupt handler to schedule
1475 * processing in the software interrupt portion of the driver.
1476 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001477static void rs_sched_event(struct e100_serial *info, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 if (info->event & (1 << event))
1480 return;
1481 info->event |= 1 << event;
1482 schedule_work(&info->work);
1483}
1484
1485/* The output DMA channel is free - use it to send as many chars as possible
1486 * NOTES:
1487 * We don't pay attention to info->x_char, which means if the TTY wants to
1488 * use XON/XOFF it will set info->x_char but we won't send any X char!
1489 *
1490 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1491 * buffer containing the X char, and skip updating xmit. We'd also have to
1492 * check if the last sent char was the X char when we enter this function
1493 * the next time, to avoid updating xmit with the sent X value.
1494 */
1495
1496static void
1497transmit_chars_dma(struct e100_serial *info)
1498{
1499 unsigned int c, sentl;
1500 struct etrax_dma_descr *descr;
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1503 *info->oclrintradr =
1504 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1505 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1506
1507#ifdef SERIAL_DEBUG_INTR
1508 if (info->line == SERIAL_DEBUG_LINE)
1509 printk("tc\n");
1510#endif
1511 if (!info->tr_running) {
1512 /* weirdo... we shouldn't get here! */
1513 printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
1514 return;
1515 }
1516
1517 descr = &info->tr_descr;
1518
1519 /* first get the amount of bytes sent during the last DMA transfer,
1520 and update xmit accordingly */
1521
1522 /* if the stop bit was not set, all data has been sent */
1523 if (!(descr->status & d_stop)) {
1524 sentl = descr->sw_len;
1525 } else
1526 /* otherwise we find the amount of data sent here */
1527 sentl = descr->hw_len;
1528
1529 DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
1530
1531 /* update stats */
1532 info->icount.tx += sentl;
1533
1534 /* update xmit buffer */
1535 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1536
1537 /* if there is only a few chars left in the buf, wake up the blocked
1538 write if any */
1539 if (CIRC_CNT(info->xmit.head,
1540 info->xmit.tail,
1541 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1542 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1543
1544 /* find out the largest amount of consecutive bytes we want to send now */
1545
1546 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1547
1548 /* Don't send all in one DMA transfer - divide it so we wake up
1549 * application before all is sent
1550 */
1551
1552 if (c >= 4*WAKEUP_CHARS)
1553 c = c/2;
1554
1555 if (c <= 0) {
1556 /* our job here is done, don't schedule any new DMA transfer */
1557 info->tr_running = 0;
1558
1559#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001560 if (info->rs485.flags & SER_RS485_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Set a short timer to toggle RTS */
1562 start_one_shot_timer(&fast_timers_rs485[info->line],
1563 rs485_toggle_rts_timer_function,
1564 (unsigned long)info,
1565 info->char_time_usec*2,
1566 "RS-485");
1567 }
1568#endif /* RS485 */
1569 return;
1570 }
1571
1572 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1573 /* set up the descriptor correctly for output */
1574 DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
1575 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1576 descr->sw_len = c;
1577 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1578 descr->status = 0;
1579
1580 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1581 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1582
1583 /* DMA is now running (hopefully) */
1584} /* transmit_chars_dma */
1585
1586static void
1587start_transmit(struct e100_serial *info)
1588{
1589#if 0
1590 if (info->line == SERIAL_DEBUG_LINE)
1591 printk("x\n");
1592#endif
1593
1594 info->tr_descr.sw_len = 0;
1595 info->tr_descr.hw_len = 0;
1596 info->tr_descr.status = 0;
1597 info->tr_running = 1;
1598 if (info->uses_dma_out)
1599 transmit_chars_dma(info);
1600 else
1601 e100_enable_serial_tx_ready_irq(info);
1602} /* start_transmit */
1603
1604#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1605static int serial_fast_timer_started = 0;
1606static int serial_fast_timer_expired = 0;
1607static void flush_timeout_function(unsigned long data);
1608#define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
1609 unsigned long timer_flags; \
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001610 local_irq_save(timer_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (fast_timers[info->line].function == NULL) { \
1612 serial_fast_timer_started++; \
1613 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1614 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1615 start_one_shot_timer(&fast_timers[info->line], \
1616 flush_timeout_function, \
1617 (unsigned long)info, \
1618 (usec), \
1619 string); \
1620 } \
1621 else { \
1622 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1623 } \
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001624 local_irq_restore(timer_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626#define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
1627
1628#else
1629#define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
1630#define START_FLUSH_FAST_TIMER(info, string)
1631#endif
1632
1633static struct etrax_recv_buffer *
1634alloc_recv_buffer(unsigned int size)
1635{
1636 struct etrax_recv_buffer *buffer;
1637
Greg Kroah-Hartman9ff46042015-04-30 11:22:07 +02001638 buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC);
1639 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return NULL;
1641
1642 buffer->next = NULL;
1643 buffer->length = 0;
1644 buffer->error = TTY_NORMAL;
1645
1646 return buffer;
1647}
1648
1649static void
1650append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1651{
1652 unsigned long flags;
1653
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001654 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (!info->first_recv_buffer)
1657 info->first_recv_buffer = buffer;
1658 else
1659 info->last_recv_buffer->next = buffer;
1660
1661 info->last_recv_buffer = buffer;
1662
1663 info->recv_cnt += buffer->length;
1664 if (info->recv_cnt > info->max_recv_cnt)
1665 info->max_recv_cnt = info->recv_cnt;
1666
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001667 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670static int
1671add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1672{
1673 struct etrax_recv_buffer *buffer;
1674 if (info->uses_dma_in) {
Greg Kroah-Hartman9ff46042015-04-30 11:22:07 +02001675 buffer = alloc_recv_buffer(4);
1676 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678
1679 buffer->length = 1;
1680 buffer->error = flag;
1681 buffer->buffer[0] = data;
1682
1683 append_recv_buffer(info, buffer);
1684
1685 info->icount.rx++;
1686 } else {
Jiri Slaby92a19f92013-01-03 15:53:03 +01001687 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 info->icount.rx++;
1689 }
1690
1691 return 1;
1692}
1693
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001694static unsigned int handle_descr_data(struct e100_serial *info,
1695 struct etrax_dma_descr *descr,
1696 unsigned int recvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1699
1700 if (info->recv_cnt + recvl > 65536) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08001701 printk(KERN_WARNING
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001702 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704 }
1705
1706 buffer->length = recvl;
1707
1708 if (info->errorcode == ERRCODE_SET_BREAK)
1709 buffer->error = TTY_BREAK;
1710 info->errorcode = 0;
1711
1712 append_recv_buffer(info, buffer);
1713
Greg Kroah-Hartman9ff46042015-04-30 11:22:07 +02001714 buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
1715 if (!buffer)
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001716 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 descr->buf = virt_to_phys(buffer->buffer);
1719
1720 return recvl;
1721}
1722
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001723static unsigned int handle_all_descr_data(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct etrax_dma_descr *descr;
1726 unsigned int recvl;
1727 unsigned int ret = 0;
1728
1729 while (1)
1730 {
1731 descr = &info->rec_descr[info->cur_rec_descr];
1732
1733 if (descr == phys_to_virt(*info->idescradr))
1734 break;
1735
1736 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1737 info->cur_rec_descr = 0;
1738
1739 /* find out how many bytes were read */
1740
1741 /* if the eop bit was not set, all data has been received */
1742 if (!(descr->status & d_eop)) {
1743 recvl = descr->sw_len;
1744 } else {
1745 /* otherwise we find the amount of data received here */
1746 recvl = descr->hw_len;
1747 }
1748
1749 /* Reset the status information */
1750 descr->status = 0;
1751
1752 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
Takashi Iwaia88487c2008-07-16 21:54:42 +01001753 if (info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 unsigned char *buf = phys_to_virt(descr->buf);
1755 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
1756 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
1757 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
1758 }
1759 );
1760
1761 /* update stats */
1762 info->icount.rx += recvl;
1763
1764 ret += handle_descr_data(info, descr, recvl);
1765 }
1766
1767 return ret;
1768}
1769
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001770static void receive_chars_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 struct tty_struct *tty;
1773 unsigned char rstat;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1776 *info->iclrintradr =
1777 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1778 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1779
Takashi Iwaia88487c2008-07-16 21:54:42 +01001780 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (!tty) /* Something wrong... */
1782 return;
1783
1784#ifdef SERIAL_HANDLE_EARLY_ERRORS
1785 if (info->uses_dma_in)
1786 e100_enable_serial_data_irq(info);
1787#endif
1788
1789 if (info->errorcode == ERRCODE_INSERT_BREAK)
1790 add_char_and_flag(info, '\0', TTY_BREAK);
1791
1792 handle_all_descr_data(info);
1793
1794 /* Read the status register to detect errors */
Alan Coxd7283352008-08-04 17:21:18 +01001795 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
1797 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
1798 }
1799
1800 if (rstat & SER_ERROR_MASK) {
1801 /* If we got an error, we must reset it by reading the
1802 * data_in field
1803 */
Alan Coxd7283352008-08-04 17:21:18 +01001804 unsigned char data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1807 ((rstat & SER_ERROR_MASK) << 8) | data);
1808
1809 if (rstat & SER_PAR_ERR_MASK)
1810 add_char_and_flag(info, data, TTY_PARITY);
1811 else if (rstat & SER_OVERRUN_MASK)
1812 add_char_and_flag(info, data, TTY_OVERRUN);
1813 else if (rstat & SER_FRAMING_ERR_MASK)
1814 add_char_and_flag(info, data, TTY_FRAME);
1815 }
1816
1817 START_FLUSH_FAST_TIMER(info, "receive_chars");
1818
1819 /* Restart the receiving DMA */
1820 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1821}
1822
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001823static int start_recv_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 struct etrax_dma_descr *descr = info->rec_descr;
1826 struct etrax_recv_buffer *buffer;
1827 int i;
1828
1829 /* Set up the receiving descriptors */
1830 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
Greg Kroah-Hartman9ff46042015-04-30 11:22:07 +02001831 buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
1832 if (!buffer)
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001833 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 descr[i].ctrl = d_int;
1836 descr[i].buf = virt_to_phys(buffer->buffer);
1837 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1838 descr[i].hw_len = 0;
1839 descr[i].status = 0;
1840 descr[i].next = virt_to_phys(&descr[i+1]);
1841 }
1842
1843 /* Link the last descriptor to the first */
1844 descr[i-1].next = virt_to_phys(&descr[0]);
1845
1846 /* Start with the first descriptor in the list */
1847 info->cur_rec_descr = 0;
1848
1849 /* Start the DMA */
1850 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1851 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1852
1853 /* Input DMA should be running now */
1854 return 1;
1855}
1856
1857static void
1858start_receive(struct e100_serial *info)
1859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (info->uses_dma_in) {
1861 /* reset the input dma channel to be sure it works */
1862
1863 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1864 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1865 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1866
1867 start_recv_dma(info);
1868 }
1869}
1870
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/* the bits in the MASK2 register are laid out like this:
1873 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1874 where I is the input channel and O is the output channel for the port.
1875 info->irq is the bit number for the DMAO_DESCR so to check the others we
1876 shift info->irq to the left.
1877*/
1878
1879/* dma output channel interrupt handler
1880 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1881 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1882*/
1883
1884static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001885tr_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 struct e100_serial *info;
1888 unsigned long ireg;
1889 int i;
1890 int handled = 0;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* find out the line that caused this irq and get it from rs_table */
1893
1894 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1895
1896 for (i = 0; i < NR_PORTS; i++) {
1897 info = rs_table + i;
1898 if (!info->enabled || !info->uses_dma_out)
1899 continue;
1900 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
1901 if (ireg & info->irq) {
1902 handled = 1;
1903 /* we can send a new dma bunch. make it so. */
1904 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
1905 /* Read jiffies_usec first,
1906 * we want this time to be as late as possible
1907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 info->last_tx_active_usec = GET_JIFFIES_USEC();
1909 info->last_tx_active = jiffies;
1910 transmit_chars_dma(info);
1911 }
1912
1913 /* FIXME: here we should really check for a change in the
1914 status lines and if so call status_handle(info) */
1915 }
1916 return IRQ_RETVAL(handled);
1917} /* tr_interrupt */
1918
1919/* dma input channel interrupt handler */
1920
1921static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001922rec_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 struct e100_serial *info;
1925 unsigned long ireg;
1926 int i;
1927 int handled = 0;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* find out the line that caused this irq and get it from rs_table */
1930
1931 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1932
1933 for (i = 0; i < NR_PORTS; i++) {
1934 info = rs_table + i;
1935 if (!info->enabled || !info->uses_dma_in)
1936 continue;
1937 /* check for both dma_eop and dma_descr for the input dma channel */
1938 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
1939 handled = 1;
1940 /* we have received something */
1941 receive_chars_dma(info);
1942 }
1943
1944 /* FIXME: here we should really check for a change in the
1945 status lines and if so call status_handle(info) */
1946 }
1947 return IRQ_RETVAL(handled);
1948} /* rec_interrupt */
1949
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001950static int force_eop_if_needed(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 /* We check data_avail bit to determine if data has
1953 * arrived since last time
1954 */
Alan Coxd7283352008-08-04 17:21:18 +01001955 unsigned char rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 /* error or datavail? */
1958 if (rstat & SER_ERROR_MASK) {
1959 /* Some error has occurred. If there has been valid data, an
1960 * EOP interrupt will be made automatically. If no data, the
1961 * normal ser_interrupt should be enabled and handle it.
1962 * So do nothing!
1963 */
1964 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
1965 rstat | (info->line << 8));
1966 return 0;
1967 }
1968
1969 if (rstat & SER_DATA_AVAIL_MASK) {
1970 /* Ok data, no error, count it */
1971 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
1972 rstat | (info->line << 8)));
1973 /* Read data to clear status flags */
Alan Coxd7283352008-08-04 17:21:18 +01001974 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 info->forced_eop = 0;
1977 START_FLUSH_FAST_TIMER(info, "magic");
1978 return 0;
1979 }
1980
1981 /* hit the timeout, force an EOP for the input
1982 * dma channel if we haven't already
1983 */
1984 if (!info->forced_eop) {
1985 info->forced_eop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
1987 FORCE_EOP(info);
1988 }
1989
1990 return 1;
1991}
1992
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001993static void flush_to_flip_buffer(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct etrax_recv_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001998 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Alan Cox2090ab02007-10-16 01:26:38 -07002000 while ((buffer = info->first_recv_buffer) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 unsigned int count = buffer->length;
2002
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002003 tty_insert_flip_string(&info->port, buffer->buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 info->recv_cnt -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if (count == buffer->length) {
2007 info->first_recv_buffer = buffer->next;
2008 kfree(buffer);
2009 } else {
2010 buffer->length -= count;
2011 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2012 buffer->error = TTY_NORMAL;
2013 }
2014 }
2015
2016 if (!info->first_recv_buffer)
2017 info->last_recv_buffer = NULL;
2018
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002019 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002021 /* This includes a check for low-latency */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002022 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002025static void check_flush_timeout(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 /* Flip what we've got (if we can) */
2028 flush_to_flip_buffer(info);
2029
2030 /* We might need to flip later, but not to fast
2031 * since the system is busy processing input... */
2032 if (info->first_recv_buffer)
2033 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2034
2035 /* Force eop last, since data might have come while we're processing
2036 * and if we started the slow timer above, we won't start a fast
2037 * below.
2038 */
2039 force_eop_if_needed(info);
2040}
2041
2042#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2043static void flush_timeout_function(unsigned long data)
2044{
2045 struct e100_serial *info = (struct e100_serial *)data;
2046
2047 fast_timers[info->line].function = NULL;
2048 serial_fast_timer_expired++;
Masanari Iida8faaaea2014-01-07 21:58:06 +09002049 TIMERD(DEBUG_LOG(info->line, "flush_timeout %i ", info->line));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2051 check_flush_timeout(info);
2052}
2053
2054#else
2055
2056/* dma fifo/buffer timeout handler
2057 forces an end-of-packet for the dma input channel if no chars
2058 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2059*/
2060
2061static struct timer_list flush_timer;
2062
2063static void
2064timed_flush_handler(unsigned long ptr)
2065{
2066 struct e100_serial *info;
2067 int i;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 for (i = 0; i < NR_PORTS; i++) {
2070 info = rs_table + i;
2071 if (info->uses_dma_in)
2072 check_flush_timeout(info);
2073 }
2074
2075 /* restart flush timer */
2076 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2077}
2078#endif
2079
2080#ifdef SERIAL_HANDLE_EARLY_ERRORS
2081
2082/* If there is an error (ie break) when the DMA is running and
2083 * there are no bytes in the fifo the DMA is stopped and we get no
2084 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2085 * transfer, and if it is without error we can turn the serial
2086 * interrupts off.
2087 */
2088
2089/*
2090BREAK handling on ETRAX 100:
2091ETRAX will generate interrupt although there is no stop bit between the
2092characters.
2093
2094Depending on how long the break sequence is, the end of the breaksequence
2095will look differently:
2096| indicates start/end of a character.
2097
2098B= Break character (0x00) with framing error.
2099E= Error byte with parity error received after B characters.
2100F= "Faked" valid byte received immediately after B characters.
2101V= Valid byte
2102
21031.
2104 B BL ___________________________ V
2105.._|__________|__________| |valid data |
2106
2107Multiple frame errors with data == 0x00 (B),
2108the timing matches up "perfectly" so no extra ending char is detected.
2109The RXD pin is 1 in the last interrupt, in that case
2110we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2111know if another byte will come and this really is case 2. below
2112(e.g F=0xFF or 0xFE)
2113If RXD pin is 0 we can expect another character (see 2. below).
2114
2115
21162.
2117
2118 B B E or F__________________..__ V
2119.._|__________|__________|______ | |valid data
2120 "valid" or
2121 parity error
2122
2123Multiple frame errors with data == 0x00 (B),
2124but the part of the break trigs is interpreted as a start bit (and possibly
2125some 0 bits followed by a number of 1 bits and a stop bit).
2126Depending on parity settings etc. this last character can be either
2127a fake "valid" char (F) or have a parity error (E).
2128
2129If the character is valid it will be put in the buffer,
2130we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2131will set the flags so the tty will handle it,
2132if it's an error byte it will not be put in the buffer
2133and we set info->errorcode = ERRCODE_INSERT_BREAK.
2134
2135To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2136of the last faulty char (B) and compares it with the current time:
2137If the time elapsed time is less then 2*char_time_usec we will assume
2138it's a faked F char and not a Valid char and set
2139info->errorcode = ERRCODE_SET_BREAK.
2140
2141Flaws in the above solution:
2142~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2143We use the timer to distinguish a F character from a V character,
2144if a V character is to close after the break we might make the wrong decision.
2145
2146TODO: The break will be delayed until an F or V character is received.
2147
2148*/
2149
Jiri Slaby12aad552013-03-07 13:12:35 +01002150static void handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 unsigned long data_read;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* Read data and status at the same time */
Alan Coxd7283352008-08-04 17:21:18 +01002155 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156more_data:
2157 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2158 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2159 }
2160 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2161
2162 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2163 IO_MASK(R_SERIAL0_READ, par_err) |
2164 IO_MASK(R_SERIAL0_READ, overrun) )) {
2165 /* An error */
2166 info->last_rx_active_usec = GET_JIFFIES_USEC();
2167 info->last_rx_active = jiffies;
2168 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2169 DLOG_INT_TRIG(
2170 if (!log_int_trig1_pos) {
2171 log_int_trig1_pos = log_int_pos;
2172 log_int(rdpc(), 0, 0);
2173 }
2174 );
2175
2176
2177 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2178 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2179 /* Most likely a break, but we get interrupts over and
2180 * over again.
2181 */
2182
2183 if (!info->break_detected_cnt) {
2184 DEBUG_LOG(info->line, "#BRK start\n", 0);
2185 }
2186 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2187 /* The RX pin is high now, so the break
2188 * must be over, but....
2189 * we can't really know if we will get another
2190 * last byte ending the break or not.
2191 * And we don't know if the byte (if any) will
2192 * have an error or look valid.
2193 */
2194 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2195 info->errorcode = ERRCODE_INSERT_BREAK;
2196 }
2197 info->break_detected_cnt++;
2198 } else {
2199 /* The error does not look like a break, but could be
2200 * the end of one
2201 */
2202 if (info->break_detected_cnt) {
2203 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2204 info->errorcode = ERRCODE_INSERT_BREAK;
2205 } else {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002206 unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
2207 data_in, data_read);
2208 char flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (info->errorcode == ERRCODE_INSERT_BREAK) {
Jiri Slaby92a19f92013-01-03 15:53:03 +01002210 tty_insert_flip_char(&info->port, 0, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 info->icount.rx++;
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2215 info->icount.parity++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002216 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2218 info->icount.overrun++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002219 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2221 info->icount.frame++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002222 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01002224 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 info->errorcode = 0;
2226 }
2227 info->break_detected_cnt = 0;
2228 }
2229 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2230 /* No error */
2231 DLOG_INT_TRIG(
2232 if (!log_int_trig1_pos) {
2233 if (log_int_pos >= log_int_size) {
2234 log_int_pos = 0;
2235 }
2236 log_int_trig0_pos = log_int_pos;
2237 log_int(rdpc(), 0, 0);
2238 }
2239 );
Jiri Slaby92a19f92013-01-03 15:53:03 +01002240 tty_insert_flip_char(&info->port,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002241 IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
2242 TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 } else {
2244 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2245 }
2246
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 info->icount.rx++;
Alan Coxd7283352008-08-04 17:21:18 +01002249 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2251 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2252 goto more_data;
2253 }
2254
Jiri Slaby2e124b42013-01-03 15:53:06 +01002255 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Jiri Slaby12aad552013-03-07 13:12:35 +01002258static void handle_ser_rx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 unsigned char rstat;
2261
2262#ifdef SERIAL_DEBUG_INTR
2263 printk("Interrupt from serport %d\n", i);
2264#endif
2265/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2266 if (!info->uses_dma_in) {
Jiri Slaby12aad552013-03-07 13:12:35 +01002267 handle_ser_rx_interrupt_no_dma(info);
2268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 /* DMA is used */
Alan Coxd7283352008-08-04 17:21:18 +01002271 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2273 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2274 }
2275
2276 if (rstat & SER_ERROR_MASK) {
2277 unsigned char data;
2278
2279 info->last_rx_active_usec = GET_JIFFIES_USEC();
2280 info->last_rx_active = jiffies;
2281 /* If we got an error, we must reset it by reading the
2282 * data_in field
2283 */
Alan Coxd7283352008-08-04 17:21:18 +01002284 data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2286 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2287 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2288 /* Most likely a break, but we get interrupts over and
2289 * over again.
2290 */
2291
2292 if (!info->break_detected_cnt) {
2293 DEBUG_LOG(info->line, "#BRK start\n", 0);
2294 }
2295 if (rstat & SER_RXD_MASK) {
2296 /* The RX pin is high now, so the break
2297 * must be over, but....
2298 * we can't really know if we will get another
2299 * last byte ending the break or not.
2300 * And we don't know if the byte (if any) will
2301 * have an error or look valid.
2302 */
2303 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2304 info->errorcode = ERRCODE_INSERT_BREAK;
2305 }
2306 info->break_detected_cnt++;
2307 } else {
2308 /* The error does not look like a break, but could be
2309 * the end of one
2310 */
2311 if (info->break_detected_cnt) {
2312 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2313 info->errorcode = ERRCODE_INSERT_BREAK;
2314 } else {
2315 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2316 info->icount.brk++;
2317 add_char_and_flag(info, '\0', TTY_BREAK);
2318 }
2319
2320 if (rstat & SER_PAR_ERR_MASK) {
2321 info->icount.parity++;
2322 add_char_and_flag(info, data, TTY_PARITY);
2323 } else if (rstat & SER_OVERRUN_MASK) {
2324 info->icount.overrun++;
2325 add_char_and_flag(info, data, TTY_OVERRUN);
2326 } else if (rstat & SER_FRAMING_ERR_MASK) {
2327 info->icount.frame++;
2328 add_char_and_flag(info, data, TTY_FRAME);
2329 }
2330
2331 info->errorcode = 0;
2332 }
2333 info->break_detected_cnt = 0;
2334 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2335 ((rstat & SER_ERROR_MASK) << 8) | data);
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else { /* It was a valid byte, now let the DMA do the rest */
2338 unsigned long curr_time_u = GET_JIFFIES_USEC();
2339 unsigned long curr_time = jiffies;
2340
2341 if (info->break_detected_cnt) {
2342 /* Detect if this character is a new valid char or the
2343 * last char in a break sequence: If LSBits are 0 and
2344 * MSBits are high AND the time is close to the
2345 * previous interrupt we should discard it.
2346 */
2347 long elapsed_usec =
2348 (curr_time - info->last_rx_active) * (1000000/HZ) +
2349 curr_time_u - info->last_rx_active_usec;
2350 if (elapsed_usec < 2*info->char_time_usec) {
2351 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2352 /* Report as BREAK (error) and let
2353 * receive_chars_dma() handle it
2354 */
2355 info->errorcode = ERRCODE_SET_BREAK;
2356 } else {
2357 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2358 }
2359 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2360 }
2361
2362#ifdef SERIAL_DEBUG_INTR
2363 printk("** OK, disabling ser_interrupts\n");
2364#endif
2365 e100_disable_serial_data_irq(info);
2366 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2367 info->break_detected_cnt = 0;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 /* Restarting the DMA never hurts */
2371 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2372 START_FLUSH_FAST_TIMER(info, "ser_int");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373} /* handle_ser_rx_interrupt */
2374
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002375static void handle_ser_tx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 unsigned long flags;
2378
2379 if (info->x_char) {
2380 unsigned char rstat;
2381 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002382 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002383 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2385
Alan Coxd7283352008-08-04 17:21:18 +01002386 info->ioport[REG_TR_DATA] = info->x_char;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 info->icount.tx++;
2388 info->x_char = 0;
2389 /* We must enable since it is disabled in ser_interrupt */
2390 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002391 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return;
2393 }
2394 if (info->uses_dma_out) {
2395 unsigned char rstat;
2396 int i;
2397 /* We only use normal tx interrupt when sending x_char */
2398 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002399 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002400 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2402 e100_disable_serial_tx_ready_irq(info);
Takashi Iwaia88487c2008-07-16 21:54:42 +01002403 if (info->port.tty->stopped)
2404 rs_stop(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* Enable the DMA channel and tell it to continue */
2406 e100_enable_txdma_channel(info);
2407 /* Wait 12 cycles before doing the DMA command */
2408 for(i = 6; i > 0; i--)
2409 nop();
2410
2411 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002412 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return;
2414 }
2415 /* Normal char-by-char interrupt */
2416 if (info->xmit.head == info->xmit.tail
Jiri Slabyee797062013-03-07 13:12:34 +01002417 || info->port.tty->stopped) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01002418 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
2419 info->port.tty->stopped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 e100_disable_serial_tx_ready_irq(info);
2421 info->tr_running = 0;
2422 return;
2423 }
2424 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
2425 /* Send a byte, rs485 timing is critical so turn of ints */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002426 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002427 info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
2429 info->icount.tx++;
2430 if (info->xmit.head == info->xmit.tail) {
2431#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01002432 if (info->rs485.flags & SER_RS485_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Set a short timer to toggle RTS */
2434 start_one_shot_timer(&fast_timers_rs485[info->line],
2435 rs485_toggle_rts_timer_function,
2436 (unsigned long)info,
2437 info->char_time_usec*2,
2438 "RS-485");
2439 }
2440#endif /* RS485 */
2441 info->last_tx_active_usec = GET_JIFFIES_USEC();
2442 info->last_tx_active = jiffies;
2443 e100_disable_serial_tx_ready_irq(info);
2444 info->tr_running = 0;
2445 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
2446 } else {
2447 /* We must enable since it is disabled in ser_interrupt */
2448 e100_enable_serial_tx_ready_irq(info);
2449 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002450 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 if (CIRC_CNT(info->xmit.head,
2453 info->xmit.tail,
2454 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2455 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2456
2457} /* handle_ser_tx_interrupt */
2458
2459/* result of time measurements:
2460 * RX duration 54-60 us when doing something, otherwise 6-9 us
2461 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2462 */
2463static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01002464ser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 static volatile int tx_started = 0;
2467 struct e100_serial *info;
2468 int i;
2469 unsigned long flags;
2470 unsigned long irq_mask1_rd;
2471 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
2472 int handled = 0;
2473 static volatile unsigned long reentered_ready_mask = 0;
2474
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002475 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 irq_mask1_rd = *R_IRQ_MASK1_RD;
2477 /* First handle all rx interrupts with ints disabled */
2478 info = rs_table;
2479 irq_mask1_rd &= e100_ser_int_mask;
2480 for (i = 0; i < NR_PORTS; i++) {
2481 /* Which line caused the data irq? */
2482 if (irq_mask1_rd & data_mask) {
2483 handled = 1;
2484 handle_ser_rx_interrupt(info);
2485 }
2486 info += 1;
2487 data_mask <<= 2;
2488 }
2489 /* Handle tx interrupts with interrupts enabled so we
2490 * can take care of new data interrupts while transmitting
2491 * We protect the tx part with the tx_started flag.
2492 * We disable the tr_ready interrupts we are about to handle and
2493 * unblock the serial interrupt so new serial interrupts may come.
2494 *
2495 * If we get a new interrupt:
2496 * - it migth be due to synchronous serial ports.
2497 * - serial irq will be blocked by general irq handler.
2498 * - async data will be handled above (sync will be ignored).
2499 * - tx_started flag will prevent us from trying to send again and
2500 * we will exit fast - no need to unblock serial irq.
2501 * - Next (sync) serial interrupt handler will be runned with
2502 * disabled interrupt due to restore_flags() at end of function,
2503 * so sync handler will not be preempted or reentered.
2504 */
2505 if (!tx_started) {
2506 unsigned long ready_mask;
2507 unsigned long
2508 tx_started = 1;
2509 /* Only the tr_ready interrupts left */
2510 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2511 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2512 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2513 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2514 while (irq_mask1_rd) {
2515 /* Disable those we are about to handle */
2516 *R_IRQ_MASK1_CLR = irq_mask1_rd;
2517 /* Unblock the serial interrupt */
2518 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
2519
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002520 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
2522 info = rs_table;
2523 for (i = 0; i < NR_PORTS; i++) {
2524 /* Which line caused the ready irq? */
2525 if (irq_mask1_rd & ready_mask) {
2526 handled = 1;
2527 handle_ser_tx_interrupt(info);
2528 }
2529 info += 1;
2530 ready_mask <<= 2;
2531 }
2532 /* handle_ser_tx_interrupt enables tr_ready interrupts */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002533 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /* Handle reentered TX interrupt */
2535 irq_mask1_rd = reentered_ready_mask;
2536 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002537 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 tx_started = 0;
2539 } else {
2540 unsigned long ready_mask;
2541 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2542 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2543 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2544 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2545 if (ready_mask) {
2546 reentered_ready_mask |= ready_mask;
2547 /* Disable those we are about to handle */
2548 *R_IRQ_MASK1_CLR = ready_mask;
2549 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
2550 }
2551 }
2552
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002553 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return IRQ_RETVAL(handled);
2555} /* ser_interrupt */
2556#endif
2557
2558/*
2559 * -------------------------------------------------------------------
2560 * Here ends the serial interrupt routines.
2561 * -------------------------------------------------------------------
2562 */
2563
2564/*
2565 * This routine is used to handle the "bottom half" processing for the
2566 * serial driver, known also the "software interrupt" processing.
2567 * This processing is done at the kernel interrupt level, after the
2568 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2569 * is where time-consuming activities which can not be done in the
2570 * interrupt driver proper are done; the interrupt driver schedules
2571 * them using rs_sched_event(), and they get done here.
2572 */
2573static void
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002574do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002576 struct e100_serial *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct tty_struct *tty;
2578
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002579 info = container_of(work, struct e100_serial, work);
2580
Takashi Iwaia88487c2008-07-16 21:54:42 +01002581 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (!tty)
2583 return;
2584
Jiri Slabyb963a842007-02-10 01:44:55 -08002585 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
2586 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589static int
2590startup(struct e100_serial * info)
2591{
2592 unsigned long flags;
2593 unsigned long xmit_page;
2594 int i;
2595
2596 xmit_page = get_zeroed_page(GFP_KERNEL);
2597 if (!xmit_page)
2598 return -ENOMEM;
2599
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002600 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 /* if it was already initialized, skip this */
2603
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002604 if (info->port.flags & ASYNC_INITIALIZED) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002605 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 free_page(xmit_page);
2607 return 0;
2608 }
2609
2610 if (info->xmit.buf)
2611 free_page(xmit_page);
2612 else
2613 info->xmit.buf = (unsigned char *) xmit_page;
2614
2615#ifdef SERIAL_DEBUG_OPEN
2616 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
2617#endif
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
2620 * Clear the FIFO buffers and disable them
2621 * (they will be reenabled in change_speed())
2622 */
2623
2624 /*
2625 * Reset the DMA channels and make sure their interrupts are cleared
2626 */
2627
2628 if (info->dma_in_enabled) {
2629 info->uses_dma_in = 1;
2630 e100_enable_rxdma_channel(info);
2631
2632 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2633
2634 /* Wait until reset cycle is complete */
2635 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2636 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2637
2638 /* Make sure the irqs are cleared */
2639 *info->iclrintradr =
2640 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2641 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2642 } else {
2643 e100_disable_rxdma_channel(info);
2644 }
2645
2646 if (info->dma_out_enabled) {
2647 info->uses_dma_out = 1;
2648 e100_enable_txdma_channel(info);
2649 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2650
2651 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2652 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2653
2654 /* Make sure the irqs are cleared */
2655 *info->oclrintradr =
2656 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2657 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2658 } else {
2659 e100_disable_txdma_channel(info);
2660 }
2661
Takashi Iwaia88487c2008-07-16 21:54:42 +01002662 if (info->port.tty)
2663 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 info->xmit.head = info->xmit.tail = 0;
2666 info->first_recv_buffer = info->last_recv_buffer = NULL;
2667 info->recv_cnt = info->max_recv_cnt = 0;
2668
2669 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2670 info->rec_descr[i].buf = 0;
2671
2672 /*
2673 * and set the speed and other flags of the serial port
2674 * this will start the rx/tx as well
2675 */
2676#ifdef SERIAL_HANDLE_EARLY_ERRORS
2677 e100_enable_serial_data_irq(info);
2678#endif
2679 change_speed(info);
2680
2681 /* dummy read to reset any serial errors */
2682
Alan Coxd7283352008-08-04 17:21:18 +01002683 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 /* enable the interrupts */
2686 if (info->uses_dma_out)
2687 e100_enable_txdma_irq(info);
2688
2689 e100_enable_rx_irq(info);
2690
2691 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2692
2693 /* setup the dma input descriptor and start dma */
2694
2695 start_receive(info);
2696
2697 /* for safety, make sure the descriptors last result is 0 bytes written */
2698
2699 info->tr_descr.sw_len = 0;
2700 info->tr_descr.hw_len = 0;
2701 info->tr_descr.status = 0;
2702
2703 /* enable RTS/DTR last */
2704
2705 e100_rts(info, 1);
2706 e100_dtr(info, 1);
2707
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002708 info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002710 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return 0;
2712}
2713
2714/*
2715 * This routine will shutdown a serial port; interrupts are disabled, and
2716 * DTR is dropped if the hangup on close termio flag is on.
2717 */
2718static void
2719shutdown(struct e100_serial * info)
2720{
2721 unsigned long flags;
2722 struct etrax_dma_descr *descr = info->rec_descr;
2723 struct etrax_recv_buffer *buffer;
2724 int i;
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* shut down the transmitter and receiver */
2727 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
2728 e100_disable_rx(info);
Alan Coxd7283352008-08-04 17:21:18 +01002729 info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 /* disable interrupts, reset dma channels */
2732 if (info->uses_dma_in) {
2733 e100_disable_rxdma_irq(info);
2734 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2735 info->uses_dma_in = 0;
2736 } else {
2737 e100_disable_serial_data_irq(info);
2738 }
2739
2740 if (info->uses_dma_out) {
2741 e100_disable_txdma_irq(info);
2742 info->tr_running = 0;
2743 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2744 info->uses_dma_out = 0;
2745 } else {
2746 e100_disable_serial_tx_ready_irq(info);
2747 info->tr_running = 0;
2748 }
2749
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002750 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return;
2752
2753#ifdef SERIAL_DEBUG_OPEN
2754 printk("Shutting down serial port %d (irq %d)....\n", info->line,
2755 info->irq);
2756#endif
2757
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002758 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 if (info->xmit.buf) {
2761 free_page((unsigned long)info->xmit.buf);
2762 info->xmit.buf = NULL;
2763 }
2764
2765 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2766 if (descr[i].buf) {
2767 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2768 kfree(buffer);
2769 descr[i].buf = 0;
2770 }
2771
Alan Coxadc8d742012-07-14 15:31:47 +01002772 if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /* hang up DTR and RTS if HUPCL is enabled */
2774 e100_dtr(info, 0);
2775 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2776 }
2777
Takashi Iwaia88487c2008-07-16 21:54:42 +01002778 if (info->port.tty)
2779 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002781 info->port.flags &= ~ASYNC_INITIALIZED;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002782 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785
2786/* change baud rate and other assorted parameters */
2787
2788static void
2789change_speed(struct e100_serial *info)
2790{
2791 unsigned int cflag;
2792 unsigned long xoff;
2793 unsigned long flags;
2794 /* first some safety checks */
2795
Alan Coxadc8d742012-07-14 15:31:47 +01002796 if (!info->port.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return;
Alan Coxd7283352008-08-04 17:21:18 +01002798 if (!info->ioport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return;
2800
Alan Coxadc8d742012-07-14 15:31:47 +01002801 cflag = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 /* possibly, the tx/rx should be disabled first to do this safely */
2804
2805 /* change baud-rate and write it to the hardware */
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002806 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /* Special baudrate */
2808 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2809 unsigned long alt_source =
2810 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2811 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2812 /* R_ALT_SER_BAUDRATE selects the source */
2813 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2814 (unsigned long)info->baud_base, info->custom_divisor));
2815 if (info->baud_base == SERIAL_PRESCALE_BASE) {
2816 /* 0, 2-65535 (0=65536) */
2817 u16 divisor = info->custom_divisor;
2818 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2819 /* baudrate is 3.125MHz/custom_divisor */
2820 alt_source =
2821 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
2822 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
2823 alt_source = 0x11;
2824 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
2825 *R_SERIAL_PRESCALE = divisor;
2826 info->baud = SERIAL_PRESCALE_BASE/divisor;
2827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 else
2829 {
2830 /* Bad baudbase, we don't support using timer0
2831 * for baudrate.
2832 */
2833 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
2834 (unsigned long)info->baud_base, info->custom_divisor);
2835 }
2836 r_alt_ser_baudrate_shadow &= ~mask;
2837 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
2838 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
2839 } else {
2840 /* Normal baudrate */
2841 /* Make sure we use normal baudrate */
2842 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2843 unsigned long alt_source =
2844 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2845 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2846 r_alt_ser_baudrate_shadow &= ~mask;
2847 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 info->baud = cflag_to_baud(cflag);
Alan Coxd7283352008-08-04 17:21:18 +01002851 info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /* start with default settings and then fill in changes */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002855 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 /* 8 bit, no/even parity */
2857 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
2858 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
2859 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
2860
2861 /* 8 bit, no/even parity, 1 stop bit, no cts */
2862 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
2863 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
2864 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
2865 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
2866 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
2867
2868 if ((cflag & CSIZE) == CS7) {
2869 /* set 7 bit mode */
2870 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
2871 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
2872 }
2873
2874 if (cflag & CSTOPB) {
2875 /* set 2 stop bit mode */
2876 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
2877 }
2878
2879 if (cflag & PARENB) {
2880 /* enable parity */
2881 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
2882 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
2883 }
2884
2885 if (cflag & CMSPAR) {
2886 /* enable stick parity, PARODD mean Mark which matches ETRAX */
2887 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
2888 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
2889 }
2890 if (cflag & PARODD) {
2891 /* set odd parity (or Mark if CMSPAR) */
2892 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
2893 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
2894 }
2895
2896 if (cflag & CRTSCTS) {
2897 /* enable automatic CTS handling */
2898 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
2899 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
2900 }
2901
2902 /* make sure the tx and rx are enabled */
2903
2904 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
2905 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
2906
2907 /* actually write the control regs to the hardware */
2908
Alan Coxd7283352008-08-04 17:21:18 +01002909 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
2910 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
Takashi Iwaia88487c2008-07-16 21:54:42 +01002911 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
Alan Coxadc8d742012-07-14 15:31:47 +01002913 if (info->port.tty->termios.c_iflag & IXON ) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01002914 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
2915 STOP_CHAR(info->port.tty)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
2917 }
2918
Alan Coxd7283352008-08-04 17:21:18 +01002919 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002920 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 update_char_time(info);
2923
2924} /* change_speed */
2925
2926/* start transmitting chars NOW */
2927
2928static void
2929rs_flush_chars(struct tty_struct *tty)
2930{
2931 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2932 unsigned long flags;
2933
2934 if (info->tr_running ||
2935 info->xmit.head == info->xmit.tail ||
2936 tty->stopped ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 !info->xmit.buf)
2938 return;
2939
2940#ifdef SERIAL_DEBUG_FLOW
2941 printk("rs_flush_chars\n");
2942#endif
2943
2944 /* this protection might not exactly be necessary here */
2945
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002946 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 start_transmit(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002948 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002951static int rs_raw_write(struct tty_struct *tty,
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002952 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 int c, ret = 0;
2955 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2956 unsigned long flags;
2957
2958 /* first some sanity checks */
2959
Cong Dingf938f372013-01-16 23:30:44 +01002960 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return 0;
2962
2963#ifdef SERIAL_DEBUG_DATA
2964 if (info->line == SERIAL_DEBUG_LINE)
2965 printk("rs_raw_write (%d), status %d\n",
Alan Coxd7283352008-08-04 17:21:18 +01002966 count, info->ioport[REG_STATUS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967#endif
2968
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002969 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
Peter Hurleyfdfb7192016-01-10 22:40:54 -08002971 DFLOW(DEBUG_LOG(info->line, "ldisc\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002974 /* The local_irq_disable/restore_flags pairs below are needed
2975 * because the DMA interrupt handler moves the info->xmit values.
2976 * the memcpy needs to be in the critical region unfortunately,
2977 * because we need to read xmit values, memcpy, write xmit values
2978 * in one atomic operation... this could perhaps be avoided by
2979 * more clever design.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002981 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 while (count) {
2983 c = CIRC_SPACE_TO_END(info->xmit.head,
2984 info->xmit.tail,
2985 SERIAL_XMIT_SIZE);
2986
2987 if (count < c)
2988 c = count;
2989 if (c <= 0)
2990 break;
2991
2992 memcpy(info->xmit.buf + info->xmit.head, buf, c);
2993 info->xmit.head = (info->xmit.head + c) &
2994 (SERIAL_XMIT_SIZE-1);
2995 buf += c;
2996 count -= c;
2997 ret += c;
2998 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002999 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 /* enable transmitter if not running, unless the tty is stopped
3002 * this does not need IRQ protection since if tr_running == 0
3003 * the IRQ's are not running anyway for this port.
3004 */
3005 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3006
3007 if (info->xmit.head != info->xmit.tail &&
3008 !tty->stopped &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 !info->tr_running) {
3010 start_transmit(info);
3011 }
3012
3013 return ret;
3014} /* raw_raw_write() */
3015
3016static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003017rs_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 const unsigned char *buf, int count)
3019{
3020#if defined(CONFIG_ETRAX_RS485)
3021 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3022
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003023 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 {
3025 /* If we are in RS-485 mode, we need to toggle RTS and disable
3026 * the receiver before initiating a DMA transfer
3027 */
3028#ifdef CONFIG_ETRAX_FAST_TIMER
3029 /* Abort any started timer */
3030 fast_timers_rs485[info->line].function = NULL;
3031 del_fast_timer(&fast_timers_rs485[info->line]);
3032#endif
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003033 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3035 e100_disable_rx(info);
3036 e100_enable_rx_irq(info);
3037#endif
Claudio Scordino93f33502011-11-09 15:51:49 +01003038 if (info->rs485.delay_rts_before_send > 0)
3039 msleep(info->rs485.delay_rts_before_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041#endif /* CONFIG_ETRAX_RS485 */
3042
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003043 count = rs_raw_write(tty, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003046 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 {
3048 unsigned int val;
3049 /* If we are in RS-485 mode the following has to be done:
3050 * wait until DMA is ready
3051 * wait on transmit shift register
3052 * toggle RTS
3053 * enable the receiver
3054 */
3055
3056 /* Sleep until all sent */
3057 tty_wait_until_sent(tty, 0);
3058#ifdef CONFIG_ETRAX_FAST_TIMER
3059 /* Now sleep a little more so that shift register is empty */
3060 schedule_usleep(info->char_time_usec * 2);
3061#endif
3062 /* wait on transmit shift register */
3063 do{
3064 get_lsr_info(info, &val);
3065 }while (!(val & TIOCSER_TEMT));
3066
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003067 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3070 e100_enable_rx(info);
3071 e100_enable_rxdma_irq(info);
3072#endif
3073 }
3074#endif /* CONFIG_ETRAX_RS485 */
3075
3076 return count;
3077} /* rs_write */
3078
3079
3080/* how much space is available in the xmit buffer? */
3081
3082static int
3083rs_write_room(struct tty_struct *tty)
3084{
3085 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3086
3087 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3088}
3089
3090/* How many chars are in the xmit buffer?
3091 * This does not include any chars in the transmitter FIFO.
3092 * Use wait_until_sent for waiting for FIFO drain.
3093 */
3094
3095static int
3096rs_chars_in_buffer(struct tty_struct *tty)
3097{
3098 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3099
3100 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3101}
3102
3103/* discard everything in the xmit buffer */
3104
3105static void
3106rs_flush_buffer(struct tty_struct *tty)
3107{
3108 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3109 unsigned long flags;
3110
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003111 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 info->xmit.head = info->xmit.tail = 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003113 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Jiri Slabyb963a842007-02-10 01:44:55 -08003115 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118/*
3119 * This function is used to send a high-priority XON/XOFF character to
3120 * the device
3121 *
3122 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3123 * but we do it in handle_ser_tx_interrupt().
3124 * We disable DMA channel and enable tx ready interrupt and write the
3125 * character when possible.
3126 */
3127static void rs_send_xchar(struct tty_struct *tty, char ch)
3128{
3129 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3130 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003131 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (info->uses_dma_out) {
3133 /* Put the DMA on hold and disable the channel */
3134 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3135 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3136 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3137 e100_disable_txdma_channel(info);
3138 }
3139
3140 /* Must make sure transmitter is not stopped before we can transmit */
3141 if (tty->stopped)
3142 rs_start(tty);
3143
3144 /* Enable manual transmit interrupt and send from there */
3145 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3146 info->x_char = ch;
3147 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003148 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151/*
3152 * ------------------------------------------------------------
3153 * rs_throttle()
3154 *
3155 * This routine is called by the upper-layer tty layer to signal that
3156 * incoming characters should be throttled.
3157 * ------------------------------------------------------------
3158 */
3159static void
3160rs_throttle(struct tty_struct * tty)
3161{
3162 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3163#ifdef SERIAL_DEBUG_THROTTLE
Peter Hurleyfdfb7192016-01-10 22:40:54 -08003164 printk("throttle %s ....\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165#endif
Peter Hurleyfdfb7192016-01-10 22:40:54 -08003166 DFLOW(DEBUG_LOG(info->line,"rs_throttle\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003169 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 /* Turn off RTS line */
3171 e100_rts(info, 0);
3172 }
3173 if (I_IXOFF(tty))
3174 rs_send_xchar(tty, STOP_CHAR(tty));
3175
3176}
3177
3178static void
3179rs_unthrottle(struct tty_struct * tty)
3180{
3181 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3182#ifdef SERIAL_DEBUG_THROTTLE
Peter Hurleyfdfb7192016-01-10 22:40:54 -08003183 printk("unthrottle %s ....\n", tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184#endif
Peter Hurleyfdfb7192016-01-10 22:40:54 -08003185 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3187 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003188 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /* Assert RTS line */
3190 e100_rts(info, 1);
3191 }
3192
3193 if (I_IXOFF(tty)) {
3194 if (info->x_char)
3195 info->x_char = 0;
3196 else
3197 rs_send_xchar(tty, START_CHAR(tty));
3198 }
3199
3200}
3201
3202/*
3203 * ------------------------------------------------------------
3204 * rs_ioctl() and friends
3205 * ------------------------------------------------------------
3206 */
3207
3208static int
3209get_serial_info(struct e100_serial * info,
3210 struct serial_struct * retinfo)
3211{
3212 struct serial_struct tmp;
3213
3214 /* this is all probably wrong, there are a lot of fields
3215 * here that we don't have in e100_serial and maybe we
3216 * should set them to something else than 0.
3217 */
3218
3219 if (!retinfo)
3220 return -EFAULT;
3221 memset(&tmp, 0, sizeof(tmp));
3222 tmp.type = info->type;
3223 tmp.line = info->line;
Alan Coxd7283352008-08-04 17:21:18 +01003224 tmp.port = (int)info->ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 tmp.irq = info->irq;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003226 tmp.flags = info->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 tmp.baud_base = info->baud_base;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003228 tmp.close_delay = info->port.close_delay;
3229 tmp.closing_wait = info->port.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 tmp.custom_divisor = info->custom_divisor;
3231 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3232 return -EFAULT;
3233 return 0;
3234}
3235
3236static int
3237set_serial_info(struct e100_serial *info,
3238 struct serial_struct *new_info)
3239{
3240 struct serial_struct new_serial;
3241 struct e100_serial old_info;
3242 int retval = 0;
3243
3244 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3245 return -EFAULT;
3246
3247 old_info = *info;
3248
3249 if (!capable(CAP_SYS_ADMIN)) {
3250 if ((new_serial.type != info->type) ||
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003251 (new_serial.close_delay != info->port.close_delay) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003253 (info->port.flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return -EPERM;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003255 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 (new_serial.flags & ASYNC_USR_MASK));
3257 goto check_and_exit;
3258 }
3259
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003260 if (info->port.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return -EBUSY;
3262
3263 /*
3264 * OK, past this point, all the error checking has been done.
3265 * At this point, we start making changes.....
3266 */
3267
3268 info->baud_base = new_serial.baud_base;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003269 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 (new_serial.flags & ASYNC_FLAGS));
3271 info->custom_divisor = new_serial.custom_divisor;
3272 info->type = new_serial.type;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003273 info->port.close_delay = new_serial.close_delay;
3274 info->port.closing_wait = new_serial.closing_wait;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003275 info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 check_and_exit:
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003278 if (info->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 change_speed(info);
3280 } else
3281 retval = startup(info);
3282 return retval;
3283}
3284
3285/*
3286 * get_lsr_info - get line status register info
3287 *
3288 * Purpose: Let user call ioctl() to get info when the UART physically
3289 * is emptied. On bus types like RS485, the transmitter must
3290 * release the bus after transmitting. This must be done when
3291 * the transmit shift register is empty, not be done when the
3292 * transmit holding register is empty. This functionality
3293 * allows an RS485 driver to be written in user space.
3294 */
3295static int
3296get_lsr_info(struct e100_serial * info, unsigned int *value)
3297{
3298 unsigned int result = TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 unsigned long curr_time = jiffies;
3300 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3301 unsigned long elapsed_usec =
3302 (curr_time - info->last_tx_active) * 1000000/HZ +
3303 curr_time_usec - info->last_tx_active_usec;
3304
3305 if (info->xmit.head != info->xmit.tail ||
3306 elapsed_usec < 2*info->char_time_usec) {
3307 result = 0;
3308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 if (copy_to_user(value, &result, sizeof(int)))
3311 return -EFAULT;
3312 return 0;
3313}
3314
3315#ifdef SERIAL_DEBUG_IO
3316struct state_str
3317{
3318 int state;
3319 const char *str;
3320};
3321
3322const struct state_str control_state_str[] = {
3323 {TIOCM_DTR, "DTR" },
3324 {TIOCM_RTS, "RTS"},
3325 {TIOCM_ST, "ST?" },
3326 {TIOCM_SR, "SR?" },
3327 {TIOCM_CTS, "CTS" },
3328 {TIOCM_CD, "CD" },
3329 {TIOCM_RI, "RI" },
3330 {TIOCM_DSR, "DSR" },
3331 {0, NULL }
3332};
3333
3334char *get_control_state_str(int MLines, char *s)
3335{
3336 int i = 0;
3337
3338 s[0]='\0';
3339 while (control_state_str[i].str != NULL) {
3340 if (MLines & control_state_str[i].state) {
3341 if (s[0] != '\0') {
3342 strcat(s, ", ");
3343 }
3344 strcat(s, control_state_str[i].str);
3345 }
3346 i++;
3347 }
3348 return s;
3349}
3350#endif
3351
Alan Coxd7283352008-08-04 17:21:18 +01003352static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003353rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003355 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3356 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Alan Coxd7283352008-08-04 17:21:18 +01003358 if (!info->ioport)
3359 return -EIO;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003360
3361 local_irq_save(flags);
3362 if (break_state == -1) {
3363 /* Go to manual mode and set the txd pin to 0 */
3364 /* Clear bit 7 (txd) and 6 (tr_enable) */
3365 info->tx_ctrl &= 0x3F;
3366 } else {
3367 /* Set bit 7 (txd) and 6 (tr_enable) */
3368 info->tx_ctrl |= (0x80 | 0x40);
3369 }
Alan Coxd7283352008-08-04 17:21:18 +01003370 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003371 local_irq_restore(flags);
Alan Coxd7283352008-08-04 17:21:18 +01003372 return 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003373}
3374
3375static int
Alan Cox20b9d172011-02-14 16:26:50 +00003376rs_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003377{
3378 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
Alan Cox978e5952008-04-30 00:53:59 -07003379 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003380
Alan Cox978e5952008-04-30 00:53:59 -07003381 local_irq_save(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003382
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003383 if (clear & TIOCM_RTS)
3384 e100_rts(info, 0);
3385 if (clear & TIOCM_DTR)
3386 e100_dtr(info, 0);
3387 /* Handle FEMALE behaviour */
3388 if (clear & TIOCM_RI)
3389 e100_ri_out(info, 0);
3390 if (clear & TIOCM_CD)
3391 e100_cd_out(info, 0);
3392
3393 if (set & TIOCM_RTS)
3394 e100_rts(info, 1);
3395 if (set & TIOCM_DTR)
3396 e100_dtr(info, 1);
3397 /* Handle FEMALE behaviour */
3398 if (set & TIOCM_RI)
3399 e100_ri_out(info, 1);
3400 if (set & TIOCM_CD)
3401 e100_cd_out(info, 1);
Alan Cox032c17e2008-04-28 02:13:50 -07003402
Alan Cox978e5952008-04-30 00:53:59 -07003403 local_irq_restore(flags);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003404 return 0;
3405}
3406
3407static int
Alan Cox60b33c12011-02-14 16:26:14 +00003408rs_tiocmget(struct tty_struct *tty)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003409{
3410 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3411 unsigned int result;
Alan Cox978e5952008-04-30 00:53:59 -07003412 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Alan Cox978e5952008-04-30 00:53:59 -07003414 local_irq_save(flags);
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 result =
3417 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3418 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3419 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3420 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3421 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3422 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3423
Alan Cox978e5952008-04-30 00:53:59 -07003424 local_irq_restore(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426#ifdef SERIAL_DEBUG_IO
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003427 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3428 info->line, result, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 {
3430 char s[100];
3431
3432 get_control_state_str(result, s);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003433 printk(KERN_DEBUG "state: %s\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
3435#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003436 return result;
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440
3441static int
Alan Cox6caa76b2011-02-14 16:27:22 +00003442rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 unsigned int cmd, unsigned long arg)
3444{
3445 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3446
3447 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3448 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3449 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3450 if (tty->flags & (1 << TTY_IO_ERROR))
3451 return -EIO;
3452 }
3453
3454 switch (cmd) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003455 case TIOCGSERIAL:
3456 return get_serial_info(info,
3457 (struct serial_struct *) arg);
3458 case TIOCSSERIAL:
3459 return set_serial_info(info,
3460 (struct serial_struct *) arg);
3461 case TIOCSERGETLSR: /* Get line status register */
3462 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003464 case TIOCSERGSTRUCT:
3465 if (copy_to_user((struct e100_serial *) arg,
3466 info, sizeof(struct e100_serial)))
3467 return -EFAULT;
3468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470#if defined(CONFIG_ETRAX_RS485)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003471 case TIOCSERSETRS485:
3472 {
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003473 /* In this ioctl we still use the old structure
3474 * rs485_control for backward compatibility
3475 * (if we use serial_rs485, then old user-level code
3476 * wouldn't work anymore...).
3477 * The use of this ioctl is deprecated: use TIOCSRS485
3478 * instead.*/
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003479 struct rs485_control rs485ctrl;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003480 struct serial_rs485 rs485data;
3481 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003482 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3483 sizeof(rs485ctrl)))
3484 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003486 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3487 rs485data.flags = 0;
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02003488
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003489 if (rs485ctrl.enabled)
3490 rs485data.flags |= SER_RS485_ENABLED;
3491 else
3492 rs485data.flags &= ~(SER_RS485_ENABLED);
3493
3494 if (rs485ctrl.rts_on_send)
3495 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3496 else
3497 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3498
3499 if (rs485ctrl.rts_after_sent)
3500 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3501 else
3502 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3503
3504 return e100_enable_rs485(tty, &rs485data);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003507 case TIOCSRS485:
3508 {
3509 /* This is the new version of TIOCSRS485, with new
3510 * data structure serial_rs485 */
3511 struct serial_rs485 rs485data;
3512 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3513 sizeof(rs485data)))
3514 return -EFAULT;
3515
3516 return e100_enable_rs485(tty, &rs485data);
3517 }
3518
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003519 case TIOCGRS485:
3520 {
3521 struct serial_rs485 *rs485data =
3522 &(((struct e100_serial *)tty->driver_data)->rs485);
3523 /* This is the ioctl to get RS485 data from user-space */
3524 if (copy_to_user((struct serial_rs485 *) arg,
3525 rs485data,
Jesper Nilsson94479c02010-10-28 12:04:55 +02003526 sizeof(struct serial_rs485)))
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003527 return -EFAULT;
3528 break;
3529 }
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003530
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003531 case TIOCSERWRRS485:
3532 {
3533 struct rs485_write rs485wr;
3534 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3535 sizeof(rs485wr)))
3536 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003538 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540#endif
3541
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003542 default:
3543 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
3545 return 0;
3546}
3547
3548static void
Alan Cox606d0992006-12-08 02:38:45 -08003549rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 change_speed(info);
3554
3555 /* Handle turning off CRTSCTS */
3556 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyee797062013-03-07 13:12:34 +01003557 !(tty->termios.c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 rs_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560}
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/*
3563 * ------------------------------------------------------------
3564 * rs_close()
3565 *
3566 * This routine is called when the serial port gets closed. First, we
3567 * wait for the last remaining data to be sent. Then, we unlink its
3568 * S structure from the interrupt chain if necessary, and we free
3569 * that IRQ if nothing is left in the chain.
3570 * ------------------------------------------------------------
3571 */
3572static void
3573rs_close(struct tty_struct *tty, struct file * filp)
3574{
3575 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3576 unsigned long flags;
3577
3578 if (!info)
3579 return;
3580
3581 /* interrupts are disabled for this entire function */
3582
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003583 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 if (tty_hung_up_p(filp)) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003586 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return;
3588 }
3589
3590#ifdef SERIAL_DEBUG_OPEN
3591 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3592 info->line, info->count);
3593#endif
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003594 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 /*
3596 * Uh, oh. tty->count is 1, which means that the tty
3597 * structure will be freed. Info->count should always
3598 * be one in these conditions. If it's greater than
3599 * one, we've got real problems, since it means the
3600 * serial port won't be shutdown.
3601 */
WANG Cong3d43b7d2011-09-01 16:47:49 +08003602 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 "rs_close: bad serial port count; tty->count is 1, "
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003604 "info->count is %d\n", info->port.count);
3605 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003607 if (--info->port.count < 0) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08003608 printk(KERN_ERR "rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003609 info->line, info->port.count);
3610 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003612 if (info->port.count) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003613 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return;
3615 }
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003616 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 * Now we wait for the transmit buffer to clear; and we notify
3619 * the line discipline to only process XON/XOFF characters.
3620 */
3621 tty->closing = 1;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003622 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
3623 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /*
3625 * At this point we stop accepting input. To do this, we
3626 * disable the serial receiver and the DMA receive interrupt.
3627 */
3628#ifdef SERIAL_HANDLE_EARLY_ERRORS
3629 e100_disable_serial_data_irq(info);
3630#endif
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 e100_disable_rx(info);
3633 e100_disable_rx_irq(info);
3634
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003635 if (info->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /*
3637 * Before we drop DTR, make sure the UART transmitter
3638 * has completely drained; this is especially
3639 * important as we have a transmit FIFO!
3640 */
3641 rs_wait_until_sent(tty, HZ);
3642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
3644 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07003645 rs_flush_buffer(tty);
Alan Cox454aa382008-05-12 12:31:37 +01003646 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 tty->closing = 0;
3648 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003649 info->port.tty = NULL;
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003650 if (info->port.blocked_open) {
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003651 if (info->port.close_delay)
3652 schedule_timeout_interruptible(info->port.close_delay);
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003653 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003655 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003656 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 /* port closed */
3659
3660#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003661 if (info->rs485.flags & SER_RS485_ENABLED) {
3662 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663#if defined(CONFIG_ETRAX_RS485_ON_PA)
3664 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
3667#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003668
3669 /*
3670 * Release any allocated DMA irq's.
3671 */
3672 if (info->dma_in_enabled) {
3673 free_irq(info->dma_in_irq_nbr, info);
3674 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3675 info->uses_dma_in = 0;
3676#ifdef SERIAL_DEBUG_OPEN
3677 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3678 info->dma_in_irq_description);
3679#endif
3680 }
3681 if (info->dma_out_enabled) {
3682 free_irq(info->dma_out_irq_nbr, info);
3683 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3684 info->uses_dma_out = 0;
3685#ifdef SERIAL_DEBUG_OPEN
3686 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3687 info->dma_out_irq_description);
3688#endif
3689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
3692/*
3693 * rs_wait_until_sent() --- wait until the transmitter is empty
3694 */
3695static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3696{
3697 unsigned long orig_jiffies;
3698 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3699 unsigned long curr_time = jiffies;
3700 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3701 long elapsed_usec =
3702 (curr_time - info->last_tx_active) * (1000000/HZ) +
3703 curr_time_usec - info->last_tx_active_usec;
3704
3705 /*
3706 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3707 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3708 */
3709 orig_jiffies = jiffies;
3710 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3711 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3712 (elapsed_usec < 2*info->char_time_usec)) {
Nishanth Aravamudan3c76bc52005-11-07 01:01:21 -08003713 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 if (signal_pending(current))
3715 break;
3716 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3717 break;
3718 curr_time = jiffies;
3719 curr_time_usec = GET_JIFFIES_USEC();
3720 elapsed_usec =
3721 (curr_time - info->last_tx_active) * (1000000/HZ) +
3722 curr_time_usec - info->last_tx_active_usec;
3723 }
3724 set_current_state(TASK_RUNNING);
3725}
3726
3727/*
3728 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3729 */
3730void
3731rs_hangup(struct tty_struct *tty)
3732{
3733 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3734
3735 rs_flush_buffer(tty);
3736 shutdown(info);
3737 info->event = 0;
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003738 info->port.count = 0;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003739 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003740 info->port.tty = NULL;
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003741 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744/*
3745 * ------------------------------------------------------------
3746 * rs_open() and friends
3747 * ------------------------------------------------------------
3748 */
3749static int
3750block_til_ready(struct tty_struct *tty, struct file * filp,
3751 struct e100_serial *info)
3752{
3753 DECLARE_WAITQUEUE(wait, current);
3754 unsigned long flags;
3755 int retval;
Peter Hurleye359a4e2014-06-16 09:17:06 -04003756 int do_clocal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 * If non-blocking mode is set, or the port is not enabled,
3760 * then make the check up front and then exit.
3761 */
3762 if ((filp->f_flags & O_NONBLOCK) ||
3763 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003764 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return 0;
3766 }
3767
Alan Coxadc8d742012-07-14 15:31:47 +01003768 if (tty->termios.c_cflag & CLOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 do_clocal = 1;
3770 }
3771
3772 /*
3773 * Block waiting for the carrier detect and the line to become
3774 * free (i.e., not in use by the callout). While we are in
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003775 * this loop, info->port.count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 * rs_close() knows when to free things. We restore it upon
3777 * exit, either normal or abnormal.
3778 */
3779 retval = 0;
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003780 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781#ifdef SERIAL_DEBUG_OPEN
3782 printk("block_til_ready before block: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003783 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003785 local_irq_save(flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -04003786 info->port.count--;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003787 local_irq_restore(flags);
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003788 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 while (1) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003790 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 /* assert RTS and DTR */
3792 e100_rts(info, 1);
3793 e100_dtr(info, 1);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003794 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 set_current_state(TASK_INTERRUPTIBLE);
3796 if (tty_hung_up_p(filp) ||
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003797 !(info->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798#ifdef SERIAL_DO_RESTART
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003799 if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 retval = -EAGAIN;
3801 else
3802 retval = -ERESTARTSYS;
3803#else
3804 retval = -EAGAIN;
3805#endif
3806 break;
3807 }
Peter Hurleyfef062c2015-10-10 16:00:52 -04003808 if (do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 /* && (do_clocal || DCD_IS_ASSERTED) */
3810 break;
3811 if (signal_pending(current)) {
3812 retval = -ERESTARTSYS;
3813 break;
3814 }
3815#ifdef SERIAL_DEBUG_OPEN
3816 printk("block_til_ready blocking: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003817 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818#endif
Alan Cox89c8d912012-08-08 16:30:13 +01003819 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +01003821 tty_lock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823 set_current_state(TASK_RUNNING);
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003824 remove_wait_queue(&info->port.open_wait, &wait);
Peter Hurleye359a4e2014-06-16 09:17:06 -04003825 if (!tty_hung_up_p(filp))
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003826 info->port.count++;
3827 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828#ifdef SERIAL_DEBUG_OPEN
3829 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003830 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831#endif
3832 if (retval)
3833 return retval;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003834 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return 0;
3836}
3837
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003838static void
3839deinit_port(struct e100_serial *info)
3840{
3841 if (info->dma_out_enabled) {
3842 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3843 free_irq(info->dma_out_irq_nbr, info);
3844 }
3845 if (info->dma_in_enabled) {
3846 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3847 free_irq(info->dma_in_irq_nbr, info);
3848 }
3849}
3850
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851/*
3852 * This routine is called whenever a serial port is opened.
3853 * It performs the serial-specific initialization for the tty structure.
3854 */
3855static int
3856rs_open(struct tty_struct *tty, struct file * filp)
3857{
3858 struct e100_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01003859 int retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003860 int allocated_resources = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Jiri Slaby410235f2012-03-05 14:52:01 +01003862 info = rs_table + tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (!info->enabled)
3864 return -ENODEV;
3865
3866#ifdef SERIAL_DEBUG_OPEN
3867 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003868 info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869#endif
3870
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003871 info->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003873 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003875 info->port.low_latency = !!(info->port.flags & ASYNC_LOW_LATENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 /*
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003878 * If DMA is enabled try to allocate the irq's.
3879 */
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003880 if (info->port.count == 1) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003881 allocated_resources = 1;
3882 if (info->dma_in_enabled) {
3883 if (request_irq(info->dma_in_irq_nbr,
3884 rec_interrupt,
3885 info->dma_in_irq_flags,
3886 info->dma_in_irq_description,
3887 info)) {
3888 printk(KERN_WARNING "DMA irq '%s' busy; "
3889 "falling back to non-DMA mode\n",
3890 info->dma_in_irq_description);
3891 /* Make sure we never try to use DMA in */
3892 /* for the port again. */
3893 info->dma_in_enabled = 0;
3894 } else if (cris_request_dma(info->dma_in_nbr,
3895 info->dma_in_irq_description,
3896 DMA_VERBOSE_ON_ERROR,
3897 info->dma_owner)) {
3898 free_irq(info->dma_in_irq_nbr, info);
3899 printk(KERN_WARNING "DMA '%s' busy; "
3900 "falling back to non-DMA mode\n",
3901 info->dma_in_irq_description);
3902 /* Make sure we never try to use DMA in */
3903 /* for the port again. */
3904 info->dma_in_enabled = 0;
3905 }
3906#ifdef SERIAL_DEBUG_OPEN
3907 else
3908 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
3909 info->dma_in_irq_description);
3910#endif
3911 }
3912 if (info->dma_out_enabled) {
3913 if (request_irq(info->dma_out_irq_nbr,
3914 tr_interrupt,
3915 info->dma_out_irq_flags,
3916 info->dma_out_irq_description,
3917 info)) {
3918 printk(KERN_WARNING "DMA irq '%s' busy; "
3919 "falling back to non-DMA mode\n",
3920 info->dma_out_irq_description);
3921 /* Make sure we never try to use DMA out */
3922 /* for the port again. */
3923 info->dma_out_enabled = 0;
3924 } else if (cris_request_dma(info->dma_out_nbr,
3925 info->dma_out_irq_description,
3926 DMA_VERBOSE_ON_ERROR,
3927 info->dma_owner)) {
3928 free_irq(info->dma_out_irq_nbr, info);
3929 printk(KERN_WARNING "DMA '%s' busy; "
3930 "falling back to non-DMA mode\n",
3931 info->dma_out_irq_description);
3932 /* Make sure we never try to use DMA out */
3933 /* for the port again. */
3934 info->dma_out_enabled = 0;
3935 }
3936#ifdef SERIAL_DEBUG_OPEN
3937 else
3938 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
3939 info->dma_out_irq_description);
3940#endif
3941 }
3942 }
3943
3944 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 * Start up the serial port
3946 */
3947
3948 retval = startup(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003949 if (retval) {
3950 if (allocated_resources)
3951 deinit_port(info);
3952
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003953 /* FIXME Decrease count info->port.count here too? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003955 }
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 retval = block_til_ready(tty, filp, info);
3959 if (retval) {
3960#ifdef SERIAL_DEBUG_OPEN
3961 printk("rs_open returning after block_til_ready with %d\n",
3962 retval);
3963#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003964 if (allocated_resources)
3965 deinit_port(info);
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return retval;
3968 }
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970#ifdef SERIAL_DEBUG_OPEN
3971 printk("rs_open ttyS%d successful...\n", info->line);
3972#endif
3973 DLOG_INT_TRIG( log_int_pos = 0);
3974
3975 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
3976 info->icount.rx = 0;
3977 } );
3978
3979 return 0;
3980}
3981
Jesper Nilsson9e040a32009-04-30 15:08:13 -07003982#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983/*
3984 * /proc fs routines....
3985 */
3986
Jesper Nilsson9e040a32009-04-30 15:08:13 -07003987static void seq_line_info(struct seq_file *m, struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 unsigned long tmp;
3990
Jesper Nilsson9e040a32009-04-30 15:08:13 -07003991 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
3992 info->line, (unsigned long)info->ioport, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Alan Coxd7283352008-08-04 17:21:18 +01003994 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07003995 seq_printf(m, "\n");
3996 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998
Jesper Nilsson9e040a32009-04-30 15:08:13 -07003999 seq_printf(m, " baud:%d", info->baud);
4000 seq_printf(m, " tx:%lu rx:%lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 (unsigned long)info->icount.tx,
4002 (unsigned long)info->icount.rx);
4003 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004004 if (tmp)
4005 seq_printf(m, " tx_pend:%lu/%lu",
4006 (unsigned long)tmp,
4007 (unsigned long)SERIAL_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004009 seq_printf(m, " rx_pend:%lu/%lu",
4010 (unsigned long)info->recv_cnt,
4011 (unsigned long)info->max_recv_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013#if 1
Takashi Iwaia88487c2008-07-16 21:54:42 +01004014 if (info->port.tty) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01004015 if (info->port.tty->stopped)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004016 seq_printf(m, " stopped:%i",
4017 (int)info->port.tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019
4020 {
Alan Coxd7283352008-08-04 17:21:18 +01004021 unsigned char rstat = info->ioport[REG_STATUS];
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004022 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4023 seq_printf(m, " xoff_detect:1");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025
4026#endif
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (info->icount.frame)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004029 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 if (info->icount.parity)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004032 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
4034 if (info->icount.brk)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004035 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037 if (info->icount.overrun)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004038 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 /*
4041 * Last thing is the RS-232 status lines
4042 */
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004043 if (!E100_RTS_GET(info))
4044 seq_puts(m, "|RTS");
4045 if (!E100_CTS_GET(info))
4046 seq_puts(m, "|CTS");
4047 if (!E100_DTR_GET(info))
4048 seq_puts(m, "|DTR");
4049 if (!E100_DSR_GET(info))
4050 seq_puts(m, "|DSR");
4051 if (!E100_CD_GET(info))
4052 seq_puts(m, "|CD");
4053 if (!E100_RI_GET(info))
4054 seq_puts(m, "|RI");
4055 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004059static int crisv10_proc_show(struct seq_file *m, void *v)
4060{
4061 int i;
4062
4063 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4064
4065 for (i = 0; i < NR_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 if (!rs_table[i].enabled)
4067 continue;
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004068 seq_line_info(m, &rs_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070#ifdef DEBUG_LOG_INCLUDED
4071 for (i = 0; i < debug_log_pos; i++) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004072 seq_printf(m, "%-4i %lu.%lu ",
4073 i, debug_log[i].time,
4074 timer_data_to_ns(debug_log[i].timer_data));
4075 seq_printf(m, debug_log[i].string, debug_log[i].value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004077 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 debug_log_pos = 0;
4079#endif
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004083static int crisv10_proc_open(struct inode *inode, struct file *file)
4084{
4085 return single_open(file, crisv10_proc_show, NULL);
4086}
4087
4088static const struct file_operations crisv10_proc_fops = {
4089 .owner = THIS_MODULE,
4090 .open = crisv10_proc_open,
4091 .read = seq_read,
4092 .llseek = seq_lseek,
4093 .release = single_release,
4094};
4095#endif
4096
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098/* Finally, routines used to initialize the serial driver. */
4099
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004100static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
4102 printk(KERN_INFO
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004103 "ETRAX 100LX serial-driver %s, "
4104 "(c) 2000-2004 Axis Communications AB\r\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 &serial_version[11]); /* "$Revision: x.yy" */
4106}
4107
4108/* rs_init inits the driver at boot (using the module_init chain) */
4109
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004110static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 .open = rs_open,
4112 .close = rs_close,
4113 .write = rs_write,
4114 .flush_chars = rs_flush_chars,
4115 .write_room = rs_write_room,
4116 .chars_in_buffer = rs_chars_in_buffer,
4117 .flush_buffer = rs_flush_buffer,
4118 .ioctl = rs_ioctl,
4119 .throttle = rs_throttle,
4120 .unthrottle = rs_unthrottle,
4121 .set_termios = rs_set_termios,
4122 .stop = rs_stop,
4123 .start = rs_start,
4124 .hangup = rs_hangup,
4125 .break_ctl = rs_break,
4126 .send_xchar = rs_send_xchar,
4127 .wait_until_sent = rs_wait_until_sent,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004128 .tiocmget = rs_tiocmget,
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004129 .tiocmset = rs_tiocmset,
4130#ifdef CONFIG_PROC_FS
4131 .proc_fops = &crisv10_proc_fops,
4132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133};
4134
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004135static int __init rs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
4137 int i;
4138 struct e100_serial *info;
4139 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4140
4141 if (!driver)
4142 return -ENOMEM;
4143
4144 show_serial_version();
4145
4146 /* Setup the timed flush handler system */
4147
4148#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004149 setup_timer(&flush_timer, timed_flush_handler, 0);
4150 mod_timer(&flush_timer, jiffies + 5);
4151#endif
4152
4153#if defined(CONFIG_ETRAX_RS485)
4154#if defined(CONFIG_ETRAX_RS485_ON_PA)
WANG Cong2f7861d2011-09-01 13:58:29 +08004155 if (cris_io_interface_allocate_pins(if_serial_0, 'a', rs485_pa_bit,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004156 rs485_pa_bit)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004157 printk(KERN_ERR "ETRAX100LX serial: Could not allocate "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004158 "RS485 pin\n");
Julia Lawall41ca7322008-10-13 10:31:37 +01004159 put_tty_driver(driver);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004160 return -EBUSY;
4161 }
4162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163#endif
4164
4165 /* Initialize the tty_driver structure */
4166
4167 driver->driver_name = "serial";
4168 driver->name = "ttyS";
4169 driver->major = TTY_MAJOR;
4170 driver->minor_start = 64;
4171 driver->type = TTY_DRIVER_TYPE_SERIAL;
4172 driver->subtype = SERIAL_TYPE_NORMAL;
4173 driver->init_termios = tty_std_termios;
4174 driver->init_termios.c_cflag =
4175 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
Alan Cox606d0992006-12-08 02:38:45 -08004176 driver->init_termios.c_ispeed = 115200;
4177 driver->init_termios.c_ospeed = 115200;
Jiri Slabyc4d6ebe2012-08-07 21:47:48 +02004178 driver->flags = TTY_DRIVER_REAL_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 tty_set_operations(driver, &rs_ops);
4181 serial_driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004183 /* do some initializing for the separate ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004185 if (info->enabled) {
4186 if (cris_request_io_interface(info->io_if,
4187 info->io_if_description)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004188 printk(KERN_ERR "ETRAX100LX async serial: "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004189 "Could not allocate IO pins for "
4190 "%s, port %d\n",
4191 info->io_if_description, i);
4192 info->enabled = 0;
4193 }
4194 }
Jiri Slaby953756e2012-04-02 13:53:46 +02004195 tty_port_init(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 info->uses_dma_in = 0;
4197 info->uses_dma_out = 0;
4198 info->line = i;
Takashi Iwaia88487c2008-07-16 21:54:42 +01004199 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 info->type = PORT_ETRAX;
4201 info->tr_running = 0;
4202 info->forced_eop = 0;
4203 info->baud_base = DEF_BAUD_BASE;
4204 info->custom_divisor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 info->x_char = 0;
4206 info->event = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 info->xmit.buf = NULL;
4208 info->xmit.tail = info->xmit.head = 0;
4209 info->first_recv_buffer = info->last_recv_buffer = NULL;
4210 info->recv_cnt = info->max_recv_cnt = 0;
4211 info->last_tx_active_usec = 0;
4212 info->last_tx_active = 0;
4213
4214#if defined(CONFIG_ETRAX_RS485)
4215 /* Set sane defaults */
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004216 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4217 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 info->rs485.delay_rts_before_send = 0;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004219 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004221 INIT_WORK(&info->work, do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
4223 if (info->enabled) {
Kulikov Vasiliy078dee22010-07-14 22:04:42 +04004224 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4225 serial_driver->name, info->line, info->ioport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004227 tty_port_link_device(&info->port, driver, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004229
4230 if (tty_register_driver(driver))
4231 panic("Couldn't register serial driver\n");
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233#ifdef CONFIG_ETRAX_FAST_TIMER
4234#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4235 memset(fast_timers, 0, sizeof(fast_timers));
4236#endif
4237#ifdef CONFIG_ETRAX_RS485
4238 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4239#endif
4240 fast_timer_init();
4241#endif
4242
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004243#ifndef CONFIG_ETRAX_KGDB
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 /* Not needed in simulator. May only complicate stuff. */
4245 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4246
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004247 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08004248 IRQF_SHARED, "serial ", driver))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07004249 panic("%s: Failed to request irq8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return 0;
4254}
4255
4256/* this makes sure that rs_init is called during kernel boot */
4257
4258module_init(rs_init);