Home | History | Annotate | Line # | Download | only in ti
ti_prcm.h revision 1.3
      1 /* $NetBSD: ti_prcm.h,v 1.3 2019/10/29 22:19:13 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #ifndef _ARM_TI_PRCM_H
     30 #define	_ARM_TI_PRCM_H
     31 
     32 #ifdef TI_PRCM_PRIVATE
     33 
     34 #include <dev/clk/clk_backend.h>
     35 
     36 struct ti_prcm_clk;
     37 struct ti_prcm_softc;
     38 
     39 enum ti_prcm_clktype {
     40 	TI_PRCM_UNKNOWN,
     41 	TI_PRCM_FIXED,
     42 	TI_PRCM_FIXED_FACTOR,
     43 	TI_PRCM_HWMOD,
     44 };
     45 
     46 struct ti_prcm_fixed {
     47 	u_int			rate;
     48 };
     49 
     50 struct ti_prcm_fixed_factor {
     51 	u_int			mult;
     52 	u_int			div;
     53 	const char		*parent;
     54 };
     55 
     56 struct ti_prcm_hwmod {
     57 	bus_size_t		reg;
     58 	uint32_t		mask;
     59 	const char		*parent;
     60 };
     61 
     62 struct ti_prcm_clk {
     63 	struct clk		base;
     64 	enum ti_prcm_clktype	type;
     65 	union {
     66 		struct ti_prcm_fixed fixed;
     67 		struct ti_prcm_fixed_factor fixed_factor;
     68 		struct ti_prcm_hwmod hwmod;
     69 	} u;
     70 
     71 	int		(*enable)(struct ti_prcm_softc *,
     72 				  struct ti_prcm_clk *, int);
     73 	u_int		(*get_rate)(struct ti_prcm_softc *,
     74 				    struct ti_prcm_clk *);
     75 	int		(*set_rate)(struct ti_prcm_softc *,
     76 				    struct ti_prcm_clk *, u_int);
     77 	const char *	(*get_parent)(struct ti_prcm_softc *,
     78 				      struct ti_prcm_clk *);
     79 	int		(*set_parent)(struct ti_prcm_softc *,
     80 				      struct ti_prcm_clk *,
     81 				      const char *);
     82 };
     83 
     84 int	ti_prcm_attach(struct ti_prcm_softc *);
     85 struct ti_prcm_clk *ti_prcm_clock_find(struct ti_prcm_softc *, const char *);
     86 
     87 static inline u_int
     88 ti_prcm_fixed_get_rate(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
     89 {
     90 	KASSERT(tc->type == TI_PRCM_FIXED);
     91 	return tc->u.fixed.rate;
     92 }
     93 
     94 #define	TI_PRCM_FIXED(_name, _rate)					\
     95 	{								\
     96 		.type = TI_PRCM_FIXED, .base.name = (_name),		\
     97 		.u.fixed.rate = (_rate),				\
     98 		.get_rate = ti_prcm_fixed_get_rate,			\
     99 	}
    100 
    101 static inline u_int
    102 ti_prcm_fixed_factor_get_rate(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    103 {
    104 	KASSERT(tc->type == TI_PRCM_FIXED_FACTOR);
    105 	struct ti_prcm_clk *tc_parent;
    106 
    107 	tc_parent = ti_prcm_clock_find(sc, tc->u.fixed_factor.parent);
    108 	KASSERT(tc_parent != NULL);
    109 
    110 	const u_int mult = tc->u.fixed_factor.mult;
    111 	const u_int div = tc->u.fixed_factor.div;
    112 
    113 	return (clk_get_rate(&tc_parent->base) * mult) / div;
    114 }
    115 
    116 static inline const char *
    117 ti_prcm_fixed_factor_get_parent(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    118 {
    119 	KASSERT(tc->type == TI_PRCM_FIXED_FACTOR);
    120 	return tc->u.fixed_factor.parent;
    121 }
    122 
    123 #define	TI_PRCM_FIXED_FACTOR(_name, _mult, _div, _parent)		\
    124 	{								\
    125 		.type = TI_PRCM_FIXED_FACTOR, .base.name = (_name),	\
    126 		.u.fixed_factor.mult = (_mult),				\
    127 		.u.fixed_factor.div = (_div),				\
    128 		.u.fixed_factor.parent = (_parent),			\
    129 		.get_rate = ti_prcm_fixed_factor_get_rate,		\
    130 		.get_parent = ti_prcm_fixed_factor_get_parent,		\
    131 	}
    132 
    133 static inline const char *
    134 ti_prcm_hwmod_get_parent(struct ti_prcm_softc *sc, struct ti_prcm_clk *tc)
    135 {
    136 	KASSERT(tc->type == TI_PRCM_HWMOD);
    137 	return tc->u.hwmod.parent;
    138 }
    139 
    140 #define	TI_PRCM_HWMOD(_name, _reg, _parent, _enable)			\
    141 	TI_PRCM_HWMOD_MASK(_name, _reg, 0, _parent, _enable)
    142 
    143 #define	TI_PRCM_HWMOD_MASK(_name, _reg, _mask, _parent, _enable)	\
    144 	{								\
    145 		.type = TI_PRCM_HWMOD, .base.name = (_name),		\
    146 		.u.hwmod.reg = (_reg),					\
    147 		.u.hwmod.mask = (_mask),				\
    148 		.u.hwmod.parent = (_parent),				\
    149 		.enable = (_enable),					\
    150 		.get_parent = ti_prcm_hwmod_get_parent,			\
    151 	}
    152 
    153 struct ti_prcm_softc {
    154 	device_t		sc_dev;
    155 	int			sc_phandle;
    156 	bus_space_tag_t		sc_bst;
    157 	bus_space_handle_t	sc_bsh;
    158 
    159 	struct clk_domain	sc_clkdom;
    160 
    161 	struct ti_prcm_clk	*sc_clks;
    162 	u_int			sc_nclks;
    163 };
    164 
    165 #define	PRCM_READ(sc, reg)		\
    166 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    167 #define	PRCM_WRITE(sc, reg, val)	\
    168 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    169 
    170 #endif /* !TI_PRCM_PRIVATE */
    171 
    172 struct clk *	ti_prcm_get_hwmod(const int, u_int);
    173 int		ti_prcm_enable_hwmod(const int, u_int);
    174 
    175 #endif /* !_ARM_TI_PRCM_H */
    176