pm.c 8.2 KB

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