ds1302.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*!***************************************************************************
  2. *!
  3. *! FILE NAME : ds1302.c
  4. *!
  5. *! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O
  6. *!
  7. *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init
  8. *!
  9. *! ---------------------------------------------------------------------------
  10. *!
  11. *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
  12. *!
  13. *!***************************************************************************/
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/delay.h>
  20. #include <linux/bcd.h>
  21. #include <linux/capability.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/system.h>
  24. #include <asm/arch/svinto.h>
  25. #include <asm/io.h>
  26. #include <asm/rtc.h>
  27. #include <asm/arch/io_interface_mux.h>
  28. #include "i2c.h"
  29. #define RTC_MAJOR_NR 121 /* local major, change later */
  30. static const char ds1302_name[] = "ds1302";
  31. /* The DS1302 might be connected to different bits on different products.
  32. * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
  33. * but SDA can have a selected direction.
  34. * For now, only PORT_PB is hardcoded.
  35. */
  36. /* The RST bit may be on either the Generic Port or Port PB. */
  37. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  38. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  39. #define TK_RST_DIR(x)
  40. #else
  41. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  42. #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  43. #endif
  44. #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  45. #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  46. #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
  47. /* 1 is out, 0 is in */
  48. #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  49. #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  50. /*
  51. * The reason for tempudelay and not udelay is that loops_per_usec
  52. * (used in udelay) is not set when functions here are called from time.c
  53. */
  54. static void tempudelay(int usecs)
  55. {
  56. volatile int loops;
  57. for(loops = usecs * 12; loops > 0; loops--)
  58. /* nothing */;
  59. }
  60. /* Send 8 bits. */
  61. static void
  62. out_byte(unsigned char x)
  63. {
  64. int i;
  65. TK_SDA_DIR(1);
  66. for (i = 8; i--;) {
  67. /* The chip latches incoming bits on the rising edge of SCL. */
  68. TK_SCL_OUT(0);
  69. TK_SDA_OUT(x & 1);
  70. tempudelay(1);
  71. TK_SCL_OUT(1);
  72. tempudelay(1);
  73. x >>= 1;
  74. }
  75. TK_SDA_DIR(0);
  76. }
  77. static unsigned char
  78. in_byte(void)
  79. {
  80. unsigned char x = 0;
  81. int i;
  82. /* Read byte. Bits come LSB first, on the falling edge of SCL.
  83. * Assume SDA is in input direction already.
  84. */
  85. TK_SDA_DIR(0);
  86. for (i = 8; i--;) {
  87. TK_SCL_OUT(0);
  88. tempudelay(1);
  89. x >>= 1;
  90. x |= (TK_SDA_IN() << 7);
  91. TK_SCL_OUT(1);
  92. tempudelay(1);
  93. }
  94. return x;
  95. }
  96. /* Prepares for a transaction by de-activating RST (active-low). */
  97. static void
  98. start(void)
  99. {
  100. TK_SCL_OUT(0);
  101. tempudelay(1);
  102. TK_RST_OUT(0);
  103. tempudelay(5);
  104. TK_RST_OUT(1);
  105. }
  106. /* Ends a transaction by taking RST active again. */
  107. static void
  108. stop(void)
  109. {
  110. tempudelay(2);
  111. TK_RST_OUT(0);
  112. }
  113. /* Enable writing. */
  114. static void
  115. ds1302_wenable(void)
  116. {
  117. start();
  118. out_byte(0x8e); /* Write control register */
  119. out_byte(0x00); /* Disable write protect bit 7 = 0 */
  120. stop();
  121. }
  122. /* Disable writing. */
  123. static void
  124. ds1302_wdisable(void)
  125. {
  126. start();
  127. out_byte(0x8e); /* Write control register */
  128. out_byte(0x80); /* Disable write protect bit 7 = 0 */
  129. stop();
  130. }
  131. /* Read a byte from the selected register in the DS1302. */
  132. unsigned char
  133. ds1302_readreg(int reg)
  134. {
  135. unsigned char x;
  136. start();
  137. out_byte(0x81 | (reg << 1)); /* read register */
  138. x = in_byte();
  139. stop();
  140. return x;
  141. }
  142. /* Write a byte to the selected register. */
  143. void
  144. ds1302_writereg(int reg, unsigned char val)
  145. {
  146. #ifndef CONFIG_ETRAX_RTC_READONLY
  147. int do_writereg = 1;
  148. #else
  149. int do_writereg = 0;
  150. if (reg == RTC_TRICKLECHARGER)
  151. do_writereg = 1;
  152. #endif
  153. if (do_writereg) {
  154. ds1302_wenable();
  155. start();
  156. out_byte(0x80 | (reg << 1)); /* write register */
  157. out_byte(val);
  158. stop();
  159. ds1302_wdisable();
  160. }
  161. }
  162. void
  163. get_rtc_time(struct rtc_time *rtc_tm)
  164. {
  165. unsigned long flags;
  166. local_irq_save(flags);
  167. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  168. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  169. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  170. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  171. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  172. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  173. local_irq_restore(flags);
  174. BCD_TO_BIN(rtc_tm->tm_sec);
  175. BCD_TO_BIN(rtc_tm->tm_min);
  176. BCD_TO_BIN(rtc_tm->tm_hour);
  177. BCD_TO_BIN(rtc_tm->tm_mday);
  178. BCD_TO_BIN(rtc_tm->tm_mon);
  179. BCD_TO_BIN(rtc_tm->tm_year);
  180. /*
  181. * Account for differences between how the RTC uses the values
  182. * and how they are defined in a struct rtc_time;
  183. */
  184. if (rtc_tm->tm_year <= 69)
  185. rtc_tm->tm_year += 100;
  186. rtc_tm->tm_mon--;
  187. }
  188. static unsigned char days_in_mo[] =
  189. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  190. /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
  191. static int
  192. rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  193. unsigned long arg)
  194. {
  195. unsigned long flags;
  196. switch(cmd) {
  197. case RTC_RD_TIME: /* read the time/date from RTC */
  198. {
  199. struct rtc_time rtc_tm;
  200. memset(&rtc_tm, 0, sizeof (struct rtc_time));
  201. get_rtc_time(&rtc_tm);
  202. if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
  203. return -EFAULT;
  204. return 0;
  205. }
  206. case RTC_SET_TIME: /* set the RTC */
  207. {
  208. struct rtc_time rtc_tm;
  209. unsigned char mon, day, hrs, min, sec, leap_yr;
  210. unsigned int yrs;
  211. if (!capable(CAP_SYS_TIME))
  212. return -EPERM;
  213. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
  214. return -EFAULT;
  215. yrs = rtc_tm.tm_year + 1900;
  216. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  217. day = rtc_tm.tm_mday;
  218. hrs = rtc_tm.tm_hour;
  219. min = rtc_tm.tm_min;
  220. sec = rtc_tm.tm_sec;
  221. if ((yrs < 1970) || (yrs > 2069))
  222. return -EINVAL;
  223. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  224. if ((mon > 12) || (day == 0))
  225. return -EINVAL;
  226. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  227. return -EINVAL;
  228. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  229. return -EINVAL;
  230. if (yrs >= 2000)
  231. yrs -= 2000; /* RTC (0, 1, ... 69) */
  232. else
  233. yrs -= 1900; /* RTC (70, 71, ... 99) */
  234. BIN_TO_BCD(sec);
  235. BIN_TO_BCD(min);
  236. BIN_TO_BCD(hrs);
  237. BIN_TO_BCD(day);
  238. BIN_TO_BCD(mon);
  239. BIN_TO_BCD(yrs);
  240. local_irq_save(flags);
  241. CMOS_WRITE(yrs, RTC_YEAR);
  242. CMOS_WRITE(mon, RTC_MONTH);
  243. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  244. CMOS_WRITE(hrs, RTC_HOURS);
  245. CMOS_WRITE(min, RTC_MINUTES);
  246. CMOS_WRITE(sec, RTC_SECONDS);
  247. local_irq_restore(flags);
  248. /* Notice that at this point, the RTC is updated but
  249. * the kernel is still running with the old time.
  250. * You need to set that separately with settimeofday
  251. * or adjtimex.
  252. */
  253. return 0;
  254. }
  255. case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
  256. {
  257. int tcs_val;
  258. if (!capable(CAP_SYS_TIME))
  259. return -EPERM;
  260. if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
  261. return -EFAULT;
  262. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  263. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  264. return 0;
  265. }
  266. case RTC_VL_READ:
  267. {
  268. /* TODO:
  269. * Implement voltage low detection support
  270. */
  271. printk(KERN_WARNING "DS1302: RTC Voltage Low detection"
  272. " is not supported\n");
  273. return 0;
  274. }
  275. case RTC_VL_CLR:
  276. {
  277. /* TODO:
  278. * Nothing to do since Voltage Low detection is not supported
  279. */
  280. return 0;
  281. }
  282. default:
  283. return -ENOIOCTLCMD;
  284. }
  285. }
  286. static void
  287. print_rtc_status(void)
  288. {
  289. struct rtc_time tm;
  290. get_rtc_time(&tm);
  291. /*
  292. * There is no way to tell if the luser has the RTC set for local
  293. * time or for Universal Standard Time (GMT). Probably local though.
  294. */
  295. printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n",
  296. tm.tm_hour, tm.tm_min, tm.tm_sec);
  297. printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n",
  298. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  299. }
  300. /* The various file operations we support. */
  301. static const struct file_operations rtc_fops = {
  302. .owner = THIS_MODULE,
  303. .ioctl = rtc_ioctl,
  304. };
  305. /* Probe for the chip by writing something to its RAM and try reading it back. */
  306. #define MAGIC_PATTERN 0x42
  307. static int __init
  308. ds1302_probe(void)
  309. {
  310. int retval, res;
  311. TK_RST_DIR(1);
  312. TK_SCL_DIR(1);
  313. TK_SDA_DIR(0);
  314. /* Try to talk to timekeeper. */
  315. ds1302_wenable();
  316. start();
  317. out_byte(0xc0); /* write RAM byte 0 */
  318. out_byte(MAGIC_PATTERN); /* write something magic */
  319. start();
  320. out_byte(0xc1); /* read RAM byte 0 */
  321. if((res = in_byte()) == MAGIC_PATTERN) {
  322. stop();
  323. ds1302_wdisable();
  324. printk(KERN_INFO "%s: RTC found.\n", ds1302_name);
  325. printk(KERN_INFO "%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
  326. ds1302_name,
  327. CONFIG_ETRAX_DS1302_SDABIT,
  328. CONFIG_ETRAX_DS1302_SCLBIT,
  329. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  330. "GENIO",
  331. #else
  332. "PB",
  333. #endif
  334. CONFIG_ETRAX_DS1302_RSTBIT);
  335. print_rtc_status();
  336. retval = 1;
  337. } else {
  338. stop();
  339. retval = 0;
  340. }
  341. return retval;
  342. }
  343. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  344. int __init
  345. ds1302_init(void)
  346. {
  347. #ifdef CONFIG_ETRAX_I2C
  348. i2c_init();
  349. #endif
  350. if (!ds1302_probe()) {
  351. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  352. #if CONFIG_ETRAX_DS1302_RSTBIT == 27
  353. /*
  354. * The only way to set g27 to output is to enable ATA.
  355. *
  356. * Make sure that R_GEN_CONFIG is setup correct.
  357. */
  358. /* Allocating the ATA interface will grab almost all
  359. * pins in I/O groups a, b, c and d. A consequence of
  360. * allocating the ATA interface is that the fixed
  361. * interfaces shared RAM, parallel port 0, parallel
  362. * port 1, parallel port W, SCSI-8 port 0, SCSI-8 port
  363. * 1, SCSI-W, serial port 2, serial port 3,
  364. * synchronous serial port 3 and USB port 2 and almost
  365. * all GPIO pins on port g cannot be used.
  366. */
  367. if (cris_request_io_interface(if_ata, "ds1302/ATA")) {
  368. printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
  369. return -1;
  370. }
  371. #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
  372. if (cris_io_interface_allocate_pins(if_gpio_grp_a,
  373. 'g',
  374. CONFIG_ETRAX_DS1302_RSTBIT,
  375. CONFIG_ETRAX_DS1302_RSTBIT)) {
  376. printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
  377. return -1;
  378. }
  379. /* Set the direction of this bit to out. */
  380. genconfig_shadow = ((genconfig_shadow &
  381. ~IO_MASK(R_GEN_CONFIG, g0dir)) |
  382. (IO_STATE(R_GEN_CONFIG, g0dir, out)));
  383. *R_GEN_CONFIG = genconfig_shadow;
  384. #endif
  385. if (!ds1302_probe()) {
  386. printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
  387. return -1;
  388. }
  389. #else
  390. printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
  391. return -1;
  392. #endif
  393. }
  394. /* Initialise trickle charger */
  395. ds1302_writereg(RTC_TRICKLECHARGER,
  396. RTC_TCR_PATTERN |(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE & 0x0F));
  397. /* Start clock by resetting CLOCK_HALT */
  398. ds1302_writereg(RTC_SECONDS, (ds1302_readreg(RTC_SECONDS) & 0x7F));
  399. return 0;
  400. }
  401. static int __init ds1302_register(void)
  402. {
  403. ds1302_init();
  404. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  405. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  406. ds1302_name, RTC_MAJOR_NR);
  407. return -1;
  408. }
  409. return 0;
  410. }
  411. module_init(ds1302_register);