Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 2 | /* |
Wasim Khan | 1185b22 | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 3 | * Copyright 2017-2020 NXP |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 4 | * Copyright 2014-2015 Freescale Semiconductor, Inc. |
| 5 | * Layerscape PCIe driver |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <pci.h> |
| 10 | #include <asm/arch/fsl_serdes.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <errno.h> |
| 13 | #ifdef CONFIG_OF_BOARD_SETUP |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 15 | #include <fdt_support.h> |
Simon Glass | 6e2941d | 2017-05-17 08:23:06 -0600 | [diff] [blame] | 16 | #ifdef CONFIG_ARM |
| 17 | #include <asm/arch/clock.h> |
| 18 | #endif |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 19 | #include "pcie_layerscape.h" |
Wasim Khan | 1185b22 | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 20 | #include "pcie_layerscape_fixup_common.h" |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 21 | |
Bharat Bhushan | 47d1736 | 2017-03-22 12:06:30 +0530 | [diff] [blame] | 22 | #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2) |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 23 | /* |
| 24 | * Return next available LUT index. |
| 25 | */ |
| 26 | static int ls_pcie_next_lut_index(struct ls_pcie *pcie) |
| 27 | { |
| 28 | if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT) |
| 29 | return pcie->next_lut_index++; |
| 30 | else |
| 31 | return -ENOSPC; /* LUT is full */ |
| 32 | } |
| 33 | |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 34 | static void lut_writel(struct ls_pcie *pcie, unsigned int value, |
| 35 | unsigned int offset) |
| 36 | { |
| 37 | if (pcie->big_endian) |
| 38 | out_be32(pcie->lut + offset, value); |
| 39 | else |
| 40 | out_le32(pcie->lut + offset, value); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Program a single LUT entry |
| 45 | */ |
| 46 | static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid, |
| 47 | u32 streamid) |
| 48 | { |
| 49 | /* leave mask as all zeroes, want to match all bits */ |
| 50 | lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index)); |
| 51 | lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index)); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * An msi-map is a property to be added to the pci controller |
| 56 | * node. It is a table, where each entry consists of 4 fields |
| 57 | * e.g.: |
| 58 | * |
| 59 | * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count] |
| 60 | * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>; |
| 61 | */ |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 62 | static void fdt_pcie_set_msi_map_entry_ls(void *blob, struct ls_pcie *pcie, |
| 63 | u32 devid, u32 streamid) |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 64 | { |
| 65 | u32 *prop; |
| 66 | u32 phandle; |
| 67 | int nodeoffset; |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 68 | uint svr; |
| 69 | char *compat = NULL; |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 70 | |
| 71 | /* find pci controller node */ |
| 72 | nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie", |
| 73 | pcie->dbi_res.start); |
| 74 | if (nodeoffset < 0) { |
Hou Zhiqiang | 19538f3 | 2016-12-13 14:54:24 +0800 | [diff] [blame] | 75 | #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */ |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 76 | svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 77 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
Priyanka Jain | e809e74 | 2017-04-27 15:08:06 +0530 | [diff] [blame] | 78 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 79 | svr == SVR_LS2081A || svr == SVR_LS2041A) |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 80 | compat = "fsl,ls2088a-pcie"; |
| 81 | else |
| 82 | compat = CONFIG_FSL_PCIE_COMPAT; |
| 83 | if (compat) |
| 84 | nodeoffset = fdt_node_offset_by_compat_reg(blob, |
| 85 | compat, pcie->dbi_res.start); |
| 86 | #endif |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 87 | if (nodeoffset < 0) |
| 88 | return; |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* get phandle to MSI controller */ |
| 92 | prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0); |
| 93 | if (prop == NULL) { |
| 94 | debug("\n%s: ERROR: missing msi-parent: PCIe%d\n", |
| 95 | __func__, pcie->idx); |
| 96 | return; |
| 97 | } |
| 98 | phandle = fdt32_to_cpu(*prop); |
| 99 | |
| 100 | /* set one msi-map row */ |
| 101 | fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid); |
| 102 | fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle); |
| 103 | fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid); |
| 104 | fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1); |
| 105 | } |
| 106 | |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 107 | /* |
| 108 | * An iommu-map is a property to be added to the pci controller |
| 109 | * node. It is a table, where each entry consists of 4 fields |
| 110 | * e.g.: |
| 111 | * |
| 112 | * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count] |
| 113 | * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>; |
| 114 | */ |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 115 | static void fdt_pcie_set_iommu_map_entry_ls(void *blob, struct ls_pcie *pcie, |
| 116 | u32 devid, u32 streamid) |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 117 | { |
| 118 | u32 *prop; |
| 119 | u32 iommu_map[4]; |
| 120 | int nodeoffset; |
| 121 | int lenp; |
Bharat Bhushan | 4b97a82 | 2017-08-31 13:26:46 +0530 | [diff] [blame] | 122 | uint svr; |
| 123 | char *compat = NULL; |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 124 | |
| 125 | /* find pci controller node */ |
| 126 | nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie", |
| 127 | pcie->dbi_res.start); |
| 128 | if (nodeoffset < 0) { |
| 129 | #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */ |
Bharat Bhushan | 4b97a82 | 2017-08-31 13:26:46 +0530 | [diff] [blame] | 130 | svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 131 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
| 132 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 133 | svr == SVR_LS2081A || svr == SVR_LS2041A) |
| 134 | compat = "fsl,ls2088a-pcie"; |
| 135 | else |
| 136 | compat = CONFIG_FSL_PCIE_COMPAT; |
| 137 | |
| 138 | if (compat) |
| 139 | nodeoffset = fdt_node_offset_by_compat_reg(blob, |
| 140 | compat, pcie->dbi_res.start); |
| 141 | #endif |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 142 | if (nodeoffset < 0) |
| 143 | return; |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* get phandle to iommu controller */ |
| 147 | prop = fdt_getprop_w(blob, nodeoffset, "iommu-map", &lenp); |
| 148 | if (prop == NULL) { |
| 149 | debug("\n%s: ERROR: missing iommu-map: PCIe%d\n", |
| 150 | __func__, pcie->idx); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | /* set iommu-map row */ |
| 155 | iommu_map[0] = cpu_to_fdt32(devid); |
| 156 | iommu_map[1] = *++prop; |
| 157 | iommu_map[2] = cpu_to_fdt32(streamid); |
| 158 | iommu_map[3] = cpu_to_fdt32(1); |
| 159 | |
| 160 | if (devid == 0) { |
| 161 | fdt_setprop_inplace(blob, nodeoffset, "iommu-map", |
| 162 | iommu_map, 16); |
| 163 | } else { |
| 164 | fdt_appendprop(blob, nodeoffset, "iommu-map", iommu_map, 16); |
| 165 | } |
| 166 | } |
| 167 | |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 168 | static void fdt_fixup_pcie_ls(void *blob) |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 169 | { |
| 170 | struct udevice *dev, *bus; |
| 171 | struct ls_pcie *pcie; |
| 172 | int streamid; |
| 173 | int index; |
| 174 | pci_dev_t bdf; |
| 175 | |
| 176 | /* Scan all known buses */ |
| 177 | for (pci_find_first_device(&dev); |
| 178 | dev; |
| 179 | pci_find_next_device(&dev)) { |
| 180 | for (bus = dev; device_is_on_pci_bus(bus);) |
| 181 | bus = bus->parent; |
| 182 | pcie = dev_get_priv(bus); |
| 183 | |
Wasim Khan | d20eb7a | 2020-01-06 12:05:59 +0000 | [diff] [blame] | 184 | streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx); |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 185 | if (streamid < 0) { |
| 186 | debug("ERROR: no stream ids free\n"); |
| 187 | continue; |
Wasim Khan | d20eb7a | 2020-01-06 12:05:59 +0000 | [diff] [blame] | 188 | } else { |
| 189 | pcie->stream_id_cur++; |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | index = ls_pcie_next_lut_index(pcie); |
| 193 | if (index < 0) { |
| 194 | debug("ERROR: no LUT indexes free\n"); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | /* the DT fixup must be relative to the hose first_busno */ |
| 199 | bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0); |
| 200 | /* map PCI b.d.f to streamID in LUT */ |
| 201 | ls_pcie_lut_set_mapping(pcie, index, bdf >> 8, |
| 202 | streamid); |
| 203 | /* update msi-map in device tree */ |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 204 | fdt_pcie_set_msi_map_entry_ls(blob, pcie, bdf >> 8, |
| 205 | streamid); |
Bharat Bhushan | 78be622 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 206 | /* update iommu-map in device tree */ |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 207 | fdt_pcie_set_iommu_map_entry_ls(blob, pcie, bdf >> 8, |
| 208 | streamid); |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 209 | } |
Wasim Khan | 9c2969e | 2020-01-06 12:06:00 +0000 | [diff] [blame^] | 210 | pcie_board_fix_fdt(blob); |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 211 | } |
| 212 | #endif |
| 213 | |
Xiaowei Bao | 59a557f | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 214 | static void ft_pcie_rc_fix(void *blob, struct ls_pcie *pcie) |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 215 | { |
| 216 | int off; |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 217 | uint svr; |
| 218 | char *compat = NULL; |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 219 | |
| 220 | off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie", |
| 221 | pcie->dbi_res.start); |
| 222 | if (off < 0) { |
Hou Zhiqiang | 19538f3 | 2016-12-13 14:54:24 +0800 | [diff] [blame] | 223 | #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */ |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 224 | svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 225 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
Priyanka Jain | e809e74 | 2017-04-27 15:08:06 +0530 | [diff] [blame] | 226 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 227 | svr == SVR_LS2081A || svr == SVR_LS2041A) |
Hou Zhiqiang | 0aaa1a9 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 228 | compat = "fsl,ls2088a-pcie"; |
| 229 | else |
| 230 | compat = CONFIG_FSL_PCIE_COMPAT; |
| 231 | if (compat) |
| 232 | off = fdt_node_offset_by_compat_reg(blob, |
| 233 | compat, pcie->dbi_res.start); |
| 234 | #endif |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 235 | if (off < 0) |
| 236 | return; |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Xiaowei Bao | 59a557f | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 239 | if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE) |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 240 | fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0); |
| 241 | else |
| 242 | fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0); |
| 243 | } |
| 244 | |
Xiaowei Bao | 59a557f | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 245 | static void ft_pcie_ep_fix(void *blob, struct ls_pcie *pcie) |
| 246 | { |
| 247 | int off; |
| 248 | |
Pankaj Bansal | 63618e7 | 2019-11-30 13:14:10 +0000 | [diff] [blame] | 249 | off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT, |
Xiaowei Bao | 59a557f | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 250 | pcie->dbi_res.start); |
| 251 | if (off < 0) |
| 252 | return; |
| 253 | |
| 254 | if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL) |
| 255 | fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0); |
| 256 | else |
| 257 | fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0); |
| 258 | } |
| 259 | |
| 260 | static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie) |
| 261 | { |
| 262 | ft_pcie_ep_fix(blob, pcie); |
| 263 | ft_pcie_rc_fix(blob, pcie); |
| 264 | } |
| 265 | |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 266 | /* Fixup Kernel DT for PCIe */ |
Wasim Khan | 1185b22 | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 267 | void ft_pci_setup_ls(void *blob, bd_t *bd) |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 268 | { |
| 269 | struct ls_pcie *pcie; |
| 270 | |
| 271 | list_for_each_entry(pcie, &ls_pcie_list, list) |
| 272 | ft_pcie_ls_setup(blob, pcie); |
| 273 | |
Bharat Bhushan | 47d1736 | 2017-03-22 12:06:30 +0530 | [diff] [blame] | 274 | #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2) |
Wasim Khan | 485304a | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 275 | fdt_fixup_pcie_ls(blob); |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 276 | #endif |
| 277 | } |
Minghuan Lian | 80afc63 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 278 | |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 279 | #else /* !CONFIG_OF_BOARD_SETUP */ |
Wasim Khan | 1185b22 | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 280 | void ft_pci_setup_ls(void *blob, bd_t *bd) |
Hou Zhiqiang | a7294ab | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 281 | { |
| 282 | } |
| 283 | #endif |