blob: 22de7afedf9c887d2be3e336127c43a76504c154 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* 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) 2012. 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/io.h>
37#include <asm/cacheflush.h>
38#include <asm/uaccess.h>
39/* #include <asm/system.h> */
40#include "mtk_dramc.h"
41//#include "x_hal_io.h"
42
43/* #define DRAMC_DEBUG */
44#ifndef DRAMC_DEBUG
45#define DRAMC_LOG(fmt, ...)
46#else
47#define DRAMC_LOG(fmt, arg...) printk("%s:%d:"fmt, __FILE__, __LINE__,##arg);
48#endif
49
50/* #define TEST_AGENT */
51
52
53#if 1
54struct dramc_desc_t dramc_desc[DRAMC_MX_CHANNUM][DRAMC_MX_GRPNUM][DRAMC_MX_NUM_IN_AGRP] = DRAMC_AGENT_TABLE;
55#else
56struct dramc_desc_t dramc_desc[DRAMC_MX_CHANNUM][DRAMC_MX_GRPNUM][DRAMC_MX_NUM_IN_AGRP] =
57{
58/* /////////////////////////////////////////////////////////
59 define channel A releationship between group and name
60 ///////////////////////////////////////////////////////// */
61 {
62 {
63 /* GRP 1 */
64 {0, "00_audio"},
65 {1, "01_demux/gcpu/ddi"},
66 {2, "02_vbi/3d/tve/dolby"},
67 {3, "03_xpscaler_ip/tddc"},
68 {4, "04_mib"},
69 {5, "05_b2r"},
70 {6, "06_cpu"},
71 {7, "07_scpos"},
72 {8, "08_vdec_vld"},
73 {9, "09_audio_dsp1"},
74 {10, "10_3d_gpu"},
75 {11, "11_2d_graph/jpgdec/osd/gfx_imgrsz/2d_graph_cmd/irt_dma/png"},
76 {12, "12_ethernet/demod_isdbt/ci_spi/rs232/usb2/usb3/usb3_d"},
77 {13, "13_osd"},
78 {14, "14_venc/vp8_encoder"},
79 {15, "15_test0/audio_dsp0"},
80 {-1, ""}
81 },
82 {
83 /* GRP 2 */
84 {16, "16_vdec_lat"},
85 {17, "17_mmu"},
86 {18, "18_memc"},
87 {19, "19_video_imgrsz0/video_imgrsz2"},
88 {20, "20_arm11"},
89 {21, "21_msdc/emmc/gdma"},
90 {22, "22_vdec_mc"},
91 {30, "30_agent_30"},
92 {-1, ""}
93 },
94 {
95 /* GRP 3 */
96 {23, "23_cpu_bim_read"},
97 {24, "24_demux/gcpu/ddi "},
98 {25, "25_nfi_dma/sfalsh_dma/lzhs/ci_spi"},
99 {26, "26_rs232"},
100 {27, "27_agent_27"},
101 {28, "28_agent_28"},
102 {29, "29_agent_29"},
103 {31, "31_agent_31"},
104 {-1, ""}
105 }
106 },
107
108/* ///////////////////////////////////////////////////////////
109 define channel B releationship between group and name
110 ///////////////////////////////////////////////////////// */
111 {
112 {
113 /* GRP 1 */
114 {0, "00_audio"},
115 {1, "01_demux/gcpu/ddi"},
116 {2, "02_vbi/3d/tve/dolby"},
117 {3, "03_xpscaler_ip/tddc"},
118 {4, "04_mib"},
119 {5, "05_b2r"},
120 {6, "06_cpu"},
121 {7, "07_scpos"},
122 {8, "08_vdec_vld"},
123 {9, "09_audio_dsp1"},
124 {10, "10_3d_gpu"},
125 {11, "11_2d_graph/jpgdec/osd/gfx_imgrsz/2d_graph_cmd/irt_dma/png"},
126 {12, "12_ethernet/demod_isdbt/ci_spi/rs232/usb2/usb3/usb3_d"},
127 {13, "13_osd"},
128 {14, "14_venc/vp8_encoder"},
129 {15, "15_test0/audio_dsp0"},
130 {-1, ""}
131 },
132 {
133 /* GRP 2 */
134 {16, "16_vdec_lat"},
135 {17, "17_mmu"},
136 {18, "18_memc"},
137 {19, "19_video_imgrsz0/video_imgrsz2"},
138 {20, "20_arm11"},
139 {21, "21_msdc/emmc/gdma"},
140 {22, "22_vdec_mc"},
141 {30, "30_agent_30"},
142 {-1, ""}
143 },
144 {
145 /* GRP 3 */
146 {23, "23_cpu_bim_read"},
147 {24, "24_demux/gcpu/ddi "},
148 {25, "25_nfi_dma/sfalsh_dma/lzhs/ci_spi"},
149 {26, "26_rs232"},
150 {27, "27_agent_27"},
151 {28, "28_agent_28"},
152 {29, "29_agent_29"},
153 {31, "31_agent_31"},
154 {-1, ""}
155 }
156 },
157
158/* ///////////////////////////////////////////////////////////
159 define channel C releationship between group and name
160 /////////////////////////////////////////////////////////// */
161 {
162 {
163 /* GRP 1 */
164 {0, "00_audio"},
165 {1, "01_demux/gcpu/ddi"},
166 {2, "02_vbi/3d/tve/dolby"},
167 {3, "03_xpscaler_ip/tddc"},
168 {4, "04_mib"},
169 {5, "05_b2r"},
170 {6, "06_cpu"},
171 {7, "07_scpos"},
172 {8, "08_vdec_vld"},
173 {9, "09_audio_dsp1"},
174 {10, "10_3d_gpu"},
175 {11, "11_2d_graph/jpgdec/osd/gfx_imgrsz/2d_graph_cmd/irt_dma/png"},
176 {12, "12_ethernet/demod_isdbt/ci_spi/rs232/usb2/usb3/usb3_d"},
177 {13, "13_osd"},
178 {14, "14_venc/vp8_encoder"},
179 {15, "15_test0/audio_dsp0"},
180 {-1, ""}
181 },
182 {
183 /* GRP 2 */
184 {16, "16_vdec_lat"},
185 {17, "17_mmu"},
186 {18, "18_memc"},
187 {19, "19_video_imgrsz0/video_imgrsz2"},
188 {20, "20_arm11"},
189 {21, "21_msdc/emmc/gdma"},
190 {22, "22_vdec_mc"},
191 {30, "30_agent_30"},
192 {-1, ""}
193 },
194 {
195 /* GRP 3 */
196 {23, "23_cpu_bim_read"},
197 {24, "24_demux/gcpu/ddi "},
198 {25, "25_nfi_dma/sfalsh_dma/lzhs/ci_spi"},
199 {26, "26_rs232"},
200 {27, "27_agent_27"},
201 {28, "28_agent_28"},
202 {29, "29_agent_29"},
203 {31, "31_agent_31"},
204 {-1, ""}
205 }
206 }
207
208};
209#endif
210
211static unsigned long dramc_base[DRAMC_MX_CHANNUM] =
212{
213 MET_DRAMC0_BASE,
214};
215
216/*
217force to 32 bit means current is 16 bit
218not force to 32 bit means current is 32 bit
219*/
220static unsigned long dramc_ext32_off[DRAMC_MX_CHANNUM] =
221{
222 DRAM_CHA_FORCE32,
223};
224
225
226static inline unsigned int dramc_reg_read(unsigned long addr)
227{
228#if 0
229#ifdef CONFIG_64BIT
230 unsigned int value = 0;
231 value = HAL_READ32((void*)addr);
232#else
233 unsigned int value = readl((void*)addr);
234#endif
235#endif
236
237 unsigned int value = readl((void*)addr);
238
239 mb();
240 return value;
241}
242
243static inline void dramc_reg_write(unsigned long addr, unsigned int value)
244{
245#if 0
246 /* make sure writel() be completed before outer_sync() */
247#ifdef CONFIG_64BIT
248 HAL_WRITE32((void*)addr, value);
249#else
250 writel(value, (void*)addr);
251#endif
252#endif
253
254 writel(value, (void*)addr);
255 mb();
256}
257
258#define DRAMC_SET_VALUE(target, value, shift, bit) \
259do { \
260 volatile u32 temp = dramc_reg_read(target); \
261 u32 mask1 = (~(((0xFFFFFFFF >> (32 - bit))<< shift))); \
262 u32 mask2 = ((0xFFFFFFFF >> (32 - bit))<< shift); \
263 dramc_reg_write(target,(temp & mask1) | ((value << shift) & mask2)); \
264} while (0)
265
266void DRAMC_Init(int chan)
267{
268 unsigned long base = dramc_base[chan];
269 volatile u32 temp_reg = 0;
270
271 DRAMC_LOG("%s chann: %d ext_32_value: %x ext_32bit: %d\n", __FUNCTION__, chan, dramc_ext32_off[chan], DRMAC_IS_FORCE32(dramc_ext32_off[chan]));
272 /* determine the number of dram for the corresponding dramc */
273 DRAMC_SET_VALUE(DRAMC_MON_BM(base), DRMAC_IS_FORCE32(dramc_ext32_off[chan]), DRAMC_BM_DMBW32B_SHIFT, 1);
274
275 /* disable all group */
276 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP1_ENABLE_SHIFT, 1);
277 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP2_ENABLE_SHIFT, 1);
278 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP3_ENABLE_SHIFT, 1);
279 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_FREEZE_SHIFT, 1);
280 temp_reg = dramc_reg_read(DRAMC_MON_BM(base));
281 DRAMC_LOG("%s MON_BM: %X\n", __FUNCTION__, temp_reg);
282
283#ifdef TEST_AGENT
284 u32 reg;
285 reg = base + 0;
286 dramc_reg_write(reg, 0xC050EF00);
287
288 reg = base + 0x100;
289 if (0 == chan)
290 dramc_reg_write(reg, 0x3C553000);
291 else if (1 == chan)
292 dramc_reg_write(reg, 0x5FFD0000);
293 else if (2 == chan)
294 dramc_reg_write(reg, 0x85D20000);
295
296 reg = base + 0x104;
297 dramc_reg_write(reg, 0x8000);
298
299 reg = base + 0x118;
300 dramc_reg_write(reg, 0x0600110D);
301 dramc_reg_write(reg, 0x1600110D);
302
303 reg = base + 0x160;
304 temp_reg = dramc_reg_read(reg);
305#endif
306}
307
308void DRAMC_MaxLtcyMode(int chan, int enable)
309{
310 unsigned long base = dramc_base[chan];
311
312 if (1 == enable) {
313 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_PACNTEN, 1);
314 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_REQCNTEN, 1);
315 } else {
316 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_PACNTEN, 1);
317 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_REQCNTEN, 1);
318 }
319}
320
321void DRAMC_Enable(int chan, int group, int agent)
322{
323 unsigned long base = dramc_base[chan];
324 volatile u32 temp_reg = 0;
325
326 /* disable all group */
327 switch (group) {
328 case 1:
329 DRAMC_SET_VALUE(DRAMC_MON_BM(base), agent, DRAMC_BM_GROUP1_AGENT_ID_SHIFT, DRAMC_BM_GROUP1_AGENT_ID_LEN);
330 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_GROUP1_ENABLE_SHIFT, 1);
331 break;
332
333 case 2:
334 DRAMC_SET_VALUE(DRAMC_MON_BM(base), agent, DRAMC_BM_GROUP2_AGENT_ID_SHIFT, DRAMC_BM_GROUP2_AGENT_ID_LEN);
335 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_GROUP2_ENABLE_SHIFT, 1);
336 break;
337
338 case 3:
339 DRAMC_SET_VALUE(DRAMC_MON_BM(base), agent, DRAMC_BM_GROUP3_AGENT_ID_SHIFT, DRAMC_BM_GROUP3_AGENT_ID_LEN);
340 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_GROUP3_ENABLE_SHIFT, 1);
341 break;
342 }
343 temp_reg = dramc_reg_read(DRAMC_MON_BM(base));
344 DRAMC_LOG("%s MON_BM: %X\n", __FUNCTION__, temp_reg);
345}
346
347
348void DRAMC_Disable(int chan)
349{
350 unsigned long base = dramc_base[chan];
351 volatile u32 temp_reg = 0;
352
353 /* disable all group */
354 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP1_ENABLE_SHIFT, 1);
355 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP2_ENABLE_SHIFT, 1);
356 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_GROUP3_ENABLE_SHIFT, 1);
357
358 /* fire the BM function */
359 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_FREEZE_SHIFT, 1);
360 temp_reg = dramc_reg_read(DRAMC_MON_BM(base));
361 DRAMC_LOG("%s MON_BM: %X\n", __FUNCTION__, temp_reg);
362}
363
364void DRAMC_Freeze(int chan)
365{
366 unsigned long base = dramc_base[chan];
367 volatile u32 temp_reg = dramc_reg_read(DRAMC_MON_BM(base));
368
369 /* Freeze the BM function */
370 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_FREEZE_SHIFT, 1);
371 temp_reg = dramc_reg_read(DRAMC_MON_BM(base));
372 DRAMC_LOG("%s MON_BM: %X\n", __FUNCTION__, temp_reg);
373}
374
375void DRAMC_ConfigTargetCount(int chan, u32 count)
376{
377 unsigned long base = dramc_base[chan];
378 volatile u32 temp_reg = 0;
379
380 dramc_reg_write(DRAMC_MON_BMCYC(base), 0xFFFFFFFF);
381 temp_reg = dramc_reg_read(DRAMC_MON_BMCYC(base));
382 DRAMC_LOG("%s MON_BMCYC: %X\n", __FUNCTION__, temp_reg);
383 return;
384}
385
386u32 DRAMC_GetMaxLtcy(int chan)
387{
388 unsigned long base = dramc_base[chan];
389
390 return dramc_reg_read(DRAMC_MON_ROBM4(base));
391}
392
393u32 DRAMC_GetDramcFreq(void)
394{
395 unsigned long tcm_dramc_flags = 0;
396 tcm_dramc_flags = dramc_reg_read(TCM_DRAM_FLAGS_ADDR);
397 return TCMGET_DDR_CLK(tcm_dramc_flags);
398}
399
400
401u32 DRAMC_GetCycleCount(int chan, int group)
402{
403 unsigned long base = dramc_base[chan];
404
405 switch (group) {
406 case 1:
407 return dramc_reg_read(DRAMC_MON_ROBM0(base));
408
409 case 2:
410 return dramc_reg_read(DRAMC_MON_ROBM1(base));
411
412 case 3:
413 return dramc_reg_read(DRAMC_MON_ROBM2(base));
414 }
415 return 0;
416}
417
418u32 DRAMC_GetTotalCycleCount(int chan)
419{
420 unsigned long base = dramc_base[chan];
421
422 return dramc_reg_read(DRAMC_MON_ROBM3(base));
423}
424
425int DRAMC_CheckCntIsOverFlow(u32 count)
426{
427 if (0xFFFFFFFF == count) {
428 return 1;
429 }
430 return 0;
431}
432
433void DRAMC_WriteMode(int chan, int enable)
434{
435 unsigned long base = dramc_base[chan];
436
437 if (1 == enable) {
438 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_WREN_SHIFT, 1);
439 } else {
440 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_WREN_SHIFT, 1);
441 }
442}
443
444void DRAMC_ReadMode(int chan, int enable)
445{
446 unsigned long base = dramc_base[chan];
447
448 if (1 == enable) {
449 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 1, DRAMC_BM_RDEN_SHIFT, 1);
450 } else {
451 DRAMC_SET_VALUE(DRAMC_MON_BM(base), 0, DRAMC_BM_RDEN_SHIFT, 1);
452 }
453}
454
455unsigned int DRAMC_GetEfuseValue(void)
456{
457 volatile u32 efuse_reg = 0;
458
459 efuse_reg = dramc_reg_read(DRAMC_EFUSE_BIT_ADDRESS);
460
461 return (efuse_reg & DRAMC_EFUSE_BIT_MASK);
462}
463
464void DRAMC_GetGroup1AgentCounter(int channel, unsigned int* counter)
465{
466 unsigned long base = dramc_base[channel];
467 unsigned long agent_base = 0;
468 int i = 0;
469
470 agent_base = DRAMC_MON_AGENT_BASE(base);
471 for (i=0; i<DRAMC_MON_AGENT_NUM; i++) {
472 counter[i] = dramc_reg_read(agent_base + i*sizeof(int));
473 }
474 smp_rmb();
475
476 return ;
477}
478
479void DRAMC_GetGroup1Latency(int channel, unsigned int* latency)
480{
481 unsigned long base = dramc_base[channel];
482 unsigned long lty_base = 0;
483 int i = 0;
484
485 lty_base = DRAMC_MON_LTY_AGENT_BASE(base);
486 for (i=0; i<DRAMC_MON_LTY_AGENT_NUM; i++) {
487 latency[i] = dramc_reg_read(lty_base + i*sizeof(int));
488 }
489 smp_rmb();
490
491 return ;
492}
493
494void DRAMC_GetGroup1MaxLatency(int channel, unsigned int* max_ltcy)
495{
496 unsigned long base = dramc_base[channel];
497 unsigned long max_lty_base = 0;
498 unsigned int value = 0;
499 int i = 0;
500
501 /* max latency counter is 16 bit*/
502 max_lty_base = DRAMC_MON_MAX_LTY_AGENT_BASE(base);
503 for (i=0; i<DRAMC_MON_LTY_AGENT_NUM/sizeof(short); i++) {
504 value = dramc_reg_read(max_lty_base + i*sizeof(int));
505 max_ltcy[2*i] = value & 0x0000FFFF;
506 max_ltcy[2*i+1] = (value & 0xFFFF0000) >> 16;
507 }
508 smp_rmb();
509
510 return ;
511}