cpu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * m8xx.c
  9. *
  10. * CPU specific code
  11. *
  12. * written or collected and sometimes rewritten by
  13. * Magnus Damm <damm@bitsmart.com>
  14. *
  15. * minor modifications by
  16. * Wolfgang Denk <wd@denx.de>
  17. */
  18. #include <common.h>
  19. #include <watchdog.h>
  20. #include <command.h>
  21. #include <mpc8xx.h>
  22. #include <commproc.h>
  23. #include <netdev.h>
  24. #include <asm/cache.h>
  25. #include <linux/compiler.h>
  26. #include <asm/io.h>
  27. #if defined(CONFIG_OF_LIBFDT)
  28. #include <libfdt.h>
  29. #include <fdt_support.h>
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. static char *cpu_warning = "\n " \
  33. "*** Warning: CPU Core has Silicon Bugs -- Check the Errata ***";
  34. #if ((defined(CONFIG_MPC86x) || defined(CONFIG_MPC855)) && \
  35. !defined(CONFIG_MPC862))
  36. static int check_CPU (long clock, uint pvr, uint immr)
  37. {
  38. char *id_str =
  39. # if defined(CONFIG_MPC855)
  40. "PC855";
  41. # elif defined(CONFIG_MPC860P)
  42. "PC860P";
  43. # else
  44. NULL;
  45. # endif
  46. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  47. uint k, m;
  48. char buf[32];
  49. char pre = 'X';
  50. char *mid = "xx";
  51. char *suf;
  52. /* the highest 16 bits should be 0x0050 for a 860 */
  53. if ((pvr >> 16) != 0x0050)
  54. return -1;
  55. k = (immr << 16) |
  56. immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)];
  57. m = 0;
  58. suf = "";
  59. /*
  60. * Some boards use sockets so different CPUs can be used.
  61. * We have to check chip version in run time.
  62. */
  63. switch (k) {
  64. case 0x00020001: pre = 'P'; break;
  65. case 0x00030001: break;
  66. case 0x00120003: suf = "A"; break;
  67. case 0x00130003: suf = "A3"; break;
  68. case 0x00200004: suf = "B"; break;
  69. case 0x00300004: suf = "C"; break;
  70. case 0x00310004: suf = "C1"; m = 1; break;
  71. case 0x00200064: mid = "SR"; suf = "B"; break;
  72. case 0x00300065: mid = "SR"; suf = "C"; break;
  73. case 0x00310065: mid = "SR"; suf = "C1"; m = 1; break;
  74. case 0x05010000: suf = "D3"; m = 1; break;
  75. case 0x05020000: suf = "D4"; m = 1; break;
  76. /* this value is not documented anywhere */
  77. case 0x40000000: pre = 'P'; suf = "D"; m = 1; break;
  78. /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
  79. case 0x08010004: /* Rev. A.0 */
  80. suf = "A";
  81. /* fall through */
  82. case 0x08000003: /* Rev. 0.3 */
  83. pre = 'M'; m = 1;
  84. if (id_str == NULL)
  85. id_str =
  86. # if defined(CONFIG_MPC852T)
  87. "PC852T";
  88. # elif defined(CONFIG_MPC859T)
  89. "PC859T";
  90. # elif defined(CONFIG_MPC859DSL)
  91. "PC859DSL";
  92. # elif defined(CONFIG_MPC866T)
  93. "PC866T";
  94. # else
  95. "PC866x"; /* Unknown chip from MPC866 family */
  96. # endif
  97. break;
  98. case 0x09000000: pre = 'M'; mid = suf = ""; m = 1;
  99. if (id_str == NULL)
  100. id_str = "PC885"; /* 870/875/880/885 */
  101. break;
  102. default: suf = NULL; break;
  103. }
  104. if (id_str == NULL)
  105. id_str = "PC86x"; /* Unknown 86x chip */
  106. if (suf)
  107. printf ("%c%s%sZPnn%s", pre, id_str, mid, suf);
  108. else
  109. printf ("unknown M%s (0x%08x)", id_str, k);
  110. #if defined(CONFIG_SYS_8xx_CPUCLK_MIN) && defined(CONFIG_SYS_8xx_CPUCLK_MAX)
  111. printf (" at %s MHz [%d.%d...%d.%d MHz]\n ",
  112. strmhz (buf, clock),
  113. CONFIG_SYS_8xx_CPUCLK_MIN / 1000000,
  114. ((CONFIG_SYS_8xx_CPUCLK_MIN % 1000000) + 50000) / 100000,
  115. CONFIG_SYS_8xx_CPUCLK_MAX / 1000000,
  116. ((CONFIG_SYS_8xx_CPUCLK_MAX % 1000000) + 50000) / 100000
  117. );
  118. #else
  119. printf (" at %s MHz: ", strmhz (buf, clock));
  120. #endif
  121. print_size(checkicache(), " I-Cache ");
  122. print_size(checkdcache(), " D-Cache");
  123. /* do we have a FEC (860T/P or 852/859/866/885)? */
  124. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  125. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  126. printf (" FEC present");
  127. }
  128. if (!m) {
  129. puts (cpu_warning);
  130. }
  131. putc ('\n');
  132. #ifdef DEBUG
  133. if(clock != measure_gclk()) {
  134. printf ("clock %ldHz != %dHz\n", clock, measure_gclk());
  135. }
  136. #endif
  137. return 0;
  138. }
  139. #elif defined(CONFIG_MPC862)
  140. static int check_CPU (long clock, uint pvr, uint immr)
  141. {
  142. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  143. uint k, m;
  144. char buf[32];
  145. char pre = 'X';
  146. __maybe_unused char *mid = "xx";
  147. char *suf;
  148. /* the highest 16 bits should be 0x0050 for a 8xx */
  149. if ((pvr >> 16) != 0x0050)
  150. return -1;
  151. k = (immr << 16) |
  152. immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)];
  153. m = 0;
  154. switch (k) {
  155. /* this value is not documented anywhere */
  156. case 0x06000000: mid = "P"; suf = "0"; break;
  157. case 0x06010001: mid = "P"; suf = "A"; m = 1; break;
  158. case 0x07000003: mid = "P"; suf = "B"; m = 1; break;
  159. default: suf = NULL; break;
  160. }
  161. #ifndef CONFIG_MPC857
  162. if (suf)
  163. printf ("%cPC862%sZPnn%s", pre, mid, suf);
  164. else
  165. printf ("unknown MPC862 (0x%08x)", k);
  166. #else
  167. if (suf)
  168. printf ("%cPC857TZPnn%s", pre, suf); /* only 857T tested right now! */
  169. else
  170. printf ("unknown MPC857 (0x%08x)", k);
  171. #endif
  172. printf(" at %s MHz: ", strmhz(buf, clock));
  173. print_size(checkicache(), " I-Cache ");
  174. print_size(checkdcache(), " D-Cache");
  175. /* lets check and see if we're running on a 862T (or P?) */
  176. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  177. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  178. printf (" FEC present");
  179. }
  180. if (!m) {
  181. puts (cpu_warning);
  182. }
  183. putc ('\n');
  184. return 0;
  185. }
  186. #elif defined(CONFIG_MPC823)
  187. static int check_CPU (long clock, uint pvr, uint immr)
  188. {
  189. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  190. uint k, m;
  191. char buf[32];
  192. char *suf;
  193. /* the highest 16 bits should be 0x0050 for a 8xx */
  194. if ((pvr >> 16) != 0x0050)
  195. return -1;
  196. k = (immr << 16) |
  197. in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
  198. m = 0;
  199. switch (k) {
  200. /* MPC823 */
  201. case 0x20000000: suf = "0"; break;
  202. case 0x20010000: suf = "0.1"; break;
  203. case 0x20020000: suf = "Z2/3"; break;
  204. case 0x20020001: suf = "Z3"; break;
  205. case 0x21000000: suf = "A"; break;
  206. case 0x21010000: suf = "B"; m = 1; break;
  207. case 0x21010001: suf = "B2"; m = 1; break;
  208. /* MPC823E */
  209. case 0x24010000: suf = NULL;
  210. puts ("PPC823EZTnnB2");
  211. m = 1;
  212. break;
  213. default:
  214. suf = NULL;
  215. printf ("unknown MPC823 (0x%08x)", k);
  216. break;
  217. }
  218. if (suf)
  219. printf ("PPC823ZTnn%s", suf);
  220. printf(" at %s MHz: ", strmhz(buf, clock));
  221. print_size(checkicache(), " I-Cache ");
  222. print_size(checkdcache(), " D-Cache");
  223. /* lets check and see if we're running on a 860T (or P?) */
  224. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  225. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  226. puts (" FEC present");
  227. }
  228. if (!m) {
  229. puts (cpu_warning);
  230. }
  231. putc ('\n');
  232. return 0;
  233. }
  234. #elif defined(CONFIG_MPC850)
  235. static int check_CPU (long clock, uint pvr, uint immr)
  236. {
  237. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  238. uint k, m;
  239. char buf[32];
  240. /* the highest 16 bits should be 0x0050 for a 8xx */
  241. if ((pvr >> 16) != 0x0050)
  242. return -1;
  243. k = (immr << 16) |
  244. immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)];
  245. m = 0;
  246. switch (k) {
  247. case 0x20020001:
  248. printf ("XPC850xxZT");
  249. break;
  250. case 0x21000065:
  251. printf ("XPC850xxZTA");
  252. break;
  253. case 0x21010067:
  254. printf ("XPC850xxZTB");
  255. m = 1;
  256. break;
  257. case 0x21020068:
  258. printf ("XPC850xxZTC");
  259. m = 1;
  260. break;
  261. default:
  262. printf ("unknown MPC850 (0x%08x)", k);
  263. }
  264. printf(" at %s MHz: ", strmhz(buf, clock));
  265. print_size(checkicache(), " I-Cache ");
  266. print_size(checkdcache(), " D-Cache");
  267. /* lets check and see if we're running on a 850T (or P?) */
  268. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  269. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  270. printf (" FEC present");
  271. }
  272. if (!m) {
  273. puts (cpu_warning);
  274. }
  275. putc ('\n');
  276. return 0;
  277. }
  278. #else
  279. #error CPU undefined
  280. #endif
  281. /* ------------------------------------------------------------------------- */
  282. int checkcpu (void)
  283. {
  284. ulong clock = gd->cpu_clk;
  285. uint immr = get_immr (0); /* Return full IMMR contents */
  286. uint pvr = get_pvr ();
  287. puts ("CPU: ");
  288. /* 850 has PARTNUM 20 */
  289. /* 801 has PARTNUM 10 */
  290. return check_CPU (clock, pvr, immr);
  291. }
  292. /* ------------------------------------------------------------------------- */
  293. /* L1 i-cache */
  294. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  295. /* the 860 P (plus) has 256 sets of 16 bytes in 4 ways (= 16 kB) */
  296. int checkicache (void)
  297. {
  298. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  299. volatile memctl8xx_t *memctl = &immap->im_memctl;
  300. u32 cacheon = rd_ic_cst () & IDC_ENABLED;
  301. #ifdef CONFIG_IP86x
  302. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  303. #else
  304. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  305. #endif
  306. u32 m;
  307. u32 lines = -1;
  308. wr_ic_cst (IDC_UNALL);
  309. wr_ic_cst (IDC_INVALL);
  310. wr_ic_cst (IDC_DISABLE);
  311. __asm__ volatile ("isync");
  312. while (!((m = rd_ic_cst ()) & IDC_CERR2)) {
  313. wr_ic_adr (k);
  314. wr_ic_cst (IDC_LDLCK);
  315. __asm__ volatile ("isync");
  316. lines++;
  317. k += 0x10; /* the number of bytes in a cacheline */
  318. }
  319. wr_ic_cst (IDC_UNALL);
  320. wr_ic_cst (IDC_INVALL);
  321. if (cacheon)
  322. wr_ic_cst (IDC_ENABLE);
  323. else
  324. wr_ic_cst (IDC_DISABLE);
  325. __asm__ volatile ("isync");
  326. return lines << 4;
  327. };
  328. /* ------------------------------------------------------------------------- */
  329. /* L1 d-cache */
  330. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  331. /* the 860 P (plus) has 256 sets of 16 bytes in 2 ways (= 8 kB) */
  332. /* call with cache disabled */
  333. int checkdcache (void)
  334. {
  335. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  336. volatile memctl8xx_t *memctl = &immap->im_memctl;
  337. u32 cacheon = rd_dc_cst () & IDC_ENABLED;
  338. #ifdef CONFIG_IP86x
  339. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  340. #else
  341. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  342. #endif
  343. u32 m;
  344. u32 lines = -1;
  345. wr_dc_cst (IDC_UNALL);
  346. wr_dc_cst (IDC_INVALL);
  347. wr_dc_cst (IDC_DISABLE);
  348. while (!((m = rd_dc_cst ()) & IDC_CERR2)) {
  349. wr_dc_adr (k);
  350. wr_dc_cst (IDC_LDLCK);
  351. lines++;
  352. k += 0x10; /* the number of bytes in a cacheline */
  353. }
  354. wr_dc_cst (IDC_UNALL);
  355. wr_dc_cst (IDC_INVALL);
  356. if (cacheon)
  357. wr_dc_cst (IDC_ENABLE);
  358. else
  359. wr_dc_cst (IDC_DISABLE);
  360. return lines << 4;
  361. };
  362. /* ------------------------------------------------------------------------- */
  363. void upmconfig (uint upm, uint * table, uint size)
  364. {
  365. uint i;
  366. uint addr = 0;
  367. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  368. volatile memctl8xx_t *memctl = &immap->im_memctl;
  369. for (i = 0; i < size; i++) {
  370. memctl->memc_mdr = table[i]; /* (16-15) */
  371. memctl->memc_mcr = addr | upm; /* (16-16) */
  372. addr++;
  373. }
  374. }
  375. /* ------------------------------------------------------------------------- */
  376. #ifndef CONFIG_LWMON
  377. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  378. {
  379. ulong msr, addr;
  380. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  381. immap->im_clkrst.car_plprcr |= PLPRCR_CSR; /* Checkstop Reset enable */
  382. /* Interrupts and MMU off */
  383. __asm__ volatile ("mtspr 81, 0");
  384. __asm__ volatile ("mfmsr %0":"=r" (msr));
  385. msr &= ~0x1030;
  386. __asm__ volatile ("mtmsr %0"::"r" (msr));
  387. /*
  388. * Trying to execute the next instruction at a non-existing address
  389. * should cause a machine check, resulting in reset
  390. */
  391. #ifdef CONFIG_SYS_RESET_ADDRESS
  392. addr = CONFIG_SYS_RESET_ADDRESS;
  393. #else
  394. /*
  395. * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address, CONFIG_SYS_MONITOR_BASE
  396. * - sizeof (ulong) is usually a valid address. Better pick an address
  397. * known to be invalid on your system and assign it to CONFIG_SYS_RESET_ADDRESS.
  398. * "(ulong)-1" used to be a good choice for many systems...
  399. */
  400. addr = CONFIG_SYS_MONITOR_BASE - sizeof (ulong);
  401. #endif
  402. ((void (*)(void)) addr) ();
  403. return 1;
  404. }
  405. #else /* CONFIG_LWMON */
  406. /*
  407. * On the LWMON board, the MCLR reset input of the PIC's on the board
  408. * uses a 47K/1n RC combination which has a 47us time constant. The
  409. * low signal on the HRESET pin of the CPU is only 512 clocks = 8 us
  410. * and thus too short to reset the external hardware. So we use the
  411. * watchdog to reset the board.
  412. */
  413. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  414. {
  415. /* prevent triggering the watchdog */
  416. disable_interrupts ();
  417. /* make sure the watchdog is running */
  418. reset_8xx_watchdog ((immap_t *) CONFIG_SYS_IMMR);
  419. /* wait for watchdog reset */
  420. while (1) {};
  421. /* NOTREACHED */
  422. return 1;
  423. }
  424. #endif /* CONFIG_LWMON */
  425. /* ------------------------------------------------------------------------- */
  426. /*
  427. * Get timebase clock frequency (like cpu_clk in Hz)
  428. *
  429. * See sections 14.2 and 14.6 of the User's Manual
  430. */
  431. unsigned long get_tbclk (void)
  432. {
  433. uint immr = get_immr (0); /* Return full IMMR contents */
  434. volatile immap_t *immap = (volatile immap_t *)(immr & 0xFFFF0000);
  435. ulong oscclk, factor, pll;
  436. if (immap->im_clkrst.car_sccr & SCCR_TBS) {
  437. return (gd->cpu_clk / 16);
  438. }
  439. pll = immap->im_clkrst.car_plprcr;
  440. #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
  441. /*
  442. * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
  443. * factor is calculated as follows:
  444. *
  445. * MFN
  446. * MFI + -------
  447. * MFD + 1
  448. * factor = -----------------
  449. * (PDF + 1) * 2^S
  450. *
  451. * For older chips, it's just MF field of PLPRCR plus one.
  452. */
  453. if ((immr & 0x0FFF) >= MPC8xx_NEW_CLK) { /* MPC866/87x/88x series */
  454. factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN)/(PLPRCR_val(MFD)+1))/
  455. (PLPRCR_val(PDF)+1) / (1<<PLPRCR_val(S));
  456. } else {
  457. factor = PLPRCR_val(MF)+1;
  458. }
  459. oscclk = gd->cpu_clk / factor;
  460. if ((immap->im_clkrst.car_sccr & SCCR_RTSEL) == 0 || factor > 2) {
  461. return (oscclk / 4);
  462. }
  463. return (oscclk / 16);
  464. }
  465. /* ------------------------------------------------------------------------- */
  466. #if defined(CONFIG_WATCHDOG)
  467. void watchdog_reset (void)
  468. {
  469. int re_enable = disable_interrupts ();
  470. reset_8xx_watchdog ((immap_t *) CONFIG_SYS_IMMR);
  471. if (re_enable)
  472. enable_interrupts ();
  473. }
  474. #endif /* CONFIG_WATCHDOG */
  475. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_LWMON)
  476. void reset_8xx_watchdog (volatile immap_t * immr)
  477. {
  478. # if defined(CONFIG_LWMON)
  479. /*
  480. * The LWMON board uses a MAX6301 Watchdog
  481. * with the trigger pin connected to port PA.7
  482. *
  483. * (The old board version used a MAX706TESA Watchdog, which
  484. * had to be handled exactly the same.)
  485. */
  486. # define WATCHDOG_BIT 0x0100
  487. immr->im_ioport.iop_papar &= ~(WATCHDOG_BIT); /* GPIO */
  488. immr->im_ioport.iop_padir |= WATCHDOG_BIT; /* Output */
  489. immr->im_ioport.iop_paodr &= ~(WATCHDOG_BIT); /* active output */
  490. immr->im_ioport.iop_padat ^= WATCHDOG_BIT; /* Toggle WDI */
  491. # elif defined(CONFIG_KUP4K) || defined(CONFIG_KUP4X)
  492. /*
  493. * The KUP4 boards uses a TPS3705 Watchdog
  494. * with the trigger pin connected to port PA.5
  495. */
  496. # define WATCHDOG_BIT 0x0400
  497. immr->im_ioport.iop_papar &= ~(WATCHDOG_BIT); /* GPIO */
  498. immr->im_ioport.iop_padir |= WATCHDOG_BIT; /* Output */
  499. immr->im_ioport.iop_paodr &= ~(WATCHDOG_BIT); /* active output */
  500. immr->im_ioport.iop_padat ^= WATCHDOG_BIT; /* Toggle WDI */
  501. # else
  502. /*
  503. * All other boards use the MPC8xx Internal Watchdog
  504. */
  505. immr->im_siu_conf.sc_swsr = 0x556c; /* write magic1 */
  506. immr->im_siu_conf.sc_swsr = 0xaa39; /* write magic2 */
  507. # endif /* CONFIG_LWMON */
  508. }
  509. #endif /* CONFIG_WATCHDOG */
  510. /*
  511. * Initializes on-chip ethernet controllers.
  512. * to override, implement board_eth_init()
  513. */
  514. int cpu_eth_init(bd_t *bis)
  515. {
  516. #if defined(SCC_ENET) && defined(CONFIG_CMD_NET)
  517. scc_initialize(bis);
  518. #endif
  519. #if defined(FEC_ENET)
  520. fec_initialize(bis);
  521. #endif
  522. return 0;
  523. }