pm.c 8.2 KB

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