tqm834x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * (C) Copyright 2005
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <ioports.h>
  9. #include <mpc83xx.h>
  10. #include <asm/mpc8349_pci.h>
  11. #include <i2c.h>
  12. #include <miiphy.h>
  13. #include <asm/mmu.h>
  14. #include <pci.h>
  15. #include <flash.h>
  16. #include <mtd/cfi_flash.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #define IOSYNC asm("eieio")
  19. #define ISYNC asm("isync")
  20. #define SYNC asm("sync")
  21. #define FPW FLASH_PORT_WIDTH
  22. #define FPWV FLASH_PORT_WIDTHV
  23. #define DDR_MAX_SIZE_PER_CS 0x20000000
  24. #if defined(DDR_CASLAT_20)
  25. #define TIMING_CASLAT TIMING_CFG1_CASLAT_20
  26. #define MODE_CASLAT DDR_MODE_CASLAT_20
  27. #else
  28. #define TIMING_CASLAT TIMING_CFG1_CASLAT_25
  29. #define MODE_CASLAT DDR_MODE_CASLAT_25
  30. #endif
  31. #define INITIAL_CS_CONFIG (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
  32. CSCONFIG_COL_BIT_9)
  33. /* External definitions */
  34. ulong flash_get_size (ulong base, int banknum);
  35. /* Local functions */
  36. static int detect_num_flash_banks(void);
  37. static long int get_ddr_bank_size(short cs, long *base);
  38. static void set_cs_bounds(short cs, ulong base, ulong size);
  39. static void set_cs_config(short cs, long config);
  40. static void set_ddr_config(void);
  41. /* Local variable */
  42. static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  43. /**************************************************************************
  44. * Board initialzation after relocation to RAM. Used to detect the number
  45. * of Flash banks on TQM834x.
  46. */
  47. int board_early_init_r (void) {
  48. /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
  49. if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
  50. return 0;
  51. /* detect the number of Flash banks */
  52. return detect_num_flash_banks();
  53. }
  54. /**************************************************************************
  55. * DRAM initalization and size detection
  56. */
  57. phys_size_t initdram (int board_type)
  58. {
  59. long bank_size;
  60. long size;
  61. int cs;
  62. /* during size detection, set up the max DDRLAW size */
  63. im->sysconf.ddrlaw[0].bar = CONFIG_SYS_DDR_BASE;
  64. im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
  65. /* set CS bounds to maximum size */
  66. for(cs = 0; cs < 4; ++cs) {
  67. set_cs_bounds(cs,
  68. CONFIG_SYS_DDR_BASE + (cs * DDR_MAX_SIZE_PER_CS),
  69. DDR_MAX_SIZE_PER_CS);
  70. set_cs_config(cs, INITIAL_CS_CONFIG);
  71. }
  72. /* configure ddr controller */
  73. set_ddr_config();
  74. udelay(200);
  75. /* enable DDR controller */
  76. im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
  77. SDRAM_CFG_SREN |
  78. SDRAM_CFG_SDRAM_TYPE_DDR1);
  79. SYNC;
  80. /* size detection */
  81. debug("\n");
  82. size = 0;
  83. for(cs = 0; cs < 4; ++cs) {
  84. debug("\nDetecting Bank%d\n", cs);
  85. bank_size = get_ddr_bank_size(cs,
  86. (long *)(CONFIG_SYS_DDR_BASE + size));
  87. size += bank_size;
  88. debug("DDR Bank%d size: %ld MiB\n\n", cs, bank_size >> 20);
  89. /* exit if less than one bank */
  90. if(size < DDR_MAX_SIZE_PER_CS) break;
  91. }
  92. return size;
  93. }
  94. /**************************************************************************
  95. * checkboard()
  96. */
  97. int checkboard (void)
  98. {
  99. puts("Board: TQM834x\n");
  100. #ifdef CONFIG_PCI
  101. volatile immap_t * immr;
  102. u32 w, f;
  103. immr = (immap_t *)CONFIG_SYS_IMMR;
  104. if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
  105. printf("PCI: NOT in host mode..?!\n");
  106. return 0;
  107. }
  108. /* get bus width */
  109. w = 32;
  110. if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
  111. w = 64;
  112. /* get clock */
  113. f = gd->pci_clk;
  114. printf("PCI1: %d bit, %d MHz\n", w, f / 1000000);
  115. #else
  116. printf("PCI: disabled\n");
  117. #endif
  118. return 0;
  119. }
  120. /**************************************************************************
  121. *
  122. * Local functions
  123. *
  124. *************************************************************************/
  125. /**************************************************************************
  126. * Detect the number of flash banks (1 or 2). Store it in
  127. * a global variable tqm834x_num_flash_banks.
  128. * Bank detection code based on the Monitor code.
  129. */
  130. static int detect_num_flash_banks(void)
  131. {
  132. typedef unsigned long FLASH_PORT_WIDTH;
  133. typedef volatile unsigned long FLASH_PORT_WIDTHV;
  134. FPWV *bank1_base;
  135. FPWV *bank2_base;
  136. FPW bank1_read;
  137. FPW bank2_read;
  138. ulong bank1_size;
  139. ulong bank2_size;
  140. ulong total_size;
  141. cfi_flash_num_flash_banks = 2; /* assume two banks */
  142. /* Get bank 1 and 2 information */
  143. bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
  144. debug("Bank1 size: %lu\n", bank1_size);
  145. bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
  146. debug("Bank2 size: %lu\n", bank2_size);
  147. total_size = bank1_size + bank2_size;
  148. if (bank2_size > 0) {
  149. /* Seems like we've got bank 2, but maybe it's mirrored 1 */
  150. /* Set the base addresses */
  151. bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
  152. bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
  153. /* Put bank 2 into CFI command mode and read */
  154. bank2_base[0x55] = 0x00980098;
  155. IOSYNC;
  156. ISYNC;
  157. bank2_read = bank2_base[0x10];
  158. /* Read from bank 1 (it's in read mode) */
  159. bank1_read = bank1_base[0x10];
  160. /* Reset Flash */
  161. bank1_base[0] = 0x00F000F0;
  162. bank2_base[0] = 0x00F000F0;
  163. if (bank2_read == bank1_read) {
  164. /*
  165. * Looks like just one bank, but not sure yet. Let's
  166. * read from bank 2 in autosoelect mode.
  167. */
  168. bank2_base[0x0555] = 0x00AA00AA;
  169. bank2_base[0x02AA] = 0x00550055;
  170. bank2_base[0x0555] = 0x00900090;
  171. IOSYNC;
  172. ISYNC;
  173. bank2_read = bank2_base[0x10];
  174. /* Read from bank 1 (it's in read mode) */
  175. bank1_read = bank1_base[0x10];
  176. /* Reset Flash */
  177. bank1_base[0] = 0x00F000F0;
  178. bank2_base[0] = 0x00F000F0;
  179. if (bank2_read == bank1_read) {
  180. /*
  181. * In both CFI command and autoselect modes,
  182. * we got the some data reading from Flash.
  183. * There is only one mirrored bank.
  184. */
  185. cfi_flash_num_flash_banks = 1;
  186. total_size = bank1_size;
  187. }
  188. }
  189. }
  190. debug("Number of flash banks detected: %d\n", cfi_flash_num_flash_banks);
  191. /* set OR0 and BR0 */
  192. set_lbc_or(0, CONFIG_SYS_OR_TIMING_FLASH |
  193. (-(total_size) & OR_GPCM_AM));
  194. set_lbc_br(0, (CONFIG_SYS_FLASH_BASE & BR_BA) |
  195. (BR_MS_GPCM | BR_PS_32 | BR_V));
  196. return (0);
  197. }
  198. /*************************************************************************
  199. * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
  200. */
  201. static long int get_ddr_bank_size(short cs, long *base)
  202. {
  203. /* This array lists all valid DDR SDRAM configurations, with
  204. * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
  205. * The last entry has to to have size equal 0 and is igonred during
  206. * autodection. Bank sizes must be in increasing order of size
  207. */
  208. struct {
  209. long row;
  210. long col;
  211. long size;
  212. } conf[] = {
  213. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_8, 32 << 20},
  214. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_9, 64 << 20},
  215. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_10, 128 << 20},
  216. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_9, 128 << 20},
  217. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_10, 256 << 20},
  218. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_11, 512 << 20},
  219. {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_10, 512 << 20},
  220. {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_11, 1024 << 20},
  221. {0, 0, 0}
  222. };
  223. int i;
  224. int detected;
  225. long size;
  226. detected = -1;
  227. for(i = 0; conf[i].size != 0; ++i) {
  228. /* set sdram bank configuration */
  229. set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
  230. debug("Getting RAM size...\n");
  231. size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
  232. if((size == conf[i].size) && (i == detected + 1))
  233. detected = i;
  234. debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
  235. conf[i].row,
  236. conf[i].col,
  237. conf[i].size >> 20,
  238. base,
  239. size >> 20);
  240. }
  241. if(detected == -1){
  242. /* disable empty cs */
  243. debug("\nNo valid configurations for CS%d, disabling...\n", cs);
  244. set_cs_config(cs, 0);
  245. return 0;
  246. }
  247. debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
  248. conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
  249. /* configure cs ro detected params */
  250. set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
  251. conf[detected].col);
  252. set_cs_bounds(cs, (long)base, conf[detected].size);
  253. return(conf[detected].size);
  254. }
  255. /**************************************************************************
  256. * Sets DDR bank CS bounds.
  257. */
  258. static void set_cs_bounds(short cs, ulong base, ulong size)
  259. {
  260. debug("Setting bounds %08lx, %08lx for cs %d\n", base, size, cs);
  261. if(size == 0){
  262. im->ddr.csbnds[cs].csbnds = 0x00000000;
  263. } else {
  264. im->ddr.csbnds[cs].csbnds =
  265. ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
  266. (((base + size - 1) >> CSBNDS_EA_SHIFT) &
  267. CSBNDS_EA);
  268. }
  269. SYNC;
  270. }
  271. /**************************************************************************
  272. * Sets DDR banks CS configuration.
  273. * config == 0x00000000 disables the CS.
  274. */
  275. static void set_cs_config(short cs, long config)
  276. {
  277. debug("Setting config %08lx for cs %d\n", config, cs);
  278. im->ddr.cs_config[cs] = config;
  279. SYNC;
  280. }
  281. /**************************************************************************
  282. * Sets DDR clocks, timings and configuration.
  283. */
  284. static void set_ddr_config(void) {
  285. /* clock control */
  286. im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
  287. DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
  288. SYNC;
  289. /* timing configuration */
  290. im->ddr.timing_cfg_1 =
  291. (4 << TIMING_CFG1_PRETOACT_SHIFT) |
  292. (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
  293. (4 << TIMING_CFG1_ACTTORW_SHIFT) |
  294. (5 << TIMING_CFG1_REFREC_SHIFT) |
  295. (3 << TIMING_CFG1_WRREC_SHIFT) |
  296. (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
  297. (1 << TIMING_CFG1_WRTORD_SHIFT) |
  298. (TIMING_CFG1_CASLAT & TIMING_CASLAT);
  299. im->ddr.timing_cfg_2 =
  300. TIMING_CFG2_CPO_DEF |
  301. (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
  302. SYNC;
  303. /* don't enable DDR controller yet */
  304. im->ddr.sdram_cfg =
  305. SDRAM_CFG_SREN |
  306. SDRAM_CFG_SDRAM_TYPE_DDR1;
  307. SYNC;
  308. /* Set SDRAM mode */
  309. im->ddr.sdram_mode =
  310. ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
  311. SDRAM_MODE_ESD_SHIFT) |
  312. ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
  313. SDRAM_MODE_SD_SHIFT) |
  314. ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
  315. MODE_CASLAT);
  316. SYNC;
  317. /* Set fast SDRAM refresh rate */
  318. im->ddr.sdram_interval =
  319. (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
  320. (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
  321. SYNC;
  322. /* Workaround for DDR6 Erratum
  323. * see MPC8349E Device Errata Rev.8, 2/2006
  324. * This workaround influences the MPC internal "input enables"
  325. * dependent on CAS latency and MPC revision. According to errata
  326. * sheet the internal reserved registers for this workaround are
  327. * not available from revision 2.0 and up.
  328. */
  329. /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
  330. * (0x200)
  331. */
  332. if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
  333. /* There is a internal reserved register at IMMRBAR+0x2F00
  334. * which has to be written with a certain value defined by
  335. * errata sheet.
  336. */
  337. u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
  338. #if defined(DDR_CASLAT_20)
  339. *reserved_p = 0x201c0000;
  340. #else
  341. *reserved_p = 0x202c0000;
  342. #endif
  343. }
  344. }
  345. #ifdef CONFIG_OF_BOARD_SETUP
  346. int ft_board_setup(void *blob, bd_t *bd)
  347. {
  348. ft_cpu_setup(blob, bd);
  349. #ifdef CONFIG_PCI
  350. ft_pci_setup(blob, bd);
  351. #endif /* CONFIG_PCI */
  352. return 0;
  353. }
  354. #endif /* CONFIG_OF_BOARD_SETUP */