1 From b039a10b277af6aeb245e3b791e2dd4ab161698c Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.com>
3 Date: Thu, 11 Jan 2024 16:33:22 +0000
4 Subject: [PATCH 0849/1085] drivers: w1-gpio: add flag to force read-polling
7 On Pi 5, the link to RP1 will bounce in and out of L1 depending on
8 inactivity timers at both the RC and EP end. Unfortunately for
9 bitbashing 1-wire, this means that on an otherwise idle Pi 5 many of the
10 reads/writes to GPIO registers are delayed by up to 8us which causes
11 mis-sampling of read data and trashes write bits.
13 By issuing dummy reads at a rate greater than the link inactivity
14 timeout while spinning on a delay, PCIe stays in L0 which does not incur
17 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
19 drivers/w1/masters/w1-gpio.c | 3 +++
20 drivers/w1/w1_io.c | 37 ++++++++++++++++++++++++------------
21 include/linux/w1.h | 5 +++++
22 3 files changed, 33 insertions(+), 12 deletions(-)
24 --- a/drivers/w1/masters/w1-gpio.c
25 +++ b/drivers/w1/masters/w1-gpio.c
26 @@ -90,6 +90,9 @@ static int w1_gpio_probe(struct platform
27 if (of_property_present(np, "linux,open-drain"))
28 gflags = GPIOD_OUT_LOW;
30 + if (of_property_present(np, "raspberrypi,delay-needs-poll"))
31 + master->delay_needs_poll = true;
33 pdev->dev.platform_data = pdata;
35 pdata = dev_get_platdata(dev);
36 --- a/drivers/w1/w1_io.c
37 +++ b/drivers/w1/w1_io.c
41 #include <linux/delay.h>
42 +#include <linux/ktime.h>
43 #include <linux/moduleparam.h>
44 #include <linux/module.h>
46 @@ -36,9 +37,21 @@ static u8 w1_crc8_table[] = {
47 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
50 -static void w1_delay(unsigned long tm)
51 +static void w1_delay(struct w1_master *dev, unsigned long tm)
53 - udelay(tm * w1_delay_parm);
54 + ktime_t start, delta;
56 + if (!dev->bus_master->delay_needs_poll) {
57 + udelay(tm * w1_delay_parm);
61 + start = ktime_get();
62 + delta = ktime_add(start, ns_to_ktime(1000 * tm * w1_delay_parm));
64 + dev->bus_master->read_bit(dev->bus_master->data);
66 + } while (ktime_before(ktime_get(), delta));
69 static void w1_write_bit(struct w1_master *dev, int bit);
70 @@ -77,14 +90,14 @@ static void w1_write_bit(struct w1_maste
73 dev->bus_master->write_bit(dev->bus_master->data, 0);
76 dev->bus_master->write_bit(dev->bus_master->data, 1);
80 dev->bus_master->write_bit(dev->bus_master->data, 0);
83 dev->bus_master->write_bit(dev->bus_master->data, 1);
88 if(w1_disable_irqs) local_irq_restore(flags);
89 @@ -164,14 +177,14 @@ static u8 w1_read_bit(struct w1_master *
90 /* sample timing is critical here */
91 local_irq_save(flags);
92 dev->bus_master->write_bit(dev->bus_master->data, 0);
95 dev->bus_master->write_bit(dev->bus_master->data, 1);
99 result = dev->bus_master->read_bit(dev->bus_master->data);
100 local_irq_restore(flags);
107 @@ -333,16 +346,16 @@ int w1_reset_bus(struct w1_master *dev)
108 * cpu for such a short amount of time AND get it back in
109 * the maximum amount of time.
112 + w1_delay(dev, 500);
113 dev->bus_master->write_bit(dev->bus_master->data, 1);
117 result = dev->bus_master->read_bit(dev->bus_master->data) & 0x1;
118 /* minimum 70 (above) + 430 = 500 us
119 * There aren't any timing requirements between a reset and
120 * the following transactions. Sleeping is safe here.
122 - /* w1_delay(430); min required time */
123 + /* w1_delay(dev, 430); min required time */
127 --- a/include/linux/w1.h
128 +++ b/include/linux/w1.h
129 @@ -121,6 +121,9 @@ typedef void (*w1_slave_found_callback)(
130 * @dev_id: Optional device id string, which w1 slaves could use for
131 * creating names, which then give a connection to the w1 master
133 + * @delay_needs_poll: work around jitter introduced with GPIO controllers
134 + * accessed over PCIe (RP1)
136 * Note: read_bit and write_bit are very low level functions and should only
137 * be used with hardware that doesn't really support 1-wire operations,
138 * like a parallel/serial port.
139 @@ -155,6 +158,8 @@ struct w1_bus_master {
140 u8, w1_slave_found_callback);
144 + bool delay_needs_poll;