1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se> 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 ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * 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 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/conf.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/systm.h> 37 #include <sys/libkern.h> 38 #include <sys/types.h> 39 #include <sys/malloc.h> 40 41 #include <machine/bus.h> 42 #include <dev/fdt/simplebus.h> 43 44 #include <dev/extres/clk/clk_mux.h> 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include "clock_common.h" 49 50 #if 0 51 #define DPRINTF(dev, msg...) device_printf(dev, msg) 52 #else 53 #define DPRINTF(dev, msg...) 54 #endif 55 56 void 57 read_clock_cells(device_t dev, struct clock_cell_info *clk) { 58 ssize_t numbytes_clocks; 59 phandle_t node, parent, *cells; 60 int index, ncells, rv; 61 62 node = ofw_bus_get_node(dev); 63 64 /* Get names of parent clocks */ 65 numbytes_clocks = OF_getproplen(node, "clocks"); 66 clk->num_clock_cells = numbytes_clocks / sizeof(cell_t); 67 68 /* Allocate space and get clock cells content */ 69 /* clock_cells / clock_cells_ncells will be freed in 70 * find_parent_clock_names() 71 */ 72 clk->clock_cells = malloc(numbytes_clocks, M_DEVBUF, M_WAITOK|M_ZERO); 73 clk->clock_cells_ncells = malloc(clk->num_clock_cells*sizeof(uint8_t), 74 M_DEVBUF, M_WAITOK|M_ZERO); 75 OF_getencprop(node, "clocks", clk->clock_cells, numbytes_clocks); 76 77 /* Count number of clocks */ 78 clk->num_real_clocks = 0; 79 for (index = 0; index < clk->num_clock_cells; index++) { 80 rv = ofw_bus_parse_xref_list_alloc(node, "clocks", "#clock-cells", 81 clk->num_real_clocks, &parent, &ncells, &cells); 82 if (rv != 0) 83 continue; 84 85 if (cells != NULL) 86 OF_prop_free(cells); 87 88 clk->clock_cells_ncells[index] = ncells; 89 index += ncells; 90 clk->num_real_clocks++; 91 } 92 } 93 94 int 95 find_parent_clock_names(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) { 96 int index, clock_index, err; 97 bool found_all = true; 98 clk_t parent; 99 100 /* Figure out names */ 101 for (index = 0, clock_index = 0; index < clk->num_clock_cells; index++) { 102 /* Get name of parent clock */ 103 err = clk_get_by_ofw_index(dev, 0, clock_index, &parent); 104 if (err != 0) { 105 clock_index++; 106 found_all = false; 107 DPRINTF(dev, "Failed to find clock_cells[%d]=0x%x\n", 108 index, clk->clock_cells[index]); 109 110 index += clk->clock_cells_ncells[index]; 111 continue; 112 } 113 114 def->parent_names[clock_index] = clk_get_name(parent); 115 clk_release(parent); 116 117 DPRINTF(dev, "Found parent clock[%d/%d]: %s\n", 118 clock_index, clk->num_real_clocks, 119 def->parent_names[clock_index]); 120 121 clock_index++; 122 index += clk->clock_cells_ncells[index]; 123 } 124 125 if (!found_all) { 126 return 1; 127 } 128 129 free(clk->clock_cells, M_DEVBUF); 130 free(clk->clock_cells_ncells, M_DEVBUF); 131 return 0; 132 } 133 134 void 135 create_clkdef(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) { 136 def->id = 1; 137 138 clk_parse_ofw_clk_name(dev, ofw_bus_get_node(dev), &def->name); 139 140 DPRINTF(dev, "node name: %s\n", def->name); 141 142 def->parent_cnt = clk->num_real_clocks; 143 def->parent_names = malloc(clk->num_real_clocks*sizeof(char *), 144 M_OFWPROP, M_WAITOK); 145 } 146 void 147 free_clkdef(struct clknode_init_def *def) { 148 OF_prop_free(__DECONST(char *, def->name)); 149 OF_prop_free(def->parent_names); 150 } 151