blob: c4cc11f1de31c66d7f0fa0078134ae369635f227 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvanibfcef282016-05-08 08:30:16 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
Jerome Brunet33e33782018-10-05 17:00:37 +02004 * (C) Copyright 2018 Neil Armstrong <narmstrong@baylibre.com>
Beniamino Galvanibfcef282016-05-08 08:30:16 +02005 */
6
7#include <common.h>
Simon Glass9b4a2052019-12-28 10:45:05 -07008#include <init.h>
Simon Glass90526e92020-05-10 11:39:56 -06009#include <net.h>
Neil Armstrongd96a7822018-07-27 14:10:00 +020010#include <asm/arch/boot.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020011#include <asm/arch/eth.h>
Neil Armstrongf0f37622018-04-11 17:13:45 +020012#include <asm/arch/gx.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020013#include <asm/arch/mem.h>
Maxime Jourdan0cc53fa2018-12-11 12:52:04 +010014#include <asm/arch/meson-vpu.h>
Neil Armstrongc7be3e52017-11-27 10:35:46 +010015#include <asm/io.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020016#include <asm/armv8/mmu.h>
17#include <linux/sizes.h>
Neil Armstrongdc999e52020-03-30 11:27:25 +020018#include <usb.h>
19#include <linux/usb/otg.h>
20#include <asm/arch/usb-gx.h>
21#include <usb/dwc2_udc.h>
22#include <clk.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020023#include <phy.h>
Beniamino Galvanibfcef282016-05-08 08:30:16 +020024
25DECLARE_GLOBAL_DATA_PTR;
26
Neil Armstrongd96a7822018-07-27 14:10:00 +020027int meson_get_boot_device(void)
28{
29 return readl(GX_AO_SEC_GP_CFG0) & GX_AO_BOOT_DEVICE;
30}
31
Jerome Brunet33e33782018-10-05 17:00:37 +020032/* Configure the reserved memory zones exported by the secure registers
33 * into EFI and DTB reserved memory entries.
34 */
35void meson_init_reserved_memory(void *fdt)
Neil Armstrongc7be3e52017-11-27 10:35:46 +010036{
37 u64 bl31_size, bl31_start;
38 u64 bl32_size, bl32_start;
39 u32 reg;
40
41 /*
42 * Get ARM Trusted Firmware reserved memory zones in :
43 * - AO_SEC_GP_CFG3: bl32 & bl31 size in KiB, can be 0
44 * - AO_SEC_GP_CFG5: bl31 physical start address, can be NULL
45 * - AO_SEC_GP_CFG4: bl32 physical start address, can be NULL
46 */
Neil Armstrongf0f37622018-04-11 17:13:45 +020047 reg = readl(GX_AO_SEC_GP_CFG3);
Neil Armstrongc7be3e52017-11-27 10:35:46 +010048
Neil Armstrongf0f37622018-04-11 17:13:45 +020049 bl31_size = ((reg & GX_AO_BL31_RSVMEM_SIZE_MASK)
50 >> GX_AO_BL31_RSVMEM_SIZE_SHIFT) * SZ_1K;
51 bl32_size = (reg & GX_AO_BL32_RSVMEM_SIZE_MASK) * SZ_1K;
Neil Armstrongc7be3e52017-11-27 10:35:46 +010052
Neil Armstrongf0f37622018-04-11 17:13:45 +020053 bl31_start = readl(GX_AO_SEC_GP_CFG5);
54 bl32_start = readl(GX_AO_SEC_GP_CFG4);
Neil Armstrongc7be3e52017-11-27 10:35:46 +010055
56 /*
Neil Armstrongf0f37622018-04-11 17:13:45 +020057 * Early Meson GX Firmware revisions did not provide the reserved
Neil Armstrongc7be3e52017-11-27 10:35:46 +010058 * memory zones in the registers, keep fixed memory zone handling.
59 */
Neil Armstrongf0f37622018-04-11 17:13:45 +020060 if (IS_ENABLED(CONFIG_MESON_GX) &&
Neil Armstrongc7be3e52017-11-27 10:35:46 +010061 !reg && !bl31_start && !bl32_start) {
62 bl31_start = 0x10000000;
63 bl31_size = 0x200000;
64 }
65
66 /* Add first 16MiB reserved zone */
Neil Armstrongf0f37622018-04-11 17:13:45 +020067 meson_board_add_reserved_memory(fdt, 0, GX_FIRMWARE_MEM_SIZE);
Neil Armstrongc7be3e52017-11-27 10:35:46 +010068
69 /* Add BL31 reserved zone */
70 if (bl31_start && bl31_size)
71 meson_board_add_reserved_memory(fdt, bl31_start, bl31_size);
72
73 /* Add BL32 reserved zone */
74 if (bl32_start && bl32_size)
75 meson_board_add_reserved_memory(fdt, bl32_start, bl32_size);
Maxime Jourdan0cc53fa2018-12-11 12:52:04 +010076
77#if defined(CONFIG_VIDEO_MESON)
78 meson_vpu_rsv_fb(fdt);
79#endif
Beniamino Galvanibfcef282016-05-08 08:30:16 +020080}
81
Jerome Brunet33e33782018-10-05 17:00:37 +020082phys_size_t get_effective_memsize(void)
Beniamino Galvanibfcef282016-05-08 08:30:16 +020083{
Jerome Brunet33e33782018-10-05 17:00:37 +020084 /* Size is reported in MiB, convert it in bytes */
85 return ((readl(GX_AO_SEC_GP_CFG0) & GX_AO_MEM_SIZE_MASK)
86 >> GX_AO_MEM_SIZE_SHIFT) * SZ_1M;
Beniamino Galvanibfcef282016-05-08 08:30:16 +020087}
88
Neil Armstrongf0f37622018-04-11 17:13:45 +020089static struct mm_region gx_mem_map[] = {
Beniamino Galvanibfcef282016-05-08 08:30:16 +020090 {
York Suncd4b0c52016-06-24 16:46:22 -070091 .virt = 0x0UL,
92 .phys = 0x0UL,
Loic Devulderc45414b2018-09-25 16:30:35 +020093 .size = 0xc0000000UL,
Beniamino Galvanibfcef282016-05-08 08:30:16 +020094 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
95 PTE_BLOCK_INNER_SHARE
96 }, {
Loic Devulderc45414b2018-09-25 16:30:35 +020097 .virt = 0xc0000000UL,
98 .phys = 0xc0000000UL,
99 .size = 0x30000000UL,
Beniamino Galvanibfcef282016-05-08 08:30:16 +0200100 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
101 PTE_BLOCK_NON_SHARE |
102 PTE_BLOCK_PXN | PTE_BLOCK_UXN
103 }, {
104 /* List terminator */
105 0,
106 }
107};
108
Neil Armstrongf0f37622018-04-11 17:13:45 +0200109struct mm_region *mem_map = gx_mem_map;
Jerome Brunet33e33782018-10-05 17:00:37 +0200110
111/* Configure the Ethernet MAC with the requested interface mode
112 * with some optional flags.
113 */
114void meson_eth_init(phy_interface_t mode, unsigned int flags)
115{
116 switch (mode) {
117 case PHY_INTERFACE_MODE_RGMII:
118 case PHY_INTERFACE_MODE_RGMII_ID:
119 case PHY_INTERFACE_MODE_RGMII_RXID:
120 case PHY_INTERFACE_MODE_RGMII_TXID:
121 /* Set RGMII mode */
122 setbits_le32(GX_ETH_REG_0, GX_ETH_REG_0_PHY_INTF |
123 GX_ETH_REG_0_TX_PHASE(1) |
124 GX_ETH_REG_0_TX_RATIO(4) |
125 GX_ETH_REG_0_PHY_CLK_EN |
126 GX_ETH_REG_0_CLK_EN);
Neil Armstrong407544c2019-05-28 13:13:19 +0200127
128 /* Reset to external PHY */
129 if(!IS_ENABLED(CONFIG_MESON_GXBB))
130 writel(0x2009087f, GX_ETH_REG_3);
131
Jerome Brunet33e33782018-10-05 17:00:37 +0200132 break;
133
134 case PHY_INTERFACE_MODE_RMII:
135 /* Set RMII mode */
136 out_le32(GX_ETH_REG_0, GX_ETH_REG_0_INVERT_RMII_CLK |
137 GX_ETH_REG_0_CLK_EN);
138
Neil Armstrong407544c2019-05-28 13:13:19 +0200139 /* Use GXL RMII Internal PHY (also on GXM) */
140 if (!IS_ENABLED(CONFIG_MESON_GXBB)) {
141 if ((flags & MESON_USE_INTERNAL_RMII_PHY)) {
142 writel(0x10110181, GX_ETH_REG_2);
143 writel(0xe40908ff, GX_ETH_REG_3);
144 } else
145 writel(0x2009087f, GX_ETH_REG_3);
Jerome Brunet33e33782018-10-05 17:00:37 +0200146 }
147
148 break;
149
150 default:
151 printf("Invalid Ethernet interface mode\n");
152 return;
153 }
154
155 /* Enable power gate */
156 clrbits_le32(GX_MEM_PD_REG_0, GX_MEM_PD_REG_0_ETH_MASK);
157}
Neil Armstrongdc999e52020-03-30 11:27:25 +0200158
159#if CONFIG_IS_ENABLED(USB_XHCI_DWC3_OF_SIMPLE) && \
160 CONFIG_IS_ENABLED(USB_GADGET_DWC2_OTG)
161static struct dwc2_plat_otg_data meson_gx_dwc2_data;
162static struct phy usb_phys[2];
163
164int board_usb_init(int index, enum usb_init_type init)
165{
166 struct ofnode_phandle_args args;
167 struct udevice *clk_dev;
168 ofnode dwc2_node;
169 struct clk clk;
170 int ret, i;
171 u32 val;
172
173 /* find the dwc2 node */
174 dwc2_node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
175 if (!ofnode_valid(dwc2_node)) {
176 debug("Not found dwc2 node\n");
177 return -ENODEV;
178 }
179
180 if (!ofnode_is_available(dwc2_node)) {
181 debug("dwc2 is disabled in the device tree\n");
182 return -ENODEV;
183 }
184
185 /* get the PHYs */
186 for (i = 0; i < 2; i++) {
Jagan Teki5a2b6772020-05-01 23:44:18 +0530187 ret = generic_phy_get_by_index_nodev(dwc2_node, i,
188 &usb_phys[i]);
Neil Armstrongdc999e52020-03-30 11:27:25 +0200189 if (ret && ret != -ENOENT) {
190 pr_err("Failed to get USB PHY%d for %s\n",
191 i, ofnode_get_name(dwc2_node));
192 return ret;
193 }
194 }
195
196 for (i = 0; i < 2; i++) {
197 ret = generic_phy_init(&usb_phys[i]);
198 if (ret) {
199 pr_err("Can't init USB PHY%d for %s\n",
200 i, ofnode_get_name(dwc2_node));
201 return ret;
202 }
203 }
204
205 for (i = 0; i < 2; i++) {
206 ret = generic_phy_power_on(&usb_phys[i]);
207 if (ret) {
208 pr_err("Can't power USB PHY%d for %s\n",
209 i, ofnode_get_name(dwc2_node));
210 return ret;
211 }
212 }
213
214 phy_meson_gxl_usb3_set_mode(&usb_phys[0], USB_DR_MODE_PERIPHERAL);
215 phy_meson_gxl_usb2_set_mode(&usb_phys[1], USB_DR_MODE_PERIPHERAL);
216
217 meson_gx_dwc2_data.regs_otg = ofnode_get_addr(dwc2_node);
218 if (meson_gx_dwc2_data.regs_otg == FDT_ADDR_T_NONE) {
219 debug("usbotg: can't get base address\n");
220 return -ENODATA;
221 }
222
223 /* Enable clock */
224 ret = ofnode_parse_phandle_with_args(dwc2_node, "clocks",
225 "#clock-cells", 0, 0, &args);
226 if (ret) {
227 debug("usbotg has no clocks defined in the device tree\n");
228 return ret;
229 }
230
231 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &clk_dev);
232 if (ret)
233 return ret;
234
235 if (args.args_count != 1) {
236 debug("Can't find clock ID in the device tree\n");
237 return -ENODATA;
238 }
239
240 clk.dev = clk_dev;
241 clk.id = args.args[0];
242
243 ret = clk_enable(&clk);
244 if (ret) {
245 debug("Failed to enable usbotg clock\n");
246 return ret;
247 }
248
249 ofnode_read_u32(dwc2_node, "g-rx-fifo-size", &val);
250 meson_gx_dwc2_data.rx_fifo_sz = val;
251 ofnode_read_u32(dwc2_node, "g-np-tx-fifo-size", &val);
252 meson_gx_dwc2_data.np_tx_fifo_sz = val;
253 ofnode_read_u32(dwc2_node, "g-tx-fifo-size", &val);
254 meson_gx_dwc2_data.tx_fifo_sz = val;
255
256 return dwc2_udc_probe(&meson_gx_dwc2_data);
257}
258
259int board_usb_cleanup(int index, enum usb_init_type init)
260{
261 int i;
262
263 phy_meson_gxl_usb3_set_mode(&usb_phys[0], USB_DR_MODE_HOST);
264 phy_meson_gxl_usb2_set_mode(&usb_phys[1], USB_DR_MODE_HOST);
265
266 for (i = 0; i < 2; i++)
267 usb_phys[i].dev = NULL;
268
269 return 0;
270}
271#endif