1bda00303SRobert Jarzmik /* 2bda00303SRobert Jarzmik * Marvell PXA family clocks 3bda00303SRobert Jarzmik * 4bda00303SRobert Jarzmik * Copyright (C) 2014 Robert Jarzmik 5bda00303SRobert Jarzmik * 6bda00303SRobert Jarzmik * Common clock code for PXA clocks ("CKEN" type clocks + DT) 7bda00303SRobert Jarzmik * 8bda00303SRobert Jarzmik * This program is free software; you can redistribute it and/or modify 9bda00303SRobert Jarzmik * it under the terms of the GNU General Public License as published by 10bda00303SRobert Jarzmik * the Free Software Foundation; version 2 of the License. 11bda00303SRobert Jarzmik * 12bda00303SRobert Jarzmik */ 13bda00303SRobert Jarzmik #include <linux/clk.h> 14bda00303SRobert Jarzmik #include <linux/clk-provider.h> 15bda00303SRobert Jarzmik #include <linux/clkdev.h> 16bda00303SRobert Jarzmik #include <linux/of.h> 17bda00303SRobert Jarzmik 18bda00303SRobert Jarzmik #include <dt-bindings/clock/pxa-clock.h> 19bda00303SRobert Jarzmik #include "clk-pxa.h" 20bda00303SRobert Jarzmik 219fe69429SRobert Jarzmik #define KHz 1000 229fe69429SRobert Jarzmik #define MHz (1000 * 1000) 239fe69429SRobert Jarzmik 249fe69429SRobert Jarzmik #define MDREFR_K0DB4 (1 << 29) /* SDCLK0 Divide by 4 Control/Status */ 259fe69429SRobert Jarzmik #define MDREFR_K2FREE (1 << 25) /* SDRAM Free-Running Control */ 269fe69429SRobert Jarzmik #define MDREFR_K1FREE (1 << 24) /* SDRAM Free-Running Control */ 279fe69429SRobert Jarzmik #define MDREFR_K0FREE (1 << 23) /* SDRAM Free-Running Control */ 289fe69429SRobert Jarzmik #define MDREFR_SLFRSH (1 << 22) /* SDRAM Self-Refresh Control/Status */ 299fe69429SRobert Jarzmik #define MDREFR_APD (1 << 20) /* SDRAM/SSRAM Auto-Power-Down Enable */ 309fe69429SRobert Jarzmik #define MDREFR_K2DB2 (1 << 19) /* SDCLK2 Divide by 2 Control/Status */ 319fe69429SRobert Jarzmik #define MDREFR_K2RUN (1 << 18) /* SDCLK2 Run Control/Status */ 329fe69429SRobert Jarzmik #define MDREFR_K1DB2 (1 << 17) /* SDCLK1 Divide by 2 Control/Status */ 339fe69429SRobert Jarzmik #define MDREFR_K1RUN (1 << 16) /* SDCLK1 Run Control/Status */ 349fe69429SRobert Jarzmik #define MDREFR_E1PIN (1 << 15) /* SDCKE1 Level Control/Status */ 359fe69429SRobert Jarzmik #define MDREFR_K0DB2 (1 << 14) /* SDCLK0 Divide by 2 Control/Status */ 369fe69429SRobert Jarzmik #define MDREFR_K0RUN (1 << 13) /* SDCLK0 Run Control/Status */ 379fe69429SRobert Jarzmik #define MDREFR_E0PIN (1 << 12) /* SDCKE0 Level Control/Status */ 389fe69429SRobert Jarzmik #define MDREFR_DB2_MASK (MDREFR_K2DB2 | MDREFR_K1DB2) 399fe69429SRobert Jarzmik #define MDREFR_DRI_MASK 0xFFF 409fe69429SRobert Jarzmik 41*84558ff7SStephen Boyd static DEFINE_SPINLOCK(pxa_clk_lock); 42bda00303SRobert Jarzmik 43bda00303SRobert Jarzmik static struct clk *pxa_clocks[CLK_MAX]; 44bda00303SRobert Jarzmik static struct clk_onecell_data onecell_data = { 45bda00303SRobert Jarzmik .clks = pxa_clocks, 46bda00303SRobert Jarzmik .clk_num = CLK_MAX, 47bda00303SRobert Jarzmik }; 48bda00303SRobert Jarzmik 4914dd5b01SRobert Jarzmik struct pxa_clk { 5014dd5b01SRobert Jarzmik struct clk_hw hw; 5114dd5b01SRobert Jarzmik struct clk_fixed_factor lp; 5214dd5b01SRobert Jarzmik struct clk_fixed_factor hp; 5314dd5b01SRobert Jarzmik struct clk_gate gate; 5414dd5b01SRobert Jarzmik bool (*is_in_low_power)(void); 5514dd5b01SRobert Jarzmik }; 5614dd5b01SRobert Jarzmik 5714dd5b01SRobert Jarzmik #define to_pxa_clk(_hw) container_of(_hw, struct pxa_clk, hw) 58bda00303SRobert Jarzmik 59bda00303SRobert Jarzmik static unsigned long cken_recalc_rate(struct clk_hw *hw, 60bda00303SRobert Jarzmik unsigned long parent_rate) 61bda00303SRobert Jarzmik { 6214dd5b01SRobert Jarzmik struct pxa_clk *pclk = to_pxa_clk(hw); 63bda00303SRobert Jarzmik struct clk_fixed_factor *fix; 64bda00303SRobert Jarzmik 65bda00303SRobert Jarzmik if (!pclk->is_in_low_power || pclk->is_in_low_power()) 66bda00303SRobert Jarzmik fix = &pclk->lp; 67bda00303SRobert Jarzmik else 68bda00303SRobert Jarzmik fix = &pclk->hp; 694e907ef6SJavier Martinez Canillas __clk_hw_set_clk(&fix->hw, hw); 70bda00303SRobert Jarzmik return clk_fixed_factor_ops.recalc_rate(&fix->hw, parent_rate); 71bda00303SRobert Jarzmik } 72bda00303SRobert Jarzmik 73bda00303SRobert Jarzmik static struct clk_ops cken_rate_ops = { 74bda00303SRobert Jarzmik .recalc_rate = cken_recalc_rate, 75bda00303SRobert Jarzmik }; 76bda00303SRobert Jarzmik 77bda00303SRobert Jarzmik static u8 cken_get_parent(struct clk_hw *hw) 78bda00303SRobert Jarzmik { 7914dd5b01SRobert Jarzmik struct pxa_clk *pclk = to_pxa_clk(hw); 80bda00303SRobert Jarzmik 81bda00303SRobert Jarzmik if (!pclk->is_in_low_power) 82bda00303SRobert Jarzmik return 0; 83bda00303SRobert Jarzmik return pclk->is_in_low_power() ? 0 : 1; 84bda00303SRobert Jarzmik } 85bda00303SRobert Jarzmik 86bda00303SRobert Jarzmik static struct clk_ops cken_mux_ops = { 87bda00303SRobert Jarzmik .get_parent = cken_get_parent, 88bda00303SRobert Jarzmik .set_parent = dummy_clk_set_parent, 89bda00303SRobert Jarzmik }; 90bda00303SRobert Jarzmik 91bda00303SRobert Jarzmik void __init clkdev_pxa_register(int ckid, const char *con_id, 92bda00303SRobert Jarzmik const char *dev_id, struct clk *clk) 93bda00303SRobert Jarzmik { 94bda00303SRobert Jarzmik if (!IS_ERR(clk) && (ckid != CLK_NONE)) 95bda00303SRobert Jarzmik pxa_clocks[ckid] = clk; 96bda00303SRobert Jarzmik if (!IS_ERR(clk)) 97bda00303SRobert Jarzmik clk_register_clkdev(clk, con_id, dev_id); 98bda00303SRobert Jarzmik } 99bda00303SRobert Jarzmik 10014dd5b01SRobert Jarzmik int __init clk_pxa_cken_init(const struct desc_clk_cken *clks, int nb_clks) 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); 10814dd5b01SRobert Jarzmik pxa_clk->is_in_low_power = clks[i].is_in_low_power; 10914dd5b01SRobert Jarzmik pxa_clk->lp = clks[i].lp; 11014dd5b01SRobert Jarzmik pxa_clk->hp = clks[i].hp; 11114dd5b01SRobert Jarzmik pxa_clk->gate = clks[i].gate; 112*84558ff7SStephen Boyd pxa_clk->gate.lock = &pxa_clk_lock; 11314dd5b01SRobert Jarzmik clk = clk_register_composite(NULL, clks[i].name, 11414dd5b01SRobert Jarzmik clks[i].parent_names, 2, 11514dd5b01SRobert Jarzmik &pxa_clk->hw, &cken_mux_ops, 11614dd5b01SRobert Jarzmik &pxa_clk->hw, &cken_rate_ops, 11714dd5b01SRobert Jarzmik &pxa_clk->gate.hw, &clk_gate_ops, 11814dd5b01SRobert Jarzmik clks[i].flags); 11914dd5b01SRobert Jarzmik clkdev_pxa_register(clks[i].ckid, clks[i].con_id, 12014dd5b01SRobert Jarzmik clks[i].dev_id, clk); 121bda00303SRobert Jarzmik } 122bda00303SRobert Jarzmik return 0; 123bda00303SRobert Jarzmik } 124bda00303SRobert Jarzmik 1256f8a444aSRobert Jarzmik void __init clk_pxa_dt_common_init(struct device_node *np) 126bda00303SRobert Jarzmik { 127bda00303SRobert Jarzmik of_clk_add_provider(np, of_clk_src_onecell_get, &onecell_data); 128bda00303SRobert Jarzmik } 1299fe69429SRobert Jarzmik 1309fe69429SRobert Jarzmik void pxa2xx_core_turbo_switch(bool on) 1319fe69429SRobert Jarzmik { 1329fe69429SRobert Jarzmik unsigned long flags; 1339fe69429SRobert Jarzmik unsigned int unused, clkcfg; 1349fe69429SRobert Jarzmik 1359fe69429SRobert Jarzmik local_irq_save(flags); 1369fe69429SRobert Jarzmik 1379fe69429SRobert Jarzmik asm("mrc p14, 0, %0, c6, c0, 0" : "=r" (clkcfg)); 1389fe69429SRobert Jarzmik clkcfg &= ~CLKCFG_TURBO & ~CLKCFG_HALFTURBO; 1399fe69429SRobert Jarzmik if (on) 1409fe69429SRobert Jarzmik clkcfg |= CLKCFG_TURBO; 1419fe69429SRobert Jarzmik clkcfg |= CLKCFG_FCS; 1429fe69429SRobert Jarzmik 1439fe69429SRobert Jarzmik asm volatile( 1449fe69429SRobert Jarzmik " b 2f\n" 1459fe69429SRobert Jarzmik " .align 5\n" 1469fe69429SRobert Jarzmik "1: mcr p14, 0, %1, c6, c0, 0\n" 1479fe69429SRobert Jarzmik " b 3f\n" 1489fe69429SRobert Jarzmik "2: b 1b\n" 1499fe69429SRobert Jarzmik "3: nop\n" 1509fe69429SRobert Jarzmik : "=&r" (unused) 1519fe69429SRobert Jarzmik : "r" (clkcfg) 1529fe69429SRobert Jarzmik : ); 1539fe69429SRobert Jarzmik 1549fe69429SRobert Jarzmik local_irq_restore(flags); 1559fe69429SRobert Jarzmik } 1569fe69429SRobert Jarzmik 1579fe69429SRobert Jarzmik void pxa2xx_cpll_change(struct pxa2xx_freq *freq, 158*84558ff7SStephen Boyd u32 (*mdrefr_dri)(unsigned int), void __iomem *mdrefr, 159*84558ff7SStephen Boyd void __iomem *cccr) 1609fe69429SRobert Jarzmik { 1619fe69429SRobert Jarzmik unsigned int clkcfg = freq->clkcfg; 1629fe69429SRobert Jarzmik unsigned int unused, preset_mdrefr, postset_mdrefr; 1639fe69429SRobert Jarzmik unsigned long flags; 1649fe69429SRobert Jarzmik 1659fe69429SRobert Jarzmik local_irq_save(flags); 1669fe69429SRobert Jarzmik 1679fe69429SRobert Jarzmik /* Calculate the next MDREFR. If we're slowing down the SDRAM clock 1689fe69429SRobert Jarzmik * we need to preset the smaller DRI before the change. If we're 1699fe69429SRobert Jarzmik * speeding up we need to set the larger DRI value after the change. 1709fe69429SRobert Jarzmik */ 1719fe69429SRobert Jarzmik preset_mdrefr = postset_mdrefr = readl(mdrefr); 1729fe69429SRobert Jarzmik if ((preset_mdrefr & MDREFR_DRI_MASK) > mdrefr_dri(freq->membus_khz)) { 1739fe69429SRobert Jarzmik preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK); 1749fe69429SRobert Jarzmik preset_mdrefr |= mdrefr_dri(freq->membus_khz); 1759fe69429SRobert Jarzmik } 1769fe69429SRobert Jarzmik postset_mdrefr = 1779fe69429SRobert Jarzmik (postset_mdrefr & ~MDREFR_DRI_MASK) | 1789fe69429SRobert Jarzmik mdrefr_dri(freq->membus_khz); 1799fe69429SRobert Jarzmik 1809fe69429SRobert Jarzmik /* If we're dividing the memory clock by two for the SDRAM clock, this 1819fe69429SRobert Jarzmik * must be set prior to the change. Clearing the divide must be done 1829fe69429SRobert Jarzmik * after the change. 1839fe69429SRobert Jarzmik */ 1849fe69429SRobert Jarzmik if (freq->div2) { 1859fe69429SRobert Jarzmik preset_mdrefr |= MDREFR_DB2_MASK; 1869fe69429SRobert Jarzmik postset_mdrefr |= MDREFR_DB2_MASK; 1879fe69429SRobert Jarzmik } else { 1889fe69429SRobert Jarzmik postset_mdrefr &= ~MDREFR_DB2_MASK; 1899fe69429SRobert Jarzmik } 1909fe69429SRobert Jarzmik 1919fe69429SRobert Jarzmik /* Set new the CCCR and prepare CLKCFG */ 1929fe69429SRobert Jarzmik writel(freq->cccr, cccr); 1939fe69429SRobert Jarzmik 1949fe69429SRobert Jarzmik asm volatile( 1959fe69429SRobert Jarzmik " ldr r4, [%1]\n" 1969fe69429SRobert Jarzmik " b 2f\n" 1979fe69429SRobert Jarzmik " .align 5\n" 1989fe69429SRobert Jarzmik "1: str %3, [%1] /* preset the MDREFR */\n" 1999fe69429SRobert Jarzmik " mcr p14, 0, %2, c6, c0, 0 /* set CLKCFG[FCS] */\n" 2009fe69429SRobert Jarzmik " str %4, [%1] /* postset the MDREFR */\n" 2019fe69429SRobert Jarzmik " b 3f\n" 2029fe69429SRobert Jarzmik "2: b 1b\n" 2039fe69429SRobert Jarzmik "3: nop\n" 2049fe69429SRobert Jarzmik : "=&r" (unused) 2059fe69429SRobert Jarzmik : "r" (mdrefr), "r" (clkcfg), "r" (preset_mdrefr), 2069fe69429SRobert Jarzmik "r" (postset_mdrefr) 2079fe69429SRobert Jarzmik : "r4", "r5"); 2089fe69429SRobert Jarzmik 2099fe69429SRobert Jarzmik local_irq_restore(flags); 2109fe69429SRobert Jarzmik } 2119fe69429SRobert Jarzmik 2129fe69429SRobert Jarzmik int pxa2xx_determine_rate(struct clk_rate_request *req, 2139fe69429SRobert Jarzmik struct pxa2xx_freq *freqs, int nb_freqs) 2149fe69429SRobert Jarzmik { 2152517b32bSArnd Bergmann int i, closest_below = -1, closest_above = -1; 2169fe69429SRobert Jarzmik unsigned long rate; 2179fe69429SRobert Jarzmik 2189fe69429SRobert Jarzmik for (i = 0; i < nb_freqs; i++) { 2199fe69429SRobert Jarzmik rate = freqs[i].cpll; 2209fe69429SRobert Jarzmik if (rate == req->rate) 2219fe69429SRobert Jarzmik break; 2229fe69429SRobert Jarzmik if (rate < req->min_rate) 2239fe69429SRobert Jarzmik continue; 2249fe69429SRobert Jarzmik if (rate > req->max_rate) 2259fe69429SRobert Jarzmik continue; 2269fe69429SRobert Jarzmik if (rate <= req->rate) 2279fe69429SRobert Jarzmik closest_below = i; 2289fe69429SRobert Jarzmik if ((rate >= req->rate) && (closest_above == -1)) 2299fe69429SRobert Jarzmik closest_above = i; 2309fe69429SRobert Jarzmik } 2319fe69429SRobert Jarzmik 2329fe69429SRobert Jarzmik req->best_parent_hw = NULL; 2339fe69429SRobert Jarzmik 2342517b32bSArnd Bergmann if (i < nb_freqs) { 2352517b32bSArnd Bergmann rate = req->rate; 2362517b32bSArnd Bergmann } else if (closest_below >= 0) { 2379fe69429SRobert Jarzmik rate = freqs[closest_below].cpll; 2382517b32bSArnd Bergmann } else if (closest_above >= 0) { 2399fe69429SRobert Jarzmik rate = freqs[closest_above].cpll; 2402517b32bSArnd Bergmann } else { 2412517b32bSArnd Bergmann pr_debug("%s(rate=%lu) no match\n", __func__, req->rate); 2422517b32bSArnd Bergmann return -EINVAL; 2432517b32bSArnd Bergmann } 2449fe69429SRobert Jarzmik 2452517b32bSArnd Bergmann pr_debug("%s(rate=%lu) rate=%lu\n", __func__, req->rate, rate); 2469fe69429SRobert Jarzmik req->rate = rate; 2479fe69429SRobert Jarzmik 2482517b32bSArnd Bergmann return 0; 2499fe69429SRobert Jarzmik } 250