Skip to contentSkip to conversation

clk: bcm2835: fixes clk-bcm2835 driver issues

PATCH New kernel at martin.sperl.org 2 participants 20 messages Feb 29, 2016 – Apr 26, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover clk: bcm2835: fixes clk-bcm2835 driver issues

From: Martin Sperl <[email protected]>

This patchset - as recommended by Stefan Wahren - is a split of the original V5 patchset that now only includes bugfixes to the clock driver not adding new clocks or functionality.

There will be another patchset that implements the changes required to add all missing clocks of the SOC.

Martin Sperl (6):
clk: bcm2835: pll_off should only set CM_PLL_ANARST
clk: bcm2835: add locking to pll*_on/off methods
clk: bcm2835: enable clocks that have been enabled by firmware
clk: bcm2835: divider value has to be 1 or more
clk: bcm2835: correctly enable fractional clock support
clk: bcm2835: clean up coding style issues

 drivers/clk/bcm/clk-bcm2835.c |   87 +++++++++++++++++++++++++++++++++--------
 1 file changed, 70 insertions(+), 17 deletions(-)

--
1.7.10.4

#1 lore raw

PATCH 1/6 clk: bcm2835: pll_off should only set CM_PLL_ANARST

From: Martin Sperl <[email protected]>

bcm2835_pll_off is currently assigning CM_PLL_ANARST to the control register.

This patch only sets the CM_PLL_ANARST bit not resetting any of the other bits, which allows restoring the register to its original value via bcm2834_pll_on.

It also now locks during the read/modify/write cycle of both registers.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

---
 drivers/clk/bcm/clk-bcm2835.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +8 -2
index 5747a9d..2b7c6af 100644
@@ -913,8 +913,14 @@ static void bcm2835_pll_off(struct clk_hw *hw)
 	struct bcm2835_cprman *cprman = pll->cprman;
 	const struct bcm2835_pll_data *data = pll->data;
 
-	cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
-	cprman_write(cprman, data->a2w_ctrl_reg, A2W_PLL_CTRL_PWRDN);
+	spin_lock(&cprman->regs_lock);
+	cprman_write(cprman, data->cm_ctrl_reg,
+		     cprman_read(cprman, data->cm_ctrl_reg) |
+		     CM_PLL_ANARST);
+	cprman_write(cprman, data->a2w_ctrl_reg,
+		     cprman_read(cprman, data->a2w_ctrl_reg) |
+		     A2W_PLL_CTRL_PWRDN);
+	spin_unlock(&cprman->regs_lock);
 }
 
 static int bcm2835_pll_on(struct clk_hw *hw)

--
1.7.10.4

#2 lore raw

PATCH 1/6 clk: bcm2835: pll_off should only set CM_PLL_ANARST

Quoted 30 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

bcm2835_pll_off is currently assigning CM_PLL_ANARST to the control register.

This patch only sets the CM_PLL_ANARST bit not resetting any of the other bits, which allows restoring the register to its original value via bcm2834_pll_on.

It also now locks during the read/modify/write cycle of both registers.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

---
 drivers/clk/bcm/clk-bcm2835.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +0 -1
index 5747a9d..2b7c6af 100644
@@ -913,8 +913,14 @@ static void bcm2835_pll_off(struct clk_hw *hw)
 	struct bcm2835_cprman *cprman = pll->cprman;
 	const struct bcm2835_pll_data *data = pll->data;
 
-	cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);

drivers/clk/bcm/clk-bcm2835.c static void bcm2835_pll_off(struct clk_hw *hw) · line 924

Quoted 10 lines
drivers/clk/bcm/clk-bcm2835.c +8 -1
-	cprman_write(cprman, data->a2w_ctrl_reg, A2W_PLL_CTRL_PWRDN);
+	spin_lock(&cprman->regs_lock);
+	cprman_write(cprman, data->cm_ctrl_reg,
+		     cprman_read(cprman, data->cm_ctrl_reg) |
+		     CM_PLL_ANARST);
+	cprman_write(cprman, data->a2w_ctrl_reg,
+		     cprman_read(cprman, data->a2w_ctrl_reg) |
+		     A2W_PLL_CTRL_PWRDN);
+	spin_unlock(&cprman->regs_lock);
 }

I think I see now: we're making sure that the hold_mask of a divider doesn't get lost if we turn its source PLL off.

-------------- 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/20160229/0ff7c01f/attachment.sig>

#3 lore raw

PATCH 2/6 clk: bcm2835: add locking to pll*_on/off methods

In reply to kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Add missing locking to:
* bcm2835_pll_divider_on
* bcm2835_pll_divider_off
to protect the read modify write cycle for the register access protecting both cm_ctl and a2w_ctl registers.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

---
 drivers/clk/bcm/clk-bcm2835.c |    4 ++++
 1 file changed, 4 insertions(+)
