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