4xx.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2007 David Gibson, IBM Corporation.
  3. *
  4. * Based on earlier code:
  5. * Matt Porter <mporter@kernel.crashing.org>
  6. * Copyright 2002-2005 MontaVista Software Inc.
  7. *
  8. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  9. * Copyright (c) 2003, 2004 Zultys Technologies
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <stddef.h>
  17. #include "types.h"
  18. #include "string.h"
  19. #include "stdio.h"
  20. #include "ops.h"
  21. #include "reg.h"
  22. #include "dcr.h"
  23. /* Read the 4xx SDRAM controller to get size of system memory. */
  24. void ibm4xx_fixup_memsize(void)
  25. {
  26. int i;
  27. unsigned long memsize, bank_config;
  28. memsize = 0;
  29. for (i = 0; i < ARRAY_SIZE(sdram_bxcr); i++) {
  30. mtdcr(DCRN_SDRAM0_CFGADDR, sdram_bxcr[i]);
  31. bank_config = mfdcr(DCRN_SDRAM0_CFGDATA);
  32. if (bank_config & SDRAM_CONFIG_BANK_ENABLE)
  33. memsize += SDRAM_CONFIG_BANK_SIZE(bank_config);
  34. }
  35. dt_fixup_memory(0, memsize);
  36. }
  37. /* 4xx DDR1/2 Denali memory controller support */
  38. /* DDR0 registers */
  39. #define DDR0_02 2
  40. #define DDR0_08 8
  41. #define DDR0_10 10
  42. #define DDR0_14 14
  43. #define DDR0_42 42
  44. #define DDR0_43 43
  45. /* DDR0_02 */
  46. #define DDR_START 0x1
  47. #define DDR_START_SHIFT 0
  48. #define DDR_MAX_CS_REG 0x3
  49. #define DDR_MAX_CS_REG_SHIFT 24
  50. #define DDR_MAX_COL_REG 0xf
  51. #define DDR_MAX_COL_REG_SHIFT 16
  52. #define DDR_MAX_ROW_REG 0xf
  53. #define DDR_MAX_ROW_REG_SHIFT 8
  54. /* DDR0_08 */
  55. #define DDR_DDR2_MODE 0x1
  56. #define DDR_DDR2_MODE_SHIFT 0
  57. /* DDR0_10 */
  58. #define DDR_CS_MAP 0x3
  59. #define DDR_CS_MAP_SHIFT 8
  60. /* DDR0_14 */
  61. #define DDR_REDUC 0x1
  62. #define DDR_REDUC_SHIFT 16
  63. /* DDR0_42 */
  64. #define DDR_APIN 0x7
  65. #define DDR_APIN_SHIFT 24
  66. /* DDR0_43 */
  67. #define DDR_COL_SZ 0x7
  68. #define DDR_COL_SZ_SHIFT 8
  69. #define DDR_BANK8 0x1
  70. #define DDR_BANK8_SHIFT 0
  71. #define DDR_GET_VAL(val, mask, shift) (((val) >> (shift)) & (mask))
  72. static inline u32 mfdcr_sdram0(u32 reg)
  73. {
  74. mtdcr(DCRN_SDRAM0_CFGADDR, reg);
  75. return mfdcr(DCRN_SDRAM0_CFGDATA);
  76. }
  77. void ibm4xx_denali_fixup_memsize(void)
  78. {
  79. u32 val, max_cs, max_col, max_row;
  80. u32 cs, col, row, bank, dpath;
  81. unsigned long memsize;
  82. val = mfdcr_sdram0(DDR0_02);
  83. if (!DDR_GET_VAL(val, DDR_START, DDR_START_SHIFT))
  84. fatal("DDR controller is not initialized\n");
  85. /* get maximum cs col and row values */
  86. max_cs = DDR_GET_VAL(val, DDR_MAX_CS_REG, DDR_MAX_CS_REG_SHIFT);
  87. max_col = DDR_GET_VAL(val, DDR_MAX_COL_REG, DDR_MAX_COL_REG_SHIFT);
  88. max_row = DDR_GET_VAL(val, DDR_MAX_ROW_REG, DDR_MAX_ROW_REG_SHIFT);
  89. /* get CS value */
  90. val = mfdcr_sdram0(DDR0_10);
  91. val = DDR_GET_VAL(val, DDR_CS_MAP, DDR_CS_MAP_SHIFT);
  92. cs = 0;
  93. while (val) {
  94. if (val && 0x1)
  95. cs++;
  96. val = val >> 1;
  97. }
  98. if (!cs)
  99. fatal("No memory installed\n");
  100. if (cs > max_cs)
  101. fatal("DDR wrong CS configuration\n");
  102. /* get data path bytes */
  103. val = mfdcr_sdram0(DDR0_14);
  104. if (DDR_GET_VAL(val, DDR_REDUC, DDR_REDUC_SHIFT))
  105. dpath = 8; /* 64 bits */
  106. else
  107. dpath = 4; /* 32 bits */
  108. /* get adress pins (rows) */
  109. val = mfdcr_sdram0(DDR0_42);
  110. row = DDR_GET_VAL(val, DDR_APIN, DDR_APIN_SHIFT);
  111. if (row > max_row)
  112. fatal("DDR wrong APIN configuration\n");
  113. row = max_row - row;
  114. /* get collomn size and banks */
  115. val = mfdcr_sdram0(DDR0_43);
  116. col = DDR_GET_VAL(val, DDR_COL_SZ, DDR_COL_SZ_SHIFT);
  117. if (col > max_col)
  118. fatal("DDR wrong COL configuration\n");
  119. col = max_col - col;
  120. if (DDR_GET_VAL(val, DDR_BANK8, DDR_BANK8_SHIFT))
  121. bank = 8; /* 8 banks */
  122. else
  123. bank = 4; /* 4 banks */
  124. memsize = cs * (1 << (col+row)) * bank * dpath;
  125. dt_fixup_memory(0, memsize);
  126. }
  127. #define SPRN_DBCR0_40X 0x3F2
  128. #define SPRN_DBCR0_44X 0x134
  129. #define DBCR0_RST_SYSTEM 0x30000000
  130. void ibm44x_dbcr_reset(void)
  131. {
  132. unsigned long tmp;
  133. asm volatile (
  134. "mfspr %0,%1\n"
  135. "oris %0,%0,%2@h\n"
  136. "mtspr %1,%0"
  137. : "=&r"(tmp) : "i"(SPRN_DBCR0_44X), "i"(DBCR0_RST_SYSTEM)
  138. );
  139. }
  140. void ibm40x_dbcr_reset(void)
  141. {
  142. unsigned long tmp;
  143. asm volatile (
  144. "mfspr %0,%1\n"
  145. "oris %0,%0,%2@h\n"
  146. "mtspr %1,%0"
  147. : "=&r"(tmp) : "i"(SPRN_DBCR0_40X), "i"(DBCR0_RST_SYSTEM)
  148. );
  149. }
  150. #define EMAC_RESET 0x20000000
  151. void ibm4xx_quiesce_eth(u32 *emac0, u32 *emac1)
  152. {
  153. /* Quiesce the MAL and EMAC(s) since PIBS/OpenBIOS don't do this for us */
  154. if (emac0)
  155. *emac0 = EMAC_RESET;
  156. if (emac1)
  157. *emac1 = EMAC_RESET;
  158. mtdcr(DCRN_MAL0_CFG, MAL_RESET);
  159. }
  160. /* Read 4xx EBC bus bridge registers to get mappings of the peripheral
  161. * banks into the OPB address space */
  162. void ibm4xx_fixup_ebc_ranges(const char *ebc)
  163. {
  164. void *devp;
  165. u32 bxcr;
  166. u32 ranges[EBC_NUM_BANKS*4];
  167. u32 *p = ranges;
  168. int i;
  169. for (i = 0; i < EBC_NUM_BANKS; i++) {
  170. mtdcr(DCRN_EBC0_CFGADDR, EBC_BXCR(i));
  171. bxcr = mfdcr(DCRN_EBC0_CFGDATA);
  172. if ((bxcr & EBC_BXCR_BU) != EBC_BXCR_BU_OFF) {
  173. *p++ = i;
  174. *p++ = 0;
  175. *p++ = bxcr & EBC_BXCR_BAS;
  176. *p++ = EBC_BXCR_BANK_SIZE(bxcr);
  177. }
  178. }
  179. devp = finddevice(ebc);
  180. if (! devp)
  181. fatal("Couldn't locate EBC node %s\n\r", ebc);
  182. setprop(devp, "ranges", ranges, (p - ranges) * sizeof(u32));
  183. }
  184. #define SPRN_CCR1 0x378
  185. void ibm440ep_fixup_clocks(unsigned int sysclk, unsigned int ser_clk)
  186. {
  187. u32 cpu, plb, opb, ebc, tb, uart0, m, vco;
  188. u32 reg;
  189. u32 fwdva, fwdvb, fbdv, lfbdv, opbdv0, perdv0, spcid0, prbdv0, tmp;
  190. mtdcr(DCRN_CPR0_ADDR, CPR0_PLLD0);
  191. reg = mfdcr(DCRN_CPR0_DATA);
  192. tmp = (reg & 0x000F0000) >> 16;
  193. fwdva = tmp ? tmp : 16;
  194. tmp = (reg & 0x00000700) >> 8;
  195. fwdvb = tmp ? tmp : 8;
  196. tmp = (reg & 0x1F000000) >> 24;
  197. fbdv = tmp ? tmp : 32;
  198. lfbdv = (reg & 0x0000007F);
  199. mtdcr(DCRN_CPR0_ADDR, CPR0_OPBD0);
  200. reg = mfdcr(DCRN_CPR0_DATA);
  201. tmp = (reg & 0x03000000) >> 24;
  202. opbdv0 = tmp ? tmp : 4;
  203. mtdcr(DCRN_CPR0_ADDR, CPR0_PERD0);
  204. reg = mfdcr(DCRN_CPR0_DATA);
  205. tmp = (reg & 0x07000000) >> 24;
  206. perdv0 = tmp ? tmp : 8;
  207. mtdcr(DCRN_CPR0_ADDR, CPR0_PRIMBD0);
  208. reg = mfdcr(DCRN_CPR0_DATA);
  209. tmp = (reg & 0x07000000) >> 24;
  210. prbdv0 = tmp ? tmp : 8;
  211. mtdcr(DCRN_CPR0_ADDR, CPR0_SCPID);
  212. reg = mfdcr(DCRN_CPR0_DATA);
  213. tmp = (reg & 0x03000000) >> 24;
  214. spcid0 = tmp ? tmp : 4;
  215. /* Calculate M */
  216. mtdcr(DCRN_CPR0_ADDR, CPR0_PLLC0);
  217. reg = mfdcr(DCRN_CPR0_DATA);
  218. tmp = (reg & 0x03000000) >> 24;
  219. if (tmp == 0) { /* PLL output */
  220. tmp = (reg & 0x20000000) >> 29;
  221. if (!tmp) /* PLLOUTA */
  222. m = fbdv * lfbdv * fwdva;
  223. else
  224. m = fbdv * lfbdv * fwdvb;
  225. }
  226. else if (tmp == 1) /* CPU output */
  227. m = fbdv * fwdva;
  228. else
  229. m = perdv0 * opbdv0 * fwdvb;
  230. vco = (m * sysclk) + (m >> 1);
  231. cpu = vco / fwdva;
  232. plb = vco / fwdvb / prbdv0;
  233. opb = plb / opbdv0;
  234. ebc = plb / perdv0;
  235. /* FIXME */
  236. uart0 = ser_clk;
  237. /* Figure out timebase. Either CPU or default TmrClk */
  238. asm volatile (
  239. "mfspr %0,%1\n"
  240. :
  241. "=&r"(reg) : "i"(SPRN_CCR1));
  242. if (reg & 0x0080)
  243. tb = 25000000; /* TmrClk is 25MHz */
  244. else
  245. tb = cpu;
  246. dt_fixup_cpu_clocks(cpu, tb, 0);
  247. dt_fixup_clock("/plb", plb);
  248. dt_fixup_clock("/plb/opb", opb);
  249. dt_fixup_clock("/plb/opb/ebc", ebc);
  250. dt_fixup_clock("/plb/opb/serial@ef600300", uart0);
  251. dt_fixup_clock("/plb/opb/serial@ef600400", uart0);
  252. dt_fixup_clock("/plb/opb/serial@ef600500", uart0);
  253. dt_fixup_clock("/plb/opb/serial@ef600600", uart0);
  254. }