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