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