1 /*	$NetBSD: comvar.h,v 1.82 2016/05/27 20:01:49 bouyer Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "opt_multiprocessor.h"
34 #include "opt_lockdebug.h"
35 #include "opt_com.h"
36 #include "opt_kgdb.h"
37 
38 #ifdef RND_COM
39 #include <sys/rndsource.h>
40 #endif
41 
42 #include <sys/callout.h>
43 #include <sys/timepps.h>
44 #include <sys/mutex.h>
45 #include <sys/device.h>
46 
47 #include <dev/ic/comreg.h>	/* for COM_NPORTS */
48 
49 struct com_regs;
50 
51 int comcnattach(bus_space_tag_t, bus_addr_t, int, int, int, tcflag_t);
52 int comcnattach1(struct com_regs *, int, int, int, tcflag_t);
53 
54 #ifdef KGDB
55 int com_kgdb_attach(bus_space_tag_t, bus_addr_t, int, int, int, tcflag_t);
56 int com_kgdb_attach1(struct com_regs *, int, int, int, tcflag_t);
57 #endif
58 
59 int com_is_console(bus_space_tag_t, bus_addr_t, bus_space_handle_t *);
60 
61 /* Hardware flag masks */
62 #define	COM_HW_NOIEN	0x01
63 #define	COM_HW_FIFO	0x02
64 		/*	0x04	free for use */
65 #define	COM_HW_FLOW	0x08
66 #define	COM_HW_DEV_OK	0x20
67 #define	COM_HW_CONSOLE	0x40
68 #define	COM_HW_KGDB	0x80
69 #define	COM_HW_TXFIFO_DISABLE	0x100
70 #define	COM_HW_NO_TXPRELOAD	0x200
71 #define	COM_HW_AFE	0x400
72 
73 /* Buffer size for character buffer */
74 #ifndef COM_RING_SIZE
75 #define	COM_RING_SIZE	2048
76 #endif
77 
78 #ifdef	COM_REGMAP
79 #define	COM_REG_RXDATA		0
80 #define	COM_REG_TXDATA		1
81 #define	COM_REG_DLBL		2
82 #define	COM_REG_DLBH		3
83 #define	COM_REG_IER		4
84 #define	COM_REG_IIR		5
85 #define	COM_REG_FIFO		6
86 #define	COM_REG_TCR		6
87 #define	COM_REG_EFR		7
88 #define	COM_REG_TLR		7
89 #define	COM_REG_LCR		8
90 #define	COM_REG_MDR1		8
91 #define	COM_REG_MCR		9
92 #define	COM_REG_LSR		10
93 #define	COM_REG_MSR		11
94 #ifdef	COM_16750
95 #define	COM_REG_USR		31
96 #endif
97 #ifdef	COM_AWIN
98 #define	COM_REG_USR		31
99 #define	COM_REG_TFL		32
100 #define	COM_REG_RFL		33
101 #define	COM_REG_HALT		41
102 #endif
103 
104 struct com_regs {
105 	bus_space_tag_t		cr_iot;
106 	bus_space_handle_t	cr_ioh;
107 	bus_addr_t		cr_iobase;
108 	bus_size_t		cr_nports;
109 #ifdef COM_16750
110 	bus_size_t		cr_map[32];
111 #else
112 	bus_size_t		cr_map[16];
113 #endif
114 };
115 
116 #ifdef COM_16750
117 extern const bus_size_t com_std_map[32];
118 #else
119 extern const bus_size_t com_std_map[16];
120 #endif
121 
122 #define	COM_INIT_REGS(regs, tag, hdl, addr)				\
123 	do {								\
124 		regs.cr_iot = tag;					\
125 		regs.cr_ioh = hdl;					\
126 		regs.cr_iobase = addr;					\
127 		regs.cr_nports = COM_NPORTS;				\
128 		memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));	\
129 	} while (0)
130 
131 #else
132 #define	COM_REG_RXDATA		com_data
133 #define	COM_REG_TXDATA		com_data
134 #define	COM_REG_DLBL		com_dlbl
135 #define	COM_REG_DLBH		com_dlbh
136 #define	COM_REG_IER		com_ier
137 #define	COM_REG_IIR		com_iir
138 #define	COM_REG_FIFO		com_fifo
139 #define	COM_REG_EFR		com_efr
140 #define	COM_REG_LCR		com_lctl
141 #define	COM_REG_MCR		com_mcr
142 #define	COM_REG_LSR		com_lsr
143 #define	COM_REG_MSR		com_msr
144 #define	COM_REG_TCR		com_msr
145 #define	COM_REG_TLR		com_scratch
146 #define	COM_REG_MDR1		8
147 #ifdef	COM_16750
148 #define COM_REG_USR		com_usr
149 #endif
150 #ifdef	COM_AWIN
151 #define COM_REG_USR		com_usr
152 #define	COM_REG_TFL		com_tfl
153 #define	COM_REG_RFL		com_rfl
154 #define	COM_REG_HALT		com_halt
155 #endif
156 
157 struct com_regs {
158 	bus_space_tag_t		cr_iot;
159 	bus_space_handle_t	cr_ioh;
160 	bus_addr_t		cr_iobase;
161 	bus_size_t		cr_nports;
162 };
163 
164 #define	COM_INIT_REGS(regs, tag, hdl, addr)		\
165 	do {						\
166 		regs.cr_iot = tag;			\
167 		regs.cr_ioh = hdl;			\
168 		regs.cr_iobase = addr;			\
169 		regs.cr_nports = COM_NPORTS;		\
170 	} while (0)
171 
172 #endif
173 
174 struct comcons_info {
175 	struct com_regs regs;
176 	int rate;
177 	int frequency;
178 	int type;
179 	tcflag_t cflag;
180 };
181 
182 struct com_softc {
183 	device_t sc_dev;
184 	void *sc_si;
185 	struct tty *sc_tty;
186 
187 	struct callout sc_diag_callout;
188 
189 	int sc_frequency;
190 
191 	struct com_regs sc_regs;
192 	bus_space_handle_t sc_hayespioh;
193 
194 
195 	u_int sc_overflows,
196 	      sc_floods,
197 	      sc_errors;
198 
199 	int sc_hwflags,
200 	    sc_swflags;
201 	u_int sc_fifolen;
202 
203 	u_int sc_r_hiwat,
204 	      sc_r_lowat;
205 	u_char *volatile sc_rbget,
206 	       *volatile sc_rbput;
207  	volatile u_int sc_rbavail;
208 	u_char *sc_rbuf,
209 	       *sc_ebuf;
210 
211  	u_char *sc_tba;
212  	u_int sc_tbc,
213 	      sc_heldtbc;
214 
215 	volatile u_char sc_rx_flags,
216 #define	RX_TTY_BLOCKED		0x01
217 #define	RX_TTY_OVERFLOWED	0x02
218 #define	RX_IBUF_BLOCKED		0x04
219 #define	RX_IBUF_OVERFLOWED	0x08
220 #define	RX_ANY_BLOCK		0x0f
221 			sc_tx_busy,
222 			sc_tx_done,
223 			sc_tx_stopped,
224 			sc_st_check,
225 			sc_rx_ready;
226 
227 	volatile u_char sc_heldchange;
228 	volatile u_char sc_msr, sc_msr_delta, sc_msr_mask, sc_mcr,
229 	    sc_mcr_active, sc_lcr, sc_ier, sc_fifo, sc_dlbl, sc_dlbh, sc_efr;
230 	u_char sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
231 
232 #ifdef COM_HAYESP
233 	u_char sc_prescaler;
234 #endif
235 
236 	/*
237 	 * There are a great many almost-ns16550-compatible UARTs out
238 	 * there, which have minor differences.  The type field here
239 	 * lets us distinguish between them.
240 	 */
241 	int sc_type;
242 #define	COM_TYPE_NORMAL		0	/* normal 16x50 */
243 #define	COM_TYPE_HAYESP		1	/* Hayes ESP modem */
244 #define	COM_TYPE_PXA2x0		2	/* Intel PXA2x0 processor built-in */
245 #define	COM_TYPE_AU1x00		3	/* AMD/Alchemy Au1x000 proc. built-in */
246 #define	COM_TYPE_OMAP		4	/* TI OMAP processor built-in */
247 #define	COM_TYPE_16550_NOERS	5	/* like a 16550, no ERS */
248 #define	COM_TYPE_INGENIC	6	/* JZ4780 built-in */
249 #define	COM_TYPE_TEGRA		7	/* NVIDIA Tegra built-in */
250 
251 	/* power management hooks */
252 	int (*enable)(struct com_softc *);
253 	void (*disable)(struct com_softc *);
254 	int enabled;
255 
256 	/* XXXX: vendor workaround functions */
257 	int (*sc_vendor_workaround)(struct com_softc *);
258 
259 	struct pps_state sc_pps_state;	/* pps state */
260 
261 #ifdef RND_COM
262 	krndsource_t  rnd_source;
263 #endif
264 	kmutex_t		sc_lock;
265 };
266 
267 int comprobe1(bus_space_tag_t, bus_space_handle_t);
268 int comintr(void *);
269 void com_attach_subr(struct com_softc *);
270 int com_probe_subr(struct com_regs *);
271 int com_detach(device_t, int);
272 bool com_resume(device_t, const pmf_qual_t *);
273 bool com_cleanup(device_t, int);
274 bool com_suspend(device_t, const pmf_qual_t *);
275 
276 #ifndef IPL_SERIAL
277 #define	IPL_SERIAL	IPL_TTY
278 #define	splserial()	spltty()
279 #endif
280