]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/clk/aw_usbclk.c
Merge OpenSSL 1.0.2l.
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / clk / aw_usbclk.c
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 USB clocks
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 #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 #include "hwreset_if.h"
53
54 #define A10_SCLK_GATING_USBPHY  (1 << 8)
55 #define A10_SCLK_GATING_OHCI1   (1 << 7)
56 #define A10_SCLK_GATING_OHCI0   (1 << 6)
57
58 #define USBPHY2_RST             (1 << 2)
59 #define USBPHY1_RST             (1 << 1)
60 #define USBPHY0_RST             (1 << 0)
61
62 enum aw_usbclk_type {
63         AW_A10_USBCLK = 1,
64         AW_A13_USBCLK,
65         AW_A31_USBCLK,
66         AW_A83T_USBCLK,
67         AW_H3_USBCLK,
68 };
69
70 static struct ofw_compat_data compat_data[] = {
71         { "allwinner,sun4i-a10-usb-clk",        AW_A10_USBCLK },
72         { "allwinner,sun5i-a13-usb-clk",        AW_A13_USBCLK },
73         { "allwinner,sun6i-a31-usb-clk",        AW_A31_USBCLK },
74         { "allwinner,sun8i-a83t-usb-clk",       AW_A83T_USBCLK },
75         { "allwinner,sun8i-h3-usb-clk",         AW_H3_USBCLK },
76         { NULL, 0 }
77 };
78
79 /* Clock indices for A10, as there is no clock-indices property in the DT */
80 static uint32_t aw_usbclk_indices_a10[] = { 6, 7, 8 };
81 /* Clock indices for H3, as there is no clock-indices property in the DT */
82 static uint32_t aw_usbclk_indices_h3[] = { 8, 9, 10, 11, 16, 17, 18, 19 };
83
84 struct aw_usbclk_softc {
85         bus_addr_t      reg;
86 };
87
88 static int
89 aw_usbclk_hwreset_assert(device_t dev, intptr_t id, bool value)
90 {
91         struct aw_usbclk_softc *sc;
92         uint32_t mask;
93         device_t pdev;
94         int error;
95
96         sc = device_get_softc(dev);
97         pdev = device_get_parent(dev);
98
99         mask = USBPHY0_RST << id;
100
101         CLKDEV_DEVICE_LOCK(pdev);
102         error = CLKDEV_MODIFY_4(pdev, sc->reg, mask, value ? 0 : mask);
103         CLKDEV_DEVICE_UNLOCK(pdev);
104
105         return (error);
106 }
107
108 static int
109 aw_usbclk_hwreset_is_asserted(device_t dev, intptr_t id, bool *value)
110 {
111         struct aw_usbclk_softc *sc;
112         uint32_t mask, val;
113         device_t pdev;
114         int error;
115
116         sc = device_get_softc(dev);
117         pdev = device_get_parent(dev);
118
119         mask = USBPHY0_RST << id;
120
121         CLKDEV_DEVICE_LOCK(pdev);
122         error = CLKDEV_READ_4(pdev, sc->reg, &val);
123         CLKDEV_DEVICE_UNLOCK(pdev);
124
125         if (error)
126                 return (error);
127
128         *value = (val & mask) != 0 ? false : true;
129
130         return (0);
131 }
132
133 static int
134 aw_usbclk_create(device_t dev, bus_addr_t paddr, struct clkdom *clkdom,
135     const char *pclkname, const char *clkname, int index)
136 {
137         const char *parent_names[1] = { pclkname };
138         struct clk_gate_def def;
139
140         memset(&def, 0, sizeof(def));
141         def.clkdef.id = index;
142         def.clkdef.name = clkname;
143         def.clkdef.parent_names = parent_names;
144         def.clkdef.parent_cnt = 1;
145         def.offset = paddr;
146         def.shift = index;
147         def.mask = 1;
148         def.on_value = 1;
149         def.off_value = 0;
150
151         return (clknode_gate_register(clkdom, &def));
152 }
153
154 static int
155 aw_usbclk_probe(device_t dev)
156 {
157         if (!ofw_bus_status_okay(dev))
158                 return (ENXIO);
159
160         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
161                 return (ENXIO);
162
163         device_set_desc(dev, "Allwinner USB Clocks");
164         return (BUS_PROBE_DEFAULT);
165 }
166
167 static int
168 aw_usbclk_attach(device_t dev)
169 {
170         struct aw_usbclk_softc *sc;
171         struct clkdom *clkdom;
172         const char **names;
173         const char *pname;
174         int index, nout, error;
175         enum aw_usbclk_type type;
176         uint32_t *indices;
177         clk_t clk_parent, clk_parent_pll;
178         bus_size_t psize;
179         phandle_t node;
180
181         sc = device_get_softc(dev);
182         node = ofw_bus_get_node(dev);
183         indices = NULL;
184         type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
185
186         if (ofw_reg_to_paddr(node, 0, &sc->reg, &psize, NULL) != 0) {
187                 device_printf(dev, "cannot parse 'reg' property\n");
188                 return (ENXIO);
189         }
190
191         clkdom = clkdom_create(dev);
192
193         nout = clk_parse_ofw_out_names(dev, node, &names, &indices);
194         if (nout == 0) {
195                 device_printf(dev, "no clock outputs found\n");
196                 error = ENOENT;
197                 goto fail;
198         }
199
200         if (indices == NULL && type == AW_A10_USBCLK)
201                 indices = aw_usbclk_indices_a10;
202         else if (indices == NULL && type == AW_H3_USBCLK)
203                 indices = aw_usbclk_indices_h3;
204
205         error = clk_get_by_ofw_index(dev, 0, 0, &clk_parent);
206         if (error != 0) {
207                 device_printf(dev, "cannot parse clock parent\n");
208                 return (ENXIO);
209         }
210         if (type == AW_A83T_USBCLK) {
211                 error = clk_get_by_ofw_index(dev, 0, 1, &clk_parent_pll);
212                 if (error != 0) {
213                         device_printf(dev, "cannot parse pll clock parent\n");
214                         return (ENXIO);
215                 }
216         }
217
218         for (index = 0; index < nout; index++) {
219                 if (strcmp(names[index], "usb_hsic_pll") == 0)
220                         pname = clk_get_name(clk_parent_pll);
221                 else
222                         pname = clk_get_name(clk_parent);
223                 error = aw_usbclk_create(dev, sc->reg, clkdom, pname,
224                     names[index], indices != NULL ? indices[index] : index);
225                 if (error)
226                         goto fail;
227         }
228
229         if (clkdom_finit(clkdom) != 0) {
230                 device_printf(dev, "cannot finalize clkdom initialization\n");
231                 error = ENXIO;
232                 goto fail;
233         }
234
235         if (bootverbose)
236                 clkdom_dump(clkdom);
237
238         hwreset_register_ofw_provider(dev);
239
240         return (0);
241
242 fail:
243         return (error);
244 }
245
246 static device_method_t aw_usbclk_methods[] = {
247         /* Device interface */
248         DEVMETHOD(device_probe,         aw_usbclk_probe),
249         DEVMETHOD(device_attach,        aw_usbclk_attach),
250
251         /* Reset interface */
252         DEVMETHOD(hwreset_assert,       aw_usbclk_hwreset_assert),
253         DEVMETHOD(hwreset_is_asserted,  aw_usbclk_hwreset_is_asserted),
254
255         DEVMETHOD_END
256 };
257
258 static driver_t aw_usbclk_driver = {
259         "aw_usbclk",
260         aw_usbclk_methods,
261         sizeof(struct aw_usbclk_softc)
262 };
263
264 static devclass_t aw_usbclk_devclass;
265
266 EARLY_DRIVER_MODULE(aw_usbclk, simplebus, aw_usbclk_driver,
267     aw_usbclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);