2 * Copyright (c) 2020 Justin Hibbits
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
41 #include <machine/bus.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
49 * A driver for the AmigaOne X5000 "Cyrus+" CPLD.
51 * This is the interface between the CPU and the "Xena" (XMOS) chip. Since the
52 * XMOS is programmable via a SPI-attached flash memory, there's no direct
53 * driver written for the Xena attachment. Instead, a userspace process would
54 * communicate with the Xena by issuing ioctl()s to this CPLD.
57 /* Resource access addresses. */
58 #define CPLD_MEM_ADDR 0x0000
59 #define CPLD_MEM_DATA 0x8000
61 #define CPLD_MAX_DRAM_WORDS 0x800
64 #define CPLD_REG_SIG1 0x00
65 #define CPLD_REG_SIG2 0x01
66 #define CPLD_REG_HWREV 0x02
67 #define CPLD_REG_MBC2X 0x05
68 #define CPLD_REG_MBX2C 0x06
69 #define CPLD_REG_XDEBUG 0x0c
70 #define CPLD_REG_XJTAG 0x0d
71 #define CPLD_REG_FAN_TACHO 0x10
72 #define CPLD_REG_DATE_LW 0x21
73 #define CPLD_REG_DATE_UW 0x22
74 #define CPLD_REG_TIME_LW 0x23
75 #define CPLD_REG_TIME_UW 0x24
76 #define CPLD_REG_SCR1 0x30
77 #define CPLD_REG_SCR2 0x31
78 #define CPLD_REG_RAM 0x8000
82 struct resource *sc_mem;
88 static d_open_t cpld_open;
89 static d_close_t cpld_close;
90 static d_ioctl_t cpld_ioctl;
92 static struct cdevsw cpld_cdevsw = {
93 .d_version = D_VERSION,
95 .d_close = cpld_close,
96 .d_ioctl = cpld_ioctl,
100 static device_probe_t cpld_probe;
101 static device_attach_t cpld_attach;
102 static int cpld_fan_sysctl(SYSCTL_HANDLER_ARGS);
104 static device_method_t cpld_methods[] = {
105 DEVMETHOD(device_probe, cpld_probe),
106 DEVMETHOD(device_attach, cpld_attach),
111 static driver_t cpld_driver = {
114 sizeof(struct cpld_softc)
117 static devclass_t cpld_devclass;
118 DRIVER_MODULE(cpld, lbc, cpld_driver, cpld_devclass, 0, 0);
121 cpld_write(struct cpld_softc *sc, int addr, int data)
123 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr);
124 bus_write_2(sc->sc_mem, CPLD_MEM_DATA, data);
128 cpld_read(struct cpld_softc *sc, int addr)
130 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr);
132 return (bus_read_2(sc->sc_mem, CPLD_MEM_DATA));
136 cpld_probe(device_t dev)
138 if (!ofw_bus_is_compatible(dev, "aeon,cyrus-cpld"))
141 device_set_desc(dev, "AmigaOne Cyrus CPLD");
143 return (BUS_PROBE_GENERIC);
147 cpld_attach(device_t dev)
149 struct make_dev_args mda;
150 struct cpld_softc *sc;
154 struct sysctl_ctx_list *ctx;
155 struct sysctl_oid *tree;
157 sc = device_get_softc(dev);
161 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
162 RF_ACTIVE|RF_SHAREABLE);
163 if (sc->sc_mem == NULL) {
164 device_printf(dev, "Unable to allocate memory resource.\n");
167 mtx_init(&sc->sc_mutex, "cpld", NULL, MTX_DEF);
169 date = (cpld_read(sc, CPLD_REG_DATE_UW) << 16) |
170 cpld_read(sc, CPLD_REG_DATE_LW);
171 time = (cpld_read(sc, CPLD_REG_TIME_UW) << 16) |
172 cpld_read(sc, CPLD_REG_TIME_LW);
174 device_printf(dev, "Build date: %04x-%02x-%02x\n",
175 (date >> 16) & 0xffff, (date >> 8) & 0xff, date & 0xff);
176 device_printf(dev, "Build time: %02x:%02x:%02x\n",
177 (time >> 16) & 0xff, (time >> 8) & 0xff, time & 0xff);
180 tmp = cpld_read(sc, CPLD_REG_HWREV);
181 device_printf(dev, "Hardware revision: %d\n", tmp);
183 ctx = device_get_sysctl_ctx(dev);
184 tree = device_get_sysctl_tree(dev);
186 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
187 "cpu_fan", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
188 cpld_fan_sysctl, "I", "CPU Fan speed in RPM");
190 make_dev_args_init(&mda);
191 mda.mda_flags = MAKEDEV_CHECKNAME;
192 mda.mda_devsw = &cpld_cdevsw;
193 mda.mda_uid = UID_ROOT;
194 mda.mda_gid = GID_WHEEL;
196 mda.mda_si_drv1 = sc;
197 err = make_dev_s(&mda, &sc->sc_cdev, "cpld");
199 device_printf(dev, "Error creating character device: %d\n", err);
200 device_printf(dev, "Only sysctl interfaces will be available.\n");
207 cpld_fan_sysctl(SYSCTL_HANDLER_ARGS)
209 struct cpld_softc *sc;
213 mtx_lock(&sc->sc_mutex);
214 /* Read until we get some level of read stability. */
215 rpm = cpld_read(sc, CPLD_REG_FAN_TACHO);
218 rpm = cpld_read(sc, CPLD_REG_FAN_TACHO);
219 } while (abs(rpm - old) > 10);
220 mtx_unlock(&sc->sc_mutex);
222 /* Convert RPS->RPM. */
224 error = sysctl_handle_int(oidp, &rpm, 0, req);
230 cpld_open(struct cdev *dev, int flags, int fmt, struct thread *td)
232 struct cpld_softc *sc = dev->si_drv1;
241 cpld_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
243 struct cpld_softc *sc = dev->si_drv1;
250 cpld_send(device_t dev, struct cpld_cmd_data *d)
252 struct cpld_softc *sc;
256 if (d->cmd > USHRT_MAX)
259 sc = device_get_softc(dev);
261 mtx_lock(&sc->sc_mutex);
262 for (i = 0, word = d->words; i < d->len; i++, word++) {
264 cpld_write(sc, CPLD_REG_RAM, *word);
266 bus_write_4(sc->sc_mem, CPLD_MEM_DATA, *word);
269 cpld_write(sc, CPLD_REG_MBC2X, d->cmd);
270 mtx_unlock(&sc->sc_mutex);
276 cpld_recv(device_t dev, struct cpld_cmd_data *d)
278 struct cpld_softc *sc;
282 sc = device_get_softc(dev);
284 mtx_lock(&sc->sc_mutex);
285 d->cmd = cpld_read(sc, CPLD_REG_MBX2C);
287 for (i = 0, word = d->words; i < d->len; i++, word++) {
289 *word = cpld_read(sc, CPLD_REG_RAM);
291 *word = bus_read_4(sc->sc_mem, CPLD_MEM_DATA);
293 mtx_unlock(&sc->sc_mutex);
299 cpld_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
301 struct cpld_softc *sc;
302 struct cpld_cmd_data *d;
303 void *xfer_data, *tmp;
309 d = (struct cpld_cmd_data *)data;
310 if (d->len + d->offset > CPLD_MAX_DRAM_WORDS) {
313 xfer_data = malloc(d->len * sizeof(uint16_t), M_TEMP, M_WAITOK);
317 err = copyin(d->words, xfer_data, d->len * sizeof(uint16_t));
318 d->words = xfer_data;
320 err = cpld_send(sc->sc_dev, d);
324 d->words = xfer_data;
325 err = cpld_recv(sc->sc_dev, d);
328 err = copyout(xfer_data, d->words,
329 d->len * sizeof(uint16_t));
335 free(xfer_data, M_TEMP);