cpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. printf (" at %s MHz:", strmhz (buf, clock));
  112. printf (" %u kB I-Cache", checkicache () >> 10);
  113. printf (" %u kB D-Cache", checkdcache () >> 10);
  114. /* do we have a FEC (860T/P or 852/859/866)? */
  115. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  116. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  117. printf (" FEC present");
  118. }
  119. if (!m) {
  120. puts (cpu_warning);
  121. }
  122. putc ('\n');
  123. #ifdef DEBUG
  124. if(clock != measure_gclk()) {
  125. printf ("clock %ldHz != %dHz\n", clock, measure_gclk());
  126. }
  127. #endif
  128. return 0;
  129. }
  130. #elif defined(CONFIG_MPC862)
  131. static int check_CPU (long clock, uint pvr, uint immr)
  132. {
  133. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  134. uint k, m;
  135. char buf[32];
  136. char pre = 'X';
  137. char *mid = "xx";
  138. char *suf;
  139. /* the highest 16 bits should be 0x0050 for a 8xx */
  140. if ((pvr >> 16) != 0x0050)
  141. return -1;
  142. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  143. m = 0;
  144. switch (k) {
  145. /* this value is not documented anywhere */
  146. case 0x06000000: mid = "P"; suf = "0"; break;
  147. case 0x06010001: mid = "P"; suf = "A"; m = 1; break;
  148. case 0x07000003: mid = "P"; suf = "B"; m = 1; break;
  149. default: suf = NULL; break;
  150. }
  151. if (suf)
  152. printf ("%cPC862%sZPnn%s", pre, mid, suf);
  153. else
  154. printf ("unknown MPC862 (0x%08x)", k);
  155. printf (" at %s MHz:", strmhz (buf, clock));
  156. printf (" %u kB I-Cache", checkicache () >> 10);
  157. printf (" %u kB D-Cache", checkdcache () >> 10);
  158. /* lets check and see if we're running on a 862T (or P?) */
  159. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  160. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  161. printf (" FEC present");
  162. }
  163. if (!m) {
  164. puts (cpu_warning);
  165. }
  166. putc ('\n');
  167. return 0;
  168. }
  169. #elif defined(CONFIG_MPC823)
  170. static int check_CPU (long clock, uint pvr, uint immr)
  171. {
  172. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  173. uint k, m;
  174. char buf[32];
  175. char *suf;
  176. /* the highest 16 bits should be 0x0050 for a 8xx */
  177. if ((pvr >> 16) != 0x0050)
  178. return -1;
  179. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  180. m = 0;
  181. switch (k) {
  182. /* MPC823 */
  183. case 0x20000000: suf = "0"; break;
  184. case 0x20010000: suf = "0.1"; break;
  185. case 0x20020000: suf = "Z2/3"; break;
  186. case 0x20020001: suf = "Z3"; break;
  187. case 0x21000000: suf = "A"; break;
  188. case 0x21010000: suf = "B"; m = 1; break;
  189. case 0x21010001: suf = "B2"; m = 1; break;
  190. /* MPC823E */
  191. case 0x24010000: suf = NULL;
  192. puts ("PPC823EZTnnB2");
  193. m = 1;
  194. break;
  195. default:
  196. suf = NULL;
  197. printf ("unknown MPC823 (0x%08x)", k);
  198. break;
  199. }
  200. if (suf)
  201. printf ("PPC823ZTnn%s", suf);
  202. printf (" at %s MHz:", strmhz (buf, clock));
  203. printf (" %u kB I-Cache", checkicache () >> 10);
  204. printf (" %u kB D-Cache", checkdcache () >> 10);
  205. /* lets check and see if we're running on a 860T (or P?) */
  206. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  207. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  208. puts (" FEC present");
  209. }
  210. if (!m) {
  211. puts (cpu_warning);
  212. }
  213. putc ('\n');
  214. return 0;
  215. }
  216. #elif defined(CONFIG_MPC850)
  217. static int check_CPU (long clock, uint pvr, uint immr)
  218. {
  219. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  220. uint k, m;
  221. char buf[32];
  222. /* the highest 16 bits should be 0x0050 for a 8xx */
  223. if ((pvr >> 16) != 0x0050)
  224. return -1;
  225. k = (immr << 16) | *((ushort *) & immap->im_cpm.cp_dparam[0xB0]);
  226. m = 0;
  227. switch (k) {
  228. case 0x20020001:
  229. printf ("XPC850xxZT");
  230. break;
  231. case 0x21000065:
  232. printf ("XPC850xxZTA");
  233. break;
  234. case 0x21010067:
  235. printf ("XPC850xxZTB");
  236. m = 1;
  237. break;
  238. case 0x21020068:
  239. printf ("XPC850xxZTC");
  240. m = 1;
  241. break;
  242. default:
  243. printf ("unknown MPC850 (0x%08x)", k);
  244. }
  245. printf (" at %s MHz:", strmhz (buf, clock));
  246. printf (" %u kB I-Cache", checkicache () >> 10);
  247. printf (" %u kB D-Cache", checkdcache () >> 10);
  248. /* lets check and see if we're running on a 850T (or P?) */
  249. immap->im_cpm.cp_fec.fec_addr_low = 0x12345678;
  250. if (immap->im_cpm.cp_fec.fec_addr_low == 0x12345678) {
  251. printf (" FEC present");
  252. }
  253. if (!m) {
  254. puts (cpu_warning);
  255. }
  256. putc ('\n');
  257. return 0;
  258. }
  259. #else
  260. #error CPU undefined
  261. #endif
  262. /* ------------------------------------------------------------------------- */
  263. int checkcpu (void)
  264. {
  265. DECLARE_GLOBAL_DATA_PTR;
  266. ulong clock = gd->cpu_clk;
  267. uint immr = get_immr (0); /* Return full IMMR contents */
  268. uint pvr = get_pvr ();
  269. puts ("CPU: ");
  270. /* 850 has PARTNUM 20 */
  271. /* 801 has PARTNUM 10 */
  272. return check_CPU (clock, pvr, immr);
  273. }
  274. /* ------------------------------------------------------------------------- */
  275. /* L1 i-cache */
  276. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  277. /* the 860 P (plus) has 256 sets of 16 bytes in 4 ways (= 16 kB) */
  278. int checkicache (void)
  279. {
  280. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  281. volatile memctl8xx_t *memctl = &immap->im_memctl;
  282. u32 cacheon = rd_ic_cst () & IDC_ENABLED;
  283. #ifdef CONFIG_IP86x
  284. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  285. #else
  286. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  287. #endif
  288. u32 m;
  289. u32 lines = -1;
  290. wr_ic_cst (IDC_UNALL);
  291. wr_ic_cst (IDC_INVALL);
  292. wr_ic_cst (IDC_DISABLE);
  293. __asm__ volatile ("isync");
  294. while (!((m = rd_ic_cst ()) & IDC_CERR2)) {
  295. wr_ic_adr (k);
  296. wr_ic_cst (IDC_LDLCK);
  297. __asm__ volatile ("isync");
  298. lines++;
  299. k += 0x10; /* the number of bytes in a cacheline */
  300. }
  301. wr_ic_cst (IDC_UNALL);
  302. wr_ic_cst (IDC_INVALL);
  303. if (cacheon)
  304. wr_ic_cst (IDC_ENABLE);
  305. else
  306. wr_ic_cst (IDC_DISABLE);
  307. __asm__ volatile ("isync");
  308. return lines << 4;
  309. };
  310. /* ------------------------------------------------------------------------- */
  311. /* L1 d-cache */
  312. /* the standard 860 has 128 sets of 16 bytes in 2 ways (= 4 kB) */
  313. /* the 860 P (plus) has 256 sets of 16 bytes in 2 ways (= 8 kB) */
  314. /* call with cache disabled */
  315. int checkdcache (void)
  316. {
  317. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  318. volatile memctl8xx_t *memctl = &immap->im_memctl;
  319. u32 cacheon = rd_dc_cst () & IDC_ENABLED;
  320. #ifdef CONFIG_IP86x
  321. u32 k = memctl->memc_br1 & ~0x00007fff; /* probe in flash memoryarea */
  322. #else
  323. u32 k = memctl->memc_br0 & ~0x00007fff; /* probe in flash memoryarea */
  324. #endif
  325. u32 m;
  326. u32 lines = -1;
  327. wr_dc_cst (IDC_UNALL);
  328. wr_dc_cst (IDC_INVALL);
  329. wr_dc_cst (IDC_DISABLE);
  330. while (!((m = rd_dc_cst ()) & IDC_CERR2)) {
  331. wr_dc_adr (k);
  332. wr_dc_cst (IDC_LDLCK);
  333. lines++;
  334. k += 0x10; /* the number of bytes in a cacheline */
  335. }
  336. wr_dc_cst (IDC_UNALL);
  337. wr_dc_cst (IDC_INVALL);
  338. if (cacheon)
  339. wr_dc_cst (IDC_ENABLE);
  340. else
  341. wr_dc_cst (IDC_DISABLE);
  342. return lines << 4;
  343. };
  344. /* ------------------------------------------------------------------------- */
  345. void upmconfig (uint upm, uint * table, uint size)
  346. {
  347. uint i;
  348. uint addr = 0;
  349. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  350. volatile memctl8xx_t *memctl = &immap->im_memctl;
  351. for (i = 0; i < size; i++) {
  352. memctl->memc_mdr = table[i]; /* (16-15) */
  353. memctl->memc_mcr = addr | upm; /* (16-16) */
  354. addr++;
  355. }
  356. }
  357. /* ------------------------------------------------------------------------- */
  358. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  359. {
  360. ulong msr, addr;
  361. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  362. immap->im_clkrst.car_plprcr |= PLPRCR_CSR; /* Checkstop Reset enable */
  363. /* Interrupts and MMU off */
  364. __asm__ volatile ("mtspr 81, 0");
  365. __asm__ volatile ("mfmsr %0":"=r" (msr));
  366. msr &= ~0x1030;
  367. __asm__ volatile ("mtmsr %0"::"r" (msr));
  368. /*
  369. * Trying to execute the next instruction at a non-existing address
  370. * should cause a machine check, resulting in reset
  371. */
  372. #ifdef CFG_RESET_ADDRESS
  373. addr = CFG_RESET_ADDRESS;
  374. #else
  375. /*
  376. * note: when CFG_MONITOR_BASE points to a RAM address, CFG_MONITOR_BASE
  377. * - sizeof (ulong) is usually a valid address. Better pick an address
  378. * known to be invalid on your system and assign it to CFG_RESET_ADDRESS.
  379. * "(ulong)-1" used to be a good choice for many systems...
  380. */
  381. addr = CFG_MONITOR_BASE - sizeof (ulong);
  382. #endif
  383. ((void (*)(void)) addr) ();
  384. return 1;
  385. }
  386. /* ------------------------------------------------------------------------- */
  387. /*
  388. * Get timebase clock frequency (like cpu_clk in Hz)
  389. *
  390. * See sections 14.2 and 14.6 of the User's Manual
  391. */
  392. unsigned long get_tbclk (void)
  393. {
  394. DECLARE_GLOBAL_DATA_PTR;
  395. uint immr = get_immr (0); /* Return full IMMR contents */
  396. volatile immap_t *immap = (volatile immap_t *)(immr & 0xFFFF0000);
  397. ulong oscclk, factor, pll;
  398. if (immap->im_clkrst.car_sccr & SCCR_TBS) {
  399. return (gd->cpu_clk / 16);
  400. }
  401. pll = immap->im_clkrst.car_plprcr;
  402. #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
  403. /*
  404. * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
  405. * factor is calculated as follows:
  406. *
  407. * MFN
  408. * MFI + -------
  409. * MFD + 1
  410. * factor = -----------------
  411. * (PDF + 1) * 2^S
  412. *
  413. * For older chips, it's just MF field of PLPRCR plus one.
  414. */
  415. if ((immr & 0xFFFF) >= MPC8xx_NEW_CLK) { /* MPC866/87x/88x series */
  416. factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN)/(PLPRCR_val(MFD)+1))/
  417. (PLPRCR_val(PDF)+1) / (1<<PLPRCR_val(S));
  418. } else {
  419. factor = PLPRCR_val(MF)+1;
  420. }
  421. oscclk = gd->cpu_clk / factor;
  422. if ((immap->im_clkrst.car_sccr & SCCR_RTSEL) == 0 || factor > 2) {
  423. return (oscclk / 4);
  424. }
  425. return (oscclk / 16);
  426. }
  427. /* ------------------------------------------------------------------------- */
  428. #if defined(CONFIG_WATCHDOG)
  429. void watchdog_reset (void)
  430. {
  431. int re_enable = disable_interrupts ();
  432. reset_8xx_watchdog ((immap_t *) CFG_IMMR);
  433. if (re_enable)
  434. enable_interrupts ();
  435. }
  436. void reset_8xx_watchdog (volatile immap_t * immr)
  437. {
  438. # if defined(CONFIG_LWMON)
  439. /*
  440. * The LWMON board uses a MAX6301 Watchdog
  441. * with the trigger pin connected to port PA.7
  442. *
  443. * (The old board version used a MAX706TESA Watchdog, which
  444. * had to be handled exactly the same.)
  445. */
  446. # define WATCHDOG_BIT 0x0100
  447. immr->im_ioport.iop_papar &= ~(WATCHDOG_BIT); /* GPIO */
  448. immr->im_ioport.iop_padir |= WATCHDOG_BIT; /* Output */
  449. immr->im_ioport.iop_paodr &= ~(WATCHDOG_BIT); /* active output */
  450. immr->im_ioport.iop_padat ^= WATCHDOG_BIT; /* Toggle WDI */
  451. # else
  452. /*
  453. * All other boards use the MPC8xx Internal Watchdog
  454. */
  455. immr->im_siu_conf.sc_swsr = 0x556c; /* write magic1 */
  456. immr->im_siu_conf.sc_swsr = 0xaa39; /* write magic2 */
  457. # endif /* CONFIG_LWMON */
  458. }
  459. #endif /* CONFIG_WATCHDOG */
  460. /* ------------------------------------------------------------------------- */