Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * drivers/clk/tegra/clk-emc.c
0004  *
0005  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
0006  *
0007  * Author:
0008  *  Mikko Perttunen <mperttunen@nvidia.com>
0009  */
0010 
0011 #include <linux/clk-provider.h>
0012 #include <linux/clk.h>
0013 #include <linux/clkdev.h>
0014 #include <linux/clk/tegra.h>
0015 #include <linux/delay.h>
0016 #include <linux/export.h>
0017 #include <linux/io.h>
0018 #include <linux/module.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/sort.h>
0023 #include <linux/string.h>
0024 
0025 #include <soc/tegra/fuse.h>
0026 
0027 #include "clk.h"
0028 
0029 #define CLK_SOURCE_EMC 0x19c
0030 
0031 #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT 0
0032 #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK 0xff
0033 #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK) << \
0034                           CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT)
0035 
0036 #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT 29
0037 #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK 0x7
0038 #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK) << \
0039                       CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
0040 
0041 static const char * const emc_parent_clk_names[] = {
0042     "pll_m", "pll_c", "pll_p", "clk_m", "pll_m_ud",
0043     "pll_c2", "pll_c3", "pll_c_ud"
0044 };
0045 
0046 /*
0047  * List of clock sources for various parents the EMC clock can have.
0048  * When we change the timing to a timing with a parent that has the same
0049  * clock source as the current parent, we must first change to a backup
0050  * timing that has a different clock source.
0051  */
0052 
0053 #define EMC_SRC_PLL_M 0
0054 #define EMC_SRC_PLL_C 1
0055 #define EMC_SRC_PLL_P 2
0056 #define EMC_SRC_CLK_M 3
0057 #define EMC_SRC_PLL_C2 4
0058 #define EMC_SRC_PLL_C3 5
0059 
0060 static const char emc_parent_clk_sources[] = {
0061     EMC_SRC_PLL_M, EMC_SRC_PLL_C, EMC_SRC_PLL_P, EMC_SRC_CLK_M,
0062     EMC_SRC_PLL_M, EMC_SRC_PLL_C2, EMC_SRC_PLL_C3, EMC_SRC_PLL_C
0063 };
0064 
0065 struct emc_timing {
0066     unsigned long rate, parent_rate;
0067     u8 parent_index;
0068     struct clk *parent;
0069     u32 ram_code;
0070 };
0071 
0072 struct tegra_clk_emc {
0073     struct clk_hw hw;
0074     void __iomem *clk_regs;
0075     struct clk *prev_parent;
0076     bool changing_timing;
0077 
0078     struct device_node *emc_node;
0079     struct tegra_emc *emc;
0080 
0081     int num_timings;
0082     struct emc_timing *timings;
0083     spinlock_t *lock;
0084 
0085     tegra124_emc_prepare_timing_change_cb *prepare_timing_change;
0086     tegra124_emc_complete_timing_change_cb *complete_timing_change;
0087 };
0088 
0089 /* Common clock framework callback implementations */
0090 
0091 static unsigned long emc_recalc_rate(struct clk_hw *hw,
0092                      unsigned long parent_rate)
0093 {
0094     struct tegra_clk_emc *tegra;
0095     u32 val, div;
0096 
0097     tegra = container_of(hw, struct tegra_clk_emc, hw);
0098 
0099     /*
0100      * CCF wrongly assumes that the parent won't change during set_rate,
0101      * so get the parent rate explicitly.
0102      */
0103     parent_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
0104 
0105     val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
0106     div = val & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK;
0107 
0108     return parent_rate / (div + 2) * 2;
0109 }
0110 
0111 /*
0112  * Rounds up unless no higher rate exists, in which case down. This way is
0113  * safer since things have EMC rate floors. Also don't touch parent_rate
0114  * since we don't want the CCF to play with our parent clocks.
0115  */
0116 static int emc_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
0117 {
0118     struct tegra_clk_emc *tegra;
0119     u8 ram_code = tegra_read_ram_code();
0120     struct emc_timing *timing = NULL;
0121     int i, k, t;
0122 
0123     tegra = container_of(hw, struct tegra_clk_emc, hw);
0124 
0125     for (k = 0; k < tegra->num_timings; k++) {
0126         if (tegra->timings[k].ram_code == ram_code)
0127             break;
0128     }
0129 
0130     for (t = k; t < tegra->num_timings; t++) {
0131         if (tegra->timings[t].ram_code != ram_code)
0132             break;
0133     }
0134 
0135     for (i = k; i < t; i++) {
0136         timing = tegra->timings + i;
0137 
0138         if (timing->rate < req->rate && i != t - 1)
0139             continue;
0140 
0141         if (timing->rate > req->max_rate) {
0142             i = max(i, k + 1);
0143             req->rate = tegra->timings[i - 1].rate;
0144             return 0;
0145         }
0146 
0147         if (timing->rate < req->min_rate)
0148             continue;
0149 
0150         req->rate = timing->rate;
0151         return 0;
0152     }
0153 
0154     if (timing) {
0155         req->rate = timing->rate;
0156         return 0;
0157     }
0158 
0159     req->rate = clk_hw_get_rate(hw);
0160     return 0;
0161 }
0162 
0163 static u8 emc_get_parent(struct clk_hw *hw)
0164 {
0165     struct tegra_clk_emc *tegra;
0166     u32 val;
0167 
0168     tegra = container_of(hw, struct tegra_clk_emc, hw);
0169 
0170     val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
0171 
0172     return (val >> CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
0173         & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK;
0174 }
0175 
0176 static struct tegra_emc *emc_ensure_emc_driver(struct tegra_clk_emc *tegra)
0177 {
0178     struct platform_device *pdev;
0179 
0180     if (tegra->emc)
0181         return tegra->emc;
0182 
0183     if (!tegra->prepare_timing_change || !tegra->complete_timing_change)
0184         return NULL;
0185 
0186     if (!tegra->emc_node)
0187         return NULL;
0188 
0189     pdev = of_find_device_by_node(tegra->emc_node);
0190     if (!pdev) {
0191         pr_err("%s: could not get external memory controller\n",
0192                __func__);
0193         return NULL;
0194     }
0195 
0196     of_node_put(tegra->emc_node);
0197     tegra->emc_node = NULL;
0198 
0199     tegra->emc = platform_get_drvdata(pdev);
0200     if (!tegra->emc) {
0201         put_device(&pdev->dev);
0202         pr_err("%s: cannot find EMC driver\n", __func__);
0203         return NULL;
0204     }
0205 
0206     return tegra->emc;
0207 }
0208 
0209 static int emc_set_timing(struct tegra_clk_emc *tegra,
0210               struct emc_timing *timing)
0211 {
0212     int err;
0213     u8 div;
0214     u32 car_value;
0215     unsigned long flags = 0;
0216     struct tegra_emc *emc = emc_ensure_emc_driver(tegra);
0217 
0218     if (!emc)
0219         return -ENOENT;
0220 
0221     pr_debug("going to rate %ld prate %ld p %s\n", timing->rate,
0222          timing->parent_rate, __clk_get_name(timing->parent));
0223 
0224     if (emc_get_parent(&tegra->hw) == timing->parent_index &&
0225         clk_get_rate(timing->parent) != timing->parent_rate) {
0226         WARN_ONCE(1, "parent %s rate mismatch %lu %lu\n",
0227               __clk_get_name(timing->parent),
0228               clk_get_rate(timing->parent),
0229               timing->parent_rate);
0230         return -EINVAL;
0231     }
0232 
0233     tegra->changing_timing = true;
0234 
0235     err = clk_set_rate(timing->parent, timing->parent_rate);
0236     if (err) {
0237         pr_err("cannot change parent %s rate to %ld: %d\n",
0238                __clk_get_name(timing->parent), timing->parent_rate,
0239                err);
0240 
0241         return err;
0242     }
0243 
0244     err = clk_prepare_enable(timing->parent);
0245     if (err) {
0246         pr_err("cannot enable parent clock: %d\n", err);
0247         return err;
0248     }
0249 
0250     div = timing->parent_rate / (timing->rate / 2) - 2;
0251 
0252     err = tegra->prepare_timing_change(emc, timing->rate);
0253     if (err) {
0254         clk_disable_unprepare(timing->parent);
0255         return err;
0256     }
0257 
0258     spin_lock_irqsave(tegra->lock, flags);
0259 
0260     car_value = readl(tegra->clk_regs + CLK_SOURCE_EMC);
0261 
0262     car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_SRC(~0);
0263     car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_SRC(timing->parent_index);
0264 
0265     car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(~0);
0266     car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(div);
0267 
0268     writel(car_value, tegra->clk_regs + CLK_SOURCE_EMC);
0269 
0270     spin_unlock_irqrestore(tegra->lock, flags);
0271 
0272     tegra->complete_timing_change(emc, timing->rate);
0273 
0274     clk_hw_reparent(&tegra->hw, __clk_get_hw(timing->parent));
0275     clk_disable_unprepare(tegra->prev_parent);
0276 
0277     tegra->prev_parent = timing->parent;
0278     tegra->changing_timing = false;
0279 
0280     return 0;
0281 }
0282 
0283 /*
0284  * Get backup timing to use as an intermediate step when a change between
0285  * two timings with the same clock source has been requested. First try to
0286  * find a timing with a higher clock rate to avoid a rate below any set rate
0287  * floors. If that is not possible, find a lower rate.
0288  */
0289 static struct emc_timing *get_backup_timing(struct tegra_clk_emc *tegra,
0290                         int timing_index)
0291 {
0292     int i;
0293     u32 ram_code = tegra_read_ram_code();
0294     struct emc_timing *timing;
0295 
0296     for (i = timing_index+1; i < tegra->num_timings; i++) {
0297         timing = tegra->timings + i;
0298         if (timing->ram_code != ram_code)
0299             break;
0300 
0301         if (emc_parent_clk_sources[timing->parent_index] !=
0302             emc_parent_clk_sources[
0303               tegra->timings[timing_index].parent_index])
0304             return timing;
0305     }
0306 
0307     for (i = timing_index-1; i >= 0; --i) {
0308         timing = tegra->timings + i;
0309         if (timing->ram_code != ram_code)
0310             break;
0311 
0312         if (emc_parent_clk_sources[timing->parent_index] !=
0313             emc_parent_clk_sources[
0314               tegra->timings[timing_index].parent_index])
0315             return timing;
0316     }
0317 
0318     return NULL;
0319 }
0320 
0321 static int emc_set_rate(struct clk_hw *hw, unsigned long rate,
0322             unsigned long parent_rate)
0323 {
0324     struct tegra_clk_emc *tegra;
0325     struct emc_timing *timing = NULL;
0326     int i, err;
0327     u32 ram_code = tegra_read_ram_code();
0328 
0329     tegra = container_of(hw, struct tegra_clk_emc, hw);
0330 
0331     if (clk_hw_get_rate(hw) == rate)
0332         return 0;
0333 
0334     /*
0335      * When emc_set_timing changes the parent rate, CCF will propagate
0336      * that downward to us, so ignore any set_rate calls while a rate
0337      * change is already going on.
0338      */
0339     if (tegra->changing_timing)
0340         return 0;
0341 
0342     for (i = 0; i < tegra->num_timings; i++) {
0343         if (tegra->timings[i].rate == rate &&
0344             tegra->timings[i].ram_code == ram_code) {
0345             timing = tegra->timings + i;
0346             break;
0347         }
0348     }
0349 
0350     if (!timing) {
0351         pr_err("cannot switch to rate %ld without emc table\n", rate);
0352         return -EINVAL;
0353     }
0354 
0355     if (emc_parent_clk_sources[emc_get_parent(hw)] ==
0356         emc_parent_clk_sources[timing->parent_index] &&
0357         clk_get_rate(timing->parent) != timing->parent_rate) {
0358         /*
0359          * Parent clock source not changed but parent rate has changed,
0360          * need to temporarily switch to another parent
0361          */
0362 
0363         struct emc_timing *backup_timing;
0364 
0365         backup_timing = get_backup_timing(tegra, i);
0366         if (!backup_timing) {
0367             pr_err("cannot find backup timing\n");
0368             return -EINVAL;
0369         }
0370 
0371         pr_debug("using %ld as backup rate when going to %ld\n",
0372              backup_timing->rate, rate);
0373 
0374         err = emc_set_timing(tegra, backup_timing);
0375         if (err) {
0376             pr_err("cannot set backup timing: %d\n", err);
0377             return err;
0378         }
0379     }
0380 
0381     return emc_set_timing(tegra, timing);
0382 }
0383 
0384 /* Initialization and deinitialization */
0385 
0386 static int load_one_timing_from_dt(struct tegra_clk_emc *tegra,
0387                    struct emc_timing *timing,
0388                    struct device_node *node)
0389 {
0390     int err, i;
0391     u32 tmp;
0392 
0393     err = of_property_read_u32(node, "clock-frequency", &tmp);
0394     if (err) {
0395         pr_err("timing %pOF: failed to read rate\n", node);
0396         return err;
0397     }
0398 
0399     timing->rate = tmp;
0400 
0401     err = of_property_read_u32(node, "nvidia,parent-clock-frequency", &tmp);
0402     if (err) {
0403         pr_err("timing %pOF: failed to read parent rate\n", node);
0404         return err;
0405     }
0406 
0407     timing->parent_rate = tmp;
0408 
0409     timing->parent = of_clk_get_by_name(node, "emc-parent");
0410     if (IS_ERR(timing->parent)) {
0411         pr_err("timing %pOF: failed to get parent clock\n", node);
0412         return PTR_ERR(timing->parent);
0413     }
0414 
0415     timing->parent_index = 0xff;
0416     i = match_string(emc_parent_clk_names, ARRAY_SIZE(emc_parent_clk_names),
0417              __clk_get_name(timing->parent));
0418     if (i < 0) {
0419         pr_err("timing %pOF: %s is not a valid parent\n",
0420                node, __clk_get_name(timing->parent));
0421         clk_put(timing->parent);
0422         return -EINVAL;
0423     }
0424 
0425     timing->parent_index = i;
0426     return 0;
0427 }
0428 
0429 static int cmp_timings(const void *_a, const void *_b)
0430 {
0431     const struct emc_timing *a = _a;
0432     const struct emc_timing *b = _b;
0433 
0434     if (a->rate < b->rate)
0435         return -1;
0436     else if (a->rate == b->rate)
0437         return 0;
0438     else
0439         return 1;
0440 }
0441 
0442 static int load_timings_from_dt(struct tegra_clk_emc *tegra,
0443                 struct device_node *node,
0444                 u32 ram_code)
0445 {
0446     struct emc_timing *timings_ptr;
0447     struct device_node *child;
0448     int child_count = of_get_child_count(node);
0449     int i = 0, err;
0450     size_t size;
0451 
0452     size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
0453 
0454     tegra->timings = krealloc(tegra->timings, size, GFP_KERNEL);
0455     if (!tegra->timings)
0456         return -ENOMEM;
0457 
0458     timings_ptr = tegra->timings + tegra->num_timings;
0459     tegra->num_timings += child_count;
0460 
0461     for_each_child_of_node(node, child) {
0462         struct emc_timing *timing = timings_ptr + (i++);
0463 
0464         err = load_one_timing_from_dt(tegra, timing, child);
0465         if (err) {
0466             of_node_put(child);
0467             return err;
0468         }
0469 
0470         timing->ram_code = ram_code;
0471     }
0472 
0473     sort(timings_ptr, child_count, sizeof(struct emc_timing),
0474          cmp_timings, NULL);
0475 
0476     return 0;
0477 }
0478 
0479 static const struct clk_ops tegra_clk_emc_ops = {
0480     .recalc_rate = emc_recalc_rate,
0481     .determine_rate = emc_determine_rate,
0482     .set_rate = emc_set_rate,
0483     .get_parent = emc_get_parent,
0484 };
0485 
0486 struct clk *tegra124_clk_register_emc(void __iomem *base, struct device_node *np,
0487                       spinlock_t *lock)
0488 {
0489     struct tegra_clk_emc *tegra;
0490     struct clk_init_data init;
0491     struct device_node *node;
0492     u32 node_ram_code;
0493     struct clk *clk;
0494     int err;
0495 
0496     tegra = kcalloc(1, sizeof(*tegra), GFP_KERNEL);
0497     if (!tegra)
0498         return ERR_PTR(-ENOMEM);
0499 
0500     tegra->clk_regs = base;
0501     tegra->lock = lock;
0502 
0503     tegra->num_timings = 0;
0504 
0505     for_each_child_of_node(np, node) {
0506         err = of_property_read_u32(node, "nvidia,ram-code",
0507                        &node_ram_code);
0508         if (err)
0509             continue;
0510 
0511         /*
0512          * Store timings for all ram codes as we cannot read the
0513          * fuses until the apbmisc driver is loaded.
0514          */
0515         err = load_timings_from_dt(tegra, node, node_ram_code);
0516         if (err) {
0517             of_node_put(node);
0518             return ERR_PTR(err);
0519         }
0520     }
0521 
0522     if (tegra->num_timings == 0)
0523         pr_warn("%s: no memory timings registered\n", __func__);
0524 
0525     tegra->emc_node = of_parse_phandle(np,
0526             "nvidia,external-memory-controller", 0);
0527     if (!tegra->emc_node)
0528         pr_warn("%s: couldn't find node for EMC driver\n", __func__);
0529 
0530     init.name = "emc";
0531     init.ops = &tegra_clk_emc_ops;
0532     init.flags = CLK_IS_CRITICAL;
0533     init.parent_names = emc_parent_clk_names;
0534     init.num_parents = ARRAY_SIZE(emc_parent_clk_names);
0535 
0536     tegra->hw.init = &init;
0537 
0538     clk = clk_register(NULL, &tegra->hw);
0539     if (IS_ERR(clk))
0540         return clk;
0541 
0542     tegra->prev_parent = clk_hw_get_parent_by_index(
0543         &tegra->hw, emc_get_parent(&tegra->hw))->clk;
0544     tegra->changing_timing = false;
0545 
0546     /* Allow debugging tools to see the EMC clock */
0547     clk_register_clkdev(clk, "emc", "tegra-clk-debug");
0548 
0549     return clk;
0550 };
0551 
0552 void tegra124_clk_set_emc_callbacks(tegra124_emc_prepare_timing_change_cb *prep_cb,
0553                     tegra124_emc_complete_timing_change_cb *complete_cb)
0554 {
0555     struct clk *clk = __clk_lookup("emc");
0556     struct tegra_clk_emc *tegra;
0557     struct clk_hw *hw;
0558 
0559     if (clk) {
0560         hw = __clk_get_hw(clk);
0561         tegra = container_of(hw, struct tegra_clk_emc, hw);
0562 
0563         tegra->prepare_timing_change = prep_cb;
0564         tegra->complete_timing_change = complete_cb;
0565     }
0566 }
0567 EXPORT_SYMBOL_GPL(tegra124_clk_set_emc_callbacks);
0568 
0569 bool tegra124_clk_emc_driver_available(struct clk_hw *hw)
0570 {
0571     struct tegra_clk_emc *tegra = container_of(hw, struct tegra_clk_emc, hw);
0572 
0573     return tegra->prepare_timing_change && tegra->complete_timing_change;
0574 }