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/device.h>
6 #include <ddktl/device.h>
7 #include <ddktl/i2c-channel.h>
8 #include <ddktl/mmio.h>
9 #include <ddktl/pdev.h>
10 #include <ddktl/protocol/ethernet/board.h>
11 #include <ddktl/protocol/gpio.h>
12 #include <threads.h>
13 
14 #include <optional>
15 
16 namespace eth {
17 
18 class AmlEthernet;
19 using DeviceType = ddk::Device<AmlEthernet, ddk::Unbindable>;
20 
21 class AmlEthernet : public DeviceType,
22                     public ddk::EthBoardProtocol<AmlEthernet, ddk::base_protocol> {
23 public:
24     DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(AmlEthernet);
25 
AmlEthernet(zx_device_t * parent)26     explicit AmlEthernet(zx_device_t* parent)
27         : DeviceType(parent), pdev_(parent) {}
28 
29     static zx_status_t Create(zx_device_t* parent);
30 
31     // DDK Hooks.
32     void DdkRelease();
33     void DdkUnbind();
34 
35     // ETH_BOARD protocol.
36     void EthBoardResetPhy();
37 
38 private:
39     // GPIO Indexes.
40     enum {
41         PHY_RESET,
42         PHY_INTR,
43         GPIO_COUNT,
44     };
45 
46     // MMIO Indexes
47     enum {
48         MMIO_PERIPH,
49         MMIO_HHI,
50     };
51 
52     zx_status_t InitPdev();
53     zx_status_t Bind();
54 
55     ddk::PDev pdev_;
56     std::optional<ddk::I2cChannel> i2c_;
57     std::optional<ddk::GpioProtocolClient> gpios_[GPIO_COUNT];
58 
59     std::optional<ddk::MmioBuffer> periph_mmio_;
60     std::optional<ddk::MmioBuffer> hhi_mmio_;
61 };
62 
63 } // namespace eth
64