Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for the ICST307 VCO clock found in the ARM Reference designs. |
| 4 | * We wrap the custom interface from <asm/hardware/icst.h> into the generic |
| 5 | * clock framework. |
| 6 | * |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 7 | * Copyright (C) 2012-2015 Linus Walleij |
Linus Walleij | 401301c | 2012-11-20 22:39:31 +0100 | [diff] [blame] | 8 | * |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 9 | * TODO: when all ARM reference designs are migrated to generic clocks, the |
| 10 | * ICST clock code from the ARM tree should probably be merged into this |
| 11 | * file. |
| 12 | */ |
Stephen Boyd | 6d31e3b2 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/export.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 16 | #include <linux/err.h> |
| 17 | #include <linux/clk-provider.h> |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 20 | #include <linux/mfd/syscon.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 21 | |
Linus Walleij | ba3fae0 | 2017-02-01 10:41:43 +0100 | [diff] [blame] | 22 | #include "icst.h" |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 23 | #include "clk-icst.h" |
| 24 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 25 | /* Magic unlocking token used on all Versatile boards */ |
| 26 | #define VERSATILE_LOCK_VAL 0xA05F |
| 27 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 28 | #define VERSATILE_AUX_OSC_BITS 0x7FFFF |
| 29 | #define INTEGRATOR_AP_CM_BITS 0xFF |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 30 | #define INTEGRATOR_AP_SYS_BITS 0xFF |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 31 | #define INTEGRATOR_CP_CM_CORE_BITS 0x7FF |
| 32 | #define INTEGRATOR_CP_CM_MEM_BITS 0x7FF000 |
| 33 | |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 34 | #define INTEGRATOR_AP_PCI_25_33_MHZ BIT(8) |
| 35 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 36 | /** |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 37 | * struct clk_icst - ICST VCO clock wrapper |
| 38 | * @hw: corresponding clock hardware entry |
Lee Jones | 0c1d46d | 2021-01-20 09:30:39 +0000 | [diff] [blame] | 39 | * @map: register map |
| 40 | * @vcoreg_off: VCO register address |
| 41 | * @lockreg_off: VCO lock register address |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 42 | * @params: parameters for this ICST instance |
| 43 | * @rate: current rate |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 44 | * @ctype: the type of control register for the ICST |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 45 | */ |
| 46 | struct clk_icst { |
| 47 | struct clk_hw hw; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 48 | struct regmap *map; |
| 49 | u32 vcoreg_off; |
| 50 | u32 lockreg_off; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 51 | struct icst_params *params; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 52 | unsigned long rate; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 53 | enum icst_control_type ctype; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | #define to_icst(_hw) container_of(_hw, struct clk_icst, hw) |
| 57 | |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 58 | /** |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 59 | * vco_get() - get ICST VCO settings from a certain ICST |
| 60 | * @icst: the ICST clock to get |
| 61 | * @vco: the VCO struct to return the value in |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 62 | */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 63 | static int vco_get(struct clk_icst *icst, struct icst_vco *vco) |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 64 | { |
| 65 | u32 val; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 66 | int ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 67 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 68 | ret = regmap_read(icst->map, icst->vcoreg_off, &val); |
| 69 | if (ret) |
| 70 | return ret; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * The Integrator/AP core clock can only access the low eight |
| 74 | * bits of the v PLL divider. Bit 8 is tied low and always zero, |
| 75 | * r is hardwired to 22 and output divider s is hardwired to 1 |
| 76 | * (divide by 2) according to the document |
| 77 | * "Integrator CM926EJ-S, CM946E-S, CM966E-S, CM1026EJ-S and |
| 78 | * CM1136JF-S User Guide" ARM DUI 0138E, page 3-13 thru 3-14. |
| 79 | */ |
| 80 | if (icst->ctype == ICST_INTEGRATOR_AP_CM) { |
| 81 | vco->v = val & INTEGRATOR_AP_CM_BITS; |
| 82 | vco->r = 22; |
| 83 | vco->s = 1; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 88 | * The Integrator/AP system clock on the base board can only |
| 89 | * access the low eight bits of the v PLL divider. Bit 8 is tied low |
| 90 | * and always zero, r is hardwired to 46, and the output divider is |
| 91 | * hardwired to 3 (divide by 4) according to the document |
| 92 | * "Integrator AP ASIC Development Motherboard" ARM DUI 0098B, |
| 93 | * page 3-16. |
| 94 | */ |
| 95 | if (icst->ctype == ICST_INTEGRATOR_AP_SYS) { |
| 96 | vco->v = val & INTEGRATOR_AP_SYS_BITS; |
| 97 | vco->r = 46; |
| 98 | vco->s = 3; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * The Integrator/AP PCI clock is using an odd pattern to create |
| 104 | * the child clock, basically a single bit called DIVX/Y is used |
| 105 | * to select between two different hardwired values: setting the |
| 106 | * bit to 0 yields v = 17, r = 22 and OD = 1, whereas setting the |
| 107 | * bit to 1 yields v = 14, r = 14 and OD = 1 giving the frequencies |
| 108 | * 33 or 25 MHz respectively. |
| 109 | */ |
| 110 | if (icst->ctype == ICST_INTEGRATOR_AP_PCI) { |
| 111 | bool divxy = !!(val & INTEGRATOR_AP_PCI_25_33_MHZ); |
| 112 | |
| 113 | vco->v = divxy ? 17 : 14; |
| 114 | vco->r = divxy ? 22 : 14; |
| 115 | vco->s = 1; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 120 | * The Integrator/CP core clock can access the low eight bits |
| 121 | * of the v PLL divider. Bit 8 is tied low and always zero, |
| 122 | * r is hardwired to 22 and the output divider s is accessible |
| 123 | * in bits 8 thru 10 according to the document |
| 124 | * "Integrator/CM940T, CM920T, CM740T, and CM720T User Guide" |
| 125 | * ARM DUI 0157A, page 3-20 thru 3-23 and 4-10. |
| 126 | */ |
| 127 | if (icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) { |
| 128 | vco->v = val & 0xFF; |
| 129 | vco->r = 22; |
| 130 | vco->s = (val >> 8) & 7; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) { |
| 135 | vco->v = (val >> 12) & 0xFF; |
| 136 | vco->r = 22; |
| 137 | vco->s = (val >> 20) & 7; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 141 | vco->v = val & 0x1ff; |
| 142 | vco->r = (val >> 9) & 0x7f; |
| 143 | vco->s = (val >> 16) & 03; |
| 144 | return 0; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * vco_set() - commit changes to an ICST VCO |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 149 | * @icst: the ICST clock to set |
| 150 | * @vco: the VCO struct to set the changes from |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 151 | */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 152 | static int vco_set(struct clk_icst *icst, struct icst_vco vco) |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 153 | { |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 154 | u32 mask; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 155 | u32 val; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 156 | int ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 157 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 158 | /* Mask the bits used by the VCO */ |
| 159 | switch (icst->ctype) { |
| 160 | case ICST_INTEGRATOR_AP_CM: |
| 161 | mask = INTEGRATOR_AP_CM_BITS; |
| 162 | val = vco.v & 0xFF; |
| 163 | if (vco.v & 0x100) |
| 164 | pr_err("ICST error: tried to set bit 8 of VDW\n"); |
| 165 | if (vco.s != 1) |
| 166 | pr_err("ICST error: tried to use VOD != 1\n"); |
| 167 | if (vco.r != 22) |
| 168 | pr_err("ICST error: tried to use RDW != 22\n"); |
| 169 | break; |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 170 | case ICST_INTEGRATOR_AP_SYS: |
| 171 | mask = INTEGRATOR_AP_SYS_BITS; |
| 172 | val = vco.v & 0xFF; |
| 173 | if (vco.v & 0x100) |
| 174 | pr_err("ICST error: tried to set bit 8 of VDW\n"); |
| 175 | if (vco.s != 3) |
| 176 | pr_err("ICST error: tried to use VOD != 1\n"); |
| 177 | if (vco.r != 46) |
| 178 | pr_err("ICST error: tried to use RDW != 22\n"); |
| 179 | break; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 180 | case ICST_INTEGRATOR_CP_CM_CORE: |
| 181 | mask = INTEGRATOR_CP_CM_CORE_BITS; /* Uses 12 bits */ |
| 182 | val = (vco.v & 0xFF) | vco.s << 8; |
| 183 | if (vco.v & 0x100) |
| 184 | pr_err("ICST error: tried to set bit 8 of VDW\n"); |
| 185 | if (vco.r != 22) |
| 186 | pr_err("ICST error: tried to use RDW != 22\n"); |
| 187 | break; |
| 188 | case ICST_INTEGRATOR_CP_CM_MEM: |
| 189 | mask = INTEGRATOR_CP_CM_MEM_BITS; /* Uses 12 bits */ |
| 190 | val = ((vco.v & 0xFF) << 12) | (vco.s << 20); |
| 191 | if (vco.v & 0x100) |
| 192 | pr_err("ICST error: tried to set bit 8 of VDW\n"); |
| 193 | if (vco.r != 22) |
| 194 | pr_err("ICST error: tried to use RDW != 22\n"); |
| 195 | break; |
| 196 | default: |
| 197 | /* Regular auxilary oscillator */ |
| 198 | mask = VERSATILE_AUX_OSC_BITS; |
| 199 | val = vco.v | (vco.r << 9) | (vco.s << 16); |
| 200 | break; |
| 201 | } |
Linus Walleij | df9cd56 | 2016-02-03 14:47:08 +0100 | [diff] [blame] | 202 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 203 | pr_debug("ICST: new val = 0x%08x\n", val); |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 204 | |
| 205 | /* This magic unlocks the VCO so it can be controlled */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 206 | ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL); |
| 207 | if (ret) |
| 208 | return ret; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 209 | ret = regmap_update_bits(icst->map, icst->vcoreg_off, mask, val); |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 210 | if (ret) |
| 211 | return ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 212 | /* This locks the VCO again */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 213 | ret = regmap_write(icst->map, icst->lockreg_off, 0); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | return 0; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 219 | static unsigned long icst_recalc_rate(struct clk_hw *hw, |
| 220 | unsigned long parent_rate) |
| 221 | { |
| 222 | struct clk_icst *icst = to_icst(hw); |
| 223 | struct icst_vco vco; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 224 | int ret; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 225 | |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 226 | if (parent_rate) |
| 227 | icst->params->ref = parent_rate; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 228 | ret = vco_get(icst, &vco); |
| 229 | if (ret) { |
| 230 | pr_err("ICST: could not get VCO setting\n"); |
| 231 | return 0; |
| 232 | } |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 233 | icst->rate = icst_hz(icst->params, vco); |
| 234 | return icst->rate; |
| 235 | } |
| 236 | |
| 237 | static long icst_round_rate(struct clk_hw *hw, unsigned long rate, |
| 238 | unsigned long *prate) |
| 239 | { |
| 240 | struct clk_icst *icst = to_icst(hw); |
| 241 | struct icst_vco vco; |
| 242 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 243 | if (icst->ctype == ICST_INTEGRATOR_AP_CM || |
| 244 | icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) { |
| 245 | if (rate <= 12000000) |
| 246 | return 12000000; |
| 247 | if (rate >= 160000000) |
| 248 | return 160000000; |
| 249 | /* Slam to closest megahertz */ |
| 250 | return DIV_ROUND_CLOSEST(rate, 1000000) * 1000000; |
| 251 | } |
| 252 | |
| 253 | if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) { |
| 254 | if (rate <= 6000000) |
| 255 | return 6000000; |
| 256 | if (rate >= 66000000) |
| 257 | return 66000000; |
| 258 | /* Slam to closest 0.5 megahertz */ |
| 259 | return DIV_ROUND_CLOSEST(rate, 500000) * 500000; |
| 260 | } |
| 261 | |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 262 | if (icst->ctype == ICST_INTEGRATOR_AP_SYS) { |
| 263 | /* Divides between 3 and 50 MHz in steps of 0.25 MHz */ |
| 264 | if (rate <= 3000000) |
| 265 | return 3000000; |
| 266 | if (rate >= 50000000) |
| 267 | return 5000000; |
| 268 | /* Slam to closest 0.25 MHz */ |
| 269 | return DIV_ROUND_CLOSEST(rate, 250000) * 250000; |
| 270 | } |
| 271 | |
| 272 | if (icst->ctype == ICST_INTEGRATOR_AP_PCI) { |
| 273 | /* |
| 274 | * If we're below or less than halfway from 25 to 33 MHz |
| 275 | * select 25 MHz |
| 276 | */ |
| 277 | if (rate <= 25000000 || rate < 29000000) |
| 278 | return 25000000; |
| 279 | /* Else just return the default frequency */ |
| 280 | return 33000000; |
| 281 | } |
| 282 | |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 283 | vco = icst_hz_to_vco(icst->params, rate); |
| 284 | return icst_hz(icst->params, vco); |
| 285 | } |
| 286 | |
| 287 | static int icst_set_rate(struct clk_hw *hw, unsigned long rate, |
| 288 | unsigned long parent_rate) |
| 289 | { |
| 290 | struct clk_icst *icst = to_icst(hw); |
| 291 | struct icst_vco vco; |
| 292 | |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 293 | if (icst->ctype == ICST_INTEGRATOR_AP_PCI) { |
| 294 | /* This clock is especially primitive */ |
| 295 | unsigned int val; |
| 296 | int ret; |
| 297 | |
| 298 | if (rate == 25000000) { |
| 299 | val = 0; |
| 300 | } else if (rate == 33000000) { |
| 301 | val = INTEGRATOR_AP_PCI_25_33_MHZ; |
| 302 | } else { |
| 303 | pr_err("ICST: cannot set PCI frequency %lu\n", |
| 304 | rate); |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | ret = regmap_write(icst->map, icst->lockreg_off, |
| 308 | VERSATILE_LOCK_VAL); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | ret = regmap_update_bits(icst->map, icst->vcoreg_off, |
| 312 | INTEGRATOR_AP_PCI_25_33_MHZ, |
| 313 | val); |
| 314 | if (ret) |
| 315 | return ret; |
| 316 | /* This locks the VCO again */ |
| 317 | ret = regmap_write(icst->map, icst->lockreg_off, 0); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | return 0; |
| 321 | } |
| 322 | |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 323 | if (parent_rate) |
| 324 | icst->params->ref = parent_rate; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 325 | vco = icst_hz_to_vco(icst->params, rate); |
| 326 | icst->rate = icst_hz(icst->params, vco); |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 327 | return vco_set(icst, vco); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static const struct clk_ops icst_ops = { |
| 331 | .recalc_rate = icst_recalc_rate, |
| 332 | .round_rate = icst_round_rate, |
| 333 | .set_rate = icst_set_rate, |
| 334 | }; |
| 335 | |
Linus Walleij | eb9d642 | 2020-02-19 11:33:25 +0100 | [diff] [blame] | 336 | struct clk *icst_clk_setup(struct device *dev, |
| 337 | const struct clk_icst_desc *desc, |
| 338 | const char *name, |
| 339 | const char *parent_name, |
| 340 | struct regmap *map, |
| 341 | enum icst_control_type ctype) |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 342 | { |
| 343 | struct clk *clk; |
| 344 | struct clk_icst *icst; |
| 345 | struct clk_init_data init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 346 | struct icst_params *pclone; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 347 | |
Markus Elfring | a72da43 | 2017-09-27 21:24:43 +0200 | [diff] [blame] | 348 | icst = kzalloc(sizeof(*icst), GFP_KERNEL); |
Markus Elfring | e343b81 | 2017-09-27 21:21:23 +0200 | [diff] [blame] | 349 | if (!icst) |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 350 | return ERR_PTR(-ENOMEM); |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 351 | |
| 352 | pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL); |
| 353 | if (!pclone) { |
Colin Ian King | ab7ad35 | 2014-04-12 18:59:14 +0100 | [diff] [blame] | 354 | kfree(icst); |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 355 | return ERR_PTR(-ENOMEM); |
| 356 | } |
| 357 | |
Linus Walleij | ae6e694 | 2013-11-22 11:30:05 +0100 | [diff] [blame] | 358 | init.name = name; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 359 | init.ops = &icst_ops; |
Stephen Boyd | ac82a8b | 2016-03-01 11:00:05 -0800 | [diff] [blame] | 360 | init.flags = 0; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 361 | init.parent_names = (parent_name ? &parent_name : NULL); |
| 362 | init.num_parents = (parent_name ? 1 : 0); |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 363 | icst->map = map; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 364 | icst->hw.init = &init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 365 | icst->params = pclone; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 366 | icst->vcoreg_off = desc->vco_offset; |
| 367 | icst->lockreg_off = desc->lock_offset; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 368 | icst->ctype = ctype; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 369 | |
| 370 | clk = clk_register(dev, &icst->hw); |
Linus Walleij | 7bdccef | 2015-10-23 11:36:01 +0200 | [diff] [blame] | 371 | if (IS_ERR(clk)) { |
| 372 | kfree(pclone); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 373 | kfree(icst); |
Linus Walleij | 7bdccef | 2015-10-23 11:36:01 +0200 | [diff] [blame] | 374 | } |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 375 | |
| 376 | return clk; |
| 377 | } |
Linus Walleij | eb9d642 | 2020-02-19 11:33:25 +0100 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(icst_clk_setup); |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 379 | |
| 380 | struct clk *icst_clk_register(struct device *dev, |
| 381 | const struct clk_icst_desc *desc, |
| 382 | const char *name, |
| 383 | const char *parent_name, |
| 384 | void __iomem *base) |
| 385 | { |
| 386 | struct regmap_config icst_regmap_conf = { |
| 387 | .reg_bits = 32, |
| 388 | .val_bits = 32, |
| 389 | .reg_stride = 4, |
| 390 | }; |
| 391 | struct regmap *map; |
| 392 | |
| 393 | map = regmap_init_mmio(dev, base, &icst_regmap_conf); |
| 394 | if (IS_ERR(map)) { |
| 395 | pr_err("could not initialize ICST regmap\n"); |
| 396 | return ERR_CAST(map); |
| 397 | } |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 398 | return icst_clk_setup(dev, desc, name, parent_name, map, |
| 399 | ICST_VERSATILE); |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 400 | } |
Arnd Bergmann | a218d7f | 2014-05-08 16:56:16 +0200 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(icst_clk_register); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 402 | |
| 403 | #ifdef CONFIG_OF |
| 404 | /* |
| 405 | * In a device tree, an memory-mapped ICST clock appear as a child |
| 406 | * of a syscon node. Assume this and probe it only as a child of a |
| 407 | * syscon. |
| 408 | */ |
| 409 | |
| 410 | static const struct icst_params icst525_params = { |
| 411 | .vco_max = ICST525_VCO_MAX_5V, |
| 412 | .vco_min = ICST525_VCO_MIN, |
| 413 | .vd_min = 8, |
| 414 | .vd_max = 263, |
| 415 | .rd_min = 3, |
| 416 | .rd_max = 65, |
| 417 | .s2div = icst525_s2div, |
| 418 | .idx2s = icst525_idx2s, |
| 419 | }; |
| 420 | |
| 421 | static const struct icst_params icst307_params = { |
| 422 | .vco_max = ICST307_VCO_MAX, |
| 423 | .vco_min = ICST307_VCO_MIN, |
| 424 | .vd_min = 4 + 8, |
| 425 | .vd_max = 511 + 8, |
| 426 | .rd_min = 1 + 2, |
| 427 | .rd_max = 127 + 2, |
| 428 | .s2div = icst307_s2div, |
| 429 | .idx2s = icst307_idx2s, |
| 430 | }; |
| 431 | |
Lee Jones | 0c1d46d | 2021-01-20 09:30:39 +0000 | [diff] [blame] | 432 | /* |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 433 | * The core modules on the Integrator/AP and Integrator/CP have |
| 434 | * especially crippled ICST525 control. |
| 435 | */ |
| 436 | static const struct icst_params icst525_apcp_cm_params = { |
| 437 | .vco_max = ICST525_VCO_MAX_5V, |
| 438 | .vco_min = ICST525_VCO_MIN, |
| 439 | /* Minimum 12 MHz, VDW = 4 */ |
| 440 | .vd_min = 12, |
| 441 | /* |
| 442 | * Maximum 160 MHz, VDW = 152 for all core modules, but |
| 443 | * CM926EJ-S, CM1026EJ-S and CM1136JF-S can actually |
| 444 | * go to 200 MHz (max VDW = 192). |
| 445 | */ |
| 446 | .vd_max = 192, |
| 447 | /* r is hardcoded to 22 and this is the actual divisor, +2 */ |
| 448 | .rd_min = 24, |
| 449 | .rd_max = 24, |
| 450 | .s2div = icst525_s2div, |
| 451 | .idx2s = icst525_idx2s, |
| 452 | }; |
| 453 | |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 454 | static const struct icst_params icst525_ap_sys_params = { |
| 455 | .vco_max = ICST525_VCO_MAX_5V, |
| 456 | .vco_min = ICST525_VCO_MIN, |
| 457 | /* Minimum 3 MHz, VDW = 4 */ |
| 458 | .vd_min = 3, |
| 459 | /* Maximum 50 MHz, VDW = 192 */ |
| 460 | .vd_max = 50, |
| 461 | /* r is hardcoded to 46 and this is the actual divisor, +2 */ |
| 462 | .rd_min = 48, |
| 463 | .rd_max = 48, |
| 464 | .s2div = icst525_s2div, |
| 465 | .idx2s = icst525_idx2s, |
| 466 | }; |
| 467 | |
| 468 | static const struct icst_params icst525_ap_pci_params = { |
| 469 | .vco_max = ICST525_VCO_MAX_5V, |
| 470 | .vco_min = ICST525_VCO_MIN, |
| 471 | /* Minimum 25 MHz */ |
| 472 | .vd_min = 25, |
| 473 | /* Maximum 33 MHz */ |
| 474 | .vd_max = 33, |
| 475 | /* r is hardcoded to 14 or 22 and this is the actual divisors +2 */ |
| 476 | .rd_min = 16, |
| 477 | .rd_max = 24, |
| 478 | .s2div = icst525_s2div, |
| 479 | .idx2s = icst525_idx2s, |
| 480 | }; |
| 481 | |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 482 | static void __init of_syscon_icst_setup(struct device_node *np) |
| 483 | { |
| 484 | struct device_node *parent; |
| 485 | struct regmap *map; |
| 486 | struct clk_icst_desc icst_desc; |
Rob Herring | 1b2189f | 2021-11-09 10:46:50 -0600 | [diff] [blame] | 487 | const char *name; |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 488 | const char *parent_name; |
| 489 | struct clk *regclk; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 490 | enum icst_control_type ctype; |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 491 | |
| 492 | /* We do not release this reference, we are using it perpetually */ |
| 493 | parent = of_get_parent(np); |
| 494 | if (!parent) { |
| 495 | pr_err("no parent node for syscon ICST clock\n"); |
| 496 | return; |
| 497 | } |
| 498 | map = syscon_node_to_regmap(parent); |
| 499 | if (IS_ERR(map)) { |
| 500 | pr_err("no regmap for syscon ICST clock parent\n"); |
| 501 | return; |
| 502 | } |
| 503 | |
Rob Herring | 69bfe08 | 2021-09-13 14:28:13 -0500 | [diff] [blame] | 504 | if (of_property_read_u32(np, "reg", &icst_desc.vco_offset) && |
| 505 | of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) { |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 506 | pr_err("no VCO register offset for ICST clock\n"); |
| 507 | return; |
| 508 | } |
| 509 | if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) { |
| 510 | pr_err("no lock register offset for ICST clock\n"); |
| 511 | return; |
| 512 | } |
| 513 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 514 | if (of_device_is_compatible(np, "arm,syscon-icst525")) { |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 515 | icst_desc.params = &icst525_params; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 516 | ctype = ICST_VERSATILE; |
| 517 | } else if (of_device_is_compatible(np, "arm,syscon-icst307")) { |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 518 | icst_desc.params = &icst307_params; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 519 | ctype = ICST_VERSATILE; |
| 520 | } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-cm")) { |
| 521 | icst_desc.params = &icst525_apcp_cm_params; |
| 522 | ctype = ICST_INTEGRATOR_AP_CM; |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 523 | } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-sys")) { |
| 524 | icst_desc.params = &icst525_ap_sys_params; |
| 525 | ctype = ICST_INTEGRATOR_AP_SYS; |
| 526 | } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-pci")) { |
| 527 | icst_desc.params = &icst525_ap_pci_params; |
| 528 | ctype = ICST_INTEGRATOR_AP_PCI; |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 529 | } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-core")) { |
| 530 | icst_desc.params = &icst525_apcp_cm_params; |
| 531 | ctype = ICST_INTEGRATOR_CP_CM_CORE; |
| 532 | } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-mem")) { |
| 533 | icst_desc.params = &icst525_apcp_cm_params; |
| 534 | ctype = ICST_INTEGRATOR_CP_CM_MEM; |
| 535 | } else { |
Rob Herring | 1b2189f | 2021-11-09 10:46:50 -0600 | [diff] [blame] | 536 | pr_err("unknown ICST clock %pOF\n", np); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| 540 | /* Parent clock name is not the same as node parent */ |
| 541 | parent_name = of_clk_get_parent_name(np, 0); |
Rob Herring | 1b2189f | 2021-11-09 10:46:50 -0600 | [diff] [blame] | 542 | name = kasprintf(GFP_KERNEL, "%pOFP", np); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 543 | |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 544 | regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map, ctype); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 545 | if (IS_ERR(regclk)) { |
Rob Herring | 1b2189f | 2021-11-09 10:46:50 -0600 | [diff] [blame] | 546 | kfree(name); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 547 | pr_err("error setting up syscon ICST clock %s\n", name); |
| 548 | return; |
| 549 | } |
| 550 | of_clk_add_provider(np, of_clk_src_simple_get, regclk); |
| 551 | pr_debug("registered syscon ICST clock %s\n", name); |
| 552 | } |
| 553 | |
| 554 | CLK_OF_DECLARE(arm_syscon_icst525_clk, |
| 555 | "arm,syscon-icst525", of_syscon_icst_setup); |
| 556 | CLK_OF_DECLARE(arm_syscon_icst307_clk, |
| 557 | "arm,syscon-icst307", of_syscon_icst_setup); |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 558 | CLK_OF_DECLARE(arm_syscon_integratorap_cm_clk, |
| 559 | "arm,syscon-icst525-integratorap-cm", of_syscon_icst_setup); |
Linus Walleij | fa62e10 | 2016-08-27 14:01:19 +0200 | [diff] [blame] | 560 | CLK_OF_DECLARE(arm_syscon_integratorap_sys_clk, |
| 561 | "arm,syscon-icst525-integratorap-sys", of_syscon_icst_setup); |
| 562 | CLK_OF_DECLARE(arm_syscon_integratorap_pci_clk, |
| 563 | "arm,syscon-icst525-integratorap-pci", of_syscon_icst_setup); |
Linus Walleij | 5e23c59 | 2016-08-22 11:19:33 +0200 | [diff] [blame] | 564 | CLK_OF_DECLARE(arm_syscon_integratorcp_cm_core_clk, |
| 565 | "arm,syscon-icst525-integratorcp-cm-core", of_syscon_icst_setup); |
| 566 | CLK_OF_DECLARE(arm_syscon_integratorcp_cm_mem_clk, |
| 567 | "arm,syscon-icst525-integratorcp-cm-mem", of_syscon_icst_setup); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 568 | #endif |