blob: 7452b82f110557726d66c458f4cd69589118b525 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese05b0ef42013-04-09 21:06:08 +00002/*
3 * Copyright 2013 Stefan Roese <sr@denx.de>
Stefan Roese05b0ef42013-04-09 21:06:08 +00004 */
5
Simon Glass2dc9c342020-05-10 11:40:01 -06006#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Jeroen Hofstee1abf3a12014-10-08 22:57:52 +02008#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Stefan Roese05b0ef42013-04-09 21:06:08 +000012#include <asm/io.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020013#include <asm/mach-imx/regs-common.h>
Stefan Roese05b0ef42013-04-09 21:06:08 +000014
Ye Life96e152019-01-04 09:10:20 +000015DECLARE_GLOBAL_DATA_PTR;
16
Stefan Roese05b0ef42013-04-09 21:06:08 +000017/* 1 second delay should be plenty of time for block reset. */
18#define RESET_MAX_TIMEOUT 1000000
19
20#define MXS_BLOCK_SFTRST (1 << 31)
21#define MXS_BLOCK_CLKGATE (1 << 30)
22
23int mxs_wait_mask_set(struct mxs_register_32 *reg, uint32_t mask, unsigned
24 int timeout)
25{
26 while (--timeout) {
27 if ((readl(&reg->reg) & mask) == mask)
28 break;
29 udelay(1);
30 }
31
32 return !timeout;
33}
34
35int mxs_wait_mask_clr(struct mxs_register_32 *reg, uint32_t mask, unsigned
36 int timeout)
37{
38 while (--timeout) {
39 if ((readl(&reg->reg) & mask) == 0)
40 break;
41 udelay(1);
42 }
43
44 return !timeout;
45}
46
47int mxs_reset_block(struct mxs_register_32 *reg)
48{
49 /* Clear SFTRST */
50 writel(MXS_BLOCK_SFTRST, &reg->reg_clr);
51
52 if (mxs_wait_mask_clr(reg, MXS_BLOCK_SFTRST, RESET_MAX_TIMEOUT))
53 return 1;
54
55 /* Clear CLKGATE */
56 writel(MXS_BLOCK_CLKGATE, &reg->reg_clr);
57
58 /* Set SFTRST */
59 writel(MXS_BLOCK_SFTRST, &reg->reg_set);
60
61 /* Wait for CLKGATE being set */
62 if (mxs_wait_mask_set(reg, MXS_BLOCK_CLKGATE, RESET_MAX_TIMEOUT))
63 return 1;
64
65 /* Clear SFTRST */
66 writel(MXS_BLOCK_SFTRST, &reg->reg_clr);
67
68 if (mxs_wait_mask_clr(reg, MXS_BLOCK_SFTRST, RESET_MAX_TIMEOUT))
69 return 1;
70
71 /* Clear CLKGATE */
72 writel(MXS_BLOCK_CLKGATE, &reg->reg_clr);
73
74 if (mxs_wait_mask_clr(reg, MXS_BLOCK_CLKGATE, RESET_MAX_TIMEOUT))
75 return 1;
76
77 return 0;
78}