common: Drop log.h from common header
[platform/kernel/u-boot.git] / board / armltd / integrator / pci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5  * Marius Groeger <mgroeger@sysgo.de>
6  *
7  * (C) Copyright 2002
8  * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
9  *
10  * (C) Copyright 2003
11  * Texas Instruments, <www.ti.com>
12  * Kshitij Gupta <Kshitij@ti.com>
13  *
14  * (C) Copyright 2004
15  * ARM Ltd.
16  * Philippe Robin, <philippe.robin@arm.com>
17  *
18  * (C) Copyright 2011
19  * Linaro
20  * Linus Walleij <linus.walleij@linaro.org>
21  */
22 #include <common.h>
23 #include <init.h>
24 #include <log.h>
25 #include <pci.h>
26 #include <asm/io.h>
27 #include "integrator-sc.h"
28 #include "pci_v3.h"
29
30 #define INTEGRATOR_BOOT_ROM_BASE        0x20000000
31 #define INTEGRATOR_HDR0_SDRAM_BASE      0x80000000
32
33 /*
34  * These are in the physical addresses on the CPU side, i.e.
35  * where we read and write stuff - you don't want to try to
36  * move these around
37  */
38 #define PHYS_PCI_MEM_BASE       0x40000000
39 #define PHYS_PCI_IO_BASE        0x60000000      /* PCI I/O space base */
40 #define PHYS_PCI_CONFIG_BASE    0x61000000
41 #define PHYS_PCI_V3_BASE        0x62000000      /* V360EPC registers */
42 #define SZ_256M                 0x10000000
43
44 /*
45  * These are in the PCI BUS address space
46  * Set to 0x00000000 in the Linux kernel, 0x40000000 in Boot monitor
47  * we follow the example of the kernel, because that is the address
48  * range that devices actually use - what would they be doing at
49  * 0x40000000?
50  */
51 #define PCI_BUS_NONMEM_START    0x00000000
52 #define PCI_BUS_NONMEM_SIZE     SZ_256M
53
54 #define PCI_BUS_PREMEM_START    (PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE)
55 #define PCI_BUS_PREMEM_SIZE     SZ_256M
56
57 #if PCI_BUS_NONMEM_START & 0x000fffff
58 #error PCI_BUS_NONMEM_START must be megabyte aligned
59 #endif
60 #if PCI_BUS_PREMEM_START & 0x000fffff
61 #error PCI_BUS_PREMEM_START must be megabyte aligned
62 #endif
63
64 /*
65  * Initialize PCI Devices, report devices found.
66  */
67
68 #ifndef CONFIG_PCI_PNP
69 #define PCI_ENET0_IOADDR        0x60000000 /* First card in PCI I/O space */
70 #define PCI_ENET0_MEMADDR       0x40000000 /* First card in PCI memory space */
71 static struct pci_config_table pci_integrator_config_table[] = {
72         { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x0f, PCI_ANY_ID,
73           pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
74                                        PCI_ENET0_MEMADDR,
75                                        PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }},
76         { }
77 };
78 #endif /* CONFIG_PCI_PNP */
79
80 /* V3 access routines */
81 #define v3_writeb(o, v) __raw_writeb(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
82 #define v3_readb(o)    (__raw_readb(PHYS_PCI_V3_BASE + (unsigned int)(o)))
83
84 #define v3_writew(o, v) __raw_writew(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
85 #define v3_readw(o)    (__raw_readw(PHYS_PCI_V3_BASE + (unsigned int)(o)))
86
87 #define v3_writel(o, v) __raw_writel(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
88 #define v3_readl(o)    (__raw_readl(PHYS_PCI_V3_BASE + (unsigned int)(o)))
89
90 static unsigned long v3_open_config_window(pci_dev_t bdf, int offset)
91 {
92         unsigned int address, mapaddress;
93         unsigned int busnr = PCI_BUS(bdf);
94         unsigned int devfn = PCI_FUNC(bdf);
95
96         /*
97          * Trap out illegal values
98          */
99         if (offset > 255)
100                 BUG();
101         if (busnr > 255)
102                 BUG();
103         if (devfn > 255)
104                 BUG();
105
106         if (busnr == 0) {
107                 /*
108                  * Linux calls the thing U-Boot calls "DEV" "SLOT"
109                  * instead, but it's the same 5 bits
110                  */
111                 int slot = PCI_DEV(bdf);
112
113                 /*
114                  * local bus segment so need a type 0 config cycle
115                  *
116                  * build the PCI configuration "address" with one-hot in
117                  * A31-A11
118                  *
119                  * mapaddress:
120                  *  3:1 = config cycle (101)
121                  *  0   = PCI A1 & A0 are 0 (0)
122                  */
123                 address = PCI_FUNC(bdf) << 8;
124                 mapaddress = V3_LB_MAP_TYPE_CONFIG;
125
126                 if (slot > 12)
127                         /*
128                          * high order bits are handled by the MAP register
129                          */
130                         mapaddress |= 1 << (slot - 5);
131                 else
132                         /*
133                          * low order bits handled directly in the address
134                          */
135                         address |= 1 << (slot + 11);
136         } else {
137                 /*
138                  * not the local bus segment so need a type 1 config cycle
139                  *
140                  * address:
141                  *  23:16 = bus number
142                  *  15:11 = slot number (7:3 of devfn)
143                  *  10:8  = func number (2:0 of devfn)
144                  *
145                  * mapaddress:
146                  *  3:1 = config cycle (101)
147                  *  0   = PCI A1 & A0 from host bus (1)
148                  */
149                 mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
150                 address = (busnr << 16) | (devfn << 8);
151         }
152
153         /*
154          * Set up base0 to see all 512Mbytes of memory space (not
155          * prefetchable), this frees up base1 for re-use by
156          * configuration memory
157          */
158         v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
159                         V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);
160
161         /*
162          * Set up base1/map1 to point into configuration space.
163          */
164         v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_CONFIG_BASE) |
165                         V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);
166         v3_writew(V3_LB_MAP1, mapaddress);
167
168         return PHYS_PCI_CONFIG_BASE + address + offset;
169 }
170
171 static void v3_close_config_window(void)
172 {
173         /*
174          * Reassign base1 for use by prefetchable PCI memory
175          */
176         v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
177                         V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
178                         V3_LB_BASE_ENABLE);
179         v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
180                         V3_LB_MAP_TYPE_MEM_MULTIPLE);
181
182         /*
183          * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
184          */
185         v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
186                         V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
187 }
188
189 static int pci_integrator_read_byte(struct pci_controller *hose, pci_dev_t bdf,
190                                     int offset, unsigned char *val)
191 {
192         unsigned long addr;
193
194         addr = v3_open_config_window(bdf, offset);
195         *val = __raw_readb(addr);
196         v3_close_config_window();
197         return 0;
198 }
199
200 static int pci_integrator_read__word(struct pci_controller *hose,
201                                      pci_dev_t bdf, int offset,
202                                      unsigned short *val)
203 {
204         unsigned long addr;
205
206         addr = v3_open_config_window(bdf, offset);
207         *val = __raw_readw(addr);
208         v3_close_config_window();
209         return 0;
210 }
211
212 static int pci_integrator_read_dword(struct pci_controller *hose,
213                                      pci_dev_t bdf, int offset,
214                                      unsigned int *val)
215 {
216         unsigned long addr;
217
218         addr = v3_open_config_window(bdf, offset);
219         *val = __raw_readl(addr);
220         v3_close_config_window();
221         return 0;
222 }
223
224 static int pci_integrator_write_byte(struct pci_controller *hose,
225                                      pci_dev_t bdf, int offset,
226                                      unsigned char val)
227 {
228         unsigned long addr;
229
230         addr = v3_open_config_window(bdf, offset);
231         __raw_writeb((u8)val, addr);
232         __raw_readb(addr);
233         v3_close_config_window();
234         return 0;
235 }
236
237 static int pci_integrator_write_word(struct pci_controller *hose,
238                                      pci_dev_t bdf, int offset,
239                                      unsigned short val)
240 {
241         unsigned long addr;
242
243         addr = v3_open_config_window(bdf, offset);
244         __raw_writew((u8)val, addr);
245         __raw_readw(addr);
246         v3_close_config_window();
247         return 0;
248 }
249
250 static int pci_integrator_write_dword(struct pci_controller *hose,
251                                       pci_dev_t bdf, int offset,
252                                       unsigned int val)
253 {
254         unsigned long addr;
255
256         addr = v3_open_config_window(bdf, offset);
257         __raw_writel((u8)val, addr);
258         __raw_readl(addr);
259         v3_close_config_window();
260         return 0;
261 }
262
263 struct pci_controller integrator_hose = {
264 #ifndef CONFIG_PCI_PNP
265         config_table: pci_integrator_config_table,
266 #endif
267 };
268
269 void pci_init_board(void)
270 {
271         struct pci_controller *hose = &integrator_hose;
272         u16 val;
273
274         /* setting this register will take the V3 out of reset */
275         __raw_writel(SC_PCI_PCIEN, SC_PCI);
276
277         /* Wait for 230 ms (from spec) before accessing any V3 registers */
278         mdelay(230);
279
280         /* Now write the Base I/O Address Word to PHYS_PCI_V3_BASE + 0x6E */
281         v3_writew(V3_LB_IO_BASE, (PHYS_PCI_V3_BASE >> 16));
282
283         /* Wait for the mailbox to settle */
284         do {
285                 v3_writeb(V3_MAIL_DATA, 0xAA);
286                 v3_writeb(V3_MAIL_DATA + 4, 0x55);
287         } while (v3_readb(V3_MAIL_DATA) != 0xAA ||
288                  v3_readb(V3_MAIL_DATA + 4) != 0x55);
289
290         /* Make sure that V3 register access is not locked, if it is, unlock it */
291         if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)
292                 v3_writew(V3_SYSTEM, 0xA05F);
293
294         /*
295          * Ensure that the slave accesses from PCI are disabled while we
296          * setup memory windows
297          */
298         val = v3_readw(V3_PCI_CMD);
299         val &= ~(V3_COMMAND_M_MEM_EN | V3_COMMAND_M_IO_EN);
300         v3_writew(V3_PCI_CMD, val);
301
302         /* Clear RST_OUT to 0; keep the PCI bus in reset until we've finished */
303         val = v3_readw(V3_SYSTEM);
304         val &= ~V3_SYSTEM_M_RST_OUT;
305         v3_writew(V3_SYSTEM, val);
306
307         /* Make all accesses from PCI space retry until we're ready for them */
308         val = v3_readw(V3_PCI_CFG);
309         val |= V3_PCI_CFG_M_RETRY_EN;
310         v3_writew(V3_PCI_CFG, val);
311
312         /*
313          * Set up any V3 PCI Configuration Registers that we absolutely have to.
314          * LB_CFG controls Local Bus protocol.
315          * Enable LocalBus byte strobes for READ accesses too.
316          * set bit 7 BE_IMODE and bit 6 BE_OMODE
317          */
318         val = v3_readw(V3_LB_CFG);
319         val |= 0x0C0;
320         v3_writew(V3_LB_CFG, val);
321
322         /* PCI_CMD controls overall PCI operation. Enable PCI bus master. */
323         val = v3_readw(V3_PCI_CMD);
324         val |= V3_COMMAND_M_MASTER_EN;
325         v3_writew(V3_PCI_CMD, val);
326
327         /*
328          * PCI_MAP0 controls where the PCI to CPU memory window is on
329          * Local Bus
330          */
331         v3_writel(V3_PCI_MAP0,
332                   (INTEGRATOR_BOOT_ROM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_512MB |
333                                                 V3_PCI_MAP_M_REG_EN |
334                                                 V3_PCI_MAP_M_ENABLE));
335
336         /* PCI_BASE0 is the PCI address of the start of the window */
337         v3_writel(V3_PCI_BASE0, INTEGRATOR_BOOT_ROM_BASE);
338
339         /* PCI_MAP1 is LOCAL address of the start of the window */
340         v3_writel(V3_PCI_MAP1,
341                   (INTEGRATOR_HDR0_SDRAM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_1GB |
342                                                   V3_PCI_MAP_M_REG_EN |
343                                                   V3_PCI_MAP_M_ENABLE));
344
345         /* PCI_BASE1 is the PCI address of the start of the window */
346         v3_writel(V3_PCI_BASE1, INTEGRATOR_HDR0_SDRAM_BASE);
347
348         /*
349          * Set up memory the windows from local bus memory into PCI
350          * configuration, I/O and Memory regions.
351          * PCI I/O, LB_BASE2 and LB_MAP2 are used exclusively for this.
352          */
353         v3_writew(V3_LB_BASE2,
354                   v3_addr_to_lb_map(PHYS_PCI_IO_BASE) | V3_LB_BASE_ENABLE);
355         v3_writew(V3_LB_MAP2, 0);
356
357         /* PCI Configuration, use LB_BASE1/LB_MAP1. */
358
359         /*
360          * PCI Memory use LB_BASE0/LB_MAP0 and LB_BASE1/LB_MAP1
361          * Map first 256Mbytes as non-prefetchable via BASE0/MAP0
362          */
363         v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
364                         V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
365         v3_writew(V3_LB_MAP0,
366                   v3_addr_to_lb_map(PCI_BUS_NONMEM_START) | V3_LB_MAP_TYPE_MEM);
367
368         /* Map second 256 Mbytes as prefetchable via BASE1/MAP1 */
369         v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
370                         V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
371                         V3_LB_BASE_ENABLE);
372         v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
373                         V3_LB_MAP_TYPE_MEM_MULTIPLE);
374
375         /* Dump PCI to local address space mappings */
376         debug("LB_BASE0 = %08x\n", v3_readl(V3_LB_BASE0));
377         debug("LB_MAP0 = %04x\n", v3_readw(V3_LB_MAP0));
378         debug("LB_BASE1 = %08x\n", v3_readl(V3_LB_BASE1));
379         debug("LB_MAP1 = %04x\n", v3_readw(V3_LB_MAP1));
380         debug("LB_BASE2 = %04x\n", v3_readw(V3_LB_BASE2));
381         debug("LB_MAP2 = %04x\n", v3_readw(V3_LB_MAP2));
382         debug("LB_IO_BASE = %04x\n", v3_readw(V3_LB_IO_BASE));
383
384         /*
385          * Allow accesses to PCI Configuration space and set up A1, A0 for
386          * type 1 config cycles
387          */
388         val = v3_readw(V3_PCI_CFG);
389         val &= ~(V3_PCI_CFG_M_RETRY_EN | V3_PCI_CFG_M_AD_LOW1);
390         val |= V3_PCI_CFG_M_AD_LOW0;
391         v3_writew(V3_PCI_CFG, val);
392
393         /* now we can allow incoming PCI MEMORY accesses */
394         val = v3_readw(V3_PCI_CMD);
395         val |= V3_COMMAND_M_MEM_EN;
396         v3_writew(V3_PCI_CMD, val);
397
398         /*
399          * Set RST_OUT to take the PCI bus is out of reset, PCI devices can
400          * now initialise.
401          */
402         val = v3_readw(V3_SYSTEM);
403         val |= V3_SYSTEM_M_RST_OUT;
404         v3_writew(V3_SYSTEM, val);
405
406         /*  Lock the V3 system register so that no one else can play with it */
407         val = v3_readw(V3_SYSTEM);
408         val |= V3_SYSTEM_M_LOCK;
409         v3_writew(V3_SYSTEM, val);
410
411         /*
412          * Configure and register the PCI hose
413          */
414         hose->first_busno = 0;
415         hose->last_busno = 0xff;
416
417         /* System memory space, window 0 256 MB non-prefetchable */
418         pci_set_region(hose->regions + 0,
419                        PCI_BUS_NONMEM_START, PHYS_PCI_MEM_BASE,
420                        SZ_256M,
421                        PCI_REGION_MEM);
422
423         /* System memory space, window 1 256 MB prefetchable */
424         pci_set_region(hose->regions + 1,
425                        PCI_BUS_PREMEM_START, PHYS_PCI_MEM_BASE + SZ_256M,
426                        SZ_256M,
427                        PCI_REGION_MEM |
428                        PCI_REGION_PREFETCH);
429
430         /* PCI I/O space */
431         pci_set_region(hose->regions + 2,
432                        0x00000000, PHYS_PCI_IO_BASE, 0x01000000,
433                        PCI_REGION_IO);
434
435         /* PCI Memory - config space */
436         pci_set_region(hose->regions + 3,
437                        0x00000000, PHYS_PCI_CONFIG_BASE, 0x01000000,
438                        PCI_REGION_MEM);
439         /* PCI V3 regs */
440         pci_set_region(hose->regions + 4,
441                        0x00000000, PHYS_PCI_V3_BASE, 0x01000000,
442                        PCI_REGION_MEM);
443
444         hose->region_count = 5;
445
446         pci_set_ops(hose,
447                     pci_integrator_read_byte,
448                     pci_integrator_read__word,
449                     pci_integrator_read_dword,
450                     pci_integrator_write_byte,
451                     pci_integrator_write_word,
452                     pci_integrator_write_dword);
453
454         pci_register_hose(hose);
455
456         pciauto_config_init(hose);
457         pciauto_config_device(hose, 0);
458
459         hose->last_busno = pci_hose_scan(hose);
460 }