162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// Copyright 2020 NXP
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Common helpers for the audio DSP on i.MX8
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/module.h>
862306a36Sopenharmony_ci#include <sound/sof/xtensa.h>
962306a36Sopenharmony_ci#include "../ops.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "imx-common.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/**
1462306a36Sopenharmony_ci * imx8_get_registers() - This function is called in case of DSP oops
1562306a36Sopenharmony_ci * in order to gather information about the registers, filename and
1662306a36Sopenharmony_ci * linenumber and stack.
1762306a36Sopenharmony_ci * @sdev: SOF device
1862306a36Sopenharmony_ci * @xoops: Stores information about registers.
1962306a36Sopenharmony_ci * @panic_info: Stores information about filename and line number.
2062306a36Sopenharmony_ci * @stack: Stores the stack dump.
2162306a36Sopenharmony_ci * @stack_words: Size of the stack dump.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_civoid imx8_get_registers(struct snd_sof_dev *sdev,
2462306a36Sopenharmony_ci			struct sof_ipc_dsp_oops_xtensa *xoops,
2562306a36Sopenharmony_ci			struct sof_ipc_panic_info *panic_info,
2662306a36Sopenharmony_ci			u32 *stack, size_t stack_words)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	u32 offset = sdev->dsp_oops_offset;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	/* first read registers */
3162306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	/* then get panic info */
3462306a36Sopenharmony_ci	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
3562306a36Sopenharmony_ci		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
3662306a36Sopenharmony_ci			xoops->arch_hdr.totalsize);
3762306a36Sopenharmony_ci		return;
3862306a36Sopenharmony_ci	}
3962306a36Sopenharmony_ci	offset += xoops->arch_hdr.totalsize;
4062306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/* then get the stack */
4362306a36Sopenharmony_ci	offset += sizeof(*panic_info);
4462306a36Sopenharmony_ci	sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/**
4862306a36Sopenharmony_ci * imx8_dump() - This function is called when a panic message is
4962306a36Sopenharmony_ci * received from the firmware.
5062306a36Sopenharmony_ci * @sdev: SOF device
5162306a36Sopenharmony_ci * @flags: parameter not used but required by ops prototype
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_civoid imx8_dump(struct snd_sof_dev *sdev, u32 flags)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	struct sof_ipc_dsp_oops_xtensa xoops;
5662306a36Sopenharmony_ci	struct sof_ipc_panic_info panic_info;
5762306a36Sopenharmony_ci	u32 stack[IMX8_STACK_DUMP_SIZE];
5862306a36Sopenharmony_ci	u32 status;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/* Get information about the panic status from the debug box area.
6162306a36Sopenharmony_ci	 * Compute the trace point based on the status.
6262306a36Sopenharmony_ci	 */
6362306a36Sopenharmony_ci	sof_mailbox_read(sdev, sdev->debug_box.offset + 0x4, &status, 4);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/* Get information about the registers, the filename and line
6662306a36Sopenharmony_ci	 * number and the stack.
6762306a36Sopenharmony_ci	 */
6862306a36Sopenharmony_ci	imx8_get_registers(sdev, &xoops, &panic_info, stack,
6962306a36Sopenharmony_ci			   IMX8_STACK_DUMP_SIZE);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	/* Print the information to the console */
7262306a36Sopenharmony_ci	sof_print_oops_and_stack(sdev, KERN_ERR, status, status, &xoops,
7362306a36Sopenharmony_ci				 &panic_info, stack, IMX8_STACK_DUMP_SIZE);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ciEXPORT_SYMBOL(imx8_dump);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciint imx8_parse_clocks(struct snd_sof_dev *sdev, struct imx_clocks *clks)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	int ret;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	ret = devm_clk_bulk_get(sdev->dev, clks->num_dsp_clks, clks->dsp_clks);
8262306a36Sopenharmony_ci	if (ret)
8362306a36Sopenharmony_ci		dev_err(sdev->dev, "Failed to request DSP clocks\n");
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	return ret;
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ciEXPORT_SYMBOL(imx8_parse_clocks);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciint imx8_enable_clocks(struct snd_sof_dev *sdev, struct imx_clocks *clks)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	return clk_bulk_prepare_enable(clks->num_dsp_clks, clks->dsp_clks);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ciEXPORT_SYMBOL(imx8_enable_clocks);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_civoid imx8_disable_clocks(struct snd_sof_dev *sdev, struct imx_clocks *clks)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	clk_bulk_disable_unprepare(clks->num_dsp_clks, clks->dsp_clks);
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ciEXPORT_SYMBOL(imx8_disable_clocks);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
102