1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <ddk/debug.h>
6 #include <ddk/device.h>
7 #include <ddk/metadata.h>
8 #include <ddk/platform-defs.h>
9 #include <ddk/protocol/ethernet.h>
10 #include <soc/aml-s912/s912-gpio.h>
11 #include <soc/aml-s912/s912-hw.h>
12 
13 #include <limits.h>
14 
15 #include "vim.h"
16 
17 static const pbus_gpio_t eth_board_gpios[] = {
18     {
19         // MAC_RST
20         .gpio = S912_GPIOZ(14),
21     },
22     {
23         // MAC_INTR (need to wire up interrupt?)
24         .gpio = S912_GPIOZ(15),
25     },
26 };
27 
28 static const pbus_irq_t eth_mac_irqs[] = {
29     {
30         .irq = S912_ETH_GMAC_IRQ,
31         .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
32     },
33 };
34 
35 static const pbus_mmio_t eth_board_mmios[] = {
36     {
37         .base = PERIPHS_REG_BASE,
38         .length = PERIPHS_REG_SIZE,
39     },
40     {
41         .base = HHI_REG_BASE,
42         .length = HHI_REG_SIZE,
43     },
44 };
45 
46 static const pbus_mmio_t eth_mac_mmios[] = {
47     {
48         .base = ETH_MAC_REG_BASE,
49         .length = ETH_MAC_REG_SIZE,
50     },
51 };
52 
53 static const pbus_bti_t eth_mac_btis[] = {
54     {
55         .iommu_index = 0,
56         .bti_id = 0,
57     },
58 };
59 
60 static const pbus_boot_metadata_t eth_mac_metadata[] = {
61     {
62         .zbi_type = DEVICE_METADATA_MAC_ADDRESS,
63         .zbi_extra = 0,
64     },
65 };
66 
67 static const eth_dev_metadata_t eth_phy_device = {
68     .vid = PDEV_VID_REALTEK,
69     .pid = PDEV_PID_RTL8211F,
70     .did = PDEV_DID_ETH_PHY,
71 };
72 
73 static const pbus_metadata_t eth_mac_device_metadata[] = {
74     {
75         .type = DEVICE_METADATA_PRIVATE,
76         .data_buffer = &eth_phy_device,
77         .data_size = sizeof(eth_dev_metadata_t),
78     },
79 };
80 
81 static const eth_dev_metadata_t eth_mac_device = {
82     .vid = PDEV_VID_DESIGNWARE,
83     .did = PDEV_DID_ETH_MAC,
84 };
85 
86 static const pbus_metadata_t eth_board_metadata[] = {
87     {
88         .type = DEVICE_METADATA_PRIVATE,
89         .data_buffer = &eth_mac_device,
90         .data_size = sizeof(eth_dev_metadata_t),
91     },
92 };
93 
94 static const pbus_i2c_channel_t vim2_mcu_i2c[] = {
95     {
96         .bus_id = 1,
97         .address = 0x18,
98     },
99 };
100 
101 static const pbus_dev_t eth_board_children[] = {
102     // Designware MAC.
103     {
104         .name = "dwmac",
105         .mmio_list = eth_mac_mmios,
106         .mmio_count = countof(eth_mac_mmios),
107         .bti_list = eth_mac_btis,
108         .bti_count = countof(eth_mac_btis),
109         .irq_list = eth_mac_irqs,
110         .irq_count = countof(eth_mac_irqs),
111         .metadata_list = eth_mac_device_metadata,
112         .metadata_count = countof(eth_mac_device_metadata),
113         .boot_metadata_list = eth_mac_metadata,
114         .boot_metadata_count = countof(eth_mac_metadata),
115     },
116 };
117 
118 static pbus_dev_t eth_board_dev = {
119     .name = "ethernet_mac",
120     .vid = PDEV_VID_KHADAS,
121     .pid = PDEV_PID_VIM2,
122     .did = PDEV_DID_AMLOGIC_ETH,
123     .mmio_list = eth_board_mmios,
124     .mmio_count = countof(eth_board_mmios),
125     .gpio_list = eth_board_gpios,
126     .gpio_count = countof(eth_board_gpios),
127     .i2c_channel_list = vim2_mcu_i2c,
128     .i2c_channel_count = countof(vim2_mcu_i2c),
129     .metadata_list = eth_board_metadata,
130     .metadata_count = countof(eth_board_metadata),
131     .child_list = eth_board_children,
132     .child_count = countof(eth_board_children),
133 };
134 
vim_eth_init(vim_bus_t * bus)135 zx_status_t vim_eth_init(vim_bus_t* bus) {
136 
137     // setup pinmux for RGMII connections
138     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_MDIO, S912_ETH_MDIO_FN);
139     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_MDC, S912_ETH_MDC_FN);
140     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RGMII_RX_CLK,
141                                S912_ETH_RGMII_RX_CLK_FN);
142     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RX_DV, S912_ETH_RX_DV_FN);
143     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RXD0, S912_ETH_RXD0_FN);
144     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RXD1, S912_ETH_RXD1_FN);
145     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RXD2, S912_ETH_RXD2_FN);
146     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RXD3, S912_ETH_RXD3_FN);
147 
148     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_RGMII_TX_CLK,
149                                S912_ETH_RGMII_TX_CLK_FN);
150     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_TX_EN, S912_ETH_TX_EN_FN);
151     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_TXD0, S912_ETH_TXD0_FN);
152     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_TXD1, S912_ETH_TXD1_FN);
153     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_TXD2, S912_ETH_TXD2_FN);
154     gpio_impl_set_alt_function(&bus->gpio, S912_ETH_TXD3, S912_ETH_TXD3_FN);
155 
156     zx_status_t status = pbus_device_add(&bus->pbus, &eth_board_dev);
157 
158     if (status != ZX_OK) {
159         zxlogf(ERROR, "vim_eth_init: pbus_device_add failed: %d\n", status);
160     }
161     return status;
162 }
163