Home | History | Annotate | Line # | Download | only in sunxi
sunxi_rtc.c revision 1.6
      1  1.6   thorpej /* $NetBSD: sunxi_rtc.c,v 1.6 2020/03/27 01:42:10 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.6   thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.6 2020/03/27 01:42:10 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/mutex.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/clock_subr.h>
     40  1.6   thorpej #include <dev/clk/clk_backend.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43  1.1  jmcneill 
     44  1.3  jmcneill #define	SUN4I_RTC_YY_MM_DD_REG	0x04
     45  1.3  jmcneill #define	 SUN4I_RTC_LEAP		__BIT(22)
     46  1.3  jmcneill #define	 SUN4I_RTC_YEAR		__BITS(21,16)
     47  1.3  jmcneill #define	 SUN4I_RTC_MONTH	__BITS(11,8)
     48  1.3  jmcneill #define	 SUN4I_RTC_DAY		__BITS(4,0)
     49  1.3  jmcneill #define	SUN4I_RTC_HH_MM_SS_REG	0x08
     50  1.3  jmcneill #define	 SUN4I_RTC_WK_NO	__BITS(31,29)
     51  1.3  jmcneill #define	 SUN4I_RTC_HOUR		__BITS(20,16)
     52  1.3  jmcneill #define	 SUN4I_RTC_MINUTE	__BITS(13,8)
     53  1.3  jmcneill #define	 SUN4I_RTC_SECOND	__BITS(5,0)
     54  1.3  jmcneill #define	SUN4I_RTC_BASE_YEAR	2010
     55  1.3  jmcneill 
     56  1.2  jmcneill #define	SUN7I_RTC_YY_MM_DD_REG	0x04
     57  1.2  jmcneill #define	 SUN7I_RTC_LEAP		__BIT(24)
     58  1.2  jmcneill #define	 SUN7I_RTC_YEAR		__BITS(23,16)
     59  1.2  jmcneill #define	 SUN7I_RTC_MONTH	__BITS(11,8)
     60  1.2  jmcneill #define	 SUN7I_RTC_DAY		__BITS(4,0)
     61  1.2  jmcneill #define	SUN7I_RTC_HH_MM_SS_REG	0x08
     62  1.2  jmcneill #define	 SUN7I_RTC_WK_NO	__BITS(31,29)
     63  1.2  jmcneill #define	 SUN7I_RTC_HOUR		__BITS(20,16)
     64  1.2  jmcneill #define	 SUN7I_RTC_MINUTE	__BITS(13,8)
     65  1.2  jmcneill #define	 SUN7I_RTC_SECOND	__BITS(5,0)
     66  1.3  jmcneill #define	SUN7I_RTC_BASE_YEAR	1970
     67  1.2  jmcneill 
     68  1.6   thorpej #define	SUN6I_LOSC_CTRL_REG	0x00
     69  1.6   thorpej #define	 SUN6I_LOSC_CTRL_KEY		(0x16aa << 16)
     70  1.6   thorpej #define	 SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS __BIT(15)
     71  1.6   thorpej #define	 SUN6I_LOSC_CTRL_ALM_DHMS_ACC	__BIT(9)
     72  1.6   thorpej #define	 SUN6I_LOSC_CTRL_RTC_HMS_ACC	__BIT(8)
     73  1.6   thorpej #define	 SUN6I_LOSC_CTRL_RTC_YMD_ACC	__BIT(7)
     74  1.6   thorpej #define	 SUN6I_LOSC_CTRL_EXT_LOSC_EN	__BIT(4)
     75  1.6   thorpej #define	 SUN6I_LOSC_CTRL_EXT_OSC	__BIT(0)
     76  1.6   thorpej 
     77  1.6   thorpej #define	SUN6I_INTOSC_CLK_PRESCAL_REG 0x08
     78  1.6   thorpej #define	 SUN6I_INTOSC_CLK_PRESCAL	__BITS(0,4)
     79  1.6   thorpej 
     80  1.2  jmcneill #define	SUN6I_RTC_YY_MM_DD_REG	0x10
     81  1.2  jmcneill #define	 SUN6I_RTC_LEAP		__BIT(22)
     82  1.2  jmcneill #define	 SUN6I_RTC_YEAR		__BITS(21,16)
     83  1.2  jmcneill #define	 SUN6I_RTC_MONTH	__BITS(11,8)
     84  1.2  jmcneill #define	 SUN6I_RTC_DAY		__BITS(4,0)
     85  1.2  jmcneill #define	SUN6I_RTC_HH_MM_SS_REG	0x14
     86  1.2  jmcneill #define	 SUN6I_RTC_WK_NO	__BITS(31,29)
     87  1.2  jmcneill #define	 SUN6I_RTC_HOUR		__BITS(20,16)
     88  1.2  jmcneill #define	 SUN6I_RTC_MINUTE	__BITS(13,8)
     89  1.2  jmcneill #define	 SUN6I_RTC_SECOND	__BITS(5,0)
     90  1.3  jmcneill #define	SUN6I_RTC_BASE_YEAR	2000
     91  1.3  jmcneill 
     92  1.6   thorpej #define	SUN6I_RTC_LOSC_OUT_GATING_REG 0x60
     93  1.6   thorpej #define	 SUN6I_RTC_LOSC_OUT_EN	__BIT(0)
     94  1.6   thorpej 
     95  1.3  jmcneill struct sunxi_rtc_config {
     96  1.3  jmcneill 	bus_size_t	yy_mm_dd_reg;
     97  1.3  jmcneill 	uint32_t	leap, year, month, day;
     98  1.3  jmcneill 	bus_size_t	hh_mm_ss_reg;
     99  1.3  jmcneill 	uint32_t	wk_no, hour, minute, second;
    100  1.3  jmcneill 	u_int		base_year;
    101  1.6   thorpej 
    102  1.6   thorpej 	u_int		iosc_rate;
    103  1.6   thorpej 	u_int		fixed_prescaler;
    104  1.6   thorpej 	uint32_t	ext_losc_en;
    105  1.6   thorpej 	uint32_t	auto_swt_bypass;
    106  1.6   thorpej 	u_int		flags;
    107  1.3  jmcneill };
    108  1.1  jmcneill 
    109  1.6   thorpej #define	SUNXI_RTC_F_HAS_VAR_PRESCALER	__BIT(0)
    110  1.6   thorpej 
    111  1.3  jmcneill static const struct sunxi_rtc_config sun4i_rtc_config = {
    112  1.3  jmcneill 	.yy_mm_dd_reg = SUN4I_RTC_YY_MM_DD_REG,
    113  1.3  jmcneill 	.leap = SUN4I_RTC_LEAP,
    114  1.3  jmcneill 	.year = SUN4I_RTC_YEAR,
    115  1.3  jmcneill 	.month = SUN4I_RTC_MONTH,
    116  1.3  jmcneill 	.day = SUN4I_RTC_DAY,
    117  1.3  jmcneill 	.hh_mm_ss_reg = SUN4I_RTC_HH_MM_SS_REG,
    118  1.3  jmcneill 	.wk_no = SUN4I_RTC_WK_NO,
    119  1.3  jmcneill 	.hour = SUN4I_RTC_HOUR,
    120  1.3  jmcneill 	.minute = SUN4I_RTC_MINUTE,
    121  1.3  jmcneill 	.second = SUN4I_RTC_SECOND,
    122  1.3  jmcneill 	.base_year = SUN4I_RTC_BASE_YEAR,
    123  1.3  jmcneill };
    124  1.3  jmcneill 
    125  1.6   thorpej static const struct sunxi_rtc_config sun6i_a31_rtc_config = {
    126  1.3  jmcneill 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    127  1.3  jmcneill 	.leap = SUN6I_RTC_LEAP,
    128  1.3  jmcneill 	.year = SUN6I_RTC_YEAR,
    129  1.3  jmcneill 	.month = SUN6I_RTC_MONTH,
    130  1.3  jmcneill 	.day = SUN6I_RTC_DAY,
    131  1.3  jmcneill 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    132  1.3  jmcneill 	.wk_no = SUN6I_RTC_WK_NO,
    133  1.3  jmcneill 	.hour = SUN6I_RTC_HOUR,
    134  1.3  jmcneill 	.minute = SUN6I_RTC_MINUTE,
    135  1.3  jmcneill 	.second = SUN6I_RTC_SECOND,
    136  1.3  jmcneill 	.base_year = SUN6I_RTC_BASE_YEAR,
    137  1.6   thorpej 
    138  1.6   thorpej 	.iosc_rate = 667000,
    139  1.6   thorpej 	.flags = SUNXI_RTC_F_HAS_VAR_PRESCALER,
    140  1.3  jmcneill };
    141  1.1  jmcneill 
    142  1.3  jmcneill static const struct sunxi_rtc_config sun7i_rtc_config = {
    143  1.3  jmcneill 	.yy_mm_dd_reg = SUN7I_RTC_YY_MM_DD_REG,
    144  1.3  jmcneill 	.leap = SUN7I_RTC_LEAP,
    145  1.3  jmcneill 	.year = SUN7I_RTC_YEAR,
    146  1.3  jmcneill 	.month = SUN7I_RTC_MONTH,
    147  1.3  jmcneill 	.day = SUN7I_RTC_DAY,
    148  1.3  jmcneill 	.hh_mm_ss_reg = SUN7I_RTC_HH_MM_SS_REG,
    149  1.3  jmcneill 	.wk_no = SUN7I_RTC_WK_NO,
    150  1.3  jmcneill 	.hour = SUN7I_RTC_HOUR,
    151  1.3  jmcneill 	.minute = SUN7I_RTC_MINUTE,
    152  1.3  jmcneill 	.second = SUN7I_RTC_SECOND,
    153  1.3  jmcneill 	.base_year = SUN7I_RTC_BASE_YEAR,
    154  1.2  jmcneill };
    155  1.2  jmcneill 
    156  1.6   thorpej static const struct sunxi_rtc_config sun8i_a23_rtc_config = {
    157  1.6   thorpej 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    158  1.6   thorpej 	.leap = SUN6I_RTC_LEAP,
    159  1.6   thorpej 	.year = SUN6I_RTC_YEAR,
    160  1.6   thorpej 	.month = SUN6I_RTC_MONTH,
    161  1.6   thorpej 	.day = SUN6I_RTC_DAY,
    162  1.6   thorpej 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    163  1.6   thorpej 	.wk_no = SUN6I_RTC_WK_NO,
    164  1.6   thorpej 	.hour = SUN6I_RTC_HOUR,
    165  1.6   thorpej 	.minute = SUN6I_RTC_MINUTE,
    166  1.6   thorpej 	.second = SUN6I_RTC_SECOND,
    167  1.6   thorpej 	.base_year = SUN6I_RTC_BASE_YEAR,
    168  1.6   thorpej 
    169  1.6   thorpej 	.iosc_rate = 667000,
    170  1.6   thorpej 	.flags = SUNXI_RTC_F_HAS_VAR_PRESCALER,
    171  1.6   thorpej };
    172  1.6   thorpej 
    173  1.6   thorpej static const struct sunxi_rtc_config sun8i_r40_rtc_config = {
    174  1.6   thorpej 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    175  1.6   thorpej 	.leap = SUN6I_RTC_LEAP,
    176  1.6   thorpej 	.year = SUN6I_RTC_YEAR,
    177  1.6   thorpej 	.month = SUN6I_RTC_MONTH,
    178  1.6   thorpej 	.day = SUN6I_RTC_DAY,
    179  1.6   thorpej 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    180  1.6   thorpej 	.wk_no = SUN6I_RTC_WK_NO,
    181  1.6   thorpej 	.hour = SUN6I_RTC_HOUR,
    182  1.6   thorpej 	.minute = SUN6I_RTC_MINUTE,
    183  1.6   thorpej 	.second = SUN6I_RTC_SECOND,
    184  1.6   thorpej 	.base_year = SUN6I_RTC_BASE_YEAR,
    185  1.6   thorpej 
    186  1.6   thorpej 	.iosc_rate = 16000000,
    187  1.6   thorpej 	.fixed_prescaler = 512,
    188  1.6   thorpej };
    189  1.6   thorpej 
    190  1.6   thorpej static const struct sunxi_rtc_config sun8i_v3_rtc_config = {
    191  1.6   thorpej 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    192  1.6   thorpej 	.leap = SUN6I_RTC_LEAP,
    193  1.6   thorpej 	.year = SUN6I_RTC_YEAR,
    194  1.6   thorpej 	.month = SUN6I_RTC_MONTH,
    195  1.6   thorpej 	.day = SUN6I_RTC_DAY,
    196  1.6   thorpej 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    197  1.6   thorpej 	.wk_no = SUN6I_RTC_WK_NO,
    198  1.6   thorpej 	.hour = SUN6I_RTC_HOUR,
    199  1.6   thorpej 	.minute = SUN6I_RTC_MINUTE,
    200  1.6   thorpej 	.second = SUN6I_RTC_SECOND,
    201  1.6   thorpej 	.base_year = SUN6I_RTC_BASE_YEAR,
    202  1.6   thorpej 
    203  1.6   thorpej 	.iosc_rate = 32000,
    204  1.6   thorpej };
    205  1.6   thorpej 
    206  1.6   thorpej static const struct sunxi_rtc_config sun8i_h3_rtc_config = {
    207  1.6   thorpej 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    208  1.6   thorpej 	.leap = SUN6I_RTC_LEAP,
    209  1.6   thorpej 	.year = SUN6I_RTC_YEAR,
    210  1.6   thorpej 	.month = SUN6I_RTC_MONTH,
    211  1.6   thorpej 	.day = SUN6I_RTC_DAY,
    212  1.6   thorpej 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    213  1.6   thorpej 	.wk_no = SUN6I_RTC_WK_NO,
    214  1.6   thorpej 	.hour = SUN6I_RTC_HOUR,
    215  1.6   thorpej 	.minute = SUN6I_RTC_MINUTE,
    216  1.6   thorpej 	.second = SUN6I_RTC_SECOND,
    217  1.6   thorpej 	.base_year = SUN6I_RTC_BASE_YEAR,
    218  1.6   thorpej 
    219  1.6   thorpej 	.iosc_rate = 16000000,
    220  1.6   thorpej 	.fixed_prescaler = 32,
    221  1.6   thorpej 	.flags = SUNXI_RTC_F_HAS_VAR_PRESCALER,
    222  1.6   thorpej };
    223  1.6   thorpej 
    224  1.6   thorpej static const struct sunxi_rtc_config sun50i_h6_rtc_config = {
    225  1.6   thorpej 	.yy_mm_dd_reg = SUN6I_RTC_YY_MM_DD_REG,
    226  1.6   thorpej 	.leap = SUN6I_RTC_LEAP,
    227  1.6   thorpej 	.year = SUN6I_RTC_YEAR,
    228  1.6   thorpej 	.month = SUN6I_RTC_MONTH,
    229  1.6   thorpej 	.day = SUN6I_RTC_DAY,
    230  1.6   thorpej 	.hh_mm_ss_reg = SUN6I_RTC_HH_MM_SS_REG,
    231  1.6   thorpej 	.wk_no = SUN6I_RTC_WK_NO,
    232  1.6   thorpej 	.hour = SUN6I_RTC_HOUR,
    233  1.6   thorpej 	.minute = SUN6I_RTC_MINUTE,
    234  1.6   thorpej 	.second = SUN6I_RTC_SECOND,
    235  1.6   thorpej 	.base_year = SUN6I_RTC_BASE_YEAR,
    236  1.6   thorpej 
    237  1.6   thorpej 	.iosc_rate = 16000000,
    238  1.6   thorpej 	.fixed_prescaler = 32,
    239  1.6   thorpej 	.auto_swt_bypass = SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS,
    240  1.6   thorpej 	.ext_losc_en = SUN6I_LOSC_CTRL_EXT_LOSC_EN,
    241  1.6   thorpej 	.flags = SUNXI_RTC_F_HAS_VAR_PRESCALER,
    242  1.6   thorpej };
    243  1.6   thorpej 
    244  1.2  jmcneill static const struct of_compat_data compat_data[] = {
    245  1.3  jmcneill 	{ "allwinner,sun4i-a10-rtc",		(uintptr_t)&sun4i_rtc_config },
    246  1.6   thorpej 	{ "allwinner,sun6i-a31-rtc",		(uintptr_t)&sun6i_a31_rtc_config },
    247  1.3  jmcneill 	{ "allwinner,sun7i-a20-rtc",		(uintptr_t)&sun7i_rtc_config },
    248  1.6   thorpej 	{ "allwinner,sun8i-a23-rtc",		(uintptr_t)&sun8i_a23_rtc_config },
    249  1.6   thorpej 	{ "allwinner,sun8i-r40-rtc",		(uintptr_t)&sun8i_r40_rtc_config },
    250  1.6   thorpej 	{ "allwinner,sun8i-v3-rtc",		(uintptr_t)&sun8i_v3_rtc_config },
    251  1.6   thorpej 	{ "allwinner,sun8i-h3-rtc",		(uintptr_t)&sun8i_h3_rtc_config },
    252  1.6   thorpej 	{ "allwinner,sun50i-h5-rtc",		(uintptr_t)&sun8i_h3_rtc_config },
    253  1.6   thorpej 	{ "allwinner,sun50i-h6-rtc",		(uintptr_t)&sun50i_h6_rtc_config },
    254  1.2  jmcneill 	{ NULL }
    255  1.1  jmcneill };
    256  1.1  jmcneill 
    257  1.6   thorpej #define	SUNXI_RTC_CLK_LOSC	0
    258  1.6   thorpej #define	SUNXI_RTC_CLK_LOSC_GATE	1
    259  1.6   thorpej #define	SUNXI_RTC_CLK_IOSC	2
    260  1.6   thorpej #define	SUNXI_RTC_NCLKS		3
    261  1.6   thorpej 
    262  1.1  jmcneill struct sunxi_rtc_softc {
    263  1.1  jmcneill 	device_t sc_dev;
    264  1.1  jmcneill 	bus_space_tag_t sc_bst;
    265  1.1  jmcneill 	bus_space_handle_t sc_bsh;
    266  1.1  jmcneill 	struct todr_chip_handle sc_todr;
    267  1.3  jmcneill 	const struct sunxi_rtc_config *sc_conf;
    268  1.6   thorpej 
    269  1.6   thorpej 	int sc_phandle;
    270  1.6   thorpej 
    271  1.6   thorpej 	struct clk *sc_parent_clk;	/* external oscillator */
    272  1.6   thorpej 
    273  1.6   thorpej 	/*
    274  1.6   thorpej 	 * We export up to 3 clocks:
    275  1.6   thorpej 	 * [0] The local oscillator output
    276  1.6   thorpej 	 * [1] Gated version of [0]
    277  1.6   thorpej 	 * [2] The internal oscillator
    278  1.6   thorpej 	 *
    279  1.6   thorpej 	 * The local oscillator is driven either by the internal
    280  1.6   thorpej 	 * oscillator (less precise) or an external oscillator.
    281  1.6   thorpej 	 *
    282  1.6   thorpej 	 * Note that these are the order they appear in the device
    283  1.6   thorpej 	 * tree "clock-output-names" property for our node.  Not
    284  1.6   thorpej 	 * all flavors of the Allwinner SoCs export all of these
    285  1.6   thorpej 	 * clocks, so we export only those that appear in the
    286  1.6   thorpej 	 * "clock-output-names" property.
    287  1.6   thorpej 	 */
    288  1.6   thorpej 	const char *sc_clk_names[SUNXI_RTC_NCLKS];
    289  1.6   thorpej 	struct clk sc_clks[SUNXI_RTC_NCLKS];
    290  1.6   thorpej 	kmutex_t sc_clk_mutex;
    291  1.6   thorpej 	struct clk_domain sc_clkdom;
    292  1.1  jmcneill };
    293  1.1  jmcneill 
    294  1.1  jmcneill #define RTC_READ(sc, reg) \
    295  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    296  1.1  jmcneill #define RTC_WRITE(sc, reg, val) \
    297  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    298  1.1  jmcneill 
    299  1.1  jmcneill static int	sunxi_rtc_match(device_t, cfdata_t, void *);
    300  1.1  jmcneill static void	sunxi_rtc_attach(device_t, device_t, void *);
    301  1.1  jmcneill 
    302  1.3  jmcneill static int	sunxi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
    303  1.3  jmcneill static int	sunxi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
    304  1.1  jmcneill 
    305  1.6   thorpej static struct clk *
    306  1.6   thorpej 		sunxi_rtc_clk_get(void *, const char *);
    307  1.6   thorpej static u_int	sunxi_rtc_clk_get_rate(void *, struct clk *);
    308  1.6   thorpej static int	sunxi_rtc_clk_enable(void *, struct clk *);
    309  1.6   thorpej static int	sunxi_rtc_clk_disable(void *, struct clk *);
    310  1.6   thorpej static int	sunxi_rtc_clk_set_parent(void *, struct clk *, struct clk *);
    311  1.6   thorpej static struct clk *
    312  1.6   thorpej 		sunxi_rtc_clk_get_parent(void *, struct clk *);
    313  1.6   thorpej 
    314  1.6   thorpej static const struct clk_funcs sunxi_rtc_clk_funcs = {
    315  1.6   thorpej 	.get = sunxi_rtc_clk_get,
    316  1.6   thorpej 	.get_rate = sunxi_rtc_clk_get_rate,
    317  1.6   thorpej 	.enable = sunxi_rtc_clk_enable,
    318  1.6   thorpej 	.disable = sunxi_rtc_clk_disable,
    319  1.6   thorpej 	.set_parent = sunxi_rtc_clk_set_parent,
    320  1.6   thorpej 	.get_parent = sunxi_rtc_clk_get_parent,
    321  1.6   thorpej };
    322  1.6   thorpej 
    323  1.6   thorpej static struct clk *
    324  1.6   thorpej sunxi_rtc_clock_decode(device_t dev, int cc_phandle, const void *data,
    325  1.6   thorpej 		       size_t len)
    326  1.6   thorpej {
    327  1.6   thorpej 	struct sunxi_rtc_softc * const sc = device_private(dev);
    328  1.6   thorpej 
    329  1.6   thorpej 	if (len != 4)
    330  1.6   thorpej 		return NULL;
    331  1.6   thorpej 
    332  1.6   thorpej 	const u_int clock_id = be32dec(data);
    333  1.6   thorpej 	if (clock_id >= SUNXI_RTC_NCLKS)
    334  1.6   thorpej 		return NULL;
    335  1.6   thorpej 
    336  1.6   thorpej 	if (sc->sc_clk_names[clock_id] == NULL)
    337  1.6   thorpej 		return NULL;
    338  1.6   thorpej 
    339  1.6   thorpej 	return &sc->sc_clks[clock_id];
    340  1.6   thorpej }
    341  1.6   thorpej 
    342  1.6   thorpej static const struct fdtbus_clock_controller_func sunxi_rtc_fdtclock_funcs = {
    343  1.6   thorpej 	.decode = sunxi_rtc_clock_decode,
    344  1.6   thorpej };
    345  1.6   thorpej 
    346  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_rtc, sizeof(struct sunxi_rtc_softc),
    347  1.1  jmcneill 	sunxi_rtc_match, sunxi_rtc_attach, NULL, NULL);
    348  1.1  jmcneill 
    349  1.1  jmcneill static int
    350  1.1  jmcneill sunxi_rtc_match(device_t parent, cfdata_t cf, void *aux)
    351  1.1  jmcneill {
    352  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    353  1.1  jmcneill 
    354  1.2  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    355  1.1  jmcneill }
    356  1.1  jmcneill 
    357  1.1  jmcneill static void
    358  1.1  jmcneill sunxi_rtc_attach(device_t parent, device_t self, void *aux)
    359  1.1  jmcneill {
    360  1.1  jmcneill 	struct sunxi_rtc_softc * const sc = device_private(self);
    361  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    362  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    363  1.1  jmcneill 	bus_addr_t addr;
    364  1.1  jmcneill 	bus_size_t size;
    365  1.1  jmcneill 
    366  1.6   thorpej 	sc->sc_phandle = phandle;
    367  1.6   thorpej 
    368  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    369  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    370  1.1  jmcneill 		return;
    371  1.1  jmcneill 	}
    372  1.1  jmcneill 
    373  1.1  jmcneill 	sc->sc_dev = self;
    374  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    375  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    376  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    377  1.1  jmcneill 		return;
    378  1.1  jmcneill 	}
    379  1.3  jmcneill 	sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
    380  1.1  jmcneill 
    381  1.1  jmcneill 	aprint_naive("\n");
    382  1.1  jmcneill 	aprint_normal(": RTC\n");
    383  1.1  jmcneill 
    384  1.6   thorpej 	mutex_init(&sc->sc_clk_mutex, MUTEX_DEFAULT, IPL_HIGH);
    385  1.6   thorpej 
    386  1.1  jmcneill 	sc->sc_todr.cookie = sc;
    387  1.3  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = sunxi_rtc_gettime;
    388  1.3  jmcneill 	sc->sc_todr.todr_settime_ymdhms = sunxi_rtc_settime;
    389  1.1  jmcneill 
    390  1.1  jmcneill 	fdtbus_todr_attach(self, phandle, &sc->sc_todr);
    391  1.6   thorpej 
    392  1.6   thorpej 	sc->sc_parent_clk = fdtbus_clock_get_index(phandle, 0);
    393  1.6   thorpej 
    394  1.6   thorpej 	if (sc->sc_parent_clk == NULL || sc->sc_conf->iosc_rate == 0)
    395  1.6   thorpej 		return;
    396  1.6   thorpej 
    397  1.6   thorpej 	uint32_t reg = SUN6I_LOSC_CTRL_KEY;
    398  1.6   thorpej 	if (sc->sc_conf->auto_swt_bypass) {
    399  1.6   thorpej 		/*
    400  1.6   thorpej 		 * Disable auto-switching to the internal oscillator
    401  1.6   thorpej 		 * if the external oscillator disappears.
    402  1.6   thorpej 		 */
    403  1.6   thorpej 		reg |= sc->sc_conf->auto_swt_bypass;
    404  1.6   thorpej 		RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg);
    405  1.6   thorpej 	}
    406  1.6   thorpej 
    407  1.6   thorpej 	/* Switch to the external oscillator by default. */
    408  1.6   thorpej 	reg |= SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en;
    409  1.6   thorpej 	RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg);
    410  1.6   thorpej 
    411  1.6   thorpej 	sc->sc_clkdom.name = device_xname(sc->sc_dev);
    412  1.6   thorpej 	sc->sc_clkdom.funcs = &sunxi_rtc_clk_funcs;
    413  1.6   thorpej 	sc->sc_clkdom.priv = sc;
    414  1.6   thorpej 
    415  1.6   thorpej 	unsigned int i;
    416  1.6   thorpej 	for (i = 0; i < SUNXI_RTC_NCLKS; i++) {
    417  1.6   thorpej 		sc->sc_clk_names[i] = fdtbus_get_string_index(phandle,
    418  1.6   thorpej 		    "clock-output-names", i);
    419  1.6   thorpej 		if (sc->sc_clk_names[i] == NULL)
    420  1.6   thorpej 			break;
    421  1.6   thorpej 		sc->sc_clks[i].domain = &sc->sc_clkdom;
    422  1.6   thorpej 		sc->sc_clks[i].name = sc->sc_clk_names[i];
    423  1.6   thorpej 		clk_attach(&sc->sc_clks[i]);
    424  1.6   thorpej 	}
    425  1.6   thorpej 
    426  1.6   thorpej 	fdtbus_register_clock_controller(sc->sc_dev, sc->sc_phandle,
    427  1.6   thorpej 	    &sunxi_rtc_fdtclock_funcs);
    428  1.1  jmcneill }
    429  1.1  jmcneill 
    430  1.1  jmcneill static int
    431  1.3  jmcneill sunxi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    432  1.1  jmcneill {
    433  1.1  jmcneill 	struct sunxi_rtc_softc *sc = tch->cookie;
    434  1.3  jmcneill 	const struct sunxi_rtc_config *conf = sc->sc_conf;
    435  1.1  jmcneill 
    436  1.3  jmcneill 	const uint32_t yymmdd = RTC_READ(sc, conf->yy_mm_dd_reg);
    437  1.3  jmcneill 	const uint32_t hhmmss = RTC_READ(sc, conf->hh_mm_ss_reg);
    438  1.1  jmcneill 
    439  1.3  jmcneill 	dt->dt_year = __SHIFTOUT(yymmdd, conf->year) + conf->base_year;
    440  1.3  jmcneill 	dt->dt_mon = __SHIFTOUT(yymmdd, conf->month);
    441  1.3  jmcneill 	dt->dt_day = __SHIFTOUT(yymmdd, conf->day);
    442  1.3  jmcneill 	dt->dt_wday = __SHIFTOUT(hhmmss, conf->wk_no);
    443  1.3  jmcneill 	dt->dt_hour = __SHIFTOUT(hhmmss, conf->hour);
    444  1.3  jmcneill 	dt->dt_min = __SHIFTOUT(hhmmss, conf->minute);
    445  1.3  jmcneill 	dt->dt_sec = __SHIFTOUT(hhmmss, conf->second);
    446  1.1  jmcneill 
    447  1.1  jmcneill 	return 0;
    448  1.1  jmcneill }
    449  1.1  jmcneill 
    450  1.1  jmcneill static int
    451  1.3  jmcneill sunxi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    452  1.1  jmcneill {
    453  1.1  jmcneill 	struct sunxi_rtc_softc *sc = tch->cookie;
    454  1.3  jmcneill 	const struct sunxi_rtc_config *conf = sc->sc_conf;
    455  1.1  jmcneill 	uint32_t yymmdd, hhmmss, maxyear;
    456  1.1  jmcneill 
    457  1.1  jmcneill 	/*
    458  1.1  jmcneill 	 * Sanity check the date before writing it back
    459  1.1  jmcneill 	 */
    460  1.3  jmcneill 	if (dt->dt_year < conf->base_year) {
    461  1.4  christos 		aprint_normal_dev(sc->sc_dev, "year pre the epoch: %" PRIu64
    462  1.4  christos 		    ", not writing back time\n", dt->dt_year);
    463  1.1  jmcneill 		return EIO;
    464  1.1  jmcneill 	}
    465  1.3  jmcneill 	maxyear = __SHIFTOUT(0xffffffff, conf->year) + conf->base_year;
    466  1.1  jmcneill 	if (dt->dt_year > maxyear) {
    467  1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
    468  1.4  christos 		    " %" PRIu64 ", not writing back time\n", dt->dt_year);
    469  1.1  jmcneill 		return EIO;
    470  1.1  jmcneill 	}
    471  1.1  jmcneill 
    472  1.3  jmcneill 	yymmdd = __SHIFTIN(dt->dt_year - conf->base_year, conf->year);
    473  1.3  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_mon, conf->month);
    474  1.3  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_day, conf->day);
    475  1.3  jmcneill 
    476  1.3  jmcneill 	hhmmss = __SHIFTIN(dt->dt_wday, conf->wk_no);
    477  1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_hour, conf->hour);
    478  1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_min, conf->minute);
    479  1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_sec, conf->second);
    480  1.1  jmcneill 
    481  1.3  jmcneill 	RTC_WRITE(sc, conf->yy_mm_dd_reg, yymmdd);
    482  1.3  jmcneill 	RTC_WRITE(sc, conf->hh_mm_ss_reg, hhmmss);
    483  1.1  jmcneill 
    484  1.1  jmcneill 	return 0;
    485  1.1  jmcneill }
    486  1.6   thorpej 
    487  1.6   thorpej static struct clk *
    488  1.6   thorpej sunxi_rtc_clk_get(void *priv, const char *name)
    489  1.6   thorpej {
    490  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    491  1.6   thorpej 	u_int i;
    492  1.6   thorpej 
    493  1.6   thorpej 	for (i = 0; i < SUNXI_RTC_NCLKS; i++) {
    494  1.6   thorpej 		if (sc->sc_clk_names[i] != NULL &&
    495  1.6   thorpej 		    strcmp(sc->sc_clk_names[i], name) == 0) {
    496  1.6   thorpej 			return &sc->sc_clks[i];
    497  1.6   thorpej 		}
    498  1.6   thorpej 	}
    499  1.6   thorpej 
    500  1.6   thorpej 	return NULL;
    501  1.6   thorpej }
    502  1.6   thorpej 
    503  1.6   thorpej static u_int
    504  1.6   thorpej sunxi_rtc_clk_get_rate(void *priv, struct clk *clk)
    505  1.6   thorpej {
    506  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    507  1.6   thorpej 
    508  1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC]) {
    509  1.6   thorpej 		KASSERT(sc->sc_clk_names[SUNXI_RTC_CLK_IOSC] != NULL);
    510  1.6   thorpej 		KASSERT(sc->sc_conf->iosc_rate != 0);
    511  1.6   thorpej 		return sc->sc_conf->iosc_rate;
    512  1.6   thorpej 	}
    513  1.6   thorpej 
    514  1.6   thorpej 	KASSERT(sc->sc_parent_clk != NULL);
    515  1.6   thorpej 	u_int parent_rate = clk_get_rate(sc->sc_parent_clk);
    516  1.6   thorpej 	uint32_t prescaler = 0;
    517  1.6   thorpej 
    518  1.6   thorpej 	if (RTC_READ(sc, SUN6I_LOSC_CTRL_REG) & SUN6I_LOSC_CTRL_EXT_OSC)
    519  1.6   thorpej 		return parent_rate;
    520  1.6   thorpej 
    521  1.6   thorpej 	if (sc->sc_conf->fixed_prescaler)
    522  1.6   thorpej 		parent_rate /= sc->sc_conf->fixed_prescaler;
    523  1.6   thorpej 
    524  1.6   thorpej 	if (sc->sc_conf->flags & SUNXI_RTC_F_HAS_VAR_PRESCALER) {
    525  1.6   thorpej 		prescaler =
    526  1.6   thorpej 		    __SHIFTOUT(RTC_READ(sc, SUN6I_INTOSC_CLK_PRESCAL_REG),
    527  1.6   thorpej 			       SUN6I_INTOSC_CLK_PRESCAL);
    528  1.6   thorpej 	}
    529  1.6   thorpej 
    530  1.6   thorpej 	return parent_rate / (prescaler + 1);
    531  1.6   thorpej }
    532  1.6   thorpej 
    533  1.6   thorpej static int
    534  1.6   thorpej sunxi_rtc_clk_enable(void *priv, struct clk *clk)
    535  1.6   thorpej {
    536  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    537  1.6   thorpej 
    538  1.6   thorpej 	if (clk != &sc->sc_clks[SUNXI_RTC_CLK_LOSC_GATE])
    539  1.6   thorpej 		return 0;
    540  1.6   thorpej 
    541  1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    542  1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_RTC_LOSC_OUT_GATING_REG);
    543  1.6   thorpej 	if ((reg & SUN6I_RTC_LOSC_OUT_EN) == 0) {
    544  1.6   thorpej 		reg |= SUN6I_RTC_LOSC_OUT_EN;
    545  1.6   thorpej 		RTC_WRITE(sc, SUN6I_RTC_LOSC_OUT_GATING_REG, reg);
    546  1.6   thorpej 	}
    547  1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    548  1.6   thorpej 
    549  1.6   thorpej 	return 0;
    550  1.6   thorpej }
    551  1.6   thorpej 
    552  1.6   thorpej static int
    553  1.6   thorpej sunxi_rtc_clk_disable(void *priv, struct clk *clk)
    554  1.6   thorpej {
    555  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    556  1.6   thorpej 
    557  1.6   thorpej 	if (clk != &sc->sc_clks[SUNXI_RTC_CLK_LOSC_GATE])
    558  1.6   thorpej 		return EINVAL;
    559  1.6   thorpej 
    560  1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    561  1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_RTC_LOSC_OUT_GATING_REG);
    562  1.6   thorpej 	if (reg & SUN6I_RTC_LOSC_OUT_EN) {
    563  1.6   thorpej 		reg &= ~SUN6I_RTC_LOSC_OUT_EN;
    564  1.6   thorpej 		RTC_WRITE(sc, SUN6I_RTC_LOSC_OUT_GATING_REG, reg);
    565  1.6   thorpej 	}
    566  1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    567  1.6   thorpej 
    568  1.6   thorpej 	return 0;
    569  1.6   thorpej }
    570  1.6   thorpej 
    571  1.6   thorpej static int
    572  1.6   thorpej sunxi_rtc_clk_set_parent(void *priv, struct clk *clk, struct clk *parent_clk)
    573  1.6   thorpej {
    574  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    575  1.6   thorpej 
    576  1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    577  1.6   thorpej 		return EINVAL;
    578  1.6   thorpej 
    579  1.6   thorpej 	if (parent_clk != sc->sc_parent_clk &&
    580  1.6   thorpej 	    parent_clk != &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    581  1.6   thorpej 		return EINVAL;
    582  1.6   thorpej 
    583  1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    584  1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_LOSC_CTRL_REG);
    585  1.6   thorpej 	if (parent_clk == sc->sc_parent_clk)
    586  1.6   thorpej 		reg |= SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en;
    587  1.6   thorpej 	else
    588  1.6   thorpej 		reg &= ~(SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en);
    589  1.6   thorpej 	RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg | SUN6I_LOSC_CTRL_KEY);
    590  1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    591  1.6   thorpej 
    592  1.6   thorpej 	return 0;
    593  1.6   thorpej }
    594  1.6   thorpej 
    595  1.6   thorpej static struct clk *
    596  1.6   thorpej sunxi_rtc_clk_get_parent(void *priv, struct clk *clk)
    597  1.6   thorpej {
    598  1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    599  1.6   thorpej 	uint32_t reg;
    600  1.6   thorpej 
    601  1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    602  1.6   thorpej 		return NULL;
    603  1.6   thorpej 
    604  1.6   thorpej 	reg = RTC_READ(sc, SUN6I_LOSC_CTRL_REG);
    605  1.6   thorpej 	if (reg & SUN6I_LOSC_CTRL_EXT_OSC)
    606  1.6   thorpej 		return sc->sc_parent_clk;
    607  1.6   thorpej 
    608  1.6   thorpej 	/*
    609  1.6   thorpej 	 * We switch to the external oscillator at attach time becacuse
    610  1.6   thorpej 	 * it's higher quality than the internal one.  If we haven't
    611  1.6   thorpej 	 * exported the internal oscillator to the clock tree, then
    612  1.6   thorpej 	 * we shouldn't get here.
    613  1.6   thorpej 	 */
    614  1.6   thorpej 	KASSERT(sc->sc_clk_names[SUNXI_RTC_CLK_IOSC] != NULL);
    615  1.6   thorpej 	return &sc->sc_clks[SUNXI_RTC_CLK_IOSC];
    616  1.6   thorpej }
    617