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