ds1306.c 13 KB

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