i2c: busses: i2c-bcm2835: limits cdiv to allowed values
Subsystems (3)
PATCH v2 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 | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/i2c/busses/i2c-bcm2835.c +13 -0
@@ -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,22 @@ static int bcm2835_i2c_probe(struct platform_device *pdev) divider =DIV_ROUND_UP (clk_get_rate (i2c_dev->clk), bus_clk_rate);/* +* Divider results in 0 by extremely high bus_clk_rate values +* such as bus_clk_rate >= 4044967297 and 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 ) + 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 ) + 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
Quoted 37 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.pdfSigned-off-by Silvan Wicki <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +10
-0@@ -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,22 @@ static int bcm2835_i2c_probe(struct platform_device *pdev) divider =DIV_ROUND_UP (clk_get_rate (i2c_dev->clk), bus_clk_rate);/* +* Divider results in 0 by extremely high bus_clk_rate values +* such as bus_clk_rate >= 4044967297 and 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 ) + divider =BCM2835_I2C_CDIV_MIN ;
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 274
Quoted 10 lines
drivers/i2c/busses/i2c-bcm2835.c +3
-0+/* * 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 ) + divider =BCM2835_I2C_CDIV_MAX ;bcm2835_i2c_writel (i2c_dev,BCM2835_I2C_DIV , divider);
I'm not clear on the motivation of this patch. If the bus_clk_rate is something totally unreasonable (125MHz? 3.8KHz?) it seems like something is misconfigured and we should at best just fail the probe. Does this fix some particular problem you've run into?
_______________________________________________
linux-rpi-kernel mailing list linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
http://lists.infradead.org/mailman/listinfo/linux-rpi-kernel
Quoted 46 lines · Eric Anholt
Quoted 44 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.pdfSigned-off-by Silvan Wicki <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +11
-0@@ -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,22 @@ static int bcm2835_i2c_probe(struct platform_device *pdev) divider =DIV_ROUND_UP (clk_get_rate (i2c_dev->clk), bus_clk_rate);/* +* Divider results in 0 by extremely high bus_clk_rate values +* such as bus_clk_rate >= 4044967297 and 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 ) + 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++;
Quoted 8 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 274
Quoted 3 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+if (divider >BCM2835_I2C_CDIV_MAX ) + divider =BCM2835_I2C_CDIV_MAX ;bcm2835_i2c_writel (i2c_dev,BCM2835_I2C_DIV , divider);I'm not clear on the motivation of this patch. If the
bus_clk_rateis something totally unreasonable (125MHz? 3.8KHz?) it seems like something is misconfigured and we should at best just fail the probe. Does this fix some particular problem you've run into?
The motivation is to not allow wrong values to be written in CDIV. No, i was not able to fix my problem. I had to choose another clock rate. Failing the probe would be an alternative. The problem with failing is, when do we fail? I did not find values in the datasheet about what speeds are supported or not.
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 262
Quoted 8 lines
drivers/i2c/busses/i2c-bcm2835.c +5
-0@@ -252,12 +255,22 @@ static int bcm2835_i2c_probe(struct platform_device *pdev) divider =DIV_ROUND_UP (clk_get_rate (i2c_dev->clk), bus_clk_rate);/* +* Divider results in 0 by extremely high bus_clk_rate values +* such as bus_clk_rate >= 4044967297 and core_clock = 250MHz. +* In such a case assume the minimal possible divider since +* bcm2835 chip sets divisor internally to 32768 if cdiv is 0. +*/
I would rephrase that as:
/*
* A divider of0 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. */
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 264
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+if (divider <BCM2835_I2C_CDIV_MIN ) + divider =BCM2835_I2C_CDIV_MIN ;
This seems reasonable; if the calculated divider is too small, then it means we can't run the clock that fas. Running it more slowly loses some performance, but should work fine. I might suggest a dev_info() or dev_dbg() here.
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 273
Quoted 9 lines
drivers/i2c/busses/i2c-bcm2835.c +3
-0+/* * 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 ) + divider =BCM2835_I2C_CDIV_MAX ;
I think this should be an error. If the calculated divider is too large, it means we want to run the clock slower than we can. If we run the clock faster than requested, the clock rate might exceed the attached I2C devices' capabilities (otherwise, why would we want such a slow clock?). As such, I'd suggest a dev_err() here, and to fail the probe() by returning an error.
I'd echo Eric's question: I'm curious whether this patch solves a real-world problem, or if you found it by code inspection?
Quoted 37 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.pdfdrivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 262
Quoted 8 lines
drivers/i2c/busses/i2c-bcm2835.c +5
-0@@ -252,12 +255,22 @@ static int bcm2835_i2c_probe(struct platform_device *pdev) divider =DIV_ROUND_UP (clk_get_rate (i2c_dev->clk), bus_clk_rate);/* +* Divider results in 0 by extremely high bus_clk_rate values +* such as bus_clk_rate >= 4044967297 and core_clock = 250MHz. +* In such a case assume the minimal possible divider since +* bcm2835 chip sets divisor internally to 32768 if cdiv is 0. +*/ I would rephrase that as:
/*
* A divider of0 results in extremely highbus_clk_ratevalues
* such asbus_clk_rate>= 4044967297 withcore_clock= 250MHz.
* In such a case assume the minimal possible divider, since
* bcm2835 chip sets divisor internally to 32768 if cdiv is 0. */drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 264
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+if (divider <BCM2835_I2C_CDIV_MIN ) + divider =BCM2835_I2C_CDIV_MIN ;This seems reasonable; if the calculated divider is too small, then it means we can't run the clock that fas. Running it more slowly loses some performance, but should work fine. I might suggest a
dev_info()ordev_dbg()here.
Quoted 17 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_probe(struct platform_device *pdev) · line 273
Quoted 9 lines
drivers/i2c/busses/i2c-bcm2835.c +3
-0+/* * 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 ) + divider =BCM2835_I2C_CDIV_MAX ;I think this should be an error. If the calculated divider is too large, it means we want to run the clock slower than we can. If we run the clock faster than requested, the clock rate might exceed the attached I2C devices' capabilities (otherwise, why would we want such a slow clock?). As such, I'd suggest a
dev_err()here, and to fail theprobe()by returning an error.
i made an updated patch, v3.
Quoted 2 lines
I'd echo Eric's question: I'm curious whether this patch solves a real-world problem, or if you found it by code inspection?
i used i2c on the raspberry pi with an atmega328p. when i was testing with diffrent i2c speeds it dit not always work as expected. so it was kind of both, a problem i encountered in real-world that lead to code inspection. so i analyzed the driver and found some issues.
one thing is this patch with the DIV register.
one thing is that in bcm2835_i2c_isr the S register is read and then directly written back without ensuring that the reserved bits (10-31) are cleared. the datasheet mentions: write as 0, read don't care. it is not guaranteed that the reserved bits will always be 0. but we need to write a 0. so the proper way i think is to clear the bits with a bitmask. i think there are more registers which we may need to clear the reserved bits. bitmask for S register would be 0x03FF.
one thing is that the DEL register is never adjusted. so if cdiv/2 gets higher than FEDL/REDL the BSC may malfunction. the datasheet mentions:
the delay values should always be set to less than cdiv/2.
and it would be nice to have the ability to manipulate the CLKT->TOUT value. to change the clock stretching timeout.
i'm new kernel development and find it best to do only one patch at a time.
cheers