Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/rtc/rtc-ds1307.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
4
 *
5
 *  This code was ported from linux-3.15 kernel by Antony Pavlov.
6
 *
7
 *  Copyright (C) 2005 James Chapman (ds1337 core)
8
 *  Copyright (C) 2006 David Brownell
9
 *  Copyright (C) 2009 Matthias Fuchs (rx8025 support)
10
 *  Copyright (C) 2012 Bertrand Achard (nvram access fixes)
11
 */
12
13
#include <common.h>
14
#include <driver.h>
15
#include <xfuncs.h>
16
#include <malloc.h>
17
#include <errno.h>
18
#include <i2c/i2c.h>
19
#include <rtc.h>
20
#include <linux/rtc.h>
21
#include <linux/bcd.h>
22
23
/*
24
 * We can't determine type by probing, but if we expect pre-Linux code
25
 * to have set the chip up as a clock (turning on the oscillator and
26
 * setting the date and time), Linux can ignore the non-clock features.
27
 * That's a natural job for a factory or repair bench.
28
 */
29
enum ds_type {
30
  ds_1307,
31
  ds_1337,
32
  ds_1338,
33
  ds_1341,
34
  last_ds_type /* always last */
35
};
36
37
/* RTC registers don't differ much, except for the century flag */
38
0
#define DS1307_REG_SECS   0x00  /* 00-59 */
39
0
# define DS1307_BIT_CH    0x80
40
# define DS1340_BIT_nEOSC   0x80
41
0
#define DS1307_REG_MIN    0x01  /* 00-59 */
42
0
#define DS1307_REG_HOUR   0x02  /* 00-23, or 1-12{am,pm} */
43
0
# define DS1307_BIT_12HR    0x40  /* in REG_HOUR */
44
0
# define DS1307_BIT_PM    0x20  /* in REG_HOUR */
45
# define DS1340_BIT_CENTURY_EN  0x80  /* in REG_HOUR */
46
# define DS1340_BIT_CENTURY 0x40  /* in REG_HOUR */
47
0
#define DS1307_REG_WDAY   0x03  /* 01-07 */
48
0
#define DS1307_REG_MDAY   0x04  /* 01-31 */
49
0
#define DS1307_REG_MONTH  0x05  /* 01-12 */
50
0
# define DS1337_BIT_CENTURY 0x80  /* in REG_MONTH */
51
0
#define DS1307_REG_YEAR   0x06  /* 00-99 */
52
53
/*
54
 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
55
 * start at 7, and they differ a LOT. Only control and status matter for
56
 * basic RTC date and time functionality; be careful using them.
57
 */
58
0
#define DS1307_REG_CONTROL  0x07    /* or ds1338, 1308 */
59
# define DS1307_BIT_OUT   0x80
60
0
# define DS1308_BIT_ECLK    0x40
61
0
# define DS1338_BIT_OSF   0x20
62
0
# define DS1307_BIT_SQWE    0x10
63
# define DS1308_BIT_LOS   0x08
64
0
# define DS1308_BIT_BBCLK   0x04
65
0
# define DS1307_BIT_RS1   0x02
66
0
# define DS1307_BIT_RS0   0x01
67
0
#define DS1337_REG_CONTROL  0x0e
68
0
# define DS1337_BIT_nEOSC   0x80
69
# define DS1339_BIT_BBSQI   0x20
70
0
# define DS1341_BIT_EGFIL   0x20
71
# define DS3231_BIT_BBSQW   0x40 /* same as BBSQI */
72
# define DS1337_BIT_RS2   0x10
73
# define DS1337_BIT_RS1   0x08
74
0
# define DS1337_BIT_INTCN   0x04
75
0
# define DS1337_BIT_A2IE    0x02
76
0
# define DS1337_BIT_A1IE    0x01
77
#define DS1340_REG_CONTROL  0x07
78
# define DS1340_BIT_OUT   0x80
79
# define DS1340_BIT_FT    0x40
80
# define DS1340_BIT_CALIB_SIGN  0x20
81
# define DS1340_M_CALIBRATION 0x1f
82
#define DS1340_REG_FLAG   0x09
83
# define DS1340_BIT_OSF   0x80
84
0
#define DS1337_REG_STATUS 0x0f
85
0
# define DS1337_BIT_OSF   0x80
86
0
# define DS1341_BIT_DOSF    0x40
87
0
# define DS1341_BIT_ECLK    0x04
88
# define DS1337_BIT_A2I   0x02
89
# define DS1337_BIT_A1I   0x01
90
91
92
struct ds1307 {
93
  struct rtc_device rtc;
94
  u8      offset; /* register's offset */
95
  u8      regs[11];
96
  enum ds_type    type;
97
  unsigned long   flags;
98
  struct i2c_client *client;
99
  s32 (*read_block_data)(const struct i2c_client *client, u8 command,
100
             u8 length, u8 *values);
101
  s32 (*write_block_data)(const struct i2c_client *client, u8 command,
102
        u8 length, const u8 *values);
103
};
104
105
static struct platform_device_id ds1307_id[] = {
106
  { "ds1307", ds_1307 },
107
  { "ds1337", ds_1337 },
108
  { "ds1308", ds_1338 }, /* Difference 1308 to 1338 irrelevant */
109
  { "ds1338", ds_1338 },
110
  { "ds1341", ds_1341 },
111
  { "ds3231", ds_1337 },
112
  { "pt7c4338", ds_1307 },
113
  { }
114
};
115
116
/*----------------------------------------------------------------------*/
117
118
0
#define BLOCK_DATA_MAX_TRIES 10
119
120
static s32 ds1307_read_block_data_once(const struct i2c_client *client,
121
               u8 command, u8 length, u8 *values)
