chrome_ec.c revision 269369
1263936Sbr/*- 2263936Sbr * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com> 3263936Sbr * All rights reserved. 4263936Sbr * 5263936Sbr * Redistribution and use in source and binary forms, with or without 6263936Sbr * modification, are permitted provided that the following conditions 7263936Sbr * are met: 8263936Sbr * 1. Redistributions of source code must retain the above copyright 9263936Sbr * notice, this list of conditions and the following disclaimer. 10263936Sbr * 2. Redistributions in binary form must reproduce the above copyright 11263936Sbr * notice, this list of conditions and the following disclaimer in the 12263936Sbr * documentation and/or other materials provided with the distribution. 13263936Sbr * 14263936Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15263936Sbr * ANY EXPREC OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16263936Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNEC FOR A PARTICULAR PURPOSE 17263936Sbr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18263936Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19263936Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20263936Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINEC INTERRUPTION) 21263936Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22263936Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23263936Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24263936Sbr * SUCH DAMAGE. 25263936Sbr */ 26263936Sbr 27263936Sbr/* 28263936Sbr * Samsung Chromebook Embedded Controller 29263936Sbr */ 30263936Sbr 31263936Sbr#include <sys/cdefs.h> 32263936Sbr__FBSDID("$FreeBSD: head/sys/arm/samsung/exynos/chrome_ec.c 269369 2014-08-01 06:20:25Z br $"); 33263936Sbr 34263936Sbr#include <sys/param.h> 35263936Sbr#include <sys/systm.h> 36263936Sbr#include <sys/bus.h> 37263936Sbr#include <sys/kernel.h> 38263936Sbr#include <sys/module.h> 39263936Sbr#include <sys/malloc.h> 40263936Sbr#include <sys/rman.h> 41263936Sbr#include <sys/timeet.h> 42263936Sbr#include <sys/timetc.h> 43263936Sbr#include <sys/watchdog.h> 44263936Sbr#include <sys/gpio.h> 45263936Sbr 46263936Sbr#include <dev/fdt/fdt_common.h> 47263936Sbr#include <dev/ofw/openfirm.h> 48263936Sbr#include <dev/ofw/ofw_bus.h> 49263936Sbr#include <dev/ofw/ofw_bus_subr.h> 50263936Sbr 51263936Sbr#include <machine/bus.h> 52263936Sbr#include <machine/fdt.h> 53263936Sbr#include <machine/cpu.h> 54263936Sbr#include <machine/intr.h> 55263936Sbr 56263936Sbr#include <dev/iicbus/iiconf.h> 57263936Sbr 58263936Sbr#include "iicbus_if.h" 59263936Sbr#include "gpio_if.h" 60263936Sbr 61263936Sbr#include <arm/samsung/exynos/chrome_ec.h> 62263936Sbr 63263936Sbrstruct ec_softc { 64263936Sbr device_t dev; 65266785Sbr int have_arbitrator; 66266785Sbr pcell_t our_gpio; 67266785Sbr pcell_t ec_gpio; 68263936Sbr}; 69263936Sbr 70263936Sbrstruct ec_softc *ec_sc; 71263936Sbr 72263936Sbr/* 73263936Sbr * bus_claim, bus_release 74263936Sbr * both functions used for bus arbitration 75263936Sbr * in multi-master mode 76263936Sbr */ 77263936Sbr 78263936Sbrstatic int 79263936Sbrbus_claim(struct ec_softc *sc) 80263936Sbr{ 81263936Sbr device_t gpio_dev; 82263936Sbr int status; 83263936Sbr 84266785Sbr if (sc->our_gpio == 0 || sc->ec_gpio == 0) { 85266785Sbr device_printf(sc->dev, "i2c arbitrator is not configured\n"); 86266785Sbr return (1); 87266785Sbr } 88266785Sbr 89263936Sbr gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 90266785Sbr if (gpio_dev == NULL) { 91263936Sbr device_printf(sc->dev, "cant find gpio_dev\n"); 92263936Sbr return (1); 93263936Sbr } 94263936Sbr 95263936Sbr /* Say we want the bus */ 96266785Sbr GPIO_PIN_SET(gpio_dev, sc->our_gpio, GPIO_PIN_LOW); 97263936Sbr 98269369Sbr /* TODO: insert a delay to allow EC to react. */ 99266785Sbr 100263936Sbr /* Check EC decision */ 101266785Sbr GPIO_PIN_GET(gpio_dev, sc->ec_gpio, &status); 102263936Sbr 103263936Sbr if (status == 1) { 104263936Sbr /* Okay. We have bus */ 105263936Sbr return (0); 106263936Sbr } 107263936Sbr 108263936Sbr /* EC is master */ 109263936Sbr return (-1); 110263936Sbr} 111263936Sbr 112263936Sbrstatic int 113263936Sbrbus_release(struct ec_softc *sc) 114263936Sbr{ 115263936Sbr device_t gpio_dev; 116263936Sbr 117266785Sbr if (sc->our_gpio == 0 || sc->ec_gpio == 0) { 118266785Sbr device_printf(sc->dev, "i2c arbitrator is not configured\n"); 119266785Sbr return (1); 120266785Sbr } 121266785Sbr 122263936Sbr gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 123266785Sbr if (gpio_dev == NULL) { 124263936Sbr device_printf(sc->dev, "cant find gpio_dev\n"); 125263936Sbr return (1); 126263936Sbr } 127263936Sbr 128266785Sbr GPIO_PIN_SET(gpio_dev, sc->our_gpio, GPIO_PIN_HIGH); 129263936Sbr 130263936Sbr return (0); 131263936Sbr} 132263936Sbr 133263936Sbrstatic int 134263936Sbrec_probe(device_t dev) 135263936Sbr{ 136263936Sbr 137263936Sbr device_set_desc(dev, "Chromebook Embedded Controller"); 138263936Sbr return (BUS_PROBE_DEFAULT); 139263936Sbr} 140263936Sbr 141263936Sbrstatic int 142263936Sbrfill_checksum(uint8_t *data_out, int len) 143263936Sbr{ 144263936Sbr int res; 145263936Sbr int i; 146263936Sbr 147263936Sbr res = 0; 148263936Sbr for (i = 0; i < len; i++) { 149263936Sbr res += data_out[i]; 150263936Sbr } 151263936Sbr 152263936Sbr data_out[len] = (res & 0xff); 153263936Sbr 154263936Sbr return (0); 155263936Sbr} 156263936Sbr 157263936Sbrint 158263936Sbrec_command(uint8_t cmd, uint8_t *dout, uint8_t dout_len, 159263936Sbr uint8_t *dinp, uint8_t dinp_len) 160263936Sbr{ 161263936Sbr struct ec_softc *sc; 162263936Sbr uint8_t *msg_dout; 163263936Sbr uint8_t *msg_dinp; 164263936Sbr int ret; 165263936Sbr int i; 166263936Sbr 167263936Sbr msg_dout = malloc(dout_len + 4, M_DEVBUF, M_NOWAIT); 168266945Sbr msg_dinp = malloc(dinp_len + 3, M_DEVBUF, M_NOWAIT); 169263936Sbr 170263936Sbr if (ec_sc == NULL) 171263936Sbr return (-1); 172263936Sbr 173263936Sbr sc = ec_sc; 174263936Sbr 175263936Sbr msg_dout[0] = EC_CMD_VERSION0; 176263936Sbr msg_dout[1] = cmd; 177263936Sbr msg_dout[2] = dout_len; 178263936Sbr 179263936Sbr for (i = 0; i < dout_len; i++) { 180263936Sbr msg_dout[i + 3] = dout[i]; 181263936Sbr }; 182263936Sbr 183263936Sbr fill_checksum(msg_dout, dout_len + 3); 184263936Sbr 185263936Sbr struct iic_msg msgs[] = { 186263936Sbr { 0x1e, IIC_M_WR, dout_len + 4, msg_dout, }, 187266945Sbr { 0x1e, IIC_M_RD, dinp_len + 3, msg_dinp, }, 188263936Sbr }; 189263936Sbr 190263936Sbr ret = iicbus_transfer(sc->dev, msgs, 2); 191263936Sbr if (ret != 0) { 192263936Sbr device_printf(sc->dev, "i2c transfer returned %d\n", ret); 193263936Sbr free(msg_dout, M_DEVBUF); 194263936Sbr free(msg_dinp, M_DEVBUF); 195263936Sbr return (-1); 196263936Sbr } 197263936Sbr 198263936Sbr for (i = 0; i < dinp_len; i++) { 199266945Sbr dinp[i] = msg_dinp[i + 2]; 200263936Sbr }; 201263936Sbr 202263936Sbr free(msg_dout, M_DEVBUF); 203263936Sbr free(msg_dinp, M_DEVBUF); 204263936Sbr return (0); 205263936Sbr} 206263936Sbr 207263936Sbrint ec_hello(void) 208263936Sbr{ 209263936Sbr uint8_t data_in[4]; 210263936Sbr uint8_t data_out[4]; 211263936Sbr 212263936Sbr data_in[0] = 0x40; 213263936Sbr data_in[1] = 0x30; 214263936Sbr data_in[2] = 0x20; 215263936Sbr data_in[3] = 0x10; 216263936Sbr 217269369Sbr ec_command(EC_CMD_HELLO, data_in, 4, 218263936Sbr data_out, 4); 219263936Sbr 220263936Sbr return (0); 221263936Sbr} 222263936Sbr 223266785Sbrstatic void 224266785Sbrconfigure_i2c_arbitrator(struct ec_softc *sc) 225266785Sbr{ 226266785Sbr phandle_t arbitrator; 227266785Sbr 228269369Sbr /* TODO: look for compatible entry instead of hard-coded path */ 229266785Sbr arbitrator = OF_finddevice("/i2c-arbitrator"); 230266785Sbr if (arbitrator > 0 && 231266785Sbr OF_hasprop(arbitrator, "freebsd,our-gpio") && 232266785Sbr OF_hasprop(arbitrator, "freebsd,ec-gpio")) { 233266785Sbr sc->have_arbitrator = 1; 234266785Sbr OF_getencprop(arbitrator, "freebsd,our-gpio", 235266785Sbr &sc->our_gpio, sizeof(sc->our_gpio)); 236266785Sbr OF_getencprop(arbitrator, "freebsd,ec-gpio", 237266785Sbr &sc->ec_gpio, sizeof(sc->ec_gpio)); 238266785Sbr } else { 239266785Sbr sc->have_arbitrator = 0; 240266785Sbr sc->our_gpio = 0; 241266785Sbr sc->ec_gpio = 0; 242266785Sbr } 243266785Sbr} 244266785Sbr 245263936Sbrstatic int 246263936Sbrec_attach(device_t dev) 247263936Sbr{ 248263936Sbr struct ec_softc *sc; 249263936Sbr 250263936Sbr sc = device_get_softc(dev); 251263936Sbr sc->dev = dev; 252263936Sbr 253263936Sbr ec_sc = sc; 254263936Sbr 255266785Sbr configure_i2c_arbitrator(sc); 256266785Sbr 257263936Sbr /* 258263936Sbr * Claim the bus. 259263936Sbr * 260263936Sbr * We don't know cases when EC is master, 261263936Sbr * so hold the bus forever for us. 262263936Sbr * 263263936Sbr */ 264263936Sbr 265266785Sbr if (sc->have_arbitrator && bus_claim(sc) != 0) { 266263936Sbr return (ENXIO); 267263936Sbr } 268263936Sbr 269263936Sbr return (0); 270263936Sbr} 271263936Sbr 272263981Sbrstatic int 273263981Sbrec_detach(device_t dev) 274263981Sbr{ 275263981Sbr struct ec_softc *sc; 276263981Sbr 277263981Sbr sc = device_get_softc(dev); 278263981Sbr 279266785Sbr if (sc->have_arbitrator) { 280266785Sbr bus_release(sc); 281266785Sbr } 282263981Sbr 283263981Sbr return (0); 284263981Sbr} 285263981Sbr 286263936Sbrstatic device_method_t ec_methods[] = { 287263936Sbr DEVMETHOD(device_probe, ec_probe), 288263936Sbr DEVMETHOD(device_attach, ec_attach), 289263981Sbr DEVMETHOD(device_detach, ec_detach), 290263936Sbr { 0, 0 } 291263936Sbr}; 292263936Sbr 293263936Sbrstatic driver_t ec_driver = { 294263936Sbr "chrome_ec", 295263936Sbr ec_methods, 296263936Sbr sizeof(struct ec_softc), 297263936Sbr}; 298263936Sbr 299263936Sbrstatic devclass_t ec_devclass; 300263936Sbr 301263936SbrDRIVER_MODULE(chrome_ec, iicbus, ec_driver, ec_devclass, 0, 0); 302263936SbrMODULE_VERSION(chrome_ec, 1); 303263936SbrMODULE_DEPEND(chrome_ec, iicbus, 1, 1, 1); 304