pm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * FR-V Power Management Routines
  3. *
  4. * Copyright (c) 2004 Red Hat, Inc.
  5. *
  6. * Based on SA1100 version:
  7. * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/pm.h>
  16. #include <linux/pm_legacy.h>
  17. #include <linux/sched.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/mb86943a.h>
  24. #include "local.h"
  25. /*
  26. * Debug macros
  27. */
  28. #define DEBUG
  29. int pm_do_suspend(void)
  30. {
  31. local_irq_disable();
  32. __set_LEDS(0xb1);
  33. /* go zzz */
  34. frv_cpu_suspend(pdm_suspend_mode);
  35. __set_LEDS(0xb2);
  36. local_irq_enable();
  37. return 0;
  38. }
  39. static unsigned long __irq_mask;
  40. /*
  41. * Setup interrupt masks, etc to enable wakeup by power switch
  42. */
  43. static void __default_power_switch_setup(void)
  44. {
  45. /* default is to mask all interrupt sources. */
  46. __irq_mask = *(unsigned long *)0xfeff9820;
  47. *(unsigned long *)0xfeff9820 = 0xfffe0000;
  48. }
  49. /*
  50. * Cleanup interrupt masks, etc after wakeup by power switch
  51. */
  52. static void __default_power_switch_cleanup(void)
  53. {
  54. *(unsigned long *)0xfeff9820 = __irq_mask;
  55. }
  56. /*
  57. * Return non-zero if wakeup irq was caused by power switch
  58. */
  59. static int __default_power_switch_check(void)
  60. {
  61. return 1;
  62. }
  63. void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
  64. int (*__power_switch_wake_check)(void) = __default_power_switch_check;
  65. void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
  66. int pm_do_bus_sleep(void)
  67. {
  68. local_irq_disable();
  69. /*
  70. * Here is where we need some platform-dependent setup
  71. * of the interrupt state so that appropriate wakeup
  72. * sources are allowed and all others are masked.
  73. */
  74. __power_switch_wake_setup();
  75. __set_LEDS(0xa1);
  76. /* go zzz
  77. *
  78. * This is in a loop in case power switch shares an irq with other
  79. * devices. The wake_check() tells us if we need to finish waking
  80. * or go back to sleep.
  81. */
  82. do {
  83. frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
  84. } while (__power_switch_wake_check && !__power_switch_wake_check());
  85. __set_LEDS(0xa2);
  86. /*
  87. * Here is where we need some platform-dependent restore
  88. * of the interrupt state prior to being called.
  89. */
  90. __power_switch_wake_cleanup();
  91. local_irq_enable();
  92. return 0;
  93. }
  94. unsigned long sleep_phys_sp(void *sp)
  95. {
  96. return virt_to_phys(sp);
  97. }
  98. #ifdef CONFIG_SYSCTL
  99. /*
  100. * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
  101. * when all the PM interfaces exist nicely.
  102. */
  103. #define CTL_PM_SUSPEND 1
  104. #define CTL_PM_CMODE 2
  105. #define CTL_PM_P0 4
  106. #define CTL_PM_CM 5
  107. static int user_atoi(char __user *ubuf, size_t len)
  108. {
  109. char buf[16];
  110. unsigned long ret;
  111. if (len > 15)
  112. return -EINVAL;
  113. if (copy_from_user(buf, ubuf, len))
  114. return -EFAULT;
  115. buf[len] = 0;
  116. ret = simple_strtoul(buf, NULL, 0);
  117. if (ret > INT_MAX)
  118. return -ERANGE;
  119. return ret;
  120. }
  121. /*
  122. * Send us to sleep.
  123. */
  124. static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
  125. void __user *buffer, size_t *lenp, loff_t *fpos)
  126. {
  127. int retval, mode;
  128. if (*lenp <= 0)
  129. return -EIO;
  130. mode = user_atoi(buffer, *lenp);
  131. if ((mode != 1) && (mode != 5))
  132. return -EINVAL;
  133. if (retval == 0) {
  134. if (mode == 5)
  135. retval = pm_do_bus_sleep();
  136. else
  137. retval = pm_do_suspend();
  138. }
  139. return retval;
  140. }
  141. static int try_set_cmode(int new_cmode)
  142. {
  143. if (new_cmode > 15)
  144. return -EINVAL;
  145. if (!(clock_cmodes_permitted & (1<<new_cmode)))
  146. return -EINVAL;
  147. /* now change cmode */
  148. local_irq_disable();
  149. frv_dma_pause_all();
  150. frv_change_cmode(new_cmode);
  151. determine_clocks(0);
  152. time_divisor_init();
  153. #ifdef DEBUG
  154. determine_clocks(1);
  155. #endif
  156. frv_dma_resume_all();
  157. local_irq_enable();
  158. return 0;
  159. }
  160. static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
  161. void __user *buffer, size_t *lenp, loff_t *fpos)
  162. {
  163. int new_cmode;
  164. if (!write)
  165. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  166. new_cmode = user_atoi(buffer, *lenp);
  167. return try_set_cmode(new_cmode)?:*lenp;
  168. }
  169. static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
  170. void __user *oldval, size_t __user *oldlenp,
  171. void __user *newval, size_t newlen)
  172. {
  173. if (oldval && oldlenp) {
  174. size_t oldlen;
  175. if (get_user(oldlen, oldlenp))
  176. return -EFAULT;
  177. if (oldlen != sizeof(int))
  178. return -EINVAL;
  179. if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
  180. put_user(sizeof(int), oldlenp))
  181. return -EFAULT;
  182. }
  183. if (newval && newlen) {
  184. int new_cmode;
  185. if (newlen != sizeof(int))
  186. return -EINVAL;
  187. if (get_user(new_cmode, (int __user *)newval))
  188. return -EFAULT;
  189. return try_set_cmode(new_cmode)?:1;
  190. }
  191. return 1;
  192. }
  193. static int try_set_p0(int new_p0)
  194. {
  195. unsigned long flags, clkc;
  196. if (new_p0 < 0 || new_p0 > 1)
  197. return -EINVAL;
  198. local_irq_save(flags);
  199. __set_PSR(flags & ~PSR_ET);
  200. frv_dma_pause_all();
  201. clkc = __get_CLKC();
  202. if (new_p0)
  203. clkc |= CLKC_P0;
  204. else
  205. clkc &= ~CLKC_P0;
  206. __set_CLKC(clkc);
  207. determine_clocks(0);
  208. time_divisor_init();
  209. #ifdef DEBUG
  210. determine_clocks(1);
  211. #endif
  212. frv_dma_resume_all();
  213. local_irq_restore(flags);
  214. return 0;
  215. }
  216. static int try_set_cm(int new_cm)
  217. {
  218. unsigned long flags, clkc;
  219. if (new_cm < 0 || new_cm > 1)
  220. return -EINVAL;
  221. local_irq_save(flags);
  222. __set_PSR(flags & ~PSR_ET);
  223. frv_dma_pause_all();
  224. clkc = __get_CLKC();
  225. clkc &= ~CLKC_CM;
  226. clkc |= new_cm;
  227. __set_CLKC(clkc);
  228. determine_clocks(0);
  229. time_divisor_init();
  230. #if 1 //def DEBUG
  231. determine_clocks(1);
  232. #endif
  233. frv_dma_resume_all();
  234. local_irq_restore(flags);
  235. return 0;
  236. }
  237. static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
  238. void __user *buffer, size_t *lenp, loff_t *fpos)
  239. {
  240. int new_p0;
  241. if (!write)
  242. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  243. new_p0 = user_atoi(buffer, *lenp);
  244. return try_set_p0(new_p0)?:*lenp;
  245. }
  246. static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
  247. void __user *oldval, size_t __user *oldlenp,
  248. void __user *newval, size_t newlen)
  249. {
  250. if (oldval && oldlenp) {
  251. size_t oldlen;
  252. if (get_user(oldlen, oldlenp))
  253. return -EFAULT;
  254. if (oldlen != sizeof(int))
  255. return -EINVAL;
  256. if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
  257. put_user(sizeof(int), oldlenp))
  258. return -EFAULT;
  259. }
  260. if (newval && newlen) {
  261. int new_p0;
  262. if (newlen != sizeof(int))
  263. return -EINVAL;
  264. if (get_user(new_p0, (int __user *)newval))
  265. return -EFAULT;
  266. return try_set_p0(new_p0)?:1;
  267. }
  268. return 1;
  269. }
  270. static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
  271. void __user *buffer, size_t *lenp, loff_t *fpos)
  272. {
  273. int new_cm;
  274. if (!write)
  275. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  276. new_cm = user_atoi(buffer, *lenp);
  277. return try_set_cm(new_cm)?:*lenp;
  278. }
  279. static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
  280. void __user *oldval, size_t __user *oldlenp,
  281. void __user *newval, size_t newlen)
  282. {
  283. if (oldval && oldlenp) {
  284. size_t oldlen;
  285. if (get_user(oldlen, oldlenp))
  286. return -EFAULT;
  287. if (oldlen != sizeof(int))
  288. return -EINVAL;
  289. if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
  290. put_user(sizeof(int), oldlenp))
  291. return -EFAULT;
  292. }
  293. if (newval && newlen) {
  294. int new_cm;
  295. if (newlen != sizeof(int))
  296. return -EINVAL;
  297. if (get_user(new_cm, (int __user *)newval))
  298. return -EFAULT;
  299. return try_set_cm(new_cm)?:1;
  300. }
  301. return 1;
  302. }
  303. static struct ctl_table pm_table[] =
  304. {
  305. {
  306. .ctl_name = CTL_PM_SUSPEND,
  307. .procname = "suspend",
  308. .data = NULL,
  309. .maxlen = 0,
  310. .mode = 0200,
  311. .proc_handler = &sysctl_pm_do_suspend,
  312. },
  313. {
  314. .ctl_name = CTL_PM_CMODE,
  315. .procname = "cmode",
  316. .data = &clock_cmode_current,
  317. .maxlen = sizeof(int),
  318. .mode = 0644,
  319. .proc_handler = &cmode_procctl,
  320. .strategy = &cmode_sysctl,
  321. },
  322. {
  323. .ctl_name = CTL_PM_P0,
  324. .procname = "p0",
  325. .data = &clock_p0_current,
  326. .maxlen = sizeof(int),
  327. .mode = 0644,
  328. .proc_handler = &p0_procctl,
  329. .strategy = &p0_sysctl,
  330. },
  331. {
  332. .ctl_name = CTL_PM_CM,
  333. .procname = "cm",
  334. .data = &clock_cm_current,
  335. .maxlen = sizeof(int),
  336. .mode = 0644,
  337. .proc_handler = &cm_procctl,
  338. .strategy = &cm_sysctl,
  339. },
  340. { .ctl_name = 0}
  341. };
  342. static struct ctl_table pm_dir_table[] =
  343. {
  344. {
  345. .ctl_name = CTL_PM,
  346. .procname = "pm",
  347. .mode = 0555,
  348. .child = pm_table,
  349. },
  350. { .ctl_name = 0}
  351. };
  352. /*
  353. * Initialize power interface
  354. */
  355. static int __init pm_init(void)
  356. {
  357. register_sysctl_table(pm_dir_table);
  358. return 0;
  359. }
  360. __initcall(pm_init);
  361. #endif