chrome_ec.c revision 266945
1/*- 2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPREC OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNEC FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINEC INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Samsung Chromebook Embedded Controller 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/arm/samsung/exynos/chrome_ec.c 266945 2014-06-01 08:45:27Z br $"); 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/bus.h> 37#include <sys/kernel.h> 38#include <sys/module.h> 39#include <sys/malloc.h> 40#include <sys/rman.h> 41#include <sys/timeet.h> 42#include <sys/timetc.h> 43#include <sys/watchdog.h> 44#include <sys/gpio.h> 45 46#include <dev/fdt/fdt_common.h> 47#include <dev/ofw/openfirm.h> 48#include <dev/ofw/ofw_bus.h> 49#include <dev/ofw/ofw_bus_subr.h> 50 51#include <machine/bus.h> 52#include <machine/fdt.h> 53#include <machine/cpu.h> 54#include <machine/intr.h> 55 56#include <dev/iicbus/iiconf.h> 57 58#include "iicbus_if.h" 59#include "gpio_if.h" 60 61#include <arm/samsung/exynos/chrome_ec.h> 62 63struct ec_softc { 64 device_t dev; 65 int have_arbitrator; 66 pcell_t our_gpio; 67 pcell_t ec_gpio; 68}; 69 70struct ec_softc *ec_sc; 71 72/* 73 * bus_claim, bus_release 74 * both functions used for bus arbitration 75 * in multi-master mode 76 */ 77 78static int 79bus_claim(struct ec_softc *sc) 80{ 81 device_t gpio_dev; 82 int status; 83 84 if (sc->our_gpio == 0 || sc->ec_gpio == 0) { 85 device_printf(sc->dev, "i2c arbitrator is not configured\n"); 86 return (1); 87 } 88 89 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 90 if (gpio_dev == NULL) { 91 device_printf(sc->dev, "cant find gpio_dev\n"); 92 return (1); 93 } 94 95 /* Say we want the bus */ 96 GPIO_PIN_SET(gpio_dev, sc->our_gpio, GPIO_PIN_LOW); 97 98 /* TODO(imax): insert a delay to allow EC to react. */ 99 100 /* Check EC decision */ 101 GPIO_PIN_GET(gpio_dev, sc->ec_gpio, &status); 102 103 if (status == 1) { 104 /* Okay. We have bus */ 105 return (0); 106 } 107 108 /* EC is master */ 109 return (-1); 110} 111 112static int 113bus_release(struct ec_softc *sc) 114{ 115 device_t gpio_dev; 116 117 if (sc->our_gpio == 0 || sc->ec_gpio == 0) { 118 device_printf(sc->dev, "i2c arbitrator is not configured\n"); 119 return (1); 120 } 121 122 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 123 if (gpio_dev == NULL) { 124 device_printf(sc->dev, "cant find gpio_dev\n"); 125 return (1); 126 } 127 128 GPIO_PIN_SET(gpio_dev, sc->our_gpio, GPIO_PIN_HIGH); 129 130 return (0); 131} 132 133static int 134ec_probe(device_t dev) 135{ 136 137 device_set_desc(dev, "Chromebook Embedded Controller"); 138 return (BUS_PROBE_DEFAULT); 139} 140 141static int 142fill_checksum(uint8_t *data_out, int len) 143{ 144 int res; 145 int i; 146 147 res = 0; 148 for (i = 0; i < len; i++) { 149 res += data_out[i]; 150 } 151 152 data_out[len] = (res & 0xff); 153 154 return (0); 155} 156 157int 158ec_command(uint8_t cmd, uint8_t *dout, uint8_t dout_len, 159 uint8_t *dinp, uint8_t dinp_len) 160{ 161 struct ec_softc *sc; 162 uint8_t *msg_dout; 163 uint8_t *msg_dinp; 164 int ret; 165 int i; 166 167 msg_dout = malloc(dout_len + 4, M_DEVBUF, M_NOWAIT); 168 msg_dinp = malloc(dinp_len + 3, M_DEVBUF, M_NOWAIT); 169 170 if (ec_sc == NULL) 171 return (-1); 172 173 sc = ec_sc; 174 175 msg_dout[0] = EC_CMD_VERSION0; 176 msg_dout[1] = cmd; 177 msg_dout[2] = dout_len; 178 179 for (i = 0; i < dout_len; i++) { 180 msg_dout[i + 3] = dout[i]; 181 }; 182 183 fill_checksum(msg_dout, dout_len + 3); 184 185 struct iic_msg msgs[] = { 186 { 0x1e, IIC_M_WR, dout_len + 4, msg_dout, }, 187 { 0x1e, IIC_M_RD, dinp_len + 3, msg_dinp, }, 188 }; 189 190 ret = iicbus_transfer(sc->dev, msgs, 2); 191 if (ret != 0) { 192 device_printf(sc->dev, "i2c transfer returned %d\n", ret); 193 free(msg_dout, M_DEVBUF); 194 free(msg_dinp, M_DEVBUF); 195 return (-1); 196 } 197 198 for (i = 0; i < dinp_len; i++) { 199 dinp[i] = msg_dinp[i + 2]; 200 }; 201 202 free(msg_dout, M_DEVBUF); 203 free(msg_dinp, M_DEVBUF); 204 return (0); 205} 206 207int ec_hello(void) 208{ 209 uint8_t data_in[4]; 210 uint8_t data_out[4]; 211 212 data_in[0] = 0x40; 213 data_in[1] = 0x30; 214 data_in[2] = 0x20; 215 data_in[3] = 0x10; 216 217 ec_command(EC_CMD_MKBP_STATE, data_in, 4, 218 data_out, 4); 219 220 return (0); 221} 222 223static void 224configure_i2c_arbitrator(struct ec_softc *sc) 225{ 226 phandle_t arbitrator; 227 228 /* TODO(imax): look for compatible entry instead of hard-coded path */ 229 arbitrator = OF_finddevice("/i2c-arbitrator"); 230 if (arbitrator > 0 && 231 OF_hasprop(arbitrator, "freebsd,our-gpio") && 232 OF_hasprop(arbitrator, "freebsd,ec-gpio")) { 233 sc->have_arbitrator = 1; 234 OF_getencprop(arbitrator, "freebsd,our-gpio", 235 &sc->our_gpio, sizeof(sc->our_gpio)); 236 OF_getencprop(arbitrator, "freebsd,ec-gpio", 237 &sc->ec_gpio, sizeof(sc->ec_gpio)); 238 } else { 239 sc->have_arbitrator = 0; 240 sc->our_gpio = 0; 241 sc->ec_gpio = 0; 242 } 243} 244 245static int 246ec_attach(device_t dev) 247{ 248 struct ec_softc *sc; 249 250 sc = device_get_softc(dev); 251 sc->dev = dev; 252 253 ec_sc = sc; 254 255 configure_i2c_arbitrator(sc); 256 257 /* 258 * Claim the bus. 259 * 260 * We don't know cases when EC is master, 261 * so hold the bus forever for us. 262 * 263 */ 264 265 if (sc->have_arbitrator && bus_claim(sc) != 0) { 266 return (ENXIO); 267 } 268 269 return (0); 270} 271 272static int 273ec_detach(device_t dev) 274{ 275 struct ec_softc *sc; 276 277 sc = device_get_softc(dev); 278 279 if (sc->have_arbitrator) { 280 bus_release(sc); 281 } 282 283 return (0); 284} 285 286static device_method_t ec_methods[] = { 287 DEVMETHOD(device_probe, ec_probe), 288 DEVMETHOD(device_attach, ec_attach), 289 DEVMETHOD(device_detach, ec_detach), 290 { 0, 0 } 291}; 292 293static driver_t ec_driver = { 294 "chrome_ec", 295 ec_methods, 296 sizeof(struct ec_softc), 297}; 298 299static devclass_t ec_devclass; 300 301DRIVER_MODULE(chrome_ec, iicbus, ec_driver, ec_devclass, 0, 0); 302MODULE_VERSION(chrome_ec, 1); 303MODULE_DEPEND(chrome_ec, iicbus, 1, 1, 1); 304