1 1.1 jmcneill /* $NetBSD: fu540_prci.c,v 1.1 2022/11/25 12:35:44 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.1 jmcneill 31 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: fu540_prci.c,v 1.1 2022/11/25 12:35:44 jmcneill Exp $"); 32 1.1 jmcneill 33 1.1 jmcneill #include <sys/param.h> 34 1.1 jmcneill #include <sys/bus.h> 35 1.1 jmcneill #include <sys/device.h> 36 1.1 jmcneill #include <sys/intr.h> 37 1.1 jmcneill #include <sys/systm.h> 38 1.1 jmcneill #include <sys/time.h> 39 1.1 jmcneill #include <sys/kmem.h> 40 1.1 jmcneill 41 1.1 jmcneill #include <dev/clk/clk_backend.h> 42 1.1 jmcneill 43 1.1 jmcneill #include <dev/fdt/fdtvar.h> 44 1.1 jmcneill 45 1.1 jmcneill #define COREPLLCFG0 0x04 46 1.1 jmcneill #define DDRPLLCFG0 0x0c 47 1.1 jmcneill #define DDRPLLCFG1 0x10 48 1.1 jmcneill #define GEMGXLPLLCFG0 0x1c 49 1.1 jmcneill #define GEMGXLPLLCFG1 0x20 50 1.1 jmcneill #define PLL0_DIVQ __BITS(17,15) 51 1.1 jmcneill #define PLL0_DIVF __BITS(14,6) 52 1.1 jmcneill #define PLL0_DIVR __BITS(5,0) 53 1.1 jmcneill #define PLL1_CKE __BIT(24) 54 1.1 jmcneill #define CORECLKSEL 0x24 55 1.1 jmcneill 56 1.1 jmcneill enum fu540_clkid { 57 1.1 jmcneill clkid_corepll, 58 1.1 jmcneill clkid_ddrpll, 59 1.1 jmcneill clkid_gemgxlpll, 60 1.1 jmcneill clkid_tlclk, 61 1.1 jmcneill num_clkid 62 1.1 jmcneill }; 63 1.1 jmcneill CTASSERT(num_clkid == 4); 64 1.1 jmcneill 65 1.1 jmcneill static int fu540_prci_match(device_t, cfdata_t, void *); 66 1.1 jmcneill static void fu540_prci_attach(device_t, device_t, void *); 67 1.1 jmcneill 68 1.1 jmcneill static u_int fu540_prci_clk_get_rate(void *, struct clk *); 69 1.1 jmcneill 70 1.1 jmcneill static const struct device_compatible_entry compat_data[] = { 71 1.1 jmcneill { .compat = "sifive,fu540-c000-prci" }, 72 1.1 jmcneill DEVICE_COMPAT_EOL 73 1.1 jmcneill }; 74 1.1 jmcneill 75 1.1 jmcneill struct fu540_prci_softc { 76 1.1 jmcneill device_t sc_dev; 77 1.1 jmcneill bus_space_tag_t sc_bst; 78 1.1 jmcneill bus_space_handle_t sc_bsh; 79 1.1 jmcneill struct clk_domain sc_clkdom; 80 1.1 jmcneill struct clk sc_clk[num_clkid]; 81 1.1 jmcneill 82 1.1 jmcneill u_int sc_hfclk; 83 1.1 jmcneill u_int sc_rtcclk; 84 1.1 jmcneill }; 85 1.1 jmcneill 86 1.1 jmcneill #define RD4(sc, reg) \ 87 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 88 1.1 jmcneill #define WR4(sc, reg, val) \ 89 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 90 1.1 jmcneill 91 1.1 jmcneill CFATTACH_DECL_NEW(fu540_prci, sizeof(struct fu540_prci_softc), 92 1.1 jmcneill fu540_prci_match, fu540_prci_attach, NULL, NULL); 93 1.1 jmcneill 94 1.1 jmcneill static struct clk * 95 1.1 jmcneill fu540_prci_clk_get(void *priv, const char *name) 96 1.1 jmcneill { 97 1.1 jmcneill struct fu540_prci_softc * const sc = priv; 98 1.1 jmcneill u_int clkid; 99 1.1 jmcneill 100 1.1 jmcneill for (clkid = 0; clkid < num_clkid; clkid++) { 101 1.1 jmcneill if (strcmp(name, sc->sc_clk[clkid].name) == 0) { 102 1.1 jmcneill return &sc->sc_clk[clkid]; 103 1.1 jmcneill } 104 1.1 jmcneill } 105 1.1 jmcneill 106 1.1 jmcneill return NULL; 107 1.1 jmcneill } 108 1.1 jmcneill 109 1.1 jmcneill static void 110 1.1 jmcneill fu540_prci_clk_put(void *priv, struct clk *clk) 111 1.1 jmcneill { 112 1.1 jmcneill } 113 1.1 jmcneill 114 1.1 jmcneill static u_int 115 1.1 jmcneill fu540_prci_clk_get_rate_pll(struct fu540_prci_softc *sc, u_int reg) 116 1.1 jmcneill { 117 1.1 jmcneill uint32_t val; 118 1.1 jmcneill u_int rate, divr, divf, divq; 119 1.1 jmcneill 120 1.1 jmcneill val = RD4(sc, reg); 121 1.1 jmcneill divr = __SHIFTOUT(val, PLL0_DIVR) + 1; 122 1.1 jmcneill divf = __SHIFTOUT(val, PLL0_DIVF) + 1; 123 1.1 jmcneill divq = __SHIFTOUT(val, PLL0_DIVQ); 124 1.1 jmcneill rate = sc->sc_hfclk * divr * divf; 125 1.1 jmcneill rate <<= divq; 126 1.1 jmcneill 127 1.1 jmcneill return rate; 128 1.1 jmcneill } 129 1.1 jmcneill 130 1.1 jmcneill static u_int 131 1.1 jmcneill fu540_prci_clk_get_rate(void *priv, struct clk *clk) 132 1.1 jmcneill { 133 1.1 jmcneill struct fu540_prci_softc * const sc = priv; 134 1.1 jmcneill u_int rate; 135 1.1 jmcneill 136 1.1 jmcneill if (clk == &sc->sc_clk[clkid_corepll] || 137 1.1 jmcneill clk == &sc->sc_clk[clkid_tlclk]) { 138 1.1 jmcneill rate = fu540_prci_clk_get_rate_pll(sc, COREPLLCFG0); 139 1.1 jmcneill if (clk == &sc->sc_clk[clkid_tlclk]) { 140 1.1 jmcneill rate /= 2; 141 1.1 jmcneill } 142 1.1 jmcneill return rate; 143 1.1 jmcneill } else if (clk == &sc->sc_clk[clkid_ddrpll]) { 144 1.1 jmcneill return fu540_prci_clk_get_rate_pll(sc, DDRPLLCFG0); 145 1.1 jmcneill } else if (clk == &sc->sc_clk[clkid_gemgxlpll]) { 146 1.1 jmcneill return fu540_prci_clk_get_rate_pll(sc, GEMGXLPLLCFG0); 147 1.1 jmcneill } else { 148 1.1 jmcneill /* Not implemented. */ 149 1.1 jmcneill return 0; 150 1.1 jmcneill } 151 1.1 jmcneill } 152 1.1 jmcneill 153 1.1 jmcneill static int 154 1.1 jmcneill fu540_prci_clk_enable(void *priv, struct clk *clk) 155 1.1 jmcneill { 156 1.1 jmcneill struct fu540_prci_softc * const sc = priv; 157 1.1 jmcneill uint32_t val; 158 1.1 jmcneill 159 1.1 jmcneill if (clk == &sc->sc_clk[clkid_corepll] || 160 1.1 jmcneill clk == &sc->sc_clk[clkid_tlclk]) { 161 1.1 jmcneill /* Always enabled. */ 162 1.1 jmcneill return 0; 163 1.1 jmcneill } else if (clk == &sc->sc_clk[clkid_ddrpll]) { 164 1.1 jmcneill val = RD4(sc, DDRPLLCFG1); 165 1.1 jmcneill WR4(sc, DDRPLLCFG1, val | PLL1_CKE); 166 1.1 jmcneill return 0; 167 1.1 jmcneill } else if (clk == &sc->sc_clk[clkid_gemgxlpll]) { 168 1.1 jmcneill val = RD4(sc, GEMGXLPLLCFG1); 169 1.1 jmcneill WR4(sc, GEMGXLPLLCFG1, val | PLL1_CKE); 170 1.1 jmcneill return 0; 171 1.1 jmcneill } else { 172 1.1 jmcneill /* Not implemented. */ 173 1.1 jmcneill return ENXIO; 174 1.1 jmcneill } 175 1.1 jmcneill } 176 1.1 jmcneill 177 1.1 jmcneill static int 178 1.1 jmcneill fu540_prci_clk_disable(void *priv, struct clk *clk) 179 1.1 jmcneill { 180 1.1 jmcneill return ENXIO; 181 1.1 jmcneill } 182 1.1 jmcneill 183 1.1 jmcneill static const struct clk_funcs fu540_prci_clk_funcs = { 184 1.1 jmcneill .get = fu540_prci_clk_get, 185 1.1 jmcneill .put = fu540_prci_clk_put, 186 1.1 jmcneill .get_rate = fu540_prci_clk_get_rate, 187 1.1 jmcneill .enable = fu540_prci_clk_enable, 188 1.1 jmcneill .disable = fu540_prci_clk_disable, 189 1.1 jmcneill }; 190 1.1 jmcneill 191 1.1 jmcneill static struct clk * 192 1.1 jmcneill fu540_prci_fdt_decode(device_t dev, int cc_phandle, const void *data, size_t len) 193 1.1 jmcneill { 194 1.1 jmcneill struct fu540_prci_softc * const sc = device_private(dev); 195 1.1 jmcneill u_int clkid; 196 1.1 jmcneill 197 1.1 jmcneill if (len != 4) { 198 1.1 jmcneill return NULL; 199 1.1 jmcneill } 200 1.1 jmcneill 201 1.1 jmcneill clkid = be32dec(data); 202 1.1 jmcneill if (clkid >= num_clkid) { 203 1.1 jmcneill return NULL; 204 1.1 jmcneill } 205 1.1 jmcneill 206 1.1 jmcneill return &sc->sc_clk[clkid]; 207 1.1 jmcneill } 208 1.1 jmcneill 209 1.1 jmcneill static const struct fdtbus_clock_controller_func fu540_prci_fdt_funcs = { 210 1.1 jmcneill .decode = fu540_prci_fdt_decode 211 1.1 jmcneill }; 212 1.1 jmcneill 213 1.1 jmcneill static int 214 1.1 jmcneill fu540_prci_match(device_t parent, cfdata_t cf, void *aux) 215 1.1 jmcneill { 216 1.1 jmcneill struct fdt_attach_args * const faa = aux; 217 1.1 jmcneill 218 1.1 jmcneill return of_compatible_match(faa->faa_phandle, compat_data); 219 1.1 jmcneill } 220 1.1 jmcneill 221 1.1 jmcneill static void 222 1.1 jmcneill fu540_prci_attach(device_t parent, device_t self, void *aux) 223 1.1 jmcneill { 224 1.1 jmcneill struct fu540_prci_softc * const sc = device_private(self); 225 1.1 jmcneill struct fdt_attach_args * const faa = aux; 226 1.1 jmcneill const int phandle = faa->faa_phandle; 227 1.1 jmcneill const char *clkname; 228 1.1 jmcneill struct clk *clk; 229 1.1 jmcneill bus_addr_t addr; 230 1.1 jmcneill bus_size_t size; 231 1.1 jmcneill u_int clkid; 232 1.1 jmcneill 233 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 234 1.1 jmcneill aprint_error(": couldn't get registers\n"); 235 1.1 jmcneill return; 236 1.1 jmcneill } 237 1.1 jmcneill 238 1.1 jmcneill sc->sc_dev = self; 239 1.1 jmcneill sc->sc_bst = faa->faa_bst; 240 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 241 1.1 jmcneill aprint_error(": couldn't map registers\n"); 242 1.1 jmcneill return; 243 1.1 jmcneill } 244 1.1 jmcneill 245 1.1 jmcneill clk = fdtbus_clock_get(phandle, "hfclk"); 246 1.1 jmcneill if (clk == NULL) { 247 1.1 jmcneill aprint_error(": couldn't get hfclk\n"); 248 1.1 jmcneill return; 249 1.1 jmcneill } 250 1.1 jmcneill sc->sc_hfclk = clk_get_rate(clk); 251 1.1 jmcneill 252 1.1 jmcneill clk = fdtbus_clock_get(phandle, "rtcclk"); 253 1.1 jmcneill if (clk == NULL) { 254 1.1 jmcneill aprint_error(": couldn't get rtcclk\n"); 255 1.1 jmcneill return; 256 1.1 jmcneill } 257 1.1 jmcneill sc->sc_rtcclk = clk_get_rate(clk); 258 1.1 jmcneill 259 1.1 jmcneill sc->sc_clkdom.name = device_xname(self); 260 1.1 jmcneill sc->sc_clkdom.funcs = &fu540_prci_clk_funcs; 261 1.1 jmcneill sc->sc_clkdom.priv = sc; 262 1.1 jmcneill for (clkid = 0; clkid < num_clkid; clkid++) { 263 1.1 jmcneill clkname = fdtbus_get_string_index(phandle, 264 1.1 jmcneill "clock-output-names", clkid); 265 1.1 jmcneill sc->sc_clk[clkid].domain = &sc->sc_clkdom; 266 1.1 jmcneill if (clkname != NULL) { 267 1.1 jmcneill sc->sc_clk[clkid].name = kmem_asprintf("%s", clkname); 268 1.1 jmcneill } 269 1.1 jmcneill clk_attach(&sc->sc_clk[clkid]); 270 1.1 jmcneill } 271 1.1 jmcneill 272 1.1 jmcneill aprint_naive("\n"); 273 1.1 jmcneill aprint_normal(": FU540 PRCI (HF %u Hz, RTC %u Hz)\n", 274 1.1 jmcneill sc->sc_hfclk, sc->sc_rtcclk); 275 1.1 jmcneill 276 1.1 jmcneill for (clkid = 0; clkid < num_clkid; clkid++) { 277 1.1 jmcneill aprint_debug_dev(self, "clkid %u [%s]: %u Hz\n", clkid, 278 1.1 jmcneill sc->sc_clk[clkid].name ? sc->sc_clk[clkid].name : "<none>", 279 1.1 jmcneill clk_get_rate(&sc->sc_clk[clkid])); 280 1.1 jmcneill } 281 1.1 jmcneill 282 1.1 jmcneill fdtbus_register_clock_controller(self, phandle, &fu540_prci_fdt_funcs); 283 1.1 jmcneill } 284