1 /* $NetBSD: pq3duart.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $ */
2 /*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 *
10 * This material is based upon work supported by the Defense Advanced Research
11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 * Contract No. N66001-09-C-2073.
13 * Approved for Public Release, Distribution Unlimited
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: pq3duart.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $");
39
40 #include "ioconf.h"
41
42 #include <sys/param.h>
43 #include <sys/cpu.h>
44 #include <sys/device.h>
45 #include <sys/tty.h>
46
47 #include <sys/intr.h>
48 #include <sys/bus.h>
49
50 #include <dev/ic/comreg.h>
51 #include <dev/ic/comvar.h>
52
53 #include <powerpc/booke/cpuvar.h>
54 #include <powerpc/booke/e500var.h>
55 #include <powerpc/booke/e500reg.h>
56
57 struct pq3duart_softc {
58 device_t dsc_dev;
59 struct com_softc *dsc_sc[2];
60 bus_space_tag_t dsc_memt;
61 bus_addr_t dsc_base;
62 void *dsc_ih;
63 };
64
65 struct pq3duart_attach_args {
66 const char *da_busname;
67 bus_space_tag_t da_memt;
68 bus_addr_t da_addr;
69 bus_addr_t da_size;
70 u_int da_port;
71 };
72
73 static int pq3duart_match(device_t, cfdata_t, void *);
74 static void pq3duart_attach(device_t, device_t, void *);
75 static int com_pq3duart_match(device_t, cfdata_t, void *);
76 static void com_pq3duart_attach(device_t, device_t, void *);
77
78 CFATTACH_DECL_NEW(pq3duart, sizeof(struct pq3duart_softc),
79 pq3duart_match, pq3duart_attach, NULL, NULL);
80
81 CFATTACH_DECL_NEW(com_pq3duart, sizeof(struct com_softc),
82 com_pq3duart_match, com_pq3duart_attach, NULL, NULL);
83
84 static int
pq3duart_match(device_t parent,cfdata_t cf,void * aux)85 pq3duart_match(device_t parent, cfdata_t cf, void *aux)
86 {
87
88 if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
89 return 0;
90
91 return 1;
92 }
93
94 static int
com_pq3duart_match(device_t parent,cfdata_t cfdata,void * aux)95 com_pq3duart_match(device_t parent, cfdata_t cfdata, void *aux)
96 {
97 struct pq3duart_softc * const dsc = device_private(parent);
98 struct pq3duart_attach_args * const da = aux;
99 struct com_regs regs;
100
101 if ((da->da_port != 1 && da->da_port != 2)
102 || dsc->dsc_sc[da->da_port-1] != NULL)
103 return 0;
104
105 bus_space_tag_t memt = da->da_memt;
106 bus_addr_t addr = da->da_addr;
107 bus_addr_t size = da->da_size;
108 bus_space_handle_t memh;
109
110 if (com_is_console(memt, addr, &memh))
111 return 1;
112
113 if (bus_space_map(memt, addr, size, 0, &memh))
114 return 0;
115
116 com_init_regs(®s, memt, memh, addr);
117
118 int rv = com_probe_subr(®s);
119
120 bus_space_unmap(memt, memh, size);
121
122 return rv;
123 }
124
125 static int
pq3duart_intr(void * arg)126 pq3duart_intr(void *arg)
127 {
128 struct pq3duart_softc * const dsc = arg;
129 int rv = 0;
130
131 if (dsc->dsc_sc[0] != NULL)
132 rv += comintr(dsc->dsc_sc[0]);
133 if (dsc->dsc_sc[1] != NULL)
134 rv += comintr(dsc->dsc_sc[1]);
135
136 return rv;
137 }
138
139 static int
pq3duart_print(void * aux,const char * pnp)140 pq3duart_print(void *aux, const char *pnp)
141 {
142 struct pq3duart_attach_args * const da = aux;
143
144 if (pnp != NULL)
145 return QUIET;
146
147 aprint_normal(" port %d", da->da_port);
148
149 return UNCONF;
150 }
151
152 static void
pq3duart_attach(device_t parent,device_t self,void * aux)153 pq3duart_attach(device_t parent, device_t self, void *aux)
154 {
155 struct cpunode_softc * const psc = device_private(parent);
156 struct pq3duart_softc * const dsc = device_private(self);
157 struct cpunode_attach_args * const cna = aux;
158 struct cpunode_locators * const cnl = &cna->cna_locs;
159 struct pq3duart_attach_args da;
160 u_int nports = cnl->cnl_size / DUART_SIZE;
161
162 psc->sc_children |= cna->cna_childmask;
163 dsc->dsc_dev = self;
164
165 aprint_normal(": %u ports\n", nports);
166
167 for (u_int port = 1; port <= uimin(2, nports); port++) {
168 da.da_memt = cna->cna_memt;
169 da.da_port = port;
170 da.da_addr = cnl->cnl_addr + (port - 1) * DUART_SIZE;
171 da.da_size = COM_NPORTS;
172 config_found(self, &da, pq3duart_print, CFARGS_NONE);
173 }
174
175 if (dsc->dsc_sc[0] != NULL || dsc->dsc_sc[1] != NULL) {
176 dsc->dsc_ih = intr_establish(cnl->cnl_intrs[0], IPL_SERIAL,
177 IST_ONCHIP, pq3duart_intr, dsc);
178 if (dsc->dsc_ih == NULL)
179 aprint_error_dev(self,
180 "failed to establish interrupt %d\n",
181 cnl->cnl_intrs[0]);
182 else
183 aprint_normal_dev(self,
184 "interrupting on irq %d\n",
185 cnl->cnl_intrs[0]);
186 }
187 }
188
189 static void
com_pq3duart_attach(device_t parent,device_t self,void * aux)190 com_pq3duart_attach(device_t parent, device_t self, void *aux)
191 {
192 struct pq3duart_softc * const dsc = device_private(parent);
193 struct com_softc * const sc = device_private(self);
194 struct pq3duart_attach_args * const da = aux;
195
196 dsc->dsc_sc[da->da_port-1] = sc;
197 sc->sc_dev = self;
198 sc->sc_frequency = (int) board_info_get_number("bus-frequency");
199
200 bus_space_tag_t memt = da->da_memt;
201 bus_addr_t addr = da->da_addr;
202 bus_addr_t size = da->da_size;
203 bus_space_handle_t memh;
204
205 if (!com_is_console(memt, addr, &memh)) {
206 int error = bus_space_map(memt, addr, size, 0, &memh);
207 if (error) {
208 aprint_error(": failed to map registers: %d\n", error);
209 return;
210 }
211 }
212
213 com_init_regs(&sc->sc_regs, memt, memh, addr);
214 sc->sc_regs.cr_nports = size;
215
216 com_attach_subr(sc);
217
218 }
219