Skip to contentSkip to conversation

Rasperry Pi clock support

PATCH In review Eric Anholt 3 participants 8 messages Aug 13, 2015 – Aug 27, 2015

Subsystems (4)

v5 of 7 In review Latest is v7 →

lore
Share & tools
lore
#0 lore raw

I'm also avoiding requests to set rate/state when they're a no-op, as apparently no-op changes to pixel clock break the firmware-configured display setup. It *shouldn't* change anything, but I've confirmed that setting the CPU clock works fine, and after a couple of weeks of prodding pixel clock now, I'm at a loss for what's going on with it.

#1 lore raw

PATCH v5 1/3 clk: bcm2835: Add binding docs for the Raspberry Pi clock provider

The hardware clocks are not controllable by the ARM, so we have to make requests to the firmware to do so from the VPU side. This will let us replace fixed clocks in our DT with actual clock control (and correct frequency information).

---

v2: Include the dt-bindings header in this commit instead of the next
one. Make the clock indices match the firmware clock IDs. Rename the binding's compat string. Move the firmware phandle to be under a vendor-specific namespace.
v3: Mention 'clk' in the subject instead of the more generic
dt/bindings.

 .../clock/raspberrypi,bcm2835-firmware-clocks.txt  | 25 ++++++++++++++++++++++
 include/dt-bindings/clk/raspberrypi.h              | 23 ++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/raspberrypi,bcm2835-firmware-clocks.txt
 create mode 100644 include/dt-bindings/clk/raspberrypi.h
Documentation/devicetree/bindings/clock/raspberrypi,bcm2835-firmware-clocks.txt new file +25 -0
new file mode 100644
index 0000000..0972602
@@ -0,0 +1,25 @@
+Raspberry Pi firmware clock provider.
+
+The Raspberry Pi architecture doesn't provide direct access to the
+CLOCKMAN peripheral from the ARM side, so Linux has to make requests
+to the VPU firmware to program them.
+
+This binding uses the common clock binding:
+Documentation/devicetree/bindings/clock/clock-bindings.txt
+
+Required properties:
+- compatible:		Should be "raspberrypi,bcm2835-firmware-clocks"
+
+- #clock-cells:		Shall have value <1>.  The permitted clock-specifier
+			  values can be found in
+			  include/dt-bindings/clk/raspberrypi.h.
+
+- raspberrypi,firmware:	Phandle to the firmware driver node.
+
+Example:
+
+firmware_clocks: firmware-clocks {
+	compatible = "raspberrypi,bcm2835-firmware-clocks";
+	#clock-cells = <1>;
+	raspberrypi,firmware = <&firmware>;
+};
include/dt-bindings/clk/raspberrypi.h new file +23 -0
new file mode 100644
index 0000000..ceec90f
@@ -0,0 +1,23 @@
+#/*
+ *  Copyright ? 2015 Broadcom
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _DT_BINDINGS_CLK_RASPBERRYPI_H
+#define _DT_BINDINGS_CLK_RASPBERRYPI_H
+
+#define RPI_CLOCK_EMMC	1
+#define RPI_CLOCK_UART0	2
+#define RPI_CLOCK_ARM	3
+#define RPI_CLOCK_CORE	4
+#define RPI_CLOCK_V3D	5
+#define RPI_CLOCK_H264	6
+#define RPI_CLOCK_ISP	7
+#define RPI_CLOCK_SDRAM	8
+#define RPI_CLOCK_PIXEL	9
+#define RPI_CLOCK_PWM	10
+
+#endif
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH v5 1/3] clk: bcm2835: Add binding docs for the Raspberry Pi clock provider

Quoted 4 lines · Eric Anholt

The hardware clocks are not controllable by the ARM, so we have to make requests to the firmware to do so from the VPU side. This will let us replace fixed clocks in our DT with actual clock control (and correct frequency information).

Gordon from the Raspberry Pi Foundation just asked me "what do you mean, you can't access the clocks from the ARM?" I'd been assured I couldn't by another developer (who was originally going to write a Linux driver for this), and my own testing had also indicated I couldn't, but after a new round of hacking together some tests, I see things that look a lot like clockman registers.

Looks like we're going to get a native driver, instead.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150824/8ff89ce0/attachment.sig>

#3 lore raw

PATCH v5 2/3 clk: Add a Raspberry Pi-specific clock driver.

In reply to Eric Anholt

Unfortunately, the clock manager's registers are not accessible by the ARM, so we have to request that the firmware modify our clocks for us.

This driver only registers the clocks at the point they are requested by a client driver. This is partially to support returning -EPROBE_DEFER when the firmware driver isn't supported yet, but it also avoids issues with disabling "unused" clocks due to them not yet being connected to their consumers in the DT.

---

v2: Declare the mutex static (from review by Baruch Siach), merge
description and copyright comments.

v3: Update for new rpi firmware API. Update the compatible string.
Make the firmware handle be under a vendor-namespaced property. Make the DT indices match the firmware clock IDs. Move the driver under the firmware driver's Kconfig, since it requires it. Move a container_of() from 2 callers into the callee.