drivers/clk/bcm/clk-bcm2835.c +4 -0
index 2b7c6af..30d6486 100644
@@ -1088,10 +1088,12 @@ static void bcm2835_pll_divider_off(struct clk_hw *hw)
 	struct bcm2835_cprman *cprman = divider->cprman;
 	const struct bcm2835_pll_divider_data *data = divider->data;
 
+	spin_lock(&cprman->regs_lock);
 	cprman_write(cprman, data->cm_reg,
 		     (cprman_read(cprman, data->cm_reg) &
 		      ~data->load_mask) | data->hold_mask);
 	cprman_write(cprman, data->a2w_reg, A2W_PLL_CHANNEL_DISABLE);
+	spin_unlock(&cprman->regs_lock);
 }
 
 static int bcm2835_pll_divider_on(struct clk_hw *hw)
@@ -1100,12 +1102,14 @@ static int bcm2835_pll_divider_on(struct clk_hw *hw)
 	struct bcm2835_cprman *cprman = divider->cprman;
 	const struct bcm2835_pll_divider_data *data = divider->data;
 
+	spin_lock(&cprman->regs_lock);
 	cprman_write(cprman, data->a2w_reg,
 		     cprman_read(cprman, data->a2w_reg) &
 		     ~A2W_PLL_CHANNEL_DISABLE);
 
 	cprman_write(cprman, data->cm_reg,
 		     cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
+	spin_unlock(&cprman->regs_lock);
 
 	return 0;
 }

--
1.7.10.4

#4 lore raw

PATCH 2/6 clk: bcm2835: add locking to pll*_on/off methods

Quoted 8 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Add missing locking to:
* bcm2835_pll_divider_on
* bcm2835_pll_divider_off
to protect the read modify write cycle for the register access protecting both cm_ctl and a2w_ctl registers.

s/ctl/reg/ in the commit message

bcm2835_pll_divider_set_rate() also does an RMW on cm_reg, why was it left out?

Since the a2w reg is only modified by _on and _off, and the core holds
the prepare lock across those, I think it was already safe and doesn't
need to be mentioned here.
-------------- 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/20160229/39df8abd/attachment.sig>

#5 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

In reply to kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

If a clock that has been enabled by the firmware gets disabled by a driver this may right now result in a crash of the system as then also the corresponding PLL_dividers as well as PLLs get disabled (if not used) - some of which are used by the VideoCore GPU (which also runs the firmware)

This patch prepares/enables those clocks that have been configured by the firmware.

Whenever the clock framework implements either CLK_IS_CRITICAL or HAND_OFF this can get changed to use this new mechanism.

For this to be completely successful (i.e not missing a clock and subsequently a pll) it is recommended to add all the known clocks of the soc so that this can get applied to all clocks.

---
 drivers/clk/bcm/clk-bcm2835.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/clk/bcm/clk-bcm2835.c +14 -1
index 30d6486..1fbb55d 100644
@@ -37,6 +37,7 @@
  * generator).
  */
 
+#include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
 #include <linux/clk/bcm2835.h>
@@ -1478,6 +1479,7 @@ static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
 	struct clk_init_data init;
 	const char *parents[1 << CM_SRC_BITS];
 	size_t i;
+	struct clk *clk;
 
 	/*
 	 * Replace our "xosc" references with the oscillator's
@@ -1511,7 +1513,18 @@ static struct clk *bcm2835_register_clock(struct bcm2835_cprman *cprman,
 	clock->data = data;
 	clock->hw.init = &init;
 
-	return devm_clk_register(cprman->dev, &clock->hw);
+	clk = devm_clk_register(cprman->dev, &clock->hw);
+	if (IS_ERR_OR_NULL(clk))
+		return clk;
+
+	/* enable/prepare if the clock is enabled by the firmware */
+	if (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) {
+		dev_info(cprman->dev,
+			 "found firmware enabled clock %pC\n", clk);
+		clk_prepare_enable(clk);
+	}
+
+	return clk;
 }
 
 static int bcm2835_clk_probe(struct platform_device *pdev)

--
1.7.10.4

#6 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

Quoted 11 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

If a clock that has been enabled by the firmware gets disabled by a driver this may right now result in a crash of the system as then also the corresponding PLL_dividers as well as PLLs get disabled (if not used) - some of which are used by the VideoCore GPU (which also runs the firmware)

This patch prepares/enables those clocks that have been configured by the firmware.

Quoted 7 lines

Whenever the clock framework implements either CLK_IS_CRITICAL or HAND_OFF this can get changed to use this new mechanism.

For this to be completely successful (i.e not missing a clock and subsequently a pll) it is recommended to add all the known clocks of the soc so that this can get applied to all clocks.

I think this makes sense to have, for now at least.

-------------- 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/20160229/5da66916/attachment.sig>

#7 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

Quoted 17 lines · Eric Anholt
Quoted 15 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

If a clock that has been enabled by the firmware gets disabled by a driver this may right now result in a crash of the system as then also the corresponding PLL_dividers as well as PLLs get disabled (if not used) - some of which are used by the VideoCore GPU (which also runs the firmware)

