1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ff1930c6SPaul Burton /* 3ff1930c6SPaul Burton * Ingenic JZ4740 SoC CGU driver 4ff1930c6SPaul Burton * 5ff1930c6SPaul Burton * Copyright (c) 2015 Imagination Technologies 6fb615d61SPaul Burton * Author: Paul Burton <paul.burton@mips.com> 7ff1930c6SPaul Burton */ 8ff1930c6SPaul Burton 9ff1930c6SPaul Burton #include <linux/clk-provider.h> 10ff1930c6SPaul Burton #include <linux/delay.h> 1162e59c4eSStephen Boyd #include <linux/io.h> 12ff1930c6SPaul Burton #include <linux/of.h> 13ff1930c6SPaul Burton #include <dt-bindings/clock/jz4740-cgu.h> 1441dd641eSPaul Burton #include <asm/mach-jz4740/clock.h> 15ff1930c6SPaul Burton #include "cgu.h" 16ff1930c6SPaul Burton 17ff1930c6SPaul Burton /* CGU register offsets */ 18ff1930c6SPaul Burton #define CGU_REG_CPCCR 0x00 1941dd641eSPaul Burton #define CGU_REG_LCR 0x04 20ff1930c6SPaul Burton #define CGU_REG_CPPCR 0x10 21ed286ca5SPaul Burton #define CGU_REG_CLKGR 0x20 22ff1930c6SPaul Burton #define CGU_REG_SCR 0x24 23ff1930c6SPaul Burton #define CGU_REG_I2SCDR 0x60 24ff1930c6SPaul Burton #define CGU_REG_LPCDR 0x64 25ff1930c6SPaul Burton #define CGU_REG_MSCCDR 0x68 26ff1930c6SPaul Burton #define CGU_REG_UHCCDR 0x6c 27ff1930c6SPaul Burton #define CGU_REG_SSICDR 0x74 28ff1930c6SPaul Burton 29ff1930c6SPaul Burton /* bits within a PLL control register */ 30ff1930c6SPaul Burton #define PLLCTL_M_SHIFT 23 31ff1930c6SPaul Burton #define PLLCTL_M_MASK (0x1ff << PLLCTL_M_SHIFT) 32ff1930c6SPaul Burton #define PLLCTL_N_SHIFT 18 33ff1930c6SPaul Burton #define PLLCTL_N_MASK (0x1f << PLLCTL_N_SHIFT) 34ff1930c6SPaul Burton #define PLLCTL_OD_SHIFT 16 35ff1930c6SPaul Burton #define PLLCTL_OD_MASK (0x3 << PLLCTL_OD_SHIFT) 36ff1930c6SPaul Burton #define PLLCTL_STABLE (1 << 10) 37ff1930c6SPaul Burton #define PLLCTL_BYPASS (1 << 9) 38ff1930c6SPaul Burton #define PLLCTL_ENABLE (1 << 8) 39ff1930c6SPaul Burton 4041dd641eSPaul Burton /* bits within the LCR register */ 4141dd641eSPaul Burton #define LCR_SLEEP (1 << 0) 4241dd641eSPaul Burton 43ed286ca5SPaul Burton /* bits within the CLKGR register */ 44ed286ca5SPaul Burton #define CLKGR_UDC (1 << 11) 45ed286ca5SPaul Burton 46ff1930c6SPaul Burton static struct ingenic_cgu *cgu; 47ff1930c6SPaul Burton 48ff1930c6SPaul Burton static const s8 pll_od_encoding[4] = { 49ff1930c6SPaul Burton 0x0, 0x1, -1, 0x3, 50ff1930c6SPaul Burton }; 51ff1930c6SPaul Burton 52ff1930c6SPaul Burton static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = { 53ff1930c6SPaul Burton 54ff1930c6SPaul Burton /* External clocks */ 55ff1930c6SPaul Burton 56ff1930c6SPaul Burton [JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT }, 57ff1930c6SPaul Burton [JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT }, 58ff1930c6SPaul Burton 59ff1930c6SPaul Burton [JZ4740_CLK_PLL] = { 60ff1930c6SPaul Burton "pll", CGU_CLK_PLL, 61ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 62ff1930c6SPaul Burton .pll = { 63ff1930c6SPaul Burton .reg = CGU_REG_CPPCR, 64ff1930c6SPaul Burton .m_shift = 23, 65ff1930c6SPaul Burton .m_bits = 9, 66ff1930c6SPaul Burton .m_offset = 2, 67ff1930c6SPaul Burton .n_shift = 18, 68ff1930c6SPaul Burton .n_bits = 5, 69ff1930c6SPaul Burton .n_offset = 2, 70ff1930c6SPaul Burton .od_shift = 16, 71ff1930c6SPaul Burton .od_bits = 2, 72ff1930c6SPaul Burton .od_max = 4, 73ff1930c6SPaul Burton .od_encoding = pll_od_encoding, 74ff1930c6SPaul Burton .stable_bit = 10, 75ff1930c6SPaul Burton .bypass_bit = 9, 76ff1930c6SPaul Burton .enable_bit = 8, 77ff1930c6SPaul Burton }, 78ff1930c6SPaul Burton }, 79ff1930c6SPaul Burton 80ff1930c6SPaul Burton /* Muxes & dividers */ 81ff1930c6SPaul Burton 82ff1930c6SPaul Burton [JZ4740_CLK_PLL_HALF] = { 83ff1930c6SPaul Burton "pll half", CGU_CLK_DIV, 84ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 854afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 21, 1, 1, -1, -1, -1 }, 86ff1930c6SPaul Burton }, 87ff1930c6SPaul Burton 88ff1930c6SPaul Burton [JZ4740_CLK_CCLK] = { 89ff1930c6SPaul Burton "cclk", CGU_CLK_DIV, 90ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 914afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 0, 1, 4, 22, -1, -1 }, 92ff1930c6SPaul Burton }, 93ff1930c6SPaul Burton 94ff1930c6SPaul Burton [JZ4740_CLK_HCLK] = { 95ff1930c6SPaul Burton "hclk", CGU_CLK_DIV, 96ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 974afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 4, 1, 4, 22, -1, -1 }, 98ff1930c6SPaul Burton }, 99ff1930c6SPaul Burton 100ff1930c6SPaul Burton [JZ4740_CLK_PCLK] = { 101ff1930c6SPaul Burton "pclk", CGU_CLK_DIV, 102ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 1034afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 8, 1, 4, 22, -1, -1 }, 104ff1930c6SPaul Burton }, 105ff1930c6SPaul Burton 106ff1930c6SPaul Burton [JZ4740_CLK_MCLK] = { 107ff1930c6SPaul Burton "mclk", CGU_CLK_DIV, 108ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, 1094afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 12, 1, 4, 22, -1, -1 }, 110ff1930c6SPaul Burton }, 111ff1930c6SPaul Burton 112ff1930c6SPaul Burton [JZ4740_CLK_LCD] = { 113ff1930c6SPaul Burton "lcd", CGU_CLK_DIV | CGU_CLK_GATE, 114ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1154afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 16, 1, 5, 22, -1, -1 }, 116ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 10 }, 117ff1930c6SPaul Burton }, 118ff1930c6SPaul Burton 119ff1930c6SPaul Burton [JZ4740_CLK_LCD_PCLK] = { 120ff1930c6SPaul Burton "lcd_pclk", CGU_CLK_DIV, 121ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1224afe2d1aSHarvey Hunt .div = { CGU_REG_LPCDR, 0, 1, 11, -1, -1, -1 }, 123ff1930c6SPaul Burton }, 124ff1930c6SPaul Burton 125ff1930c6SPaul Burton [JZ4740_CLK_I2S] = { 126ff1930c6SPaul Burton "i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 127ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, 128ff1930c6SPaul Burton .mux = { CGU_REG_CPCCR, 31, 1 }, 129574f4e80SPaul Cercueil .div = { CGU_REG_I2SCDR, 0, 1, 9, -1, -1, -1 }, 130ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 6 }, 131ff1930c6SPaul Burton }, 132ff1930c6SPaul Burton 133ff1930c6SPaul Burton [JZ4740_CLK_SPI] = { 134ff1930c6SPaul Burton "spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 135ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 }, 136ff1930c6SPaul Burton .mux = { CGU_REG_SSICDR, 31, 1 }, 1374afe2d1aSHarvey Hunt .div = { CGU_REG_SSICDR, 0, 1, 4, -1, -1, -1 }, 138ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 4 }, 139ff1930c6SPaul Burton }, 140ff1930c6SPaul Burton 141ff1930c6SPaul Burton [JZ4740_CLK_MMC] = { 142ff1930c6SPaul Burton "mmc", CGU_CLK_DIV | CGU_CLK_GATE, 143ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1444afe2d1aSHarvey Hunt .div = { CGU_REG_MSCCDR, 0, 1, 5, -1, -1, -1 }, 145ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 7 }, 146ff1930c6SPaul Burton }, 147ff1930c6SPaul Burton 148ff1930c6SPaul Burton [JZ4740_CLK_UHC] = { 149ff1930c6SPaul Burton "uhc", CGU_CLK_DIV | CGU_CLK_GATE, 150ff1930c6SPaul Burton .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, 1514afe2d1aSHarvey Hunt .div = { CGU_REG_UHCCDR, 0, 1, 4, -1, -1, -1 }, 152ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 14 }, 153ff1930c6SPaul Burton }, 154ff1930c6SPaul Burton 155ff1930c6SPaul Burton [JZ4740_CLK_UDC] = { 1562b555a4bSPaul Cercueil "udc", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, 157ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, 158ff1930c6SPaul Burton .mux = { CGU_REG_CPCCR, 29, 1 }, 1594afe2d1aSHarvey Hunt .div = { CGU_REG_CPCCR, 23, 1, 6, -1, -1, -1 }, 160b7e29924SPaul Cercueil .gate = { CGU_REG_SCR, 6, true }, 161ff1930c6SPaul Burton }, 162ff1930c6SPaul Burton 163ff1930c6SPaul Burton /* Gate-only clocks */ 164ff1930c6SPaul Burton 165ff1930c6SPaul Burton [JZ4740_CLK_UART0] = { 166ff1930c6SPaul Burton "uart0", CGU_CLK_GATE, 167ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 168ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 0 }, 169ff1930c6SPaul Burton }, 170ff1930c6SPaul Burton 171ff1930c6SPaul Burton [JZ4740_CLK_UART1] = { 172ff1930c6SPaul Burton "uart1", CGU_CLK_GATE, 173ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 174ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 15 }, 175ff1930c6SPaul Burton }, 176ff1930c6SPaul Burton 177ff1930c6SPaul Burton [JZ4740_CLK_DMA] = { 178ff1930c6SPaul Burton "dma", CGU_CLK_GATE, 179ff1930c6SPaul Burton .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, 180ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 12 }, 181ff1930c6SPaul Burton }, 182ff1930c6SPaul Burton 183ff1930c6SPaul Burton [JZ4740_CLK_IPU] = { 184ff1930c6SPaul Burton "ipu", CGU_CLK_GATE, 185ff1930c6SPaul Burton .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, 186ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 13 }, 187ff1930c6SPaul Burton }, 188ff1930c6SPaul Burton 189ff1930c6SPaul Burton [JZ4740_CLK_ADC] = { 190ff1930c6SPaul Burton "adc", CGU_CLK_GATE, 191ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 192ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 8 }, 193ff1930c6SPaul Burton }, 194ff1930c6SPaul Burton 195ff1930c6SPaul Burton [JZ4740_CLK_I2C] = { 196ff1930c6SPaul Burton "i2c", CGU_CLK_GATE, 197ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 198ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 3 }, 199ff1930c6SPaul Burton }, 200ff1930c6SPaul Burton 201ff1930c6SPaul Burton [JZ4740_CLK_AIC] = { 202ff1930c6SPaul Burton "aic", CGU_CLK_GATE, 203ff1930c6SPaul Burton .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, 204ff1930c6SPaul Burton .gate = { CGU_REG_CLKGR, 5 }, 205ff1930c6SPaul Burton }, 206ff1930c6SPaul Burton }; 207ff1930c6SPaul Burton 208ff1930c6SPaul Burton static void __init jz4740_cgu_init(struct device_node *np) 209ff1930c6SPaul Burton { 210ff1930c6SPaul Burton int retval; 211ff1930c6SPaul Burton 212ff1930c6SPaul Burton cgu = ingenic_cgu_new(jz4740_cgu_clocks, 213ff1930c6SPaul Burton ARRAY_SIZE(jz4740_cgu_clocks), np); 214ff1930c6SPaul Burton if (!cgu) { 215ff1930c6SPaul Burton pr_err("%s: failed to initialise CGU\n", __func__); 216ff1930c6SPaul Burton return; 217ff1930c6SPaul Burton } 218ff1930c6SPaul Burton 219ff1930c6SPaul Burton retval = ingenic_cgu_register_clocks(cgu); 220ff1930c6SPaul Burton if (retval) 221ff1930c6SPaul Burton pr_err("%s: failed to register CGU Clocks\n", __func__); 222ff1930c6SPaul Burton } 223ff1930c6SPaul Burton CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init); 22441dd641eSPaul Burton 22541dd641eSPaul Burton void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode) 22641dd641eSPaul Burton { 22741dd641eSPaul Burton uint32_t lcr = readl(cgu->base + CGU_REG_LCR); 22841dd641eSPaul Burton 22941dd641eSPaul Burton switch (mode) { 23041dd641eSPaul Burton case JZ4740_WAIT_MODE_IDLE: 23141dd641eSPaul Burton lcr &= ~LCR_SLEEP; 23241dd641eSPaul Burton break; 23341dd641eSPaul Burton 23441dd641eSPaul Burton case JZ4740_WAIT_MODE_SLEEP: 23541dd641eSPaul Burton lcr |= LCR_SLEEP; 23641dd641eSPaul Burton break; 23741dd641eSPaul Burton } 23841dd641eSPaul Burton 23941dd641eSPaul Burton writel(lcr, cgu->base + CGU_REG_LCR); 24041dd641eSPaul Burton } 241ed286ca5SPaul Burton 242ed286ca5SPaul Burton void jz4740_clock_udc_disable_auto_suspend(void) 243ed286ca5SPaul Burton { 244ed286ca5SPaul Burton uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR); 245ed286ca5SPaul Burton 246ed286ca5SPaul Burton clkgr &= ~CLKGR_UDC; 247ed286ca5SPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 248ed286ca5SPaul Burton } 249ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_disable_auto_suspend); 250ed286ca5SPaul Burton 251ed286ca5SPaul Burton void jz4740_clock_udc_enable_auto_suspend(void) 252ed286ca5SPaul Burton { 253ed286ca5SPaul Burton uint32_t clkgr = readl(cgu->base + CGU_REG_CLKGR); 254ed286ca5SPaul Burton 255ed286ca5SPaul Burton clkgr |= CLKGR_UDC; 256ed286ca5SPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 257ed286ca5SPaul Burton } 258ed286ca5SPaul Burton EXPORT_SYMBOL_GPL(jz4740_clock_udc_enable_auto_suspend); 25950d893ffSPaul Burton 26050d893ffSPaul Burton #define JZ_CLOCK_GATE_UART0 BIT(0) 26150d893ffSPaul Burton #define JZ_CLOCK_GATE_TCU BIT(1) 26250d893ffSPaul Burton #define JZ_CLOCK_GATE_DMAC BIT(12) 26350d893ffSPaul Burton 26450d893ffSPaul Burton void jz4740_clock_suspend(void) 26550d893ffSPaul Burton { 26650d893ffSPaul Burton uint32_t clkgr, cppcr; 26750d893ffSPaul Burton 26850d893ffSPaul Burton clkgr = readl(cgu->base + CGU_REG_CLKGR); 26950d893ffSPaul Burton clkgr |= JZ_CLOCK_GATE_TCU | JZ_CLOCK_GATE_DMAC | JZ_CLOCK_GATE_UART0; 27050d893ffSPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 27150d893ffSPaul Burton 27250d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 27350d893ffSPaul Burton cppcr &= ~BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit); 27450d893ffSPaul Burton writel(cppcr, cgu->base + CGU_REG_CPPCR); 27550d893ffSPaul Burton } 27650d893ffSPaul Burton 27750d893ffSPaul Burton void jz4740_clock_resume(void) 27850d893ffSPaul Burton { 27950d893ffSPaul Burton uint32_t clkgr, cppcr, stable; 28050d893ffSPaul Burton 28150d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 28250d893ffSPaul Burton cppcr |= BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.enable_bit); 28350d893ffSPaul Burton writel(cppcr, cgu->base + CGU_REG_CPPCR); 28450d893ffSPaul Burton 28550d893ffSPaul Burton stable = BIT(jz4740_cgu_clocks[JZ4740_CLK_PLL].pll.stable_bit); 28650d893ffSPaul Burton do { 28750d893ffSPaul Burton cppcr = readl(cgu->base + CGU_REG_CPPCR); 28850d893ffSPaul Burton } while (!(cppcr & stable)); 28950d893ffSPaul Burton 29050d893ffSPaul Burton clkgr = readl(cgu->base + CGU_REG_CLKGR); 29150d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_TCU; 29250d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_DMAC; 29350d893ffSPaul Burton clkgr &= ~JZ_CLOCK_GATE_UART0; 29450d893ffSPaul Burton writel(clkgr, cgu->base + CGU_REG_CLKGR); 29550d893ffSPaul Burton } 296