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/protocol/ethernet.h>
9#include <ddk/protocol/platform-defs.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
17static 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
28static const pbus_irq_t eth_mac_irqs[] = {
29    {
30        .irq = S912_ETH_GMAC_IRQ,
31        .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
32    },
33};
34
35static 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
46static const pbus_mmio_t eth_mac_mmios[] = {
47    {
48        .base = ETH_MAC_REG_BASE,
49        .length = ETH_MAC_REG_SIZE,
50    },
51};
52
53static const pbus_bti_t eth_mac_btis[] = {
54    {
55        .iommu_index = 0,
56        .bti_id = 0,
57    },
58};
59
60static const pbus_boot_metadata_t eth_mac_metadata[] = {
61    {
62        .zbi_type = DEVICE_METADATA_MAC_ADDRESS,
63        .zbi_extra = 0,
64    },
65};
66
67static 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
73static const pbus_metadata_t eth_mac_device_metadata[] = {
74    {
75        .type = DEVICE_METADATA_PRIVATE,
76        .data = &eth_phy_device,
77        .len = sizeof(eth_dev_metadata_t),
78    },
79};
80
81static const eth_dev_metadata_t eth_mac_device = {
82    .vid = PDEV_VID_DESIGNWARE,
83    .did = PDEV_DID_ETH_MAC,
84};
85
86static const pbus_metadata_t eth_board_metadata[] = {
87    {
88        .type = DEVICE_METADATA_PRIVATE,
89        .data = &eth_mac_device,
90        .len = sizeof(eth_dev_metadata_t),
91    },
92};
93
94static const pbus_i2c_channel_t vim2_mcu_i2c[] = {
95    {
96        .bus_id = 1,
97        .address = 0x18,
98    },
99};
100
101static const pbus_dev_t eth_board_children[] = {
102    // Designware MAC.
103    {
104        .name = "dwmac",
105        .mmios = eth_mac_mmios,
106        .mmio_count = countof(eth_mac_mmios),
107        .btis = eth_mac_btis,
108        .bti_count = countof(eth_mac_btis),
109        .irqs = eth_mac_irqs,
110        .irq_count = countof(eth_mac_irqs),
111        .metadata = eth_mac_device_metadata,
112        .metadata_count = countof(eth_mac_device_metadata),
113        .boot_metadata = eth_mac_metadata,
114        .boot_metadata_count = countof(eth_mac_metadata),
115    },
116};
117
118static 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    .mmios = eth_board_mmios,
124    .mmio_count = countof(eth_board_mmios),
125    .gpios = eth_board_gpios,
126    .gpio_count = countof(eth_board_gpios),
127    .i2c_channels = vim2_mcu_i2c,
128    .i2c_channel_count = countof(vim2_mcu_i2c),
129    .metadata = eth_board_metadata,
130    .metadata_count = countof(eth_board_metadata),
131    .children = eth_board_children,
132    .child_count = countof(eth_board_children),
133};
134
135zx_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