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> |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 9 | #include <asm/arch/boot.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 10 | #include <asm/arch/eth.h> |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 11 | #include <asm/arch/gx.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 12 | #include <asm/arch/mem.h> |
Maxime Jourdan | 1be090a | 2018-12-11 12:52:04 +0100 | [diff] [blame] | 13 | #include <asm/arch/meson-vpu.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 15 | #include <asm/armv8/mmu.h> |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 16 | #include <linux/sizes.h> |
Neil Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 17 | #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 Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 22 | #include <phy.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 26 | int meson_get_boot_device(void) |
| 27 | { |
| 28 | return readl(GX_AO_SEC_GP_CFG0) & GX_AO_BOOT_DEVICE; |
| 29 | } |
| 30 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 31 | /* Configure the reserved memory zones exported by the secure registers |
| 32 | * into EFI and DTB reserved memory entries. |
| 33 | */ |
| 34 | void meson_init_reserved_memory(void *fdt) |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 35 | { |
| 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 Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 46 | reg = readl(GX_AO_SEC_GP_CFG3); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 47 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 48 | 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 Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 51 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 52 | bl31_start = readl(GX_AO_SEC_GP_CFG5); |
| 53 | bl32_start = readl(GX_AO_SEC_GP_CFG4); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 54 | |
| 55 | /* |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 56 | * Early Meson GX Firmware revisions did not provide the reserved |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 57 | * memory zones in the registers, keep fixed memory zone handling. |
| 58 | */ |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 59 | if (IS_ENABLED(CONFIG_MESON_GX) && |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 60 | !reg && !bl31_start && !bl32_start) { |
| 61 | bl31_start = 0x10000000; |
| 62 | bl31_size = 0x200000; |
| 63 | } |
| 64 | |
| 65 | /* Add first 16MiB reserved zone */ |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 66 | meson_board_add_reserved_memory(fdt, 0, GX_FIRMWARE_MEM_SIZE); |
Neil Armstrong | 8b24569 | 2017-11-27 10:35:46 +0100 | [diff] [blame] | 67 | |
| 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 Jourdan | 1be090a | 2018-12-11 12:52:04 +0100 | [diff] [blame] | 75 | |
| 76 | #if defined(CONFIG_VIDEO_MESON) |
| 77 | meson_vpu_rsv_fb(fdt); |
| 78 | #endif |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 81 | phys_size_t get_effective_memsize(void) |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 82 | { |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 83 | /* 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 Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Neil Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 88 | static struct mm_region gx_mem_map[] = { |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 89 | { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 90 | .virt = 0x0UL, |
| 91 | .phys = 0x0UL, |
Loic Devulder | c067c58 | 2018-09-25 16:30:35 +0200 | [diff] [blame] | 92 | .size = 0xc0000000UL, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 93 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 94 | PTE_BLOCK_INNER_SHARE |
| 95 | }, { |
Loic Devulder | c067c58 | 2018-09-25 16:30:35 +0200 | [diff] [blame] | 96 | .virt = 0xc0000000UL, |
| 97 | .phys = 0xc0000000UL, |
| 98 | .size = 0x30000000UL, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 99 | .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 Armstrong | 6e89d92 | 2018-04-11 17:13:45 +0200 | [diff] [blame] | 108 | struct mm_region *mem_map = gx_mem_map; |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 109 | |
| 110 | /* Configure the Ethernet MAC with the requested interface mode |
| 111 | * with some optional flags. |
| 112 | */ |
| 113 | void 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 Armstrong | 3968a69 | 2019-05-28 13:13:19 +0200 | [diff] [blame] | 126 | |
| 127 | /* Reset to external PHY */ |
| 128 | if(!IS_ENABLED(CONFIG_MESON_GXBB)) |
| 129 | writel(0x2009087f, GX_ETH_REG_3); |
| 130 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 131 | 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 Armstrong | 3968a69 | 2019-05-28 13:13:19 +0200 | [diff] [blame] | 138 | /* 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 Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 145 | } |
| 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 Armstrong | 70f92ac | 2020-03-30 11:27:25 +0200 | [diff] [blame] | 157 | |
| 158 | #if CONFIG_IS_ENABLED(USB_XHCI_DWC3_OF_SIMPLE) && \ |
| 159 | CONFIG_IS_ENABLED(USB_GADGET_DWC2_OTG) |
| 160 | static struct dwc2_plat_otg_data meson_gx_dwc2_data; |
| 161 | static struct phy usb_phys[2]; |
| 162 | |
| 163 | int 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 | |
| 257 | int 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 |