cpu.c 15 KB

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