950-0022-clk-bcm2835-Read-max-core-clock-from-firmware.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. From 9667f053b5015bfb486e16d3c88a79b961395876 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <[email protected]>
  3. Date: Mon, 6 Mar 2017 09:06:18 +0000
  4. Subject: [PATCH] clk-bcm2835: Read max core clock from firmware
  5. The VPU is responsible for managing the core clock, usually under
  6. direction from the bcm2835-cpufreq driver but not via the clk-bcm2835
  7. driver. Since the core frequency can change without warning, it is
  8. safer to report the maximum clock rate to users of the core clock -
  9. I2C, SPI and the mini UART - to err on the safe side when calculating
  10. clock divisors.
  11. If the DT node for the clock driver includes a reference to the
  12. firmware node, use the firmware API to query the maximum core clock
  13. instead of reading the divider registers.
  14. Prior to this patch, a "100KHz" I2C bus was sometimes clocked at about
  15. 160KHz. In particular, switching to the 4.9 kernel was likely to break
  16. SenseHAT usage on a Pi3.
  17. Signed-off-by: Phil Elwell <[email protected]>
  18. ---
  19. drivers/clk/bcm/clk-bcm2835.c | 39 ++++++++++++++++++++++++++++++++++-
  20. 1 file changed, 38 insertions(+), 1 deletion(-)
  21. --- a/drivers/clk/bcm/clk-bcm2835.c
  22. +++ b/drivers/clk/bcm/clk-bcm2835.c
  23. @@ -35,6 +35,7 @@
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <dt-bindings/clock/bcm2835.h>
  27. +#include <soc/bcm2835/raspberrypi-firmware.h>
  28. #define CM_PASSWORD 0x5a000000
  29. @@ -295,6 +296,8 @@
  30. #define SOC_BCM2711 BIT(1)
  31. #define SOC_ALL (SOC_BCM2835 | SOC_BCM2711)
  32. +#define VCMSG_ID_CORE_CLOCK 4
  33. +
  34. /*
  35. * Names of clocks used within the driver that need to be replaced
  36. * with an external parent's name. This array is in the order that
  37. @@ -313,6 +316,7 @@ static const char *const cprman_parent_n
  38. struct bcm2835_cprman {
  39. struct device *dev;
  40. void __iomem *regs;
  41. + struct rpi_firmware *fw;
  42. spinlock_t regs_lock; /* spinlock for all clocks */
  43. unsigned int soc;
  44. @@ -1015,6 +1019,30 @@ static unsigned long bcm2835_clock_get_r
  45. return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
  46. }
  47. +static unsigned long bcm2835_clock_get_rate_vpu(struct clk_hw *hw,
  48. + unsigned long parent_rate)
  49. +{
  50. + struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  51. + struct bcm2835_cprman *cprman = clock->cprman;
  52. +
  53. + if (cprman->fw) {
  54. + struct {
  55. + u32 id;
  56. + u32 val;
  57. + } packet;
  58. +
  59. + packet.id = VCMSG_ID_CORE_CLOCK;
  60. + packet.val = 0;
  61. +
  62. + if (!rpi_firmware_property(cprman->fw,
  63. + RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
  64. + &packet, sizeof(packet)))
  65. + return packet.val;
  66. + }
  67. +
  68. + return bcm2835_clock_get_rate(hw, parent_rate);
  69. +}
  70. +
  71. static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
  72. {
  73. struct bcm2835_cprman *cprman = clock->cprman;
  74. @@ -1303,7 +1331,7 @@ static int bcm2835_vpu_clock_is_on(struc
  75. */
  76. static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
  77. .is_prepared = bcm2835_vpu_clock_is_on,
  78. - .recalc_rate = bcm2835_clock_get_rate,
  79. + .recalc_rate = bcm2835_clock_get_rate_vpu,
  80. .set_rate = bcm2835_clock_set_rate,
  81. .determine_rate = bcm2835_clock_determine_rate,
  82. .set_parent = bcm2835_clock_set_parent,
  83. @@ -2241,6 +2269,7 @@ static int bcm2835_clk_probe(struct plat
  84. const struct bcm2835_clk_desc *desc;
  85. const size_t asize = ARRAY_SIZE(clk_desc_array);
  86. const struct cprman_plat_data *pdata;
  87. + struct device_node *fw_node;
  88. size_t i;
  89. u32 clk_id;
  90. int ret;
  91. @@ -2262,6 +2291,14 @@ static int bcm2835_clk_probe(struct plat
  92. if (IS_ERR(cprman->regs))
  93. return PTR_ERR(cprman->regs);
  94. + fw_node = of_parse_phandle(dev->of_node, "firmware", 0);
  95. + if (fw_node) {
  96. + struct rpi_firmware *fw = rpi_firmware_get(NULL);
  97. + if (!fw)
  98. + return -EPROBE_DEFER;
  99. + cprman->fw = fw;
  100. + }
  101. +
  102. memset(bcm2835_clk_claimed, 0, sizeof(bcm2835_clk_claimed));
  103. for (i = 0;
  104. !of_property_read_u32_index(pdev->dev.of_node, "claim-clocks",