ralink_mainbus.c revision 1.2.2.1       1  1.2.2.1  yamt /*	$NetBSD: ralink_mainbus.c,v 1.2.2.1 2014/05/22 11:39:58 yamt 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.2.2.1  yamt __KERNEL_RCSID(0, "$NetBSD: ralink_mainbus.c,v 1.2.2.1 2014/05/22 11:39:58 yamt Exp $");
     31      1.2  matt 
     32      1.2  matt #include <sys/param.h>
     33      1.2  matt #include <sys/systm.h>
     34      1.2  matt #include <sys/device.h>
     35      1.2  matt 
     36      1.2  matt #include <mips/cache.h>
     37      1.2  matt #include <mips/cpuregs.h>
     38      1.2  matt 
     39      1.2  matt #include <mips/ralink/ralink_reg.h>
     40      1.2  matt #include <mips/ralink/ralink_var.h>
     41      1.2  matt 
     42      1.2  matt typedef struct mainbus_softc {
     43      1.2  matt 	device_t	sc_dev;
     44      1.2  matt 	bus_space_tag_t	sc_memt;
     45      1.2  matt 	bus_dma_tag_t	sc_dmat;
     46      1.2  matt } mainbus_softc_t;
     47      1.2  matt 
     48      1.2  matt static int  mainbus_match(device_t, cfdata_t, void *);
     49      1.2  matt static void mainbus_attach(device_t, device_t, void *);
     50      1.2  matt static int  mainbus_search(device_t, cfdata_t, const int *, void *);
     51      1.2  matt static int  mainbus_print(void *, const char *);
     52      1.2  matt static int  mainbus_find(device_t, cfdata_t, const int *, void *);
     53      1.2  matt static void mainbus_attach_critical(struct mainbus_softc *);
     54      1.2  matt 
     55      1.2  matt 
     56      1.2  matt CFATTACH_DECL_NEW(mainbus, sizeof(struct mainbus_softc),
     57      1.2  matt 	mainbus_match, mainbus_attach, NULL, NULL);
     58      1.2  matt 
     59      1.2  matt static bool mainbus_found;
     60      1.2  matt 
     61      1.2  matt /*
     62      1.2  matt  * critical devices, if found, will be attached in the order listed here
     63      1.2  matt  */
     64      1.2  matt static const struct {
     65      1.2  matt 	const char *name;
     66      1.2  matt 	bool required;
     67      1.2  matt } critical_devs[] = {
     68      1.2  matt 	{ .name = "cpu0",	.required = true  },
     69      1.2  matt 	{ .name = "rwdog0",	.required = false },
     70      1.2  matt 	{ .name = "rgpio0",	.required = true  },
     71      1.2  matt 	{ .name = "ri2c0",	.required = false },
     72      1.2  matt 	{ .name = "com0",	.required = false },
     73      1.2  matt };
     74      1.2  matt 
     75      1.2  matt 
     76      1.2  matt static int
     77      1.2  matt mainbus_match(device_t parent, cfdata_t match, void *aux)
     78      1.2  matt {
     79      1.2  matt 	if (mainbus_found)
     80      1.2  matt 		return 0;
     81      1.2  matt 	return 1;
     82      1.2  matt }
     83      1.2  matt 
     84      1.2  matt static void
     85      1.2  matt mainbus_attach(device_t parent, device_t self, void *aux)
     86      1.2  matt {
     87      1.2  matt 	mainbus_softc_t * const sc = device_private(self);
     88      1.2  matt 	struct mainbus_attach_args ma;
     89  1.2.2.1  yamt 	union {
     90  1.2.2.1  yamt 		char buf[9];
     91  1.2.2.1  yamt 		uint32_t id[2];
     92  1.2.2.1  yamt 	} xid;
     93      1.2  matt 
     94      1.2  matt 	mainbus_found = true;
     95      1.2  matt 
     96      1.2  matt 	sc->sc_dev = self;
     97  1.2.2.1  yamt 	sc->sc_memt = &ra_bus_memt;
     98  1.2.2.1  yamt 	sc->sc_dmat = &ra_bus_dmat;
     99  1.2.2.1  yamt 
    100  1.2.2.1  yamt 	xid.id[0] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID0);
    101  1.2.2.1  yamt 	xid.id[1] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID1);
    102  1.2.2.1  yamt 	xid.buf[8] = '\0';
    103  1.2.2.1  yamt 	if (xid.buf[6] == ' ') {
    104  1.2.2.1  yamt 		xid.buf[6] = '\0';
    105  1.2.2.1  yamt 	} else if (xid.buf[7] == ' ') {
    106  1.2.2.1  yamt 		xid.buf[7] = '\0';
    107  1.2.2.1  yamt 	}
    108  1.2.2.1  yamt 	const char * const manuf = xid.buf[0] == 'M' ? "Mediatek" : "Ralink";
    109  1.2.2.1  yamt 
    110  1.2.2.1  yamt 	aprint_naive(": %s %s System Bus\n", manuf, xid.buf);
    111  1.2.2.1  yamt 	aprint_normal(": %s %s System Bus\n", manuf, xid.buf);
    112  1.2.2.1  yamt 
    113      1.2  matt 	ma.ma_name = NULL;
    114  1.2.2.1  yamt 	ma.ma_memt = sc->sc_memt;
    115  1.2.2.1  yamt 	ma.ma_dmat = sc->sc_dmat;
    116      1.2  matt 
    117      1.2  matt 	/* attach critical devices */
    118      1.2  matt 	mainbus_attach_critical(sc);
    119      1.2  matt 
    120      1.2  matt 	/* attach other devices */
    121      1.2  matt 	config_search_ia(mainbus_search, self, "mainbus", &ma);
    122      1.2  matt }
    123      1.2  matt 
    124      1.2  matt static int
    125      1.2  matt mainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    126      1.2  matt {
    127      1.2  matt 	if (config_match(parent, cf, aux) > 0)
    128      1.2  matt 		config_attach(parent, cf, aux, mainbus_print);
    129      1.2  matt 	else
    130      1.2  matt 		mainbus_print(aux, cf->cf_name);
    131      1.2  matt 	return 0;
    132      1.2  matt }
    133      1.2  matt 
    134      1.2  matt int
    135      1.2  matt mainbus_print(void *aux, const char *pnp)
    136      1.2  matt {
    137      1.2  matt 	if (pnp)
    138      1.2  matt 		aprint_normal("%s unconfigured\n", pnp);
    139      1.2  matt 
    140      1.2  matt 	return UNCONF;
    141      1.2  matt }
    142      1.2  matt 
    143      1.2  matt static int
    144      1.2  matt mainbus_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    145      1.2  matt {
    146      1.2  matt 	struct mainbus_attach_args * const ma = aux;
    147      1.2  matt 	char devname[16];
    148      1.2  matt 
    149  1.2.2.1  yamt 	snprintf(devname, sizeof(devname), "%s%d", cf->cf_name, cf->cf_unit);
    150      1.2  matt 	if (strcmp(ma->ma_name, devname) != 0)
    151      1.2  matt 		return 0;
    152      1.2  matt 
    153      1.2  matt 	return config_match(parent, cf, ma);
    154      1.2  matt }
    155      1.2  matt 
    156      1.2  matt static void
    157      1.2  matt mainbus_attach_critical(struct mainbus_softc *sc)
    158      1.2  matt {
    159      1.2  matt 	struct mainbus_attach_args ma;
    160      1.2  matt 	cfdata_t cf;
    161      1.2  matt 	size_t i;
    162      1.2  matt 
    163      1.2  matt 	for (i = 0; i < __arraycount(critical_devs); i++) {
    164      1.2  matt 		ma.ma_name = critical_devs[i].name;
    165      1.2  matt 		ma.ma_memt = sc->sc_memt;
    166      1.2  matt 		ma.ma_dmat = sc->sc_dmat;
    167      1.2  matt 
    168      1.2  matt 		cf = config_search_ia(mainbus_find, sc->sc_dev, "mainbus", &ma);
    169      1.2  matt 		if (cf == NULL && critical_devs[i].required)
    170      1.2  matt 			panic("%s: failed to find %s",
    171      1.2  matt 				__func__, critical_devs[i].name);
    172      1.2  matt 
    173      1.2  matt 		config_attach(sc->sc_dev, cf, &ma, mainbus_print);
    174      1.2  matt 	}
    175      1.2  matt }
    176