1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/x86/pci/sta2x11-fixup.c
4 * glue code for lib/swiotlb.c and DMA translation between STA2x11
5 * AMBA memory mapping and the X86 memory mapping
7 * ST Microelectronics ConneXt (STA2X11/STA2X10)
9 * Copyright (c) 2010-2011 Wind River Systems, Inc.
12 #include <linux/pci.h>
13 #include <linux/pci_ids.h>
14 #include <linux/export.h>
15 #include <linux/list.h>
16 #include <linux/dma-direct.h>
17 #include <asm/iommu.h>
19 #define STA2X11_SWIOTLB_SIZE (4*1024*1024)
20 extern int swiotlb_late_init_with_default_size(size_t default_size);
23 * We build a list of bus numbers that are under the ConneXt. The
24 * main bridge hosts 4 busses, which are the 4 endpoints, in order.
26 #define STA2X11_NR_EP 4 /* 0..3 included */
27 #define STA2X11_NR_FUNCS 8 /* 0..7 included */
28 #define STA2X11_AMBA_SIZE (512 << 20)
30 struct sta2x11_ahb_regs { /* saved during suspend */
31 u32 base, pexlbase, pexhbase, crw;
34 struct sta2x11_mapping {
37 struct sta2x11_ahb_regs regs[STA2X11_NR_FUNCS];
40 struct sta2x11_instance {
41 struct list_head list;
43 struct sta2x11_mapping map[STA2X11_NR_EP];
46 static LIST_HEAD(sta2x11_instance_list);
48 /* At probe time, record new instances of this bridge (likely one only) */
49 static void sta2x11_new_instance(struct pci_dev *pdev)
51 struct sta2x11_instance *instance;
53 instance = kzalloc(sizeof(*instance), GFP_ATOMIC);
56 /* This has a subordinate bridge, with 4 more-subordinate ones */
57 instance->bus0 = pdev->subordinate->number + 1;
59 if (list_empty(&sta2x11_instance_list)) {
60 int size = STA2X11_SWIOTLB_SIZE;
61 /* First instance: register your own swiotlb area */
62 dev_info(&pdev->dev, "Using SWIOTLB (size %i)\n", size);
63 if (swiotlb_late_init_with_default_size(size))
64 dev_emerg(&pdev->dev, "init swiotlb failed\n");
66 list_add(&instance->list, &sta2x11_instance_list);
68 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, 0xcc17, sta2x11_new_instance);
71 * Utility functions used in this file from below
73 static struct sta2x11_instance *sta2x11_pdev_to_instance(struct pci_dev *pdev)
75 struct sta2x11_instance *instance;
78 list_for_each_entry(instance, &sta2x11_instance_list, list) {
79 ep = pdev->bus->number - instance->bus0;
80 if (ep >= 0 && ep < STA2X11_NR_EP)
86 static int sta2x11_pdev_to_ep(struct pci_dev *pdev)
88 struct sta2x11_instance *instance;
90 instance = sta2x11_pdev_to_instance(pdev);
94 return pdev->bus->number - instance->bus0;
97 static struct sta2x11_mapping *sta2x11_pdev_to_mapping(struct pci_dev *pdev)
99 struct sta2x11_instance *instance;
102 instance = sta2x11_pdev_to_instance(pdev);
105 ep = sta2x11_pdev_to_ep(pdev);
106 return instance->map + ep;
109 /* This is exported, as some devices need to access the MFD registers */
110 struct sta2x11_instance *sta2x11_get_instance(struct pci_dev *pdev)
112 return sta2x11_pdev_to_instance(pdev);
114 EXPORT_SYMBOL(sta2x11_get_instance);
118 * p2a - Translate physical address to STA2x11 AMBA address,
119 * used for DMA transfers to STA2x11
120 * @p: Physical address
121 * @pdev: PCI device (must be hosted within the connext)
123 static dma_addr_t p2a(dma_addr_t p, struct pci_dev *pdev)
125 struct sta2x11_mapping *map;
128 map = sta2x11_pdev_to_mapping(pdev);
129 a = p + map->amba_base;
134 * a2p - Translate STA2x11 AMBA address to physical address
135 * used for DMA transfers from STA2x11
136 * @a: STA2x11 AMBA address
137 * @pdev: PCI device (must be hosted within the connext)
139 static dma_addr_t a2p(dma_addr_t a, struct pci_dev *pdev)
141 struct sta2x11_mapping *map;
144 map = sta2x11_pdev_to_mapping(pdev);
145 p = a - map->amba_base;
149 /* At setup time, we use our own ops if the device is a ConneXt one */
150 static void sta2x11_setup_pdev(struct pci_dev *pdev)
152 struct sta2x11_instance *instance = sta2x11_pdev_to_instance(pdev);
154 if (!instance) /* either a sta2x11 bridge or another ST device */
156 pci_set_consistent_dma_mask(pdev, STA2X11_AMBA_SIZE - 1);
157 pci_set_dma_mask(pdev, STA2X11_AMBA_SIZE - 1);
158 pdev->dev.archdata.is_sta2x11 = true;
160 /* We must enable all devices as master, for audio DMA to work */
161 pci_set_master(pdev);
163 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_setup_pdev);
166 * The following three functions are exported (used in swiotlb: FIXME)
169 * dma_capable - Check if device can manage DMA transfers (FIXME: kill it)
170 * @dev: device for a PCI device
174 bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
176 struct sta2x11_mapping *map;
178 if (!dev->archdata.is_sta2x11) {
181 return addr + size - 1 <= *dev->dma_mask;
184 map = sta2x11_pdev_to_mapping(to_pci_dev(dev));
186 if (!map || (addr < map->amba_base))
188 if (addr + size >= map->amba_base + STA2X11_AMBA_SIZE) {
196 * __phys_to_dma - Return the DMA AMBA address used for this STA2x11 device
197 * @dev: device for a PCI device
198 * @paddr: Physical address
200 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
202 if (!dev->archdata.is_sta2x11)
204 return p2a(paddr, to_pci_dev(dev));
208 * dma_to_phys - Return the physical address used for this STA2x11 DMA address
209 * @dev: device for a PCI device
210 * @daddr: STA2x11 AMBA DMA address
212 phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
214 if (!dev->archdata.is_sta2x11)
216 return a2p(daddr, to_pci_dev(dev));
221 * At boot we must set up the mappings for the pcie-to-amba bridge.
222 * It involves device access, and the same happens at suspend/resume time
225 #define AHB_MAPB 0xCA4
226 #define AHB_CRW(i) (AHB_MAPB + 0 + (i) * 0x10)
227 #define AHB_CRW_SZMASK 0xfffffc00UL
228 #define AHB_CRW_ENABLE (1 << 0)
229 #define AHB_CRW_WTYPE_MEM (2 << 1)
230 #define AHB_CRW_ROE (1UL << 3) /* Relax Order Ena */
231 #define AHB_CRW_NSE (1UL << 4) /* No Snoop Enable */
232 #define AHB_BASE(i) (AHB_MAPB + 4 + (i) * 0x10)
233 #define AHB_PEXLBASE(i) (AHB_MAPB + 8 + (i) * 0x10)
234 #define AHB_PEXHBASE(i) (AHB_MAPB + 12 + (i) * 0x10)
236 /* At probe time, enable mapping for each endpoint, using the pdev */
237 static void sta2x11_map_ep(struct pci_dev *pdev)
239 struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
244 pci_read_config_dword(pdev, AHB_BASE(0), &map->amba_base);
246 /* Configure AHB mapping */
247 pci_write_config_dword(pdev, AHB_PEXLBASE(0), 0);
248 pci_write_config_dword(pdev, AHB_PEXHBASE(0), 0);
249 pci_write_config_dword(pdev, AHB_CRW(0), STA2X11_AMBA_SIZE |
250 AHB_CRW_WTYPE_MEM | AHB_CRW_ENABLE);
252 /* Disable all the other windows */
253 for (i = 1; i < STA2X11_NR_FUNCS; i++)
254 pci_write_config_dword(pdev, AHB_CRW(i), 0);
257 "sta2x11: Map EP %i: AMBA address %#8x-%#8x\n",
258 sta2x11_pdev_to_ep(pdev), map->amba_base,
259 map->amba_base + STA2X11_AMBA_SIZE - 1);
261 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_map_ep);
263 #ifdef CONFIG_PM /* Some register values must be saved and restored */
265 static void suspend_mapping(struct pci_dev *pdev)
267 struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
273 if (map->is_suspended)
275 map->is_suspended = 1;
277 /* Save all window configs */
278 for (i = 0; i < STA2X11_NR_FUNCS; i++) {
279 struct sta2x11_ahb_regs *regs = map->regs + i;
281 pci_read_config_dword(pdev, AHB_BASE(i), ®s->base);
282 pci_read_config_dword(pdev, AHB_PEXLBASE(i), ®s->pexlbase);
283 pci_read_config_dword(pdev, AHB_PEXHBASE(i), ®s->pexhbase);
284 pci_read_config_dword(pdev, AHB_CRW(i), ®s->crw);
287 DECLARE_PCI_FIXUP_SUSPEND(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, suspend_mapping);
289 static void resume_mapping(struct pci_dev *pdev)
291 struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
298 if (!map->is_suspended)
300 map->is_suspended = 0;
302 /* Restore all window configs */
303 for (i = 0; i < STA2X11_NR_FUNCS; i++) {
304 struct sta2x11_ahb_regs *regs = map->regs + i;
306 pci_write_config_dword(pdev, AHB_BASE(i), regs->base);
307 pci_write_config_dword(pdev, AHB_PEXLBASE(i), regs->pexlbase);
308 pci_write_config_dword(pdev, AHB_PEXHBASE(i), regs->pexhbase);
309 pci_write_config_dword(pdev, AHB_CRW(i), regs->crw);
312 pci_set_master(pdev); /* Like at boot, enable master on all devices */
314 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, resume_mapping);
316 #endif /* CONFIG_PM */