blob: 1881a3b1f14ca4978917b8418efca1876392d0c5 [file] [log] [blame]
Martin Fuzzey6215c6d2018-10-24 10:21:18 +02001// 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
20#include <asm/arch/clock.h>
21#include <common.h>
22#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Martin Fuzzey6215c6d2018-10-24 10:21:18 +020025#include <linux/io.h>
26#include <w1.h>
27
28struct 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
50struct 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 */
59static 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
79static u8 mxc_w1_read_byte(struct udevice *dev)
80{
81 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
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(&regs->tx_rx);
96 writew(0xFF, &regs->tx_rx);
97
98 do {
99 udelay(1); /* Without this bytes are sometimes duplicated... */
100 status = readw(&regs->interrupt);
101 } while (!(status & MXC_W1_INTERRUPT_RBF));
102
103 return (u8)readw(&regs->tx_rx);
104}
105
106static void mxc_w1_write_byte(struct udevice *dev, u8 byte)
107{
108 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
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(&regs->tx_rx);
122 writew(byte, &regs->tx_rx);
123
124 do {
125 udelay(1);
126 status = readw(&regs->interrupt);
127 } while (!(status & MXC_W1_INTERRUPT_TSRE));
128}
129
130static bool mxc_w1_reset(struct udevice *dev)
131{
132 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
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
144static u8 mxc_w1_triplet(struct udevice *dev, bool bdir)
145{
146 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
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
168static int mxc_w1_ofdata_to_platdata(struct udevice *dev)
169{
170 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
171 fdt_addr_t addr;
172
173 addr = devfdt_get_addr(dev);
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
182static int mxc_w1_probe(struct udevice *dev)
183{
184 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
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
206static 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
213static 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
226U_BOOT_DRIVER(mxc_w1_drv) = {
227 .id = UCLASS_W1,
228 .name = "mxc_w1_drv",
229 .of_match = mxc_w1_id,
230 .ofdata_to_platdata = mxc_w1_ofdata_to_platdata,
231 .ops = &mxc_w1_ops,
232 .platdata_auto_alloc_size = sizeof(struct mxc_w1_pdata),
233 .probe = mxc_w1_probe,
234};