1*6e54367aSthorpej /* $NetBSD: sun9i_a80_usbclk.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
2f0ea48bbSjmcneill
3f0ea48bbSjmcneill /*-
4f0ea48bbSjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5f0ea48bbSjmcneill * All rights reserved.
6f0ea48bbSjmcneill *
7f0ea48bbSjmcneill * Redistribution and use in source and binary forms, with or without
8f0ea48bbSjmcneill * modification, are permitted provided that the following conditions
9f0ea48bbSjmcneill * are met:
10f0ea48bbSjmcneill * 1. Redistributions of source code must retain the above copyright
11f0ea48bbSjmcneill * notice, this list of conditions and the following disclaimer.
12f0ea48bbSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13f0ea48bbSjmcneill * notice, this list of conditions and the following disclaimer in the
14f0ea48bbSjmcneill * documentation and/or other materials provided with the distribution.
15f0ea48bbSjmcneill *
16f0ea48bbSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17f0ea48bbSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18f0ea48bbSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19f0ea48bbSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20f0ea48bbSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21f0ea48bbSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22f0ea48bbSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23f0ea48bbSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24f0ea48bbSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f0ea48bbSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f0ea48bbSjmcneill * SUCH DAMAGE.
27f0ea48bbSjmcneill */
28f0ea48bbSjmcneill
29f0ea48bbSjmcneill #include <sys/cdefs.h>
30f0ea48bbSjmcneill
31*6e54367aSthorpej __KERNEL_RCSID(1, "$NetBSD: sun9i_a80_usbclk.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
32f0ea48bbSjmcneill
33f0ea48bbSjmcneill #include <sys/param.h>
34f0ea48bbSjmcneill #include <sys/bus.h>
35f0ea48bbSjmcneill #include <sys/device.h>
36f0ea48bbSjmcneill #include <sys/systm.h>
37f0ea48bbSjmcneill
38f0ea48bbSjmcneill #include <dev/fdt/fdtvar.h>
39f0ea48bbSjmcneill
40f0ea48bbSjmcneill #include <arm/sunxi/sunxi_ccu.h>
41f0ea48bbSjmcneill
42f0ea48bbSjmcneill enum {
43f0ea48bbSjmcneill CLK_BUS_HCI0 = 0,
44f0ea48bbSjmcneill CLK_USB_OHCI0,
45f0ea48bbSjmcneill CLK_BUS_HCI1,
46f0ea48bbSjmcneill CLK_BUS_HCI2,
47f0ea48bbSjmcneill CLK_USB_OHCI2,
48f0ea48bbSjmcneill CLK_USB0_PHY,
49f0ea48bbSjmcneill CLK_USB1_HSIC,
50f0ea48bbSjmcneill CLK_USB1_PHY,
51f0ea48bbSjmcneill CLK_USB2_HSIC,
52f0ea48bbSjmcneill CLK_USB2_PHY,
53f0ea48bbSjmcneill CLK_USB_HSIC
54f0ea48bbSjmcneill };
55f0ea48bbSjmcneill
56f0ea48bbSjmcneill enum {
57f0ea48bbSjmcneill RST_USB0_HCI = 0,
58f0ea48bbSjmcneill RST_USB1_HCI,
59f0ea48bbSjmcneill RST_USB2_HCI,
60f0ea48bbSjmcneill RST_USB0_PHY,
61f0ea48bbSjmcneill RST_USB1_HSIC,
62f0ea48bbSjmcneill RST_USB1_PHY,
63f0ea48bbSjmcneill RST_USB2_HSIC,
64f0ea48bbSjmcneill RST_USB2_PHY
65f0ea48bbSjmcneill };
66f0ea48bbSjmcneill
67f0ea48bbSjmcneill #define HCI_SCR 0x00
68f0ea48bbSjmcneill #define HCI_PCR 0x04
69f0ea48bbSjmcneill
70f0ea48bbSjmcneill static int sun9i_a80_usbclk_match(device_t, cfdata_t, void *);
71f0ea48bbSjmcneill static void sun9i_a80_usbclk_attach(device_t, device_t, void *);
72f0ea48bbSjmcneill
73*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
74*6e54367aSthorpej { .compat = "allwinner,sun9i-a80-usb-clks" },
75*6e54367aSthorpej DEVICE_COMPAT_EOL
76f0ea48bbSjmcneill };
77f0ea48bbSjmcneill
78f0ea48bbSjmcneill CFATTACH_DECL_NEW(sunxi_a80_usbclk, sizeof(struct sunxi_ccu_softc),
79f0ea48bbSjmcneill sun9i_a80_usbclk_match, sun9i_a80_usbclk_attach, NULL, NULL);
80f0ea48bbSjmcneill
81f0ea48bbSjmcneill static struct sunxi_ccu_reset sun9i_a80_usbclk_resets[] = {
82f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB0_HCI, HCI_SCR, 17),
83f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB1_HCI, HCI_SCR, 18),
84f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB2_HCI, HCI_SCR, 19),
85f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB0_PHY, HCI_PCR, 17),
86f0ea48bbSjmcneill
87f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB1_HSIC, HCI_PCR, 18),
88f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB1_PHY, HCI_PCR, 19),
89f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB2_HSIC, HCI_PCR, 20),
90f0ea48bbSjmcneill SUNXI_CCU_RESET(RST_USB2_PHY, HCI_PCR, 21),
91f0ea48bbSjmcneill };
92f0ea48bbSjmcneill
93f0ea48bbSjmcneill static struct sunxi_ccu_clk sun9i_a80_usbclk_clks[] = {
94f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_BUS_HCI0, "bus-hci0", "bus", HCI_SCR, 1),
95f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB_OHCI0, "usb-ohci0", "hosc", HCI_SCR, 2),
96f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_BUS_HCI1, "bus-hci1", "bus", HCI_SCR, 3),
97f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_BUS_HCI2, "bus-hci2", "bus", HCI_SCR, 5),
98f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB_OHCI2, "usb-ohci2", "hosc", HCI_SCR, 6),
99f0ea48bbSjmcneill
100f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB0_PHY, "usb0-phy", "hosc", HCI_PCR, 1),
101f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB1_HSIC, "usb1-hsic", "hosc", HCI_PCR, 2),
102f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB1_PHY, "usb1-phy", "hosc", HCI_PCR, 3),
103f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB2_HSIC, "usb2-hsic", "hosc", HCI_PCR, 4),
104f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB2_PHY, "usb2-phy", "hosc", HCI_PCR, 5),
105f0ea48bbSjmcneill SUNXI_CCU_GATE(CLK_USB_HSIC, "usb-hsic", "hosc", HCI_PCR, 10),
106f0ea48bbSjmcneill };
107f0ea48bbSjmcneill
108f0ea48bbSjmcneill static int
sun9i_a80_usbclk_match(device_t parent,cfdata_t cf,void * aux)109f0ea48bbSjmcneill sun9i_a80_usbclk_match(device_t parent, cfdata_t cf, void *aux)
110f0ea48bbSjmcneill {
111f0ea48bbSjmcneill struct fdt_attach_args * const faa = aux;
112f0ea48bbSjmcneill
113*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
114f0ea48bbSjmcneill }
115f0ea48bbSjmcneill
116f0ea48bbSjmcneill static void
sun9i_a80_usbclk_attach(device_t parent,device_t self,void * aux)117f0ea48bbSjmcneill sun9i_a80_usbclk_attach(device_t parent, device_t self, void *aux)
118f0ea48bbSjmcneill {
119f0ea48bbSjmcneill struct sunxi_ccu_softc * const sc = device_private(self);
120f0ea48bbSjmcneill struct fdt_attach_args * const faa = aux;
121f0ea48bbSjmcneill const int phandle = faa->faa_phandle;
122f0ea48bbSjmcneill struct clk *clk;
123f0ea48bbSjmcneill
124f0ea48bbSjmcneill sc->sc_dev = self;
125f0ea48bbSjmcneill sc->sc_phandle = faa->faa_phandle;
126f0ea48bbSjmcneill sc->sc_bst = faa->faa_bst;
127f0ea48bbSjmcneill
128f0ea48bbSjmcneill sc->sc_resets = sun9i_a80_usbclk_resets;
129f0ea48bbSjmcneill sc->sc_nresets = __arraycount(sun9i_a80_usbclk_resets);
130f0ea48bbSjmcneill
131f0ea48bbSjmcneill sc->sc_clks = sun9i_a80_usbclk_clks;
132f0ea48bbSjmcneill sc->sc_nclks = __arraycount(sun9i_a80_usbclk_clks);
133f0ea48bbSjmcneill
134f0ea48bbSjmcneill clk = fdtbus_clock_get(phandle, "bus");
135f0ea48bbSjmcneill if (clk == NULL || clk_enable(clk) != 0) {
136f0ea48bbSjmcneill aprint_error(": couldn't enable clock\n");
137f0ea48bbSjmcneill return;
138f0ea48bbSjmcneill }
139f0ea48bbSjmcneill
140f0ea48bbSjmcneill if (sunxi_ccu_attach(sc) != 0)
141f0ea48bbSjmcneill return;
142f0ea48bbSjmcneill
143f0ea48bbSjmcneill aprint_naive("\n");
144f0ea48bbSjmcneill aprint_normal(": A80 USB HCI clocks\n");
145f0ea48bbSjmcneill
146f0ea48bbSjmcneill sunxi_ccu_print(sc);
147f0ea48bbSjmcneill }
148