162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * MSDI IP block reset
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2012 Texas Instruments, Inc.
662306a36Sopenharmony_ci * Paul Walmsley
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * XXX What about pad muxing?
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/kernel.h>
1262306a36Sopenharmony_ci#include <linux/err.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "prm.h"
1562306a36Sopenharmony_ci#include "common.h"
1662306a36Sopenharmony_ci#include "control.h"
1762306a36Sopenharmony_ci#include "omap_hwmod.h"
1862306a36Sopenharmony_ci#include "omap_device.h"
1962306a36Sopenharmony_ci#include "mmc.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * MSDI_CON_OFFSET: offset in bytes of the MSDI IP block's CON register
2362306a36Sopenharmony_ci *     from the IP block's base address
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci#define MSDI_CON_OFFSET				0x0c
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* Register bitfields in the CON register */
2862306a36Sopenharmony_ci#define MSDI_CON_POW_MASK			BIT(11)
2962306a36Sopenharmony_ci#define MSDI_CON_CLKD_MASK			(0x3f << 0)
3062306a36Sopenharmony_ci#define MSDI_CON_CLKD_SHIFT			0
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* MSDI_TARGET_RESET_CLKD: clock divisor to use throughout the reset */
3362306a36Sopenharmony_ci#define MSDI_TARGET_RESET_CLKD		0x3ff
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/**
3662306a36Sopenharmony_ci * omap_msdi_reset - reset the MSDI IP block
3762306a36Sopenharmony_ci * @oh: struct omap_hwmod *
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * The MSDI IP block on OMAP2420 has to have both the POW and CLKD
4062306a36Sopenharmony_ci * fields set inside its CON register for a reset to complete
4162306a36Sopenharmony_ci * successfully.  This is not documented in the TRM.  For CLKD, we use
4262306a36Sopenharmony_ci * the value that results in the lowest possible clock rate, to attempt
4362306a36Sopenharmony_ci * to avoid disturbing any cards.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ciint omap_msdi_reset(struct omap_hwmod *oh)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	u16 v = 0;
4862306a36Sopenharmony_ci	int c = 0;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* Write to the SOFTRESET bit */
5162306a36Sopenharmony_ci	omap_hwmod_softreset(oh);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* Enable the MSDI core and internal clock */
5462306a36Sopenharmony_ci	v |= MSDI_CON_POW_MASK;
5562306a36Sopenharmony_ci	v |= MSDI_TARGET_RESET_CLKD << MSDI_CON_CLKD_SHIFT;
5662306a36Sopenharmony_ci	omap_hwmod_write(v, oh, MSDI_CON_OFFSET);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	/* Poll on RESETDONE bit */
5962306a36Sopenharmony_ci	omap_test_timeout((omap_hwmod_read(oh, oh->class->sysc->syss_offs)
6062306a36Sopenharmony_ci			   & SYSS_RESETDONE_MASK),
6162306a36Sopenharmony_ci			  MAX_MODULE_SOFTRESET_WAIT, c);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	if (c == MAX_MODULE_SOFTRESET_WAIT)
6462306a36Sopenharmony_ci		pr_warn("%s: %s: softreset failed (waited %d usec)\n",
6562306a36Sopenharmony_ci			__func__, oh->name, MAX_MODULE_SOFTRESET_WAIT);
6662306a36Sopenharmony_ci	else
6762306a36Sopenharmony_ci		pr_debug("%s: %s: softreset in %d usec\n", __func__,
6862306a36Sopenharmony_ci			 oh->name, c);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	/* Disable the MSDI internal clock */
7162306a36Sopenharmony_ci	v &= ~MSDI_CON_CLKD_MASK;
7262306a36Sopenharmony_ci	omap_hwmod_write(v, oh, MSDI_CON_OFFSET);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	return 0;
7562306a36Sopenharmony_ci}
76