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 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/conf.h> 30 #include <sys/kernel.h> 31 #include <sys/bus.h> 32 #include <sys/limits.h> 33 #include <sys/module.h> 34 #include <sys/malloc.h> 35 #include <sys/mutex.h> 36 #include <sys/rman.h> 37 #include <sys/sysctl.h> 38 39 #include <machine/bus.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include "cpld.h" 45 46 /* 47 * A driver for the AmigaOne X5000 "Cyrus+" CPLD. 48 * 49 * This is the interface between the CPU and the "Xena" (XMOS) chip. Since the 50 * XMOS is programmable via a SPI-attached flash memory, there's no direct 51 * driver written for the Xena attachment. Instead, a userspace process would 52 * communicate with the Xena by issuing ioctl()s to this CPLD. 53 */ 54 55 /* Resource access addresses. */ 56 #define CPLD_MEM_ADDR 0x0000 57 #define CPLD_MEM_DATA 0x8000 58 59 #define CPLD_MAX_DRAM_WORDS 0x800 60 61 /* CPLD Registers. */ 62 #define CPLD_REG_SIG1 0x00 63 #define CPLD_REG_SIG2 0x01 64 #define CPLD_REG_HWREV 0x02 65 #define CPLD_REG_MBC2X 0x05 66 #define CPLD_REG_MBX2C 0x06 67 #define CPLD_REG_XDEBUG 0x0c 68 #define CPLD_REG_XJTAG 0x0d 69 #define CPLD_REG_FAN_TACHO 0x10 70 #define CPLD_REG_DATE_LW 0x21 71 #define CPLD_REG_DATE_UW 0x22 72 #define CPLD_REG_TIME_LW 0x23 73 #define CPLD_REG_TIME_UW 0x24 74 #define CPLD_REG_SCR1 0x30 75 #define CPLD_REG_SCR2 0x31 76 #define CPLD_REG_RAM 0x8000 77 78 struct cpld_softc { 79 device_t sc_dev; 80 struct resource *sc_mem; 81 struct cdev *sc_cdev; 82 struct mtx sc_mutex; 83 bool sc_isopen; 84 }; 85 86 static d_open_t cpld_open; 87 static d_close_t cpld_close; 88 static d_ioctl_t cpld_ioctl; 89 90 static struct cdevsw cpld_cdevsw = { 91 .d_version = D_VERSION, 92 .d_open = cpld_open, 93 .d_close = cpld_close, 94 .d_ioctl = cpld_ioctl, 95 .d_name = "nvram", 96 }; 97 98 static device_probe_t cpld_probe; 99 static device_attach_t cpld_attach; 100 static int cpld_fan_sysctl(SYSCTL_HANDLER_ARGS); 101 102 static device_method_t cpld_methods[] = { 103 DEVMETHOD(device_probe, cpld_probe), 104 DEVMETHOD(device_attach, cpld_attach), 105 106 DEVMETHOD_END 107 }; 108 109 static driver_t cpld_driver = { 110 "cpld", 111 cpld_methods, 112 sizeof(struct cpld_softc) 113 }; 114 115 DRIVER_MODULE(cpld, lbc, cpld_driver, 0, 0); 116 117 static void 118 cpld_write(struct cpld_softc *sc, int addr, int data) 119 { 120 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr); 121 bus_write_2(sc->sc_mem, CPLD_MEM_DATA, data); 122 } 123 124 static int 125 cpld_read(struct cpld_softc *sc, int addr) 126 { 127 bus_write_2(sc->sc_mem, CPLD_MEM_ADDR, addr); 128 129 return (bus_read_2(sc->sc_mem, CPLD_MEM_DATA)); 130 } 131 132 static int 133 cpld_probe(device_t dev) 134 { 135 if (!ofw_bus_is_compatible(dev, "aeon,cyrus-cpld")) 136 return (ENXIO); 137 138 device_set_desc(dev, "AmigaOne Cyrus CPLD"); 139 140 return (BUS_PROBE_GENERIC); 141 } 142 143 static int 144 cpld_attach(device_t dev) 145 { 146 struct make_dev_args mda; 147 struct cpld_softc *sc; 148 int rid; 149 int date, time, tmp; 150 int err; 151 struct sysctl_ctx_list *ctx; 152 struct sysctl_oid *tree; 153 154 sc = device_get_softc(dev); 155 sc->sc_dev = dev; 156 157 rid = 0; 158 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 159 RF_ACTIVE|RF_SHAREABLE); 160 if (sc->sc_mem == NULL) { 161 device_printf(dev, "Unable to allocate memory resource.\n"); 162 return (ENXIO); 163 } 164 mtx_init(&sc->sc_mutex, "cpld", NULL, MTX_DEF); 165 if (bootverbose) { 166 date = (cpld_read(sc, CPLD_REG_DATE_UW) << 16) | 167 cpld_read(sc, CPLD_REG_DATE_LW); 168 time = (cpld_read(sc, CPLD_REG_TIME_UW) << 16) | 169 cpld_read(sc, CPLD_REG_TIME_LW); 170 171 device_printf(dev, "Build date: %04x-%02x-%02x\n", 172 (date >> 16) & 0xffff, (date >> 8) & 0xff, date & 0xff); 173 device_printf(dev, "Build time: %02x:%02x:%02x\n", 174 (time >> 16) & 0xff, (time >> 8) & 0xff, time & 0xff); 175 } 176 177 tmp = cpld_read(sc, CPLD_REG_HWREV); 178 device_printf(dev, "Hardware revision: %d\n", tmp); 179 180 ctx = device_get_sysctl_ctx(dev); 181 tree = device_get_sysctl_tree(dev); 182 183 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 184 "cpu_fan", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, 185 cpld_fan_sysctl, "I", "CPU Fan speed in RPM"); 186 187 make_dev_args_init(&mda); 188 mda.mda_flags = MAKEDEV_CHECKNAME; 189 mda.mda_devsw = &cpld_cdevsw; 190 mda.mda_uid = UID_ROOT; 191 mda.mda_gid = GID_WHEEL; 192 mda.mda_mode = 0660; 193 mda.mda_si_drv1 = sc; 194 err = make_dev_s(&mda, &sc->sc_cdev, "cpld"); 195 if (err != 0) { 196 device_printf(dev, "Error creating character device: %d\n", err); 197 device_printf(dev, "Only sysctl interfaces will be available.\n"); 198 } 199 200 return (0); 201 } 202 203 static int 204 cpld_fan_sysctl(SYSCTL_HANDLER_ARGS) 205 { 206 struct cpld_softc *sc; 207 int error, old, rpm; 208 209 sc = arg1; 210 mtx_lock(&sc->sc_mutex); 211 /* Read until we get some level of read stability. */ 212 rpm = cpld_read(sc, CPLD_REG_FAN_TACHO); 213 do { 214 old = rpm; 215 rpm = cpld_read(sc, CPLD_REG_FAN_TACHO); 216 } while (abs(rpm - old) > 10); 217 mtx_unlock(&sc->sc_mutex); 218 219 /* Convert RPS->RPM. */ 220 rpm *= 60; 221 error = sysctl_handle_int(oidp, &rpm, 0, req); 222 223 return (error); 224 } 225 226 static int 227 cpld_open(struct cdev *dev, int flags, int fmt, struct thread *td) 228 { 229 struct cpld_softc *sc = dev->si_drv1; 230 231 if (sc->sc_isopen) 232 return (EBUSY); 233 sc->sc_isopen = 1; 234 return (0); 235 } 236 237 static int 238 cpld_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 239 { 240 struct cpld_softc *sc = dev->si_drv1; 241 242 sc->sc_isopen = 0; 243 return (0); 244 } 245 246 static int 247 cpld_send(device_t dev, struct cpld_cmd_data *d) 248 { 249 struct cpld_softc *sc; 250 uint16_t *word; 251 int i; 252 253 if (d->cmd > USHRT_MAX) 254 return (EINVAL); 255 256 sc = device_get_softc(dev); 257 258 mtx_lock(&sc->sc_mutex); 259 for (i = 0, word = d->words; i < d->len; i++, word++) { 260 if (i == 0) 261 cpld_write(sc, CPLD_REG_RAM, *word); 262 else 263 bus_write_4(sc->sc_mem, CPLD_MEM_DATA, *word); 264 } 265 266 cpld_write(sc, CPLD_REG_MBC2X, d->cmd); 267 mtx_unlock(&sc->sc_mutex); 268 269 return (0); 270 } 271 272 static int 273 cpld_recv(device_t dev, struct cpld_cmd_data *d) 274 { 275 struct cpld_softc *sc; 276 uint16_t *word; 277 int i; 278 279 sc = device_get_softc(dev); 280 281 mtx_lock(&sc->sc_mutex); 282 d->cmd = cpld_read(sc, CPLD_REG_MBX2C); 283 284 for (i = 0, word = d->words; i < d->len; i++, word++) { 285 if (i == 0) 286 *word = cpld_read(sc, CPLD_REG_RAM); 287 else 288 *word = bus_read_4(sc->sc_mem, CPLD_MEM_DATA); 289 } 290 mtx_unlock(&sc->sc_mutex); 291 292 return (0); 293 } 294 295 static int 296 cpld_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 297 { 298 struct cpld_softc *sc; 299 struct cpld_cmd_data *d; 300 void *xfer_data, *tmp; 301 int err; 302 303 sc = dev->si_drv1; 304 305 err = 0; 306 d = (struct cpld_cmd_data *)data; 307 if (d->len + d->offset > CPLD_MAX_DRAM_WORDS) { 308 return (EINVAL); 309 } 310 xfer_data = malloc(d->len * sizeof(uint16_t), M_TEMP, M_WAITOK); 311 312 switch (cmd) { 313 case IOCCPLDSEND: 314 err = copyin(d->words, xfer_data, d->len * sizeof(uint16_t)); 315 d->words = xfer_data; 316 if (err == 0) 317 err = cpld_send(sc->sc_dev, d); 318 break; 319 case IOCCPLDRECV: 320 tmp = d->words; 321 d->words = xfer_data; 322 err = cpld_recv(sc->sc_dev, d); 323 d->words = tmp; 324 if (err == 0) 325 err = copyout(xfer_data, d->words, 326 d->len * sizeof(uint16_t)); 327 break; 328 default: 329 err = ENOTTY; 330 break; 331 } 332 free(xfer_data, M_TEMP); 333 334 return (err); 335 } 336