Blame view
drivers/rtc/ds1306.c
13.1 KB
affae2bff Initial revision |
1 2 3 |
/* * (C) Copyright 2002 SIXNET, dge@sixnetio.com. * |
ec4c544be Patches by Stepha... |
4 5 6 |
* (C) Copyright 2004, Li-Pro.Net <www.li-pro.net> * Stephan Linz <linz@li-pro.net> * |
1a4596601 Add GPL-2.0+ SPDX... |
7 |
* SPDX-License-Identifier: GPL-2.0+ |
affae2bff Initial revision |
8 9 10 |
*/ /* |
ec4c544be Patches by Stepha... |
11 12 13 14 15 |
* Date & Time support for DS1306 RTC using SPI: * * - SXNI855T: it uses its own soft SPI here in this file * - all other: use the external spi_xfer() function * (see include/spi.h) |
affae2bff Initial revision |
16 17 18 19 20 |
*/ #include <common.h> #include <command.h> #include <rtc.h> |
ec4c544be Patches by Stepha... |
21 |
#include <spi.h> |
affae2bff Initial revision |
22 |
|
871c18dd3 rtc: Clean driver... |
23 |
#if defined(CONFIG_CMD_DATE) |
affae2bff Initial revision |
24 |
|
ec4c544be Patches by Stepha... |
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#define RTC_SECONDS 0x00 #define RTC_MINUTES 0x01 #define RTC_HOURS 0x02 #define RTC_DAY_OF_WEEK 0x03 #define RTC_DATE_OF_MONTH 0x04 #define RTC_MONTH 0x05 #define RTC_YEAR 0x06 #define RTC_SECONDS_ALARM0 0x07 #define RTC_MINUTES_ALARM0 0x08 #define RTC_HOURS_ALARM0 0x09 #define RTC_DAY_OF_WEEK_ALARM0 0x0a #define RTC_SECONDS_ALARM1 0x0b #define RTC_MINUTES_ALARM1 0x0c #define RTC_HOURS_ALARM1 0x0d #define RTC_DAY_OF_WEEK_ALARM1 0x0e #define RTC_CONTROL 0x0f #define RTC_STATUS 0x10 #define RTC_TRICKLE_CHARGER 0x11 #define RTC_USER_RAM_BASE 0x20 |
ec4c544be Patches by Stepha... |
48 49 50 51 52 53 |
/* ************************************************************************* */ #ifdef CONFIG_SXNI855T /* !!! SHOULD BE CHANGED TO NEW CODE !!! */ static void soft_spi_send (unsigned char n); static unsigned char soft_spi_read (void); static void init_spi (void); |
affae2bff Initial revision |
54 55 56 57 58 59 60 61 62 63 64 65 66 |
/*----------------------------------------------------------------------- * Definitions */ #define PB_SPISCK 0x00000002 /* PB 30 */ #define PB_SPIMOSI 0x00000004 /* PB 29 */ #define PB_SPIMISO 0x00000008 /* PB 28 */ #define PB_SPI_CE 0x00010000 /* PB 15 */ /* ------------------------------------------------------------------------- */ /* read clock time from DS1306 and return it in *tmp */ |
b73a19e16 LWMON5: POST RTC fix |
67 |
int rtc_get (struct rtc_time *tmp) |
affae2bff Initial revision |
68 |
{ |
6d0f6bcf3 rename CFG_ macro... |
69 |
volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
ec4c544be Patches by Stepha... |
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
unsigned char spi_byte; /* Data Byte */ init_spi (); /* set port B for software SPI */ /* Now we can enable the DS1306 RTC */ immap->im_cpm.cp_pbdat |= PB_SPI_CE; udelay (10); /* Shift out the address (0) of the time in the Clock Chip */ soft_spi_send (0); /* Put the clock readings into the rtc_time structure */ tmp->tm_sec = bcd2bin (soft_spi_read ()); /* Read seconds */ tmp->tm_min = bcd2bin (soft_spi_read ()); /* Read minutes */ /* Hours are trickier */ spi_byte = soft_spi_read (); /* Read Hours into temporary value */ if (spi_byte & 0x40) { /* 12 hour mode bit is set (time is in 1-12 format) */ if (spi_byte & 0x20) { /* since PM we add 11 to get 0-23 for hours */ tmp->tm_hour = (bcd2bin (spi_byte & 0x1F)) + 11; } else { /* since AM we subtract 1 to get 0-23 for hours */ tmp->tm_hour = (bcd2bin (spi_byte & 0x1F)) - 1; } } else { /* Otherwise, 0-23 hour format */ tmp->tm_hour = (bcd2bin (spi_byte & 0x3F)); } |
affae2bff Initial revision |
100 |
|
ec4c544be Patches by Stepha... |
101 102 103 |
soft_spi_read (); /* Read and discard Day of week */ tmp->tm_mday = bcd2bin (soft_spi_read ()); /* Read Day of the Month */ tmp->tm_mon = bcd2bin (soft_spi_read ()); /* Read Month */ |
affae2bff Initial revision |
104 |
|
ec4c544be Patches by Stepha... |
105 106 |
/* Read Year and convert to this century */ tmp->tm_year = bcd2bin (soft_spi_read ()) + 2000; |
affae2bff Initial revision |
107 |
|
ec4c544be Patches by Stepha... |
108 109 110 |
/* Now we can disable the DS1306 RTC */ immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */ udelay (10); |
affae2bff Initial revision |
111 |
|
ec4c544be Patches by Stepha... |
112 |
GregorianDay (tmp); /* Determine the day of week */ |
affae2bff Initial revision |
113 |
|
ec4c544be Patches by Stepha... |
114 115 116 117 |
debug ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d ", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
b73a19e16 LWMON5: POST RTC fix |
118 119 |
return 0; |
ec4c544be Patches by Stepha... |
120 121 122 123 124 |
} /* ------------------------------------------------------------------------- */ /* set clock time in DS1306 RTC and in MPC8xx RTC */ |
d1e231941 rtc: allow rtc_se... |
125 |
int rtc_set (struct rtc_time *tmp) |
ec4c544be Patches by Stepha... |
126 |
{ |
6d0f6bcf3 rename CFG_ macro... |
127 |
volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
ec4c544be Patches by Stepha... |
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
init_spi (); /* set port B for software SPI */ /* Now we can enable the DS1306 RTC */ immap->im_cpm.cp_pbdat |= PB_SPI_CE; /* Enable DS1306 Chip */ udelay (10); /* First disable write protect in the clock chip control register */ soft_spi_send (0x8F); /* send address of the control register */ soft_spi_send (0x00); /* send control register contents */ /* Now disable the DS1306 to terminate the write */ immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; udelay (10); /* Now enable the DS1306 to initiate a new write */ immap->im_cpm.cp_pbdat |= PB_SPI_CE; udelay (10); /* Next, send the address of the clock time write registers */ soft_spi_send (0x80); /* send address of the first time register */ /* Use Burst Mode to send all of the time data to the clock */ bin2bcd (tmp->tm_sec); soft_spi_send (bin2bcd (tmp->tm_sec)); /* Send Seconds */ soft_spi_send (bin2bcd (tmp->tm_min)); /* Send Minutes */ soft_spi_send (bin2bcd (tmp->tm_hour)); /* Send Hour */ soft_spi_send (bin2bcd (tmp->tm_wday)); /* Send Day of the Week */ soft_spi_send (bin2bcd (tmp->tm_mday)); /* Send Day of Month */ soft_spi_send (bin2bcd (tmp->tm_mon)); /* Send Month */ soft_spi_send (bin2bcd (tmp->tm_year - 2000)); /* Send Year */ /* Now we can disable the Clock chip to terminate the burst write */ immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */ udelay (10); /* Now we can enable the Clock chip to initiate a new write */ immap->im_cpm.cp_pbdat |= PB_SPI_CE; /* Enable DS1306 Chip */ udelay (10); /* First we Enable write protect in the clock chip control register */ soft_spi_send (0x8F); /* send address of the control register */ soft_spi_send (0x40); /* send out Control Register contents */ /* Now disable the DS1306 */ immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */ udelay (10); /* Set standard MPC8xx clock to the same time so Linux will * see the time even if it doesn't have a DS1306 clock driver. * This helps with experimenting with standard kernels. */ { ulong tim; tim = mktime (tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); immap->im_sitk.sitk_rtck = KAPWR_KEY; immap->im_sit.sit_rtc = tim; |
affae2bff Initial revision |
188 |
} |
affae2bff Initial revision |
189 |
|
ec4c544be Patches by Stepha... |
190 191 192 193 |
debug ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d ", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
d1e231941 rtc: allow rtc_se... |
194 195 |
return 0; |
ec4c544be Patches by Stepha... |
196 |
} |
affae2bff Initial revision |
197 |
|
ec4c544be Patches by Stepha... |
198 |
/* ------------------------------------------------------------------------- */ |
affae2bff Initial revision |
199 |
|
ec4c544be Patches by Stepha... |
200 201 202 |
/* Initialize Port B for software SPI */ static void init_spi (void) { |
6d0f6bcf3 rename CFG_ macro... |
203 |
volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
affae2bff Initial revision |
204 |
|
ec4c544be Patches by Stepha... |
205 206 |
/* Force output pins to begin at logic 0 */ immap->im_cpm.cp_pbdat &= ~(PB_SPI_CE | PB_SPIMOSI | PB_SPISCK); |
affae2bff Initial revision |
207 |
|
ec4c544be Patches by Stepha... |
208 209 210 211 212 |
/* Set these 3 signals as outputs */ immap->im_cpm.cp_pbdir |= (PB_SPIMOSI | PB_SPI_CE | PB_SPISCK); immap->im_cpm.cp_pbdir &= ~PB_SPIMISO; /* Make MISO pin an input */ udelay (10); |
affae2bff Initial revision |
213 214 215 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
216 217 |
/* NOTE: soft_spi_send() assumes that the I/O lines are configured already */ static void soft_spi_send (unsigned char n) |
affae2bff Initial revision |
218 |
{ |
6d0f6bcf3 rename CFG_ macro... |
219 |
volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
ec4c544be Patches by Stepha... |
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
unsigned char bitpos; /* bit position to receive */ unsigned char i; /* Loop Control */ /* bit position to send, start with most significant bit */ bitpos = 0x80; /* Send 8 bits to software SPI */ for (i = 0; i < 8; i++) { /* Loop for 8 bits */ immap->im_cpm.cp_pbdat |= PB_SPISCK; /* Raise SCK */ if (n & bitpos) immap->im_cpm.cp_pbdat |= PB_SPIMOSI; /* Set MOSI to 1 */ else immap->im_cpm.cp_pbdat &= ~PB_SPIMOSI; /* Set MOSI to 0 */ udelay (10); immap->im_cpm.cp_pbdat &= ~PB_SPISCK; /* Lower SCK */ udelay (10); bitpos >>= 1; /* Shift for next bit position */ } |
affae2bff Initial revision |
241 242 243 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
244 245 |
/* NOTE: soft_spi_read() assumes that the I/O lines are configured already */ static unsigned char soft_spi_read (void) |
affae2bff Initial revision |
246 |
{ |
6d0f6bcf3 rename CFG_ macro... |
247 |
volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
ec4c544be Patches by Stepha... |
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
unsigned char spi_byte = 0; /* Return value, assume success */ unsigned char bitpos; /* bit position to receive */ unsigned char i; /* Loop Control */ /* bit position to receive, start with most significant bit */ bitpos = 0x80; /* Read 8 bits here */ for (i = 0; i < 8; i++) { /* Do 8 bits in loop */ immap->im_cpm.cp_pbdat |= PB_SPISCK; /* Raise SCK */ udelay (10); if (immap->im_cpm.cp_pbdat & PB_SPIMISO) /* Get a bit of data */ spi_byte |= bitpos; /* Set data accordingly */ immap->im_cpm.cp_pbdat &= ~PB_SPISCK; /* Lower SCK */ udelay (10); bitpos >>= 1; /* Shift for next bit position */ } return spi_byte; /* Return the byte read */ |
affae2bff Initial revision |
268 269 270 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
271 |
void rtc_reset (void) |
affae2bff Initial revision |
272 |
{ |
ec4c544be Patches by Stepha... |
273 274 275 276 277 |
return; /* nothing to do */ } #else /* not CONFIG_SXNI855T */ /* ************************************************************************* */ |
3f85ce278 * CVS add missing... |
278 279 |
static unsigned char rtc_read (unsigned char reg); static void rtc_write (unsigned char reg, unsigned char val); |
d255bb0e7 SPI API improvements |
280 |
static struct spi_slave *slave; |
ec4c544be Patches by Stepha... |
281 |
/* read clock time from DS1306 and return it in *tmp */ |
b73a19e16 LWMON5: POST RTC fix |
282 |
int rtc_get (struct rtc_time *tmp) |
ec4c544be Patches by Stepha... |
283 284 |
{ unsigned char sec, min, hour, mday, wday, mon, year; |
d255bb0e7 SPI API improvements |
285 286 287 288 289 290 291 |
/* * Assuming Vcc = 2.0V (lowest speed) * * REVISIT: If we add an rtc_init() function we can do this * step just once. */ if (!slave) { |
6d0f6bcf3 rename CFG_ macro... |
292 |
slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000, |
d255bb0e7 SPI API improvements |
293 294 295 296 297 298 299 |
SPI_MODE_3 | SPI_CS_HIGH); if (!slave) return; } if (spi_claim_bus(slave)) return; |
ec4c544be Patches by Stepha... |
300 301 302 303 304 305 306 |
sec = rtc_read (RTC_SECONDS); min = rtc_read (RTC_MINUTES); hour = rtc_read (RTC_HOURS); mday = rtc_read (RTC_DATE_OF_MONTH); wday = rtc_read (RTC_DAY_OF_WEEK); mon = rtc_read (RTC_MONTH); year = rtc_read (RTC_YEAR); |
d255bb0e7 SPI API improvements |
307 |
spi_release_bus(slave); |
ec4c544be Patches by Stepha... |
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
debug ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x " "hr: %02x min: %02x sec: %02x ", year, mon, mday, wday, hour, min, sec); debug ("Alarms[0]: wday: %02x hour: %02x min: %02x sec: %02x ", rtc_read (RTC_DAY_OF_WEEK_ALARM0), rtc_read (RTC_HOURS_ALARM0), rtc_read (RTC_MINUTES_ALARM0), rtc_read (RTC_SECONDS_ALARM0)); debug ("Alarms[1]: wday: %02x hour: %02x min: %02x sec: %02x ", rtc_read (RTC_DAY_OF_WEEK_ALARM1), rtc_read (RTC_HOURS_ALARM1), rtc_read (RTC_MINUTES_ALARM1), rtc_read (RTC_SECONDS_ALARM1)); tmp->tm_sec = bcd2bin (sec & 0x7F); /* convert Seconds */ tmp->tm_min = bcd2bin (min & 0x7F); /* convert Minutes */ /* convert Hours */ tmp->tm_hour = (hour & 0x40) ? ((hour & 0x20) /* 12 hour mode */ ? bcd2bin (hour & 0x1F) + 11 /* PM */ : bcd2bin (hour & 0x1F) - 1 /* AM */ ) : bcd2bin (hour & 0x3F); /* 24 hour mode */ tmp->tm_mday = bcd2bin (mday & 0x3F); /* convert Day of the Month */ tmp->tm_mon = bcd2bin (mon & 0x1F); /* convert Month */ tmp->tm_year = bcd2bin (year) + 2000; /* convert Year */ tmp->tm_wday = bcd2bin (wday & 0x07) - 1; /* convert Day of the Week */ tmp->tm_yday = 0; tmp->tm_isdst = 0; debug ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d ", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
b73a19e16 LWMON5: POST RTC fix |
345 346 |
return 0; |
affae2bff Initial revision |
347 348 349 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
350 |
/* set clock time from *tmp in DS1306 RTC */ |
d1e231941 rtc: allow rtc_se... |
351 |
int rtc_set (struct rtc_time *tmp) |
affae2bff Initial revision |
352 |
{ |
d255bb0e7 SPI API improvements |
353 354 |
/* Assuming Vcc = 2.0V (lowest speed) */ if (!slave) { |
6d0f6bcf3 rename CFG_ macro... |
355 |
slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000, |
d255bb0e7 SPI API improvements |
356 357 358 359 360 361 362 |
SPI_MODE_3 | SPI_CS_HIGH); if (!slave) return; } if (spi_claim_bus(slave)) return; |
ec4c544be Patches by Stepha... |
363 364 365 366 |
debug ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d ", tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
ec4c544be Patches by Stepha... |
367 |
rtc_write (RTC_SECONDS, bin2bcd (tmp->tm_sec)); |
da4849fb3 Write RTC seconds... |
368 369 370 371 372 373 |
rtc_write (RTC_MINUTES, bin2bcd (tmp->tm_min)); rtc_write (RTC_HOURS, bin2bcd (tmp->tm_hour)); rtc_write (RTC_DAY_OF_WEEK, bin2bcd (tmp->tm_wday + 1)); rtc_write (RTC_DATE_OF_MONTH, bin2bcd (tmp->tm_mday)); rtc_write (RTC_MONTH, bin2bcd (tmp->tm_mon)); rtc_write (RTC_YEAR, bin2bcd (tmp->tm_year - 2000)); |
d255bb0e7 SPI API improvements |
374 375 |
spi_release_bus(slave); |
affae2bff Initial revision |
376 377 378 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
379 380 381 |
/* reset the DS1306 */ void rtc_reset (void) { |
d255bb0e7 SPI API improvements |
382 383 |
/* Assuming Vcc = 2.0V (lowest speed) */ if (!slave) { |
6d0f6bcf3 rename CFG_ macro... |
384 |
slave = spi_setup_slave(0, CONFIG_SYS_SPI_RTC_DEVID, 600000, |
d255bb0e7 SPI API improvements |
385 386 387 388 389 390 391 |
SPI_MODE_3 | SPI_CS_HIGH); if (!slave) return; } if (spi_claim_bus(slave)) return; |
ec4c544be Patches by Stepha... |
392 393 394 395 396 397 398 399 400 401 402 403 404 |
/* clear the control register */ rtc_write (RTC_CONTROL, 0x00); /* 1st step: reset WP */ rtc_write (RTC_CONTROL, 0x00); /* 2nd step: reset 1Hz, AIE1, AIE0 */ /* reset all alarms */ rtc_write (RTC_SECONDS_ALARM0, 0x00); rtc_write (RTC_SECONDS_ALARM1, 0x00); rtc_write (RTC_MINUTES_ALARM0, 0x00); rtc_write (RTC_MINUTES_ALARM1, 0x00); rtc_write (RTC_HOURS_ALARM0, 0x00); rtc_write (RTC_HOURS_ALARM1, 0x00); rtc_write (RTC_DAY_OF_WEEK_ALARM0, 0x00); rtc_write (RTC_DAY_OF_WEEK_ALARM1, 0x00); |
d255bb0e7 SPI API improvements |
405 406 |
spi_release_bus(slave); |
ec4c544be Patches by Stepha... |
407 408 409 |
} /* ------------------------------------------------------------------------- */ |
affae2bff Initial revision |
410 |
|
ec4c544be Patches by Stepha... |
411 412 |
static unsigned char rtc_read (unsigned char reg) { |
d255bb0e7 SPI API improvements |
413 |
int ret; |
affae2bff Initial revision |
414 |
|
d255bb0e7 SPI API improvements |
415 416 |
ret = spi_w8r8(slave, reg); return ret < 0 ? 0 : ret; |
affae2bff Initial revision |
417 418 419 |
} /* ------------------------------------------------------------------------- */ |
ec4c544be Patches by Stepha... |
420 |
static void rtc_write (unsigned char reg, unsigned char val) |
affae2bff Initial revision |
421 |
{ |
ec4c544be Patches by Stepha... |
422 423 |
unsigned char dout[2]; /* SPI Output Data Bytes */ unsigned char din[2]; /* SPI Input Data Bytes */ |
affae2bff Initial revision |
424 |
|
ec4c544be Patches by Stepha... |
425 426 |
dout[0] = 0x80 | reg; dout[1] = val; |
affae2bff Initial revision |
427 |
|
d255bb0e7 SPI API improvements |
428 |
spi_xfer (slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END); |
ec4c544be Patches by Stepha... |
429 |
} |
affae2bff Initial revision |
430 |
|
ec4c544be Patches by Stepha... |
431 |
#endif /* end of code exclusion (see #ifdef CONFIG_SXNI855T above) */ |
affae2bff Initial revision |
432 |
|
affae2bff Initial revision |
433 |
#endif |