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