ipu_irq.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2008
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/err.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/delay.h>
  13. #include <linux/clk.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <mach/ipu.h>
  17. #include "ipu_intern.h"
  18. /*
  19. * Register read / write - shall be inlined by the compiler
  20. */
  21. static u32 ipu_read_reg(struct ipu *ipu, unsigned long reg)
  22. {
  23. return __raw_readl(ipu->reg_ipu + reg);
  24. }
  25. static void ipu_write_reg(struct ipu *ipu, u32 value, unsigned long reg)
  26. {
  27. __raw_writel(value, ipu->reg_ipu + reg);
  28. }
  29. /*
  30. * IPU IRQ chip driver
  31. */
  32. #define IPU_IRQ_NR_FN_BANKS 3
  33. #define IPU_IRQ_NR_ERR_BANKS 2
  34. #define IPU_IRQ_NR_BANKS (IPU_IRQ_NR_FN_BANKS + IPU_IRQ_NR_ERR_BANKS)
  35. struct ipu_irq_bank {
  36. unsigned int control;
  37. unsigned int status;
  38. spinlock_t lock;
  39. struct ipu *ipu;
  40. };
  41. static struct ipu_irq_bank irq_bank[IPU_IRQ_NR_BANKS] = {
  42. /* 3 groups of functional interrupts */
  43. {
  44. .control = IPU_INT_CTRL_1,
  45. .status = IPU_INT_STAT_1,
  46. }, {
  47. .control = IPU_INT_CTRL_2,
  48. .status = IPU_INT_STAT_2,
  49. }, {
  50. .control = IPU_INT_CTRL_3,
  51. .status = IPU_INT_STAT_3,
  52. },
  53. /* 2 groups of error interrupts */
  54. {
  55. .control = IPU_INT_CTRL_4,
  56. .status = IPU_INT_STAT_4,
  57. }, {
  58. .control = IPU_INT_CTRL_5,
  59. .status = IPU_INT_STAT_5,
  60. },
  61. };
  62. struct ipu_irq_map {
  63. unsigned int irq;
  64. int source;
  65. struct ipu_irq_bank *bank;
  66. struct ipu *ipu;
  67. };
  68. static struct ipu_irq_map irq_map[CONFIG_MX3_IPU_IRQS];
  69. /* Protects allocations from the above array of maps */
  70. static DEFINE_MUTEX(map_lock);
  71. /* Protects register accesses and individual mappings */
  72. static DEFINE_SPINLOCK(bank_lock);
  73. static struct ipu_irq_map *src2map(unsigned int src)
  74. {
  75. int i;
  76. for (i = 0; i < CONFIG_MX3_IPU_IRQS; i++)
  77. if (irq_map[i].source == src)
  78. return irq_map + i;
  79. return NULL;
  80. }
  81. static void ipu_irq_unmask(unsigned int irq)
  82. {
  83. struct ipu_irq_map *map = get_irq_chip_data(irq);
  84. struct ipu_irq_bank *bank;
  85. uint32_t reg;
  86. unsigned long lock_flags;
  87. spin_lock_irqsave(&bank_lock, lock_flags);
  88. bank = map->bank;
  89. if (!bank) {
  90. spin_unlock_irqrestore(&bank_lock, lock_flags);
  91. pr_err("IPU: %s(%u) - unmapped!\n", __func__, irq);
  92. return;
  93. }
  94. reg = ipu_read_reg(bank->ipu, bank->control);
  95. reg |= (1UL << (map->source & 31));
  96. ipu_write_reg(bank->ipu, reg, bank->control);
  97. spin_unlock_irqrestore(&bank_lock, lock_flags);
  98. }
  99. static void ipu_irq_mask(unsigned int irq)
  100. {
  101. struct ipu_irq_map *map = get_irq_chip_data(irq);
  102. struct ipu_irq_bank *bank;
  103. uint32_t reg;
  104. unsigned long lock_flags;
  105. spin_lock_irqsave(&bank_lock, lock_flags);
  106. bank = map->bank;
  107. if (!bank) {
  108. spin_unlock_irqrestore(&bank_lock, lock_flags);
  109. pr_err("IPU: %s(%u) - unmapped!\n", __func__, irq);
  110. return;
  111. }
  112. reg = ipu_read_reg(bank->ipu, bank->control);
  113. reg &= ~(1UL << (map->source & 31));
  114. ipu_write_reg(bank->ipu, reg, bank->control);
  115. spin_unlock_irqrestore(&bank_lock, lock_flags);
  116. }
  117. static void ipu_irq_ack(unsigned int irq)
  118. {
  119. struct ipu_irq_map *map = get_irq_chip_data(irq);
  120. struct ipu_irq_bank *bank;
  121. unsigned long lock_flags;
  122. spin_lock_irqsave(&bank_lock, lock_flags);
  123. bank = map->bank;
  124. if (!bank) {
  125. spin_unlock_irqrestore(&bank_lock, lock_flags);
  126. pr_err("IPU: %s(%u) - unmapped!\n", __func__, irq);
  127. return;
  128. }
  129. ipu_write_reg(bank->ipu, 1UL << (map->source & 31), bank->status);
  130. spin_unlock_irqrestore(&bank_lock, lock_flags);
  131. }
  132. /**
  133. * ipu_irq_status() - returns the current interrupt status of the specified IRQ.
  134. * @irq: interrupt line to get status for.
  135. * @return: true if the interrupt is pending/asserted or false if the
  136. * interrupt is not pending.
  137. */
  138. bool ipu_irq_status(unsigned int irq)
  139. {
  140. struct ipu_irq_map *map = get_irq_chip_data(irq);
  141. struct ipu_irq_bank *bank;
  142. unsigned long lock_flags;
  143. bool ret;
  144. spin_lock_irqsave(&bank_lock, lock_flags);
  145. bank = map->bank;
  146. ret = bank && ipu_read_reg(bank->ipu, bank->status) &
  147. (1UL << (map->source & 31));
  148. spin_unlock_irqrestore(&bank_lock, lock_flags);
  149. return ret;
  150. }
  151. /**
  152. * ipu_irq_map() - map an IPU interrupt source to an IRQ number
  153. * @source: interrupt source bit position (see below)
  154. * @return: mapped IRQ number or negative error code
  155. *
  156. * The source parameter has to be explained further. On i.MX31 IPU has 137 IRQ
  157. * sources, they are broken down in 5 32-bit registers, like 32, 32, 24, 32, 17.
  158. * However, the source argument of this function is not the sequence number of
  159. * the possible IRQ, but rather its bit position. So, first interrupt in fourth
  160. * register has source number 96, and not 88. This makes calculations easier,
  161. * and also provides forward compatibility with any future IPU implementations
  162. * with any interrupt bit assignments.
  163. */
  164. int ipu_irq_map(unsigned int source)
  165. {
  166. int i, ret = -ENOMEM;
  167. struct ipu_irq_map *map;
  168. might_sleep();
  169. mutex_lock(&map_lock);
  170. map = src2map(source);
  171. if (map) {
  172. pr_err("IPU: Source %u already mapped to IRQ %u\n", source, map->irq);
  173. ret = -EBUSY;
  174. goto out;
  175. }
  176. for (i = 0; i < CONFIG_MX3_IPU_IRQS; i++) {
  177. if (irq_map[i].source < 0) {
  178. unsigned long lock_flags;
  179. spin_lock_irqsave(&bank_lock, lock_flags);
  180. irq_map[i].source = source;
  181. irq_map[i].bank = irq_bank + source / 32;
  182. spin_unlock_irqrestore(&bank_lock, lock_flags);
  183. ret = irq_map[i].irq;
  184. pr_debug("IPU: mapped source %u to IRQ %u\n",
  185. source, ret);
  186. break;
  187. }
  188. }
  189. out:
  190. mutex_unlock(&map_lock);
  191. if (ret < 0)
  192. pr_err("IPU: couldn't map source %u: %d\n", source, ret);
  193. return ret;
  194. }
  195. /**
  196. * ipu_irq_map() - map an IPU interrupt source to an IRQ number
  197. * @source: interrupt source bit position (see ipu_irq_map())
  198. * @return: 0 or negative error code
  199. */
  200. int ipu_irq_unmap(unsigned int source)
  201. {
  202. int i, ret = -EINVAL;
  203. might_sleep();
  204. mutex_lock(&map_lock);
  205. for (i = 0; i < CONFIG_MX3_IPU_IRQS; i++) {
  206. if (irq_map[i].source == source) {
  207. unsigned long lock_flags;
  208. pr_debug("IPU: unmapped source %u from IRQ %u\n",
  209. source, irq_map[i].irq);
  210. spin_lock_irqsave(&bank_lock, lock_flags);
  211. irq_map[i].source = -EINVAL;
  212. irq_map[i].bank = NULL;
  213. spin_unlock_irqrestore(&bank_lock, lock_flags);
  214. ret = 0;
  215. break;
  216. }
  217. }
  218. mutex_unlock(&map_lock);
  219. return ret;
  220. }
  221. /* Chained IRQ handler for IPU error interrupt */
  222. static void ipu_irq_err(unsigned int irq, struct irq_desc *desc)
  223. {
  224. struct ipu *ipu = get_irq_data(irq);
  225. u32 status;
  226. int i, line;
  227. for (i = IPU_IRQ_NR_FN_BANKS; i < IPU_IRQ_NR_BANKS; i++) {
  228. struct ipu_irq_bank *bank = irq_bank + i;
  229. spin_lock(&bank_lock);
  230. status = ipu_read_reg(ipu, bank->status);
  231. /*
  232. * Don't think we have to clear all interrupts here, they will
  233. * be acked by ->handle_irq() (handle_level_irq). However, we
  234. * might want to clear unhandled interrupts after the loop...
  235. */
  236. status &= ipu_read_reg(ipu, bank->control);
  237. spin_unlock(&bank_lock);
  238. while ((line = ffs(status))) {
  239. struct ipu_irq_map *map;
  240. line--;
  241. status &= ~(1UL << line);
  242. spin_lock(&bank_lock);
  243. map = src2map(32 * i + line);
  244. if (map)
  245. irq = map->irq;
  246. spin_unlock(&bank_lock);
  247. if (!map) {
  248. pr_err("IPU: Interrupt on unmapped source %u bank %d\n",
  249. line, i);
  250. continue;
  251. }
  252. generic_handle_irq(irq);
  253. }
  254. }
  255. }
  256. /* Chained IRQ handler for IPU function interrupt */
  257. static void ipu_irq_fn(unsigned int irq, struct irq_desc *desc)
  258. {
  259. struct ipu *ipu = get_irq_data(irq);
  260. u32 status;
  261. int i, line;
  262. for (i = 0; i < IPU_IRQ_NR_FN_BANKS; i++) {
  263. struct ipu_irq_bank *bank = irq_bank + i;
  264. spin_lock(&bank_lock);
  265. status = ipu_read_reg(ipu, bank->status);
  266. /* Not clearing all interrupts, see above */
  267. status &= ipu_read_reg(ipu, bank->control);
  268. spin_unlock(&bank_lock);
  269. while ((line = ffs(status))) {
  270. struct ipu_irq_map *map;
  271. line--;
  272. status &= ~(1UL << line);
  273. spin_lock(&bank_lock);
  274. map = src2map(32 * i + line);
  275. if (map)
  276. irq = map->irq;
  277. spin_unlock(&bank_lock);
  278. if (!map) {
  279. pr_err("IPU: Interrupt on unmapped source %u bank %d\n",
  280. line, i);
  281. continue;
  282. }
  283. generic_handle_irq(irq);
  284. }
  285. }
  286. }
  287. static struct irq_chip ipu_irq_chip = {
  288. .name = "ipu_irq",
  289. .ack = ipu_irq_ack,
  290. .mask = ipu_irq_mask,
  291. .unmask = ipu_irq_unmask,
  292. };
  293. /* Install the IRQ handler */
  294. int __init ipu_irq_attach_irq(struct ipu *ipu, struct platform_device *dev)
  295. {
  296. struct ipu_platform_data *pdata = dev->dev.platform_data;
  297. unsigned int irq, irq_base, i;
  298. irq_base = pdata->irq_base;
  299. for (i = 0; i < IPU_IRQ_NR_BANKS; i++)
  300. irq_bank[i].ipu = ipu;
  301. for (i = 0; i < CONFIG_MX3_IPU_IRQS; i++) {
  302. int ret;
  303. irq = irq_base + i;
  304. ret = set_irq_chip(irq, &ipu_irq_chip);
  305. if (ret < 0)
  306. return ret;
  307. ret = set_irq_chip_data(irq, irq_map + i);
  308. if (ret < 0)
  309. return ret;
  310. irq_map[i].ipu = ipu;
  311. irq_map[i].irq = irq;
  312. irq_map[i].source = -EINVAL;
  313. set_irq_handler(irq, handle_level_irq);
  314. #ifdef CONFIG_ARM
  315. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  316. #endif
  317. }
  318. set_irq_data(ipu->irq_fn, ipu);
  319. set_irq_chained_handler(ipu->irq_fn, ipu_irq_fn);
  320. set_irq_data(ipu->irq_err, ipu);
  321. set_irq_chained_handler(ipu->irq_err, ipu_irq_err);
  322. return 0;
  323. }
  324. void ipu_irq_detach_irq(struct ipu *ipu, struct platform_device *dev)
  325. {
  326. struct ipu_platform_data *pdata = dev->dev.platform_data;
  327. unsigned int irq, irq_base;
  328. irq_base = pdata->irq_base;
  329. set_irq_chained_handler(ipu->irq_fn, NULL);
  330. set_irq_data(ipu->irq_fn, NULL);
  331. set_irq_chained_handler(ipu->irq_err, NULL);
  332. set_irq_data(ipu->irq_err, NULL);
  333. for (irq = irq_base; irq < irq_base + CONFIG_MX3_IPU_IRQS; irq++) {
  334. #ifdef CONFIG_ARM
  335. set_irq_flags(irq, 0);
  336. #endif
  337. set_irq_chip(irq, NULL);
  338. set_irq_chip_data(irq, NULL);
  339. }
  340. }