1ff1930c6SPaul Burton /* 2ff1930c6SPaul Burton * Ingenic JZ4740 SoC CGU driver 3ff1930c6SPaul Burton * 4ff1930c6SPaul Burton * Copyright (c) 2015 Imagination Technologies 5fb615d61SPaul Burton * Author: Paul Burton <paul.burton@mips.com> 6ff1930c6SPaul Burton * 7ff1930c6SPaul Burton * This program is free software; you can redistribute it and/or 8ff1930c6SPaul Burton * modify it under the terms of the GNU General Public License as 9ff1930c6SPaul Burton * published by the Free Software Foundation; either version 2 of 10ff1930c6SPaul Burton * the License, or (at your option) any later version. 11ff1930c6SPaul Burton * 12ff1930c6SPaul Burton * This program is distributed in the hope that it will be useful, 13ff1930c6SPaul Burton * but WITHOUT ANY WARRANTY; without even the implied warranty of 14ff1930c6SPaul Burton * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15ff1930c6SPaul Burton * GNU General Public License for more details. 16ff1930c6SPaul Burton */ 17ff1930c6SPaul Burton 18ff1930c6SPaul Burton #include <linux/clk-provider.h> 19ff1930c6SPaul Burton #include <linux/delay.h> 2062e59c4eSStephen Boyd #include <linux/io.h> 21ff1930c6SPaul Burton #include <linux/of.h> 22ff1930c6SPaul Burton #include <dt-bindings/clock/jz4740-cgu.h> 2341dd641eSPaul Burton #include <asm/mach-jz4740/clock.h> 24ff1930c6SPaul Burton #include "cgu.h" 25ff1930c6SPaul Burton 26ff1930c6SPaul Burton /* CGU register offsets */ 27ff1930c6SPaul Burton #define CGU_REG_CPCCR 0x00 2841dd641eSPaul Burton #define CGU_REG_LCR 0x04 29ff1930c6SPaul Burton #define CGU_REG_CPPCR 0x10 30ed286ca5SPaul Burton #define CGU_REG_CLKGR 0x20 31ff1930c6SPaul Burton #define CGU_REG_SCR 0x24 32ff1930c6SPaul Burton #define CGU_REG_I2SCDR 0x60 33ff1930c6SPaul Burton #define CGU_REG_LPCDR 0x64 34ff1930c6SPaul Burton #define CGU_REG_MSCCDR 0x68 35ff1930c6SPaul Burton #define CGU_REG_UHCCDR 0x6c 36ff1930c6SPaul Burton #define CGU_REG_SSICDR 0x74 37ff1930c6SPaul Burton 38ff1930c6SPaul Burton /* bits within a PLL control register */ 39ff1930c6SPaul Burton #define PLLCTL_M_SHIFT 23 40ff1930c6SPaul Burton #define PLLCTL_M_MASK (0x1ff << PLLCTL_M_SHIFT) 41ff1930c6SPaul Burton #define PLLCTL_N_SHIFT 18 42ff1930c6SPaul Burton #define PLLCTL_N_MASK (0x1f << PLLCTL_N_SHIFT) 43ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT 16 44ff1930c6SPaul Burton #define PLLCTL_OD_MASK (0x3 << PLLCTL_OD_SHIFT) 45ff1930c6SPaul Burton #define PLLCTL_STABLE (1 << 10) 46ff1930c6SPaul Burton #define PLLCTL_BYPASS (1 << 9) 47ff1930c6SPaul Burton #define PLLCTL_ENABLE (1 << 8) 48ff1930c6SPaul Burton 4941dd641eSPaul Burton /* bits within the LCR register */ 5041dd641eSPaul Burton #define LCR_SLEEP (1 << 0) 5141dd641eSPaul Burton 52ed286ca5SPaul Burton /* bits within the CLKGR register */ 53ed286ca5SPaul Burton #define CLKGR_UDC (1 << 11) 54ed286ca5SPaul Burton 55ff1930c6SPaul Burton static struct ingenic_cgu *cgu; 56ff1930c6SPaul Burton 57ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = { 58ff1930c6SPaul Burton 0x0, 0x1, -1, 0x3, 59ff1930c6SPaul Burton }; 60ff1930c6SPaul Burton 61*2a1a7036SPaul Cercueil static const u8 jz4740_cgu_cpccr_div_table[] = { 62*2a1a7036SPaul Cercueil 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 63*2a1a7036SPaul Cercueil }; 64*2a1a7036SPaul Cercueil 65ff1930c6SPaul Burton static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = { 66ff1930c6SPaul Burton 67ff1930c6SPaul Burton /* External clocks */ 68ff1930c6SPaul Burton 69ff1930c6SPaul Burton [JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT }, 70ff1930c6SPaul Burton [JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT }, 71ff1930c6SPaul Burton 72ff1930c6SPaul Burton [JZ4740_CLK_PLL] = { 73ff1930c6SPaul Burton "pll", CGU_CLK_PLL, 74ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 75ff1930c6SPaul Burton .pll = { 76ff1930c6SPaul Burton .reg = CGU_REG_CPPCR, 77ff1930c6SPaul Burton .m_shift = 23, 78ff1930c6SPaul Burton .m_bits = 9, 79ff1930c6SPaul Burton .m_offset = 2, 80ff1930c6SPaul Burton .n_shift = 18, 81ff1930c6SPaul Burton .n_bits = 5, 82ff1930c6SPaul Burton .n_offset = 2, 83ff1930c6SPaul Burton .od_shift = 16, 84ff1930c6SPaul Burton .od_bits = 2, 85ff1930c6SPaul Burton .od_max = 4, 86ff1930c6SPaul Burton .od_encoding = pll_od_encoding, 87ff1930c6SPaul Burton .stable_bit = 10, 88ff1930c6SPaul Burton .bypass_bit = 9, 89ff1930c6SPaul Burton .enable_bit = 8, 90ff1930c6SPaul Burton }, 91ff1930c6SPaul Burton }, 92ff1930c6SPaul Burton 93ff1930c6SPaul Burton /* Muxes & dividers */ 94ff1930c6SPaul Burton 95ff1930c6SPaul Burton [JZ4740_CLK_PLL_HALF] = { 96ff1930c6SPaul Burton "pll half", CGU_CLK_DIV, 97ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 984afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1 }, 99ff1930c6SPaul Burton }, 100ff1930c6SPaul Burton 101ff1930c6SPaul Burton [JZ4740_CLK_CCLK] = { 102ff1930c6SPaul Burton "cclk", CGU_CLK_DIV, 103ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 104*2a1a7036SPaul Cercueil .div = { 105*2a1a7036SPaul Cercueil CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1, 106*2a1a7036SPaul Cercueil jz4740_cgu_cpccr_div_table, 107*2a1a7036SPaul Cercueil }, 108ff1930c6SPaul Burton }, 109ff1930c6SPaul Burton 110ff1930c6SPaul Burton [JZ4740_CLK_HCLK] = { 111ff1930c6SPaul Burton "hclk", CGU_CLK_DIV, 112ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 113*2a1a7036SPaul Cercueil .div = { 114*2a1a7036SPaul Cercueil CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1, 115*2a1a7036SPaul Cercueil jz4740_cgu_cpccr_div_table, 116*2a1a7036SPaul Cercueil }, 117ff1930c6SPaul Burton }, 118ff1930c6SPaul Burton 119ff1930c6SPaul Burton [JZ4740_CLK_PCLK] = { 120ff1930c6SPaul Burton "pclk", CGU_CLK_DIV, 121ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 122*2a1a7036SPaul Cercueil .div = { 123*2a1a7036SPaul Cercueil CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1, 124*2a1a7036SPaul Cercueil jz4740_cgu_cpccr_div_table, 125*2a1a7036SPaul Cercueil }, 126ff1930c6SPaul Burton }, 127ff1930c6SPaul Burton 128ff1930c6SPaul Burton [JZ4740_CLK_MCLK] = { 129ff1930c6SPaul Burton "mclk", CGU_CLK_DIV, 130ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 131*2a1a7036SPaul Cercueil .div = { 132*2a1a7036SPaul Cercueil CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1, 133*2a1a7036SPaul Cercueil jz4740_cgu_cpccr_div_table, 134*2a1a7036SPaul Cercueil }, 135ff1930c6SPaul Burton }, 136ff1930c6SPaul Burton 137ff1930c6SPaul Burton [JZ4740_CLK_LCD] = { 138ff1930c6SPaul Burton "lcd", CGU_CLK_DIV | CGU_CLK_GATE, 139ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 140*2a1a7036SPaul Cercueil .div = { 141*2a1a7036SPaul Cercueil CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1, 142*2a1a7036SPaul Cercueil jz4740_cgu_cpccr_div_table, 143*2a1a7036SPaul Cercueil }, 144ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 10 }, 145ff1930c6SPaul Burton }, 146ff1930c6SPaul Burton 147ff1930c6SPaul Burton [JZ4740_CLK_LCD_PCLK] = { 148ff1930c6SPaul Burton "lcd_pclk", CGU_CLK_DIV, 149ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1504afe2d1aSHarvey Hunt .div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 }, 151ff1930c6SPaul Burton }, 152ff1930c6SPaul Burton 153ff1930c6SPaul Burton [JZ4740_CLK_I2S] = { 154ff1930c6SPaul Burton "i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 155ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, 156ff1930c6SPaul Burton .mux = { CGU_REG_CPCCR, 31, 1 }, 157574f4e80SPaul Cercueil .div = { CGU_REG_I2SCDR, 0, 1, 9, -1, -1, -1 }, 158ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 6 }, 159ff1930c6SPaul Burton }, 160ff1930c6SPaul Burton 161ff1930c6SPaul Burton [JZ4740_CLK_SPI] = { 162ff1930c6SPaul Burton "spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 163ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 }, 164ff1930c6SPaul Burton .mux = { CGU_REG_SSICDR, 31, 1 }, 1654afe2d1aSHarvey Hunt .div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 }, 166ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 4 }, 167ff1930c6SPaul Burton }, 168ff1930c6SPaul Burton 169ff1930c6SPaul Burton [JZ4740_CLK_MMC] = { 170ff1930c6SPaul Burton "mmc", CGU_CLK_DIV | CGU_CLK_GATE, 171ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1724afe2d1aSHarvey Hunt .div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 }, 173ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 7 }, 174ff1930c6SPaul Burton }, 175ff1930c6SPaul Burton 176ff1930c6SPaul Burton [JZ4740_CLK_UHC] = { 177ff1930c6SPaul Burton "uhc", CGU_CLK_DIV | CGU_CLK_GATE, 178ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1794afe2d1aSHarvey Hunt .div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 }, 180ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 14 }, 181ff1930c6SPaul Burton }, 182ff1930c6SPaul Burton 183ff1930c6SPaul Burton [JZ4740_CLK_UDC] = { 1842b555a4bSPaul Cercueil "udc", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 185ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, 186ff1930c6SPaul Burton .mux = { CGU_REG_CPCCR, 29, 1 }, 1874afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 }, 188b7e29924SPaul Cercueil .gate = { CGU_REG_SCR, 6, true }, 189ff1930c6SPaul Burton }, 190ff1930c6SPaul Burton 191ff1930c6SPaul Burton /* Gate-only clocks */ 192ff1930c6SPaul Burton 193ff1930c6SPaul Burton [JZ4740_CLK_UART0] = { 194ff1930c6SPaul Burton "uart0", CGU_CLK_GATE, 195ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 196ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 0 }, 197ff1930c6SPaul Burton }, 198ff1930c6SPaul Burton 199ff1930c6SPaul Burton [JZ4740_CLK_UART1] = { 200ff1930c6SPaul Burton "uart1", CGU_CLK_GATE, 201ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 202ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 15 }, 203ff1930c6SPaul Burton }, 204ff1930c6SPaul Burton 205ff1930c6SPaul Burton [JZ4740_CLK_DMA] = { 206ff1930c6SPaul Burton "dma", CGU_CLK_GATE, 207ff1930c6SPaul Burton .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, 208ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 12 }, 209ff1930c6SPaul Burton }, 210ff1930c6SPaul Burton 211ff1930c6SPaul Burton [JZ4740_CLK_IPU] = { 212ff1930c6SPaul Burton "ipu", CGU_CLK_GATE, 213ff1930c6SPaul Burton .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, 214ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 13 }, 215ff1930c6SPaul Burton }, 216ff1930c6SPaul Burton 217ff1930c6SPaul Burton [JZ4740_CLK_ADC] = { 218ff1930c6SPaul Burton "adc", CGU_CLK_GATE, 219ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 220ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 8 }, 221ff1930c6SPaul Burton }, 222ff1930c6SPaul Burton 223ff1930c6SPaul Burton [JZ4740_CLK_I2C] = { 224ff1930c6SPaul Burton "i2c", CGU_CLK_GATE, 225ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 226ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 3 }, 227ff1930c6SPaul Burton }, 228ff1930c6SPaul Burton 229ff1930c6SPaul Burton [JZ4740_CLK_AIC] = { 230ff1930c6SPaul Burton "aic", CGU_CLK_GATE, 231ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 232ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 5 }, 233ff1930c6SPaul Burton }, 234ff1930c6SPaul Burton }; 235ff1930c6SPaul Burton 236ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np) 237ff1930c6SPaul Burton { 238ff1930c6SPaul Burton int retval; 239ff1930c6SPaul Burton 240ff1930c6SPaul Burton cgu = ingenic_cgu_new(jz4740_cgu_clocks, 241ff1930c6SPaul Burton ARRAY_SIZE(jz4740_cgu_clocks), np); 242ff1930c6SPaul Burton if (!cgu) { 243ff1930c6SPaul Burton pr_err("%s: failed to initialise CGU\n", __func__); 244ff1930c6SPaul Burton return; 245ff1930c6SPaul Burton } 246ff1930c6SPaul Burton 247ff1930c6SPaul Burton retval = ingenic_cgu_register_clocks(cgu); 248ff1930c6SPaul Burton if (retval) 249ff1930c6SPaul Burton pr_err("%s: failed to register CGU Clocks\n", __func__); 250ff1930c6SPaul Burton } 251ff1930c6SPaul Burton CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init); 25241dd641eSPaul Burton 25341dd641eSPaul Burton void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode) 25441dd641eSPaul Burton { 25541dd641eSPaul Burton uint32_t lcr = readl(cgu->base + CGU_REG_LCR); 25641dd641eSPaul Burton 25741dd641eSPaul Burton switch (mode) { 25841dd641eSPaul Burton case JZ4740_WAIT_MODE_IDLE: 25941dd641eSPaul Burton lcr &= ~LCR_SLEEP; 26041dd641eSPaul Burton break; 26141dd641eSPaul Burton 26241dd641eSPaul Burton case JZ4740_WAIT_MODE_SLEEP: 26341dd641eSPaul Burton lcr |= LCR_SLEEP; 26441dd641eSPaul Burton break; 26541dd641eSPaul Burton } 26641dd641eSPaul Burton 26741dd641eSPaul Burton writel(lcr, cgu->base + CGU_REG_LCR); 26841dd641eSPaul Burton } 269ed286ca5SPaul Burton 270ed286ca5SPaul Burton void jz4740_clock_udc_disable_auto_suspend(void) 271ed286ca5SPaul Burton { 272ed286ca5SPaul Burton uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR); 273ed286ca5SPaul Burton 274ed286ca5SPaul Burton clkgr &= ~CLKGR_UDC; 275ed286ca5SPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 276ed286ca5SPaul Burton } 277ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_disable_auto_suspend); 278ed286ca5SPaul Burton 279ed286ca5SPaul Burton void jz4740_clock_udc_enable_auto_suspend(void) 280ed286ca5SPaul Burton { 281ed286ca5SPaul Burton uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR); 282ed286ca5SPaul Burton 283ed286ca5SPaul Burton clkgr |= CLKGR_UDC; 284ed286ca5SPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 285ed286ca5SPaul Burton } 286ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_enable_auto_suspend); 28750d893ffSPaul Burton 28850d893ffSPaul Burton #define JZ_CLOCK_GATE_UART0 BIT(0) 28950d893ffSPaul Burton #define JZ_CLOCK_GATE_TCU BIT(1) 29050d893ffSPaul Burton #define JZ_CLOCK_GATE_DMAC BIT(12) 29150d893ffSPaul Burton 29250d893ffSPaul Burton void jz4740_clock_suspend(void) 29350d893ffSPaul Burton { 29450d893ffSPaul Burton uint32_t clkgr, cppcr; 29550d893ffSPaul Burton 29650d893ffSPaul Burton clkgr = readl(cgu->base + CGU_REG_CLKGR); 29750d893ffSPaul Burton clkgr |= JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0; 29850d893ffSPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 29950d893ffSPaul Burton 30050d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 30150d893ffSPaul Burton cppcr &= ~BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit); 30250d893ffSPaul Burton writel(cppcr, cgu->base + CGU_REG_CPPCR); 30350d893ffSPaul Burton } 30450d893ffSPaul Burton 30550d893ffSPaul Burton void jz4740_clock_resume(void) 30650d893ffSPaul Burton { 30750d893ffSPaul Burton uint32_t clkgr, cppcr, stable; 30850d893ffSPaul Burton 30950d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 31050d893ffSPaul Burton cppcr |= BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit); 31150d893ffSPaul Burton writel(cppcr, cgu->base + CGU_REG_CPPCR); 31250d893ffSPaul Burton 31350d893ffSPaul Burton stable = BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.stable_bit); 31450d893ffSPaul Burton do { 31550d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 31650d893ffSPaul Burton } while (!(cppcr & stable)); 31750d893ffSPaul Burton 31850d893ffSPaul Burton clkgr = readl(cgu->base + CGU_REG_CLKGR); 31950d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_TCU; 32050d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_DMAC; 32150d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_UART0; 32250d893ffSPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 32350d893ffSPaul Burton } 324