10050ea24SMichal Meloun /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
30050ea24SMichal Meloun *
40050ea24SMichal Meloun * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
50050ea24SMichal Meloun *
60050ea24SMichal Meloun * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
70050ea24SMichal Meloun *
80050ea24SMichal Meloun * Redistribution and use in source and binary forms, with or without
90050ea24SMichal Meloun * modification, are permitted provided that the following conditions
100050ea24SMichal Meloun * are met:
110050ea24SMichal Meloun * 1. Redistributions of source code must retain the above copyright
120050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer.
130050ea24SMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright
140050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer in the
150050ea24SMichal Meloun * documentation and/or other materials provided with the distribution.
160050ea24SMichal Meloun *
170050ea24SMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180050ea24SMichal Meloun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190050ea24SMichal Meloun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200050ea24SMichal Meloun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210050ea24SMichal Meloun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
220050ea24SMichal Meloun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
230050ea24SMichal Meloun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
240050ea24SMichal Meloun * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
250050ea24SMichal Meloun * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260050ea24SMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270050ea24SMichal Meloun * SUCH DAMAGE.
280050ea24SMichal Meloun */
290050ea24SMichal Meloun
300050ea24SMichal Meloun #include <sys/param.h>
310050ea24SMichal Meloun #include <sys/systm.h>
320050ea24SMichal Meloun #include <sys/bus.h>
330050ea24SMichal Meloun #include <sys/fbio.h>
340050ea24SMichal Meloun #include <sys/kernel.h>
350050ea24SMichal Meloun #include <sys/module.h>
360050ea24SMichal Meloun #include <sys/rman.h>
370050ea24SMichal Meloun #include <sys/resource.h>
380050ea24SMichal Meloun #include <machine/bus.h>
390050ea24SMichal Meloun #include <vm/vm.h>
400050ea24SMichal Meloun #include <vm/vm_extern.h>
410050ea24SMichal Meloun #include <vm/vm_kern.h>
420050ea24SMichal Meloun #include <vm/pmap.h>
430050ea24SMichal Meloun
440050ea24SMichal Meloun #include <dev/fdt/simplebus.h>
450050ea24SMichal Meloun
460050ea24SMichal Meloun #include <dev/ofw/ofw_bus.h>
470050ea24SMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
480050ea24SMichal Meloun
490050ea24SMichal Meloun #include <arm/ti/clk/ti_clk_clkctrl.h>
500050ea24SMichal Meloun #include <arm/ti/ti_omap4_cm.h>
510050ea24SMichal Meloun #include <arm/ti/ti_cpuid.h>
520050ea24SMichal Meloun
530050ea24SMichal Meloun #if 0
540050ea24SMichal Meloun #define DPRINTF(dev, msg...) device_printf(dev, msg)
550050ea24SMichal Meloun #else
560050ea24SMichal Meloun #define DPRINTF(dev, msg...)
570050ea24SMichal Meloun #endif
580050ea24SMichal Meloun
590050ea24SMichal Meloun #define L4LS_CLKCTRL_38 2
600050ea24SMichal Meloun #define L4_WKUP_CLKCTRL_0 1
610050ea24SMichal Meloun #define NO_SPECIAL_REG 0
620050ea24SMichal Meloun
630050ea24SMichal Meloun /* Documentation/devicetree/bindings/clock/ti-clkctrl.txt */
640050ea24SMichal Meloun
650050ea24SMichal Meloun #define TI_CLKCTRL_L4_WKUP 5
660050ea24SMichal Meloun #define TI_CLKCTRL_L4_SECURE 4
670050ea24SMichal Meloun #define TI_CLKCTRL_L4_PER 3
680050ea24SMichal Meloun #define TI_CLKCTRL_L4_CFG 2
690050ea24SMichal Meloun #define TI_CLKCTRL 1
700050ea24SMichal Meloun #define TI_CLKCTRL_END 0
710050ea24SMichal Meloun
720050ea24SMichal Meloun static struct ofw_compat_data compat_data[] = {
730050ea24SMichal Meloun { "ti,clkctrl-l4-wkup", TI_CLKCTRL_L4_WKUP },
740050ea24SMichal Meloun { "ti,clkctrl-l4-secure", TI_CLKCTRL_L4_SECURE },
750050ea24SMichal Meloun { "ti,clkctrl-l4-per", TI_CLKCTRL_L4_PER },
760050ea24SMichal Meloun { "ti,clkctrl-l4-cfg", TI_CLKCTRL_L4_CFG },
770050ea24SMichal Meloun { "ti,clkctrl", TI_CLKCTRL },
780050ea24SMichal Meloun { NULL, TI_CLKCTRL_END }
790050ea24SMichal Meloun };
800050ea24SMichal Meloun
810050ea24SMichal Meloun struct ti_clkctrl_softc {
820050ea24SMichal Meloun device_t dev;
830050ea24SMichal Meloun
840050ea24SMichal Meloun struct clkdom *clkdom;
850050ea24SMichal Meloun };
860050ea24SMichal Meloun
870050ea24SMichal Meloun static int ti_clkctrl_probe(device_t dev);
880050ea24SMichal Meloun static int ti_clkctrl_attach(device_t dev);
890050ea24SMichal Meloun static int ti_clkctrl_detach(device_t dev);
900050ea24SMichal Meloun int clkctrl_ofw_map(struct clkdom *clkdom, uint32_t ncells,
910050ea24SMichal Meloun phandle_t *cells, struct clknode **clk);
920050ea24SMichal Meloun static int
930050ea24SMichal Meloun create_clkctrl(struct ti_clkctrl_softc *sc, cell_t *reg, uint32_t index, uint32_t reg_offset,
940050ea24SMichal Meloun uint64_t parent_offset, const char *org_name, bool special_gdbclk_reg);
950050ea24SMichal Meloun
960050ea24SMichal Meloun static int
ti_clkctrl_probe(device_t dev)970050ea24SMichal Meloun ti_clkctrl_probe(device_t dev)
980050ea24SMichal Meloun {
990050ea24SMichal Meloun if (!ofw_bus_status_okay(dev))
1000050ea24SMichal Meloun return (ENXIO);
1010050ea24SMichal Meloun
1020050ea24SMichal Meloun if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1030050ea24SMichal Meloun return (ENXIO);
1040050ea24SMichal Meloun
1050050ea24SMichal Meloun device_set_desc(dev, "TI clkctrl");
1060050ea24SMichal Meloun
1070050ea24SMichal Meloun return (BUS_PROBE_DEFAULT);
1080050ea24SMichal Meloun }
1090050ea24SMichal Meloun
1100050ea24SMichal Meloun static int
ti_clkctrl_attach(device_t dev)1110050ea24SMichal Meloun ti_clkctrl_attach(device_t dev)
1120050ea24SMichal Meloun {
1130050ea24SMichal Meloun struct ti_clkctrl_softc *sc;
1140050ea24SMichal Meloun phandle_t node;
1150050ea24SMichal Meloun cell_t *reg;
1160050ea24SMichal Meloun ssize_t numbytes_reg;
1170050ea24SMichal Meloun int num_reg, err, ti_clock_cells;
1180050ea24SMichal Meloun uint32_t index, reg_offset, reg_address;
1190050ea24SMichal Meloun const char *org_name;
1200050ea24SMichal Meloun uint64_t parent_offset;
1210050ea24SMichal Meloun uint8_t special_reg = NO_SPECIAL_REG;
1220050ea24SMichal Meloun
1230050ea24SMichal Meloun sc = device_get_softc(dev);
1240050ea24SMichal Meloun sc->dev = dev;
1250050ea24SMichal Meloun node = ofw_bus_get_node(dev);
1260050ea24SMichal Meloun
1270050ea24SMichal Meloun /* Sanity check */
1280050ea24SMichal Meloun err = OF_searchencprop(node, "#clock-cells",
1290050ea24SMichal Meloun &ti_clock_cells, sizeof(ti_clock_cells));
1300050ea24SMichal Meloun if (err == -1) {
1310050ea24SMichal Meloun device_printf(sc->dev, "Failed to get #clock-cells\n");
1320050ea24SMichal Meloun return (ENXIO);
1330050ea24SMichal Meloun }
1340050ea24SMichal Meloun
1350050ea24SMichal Meloun if (ti_clock_cells != 2) {
1360050ea24SMichal Meloun device_printf(sc->dev, "clock cells(%d) != 2\n",
1370050ea24SMichal Meloun ti_clock_cells);
1380050ea24SMichal Meloun return (ENXIO);
1390050ea24SMichal Meloun }
1400050ea24SMichal Meloun
1410050ea24SMichal Meloun /* Grab the content of reg properties */
1420050ea24SMichal Meloun numbytes_reg = OF_getproplen(node, "reg");
1430050ea24SMichal Meloun if (numbytes_reg == 0) {
1440050ea24SMichal Meloun device_printf(sc->dev, "reg property empty - check your devicetree\n");
1450050ea24SMichal Meloun return (ENXIO);
1460050ea24SMichal Meloun }
1470050ea24SMichal Meloun num_reg = numbytes_reg / sizeof(cell_t);
1480050ea24SMichal Meloun
1490050ea24SMichal Meloun reg = malloc(numbytes_reg, M_DEVBUF, M_WAITOK);
1500050ea24SMichal Meloun OF_getencprop(node, "reg", reg, numbytes_reg);
1510050ea24SMichal Meloun
1520050ea24SMichal Meloun /* Create clock domain */
1530050ea24SMichal Meloun sc->clkdom = clkdom_create(sc->dev);
1540050ea24SMichal Meloun if (sc->clkdom == NULL) {
1550050ea24SMichal Meloun free(reg, M_DEVBUF);
1560050ea24SMichal Meloun DPRINTF(sc->dev, "Failed to create clkdom\n");
1570050ea24SMichal Meloun return (ENXIO);
1580050ea24SMichal Meloun }
1590050ea24SMichal Meloun clkdom_set_ofw_mapper(sc->clkdom, clkctrl_ofw_map);
1600050ea24SMichal Meloun
1610050ea24SMichal Meloun /* Create clock nodes */
1620050ea24SMichal Meloun /* name */
1630050ea24SMichal Meloun clk_parse_ofw_clk_name(sc->dev, node, &org_name);
1640050ea24SMichal Meloun
1650050ea24SMichal Meloun /* Get parent range */
1660050ea24SMichal Meloun parent_offset = ti_omap4_cm_get_simplebus_base_host(device_get_parent(dev));
1670050ea24SMichal Meloun
1680050ea24SMichal Meloun /* Check if this is a clkctrl with special registers like gpio */
1690050ea24SMichal Meloun switch (ti_chip()) {
1700050ea24SMichal Meloun #ifdef SOC_OMAP4
1710050ea24SMichal Meloun case CHIP_OMAP_4:
1720050ea24SMichal Meloun /* FIXME: Todo */
1730050ea24SMichal Meloun break;
1740050ea24SMichal Meloun
1750050ea24SMichal Meloun #endif /* SOC_OMAP4 */
1760050ea24SMichal Meloun #ifdef SOC_TI_AM335X
1770050ea24SMichal Meloun /* Checkout TRM 8.1.12.1.29 - 8.1.12.31 and 8.1.12.2.3
1780050ea24SMichal Meloun * and the DTS.
1790050ea24SMichal Meloun */
1800050ea24SMichal Meloun case CHIP_AM335X:
1810050ea24SMichal Meloun if (strcmp(org_name, "l4ls-clkctrl@38") == 0)
1820050ea24SMichal Meloun special_reg = L4LS_CLKCTRL_38;
1830050ea24SMichal Meloun else if (strcmp(org_name, "l4-wkup-clkctrl@0") == 0)
1840050ea24SMichal Meloun special_reg = L4_WKUP_CLKCTRL_0;
1850050ea24SMichal Meloun break;
1860050ea24SMichal Meloun #endif /* SOC_TI_AM335X */
1870050ea24SMichal Meloun default:
1880050ea24SMichal Meloun break;
1890050ea24SMichal Meloun }
1900050ea24SMichal Meloun
1910050ea24SMichal Meloun /* reg property has a pair of (base address, length) */
1920050ea24SMichal Meloun for (index = 0; index < num_reg; index += 2) {
1930050ea24SMichal Meloun for (reg_offset = 0; reg_offset < reg[index+1]; reg_offset += sizeof(cell_t)) {
1940050ea24SMichal Meloun err = create_clkctrl(sc, reg, index, reg_offset, parent_offset,
1950050ea24SMichal Meloun org_name, false);
1960050ea24SMichal Meloun if (err)
1970050ea24SMichal Meloun goto cleanup;
1980050ea24SMichal Meloun
1990050ea24SMichal Meloun /* Create special clkctrl for GDBCLK in GPIO registers */
2000050ea24SMichal Meloun switch (special_reg) {
2010050ea24SMichal Meloun case NO_SPECIAL_REG:
2020050ea24SMichal Meloun break;
2030050ea24SMichal Meloun case L4LS_CLKCTRL_38:
2040050ea24SMichal Meloun reg_address = reg[index] + reg_offset-reg[0];
2050050ea24SMichal Meloun if (reg_address == 0x74 ||
2060050ea24SMichal Meloun reg_address == 0x78 ||
2070050ea24SMichal Meloun reg_address == 0x7C)
2080050ea24SMichal Meloun {
2090050ea24SMichal Meloun err = create_clkctrl(sc, reg, index, reg_offset,
2100050ea24SMichal Meloun parent_offset, org_name, true);
2110050ea24SMichal Meloun if (err)
2120050ea24SMichal Meloun goto cleanup;
2130050ea24SMichal Meloun }
2140050ea24SMichal Meloun break;
2150050ea24SMichal Meloun case L4_WKUP_CLKCTRL_0:
2160050ea24SMichal Meloun reg_address = reg[index] + reg_offset - reg[0];
2170050ea24SMichal Meloun if (reg_address == 0x8)
2180050ea24SMichal Meloun {
2190050ea24SMichal Meloun err = create_clkctrl(sc, reg, index, reg_offset,
2200050ea24SMichal Meloun parent_offset, org_name, true);
2210050ea24SMichal Meloun if (err)
2220050ea24SMichal Meloun goto cleanup;
2230050ea24SMichal Meloun }
2240050ea24SMichal Meloun break;
2250050ea24SMichal Meloun } /* switch (special_reg) */
2260050ea24SMichal Meloun } /* inner for */
2270050ea24SMichal Meloun } /* for */
2280050ea24SMichal Meloun
2290050ea24SMichal Meloun err = clkdom_finit(sc->clkdom);
2300050ea24SMichal Meloun if (err) {
2310050ea24SMichal Meloun DPRINTF(sc->dev, "Clk domain finit fails %x.\n", err);
2320050ea24SMichal Meloun err = ENXIO;
2330050ea24SMichal Meloun goto cleanup;
2340050ea24SMichal Meloun }
2350050ea24SMichal Meloun
2360050ea24SMichal Meloun cleanup:
2370050ea24SMichal Meloun OF_prop_free(__DECONST(char *, org_name));
2380050ea24SMichal Meloun
2390050ea24SMichal Meloun free(reg, M_DEVBUF);
2400050ea24SMichal Meloun
2410050ea24SMichal Meloun if (err)
2420050ea24SMichal Meloun return (err);
2430050ea24SMichal Meloun
2440050ea24SMichal Meloun return (bus_generic_attach(dev));
2450050ea24SMichal Meloun }
2460050ea24SMichal Meloun
2470050ea24SMichal Meloun static int
ti_clkctrl_detach(device_t dev)2480050ea24SMichal Meloun ti_clkctrl_detach(device_t dev)
2490050ea24SMichal Meloun {
2500050ea24SMichal Meloun return (EBUSY);
2510050ea24SMichal Meloun }
2520050ea24SMichal Meloun
2530050ea24SMichal Meloun /* modified version of default mapper from clk.c */
2540050ea24SMichal Meloun int
clkctrl_ofw_map(struct clkdom * clkdom,uint32_t ncells,phandle_t * cells,struct clknode ** clk)2550050ea24SMichal Meloun clkctrl_ofw_map(struct clkdom *clkdom, uint32_t ncells,
2560050ea24SMichal Meloun phandle_t *cells, struct clknode **clk) {
2570050ea24SMichal Meloun if (ncells == 0)
2580050ea24SMichal Meloun *clk = clknode_find_by_id(clkdom, 1);
2590050ea24SMichal Meloun else if (ncells == 1)
2600050ea24SMichal Meloun *clk = clknode_find_by_id(clkdom, cells[0]);
2610050ea24SMichal Meloun else if (ncells == 2) {
2620050ea24SMichal Meloun /* To avoid collision with other IDs just add one.
2630050ea24SMichal Meloun * All other registers has an offset of 4 from each other.
2640050ea24SMichal Meloun */
2650050ea24SMichal Meloun if (cells[1])
2660050ea24SMichal Meloun *clk = clknode_find_by_id(clkdom, cells[0]+1);
2670050ea24SMichal Meloun else
2680050ea24SMichal Meloun *clk = clknode_find_by_id(clkdom, cells[0]);
2690050ea24SMichal Meloun }
2700050ea24SMichal Meloun else
2710050ea24SMichal Meloun return (ERANGE);
2720050ea24SMichal Meloun
2730050ea24SMichal Meloun if (*clk == NULL)
2740050ea24SMichal Meloun return (ENXIO);
2750050ea24SMichal Meloun
2760050ea24SMichal Meloun return (0);
2770050ea24SMichal Meloun }
2780050ea24SMichal Meloun
2790050ea24SMichal Meloun static int
create_clkctrl(struct ti_clkctrl_softc * sc,cell_t * reg,uint32_t index,uint32_t reg_offset,uint64_t parent_offset,const char * org_name,bool special_gdbclk_reg)2800050ea24SMichal Meloun create_clkctrl(struct ti_clkctrl_softc *sc, cell_t *reg, uint32_t index, uint32_t reg_offset,
2810050ea24SMichal Meloun uint64_t parent_offset, const char *org_name, bool special_gdbclk_reg) {
2820050ea24SMichal Meloun struct ti_clk_clkctrl_def def;
2830050ea24SMichal Meloun char *name;
2840050ea24SMichal Meloun size_t name_len;
2850050ea24SMichal Meloun int err;
2860050ea24SMichal Meloun
2870050ea24SMichal Meloun name_len = strlen(org_name) + 1 + 5; /* 5 = _xxxx */
2880050ea24SMichal Meloun name = malloc(name_len, M_OFWPROP, M_WAITOK);
2890050ea24SMichal Meloun
2900050ea24SMichal Meloun /*
2910050ea24SMichal Meloun * Check out XX_CLKCTRL-INDEX(offset)-macro dance in
2920050ea24SMichal Meloun * sys/gnu/dts/dts/include/dt-bindings/clock/am3.h
2930050ea24SMichal Meloun * sys/gnu/dts/dts/include/dt-bindings/clock/am4.h
2940050ea24SMichal Meloun * sys/gnu/dts/dts/include/dt-bindings/clock/dra7.h
2950050ea24SMichal Meloun * reg[0] are in practice the same as the offset described in the dts.
2960050ea24SMichal Meloun */
2970050ea24SMichal Meloun /* special_gdbclk_reg are 0 or 1 */
2980050ea24SMichal Meloun def.clkdef.id = reg[index] + reg_offset - reg[0] + special_gdbclk_reg;
2990050ea24SMichal Meloun def.register_offset = parent_offset + reg[index] + reg_offset;
3000050ea24SMichal Meloun
3010050ea24SMichal Meloun /* Indicate this clkctrl is special and dont use IDLEST/MODULEMODE */
3020050ea24SMichal Meloun def.gdbclk = special_gdbclk_reg;
3030050ea24SMichal Meloun
3040050ea24SMichal Meloun /* Make up an uniq name in the namespace for each clkctrl */
3050050ea24SMichal Meloun snprintf(name, name_len, "%s_%x",
3060050ea24SMichal Meloun org_name, def.clkdef.id);
3070050ea24SMichal Meloun def.clkdef.name = (const char *) name;
3080050ea24SMichal Meloun
3090050ea24SMichal Meloun DPRINTF(sc->dev, "ti_clkctrl_attach: reg[%d]: %s %x\n",
3100050ea24SMichal Meloun index, def.clkdef.name, def.clkdef.id);
3110050ea24SMichal Meloun
3120050ea24SMichal Meloun /* No parent name */
3130050ea24SMichal Meloun def.clkdef.parent_cnt = 0;
3140050ea24SMichal Meloun
3150050ea24SMichal Meloun /* set flags */
3160050ea24SMichal Meloun def.clkdef.flags = 0x0;
3170050ea24SMichal Meloun
3180050ea24SMichal Meloun /* Register the clkctrl */
3190050ea24SMichal Meloun err = ti_clknode_clkctrl_register(sc->clkdom, &def);
3200050ea24SMichal Meloun if (err) {
3210050ea24SMichal Meloun DPRINTF(sc->dev,
3220050ea24SMichal Meloun "ti_clknode_clkctrl_register[%d:%d] failed %x\n",
3230050ea24SMichal Meloun index, reg_offset, err);
3240050ea24SMichal Meloun err = ENXIO;
3250050ea24SMichal Meloun }
3260050ea24SMichal Meloun OF_prop_free(name);
3270050ea24SMichal Meloun return (err);
3280050ea24SMichal Meloun }
3290050ea24SMichal Meloun
3300050ea24SMichal Meloun static device_method_t ti_clkctrl_methods[] = {
3310050ea24SMichal Meloun /* Device interface */
3320050ea24SMichal Meloun DEVMETHOD(device_probe, ti_clkctrl_probe),
3330050ea24SMichal Meloun DEVMETHOD(device_attach, ti_clkctrl_attach),
3340050ea24SMichal Meloun DEVMETHOD(device_detach, ti_clkctrl_detach),
3350050ea24SMichal Meloun
3360050ea24SMichal Meloun DEVMETHOD_END
3370050ea24SMichal Meloun };
3380050ea24SMichal Meloun
3390050ea24SMichal Meloun DEFINE_CLASS_0(ti_clkctrl, ti_clkctrl_driver, ti_clkctrl_methods,
3400050ea24SMichal Meloun sizeof(struct ti_clkctrl_softc));
3410050ea24SMichal Meloun
3428537e671SJohn Baldwin EARLY_DRIVER_MODULE(ti_clkctrl, simplebus, ti_clkctrl_driver, 0, 0,
3438537e671SJohn Baldwin BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
3440050ea24SMichal Meloun
3450050ea24SMichal Meloun MODULE_VERSION(ti_clkctrl, 1);
346