122
0
{
123
0
  s32 i, data;
124
125
0
  for (i = 0; i < length; i++) {
126
0
    data = i2c_smbus_read_byte_data(client, command + i);
127
0
    if (data < 0)
128
0
      return data;
129
0
    values[i] = data;
130
0
  }
131
0
  return i;
132
0
}
133
134
static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
135
          u8 length, u8 *values)
136
0
{
137
0
  u8 oldvalues[255];
138
0
  s32 ret;
139
0
  int tries = 0;
140
141
0
  dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
142
0
  ret = ds1307_read_block_data_once(client, command, length, values);
143
0
  if (ret < 0)
144
0
    return ret;
145
0
  do {
146
0
    if (++tries > BLOCK_DATA_MAX_TRIES) {
147
0
      dev_err(&client->dev,
148
0
        "ds1307_read_block_data failed\n");
149
0
      return -EIO;
150
0
    }
151
0
    memcpy(oldvalues, values, length);
152
0
    ret = ds1307_read_block_data_once(client, command, length,
153
0
              values);
154
0
    if (ret < 0)
155
0
      return ret;
156
0
  } while (memcmp(oldvalues, values, length));
157
0
  return length;
158
0
}
159
160
static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
161
           u8 length, const u8 *values)
162
0
{
163
0
  u8 currvalues[255];
164
0
  int tries = 0;
165
166
0
  dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
167
0
  do {
168
0
    s32 i, ret;
169
170
0
    if (++tries > BLOCK_DATA_MAX_TRIES) {
171
0
      dev_err(&client->dev,
172
0
        "ds1307_write_block_data failed\n");
173
0
      return -EIO;
174
0
    }
175
0
    for (i = 0; i < length; i++) {
176
0
      ret = i2c_smbus_write_byte_data(client, command + i,
177
0
              values[i]);
178
0
      if (ret < 0)
179
0
        return ret;
180
0
    }
181
0
    ret = ds1307_read_block_data_once(client, command, length,
182
0
              currvalues);
183
0
    if (ret < 0)
184
0
      return ret;
185
0
  } while (memcmp(currvalues, values, length));
186
0
  return length;
187
0
}
188
189
static inline struct ds1307 *to_ds1307_priv(struct rtc_device *rtcdev)
190
0
{
191
0
  return container_of(rtcdev, struct ds1307, rtc);
192
0
}
193
194
static int ds1307_get_time(struct rtc_device *rtcdev, struct rtc_time *t)
195
0
{
196
0
  struct device *dev = rtcdev->dev;
197
0
  struct ds1307 *ds1307 = to_ds1307_priv(rtcdev);
198
0
  int   tmp;
199
200
  /* read the RTC date and time registers all at once */
201
0
  tmp = ds1307->read_block_data(ds1307->client,
202
0
    ds1307->offset, 7, ds1307->regs);
203
0
  if (tmp != 7) {
204
0
    dev_err(dev, "%s error %d\n", "read", tmp);
205
0
    return -EIO;
206
0
  }
207
208
0
  dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
209
210
0
  t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
211
0
  t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
212
0
  tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
213
0
  t->tm_hour = bcd2bin(tmp);
214
0
  t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
215
0
  t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
216
0
  tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
217
0
  t->tm_mon = bcd2bin(tmp) - 1;
218
219
  /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
220
0
  t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
221
222
0
  dev_dbg(dev, "%s secs=%d, mins=%d, "
223
0
    "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
224
0
    "read", t->tm_sec, t->tm_min,
225
0
    t->tm_hour, t->tm_mday,
226
0
    t->tm_mon, t->tm_year, t->tm_wday);
227
228
  /* initial clock setting can be undefined */
229
0
  return rtc_valid_tm(t);
230
0
}
231
232
static int ds1307_set_time(struct rtc_device *rtcdev, struct rtc_time *t)
233
0
{
234
0
  struct device *dev = rtcdev->dev;
235
0
  struct ds1307 *ds1307 = to_ds1307_priv(rtcdev);
236
0
  int   result;
237
0
  int   tmp;
238
0
  u8    *buf = ds1307->regs;
239
240
0
  dev_dbg(dev, "%s secs=%d, mins=%d, "
241
0
    "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
242
0
    "write", t->tm_sec, t->tm_min,
243
0
    t->tm_hour, t->tm_mday,
244
0
    t->tm_mon, t->tm_year, t->tm_wday);
245
246
0
  buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
247
0
  buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
248
0
  buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
249
0
  buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
250
0
  buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
251
0
  buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
252
253
  /* assume 20YY not 19YY */
254
0
  tmp = t->tm_year - 100;
255
0
  buf[DS1307_REG_YEAR] = bin2bcd(tmp);
256
257
0
  switch (ds1307->type) {
258
0
  case ds_1337:
259
0
  case ds_1341:
260
0
    buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
261
0
    break;
262
0
  default:
263
0
    break;
264
0
  }
265
266
267
0
  dev_dbg(dev, "%s: %7ph\n", "write", buf);
268
269
0
  result = ds1307->write_block_data(ds1307->client,
270
0
    ds1307->offset, 7, buf);
271
0
  if (result < 0) {
272
0
    dev_err(dev, "%s error %d\n", "write", result);
273
0
    return result;
274
0
  }
275
0
  return 0;
276
0
}
277
278
static const struct rtc_class_ops ds13xx_rtc_ops = {
279
  .read_time  = ds1307_get_time,
280
  .set_time = ds1307_set_time,
281
};
282
283
static int ds1307_probe(struct device *dev)
284
0
{
285
0
  struct i2c_client *client = to_i2c_client(dev);
286
0
  struct ds1307   *ds1307;
287
0
  int     err = -ENODEV;
288
0
  int     tmp;
289
0
  unsigned char   *buf;
290
0
  unsigned long driver_data;
291
0
  const struct device_node *np = dev->of_node;
292
293
0
  driver_data = (uintptr_t)device_get_match_data(dev);
294
295
0
  ds1307 = xzalloc(sizeof(struct ds1307));
296
297
0
  ds1307->client = client;
298
0
  ds1307->type = driver_data;
299
300
0
  buf = ds1307->regs;
301
302
0
  ds1307->read_block_data = ds1307_read_block_data;
303
0
  ds1307->write_block_data = ds1307_write_block_data;
304
305
306
0
  switch (ds1307->type) {
307
0
  case ds_1337:
308
0
  case ds_1341:
309
    /* get registers that the "rtc" read below won't read... */
310
0
    tmp = ds1307->read_block_data(ds1307->client,
311
0
                DS1337_REG_CONTROL, 2, buf);
312
313
0
    if (tmp != 2) {
314
0
      dev_dbg(&client->dev, "read error %d\n", tmp);
315
0
      err = -EIO;
316
0
      goto exit;
317
0
    }
318
319
    /* oscillator off?  turn it on, so clock can tick. */
320
0
    if (ds1307->regs[0] & DS1337_BIT_nEOSC)
321
0
      ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
322
323
324
    /*
325
      Make sure no alarm interrupts or square wave signals
326
      are produced by the chip while we are in
327
      bootloader. We do this by configuring the RTC to
328
      generate alarm interrupts (thus disabling square
329
      wave generation), but disabling each individual
330
      alarm interrupt source
331
     */
332
0
    ds1307->regs[0] |= DS1337_BIT_INTCN;
333
0
    ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
334
335
0
    i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
336
0
            ds1307->regs[0]);
337
338
0
    if (ds1307->type == ds_1341) {
339
      /*
340
       * For the above to be true, DS1341 also has to have
341
       * ECLK bit set to 0
342
       */
343
0
      ds1307->regs[1] &= ~DS1341_BIT_ECLK;
344
345
      /*
346
       * Let's set additional RTC bits to
347
       * facilitate maximum power saving.
348
       */
349
0
      ds1307->regs[1] |=  DS1341_BIT_DOSF;
350
0
      ds1307->regs[0] &= ~DS1341_BIT_EGFIL;
351
352
0
      i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
353
0
              ds1307->regs[0]);
354
0
      i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
355
0
              ds1307->regs[1]);
