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