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