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