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_codecclk.c 308324 2016-11-05 04:17:32Z mmel $
27 */
28
29/*
30 * Allwinner CODEC clock
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/clk/aw_codecclk.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_gate.h>
49#include <dev/extres/hwreset/hwreset.h>
50
51#include "clkdev_if.h"
52
53#define	SCLK_GATING_SHIFT	31
54
55static struct ofw_compat_data compat_data[] = {
56	{ "allwinner,sun4i-a10-codec-clk",	1 },
57	{ NULL, 0 }
58};
59
60static int
61aw_codecclk_create(device_t dev, bus_addr_t paddr, struct clkdom *clkdom,
62    const char *pclkname, const char *clkname, int index)
63{
64	const char *parent_names[1] = { pclkname };
65	struct clk_gate_def def;
66
67	memset(&def, 0, sizeof(def));
68	def.clkdef.id = index;
69	def.clkdef.name = clkname;
70	def.clkdef.parent_names = parent_names;
71	def.clkdef.parent_cnt = 1;
72	def.offset = paddr;
73	def.shift = SCLK_GATING_SHIFT;
74	def.mask = 1;
75	def.on_value = 1;
76	def.off_value = 0;
77
78	return (clknode_gate_register(clkdom, &def));
79}
80
81static int
82aw_codecclk_probe(device_t dev)
83{
84	if (!ofw_bus_status_okay(dev))
85		return (ENXIO);
86
87	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
88		return (ENXIO);
89
90	device_set_desc(dev, "Allwinner CODEC Clock");
91	return (BUS_PROBE_DEFAULT);
92}
93
94static int
95aw_codecclk_attach(device_t dev)
96{
97	struct clkdom *clkdom;
98	const char **names;
99	int nout, error;
100	uint32_t *indices;
101	clk_t clk_parent;
102	bus_addr_t paddr;
103	bus_size_t psize;
104	phandle_t node;
105
106	node = ofw_bus_get_node(dev);
107	indices = NULL;
108
109	if (ofw_reg_to_paddr(node, 0, &paddr, &psize, NULL) != 0) {
110		device_printf(dev, "cannot parse 'reg' property\n");
111		return (ENXIO);
112	}
113
114	clkdom = clkdom_create(dev);
115
116	nout = clk_parse_ofw_out_names(dev, node, &names, &indices);
117	if (nout != 1) {
118		device_printf(dev, "must have exactly one output clock\n");
119		error = ENOENT;
120		goto fail;
121	}
122
123	error = clk_get_by_ofw_index(dev, 0, 0, &clk_parent);
124	if (error != 0) {
125		device_printf(dev, "cannot parse clock parent\n");
126		return (ENXIO);
127	}
128
129	error = aw_codecclk_create(dev, paddr, clkdom,
130	    clk_get_name(clk_parent), names[0], 1);
131
132	if (clkdom_finit(clkdom) != 0) {
133		device_printf(dev, "cannot finalize clkdom initialization\n");
134		error = ENXIO;
135		goto fail;
136	}
137
138	if (bootverbose)
139		clkdom_dump(clkdom);
140
141	return (0);
142
143fail:
144	return (error);
145}
146
147static device_method_t aw_codecclk_methods[] = {
148	/* Device interface */
149	DEVMETHOD(device_probe,		aw_codecclk_probe),
150	DEVMETHOD(device_attach,	aw_codecclk_attach),
151
152	DEVMETHOD_END
153};
154
155static driver_t aw_codecclk_driver = {
156	"aw_codecclk",
157	aw_codecclk_methods,
158	0
159};
160
161static devclass_t aw_codecclk_devclass;
162
163EARLY_DRIVER_MODULE(aw_codecclk, simplebus, aw_codecclk_driver,
164    aw_codecclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
165