Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Driver for one wire controller in some i.MX Socs |
| 4 | * |
| 5 | * There are currently two silicon variants: |
| 6 | * V1: i.MX21, i.MX27, i.MX31, i.MX51 |
| 7 | * V2: i.MX25, i.MX35, i.MX50, i.MX53 |
| 8 | * Newer i.MX SoCs such as the i.MX6 do not have one wire controllers. |
| 9 | * |
| 10 | * The V1 controller only supports single bit operations. |
| 11 | * The V2 controller is backwards compatible on the register level but adds |
| 12 | * byte size operations and a "search ROM accelerator mode" |
| 13 | * |
| 14 | * This driver does not currently support the search ROM accelerator |
| 15 | * |
| 16 | * Copyright (c) 2018 Flowbird |
| 17 | * Martin Fuzzey <martin.fuzzey@flowbird.group> |
| 18 | */ |
| 19 | |
Martin Fuzzey | 5767faf | 2021-01-13 11:21:03 +0100 | [diff] [blame] | 20 | #include <asm/arch/clock.h> |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 21 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 22 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 25 | #include <linux/io.h> |
| 26 | #include <w1.h> |
| 27 | |
| 28 | struct mxc_w1_regs { |
| 29 | u16 control; |
| 30 | #define MXC_W1_CONTROL_RPP BIT(7) |
| 31 | #define MXC_W1_CONTROL_PST BIT(6) |
| 32 | #define MXC_W1_CONTROL_WR(x) BIT(5 - (x)) |
| 33 | #define MXC_W1_CONTROL_RDST BIT(3) |
| 34 | |
| 35 | u16 time_divider; |
| 36 | u16 reset; |
| 37 | |
| 38 | /* Registers below on V2 silicon only */ |
| 39 | u16 command; |
| 40 | u16 tx_rx; |
| 41 | u16 interrupt; |
| 42 | #define MXC_W1_INTERRUPT_TBE BIT(2) |
| 43 | #define MXC_W1_INTERRUPT_TSRE BIT(3) |
| 44 | #define MXC_W1_INTERRUPT_RBF BIT(4) |
| 45 | #define MXC_W1_INTERRUPT_RSRF BIT(5) |
| 46 | |
| 47 | u16 interrupt_en; |
| 48 | }; |
| 49 | |
| 50 | struct mxc_w1_pdata { |
| 51 | struct mxc_w1_regs *regs; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * this is the low level routine to read/write a bit on the One Wire |
| 56 | * interface on the hardware. It does write 0 if parameter bit is set |
| 57 | * to 0, otherwise a write 1/read. |
| 58 | */ |
| 59 | static u8 mxc_w1_touch_bit(struct mxc_w1_pdata *pdata, u8 bit) |
| 60 | { |
| 61 | u16 *ctrl_addr = &pdata->regs->control; |
| 62 | u16 mask = MXC_W1_CONTROL_WR(bit); |
| 63 | unsigned int timeout_cnt = 400; /* Takes max. 120us according to |
| 64 | * datasheet. |
| 65 | */ |
| 66 | |
| 67 | writew(mask, ctrl_addr); |
| 68 | |
| 69 | while (timeout_cnt--) { |
| 70 | if (!(readw(ctrl_addr) & mask)) |
| 71 | break; |
| 72 | |
| 73 | udelay(1); |
| 74 | } |
| 75 | |
| 76 | return (readw(ctrl_addr) & MXC_W1_CONTROL_RDST) ? 1 : 0; |
| 77 | } |
| 78 | |
| 79 | static u8 mxc_w1_read_byte(struct udevice *dev) |
| 80 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 81 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 82 | struct mxc_w1_regs *regs = pdata->regs; |
| 83 | u16 status; |
| 84 | |
| 85 | if (dev_get_driver_data(dev) < 2) { |
| 86 | int i; |
| 87 | u8 ret = 0; |
| 88 | |
| 89 | for (i = 0; i < 8; i++) |
| 90 | ret |= (mxc_w1_touch_bit(pdata, 1) << i); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | readw(®s->tx_rx); |
| 96 | writew(0xFF, ®s->tx_rx); |
| 97 | |
| 98 | do { |
| 99 | udelay(1); /* Without this bytes are sometimes duplicated... */ |
| 100 | status = readw(®s->interrupt); |
| 101 | } while (!(status & MXC_W1_INTERRUPT_RBF)); |
| 102 | |
| 103 | return (u8)readw(®s->tx_rx); |
| 104 | } |
| 105 | |
| 106 | static void mxc_w1_write_byte(struct udevice *dev, u8 byte) |
| 107 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 108 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 109 | struct mxc_w1_regs *regs = pdata->regs; |
| 110 | u16 status; |
| 111 | |
| 112 | if (dev_get_driver_data(dev) < 2) { |
| 113 | int i; |
| 114 | |
| 115 | for (i = 0; i < 8; i++) |
| 116 | mxc_w1_touch_bit(pdata, (byte >> i) & 0x1); |
| 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | readw(®s->tx_rx); |
| 122 | writew(byte, ®s->tx_rx); |
| 123 | |
| 124 | do { |
| 125 | udelay(1); |
| 126 | status = readw(®s->interrupt); |
| 127 | } while (!(status & MXC_W1_INTERRUPT_TSRE)); |
| 128 | } |
| 129 | |
| 130 | static bool mxc_w1_reset(struct udevice *dev) |
| 131 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 132 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 133 | u16 reg_val; |
| 134 | |
| 135 | writew(MXC_W1_CONTROL_RPP, &pdata->regs->control); |
| 136 | |
| 137 | do { |
| 138 | reg_val = readw(&pdata->regs->control); |
| 139 | } while (reg_val & MXC_W1_CONTROL_RPP); |
| 140 | |
| 141 | return !(reg_val & MXC_W1_CONTROL_PST); |
| 142 | } |
| 143 | |
| 144 | static u8 mxc_w1_triplet(struct udevice *dev, bool bdir) |
| 145 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 146 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 147 | u8 id_bit = mxc_w1_touch_bit(pdata, 1); |
| 148 | u8 comp_bit = mxc_w1_touch_bit(pdata, 1); |
| 149 | u8 retval; |
| 150 | |
| 151 | if (id_bit && comp_bit) |
| 152 | return 0x03; /* error */ |
| 153 | |
| 154 | if (!id_bit && !comp_bit) { |
| 155 | /* Both bits are valid, take the direction given */ |
| 156 | retval = bdir ? 0x04 : 0; |
| 157 | } else { |
| 158 | /* Only one bit is valid, take that direction */ |
| 159 | bdir = id_bit; |
| 160 | retval = id_bit ? 0x05 : 0x02; |
| 161 | } |
| 162 | |
| 163 | mxc_w1_touch_bit(pdata, bdir); |
| 164 | |
| 165 | return retval; |
| 166 | } |
| 167 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 168 | static int mxc_w1_of_to_plat(struct udevice *dev) |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 169 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 170 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 171 | fdt_addr_t addr; |
| 172 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 173 | addr = dev_read_addr(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 174 | if (addr == FDT_ADDR_T_NONE) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | pdata->regs = (struct mxc_w1_regs *)addr; |
| 178 | |
| 179 | return 0; |
| 180 | }; |
| 181 | |
| 182 | static int mxc_w1_probe(struct udevice *dev) |
| 183 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 184 | struct mxc_w1_pdata *pdata = dev_get_plat(dev); |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 185 | unsigned int clkrate = mxc_get_clock(MXC_IPG_PERCLK); |
| 186 | unsigned int clkdiv; |
| 187 | |
| 188 | if (clkrate < 10000000) { |
| 189 | dev_err(dev, "input clock frequency (%u Hz) too low\n", |
| 190 | clkrate); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | clkdiv = clkrate / 1000000; |
| 195 | clkrate /= clkdiv; |
| 196 | if (clkrate < 980000 || clkrate > 1020000) { |
| 197 | dev_err(dev, "Incorrect time base frequency %u Hz\n", clkrate); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | writew(clkdiv - 1, &pdata->regs->time_divider); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static const struct w1_ops mxc_w1_ops = { |
| 207 | .read_byte = mxc_w1_read_byte, |
| 208 | .reset = mxc_w1_reset, |
| 209 | .triplet = mxc_w1_triplet, |
| 210 | .write_byte = mxc_w1_write_byte, |
| 211 | }; |
| 212 | |
| 213 | static const struct udevice_id mxc_w1_id[] = { |
| 214 | { .compatible = "fsl,imx21-owire", .data = 1 }, |
| 215 | { .compatible = "fsl,imx27-owire", .data = 1 }, |
| 216 | { .compatible = "fsl,imx31-owire", .data = 1 }, |
| 217 | { .compatible = "fsl,imx51-owire", .data = 1 }, |
| 218 | |
| 219 | { .compatible = "fsl,imx25-owire", .data = 2 }, |
| 220 | { .compatible = "fsl,imx35-owire", .data = 2 }, |
| 221 | { .compatible = "fsl,imx50-owire", .data = 2 }, |
| 222 | { .compatible = "fsl,imx53-owire", .data = 2 }, |
| 223 | { }, |
| 224 | }; |
| 225 | |
| 226 | U_BOOT_DRIVER(mxc_w1_drv) = { |
| 227 | .id = UCLASS_W1, |
| 228 | .name = "mxc_w1_drv", |
| 229 | .of_match = mxc_w1_id, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 230 | .of_to_plat = mxc_w1_of_to_plat, |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 231 | .ops = &mxc_w1_ops, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 232 | .plat_auto = sizeof(struct mxc_w1_pdata), |
Martin Fuzzey | 6215c6d | 2018-10-24 10:21:18 +0200 | [diff] [blame] | 233 | .probe = mxc_w1_probe, |
| 234 | }; |