leon_kernel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. 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. int leon3_ticker_irq; /* Timer ticker IRQ */
  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. leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
  233. if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
  234. (1<<LEON3_GPTIMER_SEPIRQ))) {
  235. prom_printf("irq timer not configured with separate irqs\n");
  236. BUG();
  237. }
  238. LEON3_BYPASS_STORE_PA(
  239. &leon3_gptimer_regs->e[leon3_gptimer_idx+1].val, 0);
  240. LEON3_BYPASS_STORE_PA(
  241. &leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
  242. (((1000000/HZ) - 1)));
  243. LEON3_BYPASS_STORE_PA(
  244. &leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl, 0);
  245. # endif
  246. /*
  247. * The IRQ controller may (if implemented) consist of multiple
  248. * IRQ controllers, each mapped on a 4Kb boundary.
  249. * Each CPU may be routed to different IRQCTRLs, however
  250. * we assume that all CPUs (in SMP system) is routed to the
  251. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  252. * accessed anyway.
  253. * In AMP systems, Linux must run on CPU0 for the time being.
  254. */
  255. cpu = sparc_leon3_cpuid();
  256. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
  257. icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
  258. leon3_irqctrl_regs += icsel;
  259. /* Probe extended IRQ controller */
  260. eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
  261. >> 16) & 0xf;
  262. if (eirq != 0)
  263. leon_eirq_setup(eirq);
  264. } else {
  265. goto bad;
  266. }
  267. irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
  268. err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
  269. if (err) {
  270. printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
  271. irq);
  272. prom_halt();
  273. }
  274. if (leon3_gptimer_regs) {
  275. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  276. LEON3_GPTIMER_EN |
  277. LEON3_GPTIMER_RL |
  278. LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
  279. #ifdef CONFIG_SMP
  280. /* Install per-cpu IRQ handler for broadcasted ticker */
  281. irq = leon_build_device_irq(leon3_ticker_irq,
  282. handle_percpu_irq, "per-cpu",
  283. 0);
  284. err = request_irq(irq, leon_percpu_timer_interrupt,
  285. IRQF_PERCPU | IRQF_TIMER, "ticker",
  286. NULL);
  287. if (err) {
  288. printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
  289. prom_halt();
  290. }
  291. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  292. LEON3_GPTIMER_EN |
  293. LEON3_GPTIMER_RL |
  294. LEON3_GPTIMER_LD |
  295. LEON3_GPTIMER_IRQEN);
  296. #endif
  297. }
  298. return;
  299. bad:
  300. printk(KERN_ERR "No Timer/irqctrl found\n");
  301. BUG();
  302. return;
  303. }
  304. void leon_clear_clock_irq(void)
  305. {
  306. }
  307. void leon_load_profile_irq(int cpu, unsigned int limit)
  308. {
  309. BUG();
  310. }
  311. void __init leon_trans_init(struct device_node *dp)
  312. {
  313. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  314. struct property *p;
  315. p = of_find_property(dp, "mid", (void *)0);
  316. if (p) {
  317. int mid;
  318. dp->name = prom_early_alloc(5 + 1);
  319. memcpy(&mid, p->value, p->length);
  320. sprintf((char *)dp->name, "cpu%.2d", mid);
  321. }
  322. }
  323. }
  324. void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
  325. void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
  326. {
  327. if (prom_amba_init &&
  328. strcmp(dp->type, "ambapp") == 0 &&
  329. strcmp(dp->name, "ambapp0") == 0) {
  330. prom_amba_init(dp, nextp);
  331. }
  332. }
  333. #ifdef CONFIG_SMP
  334. void leon_set_cpu_int(int cpu, int level)
  335. {
  336. unsigned long mask;
  337. mask = get_irqmask(level);
  338. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  339. }
  340. static void leon_clear_ipi(int cpu, int level)
  341. {
  342. unsigned long mask;
  343. mask = get_irqmask(level);
  344. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
  345. }
  346. static void leon_set_udt(int cpu)
  347. {
  348. }
  349. void leon_clear_profile_irq(int cpu)
  350. {
  351. }
  352. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  353. {
  354. unsigned long mask, flags, *addr;
  355. mask = get_irqmask(irq_nr);
  356. spin_lock_irqsave(&leon_irq_lock, flags);
  357. addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
  358. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
  359. spin_unlock_irqrestore(&leon_irq_lock, flags);
  360. }
  361. #endif
  362. void __init leon_init_IRQ(void)
  363. {
  364. sparc_irq_config.init_timers = leon_init_timers;
  365. sparc_irq_config.build_device_irq = _leon_build_device_irq;
  366. BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
  367. BTFIXUPCALL_NORM);
  368. BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
  369. BTFIXUPCALL_NOP);
  370. #ifdef CONFIG_SMP
  371. BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
  372. BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
  373. BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
  374. #endif
  375. }
  376. void __init leon_init(void)
  377. {
  378. of_pdt_build_more = &leon_node_init;
  379. }