leon_kernel.c 14 KB

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