leon_kernel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 <linux/clocksource.h>
  13. #include <linux/clockchips.h>
  14. #include <asm/oplib.h>
  15. #include <asm/timer.h>
  16. #include <asm/prom.h>
  17. #include <asm/leon.h>
  18. #include <asm/leon_amba.h>
  19. #include <asm/traps.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/smp.h>
  22. #include <asm/setup.h>
  23. #include "kernel.h"
  24. #include "prom.h"
  25. #include "irq.h"
  26. struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
  27. struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
  28. int leondebug_irq_disable;
  29. int leon_debug_irqout;
  30. static int dummy_master_l10_counter;
  31. unsigned long amba_system_id;
  32. static DEFINE_SPINLOCK(leon_irq_lock);
  33. unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
  34. unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
  35. int leon3_ticker_irq; /* Timer ticker IRQ */
  36. unsigned int sparc_leon_eirq;
  37. #define LEON_IMASK(cpu) (&leon3_irqctrl_regs->mask[cpu])
  38. #define LEON_IACK (&leon3_irqctrl_regs->iclear)
  39. #define LEON_DO_ACK_HW 1
  40. /* Return the last ACKed IRQ by the Extended IRQ controller. It has already
  41. * been (automatically) ACKed when the CPU takes the trap.
  42. */
  43. static inline unsigned int leon_eirq_get(int cpu)
  44. {
  45. return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
  46. }
  47. /* Handle one or multiple IRQs from the extended interrupt controller */
  48. static void leon_handle_ext_irq(unsigned int irq, struct irq_desc *desc)
  49. {
  50. unsigned int eirq;
  51. struct irq_bucket *p;
  52. int cpu = sparc_leon3_cpuid();
  53. eirq = leon_eirq_get(cpu);
  54. p = irq_map[eirq];
  55. if ((eirq & 0x10) && p && p->irq) /* bit4 tells if IRQ happened */
  56. generic_handle_irq(p->irq);
  57. }
  58. /* The extended IRQ controller has been found, this function registers it */
  59. void leon_eirq_setup(unsigned int eirq)
  60. {
  61. unsigned long mask, oldmask;
  62. unsigned int veirq;
  63. if (eirq < 1 || eirq > 0xf) {
  64. printk(KERN_ERR "LEON EXT IRQ NUMBER BAD: %d\n", eirq);
  65. return;
  66. }
  67. veirq = leon_build_device_irq(eirq, leon_handle_ext_irq, "extirq", 0);
  68. /*
  69. * Unmask the Extended IRQ, the IRQs routed through the Ext-IRQ
  70. * controller have a mask-bit of their own, so this is safe.
  71. */
  72. irq_link(veirq);
  73. mask = 1 << eirq;
  74. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(boot_cpu_id));
  75. LEON3_BYPASS_STORE_PA(LEON_IMASK(boot_cpu_id), (oldmask | mask));
  76. sparc_leon_eirq = eirq;
  77. }
  78. unsigned long leon_get_irqmask(unsigned int irq)
  79. {
  80. unsigned long mask;
  81. if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
  82. || ((irq > 0x1f) && sparc_leon_eirq)) {
  83. printk(KERN_ERR
  84. "leon_get_irqmask: false irq number: %d\n", irq);
  85. mask = 0;
  86. } else {
  87. mask = LEON_HARD_INT(irq);
  88. }
  89. return mask;
  90. }
  91. #ifdef CONFIG_SMP
  92. static int irq_choose_cpu(const struct cpumask *affinity)
  93. {
  94. cpumask_t mask;
  95. cpumask_and(&mask, cpu_online_mask, affinity);
  96. if (cpumask_equal(&mask, cpu_online_mask) || cpumask_empty(&mask))
  97. return boot_cpu_id;
  98. else
  99. return cpumask_first(&mask);
  100. }
  101. #else
  102. #define irq_choose_cpu(affinity) boot_cpu_id
  103. #endif
  104. static int leon_set_affinity(struct irq_data *data, const struct cpumask *dest,
  105. bool force)
  106. {
  107. unsigned long mask, oldmask, flags;
  108. int oldcpu, newcpu;
  109. mask = (unsigned long)data->chip_data;
  110. oldcpu = irq_choose_cpu(data->affinity);
  111. newcpu = irq_choose_cpu(dest);
  112. if (oldcpu == newcpu)
  113. goto out;
  114. /* unmask on old CPU first before enabling on the selected CPU */
  115. spin_lock_irqsave(&leon_irq_lock, flags);
  116. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(oldcpu));
  117. LEON3_BYPASS_STORE_PA(LEON_IMASK(oldcpu), (oldmask & ~mask));
  118. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(newcpu));
  119. LEON3_BYPASS_STORE_PA(LEON_IMASK(newcpu), (oldmask | mask));
  120. spin_unlock_irqrestore(&leon_irq_lock, flags);
  121. out:
  122. return IRQ_SET_MASK_OK;
  123. }
  124. static void leon_unmask_irq(struct irq_data *data)
  125. {
  126. unsigned long mask, oldmask, flags;
  127. int cpu;
  128. mask = (unsigned long)data->chip_data;
  129. cpu = irq_choose_cpu(data->affinity);
  130. spin_lock_irqsave(&leon_irq_lock, flags);
  131. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(cpu));
  132. LEON3_BYPASS_STORE_PA(LEON_IMASK(cpu), (oldmask | mask));
  133. spin_unlock_irqrestore(&leon_irq_lock, flags);
  134. }
  135. static void leon_mask_irq(struct irq_data *data)
  136. {
  137. unsigned long mask, oldmask, flags;
  138. int cpu;
  139. mask = (unsigned long)data->chip_data;
  140. cpu = irq_choose_cpu(data->affinity);
  141. spin_lock_irqsave(&leon_irq_lock, flags);
  142. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(cpu));
  143. LEON3_BYPASS_STORE_PA(LEON_IMASK(cpu), (oldmask & ~mask));
  144. spin_unlock_irqrestore(&leon_irq_lock, flags);
  145. }
  146. static unsigned int leon_startup_irq(struct irq_data *data)
  147. {
  148. irq_link(data->irq);
  149. leon_unmask_irq(data);
  150. return 0;
  151. }
  152. static void leon_shutdown_irq(struct irq_data *data)
  153. {
  154. leon_mask_irq(data);
  155. irq_unlink(data->irq);
  156. }
  157. /* Used by external level sensitive IRQ handlers on the LEON: ACK IRQ ctrl */
  158. static void leon_eoi_irq(struct irq_data *data)
  159. {
  160. unsigned long mask = (unsigned long)data->chip_data;
  161. if (mask & LEON_DO_ACK_HW)
  162. LEON3_BYPASS_STORE_PA(LEON_IACK, mask & ~LEON_DO_ACK_HW);
  163. }
  164. static struct irq_chip leon_irq = {
  165. .name = "leon",
  166. .irq_startup = leon_startup_irq,
  167. .irq_shutdown = leon_shutdown_irq,
  168. .irq_mask = leon_mask_irq,
  169. .irq_unmask = leon_unmask_irq,
  170. .irq_eoi = leon_eoi_irq,
  171. .irq_set_affinity = leon_set_affinity,
  172. };
  173. /*
  174. * Build a LEON IRQ for the edge triggered LEON IRQ controller:
  175. * Edge (normal) IRQ - handle_simple_irq, ack=DONT-CARE, never ack
  176. * Level IRQ (PCI|Level-GPIO) - handle_fasteoi_irq, ack=1, ack after ISR
  177. * Per-CPU Edge - handle_percpu_irq, ack=0
  178. */
  179. unsigned int leon_build_device_irq(unsigned int real_irq,
  180. irq_flow_handler_t flow_handler,
  181. const char *name, int do_ack)
  182. {
  183. unsigned int irq;
  184. unsigned long mask;
  185. irq = 0;
  186. mask = leon_get_irqmask(real_irq);
  187. if (mask == 0)
  188. goto out;
  189. irq = irq_alloc(real_irq, real_irq);
  190. if (irq == 0)
  191. goto out;
  192. if (do_ack)
  193. mask |= LEON_DO_ACK_HW;
  194. irq_set_chip_and_handler_name(irq, &leon_irq,
  195. flow_handler, name);
  196. irq_set_chip_data(irq, (void *)mask);
  197. out:
  198. return irq;
  199. }
  200. static unsigned int _leon_build_device_irq(struct platform_device *op,
  201. unsigned int real_irq)
  202. {
  203. return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
  204. }
  205. void leon_update_virq_handling(unsigned int virq,
  206. irq_flow_handler_t flow_handler,
  207. const char *name, int do_ack)
  208. {
  209. unsigned long mask = (unsigned long)irq_get_chip_data(virq);
  210. mask &= ~LEON_DO_ACK_HW;
  211. if (do_ack)
  212. mask |= LEON_DO_ACK_HW;
  213. irq_set_chip_and_handler_name(virq, &leon_irq,
  214. flow_handler, name);
  215. irq_set_chip_data(virq, (void *)mask);
  216. }
  217. static u32 leon_cycles_offset(void)
  218. {
  219. u32 rld, val, off;
  220. rld = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld);
  221. val = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val);
  222. off = rld - val;
  223. return rld - val;
  224. }
  225. #ifdef CONFIG_SMP
  226. /* smp clockevent irq */
  227. irqreturn_t leon_percpu_timer_ce_interrupt(int irq, void *unused)
  228. {
  229. struct clock_event_device *ce;
  230. int cpu = smp_processor_id();
  231. leon_clear_profile_irq(cpu);
  232. ce = &per_cpu(sparc32_clockevent, cpu);
  233. irq_enter();
  234. if (ce->event_handler)
  235. ce->event_handler(ce);
  236. irq_exit();
  237. return IRQ_HANDLED;
  238. }
  239. #endif /* CONFIG_SMP */
  240. void __init leon_init_timers(void)
  241. {
  242. int irq, eirq;
  243. struct device_node *rootnp, *np, *nnp;
  244. struct property *pp;
  245. int len;
  246. int icsel;
  247. int ampopts;
  248. int err;
  249. sparc_config.get_cycles_offset = leon_cycles_offset;
  250. sparc_config.cs_period = 1000000 / HZ;
  251. sparc_config.features |= FEAT_L10_CLOCKSOURCE;
  252. #ifndef CONFIG_SMP
  253. sparc_config.features |= FEAT_L10_CLOCKEVENT;
  254. #endif
  255. leondebug_irq_disable = 0;
  256. leon_debug_irqout = 0;
  257. master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
  258. dummy_master_l10_counter = 0;
  259. rootnp = of_find_node_by_path("/ambapp0");
  260. if (!rootnp)
  261. goto bad;
  262. /* Find System ID: GRLIB build ID and optional CHIP ID */
  263. pp = of_find_property(rootnp, "systemid", &len);
  264. if (pp)
  265. amba_system_id = *(unsigned long *)pp->value;
  266. /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
  267. np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
  268. if (!np) {
  269. np = of_find_node_by_name(rootnp, "01_00d");
  270. if (!np)
  271. goto bad;
  272. }
  273. pp = of_find_property(np, "reg", &len);
  274. if (!pp)
  275. goto bad;
  276. leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
  277. /* Find GPTIMER Timer Registers base address otherwise bail out. */
  278. nnp = rootnp;
  279. do {
  280. np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
  281. if (!np) {
  282. np = of_find_node_by_name(nnp, "01_011");
  283. if (!np)
  284. goto bad;
  285. }
  286. ampopts = 0;
  287. pp = of_find_property(np, "ampopts", &len);
  288. if (pp) {
  289. ampopts = *(int *)pp->value;
  290. if (ampopts == 0) {
  291. /* Skip this instance, resource already
  292. * allocated by other OS */
  293. nnp = np;
  294. continue;
  295. }
  296. }
  297. /* Select Timer-Instance on Timer Core. Default is zero */
  298. leon3_gptimer_idx = ampopts & 0x7;
  299. pp = of_find_property(np, "reg", &len);
  300. if (pp)
  301. leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
  302. pp->value;
  303. pp = of_find_property(np, "interrupts", &len);
  304. if (pp)
  305. leon3_gptimer_irq = *(unsigned int *)pp->value;
  306. } while (0);
  307. if (!(leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq))
  308. goto bad;
  309. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
  310. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
  311. (((1000000 / HZ) - 1)));
  312. LEON3_BYPASS_STORE_PA(
  313. &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
  314. #ifdef CONFIG_SMP
  315. leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
  316. if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
  317. (1<<LEON3_GPTIMER_SEPIRQ))) {
  318. printk(KERN_ERR "timer not configured with separate irqs\n");
  319. BUG();
  320. }
  321. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].val,
  322. 0);
  323. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
  324. (((1000000/HZ) - 1)));
  325. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  326. 0);
  327. #endif
  328. /*
  329. * The IRQ controller may (if implemented) consist of multiple
  330. * IRQ controllers, each mapped on a 4Kb boundary.
  331. * Each CPU may be routed to different IRQCTRLs, however
  332. * we assume that all CPUs (in SMP system) is routed to the
  333. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  334. * accessed anyway.
  335. * In AMP systems, Linux must run on CPU0 for the time being.
  336. */
  337. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[boot_cpu_id/8]);
  338. icsel = (icsel >> ((7 - (boot_cpu_id&0x7)) * 4)) & 0xf;
  339. leon3_irqctrl_regs += icsel;
  340. /* Mask all IRQs on boot-cpu IRQ controller */
  341. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[boot_cpu_id], 0);
  342. /* Probe extended IRQ controller */
  343. eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
  344. >> 16) & 0xf;
  345. if (eirq != 0)
  346. leon_eirq_setup(eirq);
  347. irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
  348. err = request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL);
  349. if (err) {
  350. printk(KERN_ERR "unable to attach timer IRQ%d\n", irq);
  351. prom_halt();
  352. }
  353. #ifdef CONFIG_SMP
  354. {
  355. unsigned long flags;
  356. /*
  357. * In SMP, sun4m adds a IPI handler to IRQ trap handler that
  358. * LEON never must take, sun4d and LEON overwrites the branch
  359. * with a NOP.
  360. */
  361. local_irq_save(flags);
  362. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  363. local_ops->cache_all();
  364. local_irq_restore(flags);
  365. }
  366. #endif
  367. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  368. LEON3_GPTIMER_EN |
  369. LEON3_GPTIMER_RL |
  370. LEON3_GPTIMER_LD |
  371. LEON3_GPTIMER_IRQEN);
  372. #ifdef CONFIG_SMP
  373. /* Install per-cpu IRQ handler for broadcasted ticker */
  374. irq = leon_build_device_irq(leon3_ticker_irq, handle_percpu_irq,
  375. "per-cpu", 0);
  376. err = request_irq(irq, leon_percpu_timer_ce_interrupt,
  377. IRQF_PERCPU | IRQF_TIMER, "ticker",
  378. NULL);
  379. if (err) {
  380. printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
  381. prom_halt();
  382. }
  383. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  384. LEON3_GPTIMER_EN |
  385. LEON3_GPTIMER_RL |
  386. LEON3_GPTIMER_LD |
  387. LEON3_GPTIMER_IRQEN);
  388. #endif
  389. return;
  390. bad:
  391. printk(KERN_ERR "No Timer/irqctrl found\n");
  392. BUG();
  393. return;
  394. }
  395. static void leon_clear_clock_irq(void)
  396. {
  397. }
  398. static void leon_load_profile_irq(int cpu, unsigned int limit)
  399. {
  400. }
  401. void __init leon_trans_init(struct device_node *dp)
  402. {
  403. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  404. struct property *p;
  405. p = of_find_property(dp, "mid", (void *)0);
  406. if (p) {
  407. int mid;
  408. dp->name = prom_early_alloc(5 + 1);
  409. memcpy(&mid, p->value, p->length);
  410. sprintf((char *)dp->name, "cpu%.2d", mid);
  411. }
  412. }
  413. }
  414. #ifdef CONFIG_SMP
  415. void leon_clear_profile_irq(int cpu)
  416. {
  417. }
  418. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  419. {
  420. unsigned long mask, flags, *addr;
  421. mask = leon_get_irqmask(irq_nr);
  422. spin_lock_irqsave(&leon_irq_lock, flags);
  423. addr = (unsigned long *)LEON_IMASK(cpu);
  424. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | mask));
  425. spin_unlock_irqrestore(&leon_irq_lock, flags);
  426. }
  427. #endif
  428. void __init leon_init_IRQ(void)
  429. {
  430. sparc_config.init_timers = leon_init_timers;
  431. sparc_config.build_device_irq = _leon_build_device_irq;
  432. sparc_config.clock_rate = 1000000;
  433. sparc_config.clear_clock_irq = leon_clear_clock_irq;
  434. sparc_config.load_profile_irq = leon_load_profile_irq;
  435. }