Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007, Michael Ellerman, IBM Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/irq.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include <linux/msi.h> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 17 | #include <linux/of_platform.h> |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 18 | #include <linux/debugfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 20 | |
| 21 | #include <asm/dcr.h> |
| 22 | #include <asm/machdep.h> |
| 23 | #include <asm/prom.h> |
| 24 | |
| 25 | |
| 26 | /* |
| 27 | * MSIC registers, specified as offsets from dcr_base |
| 28 | */ |
| 29 | #define MSIC_CTRL_REG 0x0 |
| 30 | |
| 31 | /* Base Address registers specify FIFO location in BE memory */ |
| 32 | #define MSIC_BASE_ADDR_HI_REG 0x3 |
| 33 | #define MSIC_BASE_ADDR_LO_REG 0x4 |
| 34 | |
| 35 | /* Hold the read/write offsets into the FIFO */ |
| 36 | #define MSIC_READ_OFFSET_REG 0x5 |
| 37 | #define MSIC_WRITE_OFFSET_REG 0x6 |
| 38 | |
| 39 | |
| 40 | /* MSIC control register flags */ |
| 41 | #define MSIC_CTRL_ENABLE 0x0001 |
| 42 | #define MSIC_CTRL_FIFO_FULL_ENABLE 0x0002 |
| 43 | #define MSIC_CTRL_IRQ_ENABLE 0x0008 |
| 44 | #define MSIC_CTRL_FULL_STOP_ENABLE 0x0010 |
| 45 | |
| 46 | /* |
| 47 | * The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB. |
| 48 | * Currently we're using a 64KB FIFO size. |
| 49 | */ |
| 50 | #define MSIC_FIFO_SIZE_SHIFT 16 |
| 51 | #define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT) |
| 52 | |
| 53 | /* |
| 54 | * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits |
| 55 | * 8-9 of the MSIC control reg. |
| 56 | */ |
| 57 | #define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300) |
| 58 | |
| 59 | /* |
| 60 | * We need to mask the read/write offsets to make sure they stay within |
| 61 | * the bounds of the FIFO. Also they should always be 16-byte aligned. |
| 62 | */ |
| 63 | #define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu) |
| 64 | |
| 65 | /* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */ |
| 66 | #define MSIC_FIFO_ENTRY_SIZE 0x10 |
| 67 | |
| 68 | |
| 69 | struct axon_msic { |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 70 | struct irq_domain *irq_domain; |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 71 | __le32 *fifo_virt; |
| 72 | dma_addr_t fifo_phys; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 73 | dcr_host_t dcr_host; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 74 | u32 read_offset; |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 75 | #ifdef DEBUG |
| 76 | u32 __iomem *trigger; |
| 77 | #endif |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 80 | #ifdef DEBUG |
| 81 | void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic); |
| 82 | #else |
| 83 | static inline void axon_msi_debug_setup(struct device_node *dn, |
| 84 | struct axon_msic *msic) { } |
| 85 | #endif |
| 86 | |
| 87 | |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 88 | static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val) |
| 89 | { |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 90 | pr_devel("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 91 | |
Michael Ellerman | 83f34df | 2007-10-15 19:34:36 +1000 | [diff] [blame] | 92 | dcr_write(msic->dcr_host, dcr_n, val); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 95 | static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc) |
| 96 | { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 97 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 98 | struct axon_msic *msic = irq_get_handler_data(irq); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 99 | u32 write_offset, msi; |
| 100 | int idx; |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 101 | int retry = 0; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 102 | |
Michael Ellerman | 2843e7f | 2007-10-15 19:34:38 +1000 | [diff] [blame] | 103 | write_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG); |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 104 | pr_devel("axon_msi: original write_offset 0x%x\n", write_offset); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 105 | |
| 106 | /* write_offset doesn't wrap properly, so we have to mask it */ |
| 107 | write_offset &= MSIC_FIFO_SIZE_MASK; |
| 108 | |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 109 | while (msic->read_offset != write_offset && retry < 100) { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 110 | idx = msic->read_offset / sizeof(__le32); |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 111 | msi = le32_to_cpu(msic->fifo_virt[idx]); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 112 | msi &= 0xFFFF; |
| 113 | |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 114 | pr_devel("axon_msi: woff %x roff %x msi %x\n", |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 115 | write_offset, msic->read_offset, msi); |
| 116 | |
Milton Miller | 9553361 | 2011-05-10 19:30:33 +0000 | [diff] [blame] | 117 | if (msi < NR_IRQS && irq_get_chip_data(msi) == msic) { |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 118 | generic_handle_irq(msi); |
| 119 | msic->fifo_virt[idx] = cpu_to_le32(0xffffffff); |
| 120 | } else { |
| 121 | /* |
| 122 | * Reading the MSIC_WRITE_OFFSET_REG does not |
| 123 | * reliably flush the outstanding DMA to the |
| 124 | * FIFO buffer. Here we were reading stale |
| 125 | * data, so we need to retry. |
| 126 | */ |
| 127 | udelay(1); |
| 128 | retry++; |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 129 | pr_devel("axon_msi: invalid irq 0x%x!\n", msi); |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if (retry) { |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 134 | pr_devel("axon_msi: late irq 0x%x, retry %d\n", |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 135 | msi, retry); |
| 136 | retry = 0; |
| 137 | } |
| 138 | |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 139 | msic->read_offset += MSIC_FIFO_ENTRY_SIZE; |
| 140 | msic->read_offset &= MSIC_FIFO_SIZE_MASK; |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 141 | } |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 142 | |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 143 | if (retry) { |
| 144 | printk(KERN_WARNING "axon_msi: irq timed out\n"); |
| 145 | |
| 146 | msic->read_offset += MSIC_FIFO_ENTRY_SIZE; |
| 147 | msic->read_offset &= MSIC_FIFO_SIZE_MASK; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 150 | chip->irq_eoi(&desc->irq_data); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static struct axon_msic *find_msi_translator(struct pci_dev *dev) |
| 154 | { |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 155 | struct irq_domain *irq_domain; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 156 | struct device_node *dn, *tmp; |
| 157 | const phandle *ph; |
| 158 | struct axon_msic *msic = NULL; |
| 159 | |
Michael Ellerman | db220b2 | 2007-09-17 16:03:45 +1000 | [diff] [blame] | 160 | dn = of_node_get(pci_device_to_OF_node(dev)); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 161 | if (!dn) { |
| 162 | dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n"); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Michael Ellerman | 988479e | 2008-04-24 12:08:54 +1000 | [diff] [blame] | 166 | for (; dn; dn = of_get_next_parent(dn)) { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 167 | ph = of_get_property(dn, "msi-translator", NULL); |
| 168 | if (ph) |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | if (!ph) { |
| 173 | dev_dbg(&dev->dev, |
| 174 | "axon_msi: no msi-translator property found\n"); |
| 175 | goto out_error; |
| 176 | } |
| 177 | |
| 178 | tmp = dn; |
| 179 | dn = of_find_node_by_phandle(*ph); |
Stephen Rothwell | c6d0117 | 2008-02-05 13:13:15 +1100 | [diff] [blame] | 180 | of_node_put(tmp); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 181 | if (!dn) { |
| 182 | dev_dbg(&dev->dev, |
| 183 | "axon_msi: msi-translator doesn't point to a node\n"); |
| 184 | goto out_error; |
| 185 | } |
| 186 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 187 | irq_domain = irq_find_host(dn); |
| 188 | if (!irq_domain) { |
| 189 | dev_dbg(&dev->dev, "axon_msi: no irq_domain found for node %s\n", |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 190 | dn->full_name); |
| 191 | goto out_error; |
| 192 | } |
| 193 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 194 | msic = irq_domain->host_data; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 195 | |
| 196 | out_error: |
| 197 | of_node_put(dn); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 198 | |
| 199 | return msic; |
| 200 | } |
| 201 | |
| 202 | static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type) |
| 203 | { |
| 204 | if (!find_msi_translator(dev)) |
| 205 | return -ENODEV; |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg) |
| 211 | { |
Michael Ellerman | 988479e | 2008-04-24 12:08:54 +1000 | [diff] [blame] | 212 | struct device_node *dn; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 213 | struct msi_desc *entry; |
| 214 | int len; |
| 215 | const u32 *prop; |
| 216 | |
Michael Ellerman | db220b2 | 2007-09-17 16:03:45 +1000 | [diff] [blame] | 217 | dn = of_node_get(pci_device_to_OF_node(dev)); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 218 | if (!dn) { |
| 219 | dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n"); |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
| 223 | entry = list_first_entry(&dev->msi_list, struct msi_desc, list); |
| 224 | |
Michael Ellerman | 988479e | 2008-04-24 12:08:54 +1000 | [diff] [blame] | 225 | for (; dn; dn = of_get_next_parent(dn)) { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 226 | if (entry->msi_attrib.is_64) { |
| 227 | prop = of_get_property(dn, "msi-address-64", &len); |
| 228 | if (prop) |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | prop = of_get_property(dn, "msi-address-32", &len); |
| 233 | if (prop) |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | if (!prop) { |
| 238 | dev_dbg(&dev->dev, |
| 239 | "axon_msi: no msi-address-(32|64) properties found\n"); |
| 240 | return -ENOENT; |
| 241 | } |
| 242 | |
| 243 | switch (len) { |
| 244 | case 8: |
| 245 | msg->address_hi = prop[0]; |
| 246 | msg->address_lo = prop[1]; |
| 247 | break; |
| 248 | case 4: |
| 249 | msg->address_hi = 0; |
| 250 | msg->address_lo = prop[0]; |
| 251 | break; |
| 252 | default: |
| 253 | dev_dbg(&dev->dev, |
| 254 | "axon_msi: malformed msi-address-(32|64) property\n"); |
| 255 | of_node_put(dn); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | |
| 259 | of_node_put(dn); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 265 | { |
| 266 | unsigned int virq, rc; |
| 267 | struct msi_desc *entry; |
| 268 | struct msi_msg msg; |
| 269 | struct axon_msic *msic; |
| 270 | |
| 271 | msic = find_msi_translator(dev); |
| 272 | if (!msic) |
| 273 | return -ENODEV; |
| 274 | |
| 275 | rc = setup_msi_msg_address(dev, &msg); |
| 276 | if (rc) |
| 277 | return rc; |
| 278 | |
| 279 | /* We rely on being able to stash a virq in a u16 */ |
| 280 | BUILD_BUG_ON(NR_IRQS > 65536); |
| 281 | |
| 282 | list_for_each_entry(entry, &dev->msi_list, list) { |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 283 | virq = irq_create_direct_mapping(msic->irq_domain); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 284 | if (virq == NO_IRQ) { |
| 285 | dev_warn(&dev->dev, |
| 286 | "axon_msi: virq allocation failed!\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq); |
| 290 | |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 291 | irq_set_msi_desc(virq, entry); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 292 | msg.data = virq; |
| 293 | write_msi_msg(virq, &msg); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static void axon_msi_teardown_msi_irqs(struct pci_dev *dev) |
| 300 | { |
| 301 | struct msi_desc *entry; |
| 302 | |
| 303 | dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n"); |
| 304 | |
| 305 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 306 | if (entry->irq == NO_IRQ) |
| 307 | continue; |
| 308 | |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 309 | irq_set_msi_desc(entry->irq, NULL); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 310 | irq_dispose_mapping(entry->irq); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static struct irq_chip msic_irq_chip = { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 315 | .irq_mask = mask_msi_irq, |
| 316 | .irq_unmask = unmask_msi_irq, |
| 317 | .irq_shutdown = mask_msi_irq, |
Thomas Gleixner | b27df67 | 2009-11-18 23:44:21 +0000 | [diff] [blame] | 318 | .name = "AXON-MSI", |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 319 | }; |
| 320 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 321 | static int msic_host_map(struct irq_domain *h, unsigned int virq, |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 322 | irq_hw_number_t hw) |
| 323 | { |
Milton Miller | 9553361 | 2011-05-10 19:30:33 +0000 | [diff] [blame] | 324 | irq_set_chip_data(virq, h->host_data); |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 325 | irq_set_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Grant Likely | 9f70b8e | 2012-01-26 12:24:34 -0700 | [diff] [blame] | 330 | static const struct irq_domain_ops msic_host_ops = { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 331 | .map = msic_host_map, |
| 332 | }; |
| 333 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 334 | static void axon_msi_shutdown(struct platform_device *device) |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 335 | { |
Michael Ellerman | 86c2765 | 2009-06-10 19:06:34 +0000 | [diff] [blame] | 336 | struct axon_msic *msic = dev_get_drvdata(&device->dev); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 337 | u32 tmp; |
| 338 | |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 339 | pr_devel("axon_msi: disabling %s\n", |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 340 | msic->irq_domain->of_node->full_name); |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 341 | tmp = dcr_read(msic->dcr_host, MSIC_CTRL_REG); |
| 342 | tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE; |
| 343 | msic_dcr_write(msic, MSIC_CTRL_REG, tmp); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 346 | static int axon_msi_probe(struct platform_device *device) |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 347 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 348 | struct device_node *dn = device->dev.of_node; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 349 | struct axon_msic *msic; |
| 350 | unsigned int virq; |
Michael Ellerman | 4acb8896 | 2007-09-17 16:05:02 +1000 | [diff] [blame] | 351 | int dcr_base, dcr_len; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 352 | |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 353 | pr_devel("axon_msi: setting up dn %s\n", dn->full_name); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 354 | |
| 355 | msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL); |
| 356 | if (!msic) { |
| 357 | printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n", |
| 358 | dn->full_name); |
| 359 | goto out; |
| 360 | } |
| 361 | |
Michael Ellerman | 4acb8896 | 2007-09-17 16:05:02 +1000 | [diff] [blame] | 362 | dcr_base = dcr_resource_start(dn, 0); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 363 | dcr_len = dcr_resource_len(dn, 0); |
| 364 | |
Michael Ellerman | 4acb8896 | 2007-09-17 16:05:02 +1000 | [diff] [blame] | 365 | if (dcr_base == 0 || dcr_len == 0) { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 366 | printk(KERN_ERR |
| 367 | "axon_msi: couldn't parse dcr properties on %s\n", |
| 368 | dn->full_name); |
Michael Ellerman | aee7a28 | 2009-10-12 14:29:40 +0000 | [diff] [blame] | 369 | goto out_free_msic; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Michael Ellerman | 4acb8896 | 2007-09-17 16:05:02 +1000 | [diff] [blame] | 372 | msic->dcr_host = dcr_map(dn, dcr_base, dcr_len); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 373 | if (!DCR_MAP_OK(msic->dcr_host)) { |
| 374 | printk(KERN_ERR "axon_msi: dcr_map failed for %s\n", |
| 375 | dn->full_name); |
| 376 | goto out_free_msic; |
| 377 | } |
| 378 | |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 379 | msic->fifo_virt = dma_alloc_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES, |
| 380 | &msic->fifo_phys, GFP_KERNEL); |
| 381 | if (!msic->fifo_virt) { |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 382 | printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n", |
| 383 | dn->full_name); |
| 384 | goto out_free_msic; |
| 385 | } |
| 386 | |
Michael Ellerman | 997526d | 2008-05-26 12:12:30 +1000 | [diff] [blame] | 387 | virq = irq_of_parse_and_map(dn, 0); |
| 388 | if (virq == NO_IRQ) { |
| 389 | printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n", |
| 390 | dn->full_name); |
| 391 | goto out_free_fifo; |
| 392 | } |
Arnd Bergmann | d015fe9 | 2008-11-28 09:51:22 +0000 | [diff] [blame] | 393 | memset(msic->fifo_virt, 0xff, MSIC_FIFO_SIZE_BYTES); |
Michael Ellerman | 997526d | 2008-05-26 12:12:30 +1000 | [diff] [blame] | 394 | |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 395 | msic->irq_domain = irq_domain_add_nomap(dn, 0, &msic_host_ops, msic); |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 396 | if (!msic->irq_domain) { |
| 397 | printk(KERN_ERR "axon_msi: couldn't allocate irq_domain for %s\n", |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 398 | dn->full_name); |
| 399 | goto out_free_fifo; |
| 400 | } |
| 401 | |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 402 | irq_set_handler_data(virq, msic); |
| 403 | irq_set_chained_handler(virq, axon_msi_cascade); |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 404 | pr_devel("axon_msi: irq 0x%x setup for axon_msi\n", virq); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 405 | |
| 406 | /* Enable the MSIC hardware */ |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 407 | msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, msic->fifo_phys >> 32); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 408 | msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG, |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 409 | msic->fifo_phys & 0xFFFFFFFF); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 410 | msic_dcr_write(msic, MSIC_CTRL_REG, |
| 411 | MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE | |
| 412 | MSIC_CTRL_FIFO_SIZE); |
| 413 | |
Arnd Bergmann | 23e0e8a | 2008-12-12 09:19:50 +0000 | [diff] [blame] | 414 | msic->read_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG) |
| 415 | & MSIC_FIFO_SIZE_MASK; |
| 416 | |
Michael Ellerman | 86c2765 | 2009-06-10 19:06:34 +0000 | [diff] [blame] | 417 | dev_set_drvdata(&device->dev, msic); |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 418 | |
| 419 | ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs; |
| 420 | ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs; |
| 421 | ppc_md.msi_check_device = axon_msi_check_device; |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 422 | |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 423 | axon_msi_debug_setup(dn, msic); |
| 424 | |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 425 | printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name); |
| 426 | |
| 427 | return 0; |
| 428 | |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 429 | out_free_fifo: |
Michael Ellerman | de4c928 | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 430 | dma_free_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES, msic->fifo_virt, |
| 431 | msic->fifo_phys); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 432 | out_free_msic: |
| 433 | kfree(msic); |
| 434 | out: |
| 435 | |
| 436 | return -1; |
| 437 | } |
| 438 | |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 439 | static const struct of_device_id axon_msi_device_id[] = { |
| 440 | { |
| 441 | .compatible = "ibm,axon-msic" |
| 442 | }, |
| 443 | {} |
| 444 | }; |
| 445 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 446 | static struct platform_driver axon_msi_driver = { |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 447 | .probe = axon_msi_probe, |
| 448 | .shutdown = axon_msi_shutdown, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 449 | .driver = { |
| 450 | .name = "axon-msi", |
| 451 | .owner = THIS_MODULE, |
| 452 | .of_match_table = axon_msi_device_id, |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 453 | }, |
| 454 | }; |
| 455 | |
| 456 | static int __init axon_msi_init(void) |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 457 | { |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 458 | return platform_driver_register(&axon_msi_driver); |
Michael Ellerman | ce21b3c | 2007-07-20 21:39:28 +0200 | [diff] [blame] | 459 | } |
Michael Ellerman | e4347df | 2008-01-25 16:59:14 +1100 | [diff] [blame] | 460 | subsys_initcall(axon_msi_init); |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 461 | |
| 462 | |
| 463 | #ifdef DEBUG |
| 464 | static int msic_set(void *data, u64 val) |
| 465 | { |
| 466 | struct axon_msic *msic = data; |
| 467 | out_le32(msic->trigger, val); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int msic_get(void *data, u64 *val) |
| 472 | { |
| 473 | *val = 0; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | DEFINE_SIMPLE_ATTRIBUTE(fops_msic, msic_get, msic_set, "%llu\n"); |
| 478 | |
| 479 | void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic) |
| 480 | { |
| 481 | char name[8]; |
| 482 | u64 addr; |
| 483 | |
| 484 | addr = of_translate_address(dn, of_get_property(dn, "reg", NULL)); |
| 485 | if (addr == OF_BAD_ADDR) { |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 486 | pr_devel("axon_msi: couldn't translate reg property\n"); |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | |
| 490 | msic->trigger = ioremap(addr, 0x4); |
| 491 | if (!msic->trigger) { |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 492 | pr_devel("axon_msi: ioremap failed\n"); |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
| 496 | snprintf(name, sizeof(name), "msic_%d", of_node_to_nid(dn)); |
| 497 | |
| 498 | if (!debugfs_create_file(name, 0600, powerpc_debugfs_root, |
| 499 | msic, &fops_msic)) { |
Michael Ellerman | 33875f0 | 2009-06-17 18:13:53 +0000 | [diff] [blame] | 500 | pr_devel("axon_msi: debugfs_create_file failed!\n"); |
Michael Ellerman | 72cac21 | 2008-05-23 14:21:30 +1000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | } |
| 504 | #endif /* DEBUG */ |