Home | History | Annotate | Line # | Download | only in ralink
      1  1.7   thorpej /*	$NetBSD: ralink_mainbus.c,v 1.7 2021/08/07 16:18:59 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.7   thorpej __KERNEL_RCSID(0, "$NetBSD: ralink_mainbus.c,v 1.7 2021/08/07 16:18:59 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.6   thorpej 	config_search(self, &ma,
    123  1.7   thorpej 	    CFARGS(.search = mainbus_search));
    124  1.2      matt }
    125  1.2      matt 
    126  1.2      matt static int
    127  1.2      matt mainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    128  1.2      matt {
    129  1.5       ryo 	struct mainbus_attach_args *ma;
    130  1.5       ryo 
    131  1.5       ryo 	ma = aux;
    132  1.5       ryo 	ma->ma_addr = cf->cf_loc[MAINBUSCF_ADDR];
    133  1.5       ryo 
    134  1.6   thorpej 	if (config_probe(parent, cf, aux))
    135  1.7   thorpej 		config_attach(parent, cf, aux, mainbus_print, CFARGS_NONE);
    136  1.2      matt 	else
    137  1.2      matt 		mainbus_print(aux, cf->cf_name);
    138  1.2      matt 	return 0;
    139  1.2      matt }
    140  1.2      matt 
    141  1.2      matt int
    142  1.2      matt mainbus_print(void *aux, const char *pnp)
    143  1.2      matt {
    144  1.5       ryo 	struct mainbus_attach_args *ma;
    145  1.5       ryo 
    146  1.2      matt 	if (pnp)
    147  1.2      matt 		aprint_normal("%s unconfigured\n", pnp);
    148  1.2      matt 
    149  1.5       ryo 	ma = aux;
    150  1.5       ryo 	if (ma->ma_addr != MAINBUSCF_ADDR_DEFAULT)
    151  1.5       ryo 		aprint_normal(" addr 0x%llx", ma->ma_addr);
    152  1.5       ryo 
    153  1.2      matt 	return UNCONF;
    154  1.2      matt }
    155  1.2      matt 
    156  1.2      matt static int
    157  1.2      matt mainbus_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    158  1.2      matt {
    159  1.2      matt 	struct mainbus_attach_args * const ma = aux;
    160  1.2      matt 	char devname[16];
    161  1.2      matt 
    162  1.3  christos 	snprintf(devname, sizeof(devname), "%s%d", cf->cf_name, cf->cf_unit);
    163  1.2      matt 	if (strcmp(ma->ma_name, devname) != 0)
    164  1.2      matt 		return 0;
    165  1.2      matt 
    166  1.2      matt 	return config_match(parent, cf, ma);
    167  1.2      matt }
    168  1.2      matt 
    169  1.2      matt static void
    170  1.2      matt mainbus_attach_critical(struct mainbus_softc *sc)
    171  1.2      matt {
    172  1.2      matt 	struct mainbus_attach_args ma;
    173  1.2      matt 	cfdata_t cf;
    174  1.2      matt 	size_t i;
    175  1.2      matt 
    176  1.2      matt 	for (i = 0; i < __arraycount(critical_devs); i++) {
    177  1.2      matt 		ma.ma_name = critical_devs[i].name;
    178  1.2      matt 		ma.ma_memt = sc->sc_memt;
    179  1.2      matt 		ma.ma_dmat = sc->sc_dmat;
    180  1.2      matt 
    181  1.6   thorpej 		cf = config_search(sc->sc_dev, &ma,
    182  1.7   thorpej 				   CFARGS(.submatch = mainbus_find));
    183  1.2      matt 		if (cf == NULL && critical_devs[i].required)
    184  1.2      matt 			panic("%s: failed to find %s",
    185  1.5       ryo 			    __func__, critical_devs[i].name);
    186  1.2      matt 
    187  1.5       ryo 		ma.ma_addr = cf->cf_loc[MAINBUSCF_ADDR];
    188  1.7   thorpej 		config_attach(sc->sc_dev, cf, &ma, mainbus_print, CFARGS_NONE);
    189  1.2      matt 	}
    190  1.2      matt }
    191