]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sparc64/jbusppm.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / sparc64 / jbusppm.c
1 /*-
2  * Copyright (c) 2008 Marius Strobl <marius@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/resource.h>
36 #include <sys/rman.h>
37
38 #include <dev/ofw/ofw_bus.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42
43 #define JBUSPPM_NREG    2
44
45 #define JBUSPPM_DEVID   0
46 #define JBUSPPM_ESTAR   1
47
48 #define JBUSPPM_DEVID_JID               0x00
49 #define JBUSPPM_DEVID_JID_MASTER        0x00040000
50
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
61
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];
66 };
67
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))
72
73 static device_probe_t jbusppm_probe;
74 static device_attach_t jbusppm_attach;
75
76 static device_method_t jbusppm_methods[] = {
77         /* Device interface */
78         DEVMETHOD(device_probe,         jbusppm_probe),
79         DEVMETHOD(device_attach,        jbusppm_attach),
80
81         KOBJMETHOD_END
82 };
83
84 static devclass_t jbusppm_devclass;
85
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);
89
90 static int
91 jbusppm_probe(device_t dev)
92 {
93         const char* compat;
94
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);
100         }
101         return (ENXIO);
102 }
103
104 static int
105 jbusppm_attach(device_t dev)
106 {
107         struct jbusppm_softc *sc;
108         int i, rid;
109 #if 1
110         device_t *children, tomatillo;
111         u_long tcount, tstart, jcount, jstart;
112         int j, nchildren;
113 #endif
114
115         sc = device_get_softc(dev);
116         for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR; i++) {
117                 rid = i;
118                 /*
119                  * The JBUSPPM_ESTAR resources is shared with that of the
120                  * Tomatillo bus A controller configuration register bank.
121                  */
122 #if 0
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) {
126                         device_printf(dev,
127                             "could not allocate resource %d\n", i);
128                         goto fail;
129                 }
130                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
131                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
132 #else
133                 /*
134                  * Workaround for the fact that rman(9) only allows to
135                  * share resources of the same size.
136                  */
137                 if (i == JBUSPPM_ESTAR) {
138                         if (bus_get_resource(dev, SYS_RES_MEMORY, i, &jstart,
139                             &jcount) != 0) {
140                                 device_printf(dev,
141                                     "could not determine Estar resource\n");
142                                 goto fail;
143                         }
144                         if (device_get_children(device_get_parent(dev),
145                             &children, &nchildren) != 0) {
146                                 device_printf(dev, "could not get children\n");
147                                 goto fail;
148                         }
149                         tomatillo = NULL;
150                         for (j = 0; j < nchildren; j++) {
151                                 if (ofw_bus_get_type(children[j]) != NULL &&
152                                     strcmp(ofw_bus_get_type(children[j]),
153                                     "pci") == 0 &&
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];
160                                         break;
161                                 }
162                         }
163                         free(children, M_TEMP);
164                         if (tomatillo == NULL) {
165                                 device_printf(dev,
166                                     "could not find Tomatillo\n");
167                                 goto fail;
168                         }
169                         if (bus_get_resource(tomatillo, SYS_RES_MEMORY, 1,
170                             &tstart, &tcount) != 0) {
171                                 device_printf(dev,
172                                     "could not determine Tomatillo "
173                                     "resource\n");
174                                 goto fail;
175                         }
176                         sc->sc_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY,
177                             &rid, tstart, tstart + tcount - 1, tcount,
178                             RF_SHAREABLE | RF_ACTIVE);
179                 } else
180                         sc->sc_res[i] = bus_alloc_resource_any(dev,
181                             SYS_RES_MEMORY, &rid, RF_ACTIVE);
182                 if (sc->sc_res[i] == NULL) {
183                         device_printf(dev,
184                             "could not allocate resource %d\n", i);
185                         goto fail;
186                 }
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]);
192 #endif
193         }
194
195         if (bootverbose) {
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:
203                         printf("full");
204                         break;
205                 case JBUSPPM_ESTAR_CTRL_2:
206                         printf("half");
207                         break;
208                 case JBUSPPM_ESTAR_CTRL_32:
209                         printf("1/32");
210                         break;
211                 default:
212                         printf("unknown");
213                         break;
214                 }
215                 printf(" speed\n");
216         }
217
218         return (0);
219
220  fail:
221         for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR && sc->sc_res[i] != NULL;
222             i++)
223                 bus_release_resource(dev, SYS_RES_MEMORY,
224                     rman_get_rid(sc->sc_res[i]), sc->sc_res[i]);
225         return (ENXIO);
226 }