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$
27 */
28
29/*
30 * Allwinner oscillator clock
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
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
43#include <dev/ofw/ofw_bus.h>
44#include <dev/ofw/ofw_bus_subr.h>
45
46#include <dev/extres/clk/clk_fixed.h>
47
48static int
49aw_oscclk_probe(device_t dev)
50{
51	if (!ofw_bus_status_okay(dev))
52		return (ENXIO);
53
54	if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-osc-clk"))
55		return (ENXIO);
56
57	device_set_desc(dev, "Allwinner Oscillator Clock");
58	return (BUS_PROBE_DEFAULT);
59}
60
61static int
62aw_oscclk_attach(device_t dev)
63{
64	struct clk_fixed_def def;
65	struct clkdom *clkdom;
66	phandle_t node;
67	uint32_t freq;
68	int error;
69
70	node = ofw_bus_get_node(dev);
71
72	if (OF_getencprop(node, "clock-frequency", &freq,  sizeof(freq)) <= 0) {
73		device_printf(dev, "missing clock-frequency property\n");
74		error = ENXIO;
75		goto fail;
76	}
77
78	clkdom = clkdom_create(dev);
79
80	memset(&def, 0, sizeof(def));
81	def.clkdef.id = 1;
82	def.freq = freq;
83	error = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
84	if (error != 0) {
85		device_printf(dev, "cannot parse clock name\n");
86		error = ENXIO;
87		goto fail;
88	}
89
90	error = clknode_fixed_register(clkdom, &def);
91	if (error != 0) {
92		device_printf(dev, "cannot register fixed clock\n");
93		error = ENXIO;
94		goto fail;
95	}
96
97	if (clkdom_finit(clkdom) != 0) {
98		device_printf(dev, "cannot finalize clkdom initialization\n");
99		error = ENXIO;
100		goto fail;
101	}
102
103	if (bootverbose)
104		clkdom_dump(clkdom);
105
106	OF_prop_free(__DECONST(char *, def.clkdef.name));
107
108	return (0);
109
110fail:
111	OF_prop_free(__DECONST(char *, def.clkdef.name));
112	return (error);
113}
114
115static device_method_t aw_oscclk_methods[] = {
116	/* Device interface */
117	DEVMETHOD(device_probe,		aw_oscclk_probe),
118	DEVMETHOD(device_attach,	aw_oscclk_attach),
119
120	DEVMETHOD_END
121};
122
123static driver_t aw_oscclk_driver = {
124	"aw_oscclk",
125	aw_oscclk_methods,
126	0,
127};
128
129static devclass_t aw_oscclk_devclass;
130
131EARLY_DRIVER_MODULE(aw_oscclk, simplebus, aw_oscclk_driver,
132    aw_oscclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
133