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/mmio-buffer.h>
8 #include <ddk/metadata.h>
9 #include <ddk/platform-defs.h>
10 #include <ddk/protocol/gpio.h>
11 #include <ddk/protocol/platform/bus.h>
12 #include <ddk/protocol/serial.h>
13 #include <hw/reg.h>
14 #include <soc/aml-s912/s912-gpio.h>
15 #include <soc/aml-s912/s912-hw.h>
16 #include <unistd.h>
17 #include <zircon/device/serial.h>
18
19 #include "vim.h"
20
21 // set this to enable UART test driver, which uses the second UART
22 // on the 40 pin header
23 #define UART_TEST 1
24
25 #define WIFI_32K S912_GPIOX(16)
26 #define BT_EN S912_GPIOX(17)
27
28
29 static const pbus_mmio_t bt_uart_mmios[] = {
30 // UART_A, for BT HCI
31 {
32 .base = S912_UART_A_BASE,
33 .length = S912_UART_A_LENGTH,
34 },
35 };
36
37 static const pbus_irq_t bt_uart_irqs[] = {
38 // UART_A, for BT HCI
39 {
40 .irq = S912_UART_A_IRQ,
41 .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
42 },
43 };
44
45 static const serial_port_info_t bt_uart_serial_info = {
46 .serial_class = SERIAL_CLASS_BLUETOOTH_HCI,
47 .serial_vid = PDEV_VID_BROADCOM,
48 .serial_pid = PDEV_PID_BCM4356,
49 };
50
51 static const pbus_metadata_t bt_uart_metadata[] = {
52 {
53 .type = DEVICE_METADATA_SERIAL_PORT_INFO,
54 .data_buffer = &bt_uart_serial_info,
55 .data_size = sizeof(bt_uart_serial_info),
56 },
57 };
58
59 static pbus_dev_t bt_uart_dev = {
60 .name = "bt-uart",
61 .vid = PDEV_VID_AMLOGIC,
62 .pid = PDEV_PID_GENERIC,
63 .did = PDEV_DID_AMLOGIC_UART,
64 .mmio_list = bt_uart_mmios,
65 .mmio_count = countof(bt_uart_mmios),
66 .irq_list = bt_uart_irqs,
67 .irq_count = countof(bt_uart_irqs),
68 .metadata_list = bt_uart_metadata,
69 .metadata_count = countof(bt_uart_metadata),
70 };
71
72 #if UART_TEST
73 static const pbus_mmio_t header_uart_mmios[] = {
74 // UART_AO_B, on 40 pin header
75 {
76 .base = S912_UART_AO_B_BASE,
77 .length = S912_UART_AO_B_LENGTH,
78 },
79 };
80
81 static const pbus_irq_t header_uart_irqs[] = {
82 // UART_AO_B, on 40 pin header
83 {
84 .irq = S912_UART_AO_B_IRQ,
85 .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
86 },
87 };
88
89 static const serial_port_info_t header_serial_info = {
90 .serial_class = SERIAL_CLASS_GENERIC,
91 };
92
93 static const pbus_metadata_t header_metadata[] = {
94 {
95 .type = DEVICE_METADATA_SERIAL_PORT_INFO,
96 .data_buffer = &header_serial_info,
97 .data_size = sizeof(header_serial_info),
98 },
99 };
100
101 static pbus_dev_t header_uart_dev = {
102 .name = "header-uart",
103 .vid = PDEV_VID_AMLOGIC,
104 .pid = PDEV_PID_GENERIC,
105 .did = PDEV_DID_AMLOGIC_UART,
106 .mmio_list = header_uart_mmios,
107 .mmio_count = countof(header_uart_mmios),
108 .irq_list = header_uart_irqs,
109 .irq_count = countof(header_uart_irqs),
110 .metadata_list = header_metadata,
111 .metadata_count = countof(header_metadata),
112 };
113 #endif
114
115 // Enables and configures PWM_E on the WIFI_32K line for the Wifi/Bluetooth module
vim_enable_wifi_32K(vim_bus_t * bus)116 static zx_status_t vim_enable_wifi_32K(vim_bus_t* bus) {
117 // Configure WIFI_32K pin for PWM_E
118 zx_status_t status = gpio_impl_set_alt_function(&bus->gpio, WIFI_32K, 1);
119 if (status != ZX_OK) return status;
120
121 mmio_buffer_t buffer;
122 status = mmio_buffer_init_physical(&buffer, S912_PWM_BASE, 0x10000, get_root_resource(),
123 ZX_CACHE_POLICY_UNCACHED_DEVICE);
124 if (status != ZX_OK) {
125 zxlogf(ERROR, "vim_enable_wifi_32K: io_buffer_init_physical failed: %d\n", status);
126 return status;
127 }
128 uint32_t* regs = buffer.vaddr;
129
130 // these magic numbers were gleaned by instrumenting drivers/amlogic/pwm/pwm_meson.c
131 // TODO(voydanoff) write a proper PWM driver
132 writel(0x016d016e, regs + S912_PWM_PWM_E);
133 writel(0x016d016d, regs + S912_PWM_E2);
134 writel(0x0a0a0609, regs + S912_PWM_TIME_EF);
135 writel(0x02808003, regs + S912_PWM_MISC_REG_EF);
136
137 mmio_buffer_release(&buffer);
138
139 return ZX_OK;
140 }
141
vim_uart_init(vim_bus_t * bus)142 zx_status_t vim_uart_init(vim_bus_t* bus) {
143 zx_status_t status;
144
145 // set alternate functions to enable UART_A and UART_AO_B
146 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_TX_A, S912_UART_TX_A_FN);
147 if (status != ZX_OK) return status;
148 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_RX_A, S912_UART_RX_A_FN);
149 if (status != ZX_OK) return status;
150 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_CTS_A, S912_UART_CTS_A_FN);
151 if (status != ZX_OK) return status;
152 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_RTS_A, S912_UART_RTS_A_FN);
153 if (status != ZX_OK) return status;
154 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_TX_AO_B, S912_UART_TX_AO_B_FN);
155 if (status != ZX_OK) return status;
156 status = gpio_impl_set_alt_function(&bus->gpio, S912_UART_RX_AO_B, S912_UART_RX_AO_B_FN);
157 if (status != ZX_OK) return status;
158
159 // Configure the WIFI_32K PWM, which is needed for the Bluetooth module to work properly
160 status = vim_enable_wifi_32K(bus);
161 if (status != ZX_OK) {
162 return status;
163 }
164
165 // set GPIO to reset Bluetooth module
166 gpio_impl_config_out(&bus->gpio, BT_EN, 0);
167 usleep(10 * 1000);
168 gpio_impl_write(&bus->gpio, BT_EN, 1);
169
170 // Bind UART for Bluetooth HCI
171 status = pbus_device_add(&bus->pbus, &bt_uart_dev);
172 if (status != ZX_OK) {
173 zxlogf(ERROR, "vim_gpio_init: pbus_device_add failed: %d\n", status);
174 return status;
175 }
176
177 #if UART_TEST
178 // Bind UART for 40-pin header
179 status = pbus_device_add(&bus->pbus, &header_uart_dev);
180 if (status != ZX_OK) {
181 zxlogf(ERROR, "vim_gpio_init: pbus_device_add failed: %d\n", status);
182 return status;
183 }
184 #endif
185
186 return ZX_OK;
187 }
188