1 /* $Id: imx23_digctl.c,v 1.2 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 #include <sys/timetc.h>
39
40 #include <arm/imx/imx23_digctlreg.h>
41 #include <arm/imx/imx23_digctlvar.h>
42 #include <arm/imx/imx23var.h>
43
44 typedef struct digctl_softc {
45 device_t sc_dev;
46 bus_space_tag_t sc_iot;
47 bus_space_handle_t sc_hdl;
48 } *digctl_softc_t;
49
50 static int digctl_match(device_t, cfdata_t, void *);
51 static void digctl_attach(device_t, device_t, void *);
52 static int digctl_activate(device_t, enum devact);
53
54 static void digctl_reset(struct digctl_softc *);
55 static void digctl_init(struct digctl_softc *);
56
57 /* timecounter. */
58 static u_int digctl_tc_get_timecount(struct timecounter *);
59
60 static digctl_softc_t _sc = NULL;
61
62 CFATTACH_DECL3_NEW(digctl,
63 sizeof(struct digctl_softc),
64 digctl_match,
65 digctl_attach,
66 NULL,
67 digctl_activate,
68 NULL,
69 NULL,
70 0
71 );
72
73 static struct timecounter tc_useconds;
74
75 #define DCTL_RD(sc, reg) \
76 bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
77 #define DCTL_WR(sc, reg, val) \
78 bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
79
80 static int
digctl_match(device_t parent,cfdata_t match,void * aux)81 digctl_match(device_t parent, cfdata_t match, void *aux)
82 {
83 struct apb_attach_args *aa = aux;
84
85 if ((aa->aa_addr == HW_DIGCTL_BASE) && (aa->aa_size == HW_DIGCTL_SIZE))
86 return 1;
87
88 return 0;
89 }
90
91 static void
digctl_attach(device_t parent,device_t self,void * aux)92 digctl_attach(device_t parent, device_t self, void *aux)
93 {
94 struct digctl_softc *sc = device_private(self);
95 struct apb_attach_args *aa = aux;
96 static int digctl_attached = 0;
97
98 sc->sc_dev = self;
99 sc->sc_iot = aa->aa_iot;
100
101 if (digctl_attached) {
102 aprint_error_dev(sc->sc_dev, "already attached\n");
103 return;
104 }
105
106 if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
107 &sc->sc_hdl))
108 {
109 aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
110 return;
111 }
112
113 digctl_reset(sc);
114 digctl_init(sc);
115
116 aprint_normal("\n");
117
118 /*
119 * Setup timecounter to use digctl microseconds counter.
120 */
121 tc_useconds.tc_get_timecount = digctl_tc_get_timecount;
122 tc_useconds.tc_poll_pps = NULL;
123 tc_useconds.tc_counter_mask = 0xffffffff; /* 32bit counter. */
124 tc_useconds.tc_frequency = 1000000; /* @ 1MHz */
125 tc_useconds.tc_name = "digctl";
126 tc_useconds.tc_quality = 100;
127
128 /* Enable counter. */
129 DCTL_WR(sc, HW_DIGCTL_CTRL_CLR, HW_DIGCTL_CTRL_XTAL24M_GATE);
130
131 tc_init(&tc_useconds);
132
133 digctl_attached = 1;
134
135 return;
136 }
137
138 static int
digctl_activate(device_t self,enum devact act)139 digctl_activate(device_t self, enum devact act)
140 {
141
142 return EOPNOTSUPP;
143 }
144
145 /*
146 * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
147 */
148 static void
digctl_reset(struct digctl_softc * sc)149 digctl_reset(struct digctl_softc *sc)
150 {
151 return;
152 }
153
154 static void
digctl_init(struct digctl_softc * sc)155 digctl_init(struct digctl_softc *sc)
156 {
157 _sc = sc;
158 return;
159 }
160
161 /*
162 * Control USB controller clocks.
163 */
164 void
digctl_usb_clkgate(int value)165 digctl_usb_clkgate(int value)
166 {
167 struct digctl_softc *sc = _sc;
168
169 if (sc == NULL) {
170 aprint_error("digctl is not initialized");
171 return;
172 }
173
174 if (value) {
175 /* Clocks OFF. */
176 DCTL_WR(sc, HW_DIGCTL_CTRL_SET, HW_DIGCTL_CTRL_USB_CLKGATE);
177 } else {
178 /* Clocks ON. */
179 DCTL_WR(sc, HW_DIGCTL_CTRL_CLR, HW_DIGCTL_CTRL_USB_CLKGATE);
180 }
181
182 return;
183 }
184
185 /*
186 *
187 */
188 static u_int
digctl_tc_get_timecount(struct timecounter * tc)189 digctl_tc_get_timecount(struct timecounter *tc)
190 {
191 struct digctl_softc *sc = _sc;
192 return DCTL_RD(sc, HW_DIGCTL_MICROSECONDS);
193 }
194