icee.c revision 289727
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: head/sys/dev/iicbus/icee.c 289727 2015-10-22 01:04:31Z 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 209167857Simpstatic int 210167857Simpicee_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 211167857Simp{ 212167857Simp 213289083Sian 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; 246289727Sian 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; 265289727Sian error = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT); 266289105Sian if (error) { 267289105Sian error = iic2errno(error); 268167857Simp break; 269289105Sian } 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); 301289118Sian 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; 325289727Sian error = iicbus_transfer_excl(sc->dev, wr, 1, IIC_INTRWAIT); 326289105Sian if (error) { 327289105Sian error = iic2errno(error); 328167857Simp break; 329289105Sian } 330289083Sian /* Read after write to wait for write-done. */ 331167857Simp waitlimit = 10000; 332167857Simp rd[0].slave = slave; 333289083Sian do { 334289727Sian error = iicbus_transfer_excl(sc->dev, rd, 1, 335289727Sian IIC_INTRWAIT); 336167857Simp } while (waitlimit-- > 0 && error != 0); 337289105Sian if (error) { 338289105Sian error = iic2errno(error); 339289083Sian break; 340289105Sian } 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