v4: Update commit message to mention subsystem. Add #defines for a
couple of bitfield checks, and use the bitfield check for rpi_clk_is_on(). Return an error when appropriate in rpi_clk_is_on(), and don't return specific rpi_firmware_property() errors in other places when we were only returning -EINVAL. Add some comment text describing how return values work from the firmware.

v5: Restructure as a platform_driver on suggestion from Michael
Turquette. This works around the -EPROBE_DEFER problem, so now we can register all of our clocks at probe time. Store the current clock rate in the struct, and avoid asking for no-op changes. It appears that a no-op change of the pixel clock while the clock is being used (at boot time when the KMS driver calls clk_prepare_enable()) actually breaks the display. Dropped Stephen's ack since this is a pretty significant rewrite.

 drivers/clk/Makefile          |   1 +
 drivers/clk/clk-raspberrypi.c | 286 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 287 insertions(+)
 create mode 100644 drivers/clk/clk-raspberrypi.c
drivers/clk/Makefile +1 -0
index c4cf075..ad0a4a5 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_MACH_ASM9260)		+= clk-asm9260.o
 obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN)	+= clk-axi-clkgen.o
 obj-$(CONFIG_ARCH_AXXIA)		+= clk-axm5516.o
 obj-$(CONFIG_ARCH_BCM2835)		+= clk-bcm2835.o
+obj-$(CONFIG_RASPBERRYPI_FIRMWARE)	+= clk-raspberrypi.o
 obj-$(CONFIG_COMMON_CLK_CDCE706)	+= clk-cdce706.o
 obj-$(CONFIG_ARCH_CLPS711X)		+= clk-clps711x.o
 obj-$(CONFIG_ARCH_EFM32)		+= clk-efm32gg.o
drivers/clk/clk-raspberrypi.c new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH v5 2/3] clk: Add a Raspberry Pi-specific clock driver.

Quoted 21 lines · Eric Anholt

Unfortunately, the clock manager's registers are not accessible by the ARM, so we have to request that the firmware modify our clocks for us.

This driver only registers the clocks at the point they are requested by a client driver. This is partially to support returning -EPROBE_DEFER when the firmware driver isn't supported yet, but it also avoids issues with disabling "unused" clocks due to them not yet being connected to their consumers in the DT.

drivers/clk/clk-raspberrypi.c +10 -0
 
+static const struct {
+	const char *name;
+	int flags;
+} rpi_clocks[] = {
+	[RPI_CLOCK_EMMC] = { "emmc", CLK_IS_ROOT },
+	[RPI_CLOCK_UART0] = { "uart0", CLK_IS_ROOT },
+	[RPI_CLOCK_ARM] = { "arm", CLK_IS_ROOT | CLK_IGNORE_UNUSED },
+	[RPI_CLOCK_CORE] = { "core", CLK_IS_ROOT | CLK_IGNORE_UNUSED },
+	[RPI_CLOCK_V3D] = { "v3d", CLK_IS_ROOT },
+	[RPI_CLOCK_H264] = { "h264", CLK_IS_ROOT },

drivers/clk/clk-raspberrypi.c line 49

Quoted 10 lines
drivers/clk/clk-raspberrypi.c +10 -0
+	[RPI_CLOCK_ISP] = { "isp", CLK_IS_ROOT },
+	[RPI_CLOCK_SDRAM] = { "sdram", CLK_IS_ROOT | CLK_IGNORE_UNUSED },
+	[RPI_CLOCK_PIXEL] = { "pixel", CLK_IS_ROOT | CLK_IGNORE_UNUSED },
+	[RPI_CLOCK_PWM] = { "pwm", CLK_IS_ROOT },
+};
+
+struct rpi_firmware_clock {
+	/* Clock definitions in our static struct. */
+	const char *name;
+	int flags;

Are these duplicates of the values in rpi_clocks[]? Why not just store a pointer to or index of the entry in that array?

drivers/clk/clk-raspberrypi.c line 91

Quoted 9 lines
drivers/clk/clk-raspberrypi.c +9 -0
+static int rpi_clk_set_state(struct clk_hw *hw, bool on)
+{
+	struct rpi_firmware_clock *rpi_clk =
+		container_of(hw, struct rpi_firmware_clock, hw);
+	u32 packet[2];
+	int ret;
+
+	if (on == (rpi_clk->last_rate != 0))
+		return 0;

The overloading of last_rate to represent both rate information and on/off status is slightly confusing. I would have expected this function to clear last_rate to 0 when switching the clock off, and some specific rate when turning a clock on. Is there some guarantee that the clock core will always call recalc_rate() at certain times, thus ensuring that last_rate is always accurate?

Wouldn't it be simpler to let last_rate always represent that actual rate, and have a separate last_on or is_on field to represent the enable/disable state?

Quoted 2 lines
drivers/clk/clk-raspberrypi.c +2 -0
+static unsigned long rpi_clk_get_rate(struct clk_hw *hw,
+				      unsigned long parent_rate)

...

drivers/clk/clk-raspberrypi.c

Quoted 1 line
drivers/clk/clk-raspberrypi.c +1 -0
+	rpi_clk->last_rate = packet[1];

Since this is a query API, I wouldn't have expected it to have side-effects like this. Don't we know what rate the clock runs at based on the firmware's response in set_rate()?

drivers/clk/clk-raspberrypi.c line 222

Quoted 7 lines
drivers/clk/clk-raspberrypi.c +6 -0
+static int rpi_clk_probe(struct platform_device *pdev)
 
+	onecell = devm_kmalloc(dev, sizeof(*onecell), GFP_KERNEL);
+	if (!onecell)
+		return -ENOMEM;
+	onecell->clk_num = ARRAY_SIZE(rpi_clocks);
+	onecell->clks = devm_kzalloc(dev, sizeof(*onecell->clks), GFP_KERNEL);

Don't you need to multiply the size by ARRAY_SIZE(rpi_clocks)? I assume onecell->clks is an array with one entry per each of onecell->clk_num? Yes, the for loop right after that allocation confirms this.

#5 lore raw

PATCH v5 3/3 ARM: bcm2835: Add DT for the firmware clocks driver.

In reply to Eric Anholt

---

v2: Rename our compat string to mention bcm2835, and make our firmware
phandle be under a vendor-namespaced property.

v3: Squashed in the patches to reference the other clocks, to avoid
regressions now that we register all clocks at boot. Dropped Stephen's ack, since he hadn't acked the new EMMC change yet.

 arch/arm/boot/dts/bcm2835-rpi.dtsi | 18 ++++++++++++++++++
 arch/arm/boot/dts/bcm2835.dtsi     |  3 +--
 2 files changed, 19 insertions(+), 2 deletions(-)
arch/arm/boot/dts/bcm2835-rpi.dtsi +18 -0
index ab5474e..5d370cb 100644
@@ -1,3 +1,4 @@
+#include <dt-bindings/clk/raspberrypi.h>
 #include "bcm2835.dtsi"
 
 / {
@@ -20,6 +21,12 @@
 			compatible = "raspberrypi,bcm2835-firmware";
 			mboxes = <&mailbox>;
 		};
+
+		firmware_clocks: firmware-clocks {
+			compatible = "raspberrypi,bcm2835-firmware-clocks";
+			#clock-cells = <1>;
+			raspberrypi,firmware = <&firmware>;
+		};
 	};
 };
 
