xref: /openbmc/linux/arch/mips/pci/pci-rt2880.c (revision 8eba3651f1dad49c83bb7f8d672301dac4c6add6)
1187c26ddSJohn Crispin /*
2187c26ddSJohn Crispin  *  Ralink RT288x SoC PCI register definitions
3187c26ddSJohn Crispin  *
497b92108SJohn Crispin  *  Copyright (C) 2009 John Crispin <john@phrozen.org>
5187c26ddSJohn Crispin  *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
6187c26ddSJohn Crispin  *
7187c26ddSJohn Crispin  *  Parts of this file are based on Ralink's 2.6.21 BSP
8187c26ddSJohn Crispin  *
9187c26ddSJohn Crispin  *  This program is free software; you can redistribute it and/or modify it
10187c26ddSJohn Crispin  *  under the terms of the GNU General Public License version 2 as published
11187c26ddSJohn Crispin  *  by the Free Software Foundation.
12187c26ddSJohn Crispin  */
13187c26ddSJohn Crispin 
14c861519fSRalf Baechle #include <linux/delay.h>
15187c26ddSJohn Crispin #include <linux/types.h>
16187c26ddSJohn Crispin #include <linux/pci.h>
17187c26ddSJohn Crispin #include <linux/io.h>
18187c26ddSJohn Crispin #include <linux/init.h>
19187c26ddSJohn Crispin #include <linux/of_platform.h>
20187c26ddSJohn Crispin #include <linux/of_irq.h>
21187c26ddSJohn Crispin #include <linux/of_pci.h>
22187c26ddSJohn Crispin 
23187c26ddSJohn Crispin #include <asm/mach-ralink/rt288x.h>
24187c26ddSJohn Crispin 
25187c26ddSJohn Crispin #define RT2880_PCI_BASE		0x00440000
26187c26ddSJohn Crispin #define RT288X_CPU_IRQ_PCI	4
27187c26ddSJohn Crispin 
28187c26ddSJohn Crispin #define RT2880_PCI_MEM_BASE	0x20000000
29187c26ddSJohn Crispin #define RT2880_PCI_MEM_SIZE	0x10000000
30187c26ddSJohn Crispin #define RT2880_PCI_IO_BASE	0x00460000
31187c26ddSJohn Crispin #define RT2880_PCI_IO_SIZE	0x00010000
32187c26ddSJohn Crispin 
33187c26ddSJohn Crispin #define RT2880_PCI_REG_PCICFG_ADDR	0x00
34187c26ddSJohn Crispin #define RT2880_PCI_REG_PCIMSK_ADDR	0x0c
35187c26ddSJohn Crispin #define RT2880_PCI_REG_BAR0SETUP_ADDR	0x10
36187c26ddSJohn Crispin #define RT2880_PCI_REG_IMBASEBAR0_ADDR	0x18
37187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_ADDR	0x20
38187c26ddSJohn Crispin #define RT2880_PCI_REG_CONFIG_DATA	0x24
39187c26ddSJohn Crispin #define RT2880_PCI_REG_MEMBASE		0x28
40187c26ddSJohn Crispin #define RT2880_PCI_REG_IOBASE		0x2c
41187c26ddSJohn Crispin #define RT2880_PCI_REG_ID		0x30
42187c26ddSJohn Crispin #define RT2880_PCI_REG_CLASS		0x34
43187c26ddSJohn Crispin #define RT2880_PCI_REG_SUBID		0x38
44187c26ddSJohn Crispin #define RT2880_PCI_REG_ARBCTL		0x80
45187c26ddSJohn Crispin 
46187c26ddSJohn Crispin static void __iomem *rt2880_pci_base;
47187c26ddSJohn Crispin static DEFINE_SPINLOCK(rt2880_pci_lock);
48187c26ddSJohn Crispin 
49187c26ddSJohn Crispin static u32 rt2880_pci_reg_read(u32 reg)
50187c26ddSJohn Crispin {
51187c26ddSJohn Crispin 	return readl(rt2880_pci_base + reg);
52187c26ddSJohn Crispin }
53187c26ddSJohn Crispin 
54187c26ddSJohn Crispin static void rt2880_pci_reg_write(u32 val, u32 reg)
55187c26ddSJohn Crispin {
56187c26ddSJohn Crispin 	writel(val, rt2880_pci_base + reg);
57187c26ddSJohn Crispin }
58187c26ddSJohn Crispin 
59187c26ddSJohn Crispin static inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
60187c26ddSJohn Crispin 					 unsigned int func, unsigned int where)
61187c26ddSJohn Crispin {
62187c26ddSJohn Crispin 	return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
63187c26ddSJohn Crispin 		0x80000000);
64187c26ddSJohn Crispin }
65187c26ddSJohn Crispin 
66187c26ddSJohn Crispin static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
67187c26ddSJohn Crispin 				  int where, int size, u32 *val)
68187c26ddSJohn Crispin {
69187c26ddSJohn Crispin 	unsigned long flags;
70187c26ddSJohn Crispin 	u32 address;
71187c26ddSJohn Crispin 	u32 data;
72187c26ddSJohn Crispin 
73187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
74187c26ddSJohn Crispin 					 PCI_FUNC(devfn), where);
75187c26ddSJohn Crispin 
76187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
77187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
78187c26ddSJohn Crispin 	data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
79187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
80187c26ddSJohn Crispin 
81187c26ddSJohn Crispin 	switch (size) {
82187c26ddSJohn Crispin 	case 1:
83187c26ddSJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xff;
84187c26ddSJohn Crispin 		break;
85187c26ddSJohn Crispin 	case 2:
86187c26ddSJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xffff;
87187c26ddSJohn Crispin 		break;
88187c26ddSJohn Crispin 	case 4:
89187c26ddSJohn Crispin 		*val = data;
90187c26ddSJohn Crispin 		break;
91187c26ddSJohn Crispin 	}
92187c26ddSJohn Crispin 
93187c26ddSJohn Crispin 	return PCIBIOS_SUCCESSFUL;
94187c26ddSJohn Crispin }
95187c26ddSJohn Crispin 
96187c26ddSJohn Crispin static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
97187c26ddSJohn Crispin 				   int where, int size, u32 val)
98187c26ddSJohn Crispin {
99187c26ddSJohn Crispin 	unsigned long flags;
100187c26ddSJohn Crispin 	u32 address;
101187c26ddSJohn Crispin 	u32 data;
102187c26ddSJohn Crispin 
103187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
104187c26ddSJohn Crispin 					 PCI_FUNC(devfn), where);
105187c26ddSJohn Crispin 
106187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
107187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
108187c26ddSJohn Crispin 	data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
109187c26ddSJohn Crispin 
110187c26ddSJohn Crispin 	switch (size) {
111187c26ddSJohn Crispin 	case 1:
112187c26ddSJohn Crispin 		data = (data & ~(0xff << ((where & 3) << 3))) |
113187c26ddSJohn Crispin 		       (val << ((where & 3) << 3));
114187c26ddSJohn Crispin 		break;
115187c26ddSJohn Crispin 	case 2:
116187c26ddSJohn Crispin 		data = (data & ~(0xffff << ((where & 3) << 3))) |
117187c26ddSJohn Crispin 		       (val << ((where & 3) << 3));
118187c26ddSJohn Crispin 		break;
119187c26ddSJohn Crispin 	case 4:
120187c26ddSJohn Crispin 		data = val;
121187c26ddSJohn Crispin 		break;
122187c26ddSJohn Crispin 	}
123187c26ddSJohn Crispin 
124187c26ddSJohn Crispin 	rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA);
125187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
126187c26ddSJohn Crispin 
127187c26ddSJohn Crispin 	return PCIBIOS_SUCCESSFUL;
128187c26ddSJohn Crispin }
129187c26ddSJohn Crispin 
130187c26ddSJohn Crispin static struct pci_ops rt2880_pci_ops = {
131187c26ddSJohn Crispin 	.read	= rt2880_pci_config_read,
132187c26ddSJohn Crispin 	.write	= rt2880_pci_config_write,
133187c26ddSJohn Crispin };
134187c26ddSJohn Crispin 
135187c26ddSJohn Crispin static struct resource rt2880_pci_mem_resource = {
136187c26ddSJohn Crispin 	.name	= "PCI MEM space",
137187c26ddSJohn Crispin 	.start	= RT2880_PCI_MEM_BASE,
138187c26ddSJohn Crispin 	.end	= RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1,
139187c26ddSJohn Crispin 	.flags	= IORESOURCE_MEM,
140187c26ddSJohn Crispin };
141187c26ddSJohn Crispin 
142187c26ddSJohn Crispin static struct resource rt2880_pci_io_resource = {
143187c26ddSJohn Crispin 	.name	= "PCI IO space",
144187c26ddSJohn Crispin 	.start	= RT2880_PCI_IO_BASE,
145187c26ddSJohn Crispin 	.end	= RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1,
146187c26ddSJohn Crispin 	.flags	= IORESOURCE_IO,
147187c26ddSJohn Crispin };
148187c26ddSJohn Crispin 
149187c26ddSJohn Crispin static struct pci_controller rt2880_pci_controller = {
150187c26ddSJohn Crispin 	.pci_ops	= &rt2880_pci_ops,
151187c26ddSJohn Crispin 	.mem_resource	= &rt2880_pci_mem_resource,
152187c26ddSJohn Crispin 	.io_resource	= &rt2880_pci_io_resource,
153187c26ddSJohn Crispin };
154187c26ddSJohn Crispin 
155187c26ddSJohn Crispin static inline u32 rt2880_pci_read_u32(unsigned long reg)
156187c26ddSJohn Crispin {
157187c26ddSJohn Crispin 	unsigned long flags;
158187c26ddSJohn Crispin 	u32 address;
159187c26ddSJohn Crispin 	u32 ret;
160187c26ddSJohn Crispin 
161187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
162187c26ddSJohn Crispin 
163187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
164187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
165187c26ddSJohn Crispin 	ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
166187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
167187c26ddSJohn Crispin 
168187c26ddSJohn Crispin 	return ret;
169187c26ddSJohn Crispin }
170187c26ddSJohn Crispin 
171187c26ddSJohn Crispin static inline void rt2880_pci_write_u32(unsigned long reg, u32 val)
172187c26ddSJohn Crispin {
173187c26ddSJohn Crispin 	unsigned long flags;
174187c26ddSJohn Crispin 	u32 address;
175187c26ddSJohn Crispin 
176187c26ddSJohn Crispin 	address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
177187c26ddSJohn Crispin 
178187c26ddSJohn Crispin 	spin_lock_irqsave(&rt2880_pci_lock, flags);
179187c26ddSJohn Crispin 	rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
180187c26ddSJohn Crispin 	rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
181187c26ddSJohn Crispin 	spin_unlock_irqrestore(&rt2880_pci_lock, flags);
182187c26ddSJohn Crispin }
183187c26ddSJohn Crispin 
184*8eba3651SManuel Lauss int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
185187c26ddSJohn Crispin {
186187c26ddSJohn Crispin 	u16 cmd;
187187c26ddSJohn Crispin 	int irq = -1;
188187c26ddSJohn Crispin 
189187c26ddSJohn Crispin 	if (dev->bus->number != 0)
190187c26ddSJohn Crispin 		return irq;
191187c26ddSJohn Crispin 
192187c26ddSJohn Crispin 	switch (PCI_SLOT(dev->devfn)) {
193187c26ddSJohn Crispin 	case 0x00:
194187c26ddSJohn Crispin 		rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
195187c26ddSJohn Crispin 		(void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
196187c26ddSJohn Crispin 		break;
197187c26ddSJohn Crispin 	case 0x11:
198187c26ddSJohn Crispin 		irq = RT288X_CPU_IRQ_PCI;
199187c26ddSJohn Crispin 		break;
200187c26ddSJohn Crispin 	default:
201187c26ddSJohn Crispin 		pr_err("%s:%s[%d] trying to alloc unknown pci irq\n",
202187c26ddSJohn Crispin 		       __FILE__, __func__, __LINE__);
203187c26ddSJohn Crispin 		BUG();
204187c26ddSJohn Crispin 		break;
205187c26ddSJohn Crispin 	}
206187c26ddSJohn Crispin 
207187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev,
208187c26ddSJohn Crispin 		PCI_CACHE_LINE_SIZE, 0x14);
209187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev, PCI_LATENCY_TIMER, 0xFF);
210187c26ddSJohn Crispin 	pci_read_config_word((struct pci_dev *) dev, PCI_COMMAND, &cmd);
211187c26ddSJohn Crispin 	cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
212187c26ddSJohn Crispin 		PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK |
213187c26ddSJohn Crispin 		PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY;
214187c26ddSJohn Crispin 	pci_write_config_word((struct pci_dev *) dev, PCI_COMMAND, cmd);
215187c26ddSJohn Crispin 	pci_write_config_byte((struct pci_dev *) dev, PCI_INTERRUPT_LINE,
216187c26ddSJohn Crispin 			      dev->irq);
217187c26ddSJohn Crispin 	return irq;
218187c26ddSJohn Crispin }
219187c26ddSJohn Crispin 
220187c26ddSJohn Crispin static int rt288x_pci_probe(struct platform_device *pdev)
221187c26ddSJohn Crispin {
222187c26ddSJohn Crispin 	void __iomem *io_map_base;
223187c26ddSJohn Crispin 
224187c26ddSJohn Crispin 	rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
225187c26ddSJohn Crispin 
226187c26ddSJohn Crispin 	io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE);
227187c26ddSJohn Crispin 	rt2880_pci_controller.io_map_base = (unsigned long) io_map_base;
228187c26ddSJohn Crispin 	set_io_port_base((unsigned long) io_map_base);
229187c26ddSJohn Crispin 
230187c26ddSJohn Crispin 	ioport_resource.start = RT2880_PCI_IO_BASE;
231187c26ddSJohn Crispin 	ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1;
232187c26ddSJohn Crispin 
233187c26ddSJohn Crispin 	rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
234c861519fSRalf Baechle 	udelay(1);
235187c26ddSJohn Crispin 
236187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL);
237187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR);
238187c26ddSJohn Crispin 	rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE);
239187c26ddSJohn Crispin 	rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE);
240187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR);
241187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID);
242187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS);
243187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID);
244187c26ddSJohn Crispin 	rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR);
245187c26ddSJohn Crispin 
246187c26ddSJohn Crispin 	rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
247187c26ddSJohn Crispin 	(void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
248187c26ddSJohn Crispin 
249187c26ddSJohn Crispin 	register_pci_controller(&rt2880_pci_controller);
250187c26ddSJohn Crispin 	return 0;
251187c26ddSJohn Crispin }
252187c26ddSJohn Crispin 
253187c26ddSJohn Crispin int pcibios_plat_dev_init(struct pci_dev *dev)
254187c26ddSJohn Crispin {
255187c26ddSJohn Crispin 	return 0;
256187c26ddSJohn Crispin }
257187c26ddSJohn Crispin 
258187c26ddSJohn Crispin static const struct of_device_id rt288x_pci_match[] = {
259187c26ddSJohn Crispin 	{ .compatible = "ralink,rt288x-pci" },
260187c26ddSJohn Crispin 	{},
261187c26ddSJohn Crispin };
262187c26ddSJohn Crispin 
263187c26ddSJohn Crispin static struct platform_driver rt288x_pci_driver = {
264187c26ddSJohn Crispin 	.probe = rt288x_pci_probe,
265187c26ddSJohn Crispin 	.driver = {
266187c26ddSJohn Crispin 		.name = "rt288x-pci",
267187c26ddSJohn Crispin 		.of_match_table = rt288x_pci_match,
268187c26ddSJohn Crispin 	},
269187c26ddSJohn Crispin };
270187c26ddSJohn Crispin 
271187c26ddSJohn Crispin int __init pcibios_init(void)
272187c26ddSJohn Crispin {
273187c26ddSJohn Crispin 	int ret = platform_driver_register(&rt288x_pci_driver);
274187c26ddSJohn Crispin 
275187c26ddSJohn Crispin 	if (ret)
276187c26ddSJohn Crispin 		pr_info("rt288x-pci: Error registering platform driver!");
277187c26ddSJohn Crispin 
278187c26ddSJohn Crispin 	return ret;
279187c26ddSJohn Crispin }
280187c26ddSJohn Crispin 
281187c26ddSJohn Crispin arch_initcall(pcibios_init);
282