xref: /freebsd/sys/arm/ti/am335x/am335x_pwmss.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/limits.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/resource.h>
37 #include <sys/rman.h>
38 #include <sys/sysctl.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/fdt/simplebus.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <arm/ti/ti_sysc.h>
48 
49 #include <dev/extres/syscon/syscon.h>
50 #include "syscon_if.h"
51 
52 #include "am335x_pwm.h"
53 #include "am335x_scm.h"
54 
55 #define	PWMSS_IDVER		0x00
56 #define	PWMSS_SYSCONFIG		0x04
57 #define	PWMSS_CLKCONFIG		0x08
58 #define		CLKCONFIG_EPWMCLK_EN	(1 << 8)
59 #define	PWMSS_CLKSTATUS		0x0C
60 
61 /* TRM chapter 2 memory map table 2-3 + VER register location */
62 #define PWMSS_REV_0		0x0000
63 #define PWMSS_REV_1		0x2000
64 #define PWMSS_REV_2		0x4000
65 
66 static device_probe_t am335x_pwmss_probe;
67 static device_attach_t am335x_pwmss_attach;
68 static device_detach_t am335x_pwmss_detach;
69 
70 struct am335x_pwmss_softc {
71 	struct simplebus_softc	sc_simplebus;
72 	device_t		sc_dev;
73 	struct syscon           *syscon;
74 };
75 
76 static device_method_t am335x_pwmss_methods[] = {
77 	DEVMETHOD(device_probe,		am335x_pwmss_probe),
78 	DEVMETHOD(device_attach,	am335x_pwmss_attach),
79 	DEVMETHOD(device_detach,	am335x_pwmss_detach),
80 
81 	DEVMETHOD_END
82 };
83 
84 static int
85 am335x_pwmss_probe(device_t dev)
86 {
87 
88 	if (!ofw_bus_status_okay(dev))
89 		return (ENXIO);
90 
91 	if (!ofw_bus_is_compatible(dev, "ti,am33xx-pwmss"))
92 		return (ENXIO);
93 
94 	device_set_desc(dev, "AM335x PWM");
95 
96 	return (BUS_PROBE_DEFAULT);
97 }
98 
99 static int
100 am335x_pwmss_attach(device_t dev)
101 {
102 	struct am335x_pwmss_softc *sc;
103 	uint32_t reg, id;
104 	uint64_t rev_address;
105 	phandle_t node, opp_table;
106 
107 	sc = device_get_softc(dev);
108 	sc->sc_dev = dev;
109 
110 	/* FIXME: For now; Go and kidnap syscon from opp-table */
111 	opp_table = OF_finddevice("/opp-table");
112 	if (opp_table == -1) {
113 		device_printf(dev, "Cant find /opp-table\n");
114 		return (ENXIO);
115 	}
116 	if (!OF_hasprop(opp_table, "syscon")) {
117 		device_printf(dev, "/opp-table doesnt have required syscon property\n");
118 		return (ENXIO);
119 	}
120 	if (syscon_get_by_ofw_property(dev, opp_table, "syscon", &sc->syscon) != 0) {
121 		device_printf(dev, "Failed to get syscon\n");
122 		return (ENXIO);
123 	}
124 
125 	ti_sysc_clock_enable(device_get_parent(dev));
126 
127 	rev_address = ti_sysc_get_rev_address(device_get_parent(dev));
128 	switch (rev_address) {
129 	case PWMSS_REV_0:
130 		id = 0;
131 		break;
132 	case PWMSS_REV_1:
133 		id = 1;
134 		break;
135 	case PWMSS_REV_2:
136 		id = 2;
137 		break;
138 	}
139 
140 	reg = SYSCON_READ_4(sc->syscon, SCM_PWMSS_CTRL);
141 	reg |= (1 << id);
142 	SYSCON_WRITE_4(sc->syscon, SCM_PWMSS_CTRL, reg);
143 
144 	node = ofw_bus_get_node(dev);
145 
146 	if (node == -1)
147 		return (ENXIO);
148 
149 	simplebus_init(dev, node);
150 
151 	/*
152 	 * Allow devices to identify.
153 	 */
154 	bus_generic_probe(dev);
155 
156 	/*
157 	 * Now walk the OFW tree and attach top-level devices.
158 	 */
159 	for (node = OF_child(node); node > 0; node = OF_peer(node))
160 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
161 
162 	return (bus_generic_attach(dev));
163 }
164 
165 static int
166 am335x_pwmss_detach(device_t dev)
167 {
168 
169 	return (0);
170 }
171 
172 DEFINE_CLASS_1(am335x_pwmss, am335x_pwmss_driver, am335x_pwmss_methods,
173     sizeof(struct am335x_pwmss_softc), simplebus_driver);
174 DRIVER_MODULE(am335x_pwmss, simplebus, am335x_pwmss_driver, 0, 0);
175 MODULE_VERSION(am335x_pwmss, 1);
176 MODULE_DEPEND(am335x_pwmss, ti_sysc, 1, 1, 1);
177