Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 2 | /** |
Bjorn Helgaas | 96291d5 | 2017-09-01 16:35:50 -0500 | [diff] [blame] | 3 | * Synopsys DesignWare PCIe Endpoint controller driver |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2017 Texas Instruments |
| 6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/of.h> |
| 10 | |
| 11 | #include "pcie-designware.h" |
| 12 | #include <linux/pci-epc.h> |
| 13 | #include <linux/pci-epf.h> |
| 14 | |
| 15 | void dw_pcie_ep_linkup(struct dw_pcie_ep *ep) |
| 16 | { |
| 17 | struct pci_epc *epc = ep->epc; |
| 18 | |
| 19 | pci_epc_linkup(epc); |
| 20 | } |
| 21 | |
Niklas Cassel | 9e71811 | 2017-12-20 00:29:26 +0100 | [diff] [blame] | 22 | void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 23 | { |
| 24 | u32 reg; |
| 25 | |
| 26 | reg = PCI_BASE_ADDRESS_0 + (4 * bar); |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 27 | dw_pcie_dbi_ro_wr_en(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 28 | dw_pcie_writel_dbi2(pci, reg, 0x0); |
| 29 | dw_pcie_writel_dbi(pci, reg, 0x0); |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 30 | dw_pcie_dbi_ro_wr_dis(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 31 | } |
| 32 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 33 | static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 34 | struct pci_epf_header *hdr) |
| 35 | { |
| 36 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 37 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 38 | |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 39 | dw_pcie_dbi_ro_wr_en(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 40 | dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, hdr->vendorid); |
| 41 | dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, hdr->deviceid); |
| 42 | dw_pcie_writeb_dbi(pci, PCI_REVISION_ID, hdr->revid); |
| 43 | dw_pcie_writeb_dbi(pci, PCI_CLASS_PROG, hdr->progif_code); |
| 44 | dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, |
| 45 | hdr->subclass_code | hdr->baseclass_code << 8); |
| 46 | dw_pcie_writeb_dbi(pci, PCI_CACHE_LINE_SIZE, |
| 47 | hdr->cache_line_size); |
| 48 | dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_VENDOR_ID, |
| 49 | hdr->subsys_vendor_id); |
| 50 | dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_ID, hdr->subsys_id); |
| 51 | dw_pcie_writeb_dbi(pci, PCI_INTERRUPT_PIN, |
| 52 | hdr->interrupt_pin); |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 53 | dw_pcie_dbi_ro_wr_dis(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, enum pci_barno bar, |
| 59 | dma_addr_t cpu_addr, |
| 60 | enum dw_pcie_as_type as_type) |
| 61 | { |
| 62 | int ret; |
| 63 | u32 free_win; |
| 64 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 65 | |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 66 | free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 67 | if (free_win >= ep->num_ib_windows) { |
| 68 | dev_err(pci->dev, "no free inbound window\n"); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | ret = dw_pcie_prog_inbound_atu(pci, free_win, bar, cpu_addr, |
| 73 | as_type); |
| 74 | if (ret < 0) { |
| 75 | dev_err(pci->dev, "Failed to program IB window\n"); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | ep->bar_to_atu[bar] = free_win; |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 80 | set_bit(free_win, ep->ib_window_map); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, phys_addr_t phys_addr, |
| 86 | u64 pci_addr, size_t size) |
| 87 | { |
| 88 | u32 free_win; |
| 89 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 90 | |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 91 | free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 92 | if (free_win >= ep->num_ob_windows) { |
| 93 | dev_err(pci->dev, "no free outbound window\n"); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | dw_pcie_prog_outbound_atu(pci, free_win, PCIE_ATU_TYPE_MEM, |
| 98 | phys_addr, pci_addr, size); |
| 99 | |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 100 | set_bit(free_win, ep->ob_window_map); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 101 | ep->outbound_addr[free_win] = phys_addr; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 106 | static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, |
| 107 | enum pci_barno bar) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 108 | { |
| 109 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 110 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 111 | u32 atu_index = ep->bar_to_atu[bar]; |
| 112 | |
| 113 | dw_pcie_ep_reset_bar(pci, bar); |
| 114 | |
| 115 | dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND); |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 116 | clear_bit(atu_index, ep->ib_window_map); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 117 | } |
| 118 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 119 | static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, |
Niklas Cassel | bc4a489 | 2018-03-28 13:50:07 +0200 | [diff] [blame] | 120 | struct pci_epf_bar *epf_bar) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 124 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
Niklas Cassel | bc4a489 | 2018-03-28 13:50:07 +0200 | [diff] [blame] | 125 | enum pci_barno bar = epf_bar->barno; |
| 126 | size_t size = epf_bar->size; |
| 127 | int flags = epf_bar->flags; |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 128 | enum dw_pcie_as_type as_type; |
| 129 | u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar); |
| 130 | |
| 131 | if (!(flags & PCI_BASE_ADDRESS_SPACE)) |
| 132 | as_type = DW_PCIE_AS_MEM; |
| 133 | else |
| 134 | as_type = DW_PCIE_AS_IO; |
| 135 | |
Niklas Cassel | bc4a489 | 2018-03-28 13:50:07 +0200 | [diff] [blame] | 136 | ret = dw_pcie_ep_inbound_atu(ep, bar, epf_bar->phys_addr, as_type); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 137 | if (ret) |
| 138 | return ret; |
| 139 | |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 140 | dw_pcie_dbi_ro_wr_en(pci); |
Niklas Cassel | d28810b | 2018-03-28 13:50:11 +0200 | [diff] [blame^] | 141 | |
| 142 | dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1)); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 143 | dw_pcie_writel_dbi(pci, reg, flags); |
Niklas Cassel | d28810b | 2018-03-28 13:50:11 +0200 | [diff] [blame^] | 144 | |
| 145 | if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 146 | dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1)); |
| 147 | dw_pcie_writel_dbi(pci, reg + 4, 0); |
| 148 | } |
| 149 | |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 150 | dw_pcie_dbi_ro_wr_dis(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr, |
| 156 | u32 *atu_index) |
| 157 | { |
| 158 | u32 index; |
| 159 | |
| 160 | for (index = 0; index < ep->num_ob_windows; index++) { |
| 161 | if (ep->outbound_addr[index] != addr) |
| 162 | continue; |
| 163 | *atu_index = index; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 170 | static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, |
| 171 | phys_addr_t addr) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 172 | { |
| 173 | int ret; |
| 174 | u32 atu_index; |
| 175 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 176 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 177 | |
| 178 | ret = dw_pcie_find_index(ep, addr, &atu_index); |
| 179 | if (ret < 0) |
| 180 | return; |
| 181 | |
| 182 | dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND); |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 183 | clear_bit(atu_index, ep->ob_window_map); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 184 | } |
| 185 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 186 | static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, |
| 187 | phys_addr_t addr, |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 188 | u64 pci_addr, size_t size) |
| 189 | { |
| 190 | int ret; |
| 191 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 192 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 193 | |
| 194 | ret = dw_pcie_ep_outbound_atu(ep, addr, pci_addr, size); |
| 195 | if (ret) { |
| 196 | dev_err(pci->dev, "failed to enable address\n"); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 203 | static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 204 | { |
| 205 | int val; |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 206 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 207 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 208 | |
Kishon Vijay Abraham I | a134a45 | 2017-12-19 15:25:41 +0530 | [diff] [blame] | 209 | val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL); |
| 210 | if (!(val & MSI_CAP_MSI_EN_MASK)) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Kishon Vijay Abraham I | a134a45 | 2017-12-19 15:25:41 +0530 | [diff] [blame] | 213 | val = (val & MSI_CAP_MME_MASK) >> MSI_CAP_MME_SHIFT; |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 214 | return val; |
| 215 | } |
| 216 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 217 | static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 encode_int) |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 218 | { |
| 219 | int val; |
| 220 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 221 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 222 | |
Niklas Cassel | 099a95f | 2017-12-20 00:29:23 +0100 | [diff] [blame] | 223 | val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL); |
| 224 | val &= ~MSI_CAP_MMC_MASK; |
| 225 | val |= (encode_int << MSI_CAP_MMC_SHIFT) & MSI_CAP_MMC_MASK; |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 226 | dw_pcie_dbi_ro_wr_en(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 227 | dw_pcie_writew_dbi(pci, MSI_MESSAGE_CONTROL, val); |
Niklas Cassel | 1cab826 | 2017-12-20 00:29:24 +0100 | [diff] [blame] | 228 | dw_pcie_dbi_ro_wr_dis(pci); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 233 | static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 234 | enum pci_epc_irq_type type, u8 interrupt_num) |
| 235 | { |
| 236 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 237 | |
| 238 | if (!ep->ops->raise_irq) |
| 239 | return -EINVAL; |
| 240 | |
Bjorn Helgaas | 1609336 | 2018-02-01 11:36:07 -0600 | [diff] [blame] | 241 | return ep->ops->raise_irq(ep, func_no, type, interrupt_num); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void dw_pcie_ep_stop(struct pci_epc *epc) |
| 245 | { |
| 246 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 247 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 248 | |
| 249 | if (!pci->ops->stop_link) |
| 250 | return; |
| 251 | |
| 252 | pci->ops->stop_link(pci); |
| 253 | } |
| 254 | |
| 255 | static int dw_pcie_ep_start(struct pci_epc *epc) |
| 256 | { |
| 257 | struct dw_pcie_ep *ep = epc_get_drvdata(epc); |
| 258 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 259 | |
| 260 | if (!pci->ops->start_link) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | return pci->ops->start_link(pci); |
| 264 | } |
| 265 | |
| 266 | static const struct pci_epc_ops epc_ops = { |
| 267 | .write_header = dw_pcie_ep_write_header, |
| 268 | .set_bar = dw_pcie_ep_set_bar, |
| 269 | .clear_bar = dw_pcie_ep_clear_bar, |
| 270 | .map_addr = dw_pcie_ep_map_addr, |
| 271 | .unmap_addr = dw_pcie_ep_unmap_addr, |
| 272 | .set_msi = dw_pcie_ep_set_msi, |
| 273 | .get_msi = dw_pcie_ep_get_msi, |
| 274 | .raise_irq = dw_pcie_ep_raise_irq, |
| 275 | .start = dw_pcie_ep_start, |
| 276 | .stop = dw_pcie_ep_stop, |
| 277 | }; |
| 278 | |
Bjorn Helgaas | 1609336 | 2018-02-01 11:36:07 -0600 | [diff] [blame] | 279 | int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no, |
Niklas Cassel | 6f6d787 | 2017-12-20 00:29:27 +0100 | [diff] [blame] | 280 | u8 interrupt_num) |
| 281 | { |
| 282 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 283 | struct pci_epc *epc = ep->epc; |
| 284 | u16 msg_ctrl, msg_data; |
| 285 | u32 msg_addr_lower, msg_addr_upper; |
| 286 | u64 msg_addr; |
| 287 | bool has_upper; |
| 288 | int ret; |
| 289 | |
| 290 | /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */ |
| 291 | msg_ctrl = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL); |
| 292 | has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT); |
| 293 | msg_addr_lower = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_L32); |
| 294 | if (has_upper) { |
| 295 | msg_addr_upper = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_U32); |
| 296 | msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_64); |
| 297 | } else { |
| 298 | msg_addr_upper = 0; |
| 299 | msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_32); |
| 300 | } |
| 301 | msg_addr = ((u64) msg_addr_upper) << 32 | msg_addr_lower; |
Bjorn Helgaas | 1609336 | 2018-02-01 11:36:07 -0600 | [diff] [blame] | 302 | ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr, |
Niklas Cassel | 6f6d787 | 2017-12-20 00:29:27 +0100 | [diff] [blame] | 303 | epc->mem->page_size); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | |
| 307 | writel(msg_data | (interrupt_num - 1), ep->msi_mem); |
| 308 | |
Bjorn Helgaas | 1609336 | 2018-02-01 11:36:07 -0600 | [diff] [blame] | 309 | dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys); |
Niklas Cassel | 6f6d787 | 2017-12-20 00:29:27 +0100 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 314 | void dw_pcie_ep_exit(struct dw_pcie_ep *ep) |
| 315 | { |
| 316 | struct pci_epc *epc = ep->epc; |
| 317 | |
Niklas Cassel | 2fd0c9d | 2017-12-20 00:29:25 +0100 | [diff] [blame] | 318 | pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem, |
| 319 | epc->mem->page_size); |
| 320 | |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 321 | pci_epc_mem_exit(epc); |
| 322 | } |
| 323 | |
| 324 | int dw_pcie_ep_init(struct dw_pcie_ep *ep) |
| 325 | { |
| 326 | int ret; |
| 327 | void *addr; |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 328 | struct pci_epc *epc; |
| 329 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 330 | struct device *dev = pci->dev; |
| 331 | struct device_node *np = dev->of_node; |
| 332 | |
| 333 | if (!pci->dbi_base || !pci->dbi_base2) { |
| 334 | dev_err(dev, "dbi_base/deb_base2 is not populated\n"); |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows); |
| 339 | if (ret < 0) { |
| 340 | dev_err(dev, "unable to read *num-ib-windows* property\n"); |
| 341 | return ret; |
| 342 | } |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 343 | if (ep->num_ib_windows > MAX_IATU_IN) { |
| 344 | dev_err(dev, "invalid *num-ib-windows*\n"); |
| 345 | return -EINVAL; |
| 346 | } |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 347 | |
| 348 | ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows); |
| 349 | if (ret < 0) { |
| 350 | dev_err(dev, "unable to read *num-ob-windows* property\n"); |
| 351 | return ret; |
| 352 | } |
Niklas Cassel | ad4a5be | 2017-12-14 14:01:44 +0100 | [diff] [blame] | 353 | if (ep->num_ob_windows > MAX_IATU_OUT) { |
| 354 | dev_err(dev, "invalid *num-ob-windows*\n"); |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
| 358 | ep->ib_window_map = devm_kzalloc(dev, sizeof(long) * |
| 359 | BITS_TO_LONGS(ep->num_ib_windows), |
| 360 | GFP_KERNEL); |
| 361 | if (!ep->ib_window_map) |
| 362 | return -ENOMEM; |
| 363 | |
| 364 | ep->ob_window_map = devm_kzalloc(dev, sizeof(long) * |
| 365 | BITS_TO_LONGS(ep->num_ob_windows), |
| 366 | GFP_KERNEL); |
| 367 | if (!ep->ob_window_map) |
| 368 | return -ENOMEM; |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 369 | |
| 370 | addr = devm_kzalloc(dev, sizeof(phys_addr_t) * ep->num_ob_windows, |
| 371 | GFP_KERNEL); |
| 372 | if (!addr) |
| 373 | return -ENOMEM; |
| 374 | ep->outbound_addr = addr; |
| 375 | |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 376 | if (ep->ops->ep_init) |
| 377 | ep->ops->ep_init(ep); |
| 378 | |
| 379 | epc = devm_pci_epc_create(dev, &epc_ops); |
| 380 | if (IS_ERR(epc)) { |
| 381 | dev_err(dev, "failed to create epc device\n"); |
| 382 | return PTR_ERR(epc); |
| 383 | } |
| 384 | |
| 385 | ret = of_property_read_u8(np, "max-functions", &epc->max_functions); |
| 386 | if (ret < 0) |
| 387 | epc->max_functions = 1; |
| 388 | |
Kishon Vijay Abraham I | a937fe0 | 2017-08-18 20:28:02 +0530 | [diff] [blame] | 389 | ret = __pci_epc_mem_init(epc, ep->phys_base, ep->addr_size, |
| 390 | ep->page_size); |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 391 | if (ret < 0) { |
| 392 | dev_err(dev, "Failed to initialize address space\n"); |
| 393 | return ret; |
| 394 | } |
| 395 | |
Niklas Cassel | 2fd0c9d | 2017-12-20 00:29:25 +0100 | [diff] [blame] | 396 | ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys, |
| 397 | epc->mem->page_size); |
| 398 | if (!ep->msi_mem) { |
| 399 | dev_err(dev, "Failed to reserve memory for MSI\n"); |
| 400 | return -ENOMEM; |
| 401 | } |
| 402 | |
Kishon Vijay Abraham I | f8aed6e | 2017-03-27 15:15:05 +0530 | [diff] [blame] | 403 | ep->epc = epc; |
| 404 | epc_set_drvdata(epc, ep); |
| 405 | dw_pcie_setup(pci); |
| 406 | |
| 407 | return 0; |
| 408 | } |