MCPcopy Create free account
hub / github.com/F-Stack/f-stack / aw_rtc_settime

Function aw_rtc_settime

freebsd/arm/allwinner/aw_rtc.c:311–368  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

309}
310
311static int
312aw_rtc_settime(device_t dev, struct timespec *ts)
313{
314 struct aw_rtc_softc *sc = device_get_softc(dev);
315 struct clocktime ct;
316 uint32_t clk, rdate, rtime;
317
318 /* RTC resolution is 1 sec */
319 if (ts->tv_nsec >= HALF_OF_SEC_NS)
320 ts->tv_sec++;
321 ts->tv_nsec = 0;
322
323 clock_ts_to_ct(ts, &ct);
324
325 if ((ct.year < YEAR_MIN) || (ct.year > YEAR_MAX)) {
326 device_printf(dev, "could not set time, year out of range\n");
327 return (EINVAL);
328 }
329
330 for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
331 if (clk > RTC_TIMEOUT) {
332 device_printf(dev, "could not set time, RTC busy\n");
333 return (EINVAL);
334 }
335 DELAY(1);
336 }
337 /* reset time register to avoid unexpected date increment */
338 RTC_WRITE(sc, sc->conf->rtc_time, 0);
339
340 rdate = SET_DAY_VALUE(ct.day) | SET_MON_VALUE(ct.mon) |
341 SET_YEAR_VALUE(ct.year - YEAR_OFFSET) |
342 SET_LEAP_VALUE(IS_LEAP_YEAR(ct.year));
343
344 rtime = SET_SEC_VALUE(ct.sec) | SET_MIN_VALUE(ct.min) |
345 SET_HOUR_VALUE(ct.hour);
346
347 for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
348 if (clk > RTC_TIMEOUT) {
349 device_printf(dev, "could not set date, RTC busy\n");
350 return (EINVAL);
351 }
352 DELAY(1);
353 }
354 RTC_WRITE(sc, sc->conf->rtc_date, rdate);
355
356 for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
357 if (clk > RTC_TIMEOUT) {
358 device_printf(dev, "could not set time, RTC busy\n");
359 return (EINVAL);
360 }
361 DELAY(1);
362 }
363 RTC_WRITE(sc, sc->conf->rtc_time, rtime);
364
365 DELAY(RTC_TIMEOUT);
366
367 return (0);
368}

Callers

nothing calls this directly

Calls 4

device_get_softcFunction · 0.85
clock_ts_to_ctFunction · 0.85
device_printfFunction · 0.85
DELAYFunction · 0.50

Tested by

no test coverage detected