jz4740.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Jz4740 common routines
  3. * Copyright (c) 2006 Ingenic Semiconductor, <jlwei@ingenic.cn>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <config.h>
  21. #include <common.h>
  22. #include <asm/io.h>
  23. #include <asm/jz4740.h>
  24. void enable_interrupts(void)
  25. {
  26. }
  27. int disable_interrupts(void)
  28. {
  29. return 0;
  30. }
  31. /*
  32. * PLL output clock = EXTAL * NF / (NR * NO)
  33. * NF = FD + 2, NR = RD + 2
  34. * NO = 1 (if OD = 0), NO = 2 (if OD = 1 or 2), NO = 4 (if OD = 3)
  35. */
  36. void pll_init(void)
  37. {
  38. struct jz4740_cpm *cpm = (struct jz4740_cpm *)JZ4740_CPM_BASE;
  39. register unsigned int cfcr, plcr1;
  40. int n2FR[33] = {
  41. 0, 0, 1, 2, 3, 0, 4, 0, 5, 0, 0, 0, 6, 0, 0, 0,
  42. 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0,
  43. 9
  44. };
  45. int div[5] = {1, 3, 3, 3, 3}; /* divisors of I:S:P:L:M */
  46. int nf, pllout2;
  47. cfcr = CPM_CPCCR_CLKOEN |
  48. CPM_CPCCR_PCS |
  49. (n2FR[div[0]] << CPM_CPCCR_CDIV_BIT) |
  50. (n2FR[div[1]] << CPM_CPCCR_HDIV_BIT) |
  51. (n2FR[div[2]] << CPM_CPCCR_PDIV_BIT) |
  52. (n2FR[div[3]] << CPM_CPCCR_MDIV_BIT) |
  53. (n2FR[div[4]] << CPM_CPCCR_LDIV_BIT);
  54. pllout2 = (cfcr & CPM_CPCCR_PCS) ?
  55. CONFIG_SYS_CPU_SPEED : (CONFIG_SYS_CPU_SPEED / 2);
  56. /* Init USB Host clock, pllout2 must be n*48MHz */
  57. writel(pllout2 / 48000000 - 1, &cpm->uhccdr);
  58. nf = CONFIG_SYS_CPU_SPEED * 2 / CONFIG_SYS_EXTAL;
  59. plcr1 = ((nf - 2) << CPM_CPPCR_PLLM_BIT) | /* FD */
  60. (0 << CPM_CPPCR_PLLN_BIT) | /* RD=0, NR=2 */
  61. (0 << CPM_CPPCR_PLLOD_BIT) | /* OD=0, NO=1 */
  62. (0x20 << CPM_CPPCR_PLLST_BIT) | /* PLL stable time */
  63. CPM_CPPCR_PLLEN; /* enable PLL */
  64. /* init PLL */
  65. writel(cfcr, &cpm->cpccr);
  66. writel(plcr1, &cpm->cppcr);
  67. }
  68. void sdram_init(void)
  69. {
  70. struct jz4740_emc *emc = (struct jz4740_emc *)JZ4740_EMC_BASE;
  71. register unsigned int dmcr0, dmcr, sdmode, tmp, cpu_clk, mem_clk, ns;
  72. unsigned int cas_latency_sdmr[2] = {
  73. EMC_SDMR_CAS_2,
  74. EMC_SDMR_CAS_3,
  75. };
  76. unsigned int cas_latency_dmcr[2] = {
  77. 1 << EMC_DMCR_TCL_BIT, /* CAS latency is 2 */
  78. 2 << EMC_DMCR_TCL_BIT /* CAS latency is 3 */
  79. };
  80. int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
  81. cpu_clk = CONFIG_SYS_CPU_SPEED;
  82. mem_clk = cpu_clk * div[__cpm_get_cdiv()] / div[__cpm_get_mdiv()];
  83. writel(0, &emc->bcr); /* Disable bus release */
  84. writew(0, &emc->rtcsr); /* Disable clock for counting */
  85. /* Fault DMCR value for mode register setting*/
  86. #define SDRAM_ROW0 11
  87. #define SDRAM_COL0 8
  88. #define SDRAM_BANK40 0
  89. dmcr0 = ((SDRAM_ROW0 - 11) << EMC_DMCR_RA_BIT) |
  90. ((SDRAM_COL0 - 8) << EMC_DMCR_CA_BIT) |
  91. (SDRAM_BANK40 << EMC_DMCR_BA_BIT) |
  92. (SDRAM_BW16 << EMC_DMCR_BW_BIT) |
  93. EMC_DMCR_EPIN |
  94. cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
  95. /* Basic DMCR value */
  96. dmcr = ((SDRAM_ROW - 11) << EMC_DMCR_RA_BIT) |
  97. ((SDRAM_COL - 8) << EMC_DMCR_CA_BIT) |
  98. (SDRAM_BANK4 << EMC_DMCR_BA_BIT) |
  99. (SDRAM_BW16 << EMC_DMCR_BW_BIT) |
  100. EMC_DMCR_EPIN |
  101. cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
  102. /* SDRAM timimg */
  103. ns = 1000000000 / mem_clk;
  104. tmp = SDRAM_TRAS / ns;
  105. if (tmp < 4)
  106. tmp = 4;
  107. if (tmp > 11)
  108. tmp = 11;
  109. dmcr |= (tmp - 4) << EMC_DMCR_TRAS_BIT;
  110. tmp = SDRAM_RCD / ns;
  111. if (tmp > 3)
  112. tmp = 3;
  113. dmcr |= tmp << EMC_DMCR_RCD_BIT;
  114. tmp = SDRAM_TPC / ns;
  115. if (tmp > 7)
  116. tmp = 7;
  117. dmcr |= tmp << EMC_DMCR_TPC_BIT;
  118. tmp = SDRAM_TRWL / ns;
  119. if (tmp > 3)
  120. tmp = 3;
  121. dmcr |= tmp << EMC_DMCR_TRWL_BIT;
  122. tmp = (SDRAM_TRAS + SDRAM_TPC) / ns;
  123. if (tmp > 14)
  124. tmp = 14;
  125. dmcr |= ((tmp + 1) >> 1) << EMC_DMCR_TRC_BIT;
  126. /* SDRAM mode value */
  127. sdmode = EMC_SDMR_BT_SEQ |
  128. EMC_SDMR_OM_NORMAL |
  129. EMC_SDMR_BL_4 |
  130. cas_latency_sdmr[((SDRAM_CASL == 3) ? 1 : 0)];
  131. /* Stage 1. Precharge all banks by writing SDMR with DMCR.MRSET=0 */
  132. writel(dmcr, &emc->dmcr);
  133. writeb(0, JZ4740_EMC_SDMR0 | sdmode);
  134. /* Wait for precharge, > 200us */
  135. tmp = (cpu_clk / 1000000) * 1000;
  136. while (tmp--)
  137. ;
  138. /* Stage 2. Enable auto-refresh */
  139. writel(dmcr | EMC_DMCR_RFSH, &emc->dmcr);
  140. tmp = SDRAM_TREF / ns;
  141. tmp = tmp / 64 + 1;
  142. if (tmp > 0xff)
  143. tmp = 0xff;
  144. writew(tmp, &emc->rtcor);
  145. writew(0, &emc->rtcnt);
  146. /* Divisor is 64, CKO/64 */
  147. writew(EMC_RTCSR_CKS_64, &emc->rtcsr);
  148. /* Wait for number of auto-refresh cycles */
  149. tmp = (cpu_clk / 1000000) * 1000;
  150. while (tmp--)
  151. ;
  152. /* Stage 3. Mode Register Set */
  153. writel(dmcr0 | EMC_DMCR_RFSH | EMC_DMCR_MRSET, &emc->dmcr);
  154. writeb(0, JZ4740_EMC_SDMR0 | sdmode);
  155. /* Set back to basic DMCR value */
  156. writel(dmcr | EMC_DMCR_RFSH | EMC_DMCR_MRSET, &emc->dmcr);
  157. /* everything is ok now */
  158. }
  159. DECLARE_GLOBAL_DATA_PTR;
  160. void calc_clocks(void)
  161. {
  162. unsigned int pllout;
  163. unsigned int div[10] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
  164. pllout = __cpm_get_pllout();
  165. gd->cpu_clk = pllout / div[__cpm_get_cdiv()];
  166. gd->sys_clk = pllout / div[__cpm_get_hdiv()];
  167. gd->per_clk = pllout / div[__cpm_get_pdiv()];
  168. gd->mem_clk = pllout / div[__cpm_get_mdiv()];
  169. gd->dev_clk = CONFIG_SYS_EXTAL;
  170. }
  171. void rtc_init(void)
  172. {
  173. struct jz4740_rtc *rtc = (struct jz4740_rtc *)JZ4740_RTC_BASE;
  174. while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
  175. ;
  176. writel(readl(&rtc->rcr) | RTC_RCR_AE, &rtc->rcr); /* enable alarm */
  177. while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
  178. ;
  179. writel(0x00007fff, &rtc->rgr); /* type value */
  180. while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
  181. ;
  182. writel(0x0000ffe0, &rtc->hwfcr); /* Power on delay 2s */
  183. while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
  184. ;
  185. writel(0x00000fe0, &rtc->hrcr); /* reset delay 125ms */
  186. }
  187. /* U-Boot common routines */
  188. phys_size_t initdram(int board_type)
  189. {
  190. struct jz4740_emc *emc = (struct jz4740_emc *)JZ4740_EMC_BASE;
  191. u32 dmcr;
  192. u32 rows, cols, dw, banks;
  193. ulong size;
  194. dmcr = readl(&emc->dmcr);
  195. rows = 11 + ((dmcr & EMC_DMCR_RA_MASK) >> EMC_DMCR_RA_BIT);
  196. cols = 8 + ((dmcr & EMC_DMCR_CA_MASK) >> EMC_DMCR_CA_BIT);
  197. dw = (dmcr & EMC_DMCR_BW) ? 2 : 4;
  198. banks = (dmcr & EMC_DMCR_BA) ? 4 : 2;
  199. size = (1 << (rows + cols)) * dw * banks;
  200. return size;
  201. }