aml_rtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * this driver is written for the internal rtc for M1
  3. */
  4. #include<linux/module.h>
  5. #include<linux/platform_device.h>
  6. #include<linux/rtc.h>
  7. #include<linux/slab.h>
  8. #include<asm/delay.h>
  9. #include<mach/am_regs.h>
  10. #ifdef CONFIG_ARCH_MESON6
  11. #include <plat/io.h>
  12. #endif
  13. int c_dbg_lvl = 0;
  14. #define RTC_DBG(lvl, x...) do{ if(c_dbg_lvl & lvl) printk(x);} while(0)
  15. #define RTC_DBG_VAL 1 << 0
  16. #define RTC_DBG_WR 1 << 1
  17. // Define register AO_RTC_ADDR0 bit map
  18. #define RTC_REG0_BIT_sclk_static 20
  19. #define RTC_REG0_BIT_ildo_ctrl_1 7
  20. #define RTC_REG0_BIT_ildo_ctrl_0 6
  21. #define RTC_REG0_BIT_test_mode 5
  22. #define RTC_REG0_BIT_test_clk 4
  23. #define RTC_REG0_BIT_test_bypass 3
  24. #define RTC_REG0_BIT_sdi 2
  25. #define RTC_REG0_BIT_sen 1
  26. #define RTC_REG0_BIT_sclk 0
  27. // Define register AO_RTC_ADDR1 bit map
  28. #define RTC_REG1_BIT_gpo_to_dig 3
  29. #define RTC_REG1_BIT_gpi_to_dig 2
  30. #define RTC_REG1_BIT_s_ready 1
  31. #define RTC_REG1_BIT_sdo 0
  32. // Define register AO_RTC_ADDR3 bit map
  33. #define RTC_REG3_BIT_count_always 17
  34. // Define RTC serial protocal
  35. #define RTC_SER_DATA_BITS 32
  36. #define RTC_SER_ADDR_BITS 3
  37. #define s_ready 1 << RTC_REG1_BIT_s_ready
  38. #define s_do 1 << RTC_REG1_BIT_sdo
  39. #define RESET_RETRY_TIMES 3
  40. #ifdef CONFIG_ARCH_MESON6
  41. #define WR_RTC(addr, data) aml_write_reg32(P_##addr, data)
  42. #define RD_RTC(addr) aml_read_reg32(P_##addr)
  43. #else
  44. #define WR_RTC(addr, data) WRITE_AOBUS_REG(addr, data)
  45. #define RD_RTC(addr) READ_AOBUS_REG(addr)
  46. #endif
  47. #define RTC_sbus_LOW(x) WR_RTC(AO_RTC_ADDR0, \
  48. (RD_RTC(AO_RTC_ADDR0) & \
  49. ~((1<<RTC_REG0_BIT_sen)|(1<<RTC_REG0_BIT_sclk)|(1<<RTC_REG0_BIT_sdi))))
  50. #define RTC_sdi_HIGH(x) WR_RTC(AO_RTC_ADDR0, \
  51. (RD_RTC(AO_RTC_ADDR0) | (1<<RTC_REG0_BIT_sdi) ))
  52. #define RTC_sdi_LOW(x) WR_RTC(AO_RTC_ADDR0, \
  53. (RD_RTC(AO_RTC_ADDR0) & ~(1<<RTC_REG0_BIT_sdi) ))
  54. #define RTC_sen_HIGH(x) WR_RTC(AO_RTC_ADDR0, \
  55. (RD_RTC(AO_RTC_ADDR0) | (1<<RTC_REG0_BIT_sen) ))
  56. #define RTC_sen_LOW(x) WR_RTC(AO_RTC_ADDR0, \
  57. (RD_RTC(AO_RTC_ADDR0) & ~(1<<RTC_REG0_BIT_sen) ))
  58. #define RTC_sclk_HIGH(x) WR_RTC(AO_RTC_ADDR0, \
  59. (RD_RTC(AO_RTC_ADDR0) |(1<<RTC_REG0_BIT_sclk)))
  60. #define RTC_sclk_LOW(x) WR_RTC(AO_RTC_ADDR0, \
  61. (RD_RTC(AO_RTC_ADDR0) & ~(1<<RTC_REG0_BIT_sclk)))
  62. #define RTC_sdo_READBIT (RD_RTC(AO_RTC_ADDR1)&(1<<RTC_REG1_BIT_sdo))
  63. #define RTC_sclk_static_HIGH(x) WR_RTC(AO_RTC_ADDR0, \
  64. (RD_RTC(AO_RTC_ADDR0) |(1<<RTC_REG0_BIT_sclk_static)))
  65. #define RTC_sclk_static_LOW(x) WR_RTC(AO_RTC_ADDR0, \
  66. (RD_RTC(AO_RTC_ADDR0) & ~(1<<RTC_REG0_BIT_sclk_static)))
  67. #define RTC_count_always_HIGH(x) WR_RTC(AO_RTC_ADDR3, \
  68. (RD_RTC(AO_RTC_ADDR3) |(1<<RTC_REG3_BIT_count_always)))
  69. #define RTC_count_always_LOW(x) WR_RTC(AO_RTC_ADDR3, \
  70. (RD_RTC(AO_RTC_ADDR3) & ~(1<<RTC_REG3_BIT_count_always)))
  71. #define RTC_Sdo_READBIT RD_RTC(AO_RTC_ADDR1)&s_do`
  72. #define RTC_SER_REG_DATA_NOTIFIER 0xb41b// Define RTC register address mapping
  73. //#define P_ISA_TIMERE (volatile unsigned long *)0xc1109954
  74. // Define RTC register address mapping
  75. #define RTC_COUNTER_ADDR 0
  76. #define RTC_GPO_COUNTER_ADDR 1
  77. #define RTC_SEC_ADJUST_ADDR 2
  78. #define RTC_UNUSED_ADDR_0 3
  79. #define RTC_REGMEM_ADDR_0 4
  80. #define RTC_REGMEM_ADDR_1 5
  81. #define RTC_REGMEM_ADDR_2 6
  82. #define RTC_REGMEM_ADDR_3 7
  83. struct aml_rtc_priv{
  84. struct rtc_device *rtc;
  85. unsigned long base_addr;
  86. spinlock_t lock;
  87. struct timer_list timer;
  88. struct work_struct work;
  89. struct workqueue_struct *rtc_work_queue;
  90. };
  91. static void reset_gpo_work(struct work_struct *work);
  92. static void delay_us(int us)
  93. {
  94. udelay(us);
  95. }
  96. static void rtc_comm_delay(void)
  97. {
  98. delay_us(5);
  99. }
  100. static void rtc_sclk_pulse(void)
  101. {
  102. //unsigned flags;
  103. //local_irq_save(flags);
  104. rtc_comm_delay();
  105. RTC_sclk_HIGH(1);
  106. rtc_comm_delay();
  107. RTC_sclk_LOW(0);
  108. //local_irq_restore(flags);
  109. }
  110. static int check_osc_clk(void)
  111. {
  112. unsigned long osc_clk_count1;
  113. unsigned long osc_clk_count2;
  114. WR_RTC(AO_RTC_ADDR3, RD_RTC(AO_RTC_ADDR3) | (1 << 17)); // Enable count always
  115. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- check os clk_1\n");
  116. osc_clk_count1 = RD_RTC(AO_RTC_ADDR2); // Wait for 50uS. 32.768khz is 30.5uS. This should be long
  117. // enough for one full cycle of 32.768 khz
  118. RTC_DBG(RTC_DBG_VAL, "the aml_rtc os clk 1 is %d\n", (unsigned int)osc_clk_count1);
  119. delay_us( 50 );
  120. osc_clk_count2 = RD_RTC(AO_RTC_ADDR2);
  121. RTC_DBG(RTC_DBG_VAL, "the aml_rtc os clk 2 is %d\n", (unsigned int)osc_clk_count2);
  122. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- check os clk_2\n");
  123. WR_RTC(AO_RTC_ADDR3, RD_RTC(AO_RTC_ADDR3) & ~(1 << 17)); // disable count always
  124. if( osc_clk_count1 == osc_clk_count2 ) {
  125. RTC_DBG(RTC_DBG_VAL, "The osc_clk is not running now! need to invcrease the power!\n");
  126. return(-1);
  127. }
  128. RTC_DBG(RTC_DBG_VAL, "aml_rtc : check_os_clk\n");
  129. return(0);
  130. }
  131. void rtc_ser_static_write_auto (unsigned long static_reg_data_in)
  132. {
  133. unsigned long data32;
  134. // Program MSB 15-8
  135. data32 = (static_reg_data_in >> 8) & 0xff;
  136. //WRITE_AOBUS_REG(AO_RTC_ADDR4,data32);
  137. WR_RTC(AO_RTC_ADDR4, data32);
  138. // Program LSB 7-0, and start serializing
  139. //data32 = READ_AOBUS_REG(AO_RTC_ADDR0);
  140. data32 = RD_RTC(AO_RTC_ADDR0);
  141. data32 |= 1 << 17; // auto_serialize_start
  142. data32 &= ~(0xff << 24);
  143. data32 |= (static_reg_data_in & 0xff) << 24; // auto_static_reg
  144. //WRITE_AOBUS_REG(AO_RTC_ADDR0,data32);
  145. WR_RTC(AO_RTC_ADDR0,data32);
  146. // Poll auto_serializer_busy bit until it's low (IDLE)
  147. //while ((READ_AOBUS_REG(AO_RTC_ADDR0)) & 1<<22) {}
  148. while ((RD_RTC(AO_RTC_ADDR0)) & 1<<22) {}
  149. }
  150. static void rtc_reset_s_ready(void)
  151. {
  152. //RTC_RESET_BIT_HIGH(1);
  153. delay_us(100);
  154. return;
  155. }
  156. static int rtc_wait_s_ready(void)
  157. {
  158. int i = 40000;
  159. int try_cnt = 0;
  160. /*
  161. while (i--){
  162. if((*(volatile unsigned *)AO_RTC_ADDR1)&s_ready)
  163. break;
  164. }
  165. return i;
  166. */
  167. while(!(RD_RTC(AO_RTC_ADDR1)&s_ready)){
  168. i--;
  169. if(i == 0){
  170. if(try_cnt > RESET_RETRY_TIMES){
  171. break;
  172. }
  173. rtc_reset_s_ready();
  174. try_cnt++;
  175. i = 40000;
  176. }
  177. }
  178. return i;
  179. }
  180. static int rtc_comm_init(void)
  181. {
  182. RTC_sbus_LOW(0);
  183. if(rtc_wait_s_ready()>0){
  184. RTC_sen_HIGH(1);
  185. return 0;
  186. }
  187. return -1;
  188. }
  189. static void rtc_send_bit(unsigned val)
  190. {
  191. if (val)
  192. RTC_sdi_HIGH(1);
  193. else
  194. RTC_sdi_LOW(0);
  195. rtc_sclk_pulse();
  196. }
  197. static void rtc_send_addr_data(unsigned type, unsigned val)
  198. {
  199. unsigned cursor = (type? (1<<(RTC_SER_ADDR_BITS-1)) : (1<<(RTC_SER_DATA_BITS-1)));
  200. while(cursor)
  201. {
  202. rtc_send_bit(val&cursor);
  203. cursor >>= 1;
  204. }
  205. }
  206. static void rtc_get_data(unsigned *val)
  207. {
  208. int i;
  209. RTC_DBG(RTC_DBG_VAL, "rtc-aml -- rtc get data \n");
  210. for (i=0; i<RTC_SER_DATA_BITS; i++)
  211. {
  212. rtc_sclk_pulse();
  213. *val <<= 1;
  214. *val |= RTC_sdo_READBIT;
  215. }
  216. }
  217. static void rtc_set_mode(unsigned mode)
  218. {
  219. RTC_sen_LOW(0);
  220. if (mode)
  221. RTC_sdi_HIGH (1);//WRITE
  222. else
  223. RTC_sdi_LOW(0); //READ
  224. rtc_sclk_pulse();
  225. RTC_sdi_LOW(0);
  226. }
  227. static void static_register_write(unsigned data);
  228. static int ser_access_write(unsigned long addr, unsigned long data);
  229. static void aml_rtc_reset(void)
  230. {
  231. printk("error, the rtc serial communication abnormal, reset the rtc!\n");
  232. #ifdef CONFIG_ARCH_MESON6
  233. WRITE_AOBUS_REG(AO_RTI_GEN_CNTL_REG0, READ_AOBUS_REG(AO_RTI_GEN_CNTL_REG0)|1<<21);
  234. udelay(5);
  235. WRITE_AOBUS_REG(AO_RTI_GEN_CNTL_REG0, READ_AOBUS_REG(AO_RTI_GEN_CNTL_REG0)&(~(1<<21)));
  236. static_register_write(0x180a);
  237. ser_access_write(RTC_GPO_COUNTER_ADDR,0x500000);
  238. #else
  239. WRITE_CBUS_REG(RESET3_REGISTER, 0x1<<3);
  240. #endif
  241. }
  242. static unsigned int ser_access_read(unsigned long addr)
  243. {
  244. unsigned val = 0;
  245. int s_nrdy_cnt = 0;
  246. RTC_DBG(RTC_DBG_VAL, "aml_rtc --ser_access_read_1\n");
  247. /*if(check_osc_clk() < 0){
  248. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- the osc clk does not work\n");
  249. return val;
  250. }*/
  251. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- ser_access_read_2\n");
  252. while(rtc_comm_init()<0){
  253. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- rtc_common_init fail\n");
  254. if(s_nrdy_cnt>RESET_RETRY_TIMES)
  255. aml_rtc_reset();
  256. rtc_reset_s_ready( );
  257. s_nrdy_cnt++;
  258. }
  259. RTC_DBG(RTC_DBG_VAL, "aml_rtc -- ser_access_read_3\n");
  260. rtc_send_addr_data(1,addr);
  261. rtc_set_mode(0); //Read
  262. rtc_get_data(&val);
  263. return val;
  264. }
  265. static int ser_access_write(unsigned long addr, unsigned long data)
  266. {
  267. int s_nrdy_cnt = 0;
  268. while(rtc_comm_init()<0){
  269. if(s_nrdy_cnt>RESET_RETRY_TIMES) {
  270. aml_rtc_reset();
  271. printk("error: rtc serial communication abnormal!\n");
  272. //return -1;
  273. }
  274. rtc_reset_s_ready( );
  275. s_nrdy_cnt++;
  276. }
  277. rtc_send_addr_data(0,data);
  278. rtc_send_addr_data(1,addr);
  279. rtc_set_mode(1); //Write
  280. rtc_wait_s_ready();
  281. return 0;
  282. }
  283. /***************************************************************************/
  284. int rtc_reset_gpo(struct device *dev, unsigned level)
  285. {
  286. unsigned data = 0;
  287. struct aml_rtc_priv *priv;
  288. unsigned long flags;
  289. priv = dev_get_drvdata(dev);
  290. data |= 1<<20;
  291. #ifdef CONFIG_ARCH_MESON6
  292. if(level){
  293. data |= 1<<22; //gpo pin level high
  294. }
  295. #else
  296. //reset mode
  297. if(!level){
  298. data |= 1<<22; //gpo pin level high
  299. }
  300. #endif
  301. spin_lock_irqsave(&priv->lock, flags);
  302. ser_access_write(RTC_GPO_COUNTER_ADDR, data);
  303. rtc_wait_s_ready();
  304. spin_unlock_irqrestore(&priv->lock, flags);
  305. return 0;
  306. }
  307. typedef struct alarm_data_s{
  308. int level;
  309. unsigned alarm_sec; //in s
  310. } alarm_data_t;
  311. /*
  312. * Return RTC_GPO_COUNTER bit-24 value.
  313. */
  314. int aml_rtc_alarm_status(void)
  315. {
  316. u32 data32 = ser_access_read(RTC_GPO_COUNTER_ADDR);
  317. RTC_DBG(RTC_DBG_VAL, "%s() RTC_GPO_COUNTER=%x\n", __func__, data32);
  318. return (data32 & (1 << 24));
  319. }
  320. //set the rtc alarm
  321. //after alarm_data->alarm_sec, the gpo lvl will be //alarm_data->level
  322. int rtc_set_alarm_aml(struct device *dev, alarm_data_t *alarm_data) {
  323. unsigned data = 0;
  324. //reset the gpo level
  325. struct aml_rtc_priv *priv;
  326. unsigned long flags;
  327. priv = dev_get_drvdata(dev);
  328. rtc_reset_gpo(dev, !(alarm_data->level));
  329. data |= 2 << 20; //output defined level after time
  330. #ifdef CONFIG_ARCH_MESON6
  331. data |= (alarm_data->level & 1) << 22; //
  332. #else
  333. data |= (!(alarm_data->level & 1)) << 22; //
  334. #endif
  335. if(alarm_data->alarm_sec >= 1024*1024){
  336. return -1;
  337. }
  338. data |= alarm_data->alarm_sec - 1;
  339. spin_lock_irqsave(&priv->lock, flags);
  340. ser_access_write(RTC_GPO_COUNTER_ADDR, data);
  341. rtc_wait_s_ready();
  342. rtc_comm_delay();
  343. spin_unlock_irqrestore(&priv->lock, flags);
  344. return 0;
  345. }
  346. /*************************************************************************/
  347. // -----------------------------------------------------------------------------
  348. // Function: rtc_ser_static_write_manual
  349. // Use part of the serial bus: sclk_static, sdi and sen to shift
  350. // in a 16-bit static data. Manual mode.
  351. // -----------------------------------------------------------------------------
  352. //static void rtc_ser_static_write_manual (unsigned int static_reg_data_in)
  353. //{
  354. // int i;
  355. // RTC_DBG(RTC_DBG_VAL, "rtc_ser_static_write_manual: data=0x%04x\n", static_reg_data_in);
  356. //
  357. // // Initialize: sen low for 1 clock cycle
  358. // RTC_sen_LOW(0);
  359. // RTC_sclk_static_LOW(0);
  360. // RTC_sclk_static_HIGH(1);
  361. // RTC_sen_HIGH(1);
  362. // RTC_sclk_static_LOW(0);
  363. //
  364. // // Shift in 16-bit known sequence
  365. // for (i = 15; i >= 0; i --) {
  366. //
  367. // if ((RTC_SER_REG_DATA_NOTIFIER >> i) & 0x1) {
  368. // RTC_sdi_HIGH(1);
  369. // }
  370. // else {
  371. // RTC_sdi_LOW(0);
  372. // }
  373. //
  374. // RTC_sclk_static_HIGH(1);
  375. // RTC_sclk_static_LOW(0);
  376. // }
  377. //
  378. // // 1 clock cycle turn around
  379. // RTC_sdi_LOW(0);
  380. // RTC_sclk_static_HIGH(1);
  381. // RTC_sclk_static_LOW(0);
  382. //
  383. // // Shift in 16-bit static register data
  384. // for (i = 15; i >= 0; i --) {
  385. // if ((static_reg_data_in >> i) & 0x1) {
  386. // RTC_sdi_HIGH(1);
  387. // }
  388. // else {
  389. // RTC_sdi_LOW(0);
  390. // }
  391. // RTC_sclk_static_HIGH(1);
  392. // RTC_sclk_static_LOW(0);
  393. // }
  394. //
  395. // // One more clock cycle to complete write
  396. // RTC_sen_LOW(0);
  397. // RTC_sdi_LOW(0);
  398. // RTC_sclk_static_HIGH(1);
  399. // RTC_sclk_static_LOW(0);
  400. //}
  401. static void static_register_write(unsigned data)
  402. {
  403. rtc_ser_static_write_auto(data);
  404. }
  405. static int aml_rtc_read_time(struct device *dev, struct rtc_time *tm)
  406. {
  407. unsigned int time_t;
  408. struct aml_rtc_priv *priv;
  409. unsigned long flags;
  410. priv = dev_get_drvdata(dev);
  411. RTC_DBG(RTC_DBG_VAL, "aml_rtc: read rtc time\n");
  412. spin_lock_irqsave(&priv->lock, flags);
  413. time_t = ser_access_read(RTC_COUNTER_ADDR);
  414. spin_unlock_irqrestore(&priv->lock, flags);
  415. RTC_DBG(RTC_DBG_VAL, "aml_rtc: have read the rtc time, time is %d\n", time_t);
  416. if ((int)time_t < 0) {
  417. RTC_DBG(RTC_DBG_VAL, "aml_rtc: time(%d) < 0, reset to 0", time_t);
  418. time_t = 0;
  419. }
  420. rtc_time_to_tm(time_t, tm);
  421. return 0;
  422. }
  423. static int aml_rtc_write_time(struct device *dev, struct rtc_time *tm)
  424. {
  425. unsigned long time_t;
  426. struct aml_rtc_priv *priv;
  427. unsigned long flags;
  428. priv = dev_get_drvdata(dev);
  429. rtc_tm_to_time(tm, &time_t);
  430. spin_lock_irqsave(&priv->lock, flags);
  431. RTC_DBG(RTC_DBG_VAL, "aml_rtc : write the rtc time, time is %ld\n", time_t);
  432. ser_access_write(RTC_COUNTER_ADDR, time_t);
  433. RTC_DBG(RTC_DBG_VAL, "aml_rtc : the time has been written\n");
  434. spin_unlock_irqrestore(&priv->lock, flags);
  435. return 0;
  436. }
  437. static int aml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  438. {
  439. alarm_data_t alarm_data;
  440. unsigned long alarm_secs, cur_secs;
  441. struct rtc_time cur_time;
  442. int ret;
  443. struct aml_rtc_priv *priv;
  444. unsigned long flags;
  445. priv = dev_get_drvdata(dev);
  446. //rtc_tm_to_time(&alarm->time, &secs);
  447. if (alarm->enabled) {
  448. alarm_data.level = 0;
  449. ret = rtc_tm_to_time(&alarm->time, &alarm_secs);
  450. if (ret)
  451. return ret;
  452. aml_rtc_read_time(dev, &cur_time);
  453. ret = rtc_tm_to_time(&cur_time, &cur_secs);
  454. if(alarm_secs >= cur_secs)
  455. alarm_data.alarm_sec = alarm_secs - cur_secs + 3; //3 seconds later then we real wanted, we do not need the alarm very acurate.
  456. else
  457. alarm_data.alarm_sec = 0;
  458. rtc_set_alarm_aml(dev, &alarm_data);
  459. } else {
  460. spin_lock_irqsave(&priv->lock, flags);
  461. #ifdef CONFIG_ARCH_MESON6
  462. ser_access_write(RTC_GPO_COUNTER_ADDR,0x500000);
  463. #else
  464. ser_access_write(RTC_GPO_COUNTER_ADDR,0x100000);
  465. #endif
  466. spin_unlock_irqrestore(&priv->lock, flags);
  467. queue_work(priv->rtc_work_queue, &priv->work);
  468. }
  469. return 0;
  470. }
  471. #define AUTO_RESUME_INTERVAL 8
  472. static int aml_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  473. {
  474. #ifdef CONFIG_MESON_SUSPEND_TEST
  475. alarm_data_t alarm_data;
  476. static int suspend_time = 0;
  477. alarm_data.alarm_sec = AUTO_RESUME_INTERVAL;
  478. alarm_data.level = 0;
  479. rtc_set_alarm_aml(&pdev->dev, &alarm_data);
  480. printk("suspend %d times, system will up %ds later!\n", ++suspend_time, alarm_data.alarm_sec);
  481. #endif /* CONFIG_MESON_SUSPEND_TEST */
  482. return 0;
  483. }
  484. static char *rtc_reg[8]={
  485. "RTC_COUNTER ",
  486. "RTC_GPO_COUNTER",
  487. "RTC_SEC_ADJUST ",
  488. "UNUSED ",
  489. "RTC_REGMEM_0 ",
  490. "RTC_REGMEM_1 ",
  491. "RTC_REGMEM_2 ",
  492. "RTC_REGMEM_3 "
  493. };
  494. static ssize_t show_rtc_reg(struct class *class, struct class_attribute *attr, char *buf)
  495. {
  496. int i;
  497. printk("enter function: %s \n",__FUNCTION__);
  498. for(i=0;i<8;i++)
  499. {
  500. printk(" %20s : 0x%x \n",rtc_reg[i],ser_access_read(i));
  501. }
  502. return 0;
  503. }
  504. static const struct rtc_class_ops aml_rtc_ops ={
  505. .read_time = aml_rtc_read_time,
  506. .set_time = aml_rtc_write_time,
  507. .set_alarm = aml_rtc_set_alarm,
  508. };
  509. static struct class_attribute rtc_class_attrs[] = {
  510. __ATTR(rtc_reg_log, S_IRUGO | S_IWUSR, show_rtc_reg, NULL),
  511. __ATTR_NULL
  512. };
  513. static struct class aml_rtc_class = {
  514. .name = "aml_rtc",
  515. .class_attrs = rtc_class_attrs,
  516. };
  517. static int aml_rtc_probe(struct platform_device *pdev)
  518. {
  519. struct aml_rtc_priv *priv;
  520. int ret;
  521. unsigned long flags;
  522. priv = (struct aml_rtc_priv *)kzalloc(sizeof(*priv), GFP_KERNEL);
  523. if(!priv)
  524. return -ENOMEM;
  525. INIT_WORK(&priv->work, reset_gpo_work);
  526. platform_set_drvdata(pdev, priv);
  527. priv->rtc_work_queue = create_singlethread_workqueue("rtc");
  528. if (priv->rtc_work_queue == NULL) {
  529. ret = -ENOMEM;
  530. goto out;
  531. }
  532. spin_lock_init(&priv->lock);
  533. priv->rtc = rtc_device_register("aml_rtc", &pdev->dev, &aml_rtc_ops, THIS_MODULE);
  534. if(IS_ERR(priv->rtc)){
  535. ret = PTR_ERR(priv->rtc);
  536. goto out;
  537. }
  538. //ser_access_write(RTC_GPO_COUNTER_ADDR,0x100000);
  539. //static_register_write(0x0004);
  540. spin_lock_irqsave(&priv->lock, flags);
  541. #ifdef CONFIG_ARCH_MESON6
  542. ser_access_write(RTC_GPO_COUNTER_ADDR,0x500000);
  543. #else
  544. ser_access_write(RTC_GPO_COUNTER_ADDR,0x100000);
  545. #endif
  546. rtc_wait_s_ready();
  547. spin_unlock_irqrestore(&priv->lock, flags);
  548. //check_osc_clk();
  549. //ser_access_write(RTC_COUNTER_ADDR, 0);
  550. ret = class_register(&aml_rtc_class);
  551. if(ret){
  552. printk(" class register nand_class fail!\n");
  553. }
  554. return 0;
  555. out:
  556. if(priv->rtc_work_queue)
  557. destroy_workqueue(priv->rtc_work_queue);
  558. kfree(priv);
  559. return ret;
  560. }
  561. static int get_gpo_flag(struct aml_rtc_priv *priv)
  562. {
  563. u32 data32 = 0;
  564. int ret = 0;
  565. unsigned long flags;
  566. spin_lock_irqsave(&priv->lock, flags);
  567. data32 = ser_access_read(RTC_GPO_COUNTER_ADDR);
  568. spin_unlock_irqrestore(&priv->lock, flags);
  569. RTC_DBG(RTC_DBG_VAL, "%s() RTC_GPO_COUNTER=%x\n", __func__, data32);
  570. #ifdef CONFIG_ARCH_MESON6
  571. ret = !(data32 & (1 << 24));
  572. #else
  573. ret = !!(data32 & (1 << 24));
  574. #endif
  575. return ret;
  576. }
  577. static void reset_gpo_work(struct work_struct *work)
  578. {
  579. struct aml_rtc_priv *priv = container_of(work, struct aml_rtc_priv, work);
  580. int count = 5;
  581. unsigned long flags;
  582. while(get_gpo_flag(priv)) {
  583. spin_lock_irqsave(&priv->lock, flags);
  584. #ifdef CONFIG_ARCH_MESON6
  585. ser_access_write(RTC_GPO_COUNTER_ADDR,0x500000);
  586. #else
  587. ser_access_write(RTC_GPO_COUNTER_ADDR,0x100000);
  588. #endif
  589. spin_unlock_irqrestore(&priv->lock, flags);
  590. count--;
  591. if(count <= 0) {
  592. printk("error: can not reset gpo !\n");
  593. count = 5;
  594. //panic("gpo can not be reset");
  595. }
  596. }
  597. printk("reset gpo !\n");
  598. }
  599. static int power_down_gpo(unsigned long data)
  600. {
  601. struct aml_rtc_priv *priv = (struct aml_rtc_priv *)data;
  602. queue_work(priv->rtc_work_queue, &priv->work);
  603. return 0;
  604. }
  605. static int aml_rtc_shutdown(struct platform_device *pdev)
  606. {
  607. struct aml_rtc_priv *priv;
  608. unsigned long flags;
  609. priv = platform_get_drvdata(pdev);
  610. spin_lock_irqsave(&priv->lock, flags);
  611. #ifdef CONFIG_ARCH_MESON6
  612. ser_access_write(RTC_GPO_COUNTER_ADDR,0x500000);
  613. #else
  614. ser_access_write(RTC_GPO_COUNTER_ADDR,0x100000);
  615. #endif
  616. spin_unlock_irqrestore(&priv->lock, flags);
  617. return 0;
  618. }
  619. static int aml_rtc_remove(struct platform_device *dev)
  620. {
  621. struct aml_rtc_priv *priv = platform_get_drvdata(dev);
  622. rtc_device_unregister(priv->rtc);
  623. if (priv->rtc_work_queue)
  624. destroy_workqueue(priv->rtc_work_queue);
  625. del_timer(&priv->timer);
  626. kfree(priv);
  627. return 0;
  628. }
  629. struct platform_driver aml_rtc_driver = {
  630. .driver = {
  631. .name = "aml_rtc",
  632. .owner = THIS_MODULE,
  633. },
  634. .probe = aml_rtc_probe,
  635. .remove = __devexit_p(aml_rtc_remove),
  636. .suspend = aml_rtc_suspend,
  637. .shutdown = aml_rtc_shutdown,
  638. };
  639. static int __init aml_rtc_init(void)
  640. {
  641. #ifdef CONFIG_ARCH_MESON6
  642. static_register_write(0x180a);
  643. #else
  644. static_register_write(0x3c0a);
  645. #endif
  646. return platform_driver_register(&aml_rtc_driver);
  647. }
  648. static void __init aml_rtc_exit(void)
  649. {
  650. return platform_driver_unregister(&aml_rtc_driver);
  651. }
  652. module_init(aml_rtc_init);
  653. module_exit(aml_rtc_exit);
  654. MODULE_DESCRIPTION("Amlogic internal rtc driver");
  655. MODULE_LICENSE("GPL");