1 /* 2 * Copyright (C) 2013 Broadcom Corporation 3 * Copyright 2013 Linaro Limited 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation version 2. 8 * 9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 10 * kind, whether express or implied; without even the implied warranty 11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/io.h> 16 #include <linux/of_address.h> 17 18 #include "clk-kona.h" 19 20 /* These are used when a selector or trigger is found to be unneeded */ 21 #define selector_clear_exists(sel) ((sel)->width = 0) 22 #define trigger_clear_exists(trig) FLAG_CLEAR(trig, TRIG, EXISTS) 23 24 LIST_HEAD(ccu_list); /* The list of set up CCUs */ 25 26 /* Validity checking */ 27 28 static bool clk_requires_trigger(struct kona_clk *bcm_clk) 29 { 30 struct peri_clk_data *peri = bcm_clk->peri; 31 struct bcm_clk_sel *sel; 32 struct bcm_clk_div *div; 33 34 if (bcm_clk->type != bcm_clk_peri) 35 return false; 36 37 sel = &peri->sel; 38 if (sel->parent_count && selector_exists(sel)) 39 return true; 40 41 div = &peri->div; 42 if (!divider_exists(div)) 43 return false; 44 45 /* Fixed dividers don't need triggers */ 46 if (!divider_is_fixed(div)) 47 return true; 48 49 div = &peri->pre_div; 50 51 return divider_exists(div) && !divider_is_fixed(div); 52 } 53 54 static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk) 55 { 56 struct peri_clk_data *peri; 57 struct bcm_clk_gate *gate; 58 struct bcm_clk_div *div; 59 struct bcm_clk_sel *sel; 60 struct bcm_clk_trig *trig; 61 const char *name; 62 u32 range; 63 u32 limit; 64 65 BUG_ON(bcm_clk->type != bcm_clk_peri); 66 peri = bcm_clk->peri; 67 name = bcm_clk->name; 68 range = bcm_clk->ccu->range; 69 70 limit = range - sizeof(u32); 71 limit = round_down(limit, sizeof(u32)); 72 73 gate = &peri->gate; 74 if (gate_exists(gate)) { 75 if (gate->offset > limit) { 76 pr_err("%s: bad gate offset for %s (%u > %u)\n", 77 __func__, name, gate->offset, limit); 78 return false; 79 } 80 } 81 82 div = &peri->div; 83 if (divider_exists(div)) { 84 if (div->offset > limit) { 85 pr_err("%s: bad divider offset for %s (%u > %u)\n", 86 __func__, name, div->offset, limit); 87 return false; 88 } 89 } 90 91 div = &peri->pre_div; 92 if (divider_exists(div)) { 93 if (div->offset > limit) { 94 pr_err("%s: bad pre-divider offset for %s " 95 "(%u > %u)\n", 96 __func__, name, div->offset, limit); 97 return false; 98 } 99 } 100 101 sel = &peri->sel; 102 if (selector_exists(sel)) { 103 if (sel->offset > limit) { 104 pr_err("%s: bad selector offset for %s (%u > %u)\n", 105 __func__, name, sel->offset, limit); 106 return false; 107 } 108 } 109 110 trig = &peri->trig; 111 if (trigger_exists(trig)) { 112 if (trig->offset > limit) { 113 pr_err("%s: bad trigger offset for %s (%u > %u)\n", 114 __func__, name, trig->offset, limit); 115 return false; 116 } 117 } 118 119 trig = &peri->pre_trig; 120 if (trigger_exists(trig)) { 121 if (trig->offset > limit) { 122 pr_err("%s: bad pre-trigger offset for %s (%u > %u)\n", 123 __func__, name, trig->offset, limit); 124 return false; 125 } 126 } 127 128 return true; 129 } 130 131 /* A bit position must be less than the number of bits in a 32-bit register. */ 132 static bool bit_posn_valid(u32 bit_posn, const char *field_name, 133 const char *clock_name) 134 { 135 u32 limit = BITS_PER_BYTE * sizeof(u32) - 1; 136 137 if (bit_posn > limit) { 138 pr_err("%s: bad %s bit for %s (%u > %u)\n", __func__, 139 field_name, clock_name, bit_posn, limit); 140 return false; 141 } 142 return true; 143 } 144 145 /* 146 * A bitfield must be at least 1 bit wide. Both the low-order and 147 * high-order bits must lie within a 32-bit register. We require 148 * fields to be less than 32 bits wide, mainly because we use 149 * shifting to produce field masks, and shifting a full word width 150 * is not well-defined by the C standard. 151 */ 152 static bool bitfield_valid(u32 shift, u32 width, const char *field_name, 153 const char *clock_name) 154 { 155 u32 limit = BITS_PER_BYTE * sizeof(u32); 156 157 if (!width) { 158 pr_err("%s: bad %s field width 0 for %s\n", __func__, 159 field_name, clock_name); 160 return false; 161 } 162 if (shift + width > limit) { 163 pr_err("%s: bad %s for %s (%u + %u > %u)\n", __func__, 164 field_name, clock_name, shift, width, limit); 165 return false; 166 } 167 return true; 168 } 169 170 /* 171 * All gates, if defined, have a status bit, and for hardware-only 172 * gates, that's it. Gates that can be software controlled also 173 * have an enable bit. And a gate that can be hardware or software 174 * controlled will have a hardware/software select bit. 175 */ 176 static bool gate_valid(struct bcm_clk_gate *gate, const char *field_name, 177 const char *clock_name) 178 { 179 if (!bit_posn_valid(gate->status_bit, "gate status", clock_name)) 180 return false; 181 182 if (gate_is_sw_controllable(gate)) { 183 if (!bit_posn_valid(gate->en_bit, "gate enable", clock_name)) 184 return false; 185 186 if (gate_is_hw_controllable(gate)) { 187 if (!bit_posn_valid(gate->hw_sw_sel_bit, 188 "gate hw/sw select", 189 clock_name)) 190 return false; 191 } 192 } else { 193 BUG_ON(!gate_is_hw_controllable(gate)); 194 } 195 196 return true; 197 } 198 199 /* 200 * A selector bitfield must be valid. Its parent_sel array must 201 * also be reasonable for the field. 202 */ 203 static bool sel_valid(struct bcm_clk_sel *sel, const char *field_name, 204 const char *clock_name) 205 { 206 if (!bitfield_valid(sel->shift, sel->width, field_name, clock_name)) 207 return false; 208 209 if (sel->parent_count) { 210 u32 max_sel; 211 u32 limit; 212 213 /* 214 * Make sure the selector field can hold all the 215 * selector values we expect to be able to use. A 216 * clock only needs to have a selector defined if it 217 * has more than one parent. And in that case the 218 * highest selector value will be in the last entry 219 * in the array. 220 */ 221 max_sel = sel->parent_sel[sel->parent_count - 1]; 222 limit = (1 << sel->width) - 1; 223 if (max_sel > limit) { 224 pr_err("%s: bad selector for %s " 225 "(%u needs > %u bits)\n", 226 __func__, clock_name, max_sel, 227 sel->width); 228 return false; 229 } 230 } else { 231 pr_warn("%s: ignoring selector for %s (no parents)\n", 232 __func__, clock_name); 233 selector_clear_exists(sel); 234 kfree(sel->parent_sel); 235 sel->parent_sel = NULL; 236 } 237 238 return true; 239 } 240 241 /* 242 * A fixed divider just needs to be non-zero. A variable divider 243 * has to have a valid divider bitfield, and if it has a fraction, 244 * the width of the fraction must not be no more than the width of 245 * the divider as a whole. 246 */ 247 static bool div_valid(struct bcm_clk_div *div, const char *field_name, 248 const char *clock_name) 249 { 250 if (divider_is_fixed(div)) { 251 /* Any fixed divider value but 0 is OK */ 252 if (div->fixed == 0) { 253 pr_err("%s: bad %s fixed value 0 for %s\n", __func__, 254 field_name, clock_name); 255 return false; 256 } 257 return true; 258 } 259 if (!bitfield_valid(div->shift, div->width, field_name, clock_name)) 260 return false; 261 262 if (divider_has_fraction(div)) 263 if (div->frac_width > div->width) { 264 pr_warn("%s: bad %s fraction width for %s (%u > %u)\n", 265 __func__, field_name, clock_name, 266 div->frac_width, div->width); 267 return false; 268 } 269 270 return true; 271 } 272 273 /* 274 * If a clock has two dividers, the combined number of fractional 275 * bits must be representable in a 32-bit unsigned value. This 276 * is because we scale up a dividend using both dividers before 277 * dividing to improve accuracy, and we need to avoid overflow. 278 */ 279 static bool kona_dividers_valid(struct kona_clk *bcm_clk) 280 { 281 struct peri_clk_data *peri = bcm_clk->peri; 282 struct bcm_clk_div *div; 283 struct bcm_clk_div *pre_div; 284 u32 limit; 285 286 BUG_ON(bcm_clk->type != bcm_clk_peri); 287 288 if (!divider_exists(&peri->div) || !divider_exists(&peri->pre_div)) 289 return true; 290 291 div = &peri->div; 292 pre_div = &peri->pre_div; 293 if (divider_is_fixed(div) || divider_is_fixed(pre_div)) 294 return true; 295 296 limit = BITS_PER_BYTE * sizeof(u32); 297 298 return div->frac_width + pre_div->frac_width <= limit; 299 } 300 301 302 /* A trigger just needs to represent a valid bit position */ 303 static bool trig_valid(struct bcm_clk_trig *trig, const char *field_name, 304 const char *clock_name) 305 { 306 return bit_posn_valid(trig->bit, field_name, clock_name); 307 } 308 309 /* Determine whether the set of peripheral clock registers are valid. */ 310 static bool 311 peri_clk_data_valid(struct kona_clk *bcm_clk) 312 { 313 struct peri_clk_data *peri; 314 struct bcm_clk_gate *gate; 315 struct bcm_clk_sel *sel; 316 struct bcm_clk_div *div; 317 struct bcm_clk_div *pre_div; 318 struct bcm_clk_trig *trig; 319 const char *name; 320 321 BUG_ON(bcm_clk->type != bcm_clk_peri); 322 323 /* 324 * First validate register offsets. This is the only place 325 * where we need something from the ccu, so we do these 326 * together. 327 */ 328 if (!peri_clk_data_offsets_valid(bcm_clk)) 329 return false; 330 331 peri = bcm_clk->peri; 332 name = bcm_clk->name; 333 gate = &peri->gate; 334 if (gate_exists(gate) && !gate_valid(gate, "gate", name)) 335 return false; 336 337 sel = &peri->sel; 338 if (selector_exists(sel)) { 339 if (!sel_valid(sel, "selector", name)) 340 return false; 341 342 } else if (sel->parent_count > 1) { 343 pr_err("%s: multiple parents but no selector for %s\n", 344 __func__, name); 345 346 return false; 347 } 348 349 div = &peri->div; 350 pre_div = &peri->pre_div; 351 if (divider_exists(div)) { 352 if (!div_valid(div, "divider", name)) 353 return false; 354 355 if (divider_exists(pre_div)) 356 if (!div_valid(pre_div, "pre-divider", name)) 357 return false; 358 } else if (divider_exists(pre_div)) { 359 pr_err("%s: pre-divider but no divider for %s\n", __func__, 360 name); 361 return false; 362 } 363 364 trig = &peri->trig; 365 if (trigger_exists(trig)) { 366 if (!trig_valid(trig, "trigger", name)) 367 return false; 368 369 if (trigger_exists(&peri->pre_trig)) { 370 if (!trig_valid(trig, "pre-trigger", name)) { 371 return false; 372 } 373 } 374 if (!clk_requires_trigger(bcm_clk)) { 375 pr_warn("%s: ignoring trigger for %s (not needed)\n", 376 __func__, name); 377 trigger_clear_exists(trig); 378 } 379 } else if (trigger_exists(&peri->pre_trig)) { 380 pr_err("%s: pre-trigger but no trigger for %s\n", __func__, 381 name); 382 return false; 383 } else if (clk_requires_trigger(bcm_clk)) { 384 pr_err("%s: required trigger missing for %s\n", __func__, 385 name); 386 return false; 387 } 388 389 return kona_dividers_valid(bcm_clk); 390 } 391 392 static bool kona_clk_valid(struct kona_clk *bcm_clk) 393 { 394 switch (bcm_clk->type) { 395 case bcm_clk_peri: 396 if (!peri_clk_data_valid(bcm_clk)) 397 return false; 398 break; 399 default: 400 pr_err("%s: unrecognized clock type (%d)\n", __func__, 401 (int)bcm_clk->type); 402 return false; 403 } 404 return true; 405 } 406 407 /* 408 * Scan an array of parent clock names to determine whether there 409 * are any entries containing BAD_CLK_NAME. Such entries are 410 * placeholders for non-supported clocks. Keep track of the 411 * position of each clock name in the original array. 412 * 413 * Allocates an array of pointers to to hold the names of all 414 * non-null entries in the original array, and returns a pointer to 415 * that array in *names. This will be used for registering the 416 * clock with the common clock code. On successful return, 417 * *count indicates how many entries are in that names array. 418 * 419 * If there is more than one entry in the resulting names array, 420 * another array is allocated to record the parent selector value 421 * for each (defined) parent clock. This is the value that 422 * represents this parent clock in the clock's source selector 423 * register. The position of the clock in the original parent array 424 * defines that selector value. The number of entries in this array 425 * is the same as the number of entries in the parent names array. 426 * 427 * The array of selector values is returned. If the clock has no 428 * parents, no selector is required and a null pointer is returned. 429 * 430 * Returns a null pointer if the clock names array supplied was 431 * null. (This is not an error.) 432 * 433 * Returns a pointer-coded error if an error occurs. 434 */ 435 static u32 *parent_process(const char *clocks[], 436 u32 *count, const char ***names) 437 { 438 static const char **parent_names; 439 static u32 *parent_sel; 440 const char **clock; 441 u32 parent_count; 442 u32 bad_count = 0; 443 u32 orig_count; 444 u32 i; 445 u32 j; 446 447 *count = 0; /* In case of early return */ 448 *names = NULL; 449 if (!clocks) 450 return NULL; 451 452 /* 453 * Count the number of names in the null-terminated array, 454 * and find out how many of those are actually clock names. 455 */ 456 for (clock = clocks; *clock; clock++) 457 if (*clock == BAD_CLK_NAME) 458 bad_count++; 459 orig_count = (u32)(clock - clocks); 460 parent_count = orig_count - bad_count; 461 462 /* If all clocks are unsupported, we treat it as no clock */ 463 if (!parent_count) 464 return NULL; 465 466 /* Avoid exceeding our parent clock limit */ 467 if (parent_count > PARENT_COUNT_MAX) { 468 pr_err("%s: too many parents (%u > %u)\n", __func__, 469 parent_count, PARENT_COUNT_MAX); 470 return ERR_PTR(-EINVAL); 471 } 472 473 /* 474 * There is one parent name for each defined parent clock. 475 * We also maintain an array containing the selector value 476 * for each defined clock. If there's only one clock, the 477 * selector is not required, but we allocate space for the 478 * array anyway to keep things simple. 479 */ 480 parent_names = kmalloc(parent_count * sizeof(parent_names), GFP_KERNEL); 481 if (!parent_names) { 482 pr_err("%s: error allocating %u parent names\n", __func__, 483 parent_count); 484 return ERR_PTR(-ENOMEM); 485 } 486 487 /* There is at least one parent, so allocate a selector array */ 488 489 parent_sel = kmalloc(parent_count * sizeof(*parent_sel), GFP_KERNEL); 490 if (!parent_sel) { 491 pr_err("%s: error allocating %u parent selectors\n", __func__, 492 parent_count); 493 kfree(parent_names); 494 495 return ERR_PTR(-ENOMEM); 496 } 497 498 /* Now fill in the parent names and selector arrays */ 499 for (i = 0, j = 0; i < orig_count; i++) { 500 if (clocks[i] != BAD_CLK_NAME) { 501 parent_names[j] = clocks[i]; 502 parent_sel[j] = i; 503 j++; 504 } 505 } 506 *names = parent_names; 507 *count = parent_count; 508 509 return parent_sel; 510 } 511 512 static int 513 clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel, 514 struct clk_init_data *init_data) 515 { 516 const char **parent_names = NULL; 517 u32 parent_count = 0; 518 u32 *parent_sel; 519 520 /* 521 * If a peripheral clock has multiple parents, the value 522 * used by the hardware to select that parent is represented 523 * by the parent clock's position in the "clocks" list. Some 524 * values don't have defined or supported clocks; these will 525 * have BAD_CLK_NAME entries in the parents[] array. The 526 * list is terminated by a NULL entry. 527 * 528 * We need to supply (only) the names of defined parent 529 * clocks when registering a clock though, so we use an 530 * array of parent selector values to map between the 531 * indexes the common clock code uses and the selector 532 * values we need. 533 */ 534 parent_sel = parent_process(clocks, &parent_count, &parent_names); 535 if (IS_ERR(parent_sel)) { 536 int ret = PTR_ERR(parent_sel); 537 538 pr_err("%s: error processing parent clocks for %s (%d)\n", 539 __func__, init_data->name, ret); 540 541 return ret; 542 } 543 544 init_data->parent_names = parent_names; 545 init_data->num_parents = parent_count; 546 547 sel->parent_count = parent_count; 548 sel->parent_sel = parent_sel; 549 550 return 0; 551 } 552 553 static void clk_sel_teardown(struct bcm_clk_sel *sel, 554 struct clk_init_data *init_data) 555 { 556 kfree(sel->parent_sel); 557 sel->parent_sel = NULL; 558 sel->parent_count = 0; 559 560 init_data->num_parents = 0; 561 kfree(init_data->parent_names); 562 init_data->parent_names = NULL; 563 } 564 565 static void peri_clk_teardown(struct peri_clk_data *data, 566 struct clk_init_data *init_data) 567 { 568 clk_sel_teardown(&data->sel, init_data); 569 init_data->ops = NULL; 570 } 571 572 /* 573 * Caller is responsible for freeing the parent_names[] and 574 * parent_sel[] arrays in the peripheral clock's "data" structure 575 * that can be assigned if the clock has one or more parent clocks 576 * associated with it. 577 */ 578 static int peri_clk_setup(struct ccu_data *ccu, struct peri_clk_data *data, 579 struct clk_init_data *init_data) 580 { 581 init_data->ops = &kona_peri_clk_ops; 582 init_data->flags = CLK_IGNORE_UNUSED; 583 584 return clk_sel_setup(data->clocks, &data->sel, init_data); 585 } 586 587 static void bcm_clk_teardown(struct kona_clk *bcm_clk) 588 { 589 switch (bcm_clk->type) { 590 case bcm_clk_peri: 591 peri_clk_teardown(bcm_clk->data, &bcm_clk->init_data); 592 break; 593 default: 594 break; 595 } 596 bcm_clk->data = NULL; 597 bcm_clk->type = bcm_clk_none; 598 } 599 600 static void kona_clk_teardown(struct clk *clk) 601 { 602 struct clk_hw *hw; 603 struct kona_clk *bcm_clk; 604 605 if (!clk) 606 return; 607 608 hw = __clk_get_hw(clk); 609 if (!hw) { 610 pr_err("%s: clk %p has null hw pointer\n", __func__, clk); 611 return; 612 } 613 clk_unregister(clk); 614 615 bcm_clk = to_kona_clk(hw); 616 bcm_clk_teardown(bcm_clk); 617 } 618 619 struct clk *kona_clk_setup(struct ccu_data *ccu, const char *name, 620 enum bcm_clk_type type, void *data) 621 { 622 struct kona_clk *bcm_clk; 623 struct clk_init_data *init_data; 624 struct clk *clk = NULL; 625 626 bcm_clk = kzalloc(sizeof(*bcm_clk), GFP_KERNEL); 627 if (!bcm_clk) { 628 pr_err("%s: failed to allocate bcm_clk for %s\n", __func__, 629 name); 630 return NULL; 631 } 632 bcm_clk->ccu = ccu; 633 bcm_clk->name = name; 634 635 init_data = &bcm_clk->init_data; 636 init_data->name = name; 637 switch (type) { 638 case bcm_clk_peri: 639 if (peri_clk_setup(ccu, data, init_data)) 640 goto out_free; 641 break; 642 default: 643 data = NULL; 644 break; 645 } 646 bcm_clk->type = type; 647 bcm_clk->data = data; 648 649 /* Make sure everything makes sense before we set it up */ 650 if (!kona_clk_valid(bcm_clk)) { 651 pr_err("%s: clock data invalid for %s\n", __func__, name); 652 goto out_teardown; 653 } 654 655 bcm_clk->hw.init = init_data; 656 clk = clk_register(NULL, &bcm_clk->hw); 657 if (IS_ERR(clk)) { 658 pr_err("%s: error registering clock %s (%ld)\n", __func__, 659 name, PTR_ERR(clk)); 660 goto out_teardown; 661 } 662 BUG_ON(!clk); 663 664 return clk; 665 out_teardown: 666 bcm_clk_teardown(bcm_clk); 667 out_free: 668 kfree(bcm_clk); 669 670 return NULL; 671 } 672 673 static void ccu_clks_teardown(struct ccu_data *ccu) 674 { 675 u32 i; 676 677 for (i = 0; i < ccu->data.clk_num; i++) 678 kona_clk_teardown(ccu->data.clks[i]); 679 kfree(ccu->data.clks); 680 } 681 682 static void kona_ccu_teardown(struct ccu_data *ccu) 683 { 684 if (!ccu) 685 return; 686 687 if (!ccu->base) 688 goto done; 689 690 of_clk_del_provider(ccu->node); /* safe if never added */ 691 ccu_clks_teardown(ccu); 692 list_del(&ccu->links); 693 of_node_put(ccu->node); 694 iounmap(ccu->base); 695 done: 696 kfree(ccu->name); 697 kfree(ccu); 698 } 699 700 /* 701 * Set up a CCU. Call the provided ccu_clks_setup callback to 702 * initialize the array of clocks provided by the CCU. 703 */ 704 void __init kona_dt_ccu_setup(struct device_node *node, 705 int (*ccu_clks_setup)(struct ccu_data *)) 706 { 707 struct ccu_data *ccu; 708 struct resource res = { 0 }; 709 resource_size_t range; 710 int ret; 711 712 ccu = kzalloc(sizeof(*ccu), GFP_KERNEL); 713 if (ccu) 714 ccu->name = kstrdup(node->name, GFP_KERNEL); 715 if (!ccu || !ccu->name) { 716 pr_err("%s: unable to allocate CCU struct for %s\n", 717 __func__, node->name); 718 kfree(ccu); 719 720 return; 721 } 722 723 ret = of_address_to_resource(node, 0, &res); 724 if (ret) { 725 pr_err("%s: no valid CCU registers found for %s\n", __func__, 726 node->name); 727 goto out_err; 728 } 729 730 range = resource_size(&res); 731 if (range > (resource_size_t)U32_MAX) { 732 pr_err("%s: address range too large for %s\n", __func__, 733 node->name); 734 goto out_err; 735 } 736 737 ccu->range = (u32)range; 738 ccu->base = ioremap(res.start, ccu->range); 739 if (!ccu->base) { 740 pr_err("%s: unable to map CCU registers for %s\n", __func__, 741 node->name); 742 goto out_err; 743 } 744 745 spin_lock_init(&ccu->lock); 746 INIT_LIST_HEAD(&ccu->links); 747 ccu->node = of_node_get(node); 748 749 list_add_tail(&ccu->links, &ccu_list); 750 751 /* Set up clocks array (in ccu->data) */ 752 if (ccu_clks_setup(ccu)) 753 goto out_err; 754 755 ret = of_clk_add_provider(node, of_clk_src_onecell_get, &ccu->data); 756 if (ret) { 757 pr_err("%s: error adding ccu %s as provider (%d)\n", __func__, 758 node->name, ret); 759 goto out_err; 760 } 761 762 if (!kona_ccu_init(ccu)) 763 pr_err("Broadcom %s initialization had errors\n", node->name); 764 765 return; 766 out_err: 767 kona_ccu_teardown(ccu); 768 pr_err("Broadcom %s setup aborted\n", node->name); 769 } 770