This patch prepares/enables those clocks that have been configured by the firmware.

Whenever the clock framework implements either CLK_IS_CRITICAL or HAND_OFF this can get changed to use this new mechanism.

Quoted 7 lines
Quoted 3 lines

For this to be completely successful (i.e not missing a clock and subsequently a pll) it is recommended to add all the known clocks of the soc so that this can get applied to all clocks.

I think this makes sense to have, for now at least.

Scratch that, this breaks display. Since the clkgen clocks are flagged
as needing to be gated in order to change dividers, it means you can't
set clock rates for anything that was turned on at boot.
-------------- 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/20160317/4248041e/attachment-0001.sig>

#8 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

Quoted 26 lines · Eric Anholt
Quoted 21 lines · Eric Anholt
Quoted 18 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

If a clock that has been enabled by the firmware gets disabled by a driver this may right now result in a crash of the system as then also the corresponding PLL_dividers as well as PLLs get disabled (if not used) - some of which are used by the VideoCore GPU (which also runs the firmware)

This patch prepares/enables those clocks that have been configured by the firmware.

Whenever the clock framework implements either CLK_IS_CRITICAL or HAND_OFF this can get changed to use this new mechanism.

For this to be completely successful (i.e not missing a clock and subsequently a pll) it is recommended to add all the known clocks of the soc so that this can get applied to all clocks.

Quoted 7 lines
Quoted 3 lines

I think this makes sense to have, for now at least.

Scratch that, this breaks display. Since the clkgen clocks are flagged as needing to be gated in order to change dividers, it means you can't set clock rates for anything that was turned on at boot.

See that separate thread that triggered this:
serial: clk: bcm2835: Strange effects when using aux-uart in console
and this patch fixes this issue.

To summarize the situation figured in the thread:
* you load the module

* you start using the tty (say by using "stty -F /dev/ttyAMA0")
* this opens the device
* this prepare the relevant clock (usage = 1)
* this prepares the parent pll-divider (usage = 1)
* this prepares the parent pll (usage = 1)

* you stop using the tty (stty closes the device)
* this release the clock
* usage count drops to 0, so disable the clock
* this releases the parent pll-divider
* usage count drops to 0, so disable the pll-div
* this releases the parent pll (and disables it as usage = 0)
* usage count drops to 0, so disable the pll-div

* system crashes (with a bit of delay)

The prepare should just increase the usage so it never gets to a count of 0.

Maybe we need to use those "CLK_IS_CRITICAL? ?HANDS_OFF? flags instead? (when/if they become available)

How do you want to solve that - I have not got a DSI display, but HDMI continues to work...

Martin

#9 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

Quoted 56 lines · Martin Sperl
Quoted 25 lines · Eric Anholt
Quoted 21 lines · Eric Anholt
Quoted 18 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

If a clock that has been enabled by the firmware gets disabled by a driver this may right now result in a crash of the system as then also the corresponding PLL_dividers as well as PLLs get disabled (if not used) - some of which are used by the VideoCore GPU (which also runs the firmware)

This patch prepares/enables those clocks that have been configured by the firmware.

Whenever the clock framework implements either CLK_IS_CRITICAL or HAND_OFF this can get changed to use this new mechanism.

For this to be completely successful (i.e not missing a clock and subsequently a pll) it is recommended to add all the known clocks of the soc so that this can get applied to all clocks.

Quoted 7 lines
Quoted 3 lines

I think this makes sense to have, for now at least.

Scratch that, this breaks display. Since the clkgen clocks are flagged as needing to be gated in order to change dividers, it means you can't set clock rates for anything that was turned on at boot.

See that separate thread that triggered this:
serial: clk: bcm2835: Strange effects when using aux-uart in console
and this patch fixes this issue.

To summarize the situation figured in the thread:
* you load the module

* you start using the tty (say by using "stty -F /dev/ttyAMA0")
* this opens the device
* this prepare the relevant clock (usage = 1)
* this prepares the parent pll-divider (usage = 1)
* this prepares the parent pll (usage = 1)

* you stop using the tty (stty closes the device)
* this release the clock
* usage count drops to 0, so disable the clock
* this releases the parent pll-divider
* usage count drops to 0, so disable the pll-div
* this releases the parent pll (and disables it as usage = 0)
* usage count drops to 0, so disable the pll-div

* system crashes (with a bit of delay)

Quoted 7 lines

The prepare should just increase the usage so it never gets to a count of 0.

Maybe we need to use those "CLK_IS_CRITICAL? ?HANDS_OFF? flags instead? (when/if they become available)

How do you want to solve that - I have not got a DSI display, but HDMI continues to work...

We should just prepare the necessary divider, not the leaf clocks that
we actually want to control at runtime.
-------------- 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/20160317/7fb1570f/attachment.sig>

#10 lore raw

PATCH 3/6 clk: bcm2835: enable clocks that have been enabled by firmware

Quoted 30 lines · Eric Anholt
Quoted 26 lines · Martin Sperl

