Path: blob/master/arch/sh/drivers/pci/pcie-sh7786.c
10819 views
/*1* Low-Level PCI Express Support for the SH77862*3* Copyright (C) 2009 - 2011 Paul Mundt4*5* This file is subject to the terms and conditions of the GNU General Public6* License. See the file "COPYING" in the main directory of this archive7* for more details.8*/9#define pr_fmt(fmt) "PCI: " fmt1011#include <linux/pci.h>12#include <linux/init.h>13#include <linux/kernel.h>14#include <linux/io.h>15#include <linux/async.h>16#include <linux/delay.h>17#include <linux/slab.h>18#include <linux/clk.h>19#include <linux/sh_clk.h>20#include "pcie-sh7786.h"21#include <asm/sizes.h>2223struct sh7786_pcie_port {24struct pci_channel *hose;25struct clk *fclk, phy_clk;26unsigned int index;27int endpoint;28int link;29};3031static struct sh7786_pcie_port *sh7786_pcie_ports;32static unsigned int nr_ports;3334static struct sh7786_pcie_hwops {35int (*core_init)(void);36async_func_ptr *port_init_hw;37} *sh7786_pcie_hwops;3839static struct resource sh7786_pci0_resources[] = {40{41.name = "PCIe0 IO",42.start = 0xfd000000,43.end = 0xfd000000 + SZ_8M - 1,44.flags = IORESOURCE_IO,45}, {46.name = "PCIe0 MEM 0",47.start = 0xc0000000,48.end = 0xc0000000 + SZ_512M - 1,49.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,50}, {51.name = "PCIe0 MEM 1",52.start = 0x10000000,53.end = 0x10000000 + SZ_64M - 1,54.flags = IORESOURCE_MEM,55}, {56.name = "PCIe0 MEM 2",57.start = 0xfe100000,58.end = 0xfe100000 + SZ_1M - 1,59.flags = IORESOURCE_MEM,60},61};6263static struct resource sh7786_pci1_resources[] = {64{65.name = "PCIe1 IO",66.start = 0xfd800000,67.end = 0xfd800000 + SZ_8M - 1,68.flags = IORESOURCE_IO,69}, {70.name = "PCIe1 MEM 0",71.start = 0xa0000000,72.end = 0xa0000000 + SZ_512M - 1,73.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,74}, {75.name = "PCIe1 MEM 1",76.start = 0x30000000,77.end = 0x30000000 + SZ_256M - 1,78.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,79}, {80.name = "PCIe1 MEM 2",81.start = 0xfe300000,82.end = 0xfe300000 + SZ_1M - 1,83.flags = IORESOURCE_MEM,84},85};8687static struct resource sh7786_pci2_resources[] = {88{89.name = "PCIe2 IO",90.start = 0xfc800000,91.end = 0xfc800000 + SZ_4M - 1,92.flags = IORESOURCE_IO,93}, {94.name = "PCIe2 MEM 0",95.start = 0x80000000,96.end = 0x80000000 + SZ_512M - 1,97.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,98}, {99.name = "PCIe2 MEM 1",100.start = 0x20000000,101.end = 0x20000000 + SZ_256M - 1,102.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,103}, {104.name = "PCIe2 MEM 2",105.start = 0xfcd00000,106.end = 0xfcd00000 + SZ_1M - 1,107.flags = IORESOURCE_MEM,108},109};110111extern struct pci_ops sh7786_pci_ops;112113#define DEFINE_CONTROLLER(start, idx) \114{ \115.pci_ops = &sh7786_pci_ops, \116.resources = sh7786_pci##idx##_resources, \117.nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \118.reg_base = start, \119.mem_offset = 0, \120.io_offset = 0, \121}122123static struct pci_channel sh7786_pci_channels[] = {124DEFINE_CONTROLLER(0xfe000000, 0),125DEFINE_CONTROLLER(0xfe200000, 1),126DEFINE_CONTROLLER(0xfcc00000, 2),127};128129static struct clk fixed_pciexclkp = {130.rate = 100000000, /* 100 MHz reference clock */131};132133static void __devinit sh7786_pci_fixup(struct pci_dev *dev)134{135/*136* Prevent enumeration of root complex resources.137*/138if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {139int i;140141for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {142dev->resource[i].start = 0;143dev->resource[i].end = 0;144dev->resource[i].flags = 0;145}146}147}148DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786,149sh7786_pci_fixup);150151static int __init phy_wait_for_ack(struct pci_channel *chan)152{153unsigned int timeout = 100;154155while (timeout--) {156if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))157return 0;158159udelay(100);160}161162return -ETIMEDOUT;163}164165static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)166{167unsigned int timeout = 100;168169while (timeout--) {170if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)171return 0;172173udelay(100);174}175176return -ETIMEDOUT;177}178179static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr,180unsigned int lane, unsigned int data)181{182unsigned long phyaddr;183184phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +185((addr & 0xff) << BITS_ADR);186187/* Set write data */188pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);189pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);190191phy_wait_for_ack(chan);192193/* Clear command */194pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);195pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);196197phy_wait_for_ack(chan);198}199200static int __init pcie_clk_init(struct sh7786_pcie_port *port)201{202struct pci_channel *chan = port->hose;203struct clk *clk;204char fclk_name[16];205int ret;206207/*208* First register the fixed clock209*/210ret = clk_register(&fixed_pciexclkp);211if (unlikely(ret != 0))212return ret;213214/*215* Grab the port's function clock, which the PHY clock depends216* on. clock lookups don't help us much at this point, since no217* dev_id is available this early. Lame.218*/219snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index);220221port->fclk = clk_get(NULL, fclk_name);222if (IS_ERR(port->fclk)) {223ret = PTR_ERR(port->fclk);224goto err_fclk;225}226227clk_enable(port->fclk);228229/*230* And now, set up the PHY clock231*/232clk = &port->phy_clk;233234memset(clk, 0, sizeof(struct clk));235236clk->parent = &fixed_pciexclkp;237clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR);238clk->enable_bit = BITS_CKE;239240ret = sh_clk_mstp32_register(clk, 1);241if (unlikely(ret < 0))242goto err_phy;243244return 0;245246err_phy:247clk_disable(port->fclk);248clk_put(port->fclk);249err_fclk:250clk_unregister(&fixed_pciexclkp);251252return ret;253}254255static int __init phy_init(struct sh7786_pcie_port *port)256{257struct pci_channel *chan = port->hose;258unsigned int timeout = 100;259260clk_enable(&port->phy_clk);261262/* Initialize the phy */263phy_write_reg(chan, 0x60, 0xf, 0x004b008b);264phy_write_reg(chan, 0x61, 0xf, 0x00007b41);265phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);266phy_write_reg(chan, 0x65, 0xf, 0x09070907);267phy_write_reg(chan, 0x66, 0xf, 0x00000010);268phy_write_reg(chan, 0x74, 0xf, 0x0007001c);269phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);270phy_write_reg(chan, 0xb0, 0xf, 0x00000610);271272/* Deassert Standby */273phy_write_reg(chan, 0x67, 0x1, 0x00000400);274275/* Disable clock */276clk_disable(&port->phy_clk);277278while (timeout--) {279if (pci_read_reg(chan, SH4A_PCIEPHYSR))280return 0;281282udelay(100);283}284285return -ETIMEDOUT;286}287288static void __init pcie_reset(struct sh7786_pcie_port *port)289{290struct pci_channel *chan = port->hose;291292pci_write_reg(chan, 1, SH4A_PCIESRSTR);293pci_write_reg(chan, 0, SH4A_PCIETCTLR);294pci_write_reg(chan, 0, SH4A_PCIESRSTR);295pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);296}297298static int __init pcie_init(struct sh7786_pcie_port *port)299{300struct pci_channel *chan = port->hose;301unsigned int data;302phys_addr_t memphys;303size_t memsize;304int ret, i, win;305306/* Begin initialization */307pcie_reset(port);308309/*310* Initial header for port config space is type 1, set the device311* class to match. Hardware takes care of propagating the IDSETR312* settings, so there is no need to bother with a quirk.313*/314pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI << 16, SH4A_PCIEIDSETR1);315316/* Initialize default capabilities. */317data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);318data &= ~(PCI_EXP_FLAGS_TYPE << 16);319320if (port->endpoint)321data |= PCI_EXP_TYPE_ENDPOINT << 20;322else323data |= PCI_EXP_TYPE_ROOT_PORT << 20;324325data |= PCI_CAP_ID_EXP;326pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);327328/* Enable data link layer active state reporting */329pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);330331/* Enable extended sync and ASPM L0s support */332data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);333data &= ~PCI_EXP_LNKCTL_ASPMC;334data |= PCI_EXP_LNKCTL_ES | 1;335pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);336337/* Write out the physical slot number */338data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);339data &= ~PCI_EXP_SLTCAP_PSN;340data |= (port->index + 1) << 19;341pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);342343/* Set the completion timer timeout to the maximum 32ms. */344data = pci_read_reg(chan, SH4A_PCIETLCTLR);345data &= ~0x3f00;346data |= 0x32 << 8;347pci_write_reg(chan, data, SH4A_PCIETLCTLR);348349/*350* Set fast training sequences to the maximum 255,351* and enable MAC data scrambling.352*/353data = pci_read_reg(chan, SH4A_PCIEMACCTLR);354data &= ~PCIEMACCTLR_SCR_DIS;355data |= (0xff << 16);356pci_write_reg(chan, data, SH4A_PCIEMACCTLR);357358memphys = __pa(memory_start);359memsize = roundup_pow_of_two(memory_end - memory_start);360361/*362* If there's more than 512MB of memory, we need to roll over to363* LAR1/LAMR1.364*/365if (memsize > SZ_512M) {366pci_write_reg(chan, memphys + SZ_512M, SH4A_PCIELAR1);367pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1,368SH4A_PCIELAMR1);369memsize = SZ_512M;370} else {371/*372* Otherwise just zero it out and disable it.373*/374pci_write_reg(chan, 0, SH4A_PCIELAR1);375pci_write_reg(chan, 0, SH4A_PCIELAMR1);376}377378/*379* LAR0/LAMR0 covers up to the first 512MB, which is enough to380* cover all of lowmem on most platforms.381*/382pci_write_reg(chan, memphys, SH4A_PCIELAR0);383pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0);384385/* Finish initialization */386data = pci_read_reg(chan, SH4A_PCIETCTLR);387data |= 0x1;388pci_write_reg(chan, data, SH4A_PCIETCTLR);389390/* Let things settle down a bit.. */391mdelay(100);392393/* Enable DL_Active Interrupt generation */394data = pci_read_reg(chan, SH4A_PCIEDLINTENR);395data |= PCIEDLINTENR_DLL_ACT_ENABLE;396pci_write_reg(chan, data, SH4A_PCIEDLINTENR);397398/* Disable MAC data scrambling. */399data = pci_read_reg(chan, SH4A_PCIEMACCTLR);400data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);401pci_write_reg(chan, data, SH4A_PCIEMACCTLR);402403/*404* This will timeout if we don't have a link, but we permit the405* port to register anyways in order to support hotplug on future406* hardware.407*/408ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);409410data = pci_read_reg(chan, SH4A_PCIEPCICONF1);411data &= ~(PCI_STATUS_DEVSEL_MASK << 16);412data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |413(PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;414pci_write_reg(chan, data, SH4A_PCIEPCICONF1);415416pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);417pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);418419wmb();420421if (ret == 0) {422data = pci_read_reg(chan, SH4A_PCIEMACSR);423printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n",424port->index, (data >> 20) & 0x3f);425} else426printk(KERN_NOTICE "PCI: PCIe#%d link down\n",427port->index);428429for (i = win = 0; i < chan->nr_resources; i++) {430struct resource *res = chan->resources + i;431resource_size_t size;432u32 mask;433434/*435* We can't use the 32-bit mode windows in legacy 29-bit436* mode, so just skip them entirely.437*/438if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())439continue;440441pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));442443/*444* The PAMR mask is calculated in units of 256kB, which445* keeps things pretty simple.446*/447size = resource_size(res);448mask = (roundup_pow_of_two(size) / SZ_256K) - 1;449pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win));450451pci_write_reg(chan, upper_32_bits(res->start),452SH4A_PCIEPARH(win));453pci_write_reg(chan, lower_32_bits(res->start),454SH4A_PCIEPARL(win));455456mask = MASK_PARE;457if (res->flags & IORESOURCE_IO)458mask |= MASK_SPC;459460pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win));461462win++;463}464465return 0;466}467468int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)469{470return 71;471}472473static int __init sh7786_pcie_core_init(void)474{475/* Return the number of ports */476return test_mode_pin(MODE_PIN12) ? 3 : 2;477}478479static void __init sh7786_pcie_init_hw(void *data, async_cookie_t cookie)480{481struct sh7786_pcie_port *port = data;482int ret;483484/*485* Check if we are configured in endpoint or root complex mode,486* this is a fixed pin setting that applies to all PCIe ports.487*/488port->endpoint = test_mode_pin(MODE_PIN11);489490/*491* Setup clocks, needed both for PHY and PCIe registers.492*/493ret = pcie_clk_init(port);494if (unlikely(ret < 0)) {495pr_err("clock initialization failed for port#%d\n",496port->index);497return;498}499500ret = phy_init(port);501if (unlikely(ret < 0)) {502pr_err("phy initialization failed for port#%d\n",503port->index);504return;505}506507ret = pcie_init(port);508if (unlikely(ret < 0)) {509pr_err("core initialization failed for port#%d\n",510port->index);511return;512}513514/* In the interest of preserving device ordering, synchronize */515async_synchronize_cookie(cookie);516517register_pci_controller(port->hose);518}519520static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {521.core_init = sh7786_pcie_core_init,522.port_init_hw = sh7786_pcie_init_hw,523};524525static int __init sh7786_pcie_init(void)526{527struct clk *platclk;528int i;529530printk(KERN_NOTICE "PCI: Starting initialization.\n");531532sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;533534nr_ports = sh7786_pcie_hwops->core_init();535BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));536537if (unlikely(nr_ports == 0))538return -ENODEV;539540sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),541GFP_KERNEL);542if (unlikely(!sh7786_pcie_ports))543return -ENOMEM;544545/*546* Fetch any optional platform clock associated with this block.547*548* This is a rather nasty hack for boards with spec-mocking FPGAs549* that have a secondary set of clocks outside of the on-chip550* ones that need to be accounted for before there is any chance551* of touching the existing MSTP bits or CPG clocks.552*/553platclk = clk_get(NULL, "pcie_plat_clk");554if (IS_ERR(platclk)) {555/* Sane hardware should probably get a WARN_ON.. */556platclk = NULL;557}558559clk_enable(platclk);560561printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);562563for (i = 0; i < nr_ports; i++) {564struct sh7786_pcie_port *port = sh7786_pcie_ports + i;565566port->index = i;567port->hose = sh7786_pci_channels + i;568port->hose->io_map_base = port->hose->resources[0].start;569570async_schedule(sh7786_pcie_hwops->port_init_hw, port);571}572573async_synchronize_full();574575return 0;576}577arch_initcall(sh7786_pcie_init);578579580