Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 2 | /* |
Wasim Khan | 54e44ef | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 3 | * Copyright 2017-2020 NXP |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 4 | * Copyright 2014-2015 Freescale Semiconductor, Inc. |
| 5 | * Layerscape PCIe driver |
Hou Zhiqiang | 09716a7b | 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 | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 15 | #include <fdt_support.h> |
Simon Glass | 243182c | 2017-05-17 08:23:06 -0600 | [diff] [blame] | 16 | #ifdef CONFIG_ARM |
| 17 | #include <asm/arch/clock.h> |
| 18 | #endif |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 19 | #include "pcie_layerscape.h" |
Wasim Khan | 54e44ef | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 20 | #include "pcie_layerscape_fixup_common.h" |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 21 | |
Bharat Bhushan | 36e36be | 2017-03-22 12:06:30 +0530 | [diff] [blame] | 22 | #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2) |
Hou Zhiqiang | 09716a7b | 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 | c106784 | 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 | 8cb089e | 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 | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 64 | { |
| 65 | u32 *prop; |
| 66 | u32 phandle; |
| 67 | int nodeoffset; |
Hou Zhiqiang | 8cd3f48 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 68 | uint svr; |
| 69 | char *compat = NULL; |
Minghuan Lian | c106784 | 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 | d553bf2 | 2016-12-13 14:54:24 +0800 | [diff] [blame] | 75 | #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */ |
Hou Zhiqiang | 8cd3f48 | 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 | 2b36178 | 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 | 8cd3f48 | 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 | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 87 | if (nodeoffset < 0) |
| 88 | return; |
Minghuan Lian | c106784 | 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 | 50514b9 | 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 | 8cb089e | 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 | 50514b9 | 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 | 42aea35 | 2017-08-31 13:26:46 +0530 | [diff] [blame] | 122 | uint svr; |
| 123 | char *compat = NULL; |
Bharat Bhushan | 50514b9 | 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 | 42aea35 | 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 | 50514b9 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 142 | if (nodeoffset < 0) |
| 143 | return; |
Bharat Bhushan | 50514b9 | 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 | 8cb089e | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 168 | static void fdt_fixup_pcie_ls(void *blob) |
Minghuan Lian | c106784 | 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 | 9d3d230 | 2020-01-06 12:05:59 +0000 | [diff] [blame^] | 184 | streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx); |
Minghuan Lian | c106784 | 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 | 9d3d230 | 2020-01-06 12:05:59 +0000 | [diff] [blame^] | 188 | } else { |
| 189 | pcie->stream_id_cur++; |
Minghuan Lian | c106784 | 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 | 8cb089e | 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 | 50514b9 | 2017-03-22 12:12:33 +0530 | [diff] [blame] | 206 | /* update iommu-map in device tree */ |
Wasim Khan | 8cb089e | 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 | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | #endif |
| 212 | |
Xiaowei Bao | 8d7e2e8 | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 213 | static void ft_pcie_rc_fix(void *blob, struct ls_pcie *pcie) |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 214 | { |
| 215 | int off; |
Hou Zhiqiang | 8cd3f48 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 216 | uint svr; |
| 217 | char *compat = NULL; |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 218 | |
| 219 | off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie", |
| 220 | pcie->dbi_res.start); |
| 221 | if (off < 0) { |
Hou Zhiqiang | d553bf2 | 2016-12-13 14:54:24 +0800 | [diff] [blame] | 222 | #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */ |
Hou Zhiqiang | 8cd3f48 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 223 | svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE; |
| 224 | if (svr == SVR_LS2088A || svr == SVR_LS2084A || |
Priyanka Jain | 2b36178 | 2017-04-27 15:08:06 +0530 | [diff] [blame] | 225 | svr == SVR_LS2048A || svr == SVR_LS2044A || |
| 226 | svr == SVR_LS2081A || svr == SVR_LS2041A) |
Hou Zhiqiang | 8cd3f48 | 2017-03-03 12:35:10 +0800 | [diff] [blame] | 227 | compat = "fsl,ls2088a-pcie"; |
| 228 | else |
| 229 | compat = CONFIG_FSL_PCIE_COMPAT; |
| 230 | if (compat) |
| 231 | off = fdt_node_offset_by_compat_reg(blob, |
| 232 | compat, pcie->dbi_res.start); |
| 233 | #endif |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 234 | if (off < 0) |
| 235 | return; |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Xiaowei Bao | 8d7e2e8 | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 238 | if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE) |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 239 | fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0); |
| 240 | else |
| 241 | fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0); |
| 242 | } |
| 243 | |
Xiaowei Bao | 8d7e2e8 | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 244 | static void ft_pcie_ep_fix(void *blob, struct ls_pcie *pcie) |
| 245 | { |
| 246 | int off; |
| 247 | |
Pankaj Bansal | 64d85a2 | 2019-11-30 13:14:10 +0000 | [diff] [blame] | 248 | off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT, |
Xiaowei Bao | 8d7e2e8 | 2018-10-26 09:56:26 +0800 | [diff] [blame] | 249 | pcie->dbi_res.start); |
| 250 | if (off < 0) |
| 251 | return; |
| 252 | |
| 253 | if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL) |
| 254 | fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0); |
| 255 | else |
| 256 | fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0); |
| 257 | } |
| 258 | |
| 259 | static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie) |
| 260 | { |
| 261 | ft_pcie_ep_fix(blob, pcie); |
| 262 | ft_pcie_rc_fix(blob, pcie); |
| 263 | } |
| 264 | |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 265 | /* Fixup Kernel DT for PCIe */ |
Wasim Khan | 54e44ef | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 266 | void ft_pci_setup_ls(void *blob, bd_t *bd) |
Minghuan Lian | c106784 | 2016-12-13 14:54:17 +0800 | [diff] [blame] | 267 | { |
| 268 | struct ls_pcie *pcie; |
| 269 | |
| 270 | list_for_each_entry(pcie, &ls_pcie_list, list) |
| 271 | ft_pcie_ls_setup(blob, pcie); |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 272 | |
Bharat Bhushan | 36e36be | 2017-03-22 12:06:30 +0530 | [diff] [blame] | 273 | #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2) |
Wasim Khan | 8cb089e | 2019-11-15 09:23:35 +0000 | [diff] [blame] | 274 | fdt_fixup_pcie_ls(blob); |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 275 | #endif |
| 276 | } |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 277 | |
| 278 | #else /* !CONFIG_OF_BOARD_SETUP */ |
Wasim Khan | 54e44ef | 2020-01-06 12:05:57 +0000 | [diff] [blame] | 279 | void ft_pci_setup_ls(void *blob, bd_t *bd) |
Hou Zhiqiang | 09716a7b | 2016-12-13 14:54:16 +0800 | [diff] [blame] | 280 | { |
| 281 | } |
| 282 | #endif |