1 // SPDX-License-Identifier: GPL-2.0
3 * Synopsys DesignWare PCIe Endpoint controller driver
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
11 #include "pcie-designware.h"
12 #include <linux/pci-epc.h>
13 #include <linux/pci-epf.h>
15 void dw_pcie_ep_linkup(struct dw_pcie_ep
*ep
)
17 struct pci_epc
*epc
= ep
->epc
;
22 void dw_pcie_ep_reset_bar(struct dw_pcie
*pci
, enum pci_barno bar
)
26 reg
= PCI_BASE_ADDRESS_0
+ (4 * bar
);
27 dw_pcie_dbi_ro_wr_en(pci
);
28 dw_pcie_writel_dbi2(pci
, reg
, 0x0);
29 dw_pcie_writel_dbi(pci
, reg
, 0x0);
30 dw_pcie_dbi_ro_wr_dis(pci
);
33 static int dw_pcie_ep_write_header(struct pci_epc
*epc
, u8 func_no
,
34 struct pci_epf_header
*hdr
)
36 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
37 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
39 dw_pcie_dbi_ro_wr_en(pci
);
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
,
53 dw_pcie_dbi_ro_wr_dis(pci
);
58 static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep
*ep
, enum pci_barno bar
,
60 enum dw_pcie_as_type as_type
)
64 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
66 free_win
= find_first_zero_bit(ep
->ib_window_map
, ep
->num_ib_windows
);
67 if (free_win
>= ep
->num_ib_windows
) {
68 dev_err(pci
->dev
, "no free inbound window\n");
72 ret
= dw_pcie_prog_inbound_atu(pci
, free_win
, bar
, cpu_addr
,
75 dev_err(pci
->dev
, "Failed to program IB window\n");
79 ep
->bar_to_atu
[bar
] = free_win
;
80 set_bit(free_win
, ep
->ib_window_map
);
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
)
89 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
91 free_win
= find_first_zero_bit(ep
->ob_window_map
, ep
->num_ob_windows
);
92 if (free_win
>= ep
->num_ob_windows
) {
93 dev_err(pci
->dev
, "no free outbound window\n");
97 dw_pcie_prog_outbound_atu(pci
, free_win
, PCIE_ATU_TYPE_MEM
,
98 phys_addr
, pci_addr
, size
);
100 set_bit(free_win
, ep
->ob_window_map
);
101 ep
->outbound_addr
[free_win
] = phys_addr
;
106 static void dw_pcie_ep_clear_bar(struct pci_epc
*epc
, u8 func_no
,
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
];
113 dw_pcie_ep_reset_bar(pci
, bar
);
115 dw_pcie_disable_atu(pci
, atu_index
, DW_PCIE_REGION_INBOUND
);
116 clear_bit(atu_index
, ep
->ib_window_map
);
119 static int dw_pcie_ep_set_bar(struct pci_epc
*epc
, u8 func_no
,
121 dma_addr_t bar_phys
, size_t size
, int flags
)
124 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
125 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
126 enum dw_pcie_as_type as_type
;
127 u32 reg
= PCI_BASE_ADDRESS_0
+ (4 * bar
);
129 if (!(flags
& PCI_BASE_ADDRESS_SPACE
))
130 as_type
= DW_PCIE_AS_MEM
;
132 as_type
= DW_PCIE_AS_IO
;
134 ret
= dw_pcie_ep_inbound_atu(ep
, bar
, bar_phys
, as_type
);
138 dw_pcie_dbi_ro_wr_en(pci
);
139 dw_pcie_writel_dbi2(pci
, reg
, size
- 1);
140 dw_pcie_writel_dbi(pci
, reg
, flags
);
141 dw_pcie_dbi_ro_wr_dis(pci
);
146 static int dw_pcie_find_index(struct dw_pcie_ep
*ep
, phys_addr_t addr
,
151 for (index
= 0; index
< ep
->num_ob_windows
; index
++) {
152 if (ep
->outbound_addr
[index
] != addr
)
161 static void dw_pcie_ep_unmap_addr(struct pci_epc
*epc
, u8 func_no
,
166 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
167 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
169 ret
= dw_pcie_find_index(ep
, addr
, &atu_index
);
173 dw_pcie_disable_atu(pci
, atu_index
, DW_PCIE_REGION_OUTBOUND
);
174 clear_bit(atu_index
, ep
->ob_window_map
);
177 static int dw_pcie_ep_map_addr(struct pci_epc
*epc
, u8 func_no
,
179 u64 pci_addr
, size_t size
)
182 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
183 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
185 ret
= dw_pcie_ep_outbound_atu(ep
, addr
, pci_addr
, size
);
187 dev_err(pci
->dev
, "failed to enable address\n");
194 static int dw_pcie_ep_get_msi(struct pci_epc
*epc
, u8 func_no
)
197 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
198 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
200 val
= dw_pcie_readw_dbi(pci
, MSI_MESSAGE_CONTROL
);
201 if (!(val
& MSI_CAP_MSI_EN_MASK
))
204 val
= (val
& MSI_CAP_MME_MASK
) >> MSI_CAP_MME_SHIFT
;
208 static int dw_pcie_ep_set_msi(struct pci_epc
*epc
, u8 func_no
, u8 encode_int
)
211 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
212 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
214 val
= dw_pcie_readw_dbi(pci
, MSI_MESSAGE_CONTROL
);
215 val
&= ~MSI_CAP_MMC_MASK
;
216 val
|= (encode_int
<< MSI_CAP_MMC_SHIFT
) & MSI_CAP_MMC_MASK
;
217 dw_pcie_dbi_ro_wr_en(pci
);
218 dw_pcie_writew_dbi(pci
, MSI_MESSAGE_CONTROL
, val
);
219 dw_pcie_dbi_ro_wr_dis(pci
);
224 static int dw_pcie_ep_raise_irq(struct pci_epc
*epc
, u8 func_no
,
225 enum pci_epc_irq_type type
, u8 interrupt_num
)
227 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
229 if (!ep
->ops
->raise_irq
)
232 return ep
->ops
->raise_irq(ep
, func_no
, type
, interrupt_num
);
235 static void dw_pcie_ep_stop(struct pci_epc
*epc
)
237 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
238 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
240 if (!pci
->ops
->stop_link
)
243 pci
->ops
->stop_link(pci
);
246 static int dw_pcie_ep_start(struct pci_epc
*epc
)
248 struct dw_pcie_ep
*ep
= epc_get_drvdata(epc
);
249 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
251 if (!pci
->ops
->start_link
)
254 return pci
->ops
->start_link(pci
);
257 static const struct pci_epc_ops epc_ops
= {
258 .write_header
= dw_pcie_ep_write_header
,
259 .set_bar
= dw_pcie_ep_set_bar
,
260 .clear_bar
= dw_pcie_ep_clear_bar
,
261 .map_addr
= dw_pcie_ep_map_addr
,
262 .unmap_addr
= dw_pcie_ep_unmap_addr
,
263 .set_msi
= dw_pcie_ep_set_msi
,
264 .get_msi
= dw_pcie_ep_get_msi
,
265 .raise_irq
= dw_pcie_ep_raise_irq
,
266 .start
= dw_pcie_ep_start
,
267 .stop
= dw_pcie_ep_stop
,
270 int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep
*ep
, u8 func_no
,
273 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
274 struct pci_epc
*epc
= ep
->epc
;
275 u16 msg_ctrl
, msg_data
;
276 u32 msg_addr_lower
, msg_addr_upper
;
281 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
282 msg_ctrl
= dw_pcie_readw_dbi(pci
, MSI_MESSAGE_CONTROL
);
283 has_upper
= !!(msg_ctrl
& PCI_MSI_FLAGS_64BIT
);
284 msg_addr_lower
= dw_pcie_readl_dbi(pci
, MSI_MESSAGE_ADDR_L32
);
286 msg_addr_upper
= dw_pcie_readl_dbi(pci
, MSI_MESSAGE_ADDR_U32
);
287 msg_data
= dw_pcie_readw_dbi(pci
, MSI_MESSAGE_DATA_64
);
290 msg_data
= dw_pcie_readw_dbi(pci
, MSI_MESSAGE_DATA_32
);
292 msg_addr
= ((u64
) msg_addr_upper
) << 32 | msg_addr_lower
;
293 ret
= dw_pcie_ep_map_addr(epc
, func_no
, ep
->msi_mem_phys
, msg_addr
,
294 epc
->mem
->page_size
);
298 writel(msg_data
| (interrupt_num
- 1), ep
->msi_mem
);
300 dw_pcie_ep_unmap_addr(epc
, func_no
, ep
->msi_mem_phys
);
305 void dw_pcie_ep_exit(struct dw_pcie_ep
*ep
)
307 struct pci_epc
*epc
= ep
->epc
;
309 pci_epc_mem_free_addr(epc
, ep
->msi_mem_phys
, ep
->msi_mem
,
310 epc
->mem
->page_size
);
312 pci_epc_mem_exit(epc
);
315 int dw_pcie_ep_init(struct dw_pcie_ep
*ep
)
320 struct dw_pcie
*pci
= to_dw_pcie_from_ep(ep
);
321 struct device
*dev
= pci
->dev
;
322 struct device_node
*np
= dev
->of_node
;
324 if (!pci
->dbi_base
|| !pci
->dbi_base2
) {
325 dev_err(dev
, "dbi_base/deb_base2 is not populated\n");
329 ret
= of_property_read_u32(np
, "num-ib-windows", &ep
->num_ib_windows
);
331 dev_err(dev
, "unable to read *num-ib-windows* property\n");
334 if (ep
->num_ib_windows
> MAX_IATU_IN
) {
335 dev_err(dev
, "invalid *num-ib-windows*\n");
339 ret
= of_property_read_u32(np
, "num-ob-windows", &ep
->num_ob_windows
);
341 dev_err(dev
, "unable to read *num-ob-windows* property\n");
344 if (ep
->num_ob_windows
> MAX_IATU_OUT
) {
345 dev_err(dev
, "invalid *num-ob-windows*\n");
349 ep
->ib_window_map
= devm_kzalloc(dev
, sizeof(long) *
350 BITS_TO_LONGS(ep
->num_ib_windows
),
352 if (!ep
->ib_window_map
)
355 ep
->ob_window_map
= devm_kzalloc(dev
, sizeof(long) *
356 BITS_TO_LONGS(ep
->num_ob_windows
),
358 if (!ep
->ob_window_map
)
361 addr
= devm_kzalloc(dev
, sizeof(phys_addr_t
) * ep
->num_ob_windows
,
365 ep
->outbound_addr
= addr
;
367 if (ep
->ops
->ep_init
)
368 ep
->ops
->ep_init(ep
);
370 epc
= devm_pci_epc_create(dev
, &epc_ops
);
372 dev_err(dev
, "failed to create epc device\n");
376 ret
= of_property_read_u8(np
, "max-functions", &epc
->max_functions
);
378 epc
->max_functions
= 1;
380 ret
= __pci_epc_mem_init(epc
, ep
->phys_base
, ep
->addr_size
,
383 dev_err(dev
, "Failed to initialize address space\n");
387 ep
->msi_mem
= pci_epc_mem_alloc_addr(epc
, &ep
->msi_mem_phys
,
388 epc
->mem
->page_size
);
390 dev_err(dev
, "Failed to reserve memory for MSI\n");
395 epc_set_drvdata(epc
, ep
);