irq-crossbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 irq_fwspec fwspec;
  74. int i;
  75. int err;
  76. if (!irq_domain_get_of_node(domain->parent))
  77. return -EINVAL;
  78. raw_spin_lock(&cb->lock);
  79. for (i = cb->int_max - 1; i >= 0; i--) {
  80. if (cb->irq_map[i] == IRQ_FREE) {
  81. cb->irq_map[i] = hwirq;
  82. break;
  83. }
  84. }
  85. raw_spin_unlock(&cb->lock);
  86. if (i < 0)
  87. return -ENODEV;
  88. fwspec.fwnode = domain->parent->fwnode;
  89. fwspec.param_count = 3;
  90. fwspec.param[0] = 0; /* SPI */
  91. fwspec.param[1] = i;
  92. fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
  93. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  94. if (err)
  95. cb->irq_map[i] = IRQ_FREE;
  96. else
  97. cb->write(i, hwirq);
  98. return err;
  99. }
  100. static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
  101. unsigned int nr_irqs, void *data)
  102. {
  103. struct irq_fwspec *fwspec = data;
  104. irq_hw_number_t hwirq;
  105. int i;
  106. if (fwspec->param_count != 3)
  107. return -EINVAL; /* Not GIC compliant */
  108. if (fwspec->param[0] != 0)
  109. return -EINVAL; /* No PPI should point to this domain */
  110. hwirq = fwspec->param[1];
  111. if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
  112. return -EINVAL; /* Can't deal with this */
  113. for (i = 0; i < nr_irqs; i++) {
  114. int err = allocate_gic_irq(d, virq + i, hwirq + i);
  115. if (err)
  116. return err;
  117. irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
  118. &crossbar_chip, NULL);
  119. }
  120. return 0;
  121. }
  122. /**
  123. * crossbar_domain_free - unmap/free a crossbar<->irq connection
  124. * @domain: domain of irq to unmap
  125. * @virq: virq number
  126. * @nr_irqs: number of irqs to free
  127. *
  128. * We do not maintain a use count of total number of map/unmap
  129. * calls for a particular irq to find out if a irq can be really
  130. * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
  131. * after which irq is anyways unusable. So an explicit map has to be called
  132. * after that.
  133. */
  134. static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
  135. unsigned int nr_irqs)
  136. {
  137. int i;
  138. raw_spin_lock(&cb->lock);
  139. for (i = 0; i < nr_irqs; i++) {
  140. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  141. irq_domain_reset_irq_data(d);
  142. cb->irq_map[d->hwirq] = IRQ_FREE;
  143. cb->write(d->hwirq, cb->safe_map);
  144. }
  145. raw_spin_unlock(&cb->lock);
  146. }
  147. static int crossbar_domain_translate(struct irq_domain *d,
  148. struct irq_fwspec *fwspec,
  149. unsigned long *hwirq,
  150. unsigned int *type)
  151. {
  152. if (is_of_node(fwspec->fwnode)) {
  153. if (fwspec->param_count != 3)
  154. return -EINVAL;
  155. /* No PPI should point to this domain */
  156. if (fwspec->param[0] != 0)
  157. return -EINVAL;
  158. *hwirq = fwspec->param[1];
  159. *type = fwspec->param[2];
  160. return 0;
  161. }
  162. return -EINVAL;
  163. }
  164. static const struct irq_domain_ops crossbar_domain_ops = {
  165. .alloc = crossbar_domain_alloc,
  166. .free = crossbar_domain_free,
  167. .translate = crossbar_domain_translate,
  168. };
  169. static int __init crossbar_of_init(struct device_node *node)
  170. {
  171. int i, size, reserved = 0;
  172. u32 max = 0, entry, reg_size;
  173. const __be32 *irqsr;
  174. int ret = -ENOMEM;
  175. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  176. if (!cb)
  177. return ret;
  178. cb->crossbar_base = of_iomap(node, 0);
  179. if (!cb->crossbar_base)
  180. goto err_cb;
  181. of_property_read_u32(node, "ti,max-crossbar-sources",
  182. &cb->max_crossbar_sources);
  183. if (!cb->max_crossbar_sources) {
  184. pr_err("missing 'ti,max-crossbar-sources' property\n");
  185. ret = -EINVAL;
  186. goto err_base;
  187. }
  188. of_property_read_u32(node, "ti,max-irqs", &max);
  189. if (!max) {
  190. pr_err("missing 'ti,max-irqs' property\n");
  191. ret = -EINVAL;
  192. goto err_base;
  193. }
  194. cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
  195. if (!cb->irq_map)
  196. goto err_base;
  197. cb->int_max = max;
  198. for (i = 0; i < max; i++)
  199. cb->irq_map[i] = IRQ_FREE;
  200. /* Get and mark reserved irqs */
  201. irqsr = of_get_property(node, "ti,irqs-reserved", &size);
  202. if (irqsr) {
  203. size /= sizeof(__be32);
  204. for (i = 0; i < size; i++) {
  205. of_property_read_u32_index(node,
  206. "ti,irqs-reserved",
  207. i, &entry);
  208. if (entry >= max) {
  209. pr_err("Invalid reserved entry\n");
  210. ret = -EINVAL;
  211. goto err_irq_map;
  212. }
  213. cb->irq_map[entry] = IRQ_RESERVED;
  214. }
  215. }
  216. /* Skip irqs hardwired to bypass the crossbar */
  217. irqsr = of_get_property(node, "ti,irqs-skip", &size);
  218. if (irqsr) {
  219. size /= sizeof(__be32);
  220. for (i = 0; i < size; i++) {
  221. of_property_read_u32_index(node,
  222. "ti,irqs-skip",
  223. i, &entry);
  224. if (entry >= max) {
  225. pr_err("Invalid skip entry\n");
  226. ret = -EINVAL;
  227. goto err_irq_map;
  228. }
  229. cb->irq_map[entry] = IRQ_SKIP;
  230. }
  231. }
  232. cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
  233. if (!cb->register_offsets)
  234. goto err_irq_map;
  235. of_property_read_u32(node, "ti,reg-size", &reg_size);
  236. switch (reg_size) {
  237. case 1:
  238. cb->write = crossbar_writeb;
  239. break;
  240. case 2:
  241. cb->write = crossbar_writew;
  242. break;
  243. case 4:
  244. cb->write = crossbar_writel;
  245. break;
  246. default:
  247. pr_err("Invalid reg-size property\n");
  248. ret = -EINVAL;
  249. goto err_reg_offset;
  250. break;
  251. }
  252. /*
  253. * Register offsets are not linear because of the
  254. * reserved irqs. so find and store the offsets once.
  255. */
  256. for (i = 0; i < max; i++) {
  257. if (cb->irq_map[i] == IRQ_RESERVED)
  258. continue;
  259. cb->register_offsets[i] = reserved;
  260. reserved += reg_size;
  261. }
  262. of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
  263. /* Initialize the crossbar with safe map to start with */
  264. for (i = 0; i < max; i++) {
  265. if (cb->irq_map[i] == IRQ_RESERVED ||
  266. cb->irq_map[i] == IRQ_SKIP)
  267. continue;
  268. cb->write(i, cb->safe_map);
  269. }
  270. raw_spin_lock_init(&cb->lock);
  271. return 0;
  272. err_reg_offset:
  273. kfree(cb->register_offsets);
  274. err_irq_map:
  275. kfree(cb->irq_map);
  276. err_base:
  277. iounmap(cb->crossbar_base);
  278. err_cb:
  279. kfree(cb);
  280. cb = NULL;
  281. return ret;
  282. }
  283. static int __init irqcrossbar_init(struct device_node *node,
  284. struct device_node *parent)
  285. {
  286. struct irq_domain *parent_domain, *domain;
  287. int err;
  288. if (!parent) {
  289. pr_err("%s: no parent, giving up\n", node->full_name);
  290. return -ENODEV;
  291. }
  292. parent_domain = irq_find_host(parent);
  293. if (!parent_domain) {
  294. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  295. return -ENXIO;
  296. }
  297. err = crossbar_of_init(node);
  298. if (err)
  299. return err;
  300. domain = irq_domain_add_hierarchy(parent_domain, 0,
  301. cb->max_crossbar_sources,
  302. node, &crossbar_domain_ops,
  303. NULL);
  304. if (!domain) {
  305. pr_err("%s: failed to allocated domain\n", node->full_name);
  306. return -ENOMEM;
  307. }
  308. return 0;
  309. }
  310. IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);