1 /* $OpenBSD: octrtc.c,v 1.7 2017/07/29 02:42:56 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 <mips64/dev/clockvar.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 cfdriver octrtc_cd = { 41 NULL, "octrtc", DV_DULL 42 }; 43 44 int octrtc_match(struct device *, void *, void *); 45 void octrtc_attach(struct device *, struct device *, void *); 46 47 void octrtc_gettime(void *, time_t, struct tod_time *); 48 int octrtc_read(uint8_t *, char); 49 50 void octrtc_settime(void *, struct tod_time *); 51 int octrtc_write(uint8_t); 52 53 54 struct cfattach octrtc_ca = { 55 sizeof(struct device), octrtc_match, octrtc_attach, 56 }; 57 58 59 union mio_tws_sw_twsi_reg { 60 uint64_t reg; 61 struct cvmx_mio_twsx_sw_twsi_s { 62 uint64_t v:1; /* Valid bit */ 63 uint64_t slonly:1; /* Slave Only Mode */ 64 uint64_t eia:1; /* Extended Internal Address */ 65 uint64_t op:4; /* Opcode field */ 66 uint64_t r:1; /* Read bit or result */ 67 uint64_t sovr:1; /* Size Override */ 68 uint64_t size:3; /* Size in bytes */ 69 uint64_t scr:2; /* Scratch, unused */ 70 uint64_t a:10; /* Address field */ 71 uint64_t ia:5; /* Internal Address */ 72 uint64_t eop_ia:3; /* Extra opcode */ 73 uint64_t d:32; /* Data Field */ 74 } field; 75 }; 76 77 78 static const uint16_t no_rtc_boards[] = { 79 BOARD_TYPE_UBIQUITI_E100, 80 BOARD_TYPE_UBIQUITI_E200, 81 BOARD_TYPE_UBIQUITI_E1000, 82 BOARD_TYPE_RHINOLABS_SHASTA 83 }; 84 85 int 86 octrtc_match(struct device *parent, void *match, void *aux) 87 { 88 struct mainbus_attach_args *maa = aux; 89 struct cfdata *cf = match; 90 int i; 91 92 if (strcmp(maa->maa_name, cf->cf_driver->cd_name) != 0) 93 return 0; 94 for (i = 0; i < nitems(no_rtc_boards); i++) 95 if (octeon_boot_info->board_type == no_rtc_boards[i]) 96 return 0; 97 return 1; 98 } 99 100 void 101 octrtc_attach(struct device *parent, struct device *self, void *aux) 102 { 103 struct octrtc_softc *sc = (struct octrtc_softc *)self; 104 105 sys_tod.tod_cookie = sc; 106 sys_tod.tod_get = octrtc_gettime; 107 sys_tod.tod_set = octrtc_settime; 108 109 printf(": DS1337\n"); 110 } 111 112 void 113 octrtc_gettime(void *cookie, time_t unused, struct tod_time *tt) 114 { 115 uint8_t tod[8]; 116 uint8_t check; 117 int i, rc; 118 119 int nretries = 2; 120 121 DPRINTF(("\nTOD: ")); 122 while (nretries--) { 123 rc = octrtc_read(&tod[0], 1); /* ia read */ 124 if (rc) { 125 DPRINTF(("octrtc_read(0) failed %d", rc)); 126 return; 127 } 128 129 for (i = 1; i < 8; i++) { 130 rc = octrtc_read(&tod[i], 0); /* current address */ 131 if (rc) { 132 DPRINTF(("octrtc_read(%d) failed %d", i, rc)); 133 return; 134 } 135 DPRINTF(("%#X ", tod[i])); 136 } 137 138 /* Check against time-wrap */ 139 rc = octrtc_read(&check, 1); /* ia read */ 140 if (rc) { 141 DPRINTF(("octrtc_read(check) failed %d", i, rc)); 142 return; 143 } 144 if ((check & 0xf) == (tod[0] & 0xf)) 145 break; 146 } 147 DPRINTF(("\n")); 148 149 DPRINTF(("Time: %d %d %d (%d) %02d:%02d:%02d\n", 150 ((tod[5] & 0x80) ? 2000 : 1900) + FROMBCD(tod[6]), /* year */ 151 FROMBCD(tod[5] & 0x1f), /* month */ 152 FROMBCD(tod[4] & 0x3f), /* day */ 153 (tod[3] & 0x7), /* day of the week */ 154 FROMBCD(tod[2] & 0x3f), /* hour */ 155 FROMBCD(tod[1] & 0x7f), /* minute */ 156 FROMBCD(tod[0] & 0x7f))); /* second */ 157 158 tt->year = ((tod[5] & 0x80) ? 100 : 0) + FROMBCD(tod[6]); 159 tt->mon = FROMBCD(tod[5] & 0x1f); 160 tt->day = FROMBCD(tod[4] & 0x3f); 161 tt->dow = (tod[3] & 0x7); 162 tt->hour = FROMBCD(tod[2] & 0x3f); 163 if ((tod[2] & 0x40) && (tod[2] & 0x20)) /* adjust AM/PM format */ 164 tt->hour = (tt->hour + 12) % 24; 165 tt->min = FROMBCD(tod[1] & 0x7f); 166 tt->sec = FROMBCD(tod[0] & 0x7f); 167 } 168 169 int 170 octrtc_read(uint8_t *data, char ia) 171 { 172 int nretries = 5; 173 union mio_tws_sw_twsi_reg twsi; 174 175 again: 176 twsi.reg = 0; 177 twsi.field.v = 1; 178 twsi.field.r = 1; 179 twsi.field.sovr = 1; 180 twsi.field.a = OCTRTC_REG; 181 if (ia) { 182 twsi.field.op = 1; 183 } 184 185 octeon_xkphys_write_8(MIO_TWS_SW_TWSI, twsi.reg); 186 /* The 1st bit is cleared when the operation is complete */ 187 do { 188 delay(1000); 189 twsi.reg = octeon_xkphys_read_8(MIO_TWS_SW_TWSI); 190 } while (twsi.field.v); 191 DPRINTF(("%#llX ", twsi.reg)); 192 193 /* 194 * The data field is in the upper 32 bits and we're only 195 * interested in the first byte. 196 */ 197 *data = twsi.field.d & 0xff; 198 199 /* 8th bit is the read result: 1 = success, 0 = failure */ 200 if (twsi.field.r == 0) { 201 /* 202 * Lost arbitration : 0x38, 0x68, 0xB0, 0x78 203 * Core busy as slave: 0x80, 0x88, 0xA0, 0xA8, 0xB8, 0xC0, 0xC8 204 */ 205 if (*data == 0x38 || *data == 0x68 || *data == 0xB0 || 206 *data == 0x78 || *data == 0x80 || *data == 0x88 || 207 *data == 0xA0 || *data == 0xA8 || *data == 0xB8 || 208 *data == 0xC0 || *data == 0xC8) 209 if (nretries--) 210 goto again; 211 return EIO; 212 } 213 214 return 0; 215 } 216 217 void 218 octrtc_settime(void *cookie, struct tod_time *tt) 219 { 220 int nretries = 2; 221 int rc, i; 222 uint8_t tod[8]; 223 uint8_t check; 224 225 DPRINTF(("settime: %d %d %d (%d) %02d:%02d:%02d\n", 226 tt->year, tt->mon, tt->day, tt->dow, 227 tt->hour, tt->min, tt->sec)); 228 229 tod[0] = TOBCD(tt->sec); 230 tod[1] = TOBCD(tt->min); 231 tod[2] = TOBCD(tt->hour); 232 tod[3] = TOBCD(tt->dow); 233 tod[4] = TOBCD(tt->day); 234 tod[5] = TOBCD(tt->mon); 235 if (tt->year >= 100) 236 tod[5] |= 0x80; 237 tod[6] = TOBCD(tt->year % 100); 238 239 while (nretries--) { 240 for (i = 0; i < 7; i++) { 241 rc = octrtc_write(tod[i]); 242 if (rc) { 243 DPRINTF(("octrtc_write(%d) failed %d", i, rc)); 244 return; 245 } 246 } 247 248 rc = octrtc_read(&check, 1); 249 if (rc) { 250 DPRINTF(("octrtc_read(check) failed %d", rc)); 251 return; 252 } 253 254 if ((check & 0xf) == (tod[0] & 0xf)) 255 break; 256 } 257 } 258 259 int 260 octrtc_write(uint8_t data) 261 { 262 union mio_tws_sw_twsi_reg twsi; 263 int npoll = 128; 264 265 twsi.reg = 0; 266 twsi.field.v = 1; 267 twsi.field.sovr = 1; 268 twsi.field.op = 1; 269 twsi.field.a = OCTRTC_REG; 270 twsi.field.d = data & 0xffffffff; 271 272 octeon_xkphys_write_8(MIO_TWS_SW_TWSI_EXT, 0); 273 octeon_xkphys_write_8(MIO_TWS_SW_TWSI, twsi.reg); 274 do { 275 delay(1000); 276 twsi.reg = octeon_xkphys_read_8(MIO_TWS_SW_TWSI); 277 } while (twsi.field.v); 278 279 /* Try to read back */ 280 while (npoll-- && octrtc_read(&data, 0)); 281 282 return npoll ? 0 : EIO; 283 } 284