···11+* Renesas R8A73A4 Clock Pulse Generator (CPG)22+33+The CPG generates core clocks for the R8A73A4 SoC. It includes five PLLs44+and several fixed ratio dividers.55+66+Required Properties:77+88+ - compatible: Must be "renesas,r8a73a4-cpg-clocks"99+1010+ - reg: Base address and length of the memory resource used by the CPG1111+1212+ - clocks: Reference to the parent clocks ("extal1" and "extal2")1313+1414+ - #clock-cells: Must be 11515+1616+ - clock-output-names: The names of the clocks. Supported clocks are "main",1717+ "pll0", "pll1", "pll2", "pll2s", "pll2h", "z", "z2", "i", "m3", "b",1818+ "m1", "m2", "zx", "zs", and "hp".1919+2020+2121+Example2222+-------2323+2424+ cpg_clocks: cpg_clocks@e6150000 {2525+ compatible = "renesas,r8a73a4-cpg-clocks";2626+ reg = <0 0xe6150000 0 0x10000>;2727+ clocks = <&extal1_clk>, <&extal2_clk>;2828+ #clock-cells = <1>;2929+ clock-output-names = "main", "pll0", "pll1", "pll2",3030+ "pll2s", "pll2h", "z", "z2",3131+ "i", "m3", "b", "m1", "m2",3232+ "zx", "zs", "hp";3333+ };
···88 - compatible: Must be one of99 - "renesas,r8a7790-cpg-clocks" for the r8a7790 CPG1010 - "renesas,r8a7791-cpg-clocks" for the r8a7791 CPG1111+ - "renesas,r8a7793-cpg-clocks" for the r8a7793 CPG1112 - "renesas,r8a7794-cpg-clocks" for the r8a7794 CPG1213 - "renesas,rcar-gen2-cpg-clocks" for the generic R-Car Gen2 CPG13141415 - reg: Base address and length of the memory resource used by the CPG15161616- - clocks: Reference to the parent clock1717+ - clocks: References to the parent clocks: first to the EXTAL clock, second1818+ to the USB_EXTAL clock1719 - #clock-cells: Must be 11820 - clock-output-names: The names of the clocks. Supported clocks are "main",1919- "pll0", "pll1", "pll3", "lb", "qspi", "sdh", "sd0", "sd1" and "z"2121+ "pll0", "pll1", "pll3", "lb", "qspi", "sdh", "sd0", "sd1", "z", "rcan", and2222+ "adsp"202321242225Example···2926 compatible = "renesas,r8a7790-cpg-clocks",3027 "renesas,rcar-gen2-cpg-clocks";3128 reg = <0 0xe6150000 0 0x1000>;3232- clocks = <&extal_clk>;2929+ clocks = <&extal_clk &usb_extal_clk>;3330 #clock-cells = <1>;3431 clock-output-names = "main", "pll0, "pll1", "pll3",3535- "lb", "qspi", "sdh", "sd0", "sd1", "z";3232+ "lb", "qspi", "sdh", "sd0", "sd1", "z",3333+ "rcan", "adsp";3634 };
···5454static void cpg_div6_clock_disable(struct clk_hw *hw)5555{5656 struct div6_clock *clock = to_div6_clock(hw);5757+ u32 val;57585858- /* DIV6 clocks require the divisor field to be non-zero when stopping5959- * the clock.5959+ val = clk_readl(clock->reg);6060+ val |= CPG_DIV6_CKSTP;6161+ /*6262+ * DIV6 clocks require the divisor field to be non-zero when stopping6363+ * the clock. However, some clocks (e.g. ZB on sh73a0) fail to be6464+ * re-enabled later if the divisor field is changed when stopping the6565+ * clock6066 */6161- clk_writel(clk_readl(clock->reg) | CPG_DIV6_CKSTP | CPG_DIV6_DIV_MASK,6262- clock->reg);6767+ if (!(val & CPG_DIV6_DIV_MASK))6868+ val |= CPG_DIV6_DIV_MASK;6969+ clk_writel(val, clock->reg);6370}64716572static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
+241
drivers/clk/shmobile/clk-r8a73a4.c
···11+/*22+ * r8a73a4 Core CPG Clocks33+ *44+ * Copyright (C) 2014 Ulrich Hecht55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; version 2 of the License.99+ */1010+1111+#include <linux/clk-provider.h>1212+#include <linux/clkdev.h>1313+#include <linux/clk/shmobile.h>1414+#include <linux/init.h>1515+#include <linux/kernel.h>1616+#include <linux/of.h>1717+#include <linux/of_address.h>1818+#include <linux/spinlock.h>1919+2020+struct r8a73a4_cpg {2121+ struct clk_onecell_data data;2222+ spinlock_t lock;2323+ void __iomem *reg;2424+};2525+2626+#define CPG_CKSCR 0xc02727+#define CPG_FRQCRA 0x002828+#define CPG_FRQCRB 0x042929+#define CPG_FRQCRC 0xe03030+#define CPG_PLL0CR 0xd83131+#define CPG_PLL1CR 0x283232+#define CPG_PLL2CR 0x2c3333+#define CPG_PLL2HCR 0xe43434+#define CPG_PLL2SCR 0xf43535+3636+#define CLK_ENABLE_ON_INIT BIT(0)3737+3838+struct div4_clk {3939+ const char *name;4040+ unsigned int reg;4141+ unsigned int shift;4242+};4343+4444+static struct div4_clk div4_clks[] = {4545+ { "i", CPG_FRQCRA, 20 },4646+ { "m3", CPG_FRQCRA, 12 },4747+ { "b", CPG_FRQCRA, 8 },4848+ { "m1", CPG_FRQCRA, 4 },4949+ { "m2", CPG_FRQCRA, 0 },5050+ { "zx", CPG_FRQCRB, 12 },5151+ { "zs", CPG_FRQCRB, 8 },5252+ { "hp", CPG_FRQCRB, 4 },5353+ { NULL, 0, 0 },5454+};5555+5656+static const struct clk_div_table div4_div_table[] = {5757+ { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },5858+ { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },5959+ { 12, 10 }, { 0, 0 }6060+};6161+6262+static struct clk * __init6363+r8a73a4_cpg_register_clock(struct device_node *np, struct r8a73a4_cpg *cpg,6464+ const char *name)6565+{6666+ const struct clk_div_table *table = NULL;6767+ const char *parent_name;6868+ unsigned int shift, reg;6969+ unsigned int mult = 1;7070+ unsigned int div = 1;7171+7272+7373+ if (!strcmp(name, "main")) {7474+ u32 ckscr = clk_readl(cpg->reg + CPG_CKSCR);7575+7676+ switch ((ckscr >> 28) & 3) {7777+ case 0: /* extal1 */7878+ parent_name = of_clk_get_parent_name(np, 0);7979+ break;8080+ case 1: /* extal1 / 2 */8181+ parent_name = of_clk_get_parent_name(np, 0);8282+ div = 2;8383+ break;8484+ case 2: /* extal2 */8585+ parent_name = of_clk_get_parent_name(np, 1);8686+ break;8787+ case 3: /* extal2 / 2 */8888+ parent_name = of_clk_get_parent_name(np, 1);8989+ div = 2;9090+ break;9191+ }9292+ } else if (!strcmp(name, "pll0")) {9393+ /* PLL0/1 are configurable multiplier clocks. Register them as9494+ * fixed factor clocks for now as there's no generic multiplier9595+ * clock implementation and we currently have no need to change9696+ * the multiplier value.9797+ */9898+ u32 value = clk_readl(cpg->reg + CPG_PLL0CR);9999+100100+ parent_name = "main";101101+ mult = ((value >> 24) & 0x7f) + 1;102102+ if (value & BIT(20))103103+ div = 2;104104+ } else if (!strcmp(name, "pll1")) {105105+ u32 value = clk_readl(cpg->reg + CPG_PLL1CR);106106+107107+ parent_name = "main";108108+ /* XXX: enable bit? */109109+ mult = ((value >> 24) & 0x7f) + 1;110110+ if (value & BIT(7))111111+ div = 2;112112+ } else if (!strncmp(name, "pll2", 4)) {113113+ u32 value, cr;114114+115115+ switch (name[4]) {116116+ case 0:117117+ cr = CPG_PLL2CR;118118+ break;119119+ case 's':120120+ cr = CPG_PLL2SCR;121121+ break;122122+ case 'h':123123+ cr = CPG_PLL2HCR;124124+ break;125125+ default:126126+ return ERR_PTR(-EINVAL);127127+ }128128+ value = clk_readl(cpg->reg + cr);129129+ switch ((value >> 5) & 7) {130130+ case 0:131131+ parent_name = "main";132132+ div = 2;133133+ break;134134+ case 1:135135+ parent_name = "extal2";136136+ div = 2;137137+ break;138138+ case 3:139139+ parent_name = "extal2";140140+ div = 4;141141+ break;142142+ case 4:143143+ parent_name = "main";144144+ break;145145+ case 5:146146+ parent_name = "extal2";147147+ break;148148+ default:149149+ pr_warn("%s: unexpected parent of %s\n", __func__,150150+ name);151151+ return ERR_PTR(-EINVAL);152152+ }153153+ /* XXX: enable bit? */154154+ mult = ((value >> 24) & 0x7f) + 1;155155+ } else if (!strcmp(name, "z") || !strcmp(name, "z2")) {156156+ u32 shift = 8;157157+158158+ parent_name = "pll0";159159+ if (name[1] == '2') {160160+ div = 2;161161+ shift = 0;162162+ }163163+ div *= 32;164164+ mult = 0x20 - ((clk_readl(cpg->reg + CPG_FRQCRC) >> shift)165165+ & 0x1f);166166+ } else {167167+ struct div4_clk *c;168168+169169+ for (c = div4_clks; c->name; c++) {170170+ if (!strcmp(name, c->name))171171+ break;172172+ }173173+ if (!c->name)174174+ return ERR_PTR(-EINVAL);175175+176176+ parent_name = "pll1";177177+ table = div4_div_table;178178+ reg = c->reg;179179+ shift = c->shift;180180+ }181181+182182+ if (!table) {183183+ return clk_register_fixed_factor(NULL, name, parent_name, 0,184184+ mult, div);185185+ } else {186186+ return clk_register_divider_table(NULL, name, parent_name, 0,187187+ cpg->reg + reg, shift, 4, 0,188188+ table, &cpg->lock);189189+ }190190+}191191+192192+static void __init r8a73a4_cpg_clocks_init(struct device_node *np)193193+{194194+ struct r8a73a4_cpg *cpg;195195+ struct clk **clks;196196+ unsigned int i;197197+ int num_clks;198198+199199+ num_clks = of_property_count_strings(np, "clock-output-names");200200+ if (num_clks < 0) {201201+ pr_err("%s: failed to count clocks\n", __func__);202202+ return;203203+ }204204+205205+ cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);206206+ clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);207207+ if (cpg == NULL || clks == NULL) {208208+ /* We're leaking memory on purpose, there's no point in cleaning209209+ * up as the system won't boot anyway.210210+ */211211+ return;212212+ }213213+214214+ spin_lock_init(&cpg->lock);215215+216216+ cpg->data.clks = clks;217217+ cpg->data.clk_num = num_clks;218218+219219+ cpg->reg = of_iomap(np, 0);220220+ if (WARN_ON(cpg->reg == NULL))221221+ return;222222+223223+ for (i = 0; i < num_clks; ++i) {224224+ const char *name;225225+ struct clk *clk;226226+227227+ of_property_read_string_index(np, "clock-output-names", i,228228+ &name);229229+230230+ clk = r8a73a4_cpg_register_clock(np, cpg, name);231231+ if (IS_ERR(clk))232232+ pr_err("%s: failed to register %s %s clock (%ld)\n",233233+ __func__, np->name, name, PTR_ERR(clk));234234+ else235235+ cpg->data.clks[i] = clk;236236+ }237237+238238+ of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);239239+}240240+CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",241241+ r8a73a4_cpg_clocks_init);