chrome_ec.c revision 263981
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 263981 2014-04-01 04:40:08Z 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 63/* TODO: export to DTS */ 64#define OUR_GPIO 177 65#define EC_GPIO 168 66 67struct ec_softc { 68 device_t dev; 69}; 70 71struct ec_softc *ec_sc; 72 73/* 74 * bus_claim, bus_release 75 * both functions used for bus arbitration 76 * in multi-master mode 77 */ 78 79static int 80bus_claim(struct ec_softc *sc) 81{ 82 device_t gpio_dev; 83 int status; 84 85 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 86 if (gpio_dev == NULL) { 87 device_printf(sc->dev, "cant find gpio_dev\n"); 88 return (1); 89 } 90 91 /* Say we want the bus */ 92 GPIO_PIN_SET(gpio_dev, OUR_GPIO, GPIO_PIN_LOW); 93 94 /* Check EC decision */ 95 GPIO_PIN_GET(gpio_dev, EC_GPIO, &status); 96 97 if (status == 1) { 98 /* Okay. We have bus */ 99 return (0); 100 } 101 102 /* EC is master */ 103 return (-1); 104} 105 106static int 107bus_release(struct ec_softc *sc) 108{ 109 device_t gpio_dev; 110 111 gpio_dev = devclass_get_device(devclass_find("gpio"), 0); 112 if (gpio_dev == NULL) { 113 device_printf(sc->dev, "cant find gpio_dev\n"); 114 return (1); 115 } 116 117 GPIO_PIN_SET(gpio_dev, OUR_GPIO, GPIO_PIN_HIGH); 118 119 return (0); 120} 121 122static int 123ec_probe(device_t dev) 124{ 125 126 device_set_desc(dev, "Chromebook Embedded Controller"); 127 return (BUS_PROBE_DEFAULT); 128} 129 130static int 131fill_checksum(uint8_t *data_out, int len) 132{ 133 int res; 134 int i; 135 136 res = 0; 137 for (i = 0; i < len; i++) { 138 res += data_out[i]; 139 } 140 141 data_out[len] = (res & 0xff); 142 143 return (0); 144} 145 146int 147ec_command(uint8_t cmd, uint8_t *dout, uint8_t dout_len, 148 uint8_t *dinp, uint8_t dinp_len) 149{ 150 struct ec_softc *sc; 151 uint8_t *msg_dout; 152 uint8_t *msg_dinp; 153 int ret; 154 int i; 155 156 msg_dout = malloc(dout_len + 4, M_DEVBUF, M_NOWAIT); 157 msg_dinp = malloc(dinp_len + 4, M_DEVBUF, M_NOWAIT); 158 159 if (ec_sc == NULL) 160 return (-1); 161 162 sc = ec_sc; 163 164 msg_dout[0] = EC_CMD_VERSION0; 165 msg_dout[1] = cmd; 166 msg_dout[2] = dout_len; 167 168 for (i = 0; i < dout_len; i++) { 169 msg_dout[i + 3] = dout[i]; 170 }; 171 172 fill_checksum(msg_dout, dout_len + 3); 173 174 struct iic_msg msgs[] = { 175 { 0x1e, IIC_M_WR, dout_len + 4, msg_dout, }, 176 { 0x1e, IIC_M_RD, dinp_len + 4, msg_dinp, }, 177 }; 178 179 ret = iicbus_transfer(sc->dev, msgs, 2); 180 if (ret != 0) { 181 device_printf(sc->dev, "i2c transfer returned %d\n", ret); 182 free(msg_dout, M_DEVBUF); 183 free(msg_dinp, M_DEVBUF); 184 return (-1); 185 } 186 187 for (i = 0; i < dinp_len; i++) { 188 dinp[i] = msg_dinp[i + 3]; 189 }; 190 191 free(msg_dout, M_DEVBUF); 192 free(msg_dinp, M_DEVBUF); 193 return (0); 194} 195 196int ec_hello(void) 197{ 198 uint8_t data_in[4]; 199 uint8_t data_out[4]; 200 201 data_in[0] = 0x40; 202 data_in[1] = 0x30; 203 data_in[2] = 0x20; 204 data_in[3] = 0x10; 205 206 ec_command(EC_CMD_MKBP_STATE, data_in, 4, 207 data_out, 4); 208 209 return (0); 210} 211 212static int 213ec_attach(device_t dev) 214{ 215 struct ec_softc *sc; 216 217 sc = device_get_softc(dev); 218 sc->dev = dev; 219 220 ec_sc = sc; 221 222 /* 223 * Claim the bus. 224 * 225 * We don't know cases when EC is master, 226 * so hold the bus forever for us. 227 * 228 */ 229 230 if (bus_claim(sc) != 0) { 231 return (ENXIO); 232 } 233 234 return (0); 235} 236 237static int 238ec_detach(device_t dev) 239{ 240 struct ec_softc *sc; 241 242 sc = device_get_softc(dev); 243 244 bus_release(sc); 245 246 return (0); 247} 248 249static device_method_t ec_methods[] = { 250 DEVMETHOD(device_probe, ec_probe), 251 DEVMETHOD(device_attach, ec_attach), 252 DEVMETHOD(device_detach, ec_detach), 253 { 0, 0 } 254}; 255 256static driver_t ec_driver = { 257 "chrome_ec", 258 ec_methods, 259 sizeof(struct ec_softc), 260}; 261 262static devclass_t ec_devclass; 263 264DRIVER_MODULE(chrome_ec, iicbus, ec_driver, ec_devclass, 0, 0); 265MODULE_VERSION(chrome_ec, 1); 266MODULE_DEPEND(chrome_ec, iicbus, 1, 1, 1); 267