1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <linux/types.h>
43#include <linux/pci.h>
44#include <linux/kernel.h>
45#include <linux/slab.h>
46#include <linux/version.h>
47#include <asm/pci.h>
48#include <asm/io.h>
49#include <asm/mips-cm.h>
50#include <linux/init.h>
51#include <linux/module.h>
52#include <linux/delay.h>
53#include <linux/of.h>
54#include <linux/of_pci.h>
55#include <linux/platform_device.h>
56
57#include <ralink_regs.h>
58
59
60
61
62
63
64#define RALINK_PCIE0_CLK_EN (1<<24)
65#define RALINK_PCIE1_CLK_EN (1<<25)
66#define RALINK_PCIE2_CLK_EN (1<<26)
67
68#define RALINK_PCI_CONFIG_ADDR 0x20
69#define RALINK_PCI_CONFIG_DATA_VIRTUAL_REG 0x24
70#define RALINK_PCI_MEMBASE *(volatile u32 *)(RALINK_PCI_BASE + 0x0028)
71#define RALINK_PCI_IOBASE *(volatile u32 *)(RALINK_PCI_BASE + 0x002C)
72#define RALINK_PCIE0_RST (1<<24)
73#define RALINK_PCIE1_RST (1<<25)
74#define RALINK_PCIE2_RST (1<<26)
75#define RALINK_SYSCTL_BASE 0xBE000000
76
77#define RALINK_PCI_PCICFG_ADDR *(volatile u32 *)(RALINK_PCI_BASE + 0x0000)
78#define RALINK_PCI_PCIMSK_ADDR *(volatile u32 *)(RALINK_PCI_BASE + 0x000C)
79#define RALINK_PCI_BASE 0xBE140000
80
81#define RALINK_PCIEPHY_P0P1_CTL_OFFSET (RALINK_PCI_BASE + 0x9000)
82#define RT6855_PCIE0_OFFSET 0x2000
83#define RT6855_PCIE1_OFFSET 0x3000
84#define RT6855_PCIE2_OFFSET 0x4000
85
86#define RALINK_PCI0_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0010)
87#define RALINK_PCI0_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0018)
88#define RALINK_PCI0_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0030)
89#define RALINK_PCI0_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0034)
90#define RALINK_PCI0_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0038)
91#define RALINK_PCI0_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0050)
92#define RALINK_PCI0_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0060)
93#define RALINK_PCI0_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE0_OFFSET + 0x0064)
94
95#define RALINK_PCI1_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0010)
96#define RALINK_PCI1_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0018)
97#define RALINK_PCI1_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0030)
98#define RALINK_PCI1_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0034)
99#define RALINK_PCI1_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0038)
100#define RALINK_PCI1_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0050)
101#define RALINK_PCI1_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0060)
102#define RALINK_PCI1_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE1_OFFSET + 0x0064)
103
104#define RALINK_PCI2_BAR0SETUP_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0010)
105#define RALINK_PCI2_IMBASEBAR0_ADDR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0018)
106#define RALINK_PCI2_ID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0030)
107#define RALINK_PCI2_CLASS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0034)
108#define RALINK_PCI2_SUBID *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0038)
109#define RALINK_PCI2_STATUS *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0050)
110#define RALINK_PCI2_DERR *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0060)
111#define RALINK_PCI2_ECRC *(volatile u32 *)(RALINK_PCI_BASE + RT6855_PCIE2_OFFSET + 0x0064)
112
113#define RALINK_PCIEPHY_P0P1_CTL_OFFSET (RALINK_PCI_BASE + 0x9000)
114#define RALINK_PCIEPHY_P2_CTL_OFFSET (RALINK_PCI_BASE + 0xA000)
115
116#define MV_WRITE(ofs, data) \
117 *(volatile u32 *)(RALINK_PCI_BASE+(ofs)) = cpu_to_le32(data)
118#define MV_READ(ofs, data) \
119 *(data) = le32_to_cpu(*(volatile u32 *)(RALINK_PCI_BASE+(ofs)))
120#define MV_READ_DATA(ofs) \
121 le32_to_cpu(*(volatile u32 *)(RALINK_PCI_BASE+(ofs)))
122
123#define MV_WRITE_16(ofs, data) \
124 *(volatile u16 *)(RALINK_PCI_BASE+(ofs)) = cpu_to_le16(data)
125#define MV_READ_16(ofs, data) \
126 *(data) = le16_to_cpu(*(volatile u16 *)(RALINK_PCI_BASE+(ofs)))
127
128#define MV_WRITE_8(ofs, data) \
129 *(volatile u8 *)(RALINK_PCI_BASE+(ofs)) = data
130#define MV_READ_8(ofs, data) \
131 *(data) = *(volatile u8 *)(RALINK_PCI_BASE+(ofs))
132
133#define RALINK_PCI_MM_MAP_BASE 0x60000000
134#define RALINK_PCI_IO_MAP_BASE 0x1e160000
135
136#define RALINK_SYSTEM_CONTROL_BASE 0xbe000000
137
138#define ASSERT_SYSRST_PCIE(val) \
139 do { \
140 if (*(unsigned int *)(0xbe00000c) == 0x00030101) \
141 RALINK_RSTCTRL |= val; \
142 else \
143 RALINK_RSTCTRL &= ~val; \
144 } while(0)
145#define DEASSERT_SYSRST_PCIE(val) \
146 do { \
147 if (*(unsigned int *)(0xbe00000c) == 0x00030101) \
148 RALINK_RSTCTRL &= ~val; \
149 else \
150 RALINK_RSTCTRL |= val; \
151 } while(0)
152#define RALINK_SYSCFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x14)
153#define RALINK_CLKCFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x30)
154#define RALINK_RSTCTRL *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x34)
155#define RALINK_GPIOMODE *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x60)
156#define RALINK_PCIE_CLK_GEN *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x7c)
157#define RALINK_PCIE_CLK_GEN1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x80)
158#define PPLL_CFG1 *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0x9c)
159#define PPLL_DRV *(unsigned int *)(RALINK_SYSTEM_CONTROL_BASE + 0xa0)
160
161#define RALINK_PCI_HOST_MODE_EN (1<<7)
162#define RALINK_PCIE_RC_MODE_EN (1<<8)
163
164#define RALINK_PCIE_RST (1<<23)
165#define RALINK_PCI_RST (1<<24)
166
167#define RALINK_PCI_CLK_EN (1<<19)
168#define RALINK_PCIE_CLK_EN (1<<21)
169
170#define PCI_SLOTx2 (1<<11)
171#define PCI_SLOTx1 (2<<11)
172
173#define PDRV_SW_SET (1<<31)
174#define LC_CKDRVPD_ (1<<19)
175
176#define MEMORY_BASE 0x0
177static int pcie_link_status = 0;
178
179#define PCI_ACCESS_READ_1 0
180#define PCI_ACCESS_READ_2 1
181#define PCI_ACCESS_READ_4 2
182#define PCI_ACCESS_WRITE_1 3
183#define PCI_ACCESS_WRITE_2 4
184#define PCI_ACCESS_WRITE_4 5
185
186static int config_access(unsigned char access_type, struct pci_bus *bus,
187 unsigned int devfn, unsigned int where, u32 * data)
188{
189 unsigned int slot = PCI_SLOT(devfn);
190 u8 func = PCI_FUNC(devfn);
191 uint32_t address_reg, data_reg;
192 unsigned int address;
193
194 address_reg = RALINK_PCI_CONFIG_ADDR;
195 data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
196
197 address = (((where&0xF00)>>8)<<24) |(bus->number << 16) | (slot << 11) |
198 (func << 8) | (where & 0xfc) | 0x80000000;
199 MV_WRITE(address_reg, address);
200
201 switch(access_type) {
202 case PCI_ACCESS_WRITE_1:
203 MV_WRITE_8(data_reg+(where&0x3), *data);
204 break;
205 case PCI_ACCESS_WRITE_2:
206 MV_WRITE_16(data_reg+(where&0x3), *data);
207 break;
208 case PCI_ACCESS_WRITE_4:
209 MV_WRITE(data_reg, *data);
210 break;
211 case PCI_ACCESS_READ_1:
212 MV_READ_8( data_reg+(where&0x3), data);
213 break;
214 case PCI_ACCESS_READ_2:
215 MV_READ_16(data_reg+(where&0x3), data);
216 break;
217 case PCI_ACCESS_READ_4:
218 MV_READ(data_reg, data);
219 break;
220 default:
221 printk("no specify access type\n");
222 break;
223 }
224 return 0;
225}
226
227static int
228read_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 * val)
229{
230 return config_access(PCI_ACCESS_READ_1, bus, devfn, (unsigned int)where, (u32 *)val);
231}
232
233static int
234read_config_word(struct pci_bus *bus, unsigned int devfn, int where, u16 * val)
235{
236 return config_access(PCI_ACCESS_READ_2, bus, devfn, (unsigned int)where, (u32 *)val);
237}
238
239static int
240read_config_dword(struct pci_bus *bus, unsigned int devfn, int where, u32 * val)
241{
242 return config_access(PCI_ACCESS_READ_4, bus, devfn, (unsigned int)where, (u32 *)val);
243}
244
245static int
246write_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 val)
247{
248 if (config_access(PCI_ACCESS_WRITE_1, bus, devfn, (unsigned int)where, (u32 *)&val))
249 return -1;
250
251 return PCIBIOS_SUCCESSFUL;
252}
253
254static int
255write_config_word(struct pci_bus *bus, unsigned int devfn, int where, u16 val)
256{
257 if (config_access(PCI_ACCESS_WRITE_2, bus, devfn, where, (u32 *)&val))
258 return -1;
259
260 return PCIBIOS_SUCCESSFUL;
261}
262
263static int
264write_config_dword(struct pci_bus *bus, unsigned int devfn, int where, u32 val)
265{
266 if (config_access(PCI_ACCESS_WRITE_4, bus, devfn, where, &val))
267 return -1;
268
269 return PCIBIOS_SUCCESSFUL;
270}
271
272static int
273pci_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
274{
275 switch (size) {
276 case 1:
277 return read_config_byte(bus, devfn, where, (u8 *) val);
278 case 2:
279 return read_config_word(bus, devfn, where, (u16 *) val);
280 default:
281 return read_config_dword(bus, devfn, where, val);
282 }
283}
284
285static int
286pci_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
287{
288 switch (size) {
289 case 1:
290 return write_config_byte(bus, devfn, where, (u8) val);
291 case 2:
292 return write_config_word(bus, devfn, where, (u16) val);
293 default:
294 return write_config_dword(bus, devfn, where, val);
295 }
296}
297
298struct pci_ops mt7621_pci_ops= {
299 .read = pci_config_read,
300 .write = pci_config_write,
301};
302
303static struct resource mt7621_res_pci_mem1;
304static struct resource mt7621_res_pci_io1;
305static struct pci_controller mt7621_controller = {
306 .pci_ops = &mt7621_pci_ops,
307 .mem_resource = &mt7621_res_pci_mem1,
308 .io_resource = &mt7621_res_pci_io1,
309};
310
311static void
312read_config(unsigned long bus, unsigned long dev, unsigned long func, unsigned long reg, unsigned long *val)
313{
314 unsigned int address_reg, data_reg, address;
315
316 address_reg = RALINK_PCI_CONFIG_ADDR;
317 data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
318 address = (((reg & 0xF00)>>8)<<24) | (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 0x80000000 ;
319 MV_WRITE(address_reg, address);
320 MV_READ(data_reg, val);
321 return;
322}
323
324static void
325write_config(unsigned long bus, unsigned long dev, unsigned long func, unsigned long reg, unsigned long val)
326{
327 unsigned int address_reg, data_reg, address;
328
329 address_reg = RALINK_PCI_CONFIG_ADDR;
330 data_reg = RALINK_PCI_CONFIG_DATA_VIRTUAL_REG;
331 address = (((reg & 0xF00)>>8)<<24) | (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 0x80000000 ;
332 MV_WRITE(address_reg, address);
333 MV_WRITE(data_reg, val);
334 return;
335}
336
337int
338pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
339{
340 u16 cmd;
341 u32 val;
342 int irq;
343
344 if (dev->bus->number == 0) {
345 write_config(0, slot, 0, PCI_BASE_ADDRESS_0, MEMORY_BASE);
346 read_config(0, slot, 0, PCI_BASE_ADDRESS_0, (unsigned long *)&val);
347 printk("BAR0 at slot %d = %x\n", slot, val);
348 }
349
350 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14);
351 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xFF);
352 pci_read_config_word(dev, PCI_COMMAND, &cmd);
353 cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
354 pci_write_config_word(dev, PCI_COMMAND, cmd);
355
356 irq = of_irq_parse_and_map_pci(dev, slot, pin);
357
358 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
359 return irq;
360}
361
362void
363set_pcie_phy(u32 *addr, int start_b, int bits, int val)
364{
365 *(unsigned int *)(addr) &= ~(((1<<bits) - 1)<<start_b);
366 *(unsigned int *)(addr) |= val << start_b;
367}
368
369void
370bypass_pipe_rst(void)
371{
372
373 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 12, 1, 0x01);
374 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x02c), 4, 1, 0x01);
375
376 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 12, 1, 0x01);
377 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x12c), 4, 1, 0x01);
378
379 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 12, 1, 0x01);
380 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x02c), 4, 1, 0x01);
381}
382
383void
384set_phy_for_ssc(void)
385{
386 unsigned long reg = (*(volatile u32 *)(RALINK_SYSCTL_BASE + 0x10));
387
388 reg = (reg >> 6) & 0x7;
389
390
391 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 8, 1, 0x01);
392 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x400), 9, 2, 0x00);
393 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x01);
394 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x01);
395 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x00);
396 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x00);
397 if(reg <= 5 && reg >= 3) {
398 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x01);
399 printk("***** Xtal 40MHz *****\n");
400 } else {
401 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 6, 2, 0x00);
402 if (reg >= 6) {
403 printk("***** Xtal 25MHz *****\n");
404 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4bc), 4, 2, 0x01);
405 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x49c), 0,31, 0x18000000);
406 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a4), 0,16, 0x18d);
407 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 0,12, 0x4a);
408 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a8), 16,12, 0x4a);
409 } else {
410 printk("***** Xtal 20MHz *****\n");
411 }
412 }
413 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4a0), 5, 1, 0x01);
414 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 22, 2, 0x02);
415 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 18, 4, 0x06);
416 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 12, 4, 0x02);
417 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 8, 4, 0x01);
418 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x4ac), 16, 3, 0x00);
419 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x490), 1, 3, 0x02);
420 if(reg <= 5 && reg >= 3) {
421 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 6, 2, 0x01);
422 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x414), 5, 1, 0x01);
423 }
424
425 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 5, 1, 0x01);
426 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 5, 1, 0x01);
427 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x000), 4, 1, 0x00);
428 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P0P1_CTL_OFFSET + 0x100), 4, 1, 0x00);
429
430
431
432 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 8, 1, 0x01);
433 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x400), 9, 2, 0x00);
434 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x01);
435 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x00);
436 if(reg <= 5 && reg >= 3) {
437 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x01);
438 } else {
439 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 6, 2, 0x00);
440 if (reg >= 6) {
441 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4bc), 4, 2, 0x01);
442 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x49c), 0,31, 0x18000000);
443 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a4), 0,16, 0x18d);
444 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 0,12, 0x4a);
445 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a8), 16,12, 0x4a);
446 }
447 }
448 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4a0), 5, 1, 0x01);
449 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 22, 2, 0x02);
450 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 18, 4, 0x06);
451 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 12, 4, 0x02);
452 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 8, 4, 0x01);
453 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x4ac), 16, 3, 0x00);
454 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x490), 1, 3, 0x02);
455 if(reg <= 5 && reg >= 3) {
456 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 6, 2, 0x01);
457 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x414), 5, 1, 0x01);
458 }
459
460 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 5, 1, 0x01);
461 set_pcie_phy((u32 *)(RALINK_PCIEPHY_P2_CTL_OFFSET + 0x000), 4, 1, 0x00);
462}
463
464void setup_cm_memory_region(struct resource *mem_resource)
465{
466 resource_size_t mask;
467 if (mips_cps_numiocu(0)) {
468
469
470
471 mask = ~(mem_resource->end - mem_resource->start);
472
473 write_gcr_reg1_base(mem_resource->start);
474 write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
475 printk("PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
476 (unsigned long long)read_gcr_reg1_base(),
477 (unsigned long long)read_gcr_reg1_mask());
478 }
479}
480
481static int mt7621_pci_probe(struct platform_device *pdev)
482{
483 unsigned long val = 0;
484
485 iomem_resource.start = 0;
486 iomem_resource.end= ~0;
487 ioport_resource.start= 0;
488 ioport_resource.end = ~0;
489
490 val = RALINK_PCIE0_RST;
491 val |= RALINK_PCIE1_RST;
492 val |= RALINK_PCIE2_RST;
493
494 ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST | RALINK_PCIE1_RST | RALINK_PCIE2_RST);
495
496 *(unsigned int *)(0xbe000060) &= ~(0x3<<10 | 0x3<<3);
497 *(unsigned int *)(0xbe000060) |= 0x1<<10 | 0x1<<3;
498 mdelay(100);
499 *(unsigned int *)(0xbe000600) |= 0x1<<19 | 0x1<<8 | 0x1<<7;
500 mdelay(100);
501 *(unsigned int *)(0xbe000620) &= ~(0x1<<19 | 0x1<<8 | 0x1<<7);
502
503 mdelay(100);
504
505 val = RALINK_PCIE0_RST;
506 val |= RALINK_PCIE1_RST;
507 val |= RALINK_PCIE2_RST;
508
509 DEASSERT_SYSRST_PCIE(val);
510
511 if ((*(unsigned int *)(0xbe00000c)&0xFFFF) == 0x0101)
512 bypass_pipe_rst();
513 set_phy_for_ssc();
514
515 read_config(0, 0, 0, 0x70c, &val);
516 printk("Port 0 N_FTS = %x\n", (unsigned int)val);
517
518 read_config(0, 1, 0, 0x70c, &val);
519 printk("Port 1 N_FTS = %x\n", (unsigned int)val);
520
521 read_config(0, 2, 0, 0x70c, &val);
522 printk("Port 2 N_FTS = %x\n", (unsigned int)val);
523
524 RALINK_RSTCTRL = (RALINK_RSTCTRL | RALINK_PCIE_RST);
525 RALINK_SYSCFG1 &= ~(0x30);
526 RALINK_SYSCFG1 |= (2<<4);
527 RALINK_PCIE_CLK_GEN &= 0x7fffffff;
528 RALINK_PCIE_CLK_GEN1 &= 0x80ffffff;
529 RALINK_PCIE_CLK_GEN1 |= 0xa << 24;
530 RALINK_PCIE_CLK_GEN |= 0x80000000;
531 mdelay(50);
532 RALINK_RSTCTRL = (RALINK_RSTCTRL & ~RALINK_PCIE_RST);
533
534
535 *(unsigned int *)(0xbe000620) |= 0x1<<19 | 0x1<<8 | 0x1<<7;
536 mdelay(1000);
537
538 if(( RALINK_PCI0_STATUS & 0x1) == 0)
539 {
540 printk("PCIE0 no card, disable it(RST&CLK)\n");
541 ASSERT_SYSRST_PCIE(RALINK_PCIE0_RST);
542 RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE0_CLK_EN);
543 pcie_link_status &= ~(1<<0);
544 } else {
545 pcie_link_status |= 1<<0;
546 RALINK_PCI_PCIMSK_ADDR |= (1<<20);
547 }
548
549 if(( RALINK_PCI1_STATUS & 0x1) == 0)
550 {
551 printk("PCIE1 no card, disable it(RST&CLK)\n");
552 ASSERT_SYSRST_PCIE(RALINK_PCIE1_RST);
553 RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE1_CLK_EN);
554 pcie_link_status &= ~(1<<1);
555 } else {
556 pcie_link_status |= 1<<1;
557 RALINK_PCI_PCIMSK_ADDR |= (1<<21);
558 }
559
560 if (( RALINK_PCI2_STATUS & 0x1) == 0) {
561 printk("PCIE2 no card, disable it(RST&CLK)\n");
562 ASSERT_SYSRST_PCIE(RALINK_PCIE2_RST);
563 RALINK_CLKCFG1 = (RALINK_CLKCFG1 & ~RALINK_PCIE2_CLK_EN);
564 pcie_link_status &= ~(1<<2);
565 } else {
566 pcie_link_status |= 1<<2;
567 RALINK_PCI_PCIMSK_ADDR |= (1<<22);
568 }
569
570 if (pcie_link_status == 0)
571 return 0;
572
573
574
575
576
577
578
579
580
581
582
583
584 switch(pcie_link_status) {
585 case 2:
586 RALINK_PCI_PCICFG_ADDR &= ~0x00ff0000;
587 RALINK_PCI_PCICFG_ADDR |= 0x1 << 16;
588 RALINK_PCI_PCICFG_ADDR |= 0x0 << 20;
589 break;
590 case 4:
591 RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
592 RALINK_PCI_PCICFG_ADDR |= 0x1 << 16;
593 RALINK_PCI_PCICFG_ADDR |= 0x2 << 20;
594 RALINK_PCI_PCICFG_ADDR |= 0x0 << 24;
595 break;
596 case 5:
597 RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
598 RALINK_PCI_PCICFG_ADDR |= 0x0 << 16;
599 RALINK_PCI_PCICFG_ADDR |= 0x2 << 20;
600 RALINK_PCI_PCICFG_ADDR |= 0x1 << 24;
601 break;
602 case 6:
603 RALINK_PCI_PCICFG_ADDR &= ~0x0fff0000;
604 RALINK_PCI_PCICFG_ADDR |= 0x2 << 16;
605 RALINK_PCI_PCICFG_ADDR |= 0x0 << 20;
606 RALINK_PCI_PCICFG_ADDR |= 0x1 << 24;
607 break;
608 }
609
610
611
612
613
614
615 RALINK_PCI_MEMBASE = 0xffffffff;
616 RALINK_PCI_IOBASE = RALINK_PCI_IO_MAP_BASE;
617
618
619 if((pcie_link_status & 0x1) != 0) {
620 RALINK_PCI0_BAR0SETUP_ADDR = 0x7FFF0001;
621 RALINK_PCI0_IMBASEBAR0_ADDR = MEMORY_BASE;
622 RALINK_PCI0_CLASS = 0x06040001;
623 printk("PCIE0 enabled\n");
624 }
625
626
627 if ((pcie_link_status & 0x2) != 0) {
628 RALINK_PCI1_BAR0SETUP_ADDR = 0x7FFF0001;
629 RALINK_PCI1_IMBASEBAR0_ADDR = MEMORY_BASE;
630 RALINK_PCI1_CLASS = 0x06040001;
631 printk("PCIE1 enabled\n");
632 }
633
634
635 if ((pcie_link_status & 0x4) != 0) {
636 RALINK_PCI2_BAR0SETUP_ADDR = 0x7FFF0001;
637 RALINK_PCI2_IMBASEBAR0_ADDR = MEMORY_BASE;
638 RALINK_PCI2_CLASS = 0x06040001;
639 printk("PCIE2 enabled\n");
640 }
641
642 switch(pcie_link_status) {
643 case 7:
644 read_config(0, 2, 0, 0x4, &val);
645 write_config(0, 2, 0, 0x4, val|0x4);
646 read_config(0, 2, 0, 0x70c, &val);
647 val &= ~(0xff)<<8;
648 val |= 0x50<<8;
649 write_config(0, 2, 0, 0x70c, val);
650 case 3:
651 case 5:
652 case 6:
653 read_config(0, 1, 0, 0x4, &val);
654 write_config(0, 1, 0, 0x4, val|0x4);
655 read_config(0, 1, 0, 0x70c, &val);
656 val &= ~(0xff)<<8;
657 val |= 0x50<<8;
658 write_config(0, 1, 0, 0x70c, val);
659 default:
660 read_config(0, 0, 0, 0x4, &val);
661 write_config(0, 0, 0, 0x4, val|0x4);
662 read_config(0, 0, 0, 0x70c, &val);
663 val &= ~(0xff)<<8;
664 val |= 0x50<<8;
665 write_config(0, 0, 0, 0x70c, val);
666 }
667
668 pci_load_of_ranges(&mt7621_controller, pdev->dev.of_node);
669 setup_cm_memory_region(mt7621_controller.mem_resource);
670 register_pci_controller(&mt7621_controller);
671 return 0;
672
673}
674
675int pcibios_plat_dev_init(struct pci_dev *dev)
676{
677 return 0;
678}
679
680static const struct of_device_id mt7621_pci_ids[] = {
681 { .compatible = "mediatek,mt7621-pci" },
682 {},
683};
684MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
685
686static struct platform_driver mt7621_pci_driver = {
687 .probe = mt7621_pci_probe,
688 .driver = {
689 .name = "mt7621-pci",
690 .of_match_table = of_match_ptr(mt7621_pci_ids),
691 },
692};
693
694static int __init mt7621_pci_init(void)
695{
696 return platform_driver_register(&mt7621_pci_driver);
697}
698
699arch_initcall(mt7621_pci_init);
700