The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/extres/clk/clk_fixed.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_platform.h"
   31 #include <sys/param.h>
   32 #include <sys/conf.h>
   33 #include <sys/bus.h>
   34 #include <sys/kernel.h>
   35 #include <sys/kobj.h>
   36 #include <sys/malloc.h>
   37 #include <sys/mutex.h>
   38 #include <sys/module.h>
   39 #include <sys/rman.h>
   40 #include <sys/systm.h>
   41 
   42 #include <machine/bus.h>
   43 
   44 #ifdef FDT
   45 #include <dev/ofw/ofw_bus.h>
   46 #include <dev/ofw/ofw_bus_subr.h>
   47 #endif
   48 
   49 #include <dev/extres/clk/clk_fixed.h>
   50 
   51 #define CLK_TYPE_FIXED          1
   52 #define CLK_TYPE_FIXED_FACTOR   2
   53 
   54 static int clknode_fixed_init(struct clknode *clk, device_t dev);
   55 static int clknode_fixed_recalc(struct clknode *clk, uint64_t *freq);
   56 static int clknode_fixed_set_freq(struct clknode *clk, uint64_t fin,
   57     uint64_t *fout, int flags, int *stop);
   58 
   59 struct clknode_fixed_sc {
   60         int             fixed_flags;
   61         uint64_t        freq;
   62         uint32_t        mult;
   63         uint32_t        div;
   64 };
   65 
   66 static clknode_method_t clknode_fixed_methods[] = {
   67         /* Device interface */
   68         CLKNODEMETHOD(clknode_init,        clknode_fixed_init),
   69         CLKNODEMETHOD(clknode_recalc_freq, clknode_fixed_recalc),
   70         CLKNODEMETHOD(clknode_set_freq,    clknode_fixed_set_freq),
   71         CLKNODEMETHOD_END
   72 };
   73 DEFINE_CLASS_1(clknode_fixed, clknode_fixed_class, clknode_fixed_methods,
   74    sizeof(struct clknode_fixed_sc), clknode_class);
   75 
   76 static int
   77 clknode_fixed_init(struct clknode *clk, device_t dev)
   78 {
   79         struct clknode_fixed_sc *sc;
   80 
   81         sc = clknode_get_softc(clk);
   82         if (sc->freq == 0)
   83                 clknode_init_parent_idx(clk, 0);
   84         return(0);
   85 }
   86 
   87 static int
   88 clknode_fixed_recalc(struct clknode *clk, uint64_t *freq)
   89 {
   90         struct clknode_fixed_sc *sc;
   91 
   92         sc = clknode_get_softc(clk);
   93 
   94         if ((sc->mult != 0) && (sc->div != 0))
   95                 *freq = (*freq / sc->div) * sc->mult;
   96         else
   97                 *freq = sc->freq;
   98         return (0);
   99 }
  100 
  101 static int
  102 clknode_fixed_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
  103     int flags, int *stop)
  104 {
  105         struct clknode_fixed_sc *sc;
  106 
  107         sc = clknode_get_softc(clk);
  108         if (sc->mult == 0 || sc->div == 0) {
  109                 /* Fixed frequency clock. */
  110                 *stop = 1;
  111                 if (*fout != sc->freq)
  112                         return (ERANGE);
  113                 return (0);
  114         }
  115         /* Fixed factor clock. */
  116         *stop = 0;
  117         *fout = (*fout / sc->mult) *  sc->div;
  118         return (0);
  119 }
  120 
  121 int
  122 clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef)
  123 {
  124         struct clknode *clk;
  125         struct clknode_fixed_sc *sc;
  126 
  127         clk = clknode_create(clkdom, &clknode_fixed_class, &clkdef->clkdef);
  128         if (clk == NULL)
  129                 return (1);
  130 
  131         sc = clknode_get_softc(clk);
  132         sc->fixed_flags = clkdef->fixed_flags;
  133         sc->freq = clkdef->freq;
  134         sc->mult = clkdef->mult;
  135         sc->div = clkdef->div;
  136 
  137         clknode_register(clkdom, clk);
  138         return (0);
  139 }
  140 
  141 #ifdef FDT
  142 
  143 static struct ofw_compat_data compat_data[] = {
  144         {"fixed-clock",         CLK_TYPE_FIXED},
  145         {"fixed-factor-clock",  CLK_TYPE_FIXED_FACTOR},
  146         {NULL,                  0},
  147 };
  148 
  149 struct clk_fixed_softc {
  150         device_t        dev;
  151         struct clkdom   *clkdom;
  152 };
  153 
  154 static int
  155 clk_fixed_probe(device_t dev)
  156 {
  157         intptr_t clk_type;
  158 
  159         clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
  160         switch (clk_type) {
  161         case CLK_TYPE_FIXED:
  162                 device_set_desc(dev, "Fixed clock");
  163                 return (BUS_PROBE_DEFAULT);
  164         case CLK_TYPE_FIXED_FACTOR:
  165                 device_set_desc(dev, "Fixed factor clock");
  166                 return (BUS_PROBE_DEFAULT);
  167         default:
  168                 return (ENXIO);
  169         }
  170 }
  171 
  172 static int
  173 clk_fixed_init_fixed(struct clk_fixed_softc *sc, phandle_t node,
  174     struct clk_fixed_def *def)
  175 {
  176         uint32_t freq;
  177         int rv;
  178 
  179         def->clkdef.id = 1;
  180         rv = OF_getencprop(node, "clock-frequency", &freq,  sizeof(freq));
  181         if (rv <= 0)
  182                 return (ENXIO);
  183         def->freq = freq;
  184         return (0);
  185 }
  186 
  187 static int
  188 clk_fixed_init_fixed_factor(struct clk_fixed_softc *sc, phandle_t node,
  189     struct clk_fixed_def *def)
  190 {
  191         int rv;
  192         clk_t  parent;
  193 
  194         def->clkdef.id = 1;
  195         rv = OF_getencprop(node, "clock-mult", &def->mult,  sizeof(def->mult));
  196         if (rv <= 0)
  197                 return (ENXIO);
  198         rv = OF_getencprop(node, "clock-div", &def->div,  sizeof(def->div));
  199         if (rv <= 0)
  200                 return (ENXIO);
  201         /* Get name of parent clock */
  202         rv = clk_get_by_ofw_index(sc->dev, 0, 0, &parent);
  203         if (rv != 0)
  204                 return (ENXIO);
  205         def->clkdef.parent_names = malloc(sizeof(char *), M_OFWPROP, M_WAITOK);
  206         def->clkdef.parent_names[0] = clk_get_name(parent);
  207         def->clkdef.parent_cnt  = 1;
  208         clk_release(parent);
  209         return (0);
  210 }
  211 
  212 static int
  213 clk_fixed_attach(device_t dev)
  214 {
  215         struct clk_fixed_softc *sc;
  216         intptr_t clk_type;
  217         phandle_t node;
  218         struct clk_fixed_def def;
  219         int rv;
  220 
  221         sc = device_get_softc(dev);
  222         sc->dev = dev;
  223         node  = ofw_bus_get_node(dev);
  224         clk_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
  225 
  226         bzero(&def, sizeof(def));
  227         if (clk_type == CLK_TYPE_FIXED)
  228                 rv = clk_fixed_init_fixed(sc, node, &def);
  229         else if (clk_type == CLK_TYPE_FIXED_FACTOR)
  230                 rv = clk_fixed_init_fixed_factor(sc, node, &def);
  231         else
  232                 rv = ENXIO;
  233         if (rv != 0) {
  234                 device_printf(sc->dev, "Cannot FDT parameters.\n");
  235                 goto fail;
  236         }
  237         rv = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
  238         if (rv != 0) {
  239                 device_printf(sc->dev, "Cannot parse clock name.\n");
  240                 goto fail;
  241         }
  242         sc->clkdom = clkdom_create(dev);
  243         KASSERT(sc->clkdom != NULL, ("Clock domain is NULL"));
  244 
  245         rv = clknode_fixed_register(sc->clkdom, &def);
  246         if (rv != 0) {
  247                 device_printf(sc->dev, "Cannot register fixed clock.\n");
  248                 rv = ENXIO;
  249                 goto fail;
  250         }
  251 
  252         rv = clkdom_finit(sc->clkdom);
  253         if (rv != 0) {
  254                 device_printf(sc->dev, "Clk domain finit fails.\n");
  255                 rv = ENXIO;
  256                 goto fail;
  257         }
  258 #ifdef CLK_DEBUG
  259         clkdom_dump(sc->clkdom);
  260 #endif
  261         OF_prop_free(__DECONST(char *, def.clkdef.name));
  262         OF_prop_free(def.clkdef.parent_names);
  263         return (bus_generic_attach(dev));
  264 
  265 fail:
  266         OF_prop_free(__DECONST(char *, def.clkdef.name));
  267         OF_prop_free(def.clkdef.parent_names);
  268         return (rv);
  269 }
  270 
  271 static device_method_t clk_fixed_methods[] = {
  272         /* Device interface */
  273         DEVMETHOD(device_probe,         clk_fixed_probe),
  274         DEVMETHOD(device_attach,        clk_fixed_attach),
  275 
  276         DEVMETHOD_END
  277 };
  278 
  279 DEFINE_CLASS_0(clk_fixed, clk_fixed_driver, clk_fixed_methods,
  280     sizeof(struct clk_fixed_softc));
  281 EARLY_DRIVER_MODULE(clk_fixed, simplebus, clk_fixed_driver, 0, 0,
  282     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
  283 MODULE_VERSION(clk_fixed, 1);
  284 
  285 #endif

Cache object: f01e4180e2ccb88db30f3570eb620e9f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.