comvar.h revision 1.88 1 /* $NetBSD: comvar.h,v 1.88 2018/11/30 16:26:19 jmcneill 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 #define COM_HW_POLL 0x800
73
74 /* Buffer size for character buffer */
75 #ifndef COM_RING_SIZE
76 #define COM_RING_SIZE 2048
77 #endif
78
79 #ifdef COM_REGMAP
80 #define COM_REG_RXDATA 0
81 #define COM_REG_TXDATA 1
82 #define COM_REG_DLBL 2
83 #define COM_REG_DLBH 3
84 #define COM_REG_IER 4
85 #define COM_REG_IIR 5
86 #define COM_REG_FIFO 6
87 #define COM_REG_TCR 6
88 #define COM_REG_EFR 7
89 #define COM_REG_TLR 7
90 #define COM_REG_LCR 8
91 #define COM_REG_MDR1 8
92 #define COM_REG_MCR 9
93 #define COM_REG_LSR 10
94 #define COM_REG_MSR 11
95 #define COM_REG_USR 31 /* 16750/DW APB */
96 #define COM_REG_TFL com_tfl /* DW APB */
97 #define COM_REG_RFL com_rfl /* DW APB */
98 #define COM_REG_HALT com_halt /* DW APB */
99
100 struct com_regs {
101 bus_space_tag_t cr_iot;
102 bus_space_handle_t cr_ioh;
103 bus_addr_t cr_iobase;
104 bus_size_t cr_nports;
105 bus_size_t cr_map[42];
106 };
107
108 extern const bus_size_t com_std_map[42];
109
110 #define COM_INIT_REGS(regs, tag, hdl, addr) \
111 do { \
112 regs.cr_iot = tag; \
113 regs.cr_ioh = hdl; \
114 regs.cr_iobase = addr; \
115 regs.cr_nports = COM_NPORTS; \
116 memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map)); \
117 } while (0)
118
119 #else
120 #define COM_REG_RXDATA com_data
121 #define COM_REG_TXDATA com_data
122 #define COM_REG_DLBL com_dlbl
123 #define COM_REG_DLBH com_dlbh
124 #define COM_REG_IER com_ier
125 #define COM_REG_IIR com_iir
126 #define COM_REG_FIFO com_fifo
127 #define COM_REG_EFR com_efr
128 #define COM_REG_LCR com_lctl
129 #define COM_REG_MCR com_mcr
130 #define COM_REG_LSR com_lsr
131 #define COM_REG_MSR com_msr
132 #define COM_REG_TCR com_msr
133 #define COM_REG_TLR com_scratch
134 #define COM_REG_MDR1 8
135 #define COM_REG_USR com_usr /* 16750/DW APB */
136 #define COM_REG_TFL com_tfl /* DW APB */
137 #define COM_REG_RFL com_rfl /* DW APB */
138 #define COM_REG_HALT com_halt /* DW APB */
139
140 struct com_regs {
141 bus_space_tag_t cr_iot;
142 bus_space_handle_t cr_ioh;
143 bus_addr_t cr_iobase;
144 bus_size_t cr_nports;
145 };
146
147 #define COM_INIT_REGS(regs, tag, hdl, addr) \
148 do { \
149 regs.cr_iot = tag; \
150 regs.cr_ioh = hdl; \
151 regs.cr_iobase = addr; \
152 regs.cr_nports = COM_NPORTS; \
153 } while (0)
154
155 #endif
156
157 struct comcons_info {
158 struct com_regs regs;
159 int rate;
160 int frequency;
161 int type;
162 tcflag_t cflag;
163 };
164
165 struct com_softc {
166 device_t sc_dev;
167 void *sc_si;
168 struct tty *sc_tty;
169
170 callout_t sc_diag_callout;
171 callout_t sc_poll_callout;
172
173 int sc_frequency;
174
175 struct com_regs sc_regs;
176 bus_space_handle_t sc_hayespioh;
177
178
179 u_int sc_overflows,
180 sc_floods,
181 sc_errors;
182
183 int sc_hwflags,
184 sc_swflags;
185 u_int sc_fifolen;
186
187 u_int sc_r_hiwat,
188 sc_r_lowat;
189 u_char *volatile sc_rbget,
190 *volatile sc_rbput;
191 volatile u_int sc_rbavail;
192 u_char *sc_rbuf,
193 *sc_ebuf;
194
195 u_char *sc_tba;
196 u_int sc_tbc,
197 sc_heldtbc;
198
199 volatile u_char sc_rx_flags,
200 #define RX_TTY_BLOCKED 0x01
201 #define RX_TTY_OVERFLOWED 0x02
202 #define RX_IBUF_BLOCKED 0x04
203 #define RX_IBUF_OVERFLOWED 0x08
204 #define RX_ANY_BLOCK 0x0f
205 sc_tx_busy,
206 sc_tx_done,
207 sc_tx_stopped,
208 sc_st_check,
209 sc_rx_ready;
210
211 volatile u_char sc_heldchange;
212 volatile u_char sc_msr, sc_msr_delta, sc_msr_mask, sc_mcr,
213 sc_mcr_active, sc_lcr, sc_ier, sc_fifo, sc_dlbl, sc_dlbh, sc_efr;
214 u_char sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
215
216 u_char sc_prescaler; /* for COM_TYPE_HAYESP */
217
218 /*
219 * There are a great many almost-ns16550-compatible UARTs out
220 * there, which have minor differences. The type field here
221 * lets us distinguish between them.
222 */
223 int sc_type;
224 #define COM_TYPE_NORMAL 0 /* normal 16x50 */
225 #define COM_TYPE_HAYESP 1 /* Hayes ESP modem */
226 #define COM_TYPE_PXA2x0 2 /* Intel PXA2x0 processor built-in */
227 #define COM_TYPE_AU1x00 3 /* AMD/Alchemy Au1x000 proc. built-in */
228 #define COM_TYPE_OMAP 4 /* TI OMAP processor built-in */
229 #define COM_TYPE_16550_NOERS 5 /* like a 16550, no ERS */
230 #define COM_TYPE_INGENIC 6 /* JZ4780 built-in */
231 #define COM_TYPE_TEGRA 7 /* NVIDIA Tegra built-in */
232 #define COM_TYPE_BCMAUXUART 8 /* BCM2835 AUX UART */
233 #define COM_TYPE_16650 9
234 #define COM_TYPE_16750 10
235 #define COM_TYPE_DW_APB 11 /* DesignWare APB UART */
236
237 /* power management hooks */
238 int (*enable)(struct com_softc *);
239 void (*disable)(struct com_softc *);
240 int enabled;
241
242 /* XXXX: vendor workaround functions */
243 int (*sc_vendor_workaround)(struct com_softc *);
244
245 struct pps_state sc_pps_state; /* pps state */
246
247 #ifdef RND_COM
248 krndsource_t rnd_source;
249 #endif
250 kmutex_t sc_lock;
251 };
252
253 int comprobe1(bus_space_tag_t, bus_space_handle_t);
254 int comintr(void *);
255 void com_attach_subr(struct com_softc *);
256 int com_probe_subr(struct com_regs *);
257 int com_detach(device_t, int);
258 bool com_resume(device_t, const pmf_qual_t *);
259 bool com_cleanup(device_t, int);
260 bool com_suspend(device_t, const pmf_qual_t *);
261
262 #ifndef IPL_SERIAL
263 #define IPL_SERIAL IPL_TTY
264 #define splserial() spltty()
265 #endif
266