blob: 7dced3a3f96b79b2301945544b7c9e6db012439c [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
1638 if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
1639 return NULL;
1640
1641 buffer->next = NULL;
1642 buffer->length = 0;
1643 buffer->error = TTY_NORMAL;
1644
1645 return buffer;
1646}
1647
1648static void
1649append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1650{
1651 unsigned long flags;
1652
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001653 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (!info->first_recv_buffer)
1656 info->first_recv_buffer = buffer;
1657 else
1658 info->last_recv_buffer->next = buffer;
1659
1660 info->last_recv_buffer = buffer;
1661
1662 info->recv_cnt += buffer->length;
1663 if (info->recv_cnt > info->max_recv_cnt)
1664 info->max_recv_cnt = info->recv_cnt;
1665
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001666 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669static int
1670add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1671{
1672 struct etrax_recv_buffer *buffer;
1673 if (info->uses_dma_in) {
1674 if (!(buffer = alloc_recv_buffer(4)))
1675 return 0;
1676
1677 buffer->length = 1;
1678 buffer->error = flag;
1679 buffer->buffer[0] = data;
1680
1681 append_recv_buffer(info, buffer);
1682
1683 info->icount.rx++;
1684 } else {
Jiri Slaby92a19f92013-01-03 15:53:03 +01001685 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 info->icount.rx++;
1687 }
1688
1689 return 1;
1690}
1691
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001692static unsigned int handle_descr_data(struct e100_serial *info,
1693 struct etrax_dma_descr *descr,
1694 unsigned int recvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1697
1698 if (info->recv_cnt + recvl > 65536) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08001699 printk(KERN_WARNING
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001700 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702 }
1703
1704 buffer->length = recvl;
1705
1706 if (info->errorcode == ERRCODE_SET_BREAK)
1707 buffer->error = TTY_BREAK;
1708 info->errorcode = 0;
1709
1710 append_recv_buffer(info, buffer);
1711
1712 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001713 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 descr->buf = virt_to_phys(buffer->buffer);
1716
1717 return recvl;
1718}
1719
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001720static unsigned int handle_all_descr_data(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct etrax_dma_descr *descr;
1723 unsigned int recvl;
1724 unsigned int ret = 0;
1725
1726 while (1)
1727 {
1728 descr = &info->rec_descr[info->cur_rec_descr];
1729
1730 if (descr == phys_to_virt(*info->idescradr))
1731 break;
1732
1733 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1734 info->cur_rec_descr = 0;
1735
1736 /* find out how many bytes were read */
1737
1738 /* if the eop bit was not set, all data has been received */
1739 if (!(descr->status & d_eop)) {
1740 recvl = descr->sw_len;
1741 } else {
1742 /* otherwise we find the amount of data received here */
1743 recvl = descr->hw_len;
1744 }
1745
1746 /* Reset the status information */
1747 descr->status = 0;
1748
1749 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
Takashi Iwaia88487c2008-07-16 21:54:42 +01001750 if (info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 unsigned char *buf = phys_to_virt(descr->buf);
1752 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
1753 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
1754 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
1755 }
1756 );
1757
1758 /* update stats */
1759 info->icount.rx += recvl;
1760
1761 ret += handle_descr_data(info, descr, recvl);
1762 }
1763
1764 return ret;
1765}
1766
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001767static void receive_chars_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 struct tty_struct *tty;
1770 unsigned char rstat;
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1773 *info->iclrintradr =
1774 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1775 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1776
Takashi Iwaia88487c2008-07-16 21:54:42 +01001777 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (!tty) /* Something wrong... */
1779 return;
1780
1781#ifdef SERIAL_HANDLE_EARLY_ERRORS
1782 if (info->uses_dma_in)
1783 e100_enable_serial_data_irq(info);
1784#endif
1785
1786 if (info->errorcode == ERRCODE_INSERT_BREAK)
1787 add_char_and_flag(info, '\0', TTY_BREAK);
1788
1789 handle_all_descr_data(info);
1790
1791 /* Read the status register to detect errors */
Alan Coxd7283352008-08-04 17:21:18 +01001792 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
1794 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
1795 }
1796
1797 if (rstat & SER_ERROR_MASK) {
1798 /* If we got an error, we must reset it by reading the
1799 * data_in field
1800 */
Alan Coxd7283352008-08-04 17:21:18 +01001801 unsigned char data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1804 ((rstat & SER_ERROR_MASK) << 8) | data);
1805
1806 if (rstat & SER_PAR_ERR_MASK)
1807 add_char_and_flag(info, data, TTY_PARITY);
1808 else if (rstat & SER_OVERRUN_MASK)
1809 add_char_and_flag(info, data, TTY_OVERRUN);
1810 else if (rstat & SER_FRAMING_ERR_MASK)
1811 add_char_and_flag(info, data, TTY_FRAME);
1812 }
1813
1814 START_FLUSH_FAST_TIMER(info, "receive_chars");
1815
1816 /* Restart the receiving DMA */
1817 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1818}
1819
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001820static int start_recv_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 struct etrax_dma_descr *descr = info->rec_descr;
1823 struct etrax_recv_buffer *buffer;
1824 int i;
1825
1826 /* Set up the receiving descriptors */
1827 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
1828 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001829 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 descr[i].ctrl = d_int;
1832 descr[i].buf = virt_to_phys(buffer->buffer);
1833 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1834 descr[i].hw_len = 0;
1835 descr[i].status = 0;
1836 descr[i].next = virt_to_phys(&descr[i+1]);
1837 }
1838
1839 /* Link the last descriptor to the first */
1840 descr[i-1].next = virt_to_phys(&descr[0]);
1841
1842 /* Start with the first descriptor in the list */
1843 info->cur_rec_descr = 0;
1844
1845 /* Start the DMA */
1846 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1847 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1848
1849 /* Input DMA should be running now */
1850 return 1;
1851}
1852
1853static void
1854start_receive(struct e100_serial *info)
1855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (info->uses_dma_in) {
1857 /* reset the input dma channel to be sure it works */
1858
1859 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1860 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1861 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1862
1863 start_recv_dma(info);
1864 }
1865}
1866
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/* the bits in the MASK2 register are laid out like this:
1869 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1870 where I is the input channel and O is the output channel for the port.
1871 info->irq is the bit number for the DMAO_DESCR so to check the others we
1872 shift info->irq to the left.
1873*/
1874
1875/* dma output channel interrupt handler
1876 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1877 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1878*/
1879
1880static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001881tr_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 struct e100_serial *info;
1884 unsigned long ireg;
1885 int i;
1886 int handled = 0;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* find out the line that caused this irq and get it from rs_table */
1889
1890 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1891
1892 for (i = 0; i < NR_PORTS; i++) {
1893 info = rs_table + i;
1894 if (!info->enabled || !info->uses_dma_out)
1895 continue;
1896 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
1897 if (ireg & info->irq) {
1898 handled = 1;
1899 /* we can send a new dma bunch. make it so. */
1900 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
1901 /* Read jiffies_usec first,
1902 * we want this time to be as late as possible
1903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 info->last_tx_active_usec = GET_JIFFIES_USEC();
1905 info->last_tx_active = jiffies;
1906 transmit_chars_dma(info);
1907 }
1908
1909 /* FIXME: here we should really check for a change in the
1910 status lines and if so call status_handle(info) */
1911 }
1912 return IRQ_RETVAL(handled);
1913} /* tr_interrupt */
1914
1915/* dma input channel interrupt handler */
1916
1917static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001918rec_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 struct e100_serial *info;
1921 unsigned long ireg;
1922 int i;
1923 int handled = 0;
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* find out the line that caused this irq and get it from rs_table */
1926
1927 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1928
1929 for (i = 0; i < NR_PORTS; i++) {
1930 info = rs_table + i;
1931 if (!info->enabled || !info->uses_dma_in)
1932 continue;
1933 /* check for both dma_eop and dma_descr for the input dma channel */
1934 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
1935 handled = 1;
1936 /* we have received something */
1937 receive_chars_dma(info);
1938 }
1939
1940 /* FIXME: here we should really check for a change in the
1941 status lines and if so call status_handle(info) */
1942 }
1943 return IRQ_RETVAL(handled);
1944} /* rec_interrupt */
1945
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001946static int force_eop_if_needed(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 /* We check data_avail bit to determine if data has
1949 * arrived since last time
1950 */
Alan Coxd7283352008-08-04 17:21:18 +01001951 unsigned char rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* error or datavail? */
1954 if (rstat & SER_ERROR_MASK) {
1955 /* Some error has occurred. If there has been valid data, an
1956 * EOP interrupt will be made automatically. If no data, the
1957 * normal ser_interrupt should be enabled and handle it.
1958 * So do nothing!
1959 */
1960 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
1961 rstat | (info->line << 8));
1962 return 0;
1963 }
1964
1965 if (rstat & SER_DATA_AVAIL_MASK) {
1966 /* Ok data, no error, count it */
1967 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
1968 rstat | (info->line << 8)));
1969 /* Read data to clear status flags */
Alan Coxd7283352008-08-04 17:21:18 +01001970 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 info->forced_eop = 0;
1973 START_FLUSH_FAST_TIMER(info, "magic");
1974 return 0;
1975 }
1976
1977 /* hit the timeout, force an EOP for the input
1978 * dma channel if we haven't already
1979 */
1980 if (!info->forced_eop) {
1981 info->forced_eop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
1983 FORCE_EOP(info);
1984 }
1985
1986 return 1;
1987}
1988
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001989static void flush_to_flip_buffer(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct etrax_recv_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001994 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Alan Cox2090ab02007-10-16 01:26:38 -07001996 while ((buffer = info->first_recv_buffer) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 unsigned int count = buffer->length;
1998
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001999 tty_insert_flip_string(&info->port, buffer->buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 info->recv_cnt -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 if (count == buffer->length) {
2003 info->first_recv_buffer = buffer->next;
2004 kfree(buffer);
2005 } else {
2006 buffer->length -= count;
2007 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2008 buffer->error = TTY_NORMAL;
2009 }
2010 }
2011
2012 if (!info->first_recv_buffer)
2013 info->last_recv_buffer = NULL;
2014
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002015 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002017 /* This includes a check for low-latency */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002018 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002021static void check_flush_timeout(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 /* Flip what we've got (if we can) */
2024 flush_to_flip_buffer(info);
2025
2026 /* We might need to flip later, but not to fast
2027 * since the system is busy processing input... */
2028 if (info->first_recv_buffer)
2029 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2030
2031 /* Force eop last, since data might have come while we're processing
2032 * and if we started the slow timer above, we won't start a fast
2033 * below.
2034 */
2035 force_eop_if_needed(info);
2036}
2037
2038#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2039static void flush_timeout_function(unsigned long data)
2040{
2041 struct e100_serial *info = (struct e100_serial *)data;
2042
2043 fast_timers[info->line].function = NULL;
2044 serial_fast_timer_expired++;
Masanari Iida8faaaea2014-01-07 21:58:06 +09002045 TIMERD(DEBUG_LOG(info->line, "flush_timeout %i ", info->line));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2047 check_flush_timeout(info);
2048}
2049
2050#else
2051
2052/* dma fifo/buffer timeout handler
2053 forces an end-of-packet for the dma input channel if no chars
2054 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2055*/
2056
2057static struct timer_list flush_timer;
2058
2059static void
2060timed_flush_handler(unsigned long ptr)
2061{
2062 struct e100_serial *info;
2063 int i;
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (i = 0; i < NR_PORTS; i++) {
2066 info = rs_table + i;
2067 if (info->uses_dma_in)
2068 check_flush_timeout(info);
2069 }
2070
2071 /* restart flush timer */
2072 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2073}
2074#endif
2075
2076#ifdef SERIAL_HANDLE_EARLY_ERRORS
2077
2078/* If there is an error (ie break) when the DMA is running and
2079 * there are no bytes in the fifo the DMA is stopped and we get no
2080 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2081 * transfer, and if it is without error we can turn the serial
2082 * interrupts off.
2083 */
2084
2085/*
2086BREAK handling on ETRAX 100:
2087ETRAX will generate interrupt although there is no stop bit between the
2088characters.
2089
2090Depending on how long the break sequence is, the end of the breaksequence
2091will look differently:
2092| indicates start/end of a character.
2093
2094B= Break character (0x00) with framing error.
2095E= Error byte with parity error received after B characters.
2096F= "Faked" valid byte received immediately after B characters.
2097V= Valid byte
2098
20991.
2100 B BL ___________________________ V
2101.._|__________|__________| |valid data |
2102
2103Multiple frame errors with data == 0x00 (B),
2104the timing matches up "perfectly" so no extra ending char is detected.
2105The RXD pin is 1 in the last interrupt, in that case
2106we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2107know if another byte will come and this really is case 2. below
2108(e.g F=0xFF or 0xFE)
2109If RXD pin is 0 we can expect another character (see 2. below).
2110
2111
21122.
2113
2114 B B E or F__________________..__ V
2115.._|__________|__________|______ | |valid data
2116 "valid" or
2117 parity error
2118
2119Multiple frame errors with data == 0x00 (B),
2120but the part of the break trigs is interpreted as a start bit (and possibly
2121some 0 bits followed by a number of 1 bits and a stop bit).
2122Depending on parity settings etc. this last character can be either
2123a fake "valid" char (F) or have a parity error (E).
2124
2125If the character is valid it will be put in the buffer,
2126we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2127will set the flags so the tty will handle it,
2128if it's an error byte it will not be put in the buffer
2129and we set info->errorcode = ERRCODE_INSERT_BREAK.
2130
2131To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2132of the last faulty char (B) and compares it with the current time:
2133If the time elapsed time is less then 2*char_time_usec we will assume
2134it's a faked F char and not a Valid char and set
2135info->errorcode = ERRCODE_SET_BREAK.
2136
2137Flaws in the above solution:
2138~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2139We use the timer to distinguish a F character from a V character,
2140if a V character is to close after the break we might make the wrong decision.
2141
2142TODO: The break will be delayed until an F or V character is received.
2143
2144*/
2145
Jiri Slaby12aad552013-03-07 13:12:35 +01002146static void handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 unsigned long data_read;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* Read data and status at the same time */
Alan Coxd7283352008-08-04 17:21:18 +01002151 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152more_data:
2153 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2154 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2155 }
2156 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2157
2158 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2159 IO_MASK(R_SERIAL0_READ, par_err) |
2160 IO_MASK(R_SERIAL0_READ, overrun) )) {
2161 /* An error */
2162 info->last_rx_active_usec = GET_JIFFIES_USEC();
2163 info->last_rx_active = jiffies;
2164 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2165 DLOG_INT_TRIG(
2166 if (!log_int_trig1_pos) {
2167 log_int_trig1_pos = log_int_pos;
2168 log_int(rdpc(), 0, 0);
2169 }
2170 );
2171
2172
2173 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2174 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2175 /* Most likely a break, but we get interrupts over and
2176 * over again.
2177 */
2178
2179 if (!info->break_detected_cnt) {
2180 DEBUG_LOG(info->line, "#BRK start\n", 0);
2181 }
2182 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2183 /* The RX pin is high now, so the break
2184 * must be over, but....
2185 * we can't really know if we will get another
2186 * last byte ending the break or not.
2187 * And we don't know if the byte (if any) will
2188 * have an error or look valid.
2189 */
2190 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2191 info->errorcode = ERRCODE_INSERT_BREAK;
2192 }
2193 info->break_detected_cnt++;
2194 } else {
2195 /* The error does not look like a break, but could be
2196 * the end of one
2197 */
2198 if (info->break_detected_cnt) {
2199 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2200 info->errorcode = ERRCODE_INSERT_BREAK;
2201 } else {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002202 unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
2203 data_in, data_read);
2204 char flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (info->errorcode == ERRCODE_INSERT_BREAK) {
Jiri Slaby92a19f92013-01-03 15:53:03 +01002206 tty_insert_flip_char(&info->port, 0, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 info->icount.rx++;
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2211 info->icount.parity++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002212 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2214 info->icount.overrun++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002215 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2217 info->icount.frame++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002218 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01002220 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 info->errorcode = 0;
2222 }
2223 info->break_detected_cnt = 0;
2224 }
2225 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2226 /* No error */
2227 DLOG_INT_TRIG(
2228 if (!log_int_trig1_pos) {
2229 if (log_int_pos >= log_int_size) {
2230 log_int_pos = 0;
2231 }
2232 log_int_trig0_pos = log_int_pos;
2233 log_int(rdpc(), 0, 0);
2234 }
2235 );
Jiri Slaby92a19f92013-01-03 15:53:03 +01002236 tty_insert_flip_char(&info->port,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002237 IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
2238 TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 } else {
2240 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2241 }
2242
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 info->icount.rx++;
Alan Coxd7283352008-08-04 17:21:18 +01002245 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2247 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2248 goto more_data;
2249 }
2250
Jiri Slaby2e124b42013-01-03 15:53:06 +01002251 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Jiri Slaby12aad552013-03-07 13:12:35 +01002254static void handle_ser_rx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 unsigned char rstat;
2257
2258#ifdef SERIAL_DEBUG_INTR
2259 printk("Interrupt from serport %d\n", i);
2260#endif
2261/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2262 if (!info->uses_dma_in) {
Jiri Slaby12aad552013-03-07 13:12:35 +01002263 handle_ser_rx_interrupt_no_dma(info);
2264 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 /* DMA is used */
Alan Coxd7283352008-08-04 17:21:18 +01002267 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2269 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2270 }
2271
2272 if (rstat & SER_ERROR_MASK) {
2273 unsigned char data;
2274
2275 info->last_rx_active_usec = GET_JIFFIES_USEC();
2276 info->last_rx_active = jiffies;
2277 /* If we got an error, we must reset it by reading the
2278 * data_in field
2279 */
Alan Coxd7283352008-08-04 17:21:18 +01002280 data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2282 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2283 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2284 /* Most likely a break, but we get interrupts over and
2285 * over again.
2286 */
2287
2288 if (!info->break_detected_cnt) {
2289 DEBUG_LOG(info->line, "#BRK start\n", 0);
2290 }
2291 if (rstat & SER_RXD_MASK) {
2292 /* The RX pin is high now, so the break
2293 * must be over, but....
2294 * we can't really know if we will get another
2295 * last byte ending the break or not.
2296 * And we don't know if the byte (if any) will
2297 * have an error or look valid.
2298 */
2299 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2300 info->errorcode = ERRCODE_INSERT_BREAK;
2301 }
2302 info->break_detected_cnt++;
2303 } else {
2304 /* The error does not look like a break, but could be
2305 * the end of one
2306 */
2307 if (info->break_detected_cnt) {
2308 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2309 info->errorcode = ERRCODE_INSERT_BREAK;
2310 } else {
2311 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2312 info->icount.brk++;
2313 add_char_and_flag(info, '\0', TTY_BREAK);
2314 }
2315
2316 if (rstat & SER_PAR_ERR_MASK) {
2317 info->icount.parity++;
2318 add_char_and_flag(info, data, TTY_PARITY);
2319 } else if (rstat & SER_OVERRUN_MASK) {
2320 info->icount.overrun++;
2321 add_char_and_flag(info, data, TTY_OVERRUN);
2322 } else if (rstat & SER_FRAMING_ERR_MASK) {
2323 info->icount.frame++;
2324 add_char_and_flag(info, data, TTY_FRAME);
2325 }
2326
2327 info->errorcode = 0;
2328 }
2329 info->break_detected_cnt = 0;
2330 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2331 ((rstat & SER_ERROR_MASK) << 8) | data);
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 } else { /* It was a valid byte, now let the DMA do the rest */
2334 unsigned long curr_time_u = GET_JIFFIES_USEC();
2335 unsigned long curr_time = jiffies;
2336
2337 if (info->break_detected_cnt) {
2338 /* Detect if this character is a new valid char or the
2339 * last char in a break sequence: If LSBits are 0 and
2340 * MSBits are high AND the time is close to the
2341 * previous interrupt we should discard it.
2342 */
2343 long elapsed_usec =
2344 (curr_time - info->last_rx_active) * (1000000/HZ) +
2345 curr_time_u - info->last_rx_active_usec;
2346 if (elapsed_usec < 2*info->char_time_usec) {
2347 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2348 /* Report as BREAK (error) and let
2349 * receive_chars_dma() handle it
2350 */
2351 info->errorcode = ERRCODE_SET_BREAK;
2352 } else {
2353 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2354 }
2355 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2356 }
2357
2358#ifdef SERIAL_DEBUG_INTR
2359 printk("** OK, disabling ser_interrupts\n");
2360#endif
2361 e100_disable_serial_data_irq(info);
2362 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2363 info->break_detected_cnt = 0;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 /* Restarting the DMA never hurts */
2367 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2368 START_FLUSH_FAST_TIMER(info, "ser_int");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369} /* handle_ser_rx_interrupt */
2370
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002371static void handle_ser_tx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 unsigned long flags;
2374
2375 if (info->x_char) {
2376 unsigned char rstat;
2377 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002378 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002379 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2381
Alan Coxd7283352008-08-04 17:21:18 +01002382 info->ioport[REG_TR_DATA] = info->x_char;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 info->icount.tx++;
2384 info->x_char = 0;
2385 /* We must enable since it is disabled in ser_interrupt */
2386 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002387 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return;
2389 }
2390 if (info->uses_dma_out) {
2391 unsigned char rstat;
2392 int i;
2393 /* We only use normal tx interrupt when sending x_char */
2394 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002395 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002396 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2398 e100_disable_serial_tx_ready_irq(info);
Takashi Iwaia88487c2008-07-16 21:54:42 +01002399 if (info->port.tty->stopped)
2400 rs_stop(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* Enable the DMA channel and tell it to continue */
2402 e100_enable_txdma_channel(info);
2403 /* Wait 12 cycles before doing the DMA command */
2404 for(i = 6; i > 0; i--)
2405 nop();
2406
2407 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002408 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return;
2410 }
2411 /* Normal char-by-char interrupt */
2412 if (info->xmit.head == info->xmit.tail
Jiri Slabyee797062013-03-07 13:12:34 +01002413 || info->port.tty->stopped) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01002414 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
2415 info->port.tty->stopped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 e100_disable_serial_tx_ready_irq(info);
2417 info->tr_running = 0;
2418 return;
2419 }
2420 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
2421 /* Send a byte, rs485 timing is critical so turn of ints */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002422 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002423 info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
2425 info->icount.tx++;
2426 if (info->xmit.head == info->xmit.tail) {
2427#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01002428 if (info->rs485.flags & SER_RS485_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* Set a short timer to toggle RTS */
2430 start_one_shot_timer(&fast_timers_rs485[info->line],
2431 rs485_toggle_rts_timer_function,
2432 (unsigned long)info,
2433 info->char_time_usec*2,
2434 "RS-485");
2435 }
2436#endif /* RS485 */
2437 info->last_tx_active_usec = GET_JIFFIES_USEC();
2438 info->last_tx_active = jiffies;
2439 e100_disable_serial_tx_ready_irq(info);
2440 info->tr_running = 0;
2441 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
2442 } else {
2443 /* We must enable since it is disabled in ser_interrupt */
2444 e100_enable_serial_tx_ready_irq(info);
2445 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002446 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 if (CIRC_CNT(info->xmit.head,
2449 info->xmit.tail,
2450 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2451 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2452
2453} /* handle_ser_tx_interrupt */
2454
2455/* result of time measurements:
2456 * RX duration 54-60 us when doing something, otherwise 6-9 us
2457 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2458 */
2459static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01002460ser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462 static volatile int tx_started = 0;
2463 struct e100_serial *info;
2464 int i;
2465 unsigned long flags;
2466 unsigned long irq_mask1_rd;
2467 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
2468 int handled = 0;
2469 static volatile unsigned long reentered_ready_mask = 0;
2470
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002471 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 irq_mask1_rd = *R_IRQ_MASK1_RD;
2473 /* First handle all rx interrupts with ints disabled */
2474 info = rs_table;
2475 irq_mask1_rd &= e100_ser_int_mask;
2476 for (i = 0; i < NR_PORTS; i++) {
2477 /* Which line caused the data irq? */
2478 if (irq_mask1_rd & data_mask) {
2479 handled = 1;
2480 handle_ser_rx_interrupt(info);
2481 }
2482 info += 1;
2483 data_mask <<= 2;
2484 }
2485 /* Handle tx interrupts with interrupts enabled so we
2486 * can take care of new data interrupts while transmitting
2487 * We protect the tx part with the tx_started flag.
2488 * We disable the tr_ready interrupts we are about to handle and
2489 * unblock the serial interrupt so new serial interrupts may come.
2490 *
2491 * If we get a new interrupt:
2492 * - it migth be due to synchronous serial ports.
2493 * - serial irq will be blocked by general irq handler.
2494 * - async data will be handled above (sync will be ignored).
2495 * - tx_started flag will prevent us from trying to send again and
2496 * we will exit fast - no need to unblock serial irq.
2497 * - Next (sync) serial interrupt handler will be runned with
2498 * disabled interrupt due to restore_flags() at end of function,
2499 * so sync handler will not be preempted or reentered.
2500 */
2501 if (!tx_started) {
2502 unsigned long ready_mask;
2503 unsigned long
2504 tx_started = 1;
2505 /* Only the tr_ready interrupts left */
2506 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2507 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2508 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2509 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2510 while (irq_mask1_rd) {
2511 /* Disable those we are about to handle */
2512 *R_IRQ_MASK1_CLR = irq_mask1_rd;
2513 /* Unblock the serial interrupt */
2514 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
2515
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002516 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
2518 info = rs_table;
2519 for (i = 0; i < NR_PORTS; i++) {
2520 /* Which line caused the ready irq? */
2521 if (irq_mask1_rd & ready_mask) {
2522 handled = 1;
2523 handle_ser_tx_interrupt(info);
2524 }
2525 info += 1;
2526 ready_mask <<= 2;
2527 }
2528 /* handle_ser_tx_interrupt enables tr_ready interrupts */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002529 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* Handle reentered TX interrupt */
2531 irq_mask1_rd = reentered_ready_mask;
2532 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002533 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 tx_started = 0;
2535 } else {
2536 unsigned long ready_mask;
2537 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2538 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2539 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2540 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2541 if (ready_mask) {
2542 reentered_ready_mask |= ready_mask;
2543 /* Disable those we are about to handle */
2544 *R_IRQ_MASK1_CLR = ready_mask;
2545 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
2546 }
2547 }
2548
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002549 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return IRQ_RETVAL(handled);
2551} /* ser_interrupt */
2552#endif
2553
2554/*
2555 * -------------------------------------------------------------------
2556 * Here ends the serial interrupt routines.
2557 * -------------------------------------------------------------------
2558 */
2559
2560/*
2561 * This routine is used to handle the "bottom half" processing for the
2562 * serial driver, known also the "software interrupt" processing.
2563 * This processing is done at the kernel interrupt level, after the
2564 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2565 * is where time-consuming activities which can not be done in the
2566 * interrupt driver proper are done; the interrupt driver schedules
2567 * them using rs_sched_event(), and they get done here.
2568 */
2569static void
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002570do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002572 struct e100_serial *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct tty_struct *tty;
2574
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002575 info = container_of(work, struct e100_serial, work);
2576
Takashi Iwaia88487c2008-07-16 21:54:42 +01002577 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (!tty)
2579 return;
2580
Jiri Slabyb963a842007-02-10 01:44:55 -08002581 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
2582 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585static int
2586startup(struct e100_serial * info)
2587{
2588 unsigned long flags;
2589 unsigned long xmit_page;
2590 int i;
2591
2592 xmit_page = get_zeroed_page(GFP_KERNEL);
2593 if (!xmit_page)
2594 return -ENOMEM;
2595
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002596 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /* if it was already initialized, skip this */
2599
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002600 if (info->port.flags & ASYNC_INITIALIZED) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002601 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 free_page(xmit_page);
2603 return 0;
2604 }
2605
2606 if (info->xmit.buf)
2607 free_page(xmit_page);
2608 else
2609 info->xmit.buf = (unsigned char *) xmit_page;
2610
2611#ifdef SERIAL_DEBUG_OPEN
2612 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
2613#endif
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /*
2616 * Clear the FIFO buffers and disable them
2617 * (they will be reenabled in change_speed())
2618 */
2619
2620 /*
2621 * Reset the DMA channels and make sure their interrupts are cleared
2622 */
2623
2624 if (info->dma_in_enabled) {
2625 info->uses_dma_in = 1;
2626 e100_enable_rxdma_channel(info);
2627
2628 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2629
2630 /* Wait until reset cycle is complete */
2631 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2632 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2633
2634 /* Make sure the irqs are cleared */
2635 *info->iclrintradr =
2636 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2637 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2638 } else {
2639 e100_disable_rxdma_channel(info);
2640 }
2641
2642 if (info->dma_out_enabled) {
2643 info->uses_dma_out = 1;
2644 e100_enable_txdma_channel(info);
2645 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2646
2647 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2648 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2649
2650 /* Make sure the irqs are cleared */
2651 *info->oclrintradr =
2652 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2653 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2654 } else {
2655 e100_disable_txdma_channel(info);
2656 }
2657
Takashi Iwaia88487c2008-07-16 21:54:42 +01002658 if (info->port.tty)
2659 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 info->xmit.head = info->xmit.tail = 0;
2662 info->first_recv_buffer = info->last_recv_buffer = NULL;
2663 info->recv_cnt = info->max_recv_cnt = 0;
2664
2665 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2666 info->rec_descr[i].buf = 0;
2667
2668 /*
2669 * and set the speed and other flags of the serial port
2670 * this will start the rx/tx as well
2671 */
2672#ifdef SERIAL_HANDLE_EARLY_ERRORS
2673 e100_enable_serial_data_irq(info);
2674#endif
2675 change_speed(info);
2676
2677 /* dummy read to reset any serial errors */
2678
Alan Coxd7283352008-08-04 17:21:18 +01002679 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 /* enable the interrupts */
2682 if (info->uses_dma_out)
2683 e100_enable_txdma_irq(info);
2684
2685 e100_enable_rx_irq(info);
2686
2687 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2688
2689 /* setup the dma input descriptor and start dma */
2690
2691 start_receive(info);
2692
2693 /* for safety, make sure the descriptors last result is 0 bytes written */
2694
2695 info->tr_descr.sw_len = 0;
2696 info->tr_descr.hw_len = 0;
2697 info->tr_descr.status = 0;
2698
2699 /* enable RTS/DTR last */
2700
2701 e100_rts(info, 1);
2702 e100_dtr(info, 1);
2703
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002704 info->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002706 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return 0;
2708}
2709
2710/*
2711 * This routine will shutdown a serial port; interrupts are disabled, and
2712 * DTR is dropped if the hangup on close termio flag is on.
2713 */
2714static void
2715shutdown(struct e100_serial * info)
2716{
2717 unsigned long flags;
2718 struct etrax_dma_descr *descr = info->rec_descr;
2719 struct etrax_recv_buffer *buffer;
2720 int i;
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /* shut down the transmitter and receiver */
2723 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
2724 e100_disable_rx(info);
Alan Coxd7283352008-08-04 17:21:18 +01002725 info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 /* disable interrupts, reset dma channels */
2728 if (info->uses_dma_in) {
2729 e100_disable_rxdma_irq(info);
2730 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2731 info->uses_dma_in = 0;
2732 } else {
2733 e100_disable_serial_data_irq(info);
2734 }
2735
2736 if (info->uses_dma_out) {
2737 e100_disable_txdma_irq(info);
2738 info->tr_running = 0;
2739 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2740 info->uses_dma_out = 0;
2741 } else {
2742 e100_disable_serial_tx_ready_irq(info);
2743 info->tr_running = 0;
2744 }
2745
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002746 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return;
2748
2749#ifdef SERIAL_DEBUG_OPEN
2750 printk("Shutting down serial port %d (irq %d)....\n", info->line,
2751 info->irq);
2752#endif
2753
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002754 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 if (info->xmit.buf) {
2757 free_page((unsigned long)info->xmit.buf);
2758 info->xmit.buf = NULL;
2759 }
2760
2761 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2762 if (descr[i].buf) {
2763 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2764 kfree(buffer);
2765 descr[i].buf = 0;
2766 }
2767
Alan Coxadc8d742012-07-14 15:31:47 +01002768 if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /* hang up DTR and RTS if HUPCL is enabled */
2770 e100_dtr(info, 0);
2771 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2772 }
2773
Takashi Iwaia88487c2008-07-16 21:54:42 +01002774 if (info->port.tty)
2775 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002777 info->port.flags &= ~ASYNC_INITIALIZED;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002778 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781
2782/* change baud rate and other assorted parameters */
2783
2784static void
2785change_speed(struct e100_serial *info)
2786{
2787 unsigned int cflag;
2788 unsigned long xoff;
2789 unsigned long flags;
2790 /* first some safety checks */
2791
Alan Coxadc8d742012-07-14 15:31:47 +01002792 if (!info->port.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return;
Alan Coxd7283352008-08-04 17:21:18 +01002794 if (!info->ioport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return;
2796
Alan Coxadc8d742012-07-14 15:31:47 +01002797 cflag = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 /* possibly, the tx/rx should be disabled first to do this safely */
2800
2801 /* change baud-rate and write it to the hardware */
Jiri Slabyb1d984c2013-03-07 13:12:36 +01002802 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* Special baudrate */
2804 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2805 unsigned long alt_source =
2806 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2807 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2808 /* R_ALT_SER_BAUDRATE selects the source */
2809 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2810 (unsigned long)info->baud_base, info->custom_divisor));
2811 if (info->baud_base == SERIAL_PRESCALE_BASE) {
2812 /* 0, 2-65535 (0=65536) */
2813 u16 divisor = info->custom_divisor;
2814 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2815 /* baudrate is 3.125MHz/custom_divisor */
2816 alt_source =
2817 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
2818 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
2819 alt_source = 0x11;
2820 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
2821 *R_SERIAL_PRESCALE = divisor;
2822 info->baud = SERIAL_PRESCALE_BASE/divisor;
2823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 else
2825 {
2826 /* Bad baudbase, we don't support using timer0
2827 * for baudrate.
2828 */
2829 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
2830 (unsigned long)info->baud_base, info->custom_divisor);
2831 }
2832 r_alt_ser_baudrate_shadow &= ~mask;
2833 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
2834 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
2835 } else {
2836 /* Normal baudrate */
2837 /* Make sure we use normal baudrate */
2838 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2839 unsigned long alt_source =
2840 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2841 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2842 r_alt_ser_baudrate_shadow &= ~mask;
2843 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 info->baud = cflag_to_baud(cflag);
Alan Coxd7283352008-08-04 17:21:18 +01002847 info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* start with default settings and then fill in changes */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002851 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /* 8 bit, no/even parity */
2853 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
2854 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
2855 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
2856
2857 /* 8 bit, no/even parity, 1 stop bit, no cts */
2858 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
2859 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
2860 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
2861 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
2862 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
2863
2864 if ((cflag & CSIZE) == CS7) {
2865 /* set 7 bit mode */
2866 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
2867 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
2868 }
2869
2870 if (cflag & CSTOPB) {
2871 /* set 2 stop bit mode */
2872 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
2873 }
2874
2875 if (cflag & PARENB) {
2876 /* enable parity */
2877 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
2878 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
2879 }
2880
2881 if (cflag & CMSPAR) {
2882 /* enable stick parity, PARODD mean Mark which matches ETRAX */
2883 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
2884 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
2885 }
2886 if (cflag & PARODD) {
2887 /* set odd parity (or Mark if CMSPAR) */
2888 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
2889 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
2890 }
2891
2892 if (cflag & CRTSCTS) {
2893 /* enable automatic CTS handling */
2894 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
2895 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
2896 }
2897
2898 /* make sure the tx and rx are enabled */
2899
2900 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
2901 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
2902
2903 /* actually write the control regs to the hardware */
2904
Alan Coxd7283352008-08-04 17:21:18 +01002905 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
2906 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
Takashi Iwaia88487c2008-07-16 21:54:42 +01002907 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
Alan Coxadc8d742012-07-14 15:31:47 +01002909 if (info->port.tty->termios.c_iflag & IXON ) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01002910 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
2911 STOP_CHAR(info->port.tty)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
2913 }
2914
Alan Coxd7283352008-08-04 17:21:18 +01002915 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002916 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 update_char_time(info);
2919
2920} /* change_speed */
2921
2922/* start transmitting chars NOW */
2923
2924static void
2925rs_flush_chars(struct tty_struct *tty)
2926{
2927 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2928 unsigned long flags;
2929
2930 if (info->tr_running ||
2931 info->xmit.head == info->xmit.tail ||
2932 tty->stopped ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 !info->xmit.buf)
2934 return;
2935
2936#ifdef SERIAL_DEBUG_FLOW
2937 printk("rs_flush_chars\n");
2938#endif
2939
2940 /* this protection might not exactly be necessary here */
2941
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002942 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 start_transmit(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002944 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002947static int rs_raw_write(struct tty_struct *tty,
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002948 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 int c, ret = 0;
2951 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2952 unsigned long flags;
2953
2954 /* first some sanity checks */
2955
Cong Dingf938f372013-01-16 23:30:44 +01002956 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return 0;
2958
2959#ifdef SERIAL_DEBUG_DATA
2960 if (info->line == SERIAL_DEBUG_LINE)
2961 printk("rs_raw_write (%d), status %d\n",
Alan Coxd7283352008-08-04 17:21:18 +01002962 count, info->ioport[REG_STATUS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963#endif
2964
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002965 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
2967 DFLOW(DEBUG_LOG(info->line, "ldisc %i\n", tty->ldisc.chars_in_buffer(tty)));
2968
2969
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002970 /* The local_irq_disable/restore_flags pairs below are needed
2971 * because the DMA interrupt handler moves the info->xmit values.
2972 * the memcpy needs to be in the critical region unfortunately,
2973 * because we need to read xmit values, memcpy, write xmit values
2974 * in one atomic operation... this could perhaps be avoided by
2975 * more clever design.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002977 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 while (count) {
2979 c = CIRC_SPACE_TO_END(info->xmit.head,
2980 info->xmit.tail,
2981 SERIAL_XMIT_SIZE);
2982
2983 if (count < c)
2984 c = count;
2985 if (c <= 0)
2986 break;
2987
2988 memcpy(info->xmit.buf + info->xmit.head, buf, c);
2989 info->xmit.head = (info->xmit.head + c) &
2990 (SERIAL_XMIT_SIZE-1);
2991 buf += c;
2992 count -= c;
2993 ret += c;
2994 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002995 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 /* enable transmitter if not running, unless the tty is stopped
2998 * this does not need IRQ protection since if tr_running == 0
2999 * the IRQ's are not running anyway for this port.
3000 */
3001 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3002
3003 if (info->xmit.head != info->xmit.tail &&
3004 !tty->stopped &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 !info->tr_running) {
3006 start_transmit(info);
3007 }
3008
3009 return ret;
3010} /* raw_raw_write() */
3011
3012static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003013rs_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 const unsigned char *buf, int count)
3015{
3016#if defined(CONFIG_ETRAX_RS485)
3017 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3018
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003019 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 {
3021 /* If we are in RS-485 mode, we need to toggle RTS and disable
3022 * the receiver before initiating a DMA transfer
3023 */
3024#ifdef CONFIG_ETRAX_FAST_TIMER
3025 /* Abort any started timer */
3026 fast_timers_rs485[info->line].function = NULL;
3027 del_fast_timer(&fast_timers_rs485[info->line]);
3028#endif
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003029 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3031 e100_disable_rx(info);
3032 e100_enable_rx_irq(info);
3033#endif
Claudio Scordino93f33502011-11-09 15:51:49 +01003034 if (info->rs485.delay_rts_before_send > 0)
3035 msleep(info->rs485.delay_rts_before_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037#endif /* CONFIG_ETRAX_RS485 */
3038
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003039 count = rs_raw_write(tty, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003042 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 {
3044 unsigned int val;
3045 /* If we are in RS-485 mode the following has to be done:
3046 * wait until DMA is ready
3047 * wait on transmit shift register
3048 * toggle RTS
3049 * enable the receiver
3050 */
3051
3052 /* Sleep until all sent */
3053 tty_wait_until_sent(tty, 0);
3054#ifdef CONFIG_ETRAX_FAST_TIMER
3055 /* Now sleep a little more so that shift register is empty */
3056 schedule_usleep(info->char_time_usec * 2);
3057#endif
3058 /* wait on transmit shift register */
3059 do{
3060 get_lsr_info(info, &val);
3061 }while (!(val & TIOCSER_TEMT));
3062
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003063 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3066 e100_enable_rx(info);
3067 e100_enable_rxdma_irq(info);
3068#endif
3069 }
3070#endif /* CONFIG_ETRAX_RS485 */
3071
3072 return count;
3073} /* rs_write */
3074
3075
3076/* how much space is available in the xmit buffer? */
3077
3078static int
3079rs_write_room(struct tty_struct *tty)
3080{
3081 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3082
3083 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3084}
3085
3086/* How many chars are in the xmit buffer?
3087 * This does not include any chars in the transmitter FIFO.
3088 * Use wait_until_sent for waiting for FIFO drain.
3089 */
3090
3091static int
3092rs_chars_in_buffer(struct tty_struct *tty)
3093{
3094 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3095
3096 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3097}
3098
3099/* discard everything in the xmit buffer */
3100
3101static void
3102rs_flush_buffer(struct tty_struct *tty)
3103{
3104 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3105 unsigned long flags;
3106
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003107 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 info->xmit.head = info->xmit.tail = 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003109 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Jiri Slabyb963a842007-02-10 01:44:55 -08003111 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
3114/*
3115 * This function is used to send a high-priority XON/XOFF character to
3116 * the device
3117 *
3118 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3119 * but we do it in handle_ser_tx_interrupt().
3120 * We disable DMA channel and enable tx ready interrupt and write the
3121 * character when possible.
3122 */
3123static void rs_send_xchar(struct tty_struct *tty, char ch)
3124{
3125 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3126 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003127 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (info->uses_dma_out) {
3129 /* Put the DMA on hold and disable the channel */
3130 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3131 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3132 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3133 e100_disable_txdma_channel(info);
3134 }
3135
3136 /* Must make sure transmitter is not stopped before we can transmit */
3137 if (tty->stopped)
3138 rs_start(tty);
3139
3140 /* Enable manual transmit interrupt and send from there */
3141 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3142 info->x_char = ch;
3143 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003144 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
3147/*
3148 * ------------------------------------------------------------
3149 * rs_throttle()
3150 *
3151 * This routine is called by the upper-layer tty layer to signal that
3152 * incoming characters should be throttled.
3153 * ------------------------------------------------------------
3154 */
3155static void
3156rs_throttle(struct tty_struct * tty)
3157{
3158 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3159#ifdef SERIAL_DEBUG_THROTTLE
Rasmus Villemoes429b4742015-03-31 15:55:59 +02003160 printk("throttle %s: %lu....\n", tty_name(tty),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3162#endif
3163 DFLOW(DEBUG_LOG(info->line,"rs_throttle %lu\n", tty->ldisc.chars_in_buffer(tty)));
3164
3165 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003166 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* Turn off RTS line */
3168 e100_rts(info, 0);
3169 }
3170 if (I_IXOFF(tty))
3171 rs_send_xchar(tty, STOP_CHAR(tty));
3172
3173}
3174
3175static void
3176rs_unthrottle(struct tty_struct * tty)
3177{
3178 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3179#ifdef SERIAL_DEBUG_THROTTLE
Rasmus Villemoes429b4742015-03-31 15:55:59 +02003180 printk("unthrottle %s: %lu....\n", tty_name(tty),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3182#endif
3183 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc %d\n", tty->ldisc.chars_in_buffer(tty)));
3184 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3185 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003186 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Assert RTS line */
3188 e100_rts(info, 1);
3189 }
3190
3191 if (I_IXOFF(tty)) {
3192 if (info->x_char)
3193 info->x_char = 0;
3194 else
3195 rs_send_xchar(tty, START_CHAR(tty));
3196 }
3197
3198}
3199
3200/*
3201 * ------------------------------------------------------------
3202 * rs_ioctl() and friends
3203 * ------------------------------------------------------------
3204 */
3205
3206static int
3207get_serial_info(struct e100_serial * info,
3208 struct serial_struct * retinfo)
3209{
3210 struct serial_struct tmp;
3211
3212 /* this is all probably wrong, there are a lot of fields
3213 * here that we don't have in e100_serial and maybe we
3214 * should set them to something else than 0.
3215 */
3216
3217 if (!retinfo)
3218 return -EFAULT;
3219 memset(&tmp, 0, sizeof(tmp));
3220 tmp.type = info->type;
3221 tmp.line = info->line;
Alan Coxd7283352008-08-04 17:21:18 +01003222 tmp.port = (int)info->ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 tmp.irq = info->irq;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003224 tmp.flags = info->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 tmp.baud_base = info->baud_base;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003226 tmp.close_delay = info->port.close_delay;
3227 tmp.closing_wait = info->port.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 tmp.custom_divisor = info->custom_divisor;
3229 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3230 return -EFAULT;
3231 return 0;
3232}
3233
3234static int
3235set_serial_info(struct e100_serial *info,
3236 struct serial_struct *new_info)
3237{
3238 struct serial_struct new_serial;
3239 struct e100_serial old_info;
3240 int retval = 0;
3241
3242 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3243 return -EFAULT;
3244
3245 old_info = *info;
3246
3247 if (!capable(CAP_SYS_ADMIN)) {
3248 if ((new_serial.type != info->type) ||
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003249 (new_serial.close_delay != info->port.close_delay) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003251 (info->port.flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return -EPERM;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003253 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 (new_serial.flags & ASYNC_USR_MASK));
3255 goto check_and_exit;
3256 }
3257
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003258 if (info->port.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return -EBUSY;
3260
3261 /*
3262 * OK, past this point, all the error checking has been done.
3263 * At this point, we start making changes.....
3264 */
3265
3266 info->baud_base = new_serial.baud_base;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003267 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 (new_serial.flags & ASYNC_FLAGS));
3269 info->custom_divisor = new_serial.custom_divisor;
3270 info->type = new_serial.type;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003271 info->port.close_delay = new_serial.close_delay;
3272 info->port.closing_wait = new_serial.closing_wait;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003273 info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 check_and_exit:
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003276 if (info->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 change_speed(info);
3278 } else
3279 retval = startup(info);
3280 return retval;
3281}
3282
3283/*
3284 * get_lsr_info - get line status register info
3285 *
3286 * Purpose: Let user call ioctl() to get info when the UART physically
3287 * is emptied. On bus types like RS485, the transmitter must
3288 * release the bus after transmitting. This must be done when
3289 * the transmit shift register is empty, not be done when the
3290 * transmit holding register is empty. This functionality
3291 * allows an RS485 driver to be written in user space.
3292 */
3293static int
3294get_lsr_info(struct e100_serial * info, unsigned int *value)
3295{
3296 unsigned int result = TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 unsigned long curr_time = jiffies;
3298 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3299 unsigned long elapsed_usec =
3300 (curr_time - info->last_tx_active) * 1000000/HZ +
3301 curr_time_usec - info->last_tx_active_usec;
3302
3303 if (info->xmit.head != info->xmit.tail ||
3304 elapsed_usec < 2*info->char_time_usec) {
3305 result = 0;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308 if (copy_to_user(value, &result, sizeof(int)))
3309 return -EFAULT;
3310 return 0;
3311}
3312
3313#ifdef SERIAL_DEBUG_IO
3314struct state_str
3315{
3316 int state;
3317 const char *str;
3318};
3319
3320const struct state_str control_state_str[] = {
3321 {TIOCM_DTR, "DTR" },
3322 {TIOCM_RTS, "RTS"},
3323 {TIOCM_ST, "ST?" },
3324 {TIOCM_SR, "SR?" },
3325 {TIOCM_CTS, "CTS" },
3326 {TIOCM_CD, "CD" },
3327 {TIOCM_RI, "RI" },
3328 {TIOCM_DSR, "DSR" },
3329 {0, NULL }
3330};
3331
3332char *get_control_state_str(int MLines, char *s)
3333{
3334 int i = 0;
3335
3336 s[0]='\0';
3337 while (control_state_str[i].str != NULL) {
3338 if (MLines & control_state_str[i].state) {
3339 if (s[0] != '\0') {
3340 strcat(s, ", ");
3341 }
3342 strcat(s, control_state_str[i].str);
3343 }
3344 i++;
3345 }
3346 return s;
3347}
3348#endif
3349
Alan Coxd7283352008-08-04 17:21:18 +01003350static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003351rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003353 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3354 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Alan Coxd7283352008-08-04 17:21:18 +01003356 if (!info->ioport)
3357 return -EIO;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003358
3359 local_irq_save(flags);
3360 if (break_state == -1) {
3361 /* Go to manual mode and set the txd pin to 0 */
3362 /* Clear bit 7 (txd) and 6 (tr_enable) */
3363 info->tx_ctrl &= 0x3F;
3364 } else {
3365 /* Set bit 7 (txd) and 6 (tr_enable) */
3366 info->tx_ctrl |= (0x80 | 0x40);
3367 }
Alan Coxd7283352008-08-04 17:21:18 +01003368 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003369 local_irq_restore(flags);
Alan Coxd7283352008-08-04 17:21:18 +01003370 return 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003371}
3372
3373static int
Alan Cox20b9d172011-02-14 16:26:50 +00003374rs_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003375{
3376 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
Alan Cox978e5952008-04-30 00:53:59 -07003377 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003378
Alan Cox978e5952008-04-30 00:53:59 -07003379 local_irq_save(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003380
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003381 if (clear & TIOCM_RTS)
3382 e100_rts(info, 0);
3383 if (clear & TIOCM_DTR)
3384 e100_dtr(info, 0);
3385 /* Handle FEMALE behaviour */
3386 if (clear & TIOCM_RI)
3387 e100_ri_out(info, 0);
3388 if (clear & TIOCM_CD)
3389 e100_cd_out(info, 0);
3390
3391 if (set & TIOCM_RTS)
3392 e100_rts(info, 1);
3393 if (set & TIOCM_DTR)
3394 e100_dtr(info, 1);
3395 /* Handle FEMALE behaviour */
3396 if (set & TIOCM_RI)
3397 e100_ri_out(info, 1);
3398 if (set & TIOCM_CD)
3399 e100_cd_out(info, 1);
Alan Cox032c17e2008-04-28 02:13:50 -07003400
Alan Cox978e5952008-04-30 00:53:59 -07003401 local_irq_restore(flags);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003402 return 0;
3403}
3404
3405static int
Alan Cox60b33c12011-02-14 16:26:14 +00003406rs_tiocmget(struct tty_struct *tty)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003407{
3408 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3409 unsigned int result;
Alan Cox978e5952008-04-30 00:53:59 -07003410 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Alan Cox978e5952008-04-30 00:53:59 -07003412 local_irq_save(flags);
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 result =
3415 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3416 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3417 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3418 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3419 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3420 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3421
Alan Cox978e5952008-04-30 00:53:59 -07003422 local_irq_restore(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424#ifdef SERIAL_DEBUG_IO
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003425 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3426 info->line, result, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 {
3428 char s[100];
3429
3430 get_control_state_str(result, s);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003431 printk(KERN_DEBUG "state: %s\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003434 return result;
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
3438
3439static int
Alan Cox6caa76b2011-02-14 16:27:22 +00003440rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 unsigned int cmd, unsigned long arg)
3442{
3443 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3444
3445 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3446 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3447 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3448 if (tty->flags & (1 << TTY_IO_ERROR))
3449 return -EIO;
3450 }
3451
3452 switch (cmd) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003453 case TIOCGSERIAL:
3454 return get_serial_info(info,
3455 (struct serial_struct *) arg);
3456 case TIOCSSERIAL:
3457 return set_serial_info(info,
3458 (struct serial_struct *) arg);
3459 case TIOCSERGETLSR: /* Get line status register */
3460 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003462 case TIOCSERGSTRUCT:
3463 if (copy_to_user((struct e100_serial *) arg,
3464 info, sizeof(struct e100_serial)))
3465 return -EFAULT;
3466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468#if defined(CONFIG_ETRAX_RS485)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003469 case TIOCSERSETRS485:
3470 {
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003471 /* In this ioctl we still use the old structure
3472 * rs485_control for backward compatibility
3473 * (if we use serial_rs485, then old user-level code
3474 * wouldn't work anymore...).
3475 * The use of this ioctl is deprecated: use TIOCSRS485
3476 * instead.*/
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003477 struct rs485_control rs485ctrl;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003478 struct serial_rs485 rs485data;
3479 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003480 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3481 sizeof(rs485ctrl)))
3482 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003484 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3485 rs485data.flags = 0;
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02003486
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003487 if (rs485ctrl.enabled)
3488 rs485data.flags |= SER_RS485_ENABLED;
3489 else
3490 rs485data.flags &= ~(SER_RS485_ENABLED);
3491
3492 if (rs485ctrl.rts_on_send)
3493 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3494 else
3495 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3496
3497 if (rs485ctrl.rts_after_sent)
3498 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3499 else
3500 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3501
3502 return e100_enable_rs485(tty, &rs485data);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003505 case TIOCSRS485:
3506 {
3507 /* This is the new version of TIOCSRS485, with new
3508 * data structure serial_rs485 */
3509 struct serial_rs485 rs485data;
3510 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3511 sizeof(rs485data)))
3512 return -EFAULT;
3513
3514 return e100_enable_rs485(tty, &rs485data);
3515 }
3516
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003517 case TIOCGRS485:
3518 {
3519 struct serial_rs485 *rs485data =
3520 &(((struct e100_serial *)tty->driver_data)->rs485);
3521 /* This is the ioctl to get RS485 data from user-space */
3522 if (copy_to_user((struct serial_rs485 *) arg,
3523 rs485data,
Jesper Nilsson94479c02010-10-28 12:04:55 +02003524 sizeof(struct serial_rs485)))
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003525 return -EFAULT;
3526 break;
3527 }
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003528
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003529 case TIOCSERWRRS485:
3530 {
3531 struct rs485_write rs485wr;
3532 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3533 sizeof(rs485wr)))
3534 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003536 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538#endif
3539
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003540 default:
3541 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543 return 0;
3544}
3545
3546static void
Alan Cox606d0992006-12-08 02:38:45 -08003547rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 change_speed(info);
3552
3553 /* Handle turning off CRTSCTS */
3554 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyee797062013-03-07 13:12:34 +01003555 !(tty->termios.c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 rs_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558}
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560/*
3561 * ------------------------------------------------------------
3562 * rs_close()
3563 *
3564 * This routine is called when the serial port gets closed. First, we
3565 * wait for the last remaining data to be sent. Then, we unlink its
3566 * S structure from the interrupt chain if necessary, and we free
3567 * that IRQ if nothing is left in the chain.
3568 * ------------------------------------------------------------
3569 */
3570static void
3571rs_close(struct tty_struct *tty, struct file * filp)
3572{
3573 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3574 unsigned long flags;
3575
3576 if (!info)
3577 return;
3578
3579 /* interrupts are disabled for this entire function */
3580
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003581 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 if (tty_hung_up_p(filp)) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003584 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return;
3586 }
3587
3588#ifdef SERIAL_DEBUG_OPEN
3589 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3590 info->line, info->count);
3591#endif
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003592 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 /*
3594 * Uh, oh. tty->count is 1, which means that the tty
3595 * structure will be freed. Info->count should always
3596 * be one in these conditions. If it's greater than
3597 * one, we've got real problems, since it means the
3598 * serial port won't be shutdown.
3599 */
WANG Cong3d43b7d2011-09-01 16:47:49 +08003600 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 "rs_close: bad serial port count; tty->count is 1, "
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003602 "info->count is %d\n", info->port.count);
3603 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003605 if (--info->port.count < 0) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08003606 printk(KERN_ERR "rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003607 info->line, info->port.count);
3608 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003610 if (info->port.count) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003611 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return;
3613 }
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003614 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 * Now we wait for the transmit buffer to clear; and we notify
3617 * the line discipline to only process XON/XOFF characters.
3618 */
3619 tty->closing = 1;
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003620 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
3621 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 /*
3623 * At this point we stop accepting input. To do this, we
3624 * disable the serial receiver and the DMA receive interrupt.
3625 */
3626#ifdef SERIAL_HANDLE_EARLY_ERRORS
3627 e100_disable_serial_data_irq(info);
3628#endif
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 e100_disable_rx(info);
3631 e100_disable_rx_irq(info);
3632
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003633 if (info->port.flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 /*
3635 * Before we drop DTR, make sure the UART transmitter
3636 * has completely drained; this is especially
3637 * important as we have a transmit FIFO!
3638 */
3639 rs_wait_until_sent(tty, HZ);
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07003643 rs_flush_buffer(tty);
Alan Cox454aa382008-05-12 12:31:37 +01003644 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 tty->closing = 0;
3646 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003647 info->port.tty = NULL;
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003648 if (info->port.blocked_open) {
Jiri Slaby892c7cf2013-03-07 13:12:38 +01003649 if (info->port.close_delay)
3650 schedule_timeout_interruptible(info->port.close_delay);
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003651 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003653 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003654 wake_up_interruptible(&info->port.close_wait);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003655 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 /* port closed */
3658
3659#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003660 if (info->rs485.flags & SER_RS485_ENABLED) {
3661 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662#if defined(CONFIG_ETRAX_RS485_ON_PA)
3663 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3664#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003667
3668 /*
3669 * Release any allocated DMA irq's.
3670 */
3671 if (info->dma_in_enabled) {
3672 free_irq(info->dma_in_irq_nbr, info);
3673 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3674 info->uses_dma_in = 0;
3675#ifdef SERIAL_DEBUG_OPEN
3676 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3677 info->dma_in_irq_description);
3678#endif
3679 }
3680 if (info->dma_out_enabled) {
3681 free_irq(info->dma_out_irq_nbr, info);
3682 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3683 info->uses_dma_out = 0;
3684#ifdef SERIAL_DEBUG_OPEN
3685 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3686 info->dma_out_irq_description);
3687#endif
3688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
3691/*
3692 * rs_wait_until_sent() --- wait until the transmitter is empty
3693 */
3694static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3695{
3696 unsigned long orig_jiffies;
3697 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3698 unsigned long curr_time = jiffies;
3699 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3700 long elapsed_usec =
3701 (curr_time - info->last_tx_active) * (1000000/HZ) +
3702 curr_time_usec - info->last_tx_active_usec;
3703
3704 /*
3705 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3706 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3707 */
3708 orig_jiffies = jiffies;
3709 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3710 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3711 (elapsed_usec < 2*info->char_time_usec)) {
Nishanth Aravamudan3c76bc52005-11-07 01:01:21 -08003712 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (signal_pending(current))
3714 break;
3715 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3716 break;
3717 curr_time = jiffies;
3718 curr_time_usec = GET_JIFFIES_USEC();
3719 elapsed_usec =
3720 (curr_time - info->last_tx_active) * (1000000/HZ) +
3721 curr_time_usec - info->last_tx_active_usec;
3722 }
3723 set_current_state(TASK_RUNNING);
3724}
3725
3726/*
3727 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3728 */
3729void
3730rs_hangup(struct tty_struct *tty)
3731{
3732 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3733
3734 rs_flush_buffer(tty);
3735 shutdown(info);
3736 info->event = 0;
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003737 info->port.count = 0;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003738 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003739 info->port.tty = NULL;
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003740 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
3743/*
3744 * ------------------------------------------------------------
3745 * rs_open() and friends
3746 * ------------------------------------------------------------
3747 */
3748static int
3749block_til_ready(struct tty_struct *tty, struct file * filp,
3750 struct e100_serial *info)
3751{
3752 DECLARE_WAITQUEUE(wait, current);
3753 unsigned long flags;
3754 int retval;
Peter Hurleye359a4e2014-06-16 09:17:06 -04003755 int do_clocal = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 /*
3758 * If the device is in the middle of being closed, then block
3759 * until it's done, and then try again.
3760 */
Peter Hurleye359a4e2014-06-16 09:17:06 -04003761 if (info->port.flags & ASYNC_CLOSING) {
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003762 wait_event_interruptible_tty(tty, info->port.close_wait,
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003763 !(info->port.flags & ASYNC_CLOSING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764#ifdef SERIAL_DO_RESTART
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003765 if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return -EAGAIN;
3767 else
3768 return -ERESTARTSYS;
3769#else
3770 return -EAGAIN;
3771#endif
3772 }
3773
3774 /*
3775 * If non-blocking mode is set, or the port is not enabled,
3776 * then make the check up front and then exit.
3777 */
3778 if ((filp->f_flags & O_NONBLOCK) ||
3779 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003780 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return 0;
3782 }
3783
Alan Coxadc8d742012-07-14 15:31:47 +01003784 if (tty->termios.c_cflag & CLOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 do_clocal = 1;
3786 }
3787
3788 /*
3789 * Block waiting for the carrier detect and the line to become
3790 * free (i.e., not in use by the callout). While we are in
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003791 * this loop, info->port.count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 * rs_close() knows when to free things. We restore it upon
3793 * exit, either normal or abnormal.
3794 */
3795 retval = 0;
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003796 add_wait_queue(&info->port.open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797#ifdef SERIAL_DEBUG_OPEN
3798 printk("block_til_ready before block: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003799 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003801 local_irq_save(flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -04003802 info->port.count--;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003803 local_irq_restore(flags);
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003804 info->port.blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 while (1) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003806 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* assert RTS and DTR */
3808 e100_rts(info, 1);
3809 e100_dtr(info, 1);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003810 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 set_current_state(TASK_INTERRUPTIBLE);
3812 if (tty_hung_up_p(filp) ||
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003813 !(info->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814#ifdef SERIAL_DO_RESTART
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003815 if (info->port.flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 retval = -EAGAIN;
3817 else
3818 retval = -ERESTARTSYS;
3819#else
3820 retval = -EAGAIN;
3821#endif
3822 break;
3823 }
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003824 if (!(info->port.flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 /* && (do_clocal || DCD_IS_ASSERTED) */
3826 break;
3827 if (signal_pending(current)) {
3828 retval = -ERESTARTSYS;
3829 break;
3830 }
3831#ifdef SERIAL_DEBUG_OPEN
3832 printk("block_til_ready blocking: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003833 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834#endif
Alan Cox89c8d912012-08-08 16:30:13 +01003835 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +01003837 tty_lock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 }
3839 set_current_state(TASK_RUNNING);
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003840 remove_wait_queue(&info->port.open_wait, &wait);
Peter Hurleye359a4e2014-06-16 09:17:06 -04003841 if (!tty_hung_up_p(filp))
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003842 info->port.count++;
3843 info->port.blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844#ifdef SERIAL_DEBUG_OPEN
3845 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003846 info->line, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847#endif
3848 if (retval)
3849 return retval;
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003850 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return 0;
3852}
3853
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003854static void
3855deinit_port(struct e100_serial *info)
3856{
3857 if (info->dma_out_enabled) {
3858 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3859 free_irq(info->dma_out_irq_nbr, info);
3860 }
3861 if (info->dma_in_enabled) {
3862 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3863 free_irq(info->dma_in_irq_nbr, info);
3864 }
3865}
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867/*
3868 * This routine is called whenever a serial port is opened.
3869 * It performs the serial-specific initialization for the tty structure.
3870 */
3871static int
3872rs_open(struct tty_struct *tty, struct file * filp)
3873{
3874 struct e100_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01003875 int retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003876 int allocated_resources = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Jiri Slaby410235f2012-03-05 14:52:01 +01003878 info = rs_table + tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (!info->enabled)
3880 return -ENODEV;
3881
3882#ifdef SERIAL_DEBUG_OPEN
3883 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003884 info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885#endif
3886
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003887 info->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003889 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003891 info->port.low_latency = !!(info->port.flags & ASYNC_LOW_LATENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 /*
3894 * If the port is in the middle of closing, bail out now
3895 */
Peter Hurleye359a4e2014-06-16 09:17:06 -04003896 if (info->port.flags & ASYNC_CLOSING) {
Jiri Slaby4aeaeb02013-03-07 13:12:39 +01003897 wait_event_interruptible_tty(tty, info->port.close_wait,
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003898 !(info->port.flags & ASYNC_CLOSING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899#ifdef SERIAL_DO_RESTART
Jiri Slabyb1d984c2013-03-07 13:12:36 +01003900 return ((info->port.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 -EAGAIN : -ERESTARTSYS);
3902#else
3903 return -EAGAIN;
3904#endif
3905 }
3906
3907 /*
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003908 * If DMA is enabled try to allocate the irq's.
3909 */
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003910 if (info->port.count == 1) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003911 allocated_resources = 1;
3912 if (info->dma_in_enabled) {
3913 if (request_irq(info->dma_in_irq_nbr,
3914 rec_interrupt,
3915 info->dma_in_irq_flags,
3916 info->dma_in_irq_description,
3917 info)) {
3918 printk(KERN_WARNING "DMA irq '%s' busy; "
3919 "falling back to non-DMA mode\n",
3920 info->dma_in_irq_description);
3921 /* Make sure we never try to use DMA in */
3922 /* for the port again. */
3923 info->dma_in_enabled = 0;
3924 } else if (cris_request_dma(info->dma_in_nbr,
3925 info->dma_in_irq_description,
3926 DMA_VERBOSE_ON_ERROR,
3927 info->dma_owner)) {
3928 free_irq(info->dma_in_irq_nbr, info);
3929 printk(KERN_WARNING "DMA '%s' busy; "
3930 "falling back to non-DMA mode\n",
3931 info->dma_in_irq_description);
3932 /* Make sure we never try to use DMA in */
3933 /* for the port again. */
3934 info->dma_in_enabled = 0;
3935 }
3936#ifdef SERIAL_DEBUG_OPEN
3937 else
3938 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
3939 info->dma_in_irq_description);
3940#endif
3941 }
3942 if (info->dma_out_enabled) {
3943 if (request_irq(info->dma_out_irq_nbr,
3944 tr_interrupt,
3945 info->dma_out_irq_flags,
3946 info->dma_out_irq_description,
3947 info)) {
3948 printk(KERN_WARNING "DMA irq '%s' busy; "
3949 "falling back to non-DMA mode\n",
3950 info->dma_out_irq_description);
3951 /* Make sure we never try to use DMA out */
3952 /* for the port again. */
3953 info->dma_out_enabled = 0;
3954 } else if (cris_request_dma(info->dma_out_nbr,
3955 info->dma_out_irq_description,
3956 DMA_VERBOSE_ON_ERROR,
3957 info->dma_owner)) {
3958 free_irq(info->dma_out_irq_nbr, info);
3959 printk(KERN_WARNING "DMA '%s' busy; "
3960 "falling back to non-DMA mode\n",
3961 info->dma_out_irq_description);
3962 /* Make sure we never try to use DMA out */
3963 /* for the port again. */
3964 info->dma_out_enabled = 0;
3965 }
3966#ifdef SERIAL_DEBUG_OPEN
3967 else
3968 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
3969 info->dma_out_irq_description);
3970#endif
3971 }
3972 }
3973
3974 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 * Start up the serial port
3976 */
3977
3978 retval = startup(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003979 if (retval) {
3980 if (allocated_resources)
3981 deinit_port(info);
3982
Jiri Slabyb12d8dc2013-03-07 13:12:40 +01003983 /* FIXME Decrease count info->port.count here too? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003985 }
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988 retval = block_til_ready(tty, filp, info);
3989 if (retval) {
3990#ifdef SERIAL_DEBUG_OPEN
3991 printk("rs_open returning after block_til_ready with %d\n",
3992 retval);
3993#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003994 if (allocated_resources)
3995 deinit_port(info);
3996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return retval;
3998 }
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000#ifdef SERIAL_DEBUG_OPEN
4001 printk("rs_open ttyS%d successful...\n", info->line);
4002#endif
4003 DLOG_INT_TRIG( log_int_pos = 0);
4004
4005 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4006 info->icount.rx = 0;
4007 } );
4008
4009 return 0;
4010}
4011
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004012#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013/*
4014 * /proc fs routines....
4015 */
4016
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004017static void seq_line_info(struct seq_file *m, struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 unsigned long tmp;
4020
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004021 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
4022 info->line, (unsigned long)info->ioport, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Alan Coxd7283352008-08-04 17:21:18 +01004024 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004025 seq_printf(m, "\n");
4026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004029 seq_printf(m, " baud:%d", info->baud);
4030 seq_printf(m, " tx:%lu rx:%lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 (unsigned long)info->icount.tx,
4032 (unsigned long)info->icount.rx);
4033 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004034 if (tmp)
4035 seq_printf(m, " tx_pend:%lu/%lu",
4036 (unsigned long)tmp,
4037 (unsigned long)SERIAL_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004039 seq_printf(m, " rx_pend:%lu/%lu",
4040 (unsigned long)info->recv_cnt,
4041 (unsigned long)info->max_recv_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043#if 1
Takashi Iwaia88487c2008-07-16 21:54:42 +01004044 if (info->port.tty) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01004045 if (info->port.tty->stopped)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004046 seq_printf(m, " stopped:%i",
4047 (int)info->port.tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
4049
4050 {
Alan Coxd7283352008-08-04 17:21:18 +01004051 unsigned char rstat = info->ioport[REG_STATUS];
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004052 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4053 seq_printf(m, " xoff_detect:1");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
4055
4056#endif
4057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 if (info->icount.frame)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004059 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 if (info->icount.parity)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004062 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 if (info->icount.brk)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004065 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 if (info->icount.overrun)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004068 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070 /*
4071 * Last thing is the RS-232 status lines
4072 */
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004073 if (!E100_RTS_GET(info))
4074 seq_puts(m, "|RTS");
4075 if (!E100_CTS_GET(info))
4076 seq_puts(m, "|CTS");
4077 if (!E100_DTR_GET(info))
4078 seq_puts(m, "|DTR");
4079 if (!E100_DSR_GET(info))
4080 seq_puts(m, "|DSR");
4081 if (!E100_CD_GET(info))
4082 seq_puts(m, "|CD");
4083 if (!E100_RI_GET(info))
4084 seq_puts(m, "|RI");
4085 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004089static int crisv10_proc_show(struct seq_file *m, void *v)
4090{
4091 int i;
4092
4093 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4094
4095 for (i = 0; i < NR_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (!rs_table[i].enabled)
4097 continue;
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004098 seq_line_info(m, &rs_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
4100#ifdef DEBUG_LOG_INCLUDED
4101 for (i = 0; i < debug_log_pos; i++) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004102 seq_printf(m, "%-4i %lu.%lu ",
4103 i, debug_log[i].time,
4104 timer_data_to_ns(debug_log[i].timer_data));
4105 seq_printf(m, debug_log[i].string, debug_log[i].value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004107 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 debug_log_pos = 0;
4109#endif
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004113static int crisv10_proc_open(struct inode *inode, struct file *file)
4114{
4115 return single_open(file, crisv10_proc_show, NULL);
4116}
4117
4118static const struct file_operations crisv10_proc_fops = {
4119 .owner = THIS_MODULE,
4120 .open = crisv10_proc_open,
4121 .read = seq_read,
4122 .llseek = seq_lseek,
4123 .release = single_release,
4124};
4125#endif
4126
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128/* Finally, routines used to initialize the serial driver. */
4129
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004130static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
4132 printk(KERN_INFO
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004133 "ETRAX 100LX serial-driver %s, "
4134 "(c) 2000-2004 Axis Communications AB\r\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 &serial_version[11]); /* "$Revision: x.yy" */
4136}
4137
4138/* rs_init inits the driver at boot (using the module_init chain) */
4139
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004140static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 .open = rs_open,
4142 .close = rs_close,
4143 .write = rs_write,
4144 .flush_chars = rs_flush_chars,
4145 .write_room = rs_write_room,
4146 .chars_in_buffer = rs_chars_in_buffer,
4147 .flush_buffer = rs_flush_buffer,
4148 .ioctl = rs_ioctl,
4149 .throttle = rs_throttle,
4150 .unthrottle = rs_unthrottle,
4151 .set_termios = rs_set_termios,
4152 .stop = rs_stop,
4153 .start = rs_start,
4154 .hangup = rs_hangup,
4155 .break_ctl = rs_break,
4156 .send_xchar = rs_send_xchar,
4157 .wait_until_sent = rs_wait_until_sent,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004158 .tiocmget = rs_tiocmget,
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004159 .tiocmset = rs_tiocmset,
4160#ifdef CONFIG_PROC_FS
4161 .proc_fops = &crisv10_proc_fops,
4162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163};
4164
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004165static int __init rs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
4167 int i;
4168 struct e100_serial *info;
4169 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4170
4171 if (!driver)
4172 return -ENOMEM;
4173
4174 show_serial_version();
4175
4176 /* Setup the timed flush handler system */
4177
4178#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004179 setup_timer(&flush_timer, timed_flush_handler, 0);
4180 mod_timer(&flush_timer, jiffies + 5);
4181#endif
4182
4183#if defined(CONFIG_ETRAX_RS485)
4184#if defined(CONFIG_ETRAX_RS485_ON_PA)
WANG Cong2f7861d2011-09-01 13:58:29 +08004185 if (cris_io_interface_allocate_pins(if_serial_0, 'a', rs485_pa_bit,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004186 rs485_pa_bit)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004187 printk(KERN_ERR "ETRAX100LX serial: Could not allocate "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004188 "RS485 pin\n");
Julia Lawall41ca7322008-10-13 10:31:37 +01004189 put_tty_driver(driver);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004190 return -EBUSY;
4191 }
4192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193#endif
4194
4195 /* Initialize the tty_driver structure */
4196
4197 driver->driver_name = "serial";
4198 driver->name = "ttyS";
4199 driver->major = TTY_MAJOR;
4200 driver->minor_start = 64;
4201 driver->type = TTY_DRIVER_TYPE_SERIAL;
4202 driver->subtype = SERIAL_TYPE_NORMAL;
4203 driver->init_termios = tty_std_termios;
4204 driver->init_termios.c_cflag =
4205 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
Alan Cox606d0992006-12-08 02:38:45 -08004206 driver->init_termios.c_ispeed = 115200;
4207 driver->init_termios.c_ospeed = 115200;
Jiri Slabyc4d6ebe2012-08-07 21:47:48 +02004208 driver->flags = TTY_DRIVER_REAL_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 tty_set_operations(driver, &rs_ops);
4211 serial_driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004213 /* do some initializing for the separate ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004215 if (info->enabled) {
4216 if (cris_request_io_interface(info->io_if,
4217 info->io_if_description)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004218 printk(KERN_ERR "ETRAX100LX async serial: "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004219 "Could not allocate IO pins for "
4220 "%s, port %d\n",
4221 info->io_if_description, i);
4222 info->enabled = 0;
4223 }
4224 }
Jiri Slaby953756e2012-04-02 13:53:46 +02004225 tty_port_init(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 info->uses_dma_in = 0;
4227 info->uses_dma_out = 0;
4228 info->line = i;
Takashi Iwaia88487c2008-07-16 21:54:42 +01004229 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 info->type = PORT_ETRAX;
4231 info->tr_running = 0;
4232 info->forced_eop = 0;
4233 info->baud_base = DEF_BAUD_BASE;
4234 info->custom_divisor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 info->x_char = 0;
4236 info->event = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 info->xmit.buf = NULL;
4238 info->xmit.tail = info->xmit.head = 0;
4239 info->first_recv_buffer = info->last_recv_buffer = NULL;
4240 info->recv_cnt = info->max_recv_cnt = 0;
4241 info->last_tx_active_usec = 0;
4242 info->last_tx_active = 0;
4243
4244#if defined(CONFIG_ETRAX_RS485)
4245 /* Set sane defaults */
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004246 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4247 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 info->rs485.delay_rts_before_send = 0;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004249 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004251 INIT_WORK(&info->work, do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 if (info->enabled) {
Kulikov Vasiliy078dee22010-07-14 22:04:42 +04004254 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4255 serial_driver->name, info->line, info->ioport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004257 tty_port_link_device(&info->port, driver, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004259
4260 if (tty_register_driver(driver))
4261 panic("Couldn't register serial driver\n");
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263#ifdef CONFIG_ETRAX_FAST_TIMER
4264#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4265 memset(fast_timers, 0, sizeof(fast_timers));
4266#endif
4267#ifdef CONFIG_ETRAX_RS485
4268 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4269#endif
4270 fast_timer_init();
4271#endif
4272
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004273#ifndef CONFIG_ETRAX_KGDB
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 /* Not needed in simulator. May only complicate stuff. */
4275 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4276
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004277 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08004278 IRQF_SHARED, "serial ", driver))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07004279 panic("%s: Failed to request irq8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return 0;
4284}
4285
4286/* this makes sure that rs_init is called during kernel boot */
4287
4288module_init(rs_init);