irq-crossbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * drivers/irqchip/irq-crossbar.c
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Sricharan R <r.sricharan@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/slab.h>
  19. #define IRQ_FREE -1
  20. #define IRQ_RESERVED -2
  21. #define IRQ_SKIP -3
  22. #define GIC_IRQ_START 32
  23. /**
  24. * struct crossbar_device - crossbar device description
  25. * @lock: spinlock serializing access to @irq_map
  26. * @int_max: maximum number of supported interrupts
  27. * @safe_map: safe default value to initialize the crossbar
  28. * @max_crossbar_sources: Maximum number of crossbar sources
  29. * @irq_map: array of interrupts to crossbar number mapping
  30. * @crossbar_base: crossbar base address
  31. * @register_offsets: offsets for each irq number
  32. * @write: register write function pointer
  33. */
  34. struct crossbar_device {
  35. raw_spinlock_t lock;
  36. uint int_max;
  37. uint safe_map;
  38. uint max_crossbar_sources;
  39. uint *irq_map;
  40. void __iomem *crossbar_base;
  41. int *register_offsets;
  42. void (*write)(int, int);
  43. };
  44. static struct crossbar_device *cb;
  45. static void crossbar_writel(int irq_no, int cb_no)
  46. {
  47. writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  48. }
  49. static void crossbar_writew(int irq_no, int cb_no)
  50. {
  51. writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  52. }
  53. static void crossbar_writeb(int irq_no, int cb_no)
  54. {
  55. writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  56. }
  57. static struct irq_chip crossbar_chip = {
  58. .name = "CBAR",
  59. .irq_eoi = irq_chip_eoi_parent,
  60. .irq_mask = irq_chip_mask_parent,
  61. .irq_unmask = irq_chip_unmask_parent,
  62. .irq_retrigger = irq_chip_retrigger_hierarchy,
  63. .irq_set_type = irq_chip_set_type_parent,
  64. .flags = IRQCHIP_MASK_ON_SUSPEND |
  65. IRQCHIP_SKIP_SET_WAKE,
  66. #ifdef CONFIG_SMP
  67. .irq_set_affinity = irq_chip_set_affinity_parent,
  68. #endif
  69. };
  70. static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
  71. irq_hw_number_t hwirq)
  72. {
  73. struct of_phandle_args args;
  74. int i;
  75. int err;
  76. raw_spin_lock(&cb->lock);
  77. for (i = cb->int_max - 1; i >= 0; i--) {
  78. if (cb->irq_map[i] == IRQ_FREE) {
  79. cb->irq_map[i] = hwirq;
  80. break;
  81. }
  82. }
  83. raw_spin_unlock(&cb->lock);
  84. if (i < 0)
  85. return -ENODEV;
  86. args.np = domain->parent->of_node;
  87. args.args_count = 3;
  88. args.args[0] = 0; /* SPI */
  89. args.args[1] = i;
  90. args.args[2] = IRQ_TYPE_LEVEL_HIGH;
  91. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
  92. if (err)
  93. cb->irq_map[i] = IRQ_FREE;
  94. else
  95. cb->write(i, hwirq);
  96. return err;
  97. }
  98. static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
  99. unsigned int nr_irqs, void *data)
  100. {
  101. struct of_phandle_args *args = data;
  102. irq_hw_number_t hwirq;
  103. int i;
  104. if (args->args_count != 3)
  105. return -EINVAL; /* Not GIC compliant */
  106. if (args->args[0] != 0)
  107. return -EINVAL; /* No PPI should point to this domain */
  108. hwirq = args->args[1];
  109. if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
  110. return -EINVAL; /* Can't deal with this */
  111. for (i = 0; i < nr_irqs; i++) {
  112. int err = allocate_gic_irq(d, virq + i, hwirq + i);
  113. if (err)
  114. return err;
  115. irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
  116. &crossbar_chip, NULL);
  117. }
  118. return 0;
  119. }
  120. /**
  121. * crossbar_domain_free - unmap/free a crossbar<->irq connection
  122. * @domain: domain of irq to unmap
  123. * @virq: virq number
  124. * @nr_irqs: number of irqs to free
  125. *
  126. * We do not maintain a use count of total number of map/unmap
  127. * calls for a particular irq to find out if a irq can be really
  128. * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
  129. * after which irq is anyways unusable. So an explicit map has to be called
  130. * after that.
  131. */
  132. static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
  133. unsigned int nr_irqs)
  134. {
  135. int i;
  136. raw_spin_lock(&cb->lock);
  137. for (i = 0; i < nr_irqs; i++) {
  138. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  139. irq_domain_reset_irq_data(d);
  140. cb->irq_map[d->hwirq] = IRQ_FREE;
  141. cb->write(d->hwirq, cb->safe_map);
  142. }
  143. raw_spin_unlock(&cb->lock);
  144. }
  145. static int crossbar_domain_xlate(struct irq_domain *d,
  146. struct device_node *controller,
  147. const u32 *intspec, unsigned int intsize,
  148. unsigned long *out_hwirq,
  149. unsigned int *out_type)
  150. {
  151. if (d->of_node != controller)
  152. return -EINVAL; /* Shouldn't happen, really... */
  153. if (intsize != 3)
  154. return -EINVAL; /* Not GIC compliant */
  155. if (intspec[0] != 0)
  156. return -EINVAL; /* No PPI should point to this domain */
  157. *out_hwirq = intspec[1];
  158. *out_type = intspec[2];
  159. return 0;
  160. }
  161. static const struct irq_domain_ops crossbar_domain_ops = {
  162. .alloc = crossbar_domain_alloc,
  163. .free = crossbar_domain_free,
  164. .xlate = crossbar_domain_xlate,
  165. };
  166. static int __init crossbar_of_init(struct device_node *node)
  167. {
  168. int i, size, max = 0, reserved = 0, entry;
  169. const __be32 *irqsr;
  170. int ret = -ENOMEM;
  171. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  172. if (!cb)
  173. return ret;
  174. cb->crossbar_base = of_iomap(node, 0);
  175. if (!cb->crossbar_base)
  176. goto err_cb;
  177. of_property_read_u32(node, "ti,max-crossbar-sources",
  178. &cb->max_crossbar_sources);
  179. if (!cb->max_crossbar_sources) {
  180. pr_err("missing 'ti,max-crossbar-sources' property\n");
  181. ret = -EINVAL;
  182. goto err_base;
  183. }
  184. of_property_read_u32(node, "ti,max-irqs", &max);
  185. if (!max) {
  186. pr_err("missing 'ti,max-irqs' property\n");
  187. ret = -EINVAL;
  188. goto err_base;
  189. }
  190. cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
  191. if (!cb->irq_map)
  192. goto err_base;
  193. cb->int_max = max;
  194. for (i = 0; i < max; i++)
  195. cb->irq_map[i] = IRQ_FREE;
  196. /* Get and mark reserved irqs */
  197. irqsr = of_get_property(node, "ti,irqs-reserved", &size);
  198. if (irqsr) {
  199. size /= sizeof(__be32);
  200. for (i = 0; i < size; i++) {
  201. of_property_read_u32_index(node,
  202. "ti,irqs-reserved",
  203. i, &entry);
  204. if (entry >= max) {
  205. pr_err("Invalid reserved entry\n");
  206. ret = -EINVAL;
  207. goto err_irq_map;
  208. }
  209. cb->irq_map[entry] = IRQ_RESERVED;
  210. }
  211. }
  212. /* Skip irqs hardwired to bypass the crossbar */
  213. irqsr = of_get_property(node, "ti,irqs-skip", &size);
  214. if (irqsr) {
  215. size /= sizeof(__be32);
  216. for (i = 0; i < size; i++) {
  217. of_property_read_u32_index(node,
  218. "ti,irqs-skip",
  219. i, &entry);
  220. if (entry >= max) {
  221. pr_err("Invalid skip entry\n");
  222. ret = -EINVAL;
  223. goto err_irq_map;
  224. }
  225. cb->irq_map[entry] = IRQ_SKIP;
  226. }
  227. }
  228. cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
  229. if (!cb->register_offsets)
  230. goto err_irq_map;
  231. of_property_read_u32(node, "ti,reg-size", &size);
  232. switch (size) {
  233. case 1:
  234. cb->write = crossbar_writeb;
  235. break;
  236. case 2:
  237. cb->write = crossbar_writew;
  238. break;
  239. case 4:
  240. cb->write = crossbar_writel;
  241. break;
  242. default:
  243. pr_err("Invalid reg-size property\n");
  244. ret = -EINVAL;
  245. goto err_reg_offset;
  246. break;
  247. }
  248. /*
  249. * Register offsets are not linear because of the
  250. * reserved irqs. so find and store the offsets once.
  251. */
  252. for (i = 0; i < max; i++) {
  253. if (cb->irq_map[i] == IRQ_RESERVED)
  254. continue;
  255. cb->register_offsets[i] = reserved;
  256. reserved += size;
  257. }
  258. of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
  259. /* Initialize the crossbar with safe map to start with */
  260. for (i = 0; i < max; i++) {
  261. if (cb->irq_map[i] == IRQ_RESERVED ||
  262. cb->irq_map[i] == IRQ_SKIP)
  263. continue;
  264. cb->write(i, cb->safe_map);
  265. }
  266. raw_spin_lock_init(&cb->lock);
  267. return 0;
  268. err_reg_offset:
  269. kfree(cb->register_offsets);
  270. err_irq_map:
  271. kfree(cb->irq_map);
  272. err_base:
  273. iounmap(cb->crossbar_base);
  274. err_cb:
  275. kfree(cb);
  276. cb = NULL;
  277. return ret;
  278. }
  279. static int __init irqcrossbar_init(struct device_node *node,
  280. struct device_node *parent)
  281. {
  282. struct irq_domain *parent_domain, *domain;
  283. int err;
  284. if (!parent) {
  285. pr_err("%s: no parent, giving up\n", node->full_name);
  286. return -ENODEV;
  287. }
  288. parent_domain = irq_find_host(parent);
  289. if (!parent_domain) {
  290. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  291. return -ENXIO;
  292. }
  293. err = crossbar_of_init(node);
  294. if (err)
  295. return err;
  296. domain = irq_domain_add_hierarchy(parent_domain, 0,
  297. cb->max_crossbar_sources,
  298. node, &crossbar_domain_ops,
  299. NULL);
  300. if (!domain) {
  301. pr_err("%s: failed to allocated domain\n", node->full_name);
  302. return -ENOMEM;
  303. }
  304. return 0;
  305. }
  306. IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);