@@ -55,4 +62,15 @@
 &sdhci {
 	status = "okay";
 	bus-width = <4>;
+	clocks = <&firmware_clocks RPI_CLOCK_EMMC>;
+};
+
+&uart0 {
+	clocks = <&firmware_clocks RPI_CLOCK_UART0>,
+		 <&firmware_clocks RPI_CLOCK_CORE>;
+	clock-names = "uartclk", "apb_pclk";
+};
+
+&spi {
+	clocks = <&firmware_clocks RPI_CLOCK_CORE>;
 };
arch/arm/boot/dts/bcm2835.dtsi +1 -2
index 301c73f..5be2862 100644
@@ -92,11 +92,10 @@
 			#interrupt-cells = <2>;
 		};
 
-		uart at 7e201000 {
+		uart0: uart at 7e201000 {
 			compatible = "brcm,bcm2835-pl011", "arm,pl011", "arm,primecell";
 			reg = <0x7e201000 0x1000>;
 			interrupts = <2 25>;
-			clock-frequency = <3000000>;
 			arm,primecell-periphid = <0x00241011>;
 		};
 
Signature · 1 line
2.1.4
Acked-by on v5 3/3
#6 lore raw

Re: [PATCH v5 3/3] ARM: bcm2835: Add DT for the firmware clocks driver.

Quoted 2 lines · Eric Anholt

A patch description might be nice, although admittedly the subject seems clear enough.

arch/arm/boot/dts/bcm2835-rpi.dtsi line 29

Quoted 7 lines
arch/arm/boot/dts/bcm2835-rpi.dtsi +5 -0
 
+		firmware_clocks: firmware-clocks {
+			compatible = "raspberrypi,bcm2835-firmware-clocks";
+			#clock-cells = <1>;
+			raspberrypi,firmware = <&firmware>;
+		};

No need to resend for this, but just a background note: DT node names usually contain a device type not a device identity, so "clocks" not "firmware-clocks" would be typical.

This patch,

#7 lore raw

Re: [PATCH v5 3/3] ARM: bcm2835: Add DT for the firmware clocks driver.

Quoted 6 lines · Stephen Warren
Quoted 2 lines · Eric Anholt

A patch description might be nice, although admittedly the subject seems clear enough.

Agreed that the subject is clear enough on this occasion.

Quoted 11 lines

arch/arm/boot/dts/bcm2835-rpi.dtsi line 29

Quoted 7 lines
arch/arm/boot/dts/bcm2835-rpi.dtsi +5 -0
 
+		firmware_clocks: firmware-clocks {
+			compatible = "raspberrypi,bcm2835-firmware-clocks";
+			#clock-cells = <1>;
+			raspberrypi,firmware = <&firmware>;
+		};

No need to resend for this, but just a background note: DT node names usually contain a device type not a device identity, so "clocks" not "firmware-clocks" would be typical.

Please fix this up when you re-send the set.

Quoted 2 lines

This patch,