ds1302.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/mutex.h>
  21. #include <linux/bcd.h>
  22. #include <linux/capability.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/system.h>
  25. #include <arch/svinto.h>
  26. #include <asm/io.h>
  27. #include <asm/rtc.h>
  28. #include <arch/io_interface_mux.h>
  29. #include "i2c.h"
  30. #define RTC_MAJOR_NR 121 /* local major, change later */
  31. static DEFINE_MUTEX(ds1302_mutex);
  32. static const char ds1302_name[] = "ds1302";
  33. /* The DS1302 might be connected to different bits on different products.
  34. * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
  35. * but SDA can have a selected direction.
  36. * For now, only PORT_PB is hardcoded.
  37. */
  38. /* The RST bit may be on either the Generic Port or Port PB. */
  39. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  40. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  41. #define TK_RST_DIR(x)
  42. #else
  43. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  44. #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  45. #endif
  46. #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  47. #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  48. #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
  49. /* 1 is out, 0 is in */
  50. #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  51. #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  52. /*
  53. * The reason for tempudelay and not udelay is that loops_per_usec
  54. * (used in udelay) is not set when functions here are called from time.c
  55. */
  56. static void tempudelay(int usecs)
  57. {
  58. volatile int loops;
  59. for(loops = usecs * 12; loops > 0; loops--)
  60. /* nothing */;
  61. }
  62. /* Send 8 bits. */
  63. static void
  64. out_byte(unsigned char x)
  65. {
  66. int i;
  67. TK_SDA_DIR(1);
  68. for (i = 8; i--;) {
  69. /* The chip latches incoming bits on the rising edge of SCL. */
  70. TK_SCL_OUT(0);
  71. TK_SDA_OUT(x & 1);
  72. tempudelay(1);
  73. TK_SCL_OUT(1);
  74. tempudelay(1);
  75. x >>= 1;
  76. }
  77. TK_SDA_DIR(0);
  78. }
  79. static unsigned char
  80. in_byte(void)
  81. {
  82. unsigned char x = 0;
  83. int i;
  84. /* Read byte. Bits come LSB first, on the falling edge of SCL.
  85. * Assume SDA is in input direction already.
  86. */
  87. TK_SDA_DIR(0);
  88. for (i = 8; i--;) {
  89. TK_SCL_OUT(0);
  90. tempudelay(1);
  91. x >>= 1;
  92. x |= (TK_SDA_IN() << 7);
  93. TK_SCL_OUT(1);
  94. tempudelay(1);
  95. }
  96. return x;
  97. }
  98. /* Prepares for a transaction by de-activating RST (active-low). */
  99. static void
  100. start(void)
  101. {
  102. TK_SCL_OUT(0);
  103. tempudelay(1);
  104. TK_RST_OUT(0);
  105. tempudelay(5);
  106. TK_RST_OUT(1);
  107. }
  108. /* Ends a transaction by taking RST active again. */
  109. static void
  110. stop(void)
  111. {
  112. tempudelay(2);
  113. TK_RST_OUT(0);
  114. }
  115. /* Enable writing. */
  116. static void
  117. ds1302_wenable(void)
  118. {
  119. start();
  120. out_byte(0x8e); /* Write control register */
  121. out_byte(0x00); /* Disable write protect bit 7 = 0 */
  122. stop();
  123. }
  124. /* Disable writing. */
  125. static void
  126. ds1302_wdisable(void)
  127. {
  128. start();
  129. out_byte(0x8e); /* Write control register */
  130. out_byte(0x80); /* Disable write protect bit 7 = 0 */
  131. stop();
  132. }
  133. /* Read a byte from the selected register in the DS1302. */
  134. unsigned char
  135. ds1302_readreg(int reg)
  136. {
  137. unsigned char x;
  138. start();
  139. out_byte(0x81 | (reg << 1)); /* read register */
  140. x = in_byte();
  141. stop();
  142. return x;
  143. }
  144. /* Write a byte to the selected register. */
  145. void
  146. ds1302_writereg(int reg, unsigned char val)
  147. {
  148. #ifndef CONFIG_ETRAX_RTC_READONLY
  149. int do_writereg = 1;
  150. #else
  151. int do_writereg = 0;
  152. if (reg == RTC_TRICKLECHARGER)
  153. do_writereg = 1;
  154. #endif
  155. if (do_writereg) {
  156. ds1302_wenable();
  157. start();
  158. out_byte(0x80 | (reg << 1)); /* write register */
  159. out_byte(val);
  160. stop();
  161. ds1302_wdisable();
  162. }
  163. }
  164. void
  165. get_rtc_time(struct rtc_time *rtc_tm)
  166. {
  167. unsigned long flags;
  168. local_irq_save(flags);
  169. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  170. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  171. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  172. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  173. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  174. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  175. local_irq_restore(flags);
  176. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  177. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  178. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  179. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  180. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  181. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  182. /*
  183. * Account for differences between how the RTC uses the values
  184. * and how they are defined in a struct rtc_time;
  185. */
  186. if (rtc_tm->tm_year <= 69)
  187. rtc_tm->tm_year += 100;
  188. rtc_tm->tm_mon--;
  189. }
  190. static unsigned char days_in_mo[] =
  191. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  192. /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
  193. static int rtc_ioctl(struct file *file, unsigned int cmd, 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. sec = bin2bcd(sec);
  235. min = bin2bcd(min);
  236. hrs = bin2bcd(hrs);
  237. day = bin2bcd(day);
  238. mon = bin2bcd(mon);
  239. yrs = bin2bcd(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 long rtc_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  287. {
  288. int ret;
  289. mutex_lock(&ds1302_mutex);
  290. ret = rtc_ioctl(file, cmd, arg);
  291. mutex_unlock(&ds1302_mutex);
  292. return ret;
  293. }
  294. static void
  295. print_rtc_status(void)
  296. {
  297. struct rtc_time tm;
  298. get_rtc_time(&tm);
  299. /*
  300. * There is no way to tell if the luser has the RTC set for local
  301. * time or for Universal Standard Time (GMT). Probably local though.
  302. */
  303. printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n",
  304. tm.tm_hour, tm.tm_min, tm.tm_sec);
  305. printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n",
  306. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  307. }
  308. /* The various file operations we support. */
  309. static const struct file_operations rtc_fops = {
  310. .owner = THIS_MODULE,
  311. .unlocked_ioctl = rtc_unlocked_ioctl,
  312. };
  313. /* Probe for the chip by writing something to its RAM and try reading it back. */
  314. #define MAGIC_PATTERN 0x42
  315. static int __init
  316. ds1302_probe(void)
  317. {
  318. int retval, res;
  319. TK_RST_DIR(1);
  320. TK_SCL_DIR(1);
  321. TK_SDA_DIR(0);
  322. /* Try to talk to timekeeper. */
  323. ds1302_wenable();
  324. start();
  325. out_byte(0xc0); /* write RAM byte 0 */
  326. out_byte(MAGIC_PATTERN); /* write something magic */
  327. start();
  328. out_byte(0xc1); /* read RAM byte 0 */
  329. if((res = in_byte()) == MAGIC_PATTERN) {
  330. stop();
  331. ds1302_wdisable();
  332. printk(KERN_INFO "%s: RTC found.\n", ds1302_name);
  333. printk(KERN_INFO "%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
  334. ds1302_name,
  335. CONFIG_ETRAX_DS1302_SDABIT,
  336. CONFIG_ETRAX_DS1302_SCLBIT,
  337. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  338. "GENIO",
  339. #else
  340. "PB",
  341. #endif
  342. CONFIG_ETRAX_DS1302_RSTBIT);
  343. print_rtc_status();
  344. retval = 1;
  345. } else {
  346. stop();
  347. retval = 0;
  348. }
  349. return retval;
  350. }
  351. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  352. int __init
  353. ds1302_init(void)
  354. {
  355. #ifdef CONFIG_ETRAX_I2C
  356. i2c_init();
  357. #endif
  358. if (!ds1302_probe()) {
  359. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  360. #if CONFIG_ETRAX_DS1302_RSTBIT == 27
  361. /*
  362. * The only way to set g27 to output is to enable ATA.
  363. *
  364. * Make sure that R_GEN_CONFIG is setup correct.
  365. */
  366. /* Allocating the ATA interface will grab almost all
  367. * pins in I/O groups a, b, c and d. A consequence of
  368. * allocating the ATA interface is that the fixed
  369. * interfaces shared RAM, parallel port 0, parallel
  370. * port 1, parallel port W, SCSI-8 port 0, SCSI-8 port
  371. * 1, SCSI-W, serial port 2, serial port 3,
  372. * synchronous serial port 3 and USB port 2 and almost
  373. * all GPIO pins on port g cannot be used.
  374. */
  375. if (cris_request_io_interface(if_ata, "ds1302/ATA")) {
  376. printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
  377. return -1;
  378. }
  379. #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
  380. if (cris_io_interface_allocate_pins(if_gpio_grp_a,
  381. 'g',
  382. CONFIG_ETRAX_DS1302_RSTBIT,
  383. CONFIG_ETRAX_DS1302_RSTBIT)) {
  384. printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
  385. return -1;
  386. }
  387. /* Set the direction of this bit to out. */
  388. genconfig_shadow = ((genconfig_shadow &
  389. ~IO_MASK(R_GEN_CONFIG, g0dir)) |
  390. (IO_STATE(R_GEN_CONFIG, g0dir, out)));
  391. *R_GEN_CONFIG = genconfig_shadow;
  392. #endif
  393. if (!ds1302_probe()) {
  394. printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
  395. return -1;
  396. }
  397. #else
  398. printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
  399. return -1;
  400. #endif
  401. }
  402. /* Initialise trickle charger */
  403. ds1302_writereg(RTC_TRICKLECHARGER,
  404. RTC_TCR_PATTERN |(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE & 0x0F));
  405. /* Start clock by resetting CLOCK_HALT */
  406. ds1302_writereg(RTC_SECONDS, (ds1302_readreg(RTC_SECONDS) & 0x7F));
  407. return 0;
  408. }
  409. static int __init ds1302_register(void)
  410. {
  411. ds1302_init();
  412. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  413. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  414. ds1302_name, RTC_MAJOR_NR);
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. module_init(ds1302_register);