1 /* 2 * TI clock support 3 * 4 * Copyright (C) 2013 Texas Instruments, Inc. 5 * 6 * Tero Kristo <t-kristo@ti.com> 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 * 12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 13 * kind, whether express or implied; without even the implied warranty 14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 #include <linux/clk.h> 19 #include <linux/clk-provider.h> 20 #include <linux/clkdev.h> 21 #include <linux/clk/ti.h> 22 #include <linux/of.h> 23 #include <linux/of_address.h> 24 #include <linux/list.h> 25 #include <linux/regmap.h> 26 #include <linux/bootmem.h> 27 #include <linux/device.h> 28 29 #include "clock.h" 30 31 #undef pr_fmt 32 #define pr_fmt(fmt) "%s: " fmt, __func__ 33 34 struct ti_clk_ll_ops *ti_clk_ll_ops; 35 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS]; 36 37 static struct ti_clk_features ti_clk_features; 38 39 struct clk_iomap { 40 struct regmap *regmap; 41 void __iomem *mem; 42 }; 43 44 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS]; 45 46 static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg) 47 { 48 struct clk_iomap *io = clk_memmaps[reg->index]; 49 50 if (reg->ptr) 51 writel_relaxed(val, reg->ptr); 52 else if (io->regmap) 53 regmap_write(io->regmap, reg->offset, val); 54 else 55 writel_relaxed(val, io->mem + reg->offset); 56 } 57 58 static u32 clk_memmap_readl(const struct clk_omap_reg *reg) 59 { 60 u32 val; 61 struct clk_iomap *io = clk_memmaps[reg->index]; 62 63 if (reg->ptr) 64 val = readl_relaxed(reg->ptr); 65 else if (io->regmap) 66 regmap_read(io->regmap, reg->offset, &val); 67 else 68 val = readl_relaxed(io->mem + reg->offset); 69 70 return val; 71 } 72 73 /** 74 * ti_clk_setup_ll_ops - setup low level clock operations 75 * @ops: low level clock ops descriptor 76 * 77 * Sets up low level clock operations for TI clock driver. This is used 78 * to provide various callbacks for the clock driver towards platform 79 * specific code. Returns 0 on success, -EBUSY if ll_ops have been 80 * registered already. 81 */ 82 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops) 83 { 84 if (ti_clk_ll_ops) { 85 pr_err("Attempt to register ll_ops multiple times.\n"); 86 return -EBUSY; 87 } 88 89 ti_clk_ll_ops = ops; 90 ops->clk_readl = clk_memmap_readl; 91 ops->clk_writel = clk_memmap_writel; 92 93 return 0; 94 } 95 96 /** 97 * ti_dt_clocks_register - register DT alias clocks during boot 98 * @oclks: list of clocks to register 99 * 100 * Register alias or non-standard DT clock entries during boot. By 101 * default, DT clocks are found based on their node name. If any 102 * additional con-id / dev-id -> clock mapping is required, use this 103 * function to list these. 104 */ 105 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[]) 106 { 107 struct ti_dt_clk *c; 108 struct device_node *node; 109 struct clk *clk; 110 struct of_phandle_args clkspec; 111 char buf[64]; 112 char *ptr; 113 char *tags[2]; 114 int i; 115 int num_args; 116 int ret; 117 static bool clkctrl_nodes_missing; 118 static bool has_clkctrl_data; 119 120 for (c = oclks; c->node_name != NULL; c++) { 121 strcpy(buf, c->node_name); 122 ptr = buf; 123 for (i = 0; i < 2; i++) 124 tags[i] = NULL; 125 num_args = 0; 126 while (*ptr) { 127 if (*ptr == ':') { 128 if (num_args >= 2) { 129 pr_warn("Bad number of tags on %s\n", 130 c->node_name); 131 return; 132 } 133 tags[num_args++] = ptr + 1; 134 *ptr = 0; 135 } 136 ptr++; 137 } 138 139 if (num_args && clkctrl_nodes_missing) 140 continue; 141 142 node = of_find_node_by_name(NULL, buf); 143 if (num_args) 144 node = of_find_node_by_name(node, "clk"); 145 clkspec.np = node; 146 clkspec.args_count = num_args; 147 for (i = 0; i < num_args; i++) { 148 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i); 149 if (ret) { 150 pr_warn("Bad tag in %s at %d: %s\n", 151 c->node_name, i, tags[i]); 152 return; 153 } 154 } 155 clk = of_clk_get_from_provider(&clkspec); 156 157 if (!IS_ERR(clk)) { 158 c->lk.clk = clk; 159 clkdev_add(&c->lk); 160 } else { 161 if (num_args && !has_clkctrl_data) { 162 if (of_find_compatible_node(NULL, NULL, 163 "ti,clkctrl")) { 164 has_clkctrl_data = true; 165 } else { 166 clkctrl_nodes_missing = true; 167 168 pr_warn("missing clkctrl nodes, please update your dts.\n"); 169 continue; 170 } 171 } 172 173 pr_warn("failed to lookup clock node %s, ret=%ld\n", 174 c->node_name, PTR_ERR(clk)); 175 } 176 } 177 } 178 179 struct clk_init_item { 180 struct device_node *node; 181 void *user; 182 ti_of_clk_init_cb_t func; 183 struct list_head link; 184 }; 185 186 static LIST_HEAD(retry_list); 187 188 /** 189 * ti_clk_retry_init - retries a failed clock init at later phase 190 * @node: device not for the clock 191 * @user: user data pointer 192 * @func: init function to be called for the clock 193 * 194 * Adds a failed clock init to the retry list. The retry list is parsed 195 * once all the other clocks have been initialized. 196 */ 197 int __init ti_clk_retry_init(struct device_node *node, void *user, 198 ti_of_clk_init_cb_t func) 199 { 200 struct clk_init_item *retry; 201 202 pr_debug("%s: adding to retry list...\n", node->name); 203 retry = kzalloc(sizeof(*retry), GFP_KERNEL); 204 if (!retry) 205 return -ENOMEM; 206 207 retry->node = node; 208 retry->func = func; 209 retry->user = user; 210 list_add(&retry->link, &retry_list); 211 212 return 0; 213 } 214 215 /** 216 * ti_clk_get_reg_addr - get register address for a clock register 217 * @node: device node for the clock 218 * @index: register index from the clock node 219 * @reg: pointer to target register struct 220 * 221 * Builds clock register address from device tree information, and returns 222 * the data via the provided output pointer @reg. Returns 0 on success, 223 * negative error value on failure. 224 */ 225 int ti_clk_get_reg_addr(struct device_node *node, int index, 226 struct clk_omap_reg *reg) 227 { 228 u32 val; 229 int i; 230 231 for (i = 0; i < CLK_MAX_MEMMAPS; i++) { 232 if (clocks_node_ptr[i] == node->parent) 233 break; 234 } 235 236 if (i == CLK_MAX_MEMMAPS) { 237 pr_err("clk-provider not found for %s!\n", node->name); 238 return -ENOENT; 239 } 240 241 reg->index = i; 242 243 if (of_property_read_u32_index(node, "reg", index, &val)) { 244 pr_err("%s must have reg[%d]!\n", node->name, index); 245 return -EINVAL; 246 } 247 248 reg->offset = val; 249 reg->ptr = NULL; 250 251 return 0; 252 } 253 254 /** 255 * omap2_clk_provider_init - init master clock provider 256 * @parent: master node 257 * @index: internal index for clk_reg_ops 258 * @syscon: syscon regmap pointer for accessing clock registers 259 * @mem: iomem pointer for the clock provider memory area, only used if 260 * syscon is not provided 261 * 262 * Initializes a master clock IP block. This basically sets up the 263 * mapping from clocks node to the memory map index. All the clocks 264 * are then initialized through the common of_clk_init call, and the 265 * clocks will access their memory maps based on the node layout. 266 * Returns 0 in success. 267 */ 268 int __init omap2_clk_provider_init(struct device_node *parent, int index, 269 struct regmap *syscon, void __iomem *mem) 270 { 271 struct device_node *clocks; 272 struct clk_iomap *io; 273 274 /* get clocks for this parent */ 275 clocks = of_get_child_by_name(parent, "clocks"); 276 if (!clocks) { 277 pr_err("%s missing 'clocks' child node.\n", parent->name); 278 return -EINVAL; 279 } 280 281 /* add clocks node info */ 282 clocks_node_ptr[index] = clocks; 283 284 io = kzalloc(sizeof(*io), GFP_KERNEL); 285 if (!io) 286 return -ENOMEM; 287 288 io->regmap = syscon; 289 io->mem = mem; 290 291 clk_memmaps[index] = io; 292 293 return 0; 294 } 295 296 /** 297 * omap2_clk_legacy_provider_init - initialize a legacy clock provider 298 * @index: index for the clock provider 299 * @mem: iomem pointer for the clock provider memory area 300 * 301 * Initializes a legacy clock provider memory mapping. 302 */ 303 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem) 304 { 305 struct clk_iomap *io; 306 307 io = memblock_virt_alloc(sizeof(*io), 0); 308 309 io->mem = mem; 310 311 clk_memmaps[index] = io; 312 } 313 314 /** 315 * ti_dt_clk_init_retry_clks - init clocks from the retry list 316 * 317 * Initializes any clocks that have failed to initialize before, 318 * reasons being missing parent node(s) during earlier init. This 319 * typically happens only for DPLLs which need to have both of their 320 * parent clocks ready during init. 321 */ 322 void ti_dt_clk_init_retry_clks(void) 323 { 324 struct clk_init_item *retry; 325 struct clk_init_item *tmp; 326 int retries = 5; 327 328 while (!list_empty(&retry_list) && retries) { 329 list_for_each_entry_safe(retry, tmp, &retry_list, link) { 330 pr_debug("retry-init: %s\n", retry->node->name); 331 retry->func(retry->user, retry->node); 332 list_del(&retry->link); 333 kfree(retry); 334 } 335 retries--; 336 } 337 } 338 339 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS) 340 void __init ti_clk_patch_legacy_clks(struct ti_clk **patch) 341 { 342 while (*patch) { 343 memcpy((*patch)->patch, *patch, sizeof(**patch)); 344 patch++; 345 } 346 } 347 348 struct clk __init *ti_clk_register_clk(struct ti_clk *setup) 349 { 350 struct clk *clk; 351 struct ti_clk_fixed *fixed; 352 struct ti_clk_fixed_factor *fixed_factor; 353 struct clk_hw *clk_hw; 354 int ret; 355 356 if (setup->clk) 357 return setup->clk; 358 359 switch (setup->type) { 360 case TI_CLK_FIXED: 361 fixed = setup->data; 362 363 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0, 364 fixed->frequency); 365 if (!IS_ERR(clk)) { 366 ret = ti_clk_add_alias(NULL, clk, setup->name); 367 if (ret) { 368 clk_unregister(clk); 369 clk = ERR_PTR(ret); 370 } 371 } 372 break; 373 case TI_CLK_MUX: 374 clk = ti_clk_register_mux(setup); 375 break; 376 case TI_CLK_DIVIDER: 377 clk = ti_clk_register_divider(setup); 378 break; 379 case TI_CLK_COMPOSITE: 380 clk = ti_clk_register_composite(setup); 381 break; 382 case TI_CLK_FIXED_FACTOR: 383 fixed_factor = setup->data; 384 385 clk = clk_register_fixed_factor(NULL, setup->name, 386 fixed_factor->parent, 387 0, fixed_factor->mult, 388 fixed_factor->div); 389 if (!IS_ERR(clk)) { 390 ret = ti_clk_add_alias(NULL, clk, setup->name); 391 if (ret) { 392 clk_unregister(clk); 393 clk = ERR_PTR(ret); 394 } 395 } 396 break; 397 case TI_CLK_GATE: 398 clk = ti_clk_register_gate(setup); 399 break; 400 case TI_CLK_DPLL: 401 clk = ti_clk_register_dpll(setup); 402 break; 403 default: 404 pr_err("bad type for %s!\n", setup->name); 405 clk = ERR_PTR(-EINVAL); 406 } 407 408 if (!IS_ERR(clk)) { 409 setup->clk = clk; 410 if (setup->clkdm_name) { 411 clk_hw = __clk_get_hw(clk); 412 if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) { 413 pr_warn("can't setup clkdm for basic clk %s\n", 414 setup->name); 415 } else { 416 to_clk_hw_omap(clk_hw)->clkdm_name = 417 setup->clkdm_name; 418 omap2_init_clk_clkdm(clk_hw); 419 } 420 } 421 } 422 423 return clk; 424 } 425 426 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks) 427 { 428 struct clk *clk; 429 bool retry; 430 struct ti_clk_alias *retry_clk; 431 struct ti_clk_alias *tmp; 432 433 while (clks->clk) { 434 clk = ti_clk_register_clk(clks->clk); 435 if (IS_ERR(clk)) { 436 if (PTR_ERR(clk) == -EAGAIN) { 437 list_add(&clks->link, &retry_list); 438 } else { 439 pr_err("register for %s failed: %ld\n", 440 clks->clk->name, PTR_ERR(clk)); 441 return PTR_ERR(clk); 442 } 443 } 444 clks++; 445 } 446 447 retry = true; 448 449 while (!list_empty(&retry_list) && retry) { 450 retry = false; 451 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) { 452 pr_debug("retry-init: %s\n", retry_clk->clk->name); 453 clk = ti_clk_register_clk(retry_clk->clk); 454 if (IS_ERR(clk)) { 455 if (PTR_ERR(clk) == -EAGAIN) { 456 continue; 457 } else { 458 pr_err("register for %s failed: %ld\n", 459 retry_clk->clk->name, 460 PTR_ERR(clk)); 461 return PTR_ERR(clk); 462 } 463 } else { 464 retry = true; 465 list_del(&retry_clk->link); 466 } 467 } 468 } 469 470 return 0; 471 } 472 #endif 473 474 static const struct of_device_id simple_clk_match_table[] __initconst = { 475 { .compatible = "fixed-clock" }, 476 { .compatible = "fixed-factor-clock" }, 477 { } 478 }; 479 480 /** 481 * ti_clk_add_aliases - setup clock aliases 482 * 483 * Sets up any missing clock aliases. No return value. 484 */ 485 void __init ti_clk_add_aliases(void) 486 { 487 struct device_node *np; 488 struct clk *clk; 489 490 for_each_matching_node(np, simple_clk_match_table) { 491 struct of_phandle_args clkspec; 492 493 clkspec.np = np; 494 clk = of_clk_get_from_provider(&clkspec); 495 496 ti_clk_add_alias(NULL, clk, np->name); 497 } 498 } 499 500 /** 501 * ti_clk_setup_features - setup clock features flags 502 * @features: features definition to use 503 * 504 * Initializes the clock driver features flags based on platform 505 * provided data. No return value. 506 */ 507 void __init ti_clk_setup_features(struct ti_clk_features *features) 508 { 509 memcpy(&ti_clk_features, features, sizeof(*features)); 510 } 511 512 /** 513 * ti_clk_get_features - get clock driver features flags 514 * 515 * Get TI clock driver features description. Returns a pointer 516 * to the current feature setup. 517 */ 518 const struct ti_clk_features *ti_clk_get_features(void) 519 { 520 return &ti_clk_features; 521 } 522 523 /** 524 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks 525 * @clk_names: ptr to an array of strings of clock names to enable 526 * @num_clocks: number of clock names in @clk_names 527 * 528 * Prepare and enable a list of clocks, named by @clk_names. No 529 * return value. XXX Deprecated; only needed until these clocks are 530 * properly claimed and enabled by the drivers or core code that uses 531 * them. XXX What code disables & calls clk_put on these clocks? 532 */ 533 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks) 534 { 535 struct clk *init_clk; 536 int i; 537 538 for (i = 0; i < num_clocks; i++) { 539 init_clk = clk_get(NULL, clk_names[i]); 540 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n", 541 clk_names[i])) 542 continue; 543 clk_prepare_enable(init_clk); 544 } 545 } 546 547 /** 548 * ti_clk_add_alias - add a clock alias for a TI clock 549 * @dev: device alias for this clock 550 * @clk: clock handle to create alias for 551 * @con: connection ID for this clock 552 * 553 * Creates a clock alias for a TI clock. Allocates the clock lookup entry 554 * and assigns the data to it. Returns 0 if successful, negative error 555 * value otherwise. 556 */ 557 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con) 558 { 559 struct clk_lookup *cl; 560 561 if (!clk) 562 return 0; 563 564 if (IS_ERR(clk)) 565 return PTR_ERR(clk); 566 567 cl = kzalloc(sizeof(*cl), GFP_KERNEL); 568 if (!cl) 569 return -ENOMEM; 570 571 if (dev) 572 cl->dev_id = dev_name(dev); 573 cl->con_id = con; 574 cl->clk = clk; 575 576 clkdev_add(cl); 577 578 return 0; 579 } 580 581 /** 582 * ti_clk_register - register a TI clock to the common clock framework 583 * @dev: device for this clock 584 * @hw: hardware clock handle 585 * @con: connection ID for this clock 586 * 587 * Registers a TI clock to the common clock framework, and adds a clock 588 * alias for it. Returns a handle to the registered clock if successful, 589 * ERR_PTR value in failure. 590 */ 591 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw, 592 const char *con) 593 { 594 struct clk *clk; 595 int ret; 596 597 clk = clk_register(dev, hw); 598 if (IS_ERR(clk)) 599 return clk; 600 601 ret = ti_clk_add_alias(dev, clk, con); 602 if (ret) { 603 clk_unregister(clk); 604 return ERR_PTR(ret); 605 } 606 607 return clk; 608 } 609