chrome_ec.c revision 263981
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 263981 2014-04-01 04:40:08Z 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 63263936Sbr/* TODO: export to DTS */ 64263936Sbr#define OUR_GPIO 177 65263936Sbr#define EC_GPIO 168 66263936Sbr 67263936Sbrstruct ec_softc { 68263936Sbr device_t dev; 69263936Sbr}; 70263936Sbr 71263936Sbrstruct ec_softc *ec_sc; 72263936Sbr 73263936Sbr/* 74263936Sbr * bus_claim, bus_release 75263936Sbr * both functions used for bus arbitration 76263936Sbr * in multi-master mode 77263936Sbr */ 78263936Sbr 79263936Sbrstatic int 80263936Sbrbus_claim(struct ec_softc *sc) 81263936Sbr{ 82263936Sbr device_t gpio_dev; 83263936Sbr int status; 84263936Sbr 85263936Sbr gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 86263936Sbr if (gpio_dev == NULL) { 87263936Sbr device_printf(sc->dev, "cant find gpio_dev\n"); 88263936Sbr return (1); 89263936Sbr } 90263936Sbr 91263936Sbr /* Say we want the bus */ 92263936Sbr GPIO_PIN_SET(gpio_dev, OUR_GPIO, GPIO_PIN_LOW); 93263936Sbr 94263936Sbr /* Check EC decision */ 95263936Sbr GPIO_PIN_GET(gpio_dev, EC_GPIO, &status); 96263936Sbr 97263936Sbr if (status == 1) { 98263936Sbr /* Okay. We have bus */ 99263936Sbr return (0); 100263936Sbr } 101263936Sbr 102263936Sbr /* EC is master */ 103263936Sbr return (-1); 104263936Sbr} 105263936Sbr 106263936Sbrstatic int 107263936Sbrbus_release(struct ec_softc *sc) 108263936Sbr{ 109263936Sbr device_t gpio_dev; 110263936Sbr 111263936Sbr gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 112263936Sbr if (gpio_dev == NULL) { 113263936Sbr device_printf(sc->dev, "cant find gpio_dev\n"); 114263936Sbr return (1); 115263936Sbr } 116263936Sbr 117263936Sbr GPIO_PIN_SET(gpio_dev, OUR_GPIO, GPIO_PIN_HIGH); 118263936Sbr 119263936Sbr return (0); 120263936Sbr} 121263936Sbr 122263936Sbrstatic int 123263936Sbrec_probe(device_t dev) 124263936Sbr{ 125263936Sbr 126263936Sbr device_set_desc(dev, "Chromebook Embedded Controller"); 127263936Sbr return (BUS_PROBE_DEFAULT); 128263936Sbr} 129263936Sbr 130263936Sbrstatic int 131263936Sbrfill_checksum(uint8_t *data_out, int len) 132263936Sbr{ 133263936Sbr int res; 134263936Sbr int i; 135263936Sbr 136263936Sbr res = 0; 137263936Sbr for (i = 0; i < len; i++) { 138263936Sbr res += data_out[i]; 139263936Sbr } 140263936Sbr 141263936Sbr data_out[len] = (res & 0xff); 142263936Sbr 143263936Sbr return (0); 144263936Sbr} 145263936Sbr 146263936Sbrint 147263936Sbrec_command(uint8_t cmd, uint8_t *dout, uint8_t dout_len, 148263936Sbr uint8_t *dinp, uint8_t dinp_len) 149263936Sbr{ 150263936Sbr struct ec_softc *sc; 151263936Sbr uint8_t *msg_dout; 152263936Sbr uint8_t *msg_dinp; 153263936Sbr int ret; 154263936Sbr int i; 155263936Sbr 156263936Sbr msg_dout = malloc(dout_len + 4, M_DEVBUF, M_NOWAIT); 157263936Sbr msg_dinp = malloc(dinp_len + 4, M_DEVBUF, M_NOWAIT); 158263936Sbr 159263936Sbr if (ec_sc == NULL) 160263936Sbr return (-1); 161263936Sbr 162263936Sbr sc = ec_sc; 163263936Sbr 164263936Sbr msg_dout[0] = EC_CMD_VERSION0; 165263936Sbr msg_dout[1] = cmd; 166263936Sbr msg_dout[2] = dout_len; 167263936Sbr 168263936Sbr for (i = 0; i < dout_len; i++) { 169263936Sbr msg_dout[i + 3] = dout[i]; 170263936Sbr }; 171263936Sbr 172263936Sbr fill_checksum(msg_dout, dout_len + 3); 173263936Sbr 174263936Sbr struct iic_msg msgs[] = { 175263936Sbr { 0x1e, IIC_M_WR, dout_len + 4, msg_dout, }, 176263936Sbr { 0x1e, IIC_M_RD, dinp_len + 4, msg_dinp, }, 177263936Sbr }; 178263936Sbr 179263936Sbr ret = iicbus_transfer(sc->dev, msgs, 2); 180263936Sbr if (ret != 0) { 181263936Sbr device_printf(sc->dev, "i2c transfer returned %d\n", ret); 182263936Sbr free(msg_dout, M_DEVBUF); 183263936Sbr free(msg_dinp, M_DEVBUF); 184263936Sbr return (-1); 185263936Sbr } 186263936Sbr 187263936Sbr for (i = 0; i < dinp_len; i++) { 188263936Sbr dinp[i] = msg_dinp[i + 3]; 189263936Sbr }; 190263936Sbr 191263936Sbr free(msg_dout, M_DEVBUF); 192263936Sbr free(msg_dinp, M_DEVBUF); 193263936Sbr return (0); 194263936Sbr} 195263936Sbr 196263936Sbrint ec_hello(void) 197263936Sbr{ 198263936Sbr uint8_t data_in[4]; 199263936Sbr uint8_t data_out[4]; 200263936Sbr 201263936Sbr data_in[0] = 0x40; 202263936Sbr data_in[1] = 0x30; 203263936Sbr data_in[2] = 0x20; 204263936Sbr data_in[3] = 0x10; 205263936Sbr 206263936Sbr ec_command(EC_CMD_MKBP_STATE, data_in, 4, 207263936Sbr data_out, 4); 208263936Sbr 209263936Sbr return (0); 210263936Sbr} 211263936Sbr 212263936Sbrstatic int 213263936Sbrec_attach(device_t dev) 214263936Sbr{ 215263936Sbr struct ec_softc *sc; 216263936Sbr 217263936Sbr sc = device_get_softc(dev); 218263936Sbr sc->dev = dev; 219263936Sbr 220263936Sbr ec_sc = sc; 221263936Sbr 222263936Sbr /* 223263936Sbr * Claim the bus. 224263936Sbr * 225263936Sbr * We don't know cases when EC is master, 226263936Sbr * so hold the bus forever for us. 227263936Sbr * 228263936Sbr */ 229263936Sbr 230263936Sbr if (bus_claim(sc) != 0) { 231263936Sbr return (ENXIO); 232263936Sbr } 233263936Sbr 234263936Sbr return (0); 235263936Sbr} 236263936Sbr 237263981Sbrstatic int 238263981Sbrec_detach(device_t dev) 239263981Sbr{ 240263981Sbr struct ec_softc *sc; 241263981Sbr 242263981Sbr sc = device_get_softc(dev); 243263981Sbr 244263981Sbr bus_release(sc); 245263981Sbr 246263981Sbr return (0); 247263981Sbr} 248263981Sbr 249263936Sbrstatic device_method_t ec_methods[] = { 250263936Sbr DEVMETHOD(device_probe, ec_probe), 251263936Sbr DEVMETHOD(device_attach, ec_attach), 252263981Sbr DEVMETHOD(device_detach, ec_detach), 253263936Sbr { 0, 0 } 254263936Sbr}; 255263936Sbr 256263936Sbrstatic driver_t ec_driver = { 257263936Sbr "chrome_ec", 258263936Sbr ec_methods, 259263936Sbr sizeof(struct ec_softc), 260263936Sbr}; 261263936Sbr 262263936Sbrstatic devclass_t ec_devclass; 263263936Sbr 264263936SbrDRIVER_MODULE(chrome_ec, iicbus, ec_driver, ec_devclass, 0, 0); 265263936SbrMODULE_VERSION(chrome_ec, 1); 266263936SbrMODULE_DEPEND(chrome_ec, iicbus, 1, 1, 1); 267