• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/wait.h>
2 
3 #define UART_CONFIG_TYPE	(1 << 0)
4 #define UART_CONFIG_IRQ		(1 << 1)
5 #define UPIO_PORT		(0)
6 #define UPIO_HUB6		(1)
7 #define UPIO_MEM		(2)
8 #define UPIO_MEM32		(3)
9 #define UPIO_AU			(4)			/* Au1x00 type IO */
10 #define UPIO_TSI		(5)			/* Tsi108/109 type IO */
11 #define UPF_FOURPORT		(1 << 1)
12 #define UPF_SAK			(1 << 2)
13 #define UPF_SPD_MASK		(0x1030)
14 #define UPF_SPD_HI		(0x0010)
15 #define UPF_SPD_VHI		(0x0020)
16 #define UPF_SPD_CUST		(0x0030)
17 #define UPF_SPD_SHI		(0x1000)
18 #define UPF_SPD_WARP		(0x1010)
19 #define UPF_SKIP_TEST		(1 << 6)
20 #define UPF_AUTO_IRQ		(1 << 7)
21 #define UPF_HARDPPS_CD		(1 << 11)
22 #define UPF_LOW_LATENCY		(1 << 13)
23 #define UPF_BUGGY_UART		(1 << 14)
24 #define UPF_MAGIC_MULTIPLIER	(1 << 16)
25 #define UPF_CONS_FLOW		(1 << 23)
26 #define UPF_SHARE_IRQ		(1 << 24)
27 #define UPF_BOOT_AUTOCONF	(1 << 28)
28 #define UPF_DEAD		(1 << 30)
29 #define UPF_IOREMAP		(1 << 31)
30 #define UPF_CHANGE_MASK		(0x17fff)
31 #define UPF_USR_MASK		(UPF_SPD_MASK|UPF_LOW_LATENCY)
32 #define USF_CLOSING_WAIT_INF	(0)
33 #define USF_CLOSING_WAIT_NONE	(~0U)
34 
35 #define UART_XMIT_SIZE	PAGE_SIZE
36 
37 #define UIF_CHECK_CD		(1 << 25)
38 #define UIF_CTS_FLOW		(1 << 26)
39 #define UIF_NORMAL_ACTIVE	(1 << 29)
40 #define UIF_INITIALIZED		(1 << 31)
41 #define UIF_SUSPENDED		(1 << 30)
42 
43 #define WAKEUP_CHARS		256
44 
45 #define uart_circ_empty(circ)		((circ)->head == (circ)->tail)
46 #define uart_circ_clear(circ)		((circ)->head = (circ)->tail = 0)
47 
48 #define uart_circ_chars_pending(circ)	\
49 	(CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
50 
51 #define uart_circ_chars_free(circ)	\
52 	(CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
53 
54 #define uart_tx_stopped(port)		\
55 	((port)->info->tty->stopped || (port)->info->tty->hw_stopped)
56 
57 #define UART_ENABLE_MS(port,cflag)	((port)->flags & UPF_HARDPPS_CD || \
58 					 (cflag) & CRTSCTS || \
59 					 !((cflag) & CLOCAL))
60 
61 
62 struct sb_uart_port;
63 struct sb_uart_info;
64 struct serial_struct;
65 struct device;
66 
67 struct sb_uart_ops {
68 	unsigned int	(*tx_empty)(struct sb_uart_port *);
69 	void		(*set_mctrl)(struct sb_uart_port *, unsigned int mctrl);
70 	unsigned int	(*get_mctrl)(struct sb_uart_port *);
71 	void		(*stop_tx)(struct sb_uart_port *);
72 	void		(*start_tx)(struct sb_uart_port *);
73 	void		(*send_xchar)(struct sb_uart_port *, char ch);
74 	void		(*stop_rx)(struct sb_uart_port *);
75 	void		(*enable_ms)(struct sb_uart_port *);
76 	void		(*break_ctl)(struct sb_uart_port *, int ctl);
77 	int		(*startup)(struct sb_uart_port *);
78 	void		(*shutdown)(struct sb_uart_port *);
79 	void		(*set_termios)(struct sb_uart_port *, struct MP_TERMIOS *new,
80 				       struct MP_TERMIOS *old);
81 	void		(*pm)(struct sb_uart_port *, unsigned int state,
82 			      unsigned int oldstate);
83 	int		(*set_wake)(struct sb_uart_port *, unsigned int state);
84 
85 	const char *(*type)(struct sb_uart_port *);
86 
87 	void		(*release_port)(struct sb_uart_port *);
88 
89 	int		(*request_port)(struct sb_uart_port *);
90 	void		(*config_port)(struct sb_uart_port *, int);
91 	int		(*verify_port)(struct sb_uart_port *, struct serial_struct *);
92 	int		(*ioctl)(struct sb_uart_port *, unsigned int, unsigned long);
93 };
94 
95 
96 struct sb_uart_icount {
97 	__u32	cts;
98 	__u32	dsr;
99 	__u32	rng;
100 	__u32	dcd;
101 	__u32	rx;
102 	__u32	tx;
103 	__u32	frame;
104 	__u32	overrun;
105 	__u32	parity;
106 	__u32	brk;
107 	__u32	buf_overrun;
108 };
109 typedef unsigned int  upf_t;
110 
111 struct sb_uart_port {
112 	spinlock_t		lock;			/* port lock */
113 	unsigned int		iobase;			/* in/out[bwl] */
114 	unsigned char __iomem	*membase;		/* read/write[bwl] */
115 	unsigned int		irq;			/* irq number */
116 	unsigned int		uartclk;		/* base uart clock */
117 	unsigned int		fifosize;		/* tx fifo size */
118 	unsigned char		x_char;			/* xon/xoff char */
119 	unsigned char		regshift;		/* reg offset shift */
120 	unsigned char		iotype;			/* io access style */
121 	unsigned char		unused1;
122 
123 
124 	unsigned int		read_status_mask;	/* driver specific */
125 	unsigned int		ignore_status_mask;	/* driver specific */
126 	struct sb_uart_info	*info;			/* pointer to parent info */
127 	struct sb_uart_icount	icount;			/* statistics */
128 
129 	struct console		*cons;			/* struct console, if any */
130 #ifdef CONFIG_SERIAL_CORE_CONSOLE
131 	unsigned long		sysrq;			/* sysrq timeout */
132 #endif
133 
134 	upf_t			flags;
135 
136 	unsigned int		mctrl;			/* current modem ctrl settings */
137 	unsigned int		timeout;		/* character-based timeout */
138 	unsigned int		type;			/* port type */
139 	const struct sb_uart_ops	*ops;
140 	unsigned int		custom_divisor;
141 	unsigned int		line;			/* port index */
142 	unsigned long		mapbase;		/* for ioremap */
143 	struct device		*dev;			/* parent device */
144 	unsigned char		hub6;			/* this should be in the 8250 driver */
145 	unsigned char		unused[3];
146 };
147 
148 #define mdmode			unused[2]
149 #define MDMODE_ADDR		0x1
150 #define MDMODE_ENABLE	0x2
151 #define MDMODE_AUTO		0x4
152 #define MDMODE_ADDRSEND	0x8
153 
154 struct sb_uart_state {
155 	unsigned int		close_delay;		/* msec */
156 	unsigned int		closing_wait;		/* msec */
157 
158 
159 	int			count;
160 	int			pm_state;
161 	struct sb_uart_info	*info;
162 	struct sb_uart_port	*port;
163 
164 	struct mutex		mutex;
165 };
166 
167 typedef unsigned int  uif_t;
168 
169 struct sb_uart_info {
170 	struct tty_struct	*tty;
171 	struct circ_buf		xmit;
172 	uif_t			flags;
173 
174 	int			blocked_open;
175 
176 	struct tasklet_struct	tlet;
177 
178 	wait_queue_head_t	open_wait;
179 	wait_queue_head_t	delta_msr_wait;
180 };
181 
182 
183 struct module;
184 struct tty_driver;
185 
186 struct uart_driver {
187 	struct module		*owner;
188 	const char		*driver_name;
189 	const char		*dev_name;
190 	int			 major;
191 	int			 minor;
192 	int			 nr;
193 	struct console		*cons;
194 
195 	struct sb_uart_state	*state;
196         struct tty_driver               *tty_driver;
197 };
198 
sb_uart_write_wakeup(struct sb_uart_port * port)199 void sb_uart_write_wakeup(struct sb_uart_port *port)
200 {
201     struct sb_uart_info *info = port->info;
202     tasklet_schedule(&info->tlet);
203 }
204 
sb_uart_update_timeout(struct sb_uart_port * port,unsigned int cflag,unsigned int baud)205 void sb_uart_update_timeout(struct sb_uart_port *port, unsigned int cflag,
206 			 unsigned int baud)
207 {
208     unsigned int bits;
209 
210     switch (cflag & CSIZE)
211     {
212         case CS5:
213             bits = 7;
214             break;
215 
216         case CS6:
217             bits = 8;
218             break;
219 
220         case CS7:
221             bits = 9;
222             break;
223 
224         default:
225             bits = 10;
226             break;
227     }
228 
229     if (cflag & CSTOPB)
230     {
231         bits++;
232     }
233 
234     if (cflag & PARENB)
235     {
236         bits++;
237     }
238 
239     bits = bits * port->fifosize;
240 
241     port->timeout = (HZ * bits) / baud + HZ/50;
242 }
sb_uart_get_baud_rate(struct sb_uart_port * port,struct MP_TERMIOS * termios,struct MP_TERMIOS * old,unsigned int min,unsigned int max)243 unsigned int sb_uart_get_baud_rate(struct sb_uart_port *port, struct MP_TERMIOS *termios,
244 				struct MP_TERMIOS *old, unsigned int min,
245 				unsigned int max)
246 {
247         unsigned int try, baud, altbaud = 38400;
248         upf_t flags = port->flags & UPF_SPD_MASK;
249 
250         if (flags == UPF_SPD_HI)
251                 altbaud = 57600;
252         if (flags == UPF_SPD_VHI)
253                 altbaud = 115200;
254         if (flags == UPF_SPD_SHI)
255                 altbaud = 230400;
256         if (flags == UPF_SPD_WARP)
257                 altbaud = 460800;
258 
259         for (try = 0; try < 2; try++) {
260 
261                 switch (termios->c_cflag & (CBAUD | CBAUDEX))
262                 {
263                 	case B921600    : baud = 921600;    break;
264                 	case B460800    : baud = 460800;    break;
265                 	case B230400    : baud = 230400;    break;
266                 	case B115200    : baud = 115200;    break;
267                 	case B57600     : baud = 57600;     break;
268                 	case B38400     : baud = 38400;     break;
269                 	case B19200     : baud = 19200;     break;
270                 	case B9600      : baud = 9600;      break;
271                 	case B4800      : baud = 4800;      break;
272                 	case B2400      : baud = 2400;      break;
273                 	case B1800      : baud = 1800;      break;
274                 	case B1200      : baud = 1200;      break;
275                 	case B600       : baud = 600;       break;
276                 	case B300       : baud = 300;       break;
277                         case B200       : baud = 200;       break;
278                 	case B150       : baud = 150;       break;
279                 	case B134       : baud = 134;       break;
280                 	case B110       : baud = 110;       break;
281                 	case B75        : baud = 75;        break;
282                 	case B50        : baud = 50;        break;
283                 	default         : baud = 9600;      break;
284                 }
285 
286                 if (baud == 38400)
287                         baud = altbaud;
288 
289                 if (baud == 0)
290                         baud = 9600;
291 
292                 if (baud >= min && baud <= max)
293                         return baud;
294 
295                 termios->c_cflag &= ~CBAUD;
296                 if (old) {
297                         termios->c_cflag |= old->c_cflag & CBAUD;
298                         old = NULL;
299                         continue;
300                 }
301 
302                 termios->c_cflag |= B9600;
303         }
304 
305         return 0;
306 }
sb_uart_get_divisor(struct sb_uart_port * port,unsigned int baud)307 unsigned int sb_uart_get_divisor(struct sb_uart_port *port, unsigned int baud)
308 {
309         unsigned int quot;
310 
311         if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
312                 quot = port->custom_divisor;
313         else
314                 quot = (port->uartclk + (8 * baud)) / (16 * baud);
315 
316         return quot;
317 }
318 
319 
320 
sb_uart_handle_break(struct sb_uart_port * port)321 static inline int sb_uart_handle_break(struct sb_uart_port *port)
322 {
323 	struct sb_uart_info *info = port->info;
324 
325 	if (port->flags & UPF_SAK)
326 		do_SAK(info->tty);
327 	return 0;
328 }
329 
sb_uart_handle_dcd_change(struct sb_uart_port * port,unsigned int status)330 static inline void sb_uart_handle_dcd_change(struct sb_uart_port *port, unsigned int status)
331 {
332 	struct sb_uart_info *info = port->info;
333 
334 	port->icount.dcd++;
335 
336 	if (info->flags & UIF_CHECK_CD) {
337 		if (status)
338 			wake_up_interruptible(&info->open_wait);
339 		else if (info->tty)
340 			tty_hangup(info->tty);
341 	}
342 }
343 
sb_uart_handle_cts_change(struct sb_uart_port * port,unsigned int status)344 static inline void sb_uart_handle_cts_change(struct sb_uart_port *port, unsigned int status)
345 {
346 	struct sb_uart_info *info = port->info;
347 	struct tty_struct *tty = info->tty;
348 
349 	port->icount.cts++;
350 
351 	if (info->flags & UIF_CTS_FLOW) {
352 		if (tty->hw_stopped) {
353 			if (status) {
354 				tty->hw_stopped = 0;
355 				port->ops->start_tx(port);
356 				sb_uart_write_wakeup(port);
357 			}
358 		} else {
359 			if (!status) {
360 				tty->hw_stopped = 1;
361 				port->ops->stop_tx(port);
362 			}
363 		}
364 	}
365 }
366 
367 
368 
369