Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 1 | /* |
| 2 | * OMAP2+ common Clock Management (CM) IP block functions |
| 3 | * |
| 4 | * Copyright (C) 2012 Texas Instruments, Inc. |
Paul Walmsley | d9a16f9 | 2012-10-29 20:57:39 -0600 | [diff] [blame] | 5 | * Paul Walmsley |
Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * XXX This code should eventually be moved to a CM driver. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
Peter Ujfalusi | cc4b1e2 | 2012-11-12 16:17:08 +0100 | [diff] [blame] | 16 | #include <linux/errno.h> |
Tero Kristo | 4794208 | 2014-05-11 19:41:50 -0600 | [diff] [blame] | 17 | #include <linux/bug.h> |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 18 | #include <linux/of.h> |
| 19 | #include <linux/of_address.h> |
Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 20 | |
| 21 | #include "cm2xxx.h" |
| 22 | #include "cm3xxx.h" |
| 23 | #include "cm44xx.h" |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 24 | #include "clock.h" |
Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * cm_ll_data: function pointers to SoC-specific implementations of |
| 28 | * common CM functions |
| 29 | */ |
| 30 | static struct cm_ll_data null_cm_ll_data; |
| 31 | static struct cm_ll_data *cm_ll_data = &null_cm_ll_data; |
| 32 | |
Paul Walmsley | d9a16f9 | 2012-10-29 20:57:39 -0600 | [diff] [blame] | 33 | /* cm_base: base virtual address of the CM IP block */ |
| 34 | void __iomem *cm_base; |
| 35 | |
| 36 | /* cm2_base: base virtual address of the CM2 IP block (OMAP44xx only) */ |
| 37 | void __iomem *cm2_base; |
| 38 | |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 39 | #define CM_NO_CLOCKS 0x1 |
| 40 | |
Paul Walmsley | d9a16f9 | 2012-10-29 20:57:39 -0600 | [diff] [blame] | 41 | /** |
| 42 | * omap2_set_globals_cm - set the CM/CM2 base addresses (for early use) |
| 43 | * @cm: CM base virtual address |
| 44 | * @cm2: CM2 base virtual address (if present on the booted SoC) |
| 45 | * |
| 46 | * XXX Will be replaced when the PRM/CM drivers are completed. |
| 47 | */ |
| 48 | void __init omap2_set_globals_cm(void __iomem *cm, void __iomem *cm2) |
| 49 | { |
| 50 | cm_base = cm; |
| 51 | cm2_base = cm2; |
| 52 | } |
| 53 | |
Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 54 | /** |
Paul Walmsley | c4ceedc | 2012-10-29 20:56:29 -0600 | [diff] [blame] | 55 | * cm_split_idlest_reg - split CM_IDLEST reg addr into its components |
| 56 | * @idlest_reg: CM_IDLEST* virtual address |
| 57 | * @prcm_inst: pointer to an s16 to return the PRCM instance offset |
| 58 | * @idlest_reg_id: pointer to a u8 to return the CM_IDLESTx register ID |
| 59 | * |
| 60 | * Given an absolute CM_IDLEST register address @idlest_reg, passes |
| 61 | * the PRCM instance offset and IDLEST register ID back to the caller |
| 62 | * via the @prcm_inst and @idlest_reg_id. Returns -EINVAL upon error, |
| 63 | * or 0 upon success. XXX This function is only needed until absolute |
| 64 | * register addresses are removed from the OMAP struct clk records. |
| 65 | */ |
| 66 | int cm_split_idlest_reg(void __iomem *idlest_reg, s16 *prcm_inst, |
| 67 | u8 *idlest_reg_id) |
| 68 | { |
| 69 | if (!cm_ll_data->split_idlest_reg) { |
| 70 | WARN_ONCE(1, "cm: %s: no low-level function defined\n", |
| 71 | __func__); |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | |
| 75 | return cm_ll_data->split_idlest_reg(idlest_reg, prcm_inst, |
| 76 | idlest_reg_id); |
| 77 | } |
| 78 | |
| 79 | /** |
Tero Kristo | 021b6ff | 2014-10-27 08:39:23 -0700 | [diff] [blame] | 80 | * omap_cm_wait_module_ready - wait for a module to leave idle or standby |
| 81 | * @part: PRCM partition |
Paul Walmsley | c4ceedc | 2012-10-29 20:56:29 -0600 | [diff] [blame] | 82 | * @prcm_mod: PRCM module offset |
Tero Kristo | 021b6ff | 2014-10-27 08:39:23 -0700 | [diff] [blame] | 83 | * @idlest_reg: CM_IDLESTx register |
Paul Walmsley | c4ceedc | 2012-10-29 20:56:29 -0600 | [diff] [blame] | 84 | * @idlest_shift: shift of the bit in the CM_IDLEST* register to check |
| 85 | * |
| 86 | * Wait for the PRCM to indicate that the module identified by |
| 87 | * (@prcm_mod, @idlest_id, @idlest_shift) is clocked. Return 0 upon |
| 88 | * success, -EBUSY if the module doesn't enable in time, or -EINVAL if |
| 89 | * no per-SoC wait_module_ready() function pointer has been registered |
| 90 | * or if the idlest register is unknown on the SoC. |
| 91 | */ |
Tero Kristo | 021b6ff | 2014-10-27 08:39:23 -0700 | [diff] [blame] | 92 | int omap_cm_wait_module_ready(u8 part, s16 prcm_mod, u16 idlest_reg, |
| 93 | u8 idlest_shift) |
Paul Walmsley | c4ceedc | 2012-10-29 20:56:29 -0600 | [diff] [blame] | 94 | { |
| 95 | if (!cm_ll_data->wait_module_ready) { |
| 96 | WARN_ONCE(1, "cm: %s: no low-level function defined\n", |
| 97 | __func__); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
Tero Kristo | 021b6ff | 2014-10-27 08:39:23 -0700 | [diff] [blame] | 101 | return cm_ll_data->wait_module_ready(part, prcm_mod, idlest_reg, |
| 102 | idlest_shift); |
Paul Walmsley | c4ceedc | 2012-10-29 20:56:29 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Tero Kristo | a8ae5af | 2014-10-27 08:39:23 -0700 | [diff] [blame] | 106 | * omap_cm_wait_module_idle - wait for a module to enter idle or standby |
| 107 | * @part: PRCM partition |
| 108 | * @prcm_mod: PRCM module offset |
| 109 | * @idlest_reg: CM_IDLESTx register |
| 110 | * @idlest_shift: shift of the bit in the CM_IDLEST* register to check |
| 111 | * |
| 112 | * Wait for the PRCM to indicate that the module identified by |
| 113 | * (@prcm_mod, @idlest_id, @idlest_shift) is no longer clocked. Return |
| 114 | * 0 upon success, -EBUSY if the module doesn't enable in time, or |
| 115 | * -EINVAL if no per-SoC wait_module_idle() function pointer has been |
| 116 | * registered or if the idlest register is unknown on the SoC. |
| 117 | */ |
| 118 | int omap_cm_wait_module_idle(u8 part, s16 prcm_mod, u16 idlest_reg, |
| 119 | u8 idlest_shift) |
| 120 | { |
| 121 | if (!cm_ll_data->wait_module_idle) { |
| 122 | WARN_ONCE(1, "cm: %s: no low-level function defined\n", |
| 123 | __func__); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | return cm_ll_data->wait_module_idle(part, prcm_mod, idlest_reg, |
| 128 | idlest_shift); |
| 129 | } |
| 130 | |
| 131 | /** |
Tero Kristo | 128603f | 2014-10-27 08:39:24 -0700 | [diff] [blame] | 132 | * omap_cm_module_enable - enable a module |
| 133 | * @mode: target mode for the module |
| 134 | * @part: PRCM partition |
| 135 | * @inst: PRCM instance |
| 136 | * @clkctrl_offs: CM_CLKCTRL register offset for the module |
| 137 | * |
| 138 | * Enables clocks for a module identified by (@part, @inst, @clkctrl_offs) |
| 139 | * making its IO space accessible. Return 0 upon success, -EINVAL if no |
| 140 | * per-SoC module_enable() function pointer has been registered. |
| 141 | */ |
| 142 | int omap_cm_module_enable(u8 mode, u8 part, u16 inst, u16 clkctrl_offs) |
| 143 | { |
| 144 | if (!cm_ll_data->module_enable) { |
| 145 | WARN_ONCE(1, "cm: %s: no low-level function defined\n", |
| 146 | __func__); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | cm_ll_data->module_enable(mode, part, inst, clkctrl_offs); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * omap_cm_module_disable - disable a module |
| 156 | * @part: PRCM partition |
| 157 | * @inst: PRCM instance |
| 158 | * @clkctrl_offs: CM_CLKCTRL register offset for the module |
| 159 | * |
| 160 | * Disables clocks for a module identified by (@part, @inst, @clkctrl_offs) |
| 161 | * makings its IO space inaccessible. Return 0 upon success, -EINVAL if |
| 162 | * no per-SoC module_disable() function pointer has been registered. |
| 163 | */ |
| 164 | int omap_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs) |
| 165 | { |
| 166 | if (!cm_ll_data->module_disable) { |
| 167 | WARN_ONCE(1, "cm: %s: no low-level function defined\n", |
| 168 | __func__); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | cm_ll_data->module_disable(part, inst, clkctrl_offs); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
Paul Walmsley | 21325b25 | 2012-10-21 01:01:12 -0600 | [diff] [blame] | 177 | * cm_register - register per-SoC low-level data with the CM |
| 178 | * @cld: low-level per-SoC OMAP CM data & function pointers to register |
| 179 | * |
| 180 | * Register per-SoC low-level OMAP CM data and function pointers with |
| 181 | * the OMAP CM common interface. The caller must keep the data |
| 182 | * pointed to by @cld valid until it calls cm_unregister() and |
| 183 | * it returns successfully. Returns 0 upon success, -EINVAL if @cld |
| 184 | * is NULL, or -EEXIST if cm_register() has already been called |
| 185 | * without an intervening cm_unregister(). |
| 186 | */ |
| 187 | int cm_register(struct cm_ll_data *cld) |
| 188 | { |
| 189 | if (!cld) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | if (cm_ll_data != &null_cm_ll_data) |
| 193 | return -EEXIST; |
| 194 | |
| 195 | cm_ll_data = cld; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * cm_unregister - unregister per-SoC low-level data & function pointers |
| 202 | * @cld: low-level per-SoC OMAP CM data & function pointers to unregister |
| 203 | * |
| 204 | * Unregister per-SoC low-level OMAP CM data and function pointers |
| 205 | * that were previously registered with cm_register(). The |
| 206 | * caller may not destroy any of the data pointed to by @cld until |
| 207 | * this function returns successfully. Returns 0 upon success, or |
| 208 | * -EINVAL if @cld is NULL or if @cld does not match the struct |
| 209 | * cm_ll_data * previously registered by cm_register(). |
| 210 | */ |
| 211 | int cm_unregister(struct cm_ll_data *cld) |
| 212 | { |
| 213 | if (!cld || cm_ll_data != cld) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | cm_ll_data = &null_cm_ll_data; |
| 217 | |
| 218 | return 0; |
| 219 | } |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 220 | |
| 221 | static struct omap_prcm_init_data cm_data = { |
| 222 | .index = TI_CLKM_CM, |
| 223 | }; |
| 224 | |
| 225 | static struct omap_prcm_init_data cm2_data = { |
| 226 | .index = TI_CLKM_CM2, |
| 227 | }; |
| 228 | |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 229 | static struct omap_prcm_init_data omap2_prcm_data = { |
| 230 | .index = TI_CLKM_CM, |
| 231 | .flags = CM_NO_CLOCKS, |
| 232 | }; |
| 233 | |
| 234 | static struct omap_prcm_init_data omap3_cm_data = { |
| 235 | .index = TI_CLKM_CM, |
| 236 | |
| 237 | /* |
| 238 | * IVA2 offset is a negative value, must offset the cm_base address |
| 239 | * by this to get it to positive side on the iomap |
| 240 | */ |
| 241 | .offset = -OMAP3430_IVA2_MOD, |
| 242 | }; |
| 243 | |
| 244 | static struct omap_prcm_init_data am3_prcm_data = { |
| 245 | .index = TI_CLKM_CM, |
| 246 | .flags = CM_NO_CLOCKS, |
| 247 | }; |
| 248 | |
| 249 | static struct omap_prcm_init_data am4_prcm_data = { |
| 250 | .index = TI_CLKM_CM, |
| 251 | .flags = CM_NO_CLOCKS, |
| 252 | }; |
| 253 | |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 254 | static const struct of_device_id omap_cm_dt_match_table[] = { |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 255 | { .compatible = "ti,omap2-prcm", .data = &omap2_prcm_data }, |
| 256 | { .compatible = "ti,omap3-cm", .data = &omap3_cm_data }, |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 257 | { .compatible = "ti,omap4-cm1", .data = &cm_data }, |
| 258 | { .compatible = "ti,omap4-cm2", .data = &cm2_data }, |
| 259 | { .compatible = "ti,omap5-cm-core-aon", .data = &cm_data }, |
| 260 | { .compatible = "ti,omap5-cm-core", .data = &cm2_data }, |
| 261 | { .compatible = "ti,dra7-cm-core-aon", .data = &cm_data }, |
| 262 | { .compatible = "ti,dra7-cm-core", .data = &cm2_data }, |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 263 | { .compatible = "ti,am3-prcm", .data = &am3_prcm_data }, |
| 264 | { .compatible = "ti,am4-prcm", .data = &am4_prcm_data }, |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 265 | { } |
| 266 | }; |
| 267 | |
| 268 | /** |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 269 | * omap2_cm_base_init - initialize iomappings for the CM drivers |
| 270 | * |
| 271 | * Detects and initializes the iomappings for the CM driver, based |
| 272 | * on the DT data. Returns 0 in success, negative error value |
| 273 | * otherwise. |
| 274 | */ |
| 275 | int __init omap2_cm_base_init(void) |
| 276 | { |
| 277 | struct device_node *np; |
| 278 | const struct of_device_id *match; |
| 279 | struct omap_prcm_init_data *data; |
| 280 | void __iomem *mem; |
| 281 | |
| 282 | for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) { |
| 283 | data = (struct omap_prcm_init_data *)match->data; |
| 284 | |
| 285 | mem = of_iomap(np, 0); |
| 286 | if (!mem) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | if (data->index == TI_CLKM_CM) |
| 290 | cm_base = mem + data->offset; |
| 291 | |
| 292 | if (data->index == TI_CLKM_CM2) |
| 293 | cm2_base = mem + data->offset; |
| 294 | |
| 295 | data->mem = mem; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /** |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 302 | * omap_cm_init - low level init for the CM drivers |
| 303 | * |
| 304 | * Initializes the low level clock infrastructure for CM drivers. |
| 305 | * Returns 0 in success, negative error value in failure. |
| 306 | */ |
| 307 | int __init omap_cm_init(void) |
| 308 | { |
| 309 | struct device_node *np; |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 310 | const struct of_device_id *match; |
| 311 | const struct omap_prcm_init_data *data; |
| 312 | int ret; |
| 313 | |
| 314 | for_each_matching_node_and_match(np, omap_cm_dt_match_table, &match) { |
| 315 | data = match->data; |
| 316 | |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 317 | if (data->flags & CM_NO_CLOCKS) |
| 318 | continue; |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 319 | |
Tero Kristo | 5970ca2 | 2014-11-11 16:51:52 +0200 | [diff] [blame^] | 320 | ret = omap2_clk_provider_init(np, data->index, data->mem); |
Tero Kristo | fe87414 | 2014-03-12 18:33:45 +0200 | [diff] [blame] | 321 | if (ret) |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |