1 /*- 2 * Copyright (C) 2013-2015 Daisuke Aoyama <aoyama@peach.ne.jp> 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 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/cpu.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 #include <sys/rman.h> 41 #include <sys/sema.h> 42 #include <sys/sysctl.h> 43 44 #include <machine/bus.h> 45 #include <machine/cpu.h> 46 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 50 #include <arm/broadcom/bcm2835/bcm2835_clkman.h> 51 52 static struct ofw_compat_data compat_data[] = { 53 {"brcm,bcm2835-cprman", 1}, 54 {"broadcom,bcm2835-cprman", 1}, 55 {NULL, 0} 56 }; 57 58 struct bcm2835_clkman_softc { 59 device_t sc_dev; 60 61 struct resource * sc_m_res; 62 bus_space_tag_t sc_m_bst; 63 bus_space_handle_t sc_m_bsh; 64 }; 65 66 #define BCM_CLKMAN_WRITE(_sc, _off, _val) \ 67 bus_space_write_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off, _val) 68 #define BCM_CLKMAN_READ(_sc, _off) \ 69 bus_space_read_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off) 70 71 #define W_CMCLK(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, unit, 0x5a000000 | (_val)) 72 #define R_CMCLK(_sc, unit) BCM_CLKMAN_READ(_sc, unit) 73 #define W_CMDIV(_sc, unit, _val) BCM_CLKMAN_WRITE(_sc, (unit) + 4, 0x5a000000 | (_val)) 74 #define R_CMDIV(_sc, unit) BCM_CLKMAN_READ(_sc, (unit) + 4) 75 76 static int 77 bcm2835_clkman_probe(device_t dev) 78 { 79 80 if (!ofw_bus_status_okay(dev)) 81 return (ENXIO); 82 83 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 84 return (ENXIO); 85 86 device_set_desc(dev, "BCM283x Clock Manager"); 87 88 return (BUS_PROBE_DEFAULT); 89 } 90 91 static int 92 bcm2835_clkman_attach(device_t dev) 93 { 94 struct bcm2835_clkman_softc *sc; 95 int rid; 96 97 if (device_get_unit(dev) != 0) { 98 device_printf(dev, "only one clk manager supported\n"); 99 return (ENXIO); 100 } 101 102 sc = device_get_softc(dev); 103 sc->sc_dev = dev; 104 105 rid = 0; 106 sc->sc_m_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 107 RF_ACTIVE); 108 if (!sc->sc_m_res) { 109 device_printf(dev, "cannot allocate memory window\n"); 110 return (ENXIO); 111 } 112 113 sc->sc_m_bst = rman_get_bustag(sc->sc_m_res); 114 sc->sc_m_bsh = rman_get_bushandle(sc->sc_m_res); 115 116 return (bus_generic_attach(dev)); 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: 125MHz)", 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: 123Hz)", 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 devclass_t bcm2835_clkman_devclass; 204 static driver_t bcm2835_clkman_driver = { 205 "bcm2835_clkman", 206 bcm2835_clkman_methods, 207 sizeof(struct bcm2835_clkman_softc), 208 }; 209 210 DRIVER_MODULE(bcm2835_clkman, simplebus, bcm2835_clkman_driver, 211 bcm2835_clkman_devclass, 0, 0); 212 MODULE_VERSION(bcm2835_clkman, 1); 213