Home | History | Annotate | Line # | Download | only in sunxi
sunxi_ccu.h revision 1.3
      1  1.3  jmcneill /* $NetBSD: sunxi_ccu.h,v 1.3 2017/06/29 10:53:59 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 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 #ifndef _ARM_SUNXI_CCU_H
     30  1.1  jmcneill #define _ARM_SUNXI_CCU_H
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <dev/clk/clk_backend.h>
     33  1.1  jmcneill 
     34  1.1  jmcneill struct sunxi_ccu_softc;
     35  1.1  jmcneill struct sunxi_ccu_clk;
     36  1.1  jmcneill struct sunxi_ccu_reset;
     37  1.1  jmcneill 
     38  1.1  jmcneill /*
     39  1.1  jmcneill  * Resets
     40  1.1  jmcneill  */
     41  1.1  jmcneill 
     42  1.1  jmcneill struct sunxi_ccu_reset {
     43  1.1  jmcneill 	bus_size_t	reg;
     44  1.1  jmcneill 	uint32_t	mask;
     45  1.1  jmcneill };
     46  1.1  jmcneill 
     47  1.1  jmcneill #define	SUNXI_CCU_RESET(_id, _reg, _bit)	\
     48  1.1  jmcneill 	[_id] = {				\
     49  1.1  jmcneill 		.reg = (_reg),			\
     50  1.1  jmcneill 		.mask = __BIT(_bit),		\
     51  1.1  jmcneill 	}
     52  1.1  jmcneill 
     53  1.1  jmcneill /*
     54  1.1  jmcneill  * Clocks
     55  1.1  jmcneill  */
     56  1.1  jmcneill 
     57  1.1  jmcneill enum sunxi_ccu_clktype {
     58  1.1  jmcneill 	SUNXI_CCU_UNKNOWN,
     59  1.1  jmcneill 	SUNXI_CCU_GATE,
     60  1.1  jmcneill 	SUNXI_CCU_NM,
     61  1.2  jmcneill 	SUNXI_CCU_NKMP,
     62  1.2  jmcneill 	SUNXI_CCU_PREDIV,
     63  1.1  jmcneill };
     64  1.1  jmcneill 
     65  1.1  jmcneill struct sunxi_ccu_gate {
     66  1.1  jmcneill 	bus_size_t	reg;
     67  1.1  jmcneill 	uint32_t	mask;
     68  1.1  jmcneill 	const char	*parent;
     69  1.1  jmcneill };
     70  1.1  jmcneill 
     71  1.1  jmcneill int	sunxi_ccu_gate_enable(struct sunxi_ccu_softc *,
     72  1.1  jmcneill 			      struct sunxi_ccu_clk *, int);
     73  1.1  jmcneill const char *sunxi_ccu_gate_get_parent(struct sunxi_ccu_softc *,
     74  1.1  jmcneill 				      struct sunxi_ccu_clk *);
     75  1.1  jmcneill 
     76  1.1  jmcneill #define	SUNXI_CCU_GATE(_id, _name, _pname, _reg, _bit)		\
     77  1.1  jmcneill 	[_id] = {						\
     78  1.1  jmcneill 		.type = SUNXI_CCU_GATE,				\
     79  1.1  jmcneill 		.base.name = (_name),				\
     80  1.1  jmcneill 		.u.gate.parent = (_pname),			\
     81  1.1  jmcneill 		.u.gate.reg = (_reg),				\
     82  1.1  jmcneill 		.u.gate.mask = __BIT(_bit),			\
     83  1.1  jmcneill 		.enable = sunxi_ccu_gate_enable,		\
     84  1.1  jmcneill 		.get_parent = sunxi_ccu_gate_get_parent,	\
     85  1.1  jmcneill 	}
     86  1.1  jmcneill 
     87  1.2  jmcneill struct sunxi_ccu_nkmp {
     88  1.2  jmcneill 	bus_size_t	reg;
     89  1.2  jmcneill 	const char	*parent;
     90  1.2  jmcneill 	uint32_t	n;
     91  1.2  jmcneill 	uint32_t	k;
     92  1.2  jmcneill 	uint32_t	m;
     93  1.2  jmcneill 	uint32_t	p;
     94  1.2  jmcneill 	uint32_t	lock;
     95  1.2  jmcneill 	uint32_t	enable;
     96  1.2  jmcneill 	uint32_t	flags;
     97  1.3  jmcneill #define	SUNXI_CCU_NKMP_DIVIDE_BY_TWO	__BIT(0)
     98  1.2  jmcneill };
     99  1.2  jmcneill 
    100  1.2  jmcneill int	sunxi_ccu_nkmp_enable(struct sunxi_ccu_softc *,
    101  1.2  jmcneill 			      struct sunxi_ccu_clk *, int);
    102  1.2  jmcneill u_int	sunxi_ccu_nkmp_get_rate(struct sunxi_ccu_softc *,
    103  1.2  jmcneill 				struct sunxi_ccu_clk *);
    104  1.2  jmcneill int	sunxi_ccu_nkmp_set_rate(struct sunxi_ccu_softc *,
    105  1.2  jmcneill 				struct sunxi_ccu_clk *, u_int);
    106  1.2  jmcneill const char *sunxi_ccu_nkmp_get_parent(struct sunxi_ccu_softc *,
    107  1.2  jmcneill 				      struct sunxi_ccu_clk *);
    108  1.2  jmcneill 
    109  1.2  jmcneill #define	SUNXI_CCU_NKMP(_id, _name, _parent, _reg, _n, _k, _m,	\
    110  1.2  jmcneill 		       _p, _enable, _flags)			\
    111  1.2  jmcneill 	[_id] = {						\
    112  1.2  jmcneill 		.type = SUNXI_CCU_NKMP,				\
    113  1.2  jmcneill 		.base.name = (_name),				\
    114  1.2  jmcneill 		.u.nkmp.reg = (_reg),				\
    115  1.2  jmcneill 		.u.nkmp.parent = (_parent),			\
    116  1.2  jmcneill 		.u.nkmp.n = (_n),				\
    117  1.2  jmcneill 		.u.nkmp.k = (_k),				\
    118  1.2  jmcneill 		.u.nkmp.m = (_m),				\
    119  1.2  jmcneill 		.u.nkmp.p = (_p),				\
    120  1.2  jmcneill 		.u.nkmp.enable = (_enable),			\
    121  1.2  jmcneill 		.u.nkmp.flags = (_flags),			\
    122  1.2  jmcneill 		.enable = sunxi_ccu_nkmp_enable,		\
    123  1.2  jmcneill 		.get_rate = sunxi_ccu_nkmp_get_rate,		\
    124  1.2  jmcneill 		.set_rate = sunxi_ccu_nkmp_set_rate,		\
    125  1.2  jmcneill 		.get_parent = sunxi_ccu_nkmp_get_parent,	\
    126  1.2  jmcneill 	}
    127  1.2  jmcneill 
    128  1.1  jmcneill struct sunxi_ccu_nm {
    129  1.1  jmcneill 	bus_size_t	reg;
    130  1.1  jmcneill 	const char	**parents;
    131  1.1  jmcneill 	u_int		nparents;
    132  1.1  jmcneill 	uint32_t	n;
    133  1.1  jmcneill 	uint32_t	m;
    134  1.1  jmcneill 	uint32_t	sel;
    135  1.2  jmcneill 	uint32_t	enable;
    136  1.1  jmcneill 	uint32_t	flags;
    137  1.1  jmcneill #define	SUNXI_CCU_NM_POWER_OF_TWO	__BIT(0)
    138  1.2  jmcneill #define	SUNXI_CCU_NM_ROUND_DOWN		__BIT(1)
    139  1.1  jmcneill };
    140  1.1  jmcneill 
    141  1.2  jmcneill int	sunxi_ccu_nm_enable(struct sunxi_ccu_softc *,
    142  1.2  jmcneill 			    struct sunxi_ccu_clk *, int);
    143  1.1  jmcneill u_int	sunxi_ccu_nm_get_rate(struct sunxi_ccu_softc *,
    144  1.1  jmcneill 			      struct sunxi_ccu_clk *);
    145  1.1  jmcneill int	sunxi_ccu_nm_set_rate(struct sunxi_ccu_softc *,
    146  1.1  jmcneill 			      struct sunxi_ccu_clk *, u_int);
    147  1.1  jmcneill int	sunxi_ccu_nm_set_parent(struct sunxi_ccu_softc *,
    148  1.1  jmcneill 				struct sunxi_ccu_clk *,
    149  1.1  jmcneill 				const char *);
    150  1.1  jmcneill const char *sunxi_ccu_nm_get_parent(struct sunxi_ccu_softc *,
    151  1.1  jmcneill 				    struct sunxi_ccu_clk *);
    152  1.1  jmcneill 
    153  1.1  jmcneill #define	SUNXI_CCU_NM(_id, _name, _parents, _reg, _n, _m, _sel,	\
    154  1.2  jmcneill 		     _enable, _flags)				\
    155  1.1  jmcneill 	[_id] = {						\
    156  1.1  jmcneill 		.type = SUNXI_CCU_NM,				\
    157  1.1  jmcneill 		.base.name = (_name),				\
    158  1.1  jmcneill 		.u.nm.reg = (_reg),				\
    159  1.1  jmcneill 		.u.nm.parents = (_parents),			\
    160  1.1  jmcneill 		.u.nm.nparents = __arraycount(_parents),	\
    161  1.1  jmcneill 		.u.nm.n = (_n),					\
    162  1.1  jmcneill 		.u.nm.m = (_m),					\
    163  1.1  jmcneill 		.u.nm.sel = (_sel),				\
    164  1.2  jmcneill 		.u.nm.enable = (_enable),			\
    165  1.1  jmcneill 		.u.nm.flags = (_flags),				\
    166  1.2  jmcneill 		.enable = sunxi_ccu_nm_enable,			\
    167  1.2  jmcneill 		.get_rate = sunxi_ccu_nm_get_rate,		\
    168  1.2  jmcneill 		.set_rate = sunxi_ccu_nm_set_rate,		\
    169  1.1  jmcneill 		.set_parent = sunxi_ccu_nm_set_parent,		\
    170  1.1  jmcneill 		.get_parent = sunxi_ccu_nm_get_parent,		\
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 
    173  1.2  jmcneill struct sunxi_ccu_prediv {
    174  1.2  jmcneill 	bus_size_t	reg;
    175  1.2  jmcneill 	const char	**parents;
    176  1.2  jmcneill 	u_int		nparents;
    177  1.2  jmcneill 	uint32_t	prediv;
    178  1.2  jmcneill 	uint32_t	prediv_sel;
    179  1.2  jmcneill 	uint32_t	div;
    180  1.2  jmcneill 	uint32_t	sel;
    181  1.2  jmcneill 	uint32_t	flags;
    182  1.2  jmcneill #define	SUNXI_CCU_PREDIV_POWER_OF_TWO	__BIT(0)
    183  1.2  jmcneill };
    184  1.2  jmcneill 
    185  1.2  jmcneill u_int	sunxi_ccu_prediv_get_rate(struct sunxi_ccu_softc *,
    186  1.2  jmcneill 				  struct sunxi_ccu_clk *);
    187  1.2  jmcneill int	sunxi_ccu_prediv_set_rate(struct sunxi_ccu_softc *,
    188  1.2  jmcneill 				  struct sunxi_ccu_clk *, u_int);
    189  1.2  jmcneill int	sunxi_ccu_prediv_set_parent(struct sunxi_ccu_softc *,
    190  1.2  jmcneill 				    struct sunxi_ccu_clk *,
    191  1.2  jmcneill 				    const char *);
    192  1.2  jmcneill const char *sunxi_ccu_prediv_get_parent(struct sunxi_ccu_softc *,
    193  1.2  jmcneill 					struct sunxi_ccu_clk *);
    194  1.2  jmcneill 
    195  1.2  jmcneill #define	SUNXI_CCU_PREDIV(_id, _name, _parents, _reg, _prediv,	\
    196  1.2  jmcneill 		     _prediv_sel, _div, _sel, _flags)		\
    197  1.2  jmcneill 	[_id] = {						\
    198  1.2  jmcneill 		.type = SUNXI_CCU_PREDIV,			\
    199  1.2  jmcneill 		.base.name = (_name),				\
    200  1.2  jmcneill 		.u.prediv.reg = (_reg),				\
    201  1.2  jmcneill 		.u.prediv.parents = (_parents),			\
    202  1.2  jmcneill 		.u.prediv.nparents = __arraycount(_parents),	\
    203  1.2  jmcneill 		.u.prediv.prediv = (_prediv),			\
    204  1.2  jmcneill 		.u.prediv.prediv_sel = (_prediv_sel),		\
    205  1.2  jmcneill 		.u.prediv.div = (_div),				\
    206  1.2  jmcneill 		.u.prediv.sel = (_sel),				\
    207  1.2  jmcneill 		.u.prediv.flags = (_flags),			\
    208  1.2  jmcneill 		.get_rate = sunxi_ccu_prediv_get_rate,		\
    209  1.2  jmcneill 		.set_rate = sunxi_ccu_prediv_set_rate,		\
    210  1.2  jmcneill 		.set_parent = sunxi_ccu_prediv_set_parent,	\
    211  1.2  jmcneill 		.get_parent = sunxi_ccu_prediv_get_parent,	\
    212  1.2  jmcneill 	}
    213  1.2  jmcneill 
    214  1.1  jmcneill struct sunxi_ccu_clk {
    215  1.1  jmcneill 	struct clk	base;
    216  1.1  jmcneill 	enum sunxi_ccu_clktype type;
    217  1.1  jmcneill 	union {
    218  1.1  jmcneill 		struct sunxi_ccu_gate gate;
    219  1.1  jmcneill 		struct sunxi_ccu_nm nm;
    220  1.2  jmcneill 		struct sunxi_ccu_nkmp nkmp;
    221  1.2  jmcneill 		struct sunxi_ccu_prediv prediv;
    222  1.1  jmcneill 	} u;
    223  1.1  jmcneill 
    224  1.1  jmcneill 	int		(*enable)(struct sunxi_ccu_softc *,
    225  1.1  jmcneill 				  struct sunxi_ccu_clk *, int);
    226  1.1  jmcneill 	u_int		(*get_rate)(struct sunxi_ccu_softc *,
    227  1.1  jmcneill 				    struct sunxi_ccu_clk *);
    228  1.1  jmcneill 	int		(*set_rate)(struct sunxi_ccu_softc *,
    229  1.1  jmcneill 				    struct sunxi_ccu_clk *, u_int);
    230  1.1  jmcneill 	const char *	(*get_parent)(struct sunxi_ccu_softc *,
    231  1.1  jmcneill 				      struct sunxi_ccu_clk *);
    232  1.1  jmcneill 	int		(*set_parent)(struct sunxi_ccu_softc *,
    233  1.1  jmcneill 				      struct sunxi_ccu_clk *,
    234  1.1  jmcneill 				      const char *);
    235  1.1  jmcneill };
    236  1.1  jmcneill 
    237  1.1  jmcneill struct sunxi_ccu_softc {
    238  1.1  jmcneill 	device_t		sc_dev;
    239  1.1  jmcneill 	int			sc_phandle;
    240  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    241  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    242  1.1  jmcneill 
    243  1.1  jmcneill 	struct clk_domain	sc_clkdom;
    244  1.1  jmcneill 
    245  1.1  jmcneill 	struct sunxi_ccu_reset *sc_resets;
    246  1.1  jmcneill 	u_int			sc_nresets;
    247  1.1  jmcneill 
    248  1.1  jmcneill 	struct sunxi_ccu_clk	*sc_clks;
    249  1.1  jmcneill 	u_int			sc_nclks;
    250  1.1  jmcneill };
    251  1.1  jmcneill 
    252  1.1  jmcneill int	sunxi_ccu_attach(struct sunxi_ccu_softc *);
    253  1.1  jmcneill struct sunxi_ccu_clk *sunxi_ccu_clock_find(struct sunxi_ccu_softc *,
    254  1.1  jmcneill 					   const char *);
    255  1.1  jmcneill void	sunxi_ccu_print(struct sunxi_ccu_softc *);
    256  1.1  jmcneill 
    257  1.1  jmcneill #define CCU_READ(sc, reg)	\
    258  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    259  1.1  jmcneill #define CCU_WRITE(sc, reg, val)	\
    260  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    261  1.1  jmcneill 
    262  1.1  jmcneill #endif /* _ARM_SUNXI_CCU_H */
    263