blob: 6eb21f2ea5857295d33bc5f9e8da87b7f0d9071f [file] [log] [blame]
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +00001/*
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>
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000025#include <linux/pci.h>
26#include <linux/msi.h>
27#include <linux/of_platform.h>
28#include <linux/interrupt.h>
Paul Gortmaker93087942011-07-29 16:19:31 +100029#include <linux/export.h>
Mai La247540b2012-03-18 17:59:08 +000030#include <linux/kernel.h>
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000031#include <asm/prom.h>
32#include <asm/hw_irq.h>
33#include <asm/ppc-pci.h>
Mai La247540b2012-03-18 17:59:08 +000034#include <asm/dcr.h>
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000035#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
Mai La247540b2012-03-18 17:59:08 +000046
47static int msi_irqs;
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000048
49struct ppc4xx_msi {
50 u32 msi_addr_lo;
51 u32 msi_addr_hi;
52 void __iomem *msi_regs;
Mai La247540b2012-03-18 17:59:08 +000053 int *msi_virqs;
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000054 struct msi_bitmap bitmap;
55 struct device_node *msi_dev;
56};
57
58static struct ppc4xx_msi ppc4xx_msi;
59
60static int ppc4xx_msi_init_allocator(struct platform_device *dev,
61 struct ppc4xx_msi *msi_data)
62{
63 int err;
64
Mai La247540b2012-03-18 17:59:08 +000065 err = msi_bitmap_alloc(&msi_data->bitmap, msi_irqs,
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000066 dev->dev.of_node);
67 if (err)
68 return err;
69
70 err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
71 if (err < 0) {
72 msi_bitmap_free(&msi_data->bitmap);
73 return err;
74 }
75
76 return 0;
77}
78
79static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
80{
81 int int_no = -ENOMEM;
82 unsigned int virq;
83 struct msi_msg msg;
84 struct msi_desc *entry;
85 struct ppc4xx_msi *msi_data = &ppc4xx_msi;
86
Alexander Gordeev6b2fd7ef2014-09-07 20:57:53 +020087 dev_dbg(&dev->dev, "PCIE-MSI:%s called. vec %x type %d\n",
88 __func__, nvec, type);
89 if (type == PCI_CAP_ID_MSIX)
90 pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
91
92 msi_data->msi_virqs = kmalloc((msi_irqs) * sizeof(int), GFP_KERNEL);
Mai La247540b2012-03-18 17:59:08 +000093 if (!msi_data->msi_virqs)
94 return -ENOMEM;
95
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +000096 list_for_each_entry(entry, &dev->msi_list, list) {
97 int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
98 if (int_no >= 0)
99 break;
100 if (int_no < 0) {
101 pr_debug("%s: fail allocating msi interrupt\n",
102 __func__);
103 }
104 virq = irq_of_parse_and_map(msi_data->msi_dev, int_no);
105 if (virq == NO_IRQ) {
106 dev_err(&dev->dev, "%s: fail mapping irq\n", __func__);
107 msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1);
108 return -ENOSPC;
109 }
110 dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq);
111
112 /* Setup msi address space */
113 msg.address_hi = msi_data->msi_addr_hi;
114 msg.address_lo = msi_data->msi_addr_lo;
115
116 irq_set_msi_desc(virq, entry);
117 msg.data = int_no;
Jiang Liu83a18912014-11-09 23:10:34 +0800118 pci_write_msi_msg(virq, &msg);
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000119 }
120 return 0;
121}
122
123void ppc4xx_teardown_msi_irqs(struct pci_dev *dev)
124{
125 struct msi_desc *entry;
126 struct ppc4xx_msi *msi_data = &ppc4xx_msi;
127
128 dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n");
129
130 list_for_each_entry(entry, &dev->msi_list, list) {
131 if (entry->irq == NO_IRQ)
132 continue;
133 irq_set_msi_desc(entry->irq, NULL);
134 msi_bitmap_free_hwirqs(&msi_data->bitmap,
135 virq_to_hw(entry->irq), 1);
136 irq_dispose_mapping(entry->irq);
137 }
138}
139
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000140static 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
Mai La247540b2012-03-18 17:59:08 +0000153 mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */
154 mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000155
156 msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi");
Mai La247540b2012-03-18 17:59:08 +0000157 if (!msi->msi_dev)
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000158 return -ENODEV;
159
160 msi->msi_regs = of_iomap(msi->msi_dev, 0);
161 if (!msi->msi_regs) {
162 dev_err(&dev->dev, "of_iomap problem failed\n");
163 return -ENOMEM;
164 }
165 dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n",
166 (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs));
167
168 msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL);
Mai La247540b2012-03-18 17:59:08 +0000169 if (!msi_virt)
170 return -ENOMEM;
Josh Boyerdce4c922012-05-03 20:13:13 -0400171 msi->msi_addr_hi = upper_32_bits(msi_phys);
172 msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff);
Mai La247540b2012-03-18 17:59:08 +0000173 dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n",
174 msi->msi_addr_hi, msi->msi_addr_lo);
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000175
176 /* Progam the Interrupt handler Termination addr registers */
177 out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi);
178 out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo);
179
180 msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL);
181 if (!msi_data)
182 return -1;
183 msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL);
184 if (!msi_mask)
185 return -1;
186 /* Program MSI Expected data and Mask bits */
187 out_be32(msi->msi_regs + PEIH_MSIED, *msi_data);
188 out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask);
189
Mai La247540b2012-03-18 17:59:08 +0000190 dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys);
191
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000192 return 0;
193}
194
195static int ppc4xx_of_msi_remove(struct platform_device *dev)
196{
197 struct ppc4xx_msi *msi = dev->dev.platform_data;
198 int i;
199 int virq;
200
Mai La247540b2012-03-18 17:59:08 +0000201 for (i = 0; i < msi_irqs; i++) {
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000202 virq = msi->msi_virqs[i];
203 if (virq != NO_IRQ)
204 irq_dispose_mapping(virq);
205 }
206
207 if (msi->bitmap.bitmap)
208 msi_bitmap_free(&msi->bitmap);
209 iounmap(msi->msi_regs);
210 of_node_put(msi->msi_dev);
211 kfree(msi);
212
213 return 0;
214}
215
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800216static int ppc4xx_msi_probe(struct platform_device *dev)
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000217{
218 struct ppc4xx_msi *msi;
219 struct resource res;
220 int err = 0;
Daniel Axtensb7eba2f2015-04-14 14:27:59 +1000221 struct pci_controller *phb;
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000222
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000223 dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n");
224
225 msi = kzalloc(sizeof(struct ppc4xx_msi), GFP_KERNEL);
226 if (!msi) {
227 dev_err(&dev->dev, "No memory for MSI structure\n");
228 return -ENOMEM;
229 }
230 dev->dev.platform_data = msi;
231
232 /* Get MSI ranges */
233 err = of_address_to_resource(dev->dev.of_node, 0, &res);
234 if (err) {
235 dev_err(&dev->dev, "%s resource error!\n",
236 dev->dev.of_node->full_name);
237 goto error_out;
238 }
239
Mai La247540b2012-03-18 17:59:08 +0000240 msi_irqs = of_irq_count(dev->dev.of_node);
241 if (!msi_irqs)
242 return -ENODEV;
243
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000244 if (ppc4xx_setup_pcieh_hw(dev, res, msi))
245 goto error_out;
246
247 err = ppc4xx_msi_init_allocator(dev, msi);
248 if (err) {
249 dev_err(&dev->dev, "Error allocating MSI bitmap\n");
250 goto error_out;
251 }
Mai La247540b2012-03-18 17:59:08 +0000252 ppc4xx_msi = *msi;
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000253
Daniel Axtensb7eba2f2015-04-14 14:27:59 +1000254 list_for_each_entry(phb, &hose_list, list_node) {
255 phb->controller_ops.setup_msi_irqs = ppc4xx_setup_msi_irqs;
256 phb->controller_ops.teardown_msi_irqs = ppc4xx_teardown_msi_irqs;
257 }
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000258 return err;
259
260error_out:
261 ppc4xx_of_msi_remove(dev);
262 return err;
263}
264static const struct of_device_id ppc4xx_msi_ids[] = {
265 {
266 .compatible = "amcc,ppc4xx-msi",
267 },
268 {}
269};
270static struct platform_driver ppc4xx_msi_driver = {
271 .probe = ppc4xx_msi_probe,
272 .remove = ppc4xx_of_msi_remove,
273 .driver = {
274 .name = "ppc4xx-msi",
Rupjyoti Sarmah3fb79332011-03-29 23:10:24 +0000275 .of_match_table = ppc4xx_msi_ids,
276 },
277
278};
279
280static __init int ppc4xx_msi_init(void)
281{
282 return platform_driver_register(&ppc4xx_msi_driver);
283}
284
285subsys_initcall(ppc4xx_msi_init);