Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Low-Level PCI Express Support for the SH7786 |
| 3 | * |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 4 | * Copyright (C) 2009 - 2010 Paul Mundt |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
| 10 | #include <linux/pci.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include "pcie-sh7786.h" |
| 16 | #include <asm/sizes.h> |
| 17 | |
| 18 | struct sh7786_pcie_port { |
| 19 | struct pci_channel *hose; |
| 20 | unsigned int index; |
| 21 | int endpoint; |
| 22 | int link; |
| 23 | }; |
| 24 | |
| 25 | static struct sh7786_pcie_port *sh7786_pcie_ports; |
| 26 | static unsigned int nr_ports; |
| 27 | |
| 28 | static struct sh7786_pcie_hwops { |
| 29 | int (*core_init)(void); |
| 30 | int (*port_init_hw)(struct sh7786_pcie_port *port); |
| 31 | } *sh7786_pcie_hwops; |
| 32 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 33 | static struct resource sh7786_pci0_resources[] = { |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 34 | { |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 35 | .name = "PCIe0 IO", |
| 36 | .start = 0xfd000000, |
| 37 | .end = 0xfd000000 + SZ_8M - 1, |
| 38 | .flags = IORESOURCE_IO, |
| 39 | }, { |
| 40 | .name = "PCIe0 MEM 0", |
| 41 | .start = 0xc0000000, |
| 42 | .end = 0xc0000000 + SZ_512M - 1, |
| 43 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 44 | }, { |
| 45 | .name = "PCIe0 MEM 1", |
| 46 | .start = 0x10000000, |
| 47 | .end = 0x10000000 + SZ_64M - 1, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 48 | .flags = IORESOURCE_MEM, |
| 49 | }, { |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 50 | .name = "PCIe0 MEM 2", |
| 51 | .start = 0xfe100000, |
| 52 | .end = 0xfe100000 + SZ_1M - 1, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 53 | }, |
| 54 | }; |
| 55 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 56 | static struct resource sh7786_pci1_resources[] = { |
| 57 | { |
| 58 | .name = "PCIe1 IO", |
| 59 | .start = 0xfd800000, |
| 60 | .end = 0xfd800000 + SZ_8M - 1, |
| 61 | .flags = IORESOURCE_IO, |
| 62 | }, { |
| 63 | .name = "PCIe1 MEM 0", |
| 64 | .start = 0xa0000000, |
| 65 | .end = 0xa0000000 + SZ_512M - 1, |
| 66 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 67 | }, { |
| 68 | .name = "PCIe1 MEM 1", |
| 69 | .start = 0x30000000, |
| 70 | .end = 0x30000000 + SZ_256M - 1, |
| 71 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 72 | }, { |
| 73 | .name = "PCIe1 MEM 2", |
| 74 | .start = 0xfe300000, |
| 75 | .end = 0xfe300000 + SZ_1M - 1, |
| 76 | }, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 77 | }; |
| 78 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 79 | static struct resource sh7786_pci2_resources[] = { |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 80 | { |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 81 | .name = "PCIe2 IO", |
| 82 | .start = 0xfc800000, |
| 83 | .end = 0xfc800000 + SZ_4M - 1, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 84 | }, { |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 85 | .name = "PCIe2 MEM 0", |
| 86 | .start = 0x80000000, |
| 87 | .end = 0x80000000 + SZ_512M - 1, |
| 88 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 89 | }, { |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 90 | .name = "PCIe2 MEM 1", |
| 91 | .start = 0x20000000, |
| 92 | .end = 0x20000000 + SZ_256M - 1, |
| 93 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 94 | }, { |
| 95 | .name = "PCIe2 MEM 2", |
| 96 | .start = 0xfcd00000, |
| 97 | .end = 0xfcd00000 + SZ_1M - 1, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 98 | }, |
| 99 | }; |
| 100 | |
| 101 | extern struct pci_ops sh7786_pci_ops; |
| 102 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 103 | #define DEFINE_CONTROLLER(start, idx) \ |
| 104 | { \ |
| 105 | .pci_ops = &sh7786_pci_ops, \ |
| 106 | .resources = sh7786_pci##idx##_resources, \ |
| 107 | .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \ |
| 108 | .reg_base = start, \ |
| 109 | .mem_offset = 0, \ |
| 110 | .io_offset = 0, \ |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static struct pci_channel sh7786_pci_channels[] = { |
| 114 | DEFINE_CONTROLLER(0xfe000000, 0), |
| 115 | DEFINE_CONTROLLER(0xfe200000, 1), |
| 116 | DEFINE_CONTROLLER(0xfcc00000, 2), |
| 117 | }; |
| 118 | |
| 119 | static int phy_wait_for_ack(struct pci_channel *chan) |
| 120 | { |
| 121 | unsigned int timeout = 100; |
| 122 | |
| 123 | while (timeout--) { |
| 124 | if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK)) |
| 125 | return 0; |
| 126 | |
| 127 | udelay(100); |
| 128 | } |
| 129 | |
| 130 | return -ETIMEDOUT; |
| 131 | } |
| 132 | |
| 133 | static int pci_wait_for_irq(struct pci_channel *chan, unsigned int mask) |
| 134 | { |
| 135 | unsigned int timeout = 100; |
| 136 | |
| 137 | while (timeout--) { |
| 138 | if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask) |
| 139 | return 0; |
| 140 | |
| 141 | udelay(100); |
| 142 | } |
| 143 | |
| 144 | return -ETIMEDOUT; |
| 145 | } |
| 146 | |
| 147 | static void phy_write_reg(struct pci_channel *chan, unsigned int addr, |
| 148 | unsigned int lane, unsigned int data) |
| 149 | { |
| 150 | unsigned long phyaddr, ctrl; |
| 151 | |
| 152 | phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) + |
| 153 | ((addr & 0xff) << BITS_ADR); |
| 154 | |
| 155 | /* Enable clock */ |
| 156 | ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR); |
| 157 | ctrl |= (1 << BITS_CKE); |
| 158 | pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR); |
| 159 | |
| 160 | /* Set write data */ |
| 161 | pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR); |
| 162 | pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR); |
| 163 | |
| 164 | phy_wait_for_ack(chan); |
| 165 | |
| 166 | /* Clear command */ |
| 167 | pci_write_reg(chan, 0, SH4A_PCIEPHYADRR); |
| 168 | |
| 169 | phy_wait_for_ack(chan); |
| 170 | |
| 171 | /* Disable clock */ |
| 172 | ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR); |
| 173 | ctrl &= ~(1 << BITS_CKE); |
| 174 | pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR); |
| 175 | } |
| 176 | |
| 177 | static int phy_init(struct pci_channel *chan) |
| 178 | { |
| 179 | unsigned int timeout = 100; |
| 180 | |
| 181 | /* Initialize the phy */ |
| 182 | phy_write_reg(chan, 0x60, 0xf, 0x004b008b); |
| 183 | phy_write_reg(chan, 0x61, 0xf, 0x00007b41); |
| 184 | phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00); |
| 185 | phy_write_reg(chan, 0x65, 0xf, 0x09070907); |
| 186 | phy_write_reg(chan, 0x66, 0xf, 0x00000010); |
| 187 | phy_write_reg(chan, 0x74, 0xf, 0x0007001c); |
| 188 | phy_write_reg(chan, 0x79, 0xf, 0x01fc000d); |
| 189 | |
| 190 | /* Deassert Standby */ |
| 191 | phy_write_reg(chan, 0x67, 0xf, 0x00000400); |
| 192 | |
| 193 | while (timeout--) { |
| 194 | if (pci_read_reg(chan, SH4A_PCIEPHYSR)) |
| 195 | return 0; |
| 196 | |
| 197 | udelay(100); |
| 198 | } |
| 199 | |
| 200 | return -ETIMEDOUT; |
| 201 | } |
| 202 | |
| 203 | static int pcie_init(struct sh7786_pcie_port *port) |
| 204 | { |
| 205 | struct pci_channel *chan = port->hose; |
| 206 | unsigned int data; |
| 207 | int ret; |
| 208 | |
| 209 | /* Begin initialization */ |
| 210 | pci_write_reg(chan, 0, SH4A_PCIETCTLR); |
| 211 | |
| 212 | /* Initialize as type1. */ |
| 213 | data = pci_read_reg(chan, SH4A_PCIEPCICONF3); |
| 214 | data &= ~(0x7f << 16); |
| 215 | data |= PCI_HEADER_TYPE_BRIDGE << 16; |
| 216 | pci_write_reg(chan, data, SH4A_PCIEPCICONF3); |
| 217 | |
| 218 | /* Initialize default capabilities. */ |
| 219 | data = pci_read_reg(chan, SH4A_PCIEEXPCAP0); |
| 220 | data &= ~(PCI_EXP_FLAGS_TYPE << 16); |
| 221 | |
| 222 | if (port->endpoint) |
| 223 | data |= PCI_EXP_TYPE_ENDPOINT << 20; |
| 224 | else |
| 225 | data |= PCI_EXP_TYPE_ROOT_PORT << 20; |
| 226 | |
| 227 | data |= PCI_CAP_ID_EXP; |
| 228 | pci_write_reg(chan, data, SH4A_PCIEEXPCAP0); |
| 229 | |
| 230 | /* Enable x4 link width and extended sync. */ |
| 231 | data = pci_read_reg(chan, SH4A_PCIEEXPCAP4); |
| 232 | data &= ~(PCI_EXP_LNKSTA_NLW << 16); |
| 233 | data |= (1 << 22) | PCI_EXP_LNKCTL_ES; |
| 234 | pci_write_reg(chan, data, SH4A_PCIEEXPCAP4); |
| 235 | |
| 236 | /* Set the completion timer timeout to the maximum 32ms. */ |
| 237 | data = pci_read_reg(chan, SH4A_PCIETLCTLR); |
| 238 | data &= ~0xffff; |
| 239 | data |= 0x32 << 8; |
| 240 | pci_write_reg(chan, data, SH4A_PCIETLCTLR); |
| 241 | |
| 242 | /* |
| 243 | * Set fast training sequences to the maximum 255, |
| 244 | * and enable MAC data scrambling. |
| 245 | */ |
| 246 | data = pci_read_reg(chan, SH4A_PCIEMACCTLR); |
| 247 | data &= ~PCIEMACCTLR_SCR_DIS; |
| 248 | data |= (0xff << 16); |
| 249 | pci_write_reg(chan, data, SH4A_PCIEMACCTLR); |
| 250 | |
| 251 | /* Finish initialization */ |
| 252 | data = pci_read_reg(chan, SH4A_PCIETCTLR); |
| 253 | data |= 0x1; |
| 254 | pci_write_reg(chan, data, SH4A_PCIETCTLR); |
| 255 | |
| 256 | /* Enable DL_Active Interrupt generation */ |
| 257 | data = pci_read_reg(chan, SH4A_PCIEDLINTENR); |
| 258 | data |= PCIEDLINTENR_DLL_ACT_ENABLE; |
| 259 | pci_write_reg(chan, data, SH4A_PCIEDLINTENR); |
| 260 | |
| 261 | /* Disable MAC data scrambling. */ |
| 262 | data = pci_read_reg(chan, SH4A_PCIEMACCTLR); |
| 263 | data |= PCIEMACCTLR_SCR_DIS | (0xff << 16); |
| 264 | pci_write_reg(chan, data, SH4A_PCIEMACCTLR); |
| 265 | |
| 266 | ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL); |
| 267 | if (unlikely(ret != 0)) |
| 268 | return -ENODEV; |
| 269 | |
| 270 | pci_write_reg(chan, 0x00100007, SH4A_PCIEPCICONF1); |
| 271 | pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR); |
| 272 | pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR); |
| 273 | pci_write_reg(chan, 0x000050A0, SH4A_PCIEEXPCAP2); |
| 274 | |
| 275 | wmb(); |
| 276 | |
| 277 | data = pci_read_reg(chan, SH4A_PCIEMACSR); |
| 278 | printk(KERN_NOTICE "PCI: PCIe#%d link width %d\n", |
| 279 | port->index, (data >> 20) & 0x3f); |
| 280 | |
| 281 | pci_write_reg(chan, 0x007c0000, SH4A_PCIEPAMR0); |
| 282 | pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH0); |
| 283 | pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL0); |
| 284 | pci_write_reg(chan, 0x80000100, SH4A_PCIEPTCTLR0); |
| 285 | |
| 286 | pci_write_reg(chan, 0x03fc0000, SH4A_PCIEPAMR2); |
| 287 | pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH2); |
| 288 | pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL2); |
| 289 | pci_write_reg(chan, 0x80000000, SH4A_PCIEPTCTLR2); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin) |
| 295 | { |
| 296 | return 71; |
| 297 | } |
| 298 | |
| 299 | static int sh7786_pcie_core_init(void) |
| 300 | { |
| 301 | /* Return the number of ports */ |
| 302 | return test_mode_pin(MODE_PIN12) ? 3 : 2; |
| 303 | } |
| 304 | |
| 305 | static int __devinit sh7786_pcie_init_hw(struct sh7786_pcie_port *port) |
| 306 | { |
| 307 | int ret; |
| 308 | |
| 309 | ret = phy_init(port->hose); |
| 310 | if (unlikely(ret < 0)) |
| 311 | return ret; |
| 312 | |
| 313 | /* |
| 314 | * Check if we are configured in endpoint or root complex mode, |
| 315 | * this is a fixed pin setting that applies to all PCIe ports. |
| 316 | */ |
| 317 | port->endpoint = test_mode_pin(MODE_PIN11); |
| 318 | |
| 319 | ret = pcie_init(port); |
| 320 | if (unlikely(ret < 0)) |
| 321 | return ret; |
| 322 | |
Paul Mundt | bcf3935 | 2010-02-01 13:11:25 +0900 | [diff] [blame] | 323 | return register_pci_controller(port->hose); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = { |
| 327 | .core_init = sh7786_pcie_core_init, |
| 328 | .port_init_hw = sh7786_pcie_init_hw, |
| 329 | }; |
| 330 | |
| 331 | static int __init sh7786_pcie_init(void) |
| 332 | { |
| 333 | int ret = 0, i; |
| 334 | |
| 335 | printk(KERN_NOTICE "PCI: Starting intialization.\n"); |
| 336 | |
| 337 | sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops; |
| 338 | |
| 339 | nr_ports = sh7786_pcie_hwops->core_init(); |
| 340 | BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels)); |
| 341 | |
| 342 | if (unlikely(nr_ports == 0)) |
| 343 | return -ENODEV; |
| 344 | |
| 345 | sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port), |
| 346 | GFP_KERNEL); |
| 347 | if (unlikely(!sh7786_pcie_ports)) |
| 348 | return -ENOMEM; |
| 349 | |
| 350 | printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports); |
| 351 | |
| 352 | for (i = 0; i < nr_ports; i++) { |
| 353 | struct sh7786_pcie_port *port = sh7786_pcie_ports + i; |
| 354 | |
| 355 | port->index = i; |
| 356 | port->hose = sh7786_pci_channels + i; |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame^] | 357 | port->hose->io_map_base = port->hose->resources[0].start; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 358 | |
| 359 | ret |= sh7786_pcie_hwops->port_init_hw(port); |
| 360 | } |
| 361 | |
| 362 | if (unlikely(ret)) |
| 363 | return ret; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | arch_initcall(sh7786_pcie_init); |