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