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