qe_ic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * arch/powerpc/sysdev/qe_lib/qe_ic.c
  3. *
  4. * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
  5. *
  6. * Author: Li Yang <leoli@freescale.com>
  7. * Based on code from Shlomi Gridish <gridish@freescale.com>
  8. *
  9. * QUICC ENGINE Interrupt Controller
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/reboot.h>
  20. #include <linux/slab.h>
  21. #include <linux/stddef.h>
  22. #include <linux/sched.h>
  23. #include <linux/signal.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/device.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/spinlock.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/prom.h>
  31. #include <asm/qe_ic.h>
  32. #include "qe_ic.h"
  33. static DEFINE_RAW_SPINLOCK(qe_ic_lock);
  34. static struct qe_ic_info qe_ic_info[] = {
  35. [1] = {
  36. .mask = 0x00008000,
  37. .mask_reg = QEIC_CIMR,
  38. .pri_code = 0,
  39. .pri_reg = QEIC_CIPWCC,
  40. },
  41. [2] = {
  42. .mask = 0x00004000,
  43. .mask_reg = QEIC_CIMR,
  44. .pri_code = 1,
  45. .pri_reg = QEIC_CIPWCC,
  46. },
  47. [3] = {
  48. .mask = 0x00002000,
  49. .mask_reg = QEIC_CIMR,
  50. .pri_code = 2,
  51. .pri_reg = QEIC_CIPWCC,
  52. },
  53. [10] = {
  54. .mask = 0x00000040,
  55. .mask_reg = QEIC_CIMR,
  56. .pri_code = 1,
  57. .pri_reg = QEIC_CIPZCC,
  58. },
  59. [11] = {
  60. .mask = 0x00000020,
  61. .mask_reg = QEIC_CIMR,
  62. .pri_code = 2,
  63. .pri_reg = QEIC_CIPZCC,
  64. },
  65. [12] = {
  66. .mask = 0x00000010,
  67. .mask_reg = QEIC_CIMR,
  68. .pri_code = 3,
  69. .pri_reg = QEIC_CIPZCC,
  70. },
  71. [13] = {
  72. .mask = 0x00000008,
  73. .mask_reg = QEIC_CIMR,
  74. .pri_code = 4,
  75. .pri_reg = QEIC_CIPZCC,
  76. },
  77. [14] = {
  78. .mask = 0x00000004,
  79. .mask_reg = QEIC_CIMR,
  80. .pri_code = 5,
  81. .pri_reg = QEIC_CIPZCC,
  82. },
  83. [15] = {
  84. .mask = 0x00000002,
  85. .mask_reg = QEIC_CIMR,
  86. .pri_code = 6,
  87. .pri_reg = QEIC_CIPZCC,
  88. },
  89. [20] = {
  90. .mask = 0x10000000,
  91. .mask_reg = QEIC_CRIMR,
  92. .pri_code = 3,
  93. .pri_reg = QEIC_CIPRTA,
  94. },
  95. [25] = {
  96. .mask = 0x00800000,
  97. .mask_reg = QEIC_CRIMR,
  98. .pri_code = 0,
  99. .pri_reg = QEIC_CIPRTB,
  100. },
  101. [26] = {
  102. .mask = 0x00400000,
  103. .mask_reg = QEIC_CRIMR,
  104. .pri_code = 1,
  105. .pri_reg = QEIC_CIPRTB,
  106. },
  107. [27] = {
  108. .mask = 0x00200000,
  109. .mask_reg = QEIC_CRIMR,
  110. .pri_code = 2,
  111. .pri_reg = QEIC_CIPRTB,
  112. },
  113. [28] = {
  114. .mask = 0x00100000,
  115. .mask_reg = QEIC_CRIMR,
  116. .pri_code = 3,
  117. .pri_reg = QEIC_CIPRTB,
  118. },
  119. [32] = {
  120. .mask = 0x80000000,
  121. .mask_reg = QEIC_CIMR,
  122. .pri_code = 0,
  123. .pri_reg = QEIC_CIPXCC,
  124. },
  125. [33] = {
  126. .mask = 0x40000000,
  127. .mask_reg = QEIC_CIMR,
  128. .pri_code = 1,
  129. .pri_reg = QEIC_CIPXCC,
  130. },
  131. [34] = {
  132. .mask = 0x20000000,
  133. .mask_reg = QEIC_CIMR,
  134. .pri_code = 2,
  135. .pri_reg = QEIC_CIPXCC,
  136. },
  137. [35] = {
  138. .mask = 0x10000000,
  139. .mask_reg = QEIC_CIMR,
  140. .pri_code = 3,
  141. .pri_reg = QEIC_CIPXCC,
  142. },
  143. [36] = {
  144. .mask = 0x08000000,
  145. .mask_reg = QEIC_CIMR,
  146. .pri_code = 4,
  147. .pri_reg = QEIC_CIPXCC,
  148. },
  149. [40] = {
  150. .mask = 0x00800000,
  151. .mask_reg = QEIC_CIMR,
  152. .pri_code = 0,
  153. .pri_reg = QEIC_CIPYCC,
  154. },
  155. [41] = {
  156. .mask = 0x00400000,
  157. .mask_reg = QEIC_CIMR,
  158. .pri_code = 1,
  159. .pri_reg = QEIC_CIPYCC,
  160. },
  161. [42] = {
  162. .mask = 0x00200000,
  163. .mask_reg = QEIC_CIMR,
  164. .pri_code = 2,
  165. .pri_reg = QEIC_CIPYCC,
  166. },
  167. [43] = {
  168. .mask = 0x00100000,
  169. .mask_reg = QEIC_CIMR,
  170. .pri_code = 3,
  171. .pri_reg = QEIC_CIPYCC,
  172. },
  173. };
  174. static inline u32 qe_ic_read(volatile __be32 __iomem * base, unsigned int reg)
  175. {
  176. return in_be32(base + (reg >> 2));
  177. }
  178. static inline void qe_ic_write(volatile __be32 __iomem * base, unsigned int reg,
  179. u32 value)
  180. {
  181. out_be32(base + (reg >> 2), value);
  182. }
  183. static inline struct qe_ic *qe_ic_from_irq(unsigned int virq)
  184. {
  185. return irq_get_chip_data(virq);
  186. }
  187. static inline struct qe_ic *qe_ic_from_irq_data(struct irq_data *d)
  188. {
  189. return irq_data_get_irq_chip_data(d);
  190. }
  191. static void qe_ic_unmask_irq(struct irq_data *d)
  192. {
  193. struct qe_ic *qe_ic = qe_ic_from_irq_data(d);
  194. unsigned int src = irqd_to_hwirq(d);
  195. unsigned long flags;
  196. u32 temp;
  197. raw_spin_lock_irqsave(&qe_ic_lock, flags);
  198. temp = qe_ic_read(qe_ic->regs, qe_ic_info[src].mask_reg);
  199. qe_ic_write(qe_ic->regs, qe_ic_info[src].mask_reg,
  200. temp | qe_ic_info[src].mask);
  201. raw_spin_unlock_irqrestore(&qe_ic_lock, flags);
  202. }
  203. static void qe_ic_mask_irq(struct irq_data *d)
  204. {
  205. struct qe_ic *qe_ic = qe_ic_from_irq_data(d);
  206. unsigned int src = irqd_to_hwirq(d);
  207. unsigned long flags;
  208. u32 temp;
  209. raw_spin_lock_irqsave(&qe_ic_lock, flags);
  210. temp = qe_ic_read(qe_ic->regs, qe_ic_info[src].mask_reg);
  211. qe_ic_write(qe_ic->regs, qe_ic_info[src].mask_reg,
  212. temp & ~qe_ic_info[src].mask);
  213. /* Flush the above write before enabling interrupts; otherwise,
  214. * spurious interrupts will sometimes happen. To be 100% sure
  215. * that the write has reached the device before interrupts are
  216. * enabled, the mask register would have to be read back; however,
  217. * this is not required for correctness, only to avoid wasting
  218. * time on a large number of spurious interrupts. In testing,
  219. * a sync reduced the observed spurious interrupts to zero.
  220. */
  221. mb();
  222. raw_spin_unlock_irqrestore(&qe_ic_lock, flags);
  223. }
  224. static struct irq_chip qe_ic_irq_chip = {
  225. .name = "QEIC",
  226. .irq_unmask = qe_ic_unmask_irq,
  227. .irq_mask = qe_ic_mask_irq,
  228. .irq_mask_ack = qe_ic_mask_irq,
  229. };
  230. static int qe_ic_host_match(struct irq_host *h, struct device_node *node)
  231. {
  232. /* Exact match, unless qe_ic node is NULL */
  233. return h->of_node == NULL || h->of_node == node;
  234. }
  235. static int qe_ic_host_map(struct irq_host *h, unsigned int virq,
  236. irq_hw_number_t hw)
  237. {
  238. struct qe_ic *qe_ic = h->host_data;
  239. struct irq_chip *chip;
  240. if (qe_ic_info[hw].mask == 0) {
  241. printk(KERN_ERR "Can't map reserved IRQ\n");
  242. return -EINVAL;
  243. }
  244. /* Default chip */
  245. chip = &qe_ic->hc_irq;
  246. irq_set_chip_data(virq, qe_ic);
  247. irq_set_status_flags(virq, IRQ_LEVEL);
  248. irq_set_chip_and_handler(virq, chip, handle_level_irq);
  249. return 0;
  250. }
  251. static int qe_ic_host_xlate(struct irq_host *h, struct device_node *ct,
  252. const u32 * intspec, unsigned int intsize,
  253. irq_hw_number_t * out_hwirq,
  254. unsigned int *out_flags)
  255. {
  256. *out_hwirq = intspec[0];
  257. if (intsize > 1)
  258. *out_flags = intspec[1];
  259. else
  260. *out_flags = IRQ_TYPE_NONE;
  261. return 0;
  262. }
  263. static struct irq_host_ops qe_ic_host_ops = {
  264. .match = qe_ic_host_match,
  265. .map = qe_ic_host_map,
  266. .xlate = qe_ic_host_xlate,
  267. };
  268. /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
  269. unsigned int qe_ic_get_low_irq(struct qe_ic *qe_ic)
  270. {
  271. int irq;
  272. BUG_ON(qe_ic == NULL);
  273. /* get the interrupt source vector. */
  274. irq = qe_ic_read(qe_ic->regs, QEIC_CIVEC) >> 26;
  275. if (irq == 0)
  276. return NO_IRQ;
  277. return irq_linear_revmap(qe_ic->irqhost, irq);
  278. }
  279. /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
  280. unsigned int qe_ic_get_high_irq(struct qe_ic *qe_ic)
  281. {
  282. int irq;
  283. BUG_ON(qe_ic == NULL);
  284. /* get the interrupt source vector. */
  285. irq = qe_ic_read(qe_ic->regs, QEIC_CHIVEC) >> 26;
  286. if (irq == 0)
  287. return NO_IRQ;
  288. return irq_linear_revmap(qe_ic->irqhost, irq);
  289. }
  290. void __init qe_ic_init(struct device_node *node, unsigned int flags,
  291. void (*low_handler)(unsigned int irq, struct irq_desc *desc),
  292. void (*high_handler)(unsigned int irq, struct irq_desc *desc))
  293. {
  294. struct qe_ic *qe_ic;
  295. struct resource res;
  296. u32 temp = 0, ret, high_active = 0;
  297. ret = of_address_to_resource(node, 0, &res);
  298. if (ret)
  299. return;
  300. qe_ic = kzalloc(sizeof(*qe_ic), GFP_KERNEL);
  301. if (qe_ic == NULL)
  302. return;
  303. qe_ic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
  304. NR_QE_IC_INTS, &qe_ic_host_ops, 0);
  305. if (qe_ic->irqhost == NULL) {
  306. kfree(qe_ic);
  307. return;
  308. }
  309. qe_ic->regs = ioremap(res.start, resource_size(&res));
  310. qe_ic->irqhost->host_data = qe_ic;
  311. qe_ic->hc_irq = qe_ic_irq_chip;
  312. qe_ic->virq_high = irq_of_parse_and_map(node, 0);
  313. qe_ic->virq_low = irq_of_parse_and_map(node, 1);
  314. if (qe_ic->virq_low == NO_IRQ) {
  315. printk(KERN_ERR "Failed to map QE_IC low IRQ\n");
  316. kfree(qe_ic);
  317. return;
  318. }
  319. /* default priority scheme is grouped. If spread mode is */
  320. /* required, configure cicr accordingly. */
  321. if (flags & QE_IC_SPREADMODE_GRP_W)
  322. temp |= CICR_GWCC;
  323. if (flags & QE_IC_SPREADMODE_GRP_X)
  324. temp |= CICR_GXCC;
  325. if (flags & QE_IC_SPREADMODE_GRP_Y)
  326. temp |= CICR_GYCC;
  327. if (flags & QE_IC_SPREADMODE_GRP_Z)
  328. temp |= CICR_GZCC;
  329. if (flags & QE_IC_SPREADMODE_GRP_RISCA)
  330. temp |= CICR_GRTA;
  331. if (flags & QE_IC_SPREADMODE_GRP_RISCB)
  332. temp |= CICR_GRTB;
  333. /* choose destination signal for highest priority interrupt */
  334. if (flags & QE_IC_HIGH_SIGNAL) {
  335. temp |= (SIGNAL_HIGH << CICR_HPIT_SHIFT);
  336. high_active = 1;
  337. }
  338. qe_ic_write(qe_ic->regs, QEIC_CICR, temp);
  339. irq_set_handler_data(qe_ic->virq_low, qe_ic);
  340. irq_set_chained_handler(qe_ic->virq_low, low_handler);
  341. if (qe_ic->virq_high != NO_IRQ &&
  342. qe_ic->virq_high != qe_ic->virq_low) {
  343. irq_set_handler_data(qe_ic->virq_high, qe_ic);
  344. irq_set_chained_handler(qe_ic->virq_high, high_handler);
  345. }
  346. }
  347. void qe_ic_set_highest_priority(unsigned int virq, int high)
  348. {
  349. struct qe_ic *qe_ic = qe_ic_from_irq(virq);
  350. unsigned int src = virq_to_hw(virq);
  351. u32 temp = 0;
  352. temp = qe_ic_read(qe_ic->regs, QEIC_CICR);
  353. temp &= ~CICR_HP_MASK;
  354. temp |= src << CICR_HP_SHIFT;
  355. temp &= ~CICR_HPIT_MASK;
  356. temp |= (high ? SIGNAL_HIGH : SIGNAL_LOW) << CICR_HPIT_SHIFT;
  357. qe_ic_write(qe_ic->regs, QEIC_CICR, temp);
  358. }
  359. /* Set Priority level within its group, from 1 to 8 */
  360. int qe_ic_set_priority(unsigned int virq, unsigned int priority)
  361. {
  362. struct qe_ic *qe_ic = qe_ic_from_irq(virq);
  363. unsigned int src = virq_to_hw(virq);
  364. u32 temp;
  365. if (priority > 8 || priority == 0)
  366. return -EINVAL;
  367. if (src > 127)
  368. return -EINVAL;
  369. if (qe_ic_info[src].pri_reg == 0)
  370. return -EINVAL;
  371. temp = qe_ic_read(qe_ic->regs, qe_ic_info[src].pri_reg);
  372. if (priority < 4) {
  373. temp &= ~(0x7 << (32 - priority * 3));
  374. temp |= qe_ic_info[src].pri_code << (32 - priority * 3);
  375. } else {
  376. temp &= ~(0x7 << (24 - priority * 3));
  377. temp |= qe_ic_info[src].pri_code << (24 - priority * 3);
  378. }
  379. qe_ic_write(qe_ic->regs, qe_ic_info[src].pri_reg, temp);
  380. return 0;
  381. }
  382. /* Set a QE priority to use high irq, only priority 1~2 can use high irq */
  383. int qe_ic_set_high_priority(unsigned int virq, unsigned int priority, int high)
  384. {
  385. struct qe_ic *qe_ic = qe_ic_from_irq(virq);
  386. unsigned int src = virq_to_hw(virq);
  387. u32 temp, control_reg = QEIC_CICNR, shift = 0;
  388. if (priority > 2 || priority == 0)
  389. return -EINVAL;
  390. switch (qe_ic_info[src].pri_reg) {
  391. case QEIC_CIPZCC:
  392. shift = CICNR_ZCC1T_SHIFT;
  393. break;
  394. case QEIC_CIPWCC:
  395. shift = CICNR_WCC1T_SHIFT;
  396. break;
  397. case QEIC_CIPYCC:
  398. shift = CICNR_YCC1T_SHIFT;
  399. break;
  400. case QEIC_CIPXCC:
  401. shift = CICNR_XCC1T_SHIFT;
  402. break;
  403. case QEIC_CIPRTA:
  404. shift = CRICR_RTA1T_SHIFT;
  405. control_reg = QEIC_CRICR;
  406. break;
  407. case QEIC_CIPRTB:
  408. shift = CRICR_RTB1T_SHIFT;
  409. control_reg = QEIC_CRICR;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. shift += (2 - priority) * 2;
  415. temp = qe_ic_read(qe_ic->regs, control_reg);
  416. temp &= ~(SIGNAL_MASK << shift);
  417. temp |= (high ? SIGNAL_HIGH : SIGNAL_LOW) << shift;
  418. qe_ic_write(qe_ic->regs, control_reg, temp);
  419. return 0;
  420. }
  421. static struct sysdev_class qe_ic_sysclass = {
  422. .name = "qe_ic",
  423. };
  424. static struct sys_device device_qe_ic = {
  425. .id = 0,
  426. .cls = &qe_ic_sysclass,
  427. };
  428. static int __init init_qe_ic_sysfs(void)
  429. {
  430. int rc;
  431. printk(KERN_DEBUG "Registering qe_ic with sysfs...\n");
  432. rc = sysdev_class_register(&qe_ic_sysclass);
  433. if (rc) {
  434. printk(KERN_ERR "Failed registering qe_ic sys class\n");
  435. return -ENODEV;
  436. }
  437. rc = sysdev_register(&device_qe_ic);
  438. if (rc) {
  439. printk(KERN_ERR "Failed registering qe_ic sys device\n");
  440. return -ENODEV;
  441. }
  442. return 0;
  443. }
  444. subsys_initcall(init_qe_ic_sysfs);