rmixl_iobus.c revision 1.6 1 /* $NetBSD: rmixl_iobus.c,v 1.6 2021/04/24 23:36:43 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Cliff Neighbors
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * RMI Peripherals IO Bus support
34 * - interface to NOR, NAND, PCMCIA Memory controllers, &etc.
35 * - manages the 10 Chip Selects
36 * - manages the "Flash" interrupts
37 * - manages the "Flash" errors
38 */
39
40 /*
41 * iobus control registers are accessed as 32 bits.
42 * ALEn and CLEn NAND control registers are defined as 8 bits wide
43 * but that seems to be a documentation error.
44 *
45 * iobus data access may be as 1 or 2 or 4 bytes, even if device is 1 byte wide;
46 * the controller will sequence the bytes, in big-endian order.
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: rmixl_iobus.c,v 1.6 2021/04/24 23:36:43 thorpej Exp $");
51
52 #include "locators.h"
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/device.h>
57
58 #include <sys/bus.h>
59
60 #include <mips/rmi/rmixlreg.h>
61 #include <mips/rmi/rmixlvar.h>
62 #include <mips/rmi/rmixl_intr.h>
63 #include <mips/rmi/rmixl_obiovar.h>
64 #include <mips/rmi/rmixl_iobusvar.h>
65 // #include <mips/rmi/rmixl_gpiovar.h>
66
67 typedef struct {
68 bool cs_allocated;
69 uint32_t cs_addr; /* base address on the Peripherals I/O Bus */
70 uint32_t cs_mask; /* address mask on the Peripherals I/O Bus */
71 uint32_t cs_dev_parm;
72 } rmixl_iobus_csconfig_t;
73
74 typedef struct rmixl_iobus_softc {
75 device_t sc_dev;
76 bus_space_tag_t sc_obio_bst; /* for iobus device controller access */
77 bus_space_handle_t sc_obio_bsh; /* " " " " " */
78 bus_addr_t sc_obio_addr;
79 bus_size_t sc_obio_size;
80 bus_space_tag_t sc_iobus_bst; /* for iobus access */
81 rmixl_iobus_csconfig_t sc_csconfig[RMIXL_FLASH_NCS];
82 } rmixl_iobus_softc_t;
83
84
85 static int rmixl_iobus_match(device_t, cfdata_t, void *);
86 static void rmixl_iobus_attach(device_t, device_t, void *);
87 static void rmixl_iobus_csconfig_init(struct rmixl_iobus_softc *);
88 static int rmixl_iobus_print(void *, const char *);
89 static int rmixl_iobus_search(device_t, cfdata_t, const int *, void *);
90 #ifdef NOTYET
91 static int rmixl_iobus_intr(void *);
92 #endif
93
94 #ifdef RMIXL_IOBUS_DEBUG
95 rmixl_iobus_softc_t *rmixl_iobus_sc;
96 #endif
97
98
99 CFATTACH_DECL_NEW(rmixl_iobus, sizeof (rmixl_iobus_softc_t),
100 rmixl_iobus_match, rmixl_iobus_attach, NULL, NULL);
101
102 int
103 rmixl_iobus_match(device_t parent, cfdata_t match, void *aux)
104 {
105 struct obio_attach_args *obio = aux;
106
107 if (obio->obio_addr == RMIXL_IO_DEV_FLASH)
108 return rmixl_probe_4((volatile uint32_t *)
109 RMIXL_IOREG_VADDR(obio->obio_addr));
110
111 return 0;
112 }
113
114 void
115 rmixl_iobus_attach(device_t parent, device_t self, void *aux)
116 {
117 rmixl_iobus_softc_t *sc = device_private(self);
118 struct obio_attach_args *obio = aux;
119 struct rmixl_config *rcp = &rmixl_configuration;
120 uint64_t r;
121 int err;
122
123 #ifdef RMIXL_IOBUS_DEBUG
124 rmixl_iobus_sc = sc;
125 #endif
126 sc->sc_dev = self;
127 sc->sc_obio_bst = obio->obio_eb_bst;
128 sc->sc_obio_addr = obio->obio_addr;
129 sc->sc_obio_size = 0x1000;
130
131 err = bus_space_map(sc->sc_obio_bst, sc->sc_obio_addr,
132 sc->sc_obio_size, 0, &sc->sc_obio_bsh);
133 if (err != 0) {
134 aprint_error_dev(self,
135 "bus space map err %d, iobus space\n", err);
136 return;
137 }
138
139 r = RMIXL_IOREG_READ(RMIXL_SBC_FLASH_BAR);
140 KASSERT((r & 1) != 0); /* BAR is enabled */
141 rcp->rc_flash_pbase = RMIXL_FLASH_BAR_TO_BA(r);
142 rcp->rc_flash_mask = RMIXL_FLASH_BAR_TO_MASK(r);
143
144 aprint_normal("\n");
145 aprint_debug_dev(self,
146 "Flash BAR pbase %#" PRIx64 " mask %#" PRIx64 "\n",
147 rcp->rc_flash_pbase, rcp->rc_flash_mask);
148
149 /* initialize iobus bus space */
150 rmixl_iobus_bus_mem_init(&rcp->rc_iobus_memt, rcp);
151 sc->sc_iobus_bst = (bus_space_tag_t)&rcp->rc_iobus_memt;
152
153 /* disable all Flash interrupts */
154 bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
155 RMIXL_FLASH_INT_MASK, 0);
156
157 /* write-1-to-clear Flash interrupt status */
158 bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
159 RMIXL_FLASH_INT_STATUS, ~0);
160
161 rmixl_iobus_csconfig_init(sc);
162
163 /* attach any children */
164 config_search(self, NULL,
165 CFARG_SEARCH, rmixl_iobus_search,
166 CFARG_EOL);
167 }
168
169 static void
170 rmixl_iobus_csconfig_init(struct rmixl_iobus_softc *sc)
171 {
172 rmixl_iobus_csconfig_t *cs = &sc->sc_csconfig[0];
173
174 for (int i=0; i < RMIXL_FLASH_NCS; i++) {
175 memset(cs, 0, sizeof(rmixl_iobus_csconfig_t));
176 cs->cs_addr = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
177 RMIXL_FLASH_CSBASE_ADDRn(i)) << 16;
178 cs->cs_mask = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
179 RMIXL_FLASH_CSADDR_MASKn(i)) << 16;
180 cs->cs_mask |= __BITS(15,0);
181 cs->cs_dev_parm = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
182 RMIXL_FLASH_CSDEV_PARMn(i));
183 aprint_debug_dev(sc->sc_dev,
184 "CS#%d: addr 0x%08x mask 0x%08x parm 0x%08x\n",
185 i, cs->cs_addr, cs->cs_mask, cs->cs_dev_parm);
186 cs++;
187 }
188 }
189
190
191 static int
192 rmixl_iobus_print(void *aux, const char *pnp)
193 {
194 struct rmixl_iobus_attach_args *ia = aux;
195
196 if (ia->ia_cs != RMIXL_IOBUSCF_CS_DEFAULT)
197 aprint_normal(" CS#%d", ia->ia_cs);
198 if (ia->ia_iobus_addr != RMIXL_IOBUSCF_ADDR_DEFAULT) {
199 aprint_normal(" addr %#" PRIxBUSADDR, ia->ia_iobus_addr);
200 if (ia->ia_iobus_size != RMIXL_IOBUSCF_SIZE_DEFAULT)
201 aprint_normal("-%#" PRIxBUSSIZE,
202 ia->ia_iobus_addr + (ia->ia_iobus_size - 1));
203 }
204 if (ia->ia_iobus_intr != RMIXL_IOBUSCF_INTR_DEFAULT)
205 aprint_normal(" intr %d", ia->ia_iobus_intr);
206
207 return UNCONF;
208 }
209
210 static int
211 rmixl_iobus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
212 {
213 struct rmixl_iobus_softc *sc = device_private(parent);
214 struct rmixl_iobus_attach_args ia;
215 rmixl_iobus_csconfig_t *cs;
216
217 ia.ia_obio_bst = sc->sc_obio_bst;
218 ia.ia_obio_bsh = sc->sc_obio_bsh;
219 ia.ia_iobus_bst = sc->sc_iobus_bst;
220 ia.ia_iobus_addr = (bus_addr_t)cf->cf_loc[RMIXL_IOBUSCF_ADDR];
221 ia.ia_iobus_size = (bus_size_t)cf->cf_loc[RMIXL_IOBUSCF_SIZE];
222 ia.ia_iobus_intr = cf->cf_loc[RMIXL_IOBUSCF_INTR];
223 ia.ia_cs = cf->cf_loc[RMIXL_IOBUSCF_CS];
224
225 if (ia.ia_cs != RMIXL_IOBUSCF_CS_DEFAULT) {
226 /* CS is configured */
227 cs = &sc->sc_csconfig[ia.ia_cs];
228
229 /* ensure exclusive use of chip select */
230 if (cs->cs_allocated) {
231 aprint_error_dev(parent, "CS#%d already allocated\n",
232 ia.ia_cs);
233 return 0;
234 }
235 if (ia.ia_iobus_addr != RMIXL_IOBUSCF_ADDR_DEFAULT) {
236 if (ia.ia_iobus_addr != cs->cs_addr) {
237 /*
238 * both CS and addr are configured,
239 * ensure they match
240 */
241 aprint_error_dev(parent,
242 "CS#%d addr 0x%08x mismatch cf_loc "
243 "addr 0x%08" PRIxBUSADDR "\n",
244 ia.ia_cs, cs->cs_addr, ia.ia_iobus_addr);
245 return 0;
246 }
247 } else {
248 /* no addr configured, pull from CS */
249 ia.ia_iobus_addr = cs->cs_addr;
250 }
251 } else {
252 /* addr is configured, CS is not; search for matching CS */
253 bool found = false;
254 cs = &sc->sc_csconfig[0];
255 for (int i=0; i < RMIXL_FLASH_NCS; i++) {
256 if (cs->cs_allocated)
257 continue;
258 if (cs->cs_addr == ia.ia_iobus_addr) {
259 ia.ia_cs = i;
260 found = true;
261 break;
262 }
263 cs++;
264 }
265 if (! found) {
266 aprint_error_dev(parent, "no CS for addr 0x%08"
267 PRIxBUSADDR "\n", ia.ia_iobus_addr);
268 return 0;
269 }
270 }
271
272 if (ia.ia_iobus_size != RMIXL_IOBUSCF_SIZE_DEFAULT) {
273 /* ensure size fits w/ CS mask */
274 if ((ia.ia_iobus_size - 1) > (bus_size_t)cs->cs_mask) {
275 aprint_error_dev(parent, "size %#" PRIxBUSSIZE
276 " exceeds CS#%d mask 0x%08x\n",
277 ia.ia_iobus_size, ia.ia_cs, cs->cs_mask);
278 }
279 } else {
280 /* size not configured, pull from CS */
281 ia.ia_iobus_size = (bus_size_t)cs->cs_mask + 1;
282 }
283
284 ia.ia_dev_parm = cs->cs_dev_parm;
285
286 if (config_probe(parent, cf, &ia)) {
287 cs->cs_allocated = true;
288 config_attach(parent, cf, &ia, rmixl_iobus_print, CFARG_EOL);
289 }
290
291 return 0;
292 }
293
294
295 #ifdef NOTYET
296
297 void
298 rmixl_iobus_intr_disestablish(void *uh, void *ih)
299 {
300 rmixl_iobus_softc_t *sc = uh;
301 u_int intr;
302
303 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
304 if (ih == &sc->sc_dispatch[intr]) {
305 uint32_t r;
306
307 /* disable this interrupt in the usb interface */
308 r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
309 RMIXL_USB_INTERRUPT_ENABLE);
310 r &= 1 << intr;
311 bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
312 RMIXL_USB_INTERRUPT_ENABLE, r);
313
314 /* free the dispatch slot */
315 sc->sc_dispatch[intr].func = NULL;
316 sc->sc_dispatch[intr].arg = NULL;
317
318 break;
319 }
320 }
321 }
322
323 void *
324 rmixl_iobus_intr_establish(void *uh, u_int intr, int (func)(void *), void *arg)
325 {
326 rmixl_iobus_softc_t *sc = uh;
327 uint32_t r;
328 void *ih = NULL;
329 int s;
330
331 s = splusb();
332
333 if (intr > RMIXL_UB_INTERRUPT_MAX) {
334 aprint_error_dev(sc->sc_dev, "invalid intr %d\n", intr);
335 goto out;
336 }
337
338 if (sc->sc_dispatch[intr].func != NULL) {
339 aprint_error_dev(sc->sc_dev, "intr %dq busy\n", intr);
340 goto out;
341 }
342
343 sc->sc_dispatch[intr].func = func;
344 sc->sc_dispatch[intr].arg = arg;
345 ih = &sc->sc_dispatch[intr];
346
347 /* enable this interrupt in the usb interface */
348 r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
349 RMIXL_USB_INTERRUPT_ENABLE);
350 r |= 1 << intr;
351 bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
352 RMIXL_USB_INTERRUPT_ENABLE, r);
353
354 out:
355 splx(s);
356 return ih;
357 }
358
359 static int
360 rmixl_iobus_intr(void *arg)
361 {
362 rmixl_iobus_softc_t *sc = arg;
363 uint32_t r;
364 int intr;
365 int rv = 0;
366
367 r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
368 RMIXL_USB_INTERRUPT_STATUS);
369 if (r != 0) {
370 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
371 uint32_t bit = 1 << intr;
372 if ((r & bit) != 0) {
373 int (*f)(void *) = sc->sc_dispatch[intr].func;
374 void *a = sc->sc_dispatch[intr].arg;
375 if (f != NULL) {
376 (void)(*f)(a);
377 sc->sc_dispatch[intr].count.ev_count++;
378 rv = 1;
379 }
380 }
381 }
382 }
383
384 return rv;
385 }
386
387 #endif /* NOTYET */
388