ds1306.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * (C) Copyright 2002 SIXNET, dge@sixnetio.com.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * Date & Time support for DS1306 RTC using software SPI
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <rtc.h>
  28. #if defined(CONFIG_RTC_DS1306) && (CONFIG_COMMANDS & CFG_CMD_DATE)
  29. static unsigned int bin2bcd(unsigned int n);
  30. static unsigned char bcd2bin(unsigned char c);
  31. static void soft_spi_send(unsigned char n);
  32. static unsigned char soft_spi_read(void);
  33. static void init_spi(void);
  34. /*-----------------------------------------------------------------------
  35. * Definitions
  36. */
  37. #define PB_SPISCK 0x00000002 /* PB 30 */
  38. #define PB_SPIMOSI 0x00000004 /* PB 29 */
  39. #define PB_SPIMISO 0x00000008 /* PB 28 */
  40. #define PB_SPI_CE 0x00010000 /* PB 15 */
  41. /* ------------------------------------------------------------------------- */
  42. /* read clock time from DS1306 and return it in *tmp */
  43. void rtc_get(struct rtc_time *tmp)
  44. {
  45. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  46. unsigned char spi_byte; /* Data Byte */
  47. init_spi(); /* set port B for software SPI */
  48. /* Now we can enable the DS1306 RTC */
  49. immap->im_cpm.cp_pbdat |= PB_SPI_CE;
  50. udelay(10);
  51. /* Shift out the address (0) of the time in the Clock Chip */
  52. soft_spi_send(0);
  53. /* Put the clock readings into the rtc_time structure */
  54. tmp->tm_sec = bcd2bin(soft_spi_read()); /* Read seconds */
  55. tmp->tm_min = bcd2bin(soft_spi_read()); /* Read minutes */
  56. /* Hours are trickier */
  57. spi_byte = soft_spi_read(); /* Read Hours into temporary value */
  58. if (spi_byte & 0x40) {
  59. /* 12 hour mode bit is set (time is in 1-12 format) */
  60. if (spi_byte & 0x20) {
  61. /* since PM we add 11 to get 0-23 for hours */
  62. tmp->tm_hour = (bcd2bin(spi_byte & 0x1F)) + 11;
  63. }
  64. else {
  65. /* since AM we subtract 1 to get 0-23 for hours */
  66. tmp->tm_hour = (bcd2bin(spi_byte & 0x1F)) - 1;
  67. }
  68. }
  69. else {
  70. /* Otherwise, 0-23 hour format */
  71. tmp->tm_hour = (bcd2bin(spi_byte & 0x3F));
  72. }
  73. soft_spi_read(); /* Read and discard Day of week */
  74. tmp->tm_mday = bcd2bin(soft_spi_read()); /* Read Day of the Month */
  75. tmp->tm_mon = bcd2bin(soft_spi_read()); /* Read Month */
  76. /* Read Year and convert to this century */
  77. tmp->tm_year = bcd2bin(soft_spi_read()) + 2000;
  78. /* Now we can disable the DS1306 RTC */
  79. immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */
  80. udelay(10);
  81. GregorianDay(tmp); /* Determine the day of week */
  82. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  83. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  84. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  85. }
  86. /* ------------------------------------------------------------------------- */
  87. /* set clock time in DS1306 RTC and in MPC8xx RTC */
  88. void rtc_set(struct rtc_time *tmp)
  89. {
  90. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  91. init_spi(); /* set port B for software SPI */
  92. /* Now we can enable the DS1306 RTC */
  93. immap->im_cpm.cp_pbdat |= PB_SPI_CE; /* Enable DS1306 Chip */
  94. udelay(10);
  95. /* First disable write protect in the clock chip control register */
  96. soft_spi_send(0x8F); /* send address of the control register */
  97. soft_spi_send(0x00); /* send control register contents */
  98. /* Now disable the DS1306 to terminate the write */
  99. immap->im_cpm.cp_pbdat &= ~PB_SPI_CE;
  100. udelay(10);
  101. /* Now enable the DS1306 to initiate a new write */
  102. immap->im_cpm.cp_pbdat |= PB_SPI_CE;
  103. udelay(10);
  104. /* Next, send the address of the clock time write registers */
  105. soft_spi_send(0x80); /* send address of the first time register */
  106. /* Use Burst Mode to send all of the time data to the clock */
  107. bin2bcd(tmp->tm_sec);
  108. soft_spi_send(bin2bcd(tmp->tm_sec)); /* Send Seconds */
  109. soft_spi_send(bin2bcd(tmp->tm_min)); /* Send Minutes */
  110. soft_spi_send(bin2bcd(tmp->tm_hour)); /* Send Hour */
  111. soft_spi_send(bin2bcd(tmp->tm_wday)); /* Send Day of the Week */
  112. soft_spi_send(bin2bcd(tmp->tm_mday)); /* Send Day of Month */
  113. soft_spi_send(bin2bcd(tmp->tm_mon)); /* Send Month */
  114. soft_spi_send(bin2bcd(tmp->tm_year - 2000)); /* Send Year */
  115. /* Now we can disable the Clock chip to terminate the burst write */
  116. immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */
  117. udelay(10);
  118. /* Now we can enable the Clock chip to initiate a new write */
  119. immap->im_cpm.cp_pbdat |= PB_SPI_CE; /* Enable DS1306 Chip */
  120. udelay(10);
  121. /* First we Enable write protect in the clock chip control register */
  122. soft_spi_send(0x8F); /* send address of the control register */
  123. soft_spi_send(0x40); /* send out Control Register contents */
  124. /* Now disable the DS1306 */
  125. immap->im_cpm.cp_pbdat &= ~PB_SPI_CE; /* Disable DS1306 Chip */
  126. udelay(10);
  127. /* Set standard MPC8xx clock to the same time so Linux will
  128. * see the time even if it doesn't have a DS1306 clock driver.
  129. * This helps with experimenting with standard kernels.
  130. */
  131. {
  132. ulong tim;
  133. tim = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
  134. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  135. immap->im_sitk.sitk_rtck = KAPWR_KEY;
  136. immap->im_sit.sit_rtc = tim;
  137. }
  138. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  139. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  140. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  141. }
  142. /* ------------------------------------------------------------------------- */
  143. void rtc_reset(void)
  144. {
  145. return; /* nothing to do */
  146. }
  147. /* ------------------------------------------------------------------------- */
  148. static unsigned char bcd2bin(unsigned char n)
  149. {
  150. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  151. }
  152. /* ------------------------------------------------------------------------- */
  153. static unsigned int bin2bcd(unsigned int n)
  154. {
  155. return (((n / 10) << 4) | (n % 10));
  156. }
  157. /* ------------------------------------------------------------------------- */
  158. /* Initialize Port B for software SPI */
  159. static void init_spi(void) {
  160. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  161. /* Force output pins to begin at logic 0 */
  162. immap->im_cpm.cp_pbdat &= ~(PB_SPI_CE | PB_SPIMOSI | PB_SPISCK);
  163. /* Set these 3 signals as outputs */
  164. immap->im_cpm.cp_pbdir |= (PB_SPIMOSI | PB_SPI_CE | PB_SPISCK);
  165. immap->im_cpm.cp_pbdir &= ~PB_SPIMISO; /* Make MISO pin an input */
  166. udelay(10);
  167. }
  168. /* ------------------------------------------------------------------------- */
  169. /* NOTE: soft_spi_send() assumes that the I/O lines are configured already */
  170. static void soft_spi_send(unsigned char n)
  171. {
  172. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  173. unsigned char bitpos; /* bit position to receive */
  174. unsigned char i; /* Loop Control */
  175. /* bit position to send, start with most significant bit */
  176. bitpos = 0x80;
  177. /* Send 8 bits to software SPI */
  178. for (i = 0; i < 8; i++) { /* Loop for 8 bits */
  179. immap->im_cpm.cp_pbdat |= PB_SPISCK; /* Raise SCK */
  180. if (n & bitpos)
  181. immap->im_cpm.cp_pbdat |= PB_SPIMOSI; /* Set MOSI to 1 */
  182. else
  183. immap->im_cpm.cp_pbdat &= ~PB_SPIMOSI; /* Set MOSI to 0 */
  184. udelay(10);
  185. immap->im_cpm.cp_pbdat &= ~PB_SPISCK; /* Lower SCK */
  186. udelay(10);
  187. bitpos >>= 1; /* Shift for next bit position */
  188. }
  189. }
  190. /* ------------------------------------------------------------------------- */
  191. /* NOTE: soft_spi_read() assumes that the I/O lines are configured already */
  192. static unsigned char soft_spi_read(void)
  193. {
  194. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  195. unsigned char spi_byte = 0; /* Return value, assume success */
  196. unsigned char bitpos; /* bit position to receive */
  197. unsigned char i; /* Loop Control */
  198. /* bit position to receive, start with most significant bit */
  199. bitpos = 0x80;
  200. /* Read 8 bits here */
  201. for (i = 0; i < 8; i++) { /* Do 8 bits in loop */
  202. immap->im_cpm.cp_pbdat |= PB_SPISCK; /* Raise SCK */
  203. udelay(10);
  204. if (immap->im_cpm.cp_pbdat & PB_SPIMISO) /* Get a bit of data */
  205. spi_byte |= bitpos; /* Set data accordingly */
  206. immap->im_cpm.cp_pbdat &= ~PB_SPISCK; /* Lower SCK */
  207. udelay(10);
  208. bitpos >>= 1; /* Shift for next bit position */
  209. }
  210. return spi_byte; /* Return the byte read */
  211. }
  212. /* ------------------------------------------------------------------------- */
  213. #endif