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 346548 2019-04-22 13:51:25Z 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>
41346548Sian#include <sys/sysctl.h>
42167857Simp#include <sys/uio.h>
43167857Simp#include <machine/bus.h>
44289727Sian
45289727Sian#ifdef FDT
46289727Sian#include <dev/ofw/ofw_bus.h>
47289727Sian#include <dev/ofw/ofw_bus_subr.h>
48289727Sian#endif
49289727Sian
50167857Simp#include <dev/iicbus/iiconf.h>
51167857Simp#include <dev/iicbus/iicbus.h>
52167857Simp
53167857Simp#include "iicbus_if.h"
54167857Simp
55289727Sian/*
56289727Sian * AT24 parts have a "write page size" that differs per-device, and a "read page
57289727Sian * size" that is always equal to the full device size.  We define maximum values
58289727Sian * here to limit how long we occupy the bus with a single transfer, and because
59289727Sian * there are temporary buffers of these sizes allocated on the stack.
60289727Sian */
61167857Simp#define	MAX_RD_SZ	256	/* Largest read size we support */
62289727Sian#define	MAX_WR_SZ	256	/* Largest write size we support */
63167857Simp
64167857Simpstruct icee_softc {
65289727Sian	device_t	dev;		/* Myself */
66167857Simp	struct cdev	*cdev;		/* user interface */
67289727Sian	int		addr;		/* Slave address on the bus */
68167857Simp	int		size;		/* How big am I? */
69289727Sian	int		type;		/* What address type 8 or 16 bit? */
70167857Simp	int		wr_sz;		/* What's the write page size */
71167857Simp};
72167857Simp
73289727Sian#ifdef FDT
74289727Sianstruct eeprom_desc {
75289727Sian	int	    type;
76289727Sian	int	    size;
77289727Sian	int	    wr_sz;
78289727Sian	const char *name;
79289727Sian};
80289727Sian
81289727Sianstatic struct eeprom_desc type_desc[] = {
82289727Sian	{ 8,        128,   8, "AT24C01"},
83289727Sian	{ 8,        256,   8, "AT24C02"},
84289727Sian	{ 8,        512,  16, "AT24C04"},
85289727Sian	{ 8,       1024,  16, "AT24C08"},
86289727Sian	{ 8,   2 * 1024,  16, "AT24C16"},
87289727Sian	{16,   4 * 1024,  32, "AT24C32"},
88289727Sian	{16,   8 * 1024,  32, "AT24C64"},
89289727Sian	{16,  16 * 1024,  64, "AT24C128"},
90289727Sian	{16,  32 * 1024,  64, "AT24C256"},
91289727Sian	{16,  64 * 1024, 128, "AT24C512"},
92289727Sian	{16, 128 * 1024, 256, "AT24CM01"},
93289727Sian};
94289727Sian
95289727Sianstatic struct ofw_compat_data compat_data[] = {
96289727Sian	{"atmel,24c01",	  (uintptr_t)(&type_desc[0])},
97289727Sian	{"atmel,24c02",	  (uintptr_t)(&type_desc[1])},
98289727Sian	{"atmel,24c04",	  (uintptr_t)(&type_desc[2])},
99289727Sian	{"atmel,24c08",	  (uintptr_t)(&type_desc[3])},
100289727Sian	{"atmel,24c16",	  (uintptr_t)(&type_desc[4])},
101289727Sian	{"atmel,24c32",	  (uintptr_t)(&type_desc[5])},
102289727Sian	{"atmel,24c64",	  (uintptr_t)(&type_desc[6])},
103289727Sian	{"atmel,24c128",  (uintptr_t)(&type_desc[7])},
104289727Sian	{"atmel,24c256",  (uintptr_t)(&type_desc[8])},
105289727Sian	{"atmel,24c512",  (uintptr_t)(&type_desc[9])},
106289727Sian	{"atmel,24c1024", (uintptr_t)(&type_desc[10])},
107289727Sian	{NULL,		  (uintptr_t)NULL},
108289727Sian};
109289727Sian#endif
110289727Sian
111167857Simp#define CDEV2SOFTC(dev)		((dev)->si_drv1)
112167857Simp
113167857Simp/* cdev routines */
114167857Simpstatic d_open_t icee_open;
115167857Simpstatic d_close_t icee_close;
116167857Simpstatic d_read_t icee_read;
117167857Simpstatic d_write_t icee_write;
118167857Simp
119167857Simpstatic struct cdevsw icee_cdevsw =
120167857Simp{
121167857Simp	.d_version = D_VERSION,
122181305Sjhb	.d_flags = D_TRACKCLOSE,
123167857Simp	.d_open = icee_open,
124167857Simp	.d_close = icee_close,
125167857Simp	.d_read = icee_read,
126167857Simp	.d_write = icee_write
127167857Simp};
128167857Simp
129289727Sian#ifdef FDT
130167857Simpstatic int
131167857Simpicee_probe(device_t dev)
132167857Simp{
133289727Sian	struct eeprom_desc *d;
134289083Sian
135289727Sian	if (!ofw_bus_status_okay(dev))
136289727Sian		return (ENXIO);
137289727Sian
138289727Sian	d = (struct eeprom_desc *)
139289727Sian	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
140289727Sian	if (d == NULL)
141289727Sian		return (ENXIO);
142289727Sian
143289727Sian	device_set_desc(dev, d->name);
144289727Sian	return (BUS_PROBE_DEFAULT);
145289727Sian}
146289727Sian
147289727Sianstatic void
148289727Sianicee_init(struct icee_softc *sc)
149289727Sian{
150289727Sian	struct eeprom_desc *d;
151289727Sian
152289727Sian	d = (struct eeprom_desc *)
153289727Sian	    ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
154289727Sian	if (d == NULL)
155289727Sian		return; /* attach will see sc->size == 0 and return error */
156289727Sian
157289727Sian	sc->size  = d->size;
158289727Sian	sc->type  = d->type;
159289727Sian	sc->wr_sz = d->wr_sz;
160289727Sian}
161289727Sian#else /* !FDT */
162289727Sianstatic int
163289727Sianicee_probe(device_t dev)
164289727Sian{
165289727Sian
166167857Simp	device_set_desc(dev, "I2C EEPROM");
167186833Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
168167857Simp}
169167857Simp
170289727Sianstatic void
171289727Sianicee_init(struct icee_softc *sc)
172289727Sian{
173289727Sian	const char *dname;
174289727Sian	int dunit;
175289727Sian
176289727Sian	dname = device_get_name(sc->dev);
177289727Sian	dunit = device_get_unit(sc->dev);
178289727Sian	resource_int_value(dname, dunit, "size", &sc->size);
179289727Sian	resource_int_value(dname, dunit, "type", &sc->type);
180289727Sian	resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
181289727Sian}
182289727Sian#endif /* FDT */
183289727Sian
184167857Simpstatic int
185167857Simpicee_attach(device_t dev)
186167857Simp{
187167857Simp	struct icee_softc *sc = device_get_softc(dev);
188346548Sian	struct sysctl_ctx_list *ctx;
189346548Sian	struct sysctl_oid_list *tree;
190167857Simp
191289727Sian	sc->dev = dev;
192181325Sstas	sc->addr = iicbus_get_addr(dev);
193289727Sian	icee_init(sc);
194289727Sian	if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
195289727Sian		device_printf(sc->dev, "Missing config data, "
196289727Sian		    "these cannot be zero: size %d type %d wr_sz %d\n",
197289727Sian		    sc->size, sc->type, sc->wr_sz);
198289727Sian		return (EINVAL);
199289727Sian	}
200167857Simp	if (bootverbose)
201289727Sian		device_printf(dev, "size: %d bytes, addressing: %d-bits\n",
202167857Simp		    sc->size, sc->type);
203167857Simp	sc->cdev = make_dev(&icee_cdevsw, device_get_unit(dev), UID_ROOT,
204167857Simp	    GID_WHEEL, 0600, "icee%d", device_get_unit(dev));
205167857Simp	if (sc->cdev == NULL) {
206289727Sian		return (ENOMEM);
207167857Simp	}
208167857Simp	sc->cdev->si_drv1 = sc;
209346548Sian
210346548Sian	ctx = device_get_sysctl_ctx(dev);
211346548Sian	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
212346548Sian	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "address_size", CTLFLAG_RD,
213346548Sian	    &sc->type, 0, "Memory array address size in bits");
214346548Sian	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "device_size", CTLFLAG_RD,
215346548Sian	    &sc->size, 0, "Memory array capacity in bytes");
216346548Sian	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "write_size", CTLFLAG_RD,
217346548Sian	    &sc->wr_sz, 0, "Memory array page write size in bytes");
218346548Sian
219289727Sian	return (0);
220167857Simp}
221167857Simp
222323931Sianstatic int
223323931Sianicee_detach(device_t dev)
224323931Sian{
225323931Sian	struct icee_softc *sc = device_get_softc(dev);
226323931Sian
227323931Sian	destroy_dev(sc->cdev);
228323931Sian	return (0);
229323931Sian}
230323931Sian
231167857Simpstatic int
232167857Simpicee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
233167857Simp{
234323931Sian	struct icee_softc *sc;
235167857Simp
236323931Sian	sc = CDEV2SOFTC(dev);
237323931Sian	if (device_get_state(sc->dev) < DS_BUSY)
238323931Sian		device_busy(sc->dev);
239323931Sian
240289083Sian	return (0);
241167857Simp}
242167857Simp
243167857Simpstatic int
244167857Simpicee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
245167857Simp{
246323931Sian	struct icee_softc *sc;
247167857Simp
248323931Sian	sc = CDEV2SOFTC(dev);
249323931Sian	device_unbusy(sc->dev);
250167857Simp	return (0);
251167857Simp}
252167857Simp
253167857Simpstatic int
254167857Simpicee_read(struct cdev *dev, struct uio *uio, int ioflag)
255167857Simp{
256167857Simp	struct icee_softc *sc;
257167857Simp	uint8_t addr[2];
258167857Simp	uint8_t data[MAX_RD_SZ];
259167857Simp	int error, i, len, slave;
260167857Simp	struct iic_msg msgs[2] = {
261167857Simp	     { 0, IIC_M_WR, 1, addr },
262167857Simp	     { 0, IIC_M_RD, 0, data },
263167857Simp	};
264167857Simp
265167857Simp	sc = CDEV2SOFTC(dev);
266167857Simp	if (uio->uio_offset == sc->size)
267167857Simp		return (0);
268167857Simp	if (uio->uio_offset > sc->size)
269167857Simp		return (EIO);
270167857Simp	if (sc->type != 8 && sc->type != 16)
271167857Simp		return (EINVAL);
272167857Simp	slave = error = 0;
273167857Simp	while (uio->uio_resid > 0) {
274167857Simp		if (uio->uio_offset >= sc->size)
275167857Simp			break;
276289727Sian		len = MIN(MAX_RD_SZ - (uio->uio_offset & (MAX_RD_SZ - 1)),
277167857Simp		    uio->uio_resid);
278167857Simp		switch (sc->type) {
279167857Simp		case 8:
280167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
281167857Simp			msgs[0].len = 1;
282167857Simp			msgs[1].len = len;
283167857Simp			addr[0] = uio->uio_offset & 0xff;
284167857Simp			break;
285167857Simp		case 16:
286167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
287167857Simp			msgs[0].len = 2;
288167857Simp			msgs[1].len = len;
289167857Simp			addr[0] = (uio->uio_offset >> 8) & 0xff;
290167857Simp			addr[1] = uio->uio_offset & 0xff;
291167857Simp			break;
292167857Simp		}
293167857Simp		for (i = 0; i < 2; i++)
294167857Simp			msgs[i].slave = slave;
295289727Sian		error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
296289105Sian		if (error) {
297289105Sian			error = iic2errno(error);
298167857Simp			break;
299289105Sian		}
300167857Simp		error = uiomove(data, len, uio);
301167857Simp		if (error)
302167857Simp			break;
303167857Simp	}
304167857Simp	return (error);
305167857Simp}
306167857Simp
307167857Simp/*
308167857Simp * Write to the part.  We use three transfers here since we're actually
309167857Simp * doing a write followed by a read to make sure that the write finished.
310167857Simp * It is easier to encode the dummy read here than to break things up
311167857Simp * into smaller chunks...
312167857Simp */
313167857Simpstatic int
314167857Simpicee_write(struct cdev *dev, struct uio *uio, int ioflag)
315167857Simp{
316167857Simp	struct icee_softc *sc;
317167857Simp	int error, len, slave, waitlimit;
318167857Simp	uint8_t data[MAX_WR_SZ + 2];
319167857Simp	struct iic_msg wr[1] = {
320167857Simp	     { 0, IIC_M_WR, 0, data },
321167857Simp	};
322167857Simp	struct iic_msg rd[1] = {
323167857Simp	     { 0, IIC_M_RD, 1, data },
324167857Simp	};
325167857Simp
326167857Simp	sc = CDEV2SOFTC(dev);
327167857Simp	if (uio->uio_offset >= sc->size)
328167857Simp		return (EIO);
329167857Simp	if (sc->type != 8 && sc->type != 16)
330167857Simp		return (EINVAL);
331289118Sian
332167857Simp	slave = error = 0;
333167857Simp	while (uio->uio_resid > 0) {
334167857Simp		if (uio->uio_offset >= sc->size)
335167857Simp			break;
336167857Simp		len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
337167857Simp		    uio->uio_resid);
338167857Simp		switch (sc->type) {
339167857Simp		case 8:
340167857Simp			slave = (uio->uio_offset >> 7) | sc->addr;
341167857Simp			wr[0].len = 1 + len;
342167857Simp			data[0] = uio->uio_offset & 0xff;
343167857Simp			break;
344167857Simp		case 16:
345167857Simp			slave = sc->addr | (uio->uio_offset >> 15);
346167857Simp			wr[0].len = 2 + len;
347167857Simp			data[0] = (uio->uio_offset >> 8) & 0xff;
348167857Simp			data[1] = uio->uio_offset & 0xff;
349167857Simp			break;
350167857Simp		}
351167857Simp		wr[0].slave = slave;
352167857Simp		error = uiomove(data + sc->type / 8, len, uio);
353167857Simp		if (error)
354167857Simp			break;
355289727Sian		error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT);
356289105Sian		if (error) {
357289105Sian			error = iic2errno(error);
358167857Simp			break;
359289105Sian		}
360289083Sian		/* Read after write to wait for write-done. */
361167857Simp		waitlimit = 10000;
362167857Simp		rd[0].slave = slave;
363289083Sian		do {
364289727Sian			error = iicbus_transfer_excl(sc->dev, rd, 1,
365289727Sian			    IIC_INTRWAIT);
366167857Simp		} while (waitlimit-- > 0 && error != 0);
367289105Sian		if (error) {
368289105Sian			error = iic2errno(error);
369289083Sian			break;
370289105Sian		}
371167857Simp	}
372167857Simp	return error;
373167857Simp}
374167857Simp
375167857Simpstatic device_method_t icee_methods[] = {
376167857Simp	DEVMETHOD(device_probe,		icee_probe),
377167857Simp	DEVMETHOD(device_attach,	icee_attach),
378323931Sian	DEVMETHOD(device_detach,	icee_detach),
379167857Simp
380246128Ssbz	DEVMETHOD_END
381167857Simp};
382167857Simp
383167857Simpstatic driver_t icee_driver = {
384167857Simp	"icee",
385167857Simp	icee_methods,
386167857Simp	sizeof(struct icee_softc),
387167857Simp};
388167857Simpstatic devclass_t icee_devclass;
389167857Simp
390167857SimpDRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
391167857SimpMODULE_VERSION(icee, 1);
392167857SimpMODULE_DEPEND(icee, iicbus, 1, 1, 1);
393