ds1306.c 13 KB

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