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