1 /* Copyright Statement:
2  *
3  * This software/firmware and related documentation ("MediaTek Software") are
4  * protected under relevant copyright laws. The information contained herein
5  * is confidential and proprietary to MediaTek Inc. and/or its licensors.
6  * Without the prior written permission of MediaTek inc. and/or its licensors,
7  * any reproduction, modification, use or disclosure of MediaTek Software,
8  * and information contained herein, in whole or in part, shall be strictly prohibited.
9  *
10  * MediaTek Inc. (C) 2010. All rights reserved.
11  *
12  * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
13  * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
14  * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
15  * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
18  * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
19  * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
20  * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
21  * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
22  * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
23  * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
24  * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
25  * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
26  * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
27  * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
28  * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
29  * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
30  *
31  * The following software/firmware and/or related documentation ("MediaTek Software")
32  * have been modified by MediaTek Inc. All revisions are subject to any receiver's
33  * applicable license agreements with MediaTek Inc.
34  */
35 
36 #include <linux/version.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/kthread.h>
40 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/proc_fs.h>
44 #include <linux/string.h>
45 #include <linux/uaccess.h>
46 // #include <mach/mt6575_gpt.h> /* --- by chhung */
47 #include "dbg.h"
48 #include "mt6575_sd.h"
49 #include <linux/seq_file.h>
50 
51 static char cmd_buf[256];
52 
53 /* for debug zone */
54 unsigned int sd_debug_zone[4] = {
55 	0,
56 	0,
57 	0,
58 	0
59 };
60 
61 #if defined(MT6575_SD_DEBUG)
62 /* for driver profile */
63 #define TICKS_ONE_MS  (13000)
64 u32 gpt_enable;
65 u32 sdio_pro_enable;   /* make sure gpt is enabled */
66 u32 sdio_pro_time;     /* no more than 30s */
67 struct sdio_profile sdio_perfomance = {0};
68 
69 #if 0 /* --- chhung */
70 void msdc_init_gpt(void)
71 {
72 	GPT_CONFIG config;
73 
74 	config.num  = GPT6;
75 	config.mode = GPT_FREE_RUN;
76 	config.clkSrc = GPT_CLK_SRC_SYS;
77 	config.clkDiv = GPT_CLK_DIV_1;   /* 13MHz GPT6 */
78 
79 	if (GPT_Config(config) == FALSE)
80 		return;
81 
82 	GPT_Start(GPT6);
83 }
84 #endif /* end of --- */
85 
msdc_time_calc(u32 old_L32,u32 old_H32,u32 new_L32,u32 new_H32)86 u32 msdc_time_calc(u32 old_L32, u32 old_H32, u32 new_L32, u32 new_H32)
87 {
88 	u32 ret = 0;
89 
90 	if (new_H32 == old_H32) {
91 		ret = new_L32 - old_L32;
92 	} else if (new_H32 == (old_H32 + 1)) {
93 		if (new_L32 > old_L32)
94 			pr_debug("msdc old_L<0x%x> new_L<0x%x>\n", old_L32, new_L32);
95 		ret = (0xffffffff - old_L32);
96 		ret += new_L32;
97 	} else {
98 		pr_debug("msdc old_H<0x%x> new_H<0x%x>\n", old_H32, new_H32);
99 	}
100 
101 	return ret;
102 }
103 
msdc_sdio_profile(struct sdio_profile * result)104 void msdc_sdio_profile(struct sdio_profile *result)
105 {
106 	struct cmd_profile *cmd;
107 	u32 i;
108 
109 	pr_debug("sdio === performance dump ===\n");
110 	pr_debug("sdio === total execute tick<%d> time<%dms> Tx<%dB> Rx<%dB>\n",
111 		 result->total_tc, result->total_tc / TICKS_ONE_MS,
112 		 result->total_tx_bytes, result->total_rx_bytes);
113 
114 	/* CMD52 Dump */
115 	cmd = &result->cmd52_rx;
116 	pr_debug("sdio === CMD52 Rx <%d>times tick<%d> Max<%d> Min<%d> Aver<%d>\n", cmd->count, cmd->tot_tc,
117 		 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count);
118 	cmd = &result->cmd52_tx;
119 	pr_debug("sdio === CMD52 Tx <%d>times tick<%d> Max<%d> Min<%d> Aver<%d>\n", cmd->count, cmd->tot_tc,
120 		 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count);
121 
122 	/* CMD53 Rx bytes + block mode */
123 	for (i = 0; i < 512; i++) {
124 		cmd = &result->cmd53_rx_byte[i];
125 		if (cmd->count) {
126 			pr_debug("sdio<%6d><%3dB>_Rx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
127 				 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count,
128 				 cmd->tot_bytes, (cmd->tot_bytes / 10) * 13 / (cmd->tot_tc / 10));
129 		}
130 	}
131 	for (i = 0; i < 100; i++) {
132 		cmd = &result->cmd53_rx_blk[i];
133 		if (cmd->count) {
134 			pr_debug("sdio<%6d><%3d>B_Rx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
135 				 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count,
136 				 cmd->tot_bytes, (cmd->tot_bytes / 10) * 13 / (cmd->tot_tc / 10));
137 		}
138 	}
139 
140 	/* CMD53 Tx bytes + block mode */
141 	for (i = 0; i < 512; i++) {
142 		cmd = &result->cmd53_tx_byte[i];
143 		if (cmd->count) {
144 			pr_debug("sdio<%6d><%3dB>_Tx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
145 				 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count,
146 				 cmd->tot_bytes, (cmd->tot_bytes / 10) * 13 / (cmd->tot_tc / 10));
147 		}
148 	}
149 	for (i = 0; i < 100; i++) {
150 		cmd = &result->cmd53_tx_blk[i];
151 		if (cmd->count) {
152 			pr_debug("sdio<%6d><%3d>B_Tx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
153 				 cmd->max_tc, cmd->min_tc, cmd->tot_tc / cmd->count,
154 				 cmd->tot_bytes, (cmd->tot_bytes / 10) * 13 / (cmd->tot_tc / 10));
155 		}
156 	}
157 
158 	pr_debug("sdio === performance dump done ===\n");
159 }
160 
161 //========= sdio command table ===========
msdc_performance(u32 opcode,u32 sizes,u32 bRx,u32 ticks)162 void msdc_performance(u32 opcode, u32 sizes, u32 bRx, u32 ticks)
163 {
164 	struct sdio_profile *result = &sdio_perfomance;
165 	struct cmd_profile *cmd;
166 	u32 block;
167 
168 	if (sdio_pro_enable == 0)
169 		return;
170 
171 	if (opcode == 52) {
172 		cmd = bRx ?  &result->cmd52_rx : &result->cmd52_tx;
173 	} else if (opcode == 53) {
174 		if (sizes < 512) {
175 			cmd = bRx ?  &result->cmd53_rx_byte[sizes] : &result->cmd53_tx_byte[sizes];
176 		} else {
177 			block = sizes / 512;
178 			if (block >= 99) {
179 				pr_err("cmd53 error blocks\n");
180 				while (1)
181 					;
182 			}
183 			cmd = bRx ?  &result->cmd53_rx_blk[block] : &result->cmd53_tx_blk[block];
184 		}
185 	} else {
186 		return;
187 	}
188 
189 	/* update the members */
190 	if (ticks > cmd->max_tc)
191 		cmd->max_tc = ticks;
192 	if (cmd->min_tc == 0 || ticks < cmd->min_tc)
193 		cmd->min_tc = ticks;
194 	cmd->tot_tc += ticks;
195 	cmd->tot_bytes += sizes;
196 	cmd->count++;
197 
198 	if (bRx)
199 		result->total_rx_bytes += sizes;
200 	else
201 		result->total_tx_bytes += sizes;
202 	result->total_tc += ticks;
203 
204 	/* dump when total_tc > 30s */
205 	if (result->total_tc >= sdio_pro_time * TICKS_ONE_MS * 1000) {
206 		msdc_sdio_profile(result);
207 		memset(result, 0, sizeof(struct sdio_profile));
208 	}
209 }
210 
211 //========== driver proc interface ===========
msdc_debug_proc_read(struct seq_file * s,void * p)212 static int msdc_debug_proc_read(struct seq_file *s, void *p)
213 {
214 	seq_puts(s, "\n=========================================\n");
215 	seq_puts(s, "Index<0> + Id + Zone\n");
216 	seq_puts(s, "-> PWR<9> WRN<8> | FIO<7> OPS<6> FUN<5> CFG<4> | INT<3> RSP<2> CMD<1> DMA<0>\n");
217 	seq_puts(s, "-> echo 0 3 0x3ff >msdc_bebug -> host[3] debug zone set to 0x3ff\n");
218 	seq_printf(s, "-> MSDC[0] Zone: 0x%.8x\n", sd_debug_zone[0]);
219 	seq_printf(s, "-> MSDC[1] Zone: 0x%.8x\n", sd_debug_zone[1]);
220 	seq_printf(s, "-> MSDC[2] Zone: 0x%.8x\n", sd_debug_zone[2]);
221 	seq_printf(s, "-> MSDC[3] Zone: 0x%.8x\n", sd_debug_zone[3]);
222 
223 	seq_puts(s, "Index<3> + SDIO_PROFILE + TIME\n");
224 	seq_puts(s, "-> echo 3 1 0x1E >msdc_bebug -> enable sdio_profile, 30s\n");
225 	seq_printf(s, "-> SDIO_PROFILE<%d> TIME<%ds>\n", sdio_pro_enable, sdio_pro_time);
226 	seq_puts(s, "=========================================\n\n");
227 
228 	return 0;
229 }
230 
msdc_debug_proc_write(struct file * file,const char __user * buf,size_t count,loff_t * data)231 static ssize_t msdc_debug_proc_write(struct file *file,
232 				     const char __user *buf,
233 				     size_t count, loff_t *data)
234 {
235 	int ret;
236 
237 	int cmd, p1, p2;
238 	int id, zone;
239 	int mode, size;
240 
241 	if (count == 0)
242 		return -1;
243 	if (count > 255)
244 		count = 255;
245 
246 	if (copy_from_user(cmd_buf, buf, count))
247 		return -EFAULT;
248 
249 	cmd_buf[count] = '\0';
250 	pr_debug("msdc Write %s\n", cmd_buf);
251 
252 	sscanf(cmd_buf, "%x %x %x", &cmd, &p1, &p2);
253 
254 	if (cmd == SD_TOOL_ZONE) {
255 		id = p1;
256 		zone = p2;
257 		zone &= 0x3ff;
258 		pr_debug("msdc host_id<%d> zone<0x%.8x>\n", id, zone);
259 		if (id >= 0 && id <= 3) {
260 			sd_debug_zone[id] = zone;
261 		} else if (id == 4) {
262 			sd_debug_zone[0] = sd_debug_zone[1] = zone;
263 			sd_debug_zone[2] = sd_debug_zone[3] = zone;
264 		} else {
265 			pr_err("msdc host_id error when set debug zone\n");
266 		}
267 	} else if (cmd == SD_TOOL_SDIO_PROFILE) {
268 		if (p1 == 1) { /* enable profile */
269 			if (gpt_enable == 0) {
270 				// msdc_init_gpt(); /* --- by chhung */
271 				gpt_enable = 1;
272 			}
273 			sdio_pro_enable = 1;
274 			if (p2 == 0)
275 				p2 = 1;
276 			if (p2 >= 30)
277 				p2 = 30;
278 			sdio_pro_time = p2;
279 		} else if (p1 == 0) {
280 			/* todo */
281 			sdio_pro_enable = 0;
282 		}
283 	}
284 
285 	return count;
286 }
287 
msdc_debug_show(struct inode * inode,struct file * file)288 static int msdc_debug_show(struct inode *inode, struct file *file)
289 {
290 	return single_open(file, msdc_debug_proc_read, NULL);
291 }
292 
293 static const struct file_operations msdc_debug_fops = {
294 	.owner		= THIS_MODULE,
295 	.open		= msdc_debug_show,
296 	.read		= seq_read,
297 	.write		= msdc_debug_proc_write,
298 	.llseek		= seq_lseek,
299 	.release	= single_release,
300 };
301 
msdc_debug_proc_init(void)302 void msdc_debug_proc_init(void)
303 {
304 	proc_create("msdc_debug", 0660, NULL, &msdc_debug_fops);
305 }
306 EXPORT_SYMBOL_GPL(msdc_debug_proc_init);
307 #endif
308