1/*- 2 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * 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/bus.h> 32#include <sys/conf.h> 33#include <sys/kernel.h> 34#include <sys/lock.h> 35#include <sys/module.h> 36#include <sys/mutex.h> 37#include <sys/rman.h> 38#include <machine/bus.h> 39 40#include <arm/at91/at91_sscreg.h> 41 42struct at91_ssc_softc 43{ 44 device_t dev; /* Myself */ 45 void *intrhand; /* Interrupt handle */ 46 struct resource *irq_res; /* IRQ resource */ 47 struct resource *mem_res; /* Memory resource */ 48 struct mtx sc_mtx; /* basically a perimeter lock */ 49 struct cdev *cdev; 50 int flags; 51#define OPENED 1 52}; 53 54static inline uint32_t 55RD4(struct at91_ssc_softc *sc, bus_size_t off) 56{ 57 return bus_read_4(sc->mem_res, off); 58} 59 60static inline void 61WR4(struct at91_ssc_softc *sc, bus_size_t off, uint32_t val) 62{ 63 bus_write_4(sc->mem_res, off, val); 64} 65 66#define AT91_SSC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 67#define AT91_SSC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 68#define AT91_SSC_LOCK_INIT(_sc) \ 69 mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \ 70 "ssc", MTX_DEF) 71#define AT91_SSC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx); 72#define AT91_SSC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED); 73#define AT91_SSC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED); 74#define CDEV2SOFTC(dev) ((dev)->si_drv1) 75 76static devclass_t at91_ssc_devclass; 77 78/* bus entry points */ 79 80static int at91_ssc_probe(device_t dev); 81static int at91_ssc_attach(device_t dev); 82static int at91_ssc_detach(device_t dev); 83static void at91_ssc_intr(void *); 84 85/* helper routines */ 86static int at91_ssc_activate(device_t dev); 87static void at91_ssc_deactivate(device_t dev); 88 89/* cdev routines */ 90static d_open_t at91_ssc_open; 91static d_close_t at91_ssc_close; 92static d_read_t at91_ssc_read; 93static d_write_t at91_ssc_write; 94 95static struct cdevsw at91_ssc_cdevsw = 96{ 97 .d_version = D_VERSION, 98 .d_open = at91_ssc_open, 99 .d_close = at91_ssc_close, 100 .d_read = at91_ssc_read, 101 .d_write = at91_ssc_write, 102}; 103 104static int 105at91_ssc_probe(device_t dev) 106{ 107 device_set_desc(dev, "SSC"); 108 return (0); 109} 110 111static int 112at91_ssc_attach(device_t dev) 113{ 114 struct at91_ssc_softc *sc = device_get_softc(dev); 115 int err; 116 117 sc->dev = dev; 118 err = at91_ssc_activate(dev); 119 if (err) 120 goto out; 121 122 AT91_SSC_LOCK_INIT(sc); 123 124 /* 125 * Activate the interrupt 126 */ 127 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 128 NULL, at91_ssc_intr, sc, &sc->intrhand); 129 if (err) { 130 AT91_SSC_LOCK_DESTROY(sc); 131 goto out; 132 } 133 sc->cdev = make_dev(&at91_ssc_cdevsw, device_get_unit(dev), UID_ROOT, 134 GID_WHEEL, 0600, "ssc%d", device_get_unit(dev)); 135 if (sc->cdev == NULL) { 136 err = ENOMEM; 137 goto out; 138 } 139 sc->cdev->si_drv1 = sc; 140 141 // Init for TSC needs 142 WR4(sc, SSC_CR, SSC_CR_SWRST); 143 WR4(sc, SSC_CMR, 0); // clock divider unused 144 WR4(sc, SSC_RCMR, 145 SSC_RCMR_CKS_RK | SSC_RCMR_CKO_NONE | SSC_RCMR_START_FALL_EDGE_RF); 146 WR4(sc, SSC_RFMR, 147 0x1f | SSC_RFMR_MSFBF | SSC_RFMR_FSOS_NONE); 148 WR4(sc, SSC_TCMR, 149 SSC_TCMR_CKS_TK | SSC_TCMR_CKO_NONE | SSC_RCMR_START_CONT); 150 WR4(sc, SSC_TFMR, 151 0x1f | SSC_TFMR_DATDEF | SSC_TFMR_MSFBF | SSC_TFMR_FSOS_NEG_PULSE); 152 153out: 154 if (err) 155 at91_ssc_deactivate(dev); 156 return (err); 157} 158 159static int 160at91_ssc_detach(device_t dev) 161{ 162 return (EBUSY); /* XXX */ 163} 164 165static int 166at91_ssc_activate(device_t dev) 167{ 168 struct at91_ssc_softc *sc; 169 int rid; 170 171 sc = device_get_softc(dev); 172 rid = 0; 173 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 174 RF_ACTIVE); 175 if (sc->mem_res == NULL) 176 goto errout; 177 rid = 0; 178 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 179 RF_ACTIVE); 180 if (sc->irq_res == NULL) 181 goto errout; 182 return (0); 183errout: 184 at91_ssc_deactivate(dev); 185 return (ENOMEM); 186} 187 188static void 189at91_ssc_deactivate(device_t dev) 190{ 191 struct at91_ssc_softc *sc; 192 193 sc = device_get_softc(dev); 194 if (sc->intrhand) 195 bus_teardown_intr(dev, sc->irq_res, sc->intrhand); 196 sc->intrhand = NULL; 197 bus_generic_detach(sc->dev); 198 if (sc->mem_res) 199 bus_release_resource(dev, SYS_RES_IOPORT, 200 rman_get_rid(sc->mem_res), sc->mem_res); 201 sc->mem_res = NULL; 202 if (sc->irq_res) 203 bus_release_resource(dev, SYS_RES_IRQ, 204 rman_get_rid(sc->irq_res), sc->irq_res); 205 sc->irq_res = NULL; 206 return; 207} 208 209static void 210at91_ssc_intr(void *xsc) 211{ 212 struct at91_ssc_softc *sc = xsc; 213 wakeup(sc); 214 return; 215} 216 217static int 218at91_ssc_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 219{ 220 struct at91_ssc_softc *sc; 221 222 sc = CDEV2SOFTC(dev); 223 AT91_SSC_LOCK(sc); 224 if (!(sc->flags & OPENED)) { 225 sc->flags |= OPENED; 226 } 227 AT91_SSC_UNLOCK(sc); 228 return (0); 229} 230 231static int 232at91_ssc_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 233{ 234 struct at91_ssc_softc *sc; 235 236 sc = CDEV2SOFTC(dev); 237 AT91_SSC_LOCK(sc); 238 sc->flags &= ~OPENED; 239 AT91_SSC_UNLOCK(sc); 240 return (0); 241} 242 243static int 244at91_ssc_read(struct cdev *dev, struct uio *uio, int flag) 245{ 246 return EIO; 247} 248 249static int 250at91_ssc_write(struct cdev *dev, struct uio *uio, int flag) 251{ 252 return EIO; 253} 254 255static device_method_t at91_ssc_methods[] = { 256 /* Device interface */ 257 DEVMETHOD(device_probe, at91_ssc_probe), 258 DEVMETHOD(device_attach, at91_ssc_attach), 259 DEVMETHOD(device_detach, at91_ssc_detach), 260 261 { 0, 0 } 262}; 263 264static driver_t at91_ssc_driver = { 265 "at91_ssc", 266 at91_ssc_methods, 267 sizeof(struct at91_ssc_softc), 268}; 269 270DRIVER_MODULE(at91_ssc, atmelarm, at91_ssc_driver, at91_ssc_devclass, 0, 0); 271