1 /* 2 * TI Divider Clock 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-provider.h> 19 #include <linux/slab.h> 20 #include <linux/err.h> 21 #include <linux/of.h> 22 #include <linux/of_address.h> 23 #include <linux/clk/ti.h> 24 #include "clock.h" 25 26 #undef pr_fmt 27 #define pr_fmt(fmt) "%s: " fmt, __func__ 28 29 #define div_mask(d) ((1 << ((d)->width)) - 1) 30 31 static unsigned int _get_table_maxdiv(const struct clk_div_table *table) 32 { 33 unsigned int maxdiv = 0; 34 const struct clk_div_table *clkt; 35 36 for (clkt = table; clkt->div; clkt++) 37 if (clkt->div > maxdiv) 38 maxdiv = clkt->div; 39 return maxdiv; 40 } 41 42 static unsigned int _get_maxdiv(struct clk_omap_divider *divider) 43 { 44 if (divider->flags & CLK_DIVIDER_ONE_BASED) 45 return div_mask(divider); 46 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) 47 return 1 << div_mask(divider); 48 if (divider->table) 49 return _get_table_maxdiv(divider->table); 50 return div_mask(divider) + 1; 51 } 52 53 static unsigned int _get_table_div(const struct clk_div_table *table, 54 unsigned int val) 55 { 56 const struct clk_div_table *clkt; 57 58 for (clkt = table; clkt->div; clkt++) 59 if (clkt->val == val) 60 return clkt->div; 61 return 0; 62 } 63 64 static unsigned int _get_div(struct clk_omap_divider *divider, unsigned int val) 65 { 66 if (divider->flags & CLK_DIVIDER_ONE_BASED) 67 return val; 68 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) 69 return 1 << val; 70 if (divider->table) 71 return _get_table_div(divider->table, val); 72 return val + 1; 73 } 74 75 static unsigned int _get_table_val(const struct clk_div_table *table, 76 unsigned int div) 77 { 78 const struct clk_div_table *clkt; 79 80 for (clkt = table; clkt->div; clkt++) 81 if (clkt->div == div) 82 return clkt->val; 83 return 0; 84 } 85 86 static unsigned int _get_val(struct clk_omap_divider *divider, u8 div) 87 { 88 if (divider->flags & CLK_DIVIDER_ONE_BASED) 89 return div; 90 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) 91 return __ffs(div); 92 if (divider->table) 93 return _get_table_val(divider->table, div); 94 return div - 1; 95 } 96 97 static unsigned long ti_clk_divider_recalc_rate(struct clk_hw *hw, 98 unsigned long parent_rate) 99 { 100 struct clk_omap_divider *divider = to_clk_omap_divider(hw); 101 unsigned int div, val; 102 103 val = ti_clk_ll_ops->clk_readl(÷r->reg) >> divider->shift; 104 val &= div_mask(divider); 105 106 div = _get_div(divider, val); 107 if (!div) { 108 WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO), 109 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 110 clk_hw_get_name(hw)); 111 return parent_rate; 112 } 113 114 return DIV_ROUND_UP(parent_rate, div); 115 } 116 117 /* 118 * The reverse of DIV_ROUND_UP: The maximum number which 119 * divided by m is r 120 */ 121 #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1) 122 123 static bool _is_valid_table_div(const struct clk_div_table *table, 124 unsigned int div) 125 { 126 const struct clk_div_table *clkt; 127 128 for (clkt = table; clkt->div; clkt++) 129 if (clkt->div == div) 130 return true; 131 return false; 132 } 133 134 static bool _is_valid_div(struct clk_omap_divider *divider, unsigned int div) 135 { 136 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) 137 return is_power_of_2(div); 138 if (divider->table) 139 return _is_valid_table_div(divider->table, div); 140 return true; 141 } 142 143 static int _div_round_up(const struct clk_div_table *table, 144 unsigned long parent_rate, unsigned long rate) 145 { 146 const struct clk_div_table *clkt; 147 int up = INT_MAX; 148 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 149 150 for (clkt = table; clkt->div; clkt++) { 151 if (clkt->div == div) 152 return clkt->div; 153 else if (clkt->div < div) 154 continue; 155 156 if ((clkt->div - div) < (up - div)) 157 up = clkt->div; 158 } 159 160 return up; 161 } 162 163 static int _div_round(const struct clk_div_table *table, 164 unsigned long parent_rate, unsigned long rate) 165 { 166 if (!table) 167 return DIV_ROUND_UP(parent_rate, rate); 168 169 return _div_round_up(table, parent_rate, rate); 170 } 171 172 static int ti_clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate, 173 unsigned long *best_parent_rate) 174 { 175 struct clk_omap_divider *divider = to_clk_omap_divider(hw); 176 int i, bestdiv = 0; 177 unsigned long parent_rate, best = 0, now, maxdiv; 178 unsigned long parent_rate_saved = *best_parent_rate; 179 180 if (!rate) 181 rate = 1; 182 183 maxdiv = _get_maxdiv(divider); 184 185 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 186 parent_rate = *best_parent_rate; 187 bestdiv = _div_round(divider->table, parent_rate, rate); 188 bestdiv = bestdiv == 0 ? 1 : bestdiv; 189 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 190 return bestdiv; 191 } 192 193 /* 194 * The maximum divider we can use without overflowing 195 * unsigned long in rate * i below 196 */ 197 maxdiv = min(ULONG_MAX / rate, maxdiv); 198 199 for (i = 1; i <= maxdiv; i++) { 200 if (!_is_valid_div(divider, i)) 201 continue; 202 if (rate * i == parent_rate_saved) { 203 /* 204 * It's the most ideal case if the requested rate can be 205 * divided from parent clock without needing to change 206 * parent rate, so return the divider immediately. 207 */ 208 *best_parent_rate = parent_rate_saved; 209 return i; 210 } 211 parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), 212 MULT_ROUND_UP(rate, i)); 213 now = DIV_ROUND_UP(parent_rate, i); 214 if (now <= rate && now > best) { 215 bestdiv = i; 216 best = now; 217 *best_parent_rate = parent_rate; 218 } 219 } 220 221 if (!bestdiv) { 222 bestdiv = _get_maxdiv(divider); 223 *best_parent_rate = 224 clk_hw_round_rate(clk_hw_get_parent(hw), 1); 225 } 226 227 return bestdiv; 228 } 229 230 static long ti_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 231 unsigned long *prate) 232 { 233 int div; 234 div = ti_clk_divider_bestdiv(hw, rate, prate); 235 236 return DIV_ROUND_UP(*prate, div); 237 } 238 239 static int ti_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 240 unsigned long parent_rate) 241 { 242 struct clk_omap_divider *divider; 243 unsigned int div, value; 244 u32 val; 245 246 if (!hw || !rate) 247 return -EINVAL; 248 249 divider = to_clk_omap_divider(hw); 250 251 div = DIV_ROUND_UP(parent_rate, rate); 252 value = _get_val(divider, div); 253 254 if (value > div_mask(divider)) 255 value = div_mask(divider); 256 257 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 258 val = div_mask(divider) << (divider->shift + 16); 259 } else { 260 val = ti_clk_ll_ops->clk_readl(÷r->reg); 261 val &= ~(div_mask(divider) << divider->shift); 262 } 263 val |= value << divider->shift; 264 ti_clk_ll_ops->clk_writel(val, ÷r->reg); 265 266 return 0; 267 } 268 269 const struct clk_ops ti_clk_divider_ops = { 270 .recalc_rate = ti_clk_divider_recalc_rate, 271 .round_rate = ti_clk_divider_round_rate, 272 .set_rate = ti_clk_divider_set_rate, 273 }; 274 275 static struct clk *_register_divider(struct device *dev, const char *name, 276 const char *parent_name, 277 unsigned long flags, 278 struct clk_omap_reg *reg, 279 u8 shift, u8 width, u8 clk_divider_flags, 280 const struct clk_div_table *table) 281 { 282 struct clk_omap_divider *div; 283 struct clk *clk; 284 struct clk_init_data init; 285 286 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 287 if (width + shift > 16) { 288 pr_warn("divider value exceeds LOWORD field\n"); 289 return ERR_PTR(-EINVAL); 290 } 291 } 292 293 /* allocate the divider */ 294 div = kzalloc(sizeof(*div), GFP_KERNEL); 295 if (!div) { 296 pr_err("%s: could not allocate divider clk\n", __func__); 297 return ERR_PTR(-ENOMEM); 298 } 299 300 init.name = name; 301 init.ops = &ti_clk_divider_ops; 302 init.flags = flags | CLK_IS_BASIC; 303 init.parent_names = (parent_name ? &parent_name : NULL); 304 init.num_parents = (parent_name ? 1 : 0); 305 306 /* struct clk_divider assignments */ 307 memcpy(&div->reg, reg, sizeof(*reg)); 308 div->shift = shift; 309 div->width = width; 310 div->flags = clk_divider_flags; 311 div->hw.init = &init; 312 div->table = table; 313 314 /* register the clock */ 315 clk = ti_clk_register(dev, &div->hw, name); 316 317 if (IS_ERR(clk)) 318 kfree(div); 319 320 return clk; 321 } 322 323 int ti_clk_parse_divider_data(int *div_table, int num_dividers, int max_div, 324 u8 flags, u8 *width, 325 const struct clk_div_table **table) 326 { 327 int valid_div = 0; 328 u32 val; 329 int div; 330 int i; 331 struct clk_div_table *tmp; 332 333 if (!div_table) { 334 if (flags & CLKF_INDEX_STARTS_AT_ONE) 335 val = 1; 336 else 337 val = 0; 338 339 div = 1; 340 341 while (div < max_div) { 342 if (flags & CLKF_INDEX_POWER_OF_TWO) 343 div <<= 1; 344 else 345 div++; 346 val++; 347 } 348 349 *width = fls(val); 350 *table = NULL; 351 352 return 0; 353 } 354 355 i = 0; 356 357 while (!num_dividers || i < num_dividers) { 358 if (div_table[i] == -1) 359 break; 360 if (div_table[i]) 361 valid_div++; 362 i++; 363 } 364 365 num_dividers = i; 366 367 tmp = kzalloc(sizeof(*tmp) * (valid_div + 1), GFP_KERNEL); 368 if (!tmp) 369 return -ENOMEM; 370 371 valid_div = 0; 372 *width = 0; 373 374 for (i = 0; i < num_dividers; i++) 375 if (div_table[i] > 0) { 376 tmp[valid_div].div = div_table[i]; 377 tmp[valid_div].val = i; 378 valid_div++; 379 *width = i; 380 } 381 382 *width = fls(*width); 383 *table = tmp; 384 385 return 0; 386 } 387 388 static const struct clk_div_table * 389 _get_div_table_from_setup(struct ti_clk_divider *setup, u8 *width) 390 { 391 const struct clk_div_table *table = NULL; 392 393 ti_clk_parse_divider_data(setup->dividers, setup->num_dividers, 394 setup->max_div, setup->flags, width, 395 &table); 396 397 return table; 398 } 399 400 struct clk_hw *ti_clk_build_component_div(struct ti_clk_divider *setup) 401 { 402 struct clk_omap_divider *div; 403 struct clk_omap_reg *reg; 404 405 if (!setup) 406 return NULL; 407 408 div = kzalloc(sizeof(*div), GFP_KERNEL); 409 if (!div) 410 return ERR_PTR(-ENOMEM); 411 412 reg = (struct clk_omap_reg *)&div->reg; 413 reg->index = setup->module; 414 reg->offset = setup->reg; 415 416 if (setup->flags & CLKF_INDEX_STARTS_AT_ONE) 417 div->flags |= CLK_DIVIDER_ONE_BASED; 418 419 if (setup->flags & CLKF_INDEX_POWER_OF_TWO) 420 div->flags |= CLK_DIVIDER_POWER_OF_TWO; 421 422 div->table = _get_div_table_from_setup(setup, &div->width); 423 424 div->shift = setup->bit_shift; 425 426 return &div->hw; 427 } 428 429 struct clk *ti_clk_register_divider(struct ti_clk *setup) 430 { 431 struct ti_clk_divider *div = setup->data; 432 struct clk_omap_reg reg = { 433 .index = div->module, 434 .offset = div->reg, 435 }; 436 u8 width; 437 u32 flags = 0; 438 u8 div_flags = 0; 439 const struct clk_div_table *table; 440 struct clk *clk; 441 442 if (div->flags & CLKF_INDEX_STARTS_AT_ONE) 443 div_flags |= CLK_DIVIDER_ONE_BASED; 444 445 if (div->flags & CLKF_INDEX_POWER_OF_TWO) 446 div_flags |= CLK_DIVIDER_POWER_OF_TWO; 447 448 if (div->flags & CLKF_SET_RATE_PARENT) 449 flags |= CLK_SET_RATE_PARENT; 450 451 table = _get_div_table_from_setup(div, &width); 452 if (IS_ERR(table)) 453 return (struct clk *)table; 454 455 clk = _register_divider(NULL, setup->name, div->parent, 456 flags, ®, div->bit_shift, 457 width, div_flags, table); 458 459 if (IS_ERR(clk)) 460 kfree(table); 461 462 return clk; 463 } 464 465 static struct clk_div_table * 466 __init ti_clk_get_div_table(struct device_node *node) 467 { 468 struct clk_div_table *table; 469 const __be32 *divspec; 470 u32 val; 471 u32 num_div; 472 u32 valid_div; 473 int i; 474 475 divspec = of_get_property(node, "ti,dividers", &num_div); 476 477 if (!divspec) 478 return NULL; 479 480 num_div /= 4; 481 482 valid_div = 0; 483 484 /* Determine required size for divider table */ 485 for (i = 0; i < num_div; i++) { 486 of_property_read_u32_index(node, "ti,dividers", i, &val); 487 if (val) 488 valid_div++; 489 } 490 491 if (!valid_div) { 492 pr_err("no valid dividers for %s table\n", node->name); 493 return ERR_PTR(-EINVAL); 494 } 495 496 table = kzalloc(sizeof(*table) * (valid_div + 1), GFP_KERNEL); 497 498 if (!table) 499 return ERR_PTR(-ENOMEM); 500 501 valid_div = 0; 502 503 for (i = 0; i < num_div; i++) { 504 of_property_read_u32_index(node, "ti,dividers", i, &val); 505 if (val) { 506 table[valid_div].div = val; 507 table[valid_div].val = i; 508 valid_div++; 509 } 510 } 511 512 return table; 513 } 514 515 static int _get_divider_width(struct device_node *node, 516 const struct clk_div_table *table, 517 u8 flags) 518 { 519 u32 min_div; 520 u32 max_div; 521 u32 val = 0; 522 u32 div; 523 524 if (!table) { 525 /* Clk divider table not provided, determine min/max divs */ 526 if (of_property_read_u32(node, "ti,min-div", &min_div)) 527 min_div = 1; 528 529 if (of_property_read_u32(node, "ti,max-div", &max_div)) { 530 pr_err("no max-div for %s!\n", node->name); 531 return -EINVAL; 532 } 533 534 /* Determine bit width for the field */ 535 if (flags & CLK_DIVIDER_ONE_BASED) 536 val = 1; 537 538 div = min_div; 539 540 while (div < max_div) { 541 if (flags & CLK_DIVIDER_POWER_OF_TWO) 542 div <<= 1; 543 else 544 div++; 545 val++; 546 } 547 } else { 548 div = 0; 549 550 while (table[div].div) { 551 val = table[div].val; 552 div++; 553 } 554 } 555 556 return fls(val); 557 } 558 559 static int __init ti_clk_divider_populate(struct device_node *node, 560 struct clk_omap_reg *reg, const struct clk_div_table **table, 561 u32 *flags, u8 *div_flags, u8 *width, u8 *shift) 562 { 563 u32 val; 564 int ret; 565 566 ret = ti_clk_get_reg_addr(node, 0, reg); 567 if (ret) 568 return ret; 569 570 if (!of_property_read_u32(node, "ti,bit-shift", &val)) 571 *shift = val; 572 else 573 *shift = 0; 574 575 *flags = 0; 576 *div_flags = 0; 577 578 if (of_property_read_bool(node, "ti,index-starts-at-one")) 579 *div_flags |= CLK_DIVIDER_ONE_BASED; 580 581 if (of_property_read_bool(node, "ti,index-power-of-two")) 582 *div_flags |= CLK_DIVIDER_POWER_OF_TWO; 583 584 if (of_property_read_bool(node, "ti,set-rate-parent")) 585 *flags |= CLK_SET_RATE_PARENT; 586 587 *table = ti_clk_get_div_table(node); 588 589 if (IS_ERR(*table)) 590 return PTR_ERR(*table); 591 592 *width = _get_divider_width(node, *table, *div_flags); 593 594 return 0; 595 } 596 597 /** 598 * of_ti_divider_clk_setup - Setup function for simple div rate clock 599 * @node: device node for this clock 600 * 601 * Sets up a basic divider clock. 602 */ 603 static void __init of_ti_divider_clk_setup(struct device_node *node) 604 { 605 struct clk *clk; 606 const char *parent_name; 607 struct clk_omap_reg reg; 608 u8 clk_divider_flags = 0; 609 u8 width = 0; 610 u8 shift = 0; 611 const struct clk_div_table *table = NULL; 612 u32 flags = 0; 613 614 parent_name = of_clk_get_parent_name(node, 0); 615 616 if (ti_clk_divider_populate(node, ®, &table, &flags, 617 &clk_divider_flags, &width, &shift)) 618 goto cleanup; 619 620 clk = _register_divider(NULL, node->name, parent_name, flags, ®, 621 shift, width, clk_divider_flags, table); 622 623 if (!IS_ERR(clk)) { 624 of_clk_add_provider(node, of_clk_src_simple_get, clk); 625 of_ti_clk_autoidle_setup(node); 626 return; 627 } 628 629 cleanup: 630 kfree(table); 631 } 632 CLK_OF_DECLARE(divider_clk, "ti,divider-clock", of_ti_divider_clk_setup); 633 634 static void __init of_ti_composite_divider_clk_setup(struct device_node *node) 635 { 636 struct clk_omap_divider *div; 637 u32 val; 638 639 div = kzalloc(sizeof(*div), GFP_KERNEL); 640 if (!div) 641 return; 642 643 if (ti_clk_divider_populate(node, &div->reg, &div->table, &val, 644 &div->flags, &div->width, &div->shift) < 0) 645 goto cleanup; 646 647 if (!ti_clk_add_component(node, &div->hw, CLK_COMPONENT_TYPE_DIVIDER)) 648 return; 649 650 cleanup: 651 kfree(div->table); 652 kfree(div); 653 } 654 CLK_OF_DECLARE(ti_composite_divider_clk, "ti,composite-divider-clock", 655 of_ti_composite_divider_clk_setup); 656