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