1 /* $OpenBSD: octrtc.c,v 1.12 2020/06/30 14:47:16 visa Exp $ */ 2 3 /* 4 * Copyright (c) 2013, 2014 Paul Irofti. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/device.h> 22 #include <sys/proc.h> 23 24 #include <dev/clock_subr.h> 25 26 #include <machine/bus.h> 27 #include <machine/autoconf.h> 28 #include <machine/octeonvar.h> 29 30 #ifdef OCTRTC_DEBUG 31 #define DPRINTF(x) printf x 32 #else 33 #define DPRINTF(x) 34 #endif 35 36 #define MIO_TWS_SW_TWSI 0x0001180000001000ULL 37 #define MIO_TWS_SW_TWSI_EXT 0x0001180000001018ULL 38 #define OCTRTC_REG 0x68 39 40 struct octrtc_softc { 41 struct device sc_dev; 42 struct todr_chip_handle sc_todr; 43 }; 44 45 struct cfdriver octrtc_cd = { 46 NULL, "octrtc", DV_DULL 47 }; 48 49 int octrtc_match(struct device *, void *, void *); 50 void octrtc_attach(struct device *, struct device *, void *); 51 52 int octrtc_gettime(struct todr_chip_handle *, struct timeval *); 53 int octrtc_read(uint8_t *, char); 54 55 int octrtc_settime(struct todr_chip_handle *, struct timeval *); 56 int octrtc_write(uint8_t); 57 58 59 struct cfattach octrtc_ca = { 60 sizeof(struct octrtc_softc), octrtc_match, octrtc_attach, 61 }; 62 63 64 union mio_tws_sw_twsi_reg { 65 uint64_t reg; 66 struct cvmx_mio_twsx_sw_twsi_s { 67 uint64_t v:1; /* Valid bit */ 68 uint64_t slonly:1; /* Slave Only Mode */ 69 uint64_t eia:1; /* Extended Internal Address */ 70 uint64_t op:4; /* Opcode field */ 71 uint64_t r:1; /* Read bit or result */ 72 uint64_t sovr:1; /* Size Override */ 73 uint64_t size:3; /* Size in bytes */ 74 uint64_t scr:2; /* Scratch, unused */ 75 uint64_t a:10; /* Address field */ 76 uint64_t ia:5; /* Internal Address */ 77 uint64_t eop_ia:3; /* Extra opcode */ 78 uint64_t d:32; /* Data Field */ 79 } field; 80 }; 81 82 83 static const uint16_t no_rtc_boards[] = { 84 BOARD_TYPE_UBIQUITI_E100, 85 BOARD_TYPE_UBIQUITI_E120, 86 BOARD_TYPE_UBIQUITI_E200, 87 BOARD_TYPE_UBIQUITI_E220, 88 BOARD_TYPE_UBIQUITI_E300, 89 BOARD_TYPE_UBIQUITI_E1000, 90 BOARD_TYPE_RHINOLABS_SHASTA 91 }; 92 93 int 94 octrtc_match(struct device *parent, void *match, void *aux) 95 { 96 struct mainbus_attach_args *maa = aux; 97 struct cfdata *cf = match; 98 int i; 99 100 if (strcmp(maa->maa_name, cf->cf_driver->cd_name) != 0) 101 return 0; 102 for (i = 0; i < nitems(no_rtc_boards); i++) 103 if (octeon_boot_info->board_type == no_rtc_boards[i]) 104 return 0; 105 return 1; 106 } 107 108 void 109 octrtc_attach(struct device *parent, struct device *self, void *aux) 110 { 111 struct octrtc_softc *sc = (struct octrtc_softc *)self; 112 113 sc->sc_todr.cookie = sc; 114 sc->sc_todr.todr_gettime = octrtc_gettime; 115 sc->sc_todr.todr_settime = octrtc_settime; 116 todr_attach(&sc->sc_todr); 117 118 printf(": DS1337\n"); 119 } 120 121 int 122 octrtc_gettime(struct todr_chip_handle *handle, struct timeval *tv) 123 { 124 struct clock_ymdhms dt; 125 uint8_t tod[8]; 126 uint8_t check; 127 int i, rc; 128 129 int nretries = 2; 130 131 DPRINTF(("\nTOD: ")); 132 while (nretries--) { 133 rc = octrtc_read(&tod[0], 1); /* ia read */ 134 if (rc) { 135 DPRINTF(("octrtc_read(0) failed %d", rc)); 136 return EIO; 137 } 138 139 for (i = 1; i < 8; i++) { 140 rc = octrtc_read(&tod[i], 0); /* current address */ 141 if (rc) { 142 DPRINTF(("octrtc_read(%d) failed %d", i, rc)); 143 return EIO; 144 } 145 DPRINTF(("%#X ", tod[i])); 146 } 147 148 /* Check against time-wrap */ 149 rc = octrtc_read(&check, 1); /* ia read */ 150 if (rc) { 151 DPRINTF(("octrtc_read(check) failed %d", rc)); 152 return EIO; 153 } 154 if ((check & 0xf) == (tod[0] & 0xf)) 155 break; 156 } 157 DPRINTF(("\n")); 158 159 DPRINTF(("Time: %d %d %d (%d) %02d:%02d:%02d\n", 160 ((tod[5] & 0x80) ? 2000 : 1900) + FROMBCD(tod[6]), /* year */ 161 FROMBCD(tod[5] & 0x1f), /* month */ 162 FROMBCD(tod[4] & 0x3f), /* day */ 163 (tod[3] & 0x7), /* day of the week */ 164 FROMBCD(tod[2] & 0x3f), /* hour */ 165 FROMBCD(tod[1] & 0x7f), /* minute */ 166 FROMBCD(tod[0] & 0x7f))); /* second */ 167 168 dt.dt_year = ((tod[5] & 0x80) ? 2000 : 1900) + FROMBCD(tod[6]); 169 dt.dt_mon = FROMBCD(tod[5] & 0x1f); 170 dt.dt_day = FROMBCD(tod[4] & 0x3f); 171 dt.dt_hour = FROMBCD(tod[2] & 0x3f); 172 if ((tod[2] & 0x40) && (tod[2] & 0x20)) /* adjust AM/PM format */ 173 dt.dt_hour = (dt.dt_hour + 12) % 24; 174 dt.dt_min = FROMBCD(tod[1] & 0x7f); 175 dt.dt_sec = FROMBCD(tod[0] & 0x7f); 176 177 if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 || 178 dt.dt_day > 31 || dt.dt_day == 0 || 179 dt.dt_mon > 12 || dt.dt_mon == 0 || 180 dt.dt_year < POSIX_BASE_YEAR) 181 return EINVAL; 182 183 tv->tv_sec = clock_ymdhms_to_secs(&dt); 184 tv->tv_usec = 0; 185 return 0; 186 } 187 188 int 189 octrtc_read(uint8_t *data, char ia) 190 { 191 int nretries = 5; 192 union mio_tws_sw_twsi_reg twsi; 193 194 again: 195 twsi.reg = 0; 196 twsi.field.v = 1; 197 twsi.field.r = 1; 198 twsi.field.sovr = 1; 199 twsi.field.a = OCTRTC_REG; 200 if (ia) { 201 twsi.field.op = 1; 202 } 203 204 octeon_xkphys_write_8(MIO_TWS_SW_TWSI, twsi.reg); 205 /* The 1st bit is cleared when the operation is complete */ 206 do { 207 delay(1000); 208 twsi.reg = octeon_xkphys_read_8(MIO_TWS_SW_TWSI); 209 } while (twsi.field.v); 210 DPRINTF(("%#llX ", twsi.reg)); 211 212 /* 213 * The data field is in the upper 32 bits and we're only 214 * interested in the first byte. 215 */ 216 *data = twsi.field.d & 0xff; 217 218 /* 8th bit is the read result: 1 = success, 0 = failure */ 219 if (twsi.field.r == 0) { 220 /* 221 * Lost arbitration : 0x38, 0x68, 0xB0, 0x78 222 * Core busy as slave: 0x80, 0x88, 0xA0, 0xA8, 0xB8, 0xC0, 0xC8 223 */ 224 if (*data == 0x38 || *data == 0x68 || *data == 0xB0 || 225 *data == 0x78 || *data == 0x80 || *data == 0x88 || 226 *data == 0xA0 || *data == 0xA8 || *data == 0xB8 || 227 *data == 0xC0 || *data == 0xC8) 228 if (nretries--) 229 goto again; 230 return EIO; 231 } 232 233 return 0; 234 } 235 236 int 237 octrtc_settime(struct todr_chip_handle *handle, struct timeval *tv) 238 { 239 struct clock_ymdhms dt; 240 int nretries = 2; 241 int rc, i; 242 uint8_t tod[8]; 243 uint8_t check; 244 245 clock_secs_to_ymdhms(tv->tv_sec, &dt); 246 247 DPRINTF(("settime: %d %d %d (%d) %02d:%02d:%02d\n", 248 dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_wday, 249 dt.dt_hour, dt.dt_min, dt.dt_sec)); 250 251 tod[0] = TOBCD(dt.dt_sec); 252 tod[1] = TOBCD(dt.dt_min); 253 tod[2] = TOBCD(dt.dt_hour); 254 tod[3] = TOBCD(dt.dt_wday + 1); 255 tod[4] = TOBCD(dt.dt_day); 256 tod[5] = TOBCD(dt.dt_mon); 257 if (dt.dt_year >= 2000) 258 tod[5] |= 0x80; 259 tod[6] = TOBCD(dt.dt_year % 100); 260 261 while (nretries--) { 262 for (i = 0; i < 7; i++) { 263 rc = octrtc_write(tod[i]); 264 if (rc) { 265 DPRINTF(("octrtc_write(%d) failed %d", i, rc)); 266 return EIO; 267 } 268 } 269 270 rc = octrtc_read(&check, 1); 271 if (rc) { 272 DPRINTF(("octrtc_read(check) failed %d", rc)); 273 return EIO; 274 } 275 276 if ((check & 0xf) == (tod[0] & 0xf)) 277 break; 278 } 279 280 return 0; 281 } 282 283 int 284 octrtc_write(uint8_t data) 285 { 286 union mio_tws_sw_twsi_reg twsi; 287 int npoll = 128; 288 289 twsi.reg = 0; 290 twsi.field.v = 1; 291 twsi.field.sovr = 1; 292 twsi.field.op = 1; 293 twsi.field.a = OCTRTC_REG; 294 twsi.field.d = data & 0xffffffff; 295 296 octeon_xkphys_write_8(MIO_TWS_SW_TWSI_EXT, 0); 297 octeon_xkphys_write_8(MIO_TWS_SW_TWSI, twsi.reg); 298 do { 299 delay(1000); 300 twsi.reg = octeon_xkphys_read_8(MIO_TWS_SW_TWSI); 301 } while (twsi.field.v); 302 303 /* Try to read back */ 304 while (npoll-- && octrtc_read(&data, 0)); 305 306 return npoll ? 0 : EIO; 307 } 308