1171176Smlaier/*- 2171176Smlaier * Copyright (c) 2008 Marius Strobl <marius@FreeBSD.org> 3171176Smlaier * All rights reserved. 4171176Smlaier * 5171176Smlaier * Redistribution and use in source and binary forms, with or without 6171176Smlaier * modification, are permitted provided that the following conditions 7171176Smlaier * are met: 8171176Smlaier * 1. Redistributions of source code must retain the above copyright 9171176Smlaier * notice, this list of conditions and the following disclaimer. 10171176Smlaier * 2. Redistributions in binary form must reproduce the above copyright 11171176Smlaier * notice, this list of conditions and the following disclaimer in the 12171176Smlaier * documentation and/or other materials provided with the distribution. 13171176Smlaier * 14171176Smlaier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15171176Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16171176Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17171176Smlaier * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18171176Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19171176Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20171176Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21171176Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22171176Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23171176Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24171176Smlaier * SUCH DAMAGE. 25171176Smlaier */ 26171176Smlaier 27171176Smlaier#include <sys/cdefs.h> 28171176Smlaier__FBSDID("$FreeBSD$"); 29171176Smlaier 30171176Smlaier#include <sys/param.h> 31171176Smlaier#include <sys/systm.h> 32171176Smlaier#include <sys/bus.h> 33171176Smlaier#include <sys/kernel.h> 34171176Smlaier#include <sys/module.h> 35171176Smlaier#include <sys/resource.h> 36171176Smlaier#include <sys/rman.h> 37171176Smlaier 38171176Smlaier#include <dev/ofw/ofw_bus.h> 39171176Smlaier 40171176Smlaier#include <machine/bus.h> 41171176Smlaier#include <machine/resource.h> 42171176Smlaier 43171176Smlaier#if 1 44171176Smlaier#include <sparc64/pci/ofw_pci.h> 45171176Smlaier#endif 46171176Smlaier 47171176Smlaier#define JBUSPPM_NREG 2 48171176Smlaier 49171176Smlaier#define JBUSPPM_DEVID 0 50171176Smlaier#define JBUSPPM_ESTAR 1 51171176Smlaier 52171176Smlaier#define JBUSPPM_DEVID_JID 0x00 53171176Smlaier#define JBUSPPM_DEVID_JID_MASTER 0x00040000 54171176Smlaier 55171176Smlaier#define JBUSPPM_ESTAR_CTRL 0x00 56171176Smlaier#define JBUSPPM_ESTAR_CTRL_1 0x00000001 57171176Smlaier#define JBUSPPM_ESTAR_CTRL_2 0x00000002 58171176Smlaier#define JBUSPPM_ESTAR_CTRL_32 0x00000020 59171176Smlaier#define JBUSPPM_ESTAR_CTRL_MASK \ 60171176Smlaier (JBUSPPM_ESTAR_CTRL_1 | JBUSPPM_ESTAR_CTRL_2 | JBUSPPM_ESTAR_CTRL_32) 61171176Smlaier#define JBUSPPM_ESTAR_JCHNG 0x08 62171176Smlaier#define JBUSPPM_ESTAR_JCHNG_DELAY_MASK 0x00000007 63171176Smlaier#define JBUSPPM_ESTAR_JCHNG_START 0x00000010 64171176Smlaier#define JBUSPPM_ESTAR_JCHNG_OCCURED 0x00000018 65171176Smlaier 66171176Smlaierstruct jbusppm_softc { 67171176Smlaier struct resource *sc_res[JBUSPPM_NREG]; 68171176Smlaier bus_space_tag_t sc_bt[JBUSPPM_NREG]; 69171176Smlaier bus_space_handle_t sc_bh[JBUSPPM_NREG]; 70171176Smlaier}; 71171176Smlaier 72171176Smlaier#define JBUSPPM_READ(sc, reg, off) \ 73171176Smlaier bus_space_read_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off)) 74171176Smlaier#define JBUSPPM_WRITE(sc, reg, off, val) \ 75171176Smlaier bus_space_write_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val)) 76171176Smlaier 77171176Smlaierstatic device_probe_t jbusppm_probe; 78171176Smlaierstatic device_attach_t jbusppm_attach; 79171176Smlaier 80171176Smlaierstatic device_method_t jbusppm_methods[] = { 81171176Smlaier /* Device interface */ 82171176Smlaier DEVMETHOD(device_probe, jbusppm_probe), 83171176Smlaier DEVMETHOD(device_attach, jbusppm_attach), 84171176Smlaier 85171176Smlaier DEVMETHOD_END 86171176Smlaier}; 87171176Smlaier 88171176Smlaierstatic devclass_t jbusppm_devclass; 89171176Smlaier 90171176SmlaierDEFINE_CLASS_0(jbusppm, jbusppm_driver, jbusppm_methods, 91171176Smlaier sizeof(struct jbusppm_softc)); 92171176SmlaierDRIVER_MODULE(jbusppm, nexus, jbusppm_driver, jbusppm_devclass, 0, 0); 93171176Smlaier 94171176Smlaierstatic int 95171176Smlaierjbusppm_probe(device_t dev) 96171176Smlaier{ 97171176Smlaier const char* compat; 98171176Smlaier 99171176Smlaier compat = ofw_bus_get_compat(dev); 100171176Smlaier if (compat != NULL && strcmp(ofw_bus_get_name(dev), "ppm") == 0 && 101171176Smlaier strcmp(compat, "jbus-ppm") == 0) { 102171176Smlaier device_set_desc(dev, "JBus power management"); 103171176Smlaier return (BUS_PROBE_DEFAULT); 104171176Smlaier } 105171176Smlaier return (ENXIO); 106171176Smlaier} 107171176Smlaier 108171176Smlaierstatic int 109171176Smlaierjbusppm_attach(device_t dev) 110171176Smlaier{ 111171176Smlaier struct jbusppm_softc *sc; 112171176Smlaier int i, rid; 113171176Smlaier#if 1 114171176Smlaier device_t *children, tomatillo; 115171176Smlaier u_long tcount, tstart, jcount, jstart; 116 int j, nchildren; 117#endif 118 119 sc = device_get_softc(dev); 120 for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR; i++) { 121 rid = i; 122 /* 123 * The JBUSPPM_ESTAR resources is shared with that of the 124 * Tomatillo bus A controller configuration register bank. 125 */ 126#if 0 127 sc->sc_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 128 &rid, (i == JBUSPPM_ESTAR ? RF_SHAREABLE : 0) | RF_ACTIVE); 129 if (sc->sc_res[i] == NULL) { 130 device_printf(dev, 131 "could not allocate resource %d\n", i); 132 goto fail; 133 } 134 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]); 135 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]); 136#else 137 /* 138 * Workaround for the fact that rman(9) only allows to 139 * share resources of the same size. 140 */ 141 if (i == JBUSPPM_ESTAR) { 142 if (bus_get_resource(dev, SYS_RES_MEMORY, i, &jstart, 143 &jcount) != 0) { 144 device_printf(dev, 145 "could not determine Estar resource\n"); 146 goto fail; 147 } 148 if (device_get_children(device_get_parent(dev), 149 &children, &nchildren) != 0) { 150 device_printf(dev, "could not get children\n"); 151 goto fail; 152 } 153 tomatillo = NULL; 154 for (j = 0; j < nchildren; j++) { 155 if (ofw_bus_get_type(children[j]) != NULL && 156 strcmp(ofw_bus_get_type(children[j]), 157 OFW_TYPE_PCI) == 0 && 158 ofw_bus_get_compat(children[j]) != NULL && 159 strcmp(ofw_bus_get_compat(children[j]), 160 "pci108e,a801") == 0 && 161 ((bus_get_resource_start(children[j], 162 SYS_RES_MEMORY, 0) >> 20) & 1) == 0) { 163 tomatillo = children[j]; 164 break; 165 } 166 } 167 free(children, M_TEMP); 168 if (tomatillo == NULL) { 169 device_printf(dev, 170 "could not find Tomatillo\n"); 171 goto fail; 172 } 173 if (bus_get_resource(tomatillo, SYS_RES_MEMORY, 1, 174 &tstart, &tcount) != 0) { 175 device_printf(dev, 176 "could not determine Tomatillo " 177 "resource\n"); 178 goto fail; 179 } 180 sc->sc_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY, 181 &rid, tstart, tstart + tcount - 1, tcount, 182 RF_SHAREABLE | RF_ACTIVE); 183 } else 184 sc->sc_res[i] = bus_alloc_resource_any(dev, 185 SYS_RES_MEMORY, &rid, RF_ACTIVE); 186 if (sc->sc_res[i] == NULL) { 187 device_printf(dev, 188 "could not allocate resource %d\n", i); 189 goto fail; 190 } 191 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]); 192 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]); 193 if (i == JBUSPPM_ESTAR) 194 bus_space_subregion(sc->sc_bt[i], sc->sc_bh[i], 195 jstart - tstart, jcount, &sc->sc_bh[i]); 196#endif 197 } 198 199 if (bootverbose) { 200 if ((JBUSPPM_READ(sc, JBUSPPM_DEVID, JBUSPPM_DEVID_JID) & 201 JBUSPPM_DEVID_JID_MASTER) != 0) 202 device_printf(dev, "master I/O bridge\n"); 203 device_printf(dev, "running at "); 204 switch (JBUSPPM_READ(sc, JBUSPPM_ESTAR, JBUSPPM_ESTAR_CTRL) & 205 JBUSPPM_ESTAR_CTRL_MASK) { 206 case JBUSPPM_ESTAR_CTRL_1: 207 printf("full"); 208 break; 209 case JBUSPPM_ESTAR_CTRL_2: 210 printf("half"); 211 break; 212 case JBUSPPM_ESTAR_CTRL_32: 213 printf("1/32"); 214 break; 215 default: 216 printf("unknown"); 217 break; 218 } 219 printf(" speed\n"); 220 } 221 222 return (0); 223 224 fail: 225 for (i = JBUSPPM_DEVID; i <= JBUSPPM_ESTAR && sc->sc_res[i] != NULL; 226 i++) 227 bus_release_resource(dev, SYS_RES_MEMORY, 228 rman_get_rid(sc->sc_res[i]), sc->sc_res[i]); 229 return (ENXIO); 230} 231