Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 4 | * (C) Copyright 2018 Neil Armstrong <narmstrong@baylibre.com> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 8e16b1e | 2019-12-28 10:45:05 -0700 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <net.h> |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 10 | #include <asm/arch/boot.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 11 | #include <asm/arch/eth.h> |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 12 | #include <asm/arch/gx.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 13 | #include <asm/arch/mem.h> |
Maxime Jourdan | 1be090a | 2018-12-11 12:52:04 +0100 | [diff] [blame] | 14 | #include <asm/arch/meson-vpu.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 16 | #include <asm/armv8/mmu.h> |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 17 | #include <linux/sizes.h> |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 18 | #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 Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 23 | #include <phy.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 27 | int meson_get_boot_device(void) |
| 28 | { |
| 29 | return readl(GX_AO_SEC_GP_CFG0) & GX_AO_BOOT_DEVICE; |
| 30 | } |
| 31 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 32 | /* Configure the reserved memory zones exported by the secure registers |
| 33 | * into EFI and DTB reserved memory entries. |
| 34 | */ |
| 35 | void meson_init_reserved_memory(void *fdt) |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 36 | { |
| 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 Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 47 | reg = readl(GX_AO_SEC_GP_CFG3); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 48 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 49 | 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 Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 52 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 53 | bl31_start = readl(GX_AO_SEC_GP_CFG5); |
| 54 | bl32_start = readl(GX_AO_SEC_GP_CFG4); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 55 | |
| 56 | /* |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 57 | * Early Meson GX Firmware revisions did not provide the reserved |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 58 | * memory zones in the registers, keep fixed memory zone handling. |
| 59 | */ |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 60 | if (IS_ENABLED(CONFIG_MESON_GX) && |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 61 | !reg && !bl31_start && !bl32_start) { |
| 62 | bl31_start = 0x10000000; |
| 63 | bl31_size = 0x200000; |
| 64 | } |
| 65 | |
| 66 | /* Add first 16MiB reserved zone */ |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 67 | meson_board_add_reserved_memory(fdt, 0, GX_FIRMWARE_MEM_SIZE); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 68 | |
| 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 Jourdan | 1be090a | 2018-12-11 12:52:04 +0100 | [diff] [blame] | 76 | |
| 77 | #if defined(CONFIG_VIDEO_MESON) |
| 78 | meson_vpu_rsv_fb(fdt); |
| 79 | #endif |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 82 | phys_size_t get_effective_memsize(void) |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 83 | { |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 84 | /* 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 Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 89 | static struct mm_region gx_mem_map[] = { |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 90 | { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 91 | .virt = 0x0UL, |
| 92 | .phys = 0x0UL, |
Loic Devulder | c067c58 | 2018-09-25 16:30:35 +0200 | [diff] [blame] | 93 | .size = 0xc0000000UL, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 94 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 95 | PTE_BLOCK_INNER_SHARE |
| 96 | }, { |
Loic Devulder | c067c58 | 2018-09-25 16:30:35 +0200 | [diff] [blame] | 97 | .virt = 0xc0000000UL, |
| 98 | .phys = 0xc0000000UL, |
| 99 | .size = 0x30000000UL, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 100 | .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 Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 109 | struct mm_region *mem_map = gx_mem_map; |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 110 | |
| 111 | /* Configure the Ethernet MAC with the requested interface mode |
| 112 | * with some optional flags. |
| 113 | */ |
| 114 | void 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 Armstrong | 3968a69 | 2019-05-28 13:13:19 +0200 | [diff] [blame] | 127 | |
| 128 | /* Reset to external PHY */ |
| 129 | if(!IS_ENABLED(CONFIG_MESON_GXBB)) |
| 130 | writel(0x2009087f, GX_ETH_REG_3); |
| 131 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 132 | 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 Armstrong | 3968a69 | 2019-05-28 13:13:19 +0200 | [diff] [blame] | 139 | /* 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 Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 146 | } |
| 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 Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 158 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 159 | #if CONFIG_IS_ENABLED(USB_DWC3_MESON_GXL) && \ |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 160 | CONFIG_IS_ENABLED(USB_GADGET_DWC2_OTG) |
| 161 | static struct dwc2_plat_otg_data meson_gx_dwc2_data; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 162 | |
| 163 | int board_usb_init(int index, enum usb_init_type init) |
| 164 | { |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 165 | struct fdtdec_phandle_args args; |
| 166 | const void *blob = gd->fdt_blob; |
| 167 | int node, dwc2_node; |
| 168 | struct udevice *dev, *clk_dev; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 169 | struct clk clk; |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 170 | int ret; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 171 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 172 | /* find the usb glue node */ |
| 173 | node = fdt_node_offset_by_compatible(blob, -1, |
| 174 | "amlogic,meson-gxl-usb-ctrl"); |
| 175 | if (node < 0) { |
| 176 | node = fdt_node_offset_by_compatible(blob, -1, |
| 177 | "amlogic,meson-gxm-usb-ctrl"); |
| 178 | if (node < 0) { |
| 179 | debug("Not found usb-control node\n"); |
| 180 | return -ENODEV; |
| 181 | } |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 184 | if (!fdtdec_get_is_enabled(blob, node)) { |
| 185 | debug("usb is disabled in the device tree\n"); |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 186 | return -ENODEV; |
| 187 | } |
| 188 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 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; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 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; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 203 | if (!fdtdec_get_is_enabled(blob, dwc2_node)) { |
| 204 | debug("dwc2 is disabled in the device tree\n"); |
| 205 | return -ENODEV; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 208 | meson_gx_dwc2_data.regs_otg = fdtdec_get_addr(blob, dwc2_node, "reg"); |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 209 | if (meson_gx_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 */ |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 215 | ret = fdtdec_parse_phandle_with_args(blob, dwc2_node, "clocks", |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 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 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 222 | ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, &clk_dev); |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 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 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 240 | meson_gx_dwc2_data.rx_fifo_sz = fdtdec_get_int(blob, dwc2_node, |
| 241 | "g-rx-fifo-size", 0); |
| 242 | meson_gx_dwc2_data.np_tx_fifo_sz = fdtdec_get_int(blob, dwc2_node, |
| 243 | "g-np-tx-fifo-size", 0); |
| 244 | meson_gx_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_gxl_force_mode(dev, USB_DR_MODE_PERIPHERAL); |
| 249 | if (ret) |
| 250 | return ret; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 251 | |
| 252 | return dwc2_udc_probe(&meson_gx_dwc2_data); |
| 253 | } |
| 254 | |
| 255 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 256 | { |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 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-gxl-usb-ctrl"); |
| 265 | if (node < 0) { |
| 266 | node = fdt_node_offset_by_compatible(blob, -1, |
| 267 | "amlogic,meson-gxm-usb-ctrl"); |
| 268 | if (node < 0) { |
| 269 | debug("Not found usb-control node\n"); |
| 270 | return -ENODEV; |
| 271 | } |
| 272 | } |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 273 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 274 | if (!fdtdec_get_is_enabled(blob, node)) |
| 275 | return -ENODEV; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 276 | |
Neil Armstrong | 69120d8 | 2020-09-10 10:48:14 +0200 | [diff] [blame] | 277 | ret = uclass_get_device_by_of_offset(UCLASS_SIMPLE_BUS, node, &dev); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | |
| 281 | /* Switch to OTG mode */ |
| 282 | ret = dwc3_meson_gxl_force_mode(dev, USB_DR_MODE_HOST); |
| 283 | if (ret) |
| 284 | return ret; |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | #endif |