leon_kernel.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
  3. * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/errno.h>
  8. #include <linux/mutex.h>
  9. #include <linux/of.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/of_device.h>
  13. #include <asm/oplib.h>
  14. #include <asm/timer.h>
  15. #include <asm/prom.h>
  16. #include <asm/leon.h>
  17. #include <asm/leon_amba.h>
  18. #include <asm/traps.h>
  19. #include <asm/cacheflush.h>
  20. #include "prom.h"
  21. #include "irq.h"
  22. struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
  23. struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
  24. struct amba_apb_device leon_percpu_timer_dev[16];
  25. int leondebug_irq_disable;
  26. int leon_debug_irqout;
  27. static int dummy_master_l10_counter;
  28. unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
  29. unsigned int sparc_leon_eirq;
  30. #define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
  31. /* Return the IRQ of the pending IRQ on the extended IRQ controller */
  32. int sparc_leon_eirq_get(int eirq, int cpu)
  33. {
  34. return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
  35. }
  36. irqreturn_t sparc_leon_eirq_isr(int dummy, void *dev_id)
  37. {
  38. printk(KERN_ERR "sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
  39. return IRQ_HANDLED;
  40. }
  41. /* The extended IRQ controller has been found, this function registers it */
  42. void sparc_leon_eirq_register(int eirq)
  43. {
  44. int irq;
  45. /* Register a "BAD" handler for this interrupt, it should never happen */
  46. irq = request_irq(eirq, sparc_leon_eirq_isr,
  47. (IRQF_DISABLED | SA_STATIC_ALLOC), "extirq", NULL);
  48. if (irq) {
  49. printk(KERN_ERR
  50. "sparc_leon_eirq_register: unable to attach IRQ%d\n",
  51. eirq);
  52. } else {
  53. sparc_leon_eirq = eirq;
  54. }
  55. }
  56. static inline unsigned long get_irqmask(unsigned int irq)
  57. {
  58. unsigned long mask;
  59. if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
  60. || ((irq > 0x1f) && sparc_leon_eirq)) {
  61. printk(KERN_ERR
  62. "leon_get_irqmask: false irq number: %d\n", irq);
  63. mask = 0;
  64. } else {
  65. mask = LEON_HARD_INT(irq);
  66. }
  67. return mask;
  68. }
  69. static void leon_enable_irq(unsigned int irq_nr)
  70. {
  71. unsigned long mask, flags;
  72. mask = get_irqmask(irq_nr);
  73. local_irq_save(flags);
  74. LEON3_BYPASS_STORE_PA(LEON_IMASK,
  75. (LEON3_BYPASS_LOAD_PA(LEON_IMASK) | (mask)));
  76. local_irq_restore(flags);
  77. }
  78. static void leon_disable_irq(unsigned int irq_nr)
  79. {
  80. unsigned long mask, flags;
  81. mask = get_irqmask(irq_nr);
  82. local_irq_save(flags);
  83. LEON3_BYPASS_STORE_PA(LEON_IMASK,
  84. (LEON3_BYPASS_LOAD_PA(LEON_IMASK) & ~(mask)));
  85. local_irq_restore(flags);
  86. }
  87. void __init leon_init_timers(irq_handler_t counter_fn)
  88. {
  89. int irq;
  90. struct device_node *rootnp, *np;
  91. struct property *pp;
  92. int len;
  93. int cpu, icsel;
  94. leondebug_irq_disable = 0;
  95. leon_debug_irqout = 0;
  96. master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
  97. dummy_master_l10_counter = 0;
  98. /*Find IRQMP IRQ Controller Registers base address otherwise bail out.*/
  99. rootnp = of_find_node_by_path("/ambapp0");
  100. if (!rootnp)
  101. goto bad;
  102. np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
  103. if (!np) {
  104. np = of_find_node_by_name(rootnp, "01_00d");
  105. if (!np)
  106. goto bad;
  107. }
  108. pp = of_find_property(np, "reg", &len);
  109. if (!pp)
  110. goto bad;
  111. leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
  112. /* Find GPTIMER Timer Registers base address otherwise bail out. */
  113. np = of_find_node_by_name(rootnp, "GAISLER_GPTIMER");
  114. if (!np) {
  115. np = of_find_node_by_name(np, "01_011");
  116. if (!np)
  117. goto bad;
  118. }
  119. pp = of_find_property(np, "reg", &len);
  120. if (!pp)
  121. goto bad;
  122. leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)pp->value;
  123. pp = of_find_property(np, "interrupts", &len);
  124. if (!pp)
  125. goto bad;
  126. leon3_gptimer_irq = *(unsigned int *)pp->value;
  127. if (leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq) {
  128. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].val, 0);
  129. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].rld,
  130. (((1000000 / HZ) - 1)));
  131. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl, 0);
  132. #ifdef CONFIG_SMP
  133. leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
  134. leon_percpu_timer_dev[0].irq = leon3_gptimer_irq+1;
  135. if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
  136. (1<<LEON3_GPTIMER_SEPIRQ))) {
  137. prom_printf("irq timer not configured with separate irqs\n");
  138. BUG();
  139. }
  140. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].val, 0);
  141. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].rld, (((1000000/HZ) - 1)));
  142. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl, 0);
  143. # endif
  144. /*
  145. * The IRQ controller may (if implemented) consist of multiple
  146. * IRQ controllers, each mapped on a 4Kb boundary.
  147. * Each CPU may be routed to different IRQCTRLs, however
  148. * we assume that all CPUs (in SMP system) is routed to the
  149. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  150. * accessed anyway.
  151. * In AMP systems, Linux must run on CPU0 for the time being.
  152. */
  153. cpu = sparc_leon3_cpuid();
  154. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
  155. icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
  156. leon3_irqctrl_regs += icsel;
  157. } else {
  158. goto bad;
  159. }
  160. irq = request_irq(leon3_gptimer_irq,
  161. counter_fn,
  162. (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
  163. if (irq) {
  164. printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
  165. LEON_INTERRUPT_TIMER1);
  166. prom_halt();
  167. }
  168. # ifdef CONFIG_SMP
  169. {
  170. unsigned long flags;
  171. struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
  172. /* For SMP we use the level 14 ticker, however the bootup code
  173. * has copied the firmwares level 14 vector into boot cpu's
  174. * trap table, we must fix this now or we get squashed.
  175. */
  176. local_irq_save(flags);
  177. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  178. /* Adjust so that we jump directly to smpleon_ticker */
  179. trap_table->inst_three += smpleon_ticker - real_irq_entry;
  180. local_flush_cache_all();
  181. local_irq_restore(flags);
  182. }
  183. # endif
  184. if (leon3_gptimer_regs) {
  185. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl,
  186. LEON3_GPTIMER_EN |
  187. LEON3_GPTIMER_RL |
  188. LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
  189. #ifdef CONFIG_SMP
  190. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl,
  191. LEON3_GPTIMER_EN |
  192. LEON3_GPTIMER_RL |
  193. LEON3_GPTIMER_LD |
  194. LEON3_GPTIMER_IRQEN);
  195. #endif
  196. }
  197. return;
  198. bad:
  199. printk(KERN_ERR "No Timer/irqctrl found\n");
  200. BUG();
  201. return;
  202. }
  203. void leon_clear_clock_irq(void)
  204. {
  205. }
  206. void leon_load_profile_irq(int cpu, unsigned int limit)
  207. {
  208. BUG();
  209. }
  210. void __init leon_trans_init(struct device_node *dp)
  211. {
  212. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  213. struct property *p;
  214. p = of_find_property(dp, "mid", (void *)0);
  215. if (p) {
  216. int mid;
  217. dp->name = prom_early_alloc(5 + 1);
  218. memcpy(&mid, p->value, p->length);
  219. sprintf((char *)dp->name, "cpu%.2d", mid);
  220. }
  221. }
  222. }
  223. void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
  224. void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
  225. {
  226. if (prom_amba_init &&
  227. strcmp(dp->type, "ambapp") == 0 &&
  228. strcmp(dp->name, "ambapp0") == 0) {
  229. prom_amba_init(dp, nextp);
  230. }
  231. }
  232. #ifdef CONFIG_SMP
  233. void leon_set_cpu_int(int cpu, int level)
  234. {
  235. unsigned long mask;
  236. mask = get_irqmask(level);
  237. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  238. }
  239. static void leon_clear_ipi(int cpu, int level)
  240. {
  241. unsigned long mask;
  242. mask = get_irqmask(level);
  243. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
  244. }
  245. static void leon_set_udt(int cpu)
  246. {
  247. }
  248. void leon_clear_profile_irq(int cpu)
  249. {
  250. }
  251. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  252. {
  253. unsigned long mask, flags, *addr;
  254. mask = get_irqmask(irq_nr);
  255. local_irq_save(flags);
  256. addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
  257. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
  258. local_irq_restore(flags);
  259. }
  260. #endif
  261. void __init leon_init_IRQ(void)
  262. {
  263. sparc_init_timers = leon_init_timers;
  264. BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
  265. BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
  266. BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
  267. BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
  268. BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
  269. BTFIXUPCALL_NORM);
  270. BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
  271. BTFIXUPCALL_NOP);
  272. #ifdef CONFIG_SMP
  273. BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
  274. BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
  275. BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
  276. #endif
  277. }
  278. void __init leon_init(void)
  279. {
  280. of_pdt_build_more = &leon_node_init;
  281. }