leon_kernel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. int cpu = sparc_leon3_cpuid();
  52. eirq = leon_eirq_get(cpu);
  53. if ((eirq & 0x10) && irq_map[eirq]->irq) /* bit4 tells if IRQ happened */
  54. generic_handle_irq(irq_map[eirq]->irq);
  55. }
  56. /* The extended IRQ controller has been found, this function registers it */
  57. void leon_eirq_setup(unsigned int eirq)
  58. {
  59. unsigned long mask, oldmask;
  60. unsigned int veirq;
  61. if (eirq < 1 || eirq > 0xf) {
  62. printk(KERN_ERR "LEON EXT IRQ NUMBER BAD: %d\n", eirq);
  63. return;
  64. }
  65. veirq = leon_build_device_irq(eirq, leon_handle_ext_irq, "extirq", 0);
  66. /*
  67. * Unmask the Extended IRQ, the IRQs routed through the Ext-IRQ
  68. * controller have a mask-bit of their own, so this is safe.
  69. */
  70. irq_link(veirq);
  71. mask = 1 << eirq;
  72. oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(boot_cpu_id));
  73. LEON3_BYPASS_STORE_PA(LEON_IMASK(boot_cpu_id), (oldmask | mask));
  74. sparc_leon_eirq = eirq;
  75. }
  76. unsigned long leon_get_irqmask(unsigned int irq)
  77. {
  78. unsigned long mask;
  79. if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
  80. || ((irq > 0x1f) && sparc_leon_eirq)) {
  81. printk(KERN_ERR
  82. "leon_get_irqmask: false irq number: %d\n", irq);
  83. mask = 0;
  84. } else {
  85. mask = LEON_HARD_INT(irq);
  86. }
  87. return mask;
  88. }
  89. #ifdef CONFIG_SMP
  90. static int irq_choose_cpu(const struct cpumask *affinity)
  91. {
  92. cpumask_t mask;
  93. cpumask_and(&mask, cpu_online_mask, affinity);
  94. if (cpumask_equal(&mask, cpu_online_mask) || cpumask_empty(&mask))
  95. return boot_cpu_id;
  96. else
  97. return cpumask_first(&mask);
  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(data->affinity);
  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(data->affinity);
  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(data->affinity);
  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=DONT-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. irq = 0;
  184. mask = leon_get_irqmask(real_irq);
  185. if (mask == 0)
  186. goto out;
  187. irq = irq_alloc(real_irq, real_irq);
  188. if (irq == 0)
  189. goto out;
  190. if (do_ack)
  191. mask |= LEON_DO_ACK_HW;
  192. irq_set_chip_and_handler_name(irq, &leon_irq,
  193. flow_handler, name);
  194. irq_set_chip_data(irq, (void *)mask);
  195. out:
  196. return irq;
  197. }
  198. static unsigned int _leon_build_device_irq(struct platform_device *op,
  199. unsigned int real_irq)
  200. {
  201. return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
  202. }
  203. void leon_update_virq_handling(unsigned int virq,
  204. irq_flow_handler_t flow_handler,
  205. const char *name, int do_ack)
  206. {
  207. unsigned long mask = (unsigned long)irq_get_chip_data(virq);
  208. mask &= ~LEON_DO_ACK_HW;
  209. if (do_ack)
  210. mask |= LEON_DO_ACK_HW;
  211. irq_set_chip_and_handler_name(virq, &leon_irq,
  212. flow_handler, name);
  213. irq_set_chip_data(virq, (void *)mask);
  214. }
  215. static u32 leon_cycles_offset(void)
  216. {
  217. u32 rld, val, off;
  218. rld = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld);
  219. val = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val);
  220. off = rld - val;
  221. return rld - val;
  222. }
  223. #ifdef CONFIG_SMP
  224. /* smp clockevent irq */
  225. irqreturn_t leon_percpu_timer_ce_interrupt(int irq, void *unused)
  226. {
  227. struct clock_event_device *ce;
  228. int cpu = smp_processor_id();
  229. leon_clear_profile_irq(cpu);
  230. ce = &per_cpu(sparc32_clockevent, cpu);
  231. irq_enter();
  232. if (ce->event_handler)
  233. ce->event_handler(ce);
  234. irq_exit();
  235. return IRQ_HANDLED;
  236. }
  237. #endif /* CONFIG_SMP */
  238. void __init leon_init_timers(void)
  239. {
  240. int irq, eirq;
  241. struct device_node *rootnp, *np, *nnp;
  242. struct property *pp;
  243. int len;
  244. int icsel;
  245. int ampopts;
  246. int err;
  247. sparc_config.get_cycles_offset = leon_cycles_offset;
  248. sparc_config.cs_period = 1000000 / HZ;
  249. sparc_config.features |= FEAT_L10_CLOCKSOURCE;
  250. #ifndef CONFIG_SMP
  251. sparc_config.features |= FEAT_L10_CLOCKEVENT;
  252. #endif
  253. leondebug_irq_disable = 0;
  254. leon_debug_irqout = 0;
  255. master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
  256. dummy_master_l10_counter = 0;
  257. rootnp = of_find_node_by_path("/ambapp0");
  258. if (!rootnp)
  259. goto bad;
  260. /* Find System ID: GRLIB build ID and optional CHIP ID */
  261. pp = of_find_property(rootnp, "systemid", &len);
  262. if (pp)
  263. amba_system_id = *(unsigned long *)pp->value;
  264. /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
  265. np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
  266. if (!np) {
  267. np = of_find_node_by_name(rootnp, "01_00d");
  268. if (!np)
  269. goto bad;
  270. }
  271. pp = of_find_property(np, "reg", &len);
  272. if (!pp)
  273. goto bad;
  274. leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
  275. /* Find GPTIMER Timer Registers base address otherwise bail out. */
  276. nnp = rootnp;
  277. do {
  278. np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
  279. if (!np) {
  280. np = of_find_node_by_name(nnp, "01_011");
  281. if (!np)
  282. goto bad;
  283. }
  284. ampopts = 0;
  285. pp = of_find_property(np, "ampopts", &len);
  286. if (pp) {
  287. ampopts = *(int *)pp->value;
  288. if (ampopts == 0) {
  289. /* Skip this instance, resource already
  290. * allocated by other OS */
  291. nnp = np;
  292. continue;
  293. }
  294. }
  295. /* Select Timer-Instance on Timer Core. Default is zero */
  296. leon3_gptimer_idx = ampopts & 0x7;
  297. pp = of_find_property(np, "reg", &len);
  298. if (pp)
  299. leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
  300. pp->value;
  301. pp = of_find_property(np, "interrupts", &len);
  302. if (pp)
  303. leon3_gptimer_irq = *(unsigned int *)pp->value;
  304. } while (0);
  305. if (!(leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq))
  306. goto bad;
  307. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
  308. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
  309. (((1000000 / HZ) - 1)));
  310. LEON3_BYPASS_STORE_PA(
  311. &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
  312. #ifdef CONFIG_SMP
  313. leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
  314. if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
  315. (1<<LEON3_GPTIMER_SEPIRQ))) {
  316. printk(KERN_ERR "timer not configured with separate irqs\n");
  317. BUG();
  318. }
  319. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].val,
  320. 0);
  321. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
  322. (((1000000/HZ) - 1)));
  323. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  324. 0);
  325. #endif
  326. /*
  327. * The IRQ controller may (if implemented) consist of multiple
  328. * IRQ controllers, each mapped on a 4Kb boundary.
  329. * Each CPU may be routed to different IRQCTRLs, however
  330. * we assume that all CPUs (in SMP system) is routed to the
  331. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  332. * accessed anyway.
  333. * In AMP systems, Linux must run on CPU0 for the time being.
  334. */
  335. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[boot_cpu_id/8]);
  336. icsel = (icsel >> ((7 - (boot_cpu_id&0x7)) * 4)) & 0xf;
  337. leon3_irqctrl_regs += icsel;
  338. /* Mask all IRQs on boot-cpu IRQ controller */
  339. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[boot_cpu_id], 0);
  340. /* Probe extended IRQ controller */
  341. eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
  342. >> 16) & 0xf;
  343. if (eirq != 0)
  344. leon_eirq_setup(eirq);
  345. irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
  346. err = request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL);
  347. if (err) {
  348. printk(KERN_ERR "unable to attach timer IRQ%d\n", irq);
  349. prom_halt();
  350. }
  351. #ifdef CONFIG_SMP
  352. {
  353. unsigned long flags;
  354. /*
  355. * In SMP, sun4m adds a IPI handler to IRQ trap handler that
  356. * LEON never must take, sun4d and LEON overwrites the branch
  357. * with a NOP.
  358. */
  359. local_irq_save(flags);
  360. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  361. local_ops->cache_all();
  362. local_irq_restore(flags);
  363. }
  364. #endif
  365. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  366. LEON3_GPTIMER_EN |
  367. LEON3_GPTIMER_RL |
  368. LEON3_GPTIMER_LD |
  369. LEON3_GPTIMER_IRQEN);
  370. #ifdef CONFIG_SMP
  371. /* Install per-cpu IRQ handler for broadcasted ticker */
  372. irq = leon_build_device_irq(leon3_ticker_irq, handle_percpu_irq,
  373. "per-cpu", 0);
  374. err = request_irq(irq, leon_percpu_timer_ce_interrupt,
  375. IRQF_PERCPU | IRQF_TIMER, "ticker",
  376. NULL);
  377. if (err) {
  378. printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
  379. prom_halt();
  380. }
  381. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
  382. LEON3_GPTIMER_EN |
  383. LEON3_GPTIMER_RL |
  384. LEON3_GPTIMER_LD |
  385. LEON3_GPTIMER_IRQEN);
  386. #endif
  387. return;
  388. bad:
  389. printk(KERN_ERR "No Timer/irqctrl found\n");
  390. BUG();
  391. return;
  392. }
  393. static void leon_clear_clock_irq(void)
  394. {
  395. }
  396. static void leon_load_profile_irq(int cpu, unsigned int limit)
  397. {
  398. }
  399. void __init leon_trans_init(struct device_node *dp)
  400. {
  401. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  402. struct property *p;
  403. p = of_find_property(dp, "mid", (void *)0);
  404. if (p) {
  405. int mid;
  406. dp->name = prom_early_alloc(5 + 1);
  407. memcpy(&mid, p->value, p->length);
  408. sprintf((char *)dp->name, "cpu%.2d", mid);
  409. }
  410. }
  411. }
  412. #ifdef CONFIG_SMP
  413. void leon_clear_profile_irq(int cpu)
  414. {
  415. }
  416. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  417. {
  418. unsigned long mask, flags, *addr;
  419. mask = leon_get_irqmask(irq_nr);
  420. spin_lock_irqsave(&leon_irq_lock, flags);
  421. addr = (unsigned long *)LEON_IMASK(cpu);
  422. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | mask));
  423. spin_unlock_irqrestore(&leon_irq_lock, flags);
  424. }
  425. #endif
  426. void __init leon_init_IRQ(void)
  427. {
  428. sparc_config.init_timers = leon_init_timers;
  429. sparc_config.build_device_irq = _leon_build_device_irq;
  430. sparc_config.clock_rate = 1000000;
  431. sparc_config.clear_clock_irq = leon_clear_clock_irq;
  432. sparc_config.load_profile_irq = leon_load_profile_irq;
  433. }