1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Keystone2 based boards and SOC related code.
4  *
5  * Copyright 2013 Texas Instruments, Inc.
6  *	Cyril Chemparathy <cyril@ti.com>
7  *	Santosh Shilimkar <santosh.shillimkar@ti.com>
8  */
9 #include <linux/io.h>
10 #include <linux/of.h>
11 #include <linux/init.h>
12 #include <linux/of_platform.h>
13 #include <linux/of_address.h>
14 #include <linux/memblock.h>
15 
16 #include <asm/setup.h>
17 #include <asm/mach/map.h>
18 #include <asm/mach/arch.h>
19 #include <asm/mach/time.h>
20 #include <asm/smp_plat.h>
21 #include <asm/memory.h>
22 
23 #include "memory.h"
24 
25 #include "keystone.h"
26 
27 static unsigned long keystone_dma_pfn_offset __read_mostly;
28 
29 static int keystone_platform_notifier(struct notifier_block *nb,
30 				      unsigned long event, void *data)
31 {
32 	struct device *dev = data;
33 
34 	if (event != BUS_NOTIFY_ADD_DEVICE)
35 		return NOTIFY_DONE;
36 
37 	if (!dev)
38 		return NOTIFY_BAD;
39 
40 	if (!dev->of_node) {
41 		dev->dma_pfn_offset = keystone_dma_pfn_offset;
42 		dev_err(dev, "set dma_pfn_offset%08lx\n",
43 			dev->dma_pfn_offset);
44 	}
45 	return NOTIFY_OK;
46 }
47 
48 static struct notifier_block platform_nb = {
49 	.notifier_call = keystone_platform_notifier,
50 };
51 
52 static void __init keystone_init(void)
53 {
54 	if (PHYS_OFFSET >= KEYSTONE_HIGH_PHYS_START) {
55 		keystone_dma_pfn_offset = PFN_DOWN(KEYSTONE_HIGH_PHYS_START -
56 						   KEYSTONE_LOW_PHYS_START);
57 		bus_register_notifier(&platform_bus_type, &platform_nb);
58 	}
59 	keystone_pm_runtime_init();
60 }
61 
62 static long long __init keystone_pv_fixup(void)
63 {
64 	long long offset;
65 	phys_addr_t mem_start, mem_end;
66 
67 	mem_start = memblock_start_of_DRAM();
68 	mem_end = memblock_end_of_DRAM();
69 
70 	/* nothing to do if we are running out of the <32-bit space */
71 	if (mem_start >= KEYSTONE_LOW_PHYS_START &&
72 	    mem_end   <= KEYSTONE_LOW_PHYS_END)
73 		return 0;
74 
75 	if (mem_start < KEYSTONE_HIGH_PHYS_START ||
76 	    mem_end   > KEYSTONE_HIGH_PHYS_END) {
77 		pr_crit("Invalid address space for memory (%08llx-%08llx)\n",
78 		        (u64)mem_start, (u64)mem_end);
79 		return 0;
80 	}
81 
82 	offset = KEYSTONE_HIGH_PHYS_START - KEYSTONE_LOW_PHYS_START;
83 
84 	/* Populate the arch idmap hook */
85 	arch_phys_to_idmap_offset = -offset;
86 
87 	return offset;
88 }
89 
90 static const char *const keystone_match[] __initconst = {
91 	"ti,k2hk",
92 	"ti,k2e",
93 	"ti,k2l",
94 	"ti,k2g",
95 	"ti,keystone",
96 	NULL,
97 };
98 
99 DT_MACHINE_START(KEYSTONE, "Keystone")
100 #if defined(CONFIG_ZONE_DMA) && defined(CONFIG_ARM_LPAE)
101 	.dma_zone_size	= SZ_2G,
102 #endif
103 	.smp		= smp_ops(keystone_smp_ops),
104 	.init_machine	= keystone_init,
105 	.dt_compat	= keystone_match,
106 	.pv_fixup	= keystone_pv_fixup,
107 MACHINE_END
108