ds1306.c 14 KB

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