cpu_init.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Initializes CPU and basic hardware such as memory
  2. * controllers, IRQ controller and system timer 0.
  3. *
  4. * (C) Copyright 2007
  5. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. *
  25. */
  26. #include <common.h>
  27. #include <asm/asi.h>
  28. #include <asm/leon.h>
  29. #include <ambapp.h>
  30. #include <config.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* reset CPU (jump to 0, without reset) */
  33. void start(void);
  34. /* find & initialize the memory controller */
  35. int init_memory_ctrl(void);
  36. ambapp_dev_irqmp *irqmp = NULL;
  37. ambapp_dev_mctrl memctrl;
  38. ambapp_dev_gptimer *gptimer = NULL;
  39. unsigned int gptimer_irq = 0;
  40. int leon3_snooping_avail = 0;
  41. struct {
  42. gd_t gd_area;
  43. bd_t bd;
  44. } global_data;
  45. /*
  46. * Breath some life into the CPU...
  47. *
  48. * Set up the memory map,
  49. * initialize a bunch of registers.
  50. *
  51. * Run from FLASH/PROM:
  52. * - until memory controller is set up, only registers avaiable
  53. * - no global variables available for writing
  54. * - constants avaiable
  55. */
  56. void cpu_init_f(void)
  57. {
  58. /* these varaiable must not be initialized */
  59. ambapp_dev_irqmp *irqmp;
  60. ambapp_apbdev apbdev;
  61. register unsigned int apbmst;
  62. /* find AMBA APB Master */
  63. apbmst = (unsigned int)
  64. ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_APBMST, 1, 0);
  65. if (!apbmst) {
  66. /*
  67. * no AHB/APB bridge, something is wrong
  68. * ==> jump to start (or hang)
  69. */
  70. while (1) ;
  71. }
  72. /* Init memory controller */
  73. if (init_memory_ctrl()) {
  74. while (1) ;
  75. }
  76. /****************************************************
  77. * From here we can use the main memory and the stack.
  78. */
  79. /* Find AMBA APB IRQMP Controller */
  80. if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_IRQMP, &apbdev) != 1) {
  81. /* no IRQ controller, something is wrong
  82. * ==> jump to start (or hang)
  83. */
  84. while (1) ;
  85. }
  86. irqmp = (ambapp_dev_irqmp *) apbdev.address;
  87. /* initialize the IRQMP */
  88. irqmp->ilevel = 0xf; /* all IRQ off */
  89. irqmp->iforce = 0;
  90. irqmp->ipend = 0;
  91. irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
  92. irqmp->cpu_mask[0] = 0; /* mask all IRQs on CPU 0 */
  93. irqmp->cpu_force[0] = 0; /* no force IRQ on CPU 0 */
  94. /* cache */
  95. }
  96. void cpu_init_f2(void)
  97. {
  98. }
  99. /*
  100. * initialize higher level parts of CPU like time base and timers
  101. */
  102. int cpu_init_r(void)
  103. {
  104. ambapp_apbdev apbdev;
  105. /*
  106. * Find AMBA APB IRQMP Controller,
  107. * When we come so far we know there is a IRQMP available
  108. */
  109. ambapp_apb_first(VENDOR_GAISLER, GAISLER_IRQMP, &apbdev);
  110. irqmp = (ambapp_dev_irqmp *) apbdev.address;
  111. /* timer */
  112. if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_GPTIMER, &apbdev) != 1) {
  113. printf("cpu_init_r: gptimer not found!\n");
  114. return 1;
  115. }
  116. gptimer = (ambapp_dev_gptimer *) apbdev.address;
  117. gptimer_irq = apbdev.irq;
  118. /* initialize prescaler common to all timers to 1MHz */
  119. gptimer->scalar = gptimer->scalar_reload =
  120. (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
  121. return (0);
  122. }
  123. /* find & setup memory controller */
  124. int init_memory_ctrl()
  125. {
  126. register ambapp_dev_mctrl *mctrl;
  127. register ambapp_dev_sdctrl *sdctrl;
  128. register ambapp_dev_ddrspa *ddrspa;
  129. register ambapp_dev_ddr2spa *ddr2spa;
  130. register ahbctrl_pp_dev *ahb;
  131. register unsigned int base;
  132. register int not_found_mctrl = -1;
  133. /* find ESA Memory controller */
  134. base = ambapp_apb_next_nomem(VENDOR_ESA, ESA_MCTRL, 0);
  135. if (base) {
  136. mctrl = (ambapp_dev_mctrl *) base;
  137. /* config MCTRL memory controller */
  138. mctrl->mcfg1 = CFG_GRLIB_MEMCFG1 | (mctrl->mcfg1 & 0x300);
  139. mctrl->mcfg2 = CFG_GRLIB_MEMCFG2;
  140. mctrl->mcfg3 = CFG_GRLIB_MEMCFG3;
  141. not_found_mctrl = 0;
  142. }
  143. /* find Gaisler Fault Tolerant Memory controller */
  144. base = ambapp_apb_next_nomem(VENDOR_GAISLER, GAISLER_FTMCTRL, 0);
  145. if (base) {
  146. mctrl = (ambapp_dev_mctrl *) base;
  147. /* config MCTRL memory controller */
  148. mctrl->mcfg1 = CFG_GRLIB_FT_MEMCFG1 | (mctrl->mcfg1 & 0x300);
  149. mctrl->mcfg2 = CFG_GRLIB_FT_MEMCFG2;
  150. mctrl->mcfg3 = CFG_GRLIB_FT_MEMCFG3;
  151. not_found_mctrl = 0;
  152. }
  153. /* find SDRAM controller */
  154. base = ambapp_apb_next_nomem(VENDOR_GAISLER, GAISLER_SDCTRL, 0);
  155. if (base) {
  156. sdctrl = (ambapp_dev_sdctrl *) base;
  157. /* config memory controller */
  158. sdctrl->sdcfg = CFG_GRLIB_SDRAM;
  159. not_found_mctrl = 0;
  160. }
  161. ahb = ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_DDR2SPA, 1, 0);
  162. if (ahb) {
  163. ddr2spa = (ambapp_dev_ddr2spa *) ambapp_ahb_get_info(ahb, 1);
  164. /* Config DDR2 memory controller */
  165. ddr2spa->cfg1 = CFG_GRLIB_DDR2_CFG1;
  166. ddr2spa->cfg3 = CFG_GRLIB_DDR2_CFG3;
  167. not_found_mctrl = 0;
  168. }
  169. ahb = ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_DDRSPA, 1, 0);
  170. if (ahb) {
  171. ddrspa = (ambapp_dev_ddrspa *) ambapp_ahb_get_info(ahb, 1);
  172. /* Config DDR memory controller */
  173. ddrspa->ctrl = CFG_GRLIB_DDR_CFG;
  174. not_found_mctrl = 0;
  175. }
  176. /* failed to find any memory controller */
  177. return not_found_mctrl;
  178. }
  179. /* Uses Timer 0 to get accurate
  180. * pauses. Max 2 raised to 32 ticks
  181. *
  182. */
  183. void cpu_wait_ticks(unsigned long ticks)
  184. {
  185. unsigned long start = get_timer(0);
  186. while (get_timer(start) < ticks) ;
  187. }
  188. /* initiate and setup timer0 interrupt to 1MHz
  189. * Return irq number for timer int or a negative number for
  190. * dealing with self
  191. */
  192. int timer_interrupt_init_cpu(void)
  193. {
  194. /* 1ms ticks */
  195. gptimer->e[0].val = 0;
  196. gptimer->e[0].rld = 999; /* (((1000000 / 100) - 1)) */
  197. gptimer->e[0].ctrl =
  198. (LEON3_GPTIMER_EN |
  199. LEON3_GPTIMER_RL | LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
  200. return gptimer_irq;
  201. }
  202. /*
  203. * This function is intended for SHORT delays only.
  204. */
  205. unsigned long cpu_usec2ticks(unsigned long usec)
  206. {
  207. /* timer set to 1kHz ==> 1 clk tick = 1 msec */
  208. if (usec < 1000)
  209. return 1;
  210. return (usec / 1000);
  211. }
  212. unsigned long cpu_ticks2usec(unsigned long ticks)
  213. {
  214. /* 1tick = 1usec */
  215. return ticks * 1000;
  216. }