]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/mv/mv_machdep.c
tcpdump: remove undesired svn:keywords property from contrib
[FreeBSD/FreeBSD.git] / sys / arm / mv / mv_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1994-1998 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Brini.
21  * 4. The name of the company nor the name of the author may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * from: FreeBSD: //depot/projects/arm/src/sys/arm/at91/kb920x_machdep.c, rev 45
38  */
39
40 #include "opt_ddb.h"
41 #include "opt_platform.h"
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #define _ARM32_BUS_DMA_PRIVATE
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bus.h>
50 #include <sys/devmap.h>
51 #include <sys/kernel.h>
52
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55
56 #include <arm/arm/mpcore_timervar.h>
57 #include <arm/arm/nexusvar.h>
58
59 #include <machine/bus.h>
60 #include <machine/fdt.h>
61 #include <machine/machdep.h>
62 #include <machine/platform.h> 
63
64 #if __ARM_ARCH < 6
65 #include <machine/cpu-v4.h>
66 #else
67 #include <machine/cpu-v6.h>
68 #include <machine/pte-v6.h>
69 #endif
70
71 #include <arm/mv/mvreg.h>       /* XXX */
72 #include <arm/mv/mvvar.h>       /* XXX eventually this should be eliminated */
73 #include <arm/mv/mvwin.h>
74
75 #include <dev/fdt/fdt_common.h>
76 #include <dev/ofw/ofw_bus_subr.h>
77
78 static int platform_mpp_init(void);
79 #if defined(SOC_MV_ARMADAXP)
80 void armadaxp_init_coher_fabric(void);
81 void armadaxp_l2_init(void);
82 #endif
83 #if defined(SOC_MV_ARMADA38X)
84 int armada38x_win_set_iosync_barrier(void);
85 int armada38x_scu_enable(void);
86 int armada38x_open_bootrom_win(void);
87 int armada38x_mbus_optimization(void);
88 #endif
89
90 #define MPP_PIN_MAX             68
91 #define MPP_PIN_CELLS           2
92 #define MPP_PINS_PER_REG        8
93 #define MPP_SEL(pin,func)       (((func) & 0xf) <<              \
94     (((pin) % MPP_PINS_PER_REG) * 4))
95
96 static void
97 mv_busdma_tag_init(void *arg __unused)
98 {
99         phandle_t node;
100         bus_dma_tag_t dmat;
101
102         /*
103          * If this platform has coherent DMA, create the parent DMA tag to pass
104          * down the coherent flag to all busses and devices on the platform,
105          * otherwise return without doing anything. By default create tag
106          * for all A38x-based platforms only.
107          */
108         if ((node = OF_finddevice("/")) == -1)
109                 return;
110         if (ofw_bus_node_is_compatible(node, "marvell,armada380") == 0)
111                 return;
112
113         bus_dma_tag_create(NULL,        /* No parent tag */
114             1, 0,                       /* alignment, bounds */
115             BUS_SPACE_MAXADDR,          /* lowaddr */
116             BUS_SPACE_MAXADDR,          /* highaddr */
117             NULL, NULL,                 /* filter, filterarg */
118             BUS_SPACE_MAXSIZE,          /* maxsize */
119             BUS_SPACE_UNRESTRICTED,     /* nsegments */
120             BUS_SPACE_MAXSIZE,          /* maxsegsize */
121             BUS_DMA_COHERENT,           /* flags */
122             NULL, NULL,                 /* lockfunc, lockarg */
123             &dmat);
124
125         nexus_set_dma_tag(dmat);
126 }
127 SYSINIT(mv_busdma_tag, SI_SUB_DRIVERS, SI_ORDER_ANY, mv_busdma_tag_init, NULL);
128
129 static int
130 platform_mpp_init(void)
131 {
132         pcell_t pinmap[MPP_PIN_MAX * MPP_PIN_CELLS];
133         int mpp[MPP_PIN_MAX];
134         uint32_t ctrl_val, ctrl_offset;
135         pcell_t reg[4];
136         u_long start, size;
137         phandle_t node;
138         pcell_t pin_cells, *pinmap_ptr, pin_count;
139         ssize_t len;
140         int par_addr_cells, par_size_cells;
141         int tuple_size, tuples, rv, pins, i, j;
142         int mpp_pin, mpp_function;
143
144         /*
145          * Try to access the MPP node directly i.e. through /aliases/mpp.
146          */
147         if ((node = OF_finddevice("mpp")) != -1)
148                 if (ofw_bus_node_is_compatible(node, "mrvl,mpp"))
149                         goto moveon;
150         /*
151          * Find the node the long way.
152          */
153         if ((node = OF_finddevice("/")) == -1)
154                 return (ENXIO);
155
156         if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
157                 return (ENXIO);
158
159         if ((node = fdt_find_compatible(node, "mrvl,mpp", 0)) == 0)
160                 /*
161                  * No MPP node. Fall back to how MPP got set by the
162                  * first-stage loader and try to continue booting.
163                  */
164                 return (0);
165 moveon:
166         /*
167          * Process 'reg' prop.
168          */
169         if ((rv = fdt_addrsize_cells(OF_parent(node), &par_addr_cells,
170             &par_size_cells)) != 0)
171                 return(ENXIO);
172
173         tuple_size = sizeof(pcell_t) * (par_addr_cells + par_size_cells);
174         len = OF_getprop(node, "reg", reg, sizeof(reg));
175         tuples = len / tuple_size;
176         if (tuple_size <= 0)
177                 return (EINVAL);
178
179         /*
180          * Get address/size. XXX we assume only the first 'reg' tuple is used.
181          */
182         rv = fdt_data_to_res(reg, par_addr_cells, par_size_cells,
183             &start, &size);
184         if (rv != 0)
185                 return (rv);
186         start += fdt_immr_va;
187
188         /*
189          * Process 'pin-count' and 'pin-map' props.
190          */
191         if (OF_getencprop(node, "pin-count", &pin_count, sizeof(pin_count)) <= 0)
192                 return (ENXIO);
193         if (pin_count > MPP_PIN_MAX)
194                 return (ERANGE);
195
196         if (OF_getencprop(node, "#pin-cells", &pin_cells, sizeof(pin_cells)) <= 0)
197                 pin_cells = MPP_PIN_CELLS;
198         if (pin_cells > MPP_PIN_CELLS)
199                 return (ERANGE);
200         tuple_size = sizeof(pcell_t) * pin_cells;
201
202         bzero(pinmap, sizeof(pinmap));
203         len = OF_getencprop(node, "pin-map", pinmap, sizeof(pinmap));
204         if (len <= 0)
205                 return (ERANGE);
206         if (len % tuple_size)
207                 return (ERANGE);
208         pins = len / tuple_size;
209         if (pins > pin_count)
210                 return (ERANGE);
211         /*
212          * Fill out a "mpp[pin] => function" table. All pins unspecified in
213          * the 'pin-map' property are defaulted to 0 function i.e. GPIO.
214          */
215         bzero(mpp, sizeof(mpp));
216         pinmap_ptr = pinmap;
217         for (i = 0; i < pins; i++) {
218                 mpp_pin = *pinmap_ptr;
219                 mpp_function = *(pinmap_ptr + 1);
220                 mpp[mpp_pin] = mpp_function;
221                 pinmap_ptr += pin_cells;
222         }
223
224         /*
225          * Prepare and program MPP control register values.
226          */
227         ctrl_offset = 0;
228         for (i = 0; i < pin_count;) {
229                 ctrl_val = 0;
230
231                 for (j = 0; j < MPP_PINS_PER_REG; j++) {
232                         if (i + j == pin_count - 1)
233                                 break;
234                         ctrl_val |= MPP_SEL(i + j, mpp[i + j]);
235                 }
236                 i += MPP_PINS_PER_REG;
237                 bus_space_write_4(fdtbus_bs_tag, start, ctrl_offset,
238                     ctrl_val);
239
240 #if defined(SOC_MV_ORION)
241                 /*
242                  * Third MPP reg on Orion SoC is placed
243                  * non-linearly (with different offset).
244                  */
245                 if (i ==  (2 * MPP_PINS_PER_REG))
246                         ctrl_offset = 0x50;
247                 else
248 #endif
249                         ctrl_offset += 4;
250         }
251
252         return (0);
253 }
254
255 vm_offset_t
256 platform_lastaddr(void)
257 {
258
259         return (fdt_immr_va);
260 }
261
262 void
263 platform_probe_and_attach(void)
264 {
265
266         if (fdt_immr_addr(MV_BASE) != 0)
267                 while (1);
268 }
269
270 void
271 platform_gpio_init(void)
272 {
273
274         /*
275          * Re-initialise MPP. It is important to call this prior to using
276          * console as the physical connection can be routed via MPP.
277          */
278         if (platform_mpp_init() != 0)
279                 while (1);
280 }
281
282 void
283 platform_late_init(void)
284 {
285         /*
286          * Re-initialise decode windows
287          */
288         if (soc_decode_win() != 0)
289                 printf("WARNING: could not re-initialise decode windows! "
290                     "Running with existing settings...\n");
291 #if defined(SOC_MV_ARMADAXP)
292 #if !defined(SMP)
293         /* For SMP case it should be initialized after APs are booted */
294         armadaxp_init_coher_fabric();
295 #endif
296         armadaxp_l2_init();
297 #endif
298
299 #if defined(SOC_MV_ARMADA38X)
300         /* Configure timers' base frequency */
301         arm_tmr_change_frequency(get_cpu_freq() / 2);
302
303         /*
304          * Workaround for Marvell Armada38X family HW issue
305          * between Cortex-A9 CPUs and on-chip devices that may
306          * cause hang on heavy load.
307          * To avoid that, map all registers including PCIe IO
308          * as strongly ordered instead of device memory.
309          */
310         pmap_remap_vm_attr(VM_MEMATTR_DEVICE, VM_MEMATTR_SO);
311
312         /* Set IO Sync Barrier bit for all Mbus devices */
313         if (armada38x_win_set_iosync_barrier() != 0)
314                 printf("WARNING: could not map CPU Subsystem registers\n");
315         if (armada38x_mbus_optimization() != 0)
316                 printf("WARNING: could not enable mbus optimization\n");
317         if (armada38x_scu_enable() != 0)
318                 printf("WARNING: could not enable SCU\n");
319 #ifdef SMP
320         /* Open window to bootROM memory - needed for SMP */
321         if (armada38x_open_bootrom_win() != 0)
322                 printf("WARNING: could not open window to bootROM\n");
323 #endif
324 #endif
325 }
326
327 #define FDT_DEVMAP_MAX  (MV_WIN_CPU_MAX + 2)
328 static struct devmap_entry fdt_devmap[FDT_DEVMAP_MAX] = {
329         { 0, 0, 0, }
330 };
331
332 static int
333 platform_sram_devmap(struct devmap_entry *map)
334 {
335 #if !defined(SOC_MV_ARMADAXP) && !defined(SOC_MV_ARMADA38X)
336         phandle_t child, root;
337         u_long base, size;
338         /*
339          * SRAM range.
340          */
341         if ((child = OF_finddevice("/sram")) != 0)
342                 if (ofw_bus_node_is_compatible(child, "mrvl,cesa-sram") ||
343                     ofw_bus_node_is_compatible(child, "mrvl,scratchpad"))
344                         goto moveon;
345
346         if ((root = OF_finddevice("/")) == 0)
347                 return (ENXIO);
348
349         if ((child = fdt_find_compatible(root, "mrvl,cesa-sram", 0)) == 0 &&
350             (child = fdt_find_compatible(root, "mrvl,scratchpad", 0)) == 0)
351                         goto out;
352
353 moveon:
354         if (fdt_regsize(child, &base, &size) != 0)
355                 return (EINVAL);
356
357         map->pd_va = MV_CESA_SRAM_BASE; /* XXX */
358         map->pd_pa = base;
359         map->pd_size = size;
360
361         return (0);
362 out:
363 #endif
364         return (ENOENT);
365
366 }
367
368 /*
369  * Supply a default do-nothing implementation of mv_pci_devmap() via a weak
370  * alias.  Many Marvell platforms don't support a PCI interface, but to support
371  * those that do, we end up with a reference to this function below, in
372  * platform_devmap_init().  If "device pci" appears in the kernel config, the
373  * real implementation of this function in arm/mv/mv_pci.c overrides the weak
374  * alias defined here.
375  */
376 int mv_default_fdt_pci_devmap(phandle_t node, struct devmap_entry *devmap,
377     vm_offset_t io_va, vm_offset_t mem_va);
378 int
379 mv_default_fdt_pci_devmap(phandle_t node, struct devmap_entry *devmap,
380     vm_offset_t io_va, vm_offset_t mem_va)
381 {
382
383         return (0);
384 }
385 __weak_reference(mv_default_fdt_pci_devmap, mv_pci_devmap);
386
387 /*
388  * XXX: When device entry in devmap has pd_size smaller than section size,
389  * system will freeze during initialization
390  */
391
392 /*
393  * Construct devmap table with DT-derived config data.
394  */
395 int
396 platform_devmap_init(void)
397 {
398         phandle_t root, child;
399         pcell_t bank_count;
400         int i, num_mapped;
401
402         i = 0;
403         devmap_register_table(&fdt_devmap[0]);
404
405 #ifdef SOC_MV_ARMADAXP
406         vm_paddr_t cur_immr_pa;
407
408         /*
409          * Acquire SoC registers' base passed by u-boot and fill devmap
410          * accordingly. DTB is going to be modified basing on this data
411          * later.
412          */
413         __asm __volatile("mrc p15, 4, %0, c15, c0, 0" : "=r" (cur_immr_pa));
414         cur_immr_pa = (cur_immr_pa << 13) & 0xff000000;
415         if (cur_immr_pa != 0)
416                 fdt_immr_pa = cur_immr_pa;
417 #endif
418         /*
419          * IMMR range.
420          */
421         fdt_devmap[i].pd_va = fdt_immr_va;
422         fdt_devmap[i].pd_pa = fdt_immr_pa;
423         fdt_devmap[i].pd_size = fdt_immr_size;
424         i++;
425
426         /*
427          * SRAM range.
428          */
429         if (i < FDT_DEVMAP_MAX)
430                 if (platform_sram_devmap(&fdt_devmap[i]) == 0)
431                         i++;
432
433         /*
434          * PCI range(s).
435          * PCI range(s) and localbus.
436          */
437         if ((root = OF_finddevice("/")) == -1)
438                 return (ENXIO);
439         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
440                 if (fdt_is_type(child, "pci") || fdt_is_type(child, "pciep")) {
441                         /*
442                          * Check space: each PCI node will consume 2 devmap
443                          * entries.
444                          */
445                         if (i + 1 >= FDT_DEVMAP_MAX)
446                                 return (ENOMEM);
447
448                         /*
449                          * XXX this should account for PCI and multiple ranges
450                          * of a given kind.
451                          */
452                         if (mv_pci_devmap(child, &fdt_devmap[i], MV_PCI_VA_IO_BASE,
453                                     MV_PCI_VA_MEM_BASE) != 0)
454                                 return (ENXIO);
455                         i += 2;
456                 }
457
458                 if (ofw_bus_node_is_compatible(child, "mrvl,lbc")) {
459                         /* Check available space */
460                         if (OF_getencprop(child, "bank-count", &bank_count,
461                             sizeof(bank_count)) <= 0)
462                                 /* If no property, use default value */
463                                 bank_count = 1;
464
465                         if ((i + bank_count) >= FDT_DEVMAP_MAX)
466                                 return (ENOMEM);
467
468                         /* Add all localbus ranges to device map */
469                         num_mapped = 0;
470
471                         if (fdt_localbus_devmap(child, &fdt_devmap[i],
472                             (int)bank_count, &num_mapped) != 0)
473                                 return (ENXIO);
474
475                         i += num_mapped;
476                 }
477         }
478
479         return (0);
480 }
481
482 #if __ARM_ARCH < 6
483 struct arm32_dma_range *
484 bus_dma_get_range(void)
485 {
486
487         return (NULL);
488 }
489
490 int
491 bus_dma_get_range_nb(void)
492 {
493
494         return (0);
495 }
496 #endif
497
498 #if defined(CPU_MV_PJ4B)
499 #ifdef DDB
500 #include <ddb/ddb.h>
501
502 DB_SHOW_COMMAND(cp15, db_show_cp15)
503 {
504         u_int reg;
505
506         __asm __volatile("mrc p15, 0, %0, c0, c0, 0" : "=r" (reg));
507         db_printf("Cpu ID: 0x%08x\n", reg);
508         __asm __volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (reg));
509         db_printf("Current Cache Lvl ID: 0x%08x\n",reg);
510
511         reg = cp15_sctlr_get();
512         db_printf("Ctrl: 0x%08x\n",reg);
513         reg = cp15_actlr_get();
514         db_printf("Aux Ctrl: 0x%08x\n",reg);
515
516         __asm __volatile("mrc p15, 0, %0, c0, c1, 0" : "=r" (reg));
517         db_printf("Processor Feat 0: 0x%08x\n", reg);
518         __asm __volatile("mrc p15, 0, %0, c0, c1, 1" : "=r" (reg));
519         db_printf("Processor Feat 1: 0x%08x\n", reg);
520         __asm __volatile("mrc p15, 0, %0, c0, c1, 2" : "=r" (reg));
521         db_printf("Debug Feat 0: 0x%08x\n", reg);
522         __asm __volatile("mrc p15, 0, %0, c0, c1, 3" : "=r" (reg));
523         db_printf("Auxiliary Feat 0: 0x%08x\n", reg);
524         __asm __volatile("mrc p15, 0, %0, c0, c1, 4" : "=r" (reg));
525         db_printf("Memory Model Feat 0: 0x%08x\n", reg);
526         __asm __volatile("mrc p15, 0, %0, c0, c1, 5" : "=r" (reg));
527         db_printf("Memory Model Feat 1: 0x%08x\n", reg);
528         __asm __volatile("mrc p15, 0, %0, c0, c1, 6" : "=r" (reg));
529         db_printf("Memory Model Feat 2: 0x%08x\n", reg);
530         __asm __volatile("mrc p15, 0, %0, c0, c1, 7" : "=r" (reg));
531         db_printf("Memory Model Feat 3: 0x%08x\n", reg);
532
533         __asm __volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (reg));
534         db_printf("Aux Func Modes Ctrl 0: 0x%08x\n",reg);
535         __asm __volatile("mrc p15, 1, %0, c15, c2, 1" : "=r" (reg));
536         db_printf("Aux Func Modes Ctrl 1: 0x%08x\n",reg);
537
538         __asm __volatile("mrc p15, 1, %0, c15, c12, 0" : "=r" (reg));
539         db_printf("CPU ID code extension: 0x%08x\n",reg);
540 }
541
542 DB_SHOW_COMMAND(vtop, db_show_vtop)
543 {
544         u_int reg;
545
546         if (have_addr) {
547                 __asm __volatile("mcr p15, 0, %0, c7, c8, 0" : : "r" (addr));
548                 __asm __volatile("mrc p15, 0, %0, c7, c4, 0" : "=r" (reg));
549                 db_printf("Physical address reg: 0x%08x\n",reg);
550         } else
551                 db_printf("show vtop <virt_addr>\n");
552 }
553 #endif /* DDB */
554 #endif /* CPU_MV_PJ4B */
555