pm.c 6.1 KB

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