1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2bda00303SRobert Jarzmik /* 3bda00303SRobert Jarzmik * Marvell PXA family clocks 4bda00303SRobert Jarzmik * 5bda00303SRobert Jarzmik * Copyright (C) 2014 Robert Jarzmik 6bda00303SRobert Jarzmik * 7bda00303SRobert Jarzmik * Common clock code for PXA clocks ("CKEN" type clocks + DT) 8bda00303SRobert Jarzmik */ 9bda00303SRobert Jarzmik #include <linux/clk.h> 10bda00303SRobert Jarzmik #include <linux/clk-provider.h> 11bda00303SRobert Jarzmik #include <linux/clkdev.h> 1262e59c4eSStephen Boyd #include <linux/io.h> 13bda00303SRobert Jarzmik #include <linux/of.h> 14fd13f811SArnd Bergmann #include <linux/soc/pxa/smemc.h> 15bda00303SRobert Jarzmik 16bda00303SRobert Jarzmik #include <dt-bindings/clock/pxa-clock.h> 17bda00303SRobert Jarzmik #include "clk-pxa.h" 18bda00303SRobert Jarzmik 199fe69429SRobert Jarzmik #define KHz 1000 209fe69429SRobert Jarzmik #define MHz (1000 * 1000) 219fe69429SRobert Jarzmik 229fe69429SRobert Jarzmik #define MDREFR_K0DB4 (1 << 29) /* SDCLK0 Divide by 4 Control/Status */ 239fe69429SRobert Jarzmik #define MDREFR_K2FREE (1 << 25) /* SDRAM Free-Running Control */ 249fe69429SRobert Jarzmik #define MDREFR_K1FREE (1 << 24) /* SDRAM Free-Running Control */ 259fe69429SRobert Jarzmik #define MDREFR_K0FREE (1 << 23) /* SDRAM Free-Running Control */ 269fe69429SRobert Jarzmik #define MDREFR_SLFRSH (1 << 22) /* SDRAM Self-Refresh Control/Status */ 279fe69429SRobert Jarzmik #define MDREFR_APD (1 << 20) /* SDRAM/SSRAM Auto-Power-Down Enable */ 289fe69429SRobert Jarzmik #define MDREFR_K2DB2 (1 << 19) /* SDCLK2 Divide by 2 Control/Status */ 299fe69429SRobert Jarzmik #define MDREFR_K2RUN (1 << 18) /* SDCLK2 Run Control/Status */ 309fe69429SRobert Jarzmik #define MDREFR_K1DB2 (1 << 17) /* SDCLK1 Divide by 2 Control/Status */ 319fe69429SRobert Jarzmik #define MDREFR_K1RUN (1 << 16) /* SDCLK1 Run Control/Status */ 329fe69429SRobert Jarzmik #define MDREFR_E1PIN (1 << 15) /* SDCKE1 Level Control/Status */ 339fe69429SRobert Jarzmik #define MDREFR_K0DB2 (1 << 14) /* SDCLK0 Divide by 2 Control/Status */ 349fe69429SRobert Jarzmik #define MDREFR_K0RUN (1 << 13) /* SDCLK0 Run Control/Status */ 359fe69429SRobert Jarzmik #define MDREFR_E0PIN (1 << 12) /* SDCKE0 Level Control/Status */ 369fe69429SRobert Jarzmik #define MDREFR_DB2_MASK (MDREFR_K2DB2 | MDREFR_K1DB2) 379fe69429SRobert Jarzmik #define MDREFR_DRI_MASK 0xFFF 389fe69429SRobert Jarzmik 3984558ff7SStephen Boyd static DEFINE_SPINLOCK(pxa_clk_lock); 40bda00303SRobert Jarzmik 41bda00303SRobert Jarzmik static struct clk *pxa_clocks[CLK_MAX]; 42bda00303SRobert Jarzmik static struct clk_onecell_data onecell_data = { 43bda00303SRobert Jarzmik .clks = pxa_clocks, 44bda00303SRobert Jarzmik .clk_num = CLK_MAX, 45bda00303SRobert Jarzmik }; 46bda00303SRobert Jarzmik 4714dd5b01SRobert Jarzmik struct pxa_clk { 4814dd5b01SRobert Jarzmik struct clk_hw hw; 4914dd5b01SRobert Jarzmik struct clk_fixed_factor lp; 5014dd5b01SRobert Jarzmik struct clk_fixed_factor hp; 5114dd5b01SRobert Jarzmik struct clk_gate gate; 5214dd5b01SRobert Jarzmik bool (*is_in_low_power)(void); 5314dd5b01SRobert Jarzmik }; 5414dd5b01SRobert Jarzmik 5514dd5b01SRobert Jarzmik #define to_pxa_clk(_hw) container_of(_hw, struct pxa_clk, hw) 56bda00303SRobert Jarzmik 57bda00303SRobert Jarzmik static unsigned long cken_recalc_rate(struct clk_hw *hw, 58bda00303SRobert Jarzmik unsigned long parent_rate) 59bda00303SRobert Jarzmik { 6014dd5b01SRobert Jarzmik struct pxa_clk *pclk = to_pxa_clk(hw); 61bda00303SRobert Jarzmik struct clk_fixed_factor *fix; 62bda00303SRobert Jarzmik 63bda00303SRobert Jarzmik if (!pclk->is_in_low_power || pclk->is_in_low_power()) 64bda00303SRobert Jarzmik fix = &pclk->lp; 65bda00303SRobert Jarzmik else 66bda00303SRobert Jarzmik fix = &pclk->hp; 674e907ef6SJavier Martinez Canillas __clk_hw_set_clk(&fix->hw, hw); 68bda00303SRobert Jarzmik return clk_fixed_factor_ops.recalc_rate(&fix->hw, parent_rate); 69bda00303SRobert Jarzmik } 70bda00303SRobert Jarzmik 715fc6eb7dSJulia Lawall static const struct clk_ops cken_rate_ops = { 72bda00303SRobert Jarzmik .recalc_rate = cken_recalc_rate, 73bda00303SRobert Jarzmik }; 74bda00303SRobert Jarzmik 75bda00303SRobert Jarzmik static u8 cken_get_parent(struct clk_hw *hw) 76bda00303SRobert Jarzmik { 7714dd5b01SRobert Jarzmik struct pxa_clk *pclk = to_pxa_clk(hw); 78bda00303SRobert Jarzmik 79bda00303SRobert Jarzmik if (!pclk->is_in_low_power) 80bda00303SRobert Jarzmik return 0; 81bda00303SRobert Jarzmik return pclk->is_in_low_power() ? 0 : 1; 82bda00303SRobert Jarzmik } 83bda00303SRobert Jarzmik 845fc6eb7dSJulia Lawall static const struct clk_ops cken_mux_ops = { 85*e9b6ea4eSMaxime Ripard .determine_rate = clk_hw_determine_rate_no_reparent, 86bda00303SRobert Jarzmik .get_parent = cken_get_parent, 87bda00303SRobert Jarzmik .set_parent = dummy_clk_set_parent, 88bda00303SRobert Jarzmik }; 89bda00303SRobert Jarzmik 90bda00303SRobert Jarzmik void __init clkdev_pxa_register(int ckid, const char *con_id, 91bda00303SRobert Jarzmik const char *dev_id, struct clk *clk) 92bda00303SRobert Jarzmik { 93bda00303SRobert Jarzmik if (!IS_ERR(clk) && (ckid != CLK_NONE)) 94bda00303SRobert Jarzmik pxa_clocks[ckid] = clk; 95bda00303SRobert Jarzmik if (!IS_ERR(clk)) 96bda00303SRobert Jarzmik clk_register_clkdev(clk, con_id, dev_id); 97bda00303SRobert Jarzmik } 98bda00303SRobert Jarzmik 993c816d95SArnd Bergmann int __init clk_pxa_cken_init(const struct desc_clk_cken *clks, 1003c816d95SArnd Bergmann int nb_clks, void __iomem *clk_regs) 101bda00303SRobert Jarzmik { 102bda00303SRobert Jarzmik int i; 10314dd5b01SRobert Jarzmik struct pxa_clk *pxa_clk; 104bda00303SRobert Jarzmik struct clk *clk; 105bda00303SRobert Jarzmik 106bda00303SRobert Jarzmik for (i = 0; i < nb_clks; i++) { 10714dd5b01SRobert Jarzmik pxa_clk = kzalloc(sizeof(*pxa_clk), GFP_KERNEL); 108117a1542SXiaoke Wang if (!pxa_clk) 109117a1542SXiaoke Wang return -ENOMEM; 11014dd5b01SRobert Jarzmik pxa_clk->is_in_low_power = clks[i].is_in_low_power; 11114dd5b01SRobert Jarzmik pxa_clk->lp = clks[i].lp; 11214dd5b01SRobert Jarzmik pxa_clk->hp = clks[i].hp; 11314dd5b01SRobert Jarzmik pxa_clk->gate = clks[i].gate; 1143c816d95SArnd Bergmann pxa_clk->gate.reg = clk_regs + clks[i].cken_reg; 11584558ff7SStephen Boyd pxa_clk->gate.lock = &pxa_clk_lock; 11614dd5b01SRobert Jarzmik clk = clk_register_composite(NULL, clks[i].name, 11714dd5b01SRobert Jarzmik clks[i].parent_names, 2, 11814dd5b01SRobert Jarzmik &pxa_clk->hw, &cken_mux_ops, 11914dd5b01SRobert Jarzmik &pxa_clk->hw, &cken_rate_ops, 12014dd5b01SRobert Jarzmik &pxa_clk->gate.hw, &clk_gate_ops, 12114dd5b01SRobert Jarzmik clks[i].flags); 12214dd5b01SRobert Jarzmik clkdev_pxa_register(clks[i].ckid, clks[i].con_id, 12314dd5b01SRobert Jarzmik clks[i].dev_id, clk); 124bda00303SRobert Jarzmik } 125bda00303SRobert Jarzmik return 0; 126bda00303SRobert Jarzmik } 127bda00303SRobert Jarzmik 1286f8a444aSRobert Jarzmik void __init clk_pxa_dt_common_init(struct device_node *np) 129bda00303SRobert Jarzmik { 130bda00303SRobert Jarzmik of_clk_add_provider(np, of_clk_src_onecell_get, &onecell_data); 131bda00303SRobert Jarzmik } 1329fe69429SRobert Jarzmik 1339fe69429SRobert Jarzmik void pxa2xx_core_turbo_switch(bool on) 1349fe69429SRobert Jarzmik { 1359fe69429SRobert Jarzmik unsigned long flags; 1369fe69429SRobert Jarzmik unsigned int unused, clkcfg; 1379fe69429SRobert Jarzmik 1389fe69429SRobert Jarzmik local_irq_save(flags); 1399fe69429SRobert Jarzmik 1409fe69429SRobert Jarzmik asm("mrc p14, 0, %0, c6, c0, 0" : "=r" (clkcfg)); 1419fe69429SRobert Jarzmik clkcfg &= ~CLKCFG_TURBO & ~CLKCFG_HALFTURBO; 1429fe69429SRobert Jarzmik if (on) 1439fe69429SRobert Jarzmik clkcfg |= CLKCFG_TURBO; 1449fe69429SRobert Jarzmik clkcfg |= CLKCFG_FCS; 1459fe69429SRobert Jarzmik 1469fe69429SRobert Jarzmik asm volatile( 1479fe69429SRobert Jarzmik " b 2f\n" 1489fe69429SRobert Jarzmik " .align 5\n" 1499fe69429SRobert Jarzmik "1: mcr p14, 0, %1, c6, c0, 0\n" 1509fe69429SRobert Jarzmik " b 3f\n" 1519fe69429SRobert Jarzmik "2: b 1b\n" 1529fe69429SRobert Jarzmik "3: nop\n" 153c82a2cb8SArnd Bergmann : "=&r" (unused) : "r" (clkcfg)); 1549fe69429SRobert Jarzmik 1559fe69429SRobert Jarzmik local_irq_restore(flags); 1569fe69429SRobert Jarzmik } 1579fe69429SRobert Jarzmik 1589fe69429SRobert Jarzmik void pxa2xx_cpll_change(struct pxa2xx_freq *freq, 159fd13f811SArnd Bergmann u32 (*mdrefr_dri)(unsigned int), 16084558ff7SStephen Boyd void __iomem *cccr) 1619fe69429SRobert Jarzmik { 1629fe69429SRobert Jarzmik unsigned int clkcfg = freq->clkcfg; 1639fe69429SRobert Jarzmik unsigned int unused, preset_mdrefr, postset_mdrefr; 1649fe69429SRobert Jarzmik unsigned long flags; 165fd13f811SArnd Bergmann void __iomem *mdrefr = pxa_smemc_get_mdrefr(); 1669fe69429SRobert Jarzmik 1679fe69429SRobert Jarzmik local_irq_save(flags); 1689fe69429SRobert Jarzmik 1699fe69429SRobert Jarzmik /* Calculate the next MDREFR. If we're slowing down the SDRAM clock 1709fe69429SRobert Jarzmik * we need to preset the smaller DRI before the change. If we're 1719fe69429SRobert Jarzmik * speeding up we need to set the larger DRI value after the change. 1729fe69429SRobert Jarzmik */ 1739fe69429SRobert Jarzmik preset_mdrefr = postset_mdrefr = readl(mdrefr); 1749fe69429SRobert Jarzmik if ((preset_mdrefr & MDREFR_DRI_MASK) > mdrefr_dri(freq->membus_khz)) { 1759fe69429SRobert Jarzmik preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK); 1769fe69429SRobert Jarzmik preset_mdrefr |= mdrefr_dri(freq->membus_khz); 1779fe69429SRobert Jarzmik } 1789fe69429SRobert Jarzmik postset_mdrefr = 1799fe69429SRobert Jarzmik (postset_mdrefr & ~MDREFR_DRI_MASK) | 1809fe69429SRobert Jarzmik mdrefr_dri(freq->membus_khz); 1819fe69429SRobert Jarzmik 1829fe69429SRobert Jarzmik /* If we're dividing the memory clock by two for the SDRAM clock, this 1839fe69429SRobert Jarzmik * must be set prior to the change. Clearing the divide must be done 1849fe69429SRobert Jarzmik * after the change. 1859fe69429SRobert Jarzmik */ 1869fe69429SRobert Jarzmik if (freq->div2) { 1879fe69429SRobert Jarzmik preset_mdrefr |= MDREFR_DB2_MASK; 1889fe69429SRobert Jarzmik postset_mdrefr |= MDREFR_DB2_MASK; 1899fe69429SRobert Jarzmik } else { 1909fe69429SRobert Jarzmik postset_mdrefr &= ~MDREFR_DB2_MASK; 1919fe69429SRobert Jarzmik } 1929fe69429SRobert Jarzmik 1939fe69429SRobert Jarzmik /* Set new the CCCR and prepare CLKCFG */ 1949fe69429SRobert Jarzmik writel(freq->cccr, cccr); 1959fe69429SRobert Jarzmik 1969fe69429SRobert Jarzmik asm volatile( 1979fe69429SRobert Jarzmik " ldr r4, [%1]\n" 1989fe69429SRobert Jarzmik " b 2f\n" 1999fe69429SRobert Jarzmik " .align 5\n" 2009fe69429SRobert Jarzmik "1: str %3, [%1] /* preset the MDREFR */\n" 2019fe69429SRobert Jarzmik " mcr p14, 0, %2, c6, c0, 0 /* set CLKCFG[FCS] */\n" 2029fe69429SRobert Jarzmik " str %4, [%1] /* postset the MDREFR */\n" 2039fe69429SRobert Jarzmik " b 3f\n" 2049fe69429SRobert Jarzmik "2: b 1b\n" 2059fe69429SRobert Jarzmik "3: nop\n" 2069fe69429SRobert Jarzmik : "=&r" (unused) 2079fe69429SRobert Jarzmik : "r" (mdrefr), "r" (clkcfg), "r" (preset_mdrefr), 2089fe69429SRobert Jarzmik "r" (postset_mdrefr) 2099fe69429SRobert Jarzmik : "r4", "r5"); 2109fe69429SRobert Jarzmik 2119fe69429SRobert Jarzmik local_irq_restore(flags); 2129fe69429SRobert Jarzmik } 2139fe69429SRobert Jarzmik 2149fe69429SRobert Jarzmik int pxa2xx_determine_rate(struct clk_rate_request *req, 2159fe69429SRobert Jarzmik struct pxa2xx_freq *freqs, int nb_freqs) 2169fe69429SRobert Jarzmik { 2172517b32bSArnd Bergmann int i, closest_below = -1, closest_above = -1; 2189fe69429SRobert Jarzmik unsigned long rate; 2199fe69429SRobert Jarzmik 2209fe69429SRobert Jarzmik for (i = 0; i < nb_freqs; i++) { 2219fe69429SRobert Jarzmik rate = freqs[i].cpll; 2229fe69429SRobert Jarzmik if (rate == req->rate) 2239fe69429SRobert Jarzmik break; 2249fe69429SRobert Jarzmik if (rate < req->min_rate) 2259fe69429SRobert Jarzmik continue; 2269fe69429SRobert Jarzmik if (rate > req->max_rate) 2279fe69429SRobert Jarzmik continue; 2289fe69429SRobert Jarzmik if (rate <= req->rate) 2299fe69429SRobert Jarzmik closest_below = i; 2309fe69429SRobert Jarzmik if ((rate >= req->rate) && (closest_above == -1)) 2319fe69429SRobert Jarzmik closest_above = i; 2329fe69429SRobert Jarzmik } 2339fe69429SRobert Jarzmik 2349fe69429SRobert Jarzmik req->best_parent_hw = NULL; 2359fe69429SRobert Jarzmik 2362517b32bSArnd Bergmann if (i < nb_freqs) { 2372517b32bSArnd Bergmann rate = req->rate; 2382517b32bSArnd Bergmann } else if (closest_below >= 0) { 2399fe69429SRobert Jarzmik rate = freqs[closest_below].cpll; 2402517b32bSArnd Bergmann } else if (closest_above >= 0) { 2419fe69429SRobert Jarzmik rate = freqs[closest_above].cpll; 2422517b32bSArnd Bergmann } else { 2432517b32bSArnd Bergmann pr_debug("%s(rate=%lu) no match\n", __func__, req->rate); 2442517b32bSArnd Bergmann return -EINVAL; 2452517b32bSArnd Bergmann } 2469fe69429SRobert Jarzmik 2472517b32bSArnd Bergmann pr_debug("%s(rate=%lu) rate=%lu\n", __func__, req->rate, rate); 2489fe69429SRobert Jarzmik req->rate = rate; 2499fe69429SRobert Jarzmik 2502517b32bSArnd Bergmann return 0; 2519fe69429SRobert Jarzmik } 252