cpu_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * (C) Copyright 2000-2007
  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. #include <common.h>
  24. #include <watchdog.h>
  25. #include <asm/ppc4xx-emac.h>
  26. #include <asm/processor.h>
  27. #include <asm/ppc4xx-gpio.h>
  28. #include <asm/ppc4xx.h>
  29. #if defined(CONFIG_405GP) || defined(CONFIG_405EP)
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #endif
  32. #ifndef CONFIG_SYS_PLL_RECONFIG
  33. #define CONFIG_SYS_PLL_RECONFIG 0
  34. #endif
  35. #if defined(CONFIG_440EPX) || \
  36. defined(CONFIG_460EX) || defined(CONFIG_460GT)
  37. static void reset_with_rli(void)
  38. {
  39. u32 reg;
  40. /*
  41. * Set reload inhibit so configuration will persist across
  42. * processor resets
  43. */
  44. mfcpr(CPR0_ICFG, reg);
  45. reg |= CPR0_ICFG_RLI_MASK;
  46. mtcpr(CPR0_ICFG, reg);
  47. /* Reset processor if configuration changed */
  48. __asm__ __volatile__ ("sync; isync");
  49. mtspr(SPRN_DBCR0, 0x20000000);
  50. }
  51. #endif
  52. void reconfigure_pll(u32 new_cpu_freq)
  53. {
  54. #if defined(CONFIG_440EPX)
  55. int reset_needed = 0;
  56. u32 reg, temp;
  57. u32 prbdv0, target_prbdv0, /* CLK_PRIMBD */
  58. fwdva, target_fwdva, fwdvb, target_fwdvb, /* CLK_PLLD */
  59. fbdv, target_fbdv, lfbdv, target_lfbdv,
  60. perdv0, target_perdv0, /* CLK_PERD */
  61. spcid0, target_spcid0; /* CLK_SPCID */
  62. /* Reconfigure clocks if necessary.
  63. * See PPC440EPx User's Manual, sections 8.2 and 14 */
  64. if (new_cpu_freq == 667) {
  65. target_prbdv0 = 2;
  66. target_fwdva = 2;
  67. target_fwdvb = 4;
  68. target_fbdv = 20;
  69. target_lfbdv = 1;
  70. target_perdv0 = 4;
  71. target_spcid0 = 4;
  72. mfcpr(CPR0_PRIMBD0, reg);
  73. temp = (reg & PRBDV_MASK) >> 24;
  74. prbdv0 = temp ? temp : 8;
  75. if (prbdv0 != target_prbdv0) {
  76. reg &= ~PRBDV_MASK;
  77. reg |= ((target_prbdv0 == 8 ? 0 : target_prbdv0) << 24);
  78. mtcpr(CPR0_PRIMBD0, reg);
  79. reset_needed = 1;
  80. }
  81. mfcpr(CPR0_PLLD, reg);
  82. temp = (reg & PLLD_FWDVA_MASK) >> 16;
  83. fwdva = temp ? temp : 16;
  84. temp = (reg & PLLD_FWDVB_MASK) >> 8;
  85. fwdvb = temp ? temp : 8;
  86. temp = (reg & PLLD_FBDV_MASK) >> 24;
  87. fbdv = temp ? temp : 32;
  88. temp = (reg & PLLD_LFBDV_MASK);
  89. lfbdv = temp ? temp : 64;
  90. if (fwdva != target_fwdva || fbdv != target_fbdv || lfbdv != target_lfbdv) {
  91. reg &= ~(PLLD_FWDVA_MASK | PLLD_FWDVB_MASK |
  92. PLLD_FBDV_MASK | PLLD_LFBDV_MASK);
  93. reg |= ((target_fwdva == 16 ? 0 : target_fwdva) << 16) |
  94. ((target_fwdvb == 8 ? 0 : target_fwdvb) << 8) |
  95. ((target_fbdv == 32 ? 0 : target_fbdv) << 24) |
  96. (target_lfbdv == 64 ? 0 : target_lfbdv);
  97. mtcpr(CPR0_PLLD, reg);
  98. reset_needed = 1;
  99. }
  100. mfcpr(CPR0_PERD, reg);
  101. perdv0 = (reg & CPR0_PERD_PERDV0_MASK) >> 24;
  102. if (perdv0 != target_perdv0) {
  103. reg &= ~CPR0_PERD_PERDV0_MASK;
  104. reg |= (target_perdv0 << 24);
  105. mtcpr(CPR0_PERD, reg);
  106. reset_needed = 1;
  107. }
  108. mfcpr(CPR0_SPCID, reg);
  109. temp = (reg & CPR0_SPCID_SPCIDV0_MASK) >> 24;
  110. spcid0 = temp ? temp : 4;
  111. if (spcid0 != target_spcid0) {
  112. reg &= ~CPR0_SPCID_SPCIDV0_MASK;
  113. reg |= ((target_spcid0 == 4 ? 0 : target_spcid0) << 24);
  114. mtcpr(CPR0_SPCID, reg);
  115. reset_needed = 1;
  116. }
  117. }
  118. /* Get current value of FWDVA.*/
  119. mfcpr(CPR0_PLLD, reg);
  120. temp = (reg & PLLD_FWDVA_MASK) >> 16;
  121. /*
  122. * Check to see if FWDVA has been set to value of 1. if it has we must
  123. * modify it.
  124. */
  125. if (temp == 1) {
  126. /*
  127. * Load register that contains current boot strapping option.
  128. */
  129. mfcpr(CPR0_ICFG, reg);
  130. /*
  131. * Strapping option bits (ICS) are already in correct position,
  132. * only masking needed.
  133. */
  134. reg &= CPR0_ICFG_ICS_MASK;
  135. if ((reg == BOOT_STRAP_OPTION_A) || (reg == BOOT_STRAP_OPTION_B) ||
  136. (reg == BOOT_STRAP_OPTION_D) || (reg == BOOT_STRAP_OPTION_E)) {
  137. mfcpr(CPR0_PLLD, reg);
  138. /* Get current value of fbdv. */
  139. temp = (reg & PLLD_FBDV_MASK) >> 24;
  140. fbdv = temp ? temp : 32;
  141. /* Get current value of lfbdv. */
  142. temp = (reg & PLLD_LFBDV_MASK);
  143. lfbdv = temp ? temp : 64;
  144. /*
  145. * Get current value of FWDVA. Assign current FWDVA to
  146. * new FWDVB.
  147. */
  148. mfcpr(CPR0_PLLD, reg);
  149. target_fwdvb = (reg & PLLD_FWDVA_MASK) >> 16;
  150. fwdvb = target_fwdvb ? target_fwdvb : 8;
  151. /*
  152. * Get current value of FWDVB. Assign current FWDVB to
  153. * new FWDVA.
  154. */
  155. target_fwdva = (reg & PLLD_FWDVB_MASK) >> 8;
  156. fwdva = target_fwdva ? target_fwdva : 16;
  157. /*
  158. * Update CPR0_PLLD with switched FWDVA and FWDVB.
  159. */
  160. reg &= ~(PLLD_FWDVA_MASK | PLLD_FWDVB_MASK |
  161. PLLD_FBDV_MASK | PLLD_LFBDV_MASK);
  162. reg |= ((fwdva == 16 ? 0 : fwdva) << 16) |
  163. ((fwdvb == 8 ? 0 : fwdvb) << 8) |
  164. ((fbdv == 32 ? 0 : fbdv) << 24) |
  165. (lfbdv == 64 ? 0 : lfbdv);
  166. mtcpr(CPR0_PLLD, reg);
  167. /* Acknowledge that a reset is required. */
  168. reset_needed = 1;
  169. }
  170. }
  171. /* Now reset the CPU if needed */
  172. if (reset_needed)
  173. reset_with_rli();
  174. #endif
  175. #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
  176. u32 reg;
  177. /*
  178. * See "9.2.1.1 Booting with Option E" in the 460EX/GT
  179. * users manual
  180. */
  181. mfcpr(CPR0_PLLC, reg);
  182. if ((reg & (CPR0_PLLC_RST | CPR0_PLLC_ENG)) == CPR0_PLLC_RST) {
  183. /*
  184. * Set engage bit
  185. */
  186. reg = (reg & ~CPR0_PLLC_RST) | CPR0_PLLC_ENG;
  187. mtcpr(CPR0_PLLC, reg);
  188. /* Now reset the CPU */
  189. reset_with_rli();
  190. }
  191. #endif
  192. }
  193. /*
  194. * Breath some life into the CPU...
  195. *
  196. * Reconfigure PLL if necessary,
  197. * set up the memory map,
  198. * initialize a bunch of registers
  199. */
  200. void
  201. cpu_init_f (void)
  202. {
  203. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_440GX) || defined(CONFIG_460EX)
  204. u32 val;
  205. #endif
  206. reconfigure_pll(CONFIG_SYS_PLL_RECONFIG);
  207. #if (defined(CONFIG_405EP) || defined (CONFIG_405EX)) && !defined(CONFIG_SYS_4xx_GPIO_TABLE)
  208. /*
  209. * GPIO0 setup (select GPIO or alternate function)
  210. */
  211. #if defined(CONFIG_SYS_GPIO0_OR)
  212. out32(GPIO0_OR, CONFIG_SYS_GPIO0_OR); /* set initial state of output pins */
  213. #endif
  214. #if defined(CONFIG_SYS_GPIO0_ODR)
  215. out32(GPIO0_ODR, CONFIG_SYS_GPIO0_ODR); /* open-drain select */
  216. #endif
  217. out32(GPIO0_OSRH, CONFIG_SYS_GPIO0_OSRH); /* output select */
  218. out32(GPIO0_OSRL, CONFIG_SYS_GPIO0_OSRL);
  219. out32(GPIO0_ISR1H, CONFIG_SYS_GPIO0_ISR1H); /* input select */
  220. out32(GPIO0_ISR1L, CONFIG_SYS_GPIO0_ISR1L);
  221. out32(GPIO0_TSRH, CONFIG_SYS_GPIO0_TSRH); /* three-state select */
  222. out32(GPIO0_TSRL, CONFIG_SYS_GPIO0_TSRL);
  223. #if defined(CONFIG_SYS_GPIO0_ISR2H)
  224. out32(GPIO0_ISR2H, CONFIG_SYS_GPIO0_ISR2H);
  225. out32(GPIO0_ISR2L, CONFIG_SYS_GPIO0_ISR2L);
  226. #endif
  227. #if defined (CONFIG_SYS_GPIO0_TCR)
  228. out32(GPIO0_TCR, CONFIG_SYS_GPIO0_TCR); /* enable output driver for outputs */
  229. #endif
  230. #endif /* CONFIG_405EP ... && !CONFIG_SYS_4xx_GPIO_TABLE */
  231. #if defined (CONFIG_405EP)
  232. /*
  233. * Set EMAC noise filter bits
  234. */
  235. mtdcr(CPC0_EPCTL, CPC0_EPRCSR_E0NFE | CPC0_EPRCSR_E1NFE);
  236. #endif /* CONFIG_405EP */
  237. #if defined(CONFIG_SYS_4xx_GPIO_TABLE)
  238. gpio_set_chip_configuration();
  239. #endif /* CONFIG_SYS_4xx_GPIO_TABLE */
  240. /*
  241. * External Bus Controller (EBC) Setup
  242. */
  243. #if (defined(CONFIG_SYS_EBC_PB0AP) && defined(CONFIG_SYS_EBC_PB0CR))
  244. #if (defined(CONFIG_405GP) || defined(CONFIG_405CR) || \
  245. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  246. defined(CONFIG_405EX) || defined(CONFIG_405))
  247. /*
  248. * Move the next instructions into icache, since these modify the flash
  249. * we are running from!
  250. */
  251. asm volatile(" bl 0f" ::: "lr");
  252. asm volatile("0: mflr 3" ::: "r3");
  253. asm volatile(" addi 4, 0, 14" ::: "r4");
  254. asm volatile(" mtctr 4" ::: "ctr");
  255. asm volatile("1: icbt 0, 3");
  256. asm volatile(" addi 3, 3, 32" ::: "r3");
  257. asm volatile(" bdnz 1b" ::: "ctr", "cr0");
  258. asm volatile(" addis 3, 0, 0x0" ::: "r3");
  259. asm volatile(" ori 3, 3, 0xA000" ::: "r3");
  260. asm volatile(" mtctr 3" ::: "ctr");
  261. asm volatile("2: bdnz 2b" ::: "ctr", "cr0");
  262. #endif
  263. mtebc(PB0AP, CONFIG_SYS_EBC_PB0AP);
  264. mtebc(PB0CR, CONFIG_SYS_EBC_PB0CR);
  265. #endif
  266. #if (defined(CONFIG_SYS_EBC_PB1AP) && defined(CONFIG_SYS_EBC_PB1CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 1))
  267. mtebc(PB1AP, CONFIG_SYS_EBC_PB1AP);
  268. mtebc(PB1CR, CONFIG_SYS_EBC_PB1CR);
  269. #endif
  270. #if (defined(CONFIG_SYS_EBC_PB2AP) && defined(CONFIG_SYS_EBC_PB2CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 2))
  271. mtebc(PB2AP, CONFIG_SYS_EBC_PB2AP);
  272. mtebc(PB2CR, CONFIG_SYS_EBC_PB2CR);
  273. #endif
  274. #if (defined(CONFIG_SYS_EBC_PB3AP) && defined(CONFIG_SYS_EBC_PB3CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 3))
  275. mtebc(PB3AP, CONFIG_SYS_EBC_PB3AP);
  276. mtebc(PB3CR, CONFIG_SYS_EBC_PB3CR);
  277. #endif
  278. #if (defined(CONFIG_SYS_EBC_PB4AP) && defined(CONFIG_SYS_EBC_PB4CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 4))
  279. mtebc(PB4AP, CONFIG_SYS_EBC_PB4AP);
  280. mtebc(PB4CR, CONFIG_SYS_EBC_PB4CR);
  281. #endif
  282. #if (defined(CONFIG_SYS_EBC_PB5AP) && defined(CONFIG_SYS_EBC_PB5CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 5))
  283. mtebc(PB5AP, CONFIG_SYS_EBC_PB5AP);
  284. mtebc(PB5CR, CONFIG_SYS_EBC_PB5CR);
  285. #endif
  286. #if (defined(CONFIG_SYS_EBC_PB6AP) && defined(CONFIG_SYS_EBC_PB6CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 6))
  287. mtebc(PB6AP, CONFIG_SYS_EBC_PB6AP);
  288. mtebc(PB6CR, CONFIG_SYS_EBC_PB6CR);
  289. #endif
  290. #if (defined(CONFIG_SYS_EBC_PB7AP) && defined(CONFIG_SYS_EBC_PB7CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 7))
  291. mtebc(PB7AP, CONFIG_SYS_EBC_PB7AP);
  292. mtebc(PB7CR, CONFIG_SYS_EBC_PB7CR);
  293. #endif
  294. #if defined (CONFIG_SYS_EBC_CFG)
  295. mtebc(EBC0_CFG, CONFIG_SYS_EBC_CFG);
  296. #endif
  297. #if defined(CONFIG_WATCHDOG)
  298. val = mfspr(tcr);
  299. #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
  300. val |= 0xb8000000; /* generate system reset after 1.34 seconds */
  301. #elif defined(CONFIG_440EPX)
  302. val |= 0xb0000000; /* generate system reset after 1.34 seconds */
  303. #else
  304. val |= 0xf0000000; /* generate system reset after 2.684 seconds */
  305. #endif
  306. #if defined(CONFIG_SYS_4xx_RESET_TYPE)
  307. val &= ~0x30000000; /* clear WRC bits */
  308. val |= CONFIG_SYS_4xx_RESET_TYPE << 28; /* set board specific WRC type */
  309. #endif
  310. mtspr(tcr, val);
  311. val = mfspr(tsr);
  312. val |= 0x80000000; /* enable watchdog timer */
  313. mtspr(tsr, val);
  314. reset_4xx_watchdog();
  315. #endif /* CONFIG_WATCHDOG */
  316. #if defined(CONFIG_440GX)
  317. /* Take the GX out of compatibility mode
  318. * Travis Sawyer, 9 Mar 2004
  319. * NOTE: 440gx user manual inconsistency here
  320. * Compatibility mode and Ethernet Clock select are not
  321. * correct in the manual
  322. */
  323. mfsdr(SDR0_MFR, val);
  324. val &= ~0x10000000;
  325. mtsdr(SDR0_MFR,val);
  326. #endif /* CONFIG_440GX */
  327. #if defined(CONFIG_460EX)
  328. /*
  329. * Set SDR0_AHB_CFG[A2P_INCR4] (bit 24) and
  330. * clear SDR0_AHB_CFG[A2P_PROT2] (bit 25) for a new 460EX errata
  331. * regarding concurrent use of AHB USB OTG, USB 2.0 host and SATA
  332. */
  333. mfsdr(SDR0_AHB_CFG, val);
  334. val |= 0x80;
  335. val &= ~0x40;
  336. mtsdr(SDR0_AHB_CFG, val);
  337. mfsdr(SDR0_USB2HOST_CFG, val);
  338. val &= ~0xf00;
  339. val |= 0x400;
  340. mtsdr(SDR0_USB2HOST_CFG, val);
  341. #endif /* CONFIG_460EX */
  342. #if defined(CONFIG_405EX) || \
  343. defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  344. defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
  345. defined(CONFIG_460SX)
  346. /*
  347. * Set PLB4 arbiter (Segment 0 and 1) to 4 deep pipeline read
  348. */
  349. mtdcr(PLB4A0_ACR, (mfdcr(PLB4A0_ACR) & ~PLB4Ax_ACR_RDP_MASK) |
  350. PLB4Ax_ACR_RDP_4DEEP);
  351. mtdcr(PLB4A1_ACR, (mfdcr(PLB4A1_ACR) & ~PLB4Ax_ACR_RDP_MASK) |
  352. PLB4Ax_ACR_RDP_4DEEP);
  353. #endif /* CONFIG_440SP/SPE || CONFIG_460EX/GT || CONFIG_405EX */
  354. }
  355. /*
  356. * initialize higher level parts of CPU like time base and timers
  357. */
  358. int cpu_init_r (void)
  359. {
  360. #if defined(CONFIG_405GP)
  361. uint pvr = get_pvr();
  362. /*
  363. * Set edge conditioning circuitry on PPC405GPr
  364. * for compatibility to existing PPC405GP designs.
  365. */
  366. if ((pvr & 0xfffffff0) == (PVR_405GPR_RB & 0xfffffff0)) {
  367. mtdcr(CPC0_ECR, 0x60606000);
  368. }
  369. #endif /* defined(CONFIG_405GP) */
  370. return 0;
  371. }
  372. #if defined(CONFIG_PCI) && \
  373. (defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  374. defined(CONFIG_440GR) || defined(CONFIG_440GRX))
  375. /*
  376. * 440EP(x)/GR(x) PCI async/sync clocking restriction:
  377. *
  378. * In asynchronous PCI mode, the synchronous PCI clock must meet
  379. * certain requirements. The following equation describes the
  380. * relationship that must be maintained between the asynchronous PCI
  381. * clock and synchronous PCI clock. Select an appropriate PCI:PLB
  382. * ratio to maintain the relationship:
  383. *
  384. * AsyncPCIClk - 1MHz <= SyncPCIclock <= (2 * AsyncPCIClk) - 1MHz
  385. */
  386. static int ppc4xx_pci_sync_clock_ok(u32 sync, u32 async)
  387. {
  388. if (((async - 1000000) > sync) || (sync > ((2 * async) - 1000000)))
  389. return 0;
  390. else
  391. return 1;
  392. }
  393. int ppc4xx_pci_sync_clock_config(u32 async)
  394. {
  395. sys_info_t sys_info;
  396. u32 sync;
  397. int div;
  398. u32 reg;
  399. u32 spcid_val[] = {
  400. CPR0_SPCID_SPCIDV0_DIV1, CPR0_SPCID_SPCIDV0_DIV2,
  401. CPR0_SPCID_SPCIDV0_DIV3, CPR0_SPCID_SPCIDV0_DIV4 };
  402. get_sys_info(&sys_info);
  403. sync = sys_info.freqPCI;
  404. /*
  405. * First check if the equation above is met
  406. */
  407. if (!ppc4xx_pci_sync_clock_ok(sync, async)) {
  408. /*
  409. * Reconfigure PCI sync clock to meet the equation.
  410. * Start with highest possible PCI sync frequency
  411. * (divider 1).
  412. */
  413. for (div = 1; div <= 4; div++) {
  414. sync = sys_info.freqPLB / div;
  415. if (ppc4xx_pci_sync_clock_ok(sync, async))
  416. break;
  417. }
  418. if (div <= 4) {
  419. mtcpr(CPR0_SPCID, spcid_val[div]);
  420. mfcpr(CPR0_ICFG, reg);
  421. reg |= CPR0_ICFG_RLI_MASK;
  422. mtcpr(CPR0_ICFG, reg);
  423. /* do chip reset */
  424. mtspr(SPRN_DBCR0, 0x20000000);
  425. } else {
  426. /* Impossible to configure the PCI sync clock */
  427. return -1;
  428. }
  429. }
  430. return 0;
  431. }
  432. #endif