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 #include <sys/param.h>
30 #include <sys/conf.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/systm.h>
35 #include <sys/libkern.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38
39 #include <machine/bus.h>
40 #include <dev/fdt/simplebus.h>
41
42 #include <dev/extres/clk/clk_mux.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45
46 #include "clock_common.h"
47
48 #if 0
49 #define DPRINTF(dev, msg...) device_printf(dev, msg)
50 #else
51 #define DPRINTF(dev, msg...)
52 #endif
53
54 void
read_clock_cells(device_t dev,struct clock_cell_info * clk)55 read_clock_cells(device_t dev, struct clock_cell_info *clk) {
56 ssize_t numbytes_clocks;
57 phandle_t node, parent, *cells;
58 int index, ncells, rv;
59
60 node = ofw_bus_get_node(dev);
61
62 /* Get names of parent clocks */
63 numbytes_clocks = OF_getproplen(node, "clocks");
64 clk->num_clock_cells = numbytes_clocks / sizeof(cell_t);
65
66 /* Allocate space and get clock cells content */
67 /* clock_cells / clock_cells_ncells will be freed in
68 * find_parent_clock_names()
69 */
70 clk->clock_cells = malloc(numbytes_clocks, M_DEVBUF, M_WAITOK|M_ZERO);
71 clk->clock_cells_ncells = malloc(clk->num_clock_cells*sizeof(uint8_t),
72 M_DEVBUF, M_WAITOK|M_ZERO);
73 OF_getencprop(node, "clocks", clk->clock_cells, numbytes_clocks);
74
75 /* Count number of clocks */
76 clk->num_real_clocks = 0;
77 for (index = 0; index < clk->num_clock_cells; index++) {
78 rv = ofw_bus_parse_xref_list_alloc(node, "clocks", "#clock-cells",
79 clk->num_real_clocks, &parent, &ncells, &cells);
80 if (rv != 0)
81 continue;
82
83 if (cells != NULL)
84 OF_prop_free(cells);
85
86 clk->clock_cells_ncells[index] = ncells;
87 index += ncells;
88 clk->num_real_clocks++;
89 }
90 }
91
92 int
find_parent_clock_names(device_t dev,struct clock_cell_info * clk,struct clknode_init_def * def)93 find_parent_clock_names(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) {
94 int index, clock_index, err;
95 bool found_all = true;
96 clk_t parent;
97
98 /* Figure out names */
99 for (index = 0, clock_index = 0; index < clk->num_clock_cells; index++) {
100 /* Get name of parent clock */
101 err = clk_get_by_ofw_index(dev, 0, clock_index, &parent);
102 if (err != 0) {
103 clock_index++;
104 found_all = false;
105 DPRINTF(dev, "Failed to find clock_cells[%d]=0x%x\n",
106 index, clk->clock_cells[index]);
107
108 index += clk->clock_cells_ncells[index];
109 continue;
110 }
111
112 def->parent_names[clock_index] = clk_get_name(parent);
113 clk_release(parent);
114
115 DPRINTF(dev, "Found parent clock[%d/%d]: %s\n",
116 clock_index, clk->num_real_clocks,
117 def->parent_names[clock_index]);
118
119 clock_index++;
120 index += clk->clock_cells_ncells[index];
121 }
122
123 if (!found_all) {
124 return 1;
125 }
126
127 free(clk->clock_cells, M_DEVBUF);
128 free(clk->clock_cells_ncells, M_DEVBUF);
129 return 0;
130 }
131
132 void
create_clkdef(device_t dev,struct clock_cell_info * clk,struct clknode_init_def * def)133 create_clkdef(device_t dev, struct clock_cell_info *clk, struct clknode_init_def *def) {
134 def->id = 1;
135
136 clk_parse_ofw_clk_name(dev, ofw_bus_get_node(dev), &def->name);
137
138 DPRINTF(dev, "node name: %s\n", def->name);
139
140 def->parent_cnt = clk->num_real_clocks;
141 def->parent_names = malloc(clk->num_real_clocks*sizeof(char *),
142 M_OFWPROP, M_WAITOK);
143 }
144 void
free_clkdef(struct clknode_init_def * def)145 free_clkdef(struct clknode_init_def *def) {
146 OF_prop_free(__DECONST(char *, def->name));
147 OF_prop_free(def->parent_names);
148 }
149