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