1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Google, LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #define LOG_CATEGORY UCLASS_IRQ
8 
9 #include <dm.h>
10 #include <dt-structs.h>
11 #include <irq.h>
12 #include <log.h>
13 #include <dm/device-internal.h>
14 
irq_route_pmc_gpio_gpe(struct udevice * dev,uint pmc_gpe_num)15 int irq_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
16 {
17 	const struct irq_ops *ops = irq_get_ops(dev);
18 
19 	if (!ops->route_pmc_gpio_gpe)
20 		return -ENOSYS;
21 
22 	return ops->route_pmc_gpio_gpe(dev, pmc_gpe_num);
23 }
24 
irq_set_polarity(struct udevice * dev,uint irq,bool active_low)25 int irq_set_polarity(struct udevice *dev, uint irq, bool active_low)
26 {
27 	const struct irq_ops *ops = irq_get_ops(dev);
28 
29 	if (!ops->set_polarity)
30 		return -ENOSYS;
31 
32 	return ops->set_polarity(dev, irq, active_low);
33 }
34 
irq_snapshot_polarities(struct udevice * dev)35 int irq_snapshot_polarities(struct udevice *dev)
36 {
37 	const struct irq_ops *ops = irq_get_ops(dev);
38 
39 	if (!ops->snapshot_polarities)
40 		return -ENOSYS;
41 
42 	return ops->snapshot_polarities(dev);
43 }
44 
irq_restore_polarities(struct udevice * dev)45 int irq_restore_polarities(struct udevice *dev)
46 {
47 	const struct irq_ops *ops = irq_get_ops(dev);
48 
49 	if (!ops->restore_polarities)
50 		return -ENOSYS;
51 
52 	return ops->restore_polarities(dev);
53 }
54 
irq_read_and_clear(struct irq * irq)55 int irq_read_and_clear(struct irq *irq)
56 {
57 	const struct irq_ops *ops = irq_get_ops(irq->dev);
58 
59 	if (!ops->read_and_clear)
60 		return -ENOSYS;
61 
62 	return ops->read_and_clear(irq);
63 }
64 
irq_get_interrupt_parent(const struct udevice * dev,struct udevice ** interrupt_parent)65 int irq_get_interrupt_parent(const struct udevice *dev,
66 			     struct udevice **interrupt_parent)
67 {
68 	struct ofnode_phandle_args phandle_args;
69 	struct udevice *irq = NULL;
70 	ofnode node;
71 	int ret;
72 
73 	if (!dev || !interrupt_parent)
74 		return -EINVAL;
75 
76 	*interrupt_parent = NULL;
77 
78 	node = dev_ofnode(dev);
79 	if (!ofnode_valid(node))
80 		return -EINVAL;
81 
82 	while (ofnode_valid(node)) {
83 		ret = ofnode_parse_phandle_with_args(node, "interrupt-parent",
84 						     NULL, 0, 0, &phandle_args);
85 		if (!ret && !device_get_global_by_ofnode(phandle_args.node, &irq))
86 			break;
87 		node = ofnode_get_parent(node);
88 	}
89 
90 	if (!irq) {
91 		log_err("Cannot find an interrupt parent for device %s\n", dev->name);
92 		return -ENODEV;
93 	}
94 	*interrupt_parent = irq;
95 
96 	return 0;
97 }
98 
99 #if CONFIG_IS_ENABLED(OF_PLATDATA)
irq_get_by_phandle(struct udevice * dev,const struct phandle_2_arg * cells,struct irq * irq)100 int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells,
101 		       struct irq *irq)
102 {
103 	int ret;
104 
105 	ret = device_get_by_ofplat_idx(cells->idx, &irq->dev);
106 	if (ret)
107 		return ret;
108 	irq->id = cells->arg[0];
109 
110 	/*
111 	 * Note: we could call irq_of_xlate_default() here to do this properly.
112 	 * For now, this is good enough for existing cases.
113 	 */
114 	irq->flags = cells->arg[1];
115 
116 	return 0;
117 }
118 #else
irq_of_xlate_default(struct irq * irq,struct ofnode_phandle_args * args)119 static int irq_of_xlate_default(struct irq *irq,
120 				struct ofnode_phandle_args *args)
121 {
122 	log_debug("(irq=%p)\n", irq);
123 
124 	if (args->args_count > 1) {
125 		log_debug("Invalid args_count: %d\n", args->args_count);
126 		return -EINVAL;
127 	}
128 
129 	if (args->args_count)
130 		irq->id = args->args[0];
131 	else
132 		irq->id = 0;
133 
134 	return 0;
135 }
136 
irq_get_by_index_tail(int ret,ofnode node,struct ofnode_phandle_args * args,const char * list_name,int index,struct irq * irq)137 static int irq_get_by_index_tail(int ret, ofnode node,
138 				 struct ofnode_phandle_args *args,
139 				 const char *list_name, int index,
140 				 struct irq *irq)
141 {
142 	struct udevice *dev_irq;
143 	const struct irq_ops *ops;
144 
145 	assert(irq);
146 	irq->dev = NULL;
147 	if (ret)
148 		goto err;
149 
150 	ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
151 	if (ret) {
152 		log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
153 		return ret;
154 	}
155 
156 	irq->dev = dev_irq;
157 
158 	ops = irq_get_ops(dev_irq);
159 
160 	if (ops->of_xlate)
161 		ret = ops->of_xlate(irq, args);
162 	else
163 		ret = irq_of_xlate_default(irq, args);
164 	if (ret) {
165 		log_debug("of_xlate() failed: %d\n", ret);
166 		return ret;
167 	}
168 
169 	return irq_request(dev_irq, irq);
170 err:
171 	log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
172 		  ofnode_get_name(node), list_name, index, ret);
173 	return ret;
174 }
175 
irq_get_by_index(struct udevice * dev,int index,struct irq * irq)176 int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
177 {
178 	struct ofnode_phandle_args args;
179 	struct udevice *interrupt_parent;
180 	int ret, size, i;
181 	const __be32 *list;
182 	u32 count;
183 
184 	ret = dev_read_phandle_with_args(dev, "interrupts-extended",
185 					 "#interrupt-cells", 0, index, &args);
186 	if (ret) {
187 		list = dev_read_prop(dev, "interrupts", &size);
188 		if (!list)
189 			return -ENOENT;
190 
191 		ret = irq_get_interrupt_parent(dev, &interrupt_parent);
192 		if (ret)
193 			return -ENODEV;
194 		args.node = dev_ofnode(interrupt_parent);
195 
196 		if (dev_read_u32(interrupt_parent, "#interrupt-cells", &count)) {
197 			log_err("%s: could not get #interrupt-cells for %s\n",
198 				__func__, dev->name);
199 			return -ENOENT;
200 		}
201 
202 		if (index * count >= size / sizeof(*list))
203 			return -ENOENT;
204 		if (count > OF_MAX_PHANDLE_ARGS)
205 			count = OF_MAX_PHANDLE_ARGS;
206 		args.args_count = count;
207 		for (i = 0; i < count; i++)
208 			args.args[i] = be32_to_cpup(&list[index * count + i]);
209 
210 		return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
211 					     "interrupts", index, irq);
212 	}
213 
214 	return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
215 				     "interrupts-extended", index > 0, irq);
216 }
217 #endif /* OF_PLATDATA */
218 
irq_request(struct udevice * dev,struct irq * irq)219 int irq_request(struct udevice *dev, struct irq *irq)
220 {
221 	const struct irq_ops *ops;
222 
223 	log_debug("(dev=%p, irq=%p)\n", dev, irq);
224 	ops = irq_get_ops(dev);
225 
226 	irq->dev = dev;
227 
228 	if (!ops->request)
229 		return 0;
230 
231 	return ops->request(irq);
232 }
233 
irq_first_device_type(enum irq_dev_t type,struct udevice ** devp)234 int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
235 {
236 	int ret;
237 
238 	ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
239 	if (ret)
240 		return ret;
241 
242 	return 0;
243 }
244 
245 #if CONFIG_IS_ENABLED(ACPIGEN)
irq_get_acpi(const struct irq * irq,struct acpi_irq * acpi_irq)246 int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq)
247 {
248 	struct irq_ops *ops;
249 
250 	if (!irq_is_valid(irq))
251 		return -EINVAL;
252 
253 	ops = irq_get_ops(irq->dev);
254 	if (!ops->get_acpi)
255 		return -ENOSYS;
256 
257 	return ops->get_acpi(irq, acpi_irq);
258 }
259 #endif
260 
261 UCLASS_DRIVER(irq) = {
262 	.id		= UCLASS_IRQ,
263 	.name		= "irq",
264 };
265