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