Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. |
| 4 | * |
| 5 | * Copyright (C) 2018 SiFive, Inc. |
| 6 | * Wesley Terpstra |
| 7 | * Paul Walmsley |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * The FU540 PRCI implements clock and reset control for the SiFive |
| 19 | * FU540-C000 chip. This driver assumes that it has sole control |
| 20 | * over all PRCI resources. |
| 21 | * |
| 22 | * This driver is based on the PRCI driver written by Wesley Terpstra. |
| 23 | * |
| 24 | * Refer, commit 999529edf517ed75b56659d456d221b2ee56bb60 of: |
| 25 | * https://github.com/riscv/riscv-linux |
| 26 | * |
| 27 | * References: |
| 28 | * - SiFive FU540-C000 manual v1p0, Chapter 7 "Clocking and Reset" |
| 29 | */ |
| 30 | |
Jagan Teki | 72be986 | 2019-05-08 19:52:18 +0530 | [diff] [blame] | 31 | #include <common.h> |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 32 | #include <asm/io.h> |
| 33 | #include <clk-uclass.h> |
| 34 | #include <clk.h> |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 35 | #include <div64.h> |
| 36 | #include <dm.h> |
| 37 | #include <errno.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 38 | #include <linux/err.h> |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 39 | |
| 40 | #include <linux/math64.h> |
Anup Patel | 00a156d | 2019-06-25 06:31:02 +0000 | [diff] [blame] | 41 | #include <linux/clk/analogbits-wrpll-cln28hpc.h> |
Anup Patel | 83d5b50 | 2019-06-25 06:31:15 +0000 | [diff] [blame] | 42 | #include <dt-bindings/clock/sifive-fu540-prci.h> |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 43 | |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 44 | /* |
| 45 | * EXPECTED_CLK_PARENT_COUNT: how many parent clocks this driver expects: |
| 46 | * hfclk and rtcclk |
| 47 | */ |
| 48 | #define EXPECTED_CLK_PARENT_COUNT 2 |
| 49 | |
| 50 | /* |
| 51 | * Register offsets and bitmasks |
| 52 | */ |
| 53 | |
| 54 | /* COREPLLCFG0 */ |
| 55 | #define PRCI_COREPLLCFG0_OFFSET 0x4 |
| 56 | #define PRCI_COREPLLCFG0_DIVR_SHIFT 0 |
| 57 | #define PRCI_COREPLLCFG0_DIVR_MASK (0x3f << PRCI_COREPLLCFG0_DIVR_SHIFT) |
| 58 | #define PRCI_COREPLLCFG0_DIVF_SHIFT 6 |
| 59 | #define PRCI_COREPLLCFG0_DIVF_MASK (0x1ff << PRCI_COREPLLCFG0_DIVF_SHIFT) |
| 60 | #define PRCI_COREPLLCFG0_DIVQ_SHIFT 15 |
| 61 | #define PRCI_COREPLLCFG0_DIVQ_MASK (0x7 << PRCI_COREPLLCFG0_DIVQ_SHIFT) |
| 62 | #define PRCI_COREPLLCFG0_RANGE_SHIFT 18 |
| 63 | #define PRCI_COREPLLCFG0_RANGE_MASK (0x7 << PRCI_COREPLLCFG0_RANGE_SHIFT) |
| 64 | #define PRCI_COREPLLCFG0_BYPASS_SHIFT 24 |
| 65 | #define PRCI_COREPLLCFG0_BYPASS_MASK (0x1 << PRCI_COREPLLCFG0_BYPASS_SHIFT) |
| 66 | #define PRCI_COREPLLCFG0_FSE_SHIFT 25 |
| 67 | #define PRCI_COREPLLCFG0_FSE_MASK (0x1 << PRCI_COREPLLCFG0_FSE_SHIFT) |
| 68 | #define PRCI_COREPLLCFG0_LOCK_SHIFT 31 |
| 69 | #define PRCI_COREPLLCFG0_LOCK_MASK (0x1 << PRCI_COREPLLCFG0_LOCK_SHIFT) |
| 70 | |
| 71 | /* DDRPLLCFG0 */ |
| 72 | #define PRCI_DDRPLLCFG0_OFFSET 0xc |
| 73 | #define PRCI_DDRPLLCFG0_DIVR_SHIFT 0 |
| 74 | #define PRCI_DDRPLLCFG0_DIVR_MASK (0x3f << PRCI_DDRPLLCFG0_DIVR_SHIFT) |
| 75 | #define PRCI_DDRPLLCFG0_DIVF_SHIFT 6 |
| 76 | #define PRCI_DDRPLLCFG0_DIVF_MASK (0x1ff << PRCI_DDRPLLCFG0_DIVF_SHIFT) |
| 77 | #define PRCI_DDRPLLCFG0_DIVQ_SHIFT 15 |
| 78 | #define PRCI_DDRPLLCFG0_DIVQ_MASK (0x7 << PRCI_DDRPLLCFG0_DIVQ_SHIFT) |
| 79 | #define PRCI_DDRPLLCFG0_RANGE_SHIFT 18 |
| 80 | #define PRCI_DDRPLLCFG0_RANGE_MASK (0x7 << PRCI_DDRPLLCFG0_RANGE_SHIFT) |
| 81 | #define PRCI_DDRPLLCFG0_BYPASS_SHIFT 24 |
| 82 | #define PRCI_DDRPLLCFG0_BYPASS_MASK (0x1 << PRCI_DDRPLLCFG0_BYPASS_SHIFT) |
| 83 | #define PRCI_DDRPLLCFG0_FSE_SHIFT 25 |
| 84 | #define PRCI_DDRPLLCFG0_FSE_MASK (0x1 << PRCI_DDRPLLCFG0_FSE_SHIFT) |
| 85 | #define PRCI_DDRPLLCFG0_LOCK_SHIFT 31 |
| 86 | #define PRCI_DDRPLLCFG0_LOCK_MASK (0x1 << PRCI_DDRPLLCFG0_LOCK_SHIFT) |
| 87 | |
| 88 | /* DDRPLLCFG1 */ |
| 89 | #define PRCI_DDRPLLCFG1_OFFSET 0x10 |
| 90 | #define PRCI_DDRPLLCFG1_CKE_SHIFT 24 |
| 91 | #define PRCI_DDRPLLCFG1_CKE_MASK (0x1 << PRCI_DDRPLLCFG1_CKE_SHIFT) |
| 92 | |
| 93 | /* GEMGXLPLLCFG0 */ |
| 94 | #define PRCI_GEMGXLPLLCFG0_OFFSET 0x1c |
| 95 | #define PRCI_GEMGXLPLLCFG0_DIVR_SHIFT 0 |
| 96 | #define PRCI_GEMGXLPLLCFG0_DIVR_MASK \ |
| 97 | (0x3f << PRCI_GEMGXLPLLCFG0_DIVR_SHIFT) |
| 98 | #define PRCI_GEMGXLPLLCFG0_DIVF_SHIFT 6 |
| 99 | #define PRCI_GEMGXLPLLCFG0_DIVF_MASK \ |
| 100 | (0x1ff << PRCI_GEMGXLPLLCFG0_DIVF_SHIFT) |
| 101 | #define PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT 15 |
| 102 | #define PRCI_GEMGXLPLLCFG0_DIVQ_MASK (0x7 << PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT) |
| 103 | #define PRCI_GEMGXLPLLCFG0_RANGE_SHIFT 18 |
| 104 | #define PRCI_GEMGXLPLLCFG0_RANGE_MASK \ |
| 105 | (0x7 << PRCI_GEMGXLPLLCFG0_RANGE_SHIFT) |
| 106 | #define PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT 24 |
| 107 | #define PRCI_GEMGXLPLLCFG0_BYPASS_MASK \ |
| 108 | (0x1 << PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT) |
| 109 | #define PRCI_GEMGXLPLLCFG0_FSE_SHIFT 25 |
| 110 | #define PRCI_GEMGXLPLLCFG0_FSE_MASK \ |
| 111 | (0x1 << PRCI_GEMGXLPLLCFG0_FSE_SHIFT) |
| 112 | #define PRCI_GEMGXLPLLCFG0_LOCK_SHIFT 31 |
| 113 | #define PRCI_GEMGXLPLLCFG0_LOCK_MASK (0x1 << PRCI_GEMGXLPLLCFG0_LOCK_SHIFT) |
| 114 | |
| 115 | /* GEMGXLPLLCFG1 */ |
| 116 | #define PRCI_GEMGXLPLLCFG1_OFFSET 0x20 |
| 117 | #define PRCI_GEMGXLPLLCFG1_CKE_SHIFT 24 |
| 118 | #define PRCI_GEMGXLPLLCFG1_CKE_MASK (0x1 << PRCI_GEMGXLPLLCFG1_CKE_SHIFT) |
| 119 | |
| 120 | /* CORECLKSEL */ |
| 121 | #define PRCI_CORECLKSEL_OFFSET 0x24 |
| 122 | #define PRCI_CORECLKSEL_CORECLKSEL_SHIFT 0 |
| 123 | #define PRCI_CORECLKSEL_CORECLKSEL_MASK \ |
| 124 | (0x1 << PRCI_CORECLKSEL_CORECLKSEL_SHIFT) |
| 125 | |
| 126 | /* DEVICESRESETREG */ |
| 127 | #define PRCI_DEVICESRESETREG_OFFSET 0x28 |
| 128 | #define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT 0 |
| 129 | #define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_MASK \ |
| 130 | (0x1 << PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT) |
| 131 | #define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT 1 |
| 132 | #define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_MASK \ |
| 133 | (0x1 << PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT) |
| 134 | #define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT 2 |
| 135 | #define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_MASK \ |
| 136 | (0x1 << PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT) |
| 137 | #define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT 3 |
| 138 | #define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_MASK \ |
| 139 | (0x1 << PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT) |
| 140 | #define PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT 5 |
| 141 | #define PRCI_DEVICESRESETREG_GEMGXL_RST_N_MASK \ |
| 142 | (0x1 << PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT) |
| 143 | |
| 144 | /* CLKMUXSTATUSREG */ |
| 145 | #define PRCI_CLKMUXSTATUSREG_OFFSET 0x2c |
| 146 | #define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT 1 |
| 147 | #define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK \ |
| 148 | (0x1 << PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT) |
| 149 | |
| 150 | /* |
| 151 | * Private structures |
| 152 | */ |
| 153 | |
| 154 | /** |
| 155 | * struct __prci_data - per-device-instance data |
| 156 | * @va: base virtual address of the PRCI IP block |
| 157 | * @parent: parent clk instance |
| 158 | * |
| 159 | * PRCI per-device instance data |
| 160 | */ |
| 161 | struct __prci_data { |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 162 | void *va; |
| 163 | struct clk parent_hfclk; |
| 164 | struct clk parent_rtcclk; |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * struct __prci_wrpll_data - WRPLL configuration and integration data |
| 169 | * @c: WRPLL current configuration record |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 170 | * @enable_bypass: fn ptr to code to bypass the WRPLL (if applicable; else NULL) |
| 171 | * @disable_bypass: fn ptr to code to not bypass the WRPLL (or NULL) |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 172 | * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address |
| 173 | * |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 174 | * @enable_bypass and @disable_bypass are used for WRPLL instances |
| 175 | * that contain a separate external glitchless clock mux downstream |
| 176 | * from the PLL. The WRPLL internal bypass mux is not glitchless. |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 177 | */ |
| 178 | struct __prci_wrpll_data { |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 179 | struct wrpll_cfg c; |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 180 | void (*enable_bypass)(struct __prci_data *pd); |
| 181 | void (*disable_bypass)(struct __prci_data *pd); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 182 | u8 cfg0_offs; |
| 183 | }; |
| 184 | |
| 185 | struct __prci_clock; |
| 186 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 187 | /* struct __prci_clock_ops - clock operations */ |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 188 | struct __prci_clock_ops { |
| 189 | int (*set_rate)(struct __prci_clock *pc, |
| 190 | unsigned long rate, |
| 191 | unsigned long parent_rate); |
| 192 | unsigned long (*round_rate)(struct __prci_clock *pc, |
| 193 | unsigned long rate, |
| 194 | unsigned long *parent_rate); |
| 195 | unsigned long (*recalc_rate)(struct __prci_clock *pc, |
| 196 | unsigned long parent_rate); |
| 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * struct __prci_clock - describes a clock device managed by PRCI |
| 201 | * @name: user-readable clock name string - should match the manual |
| 202 | * @parent_name: parent name for this clock |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 203 | * @ops: struct __prci_clock_ops for control |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 204 | * @pwd: WRPLL-specific data, associated with this clock (if not NULL) |
| 205 | * @pd: PRCI-specific data associated with this clock (if not NULL) |
| 206 | * |
| 207 | * PRCI clock data. Used by the PRCI driver to register PRCI-provided |
| 208 | * clocks to the Linux clock infrastructure. |
| 209 | */ |
| 210 | struct __prci_clock { |
| 211 | const char *name; |
| 212 | const char *parent_name; |
| 213 | const struct __prci_clock_ops *ops; |
| 214 | struct __prci_wrpll_data *pwd; |
| 215 | struct __prci_data *pd; |
| 216 | }; |
| 217 | |
| 218 | /* |
| 219 | * Private functions |
| 220 | */ |
| 221 | |
| 222 | /** |
| 223 | * __prci_readl() - read from a PRCI register |
| 224 | * @pd: PRCI context |
| 225 | * @offs: register offset to read from (in bytes, from PRCI base address) |
| 226 | * |
| 227 | * Read the register located at offset @offs from the base virtual |
| 228 | * address of the PRCI register target described by @pd, and return |
| 229 | * the value to the caller. |
| 230 | * |
| 231 | * Context: Any context. |
| 232 | * |
| 233 | * Return: the contents of the register described by @pd and @offs. |
| 234 | */ |
| 235 | static u32 __prci_readl(struct __prci_data *pd, u32 offs) |
| 236 | { |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 237 | return readl(pd->va + offs); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd) |
| 241 | { |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 242 | writel(v, pd->va + offs); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* WRPLL-related private functions */ |
| 246 | |
| 247 | /** |
| 248 | * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 249 | * @c: ptr to a struct wrpll_cfg record to write config into |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 250 | * @r: value read from the PRCI PLL configuration register |
| 251 | * |
| 252 | * Given a value @r read from an FU540 PRCI PLL configuration register, |
| 253 | * split it into fields and populate it into the WRPLL configuration record |
| 254 | * pointed to by @c. |
| 255 | * |
| 256 | * The COREPLLCFG0 macros are used below, but the other *PLLCFG0 macros |
| 257 | * have the same register layout. |
| 258 | * |
| 259 | * Context: Any context. |
| 260 | */ |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 261 | static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r) |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 262 | { |
| 263 | u32 v; |
| 264 | |
| 265 | v = r & PRCI_COREPLLCFG0_DIVR_MASK; |
| 266 | v >>= PRCI_COREPLLCFG0_DIVR_SHIFT; |
| 267 | c->divr = v; |
| 268 | |
| 269 | v = r & PRCI_COREPLLCFG0_DIVF_MASK; |
| 270 | v >>= PRCI_COREPLLCFG0_DIVF_SHIFT; |
| 271 | c->divf = v; |
| 272 | |
| 273 | v = r & PRCI_COREPLLCFG0_DIVQ_MASK; |
| 274 | v >>= PRCI_COREPLLCFG0_DIVQ_SHIFT; |
| 275 | c->divq = v; |
| 276 | |
| 277 | v = r & PRCI_COREPLLCFG0_RANGE_MASK; |
| 278 | v >>= PRCI_COREPLLCFG0_RANGE_SHIFT; |
| 279 | c->range = v; |
| 280 | |
| 281 | c->flags &= (WRPLL_FLAGS_INT_FEEDBACK_MASK | |
| 282 | WRPLL_FLAGS_EXT_FEEDBACK_MASK); |
| 283 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 284 | /* external feedback mode not supported */ |
| 285 | c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK; |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * __prci_wrpll_pack() - pack PLL configuration parameters into a register value |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 290 | * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 291 | * |
| 292 | * Using a set of WRPLL configuration values pointed to by @c, |
| 293 | * assemble a PRCI PLL configuration register value, and return it to |
| 294 | * the caller. |
| 295 | * |
| 296 | * Context: Any context. Caller must ensure that the contents of the |
| 297 | * record pointed to by @c do not change during the execution |
| 298 | * of this function. |
| 299 | * |
| 300 | * Returns: a value suitable for writing into a PRCI PLL configuration |
| 301 | * register |
| 302 | */ |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 303 | static u32 __prci_wrpll_pack(const struct wrpll_cfg *c) |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 304 | { |
| 305 | u32 r = 0; |
| 306 | |
| 307 | r |= c->divr << PRCI_COREPLLCFG0_DIVR_SHIFT; |
| 308 | r |= c->divf << PRCI_COREPLLCFG0_DIVF_SHIFT; |
| 309 | r |= c->divq << PRCI_COREPLLCFG0_DIVQ_SHIFT; |
| 310 | r |= c->range << PRCI_COREPLLCFG0_RANGE_SHIFT; |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 311 | |
| 312 | /* external feedback mode not supported */ |
| 313 | r |= PRCI_COREPLLCFG0_FSE_MASK; |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 314 | |
| 315 | return r; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * __prci_wrpll_read_cfg() - read the WRPLL configuration from the PRCI |
| 320 | * @pd: PRCI context |
| 321 | * @pwd: PRCI WRPLL metadata |
| 322 | * |
| 323 | * Read the current configuration of the PLL identified by @pwd from |
| 324 | * the PRCI identified by @pd, and store it into the local configuration |
| 325 | * cache in @pwd. |
| 326 | * |
| 327 | * Context: Any context. Caller must prevent the records pointed to by |
| 328 | * @pd and @pwd from changing during execution. |
| 329 | */ |
| 330 | static void __prci_wrpll_read_cfg(struct __prci_data *pd, |
| 331 | struct __prci_wrpll_data *pwd) |
| 332 | { |
| 333 | __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs)); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * __prci_wrpll_write_cfg() - write WRPLL configuration into the PRCI |
| 338 | * @pd: PRCI context |
| 339 | * @pwd: PRCI WRPLL metadata |
| 340 | * @c: WRPLL configuration record to write |
| 341 | * |
| 342 | * Write the WRPLL configuration described by @c into the WRPLL |
| 343 | * configuration register identified by @pwd in the PRCI instance |
| 344 | * described by @c. Make a cached copy of the WRPLL's current |
| 345 | * configuration so it can be used by other code. |
| 346 | * |
| 347 | * Context: Any context. Caller must prevent the records pointed to by |
| 348 | * @pd and @pwd from changing during execution. |
| 349 | */ |
| 350 | static void __prci_wrpll_write_cfg(struct __prci_data *pd, |
| 351 | struct __prci_wrpll_data *pwd, |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 352 | struct wrpll_cfg *c) |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 353 | { |
| 354 | __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd); |
| 355 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 356 | memcpy(&pwd->c, c, sizeof(*c)); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* Core clock mux control */ |
| 360 | |
| 361 | /** |
| 362 | * __prci_coreclksel_use_hfclk() - switch the CORECLK mux to output HFCLK |
| 363 | * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg |
| 364 | * |
| 365 | * Switch the CORECLK mux to the HFCLK input source; return once complete. |
| 366 | * |
| 367 | * Context: Any context. Caller must prevent concurrent changes to the |
| 368 | * PRCI_CORECLKSEL_OFFSET register. |
| 369 | */ |
| 370 | static void __prci_coreclksel_use_hfclk(struct __prci_data *pd) |
| 371 | { |
| 372 | u32 r; |
| 373 | |
| 374 | r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); |
| 375 | r |= PRCI_CORECLKSEL_CORECLKSEL_MASK; |
| 376 | __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd); |
| 377 | |
| 378 | r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */ |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * __prci_coreclksel_use_corepll() - switch the CORECLK mux to output COREPLL |
| 383 | * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg |
| 384 | * |
| 385 | * Switch the CORECLK mux to the PLL output clock; return once complete. |
| 386 | * |
| 387 | * Context: Any context. Caller must prevent concurrent changes to the |
| 388 | * PRCI_CORECLKSEL_OFFSET register. |
| 389 | */ |
| 390 | static void __prci_coreclksel_use_corepll(struct __prci_data *pd) |
| 391 | { |
| 392 | u32 r; |
| 393 | |
| 394 | r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); |
| 395 | r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK; |
| 396 | __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd); |
| 397 | |
| 398 | r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */ |
| 399 | } |
| 400 | |
| 401 | static unsigned long sifive_fu540_prci_wrpll_recalc_rate( |
| 402 | struct __prci_clock *pc, |
| 403 | unsigned long parent_rate) |
| 404 | { |
| 405 | struct __prci_wrpll_data *pwd = pc->pwd; |
| 406 | |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 407 | return wrpll_calc_output_rate(&pwd->c, parent_rate); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static unsigned long sifive_fu540_prci_wrpll_round_rate( |
| 411 | struct __prci_clock *pc, |
| 412 | unsigned long rate, |
| 413 | unsigned long *parent_rate) |
| 414 | { |
| 415 | struct __prci_wrpll_data *pwd = pc->pwd; |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 416 | struct wrpll_cfg c; |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 417 | |
| 418 | memcpy(&c, &pwd->c, sizeof(c)); |
| 419 | |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 420 | wrpll_configure_for_rate(&c, rate, *parent_rate); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 421 | |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 422 | return wrpll_calc_output_rate(&c, *parent_rate); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static int sifive_fu540_prci_wrpll_set_rate(struct __prci_clock *pc, |
| 426 | unsigned long rate, |
| 427 | unsigned long parent_rate) |
| 428 | { |
| 429 | struct __prci_wrpll_data *pwd = pc->pwd; |
| 430 | struct __prci_data *pd = pc->pd; |
| 431 | int r; |
| 432 | |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 433 | r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 434 | if (r) |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 435 | return r; |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 436 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 437 | if (pwd->enable_bypass) |
| 438 | pwd->enable_bypass(pd); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 439 | |
| 440 | __prci_wrpll_write_cfg(pd, pwd, &pwd->c); |
| 441 | |
Anup Patel | 6f7b5a2 | 2019-06-25 06:31:08 +0000 | [diff] [blame] | 442 | udelay(wrpll_calc_max_lock_us(&pwd->c)); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 443 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 444 | if (pwd->disable_bypass) |
| 445 | pwd->disable_bypass(pd); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static const struct __prci_clock_ops sifive_fu540_prci_wrpll_clk_ops = { |
| 451 | .set_rate = sifive_fu540_prci_wrpll_set_rate, |
| 452 | .round_rate = sifive_fu540_prci_wrpll_round_rate, |
| 453 | .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate, |
| 454 | }; |
| 455 | |
| 456 | static const struct __prci_clock_ops sifive_fu540_prci_wrpll_ro_clk_ops = { |
| 457 | .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate, |
| 458 | }; |
| 459 | |
| 460 | /* TLCLKSEL clock integration */ |
| 461 | |
| 462 | static unsigned long sifive_fu540_prci_tlclksel_recalc_rate( |
| 463 | struct __prci_clock *pc, |
| 464 | unsigned long parent_rate) |
| 465 | { |
| 466 | struct __prci_data *pd = pc->pd; |
| 467 | u32 v; |
| 468 | u8 div; |
| 469 | |
| 470 | v = __prci_readl(pd, PRCI_CLKMUXSTATUSREG_OFFSET); |
| 471 | v &= PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK; |
| 472 | div = v ? 1 : 2; |
| 473 | |
| 474 | return div_u64(parent_rate, div); |
| 475 | } |
| 476 | |
| 477 | static const struct __prci_clock_ops sifive_fu540_prci_tlclksel_clk_ops = { |
| 478 | .recalc_rate = sifive_fu540_prci_tlclksel_recalc_rate, |
| 479 | }; |
| 480 | |
| 481 | /* |
| 482 | * PRCI integration data for each WRPLL instance |
| 483 | */ |
| 484 | |
| 485 | static struct __prci_wrpll_data __prci_corepll_data = { |
| 486 | .cfg0_offs = PRCI_COREPLLCFG0_OFFSET, |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 487 | .enable_bypass = __prci_coreclksel_use_hfclk, |
| 488 | .disable_bypass = __prci_coreclksel_use_corepll, |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | static struct __prci_wrpll_data __prci_ddrpll_data = { |
| 492 | .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET, |
| 493 | }; |
| 494 | |
| 495 | static struct __prci_wrpll_data __prci_gemgxlpll_data = { |
| 496 | .cfg0_offs = PRCI_GEMGXLPLLCFG0_OFFSET, |
| 497 | }; |
| 498 | |
| 499 | /* |
| 500 | * List of clock controls provided by the PRCI |
| 501 | */ |
| 502 | |
| 503 | static struct __prci_clock __prci_init_clocks[] = { |
| 504 | [PRCI_CLK_COREPLL] = { |
| 505 | .name = "corepll", |
| 506 | .parent_name = "hfclk", |
| 507 | .ops = &sifive_fu540_prci_wrpll_clk_ops, |
| 508 | .pwd = &__prci_corepll_data, |
| 509 | }, |
| 510 | [PRCI_CLK_DDRPLL] = { |
| 511 | .name = "ddrpll", |
| 512 | .parent_name = "hfclk", |
| 513 | .ops = &sifive_fu540_prci_wrpll_ro_clk_ops, |
| 514 | .pwd = &__prci_ddrpll_data, |
| 515 | }, |
| 516 | [PRCI_CLK_GEMGXLPLL] = { |
| 517 | .name = "gemgxlpll", |
| 518 | .parent_name = "hfclk", |
| 519 | .ops = &sifive_fu540_prci_wrpll_clk_ops, |
| 520 | .pwd = &__prci_gemgxlpll_data, |
| 521 | }, |
| 522 | [PRCI_CLK_TLCLK] = { |
| 523 | .name = "tlclk", |
| 524 | .parent_name = "corepll", |
| 525 | .ops = &sifive_fu540_prci_tlclksel_clk_ops, |
| 526 | }, |
| 527 | }; |
| 528 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 529 | static ulong sifive_fu540_prci_parent_rate(struct __prci_clock *pc) |
| 530 | { |
| 531 | ulong parent_rate; |
| 532 | struct __prci_clock *p; |
| 533 | |
| 534 | if (strcmp(pc->parent_name, "corepll") == 0) { |
| 535 | p = &__prci_init_clocks[PRCI_CLK_COREPLL]; |
| 536 | if (!p->pd || !p->ops->recalc_rate) |
| 537 | return -ENXIO; |
| 538 | |
| 539 | return p->ops->recalc_rate(p, sifive_fu540_prci_parent_rate(p)); |
| 540 | } |
| 541 | |
| 542 | if (strcmp(pc->parent_name, "rtcclk") == 0) |
| 543 | parent_rate = clk_get_rate(&pc->pd->parent_rtcclk); |
| 544 | else |
| 545 | parent_rate = clk_get_rate(&pc->pd->parent_hfclk); |
| 546 | |
| 547 | return parent_rate; |
| 548 | } |
| 549 | |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 550 | static ulong sifive_fu540_prci_get_rate(struct clk *clk) |
| 551 | { |
| 552 | struct __prci_clock *pc; |
| 553 | |
| 554 | if (ARRAY_SIZE(__prci_init_clocks) <= clk->id) |
| 555 | return -ENXIO; |
| 556 | |
| 557 | pc = &__prci_init_clocks[clk->id]; |
| 558 | if (!pc->pd || !pc->ops->recalc_rate) |
| 559 | return -ENXIO; |
| 560 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 561 | return pc->ops->recalc_rate(pc, sifive_fu540_prci_parent_rate(pc)); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static ulong sifive_fu540_prci_set_rate(struct clk *clk, ulong rate) |
| 565 | { |
| 566 | int err; |
| 567 | struct __prci_clock *pc; |
| 568 | |
| 569 | if (ARRAY_SIZE(__prci_init_clocks) <= clk->id) |
| 570 | return -ENXIO; |
| 571 | |
| 572 | pc = &__prci_init_clocks[clk->id]; |
| 573 | if (!pc->pd || !pc->ops->set_rate) |
| 574 | return -ENXIO; |
| 575 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 576 | err = pc->ops->set_rate(pc, rate, sifive_fu540_prci_parent_rate(pc)); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 577 | if (err) |
| 578 | return err; |
| 579 | |
| 580 | return rate; |
| 581 | } |
| 582 | |
| 583 | static int sifive_fu540_prci_probe(struct udevice *dev) |
| 584 | { |
| 585 | int i, err; |
| 586 | struct __prci_clock *pc; |
| 587 | struct __prci_data *pd = dev_get_priv(dev); |
| 588 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 589 | pd->va = (void *)dev_read_addr(dev); |
| 590 | if (IS_ERR(pd->va)) |
| 591 | return PTR_ERR(pd->va); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 592 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 593 | err = clk_get_by_index(dev, 0, &pd->parent_hfclk); |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 594 | if (err) |
| 595 | return err; |
| 596 | |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 597 | err = clk_get_by_index(dev, 1, &pd->parent_rtcclk); |
| 598 | if (err) |
| 599 | return err; |
| 600 | |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 601 | for (i = 0; i < ARRAY_SIZE(__prci_init_clocks); ++i) { |
| 602 | pc = &__prci_init_clocks[i]; |
| 603 | pc->pd = pd; |
| 604 | if (pc->pwd) |
| 605 | __prci_wrpll_read_cfg(pd, pc->pwd); |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static struct clk_ops sifive_fu540_prci_ops = { |
| 612 | .set_rate = sifive_fu540_prci_set_rate, |
| 613 | .get_rate = sifive_fu540_prci_get_rate, |
| 614 | }; |
| 615 | |
| 616 | static const struct udevice_id sifive_fu540_prci_ids[] = { |
Anup Patel | 9a99add | 2019-06-25 06:31:21 +0000 | [diff] [blame] | 617 | { .compatible = "sifive,fu540-c000-prci" }, |
Anup Patel | 42fdf08 | 2019-02-25 08:14:49 +0000 | [diff] [blame] | 618 | { } |
| 619 | }; |
| 620 | |
| 621 | U_BOOT_DRIVER(sifive_fu540_prci) = { |
| 622 | .name = "sifive-fu540-prci", |
| 623 | .id = UCLASS_CLK, |
| 624 | .of_match = sifive_fu540_prci_ids, |
| 625 | .probe = sifive_fu540_prci_probe, |
| 626 | .ops = &sifive_fu540_prci_ops, |
| 627 | .priv_auto_alloc_size = sizeof(struct __prci_data), |
| 628 | }; |