xref: /freebsd/sys/arm/freescale/imx/imx6_ccm.c (revision 1a61beb0549e05b33df31380e427d90f6e46ff7e)
1 /*-
2  * Copyright (c) 2013 Ian Lepore <ian@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 /*
31  * Clocks and power control driver for Freescale i.MX6 family of SoCs.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include <machine/bus.h>
45 
46 #include <arm/freescale/imx/imx6_anatopreg.h>
47 #include <arm/freescale/imx/imx6_anatopvar.h>
48 #include <arm/freescale/imx/imx_machdep.h>
49 #include <arm/freescale/imx/imx6_ccmreg.h>
50 
51 
52 /* XXX temp kludge for imx51_get_clock. */
53 #include <arm/freescale/imx/imx51_ccmvar.h>
54 #include <arm/freescale/imx/imx51_ccmreg.h>
55 
56 struct ccm_softc {
57 	device_t	dev;
58 	struct resource	*mem_res;
59 };
60 
61 static struct ccm_softc *ccm_sc;
62 
63 static inline uint32_t
64 RD4(struct ccm_softc *sc, bus_size_t off)
65 {
66 
67 	return (bus_read_4(sc->mem_res, off));
68 }
69 
70 static inline void
71 WR4(struct ccm_softc *sc, bus_size_t off, uint32_t val)
72 {
73 
74 	bus_write_4(sc->mem_res, off, val);
75 }
76 
77 static int
78 ccm_detach(device_t dev)
79 {
80 	struct ccm_softc *sc;
81 
82 	sc = device_get_softc(dev);
83 
84 	if (sc->mem_res != NULL)
85 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
86 
87 	return (0);
88 }
89 
90 static int
91 ccm_attach(device_t dev)
92 {
93 	struct ccm_softc *sc;
94 	int err, rid;
95 
96 	sc = device_get_softc(dev);
97 	err = 0;
98 
99 	/* Allocate bus_space resources. */
100 	rid = 0;
101 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
102 	    RF_ACTIVE);
103 	if (sc->mem_res == NULL) {
104 		device_printf(dev, "Cannot allocate memory resources\n");
105 		err = ENXIO;
106 		goto out;
107 	}
108 
109 	ccm_sc = sc;
110 	err = 0;
111 
112 out:
113 
114 	if (err != 0)
115 		ccm_detach(dev);
116 
117 	return (err);
118 }
119 
120 static int
121 ccm_probe(device_t dev)
122 {
123 
124 	if (!ofw_bus_status_okay(dev))
125 		return (ENXIO);
126 
127         if (ofw_bus_is_compatible(dev, "fsl,imx6q-ccm") == 0)
128 		return (ENXIO);
129 
130 	device_set_desc(dev, "Freescale i.MX6 Clock Control Module");
131 
132 	return (BUS_PROBE_DEFAULT);
133 }
134 
135 void
136 imx_ccm_usb_enable(device_t _usbdev)
137 {
138 
139 	/*
140 	 * For imx6, the USBOH3 clock gate is bits 0-1 of CCGR6, so no need for
141 	 * shifting and masking here, just set the low-order two bits to ALWAYS.
142 	 */
143 	WR4(ccm_sc, CCM_CCGR6, RD4(ccm_sc, CCM_CCGR6) | CCGR_CLK_MODE_ALWAYS);
144 }
145 
146 void
147 imx_ccm_usbphy_enable(device_t _phydev)
148 {
149         /*
150          * XXX Which unit?
151          * Right now it's not clear how to figure from fdt data which phy unit
152          * we're supposed to operate on.  Until this is worked out, just enable
153          * both PHYs.
154          */
155 #if 0
156 	int phy_num, regoff;
157 
158 	phy_num = 0; /* XXX */
159 
160 	switch (phy_num) {
161 	case 0:
162 		regoff = 0;
163 		break;
164 	case 1:
165 		regoff = 0x10;
166 		break;
167 	default:
168 		device_printf(ccm_sc->dev, "Bad PHY number %u,\n",
169 		    phy_num);
170 		return;
171 	}
172 
173 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_USB1 + regoff,
174 	    IMX6_ANALOG_CCM_PLL_USB_ENABLE |
175 	    IMX6_ANALOG_CCM_PLL_USB_POWER |
176 	    IMX6_ANALOG_CCM_PLL_USB_EN_USB_CLKS);
177 #else
178 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_USB1 + 0,
179 	    IMX6_ANALOG_CCM_PLL_USB_ENABLE |
180 	    IMX6_ANALOG_CCM_PLL_USB_POWER |
181 	    IMX6_ANALOG_CCM_PLL_USB_EN_USB_CLKS);
182 
183 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_USB1 + 0x10,
184 	    IMX6_ANALOG_CCM_PLL_USB_ENABLE |
185 	    IMX6_ANALOG_CCM_PLL_USB_POWER |
186 	    IMX6_ANALOG_CCM_PLL_USB_EN_USB_CLKS);
187 #endif
188 }
189 
190 
191 
192 
193 
194 // XXX Fix this.  This has to be here for other code to link,
195 // but it doesn't have to return anything useful for imx6 right now.
196 u_int
197 imx51_get_clock(enum imx51_clock clk)
198 {
199 	switch (clk)
200 	{
201 	case IMX51CLK_IPG_CLK_ROOT:
202 		return 66000000;
203 	default:
204 		printf("imx51_get_clock() on imx6 doesn't know about clock %d\n", clk);
205 		break;
206 	}
207 	return 0;
208 }
209 
210 static device_method_t ccm_methods[] = {
211 	/* Device interface */
212 	DEVMETHOD(device_probe,  ccm_probe),
213 	DEVMETHOD(device_attach, ccm_attach),
214 	DEVMETHOD(device_detach, ccm_detach),
215 
216 	DEVMETHOD_END
217 };
218 
219 static driver_t ccm_driver = {
220 	"ccm",
221 	ccm_methods,
222 	sizeof(struct ccm_softc)
223 };
224 
225 static devclass_t ccm_devclass;
226 
227 DRIVER_MODULE(ccm, simplebus, ccm_driver, ccm_devclass, 0, 0);
228 
229