via_clock.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 1998-2008 VIA Technologies, Inc. All Rights Reserved.
  3. * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
  4. * Copyright 2011 Florian Tobias Schandinat <FlorianSchandinat@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * either version 2, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
  13. * the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE.See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. /*
  23. * clock and PLL management functions
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/via-core.h>
  27. #include <asm/olpc.h>
  28. #include "via_clock.h"
  29. #include "global.h"
  30. #include "debug.h"
  31. static const char *via_slap = "Please slap VIA Technologies to motivate them "
  32. "releasing full documentation for your platform!\n";
  33. static inline u32 cle266_encode_pll(struct via_pll_config pll)
  34. {
  35. return (pll.multiplier << 8)
  36. | (pll.rshift << 6)
  37. | pll.divisor;
  38. }
  39. static inline u32 k800_encode_pll(struct via_pll_config pll)
  40. {
  41. return ((pll.divisor - 2) << 16)
  42. | (pll.rshift << 10)
  43. | (pll.multiplier - 2);
  44. }
  45. static inline u32 vx855_encode_pll(struct via_pll_config pll)
  46. {
  47. return (pll.divisor << 16)
  48. | (pll.rshift << 10)
  49. | pll.multiplier;
  50. }
  51. static inline void cle266_set_primary_pll_encoded(u32 data)
  52. {
  53. via_write_reg_mask(VIASR, 0x40, 0x02, 0x02); /* enable reset */
  54. via_write_reg(VIASR, 0x46, data & 0xFF);
  55. via_write_reg(VIASR, 0x47, (data >> 8) & 0xFF);
  56. via_write_reg_mask(VIASR, 0x40, 0x00, 0x02); /* disable reset */
  57. }
  58. static inline void k800_set_primary_pll_encoded(u32 data)
  59. {
  60. via_write_reg_mask(VIASR, 0x40, 0x02, 0x02); /* enable reset */
  61. via_write_reg(VIASR, 0x44, data & 0xFF);
  62. via_write_reg(VIASR, 0x45, (data >> 8) & 0xFF);
  63. via_write_reg(VIASR, 0x46, (data >> 16) & 0xFF);
  64. via_write_reg_mask(VIASR, 0x40, 0x00, 0x02); /* disable reset */
  65. }
  66. static inline void cle266_set_secondary_pll_encoded(u32 data)
  67. {
  68. via_write_reg_mask(VIASR, 0x40, 0x04, 0x04); /* enable reset */
  69. via_write_reg(VIASR, 0x44, data & 0xFF);
  70. via_write_reg(VIASR, 0x45, (data >> 8) & 0xFF);
  71. via_write_reg_mask(VIASR, 0x40, 0x00, 0x04); /* disable reset */
  72. }
  73. static inline void k800_set_secondary_pll_encoded(u32 data)
  74. {
  75. via_write_reg_mask(VIASR, 0x40, 0x04, 0x04); /* enable reset */
  76. via_write_reg(VIASR, 0x4A, data & 0xFF);
  77. via_write_reg(VIASR, 0x4B, (data >> 8) & 0xFF);
  78. via_write_reg(VIASR, 0x4C, (data >> 16) & 0xFF);
  79. via_write_reg_mask(VIASR, 0x40, 0x00, 0x04); /* disable reset */
  80. }
  81. static inline void set_engine_pll_encoded(u32 data)
  82. {
  83. via_write_reg_mask(VIASR, 0x40, 0x01, 0x01); /* enable reset */
  84. via_write_reg(VIASR, 0x47, data & 0xFF);
  85. via_write_reg(VIASR, 0x48, (data >> 8) & 0xFF);
  86. via_write_reg(VIASR, 0x49, (data >> 16) & 0xFF);
  87. via_write_reg_mask(VIASR, 0x40, 0x00, 0x01); /* disable reset */
  88. }
  89. static void cle266_set_primary_pll(struct via_pll_config config)
  90. {
  91. cle266_set_primary_pll_encoded(cle266_encode_pll(config));
  92. }
  93. static void k800_set_primary_pll(struct via_pll_config config)
  94. {
  95. k800_set_primary_pll_encoded(k800_encode_pll(config));
  96. }
  97. static void vx855_set_primary_pll(struct via_pll_config config)
  98. {
  99. k800_set_primary_pll_encoded(vx855_encode_pll(config));
  100. }
  101. static void cle266_set_secondary_pll(struct via_pll_config config)
  102. {
  103. cle266_set_secondary_pll_encoded(cle266_encode_pll(config));
  104. }
  105. static void k800_set_secondary_pll(struct via_pll_config config)
  106. {
  107. k800_set_secondary_pll_encoded(k800_encode_pll(config));
  108. }
  109. static void vx855_set_secondary_pll(struct via_pll_config config)
  110. {
  111. k800_set_secondary_pll_encoded(vx855_encode_pll(config));
  112. }
  113. static void k800_set_engine_pll(struct via_pll_config config)
  114. {
  115. set_engine_pll_encoded(k800_encode_pll(config));
  116. }
  117. static void vx855_set_engine_pll(struct via_pll_config config)
  118. {
  119. set_engine_pll_encoded(vx855_encode_pll(config));
  120. }
  121. static void set_primary_pll_state(u8 state)
  122. {
  123. u8 value;
  124. switch (state) {
  125. case VIA_STATE_ON:
  126. value = 0x20;
  127. break;
  128. case VIA_STATE_OFF:
  129. value = 0x00;
  130. break;
  131. default:
  132. return;
  133. }
  134. via_write_reg_mask(VIASR, 0x2D, value, 0x30);
  135. }
  136. static void set_secondary_pll_state(u8 state)
  137. {
  138. u8 value;
  139. switch (state) {
  140. case VIA_STATE_ON:
  141. value = 0x08;
  142. break;
  143. case VIA_STATE_OFF:
  144. value = 0x00;
  145. break;
  146. default:
  147. return;
  148. }
  149. via_write_reg_mask(VIASR, 0x2D, value, 0x0C);
  150. }
  151. static void set_engine_pll_state(u8 state)
  152. {
  153. u8 value;
  154. switch (state) {
  155. case VIA_STATE_ON:
  156. value = 0x02;
  157. break;
  158. case VIA_STATE_OFF:
  159. value = 0x00;
  160. break;
  161. default:
  162. return;
  163. }
  164. via_write_reg_mask(VIASR, 0x2D, value, 0x03);
  165. }
  166. static void set_primary_clock_state(u8 state)
  167. {
  168. u8 value;
  169. switch (state) {
  170. case VIA_STATE_ON:
  171. value = 0x20;
  172. break;
  173. case VIA_STATE_OFF:
  174. value = 0x00;
  175. break;
  176. default:
  177. return;
  178. }
  179. via_write_reg_mask(VIASR, 0x1B, value, 0x30);
  180. }
  181. static void set_secondary_clock_state(u8 state)
  182. {
  183. u8 value;
  184. switch (state) {
  185. case VIA_STATE_ON:
  186. value = 0x80;
  187. break;
  188. case VIA_STATE_OFF:
  189. value = 0x00;
  190. break;
  191. default:
  192. return;
  193. }
  194. via_write_reg_mask(VIASR, 0x1B, value, 0xC0);
  195. }
  196. static inline u8 set_clock_source_common(enum via_clksrc source, bool use_pll)
  197. {
  198. u8 data = 0;
  199. switch (source) {
  200. case VIA_CLKSRC_X1:
  201. data = 0x00;
  202. break;
  203. case VIA_CLKSRC_TVX1:
  204. data = 0x02;
  205. break;
  206. case VIA_CLKSRC_TVPLL:
  207. data = 0x04; /* 0x06 should be the same */
  208. break;
  209. case VIA_CLKSRC_DVP1TVCLKR:
  210. data = 0x0A;
  211. break;
  212. case VIA_CLKSRC_CAP0:
  213. data = 0xC;
  214. break;
  215. case VIA_CLKSRC_CAP1:
  216. data = 0x0E;
  217. break;
  218. }
  219. if (!use_pll)
  220. data |= 1;
  221. return data;
  222. }
  223. static void set_primary_clock_source(enum via_clksrc source, bool use_pll)
  224. {
  225. u8 data = set_clock_source_common(source, use_pll) << 4;
  226. via_write_reg_mask(VIACR, 0x6C, data, 0xF0);
  227. }
  228. static void set_secondary_clock_source(enum via_clksrc source, bool use_pll)
  229. {
  230. u8 data = set_clock_source_common(source, use_pll);
  231. via_write_reg_mask(VIACR, 0x6C, data, 0x0F);
  232. }
  233. static void dummy_set_clock_state(u8 state)
  234. {
  235. printk(KERN_INFO "Using undocumented set clock state.\n%s", via_slap);
  236. }
  237. static void dummy_set_clock_source(enum via_clksrc source, bool use_pll)
  238. {
  239. printk(KERN_INFO "Using undocumented set clock source.\n%s", via_slap);
  240. }
  241. static void dummy_set_pll_state(u8 state)
  242. {
  243. printk(KERN_INFO "Using undocumented set PLL state.\n%s", via_slap);
  244. }
  245. static void dummy_set_pll(struct via_pll_config config)
  246. {
  247. printk(KERN_INFO "Using undocumented set PLL.\n%s", via_slap);
  248. }
  249. static void noop_set_clock_state(u8 state)
  250. {
  251. }
  252. void via_clock_init(struct via_clock *clock, int gfx_chip)
  253. {
  254. switch (gfx_chip) {
  255. case UNICHROME_CLE266:
  256. case UNICHROME_K400:
  257. clock->set_primary_clock_state = dummy_set_clock_state;
  258. clock->set_primary_clock_source = dummy_set_clock_source;
  259. clock->set_primary_pll_state = dummy_set_pll_state;
  260. clock->set_primary_pll = cle266_set_primary_pll;
  261. clock->set_secondary_clock_state = dummy_set_clock_state;
  262. clock->set_secondary_clock_source = dummy_set_clock_source;
  263. clock->set_secondary_pll_state = dummy_set_pll_state;
  264. clock->set_secondary_pll = cle266_set_secondary_pll;
  265. clock->set_engine_pll_state = dummy_set_pll_state;
  266. clock->set_engine_pll = dummy_set_pll;
  267. break;
  268. case UNICHROME_K800:
  269. case UNICHROME_PM800:
  270. case UNICHROME_CN700:
  271. case UNICHROME_CX700:
  272. case UNICHROME_CN750:
  273. case UNICHROME_K8M890:
  274. case UNICHROME_P4M890:
  275. case UNICHROME_P4M900:
  276. case UNICHROME_VX800:
  277. clock->set_primary_clock_state = set_primary_clock_state;
  278. clock->set_primary_clock_source = set_primary_clock_source;
  279. clock->set_primary_pll_state = set_primary_pll_state;
  280. clock->set_primary_pll = k800_set_primary_pll;
  281. clock->set_secondary_clock_state = set_secondary_clock_state;
  282. clock->set_secondary_clock_source = set_secondary_clock_source;
  283. clock->set_secondary_pll_state = set_secondary_pll_state;
  284. clock->set_secondary_pll = k800_set_secondary_pll;
  285. clock->set_engine_pll_state = set_engine_pll_state;
  286. clock->set_engine_pll = k800_set_engine_pll;
  287. break;
  288. case UNICHROME_VX855:
  289. case UNICHROME_VX900:
  290. clock->set_primary_clock_state = set_primary_clock_state;
  291. clock->set_primary_clock_source = set_primary_clock_source;
  292. clock->set_primary_pll_state = set_primary_pll_state;
  293. clock->set_primary_pll = vx855_set_primary_pll;
  294. clock->set_secondary_clock_state = set_secondary_clock_state;
  295. clock->set_secondary_clock_source = set_secondary_clock_source;
  296. clock->set_secondary_pll_state = set_secondary_pll_state;
  297. clock->set_secondary_pll = vx855_set_secondary_pll;
  298. clock->set_engine_pll_state = set_engine_pll_state;
  299. clock->set_engine_pll = vx855_set_engine_pll;
  300. break;
  301. }
  302. if (machine_is_olpc()) {
  303. /* The OLPC XO-1.5 cannot suspend/resume reliably if the
  304. * IGA1/IGA2 clocks are set as on or off (memory rot
  305. * occasionally happens during suspend under such
  306. * configurations).
  307. *
  308. * The only known stable scenario is to leave this bits as-is,
  309. * which in their default states are documented to enable the
  310. * clock only when it is needed.
  311. */
  312. clock->set_primary_clock_state = noop_set_clock_state;
  313. clock->set_secondary_clock_state = noop_set_clock_state;
  314. }
  315. }