]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/iicbus/icee.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / iicbus / icee.c
1 /*-
2  * Copyright (c) 2006 Warner Losh.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 /*
28  * Generic IIC eeprom support, modeled after the AT24C family of products.
29  */
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/resource.h>
38 #include <sys/uio.h>
39 #include <machine/bus.h>
40 #include <dev/iicbus/iiconf.h>
41 #include <dev/iicbus/iicbus.h>
42
43 #include "iicbus_if.h"
44
45 #define IIC_M_WR        0       /* write operation */
46 #define MAX_RD_SZ       256     /* Largest read size we support */
47 #define MAX_WR_SZ       256     /* Largest write size we support */
48
49 struct icee_softc {
50         device_t        sc_dev;         /* Myself */
51         struct mtx      sc_mtx;         /* basically a perimeter lock */
52         struct cdev     *cdev;          /* user interface */
53         int             addr;
54         int             flags;
55 #define OPENED  1
56         int             size;           /* How big am I? */
57         int             type;           /* What type 8 or 16 bit? */
58         int             rd_sz;          /* What's the read page size */
59         int             wr_sz;          /* What's the write page size */
60 };
61
62 #define ICEE_LOCK(_sc)          mtx_lock(&(_sc)->sc_mtx)
63 #define ICEE_UNLOCK(_sc)        mtx_unlock(&(_sc)->sc_mtx)
64 #define ICEE_LOCK_INIT(_sc) \
65         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), "icee", MTX_DEF)
66 #define ICEE_LOCK_DESTROY(_sc)  mtx_destroy(&_sc->sc_mtx);
67 #define ICEE_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
68 #define ICEE_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
69 #define CDEV2SOFTC(dev)         ((dev)->si_drv1)
70
71 /* cdev routines */
72 static d_open_t icee_open;
73 static d_close_t icee_close;
74 static d_read_t icee_read;
75 static d_write_t icee_write;
76
77 static struct cdevsw icee_cdevsw =
78 {
79         .d_version = D_VERSION,
80         .d_open = icee_open,
81         .d_close = icee_close,
82         .d_read = icee_read,
83         .d_write = icee_write
84 };
85
86 static int
87 icee_probe(device_t dev)
88 {
89         /* XXX really probe? -- not until we know the size... */
90         device_set_desc(dev, "I2C EEPROM");
91         return (0);
92 }
93
94 static int
95 icee_attach(device_t dev)
96 {
97         struct icee_softc *sc = device_get_softc(dev);
98         const char *dname;
99         int dunit, err;
100
101         sc->sc_dev = dev;
102         iicbus_get_addr(dev, &sc->addr);
103         err = 0;
104         dname = device_get_name(dev);
105         dunit = device_get_unit(dev);
106         resource_int_value(dname, dunit, "size", &sc->size);
107         resource_int_value(dname, dunit, "type", &sc->type);
108         resource_int_value(dname, dunit, "rd_sz", &sc->rd_sz);
109         if (sc->rd_sz > MAX_RD_SZ)
110                 sc->rd_sz = MAX_RD_SZ;
111         resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
112         if (bootverbose)
113                 device_printf(dev, "size: %d bytes bus_width: %d-bits\n",
114                     sc->size, sc->type);
115         sc->cdev = make_dev(&icee_cdevsw, device_get_unit(dev), UID_ROOT,
116             GID_WHEEL, 0600, "icee%d", device_get_unit(dev));
117         if (sc->cdev == NULL) {
118                 err = ENOMEM;
119                 goto out;
120         }
121         sc->cdev->si_drv1 = sc;
122         ICEE_LOCK_INIT(sc);
123 out:;
124         return (err);
125 }
126
127 static int 
128 icee_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
129 {
130         struct icee_softc *sc;
131
132         sc = CDEV2SOFTC(dev);
133         ICEE_LOCK(sc);
134         if (!(sc->flags & OPENED)) {
135                 sc->flags |= OPENED;
136         }
137         ICEE_UNLOCK(sc);
138         return (0);
139 }
140
141 static int
142 icee_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
143 {
144         struct icee_softc *sc;
145
146         sc = CDEV2SOFTC(dev);
147         ICEE_LOCK(sc);
148         sc->flags &= ~OPENED;
149         ICEE_UNLOCK(sc);
150         return (0);
151 }
152
153 static int
154 icee_read(struct cdev *dev, struct uio *uio, int ioflag)
155 {
156         struct icee_softc *sc;
157         uint8_t addr[2];
158         uint8_t data[MAX_RD_SZ];
159         int error, i, len, slave;
160         struct iic_msg msgs[2] = {
161              { 0, IIC_M_WR, 1, addr },
162              { 0, IIC_M_RD, 0, data },
163         };
164
165         sc = CDEV2SOFTC(dev);
166         if (uio->uio_offset == sc->size)
167                 return (0);
168         if (uio->uio_offset > sc->size)
169                 return (EIO);
170         if (sc->type != 8 && sc->type != 16)
171                 return (EINVAL);
172         ICEE_LOCK(sc);
173         slave = error = 0;
174         while (uio->uio_resid > 0) {
175                 if (uio->uio_offset >= sc->size)
176                         break;
177                 len = MIN(sc->rd_sz - (uio->uio_offset & (sc->rd_sz - 1)),
178                     uio->uio_resid);
179                 switch (sc->type) {
180                 case 8:
181                         slave = (uio->uio_offset >> 7) | sc->addr;
182                         msgs[0].len = 1;
183                         msgs[1].len = len;
184                         addr[0] = uio->uio_offset & 0xff;
185                         break;
186                 case 16:
187                         slave = sc->addr | (uio->uio_offset >> 15);
188                         msgs[0].len = 2;
189                         msgs[1].len = len;
190                         addr[0] = (uio->uio_offset >> 8) & 0xff;
191                         addr[1] = uio->uio_offset & 0xff;
192                         break;
193                 }
194                 for (i = 0; i < 2; i++)
195                         msgs[i].slave = slave;
196                 error = iicbus_transfer(sc->sc_dev, msgs, 2);
197                 if (error)
198                         break;
199                 error = uiomove(data, len, uio);
200                 if (error)
201                         break;
202         }
203         ICEE_UNLOCK(sc);
204         return (error);
205 }
206
207 /*
208  * Write to the part.  We use three transfers here since we're actually
209  * doing a write followed by a read to make sure that the write finished.
210  * It is easier to encode the dummy read here than to break things up
211  * into smaller chunks...
212  */
213 static int
214 icee_write(struct cdev *dev, struct uio *uio, int ioflag)
215 {
216         struct icee_softc *sc;
217         int error, len, slave, waitlimit;
218         uint8_t data[MAX_WR_SZ + 2];
219         struct iic_msg wr[1] = {
220              { 0, IIC_M_WR, 0, data },
221         };
222         struct iic_msg rd[1] = {
223              { 0, IIC_M_RD, 1, data },
224         };
225
226         sc = CDEV2SOFTC(dev);
227         if (uio->uio_offset >= sc->size)
228                 return (EIO);
229         if (sc->type != 8 && sc->type != 16)
230                 return (EINVAL);
231         ICEE_LOCK(sc);
232         slave = error = 0;
233         while (uio->uio_resid > 0) {
234                 if (uio->uio_offset >= sc->size)
235                         break;
236                 len = MIN(sc->wr_sz - (uio->uio_offset & (sc->wr_sz - 1)),
237                     uio->uio_resid);
238                 switch (sc->type) {
239                 case 8:
240                         slave = (uio->uio_offset >> 7) | sc->addr;
241                         wr[0].len = 1 + len;
242                         data[0] = uio->uio_offset & 0xff;
243                         break;
244                 case 16:
245                         slave = sc->addr | (uio->uio_offset >> 15);
246                         wr[0].len = 2 + len;
247                         data[0] = (uio->uio_offset >> 8) & 0xff;
248                         data[1] = uio->uio_offset & 0xff;
249                         break;
250                 }
251                 wr[0].slave = slave;
252                 error = uiomove(data + sc->type / 8, len, uio);
253                 if (error)
254                         break;
255                 error = iicbus_transfer(sc->sc_dev, wr, 1);
256                 if (error)
257                         break;
258                 // Now wait for the write to be done by trying to read
259                 // the part.
260                 waitlimit = 10000;
261                 rd[0].slave = slave;
262                 do 
263                 {
264                     error = iicbus_transfer(sc->sc_dev, rd, 1);
265                 } while (waitlimit-- > 0 && error != 0);
266                 if (error) {
267                     printf("waiting for write failed %d\n", error);
268                     break;
269                 }
270         }
271         ICEE_UNLOCK(sc);
272         return error;
273 }
274
275 static device_method_t icee_methods[] = {
276         DEVMETHOD(device_probe,         icee_probe),
277         DEVMETHOD(device_attach,        icee_attach),
278
279         {0, 0},
280 };
281
282 static driver_t icee_driver = {
283         "icee",
284         icee_methods,
285         sizeof(struct icee_softc),
286 };
287 static devclass_t icee_devclass;
288
289 DRIVER_MODULE(icee, iicbus, icee_driver, icee_devclass, 0, 0);
290 MODULE_VERSION(icee, 1);
291 MODULE_DEPEND(icee, iicbus, 1, 1, 1);