Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / arch / x86 / pci / sta2x11-fixup.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
6  *
7  * ST Microelectronics ConneXt (STA2X11/STA2X10)
8  *
9  * Copyright (c) 2010-2011 Wind River Systems, Inc.
10  */
11
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>
18
19 #define STA2X11_SWIOTLB_SIZE (4*1024*1024)
20 extern int swiotlb_late_init_with_default_size(size_t default_size);
21
22 /*
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.
25  */
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)
29
30 struct sta2x11_ahb_regs { /* saved during suspend */
31         u32 base, pexlbase, pexhbase, crw;
32 };
33
34 struct sta2x11_mapping {
35         u32 amba_base;
36         int is_suspended;
37         struct sta2x11_ahb_regs regs[STA2X11_NR_FUNCS];
38 };
39
40 struct sta2x11_instance {
41         struct list_head list;
42         int bus0;
43         struct sta2x11_mapping map[STA2X11_NR_EP];
44 };
45
46 static LIST_HEAD(sta2x11_instance_list);
47
48 /* At probe time, record new instances of this bridge (likely one only) */
49 static void sta2x11_new_instance(struct pci_dev *pdev)
50 {
51         struct sta2x11_instance *instance;
52
53         instance = kzalloc(sizeof(*instance), GFP_ATOMIC);
54         if (!instance)
55                 return;
56         /* This has a subordinate bridge, with 4 more-subordinate ones */
57         instance->bus0 = pdev->subordinate->number + 1;
58
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");
65         }
66         list_add(&instance->list, &sta2x11_instance_list);
67 }
68 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, 0xcc17, sta2x11_new_instance);
69
70 /*
71  * Utility functions used in this file from below
72  */
73 static struct sta2x11_instance *sta2x11_pdev_to_instance(struct pci_dev *pdev)
74 {
75         struct sta2x11_instance *instance;
76         int ep;
77
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)
81                         return instance;
82         }
83         return NULL;
84 }
85
86 static int sta2x11_pdev_to_ep(struct pci_dev *pdev)
87 {
88         struct sta2x11_instance *instance;
89
90         instance = sta2x11_pdev_to_instance(pdev);
91         if (!instance)
92                 return -1;
93
94         return pdev->bus->number - instance->bus0;
95 }
96
97 static struct sta2x11_mapping *sta2x11_pdev_to_mapping(struct pci_dev *pdev)
98 {
99         struct sta2x11_instance *instance;
100         int ep;
101
102         instance = sta2x11_pdev_to_instance(pdev);
103         if (!instance)
104                 return NULL;
105         ep = sta2x11_pdev_to_ep(pdev);
106         return instance->map + ep;
107 }
108
109 /* This is exported, as some devices need to access the MFD registers */
110 struct sta2x11_instance *sta2x11_get_instance(struct pci_dev *pdev)
111 {
112         return sta2x11_pdev_to_instance(pdev);
113 }
114 EXPORT_SYMBOL(sta2x11_get_instance);
115
116
117 /**
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)
122  */
123 static dma_addr_t p2a(dma_addr_t p, struct pci_dev *pdev)
124 {
125         struct sta2x11_mapping *map;
126         dma_addr_t a;
127
128         map = sta2x11_pdev_to_mapping(pdev);
129         a = p + map->amba_base;
130         return a;
131 }
132
133 /**
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)
138  */
139 static dma_addr_t a2p(dma_addr_t a, struct pci_dev *pdev)
140 {
141         struct sta2x11_mapping *map;
142         dma_addr_t p;
143
144         map = sta2x11_pdev_to_mapping(pdev);
145         p = a - map->amba_base;
146         return p;
147 }
148
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)
151 {
152         struct sta2x11_instance *instance = sta2x11_pdev_to_instance(pdev);
153
154         if (!instance) /* either a sta2x11 bridge or another ST device */
155                 return;
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;
159
160         /* We must enable all devices as master, for audio DMA to work */
161         pci_set_master(pdev);
162 }
163 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_setup_pdev);
164
165 /*
166  * The following three functions are exported (used in swiotlb: FIXME)
167  */
168 /**
169  * dma_capable - Check if device can manage DMA transfers (FIXME: kill it)
170  * @dev: device for a PCI device
171  * @addr: DMA address
172  * @size: DMA size
173  */
174 bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
175 {
176         struct sta2x11_mapping *map;
177
178         if (!dev->archdata.is_sta2x11) {
179                 if (!dev->dma_mask)
180                         return false;
181                 return addr + size - 1 <= *dev->dma_mask;
182         }
183
184         map = sta2x11_pdev_to_mapping(to_pci_dev(dev));
185
186         if (!map || (addr < map->amba_base))
187                 return false;
188         if (addr + size >= map->amba_base + STA2X11_AMBA_SIZE) {
189                 return false;
190         }
191
192         return true;
193 }
194
195 /**
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
199  */
200 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
201 {
202         if (!dev->archdata.is_sta2x11)
203                 return paddr;
204         return p2a(paddr, to_pci_dev(dev));
205 }
206
207 /**
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
211  */
212 phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
213 {
214         if (!dev->archdata.is_sta2x11)
215                 return daddr;
216         return a2p(daddr, to_pci_dev(dev));
217 }
218
219
220 /*
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
223  */
224
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)
235
236 /* At probe time, enable mapping for each endpoint, using the pdev */
237 static void sta2x11_map_ep(struct pci_dev *pdev)
238 {
239         struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
240         int i;
241
242         if (!map)
243                 return;
244         pci_read_config_dword(pdev, AHB_BASE(0), &map->amba_base);
245
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);
251
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);
255
256         dev_info(&pdev->dev,
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);
260 }
261 DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_map_ep);
262
263 #ifdef CONFIG_PM /* Some register values must be saved and restored */
264
265 static void suspend_mapping(struct pci_dev *pdev)
266 {
267         struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
268         int i;
269
270         if (!map)
271                 return;
272
273         if (map->is_suspended)
274                 return;
275         map->is_suspended = 1;
276
277         /* Save all window configs */
278         for (i = 0; i < STA2X11_NR_FUNCS; i++) {
279                 struct sta2x11_ahb_regs *regs = map->regs + i;
280
281                 pci_read_config_dword(pdev, AHB_BASE(i), &regs->base);
282                 pci_read_config_dword(pdev, AHB_PEXLBASE(i), &regs->pexlbase);
283                 pci_read_config_dword(pdev, AHB_PEXHBASE(i), &regs->pexhbase);
284                 pci_read_config_dword(pdev, AHB_CRW(i), &regs->crw);
285         }
286 }
287 DECLARE_PCI_FIXUP_SUSPEND(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, suspend_mapping);
288
289 static void resume_mapping(struct pci_dev *pdev)
290 {
291         struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
292         int i;
293
294         if (!map)
295                 return;
296
297
298         if (!map->is_suspended)
299                 goto out;
300         map->is_suspended = 0;
301
302         /* Restore all window configs */
303         for (i = 0; i < STA2X11_NR_FUNCS; i++) {
304                 struct sta2x11_ahb_regs *regs = map->regs + i;
305
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);
310         }
311 out:
312         pci_set_master(pdev); /* Like at boot, enable master on all devices */
313 }
314 DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, resume_mapping);
315
316 #endif /* CONFIG_PM */