Home | History | Annotate | Line # | Download | only in sunxi
sunxi_rtc.c revision 1.12
      1  1.12   thorpej /* $NetBSD: sunxi_rtc.c,v 1.12 2025/09/08 13:06:16 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.12   thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.12 2025/09/08 13:06:16 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.7   thorpej static const struct device_compatible_entry compat_data[] = {
    245   1.7   thorpej 	{ .compat = "allwinner,sun4i-a10-rtc",
    246   1.7   thorpej 	  .data = &sun4i_rtc_config },
    247   1.7   thorpej 	{ .compat = "allwinner,sun6i-a31-rtc",
    248   1.7   thorpej 	  .data = &sun6i_a31_rtc_config },
    249   1.7   thorpej 	{ .compat = "allwinner,sun7i-a20-rtc",
    250   1.7   thorpej 	  .data = &sun7i_rtc_config },
    251   1.7   thorpej 	{ .compat = "allwinner,sun8i-a23-rtc",
    252   1.7   thorpej 	  .data = &sun8i_a23_rtc_config },
    253   1.7   thorpej 	{ .compat = "allwinner,sun8i-r40-rtc",
    254   1.7   thorpej 	  .data = &sun8i_r40_rtc_config },
    255   1.7   thorpej 	{ .compat = "allwinner,sun8i-v3-rtc",
    256   1.7   thorpej 	  .data = &sun8i_v3_rtc_config },
    257   1.7   thorpej 	{ .compat = "allwinner,sun8i-h3-rtc",
    258   1.7   thorpej 	  .data = &sun8i_h3_rtc_config },
    259   1.7   thorpej 	{ .compat = "allwinner,sun50i-h5-rtc",
    260   1.7   thorpej 	  .data = &sun8i_h3_rtc_config },
    261   1.7   thorpej 	{ .compat = "allwinner,sun50i-h6-rtc",
    262   1.7   thorpej 	  .data = &sun50i_h6_rtc_config },
    263   1.7   thorpej 
    264   1.9   thorpej 	DEVICE_COMPAT_EOL
    265   1.1  jmcneill };
    266   1.1  jmcneill 
    267   1.6   thorpej #define	SUNXI_RTC_CLK_LOSC	0
    268   1.6   thorpej #define	SUNXI_RTC_CLK_LOSC_GATE	1
    269   1.6   thorpej #define	SUNXI_RTC_CLK_IOSC	2
    270   1.6   thorpej #define	SUNXI_RTC_NCLKS		3
    271   1.6   thorpej 
    272   1.1  jmcneill struct sunxi_rtc_softc {
    273   1.1  jmcneill 	device_t sc_dev;
    274   1.1  jmcneill 	bus_space_tag_t sc_bst;
    275   1.1  jmcneill 	bus_space_handle_t sc_bsh;
    276   1.1  jmcneill 	struct todr_chip_handle sc_todr;
    277   1.3  jmcneill 	const struct sunxi_rtc_config *sc_conf;
    278   1.6   thorpej 
    279   1.6   thorpej 	int sc_phandle;
    280   1.6   thorpej 
    281   1.6   thorpej 	struct clk *sc_parent_clk;	/* external oscillator */
    282   1.6   thorpej 
    283   1.6   thorpej 	/*
    284   1.6   thorpej 	 * We export up to 3 clocks:
    285   1.6   thorpej 	 * [0] The local oscillator output
    286   1.6   thorpej 	 * [1] Gated version of [0]
    287   1.6   thorpej 	 * [2] The internal oscillator
    288   1.6   thorpej 	 *
    289   1.6   thorpej 	 * The local oscillator is driven either by the internal
    290   1.6   thorpej 	 * oscillator (less precise) or an external oscillator.
    291   1.6   thorpej 	 *
    292   1.6   thorpej 	 * Note that these are the order they appear in the device
    293   1.6   thorpej 	 * tree "clock-output-names" property for our node.  Not
    294   1.6   thorpej 	 * all flavors of the Allwinner SoCs export all of these
    295   1.6   thorpej 	 * clocks, so we export only those that appear in the
    296   1.6   thorpej 	 * "clock-output-names" property.
    297   1.6   thorpej 	 */
    298   1.6   thorpej 	const char *sc_clk_names[SUNXI_RTC_NCLKS];
    299   1.6   thorpej 	struct clk sc_clks[SUNXI_RTC_NCLKS];
    300   1.6   thorpej 	kmutex_t sc_clk_mutex;
    301   1.6   thorpej 	struct clk_domain sc_clkdom;
    302   1.1  jmcneill };
    303   1.1  jmcneill 
    304   1.1  jmcneill #define RTC_READ(sc, reg) \
    305   1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    306   1.1  jmcneill #define RTC_WRITE(sc, reg, val) \
    307   1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    308   1.1  jmcneill 
    309   1.1  jmcneill static int	sunxi_rtc_match(device_t, cfdata_t, void *);
    310   1.1  jmcneill static void	sunxi_rtc_attach(device_t, device_t, void *);
    311   1.1  jmcneill 
    312   1.3  jmcneill static int	sunxi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
    313   1.3  jmcneill static int	sunxi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
    314   1.1  jmcneill 
    315   1.6   thorpej static struct clk *
    316   1.6   thorpej 		sunxi_rtc_clk_get(void *, const char *);
    317   1.6   thorpej static u_int	sunxi_rtc_clk_get_rate(void *, struct clk *);
    318   1.6   thorpej static int	sunxi_rtc_clk_enable(void *, struct clk *);
    319   1.6   thorpej static int	sunxi_rtc_clk_disable(void *, struct clk *);
    320   1.6   thorpej static int	sunxi_rtc_clk_set_parent(void *, struct clk *, struct clk *);
    321   1.6   thorpej static struct clk *
    322   1.6   thorpej 		sunxi_rtc_clk_get_parent(void *, struct clk *);
    323   1.6   thorpej 
    324   1.6   thorpej static const struct clk_funcs sunxi_rtc_clk_funcs = {
    325   1.6   thorpej 	.get = sunxi_rtc_clk_get,
    326   1.6   thorpej 	.get_rate = sunxi_rtc_clk_get_rate,
    327   1.6   thorpej 	.enable = sunxi_rtc_clk_enable,
    328   1.6   thorpej 	.disable = sunxi_rtc_clk_disable,
    329   1.6   thorpej 	.set_parent = sunxi_rtc_clk_set_parent,
    330   1.6   thorpej 	.get_parent = sunxi_rtc_clk_get_parent,
    331   1.6   thorpej };
    332   1.6   thorpej 
    333   1.6   thorpej static struct clk *
    334   1.6   thorpej sunxi_rtc_clock_decode(device_t dev, int cc_phandle, const void *data,
    335   1.6   thorpej 		       size_t len)
    336   1.6   thorpej {
    337   1.6   thorpej 	struct sunxi_rtc_softc * const sc = device_private(dev);
    338   1.6   thorpej 
    339   1.6   thorpej 	if (len != 4)
    340   1.6   thorpej 		return NULL;
    341   1.6   thorpej 
    342   1.6   thorpej 	const u_int clock_id = be32dec(data);
    343   1.6   thorpej 	if (clock_id >= SUNXI_RTC_NCLKS)
    344   1.6   thorpej 		return NULL;
    345   1.6   thorpej 
    346   1.6   thorpej 	if (sc->sc_clk_names[clock_id] == NULL)
    347   1.6   thorpej 		return NULL;
    348   1.6   thorpej 
    349   1.6   thorpej 	return &sc->sc_clks[clock_id];
    350   1.6   thorpej }
    351   1.6   thorpej 
    352   1.6   thorpej static const struct fdtbus_clock_controller_func sunxi_rtc_fdtclock_funcs = {
    353   1.6   thorpej 	.decode = sunxi_rtc_clock_decode,
    354   1.6   thorpej };
    355   1.6   thorpej 
    356   1.1  jmcneill CFATTACH_DECL_NEW(sunxi_rtc, sizeof(struct sunxi_rtc_softc),
    357   1.1  jmcneill 	sunxi_rtc_match, sunxi_rtc_attach, NULL, NULL);
    358   1.1  jmcneill 
    359   1.1  jmcneill static int
    360   1.1  jmcneill sunxi_rtc_match(device_t parent, cfdata_t cf, void *aux)
    361   1.1  jmcneill {
    362   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    363   1.1  jmcneill 
    364  1.10   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    365   1.1  jmcneill }
    366   1.1  jmcneill 
    367   1.1  jmcneill static void
    368   1.1  jmcneill sunxi_rtc_attach(device_t parent, device_t self, void *aux)
    369   1.1  jmcneill {
    370   1.1  jmcneill 	struct sunxi_rtc_softc * const sc = device_private(self);
    371   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    372   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    373   1.1  jmcneill 	bus_addr_t addr;
    374   1.1  jmcneill 	bus_size_t size;
    375   1.1  jmcneill 
    376   1.6   thorpej 	sc->sc_phandle = phandle;
    377   1.6   thorpej 
    378   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    379   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    380   1.1  jmcneill 		return;
    381   1.1  jmcneill 	}
    382   1.1  jmcneill 
    383   1.1  jmcneill 	sc->sc_dev = self;
    384   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    385   1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    386   1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    387   1.1  jmcneill 		return;
    388   1.1  jmcneill 	}
    389  1.10   thorpej 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
    390   1.1  jmcneill 
    391   1.1  jmcneill 	aprint_naive("\n");
    392   1.1  jmcneill 	aprint_normal(": RTC\n");
    393   1.1  jmcneill 
    394   1.6   thorpej 	mutex_init(&sc->sc_clk_mutex, MUTEX_DEFAULT, IPL_HIGH);
    395   1.6   thorpej 
    396  1.11   thorpej 	sc->sc_todr.todr_dev = self;
    397   1.3  jmcneill 	sc->sc_todr.todr_gettime_ymdhms = sunxi_rtc_gettime;
    398   1.3  jmcneill 	sc->sc_todr.todr_settime_ymdhms = sunxi_rtc_settime;
    399   1.1  jmcneill 
    400  1.12   thorpej 	todr_attach(&sc->sc_todr);
    401   1.6   thorpej 
    402   1.6   thorpej 	sc->sc_parent_clk = fdtbus_clock_get_index(phandle, 0);
    403   1.6   thorpej 
    404   1.6   thorpej 	if (sc->sc_parent_clk == NULL || sc->sc_conf->iosc_rate == 0)
    405   1.6   thorpej 		return;
    406   1.6   thorpej 
    407   1.6   thorpej 	uint32_t reg = SUN6I_LOSC_CTRL_KEY;
    408   1.6   thorpej 	if (sc->sc_conf->auto_swt_bypass) {
    409   1.6   thorpej 		/*
    410   1.6   thorpej 		 * Disable auto-switching to the internal oscillator
    411   1.6   thorpej 		 * if the external oscillator disappears.
    412   1.6   thorpej 		 */
    413   1.6   thorpej 		reg |= sc->sc_conf->auto_swt_bypass;
    414   1.6   thorpej 		RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg);
    415   1.6   thorpej 	}
    416   1.6   thorpej 
    417   1.6   thorpej 	/* Switch to the external oscillator by default. */
    418   1.6   thorpej 	reg |= SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en;
    419   1.6   thorpej 	RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg);
    420   1.6   thorpej 
    421   1.6   thorpej 	sc->sc_clkdom.name = device_xname(sc->sc_dev);
    422   1.6   thorpej 	sc->sc_clkdom.funcs = &sunxi_rtc_clk_funcs;
    423   1.6   thorpej 	sc->sc_clkdom.priv = sc;
    424   1.6   thorpej 
    425   1.6   thorpej 	unsigned int i;
    426   1.6   thorpej 	for (i = 0; i < SUNXI_RTC_NCLKS; i++) {
    427   1.6   thorpej 		sc->sc_clk_names[i] = fdtbus_get_string_index(phandle,
    428   1.6   thorpej 		    "clock-output-names", i);
    429   1.6   thorpej 		if (sc->sc_clk_names[i] == NULL)
    430   1.6   thorpej 			break;
    431   1.6   thorpej 		sc->sc_clks[i].domain = &sc->sc_clkdom;
    432   1.6   thorpej 		sc->sc_clks[i].name = sc->sc_clk_names[i];
    433   1.6   thorpej 		clk_attach(&sc->sc_clks[i]);
    434   1.6   thorpej 	}
    435   1.6   thorpej 
    436   1.6   thorpej 	fdtbus_register_clock_controller(sc->sc_dev, sc->sc_phandle,
    437   1.6   thorpej 	    &sunxi_rtc_fdtclock_funcs);
    438   1.1  jmcneill }
    439   1.1  jmcneill 
    440   1.1  jmcneill static int
    441   1.3  jmcneill sunxi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    442   1.1  jmcneill {
    443  1.11   thorpej 	struct sunxi_rtc_softc *sc = device_private(tch->todr_dev);
    444   1.3  jmcneill 	const struct sunxi_rtc_config *conf = sc->sc_conf;
    445   1.1  jmcneill 
    446   1.3  jmcneill 	const uint32_t yymmdd = RTC_READ(sc, conf->yy_mm_dd_reg);
    447   1.3  jmcneill 	const uint32_t hhmmss = RTC_READ(sc, conf->hh_mm_ss_reg);
    448   1.1  jmcneill 
    449   1.3  jmcneill 	dt->dt_year = __SHIFTOUT(yymmdd, conf->year) + conf->base_year;
    450   1.3  jmcneill 	dt->dt_mon = __SHIFTOUT(yymmdd, conf->month);
    451   1.3  jmcneill 	dt->dt_day = __SHIFTOUT(yymmdd, conf->day);
    452   1.3  jmcneill 	dt->dt_wday = __SHIFTOUT(hhmmss, conf->wk_no);
    453   1.3  jmcneill 	dt->dt_hour = __SHIFTOUT(hhmmss, conf->hour);
    454   1.3  jmcneill 	dt->dt_min = __SHIFTOUT(hhmmss, conf->minute);
    455   1.3  jmcneill 	dt->dt_sec = __SHIFTOUT(hhmmss, conf->second);
    456   1.1  jmcneill 
    457   1.1  jmcneill 	return 0;
    458   1.1  jmcneill }
    459   1.1  jmcneill 
    460   1.1  jmcneill static int
    461   1.3  jmcneill sunxi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    462   1.1  jmcneill {
    463  1.11   thorpej 	struct sunxi_rtc_softc *sc = device_private(tch->todr_dev);
    464   1.3  jmcneill 	const struct sunxi_rtc_config *conf = sc->sc_conf;
    465   1.1  jmcneill 	uint32_t yymmdd, hhmmss, maxyear;
    466   1.1  jmcneill 
    467   1.1  jmcneill 	/*
    468   1.1  jmcneill 	 * Sanity check the date before writing it back
    469   1.1  jmcneill 	 */
    470   1.3  jmcneill 	if (dt->dt_year < conf->base_year) {
    471   1.4  christos 		aprint_normal_dev(sc->sc_dev, "year pre the epoch: %" PRIu64
    472   1.4  christos 		    ", not writing back time\n", dt->dt_year);
    473   1.1  jmcneill 		return EIO;
    474   1.1  jmcneill 	}
    475   1.3  jmcneill 	maxyear = __SHIFTOUT(0xffffffff, conf->year) + conf->base_year;
    476   1.1  jmcneill 	if (dt->dt_year > maxyear) {
    477   1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
    478   1.4  christos 		    " %" PRIu64 ", not writing back time\n", dt->dt_year);
    479   1.1  jmcneill 		return EIO;
    480   1.1  jmcneill 	}
    481   1.1  jmcneill 
    482   1.3  jmcneill 	yymmdd = __SHIFTIN(dt->dt_year - conf->base_year, conf->year);
    483   1.3  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_mon, conf->month);
    484   1.3  jmcneill 	yymmdd |= __SHIFTIN(dt->dt_day, conf->day);
    485   1.3  jmcneill 
    486   1.3  jmcneill 	hhmmss = __SHIFTIN(dt->dt_wday, conf->wk_no);
    487   1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_hour, conf->hour);
    488   1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_min, conf->minute);
    489   1.3  jmcneill 	hhmmss |= __SHIFTIN(dt->dt_sec, conf->second);
    490   1.1  jmcneill 
    491   1.3  jmcneill 	RTC_WRITE(sc, conf->yy_mm_dd_reg, yymmdd);
    492   1.3  jmcneill 	RTC_WRITE(sc, conf->hh_mm_ss_reg, hhmmss);
    493   1.1  jmcneill 
    494   1.1  jmcneill 	return 0;
    495   1.1  jmcneill }
    496   1.6   thorpej 
    497   1.6   thorpej static struct clk *
    498   1.6   thorpej sunxi_rtc_clk_get(void *priv, const char *name)
    499   1.6   thorpej {
    500   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    501   1.6   thorpej 	u_int i;
    502   1.6   thorpej 
    503   1.6   thorpej 	for (i = 0; i < SUNXI_RTC_NCLKS; i++) {
    504   1.6   thorpej 		if (sc->sc_clk_names[i] != NULL &&
    505   1.6   thorpej 		    strcmp(sc->sc_clk_names[i], name) == 0) {
    506   1.6   thorpej 			return &sc->sc_clks[i];
    507   1.6   thorpej 		}
    508   1.6   thorpej 	}
    509   1.6   thorpej 
    510   1.6   thorpej 	return NULL;
    511   1.6   thorpej }
    512   1.6   thorpej 
    513   1.6   thorpej static u_int
    514   1.6   thorpej sunxi_rtc_clk_get_rate(void *priv, struct clk *clk)
    515   1.6   thorpej {
    516   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    517   1.6   thorpej 
    518   1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC]) {
    519   1.6   thorpej 		KASSERT(sc->sc_clk_names[SUNXI_RTC_CLK_IOSC] != NULL);
    520   1.6   thorpej 		KASSERT(sc->sc_conf->iosc_rate != 0);
    521   1.6   thorpej 		return sc->sc_conf->iosc_rate;
    522   1.6   thorpej 	}
    523   1.6   thorpej 
    524   1.6   thorpej 	KASSERT(sc->sc_parent_clk != NULL);
    525   1.6   thorpej 	u_int parent_rate = clk_get_rate(sc->sc_parent_clk);
    526   1.6   thorpej 	uint32_t prescaler = 0;
    527   1.6   thorpej 
    528   1.6   thorpej 	if (RTC_READ(sc, SUN6I_LOSC_CTRL_REG) & SUN6I_LOSC_CTRL_EXT_OSC)
    529   1.6   thorpej 		return parent_rate;
    530   1.6   thorpej 
    531   1.6   thorpej 	if (sc->sc_conf->fixed_prescaler)
    532   1.6   thorpej 		parent_rate /= sc->sc_conf->fixed_prescaler;
    533   1.6   thorpej 
    534   1.6   thorpej 	if (sc->sc_conf->flags & SUNXI_RTC_F_HAS_VAR_PRESCALER) {
    535   1.6   thorpej 		prescaler =
    536   1.6   thorpej 		    __SHIFTOUT(RTC_READ(sc, SUN6I_INTOSC_CLK_PRESCAL_REG),
    537   1.6   thorpej 			       SUN6I_INTOSC_CLK_PRESCAL);
    538   1.6   thorpej 	}
    539   1.6   thorpej 
    540   1.6   thorpej 	return parent_rate / (prescaler + 1);
    541   1.6   thorpej }
    542   1.6   thorpej 
    543   1.6   thorpej static int
    544   1.6   thorpej sunxi_rtc_clk_enable(void *priv, struct clk *clk)
    545   1.6   thorpej {
    546   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    547   1.6   thorpej 
    548   1.6   thorpej 	if (clk != &sc->sc_clks[SUNXI_RTC_CLK_LOSC_GATE])
    549   1.6   thorpej 		return 0;
    550   1.6   thorpej 
    551   1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    552   1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_RTC_LOSC_OUT_GATING_REG);
    553   1.6   thorpej 	if ((reg & SUN6I_RTC_LOSC_OUT_EN) == 0) {
    554   1.6   thorpej 		reg |= SUN6I_RTC_LOSC_OUT_EN;
    555   1.6   thorpej 		RTC_WRITE(sc, SUN6I_RTC_LOSC_OUT_GATING_REG, reg);
    556   1.6   thorpej 	}
    557   1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    558   1.6   thorpej 
    559   1.6   thorpej 	return 0;
    560   1.6   thorpej }
    561   1.6   thorpej 
    562   1.6   thorpej static int
    563   1.6   thorpej sunxi_rtc_clk_disable(void *priv, struct clk *clk)
    564   1.6   thorpej {
    565   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    566   1.6   thorpej 
    567   1.6   thorpej 	if (clk != &sc->sc_clks[SUNXI_RTC_CLK_LOSC_GATE])
    568   1.6   thorpej 		return EINVAL;
    569   1.6   thorpej 
    570   1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    571   1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_RTC_LOSC_OUT_GATING_REG);
    572   1.6   thorpej 	if (reg & SUN6I_RTC_LOSC_OUT_EN) {
    573   1.6   thorpej 		reg &= ~SUN6I_RTC_LOSC_OUT_EN;
    574   1.6   thorpej 		RTC_WRITE(sc, SUN6I_RTC_LOSC_OUT_GATING_REG, reg);
    575   1.6   thorpej 	}
    576   1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    577   1.6   thorpej 
    578   1.6   thorpej 	return 0;
    579   1.6   thorpej }
    580   1.6   thorpej 
    581   1.6   thorpej static int
    582   1.6   thorpej sunxi_rtc_clk_set_parent(void *priv, struct clk *clk, struct clk *parent_clk)
    583   1.6   thorpej {
    584   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    585   1.6   thorpej 
    586   1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    587   1.6   thorpej 		return EINVAL;
    588   1.6   thorpej 
    589   1.6   thorpej 	if (parent_clk != sc->sc_parent_clk &&
    590   1.6   thorpej 	    parent_clk != &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    591   1.6   thorpej 		return EINVAL;
    592   1.6   thorpej 
    593   1.6   thorpej 	mutex_enter(&sc->sc_clk_mutex);
    594   1.6   thorpej 	uint32_t reg = RTC_READ(sc, SUN6I_LOSC_CTRL_REG);
    595   1.6   thorpej 	if (parent_clk == sc->sc_parent_clk)
    596   1.6   thorpej 		reg |= SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en;
    597   1.6   thorpej 	else
    598   1.6   thorpej 		reg &= ~(SUN6I_LOSC_CTRL_EXT_OSC | sc->sc_conf->ext_losc_en);
    599   1.6   thorpej 	RTC_WRITE(sc, SUN6I_LOSC_CTRL_REG, reg | SUN6I_LOSC_CTRL_KEY);
    600   1.6   thorpej 	mutex_exit(&sc->sc_clk_mutex);
    601   1.6   thorpej 
    602   1.6   thorpej 	return 0;
    603   1.6   thorpej }
    604   1.6   thorpej 
    605   1.6   thorpej static struct clk *
    606   1.6   thorpej sunxi_rtc_clk_get_parent(void *priv, struct clk *clk)
    607   1.6   thorpej {
    608   1.6   thorpej 	struct sunxi_rtc_softc * const sc = priv;
    609   1.6   thorpej 	uint32_t reg;
    610   1.6   thorpej 
    611   1.6   thorpej 	if (clk == &sc->sc_clks[SUNXI_RTC_CLK_IOSC])
    612   1.6   thorpej 		return NULL;
    613   1.6   thorpej 
    614   1.6   thorpej 	reg = RTC_READ(sc, SUN6I_LOSC_CTRL_REG);
    615   1.6   thorpej 	if (reg & SUN6I_LOSC_CTRL_EXT_OSC)
    616   1.6   thorpej 		return sc->sc_parent_clk;
    617   1.6   thorpej 
    618   1.6   thorpej 	/*
    619   1.6   thorpej 	 * We switch to the external oscillator at attach time becacuse
    620   1.6   thorpej 	 * it's higher quality than the internal one.  If we haven't
    621   1.6   thorpej 	 * exported the internal oscillator to the clock tree, then
    622   1.6   thorpej 	 * we shouldn't get here.
    623   1.6   thorpej 	 */
    624   1.6   thorpej 	KASSERT(sc->sc_clk_names[SUNXI_RTC_CLK_IOSC] != NULL);
    625   1.6   thorpej 	return &sc->sc_clks[SUNXI_RTC_CLK_IOSC];
    626   1.6   thorpej }
    627