1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2017 Poul-Henning Kamp <phk@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/cpu.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/rman.h> 39 #include <sys/sema.h> 40 #include <sys/sysctl.h> 41 42 #include <machine/bus.h> 43 #include <machine/cpu.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <arm/broadcom/bcm2835/bcm2835_clkman.h> 49 50 static struct ofw_compat_data compat_data[] = { 51 {"brcm,bcm2711-cprman", 1}, 52 {"brcm,bcm2835-cprman", 1}, 53 {"broadcom,bcm2835-cprman", 1}, 54 {NULL, 0} 55 }; 56 57 struct bcm2835_clkman_softc { 58 device_t sc_dev; 59 60 struct resource * sc_m_res; 61 bus_space_tag_t sc_m_bst; 62 bus_space_handle_t sc_m_bsh; 63 }; 64 65 #define BCM_CLKMAN_WRITE(_sc, _off, _val) \ 66 bus_space_write_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off, _val) 67 #define BCM_CLKMAN_READ(_sc, _off) \ 68 bus_space_read_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off) 69 70 #define W_CMCLK(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, unit, 0x5a000000 | (_val)) 71 #define R_CMCLK(_sc, unit) BCM_CLKMAN_READ(_sc, unit) 72 #define W_CMDIV(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, (unit) + 4, 0x5a000000 | (_val)) 73 #define R_CMDIV(_sc, unit) BCM_CLKMAN_READ(_sc, (unit) + 4) 74 75 static int 76 bcm2835_clkman_probe(device_t dev) 77 { 78 79 if (!ofw_bus_status_okay(dev)) 80 return (ENXIO); 81 82 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 83 return (ENXIO); 84 85 device_set_desc(dev, "BCM283x Clock Manager"); 86 87 return (BUS_PROBE_DEFAULT); 88 } 89 90 static int 91 bcm2835_clkman_attach(device_t dev) 92 { 93 struct bcm2835_clkman_softc *sc; 94 int rid; 95 96 if (device_get_unit(dev) != 0) { 97 device_printf(dev, "only one clk manager supported\n"); 98 return (ENXIO); 99 } 100 101 sc = device_get_softc(dev); 102 sc->sc_dev = dev; 103 104 rid = 0; 105 sc->sc_m_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 106 RF_ACTIVE); 107 if (!sc->sc_m_res) { 108 device_printf(dev, "cannot allocate memory window\n"); 109 return (ENXIO); 110 } 111 112 sc->sc_m_bst = rman_get_bustag(sc->sc_m_res); 113 sc->sc_m_bsh = rman_get_bushandle(sc->sc_m_res); 114 115 bus_attach_children(dev); 116 return (0); 117 } 118 119 uint32_t 120 bcm2835_clkman_set_frequency(device_t dev, uint32_t unit, uint32_t hz) 121 { 122 struct bcm2835_clkman_softc *sc; 123 int i; 124 uint32_t u; 125 126 sc = device_get_softc(dev); 127 128 if (unit != BCM_PWM_CLKSRC) { 129 device_printf(sc->sc_dev, 130 "Unsupported unit 0x%x", unit); 131 return (0); 132 } 133 134 W_CMCLK(sc, unit, 6); 135 for (i = 0; i < 10; i++) { 136 u = R_CMCLK(sc, unit); 137 if (!(u&0x80)) 138 break; 139 DELAY(1000); 140 } 141 if (u & 0x80) { 142 device_printf(sc->sc_dev, 143 "Failed to stop clock for unit 0x%x", unit); 144 return (0); 145 } 146 if (hz == 0) 147 return (0); 148 149 u = 500000000/hz; 150 if (u < 4) { 151 device_printf(sc->sc_dev, 152 "Frequency too high for unit 0x%x (max: 125 MHz)", 153 unit); 154 return (0); 155 } 156 if (u > 0xfff) { 157 device_printf(sc->sc_dev, 158 "Frequency too low for unit 0x%x (min: 123 kHz)", 159 unit); 160 return (0); 161 } 162 hz = 500000000/u; 163 W_CMDIV(sc, unit, u << 12); 164 165 W_CMCLK(sc, unit, 0x16); 166 for (i = 0; i < 10; i++) { 167 u = R_CMCLK(sc, unit); 168 if ((u&0x80)) 169 break; 170 DELAY(1000); 171 } 172 if (!(u & 0x80)) { 173 device_printf(sc->sc_dev, 174 "Failed to start clock for unit 0x%x", unit); 175 return (0); 176 } 177 return (hz); 178 } 179 180 static int 181 bcm2835_clkman_detach(device_t dev) 182 { 183 struct bcm2835_clkman_softc *sc; 184 185 bus_generic_detach(dev); 186 187 sc = device_get_softc(dev); 188 if (sc->sc_m_res) 189 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_m_res); 190 191 return (0); 192 } 193 194 static device_method_t bcm2835_clkman_methods[] = { 195 /* Device interface */ 196 DEVMETHOD(device_probe, bcm2835_clkman_probe), 197 DEVMETHOD(device_attach, bcm2835_clkman_attach), 198 DEVMETHOD(device_detach, bcm2835_clkman_detach), 199 200 DEVMETHOD_END 201 }; 202 203 static driver_t bcm2835_clkman_driver = { 204 "bcm2835_clkman", 205 bcm2835_clkman_methods, 206 sizeof(struct bcm2835_clkman_softc), 207 }; 208 209 DRIVER_MODULE(bcm2835_clkman, simplebus, bcm2835_clkman_driver, 0, 0); 210 MODULE_VERSION(bcm2835_clkman, 1); 211