leon_kernel.c 14 KB

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