See that separate thread that triggered this:
serial: clk: bcm2835: Strange effects when using aux-uart in console
and this patch fixes this issue.

To summarize the situation figured in the thread:
* you load the module

* you start using the tty (say by using "stty -F /dev/ttyAMA0")
* this opens the device
* this prepare the relevant clock (usage = 1)
* this prepares the parent pll-divider (usage = 1)
* this prepares the parent pll (usage = 1)

* you stop using the tty (stty closes the device)
* this release the clock
* usage count drops to 0, so disable the clock
* this releases the parent pll-divider
* usage count drops to 0, so disable the pll-div
* this releases the parent pll (and disables it as usage = 0)
* usage count drops to 0, so disable the pll-div

* system crashes (with a bit of delay)

The prepare should just increase the usage so it never gets to a count of 0.

Quoted 8 lines
Quoted 5 lines

Maybe we need to use those "CLK_IS_CRITICAL? ?HANDS_OFF? flags instead? (when/if they become available)

How do you want to solve that - I have not got a DSI display, but HDMI continues to work...

We should just prepare the necessary divider, not the leaf clocks that we actually want to control at runtime.

So how are we continuing here?
The reason why I am asking is that switching downstream to use the clock driver result in the lockups also when using i2s/pcm.

I have seen that CLK_IS_CRITICAL has made it into the clk-next tree. Should we use that for plls (or pll_dividers) that are running?

Something like this:

drivers/clk/bcm/clk-bcm2835.c +9 -0
index 35f8de7..c17019f 100644
@@ -1200,6 +1200,15 @@ bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
        divider->cprman = cprman;
        divider->data = data;
 
+       /* if the pll-divider is running, then mark is as critical */
+       if ((cprman_read(cprman, data->a2w_reg) &
+            A2W_PLL_CHANNEL_DISABLE) == 0) {
+               dev_info(cprman->dev,
+                        "found enabled pll_div %s - marking it as critical\n",
+                        data->name);
+               init.flags |= CLK_IS_CRITICAL;
+       }
+
        clk = devm_clk_register(cprman->dev, &divider->div.hw);
        if (IS_ERR(clk))
                return clk;

At least for downstream this does work and gives the following messages:

[    2.861321] bcm2835-clk 20101000.cprman: found enabled pll_div plla_core - marking it as critical
[    2.875917] bcm2835-clk 20101000.cprman: found enabled pll_div pllb_arm - marking it as critical
[    2.961250] bcm2835-clk 20101000.cprman: found enabled pll_div pllc_core0 - marking it as critical
[    2.977317] bcm2835-clk 20101000.cprman: found enabled pll_div pllc_per - marking it as critical
[    2.993226] bcm2835-clk 20101000.cprman: found enabled pll_div plld_core - marking it as critical
[    3.010189] bcm2835-clk 20101000.cprman: found enabled pll_div plld_per - marking it as critical
[    3.024993] bcm2835-clk 20101000.cprman: found enabled pll_div pllh_pix - marking it as critical

Martin

#11 lore raw

PATCH 4/6 clk: bcm2835: divider value has to be 1 or more

In reply to kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Current clamping of a normal divider allows a value < 1 to be valid.

A divider of < 1 would actually only be possible if we had a PLL...

So this patch clamps the divider to 1.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

---
 drivers/clk/bcm/clk-bcm2835.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +3 -2
index 1fbb55d..edb1f74 100644
@@ -1194,8 +1194,9 @@ static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
 		div += unused_frac_mask + 1;
 	div &= ~unused_frac_mask;
 
-	/* Clamp to the limits. */
-	div = max(div, unused_frac_mask + 1);
+	/* clamp to min divider of 1 */
+	div = max_t(u32, div, 1 << CM_DIV_FRAC_BITS);
+	/* clamp to the highest possible fractional divider */
 	div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
 				      CM_DIV_FRAC_BITS - data->frac_bits));
 

--
1.7.10.4

#12 lore raw

PATCH 4/6 clk: bcm2835: divider value has to be 1 or more

Quoted 4 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Current clamping of a normal divider allows a value < 1 to be valid.

Quoted 8 lines

A divider of < 1 would actually only be possible if we had a PLL...

So this patch clamps the divider to 1.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

-------------- 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/20160229/3c909ba8/attachment.sig>

#13 lore raw

PATCH 5/6 clk: bcm2835: correctly enable fractional clock support

In reply to kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

The current driver calculates the clock divider with fractional support enabled.

But it does not enable fractional support in the control register itself resulting in an integer only divider, but in clk_set_rate responds back the fractionally divided clock frequency.

This patch enables fractional support in the control register whenever there is a fractional bit set in the requested clock divider.

Mash clock limits are are also handled for the PWM clock applying the correct divider limits (2 and max_int) applicable to basic fractional divider support (mash order of 1).

It also adds locking to protect the read/modify/write cycle of the register modification.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

---
 drivers/clk/bcm/clk-bcm2835.c |   45 +++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +39 -6
