via_clock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "via_clock.h"
  28. #include "global.h"
  29. #include "debug.h"
  30. const char *via_slap = "Please slap VIA Technologies to motivate them "
  31. "releasing full documentation for your platform!\n";
  32. static inline u32 cle266_encode_pll(struct via_pll_config pll)
  33. {
  34. return (pll.multiplier << 8)
  35. | (pll.rshift << 6)
  36. | pll.divisor;
  37. }
  38. static inline u32 k800_encode_pll(struct via_pll_config pll)
  39. {
  40. return ((pll.divisor - 2) << 16)
  41. | (pll.rshift << 10)
  42. | (pll.multiplier - 2);
  43. }
  44. static inline u32 vx855_encode_pll(struct via_pll_config pll)
  45. {
  46. return (pll.divisor << 16)
  47. | (pll.rshift << 10)
  48. | pll.multiplier;
  49. }
  50. static inline void cle266_set_primary_pll_encoded(u32 data)
  51. {
  52. via_write_reg_mask(VIASR, 0x40, 0x02, 0x02); /* enable reset */
  53. via_write_reg(VIASR, 0x46, data & 0xFF);
  54. via_write_reg(VIASR, 0x47, (data >> 8) & 0xFF);
  55. via_write_reg_mask(VIASR, 0x40, 0x00, 0x02); /* disable reset */
  56. }
  57. static inline void k800_set_primary_pll_encoded(u32 data)
  58. {
  59. via_write_reg_mask(VIASR, 0x40, 0x02, 0x02); /* enable reset */
  60. via_write_reg(VIASR, 0x44, data & 0xFF);
  61. via_write_reg(VIASR, 0x45, (data >> 8) & 0xFF);
  62. via_write_reg(VIASR, 0x46, (data >> 16) & 0xFF);
  63. via_write_reg_mask(VIASR, 0x40, 0x00, 0x02); /* disable reset */
  64. }
  65. static inline void cle266_set_secondary_pll_encoded(u32 data)
  66. {
  67. via_write_reg_mask(VIASR, 0x40, 0x04, 0x04); /* enable reset */
  68. via_write_reg(VIASR, 0x44, data & 0xFF);
  69. via_write_reg(VIASR, 0x45, (data >> 8) & 0xFF);
  70. via_write_reg_mask(VIASR, 0x40, 0x00, 0x04); /* disable reset */
  71. }
  72. static inline void k800_set_secondary_pll_encoded(u32 data)
  73. {
  74. via_write_reg_mask(VIASR, 0x40, 0x04, 0x04); /* enable reset */
  75. via_write_reg(VIASR, 0x4A, data & 0xFF);
  76. via_write_reg(VIASR, 0x4B, (data >> 8) & 0xFF);
  77. via_write_reg(VIASR, 0x4C, (data >> 16) & 0xFF);
  78. via_write_reg_mask(VIASR, 0x40, 0x00, 0x04); /* disable reset */
  79. }
  80. static inline void set_engine_pll_encoded(u32 data)
  81. {
  82. via_write_reg_mask(VIASR, 0x40, 0x01, 0x01); /* enable reset */
  83. via_write_reg(VIASR, 0x47, data & 0xFF);
  84. via_write_reg(VIASR, 0x48, (data >> 8) & 0xFF);
  85. via_write_reg(VIASR, 0x49, (data >> 16) & 0xFF);
  86. via_write_reg_mask(VIASR, 0x40, 0x00, 0x01); /* disable reset */
  87. }
  88. static void cle266_set_primary_pll(struct via_pll_config config)
  89. {
  90. cle266_set_primary_pll_encoded(cle266_encode_pll(config));
  91. }
  92. static void k800_set_primary_pll(struct via_pll_config config)
  93. {
  94. k800_set_primary_pll_encoded(k800_encode_pll(config));
  95. }
  96. static void vx855_set_primary_pll(struct via_pll_config config)
  97. {
  98. k800_set_primary_pll_encoded(vx855_encode_pll(config));
  99. }
  100. static void cle266_set_secondary_pll(struct via_pll_config config)
  101. {
  102. cle266_set_secondary_pll_encoded(cle266_encode_pll(config));
  103. }
  104. static void k800_set_secondary_pll(struct via_pll_config config)
  105. {
  106. k800_set_secondary_pll_encoded(k800_encode_pll(config));
  107. }
  108. static void vx855_set_secondary_pll(struct via_pll_config config)
  109. {
  110. k800_set_secondary_pll_encoded(vx855_encode_pll(config));
  111. }
  112. static void k800_set_engine_pll(struct via_pll_config config)
  113. {
  114. set_engine_pll_encoded(k800_encode_pll(config));
  115. }
  116. static void vx855_set_engine_pll(struct via_pll_config config)
  117. {
  118. set_engine_pll_encoded(vx855_encode_pll(config));
  119. }
  120. static void set_primary_pll_state(u8 state)
  121. {
  122. u8 value;
  123. switch (state) {
  124. case VIA_STATE_ON:
  125. value = 0x20;
  126. break;
  127. case VIA_STATE_OFF:
  128. value = 0x00;
  129. break;
  130. default:
  131. return;
  132. }
  133. via_write_reg_mask(VIASR, 0x2D, value, 0x30);
  134. }
  135. static void set_secondary_pll_state(u8 state)
  136. {
  137. u8 value;
  138. switch (state) {
  139. case VIA_STATE_ON:
  140. value = 0x08;
  141. break;
  142. case VIA_STATE_OFF:
  143. value = 0x00;
  144. break;
  145. default:
  146. return;
  147. }
  148. via_write_reg_mask(VIASR, 0x2D, value, 0x0C);
  149. }
  150. static void set_engine_pll_state(u8 state)
  151. {
  152. u8 value;
  153. switch (state) {
  154. case VIA_STATE_ON:
  155. value = 0x02;
  156. break;
  157. case VIA_STATE_OFF:
  158. value = 0x00;
  159. break;
  160. default:
  161. return;
  162. }
  163. via_write_reg_mask(VIASR, 0x2D, value, 0x03);
  164. }
  165. static void set_primary_clock_state(u8 state)
  166. {
  167. u8 value;
  168. switch (state) {
  169. case VIA_STATE_ON:
  170. value = 0x20;
  171. break;
  172. case VIA_STATE_OFF:
  173. value = 0x00;
  174. break;
  175. default:
  176. return;
  177. }
  178. via_write_reg_mask(VIASR, 0x1B, value, 0x30);
  179. }
  180. static void set_secondary_clock_state(u8 state)
  181. {
  182. u8 value;
  183. switch (state) {
  184. case VIA_STATE_ON:
  185. value = 0x80;
  186. break;
  187. case VIA_STATE_OFF:
  188. value = 0x00;
  189. break;
  190. default:
  191. return;
  192. }
  193. via_write_reg_mask(VIASR, 0x1B, value, 0xC0);
  194. }
  195. static inline u8 set_clock_source_common(enum via_clksrc source, bool use_pll)
  196. {
  197. u8 data = 0;
  198. switch (source) {
  199. case VIA_CLKSRC_X1:
  200. data = 0x00;
  201. break;
  202. case VIA_CLKSRC_TVX1:
  203. data = 0x02;
  204. break;
  205. case VIA_CLKSRC_TVPLL:
  206. data = 0x04; /* 0x06 should be the same */
  207. break;
  208. case VIA_CLKSRC_DVP1TVCLKR:
  209. data = 0x0A;
  210. break;
  211. case VIA_CLKSRC_CAP0:
  212. data = 0xC;
  213. break;
  214. case VIA_CLKSRC_CAP1:
  215. data = 0x0E;
  216. break;
  217. }
  218. if (!use_pll)
  219. data |= 1;
  220. return data;
  221. }
  222. static void set_primary_clock_source(enum via_clksrc source, bool use_pll)
  223. {
  224. u8 data = set_clock_source_common(source, use_pll) << 4;
  225. via_write_reg_mask(VIACR, 0x6C, data, 0xF0);
  226. }
  227. static void set_secondary_clock_source(enum via_clksrc source, bool use_pll)
  228. {
  229. u8 data = set_clock_source_common(source, use_pll);
  230. via_write_reg_mask(VIACR, 0x6C, data, 0x0F);
  231. }
  232. static void dummy_set_clock_state(u8 state)
  233. {
  234. printk(KERN_INFO "Using undocumented set clock state.\n%s", via_slap);
  235. }
  236. static void dummy_set_clock_source(enum via_clksrc source, bool use_pll)
  237. {
  238. printk(KERN_INFO "Using undocumented set clock source.\n%s", via_slap);
  239. }
  240. static void dummy_set_pll_state(u8 state)
  241. {
  242. printk(KERN_INFO "Using undocumented set PLL state.\n%s", via_slap);
  243. }
  244. static void dummy_set_pll(struct via_pll_config config)
  245. {
  246. printk(KERN_INFO "Using undocumented set PLL.\n%s", via_slap);
  247. }
  248. void via_clock_init(struct via_clock *clock, int gfx_chip)
  249. {
  250. switch (gfx_chip) {
  251. case UNICHROME_CLE266:
  252. case UNICHROME_K400:
  253. clock->set_primary_clock_state = dummy_set_clock_state;
  254. clock->set_primary_clock_source = dummy_set_clock_source;
  255. clock->set_primary_pll_state = dummy_set_pll_state;
  256. clock->set_primary_pll = cle266_set_primary_pll;
  257. clock->set_secondary_clock_state = dummy_set_clock_state;
  258. clock->set_secondary_clock_source = dummy_set_clock_source;
  259. clock->set_secondary_pll_state = dummy_set_pll_state;
  260. clock->set_secondary_pll = cle266_set_secondary_pll;
  261. clock->set_engine_pll_state = dummy_set_pll_state;
  262. clock->set_engine_pll = dummy_set_pll;
  263. break;
  264. case UNICHROME_K800:
  265. case UNICHROME_PM800:
  266. case UNICHROME_CN700:
  267. case UNICHROME_CX700:
  268. case UNICHROME_CN750:
  269. case UNICHROME_K8M890:
  270. case UNICHROME_P4M890:
  271. case UNICHROME_P4M900:
  272. case UNICHROME_VX800:
  273. clock->set_primary_clock_state = set_primary_clock_state;
  274. clock->set_primary_clock_source = set_primary_clock_source;
  275. clock->set_primary_pll_state = set_primary_pll_state;
  276. clock->set_primary_pll = k800_set_primary_pll;
  277. clock->set_secondary_clock_state = set_secondary_clock_state;
  278. clock->set_secondary_clock_source = set_secondary_clock_source;
  279. clock->set_secondary_pll_state = set_secondary_pll_state;
  280. clock->set_secondary_pll = k800_set_secondary_pll;
  281. clock->set_engine_pll_state = set_engine_pll_state;
  282. clock->set_engine_pll = k800_set_engine_pll;
  283. break;
  284. case UNICHROME_VX855:
  285. case UNICHROME_VX900:
  286. clock->set_primary_clock_state = set_primary_clock_state;
  287. clock->set_primary_clock_source = set_primary_clock_source;
  288. clock->set_primary_pll_state = set_primary_pll_state;
  289. clock->set_primary_pll = vx855_set_primary_pll;
  290. clock->set_secondary_clock_state = set_secondary_clock_state;
  291. clock->set_secondary_clock_source = set_secondary_clock_source;
  292. clock->set_secondary_pll_state = set_secondary_pll_state;
  293. clock->set_secondary_pll = vx855_set_secondary_pll;
  294. clock->set_engine_pll_state = set_engine_pll_state;
  295. clock->set_engine_pll = vx855_set_engine_pll;
  296. break;
  297. }
  298. }