cpu_init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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)) && \
  208. !defined(CONFIG_APM821XX) &&!defined(CONFIG_SYS_4xx_GPIO_TABLE)
  209. /*
  210. * GPIO0 setup (select GPIO or alternate function)
  211. */
  212. #if defined(CONFIG_SYS_GPIO0_OR)
  213. out32(GPIO0_OR, CONFIG_SYS_GPIO0_OR); /* set initial state of output pins */
  214. #endif
  215. #if defined(CONFIG_SYS_GPIO0_ODR)
  216. out32(GPIO0_ODR, CONFIG_SYS_GPIO0_ODR); /* open-drain select */
  217. #endif
  218. out32(GPIO0_OSRH, CONFIG_SYS_GPIO0_OSRH); /* output select */
  219. out32(GPIO0_OSRL, CONFIG_SYS_GPIO0_OSRL);
  220. out32(GPIO0_ISR1H, CONFIG_SYS_GPIO0_ISR1H); /* input select */
  221. out32(GPIO0_ISR1L, CONFIG_SYS_GPIO0_ISR1L);
  222. out32(GPIO0_TSRH, CONFIG_SYS_GPIO0_TSRH); /* three-state select */
  223. out32(GPIO0_TSRL, CONFIG_SYS_GPIO0_TSRL);
  224. #if defined(CONFIG_SYS_GPIO0_ISR2H)
  225. out32(GPIO0_ISR2H, CONFIG_SYS_GPIO0_ISR2H);
  226. out32(GPIO0_ISR2L, CONFIG_SYS_GPIO0_ISR2L);
  227. #endif
  228. #if defined (CONFIG_SYS_GPIO0_TCR)
  229. out32(GPIO0_TCR, CONFIG_SYS_GPIO0_TCR); /* enable output driver for outputs */
  230. #endif
  231. #endif /* CONFIG_405EP ... && !CONFIG_SYS_4xx_GPIO_TABLE */
  232. #if defined (CONFIG_405EP)
  233. /*
  234. * Set EMAC noise filter bits
  235. */
  236. mtdcr(CPC0_EPCTL, CPC0_EPCTL_E0NFE | CPC0_EPCTL_E1NFE);
  237. #endif /* CONFIG_405EP */
  238. #if defined(CONFIG_SYS_4xx_GPIO_TABLE)
  239. gpio_set_chip_configuration();
  240. #endif /* CONFIG_SYS_4xx_GPIO_TABLE */
  241. /*
  242. * External Bus Controller (EBC) Setup
  243. */
  244. #if (defined(CONFIG_SYS_EBC_PB0AP) && defined(CONFIG_SYS_EBC_PB0CR))
  245. #if (defined(CONFIG_405GP) || defined(CONFIG_405CR) || \
  246. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  247. defined(CONFIG_405EX) || defined(CONFIG_405))
  248. /*
  249. * Move the next instructions into icache, since these modify the flash
  250. * we are running from!
  251. */
  252. asm volatile(" bl 0f" ::: "lr");
  253. asm volatile("0: mflr 3" ::: "r3");
  254. asm volatile(" addi 4, 0, 14" ::: "r4");
  255. asm volatile(" mtctr 4" ::: "ctr");
  256. asm volatile("1: icbt 0, 3");
  257. asm volatile(" addi 3, 3, 32" ::: "r3");
  258. asm volatile(" bdnz 1b" ::: "ctr", "cr0");
  259. asm volatile(" addis 3, 0, 0x0" ::: "r3");
  260. asm volatile(" ori 3, 3, 0xA000" ::: "r3");
  261. asm volatile(" mtctr 3" ::: "ctr");
  262. asm volatile("2: bdnz 2b" ::: "ctr", "cr0");
  263. #endif
  264. mtebc(PB0AP, CONFIG_SYS_EBC_PB0AP);
  265. mtebc(PB0CR, CONFIG_SYS_EBC_PB0CR);
  266. #endif
  267. #if (defined(CONFIG_SYS_EBC_PB1AP) && defined(CONFIG_SYS_EBC_PB1CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 1))
  268. mtebc(PB1AP, CONFIG_SYS_EBC_PB1AP);
  269. mtebc(PB1CR, CONFIG_SYS_EBC_PB1CR);
  270. #endif
  271. #if (defined(CONFIG_SYS_EBC_PB2AP) && defined(CONFIG_SYS_EBC_PB2CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 2))
  272. mtebc(PB2AP, CONFIG_SYS_EBC_PB2AP);
  273. mtebc(PB2CR, CONFIG_SYS_EBC_PB2CR);
  274. #endif
  275. #if (defined(CONFIG_SYS_EBC_PB3AP) && defined(CONFIG_SYS_EBC_PB3CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 3))
  276. mtebc(PB3AP, CONFIG_SYS_EBC_PB3AP);
  277. mtebc(PB3CR, CONFIG_SYS_EBC_PB3CR);
  278. #endif
  279. #if (defined(CONFIG_SYS_EBC_PB4AP) && defined(CONFIG_SYS_EBC_PB4CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 4))
  280. mtebc(PB4AP, CONFIG_SYS_EBC_PB4AP);
  281. mtebc(PB4CR, CONFIG_SYS_EBC_PB4CR);
  282. #endif
  283. #if (defined(CONFIG_SYS_EBC_PB5AP) && defined(CONFIG_SYS_EBC_PB5CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 5))
  284. mtebc(PB5AP, CONFIG_SYS_EBC_PB5AP);
  285. mtebc(PB5CR, CONFIG_SYS_EBC_PB5CR);
  286. #endif
  287. #if (defined(CONFIG_SYS_EBC_PB6AP) && defined(CONFIG_SYS_EBC_PB6CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 6))
  288. mtebc(PB6AP, CONFIG_SYS_EBC_PB6AP);
  289. mtebc(PB6CR, CONFIG_SYS_EBC_PB6CR);
  290. #endif
  291. #if (defined(CONFIG_SYS_EBC_PB7AP) && defined(CONFIG_SYS_EBC_PB7CR) && !(CONFIG_SYS_INIT_DCACHE_CS == 7))
  292. mtebc(PB7AP, CONFIG_SYS_EBC_PB7AP);
  293. mtebc(PB7CR, CONFIG_SYS_EBC_PB7CR);
  294. #endif
  295. #if defined (CONFIG_SYS_EBC_CFG)
  296. mtebc(EBC0_CFG, CONFIG_SYS_EBC_CFG);
  297. #endif
  298. #if defined(CONFIG_WATCHDOG)
  299. val = mfspr(tcr);
  300. #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
  301. val |= 0xb8000000; /* generate system reset after 1.34 seconds */
  302. #elif defined(CONFIG_440EPX)
  303. val |= 0xb0000000; /* generate system reset after 1.34 seconds */
  304. #else
  305. val |= 0xf0000000; /* generate system reset after 2.684 seconds */
  306. #endif
  307. #if defined(CONFIG_SYS_4xx_RESET_TYPE)
  308. val &= ~0x30000000; /* clear WRC bits */
  309. val |= CONFIG_SYS_4xx_RESET_TYPE << 28; /* set board specific WRC type */
  310. #endif
  311. mtspr(tcr, val);
  312. val = mfspr(tsr);
  313. val |= 0x80000000; /* enable watchdog timer */
  314. mtspr(tsr, val);
  315. reset_4xx_watchdog();
  316. #endif /* CONFIG_WATCHDOG */
  317. #if defined(CONFIG_440GX)
  318. /* Take the GX out of compatibility mode
  319. * Travis Sawyer, 9 Mar 2004
  320. * NOTE: 440gx user manual inconsistency here
  321. * Compatibility mode and Ethernet Clock select are not
  322. * correct in the manual
  323. */
  324. mfsdr(SDR0_MFR, val);
  325. val &= ~0x10000000;
  326. mtsdr(SDR0_MFR,val);
  327. #endif /* CONFIG_440GX */
  328. #if defined(CONFIG_460EX)
  329. /*
  330. * Set SDR0_AHB_CFG[A2P_INCR4] (bit 24) and
  331. * clear SDR0_AHB_CFG[A2P_PROT2] (bit 25) for a new 460EX errata
  332. * regarding concurrent use of AHB USB OTG, USB 2.0 host and SATA
  333. */
  334. mfsdr(SDR0_AHB_CFG, val);
  335. val |= 0x80;
  336. val &= ~0x40;
  337. mtsdr(SDR0_AHB_CFG, val);
  338. mfsdr(SDR0_USB2HOST_CFG, val);
  339. val &= ~0xf00;
  340. val |= 0x400;
  341. mtsdr(SDR0_USB2HOST_CFG, val);
  342. #endif /* CONFIG_460EX */
  343. #if defined(CONFIG_405EX) || \
  344. defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  345. defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
  346. defined(CONFIG_460SX) || defined(CONFIG_APM821XX)
  347. /*
  348. * Set PLB4 arbiter (Segment 0 and 1) to 4 deep pipeline read
  349. */
  350. mtdcr(PLB4A0_ACR, (mfdcr(PLB4A0_ACR) & ~PLB4Ax_ACR_RDP_MASK) |
  351. PLB4Ax_ACR_RDP_4DEEP);
  352. mtdcr(PLB4A1_ACR, (mfdcr(PLB4A1_ACR) & ~PLB4Ax_ACR_RDP_MASK) |
  353. PLB4Ax_ACR_RDP_4DEEP);
  354. #endif /* CONFIG_440SP/SPE || CONFIG_460EX/GT || CONFIG_405EX */
  355. }
  356. /*
  357. * initialize higher level parts of CPU like time base and timers
  358. */
  359. int cpu_init_r (void)
  360. {
  361. #if defined(CONFIG_405GP)
  362. uint pvr = get_pvr();
  363. /*
  364. * Set edge conditioning circuitry on PPC405GPr
  365. * for compatibility to existing PPC405GP designs.
  366. */
  367. if ((pvr & 0xfffffff0) == (PVR_405GPR_RB & 0xfffffff0)) {
  368. mtdcr(CPC0_ECR, 0x60606000);
  369. }
  370. #endif /* defined(CONFIG_405GP) */
  371. return 0;
  372. }
  373. #if defined(CONFIG_PCI) && \
  374. (defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  375. defined(CONFIG_440GR) || defined(CONFIG_440GRX))
  376. /*
  377. * 440EP(x)/GR(x) PCI async/sync clocking restriction:
  378. *
  379. * In asynchronous PCI mode, the synchronous PCI clock must meet
  380. * certain requirements. The following equation describes the
  381. * relationship that must be maintained between the asynchronous PCI
  382. * clock and synchronous PCI clock. Select an appropriate PCI:PLB
  383. * ratio to maintain the relationship:
  384. *
  385. * AsyncPCIClk - 1MHz <= SyncPCIclock <= (2 * AsyncPCIClk) - 1MHz
  386. */
  387. static int ppc4xx_pci_sync_clock_ok(u32 sync, u32 async)
  388. {
  389. if (((async - 1000000) > sync) || (sync > ((2 * async) - 1000000)))
  390. return 0;
  391. else
  392. return 1;
  393. }
  394. int ppc4xx_pci_sync_clock_config(u32 async)
  395. {
  396. sys_info_t sys_info;
  397. u32 sync;
  398. int div;
  399. u32 reg;
  400. u32 spcid_val[] = {
  401. CPR0_SPCID_SPCIDV0_DIV1, CPR0_SPCID_SPCIDV0_DIV2,
  402. CPR0_SPCID_SPCIDV0_DIV3, CPR0_SPCID_SPCIDV0_DIV4 };
  403. get_sys_info(&sys_info);
  404. sync = sys_info.freqPCI;
  405. /*
  406. * First check if the equation above is met
  407. */
  408. if (!ppc4xx_pci_sync_clock_ok(sync, async)) {
  409. /*
  410. * Reconfigure PCI sync clock to meet the equation.
  411. * Start with highest possible PCI sync frequency
  412. * (divider 1).
  413. */
  414. for (div = 1; div <= 4; div++) {
  415. sync = sys_info.freqPLB / div;
  416. if (ppc4xx_pci_sync_clock_ok(sync, async))
  417. break;
  418. }
  419. if (div <= 4) {
  420. mtcpr(CPR0_SPCID, spcid_val[div]);
  421. mfcpr(CPR0_ICFG, reg);
  422. reg |= CPR0_ICFG_RLI_MASK;
  423. mtcpr(CPR0_ICFG, reg);
  424. /* do chip reset */
  425. mtspr(SPRN_DBCR0, 0x20000000);
  426. } else {
  427. /* Impossible to configure the PCI sync clock */
  428. return -1;
  429. }
  430. }
  431. return 0;
  432. }
  433. #endif