blob: 3da99017a5a51dd52fffcc00250b026f4054e2f1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvanid1037e42016-05-08 08:30:16 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
Jerome Brunetf897c4b2018-10-05 17:00:37 +02004 * (C) Copyright 2018 Neil Armstrong <narmstrong@baylibre.com>
Beniamino Galvanid1037e42016-05-08 08:30:16 +02005 */
6
7#include <common.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07008#include <init.h>
Neil Armstrong2fbfcbb2018-07-27 14:10:00 +02009#include <asm/arch/boot.h>
Jerome Brunetf897c4b2018-10-05 17:00:37 +020010#include <asm/arch/eth.h>
Neil Armstrong6e89d922018-04-11 17:13:45 +020011#include <asm/arch/gx.h>
Jerome Brunetf897c4b2018-10-05 17:00:37 +020012#include <asm/arch/mem.h>
Maxime Jourdan1be090a2018-12-11 12:52:04 +010013#include <asm/arch/meson-vpu.h>
Jerome Brunetf897c4b2018-10-05 17:00:37 +020014#include <asm/io.h>
Beniamino Galvanid1037e42016-05-08 08:30:16 +020015#include <asm/armv8/mmu.h>
Neil Armstrong8b245692017-11-27 10:35:46 +010016#include <linux/sizes.h>
Neil Armstrong70f92ac2020-03-30 11:27:25 +020017#include <usb.h>
18#include <linux/usb/otg.h>
19#include <asm/arch/usb-gx.h>
20#include <usb/dwc2_udc.h>
21#include <clk.h>
Jerome Brunetf897c4b2018-10-05 17:00:37 +020022#include <phy.h>
Beniamino Galvanid1037e42016-05-08 08:30:16 +020023
24DECLARE_GLOBAL_DATA_PTR;
25
Neil Armstrong2fbfcbb2018-07-27 14:10:00 +020026int meson_get_boot_device(void)
27{
28 return readl(GX_AO_SEC_GP_CFG0) & GX_AO_BOOT_DEVICE;
29}
30
Jerome Brunetf897c4b2018-10-05 17:00:37 +020031/* Configure the reserved memory zones exported by the secure registers
32 * into EFI and DTB reserved memory entries.
33 */
34void meson_init_reserved_memory(void *fdt)
Neil Armstrong8b245692017-11-27 10:35:46 +010035{
36 u64 bl31_size, bl31_start;
37 u64 bl32_size, bl32_start;
38 u32 reg;
39
40 /*
41 * Get ARM Trusted Firmware reserved memory zones in :
42 * - AO_SEC_GP_CFG3: bl32 & bl31 size in KiB, can be 0
43 * - AO_SEC_GP_CFG5: bl31 physical start address, can be NULL
44 * - AO_SEC_GP_CFG4: bl32 physical start address, can be NULL
45 */
Neil Armstrong6e89d922018-04-11 17:13:45 +020046 reg = readl(GX_AO_SEC_GP_CFG3);
Neil Armstrong8b245692017-11-27 10:35:46 +010047
Neil Armstrong6e89d922018-04-11 17:13:45 +020048 bl31_size = ((reg & GX_AO_BL31_RSVMEM_SIZE_MASK)
49 >> GX_AO_BL31_RSVMEM_SIZE_SHIFT) * SZ_1K;
50 bl32_size = (reg & GX_AO_BL32_RSVMEM_SIZE_MASK) * SZ_1K;
Neil Armstrong8b245692017-11-27 10:35:46 +010051
Neil Armstrong6e89d922018-04-11 17:13:45 +020052 bl31_start = readl(GX_AO_SEC_GP_CFG5);
53 bl32_start = readl(GX_AO_SEC_GP_CFG4);
Neil Armstrong8b245692017-11-27 10:35:46 +010054
55 /*
Neil Armstrong6e89d922018-04-11 17:13:45 +020056 * Early Meson GX Firmware revisions did not provide the reserved
Neil Armstrong8b245692017-11-27 10:35:46 +010057 * memory zones in the registers, keep fixed memory zone handling.
58 */
Neil Armstrong6e89d922018-04-11 17:13:45 +020059 if (IS_ENABLED(CONFIG_MESON_GX) &&
Neil Armstrong8b245692017-11-27 10:35:46 +010060 !reg && !bl31_start && !bl32_start) {
61 bl31_start = 0x10000000;
62 bl31_size = 0x200000;
63 }
64
65 /* Add first 16MiB reserved zone */
Neil Armstrong6e89d922018-04-11 17:13:45 +020066 meson_board_add_reserved_memory(fdt, 0, GX_FIRMWARE_MEM_SIZE);
Neil Armstrong8b245692017-11-27 10:35:46 +010067
68 /* Add BL31 reserved zone */
69 if (bl31_start && bl31_size)
70 meson_board_add_reserved_memory(fdt, bl31_start, bl31_size);
71
72 /* Add BL32 reserved zone */
73 if (bl32_start && bl32_size)
74 meson_board_add_reserved_memory(fdt, bl32_start, bl32_size);
Maxime Jourdan1be090a2018-12-11 12:52:04 +010075
76#if defined(CONFIG_VIDEO_MESON)
77 meson_vpu_rsv_fb(fdt);
78#endif
Beniamino Galvanid1037e42016-05-08 08:30:16 +020079}
80
Jerome Brunetf897c4b2018-10-05 17:00:37 +020081phys_size_t get_effective_memsize(void)
Beniamino Galvanid1037e42016-05-08 08:30:16 +020082{
Jerome Brunetf897c4b2018-10-05 17:00:37 +020083 /* Size is reported in MiB, convert it in bytes */
84 return ((readl(GX_AO_SEC_GP_CFG0) & GX_AO_MEM_SIZE_MASK)
85 >> GX_AO_MEM_SIZE_SHIFT) * SZ_1M;
Beniamino Galvanid1037e42016-05-08 08:30:16 +020086}
87
Neil Armstrong6e89d922018-04-11 17:13:45 +020088static struct mm_region gx_mem_map[] = {
Beniamino Galvanid1037e42016-05-08 08:30:16 +020089 {
York Sunc7104e52016-06-24 16:46:22 -070090 .virt = 0x0UL,
91 .phys = 0x0UL,
Loic Devulderc067c582018-09-25 16:30:35 +020092 .size = 0xc0000000UL,
Beniamino Galvanid1037e42016-05-08 08:30:16 +020093 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
94 PTE_BLOCK_INNER_SHARE
95 }, {
Loic Devulderc067c582018-09-25 16:30:35 +020096 .virt = 0xc0000000UL,
97 .phys = 0xc0000000UL,
98 .size = 0x30000000UL,
Beniamino Galvanid1037e42016-05-08 08:30:16 +020099 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
100 PTE_BLOCK_NON_SHARE |
101 PTE_BLOCK_PXN | PTE_BLOCK_UXN
102 }, {
103 /* List terminator */
104 0,
105 }
106};
107
Neil Armstrong6e89d922018-04-11 17:13:45 +0200108struct mm_region *mem_map = gx_mem_map;
Jerome Brunetf897c4b2018-10-05 17:00:37 +0200109
110/* Configure the Ethernet MAC with the requested interface mode
111 * with some optional flags.
112 */
113void meson_eth_init(phy_interface_t mode, unsigned int flags)
114{
115 switch (mode) {
116 case PHY_INTERFACE_MODE_RGMII:
117 case PHY_INTERFACE_MODE_RGMII_ID:
118 case PHY_INTERFACE_MODE_RGMII_RXID:
119 case PHY_INTERFACE_MODE_RGMII_TXID:
120 /* Set RGMII mode */
121 setbits_le32(GX_ETH_REG_0, GX_ETH_REG_0_PHY_INTF |
122 GX_ETH_REG_0_TX_PHASE(1) |
123 GX_ETH_REG_0_TX_RATIO(4) |
124 GX_ETH_REG_0_PHY_CLK_EN |
125 GX_ETH_REG_0_CLK_EN);
Neil Armstrong3968a692019-05-28 13:13:19 +0200126
127 /* Reset to external PHY */
128 if(!IS_ENABLED(CONFIG_MESON_GXBB))
129 writel(0x2009087f, GX_ETH_REG_3);
130
Jerome Brunetf897c4b2018-10-05 17:00:37 +0200131 break;
132
133 case PHY_INTERFACE_MODE_RMII:
134 /* Set RMII mode */
135 out_le32(GX_ETH_REG_0, GX_ETH_REG_0_INVERT_RMII_CLK |
136 GX_ETH_REG_0_CLK_EN);
137
Neil Armstrong3968a692019-05-28 13:13:19 +0200138 /* Use GXL RMII Internal PHY (also on GXM) */
139 if (!IS_ENABLED(CONFIG_MESON_GXBB)) {
140 if ((flags & MESON_USE_INTERNAL_RMII_PHY)) {
141 writel(0x10110181, GX_ETH_REG_2);
142 writel(0xe40908ff, GX_ETH_REG_3);
143 } else
144 writel(0x2009087f, GX_ETH_REG_3);
Jerome Brunetf897c4b2018-10-05 17:00:37 +0200145 }
146
147 break;
148
149 default:
150 printf("Invalid Ethernet interface mode\n");
151 return;
152 }
153
154 /* Enable power gate */
155 clrbits_le32(GX_MEM_PD_REG_0, GX_MEM_PD_REG_0_ETH_MASK);
156}
Neil Armstrong70f92ac2020-03-30 11:27:25 +0200157
158#if CONFIG_IS_ENABLED(USB_XHCI_DWC3_OF_SIMPLE) && \
159 CONFIG_IS_ENABLED(USB_GADGET_DWC2_OTG)
160static struct dwc2_plat_otg_data meson_gx_dwc2_data;
161static struct phy usb_phys[2];
162
163int board_usb_init(int index, enum usb_init_type init)
164{
165 struct ofnode_phandle_args args;
166 struct udevice *clk_dev;
167 ofnode dwc2_node;
168 struct clk clk;
169 int ret, i;
170 u32 val;
171
172 /* find the dwc2 node */
173 dwc2_node = ofnode_by_compatible(ofnode_null(), "snps,dwc2");
174 if (!ofnode_valid(dwc2_node)) {
175 debug("Not found dwc2 node\n");
176 return -ENODEV;
177 }
178
179 if (!ofnode_is_available(dwc2_node)) {
180 debug("dwc2 is disabled in the device tree\n");
181 return -ENODEV;
182 }
183
184 /* get the PHYs */
185 for (i = 0; i < 2; i++) {
186 ret = generic_phy_get_by_node(dwc2_node, i, &usb_phys[i]);
187 if (ret && ret != -ENOENT) {
188 pr_err("Failed to get USB PHY%d for %s\n",
189 i, ofnode_get_name(dwc2_node));
190 return ret;
191 }
192 }
193
194 for (i = 0; i < 2; i++) {
195 ret = generic_phy_init(&usb_phys[i]);
196 if (ret) {
197 pr_err("Can't init USB PHY%d for %s\n",
198 i, ofnode_get_name(dwc2_node));
199 return ret;
200 }
201 }
202
203 for (i = 0; i < 2; i++) {
204 ret = generic_phy_power_on(&usb_phys[i]);
205 if (ret) {
206 pr_err("Can't power USB PHY%d for %s\n",
207 i, ofnode_get_name(dwc2_node));
208 return ret;
209 }
210 }
211
212 phy_meson_gxl_usb3_set_mode(&usb_phys[0], USB_DR_MODE_PERIPHERAL);
213 phy_meson_gxl_usb2_set_mode(&usb_phys[1], USB_DR_MODE_PERIPHERAL);
214
215 meson_gx_dwc2_data.regs_otg = ofnode_get_addr(dwc2_node);
216 if (meson_gx_dwc2_data.regs_otg == FDT_ADDR_T_NONE) {
217 debug("usbotg: can't get base address\n");
218 return -ENODATA;
219 }
220
221 /* Enable clock */
222 ret = ofnode_parse_phandle_with_args(dwc2_node, "clocks",
223 "#clock-cells", 0, 0, &args);
224 if (ret) {
225 debug("usbotg has no clocks defined in the device tree\n");
226 return ret;
227 }
228
229 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &clk_dev);
230 if (ret)
231 return ret;
232
233 if (args.args_count != 1) {
234 debug("Can't find clock ID in the device tree\n");
235 return -ENODATA;
236 }
237
238 clk.dev = clk_dev;
239 clk.id = args.args[0];
240
241 ret = clk_enable(&clk);
242 if (ret) {
243 debug("Failed to enable usbotg clock\n");
244 return ret;
245 }
246
247 ofnode_read_u32(dwc2_node, "g-rx-fifo-size", &val);
248 meson_gx_dwc2_data.rx_fifo_sz = val;
249 ofnode_read_u32(dwc2_node, "g-np-tx-fifo-size", &val);
250 meson_gx_dwc2_data.np_tx_fifo_sz = val;
251 ofnode_read_u32(dwc2_node, "g-tx-fifo-size", &val);
252 meson_gx_dwc2_data.tx_fifo_sz = val;
253
254 return dwc2_udc_probe(&meson_gx_dwc2_data);
255}
256
257int board_usb_cleanup(int index, enum usb_init_type init)
258{
259 int i;
260
261 phy_meson_gxl_usb3_set_mode(&usb_phys[0], USB_DR_MODE_HOST);
262 phy_meson_gxl_usb2_set_mode(&usb_phys[1], USB_DR_MODE_HOST);
263
264 for (i = 0; i < 2; i++)
265 usb_phys[i].dev = NULL;
266
267 return 0;
268}
269#endif