10050ea24SMichal Meloun /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
30050ea24SMichal Meloun *
40050ea24SMichal Meloun * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
50050ea24SMichal Meloun *
60050ea24SMichal Meloun * Redistribution and use in source and binary forms, with or without
70050ea24SMichal Meloun * modification, are permitted provided that the following conditions
80050ea24SMichal Meloun * are met:
90050ea24SMichal Meloun * 1. Redistributions of source code must retain the above copyright
100050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer.
110050ea24SMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright
120050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer in the
130050ea24SMichal Meloun * documentation and/or other materials provided with the distribution.
140050ea24SMichal Meloun *
150050ea24SMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
160050ea24SMichal Meloun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
170050ea24SMichal Meloun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
180050ea24SMichal Meloun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
190050ea24SMichal Meloun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
200050ea24SMichal Meloun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
210050ea24SMichal Meloun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
220050ea24SMichal Meloun * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
230050ea24SMichal Meloun * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240050ea24SMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250050ea24SMichal Meloun * SUCH DAMAGE.
260050ea24SMichal Meloun */
270050ea24SMichal Meloun
280050ea24SMichal Meloun #include <sys/param.h>
290050ea24SMichal Meloun #include <sys/conf.h>
300050ea24SMichal Meloun #include <sys/bus.h>
310050ea24SMichal Meloun #include <sys/kernel.h>
320050ea24SMichal Meloun #include <sys/module.h>
330050ea24SMichal Meloun #include <sys/systm.h>
340050ea24SMichal Meloun #include <sys/libkern.h>
350050ea24SMichal Meloun #include <sys/types.h>
360050ea24SMichal Meloun #include <sys/malloc.h>
370050ea24SMichal Meloun
380050ea24SMichal Meloun #include <machine/bus.h>
390050ea24SMichal Meloun #include <dev/fdt/simplebus.h>
400050ea24SMichal Meloun
41*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
420050ea24SMichal Meloun #include <dev/ofw/ofw_bus.h>
430050ea24SMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
440050ea24SMichal Meloun
450050ea24SMichal Meloun #include "clock_common.h"
460050ea24SMichal Meloun
470050ea24SMichal Meloun #if 0
480050ea24SMichal Meloun #define DPRINTF(dev, msg...) device_printf(dev, msg)
490050ea24SMichal Meloun #else
500050ea24SMichal Meloun #define DPRINTF(dev, msg...)
510050ea24SMichal Meloun #endif
520050ea24SMichal Meloun
530050ea24SMichal Meloun void
read_clock_cells(device_t dev,struct clock_cell_info * clk)540050ea24SMichal Meloun read_clock_cells(device_t dev, struct clock_cell_info *clk) {
550050ea24SMichal Meloun ssize_t numbytes_clocks;
560050ea24SMichal Meloun phandle_t node, parent, *cells;
570050ea24SMichal Meloun int index, ncells, rv;
580050ea24SMichal Meloun
590050ea24SMichal Meloun node = ofw_bus_get_node(dev);
600050ea24SMichal Meloun
610050ea24SMichal Meloun /* Get names of parent clocks */
620050ea24SMichal Meloun numbytes_clocks = OF_getproplen(node, "clocks");
630050ea24SMichal Meloun clk->num_clock_cells = numbytes_clocks / sizeof(cell_t);
640050ea24SMichal Meloun
650050ea24SMichal Meloun /* Allocate space and get clock cells content */
660050ea24SMichal Meloun /* clock_cells / clock_cells_ncells will be freed in
670050ea24SMichal Meloun * find_parent_clock_names()
680050ea24SMichal Meloun */
690050ea24SMichal Meloun clk->clock_cells = malloc(numbytes_clocks, M_DEVBUF, M_WAITOK|M_ZERO);
700050ea24SMichal Meloun clk->clock_cells_ncells = malloc(clk->num_clock_cells*sizeof(uint8_t),
710050ea24SMichal Meloun M_DEVBUF, M_WAITOK|M_ZERO);
720050ea24SMichal Meloun OF_getencprop(node, "clocks", clk->clock_cells, numbytes_clocks);
730050ea24SMichal Meloun
740050ea24SMichal Meloun /* Count number of clocks */
750050ea24SMichal Meloun clk->num_real_clocks = 0;
760050ea24SMichal Meloun for (index = 0; index < clk->num_clock_cells; index++) {
770050ea24SMichal Meloun rv = ofw_bus_parse_xref_list_alloc(node, "clocks", "#clock-cells",
780050ea24SMichal Meloun clk->num_real_clocks, &parent, &ncells, &cells);
790050ea24SMichal Meloun if (rv != 0)
800050ea24SMichal Meloun continue;
810050ea24SMichal Meloun
820050ea24SMichal Meloun if (cells != NULL)
830050ea24SMichal Meloun OF_prop_free(cells);
840050ea24SMichal Meloun
850050ea24SMichal Meloun clk->clock_cells_ncells[index] = ncells;
860050ea24SMichal Meloun index += ncells;
870050ea24SMichal Meloun clk->num_real_clocks++;
880050ea24SMichal Meloun }
890050ea24SMichal Meloun }
900050ea24SMichal Meloun
910050ea24SMichal Meloun int
find_parent_clock_names(device_t dev,struct clock_cell_info * clk,struct clknode_init_def * def)920050ea24SMichal Meloun find_parent_clock_names(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) {
930050ea24SMichal Meloun int index, clock_index, err;
940050ea24SMichal Meloun bool found_all = true;
950050ea24SMichal Meloun clk_t parent;
960050ea24SMichal Meloun
970050ea24SMichal Meloun /* Figure out names */
980050ea24SMichal Meloun for (index = 0, clock_index = 0; index < clk->num_clock_cells; index++) {
990050ea24SMichal Meloun /* Get name of parent clock */
1000050ea24SMichal Meloun err = clk_get_by_ofw_index(dev, 0, clock_index, &parent);
1010050ea24SMichal Meloun if (err != 0) {
1020050ea24SMichal Meloun clock_index++;
1030050ea24SMichal Meloun found_all = false;
1040050ea24SMichal Meloun DPRINTF(dev, "Failed to find clock_cells[%d]=0x%x\n",
1050050ea24SMichal Meloun index, clk->clock_cells[index]);
1060050ea24SMichal Meloun
1070050ea24SMichal Meloun index += clk->clock_cells_ncells[index];
1080050ea24SMichal Meloun continue;
1090050ea24SMichal Meloun }
1100050ea24SMichal Meloun
1110050ea24SMichal Meloun def->parent_names[clock_index] = clk_get_name(parent);
1120050ea24SMichal Meloun clk_release(parent);
1130050ea24SMichal Meloun
1140050ea24SMichal Meloun DPRINTF(dev, "Found parent clock[%d/%d]: %s\n",
1150050ea24SMichal Meloun clock_index, clk->num_real_clocks,
1160050ea24SMichal Meloun def->parent_names[clock_index]);
1170050ea24SMichal Meloun
1180050ea24SMichal Meloun clock_index++;
1190050ea24SMichal Meloun index += clk->clock_cells_ncells[index];
1200050ea24SMichal Meloun }
1210050ea24SMichal Meloun
1220050ea24SMichal Meloun if (!found_all) {
1230050ea24SMichal Meloun return 1;
1240050ea24SMichal Meloun }
1250050ea24SMichal Meloun
1260050ea24SMichal Meloun free(clk->clock_cells, M_DEVBUF);
1270050ea24SMichal Meloun free(clk->clock_cells_ncells, M_DEVBUF);
1280050ea24SMichal Meloun return 0;
1290050ea24SMichal Meloun }
1300050ea24SMichal Meloun
1310050ea24SMichal Meloun void
create_clkdef(device_t dev,struct clock_cell_info * clk,struct clknode_init_def * def)1320050ea24SMichal Meloun create_clkdef(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) {
1330050ea24SMichal Meloun def->id = 1;
1340050ea24SMichal Meloun
1350050ea24SMichal Meloun clk_parse_ofw_clk_name(dev, ofw_bus_get_node(dev), &def->name);
1360050ea24SMichal Meloun
1370050ea24SMichal Meloun DPRINTF(dev, "node name: %s\n", def->name);
1380050ea24SMichal Meloun
1390050ea24SMichal Meloun def->parent_cnt = clk->num_real_clocks;
1400050ea24SMichal Meloun def->parent_names = malloc(clk->num_real_clocks*sizeof(char *),
1410050ea24SMichal Meloun M_OFWPROP, M_WAITOK);
1420050ea24SMichal Meloun }
1430050ea24SMichal Meloun void
free_clkdef(struct clknode_init_def * def)1440050ea24SMichal Meloun free_clkdef(struct clknode_init_def *def) {
1450050ea24SMichal Meloun OF_prop_free(__DECONST(char *, def->name));
1460050ea24SMichal Meloun OF_prop_free(def->parent_names);
1470050ea24SMichal Meloun }
148