2 * Copyright (c) 2006 Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 * Generic IIC eeprom support, modeled after the AT24C family of products.
31 #include "opt_platform.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/resource.h>
42 #include <machine/bus.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
52 #include "iicbus_if.h"
55 * AT24 parts have a "write page size" that differs per-device, and a "read page
56 * size" that is always equal to the full device size. We define maximum values
57 * here to limit how long we occupy the bus with a single transfer, and because
58 * there are temporary buffers of these sizes allocated on the stack.
60 #define MAX_RD_SZ 256 /* Largest read size we support */
61 #define MAX_WR_SZ 256 /* Largest write size we support */
64 device_t dev; /* Myself */
65 struct cdev *cdev; /* user interface */
66 int addr; /* Slave address on the bus */
67 int size; /* How big am I? */
68 int type; /* What address type 8 or 16 bit? */
69 int wr_sz; /* What's the write page size */
80 static struct eeprom_desc type_desc[] = {
81 { 8, 128, 8, "AT24C01"},
82 { 8, 256, 8, "AT24C02"},
83 { 8, 512, 16, "AT24C04"},
84 { 8, 1024, 16, "AT24C08"},
85 { 8, 2 * 1024, 16, "AT24C16"},
86 {16, 4 * 1024, 32, "AT24C32"},
87 {16, 8 * 1024, 32, "AT24C64"},
88 {16, 16 * 1024, 64, "AT24C128"},
89 {16, 32 * 1024, 64, "AT24C256"},
90 {16, 64 * 1024, 128, "AT24C512"},
91 {16, 128 * 1024, 256, "AT24CM01"},
94 static struct ofw_compat_data compat_data[] = {
95 {"atmel,24c01", (uintptr_t)(&type_desc[0])},
96 {"atmel,24c02", (uintptr_t)(&type_desc[1])},
97 {"atmel,24c04", (uintptr_t)(&type_desc[2])},
98 {"atmel,24c08", (uintptr_t)(&type_desc[3])},
99 {"atmel,24c16", (uintptr_t)(&type_desc[4])},
100 {"atmel,24c32", (uintptr_t)(&type_desc[5])},
101 {"atmel,24c64", (uintptr_t)(&type_desc[6])},
102 {"atmel,24c128", (uintptr_t)(&type_desc[7])},
103 {"atmel,24c256", (uintptr_t)(&type_desc[8])},
104 {"atmel,24c512", (uintptr_t)(&type_desc[9])},
105 {"atmel,24c1024", (uintptr_t)(&type_desc[10])},
106 {NULL, (uintptr_t)NULL},
110 #define CDEV2SOFTC(dev) ((dev)->si_drv1)
113 static d_open_t icee_open;
114 static d_close_t icee_close;
115 static d_read_t icee_read;
116 static d_write_t icee_write;
118 static struct cdevsw icee_cdevsw =
120 .d_version = D_VERSION,
121 .d_flags = D_TRACKCLOSE,
123 .d_close = icee_close,
125 .d_write = icee_write
130 icee_probe(device_t dev)
132 struct eeprom_desc *d;
134 if (!ofw_bus_status_okay(dev))
137 d = (struct eeprom_desc *)
138 ofw_bus_search_compatible(dev, compat_data)->ocd_data;
142 device_set_desc(dev, d->name);
143 return (BUS_PROBE_DEFAULT);
147 icee_init(struct icee_softc *sc)
149 struct eeprom_desc *d;
151 d = (struct eeprom_desc *)
152 ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
154 return; /* attach will see sc->size == 0 and return error */
158 sc->wr_sz = d->wr_sz;
162 icee_probe(device_t dev)
165 device_set_desc(dev, "I2C EEPROM");
166 return (BUS_PROBE_NOWILDCARD);
170 icee_init(struct icee_softc *sc)
175 dname = device_get_name(sc->dev);
176 dunit = device_get_unit(sc->dev);
177 resource_int_value(dname, dunit, "size", &sc->size);
178 resource_int_value(dname, dunit, "type", &sc->type);
179 resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
184 icee_attach(device_t dev)
186 struct icee_softc *sc = device_get_softc(dev);
189 sc->addr = iicbus_get_addr(dev);
191 if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
192 device_printf(sc->dev, "Missing config data, "
193 "these cannot be zero: size %d type %d wr_sz %d\n",
194 sc->size, sc->type, sc->wr_sz);
198 device_printf(dev, "size: %d bytes, addressing: %d-bits\n",
200 sc->cdev = make_dev(&icee_cdevsw, device_get_unit(dev), UID_ROOT,
201 GID_WHEEL, 0600, "icee%d", device_get_unit(dev));
202 if (sc->cdev == NULL) {
205 sc->cdev->si_drv1 = sc;
210 icee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
217 icee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
224 icee_read(struct cdev *dev, struct uio *uio, int ioflag)
226 struct icee_softc *sc;
228 uint8_t data[MAX_RD_SZ];
229 int error, i, len, slave;
230 struct iic_msg msgs[2] = {
231 { 0, IIC_M_WR, 1, addr },
232 { 0, IIC_M_RD, 0, data },
235 sc = CDEV2SOFTC(dev);
236 if (uio->uio_offset == sc->size)
238 if (uio->uio_offset > sc->size)
240 if (sc->type != 8 && sc->type != 16)
243 while (uio->uio_resid > 0) {
244 if (uio->uio_offset >= sc->size)
246 len = MIN(MAX_RD_SZ - (uio->uio_offset & (MAX_RD_SZ - 1)),
250 slave = (uio->uio_offset >> 7) | sc->addr;
253 addr[0] = uio->uio_offset & 0xff;
256 slave = sc->addr | (uio->uio_offset >> 15);
259 addr[0] = (uio->uio_offset >> 8) & 0xff;
260 addr[1] = uio->uio_offset & 0xff;
263 for (i = 0; i < 2; i++)
264 msgs[i].slave = slave;
265 error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
267 error = iic2errno(error);
270 error = uiomove(data, len, uio);
278 * Write to the part. We use three transfers here since we're actually
279 * doing a write followed by a read to make sure that the write finished.
280 * It is easier to encode the dummy read here than to break things up
281 * into smaller chunks...
284 icee_write(struct cdev *dev, struct uio *uio, int ioflag)
286 struct icee_softc *sc;
287 int error, len, slave, waitlimit;
288 uint8_t data[MAX_WR_SZ + 2];
289 struct iic_msg wr[1] = {
290 { 0, IIC_M_WR, 0, data },
292 struct iic_msg rd[1] = {
293 { 0, IIC_M_RD, 1, data },
296 sc = CDEV2SOFTC(dev);
297 if (uio->uio_offset >= sc->size)
299 if (sc->type != 8 && sc->type != 16)
303 while (uio->uio_resid > 0) {
304 if (uio->uio_offset >= sc->size)
306 len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
310 slave = (uio->uio_offset >> 7) | sc->addr;
312 data[0] = uio->uio_offset & 0xff;
315 slave = sc->addr | (uio->uio_offset >> 15);
317 data[0] = (uio->uio_offset >> 8) & 0xff;
318 data[1] = uio->uio_offset & 0xff;
322 error = uiomove(data + sc->type / 8, len, uio);
325 error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT);
327 error = iic2errno(error);
330 /* Read after write to wait for write-done. */
334 error = iicbus_transfer_excl(sc->dev, rd, 1,
336 } while (waitlimit-- > 0 && error != 0);
338 error = iic2errno(error);
345 static device_method_t icee_methods[] = {
346 DEVMETHOD(device_probe, icee_probe),
347 DEVMETHOD(device_attach, icee_attach),
352 static driver_t icee_driver = {
355 sizeof(struct icee_softc),
357 static devclass_t icee_devclass;
359 DRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
360 MODULE_VERSION(icee, 1);
361 MODULE_DEPEND(icee, iicbus, 1, 1, 1);