STG4000InitDevice.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * linux/drivers/video/kyro/STG4000InitDevice.c
  3. *
  4. * Copyright (C) 2000 Imagination Technologies Ltd
  5. * Copyright (C) 2002 STMicroelectronics
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/pci.h>
  15. #include "STG4000Reg.h"
  16. #include "STG4000Interface.h"
  17. /* SDRAM fixed settings */
  18. #define SDRAM_CFG_0 0x49A1
  19. #define SDRAM_CFG_1 0xA732
  20. #define SDRAM_CFG_2 0x31
  21. #define SDRAM_ARB_CFG 0xA0
  22. #define SDRAM_REFRESH 0x20
  23. /* Reset values */
  24. #define PMX2_SOFTRESET_DAC_RST 0x0001
  25. #define PMX2_SOFTRESET_C1_RST 0x0004
  26. #define PMX2_SOFTRESET_C2_RST 0x0008
  27. #define PMX2_SOFTRESET_3D_RST 0x0010
  28. #define PMX2_SOFTRESET_VIDIN_RST 0x0020
  29. #define PMX2_SOFTRESET_TLB_RST 0x0040
  30. #define PMX2_SOFTRESET_SD_RST 0x0080
  31. #define PMX2_SOFTRESET_VGA_RST 0x0100
  32. #define PMX2_SOFTRESET_ROM_RST 0x0200 /* reserved bit, do not reset */
  33. #define PMX2_SOFTRESET_TA_RST 0x0400
  34. #define PMX2_SOFTRESET_REG_RST 0x4000
  35. #define PMX2_SOFTRESET_ALL 0x7fff
  36. /* Core clock freq */
  37. #define CORE_PLL_FREQ 1000000
  38. /* Reference Clock freq */
  39. #define REF_FREQ 14318
  40. /* PCI Registers */
  41. static u16 CorePllControl = 0x70;
  42. #define PCI_CONFIG_SUBSYS_ID 0x2e
  43. /* Misc */
  44. #define CORE_PLL_MODE_REG_0_7 3
  45. #define CORE_PLL_MODE_REG_8_15 2
  46. #define CORE_PLL_MODE_CONFIG_REG 1
  47. #define DAC_PLL_CONFIG_REG 0
  48. #define STG_MAX_VCO 500000
  49. #define STG_MIN_VCO 100000
  50. /* PLL Clock */
  51. #define STG4K3_PLL_SCALER 8 /* scale numbers by 2^8 for fixed point calc */
  52. #define STG4K3_PLL_MIN_R 2 /* Minimum multiplier */
  53. #define STG4K3_PLL_MAX_R 33 /* Max */
  54. #define STG4K3_PLL_MIN_F 2 /* Minimum divisor */
  55. #define STG4K3_PLL_MAX_F 513 /* Max */
  56. #define STG4K3_PLL_MIN_OD 0 /* Min output divider (shift) */
  57. #define STG4K3_PLL_MAX_OD 2 /* Max */
  58. #define STG4K3_PLL_MIN_VCO_SC (100000000 >> STG4K3_PLL_SCALER) /* Min VCO rate */
  59. #define STG4K3_PLL_MAX_VCO_SC (500000000 >> STG4K3_PLL_SCALER) /* Max VCO rate */
  60. #define STG4K3_PLL_MINR_VCO_SC (100000000 >> STG4K3_PLL_SCALER) /* Min VCO rate (restricted) */
  61. #define STG4K3_PLL_MAXR_VCO_SC (500000000 >> STG4K3_PLL_SCALER) /* Max VCO rate (restricted) */
  62. #define STG4K3_PLL_MINR_VCO 100000000 /* Min VCO rate (restricted) */
  63. #define STG4K3_PLL_MAX_VCO 500000000 /* Max VCO rate */
  64. #define STG4K3_PLL_MAXR_VCO 500000000 /* Max VCO rate (restricted) */
  65. #define OS_DELAY(X) \
  66. { \
  67. volatile u32 i,count=0; \
  68. for(i=0;i<X;i++) count++; \
  69. }
  70. static u32 InitSDRAMRegisters(volatile STG4000REG __iomem *pSTGReg,
  71. u32 dwSubSysID, u32 dwRevID)
  72. {
  73. u32 adwSDRAMArgCfg0[] = { 0xa0, 0x80, 0xa0, 0xa0, 0xa0 };
  74. u32 adwSDRAMCfg1[] = { 0x8732, 0x8732, 0xa732, 0xa732, 0x8732 };
  75. u32 adwSDRAMCfg2[] = { 0x87d2, 0x87d2, 0xa7d2, 0x87d2, 0xa7d2 };
  76. u32 adwSDRAMRsh[] = { 36, 39, 40 };
  77. u32 adwChipSpeed[] = { 110, 120, 125 };
  78. u32 dwMemTypeIdx;
  79. u32 dwChipSpeedIdx;
  80. /* Get memory tpye and chip speed indexs from the SubSysDevID */
  81. dwMemTypeIdx = (dwSubSysID & 0x70) >> 4;
  82. dwChipSpeedIdx = (dwSubSysID & 0x180) >> 7;
  83. if (dwMemTypeIdx > 4 || dwChipSpeedIdx > 2)
  84. return 0;
  85. /* Program SD-RAM interface */
  86. STG_WRITE_REG(SDRAMArbiterConf, adwSDRAMArgCfg0[dwMemTypeIdx]);
  87. if (dwRevID < 5) {
  88. STG_WRITE_REG(SDRAMConf0, 0x49A1);
  89. STG_WRITE_REG(SDRAMConf1, adwSDRAMCfg1[dwMemTypeIdx]);
  90. } else {
  91. STG_WRITE_REG(SDRAMConf0, 0x4DF1);
  92. STG_WRITE_REG(SDRAMConf1, adwSDRAMCfg2[dwMemTypeIdx]);
  93. }
  94. STG_WRITE_REG(SDRAMConf2, 0x31);
  95. STG_WRITE_REG(SDRAMRefresh, adwSDRAMRsh[dwChipSpeedIdx]);
  96. return adwChipSpeed[dwChipSpeedIdx] * 10000;
  97. }
  98. u32 ProgramClock(u32 refClock,
  99. u32 coreClock,
  100. u32 * FOut, u32 * ROut, u32 * POut)
  101. {
  102. u32 R = 0, F = 0, OD = 0, ODIndex = 0;
  103. u32 ulBestR = 0, ulBestF = 0, ulBestOD = 0;
  104. u32 ulBestVCO = 0, ulBestClk = 0, ulBestScore = 0;
  105. u32 ulScore, ulPhaseScore, ulVcoScore;
  106. u32 ulTmp = 0, ulVCO;
  107. u32 ulScaleClockReq, ulMinClock, ulMaxClock;
  108. u32 ODValues[] = { 1, 2, 0 };
  109. /* Translate clock in Hz */
  110. coreClock *= 100; /* in Hz */
  111. refClock *= 1000; /* in Hz */
  112. /* Work out acceptable clock
  113. * The method calculates ~ +- 0.4% (1/256)
  114. */
  115. ulMinClock = coreClock - (coreClock >> 8);
  116. ulMaxClock = coreClock + (coreClock >> 8);
  117. /* Scale clock required for use in calculations */
  118. ulScaleClockReq = coreClock >> STG4K3_PLL_SCALER;
  119. /* Iterate through post divider values */
  120. for (ODIndex = 0; ODIndex < 3; ODIndex++) {
  121. OD = ODValues[ODIndex];
  122. R = STG4K3_PLL_MIN_R;
  123. /* loop for pre-divider from min to max */
  124. while (R <= STG4K3_PLL_MAX_R) {
  125. /* estimate required feedback multiplier */
  126. ulTmp = R * (ulScaleClockReq << OD);
  127. /* F = ClkRequired * R * (2^OD) / Fref */
  128. F = (u32)(ulTmp / (refClock >> STG4K3_PLL_SCALER));
  129. /* compensate for accuracy */
  130. if (F > STG4K3_PLL_MIN_F)
  131. F--;
  132. /*
  133. * We should be close to our target frequency (if it's
  134. * achievable with current OD & R) let's iterate
  135. * through F for best fit
  136. */
  137. while ((F >= STG4K3_PLL_MIN_F) &&
  138. (F <= STG4K3_PLL_MAX_F)) {
  139. /* Calc VCO at full accuracy */
  140. ulVCO = refClock / R;
  141. ulVCO = F * ulVCO;
  142. /*
  143. * Check it's within restricted VCO range
  144. * unless of course the desired frequency is
  145. * above the restricted range, then test
  146. * against VCO limit
  147. */
  148. if ((ulVCO >= STG4K3_PLL_MINR_VCO) &&
  149. ((ulVCO <= STG4K3_PLL_MAXR_VCO) ||
  150. ((coreClock > STG4K3_PLL_MAXR_VCO)
  151. && (ulVCO <= STG4K3_PLL_MAX_VCO)))) {
  152. ulTmp = (ulVCO >> OD); /* Clock = VCO / (2^OD) */
  153. /* Is this clock good enough? */
  154. if ((ulTmp >= ulMinClock)
  155. && (ulTmp <= ulMaxClock)) {
  156. ulPhaseScore = (((refClock / R) - (refClock / STG4K3_PLL_MAX_R))) / ((refClock - (refClock / STG4K3_PLL_MAX_R)) >> 10);
  157. ulVcoScore = ((ulVCO - STG4K3_PLL_MINR_VCO)) / ((STG4K3_PLL_MAXR_VCO - STG4K3_PLL_MINR_VCO) >> 10);
  158. ulScore = ulPhaseScore + ulVcoScore;
  159. if (!ulBestScore) {
  160. ulBestVCO = ulVCO;
  161. ulBestOD = OD;
  162. ulBestF = F;
  163. ulBestR = R;
  164. ulBestClk = ulTmp;
  165. ulBestScore =
  166. ulScore;
  167. }
  168. /* is this better, ( aim for highest Score) */
  169. /*--------------------------------------------------------------------------
  170. Here we want to use a scoring system which will take account of both the
  171. value at the phase comparater and the VCO output
  172. to do this we will use a cumulative score between the two
  173. The way this ends up is that we choose the first value in the loop anyway
  174. but we shall keep this code in case new restrictions come into play
  175. --------------------------------------------------------------------------*/
  176. if ((ulScore >= ulBestScore) && (OD > 0)) {
  177. ulBestVCO = ulVCO;
  178. ulBestOD = OD;
  179. ulBestF = F;
  180. ulBestR = R;
  181. ulBestClk = ulTmp;
  182. ulBestScore =
  183. ulScore;
  184. }
  185. }
  186. }
  187. F++;
  188. }
  189. R++;
  190. }
  191. }
  192. /*
  193. did we find anything?
  194. Then return RFOD
  195. */
  196. if (ulBestScore) {
  197. *ROut = ulBestR;
  198. *FOut = ulBestF;
  199. if ((ulBestOD == 2) || (ulBestOD == 3)) {
  200. *POut = 3;
  201. } else
  202. *POut = ulBestOD;
  203. }
  204. return (ulBestClk);
  205. }
  206. int SetCoreClockPLL(volatile STG4000REG __iomem *pSTGReg, struct pci_dev *pDev)
  207. {
  208. u32 F, R, P;
  209. u16 core_pll = 0, sub;
  210. u32 ulCoreClock;
  211. u32 tmp;
  212. u32 ulChipSpeed;
  213. STG_WRITE_REG(IntMask, 0xFFFF);
  214. /* Disable Primary Core Thread0 */
  215. tmp = STG_READ_REG(Thread0Enable);
  216. CLEAR_BIT(0);
  217. STG_WRITE_REG(Thread0Enable, tmp);
  218. /* Disable Primary Core Thread1 */
  219. tmp = STG_READ_REG(Thread1Enable);
  220. CLEAR_BIT(0);
  221. STG_WRITE_REG(Thread1Enable, tmp);
  222. STG_WRITE_REG(SoftwareReset,
  223. PMX2_SOFTRESET_REG_RST | PMX2_SOFTRESET_ROM_RST);
  224. STG_WRITE_REG(SoftwareReset,
  225. PMX2_SOFTRESET_REG_RST | PMX2_SOFTRESET_TA_RST |
  226. PMX2_SOFTRESET_ROM_RST);
  227. /* Need to play around to reset TA */
  228. STG_WRITE_REG(TAConfiguration, 0);
  229. STG_WRITE_REG(SoftwareReset,
  230. PMX2_SOFTRESET_REG_RST | PMX2_SOFTRESET_ROM_RST);
  231. STG_WRITE_REG(SoftwareReset,
  232. PMX2_SOFTRESET_REG_RST | PMX2_SOFTRESET_TA_RST |
  233. PMX2_SOFTRESET_ROM_RST);
  234. pci_read_config_word(pDev, PCI_CONFIG_SUBSYS_ID, &sub);
  235. ulChipSpeed = InitSDRAMRegisters(pSTGReg, (u32)sub,
  236. (u32)pDev->revision);
  237. if (ulChipSpeed == 0)
  238. return -EINVAL;
  239. ulCoreClock = ProgramClock(REF_FREQ, CORE_PLL_FREQ, &F, &R, &P);
  240. core_pll |= ((P) | ((F - 2) << 2) | ((R - 2) << 11));
  241. /* Set Core PLL Control to Core PLL Mode */
  242. /* Send bits 0:7 of the Core PLL Mode register */
  243. tmp = ((CORE_PLL_MODE_REG_0_7 << 8) | (core_pll & 0x00FF));
  244. pci_write_config_word(pDev, CorePllControl, tmp);
  245. /* Without some delay between the PCI config writes the clock does
  246. not reliably set when the code is compiled -O3
  247. */
  248. OS_DELAY(1000000);
  249. tmp |= SET_BIT(14);
  250. pci_write_config_word(pDev, CorePllControl, tmp);
  251. OS_DELAY(1000000);
  252. /* Send bits 8:15 of the Core PLL Mode register */
  253. tmp =
  254. ((CORE_PLL_MODE_REG_8_15 << 8) | ((core_pll & 0xFF00) >> 8));
  255. pci_write_config_word(pDev, CorePllControl, tmp);
  256. OS_DELAY(1000000);
  257. tmp |= SET_BIT(14);
  258. pci_write_config_word(pDev, CorePllControl, tmp);
  259. OS_DELAY(1000000);
  260. STG_WRITE_REG(SoftwareReset, PMX2_SOFTRESET_ALL);
  261. #if 0
  262. /* Enable Primary Core Thread0 */
  263. tmp = ((STG_READ_REG(Thread0Enable)) | SET_BIT(0));
  264. STG_WRITE_REG(Thread0Enable, tmp);
  265. /* Enable Primary Core Thread1 */
  266. tmp = ((STG_READ_REG(Thread1Enable)) | SET_BIT(0));
  267. STG_WRITE_REG(Thread1Enable, tmp);
  268. #endif
  269. return 0;
  270. }