1/* $Id: imx23_rtc.c,v 1.2 2019/10/18 04:09:01 msaitoh Exp $ */
2
3/*
4* Copyright (c) 2014 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_rtcreg.h>
40#include <arm/imx/imx23_rtcvar.h>
41#include <arm/imx/imx23var.h>
42
43typedef struct rtc_softc {
44	device_t sc_dev;
45	bus_space_tag_t sc_iot;
46	bus_space_handle_t sc_hdl;
47} *rtc_softc_t;
48
49static int	rtc_match(device_t, cfdata_t, void *);
50static void	rtc_attach(device_t, device_t, void *);
51static int	rtc_activate(device_t, enum devact);
52
53static void     rtc_init(struct rtc_softc *);
54static void     rtc_reset(struct rtc_softc *);
55
56static rtc_softc_t _sc = NULL;
57
58CFATTACH_DECL3_NEW(rtc,
59        sizeof(struct rtc_softc),
60        rtc_match,
61        rtc_attach,
62        NULL,
63        rtc_activate,
64        NULL,
65        NULL,
66        0
67);
68
69#define RTC_RD(sc, reg)                                                 \
70        bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
71#define RTC_WR(sc, reg, val)                                            \
72        bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
73
74#define RTC_SOFT_RST_LOOP 455 /* At least 1 us ... */
75
76static int
77rtc_match(device_t parent, cfdata_t match, void *aux)
78{
79	struct apb_attach_args *aa = aux;
80
81	if ((aa->aa_addr == HW_RTC_BASE) &&
82	    (aa->aa_size == HW_RTC_BASE_SIZE))
83		return 1;
84
85	return 0;
86}
87
88static void
89rtc_attach(device_t parent, device_t self, void *aux)
90{
91	struct rtc_softc *sc = device_private(self);
92	struct apb_attach_args *aa = aux;
93	static int rtc_attached = 0;
94
95	sc->sc_dev = self;
96	sc->sc_iot = aa->aa_iot;
97
98	if (rtc_attached) {
99		aprint_error_dev(sc->sc_dev, "rtc is already attached\n");
100		return;
101	}
102
103	if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
104	    &sc->sc_hdl))
105	{
106		aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
107		return;
108	}
109
110
111	rtc_init(sc);
112	rtc_reset(sc);
113
114	aprint_normal("\n");
115
116	rtc_attached = 1;
117
118	return;
119}
120
121static int
122rtc_activate(device_t self, enum devact act)
123{
124
125	return EOPNOTSUPP;
126}
127
128static void
129rtc_init(struct rtc_softc *sc)
130{
131	_sc = sc;
132	return;
133}
134
135/*
136 * Remove pulldown resistors on the headphone outputs.
137 */
138void
139rtc_release_gnd(int val)
140{
141	struct rtc_softc *sc = _sc;
142
143        if (sc == NULL) {
144                aprint_error("rtc is not initialized");
145                return;
146        }
147	if(val)
148		RTC_WR(sc, HW_RTC_PERSISTENT0_SET, (1<<19));
149	else
150		RTC_WR(sc, HW_RTC_PERSISTENT0_CLR, (1<<19));
151
152	return;
153}
154
155/*
156 * Reset the RTC block.
157 *
158 * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
159 */
160static void
161rtc_reset(struct rtc_softc *sc)
162{
163        unsigned int loop;
164
165        /* Prepare for soft-reset by making sure that SFTRST is not currently
166        * asserted. Also clear CLKGATE so we can wait for its assertion below.
167        */
168        RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_SFTRST);
169
170        /* Wait at least a microsecond for SFTRST to deassert. */
171        loop = 0;
172        while ((RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_SFTRST) ||
173            (loop < RTC_SOFT_RST_LOOP))
174                loop++;
175
176        /* Clear CLKGATE so we can wait for its assertion below. */
177        RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_CLKGATE);
178
179        /* Soft-reset the block. */
180        RTC_WR(sc, HW_RTC_CTRL_SET, HW_RTC_CTRL_SFTRST);
181
182        /* Wait until clock is in the gated state. */
183        while (!(RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_CLKGATE));
184
185        /* Bring block out of reset. */
186        RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_SFTRST);
187
188        loop = 0;
189        while ((RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_SFTRST) ||
190            (loop < RTC_SOFT_RST_LOOP))
191                loop++;
192
193        RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_CLKGATE);
194
195        /* Wait until clock is in the NON-gated state. */
196        while (RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_CLKGATE);
197
198        return;
199}
200