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 (EC)
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
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/ofw/openfirm.h>
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.h>
49
50#include <machine/bus.h>
51#include <machine/cpu.h>
52#include <machine/intr.h>
53
54#include <dev/spibus/spi.h>
55#include <dev/spibus/spibusvar.h>
56
57#include "spibus_if.h"
58#include "gpio_if.h"
59
60#include <arm/samsung/exynos/chrome_ec.h>
61
62struct ec_softc {
63	device_t	dev;
64	device_t	dev_gpio;
65};
66
67struct ec_softc *ec_sc;
68
69#define EC_SPI_CS	200
70
71static int
72assert_cs(struct ec_softc *sc, int enable)
73{
74	/* Get the GPIO device */
75	sc->dev_gpio = devclass_get_device(devclass_find("gpio"), 0);
76	if (sc->dev_gpio == NULL) {
77		device_printf(sc->dev, "Error: failed to get the GPIO dev\n");
78		return (1);
79	}
80
81	GPIO_PIN_SETFLAGS(sc->dev_gpio, EC_SPI_CS, GPIO_PIN_OUTPUT);
82
83	if (enable) {
84		GPIO_PIN_SET(sc->dev_gpio, EC_SPI_CS, GPIO_PIN_LOW);
85	} else {
86		GPIO_PIN_SET(sc->dev_gpio, EC_SPI_CS, GPIO_PIN_HIGH);
87	}
88
89	return (0);
90}
91
92static int
93ec_probe(device_t dev)
94{
95
96	device_set_desc(dev, "Chromebook Embedded Controller");
97	return (BUS_PROBE_DEFAULT);
98}
99
100static int
101fill_checksum(uint8_t *data_out, int len)
102{
103	int res;
104	int i;
105
106	res = 0;
107	for (i = 0; i < len; i++) {
108		res += data_out[i];
109	}
110
111	data_out[len] = (res & 0xff);
112
113	return (0);
114}
115
116int
117ec_command(uint8_t cmd, uint8_t *dout, uint8_t dout_len,
118    uint8_t *dinp, uint8_t dinp_len)
119{
120	struct spi_command spi_cmd;
121	struct ec_softc *sc;
122	uint8_t *msg_dout;
123	uint8_t *msg_dinp;
124	int ret;
125	int i;
126
127	memset(&spi_cmd, 0, sizeof(spi_cmd));
128
129	msg_dout = malloc(dout_len + 4, M_DEVBUF, M_NOWAIT | M_ZERO);
130	msg_dinp = malloc(dinp_len + 4, M_DEVBUF, M_NOWAIT | M_ZERO);
131
132	spi_cmd.tx_cmd = msg_dout;
133	spi_cmd.rx_cmd = msg_dinp;
134
135	if (ec_sc == NULL)
136		return (-1);
137
138	sc = ec_sc;
139
140	msg_dout[0] = EC_CMD_VERSION0;
141	msg_dout[1] = cmd;
142	msg_dout[2] = dout_len;
143
144	for (i = 0; i < dout_len; i++) {
145		msg_dout[i + 3] = dout[i];
146	}
147
148	fill_checksum(msg_dout, dout_len + 3);
149
150	assert_cs(sc, 1);
151	spi_cmd.rx_cmd_sz = spi_cmd.tx_cmd_sz = dout_len + 4;
152	ret = SPIBUS_TRANSFER(device_get_parent(sc->dev), sc->dev, &spi_cmd);
153
154	/* Wait 0xec */
155	for (i = 0; i < 1000; i++) {
156		DELAY(10);
157		msg_dout[0] = 0xff;
158		spi_cmd.rx_cmd_sz = spi_cmd.tx_cmd_sz = 1;
159		SPIBUS_TRANSFER(device_get_parent(sc->dev), sc->dev, &spi_cmd);
160		if (msg_dinp[0] == 0xec)
161			break;
162	}
163
164	/* Get the rest */
165	for (i = 0; i < (dout_len + 4); i++)
166		msg_dout[i] = 0xff;
167	spi_cmd.rx_cmd_sz = spi_cmd.tx_cmd_sz = dout_len + 4 - 1;
168	ret = SPIBUS_TRANSFER(device_get_parent(sc->dev), sc->dev, &spi_cmd);
169	assert_cs(sc, 0);
170
171	if (ret != 0) {
172		device_printf(sc->dev, "spibus_transfer returned %d\n", ret);
173		free(msg_dout, M_DEVBUF);
174		free(msg_dinp, M_DEVBUF);
175		return (-1);
176	}
177
178	for (i = 0; i < dinp_len; i++) {
179		dinp[i] = msg_dinp[i + 2];
180	}
181
182	free(msg_dout, M_DEVBUF);
183	free(msg_dinp, M_DEVBUF);
184
185	return (0);
186}
187
188static int
189ec_attach(device_t dev)
190{
191	struct ec_softc *sc;
192
193	sc = device_get_softc(dev);
194	sc->dev = dev;
195
196	ec_sc = sc;
197
198	return (0);
199}
200
201static int
202ec_detach(device_t dev)
203{
204	struct ec_softc *sc;
205
206	sc = device_get_softc(dev);
207
208	return (0);
209}
210
211static device_method_t ec_methods[] = {
212	DEVMETHOD(device_probe,		ec_probe),
213	DEVMETHOD(device_attach,	ec_attach),
214	DEVMETHOD(device_detach,	ec_detach),
215	{ 0, 0 }
216};
217
218static driver_t ec_driver = {
219	"chrome_ec",
220	ec_methods,
221	sizeof(struct ec_softc),
222};
223
224static devclass_t ec_devclass;
225
226DRIVER_MODULE(chrome_ec, spibus, ec_driver, ec_devclass, 0, 0);
227MODULE_VERSION(chrome_ec, 1);
228MODULE_DEPEND(chrome_ec, spibus, 1, 1, 1);
229