blob: 08715c6a666a431550644cfaf0cc014e712deeba [file] [log] [blame]
Martin Fuzzeya2e99a72018-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 Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Martin Fuzzeya2e99a72018-10-24 10:21:18 +020024#include <linux/io.h>
25#include <w1.h>
26
27struct mxc_w1_regs {
28 u16 control;
29#define MXC_W1_CONTROL_RPP BIT(7)
30#define MXC_W1_CONTROL_PST BIT(6)
31#define MXC_W1_CONTROL_WR(x) BIT(5 - (x))
32#define MXC_W1_CONTROL_RDST BIT(3)
33
34 u16 time_divider;
35 u16 reset;
36
37 /* Registers below on V2 silicon only */
38 u16 command;
39 u16 tx_rx;
40 u16 interrupt;
41#define MXC_W1_INTERRUPT_TBE BIT(2)
42#define MXC_W1_INTERRUPT_TSRE BIT(3)
43#define MXC_W1_INTERRUPT_RBF BIT(4)
44#define MXC_W1_INTERRUPT_RSRF BIT(5)
45
46 u16 interrupt_en;
47};
48
49struct mxc_w1_pdata {
50 struct mxc_w1_regs *regs;
51};
52
53/*
54 * this is the low level routine to read/write a bit on the One Wire
55 * interface on the hardware. It does write 0 if parameter bit is set
56 * to 0, otherwise a write 1/read.
57 */
58static u8 mxc_w1_touch_bit(struct mxc_w1_pdata *pdata, u8 bit)
59{
60 u16 *ctrl_addr = &pdata->regs->control;
61 u16 mask = MXC_W1_CONTROL_WR(bit);
62 unsigned int timeout_cnt = 400; /* Takes max. 120us according to
63 * datasheet.
64 */
65
66 writew(mask, ctrl_addr);
67
68 while (timeout_cnt--) {
69 if (!(readw(ctrl_addr) & mask))
70 break;
71
72 udelay(1);
73 }
74
75 return (readw(ctrl_addr) & MXC_W1_CONTROL_RDST) ? 1 : 0;
76}
77
78static u8 mxc_w1_read_byte(struct udevice *dev)
79{
80 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
81 struct mxc_w1_regs *regs = pdata->regs;
82 u16 status;
83
84 if (dev_get_driver_data(dev) < 2) {
85 int i;
86 u8 ret = 0;
87
88 for (i = 0; i < 8; i++)
89 ret |= (mxc_w1_touch_bit(pdata, 1) << i);
90
91 return ret;
92 }
93
94 readw(&regs->tx_rx);
95 writew(0xFF, &regs->tx_rx);
96
97 do {
98 udelay(1); /* Without this bytes are sometimes duplicated... */
99 status = readw(&regs->interrupt);
100 } while (!(status & MXC_W1_INTERRUPT_RBF));
101
102 return (u8)readw(&regs->tx_rx);
103}
104
105static void mxc_w1_write_byte(struct udevice *dev, u8 byte)
106{
107 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
108 struct mxc_w1_regs *regs = pdata->regs;
109 u16 status;
110
111 if (dev_get_driver_data(dev) < 2) {
112 int i;
113
114 for (i = 0; i < 8; i++)
115 mxc_w1_touch_bit(pdata, (byte >> i) & 0x1);
116
117 return;
118 }
119
120 readw(&regs->tx_rx);
121 writew(byte, &regs->tx_rx);
122
123 do {
124 udelay(1);
125 status = readw(&regs->interrupt);
126 } while (!(status & MXC_W1_INTERRUPT_TSRE));
127}
128
129static bool mxc_w1_reset(struct udevice *dev)
130{
131 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
132 u16 reg_val;
133
134 writew(MXC_W1_CONTROL_RPP, &pdata->regs->control);
135
136 do {
137 reg_val = readw(&pdata->regs->control);
138 } while (reg_val & MXC_W1_CONTROL_RPP);
139
140 return !(reg_val & MXC_W1_CONTROL_PST);
141}
142
143static u8 mxc_w1_triplet(struct udevice *dev, bool bdir)
144{
145 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
146 u8 id_bit = mxc_w1_touch_bit(pdata, 1);
147 u8 comp_bit = mxc_w1_touch_bit(pdata, 1);
148 u8 retval;
149
150 if (id_bit && comp_bit)
151 return 0x03; /* error */
152
153 if (!id_bit && !comp_bit) {
154 /* Both bits are valid, take the direction given */
155 retval = bdir ? 0x04 : 0;
156 } else {
157 /* Only one bit is valid, take that direction */
158 bdir = id_bit;
159 retval = id_bit ? 0x05 : 0x02;
160 }
161
162 mxc_w1_touch_bit(pdata, bdir);
163
164 return retval;
165}
166
167static int mxc_w1_ofdata_to_platdata(struct udevice *dev)
168{
169 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
170 fdt_addr_t addr;
171
172 addr = devfdt_get_addr(dev);
173 if (addr == FDT_ADDR_T_NONE)
174 return -EINVAL;
175
176 pdata->regs = (struct mxc_w1_regs *)addr;
177
178 return 0;
179};
180
181static int mxc_w1_probe(struct udevice *dev)
182{
183 struct mxc_w1_pdata *pdata = dev_get_platdata(dev);
184 unsigned int clkrate = mxc_get_clock(MXC_IPG_PERCLK);
185 unsigned int clkdiv;
186
187 if (clkrate < 10000000) {
188 dev_err(dev, "input clock frequency (%u Hz) too low\n",
189 clkrate);
190 return -EINVAL;
191 }
192
193 clkdiv = clkrate / 1000000;
194 clkrate /= clkdiv;
195 if (clkrate < 980000 || clkrate > 1020000) {
196 dev_err(dev, "Incorrect time base frequency %u Hz\n", clkrate);
197 return -EINVAL;
198 }
199
200 writew(clkdiv - 1, &pdata->regs->time_divider);
201
202 return 0;
203}
204
205static const struct w1_ops mxc_w1_ops = {
206 .read_byte = mxc_w1_read_byte,
207 .reset = mxc_w1_reset,
208 .triplet = mxc_w1_triplet,
209 .write_byte = mxc_w1_write_byte,
210};
211
212static const struct udevice_id mxc_w1_id[] = {
213 { .compatible = "fsl,imx21-owire", .data = 1 },
214 { .compatible = "fsl,imx27-owire", .data = 1 },
215 { .compatible = "fsl,imx31-owire", .data = 1 },
216 { .compatible = "fsl,imx51-owire", .data = 1 },
217
218 { .compatible = "fsl,imx25-owire", .data = 2 },
219 { .compatible = "fsl,imx35-owire", .data = 2 },
220 { .compatible = "fsl,imx50-owire", .data = 2 },
221 { .compatible = "fsl,imx53-owire", .data = 2 },
222 { },
223};
224
225U_BOOT_DRIVER(mxc_w1_drv) = {
226 .id = UCLASS_W1,
227 .name = "mxc_w1_drv",
228 .of_match = mxc_w1_id,
229 .ofdata_to_platdata = mxc_w1_ofdata_to_platdata,
230 .ops = &mxc_w1_ops,
231 .platdata_auto_alloc_size = sizeof(struct mxc_w1_pdata),
232 .probe = mxc_w1_probe,
233};