1/*- 2 * Copyright (c) 2020 Justin Hibbits 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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 ``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. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD$"); 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/conf.h> 32#include <sys/kernel.h> 33#include <sys/bus.h> 34#include <sys/limits.h> 35#include <sys/module.h> 36#include <sys/malloc.h> 37#include <sys/mutex.h> 38#include <sys/rman.h> 39#include <sys/sysctl.h> 40 41#include <machine/bus.h> 42 43#include <dev/ofw/ofw_bus.h> 44#include <dev/ofw/ofw_bus_subr.h> 45 46#include "cpld.h" 47 48/* 49 * A driver for the AmigaOne X5000 "Cyrus+" CPLD. 50 * 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. 55 */ 56 57/* Resource access addresses. */ 58#define CPLD_MEM_ADDR 0x0000 59#define CPLD_MEM_DATA 0x8000 60 61#define CPLD_MAX_DRAM_WORDS 0x800 62 63/* CPLD Registers. */ 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 79 80struct cpld_softc { 81 device_t sc_dev; 82 struct resource *sc_mem; 83 struct cdev *sc_cdev; 84 struct mtx sc_mutex; 85 bool sc_isopen; 86}; 87 88static d_open_t cpld_open; 89static d_close_t cpld_close; 90static d_ioctl_t cpld_ioctl; 91 92static struct cdevsw cpld_cdevsw = { 93 .d_version = D_VERSION, 94 .d_open = cpld_open, 95 .d_close = cpld_close, 96 .d_ioctl = cpld_ioctl, 97 .d_name = "nvram", 98}; 99 100static device_probe_t cpld_probe; 101static device_attach_t cpld_attach; 102static int cpld_fan_sysctl(SYSCTL_HANDLER_ARGS); 103 104static device_method_t cpld_methods[] = { 105 DEVMETHOD(device_probe, cpld_probe), 106 DEVMETHOD(device_attach, cpld_attach), 107 108 DEVMETHOD_END 109}; 110 111static driver_t cpld_driver = { 112 "cpld", 113 cpld_methods, 114 sizeof(struct cpld_softc) 115}; 116 117static devclass_t cpld_devclass; 118DRIVER_MODULE(cpld, lbc, cpld_driver, cpld_devclass, 0, 0); 119 120static void 121cpld_write(struct cpld_softc *sc, int addr, int data) 122{ 123 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr); 124 bus_write_2(sc->sc_mem, CPLD_MEM_DATA, data); 125} 126 127static int 128cpld_read(struct cpld_softc *sc, int addr) 129{ 130 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr); 131 132 return (bus_read_2(sc->sc_mem, CPLD_MEM_DATA)); 133} 134 135static int 136cpld_probe(device_t dev) 137{ 138 if (!ofw_bus_is_compatible(dev, "aeon,cyrus-cpld")) 139 return (ENXIO); 140 141 device_set_desc(dev, "AmigaOne Cyrus CPLD"); 142 143 return (BUS_PROBE_GENERIC); 144} 145 146static int 147cpld_attach(device_t dev) 148{ 149 struct make_dev_args mda; 150 struct cpld_softc *sc; 151 int rid; 152 int date, time, tmp; 153 int err; 154 struct sysctl_ctx_list *ctx; 155 struct sysctl_oid *tree; 156 157 sc = device_get_softc(dev); 158 sc->sc_dev = dev; 159 160 rid = 0; 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"); 165 return (ENXIO); 166 } 167 mtx_init(&sc->sc_mutex, "cpld", NULL, MTX_DEF); 168 if (bootverbose) { 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); 173 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); 178 } 179 180 tmp = cpld_read(sc, CPLD_REG_HWREV); 181 device_printf(dev, "Hardware revision: %d\n", tmp); 182 183 ctx = device_get_sysctl_ctx(dev); 184 tree = device_get_sysctl_tree(dev); 185 186 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 187 "cpu_fan", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, 188 cpld_fan_sysctl, "I", "CPU Fan speed in RPM"); 189 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; 195 mda.mda_mode = 0660; 196 mda.mda_si_drv1 = sc; 197 err = make_dev_s(&mda, &sc->sc_cdev, "cpld"); 198 if (err != 0) { 199 device_printf(dev, "Error creating character device: %d\n", err); 200 device_printf(dev, "Only sysctl interfaces will be available.\n"); 201 } 202 203 return (0); 204} 205 206static int 207cpld_fan_sysctl(SYSCTL_HANDLER_ARGS) 208{ 209 struct cpld_softc *sc; 210 int error, old, rpm; 211 212 sc = arg1; 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); 216 do { 217 old = rpm; 218 rpm = cpld_read(sc, CPLD_REG_FAN_TACHO); 219 } while (abs(rpm - old) > 10); 220 mtx_unlock(&sc->sc_mutex); 221 222 /* Convert RPS->RPM. */ 223 rpm *= 60; 224 error = sysctl_handle_int(oidp, &rpm, 0, req); 225 226 return (error); 227} 228 229static int 230cpld_open(struct cdev *dev, int flags, int fmt, struct thread *td) 231{ 232 struct cpld_softc *sc = dev->si_drv1; 233 234 if (sc->sc_isopen) 235 return (EBUSY); 236 sc->sc_isopen = 1; 237 return (0); 238} 239 240static int 241cpld_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 242{ 243 struct cpld_softc *sc = dev->si_drv1; 244 245 sc->sc_isopen = 0; 246 return (0); 247} 248 249static int 250cpld_send(device_t dev, struct cpld_cmd_data *d) 251{ 252 struct cpld_softc *sc; 253 uint16_t *word; 254 int i; 255 256 if (d->cmd > USHRT_MAX) 257 return (EINVAL); 258 259 sc = device_get_softc(dev); 260 261 mtx_lock(&sc->sc_mutex); 262 for (i = 0, word = d->words; i < d->len; i++, word++) { 263 if (i == 0) 264 cpld_write(sc, CPLD_REG_RAM, *word); 265 else 266 bus_write_4(sc->sc_mem, CPLD_MEM_DATA, *word); 267 } 268 269 cpld_write(sc, CPLD_REG_MBC2X, d->cmd); 270 mtx_unlock(&sc->sc_mutex); 271 272 return (0); 273} 274 275static int 276cpld_recv(device_t dev, struct cpld_cmd_data *d) 277{ 278 struct cpld_softc *sc; 279 uint16_t *word; 280 int i; 281 282 sc = device_get_softc(dev); 283 284 mtx_lock(&sc->sc_mutex); 285 d->cmd = cpld_read(sc, CPLD_REG_MBX2C); 286 287 for (i = 0, word = d->words; i < d->len; i++, word++) { 288 if (i == 0) 289 *word = cpld_read(sc, CPLD_REG_RAM); 290 else 291 *word = bus_read_4(sc->sc_mem, CPLD_MEM_DATA); 292 } 293 mtx_unlock(&sc->sc_mutex); 294 295 return (0); 296} 297 298static int 299cpld_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 300{ 301 struct cpld_softc *sc; 302 struct cpld_cmd_data *d; 303 void *xfer_data, *tmp; 304 int err; 305 306 sc = dev->si_drv1; 307 308 err = 0; 309 d = (struct cpld_cmd_data *)data; 310 if (d->len + d->offset > CPLD_MAX_DRAM_WORDS) { 311 return (EINVAL); 312 } 313 xfer_data = malloc(d->len * sizeof(uint16_t), M_TEMP, M_WAITOK); 314 315 switch (cmd) { 316 case IOCCPLDSEND: 317 err = copyin(d->words, xfer_data, d->len * sizeof(uint16_t)); 318 d->words = xfer_data; 319 if (err == 0) 320 err = cpld_send(sc->sc_dev, d); 321 break; 322 case IOCCPLDRECV: 323 tmp = d->words; 324 d->words = xfer_data; 325 err = cpld_recv(sc->sc_dev, d); 326 d->words = tmp; 327 if (err == 0) 328 err = copyout(xfer_data, d->words, 329 d->len * sizeof(uint16_t)); 330 break; 331 default: 332 err = ENOTTY; 333 break; 334 } 335 free(xfer_data, M_TEMP); 336 337 return (err); 338} 339