xref: /openbmc/linux/arch/mips/pci/pci-rt2880.c (revision 187c26ddf0b201aeb7e343be19a3da72eef19db4)
1*187c26ddSJohn Crispin /*
2*187c26ddSJohn Crispin  *  Ralink RT288x SoC PCI register definitions
3*187c26ddSJohn Crispin  *
4*187c26ddSJohn Crispin  *  Copyright (C) 2009 John Crispin <blogic@openwrt.org>
5*187c26ddSJohn Crispin  *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
6*187c26ddSJohn Crispin  *
7*187c26ddSJohn Crispin  *  Parts of this file are based on Ralink's 2.6.21 BSP
8*187c26ddSJohn Crispin  *
9*187c26ddSJohn Crispin  *  This program is free software; you can redistribute it and/or modify it
10*187c26ddSJohn Crispin  *  under the terms of the GNU General Public License version 2 as published
11*187c26ddSJohn Crispin  *  by the Free Software Foundation.
12*187c26ddSJohn Crispin  */
13*187c26ddSJohn Crispin 
14*187c26ddSJohn Crispin #include <linux/types.h>
15*187c26ddSJohn Crispin #include <linux/pci.h>
16*187c26ddSJohn Crispin #include <linux/io.h>
17*187c26ddSJohn Crispin #include <linux/init.h>
18*187c26ddSJohn Crispin #include <linux/module.h>
19*187c26ddSJohn Crispin #include <linux/of_platform.h>
20*187c26ddSJohn Crispin #include <linux/of_irq.h>
21*187c26ddSJohn Crispin #include <linux/of_pci.h>
22*187c26ddSJohn Crispin 
23*187c26ddSJohn Crispin #include <asm/mach-ralink/rt288x.h>
24*187c26ddSJohn Crispin 
25*187c26ddSJohn Crispin #define RT2880_PCI_BASE		0x00440000
26*187c26ddSJohn Crispin #define RT288X_CPU_IRQ_PCI	4
27*187c26ddSJohn Crispin 
28*187c26ddSJohn Crispin #define RT2880_PCI_MEM_BASE	0x20000000
29*187c26ddSJohn Crispin #define RT2880_PCI_MEM_SIZE	0x10000000
30*187c26ddSJohn Crispin #define RT2880_PCI_IO_BASE	0x00460000
31*187c26ddSJohn Crispin #define RT2880_PCI_IO_SIZE	0x00010000
32*187c26ddSJohn Crispin 
33*187c26ddSJohn Crispin #define RT2880_PCI_REG_PCICFG_ADDR	0x00
34*187c26ddSJohn Crispin #define RT2880_PCI_REG_PCIMSK_ADDR	0x0c
35*187c26ddSJohn Crispin #define RT2880_PCI_REG_BAR0SETUP_ADDR	0x10
36*187c26ddSJohn Crispin #define RT2880_PCI_REG_IMBASEBAR0_ADDR	0x18
37*187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_ADDR	0x20
38*187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_DATA	0x24
39*187c26ddSJohn Crispin #define RT2880_PCI_REG_MEMBASE		0x28
40*187c26ddSJohn Crispin #define RT2880_PCI_REG_IOBASE		0x2c
41*187c26ddSJohn Crispin #define RT2880_PCI_REG_ID		0x30
42*187c26ddSJohn Crispin #define RT2880_PCI_REG_CLASS		0x34
43*187c26ddSJohn Crispin #define RT2880_PCI_REG_SUBID		0x38
44*187c26ddSJohn Crispin #define RT2880_PCI_REG_ARBCTL		0x80
45*187c26ddSJohn Crispin 
46*187c26ddSJohn Crispin static void __iomem *rt2880_pci_base;
47*187c26ddSJohn Crispin static DEFINE_SPINLOCK(rt2880_pci_lock);
48*187c26ddSJohn Crispin 
49*187c26ddSJohn Crispin static u32 rt2880_pci_reg_read(u32 reg)
50*187c26ddSJohn Crispin {
51*187c26ddSJohn Crispin 	return readl(rt2880_pci_base + reg);
52*187c26ddSJohn Crispin }
53*187c26ddSJohn Crispin 
54*187c26ddSJohn Crispin static void rt2880_pci_reg_write(u32 val, u32 reg)
55*187c26ddSJohn Crispin {
56*187c26ddSJohn Crispin 	writel(val, rt2880_pci_base + reg);
57*187c26ddSJohn Crispin }
58*187c26ddSJohn Crispin 
59*187c26ddSJohn Crispin static inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
60*187c26ddSJohn Crispin 					 unsigned int func, unsigned int where)
61*187c26ddSJohn Crispin {
62*187c26ddSJohn Crispin 	return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
63*187c26ddSJohn Crispin 		0x80000000);
64*187c26ddSJohn Crispin }
65*187c26ddSJohn Crispin 
66*187c26ddSJohn Crispin static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
67*187c26ddSJohn Crispin 				  int where, int size, u32 *val)
68*187c26ddSJohn Crispin {
69*187c26ddSJohn Crispin 	unsigned long flags;
70*187c26ddSJohn Crispin 	u32 address;
71*187c26ddSJohn Crispin 	u32 data;
72*187c26ddSJohn Crispin 
73*187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
74*187c26ddSJohn Crispin 					 PCI_FUNC(devfn), where);
75*187c26ddSJohn Crispin 
76*187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
77*187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
78*187c26ddSJohn Crispin 	data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
79*187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
80*187c26ddSJohn Crispin 
81*187c26ddSJohn Crispin 	switch (size) {
82*187c26ddSJohn Crispin 	case 1:
83*187c26ddSJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xff;
84*187c26ddSJohn Crispin 		break;
85*187c26ddSJohn Crispin 	case 2:
86*187c26ddSJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xffff;
87*187c26ddSJohn Crispin 		break;
88*187c26ddSJohn Crispin 	case 4:
89*187c26ddSJohn Crispin 		*val = data;
90*187c26ddSJohn Crispin 		break;
91*187c26ddSJohn Crispin 	}
92*187c26ddSJohn Crispin 
93*187c26ddSJohn Crispin 	return PCIBIOS_SUCCESSFUL;
94*187c26ddSJohn Crispin }
95*187c26ddSJohn Crispin 
96*187c26ddSJohn Crispin static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
97*187c26ddSJohn Crispin 				   int where, int size, u32 val)
98*187c26ddSJohn Crispin {
99*187c26ddSJohn Crispin 	unsigned long flags;
100*187c26ddSJohn Crispin 	u32 address;
101*187c26ddSJohn Crispin 	u32 data;
102*187c26ddSJohn Crispin 
103*187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
104*187c26ddSJohn Crispin 					 PCI_FUNC(devfn), where);
105*187c26ddSJohn Crispin 
106*187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
107*187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
108*187c26ddSJohn Crispin 	data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
109*187c26ddSJohn Crispin 
110*187c26ddSJohn Crispin 	switch (size) {
111*187c26ddSJohn Crispin 	case 1:
112*187c26ddSJohn Crispin 		data = (data & ~(0xff << ((where & 3) << 3))) |
113*187c26ddSJohn Crispin 		       (val << ((where & 3) << 3));
114*187c26ddSJohn Crispin 		break;
115*187c26ddSJohn Crispin 	case 2:
116*187c26ddSJohn Crispin 		data = (data & ~(0xffff << ((where & 3) << 3))) |
117*187c26ddSJohn Crispin 		       (val << ((where & 3) << 3));
118*187c26ddSJohn Crispin 		break;
119*187c26ddSJohn Crispin 	case 4:
120*187c26ddSJohn Crispin 		data = val;
121*187c26ddSJohn Crispin 		break;
122*187c26ddSJohn Crispin 	}
123*187c26ddSJohn Crispin 
124*187c26ddSJohn Crispin 	rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA);
125*187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
126*187c26ddSJohn Crispin 
127*187c26ddSJohn Crispin 	return PCIBIOS_SUCCESSFUL;
128*187c26ddSJohn Crispin }
129*187c26ddSJohn Crispin 
130*187c26ddSJohn Crispin static struct pci_ops rt2880_pci_ops = {
131*187c26ddSJohn Crispin 	.read	= rt2880_pci_config_read,
132*187c26ddSJohn Crispin 	.write	= rt2880_pci_config_write,
133*187c26ddSJohn Crispin };
134*187c26ddSJohn Crispin 
135*187c26ddSJohn Crispin static struct resource rt2880_pci_mem_resource = {
136*187c26ddSJohn Crispin 	.name	= "PCI MEM space",
137*187c26ddSJohn Crispin 	.start	= RT2880_PCI_MEM_BASE,
138*187c26ddSJohn Crispin 	.end	= RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1,
139*187c26ddSJohn Crispin 	.flags	= IORESOURCE_MEM,
140*187c26ddSJohn Crispin };
141*187c26ddSJohn Crispin 
142*187c26ddSJohn Crispin static struct resource rt2880_pci_io_resource = {
143*187c26ddSJohn Crispin 	.name	= "PCI IO space",
144*187c26ddSJohn Crispin 	.start	= RT2880_PCI_IO_BASE,
145*187c26ddSJohn Crispin 	.end	= RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1,
146*187c26ddSJohn Crispin 	.flags	= IORESOURCE_IO,
147*187c26ddSJohn Crispin };
148*187c26ddSJohn Crispin 
149*187c26ddSJohn Crispin static struct pci_controller rt2880_pci_controller = {
150*187c26ddSJohn Crispin 	.pci_ops	= &rt2880_pci_ops,
151*187c26ddSJohn Crispin 	.mem_resource	= &rt2880_pci_mem_resource,
152*187c26ddSJohn Crispin 	.io_resource	= &rt2880_pci_io_resource,
153*187c26ddSJohn Crispin };
154*187c26ddSJohn Crispin 
155*187c26ddSJohn Crispin static inline u32 rt2880_pci_read_u32(unsigned long reg)
156*187c26ddSJohn Crispin {
157*187c26ddSJohn Crispin 	unsigned long flags;
158*187c26ddSJohn Crispin 	u32 address;
159*187c26ddSJohn Crispin 	u32 ret;
160*187c26ddSJohn Crispin 
161*187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
162*187c26ddSJohn Crispin 
163*187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
164*187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
165*187c26ddSJohn Crispin 	ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
166*187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
167*187c26ddSJohn Crispin 
168*187c26ddSJohn Crispin 	return ret;
169*187c26ddSJohn Crispin }
170*187c26ddSJohn Crispin 
171*187c26ddSJohn Crispin static inline void rt2880_pci_write_u32(unsigned long reg, u32 val)
172*187c26ddSJohn Crispin {
173*187c26ddSJohn Crispin 	unsigned long flags;
174*187c26ddSJohn Crispin 	u32 address;
175*187c26ddSJohn Crispin 
176*187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
177*187c26ddSJohn Crispin 
178*187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
179*187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
180*187c26ddSJohn Crispin 	rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
181*187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
182*187c26ddSJohn Crispin }
183*187c26ddSJohn Crispin 
184*187c26ddSJohn Crispin int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
185*187c26ddSJohn Crispin {
186*187c26ddSJohn Crispin 	u16 cmd;
187*187c26ddSJohn Crispin 	int irq = -1;
188*187c26ddSJohn Crispin 
189*187c26ddSJohn Crispin 	if (dev->bus->number != 0)
190*187c26ddSJohn Crispin 		return irq;
191*187c26ddSJohn Crispin 
192*187c26ddSJohn Crispin 	switch (PCI_SLOT(dev->devfn)) {
193*187c26ddSJohn Crispin 	case 0x00:
194*187c26ddSJohn Crispin 		rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
195*187c26ddSJohn Crispin 		(void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
196*187c26ddSJohn Crispin 		break;
197*187c26ddSJohn Crispin 	case 0x11:
198*187c26ddSJohn Crispin 		irq = RT288X_CPU_IRQ_PCI;
199*187c26ddSJohn Crispin 		break;
200*187c26ddSJohn Crispin 	default:
201*187c26ddSJohn Crispin 		pr_err("%s:%s[%d] trying to alloc unknown pci irq\n",
202*187c26ddSJohn Crispin 		       __FILE__, __func__, __LINE__);
203*187c26ddSJohn Crispin 		BUG();
204*187c26ddSJohn Crispin 		break;
205*187c26ddSJohn Crispin 	}
206*187c26ddSJohn Crispin 
207*187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev,
208*187c26ddSJohn Crispin 		PCI_CACHE_LINE_SIZE, 0x14);
209*187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev, PCI_LATENCY_TIMER, 0xFF);
210*187c26ddSJohn Crispin 	pci_read_config_word((struct pci_dev *) dev, PCI_COMMAND, &cmd);
211*187c26ddSJohn Crispin 	cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
212*187c26ddSJohn Crispin 		PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK |
213*187c26ddSJohn Crispin 		PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY;
214*187c26ddSJohn Crispin 	pci_write_config_word((struct pci_dev *) dev, PCI_COMMAND, cmd);
215*187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev, PCI_INTERRUPT_LINE,
216*187c26ddSJohn Crispin 			      dev->irq);
217*187c26ddSJohn Crispin 	return irq;
218*187c26ddSJohn Crispin }
219*187c26ddSJohn Crispin 
220*187c26ddSJohn Crispin static int rt288x_pci_probe(struct platform_device *pdev)
221*187c26ddSJohn Crispin {
222*187c26ddSJohn Crispin 	void __iomem *io_map_base;
223*187c26ddSJohn Crispin 	int i;
224*187c26ddSJohn Crispin 
225*187c26ddSJohn Crispin 	rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
226*187c26ddSJohn Crispin 
227*187c26ddSJohn Crispin 	io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE);
228*187c26ddSJohn Crispin 	rt2880_pci_controller.io_map_base = (unsigned long) io_map_base;
229*187c26ddSJohn Crispin 	set_io_port_base((unsigned long) io_map_base);
230*187c26ddSJohn Crispin 
231*187c26ddSJohn Crispin 	ioport_resource.start = RT2880_PCI_IO_BASE;
232*187c26ddSJohn Crispin 	ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1;
233*187c26ddSJohn Crispin 
234*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
235*187c26ddSJohn Crispin 	for (i = 0; i < 0xfffff; i++)
236*187c26ddSJohn Crispin 		;
237*187c26ddSJohn Crispin 
238*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL);
239*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR);
240*187c26ddSJohn Crispin 	rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE);
241*187c26ddSJohn Crispin 	rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE);
242*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR);
243*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID);
244*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS);
245*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID);
246*187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR);
247*187c26ddSJohn Crispin 
248*187c26ddSJohn Crispin 	rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
249*187c26ddSJohn Crispin 	(void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
250*187c26ddSJohn Crispin 
251*187c26ddSJohn Crispin 	register_pci_controller(&rt2880_pci_controller);
252*187c26ddSJohn Crispin 	return 0;
253*187c26ddSJohn Crispin }
254*187c26ddSJohn Crispin 
255*187c26ddSJohn Crispin int pcibios_plat_dev_init(struct pci_dev *dev)
256*187c26ddSJohn Crispin {
257*187c26ddSJohn Crispin 	return 0;
258*187c26ddSJohn Crispin }
259*187c26ddSJohn Crispin 
260*187c26ddSJohn Crispin static const struct of_device_id rt288x_pci_match[] = {
261*187c26ddSJohn Crispin 	{ .compatible = "ralink,rt288x-pci" },
262*187c26ddSJohn Crispin 	{},
263*187c26ddSJohn Crispin };
264*187c26ddSJohn Crispin MODULE_DEVICE_TABLE(of, rt288x_pci_match);
265*187c26ddSJohn Crispin 
266*187c26ddSJohn Crispin static struct platform_driver rt288x_pci_driver = {
267*187c26ddSJohn Crispin 	.probe = rt288x_pci_probe,
268*187c26ddSJohn Crispin 	.driver = {
269*187c26ddSJohn Crispin 		.name = "rt288x-pci",
270*187c26ddSJohn Crispin 		.owner = THIS_MODULE,
271*187c26ddSJohn Crispin 		.of_match_table = rt288x_pci_match,
272*187c26ddSJohn Crispin 	},
273*187c26ddSJohn Crispin };
274*187c26ddSJohn Crispin 
275*187c26ddSJohn Crispin int __init pcibios_init(void)
276*187c26ddSJohn Crispin {
277*187c26ddSJohn Crispin 	int ret = platform_driver_register(&rt288x_pci_driver);
278*187c26ddSJohn Crispin 
279*187c26ddSJohn Crispin 	if (ret)
280*187c26ddSJohn Crispin 		pr_info("rt288x-pci: Error registering platform driver!");
281*187c26ddSJohn Crispin 
282*187c26ddSJohn Crispin 	return ret;
283*187c26ddSJohn Crispin }
284*187c26ddSJohn Crispin 
285*187c26ddSJohn Crispin arch_initcall(pcibios_init);
286