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 #pragma once
6 
7 #include <ddk/protocol/i2c.h>
8 #include <ddk/protocol/platform/device.h>
9 #include <ddk/protocol/platform/proxy.h>
10 
11 namespace platform_bus {
12 
13 // Maximum transfer size we can proxy.
14 static constexpr size_t PROXY_MAX_TRANSFER_SIZE = 4096;
15 
16 // Device ID for a top level platform device (that is, an immediate child of the platform bus).
17 static constexpr uint32_t ROOT_DEVICE_ID = 0;
18 
19 // ZX_PROTOCOL_PDEV proxy support.
20 enum {
21     PDEV_GET_MMIO,
22     PDEV_GET_INTERRUPT,
23     PDEV_GET_BTI,
24     PDEV_GET_SMC,
25     PDEV_GET_DEVICE_INFO,
26     PDEV_GET_BOARD_INFO,
27     PDEV_DEVICE_ADD,
28     PDEV_GET_METADATA,
29     PDEV_GET_PROTOCOLS,
30 };
31 
32 typedef struct {
33     platform_proxy_req_t header;
34     uint32_t index;
35     uint32_t flags;
36 } rpc_pdev_req_t;
37 
38 typedef struct {
39     platform_proxy_rsp_t header;
40     zx_paddr_t paddr;
41     size_t length;
42     uint32_t irq;
43     uint32_t mode;
44     pdev_device_info_t device_info;
45     pdev_board_info_t board_info;
46     uint32_t device_id;
47     uint32_t metadata_type;
48     uint32_t metadata_length;
49     uint32_t protocol_count;
50 } rpc_pdev_rsp_t;
51 
52 // Maximum metadata size that can be returned via PDEV_DEVICE_GET_METADATA.
53 static constexpr uint32_t PROXY_MAX_METADATA_SIZE =
54     (PROXY_MAX_TRANSFER_SIZE - sizeof(rpc_pdev_rsp_t));
55 
56 typedef struct {
57     rpc_pdev_rsp_t pdev;
58     uint8_t metadata[PROXY_MAX_METADATA_SIZE];
59 } rpc_pdev_metadata_rsp_t;
60 
61 // Maximum number of protocols that can be returned via PDEV_GET_PROTOCOLS.
62 static constexpr size_t PROXY_MAX_PROTOCOLS = ((PLATFORM_PROXY_MAX_DATA - sizeof(rpc_pdev_rsp_t))
63                                                 / sizeof(uint32_t));
64 
65 // Maximum I2C transfer is I2C_MAX_TRANSFER_SIZE minus size of largest header.
66 static constexpr uint32_t I2C_MAX_TRANSFER_SIZE = (PROXY_MAX_TRANSFER_SIZE -
67             (sizeof(rpc_pdev_req_t) > sizeof(rpc_pdev_rsp_t) ?
68              sizeof(rpc_pdev_req_t) : sizeof(rpc_pdev_rsp_t)));
69 
70 // ZX_PROTOCOL_GPIO proxy support.
71 enum {
72     GPIO_CONFIG_IN,
73     GPIO_CONFIG_OUT,
74     GPIO_SET_ALT_FUNCTION,
75     GPIO_READ,
76     GPIO_WRITE,
77     GPIO_GET_INTERRUPT,
78     GPIO_RELEASE_INTERRUPT,
79     GPIO_SET_POLARITY,
80 };
81 
82 typedef struct {
83     platform_proxy_req_t header;
84     uint32_t index;
85     uint32_t flags;
86     uint32_t polarity;
87     uint64_t alt_function;
88     uint8_t value;
89 } rpc_gpio_req_t;
90 
91 typedef struct {
92     platform_proxy_rsp_t header;
93     uint8_t value;
94 } rpc_gpio_rsp_t;
95 
96 // ZX_PROTOCOL_I2C proxy support.
97 enum {
98     I2C_GET_MAX_TRANSFER,
99     I2C_TRANSACT,
100 };
101 
102 typedef struct {
103     platform_proxy_req_t header;
104     uint32_t index;
105     i2c_transact_callback transact_cb;
106     void* cookie;
107     size_t cnt;
108 } rpc_i2c_req_t;
109 
110 typedef struct {
111     platform_proxy_rsp_t header;
112     size_t max_transfer;
113     i2c_transact_callback transact_cb;
114     void* cookie;
115 } rpc_i2c_rsp_t;
116 
117 // ZX_PROTOCOL_CLK proxy support.
118 enum {
119     CLK_ENABLE,
120     CLK_DISABLE,
121 };
122 
123 typedef struct {
124     platform_proxy_req_t header;
125     uint32_t index;
126 } rpc_clk_req_t;
127 
128 } // namespace platform_bus
129