blob: a7e7058724efeb1302c4a7634467121e60dde22f [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fidl/fuchsia.hardware.platform.bus/cpp/driver/fidl.h>
#include <fidl/fuchsia.hardware.platform.bus/cpp/fidl.h>
#include <fidl/fuchsia.hardware.sdmmc/cpp/wire.h>
#include <lib/ddk/binding.h>
#include <lib/ddk/debug.h>
#include <lib/ddk/metadata.h>
#include <lib/ddk/platform-defs.h>
#include <lib/driver/component/cpp/composite_node_spec.h>
#include <lib/driver/component/cpp/node_add_args.h>
#include <lib/mmio/mmio.h>
#include <lib/zx/handle.h>
#include <optional>
#include <bind/fuchsia/amlogic/platform/cpp/bind.h>
#include <bind/fuchsia/broadcom/platform/cpp/bind.h>
#include <bind/fuchsia/broadcom/platform/sdio/cpp/bind.h>
#include <bind/fuchsia/cpp/bind.h>
#include <bind/fuchsia/gpio/cpp/bind.h>
#include <bind/fuchsia/hardware/gpio/cpp/bind.h>
#include <bind/fuchsia/hardware/sdio/cpp/bind.h>
#include <bind/fuchsia/platform/cpp/bind.h>
#include <bind/fuchsia/pwm/cpp/bind.h>
#include <bind/fuchsia/sdio/cpp/bind.h>
#include <fbl/algorithm.h>
#include <hwreg/bitfields.h>
#include <soc/aml-common/aml-sdmmc.h>
#include <soc/aml-t931/t931-gpio.h>
#include <soc/aml-t931/t931-hw.h>
#include <wifi/wifi-config.h>
#include "sherlock.h"
namespace fdf {
using namespace fuchsia_driver_framework;
} // namespace fdf
namespace sherlock {
namespace fpbus = fuchsia_hardware_platform_bus;
namespace {
constexpr uint32_t kGpioBase = fbl::round_down<uint32_t, uint32_t>(T931_GPIO_BASE, PAGE_SIZE);
constexpr uint32_t kGpioBaseOffset = T931_GPIO_BASE - kGpioBase;
class PadDsReg2A : public hwreg::RegisterBase<PadDsReg2A, uint32_t> {
public:
static constexpr uint32_t kDriveStrengthMax = 3;
static auto Get() { return hwreg::RegisterAddr<PadDsReg2A>((0xd2 * 4) + kGpioBaseOffset); }
DEF_FIELD(1, 0, gpiox_0_select);
DEF_FIELD(3, 2, gpiox_1_select);
DEF_FIELD(5, 4, gpiox_2_select);
DEF_FIELD(7, 6, gpiox_3_select);
DEF_FIELD(9, 8, gpiox_4_select);
DEF_FIELD(11, 10, gpiox_5_select);
};
static const std::vector<fpbus::BootMetadata> wifi_boot_metadata{
{{
.zbi_type = DEVICE_METADATA_MAC_ADDRESS,
.zbi_extra = MACADDR_WIFI,
}},
};
static const std::vector<fpbus::Mmio> sd_emmc_mmios{
{{
.base = T931_SD_EMMC_A_BASE,
.length = T931_SD_EMMC_A_LENGTH,
}},
};
static const std::vector<fpbus::Irq> sd_emmc_irqs{
{{
.irq = T931_SD_EMMC_A_IRQ,
.mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
}},
};
static const std::vector<fpbus::Bti> sd_emmc_btis{
{{
.iommu_index = 0,
.bti_id = BTI_SDIO,
}},
};
constexpr wifi_config_t wifi_config = {
.oob_irq_mode = ZX_INTERRUPT_MODE_LEVEL_HIGH,
.iovar_table =
{
{IOVAR_STR_TYPE, {"ampdu_ba_wsize"}, 32},
{IOVAR_STR_TYPE, {"stbc_tx"}, 0}, // since tx_streams is 1
{IOVAR_STR_TYPE, {"stbc_rx"}, 1},
{IOVAR_CMD_TYPE, {.iovar_cmd = BRCMF_C_SET_PM}, 0},
{IOVAR_CMD_TYPE, {.iovar_cmd = BRCMF_C_SET_FAKEFRAG}, 1},
{IOVAR_LIST_END_TYPE, {{0}}, 0},
},
.cc_table =
{
{"WW", 1}, {"AU", 923}, {"CA", 901}, {"US", 843}, {"GB", 889}, {"BE", 889},
{"BG", 889}, {"CZ", 889}, {"DK", 889}, {"DE", 889}, {"EE", 889}, {"IE", 889},
{"GR", 889}, {"ES", 889}, {"FR", 889}, {"HR", 889}, {"IT", 889}, {"CY", 889},
{"LV", 889}, {"LT", 889}, {"LU", 889}, {"HU", 889}, {"MT", 889}, {"NL", 889},
{"AT", 889}, {"PL", 889}, {"PT", 889}, {"RO", 889}, {"SI", 889}, {"SK", 889},
{"FI", 889}, {"SE", 889}, {"EL", 889}, {"IS", 889}, {"LI", 889}, {"TR", 889},
{"CH", 889}, {"NO", 889}, {"JP", 2}, {"", 0},
},
};
static const std::vector<fpbus::Metadata> wifi_metadata{
{{
.type = DEVICE_METADATA_WIFI_CONFIG,
.data = std::vector<uint8_t>(
reinterpret_cast<const uint8_t*>(&wifi_config),
reinterpret_cast<const uint8_t*>(&wifi_config) + sizeof(wifi_config)),
}},
};
const std::vector<fdf::BindRule> kPwmRules = std::vector{
fdf::MakeAcceptBindRule(bind_fuchsia::INIT_STEP, bind_fuchsia_pwm::BIND_INIT_STEP_PWM),
};
const std::vector<fdf::NodeProperty> kPwmProperties = std::vector{
fdf::MakeProperty(bind_fuchsia::INIT_STEP, bind_fuchsia_pwm::BIND_INIT_STEP_PWM),
};
const std::vector<fdf::BindRule> kGpioResetRules = std::vector{
fdf::MakeAcceptBindRule(bind_fuchsia_hardware_gpio::SERVICE,
bind_fuchsia_hardware_gpio::SERVICE_ZIRCONTRANSPORT),
fdf::MakeAcceptBindRule(bind_fuchsia::GPIO_PIN, static_cast<uint32_t>(T931_WIFI_REG_ON)),
};
const std::vector<fdf::NodeProperty> kGpioResetProperties = std::vector{
fdf::MakeProperty(bind_fuchsia_hardware_gpio::SERVICE,
bind_fuchsia_hardware_gpio::SERVICE_ZIRCONTRANSPORT),
fdf::MakeProperty(bind_fuchsia_gpio::FUNCTION, bind_fuchsia_gpio::FUNCTION_SDMMC_RESET),
};
const std::vector<fdf::BindRule> kGpioInitRules = std::vector{
fdf::MakeAcceptBindRule(bind_fuchsia::INIT_STEP, bind_fuchsia_gpio::BIND_INIT_STEP_GPIO),
};
const std::vector<fdf::NodeProperty> kGpioInitProperties = std::vector{
fdf::MakeProperty(bind_fuchsia::INIT_STEP, bind_fuchsia_gpio::BIND_INIT_STEP_GPIO),
};
zx_status_t AddWifiComposite(fdf::WireSyncClient<fpbus::PlatformBus>& pbus,
fidl::AnyArena& fidl_arena, fdf::Arena& arena) {
const std::vector<fdf::BindRule> kGpioWifiHostRules = std::vector{
fdf::MakeAcceptBindRule(bind_fuchsia_hardware_gpio::SERVICE,
bind_fuchsia_hardware_gpio::SERVICE_ZIRCONTRANSPORT),
fdf::MakeAcceptBindRule(bind_fuchsia::GPIO_PIN, static_cast<uint32_t>(T931_WIFI_HOST_WAKE)),
};
const std::vector<fdf::NodeProperty> kGpioWifiHostProperties = std::vector{
fdf::MakeProperty(bind_fuchsia_hardware_gpio::SERVICE,
bind_fuchsia_hardware_gpio::SERVICE_ZIRCONTRANSPORT),
};
fpbus::Node wifi_dev;
wifi_dev.name() = "wifi";
wifi_dev.vid() = bind_fuchsia_broadcom_platform::BIND_PLATFORM_DEV_VID_BROADCOM;
wifi_dev.pid() = bind_fuchsia_broadcom_platform::BIND_PLATFORM_DEV_PID_BCM43458;
wifi_dev.did() = bind_fuchsia_broadcom_platform::BIND_PLATFORM_DEV_DID_WIFI;
wifi_dev.metadata() = wifi_metadata;
wifi_dev.boot_metadata() = wifi_boot_metadata;
constexpr uint32_t kSdioFunctionCount = 2;
std::vector<fdf::ParentSpec> wifi_parents = {
fdf::ParentSpec{{kGpioWifiHostRules, kGpioWifiHostProperties}}};
wifi_parents.reserve(wifi_parents.size() + kSdioFunctionCount);
for (uint32_t i = 1; i <= kSdioFunctionCount; i++) {
auto sdio_bind_rules = {
fdf::MakeAcceptBindRule(bind_fuchsia::PROTOCOL, bind_fuchsia_sdio::BIND_PROTOCOL_DEVICE),
fdf::MakeAcceptBindRule(bind_fuchsia::SDIO_VID,
bind_fuchsia_broadcom_platform_sdio::BIND_SDIO_VID_BROADCOM),
fdf::MakeAcceptBindRule(bind_fuchsia::SDIO_PID,
bind_fuchsia_broadcom_platform_sdio::BIND_SDIO_PID_BCM4345),
fdf::MakeAcceptBindRule(bind_fuchsia::SDIO_FUNCTION, i),
};
auto sdio_properties = {
fdf::MakeProperty(bind_fuchsia_hardware_sdio::SERVICE,
bind_fuchsia_hardware_sdio::SERVICE_ZIRCONTRANSPORT),
fdf::MakeProperty(bind_fuchsia::SDIO_FUNCTION, i),
};
wifi_parents.push_back(fdf::ParentSpec{
{sdio_bind_rules, sdio_properties},
});
}
fdf::WireUnownedResult result = pbus.buffer(arena)->AddCompositeNodeSpec(
fidl::ToWire(fidl_arena, wifi_dev),
fidl::ToWire(fidl_arena, fuchsia_driver_framework::CompositeNodeSpec{
{.name = "wifi", .parents = wifi_parents}}));
if (!result.ok()) {
zxlogf(ERROR, "Failed to send AddCompositeNodeSpec request to platform bus: %s",
result.status_string());
return result.status();
}
if (result->is_error()) {
zxlogf(ERROR, "Failed to add wifi composite to platform device: %s",
zx_status_get_string(result->error_value()));
return result->error_value();
}
return ZX_OK;
}
} // namespace
zx_status_t Sherlock::SdioInit() {
fidl::Arena<> fidl_arena;
fit::result sdmmc_metadata =
fidl::Persist(fuchsia_hardware_sdmmc::wire::SdmmcMetadata::Builder(fidl_arena)
.max_frequency(208'000'000)
// TODO(https://fxbug.dev/42084501): Use the FIDL SDMMC protocol.
.use_fidl(false)
.Build());
if (!sdmmc_metadata.is_ok()) {
zxlogf(ERROR, "Failed to encode SDMMC metadata: %s",
sdmmc_metadata.error_value().FormatDescription().c_str());
return sdmmc_metadata.error_value().status();
}
const std::vector<fpbus::Metadata> sd_emmc_metadata{
{{
.type = DEVICE_METADATA_SDMMC,
.data = std::move(sdmmc_metadata.value()),
}},
};
fpbus::Node sdio_dev;
sdio_dev.name() = "sherlock-sd-emmc";
sdio_dev.vid() = bind_fuchsia_amlogic_platform::BIND_PLATFORM_DEV_VID_AMLOGIC;
sdio_dev.pid() = bind_fuchsia_platform::BIND_PLATFORM_DEV_PID_GENERIC;
sdio_dev.did() = bind_fuchsia_amlogic_platform::BIND_PLATFORM_DEV_DID_SDMMC_A;
sdio_dev.mmio() = sd_emmc_mmios;
sdio_dev.bti() = sd_emmc_btis;
sdio_dev.irq() = sd_emmc_irqs;
sdio_dev.metadata() = sd_emmc_metadata;
// Configure eMMC-SD soc pads.
gpio_init_steps_.push_back({T931_SDIO_D0, GpioSetAltFunction(T931_SDIO_D0_FN)});
gpio_init_steps_.push_back({T931_SDIO_D1, GpioSetAltFunction(T931_SDIO_D1_FN)});
gpio_init_steps_.push_back({T931_SDIO_D2, GpioSetAltFunction(T931_SDIO_D2_FN)});
gpio_init_steps_.push_back({T931_SDIO_D3, GpioSetAltFunction(T931_SDIO_D3_FN)});
gpio_init_steps_.push_back({T931_SDIO_CLK, GpioSetAltFunction(T931_SDIO_CLK_FN)});
gpio_init_steps_.push_back({T931_SDIO_CMD, GpioSetAltFunction(T931_SDIO_CMD_FN)});
zx::unowned_resource res(get_mmio_resource(parent()));
zx::vmo vmo;
zx_status_t status =
zx::vmo::create_physical(*res, kGpioBase, kGpioBaseOffset + T931_GPIO_LENGTH, &vmo);
if (status != ZX_OK) {
zxlogf(ERROR, "failed to create VMO: %s", zx_status_get_string(status));
return status;
}
zx::result<fdf::MmioBuffer> buf = fdf::MmioBuffer::Create(
0, kGpioBaseOffset + T931_GPIO_LENGTH, std::move(vmo), ZX_CACHE_POLICY_UNCACHED_DEVICE);
if (buf.is_error()) {
zxlogf(ERROR, "fdf::MmioBuffer::Create() error: %s", buf.status_string());
return buf.status_value();
}
PadDsReg2A::Get()
.ReadFrom(&(*buf))
.set_gpiox_0_select(PadDsReg2A::kDriveStrengthMax)
.set_gpiox_1_select(PadDsReg2A::kDriveStrengthMax)
.set_gpiox_2_select(PadDsReg2A::kDriveStrengthMax)
.set_gpiox_3_select(PadDsReg2A::kDriveStrengthMax)
.set_gpiox_4_select(PadDsReg2A::kDriveStrengthMax)
.set_gpiox_5_select(PadDsReg2A::kDriveStrengthMax)
.WriteTo(&(*buf));
gpio_init_steps_.push_back({T931_WIFI_REG_ON, GpioSetAltFunction(T931_WIFI_REG_ON_FN)});
gpio_init_steps_.push_back({T931_WIFI_HOST_WAKE, GpioSetAltFunction(T931_WIFI_HOST_WAKE_FN)});
std::vector<fdf::ParentSpec> kSdioParents = {
fdf::ParentSpec{{kPwmRules, kPwmProperties}},
fdf::ParentSpec{{kGpioInitRules, kGpioInitProperties}},
fdf::ParentSpec{{kGpioResetRules, kGpioResetProperties}}};
fdf::Arena sdio_arena('SDIO');
auto result =
pbus_.buffer(sdio_arena)
->AddCompositeNodeSpec(
fidl::ToWire(fidl_arena, sdio_dev),
fidl::ToWire(fidl_arena, fuchsia_driver_framework::CompositeNodeSpec{
{.name = "sherlock_sd_emmc", .parents = kSdioParents}}));
if (!result.ok()) {
zxlogf(ERROR, "AddCompositeNodeSpec Sdio(sdio_dev) request failed: %s",
result.FormatDescription().data());
return result.status();
}
if (result->is_error()) {
zxlogf(ERROR, "AddCompositeNodeSpec Sdio(sdio_dev) failed: %s",
zx_status_get_string(result->error_value()));
return result->error_value();
}
fdf::Arena wifi_arena('WIFI');
status = AddWifiComposite(pbus_, fidl_arena, wifi_arena);
if (status != ZX_OK) {
return status;
}
return ZX_OK;
}
} // namespace sherlock