irq-crossbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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] & IRQ_TYPE_SENSE_MASK;
  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, max = 0, reserved = 0, entry;
  172. const __be32 *irqsr;
  173. int ret = -ENOMEM;
  174. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  175. if (!cb)
  176. return ret;
  177. cb->crossbar_base = of_iomap(node, 0);
  178. if (!cb->crossbar_base)
  179. goto err_cb;
  180. of_property_read_u32(node, "ti,max-crossbar-sources",
  181. &cb->max_crossbar_sources);
  182. if (!cb->max_crossbar_sources) {
  183. pr_err("missing 'ti,max-crossbar-sources' property\n");
  184. ret = -EINVAL;
  185. goto err_base;
  186. }
  187. of_property_read_u32(node, "ti,max-irqs", &max);
  188. if (!max) {
  189. pr_err("missing 'ti,max-irqs' property\n");
  190. ret = -EINVAL;
  191. goto err_base;
  192. }
  193. cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
  194. if (!cb->irq_map)
  195. goto err_base;
  196. cb->int_max = max;
  197. for (i = 0; i < max; i++)
  198. cb->irq_map[i] = IRQ_FREE;
  199. /* Get and mark reserved irqs */
  200. irqsr = of_get_property(node, "ti,irqs-reserved", &size);
  201. if (irqsr) {
  202. size /= sizeof(__be32);
  203. for (i = 0; i < size; i++) {
  204. of_property_read_u32_index(node,
  205. "ti,irqs-reserved",
  206. i, &entry);
  207. if (entry >= max) {
  208. pr_err("Invalid reserved entry\n");
  209. ret = -EINVAL;
  210. goto err_irq_map;
  211. }
  212. cb->irq_map[entry] = IRQ_RESERVED;
  213. }
  214. }
  215. /* Skip irqs hardwired to bypass the crossbar */
  216. irqsr = of_get_property(node, "ti,irqs-skip", &size);
  217. if (irqsr) {
  218. size /= sizeof(__be32);
  219. for (i = 0; i < size; i++) {
  220. of_property_read_u32_index(node,
  221. "ti,irqs-skip",
  222. i, &entry);
  223. if (entry >= max) {
  224. pr_err("Invalid skip entry\n");
  225. ret = -EINVAL;
  226. goto err_irq_map;
  227. }
  228. cb->irq_map[entry] = IRQ_SKIP;
  229. }
  230. }
  231. cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
  232. if (!cb->register_offsets)
  233. goto err_irq_map;
  234. of_property_read_u32(node, "ti,reg-size", &size);
  235. switch (size) {
  236. case 1:
  237. cb->write = crossbar_writeb;
  238. break;
  239. case 2:
  240. cb->write = crossbar_writew;
  241. break;
  242. case 4:
  243. cb->write = crossbar_writel;
  244. break;
  245. default:
  246. pr_err("Invalid reg-size property\n");
  247. ret = -EINVAL;
  248. goto err_reg_offset;
  249. break;
  250. }
  251. /*
  252. * Register offsets are not linear because of the
  253. * reserved irqs. so find and store the offsets once.
  254. */
  255. for (i = 0; i < max; i++) {
  256. if (cb->irq_map[i] == IRQ_RESERVED)
  257. continue;
  258. cb->register_offsets[i] = reserved;
  259. reserved += size;
  260. }
  261. of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
  262. /* Initialize the crossbar with safe map to start with */
  263. for (i = 0; i < max; i++) {
  264. if (cb->irq_map[i] == IRQ_RESERVED ||
  265. cb->irq_map[i] == IRQ_SKIP)
  266. continue;
  267. cb->write(i, cb->safe_map);
  268. }
  269. raw_spin_lock_init(&cb->lock);
  270. return 0;
  271. err_reg_offset:
  272. kfree(cb->register_offsets);
  273. err_irq_map:
  274. kfree(cb->irq_map);
  275. err_base:
  276. iounmap(cb->crossbar_base);
  277. err_cb:
  278. kfree(cb);
  279. cb = NULL;
  280. return ret;
  281. }
  282. static int __init irqcrossbar_init(struct device_node *node,
  283. struct device_node *parent)
  284. {
  285. struct irq_domain *parent_domain, *domain;
  286. int err;
  287. if (!parent) {
  288. pr_err("%s: no parent, giving up\n", node->full_name);
  289. return -ENODEV;
  290. }
  291. parent_domain = irq_find_host(parent);
  292. if (!parent_domain) {
  293. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  294. return -ENXIO;
  295. }
  296. err = crossbar_of_init(node);
  297. if (err)
  298. return err;
  299. domain = irq_domain_add_hierarchy(parent_domain, 0,
  300. cb->max_crossbar_sources,
  301. node, &crossbar_domain_ops,
  302. NULL);
  303. if (!domain) {
  304. pr_err("%s: failed to allocated domain\n", node->full_name);
  305. return -ENOMEM;
  306. }
  307. return 0;
  308. }
  309. IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);