leon_kernel.c 12 KB

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