ralink_mainbus.c revision 1.4 1 /* $NetBSD: ralink_mainbus.c,v 1.4 2014/04/30 00:52: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.4 2014/04/30 00:52: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 union {
90 char buf[9];
91 uint32_t id[2];
92 } xid;
93
94 mainbus_found = true;
95
96 sc->sc_dev = self;
97 sc->sc_memt = &ra_bus_memt;
98 sc->sc_dmat = &ra_bus_dmat;
99
100 xid.id[0] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID0);
101 xid.id[1] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID1);
102 xid.buf[8] = '\0';
103 if (xid.buf[6] == ' ') {
104 xid.buf[6] = '\0';
105 } else if (xid.buf[7] == ' ') {
106 xid.buf[7] = '\0';
107 }
108 const char * const manuf = xid.buf[0] == 'M' ? "Mediatek" : "Ralink";
109
110 aprint_naive(": %s %s System Bus\n", manuf, xid.buf);
111 aprint_normal(": %s %s System Bus\n", manuf, xid.buf);
112
113 ma.ma_name = NULL;
114 ma.ma_memt = sc->sc_memt;
115 ma.ma_dmat = sc->sc_dmat;
116
117 /* attach critical devices */
118 mainbus_attach_critical(sc);
119
120 /* attach other devices */
121 config_search_ia(mainbus_search, self, "mainbus", &ma);
122 }
123
124 static int
125 mainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
126 {
127 if (config_match(parent, cf, aux) > 0)
128 config_attach(parent, cf, aux, mainbus_print);
129 else
130 mainbus_print(aux, cf->cf_name);
131 return 0;
132 }
133
134 int
135 mainbus_print(void *aux, const char *pnp)
136 {
137 if (pnp)
138 aprint_normal("%s unconfigured\n", pnp);
139
140 return UNCONF;
141 }
142
143 static int
144 mainbus_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
145 {
146 struct mainbus_attach_args * const ma = aux;
147 char devname[16];
148
149 snprintf(devname, sizeof(devname), "%s%d", cf->cf_name, cf->cf_unit);
150 if (strcmp(ma->ma_name, devname) != 0)
151 return 0;
152
153 return config_match(parent, cf, ma);
154 }
155
156 static void
157 mainbus_attach_critical(struct mainbus_softc *sc)
158 {
159 struct mainbus_attach_args ma;
160 cfdata_t cf;
161 size_t i;
162
163 for (i = 0; i < __arraycount(critical_devs); i++) {
164 ma.ma_name = critical_devs[i].name;
165 ma.ma_memt = sc->sc_memt;
166 ma.ma_dmat = sc->sc_dmat;
167
168 cf = config_search_ia(mainbus_find, sc->sc_dev, "mainbus", &ma);
169 if (cf == NULL && critical_devs[i].required)
170 panic("%s: failed to find %s",
171 __func__, critical_devs[i].name);
172
173 config_attach(sc->sc_dev, cf, &ma, mainbus_print);
174 }
175 }
176