]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/icee.c
sys/dev: further adoption of SPDX licensing ID tags.
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / icee.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Warner Losh.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 /*
30  * Generic IIC eeprom support, modeled after the AT24C family of products.
31  */
32
33 #include "opt_platform.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/resource.h>
42 #include <sys/sx.h>
43 #include <sys/uio.h>
44 #include <machine/bus.h>
45
46 #ifdef FDT
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #endif
50
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/iicbus/iicbus.h>
53
54 #include "iicbus_if.h"
55
56 /*
57  * AT24 parts have a "write page size" that differs per-device, and a "read page
58  * size" that is always equal to the full device size.  We define maximum values
59  * here to limit how long we occupy the bus with a single transfer, and because
60  * there are temporary buffers of these sizes allocated on the stack.
61  */
62 #define MAX_RD_SZ       256     /* Largest read size we support */
63 #define MAX_WR_SZ       256     /* Largest write size we support */
64
65 struct icee_softc {
66         device_t        dev;            /* Myself */
67         struct cdev     *cdev;          /* user interface */
68         int             addr;           /* Slave address on the bus */
69         int             size;           /* How big am I? */
70         int             type;           /* What address type 8 or 16 bit? */
71         int             wr_sz;          /* What's the write page size */
72 };
73
74 #ifdef FDT
75 struct eeprom_desc {
76         int         type;
77         int         size;
78         int         wr_sz;
79         const char *name;
80 };
81
82 static struct eeprom_desc type_desc[] = {
83         { 8,        128,   8, "AT24C01"},
84         { 8,        256,   8, "AT24C02"},
85         { 8,        512,  16, "AT24C04"},
86         { 8,       1024,  16, "AT24C08"},
87         { 8,   2 * 1024,  16, "AT24C16"},
88         {16,   4 * 1024,  32, "AT24C32"},
89         {16,   8 * 1024,  32, "AT24C64"},
90         {16,  16 * 1024,  64, "AT24C128"},
91         {16,  32 * 1024,  64, "AT24C256"},
92         {16,  64 * 1024, 128, "AT24C512"},
93         {16, 128 * 1024, 256, "AT24CM01"},
94 };
95
96 static struct ofw_compat_data compat_data[] = {
97         {"atmel,24c01",   (uintptr_t)(&type_desc[0])},
98         {"atmel,24c02",   (uintptr_t)(&type_desc[1])},
99         {"atmel,24c04",   (uintptr_t)(&type_desc[2])},
100         {"atmel,24c08",   (uintptr_t)(&type_desc[3])},
101         {"atmel,24c16",   (uintptr_t)(&type_desc[4])},
102         {"atmel,24c32",   (uintptr_t)(&type_desc[5])},
103         {"atmel,24c64",   (uintptr_t)(&type_desc[6])},
104         {"atmel,24c128",  (uintptr_t)(&type_desc[7])},
105         {"atmel,24c256",  (uintptr_t)(&type_desc[8])},
106         {"atmel,24c512",  (uintptr_t)(&type_desc[9])},
107         {"atmel,24c1024", (uintptr_t)(&type_desc[10])},
108         {NULL,            (uintptr_t)NULL},
109 };
110 #endif
111
112 #define CDEV2SOFTC(dev)         ((dev)->si_drv1)
113
114 /* cdev routines */
115 static d_open_t icee_open;
116 static d_close_t icee_close;
117 static d_read_t icee_read;
118 static d_write_t icee_write;
119
120 static struct cdevsw icee_cdevsw =
121 {
122         .d_version = D_VERSION,
123         .d_flags = D_TRACKCLOSE,
124         .d_open = icee_open,
125         .d_close = icee_close,
126         .d_read = icee_read,
127         .d_write = icee_write
128 };
129
130 #ifdef FDT
131 static int
132 icee_probe(device_t dev)
133 {
134         struct eeprom_desc *d;
135
136         if (!ofw_bus_status_okay(dev))
137                 return (ENXIO);
138
139         d = (struct eeprom_desc *)
140             ofw_bus_search_compatible(dev, compat_data)->ocd_data;
141         if (d == NULL)
142                 return (ENXIO);
143
144         device_set_desc(dev, d->name);
145         return (BUS_PROBE_DEFAULT);
146 }
147
148 static void
149 icee_init(struct icee_softc *sc)
150 {
151         struct eeprom_desc *d;
152
153         d = (struct eeprom_desc *)
154             ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
155         if (d == NULL)
156                 return; /* attach will see sc->size == 0 and return error */
157
158         sc->size  = d->size;
159         sc->type  = d->type;
160         sc->wr_sz = d->wr_sz;
161 }
162 #else /* !FDT */
163 static int
164 icee_probe(device_t dev)
165 {
166
167         device_set_desc(dev, "I2C EEPROM");
168         return (BUS_PROBE_NOWILDCARD);
169 }
170
171 static void
172 icee_init(struct icee_softc *sc)
173 {
174         const char *dname;
175         int dunit;
176
177         dname = device_get_name(sc->dev);
178         dunit = device_get_unit(sc->dev);
179         resource_int_value(dname, dunit, "size", &sc->size);
180         resource_int_value(dname, dunit, "type", &sc->type);
181         resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
182 }
183 #endif /* FDT */
184
185 static int
186 icee_attach(device_t dev)
187 {
188         struct icee_softc *sc = device_get_softc(dev);
189
190         sc->dev = dev;
191         sc->addr = iicbus_get_addr(dev);
192         icee_init(sc);
193         if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
194                 device_printf(sc->dev, "Missing config data, "
195                     "these cannot be zero: size %d type %d wr_sz %d\n",
196                     sc->size, sc->type, sc->wr_sz);
197                 return (EINVAL);
198         }
199         if (bootverbose)
200                 device_printf(dev, "size: %d bytes, addressing: %d-bits\n",
201                     sc->size, sc->type);
202         sc->cdev = make_dev(&icee_cdevsw, device_get_unit(dev), UID_ROOT,
203             GID_WHEEL, 0600, "icee%d", device_get_unit(dev));
204         if (sc->cdev == NULL) {
205                 return (ENOMEM);
206         }
207         sc->cdev->si_drv1 = sc;
208         return (0);
209 }
210
211 static int
212 icee_detach(device_t dev)
213 {
214         struct icee_softc *sc = device_get_softc(dev);
215
216         destroy_dev(sc->cdev);
217         return (0);
218 }
219
220 static int 
221 icee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
222 {
223         struct icee_softc *sc;
224
225         sc = CDEV2SOFTC(dev);
226         if (device_get_state(sc->dev) < DS_BUSY)
227                 device_busy(sc->dev);
228
229         return (0);
230 }
231
232 static int
233 icee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
234 {
235         struct icee_softc *sc;
236
237         sc = CDEV2SOFTC(dev);
238         device_unbusy(sc->dev);
239         return (0);
240 }
241
242 static int
243 icee_read(struct cdev *dev, struct uio *uio, int ioflag)
244 {
245         struct icee_softc *sc;
246         uint8_t addr[2];
247         uint8_t data[MAX_RD_SZ];
248         int error, i, len, slave;
249         struct iic_msg msgs[2] = {
250              { 0, IIC_M_WR, 1, addr },
251              { 0, IIC_M_RD, 0, data },
252         };
253
254         sc = CDEV2SOFTC(dev);
255         if (uio->uio_offset == sc->size)
256                 return (0);
257         if (uio->uio_offset > sc->size)
258                 return (EIO);
259         if (sc->type != 8 && sc->type != 16)
260                 return (EINVAL);
261         slave = error = 0;
262         while (uio->uio_resid > 0) {
263                 if (uio->uio_offset >= sc->size)
264                         break;
265                 len = MIN(MAX_RD_SZ - (uio->uio_offset & (MAX_RD_SZ - 1)),
266                     uio->uio_resid);
267                 switch (sc->type) {
268                 case 8:
269                         slave = (uio->uio_offset >> 7) | sc->addr;
270                         msgs[0].len = 1;
271                         msgs[1].len = len;
272                         addr[0] = uio->uio_offset & 0xff;
273                         break;
274                 case 16:
275                         slave = sc->addr | (uio->uio_offset >> 15);
276                         msgs[0].len = 2;
277                         msgs[1].len = len;
278                         addr[0] = (uio->uio_offset >> 8) & 0xff;
279                         addr[1] = uio->uio_offset & 0xff;
280                         break;
281                 }
282                 for (i = 0; i < 2; i++)
283                         msgs[i].slave = slave;
284                 error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
285                 if (error) {
286                         error = iic2errno(error);
287                         break;
288                 }
289                 error = uiomove(data, len, uio);
290                 if (error)
291                         break;
292         }
293         return (error);
294 }
295
296 /*
297  * Write to the part.  We use three transfers here since we're actually
298  * doing a write followed by a read to make sure that the write finished.
299  * It is easier to encode the dummy read here than to break things up
300  * into smaller chunks...
301  */
302 static int
303 icee_write(struct cdev *dev, struct uio *uio, int ioflag)
304 {
305         struct icee_softc *sc;
306         int error, len, slave, waitlimit;
307         uint8_t data[MAX_WR_SZ + 2];
308         struct iic_msg wr[1] = {
309              { 0, IIC_M_WR, 0, data },
310         };
311         struct iic_msg rd[1] = {
312              { 0, IIC_M_RD, 1, data },
313         };
314
315         sc = CDEV2SOFTC(dev);
316         if (uio->uio_offset >= sc->size)
317                 return (EIO);
318         if (sc->type != 8 && sc->type != 16)
319                 return (EINVAL);
320
321         slave = error = 0;
322         while (uio->uio_resid > 0) {
323                 if (uio->uio_offset >= sc->size)
324                         break;
325                 len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
326                     uio->uio_resid);
327                 switch (sc->type) {
328                 case 8:
329                         slave = (uio->uio_offset >> 7) | sc->addr;
330                         wr[0].len = 1 + len;
331                         data[0] = uio->uio_offset & 0xff;
332                         break;
333                 case 16:
334                         slave = sc->addr | (uio->uio_offset >> 15);
335                         wr[0].len = 2 + len;
336                         data[0] = (uio->uio_offset >> 8) & 0xff;
337                         data[1] = uio->uio_offset & 0xff;
338                         break;
339                 }
340                 wr[0].slave = slave;
341                 error = uiomove(data + sc->type / 8, len, uio);
342                 if (error)
343                         break;
344                 error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT);
345                 if (error) {
346                         error = iic2errno(error);
347                         break;
348                 }
349                 /* Read after write to wait for write-done. */
350                 waitlimit = 10000;
351                 rd[0].slave = slave;
352                 do {
353                         error = iicbus_transfer_excl(sc->dev, rd, 1,
354                             IIC_INTRWAIT);
355                 } while (waitlimit-- > 0 && error != 0);
356                 if (error) {
357                         error = iic2errno(error);
358                         break;
359                 }
360         }
361         return error;
362 }
363
364 static device_method_t icee_methods[] = {
365         DEVMETHOD(device_probe,         icee_probe),
366         DEVMETHOD(device_attach,        icee_attach),
367         DEVMETHOD(device_detach,        icee_detach),
368
369         DEVMETHOD_END
370 };
371
372 static driver_t icee_driver = {
373         "icee",
374         icee_methods,
375         sizeof(struct icee_softc),
376 };
377 static devclass_t icee_devclass;
378
379 DRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
380 MODULE_VERSION(icee, 1);
381 MODULE_DEPEND(icee, iicbus, 1, 1, 1);