Merge branch 'master'
[sfrench/cifs-2.6.git] / arch / ppc / syslib / ppc440spe_pcie.c
1 /*
2  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3  * Roland Dreier <rolandd@cisco.com>
4  *
5  * This program is free software; you can redistribute  it and/or modify it
6  * under  the terms of  the GNU General  Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/pci.h>
15 #include <linux/init.h>
16
17 #include <asm/reg.h>
18 #include <asm/io.h>
19 #include <asm/ibm44x.h>
20
21 #include "ppc440spe_pcie.h"
22
23 static int
24 pcie_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
25                      int len, u32 *val)
26 {
27         struct pci_controller *hose = bus->sysdata;
28
29         if (PCI_SLOT(devfn) != 1)
30                 return PCIBIOS_DEVICE_NOT_FOUND;
31
32         offset += devfn << 12;
33
34         /*
35          * Note: the caller has already checked that offset is
36          * suitably aligned and that len is 1, 2 or 4.
37          */
38         switch (len) {
39         case 1:
40                 *val = in_8(hose->cfg_data + offset);
41                 break;
42         case 2:
43                 *val = in_le16(hose->cfg_data + offset);
44                 break;
45         default:
46                 *val = in_le32(hose->cfg_data + offset);
47                 break;
48         }
49
50         if (0) printk("%s: read %x(%d) @ %x\n", __func__, *val, len, offset);
51
52         return PCIBIOS_SUCCESSFUL;
53 }
54
55 static int
56 pcie_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
57                       int len, u32 val)
58 {
59         struct pci_controller *hose = bus->sysdata;
60
61         if (PCI_SLOT(devfn) != 1)
62                 return PCIBIOS_DEVICE_NOT_FOUND;
63
64         offset += devfn << 12;
65
66         switch (len) {
67         case 1:
68                 out_8(hose->cfg_data + offset, val);
69                 break;
70         case 2:
71                 out_le16(hose->cfg_data + offset, val);
72                 break;
73         default:
74                 out_le32(hose->cfg_data + offset, val);
75                 break;
76         }
77         return PCIBIOS_SUCCESSFUL;
78 }
79
80 static struct pci_ops pcie_pci_ops =
81 {
82         .read  = pcie_read_config,
83         .write = pcie_write_config
84 };
85
86 enum {
87         PTYPE_ENDPOINT          = 0x0,
88         PTYPE_LEGACY_ENDPOINT   = 0x1,
89         PTYPE_ROOT_PORT         = 0x4,
90
91         LNKW_X1                 = 0x1,
92         LNKW_X4                 = 0x4,
93         LNKW_X8                 = 0x8
94 };
95
96 static void check_error(void)
97 {
98         u32 valPE0, valPE1, valPE2;
99
100         /* SDR0_PEGPLLLCT1 reset */
101         if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
102                 printk(KERN_INFO "PCIE: SDR0_PEGPLLLCT1 reset error 0x%8x\n", valPE0);
103         }
104
105         valPE0 = SDR_READ(PESDR0_RCSSET);
106         valPE1 = SDR_READ(PESDR1_RCSSET);
107         valPE2 = SDR_READ(PESDR2_RCSSET);
108
109         /* SDR0_PExRCSSET rstgu */
110         if ( !(valPE0 & 0x01000000) ||
111              !(valPE1 & 0x01000000) ||
112              !(valPE2 & 0x01000000)) {
113                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET rstgu error\n");
114         }
115
116         /* SDR0_PExRCSSET rstdl */
117         if ( !(valPE0 & 0x00010000) ||
118              !(valPE1 & 0x00010000) ||
119              !(valPE2 & 0x00010000)) {
120                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET rstdl error\n");
121         }
122
123         /* SDR0_PExRCSSET rstpyn */
124         if ( (valPE0 & 0x00001000) ||
125              (valPE1 & 0x00001000) ||
126              (valPE2 & 0x00001000)) {
127                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET rstpyn error\n");
128         }
129
130         /* SDR0_PExRCSSET hldplb */
131         if ( (valPE0 & 0x10000000) ||
132              (valPE1 & 0x10000000) ||
133              (valPE2 & 0x10000000)) {
134                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET hldplb error\n");
135         }
136
137         /* SDR0_PExRCSSET rdy */
138         if ( (valPE0 & 0x00100000) ||
139              (valPE1 & 0x00100000) ||
140              (valPE2 & 0x00100000)) {
141                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET rdy error\n");
142         }
143
144         /* SDR0_PExRCSSET shutdown */
145         if ( (valPE0 & 0x00000100) ||
146              (valPE1 & 0x00000100) ||
147              (valPE2 & 0x00000100)) {
148                 printk(KERN_INFO "PCIE:  SDR0_PExRCSSET shutdown error\n");
149         }
150 }
151
152 /*
153  * Initialize PCI Express core as described in User Manual section 27.12.1
154  */
155 int ppc440spe_init_pcie(void)
156 {
157         /* Set PLL clock receiver to LVPECL */
158         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
159
160         check_error();
161
162         printk(KERN_INFO "PCIE initialization OK\n");
163
164         if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
165                 printk(KERN_INFO "PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
166                        SDR_READ(PESDR0_PLLLCT2));
167
168         /* De-assert reset of PCIe PLL, wait for lock */
169         SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
170         udelay(3);
171
172         return 0;
173 }
174
175 int ppc440spe_init_pcie_rootport(int port)
176 {
177         static int core_init;
178         void __iomem *utl_base;
179         u32 val = 0;
180         int i;
181
182         if (!core_init) {
183                 ++core_init;
184                 i = ppc440spe_init_pcie();
185                 if (i)
186                         return i;
187         }
188
189         /*
190          * Initialize various parts of the PCI Express core for our port:
191          *
192          * - Set as a root port and enable max width
193          *   (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
194          * - Set up UTL configuration.
195          * - Increase SERDES drive strength to levels suggested by AMCC.
196          * - De-assert RSTPYN, RSTDL and RSTGU.
197          */
198         switch (port) {
199         case 0:
200                 SDR_WRITE(PESDR0_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X8 << 12);
201
202                 SDR_WRITE(PESDR0_UTLSET1, 0x21222222);
203                 SDR_WRITE(PESDR0_UTLSET2, 0x11000000);
204
205                 SDR_WRITE(PESDR0_HSSL0SET1, 0x35000000);
206                 SDR_WRITE(PESDR0_HSSL1SET1, 0x35000000);
207                 SDR_WRITE(PESDR0_HSSL2SET1, 0x35000000);
208                 SDR_WRITE(PESDR0_HSSL3SET1, 0x35000000);
209                 SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
210                 SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
211                 SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
212                 SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
213
214                 SDR_WRITE(PESDR0_RCSSET,
215                           (SDR_READ(PESDR0_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
216                 break;
217
218         case 1:
219                 SDR_WRITE(PESDR1_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
220
221                 SDR_WRITE(PESDR1_UTLSET1, 0x21222222);
222                 SDR_WRITE(PESDR1_UTLSET2, 0x11000000);
223
224                 SDR_WRITE(PESDR1_HSSL0SET1, 0x35000000);
225                 SDR_WRITE(PESDR1_HSSL1SET1, 0x35000000);
226                 SDR_WRITE(PESDR1_HSSL2SET1, 0x35000000);
227                 SDR_WRITE(PESDR1_HSSL3SET1, 0x35000000);
228
229                 SDR_WRITE(PESDR1_RCSSET,
230                           (SDR_READ(PESDR1_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
231                 break;
232
233         case 2:
234                 SDR_WRITE(PESDR2_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
235
236                 SDR_WRITE(PESDR2_UTLSET1, 0x21222222);
237                 SDR_WRITE(PESDR2_UTLSET2, 0x11000000);
238
239                 SDR_WRITE(PESDR2_HSSL0SET1, 0x35000000);
240                 SDR_WRITE(PESDR2_HSSL1SET1, 0x35000000);
241                 SDR_WRITE(PESDR2_HSSL2SET1, 0x35000000);
242                 SDR_WRITE(PESDR2_HSSL3SET1, 0x35000000);
243
244                 SDR_WRITE(PESDR2_RCSSET,
245                           (SDR_READ(PESDR2_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
246                 break;
247         }
248
249         mdelay(1000);
250
251         switch (port) {
252         case 0: val = SDR_READ(PESDR0_RCSSTS); break;
253         case 1: val = SDR_READ(PESDR1_RCSSTS); break;
254         case 2: val = SDR_READ(PESDR2_RCSSTS); break;
255         }
256
257         if (!(val & (1 << 20)))
258                 printk(KERN_INFO "PCIE%d: PGRST inactive\n", port);
259         else
260                 printk(KERN_WARNING "PGRST for PCIE%d failed %08x\n", port, val);
261
262         switch (port) {
263         case 0: printk(KERN_INFO "PCIE0: LOOP %08x\n", SDR_READ(PESDR0_LOOP)); break;
264         case 1: printk(KERN_INFO "PCIE1: LOOP %08x\n", SDR_READ(PESDR1_LOOP)); break;
265         case 2: printk(KERN_INFO "PCIE2: LOOP %08x\n", SDR_READ(PESDR2_LOOP)); break;
266         }
267
268         /*
269          * Map UTL registers at 0xc_1000_0n00
270          */
271         switch (port) {
272         case 0:
273                 mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
274                 mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x10000000);
275                 mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
276                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
277                 break;
278
279         case 1:
280                 mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
281                 mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x10001000);
282                 mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
283                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
284                 break;
285
286         case 2:
287                 mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
288                 mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x10002000);
289                 mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
290                 mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
291         }
292
293         utl_base = ioremap64(0xc10000000ull + 0x1000 * port, 0x100);
294
295         /*
296          * Set buffer allocations and then assert VRB and TXE.
297          */
298         out_be32(utl_base + PEUTL_OUTTR,   0x08000000);
299         out_be32(utl_base + PEUTL_INTR,    0x02000000);
300         out_be32(utl_base + PEUTL_OPDBSZ,  0x10000000);
301         out_be32(utl_base + PEUTL_PBBSZ,   0x53000000);
302         out_be32(utl_base + PEUTL_IPHBSZ,  0x08000000);
303         out_be32(utl_base + PEUTL_IPDBSZ,  0x10000000);
304         out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
305         out_be32(utl_base + PEUTL_PCTL,    0x80800066);
306
307         iounmap(utl_base);
308
309         /*
310          * We map PCI Express configuration access into the 512MB regions
311          *     PCIE0: 0xc_4000_0000
312          *     PCIE1: 0xc_8000_0000
313          *     PCIE2: 0xc_c000_0000
314          */
315         switch (port) {
316         case 0:
317                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000c);
318                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x40000000);
319                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
320                 break;
321
322         case 1:
323                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000c);
324                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x80000000);
325                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
326                 break;
327
328         case 2:
329                 mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000c);
330                 mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0xc0000000);
331                 mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
332                 break;
333         }
334
335         /*
336          * Check for VC0 active and assert RDY.
337          */
338         switch (port) {
339         case 0:
340                 if (!(SDR_READ(PESDR0_RCSSTS) & (1 << 16)))
341                         printk(KERN_WARNING "PCIE0: VC0 not active\n");
342                 SDR_WRITE(PESDR0_RCSSET, SDR_READ(PESDR0_RCSSET) | 1 << 20);
343                 break;
344         case 1:
345                 if (!(SDR_READ(PESDR1_RCSSTS) & (1 << 16)))
346                         printk(KERN_WARNING "PCIE0: VC0 not active\n");
347                 SDR_WRITE(PESDR1_RCSSET, SDR_READ(PESDR1_RCSSET) | 1 << 20);
348                 break;
349         case 2:
350                 if (!(SDR_READ(PESDR2_RCSSTS) & (1 << 16)))
351                         printk(KERN_WARNING "PCIE0: VC0 not active\n");
352                 SDR_WRITE(PESDR2_RCSSET, SDR_READ(PESDR2_RCSSET) | 1 << 20);
353                 break;
354         }
355
356 #if 0
357         /* Dump all config regs */
358         for (i = 0x300; i <= 0x320; ++i)
359                 printk("[%04x] 0x%08x\n", i, SDR_READ(i));
360         for (i = 0x340; i <= 0x353; ++i)
361                 printk("[%04x] 0x%08x\n", i, SDR_READ(i));
362         for (i = 0x370; i <= 0x383; ++i)
363                 printk("[%04x] 0x%08x\n", i, SDR_READ(i));
364         for (i = 0x3a0; i <= 0x3a2; ++i)
365                 printk("[%04x] 0x%08x\n", i, SDR_READ(i));
366         for (i = 0x3c0; i <= 0x3c3; ++i)
367                 printk("[%04x] 0x%08x\n", i, SDR_READ(i));
368 #endif
369
370         mdelay(100);
371
372         return 0;
373 }
374
375 void ppc440spe_setup_pcie(struct pci_controller *hose, int port)
376 {
377         void __iomem *mbase;
378
379         /*
380          * Map 16MB, which is enough for 4 bits of bus #
381          */
382         hose->cfg_data = ioremap64(0xc40000000ull + port * 0x40000000,
383                                    1 << 24);
384         hose->ops = &pcie_pci_ops;
385
386         /*
387          * Set bus numbers on our root port
388          */
389         mbase = ioremap64(0xc50000000ull + port * 0x40000000, 4096);
390         out_8(mbase + PCI_PRIMARY_BUS, 0);
391         out_8(mbase + PCI_SECONDARY_BUS, 0);
392
393         /*
394          * Set up outbound translation to hose->mem_space from PLB
395          * addresses at an offset of 0xd_0000_0000.  We set the low
396          * bits of the mask to 11 to turn off splitting into 8
397          * subregions and to enable the outbound translation.
398          */
399         out_le32(mbase + PECFG_POM0LAH, 0);
400         out_le32(mbase + PECFG_POM0LAL, hose->mem_space.start);
401
402         switch (port) {
403         case 0:
404                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0),  0x0000000d);
405                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0),  hose->mem_space.start);
406                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
407                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
408                       ~(hose->mem_space.end - hose->mem_space.start) | 3);
409                 break;
410         case 1:
411                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1),  0x0000000d);
412                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1),  hose->mem_space.start);
413                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
414                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
415                       ~(hose->mem_space.end - hose->mem_space.start) | 3);
416
417                 break;
418         case 2:
419                 mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2),  0x0000000d);
420                 mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2),  hose->mem_space.start);
421                 mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
422                 mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
423                       ~(hose->mem_space.end - hose->mem_space.start) | 3);
424                 break;
425         }
426
427         /* Set up 16GB inbound memory window at 0 */
428         out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
429         out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
430         out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
431         out_le32(mbase + PECFG_BAR0LMPA, 0);
432         out_le32(mbase + PECFG_PIM0LAL, 0);
433         out_le32(mbase + PECFG_PIM0LAH, 0);
434         out_le32(mbase + PECFG_PIMEN, 0x1);
435
436         /* Enable I/O, Mem, and Busmaster cycles */
437         out_le16(mbase + PCI_COMMAND,
438                  in_le16(mbase + PCI_COMMAND) |
439                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
440
441         iounmap(mbase);
442 }