xref: /netbsd-src/sys/arch/powerpc/booke/dev/pq3obio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: pq3obio.c,v 1.7 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 #define	LBC_PRIVATE
38b8ea2c8cSmatt 
39b8ea2c8cSmatt #include <sys/cdefs.h>
40*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: pq3obio.c,v 1.7 2021/08/07 16:19:02 thorpej Exp $");
41b8ea2c8cSmatt 
4216031f7dSrin #include "locators.h"
43b8ea2c8cSmatt 
44b8ea2c8cSmatt #include <sys/param.h>
45b8ea2c8cSmatt #include <sys/device.h>
46b8ea2c8cSmatt #include <sys/kmem.h>
47b8ea2c8cSmatt #include <sys/bus.h>
48b8ea2c8cSmatt #include <sys/extent.h>
49b8ea2c8cSmatt 
50b8ea2c8cSmatt #include <powerpc/booke/cpuvar.h>
51b8ea2c8cSmatt #include <powerpc/booke/e500var.h>
52b8ea2c8cSmatt #include <powerpc/booke/e500reg.h>
53d87f482eSmatt #include <powerpc/booke/obiovar.h>
54b8ea2c8cSmatt 
557ffd9df1Sjdolecek #define OBIO_PORTS	8
567ffd9df1Sjdolecek 
57b8ea2c8cSmatt static int pq3obio_match(device_t, cfdata_t, void *);
58b8ea2c8cSmatt static void pq3obio_attach(device_t, device_t, void *);
59b8ea2c8cSmatt 
60b8ea2c8cSmatt CFATTACH_DECL_NEW(pq3obio, sizeof(struct pq3obio_softc),
61b8ea2c8cSmatt    pq3obio_match, pq3obio_attach, NULL, NULL);
62b8ea2c8cSmatt 
63b8ea2c8cSmatt static int
pq3obio_match(device_t parent,cfdata_t cf,void * aux)64b8ea2c8cSmatt pq3obio_match(device_t parent, cfdata_t cf, void *aux)
65b8ea2c8cSmatt {
66b8ea2c8cSmatt 
67b8ea2c8cSmatt 	if (!e500_cpunode_submatch(parent, cf, "lbc", aux))
68b8ea2c8cSmatt 		return 0;
69b8ea2c8cSmatt 
70b8ea2c8cSmatt 	return 1;
71b8ea2c8cSmatt }
72b8ea2c8cSmatt 
73b8ea2c8cSmatt static int
pq3obio_print(void * aux,const char * pnp)74b8ea2c8cSmatt pq3obio_print(void *aux, const char *pnp)
75b8ea2c8cSmatt {
76b8ea2c8cSmatt 	struct generic_attach_args * const ga = aux;
77b8ea2c8cSmatt 
78b8ea2c8cSmatt 	if (pnp)
79b8ea2c8cSmatt 		aprint_normal(" at %s", pnp);
80b8ea2c8cSmatt 
81b8ea2c8cSmatt 	if (ga->ga_cs != OBIOCF_CS_DEFAULT)
82b8ea2c8cSmatt 		aprint_normal(" cs %d", ga->ga_cs);
83b8ea2c8cSmatt 
84b8ea2c8cSmatt 	if (ga->ga_addr != OBIOCF_ADDR_DEFAULT) {
85b8ea2c8cSmatt 		aprint_normal(" addr %#x", ga->ga_addr);
86b8ea2c8cSmatt 		if (ga->ga_size != OBIOCF_SIZE_DEFAULT)
87b8ea2c8cSmatt 			aprint_normal(" size %#x", ga->ga_size);
88b8ea2c8cSmatt 	}
89b8ea2c8cSmatt 	if (ga->ga_irq != OBIOCF_IRQ_DEFAULT)
90b8ea2c8cSmatt 		aprint_normal(" irq %d", ga->ga_irq);
91b8ea2c8cSmatt 
92b8ea2c8cSmatt 	return UNCONF;
93b8ea2c8cSmatt }
94b8ea2c8cSmatt 
95b8ea2c8cSmatt static int
pq3obio_search(device_t parent,cfdata_t cf,const int * slocs,void * aux)96b8ea2c8cSmatt pq3obio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
97b8ea2c8cSmatt {
98b8ea2c8cSmatt 	struct pq3obio_softc * const sc = device_private(parent);
99b8ea2c8cSmatt 	struct generic_attach_args ga;
100b8ea2c8cSmatt 	bool tryagain;
101b8ea2c8cSmatt 
102b8ea2c8cSmatt 	do {
103b8ea2c8cSmatt 		const struct pq3lbc_softc *lbc;
104b8ea2c8cSmatt 		ga.ga_name = "obio";
105b8ea2c8cSmatt 		ga.ga_addr = cf->cf_loc[OBIOCF_ADDR];
106b8ea2c8cSmatt 		ga.ga_size = cf->cf_loc[OBIOCF_SIZE];
107b8ea2c8cSmatt 		ga.ga_irq = cf->cf_loc[OBIOCF_IRQ];
108b8ea2c8cSmatt 		ga.ga_cs = cf->cf_loc[OBIOCF_CS];
109b8ea2c8cSmatt 		ga.ga_bst = &sc->sc_obio_bst;
110b8ea2c8cSmatt 
111b8ea2c8cSmatt 		if (ga.ga_cs != OBIOCF_CS_DEFAULT) {
112b8ea2c8cSmatt 			lbc = &sc->sc_lbcs[ga.ga_cs];
113b8ea2c8cSmatt 			if ((u_int) ga.ga_cs >= __arraycount(sc->sc_lbcs))
114b8ea2c8cSmatt 				return 0;
115b8ea2c8cSmatt 			if (ga.ga_addr != OBIOCF_ADDR_DEFAULT) {
116b8ea2c8cSmatt 				if (ga.ga_addr < lbc->lbc_base
117b8ea2c8cSmatt 				    || ga.ga_addr > lbc->lbc_limit)
118b8ea2c8cSmatt 					return 0;
119b8ea2c8cSmatt 			} else {
120b8ea2c8cSmatt 				ga.ga_addr = lbc->lbc_base;
121b8ea2c8cSmatt 			}
122b8ea2c8cSmatt 		} else {
123b8ea2c8cSmatt 			u_int cs;
124b8ea2c8cSmatt 			if (ga.ga_addr == OBIOCF_ADDR_DEFAULT)
125b8ea2c8cSmatt 				return 0;
126b8ea2c8cSmatt 			for (cs = 0, lbc = sc->sc_lbcs;
127b8ea2c8cSmatt 			     cs < __arraycount(sc->sc_lbcs);
128b8ea2c8cSmatt 			     cs++, lbc++) {
129b8ea2c8cSmatt 				if (ga.ga_addr >= lbc->lbc_base
130b8ea2c8cSmatt 				    && ga.ga_addr <= lbc->lbc_limit) {
131b8ea2c8cSmatt 					ga.ga_cs = cs;
132b8ea2c8cSmatt 					break;
133b8ea2c8cSmatt 				}
134b8ea2c8cSmatt 			}
135b8ea2c8cSmatt 			if (ga.ga_cs == OBIOCF_CS_DEFAULT)
136b8ea2c8cSmatt 				return 0;
137b8ea2c8cSmatt 		}
138b8ea2c8cSmatt 
139b8ea2c8cSmatt 		if (ga.ga_size != OBIOCF_SIZE_DEFAULT) {
140b8ea2c8cSmatt 			if (ga.ga_size >= lbc->lbc_limit - ga.ga_addr)
141b8ea2c8cSmatt 				return 0;
142b8ea2c8cSmatt 		} else {
143b8ea2c8cSmatt 			ga.ga_size = lbc->lbc_limit + 1 - lbc->lbc_base;
144b8ea2c8cSmatt 		}
145b8ea2c8cSmatt 
146b8ea2c8cSmatt 		tryagain = false;
1472685996bSthorpej 		if (config_probe(parent, cf, &ga)) {
148b8ea2c8cSmatt 			int floc[OBIOCF_NLOCS] = {
149b8ea2c8cSmatt 			    [OBIOCF_ADDR] = ga.ga_addr,
150b8ea2c8cSmatt 			    [OBIOCF_SIZE] = ga.ga_size,
151b8ea2c8cSmatt 			    [OBIOCF_IRQ] = ga.ga_irq,
152b8ea2c8cSmatt 			    [OBIOCF_CS] = ga.ga_cs,
153b8ea2c8cSmatt 			};
1542685996bSthorpej 			config_attach(parent, cf, &ga, pq3obio_print,
155*c7fb772bSthorpej 			    CFARGS(.locators = floc));
156b8ea2c8cSmatt 			tryagain = (cf->cf_fstate == FSTATE_STAR);
157b8ea2c8cSmatt 		}
158b8ea2c8cSmatt 	} while (tryagain);
159b8ea2c8cSmatt 
160b8ea2c8cSmatt 	return (0);
161b8ea2c8cSmatt }
162b8ea2c8cSmatt 
163b8ea2c8cSmatt static const char br_msel_strings[8][6] = {
164b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_GPCM,BR_MSEL)] = "GPCM",
165b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_FCM,BR_MSEL)] = "FCM",
166b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_SDRAM,BR_MSEL)] = "SDRAM",
167b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_UPMA,BR_MSEL)] = "UPMA",
168b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_UPMB,BR_MSEL)] = "UPMB",
169b8ea2c8cSmatt     [__SHIFTOUT(BR_MSEL_UPMC,BR_MSEL)] = "UPMC",
170b8ea2c8cSmatt };
171b8ea2c8cSmatt 
172b8ea2c8cSmatt static void
pq3obio_attach(device_t parent,device_t self,void * aux)173b8ea2c8cSmatt pq3obio_attach(device_t parent, device_t self, void *aux)
174b8ea2c8cSmatt {
175b8ea2c8cSmatt 	struct cpunode_softc * const psc = device_private(parent);
176b8ea2c8cSmatt 	struct pq3obio_softc * const sc = device_private(self);
177b8ea2c8cSmatt 	struct cpunode_attach_args * const cna = aux;
178b8ea2c8cSmatt 	struct cpunode_locators * const cnl = &cna->cna_locs;
179b8ea2c8cSmatt 	struct powerpc_bus_space * const t = &sc->sc_obio_bst;
180b8ea2c8cSmatt 	u_int found = 0;
181b8ea2c8cSmatt 	int error;
182b8ea2c8cSmatt 
183b8ea2c8cSmatt 	psc->sc_children |= cna->cna_childmask;
184b8ea2c8cSmatt 	sc->sc_dev = self;
185b8ea2c8cSmatt 	sc->sc_bst = cna->cna_memt;
186b8ea2c8cSmatt 
187b8ea2c8cSmatt 	error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
188b8ea2c8cSmatt 		&sc->sc_bsh);
189b8ea2c8cSmatt 	if (error) {
190b8ea2c8cSmatt 		aprint_error("failed to map registers: %d\n", error);
191b8ea2c8cSmatt 		return;
192b8ea2c8cSmatt 	}
193b8ea2c8cSmatt 
1947ffd9df1Sjdolecek 	for (u_int i = 0; i < OBIO_PORTS; i++) {
195b8ea2c8cSmatt 		struct pq3lbc_softc * const lbc = &sc->sc_lbcs[i];
196b8ea2c8cSmatt 		uint32_t br = bus_space_read_4(sc->sc_bst, sc->sc_bsh, BRn(i));
197b8ea2c8cSmatt 		if (br & BR_V) {
198b8ea2c8cSmatt 			found++;
199b8ea2c8cSmatt 			lbc->lbc_br = br;
200b8ea2c8cSmatt 			lbc->lbc_or = bus_space_read_4(sc->sc_bst,
201b8ea2c8cSmatt 			    sc->sc_bsh, ORn(i));
202b8ea2c8cSmatt 			lbc->lbc_base = lbc->lbc_br & BR_BA & lbc->lbc_or;
203b8ea2c8cSmatt 			lbc->lbc_limit = lbc->lbc_base + ~(lbc->lbc_or & OR_AM);
204b8ea2c8cSmatt 		}
205b8ea2c8cSmatt 	}
206b8ea2c8cSmatt 
207b8ea2c8cSmatt 	aprint_normal(": %u of 8 ports enabled\n", found);
208b8ea2c8cSmatt 	if (found == 0)
209b8ea2c8cSmatt 		return;
210b8ea2c8cSmatt 
211b8ea2c8cSmatt 	t->pbs_base = 0xffffffff;
212b8ea2c8cSmatt 	t->pbs_limit = 0;
213b8ea2c8cSmatt 	t->pbs_flags = _BUS_SPACE_BIG_ENDIAN;
214b8ea2c8cSmatt 
2157ffd9df1Sjdolecek 	u_int sorted[OBIO_PORTS];
216b8ea2c8cSmatt 	u_int nsorted = 0;
217b8ea2c8cSmatt 
2187ffd9df1Sjdolecek 	for (u_int i = 0; i < OBIO_PORTS; i++) {
219b8ea2c8cSmatt 		struct pq3lbc_softc * const lbc = &sc->sc_lbcs[i];
220b8ea2c8cSmatt 		if ((lbc->lbc_br & BR_V) == 0)
221b8ea2c8cSmatt 			continue;
222b8ea2c8cSmatt 
223b8ea2c8cSmatt 		u_int j;
224b8ea2c8cSmatt 		for (j = 0; j < nsorted; j++) {
225b8ea2c8cSmatt 			if (lbc->lbc_base < sc->sc_lbcs[sorted[j]].lbc_base)
226b8ea2c8cSmatt 				break;
227b8ea2c8cSmatt 		}
228b8ea2c8cSmatt 		for (u_int k = ++nsorted; k > j; k--) {
229b8ea2c8cSmatt 			sorted[k] = sorted[k - 1];
230b8ea2c8cSmatt 		}
231b8ea2c8cSmatt 		sorted[j] = i;
232b8ea2c8cSmatt 
233b8ea2c8cSmatt 		if (lbc->lbc_base < t->pbs_base)
234b8ea2c8cSmatt 			t->pbs_base = lbc->lbc_base;
235b8ea2c8cSmatt 		if (lbc->lbc_limit > t->pbs_limit)
236b8ea2c8cSmatt 			t->pbs_limit = lbc->lbc_limit;
237b8ea2c8cSmatt #if 0
238b8ea2c8cSmatt 		aprint_normal_dev(sc->sc_dev,
239b8ea2c8cSmatt 		    "cs%u: br=%#x or=%#x", i, lbc->lbc_br, lbc->lbc_or);
240b8ea2c8cSmatt #endif
241b8ea2c8cSmatt 		u_int n = ffs(~(lbc->lbc_or & OR_AM) + 1) - 1;
242b8ea2c8cSmatt 		aprint_normal_dev(sc->sc_dev,
243b8ea2c8cSmatt 		    "cs%u: %u%cB %u-bit %s region at %#x\n",
244b8ea2c8cSmatt 		    i,
245b8ea2c8cSmatt 		    1 << (n % 10), " KMGTPE"[n / 10],
246b8ea2c8cSmatt 		    1 << (__SHIFTOUT(lbc->lbc_br, BR_PS) + 2),
247b8ea2c8cSmatt 		    br_msel_strings[__SHIFTOUT(lbc->lbc_br,BR_MSEL)],
248b8ea2c8cSmatt 		    lbc->lbc_base);
249b8ea2c8cSmatt 	}
250b8ea2c8cSmatt 
251b8ea2c8cSmatt 	error = bus_space_init(t, device_xname(sc->sc_dev), NULL, 0);
252b8ea2c8cSmatt 	if (error) {
253b8ea2c8cSmatt 		aprint_error_dev(sc->sc_dev,
254b8ea2c8cSmatt 		    "failed to initialize obio bus space: %d\n", error);
255b8ea2c8cSmatt 		return;
256b8ea2c8cSmatt 	}
257b8ea2c8cSmatt 
258b8ea2c8cSmatt 	/*
259b8ea2c8cSmatt 	 * We've created a bus space which covers all the chip selects
260b8ea2c8cSmatt 	 * but there might be gaps inbetween them.  We need to make sure
261b8ea2c8cSmatt 	 * bus_space_map will fail for those.  To do that, we reserve
262b8ea2c8cSmatt 	 * the gaps between the chip-selects.
263b8ea2c8cSmatt 	 */
264b8ea2c8cSmatt 	for (u_int i = 1; i < found; i++) {
265b8ea2c8cSmatt 		struct pq3lbc_softc * const lbc_lo = &sc->sc_lbcs[sorted[i-1]];
266b8ea2c8cSmatt 		struct pq3lbc_softc * const lbc_hi = &sc->sc_lbcs[sorted[i]];
267b8ea2c8cSmatt 		if (lbc_lo->lbc_limit + 1 == lbc_hi->lbc_base)
268b8ea2c8cSmatt 			continue;
269b8ea2c8cSmatt 		error = extent_alloc_region(t->pbs_extent,
270b8ea2c8cSmatt 		    lbc_lo->lbc_limit + 1,
271b8ea2c8cSmatt 		    lbc_hi->lbc_base - (lbc_lo->lbc_limit + 1), 0);
272b8ea2c8cSmatt 		if (error) {
273b8ea2c8cSmatt 			aprint_error_dev(sc->sc_dev,
274b8ea2c8cSmatt 			    "failed to reserve %#x..%#x: %d\n",
275b8ea2c8cSmatt 			    lbc_lo->lbc_limit + 1,
276b8ea2c8cSmatt 			    lbc_hi->lbc_base - 1,
277b8ea2c8cSmatt 			    error);
278b8ea2c8cSmatt 			return;
279b8ea2c8cSmatt 		}
280b8ea2c8cSmatt 	}
281b8ea2c8cSmatt 
282b8ea2c8cSmatt 	/*
283b8ea2c8cSmatt 	 * Now we can do the device configuration to find what might
284b8ea2c8cSmatt 	 * be using obio.
285b8ea2c8cSmatt 	 */
286b8ea2c8cSmatt 	int locs[OBIOCF_NLOCS];
287b8ea2c8cSmatt 	locs[OBIOCF_ADDR] = OBIOCF_ADDR_DEFAULT;
288b8ea2c8cSmatt 	locs[OBIOCF_SIZE] = OBIOCF_SIZE_DEFAULT;
289b8ea2c8cSmatt 	locs[OBIOCF_CS] = OBIOCF_CS_DEFAULT;
2902685996bSthorpej 	config_search(self, NULL,
291*c7fb772bSthorpej 	    CFARGS(.search = pq3obio_search,
292*c7fb772bSthorpej 		   .locators = locs));
293b8ea2c8cSmatt }
294