1 // Copyright 2017 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/metadata.h>
7 #include <ddk/platform-defs.h>
8 #include <soc/hi3660/hi3660-hw.h>
9 
10 #include <stdio.h>
11 
12 #include "hikey960.h"
13 #include "hikey960-hw.h"
14 
15 // #define GPIO_TEST 1
16 // #define I2C_TEST 1
17 //#define DSI_ENABLE 1
18 
19 #ifdef DSI_ENABLE
20 static const pbus_mmio_t dsi_mmios[] = {
21     {
22         .base = MMIO_DSI_BASE,
23         .length = MMIO_DSI_LENGTH,
24     },
25 };
26 
27 static const pbus_i2c_channel_t dsi.i2c_channel_list[] = {
28     {
29         // HDMI_MAIN
30         .bus_id = DW_I2C_1,
31         .address = 0x39,
32     },
33     {
34         // HDMI_CEC
35         .bus_id = DW_I2C_1,
36         .address = 0x38,
37     },
38     {
39         // HDMI_EDID
40         .bus_id = DW_I2C_1,
41         .address = 0x3b,
42     },
43 };
44 
45 static const pbus_gpio_t dsi_gpios[] = {
46     {
47         .gpio = GPIO_HDMI_MUX,
48     },
49     {
50         .gpio = GPIO_HDMI_PD,
51     },
52     {
53         .gpio = GPIO_HDMI_INT,
54     },
55 };
56 
57 static const pbus_bti_t dsi_btis[] = {
58     {
59         .iommu_index = 0,
60         .bti_id = BTI_DSI,
61     },
62 };
63 
64 static const pbus_dev_t dsi_dev = {
65     .name = "dsi",
66     .vid = PDEV_VID_GENERIC,
67     .pid = PDEV_PID_GENERIC,
68     .did = PDEV_DID_DSI,
69     .mmio_list = dsi_mmios,
70     .mmio_count = countof(dsi_mmios),
71     .i2c_channel_list = dsi.i2c_channel_list,
72     .i2c_channel_count = countof(dsi.i2c_channel_list),
73     .gpio_list = dsi_gpios,
74     .gpio_count = countof(dsi_gpios),
75     .bti_list = dsi_btis,
76     .bti_count = countof(dsi_btis),
77 };
78 #endif
79 
80 static const pbus_mmio_t mali_mmios[] = {
81     {
82         .base = MMIO_G3D_BASE,
83         .length = MMIO_G3D_LENGTH,
84     },
85 };
86 
87 static const pbus_irq_t mali_irqs[] = {
88     {
89         .irq = IRQ_G3D_JOB,
90         .mode = ZX_INTERRUPT_MODE_LEVEL_HIGH,
91     },
92     {
93         .irq = IRQ_G3D_MMU,
94         .mode = ZX_INTERRUPT_MODE_LEVEL_HIGH,
95     },
96     {
97         .irq = IRQ_G3D_GPU,
98         .mode = ZX_INTERRUPT_MODE_LEVEL_HIGH,
99     },
100 };
101 
102 static const pbus_bti_t mali_btis[] = {
103     {
104         .iommu_index = 0,
105         .bti_id = BTI_MALI,
106     },
107 };
108 
109 static const pbus_dev_t mali_dev = {
110     .name = "mali",
111     .vid = PDEV_VID_GENERIC,
112     .pid = PDEV_PID_GENERIC,
113     .did = PDEV_DID_ARM_MALI,
114     .mmio_list = mali_mmios,
115     .mmio_count = countof(mali_mmios),
116     .irq_list = mali_irqs,
117     .irq_count = countof(mali_irqs),
118     .bti_list = mali_btis,
119     .bti_count = countof(mali_btis),
120 };
121 
122 static const pbus_mmio_t clk_mmios[] = {
123     {
124         .base = MMIO_PERI_CRG_BASE,
125         .length = MMIO_PERI_CRG_LENGTH,
126     },
127     {
128         .base = MMIO_SCTRL_BASE,
129         .length = MMIO_SCTRL_LENGTH,
130     },
131 };
132 
133 static const pbus_dev_t hi3660_clk_dev = {
134     .name = "hi3660-clk",
135     .vid = PDEV_VID_96BOARDS,
136     .did = PDEV_DID_HI3660_CLK,
137     .mmio_list = clk_mmios,
138     .mmio_count = countof(clk_mmios),
139 };
140 
141 #if GPIO_TEST
142 static const pbus_gpio_t gpio_test_gpios[] = {
143     {
144         .gpio = GPIO_USER_LED3,
145     },
146     {
147         .gpio = GPIO_USER_LED1,
148     },
149     {
150         .gpio = GPIO_USER_LED2,
151     },
152     {
153         .gpio = GPIO_USER_LED4,
154     },
155 };
156 
157 static const pbus_dev_t gpio_test_dev = {
158     .name = "hikey960-gpio-test",
159     .vid = PDEV_VID_GENERIC,
160     .pid = PDEV_PID_GENERIC,
161     .did = PDEV_DID_GPIO_TEST,
162     .gpio_list = gpio_test_gpios,
163     .gpio_count = countof(gpio_test_gpios),
164 };
165 #endif
166 
167 #if I2C_TEST
168 static const pbus_i2c_channel_t i2c_test_channels[] = {
169     {
170         // USB HUB
171         .bus_id = DW_I2C_1,
172         .address = 0x4e,
173     },
174 };
175 
176 static const pbus_dev_t i2c_test_dev = {
177     .name = "hikey960-i2c-test",
178     .vid = PDEV_VID_96BOARDS,
179     .pid = PDEV_PID_HIKEY960,
180     .did = PDEV_DID_HIKEY960_I2C_TEST,
181     .i2c_channel_list = i2c_test_channels,
182     .i2c_channel_count = countof(i2c_test_channels),
183 };
184 #endif
185 
186 static const pbus_dev_t dummy_display_dev = {
187     .name = "dummy-display",
188     .vid = PDEV_VID_GENERIC,
189     .did = PDEV_DID_DUMMY_DISPLAY,
190 };
191 
hikey960_add_devices(hikey960_t * hikey)192 zx_status_t hikey960_add_devices(hikey960_t* hikey) {
193     zx_status_t status;
194 
195     if ((status = pbus_protocol_device_add(&hikey->pbus, ZX_PROTOCOL_CLK, &hi3660_clk_dev))
196             != ZX_OK) {
197         zxlogf(ERROR, "hikey960_add_devices could not add clk_dev: %d\n", status);
198     }
199     if ((status = hikey960_usb_init(hikey)) != ZX_OK) {
200         zxlogf(ERROR, "hikey960_usb_init failed: %d\n", status);
201     }
202     if ((status = pbus_device_add(&hikey->pbus, &mali_dev)) != ZX_OK) {
203         zxlogf(ERROR, "hikey960_add_devices could not add mali_dev: %d\n", status);
204         return status;
205     }
206 #ifdef DSI_ENABLE
207     if ((status = pbus_device_add(&hikey->pbus, &dsi_dev)) != ZX_OK) {
208         zxlogf(ERROR, "hikey960_add_devices could not add dsi_dev: %d\n", status);
209     }
210 #endif
211 
212 #if GPIO_TEST
213     if ((status = pbus_device_add(&hikey->pbus, &gpio_test_dev)) != ZX_OK) {
214         zxlogf(ERROR, "hikey960_add_devices could not add gpio_test_dev: %d\n", status);
215     }
216 #endif
217 
218 #if I2C_TEST
219     if ((status = pbus_device_add(&hikey->pbus, &i2c_test_dev)) != ZX_OK) {
220         zxlogf(ERROR, "hikey960_add_devices could not add i2c_test_dev: %d\n", status);
221     }
222 #endif
223 
224     if ((status = pbus_device_add(&hikey->pbus, &dummy_display_dev)) != ZX_OK) {
225         zxlogf(ERROR, "hikey960_add_devices could not add dummy_display_dev: %d\n", status);
226     }
227 
228     return ZX_OK;
229 }
230