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