sab.c revision 1.1.2.3 1 1.1.2.3 nathanw /* $NetBSD: sab.c,v 1.1.2.3 2002/09/17 21:17:59 nathanw Exp $ */
2 1.1.2.2 nathanw /* $OpenBSD: sab.c,v 1.7 2002/04/08 17:49:42 jason Exp $ */
3 1.1.2.2 nathanw
4 1.1.2.2 nathanw /*
5 1.1.2.2 nathanw * Copyright (c) 2001 Jason L. Wright (jason (at) thought.net)
6 1.1.2.2 nathanw * All rights reserved.
7 1.1.2.2 nathanw *
8 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
9 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
10 1.1.2.2 nathanw * are met:
11 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
12 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
13 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
15 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
16 1.1.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
17 1.1.2.2 nathanw * must display the following acknowledgement:
18 1.1.2.2 nathanw * This product includes software developed by Jason L. Wright
19 1.1.2.2 nathanw * 4. The name of the author may not be used to endorse or promote products
20 1.1.2.2 nathanw * derived from this software without specific prior written permission.
21 1.1.2.2 nathanw *
22 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.1.2.2 nathanw * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 1.1.2.2 nathanw * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 1.1.2.2 nathanw * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 1.1.2.2 nathanw * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 1.1.2.2 nathanw * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 1.1.2.2 nathanw * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 1.1.2.2 nathanw * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
33 1.1.2.2 nathanw *
34 1.1.2.2 nathanw * Effort sponsored in part by the Defense Advanced Research Projects
35 1.1.2.2 nathanw * Agency (DARPA) and Air Force Research Laboratory, Air Force
36 1.1.2.2 nathanw * Materiel Command, USAF, under agreement number F30602-01-2-0537.
37 1.1.2.2 nathanw *
38 1.1.2.2 nathanw */
39 1.1.2.2 nathanw
40 1.1.2.2 nathanw /*
41 1.1.2.2 nathanw * SAB82532 Dual UART driver
42 1.1.2.2 nathanw */
43 1.1.2.2 nathanw
44 1.1.2.2 nathanw #include <sys/types.h>
45 1.1.2.2 nathanw #include <sys/param.h>
46 1.1.2.2 nathanw #include <sys/systm.h>
47 1.1.2.2 nathanw #include <sys/device.h>
48 1.1.2.2 nathanw #include <sys/conf.h>
49 1.1.2.2 nathanw #include <sys/file.h>
50 1.1.2.2 nathanw #include <sys/ioctl.h>
51 1.1.2.2 nathanw #include <sys/kernel.h>
52 1.1.2.2 nathanw #include <sys/proc.h>
53 1.1.2.2 nathanw #include <sys/tty.h>
54 1.1.2.2 nathanw #include <sys/syslog.h>
55 1.1.2.2 nathanw
56 1.1.2.2 nathanw #include <machine/autoconf.h>
57 1.1.2.2 nathanw #include <machine/openfirm.h>
58 1.1.2.2 nathanw
59 1.1.2.2 nathanw #include <dev/cons.h>
60 1.1.2.2 nathanw
61 1.1.2.2 nathanw #include <dev/ebus/ebusreg.h>
62 1.1.2.2 nathanw #include <dev/ebus/ebusvar.h>
63 1.1.2.2 nathanw #include <sparc64/dev/sab82532reg.h>
64 1.1.2.2 nathanw
65 1.1.2.2 nathanw #define SABUNIT(x) (minor(x) & 0x7ffff)
66 1.1.2.2 nathanw #define SABDIALOUT(x) (minor(x) & 0x80000)
67 1.1.2.2 nathanw
68 1.1.2.2 nathanw #define SABTTY_RBUF_SIZE 1024 /* must be divisible by 2 */
69 1.1.2.2 nathanw
70 1.1.2.2 nathanw struct sab_softc {
71 1.1.2.2 nathanw struct device sc_dv;
72 1.1.2.2 nathanw struct intrhand * sc_ih;
73 1.1.2.2 nathanw bus_space_tag_t sc_bt;
74 1.1.2.2 nathanw bus_space_handle_t sc_bh;
75 1.1.2.2 nathanw struct sabtty_softc * sc_child[SAB_NCHAN];
76 1.1.2.2 nathanw u_int sc_nchild;
77 1.1.2.2 nathanw void * sc_softintr;
78 1.1.2.2 nathanw int sc_node;
79 1.1.2.2 nathanw };
80 1.1.2.2 nathanw
81 1.1.2.2 nathanw struct sabtty_attach_args {
82 1.1.2.2 nathanw u_int sbt_portno;
83 1.1.2.2 nathanw };
84 1.1.2.2 nathanw
85 1.1.2.2 nathanw struct sabtty_softc {
86 1.1.2.2 nathanw struct device sc_dv;
87 1.1.2.2 nathanw struct sab_softc * sc_parent;
88 1.1.2.2 nathanw bus_space_tag_t sc_bt;
89 1.1.2.2 nathanw bus_space_handle_t sc_bh;
90 1.1.2.2 nathanw struct tty * sc_tty;
91 1.1.2.2 nathanw u_int sc_portno;
92 1.1.2.2 nathanw u_int8_t sc_pvr_dtr, sc_pvr_dsr;
93 1.1.2.2 nathanw u_int8_t sc_imr0, sc_imr1;
94 1.1.2.2 nathanw int sc_openflags;
95 1.1.2.2 nathanw u_char * sc_txp;
96 1.1.2.2 nathanw int sc_txc;
97 1.1.2.2 nathanw int sc_flags;
98 1.1.2.2 nathanw #define SABTTYF_STOP 0x01
99 1.1.2.2 nathanw #define SABTTYF_DONE 0x02
100 1.1.2.2 nathanw #define SABTTYF_RINGOVERFLOW 0x04
101 1.1.2.2 nathanw #define SABTTYF_CDCHG 0x08
102 1.1.2.2 nathanw #define SABTTYF_CONS_IN 0x10
103 1.1.2.2 nathanw #define SABTTYF_CONS_OUT 0x20
104 1.1.2.2 nathanw #define SABTTYF_TXDRAIN 0x40
105 1.1.2.2 nathanw #define SABTTYF_DONTDDB 0x80
106 1.1.2.2 nathanw u_int8_t sc_rbuf[SABTTY_RBUF_SIZE];
107 1.1.2.2 nathanw u_int8_t *sc_rend, *sc_rput, *sc_rget;
108 1.1.2.2 nathanw u_int8_t sc_polling, sc_pollrfc;
109 1.1.2.2 nathanw };
110 1.1.2.2 nathanw
111 1.1.2.2 nathanw struct sabtty_softc *sabtty_cons_input;
112 1.1.2.2 nathanw struct sabtty_softc *sabtty_cons_output;
113 1.1.2.2 nathanw
114 1.1.2.2 nathanw #define SAB_READ(sc,r) \
115 1.1.2.2 nathanw bus_space_read_1((sc)->sc_bt, (sc)->sc_bh, (r))
116 1.1.2.2 nathanw #define SAB_WRITE(sc,r,v) \
117 1.1.2.2 nathanw bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, (r), (v))
118 1.1.2.2 nathanw
119 1.1.2.2 nathanw int sab_match(struct device *, struct cfdata *, void *);
120 1.1.2.2 nathanw void sab_attach(struct device *, struct device *, void *);
121 1.1.2.2 nathanw int sab_print(void *, const char *);
122 1.1.2.2 nathanw int sab_intr(void *);
123 1.1.2.2 nathanw
124 1.1.2.2 nathanw void sab_softintr(void *);
125 1.1.2.2 nathanw void sab_cnputc(dev_t, int);
126 1.1.2.2 nathanw int sab_cngetc(dev_t);
127 1.1.2.2 nathanw void sab_cnpollc(dev_t, int);
128 1.1.2.2 nathanw
129 1.1.2.2 nathanw int sabtty_match(struct device *, struct cfdata *, void *);
130 1.1.2.2 nathanw void sabtty_attach(struct device *, struct device *, void *);
131 1.1.2.2 nathanw void sabtty_start(struct tty *);
132 1.1.2.2 nathanw int sabtty_param(struct tty *, struct termios *);
133 1.1.2.2 nathanw int sabtty_intr(struct sabtty_softc *, int *);
134 1.1.2.2 nathanw void sabtty_softintr(struct sabtty_softc *);
135 1.1.2.2 nathanw int sabtty_mdmctrl(struct sabtty_softc *, int, int);
136 1.1.2.2 nathanw void sabtty_cec_wait(struct sabtty_softc *);
137 1.1.2.2 nathanw void sabtty_tec_wait(struct sabtty_softc *);
138 1.1.2.2 nathanw void sabtty_reset(struct sabtty_softc *);
139 1.1.2.2 nathanw void sabtty_flush(struct sabtty_softc *);
140 1.1.2.2 nathanw int sabtty_speed(int);
141 1.1.2.2 nathanw void sabtty_console_flags(struct sabtty_softc *);
142 1.1.2.2 nathanw void sabtty_cnpollc(struct sabtty_softc *, int);
143 1.1.2.2 nathanw void sabtty_shutdown(void *);
144 1.1.2.2 nathanw int sabttyparam(struct sabtty_softc *, struct tty *, struct termios *);
145 1.1.2.2 nathanw
146 1.1.2.2 nathanw void sabtty_cnputc(struct sabtty_softc *, int);
147 1.1.2.2 nathanw int sabtty_cngetc(struct sabtty_softc *);
148 1.1.2.2 nathanw void sabtty_abort(struct sabtty_softc *);
149 1.1.2.2 nathanw
150 1.1.2.2 nathanw struct cfattach sab_ca = {
151 1.1.2.2 nathanw sizeof(struct sab_softc), sab_match, sab_attach
152 1.1.2.2 nathanw };
153 1.1.2.2 nathanw
154 1.1.2.2 nathanw extern struct cfdriver sab_cd;
155 1.1.2.2 nathanw
156 1.1.2.2 nathanw
157 1.1.2.2 nathanw struct cfattach sabtty_ca = {
158 1.1.2.2 nathanw sizeof(struct sabtty_softc), sabtty_match, sabtty_attach
159 1.1.2.2 nathanw };
160 1.1.2.2 nathanw
161 1.1.2.2 nathanw extern struct cfdriver sabtty_cd;
162 1.1.2.2 nathanw
163 1.1.2.3 nathanw dev_type_open(sabopen);
164 1.1.2.3 nathanw dev_type_close(sabclose);
165 1.1.2.3 nathanw dev_type_read(sabread);
166 1.1.2.3 nathanw dev_type_write(sabwrite);
167 1.1.2.3 nathanw dev_type_ioctl(sabioctl);
168 1.1.2.3 nathanw dev_type_stop(sabstop);
169 1.1.2.3 nathanw dev_type_tty(sabtty);
170 1.1.2.3 nathanw dev_type_poll(sabpoll);
171 1.1.2.3 nathanw
172 1.1.2.3 nathanw const struct cdevsw sabtty_cdevsw = {
173 1.1.2.3 nathanw sabopen, sabclose, sabread, sabwrite, sabioctl,
174 1.1.2.3 nathanw sabstop, sabtty, sabpoll, nommap, D_TTY
175 1.1.2.3 nathanw };
176 1.1.2.3 nathanw
177 1.1.2.2 nathanw struct sabtty_rate {
178 1.1.2.2 nathanw int baud;
179 1.1.2.2 nathanw int n, m;
180 1.1.2.2 nathanw };
181 1.1.2.2 nathanw
182 1.1.2.2 nathanw struct sabtty_rate sabtty_baudtable[] = {
183 1.1.2.2 nathanw { 50, 35, 10 },
184 1.1.2.2 nathanw { 75, 47, 9 },
185 1.1.2.2 nathanw { 110, 32, 9 },
186 1.1.2.2 nathanw { 134, 53, 8 },
187 1.1.2.2 nathanw { 150, 47, 8 },
188 1.1.2.2 nathanw { 200, 35, 8 },
189 1.1.2.2 nathanw { 300, 47, 7 },
190 1.1.2.2 nathanw { 600, 47, 6 },
191 1.1.2.2 nathanw { 1200, 47, 5 },
192 1.1.2.2 nathanw { 1800, 31, 5 },
193 1.1.2.2 nathanw { 2400, 47, 4 },
194 1.1.2.2 nathanw { 4800, 47, 3 },
195 1.1.2.2 nathanw { 9600, 47, 2 },
196 1.1.2.2 nathanw { 19200, 47, 1 },
197 1.1.2.2 nathanw { 38400, 23, 1 },
198 1.1.2.2 nathanw { 57600, 15, 1 },
199 1.1.2.2 nathanw { 115200, 7, 1 },
200 1.1.2.2 nathanw { 230400, 3, 1 },
201 1.1.2.2 nathanw { 460800, 1, 1 },
202 1.1.2.2 nathanw { 76800, 11, 1 },
203 1.1.2.2 nathanw { 153600, 5, 1 },
204 1.1.2.2 nathanw { 307200, 3, 1 },
205 1.1.2.2 nathanw { 614400, 3, 0 },
206 1.1.2.2 nathanw { 921600, 0, 1 },
207 1.1.2.2 nathanw };
208 1.1.2.2 nathanw
209 1.1.2.2 nathanw int
210 1.1.2.2 nathanw sab_match(parent, match, aux)
211 1.1.2.2 nathanw struct device *parent;
212 1.1.2.2 nathanw struct cfdata *match;
213 1.1.2.2 nathanw void *aux;
214 1.1.2.2 nathanw {
215 1.1.2.2 nathanw struct ebus_attach_args *ea = aux;
216 1.1.2.2 nathanw char *compat;
217 1.1.2.2 nathanw
218 1.1.2.2 nathanw if (strcmp(ea->ea_name, "se") == 0)
219 1.1.2.2 nathanw return (1);
220 1.1.2.2 nathanw
221 1.1.2.2 nathanw compat = PROM_getpropstring(ea->ea_node, "compatible");
222 1.1.2.2 nathanw if (compat != NULL && !strcmp(compat, "sab82532"))
223 1.1.2.2 nathanw return (1);
224 1.1.2.2 nathanw
225 1.1.2.2 nathanw return (0);
226 1.1.2.2 nathanw }
227 1.1.2.2 nathanw
228 1.1.2.2 nathanw void
229 1.1.2.2 nathanw sab_attach(parent, self, aux)
230 1.1.2.2 nathanw struct device *parent;
231 1.1.2.2 nathanw struct device *self;
232 1.1.2.2 nathanw void *aux;
233 1.1.2.2 nathanw {
234 1.1.2.2 nathanw struct sab_softc *sc = (struct sab_softc *)self;
235 1.1.2.2 nathanw struct ebus_attach_args *ea = aux;
236 1.1.2.2 nathanw u_int8_t r;
237 1.1.2.2 nathanw u_int i;
238 1.1.2.2 nathanw
239 1.1.2.2 nathanw sc->sc_bt = ea->ea_bustag;
240 1.1.2.2 nathanw sc->sc_node = ea->ea_node;
241 1.1.2.2 nathanw
242 1.1.2.2 nathanw /* Use prom mapping, if available. */
243 1.1.2.2 nathanw if (ea->ea_nvaddr)
244 1.1.2.2 nathanw sparc_promaddr_to_handle(sc->sc_bt, ea->ea_vaddr[0], &sc->sc_bh);
245 1.1.2.2 nathanw else if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
246 1.1.2.2 nathanw ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
247 1.1.2.2 nathanw printf(": can't map register space\n");
248 1.1.2.2 nathanw return;
249 1.1.2.2 nathanw }
250 1.1.2.2 nathanw
251 1.1.2.2 nathanw sc->sc_ih = bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
252 1.1.2.2 nathanw IPL_TTY, 0, sab_intr, sc);
253 1.1.2.2 nathanw if (sc->sc_ih == NULL) {
254 1.1.2.2 nathanw printf(": can't map interrupt\n");
255 1.1.2.2 nathanw return;
256 1.1.2.2 nathanw }
257 1.1.2.2 nathanw
258 1.1.2.2 nathanw sc->sc_softintr = softintr_establish(IPL_TTY, sab_softintr, sc);
259 1.1.2.2 nathanw if (sc->sc_softintr == NULL) {
260 1.1.2.2 nathanw printf(": can't get soft intr\n");
261 1.1.2.2 nathanw return;
262 1.1.2.2 nathanw }
263 1.1.2.2 nathanw
264 1.1.2.2 nathanw printf(": rev ");
265 1.1.2.2 nathanw r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_VMASK;
266 1.1.2.2 nathanw switch (r) {
267 1.1.2.2 nathanw case SAB_VSTR_V_1:
268 1.1.2.2 nathanw printf("1");
269 1.1.2.2 nathanw break;
270 1.1.2.2 nathanw case SAB_VSTR_V_2:
271 1.1.2.2 nathanw printf("2");
272 1.1.2.2 nathanw break;
273 1.1.2.2 nathanw case SAB_VSTR_V_32:
274 1.1.2.2 nathanw printf("3.2");
275 1.1.2.2 nathanw break;
276 1.1.2.2 nathanw default:
277 1.1.2.2 nathanw printf("unknown(0x%x)", r);
278 1.1.2.2 nathanw break;
279 1.1.2.2 nathanw }
280 1.1.2.2 nathanw printf("\n");
281 1.1.2.2 nathanw
282 1.1.2.2 nathanw /* Set all pins, except DTR pins to be inputs */
283 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PCR, ~(SAB_PVR_DTR_A | SAB_PVR_DTR_B));
284 1.1.2.2 nathanw /* Disable port interrupts */
285 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PIM, 0xff);
286 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PVR, SAB_PVR_DTR_A | SAB_PVR_DTR_B | SAB_PVR_MAGIC);
287 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IPC, SAB_IPC_ICPL);
288 1.1.2.2 nathanw
289 1.1.2.2 nathanw for (i = 0; i < SAB_NCHAN; i++) {
290 1.1.2.2 nathanw struct sabtty_attach_args sta;
291 1.1.2.2 nathanw
292 1.1.2.2 nathanw sta.sbt_portno = i;
293 1.1.2.2 nathanw sc->sc_child[i] = (struct sabtty_softc *)config_found_sm(self,
294 1.1.2.2 nathanw &sta, sab_print, sabtty_match);
295 1.1.2.2 nathanw if (sc->sc_child[i] != NULL)
296 1.1.2.2 nathanw sc->sc_nchild++;
297 1.1.2.2 nathanw }
298 1.1.2.2 nathanw }
299 1.1.2.2 nathanw
300 1.1.2.2 nathanw int
301 1.1.2.2 nathanw sab_print(args, name)
302 1.1.2.2 nathanw void *args;
303 1.1.2.2 nathanw const char *name;
304 1.1.2.2 nathanw {
305 1.1.2.2 nathanw struct sabtty_attach_args *sa = args;
306 1.1.2.2 nathanw
307 1.1.2.2 nathanw if (name)
308 1.1.2.2 nathanw printf("sabtty at %s", name);
309 1.1.2.2 nathanw printf(" port %d", sa->sbt_portno);
310 1.1.2.2 nathanw return (UNCONF);
311 1.1.2.2 nathanw }
312 1.1.2.2 nathanw
313 1.1.2.2 nathanw int
314 1.1.2.2 nathanw sab_intr(vsc)
315 1.1.2.2 nathanw void *vsc;
316 1.1.2.2 nathanw {
317 1.1.2.2 nathanw struct sab_softc *sc = vsc;
318 1.1.2.2 nathanw int r = 0, needsoft = 0;
319 1.1.2.2 nathanw u_int8_t gis;
320 1.1.2.2 nathanw
321 1.1.2.2 nathanw gis = SAB_READ(sc, SAB_GIS);
322 1.1.2.2 nathanw
323 1.1.2.2 nathanw /* channel A */
324 1.1.2.2 nathanw if ((gis & (SAB_GIS_ISA1 | SAB_GIS_ISA0)) && sc->sc_child[0] &&
325 1.1.2.2 nathanw sc->sc_child[0]->sc_tty)
326 1.1.2.2 nathanw r |= sabtty_intr(sc->sc_child[0], &needsoft);
327 1.1.2.2 nathanw
328 1.1.2.2 nathanw /* channel B */
329 1.1.2.2 nathanw if ((gis & (SAB_GIS_ISB1 | SAB_GIS_ISB0)) && sc->sc_child[1] &&
330 1.1.2.2 nathanw sc->sc_child[1]->sc_tty)
331 1.1.2.2 nathanw r |= sabtty_intr(sc->sc_child[1], &needsoft);
332 1.1.2.2 nathanw
333 1.1.2.2 nathanw if (needsoft)
334 1.1.2.2 nathanw softintr_schedule(sc->sc_softintr);
335 1.1.2.2 nathanw
336 1.1.2.2 nathanw return (r);
337 1.1.2.2 nathanw }
338 1.1.2.2 nathanw
339 1.1.2.2 nathanw void
340 1.1.2.2 nathanw sab_softintr(vsc)
341 1.1.2.2 nathanw void *vsc;
342 1.1.2.2 nathanw {
343 1.1.2.2 nathanw struct sab_softc *sc = vsc;
344 1.1.2.2 nathanw
345 1.1.2.2 nathanw if (sc->sc_child[0] && sc->sc_child[0]->sc_tty)
346 1.1.2.2 nathanw sabtty_softintr(sc->sc_child[0]);
347 1.1.2.2 nathanw if (sc->sc_child[1] && sc->sc_child[1]->sc_tty)
348 1.1.2.2 nathanw sabtty_softintr(sc->sc_child[1]);
349 1.1.2.2 nathanw }
350 1.1.2.2 nathanw
351 1.1.2.2 nathanw int
352 1.1.2.2 nathanw sabtty_match(parent, match, aux)
353 1.1.2.2 nathanw struct device *parent;
354 1.1.2.2 nathanw struct cfdata *match;
355 1.1.2.2 nathanw void *aux;
356 1.1.2.2 nathanw {
357 1.1.2.2 nathanw struct sabtty_attach_args *sa = aux;
358 1.1.2.2 nathanw
359 1.1.2.2 nathanw if (sa->sbt_portno < SAB_NCHAN)
360 1.1.2.2 nathanw return (1);
361 1.1.2.2 nathanw return (0);
362 1.1.2.2 nathanw }
363 1.1.2.2 nathanw
364 1.1.2.2 nathanw void
365 1.1.2.2 nathanw sabtty_attach(parent, self, aux)
366 1.1.2.2 nathanw struct device *parent;
367 1.1.2.2 nathanw struct device *self;
368 1.1.2.2 nathanw void *aux;
369 1.1.2.2 nathanw {
370 1.1.2.2 nathanw struct sabtty_softc *sc = (struct sabtty_softc *)self;
371 1.1.2.2 nathanw struct sabtty_attach_args *sa = aux;
372 1.1.2.2 nathanw int r;
373 1.1.2.2 nathanw
374 1.1.2.2 nathanw sc->sc_tty = ttymalloc();
375 1.1.2.2 nathanw if (sc->sc_tty == NULL) {
376 1.1.2.2 nathanw printf(": failed to allocate tty\n");
377 1.1.2.2 nathanw return;
378 1.1.2.2 nathanw }
379 1.1.2.2 nathanw tty_attach(sc->sc_tty);
380 1.1.2.2 nathanw sc->sc_tty->t_oproc = sabtty_start;
381 1.1.2.2 nathanw sc->sc_tty->t_param = sabtty_param;
382 1.1.2.2 nathanw
383 1.1.2.2 nathanw sc->sc_parent = (struct sab_softc *)parent;
384 1.1.2.2 nathanw sc->sc_bt = sc->sc_parent->sc_bt;
385 1.1.2.2 nathanw sc->sc_portno = sa->sbt_portno;
386 1.1.2.2 nathanw sc->sc_rend = sc->sc_rbuf + SABTTY_RBUF_SIZE;
387 1.1.2.2 nathanw
388 1.1.2.2 nathanw switch (sa->sbt_portno) {
389 1.1.2.2 nathanw case 0: /* port A */
390 1.1.2.2 nathanw sc->sc_pvr_dtr = SAB_PVR_DTR_A;
391 1.1.2.2 nathanw sc->sc_pvr_dsr = SAB_PVR_DSR_A;
392 1.1.2.2 nathanw r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
393 1.1.2.2 nathanw SAB_CHAN_A, SAB_CHANLEN, &sc->sc_bh);
394 1.1.2.2 nathanw break;
395 1.1.2.2 nathanw case 1: /* port B */
396 1.1.2.2 nathanw sc->sc_pvr_dtr = SAB_PVR_DTR_B;
397 1.1.2.2 nathanw sc->sc_pvr_dsr = SAB_PVR_DSR_B;
398 1.1.2.2 nathanw r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
399 1.1.2.2 nathanw SAB_CHAN_B, SAB_CHANLEN, &sc->sc_bh);
400 1.1.2.2 nathanw break;
401 1.1.2.2 nathanw default:
402 1.1.2.2 nathanw printf(": invalid channel: %u\n", sa->sbt_portno);
403 1.1.2.2 nathanw return;
404 1.1.2.2 nathanw }
405 1.1.2.2 nathanw if (r != 0) {
406 1.1.2.2 nathanw printf(": failed to allocate register subregion\n");
407 1.1.2.2 nathanw return;
408 1.1.2.2 nathanw }
409 1.1.2.2 nathanw
410 1.1.2.2 nathanw sabtty_console_flags(sc);
411 1.1.2.2 nathanw
412 1.1.2.2 nathanw if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
413 1.1.2.2 nathanw struct termios t;
414 1.1.2.2 nathanw char *acc;
415 1.1.2.2 nathanw
416 1.1.2.2 nathanw switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
417 1.1.2.2 nathanw case SABTTYF_CONS_IN:
418 1.1.2.2 nathanw acc = "input";
419 1.1.2.2 nathanw break;
420 1.1.2.2 nathanw case SABTTYF_CONS_OUT:
421 1.1.2.2 nathanw acc = "output";
422 1.1.2.2 nathanw break;
423 1.1.2.2 nathanw case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
424 1.1.2.2 nathanw default:
425 1.1.2.2 nathanw acc = "i/o";
426 1.1.2.2 nathanw break;
427 1.1.2.2 nathanw }
428 1.1.2.2 nathanw
429 1.1.2.2 nathanw /* Let current output drain */
430 1.1.2.2 nathanw DELAY(100000);
431 1.1.2.2 nathanw
432 1.1.2.2 nathanw t.c_ispeed= 0;
433 1.1.2.2 nathanw t.c_ospeed = 9600;
434 1.1.2.2 nathanw t.c_cflag = CREAD | CS8 | HUPCL;
435 1.1.2.2 nathanw sc->sc_tty->t_ospeed = 0;
436 1.1.2.2 nathanw sabttyparam(sc, sc->sc_tty, &t);
437 1.1.2.2 nathanw
438 1.1.2.2 nathanw if (sc->sc_flags & SABTTYF_CONS_IN) {
439 1.1.2.2 nathanw sabtty_cons_input = sc;
440 1.1.2.2 nathanw cn_tab->cn_pollc = sab_cnpollc;
441 1.1.2.2 nathanw cn_tab->cn_getc = sab_cngetc;
442 1.1.2.2 nathanw cn_tab->cn_dev = makedev(77/*XXX*/, self->dv_unit);
443 1.1.2.2 nathanw shutdownhook_establish(sabtty_shutdown, sc);
444 1.1.2.2 nathanw }
445 1.1.2.2 nathanw
446 1.1.2.2 nathanw if (sc->sc_flags & SABTTYF_CONS_OUT) {
447 1.1.2.2 nathanw sabtty_cons_output = sc;
448 1.1.2.2 nathanw cn_tab->cn_putc = sab_cnputc;
449 1.1.2.2 nathanw cn_tab->cn_dev = makedev(77/*XXX*/, self->dv_unit);
450 1.1.2.2 nathanw }
451 1.1.2.2 nathanw printf(": console %s", acc);
452 1.1.2.2 nathanw } else {
453 1.1.2.2 nathanw /* Not a console... */
454 1.1.2.2 nathanw sabtty_reset(sc);
455 1.1.2.2 nathanw }
456 1.1.2.2 nathanw
457 1.1.2.2 nathanw printf("\n");
458 1.1.2.2 nathanw }
459 1.1.2.2 nathanw
460 1.1.2.2 nathanw int
461 1.1.2.2 nathanw sabtty_intr(sc, needsoftp)
462 1.1.2.2 nathanw struct sabtty_softc *sc;
463 1.1.2.2 nathanw int *needsoftp;
464 1.1.2.2 nathanw {
465 1.1.2.2 nathanw u_int8_t isr0, isr1;
466 1.1.2.2 nathanw int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
467 1.1.2.2 nathanw
468 1.1.2.2 nathanw isr0 = SAB_READ(sc, SAB_ISR0);
469 1.1.2.2 nathanw isr1 = SAB_READ(sc, SAB_ISR1);
470 1.1.2.2 nathanw
471 1.1.2.2 nathanw if (isr0 || isr1)
472 1.1.2.2 nathanw r = 1;
473 1.1.2.2 nathanw
474 1.1.2.2 nathanw if (isr0 & SAB_ISR0_RPF) {
475 1.1.2.2 nathanw len = 32;
476 1.1.2.2 nathanw clearfifo = 1;
477 1.1.2.2 nathanw }
478 1.1.2.2 nathanw if (isr0 & SAB_ISR0_TCD) {
479 1.1.2.2 nathanw len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
480 1.1.2.2 nathanw clearfifo = 1;
481 1.1.2.2 nathanw }
482 1.1.2.2 nathanw if (isr0 & SAB_ISR0_TIME) {
483 1.1.2.2 nathanw sabtty_cec_wait(sc);
484 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
485 1.1.2.2 nathanw }
486 1.1.2.2 nathanw if (isr0 & SAB_ISR0_RFO) {
487 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_RINGOVERFLOW;
488 1.1.2.2 nathanw clearfifo = 1;
489 1.1.2.2 nathanw }
490 1.1.2.2 nathanw if (len != 0) {
491 1.1.2.2 nathanw u_int8_t *ptr;
492 1.1.2.2 nathanw
493 1.1.2.2 nathanw ptr = sc->sc_rput;
494 1.1.2.2 nathanw for (i = 0; i < len; i++) {
495 1.1.2.2 nathanw *ptr++ = SAB_READ(sc, SAB_RFIFO);
496 1.1.2.2 nathanw if (ptr == sc->sc_rend)
497 1.1.2.2 nathanw ptr = sc->sc_rbuf;
498 1.1.2.2 nathanw if (ptr == sc->sc_rget) {
499 1.1.2.2 nathanw if (ptr == sc->sc_rbuf)
500 1.1.2.2 nathanw ptr = sc->sc_rend;
501 1.1.2.2 nathanw ptr--;
502 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_RINGOVERFLOW;
503 1.1.2.2 nathanw }
504 1.1.2.2 nathanw }
505 1.1.2.2 nathanw sc->sc_rput = ptr;
506 1.1.2.2 nathanw needsoft = 1;
507 1.1.2.2 nathanw }
508 1.1.2.2 nathanw
509 1.1.2.2 nathanw if (clearfifo) {
510 1.1.2.2 nathanw sabtty_cec_wait(sc);
511 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
512 1.1.2.2 nathanw }
513 1.1.2.2 nathanw
514 1.1.2.2 nathanw if (isr0 & SAB_ISR0_CDSC) {
515 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_CDCHG;
516 1.1.2.2 nathanw needsoft = 1;
517 1.1.2.2 nathanw }
518 1.1.2.2 nathanw
519 1.1.2.2 nathanw if (isr1 & SAB_ISR1_BRKT)
520 1.1.2.2 nathanw sabtty_abort(sc);
521 1.1.2.2 nathanw
522 1.1.2.2 nathanw if (isr1 & SAB_ISR1_ALLS) {
523 1.1.2.2 nathanw if (sc->sc_flags & SABTTYF_TXDRAIN)
524 1.1.2.2 nathanw wakeup(sc);
525 1.1.2.2 nathanw sc->sc_flags &= ~SABTTYF_STOP;
526 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_DONE;
527 1.1.2.2 nathanw sc->sc_imr1 |= SAB_IMR1_ALLS;
528 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
529 1.1.2.2 nathanw needsoft = 1;
530 1.1.2.2 nathanw }
531 1.1.2.2 nathanw
532 1.1.2.2 nathanw if (isr1 & SAB_ISR1_XPR) {
533 1.1.2.2 nathanw r = 1;
534 1.1.2.2 nathanw if ((sc->sc_flags & SABTTYF_STOP) == 0) {
535 1.1.2.2 nathanw if (sc->sc_txc < 32)
536 1.1.2.2 nathanw len = sc->sc_txc;
537 1.1.2.2 nathanw else
538 1.1.2.2 nathanw len = 32;
539 1.1.2.2 nathanw for (i = 0; i < len; i++) {
540 1.1.2.2 nathanw SAB_WRITE(sc, SAB_XFIFO + i, *sc->sc_txp);
541 1.1.2.2 nathanw sc->sc_txp++;
542 1.1.2.2 nathanw sc->sc_txc--;
543 1.1.2.2 nathanw }
544 1.1.2.2 nathanw if (i != 0) {
545 1.1.2.2 nathanw sabtty_cec_wait(sc);
546 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
547 1.1.2.2 nathanw }
548 1.1.2.2 nathanw }
549 1.1.2.2 nathanw
550 1.1.2.2 nathanw if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
551 1.1.2.2 nathanw sc->sc_imr1 |= SAB_IMR1_XPR;
552 1.1.2.2 nathanw sc->sc_imr1 &= ~SAB_IMR1_ALLS;
553 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
554 1.1.2.2 nathanw }
555 1.1.2.2 nathanw }
556 1.1.2.2 nathanw
557 1.1.2.2 nathanw if (needsoft)
558 1.1.2.2 nathanw *needsoftp = needsoft;
559 1.1.2.2 nathanw return (r);
560 1.1.2.2 nathanw }
561 1.1.2.2 nathanw
562 1.1.2.2 nathanw void
563 1.1.2.2 nathanw sabtty_softintr(sc)
564 1.1.2.2 nathanw struct sabtty_softc *sc;
565 1.1.2.2 nathanw {
566 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
567 1.1.2.2 nathanw int s, flags;
568 1.1.2.2 nathanw u_int8_t r;
569 1.1.2.2 nathanw
570 1.1.2.2 nathanw if (tp == NULL)
571 1.1.2.2 nathanw return;
572 1.1.2.2 nathanw
573 1.1.2.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0)
574 1.1.2.2 nathanw return;
575 1.1.2.2 nathanw
576 1.1.2.2 nathanw while (sc->sc_rget != sc->sc_rput) {
577 1.1.2.2 nathanw int data;
578 1.1.2.2 nathanw u_int8_t stat;
579 1.1.2.2 nathanw
580 1.1.2.2 nathanw data = sc->sc_rget[0];
581 1.1.2.2 nathanw stat = sc->sc_rget[1];
582 1.1.2.2 nathanw sc->sc_rget += 2;
583 1.1.2.2 nathanw if (stat & SAB_RSTAT_PE)
584 1.1.2.2 nathanw data |= TTY_PE;
585 1.1.2.2 nathanw if (stat & SAB_RSTAT_FE)
586 1.1.2.2 nathanw data |= TTY_FE;
587 1.1.2.2 nathanw if (sc->sc_rget == sc->sc_rend)
588 1.1.2.2 nathanw sc->sc_rget = sc->sc_rbuf;
589 1.1.2.2 nathanw
590 1.1.2.2 nathanw (*tp->t_linesw->l_rint)(data, tp);
591 1.1.2.2 nathanw }
592 1.1.2.2 nathanw
593 1.1.2.2 nathanw s = splhigh();
594 1.1.2.2 nathanw flags = sc->sc_flags;
595 1.1.2.2 nathanw sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
596 1.1.2.2 nathanw splx(s);
597 1.1.2.2 nathanw
598 1.1.2.2 nathanw if (flags & SABTTYF_CDCHG) {
599 1.1.2.2 nathanw s = spltty();
600 1.1.2.2 nathanw r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
601 1.1.2.2 nathanw splx(s);
602 1.1.2.2 nathanw
603 1.1.2.2 nathanw (*tp->t_linesw->l_modem)(tp, r);
604 1.1.2.2 nathanw }
605 1.1.2.2 nathanw
606 1.1.2.2 nathanw if (flags & SABTTYF_RINGOVERFLOW)
607 1.1.2.2 nathanw log(LOG_WARNING, "%s: ring overflow\n", sc->sc_dv.dv_xname);
608 1.1.2.2 nathanw
609 1.1.2.2 nathanw if (flags & SABTTYF_DONE) {
610 1.1.2.2 nathanw ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
611 1.1.2.2 nathanw tp->t_state &= ~TS_BUSY;
612 1.1.2.2 nathanw (*tp->t_linesw->l_start)(tp);
613 1.1.2.2 nathanw }
614 1.1.2.2 nathanw }
615 1.1.2.2 nathanw
616 1.1.2.2 nathanw int
617 1.1.2.2 nathanw sabopen(dev, flags, mode, p)
618 1.1.2.2 nathanw dev_t dev;
619 1.1.2.2 nathanw int flags, mode;
620 1.1.2.2 nathanw struct proc *p;
621 1.1.2.2 nathanw {
622 1.1.2.2 nathanw struct sabtty_softc *sc;
623 1.1.2.2 nathanw struct tty *tp;
624 1.1.2.2 nathanw int s, s1;
625 1.1.2.2 nathanw
626 1.1.2.2 nathanw sc = device_lookup(&sabtty_cd, SABUNIT(dev));
627 1.1.2.2 nathanw if (sc == NULL)
628 1.1.2.2 nathanw return (ENXIO);
629 1.1.2.2 nathanw
630 1.1.2.2 nathanw tp = sc->sc_tty;
631 1.1.2.2 nathanw tp->t_dev = dev;
632 1.1.2.2 nathanw
633 1.1.2.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0) {
634 1.1.2.2 nathanw /* XXX tp->t_state |= TS_WOPEN; */
635 1.1.2.2 nathanw
636 1.1.2.2 nathanw ttychars(tp);
637 1.1.2.2 nathanw tp->t_iflag = TTYDEF_IFLAG;
638 1.1.2.2 nathanw tp->t_oflag = TTYDEF_OFLAG;
639 1.1.2.2 nathanw tp->t_cflag = TTYDEF_CFLAG;
640 1.1.2.2 nathanw if (sc->sc_openflags & TIOCFLAG_CLOCAL)
641 1.1.2.2 nathanw tp->t_cflag |= CLOCAL;
642 1.1.2.2 nathanw if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
643 1.1.2.2 nathanw tp->t_cflag |= CRTSCTS;
644 1.1.2.2 nathanw if (sc->sc_openflags & TIOCFLAG_MDMBUF)
645 1.1.2.2 nathanw tp->t_cflag |= MDMBUF;
646 1.1.2.2 nathanw tp->t_lflag = TTYDEF_LFLAG;
647 1.1.2.2 nathanw tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
648 1.1.2.2 nathanw
649 1.1.2.2 nathanw sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
650 1.1.2.2 nathanw
651 1.1.2.2 nathanw s = spltty();
652 1.1.2.2 nathanw
653 1.1.2.2 nathanw ttsetwater(tp);
654 1.1.2.2 nathanw
655 1.1.2.2 nathanw s1 = splhigh();
656 1.1.2.2 nathanw sabtty_reset(sc);
657 1.1.2.2 nathanw sabtty_param(tp, &tp->t_termios);
658 1.1.2.2 nathanw sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
659 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
660 1.1.2.2 nathanw sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
661 1.1.2.2 nathanw SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
662 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
663 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
664 1.1.2.2 nathanw sabtty_cec_wait(sc);
665 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
666 1.1.2.2 nathanw sabtty_cec_wait(sc);
667 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
668 1.1.2.2 nathanw sabtty_cec_wait(sc);
669 1.1.2.2 nathanw splx(s1);
670 1.1.2.2 nathanw
671 1.1.2.2 nathanw sabtty_flush(sc);
672 1.1.2.2 nathanw
673 1.1.2.2 nathanw if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
674 1.1.2.2 nathanw (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
675 1.1.2.2 nathanw tp->t_state |= TS_CARR_ON;
676 1.1.2.2 nathanw else
677 1.1.2.2 nathanw tp->t_state &= ~TS_CARR_ON;
678 1.1.2.2 nathanw } else if ((tp->t_state & TS_XCLUDE) &&
679 1.1.2.2 nathanw (!suser(p->p_ucred, &p->p_acflag))) {
680 1.1.2.2 nathanw return (EBUSY);
681 1.1.2.2 nathanw } else {
682 1.1.2.2 nathanw s = spltty();
683 1.1.2.2 nathanw }
684 1.1.2.2 nathanw
685 1.1.2.2 nathanw if ((flags & O_NONBLOCK) == 0) {
686 1.1.2.2 nathanw while ((tp->t_cflag & CLOCAL) == 0 &&
687 1.1.2.2 nathanw (tp->t_state & TS_CARR_ON) == 0) {
688 1.1.2.2 nathanw int error;
689 1.1.2.2 nathanw
690 1.1.2.2 nathanw /* XXX tp->t_state |= TS_WOPEN; */
691 1.1.2.2 nathanw error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
692 1.1.2.2 nathanw "sabttycd", 0);
693 1.1.2.2 nathanw if (error != 0) {
694 1.1.2.2 nathanw splx(s);
695 1.1.2.2 nathanw /* XXX tp->t_state &= ~TS_WOPEN; */
696 1.1.2.2 nathanw return (error);
697 1.1.2.2 nathanw }
698 1.1.2.2 nathanw }
699 1.1.2.2 nathanw }
700 1.1.2.2 nathanw
701 1.1.2.2 nathanw splx(s);
702 1.1.2.2 nathanw
703 1.1.2.2 nathanw s = (*tp->t_linesw->l_open)(dev, tp);
704 1.1.2.2 nathanw if (s != 0) {
705 1.1.2.2 nathanw if (tp->t_state & TS_ISOPEN)
706 1.1.2.2 nathanw return (s);
707 1.1.2.2 nathanw
708 1.1.2.2 nathanw if (tp->t_cflag & HUPCL) {
709 1.1.2.2 nathanw sabtty_mdmctrl(sc, 0, DMSET);
710 1.1.2.2 nathanw (void)tsleep(sc, TTIPRI, ttclos, hz);
711 1.1.2.2 nathanw }
712 1.1.2.2 nathanw
713 1.1.2.2 nathanw if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
714 1.1.2.2 nathanw /* Flush and power down if we're not the console */
715 1.1.2.2 nathanw sabtty_flush(sc);
716 1.1.2.2 nathanw sabtty_reset(sc);
717 1.1.2.2 nathanw }
718 1.1.2.2 nathanw }
719 1.1.2.2 nathanw return (s);
720 1.1.2.2 nathanw }
721 1.1.2.2 nathanw
722 1.1.2.2 nathanw int
723 1.1.2.2 nathanw sabclose(dev, flags, mode, p)
724 1.1.2.2 nathanw dev_t dev;
725 1.1.2.2 nathanw int flags, mode;
726 1.1.2.2 nathanw struct proc *p;
727 1.1.2.2 nathanw {
728 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
729 1.1.2.2 nathanw struct sab_softc *bc = sc->sc_parent;
730 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
731 1.1.2.2 nathanw int s;
732 1.1.2.2 nathanw
733 1.1.2.2 nathanw (*tp->t_linesw->l_close)(tp, flags);
734 1.1.2.2 nathanw
735 1.1.2.2 nathanw s = spltty();
736 1.1.2.2 nathanw
737 1.1.2.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0) {
738 1.1.2.2 nathanw /* Wait for output drain */
739 1.1.2.2 nathanw sc->sc_imr1 &= ~SAB_IMR1_ALLS;
740 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
741 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_TXDRAIN;
742 1.1.2.2 nathanw (void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
743 1.1.2.2 nathanw sc->sc_imr1 |= SAB_IMR1_ALLS;
744 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
745 1.1.2.2 nathanw sc->sc_flags &= ~SABTTYF_TXDRAIN;
746 1.1.2.2 nathanw
747 1.1.2.2 nathanw if (tp->t_cflag & HUPCL) {
748 1.1.2.2 nathanw sabtty_mdmctrl(sc, 0, DMSET);
749 1.1.2.2 nathanw (void)tsleep(bc, TTIPRI, ttclos, hz);
750 1.1.2.2 nathanw }
751 1.1.2.2 nathanw
752 1.1.2.2 nathanw if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
753 1.1.2.2 nathanw /* Flush and power down if we're not the console */
754 1.1.2.2 nathanw sabtty_flush(sc);
755 1.1.2.2 nathanw sabtty_reset(sc);
756 1.1.2.2 nathanw }
757 1.1.2.2 nathanw }
758 1.1.2.2 nathanw
759 1.1.2.2 nathanw ttyclose(tp);
760 1.1.2.2 nathanw splx(s);
761 1.1.2.2 nathanw
762 1.1.2.2 nathanw return (0);
763 1.1.2.2 nathanw }
764 1.1.2.2 nathanw
765 1.1.2.2 nathanw int
766 1.1.2.2 nathanw sabread(dev, uio, flags)
767 1.1.2.2 nathanw dev_t dev;
768 1.1.2.2 nathanw struct uio *uio;
769 1.1.2.2 nathanw int flags;
770 1.1.2.2 nathanw {
771 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
772 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
773 1.1.2.2 nathanw
774 1.1.2.2 nathanw return ((*tp->t_linesw->l_read)(tp, uio, flags));
775 1.1.2.2 nathanw }
776 1.1.2.2 nathanw
777 1.1.2.2 nathanw int
778 1.1.2.2 nathanw sabwrite(dev, uio, flags)
779 1.1.2.2 nathanw dev_t dev;
780 1.1.2.2 nathanw struct uio *uio;
781 1.1.2.2 nathanw int flags;
782 1.1.2.2 nathanw {
783 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
784 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
785 1.1.2.2 nathanw
786 1.1.2.2 nathanw return ((*tp->t_linesw->l_write)(tp, uio, flags));
787 1.1.2.2 nathanw }
788 1.1.2.2 nathanw
789 1.1.2.2 nathanw int
790 1.1.2.2 nathanw sabioctl(dev, cmd, data, flags, p)
791 1.1.2.2 nathanw dev_t dev;
792 1.1.2.2 nathanw u_long cmd;
793 1.1.2.2 nathanw caddr_t data;
794 1.1.2.2 nathanw int flags;
795 1.1.2.2 nathanw struct proc *p;
796 1.1.2.2 nathanw {
797 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
798 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
799 1.1.2.2 nathanw int error;
800 1.1.2.2 nathanw
801 1.1.2.2 nathanw error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, p);
802 1.1.2.2 nathanw if (error >= 0)
803 1.1.2.2 nathanw return (error);
804 1.1.2.2 nathanw
805 1.1.2.2 nathanw error = ttioctl(tp, cmd, data, flags, p);
806 1.1.2.2 nathanw if (error >= 0)
807 1.1.2.2 nathanw return (error);
808 1.1.2.2 nathanw
809 1.1.2.2 nathanw error = 0;
810 1.1.2.2 nathanw
811 1.1.2.2 nathanw switch (cmd) {
812 1.1.2.2 nathanw case TIOCSBRK:
813 1.1.2.2 nathanw SAB_WRITE(sc, SAB_DAFO,
814 1.1.2.2 nathanw SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
815 1.1.2.2 nathanw break;
816 1.1.2.2 nathanw case TIOCCBRK:
817 1.1.2.2 nathanw SAB_WRITE(sc, SAB_DAFO,
818 1.1.2.2 nathanw SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
819 1.1.2.2 nathanw break;
820 1.1.2.2 nathanw case TIOCSDTR:
821 1.1.2.2 nathanw sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
822 1.1.2.2 nathanw break;
823 1.1.2.2 nathanw case TIOCCDTR:
824 1.1.2.2 nathanw sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
825 1.1.2.2 nathanw break;
826 1.1.2.2 nathanw case TIOCMBIS:
827 1.1.2.2 nathanw sabtty_mdmctrl(sc, *((int *)data), DMBIS);
828 1.1.2.2 nathanw break;
829 1.1.2.2 nathanw case TIOCMBIC:
830 1.1.2.2 nathanw sabtty_mdmctrl(sc, *((int *)data), DMBIC);
831 1.1.2.2 nathanw break;
832 1.1.2.2 nathanw case TIOCMGET:
833 1.1.2.2 nathanw *((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
834 1.1.2.2 nathanw break;
835 1.1.2.2 nathanw case TIOCMSET:
836 1.1.2.2 nathanw sabtty_mdmctrl(sc, *((int *)data), DMSET);
837 1.1.2.2 nathanw break;
838 1.1.2.2 nathanw case TIOCGFLAGS:
839 1.1.2.2 nathanw *((int *)data) = sc->sc_openflags;
840 1.1.2.2 nathanw break;
841 1.1.2.2 nathanw case TIOCSFLAGS:
842 1.1.2.2 nathanw if (suser(p->p_ucred, &p->p_acflag))
843 1.1.2.2 nathanw error = EPERM;
844 1.1.2.2 nathanw else
845 1.1.2.2 nathanw sc->sc_openflags = *((int *)data) &
846 1.1.2.2 nathanw (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
847 1.1.2.2 nathanw TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
848 1.1.2.2 nathanw break;
849 1.1.2.2 nathanw default:
850 1.1.2.2 nathanw error = ENOTTY;
851 1.1.2.2 nathanw }
852 1.1.2.2 nathanw
853 1.1.2.2 nathanw return (error);
854 1.1.2.2 nathanw }
855 1.1.2.2 nathanw
856 1.1.2.2 nathanw struct tty *
857 1.1.2.2 nathanw sabtty(dev)
858 1.1.2.2 nathanw dev_t dev;
859 1.1.2.2 nathanw {
860 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
861 1.1.2.2 nathanw
862 1.1.2.2 nathanw return (sc->sc_tty);
863 1.1.2.2 nathanw }
864 1.1.2.2 nathanw
865 1.1.2.2 nathanw void
866 1.1.2.2 nathanw sabstop(tp, flag)
867 1.1.2.2 nathanw struct tty *tp;
868 1.1.2.2 nathanw int flag;
869 1.1.2.2 nathanw {
870 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
871 1.1.2.2 nathanw int s;
872 1.1.2.2 nathanw
873 1.1.2.2 nathanw s = spltty();
874 1.1.2.2 nathanw if (tp->t_state & TS_BUSY) {
875 1.1.2.2 nathanw if ((tp->t_state & TS_TTSTOP) == 0)
876 1.1.2.2 nathanw tp->t_state |= TS_FLUSH;
877 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_STOP;
878 1.1.2.2 nathanw sc->sc_imr1 &= ~SAB_IMR1_ALLS;
879 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
880 1.1.2.2 nathanw }
881 1.1.2.2 nathanw splx(s);
882 1.1.2.2 nathanw }
883 1.1.2.2 nathanw
884 1.1.2.2 nathanw int
885 1.1.2.2 nathanw sabpoll(dev, events, p)
886 1.1.2.2 nathanw dev_t dev;
887 1.1.2.2 nathanw int events;
888 1.1.2.2 nathanw struct proc *p;
889 1.1.2.2 nathanw {
890 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
891 1.1.2.2 nathanw struct tty *tp = sc->sc_tty;
892 1.1.2.2 nathanw
893 1.1.2.2 nathanw return ((*tp->t_linesw->l_poll)(tp, events, p));
894 1.1.2.2 nathanw }
895 1.1.2.2 nathanw
896 1.1.2.2 nathanw int
897 1.1.2.2 nathanw sabtty_mdmctrl(sc, bits, how)
898 1.1.2.2 nathanw struct sabtty_softc *sc;
899 1.1.2.2 nathanw int bits, how;
900 1.1.2.2 nathanw {
901 1.1.2.2 nathanw u_int8_t r;
902 1.1.2.2 nathanw int s;
903 1.1.2.2 nathanw
904 1.1.2.2 nathanw s = spltty();
905 1.1.2.2 nathanw switch (how) {
906 1.1.2.2 nathanw case DMGET:
907 1.1.2.2 nathanw bits = 0;
908 1.1.2.2 nathanw if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
909 1.1.2.2 nathanw bits |= TIOCM_CTS;
910 1.1.2.2 nathanw if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
911 1.1.2.2 nathanw bits |= TIOCM_CD;
912 1.1.2.2 nathanw
913 1.1.2.2 nathanw r = SAB_READ(sc, SAB_PVR);
914 1.1.2.2 nathanw if ((r & sc->sc_pvr_dtr) == 0)
915 1.1.2.2 nathanw bits |= TIOCM_DTR;
916 1.1.2.2 nathanw if ((r & sc->sc_pvr_dsr) == 0)
917 1.1.2.2 nathanw bits |= TIOCM_DSR;
918 1.1.2.2 nathanw
919 1.1.2.2 nathanw r = SAB_READ(sc, SAB_MODE);
920 1.1.2.2 nathanw if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
921 1.1.2.2 nathanw bits |= TIOCM_RTS;
922 1.1.2.2 nathanw break;
923 1.1.2.2 nathanw case DMSET:
924 1.1.2.2 nathanw r = SAB_READ(sc, SAB_MODE);
925 1.1.2.2 nathanw if (bits & TIOCM_RTS) {
926 1.1.2.2 nathanw r &= ~SAB_MODE_FRTS;
927 1.1.2.2 nathanw r |= SAB_MODE_RTS;
928 1.1.2.2 nathanw } else
929 1.1.2.2 nathanw r |= SAB_MODE_FRTS | SAB_MODE_RTS;
930 1.1.2.2 nathanw SAB_WRITE(sc, SAB_MODE, r);
931 1.1.2.2 nathanw
932 1.1.2.2 nathanw r = SAB_READ(sc, SAB_PVR);
933 1.1.2.2 nathanw if (bits & TIOCM_DTR)
934 1.1.2.2 nathanw r &= ~sc->sc_pvr_dtr;
935 1.1.2.2 nathanw else
936 1.1.2.2 nathanw r |= sc->sc_pvr_dtr;
937 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PVR, r);
938 1.1.2.2 nathanw break;
939 1.1.2.2 nathanw case DMBIS:
940 1.1.2.2 nathanw if (bits & TIOCM_RTS) {
941 1.1.2.2 nathanw r = SAB_READ(sc, SAB_MODE);
942 1.1.2.2 nathanw r &= ~SAB_MODE_FRTS;
943 1.1.2.2 nathanw r |= SAB_MODE_RTS;
944 1.1.2.2 nathanw SAB_WRITE(sc, SAB_MODE, r);
945 1.1.2.2 nathanw }
946 1.1.2.2 nathanw if (bits & TIOCM_DTR) {
947 1.1.2.2 nathanw r = SAB_READ(sc, SAB_PVR);
948 1.1.2.2 nathanw r &= ~sc->sc_pvr_dtr;
949 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PVR, r);
950 1.1.2.2 nathanw }
951 1.1.2.2 nathanw break;
952 1.1.2.2 nathanw case DMBIC:
953 1.1.2.2 nathanw if (bits & TIOCM_RTS) {
954 1.1.2.2 nathanw r = SAB_READ(sc, SAB_MODE);
955 1.1.2.2 nathanw r |= SAB_MODE_FRTS | SAB_MODE_RTS;
956 1.1.2.2 nathanw SAB_WRITE(sc, SAB_MODE, r);
957 1.1.2.2 nathanw }
958 1.1.2.2 nathanw if (bits & TIOCM_DTR) {
959 1.1.2.2 nathanw r = SAB_READ(sc, SAB_PVR);
960 1.1.2.2 nathanw r |= sc->sc_pvr_dtr;
961 1.1.2.2 nathanw SAB_WRITE(sc, SAB_PVR, r);
962 1.1.2.2 nathanw }
963 1.1.2.2 nathanw break;
964 1.1.2.2 nathanw }
965 1.1.2.2 nathanw splx(s);
966 1.1.2.2 nathanw return (bits);
967 1.1.2.2 nathanw }
968 1.1.2.2 nathanw
969 1.1.2.2 nathanw int
970 1.1.2.2 nathanw sabttyparam(sc, tp, t)
971 1.1.2.2 nathanw struct sabtty_softc *sc;
972 1.1.2.2 nathanw struct tty *tp;
973 1.1.2.2 nathanw struct termios *t;
974 1.1.2.2 nathanw {
975 1.1.2.2 nathanw int s, ospeed;
976 1.1.2.2 nathanw tcflag_t cflag;
977 1.1.2.2 nathanw u_int8_t dafo, r;
978 1.1.2.2 nathanw
979 1.1.2.2 nathanw ospeed = sabtty_speed(t->c_ospeed);
980 1.1.2.2 nathanw if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
981 1.1.2.2 nathanw return (EINVAL);
982 1.1.2.2 nathanw
983 1.1.2.2 nathanw s = spltty();
984 1.1.2.2 nathanw
985 1.1.2.2 nathanw /* hang up line if ospeed is zero, otherwise raise dtr */
986 1.1.2.2 nathanw sabtty_mdmctrl(sc, TIOCM_DTR,
987 1.1.2.2 nathanw (t->c_ospeed == 0) ? DMBIC : DMBIS);
988 1.1.2.2 nathanw
989 1.1.2.2 nathanw dafo = SAB_READ(sc, SAB_DAFO);
990 1.1.2.2 nathanw
991 1.1.2.2 nathanw cflag = t->c_cflag;
992 1.1.2.2 nathanw
993 1.1.2.2 nathanw if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
994 1.1.2.2 nathanw cflag |= CLOCAL;
995 1.1.2.2 nathanw cflag &= ~HUPCL;
996 1.1.2.2 nathanw }
997 1.1.2.2 nathanw
998 1.1.2.2 nathanw if (cflag & CSTOPB)
999 1.1.2.2 nathanw dafo |= SAB_DAFO_STOP;
1000 1.1.2.2 nathanw else
1001 1.1.2.2 nathanw dafo &= ~SAB_DAFO_STOP;
1002 1.1.2.2 nathanw
1003 1.1.2.2 nathanw dafo &= ~SAB_DAFO_CHL_CSIZE;
1004 1.1.2.2 nathanw switch (cflag & CSIZE) {
1005 1.1.2.2 nathanw case CS5:
1006 1.1.2.2 nathanw dafo |= SAB_DAFO_CHL_CS5;
1007 1.1.2.2 nathanw break;
1008 1.1.2.2 nathanw case CS6:
1009 1.1.2.2 nathanw dafo |= SAB_DAFO_CHL_CS6;
1010 1.1.2.2 nathanw break;
1011 1.1.2.2 nathanw case CS7:
1012 1.1.2.2 nathanw dafo |= SAB_DAFO_CHL_CS7;
1013 1.1.2.2 nathanw break;
1014 1.1.2.2 nathanw default:
1015 1.1.2.2 nathanw dafo |= SAB_DAFO_CHL_CS8;
1016 1.1.2.2 nathanw break;
1017 1.1.2.2 nathanw }
1018 1.1.2.2 nathanw
1019 1.1.2.2 nathanw dafo &= ~SAB_DAFO_PARMASK;
1020 1.1.2.2 nathanw if (cflag & PARENB) {
1021 1.1.2.2 nathanw if (cflag & PARODD)
1022 1.1.2.2 nathanw dafo |= SAB_DAFO_PAR_ODD;
1023 1.1.2.2 nathanw else
1024 1.1.2.2 nathanw dafo |= SAB_DAFO_PAR_EVEN;
1025 1.1.2.2 nathanw } else
1026 1.1.2.2 nathanw dafo |= SAB_DAFO_PAR_NONE;
1027 1.1.2.2 nathanw
1028 1.1.2.2 nathanw if (ospeed != 0) {
1029 1.1.2.2 nathanw SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
1030 1.1.2.2 nathanw r = SAB_READ(sc, SAB_CCR2);
1031 1.1.2.2 nathanw r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
1032 1.1.2.2 nathanw r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
1033 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR2, r);
1034 1.1.2.2 nathanw }
1035 1.1.2.2 nathanw
1036 1.1.2.2 nathanw r = SAB_READ(sc, SAB_MODE);
1037 1.1.2.2 nathanw r |= SAB_MODE_RAC;
1038 1.1.2.2 nathanw if (cflag & CRTSCTS) {
1039 1.1.2.2 nathanw r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
1040 1.1.2.2 nathanw r |= SAB_MODE_FRTS;
1041 1.1.2.2 nathanw sc->sc_imr1 &= ~SAB_IMR1_CSC;
1042 1.1.2.2 nathanw } else {
1043 1.1.2.2 nathanw r |= SAB_MODE_RTS | SAB_MODE_FCTS;
1044 1.1.2.2 nathanw r &= ~SAB_MODE_FRTS;
1045 1.1.2.2 nathanw sc->sc_imr1 |= SAB_IMR1_CSC;
1046 1.1.2.2 nathanw }
1047 1.1.2.2 nathanw SAB_WRITE(sc, SAB_MODE, r);
1048 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1049 1.1.2.2 nathanw
1050 1.1.2.2 nathanw tp->t_cflag = cflag;
1051 1.1.2.2 nathanw
1052 1.1.2.2 nathanw splx(s);
1053 1.1.2.2 nathanw return (0);
1054 1.1.2.2 nathanw }
1055 1.1.2.2 nathanw
1056 1.1.2.2 nathanw int
1057 1.1.2.2 nathanw sabtty_param(tp, t)
1058 1.1.2.2 nathanw struct tty *tp;
1059 1.1.2.2 nathanw struct termios *t;
1060 1.1.2.2 nathanw {
1061 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
1062 1.1.2.2 nathanw
1063 1.1.2.2 nathanw return (sabttyparam(sc, tp, t));
1064 1.1.2.2 nathanw }
1065 1.1.2.2 nathanw
1066 1.1.2.2 nathanw void
1067 1.1.2.2 nathanw sabtty_start(tp)
1068 1.1.2.2 nathanw struct tty *tp;
1069 1.1.2.2 nathanw {
1070 1.1.2.2 nathanw struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
1071 1.1.2.2 nathanw int s;
1072 1.1.2.2 nathanw
1073 1.1.2.2 nathanw s = spltty();
1074 1.1.2.2 nathanw if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
1075 1.1.2.2 nathanw if (tp->t_outq.c_cc <= tp->t_lowat) {
1076 1.1.2.2 nathanw if (tp->t_state & TS_ASLEEP) {
1077 1.1.2.2 nathanw tp->t_state &= ~TS_ASLEEP;
1078 1.1.2.2 nathanw wakeup(&tp->t_outq);
1079 1.1.2.2 nathanw }
1080 1.1.2.2 nathanw selwakeup(&tp->t_wsel);
1081 1.1.2.2 nathanw }
1082 1.1.2.2 nathanw if (tp->t_outq.c_cc) {
1083 1.1.2.2 nathanw sc->sc_txc = ndqb(&tp->t_outq, 0);
1084 1.1.2.2 nathanw sc->sc_txp = tp->t_outq.c_cf;
1085 1.1.2.2 nathanw tp->t_state |= TS_BUSY;
1086 1.1.2.2 nathanw sc->sc_imr1 &= ~SAB_IMR1_XPR;
1087 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1088 1.1.2.2 nathanw }
1089 1.1.2.2 nathanw }
1090 1.1.2.2 nathanw splx(s);
1091 1.1.2.2 nathanw }
1092 1.1.2.2 nathanw
1093 1.1.2.2 nathanw void
1094 1.1.2.2 nathanw sabtty_cec_wait(sc)
1095 1.1.2.2 nathanw struct sabtty_softc *sc;
1096 1.1.2.2 nathanw {
1097 1.1.2.2 nathanw int i = 50000;
1098 1.1.2.2 nathanw
1099 1.1.2.2 nathanw for (;;) {
1100 1.1.2.2 nathanw if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
1101 1.1.2.2 nathanw return;
1102 1.1.2.2 nathanw if (--i == 0)
1103 1.1.2.2 nathanw break;
1104 1.1.2.2 nathanw DELAY(1);
1105 1.1.2.2 nathanw }
1106 1.1.2.2 nathanw }
1107 1.1.2.2 nathanw
1108 1.1.2.2 nathanw void
1109 1.1.2.2 nathanw sabtty_tec_wait(sc)
1110 1.1.2.2 nathanw struct sabtty_softc *sc;
1111 1.1.2.2 nathanw {
1112 1.1.2.2 nathanw int i = 200000;
1113 1.1.2.2 nathanw
1114 1.1.2.2 nathanw for (;;) {
1115 1.1.2.2 nathanw if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
1116 1.1.2.2 nathanw return;
1117 1.1.2.2 nathanw if (--i == 0)
1118 1.1.2.2 nathanw break;
1119 1.1.2.2 nathanw DELAY(1);
1120 1.1.2.2 nathanw }
1121 1.1.2.2 nathanw }
1122 1.1.2.2 nathanw
1123 1.1.2.2 nathanw void
1124 1.1.2.2 nathanw sabtty_reset(sc)
1125 1.1.2.2 nathanw struct sabtty_softc *sc;
1126 1.1.2.2 nathanw {
1127 1.1.2.2 nathanw /* power down */
1128 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR0, 0);
1129 1.1.2.2 nathanw
1130 1.1.2.2 nathanw /* set basic configuration */
1131 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR0,
1132 1.1.2.2 nathanw SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
1133 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
1134 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
1135 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR3, 0);
1136 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG);
1137 1.1.2.2 nathanw SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
1138 1.1.2.2 nathanw SAB_WRITE(sc, SAB_RFC,
1139 1.1.2.2 nathanw SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
1140 1.1.2.2 nathanw
1141 1.1.2.2 nathanw /* clear interrupts */
1142 1.1.2.2 nathanw sc->sc_imr0 = sc->sc_imr1 = 0xff;
1143 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
1144 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1145 1.1.2.2 nathanw SAB_READ(sc, SAB_ISR0);
1146 1.1.2.2 nathanw SAB_READ(sc, SAB_ISR1);
1147 1.1.2.2 nathanw }
1148 1.1.2.2 nathanw
1149 1.1.2.2 nathanw void
1150 1.1.2.2 nathanw sabtty_flush(sc)
1151 1.1.2.2 nathanw struct sabtty_softc *sc;
1152 1.1.2.2 nathanw {
1153 1.1.2.2 nathanw /* clear rx fifo */
1154 1.1.2.2 nathanw sabtty_cec_wait(sc);
1155 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1156 1.1.2.2 nathanw
1157 1.1.2.2 nathanw /* clear tx fifo */
1158 1.1.2.2 nathanw sabtty_cec_wait(sc);
1159 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
1160 1.1.2.2 nathanw }
1161 1.1.2.2 nathanw
1162 1.1.2.2 nathanw int
1163 1.1.2.2 nathanw sabtty_speed(rate)
1164 1.1.2.2 nathanw int rate;
1165 1.1.2.2 nathanw {
1166 1.1.2.2 nathanw int i, len, r;
1167 1.1.2.2 nathanw
1168 1.1.2.2 nathanw if (rate == 0)
1169 1.1.2.2 nathanw return (0);
1170 1.1.2.2 nathanw len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
1171 1.1.2.2 nathanw for (i = 0; i < len; i++) {
1172 1.1.2.2 nathanw if (rate == sabtty_baudtable[i].baud) {
1173 1.1.2.2 nathanw r = sabtty_baudtable[i].n |
1174 1.1.2.2 nathanw (sabtty_baudtable[i].m << 6);
1175 1.1.2.2 nathanw return (r);
1176 1.1.2.2 nathanw }
1177 1.1.2.2 nathanw }
1178 1.1.2.2 nathanw return (-1);
1179 1.1.2.2 nathanw }
1180 1.1.2.2 nathanw
1181 1.1.2.2 nathanw void
1182 1.1.2.2 nathanw sabtty_cnputc(sc, c)
1183 1.1.2.2 nathanw struct sabtty_softc *sc;
1184 1.1.2.2 nathanw int c;
1185 1.1.2.2 nathanw {
1186 1.1.2.2 nathanw sabtty_tec_wait(sc);
1187 1.1.2.2 nathanw SAB_WRITE(sc, SAB_TIC, c);
1188 1.1.2.2 nathanw sabtty_tec_wait(sc);
1189 1.1.2.2 nathanw }
1190 1.1.2.2 nathanw
1191 1.1.2.2 nathanw int
1192 1.1.2.2 nathanw sabtty_cngetc(sc)
1193 1.1.2.2 nathanw struct sabtty_softc *sc;
1194 1.1.2.2 nathanw {
1195 1.1.2.2 nathanw u_int8_t r, len;
1196 1.1.2.2 nathanw
1197 1.1.2.2 nathanw again:
1198 1.1.2.2 nathanw do {
1199 1.1.2.2 nathanw r = SAB_READ(sc, SAB_STAR);
1200 1.1.2.2 nathanw } while ((r & SAB_STAR_RFNE) == 0);
1201 1.1.2.2 nathanw
1202 1.1.2.2 nathanw /*
1203 1.1.2.2 nathanw * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
1204 1.1.2.2 nathanw * (I hate this chip... hate hate hate).
1205 1.1.2.2 nathanw */
1206 1.1.2.2 nathanw sabtty_cec_wait(sc);
1207 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
1208 1.1.2.2 nathanw
1209 1.1.2.2 nathanw /* Wait for RFIFO to come ready */
1210 1.1.2.2 nathanw do {
1211 1.1.2.2 nathanw r = SAB_READ(sc, SAB_ISR0);
1212 1.1.2.2 nathanw } while ((r & SAB_ISR0_TCD) == 0);
1213 1.1.2.2 nathanw
1214 1.1.2.2 nathanw len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
1215 1.1.2.2 nathanw if (len == 0)
1216 1.1.2.2 nathanw goto again; /* Shouldn't happen... */
1217 1.1.2.2 nathanw
1218 1.1.2.2 nathanw r = SAB_READ(sc, SAB_RFIFO);
1219 1.1.2.2 nathanw
1220 1.1.2.2 nathanw /*
1221 1.1.2.2 nathanw * Blow away everything left in the FIFO...
1222 1.1.2.2 nathanw */
1223 1.1.2.2 nathanw sabtty_cec_wait(sc);
1224 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
1225 1.1.2.2 nathanw return (r);
1226 1.1.2.2 nathanw }
1227 1.1.2.2 nathanw
1228 1.1.2.2 nathanw void
1229 1.1.2.2 nathanw sabtty_cnpollc(sc, on)
1230 1.1.2.2 nathanw struct sabtty_softc *sc;
1231 1.1.2.2 nathanw int on;
1232 1.1.2.2 nathanw {
1233 1.1.2.2 nathanw u_int8_t r;
1234 1.1.2.2 nathanw
1235 1.1.2.2 nathanw if (on) {
1236 1.1.2.2 nathanw if (sc->sc_polling)
1237 1.1.2.2 nathanw return;
1238 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
1239 1.1.2.2 nathanw r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
1240 1.1.2.2 nathanw r &= ~(SAB_RFC_RFDF);
1241 1.1.2.2 nathanw SAB_WRITE(sc, SAB_RFC, r);
1242 1.1.2.2 nathanw sabtty_cec_wait(sc);
1243 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1244 1.1.2.2 nathanw sc->sc_polling = 1;
1245 1.1.2.2 nathanw } else {
1246 1.1.2.2 nathanw if (!sc->sc_polling)
1247 1.1.2.2 nathanw return;
1248 1.1.2.2 nathanw SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
1249 1.1.2.2 nathanw SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
1250 1.1.2.2 nathanw sabtty_cec_wait(sc);
1251 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1252 1.1.2.2 nathanw sc->sc_polling = 0;
1253 1.1.2.2 nathanw }
1254 1.1.2.2 nathanw }
1255 1.1.2.2 nathanw
1256 1.1.2.2 nathanw void
1257 1.1.2.2 nathanw sab_cnputc(dev, c)
1258 1.1.2.2 nathanw dev_t dev;
1259 1.1.2.2 nathanw int c;
1260 1.1.2.2 nathanw {
1261 1.1.2.2 nathanw struct sabtty_softc *sc = sabtty_cons_output;
1262 1.1.2.2 nathanw
1263 1.1.2.2 nathanw if (sc == NULL)
1264 1.1.2.2 nathanw return;
1265 1.1.2.2 nathanw sabtty_cnputc(sc, c);
1266 1.1.2.2 nathanw }
1267 1.1.2.2 nathanw
1268 1.1.2.2 nathanw void
1269 1.1.2.2 nathanw sab_cnpollc(dev, on)
1270 1.1.2.2 nathanw dev_t dev;
1271 1.1.2.2 nathanw int on;
1272 1.1.2.2 nathanw {
1273 1.1.2.2 nathanw struct sabtty_softc *sc = sabtty_cons_input;
1274 1.1.2.2 nathanw
1275 1.1.2.2 nathanw sabtty_cnpollc(sc, on);
1276 1.1.2.2 nathanw }
1277 1.1.2.2 nathanw
1278 1.1.2.2 nathanw int
1279 1.1.2.2 nathanw sab_cngetc(dev)
1280 1.1.2.2 nathanw dev_t dev;
1281 1.1.2.2 nathanw {
1282 1.1.2.2 nathanw struct sabtty_softc *sc = sabtty_cons_input;
1283 1.1.2.2 nathanw
1284 1.1.2.2 nathanw if (sc == NULL)
1285 1.1.2.2 nathanw return (-1);
1286 1.1.2.2 nathanw return (sabtty_cngetc(sc));
1287 1.1.2.2 nathanw }
1288 1.1.2.2 nathanw
1289 1.1.2.2 nathanw void
1290 1.1.2.2 nathanw sabtty_console_flags(sc)
1291 1.1.2.2 nathanw struct sabtty_softc *sc;
1292 1.1.2.2 nathanw {
1293 1.1.2.2 nathanw int node, channel, cookie;
1294 1.1.2.2 nathanw u_int chosen;
1295 1.1.2.2 nathanw char buf[255];
1296 1.1.2.2 nathanw
1297 1.1.2.2 nathanw node = sc->sc_parent->sc_node;
1298 1.1.2.2 nathanw channel = sc->sc_portno;
1299 1.1.2.2 nathanw
1300 1.1.2.2 nathanw chosen = OF_finddevice("/chosen");
1301 1.1.2.2 nathanw
1302 1.1.2.2 nathanw /* Default to channel 0 if there are no explicit prom args */
1303 1.1.2.2 nathanw cookie = 0;
1304 1.1.2.2 nathanw
1305 1.1.2.2 nathanw if (node == OF_instance_to_package(OF_stdin())) {
1306 1.1.2.2 nathanw if (OF_getprop(chosen, "input-device", buf,
1307 1.1.2.2 nathanw sizeof(buf)) != -1) {
1308 1.1.2.2 nathanw if (strcmp("ttyb", buf) == 0)
1309 1.1.2.2 nathanw cookie = 1;
1310 1.1.2.2 nathanw }
1311 1.1.2.2 nathanw
1312 1.1.2.2 nathanw if (channel == cookie)
1313 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_CONS_IN;
1314 1.1.2.2 nathanw }
1315 1.1.2.2 nathanw
1316 1.1.2.2 nathanw /* Default to same channel if there are no explicit prom args */
1317 1.1.2.2 nathanw
1318 1.1.2.2 nathanw if (node == OF_instance_to_package(OF_stdout())) {
1319 1.1.2.2 nathanw if (OF_getprop(chosen, "output-device", buf,
1320 1.1.2.2 nathanw sizeof(buf)) != -1) {
1321 1.1.2.2 nathanw if (strcmp("ttyb", buf) == 0)
1322 1.1.2.2 nathanw cookie = 1;
1323 1.1.2.2 nathanw }
1324 1.1.2.2 nathanw
1325 1.1.2.2 nathanw if (channel == cookie)
1326 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_CONS_OUT;
1327 1.1.2.2 nathanw }
1328 1.1.2.2 nathanw }
1329 1.1.2.2 nathanw
1330 1.1.2.2 nathanw void
1331 1.1.2.2 nathanw sabtty_abort(sc)
1332 1.1.2.2 nathanw struct sabtty_softc *sc;
1333 1.1.2.2 nathanw {
1334 1.1.2.2 nathanw
1335 1.1.2.2 nathanw if (sc->sc_flags & SABTTYF_CONS_IN) {
1336 1.1.2.2 nathanw #ifdef DDB
1337 1.1.2.2 nathanw cn_trap();
1338 1.1.2.2 nathanw #else
1339 1.1.2.2 nathanw callrom();
1340 1.1.2.2 nathanw #endif
1341 1.1.2.2 nathanw }
1342 1.1.2.2 nathanw }
1343 1.1.2.2 nathanw
1344 1.1.2.2 nathanw void
1345 1.1.2.2 nathanw sabtty_shutdown(vsc)
1346 1.1.2.2 nathanw void *vsc;
1347 1.1.2.2 nathanw {
1348 1.1.2.2 nathanw struct sabtty_softc *sc = vsc;
1349 1.1.2.2 nathanw
1350 1.1.2.2 nathanw /* Have to put the chip back into single char mode */
1351 1.1.2.2 nathanw sc->sc_flags |= SABTTYF_DONTDDB;
1352 1.1.2.2 nathanw SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
1353 1.1.2.2 nathanw sabtty_cec_wait(sc);
1354 1.1.2.2 nathanw SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1355 1.1.2.2 nathanw sabtty_cec_wait(sc);
1356 1.1.2.2 nathanw }
1357