/*
 * Copyright (c) 2023 Aleksandr Senin
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT zephyr_mdio_gpio

#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/mdio.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mdio_gpio, CONFIG_MDIO_LOG_LEVEL);

#define MDIO_GPIO_READ_OP  0
#define MDIO_GPIO_WRITE_OP 1
#define MDIO_GPIO_MSB      0x80000000

struct mdio_gpio_data {
	struct k_sem sem;
};

struct mdio_gpio_config {
	struct gpio_dt_spec mdc_gpio;
	struct gpio_dt_spec mdio_gpio;
};

static ALWAYS_INLINE void mdio_gpio_clock_the_bit(const struct mdio_gpio_config *dev_cfg)
{
	k_busy_wait(1);
	gpio_pin_set_dt(&dev_cfg->mdc_gpio, 1);
	k_busy_wait(1);
	gpio_pin_set_dt(&dev_cfg->mdc_gpio, 0);
}

static ALWAYS_INLINE void mdio_gpio_dir(const struct mdio_gpio_config *dev_cfg, uint8_t dir)
{
	gpio_pin_configure_dt(&dev_cfg->mdio_gpio, dir ? GPIO_OUTPUT_ACTIVE : GPIO_INPUT);
	if (dir == 0) {
		mdio_gpio_clock_the_bit(dev_cfg);
	}
}

static ALWAYS_INLINE void mdio_gpio_read(const struct mdio_gpio_config *dev_cfg, uint16_t *pdata)
{
	uint16_t data = 0;

	for (uint16_t i = 0; i < 16; i++) {
		data <<= 1;
		mdio_gpio_clock_the_bit(dev_cfg);
		if (gpio_pin_get_dt(&dev_cfg->mdio_gpio) == 1) {
			data |= 1;
		}
	}

	*pdata = data;
}

static ALWAYS_INLINE void mdio_gpio_write(const struct mdio_gpio_config *dev_cfg,
					  uint32_t data, uint8_t len)
{
	uint32_t v_data = data;
	uint32_t v_len = len;

	v_data <<= 32 - v_len;
	for (; v_len > 0; v_len--) {
		gpio_pin_set_dt(&dev_cfg->mdio_gpio, (v_data & MDIO_GPIO_MSB) ? 1 : 0);
		mdio_gpio_clock_the_bit(dev_cfg);
		v_data <<= 1;
	}
}

static int mdio_gpio_transfer(const struct device *dev, uint8_t prtad, uint8_t devad, uint8_t rw,
			      uint16_t data_in, uint16_t *data_out)
{
	const struct mdio_gpio_config *const dev_cfg = dev->config;
	struct mdio_gpio_data *const dev_data = dev->data;

	k_sem_take(&dev_data->sem, K_FOREVER);

	/* DIR: output */
	mdio_gpio_dir(dev_cfg, MDIO_GPIO_WRITE_OP);
	/* PRE32: 32 bits '1' for sync*/
	mdio_gpio_write(dev_cfg, 0xFFFFFFFF, 32);
	/* ST: 2 bits start of frame */
	mdio_gpio_write(dev_cfg, 0x1, 2);
	/* OP: 2 bits opcode, read '10' or write '01' */
	mdio_gpio_write(dev_cfg, rw ? 0x1 : 0x2, 2);
	/* PA5: 5 bits PHY address */
	mdio_gpio_write(dev_cfg, prtad, 5);
	/* RA5: 5 bits register address */
	mdio_gpio_write(dev_cfg, devad, 5);

	if (rw) { /* Write data */
		/* TA: 2 bits turn-around */
		mdio_gpio_write(dev_cfg, 0x2, 2);
		mdio_gpio_write(dev_cfg, data_in, 16);
	} else { /* Read data */
		/* Release the MDIO line */
		mdio_gpio_dir(dev_cfg, MDIO_GPIO_READ_OP);
		mdio_gpio_read(dev_cfg, data_out);
	}

	/* DIR: input. Tristate MDIO line */
	mdio_gpio_dir(dev_cfg, MDIO_GPIO_READ_OP);

	k_sem_give(&dev_data->sem);

	return 0;
}

static int mdio_gpio_read_mmi(const struct device *dev, uint8_t prtad, uint8_t devad,
			      uint16_t *data)
{
	return mdio_gpio_transfer(dev, prtad, devad, MDIO_GPIO_READ_OP, 0, data);
}

static int mdio_gpio_write_mmi(const struct device *dev, uint8_t prtad, uint8_t devad,
			       uint16_t data)
{
	return mdio_gpio_transfer(dev, prtad, devad, MDIO_GPIO_WRITE_OP, data, NULL);
}

static int mdio_gpio_initialize(const struct device *dev)
{
	const struct mdio_gpio_config *const dev_cfg = dev->config;
	struct mdio_gpio_data *const dev_data = dev->data;
	int rc;

	k_sem_init(&dev_data->sem, 1, 1);

	if (!device_is_ready(dev_cfg->mdc_gpio.port)) {
		LOG_ERR("GPIO port for MDC pin is not ready");
		return -ENODEV;
	}

	if (!device_is_ready(dev_cfg->mdio_gpio.port)) {
		LOG_ERR("GPIO port for MDIO pin is not ready");
		return -ENODEV;
	}

	rc = gpio_pin_configure_dt(&dev_cfg->mdc_gpio, GPIO_OUTPUT_INACTIVE);
	if (rc < 0) {
		LOG_ERR("Couldn't configure MDC pin; (%d)", rc);
		return rc;
	}

	rc = gpio_pin_configure_dt(&dev_cfg->mdio_gpio, GPIO_INPUT);
	if (rc < 0) {
		LOG_ERR("Couldn't configure MDIO pin; (%d)", rc);
		return rc;
	}

	return 0;
}

static const struct mdio_driver_api mdio_gpio_driver_api = {
	.read = mdio_gpio_read_mmi,
	.write = mdio_gpio_write_mmi,
};

#define MDIO_GPIO_CONFIG(inst)                                                                     \
	static struct mdio_gpio_config mdio_gpio_dev_config_##inst = {                             \
		.mdc_gpio = GPIO_DT_SPEC_INST_GET(inst, mdc_gpios),                                \
		.mdio_gpio = GPIO_DT_SPEC_INST_GET(inst, mdio_gpios),                              \
	};

#define MDIO_GPIO_DEVICE(inst)                                                                     \
	MDIO_GPIO_CONFIG(inst);                                                                    \
	static struct mdio_gpio_data mdio_gpio_dev_data_##inst;                                    \
	DEVICE_DT_INST_DEFINE(inst, &mdio_gpio_initialize, NULL, &mdio_gpio_dev_data_##inst,       \
			      &mdio_gpio_dev_config_##inst, POST_KERNEL,                           \
			      CONFIG_MDIO_INIT_PRIORITY, &mdio_gpio_driver_api);

DT_INST_FOREACH_STATUS_OKAY(MDIO_GPIO_DEVICE)