index edb1f74..da77069 100644
@@ -52,6 +52,7 @@
 #define CM_GNRICCTL		0x000
 #define CM_GNRICDIV		0x004
 # define CM_DIV_FRAC_BITS	12
+# define CM_DIV_FRAC_MASK	GENMASK(CM_DIV_FRAC_BITS - 1, 0)
 
 #define CM_VPUCTL		0x008
 #define CM_VPUDIV		0x00c
@@ -129,6 +130,7 @@
 # define CM_GATE			BIT(CM_GATE_BIT)
 # define CM_BUSY			BIT(7)
 # define CM_BUSYD			BIT(8)
+# define CM_FRAC			BIT(9)
 # define CM_SRC_SHIFT			0
 # define CM_SRC_BITS			4
 # define CM_SRC_MASK			0xf
@@ -648,6 +650,7 @@ struct bcm2835_clock_data {
 	u32 frac_bits;
 
 	bool is_vpu_clock;
+	bool is_mash_clock;
 };
 
 static const char *const bcm2835_clock_per_parents[] = {
@@ -829,6 +832,7 @@ static const struct bcm2835_clock_data bcm2835_clock_pwm_data = {
 	.div_reg = CM_PWMDIV,
 	.int_bits = 12,
 	.frac_bits = 12,
+	.is_mash_clock = true,
 };
 
 struct bcm2835_pll {
@@ -1184,7 +1188,7 @@ static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
 		GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
 	u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
 	u64 rem;
-	u32 div;
+	u32 div, mindiv, maxdiv;
 
 	rem = do_div(temp, rate);
 	div = temp;
@@ -1194,11 +1198,23 @@ static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
 		div += unused_frac_mask + 1;
 	div &= ~unused_frac_mask;
 
-	/* clamp to min divider of 1 */
-	div = max_t(u32, div, 1 << CM_DIV_FRAC_BITS);
-	/* clamp to the highest possible fractional divider */
-	div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
-				      CM_DIV_FRAC_BITS - data->frac_bits));
+	/* different clamping limits apply for a mash clock */
+	if (data->is_mash_clock) {
+		/* clamp to min divider of 2 */
+		mindiv = 2 << CM_DIV_FRAC_BITS;
+		/* clamp to the highest possible integer divider */
+		maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
+	} else {
+		/* clamp to min divider of 1 */
+		mindiv = 1 << CM_DIV_FRAC_BITS;
+		/* clamp to the highest possible fractional divider */
+		maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
+				 CM_DIV_FRAC_BITS - data->frac_bits);
+	}
+
+	/* apply the clamping  limits */
+	div = max_t(u32, div, mindiv);
+	div = min_t(u32, div, maxdiv);
 
 	return div;
 }
@@ -1292,9 +1308,26 @@ static int bcm2835_clock_set_rate(struct clk_hw *hw,
 	struct bcm2835_cprman *cprman = clock->cprman;
 	const struct bcm2835_clock_data *data = clock->data;
 	u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
+	u32 ctl;
+
+	spin_lock(&cprman->regs_lock);
+
+	/*
+	 * Setting up frac support
+	 *
+	 * In principle it is recommended to stop/start the clock first,
+	 * but as we set CLK_SET_RATE_GATE during registration of the
+	 * clock this requirement should be take care of by the
+	 * clk-framework.
+	 */
+	ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
+	ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
+	cprman_write(cprman, data->ctl_reg, ctl);
 
 	cprman_write(cprman, data->div_reg, div);
 
+	spin_unlock(&cprman->regs_lock);
+
 	return 0;
 }
 

--
1.7.10.4

#14 lore raw

PATCH 5/6 clk: bcm2835: correctly enable fractional clock support

Quoted 17 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

The current driver calculates the clock divider with fractional support enabled.

But it does not enable fractional support in the control register itself resulting in an integer only divider, but in clk_set_rate responds back the fractionally divided clock frequency.

This patch enables fractional support in the control register whenever there is a fractional bit set in the requested clock divider.

Mash clock limits are are also handled for the PWM clock applying the correct divider limits (2 and max_int) applicable to basic fractional divider support (mash order of 1).

Quoted 7 lines

It also adds locking to protect the read/modify/write cycle of the register modification.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

Rewrite of the commit message:

The driver has been using fractional dividers for all clocks with fractional bits. However, MASH clocks (those used to drive audio) only do fractional division when in MASH mode, which is used to push clock jitter out of the audio band. The PWM clock (used to drive i2s audio) is the only MASH clock currently enabled in the driver.

Additional MASH modes are available that widen the frequency range, but only 1-level MASH is used for now, until we characterize some better policy.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

Quoted 17 lines
---
 drivers/clk/bcm/clk-bcm2835.c |   45 +++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +1 -0
index edb1f74..da77069 100644
@@ -52,6 +52,7 @@
 #define CM_GNRICCTL		0x000
 #define CM_GNRICDIV		0x004
 # define CM_DIV_FRAC_BITS	12
