/*
 * Copyright (c) 2024, NVIDIA CORPORATION.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <osa/rtos-task.h>
#include <misc/macros.h>
#include <misc/bitops.h>
#include <argpcdma_ao.h>
#include <spi-slv/spi-slave.h>
#include <processor/spi-hw.h>
#include <processor/gpcdma-hw.h>
#include <printf-isr.h>
#include <err-hook.h>


#include "spi-slv-app.h"

#define SPI_TEST_CONTROLLER	spi_ctlr_spi2
#define SPI_TEST_CLOCK_RATE	12000000
#define SPI_TEST_DMA_TX_CHANNEL	2
#define SPI_TEST_DMA_RX_CHANNEL	3

#define SPI_TEST_DELAY		500
#define SPI_BUFFER_SIZE		2

/* Data expected from Master */
static uint8_t txBuffer[SPI_BUFFER_SIZE] = { 0xcc, 0x55 };

static void spi_slv_test_task(void *pvParameters)
{
	int ret;
	(void)pvParameters; /* unused */

	uint8_t rx[SPI_BUFFER_SIZE];
	uint32_t i = 0;
	uint32_t errs = 0;
	memset(rx, 0x0, ARRAY_SIZE(rx) * sizeof(uint8_t));

	struct spi_client_setting spi_slv_test_device = {
		.chip_select = 0U,
		/* Optional params for cs */
		.cs_setup_clk_count = 16,
		.cs_hold_clk_count = 16,
		.cs_inactive_cycles = 32,
		.set_rx_tap_delay = false,
		.spi_max_clk_rate = SPI_TEST_CLOCK_RATE,
		.spi_no_dma = false,
	};

	struct spi_slave_init_setup spi_slv_setup = {
		.dma_id = &gpcdma_id_aon,
		.dma_chans.tx = SPI_TEST_DMA_TX_CHANNEL,
		.dma_chans.rx = SPI_TEST_DMA_RX_CHANNEL,
		.spi_max_clk_rate = SPI_TEST_CLOCK_RATE,
		.dma_slave_req = GPCDMA_AO_CHANNEL_CH0_CSR_0_REQ_SEL_SPI,
	};

	ret = spi_slave_init(&SPI_TEST_CONTROLLER, &spi_slv_setup);
	if (ret) {
		printf("spi_slv_test: slave init failed\r\n");
		goto out;
	}

	ret = spi_slave_setup(&SPI_TEST_CONTROLLER, &spi_slv_test_device);
	if (ret) {
		printf("spi_slv_test: couldn't setup SPI slave device\r\n");
		goto out;
	}

	struct spi_xfer xfer = {
		.flags = BIT(TEGRA_SPI_XFER_LAST_MSG) | BIT(TEGRA_SPI_XFER_FIRST_MSG),
		.tx_buf = NULL,
		.rx_buf = (uint8_t *)&rx,
		.len = SPI_BUFFER_SIZE,
		.chip_select = 0,
		.tx_nbits = TEGRA_SPI_NBITS_SINGLE,
		.rx_nbits = TEGRA_SPI_NBITS_SINGLE,
		.bits_per_word = 8,
		.mode = TEGRA_SPI_MODE_0 | TEGRA_SPI_LSBYTE_FIRST,
	};

	ret = spi_slave_transfer(&SPI_TEST_CONTROLLER, &xfer);
	if (ret != E_SUCCESS) {
		printf("spi_slave_test: not ready to accept the data\r\n");
		goto out;
	}

	for (i = 0; i < ARRAY_SIZE(rx); i++) {
		if(rx[i] != txBuffer[i]) {
			errs++;
			break;
		}
	}

	if (errs != 0) {
		printf("** SPI Slave test Failed **\r\n");
	} else {
		printf("** SPI Slave test Passed **\r\n");
	}

out:
	rtosTaskDelay(SPI_TEST_DELAY);
	rtosTaskDelete(NULL);
}

void spi_slv_app_init(void)
{
	int ret;

	rtosTaskParameters spi_slv_test_task_params = {
		.pvTaskCode = &spi_slv_test_task,
		.pcTaskName = (char *)"spi_slv_test",
		.uxPriority = rtosIDLE_PRIORITY,
		.pvParameters = NULL,
		.uxStackDepthBytes = 512,
	};
	printf("spi_slv_test: Launching task...\n\r");
	ret = rtosTaskCreate(&spi_slv_test_task_params, NULL);
	if (ret != rtosPASS)
		error_hook("xTaskCreate for spi_slv_test_task_params\r\n");
}
