leon_kernel.c 9.4 KB

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