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