1 /*
2  * Copyright (C) 1999 ARM Limited
3  * Copyright (C) 2000 Deep Blue Solutions Ltd
4  * Copyright 2006-2007,2010 Freescale Semiconductor, Inc. All Rights Reserved.
5  * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
6  * Copyright 2009 Ilya Yanok, Emcraft Systems Ltd, yanok@emcraft.com
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 
27 #include <asm/proc-fns.h>
28 #include <asm/system_misc.h>
29 
30 #include <mach/mxs.h>
31 #include <mach/common.h>
32 
33 #define MX23_CLKCTRL_RESET_OFFSET	0x120
34 #define MX28_CLKCTRL_RESET_OFFSET	0x1e0
35 #define MXS_CLKCTRL_RESET_CHIP		(1 << 1)
36 
37 #define MXS_MODULE_CLKGATE		(1 << 30)
38 #define MXS_MODULE_SFTRST		(1 << 31)
39 
40 #define CLKCTRL_TIMEOUT		10	/* 10 ms */
41 
42 static void __iomem *mxs_clkctrl_reset_addr;
43 
44 /*
45  * Reset the system. It is called by machine_restart().
46  */
mxs_restart(char mode,const char * cmd)47 void mxs_restart(char mode, const char *cmd)
48 {
49 	/* reset the chip */
50 	__mxs_setl(MXS_CLKCTRL_RESET_CHIP, mxs_clkctrl_reset_addr);
51 
52 	pr_err("Failed to assert the chip reset\n");
53 
54 	/* Delay to allow the serial port to show the message */
55 	mdelay(50);
56 
57 	/* We'll take a jump through zero as a poor second */
58 	soft_restart(0);
59 }
60 
mxs_arch_reset_init(void)61 static int __init mxs_arch_reset_init(void)
62 {
63 	struct clk *clk;
64 
65 	mxs_clkctrl_reset_addr = MXS_IO_ADDRESS(MXS_CLKCTRL_BASE_ADDR) +
66 				(cpu_is_mx23() ? MX23_CLKCTRL_RESET_OFFSET :
67 						 MX28_CLKCTRL_RESET_OFFSET);
68 
69 	clk = clk_get_sys("rtc", NULL);
70 	if (!IS_ERR(clk))
71 		clk_prepare_enable(clk);
72 
73 	return 0;
74 }
75 core_initcall(mxs_arch_reset_init);
76 
77 /*
78  * Clear the bit and poll it cleared.  This is usually called with
79  * a reset address and mask being either SFTRST(bit 31) or CLKGATE
80  * (bit 30).
81  */
clear_poll_bit(void __iomem * addr,u32 mask)82 static int clear_poll_bit(void __iomem *addr, u32 mask)
83 {
84 	int timeout = 0x400;
85 
86 	/* clear the bit */
87 	__mxs_clrl(mask, addr);
88 
89 	/*
90 	 * SFTRST needs 3 GPMI clocks to settle, the reference manual
91 	 * recommends to wait 1us.
92 	 */
93 	udelay(1);
94 
95 	/* poll the bit becoming clear */
96 	while ((__raw_readl(addr) & mask) && --timeout)
97 		/* nothing */;
98 
99 	return !timeout;
100 }
101 
mxs_reset_block(void __iomem * reset_addr)102 int mxs_reset_block(void __iomem *reset_addr)
103 {
104 	int ret;
105 	int timeout = 0x400;
106 
107 	/* clear and poll SFTRST */
108 	ret = clear_poll_bit(reset_addr, MXS_MODULE_SFTRST);
109 	if (unlikely(ret))
110 		goto error;
111 
112 	/* clear CLKGATE */
113 	__mxs_clrl(MXS_MODULE_CLKGATE, reset_addr);
114 
115 	/* set SFTRST to reset the block */
116 	__mxs_setl(MXS_MODULE_SFTRST, reset_addr);
117 	udelay(1);
118 
119 	/* poll CLKGATE becoming set */
120 	while ((!(__raw_readl(reset_addr) & MXS_MODULE_CLKGATE)) && --timeout)
121 		/* nothing */;
122 	if (unlikely(!timeout))
123 		goto error;
124 
125 	/* clear and poll SFTRST */
126 	ret = clear_poll_bit(reset_addr, MXS_MODULE_SFTRST);
127 	if (unlikely(ret))
128 		goto error;
129 
130 	/* clear and poll CLKGATE */
131 	ret = clear_poll_bit(reset_addr, MXS_MODULE_CLKGATE);
132 	if (unlikely(ret))
133 		goto error;
134 
135 	return 0;
136 
137 error:
138 	pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
139 	return -ETIMEDOUT;
140 }
141 EXPORT_SYMBOL(mxs_reset_block);
142 
mxs_clkctrl_timeout(unsigned int reg_offset,unsigned int mask)143 int mxs_clkctrl_timeout(unsigned int reg_offset, unsigned int mask)
144 {
145 	unsigned long timeout = jiffies + msecs_to_jiffies(CLKCTRL_TIMEOUT);
146 	while (readl_relaxed(MXS_IO_ADDRESS(MXS_CLKCTRL_BASE_ADDR)
147 						+ reg_offset) & mask) {
148 		if (time_after(jiffies, timeout)) {
149 			pr_err("Timeout at CLKCTRL + 0x%x\n", reg_offset);
150 			return -ETIMEDOUT;
151 		}
152 	}
153 
154 	return 0;
155 }
156