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: releng/10.3/sys/dev/iicbus/icee.c 294674 2016-01-24 18:54:55Z ian $");
27167857Simp/*
28167857Simp * Generic IIC eeprom support, modeled after the AT24C family of products.
29167857Simp */
30294674Sian
31294674Sian#include "opt_platform.h"
32294674Sian
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>
43294674Sian
44294674Sian#ifdef FDT
45294674Sian#include <dev/ofw/ofw_bus.h>
46294674Sian#include <dev/ofw/ofw_bus_subr.h>
47294674Sian#endif
48294674Sian
49167857Simp#include <dev/iicbus/iiconf.h>
50167857Simp#include <dev/iicbus/iicbus.h>
51167857Simp
52167857Simp#include "iicbus_if.h"
53167857Simp
54294674Sian/*
55294674Sian * AT24 parts have a "write page size" that differs per-device, and a "read page
56294674Sian * size" that is always equal to the full device size.  We define maximum values
57294674Sian * here to limit how long we occupy the bus with a single transfer, and because
58294674Sian * there are temporary buffers of these sizes allocated on the stack.
59294674Sian */
60167857Simp#define	MAX_RD_SZ	256	/* Largest read size we support */
61294674Sian#define	MAX_WR_SZ	256	/* Largest write size we support */
62167857Simp
63167857Simpstruct icee_softc {
64294674Sian	device_t	dev;		/* Myself */
65167857Simp	struct cdev	*cdev;		/* user interface */
66294674Sian	int		addr;		/* Slave address on the bus */
67167857Simp	int		size;		/* How big am I? */
68294674Sian	int		type;		/* What address type 8 or 16 bit? */
69167857Simp	int		wr_sz;		/* What's the write page size */
70167857Simp};
71167857Simp
72294674Sian#ifdef FDT
73294674Sianstruct eeprom_desc {
74294674Sian	int	    type;
75294674Sian	int	    size;
76294674Sian	int	    wr_sz;
77294674Sian	const char *name;
78294674Sian};
79294674Sian
80294674Sianstatic struct eeprom_desc type_desc[] = {
81294674Sian	{ 8,        128,   8, "AT24C01"},
82294674Sian	{ 8,        256,   8, "AT24C02"},
83294674Sian	{ 8,        512,  16, "AT24C04"},
84294674Sian	{ 8,       1024,  16, "AT24C08"},
85294674Sian	{ 8,   2 * 1024,  16, "AT24C16"},
86294674Sian	{16,   4 * 1024,  32, "AT24C32"},
87294674Sian	{16,   8 * 1024,  32, "AT24C64"},
88294674Sian	{16,  16 * 1024,  64, "AT24C128"},
89294674Sian	{16,  32 * 1024,  64, "AT24C256"},
90294674Sian	{16,  64 * 1024, 128, "AT24C512"},
91294674Sian	{16, 128 * 1024, 256, "AT24CM01"},
92294674Sian};
93294674Sian
94294674Sianstatic struct ofw_compat_data compat_data[] = {
95294674Sian	{"atmel,24c01",	  (uintptr_t)(&type_desc[0])},
96294674Sian	{"atmel,24c02",	  (uintptr_t)(&type_desc[1])},
97294674Sian	{"atmel,24c04",	  (uintptr_t)(&type_desc[2])},
98294674Sian	{"atmel,24c08",	  (uintptr_t)(&type_desc[3])},
99294674Sian	{"atmel,24c16",	  (uintptr_t)(&type_desc[4])},
100294674Sian	{"atmel,24c32",	  (uintptr_t)(&type_desc[5])},
101294674Sian	{"atmel,24c64",	  (uintptr_t)(&type_desc[6])},
102294674Sian	{"atmel,24c128",  (uintptr_t)(&type_desc[7])},
103294674Sian	{"atmel,24c256",  (uintptr_t)(&type_desc[8])},
104294674Sian	{"atmel,24c512",  (uintptr_t)(&type_desc[9])},
105294674Sian	{"atmel,24c1024", (uintptr_t)(&type_desc[10])},
106294674Sian	{NULL,		  (uintptr_t)NULL},
107294674Sian};
108294674Sian#endif
109294674Sian
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
128294674Sian#ifdef FDT
129167857Simpstatic int
130167857Simpicee_probe(device_t dev)
131167857Simp{
132294674Sian	struct eeprom_desc *d;
133289666Sian
134294674Sian	if (!ofw_bus_status_okay(dev))
135294674Sian		return (ENXIO);
136294674Sian
137294674Sian	d = (struct eeprom_desc *)
138294674Sian	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
139294674Sian	if (d == NULL)
140294674Sian		return (ENXIO);
141294674Sian
142294674Sian	device_set_desc(dev, d->name);
143294674Sian	return (BUS_PROBE_DEFAULT);
144294674Sian}
145294674Sian
146294674Sianstatic void
147294674Sianicee_init(struct icee_softc *sc)
148294674Sian{
149294674Sian	struct eeprom_desc *d;
150294674Sian
151294674Sian	d = (struct eeprom_desc *)
152294674Sian	    ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
153294674Sian	if (d == NULL)
154294674Sian		return; /* attach will see sc->size == 0 and return error */
155294674Sian
156294674Sian	sc->size  = d->size;
157294674Sian	sc->type  = d->type;
158294674Sian	sc->wr_sz = d->wr_sz;
159294674Sian}
160294674Sian#else /* !FDT */
161294674Sianstatic int
162294674Sianicee_probe(device_t dev)
163294674Sian{
164294674Sian
165167857Simp	device_set_desc(dev, "I2C EEPROM");
166186833Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
167167857Simp}
168167857Simp
169294674Sianstatic void
170294674Sianicee_init(struct icee_softc *sc)
171294674Sian{
172294674Sian	const char *dname;
173294674Sian	int dunit;
174294674Sian
175294674Sian	dname = device_get_name(sc->dev);
176294674Sian	dunit = device_get_unit(sc->dev);
177294674Sian	resource_int_value(dname, dunit, "size", &sc->size);
178294674Sian	resource_int_value(dname, dunit, "type", &sc->type);
179294674Sian	resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
180294674Sian}
181294674Sian#endif /* FDT */
182294674Sian
183167857Simpstatic int
184167857Simpicee_attach(device_t dev)
185167857Simp{
186167857Simp	struct icee_softc *sc = device_get_softc(dev);
187167857Simp
188294674Sian	sc->dev = dev;
189181325Sstas	sc->addr = iicbus_get_addr(dev);
190294674Sian	icee_init(sc);
191294674Sian	if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
192294674Sian		device_printf(sc->dev, "Missing config data, "
193294674Sian		    "these cannot be zero: size %d type %d wr_sz %d\n",
194294674Sian		    sc->size, sc->type, sc->wr_sz);
195294674Sian		return (EINVAL);
196294674Sian	}
197167857Simp	if (bootverbose)
198294674Sian		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) {
203294674Sian		return (ENOMEM);
204167857Simp	}
205167857Simp	sc->cdev->si_drv1 = sc;
206294674Sian	return (0);
207167857Simp}
208167857Simp
209167857Simpstatic int
210167857Simpicee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
211167857Simp{
212167857Simp
213289666Sian	return (0);
214167857Simp}
215167857Simp
216167857Simpstatic int
217167857Simpicee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
218167857Simp{
219167857Simp
220167857Simp	return (0);
221167857Simp}
222167857Simp
223167857Simpstatic int
224167857Simpicee_read(struct cdev *dev, struct uio *uio, int ioflag)
225167857Simp{
226167857Simp	struct icee_softc *sc;
227167857Simp	uint8_t addr[2];
228167857Simp	uint8_t data[MAX_RD_SZ];
229167857Simp	int error, i, len, slave;
230167857Simp	struct iic_msg msgs[2] = {
231167857Simp	     { 0, IIC_M_WR, 1, addr },
232167857Simp	     { 0, IIC_M_RD, 0, data },
233167857Simp	};
234167857Simp
235167857Simp	sc = CDEV2SOFTC(dev);
236167857Simp	if (uio->uio_offset == sc->size)
237167857Simp		return (0);
238167857Simp	if (uio->uio_offset > sc->size)
239167857Simp		return (EIO);
240167857Simp	if (sc->type != 8 && sc->type != 16)
241167857Simp		return (EINVAL);
242167857Simp	slave = error = 0;
243167857Simp	while (uio->uio_resid > 0) {
244167857Simp		if (uio->uio_offset >= sc->size)
245167857Simp			break;
246294674Sian		len = MIN(MAX_RD_SZ - (uio->uio_offset & (MAX_RD_SZ - 1)),
247167857Simp		    uio->uio_resid);
248167857Simp		switch (sc->type) {
249167857Simp		case 8:
250167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
251167857Simp			msgs[0].len = 1;
252167857Simp			msgs[1].len = len;
253167857Simp			addr[0] = uio->uio_offset & 0xff;
254167857Simp			break;
255167857Simp		case 16:
256167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
257167857Simp			msgs[0].len = 2;
258167857Simp			msgs[1].len = len;
259167857Simp			addr[0] = (uio->uio_offset >> 8) & 0xff;
260167857Simp			addr[1] = uio->uio_offset & 0xff;
261167857Simp			break;
262167857Simp		}
263167857Simp		for (i = 0; i < 2; i++)
264167857Simp			msgs[i].slave = slave;
265294674Sian		error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
266289666Sian		if (error) {
267289666Sian			error = iic2errno(error);
268167857Simp			break;
269289666Sian		}
270167857Simp		error = uiomove(data, len, uio);
271167857Simp		if (error)
272167857Simp			break;
273167857Simp	}
274167857Simp	return (error);
275167857Simp}
276167857Simp
277167857Simp/*
278167857Simp * Write to the part.  We use three transfers here since we're actually
279167857Simp * doing a write followed by a read to make sure that the write finished.
280167857Simp * It is easier to encode the dummy read here than to break things up
281167857Simp * into smaller chunks...
282167857Simp */
283167857Simpstatic int
284167857Simpicee_write(struct cdev *dev, struct uio *uio, int ioflag)
285167857Simp{
286167857Simp	struct icee_softc *sc;
287167857Simp	int error, len, slave, waitlimit;
288167857Simp	uint8_t data[MAX_WR_SZ + 2];
289167857Simp	struct iic_msg wr[1] = {
290167857Simp	     { 0, IIC_M_WR, 0, data },
291167857Simp	};
292167857Simp	struct iic_msg rd[1] = {
293167857Simp	     { 0, IIC_M_RD, 1, data },
294167857Simp	};
295167857Simp
296167857Simp	sc = CDEV2SOFTC(dev);
297167857Simp	if (uio->uio_offset >= sc->size)
298167857Simp		return (EIO);
299167857Simp	if (sc->type != 8 && sc->type != 16)
300167857Simp		return (EINVAL);
301289666Sian
302167857Simp	slave = error = 0;
303167857Simp	while (uio->uio_resid > 0) {
304167857Simp		if (uio->uio_offset >= sc->size)
305167857Simp			break;
306167857Simp		len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
307167857Simp		    uio->uio_resid);
308167857Simp		switch (sc->type) {
309167857Simp		case 8:
310167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
311167857Simp			wr[0].len = 1 + len;
312167857Simp			data[0] = uio->uio_offset & 0xff;
313167857Simp			break;
314167857Simp		case 16:
315167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
316167857Simp			wr[0].len = 2 + len;
317167857Simp			data[0] = (uio->uio_offset >> 8) & 0xff;
318167857Simp			data[1] = uio->uio_offset & 0xff;
319167857Simp			break;
320167857Simp		}
321167857Simp		wr[0].slave = slave;
322167857Simp		error = uiomove(data + sc->type / 8, len, uio);
323167857Simp		if (error)
324167857Simp			break;
325294674Sian		error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT);
326289666Sian		if (error) {
327289666Sian			error = iic2errno(error);
328167857Simp			break;
329289666Sian		}
330289666Sian		/* Read after write to wait for write-done. */
331167857Simp		waitlimit = 10000;
332167857Simp		rd[0].slave = slave;
333289666Sian		do {
334294674Sian			error = iicbus_transfer_excl(sc->dev, rd, 1,
335294674Sian			    IIC_INTRWAIT);
336167857Simp		} while (waitlimit-- > 0 && error != 0);
337167857Simp		if (error) {
338289666Sian			error = iic2errno(error);
339289666Sian			break;
340167857Simp		}
341167857Simp	}
342167857Simp	return error;
343167857Simp}
344167857Simp
345167857Simpstatic device_method_t icee_methods[] = {
346167857Simp	DEVMETHOD(device_probe,		icee_probe),
347167857Simp	DEVMETHOD(device_attach,	icee_attach),
348167857Simp
349246128Ssbz	DEVMETHOD_END
350167857Simp};
351167857Simp
352167857Simpstatic driver_t icee_driver = {
353167857Simp	"icee",
354167857Simp	icee_methods,
355167857Simp	sizeof(struct icee_softc),
356167857Simp};
357167857Simpstatic devclass_t icee_devclass;
358167857Simp
359167857SimpDRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
360167857SimpMODULE_VERSION(icee, 1);
361167857SimpMODULE_DEPEND(icee, iicbus, 1, 1, 1);
362