diff options
Diffstat (limited to 'drivers/w1/masters/mxc_w1.c')
-rw-r--r-- | drivers/w1/masters/mxc_w1.c | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/drivers/w1/masters/mxc_w1.c b/drivers/w1/masters/mxc_w1.c index a5df5e89d456..da3d0f0ad63c 100644 --- a/drivers/w1/masters/mxc_w1.c +++ b/drivers/w1/masters/mxc_w1.c @@ -15,16 +15,13 @@ #include <linux/clk.h> #include <linux/delay.h> #include <linux/io.h> +#include <linux/jiffies.h> #include <linux/module.h> #include <linux/platform_device.h> #include "../w1.h" #include "../w1_int.h" -/* According to the mx27 Datasheet the reset procedure should take up to about - * 1350us. We set the timeout to 500*100us = 50ms for sure */ -#define MXC_W1_RESET_TIMEOUT 500 - /* * MXC W1 Register offsets */ @@ -35,6 +32,7 @@ # define MXC_W1_CONTROL_RPP BIT(7) #define MXC_W1_TIME_DIVIDER 0x02 #define MXC_W1_RESET 0x04 +# define MXC_W1_RESET_RST BIT(0) struct mxc_w1_device { void __iomem *regs; @@ -49,24 +47,25 @@ struct mxc_w1_device { */ static u8 mxc_w1_ds2_reset_bus(void *data) { - u8 reg_val; - unsigned int timeout_cnt = 0; struct mxc_w1_device *dev = data; + unsigned long timeout; + + writeb(MXC_W1_CONTROL_RPP, dev->regs + MXC_W1_CONTROL); - writeb(MXC_W1_CONTROL_RPP, (dev->regs + MXC_W1_CONTROL)); + /* Wait for reset sequence 511+512us, use 1500us for sure */ + timeout = jiffies + usecs_to_jiffies(1500); - while (1) { - reg_val = readb(dev->regs + MXC_W1_CONTROL); + udelay(511 + 512); - if (!(reg_val & MXC_W1_CONTROL_RPP) || - timeout_cnt > MXC_W1_RESET_TIMEOUT) - break; - else - timeout_cnt++; + do { + u8 ctrl = readb(dev->regs + MXC_W1_CONTROL); - udelay(100); - } - return !(reg_val & MXC_W1_CONTROL_PST); + /* PST bit is valid after the RPP bit is self-cleared */ + if (!(ctrl & MXC_W1_CONTROL_RPP)) + return !(ctrl & MXC_W1_CONTROL_PST); + } while (time_is_after_jiffies(timeout)); + + return 1; } /* @@ -76,22 +75,25 @@ static u8 mxc_w1_ds2_reset_bus(void *data) */ static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit) { - struct mxc_w1_device *mdev = data; - void __iomem *ctrl_addr = mdev->regs + MXC_W1_CONTROL; - unsigned int timeout_cnt = 400; /* Takes max. 120us according to - * datasheet. - */ + struct mxc_w1_device *dev = data; + unsigned long timeout; - writeb(MXC_W1_CONTROL_WR(bit), ctrl_addr); + writeb(MXC_W1_CONTROL_WR(bit), dev->regs + MXC_W1_CONTROL); - while (timeout_cnt--) { - if (!(readb(ctrl_addr) & MXC_W1_CONTROL_WR(bit))) - break; + /* Wait for read/write bit (60us, Max 120us), use 200us for sure */ + timeout = jiffies + usecs_to_jiffies(200); - udelay(1); - } + udelay(60); - return !!(readb(ctrl_addr) & MXC_W1_CONTROL_RDST); + do { + u8 ctrl = readb(dev->regs + MXC_W1_CONTROL); + + /* RDST bit is valid after the WR1/RD bit is self-cleared */ + if (!(ctrl & MXC_W1_CONTROL_WR(bit))) + return !!(ctrl & MXC_W1_CONTROL_RDST); + } while (time_is_after_jiffies(timeout)); + + return 0; } static int mxc_w1_probe(struct platform_device *pdev) @@ -131,6 +133,10 @@ static int mxc_w1_probe(struct platform_device *pdev) if (err) return err; + /* Software reset 1-Wire module */ + writeb(MXC_W1_RESET_RST, mdev->regs + MXC_W1_RESET); + writeb(0, mdev->regs + MXC_W1_RESET); + writeb(clkdiv - 1, mdev->regs + MXC_W1_TIME_DIVIDER); mdev->bus_master.data = mdev; |