+# define CM_DIV_FRAC_MASK	GENMASK(CM_DIV_FRAC_BITS - 1, 0)
 
 #define CM_VPUCTL		0x008
 #define CM_VPUDIV		0x00c

drivers/clk/bcm/clk-bcm2835.c line 133

Quoted 4 lines
drivers/clk/bcm/clk-bcm2835.c +1 -0
@@ -129,6 +130,7 @@
 # define CM_GATE			BIT(CM_GATE_BIT)
 # define CM_BUSY			BIT(7)
 # define CM_BUSYD			BIT(8)
+# define CM_FRAC			BIT(9)

This is just the low bit of the 2-bit CM_MASH field at 9:10. So you're enabling MASH mode 1 in this patch. Similarly, the subject of the patch should be something like "Fix MASH-based fractional clock support for PWM" since all the other clocks work with fractional already.

Quoted 66 lines
drivers/clk/bcm/clk-bcm2835.c +25 -6
 # define CM_SRC_SHIFT			0
 # define CM_SRC_BITS			4
 # define CM_SRC_MASK			0xf
@@ -648,6 +650,7 @@ struct bcm2835_clock_data {
 	u32 frac_bits;
 
 	bool is_vpu_clock;
+	bool is_mash_clock;
 };
 
 static const char *const bcm2835_clock_per_parents[] = {
@@ -829,6 +832,7 @@ static const struct bcm2835_clock_data bcm2835_clock_pwm_data = {
 	.div_reg = CM_PWMDIV,
 	.int_bits = 12,
 	.frac_bits = 12,
+	.is_mash_clock = true,
 };
 
 struct bcm2835_pll {
@@ -1184,7 +1188,7 @@ static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
 		GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
 	u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
 	u64 rem;
-	u32 div;
+	u32 div, mindiv, maxdiv;
 
 	rem = do_div(temp, rate);
 	div = temp;
@@ -1194,11 +1198,23 @@ static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
 		div += unused_frac_mask + 1;
 	div &= ~unused_frac_mask;
 
-	/* clamp to min divider of 1 */
-	div = max_t(u32, div, 1 << CM_DIV_FRAC_BITS);
-	/* clamp to the highest possible fractional divider */
-	div = min_t(u32, div, GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
-				      CM_DIV_FRAC_BITS - data->frac_bits));
+	/* different clamping limits apply for a mash clock */
+	if (data->is_mash_clock) {
+		/* clamp to min divider of 2 */
+		mindiv = 2 << CM_DIV_FRAC_BITS;
+		/* clamp to the highest possible integer divider */
+		maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
+	} else {
+		/* clamp to min divider of 1 */
+		mindiv = 1 << CM_DIV_FRAC_BITS;
+		/* clamp to the highest possible fractional divider */
+		maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
+				 CM_DIV_FRAC_BITS - data->frac_bits);
+	}
+
+	/* apply the clamping  limits */
+	div = max_t(u32, div, mindiv);
+	div = min_t(u32, div, maxdiv);
 
 	return div;
 }
@@ -1292,9 +1308,26 @@ static int bcm2835_clock_set_rate(struct clk_hw *hw,
 	struct bcm2835_cprman *cprman = clock->cprman;
 	const struct bcm2835_clock_data *data = clock->data;
 	u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
+	u32 ctl;
+
+	spin_lock(&cprman->regs_lock);
+
+	/*

drivers/clk/bcm/clk-bcm2835.c static int bcm2835_clock_set_rate(struct clk_hw *hw, · line 1325

Quoted 10 lines
drivers/clk/bcm/clk-bcm2835.c +10 -0
+	 * Setting up frac support
+	 *
+	 * In principle it is recommended to stop/start the clock first,
+	 * but as we set CLK_SET_RATE_GATE during registration of the
+	 * clock this requirement should be take care of by the
+	 * clk-framework.
+	 */
+	ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
+	ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
+	cprman_write(cprman, data->ctl_reg, ctl);

This should all be under "if (data->is_mash_clock) {}" since it doesn't do anything on non-mash clocks.

drivers/clk/bcm/clk-bcm2835.c static int bcm2835_clock_set_rate(struct clk_hw *hw, · line 1332

Quoted 7 lines
drivers/clk/bcm/clk-bcm2835.c +2 -0
 	cprman_write(cprman, data->div_reg, div);
 
+	spin_unlock(&cprman->regs_lock);
+
 	return 0;
 }

-------------- 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/20160229/c683e2f1/attachment.sig>

#15 lore raw

PATCH 5/6 clk: bcm2835: correctly enable fractional clock support

Quoted 38 lines · Eric Anholt
Quoted 17 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

The current driver calculates the clock divider with fractional support enabled.

But it does not enable fractional support in the control register itself resulting in an integer only divider, but in clk_set_rate responds back the fractionally divided clock frequency.

This patch enables fractional support in the control register whenever there is a fractional bit set in the requested clock divider.

Mash clock limits are are also handled for the PWM clock applying the correct divider limits (2 and max_int) applicable to basic fractional divider support (mash order of 1).

Quoted 7 lines

It also adds locking to protect the read/modify/write cycle of the register modification.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

Rewrite of the commit message:

The driver has been using fractional dividers for all clocks with fractional bits. However, MASH clocks (those used to drive audio) only do fractional division when in MASH mode, which is used to push clock jitter out of the audio band. The PWM clock (used to drive i2s audio) is the only MASH clock currently enabled in the driver.

Quoted 6 lines

Additional MASH modes are available that widen the frequency range, but only 1-level MASH is used for now, until we characterize some better policy.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks?)

