2 * Adding PCI-E MSI support for PPC4XX SoCs.
4 * Copyright (c) 2010, Applied Micro Circuits Corporation
5 * Authors: Tirumala R Marri <tmarri@apm.com>
6 * Feng Kan <fkan@apm.com>
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.
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.
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,
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>
31 #include <asm/hw_irq.h>
32 #include <asm/ppc-pci.h>
34 #include <asm/dcr-regs.h>
35 #include <asm/msi_bitmap.h>
37 #define PEIH_TERMADH 0x00
38 #define PEIH_TERMADL 0x08
39 #define PEIH_MSIED 0x10
40 #define PEIH_MSIMK 0x18
41 #define PEIH_MSIASS 0x20
42 #define PEIH_FLUSH0 0x30
43 #define PEIH_FLUSH1 0x38
44 #define PEIH_CNTRST 0x48
50 void __iomem
*msi_regs
;
51 int msi_virqs
[NR_MSI_IRQS
];
52 struct msi_bitmap bitmap
;
53 struct device_node
*msi_dev
;
56 static struct ppc4xx_msi ppc4xx_msi
;
58 static int ppc4xx_msi_init_allocator(struct platform_device
*dev
,
59 struct ppc4xx_msi
*msi_data
)
63 err
= msi_bitmap_alloc(&msi_data
->bitmap
, NR_MSI_IRQS
,
68 err
= msi_bitmap_reserve_dt_hwirqs(&msi_data
->bitmap
);
70 msi_bitmap_free(&msi_data
->bitmap
);
77 static int ppc4xx_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
82 struct msi_desc
*entry
;
83 struct ppc4xx_msi
*msi_data
= &ppc4xx_msi
;
85 list_for_each_entry(entry
, &dev
->msi_list
, list
) {
86 int_no
= msi_bitmap_alloc_hwirqs(&msi_data
->bitmap
, 1);
90 pr_debug("%s: fail allocating msi interrupt\n",
93 virq
= irq_of_parse_and_map(msi_data
->msi_dev
, int_no
);
95 dev_err(&dev
->dev
, "%s: fail mapping irq\n", __func__
);
96 msi_bitmap_free_hwirqs(&msi_data
->bitmap
, int_no
, 1);
99 dev_dbg(&dev
->dev
, "%s: virq = %d\n", __func__
, virq
);
101 /* Setup msi address space */
102 msg
.address_hi
= msi_data
->msi_addr_hi
;
103 msg
.address_lo
= msi_data
->msi_addr_lo
;
105 irq_set_msi_desc(virq
, entry
);
107 write_msi_msg(virq
, &msg
);
112 void ppc4xx_teardown_msi_irqs(struct pci_dev
*dev
)
114 struct msi_desc
*entry
;
115 struct ppc4xx_msi
*msi_data
= &ppc4xx_msi
;
117 dev_dbg(&dev
->dev
, "PCIE-MSI: tearing down msi irqs\n");
119 list_for_each_entry(entry
, &dev
->msi_list
, list
) {
120 if (entry
->irq
== NO_IRQ
)
122 irq_set_msi_desc(entry
->irq
, NULL
);
123 msi_bitmap_free_hwirqs(&msi_data
->bitmap
,
124 virq_to_hw(entry
->irq
), 1);
125 irq_dispose_mapping(entry
->irq
);
129 static int ppc4xx_msi_check_device(struct pci_dev
*pdev
, int nvec
, int type
)
131 dev_dbg(&pdev
->dev
, "PCIE-MSI:%s called. vec %x type %d\n",
132 __func__
, nvec
, type
);
133 if (type
== PCI_CAP_ID_MSIX
)
134 pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
139 static int ppc4xx_setup_pcieh_hw(struct platform_device
*dev
,
140 struct resource res
, struct ppc4xx_msi
*msi
)
148 sdr_addr
= of_get_property(dev
->dev
.of_node
, "sdr-base", NULL
);
152 SDR0_WRITE(sdr_addr
, (u64
)res
.start
>> 32); /*HIGH addr */
153 SDR0_WRITE(sdr_addr
+ 1, res
.start
& 0xFFFFFFFF); /* Low addr */
156 msi
->msi_dev
= of_find_node_by_name(NULL
, "ppc4xx-msi");
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");
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
));
168 msi_virt
= dma_alloc_coherent(&dev
->dev
, 64, &msi_phys
, GFP_KERNEL
);
169 msi
->msi_addr_hi
= 0x0;
170 msi
->msi_addr_lo
= (u32
) msi_phys
;
171 dev_dbg(&dev
->dev
, "PCIE-MSI: msi address 0x%x\n", msi
->msi_addr_lo
);
173 /* Progam the Interrupt handler Termination addr registers */
174 out_be32(msi
->msi_regs
+ PEIH_TERMADH
, msi
->msi_addr_hi
);
175 out_be32(msi
->msi_regs
+ PEIH_TERMADL
, msi
->msi_addr_lo
);
177 msi_data
= of_get_property(dev
->dev
.of_node
, "msi-data", NULL
);
180 msi_mask
= of_get_property(dev
->dev
.of_node
, "msi-mask", NULL
);
183 /* Program MSI Expected data and Mask bits */
184 out_be32(msi
->msi_regs
+ PEIH_MSIED
, *msi_data
);
185 out_be32(msi
->msi_regs
+ PEIH_MSIMK
, *msi_mask
);
190 static int ppc4xx_of_msi_remove(struct platform_device
*dev
)
192 struct ppc4xx_msi
*msi
= dev
->dev
.platform_data
;
196 for (i
= 0; i
< NR_MSI_IRQS
; i
++) {
197 virq
= msi
->msi_virqs
[i
];
199 irq_dispose_mapping(virq
);
202 if (msi
->bitmap
.bitmap
)
203 msi_bitmap_free(&msi
->bitmap
);
204 iounmap(msi
->msi_regs
);
205 of_node_put(msi
->msi_dev
);
211 static int __devinit
ppc4xx_msi_probe(struct platform_device
*dev
)
213 struct ppc4xx_msi
*msi
;
217 msi
= &ppc4xx_msi
;/*keep the msi data for further use*/
219 dev_dbg(&dev
->dev
, "PCIE-MSI: Setting up MSI support...\n");
221 msi
= kzalloc(sizeof(struct ppc4xx_msi
), GFP_KERNEL
);
223 dev_err(&dev
->dev
, "No memory for MSI structure\n");
226 dev
->dev
.platform_data
= msi
;
229 err
= of_address_to_resource(dev
->dev
.of_node
, 0, &res
);
231 dev_err(&dev
->dev
, "%s resource error!\n",
232 dev
->dev
.of_node
->full_name
);
236 if (ppc4xx_setup_pcieh_hw(dev
, res
, msi
))
239 err
= ppc4xx_msi_init_allocator(dev
, msi
);
241 dev_err(&dev
->dev
, "Error allocating MSI bitmap\n");
245 ppc_md
.setup_msi_irqs
= ppc4xx_setup_msi_irqs
;
246 ppc_md
.teardown_msi_irqs
= ppc4xx_teardown_msi_irqs
;
247 ppc_md
.msi_check_device
= ppc4xx_msi_check_device
;
251 ppc4xx_of_msi_remove(dev
);
254 static const struct of_device_id ppc4xx_msi_ids
[] = {
256 .compatible
= "amcc,ppc4xx-msi",
260 static struct platform_driver ppc4xx_msi_driver
= {
261 .probe
= ppc4xx_msi_probe
,
262 .remove
= ppc4xx_of_msi_remove
,
264 .name
= "ppc4xx-msi",
265 .owner
= THIS_MODULE
,
266 .of_match_table
= ppc4xx_msi_ids
,
271 static __init
int ppc4xx_msi_init(void)
273 return platform_driver_register(&ppc4xx_msi_driver
);
276 subsys_initcall(ppc4xx_msi_init
);