2 * Copyright (c) 2008 Marius Strobl <marius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/resource.h>
38 #include <dev/ofw/ofw_bus.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
43 #define JBUSPPM_NREG 2
45 #define JBUSPPM_DEVID 0
46 #define JBUSPPM_ESTAR 1
48 #define JBUSPPM_DEVID_JID 0x00
49 #define JBUSPPM_DEVID_JID_MASTER 0x00040000
51 #define JBUSPPM_ESTAR_CTRL 0x00
52 #define JBUSPPM_ESTAR_CTRL_1 0x00000001
53 #define JBUSPPM_ESTAR_CTRL_2 0x00000002
54 #define JBUSPPM_ESTAR_CTRL_32 0x00000020
55 #define JBUSPPM_ESTAR_CTRL_MASK \
56 (JBUSPPM_ESTAR_CTRL_1 | JBUSPPM_ESTAR_CTRL_2 | JBUSPPM_ESTAR_CTRL_32)
57 #define JBUSPPM_ESTAR_JCHNG 0x08
58 #define JBUSPPM_ESTAR_JCHNG_DELAY_MASK 0x00000007
59 #define JBUSPPM_ESTAR_JCHNG_START 0x00000010
60 #define JBUSPPM_ESTAR_JCHNG_OCCURED 0x00000018
62 struct jbusppm_softc {
63 struct resource *sc_res[JBUSPPM_NREG];
64 bus_space_tag_t sc_bt[JBUSPPM_NREG];
65 bus_space_handle_t sc_bh[JBUSPPM_NREG];
68 #define JBUSPPM_READ(sc, reg, off) \
69 bus_space_read_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off))
70 #define JBUSPPM_WRITE(sc, reg, off, val) \
71 bus_space_write_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val))
73 static device_probe_t jbusppm_probe;
74 static device_attach_t jbusppm_attach;
76 static device_method_t jbusppm_methods[] = {
77 /* Device interface */
78 DEVMETHOD(device_probe, jbusppm_probe),
79 DEVMETHOD(device_attach, jbusppm_attach),
84 static devclass_t jbusppm_devclass;
86 DEFINE_CLASS_0(jbusppm, jbusppm_driver, jbusppm_methods,
87 sizeof(struct jbusppm_softc));
88 DRIVER_MODULE(jbusppm, nexus, jbusppm_driver, jbusppm_devclass, 0, 0);
91 jbusppm_probe(device_t dev)
95 compat = ofw_bus_get_compat(dev);
96 if (compat != NULL && strcmp(ofw_bus_get_name(dev), "ppm") == 0 &&
97 strcmp(compat, "jbus-ppm") == 0) {
98 device_set_desc(dev, "JBus power management");
99 return (BUS_PROBE_DEFAULT);
105 jbusppm_attach(device_t dev)
107 struct jbusppm_softc *sc;
110 device_t *children, tomatillo;
111 u_long tcount, tstart, jcount, jstart;
115 sc = device_get_softc(dev);
116 for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR; i++) {
119 * The JBUSPPM_ESTAR resources is shared with that of the
120 * Tomatillo bus A controller configuration register bank.
123 sc->sc_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
124 &rid, (i == JBUSPPM_ESTAR ? RF_SHAREABLE : 0) | RF_ACTIVE);
125 if (sc->sc_res[i] == NULL) {
127 "could not allocate resource %d\n", i);
130 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
131 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
134 * Workaround for the fact that rman(9) only allows to
135 * share resources of the same size.
137 if (i == JBUSPPM_ESTAR) {
138 if (bus_get_resource(dev, SYS_RES_MEMORY, i, &jstart,
141 "could not determine Estar resource\n");
144 if (device_get_children(device_get_parent(dev),
145 &children, &nchildren) != 0) {
146 device_printf(dev, "could not get children\n");
150 for (j = 0; j < nchildren; j++) {
151 if (ofw_bus_get_type(children[j]) != NULL &&
152 strcmp(ofw_bus_get_type(children[j]),
154 ofw_bus_get_compat(children[j]) != NULL &&
155 strcmp(ofw_bus_get_compat(children[j]),
156 "pci108e,a801") == 0 &&
157 ((bus_get_resource_start(children[j],
158 SYS_RES_MEMORY, 0) >> 20) & 1) == 0) {
159 tomatillo = children[j];
163 free(children, M_TEMP);
164 if (tomatillo == NULL) {
166 "could not find Tomatillo\n");
169 if (bus_get_resource(tomatillo, SYS_RES_MEMORY, 1,
170 &tstart, &tcount) != 0) {
172 "could not determine Tomatillo "
176 sc->sc_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY,
177 &rid, tstart, tstart + tcount - 1, tcount,
178 RF_SHAREABLE | RF_ACTIVE);
180 sc->sc_res[i] = bus_alloc_resource_any(dev,
181 SYS_RES_MEMORY, &rid, RF_ACTIVE);
182 if (sc->sc_res[i] == NULL) {
184 "could not allocate resource %d\n", i);
187 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
188 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
189 if (i == JBUSPPM_ESTAR)
190 bus_space_subregion(sc->sc_bt[i], sc->sc_bh[i],
191 jstart - tstart, jcount, &sc->sc_bh[i]);
196 if ((JBUSPPM_READ(sc, JBUSPPM_DEVID, JBUSPPM_DEVID_JID) &
197 JBUSPPM_DEVID_JID_MASTER) != 0)
198 device_printf(dev, "master I/O bridge\n");
199 device_printf(dev, "running at ");
200 switch (JBUSPPM_READ(sc, JBUSPPM_ESTAR, JBUSPPM_ESTAR_CTRL) &
201 JBUSPPM_ESTAR_CTRL_MASK) {
202 case JBUSPPM_ESTAR_CTRL_1:
205 case JBUSPPM_ESTAR_CTRL_2:
208 case JBUSPPM_ESTAR_CTRL_32:
221 for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR && sc->sc_res[i] != NULL;
223 bus_release_resource(dev, SYS_RES_MEMORY,
224 rman_get_rid(sc->sc_res[i]), sc->sc_res[i]);