That is not 100% true - non mash modes have CM_FRAC and CM_FRAC = CM_MASH0 so these map each other

And for a non-mash clock if CM_FRAC is not set then the factual divider support is not used and only the integer portion is used.
This results in a frequency that is to high (as the frac bits are missing).

Just to giv an example of firmware set clock for the timer:
root at raspcm:~# head /sys/kernel/debug/clk/timer/current_* ==> /sys/kernel/debug/clk/timer/current_divf <== 819

==> /sys/kernel/debug/clk/timer/current_divi <==
19

==> /sys/kernel/debug/clk/timer/current_frac <==
1

==> /sys/kernel/debug/clk/timer/current_parent <== xosc

root@raspcm:~# head /sys/kernel/debug/clk/timer/regdump
ctl = 0x00000291
div = 0x00013333

So this - non mash - clock has frac set by default and this clock is set up by the firmware!

Similar things for: hsm and uart - all of them are ?normal? clocks and they have the CM_FRAC bit set and divf > 0.

So the code essentially sets:
* CM_FRAC for ?normal? clocks
* CM_MASH0 for mash clocks
when the divider has a fract component.

So I believe my description is correct.

But anyway: except for the pcm clocks clk_set_rate is not really being used right now, as we still consume the clocks set up by the firmware.

The comment portion about mash-order=1 could probably get added.

As for enabling higher order mash modes - at least in the audio domain using I2S I have done some interresting measurements making use of all the variations (and clock selections).

The result there was that - at least with the DAC that I am using - the noise level varies widely between different mash modes and clock dividers.

So the ?lowest? noise showed the following combinations:
osc-mash3
pllc-mash2
osc-mash1
osc-non-frac (optimized so that we may use integer divider) osc-mash2
pllc-mash1
pllc-mash3

So the "selection? of clocks/divider/mash is really requiring fine tuning and measurement - because there is no real pattern visible.

Here a graph showing the audio spectrum for the different modes with a 400Hz signal:
https://cloud.githubusercontent.com/assets/2638784/13278003/20e09002-dacd-11e5-8d62-17eb7118a4fa.jpg

Quoted 6 lines

This is just the low bit of the 2-bit CM_MASH field at 9:10. So you're enabling MASH mode 1 in this patch. Similarly, the subject of the patch should be something like "Fix MASH-based fractional clock support for PWM" since all the other clocks work with fractional already.

As explained above: no they do not work without setting the FRAC bit!

Take again the example of timer:
when not using FRAC you get a timer clock of:
1010526Hz while with FRAC you get 1000002 Hz

So you NEED to set FRAC for all.

Quoted 8 lines

