tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / cpu / armv7 / sc8810 / efuse_drv.c
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 //#include <linux/kernel.h>
15 //#include <linux/init.h>
16 //#include <linux/io.h>
17 //#include <linux/module.h>
18
19 //#include <mach/hardware.h>
20 //#include <mach/regs_global.h>
21 #include <common.h>
22 #include <asm/io.h>
23
24 #include <asm/arch/regs_global.h>
25
26 #include "ctl_efuse.h"
27
28 #define CTL_EFUSE_BASE_PHYS                     ( 0x89000000 )
29 #define CTL_EFUSE_BASE                          ( CTL_EFUSE_BASE_PHYS )
30 #define SCI_ADDRESS(_b_, _o_)                   ( (u32)(_b_) + (_o_) )
31
32 #define SCI_D(reg)                                              ( *(volatile u32 *)(reg) )
33
34 //static void __iomem *ctl_efuse_base = 0;
35 void sci_efuse_poweron(void)
36 {
37         //ctl_efuse_base = ioremap(CTL_EFUSE_BASE_PHYS, PAGE_SIZE);
38         SCI_D(GR_GEN0) |= GEN0_EFUSE_EN;
39         SCI_D(REG_EFUSE_PGM_PARA) |= BIT_EFUSE_VDD_ON;
40         SCI_D(REG_EFUSE_PGM_PARA) |= BIT_CLK_EFS_EN;
41 }
42
43 void sci_efuse_poweroff(void)
44 {
45         SCI_D(REG_EFUSE_PGM_PARA) &= ~BIT_PGM_EN;
46         SCI_D(REG_EFUSE_PGM_PARA) &= ~BIT_CLK_EFS_EN;
47         SCI_D(REG_EFUSE_PGM_PARA) &= ~BIT_EFUSE_VDD_ON;
48         SCI_D(GR_GEN0) &= ~GEN0_EFUSE_EN;
49         //if (ctl_efuse_base) {
50         //      iounmap(ctl_efuse_base);
51         //      ctl_efuse_base = 0;
52         //}
53 }
54
55 int sci_efuse_read(unsigned blk)
56 {
57         int busy = 0;
58         BUG_ON(blk > (MASK_READ_INDEX >> SHIFT_READ_INDEX));
59
60         SCI_D(REG_EFUSE_BLOCK_INDEX) = BITS_READ_INDEX(blk);
61         SCI_D(REG_EFUSE_MODE_CTRL) |= BIT_RD_START;
62
63         do {
64                 //TODO: timeout
65                 busy = SCI_D(REG_EFUSE_STATUS) & BIT_READ_BUSY;
66         } while (busy);
67
68         return SCI_D(REG_EFUSE_DATA_RD);
69 }
70
71 int sci_efuse_program(unsigned blk, int data)
72 {
73         return 0;
74 }
75
76 int sci_efuse_is_locked(unsigned blk)
77 {
78         return 0;
79 }
80
81 /* low level */
82 int sci_efuse_raw_write(unsigned blk, int data, u32 magic)
83 {
84         SCI_D(REG_EFUSE_MAGIC_NUMBER) = BITS_MAGIC_NUMBER(magic);
85         return 0;
86 }
87
88 int sci_efuse_lock(unsigned blk)
89 {
90         return 0;
91 }
92
93 #define CAL_DATA_BLK    7
94 #define BASE_ADC_P0   785   //3.6V
95 #define BASE_ADC_P1   917   //4.2V
96 #define VOL_P0        3600
97 #define VOL_P1        4200
98 #define ADC_DATA_OFFSET 128
99 int sci_efuse_calibration_get(unsigned int * p_cal_data)
100 {
101         int data;
102     unsigned int cal_temp;
103     unsigned short adc_temp;
104
105         sci_efuse_poweron();
106         data = sci_efuse_read(CAL_DATA_BLK);
107         sci_efuse_poweroff();
108
109         data &= ~(1 << 31); 
110     
111 //    data = (173 |(171 << 8));
112     printf("sci_efuse_calibration data:%d\n",data);
113         if((!data)||(p_cal_data == NULL))
114         {
115                 return 0;
116         }
117     //adc 3.6V 
118     adc_temp = ((data>>8) & 0x00FF) + BASE_ADC_P0 - ADC_DATA_OFFSET;
119     p_cal_data[1] = (VOL_P0)|(adc_temp << 16);
120
121     //adc 4.2V
122         adc_temp = (data & 0x00FF) + BASE_ADC_P1 - ADC_DATA_OFFSET;
123     p_cal_data[0] = (VOL_P1)|(adc_temp << 16);
124
125         return 1;
126 }
127