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