mpc52xx_setup.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Common code for the boards based on Freescale MPC52xx embedded CPU.
  3. *
  4. *
  5. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  6. *
  7. * Support for other bootloaders than UBoot by Dale Farnsworth
  8. * <dfarnsworth@mvista.com>
  9. *
  10. * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
  11. * Copyright (C) 2003 Montavista Software, Inc
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/spinlock.h>
  18. #include <asm/io.h>
  19. #include <asm/time.h>
  20. #include <asm/mpc52xx.h>
  21. #include <asm/mpc52xx_psc.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/ppcboot.h>
  24. #include <syslib/mpc52xx_pci.h>
  25. extern bd_t __res;
  26. static int core_mult[] = { /* CPU Frequency multiplier, taken */
  27. 0, 0, 0, 10, 20, 20, 25, 45, /* from the datasheet used to compute */
  28. 30, 55, 40, 50, 0, 60, 35, 0, /* CPU frequency from XLB freq and */
  29. 30, 25, 65, 10, 70, 20, 75, 45, /* external jumper config */
  30. 0, 55, 40, 50, 80, 60, 35, 0
  31. };
  32. void
  33. mpc52xx_restart(char *cmd)
  34. {
  35. struct mpc52xx_gpt __iomem *gpt0 = MPC52xx_VA(MPC52xx_GPTx_OFFSET(0));
  36. local_irq_disable();
  37. /* Turn on the watchdog and wait for it to expire. It effectively
  38. does a reset */
  39. out_be32(&gpt0->count, 0x000000ff);
  40. out_be32(&gpt0->mode, 0x00009004);
  41. while (1);
  42. }
  43. void
  44. mpc52xx_halt(void)
  45. {
  46. local_irq_disable();
  47. while (1);
  48. }
  49. void
  50. mpc52xx_power_off(void)
  51. {
  52. /* By default we don't have any way of shut down.
  53. If a specific board wants to, it can set the power down
  54. code to any hardware implementation dependent code */
  55. mpc52xx_halt();
  56. }
  57. void __init
  58. mpc52xx_set_bat(void)
  59. {
  60. /* Set BAT 2 to map the 0xf0000000 area */
  61. /* This mapping is used during mpc52xx_progress,
  62. * mpc52xx_find_end_of_memory, and UARTs/GPIO access for debug
  63. */
  64. mb();
  65. mtspr(SPRN_DBAT2U, 0xf0001ffe);
  66. mtspr(SPRN_DBAT2L, 0xf000002a);
  67. mb();
  68. }
  69. void __init
  70. mpc52xx_map_io(void)
  71. {
  72. /* Here we map the MBAR and the whole upper zone. MBAR is only
  73. 64k but we can't map only 64k with BATs. Map the whole
  74. 0xf0000000 range is ok and helps eventual lpb devices placed there */
  75. io_block_mapping(
  76. MPC52xx_MBAR_VIRT, MPC52xx_MBAR, 0x10000000, _PAGE_IO);
  77. }
  78. #ifdef CONFIG_SERIAL_TEXT_DEBUG
  79. #ifndef MPC52xx_PF_CONSOLE_PORT
  80. #error "mpc52xx PSC for console not selected"
  81. #endif
  82. static void
  83. mpc52xx_psc_putc(struct mpc52xx_psc __iomem *psc, unsigned char c)
  84. {
  85. while (!(in_be16(&psc->mpc52xx_psc_status) &
  86. MPC52xx_PSC_SR_TXRDY));
  87. out_8(&psc->mpc52xx_psc_buffer_8, c);
  88. }
  89. void
  90. mpc52xx_progress(char *s, unsigned short hex)
  91. {
  92. char c;
  93. struct mpc52xx_psc __iomem *psc;
  94. psc = MPC52xx_VA(MPC52xx_PSCx_OFFSET(MPC52xx_PF_CONSOLE_PORT));
  95. while ((c = *s++) != 0) {
  96. if (c == '\n')
  97. mpc52xx_psc_putc(psc, '\r');
  98. mpc52xx_psc_putc(psc, c);
  99. }
  100. mpc52xx_psc_putc(psc, '\r');
  101. mpc52xx_psc_putc(psc, '\n');
  102. }
  103. #endif /* CONFIG_SERIAL_TEXT_DEBUG */
  104. unsigned long __init
  105. mpc52xx_find_end_of_memory(void)
  106. {
  107. u32 ramsize = __res.bi_memsize;
  108. /*
  109. * if bootloader passed a memsize, just use it
  110. * else get size from sdram config registers
  111. */
  112. if (ramsize == 0) {
  113. struct mpc52xx_mmap_ctl __iomem *mmap_ctl;
  114. u32 sdram_config_0, sdram_config_1;
  115. /* Temp BAT2 mapping active when this is called ! */
  116. mmap_ctl = MPC52xx_VA(MPC52xx_MMAP_CTL_OFFSET);
  117. sdram_config_0 = in_be32(&mmap_ctl->sdram0);
  118. sdram_config_1 = in_be32(&mmap_ctl->sdram1);
  119. if ((sdram_config_0 & 0x1f) >= 0x13)
  120. ramsize = 1 << ((sdram_config_0 & 0xf) + 17);
  121. if (((sdram_config_1 & 0x1f) >= 0x13) &&
  122. ((sdram_config_1 & 0xfff00000) == ramsize))
  123. ramsize += 1 << ((sdram_config_1 & 0xf) + 17);
  124. }
  125. return ramsize;
  126. }
  127. void __init
  128. mpc52xx_calibrate_decr(void)
  129. {
  130. int current_time, previous_time;
  131. int tbl_start, tbl_end;
  132. unsigned int xlbfreq, cpufreq, ipbfreq, pcifreq, divisor;
  133. xlbfreq = __res.bi_busfreq;
  134. /* if bootloader didn't pass bus frequencies, calculate them */
  135. if (xlbfreq == 0) {
  136. /* Get RTC & Clock manager modules */
  137. struct mpc52xx_rtc __iomem *rtc;
  138. struct mpc52xx_cdm __iomem *cdm;
  139. rtc = ioremap(MPC52xx_PA(MPC52xx_RTC_OFFSET), MPC52xx_RTC_SIZE);
  140. cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
  141. if ((rtc==NULL) || (cdm==NULL))
  142. panic("Can't ioremap RTC/CDM while computing bus freq");
  143. /* Count bus clock during 1/64 sec */
  144. out_be32(&rtc->dividers, 0x8f1f0000); /* Set RTC 64x faster */
  145. previous_time = in_be32(&rtc->time);
  146. while ((current_time = in_be32(&rtc->time)) == previous_time) ;
  147. tbl_start = get_tbl();
  148. previous_time = current_time;
  149. while ((current_time = in_be32(&rtc->time)) == previous_time) ;
  150. tbl_end = get_tbl();
  151. out_be32(&rtc->dividers, 0xffff0000); /* Restore RTC */
  152. /* Compute all frequency from that & CDM settings */
  153. xlbfreq = (tbl_end - tbl_start) << 8;
  154. cpufreq = (xlbfreq * core_mult[in_be32(&cdm->rstcfg)&0x1f])/10;
  155. ipbfreq = (in_8(&cdm->ipb_clk_sel) & 1) ?
  156. xlbfreq / 2 : xlbfreq;
  157. switch (in_8(&cdm->pci_clk_sel) & 3) {
  158. case 0:
  159. pcifreq = ipbfreq;
  160. break;
  161. case 1:
  162. pcifreq = ipbfreq / 2;
  163. break;
  164. default:
  165. pcifreq = xlbfreq / 4;
  166. break;
  167. }
  168. __res.bi_busfreq = xlbfreq;
  169. __res.bi_intfreq = cpufreq;
  170. __res.bi_ipbfreq = ipbfreq;
  171. __res.bi_pcifreq = pcifreq;
  172. /* Release mapping */
  173. iounmap(rtc);
  174. iounmap(cdm);
  175. }
  176. divisor = 4;
  177. tb_ticks_per_jiffy = xlbfreq / HZ / divisor;
  178. tb_to_us = mulhwu_scale_factor(xlbfreq / divisor, 1000000);
  179. }
  180. void __init
  181. mpc52xx_setup_cpu(void)
  182. {
  183. struct mpc52xx_cdm __iomem *cdm;
  184. struct mpc52xx_xlb __iomem *xlb;
  185. /* Map zones */
  186. cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
  187. xlb = ioremap(MPC52xx_PA(MPC52xx_XLB_OFFSET), MPC52xx_XLB_SIZE);
  188. if (!cdm || !xlb) {
  189. printk(KERN_ERR __FILE__ ": "
  190. "Error while mapping CDM/XLB during "
  191. "mpc52xx_setup_cpu\n");
  192. goto unmap_regs;
  193. }
  194. /* Use internal 48 Mhz */
  195. out_8(&cdm->ext_48mhz_en, 0x00);
  196. out_8(&cdm->fd_enable, 0x01);
  197. if (in_be32(&cdm->rstcfg) & 0x40) /* Assumes 33Mhz clock */
  198. out_be16(&cdm->fd_counters, 0x0001);
  199. else
  200. out_be16(&cdm->fd_counters, 0x5555);
  201. /* Configure the XLB Arbiter priorities */
  202. out_be32(&xlb->master_pri_enable, 0xff);
  203. out_be32(&xlb->master_priority, 0x11111111);
  204. /* Enable ram snooping for 1GB window */
  205. out_be32(&xlb->config, in_be32(&xlb->config) | MPC52xx_XLB_CFG_SNOOP);
  206. out_be32(&xlb->snoop_window, MPC52xx_PCI_TARGET_MEM | 0x1d);
  207. /* Disable XLB pipelining */
  208. /* (cfr errata 292. We could do this only just before ATA PIO
  209. transaction and re-enable it after ...) */
  210. out_be32(&xlb->config, in_be32(&xlb->config) | MPC52xx_XLB_CFG_PLDIS);
  211. /* Unmap reg zone */
  212. unmap_regs:
  213. if (cdm) iounmap(cdm);
  214. if (xlb) iounmap(xlb);
  215. }
  216. int mpc52xx_match_psc_function(int psc_idx, const char *func)
  217. {
  218. struct mpc52xx_psc_func *cf = mpc52xx_psc_functions;
  219. while ((cf->id != -1) && (cf->func != NULL)) {
  220. if ((cf->id == psc_idx) && !strcmp(cf->func,func))
  221. return 1;
  222. cf++;
  223. }
  224. return 0;
  225. }
  226. int mpc52xx_set_psc_clkdiv(int psc_id, int clkdiv)
  227. {
  228. static DEFINE_SPINLOCK(lock);
  229. struct mpc52xx_cdm __iomem *cdm;
  230. unsigned long flags;
  231. u16 mclken_div;
  232. u16 __iomem *reg;
  233. u32 mask;
  234. cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
  235. if (!cdm) {
  236. printk(KERN_ERR __FILE__ ": Error mapping CDM\n");
  237. return -ENODEV;
  238. }
  239. mclken_div = 0x8000 | (clkdiv & 0x1FF);
  240. switch (psc_id) {
  241. case 1: reg = &cdm->mclken_div_psc1; mask = 0x20; break;
  242. case 2: reg = &cdm->mclken_div_psc2; mask = 0x40; break;
  243. case 3: reg = &cdm->mclken_div_psc3; mask = 0x80; break;
  244. case 6: reg = &cdm->mclken_div_psc6; mask = 0x10; break;
  245. default:
  246. return -ENODEV;
  247. }
  248. /* Set the rate and enable the clock */
  249. spin_lock_irqsave(&lock, flags);
  250. out_be16(reg, mclken_div);
  251. out_be32(&cdm->clk_enables, in_be32(&cdm->clk_enables) | mask);
  252. spin_unlock_irqrestore(&lock, flags);
  253. iounmap(cdm);
  254. return 0;
  255. }