xref: /freebsd/sys/arm/ti/am335x/am335x_pwmss.c (revision 64a0982bee3db2236df43357e70ce8dddbc21d48)
1 /*-
2  * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, 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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/resource.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 
42 #include <machine/bus.h>
43 
44 #include <dev/fdt/simplebus.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <arm/ti/ti_prcm.h>
50 #include <arm/ti/ti_hwmods.h>
51 #include <arm/ti/ti_scm.h>
52 
53 #include "am335x_pwm.h"
54 #include "am335x_scm.h"
55 
56 #define	PWMSS_IDVER		0x00
57 #define	PWMSS_SYSCONFIG		0x04
58 #define	PWMSS_CLKCONFIG		0x08
59 #define		CLKCONFIG_EPWMCLK_EN	(1 << 8)
60 #define	PWMSS_CLKSTATUS		0x0C
61 
62 static device_probe_t am335x_pwmss_probe;
63 static device_attach_t am335x_pwmss_attach;
64 static device_detach_t am335x_pwmss_detach;
65 
66 struct am335x_pwmss_softc {
67 	struct simplebus_softc	sc_simplebus;
68 	device_t		sc_dev;
69 	clk_ident_t		sc_clk;
70 };
71 
72 static device_method_t am335x_pwmss_methods[] = {
73 	DEVMETHOD(device_probe,		am335x_pwmss_probe),
74 	DEVMETHOD(device_attach,	am335x_pwmss_attach),
75 	DEVMETHOD(device_detach,	am335x_pwmss_detach),
76 
77 	DEVMETHOD_END
78 };
79 
80 static int
81 am335x_pwmss_probe(device_t dev)
82 {
83 
84 	if (!ofw_bus_status_okay(dev))
85 		return (ENXIO);
86 
87 	if (!ofw_bus_is_compatible(dev, "ti,am33xx-pwmss"))
88 		return (ENXIO);
89 
90 	device_set_desc(dev, "AM335x PWM");
91 
92 	return (BUS_PROBE_DEFAULT);
93 }
94 
95 static int
96 am335x_pwmss_attach(device_t dev)
97 {
98 	struct am335x_pwmss_softc *sc;
99 	uint32_t reg, id;
100 	phandle_t node;
101 
102 	sc = device_get_softc(dev);
103 	sc->sc_dev = dev;
104 
105 	sc->sc_clk = ti_hwmods_get_clock(dev);
106 	if (sc->sc_clk == INVALID_CLK_IDENT) {
107 		device_printf(dev, "failed to get device id based on ti,hwmods\n");
108 		return (EINVAL);
109 	}
110 
111 	ti_prcm_clk_enable(sc->sc_clk);
112 	ti_scm_reg_read_4(SCM_PWMSS_CTRL, &reg);
113 	switch (sc->sc_clk) {
114 		case PWMSS0_CLK:
115 			id = 0;
116 			break;
117 		case PWMSS1_CLK:
118 			id = 1;
119 			break;
120 
121 		case PWMSS2_CLK:
122 			id = 2;
123 			break;
124 		default:
125 			device_printf(dev, "unknown pwmss clock id: %d\n", sc->sc_clk);
126 			return (EINVAL);
127 	}
128 	reg |= (1 << id);
129 	ti_scm_reg_write_4(SCM_PWMSS_CTRL, reg);
130 
131 	node = ofw_bus_get_node(dev);
132 
133 	if (node == -1)
134 		return (ENXIO);
135 
136 	simplebus_init(dev, node);
137 
138 	/*
139 	 * Allow devices to identify.
140 	 */
141 	bus_generic_probe(dev);
142 
143 	/*
144 	 * Now walk the OFW tree and attach top-level devices.
145 	 */
146 	for (node = OF_child(node); node > 0; node = OF_peer(node))
147 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
148 
149 	return (bus_generic_attach(dev));
150 }
151 
152 static int
153 am335x_pwmss_detach(device_t dev)
154 {
155 
156 	return (0);
157 }
158 
159 DEFINE_CLASS_1(am335x_pwmss, am335x_pwmss_driver, am335x_pwmss_methods,
160     sizeof(struct am335x_pwmss_softc), simplebus_driver);
161 static devclass_t am335x_pwmss_devclass;
162 DRIVER_MODULE(am335x_pwmss, simplebus, am335x_pwmss_driver, am335x_pwmss_devclass, 0, 0);
163 MODULE_VERSION(am335x_pwmss, 1);
164