clksrc_st_lpc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Clocksource using the Low Power Timer found in the Low Power Controller (LPC)
  3. *
  4. * Copyright (C) 2015 STMicroelectronics – All Rights Reserved
  5. *
  6. * Author(s): Francesco Virlinzi <francesco.virlinzi@st.com>
  7. * Ajit Pal Singh <ajitpal.singh@st.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/init.h>
  17. #include <linux/of_address.h>
  18. #include <linux/sched_clock.h>
  19. #include <linux/slab.h>
  20. #include <dt-bindings/mfd/st-lpc.h>
  21. /* Low Power Timer */
  22. #define LPC_LPT_LSB_OFF 0x400
  23. #define LPC_LPT_MSB_OFF 0x404
  24. #define LPC_LPT_START_OFF 0x408
  25. static struct st_clksrc_ddata {
  26. struct clk *clk;
  27. void __iomem *base;
  28. } ddata;
  29. static void __init st_clksrc_reset(void)
  30. {
  31. writel_relaxed(0, ddata.base + LPC_LPT_START_OFF);
  32. writel_relaxed(0, ddata.base + LPC_LPT_MSB_OFF);
  33. writel_relaxed(0, ddata.base + LPC_LPT_LSB_OFF);
  34. writel_relaxed(1, ddata.base + LPC_LPT_START_OFF);
  35. }
  36. static u64 notrace st_clksrc_sched_clock_read(void)
  37. {
  38. return (u64)readl_relaxed(ddata.base + LPC_LPT_LSB_OFF);
  39. }
  40. static int __init st_clksrc_init(void)
  41. {
  42. unsigned long rate;
  43. int ret;
  44. st_clksrc_reset();
  45. rate = clk_get_rate(ddata.clk);
  46. sched_clock_register(st_clksrc_sched_clock_read, 32, rate);
  47. ret = clocksource_mmio_init(ddata.base + LPC_LPT_LSB_OFF,
  48. "clksrc-st-lpc", rate, 300, 32,
  49. clocksource_mmio_readl_up);
  50. if (ret) {
  51. pr_err("clksrc-st-lpc: Failed to register clocksource\n");
  52. return ret;
  53. }
  54. return 0;
  55. }
  56. static int __init st_clksrc_setup_clk(struct device_node *np)
  57. {
  58. struct clk *clk;
  59. clk = of_clk_get(np, 0);
  60. if (IS_ERR(clk)) {
  61. pr_err("clksrc-st-lpc: Failed to get LPC clock\n");
  62. return PTR_ERR(clk);
  63. }
  64. if (clk_prepare_enable(clk)) {
  65. pr_err("clksrc-st-lpc: Failed to enable LPC clock\n");
  66. return -EINVAL;
  67. }
  68. if (!clk_get_rate(clk)) {
  69. pr_err("clksrc-st-lpc: Failed to get LPC clock rate\n");
  70. clk_disable_unprepare(clk);
  71. return -EINVAL;
  72. }
  73. ddata.clk = clk;
  74. return 0;
  75. }
  76. static void __init st_clksrc_of_register(struct device_node *np)
  77. {
  78. int ret;
  79. uint32_t mode;
  80. ret = of_property_read_u32(np, "st,lpc-mode", &mode);
  81. if (ret) {
  82. pr_err("clksrc-st-lpc: An LPC mode must be provided\n");
  83. return;
  84. }
  85. /* LPC can either run as a Clocksource or in RTC or WDT mode */
  86. if (mode != ST_LPC_MODE_CLKSRC)
  87. return;
  88. ddata.base = of_iomap(np, 0);
  89. if (!ddata.base) {
  90. pr_err("clksrc-st-lpc: Unable to map iomem\n");
  91. return;
  92. }
  93. if (st_clksrc_setup_clk(np)) {
  94. iounmap(ddata.base);
  95. return;
  96. }
  97. if (st_clksrc_init()) {
  98. clk_disable_unprepare(ddata.clk);
  99. clk_put(ddata.clk);
  100. iounmap(ddata.base);
  101. return;
  102. }
  103. pr_info("clksrc-st-lpc: clocksource initialised - running @ %luHz\n",
  104. clk_get_rate(ddata.clk));
  105. }
  106. CLOCKSOURCE_OF_DECLARE(ddata, "st,stih407-lpc", st_clksrc_of_register);