1297627Sjmcneill/*-
2297627Sjmcneill * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3297627Sjmcneill * All rights reserved.
4297627Sjmcneill *
5297627Sjmcneill * Redistribution and use in source and binary forms, with or without
6297627Sjmcneill * modification, are permitted provided that the following conditions
7297627Sjmcneill * are met:
8297627Sjmcneill * 1. Redistributions of source code must retain the above copyright
9297627Sjmcneill *    notice, this list of conditions and the following disclaimer.
10297627Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
11297627Sjmcneill *    notice, this list of conditions and the following disclaimer in the
12297627Sjmcneill *    documentation and/or other materials provided with the distribution.
13297627Sjmcneill *
14297627Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15297627Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16297627Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17297627Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18297627Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19297627Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20297627Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21297627Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22297627Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23297627Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24297627Sjmcneill * SUCH DAMAGE.
25297627Sjmcneill *
26297627Sjmcneill * $FreeBSD$
27297627Sjmcneill */
28297627Sjmcneill
29297627Sjmcneill/*
30297627Sjmcneill * Allwinner oscillator clock
31297627Sjmcneill */
32297627Sjmcneill
33297627Sjmcneill#include <sys/cdefs.h>
34297627Sjmcneill__FBSDID("$FreeBSD$");
35297627Sjmcneill
36297627Sjmcneill#include <sys/param.h>
37297627Sjmcneill#include <sys/systm.h>
38297627Sjmcneill#include <sys/bus.h>
39297627Sjmcneill#include <sys/rman.h>
40297627Sjmcneill#include <sys/kernel.h>
41297627Sjmcneill#include <sys/module.h>
42297627Sjmcneill
43297627Sjmcneill#include <dev/ofw/ofw_bus.h>
44297627Sjmcneill#include <dev/ofw/ofw_bus_subr.h>
45297627Sjmcneill
46297627Sjmcneill#include <dev/extres/clk/clk_fixed.h>
47297627Sjmcneill
48297627Sjmcneillstatic int
49297627Sjmcneillaw_oscclk_probe(device_t dev)
50297627Sjmcneill{
51297627Sjmcneill	if (!ofw_bus_status_okay(dev))
52297627Sjmcneill		return (ENXIO);
53297627Sjmcneill
54297627Sjmcneill	if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-osc-clk"))
55297627Sjmcneill		return (ENXIO);
56297627Sjmcneill
57297627Sjmcneill	device_set_desc(dev, "Allwinner Oscillator Clock");
58297627Sjmcneill	return (BUS_PROBE_DEFAULT);
59297627Sjmcneill}
60297627Sjmcneill
61297627Sjmcneillstatic int
62297627Sjmcneillaw_oscclk_attach(device_t dev)
63297627Sjmcneill{
64297627Sjmcneill	struct clk_fixed_def def;
65297627Sjmcneill	struct clkdom *clkdom;
66297627Sjmcneill	phandle_t node;
67297627Sjmcneill	uint32_t freq;
68297627Sjmcneill	int error;
69297627Sjmcneill
70297627Sjmcneill	node = ofw_bus_get_node(dev);
71297627Sjmcneill
72297627Sjmcneill	if (OF_getencprop(node, "clock-frequency", &freq,  sizeof(freq)) <= 0) {
73297627Sjmcneill		device_printf(dev, "missing clock-frequency property\n");
74297627Sjmcneill		error = ENXIO;
75297627Sjmcneill		goto fail;
76297627Sjmcneill	}
77297627Sjmcneill
78297627Sjmcneill	clkdom = clkdom_create(dev);
79297627Sjmcneill
80297627Sjmcneill	memset(&def, 0, sizeof(def));
81297627Sjmcneill	def.clkdef.id = 1;
82297627Sjmcneill	def.freq = freq;
83297627Sjmcneill	error = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
84297627Sjmcneill	if (error != 0) {
85297627Sjmcneill		device_printf(dev, "cannot parse clock name\n");
86297627Sjmcneill		error = ENXIO;
87297627Sjmcneill		goto fail;
88297627Sjmcneill	}
89297627Sjmcneill
90297627Sjmcneill	error = clknode_fixed_register(clkdom, &def);
91297627Sjmcneill	if (error != 0) {
92297627Sjmcneill		device_printf(dev, "cannot register fixed clock\n");
93297627Sjmcneill		error = ENXIO;
94297627Sjmcneill		goto fail;
95297627Sjmcneill	}
96297627Sjmcneill
97297627Sjmcneill	if (clkdom_finit(clkdom) != 0) {
98297627Sjmcneill		device_printf(dev, "cannot finalize clkdom initialization\n");
99297627Sjmcneill		error = ENXIO;
100297627Sjmcneill		goto fail;
101297627Sjmcneill	}
102297627Sjmcneill
103297627Sjmcneill	if (bootverbose)
104297627Sjmcneill		clkdom_dump(clkdom);
105297627Sjmcneill
106299703Sgonzo	OF_prop_free(__DECONST(char *, def.clkdef.name));
107297627Sjmcneill
108297627Sjmcneill	return (0);
109297627Sjmcneill
110297627Sjmcneillfail:
111299703Sgonzo	OF_prop_free(__DECONST(char *, def.clkdef.name));
112297627Sjmcneill	return (error);
113297627Sjmcneill}
114297627Sjmcneill
115297627Sjmcneillstatic device_method_t aw_oscclk_methods[] = {
116297627Sjmcneill	/* Device interface */
117297627Sjmcneill	DEVMETHOD(device_probe,		aw_oscclk_probe),
118297627Sjmcneill	DEVMETHOD(device_attach,	aw_oscclk_attach),
119297627Sjmcneill
120297627Sjmcneill	DEVMETHOD_END
121297627Sjmcneill};
122297627Sjmcneill
123297627Sjmcneillstatic driver_t aw_oscclk_driver = {
124297627Sjmcneill	"aw_oscclk",
125297627Sjmcneill	aw_oscclk_methods,
126297627Sjmcneill	0,
127297627Sjmcneill};
128297627Sjmcneill
129297627Sjmcneillstatic devclass_t aw_oscclk_devclass;
130297627Sjmcneill
131297627SjmcneillEARLY_DRIVER_MODULE(aw_oscclk, simplebus, aw_oscclk_driver,
132297627Sjmcneill    aw_oscclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
133