1 /* $OpenBSD: acrtc.c,v 1.4 2020/03/28 11:40:29 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 23 #include <dev/fdt/rsbvar.h> 24 25 #include <dev/ofw/openfirm.h> 26 #include <dev/ofw/ofw_clock.h> 27 #include <dev/ofw/fdt.h> 28 29 #include <dev/clock_subr.h> 30 31 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 32 33 extern todr_chip_handle_t todr_handle; 34 35 #define CK32K_OUT_CTRL1 0xc1 36 #define CK32K_OUT_CTRL_PRE_DIV_MASK (0x7 << 5) 37 #define CK32K_OUT_CTRL_PRE_DIV_32K (0x7 << 5) 38 #define CK32K_OUT_CTRL_MUX_SEL_MASK (1 << 4) 39 #define CK32K_OUT_CTRL_MUX_SEL_32K (0 << 4) 40 #define CK32K_OUT_CTRL_POST_DIV_MASK (0x7 << 1) 41 #define CK32K_OUT_CTRL_POST_DIV_32K (0x0 << 1) 42 #define CK32K_OUT_CTRL_ENA (1 << 0) 43 #define RTC_CTRL 0xc7 44 #define RTC_CTRL_12H_24H_MODE (1 << 0) 45 #define RTC_SEC 0xc8 46 #define RTC_SEC_MASK (0x7f << 0) 47 #define RTC_MIN 0xc9 48 #define RTC_MIN_MASK (0x7f << 0) 49 #define RTC_HOU 0xca 50 #define RTC_HOU_MASK (0x3f << 0) 51 #define RTC_WEE 0xcb 52 #define RTC_WEE_MASK (0x07 << 0) 53 #define RTC_DAY 0xcc 54 #define RTC_DAY_MASK (0x3f << 0) 55 #define RTC_MON 0xcd 56 #define RTC_MON_MASK (0x1f << 0) 57 #define RTC_YEA 0xce 58 #define RTC_YEA_LEAP_YEAR (1 << 15) 59 #define RTC_YEA_MASK (0xff << 0) 60 #define RTC_UPD_TRIG 0xcf 61 #define RTC_UPD_TRIG_UPDATE (1 << 15) 62 63 struct acrtc_softc { 64 struct device sc_dev; 65 void *sc_cookie; 66 uint16_t sc_rta; 67 68 struct todr_chip_handle sc_todr; 69 struct clock_device sc_cd; 70 }; 71 72 int acrtc_match(struct device *, void *, void *); 73 void acrtc_attach(struct device *, struct device *, void *); 74 75 struct cfattach acrtc_ca = { 76 sizeof(struct acrtc_softc), acrtc_match, acrtc_attach 77 }; 78 79 struct cfdriver acrtc_cd = { 80 NULL, "acrtc", DV_DULL 81 }; 82 83 int acrtc_clock_read(struct acrtc_softc *, struct clock_ymdhms *); 84 int acrtc_clock_write(struct acrtc_softc *, struct clock_ymdhms *); 85 int acrtc_gettime(struct todr_chip_handle *, struct timeval *); 86 int acrtc_settime(struct todr_chip_handle *, struct timeval *); 87 88 void acrtc_ck32k_enable(void *, uint32_t *, int); 89 90 int 91 acrtc_match(struct device *parent, void *match, void *aux) 92 { 93 struct rsb_attach_args *ra = aux; 94 95 if (strcmp(ra->ra_name, "x-powers,ac100") == 0) 96 return 1; 97 return 0; 98 } 99 100 void 101 acrtc_attach(struct device *parent, struct device *self, void *aux) 102 { 103 struct acrtc_softc *sc = (struct acrtc_softc *)self; 104 struct rsb_attach_args *ra = aux; 105 int node; 106 107 sc->sc_cookie = ra->ra_cookie; 108 sc->sc_rta = ra->ra_rta; 109 110 printf("\n"); 111 112 sc->sc_todr.cookie = sc; 113 sc->sc_todr.todr_gettime = acrtc_gettime; 114 sc->sc_todr.todr_settime = acrtc_settime; 115 todr_handle = &sc->sc_todr; 116 117 node = OF_getnodebyname(ra->ra_node, "rtc"); 118 if (node == 0) 119 return; 120 121 sc->sc_cd.cd_node = node; 122 sc->sc_cd.cd_cookie = sc; 123 sc->sc_cd.cd_enable = acrtc_ck32k_enable; 124 clock_register(&sc->sc_cd); 125 } 126 127 static inline uint16_t 128 acrtc_read_reg(struct acrtc_softc *sc, uint8_t reg) 129 { 130 return rsb_read_2(sc->sc_cookie, sc->sc_rta, reg); 131 } 132 133 static inline void 134 acrtc_write_reg(struct acrtc_softc *sc, uint8_t reg, uint16_t value) 135 { 136 rsb_write_2(sc->sc_cookie, sc->sc_rta, reg, value); 137 } 138 139 int 140 acrtc_gettime(struct todr_chip_handle *handle, struct timeval *tv) 141 { 142 struct acrtc_softc *sc = handle->cookie; 143 struct clock_ymdhms dt; 144 int error; 145 146 error = acrtc_clock_read(sc, &dt); 147 if (error) 148 return error; 149 150 if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 || 151 dt.dt_day > 31 || dt.dt_day == 0 || 152 dt.dt_mon > 12 || dt.dt_mon == 0 || 153 dt.dt_year < POSIX_BASE_YEAR) 154 return EINVAL; 155 156 tv->tv_sec = clock_ymdhms_to_secs(&dt); 157 tv->tv_usec = 0; 158 return 0; 159 } 160 161 int 162 acrtc_settime(struct todr_chip_handle *handle, struct timeval *tv) 163 { 164 struct acrtc_softc *sc = handle->cookie; 165 struct clock_ymdhms dt; 166 167 clock_secs_to_ymdhms(tv->tv_sec, &dt); 168 169 return acrtc_clock_write(sc, &dt); 170 } 171 172 int 173 acrtc_clock_read(struct acrtc_softc *sc, struct clock_ymdhms *dt) 174 { 175 uint16_t ctrl; 176 177 dt->dt_sec = FROMBCD(acrtc_read_reg(sc, RTC_SEC) & RTC_SEC_MASK); 178 dt->dt_min = FROMBCD(acrtc_read_reg(sc, RTC_MIN) & RTC_MIN_MASK); 179 dt->dt_hour = FROMBCD(acrtc_read_reg(sc, RTC_HOU) & RTC_HOU_MASK); 180 dt->dt_day = FROMBCD(acrtc_read_reg(sc, RTC_DAY) & RTC_DAY_MASK); 181 dt->dt_mon = FROMBCD(acrtc_read_reg(sc, RTC_MON) & RTC_MON_MASK); 182 dt->dt_year = FROMBCD(acrtc_read_reg(sc, RTC_YEA) & RTC_YEA_MASK); 183 dt->dt_year += 2000; 184 185 #ifdef DEBUG 186 printf("%02d/%02d/%04d %02d:%02d:%0d\n", dt->dt_day, dt->dt_mon, 187 dt->dt_year, dt->dt_hour, dt->dt_min, dt->dt_sec); 188 #endif 189 190 /* Consider the time to be invalid if the clock is in 12H mode. */ 191 ctrl = acrtc_read_reg(sc, RTC_CTRL); 192 if ((ctrl & RTC_CTRL_12H_24H_MODE) == 0) 193 return EINVAL; 194 195 return 0; 196 } 197 198 int 199 acrtc_clock_write(struct acrtc_softc *sc, struct clock_ymdhms *dt) 200 { 201 uint16_t leap = isleap(dt->dt_year) ? RTC_YEA_LEAP_YEAR : 0; 202 203 acrtc_write_reg(sc, RTC_SEC, TOBCD(dt->dt_sec)); 204 acrtc_write_reg(sc, RTC_MIN, TOBCD(dt->dt_min)); 205 acrtc_write_reg(sc, RTC_HOU, TOBCD(dt->dt_hour)); 206 acrtc_write_reg(sc, RTC_WEE, TOBCD(dt->dt_wday)); 207 acrtc_write_reg(sc, RTC_DAY, TOBCD(dt->dt_day)); 208 acrtc_write_reg(sc, RTC_MON, TOBCD(dt->dt_mon)); 209 acrtc_write_reg(sc, RTC_YEA, TOBCD(dt->dt_year - 2000) | leap); 210 acrtc_write_reg(sc, RTC_UPD_TRIG, RTC_UPD_TRIG_UPDATE); 211 212 /* Switch to 24H mode to indicate the time is now valid. */ 213 acrtc_write_reg(sc, RTC_CTRL, RTC_CTRL_12H_24H_MODE); 214 215 return 0; 216 } 217 218 void 219 acrtc_ck32k_enable(void *cookie, uint32_t *cells, int on) 220 { 221 struct acrtc_softc *sc = cookie; 222 uint32_t idx = cells[0]; 223 uint16_t reg; 224 225 reg = acrtc_read_reg(sc, CK32K_OUT_CTRL1 + idx); 226 reg &= ~CK32K_OUT_CTRL_PRE_DIV_MASK; 227 reg &= ~CK32K_OUT_CTRL_MUX_SEL_MASK; 228 reg &= ~CK32K_OUT_CTRL_POST_DIV_MASK; 229 reg |= CK32K_OUT_CTRL_PRE_DIV_32K; 230 reg |= CK32K_OUT_CTRL_MUX_SEL_32K; 231 reg |= CK32K_OUT_CTRL_POST_DIV_32K; 232 if (on) 233 reg |= CK32K_OUT_CTRL_ENA; 234 else 235 reg &= ~CK32K_OUT_CTRL_ENA; 236 acrtc_write_reg(sc, CK32K_OUT_CTRL1 + idx, reg); 237 } 238