leon_kernel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/errno.h>
  7. #include <linux/mutex.h>
  8. #include <linux/of.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/of_device.h>
  12. #include <asm/oplib.h>
  13. #include <asm/timer.h>
  14. #include <asm/prom.h>
  15. #include <asm/leon.h>
  16. #include <asm/leon_amba.h>
  17. #include <asm/traps.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/smp.h>
  20. #include <asm/setup.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(cpu) (&leon3_irqctrl_regs->mask[cpu])
  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 = sparc_leon3_cpuid();
  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(boot_cpu_id));
  70. LEON3_BYPASS_STORE_PA(LEON_IMASK(boot_cpu_id), (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. #ifdef CONFIG_SMP
  87. static int irq_choose_cpu(const struct cpumask *affinity)
  88. {
  89. cpumask_t mask;
  90. cpumask_and(&mask, cpu_online_mask, affinity);
  91. if (cpumask_equal(&mask, cpu_online_mask) || cpumask_empty(&mask))
  92. return boot_cpu_id;
  93. else
  94. return cpumask_first(&mask);
  95. }
  96. #else
  97. #define irq_choose_cpu(affinity) boot_cpu_id
  98. #endif
  99. static int leon_set_affinity(struct irq_data *data, const struct cpumask *dest,
  100. bool force)
  101. {
  102. unsigned long mask, oldmask, flags;
  103. int oldcpu, newcpu;
  104. mask = (unsigned long)data->chip_data;
  105. oldcpu = irq_choose_cpu(data->affinity);
  106. newcpu = irq_choose_cpu(dest);
  107. if (oldcpu == newcpu)
  108. goto out;
  109. /* unmask on old CPU first before enabling on the selected CPU */
  110. spin_lock_irqsave(&leon_irq_lock, flags);
  111. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(oldcpu));
  112. LEON3_BYPASS_STORE_PA(LEON_IMASK(oldcpu), (oldmask & ~mask));
  113. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(newcpu));
  114. LEON3_BYPASS_STORE_PA(LEON_IMASK(newcpu), (oldmask | mask));
  115. spin_unlock_irqrestore(&leon_irq_lock, flags);
  116. out:
  117. return IRQ_SET_MASK_OK;
  118. }
  119. static void leon_unmask_irq(struct irq_data *data)
  120. {
  121. unsigned long mask, oldmask, flags;
  122. int cpu;
  123. mask = (unsigned long)data->chip_data;
  124. cpu = irq_choose_cpu(data->affinity);
  125. spin_lock_irqsave(&leon_irq_lock, flags);
  126. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(cpu));
  127. LEON3_BYPASS_STORE_PA(LEON_IMASK(cpu), (oldmask | mask));
  128. spin_unlock_irqrestore(&leon_irq_lock, flags);
  129. }
  130. static void leon_mask_irq(struct irq_data *data)
  131. {
  132. unsigned long mask, oldmask, flags;
  133. int cpu;
  134. mask = (unsigned long)data->chip_data;
  135. cpu = irq_choose_cpu(data->affinity);
  136. spin_lock_irqsave(&leon_irq_lock, flags);
  137. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(cpu));
  138. LEON3_BYPASS_STORE_PA(LEON_IMASK(cpu), (oldmask & ~mask));
  139. spin_unlock_irqrestore(&leon_irq_lock, flags);
  140. }
  141. static unsigned int leon_startup_irq(struct irq_data *data)
  142. {
  143. irq_link(data->irq);
  144. leon_unmask_irq(data);
  145. return 0;
  146. }
  147. static void leon_shutdown_irq(struct irq_data *data)
  148. {
  149. leon_mask_irq(data);
  150. irq_unlink(data->irq);
  151. }
  152. /* Used by external level sensitive IRQ handlers on the LEON: ACK IRQ ctrl */
  153. static void leon_eoi_irq(struct irq_data *data)
  154. {
  155. unsigned long mask = (unsigned long)data->chip_data;
  156. if (mask & LEON_DO_ACK_HW)
  157. LEON3_BYPASS_STORE_PA(LEON_IACK, mask & ~LEON_DO_ACK_HW);
  158. }
  159. static struct irq_chip leon_irq = {
  160. .name = "leon",
  161. .irq_startup = leon_startup_irq,
  162. .irq_shutdown = leon_shutdown_irq,
  163. .irq_mask = leon_mask_irq,
  164. .irq_unmask = leon_unmask_irq,
  165. .irq_eoi = leon_eoi_irq,
  166. .irq_set_affinity = leon_set_affinity,
  167. };
  168. /*
  169. * Build a LEON IRQ for the edge triggered LEON IRQ controller:
  170. * Edge (normal) IRQ - handle_simple_irq, ack=DONT-CARE, never ack
  171. * Level IRQ (PCI|Level-GPIO) - handle_fasteoi_irq, ack=1, ack after ISR
  172. * Per-CPU Edge - handle_percpu_irq, ack=0
  173. */
  174. unsigned int leon_build_device_irq(unsigned int real_irq,
  175. irq_flow_handler_t flow_handler,
  176. const char *name, int do_ack)
  177. {
  178. unsigned int irq;
  179. unsigned long mask;
  180. irq = 0;
  181. mask = get_irqmask(real_irq);
  182. if (mask == 0)
  183. goto out;
  184. irq = irq_alloc(real_irq, real_irq);
  185. if (irq == 0)
  186. goto out;
  187. if (do_ack)
  188. mask |= LEON_DO_ACK_HW;
  189. irq_set_chip_and_handler_name(irq, &leon_irq,
  190. flow_handler, name);
  191. irq_set_chip_data(irq, (void *)mask);
  192. out:
  193. return irq;
  194. }
  195. static unsigned int _leon_build_device_irq(struct platform_device *op,
  196. unsigned int real_irq)
  197. {
  198. return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
  199. }
  200. void leon_update_virq_handling(unsigned int virq,
  201. irq_flow_handler_t flow_handler,
  202. const char *name, int do_ack)
  203. {
  204. unsigned long mask = (unsigned long)irq_get_chip_data(virq);
  205. mask &= ~LEON_DO_ACK_HW;
  206. if (do_ack)
  207. mask |= LEON_DO_ACK_HW;
  208. irq_set_chip_and_handler_name(virq, &leon_irq,
  209. flow_handler, name);
  210. irq_set_chip_data(virq, (void *)mask);
  211. }
  212. void __init leon_init_timers(irq_handler_t counter_fn)
  213. {
  214. int irq, eirq;
  215. struct device_node *rootnp, *np, *nnp;
  216. struct property *pp;
  217. int len;
  218. int icsel;
  219. int ampopts;
  220. int err;
  221. leondebug_irq_disable = 0;
  222. leon_debug_irqout = 0;
  223. master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
  224. dummy_master_l10_counter = 0;
  225. rootnp = of_find_node_by_path("/ambapp0");
  226. if (!rootnp)
  227. goto bad;
  228. /* Find System ID: GRLIB build ID and optional CHIP ID */
  229. pp = of_find_property(rootnp, "systemid", &len);
  230. if (pp)
  231. amba_system_id = *(unsigned long *)pp->value;
  232. /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
  233. np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
  234. if (!np) {
  235. np = of_find_node_by_name(rootnp, "01_00d");
  236. if (!np)
  237. goto bad;
  238. }
  239. pp = of_find_property(np, "reg", &len);
  240. if (!pp)
  241. goto bad;
  242. leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
  243. /* Find GPTIMER Timer Registers base address otherwise bail out. */
  244. nnp = rootnp;
  245. do {
  246. np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
  247. if (!np) {
  248. np = of_find_node_by_name(nnp, "01_011");
  249. if (!np)
  250. goto bad;
  251. }
  252. ampopts = 0;
  253. pp = of_find_property(np, "ampopts", &len);
  254. if (pp) {
  255. ampopts = *(int *)pp->value;
  256. if (ampopts == 0) {
  257. /* Skip this instance, resource already
  258. * allocated by other OS */
  259. nnp = np;
  260. continue;
  261. }
  262. }
  263. /* Select Timer-Instance on Timer Core. Default is zero */
  264. leon3_gptimer_idx = ampopts & 0x7;
  265. pp = of_find_property(np, "reg", &len);
  266. if (pp)
  267. leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
  268. pp->value;
  269. pp = of_find_property(np, "interrupts", &len);
  270. if (pp)
  271. leon3_gptimer_irq = *(unsigned int *)pp->value;
  272. } while (0);
  273. if (!(leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq))
  274. goto bad;
  275. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
  276. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
  277. (((1000000 / HZ) - 1)));
  278. LEON3_BYPASS_STORE_PA(
  279. &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
  280. #ifdef CONFIG_SMP
  281. leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
  282. if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
  283. (1<<LEON3_GPTIMER_SEPIRQ))) {
  284. printk(KERN_ERR "timer not configured with separate irqs\n");
  285. BUG();
  286. }
  287. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].val,
  288. 0);
  289. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
  290. (((1000000/HZ) - 1)));
  291. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  292. 0);
  293. #endif
  294. /*
  295. * The IRQ controller may (if implemented) consist of multiple
  296. * IRQ controllers, each mapped on a 4Kb boundary.
  297. * Each CPU may be routed to different IRQCTRLs, however
  298. * we assume that all CPUs (in SMP system) is routed to the
  299. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  300. * accessed anyway.
  301. * In AMP systems, Linux must run on CPU0 for the time being.
  302. */
  303. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[boot_cpu_id/8]);
  304. icsel = (icsel >> ((7 - (boot_cpu_id&0x7)) * 4)) & 0xf;
  305. leon3_irqctrl_regs += icsel;
  306. /* Mask all IRQs on boot-cpu IRQ controller */
  307. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[boot_cpu_id], 0);
  308. /* Probe extended IRQ controller */
  309. eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
  310. >> 16) & 0xf;
  311. if (eirq != 0)
  312. leon_eirq_setup(eirq);
  313. irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
  314. err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
  315. if (err) {
  316. printk(KERN_ERR "unable to attach timer IRQ%d\n", irq);
  317. prom_halt();
  318. }
  319. #ifdef CONFIG_SMP
  320. {
  321. unsigned long flags;
  322. /*
  323. * In SMP, sun4m adds a IPI handler to IRQ trap handler that
  324. * LEON never must take, sun4d and LEON overwrites the branch
  325. * with a NOP.
  326. */
  327. local_irq_save(flags);
  328. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  329. local_flush_cache_all();
  330. local_irq_restore(flags);
  331. }
  332. #endif
  333. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  334. LEON3_GPTIMER_EN |
  335. LEON3_GPTIMER_RL |
  336. LEON3_GPTIMER_LD |
  337. LEON3_GPTIMER_IRQEN);
  338. #ifdef CONFIG_SMP
  339. /* Install per-cpu IRQ handler for broadcasted ticker */
  340. irq = leon_build_device_irq(leon3_ticker_irq, handle_percpu_irq,
  341. "per-cpu", 0);
  342. err = request_irq(irq, leon_percpu_timer_interrupt,
  343. IRQF_PERCPU | IRQF_TIMER, "ticker",
  344. NULL);
  345. if (err) {
  346. printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
  347. prom_halt();
  348. }
  349. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  350. LEON3_GPTIMER_EN |
  351. LEON3_GPTIMER_RL |
  352. LEON3_GPTIMER_LD |
  353. LEON3_GPTIMER_IRQEN);
  354. #endif
  355. return;
  356. bad:
  357. printk(KERN_ERR "No Timer/irqctrl found\n");
  358. BUG();
  359. return;
  360. }
  361. void leon_clear_clock_irq(void)
  362. {
  363. }
  364. void leon_load_profile_irq(int cpu, unsigned int limit)
  365. {
  366. BUG();
  367. }
  368. void __init leon_trans_init(struct device_node *dp)
  369. {
  370. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  371. struct property *p;
  372. p = of_find_property(dp, "mid", (void *)0);
  373. if (p) {
  374. int mid;
  375. dp->name = prom_early_alloc(5 + 1);
  376. memcpy(&mid, p->value, p->length);
  377. sprintf((char *)dp->name, "cpu%.2d", mid);
  378. }
  379. }
  380. }
  381. void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
  382. void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
  383. {
  384. if (prom_amba_init &&
  385. strcmp(dp->type, "ambapp") == 0 &&
  386. strcmp(dp->name, "ambapp0") == 0) {
  387. prom_amba_init(dp, nextp);
  388. }
  389. }
  390. #ifdef CONFIG_SMP
  391. void leon_set_cpu_int(int cpu, int level)
  392. {
  393. unsigned long mask;
  394. mask = get_irqmask(level);
  395. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
  396. }
  397. static void leon_clear_ipi(int cpu, int level)
  398. {
  399. unsigned long mask;
  400. mask = get_irqmask(level);
  401. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
  402. }
  403. static void leon_set_udt(int cpu)
  404. {
  405. }
  406. void leon_clear_profile_irq(int cpu)
  407. {
  408. }
  409. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  410. {
  411. unsigned long mask, flags, *addr;
  412. mask = get_irqmask(irq_nr);
  413. spin_lock_irqsave(&leon_irq_lock, flags);
  414. addr = (unsigned long *)LEON_IMASK(cpu);
  415. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | mask));
  416. spin_unlock_irqrestore(&leon_irq_lock, flags);
  417. }
  418. #endif
  419. void __init leon_init_IRQ(void)
  420. {
  421. sparc_irq_config.init_timers = leon_init_timers;
  422. sparc_irq_config.build_device_irq = _leon_build_device_irq;
  423. BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
  424. BTFIXUPCALL_NORM);
  425. BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
  426. BTFIXUPCALL_NOP);
  427. #ifdef CONFIG_SMP
  428. BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
  429. BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
  430. BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
  431. #endif
  432. }
  433. void __init leon_init(void)
  434. {
  435. of_pdt_build_more = &leon_node_init;
  436. }