Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 1 | /* |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 2 | * Synopsys Designware PCIe host controller driver |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * Author: Jingoo Han <jg1.han@samsung.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/types.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 17 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 18 | #include "pcie-designware.h" |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 19 | |
Joao Pinto | dac29e6 | 2016-03-10 14:44:44 -0600 | [diff] [blame] | 20 | /* PCIe Port Logic registers */ |
| 21 | #define PLR_OFFSET 0x700 |
| 22 | #define PCIE_PHY_DEBUG_R1 (PLR_OFFSET + 0x2c) |
Jisheng Zhang | 01c0767 | 2016-08-17 15:57:37 -0500 | [diff] [blame] | 23 | #define PCIE_PHY_DEBUG_R1_LINK_UP (0x1 << 4) |
| 24 | #define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (0x1 << 29) |
Joao Pinto | dac29e6 | 2016-03-10 14:44:44 -0600 | [diff] [blame] | 25 | |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 26 | int dw_pcie_read(void __iomem *addr, int size, u32 *val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 27 | { |
Gabriele Paoloni | b6b18f5 | 2015-10-08 14:27:53 -0500 | [diff] [blame] | 28 | if ((uintptr_t)addr & (size - 1)) { |
| 29 | *val = 0; |
| 30 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 31 | } |
| 32 | |
Kishon Vijay Abraham I | 314fc85 | 2017-02-15 18:48:16 +0530 | [diff] [blame] | 33 | if (size == 4) { |
Gabriele Paoloni | c003ca9 | 2015-10-08 14:27:43 -0500 | [diff] [blame] | 34 | *val = readl(addr); |
Kishon Vijay Abraham I | 314fc85 | 2017-02-15 18:48:16 +0530 | [diff] [blame] | 35 | } else if (size == 2) { |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 36 | *val = readw(addr); |
Kishon Vijay Abraham I | 314fc85 | 2017-02-15 18:48:16 +0530 | [diff] [blame] | 37 | } else if (size == 1) { |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 38 | *val = readb(addr); |
Kishon Vijay Abraham I | 314fc85 | 2017-02-15 18:48:16 +0530 | [diff] [blame] | 39 | } else { |
Gabriele Paoloni | c003ca9 | 2015-10-08 14:27:43 -0500 | [diff] [blame] | 40 | *val = 0; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 41 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Gabriele Paoloni | c003ca9 | 2015-10-08 14:27:43 -0500 | [diff] [blame] | 42 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 43 | |
| 44 | return PCIBIOS_SUCCESSFUL; |
| 45 | } |
| 46 | |
Kishon Vijay Abraham I | 19ce01cc | 2017-02-15 18:48:12 +0530 | [diff] [blame] | 47 | int dw_pcie_write(void __iomem *addr, int size, u32 val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 48 | { |
Gabriele Paoloni | b6b18f5 | 2015-10-08 14:27:53 -0500 | [diff] [blame] | 49 | if ((uintptr_t)addr & (size - 1)) |
| 50 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 51 | |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 52 | if (size == 4) |
| 53 | writel(val, addr); |
| 54 | else if (size == 2) |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 55 | writew(val, addr); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 56 | else if (size == 1) |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 57 | writeb(val, addr); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 58 | else |
| 59 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 60 | |
| 61 | return PCIBIOS_SUCCESSFUL; |
| 62 | } |
| 63 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 64 | u32 dw_pcie_readl_dbi(struct dw_pcie *pci, u32 reg) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 65 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 66 | if (pci->ops->readl_dbi) |
| 67 | return pci->ops->readl_dbi(pci, reg); |
Bjorn Helgaas | 446fc23 | 2016-08-17 14:17:58 -0500 | [diff] [blame] | 68 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 69 | return readl(pci->dbi_base + reg); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 72 | void dw_pcie_writel_dbi(struct dw_pcie *pci, u32 reg, u32 val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 73 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 74 | if (pci->ops->writel_dbi) |
| 75 | pci->ops->writel_dbi(pci, reg, val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 76 | else |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 77 | writel(val, pci->dbi_base + reg); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 80 | static u32 dw_pcie_readl_unroll(struct dw_pcie *pci, u32 index, u32 reg) |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 81 | { |
| 82 | u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index); |
| 83 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 84 | return dw_pcie_readl_dbi(pci, offset + reg); |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 87 | static void dw_pcie_writel_unroll(struct dw_pcie *pci, u32 index, u32 reg, |
Bjorn Helgaas | f5acb5c | 2016-10-11 08:33:00 -0500 | [diff] [blame] | 88 | u32 val) |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 89 | { |
| 90 | u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index); |
| 91 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 92 | dw_pcie_writel_dbi(pci, offset + reg, val); |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 95 | void dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index, int type, |
| 96 | u64 cpu_addr, u64 pci_addr, u32 size) |
Jisheng Zhang | 63503c8 | 2015-04-30 16:22:28 +0800 | [diff] [blame] | 97 | { |
Joao Pinto | d8bbeb3 | 2016-08-17 13:26:07 -0500 | [diff] [blame] | 98 | u32 retries, val; |
Stanimir Varbanov | 17209df | 2015-12-18 14:38:55 +0200 | [diff] [blame] | 99 | |
Kishon Vijay Abraham I | a660083 | 2017-03-13 19:13:22 +0530 | [diff] [blame^] | 100 | if (pci->ops->cpu_addr_fixup) |
| 101 | cpu_addr = pci->ops->cpu_addr_fixup(cpu_addr); |
| 102 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 103 | if (pci->iatu_unroll_enabled) { |
| 104 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE, |
| 105 | lower_32_bits(cpu_addr)); |
| 106 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE, |
| 107 | upper_32_bits(cpu_addr)); |
| 108 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_LIMIT, |
| 109 | lower_32_bits(cpu_addr + size - 1)); |
| 110 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET, |
| 111 | lower_32_bits(pci_addr)); |
| 112 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET, |
| 113 | upper_32_bits(pci_addr)); |
| 114 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, |
| 115 | type); |
| 116 | dw_pcie_writel_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2, |
| 117 | PCIE_ATU_ENABLE); |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 118 | } else { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 119 | dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, |
| 120 | PCIE_ATU_REGION_OUTBOUND | index); |
| 121 | dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_BASE, |
| 122 | lower_32_bits(cpu_addr)); |
| 123 | dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_BASE, |
| 124 | upper_32_bits(cpu_addr)); |
| 125 | dw_pcie_writel_dbi(pci, PCIE_ATU_LIMIT, |
| 126 | lower_32_bits(cpu_addr + size - 1)); |
| 127 | dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, |
| 128 | lower_32_bits(pci_addr)); |
| 129 | dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET, |
| 130 | upper_32_bits(pci_addr)); |
| 131 | dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type); |
| 132 | dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE); |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 133 | } |
Stanimir Varbanov | 17209df | 2015-12-18 14:38:55 +0200 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Make sure ATU enable takes effect before any subsequent config |
| 137 | * and I/O accesses. |
| 138 | */ |
Joao Pinto | d8bbeb3 | 2016-08-17 13:26:07 -0500 | [diff] [blame] | 139 | for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 140 | if (pci->iatu_unroll_enabled) |
| 141 | val = dw_pcie_readl_unroll(pci, index, |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 142 | PCIE_ATU_UNR_REGION_CTRL2); |
| 143 | else |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 144 | val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2); |
Joao Pinto | a0601a4 | 2016-08-10 11:02:39 +0100 | [diff] [blame] | 145 | |
Joao Pinto | d8bbeb3 | 2016-08-17 13:26:07 -0500 | [diff] [blame] | 146 | if (val == PCIE_ATU_ENABLE) |
| 147 | return; |
| 148 | |
| 149 | usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX); |
| 150 | } |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 151 | dev_err(pci->dev, "iATU is not being enabled\n"); |
Jisheng Zhang | 63503c8 | 2015-04-30 16:22:28 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 154 | int dw_pcie_wait_for_link(struct dw_pcie *pci) |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 155 | { |
| 156 | int retries; |
| 157 | |
| 158 | /* check if the link is up or not */ |
| 159 | for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 160 | if (dw_pcie_link_up(pci)) { |
| 161 | dev_info(pci->dev, "link up\n"); |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX); |
| 165 | } |
| 166 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 167 | dev_err(pci->dev, "phy link never came up\n"); |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 168 | |
| 169 | return -ETIMEDOUT; |
| 170 | } |
| 171 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 172 | int dw_pcie_link_up(struct dw_pcie *pci) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 173 | { |
Joao Pinto | dac29e6 | 2016-03-10 14:44:44 -0600 | [diff] [blame] | 174 | u32 val; |
| 175 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 176 | if (pci->ops->link_up) |
| 177 | return pci->ops->link_up(pci); |
Bjorn Helgaas | 116a489 | 2016-01-05 15:48:11 -0600 | [diff] [blame] | 178 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 179 | val = readl(pci->dbi_base + PCIE_PHY_DEBUG_R1); |
Jisheng Zhang | 01c0767 | 2016-08-17 15:57:37 -0500 | [diff] [blame] | 180 | return ((val & PCIE_PHY_DEBUG_R1_LINK_UP) && |
| 181 | (!(val & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 182 | } |
| 183 | |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 184 | void dw_pcie_setup(struct dw_pcie *pci) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 185 | { |
Kishon Vijay Abraham I | 5f334db | 2017-02-15 18:48:15 +0530 | [diff] [blame] | 186 | int ret; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 187 | u32 val; |
Kishon Vijay Abraham I | feb85d9 | 2017-02-15 18:48:17 +0530 | [diff] [blame] | 188 | u32 lanes; |
Kishon Vijay Abraham I | 5f334db | 2017-02-15 18:48:15 +0530 | [diff] [blame] | 189 | struct device *dev = pci->dev; |
| 190 | struct device_node *np = dev->of_node; |
| 191 | |
| 192 | ret = of_property_read_u32(np, "num-lanes", &lanes); |
| 193 | if (ret) |
| 194 | lanes = 0; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 195 | |
Mohit Kumar | 66c5c34 | 2014-04-14 14:22:54 -0600 | [diff] [blame] | 196 | /* set the number of lanes */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 197 | val = dw_pcie_readl_dbi(pci, PCIE_PORT_LINK_CONTROL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 198 | val &= ~PORT_LINK_MODE_MASK; |
Kishon Vijay Abraham I | 5f334db | 2017-02-15 18:48:15 +0530 | [diff] [blame] | 199 | switch (lanes) { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 200 | case 1: |
| 201 | val |= PORT_LINK_MODE_1_LANES; |
| 202 | break; |
| 203 | case 2: |
| 204 | val |= PORT_LINK_MODE_2_LANES; |
| 205 | break; |
| 206 | case 4: |
| 207 | val |= PORT_LINK_MODE_4_LANES; |
| 208 | break; |
Zhou Wang | 5b0f073 | 2015-05-13 14:44:34 +0800 | [diff] [blame] | 209 | case 8: |
| 210 | val |= PORT_LINK_MODE_8_LANES; |
| 211 | break; |
Gabriele Paoloni | 907fce0 | 2015-09-29 00:03:10 +0800 | [diff] [blame] | 212 | default: |
Kishon Vijay Abraham I | 5f334db | 2017-02-15 18:48:15 +0530 | [diff] [blame] | 213 | dev_err(pci->dev, "num-lanes %u: invalid value\n", lanes); |
Gabriele Paoloni | 907fce0 | 2015-09-29 00:03:10 +0800 | [diff] [blame] | 214 | return; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 215 | } |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 216 | dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 217 | |
| 218 | /* set link width speed control register */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 219 | val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 220 | val &= ~PORT_LOGIC_LINK_WIDTH_MASK; |
Kishon Vijay Abraham I | 5f334db | 2017-02-15 18:48:15 +0530 | [diff] [blame] | 221 | switch (lanes) { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 222 | case 1: |
| 223 | val |= PORT_LOGIC_LINK_WIDTH_1_LANES; |
| 224 | break; |
| 225 | case 2: |
| 226 | val |= PORT_LOGIC_LINK_WIDTH_2_LANES; |
| 227 | break; |
| 228 | case 4: |
| 229 | val |= PORT_LOGIC_LINK_WIDTH_4_LANES; |
| 230 | break; |
Zhou Wang | 5b0f073 | 2015-05-13 14:44:34 +0800 | [diff] [blame] | 231 | case 8: |
| 232 | val |= PORT_LOGIC_LINK_WIDTH_8_LANES; |
| 233 | break; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 234 | } |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 235 | dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 236 | } |