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