1 /* 2 * ti-sysc.c - Texas Instruments sysc interconnect target driver 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 9 * kind, whether express or implied; without even the implied warranty 10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/io.h> 15 #include <linux/clk.h> 16 #include <linux/clkdev.h> 17 #include <linux/delay.h> 18 #include <linux/module.h> 19 #include <linux/platform_device.h> 20 #include <linux/pm_domain.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/reset.h> 23 #include <linux/of_address.h> 24 #include <linux/of_platform.h> 25 #include <linux/slab.h> 26 #include <linux/iopoll.h> 27 28 #include <linux/platform_data/ti-sysc.h> 29 30 #include <dt-bindings/bus/ti-sysc.h> 31 32 #define MAX_MODULE_SOFTRESET_WAIT 10000 33 34 static const char * const reg_names[] = { "rev", "sysc", "syss", }; 35 36 enum sysc_clocks { 37 SYSC_FCK, 38 SYSC_ICK, 39 SYSC_OPTFCK0, 40 SYSC_OPTFCK1, 41 SYSC_OPTFCK2, 42 SYSC_OPTFCK3, 43 SYSC_OPTFCK4, 44 SYSC_OPTFCK5, 45 SYSC_OPTFCK6, 46 SYSC_OPTFCK7, 47 SYSC_MAX_CLOCKS, 48 }; 49 50 static const char * const clock_names[SYSC_MAX_CLOCKS] = { 51 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4", 52 "opt5", "opt6", "opt7", 53 }; 54 55 #define SYSC_IDLEMODE_MASK 3 56 #define SYSC_CLOCKACTIVITY_MASK 3 57 58 /** 59 * struct sysc - TI sysc interconnect target module registers and capabilities 60 * @dev: struct device pointer 61 * @module_pa: physical address of the interconnect target module 62 * @module_size: size of the interconnect target module 63 * @module_va: virtual address of the interconnect target module 64 * @offsets: register offsets from module base 65 * @clocks: clocks used by the interconnect target module 66 * @clock_roles: clock role names for the found clocks 67 * @nr_clocks: number of clocks used by the interconnect target module 68 * @legacy_mode: configured for legacy mode if set 69 * @cap: interconnect target module capabilities 70 * @cfg: interconnect target module configuration 71 * @name: name if available 72 * @revision: interconnect target module revision 73 * @needs_resume: runtime resume needed on resume from suspend 74 */ 75 struct sysc { 76 struct device *dev; 77 u64 module_pa; 78 u32 module_size; 79 void __iomem *module_va; 80 int offsets[SYSC_MAX_REGS]; 81 struct ti_sysc_module_data *mdata; 82 struct clk **clocks; 83 const char **clock_roles; 84 int nr_clocks; 85 struct reset_control *rsts; 86 const char *legacy_mode; 87 const struct sysc_capabilities *cap; 88 struct sysc_config cfg; 89 struct ti_sysc_cookie cookie; 90 const char *name; 91 u32 revision; 92 bool enabled; 93 bool needs_resume; 94 bool child_needs_resume; 95 struct delayed_work idle_work; 96 }; 97 98 static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np, 99 bool is_child); 100 101 static void sysc_write(struct sysc *ddata, int offset, u32 value) 102 { 103 writel_relaxed(value, ddata->module_va + offset); 104 } 105 106 static u32 sysc_read(struct sysc *ddata, int offset) 107 { 108 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) { 109 u32 val; 110 111 val = readw_relaxed(ddata->module_va + offset); 112 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16); 113 114 return val; 115 } 116 117 return readl_relaxed(ddata->module_va + offset); 118 } 119 120 static bool sysc_opt_clks_needed(struct sysc *ddata) 121 { 122 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED); 123 } 124 125 static u32 sysc_read_revision(struct sysc *ddata) 126 { 127 int offset = ddata->offsets[SYSC_REVISION]; 128 129 if (offset < 0) 130 return 0; 131 132 return sysc_read(ddata, offset); 133 } 134 135 static int sysc_add_named_clock_from_child(struct sysc *ddata, 136 const char *name, 137 const char *optfck_name) 138 { 139 struct device_node *np = ddata->dev->of_node; 140 struct device_node *child; 141 struct clk_lookup *cl; 142 struct clk *clock; 143 const char *n; 144 145 if (name) 146 n = name; 147 else 148 n = optfck_name; 149 150 /* Does the clock alias already exist? */ 151 clock = of_clk_get_by_name(np, n); 152 if (!IS_ERR(clock)) { 153 clk_put(clock); 154 155 return 0; 156 } 157 158 child = of_get_next_available_child(np, NULL); 159 if (!child) 160 return -ENODEV; 161 162 clock = devm_get_clk_from_child(ddata->dev, child, name); 163 if (IS_ERR(clock)) 164 return PTR_ERR(clock); 165 166 /* 167 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID 168 * limit for clk_get(). If cl ever needs to be freed, it should be done 169 * with clkdev_drop(). 170 */ 171 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL); 172 if (!cl) 173 return -ENOMEM; 174 175 cl->con_id = n; 176 cl->dev_id = dev_name(ddata->dev); 177 cl->clk = clock; 178 clkdev_add(cl); 179 180 clk_put(clock); 181 182 return 0; 183 } 184 185 static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name) 186 { 187 const char *optfck_name; 188 int error, index; 189 190 if (ddata->nr_clocks < SYSC_OPTFCK0) 191 index = SYSC_OPTFCK0; 192 else 193 index = ddata->nr_clocks; 194 195 if (name) 196 optfck_name = name; 197 else 198 optfck_name = clock_names[index]; 199 200 error = sysc_add_named_clock_from_child(ddata, name, optfck_name); 201 if (error) 202 return error; 203 204 ddata->clock_roles[index] = optfck_name; 205 ddata->nr_clocks++; 206 207 return 0; 208 } 209 210 static int sysc_get_one_clock(struct sysc *ddata, const char *name) 211 { 212 int error, i, index = -ENODEV; 213 214 if (!strncmp(clock_names[SYSC_FCK], name, 3)) 215 index = SYSC_FCK; 216 else if (!strncmp(clock_names[SYSC_ICK], name, 3)) 217 index = SYSC_ICK; 218 219 if (index < 0) { 220 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) { 221 if (!ddata->clocks[i]) { 222 index = i; 223 break; 224 } 225 } 226 } 227 228 if (index < 0) { 229 dev_err(ddata->dev, "clock %s not added\n", name); 230 return index; 231 } 232 233 ddata->clocks[index] = devm_clk_get(ddata->dev, name); 234 if (IS_ERR(ddata->clocks[index])) { 235 if (PTR_ERR(ddata->clocks[index]) == -ENOENT) 236 return 0; 237 238 dev_err(ddata->dev, "clock get error for %s: %li\n", 239 name, PTR_ERR(ddata->clocks[index])); 240 241 return PTR_ERR(ddata->clocks[index]); 242 } 243 244 error = clk_prepare(ddata->clocks[index]); 245 if (error) { 246 dev_err(ddata->dev, "clock prepare error for %s: %i\n", 247 name, error); 248 249 return error; 250 } 251 252 return 0; 253 } 254 255 static int sysc_get_clocks(struct sysc *ddata) 256 { 257 struct device_node *np = ddata->dev->of_node; 258 struct property *prop; 259 const char *name; 260 int nr_fck = 0, nr_ick = 0, i, error = 0; 261 262 ddata->clock_roles = devm_kcalloc(ddata->dev, 263 SYSC_MAX_CLOCKS, 264 sizeof(*ddata->clock_roles), 265 GFP_KERNEL); 266 if (!ddata->clock_roles) 267 return -ENOMEM; 268 269 of_property_for_each_string(np, "clock-names", prop, name) { 270 if (!strncmp(clock_names[SYSC_FCK], name, 3)) 271 nr_fck++; 272 if (!strncmp(clock_names[SYSC_ICK], name, 3)) 273 nr_ick++; 274 ddata->clock_roles[ddata->nr_clocks] = name; 275 ddata->nr_clocks++; 276 } 277 278 if (ddata->nr_clocks < 1) 279 return 0; 280 281 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) { 282 error = sysc_init_ext_opt_clock(ddata, NULL); 283 if (error) 284 return error; 285 } 286 287 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) { 288 dev_err(ddata->dev, "too many clocks for %pOF\n", np); 289 290 return -EINVAL; 291 } 292 293 if (nr_fck > 1 || nr_ick > 1) { 294 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np); 295 296 return -EINVAL; 297 } 298 299 ddata->clocks = devm_kcalloc(ddata->dev, 300 ddata->nr_clocks, sizeof(*ddata->clocks), 301 GFP_KERNEL); 302 if (!ddata->clocks) 303 return -ENOMEM; 304 305 for (i = 0; i < SYSC_MAX_CLOCKS; i++) { 306 const char *name = ddata->clock_roles[i]; 307 308 if (!name) 309 continue; 310 311 error = sysc_get_one_clock(ddata, name); 312 if (error && error != -ENOENT) 313 return error; 314 } 315 316 return 0; 317 } 318 319 static int sysc_enable_main_clocks(struct sysc *ddata) 320 { 321 struct clk *clock; 322 int i, error; 323 324 if (!ddata->clocks) 325 return 0; 326 327 for (i = 0; i < SYSC_OPTFCK0; i++) { 328 clock = ddata->clocks[i]; 329 330 /* Main clocks may not have ick */ 331 if (IS_ERR_OR_NULL(clock)) 332 continue; 333 334 error = clk_enable(clock); 335 if (error) 336 goto err_disable; 337 } 338 339 return 0; 340 341 err_disable: 342 for (i--; i >= 0; i--) { 343 clock = ddata->clocks[i]; 344 345 /* Main clocks may not have ick */ 346 if (IS_ERR_OR_NULL(clock)) 347 continue; 348 349 clk_disable(clock); 350 } 351 352 return error; 353 } 354 355 static void sysc_disable_main_clocks(struct sysc *ddata) 356 { 357 struct clk *clock; 358 int i; 359 360 if (!ddata->clocks) 361 return; 362 363 for (i = 0; i < SYSC_OPTFCK0; i++) { 364 clock = ddata->clocks[i]; 365 if (IS_ERR_OR_NULL(clock)) 366 continue; 367 368 clk_disable(clock); 369 } 370 } 371 372 static int sysc_enable_opt_clocks(struct sysc *ddata) 373 { 374 struct clk *clock; 375 int i, error; 376 377 if (!ddata->clocks) 378 return 0; 379 380 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) { 381 clock = ddata->clocks[i]; 382 383 /* Assume no holes for opt clocks */ 384 if (IS_ERR_OR_NULL(clock)) 385 return 0; 386 387 error = clk_enable(clock); 388 if (error) 389 goto err_disable; 390 } 391 392 return 0; 393 394 err_disable: 395 for (i--; i >= 0; i--) { 396 clock = ddata->clocks[i]; 397 if (IS_ERR_OR_NULL(clock)) 398 continue; 399 400 clk_disable(clock); 401 } 402 403 return error; 404 } 405 406 static void sysc_disable_opt_clocks(struct sysc *ddata) 407 { 408 struct clk *clock; 409 int i; 410 411 if (!ddata->clocks) 412 return; 413 414 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) { 415 clock = ddata->clocks[i]; 416 417 /* Assume no holes for opt clocks */ 418 if (IS_ERR_OR_NULL(clock)) 419 return; 420 421 clk_disable(clock); 422 } 423 } 424 425 /** 426 * sysc_init_resets - init rstctrl reset line if configured 427 * @ddata: device driver data 428 * 429 * See sysc_rstctrl_reset_deassert(). 430 */ 431 static int sysc_init_resets(struct sysc *ddata) 432 { 433 ddata->rsts = 434 devm_reset_control_array_get_optional_exclusive(ddata->dev); 435 if (IS_ERR(ddata->rsts)) 436 return PTR_ERR(ddata->rsts); 437 438 return 0; 439 } 440 441 /** 442 * sysc_parse_and_check_child_range - parses module IO region from ranges 443 * @ddata: device driver data 444 * 445 * In general we only need rev, syss, and sysc registers and not the whole 446 * module range. But we do want the offsets for these registers from the 447 * module base. This allows us to check them against the legacy hwmod 448 * platform data. Let's also check the ranges are configured properly. 449 */ 450 static int sysc_parse_and_check_child_range(struct sysc *ddata) 451 { 452 struct device_node *np = ddata->dev->of_node; 453 const __be32 *ranges; 454 u32 nr_addr, nr_size; 455 int len, error; 456 457 ranges = of_get_property(np, "ranges", &len); 458 if (!ranges) { 459 dev_err(ddata->dev, "missing ranges for %pOF\n", np); 460 461 return -ENOENT; 462 } 463 464 len /= sizeof(*ranges); 465 466 if (len < 3) { 467 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np); 468 469 return -EINVAL; 470 } 471 472 error = of_property_read_u32(np, "#address-cells", &nr_addr); 473 if (error) 474 return -ENOENT; 475 476 error = of_property_read_u32(np, "#size-cells", &nr_size); 477 if (error) 478 return -ENOENT; 479 480 if (nr_addr != 1 || nr_size != 1) { 481 dev_err(ddata->dev, "invalid ranges for %pOF\n", np); 482 483 return -EINVAL; 484 } 485 486 ranges++; 487 ddata->module_pa = of_translate_address(np, ranges++); 488 ddata->module_size = be32_to_cpup(ranges); 489 490 return 0; 491 } 492 493 static struct device_node *stdout_path; 494 495 static void sysc_init_stdout_path(struct sysc *ddata) 496 { 497 struct device_node *np = NULL; 498 const char *uart; 499 500 if (IS_ERR(stdout_path)) 501 return; 502 503 if (stdout_path) 504 return; 505 506 np = of_find_node_by_path("/chosen"); 507 if (!np) 508 goto err; 509 510 uart = of_get_property(np, "stdout-path", NULL); 511 if (!uart) 512 goto err; 513 514 np = of_find_node_by_path(uart); 515 if (!np) 516 goto err; 517 518 stdout_path = np; 519 520 return; 521 522 err: 523 stdout_path = ERR_PTR(-ENODEV); 524 } 525 526 static void sysc_check_quirk_stdout(struct sysc *ddata, 527 struct device_node *np) 528 { 529 sysc_init_stdout_path(ddata); 530 if (np != stdout_path) 531 return; 532 533 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT | 534 SYSC_QUIRK_NO_RESET_ON_INIT; 535 } 536 537 /** 538 * sysc_check_one_child - check child configuration 539 * @ddata: device driver data 540 * @np: child device node 541 * 542 * Let's avoid messy situations where we have new interconnect target 543 * node but children have "ti,hwmods". These belong to the interconnect 544 * target node and are managed by this driver. 545 */ 546 static int sysc_check_one_child(struct sysc *ddata, 547 struct device_node *np) 548 { 549 const char *name; 550 551 name = of_get_property(np, "ti,hwmods", NULL); 552 if (name) 553 dev_warn(ddata->dev, "really a child ti,hwmods property?"); 554 555 sysc_check_quirk_stdout(ddata, np); 556 sysc_parse_dts_quirks(ddata, np, true); 557 558 return 0; 559 } 560 561 static int sysc_check_children(struct sysc *ddata) 562 { 563 struct device_node *child; 564 int error; 565 566 for_each_child_of_node(ddata->dev->of_node, child) { 567 error = sysc_check_one_child(ddata, child); 568 if (error) 569 return error; 570 } 571 572 return 0; 573 } 574 575 /* 576 * So far only I2C uses 16-bit read access with clockactivity with revision 577 * in two registers with stride of 4. We can detect this based on the rev 578 * register size to configure things far enough to be able to properly read 579 * the revision register. 580 */ 581 static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res) 582 { 583 if (resource_size(res) == 8) 584 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT; 585 } 586 587 /** 588 * sysc_parse_one - parses the interconnect target module registers 589 * @ddata: device driver data 590 * @reg: register to parse 591 */ 592 static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg) 593 { 594 struct resource *res; 595 const char *name; 596 597 switch (reg) { 598 case SYSC_REVISION: 599 case SYSC_SYSCONFIG: 600 case SYSC_SYSSTATUS: 601 name = reg_names[reg]; 602 break; 603 default: 604 return -EINVAL; 605 } 606 607 res = platform_get_resource_byname(to_platform_device(ddata->dev), 608 IORESOURCE_MEM, name); 609 if (!res) { 610 ddata->offsets[reg] = -ENODEV; 611 612 return 0; 613 } 614 615 ddata->offsets[reg] = res->start - ddata->module_pa; 616 if (reg == SYSC_REVISION) 617 sysc_check_quirk_16bit(ddata, res); 618 619 return 0; 620 } 621 622 static int sysc_parse_registers(struct sysc *ddata) 623 { 624 int i, error; 625 626 for (i = 0; i < SYSC_MAX_REGS; i++) { 627 error = sysc_parse_one(ddata, i); 628 if (error) 629 return error; 630 } 631 632 return 0; 633 } 634 635 /** 636 * sysc_check_registers - check for misconfigured register overlaps 637 * @ddata: device driver data 638 */ 639 static int sysc_check_registers(struct sysc *ddata) 640 { 641 int i, j, nr_regs = 0, nr_matches = 0; 642 643 for (i = 0; i < SYSC_MAX_REGS; i++) { 644 if (ddata->offsets[i] < 0) 645 continue; 646 647 if (ddata->offsets[i] > (ddata->module_size - 4)) { 648 dev_err(ddata->dev, "register outside module range"); 649 650 return -EINVAL; 651 } 652 653 for (j = 0; j < SYSC_MAX_REGS; j++) { 654 if (ddata->offsets[j] < 0) 655 continue; 656 657 if (ddata->offsets[i] == ddata->offsets[j]) 658 nr_matches++; 659 } 660 nr_regs++; 661 } 662 663 if (nr_regs < 1) { 664 dev_err(ddata->dev, "missing registers\n"); 665 666 return -EINVAL; 667 } 668 669 if (nr_matches > nr_regs) { 670 dev_err(ddata->dev, "overlapping registers: (%i/%i)", 671 nr_regs, nr_matches); 672 673 return -EINVAL; 674 } 675 676 return 0; 677 } 678 679 /** 680 * syc_ioremap - ioremap register space for the interconnect target module 681 * @ddata: device driver data 682 * 683 * Note that the interconnect target module registers can be anywhere 684 * within the interconnect target module range. For example, SGX has 685 * them at offset 0x1fc00 in the 32MB module address space. And cpsw 686 * has them at offset 0x1200 in the CPSW_WR child. Usually the 687 * the interconnect target module registers are at the beginning of 688 * the module range though. 689 */ 690 static int sysc_ioremap(struct sysc *ddata) 691 { 692 int size; 693 694 size = max3(ddata->offsets[SYSC_REVISION], 695 ddata->offsets[SYSC_SYSCONFIG], 696 ddata->offsets[SYSC_SYSSTATUS]); 697 698 if (size < 0 || (size + sizeof(u32)) > ddata->module_size) 699 return -EINVAL; 700 701 ddata->module_va = devm_ioremap(ddata->dev, 702 ddata->module_pa, 703 size + sizeof(u32)); 704 if (!ddata->module_va) 705 return -EIO; 706 707 return 0; 708 } 709 710 /** 711 * sysc_map_and_check_registers - ioremap and check device registers 712 * @ddata: device driver data 713 */ 714 static int sysc_map_and_check_registers(struct sysc *ddata) 715 { 716 int error; 717 718 error = sysc_parse_and_check_child_range(ddata); 719 if (error) 720 return error; 721 722 error = sysc_check_children(ddata); 723 if (error) 724 return error; 725 726 error = sysc_parse_registers(ddata); 727 if (error) 728 return error; 729 730 error = sysc_ioremap(ddata); 731 if (error) 732 return error; 733 734 error = sysc_check_registers(ddata); 735 if (error) 736 return error; 737 738 return 0; 739 } 740 741 /** 742 * sysc_show_rev - read and show interconnect target module revision 743 * @bufp: buffer to print the information to 744 * @ddata: device driver data 745 */ 746 static int sysc_show_rev(char *bufp, struct sysc *ddata) 747 { 748 int len; 749 750 if (ddata->offsets[SYSC_REVISION] < 0) 751 return sprintf(bufp, ":NA"); 752 753 len = sprintf(bufp, ":%08x", ddata->revision); 754 755 return len; 756 } 757 758 static int sysc_show_reg(struct sysc *ddata, 759 char *bufp, enum sysc_registers reg) 760 { 761 if (ddata->offsets[reg] < 0) 762 return sprintf(bufp, ":NA"); 763 764 return sprintf(bufp, ":%x", ddata->offsets[reg]); 765 } 766 767 static int sysc_show_name(char *bufp, struct sysc *ddata) 768 { 769 if (!ddata->name) 770 return 0; 771 772 return sprintf(bufp, ":%s", ddata->name); 773 } 774 775 /** 776 * sysc_show_registers - show information about interconnect target module 777 * @ddata: device driver data 778 */ 779 static void sysc_show_registers(struct sysc *ddata) 780 { 781 char buf[128]; 782 char *bufp = buf; 783 int i; 784 785 for (i = 0; i < SYSC_MAX_REGS; i++) 786 bufp += sysc_show_reg(ddata, bufp, i); 787 788 bufp += sysc_show_rev(bufp, ddata); 789 bufp += sysc_show_name(bufp, ddata); 790 791 dev_dbg(ddata->dev, "%llx:%x%s\n", 792 ddata->module_pa, ddata->module_size, 793 buf); 794 } 795 796 #define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1) 797 798 static int sysc_enable_module(struct device *dev) 799 { 800 struct sysc *ddata; 801 const struct sysc_regbits *regbits; 802 u32 reg, idlemodes, best_mode; 803 804 ddata = dev_get_drvdata(dev); 805 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV) 806 return 0; 807 808 /* 809 * TODO: Need to prevent clockdomain autoidle? 810 * See clkdm_deny_idle() in arch/mach-omap2/omap_hwmod.c 811 */ 812 813 regbits = ddata->cap->regbits; 814 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]); 815 816 /* Set SIDLE mode */ 817 idlemodes = ddata->cfg.sidlemodes; 818 if (!idlemodes || regbits->sidle_shift < 0) 819 goto set_midle; 820 821 best_mode = fls(ddata->cfg.sidlemodes) - 1; 822 if (best_mode > SYSC_IDLE_MASK) { 823 dev_err(dev, "%s: invalid sidlemode\n", __func__); 824 return -EINVAL; 825 } 826 827 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift); 828 reg |= best_mode << regbits->sidle_shift; 829 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg); 830 831 set_midle: 832 /* Set MIDLE mode */ 833 idlemodes = ddata->cfg.midlemodes; 834 if (!idlemodes || regbits->midle_shift < 0) 835 return 0; 836 837 best_mode = fls(ddata->cfg.midlemodes) - 1; 838 if (best_mode > SYSC_IDLE_MASK) { 839 dev_err(dev, "%s: invalid midlemode\n", __func__); 840 return -EINVAL; 841 } 842 843 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift); 844 reg |= best_mode << regbits->midle_shift; 845 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg); 846 847 return 0; 848 } 849 850 static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode) 851 { 852 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP)) 853 *best_mode = SYSC_IDLE_SMART_WKUP; 854 else if (idlemodes & BIT(SYSC_IDLE_SMART)) 855 *best_mode = SYSC_IDLE_SMART; 856 else if (idlemodes & SYSC_IDLE_FORCE) 857 *best_mode = SYSC_IDLE_FORCE; 858 else 859 return -EINVAL; 860 861 return 0; 862 } 863 864 static int sysc_disable_module(struct device *dev) 865 { 866 struct sysc *ddata; 867 const struct sysc_regbits *regbits; 868 u32 reg, idlemodes, best_mode; 869 int ret; 870 871 ddata = dev_get_drvdata(dev); 872 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV) 873 return 0; 874 875 /* 876 * TODO: Need to prevent clockdomain autoidle? 877 * See clkdm_deny_idle() in arch/mach-omap2/omap_hwmod.c 878 */ 879 880 regbits = ddata->cap->regbits; 881 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]); 882 883 /* Set MIDLE mode */ 884 idlemodes = ddata->cfg.midlemodes; 885 if (!idlemodes || regbits->midle_shift < 0) 886 goto set_sidle; 887 888 ret = sysc_best_idle_mode(idlemodes, &best_mode); 889 if (ret) { 890 dev_err(dev, "%s: invalid midlemode\n", __func__); 891 return ret; 892 } 893 894 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift); 895 reg |= best_mode << regbits->midle_shift; 896 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg); 897 898 set_sidle: 899 /* Set SIDLE mode */ 900 idlemodes = ddata->cfg.sidlemodes; 901 if (!idlemodes || regbits->sidle_shift < 0) 902 return 0; 903 904 ret = sysc_best_idle_mode(idlemodes, &best_mode); 905 if (ret) { 906 dev_err(dev, "%s: invalid sidlemode\n", __func__); 907 return ret; 908 } 909 910 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift); 911 reg |= best_mode << regbits->sidle_shift; 912 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg); 913 914 return 0; 915 } 916 917 static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev, 918 struct sysc *ddata) 919 { 920 struct ti_sysc_platform_data *pdata; 921 int error; 922 923 pdata = dev_get_platdata(ddata->dev); 924 if (!pdata) 925 return 0; 926 927 if (!pdata->idle_module) 928 return -ENODEV; 929 930 error = pdata->idle_module(dev, &ddata->cookie); 931 if (error) 932 dev_err(dev, "%s: could not idle: %i\n", 933 __func__, error); 934 935 return 0; 936 } 937 938 static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev, 939 struct sysc *ddata) 940 { 941 struct ti_sysc_platform_data *pdata; 942 int error; 943 944 pdata = dev_get_platdata(ddata->dev); 945 if (!pdata) 946 return 0; 947 948 if (!pdata->enable_module) 949 return -ENODEV; 950 951 error = pdata->enable_module(dev, &ddata->cookie); 952 if (error) 953 dev_err(dev, "%s: could not enable: %i\n", 954 __func__, error); 955 956 return 0; 957 } 958 959 static int __maybe_unused sysc_runtime_suspend(struct device *dev) 960 { 961 struct sysc *ddata; 962 int error = 0; 963 964 ddata = dev_get_drvdata(dev); 965 966 if (!ddata->enabled) 967 return 0; 968 969 if (ddata->legacy_mode) { 970 error = sysc_runtime_suspend_legacy(dev, ddata); 971 if (error) 972 return error; 973 } else { 974 error = sysc_disable_module(dev); 975 if (error) 976 return error; 977 } 978 979 sysc_disable_main_clocks(ddata); 980 981 if (sysc_opt_clks_needed(ddata)) 982 sysc_disable_opt_clocks(ddata); 983 984 ddata->enabled = false; 985 986 return error; 987 } 988 989 static int __maybe_unused sysc_runtime_resume(struct device *dev) 990 { 991 struct sysc *ddata; 992 int error = 0; 993 994 ddata = dev_get_drvdata(dev); 995 996 if (ddata->enabled) 997 return 0; 998 999 if (sysc_opt_clks_needed(ddata)) { 1000 error = sysc_enable_opt_clocks(ddata); 1001 if (error) 1002 return error; 1003 } 1004 1005 error = sysc_enable_main_clocks(ddata); 1006 if (error) 1007 goto err_opt_clocks; 1008 1009 if (ddata->legacy_mode) { 1010 error = sysc_runtime_resume_legacy(dev, ddata); 1011 if (error) 1012 goto err_main_clocks; 1013 } else { 1014 error = sysc_enable_module(dev); 1015 if (error) 1016 goto err_main_clocks; 1017 } 1018 1019 ddata->enabled = true; 1020 1021 return 0; 1022 1023 err_main_clocks: 1024 sysc_disable_main_clocks(ddata); 1025 err_opt_clocks: 1026 if (sysc_opt_clks_needed(ddata)) 1027 sysc_disable_opt_clocks(ddata); 1028 1029 return error; 1030 } 1031 1032 static int __maybe_unused sysc_noirq_suspend(struct device *dev) 1033 { 1034 struct sysc *ddata; 1035 1036 ddata = dev_get_drvdata(dev); 1037 1038 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE) 1039 return 0; 1040 1041 return pm_runtime_force_suspend(dev); 1042 } 1043 1044 static int __maybe_unused sysc_noirq_resume(struct device *dev) 1045 { 1046 struct sysc *ddata; 1047 1048 ddata = dev_get_drvdata(dev); 1049 1050 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE) 1051 return 0; 1052 1053 return pm_runtime_force_resume(dev); 1054 } 1055 1056 static const struct dev_pm_ops sysc_pm_ops = { 1057 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume) 1058 SET_RUNTIME_PM_OPS(sysc_runtime_suspend, 1059 sysc_runtime_resume, 1060 NULL) 1061 }; 1062 1063 /* Module revision register based quirks */ 1064 struct sysc_revision_quirk { 1065 const char *name; 1066 u32 base; 1067 int rev_offset; 1068 int sysc_offset; 1069 int syss_offset; 1070 u32 revision; 1071 u32 revision_mask; 1072 u32 quirks; 1073 }; 1074 1075 #define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \ 1076 optrev_val, optrevmask, optquirkmask) \ 1077 { \ 1078 .name = (optname), \ 1079 .base = (optbase), \ 1080 .rev_offset = (optrev), \ 1081 .sysc_offset = (optsysc), \ 1082 .syss_offset = (optsyss), \ 1083 .revision = (optrev_val), \ 1084 .revision_mask = (optrevmask), \ 1085 .quirks = (optquirkmask), \ 1086 } 1087 1088 static const struct sysc_revision_quirk sysc_revision_quirks[] = { 1089 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */ 1090 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff, 1091 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET), 1092 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff, 1093 SYSC_QUIRK_LEGACY_IDLE), 1094 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 1095 SYSC_QUIRK_LEGACY_IDLE), 1096 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff, 1097 SYSC_QUIRK_LEGACY_IDLE), 1098 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff, 1099 SYSC_QUIRK_LEGACY_IDLE), 1100 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff, 1101 SYSC_QUIRK_LEGACY_IDLE), 1102 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff, 1103 0), 1104 /* Some timers on omap4 and later */ 1105 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff, 1106 0), 1107 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff, 1108 0), 1109 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff, 1110 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE), 1111 /* Uarts on omap4 and later */ 1112 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff, 1113 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE), 1114 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff, 1115 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE), 1116 1117 /* Quirks that need to be set based on the module address */ 1118 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff, 1119 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT | 1120 SYSC_QUIRK_SWSUP_SIDLE), 1121 1122 #ifdef DEBUG 1123 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0), 1124 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0), 1125 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0), 1126 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0), 1127 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0), 1128 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902, 1129 0xffff00f0, 0), 1130 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0), 1131 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0), 1132 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0), 1133 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0), 1134 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0), 1135 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0), 1136 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0), 1137 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0), 1138 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0), 1139 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0), 1140 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0), 1141 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0), 1142 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0), 1143 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0), 1144 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0), 1145 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0), 1146 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0), 1147 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0), 1148 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0), 1149 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0), 1150 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0), 1151 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0), 1152 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0), 1153 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0), 1154 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0), 1155 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0), 1156 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0), 1157 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0), 1158 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0), 1159 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0), 1160 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0), 1161 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0), 1162 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0), 1163 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0), 1164 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0), 1165 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0), 1166 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0), 1167 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0), 1168 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0), 1169 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0), 1170 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0), 1171 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0), 1172 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0), 1173 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0), 1174 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050, 1175 0xffffffff, 0), 1176 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0), 1177 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0), 1178 #endif 1179 }; 1180 1181 /* 1182 * Early quirks based on module base and register offsets only that are 1183 * needed before the module revision can be read 1184 */ 1185 static void sysc_init_early_quirks(struct sysc *ddata) 1186 { 1187 const struct sysc_revision_quirk *q; 1188 int i; 1189 1190 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) { 1191 q = &sysc_revision_quirks[i]; 1192 1193 if (!q->base) 1194 continue; 1195 1196 if (q->base != ddata->module_pa) 1197 continue; 1198 1199 if (q->rev_offset >= 0 && 1200 q->rev_offset != ddata->offsets[SYSC_REVISION]) 1201 continue; 1202 1203 if (q->sysc_offset >= 0 && 1204 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG]) 1205 continue; 1206 1207 if (q->syss_offset >= 0 && 1208 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS]) 1209 continue; 1210 1211 ddata->name = q->name; 1212 ddata->cfg.quirks |= q->quirks; 1213 } 1214 } 1215 1216 /* Quirks that also consider the revision register value */ 1217 static void sysc_init_revision_quirks(struct sysc *ddata) 1218 { 1219 const struct sysc_revision_quirk *q; 1220 int i; 1221 1222 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) { 1223 q = &sysc_revision_quirks[i]; 1224 1225 if (q->base && q->base != ddata->module_pa) 1226 continue; 1227 1228 if (q->rev_offset >= 0 && 1229 q->rev_offset != ddata->offsets[SYSC_REVISION]) 1230 continue; 1231 1232 if (q->sysc_offset >= 0 && 1233 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG]) 1234 continue; 1235 1236 if (q->syss_offset >= 0 && 1237 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS]) 1238 continue; 1239 1240 if (q->revision == ddata->revision || 1241 (q->revision & q->revision_mask) == 1242 (ddata->revision & q->revision_mask)) { 1243 ddata->name = q->name; 1244 ddata->cfg.quirks |= q->quirks; 1245 } 1246 } 1247 } 1248 1249 /* 1250 * Note that pdata->init_module() typically does a reset first. After 1251 * pdata->init_module() is done, PM runtime can be used for the interconnect 1252 * target module. 1253 */ 1254 static int sysc_legacy_init(struct sysc *ddata) 1255 { 1256 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev); 1257 int error; 1258 1259 if (!ddata->legacy_mode || !pdata || !pdata->init_module) 1260 return 0; 1261 1262 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie); 1263 if (error == -EEXIST) 1264 error = 0; 1265 1266 return error; 1267 } 1268 1269 /** 1270 * sysc_rstctrl_reset_deassert - deassert rstctrl reset 1271 * @ddata: device driver data 1272 * @reset: reset before deassert 1273 * 1274 * A module can have both OCP softreset control and external rstctrl. 1275 * If more complicated rstctrl resets are needed, please handle these 1276 * directly from the child device driver and map only the module reset 1277 * for the parent interconnect target module device. 1278 * 1279 * Automatic reset of the module on init can be skipped with the 1280 * "ti,no-reset-on-init" device tree property. 1281 */ 1282 static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset) 1283 { 1284 int error; 1285 1286 if (!ddata->rsts) 1287 return 0; 1288 1289 if (reset) { 1290 error = reset_control_assert(ddata->rsts); 1291 if (error) 1292 return error; 1293 } 1294 1295 return reset_control_deassert(ddata->rsts); 1296 } 1297 1298 static int sysc_reset(struct sysc *ddata) 1299 { 1300 int offset = ddata->offsets[SYSC_SYSCONFIG]; 1301 int val; 1302 1303 if (ddata->legacy_mode || offset < 0 || 1304 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT) 1305 return 0; 1306 1307 /* 1308 * Currently only support reset status in sysstatus. 1309 * Warn and return error in all other cases 1310 */ 1311 if (!ddata->cfg.syss_mask) { 1312 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n"); 1313 return -EINVAL; 1314 } 1315 1316 val = sysc_read(ddata, offset); 1317 val |= (0x1 << ddata->cap->regbits->srst_shift); 1318 sysc_write(ddata, offset, val); 1319 1320 /* Poll on reset status */ 1321 offset = ddata->offsets[SYSC_SYSSTATUS]; 1322 1323 return readl_poll_timeout(ddata->module_va + offset, val, 1324 (val & ddata->cfg.syss_mask) == 0x0, 1325 100, MAX_MODULE_SOFTRESET_WAIT); 1326 } 1327 1328 /* 1329 * At this point the module is configured enough to read the revision but 1330 * module may not be completely configured yet to use PM runtime. Enable 1331 * all clocks directly during init to configure the quirks needed for PM 1332 * runtime based on the revision register. 1333 */ 1334 static int sysc_init_module(struct sysc *ddata) 1335 { 1336 int error = 0; 1337 bool manage_clocks = true; 1338 bool reset = true; 1339 1340 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT) 1341 reset = false; 1342 1343 error = sysc_rstctrl_reset_deassert(ddata, reset); 1344 if (error) 1345 return error; 1346 1347 if (ddata->cfg.quirks & 1348 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT)) 1349 manage_clocks = false; 1350 1351 if (manage_clocks) { 1352 error = sysc_enable_opt_clocks(ddata); 1353 if (error) 1354 return error; 1355 1356 error = sysc_enable_main_clocks(ddata); 1357 if (error) 1358 goto err_opt_clocks; 1359 } 1360 1361 ddata->revision = sysc_read_revision(ddata); 1362 sysc_init_revision_quirks(ddata); 1363 1364 error = sysc_legacy_init(ddata); 1365 if (error) 1366 goto err_main_clocks; 1367 1368 error = sysc_reset(ddata); 1369 if (error) 1370 dev_err(ddata->dev, "Reset failed with %d\n", error); 1371 1372 err_main_clocks: 1373 if (manage_clocks) 1374 sysc_disable_main_clocks(ddata); 1375 err_opt_clocks: 1376 if (manage_clocks) 1377 sysc_disable_opt_clocks(ddata); 1378 1379 return error; 1380 } 1381 1382 static int sysc_init_sysc_mask(struct sysc *ddata) 1383 { 1384 struct device_node *np = ddata->dev->of_node; 1385 int error; 1386 u32 val; 1387 1388 error = of_property_read_u32(np, "ti,sysc-mask", &val); 1389 if (error) 1390 return 0; 1391 1392 if (val) 1393 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask; 1394 else 1395 ddata->cfg.sysc_val = ddata->cap->sysc_mask; 1396 1397 return 0; 1398 } 1399 1400 static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes, 1401 const char *name) 1402 { 1403 struct device_node *np = ddata->dev->of_node; 1404 struct property *prop; 1405 const __be32 *p; 1406 u32 val; 1407 1408 of_property_for_each_u32(np, name, prop, p, val) { 1409 if (val >= SYSC_NR_IDLEMODES) { 1410 dev_err(ddata->dev, "invalid idlemode: %i\n", val); 1411 return -EINVAL; 1412 } 1413 *idlemodes |= (1 << val); 1414 } 1415 1416 return 0; 1417 } 1418 1419 static int sysc_init_idlemodes(struct sysc *ddata) 1420 { 1421 int error; 1422 1423 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes, 1424 "ti,sysc-midle"); 1425 if (error) 1426 return error; 1427 1428 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes, 1429 "ti,sysc-sidle"); 1430 if (error) 1431 return error; 1432 1433 return 0; 1434 } 1435 1436 /* 1437 * Only some devices on omap4 and later have SYSCONFIG reset done 1438 * bit. We can detect this if there is no SYSSTATUS at all, or the 1439 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers 1440 * have multiple bits for the child devices like OHCI and EHCI. 1441 * Depends on SYSC being parsed first. 1442 */ 1443 static int sysc_init_syss_mask(struct sysc *ddata) 1444 { 1445 struct device_node *np = ddata->dev->of_node; 1446 int error; 1447 u32 val; 1448 1449 error = of_property_read_u32(np, "ti,syss-mask", &val); 1450 if (error) { 1451 if ((ddata->cap->type == TI_SYSC_OMAP4 || 1452 ddata->cap->type == TI_SYSC_OMAP4_TIMER) && 1453 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET)) 1454 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS; 1455 1456 return 0; 1457 } 1458 1459 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET)) 1460 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS; 1461 1462 ddata->cfg.syss_mask = val; 1463 1464 return 0; 1465 } 1466 1467 /* 1468 * Many child device drivers need to have fck and opt clocks available 1469 * to get the clock rate for device internal configuration etc. 1470 */ 1471 static int sysc_child_add_named_clock(struct sysc *ddata, 1472 struct device *child, 1473 const char *name) 1474 { 1475 struct clk *clk; 1476 struct clk_lookup *l; 1477 int error = 0; 1478 1479 if (!name) 1480 return 0; 1481 1482 clk = clk_get(child, name); 1483 if (!IS_ERR(clk)) { 1484 clk_put(clk); 1485 1486 return -EEXIST; 1487 } 1488 1489 clk = clk_get(ddata->dev, name); 1490 if (IS_ERR(clk)) 1491 return -ENODEV; 1492 1493 l = clkdev_create(clk, name, dev_name(child)); 1494 if (!l) 1495 error = -ENOMEM; 1496 1497 clk_put(clk); 1498 1499 return error; 1500 } 1501 1502 static int sysc_child_add_clocks(struct sysc *ddata, 1503 struct device *child) 1504 { 1505 int i, error; 1506 1507 for (i = 0; i < ddata->nr_clocks; i++) { 1508 error = sysc_child_add_named_clock(ddata, 1509 child, 1510 ddata->clock_roles[i]); 1511 if (error && error != -EEXIST) { 1512 dev_err(ddata->dev, "could not add child clock %s: %i\n", 1513 ddata->clock_roles[i], error); 1514 1515 return error; 1516 } 1517 } 1518 1519 return 0; 1520 } 1521 1522 static struct device_type sysc_device_type = { 1523 }; 1524 1525 static struct sysc *sysc_child_to_parent(struct device *dev) 1526 { 1527 struct device *parent = dev->parent; 1528 1529 if (!parent || parent->type != &sysc_device_type) 1530 return NULL; 1531 1532 return dev_get_drvdata(parent); 1533 } 1534 1535 static int __maybe_unused sysc_child_runtime_suspend(struct device *dev) 1536 { 1537 struct sysc *ddata; 1538 int error; 1539 1540 ddata = sysc_child_to_parent(dev); 1541 1542 error = pm_generic_runtime_suspend(dev); 1543 if (error) 1544 return error; 1545 1546 if (!ddata->enabled) 1547 return 0; 1548 1549 return sysc_runtime_suspend(ddata->dev); 1550 } 1551 1552 static int __maybe_unused sysc_child_runtime_resume(struct device *dev) 1553 { 1554 struct sysc *ddata; 1555 int error; 1556 1557 ddata = sysc_child_to_parent(dev); 1558 1559 if (!ddata->enabled) { 1560 error = sysc_runtime_resume(ddata->dev); 1561 if (error < 0) 1562 dev_err(ddata->dev, 1563 "%s error: %i\n", __func__, error); 1564 } 1565 1566 return pm_generic_runtime_resume(dev); 1567 } 1568 1569 #ifdef CONFIG_PM_SLEEP 1570 static int sysc_child_suspend_noirq(struct device *dev) 1571 { 1572 struct sysc *ddata; 1573 int error; 1574 1575 ddata = sysc_child_to_parent(dev); 1576 1577 dev_dbg(ddata->dev, "%s %s\n", __func__, 1578 ddata->name ? ddata->name : ""); 1579 1580 error = pm_generic_suspend_noirq(dev); 1581 if (error) { 1582 dev_err(dev, "%s error at %i: %i\n", 1583 __func__, __LINE__, error); 1584 1585 return error; 1586 } 1587 1588 if (!pm_runtime_status_suspended(dev)) { 1589 error = pm_generic_runtime_suspend(dev); 1590 if (error) { 1591 dev_dbg(dev, "%s busy at %i: %i\n", 1592 __func__, __LINE__, error); 1593 1594 return 0; 1595 } 1596 1597 error = sysc_runtime_suspend(ddata->dev); 1598 if (error) { 1599 dev_err(dev, "%s error at %i: %i\n", 1600 __func__, __LINE__, error); 1601 1602 return error; 1603 } 1604 1605 ddata->child_needs_resume = true; 1606 } 1607 1608 return 0; 1609 } 1610 1611 static int sysc_child_resume_noirq(struct device *dev) 1612 { 1613 struct sysc *ddata; 1614 int error; 1615 1616 ddata = sysc_child_to_parent(dev); 1617 1618 dev_dbg(ddata->dev, "%s %s\n", __func__, 1619 ddata->name ? ddata->name : ""); 1620 1621 if (ddata->child_needs_resume) { 1622 ddata->child_needs_resume = false; 1623 1624 error = sysc_runtime_resume(ddata->dev); 1625 if (error) 1626 dev_err(ddata->dev, 1627 "%s runtime resume error: %i\n", 1628 __func__, error); 1629 1630 error = pm_generic_runtime_resume(dev); 1631 if (error) 1632 dev_err(ddata->dev, 1633 "%s generic runtime resume: %i\n", 1634 __func__, error); 1635 } 1636 1637 return pm_generic_resume_noirq(dev); 1638 } 1639 #endif 1640 1641 static struct dev_pm_domain sysc_child_pm_domain = { 1642 .ops = { 1643 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend, 1644 sysc_child_runtime_resume, 1645 NULL) 1646 USE_PLATFORM_PM_SLEEP_OPS 1647 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq, 1648 sysc_child_resume_noirq) 1649 } 1650 }; 1651 1652 /** 1653 * sysc_legacy_idle_quirk - handle children in omap_device compatible way 1654 * @ddata: device driver data 1655 * @child: child device driver 1656 * 1657 * Allow idle for child devices as done with _od_runtime_suspend(). 1658 * Otherwise many child devices will not idle because of the permanent 1659 * parent usecount set in pm_runtime_irq_safe(). 1660 * 1661 * Note that the long term solution is to just modify the child device 1662 * drivers to not set pm_runtime_irq_safe() and then this can be just 1663 * dropped. 1664 */ 1665 static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child) 1666 { 1667 if (!ddata->legacy_mode) 1668 return; 1669 1670 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE) 1671 dev_pm_domain_set(child, &sysc_child_pm_domain); 1672 } 1673 1674 static int sysc_notifier_call(struct notifier_block *nb, 1675 unsigned long event, void *device) 1676 { 1677 struct device *dev = device; 1678 struct sysc *ddata; 1679 int error; 1680 1681 ddata = sysc_child_to_parent(dev); 1682 if (!ddata) 1683 return NOTIFY_DONE; 1684 1685 switch (event) { 1686 case BUS_NOTIFY_ADD_DEVICE: 1687 error = sysc_child_add_clocks(ddata, dev); 1688 if (error) 1689 return error; 1690 sysc_legacy_idle_quirk(ddata, dev); 1691 break; 1692 default: 1693 break; 1694 } 1695 1696 return NOTIFY_DONE; 1697 } 1698 1699 static struct notifier_block sysc_nb = { 1700 .notifier_call = sysc_notifier_call, 1701 }; 1702 1703 /* Device tree configured quirks */ 1704 struct sysc_dts_quirk { 1705 const char *name; 1706 u32 mask; 1707 }; 1708 1709 static const struct sysc_dts_quirk sysc_dts_quirks[] = { 1710 { .name = "ti,no-idle-on-init", 1711 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, }, 1712 { .name = "ti,no-reset-on-init", 1713 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, }, 1714 { .name = "ti,no-idle", 1715 .mask = SYSC_QUIRK_NO_IDLE, }, 1716 }; 1717 1718 static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np, 1719 bool is_child) 1720 { 1721 const struct property *prop; 1722 int i, len; 1723 1724 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) { 1725 const char *name = sysc_dts_quirks[i].name; 1726 1727 prop = of_get_property(np, name, &len); 1728 if (!prop) 1729 continue; 1730 1731 ddata->cfg.quirks |= sysc_dts_quirks[i].mask; 1732 if (is_child) { 1733 dev_warn(ddata->dev, 1734 "dts flag should be at module level for %s\n", 1735 name); 1736 } 1737 } 1738 } 1739 1740 static int sysc_init_dts_quirks(struct sysc *ddata) 1741 { 1742 struct device_node *np = ddata->dev->of_node; 1743 int error; 1744 u32 val; 1745 1746 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL); 1747 1748 sysc_parse_dts_quirks(ddata, np, false); 1749 error = of_property_read_u32(np, "ti,sysc-delay-us", &val); 1750 if (!error) { 1751 if (val > 255) { 1752 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n", 1753 val); 1754 } 1755 1756 ddata->cfg.srst_udelay = (u8)val; 1757 } 1758 1759 return 0; 1760 } 1761 1762 static void sysc_unprepare(struct sysc *ddata) 1763 { 1764 int i; 1765 1766 if (!ddata->clocks) 1767 return; 1768 1769 for (i = 0; i < SYSC_MAX_CLOCKS; i++) { 1770 if (!IS_ERR_OR_NULL(ddata->clocks[i])) 1771 clk_unprepare(ddata->clocks[i]); 1772 } 1773 } 1774 1775 /* 1776 * Common sysc register bits found on omap2, also known as type1 1777 */ 1778 static const struct sysc_regbits sysc_regbits_omap2 = { 1779 .dmadisable_shift = -ENODEV, 1780 .midle_shift = 12, 1781 .sidle_shift = 3, 1782 .clkact_shift = 8, 1783 .emufree_shift = 5, 1784 .enwkup_shift = 2, 1785 .srst_shift = 1, 1786 .autoidle_shift = 0, 1787 }; 1788 1789 static const struct sysc_capabilities sysc_omap2 = { 1790 .type = TI_SYSC_OMAP2, 1791 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE | 1792 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET | 1793 SYSC_OMAP2_AUTOIDLE, 1794 .regbits = &sysc_regbits_omap2, 1795 }; 1796 1797 /* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */ 1798 static const struct sysc_capabilities sysc_omap2_timer = { 1799 .type = TI_SYSC_OMAP2_TIMER, 1800 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE | 1801 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET | 1802 SYSC_OMAP2_AUTOIDLE, 1803 .regbits = &sysc_regbits_omap2, 1804 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT, 1805 }; 1806 1807 /* 1808 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2 1809 * with different sidle position 1810 */ 1811 static const struct sysc_regbits sysc_regbits_omap3_sham = { 1812 .dmadisable_shift = -ENODEV, 1813 .midle_shift = -ENODEV, 1814 .sidle_shift = 4, 1815 .clkact_shift = -ENODEV, 1816 .enwkup_shift = -ENODEV, 1817 .srst_shift = 1, 1818 .autoidle_shift = 0, 1819 .emufree_shift = -ENODEV, 1820 }; 1821 1822 static const struct sysc_capabilities sysc_omap3_sham = { 1823 .type = TI_SYSC_OMAP3_SHAM, 1824 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE, 1825 .regbits = &sysc_regbits_omap3_sham, 1826 }; 1827 1828 /* 1829 * AES register bits found on omap3 and later, a variant of 1830 * sysc_regbits_omap2 with different sidle position 1831 */ 1832 static const struct sysc_regbits sysc_regbits_omap3_aes = { 1833 .dmadisable_shift = -ENODEV, 1834 .midle_shift = -ENODEV, 1835 .sidle_shift = 6, 1836 .clkact_shift = -ENODEV, 1837 .enwkup_shift = -ENODEV, 1838 .srst_shift = 1, 1839 .autoidle_shift = 0, 1840 .emufree_shift = -ENODEV, 1841 }; 1842 1843 static const struct sysc_capabilities sysc_omap3_aes = { 1844 .type = TI_SYSC_OMAP3_AES, 1845 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE, 1846 .regbits = &sysc_regbits_omap3_aes, 1847 }; 1848 1849 /* 1850 * Common sysc register bits found on omap4, also known as type2 1851 */ 1852 static const struct sysc_regbits sysc_regbits_omap4 = { 1853 .dmadisable_shift = 16, 1854 .midle_shift = 4, 1855 .sidle_shift = 2, 1856 .clkact_shift = -ENODEV, 1857 .enwkup_shift = -ENODEV, 1858 .emufree_shift = 1, 1859 .srst_shift = 0, 1860 .autoidle_shift = -ENODEV, 1861 }; 1862 1863 static const struct sysc_capabilities sysc_omap4 = { 1864 .type = TI_SYSC_OMAP4, 1865 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU | 1866 SYSC_OMAP4_SOFTRESET, 1867 .regbits = &sysc_regbits_omap4, 1868 }; 1869 1870 static const struct sysc_capabilities sysc_omap4_timer = { 1871 .type = TI_SYSC_OMAP4_TIMER, 1872 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU | 1873 SYSC_OMAP4_SOFTRESET, 1874 .regbits = &sysc_regbits_omap4, 1875 }; 1876 1877 /* 1878 * Common sysc register bits found on omap4, also known as type3 1879 */ 1880 static const struct sysc_regbits sysc_regbits_omap4_simple = { 1881 .dmadisable_shift = -ENODEV, 1882 .midle_shift = 2, 1883 .sidle_shift = 0, 1884 .clkact_shift = -ENODEV, 1885 .enwkup_shift = -ENODEV, 1886 .srst_shift = -ENODEV, 1887 .emufree_shift = -ENODEV, 1888 .autoidle_shift = -ENODEV, 1889 }; 1890 1891 static const struct sysc_capabilities sysc_omap4_simple = { 1892 .type = TI_SYSC_OMAP4_SIMPLE, 1893 .regbits = &sysc_regbits_omap4_simple, 1894 }; 1895 1896 /* 1897 * SmartReflex sysc found on omap34xx 1898 */ 1899 static const struct sysc_regbits sysc_regbits_omap34xx_sr = { 1900 .dmadisable_shift = -ENODEV, 1901 .midle_shift = -ENODEV, 1902 .sidle_shift = -ENODEV, 1903 .clkact_shift = 20, 1904 .enwkup_shift = -ENODEV, 1905 .srst_shift = -ENODEV, 1906 .emufree_shift = -ENODEV, 1907 .autoidle_shift = -ENODEV, 1908 }; 1909 1910 static const struct sysc_capabilities sysc_34xx_sr = { 1911 .type = TI_SYSC_OMAP34XX_SR, 1912 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY, 1913 .regbits = &sysc_regbits_omap34xx_sr, 1914 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED | 1915 SYSC_QUIRK_LEGACY_IDLE, 1916 }; 1917 1918 /* 1919 * SmartReflex sysc found on omap36xx and later 1920 */ 1921 static const struct sysc_regbits sysc_regbits_omap36xx_sr = { 1922 .dmadisable_shift = -ENODEV, 1923 .midle_shift = -ENODEV, 1924 .sidle_shift = 24, 1925 .clkact_shift = -ENODEV, 1926 .enwkup_shift = 26, 1927 .srst_shift = -ENODEV, 1928 .emufree_shift = -ENODEV, 1929 .autoidle_shift = -ENODEV, 1930 }; 1931 1932 static const struct sysc_capabilities sysc_36xx_sr = { 1933 .type = TI_SYSC_OMAP36XX_SR, 1934 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP, 1935 .regbits = &sysc_regbits_omap36xx_sr, 1936 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE, 1937 }; 1938 1939 static const struct sysc_capabilities sysc_omap4_sr = { 1940 .type = TI_SYSC_OMAP4_SR, 1941 .regbits = &sysc_regbits_omap36xx_sr, 1942 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE, 1943 }; 1944 1945 /* 1946 * McASP register bits found on omap4 and later 1947 */ 1948 static const struct sysc_regbits sysc_regbits_omap4_mcasp = { 1949 .dmadisable_shift = -ENODEV, 1950 .midle_shift = -ENODEV, 1951 .sidle_shift = 0, 1952 .clkact_shift = -ENODEV, 1953 .enwkup_shift = -ENODEV, 1954 .srst_shift = -ENODEV, 1955 .emufree_shift = -ENODEV, 1956 .autoidle_shift = -ENODEV, 1957 }; 1958 1959 static const struct sysc_capabilities sysc_omap4_mcasp = { 1960 .type = TI_SYSC_OMAP4_MCASP, 1961 .regbits = &sysc_regbits_omap4_mcasp, 1962 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED, 1963 }; 1964 1965 /* 1966 * McASP found on dra7 and later 1967 */ 1968 static const struct sysc_capabilities sysc_dra7_mcasp = { 1969 .type = TI_SYSC_OMAP4_SIMPLE, 1970 .regbits = &sysc_regbits_omap4_simple, 1971 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED, 1972 }; 1973 1974 /* 1975 * FS USB host found on omap4 and later 1976 */ 1977 static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = { 1978 .dmadisable_shift = -ENODEV, 1979 .midle_shift = -ENODEV, 1980 .sidle_shift = 24, 1981 .clkact_shift = -ENODEV, 1982 .enwkup_shift = 26, 1983 .srst_shift = -ENODEV, 1984 .emufree_shift = -ENODEV, 1985 .autoidle_shift = -ENODEV, 1986 }; 1987 1988 static const struct sysc_capabilities sysc_omap4_usb_host_fs = { 1989 .type = TI_SYSC_OMAP4_USB_HOST_FS, 1990 .sysc_mask = SYSC_OMAP2_ENAWAKEUP, 1991 .regbits = &sysc_regbits_omap4_usb_host_fs, 1992 }; 1993 1994 static const struct sysc_regbits sysc_regbits_dra7_mcan = { 1995 .dmadisable_shift = -ENODEV, 1996 .midle_shift = -ENODEV, 1997 .sidle_shift = -ENODEV, 1998 .clkact_shift = -ENODEV, 1999 .enwkup_shift = 4, 2000 .srst_shift = 0, 2001 .emufree_shift = -ENODEV, 2002 .autoidle_shift = -ENODEV, 2003 }; 2004 2005 static const struct sysc_capabilities sysc_dra7_mcan = { 2006 .type = TI_SYSC_DRA7_MCAN, 2007 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET, 2008 .regbits = &sysc_regbits_dra7_mcan, 2009 }; 2010 2011 static int sysc_init_pdata(struct sysc *ddata) 2012 { 2013 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev); 2014 struct ti_sysc_module_data *mdata; 2015 2016 if (!pdata || !ddata->legacy_mode) 2017 return 0; 2018 2019 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL); 2020 if (!mdata) 2021 return -ENOMEM; 2022 2023 mdata->name = ddata->legacy_mode; 2024 mdata->module_pa = ddata->module_pa; 2025 mdata->module_size = ddata->module_size; 2026 mdata->offsets = ddata->offsets; 2027 mdata->nr_offsets = SYSC_MAX_REGS; 2028 mdata->cap = ddata->cap; 2029 mdata->cfg = &ddata->cfg; 2030 2031 ddata->mdata = mdata; 2032 2033 return 0; 2034 } 2035 2036 static int sysc_init_match(struct sysc *ddata) 2037 { 2038 const struct sysc_capabilities *cap; 2039 2040 cap = of_device_get_match_data(ddata->dev); 2041 if (!cap) 2042 return -EINVAL; 2043 2044 ddata->cap = cap; 2045 if (ddata->cap) 2046 ddata->cfg.quirks |= ddata->cap->mod_quirks; 2047 2048 return 0; 2049 } 2050 2051 static void ti_sysc_idle(struct work_struct *work) 2052 { 2053 struct sysc *ddata; 2054 2055 ddata = container_of(work, struct sysc, idle_work.work); 2056 2057 if (pm_runtime_active(ddata->dev)) 2058 pm_runtime_put_sync(ddata->dev); 2059 } 2060 2061 static const struct of_device_id sysc_match_table[] = { 2062 { .compatible = "simple-bus", }, 2063 { /* sentinel */ }, 2064 }; 2065 2066 static int sysc_probe(struct platform_device *pdev) 2067 { 2068 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev); 2069 struct sysc *ddata; 2070 int error; 2071 2072 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 2073 if (!ddata) 2074 return -ENOMEM; 2075 2076 ddata->dev = &pdev->dev; 2077 platform_set_drvdata(pdev, ddata); 2078 2079 error = sysc_init_match(ddata); 2080 if (error) 2081 return error; 2082 2083 error = sysc_init_dts_quirks(ddata); 2084 if (error) 2085 goto unprepare; 2086 2087 error = sysc_map_and_check_registers(ddata); 2088 if (error) 2089 goto unprepare; 2090 2091 error = sysc_init_sysc_mask(ddata); 2092 if (error) 2093 goto unprepare; 2094 2095 error = sysc_init_idlemodes(ddata); 2096 if (error) 2097 goto unprepare; 2098 2099 error = sysc_init_syss_mask(ddata); 2100 if (error) 2101 goto unprepare; 2102 2103 error = sysc_init_pdata(ddata); 2104 if (error) 2105 goto unprepare; 2106 2107 sysc_init_early_quirks(ddata); 2108 2109 error = sysc_get_clocks(ddata); 2110 if (error) 2111 return error; 2112 2113 error = sysc_init_resets(ddata); 2114 if (error) 2115 return error; 2116 2117 error = sysc_init_module(ddata); 2118 if (error) 2119 goto unprepare; 2120 2121 pm_runtime_enable(ddata->dev); 2122 error = pm_runtime_get_sync(ddata->dev); 2123 if (error < 0) { 2124 pm_runtime_put_noidle(ddata->dev); 2125 pm_runtime_disable(ddata->dev); 2126 goto unprepare; 2127 } 2128 2129 sysc_show_registers(ddata); 2130 2131 ddata->dev->type = &sysc_device_type; 2132 error = of_platform_populate(ddata->dev->of_node, sysc_match_table, 2133 pdata ? pdata->auxdata : NULL, 2134 ddata->dev); 2135 if (error) 2136 goto err; 2137 2138 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle); 2139 2140 /* At least earlycon won't survive without deferred idle */ 2141 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT | 2142 SYSC_QUIRK_NO_RESET_ON_INIT)) { 2143 schedule_delayed_work(&ddata->idle_work, 3000); 2144 } else { 2145 pm_runtime_put(&pdev->dev); 2146 } 2147 2148 if (!of_get_available_child_count(ddata->dev->of_node)) 2149 reset_control_assert(ddata->rsts); 2150 2151 return 0; 2152 2153 err: 2154 pm_runtime_put_sync(&pdev->dev); 2155 pm_runtime_disable(&pdev->dev); 2156 unprepare: 2157 sysc_unprepare(ddata); 2158 2159 return error; 2160 } 2161 2162 static int sysc_remove(struct platform_device *pdev) 2163 { 2164 struct sysc *ddata = platform_get_drvdata(pdev); 2165 int error; 2166 2167 cancel_delayed_work_sync(&ddata->idle_work); 2168 2169 error = pm_runtime_get_sync(ddata->dev); 2170 if (error < 0) { 2171 pm_runtime_put_noidle(ddata->dev); 2172 pm_runtime_disable(ddata->dev); 2173 goto unprepare; 2174 } 2175 2176 of_platform_depopulate(&pdev->dev); 2177 2178 pm_runtime_put_sync(&pdev->dev); 2179 pm_runtime_disable(&pdev->dev); 2180 reset_control_assert(ddata->rsts); 2181 2182 unprepare: 2183 sysc_unprepare(ddata); 2184 2185 return 0; 2186 } 2187 2188 static const struct of_device_id sysc_match[] = { 2189 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, }, 2190 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, }, 2191 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, }, 2192 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, }, 2193 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, }, 2194 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, }, 2195 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, }, 2196 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, }, 2197 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, }, 2198 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, }, 2199 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, }, 2200 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, }, 2201 { .compatible = "ti,sysc-usb-host-fs", 2202 .data = &sysc_omap4_usb_host_fs, }, 2203 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, }, 2204 { }, 2205 }; 2206 MODULE_DEVICE_TABLE(of, sysc_match); 2207 2208 static struct platform_driver sysc_driver = { 2209 .probe = sysc_probe, 2210 .remove = sysc_remove, 2211 .driver = { 2212 .name = "ti-sysc", 2213 .of_match_table = sysc_match, 2214 .pm = &sysc_pm_ops, 2215 }, 2216 }; 2217 2218 static int __init sysc_init(void) 2219 { 2220 bus_register_notifier(&platform_bus_type, &sysc_nb); 2221 2222 return platform_driver_register(&sysc_driver); 2223 } 2224 module_init(sysc_init); 2225 2226 static void __exit sysc_exit(void) 2227 { 2228 bus_unregister_notifier(&platform_bus_type, &sysc_nb); 2229 platform_driver_unregister(&sysc_driver); 2230 } 2231 module_exit(sysc_exit); 2232 2233 MODULE_DESCRIPTION("TI sysc interconnect target driver"); 2234 MODULE_LICENSE("GPL v2"); 2235