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>
51 * A driver for the AmigaOne A1222 "Tabor" Main CPLD.
53 * The main CPLD is the interface between the CPU and the GPIO CPLD.
54 * Communication with the GPIO CPLD is over the main CPLD's mailbox interface,
55 * along with the dual-port RAM on the CPLD.
57 * Only one process can open the CPLD character device at a time. The driver
58 * enforces this to simplify the communication protocol.
61 /* Resource access addresses. */
62 #define CPLD_MEM_ADDR_H 0x00
63 #define CPLD_MEM_ADDR_L 0x01
64 #define CPLD_MEM_DATA 0x80
66 #define CPLD_MAX_DRAM_WORDS 0x800
69 #define CPLD_REG_SIG1 0x00
70 #define CPLD_REG_SIG2 0x01
71 #define CPLD_REG_HWREV 0x02
72 #define CPLD_REG_CPLDREV 0x03
73 #define CPLD_REG_MBC2X 0x04
74 #define CPLD_REG_MBX2C 0x05
75 #define CPLD_REG_FAN1_TACHO_U 0x10
76 #define CPLD_REG_FAN1_TACHO_L 0x11
77 #define CPLD_REG_FAN2_TACHO_U 0x12
78 #define CPLD_REG_FAN2_TACHO_L 0x13
79 #define CPLD_REG_FAN3_TACHO_U 0x14
80 #define CPLD_REG_FAN3_TACHO_L 0x15
81 #define CPLD_REG_DATE_UU 0x20
82 #define CPLD_REG_DATE_UL 0x21
83 #define CPLD_REG_DATE_LU 0x22
84 #define CPLD_REG_DATE_LL 0x23
85 #define CPLD_REG_TIME_UU 0x24
86 #define CPLD_REG_TIME_UL 0x25
87 #define CPLD_REG_TIME_LU 0x26
88 #define CPLD_REG_TIME_LL 0x27
89 #define CPLD_REG_SCR1 0x5c
90 #define CPLD_REG_SCR2 0x6a
91 #define CPLD_REG_RAM 0x80
95 struct resource *sc_mem;
101 static d_open_t cpld_open;
102 static d_close_t cpld_close;
103 static d_ioctl_t cpld_ioctl;
105 static struct cdevsw cpld_cdevsw = {
106 .d_version = D_VERSION,
108 .d_close = cpld_close,
109 .d_ioctl = cpld_ioctl,
113 static device_probe_t cpld_probe;
114 static device_attach_t cpld_attach;
115 static int cpld_fan_sysctl(SYSCTL_HANDLER_ARGS);
117 static device_method_t cpld_methods[] = {
118 DEVMETHOD(device_probe, cpld_probe),
119 DEVMETHOD(device_attach, cpld_attach),
124 static driver_t cpld_driver = {
127 sizeof(struct cpld_softc)
130 static devclass_t cpld_devclass;
131 DRIVER_MODULE(cpld, lbc, cpld_driver, cpld_devclass, 0, 0);
134 cpld_write(struct cpld_softc *sc, int addr, int data)
136 if (addr >= CPLD_REG_RAM) {
137 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr);
138 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_L, addr);
140 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr);
141 bus_write_1(sc->sc_mem, CPLD_MEM_DATA, data);
145 cpld_read(struct cpld_softc *sc, int addr)
147 if (addr >= CPLD_REG_RAM) {
148 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr);
149 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_L, addr);
151 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr);
153 return (bus_read_1(sc->sc_mem, CPLD_MEM_DATA));
157 * This is only to read a register that's split into two 8-bit registers.
158 * Dual-port RAM is not accepted for this purpose.
161 cpld_read_pair(struct cpld_softc *sc, int addr)
165 KASSERT(addr <= 0xff, ("Invalid register-pair base address %x.", addr));
166 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr);
167 tmp = bus_read_1(sc->sc_mem, CPLD_MEM_DATA) << 8;
169 bus_write_1(sc->sc_mem, CPLD_MEM_ADDR_H, addr + 1);
170 tmp |= bus_read_1(sc->sc_mem, CPLD_MEM_DATA);
176 cpld_probe(device_t dev)
178 if (!ofw_bus_is_compatible(dev, "aeon,tabor-cpld"))
181 device_set_desc(dev, "AmigaOne Tabor CPLD");
183 return (BUS_PROBE_GENERIC);
187 cpld_attach(device_t dev)
189 struct make_dev_args mda;
190 struct cpld_softc *sc;
194 struct sysctl_ctx_list *ctx;
195 struct sysctl_oid *tree;
197 sc = device_get_softc(dev);
201 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
202 RF_ACTIVE|RF_SHAREABLE);
203 if (sc->sc_mem == NULL) {
204 device_printf(dev, "Unable to allocate memory resource.\n");
207 mtx_init(&sc->sc_mutex, "cpld", NULL, MTX_DEF);
210 date = (cpld_read_pair(sc, CPLD_REG_DATE_UU) << 16) |
211 cpld_read_pair(sc, CPLD_REG_DATE_LU);
212 time = (cpld_read_pair(sc, CPLD_REG_TIME_UU) << 16) |
213 cpld_read_pair(sc, CPLD_REG_TIME_LU);
215 device_printf(dev, "Build date: %04x-%02x-%02x\n",
216 (date >> 16) & 0xffff, (date >> 8) & 0xff, date & 0xff);
218 /* Build time is nonsense on tested system. */
219 device_printf(dev, "Build time: %02x:%02x:%02x\n",
220 (time >> 16) & 0xff, (time >> 8) & 0xff, time & 0xff);
224 tmp = cpld_read(sc, CPLD_REG_HWREV);
225 device_printf(dev, "Hardware revision: %d\n", tmp);
227 ctx = device_get_sysctl_ctx(dev);
228 tree = device_get_sysctl_tree(dev);
230 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
231 "cpu_fan", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
232 CPLD_REG_FAN1_TACHO_U, cpld_fan_sysctl, "I",
233 "CPU Fan speed in RPM");
235 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236 "case_1_fan", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
237 CPLD_REG_FAN2_TACHO_U, cpld_fan_sysctl, "I",
238 "Case fan 1 speed in RPM");
240 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
241 "case_2_fan", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
242 CPLD_REG_FAN3_TACHO_U, cpld_fan_sysctl, "I",
243 "Case fan 2 speed in RPM");
245 make_dev_args_init(&mda);
246 mda.mda_flags = MAKEDEV_CHECKNAME;
247 mda.mda_devsw = &cpld_cdevsw;
248 mda.mda_uid = UID_ROOT;
249 mda.mda_gid = GID_WHEEL;
251 err = make_dev_s(&mda, &sc->sc_cdev, "cpld");
253 device_printf(dev, "Error creating character device: %d\n", err);
254 device_printf(dev, "Only sysctl interfaces will be available.\n");
261 cpld_fan_sysctl(SYSCTL_HANDLER_ARGS)
263 struct cpld_softc *sc;
269 mtx_lock(&sc->sc_mutex);
270 /* Read until we get some level of read stability. */
271 rpm = cpld_read(sc, fan_reg);
274 rpm = cpld_read_pair(sc, fan_reg);
275 } while (abs(rpm - old) > 10);
276 mtx_unlock(&sc->sc_mutex);
278 /* Convert RPS->RPM. */
280 error = sysctl_handle_int(oidp, &rpm, 0, req);
286 cpld_open(struct cdev *dev, int flags, int fmt, struct thread *td)
288 struct cpld_softc *sc = dev->si_drv1;
297 cpld_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
299 struct cpld_softc *sc = dev->si_drv1;
306 * Send a command over the CPLD to the other side.
308 * This will first copy the data into the dual-port RAM, then signal the other
309 * side by writing to the mailbox.
312 cpld_send(device_t dev, struct cpld_cmd_data *d)
314 struct cpld_softc *sc;
318 if (d->cmd > USHRT_MAX)
321 sc = device_get_softc(dev);
323 mtx_lock(&sc->sc_mutex);
324 for (i = 0, word = d->words; i < d->len; i++, word++) {
326 cpld_write(sc, CPLD_REG_RAM + d->offset, *word);
328 bus_write_4(sc->sc_mem, CPLD_MEM_DATA, *word);
331 cpld_write(sc, CPLD_REG_MBC2X, d->cmd);
332 mtx_unlock(&sc->sc_mutex);
338 cpld_recv(device_t dev, struct cpld_cmd_data *d)
340 struct cpld_softc *sc;
344 sc = device_get_softc(dev);
346 mtx_lock(&sc->sc_mutex);
347 d->cmd = cpld_read(sc, CPLD_REG_MBX2C);
349 for (i = 0, word = d->words; i < d->len; i++, word++) {
351 *word = cpld_read(sc, CPLD_REG_RAM + d->offset);
353 *word = bus_read_4(sc->sc_mem, CPLD_MEM_DATA);
355 mtx_unlock(&sc->sc_mutex);
361 cpld_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
364 struct cpld_softc *sc;
365 struct cpld_cmd_data *d;
366 void *xfer_data, *tmp;
372 d = (struct cpld_cmd_data *)data;
373 if (d->len + d->offset > CPLD_MAX_DRAM_WORDS) {
376 xfer_data = malloc(d->len * sizeof(uint16_t), M_TEMP, M_WAITOK);
380 err = copyin(d->words, xfer_data, d->len * sizeof(uint16_t));
381 d->words = xfer_data;
383 err = cpld_send(sc->sc_dev, d);
387 d->words = xfer_data;
388 err = cpld_recv(sc->sc_dev, d);
391 err = copyout(xfer_data, d->words,
392 d->len * sizeof(uint16_t));
398 free(xfer_data, M_TEMP);