cpu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. /*
  65. * Some boards use sockets so different CPUs can be used.
  66. * We have to check chip version in run time.
  67. */
  68. switch (k) {
  69. case 0x00020001: pre = 'P'; suf = ""; break;
  70. case 0x00030001: suf = ""; break;
  71. case 0x00120003: suf = "A"; break;
  72. case 0x00130003: suf = "A3"; break;
  73. case 0x00200004: suf = "B"; break;
  74. case 0x00300004: suf = "C"; break;
  75. case 0x00310004: suf = "C1"; m = 1; break;
  76. case 0x00200064: mid = "SR"; suf = "B"; break;
  77. case 0x00300065: mid = "SR"; suf = "C"; break;
  78. case 0x00310065: mid = "SR"; suf = "C1"; m = 1; break;
  79. case 0x05010000: suf = "D3"; m = 1; break;
  80. case 0x05020000: suf = "D4"; m = 1; break;
  81. /* this value is not documented anywhere */
  82. case 0x40000000: pre = 'P'; suf = "D"; m = 1; break;
  83. /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
  84. case 0x08000003: pre = 'M'; suf = ""; m = 1;
  85. if (id_str == NULL)
  86. id_str =
  87. # if defined(CONFIG_MPC852T)
  88. "PC852T";
  89. # elif defined(CONFIG_MPC859T)
  90. "PC859T";
  91. # elif defined(CONFIG_MPC859DSL)
  92. "PC859DSL";
  93. # elif defined(CONFIG_MPC866T)
  94. "PC866T";
  95. # else
  96. "PC866x"; /* Unknown chip from MPC866 family */
  97. # endif
  98. break;
  99. case 0x09000000: pre = 'M'; mid = suf = ""; m = 1;
  100. if (id_str == NULL)
  101. id_str = "PC885"; /* 870/875/880/885 */
  102. break;
  103. default: suf = NULL; break;
  104. }
  105. if (id_str == NULL)
  106. id_str = "PC86x"; /* Unknown 86x chip */
  107. if (suf)
  108. printf ("%c%s%sZPnn%s", pre, id_str, mid, suf);
  109. else
  110. printf ("unknown M%s (0x%08x)", id_str, k);
  111. #if defined(CFG_8xx_CPUCLK_MIN) && defined(CFG_8xx_CPUCLK_MAX)
  112. printf (" at %s MHz [%d.%d...%d.%d MHz]\n ",
  113. strmhz (buf, clock),
  114. CFG_8xx_CPUCLK_MIN / 1000000,
  115. ((CFG_8xx_CPUCLK_MIN % 1000000) + 50000) / 100000,
  116. CFG_8xx_CPUCLK_MAX / 1000000,
  117. ((CFG_8xx_CPUCLK_MAX % 1000000) + 50000) / 100000
  118. );
  119. #else
  120. printf (" at %s MHz: ", strmhz (buf, clock));
  121. #endif
  122. printf ("%u kB I-Cache %u kB D-Cache",
  123. checkicache () >> 10,
  124. checkdcache () >> 10
  125. );
  126. /* do we have a FEC (860T/P or 852/859/866/885)? */
  127. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  128. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  129. printf (" FEC present");
  130. }
  131. if (!m) {
  132. puts (cpu_warning);
  133. }
  134. putc ('\n');
  135. #ifdef DEBUG
  136. if(clock != measure_gclk()) {
  137. printf ("clock %ldHz != %dHz\n", clock, measure_gclk());
  138. }
  139. #endif
  140. return 0;
  141. }
  142. #elif defined(CONFIG_MPC862)
  143. static int check_CPU (long clock, uint pvr, uint immr)
  144. {
  145. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  146. uint k, m;
  147. char buf[32];
  148. char pre = 'X';
  149. char *mid = "xx";
  150. char *suf;
  151. /* the highest 16 bits should be 0x0050 for a 8xx */
  152. if ((pvr >> 16) != 0x0050)
  153. return -1;
  154. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  155. m = 0;
  156. switch (k) {
  157. /* this value is not documented anywhere */
  158. case 0x06000000: mid = "P"; suf = "0"; break;
  159. case 0x06010001: mid = "P"; suf = "A"; m = 1; break;
  160. case 0x07000003: mid = "P"; suf = "B"; m = 1; break;
  161. default: suf = NULL; break;
  162. }
  163. #ifndef CONFIG_MPC857
  164. if (suf)
  165. printf ("%cPC862%sZPnn%s", pre, mid, suf);
  166. else
  167. printf ("unknown MPC862 (0x%08x)", k);
  168. #else
  169. if (suf)
  170. printf ("%cPC857TZPnn%s", pre, suf); /* only 857T tested right now! */
  171. else
  172. printf ("unknown MPC857 (0x%08x)", k);
  173. #endif
  174. printf (" at %s MHz:", strmhz (buf, clock));
  175. printf (" %u kB I-Cache", checkicache () >> 10);
  176. printf (" %u kB D-Cache", checkdcache () >> 10);
  177. /* lets check and see if we're running on a 862T (or P?) */
  178. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  179. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  180. printf (" FEC present");
  181. }
  182. if (!m) {
  183. puts (cpu_warning);
  184. }
  185. putc ('\n');
  186. return 0;
  187. }
  188. #elif defined(CONFIG_MPC823)
  189. static int check_CPU (long clock, uint pvr, uint immr)
  190. {
  191. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  192. uint k, m;
  193. char buf[32];
  194. char *suf;
  195. /* the highest 16 bits should be 0x0050 for a 8xx */
  196. if ((pvr >> 16) != 0x0050)
  197. return -1;
  198. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  199. m = 0;
  200. switch (k) {
  201. /* MPC823 */
  202. case 0x20000000: suf = "0"; break;
  203. case 0x20010000: suf = "0.1"; break;
  204. case 0x20020000: suf = "Z2/3"; break;
  205. case 0x20020001: suf = "Z3"; break;
  206. case 0x21000000: suf = "A"; break;
  207. case 0x21010000: suf = "B"; m = 1; break;
  208. case 0x21010001: suf = "B2"; m = 1; break;
  209. /* MPC823E */
  210. case 0x24010000: suf = NULL;
  211. puts ("PPC823EZTnnB2");
  212. m = 1;
  213. break;
  214. default:
  215. suf = NULL;
  216. printf ("unknown MPC823 (0x%08x)", k);
  217. break;
  218. }
  219. if (suf)
  220. printf ("PPC823ZTnn%s", suf);
  221. printf (" at %s MHz:", strmhz (buf, clock));
  222. printf (" %u kB I-Cache", checkicache () >> 10);
  223. printf (" %u kB D-Cache", checkdcache () >> 10);
  224. /* lets check and see if we're running on a 860T (or P?) */
  225. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  226. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  227. puts (" FEC present");
  228. }
  229. if (!m) {
  230. puts (cpu_warning);
  231. }
  232. putc ('\n');
  233. return 0;
  234. }
  235. #elif defined(CONFIG_MPC850)
  236. static int check_CPU (long clock, uint pvr, uint immr)
  237. {
  238. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  239. uint k, m;
  240. char buf[32];
  241. /* the highest 16 bits should be 0x0050 for a 8xx */
  242. if ((pvr >> 16) != 0x0050)
  243. return -1;
  244. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  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. printf (" %u kB I-Cache", checkicache () >> 10);
  266. printf (" %u kB D-Cache", checkdcache () >> 10);
  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. DECLARE_GLOBAL_DATA_PTR;
  285. ulong clock = gd->cpu_clk;
  286. uint immr = get_immr (0); /* Return full IMMR contents */
  287. uint pvr = get_pvr ();
  288. puts ("CPU: ");
  289. /* 850 has PARTNUM 20 */
  290. /* 801 has PARTNUM 10 */
  291. return check_CPU (clock, pvr, immr);
  292. }
  293. /* ------------------------------------------------------------------------- */
  294. /* L1 i-cache */
  295. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  296. /* the 860 P (plus) has 256 sets of 16 bytes in 4 ways (= 16 kB) */
  297. int checkicache (void)
  298. {
  299. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  300. volatile memctl8xx_t *memctl = &immap->im_memctl;
  301. u32 cacheon = rd_ic_cst () & IDC_ENABLED;
  302. #ifdef CONFIG_IP86x
  303. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  304. #else
  305. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  306. #endif
  307. u32 m;
  308. u32 lines = -1;
  309. wr_ic_cst (IDC_UNALL);
  310. wr_ic_cst (IDC_INVALL);
  311. wr_ic_cst (IDC_DISABLE);
  312. __asm__ volatile ("isync");
  313. while (!((m = rd_ic_cst ()) & IDC_CERR2)) {
  314. wr_ic_adr (k);
  315. wr_ic_cst (IDC_LDLCK);
  316. __asm__ volatile ("isync");
  317. lines++;
  318. k += 0x10; /* the number of bytes in a cacheline */
  319. }
  320. wr_ic_cst (IDC_UNALL);
  321. wr_ic_cst (IDC_INVALL);
  322. if (cacheon)
  323. wr_ic_cst (IDC_ENABLE);
  324. else
  325. wr_ic_cst (IDC_DISABLE);
  326. __asm__ volatile ("isync");
  327. return lines << 4;
  328. };
  329. /* ------------------------------------------------------------------------- */
  330. /* L1 d-cache */
  331. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  332. /* the 860 P (plus) has 256 sets of 16 bytes in 2 ways (= 8 kB) */
  333. /* call with cache disabled */
  334. int checkdcache (void)
  335. {
  336. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  337. volatile memctl8xx_t *memctl = &immap->im_memctl;
  338. u32 cacheon = rd_dc_cst () & IDC_ENABLED;
  339. #ifdef CONFIG_IP86x
  340. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  341. #else
  342. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  343. #endif
  344. u32 m;
  345. u32 lines = -1;
  346. wr_dc_cst (IDC_UNALL);
  347. wr_dc_cst (IDC_INVALL);
  348. wr_dc_cst (IDC_DISABLE);
  349. while (!((m = rd_dc_cst ()) & IDC_CERR2)) {
  350. wr_dc_adr (k);
  351. wr_dc_cst (IDC_LDLCK);
  352. lines++;
  353. k += 0x10; /* the number of bytes in a cacheline */
  354. }
  355. wr_dc_cst (IDC_UNALL);
  356. wr_dc_cst (IDC_INVALL);
  357. if (cacheon)
  358. wr_dc_cst (IDC_ENABLE);
  359. else
  360. wr_dc_cst (IDC_DISABLE);
  361. return lines << 4;
  362. };
  363. /* ------------------------------------------------------------------------- */
  364. void upmconfig (uint upm, uint * table, uint size)
  365. {
  366. uint i;
  367. uint addr = 0;
  368. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  369. volatile memctl8xx_t *memctl = &immap->im_memctl;
  370. for (i = 0; i < size; i++) {
  371. memctl->memc_mdr = table[i]; /* (16-15) */
  372. memctl->memc_mcr = addr | upm; /* (16-16) */
  373. addr++;
  374. }
  375. }
  376. /* ------------------------------------------------------------------------- */
  377. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  378. {
  379. ulong msr, addr;
  380. volatile immap_t *immap = (immap_t *) CFG_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 CFG_RESET_ADDRESS
  392. addr = CFG_RESET_ADDRESS;
  393. #else
  394. /*
  395. * note: when CFG_MONITOR_BASE points to a RAM address, CFG_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 CFG_RESET_ADDRESS.
  398. * "(ulong)-1" used to be a good choice for many systems...
  399. */
  400. addr = CFG_MONITOR_BASE - sizeof (ulong);
  401. #endif
  402. ((void (*)(void)) addr) ();
  403. return 1;
  404. }
  405. /* ------------------------------------------------------------------------- */
  406. /*
  407. * Get timebase clock frequency (like cpu_clk in Hz)
  408. *
  409. * See sections 14.2 and 14.6 of the User's Manual
  410. */
  411. unsigned long get_tbclk (void)
  412. {
  413. DECLARE_GLOBAL_DATA_PTR;
  414. uint immr = get_immr (0); /* Return full IMMR contents */
  415. volatile immap_t *immap = (volatile immap_t *)(immr & 0xFFFF0000);
  416. ulong oscclk, factor, pll;
  417. if (immap->im_clkrst.car_sccr & SCCR_TBS) {
  418. return (gd->cpu_clk / 16);
  419. }
  420. pll = immap->im_clkrst.car_plprcr;
  421. #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
  422. /*
  423. * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
  424. * factor is calculated as follows:
  425. *
  426. * MFN
  427. * MFI + -------
  428. * MFD + 1
  429. * factor = -----------------
  430. * (PDF + 1) * 2^S
  431. *
  432. * For older chips, it's just MF field of PLPRCR plus one.
  433. */
  434. if ((immr & 0x0FFF) >= MPC8xx_NEW_CLK) { /* MPC866/87x/88x series */
  435. factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN)/(PLPRCR_val(MFD)+1))/
  436. (PLPRCR_val(PDF)+1) / (1<<PLPRCR_val(S));
  437. } else {
  438. factor = PLPRCR_val(MF)+1;
  439. }
  440. oscclk = gd->cpu_clk / factor;
  441. if ((immap->im_clkrst.car_sccr & SCCR_RTSEL) == 0 || factor > 2) {
  442. return (oscclk / 4);
  443. }
  444. return (oscclk / 16);
  445. }
  446. /* ------------------------------------------------------------------------- */
  447. #if defined(CONFIG_WATCHDOG)
  448. void watchdog_reset (void)
  449. {
  450. int re_enable = disable_interrupts ();
  451. reset_8xx_watchdog ((immap_t *) CFG_IMMR);
  452. if (re_enable)
  453. enable_interrupts ();
  454. }
  455. void reset_8xx_watchdog (volatile immap_t * immr)
  456. {
  457. # if defined(CONFIG_LWMON)
  458. /*
  459. * The LWMON board uses a MAX6301 Watchdog
  460. * with the trigger pin connected to port PA.7
  461. *
  462. * (The old board version used a MAX706TESA Watchdog, which
  463. * had to be handled exactly the same.)
  464. */
  465. # define WATCHDOG_BIT 0x0100
  466. immr->im_ioport.iop_papar &= ~(WATCHDOG_BIT); /* GPIO */
  467. immr->im_ioport.iop_padir |= WATCHDOG_BIT; /* Output */
  468. immr->im_ioport.iop_paodr &= ~(WATCHDOG_BIT); /* active output */
  469. immr->im_ioport.iop_padat ^= WATCHDOG_BIT; /* Toggle WDI */
  470. # elif defined(CONFIG_KUP4K) || defined(CONFIG_KUP4X)
  471. /*
  472. * The KUP4 boards uses a TPS3705 Watchdog
  473. * with the trigger pin connected to port PA.5
  474. */
  475. # define WATCHDOG_BIT 0x0400
  476. immr->im_ioport.iop_papar &= ~(WATCHDOG_BIT); /* GPIO */
  477. immr->im_ioport.iop_padir |= WATCHDOG_BIT; /* Output */
  478. immr->im_ioport.iop_paodr &= ~(WATCHDOG_BIT); /* active output */
  479. immr->im_ioport.iop_padat ^= WATCHDOG_BIT; /* Toggle WDI */
  480. # else
  481. /*
  482. * All other boards use the MPC8xx Internal Watchdog
  483. */
  484. immr->im_siu_conf.sc_swsr = 0x556c; /* write magic1 */
  485. immr->im_siu_conf.sc_swsr = 0xaa39; /* write magic2 */
  486. # endif /* CONFIG_LWMON */
  487. }
  488. #endif /* CONFIG_WATCHDOG */
  489. /* ------------------------------------------------------------------------- */