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/export.h>
  32. #include <linux/kernel.h>
  33. #include <linux/smp.h>
  34. #include <linux/types.h>
  35. #include <asm/cpu-type.h>
  36. #include <asm/cpu.h>
  37. #include <asm/io.h>
  38. #define CLKSPEEDREG_TYPE1 (void __iomem *)KSEG1ADDR(0x0b000014)
  39. #define CLKSPEEDREG_TYPE2 (void __iomem *)KSEG1ADDR(0x0f000014)
  40. #define CLKSP(x) ((x) & 0x001f)
  41. #define CLKSP_VR4133(x) ((x) & 0x0007)
  42. #define DIV2B 0x8000
  43. #define DIV3B 0x4000
  44. #define DIV4B 0x2000
  45. #define DIVT(x) (((x) & 0xf000) >> 12)
  46. #define DIVVT(x) (((x) & 0x0f00) >> 8)
  47. #define TDIVMODE(x) (2 << (((x) & 0x1000) >> 12))
  48. #define VTDIVMODE(x) (((x) & 0x0700) >> 8)
  49. static unsigned long vr41xx_vtclock;
  50. static unsigned long vr41xx_tclock;
  51. unsigned long vr41xx_get_vtclock_frequency(void)
  52. {
  53. return vr41xx_vtclock;
  54. }
  55. EXPORT_SYMBOL_GPL(vr41xx_get_vtclock_frequency);
  56. unsigned long vr41xx_get_tclock_frequency(void)
  57. {
  58. return vr41xx_tclock;
  59. }
  60. EXPORT_SYMBOL_GPL(vr41xx_get_tclock_frequency);
  61. static inline uint16_t read_clkspeed(void)
  62. {
  63. switch (current_cpu_type()) {
  64. case CPU_VR4111:
  65. case CPU_VR4121: return readw(CLKSPEEDREG_TYPE1);
  66. case CPU_VR4122:
  67. case CPU_VR4131:
  68. case CPU_VR4133: return readw(CLKSPEEDREG_TYPE2);
  69. default:
  70. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  71. break;
  72. }
  73. return 0;
  74. }
  75. static inline unsigned long calculate_pclock(uint16_t clkspeed)
  76. {
  77. unsigned long pclock = 0;
  78. switch (current_cpu_type()) {
  79. case CPU_VR4111:
  80. case CPU_VR4121:
  81. pclock = 18432000 * 64;
  82. pclock /= CLKSP(clkspeed);
  83. break;
  84. case CPU_VR4122:
  85. pclock = 18432000 * 98;
  86. pclock /= CLKSP(clkspeed);
  87. break;
  88. case CPU_VR4131:
  89. pclock = 18432000 * 108;
  90. pclock /= CLKSP(clkspeed);
  91. break;
  92. case CPU_VR4133:
  93. switch (CLKSP_VR4133(clkspeed)) {
  94. case 0:
  95. pclock = 133000000;
  96. break;
  97. case 1:
  98. pclock = 149000000;
  99. break;
  100. case 2:
  101. pclock = 165900000;
  102. break;
  103. case 3:
  104. pclock = 199100000;
  105. break;
  106. case 4:
  107. pclock = 265900000;
  108. break;
  109. default:
  110. printk(KERN_INFO "Unknown PClock speed for NEC VR4133\n");
  111. break;
  112. }
  113. break;
  114. default:
  115. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  116. break;
  117. }
  118. printk(KERN_INFO "PClock: %ldHz\n", pclock);
  119. return pclock;
  120. }
  121. static inline unsigned long calculate_vtclock(uint16_t clkspeed, unsigned long pclock)
  122. {
  123. unsigned long vtclock = 0;
  124. switch (current_cpu_type()) {
  125. case CPU_VR4111:
  126. /* The NEC VR4111 doesn't have the VTClock. */
  127. break;
  128. case CPU_VR4121:
  129. vtclock = pclock;
  130. /* DIVVT == 9 Divide by 1.5 . VTClock = (PClock * 6) / 9 */
  131. if (DIVVT(clkspeed) == 9)
  132. vtclock = pclock * 6;
  133. /* DIVVT == 10 Divide by 2.5 . VTClock = (PClock * 4) / 10 */
  134. else if (DIVVT(clkspeed) == 10)
  135. vtclock = pclock * 4;
  136. vtclock /= DIVVT(clkspeed);
  137. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  138. break;
  139. case CPU_VR4122:
  140. if(VTDIVMODE(clkspeed) == 7)
  141. vtclock = pclock / 1;
  142. else if(VTDIVMODE(clkspeed) == 1)
  143. vtclock = pclock / 2;
  144. else
  145. vtclock = pclock / VTDIVMODE(clkspeed);
  146. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  147. break;
  148. case CPU_VR4131:
  149. case CPU_VR4133:
  150. vtclock = pclock / VTDIVMODE(clkspeed);
  151. printk(KERN_INFO "VTClock: %ldHz\n", vtclock);
  152. break;
  153. default:
  154. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  155. break;
  156. }
  157. return vtclock;
  158. }
  159. static inline unsigned long calculate_tclock(uint16_t clkspeed, unsigned long pclock,
  160. unsigned long vtclock)
  161. {
  162. unsigned long tclock = 0;
  163. switch (current_cpu_type()) {
  164. case CPU_VR4111:
  165. if (!(clkspeed & DIV2B))
  166. tclock = pclock / 2;
  167. else if (!(clkspeed & DIV3B))
  168. tclock = pclock / 3;
  169. else if (!(clkspeed & DIV4B))
  170. tclock = pclock / 4;
  171. break;
  172. case CPU_VR4121:
  173. tclock = pclock / DIVT(clkspeed);
  174. break;
  175. case CPU_VR4122:
  176. case CPU_VR4131:
  177. case CPU_VR4133:
  178. tclock = vtclock / TDIVMODE(clkspeed);
  179. break;
  180. default:
  181. printk(KERN_INFO "Unexpected CPU of NEC VR4100 series\n");
  182. break;
  183. }
  184. printk(KERN_INFO "TClock: %ldHz\n", tclock);
  185. return tclock;
  186. }
  187. void vr41xx_calculate_clock_frequency(void)
  188. {
  189. unsigned long pclock;
  190. uint16_t clkspeed;
  191. clkspeed = read_clkspeed();
  192. pclock = calculate_pclock(clkspeed);
  193. vr41xx_vtclock = calculate_vtclock(clkspeed, pclock);
  194. vr41xx_tclock = calculate_tclock(clkspeed, pclock, vr41xx_vtclock);
  195. }
  196. EXPORT_SYMBOL_GPL(vr41xx_calculate_clock_frequency);