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