leon_kernel.c 13 KB

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