Paul Burton | ff1930c | 2015-05-24 16:11:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Ingenic JZ4740 SoC CGU driver |
| 3 | * |
| 4 | * Copyright (c) 2015 Imagination Technologies |
| 5 | * Author: Paul Burton <paul.burton@imgtec.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/clk-provider.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <dt-bindings/clock/jz4740-cgu.h> |
Paul Burton | 41dd641 | 2015-05-24 16:11:37 +0100 | [diff] [blame^] | 22 | #include <asm/mach-jz4740/clock.h> |
Paul Burton | ff1930c | 2015-05-24 16:11:36 +0100 | [diff] [blame] | 23 | #include "cgu.h" |
| 24 | |
| 25 | /* CGU register offsets */ |
| 26 | #define CGU_REG_CPCCR 0x00 |
Paul Burton | 41dd641 | 2015-05-24 16:11:37 +0100 | [diff] [blame^] | 27 | #define CGU_REG_LCR 0x04 |
Paul Burton | ff1930c | 2015-05-24 16:11:36 +0100 | [diff] [blame] | 28 | #define CGU_REG_CPPCR 0x10 |
| 29 | #define CGU_REG_SCR 0x24 |
| 30 | #define CGU_REG_I2SCDR 0x60 |
| 31 | #define CGU_REG_LPCDR 0x64 |
| 32 | #define CGU_REG_MSCCDR 0x68 |
| 33 | #define CGU_REG_UHCCDR 0x6c |
| 34 | #define CGU_REG_SSICDR 0x74 |
| 35 | |
| 36 | /* bits within a PLL control register */ |
| 37 | #define PLLCTL_M_SHIFT 23 |
| 38 | #define PLLCTL_M_MASK (0x1ff << PLLCTL_M_SHIFT) |
| 39 | #define PLLCTL_N_SHIFT 18 |
| 40 | #define PLLCTL_N_MASK (0x1f << PLLCTL_N_SHIFT) |
| 41 | #define PLLCTL_OD_SHIFT 16 |
| 42 | #define PLLCTL_OD_MASK (0x3 << PLLCTL_OD_SHIFT) |
| 43 | #define PLLCTL_STABLE (1 << 10) |
| 44 | #define PLLCTL_BYPASS (1 << 9) |
| 45 | #define PLLCTL_ENABLE (1 << 8) |
| 46 | |
Paul Burton | 41dd641 | 2015-05-24 16:11:37 +0100 | [diff] [blame^] | 47 | /* bits within the LCR register */ |
| 48 | #define LCR_SLEEP (1 << 0) |
| 49 | |
Paul Burton | ff1930c | 2015-05-24 16:11:36 +0100 | [diff] [blame] | 50 | static struct ingenic_cgu *cgu; |
| 51 | |
| 52 | static const s8 pll_od_encoding[4] = { |
| 53 | 0x0, 0x1, -1, 0x3, |
| 54 | }; |
| 55 | |
| 56 | static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = { |
| 57 | |
| 58 | /* External clocks */ |
| 59 | |
| 60 | [JZ4740_CLK_EXT] = { "ext", CGU_CLK_EXT }, |
| 61 | [JZ4740_CLK_RTC] = { "rtc", CGU_CLK_EXT }, |
| 62 | |
| 63 | [JZ4740_CLK_PLL] = { |
| 64 | "pll", CGU_CLK_PLL, |
| 65 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 66 | .pll = { |
| 67 | .reg = CGU_REG_CPPCR, |
| 68 | .m_shift = 23, |
| 69 | .m_bits = 9, |
| 70 | .m_offset = 2, |
| 71 | .n_shift = 18, |
| 72 | .n_bits = 5, |
| 73 | .n_offset = 2, |
| 74 | .od_shift = 16, |
| 75 | .od_bits = 2, |
| 76 | .od_max = 4, |
| 77 | .od_encoding = pll_od_encoding, |
| 78 | .stable_bit = 10, |
| 79 | .bypass_bit = 9, |
| 80 | .enable_bit = 8, |
| 81 | }, |
| 82 | }, |
| 83 | |
| 84 | /* Muxes & dividers */ |
| 85 | |
| 86 | [JZ4740_CLK_PLL_HALF] = { |
| 87 | "pll half", CGU_CLK_DIV, |
| 88 | .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, |
| 89 | .div = { CGU_REG_CPCCR, 21, 1, -1, -1, -1 }, |
| 90 | }, |
| 91 | |
| 92 | [JZ4740_CLK_CCLK] = { |
| 93 | "cclk", CGU_CLK_DIV, |
| 94 | .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, |
| 95 | .div = { CGU_REG_CPCCR, 0, 4, 22, -1, -1 }, |
| 96 | }, |
| 97 | |
| 98 | [JZ4740_CLK_HCLK] = { |
| 99 | "hclk", CGU_CLK_DIV, |
| 100 | .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, |
| 101 | .div = { CGU_REG_CPCCR, 4, 4, 22, -1, -1 }, |
| 102 | }, |
| 103 | |
| 104 | [JZ4740_CLK_PCLK] = { |
| 105 | "pclk", CGU_CLK_DIV, |
| 106 | .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, |
| 107 | .div = { CGU_REG_CPCCR, 8, 4, 22, -1, -1 }, |
| 108 | }, |
| 109 | |
| 110 | [JZ4740_CLK_MCLK] = { |
| 111 | "mclk", CGU_CLK_DIV, |
| 112 | .parents = { JZ4740_CLK_PLL, -1, -1, -1 }, |
| 113 | .div = { CGU_REG_CPCCR, 12, 4, 22, -1, -1 }, |
| 114 | }, |
| 115 | |
| 116 | [JZ4740_CLK_LCD] = { |
| 117 | "lcd", CGU_CLK_DIV | CGU_CLK_GATE, |
| 118 | .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, |
| 119 | .div = { CGU_REG_CPCCR, 16, 5, 22, -1, -1 }, |
| 120 | .gate = { CGU_REG_CLKGR, 10 }, |
| 121 | }, |
| 122 | |
| 123 | [JZ4740_CLK_LCD_PCLK] = { |
| 124 | "lcd_pclk", CGU_CLK_DIV, |
| 125 | .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, |
| 126 | .div = { CGU_REG_LPCDR, 0, 11, -1, -1, -1 }, |
| 127 | }, |
| 128 | |
| 129 | [JZ4740_CLK_I2S] = { |
| 130 | "i2s", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, |
| 131 | .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, |
| 132 | .mux = { CGU_REG_CPCCR, 31, 1 }, |
| 133 | .div = { CGU_REG_I2SCDR, 0, 8, -1, -1, -1 }, |
| 134 | .gate = { CGU_REG_CLKGR, 6 }, |
| 135 | }, |
| 136 | |
| 137 | [JZ4740_CLK_SPI] = { |
| 138 | "spi", CGU_CLK_MUX | CGU_CLK_DIV | CGU_CLK_GATE, |
| 139 | .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL, -1, -1 }, |
| 140 | .mux = { CGU_REG_SSICDR, 31, 1 }, |
| 141 | .div = { CGU_REG_SSICDR, 0, 4, -1, -1, -1 }, |
| 142 | .gate = { CGU_REG_CLKGR, 4 }, |
| 143 | }, |
| 144 | |
| 145 | [JZ4740_CLK_MMC] = { |
| 146 | "mmc", CGU_CLK_DIV | CGU_CLK_GATE, |
| 147 | .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, |
| 148 | .div = { CGU_REG_MSCCDR, 0, 5, -1, -1, -1 }, |
| 149 | .gate = { CGU_REG_CLKGR, 7 }, |
| 150 | }, |
| 151 | |
| 152 | [JZ4740_CLK_UHC] = { |
| 153 | "uhc", CGU_CLK_DIV | CGU_CLK_GATE, |
| 154 | .parents = { JZ4740_CLK_PLL_HALF, -1, -1, -1 }, |
| 155 | .div = { CGU_REG_UHCCDR, 0, 4, -1, -1, -1 }, |
| 156 | .gate = { CGU_REG_CLKGR, 14 }, |
| 157 | }, |
| 158 | |
| 159 | [JZ4740_CLK_UDC] = { |
| 160 | "udc", CGU_CLK_MUX | CGU_CLK_DIV, |
| 161 | .parents = { JZ4740_CLK_EXT, JZ4740_CLK_PLL_HALF, -1, -1 }, |
| 162 | .mux = { CGU_REG_CPCCR, 29, 1 }, |
| 163 | .div = { CGU_REG_CPCCR, 23, 6, -1, -1, -1 }, |
| 164 | .gate = { CGU_REG_SCR, 6 }, |
| 165 | }, |
| 166 | |
| 167 | /* Gate-only clocks */ |
| 168 | |
| 169 | [JZ4740_CLK_UART0] = { |
| 170 | "uart0", CGU_CLK_GATE, |
| 171 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 172 | .gate = { CGU_REG_CLKGR, 0 }, |
| 173 | }, |
| 174 | |
| 175 | [JZ4740_CLK_UART1] = { |
| 176 | "uart1", CGU_CLK_GATE, |
| 177 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 178 | .gate = { CGU_REG_CLKGR, 15 }, |
| 179 | }, |
| 180 | |
| 181 | [JZ4740_CLK_DMA] = { |
| 182 | "dma", CGU_CLK_GATE, |
| 183 | .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, |
| 184 | .gate = { CGU_REG_CLKGR, 12 }, |
| 185 | }, |
| 186 | |
| 187 | [JZ4740_CLK_IPU] = { |
| 188 | "ipu", CGU_CLK_GATE, |
| 189 | .parents = { JZ4740_CLK_PCLK, -1, -1, -1 }, |
| 190 | .gate = { CGU_REG_CLKGR, 13 }, |
| 191 | }, |
| 192 | |
| 193 | [JZ4740_CLK_ADC] = { |
| 194 | "adc", CGU_CLK_GATE, |
| 195 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 196 | .gate = { CGU_REG_CLKGR, 8 }, |
| 197 | }, |
| 198 | |
| 199 | [JZ4740_CLK_I2C] = { |
| 200 | "i2c", CGU_CLK_GATE, |
| 201 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 202 | .gate = { CGU_REG_CLKGR, 3 }, |
| 203 | }, |
| 204 | |
| 205 | [JZ4740_CLK_AIC] = { |
| 206 | "aic", CGU_CLK_GATE, |
| 207 | .parents = { JZ4740_CLK_EXT, -1, -1, -1 }, |
| 208 | .gate = { CGU_REG_CLKGR, 5 }, |
| 209 | }, |
| 210 | }; |
| 211 | |
| 212 | static void __init jz4740_cgu_init(struct device_node *np) |
| 213 | { |
| 214 | int retval; |
| 215 | |
| 216 | cgu = ingenic_cgu_new(jz4740_cgu_clocks, |
| 217 | ARRAY_SIZE(jz4740_cgu_clocks), np); |
| 218 | if (!cgu) { |
| 219 | pr_err("%s: failed to initialise CGU\n", __func__); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | retval = ingenic_cgu_register_clocks(cgu); |
| 224 | if (retval) |
| 225 | pr_err("%s: failed to register CGU Clocks\n", __func__); |
| 226 | } |
| 227 | CLK_OF_DECLARE(jz4740_cgu, "ingenic,jz4740-cgu", jz4740_cgu_init); |
Paul Burton | 41dd641 | 2015-05-24 16:11:37 +0100 | [diff] [blame^] | 228 | |
| 229 | void jz4740_clock_set_wait_mode(enum jz4740_wait_mode mode) |
| 230 | { |
| 231 | uint32_t lcr = readl(cgu->base + CGU_REG_LCR); |
| 232 | |
| 233 | switch (mode) { |
| 234 | case JZ4740_WAIT_MODE_IDLE: |
| 235 | lcr &= ~LCR_SLEEP; |
| 236 | break; |
| 237 | |
| 238 | case JZ4740_WAIT_MODE_SLEEP: |
| 239 | lcr |= LCR_SLEEP; |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | writel(lcr, cgu->base + CGU_REG_LCR); |
| 244 | } |