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