leon_kernel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 volatile u32 dummy_master_l10_counter;
  31. unsigned long amba_system_id;
  32. static DEFINE_SPINLOCK(leon_irq_lock);
  33. static unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
  34. static unsigned long leon3_gptimer_ackmask; /* For clearing pending bit */
  35. unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
  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(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. static 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(irq_data_get_affinity_mask(data));
  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(irq_data_get_affinity_mask(data));
  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(irq_data_get_affinity_mask(data));
  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=DON'T-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. struct irq_desc *desc;
  186. irq = 0;
  187. mask = leon_get_irqmask(real_irq);
  188. if (mask == 0)
  189. goto out;
  190. irq = irq_alloc(real_irq, real_irq);
  191. if (irq == 0)
  192. goto out;
  193. if (do_ack)
  194. mask |= LEON_DO_ACK_HW;
  195. desc = irq_to_desc(irq);
  196. if (!desc || !desc->handle_irq || desc->handle_irq == handle_bad_irq) {
  197. irq_set_chip_and_handler_name(irq, &leon_irq,
  198. flow_handler, name);
  199. irq_set_chip_data(irq, (void *)mask);
  200. }
  201. out:
  202. return irq;
  203. }
  204. static unsigned int _leon_build_device_irq(struct platform_device *op,
  205. unsigned int real_irq)
  206. {
  207. return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
  208. }
  209. void leon_update_virq_handling(unsigned int virq,
  210. irq_flow_handler_t flow_handler,
  211. const char *name, int do_ack)
  212. {
  213. unsigned long mask = (unsigned long)irq_get_chip_data(virq);
  214. mask &= ~LEON_DO_ACK_HW;
  215. if (do_ack)
  216. mask |= LEON_DO_ACK_HW;
  217. irq_set_chip_and_handler_name(virq, &leon_irq,
  218. flow_handler, name);
  219. irq_set_chip_data(virq, (void *)mask);
  220. }
  221. static u32 leon_cycles_offset(void)
  222. {
  223. u32 rld, val, ctrl, off;
  224. rld = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld);
  225. val = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val);
  226. ctrl = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl);
  227. if (LEON3_GPTIMER_CTRL_ISPENDING(ctrl)) {
  228. val = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val);
  229. off = 2 * rld - val;
  230. } else {
  231. off = rld - val;
  232. }
  233. return off;
  234. }
  235. #ifdef CONFIG_SMP
  236. /* smp clockevent irq */
  237. static irqreturn_t leon_percpu_timer_ce_interrupt(int irq, void *unused)
  238. {
  239. struct clock_event_device *ce;
  240. int cpu = smp_processor_id();
  241. leon_clear_profile_irq(cpu);
  242. if (cpu == boot_cpu_id)
  243. timer_interrupt(irq, NULL);
  244. ce = &per_cpu(sparc32_clockevent, cpu);
  245. irq_enter();
  246. if (ce->event_handler)
  247. ce->event_handler(ce);
  248. irq_exit();
  249. return IRQ_HANDLED;
  250. }
  251. #endif /* CONFIG_SMP */
  252. void __init leon_init_timers(void)
  253. {
  254. int irq, eirq;
  255. struct device_node *rootnp, *np, *nnp;
  256. struct property *pp;
  257. int len;
  258. int icsel;
  259. int ampopts;
  260. int err;
  261. u32 config;
  262. u32 ctrl;
  263. sparc_config.get_cycles_offset = leon_cycles_offset;
  264. sparc_config.cs_period = 1000000 / HZ;
  265. sparc_config.features |= FEAT_L10_CLOCKSOURCE;
  266. #ifndef CONFIG_SMP
  267. sparc_config.features |= FEAT_L10_CLOCKEVENT;
  268. #endif
  269. leondebug_irq_disable = 0;
  270. leon_debug_irqout = 0;
  271. master_l10_counter = (u32 __iomem *)&dummy_master_l10_counter;
  272. dummy_master_l10_counter = 0;
  273. rootnp = of_find_node_by_path("/ambapp0");
  274. if (!rootnp)
  275. goto bad;
  276. /* Find System ID: GRLIB build ID and optional CHIP ID */
  277. pp = of_find_property(rootnp, "systemid", &len);
  278. if (pp)
  279. amba_system_id = *(unsigned long *)pp->value;
  280. /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
  281. np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
  282. if (!np) {
  283. np = of_find_node_by_name(rootnp, "01_00d");
  284. if (!np)
  285. goto bad;
  286. }
  287. pp = of_find_property(np, "reg", &len);
  288. if (!pp)
  289. goto bad;
  290. leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
  291. /* Find GPTIMER Timer Registers base address otherwise bail out. */
  292. nnp = rootnp;
  293. do {
  294. np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
  295. if (!np) {
  296. np = of_find_node_by_name(nnp, "01_011");
  297. if (!np)
  298. goto bad;
  299. }
  300. ampopts = 0;
  301. pp = of_find_property(np, "ampopts", &len);
  302. if (pp) {
  303. ampopts = *(int *)pp->value;
  304. if (ampopts == 0) {
  305. /* Skip this instance, resource already
  306. * allocated by other OS */
  307. nnp = np;
  308. continue;
  309. }
  310. }
  311. /* Select Timer-Instance on Timer Core. Default is zero */
  312. leon3_gptimer_idx = ampopts & 0x7;
  313. pp = of_find_property(np, "reg", &len);
  314. if (pp)
  315. leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
  316. pp->value;
  317. pp = of_find_property(np, "interrupts", &len);
  318. if (pp)
  319. leon3_gptimer_irq = *(unsigned int *)pp->value;
  320. } while (0);
  321. if (!(leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq))
  322. goto bad;
  323. ctrl = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl);
  324. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  325. ctrl | LEON3_GPTIMER_CTRL_PENDING);
  326. ctrl = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl);
  327. if ((ctrl & LEON3_GPTIMER_CTRL_PENDING) != 0)
  328. leon3_gptimer_ackmask = ~LEON3_GPTIMER_CTRL_PENDING;
  329. else
  330. leon3_gptimer_ackmask = ~0;
  331. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
  332. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
  333. (((1000000 / HZ) - 1)));
  334. LEON3_BYPASS_STORE_PA(
  335. &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
  336. /*
  337. * The IRQ controller may (if implemented) consist of multiple
  338. * IRQ controllers, each mapped on a 4Kb boundary.
  339. * Each CPU may be routed to different IRQCTRLs, however
  340. * we assume that all CPUs (in SMP system) is routed to the
  341. * same IRQ Controller, and for non-SMP only one IRQCTRL is
  342. * accessed anyway.
  343. * In AMP systems, Linux must run on CPU0 for the time being.
  344. */
  345. icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[boot_cpu_id/8]);
  346. icsel = (icsel >> ((7 - (boot_cpu_id&0x7)) * 4)) & 0xf;
  347. leon3_irqctrl_regs += icsel;
  348. /* Mask all IRQs on boot-cpu IRQ controller */
  349. LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[boot_cpu_id], 0);
  350. /* Probe extended IRQ controller */
  351. eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
  352. >> 16) & 0xf;
  353. if (eirq != 0)
  354. leon_eirq_setup(eirq);
  355. #ifdef CONFIG_SMP
  356. {
  357. unsigned long flags;
  358. /*
  359. * In SMP, sun4m adds a IPI handler to IRQ trap handler that
  360. * LEON never must take, sun4d and LEON overwrites the branch
  361. * with a NOP.
  362. */
  363. local_irq_save(flags);
  364. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  365. local_ops->cache_all();
  366. local_irq_restore(flags);
  367. }
  368. #endif
  369. config = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config);
  370. if (config & (1 << LEON3_GPTIMER_SEPIRQ))
  371. leon3_gptimer_irq += leon3_gptimer_idx;
  372. else if ((config & LEON3_GPTIMER_TIMERS) > 1)
  373. pr_warn("GPTIMER uses shared irqs, using other timers of the same core will fail.\n");
  374. #ifdef CONFIG_SMP
  375. /* Install per-cpu IRQ handler for broadcasted ticker */
  376. irq = leon_build_device_irq(leon3_gptimer_irq, handle_percpu_irq,
  377. "per-cpu", 0);
  378. err = request_irq(irq, leon_percpu_timer_ce_interrupt,
  379. IRQF_PERCPU | IRQF_TIMER, "timer", NULL);
  380. #else
  381. irq = _leon_build_device_irq(NULL, leon3_gptimer_irq);
  382. err = request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL);
  383. #endif
  384. if (err) {
  385. pr_err("Unable to attach timer IRQ%d\n", irq);
  386. prom_halt();
  387. }
  388. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  389. LEON3_GPTIMER_EN |
  390. LEON3_GPTIMER_RL |
  391. LEON3_GPTIMER_LD |
  392. LEON3_GPTIMER_IRQEN);
  393. return;
  394. bad:
  395. printk(KERN_ERR "No Timer/irqctrl found\n");
  396. BUG();
  397. return;
  398. }
  399. static void leon_clear_clock_irq(void)
  400. {
  401. u32 ctrl;
  402. ctrl = LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl);
  403. LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
  404. ctrl & leon3_gptimer_ackmask);
  405. }
  406. static void leon_load_profile_irq(int cpu, unsigned int limit)
  407. {
  408. }
  409. void __init leon_trans_init(struct device_node *dp)
  410. {
  411. if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
  412. struct property *p;
  413. p = of_find_property(dp, "mid", (void *)0);
  414. if (p) {
  415. int mid;
  416. dp->name = prom_early_alloc(5 + 1);
  417. memcpy(&mid, p->value, p->length);
  418. sprintf((char *)dp->name, "cpu%.2d", mid);
  419. }
  420. }
  421. }
  422. #ifdef CONFIG_SMP
  423. void leon_clear_profile_irq(int cpu)
  424. {
  425. }
  426. void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
  427. {
  428. unsigned long mask, flags, *addr;
  429. mask = leon_get_irqmask(irq_nr);
  430. spin_lock_irqsave(&leon_irq_lock, flags);
  431. addr = (unsigned long *)LEON_IMASK(cpu);
  432. LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | mask));
  433. spin_unlock_irqrestore(&leon_irq_lock, flags);
  434. }
  435. #endif
  436. void __init leon_init_IRQ(void)
  437. {
  438. sparc_config.init_timers = leon_init_timers;
  439. sparc_config.build_device_irq = _leon_build_device_irq;
  440. sparc_config.clock_rate = 1000000;
  441. sparc_config.clear_clock_irq = leon_clear_clock_irq;
  442. sparc_config.load_profile_irq = leon_load_profile_irq;
  443. }