1 /*
2  * Copyright (c) 2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this program; if not, write the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  */
29 
30 #ifndef _ASM_IA64_SN_SERIALIO_H
31 #define _ASM_IA64_SN_SERIALIO_H
32 
33 /*
34  * Definitions for the modular serial i/o driver.
35  *
36  * The modular serial i/o driver is a driver which has the hardware
37  * dependent and hardware independent parts separated into separate
38  * modules. The upper half is responsible for all hardware independent
39  * operations, specifically the interface to the kernel. An upper half
40  * may implement a streams interface, character interface, or whatever
41  * interface it wishes to the kernel. The same upper half may access any
42  * physical hardware through a set of standardized entry points into the
43  * lower level, which deals directly with the hardware. Whereas a
44  * separate upper layer exists for each kernel interface type (streams,
45  * character, polling etc), a separate lower level exists for each
46  * hardware type supported. Any upper and lower layer pair may be
47  * connected to form a complete driver. This file defines the interface
48  * between the two
49  */
50 
51 /* Definitions needed per port by both layers. Each lower layer
52  * declares a set of per-port private data areas describing each
53  * physical port, and by definition the first member of that private
54  * data is the following structure. Thus a pointer to the lower
55  * layer's private data is interchangeable with a pointer to the
56  * common private data, and the upper layer does not allocate anything
57  * so it does need to know anything about the physical configuration
58  * of the machine. This structure may also contain any hardware
59  * independent info that must be persistent across device closes.
60  */
61 typedef struct sioport {
62     /* calling vectors */
63     struct serial_calldown	*sio_calldown;
64     struct serial_callup	*sio_callup;
65 
66     void	*sio_upper;	/* upper layer's private data area */
67 
68     vertex_hdl_t sio_vhdl;	/* vertex handle of the hardware independent
69 				 * portion of this port (e.g. tty/1 without
70 				 * the d,m,f, etc)
71 				 */
72     spinlock_t  sio_lock;
73 } sioport_t;
74 
75 /* bits for sio_flags */
76 #define SIO_HWGRAPH_INITED	0x1
77 #define SIO_SPINLOCK_HELD	0x2
78 #define SIO_MUTEX_HELD		0x4
79 #define SIO_LOCKS_MASK (SIO_SPINLOCK_HELD | SIO_MUTEX_HELD)
80 
81 #if DEBUG
82 /* bits for sio_lockcalls, one per downcall except du_write which is
83  * not called by an upper layer.
84  */
85 #define L_OPEN		0x0001
86 #define L_CONFIG	0x0002
87 #define L_ENABLE_HFC	0x0004
88 #define L_SET_EXTCLK	0x0008
89 #define L_WRITE		0x0010
90 #define L_BREAK		0x0020
91 #define L_READ		0x0040
92 #define L_NOTIFICATION	0x0080
93 #define L_RX_TIMEOUT	0x0100
94 #define L_SET_DTR	0x0200
95 #define L_SET_RTS	0x0400
96 #define L_QUERY_DCD	0x0800
97 #define L_QUERY_CTS	0x1000
98 #define L_SET_PROTOCOL	0x2000
99 #define L_ENABLE_TX	0x4000
100 
101 #define L_LOCK_ALL	(~0)
102 
103 /* debug lock assertion: each lower layer entry point does an
104  * assertion with the following macro, passing in the port passed to
105  * the entry point and the bit corresponding to which entry point it
106  * is. If the upper layer has turned on the bit for that entry point,
107  * sio_port_islocked is called, thus an upper layer may specify that
108  * it is ok for a particular downcall to be made without the port lock
109  * held.
110  */
111 #define L_LOCKED(port, flag) (((port)->sio_lockcalls & (flag)) == 0 || \
112 			      sio_port_islocked(port))
113 #endif
114 
115 /* flags for next_char_state */
116 #define NCS_BREAK 1
117 #define NCS_PARITY 2
118 #define NCS_FRAMING 4
119 #define NCS_OVERRUN 8
120 
121 /* protocol types for DOWN_SET_PROTOCOL */
122 enum sio_proto {
123     PROTO_RS232,
124     PROTO_RS422
125 };
126 
127 /* calldown vector. This is a set of entry points into a lower layer
128  * module, providing black-box access to the hardware by the upper
129  * layer
130  */
131 struct serial_calldown {
132 
133     /* hardware configuration */
134     int (*down_open)		(sioport_t *port);
135     int (*down_config)		(sioport_t *port, int baud, int byte_size,
136 				 int stop_bits, int parenb, int parodd);
137     int (*down_enable_hfc)	(sioport_t *port, int enable);
138     int (*down_set_extclk)	(sioport_t *port, int clock_factor);
139 
140     /* data transmission */
141     int (*down_write)		(sioport_t *port, char *buf, int len);
142     int (*down_du_write)	(sioport_t *port, char *buf, int len);
143     void (*down_du_flush)	(sioport_t *port);
144     int (*down_break)		(sioport_t *port, int brk);
145     int (*down_enable_tx)	(sioport_t *port, int enb);
146 
147     /* data reception */
148     int (*down_read)		(sioport_t *port, char *buf, int len);
149 
150     /* event notification */
151     int (*down_notification)	(sioport_t *port, int mask, int on);
152     int (*down_rx_timeout)	(sioport_t *port, int timeout);
153 
154     /* modem control */
155     int (*down_set_DTR)		(sioport_t *port, int dtr);
156     int (*down_set_RTS)		(sioport_t *port, int rts);
157     int (*down_query_DCD)	(sioport_t *port);
158     int (*down_query_CTS)	(sioport_t *port);
159 
160     /* transmission protocol */
161     int (*down_set_protocol)	(sioport_t *port, enum sio_proto protocol);
162 
163     /* memory mapped user driver support */
164     int (*down_mapid)		(sioport_t *port, void *arg);
165     int (*down_map)		(sioport_t *port, uint64_t *vt, off_t off);
166     void (*down_unmap)		(sioport_t *port);
167     int (*down_set_sscr)	(sioport_t *port, int arg, int flag);
168 };
169 
170 /*
171  * Macros used by the upper layer to access the lower layer. Unless
172  * otherwise noted, all integer functions should return 0 on success
173  * or 1 if the hardware does not support the requested operation. In
174  * the case of non-support, the upper layer may work around the problem
175  * where appropriate or just notify the user.
176  * For hardware which supports detaching, these functions should
177  * return -1 if the hardware is no longer present.
178  */
179 
180 /* open a device. Do whatever initialization/resetting necessary */
181 #define DOWN_OPEN(p) \
182     ((p)->sio_calldown->down_open(p))
183 
184 /* configure the hardware with the given baud rate, number of stop
185  * bits, byte size and parity
186  */
187 #define DOWN_CONFIG(p, a, b, c, d, e) \
188     ((p)->sio_calldown->down_config(p, a, b, c, d, e))
189 
190 /* Enable hardware flow control. If the hardware does not support
191  * this, the upper layer will emulate HFC by manipulating RTS and CTS
192  */
193 #define DOWN_ENABLE_HFC(p, enb) \
194     ((p)->sio_calldown->down_enable_hfc(p, enb))
195 
196 /* Set external clock to the given clock factor. If cf is zero,
197  * internal clock is used. If cf is non-zero external clock is used
198  * and the clock is cf times the baud.
199  */
200 #define DOWN_SET_EXTCLK(p, cf) \
201     ((p)->sio_calldown->down_set_extclk(p, cf))
202 
203 /* Write bytes to the device. The number of bytes actually written is
204  * returned. The upper layer will continue to call this function until
205  * it has no more data to send or until 0 is returned, indicating that
206  * no more bytes may be sent until some have drained.
207  */
208 #define DOWN_WRITE(p, buf, len) \
209     ((p)->sio_calldown->down_write(p, buf, len))
210 
211 /* Same as DOWN_WRITE, but called only from synchronous du output
212  * routines. Allows lower layer the option of implementing kernel
213  * printfs differently than ordinary console output.
214  */
215 #define DOWN_DU_WRITE(p, buf, len) \
216     ((p)->sio_calldown->down_du_write(p, buf, len))
217 
218 /* Flushes previous down_du_write() calls.  Needed on serial controllers
219  * that can heavily buffer output like IOC3 for conbuf_flush().
220  */
221 #define DOWN_DU_FLUSH(p) \
222      ((p)->sio_calldown->down_du_flush(p))
223 
224 /* Set the output break condition high or low */
225 #define DOWN_BREAK(p, brk) \
226     ((p)->sio_calldown->down_break(p, brk))
227 
228 /* Enable/disable TX for soft flow control */
229 #define DOWN_ENABLE_TX(p) \
230     ((p)->sio_calldown->down_enable_tx(p, 1))
231 #define DOWN_DISABLE_TX(p) \
232     ((p)->sio_calldown->down_enable_tx(p, 0))
233 
234 /* Read bytes from the device. The number of bytes actually read is
235  * returned. All bytes returned by a single call have the same error
236  * status. Thus if the device has 10 bytes queued for input and byte 5
237  * has a parity error, the first call to DOWN_READ will return bytes 0-4
238  * only. A subsequent call to DOWN_READ will first cause a call to
239  * UP_PARITY_ERROR to notify the upper layer that the next byte has an
240  * error, and then the call to DOWN_READ returns byte 5 alone. A
241  * subsequent call to DOWN_READ returns bytes 6-9. The upper layer
242  * continues to call DOWN_READ until 0 is returned, or until it runs out
243  * of buffer space to receive the chars.
244  */
245 #define DOWN_READ(p, buf, len) \
246     ((p)->sio_calldown->down_read(p, buf, len))
247 
248 /* Turn on/off event notification for the specified events. Notification
249  * status is unchanged for those events not specified.
250  */
251 #define DOWN_NOTIFICATION(p, mask, on) \
252     ((p)->sio_calldown->down_notification(p, mask, on))
253 
254 /* Notification types. 1 per upcall. The upper layer can specify
255  * exactly which upcalls it wishes to receive. UP_DETACH is mandatory
256  * when applicable and cannot be enabled/disabled.
257  */
258 #define N_DATA_READY	0x01
259 #define N_OUTPUT_LOWAT	0x02
260 #define N_BREAK		0x04
261 #define N_PARITY_ERROR	0x08
262 #define N_FRAMING_ERROR	0x10
263 #define N_OVERRUN_ERROR	0x20
264 #define N_DDCD		0x40
265 #define N_DCTS		0x80
266 
267 #define N_ALL_INPUT	(N_DATA_READY | N_BREAK |			\
268 			 N_PARITY_ERROR | N_FRAMING_ERROR |		\
269 			 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
270 
271 #define N_ALL_OUTPUT	N_OUTPUT_LOWAT
272 
273 #define N_ALL_ERRORS	(N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
274 
275 #define N_ALL		(N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK |	\
276 			 N_PARITY_ERROR | N_FRAMING_ERROR |		\
277 			 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
278 
279 /* Instruct the lower layer that the upper layer would like to be
280  * notified every t ticks when data is being received. If data is
281  * streaming in, the lower layer should buffer enough data that
282  * notification is not required more often than requested, and set a
283  * timeout so that notification does not occur less often than
284  * requested. If the lower layer does not support such operations, it
285  * should return 1, indicating that the upper layer should emulate these
286  * functions in software.
287  */
288 #define DOWN_RX_TIMEOUT(p, t) \
289     ((p)->sio_calldown->down_rx_timeout(p, t))
290 
291 /* Set the output value of DTR */
292 #define DOWN_SET_DTR(p, dtr) \
293     ((p)->sio_calldown->down_set_DTR(p, dtr))
294 
295 /* Set the output value of RTS */
296 #define DOWN_SET_RTS(p, rts) \
297     ((p)->sio_calldown->down_set_RTS(p, rts))
298 
299 /* Query current input value of DCD */
300 #define DOWN_QUERY_DCD(p) \
301     ((p)->sio_calldown->down_query_DCD(p))
302 
303 /* Query current input value of CTS */
304 #define DOWN_QUERY_CTS(p) \
305     ((p)->sio_calldown->down_query_CTS(p))
306 
307 /* Set transmission protocol */
308 #define DOWN_SET_PROTOCOL(p, proto) \
309     ((p)->sio_calldown->down_set_protocol(p, proto))
310 
311 /* Query mapped interface type */
312 #define DOWN_GET_MAPID(p, arg) \
313     ((p)->sio_calldown->down_mapid(p, arg))
314 
315 /* Perform mapping to user address space */
316 #define DOWN_MAP(p, vt, off) \
317     ((p)->sio_calldown->down_map(p, vt, off))
318 
319 /* Cleanup after mapped port is closed */
320 #define DOWN_UNMAP(p) \
321     ((p)->sio_calldown->down_unmap(p))
322 
323 /* Set/Reset ioc3 sscr register */
324 #define DOWN_SET_SSCR(p, arg, flag) \
325     ((p)->sio_calldown->down_set_sscr(p, arg, flag))
326 
327 
328 /* The callup struct. This is a set of entry points providing
329  * black-box access to the upper level kernel interface by the
330  * hardware handling code. These entry points are used for event
331  * notification
332  */
333 struct serial_callup {
334     void (*up_data_ready)	(sioport_t *port);
335     void (*up_output_lowat)	(sioport_t *port);
336     void (*up_ncs)		(sioport_t *port, int ncs);
337     void (*up_dDCD)		(sioport_t *port, int dcd);
338     void (*up_dCTS)		(sioport_t *port, int cts);
339     void (*up_detach)		(sioport_t *port);
340 };
341 
342 /*
343  * Macros used by the lower layer to access the upper layer for event
344  * notificaiton. These functions are generally called in response to
345  * an interrupt. Since the port lock may be released across UP calls,
346  * we must check the callup vector each time. However since the port
347  * lock is held during DOWN calls (from which these UP calls are made)
348  * there is no danger of the sio_callup vector being cleared between
349  * where it is checked and where it is used in the macro
350  */
351 
352 /* Notify the upper layer that there are input bytes available and
353  * DOWN_READ may now be called
354  */
355 #define UP_DATA_READY(p) \
356     ((p)->sio_callup ? (p)->sio_callup->up_data_ready(p):(void)0)
357 
358 /* Notify the upper layer that the lower layer has freed up some
359  * output buffer space and DOWN_WRITE may now be called
360  */
361 #define UP_OUTPUT_LOWAT(p) \
362     ((p)->sio_callup ? (p)->sio_callup->up_output_lowat(p):(void)0)
363 
364 /* Notify the upper layer that the next char returned by DOWN_READ
365  * has the indicated special status. (see NCS_* above)
366  */
367 #define UP_NCS(p, ncs) \
368     ((p)->sio_callup ? (p)->sio_callup->up_ncs(p, ncs):(void)0)
369 
370 /* Notify the upper layer of the new DCD input value */
371 #define UP_DDCD(p, dcd) \
372     ((p)->sio_callup ? (p)->sio_callup->up_dDCD(p, dcd):(void)0)
373 
374 /* Notify the upper layer of the new CTS input value */
375 #define UP_DCTS(p, cts) \
376     ((p)->sio_callup ? (p)->sio_callup->up_dCTS(p, cts):(void)0)
377 
378 /* notify the upper layer that the lower layer hardware has been detached
379  * Since the port lock is NOT held when this macro is executed, we must
380  * guard against the sio_callup vector being cleared between when we check
381  * it and when we make the upcall, so we use a local copy.
382  */
383 #define UP_DETACH(p) \
384 { \
385     struct serial_callup *up; \
386     if ((up = (p)->sio_callup)) \
387 	up->up_detach(p); \
388 }
389 
390 /* Port locking protocol:
391  * Any time a DOWN call is made into one of the lower layer entry points,
392  * the corresponding port is already locked and remains locked throughout
393  * that downcall. When a lower layer routine makes an UP call, the port
394  * is assumed to be locked on entry to the upper layer routine, but the
395  * upper layer routine may release and reacquire the lock if it wishes.
396  * Thus the lower layer routine should not rely on the port lock being
397  * held across upcalls. Further, since the port may be disconnected
398  * any time the port lock is not held, an UP call may cause subsequent
399  * UP calls to become noops since the upcall vector will be zeroed when
400  * the port is closed. Thus, any lower layer routine making UP calls must
401  * be prepared to deal with the possibility that any UP calls it makes
402  * are noops.
403  *
404  * The only time a lower layer routine should manipulate the port lock
405  * is the lower layer interrupt handler, which should acquire the lock
406  * during its critical execution.
407  *
408  * Any function which assumes that the port is or isn't locked should
409  * use the function sio_port_islocked in an ASSERT statement to verify
410  * this assumption
411  */
412 
413 #if DEBUG
414 extern int sio_port_islocked(sioport_t *);
415 #endif
416 
417 #define SIO_LOCK_PORT(port, flags)	spin_lock_irqsave(&port->sio_lock, flags)
418 #define SIO_UNLOCK_PORT(port, flags)	spin_unlock_irqrestore(&port->sio_lock, flags)
419 
420 /* kernel debugger support */
421 #ifdef _LANGUAGE_C
422 extern int console_is_tport;
423 #define CNTRL_A		'\001'
424 #if DEBUG
425 #ifndef DEBUG_CHAR
426 #define DEBUG_CHAR	CNTRL_A
427 #endif
428 #else
429 #define DEBUG_CHAR	CNTRL_A
430 #endif
431 #endif
432 
433 
434 extern void ioc4_serial_initport(sioport_t *, int);
435 
436 
437 /* flags to notify sio_initport() which type of nodes are
438  * desired for a particular hardware type
439  */
440 #define NODE_TYPE_D		0x01 /* standard plain streams interface */
441 #define NODE_TYPE_MODEM		0x02 /* modem streams interface */
442 #define NODE_TYPE_FLOW_MODEM	0x04 /* modem/flow control streams */
443 #define NODE_TYPE_CHAR		0x08 /* character interface */
444 #define NODE_TYPE_MIDI		0x10 /* midi interface */
445 #define NODE_TYPE_D_RS422	0x20 /* RS422 without flow control */
446 #define NODE_TYPE_FLOW_RS422	0x40 /* RS422 with flow control */
447 
448 #define NODE_TYPE_USER		0x80 /* user mapped interface */
449 #define NODE_TYPE_TIMESTAMPED	0x100 /* user mapped interface */
450 
451 #define NODE_TYPE_ALL_RS232	(NODE_TYPE_D | NODE_TYPE_MODEM | \
452 				 NODE_TYPE_FLOW_MODEM | NODE_TYPE_CHAR | \
453 				 NODE_TYPE_MIDI | NODE_TYPE_TIMESTAMPED)
454 #define NODE_TYPE_ALL_RS422	(NODE_TYPE_D_RS422 | NODE_TYPE_FLOW_RS422 | \
455 				 NODE_TYPE_TIMESTAMPED)
456 
457 /* Flags for devflags field of miditype structure */
458 #define MIDIDEV_EXTERNAL 0             /* lower half initializes devflags to this for an external device */
459 #define MIDIDEV_INTERNAL 0x2
460 
461 #define MIDIDEV_UNREGISTERED -1    /* Initialization for portidx field of miditype structure */
462 
463 typedef struct miditype_s{
464   int devflags;                    /* DEV_EXTERNAL, DEV_INTERNAL */
465   int portidx;
466   void *midi_upper;
467   sioport_t *port;
468 } miditype_t;
469 
470 typedef struct tsiotype_s{
471   void *tsio_upper;
472   sioport_t *port;
473   int portidx;
474   int urbidx;
475 } tsiotype_t;
476 
477 #endif /* _ASM_IA64_SN_SERIALIO_H */
478