qe_ic.c 11 KB

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