Home | History | Annotate | Line # | Download | only in ti
      1  1.4  jmcneill /* $NetBSD: ti_prcm.h,v 1.4 2019/10/30 21:41:40 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_TI_PRCM_H
     30  1.1  jmcneill #define	_ARM_TI_PRCM_H
     31  1.1  jmcneill 
     32  1.1  jmcneill #ifdef TI_PRCM_PRIVATE
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <dev/clk/clk_backend.h>
     35  1.1  jmcneill 
     36  1.1  jmcneill struct ti_prcm_clk;
     37  1.1  jmcneill struct ti_prcm_softc;
     38  1.1  jmcneill 
     39  1.1  jmcneill enum ti_prcm_clktype {
     40  1.1  jmcneill 	TI_PRCM_UNKNOWN,
     41  1.1  jmcneill 	TI_PRCM_FIXED,
     42  1.1  jmcneill 	TI_PRCM_FIXED_FACTOR,
     43  1.1  jmcneill 	TI_PRCM_HWMOD,
     44  1.1  jmcneill };
     45  1.1  jmcneill 
     46  1.1  jmcneill struct ti_prcm_fixed {
     47  1.1  jmcneill 	u_int			rate;
     48  1.1  jmcneill };
     49  1.1  jmcneill 
     50  1.1  jmcneill struct ti_prcm_fixed_factor {
     51  1.1  jmcneill 	u_int			mult;
     52  1.1  jmcneill 	u_int			div;
     53  1.1  jmcneill 	const char		*parent;
     54  1.1  jmcneill };
     55  1.1  jmcneill 
     56  1.1  jmcneill struct ti_prcm_hwmod {
     57  1.1  jmcneill 	bus_size_t		reg;
     58  1.3  jmcneill 	uint32_t		mask;
     59  1.1  jmcneill 	const char		*parent;
     60  1.4  jmcneill 	u_int			flags;
     61  1.1  jmcneill };
     62  1.1  jmcneill 
     63  1.4  jmcneill #define	TI_HWMOD_DISABLE_AUTOIDLE	0x01
     64  1.4  jmcneill 
     65  1.1  jmcneill struct ti_prcm_clk {
     66  1.1  jmcneill 	struct clk		base;
     67  1.1  jmcneill 	enum ti_prcm_clktype	type;
     68  1.1  jmcneill 	union {
     69  1.1  jmcneill 		struct ti_prcm_fixed fixed;
     70  1.1  jmcneill 		struct ti_prcm_fixed_factor fixed_factor;
     71  1.1  jmcneill 		struct ti_prcm_hwmod hwmod;
     72  1.1  jmcneill 	} u;
     73  1.1  jmcneill 
     74  1.1  jmcneill 	int		(*enable)(struct ti_prcm_softc *,
     75  1.1  jmcneill 				  struct ti_prcm_clk *, int);
     76  1.1  jmcneill 	u_int		(*get_rate)(struct ti_prcm_softc *,
     77  1.1  jmcneill 				    struct ti_prcm_clk *);
     78  1.1  jmcneill 	int		(*set_rate)(struct ti_prcm_softc *,
     79  1.1  jmcneill 				    struct ti_prcm_clk *, u_int);
     80  1.1  jmcneill 	const char *	(*get_parent)(struct ti_prcm_softc *,
     81  1.1  jmcneill 				      struct ti_prcm_clk *);
     82  1.1  jmcneill 	int		(*set_parent)(struct ti_prcm_softc *,
     83  1.1  jmcneill 				      struct ti_prcm_clk *,
     84  1.1  jmcneill 				      const char *);
     85  1.1  jmcneill };
     86  1.1  jmcneill 
     87  1.1  jmcneill int	ti_prcm_attach(struct ti_prcm_softc *);
     88  1.1  jmcneill struct ti_prcm_clk *ti_prcm_clock_find(struct ti_prcm_softc *, const char *);
     89  1.1  jmcneill 
     90  1.1  jmcneill static inline u_int
     91  1.1  jmcneill ti_prcm_fixed_get_rate(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
     92  1.1  jmcneill {
     93  1.1  jmcneill 	KASSERT(tc->type == TI_PRCM_FIXED);
     94  1.1  jmcneill 	return tc->u.fixed.rate;
     95  1.1  jmcneill }
     96  1.1  jmcneill 
     97  1.1  jmcneill #define	TI_PRCM_FIXED(_name, _rate)					\
     98  1.1  jmcneill 	{								\
     99  1.1  jmcneill 		.type = TI_PRCM_FIXED, .base.name = (_name),		\
    100  1.1  jmcneill 		.u.fixed.rate = (_rate),				\
    101  1.1  jmcneill 		.get_rate = ti_prcm_fixed_get_rate,			\
    102  1.1  jmcneill 	}
    103  1.1  jmcneill 
    104  1.1  jmcneill static inline u_int
    105  1.1  jmcneill ti_prcm_fixed_factor_get_rate(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    106  1.1  jmcneill {
    107  1.1  jmcneill 	KASSERT(tc->type == TI_PRCM_FIXED_FACTOR);
    108  1.1  jmcneill 	struct ti_prcm_clk *tc_parent;
    109  1.1  jmcneill 
    110  1.1  jmcneill 	tc_parent = ti_prcm_clock_find(sc, tc->u.fixed_factor.parent);
    111  1.1  jmcneill 	KASSERT(tc_parent != NULL);
    112  1.1  jmcneill 
    113  1.1  jmcneill 	const u_int mult = tc->u.fixed_factor.mult;
    114  1.1  jmcneill 	const u_int div = tc->u.fixed_factor.div;
    115  1.1  jmcneill 
    116  1.1  jmcneill 	return (clk_get_rate(&tc_parent->base) * mult) / div;
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static inline const char *
    120  1.1  jmcneill ti_prcm_fixed_factor_get_parent(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    121  1.1  jmcneill {
    122  1.1  jmcneill 	KASSERT(tc->type == TI_PRCM_FIXED_FACTOR);
    123  1.1  jmcneill 	return tc->u.fixed_factor.parent;
    124  1.1  jmcneill }
    125  1.1  jmcneill 
    126  1.1  jmcneill #define	TI_PRCM_FIXED_FACTOR(_name, _mult, _div, _parent)		\
    127  1.1  jmcneill 	{								\
    128  1.1  jmcneill 		.type = TI_PRCM_FIXED_FACTOR, .base.name = (_name),	\
    129  1.1  jmcneill 		.u.fixed_factor.mult = (_mult),				\
    130  1.1  jmcneill 		.u.fixed_factor.div = (_div),				\
    131  1.1  jmcneill 		.u.fixed_factor.parent = (_parent),			\
    132  1.1  jmcneill 		.get_rate = ti_prcm_fixed_factor_get_rate,		\
    133  1.1  jmcneill 		.get_parent = ti_prcm_fixed_factor_get_parent,		\
    134  1.1  jmcneill 	}
    135  1.1  jmcneill 
    136  1.1  jmcneill static inline const char *
    137  1.1  jmcneill ti_prcm_hwmod_get_parent(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    138  1.1  jmcneill {
    139  1.1  jmcneill 	KASSERT(tc->type == TI_PRCM_HWMOD);
    140  1.1  jmcneill 	return tc->u.hwmod.parent;
    141  1.1  jmcneill }
    142  1.1  jmcneill 
    143  1.1  jmcneill #define	TI_PRCM_HWMOD(_name, _reg, _parent, _enable)			\
    144  1.4  jmcneill 	TI_PRCM_HWMOD_MASK(_name, _reg, 0, _parent, _enable, 0)
    145  1.3  jmcneill 
    146  1.4  jmcneill #define	TI_PRCM_HWMOD_MASK(_name, _reg, _mask, _parent, _enable, _flags) \
    147  1.1  jmcneill 	{								\
    148  1.1  jmcneill 		.type = TI_PRCM_HWMOD, .base.name = (_name),		\
    149  1.1  jmcneill 		.u.hwmod.reg = (_reg),					\
    150  1.3  jmcneill 		.u.hwmod.mask = (_mask),				\
    151  1.4  jmcneill 		.u.hwmod.flags = (_flags),				\
    152  1.1  jmcneill 		.u.hwmod.parent = (_parent),				\
    153  1.1  jmcneill 		.enable = (_enable),					\
    154  1.1  jmcneill 		.get_parent = ti_prcm_hwmod_get_parent,			\
    155  1.1  jmcneill 	}
    156  1.1  jmcneill 
    157  1.1  jmcneill struct ti_prcm_softc {
    158  1.1  jmcneill 	device_t		sc_dev;
    159  1.1  jmcneill 	int			sc_phandle;
    160  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    161  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    162  1.1  jmcneill 
    163  1.1  jmcneill 	struct clk_domain	sc_clkdom;
    164  1.1  jmcneill 
    165  1.1  jmcneill 	struct ti_prcm_clk	*sc_clks;
    166  1.1  jmcneill 	u_int			sc_nclks;
    167  1.1  jmcneill };
    168  1.1  jmcneill 
    169  1.1  jmcneill #define	PRCM_READ(sc, reg)		\
    170  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    171  1.1  jmcneill #define	PRCM_WRITE(sc, reg, val)	\
    172  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    173  1.1  jmcneill 
    174  1.1  jmcneill #endif /* !TI_PRCM_PRIVATE */
    175  1.1  jmcneill 
    176  1.1  jmcneill struct clk *	ti_prcm_get_hwmod(const int, u_int);
    177  1.2  jmcneill int		ti_prcm_enable_hwmod(const int, u_int);
    178  1.1  jmcneill 
    179  1.1  jmcneill #endif /* !_ARM_TI_PRCM_H */
    180