Home | History | Annotate | Line # | Download | only in ralink
ralink_mainbus.c revision 1.5.30.5
      1  1.5.30.5   thorpej /*	$NetBSD: ralink_mainbus.c,v 1.5.30.5 2021/04/05 00:48:50 thorpej Exp $	*/
      2       1.2      matt /*-
      3       1.2      matt  * Copyright (c) 2011 CradlePoint Technology, Inc.
      4       1.2      matt  * All rights reserved.
      5       1.2      matt  *
      6       1.2      matt  *
      7       1.2      matt  * Redistribution and use in source and binary forms, with or without
      8       1.2      matt  * modification, are permitted provided that the following conditions
      9       1.2      matt  * are met:
     10       1.2      matt  * 1. Redistributions of source code must retain the above copyright
     11       1.2      matt  *    notice, this list of conditions and the following disclaimer.
     12       1.2      matt  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.2      matt  *    notice, this list of conditions and the following disclaimer in the
     14       1.2      matt  *    documentation and/or other materials provided with the distribution.
     15       1.2      matt  *
     16       1.2      matt  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
     17       1.2      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.2      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.2      matt  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     20       1.2      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.2      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.2      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.2      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.2      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.2      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.2      matt  * POSSIBILITY OF SUCH DAMAGE.
     27       1.2      matt  */
     28       1.2      matt 
     29       1.2      matt #include <sys/cdefs.h>
     30  1.5.30.5   thorpej __KERNEL_RCSID(0, "$NetBSD: ralink_mainbus.c,v 1.5.30.5 2021/04/05 00:48:50 thorpej Exp $");
     31       1.2      matt 
     32       1.5       ryo #include "locators.h"
     33       1.2      matt #include <sys/param.h>
     34       1.2      matt #include <sys/systm.h>
     35       1.2      matt #include <sys/device.h>
     36       1.2      matt 
     37       1.2      matt #include <mips/cache.h>
     38       1.2      matt #include <mips/cpuregs.h>
     39       1.2      matt 
     40       1.2      matt #include <mips/ralink/ralink_reg.h>
     41       1.2      matt #include <mips/ralink/ralink_var.h>
     42       1.2      matt 
     43       1.2      matt typedef struct mainbus_softc {
     44       1.2      matt 	device_t	sc_dev;
     45       1.2      matt 	bus_space_tag_t	sc_memt;
     46       1.2      matt 	bus_dma_tag_t	sc_dmat;
     47       1.2      matt } mainbus_softc_t;
     48       1.2      matt 
     49       1.2      matt static int  mainbus_match(device_t, cfdata_t, void *);
     50       1.2      matt static void mainbus_attach(device_t, device_t, void *);
     51       1.2      matt static int  mainbus_search(device_t, cfdata_t, const int *, void *);
     52       1.2      matt static int  mainbus_print(void *, const char *);
     53       1.2      matt static int  mainbus_find(device_t, cfdata_t, const int *, void *);
     54       1.2      matt static void mainbus_attach_critical(struct mainbus_softc *);
     55       1.2      matt 
     56       1.2      matt 
     57       1.2      matt CFATTACH_DECL_NEW(mainbus, sizeof(struct mainbus_softc),
     58       1.2      matt 	mainbus_match, mainbus_attach, NULL, NULL);
     59       1.2      matt 
     60       1.2      matt static bool mainbus_found;
     61       1.2      matt 
     62       1.2      matt /*
     63       1.2      matt  * critical devices, if found, will be attached in the order listed here
     64       1.2      matt  */
     65       1.2      matt static const struct {
     66       1.2      matt 	const char *name;
     67       1.2      matt 	bool required;
     68       1.2      matt } critical_devs[] = {
     69       1.2      matt 	{ .name = "cpu0",	.required = true  },
     70       1.2      matt 	{ .name = "rwdog0",	.required = false },
     71       1.2      matt 	{ .name = "rgpio0",	.required = true  },
     72       1.2      matt 	{ .name = "ri2c0",	.required = false },
     73       1.2      matt 	{ .name = "com0",	.required = false },
     74       1.2      matt };
     75       1.2      matt 
     76       1.2      matt 
     77       1.2      matt static int
     78       1.2      matt mainbus_match(device_t parent, cfdata_t match, void *aux)
     79       1.2      matt {
     80       1.2      matt 	if (mainbus_found)
     81       1.2      matt 		return 0;
     82       1.2      matt 	return 1;
     83       1.2      matt }
     84       1.2      matt 
     85       1.2      matt static void
     86       1.2      matt mainbus_attach(device_t parent, device_t self, void *aux)
     87       1.2      matt {
     88       1.2      matt 	mainbus_softc_t * const sc = device_private(self);
     89       1.2      matt 	struct mainbus_attach_args ma;
     90       1.4      matt 	union {
     91       1.4      matt 		char buf[9];
     92       1.4      matt 		uint32_t id[2];
     93       1.4      matt 	} xid;
     94       1.2      matt 
     95       1.2      matt 	mainbus_found = true;
     96       1.2      matt 
     97       1.4      matt 	sc->sc_dev = self;
     98       1.4      matt 	sc->sc_memt = &ra_bus_memt;
     99       1.4      matt 	sc->sc_dmat = &ra_bus_dmat;
    100       1.4      matt 
    101       1.4      matt 	xid.id[0] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID0);
    102       1.4      matt 	xid.id[1] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID1);
    103       1.4      matt 	xid.buf[8] = '\0';
    104       1.4      matt 	if (xid.buf[6] == ' ') {
    105       1.4      matt 		xid.buf[6] = '\0';
    106       1.4      matt 	} else if (xid.buf[7] == ' ') {
    107       1.4      matt 		xid.buf[7] = '\0';
    108       1.4      matt 	}
    109       1.4      matt 	const char * const manuf = xid.buf[0] == 'M' ? "Mediatek" : "Ralink";
    110       1.4      matt 
    111       1.4      matt 	aprint_naive(": %s %s System Bus\n", manuf, xid.buf);
    112       1.4      matt 	aprint_normal(": %s %s System Bus\n", manuf, xid.buf);
    113       1.2      matt 
    114       1.2      matt 	ma.ma_name = NULL;
    115       1.4      matt 	ma.ma_memt = sc->sc_memt;
    116       1.4      matt 	ma.ma_dmat = sc->sc_dmat;
    117       1.2      matt 
    118       1.2      matt 	/* attach critical devices */
    119       1.2      matt 	mainbus_attach_critical(sc);
    120       1.2      matt 
    121       1.2      matt 	/* attach other devices */
    122  1.5.30.1   thorpej 	config_search(self, &ma,
    123  1.5.30.4   thorpej 	    CFARG_SEARCH, mainbus_search,
    124  1.5.30.1   thorpej 	    CFARG_EOL);
    125       1.2      matt }
    126       1.2      matt 
    127       1.2      matt static int
    128       1.2      matt mainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    129       1.2      matt {
    130       1.5       ryo 	struct mainbus_attach_args *ma;
    131       1.5       ryo 
    132       1.5       ryo 	ma = aux;
    133       1.5       ryo 	ma->ma_addr = cf->cf_loc[MAINBUSCF_ADDR];
    134       1.5       ryo 
    135  1.5.30.5   thorpej 	if (config_probe(parent, cf, aux) > 0)
    136  1.5.30.3   thorpej 		config_attach(parent, cf, aux, mainbus_print, CFARG_EOL);
    137       1.2      matt 	else
    138       1.2      matt 		mainbus_print(aux, cf->cf_name);
    139       1.2      matt 	return 0;
    140       1.2      matt }
    141       1.2      matt 
    142       1.2      matt int
    143       1.2      matt mainbus_print(void *aux, const char *pnp)
    144       1.2      matt {
    145       1.5       ryo 	struct mainbus_attach_args *ma;
    146       1.5       ryo 
    147       1.2      matt 	if (pnp)
    148       1.2      matt 		aprint_normal("%s unconfigured\n", pnp);
    149       1.2      matt 
    150       1.5       ryo 	ma = aux;
    151       1.5       ryo 	if (ma->ma_addr != MAINBUSCF_ADDR_DEFAULT)
    152       1.5       ryo 		aprint_normal(" addr 0x%llx", ma->ma_addr);
    153       1.5       ryo 
    154       1.2      matt 	return UNCONF;
    155       1.2      matt }
    156       1.2      matt 
    157       1.2      matt static int
    158       1.2      matt mainbus_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    159       1.2      matt {
    160       1.2      matt 	struct mainbus_attach_args * const ma = aux;
    161       1.2      matt 	char devname[16];
    162       1.2      matt 
    163       1.3  christos 	snprintf(devname, sizeof(devname), "%s%d", cf->cf_name, cf->cf_unit);
    164       1.2      matt 	if (strcmp(ma->ma_name, devname) != 0)
    165       1.2      matt 		return 0;
    166       1.2      matt 
    167       1.2      matt 	return config_match(parent, cf, ma);
    168       1.2      matt }
    169       1.2      matt 
    170       1.2      matt static void
    171       1.2      matt mainbus_attach_critical(struct mainbus_softc *sc)
    172       1.2      matt {
    173       1.2      matt 	struct mainbus_attach_args ma;
    174       1.2      matt 	cfdata_t cf;
    175       1.2      matt 	size_t i;
    176       1.2      matt 
    177       1.2      matt 	for (i = 0; i < __arraycount(critical_devs); i++) {
    178       1.2      matt 		ma.ma_name = critical_devs[i].name;
    179       1.2      matt 		ma.ma_memt = sc->sc_memt;
    180       1.2      matt 		ma.ma_dmat = sc->sc_dmat;
    181       1.2      matt 
    182  1.5.30.1   thorpej 		cf = config_search(sc->sc_dev, &ma,
    183  1.5.30.1   thorpej 				   CFARG_SUBMATCH, mainbus_find,
    184  1.5.30.1   thorpej 				   CFARG_EOL);
    185       1.2      matt 		if (cf == NULL && critical_devs[i].required)
    186       1.2      matt 			panic("%s: failed to find %s",
    187       1.5       ryo 			    __func__, critical_devs[i].name);
    188       1.2      matt 
    189       1.5       ryo 		ma.ma_addr = cf->cf_loc[MAINBUSCF_ADDR];
    190  1.5.30.3   thorpej 		config_attach(sc->sc_dev, cf, &ma, mainbus_print, CFARG_EOL);
    191       1.2      matt 	}
    192       1.2      matt }
    193