1 /* $Id: imx23_clkctrl.c,v 1.3 2019/10/18 04:09:01 msaitoh Exp $ */
2 
3 /*
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Petri Laakso.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/bus.h>
35 #include <sys/cdefs.h>
36 #include <sys/device.h>
37 #include <sys/errno.h>
38 
39 #include <arm/imx/imx23_clkctrlreg.h>
40 #include <arm/imx/imx23_clkctrlvar.h>
41 #include <arm/imx/imx23var.h>
42 
43 typedef struct clkctrl_softc {
44           device_t sc_dev;
45           bus_space_tag_t sc_iot;
46           bus_space_handle_t sc_hdl;
47 } *clkctrl_softc_t;
48 
49 static int          clkctrl_match(device_t, cfdata_t, void *);
50 static void         clkctrl_attach(device_t, device_t, void *);
51 static int          clkctrl_activate(device_t, enum devact);
52 
53 static void     clkctrl_init(struct clkctrl_softc *);
54 
55 static clkctrl_softc_t _sc = NULL;
56 
57 CFATTACH_DECL3_NEW(clkctrl,
58         sizeof(struct clkctrl_softc),
59         clkctrl_match,
60         clkctrl_attach,
61         NULL,
62         clkctrl_activate,
63         NULL,
64         NULL,
65         0
66 );
67 
68 #define CLKCTRL_RD(sc, reg)                                                 \
69         bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
70 #define CLKCTRL_WR(sc, reg, val)                                            \
71         bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
72 
73 #define CLKCTRL_SOFT_RST_LOOP 455 /* At least 1 us ... */
74 
75 static int
clkctrl_match(device_t parent,cfdata_t match,void * aux)76 clkctrl_match(device_t parent, cfdata_t match, void *aux)
77 {
78           struct apb_attach_args *aa = aux;
79 
80           if ((aa->aa_addr == HW_CLKCTRL_BASE) &&
81               (aa->aa_size == HW_CLKCTRL_SIZE))
82                     return 1;
83 
84           return 0;
85 }
86 
87 static void
clkctrl_attach(device_t parent,device_t self,void * aux)88 clkctrl_attach(device_t parent, device_t self, void *aux)
89 {
90           struct clkctrl_softc *sc = device_private(self);
91           struct apb_attach_args *aa = aux;
92           static int clkctrl_attached = 0;
93 
94           sc->sc_dev = self;
95           sc->sc_iot = aa->aa_iot;
96 
97           if (clkctrl_attached) {
98                     aprint_error_dev(sc->sc_dev, "already attached\n");
99                     return;
100           }
101 
102           if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
103               &sc->sc_hdl))
104           {
105                     aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
106                     return;
107           }
108 
109 
110           clkctrl_init(sc);
111 
112           aprint_normal("\n");
113 
114           clkctrl_attached = 1;
115 
116           return;
117 }
118 
119 static int
clkctrl_activate(device_t self,enum devact act)120 clkctrl_activate(device_t self, enum devact act)
121 {
122 
123           return EOPNOTSUPP;
124 }
125 
126 static void
clkctrl_init(struct clkctrl_softc * sc)127 clkctrl_init(struct clkctrl_softc *sc)
128 {
129           _sc = sc;
130           return;
131 }
132 
133 /*
134  * Power up 8-phase PLL outputs for USB PHY
135  *
136  */
137 void
clkctrl_en_usb(void)138 clkctrl_en_usb(void)
139 {
140           struct clkctrl_softc *sc = _sc;
141 
142         if (sc == NULL) {
143                 aprint_error("clkctrl is not initialized");
144                 return;
145         }
146 
147           CLKCTRL_WR(sc, HW_CLKCTRL_PLLCTRL0_SET,
148               HW_CLKCTRL_PLLCTRL0_EN_USB_CLKS);
149 
150           return;
151 }
152 
153 /*
154  * Enable 24MHz clock for the Digital Filter.
155  *
156  */
157 void
clkctrl_en_filtclk(void)158 clkctrl_en_filtclk(void)
159 {
160           struct clkctrl_softc *sc = _sc;
161 
162           if (sc == NULL) {
163                     aprint_error("clkctrl is not initialized");
164                     return;
165           }
166 
167           CLKCTRL_WR(sc, HW_CLKCTRL_XTAL_CLR, HW_CLKCTRL_XTAL_FILT_CLK24M_GATE);
168 
169           return;
170 }
171