leon_kernel.c 10 KB

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