ds1302.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <arch/svinto.h>
  25. #include <asm/io.h>
  26. #include <asm/rtc.h>
  27. #include <arch/io_interface_mux.h>
  28. #include "i2c.h"
  29. #define RTC_MAJOR_NR 121 /* local major, change later */
  30. static DEFINE_MUTEX(ds1302_mutex);
  31. static const char ds1302_name[] = "ds1302";
  32. /* The DS1302 might be connected to different bits on different products.
  33. * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
  34. * but SDA can have a selected direction.
  35. * For now, only PORT_PB is hardcoded.
  36. */
  37. /* The RST bit may be on either the Generic Port or Port PB. */
  38. #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
  39. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  40. #define TK_RST_DIR(x)
  41. #else
  42. #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  43. #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
  44. #endif
  45. #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  46. #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  47. #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
  48. /* 1 is out, 0 is in */
  49. #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
  50. #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
  51. /*
  52. * The reason for tempudelay and not udelay is that loops_per_usec
  53. * (used in udelay) is not set when functions here are called from time.c
  54. */
  55. static void tempudelay(int usecs)
  56. {
  57. volatile int loops;
  58. for(loops = usecs * 12; loops > 0; loops--)
  59. /* nothing */;
  60. }
  61. /* Send 8 bits. */
  62. static void
  63. out_byte(unsigned char x)
  64. {
  65. int i;
  66. TK_SDA_DIR(1);
  67. for (i = 8; i--;) {
  68. /* The chip latches incoming bits on the rising edge of SCL. */
  69. TK_SCL_OUT(0);
  70. TK_SDA_OUT(x & 1);
  71. tempudelay(1);
  72. TK_SCL_OUT(1);
  73. tempudelay(1);
  74. x >>= 1;
  75. }
  76. TK_SDA_DIR(0);
  77. }
  78. static unsigned char
  79. in_byte(void)
  80. {
  81. unsigned char x = 0;
  82. int i;
  83. /* Read byte. Bits come LSB first, on the falling edge of SCL.
  84. * Assume SDA is in input direction already.
  85. */
  86. TK_SDA_DIR(0);
  87. for (i = 8; i--;) {
  88. TK_SCL_OUT(0);
  89. tempudelay(1);
  90. x >>= 1;
  91. x |= (TK_SDA_IN() << 7);
  92. TK_SCL_OUT(1);
  93. tempudelay(1);
  94. }
  95. return x;
  96. }
  97. /* Prepares for a transaction by de-activating RST (active-low). */
  98. static void
  99. start(void)
  100. {
  101. TK_SCL_OUT(0);
  102. tempudelay(1);
  103. TK_RST_OUT(0);
  104. tempudelay(5);
  105. TK_RST_OUT(1);
  106. }
  107. /* Ends a transaction by taking RST active again. */
  108. static void
  109. stop(void)
  110. {
  111. tempudelay(2);
  112. TK_RST_OUT(0);
  113. }
  114. /* Enable writing. */
  115. static void
  116. ds1302_wenable(void)
  117. {
  118. start();
  119. out_byte(0x8e); /* Write control register */
  120. out_byte(0x00); /* Disable write protect bit 7 = 0 */
  121. stop();
  122. }
  123. /* Disable writing. */
  124. static void
  125. ds1302_wdisable(void)
  126. {
  127. start();
  128. out_byte(0x8e); /* Write control register */
  129. out_byte(0x80); /* Disable write protect bit 7 = 0 */
  130. stop();
  131. }
  132. /* Read a byte from the selected register in the DS1302. */
  133. unsigned char
  134. ds1302_readreg(int reg)
  135. {
  136. unsigned char x;
  137. start();
  138. out_byte(0x81 | (reg << 1)); /* read register */
  139. x = in_byte();
  140. stop();
  141. return x;
  142. }
  143. /* Write a byte to the selected register. */
  144. void
  145. ds1302_writereg(int reg, unsigned char val)
  146. {
  147. #ifndef CONFIG_ETRAX_RTC_READONLY
  148. int do_writereg = 1;
  149. #else
  150. int do_writereg = 0;
  151. if (reg == RTC_TRICKLECHARGER)
  152. do_writereg = 1;
  153. #endif
  154. if (do_writereg) {
  155. ds1302_wenable();
  156. start();
  157. out_byte(0x80 | (reg << 1)); /* write register */
  158. out_byte(val);
  159. stop();
  160. ds1302_wdisable();
  161. }
  162. }
  163. void
  164. get_rtc_time(struct rtc_time *rtc_tm)
  165. {
  166. unsigned long flags;
  167. local_irq_save(flags);
  168. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  169. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  170. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  171. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  172. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  173. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  174. local_irq_restore(flags);
  175. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  176. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  177. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  178. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  179. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  180. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  181. /*
  182. * Account for differences between how the RTC uses the values
  183. * and how they are defined in a struct rtc_time;
  184. */
  185. if (rtc_tm->tm_year <= 69)
  186. rtc_tm->tm_year += 100;
  187. rtc_tm->tm_mon--;
  188. }
  189. static unsigned char days_in_mo[] =
  190. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  191. /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
  192. static int rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  193. {
  194. unsigned long flags;
  195. switch(cmd) {
  196. case RTC_RD_TIME: /* read the time/date from RTC */
  197. {
  198. struct rtc_time rtc_tm;
  199. memset(&rtc_tm, 0, sizeof (struct rtc_time));
  200. get_rtc_time(&rtc_tm);
  201. if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
  202. return -EFAULT;
  203. return 0;
  204. }
  205. case RTC_SET_TIME: /* set the RTC */
  206. {
  207. struct rtc_time rtc_tm;
  208. unsigned char mon, day, hrs, min, sec, leap_yr;
  209. unsigned int yrs;
  210. if (!capable(CAP_SYS_TIME))
  211. return -EPERM;
  212. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
  213. return -EFAULT;
  214. yrs = rtc_tm.tm_year + 1900;
  215. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  216. day = rtc_tm.tm_mday;
  217. hrs = rtc_tm.tm_hour;
  218. min = rtc_tm.tm_min;
  219. sec = rtc_tm.tm_sec;
  220. if ((yrs < 1970) || (yrs > 2069))
  221. return -EINVAL;
  222. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  223. if ((mon > 12) || (day == 0))
  224. return -EINVAL;
  225. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  226. return -EINVAL;
  227. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  228. return -EINVAL;
  229. if (yrs >= 2000)
  230. yrs -= 2000; /* RTC (0, 1, ... 69) */
  231. else
  232. yrs -= 1900; /* RTC (70, 71, ... 99) */
  233. sec = bin2bcd(sec);
  234. min = bin2bcd(min);
  235. hrs = bin2bcd(hrs);
  236. day = bin2bcd(day);
  237. mon = bin2bcd(mon);
  238. yrs = bin2bcd(yrs);
  239. local_irq_save(flags);
  240. CMOS_WRITE(yrs, RTC_YEAR);
  241. CMOS_WRITE(mon, RTC_MONTH);
  242. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  243. CMOS_WRITE(hrs, RTC_HOURS);
  244. CMOS_WRITE(min, RTC_MINUTES);
  245. CMOS_WRITE(sec, RTC_SECONDS);
  246. local_irq_restore(flags);
  247. /* Notice that at this point, the RTC is updated but
  248. * the kernel is still running with the old time.
  249. * You need to set that separately with settimeofday
  250. * or adjtimex.
  251. */
  252. return 0;
  253. }
  254. case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
  255. {
  256. int tcs_val;
  257. if (!capable(CAP_SYS_TIME))
  258. return -EPERM;
  259. if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
  260. return -EFAULT;
  261. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  262. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  263. return 0;
  264. }
  265. case RTC_VL_READ:
  266. {
  267. /* TODO:
  268. * Implement voltage low detection support
  269. */
  270. printk(KERN_WARNING "DS1302: RTC Voltage Low detection"
  271. " is not supported\n");
  272. return 0;
  273. }
  274. case RTC_VL_CLR:
  275. {
  276. /* TODO:
  277. * Nothing to do since Voltage Low detection is not supported
  278. */
  279. return 0;
  280. }
  281. default:
  282. return -ENOIOCTLCMD;
  283. }
  284. }
  285. static long rtc_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  286. {
  287. int ret;
  288. mutex_lock(&ds1302_mutex);
  289. ret = rtc_ioctl(file, cmd, arg);
  290. mutex_unlock(&ds1302_mutex);
  291. return ret;
  292. }
  293. static void
  294. print_rtc_status(void)
  295. {
  296. struct rtc_time tm;
  297. get_rtc_time(&tm);
  298. /*
  299. * There is no way to tell if the luser has the RTC set for local
  300. * time or for Universal Standard Time (GMT). Probably local though.
  301. */
  302. printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n",
  303. tm.tm_hour, tm.tm_min, tm.tm_sec);
  304. printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n",
  305. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  306. }
  307. /* The various file operations we support. */
  308. static const struct file_operations rtc_fops = {
  309. .owner = THIS_MODULE,
  310. .unlocked_ioctl = rtc_unlocked_ioctl,
  311. .llseek = noop_llseek,
  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);