xref: /netbsd-src/sys/dev/i2c/at24cxx.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: at24cxx.c,v 1.9 2007/12/11 05:38:12 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: at24cxx.c,v 1.9 2007/12/11 05:38:12 lukem Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/uio.h>
47 #include <sys/conf.h>
48 #include <sys/proc.h>
49 #include <sys/event.h>
50 
51 #include <sys/bus.h>
52 
53 #include <dev/i2c/i2cvar.h>
54 #include <dev/i2c/at24cxxvar.h>
55 
56 /*
57  * AT24Cxx EEPROM I2C address:
58  *	101 0xxx
59  * (and others depending on the exact model)  The bigger 8-bit parts
60  * decode multiple addresses.  The bigger 16-bit parts do too (those
61  * larger than 512kb).  Be sure to check the datasheet of your EEPROM
62  * because there's much variation between models.
63  */
64 #define	AT24CXX_ADDRMASK	0x78
65 #define	AT24CXX_ADDR		0x50
66 
67 #define	AT24CXX_WRITE_CYCLE_MS	10
68 #define	AT24CXX_ADDR_HI(a)	(((a) >> 8) & 0x1f)
69 #define	AT24CXX_ADDR_LO(a)	((a) & 0xff)
70 
71 #include "seeprom.h"
72 
73 #if NSEEPROM > 0
74 
75 struct seeprom_softc {
76 	struct device sc_dev;
77 	i2c_tag_t sc_tag;
78 	int sc_address;
79 	int sc_size;
80 	int sc_cmdlen;
81 	int sc_open;
82 };
83 
84 static int  seeprom_match(struct device *, struct cfdata *, void *);
85 static void seeprom_attach(struct device *, struct device *, void *);
86 
87 CFATTACH_DECL(seeprom, sizeof(struct seeprom_softc),
88 	seeprom_match, seeprom_attach, NULL, NULL);
89 extern struct cfdriver seeprom_cd;
90 
91 dev_type_open(seeprom_open);
92 dev_type_close(seeprom_close);
93 dev_type_read(seeprom_read);
94 dev_type_write(seeprom_write);
95 
96 const struct cdevsw seeprom_cdevsw = {
97 	seeprom_open, seeprom_close, seeprom_read, seeprom_write, noioctl,
98 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
99 };
100 
101 static int seeprom_wait_idle(struct seeprom_softc *);
102 
103 
104 static int
105 seeprom_match(struct device *parent, struct cfdata *cf, void *aux)
106 {
107 	struct i2c_attach_args *ia = aux;
108 
109 	if ((ia->ia_addr & AT24CXX_ADDRMASK) == AT24CXX_ADDR)
110 		return (1);
111 
112 	return (0);
113 }
114 
115 static void
116 seeprom_attach(struct device *parent, struct device *self, void *aux)
117 {
118 	struct seeprom_softc *sc = device_private(self);
119 	struct i2c_attach_args *ia = aux;
120 
121 	sc->sc_tag = ia->ia_tag;
122 	sc->sc_address = ia->ia_addr;
123 
124 	aprint_naive(": EEPROM\n");
125 	aprint_normal(": AT24Cxx EEPROM\n");
126 
127 	/*
128 	 * The AT24C01A/02/04/08/16 EEPROMs use a 1 byte command
129 	 * word to select the offset into the EEPROM page.  The
130 	 * AT24C04/08/16 decode fewer of the i2c address bits,
131 	 * using the bottom 1, 2, or 3 to select the 256-byte
132 	 * super-page.
133 	 *
134 	 * The AT24C32/64/128/256/512 EEPROMs use a 2 byte command
135 	 * word and decode all of the i2c address bits.
136 	 *
137 	 * The AT24C1024 EEPROMs use a 2 byte command and also do bank
138 	 * switching to select the proper super-page.  This isn't
139 	 * supported by this driver.
140 	 */
141 	sc->sc_size = ia->ia_size;
142 	switch (sc->sc_size) {
143 	case 128:		/* 1Kbit */
144 	case 256:		/* 2Kbit */
145 	case 512:		/* 4Kbit */
146 	case 1024:		/* 8Kbit */
147 	case 2048:		/* 16Kbit */
148 		sc->sc_cmdlen = 1;
149 		break;
150 
151 	case 4096:		/* 32Kbit */
152 	case 8192:		/* 64Kbit */
153 	case 16384:		/* 128Kbit */
154 	case 32768:		/* 256Kbit */
155 	case 65536:		/* 512Kbit */
156 		sc->sc_cmdlen = 2;
157 		break;
158 
159 	default:
160 		/*
161 		 * Default to 2KB.  If we happen to have a 2KB
162 		 * EEPROM this will allow us to access it.  If we
163 		 * have a smaller one, the worst that can happen
164 		 * is that we end up trying to read a different
165 		 * EEPROM on the bus when accessing it.
166 		 *
167 		 * Obviously this will not work for 4KB or 8KB
168 		 * EEPROMs, but them's the breaks.
169 		 */
170 		aprint_error("%s: invalid size specified; "
171 		    "assuming 2KB (16Kb)\n", sc->sc_dev.dv_xname);
172 		sc->sc_size = 2048;
173 		sc->sc_cmdlen = 1;
174 	}
175 
176 	sc->sc_open = 0;
177 }
178 
179 /*ARGSUSED*/
180 int
181 seeprom_open(dev_t dev, int flag, int fmt, struct lwp *l)
182 {
183 	struct seeprom_softc *sc;
184 
185 	if ((sc = device_lookup(&seeprom_cd, minor(dev))) == NULL)
186 		return (ENXIO);
187 
188 	/* XXX: Locking */
189 
190 	if (sc->sc_open)
191 		return (EBUSY);
192 
193 	sc->sc_open = 1;
194 	return (0);
195 }
196 
197 /*ARGSUSED*/
198 int
199 seeprom_close(dev_t dev, int flag, int fmt, struct lwp *l)
200 {
201 	struct seeprom_softc *sc;
202 
203 	if ((sc = device_lookup(&seeprom_cd, minor(dev))) == NULL)
204 		return (ENXIO);
205 
206 	sc->sc_open = 0;
207 	return (0);
208 }
209 
210 /*ARGSUSED*/
211 int
212 seeprom_read(dev_t dev, struct uio *uio, int flags)
213 {
214 	struct seeprom_softc *sc;
215 	i2c_addr_t addr;
216 	u_int8_t ch, cmdbuf[2];
217 	int a, error;
218 
219 	if ((sc = device_lookup(&seeprom_cd, minor(dev))) == NULL)
220 		return (ENXIO);
221 
222 	if (uio->uio_offset >= sc->sc_size)
223 		return (EINVAL);
224 
225 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
226 		return (error);
227 
228 	/*
229 	 * Even though the AT24Cxx EEPROMs support sequential
230 	 * reads within a page, some I2C controllers do not
231 	 * support anything other than single-byte transfers,
232 	 * so we're stuck with this lowest-common-denominator.
233 	 */
234 
235 	while (uio->uio_resid > 0 && uio->uio_offset < sc->sc_size) {
236 		a = (int)uio->uio_offset;
237 		if (sc->sc_cmdlen == 1) {
238 			addr = sc->sc_address + (a >> 8);
239 			cmdbuf[0] = a & 0xff;
240 		} else {
241 			addr = sc->sc_address;
242 			cmdbuf[0] = AT24CXX_ADDR_HI(a);
243 			cmdbuf[1] = AT24CXX_ADDR_LO(a);
244 		}
245 		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
246 				      addr, cmdbuf, sc->sc_cmdlen,
247 				      &ch, 1, 0)) != 0) {
248 			iic_release_bus(sc->sc_tag, 0);
249 			printf("%s: seeprom_read: byte read failed at 0x%x\n",
250 			    sc->sc_dev.dv_xname, a);
251 			return (error);
252 		}
253 		if ((error = uiomove(&ch, 1, uio)) != 0) {
254 			iic_release_bus(sc->sc_tag, 0);
255 			return (error);
256 		}
257 	}
258 
259 	iic_release_bus(sc->sc_tag, 0);
260 
261 	return (0);
262 }
263 
264 /*ARGSUSED*/
265 int
266 seeprom_write(dev_t dev, struct uio *uio, int flags)
267 {
268 	struct seeprom_softc *sc;
269 	i2c_addr_t addr;
270 	u_int8_t ch, cmdbuf[2];
271 	int a, error;
272 
273 	if ((sc = device_lookup(&seeprom_cd, minor(dev))) == NULL)
274 		return (ENXIO);
275 
276 	if (uio->uio_offset >= sc->sc_size)
277 		return (EINVAL);
278 
279 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
280 		return (error);
281 
282 	/*
283 	 * See seeprom_read() for why we don't use sequential
284 	 * writes within a page.
285 	 */
286 
287 	while (uio->uio_resid > 0 && uio->uio_offset < sc->sc_size) {
288 		a = (int)uio->uio_offset;
289 		if (sc->sc_cmdlen == 1) {
290 			addr = sc->sc_address + (a >> 8);
291 			cmdbuf[0] = a & 0xff;
292 		} else {
293 			addr = sc->sc_address;
294 			cmdbuf[0] = AT24CXX_ADDR_HI(a);
295 			cmdbuf[1] = AT24CXX_ADDR_LO(a);
296 		}
297 		if ((error = uiomove(&ch, 1, uio)) != 0) {
298 			iic_release_bus(sc->sc_tag, 0);
299 			return (error);
300 		}
301 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
302 				      addr, cmdbuf, sc->sc_cmdlen,
303 				      &ch, 1, 0)) != 0) {
304 			iic_release_bus(sc->sc_tag, 0);
305 			printf("%s: seeprom_write: byte write failed at 0x%x\n",
306 			    sc->sc_dev.dv_xname, a);
307 			return (error);
308 		}
309 
310 		/* Wait until the device commits the byte. */
311 		if ((error = seeprom_wait_idle(sc)) != 0) {
312 			iic_release_bus(sc->sc_tag, 0);
313 			return (error);
314 		}
315 	}
316 
317 	iic_release_bus(sc->sc_tag, 0);
318 
319 	return (0);
320 }
321 
322 static int
323 seeprom_wait_idle(struct seeprom_softc *sc)
324 {
325 	uint8_t cmdbuf[2] = { 0, 0 };
326 	int rv, timeout;
327 	u_int8_t dummy;
328 
329 	timeout = (1000 / hz) / AT24CXX_WRITE_CYCLE_MS;
330 	if (timeout == 0)
331 		timeout = 1;
332 
333 	delay(10);
334 
335 	/*
336 	 * Read the byte at address 0.  This is just a dummy
337 	 * read to wait for the EEPROM's write cycle to complete.
338 	 */
339 	while (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
340 			cmdbuf, sc->sc_cmdlen, &dummy, 1, 0)) {
341 		rv = tsleep(sc, PRIBIO | PCATCH, "seepromwr", timeout);
342 		if (rv != EWOULDBLOCK)
343 			return (rv);
344 	}
345 
346 	return (0);
347 }
348 
349 #endif /* NSEEPROM > 0 */
350 
351 int
352 seeprom_bootstrap_read(i2c_tag_t tag, int i2caddr, int offset, int devsize,
353     u_int8_t *rvp, size_t len)
354 {
355 	i2c_addr_t addr;
356 	int cmdlen;
357 	uint8_t cmdbuf[2];
358 
359 	if (len == 0)
360 		return (0);
361 
362 	/* We are very forgiving about devsize during bootstrap. */
363 	cmdlen = (devsize >= 4096) ? 2 : 1;
364 
365 	if (iic_acquire_bus(tag, I2C_F_POLL) != 0)
366 		return (-1);
367 
368 	while (len) {
369 		if (cmdlen == 1) {
370 			addr = i2caddr + (offset >> 8);
371 			cmdbuf[0] = offset & 0xff;
372 		} else {
373 			addr = i2caddr;
374 			cmdbuf[0] = AT24CXX_ADDR_HI(offset);
375 			cmdbuf[1] = AT24CXX_ADDR_LO(offset);
376 		}
377 
378 		/* Read a single byte. */
379 		if (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr,
380 			     cmdbuf, cmdlen, rvp, 1, I2C_F_POLL)) {
381 			iic_release_bus(tag, I2C_F_POLL);
382 			return (-1);
383 		}
384 
385 		len--;
386 		rvp++;
387 		offset++;
388 	}
389 
390 	iic_release_bus(tag, I2C_F_POLL);
391 	return (0);
392 }
393