aw_cpuclk.c revision 308324
1/*-
2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/arm/allwinner/clk/aw_cpuclk.c 308324 2016-11-05 04:17:32Z mmel $
27 */
28
29/*
30 * Allwinner CPU clock
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/clk/aw_cpuclk.c 308324 2016-11-05 04:17:32Z mmel $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bus.h>
39#include <sys/rman.h>
40#include <sys/kernel.h>
41#include <sys/module.h>
42#include <machine/bus.h>
43
44#include <dev/ofw/ofw_bus.h>
45#include <dev/ofw/ofw_bus_subr.h>
46#include <dev/ofw/ofw_subr.h>
47
48#include <dev/extres/clk/clk_mux.h>
49
50#define	CPU_CLK_SRC_SEL_WIDTH	2
51#define	CPU_CLK_SRC_SEL_SHIFT	16
52
53static int
54aw_cpuclk_probe(device_t dev)
55{
56	if (!ofw_bus_status_okay(dev))
57		return (ENXIO);
58
59	if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-cpu-clk"))
60		return (ENXIO);
61
62	device_set_desc(dev, "Allwinner CPU Clock");
63	return (BUS_PROBE_DEFAULT);
64}
65
66static int
67aw_cpuclk_attach(device_t dev)
68{
69	struct clk_mux_def def;
70	struct clkdom *clkdom;
71	bus_addr_t paddr;
72	bus_size_t psize;
73	phandle_t node;
74	int error, ncells, i;
75	clk_t clk;
76
77	node = ofw_bus_get_node(dev);
78
79	if (ofw_reg_to_paddr(node, 0, &paddr, &psize, NULL) != 0) {
80		device_printf(dev, "cannot parse 'reg' property\n");
81		return (ENXIO);
82	}
83
84	error = ofw_bus_parse_xref_list_get_length(node, "clocks",
85	    "#clock-cells", &ncells);
86	if (error != 0) {
87		device_printf(dev, "cannot get clock count\n");
88		return (error);
89	}
90
91	clkdom = clkdom_create(dev);
92
93	memset(&def, 0, sizeof(def));
94	def.clkdef.id = 1;
95	def.clkdef.parent_names = malloc(sizeof(char *) * ncells, M_OFWPROP,
96	    M_WAITOK);
97	for (i = 0; i < ncells; i++) {
98		error = clk_get_by_ofw_index(dev, 0, i, &clk);
99		if (error != 0) {
100			device_printf(dev, "cannot get clock %d\n", i);
101			goto fail;
102		}
103		def.clkdef.parent_names[i] = clk_get_name(clk);
104		clk_release(clk);
105	}
106	def.clkdef.parent_cnt = ncells;
107	def.offset = paddr;
108	def.shift = CPU_CLK_SRC_SEL_SHIFT;
109	def.width = CPU_CLK_SRC_SEL_WIDTH;
110
111	error = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
112	if (error != 0) {
113		device_printf(dev, "cannot parse clock name\n");
114		error = ENXIO;
115		goto fail;
116	}
117
118	error = clknode_mux_register(clkdom, &def);
119	if (error != 0) {
120		device_printf(dev, "cannot register mux clock\n");
121		error = ENXIO;
122		goto fail;
123	}
124
125	if (clkdom_finit(clkdom) != 0) {
126		device_printf(dev, "cannot finalize clkdom initialization\n");
127		error = ENXIO;
128		goto fail;
129	}
130
131	OF_prop_free(__DECONST(char *, def.clkdef.parent_names));
132	OF_prop_free(__DECONST(char *, def.clkdef.name));
133
134	if (bootverbose)
135		clkdom_dump(clkdom);
136
137	return (0);
138
139fail:
140	OF_prop_free(__DECONST(char *, def.clkdef.name));
141	return (error);
142}
143
144static device_method_t aw_cpuclk_methods[] = {
145	/* Device interface */
146	DEVMETHOD(device_probe,		aw_cpuclk_probe),
147	DEVMETHOD(device_attach,	aw_cpuclk_attach),
148
149	DEVMETHOD_END
150};
151
152static driver_t aw_cpuclk_driver = {
153	"aw_cpuclk",
154	aw_cpuclk_methods,
155	0
156};
157
158static devclass_t aw_cpuclk_devclass;
159
160EARLY_DRIVER_MODULE(aw_cpuclk, simplebus, aw_cpuclk_driver,
161    aw_cpuclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
162