1 // SPDX-License-Identifier: GPL-2.0-only
4 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
7 #include <linux/init.h>
9 #include <linux/spinlock.h>
10 #include <linux/delay.h>
11 #include <linux/clk.h>
12 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/dma/ipu-dma.h>
17 #include "ipu_intern.h"
20 * Register read / write - shall be inlined by the compiler
22 static u32
ipu_read_reg(struct ipu
*ipu
, unsigned long reg
)
24 return __raw_readl(ipu
->reg_ipu
+ reg
);
27 static void ipu_write_reg(struct ipu
*ipu
, u32 value
, unsigned long reg
)
29 __raw_writel(value
, ipu
->reg_ipu
+ reg
);
37 #define IPU_IRQ_NR_FN_BANKS 3
38 #define IPU_IRQ_NR_ERR_BANKS 2
39 #define IPU_IRQ_NR_BANKS (IPU_IRQ_NR_FN_BANKS + IPU_IRQ_NR_ERR_BANKS)
47 static struct ipu_irq_bank irq_bank
[IPU_IRQ_NR_BANKS
] = {
48 /* 3 groups of functional interrupts */
50 .control
= IPU_INT_CTRL_1
,
51 .status
= IPU_INT_STAT_1
,
53 .control
= IPU_INT_CTRL_2
,
54 .status
= IPU_INT_STAT_2
,
56 .control
= IPU_INT_CTRL_3
,
57 .status
= IPU_INT_STAT_3
,
59 /* 2 groups of error interrupts */
61 .control
= IPU_INT_CTRL_4
,
62 .status
= IPU_INT_STAT_4
,
64 .control
= IPU_INT_CTRL_5
,
65 .status
= IPU_INT_STAT_5
,
72 struct ipu_irq_bank
*bank
;
76 static struct ipu_irq_map irq_map
[CONFIG_MX3_IPU_IRQS
];
77 /* Protects allocations from the above array of maps */
78 static DEFINE_MUTEX(map_lock
);
79 /* Protects register accesses and individual mappings */
80 static DEFINE_RAW_SPINLOCK(bank_lock
);
82 static struct ipu_irq_map
*src2map(unsigned int src
)
86 for (i
= 0; i
< CONFIG_MX3_IPU_IRQS
; i
++)
87 if (irq_map
[i
].source
== src
)
93 static void ipu_irq_unmask(struct irq_data
*d
)
95 struct ipu_irq_map
*map
= irq_data_get_irq_chip_data(d
);
96 struct ipu_irq_bank
*bank
;
98 unsigned long lock_flags
;
100 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
104 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
105 pr_err("IPU: %s(%u) - unmapped!\n", __func__
, d
->irq
);
109 reg
= ipu_read_reg(bank
->ipu
, bank
->control
);
110 reg
|= (1UL << (map
->source
& 31));
111 ipu_write_reg(bank
->ipu
, reg
, bank
->control
);
113 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
116 static void ipu_irq_mask(struct irq_data
*d
)
118 struct ipu_irq_map
*map
= irq_data_get_irq_chip_data(d
);
119 struct ipu_irq_bank
*bank
;
121 unsigned long lock_flags
;
123 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
127 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
128 pr_err("IPU: %s(%u) - unmapped!\n", __func__
, d
->irq
);
132 reg
= ipu_read_reg(bank
->ipu
, bank
->control
);
133 reg
&= ~(1UL << (map
->source
& 31));
134 ipu_write_reg(bank
->ipu
, reg
, bank
->control
);
136 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
139 static void ipu_irq_ack(struct irq_data
*d
)
141 struct ipu_irq_map
*map
= irq_data_get_irq_chip_data(d
);
142 struct ipu_irq_bank
*bank
;
143 unsigned long lock_flags
;
145 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
149 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
150 pr_err("IPU: %s(%u) - unmapped!\n", __func__
, d
->irq
);
154 ipu_write_reg(bank
->ipu
, 1UL << (map
->source
& 31), bank
->status
);
155 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
159 * ipu_irq_status() - returns the current interrupt status of the specified IRQ.
160 * @irq: interrupt line to get status for.
161 * @return: true if the interrupt is pending/asserted or false if the
162 * interrupt is not pending.
164 bool ipu_irq_status(unsigned int irq
)
166 struct ipu_irq_map
*map
= irq_get_chip_data(irq
);
167 struct ipu_irq_bank
*bank
;
168 unsigned long lock_flags
;
171 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
173 ret
= bank
&& ipu_read_reg(bank
->ipu
, bank
->status
) &
174 (1UL << (map
->source
& 31));
175 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
181 * ipu_irq_map() - map an IPU interrupt source to an IRQ number
182 * @source: interrupt source bit position (see below)
183 * @return: mapped IRQ number or negative error code
185 * The source parameter has to be explained further. On i.MX31 IPU has 137 IRQ
186 * sources, they are broken down in 5 32-bit registers, like 32, 32, 24, 32, 17.
187 * However, the source argument of this function is not the sequence number of
188 * the possible IRQ, but rather its bit position. So, first interrupt in fourth
189 * register has source number 96, and not 88. This makes calculations easier,
190 * and also provides forward compatibility with any future IPU implementations
191 * with any interrupt bit assignments.
193 int ipu_irq_map(unsigned int source
)
195 int i
, ret
= -ENOMEM
;
196 struct ipu_irq_map
*map
;
200 mutex_lock(&map_lock
);
201 map
= src2map(source
);
203 pr_err("IPU: Source %u already mapped to IRQ %u\n", source
, map
->irq
);
208 for (i
= 0; i
< CONFIG_MX3_IPU_IRQS
; i
++) {
209 if (irq_map
[i
].source
< 0) {
210 unsigned long lock_flags
;
212 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
213 irq_map
[i
].source
= source
;
214 irq_map
[i
].bank
= irq_bank
+ source
/ 32;
215 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
217 ret
= irq_map
[i
].irq
;
218 pr_debug("IPU: mapped source %u to IRQ %u\n",
224 mutex_unlock(&map_lock
);
227 pr_err("IPU: couldn't map source %u: %d\n", source
, ret
);
233 * ipu_irq_map() - map an IPU interrupt source to an IRQ number
234 * @source: interrupt source bit position (see ipu_irq_map())
235 * @return: 0 or negative error code
237 int ipu_irq_unmap(unsigned int source
)
239 int i
, ret
= -EINVAL
;
243 mutex_lock(&map_lock
);
244 for (i
= 0; i
< CONFIG_MX3_IPU_IRQS
; i
++) {
245 if (irq_map
[i
].source
== source
) {
246 unsigned long lock_flags
;
248 pr_debug("IPU: unmapped source %u from IRQ %u\n",
249 source
, irq_map
[i
].irq
);
251 raw_spin_lock_irqsave(&bank_lock
, lock_flags
);
252 irq_map
[i
].source
= -EINVAL
;
253 irq_map
[i
].bank
= NULL
;
254 raw_spin_unlock_irqrestore(&bank_lock
, lock_flags
);
260 mutex_unlock(&map_lock
);
265 /* Chained IRQ handler for IPU function and error interrupt */
266 static void ipu_irq_handler(struct irq_desc
*desc
)
268 struct ipu
*ipu
= irq_desc_get_handler_data(desc
);
272 for (i
= 0; i
< IPU_IRQ_NR_BANKS
; i
++) {
273 struct ipu_irq_bank
*bank
= irq_bank
+ i
;
275 raw_spin_lock(&bank_lock
);
276 status
= ipu_read_reg(ipu
, bank
->status
);
278 * Don't think we have to clear all interrupts here, they will
279 * be acked by ->handle_irq() (handle_level_irq). However, we
280 * might want to clear unhandled interrupts after the loop...
282 status
&= ipu_read_reg(ipu
, bank
->control
);
283 raw_spin_unlock(&bank_lock
);
284 while ((line
= ffs(status
))) {
285 struct ipu_irq_map
*map
;
289 status
&= ~(1UL << line
);
291 raw_spin_lock(&bank_lock
);
292 map
= src2map(32 * i
+ line
);
294 raw_spin_unlock(&bank_lock
);
295 pr_err("IPU: Interrupt on unmapped source %u bank %d\n",
300 raw_spin_unlock(&bank_lock
);
301 generic_handle_irq(irq
);
306 static struct irq_chip ipu_irq_chip
= {
308 .irq_ack
= ipu_irq_ack
,
309 .irq_mask
= ipu_irq_mask
,
310 .irq_unmask
= ipu_irq_unmask
,
313 /* Install the IRQ handler */
314 int __init
ipu_irq_attach_irq(struct ipu
*ipu
, struct platform_device
*dev
)
317 int irq_base
= irq_alloc_descs(-1, 0, CONFIG_MX3_IPU_IRQS
,
323 for (i
= 0; i
< IPU_IRQ_NR_BANKS
; i
++)
324 irq_bank
[i
].ipu
= ipu
;
326 for (i
= 0; i
< CONFIG_MX3_IPU_IRQS
; i
++) {
330 ret
= irq_set_chip(irq
, &ipu_irq_chip
);
333 ret
= irq_set_chip_data(irq
, irq_map
+ i
);
336 irq_map
[i
].ipu
= ipu
;
337 irq_map
[i
].irq
= irq
;
338 irq_map
[i
].source
= -EINVAL
;
339 irq_set_handler(irq
, handle_level_irq
);
340 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
343 irq_set_chained_handler_and_data(ipu
->irq_fn
, ipu_irq_handler
, ipu
);
345 irq_set_chained_handler_and_data(ipu
->irq_err
, ipu_irq_handler
, ipu
);
347 ipu
->irq_base
= irq_base
;
352 void ipu_irq_detach_irq(struct ipu
*ipu
, struct platform_device
*dev
)
354 unsigned int irq
, irq_base
;
356 irq_base
= ipu
->irq_base
;
358 irq_set_chained_handler_and_data(ipu
->irq_fn
, NULL
, NULL
);
360 irq_set_chained_handler_and_data(ipu
->irq_err
, NULL
, NULL
);
362 for (irq
= irq_base
; irq
< irq_base
+ CONFIG_MX3_IPU_IRQS
; irq
++) {
363 irq_set_status_flags(irq
, IRQ_NOREQUEST
);
364 irq_set_chip(irq
, NULL
);
365 irq_set_chip_data(irq
, NULL
);