1 /* $NetBSD: com_gsc.c,v 1.2 2018/12/08 17:46:11 thorpej Exp $ */
2
3 /* $OpenBSD: com_gsc.c,v 1.8 2000/03/13 14:39:59 mickey Exp $ */
4
5 /*
6 * Copyright (c) 1998-2004 Michael Shalayeff
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: com_gsc.c,v 1.2 2018/12/08 17:46:11 thorpej Exp $");
33
34 #include "opt_kgdb.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/tty.h>
40
41 #ifdef KGDB
42 #include <sys/kgdb.h>
43 #endif
44
45 #include <sys/bus.h>
46 #include <machine/intr.h>
47 #include <machine/iomod.h>
48 #include <machine/autoconf.h>
49
50 #include <dev/ic/comreg.h>
51 #include <dev/ic/comvar.h>
52
53 #include <hppa/dev/cpudevs.h>
54 #include <hppa/gsc/gscbusvar.h>
55 #include <hppa/hppa/machdep.h>
56
57 #define COMGSC_OFFSET 0x800
58 #define COMGSC_FREQUENCY (1843200 * 4) /* 16-bit baud rate divisor */
59
60 struct com_gsc_regs {
61 uint8_t reset;
62 };
63
64 struct com_gsc_softc {
65 struct com_softc sc_com; /* real "com" softc */
66
67 /* GSC-specific goo. */
68 void *sc_ih; /* interrupt handler */
69 };
70
71 int com_gsc_probe(device_t, cfdata_t , void *);
72 void com_gsc_attach(device_t, device_t, void *);
73
74 CFATTACH_DECL_NEW(com_gsc, sizeof(struct com_gsc_softc),
75 com_gsc_probe, com_gsc_attach, NULL, NULL);
76
77 int
com_gsc_probe(device_t parent,cfdata_t match,void * aux)78 com_gsc_probe(device_t parent, cfdata_t match, void *aux)
79 {
80 struct gsc_attach_args *ga = aux;
81
82 if (ga->ga_type.iodc_type != HPPA_TYPE_FIO ||
83 (ga->ga_type.iodc_sv_model != HPPA_FIO_GRS232 &&
84 ga->ga_type.iodc_sv_model != HPPA_FIO_RS232))
85 return 0;
86
87 return 1;
88 }
89
90 void
com_gsc_attach(device_t parent,device_t self,void * aux)91 com_gsc_attach(device_t parent, device_t self, void *aux)
92 {
93 struct com_gsc_softc *gsc = device_private(self);
94 struct com_softc *sc = &gsc->sc_com;
95 struct gsc_attach_args *ga = aux;
96 int pagezero_cookie;
97 bus_space_tag_t iot;
98 bus_space_handle_t ioh;
99 bus_addr_t iobase;
100
101 sc->sc_dev = self;
102 sc->sc_hwflags = 0;
103 sc->sc_swflags = 0;
104 iot = ga->ga_iot;
105 iobase = (bus_addr_t)ga->ga_hpa + COMGSC_OFFSET;
106 sc->sc_frequency = COMGSC_FREQUENCY;
107
108 /* Test if this is the console. Compare either HPA or device path. */
109 pagezero_cookie = hppa_pagezero_map();
110 if ((hppa_hpa_t)PAGE0->mem_cons.pz_hpa == ga->ga_hpa ) {
111
112 /*
113 * This port is the console. In this case we must call
114 * comcnattach() and later com_is_console() to initialize
115 * everything properly.
116 */
117
118 if (comcnattach(iot, iobase, B9600,
119 sc->sc_frequency, COM_TYPE_NORMAL,
120 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0) {
121 aprint_error(": can't comcnattach\n");
122 hppa_pagezero_unmap(pagezero_cookie);
123 return;
124 }
125 }
126 hppa_pagezero_unmap(pagezero_cookie);
127
128 /*
129 * Get the already initialized console ioh via com_is_console() if
130 * this is the console or map the I/O space if this isn't the console.
131 */
132
133 if (!com_is_console(iot, iobase, &ioh) &&
134 bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh) != 0) {
135 aprint_error(": can't map I/O space\n");
136 return;
137 }
138 com_init_regs(&sc->sc_regs, iot, ioh, iobase);
139
140 com_attach_subr(sc);
141 gsc->sc_ih = hppa_intr_establish(IPL_TTY, comintr, sc, ga->ga_ir,
142 ga->ga_irq);
143 }
144
145 #ifdef KGDB
146 int com_gsc_kgdb_attach(void);
147 int
com_gsc_kgdb_attach(void)148 com_gsc_kgdb_attach(void)
149 {
150 int error;
151
152 printf("kgdb: attaching com at 0x%x at %d baud...",
153 KGDBADDR, KGDBRATE);
154 error = com_kgdb_attach(&hppa_bustag, KGDBADDR + COMGSC_OFFSET,
155 KGDBRATE, COMGSC_FREQUENCY, COM_TYPE_NORMAL,
156 ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) |
157 CS8));
158 if (error) {
159 printf(" failed (%d)\n", error);
160 } else {
161 printf(" ok\n");
162 }
163 return (error);
164 }
165 #endif /* KGDB */
166