Home | History | Annotate | Line # | Download | only in starfive
jh71x0_clkc.h revision 1.1
      1  1.1  skrll /* $NetBSD: jh71x0_clkc.h,v 1.1 2024/07/27 07:09:50 skrll Exp $ */
      2  1.1  skrll 
      3  1.1  skrll /*-
      4  1.1  skrll  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  skrll  * All rights reserved.
      6  1.1  skrll  *
      7  1.1  skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  skrll  * by Nick Hudson
      9  1.1  skrll  *
     10  1.1  skrll  * Redistribution and use in source and binary forms, with or without
     11  1.1  skrll  * modification, are permitted provided that the following conditions
     12  1.1  skrll  * are met:
     13  1.1  skrll  * 1. Redistributions of source code must retain the above copyright
     14  1.1  skrll  *    notice, this list of conditions and the following disclaimer.
     15  1.1  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  skrll  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  skrll  *    documentation and/or other materials provided with the distribution.
     18  1.1  skrll  *
     19  1.1  skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  skrll  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  skrll  */
     31  1.1  skrll 
     32  1.1  skrll #ifndef _STARFIVE_JH71X0CLKC_H
     33  1.1  skrll #define _STARFIVE_JH71X0CLKC_H
     34  1.1  skrll 
     35  1.1  skrll #include <dev/clk/clk_backend.h>
     36  1.1  skrll #include <dev/fdt/syscon.h>
     37  1.1  skrll 
     38  1.1  skrll /*
     39  1.1  skrll  * Each clock has a 32-bit register indexed from the register base with
     40  1.1  skrll  * the following bit field definitions depending on type.
     41  1.1  skrll  */
     42  1.1  skrll 
     43  1.1  skrll /* register fields */
     44  1.1  skrll #define JH71X0_CLK_ENABLE	__BIT(31)
     45  1.1  skrll #define JH71X0_CLK_INVERT	__BIT(30)
     46  1.1  skrll #define JH71X0_CLK_MUX_MASK	__BITS(27, 24)
     47  1.1  skrll #define JH71X0_CLK_DIV_MASK	__BITS(23, 0)
     48  1.1  skrll #define JH71X0_CLK_FRAC_MASK	__BITS(15, 8)
     49  1.1  skrll #define JH71X0_CLK_INT_MASK	__BITS(7, 0)
     50  1.1  skrll 
     51  1.1  skrll /* fractional divider min/max */
     52  1.1  skrll #define JH71X0_CLK_FRAC_MIN	100UL
     53  1.1  skrll #define JH71X0_CLK_FRAC_MAX	(26600UL - 1)
     54  1.1  skrll 
     55  1.1  skrll 
     56  1.1  skrll struct jh71x0_clkc_clk;
     57  1.1  skrll 
     58  1.1  skrll struct jh71x0_clkc_softc {
     59  1.1  skrll 	device_t		sc_dev;
     60  1.1  skrll 	bus_space_tag_t		sc_bst;
     61  1.1  skrll 	bus_space_handle_t	sc_bsh;
     62  1.1  skrll 	int			sc_phandle;
     63  1.1  skrll 	struct clk_domain	sc_clkdom;
     64  1.1  skrll 	struct jh71x0_clkc_clk *sc_clk;
     65  1.1  skrll 	size_t			sc_nclks;
     66  1.1  skrll };
     67  1.1  skrll 
     68  1.1  skrll enum jh71x0_clkc_clktype {
     69  1.1  skrll 	JH71X0CLK_UNKNOWN,
     70  1.1  skrll 	JH71X0CLK_FIXED_FACTOR,
     71  1.1  skrll 	JH71X0CLK_GATE,
     72  1.1  skrll 	JH71X0CLK_DIV,
     73  1.1  skrll 	JH71X0CLK_FRACDIV,
     74  1.1  skrll 	JH71X0CLK_MUX,
     75  1.1  skrll 	JH71X0CLK_INV,
     76  1.1  skrll };
     77  1.1  skrll 
     78  1.1  skrll /*
     79  1.1  skrll  * Fixed-factor clocks
     80  1.1  skrll  */
     81  1.1  skrll 
     82  1.1  skrll struct jh71x0_clkc_fixed_factor {
     83  1.1  skrll 	const char *	jcff_parent;
     84  1.1  skrll 	u_int		jcff_div;
     85  1.1  skrll 	u_int		jcff_mult;
     86  1.1  skrll };
     87  1.1  skrll 
     88  1.1  skrll u_int	jh71x0_clkc_fixed_factor_get_rate(struct jh71x0_clkc_softc *,
     89  1.1  skrll 	    struct jh71x0_clkc_clk *);
     90  1.1  skrll int	jh71x0_clkc_fixed_factor_set_rate(struct jh71x0_clkc_softc *,
     91  1.1  skrll 	    struct jh71x0_clkc_clk *, u_int);
     92  1.1  skrll const char *
     93  1.1  skrll 	jh71x0_clkc_fixed_factor_get_parent(struct jh71x0_clkc_softc *,
     94  1.1  skrll 	    struct jh71x0_clkc_clk *);
     95  1.1  skrll 
     96  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_ffactor_ops;
     97  1.1  skrll 
     98  1.1  skrll #define	JH71X0CLKC_FIXED_FACTOR(_id, _name, _parent, _div, _mult)	      \
     99  1.1  skrll 	[_id] = {							      \
    100  1.1  skrll 		.jcc_type = JH71X0CLK_FIXED_FACTOR,			      \
    101  1.1  skrll 		.jcc_clk.name = (_name),				      \
    102  1.1  skrll 		.jcc_ffactor.jcff_parent = (_parent),			      \
    103  1.1  skrll 		.jcc_ffactor.jcff_div = (_div),				      \
    104  1.1  skrll 		.jcc_ffactor.jcff_mult = (_mult),			      \
    105  1.1  skrll 		.jcc_ops = &jh71x0_clkc_ffactor_ops,			      \
    106  1.1  skrll 	}
    107  1.1  skrll 
    108  1.1  skrll /*
    109  1.1  skrll  * Gate clocks
    110  1.1  skrll  */
    111  1.1  skrll 
    112  1.1  skrll struct jh71x0_clkc_gate {
    113  1.1  skrll 	const char	*jcg_parent;
    114  1.1  skrll };
    115  1.1  skrll 
    116  1.1  skrll int	jh71x0_clkc_gate_enable(struct jh71x0_clkc_softc *,
    117  1.1  skrll 	    struct jh71x0_clkc_clk *, int);
    118  1.1  skrll const char *
    119  1.1  skrll 	jh71x0_clkc_gate_get_parent(struct jh71x0_clkc_softc *,
    120  1.1  skrll 	    struct jh71x0_clkc_clk *);
    121  1.1  skrll 
    122  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_gate_ops;
    123  1.1  skrll 
    124  1.1  skrll #define	JH71X0CLKC_GATE(_id, _name, _pname)				      \
    125  1.1  skrll 	[_id] = {							      \
    126  1.1  skrll 		.jcc_type = JH71X0CLK_GATE,				      \
    127  1.1  skrll 		.jcc_clk = {						      \
    128  1.1  skrll 			.name = (_name),				      \
    129  1.1  skrll 			.flags = CLK_SET_RATE_PARENT,			      \
    130  1.1  skrll 		},							      \
    131  1.1  skrll 		.jcc_reg = (_id) * sizeof(uint32_t),			      \
    132  1.1  skrll 		.jcc_gate.jcg_parent = (_pname),			      \
    133  1.1  skrll 		.jcc_ops = &jh71x0_clkc_gate_ops,			      \
    134  1.1  skrll 	}
    135  1.1  skrll 
    136  1.1  skrll /*
    137  1.1  skrll  * Divider clocks
    138  1.1  skrll  */
    139  1.1  skrll 
    140  1.1  skrll struct jh71x0_clkc_div {
    141  1.1  skrll 	bus_size_t	jcd_reg;
    142  1.1  skrll 	const char *	jcd_parent;
    143  1.1  skrll 	uint32_t	jcd_maxdiv;
    144  1.1  skrll 	uint32_t	jcd_flags;
    145  1.1  skrll #define	JH71X0CLKC_DIV_GATE	__BIT(0)
    146  1.1  skrll };
    147  1.1  skrll 
    148  1.1  skrll u_int	jh71x0_clkc_div_get_rate(struct jh71x0_clkc_softc *,
    149  1.1  skrll 	    struct jh71x0_clkc_clk *);
    150  1.1  skrll int	jh71x0_clkc_div_set_rate(struct jh71x0_clkc_softc *,
    151  1.1  skrll 	    struct jh71x0_clkc_clk *, u_int);
    152  1.1  skrll const char *
    153  1.1  skrll 	jh71x0_clkc_div_get_parent(struct jh71x0_clkc_softc *,
    154  1.1  skrll 	    struct jh71x0_clkc_clk *);
    155  1.1  skrll 
    156  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_div_ops;
    157  1.1  skrll 
    158  1.1  skrll #define	JH71X0CLKC_DIV_FLAGS(_id, _name, _maxdiv, _parent, _flags)	      \
    159  1.1  skrll 	[_id] = {							      \
    160  1.1  skrll 		.jcc_type = JH71X0CLK_DIV,				      \
    161  1.1  skrll 		.jcc_clk = {						      \
    162  1.1  skrll 			.name = (_name),				      \
    163  1.1  skrll 		},							      \
    164  1.1  skrll 		.jcc_reg = (_id) * sizeof(uint32_t),			      \
    165  1.1  skrll 		.jcc_div = {						      \
    166  1.1  skrll 			.jcd_parent = (_parent),			      \
    167  1.1  skrll 			.jcd_maxdiv = (_maxdiv),			      \
    168  1.1  skrll 			.jcd_flags = (_flags),				      \
    169  1.1  skrll 		},							      \
    170  1.1  skrll 		.jcc_ops = &jh71x0_clkc_div_ops,			      \
    171  1.1  skrll 	}
    172  1.1  skrll 
    173  1.1  skrll #define	JH71X0CLKC_DIV(_id, _n, _m, _p)		  			      \
    174  1.1  skrll     JH71X0CLKC_DIV_FLAGS((_id), (_n), (_m), (_p), 0)
    175  1.1  skrll 
    176  1.1  skrll #define	JH71X0CLKC_GATEDIV(_id, _n, _m, _p)				      \
    177  1.1  skrll     JH71X0CLKC_DIV_FLAGS((_id), (_n), (_m), (_p), JH71X0CLKC_DIV_GATE)
    178  1.1  skrll 
    179  1.1  skrll /*
    180  1.1  skrll  * Fractional Divider clocks
    181  1.1  skrll  */
    182  1.1  skrll 
    183  1.1  skrll struct jh71x0_clkc_fracdiv {
    184  1.1  skrll 	bus_size_t	jcd_reg;
    185  1.1  skrll 	const char *	jcd_parent;
    186  1.1  skrll 	uint32_t	jcd_flags;
    187  1.1  skrll #define	JH71X0CLKC_DIV_GATE	__BIT(0)
    188  1.1  skrll };
    189  1.1  skrll 
    190  1.1  skrll u_int	jh71x0_clkc_fracdiv_get_rate(struct jh71x0_clkc_softc *,
    191  1.1  skrll 	    struct jh71x0_clkc_clk *);
    192  1.1  skrll int	jh71x0_clkc_fracdiv_set_rate(struct jh71x0_clkc_softc *,
    193  1.1  skrll 	    struct jh71x0_clkc_clk *, u_int);
    194  1.1  skrll const char *
    195  1.1  skrll 	jh71x0_clkc_fracdiv_get_parent(struct jh71x0_clkc_softc *,
    196  1.1  skrll 	    struct jh71x0_clkc_clk *);
    197  1.1  skrll 
    198  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_fracdiv_ops;
    199  1.1  skrll 
    200  1.1  skrll #define	JH71X0CLKC_FRACDIV(_id, _name, _parent)				      \
    201  1.1  skrll 	[_id] = {							      \
    202  1.1  skrll 		.jcc_type = JH71X0CLK_FRACDIV,				      \
    203  1.1  skrll 		.jcc_clk = {						      \
    204  1.1  skrll 			.name = (_name),				      \
    205  1.1  skrll 		},							      \
    206  1.1  skrll 		.jcc_reg = (_id) * sizeof(uint32_t),			      \
    207  1.1  skrll 		.jcc_fracdiv = {					      \
    208  1.1  skrll 			.jcd_parent = (_parent),			      \
    209  1.1  skrll 		},							      \
    210  1.1  skrll 		.jcc_ops = &jh71x0_clkc_fracdiv_ops,			      \
    211  1.1  skrll 	}
    212  1.1  skrll 
    213  1.1  skrll 
    214  1.1  skrll /*
    215  1.1  skrll  * Mux clocks
    216  1.1  skrll  */
    217  1.1  skrll 
    218  1.1  skrll struct jh71x0_clkc_mux {
    219  1.1  skrll 	size_t		jcm_nparents;
    220  1.1  skrll 	const char **	jcm_parents;
    221  1.1  skrll };
    222  1.1  skrll 
    223  1.1  skrll int	jh71x0_clkc_mux_set_parent(struct jh71x0_clkc_softc *,
    224  1.1  skrll 	    struct jh71x0_clkc_clk *, const char *);
    225  1.1  skrll const char *
    226  1.1  skrll 	jh71x0_clkc_mux_get_parent(struct jh71x0_clkc_softc *,
    227  1.1  skrll 	    struct jh71x0_clkc_clk *);
    228  1.1  skrll 
    229  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_mux_ops;
    230  1.1  skrll 
    231  1.1  skrll #define	JH71X0CLKC_MUX_FLAGS(_id, _name, _parents, _cflags)		      \
    232  1.1  skrll 	[_id] = {							      \
    233  1.1  skrll 		.jcc_type = JH71X0CLK_MUX,				      \
    234  1.1  skrll 		.jcc_clk = {						      \
    235  1.1  skrll 			.name = (_name),				      \
    236  1.1  skrll 			.flags = (_cflags),				      \
    237  1.1  skrll 		},							      \
    238  1.1  skrll 		.jcc_reg = (_id) * sizeof(uint32_t),			      \
    239  1.1  skrll 		.jcc_mux = {						      \
    240  1.1  skrll 			.jcm_parents = (_parents),			      \
    241  1.1  skrll 			.jcm_nparents = __arraycount(_parents),		      \
    242  1.1  skrll 		},							      \
    243  1.1  skrll 		.jcc_ops = &jh71x0_clkc_mux_ops,			      \
    244  1.1  skrll 	}
    245  1.1  skrll 
    246  1.1  skrll #define	JH71X0CLKC_MUX(_id, _n, _p)		  			      \
    247  1.1  skrll     JH71X0CLKC_MUX_FLAGS((_id), (_n), (_p), 0)
    248  1.1  skrll 
    249  1.1  skrll struct jh71x0_clkc_inv {
    250  1.1  skrll 	const char *	jci_parent;
    251  1.1  skrll };
    252  1.1  skrll 
    253  1.1  skrll const char *
    254  1.1  skrll jh71x0_clkc_inv_get_parent(struct jh71x0_clkc_softc *sc,
    255  1.1  skrll     struct jh71x0_clkc_clk *jcc);
    256  1.1  skrll 
    257  1.1  skrll extern struct jh71x0_clkc_clkops jh71x0_clkc_inv_ops;
    258  1.1  skrll 
    259  1.1  skrll #define	JH71X0CLKC_INV(_id, _name, _pname)				      \
    260  1.1  skrll 	[_id] = {							      \
    261  1.1  skrll 		.jcc_type = JH71X0CLK_INV,				      \
    262  1.1  skrll 		.jcc_clk = {						      \
    263  1.1  skrll 			.name = (_name),				      \
    264  1.1  skrll 			.flags = CLK_SET_RATE_PARENT,			      \
    265  1.1  skrll 		},							      \
    266  1.1  skrll 		.jcc_reg = (_id) * sizeof(uint32_t),			      \
    267  1.1  skrll 		.jcc_inv.jci_parent = (_pname),				      \
    268  1.1  skrll 		.jcc_ops = &jh71x0_clkc_inv_ops,			      \
    269  1.1  skrll 	}
    270  1.1  skrll 
    271  1.1  skrll 
    272  1.1  skrll struct jh71x0_clkc_clkops {
    273  1.1  skrll 
    274  1.1  skrll 	int		(*jcco_enable)(struct jh71x0_clkc_softc *,
    275  1.1  skrll 			    struct jh71x0_clkc_clk *, int);
    276  1.1  skrll 	u_int		(*jcco_getrate)(struct jh71x0_clkc_softc *,
    277  1.1  skrll 			    struct jh71x0_clkc_clk *);
    278  1.1  skrll 	int		(*jcco_setrate)(struct jh71x0_clkc_softc *,
    279  1.1  skrll 			    struct jh71x0_clkc_clk *, u_int);
    280  1.1  skrll 	const char *    (*jcco_getparent)(struct jh71x0_clkc_softc *,
    281  1.1  skrll 			    struct jh71x0_clkc_clk *);
    282  1.1  skrll 	int		(*jcco_setparent)(struct jh71x0_clkc_softc *,
    283  1.1  skrll 			    struct jh71x0_clkc_clk *, const char *);
    284  1.1  skrll };
    285  1.1  skrll 
    286  1.1  skrll 
    287  1.1  skrll struct jh71x0_clkc_clk {
    288  1.1  skrll 	struct clk		 		jcc_clk;
    289  1.1  skrll 	enum jh71x0_clkc_clktype 		jcc_type;
    290  1.1  skrll 	bus_size_t				jcc_reg;
    291  1.1  skrll 	union {
    292  1.1  skrll 		struct jh71x0_clkc_gate		jcc_gate;
    293  1.1  skrll 		struct jh71x0_clkc_div		jcc_div;
    294  1.1  skrll 		struct jh71x0_clkc_fracdiv	jcc_fracdiv;
    295  1.1  skrll 		struct jh71x0_clkc_fixed_factor jcc_ffactor;
    296  1.1  skrll 		struct jh71x0_clkc_mux		jcc_mux;
    297  1.1  skrll 		struct jh71x0_clkc_inv		jcc_inv;
    298  1.1  skrll 	};
    299  1.1  skrll 	struct jh71x0_clkc_clkops *		jcc_ops;
    300  1.1  skrll };
    301  1.1  skrll 
    302  1.1  skrll extern const struct clk_funcs jh71x0_clkc_funcs;
    303  1.1  skrll 
    304  1.1  skrll #endif
    305