| Rupjyoti Sarmah | 3fb7933 | 2011-03-29 23:10:24 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Adding PCI-E MSI support for PPC4XX SoCs. | 
|  | 3 | * | 
|  | 4 | * Copyright (c) 2010, Applied Micro Circuits Corporation | 
|  | 5 | * Authors:	Tirumala R Marri <tmarri@apm.com> | 
|  | 6 | *		Feng Kan <fkan@apm.com> | 
|  | 7 | * | 
|  | 8 | * This program is free software; you can redistribute it and/or | 
|  | 9 | * modify it under the terms of the GNU General Public License as | 
|  | 10 | * published by the Free Software Foundation; either version 2 of | 
|  | 11 | * the License, or (at your option) any later version. | 
|  | 12 | * | 
|  | 13 | * This program is distributed in the hope that it will be useful, | 
|  | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | * GNU General Public License for more details. | 
|  | 17 | * | 
|  | 18 | * You should have received a copy of the GNU General Public License | 
|  | 19 | * along with this program; if not, write to the Free Software | 
|  | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | 
|  | 21 | * MA 02111-1307 USA | 
|  | 22 | */ | 
|  | 23 |  | 
|  | 24 | #include <linux/irq.h> | 
|  | 25 | #include <linux/bootmem.h> | 
|  | 26 | #include <linux/pci.h> | 
|  | 27 | #include <linux/msi.h> | 
|  | 28 | #include <linux/of_platform.h> | 
|  | 29 | #include <linux/interrupt.h> | 
| Paul Gortmaker | 9308794 | 2011-07-29 16:19:31 +1000 | [diff] [blame] | 30 | #include <linux/export.h> | 
| Rupjyoti Sarmah | 3fb7933 | 2011-03-29 23:10:24 +0000 | [diff] [blame] | 31 | #include <asm/prom.h> | 
|  | 32 | #include <asm/hw_irq.h> | 
|  | 33 | #include <asm/ppc-pci.h> | 
|  | 34 | #include <boot/dcr.h> | 
|  | 35 | #include <asm/dcr-regs.h> | 
|  | 36 | #include <asm/msi_bitmap.h> | 
|  | 37 |  | 
|  | 38 | #define PEIH_TERMADH	0x00 | 
|  | 39 | #define PEIH_TERMADL	0x08 | 
|  | 40 | #define PEIH_MSIED	0x10 | 
|  | 41 | #define PEIH_MSIMK	0x18 | 
|  | 42 | #define PEIH_MSIASS	0x20 | 
|  | 43 | #define PEIH_FLUSH0	0x30 | 
|  | 44 | #define PEIH_FLUSH1	0x38 | 
|  | 45 | #define PEIH_CNTRST	0x48 | 
|  | 46 | #define NR_MSI_IRQS	4 | 
|  | 47 |  | 
|  | 48 | struct ppc4xx_msi { | 
|  | 49 | u32 msi_addr_lo; | 
|  | 50 | u32 msi_addr_hi; | 
|  | 51 | void __iomem *msi_regs; | 
|  | 52 | int msi_virqs[NR_MSI_IRQS]; | 
|  | 53 | struct msi_bitmap bitmap; | 
|  | 54 | struct device_node *msi_dev; | 
|  | 55 | }; | 
|  | 56 |  | 
|  | 57 | static struct ppc4xx_msi ppc4xx_msi; | 
|  | 58 |  | 
|  | 59 | static int ppc4xx_msi_init_allocator(struct platform_device *dev, | 
|  | 60 | struct ppc4xx_msi *msi_data) | 
|  | 61 | { | 
|  | 62 | int err; | 
|  | 63 |  | 
|  | 64 | err = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS, | 
|  | 65 | dev->dev.of_node); | 
|  | 66 | if (err) | 
|  | 67 | return err; | 
|  | 68 |  | 
|  | 69 | err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap); | 
|  | 70 | if (err < 0) { | 
|  | 71 | msi_bitmap_free(&msi_data->bitmap); | 
|  | 72 | return err; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | return 0; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | 
|  | 79 | { | 
|  | 80 | int int_no = -ENOMEM; | 
|  | 81 | unsigned int virq; | 
|  | 82 | struct msi_msg msg; | 
|  | 83 | struct msi_desc *entry; | 
|  | 84 | struct ppc4xx_msi *msi_data = &ppc4xx_msi; | 
|  | 85 |  | 
|  | 86 | list_for_each_entry(entry, &dev->msi_list, list) { | 
|  | 87 | int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1); | 
|  | 88 | if (int_no >= 0) | 
|  | 89 | break; | 
|  | 90 | if (int_no < 0) { | 
|  | 91 | pr_debug("%s: fail allocating msi interrupt\n", | 
|  | 92 | __func__); | 
|  | 93 | } | 
|  | 94 | virq = irq_of_parse_and_map(msi_data->msi_dev, int_no); | 
|  | 95 | if (virq == NO_IRQ) { | 
|  | 96 | dev_err(&dev->dev, "%s: fail mapping irq\n", __func__); | 
|  | 97 | msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1); | 
|  | 98 | return -ENOSPC; | 
|  | 99 | } | 
|  | 100 | dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq); | 
|  | 101 |  | 
|  | 102 | /* Setup msi address space */ | 
|  | 103 | msg.address_hi = msi_data->msi_addr_hi; | 
|  | 104 | msg.address_lo = msi_data->msi_addr_lo; | 
|  | 105 |  | 
|  | 106 | irq_set_msi_desc(virq, entry); | 
|  | 107 | msg.data = int_no; | 
|  | 108 | write_msi_msg(virq, &msg); | 
|  | 109 | } | 
|  | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | void ppc4xx_teardown_msi_irqs(struct pci_dev *dev) | 
|  | 114 | { | 
|  | 115 | struct msi_desc *entry; | 
|  | 116 | struct ppc4xx_msi *msi_data = &ppc4xx_msi; | 
|  | 117 |  | 
|  | 118 | dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n"); | 
|  | 119 |  | 
|  | 120 | list_for_each_entry(entry, &dev->msi_list, list) { | 
|  | 121 | if (entry->irq == NO_IRQ) | 
|  | 122 | continue; | 
|  | 123 | irq_set_msi_desc(entry->irq, NULL); | 
|  | 124 | msi_bitmap_free_hwirqs(&msi_data->bitmap, | 
|  | 125 | virq_to_hw(entry->irq), 1); | 
|  | 126 | irq_dispose_mapping(entry->irq); | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | static int ppc4xx_msi_check_device(struct pci_dev *pdev, int nvec, int type) | 
|  | 131 | { | 
|  | 132 | dev_dbg(&pdev->dev, "PCIE-MSI:%s called. vec %x type %d\n", | 
|  | 133 | __func__, nvec, type); | 
|  | 134 | if (type == PCI_CAP_ID_MSIX) | 
|  | 135 | pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n"); | 
|  | 136 |  | 
|  | 137 | return 0; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | static int ppc4xx_setup_pcieh_hw(struct platform_device *dev, | 
|  | 141 | struct resource res, struct ppc4xx_msi *msi) | 
|  | 142 | { | 
|  | 143 | const u32 *msi_data; | 
|  | 144 | const u32 *msi_mask; | 
|  | 145 | const u32 *sdr_addr; | 
|  | 146 | dma_addr_t msi_phys; | 
|  | 147 | void *msi_virt; | 
|  | 148 |  | 
|  | 149 | sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL); | 
|  | 150 | if (!sdr_addr) | 
|  | 151 | return -1; | 
|  | 152 |  | 
|  | 153 | SDR0_WRITE(sdr_addr, (u64)res.start >> 32);	 /*HIGH addr */ | 
|  | 154 | SDR0_WRITE(sdr_addr + 1, res.start & 0xFFFFFFFF); /* Low addr */ | 
|  | 155 |  | 
|  | 156 |  | 
|  | 157 | msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi"); | 
|  | 158 | if (msi->msi_dev) | 
|  | 159 | return -ENODEV; | 
|  | 160 |  | 
|  | 161 | msi->msi_regs = of_iomap(msi->msi_dev, 0); | 
|  | 162 | if (!msi->msi_regs) { | 
|  | 163 | dev_err(&dev->dev, "of_iomap problem failed\n"); | 
|  | 164 | return -ENOMEM; | 
|  | 165 | } | 
|  | 166 | dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n", | 
|  | 167 | (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs)); | 
|  | 168 |  | 
|  | 169 | msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL); | 
|  | 170 | msi->msi_addr_hi = 0x0; | 
|  | 171 | msi->msi_addr_lo = (u32) msi_phys; | 
|  | 172 | dev_dbg(&dev->dev, "PCIE-MSI: msi address 0x%x\n", msi->msi_addr_lo); | 
|  | 173 |  | 
|  | 174 | /* Progam the Interrupt handler Termination addr registers */ | 
|  | 175 | out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi); | 
|  | 176 | out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo); | 
|  | 177 |  | 
|  | 178 | msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL); | 
|  | 179 | if (!msi_data) | 
|  | 180 | return -1; | 
|  | 181 | msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL); | 
|  | 182 | if (!msi_mask) | 
|  | 183 | return -1; | 
|  | 184 | /* Program MSI Expected data and Mask bits */ | 
|  | 185 | out_be32(msi->msi_regs + PEIH_MSIED, *msi_data); | 
|  | 186 | out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask); | 
|  | 187 |  | 
|  | 188 | return 0; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | static int ppc4xx_of_msi_remove(struct platform_device *dev) | 
|  | 192 | { | 
|  | 193 | struct ppc4xx_msi *msi = dev->dev.platform_data; | 
|  | 194 | int i; | 
|  | 195 | int virq; | 
|  | 196 |  | 
|  | 197 | for (i = 0; i < NR_MSI_IRQS; i++) { | 
|  | 198 | virq = msi->msi_virqs[i]; | 
|  | 199 | if (virq != NO_IRQ) | 
|  | 200 | irq_dispose_mapping(virq); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | if (msi->bitmap.bitmap) | 
|  | 204 | msi_bitmap_free(&msi->bitmap); | 
|  | 205 | iounmap(msi->msi_regs); | 
|  | 206 | of_node_put(msi->msi_dev); | 
|  | 207 | kfree(msi); | 
|  | 208 |  | 
|  | 209 | return 0; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | static int __devinit ppc4xx_msi_probe(struct platform_device *dev) | 
|  | 213 | { | 
|  | 214 | struct ppc4xx_msi *msi; | 
|  | 215 | struct resource res; | 
|  | 216 | int err = 0; | 
|  | 217 |  | 
|  | 218 | msi = &ppc4xx_msi;/*keep the msi data for further use*/ | 
|  | 219 |  | 
|  | 220 | dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n"); | 
|  | 221 |  | 
|  | 222 | msi = kzalloc(sizeof(struct ppc4xx_msi), GFP_KERNEL); | 
|  | 223 | if (!msi) { | 
|  | 224 | dev_err(&dev->dev, "No memory for MSI structure\n"); | 
|  | 225 | return -ENOMEM; | 
|  | 226 | } | 
|  | 227 | dev->dev.platform_data = msi; | 
|  | 228 |  | 
|  | 229 | /* Get MSI ranges */ | 
|  | 230 | err = of_address_to_resource(dev->dev.of_node, 0, &res); | 
|  | 231 | if (err) { | 
|  | 232 | dev_err(&dev->dev, "%s resource error!\n", | 
|  | 233 | dev->dev.of_node->full_name); | 
|  | 234 | goto error_out; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | if (ppc4xx_setup_pcieh_hw(dev, res, msi)) | 
|  | 238 | goto error_out; | 
|  | 239 |  | 
|  | 240 | err = ppc4xx_msi_init_allocator(dev, msi); | 
|  | 241 | if (err) { | 
|  | 242 | dev_err(&dev->dev, "Error allocating MSI bitmap\n"); | 
|  | 243 | goto error_out; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | ppc_md.setup_msi_irqs = ppc4xx_setup_msi_irqs; | 
|  | 247 | ppc_md.teardown_msi_irqs = ppc4xx_teardown_msi_irqs; | 
|  | 248 | ppc_md.msi_check_device = ppc4xx_msi_check_device; | 
|  | 249 | return err; | 
|  | 250 |  | 
|  | 251 | error_out: | 
|  | 252 | ppc4xx_of_msi_remove(dev); | 
|  | 253 | return err; | 
|  | 254 | } | 
|  | 255 | static const struct of_device_id ppc4xx_msi_ids[] = { | 
|  | 256 | { | 
|  | 257 | .compatible = "amcc,ppc4xx-msi", | 
|  | 258 | }, | 
|  | 259 | {} | 
|  | 260 | }; | 
|  | 261 | static struct platform_driver ppc4xx_msi_driver = { | 
|  | 262 | .probe = ppc4xx_msi_probe, | 
|  | 263 | .remove = ppc4xx_of_msi_remove, | 
|  | 264 | .driver = { | 
|  | 265 | .name = "ppc4xx-msi", | 
|  | 266 | .owner = THIS_MODULE, | 
|  | 267 | .of_match_table = ppc4xx_msi_ids, | 
|  | 268 | }, | 
|  | 269 |  | 
|  | 270 | }; | 
|  | 271 |  | 
|  | 272 | static __init int ppc4xx_msi_init(void) | 
|  | 273 | { | 
|  | 274 | return platform_driver_register(&ppc4xx_msi_driver); | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | subsys_initcall(ppc4xx_msi_init); |