drivers/clk/bcm/clk-bcm2835.c static int bcm2835_clock_set_rate(struct clk_hw *hw, · line 1325

Quoted 5 lines
drivers/clk/bcm/clk-bcm2835.c +3 -0
+	ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
+	ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
+	cprman_write(cprman, data->ctl_reg, ctl);

This should all be under "if (data->is_mash_clock) {}" since it doesn't do anything on non-mash clocks.

Again: without CM_FRAC you get a frequency that is too high, so it needs to be implemented for all - independent of if it is a mash clock or a FRAC clock.
The only cases where it does not apply is for clocks that have no fract bits at all.

#16 lore raw

PATCH 5/6 clk: bcm2835: correctly enable fractional clock support

Quoted 41 lines · Martin Sperl
Quoted 41 lines · Eric Anholt
Quoted 17 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

The current driver calculates the clock divider with fractional support enabled.

But it does not enable fractional support in the control register itself resulting in an integer only divider, but in clk_set_rate responds back the fractionally divided clock frequency.

This patch enables fractional support in the control register whenever there is a fractional bit set in the requested clock divider.

Mash clock limits are are also handled for the PWM clock applying the correct divider limits (2 and max_int) applicable to basic fractional divider support (mash order of 1).

Quoted 7 lines

It also adds locking to protect the read/modify/write cycle of the register modification.

Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks")

Rewrite of the commit message:

The driver has been using fractional dividers for all clocks with fractional bits. However, MASH clocks (those used to drive audio) only do fractional division when in MASH mode, which is used to push clock jitter out of the audio band. The PWM clock (used to drive i2s audio) is the only MASH clock currently enabled in the driver.

Additional MASH modes are available that widen the frequency range, but only 1-level MASH is used for now, until we characterize some better policy.

Quoted 5 lines
Quoted 2 lines
Fixes 41691b8862e2 ("clk: bcm2835: Add support for programming the

audio domain clocks?)

That is not 100% true - non mash modes have CM_FRAC and CM_FRAC = CM_MASH0 so these map each other

Once again, trusting the docs turns out to be a bad idea. You're right, the non-MASH clocks *do* have a bit 9 to enable fractional mode. Sigh.

So, this patch is:

-------------- 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/20160229/9a4365e6/attachment.sig>

#17 lore raw

PATCH 5/6 clk: bcm2835: correctly enable fractional clock support

Quoted 9 lines · Eric Anholt

Once again, trusting the docs turns out to be a bad idea. You're right, the non-MASH clocks *do* have a bit 9 to enable fractional mode. Sigh.

So, this patch is:

The only really reliable ?docs? on registers I have found outside of probably signing a NDA are those header files for the VC4 provided by broadcom.

That is why I have taken the effort to translate those into something much more easily readable/searchable (i.e html pages on the web).

And that is where I have ?guessed? all the information on the clock tree structure.

The main thing that is sometimes missing from those documents is some extra context like: what is that pllt clock with a mux of 8 parent clocks (not 4 or 16 as typical) and what do those corresponding 4 counter really do?

See:
https://github.com/msperl/rpi-registers/blob/master/md/Region_CM.md#cm_plltctl

Martin

#18 lore raw

PATCH 6/6 clk: bcm2835: clean up coding style issues

In reply to kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Fix all the checkpatch complaints for clk-bcm2835.c

---
 drivers/clk/bcm/clk-bcm2835.c |   14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +5 -9
index da77069..105a42f 100644
@@ -12,9 +12,6 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 /**
@@ -300,7 +297,7 @@
 struct bcm2835_cprman {
 	struct device *dev;
 	void __iomem *regs;
-	spinlock_t regs_lock;
+	spinlock_t regs_lock; /* spinlock for all clocks */
 	const char *osc_name;
 
 	struct clk_onecell_data onecell;
@@ -328,12 +325,12 @@ void __init bcm2835_init_clocks(void)
 	int ret;
 
 	clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, CLK_IS_ROOT,
-					126000000);
+				      126000000);
 	if (IS_ERR(clk))
 		pr_err("apb_pclk not registered\n");
 
 	clk = clk_register_fixed_rate(NULL, "uart0_pclk", NULL, CLK_IS_ROOT,
-					3000000);
+				      3000000);
 	if (IS_ERR(clk))
 		pr_err("uart0_pclk not registered\n");
 	ret = clk_register_clkdev(clk, NULL, "20201000.uart");
@@ -341,7 +338,7 @@ void __init bcm2835_init_clocks(void)
 		pr_err("uart0_pclk alias not registered\n");
 
 	clk = clk_register_fixed_rate(NULL, "uart1_pclk", NULL, CLK_IS_ROOT,
-					125000000);
+				      125000000);
 	if (IS_ERR(clk))
 		pr_err("uart1_pclk not registered\n");
 	ret = clk_register_clkdev(clk, NULL, "20215000.uart");
@@ -1332,7 +1329,7 @@ static int bcm2835_clock_set_rate(struct clk_hw *hw,
 }
 
 static int bcm2835_clock_determine_rate(struct clk_hw *hw,
-		struct clk_rate_request *req)
+					struct clk_rate_request *req)
 {
 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
 	struct clk_hw *parent, *best_parent = NULL;
@@ -1390,7 +1387,6 @@ static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
 	return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
 }
 
-
 static const struct clk_ops bcm2835_clock_clk_ops = {
 	.is_prepared = bcm2835_clock_is_on,
 	.prepare = bcm2835_clock_on,
Signature · 1 line
1.7.10.4
#19 lore raw

PATCH 6/6 clk: bcm2835: clean up coding style issues

Quoted 24 lines · kernel at martin.sperl.org

From: Martin Sperl <[email protected]>

Fix all the checkpatch complaints for clk-bcm2835.c

---
 drivers/clk/bcm/clk-bcm2835.c |   14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/clk/bcm/clk-bcm2835.c +0 -3
index da77069..105a42f 100644
@@ -12,9 +12,6 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 /**

drivers/clk/bcm/clk-bcm2835.c line 301

Quoted 6 lines
drivers/clk/bcm/clk-bcm2835.c +1 -1
@@ -300,7 +297,7 @@
 struct bcm2835_cprman {
 	struct device *dev;
 	void __iomem *regs;
-	spinlock_t regs_lock;
+	spinlock_t regs_lock; /* spinlock for all clocks */
 	const char *osc_name;

I don't think this comment adds anything at all, but I understand you're trying to shut checkpatch up and I don't think there's much to say. I'd drop it, but I won't block the patch for it. Everything else in the patch looks good.

-------------- 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/20160229/c29e5ef3/attachment.sig>