tqm834x.c 11 KB

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