blob: 50f56f39d436e6da31e947a4c8dff906ab84cd95 [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
59#if defined(CONFIG_ETRAX_RS485_ON_PA) && defined(CONFIG_ETRAX_RS485_ON_PORT_G)
60#error "Disable either CONFIG_ETRAX_RS485_ON_PA or CONFIG_ETRAX_RS485_ON_PORT_G"
61#endif
62
63/*
64 * All of the compatibilty code so we can compile serial.c against
65 * older kernels is hidden in serial_compat.h
66 */
67#if defined(LOCAL_HEADERS)
68#include "serial_compat.h"
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct tty_driver *serial_driver;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* number of characters left in xmit buffer before we ask for more */
74#define WAKEUP_CHARS 256
75
76//#define SERIAL_DEBUG_INTR
77//#define SERIAL_DEBUG_OPEN
78//#define SERIAL_DEBUG_FLOW
79//#define SERIAL_DEBUG_DATA
80//#define SERIAL_DEBUG_THROTTLE
81//#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
82//#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
83
84/* Enable this to use serial interrupts to handle when you
85 expect the first received event on the serial port to
86 be an error, break or similar. Used to be able to flash IRMA
87 from eLinux */
88#define SERIAL_HANDLE_EARLY_ERRORS
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Currently 16 descriptors x 128 bytes = 2048 bytes */
91#define SERIAL_DESCR_BUF_SIZE 256
92
93#define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
94#define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
95
96/* We don't want to load the system with massive fast timer interrupt
97 * on high baudrates so limit it to 250 us (4kHz) */
98#define MIN_FLUSH_TIME_USEC 250
99
100/* Add an x here to log a lot of timer stuff */
101#define TIMERD(x)
102/* Debug details of interrupt handling */
103#define DINTR1(x) /* irq on/off, errors */
104#define DINTR2(x) /* tx and rx */
105/* Debug flip buffer stuff */
106#define DFLIP(x)
107/* Debug flow control and overview of data flow */
108#define DFLOW(x)
109#define DBAUD(x)
110#define DLOG_INT_TRIG(x)
111
112//#define DEBUG_LOG_INCLUDED
113#ifndef DEBUG_LOG_INCLUDED
114#define DEBUG_LOG(line, string, value)
115#else
116struct debug_log_info
117{
118 unsigned long time;
119 unsigned long timer_data;
120// int line;
121 const char *string;
122 int value;
123};
124#define DEBUG_LOG_SIZE 4096
125
126struct debug_log_info debug_log[DEBUG_LOG_SIZE];
127int debug_log_pos = 0;
128
129#define DEBUG_LOG(_line, _string, _value) do { \
130 if ((_line) == SERIAL_DEBUG_LINE) {\
131 debug_log_func(_line, _string, _value); \
132 }\
133}while(0)
134
135void debug_log_func(int line, const char *string, int value)
136{
137 if (debug_log_pos < DEBUG_LOG_SIZE) {
138 debug_log[debug_log_pos].time = jiffies;
139 debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
140// debug_log[debug_log_pos].line = line;
141 debug_log[debug_log_pos].string = string;
142 debug_log[debug_log_pos].value = value;
143 debug_log_pos++;
144 }
145 /*printk(string, value);*/
146}
147#endif
148
149#ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
150/* Default number of timer ticks before flushing rx fifo
151 * When using "little data, low latency applications: use 0
152 * When using "much data applications (PPP)" use ~5
153 */
154#define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
155#endif
156
157unsigned long timer_data_to_ns(unsigned long timer_data);
158
159static void change_speed(struct e100_serial *info);
160static void rs_throttle(struct tty_struct * tty);
161static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800162static int rs_write(struct tty_struct *tty,
163 const unsigned char *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#ifdef CONFIG_ETRAX_RS485
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800165static int e100_write_rs485(struct tty_struct *tty,
166 const unsigned char *buf, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800168static int get_lsr_info(struct e100_serial *info, unsigned int *value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170
171#define DEF_BAUD 115200 /* 115.2 kbit/s */
172#define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
173#define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
174/* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
175#define DEF_TX 0x80 /* or SERIAL_CTRL_B */
176
177/* offsets from R_SERIALx_CTRL */
178
179#define REG_DATA 0
180#define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
181#define REG_TR_DATA 0
182#define REG_STATUS 1
183#define REG_TR_CTRL 1
184#define REG_REC_CTRL 2
185#define REG_BAUD 3
186#define REG_XOFF 4 /* this is a 32 bit register */
187
188/* The bitfields are the same for all serial ports */
189#define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
190#define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
191#define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
192#define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
193#define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
194
195#define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
196
197/* Values for info->errorcode */
198#define ERRCODE_SET_BREAK (TTY_BREAK)
199#define ERRCODE_INSERT 0x100
200#define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
201
202#define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
203
204/*
205 * General note regarding the use of IO_* macros in this file:
206 *
207 * We will use the bits defined for DMA channel 6 when using various
208 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
209 * the same for all channels (which of course they are).
210 *
211 * We will also use the bits defined for serial port 0 when writing commands
212 * to the different ports, as these bits too are the same for all ports.
213 */
214
215
216/* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
217static const unsigned long e100_ser_int_mask = 0
218#ifdef CONFIG_ETRAX_SERIAL_PORT0
219| IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
220#endif
221#ifdef CONFIG_ETRAX_SERIAL_PORT1
222| IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
223#endif
224#ifdef CONFIG_ETRAX_SERIAL_PORT2
225| IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
226#endif
227#ifdef CONFIG_ETRAX_SERIAL_PORT3
228| IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
229#endif
230;
231unsigned long r_alt_ser_baudrate_shadow = 0;
232
233/* this is the data for the four serial ports in the etrax100 */
234/* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
235/* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
236
237static struct e100_serial rs_table[] = {
238 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100239 .ioport = (unsigned char *)R_SERIAL0_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .irq = 1U << 12, /* uses DMA 6 and 7 */
241 .oclrintradr = R_DMA_CH6_CLR_INTR,
242 .ofirstadr = R_DMA_CH6_FIRST,
243 .ocmdadr = R_DMA_CH6_CMD,
244 .ostatusadr = R_DMA_CH6_STATUS,
245 .iclrintradr = R_DMA_CH7_CLR_INTR,
246 .ifirstadr = R_DMA_CH7_FIRST,
247 .icmdadr = R_DMA_CH7_CMD,
248 .idescradr = R_DMA_CH7_DESCR,
249 .flags = STD_FLAGS,
250 .rx_ctrl = DEF_RX,
251 .tx_ctrl = DEF_TX,
252 .iseteop = 2,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800253 .dma_owner = dma_ser0,
254 .io_if = if_serial_0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#ifdef CONFIG_ETRAX_SERIAL_PORT0
256 .enabled = 1,
257#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
258 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800259 .dma_out_nbr = SER0_TX_DMA_NBR,
260 .dma_out_irq_nbr = SER0_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800261 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800262 .dma_out_irq_description = "serial 0 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#else
264 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800265 .dma_out_nbr = UINT_MAX,
266 .dma_out_irq_nbr = 0,
267 .dma_out_irq_flags = 0,
268 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#endif
270#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
271 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800272 .dma_in_nbr = SER0_RX_DMA_NBR,
273 .dma_in_irq_nbr = SER0_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800274 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800275 .dma_in_irq_description = "serial 0 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800277 .dma_in_enabled = 0,
278 .dma_in_nbr = UINT_MAX,
279 .dma_in_irq_nbr = 0,
280 .dma_in_irq_flags = 0,
281 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#endif
283#else
284 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800285 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .dma_out_enabled = 0,
287 .dma_in_enabled = 0
288#endif
289
290}, /* ttyS0 */
291#ifndef CONFIG_SVINTO_SIM
292 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100293 .ioport = (unsigned char *)R_SERIAL1_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .irq = 1U << 16, /* uses DMA 8 and 9 */
295 .oclrintradr = R_DMA_CH8_CLR_INTR,
296 .ofirstadr = R_DMA_CH8_FIRST,
297 .ocmdadr = R_DMA_CH8_CMD,
298 .ostatusadr = R_DMA_CH8_STATUS,
299 .iclrintradr = R_DMA_CH9_CLR_INTR,
300 .ifirstadr = R_DMA_CH9_FIRST,
301 .icmdadr = R_DMA_CH9_CMD,
302 .idescradr = R_DMA_CH9_DESCR,
303 .flags = STD_FLAGS,
304 .rx_ctrl = DEF_RX,
305 .tx_ctrl = DEF_TX,
306 .iseteop = 3,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800307 .dma_owner = dma_ser1,
308 .io_if = if_serial_1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#ifdef CONFIG_ETRAX_SERIAL_PORT1
310 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800311 .io_if_description = "ser1",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
313 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800314 .dma_out_nbr = SER1_TX_DMA_NBR,
315 .dma_out_irq_nbr = SER1_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800316 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800317 .dma_out_irq_description = "serial 1 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#else
319 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800320 .dma_out_nbr = UINT_MAX,
321 .dma_out_irq_nbr = 0,
322 .dma_out_irq_flags = 0,
323 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#endif
325#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
326 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800327 .dma_in_nbr = SER1_RX_DMA_NBR,
328 .dma_in_irq_nbr = SER1_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800329 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800330 .dma_in_irq_description = "serial 1 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800332 .dma_in_enabled = 0,
333 .dma_in_enabled = 0,
334 .dma_in_nbr = UINT_MAX,
335 .dma_in_irq_nbr = 0,
336 .dma_in_irq_flags = 0,
337 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
339#else
340 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800341 .io_if_description = NULL,
342 .dma_in_irq_nbr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .dma_out_enabled = 0,
344 .dma_in_enabled = 0
345#endif
346}, /* ttyS1 */
347
348 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100349 .ioport = (unsigned char *)R_SERIAL2_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .irq = 1U << 4, /* uses DMA 2 and 3 */
351 .oclrintradr = R_DMA_CH2_CLR_INTR,
352 .ofirstadr = R_DMA_CH2_FIRST,
353 .ocmdadr = R_DMA_CH2_CMD,
354 .ostatusadr = R_DMA_CH2_STATUS,
355 .iclrintradr = R_DMA_CH3_CLR_INTR,
356 .ifirstadr = R_DMA_CH3_FIRST,
357 .icmdadr = R_DMA_CH3_CMD,
358 .idescradr = R_DMA_CH3_DESCR,
359 .flags = STD_FLAGS,
360 .rx_ctrl = DEF_RX,
361 .tx_ctrl = DEF_TX,
362 .iseteop = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800363 .dma_owner = dma_ser2,
364 .io_if = if_serial_2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#ifdef CONFIG_ETRAX_SERIAL_PORT2
366 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800367 .io_if_description = "ser2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
369 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800370 .dma_out_nbr = SER2_TX_DMA_NBR,
371 .dma_out_irq_nbr = SER2_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800372 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800373 .dma_out_irq_description = "serial 2 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#else
375 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800376 .dma_out_nbr = UINT_MAX,
377 .dma_out_irq_nbr = 0,
378 .dma_out_irq_flags = 0,
379 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#endif
381#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
382 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800383 .dma_in_nbr = SER2_RX_DMA_NBR,
384 .dma_in_irq_nbr = SER2_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800385 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800386 .dma_in_irq_description = "serial 2 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800388 .dma_in_enabled = 0,
389 .dma_in_nbr = UINT_MAX,
390 .dma_in_irq_nbr = 0,
391 .dma_in_irq_flags = 0,
392 .dma_in_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
394#else
395 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800396 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .dma_out_enabled = 0,
398 .dma_in_enabled = 0
399#endif
400 }, /* ttyS2 */
401
402 { .baud = DEF_BAUD,
Alan Coxd7283352008-08-04 17:21:18 +0100403 .ioport = (unsigned char *)R_SERIAL3_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .irq = 1U << 8, /* uses DMA 4 and 5 */
405 .oclrintradr = R_DMA_CH4_CLR_INTR,
406 .ofirstadr = R_DMA_CH4_FIRST,
407 .ocmdadr = R_DMA_CH4_CMD,
408 .ostatusadr = R_DMA_CH4_STATUS,
409 .iclrintradr = R_DMA_CH5_CLR_INTR,
410 .ifirstadr = R_DMA_CH5_FIRST,
411 .icmdadr = R_DMA_CH5_CMD,
412 .idescradr = R_DMA_CH5_DESCR,
413 .flags = STD_FLAGS,
414 .rx_ctrl = DEF_RX,
415 .tx_ctrl = DEF_TX,
416 .iseteop = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800417 .dma_owner = dma_ser3,
418 .io_if = if_serial_3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#ifdef CONFIG_ETRAX_SERIAL_PORT3
420 .enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800421 .io_if_description = "ser3",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
423 .dma_out_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800424 .dma_out_nbr = SER3_TX_DMA_NBR,
425 .dma_out_irq_nbr = SER3_DMA_TX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800426 .dma_out_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800427 .dma_out_irq_description = "serial 3 dma tr",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#else
429 .dma_out_enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800430 .dma_out_nbr = UINT_MAX,
431 .dma_out_irq_nbr = 0,
432 .dma_out_irq_flags = 0,
433 .dma_out_irq_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif
435#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
436 .dma_in_enabled = 1,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800437 .dma_in_nbr = SER3_RX_DMA_NBR,
438 .dma_in_irq_nbr = SER3_DMA_RX_IRQ_NBR,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800439 .dma_in_irq_flags = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800440 .dma_in_irq_description = "serial 3 dma rec",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#else
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800442 .dma_in_enabled = 0,
443 .dma_in_nbr = UINT_MAX,
444 .dma_in_irq_nbr = 0,
445 .dma_in_irq_flags = 0,
446 .dma_in_irq_description = NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#endif
448#else
449 .enabled = 0,
Jesper Nilsson77accbf2007-11-14 17:01:15 -0800450 .io_if_description = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .dma_out_enabled = 0,
452 .dma_in_enabled = 0
453#endif
454 } /* ttyS3 */
455#endif
456};
457
458
459#define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
462static struct fast_timer fast_timers[NR_PORTS];
463#endif
464
465#ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
466#define PROCSTAT(x) x
467struct ser_statistics_type {
468 int overrun_cnt;
469 int early_errors_cnt;
470 int ser_ints_ok_cnt;
471 int errors_cnt;
472 unsigned long int processing_flip;
473 unsigned long processing_flip_still_room;
474 unsigned long int timeout_flush_cnt;
475 int rx_dma_ints;
476 int tx_dma_ints;
477 int rx_tot;
478 int tx_tot;
479};
480
481static struct ser_statistics_type ser_stat[NR_PORTS];
482
483#else
484
485#define PROCSTAT(x)
486
487#endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
488
489/* RS-485 */
490#if defined(CONFIG_ETRAX_RS485)
491#ifdef CONFIG_ETRAX_FAST_TIMER
492static struct fast_timer fast_timers_rs485[NR_PORTS];
493#endif
494#if defined(CONFIG_ETRAX_RS485_ON_PA)
495static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
496#endif
497#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
498static int rs485_port_g_bit = CONFIG_ETRAX_RS485_ON_PORT_G_BIT;
499#endif
500#endif
501
502/* Info and macros needed for each ports extra control/status signals. */
503#define E100_STRUCT_PORT(line, pinname) \
504 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
505 (R_PORT_PA_DATA): ( \
506 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
507 (R_PORT_PB_DATA):&dummy_ser[line]))
508
509#define E100_STRUCT_SHADOW(line, pinname) \
510 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
511 (&port_pa_data_shadow): ( \
512 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
513 (&port_pb_data_shadow):&dummy_ser[line]))
514#define E100_STRUCT_MASK(line, pinname) \
515 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
516 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
517 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
518 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
519
520#define DUMMY_DTR_MASK 1
521#define DUMMY_RI_MASK 2
522#define DUMMY_DSR_MASK 4
523#define DUMMY_CD_MASK 8
524static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
525
526/* If not all status pins are used or disabled, use mixed mode */
527#ifdef CONFIG_ETRAX_SERIAL_PORT0
528
529#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)
530
531#if SER0_PA_BITSUM != -4
532# if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
533# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
534# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
535# endif
536# endif
537# if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
538# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
539# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
540# endif
541# endif
542# if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
543# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
544# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
545# endif
546# endif
547# if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
548# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
549# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
550# endif
551# endif
552#endif
553
554#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)
555
556#if SER0_PB_BITSUM != -4
557# if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
558# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
559# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
560# endif
561# endif
562# if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
563# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
564# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
565# endif
566# endif
567# if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
568# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
569# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
570# endif
571# endif
572# if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
573# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
574# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
575# endif
576# endif
577#endif
578
579#endif /* PORT0 */
580
581
582#ifdef CONFIG_ETRAX_SERIAL_PORT1
583
584#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)
585
586#if SER1_PA_BITSUM != -4
587# if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
588# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
589# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
590# endif
591# endif
592# if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
593# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
594# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
595# endif
596# endif
597# if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
598# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
599# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
600# endif
601# endif
602# if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
603# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
604# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
605# endif
606# endif
607#endif
608
609#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)
610
611#if SER1_PB_BITSUM != -4
612# if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
613# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
614# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
615# endif
616# endif
617# if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
618# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
619# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
620# endif
621# endif
622# if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
623# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
624# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
625# endif
626# endif
627# if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
628# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
629# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
630# endif
631# endif
632#endif
633
634#endif /* PORT1 */
635
636#ifdef CONFIG_ETRAX_SERIAL_PORT2
637
638#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)
639
640#if SER2_PA_BITSUM != -4
641# if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
642# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
643# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
644# endif
645# endif
646# if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
647# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
648# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
649# endif
650# endif
651# if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
652# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
653# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
654# endif
655# endif
656# if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
657# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
658# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
659# endif
660# endif
661#endif
662
663#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)
664
665#if SER2_PB_BITSUM != -4
666# if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
667# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
668# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
669# endif
670# endif
671# if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
672# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
673# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
674# endif
675# endif
676# if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
677# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
678# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
679# endif
680# endif
681# if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
682# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
683# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
684# endif
685# endif
686#endif
687
688#endif /* PORT2 */
689
690#ifdef CONFIG_ETRAX_SERIAL_PORT3
691
692#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)
693
694#if SER3_PA_BITSUM != -4
695# if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
696# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
697# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
698# endif
699# endif
700# if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
701# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
702# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
703# endif
704# endif
705# if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
706# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
707# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
708# endif
709# endif
710# if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
711# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
712# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
713# endif
714# endif
715#endif
716
717#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)
718
719#if SER3_PB_BITSUM != -4
720# if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
721# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
722# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
723# endif
724# endif
725# if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
726# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
727# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
728# endif
729# endif
730# if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
731# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
732# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
733# endif
734# endif
735# if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
736# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
737# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
738# endif
739# endif
740#endif
741
742#endif /* PORT3 */
743
744
745#if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
746 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
747 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
748 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
749#define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
750#endif
751
752#ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
753/* The pins can be mixed on PA and PB */
754#define CONTROL_PINS_PORT_NOT_USED(line) \
755 &dummy_ser[line], &dummy_ser[line], \
756 &dummy_ser[line], &dummy_ser[line], \
757 &dummy_ser[line], &dummy_ser[line], \
758 &dummy_ser[line], &dummy_ser[line], \
759 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
760
761
762struct control_pins
763{
764 volatile unsigned char *dtr_port;
765 unsigned char *dtr_shadow;
766 volatile unsigned char *ri_port;
767 unsigned char *ri_shadow;
768 volatile unsigned char *dsr_port;
769 unsigned char *dsr_shadow;
770 volatile unsigned char *cd_port;
771 unsigned char *cd_shadow;
772
773 unsigned char dtr_mask;
774 unsigned char ri_mask;
775 unsigned char dsr_mask;
776 unsigned char cd_mask;
777};
778
779static const struct control_pins e100_modem_pins[NR_PORTS] =
780{
781 /* Ser 0 */
782 {
783#ifdef CONFIG_ETRAX_SERIAL_PORT0
784 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
785 E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
786 E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
787 E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
788 E100_STRUCT_MASK(0,DTR),
789 E100_STRUCT_MASK(0,RI),
790 E100_STRUCT_MASK(0,DSR),
791 E100_STRUCT_MASK(0,CD)
792#else
793 CONTROL_PINS_PORT_NOT_USED(0)
794#endif
795 },
796
797 /* Ser 1 */
798 {
799#ifdef CONFIG_ETRAX_SERIAL_PORT1
800 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
801 E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
802 E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
803 E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
804 E100_STRUCT_MASK(1,DTR),
805 E100_STRUCT_MASK(1,RI),
806 E100_STRUCT_MASK(1,DSR),
807 E100_STRUCT_MASK(1,CD)
808#else
809 CONTROL_PINS_PORT_NOT_USED(1)
810#endif
811 },
812
813 /* Ser 2 */
814 {
815#ifdef CONFIG_ETRAX_SERIAL_PORT2
816 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
817 E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
818 E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
819 E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
820 E100_STRUCT_MASK(2,DTR),
821 E100_STRUCT_MASK(2,RI),
822 E100_STRUCT_MASK(2,DSR),
823 E100_STRUCT_MASK(2,CD)
824#else
825 CONTROL_PINS_PORT_NOT_USED(2)
826#endif
827 },
828
829 /* Ser 3 */
830 {
831#ifdef CONFIG_ETRAX_SERIAL_PORT3
832 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
833 E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
834 E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
835 E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
836 E100_STRUCT_MASK(3,DTR),
837 E100_STRUCT_MASK(3,RI),
838 E100_STRUCT_MASK(3,DSR),
839 E100_STRUCT_MASK(3,CD)
840#else
841 CONTROL_PINS_PORT_NOT_USED(3)
842#endif
843 }
844};
845#else /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
846
847/* All pins are on either PA or PB for each serial port */
848#define CONTROL_PINS_PORT_NOT_USED(line) \
849 &dummy_ser[line], &dummy_ser[line], \
850 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
851
852
853struct control_pins
854{
855 volatile unsigned char *port;
856 unsigned char *shadow;
857
858 unsigned char dtr_mask;
859 unsigned char ri_mask;
860 unsigned char dsr_mask;
861 unsigned char cd_mask;
862};
863
864#define dtr_port port
865#define dtr_shadow shadow
866#define ri_port port
867#define ri_shadow shadow
868#define dsr_port port
869#define dsr_shadow shadow
870#define cd_port port
871#define cd_shadow shadow
872
873static const struct control_pins e100_modem_pins[NR_PORTS] =
874{
875 /* Ser 0 */
876 {
877#ifdef CONFIG_ETRAX_SERIAL_PORT0
878 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
879 E100_STRUCT_MASK(0,DTR),
880 E100_STRUCT_MASK(0,RI),
881 E100_STRUCT_MASK(0,DSR),
882 E100_STRUCT_MASK(0,CD)
883#else
884 CONTROL_PINS_PORT_NOT_USED(0)
885#endif
886 },
887
888 /* Ser 1 */
889 {
890#ifdef CONFIG_ETRAX_SERIAL_PORT1
891 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
892 E100_STRUCT_MASK(1,DTR),
893 E100_STRUCT_MASK(1,RI),
894 E100_STRUCT_MASK(1,DSR),
895 E100_STRUCT_MASK(1,CD)
896#else
897 CONTROL_PINS_PORT_NOT_USED(1)
898#endif
899 },
900
901 /* Ser 2 */
902 {
903#ifdef CONFIG_ETRAX_SERIAL_PORT2
904 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
905 E100_STRUCT_MASK(2,DTR),
906 E100_STRUCT_MASK(2,RI),
907 E100_STRUCT_MASK(2,DSR),
908 E100_STRUCT_MASK(2,CD)
909#else
910 CONTROL_PINS_PORT_NOT_USED(2)
911#endif
912 },
913
914 /* Ser 3 */
915 {
916#ifdef CONFIG_ETRAX_SERIAL_PORT3
917 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
918 E100_STRUCT_MASK(3,DTR),
919 E100_STRUCT_MASK(3,RI),
920 E100_STRUCT_MASK(3,DSR),
921 E100_STRUCT_MASK(3,CD)
922#else
923 CONTROL_PINS_PORT_NOT_USED(3)
924#endif
925 }
926};
927#endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
928
929#define E100_RTS_MASK 0x20
930#define E100_CTS_MASK 0x40
931
932/* All serial port signals are active low:
933 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
934 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
935 *
936 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
937 */
938
939/* Output */
940#define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
941/* Input */
Alan Coxd7283352008-08-04 17:21:18 +0100942#define E100_CTS_GET(info) ((info)->ioport[REG_STATUS] & E100_CTS_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944/* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
945/* Is an output */
946#define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
947
948/* Normally inputs */
949#define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
950#define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
951
952/* Input */
953#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/* Calculate the chartime depending on baudrate, numbor of bits etc. */
956static void update_char_time(struct e100_serial * info)
957{
Alan Coxadc8d742012-07-14 15:31:47 +0100958 tcflag_t cflags = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int bits;
960
961 /* calc. number of bits / data byte */
962 /* databits + startbit and 1 stopbit */
963 if ((cflags & CSIZE) == CS7)
964 bits = 9;
965 else
966 bits = 10;
967
968 if (cflags & CSTOPB) /* 2 stopbits ? */
969 bits++;
970
971 if (cflags & PARENB) /* parity bit ? */
972 bits++;
973
974 /* calc timeout */
975 info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
976 info->flush_time_usec = 4*info->char_time_usec;
977 if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
978 info->flush_time_usec = MIN_FLUSH_TIME_USEC;
979
980}
981
982/*
983 * This function maps from the Bxxxx defines in asm/termbits.h into real
984 * baud rates.
985 */
986
987static int
988cflag_to_baud(unsigned int cflag)
989{
990 static int baud_table[] = {
991 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
992 4800, 9600, 19200, 38400 };
993
994 static int ext_baud_table[] = {
995 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
996 0, 0, 0, 0, 0, 0, 0, 0 };
997
998 if (cflag & CBAUDEX)
999 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1000 else
1001 return baud_table[cflag & CBAUD];
1002}
1003
1004/* and this maps to an etrax100 hardware baud constant */
1005
1006static unsigned char
1007cflag_to_etrax_baud(unsigned int cflag)
1008{
1009 char retval;
1010
1011 static char baud_table[] = {
1012 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1013
1014 static char ext_baud_table[] = {
1015 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1016
1017 if (cflag & CBAUDEX)
1018 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1019 else
1020 retval = baud_table[cflag & CBAUD];
1021
1022 if (retval < 0) {
1023 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
1024 retval = 5; /* choose default 9600 instead */
1025 }
1026
1027 return retval | (retval << 4); /* choose same for both TX and RX */
1028}
1029
1030
1031/* Various static support functions */
1032
1033/* Functions to set or clear DTR/RTS on the requested line */
1034/* It is complicated by the fact that RTS is a serial port register, while
1035 * DTR might not be implemented in the HW at all, and if it is, it can be on
1036 * any general port.
1037 */
1038
1039
1040static inline void
1041e100_dtr(struct e100_serial *info, int set)
1042{
1043#ifndef CONFIG_SVINTO_SIM
1044 unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1045
1046#ifdef SERIAL_DEBUG_IO
1047 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1048 printk("ser%i shadow before 0x%02X get: %i\n",
1049 info->line, *e100_modem_pins[info->line].dtr_shadow,
1050 E100_DTR_GET(info));
1051#endif
1052 /* DTR is active low */
1053 {
1054 unsigned long flags;
1055
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001056 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1058 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
1059 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001060 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063#ifdef SERIAL_DEBUG_IO
1064 printk("ser%i shadow after 0x%02X get: %i\n",
1065 info->line, *e100_modem_pins[info->line].dtr_shadow,
1066 E100_DTR_GET(info));
1067#endif
1068#endif
1069}
1070
1071/* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1072 * 0=0V , 1=3.3V
1073 */
1074static inline void
1075e100_rts(struct e100_serial *info, int set)
1076{
1077#ifndef CONFIG_SVINTO_SIM
1078 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001079 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 info->rx_ctrl &= ~E100_RTS_MASK;
1081 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
Alan Coxd7283352008-08-04 17:21:18 +01001082 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001083 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084#ifdef SERIAL_DEBUG_IO
1085 printk("ser%i rts %i\n", info->line, set);
1086#endif
1087#endif
1088}
1089
1090
1091/* If this behaves as a modem, RI and CD is an output */
1092static inline void
1093e100_ri_out(struct e100_serial *info, int set)
1094{
1095#ifndef CONFIG_SVINTO_SIM
1096 /* RI is active low */
1097 {
1098 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1099 unsigned long flags;
1100
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001101 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1103 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
1104 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001105 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107#endif
1108}
1109static inline void
1110e100_cd_out(struct e100_serial *info, int set)
1111{
1112#ifndef CONFIG_SVINTO_SIM
1113 /* CD is active low */
1114 {
1115 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1116 unsigned long flags;
1117
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001118 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1120 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
1121 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001122 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124#endif
1125}
1126
1127static inline void
1128e100_disable_rx(struct e100_serial *info)
1129{
1130#ifndef CONFIG_SVINTO_SIM
1131 /* disable the receiver */
Alan Coxd7283352008-08-04 17:21:18 +01001132 info->ioport[REG_REC_CTRL] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1134#endif
1135}
1136
1137static inline void
1138e100_enable_rx(struct e100_serial *info)
1139{
1140#ifndef CONFIG_SVINTO_SIM
1141 /* enable the receiver */
Alan Coxd7283352008-08-04 17:21:18 +01001142 info->ioport[REG_REC_CTRL] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1144#endif
1145}
1146
1147/* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1148
1149static inline void
1150e100_disable_rxdma_irq(struct e100_serial *info)
1151{
1152#ifdef SERIAL_DEBUG_INTR
1153 printk("rxdma_irq(%d): 0\n",info->line);
1154#endif
1155 DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
1156 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1157}
1158
1159static inline void
1160e100_enable_rxdma_irq(struct e100_serial *info)
1161{
1162#ifdef SERIAL_DEBUG_INTR
1163 printk("rxdma_irq(%d): 1\n",info->line);
1164#endif
1165 DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
1166 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1167}
1168
1169/* the tx DMA uses only dma_descr interrupt */
1170
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001171static void e100_disable_txdma_irq(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173#ifdef SERIAL_DEBUG_INTR
1174 printk("txdma_irq(%d): 0\n",info->line);
1175#endif
1176 DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
1177 *R_IRQ_MASK2_CLR = info->irq;
1178}
1179
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001180static void e100_enable_txdma_irq(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182#ifdef SERIAL_DEBUG_INTR
1183 printk("txdma_irq(%d): 1\n",info->line);
1184#endif
1185 DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
1186 *R_IRQ_MASK2_SET = info->irq;
1187}
1188
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001189static void e100_disable_txdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 unsigned long flags;
1192
1193 /* Disable output DMA channel for the serial port in question
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001194 * ( set to something other than serialX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001196 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
1198 if (info->line == 0) {
1199 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
1200 IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
1201 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1202 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1203 }
1204 } else if (info->line == 1) {
1205 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
1206 IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
1207 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1208 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1209 }
1210 } else if (info->line == 2) {
1211 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
1212 IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
1213 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1214 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1215 }
1216 } else if (info->line == 3) {
1217 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
1218 IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
1219 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1220 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1221 }
1222 }
1223 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001224 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001228static void e100_enable_txdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 unsigned long flags;
1231
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001232 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
1234 /* Enable output DMA channel for the serial port in question */
1235 if (info->line == 0) {
1236 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1237 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1238 } else if (info->line == 1) {
1239 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1240 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1241 } else if (info->line == 2) {
1242 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1243 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1244 } else if (info->line == 3) {
1245 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1246 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1247 }
1248 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001249 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001252static void e100_disable_rxdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 unsigned long flags;
1255
1256 /* Disable input DMA channel for the serial port in question
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001257 * ( set to something other than serialX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001259 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (info->line == 0) {
1261 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
1262 IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
1263 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1264 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
1265 }
1266 } else if (info->line == 1) {
1267 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
1268 IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
1269 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1270 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
1271 }
1272 } else if (info->line == 2) {
1273 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
1274 IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
1275 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1276 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
1277 }
1278 } else if (info->line == 3) {
1279 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
1280 IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
1281 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1282 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
1283 }
1284 }
1285 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001286 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001290static void e100_enable_rxdma_channel(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 unsigned long flags;
1293
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001294 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Enable input DMA channel for the serial port in question */
1296 if (info->line == 0) {
1297 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1298 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
1299 } else if (info->line == 1) {
1300 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1301 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
1302 } else if (info->line == 2) {
1303 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1304 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
1305 } else if (info->line == 3) {
1306 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1307 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
1308 }
1309 *R_GEN_CONFIG = genconfig_shadow;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001310 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313#ifdef SERIAL_HANDLE_EARLY_ERRORS
1314/* in order to detect and fix errors on the first byte
1315 we have to use the serial interrupts as well. */
1316
1317static inline void
1318e100_disable_serial_data_irq(struct e100_serial *info)
1319{
1320#ifdef SERIAL_DEBUG_INTR
1321 printk("ser_irq(%d): 0\n",info->line);
1322#endif
1323 DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
1324 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1325}
1326
1327static inline void
1328e100_enable_serial_data_irq(struct e100_serial *info)
1329{
1330#ifdef SERIAL_DEBUG_INTR
1331 printk("ser_irq(%d): 1\n",info->line);
1332 printk("**** %d = %d\n",
1333 (8+2*info->line),
1334 (1U << (8+2*info->line)));
1335#endif
1336 DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
1337 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1338}
1339#endif
1340
1341static inline void
1342e100_disable_serial_tx_ready_irq(struct e100_serial *info)
1343{
1344#ifdef SERIAL_DEBUG_INTR
1345 printk("ser_tx_irq(%d): 0\n",info->line);
1346#endif
1347 DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
1348 *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
1349}
1350
1351static inline void
1352e100_enable_serial_tx_ready_irq(struct e100_serial *info)
1353{
1354#ifdef SERIAL_DEBUG_INTR
1355 printk("ser_tx_irq(%d): 1\n",info->line);
1356 printk("**** %d = %d\n",
1357 (8+1+2*info->line),
1358 (1U << (8+1+2*info->line)));
1359#endif
1360 DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
1361 *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
1362}
1363
1364static inline void e100_enable_rx_irq(struct e100_serial *info)
1365{
1366 if (info->uses_dma_in)
1367 e100_enable_rxdma_irq(info);
1368 else
1369 e100_enable_serial_data_irq(info);
1370}
1371static inline void e100_disable_rx_irq(struct e100_serial *info)
1372{
1373 if (info->uses_dma_in)
1374 e100_disable_rxdma_irq(info);
1375 else
1376 e100_disable_serial_data_irq(info);
1377}
1378
1379#if defined(CONFIG_ETRAX_RS485)
1380/* Enable RS-485 mode on selected port. This is UGLY. */
1381static int
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001382e100_enable_rs485(struct tty_struct *tty, struct serial_rs485 *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1385
1386#if defined(CONFIG_ETRAX_RS485_ON_PA)
1387 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1388#endif
1389#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
1390 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1391 rs485_port_g_bit, 1);
1392#endif
1393#if defined(CONFIG_ETRAX_RS485_LTC1387)
1394 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1395 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 1);
1396 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1397 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 1);
1398#endif
1399
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02001400 info->rs485 = *r;
1401
1402 /* Maximum delay before RTS equal to 1000 */
1403 if (info->rs485.delay_rts_before_send >= 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 info->rs485.delay_rts_before_send = 1000;
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/* printk("rts: on send = %i, after = %i, enabled = %i",
1407 info->rs485.rts_on_send,
1408 info->rs485.rts_after_sent,
1409 info->rs485.enabled
1410 );
1411*/
1412 return 0;
1413}
1414
1415static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001416e100_write_rs485(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 const unsigned char *buf, int count)
1418{
1419 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001420 int old_value = (info->rs485.flags) & SER_RS485_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* rs485 is always implicitly enabled if we're using the ioctl()
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001423 * but it doesn't have to be set in the serial_rs485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * (to be backward compatible with old apps)
1425 * So we store, set and restore it.
1426 */
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001427 info->rs485.flags |= SER_RS485_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* rs_write now deals with RS485 if enabled */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001429 count = rs_write(tty, buf, count);
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001430 if (!old_value)
1431 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return count;
1433}
1434
1435#ifdef CONFIG_ETRAX_FAST_TIMER
1436/* Timer function to toggle RTS when using FAST_TIMER */
1437static void rs485_toggle_rts_timer_function(unsigned long data)
1438{
1439 struct e100_serial *info = (struct e100_serial *)data;
1440
1441 fast_timers_rs485[info->line].function = NULL;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001442 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1444 e100_enable_rx(info);
1445 e100_enable_rx_irq(info);
1446#endif
1447}
1448#endif
1449#endif /* CONFIG_ETRAX_RS485 */
1450
1451/*
1452 * ------------------------------------------------------------
1453 * rs_stop() and rs_start()
1454 *
1455 * This routines are called before setting or resetting tty->stopped.
1456 * They enable or disable transmitter using the XOFF registers, as necessary.
1457 * ------------------------------------------------------------
1458 */
1459
1460static void
1461rs_stop(struct tty_struct *tty)
1462{
1463 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1464 if (info) {
1465 unsigned long flags;
1466 unsigned long xoff;
1467
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001468 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
1470 CIRC_CNT(info->xmit.head,
1471 info->xmit.tail,SERIAL_XMIT_SIZE)));
1472
Takashi Iwaia88487c2008-07-16 21:54:42 +01001473 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char,
1474 STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
Alan Coxadc8d742012-07-14 15:31:47 +01001476 if (tty->termios.c_iflag & IXON ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1478 }
1479
Alan Coxd7283352008-08-04 17:21:18 +01001480 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001481 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483}
1484
1485static void
1486rs_start(struct tty_struct *tty)
1487{
1488 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1489 if (info) {
1490 unsigned long flags;
1491 unsigned long xoff;
1492
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001493 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
1495 CIRC_CNT(info->xmit.head,
1496 info->xmit.tail,SERIAL_XMIT_SIZE)));
1497 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1498 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
Alan Coxadc8d742012-07-14 15:31:47 +01001499 if (tty->termios.c_iflag & IXON ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1501 }
1502
Alan Coxd7283352008-08-04 17:21:18 +01001503 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (!info->uses_dma_out &&
1505 info->xmit.head != info->xmit.tail && info->xmit.buf)
1506 e100_enable_serial_tx_ready_irq(info);
1507
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001508 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510}
1511
1512/*
1513 * ----------------------------------------------------------------------
1514 *
1515 * Here starts the interrupt handling routines. All of the following
1516 * subroutines are declared as inline and are folded into
1517 * rs_interrupt(). They were separated out for readability's sake.
1518 *
1519 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1520 * runs with interrupts turned off. People who may want to modify
1521 * rs_interrupt() should try to keep the interrupt handler as fast as
1522 * possible. After you are done making modifications, it is not a bad
1523 * idea to do:
1524 *
1525 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1526 *
1527 * and look at the resulting assemble code in serial.s.
1528 *
1529 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1530 * -----------------------------------------------------------------------
1531 */
1532
1533/*
1534 * This routine is used by the interrupt handler to schedule
1535 * processing in the software interrupt portion of the driver.
1536 */
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001537static void rs_sched_event(struct e100_serial *info, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 if (info->event & (1 << event))
1540 return;
1541 info->event |= 1 << event;
1542 schedule_work(&info->work);
1543}
1544
1545/* The output DMA channel is free - use it to send as many chars as possible
1546 * NOTES:
1547 * We don't pay attention to info->x_char, which means if the TTY wants to
1548 * use XON/XOFF it will set info->x_char but we won't send any X char!
1549 *
1550 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1551 * buffer containing the X char, and skip updating xmit. We'd also have to
1552 * check if the last sent char was the X char when we enter this function
1553 * the next time, to avoid updating xmit with the sent X value.
1554 */
1555
1556static void
1557transmit_chars_dma(struct e100_serial *info)
1558{
1559 unsigned int c, sentl;
1560 struct etrax_dma_descr *descr;
1561
1562#ifdef CONFIG_SVINTO_SIM
1563 /* This will output too little if tail is not 0 always since
1564 * we don't reloop to send the other part. Anyway this SHOULD be a
1565 * no-op - transmit_chars_dma would never really be called during sim
1566 * since rs_write does not write into the xmit buffer then.
1567 */
1568 if (info->xmit.tail)
1569 printk("Error in serial.c:transmit_chars-dma(), tail!=0\n");
1570 if (info->xmit.head != info->xmit.tail) {
1571 SIMCOUT(info->xmit.buf + info->xmit.tail,
1572 CIRC_CNT(info->xmit.head,
1573 info->xmit.tail,
1574 SERIAL_XMIT_SIZE));
1575 info->xmit.head = info->xmit.tail; /* move back head */
1576 info->tr_running = 0;
1577 }
1578 return;
1579#endif
1580 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1581 *info->oclrintradr =
1582 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1583 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1584
1585#ifdef SERIAL_DEBUG_INTR
1586 if (info->line == SERIAL_DEBUG_LINE)
1587 printk("tc\n");
1588#endif
1589 if (!info->tr_running) {
1590 /* weirdo... we shouldn't get here! */
1591 printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
1592 return;
1593 }
1594
1595 descr = &info->tr_descr;
1596
1597 /* first get the amount of bytes sent during the last DMA transfer,
1598 and update xmit accordingly */
1599
1600 /* if the stop bit was not set, all data has been sent */
1601 if (!(descr->status & d_stop)) {
1602 sentl = descr->sw_len;
1603 } else
1604 /* otherwise we find the amount of data sent here */
1605 sentl = descr->hw_len;
1606
1607 DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
1608
1609 /* update stats */
1610 info->icount.tx += sentl;
1611
1612 /* update xmit buffer */
1613 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1614
1615 /* if there is only a few chars left in the buf, wake up the blocked
1616 write if any */
1617 if (CIRC_CNT(info->xmit.head,
1618 info->xmit.tail,
1619 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1620 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1621
1622 /* find out the largest amount of consecutive bytes we want to send now */
1623
1624 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1625
1626 /* Don't send all in one DMA transfer - divide it so we wake up
1627 * application before all is sent
1628 */
1629
1630 if (c >= 4*WAKEUP_CHARS)
1631 c = c/2;
1632
1633 if (c <= 0) {
1634 /* our job here is done, don't schedule any new DMA transfer */
1635 info->tr_running = 0;
1636
1637#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01001638 if (info->rs485.flags & SER_RS485_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /* Set a short timer to toggle RTS */
1640 start_one_shot_timer(&fast_timers_rs485[info->line],
1641 rs485_toggle_rts_timer_function,
1642 (unsigned long)info,
1643 info->char_time_usec*2,
1644 "RS-485");
1645 }
1646#endif /* RS485 */
1647 return;
1648 }
1649
1650 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1651 /* set up the descriptor correctly for output */
1652 DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
1653 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1654 descr->sw_len = c;
1655 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1656 descr->status = 0;
1657
1658 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1659 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1660
1661 /* DMA is now running (hopefully) */
1662} /* transmit_chars_dma */
1663
1664static void
1665start_transmit(struct e100_serial *info)
1666{
1667#if 0
1668 if (info->line == SERIAL_DEBUG_LINE)
1669 printk("x\n");
1670#endif
1671
1672 info->tr_descr.sw_len = 0;
1673 info->tr_descr.hw_len = 0;
1674 info->tr_descr.status = 0;
1675 info->tr_running = 1;
1676 if (info->uses_dma_out)
1677 transmit_chars_dma(info);
1678 else
1679 e100_enable_serial_tx_ready_irq(info);
1680} /* start_transmit */
1681
1682#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1683static int serial_fast_timer_started = 0;
1684static int serial_fast_timer_expired = 0;
1685static void flush_timeout_function(unsigned long data);
1686#define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
1687 unsigned long timer_flags; \
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001688 local_irq_save(timer_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (fast_timers[info->line].function == NULL) { \
1690 serial_fast_timer_started++; \
1691 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1692 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1693 start_one_shot_timer(&fast_timers[info->line], \
1694 flush_timeout_function, \
1695 (unsigned long)info, \
1696 (usec), \
1697 string); \
1698 } \
1699 else { \
1700 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1701 } \
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001702 local_irq_restore(timer_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704#define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
1705
1706#else
1707#define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
1708#define START_FLUSH_FAST_TIMER(info, string)
1709#endif
1710
1711static struct etrax_recv_buffer *
1712alloc_recv_buffer(unsigned int size)
1713{
1714 struct etrax_recv_buffer *buffer;
1715
1716 if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
1717 return NULL;
1718
1719 buffer->next = NULL;
1720 buffer->length = 0;
1721 buffer->error = TTY_NORMAL;
1722
1723 return buffer;
1724}
1725
1726static void
1727append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1728{
1729 unsigned long flags;
1730
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001731 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (!info->first_recv_buffer)
1734 info->first_recv_buffer = buffer;
1735 else
1736 info->last_recv_buffer->next = buffer;
1737
1738 info->last_recv_buffer = buffer;
1739
1740 info->recv_cnt += buffer->length;
1741 if (info->recv_cnt > info->max_recv_cnt)
1742 info->max_recv_cnt = info->recv_cnt;
1743
Jesper Nilsson77accbf2007-11-14 17:01:15 -08001744 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747static int
1748add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1749{
1750 struct etrax_recv_buffer *buffer;
1751 if (info->uses_dma_in) {
1752 if (!(buffer = alloc_recv_buffer(4)))
1753 return 0;
1754
1755 buffer->length = 1;
1756 buffer->error = flag;
1757 buffer->buffer[0] = data;
1758
1759 append_recv_buffer(info, buffer);
1760
1761 info->icount.rx++;
1762 } else {
Jiri Slaby92a19f92013-01-03 15:53:03 +01001763 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 info->icount.rx++;
1765 }
1766
1767 return 1;
1768}
1769
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001770static unsigned int handle_descr_data(struct e100_serial *info,
1771 struct etrax_dma_descr *descr,
1772 unsigned int recvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1775
1776 if (info->recv_cnt + recvl > 65536) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08001777 printk(KERN_WARNING
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001778 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return 0;
1780 }
1781
1782 buffer->length = recvl;
1783
1784 if (info->errorcode == ERRCODE_SET_BREAK)
1785 buffer->error = TTY_BREAK;
1786 info->errorcode = 0;
1787
1788 append_recv_buffer(info, buffer);
1789
1790 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001791 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 descr->buf = virt_to_phys(buffer->buffer);
1794
1795 return recvl;
1796}
1797
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001798static unsigned int handle_all_descr_data(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 struct etrax_dma_descr *descr;
1801 unsigned int recvl;
1802 unsigned int ret = 0;
1803
1804 while (1)
1805 {
1806 descr = &info->rec_descr[info->cur_rec_descr];
1807
1808 if (descr == phys_to_virt(*info->idescradr))
1809 break;
1810
1811 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1812 info->cur_rec_descr = 0;
1813
1814 /* find out how many bytes were read */
1815
1816 /* if the eop bit was not set, all data has been received */
1817 if (!(descr->status & d_eop)) {
1818 recvl = descr->sw_len;
1819 } else {
1820 /* otherwise we find the amount of data received here */
1821 recvl = descr->hw_len;
1822 }
1823
1824 /* Reset the status information */
1825 descr->status = 0;
1826
1827 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
Takashi Iwaia88487c2008-07-16 21:54:42 +01001828 if (info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned char *buf = phys_to_virt(descr->buf);
1830 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
1831 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
1832 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
1833 }
1834 );
1835
1836 /* update stats */
1837 info->icount.rx += recvl;
1838
1839 ret += handle_descr_data(info, descr, recvl);
1840 }
1841
1842 return ret;
1843}
1844
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001845static void receive_chars_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct tty_struct *tty;
1848 unsigned char rstat;
1849
1850#ifdef CONFIG_SVINTO_SIM
1851 /* No receive in the simulator. Will probably be when the rest of
1852 * the serial interface works, and this piece will just be removed.
1853 */
1854 return;
1855#endif
1856
1857 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1858 *info->iclrintradr =
1859 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1860 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1861
Takashi Iwaia88487c2008-07-16 21:54:42 +01001862 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (!tty) /* Something wrong... */
1864 return;
1865
1866#ifdef SERIAL_HANDLE_EARLY_ERRORS
1867 if (info->uses_dma_in)
1868 e100_enable_serial_data_irq(info);
1869#endif
1870
1871 if (info->errorcode == ERRCODE_INSERT_BREAK)
1872 add_char_and_flag(info, '\0', TTY_BREAK);
1873
1874 handle_all_descr_data(info);
1875
1876 /* Read the status register to detect errors */
Alan Coxd7283352008-08-04 17:21:18 +01001877 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
1879 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
1880 }
1881
1882 if (rstat & SER_ERROR_MASK) {
1883 /* If we got an error, we must reset it by reading the
1884 * data_in field
1885 */
Alan Coxd7283352008-08-04 17:21:18 +01001886 unsigned char data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 PROCSTAT(ser_stat[info->line].errors_cnt++);
1889 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1890 ((rstat & SER_ERROR_MASK) << 8) | data);
1891
1892 if (rstat & SER_PAR_ERR_MASK)
1893 add_char_and_flag(info, data, TTY_PARITY);
1894 else if (rstat & SER_OVERRUN_MASK)
1895 add_char_and_flag(info, data, TTY_OVERRUN);
1896 else if (rstat & SER_FRAMING_ERR_MASK)
1897 add_char_and_flag(info, data, TTY_FRAME);
1898 }
1899
1900 START_FLUSH_FAST_TIMER(info, "receive_chars");
1901
1902 /* Restart the receiving DMA */
1903 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1904}
1905
Adrian Bunk41c28ff2006-03-23 03:00:56 -08001906static int start_recv_dma(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct etrax_dma_descr *descr = info->rec_descr;
1909 struct etrax_recv_buffer *buffer;
1910 int i;
1911
1912 /* Set up the receiving descriptors */
1913 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
1914 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001915 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 descr[i].ctrl = d_int;
1918 descr[i].buf = virt_to_phys(buffer->buffer);
1919 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1920 descr[i].hw_len = 0;
1921 descr[i].status = 0;
1922 descr[i].next = virt_to_phys(&descr[i+1]);
1923 }
1924
1925 /* Link the last descriptor to the first */
1926 descr[i-1].next = virt_to_phys(&descr[0]);
1927
1928 /* Start with the first descriptor in the list */
1929 info->cur_rec_descr = 0;
1930
1931 /* Start the DMA */
1932 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1933 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1934
1935 /* Input DMA should be running now */
1936 return 1;
1937}
1938
1939static void
1940start_receive(struct e100_serial *info)
1941{
1942#ifdef CONFIG_SVINTO_SIM
1943 /* No receive in the simulator. Will probably be when the rest of
1944 * the serial interface works, and this piece will just be removed.
1945 */
1946 return;
1947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (info->uses_dma_in) {
1949 /* reset the input dma channel to be sure it works */
1950
1951 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1952 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1953 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1954
1955 start_recv_dma(info);
1956 }
1957}
1958
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960/* the bits in the MASK2 register are laid out like this:
1961 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1962 where I is the input channel and O is the output channel for the port.
1963 info->irq is the bit number for the DMAO_DESCR so to check the others we
1964 shift info->irq to the left.
1965*/
1966
1967/* dma output channel interrupt handler
1968 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1969 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1970*/
1971
1972static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001973tr_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 struct e100_serial *info;
1976 unsigned long ireg;
1977 int i;
1978 int handled = 0;
1979
1980#ifdef CONFIG_SVINTO_SIM
1981 /* No receive in the simulator. Will probably be when the rest of
1982 * the serial interface works, and this piece will just be removed.
1983 */
1984 {
1985 const char *s = "What? tr_interrupt in simulator??\n";
1986 SIMCOUT(s,strlen(s));
1987 }
1988 return IRQ_HANDLED;
1989#endif
1990
1991 /* find out the line that caused this irq and get it from rs_table */
1992
1993 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1994
1995 for (i = 0; i < NR_PORTS; i++) {
1996 info = rs_table + i;
1997 if (!info->enabled || !info->uses_dma_out)
1998 continue;
1999 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2000 if (ireg & info->irq) {
2001 handled = 1;
2002 /* we can send a new dma bunch. make it so. */
2003 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
2004 /* Read jiffies_usec first,
2005 * we want this time to be as late as possible
2006 */
2007 PROCSTAT(ser_stat[info->line].tx_dma_ints++);
2008 info->last_tx_active_usec = GET_JIFFIES_USEC();
2009 info->last_tx_active = jiffies;
2010 transmit_chars_dma(info);
2011 }
2012
2013 /* FIXME: here we should really check for a change in the
2014 status lines and if so call status_handle(info) */
2015 }
2016 return IRQ_RETVAL(handled);
2017} /* tr_interrupt */
2018
2019/* dma input channel interrupt handler */
2020
2021static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01002022rec_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 struct e100_serial *info;
2025 unsigned long ireg;
2026 int i;
2027 int handled = 0;
2028
2029#ifdef CONFIG_SVINTO_SIM
2030 /* No receive in the simulator. Will probably be when the rest of
2031 * the serial interface works, and this piece will just be removed.
2032 */
2033 {
2034 const char *s = "What? rec_interrupt in simulator??\n";
2035 SIMCOUT(s,strlen(s));
2036 }
2037 return IRQ_HANDLED;
2038#endif
2039
2040 /* find out the line that caused this irq and get it from rs_table */
2041
2042 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2043
2044 for (i = 0; i < NR_PORTS; i++) {
2045 info = rs_table + i;
2046 if (!info->enabled || !info->uses_dma_in)
2047 continue;
2048 /* check for both dma_eop and dma_descr for the input dma channel */
2049 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
2050 handled = 1;
2051 /* we have received something */
2052 receive_chars_dma(info);
2053 }
2054
2055 /* FIXME: here we should really check for a change in the
2056 status lines and if so call status_handle(info) */
2057 }
2058 return IRQ_RETVAL(handled);
2059} /* rec_interrupt */
2060
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002061static int force_eop_if_needed(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 /* We check data_avail bit to determine if data has
2064 * arrived since last time
2065 */
Alan Coxd7283352008-08-04 17:21:18 +01002066 unsigned char rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 /* error or datavail? */
2069 if (rstat & SER_ERROR_MASK) {
2070 /* Some error has occurred. If there has been valid data, an
2071 * EOP interrupt will be made automatically. If no data, the
2072 * normal ser_interrupt should be enabled and handle it.
2073 * So do nothing!
2074 */
2075 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
2076 rstat | (info->line << 8));
2077 return 0;
2078 }
2079
2080 if (rstat & SER_DATA_AVAIL_MASK) {
2081 /* Ok data, no error, count it */
2082 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
2083 rstat | (info->line << 8)));
2084 /* Read data to clear status flags */
Alan Coxd7283352008-08-04 17:21:18 +01002085 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 info->forced_eop = 0;
2088 START_FLUSH_FAST_TIMER(info, "magic");
2089 return 0;
2090 }
2091
2092 /* hit the timeout, force an EOP for the input
2093 * dma channel if we haven't already
2094 */
2095 if (!info->forced_eop) {
2096 info->forced_eop = 1;
2097 PROCSTAT(ser_stat[info->line].timeout_flush_cnt++);
2098 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
2099 FORCE_EOP(info);
2100 }
2101
2102 return 1;
2103}
2104
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002105static void flush_to_flip_buffer(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct etrax_recv_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002110 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Alan Cox2090ab02007-10-16 01:26:38 -07002112 while ((buffer = info->first_recv_buffer) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 unsigned int count = buffer->length;
2114
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002115 tty_insert_flip_string(&info->port, buffer->buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 info->recv_cnt -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (count == buffer->length) {
2119 info->first_recv_buffer = buffer->next;
2120 kfree(buffer);
2121 } else {
2122 buffer->length -= count;
2123 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2124 buffer->error = TTY_NORMAL;
2125 }
2126 }
2127
2128 if (!info->first_recv_buffer)
2129 info->last_recv_buffer = NULL;
2130
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002131 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002133 /* This includes a check for low-latency */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002134 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002137static void check_flush_timeout(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 /* Flip what we've got (if we can) */
2140 flush_to_flip_buffer(info);
2141
2142 /* We might need to flip later, but not to fast
2143 * since the system is busy processing input... */
2144 if (info->first_recv_buffer)
2145 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2146
2147 /* Force eop last, since data might have come while we're processing
2148 * and if we started the slow timer above, we won't start a fast
2149 * below.
2150 */
2151 force_eop_if_needed(info);
2152}
2153
2154#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2155static void flush_timeout_function(unsigned long data)
2156{
2157 struct e100_serial *info = (struct e100_serial *)data;
2158
2159 fast_timers[info->line].function = NULL;
2160 serial_fast_timer_expired++;
2161 TIMERD(DEBUG_LOG(info->line, "flush_timout %i ", info->line));
2162 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2163 check_flush_timeout(info);
2164}
2165
2166#else
2167
2168/* dma fifo/buffer timeout handler
2169 forces an end-of-packet for the dma input channel if no chars
2170 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2171*/
2172
2173static struct timer_list flush_timer;
2174
2175static void
2176timed_flush_handler(unsigned long ptr)
2177{
2178 struct e100_serial *info;
2179 int i;
2180
2181#ifdef CONFIG_SVINTO_SIM
2182 return;
2183#endif
2184
2185 for (i = 0; i < NR_PORTS; i++) {
2186 info = rs_table + i;
2187 if (info->uses_dma_in)
2188 check_flush_timeout(info);
2189 }
2190
2191 /* restart flush timer */
2192 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2193}
2194#endif
2195
2196#ifdef SERIAL_HANDLE_EARLY_ERRORS
2197
2198/* If there is an error (ie break) when the DMA is running and
2199 * there are no bytes in the fifo the DMA is stopped and we get no
2200 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2201 * transfer, and if it is without error we can turn the serial
2202 * interrupts off.
2203 */
2204
2205/*
2206BREAK handling on ETRAX 100:
2207ETRAX will generate interrupt although there is no stop bit between the
2208characters.
2209
2210Depending on how long the break sequence is, the end of the breaksequence
2211will look differently:
2212| indicates start/end of a character.
2213
2214B= Break character (0x00) with framing error.
2215E= Error byte with parity error received after B characters.
2216F= "Faked" valid byte received immediately after B characters.
2217V= Valid byte
2218
22191.
2220 B BL ___________________________ V
2221.._|__________|__________| |valid data |
2222
2223Multiple frame errors with data == 0x00 (B),
2224the timing matches up "perfectly" so no extra ending char is detected.
2225The RXD pin is 1 in the last interrupt, in that case
2226we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2227know if another byte will come and this really is case 2. below
2228(e.g F=0xFF or 0xFE)
2229If RXD pin is 0 we can expect another character (see 2. below).
2230
2231
22322.
2233
2234 B B E or F__________________..__ V
2235.._|__________|__________|______ | |valid data
2236 "valid" or
2237 parity error
2238
2239Multiple frame errors with data == 0x00 (B),
2240but the part of the break trigs is interpreted as a start bit (and possibly
2241some 0 bits followed by a number of 1 bits and a stop bit).
2242Depending on parity settings etc. this last character can be either
2243a fake "valid" char (F) or have a parity error (E).
2244
2245If the character is valid it will be put in the buffer,
2246we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2247will set the flags so the tty will handle it,
2248if it's an error byte it will not be put in the buffer
2249and we set info->errorcode = ERRCODE_INSERT_BREAK.
2250
2251To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2252of the last faulty char (B) and compares it with the current time:
2253If the time elapsed time is less then 2*char_time_usec we will assume
2254it's a faked F char and not a Valid char and set
2255info->errorcode = ERRCODE_SET_BREAK.
2256
2257Flaws in the above solution:
2258~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2259We use the timer to distinguish a F character from a V character,
2260if a V character is to close after the break we might make the wrong decision.
2261
2262TODO: The break will be delayed until an F or V character is received.
2263
2264*/
2265
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002266static
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267struct e100_serial * handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
2268{
2269 unsigned long data_read;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* Read data and status at the same time */
Alan Coxd7283352008-08-04 17:21:18 +01002272 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273more_data:
2274 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2275 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2276 }
2277 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2278
2279 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2280 IO_MASK(R_SERIAL0_READ, par_err) |
2281 IO_MASK(R_SERIAL0_READ, overrun) )) {
2282 /* An error */
2283 info->last_rx_active_usec = GET_JIFFIES_USEC();
2284 info->last_rx_active = jiffies;
2285 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2286 DLOG_INT_TRIG(
2287 if (!log_int_trig1_pos) {
2288 log_int_trig1_pos = log_int_pos;
2289 log_int(rdpc(), 0, 0);
2290 }
2291 );
2292
2293
2294 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2295 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2296 /* Most likely a break, but we get interrupts over and
2297 * over again.
2298 */
2299
2300 if (!info->break_detected_cnt) {
2301 DEBUG_LOG(info->line, "#BRK start\n", 0);
2302 }
2303 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2304 /* The RX pin is high now, so the break
2305 * must be over, but....
2306 * we can't really know if we will get another
2307 * last byte ending the break or not.
2308 * And we don't know if the byte (if any) will
2309 * have an error or look valid.
2310 */
2311 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2312 info->errorcode = ERRCODE_INSERT_BREAK;
2313 }
2314 info->break_detected_cnt++;
2315 } else {
2316 /* The error does not look like a break, but could be
2317 * the end of one
2318 */
2319 if (info->break_detected_cnt) {
2320 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2321 info->errorcode = ERRCODE_INSERT_BREAK;
2322 } else {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002323 unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
2324 data_in, data_read);
2325 char flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (info->errorcode == ERRCODE_INSERT_BREAK) {
Jiri Slaby92a19f92013-01-03 15:53:03 +01002327 tty_insert_flip_char(&info->port, 0, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 info->icount.rx++;
2329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2332 info->icount.parity++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002333 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2335 info->icount.overrun++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002336 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2338 info->icount.frame++;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002339 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01002341 tty_insert_flip_char(&info->port, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 info->errorcode = 0;
2343 }
2344 info->break_detected_cnt = 0;
2345 }
2346 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2347 /* No error */
2348 DLOG_INT_TRIG(
2349 if (!log_int_trig1_pos) {
2350 if (log_int_pos >= log_int_size) {
2351 log_int_pos = 0;
2352 }
2353 log_int_trig0_pos = log_int_pos;
2354 log_int(rdpc(), 0, 0);
2355 }
2356 );
Jiri Slaby92a19f92013-01-03 15:53:03 +01002357 tty_insert_flip_char(&info->port,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002358 IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
2359 TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 } else {
2361 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2362 }
2363
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 info->icount.rx++;
Alan Coxd7283352008-08-04 17:21:18 +01002366 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2368 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2369 goto more_data;
2370 }
2371
Jiri Slaby2e124b42013-01-03 15:53:06 +01002372 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return info;
2374}
2375
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002376static struct e100_serial* handle_ser_rx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 unsigned char rstat;
2379
2380#ifdef SERIAL_DEBUG_INTR
2381 printk("Interrupt from serport %d\n", i);
2382#endif
2383/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2384 if (!info->uses_dma_in) {
2385 return handle_ser_rx_interrupt_no_dma(info);
2386 }
2387 /* DMA is used */
Alan Coxd7283352008-08-04 17:21:18 +01002388 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2390 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2391 }
2392
2393 if (rstat & SER_ERROR_MASK) {
2394 unsigned char data;
2395
2396 info->last_rx_active_usec = GET_JIFFIES_USEC();
2397 info->last_rx_active = jiffies;
2398 /* If we got an error, we must reset it by reading the
2399 * data_in field
2400 */
Alan Coxd7283352008-08-04 17:21:18 +01002401 data = info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2403 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2404 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2405 /* Most likely a break, but we get interrupts over and
2406 * over again.
2407 */
2408
2409 if (!info->break_detected_cnt) {
2410 DEBUG_LOG(info->line, "#BRK start\n", 0);
2411 }
2412 if (rstat & SER_RXD_MASK) {
2413 /* The RX pin is high now, so the break
2414 * must be over, but....
2415 * we can't really know if we will get another
2416 * last byte ending the break or not.
2417 * And we don't know if the byte (if any) will
2418 * have an error or look valid.
2419 */
2420 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2421 info->errorcode = ERRCODE_INSERT_BREAK;
2422 }
2423 info->break_detected_cnt++;
2424 } else {
2425 /* The error does not look like a break, but could be
2426 * the end of one
2427 */
2428 if (info->break_detected_cnt) {
2429 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2430 info->errorcode = ERRCODE_INSERT_BREAK;
2431 } else {
2432 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2433 info->icount.brk++;
2434 add_char_and_flag(info, '\0', TTY_BREAK);
2435 }
2436
2437 if (rstat & SER_PAR_ERR_MASK) {
2438 info->icount.parity++;
2439 add_char_and_flag(info, data, TTY_PARITY);
2440 } else if (rstat & SER_OVERRUN_MASK) {
2441 info->icount.overrun++;
2442 add_char_and_flag(info, data, TTY_OVERRUN);
2443 } else if (rstat & SER_FRAMING_ERR_MASK) {
2444 info->icount.frame++;
2445 add_char_and_flag(info, data, TTY_FRAME);
2446 }
2447
2448 info->errorcode = 0;
2449 }
2450 info->break_detected_cnt = 0;
2451 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2452 ((rstat & SER_ERROR_MASK) << 8) | data);
2453 }
2454 PROCSTAT(ser_stat[info->line].early_errors_cnt++);
2455 } else { /* It was a valid byte, now let the DMA do the rest */
2456 unsigned long curr_time_u = GET_JIFFIES_USEC();
2457 unsigned long curr_time = jiffies;
2458
2459 if (info->break_detected_cnt) {
2460 /* Detect if this character is a new valid char or the
2461 * last char in a break sequence: If LSBits are 0 and
2462 * MSBits are high AND the time is close to the
2463 * previous interrupt we should discard it.
2464 */
2465 long elapsed_usec =
2466 (curr_time - info->last_rx_active) * (1000000/HZ) +
2467 curr_time_u - info->last_rx_active_usec;
2468 if (elapsed_usec < 2*info->char_time_usec) {
2469 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2470 /* Report as BREAK (error) and let
2471 * receive_chars_dma() handle it
2472 */
2473 info->errorcode = ERRCODE_SET_BREAK;
2474 } else {
2475 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2476 }
2477 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2478 }
2479
2480#ifdef SERIAL_DEBUG_INTR
2481 printk("** OK, disabling ser_interrupts\n");
2482#endif
2483 e100_disable_serial_data_irq(info);
2484 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2485 info->break_detected_cnt = 0;
2486
2487 PROCSTAT(ser_stat[info->line].ser_ints_ok_cnt++);
2488 }
2489 /* Restarting the DMA never hurts */
2490 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2491 START_FLUSH_FAST_TIMER(info, "ser_int");
2492 return info;
2493} /* handle_ser_rx_interrupt */
2494
Adrian Bunk41c28ff2006-03-23 03:00:56 -08002495static void handle_ser_tx_interrupt(struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 unsigned long flags;
2498
2499 if (info->x_char) {
2500 unsigned char rstat;
2501 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002502 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002503 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2505
Alan Coxd7283352008-08-04 17:21:18 +01002506 info->ioport[REG_TR_DATA] = info->x_char;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 info->icount.tx++;
2508 info->x_char = 0;
2509 /* We must enable since it is disabled in ser_interrupt */
2510 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002511 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return;
2513 }
2514 if (info->uses_dma_out) {
2515 unsigned char rstat;
2516 int i;
2517 /* We only use normal tx interrupt when sending x_char */
2518 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002519 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002520 rstat = info->ioport[REG_STATUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2522 e100_disable_serial_tx_ready_irq(info);
Takashi Iwaia88487c2008-07-16 21:54:42 +01002523 if (info->port.tty->stopped)
2524 rs_stop(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /* Enable the DMA channel and tell it to continue */
2526 e100_enable_txdma_channel(info);
2527 /* Wait 12 cycles before doing the DMA command */
2528 for(i = 6; i > 0; i--)
2529 nop();
2530
2531 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002532 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return;
2534 }
2535 /* Normal char-by-char interrupt */
2536 if (info->xmit.head == info->xmit.tail
Jiri Slabyee797062013-03-07 13:12:34 +01002537 || info->port.tty->stopped) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01002538 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
2539 info->port.tty->stopped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 e100_disable_serial_tx_ready_irq(info);
2541 info->tr_running = 0;
2542 return;
2543 }
2544 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
2545 /* Send a byte, rs485 timing is critical so turn of ints */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002546 local_irq_save(flags);
Alan Coxd7283352008-08-04 17:21:18 +01002547 info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
2549 info->icount.tx++;
2550 if (info->xmit.head == info->xmit.tail) {
2551#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01002552 if (info->rs485.flags & SER_RS485_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /* Set a short timer to toggle RTS */
2554 start_one_shot_timer(&fast_timers_rs485[info->line],
2555 rs485_toggle_rts_timer_function,
2556 (unsigned long)info,
2557 info->char_time_usec*2,
2558 "RS-485");
2559 }
2560#endif /* RS485 */
2561 info->last_tx_active_usec = GET_JIFFIES_USEC();
2562 info->last_tx_active = jiffies;
2563 e100_disable_serial_tx_ready_irq(info);
2564 info->tr_running = 0;
2565 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
2566 } else {
2567 /* We must enable since it is disabled in ser_interrupt */
2568 e100_enable_serial_tx_ready_irq(info);
2569 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002570 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 if (CIRC_CNT(info->xmit.head,
2573 info->xmit.tail,
2574 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2575 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2576
2577} /* handle_ser_tx_interrupt */
2578
2579/* result of time measurements:
2580 * RX duration 54-60 us when doing something, otherwise 6-9 us
2581 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2582 */
2583static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01002584ser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 static volatile int tx_started = 0;
2587 struct e100_serial *info;
2588 int i;
2589 unsigned long flags;
2590 unsigned long irq_mask1_rd;
2591 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
2592 int handled = 0;
2593 static volatile unsigned long reentered_ready_mask = 0;
2594
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002595 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 irq_mask1_rd = *R_IRQ_MASK1_RD;
2597 /* First handle all rx interrupts with ints disabled */
2598 info = rs_table;
2599 irq_mask1_rd &= e100_ser_int_mask;
2600 for (i = 0; i < NR_PORTS; i++) {
2601 /* Which line caused the data irq? */
2602 if (irq_mask1_rd & data_mask) {
2603 handled = 1;
2604 handle_ser_rx_interrupt(info);
2605 }
2606 info += 1;
2607 data_mask <<= 2;
2608 }
2609 /* Handle tx interrupts with interrupts enabled so we
2610 * can take care of new data interrupts while transmitting
2611 * We protect the tx part with the tx_started flag.
2612 * We disable the tr_ready interrupts we are about to handle and
2613 * unblock the serial interrupt so new serial interrupts may come.
2614 *
2615 * If we get a new interrupt:
2616 * - it migth be due to synchronous serial ports.
2617 * - serial irq will be blocked by general irq handler.
2618 * - async data will be handled above (sync will be ignored).
2619 * - tx_started flag will prevent us from trying to send again and
2620 * we will exit fast - no need to unblock serial irq.
2621 * - Next (sync) serial interrupt handler will be runned with
2622 * disabled interrupt due to restore_flags() at end of function,
2623 * so sync handler will not be preempted or reentered.
2624 */
2625 if (!tx_started) {
2626 unsigned long ready_mask;
2627 unsigned long
2628 tx_started = 1;
2629 /* Only the tr_ready interrupts left */
2630 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2631 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2632 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2633 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2634 while (irq_mask1_rd) {
2635 /* Disable those we are about to handle */
2636 *R_IRQ_MASK1_CLR = irq_mask1_rd;
2637 /* Unblock the serial interrupt */
2638 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
2639
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002640 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
2642 info = rs_table;
2643 for (i = 0; i < NR_PORTS; i++) {
2644 /* Which line caused the ready irq? */
2645 if (irq_mask1_rd & ready_mask) {
2646 handled = 1;
2647 handle_ser_tx_interrupt(info);
2648 }
2649 info += 1;
2650 ready_mask <<= 2;
2651 }
2652 /* handle_ser_tx_interrupt enables tr_ready interrupts */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002653 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* Handle reentered TX interrupt */
2655 irq_mask1_rd = reentered_ready_mask;
2656 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002657 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 tx_started = 0;
2659 } else {
2660 unsigned long ready_mask;
2661 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2662 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2663 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2664 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2665 if (ready_mask) {
2666 reentered_ready_mask |= ready_mask;
2667 /* Disable those we are about to handle */
2668 *R_IRQ_MASK1_CLR = ready_mask;
2669 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
2670 }
2671 }
2672
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002673 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return IRQ_RETVAL(handled);
2675} /* ser_interrupt */
2676#endif
2677
2678/*
2679 * -------------------------------------------------------------------
2680 * Here ends the serial interrupt routines.
2681 * -------------------------------------------------------------------
2682 */
2683
2684/*
2685 * This routine is used to handle the "bottom half" processing for the
2686 * serial driver, known also the "software interrupt" processing.
2687 * This processing is done at the kernel interrupt level, after the
2688 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2689 * is where time-consuming activities which can not be done in the
2690 * interrupt driver proper are done; the interrupt driver schedules
2691 * them using rs_sched_event(), and they get done here.
2692 */
2693static void
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002694do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002696 struct e100_serial *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct tty_struct *tty;
2698
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002699 info = container_of(work, struct e100_serial, work);
2700
Takashi Iwaia88487c2008-07-16 21:54:42 +01002701 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (!tty)
2703 return;
2704
Jiri Slabyb963a842007-02-10 01:44:55 -08002705 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
2706 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709static int
2710startup(struct e100_serial * info)
2711{
2712 unsigned long flags;
2713 unsigned long xmit_page;
2714 int i;
2715
2716 xmit_page = get_zeroed_page(GFP_KERNEL);
2717 if (!xmit_page)
2718 return -ENOMEM;
2719
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002720 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* if it was already initialized, skip this */
2723
2724 if (info->flags & ASYNC_INITIALIZED) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002725 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 free_page(xmit_page);
2727 return 0;
2728 }
2729
2730 if (info->xmit.buf)
2731 free_page(xmit_page);
2732 else
2733 info->xmit.buf = (unsigned char *) xmit_page;
2734
2735#ifdef SERIAL_DEBUG_OPEN
2736 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
2737#endif
2738
2739#ifdef CONFIG_SVINTO_SIM
2740 /* Bits and pieces collected from below. Better to have them
2741 in one ifdef:ed clause than to mix in a lot of ifdefs,
2742 right? */
Takashi Iwaia88487c2008-07-16 21:54:42 +01002743 if (info->port.tty)
2744 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 info->xmit.head = info->xmit.tail = 0;
2747 info->first_recv_buffer = info->last_recv_buffer = NULL;
2748 info->recv_cnt = info->max_recv_cnt = 0;
2749
2750 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2751 info->rec_descr[i].buf = NULL;
2752
2753 /* No real action in the simulator, but may set info important
2754 to ioctl. */
2755 change_speed(info);
2756#else
2757
2758 /*
2759 * Clear the FIFO buffers and disable them
2760 * (they will be reenabled in change_speed())
2761 */
2762
2763 /*
2764 * Reset the DMA channels and make sure their interrupts are cleared
2765 */
2766
2767 if (info->dma_in_enabled) {
2768 info->uses_dma_in = 1;
2769 e100_enable_rxdma_channel(info);
2770
2771 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2772
2773 /* Wait until reset cycle is complete */
2774 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2775 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2776
2777 /* Make sure the irqs are cleared */
2778 *info->iclrintradr =
2779 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2780 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2781 } else {
2782 e100_disable_rxdma_channel(info);
2783 }
2784
2785 if (info->dma_out_enabled) {
2786 info->uses_dma_out = 1;
2787 e100_enable_txdma_channel(info);
2788 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2789
2790 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2791 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2792
2793 /* Make sure the irqs are cleared */
2794 *info->oclrintradr =
2795 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2796 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2797 } else {
2798 e100_disable_txdma_channel(info);
2799 }
2800
Takashi Iwaia88487c2008-07-16 21:54:42 +01002801 if (info->port.tty)
2802 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 info->xmit.head = info->xmit.tail = 0;
2805 info->first_recv_buffer = info->last_recv_buffer = NULL;
2806 info->recv_cnt = info->max_recv_cnt = 0;
2807
2808 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2809 info->rec_descr[i].buf = 0;
2810
2811 /*
2812 * and set the speed and other flags of the serial port
2813 * this will start the rx/tx as well
2814 */
2815#ifdef SERIAL_HANDLE_EARLY_ERRORS
2816 e100_enable_serial_data_irq(info);
2817#endif
2818 change_speed(info);
2819
2820 /* dummy read to reset any serial errors */
2821
Alan Coxd7283352008-08-04 17:21:18 +01002822 (void)info->ioport[REG_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 /* enable the interrupts */
2825 if (info->uses_dma_out)
2826 e100_enable_txdma_irq(info);
2827
2828 e100_enable_rx_irq(info);
2829
2830 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2831
2832 /* setup the dma input descriptor and start dma */
2833
2834 start_receive(info);
2835
2836 /* for safety, make sure the descriptors last result is 0 bytes written */
2837
2838 info->tr_descr.sw_len = 0;
2839 info->tr_descr.hw_len = 0;
2840 info->tr_descr.status = 0;
2841
2842 /* enable RTS/DTR last */
2843
2844 e100_rts(info, 1);
2845 e100_dtr(info, 1);
2846
2847#endif /* CONFIG_SVINTO_SIM */
2848
2849 info->flags |= ASYNC_INITIALIZED;
2850
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002851 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return 0;
2853}
2854
2855/*
2856 * This routine will shutdown a serial port; interrupts are disabled, and
2857 * DTR is dropped if the hangup on close termio flag is on.
2858 */
2859static void
2860shutdown(struct e100_serial * info)
2861{
2862 unsigned long flags;
2863 struct etrax_dma_descr *descr = info->rec_descr;
2864 struct etrax_recv_buffer *buffer;
2865 int i;
2866
2867#ifndef CONFIG_SVINTO_SIM
2868 /* shut down the transmitter and receiver */
2869 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
2870 e100_disable_rx(info);
Alan Coxd7283352008-08-04 17:21:18 +01002871 info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 /* disable interrupts, reset dma channels */
2874 if (info->uses_dma_in) {
2875 e100_disable_rxdma_irq(info);
2876 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2877 info->uses_dma_in = 0;
2878 } else {
2879 e100_disable_serial_data_irq(info);
2880 }
2881
2882 if (info->uses_dma_out) {
2883 e100_disable_txdma_irq(info);
2884 info->tr_running = 0;
2885 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2886 info->uses_dma_out = 0;
2887 } else {
2888 e100_disable_serial_tx_ready_irq(info);
2889 info->tr_running = 0;
2890 }
2891
2892#endif /* CONFIG_SVINTO_SIM */
2893
2894 if (!(info->flags & ASYNC_INITIALIZED))
2895 return;
2896
2897#ifdef SERIAL_DEBUG_OPEN
2898 printk("Shutting down serial port %d (irq %d)....\n", info->line,
2899 info->irq);
2900#endif
2901
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002902 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 if (info->xmit.buf) {
2905 free_page((unsigned long)info->xmit.buf);
2906 info->xmit.buf = NULL;
2907 }
2908
2909 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2910 if (descr[i].buf) {
2911 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2912 kfree(buffer);
2913 descr[i].buf = 0;
2914 }
2915
Alan Coxadc8d742012-07-14 15:31:47 +01002916 if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /* hang up DTR and RTS if HUPCL is enabled */
2918 e100_dtr(info, 0);
2919 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2920 }
2921
Takashi Iwaia88487c2008-07-16 21:54:42 +01002922 if (info->port.tty)
2923 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 info->flags &= ~ASYNC_INITIALIZED;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08002926 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
2929
2930/* change baud rate and other assorted parameters */
2931
2932static void
2933change_speed(struct e100_serial *info)
2934{
2935 unsigned int cflag;
2936 unsigned long xoff;
2937 unsigned long flags;
2938 /* first some safety checks */
2939
Alan Coxadc8d742012-07-14 15:31:47 +01002940 if (!info->port.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return;
Alan Coxd7283352008-08-04 17:21:18 +01002942 if (!info->ioport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return;
2944
Alan Coxadc8d742012-07-14 15:31:47 +01002945 cflag = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 /* possibly, the tx/rx should be disabled first to do this safely */
2948
2949 /* change baud-rate and write it to the hardware */
2950 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
2951 /* Special baudrate */
2952 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2953 unsigned long alt_source =
2954 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2955 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2956 /* R_ALT_SER_BAUDRATE selects the source */
2957 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2958 (unsigned long)info->baud_base, info->custom_divisor));
2959 if (info->baud_base == SERIAL_PRESCALE_BASE) {
2960 /* 0, 2-65535 (0=65536) */
2961 u16 divisor = info->custom_divisor;
2962 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2963 /* baudrate is 3.125MHz/custom_divisor */
2964 alt_source =
2965 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
2966 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
2967 alt_source = 0x11;
2968 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
2969 *R_SERIAL_PRESCALE = divisor;
2970 info->baud = SERIAL_PRESCALE_BASE/divisor;
2971 }
2972#ifdef CONFIG_ETRAX_EXTERN_PB6CLK_ENABLED
2973 else if ((info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8 &&
2974 info->custom_divisor == 1) ||
2975 (info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ &&
2976 info->custom_divisor == 8)) {
2977 /* ext_clk selected */
2978 alt_source =
2979 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, extern) |
2980 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, extern);
2981 DBAUD(printk("using external baudrate: %lu\n", CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8));
2982 info->baud = CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8;
2983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984#endif
2985 else
2986 {
2987 /* Bad baudbase, we don't support using timer0
2988 * for baudrate.
2989 */
2990 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
2991 (unsigned long)info->baud_base, info->custom_divisor);
2992 }
2993 r_alt_ser_baudrate_shadow &= ~mask;
2994 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
2995 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
2996 } else {
2997 /* Normal baudrate */
2998 /* Make sure we use normal baudrate */
2999 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
3000 unsigned long alt_source =
3001 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
3002 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
3003 r_alt_ser_baudrate_shadow &= ~mask;
3004 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3005#ifndef CONFIG_SVINTO_SIM
3006 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3007#endif /* CONFIG_SVINTO_SIM */
3008
3009 info->baud = cflag_to_baud(cflag);
3010#ifndef CONFIG_SVINTO_SIM
Alan Coxd7283352008-08-04 17:21:18 +01003011 info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012#endif /* CONFIG_SVINTO_SIM */
3013 }
3014
3015#ifndef CONFIG_SVINTO_SIM
3016 /* start with default settings and then fill in changes */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003017 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /* 8 bit, no/even parity */
3019 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
3020 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
3021 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
3022
3023 /* 8 bit, no/even parity, 1 stop bit, no cts */
3024 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
3025 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
3026 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
3027 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
3028 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
3029
3030 if ((cflag & CSIZE) == CS7) {
3031 /* set 7 bit mode */
3032 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
3033 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
3034 }
3035
3036 if (cflag & CSTOPB) {
3037 /* set 2 stop bit mode */
3038 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
3039 }
3040
3041 if (cflag & PARENB) {
3042 /* enable parity */
3043 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
3044 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
3045 }
3046
3047 if (cflag & CMSPAR) {
3048 /* enable stick parity, PARODD mean Mark which matches ETRAX */
3049 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
3050 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
3051 }
3052 if (cflag & PARODD) {
3053 /* set odd parity (or Mark if CMSPAR) */
3054 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
3055 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
3056 }
3057
3058 if (cflag & CRTSCTS) {
3059 /* enable automatic CTS handling */
3060 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
3061 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
3062 }
3063
3064 /* make sure the tx and rx are enabled */
3065
3066 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
3067 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
3068
3069 /* actually write the control regs to the hardware */
3070
Alan Coxd7283352008-08-04 17:21:18 +01003071 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
3072 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003073 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
Alan Coxadc8d742012-07-14 15:31:47 +01003075 if (info->port.tty->termios.c_iflag & IXON ) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01003076 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
3077 STOP_CHAR(info->port.tty)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
3079 }
3080
Alan Coxd7283352008-08-04 17:21:18 +01003081 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003082 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083#endif /* !CONFIG_SVINTO_SIM */
3084
3085 update_char_time(info);
3086
3087} /* change_speed */
3088
3089/* start transmitting chars NOW */
3090
3091static void
3092rs_flush_chars(struct tty_struct *tty)
3093{
3094 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3095 unsigned long flags;
3096
3097 if (info->tr_running ||
3098 info->xmit.head == info->xmit.tail ||
3099 tty->stopped ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 !info->xmit.buf)
3101 return;
3102
3103#ifdef SERIAL_DEBUG_FLOW
3104 printk("rs_flush_chars\n");
3105#endif
3106
3107 /* this protection might not exactly be necessary here */
3108
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003109 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 start_transmit(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003111 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003114static int rs_raw_write(struct tty_struct *tty,
Adrian Bunk41c28ff2006-03-23 03:00:56 -08003115 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 int c, ret = 0;
3118 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3119 unsigned long flags;
3120
3121 /* first some sanity checks */
3122
Cong Dingf938f372013-01-16 23:30:44 +01003123 if (!info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return 0;
3125
3126#ifdef SERIAL_DEBUG_DATA
3127 if (info->line == SERIAL_DEBUG_LINE)
3128 printk("rs_raw_write (%d), status %d\n",
Alan Coxd7283352008-08-04 17:21:18 +01003129 count, info->ioport[REG_STATUS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130#endif
3131
3132#ifdef CONFIG_SVINTO_SIM
3133 /* Really simple. The output is here and now. */
3134 SIMCOUT(buf, count);
3135 return count;
3136#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003137 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
3139 DFLOW(DEBUG_LOG(info->line, "ldisc %i\n", tty->ldisc.chars_in_buffer(tty)));
3140
3141
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003142 /* The local_irq_disable/restore_flags pairs below are needed
3143 * because the DMA interrupt handler moves the info->xmit values.
3144 * the memcpy needs to be in the critical region unfortunately,
3145 * because we need to read xmit values, memcpy, write xmit values
3146 * in one atomic operation... this could perhaps be avoided by
3147 * more clever design.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003149 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 while (count) {
3151 c = CIRC_SPACE_TO_END(info->xmit.head,
3152 info->xmit.tail,
3153 SERIAL_XMIT_SIZE);
3154
3155 if (count < c)
3156 c = count;
3157 if (c <= 0)
3158 break;
3159
3160 memcpy(info->xmit.buf + info->xmit.head, buf, c);
3161 info->xmit.head = (info->xmit.head + c) &
3162 (SERIAL_XMIT_SIZE-1);
3163 buf += c;
3164 count -= c;
3165 ret += c;
3166 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003167 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 /* enable transmitter if not running, unless the tty is stopped
3170 * this does not need IRQ protection since if tr_running == 0
3171 * the IRQ's are not running anyway for this port.
3172 */
3173 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3174
3175 if (info->xmit.head != info->xmit.tail &&
3176 !tty->stopped &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 !info->tr_running) {
3178 start_transmit(info);
3179 }
3180
3181 return ret;
3182} /* raw_raw_write() */
3183
3184static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003185rs_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 const unsigned char *buf, int count)
3187{
3188#if defined(CONFIG_ETRAX_RS485)
3189 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3190
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003191 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 {
3193 /* If we are in RS-485 mode, we need to toggle RTS and disable
3194 * the receiver before initiating a DMA transfer
3195 */
3196#ifdef CONFIG_ETRAX_FAST_TIMER
3197 /* Abort any started timer */
3198 fast_timers_rs485[info->line].function = NULL;
3199 del_fast_timer(&fast_timers_rs485[info->line]);
3200#endif
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003201 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3203 e100_disable_rx(info);
3204 e100_enable_rx_irq(info);
3205#endif
Claudio Scordino93f33502011-11-09 15:51:49 +01003206 if (info->rs485.delay_rts_before_send > 0)
3207 msleep(info->rs485.delay_rts_before_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
3209#endif /* CONFIG_ETRAX_RS485 */
3210
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003211 count = rs_raw_write(tty, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003214 if (info->rs485.flags & SER_RS485_ENABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 {
3216 unsigned int val;
3217 /* If we are in RS-485 mode the following has to be done:
3218 * wait until DMA is ready
3219 * wait on transmit shift register
3220 * toggle RTS
3221 * enable the receiver
3222 */
3223
3224 /* Sleep until all sent */
3225 tty_wait_until_sent(tty, 0);
3226#ifdef CONFIG_ETRAX_FAST_TIMER
3227 /* Now sleep a little more so that shift register is empty */
3228 schedule_usleep(info->char_time_usec * 2);
3229#endif
3230 /* wait on transmit shift register */
3231 do{
3232 get_lsr_info(info, &val);
3233 }while (!(val & TIOCSER_TEMT));
3234
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003235 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3238 e100_enable_rx(info);
3239 e100_enable_rxdma_irq(info);
3240#endif
3241 }
3242#endif /* CONFIG_ETRAX_RS485 */
3243
3244 return count;
3245} /* rs_write */
3246
3247
3248/* how much space is available in the xmit buffer? */
3249
3250static int
3251rs_write_room(struct tty_struct *tty)
3252{
3253 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3254
3255 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3256}
3257
3258/* How many chars are in the xmit buffer?
3259 * This does not include any chars in the transmitter FIFO.
3260 * Use wait_until_sent for waiting for FIFO drain.
3261 */
3262
3263static int
3264rs_chars_in_buffer(struct tty_struct *tty)
3265{
3266 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3267
3268 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3269}
3270
3271/* discard everything in the xmit buffer */
3272
3273static void
3274rs_flush_buffer(struct tty_struct *tty)
3275{
3276 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3277 unsigned long flags;
3278
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003279 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 info->xmit.head = info->xmit.tail = 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003281 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Jiri Slabyb963a842007-02-10 01:44:55 -08003283 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
3286/*
3287 * This function is used to send a high-priority XON/XOFF character to
3288 * the device
3289 *
3290 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3291 * but we do it in handle_ser_tx_interrupt().
3292 * We disable DMA channel and enable tx ready interrupt and write the
3293 * character when possible.
3294 */
3295static void rs_send_xchar(struct tty_struct *tty, char ch)
3296{
3297 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3298 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003299 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (info->uses_dma_out) {
3301 /* Put the DMA on hold and disable the channel */
3302 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3303 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3304 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3305 e100_disable_txdma_channel(info);
3306 }
3307
3308 /* Must make sure transmitter is not stopped before we can transmit */
3309 if (tty->stopped)
3310 rs_start(tty);
3311
3312 /* Enable manual transmit interrupt and send from there */
3313 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3314 info->x_char = ch;
3315 e100_enable_serial_tx_ready_irq(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003316 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
3319/*
3320 * ------------------------------------------------------------
3321 * rs_throttle()
3322 *
3323 * This routine is called by the upper-layer tty layer to signal that
3324 * incoming characters should be throttled.
3325 * ------------------------------------------------------------
3326 */
3327static void
3328rs_throttle(struct tty_struct * tty)
3329{
3330 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3331#ifdef SERIAL_DEBUG_THROTTLE
3332 char buf[64];
3333
3334 printk("throttle %s: %lu....\n", tty_name(tty, buf),
3335 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3336#endif
3337 DFLOW(DEBUG_LOG(info->line,"rs_throttle %lu\n", tty->ldisc.chars_in_buffer(tty)));
3338
3339 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003340 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* Turn off RTS line */
3342 e100_rts(info, 0);
3343 }
3344 if (I_IXOFF(tty))
3345 rs_send_xchar(tty, STOP_CHAR(tty));
3346
3347}
3348
3349static void
3350rs_unthrottle(struct tty_struct * tty)
3351{
3352 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3353#ifdef SERIAL_DEBUG_THROTTLE
3354 char buf[64];
3355
3356 printk("unthrottle %s: %lu....\n", tty_name(tty, buf),
3357 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3358#endif
3359 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc %d\n", tty->ldisc.chars_in_buffer(tty)));
3360 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3361 /* Do RTS before XOFF since XOFF might take some time */
Alan Coxadc8d742012-07-14 15:31:47 +01003362 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /* Assert RTS line */
3364 e100_rts(info, 1);
3365 }
3366
3367 if (I_IXOFF(tty)) {
3368 if (info->x_char)
3369 info->x_char = 0;
3370 else
3371 rs_send_xchar(tty, START_CHAR(tty));
3372 }
3373
3374}
3375
3376/*
3377 * ------------------------------------------------------------
3378 * rs_ioctl() and friends
3379 * ------------------------------------------------------------
3380 */
3381
3382static int
3383get_serial_info(struct e100_serial * info,
3384 struct serial_struct * retinfo)
3385{
3386 struct serial_struct tmp;
3387
3388 /* this is all probably wrong, there are a lot of fields
3389 * here that we don't have in e100_serial and maybe we
3390 * should set them to something else than 0.
3391 */
3392
3393 if (!retinfo)
3394 return -EFAULT;
3395 memset(&tmp, 0, sizeof(tmp));
3396 tmp.type = info->type;
3397 tmp.line = info->line;
Alan Coxd7283352008-08-04 17:21:18 +01003398 tmp.port = (int)info->ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 tmp.irq = info->irq;
3400 tmp.flags = info->flags;
3401 tmp.baud_base = info->baud_base;
3402 tmp.close_delay = info->close_delay;
3403 tmp.closing_wait = info->closing_wait;
3404 tmp.custom_divisor = info->custom_divisor;
3405 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3406 return -EFAULT;
3407 return 0;
3408}
3409
3410static int
3411set_serial_info(struct e100_serial *info,
3412 struct serial_struct *new_info)
3413{
3414 struct serial_struct new_serial;
3415 struct e100_serial old_info;
3416 int retval = 0;
3417
3418 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3419 return -EFAULT;
3420
3421 old_info = *info;
3422
3423 if (!capable(CAP_SYS_ADMIN)) {
3424 if ((new_serial.type != info->type) ||
3425 (new_serial.close_delay != info->close_delay) ||
3426 ((new_serial.flags & ~ASYNC_USR_MASK) !=
3427 (info->flags & ~ASYNC_USR_MASK)))
3428 return -EPERM;
3429 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3430 (new_serial.flags & ASYNC_USR_MASK));
3431 goto check_and_exit;
3432 }
3433
3434 if (info->count > 1)
3435 return -EBUSY;
3436
3437 /*
3438 * OK, past this point, all the error checking has been done.
3439 * At this point, we start making changes.....
3440 */
3441
3442 info->baud_base = new_serial.baud_base;
3443 info->flags = ((info->flags & ~ASYNC_FLAGS) |
3444 (new_serial.flags & ASYNC_FLAGS));
3445 info->custom_divisor = new_serial.custom_divisor;
3446 info->type = new_serial.type;
3447 info->close_delay = new_serial.close_delay;
3448 info->closing_wait = new_serial.closing_wait;
Jiri Slabyd6c53c02013-01-03 15:53:05 +01003449 info->port.low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 check_and_exit:
3452 if (info->flags & ASYNC_INITIALIZED) {
3453 change_speed(info);
3454 } else
3455 retval = startup(info);
3456 return retval;
3457}
3458
3459/*
3460 * get_lsr_info - get line status register info
3461 *
3462 * Purpose: Let user call ioctl() to get info when the UART physically
3463 * is emptied. On bus types like RS485, the transmitter must
3464 * release the bus after transmitting. This must be done when
3465 * the transmit shift register is empty, not be done when the
3466 * transmit holding register is empty. This functionality
3467 * allows an RS485 driver to be written in user space.
3468 */
3469static int
3470get_lsr_info(struct e100_serial * info, unsigned int *value)
3471{
3472 unsigned int result = TIOCSER_TEMT;
3473#ifndef CONFIG_SVINTO_SIM
3474 unsigned long curr_time = jiffies;
3475 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3476 unsigned long elapsed_usec =
3477 (curr_time - info->last_tx_active) * 1000000/HZ +
3478 curr_time_usec - info->last_tx_active_usec;
3479
3480 if (info->xmit.head != info->xmit.tail ||
3481 elapsed_usec < 2*info->char_time_usec) {
3482 result = 0;
3483 }
3484#endif
3485
3486 if (copy_to_user(value, &result, sizeof(int)))
3487 return -EFAULT;
3488 return 0;
3489}
3490
3491#ifdef SERIAL_DEBUG_IO
3492struct state_str
3493{
3494 int state;
3495 const char *str;
3496};
3497
3498const struct state_str control_state_str[] = {
3499 {TIOCM_DTR, "DTR" },
3500 {TIOCM_RTS, "RTS"},
3501 {TIOCM_ST, "ST?" },
3502 {TIOCM_SR, "SR?" },
3503 {TIOCM_CTS, "CTS" },
3504 {TIOCM_CD, "CD" },
3505 {TIOCM_RI, "RI" },
3506 {TIOCM_DSR, "DSR" },
3507 {0, NULL }
3508};
3509
3510char *get_control_state_str(int MLines, char *s)
3511{
3512 int i = 0;
3513
3514 s[0]='\0';
3515 while (control_state_str[i].str != NULL) {
3516 if (MLines & control_state_str[i].state) {
3517 if (s[0] != '\0') {
3518 strcat(s, ", ");
3519 }
3520 strcat(s, control_state_str[i].str);
3521 }
3522 i++;
3523 }
3524 return s;
3525}
3526#endif
3527
Alan Coxd7283352008-08-04 17:21:18 +01003528static int
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003529rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003531 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3532 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Alan Coxd7283352008-08-04 17:21:18 +01003534 if (!info->ioport)
3535 return -EIO;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003536
3537 local_irq_save(flags);
3538 if (break_state == -1) {
3539 /* Go to manual mode and set the txd pin to 0 */
3540 /* Clear bit 7 (txd) and 6 (tr_enable) */
3541 info->tx_ctrl &= 0x3F;
3542 } else {
3543 /* Set bit 7 (txd) and 6 (tr_enable) */
3544 info->tx_ctrl |= (0x80 | 0x40);
3545 }
Alan Coxd7283352008-08-04 17:21:18 +01003546 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003547 local_irq_restore(flags);
Alan Coxd7283352008-08-04 17:21:18 +01003548 return 0;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003549}
3550
3551static int
Alan Cox20b9d172011-02-14 16:26:50 +00003552rs_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003553{
3554 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
Alan Cox978e5952008-04-30 00:53:59 -07003555 unsigned long flags;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003556
Alan Cox978e5952008-04-30 00:53:59 -07003557 local_irq_save(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003558
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003559 if (clear & TIOCM_RTS)
3560 e100_rts(info, 0);
3561 if (clear & TIOCM_DTR)
3562 e100_dtr(info, 0);
3563 /* Handle FEMALE behaviour */
3564 if (clear & TIOCM_RI)
3565 e100_ri_out(info, 0);
3566 if (clear & TIOCM_CD)
3567 e100_cd_out(info, 0);
3568
3569 if (set & TIOCM_RTS)
3570 e100_rts(info, 1);
3571 if (set & TIOCM_DTR)
3572 e100_dtr(info, 1);
3573 /* Handle FEMALE behaviour */
3574 if (set & TIOCM_RI)
3575 e100_ri_out(info, 1);
3576 if (set & TIOCM_CD)
3577 e100_cd_out(info, 1);
Alan Cox032c17e2008-04-28 02:13:50 -07003578
Alan Cox978e5952008-04-30 00:53:59 -07003579 local_irq_restore(flags);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003580 return 0;
3581}
3582
3583static int
Alan Cox60b33c12011-02-14 16:26:14 +00003584rs_tiocmget(struct tty_struct *tty)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003585{
3586 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3587 unsigned int result;
Alan Cox978e5952008-04-30 00:53:59 -07003588 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Alan Cox978e5952008-04-30 00:53:59 -07003590 local_irq_save(flags);
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 result =
3593 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3594 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3595 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3596 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3597 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3598 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3599
Alan Cox978e5952008-04-30 00:53:59 -07003600 local_irq_restore(flags);
Alan Cox032c17e2008-04-28 02:13:50 -07003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602#ifdef SERIAL_DEBUG_IO
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003603 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3604 info->line, result, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 {
3606 char s[100];
3607
3608 get_control_state_str(result, s);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003609 printk(KERN_DEBUG "state: %s\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
3611#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003612 return result;
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
3615
3616
3617static int
Alan Cox6caa76b2011-02-14 16:27:22 +00003618rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 unsigned int cmd, unsigned long arg)
3620{
3621 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3622
3623 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3624 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3625 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3626 if (tty->flags & (1 << TTY_IO_ERROR))
3627 return -EIO;
3628 }
3629
3630 switch (cmd) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003631 case TIOCGSERIAL:
3632 return get_serial_info(info,
3633 (struct serial_struct *) arg);
3634 case TIOCSSERIAL:
3635 return set_serial_info(info,
3636 (struct serial_struct *) arg);
3637 case TIOCSERGETLSR: /* Get line status register */
3638 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003640 case TIOCSERGSTRUCT:
3641 if (copy_to_user((struct e100_serial *) arg,
3642 info, sizeof(struct e100_serial)))
3643 return -EFAULT;
3644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646#if defined(CONFIG_ETRAX_RS485)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003647 case TIOCSERSETRS485:
3648 {
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003649 /* In this ioctl we still use the old structure
3650 * rs485_control for backward compatibility
3651 * (if we use serial_rs485, then old user-level code
3652 * wouldn't work anymore...).
3653 * The use of this ioctl is deprecated: use TIOCSRS485
3654 * instead.*/
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003655 struct rs485_control rs485ctrl;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003656 struct serial_rs485 rs485data;
3657 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003658 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3659 sizeof(rs485ctrl)))
3660 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003662 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3663 rs485data.flags = 0;
Jesper Nilssonc7213fc2010-10-28 12:08:27 +02003664
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003665 if (rs485ctrl.enabled)
3666 rs485data.flags |= SER_RS485_ENABLED;
3667 else
3668 rs485data.flags &= ~(SER_RS485_ENABLED);
3669
3670 if (rs485ctrl.rts_on_send)
3671 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3672 else
3673 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3674
3675 if (rs485ctrl.rts_after_sent)
3676 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3677 else
3678 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3679
3680 return e100_enable_rs485(tty, &rs485data);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003683 case TIOCSRS485:
3684 {
3685 /* This is the new version of TIOCSRS485, with new
3686 * data structure serial_rs485 */
3687 struct serial_rs485 rs485data;
3688 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3689 sizeof(rs485data)))
3690 return -EFAULT;
3691
3692 return e100_enable_rs485(tty, &rs485data);
3693 }
3694
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003695 case TIOCGRS485:
3696 {
3697 struct serial_rs485 *rs485data =
3698 &(((struct e100_serial *)tty->driver_data)->rs485);
3699 /* This is the ioctl to get RS485 data from user-space */
3700 if (copy_to_user((struct serial_rs485 *) arg,
3701 rs485data,
Jesper Nilsson94479c02010-10-28 12:04:55 +02003702 sizeof(struct serial_rs485)))
Claudio Scordinof1d23ed2010-05-31 10:19:09 +02003703 return -EFAULT;
3704 break;
3705 }
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003706
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003707 case TIOCSERWRRS485:
3708 {
3709 struct rs485_write rs485wr;
3710 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3711 sizeof(rs485wr)))
3712 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003714 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716#endif
3717
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003718 default:
3719 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721 return 0;
3722}
3723
3724static void
Alan Cox606d0992006-12-08 02:38:45 -08003725rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 change_speed(info);
3730
3731 /* Handle turning off CRTSCTS */
3732 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyee797062013-03-07 13:12:34 +01003733 !(tty->termios.c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 rs_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738/*
3739 * ------------------------------------------------------------
3740 * rs_close()
3741 *
3742 * This routine is called when the serial port gets closed. First, we
3743 * wait for the last remaining data to be sent. Then, we unlink its
3744 * S structure from the interrupt chain if necessary, and we free
3745 * that IRQ if nothing is left in the chain.
3746 * ------------------------------------------------------------
3747 */
3748static void
3749rs_close(struct tty_struct *tty, struct file * filp)
3750{
3751 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3752 unsigned long flags;
3753
3754 if (!info)
3755 return;
3756
3757 /* interrupts are disabled for this entire function */
3758
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003759 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 if (tty_hung_up_p(filp)) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003762 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return;
3764 }
3765
3766#ifdef SERIAL_DEBUG_OPEN
3767 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3768 info->line, info->count);
3769#endif
3770 if ((tty->count == 1) && (info->count != 1)) {
3771 /*
3772 * Uh, oh. tty->count is 1, which means that the tty
3773 * structure will be freed. Info->count should always
3774 * be one in these conditions. If it's greater than
3775 * one, we've got real problems, since it means the
3776 * serial port won't be shutdown.
3777 */
WANG Cong3d43b7d2011-09-01 16:47:49 +08003778 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 "rs_close: bad serial port count; tty->count is 1, "
3780 "info->count is %d\n", info->count);
3781 info->count = 1;
3782 }
3783 if (--info->count < 0) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08003784 printk(KERN_ERR "rs_close: bad serial port count for ttyS%d: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 info->line, info->count);
3786 info->count = 0;
3787 }
3788 if (info->count) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003789 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return;
3791 }
3792 info->flags |= ASYNC_CLOSING;
3793 /*
3794 * Save the termios structure, since this port may have
3795 * separate termios for callout and dialin.
3796 */
3797 if (info->flags & ASYNC_NORMAL_ACTIVE)
Alan Coxadc8d742012-07-14 15:31:47 +01003798 info->normal_termios = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 /*
3800 * Now we wait for the transmit buffer to clear; and we notify
3801 * the line discipline to only process XON/XOFF characters.
3802 */
3803 tty->closing = 1;
3804 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
3805 tty_wait_until_sent(tty, info->closing_wait);
3806 /*
3807 * At this point we stop accepting input. To do this, we
3808 * disable the serial receiver and the DMA receive interrupt.
3809 */
3810#ifdef SERIAL_HANDLE_EARLY_ERRORS
3811 e100_disable_serial_data_irq(info);
3812#endif
3813
3814#ifndef CONFIG_SVINTO_SIM
3815 e100_disable_rx(info);
3816 e100_disable_rx_irq(info);
3817
3818 if (info->flags & ASYNC_INITIALIZED) {
3819 /*
3820 * Before we drop DTR, make sure the UART transmitter
3821 * has completely drained; this is especially
3822 * important as we have a transmit FIFO!
3823 */
3824 rs_wait_until_sent(tty, HZ);
3825 }
3826#endif
3827
3828 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07003829 rs_flush_buffer(tty);
Alan Cox454aa382008-05-12 12:31:37 +01003830 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 tty->closing = 0;
3832 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003833 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 if (info->blocked_open) {
Nishanth Aravamudan3c76bc52005-11-07 01:01:21 -08003835 if (info->close_delay)
3836 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 wake_up_interruptible(&info->open_wait);
3838 }
3839 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
3840 wake_up_interruptible(&info->close_wait);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003841 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 /* port closed */
3844
3845#if defined(CONFIG_ETRAX_RS485)
Claudio Scordino6fd1af42009-04-07 16:48:19 +01003846 if (info->rs485.flags & SER_RS485_ENABLED) {
3847 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848#if defined(CONFIG_ETRAX_RS485_ON_PA)
3849 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3850#endif
3851#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
3852 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3853 rs485_port_g_bit, 0);
3854#endif
3855#if defined(CONFIG_ETRAX_RS485_LTC1387)
3856 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3857 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 0);
3858 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3859 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 0);
3860#endif
3861 }
3862#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003863
3864 /*
3865 * Release any allocated DMA irq's.
3866 */
3867 if (info->dma_in_enabled) {
3868 free_irq(info->dma_in_irq_nbr, info);
3869 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3870 info->uses_dma_in = 0;
3871#ifdef SERIAL_DEBUG_OPEN
3872 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3873 info->dma_in_irq_description);
3874#endif
3875 }
3876 if (info->dma_out_enabled) {
3877 free_irq(info->dma_out_irq_nbr, info);
3878 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3879 info->uses_dma_out = 0;
3880#ifdef SERIAL_DEBUG_OPEN
3881 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3882 info->dma_out_irq_description);
3883#endif
3884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
3887/*
3888 * rs_wait_until_sent() --- wait until the transmitter is empty
3889 */
3890static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3891{
3892 unsigned long orig_jiffies;
3893 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3894 unsigned long curr_time = jiffies;
3895 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3896 long elapsed_usec =
3897 (curr_time - info->last_tx_active) * (1000000/HZ) +
3898 curr_time_usec - info->last_tx_active_usec;
3899
3900 /*
3901 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3902 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3903 */
3904 orig_jiffies = jiffies;
3905 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3906 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3907 (elapsed_usec < 2*info->char_time_usec)) {
Nishanth Aravamudan3c76bc52005-11-07 01:01:21 -08003908 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (signal_pending(current))
3910 break;
3911 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3912 break;
3913 curr_time = jiffies;
3914 curr_time_usec = GET_JIFFIES_USEC();
3915 elapsed_usec =
3916 (curr_time - info->last_tx_active) * (1000000/HZ) +
3917 curr_time_usec - info->last_tx_active_usec;
3918 }
3919 set_current_state(TASK_RUNNING);
3920}
3921
3922/*
3923 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3924 */
3925void
3926rs_hangup(struct tty_struct *tty)
3927{
3928 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3929
3930 rs_flush_buffer(tty);
3931 shutdown(info);
3932 info->event = 0;
3933 info->count = 0;
3934 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01003935 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 wake_up_interruptible(&info->open_wait);
3937}
3938
3939/*
3940 * ------------------------------------------------------------
3941 * rs_open() and friends
3942 * ------------------------------------------------------------
3943 */
3944static int
3945block_til_ready(struct tty_struct *tty, struct file * filp,
3946 struct e100_serial *info)
3947{
3948 DECLARE_WAITQUEUE(wait, current);
3949 unsigned long flags;
3950 int retval;
3951 int do_clocal = 0, extra_count = 0;
3952
3953 /*
3954 * If the device is in the middle of being closed, then block
3955 * until it's done, and then try again.
3956 */
3957 if (tty_hung_up_p(filp) ||
3958 (info->flags & ASYNC_CLOSING)) {
Alan Cox89c8d912012-08-08 16:30:13 +01003959 wait_event_interruptible_tty(tty, info->close_wait,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003960 !(info->flags & ASYNC_CLOSING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961#ifdef SERIAL_DO_RESTART
3962 if (info->flags & ASYNC_HUP_NOTIFY)
3963 return -EAGAIN;
3964 else
3965 return -ERESTARTSYS;
3966#else
3967 return -EAGAIN;
3968#endif
3969 }
3970
3971 /*
3972 * If non-blocking mode is set, or the port is not enabled,
3973 * then make the check up front and then exit.
3974 */
3975 if ((filp->f_flags & O_NONBLOCK) ||
3976 (tty->flags & (1 << TTY_IO_ERROR))) {
3977 info->flags |= ASYNC_NORMAL_ACTIVE;
3978 return 0;
3979 }
3980
Alan Coxadc8d742012-07-14 15:31:47 +01003981 if (tty->termios.c_cflag & CLOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 do_clocal = 1;
3983 }
3984
3985 /*
3986 * Block waiting for the carrier detect and the line to become
3987 * free (i.e., not in use by the callout). While we are in
3988 * this loop, info->count is dropped by one, so that
3989 * rs_close() knows when to free things. We restore it upon
3990 * exit, either normal or abnormal.
3991 */
3992 retval = 0;
3993 add_wait_queue(&info->open_wait, &wait);
3994#ifdef SERIAL_DEBUG_OPEN
3995 printk("block_til_ready before block: ttyS%d, count = %d\n",
3996 info->line, info->count);
3997#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08003998 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (!tty_hung_up_p(filp)) {
4000 extra_count++;
4001 info->count--;
4002 }
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004003 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 info->blocked_open++;
4005 while (1) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004006 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 /* assert RTS and DTR */
4008 e100_rts(info, 1);
4009 e100_dtr(info, 1);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004010 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 set_current_state(TASK_INTERRUPTIBLE);
4012 if (tty_hung_up_p(filp) ||
4013 !(info->flags & ASYNC_INITIALIZED)) {
4014#ifdef SERIAL_DO_RESTART
4015 if (info->flags & ASYNC_HUP_NOTIFY)
4016 retval = -EAGAIN;
4017 else
4018 retval = -ERESTARTSYS;
4019#else
4020 retval = -EAGAIN;
4021#endif
4022 break;
4023 }
4024 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
4025 /* && (do_clocal || DCD_IS_ASSERTED) */
4026 break;
4027 if (signal_pending(current)) {
4028 retval = -ERESTARTSYS;
4029 break;
4030 }
4031#ifdef SERIAL_DEBUG_OPEN
4032 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4033 info->line, info->count);
4034#endif
Alan Cox89c8d912012-08-08 16:30:13 +01004035 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +01004037 tty_lock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
4039 set_current_state(TASK_RUNNING);
4040 remove_wait_queue(&info->open_wait, &wait);
4041 if (extra_count)
4042 info->count++;
4043 info->blocked_open--;
4044#ifdef SERIAL_DEBUG_OPEN
4045 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4046 info->line, info->count);
4047#endif
4048 if (retval)
4049 return retval;
4050 info->flags |= ASYNC_NORMAL_ACTIVE;
4051 return 0;
4052}
4053
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004054static void
4055deinit_port(struct e100_serial *info)
4056{
4057 if (info->dma_out_enabled) {
4058 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
4059 free_irq(info->dma_out_irq_nbr, info);
4060 }
4061 if (info->dma_in_enabled) {
4062 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
4063 free_irq(info->dma_in_irq_nbr, info);
4064 }
4065}
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067/*
4068 * This routine is called whenever a serial port is opened.
4069 * It performs the serial-specific initialization for the tty structure.
4070 */
4071static int
4072rs_open(struct tty_struct *tty, struct file * filp)
4073{
4074 struct e100_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01004075 int retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004076 int allocated_resources = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Jiri Slaby410235f2012-03-05 14:52:01 +01004078 info = rs_table + tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 if (!info->enabled)
4080 return -ENODEV;
4081
4082#ifdef SERIAL_DEBUG_OPEN
4083 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
4084 info->count);
4085#endif
4086
4087 info->count++;
4088 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01004089 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Jiri Slabyd6c53c02013-01-03 15:53:05 +01004091 info->port.low_latency = !!(info->flags & ASYNC_LOW_LATENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /*
4094 * If the port is in the middle of closing, bail out now
4095 */
4096 if (tty_hung_up_p(filp) ||
4097 (info->flags & ASYNC_CLOSING)) {
Alan Cox89c8d912012-08-08 16:30:13 +01004098 wait_event_interruptible_tty(tty, info->close_wait,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004099 !(info->flags & ASYNC_CLOSING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100#ifdef SERIAL_DO_RESTART
4101 return ((info->flags & ASYNC_HUP_NOTIFY) ?
4102 -EAGAIN : -ERESTARTSYS);
4103#else
4104 return -EAGAIN;
4105#endif
4106 }
4107
4108 /*
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004109 * If DMA is enabled try to allocate the irq's.
4110 */
4111 if (info->count == 1) {
4112 allocated_resources = 1;
4113 if (info->dma_in_enabled) {
4114 if (request_irq(info->dma_in_irq_nbr,
4115 rec_interrupt,
4116 info->dma_in_irq_flags,
4117 info->dma_in_irq_description,
4118 info)) {
4119 printk(KERN_WARNING "DMA irq '%s' busy; "
4120 "falling back to non-DMA mode\n",
4121 info->dma_in_irq_description);
4122 /* Make sure we never try to use DMA in */
4123 /* for the port again. */
4124 info->dma_in_enabled = 0;
4125 } else if (cris_request_dma(info->dma_in_nbr,
4126 info->dma_in_irq_description,
4127 DMA_VERBOSE_ON_ERROR,
4128 info->dma_owner)) {
4129 free_irq(info->dma_in_irq_nbr, info);
4130 printk(KERN_WARNING "DMA '%s' busy; "
4131 "falling back to non-DMA mode\n",
4132 info->dma_in_irq_description);
4133 /* Make sure we never try to use DMA in */
4134 /* for the port again. */
4135 info->dma_in_enabled = 0;
4136 }
4137#ifdef SERIAL_DEBUG_OPEN
4138 else
4139 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4140 info->dma_in_irq_description);
4141#endif
4142 }
4143 if (info->dma_out_enabled) {
4144 if (request_irq(info->dma_out_irq_nbr,
4145 tr_interrupt,
4146 info->dma_out_irq_flags,
4147 info->dma_out_irq_description,
4148 info)) {
4149 printk(KERN_WARNING "DMA irq '%s' busy; "
4150 "falling back to non-DMA mode\n",
4151 info->dma_out_irq_description);
4152 /* Make sure we never try to use DMA out */
4153 /* for the port again. */
4154 info->dma_out_enabled = 0;
4155 } else if (cris_request_dma(info->dma_out_nbr,
4156 info->dma_out_irq_description,
4157 DMA_VERBOSE_ON_ERROR,
4158 info->dma_owner)) {
4159 free_irq(info->dma_out_irq_nbr, info);
4160 printk(KERN_WARNING "DMA '%s' busy; "
4161 "falling back to non-DMA mode\n",
4162 info->dma_out_irq_description);
4163 /* Make sure we never try to use DMA out */
4164 /* for the port again. */
4165 info->dma_out_enabled = 0;
4166 }
4167#ifdef SERIAL_DEBUG_OPEN
4168 else
4169 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4170 info->dma_out_irq_description);
4171#endif
4172 }
4173 }
4174
4175 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 * Start up the serial port
4177 */
4178
4179 retval = startup(info);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004180 if (retval) {
4181 if (allocated_resources)
4182 deinit_port(info);
4183
4184 /* FIXME Decrease count info->count here too? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 return retval;
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004186 }
4187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 retval = block_til_ready(tty, filp, info);
4190 if (retval) {
4191#ifdef SERIAL_DEBUG_OPEN
4192 printk("rs_open returning after block_til_ready with %d\n",
4193 retval);
4194#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004195 if (allocated_resources)
4196 deinit_port(info);
4197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return retval;
4199 }
4200
4201 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
Alan Coxadc8d742012-07-14 15:31:47 +01004202 tty->termios = info->normal_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 change_speed(info);
4204 }
4205
4206#ifdef SERIAL_DEBUG_OPEN
4207 printk("rs_open ttyS%d successful...\n", info->line);
4208#endif
4209 DLOG_INT_TRIG( log_int_pos = 0);
4210
4211 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4212 info->icount.rx = 0;
4213 } );
4214
4215 return 0;
4216}
4217
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004218#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219/*
4220 * /proc fs routines....
4221 */
4222
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004223static void seq_line_info(struct seq_file *m, struct e100_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 unsigned long tmp;
4226
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004227 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
4228 info->line, (unsigned long)info->ioport, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Alan Coxd7283352008-08-04 17:21:18 +01004230 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004231 seq_printf(m, "\n");
4232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004235 seq_printf(m, " baud:%d", info->baud);
4236 seq_printf(m, " tx:%lu rx:%lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 (unsigned long)info->icount.tx,
4238 (unsigned long)info->icount.rx);
4239 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004240 if (tmp)
4241 seq_printf(m, " tx_pend:%lu/%lu",
4242 (unsigned long)tmp,
4243 (unsigned long)SERIAL_XMIT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004245 seq_printf(m, " rx_pend:%lu/%lu",
4246 (unsigned long)info->recv_cnt,
4247 (unsigned long)info->max_recv_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249#if 1
Takashi Iwaia88487c2008-07-16 21:54:42 +01004250 if (info->port.tty) {
Takashi Iwaia88487c2008-07-16 21:54:42 +01004251 if (info->port.tty->stopped)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004252 seq_printf(m, " stopped:%i",
4253 (int)info->port.tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
4255
4256 {
Alan Coxd7283352008-08-04 17:21:18 +01004257 unsigned char rstat = info->ioport[REG_STATUS];
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004258 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4259 seq_printf(m, " xoff_detect:1");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
4262#endif
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 if (info->icount.frame)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004265 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 if (info->icount.parity)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004268 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 if (info->icount.brk)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004271 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 if (info->icount.overrun)
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004274 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 /*
4277 * Last thing is the RS-232 status lines
4278 */
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004279 if (!E100_RTS_GET(info))
4280 seq_puts(m, "|RTS");
4281 if (!E100_CTS_GET(info))
4282 seq_puts(m, "|CTS");
4283 if (!E100_DTR_GET(info))
4284 seq_puts(m, "|DTR");
4285 if (!E100_DSR_GET(info))
4286 seq_puts(m, "|DSR");
4287 if (!E100_CD_GET(info))
4288 seq_puts(m, "|CD");
4289 if (!E100_RI_GET(info))
4290 seq_puts(m, "|RI");
4291 seq_puts(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292}
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004295static int crisv10_proc_show(struct seq_file *m, void *v)
4296{
4297 int i;
4298
4299 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4300
4301 for (i = 0; i < NR_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (!rs_table[i].enabled)
4303 continue;
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004304 seq_line_info(m, &rs_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
4306#ifdef DEBUG_LOG_INCLUDED
4307 for (i = 0; i < debug_log_pos; i++) {
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004308 seq_printf(m, "%-4i %lu.%lu ",
4309 i, debug_log[i].time,
4310 timer_data_to_ns(debug_log[i].timer_data));
4311 seq_printf(m, debug_log[i].string, debug_log[i].value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004313 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 debug_log_pos = 0;
4315#endif
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004319static int crisv10_proc_open(struct inode *inode, struct file *file)
4320{
4321 return single_open(file, crisv10_proc_show, NULL);
4322}
4323
4324static const struct file_operations crisv10_proc_fops = {
4325 .owner = THIS_MODULE,
4326 .open = crisv10_proc_open,
4327 .read = seq_read,
4328 .llseek = seq_lseek,
4329 .release = single_release,
4330};
4331#endif
4332
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334/* Finally, routines used to initialize the serial driver. */
4335
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004336static void show_serial_version(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 printk(KERN_INFO
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004339 "ETRAX 100LX serial-driver %s, "
4340 "(c) 2000-2004 Axis Communications AB\r\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 &serial_version[11]); /* "$Revision: x.yy" */
4342}
4343
4344/* rs_init inits the driver at boot (using the module_init chain) */
4345
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004346static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 .open = rs_open,
4348 .close = rs_close,
4349 .write = rs_write,
4350 .flush_chars = rs_flush_chars,
4351 .write_room = rs_write_room,
4352 .chars_in_buffer = rs_chars_in_buffer,
4353 .flush_buffer = rs_flush_buffer,
4354 .ioctl = rs_ioctl,
4355 .throttle = rs_throttle,
4356 .unthrottle = rs_unthrottle,
4357 .set_termios = rs_set_termios,
4358 .stop = rs_stop,
4359 .start = rs_start,
4360 .hangup = rs_hangup,
4361 .break_ctl = rs_break,
4362 .send_xchar = rs_send_xchar,
4363 .wait_until_sent = rs_wait_until_sent,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004364 .tiocmget = rs_tiocmget,
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004365 .tiocmset = rs_tiocmset,
4366#ifdef CONFIG_PROC_FS
4367 .proc_fops = &crisv10_proc_fops,
4368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369};
4370
Jesper Nilsson9e040a32009-04-30 15:08:13 -07004371static int __init rs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 int i;
4374 struct e100_serial *info;
4375 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4376
4377 if (!driver)
4378 return -ENOMEM;
4379
4380 show_serial_version();
4381
4382 /* Setup the timed flush handler system */
4383
4384#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004385 setup_timer(&flush_timer, timed_flush_handler, 0);
4386 mod_timer(&flush_timer, jiffies + 5);
4387#endif
4388
4389#if defined(CONFIG_ETRAX_RS485)
4390#if defined(CONFIG_ETRAX_RS485_ON_PA)
WANG Cong2f7861d2011-09-01 13:58:29 +08004391 if (cris_io_interface_allocate_pins(if_serial_0, 'a', rs485_pa_bit,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004392 rs485_pa_bit)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004393 printk(KERN_ERR "ETRAX100LX serial: Could not allocate "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004394 "RS485 pin\n");
Julia Lawall41ca7322008-10-13 10:31:37 +01004395 put_tty_driver(driver);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004396 return -EBUSY;
4397 }
4398#endif
4399#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
WANG Cong2f7861d2011-09-01 13:58:29 +08004400 if (cris_io_interface_allocate_pins(if_serial_0, 'g', rs485_pa_bit,
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004401 rs485_port_g_bit)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004402 printk(KERN_ERR "ETRAX100LX serial: Could not allocate "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004403 "RS485 pin\n");
Julia Lawall41ca7322008-10-13 10:31:37 +01004404 put_tty_driver(driver);
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004405 return -EBUSY;
4406 }
4407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408#endif
4409
4410 /* Initialize the tty_driver structure */
4411
4412 driver->driver_name = "serial";
4413 driver->name = "ttyS";
4414 driver->major = TTY_MAJOR;
4415 driver->minor_start = 64;
4416 driver->type = TTY_DRIVER_TYPE_SERIAL;
4417 driver->subtype = SERIAL_TYPE_NORMAL;
4418 driver->init_termios = tty_std_termios;
4419 driver->init_termios.c_cflag =
4420 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
Alan Cox606d0992006-12-08 02:38:45 -08004421 driver->init_termios.c_ispeed = 115200;
4422 driver->init_termios.c_ospeed = 115200;
Jiri Slabyc4d6ebe2012-08-07 21:47:48 +02004423 driver->flags = TTY_DRIVER_REAL_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 tty_set_operations(driver, &rs_ops);
4426 serial_driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004428 /* do some initializing for the separate ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004430 if (info->enabled) {
4431 if (cris_request_io_interface(info->io_if,
4432 info->io_if_description)) {
WANG Cong3d43b7d2011-09-01 16:47:49 +08004433 printk(KERN_ERR "ETRAX100LX async serial: "
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004434 "Could not allocate IO pins for "
4435 "%s, port %d\n",
4436 info->io_if_description, i);
4437 info->enabled = 0;
4438 }
4439 }
Jiri Slaby953756e2012-04-02 13:53:46 +02004440 tty_port_init(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 info->uses_dma_in = 0;
4442 info->uses_dma_out = 0;
4443 info->line = i;
Takashi Iwaia88487c2008-07-16 21:54:42 +01004444 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 info->type = PORT_ETRAX;
4446 info->tr_running = 0;
4447 info->forced_eop = 0;
4448 info->baud_base = DEF_BAUD_BASE;
4449 info->custom_divisor = 0;
4450 info->flags = 0;
4451 info->close_delay = 5*HZ/10;
4452 info->closing_wait = 30*HZ;
4453 info->x_char = 0;
4454 info->event = 0;
4455 info->count = 0;
4456 info->blocked_open = 0;
4457 info->normal_termios = driver->init_termios;
4458 init_waitqueue_head(&info->open_wait);
4459 init_waitqueue_head(&info->close_wait);
4460 info->xmit.buf = NULL;
4461 info->xmit.tail = info->xmit.head = 0;
4462 info->first_recv_buffer = info->last_recv_buffer = NULL;
4463 info->recv_cnt = info->max_recv_cnt = 0;
4464 info->last_tx_active_usec = 0;
4465 info->last_tx_active = 0;
4466
4467#if defined(CONFIG_ETRAX_RS485)
4468 /* Set sane defaults */
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004469 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4470 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 info->rs485.delay_rts_before_send = 0;
Claudio Scordino6fd1af42009-04-07 16:48:19 +01004472 info->rs485.flags &= ~(SER_RS485_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473#endif
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004474 INIT_WORK(&info->work, do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 if (info->enabled) {
Kulikov Vasiliy078dee22010-07-14 22:04:42 +04004477 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4478 serial_driver->name, info->line, info->ioport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004480 tty_port_link_device(&info->port, driver, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02004482
4483 if (tty_register_driver(driver))
4484 panic("Couldn't register serial driver\n");
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486#ifdef CONFIG_ETRAX_FAST_TIMER
4487#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4488 memset(fast_timers, 0, sizeof(fast_timers));
4489#endif
4490#ifdef CONFIG_ETRAX_RS485
4491 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4492#endif
4493 fast_timer_init();
4494#endif
4495
4496#ifndef CONFIG_SVINTO_SIM
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004497#ifndef CONFIG_ETRAX_KGDB
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 /* Not needed in simulator. May only complicate stuff. */
4499 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4500
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004501 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08004502 IRQF_SHARED, "serial ", driver))
Harvey Harrison71cc2c22008-04-30 00:55:10 -07004503 panic("%s: Failed to request irq8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505#endif
4506#endif /* CONFIG_SVINTO_SIM */
Jesper Nilsson77accbf2007-11-14 17:01:15 -08004507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return 0;
4509}
4510
4511/* this makes sure that rs_init is called during kernel boot */
4512
4513module_init(rs_init);