icee.c revision 323931
1167857Simp/*-
2167857Simp * Copyright (c) 2006 Warner Losh.  All rights reserved.
3167857Simp *
4167857Simp * Redistribution and use in source and binary forms, with or without
5167857Simp * modification, are permitted provided that the following conditions
6167857Simp * are met:
7167857Simp * 1. Redistributions of source code must retain the above copyright
8167857Simp *    notice, this list of conditions and the following disclaimer.
9167857Simp * 2. Redistributions in binary form must reproduce the above copyright
10167857Simp *    notice, this list of conditions and the following disclaimer in the
11167857Simp *    documentation and/or other materials provided with the distribution.
12167857Simp *
13167857Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14167857Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15167857Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16167857Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17167857Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18167857Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19167857Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20167857Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21167857Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22167857Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23167857Simp */
24167857Simp
25167857Simp#include <sys/cdefs.h>
26167857Simp__FBSDID("$FreeBSD: stable/11/sys/dev/iicbus/icee.c 323931 2017-09-22 15:53:22Z ian $");
27167857Simp/*
28167857Simp * Generic IIC eeprom support, modeled after the AT24C family of products.
29167857Simp */
30289727Sian
31289727Sian#include "opt_platform.h"
32289727Sian
33167857Simp#include <sys/param.h>
34167857Simp#include <sys/systm.h>
35167857Simp#include <sys/bus.h>
36167857Simp#include <sys/conf.h>
37167857Simp#include <sys/kernel.h>
38167857Simp#include <sys/module.h>
39167857Simp#include <sys/resource.h>
40181305Sjhb#include <sys/sx.h>
41167857Simp#include <sys/uio.h>
42167857Simp#include <machine/bus.h>
43289727Sian
44289727Sian#ifdef FDT
45289727Sian#include <dev/ofw/ofw_bus.h>
46289727Sian#include <dev/ofw/ofw_bus_subr.h>
47289727Sian#endif
48289727Sian
49167857Simp#include <dev/iicbus/iiconf.h>
50167857Simp#include <dev/iicbus/iicbus.h>
51167857Simp
52167857Simp#include "iicbus_if.h"
53167857Simp
54289727Sian/*
55289727Sian * AT24 parts have a "write page size" that differs per-device, and a "read page
56289727Sian * size" that is always equal to the full device size.  We define maximum values
57289727Sian * here to limit how long we occupy the bus with a single transfer, and because
58289727Sian * there are temporary buffers of these sizes allocated on the stack.
59289727Sian */
60167857Simp#define	MAX_RD_SZ	256	/* Largest read size we support */
61289727Sian#define	MAX_WR_SZ	256	/* Largest write size we support */
62167857Simp
63167857Simpstruct icee_softc {
64289727Sian	device_t	dev;		/* Myself */
65167857Simp	struct cdev	*cdev;		/* user interface */
66289727Sian	int		addr;		/* Slave address on the bus */
67167857Simp	int		size;		/* How big am I? */
68289727Sian	int		type;		/* What address type 8 or 16 bit? */
69167857Simp	int		wr_sz;		/* What's the write page size */
70167857Simp};
71167857Simp
72289727Sian#ifdef FDT
73289727Sianstruct eeprom_desc {
74289727Sian	int	    type;
75289727Sian	int	    size;
76289727Sian	int	    wr_sz;
77289727Sian	const char *name;
78289727Sian};
79289727Sian
80289727Sianstatic struct eeprom_desc type_desc[] = {
81289727Sian	{ 8,        128,   8, "AT24C01"},
82289727Sian	{ 8,        256,   8, "AT24C02"},
83289727Sian	{ 8,        512,  16, "AT24C04"},
84289727Sian	{ 8,       1024,  16, "AT24C08"},
85289727Sian	{ 8,   2 * 1024,  16, "AT24C16"},
86289727Sian	{16,   4 * 1024,  32, "AT24C32"},
87289727Sian	{16,   8 * 1024,  32, "AT24C64"},
88289727Sian	{16,  16 * 1024,  64, "AT24C128"},
89289727Sian	{16,  32 * 1024,  64, "AT24C256"},
90289727Sian	{16,  64 * 1024, 128, "AT24C512"},
91289727Sian	{16, 128 * 1024, 256, "AT24CM01"},
92289727Sian};
93289727Sian
94289727Sianstatic struct ofw_compat_data compat_data[] = {
95289727Sian	{"atmel,24c01",	  (uintptr_t)(&type_desc[0])},
96289727Sian	{"atmel,24c02",	  (uintptr_t)(&type_desc[1])},
97289727Sian	{"atmel,24c04",	  (uintptr_t)(&type_desc[2])},
98289727Sian	{"atmel,24c08",	  (uintptr_t)(&type_desc[3])},
99289727Sian	{"atmel,24c16",	  (uintptr_t)(&type_desc[4])},
100289727Sian	{"atmel,24c32",	  (uintptr_t)(&type_desc[5])},
101289727Sian	{"atmel,24c64",	  (uintptr_t)(&type_desc[6])},
102289727Sian	{"atmel,24c128",  (uintptr_t)(&type_desc[7])},
103289727Sian	{"atmel,24c256",  (uintptr_t)(&type_desc[8])},
104289727Sian	{"atmel,24c512",  (uintptr_t)(&type_desc[9])},
105289727Sian	{"atmel,24c1024", (uintptr_t)(&type_desc[10])},
106289727Sian	{NULL,		  (uintptr_t)NULL},
107289727Sian};
108289727Sian#endif
109289727Sian
110167857Simp#define CDEV2SOFTC(dev)		((dev)->si_drv1)
111167857Simp
112167857Simp/* cdev routines */
113167857Simpstatic d_open_t icee_open;
114167857Simpstatic d_close_t icee_close;
115167857Simpstatic d_read_t icee_read;
116167857Simpstatic d_write_t icee_write;
117167857Simp
118167857Simpstatic struct cdevsw icee_cdevsw =
119167857Simp{
120167857Simp	.d_version = D_VERSION,
121181305Sjhb	.d_flags = D_TRACKCLOSE,
122167857Simp	.d_open = icee_open,
123167857Simp	.d_close = icee_close,
124167857Simp	.d_read = icee_read,
125167857Simp	.d_write = icee_write
126167857Simp};
127167857Simp
128289727Sian#ifdef FDT
129167857Simpstatic int
130167857Simpicee_probe(device_t dev)
131167857Simp{
132289727Sian	struct eeprom_desc *d;
133289083Sian
134289727Sian	if (!ofw_bus_status_okay(dev))
135289727Sian		return (ENXIO);
136289727Sian
137289727Sian	d = (struct eeprom_desc *)
138289727Sian	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
139289727Sian	if (d == NULL)
140289727Sian		return (ENXIO);
141289727Sian
142289727Sian	device_set_desc(dev, d->name);
143289727Sian	return (BUS_PROBE_DEFAULT);
144289727Sian}
145289727Sian
146289727Sianstatic void
147289727Sianicee_init(struct icee_softc *sc)
148289727Sian{
149289727Sian	struct eeprom_desc *d;
150289727Sian
151289727Sian	d = (struct eeprom_desc *)
152289727Sian	    ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
153289727Sian	if (d == NULL)
154289727Sian		return; /* attach will see sc->size == 0 and return error */
155289727Sian
156289727Sian	sc->size  = d->size;
157289727Sian	sc->type  = d->type;
158289727Sian	sc->wr_sz = d->wr_sz;
159289727Sian}
160289727Sian#else /* !FDT */
161289727Sianstatic int
162289727Sianicee_probe(device_t dev)
163289727Sian{
164289727Sian
165167857Simp	device_set_desc(dev, "I2C EEPROM");
166186833Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
167167857Simp}
168167857Simp
169289727Sianstatic void
170289727Sianicee_init(struct icee_softc *sc)
171289727Sian{
172289727Sian	const char *dname;
173289727Sian	int dunit;
174289727Sian
175289727Sian	dname = device_get_name(sc->dev);
176289727Sian	dunit = device_get_unit(sc->dev);
177289727Sian	resource_int_value(dname, dunit, "size", &sc->size);
178289727Sian	resource_int_value(dname, dunit, "type", &sc->type);
179289727Sian	resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
180289727Sian}
181289727Sian#endif /* FDT */
182289727Sian
183167857Simpstatic int
184167857Simpicee_attach(device_t dev)
185167857Simp{
186167857Simp	struct icee_softc *sc = device_get_softc(dev);
187167857Simp
188289727Sian	sc->dev = dev;
189181325Sstas	sc->addr = iicbus_get_addr(dev);
190289727Sian	icee_init(sc);
191289727Sian	if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
192289727Sian		device_printf(sc->dev, "Missing config data, "
193289727Sian		    "these cannot be zero: size %d type %d wr_sz %d\n",
194289727Sian		    sc->size, sc->type, sc->wr_sz);
195289727Sian		return (EINVAL);
196289727Sian	}
197167857Simp	if (bootverbose)
198289727Sian		device_printf(dev, "size: %d bytes, addressing: %d-bits\n",
199167857Simp		    sc->size, sc->type);
200167857Simp	sc->cdev = make_dev(&icee_cdevsw, device_get_unit(dev), UID_ROOT,
201167857Simp	    GID_WHEEL, 0600, "icee%d", device_get_unit(dev));
202167857Simp	if (sc->cdev == NULL) {
203289727Sian		return (ENOMEM);
204167857Simp	}
205167857Simp	sc->cdev->si_drv1 = sc;
206289727Sian	return (0);
207167857Simp}
208167857Simp
209323931Sianstatic int
210323931Sianicee_detach(device_t dev)
211323931Sian{
212323931Sian	struct icee_softc *sc = device_get_softc(dev);
213323931Sian
214323931Sian	destroy_dev(sc->cdev);
215323931Sian	return (0);
216323931Sian}
217323931Sian
218167857Simpstatic int
219167857Simpicee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
220167857Simp{
221323931Sian	struct icee_softc *sc;
222167857Simp
223323931Sian	sc = CDEV2SOFTC(dev);
224323931Sian	if (device_get_state(sc->dev) < DS_BUSY)
225323931Sian		device_busy(sc->dev);
226323931Sian
227289083Sian	return (0);
228167857Simp}
229167857Simp
230167857Simpstatic int
231167857Simpicee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
232167857Simp{
233323931Sian	struct icee_softc *sc;
234167857Simp
235323931Sian	sc = CDEV2SOFTC(dev);
236323931Sian	device_unbusy(sc->dev);
237167857Simp	return (0);
238167857Simp}
239167857Simp
240167857Simpstatic int
241167857Simpicee_read(struct cdev *dev, struct uio *uio, int ioflag)
242167857Simp{
243167857Simp	struct icee_softc *sc;
244167857Simp	uint8_t addr[2];
245167857Simp	uint8_t data[MAX_RD_SZ];
246167857Simp	int error, i, len, slave;
247167857Simp	struct iic_msg msgs[2] = {
248167857Simp	     { 0, IIC_M_WR, 1, addr },
249167857Simp	     { 0, IIC_M_RD, 0, data },
250167857Simp	};
251167857Simp
252167857Simp	sc = CDEV2SOFTC(dev);
253167857Simp	if (uio->uio_offset == sc->size)
254167857Simp		return (0);
255167857Simp	if (uio->uio_offset > sc->size)
256167857Simp		return (EIO);
257167857Simp	if (sc->type != 8 && sc->type != 16)
258167857Simp		return (EINVAL);
259167857Simp	slave = error = 0;
260167857Simp	while (uio->uio_resid > 0) {
261167857Simp		if (uio->uio_offset >= sc->size)
262167857Simp			break;
263289727Sian		len = MIN(MAX_RD_SZ - (uio->uio_offset & (MAX_RD_SZ - 1)),
264167857Simp		    uio->uio_resid);
265167857Simp		switch (sc->type) {
266167857Simp		case 8:
267167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
268167857Simp			msgs[0].len = 1;
269167857Simp			msgs[1].len = len;
270167857Simp			addr[0] = uio->uio_offset & 0xff;
271167857Simp			break;
272167857Simp		case 16:
273167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
274167857Simp			msgs[0].len = 2;
275167857Simp			msgs[1].len = len;
276167857Simp			addr[0] = (uio->uio_offset >> 8) & 0xff;
277167857Simp			addr[1] = uio->uio_offset & 0xff;
278167857Simp			break;
279167857Simp		}
280167857Simp		for (i = 0; i < 2; i++)
281167857Simp			msgs[i].slave = slave;
282289727Sian		error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
283289105Sian		if (error) {
284289105Sian			error = iic2errno(error);
285167857Simp			break;
286289105Sian		}
287167857Simp		error = uiomove(data, len, uio);
288167857Simp		if (error)
289167857Simp			break;
290167857Simp	}
291167857Simp	return (error);
292167857Simp}
293167857Simp
294167857Simp/*
295167857Simp * Write to the part.  We use three transfers here since we're actually
296167857Simp * doing a write followed by a read to make sure that the write finished.
297167857Simp * It is easier to encode the dummy read here than to break things up
298167857Simp * into smaller chunks...
299167857Simp */
300167857Simpstatic int
301167857Simpicee_write(struct cdev *dev, struct uio *uio, int ioflag)
302167857Simp{
303167857Simp	struct icee_softc *sc;
304167857Simp	int error, len, slave, waitlimit;
305167857Simp	uint8_t data[MAX_WR_SZ + 2];
306167857Simp	struct iic_msg wr[1] = {
307167857Simp	     { 0, IIC_M_WR, 0, data },
308167857Simp	};
309167857Simp	struct iic_msg rd[1] = {
310167857Simp	     { 0, IIC_M_RD, 1, data },
311167857Simp	};
312167857Simp
313167857Simp	sc = CDEV2SOFTC(dev);
314167857Simp	if (uio->uio_offset >= sc->size)
315167857Simp		return (EIO);
316167857Simp	if (sc->type != 8 && sc->type != 16)
317167857Simp		return (EINVAL);
318289118Sian
319167857Simp	slave = error = 0;
320167857Simp	while (uio->uio_resid > 0) {
321167857Simp		if (uio->uio_offset >= sc->size)
322167857Simp			break;
323167857Simp		len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
324167857Simp		    uio->uio_resid);
325167857Simp		switch (sc->type) {
326167857Simp		case 8:
327167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
328167857Simp			wr[0].len = 1 + len;
329167857Simp			data[0] = uio->uio_offset & 0xff;
330167857Simp			break;
331167857Simp		case 16:
332167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
333167857Simp			wr[0].len = 2 + len;
334167857Simp			data[0] = (uio->uio_offset >> 8) & 0xff;
335167857Simp			data[1] = uio->uio_offset & 0xff;
336167857Simp			break;
337167857Simp		}
338167857Simp		wr[0].slave = slave;
339167857Simp		error = uiomove(data + sc->type / 8, len, uio);
340167857Simp		if (error)
341167857Simp			break;
342289727Sian		error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT);
343289105Sian		if (error) {
344289105Sian			error = iic2errno(error);
345167857Simp			break;
346289105Sian		}
347289083Sian		/* Read after write to wait for write-done. */
348167857Simp		waitlimit = 10000;
349167857Simp		rd[0].slave = slave;
350289083Sian		do {
351289727Sian			error = iicbus_transfer_excl(sc->dev, rd, 1,
352289727Sian			    IIC_INTRWAIT);
353167857Simp		} while (waitlimit-- > 0 && error != 0);
354289105Sian		if (error) {
355289105Sian			error = iic2errno(error);
356289083Sian			break;
357289105Sian		}
358167857Simp	}
359167857Simp	return error;
360167857Simp}
361167857Simp
362167857Simpstatic device_method_t icee_methods[] = {
363167857Simp	DEVMETHOD(device_probe,		icee_probe),
364167857Simp	DEVMETHOD(device_attach,	icee_attach),
365323931Sian	DEVMETHOD(device_detach,	icee_detach),
366167857Simp
367246128Ssbz	DEVMETHOD_END
368167857Simp};
369167857Simp
370167857Simpstatic driver_t icee_driver = {
371167857Simp	"icee",
372167857Simp	icee_methods,
373167857Simp	sizeof(struct icee_softc),
374167857Simp};
375167857Simpstatic devclass_t icee_devclass;
376167857Simp
377167857SimpDRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
378167857SimpMODULE_VERSION(icee, 1);
379167857SimpMODULE_DEPEND(icee, iicbus, 1, 1, 1);
380