24786df6cdd1142f985b7fc2dad1e01ae5ea9f95
[platform/kernel/u-boot.git] / arch / arm / mach-meson / board-g12a.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4  * (C) Copyright 2018 Neil Armstrong <narmstrong@baylibre.com>
5  */
6
7 #include <common.h>
8 #include <asm/arch/boot.h>
9 #include <asm/arch/eth.h>
10 #include <asm/arch/g12a.h>
11 #include <asm/arch/mem.h>
12 #include <asm/arch/meson-vpu.h>
13 #include <asm/io.h>
14 #include <asm/armv8/mmu.h>
15 #include <linux/sizes.h>
16 #include <usb.h>
17 #include <linux/usb/otg.h>
18 #include <asm/arch/usb.h>
19 #include <usb/dwc2_udc.h>
20 #include <phy.h>
21 #include <clk.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 int meson_get_boot_device(void)
26 {
27         return readl(G12A_AO_SEC_GP_CFG0) & G12A_AO_BOOT_DEVICE;
28 }
29
30 /* Configure the reserved memory zones exported by the secure registers
31  * into EFI and DTB reserved memory entries.
32  */
33 void meson_init_reserved_memory(void *fdt)
34 {
35         u64 bl31_size, bl31_start;
36         u64 bl32_size, bl32_start;
37         u32 reg;
38
39         /*
40          * Get ARM Trusted Firmware reserved memory zones in :
41          * - AO_SEC_GP_CFG3: bl32 & bl31 size in KiB, can be 0
42          * - AO_SEC_GP_CFG5: bl31 physical start address, can be NULL
43          * - AO_SEC_GP_CFG4: bl32 physical start address, can be NULL
44          */
45         reg = readl(G12A_AO_SEC_GP_CFG3);
46
47         bl31_size = ((reg & G12A_AO_BL31_RSVMEM_SIZE_MASK)
48                         >> G12A_AO_BL31_RSVMEM_SIZE_SHIFT) * SZ_1K;
49         bl32_size = (reg & G12A_AO_BL32_RSVMEM_SIZE_MASK) * SZ_1K;
50
51         bl31_start = readl(G12A_AO_SEC_GP_CFG5);
52         bl32_start = readl(G12A_AO_SEC_GP_CFG4);
53
54         /* Add BL31 reserved zone */
55         if (bl31_start && bl31_size)
56                 meson_board_add_reserved_memory(fdt, bl31_start, bl31_size);
57
58         /* Add BL32 reserved zone */
59         if (bl32_start && bl32_size)
60                 meson_board_add_reserved_memory(fdt, bl32_start, bl32_size);
61
62 #if defined(CONFIG_VIDEO_MESON)
63         meson_vpu_rsv_fb(fdt);
64 #endif
65 }
66
67 phys_size_t get_effective_memsize(void)
68 {
69         /* Size is reported in MiB, convert it in bytes */
70         return min(((readl(G12A_AO_SEC_GP_CFG0) & G12A_AO_MEM_SIZE_MASK)
71                         >> G12A_AO_MEM_SIZE_SHIFT) * SZ_1M, 0xf5000000);
72 }
73
74 static struct mm_region g12a_mem_map[] = {
75         {
76                 .virt = 0x0UL,
77                 .phys = 0x0UL,
78                 .size = 0xf5000000UL,
79                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
80                          PTE_BLOCK_INNER_SHARE
81         }, {
82                 .virt = 0xf5000000UL,
83                 .phys = 0xf5000000UL,
84                 .size = 0x0b000000UL,
85                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
86                          PTE_BLOCK_NON_SHARE |
87                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
88         }, {
89                 /* List terminator */
90                 0,
91         }
92 };
93
94 struct mm_region *mem_map = g12a_mem_map;
95
96 static void g12a_enable_external_mdio(void)
97 {
98         writel(0x0, ETH_PHY_CNTL2);
99 }
100
101 static void g12a_enable_internal_mdio(void)
102 {
103         /* Fire up the PHY PLL */
104         writel(0x29c0040a, ETH_PLL_CNTL0);
105         writel(0x927e0000, ETH_PLL_CNTL1);
106         writel(0xac5f49e5, ETH_PLL_CNTL2);
107         writel(0x00000000, ETH_PLL_CNTL3);
108         writel(0x00000000, ETH_PLL_CNTL4);
109         writel(0x20200000, ETH_PLL_CNTL5);
110         writel(0x0000c002, ETH_PLL_CNTL6);
111         writel(0x00000023, ETH_PLL_CNTL7);
112         writel(0x39c0040a, ETH_PLL_CNTL0);
113         writel(0x19c0040a, ETH_PLL_CNTL0);
114
115         /* Select the internal MDIO */
116         writel(0x33000180, ETH_PHY_CNTL0);
117         writel(0x00074043, ETH_PHY_CNTL1);
118         writel(0x00000260, ETH_PHY_CNTL2);
119 }
120
121 /* Configure the Ethernet MAC with the requested interface mode
122  * with some optional flags.
123  */
124 void meson_eth_init(phy_interface_t mode, unsigned int flags)
125 {
126         switch (mode) {
127         case PHY_INTERFACE_MODE_RGMII:
128         case PHY_INTERFACE_MODE_RGMII_ID:
129         case PHY_INTERFACE_MODE_RGMII_RXID:
130         case PHY_INTERFACE_MODE_RGMII_TXID:
131                 /* Set RGMII mode */
132                 setbits_le32(G12A_ETH_REG_0, G12A_ETH_REG_0_PHY_INTF_RGMII |
133                              G12A_ETH_REG_0_TX_PHASE(1) |
134                              G12A_ETH_REG_0_TX_RATIO(4) |
135                              G12A_ETH_REG_0_PHY_CLK_EN |
136                              G12A_ETH_REG_0_CLK_EN);
137                 g12a_enable_external_mdio();
138                 break;
139
140         case PHY_INTERFACE_MODE_RMII:
141                 /* Set RMII mode */
142                 out_le32(G12A_ETH_REG_0, G12A_ETH_REG_0_PHY_INTF_RMII |
143                                         G12A_ETH_REG_0_INVERT_RMII_CLK |
144                                         G12A_ETH_REG_0_CLK_EN);
145
146                 /* Use G12A RMII Internal PHY */
147                 if (flags & MESON_USE_INTERNAL_RMII_PHY)
148                         g12a_enable_internal_mdio();
149                 else
150                         g12a_enable_external_mdio();
151
152                 break;
153
154         default:
155                 printf("Invalid Ethernet interface mode\n");
156                 return;
157         }
158
159         /* Enable power gate */
160         clrbits_le32(G12A_MEM_PD_REG_0, G12A_MEM_PD_REG_0_ETH_MASK);
161 }
162
163 #if CONFIG_IS_ENABLED(USB_DWC3_MESON_G12A) && \
164         CONFIG_IS_ENABLED(USB_GADGET_DWC2_OTG)
165 static struct dwc2_plat_otg_data meson_g12a_dwc2_data;
166
167 int board_usb_init(int index, enum usb_init_type init)
168 {
169         struct fdtdec_phandle_args args;
170         const void *blob = gd->fdt_blob;
171         int node, dwc2_node;
172         struct udevice *dev, *clk_dev;
173         struct clk clk;
174         int ret;
175
176         /* find the usb glue node */
177         node = fdt_node_offset_by_compatible(blob, -1,
178                                              "amlogic,meson-g12a-usb-ctrl");
179         if (node < 0) {
180                 debug("Not found usb-control node\n");
181                 return -ENODEV;
182         }
183
184         if (!fdtdec_get_is_enabled(blob, node)) {
185                 debug("usb is disabled in the device tree\n");
186                 return -ENODEV;
187         }
188
189         ret = uclass_get_device_by_of_offset(UCLASS_SIMPLE_BUS, node, &dev);
190         if (ret) {
191                 debug("Not found usb-control device\n");
192                 return ret;
193         }
194
195         /* find the dwc2 node */
196         dwc2_node = fdt_node_offset_by_compatible(blob, node,
197                                                   "amlogic,meson-g12a-usb");
198         if (dwc2_node < 0) {
199                 debug("Not found dwc2 node\n");
200                 return -ENODEV;
201         }
202
203         if (!fdtdec_get_is_enabled(blob, dwc2_node)) {
204                 debug("dwc2 is disabled in the device tree\n");
205                 return -ENODEV;
206         }
207
208         meson_g12a_dwc2_data.regs_otg = fdtdec_get_addr(blob, dwc2_node, "reg");
209         if (meson_g12a_dwc2_data.regs_otg == FDT_ADDR_T_NONE) {
210                 debug("usbotg: can't get base address\n");
211                 return -ENODATA;
212         }
213
214         /* Enable clock */
215         ret = fdtdec_parse_phandle_with_args(blob, dwc2_node, "clocks",
216                                              "#clock-cells", 0, 0, &args);
217         if (ret) {
218                 debug("usbotg has no clocks defined in the device tree\n");
219                 return ret;
220         }
221
222         ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, &clk_dev);
223         if (ret)
224                 return ret;
225
226         if (args.args_count != 1) {
227                 debug("Can't find clock ID in the device tree\n");
228                 return -ENODATA;
229         }
230
231         clk.dev = clk_dev;
232         clk.id = args.args[0];
233
234         ret = clk_enable(&clk);
235         if (ret) {
236                 debug("Failed to enable usbotg clock\n");
237                 return ret;
238         }
239
240         meson_g12a_dwc2_data.rx_fifo_sz = fdtdec_get_int(blob, dwc2_node,
241                                                      "g-rx-fifo-size", 0);
242         meson_g12a_dwc2_data.np_tx_fifo_sz = fdtdec_get_int(blob, dwc2_node,
243                                                         "g-np-tx-fifo-size", 0);
244         meson_g12a_dwc2_data.tx_fifo_sz = fdtdec_get_int(blob, dwc2_node,
245                                                      "g-tx-fifo-size", 0);
246
247         /* Switch to peripheral mode */
248         ret = dwc3_meson_g12a_force_mode(dev, USB_DR_MODE_PERIPHERAL);
249         if (ret)
250                 return ret;
251
252         return dwc2_udc_probe(&meson_g12a_dwc2_data);
253 }
254
255 int board_usb_cleanup(int index, enum usb_init_type init)
256 {
257         const void *blob = gd->fdt_blob;
258         struct udevice *dev;
259         int node;
260         int ret;
261
262         /* find the usb glue node */
263         node = fdt_node_offset_by_compatible(blob, -1,
264                                              "amlogic,meson-g12a-usb-ctrl");
265         if (node < 0)
266                 return -ENODEV;
267
268         if (!fdtdec_get_is_enabled(blob, node))
269                 return -ENODEV;
270
271         ret = uclass_get_device_by_of_offset(UCLASS_SIMPLE_BUS, node, &dev);
272         if (ret)
273                 return ret;
274
275         /* Switch to OTG mode */
276         ret = dwc3_meson_g12a_force_mode(dev, USB_DR_MODE_HOST);
277         if (ret)
278                 return ret;
279
280         return 0;
281 }
282 #endif