Home | History | Annotate | Line # | Download | only in dev
uni-n.c revision 1.10
      1  1.10   thorpej /*	$NetBSD: uni-n.c,v 1.10 2021/04/24 23:36:41 thorpej Exp $	*/
      2   1.1  macallan 
      3   1.1  macallan /*-
      4   1.1  macallan  * Copyright (C) 2005 Michael Lorenz.
      5   1.1  macallan  *
      6   1.1  macallan  * Redistribution and use in source and binary forms, with or without
      7   1.1  macallan  * modification, are permitted provided that the following conditions
      8   1.1  macallan  * are met:
      9   1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     10   1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     11   1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  macallan  *    documentation and/or other materials provided with the distribution.
     14   1.1  macallan  * 3. The name of the author may not be used to endorse or promote products
     15   1.1  macallan  *    derived from this software without specific prior written permission.
     16   1.1  macallan  *
     17   1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.1  macallan  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1  macallan  */
     28   1.1  macallan 
     29   1.1  macallan /*
     30   1.1  macallan  * a driver to match the uni_n node in OF's device tree and attach its children
     31   1.1  macallan  */
     32   1.1  macallan 
     33   1.1  macallan #include <sys/cdefs.h>
     34  1.10   thorpej __KERNEL_RCSID(0, "$NetBSD: uni-n.c,v 1.10 2021/04/24 23:36:41 thorpej Exp $");
     35   1.1  macallan 
     36   1.1  macallan #include <sys/param.h>
     37   1.1  macallan #include <sys/systm.h>
     38   1.1  macallan #include <sys/kernel.h>
     39   1.1  macallan #include <sys/device.h>
     40   1.1  macallan 
     41   1.1  macallan #include <dev/ofw/openfirm.h>
     42   1.7  macallan #include <dev/ofw/ofw_pci.h>
     43   1.1  macallan 
     44   1.1  macallan #include <machine/autoconf.h>
     45   1.1  macallan 
     46   1.9  macallan #include "fcu.h"
     47   1.9  macallan 
     48   1.5      matt static void uni_n_attach(device_t, device_t, void *);
     49   1.5      matt static int uni_n_match(device_t, cfdata_t, void *);
     50   1.1  macallan static int uni_n_print(void *, const char *);
     51   1.1  macallan 
     52   1.1  macallan struct uni_n_softc {
     53   1.6  macallan 	device_t sc_dev;
     54   1.7  macallan 	struct powerpc_bus_space sc_memt;
     55   1.1  macallan 	int sc_node;
     56   1.1  macallan };
     57   1.1  macallan 
     58   1.6  macallan CFATTACH_DECL_NEW(uni_n, sizeof(struct uni_n_softc),
     59   1.1  macallan     uni_n_match, uni_n_attach, NULL, NULL);
     60   1.1  macallan 
     61   1.9  macallan #if NFCU > 0
     62   1.9  macallan /* storage for CPUID SEEPROM contents found on some G5 */
     63   1.9  macallan static uint8_t eeprom[2][160];
     64   1.9  macallan #endif
     65   1.9  macallan 
     66   1.1  macallan int
     67   1.5      matt uni_n_match(device_t parent, cfdata_t cf, void *aux)
     68   1.1  macallan {
     69   1.1  macallan 	struct confargs *ca = aux;
     70   1.1  macallan 	char compat[32];
     71   1.7  macallan 	if ((strcmp(ca->ca_name, "uni-n") != 0) &&
     72   1.8  macallan 	    (strcmp(ca->ca_name, "u4") != 0) &&
     73   1.8  macallan 	    (strcmp(ca->ca_name, "u3") != 0))
     74   1.1  macallan 		return 0;
     75   1.1  macallan 
     76   1.1  macallan 	memset(compat, 0, sizeof(compat));
     77   1.7  macallan #if 0
     78   1.1  macallan 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     79   1.1  macallan 	if (strcmp(compat, "uni-north") != 0)
     80   1.1  macallan 		return 0;
     81   1.7  macallan #endif
     82   1.1  macallan 	return 1;
     83   1.1  macallan }
     84   1.1  macallan 
     85   1.1  macallan /*
     86   1.1  macallan  * Attach all the sub-devices we can find
     87   1.1  macallan  */
     88   1.1  macallan void
     89   1.5      matt uni_n_attach(device_t parent, device_t self, void *aux)
     90   1.1  macallan {
     91   1.5      matt 	struct uni_n_softc *sc = device_private(self);
     92   1.1  macallan 	struct confargs *our_ca = aux;
     93   1.1  macallan 	struct confargs ca;
     94   1.1  macallan 	int node, child, namelen;
     95   1.9  macallan #if NFCU > 0
     96   1.9  macallan 	int cpuid;
     97   1.9  macallan #endif
     98   1.1  macallan 	u_int reg[20];
     99   1.1  macallan 	int intr[6];
    100   1.1  macallan 	char name[32];
    101   1.1  macallan 
    102   1.6  macallan 	sc->sc_dev = self;
    103   1.7  macallan 	node = our_ca->ca_node;
    104   1.1  macallan 	sc->sc_node = node;
    105   1.9  macallan 	printf(" address 0x%08x\n",
    106   1.9  macallan 	    our_ca->ca_reg[our_ca->ca_nreg > 8 ? 1 : 0]);
    107   1.9  macallan 
    108   1.9  macallan #if NFCU > 0
    109   1.9  macallan 	/*
    110   1.9  macallan 	 * zero out eeprom blocks, then see if we have valid data
    111   1.9  macallan 	 * doing this here because the EEPROMs are dangling from out i2c bus
    112   1.9  macallan 	 * but we can get all the data just from looking at the properties
    113   1.9  macallan 	 */
    114   1.9  macallan 	memset(eeprom, 0, sizeof(eeprom));
    115   1.9  macallan 	cpuid = OF_finddevice("/u3/i2c/cpuid@a0");
    116   1.9  macallan 	OF_getprop(cpuid, "cpuid", eeprom[0], sizeof(eeprom[0]));
    117   1.9  macallan 	if (eeprom[0][1] != 0)
    118   1.9  macallan 		aprint_normal_dev(self, "found EEPROM data for CPU 0\n");
    119   1.9  macallan 	cpuid = OF_finddevice("/u3/i2c/cpuid@a2");
    120   1.9  macallan 	OF_getprop(cpuid, "cpuid", eeprom[1], sizeof(eeprom[1]));
    121   1.9  macallan 	if (eeprom[1][1] != 0)
    122   1.9  macallan 		aprint_normal_dev(self, "found EEPROM data for CPU 1\n");
    123   1.9  macallan #endif
    124   1.7  macallan 
    125   1.7  macallan 	memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
    126   1.7  macallan 	sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
    127   1.7  macallan 	if (ofwoea_map_space(RANGE_TYPE_MACIO, RANGE_MEM, node, &sc->sc_memt,
    128   1.7  macallan 	    "uni-n mem-space") != 0) {
    129   1.7  macallan 		panic("Can't init uni-n mem tag");
    130   1.7  macallan 	}
    131   1.7  macallan 
    132   1.1  macallan 	for (child = OF_child(node); child; child = OF_peer(child)) {
    133   1.1  macallan 		namelen = OF_getprop(child, "name", name, sizeof(name));
    134   1.1  macallan 		if (namelen < 0)
    135   1.1  macallan 			continue;
    136   1.1  macallan 		if (namelen >= sizeof(name))
    137   1.1  macallan 			continue;
    138   1.1  macallan 
    139   1.1  macallan 		name[namelen] = 0;
    140   1.1  macallan 		ca.ca_name = name;
    141   1.1  macallan 		ca.ca_node = child;
    142   1.7  macallan 		ca.ca_tag = &sc->sc_memt;
    143   1.1  macallan 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    144   1.1  macallan 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    145   1.1  macallan 				sizeof(intr));
    146   1.1  macallan 		if (ca.ca_nintr == -1)
    147   1.1  macallan 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    148   1.1  macallan 					sizeof(intr));
    149   1.1  macallan 
    150   1.1  macallan 		ca.ca_reg = reg;
    151   1.1  macallan 		ca.ca_intr = intr;
    152  1.10   thorpej 		config_found(self, &ca, uni_n_print,
    153  1.10   thorpej 		    CFARG_DEVHANDLE, devhandle_from_of(child),
    154  1.10   thorpej 		    CFARG_EOL);
    155   1.1  macallan 	}
    156   1.1  macallan }
    157   1.1  macallan 
    158   1.1  macallan int
    159   1.3       dsl uni_n_print(void *aux, const char *uni_n)
    160   1.1  macallan {
    161   1.1  macallan 	struct confargs *ca = aux;
    162   1.1  macallan 	if (uni_n)
    163   1.1  macallan 		aprint_normal("%s at %s", ca->ca_name, uni_n);
    164   1.1  macallan 
    165   1.1  macallan 	if (ca->ca_nreg > 0)
    166   1.1  macallan 		aprint_normal(" address 0x%x", ca->ca_reg[0]);
    167   1.1  macallan 
    168   1.1  macallan 	return UNCONF;
    169   1.1  macallan }
    170   1.9  macallan 
    171   1.9  macallan #if NFCU > 0
    172   1.9  macallan int
    173   1.9  macallan get_cpuid(int cpu, uint8_t *buf)
    174   1.9  macallan {
    175   1.9  macallan 	if ((cpu < 0) || (cpu > 1)) return -1;
    176   1.9  macallan 	if (eeprom[cpu][1] == 0) return 0;
    177   1.9  macallan 	memcpy(buf, eeprom[cpu], sizeof(eeprom[cpu]));
    178   1.9  macallan 	return sizeof(eeprom[cpu]);
    179   1.9  macallan }
    180   1.9  macallan #endif
    181