From 26feff52ee7710f573fcbdd73cdbd90fdaabe23d Mon Sep 17 00:00:00 2001 From: jsg Date: Sun, 14 Jun 2015 05:01:31 +0000 Subject: [PATCH] add a driver for the ARM PrimeCell PL031 RTC --- sys/arch/armv7/vexpress/files.vexpress | 6 +- sys/arch/armv7/vexpress/pl031.c | 112 +++++++++++++++++++++++++ sys/arch/armv7/vexpress/vexpress.c | 3 +- sys/arch/armv7/vexpress/vexpress_a15.c | 11 ++- sys/arch/armv7/vexpress/vexpress_a9.c | 11 ++- 5 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 sys/arch/armv7/vexpress/pl031.c diff --git a/sys/arch/armv7/vexpress/files.vexpress b/sys/arch/armv7/vexpress/files.vexpress index d4cd4eb0912..dd7db10932d 100644 --- a/sys/arch/armv7/vexpress/files.vexpress +++ b/sys/arch/armv7/vexpress/files.vexpress @@ -1,4 +1,4 @@ -# $OpenBSD: files.vexpress,v 1.1 2015/06/08 06:33:16 jsg Exp $ +# $OpenBSD: files.vexpress,v 1.2 2015/06/14 05:01:31 jsg Exp $ define vexpress {} device vexpress: vexpress @@ -12,6 +12,10 @@ device pluart attach pluart at vexpress file arch/armv7/vexpress/pl011.c pluart +device plrtc +attach plrtc at vexpress +file arch/armv7/vexpress/pl031.c plrtc + attach virtio at vexpress with virtio_mmio file arch/armv7/vexpress/virtio_mmio.c virtio_mmio diff --git a/sys/arch/armv7/vexpress/pl031.c b/sys/arch/armv7/vexpress/pl031.c new file mode 100644 index 00000000000..40f8d70cd91 --- /dev/null +++ b/sys/arch/armv7/vexpress/pl031.c @@ -0,0 +1,112 @@ +/* $OpenBSD: pl031.c,v 1.1 2015/06/14 05:01:31 jsg Exp $ */ + +/* + * Copyright (c) 2015 Jonathan Gray + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include +#include +#include + +#define RTCDR 0x00 +#define RTCMR 0x04 +#define RTCLR 0x08 +#define RTCCR 0x0c +#define RTCIMSC 0x10 +#define RTCRIS 0x14 +#define RTCMIS 0x18 +#define RTCICR 0x1c + +#define RTCCR_START (1 << 0) + +extern todr_chip_handle_t todr_handle; + +struct plrtc_softc { + struct device sc_dev; + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; +}; + +void plrtc_attach(struct device *, struct device *, void *); +int plrtc_gettime(struct todr_chip_handle *, struct timeval *); +int plrtc_settime(struct todr_chip_handle *, struct timeval *); + + +struct cfattach plrtc_ca = { + sizeof(struct plrtc_softc), NULL , plrtc_attach +}; + +struct cfdriver plrtc_cd = { + NULL, "plrtc", DV_DULL +}; + +int +plrtc_gettime(todr_chip_handle_t handle, struct timeval *tv) +{ + struct plrtc_softc *sc = handle->cookie; + uint32_t tod; + + tod = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RTCDR); + + tv->tv_sec = tod; + tv->tv_usec = 0; + + return (0); +} + +int +plrtc_settime(todr_chip_handle_t handle, struct timeval *tv) +{ + struct plrtc_softc *sc = handle->cookie; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTCLR, tv->tv_sec); + + return (0); +} + +void +plrtc_attach(struct device *parent, struct device *self, void *aux) +{ + struct armv7_attach_args *aa = aux; + struct plrtc_softc *sc = (struct plrtc_softc *) self; + todr_chip_handle_t handle; + + sc->sc_iot = aa->aa_iot; + if (bus_space_map(sc->sc_iot, aa->aa_dev->mem[0].addr, + aa->aa_dev->mem[0].size, 0, &sc->sc_ioh)) { + printf(": failed to map mem space\n"); + return; + } + + handle = malloc(sizeof(struct todr_chip_handle), M_DEVBUF, + M_NOWAIT | M_ZERO); + if (handle == NULL) + panic("couldn't allocate todr_handle"); + + handle->cookie = sc; + handle->todr_gettime = plrtc_gettime; + handle->todr_settime = plrtc_settime; + todr_handle = handle; + + /* enable the rtc */ + bus_space_write_4(sc->sc_iot, sc->sc_ioh, RTCCR, RTCCR_START); + + printf("\n"); +} diff --git a/sys/arch/armv7/vexpress/vexpress.c b/sys/arch/armv7/vexpress/vexpress.c index fc9481916c2..2064a6b3a12 100644 --- a/sys/arch/armv7/vexpress/vexpress.c +++ b/sys/arch/armv7/vexpress/vexpress.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vexpress.c,v 1.1 2015/06/08 06:33:16 jsg Exp $ */ +/* $OpenBSD: vexpress.c,v 1.2 2015/06/14 05:01:31 jsg Exp $ */ /* * Copyright (c) 2015 Jonathan Gray @@ -40,6 +40,7 @@ struct cfdriver vexpress_cd = { struct board_dev vexpress_devs[] = { { "sysreg", 0 }, { "pluart", 0 }, + { "plrtc", 0 }, { "virtio", 0 }, { "virtio", 1 }, { "virtio", 2 }, diff --git a/sys/arch/armv7/vexpress/vexpress_a15.c b/sys/arch/armv7/vexpress/vexpress_a15.c index b6025cb3484..8112e449ecb 100644 --- a/sys/arch/armv7/vexpress/vexpress_a15.c +++ b/sys/arch/armv7/vexpress/vexpress_a15.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vexpress_a15.c,v 1.1 2015/06/08 06:33:16 jsg Exp $ */ +/* $OpenBSD: vexpress_a15.c,v 1.2 2015/06/14 05:01:31 jsg Exp $ */ /* * Copyright (c) 2015 Jonathan Gray @@ -49,6 +49,10 @@ #define VIRTIO2_IRQ 42 #define VIRTIO3_IRQ 43 +#define RTC_ADDR 0x1c170000 +#define RTC_SIZE 0x1000 +#define RTC_IRQ 4 + struct armv7_dev vexpress_a15_devs[] = { { .name = "sysreg", .unit = 0, @@ -74,6 +78,11 @@ struct armv7_dev vexpress_a15_devs[] = { .mem = { { UART1_ADDR, UARTx_SIZE } }, .irq = { UART3_IRQ } }, + { .name = "plrtc", + .unit = 0, + .mem = { { RTC_ADDR, RTC_SIZE } }, + .irq = { RTC_IRQ } + }, { .name = "virtio", .unit = 0, .mem = { { VIRTIO0_ADDR, VIRTIO_SIZE } }, diff --git a/sys/arch/armv7/vexpress/vexpress_a9.c b/sys/arch/armv7/vexpress/vexpress_a9.c index 3204e10ed94..2fe14e76f40 100644 --- a/sys/arch/armv7/vexpress/vexpress_a9.c +++ b/sys/arch/armv7/vexpress/vexpress_a9.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vexpress_a9.c,v 1.1 2015/06/08 06:33:16 jsg Exp $ */ +/* $OpenBSD: vexpress_a9.c,v 1.2 2015/06/14 05:01:31 jsg Exp $ */ /* * Copyright (c) 2015 Jonathan Gray @@ -49,6 +49,10 @@ #define VIRTIO2_IRQ 42 #define VIRTIO3_IRQ 43 +#define RTC_ADDR 0x10017000 +#define RTC_SIZE 0x1000 +#define RTC_IRQ 4 + struct armv7_dev vexpress_a9_devs[] = { { .name = "sysreg", .unit = 0, @@ -74,6 +78,11 @@ struct armv7_dev vexpress_a9_devs[] = { .mem = { { UART1_ADDR, UARTx_SIZE } }, .irq = { UART3_IRQ } }, + { .name = "plrtc", + .unit = 0, + .mem = { { RTC_ADDR, RTC_SIZE } }, + .irq = { RTC_IRQ } + }, { .name = "virtio", .unit = 0, .mem = { { VIRTIO0_ADDR, VIRTIO_SIZE } }, -- 2.20.1