bcu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * bcu.c, Bus Control Unit routines for the NEC VR4100 series.
  3. *
  4. * Copyright (C) 2002 MontaVista Software Inc.
  5. * Author: Yoichi Yuasa <source@mvista.com>
  6. * Copyright (C) 2003-2005 Yoichi Yuasa <yuasa@linux-mips.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. * Changes:
  24. * MontaVista Software Inc. <source@mvista.com>
  25. * - New creation, NEC VR4122 and VR4131 are supported.
  26. * - Added support for NEC VR4111 and VR4121.
  27. *
  28. * Yoichi Yuasa <yuasa@linux-mips.org>
  29. * - Added support for NEC VR4133.
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/smp.h>
  34. #include <linux/types.h>
  35. #include <asm/cpu.h>
  36. #include <asm/io.h>
  37. #define CLKSPEEDREG_TYPE1 (void __iomem *)KSEG1ADDR(0x0b000014)
  38. #define CLKSPEEDREG_TYPE2 (void __iomem *)KSEG1ADDR(0x0f000014)
  39. #define CLKSP(x) ((x) & 0x001f)
  40. #define CLKSP_VR4133(x) ((x) & 0x0007)
  41. #define DIV2B 0x8000
  42. #define DIV3B 0x4000
  43. #define DIV4B 0x2000
  44. #define DIVT(x) (((x) & 0xf000) >> 12)
  45. #define DIVVT(x) (((x) & 0x0f00) >> 8)
  46. #define TDIVMODE(x) (2 << (((x) & 0x1000) >> 12))
  47. #define VTDIVMODE(x) (((x) & 0x0700) >> 8)
  48. static unsigned long vr41xx_vtclock;
  49. static unsigned long vr41xx_tclock;
  50. unsigned long vr41xx_get_vtclock_frequency(void)
  51. {
  52. return vr41xx_vtclock;
  53. }
  54. EXPORT_SYMBOL_GPL(vr41xx_get_vtclock_frequency);
  55. unsigned long vr41xx_get_tclock_frequency(void)
  56. {
  57. return vr41xx_tclock;
  58. }
  59. EXPORT_SYMBOL_GPL(vr41xx_get_tclock_frequency);
  60. static inline uint16_t read_clkspeed(void)
  61. {
  62. switch (current_cpu_type()) {
  63. case CPU_VR4111:
  64. case CPU_VR4121: return readw(CLKSPEEDREG_TYPE1);
  65. case CPU_VR4122:
  66. case CPU_VR4131:
  67. case CPU_VR4133: return readw(CLKSPEEDREG_TYPE2);
  68. default:
  69. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  70. break;
  71. }
  72. return 0;
  73. }
  74. static inline unsigned long calculate_pclock(uint16_t clkspeed)
  75. {
  76. unsigned long pclock = 0;
  77. switch (current_cpu_type()) {
  78. case CPU_VR4111:
  79. case CPU_VR4121:
  80. pclock = 18432000 * 64;
  81. pclock /= CLKSP(clkspeed);
  82. break;
  83. case CPU_VR4122:
  84. pclock = 18432000 * 98;
  85. pclock /= CLKSP(clkspeed);
  86. break;
  87. case CPU_VR4131:
  88. pclock = 18432000 * 108;
  89. pclock /= CLKSP(clkspeed);
  90. break;
  91. case CPU_VR4133:
  92. switch (CLKSP_VR4133(clkspeed)) {
  93. case 0:
  94. pclock = 133000000;
  95. break;
  96. case 1:
  97. pclock = 149000000;
  98. break;
  99. case 2:
  100. pclock = 165900000;
  101. break;
  102. case 3:
  103. pclock = 199100000;
  104. break;
  105. case 4:
  106. pclock = 265900000;
  107. break;
  108. default:
  109. printk(KERN_INFO "Unknown PClock speed for NEC VR4133\n");
  110. break;
  111. }
  112. break;
  113. default:
  114. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  115. break;
  116. }
  117. printk(KERN_INFO "PClock: %ldHz\n", pclock);
  118. return pclock;
  119. }
  120. static inline unsigned long calculate_vtclock(uint16_t clkspeed, unsigned long pclock)
  121. {
  122. unsigned long vtclock = 0;
  123. switch (current_cpu_type()) {
  124. case CPU_VR4111:
  125. /* The NEC VR4111 doesn't have the VTClock. */
  126. break;
  127. case CPU_VR4121:
  128. vtclock = pclock;
  129. /* DIVVT == 9 Divide by 1.5 . VTClock = (PClock * 6) / 9 */
  130. if (DIVVT(clkspeed) == 9)
  131. vtclock = pclock * 6;
  132. /* DIVVT == 10 Divide by 2.5 . VTClock = (PClock * 4) / 10 */
  133. else if (DIVVT(clkspeed) == 10)
  134. vtclock = pclock * 4;
  135. vtclock /= DIVVT(clkspeed);
  136. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  137. break;
  138. case CPU_VR4122:
  139. if(VTDIVMODE(clkspeed) == 7)
  140. vtclock = pclock / 1;
  141. else if(VTDIVMODE(clkspeed) == 1)
  142. vtclock = pclock / 2;
  143. else
  144. vtclock = pclock / VTDIVMODE(clkspeed);
  145. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  146. break;
  147. case CPU_VR4131:
  148. case CPU_VR4133:
  149. vtclock = pclock / VTDIVMODE(clkspeed);
  150. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  151. break;
  152. default:
  153. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  154. break;
  155. }
  156. return vtclock;
  157. }
  158. static inline unsigned long calculate_tclock(uint16_t clkspeed, unsigned long pclock,
  159. unsigned long vtclock)
  160. {
  161. unsigned long tclock = 0;
  162. switch (current_cpu_type()) {
  163. case CPU_VR4111:
  164. if (!(clkspeed & DIV2B))
  165. tclock = pclock / 2;
  166. else if (!(clkspeed & DIV3B))
  167. tclock = pclock / 3;
  168. else if (!(clkspeed & DIV4B))
  169. tclock = pclock / 4;
  170. break;
  171. case CPU_VR4121:
  172. tclock = pclock / DIVT(clkspeed);
  173. break;
  174. case CPU_VR4122:
  175. case CPU_VR4131:
  176. case CPU_VR4133:
  177. tclock = vtclock / TDIVMODE(clkspeed);
  178. break;
  179. default:
  180. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  181. break;
  182. }
  183. printk(KERN_INFO "TClock: %ldHz\n", tclock);
  184. return tclock;
  185. }
  186. void vr41xx_calculate_clock_frequency(void)
  187. {
  188. unsigned long pclock;
  189. uint16_t clkspeed;
  190. clkspeed = read_clkspeed();
  191. pclock = calculate_pclock(clkspeed);
  192. vr41xx_vtclock = calculate_vtclock(clkspeed, pclock);
  193. vr41xx_tclock = calculate_tclock(clkspeed, pclock, vr41xx_vtclock);
  194. }
  195. EXPORT_SYMBOL_GPL(vr41xx_calculate_clock_frequency);