1233539Sjchandra/*- 2233539Sjchandra * Copyright (c) 2003-2012 Broadcom Corporation 3233539Sjchandra * All Rights Reserved 4233539Sjchandra * 5233539Sjchandra * Redistribution and use in source and binary forms, with or without 6233539Sjchandra * modification, are permitted provided that the following conditions 7233539Sjchandra * are met: 8233539Sjchandra * 9233539Sjchandra * 1. Redistributions of source code must retain the above copyright 10233539Sjchandra * notice, this list of conditions and the following disclaimer. 11233539Sjchandra * 2. Redistributions in binary form must reproduce the above copyright 12233539Sjchandra * notice, this list of conditions and the following disclaimer in 13233539Sjchandra * the documentation and/or other materials provided with the 14233539Sjchandra * distribution. 15233539Sjchandra * 16233539Sjchandra * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR 17233539Sjchandra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18233539Sjchandra * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19233539Sjchandra * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE 20233539Sjchandra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21233539Sjchandra * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22233539Sjchandra * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23233539Sjchandra * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24233539Sjchandra * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25233539Sjchandra * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26233539Sjchandra * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27233539Sjchandra */ 28233539Sjchandra 29233539Sjchandra#include <sys/cdefs.h> 30233539Sjchandra__FBSDID("$FreeBSD: releng/11.0/sys/dev/iicbus/iicoc.c 296137 2016-02-27 03:38:01Z jhibbits $"); 31233539Sjchandra 32233539Sjchandra#include <sys/param.h> 33233539Sjchandra#include <sys/systm.h> 34233539Sjchandra#include <sys/kernel.h> 35233539Sjchandra#include <sys/lock.h> 36233539Sjchandra#include <sys/module.h> 37233539Sjchandra#include <sys/mutex.h> 38233539Sjchandra#include <sys/bus.h> 39233539Sjchandra#include <sys/rman.h> 40233539Sjchandra 41233539Sjchandra#include <machine/bus.h> 42233539Sjchandra 43233539Sjchandra#include <dev/iicbus/iiconf.h> 44233539Sjchandra#include <dev/iicbus/iicbus.h> 45233539Sjchandra#include <dev/iicbus/iicoc.h> 46233539Sjchandra 47233539Sjchandra#include <dev/pci/pcireg.h> 48233539Sjchandra#include <dev/pci/pcivar.h> 49233539Sjchandra 50233539Sjchandra#include "iicbus_if.h" 51233539Sjchandra 52233539Sjchandrastatic devclass_t iicoc_devclass; 53233539Sjchandra 54233539Sjchandra/* 55233539Sjchandra * Device methods 56233539Sjchandra */ 57233539Sjchandrastatic int iicoc_probe(device_t); 58233539Sjchandrastatic int iicoc_attach(device_t); 59233539Sjchandrastatic int iicoc_detach(device_t); 60233539Sjchandra 61233539Sjchandrastatic int iicoc_start(device_t dev, u_char slave, int timeout); 62233539Sjchandrastatic int iicoc_stop(device_t dev); 63233539Sjchandrastatic int iicoc_read(device_t dev, char *buf, 64233539Sjchandra int len, int *read, int last, int delay); 65233539Sjchandrastatic int iicoc_write(device_t dev, const char *buf, 66233539Sjchandra int len, int *sent, int timeout); 67233539Sjchandrastatic int iicoc_repeated_start(device_t dev, u_char slave, int timeout); 68233539Sjchandra 69233539Sjchandrastruct iicoc_softc { 70233539Sjchandra device_t dev; /* Self */ 71233539Sjchandra u_int reg_shift; /* Chip specific */ 72233539Sjchandra u_int clockfreq; 73233539Sjchandra u_int i2cfreq; 74233539Sjchandra struct resource *mem_res; /* Memory resource */ 75233539Sjchandra int mem_rid; 76233539Sjchandra int sc_started; 77233539Sjchandra uint8_t i2cdev_addr; 78233539Sjchandra device_t iicbus; 79233539Sjchandra struct mtx sc_mtx; 80233539Sjchandra}; 81233539Sjchandra 82233539Sjchandrastatic void 83233539Sjchandraiicoc_dev_write(device_t dev, int reg, int value) 84233539Sjchandra{ 85233539Sjchandra struct iicoc_softc *sc; 86233539Sjchandra 87233539Sjchandra sc = device_get_softc(dev); 88233539Sjchandra bus_write_1(sc->mem_res, reg<<sc->reg_shift, value); 89233539Sjchandra} 90233539Sjchandra 91233539Sjchandrastatic int 92233539Sjchandraiicoc_dev_read(device_t dev, int reg) 93233539Sjchandra{ 94233539Sjchandra uint8_t val; 95233539Sjchandra struct iicoc_softc *sc; 96233539Sjchandra 97233539Sjchandra sc = device_get_softc(dev); 98233539Sjchandra val = bus_read_1(sc->mem_res, reg<<sc->reg_shift); 99233539Sjchandra return (val); 100233539Sjchandra} 101233539Sjchandra 102233539Sjchandrastatic int 103233539Sjchandraiicoc_wait_on_status(device_t dev, uint8_t bit) 104233539Sjchandra{ 105233539Sjchandra int tries = I2C_TIMEOUT; 106233539Sjchandra uint8_t status; 107233539Sjchandra 108233539Sjchandra do { 109233539Sjchandra status = iicoc_dev_read(dev, OC_I2C_STATUS_REG); 110233539Sjchandra } while ((status & bit) != 0 && --tries > 0); 111233539Sjchandra 112233539Sjchandra return (tries == 0 ? -1: 0); 113233539Sjchandra} 114233539Sjchandra 115233539Sjchandrastatic int 116233539Sjchandraiicoc_rd_cmd(device_t dev, uint8_t cmd) 117233539Sjchandra{ 118233539Sjchandra uint8_t data; 119233539Sjchandra 120233539Sjchandra iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd); 121233539Sjchandra if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) { 122233539Sjchandra device_printf(dev, "read: Timeout waiting for TIP clear.\n"); 123233539Sjchandra return (-1); 124233539Sjchandra } 125233539Sjchandra data = iicoc_dev_read(dev, OC_I2C_DATA_REG); 126233539Sjchandra return (data); 127233539Sjchandra} 128233539Sjchandra 129233539Sjchandrastatic int 130233539Sjchandraiicoc_wr_cmd(device_t dev, uint8_t data, uint8_t cmd) 131233539Sjchandra{ 132233539Sjchandra 133233539Sjchandra iicoc_dev_write(dev, OC_I2C_DATA_REG, data); 134233539Sjchandra iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd); 135233539Sjchandra if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) { 136233539Sjchandra device_printf(dev, "write: Timeout waiting for TIP clear.\n"); 137233539Sjchandra return (-1); 138233539Sjchandra } 139233539Sjchandra return (0); 140233539Sjchandra} 141233539Sjchandra 142233539Sjchandrastatic int 143233539Sjchandraiicoc_wr_ack_cmd(device_t dev, uint8_t data, uint8_t cmd) 144233539Sjchandra{ 145233539Sjchandra if (iicoc_wr_cmd(dev, data, cmd) < 0) 146233539Sjchandra return (-1); 147233539Sjchandra 148233539Sjchandra if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_NACK) { 149233539Sjchandra device_printf(dev, "write: I2C command ACK Error.\n"); 150233539Sjchandra return (IIC_ENOACK); 151233539Sjchandra } 152233539Sjchandra return (0); 153233539Sjchandra} 154233539Sjchandra 155233539Sjchandrastatic int 156233539Sjchandraiicoc_init(device_t dev) 157233539Sjchandra{ 158233539Sjchandra struct iicoc_softc *sc; 159233539Sjchandra int value; 160233539Sjchandra 161233539Sjchandra sc = device_get_softc(dev); 162233539Sjchandra value = iicoc_dev_read(dev, OC_I2C_CTRL_REG); 163233539Sjchandra iicoc_dev_write(dev, OC_I2C_CTRL_REG, 164233539Sjchandra value & ~(OC_CONTROL_EN | OC_CONTROL_IEN)); 165233539Sjchandra value = (sc->clockfreq/(5 * sc->i2cfreq)) - 1; 166233539Sjchandra iicoc_dev_write(dev, OC_I2C_PRESCALE_LO_REG, value & 0xff); 167233539Sjchandra iicoc_dev_write(dev, OC_I2C_PRESCALE_HI_REG, value >> 8); 168233539Sjchandra value = iicoc_dev_read(dev, OC_I2C_CTRL_REG); 169233539Sjchandra iicoc_dev_write(dev, OC_I2C_CTRL_REG, value | OC_CONTROL_EN); 170233539Sjchandra 171233539Sjchandra value = iicoc_dev_read(dev, OC_I2C_CTRL_REG); 172233539Sjchandra /* return 0 on success, 1 on error */ 173233539Sjchandra return ((value & OC_CONTROL_EN) == 0); 174233539Sjchandra} 175233539Sjchandra 176233539Sjchandrastatic int 177233539Sjchandraiicoc_probe(device_t dev) 178233539Sjchandra{ 179233539Sjchandra struct iicoc_softc *sc; 180233539Sjchandra 181233539Sjchandra sc = device_get_softc(dev); 182233539Sjchandra if ((pci_get_vendor(dev) == 0x184e) && 183233539Sjchandra (pci_get_device(dev) == 0x1011)) { 184233539Sjchandra sc->clockfreq = XLP_I2C_CLKFREQ; 185233539Sjchandra sc->i2cfreq = XLP_I2C_FREQ; 186233539Sjchandra sc->reg_shift = 2; 187233539Sjchandra device_set_desc(dev, "Netlogic XLP I2C Controller"); 188233539Sjchandra return (BUS_PROBE_DEFAULT); 189233539Sjchandra } 190233539Sjchandra return (ENXIO); 191233539Sjchandra} 192233539Sjchandra 193233539Sjchandra 194233539Sjchandra/* 195233539Sjchandra * We add all the devices which we know about. 196233539Sjchandra * The generic attach routine will attach them if they are alive. 197233539Sjchandra */ 198233539Sjchandrastatic int 199233539Sjchandraiicoc_attach(device_t dev) 200233539Sjchandra{ 201233539Sjchandra int bus; 202233539Sjchandra struct iicoc_softc *sc; 203233539Sjchandra 204233539Sjchandra sc = device_get_softc(dev); 205233539Sjchandra bus = device_get_unit(dev); 206233539Sjchandra 207233539Sjchandra sc->dev = dev; 208233539Sjchandra mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF); 209233539Sjchandra sc->mem_rid = 0; 210296137Sjhibbits sc->mem_res = bus_alloc_resource_anywhere(dev, 211296137Sjhibbits SYS_RES_MEMORY, &sc->mem_rid, 0x100, RF_ACTIVE); 212233539Sjchandra 213233539Sjchandra if (sc->mem_res == NULL) { 214233539Sjchandra device_printf(dev, "Could not allocate bus resource.\n"); 215233539Sjchandra return (-1); 216233539Sjchandra } 217233539Sjchandra iicoc_init(dev); 218233539Sjchandra sc->iicbus = device_add_child(dev, "iicbus", -1); 219233539Sjchandra if (sc->iicbus == NULL) { 220233539Sjchandra device_printf(dev, "Could not allocate iicbus instance.\n"); 221233539Sjchandra return (-1); 222233539Sjchandra } 223233539Sjchandra bus_generic_attach(dev); 224233539Sjchandra 225233539Sjchandra return (0); 226233539Sjchandra} 227233539Sjchandra 228233539Sjchandrastatic int 229233539Sjchandraiicoc_detach(device_t dev) 230233539Sjchandra{ 231233539Sjchandra bus_generic_detach(dev); 232289657Sdumbbell device_delete_children(dev); 233233539Sjchandra 234233539Sjchandra return (0); 235233539Sjchandra} 236233539Sjchandra 237233539Sjchandrastatic int 238233539Sjchandraiicoc_start(device_t dev, u_char slave, int timeout) 239233539Sjchandra{ 240289093Sian int error = IIC_EBUSERR; 241233539Sjchandra struct iicoc_softc *sc; 242233539Sjchandra 243233539Sjchandra sc = device_get_softc(dev); 244233539Sjchandra mtx_lock(&sc->sc_mtx); 245233539Sjchandra sc->i2cdev_addr = (slave >> 1); 246233539Sjchandra 247233539Sjchandra /* Verify the bus is idle */ 248233539Sjchandra if (iicoc_wait_on_status(dev, OC_STATUS_BUSY) < 0) 249233539Sjchandra goto i2c_stx_error; 250233539Sjchandra 251233539Sjchandra /* Write Slave Address */ 252233539Sjchandra if (iicoc_wr_ack_cmd(dev, slave, OC_COMMAND_START)) { 253233539Sjchandra device_printf(dev, 254233539Sjchandra "I2C write slave address [0x%x] failed.\n", slave); 255233539Sjchandra error = IIC_ENOACK; 256233539Sjchandra goto i2c_stx_error; 257233539Sjchandra } 258233539Sjchandra 259233539Sjchandra /* Verify Arbitration is not Lost */ 260233539Sjchandra if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_AL) { 261233539Sjchandra device_printf(dev, "I2C Bus Arbitration Lost, Aborting.\n"); 262233539Sjchandra error = IIC_EBUSERR; 263233539Sjchandra goto i2c_stx_error; 264233539Sjchandra } 265233539Sjchandra error = IIC_NOERR; 266233539Sjchandra mtx_unlock(&sc->sc_mtx); 267233539Sjchandra return (error); 268233539Sjchandrai2c_stx_error: 269233539Sjchandra iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP); 270233539Sjchandra iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */ 271233539Sjchandra mtx_unlock(&sc->sc_mtx); 272233539Sjchandra return (error); 273233539Sjchandra} 274233539Sjchandra 275233539Sjchandrastatic int 276233539Sjchandraiicoc_stop(device_t dev) 277233539Sjchandra{ 278233539Sjchandra int error = 0; 279233539Sjchandra struct iicoc_softc *sc; 280233539Sjchandra 281233539Sjchandra sc = device_get_softc(dev); 282233539Sjchandra mtx_lock(&sc->sc_mtx); 283233539Sjchandra iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP); 284233539Sjchandra iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */ 285233539Sjchandra mtx_unlock(&sc->sc_mtx); 286233539Sjchandra return (error); 287233539Sjchandra 288233539Sjchandra} 289233539Sjchandra 290233539Sjchandrastatic int 291233539Sjchandraiicoc_write(device_t dev, const char *buf, int len, 292233539Sjchandra int *sent, int timeout /* us */ ) 293233539Sjchandra{ 294233539Sjchandra uint8_t value; 295233539Sjchandra int i; 296233539Sjchandra 297233539Sjchandra value = buf[0]; 298233539Sjchandra /* Write Slave Offset */ 299233539Sjchandra if (iicoc_wr_ack_cmd(dev, value, OC_COMMAND_WRITE)) { 300233539Sjchandra device_printf(dev, "I2C write slave offset failed.\n"); 301233539Sjchandra goto i2c_tx_error; 302233539Sjchandra } 303233539Sjchandra 304233539Sjchandra for (i = 1; i < len; i++) { 305233539Sjchandra /* Write data byte */ 306233539Sjchandra value = buf[i]; 307233539Sjchandra if (iicoc_wr_cmd(dev, value, OC_COMMAND_WRITE)) { 308233539Sjchandra device_printf(dev, "I2C write data byte %d failed.\n", 309233539Sjchandra i); 310233539Sjchandra goto i2c_tx_error; 311233539Sjchandra } 312233539Sjchandra } 313233539Sjchandra *sent = len; 314233539Sjchandra return (IIC_NOERR); 315233539Sjchandra 316233539Sjchandrai2c_tx_error: 317233539Sjchandra return (IIC_EBUSERR); 318233539Sjchandra} 319233539Sjchandra 320233539Sjchandrastatic int 321233539Sjchandraiicoc_read(device_t dev, char *buf, int len, int *read, int last, 322233539Sjchandra int delay) 323233539Sjchandra{ 324233539Sjchandra int data, i; 325233539Sjchandra uint8_t cmd; 326233539Sjchandra 327233539Sjchandra for (i = 0; i < len; i++) { 328233539Sjchandra /* Read data byte */ 329233539Sjchandra cmd = (i == len - 1) ? OC_COMMAND_RDNACK : OC_COMMAND_READ; 330233539Sjchandra data = iicoc_rd_cmd(dev, cmd); 331233539Sjchandra if (data < 0) { 332233539Sjchandra device_printf(dev, 333233539Sjchandra "I2C read data byte %d failed.\n", i); 334233539Sjchandra goto i2c_rx_error; 335233539Sjchandra } 336233539Sjchandra buf[i] = (uint8_t)data; 337233539Sjchandra } 338233539Sjchandra 339233539Sjchandra *read = len; 340233539Sjchandra return (IIC_NOERR); 341233539Sjchandra 342233539Sjchandrai2c_rx_error: 343233539Sjchandra return (IIC_EBUSERR); 344233539Sjchandra} 345233539Sjchandra 346233539Sjchandrastatic int 347233539Sjchandraiicoc_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr) 348233539Sjchandra{ 349233539Sjchandra int error; 350233539Sjchandra struct iicoc_softc *sc; 351233539Sjchandra 352233539Sjchandra sc = device_get_softc(dev); 353233539Sjchandra mtx_lock(&sc->sc_mtx); 354233539Sjchandra error = iicoc_init(dev); 355233539Sjchandra mtx_unlock(&sc->sc_mtx); 356233539Sjchandra return (error); 357233539Sjchandra} 358233539Sjchandra 359233539Sjchandrastatic int 360233539Sjchandraiicoc_repeated_start(device_t dev, u_char slave, int timeout) 361233539Sjchandra{ 362233539Sjchandra return 0; 363233539Sjchandra} 364233539Sjchandra 365233539Sjchandrastatic device_method_t iicoc_methods[] = { 366233539Sjchandra /* device interface */ 367233539Sjchandra DEVMETHOD(device_probe, iicoc_probe), 368233539Sjchandra DEVMETHOD(device_attach, iicoc_attach), 369233539Sjchandra DEVMETHOD(device_detach, iicoc_detach), 370233539Sjchandra 371233539Sjchandra /* iicbus interface */ 372233539Sjchandra DEVMETHOD(iicbus_callback, iicbus_null_callback), 373233539Sjchandra DEVMETHOD(iicbus_repeated_start, iicoc_repeated_start), 374233539Sjchandra DEVMETHOD(iicbus_start, iicoc_start), 375233539Sjchandra DEVMETHOD(iicbus_stop, iicoc_stop), 376233539Sjchandra DEVMETHOD(iicbus_reset, iicoc_reset), 377233539Sjchandra DEVMETHOD(iicbus_write, iicoc_write), 378233539Sjchandra DEVMETHOD(iicbus_read, iicoc_read), 379233539Sjchandra DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 380233539Sjchandra 381233539Sjchandra DEVMETHOD_END 382233539Sjchandra}; 383233539Sjchandra 384233539Sjchandrastatic driver_t iicoc_driver = { 385233539Sjchandra "iicoc", 386233539Sjchandra iicoc_methods, 387233539Sjchandra sizeof(struct iicoc_softc), 388233539Sjchandra}; 389233539Sjchandra 390233539SjchandraDRIVER_MODULE(iicoc, pci, iicoc_driver, iicoc_devclass, 0, 0); 391233539SjchandraDRIVER_MODULE(iicbus, iicoc, iicbus_driver, iicbus_devclass, 0, 0); 392