Skip to contentSkip to conversation

clk: fractional-divider: prevent division by zero

PATCH In review Heikki Krogerus 2 participants 3 messages Jan 16, 2015 – Jan 19, 2015 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: fractional-divider: prevent division by zero

Preventing division by zero condition by making sure that the initial n and m values are not 0.

---
 drivers/clk/clk-fractional-divider.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk-fractional-divider.c +9 -0
index 82a59d0..dc91da7 100644
@@ -104,6 +104,7 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	struct clk_fractional_divider *fd;
 	struct clk_init_data init;
 	struct clk *clk;
+	u32 val;
 
 	fd = kzalloc(sizeof(*fd), GFP_KERNEL);
 	if (!fd) {
@@ -126,6 +127,14 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	fd->lock = lock;
 	fd->hw.init = &init;
 
+	/* Prevent division by zero */
+	val = clk_readl(fd->reg);
+	if (!(val & fd->nmask))
+		val |= 1 << fd->nshift;
+	if (!(val & fd->mmask))
+		val |= 1 << fd->mshift;
+	clk_writel(val, fd->reg);
+
 	clk = clk_register(dev, &fd->hw);
 	if (IS_ERR(clk))
 		kfree(fd);
Signature · 1 line
2.1.4
#1 lore raw
Quoted 25 lines · Heikki Krogerus

Preventing division by zero condition by making sure that the initial n and m values are not 0.

---
 drivers/clk/clk-fractional-divider.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk-fractional-divider.c +2 -0
index 82a59d0..dc91da7 100644
@@ -104,6 +104,7 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	struct clk_fractional_divider *fd;
 	struct clk_init_data init;
 	struct clk *clk;
+	u32 val;
 
 	fd = kzalloc(sizeof(*fd), GFP_KERNEL);
 	if (!fd) {
@@ -126,6 +127,14 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	fd->lock = lock;
 	fd->hw.init = &init;
 
+	/* Prevent division by zero */

drivers/clk/clk-fractional-divider.c struct clk *clk_register_fractional_divider(struct device *dev, · line 140

Quoted 10 lines
drivers/clk/clk-fractional-divider.c +7 -0
+	val = clk_readl(fd->reg);
+	if (!(val & fd->nmask))
+		val |= 1 << fd->nshift;
+	if (!(val & fd->mmask))
+		val |= 1 << fd->mshift;
+	clk_writel(val, fd->reg);
+
 	clk = clk_register(dev, &fd->hw);
 	if (IS_ERR(clk))
 		kfree(fd);

Sorry I don't understand this at all. Does your hardware support a value of 0 in the register? Doesn't that mean "bypass" so that the rate of the parent bypasses the divider? If so, why aren't we fixing the recalc_rate() logic to check for a special case of 0?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
Quoted 25 lines · Stephen Boyd
Quoted 24 lines · Heikki Krogerus

Preventing division by zero condition by making sure that the initial n and m values are not 0.

---
 drivers/clk/clk-fractional-divider.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/clk/clk-fractional-divider.c +1 -0
index 82a59d0..dc91da7 100644
@@ -104,6 +104,7 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	struct clk_fractional_divider *fd;
 	struct clk_init_data init;
 	struct clk *clk;
+	u32 val;
 
 	fd = kzalloc(sizeof(*fd), GFP_KERNEL);
 	if (!fd) {
@@ -126,6 +127,14 @@ struct clk *clk_register_fractional_divider(struct device *dev,
 	fd->lock = lock;
 	fd->hw.init = &init;
 
Quoted 16 lines

drivers/clk/clk-fractional-divider.c struct clk *clk_register_fractional_divider(struct device *dev, · line 140

Quoted 11 lines
drivers/clk/clk-fractional-divider.c +8 -0
+	/* Prevent division by zero */
+	val = clk_readl(fd->reg);
+	if (!(val & fd->nmask))
+		val |= 1 << fd->nshift;
+	if (!(val & fd->mmask))
+		val |= 1 << fd->mshift;
+	clk_writel(val, fd->reg);
+
 	clk = clk_register(dev, &fd->hw);
 	if (IS_ERR(clk))
 		kfree(fd);

Sorry I don't understand this at all. Does your hardware support a value of 0 in the register? Doesn't that mean "bypass" so that the rate of the parent bypasses the divider? If so, why aren't we fixing the recalc_rate() logic to check for a special case of 0?

OK. Makes sense.

Signature · 1 line
heikki