Skip to contentSkip to conversation

i2c: busses: i2c-bcm2835: limits cdiv to allowed values

PATCH Superseded Silvan Wicki 3 participants 4 messages Jun 6, 2015 – Jun 17, 2015 2 messages not in the archive

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 i2c: busses: i2c-bcm2835: limits cdiv to allowed values

Adds: make sure bits 16-31 of DIV register are always 0
Adds: assume minimal divider of 2 if divider resulted in 0
(bcm2835 sets divider to 32768 if cdiv is set to 0)

See page 33/34 of BCM2835-ARM-Peripherals.pdf for the DIV register.
https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

---
 drivers/i2c/busses/i2c-bcm2835.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/i2c/busses/i2c-bcm2835.c +19 -0
index c9336a3..7a5b96d 100644
@@ -50,6 +50,9 @@
 #define BCM2835_I2C_S_CLKT	BIT(9)
 #define BCM2835_I2C_S_LEN	BIT(10) /* Fake bit for SW error reporting */
 
+#define BCM2835_I2C_CDIV_MIN   0x0002
+#define BCM2835_I2C_CDIV_MAX   0xFFFE
+
 #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
 
 struct bcm2835_i2c_dev {
@@ -252,12 +255,28 @@ static int bcm2835_i2c_probe(struct platform_device *pdev)
 
 	divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
 	/*
+	 * A divider of 0 results in extremely high bus_clk_rate values
+	 * such as bus_clk_rate >= 4044967297 with core_clock = 250MHz.
+	 * In such a case assume the minimal possible divider, since
+	 * bcm2835 chip sets divisor internally to 32768 if cdiv is 0.
+	 */
+	if (divider < BCM2835_I2C_CDIV_MIN) {
+		dev_warn(&pdev->dev,
+			 "Clock too fast, falling back to max clock speed\n");
+		divider = BCM2835_I2C_CDIV_MIN;
+	}
+	/*
 	 * Per the datasheet, the register is always interpreted as an even
 	 * number, by rounding down. In other words, the LSB is ignored. So,
 	 * if the LSB is set, increment the divider to avoid any issue.
 	 */
 	if (divider & 1)
 		divider++;
+	if (divider > BCM2835_I2C_CDIV_MAX) {
+		dev_warn(&pdev->dev,
+			 "Clock too slow, falling back to min clock speed\n");
+		divider = BCM2835_I2C_CDIV_MAX;
+	}
 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
 
 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Signature · 1 line
2.1.4
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Silvan Wicki

Adds: make sure bits 16-31 of DIV register are always 0
Adds: assume minimal divider of 2 if divider resulted in 0
(bcm2835 sets divider to 32768 if cdiv is set to 0)

See page 33/34 of BCM2835-ARM-Peripherals.pdf for the DIV register.
https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

---

Please provide a small changelog below the --- line, like:

Changes since V3: ...

I get so many patches and can't figure that out on my own.

Thanks!

#3 lore raw
In reply to a message not in the archive
Quoted 10 lines · Silvan Wicki

Adds: make sure bits 16-31 of DIV register are always 0
Adds: assume minimal divider of 2 if divider resulted in 0
(bcm2835 sets divider to 32768 if cdiv is set to 0)

See page 33/34 of BCM2835-ARM-Peripherals.pdf for the DIV register.
https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 261

Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +5 -0
@@ -252,12 +255,28 @@ static int bcm2835_i2c_probe(struct platform_device *pdev)
 
+	if (divider > BCM2835_I2C_CDIV_MAX) {
+		dev_warn(&pdev->dev,
+			 "Clock too slow, falling back to min clock speed\n");
+		divider = BCM2835_I2C_CDIV_MAX;
+	}
 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);

Shouldn't this be an error? i.e. dev_err() not dev_warn(), and return an error code.

If the divider that's required to reach the requested clock is higher than the HW can implement, that means the actual clock will be faster than the requested clock. Presumably there's a reason the specific slow rate was requested, and if we exceed it, there will be problems.

#5 lore raw
In reply to a message not in the archive
Quoted 22 lines · Stephen Warren
Quoted 10 lines · Silvan Wicki

Adds: make sure bits 16-31 of DIV register are always 0
Adds: assume minimal divider of 2 if divider resulted in 0
(bcm2835 sets divider to 32768 if cdiv is set to 0)

See page 33/34 of BCM2835-ARM-Peripherals.pdf for the DIV register.
https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 261

Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +5 -0
@@ -252,12 +255,28 @@ static int bcm2835_i2c_probe(struct platform_device *pdev)
 
+	if (divider > BCM2835_I2C_CDIV_MAX) {
+		dev_warn(&pdev->dev,
+			 "Clock too slow, falling back to min clock speed\n");
+		divider = BCM2835_I2C_CDIV_MAX;
+	}
 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);

Shouldn't this be an error? i.e. dev_err() not dev_warn(), and return an error code.

Quoted 4 lines

If the divider that's required to reach the requested clock is higher than the HW can implement, that means the actual clock will be faster than the requested clock. Presumably there's a reason the specific slow rate was requested, and if we exceed it, there will be problems.

i searched for similar things in other i2c drivers and found that i2c-bcm-iproc, i2c-digicolor do a dev_err. so it seems like dev_err is the way it should be implemented. i'll update that.