Home | History | Annotate | Line # | Download | only in imx
imx23_rtc.c revision 1.1.22.1
      1       1.1  jmcneill /* $Id: imx23_rtc.c,v 1.1.22.1 2020/04/13 08:03:34 martin Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*
      4       1.1  jmcneill * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5       1.1  jmcneill * All rights reserved.
      6       1.1  jmcneill *
      7       1.1  jmcneill * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  jmcneill * by Petri Laakso.
      9       1.1  jmcneill *
     10       1.1  jmcneill * Redistribution and use in source and binary forms, with or without
     11       1.1  jmcneill * modification, are permitted provided that the following conditions
     12       1.1  jmcneill * are met:
     13       1.1  jmcneill * 1. Redistributions of source code must retain the above copyright
     14       1.1  jmcneill *    notice, this list of conditions and the following disclaimer.
     15       1.1  jmcneill * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  jmcneill *    notice, this list of conditions and the following disclaimer in the
     17       1.1  jmcneill *    documentation and/or other materials provided with the distribution.
     18       1.1  jmcneill *
     19       1.1  jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  jmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  jmcneill * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  jmcneill */
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/param.h>
     33       1.1  jmcneill #include <sys/types.h>
     34       1.1  jmcneill #include <sys/bus.h>
     35       1.1  jmcneill #include <sys/cdefs.h>
     36       1.1  jmcneill #include <sys/device.h>
     37       1.1  jmcneill #include <sys/errno.h>
     38       1.1  jmcneill 
     39       1.1  jmcneill #include <arm/imx/imx23_rtcreg.h>
     40       1.1  jmcneill #include <arm/imx/imx23_rtcvar.h>
     41       1.1  jmcneill #include <arm/imx/imx23var.h>
     42       1.1  jmcneill 
     43       1.1  jmcneill typedef struct rtc_softc {
     44       1.1  jmcneill 	device_t sc_dev;
     45       1.1  jmcneill 	bus_space_tag_t sc_iot;
     46       1.1  jmcneill 	bus_space_handle_t sc_hdl;
     47       1.1  jmcneill } *rtc_softc_t;
     48       1.1  jmcneill 
     49       1.1  jmcneill static int	rtc_match(device_t, cfdata_t, void *);
     50       1.1  jmcneill static void	rtc_attach(device_t, device_t, void *);
     51       1.1  jmcneill static int	rtc_activate(device_t, enum devact);
     52       1.1  jmcneill 
     53       1.1  jmcneill static void     rtc_init(struct rtc_softc *);
     54       1.1  jmcneill static void     rtc_reset(struct rtc_softc *);
     55       1.1  jmcneill 
     56       1.1  jmcneill static rtc_softc_t _sc = NULL;
     57       1.1  jmcneill 
     58       1.1  jmcneill CFATTACH_DECL3_NEW(rtc,
     59       1.1  jmcneill         sizeof(struct rtc_softc),
     60       1.1  jmcneill         rtc_match,
     61       1.1  jmcneill         rtc_attach,
     62       1.1  jmcneill         NULL,
     63       1.1  jmcneill         rtc_activate,
     64       1.1  jmcneill         NULL,
     65       1.1  jmcneill         NULL,
     66       1.1  jmcneill         0
     67       1.1  jmcneill );
     68       1.1  jmcneill 
     69       1.1  jmcneill #define RTC_RD(sc, reg)                                                 \
     70       1.1  jmcneill         bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
     71       1.1  jmcneill #define RTC_WR(sc, reg, val)                                            \
     72       1.1  jmcneill         bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
     73       1.1  jmcneill 
     74       1.1  jmcneill #define RTC_SOFT_RST_LOOP 455 /* At least 1 us ... */
     75       1.1  jmcneill 
     76       1.1  jmcneill static int
     77       1.1  jmcneill rtc_match(device_t parent, cfdata_t match, void *aux)
     78       1.1  jmcneill {
     79       1.1  jmcneill 	struct apb_attach_args *aa = aux;
     80       1.1  jmcneill 
     81       1.1  jmcneill 	if ((aa->aa_addr == HW_RTC_BASE) &&
     82       1.1  jmcneill 	    (aa->aa_size == HW_RTC_BASE_SIZE))
     83       1.1  jmcneill 		return 1;
     84       1.1  jmcneill 
     85       1.1  jmcneill 	return 0;
     86       1.1  jmcneill }
     87       1.1  jmcneill 
     88       1.1  jmcneill static void
     89       1.1  jmcneill rtc_attach(device_t parent, device_t self, void *aux)
     90       1.1  jmcneill {
     91       1.1  jmcneill 	struct rtc_softc *sc = device_private(self);
     92       1.1  jmcneill 	struct apb_attach_args *aa = aux;
     93       1.1  jmcneill 	static int rtc_attached = 0;
     94       1.1  jmcneill 
     95       1.1  jmcneill 	sc->sc_dev = self;
     96       1.1  jmcneill 	sc->sc_iot = aa->aa_iot;
     97       1.1  jmcneill 
     98       1.1  jmcneill 	if (rtc_attached) {
     99       1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "rtc is already attached\n");
    100       1.1  jmcneill 		return;
    101       1.1  jmcneill 	}
    102       1.1  jmcneill 
    103       1.1  jmcneill 	if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
    104       1.1  jmcneill 	    &sc->sc_hdl))
    105       1.1  jmcneill 	{
    106       1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
    107       1.1  jmcneill 		return;
    108       1.1  jmcneill 	}
    109       1.1  jmcneill 
    110       1.1  jmcneill 
    111       1.1  jmcneill 	rtc_init(sc);
    112       1.1  jmcneill 	rtc_reset(sc);
    113       1.1  jmcneill 
    114       1.1  jmcneill 	aprint_normal("\n");
    115       1.1  jmcneill 
    116       1.1  jmcneill 	rtc_attached = 1;
    117       1.1  jmcneill 
    118       1.1  jmcneill 	return;
    119       1.1  jmcneill }
    120       1.1  jmcneill 
    121       1.1  jmcneill static int
    122       1.1  jmcneill rtc_activate(device_t self, enum devact act)
    123       1.1  jmcneill {
    124       1.1  jmcneill 
    125       1.1  jmcneill 	return EOPNOTSUPP;
    126       1.1  jmcneill }
    127       1.1  jmcneill 
    128       1.1  jmcneill static void
    129       1.1  jmcneill rtc_init(struct rtc_softc *sc)
    130       1.1  jmcneill {
    131       1.1  jmcneill 	_sc = sc;
    132       1.1  jmcneill 	return;
    133       1.1  jmcneill }
    134       1.1  jmcneill 
    135       1.1  jmcneill /*
    136       1.1  jmcneill  * Remove pulldown resistors on the headphone outputs.
    137       1.1  jmcneill  */
    138       1.1  jmcneill void
    139       1.1  jmcneill rtc_release_gnd(int val)
    140       1.1  jmcneill {
    141       1.1  jmcneill 	struct rtc_softc *sc = _sc;
    142       1.1  jmcneill 
    143       1.1  jmcneill         if (sc == NULL) {
    144  1.1.22.1    martin                 aprint_error("rtc is not initialized");
    145       1.1  jmcneill                 return;
    146       1.1  jmcneill         }
    147       1.1  jmcneill 	if(val)
    148       1.1  jmcneill 		RTC_WR(sc, HW_RTC_PERSISTENT0_SET, (1<<19));
    149       1.1  jmcneill 	else
    150       1.1  jmcneill 		RTC_WR(sc, HW_RTC_PERSISTENT0_CLR, (1<<19));
    151       1.1  jmcneill 
    152       1.1  jmcneill 	return;
    153       1.1  jmcneill }
    154       1.1  jmcneill 
    155       1.1  jmcneill /*
    156       1.1  jmcneill  * Reset the RTC block.
    157       1.1  jmcneill  *
    158       1.1  jmcneill  * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
    159       1.1  jmcneill  */
    160       1.1  jmcneill static void
    161       1.1  jmcneill rtc_reset(struct rtc_softc *sc)
    162       1.1  jmcneill {
    163       1.1  jmcneill         unsigned int loop;
    164       1.1  jmcneill 
    165       1.1  jmcneill         /* Prepare for soft-reset by making sure that SFTRST is not currently
    166       1.1  jmcneill         * asserted. Also clear CLKGATE so we can wait for its assertion below.
    167       1.1  jmcneill         */
    168       1.1  jmcneill         RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_SFTRST);
    169       1.1  jmcneill 
    170       1.1  jmcneill         /* Wait at least a microsecond for SFTRST to deassert. */
    171       1.1  jmcneill         loop = 0;
    172       1.1  jmcneill         while ((RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_SFTRST) ||
    173       1.1  jmcneill             (loop < RTC_SOFT_RST_LOOP))
    174       1.1  jmcneill                 loop++;
    175       1.1  jmcneill 
    176       1.1  jmcneill         /* Clear CLKGATE so we can wait for its assertion below. */
    177       1.1  jmcneill         RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_CLKGATE);
    178       1.1  jmcneill 
    179       1.1  jmcneill         /* Soft-reset the block. */
    180       1.1  jmcneill         RTC_WR(sc, HW_RTC_CTRL_SET, HW_RTC_CTRL_SFTRST);
    181       1.1  jmcneill 
    182       1.1  jmcneill         /* Wait until clock is in the gated state. */
    183       1.1  jmcneill         while (!(RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_CLKGATE));
    184       1.1  jmcneill 
    185       1.1  jmcneill         /* Bring block out of reset. */
    186       1.1  jmcneill         RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_SFTRST);
    187       1.1  jmcneill 
    188       1.1  jmcneill         loop = 0;
    189       1.1  jmcneill         while ((RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_SFTRST) ||
    190       1.1  jmcneill             (loop < RTC_SOFT_RST_LOOP))
    191       1.1  jmcneill                 loop++;
    192       1.1  jmcneill 
    193       1.1  jmcneill         RTC_WR(sc, HW_RTC_CTRL_CLR, HW_RTC_CTRL_CLKGATE);
    194       1.1  jmcneill 
    195       1.1  jmcneill         /* Wait until clock is in the NON-gated state. */
    196       1.1  jmcneill         while (RTC_RD(sc, HW_RTC_CTRL) & HW_RTC_CTRL_CLKGATE);
    197       1.1  jmcneill 
    198       1.1  jmcneill         return;
    199       1.1  jmcneill }
    200