ipu_irq.c 9.8 KB

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