356
0
    }
357
358
359
    /* oscillator fault?  clear flag, and warn */
360
0
    if (ds1307->regs[1] & DS1337_BIT_OSF) {
361
0
      i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
362
0
        ds1307->regs[1] & ~DS1337_BIT_OSF);
363
0
      dev_warn(&client->dev, "SET TIME!\n");
364
0
    }
365
366
0
  default:
367
0
    break;
368
0
  }
369
370
0
read_rtc:
371
  /* read RTC registers */
372
0
  tmp = ds1307->read_block_data(client, ds1307->offset, 8, buf);
373
0
  if (tmp != 8) {
374
0
    dev_dbg(&client->dev, "read error %d\n", tmp);
375
0
    err = -EIO;
376
0
    goto exit;
377
0
  }
378
379
  /* Configure clock using OF data if available */
380
0
  if (IS_ENABLED(CONFIG_OFDEVICE) && np) {
381
0
    u8 control = ds1307->regs[DS1307_REG_CONTROL];
382
0
    u32 rate = 0;
383
384
0
    if (of_property_read_bool(np, "ext-clock-input"))
385
0
      control |= DS1308_BIT_ECLK;
386
0
    else
387
0
      control &= ~DS1308_BIT_ECLK;
388
389
0
    if (of_property_read_bool(np, "ext-clock-output"))
390
0
      control |= DS1307_BIT_SQWE;
391
0
    else
392
0
      control &= ~DS1307_BIT_SQWE;
393
394
0
    if (of_property_read_bool(np, "ext-clock-bb"))
395
0
      control |= DS1308_BIT_BBCLK;
396
0
    else
397
0
      control &= ~DS1308_BIT_BBCLK;
398
399
0
    control &= ~(DS1307_BIT_RS1 | DS1307_BIT_RS0);
400
0
    of_property_read_u32(np, "ext-clock-rate", &rate);
401
0
    switch (rate) {
402
0
      default:
403
0
      case 1:     control |= 0; break;
404
0
      case 50:    control |= 1; break;
405
0
      case 60:    control |= 2; break;
406
0
      case 4096:  control |= 1; break;
407
0
      case 8192:  control |= 2; break;
408
0
      case 32768: control |= 3; break;
409
0
    }
410
0
    dev_dbg(&client->dev, "control reg: 0x%02x\n", control);
411
412
0
    if (ds1307->regs[DS1307_REG_CONTROL] != control) {
413
0
      i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL, control);
414
0
      ds1307->regs[DS1307_REG_CONTROL]  = control;
415
0
    }
416
0
  }
417
418
  /*
419
   * minimal sanity checking; some chips (like DS1340) don't
420
   * specify the extra bits as must-be-zero, but there are
421
   * still a few values that are clearly out-of-range.
422
   */
423
0
  tmp = ds1307->regs[DS1307_REG_SECS];
424
0
  switch (ds1307->type) {
425
0
  case ds_1307:
426
    /* clock halted?  turn it on, so clock can tick. */
427
0
    if (tmp & DS1307_BIT_CH) {
428
0
      i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
429
0
      dev_warn(&client->dev, "SET TIME!\n");
430
0
      goto read_rtc;
431
0
    }
432
0
    break;
433
0
  case ds_1338:
434
    /* clock halted?  turn it on, so clock can tick. */
435
0
    if (tmp & DS1307_BIT_CH)
436
0
      i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
437
438
    /* oscillator fault?  clear flag, and warn */
439
0
    if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
440
0
      i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
441
0
          ds1307->regs[DS1307_REG_CONTROL]
442
0
          & ~DS1338_BIT_OSF);
443
0
      dev_warn(&client->dev, "SET TIME!\n");
444
0
      goto read_rtc;
445
0
    }
446
0
    break;
447
0
  default:
448
0
    break;
449
0
  }
450
451
0
  tmp = ds1307->regs[DS1307_REG_HOUR];
452
0
  switch (ds1307->type) {
453
0
  default:
454
0
    if (!(tmp & DS1307_BIT_12HR))
455
0
      break;
456
457
    /*
458
     * Be sure we're in 24 hour mode.  Multi-master systems
459
     * take note...
460
     */
461
0
    tmp = bcd2bin(tmp & 0x1f);
462
0
    if (tmp == 12)
463
0
      tmp = 0;
464
0
    if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
465
0
      tmp += 12;
466
0
    i2c_smbus_write_byte_data(client,
467
0
        ds1307->offset + DS1307_REG_HOUR,
468
0
        bin2bcd(tmp));
469
0
  }
470
471
0
  ds1307->rtc.ops = &ds13xx_rtc_ops;
472
0
  ds1307->rtc.dev = dev;
473
474
0
  err = rtc_register(&ds1307->rtc);
475
476
0
exit:
477
0
  if (err)
478
0
    free(ds1307);
479
0
  return err;
480
0
}
481
482
static struct driver ds1307_driver = {
483
  .name = "rtc-ds1307",
484
  .probe    = ds1307_probe,
485
  .id_table = ds1307_id,
486
};
487
device_i2c_driver(ds1307_driver);