xref: /utopia/UTPA2-700.0.x/modules/hwi2c/hal/manhattan/hwi2c/halHWI2C.h (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 ////////////////////////////////////////////////////////////////////////////////
93 
94 #ifndef _HAL_HWI2C_H_
95 #define _HAL_HWI2C_H_
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// @file halHWI2C.h
99 /// @author MStar Semiconductor Inc.
100 /// @brief MIIC control functions
101 ////////////////////////////////////////////////////////////////////////////////
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 // Header Files
105 ////////////////////////////////////////////////////////////////////////////////
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 // Define & data type
109 ////////////////////////////////////////////////////////////////////////////////
110 //v: value n: shift n bits
111 //v: value n: shift n bits
112 #define _LShift(v, n)       ((v) << (n))
113 #define _RShift(v, n)       ((v) >> (n))
114 
115 #define HIGH_BYTE(val)      (MS_U8)_RShift((val), 8)
116 #define LOW_BYTE(val)       ((MS_U8)((val) & 0xFF))
117 
118 #define __BIT(x)    ((MS_U8)_LShift(1, x))
119 #define __BIT0       __BIT(0)
120 #define __BIT1       __BIT(1)
121 #define __BIT2       __BIT(2)
122 #define __BIT3       __BIT(3)
123 #define __BIT4       __BIT(4)
124 #define __BIT5       __BIT(5)
125 #define __BIT6       __BIT(6)
126 #define __BIT7       __BIT(7)
127 
128 #define HWI2C_SET_RW_BIT(bRead, val) ((bRead) ? ((val) | __BIT0) : ((val) & ~__BIT0))
129 
130 #define HAL_HWI2C_PORTS         6
131 #define HAL_HWI2C_PORT0         0
132 #define HAL_HWI2C_PORT1         1
133 #define HAL_HWI2C_PORT2         2
134 #define HAL_HWI2C_PORT3         3
135 #define HAL_HWI2C_PORT4         4
136 #define HAL_HWI2C_PORT5         5
137 
138 
139 
140 typedef enum _HAL_HWI2C_STATE
141 {
142 	E_HAL_HWI2C_STATE_IDEL = 0,
143 	E_HAL_HWI2C_STATE_START,
144 	E_HAL_HWI2C_STATE_WRITE,
145 	E_HAL_HWI2C_STATE_READ,
146 	E_HAL_HWI2C_STATE_INT,
147 	E_HAL_HWI2C_STATE_WAIT,
148 	E_HAL_HWI2C_STATE_STOP
149 } HAL_HWI2C_STATE;
150 
151 
152 typedef enum _HAL_HWI2C_PORT
153 {
154     E_HAL_HWI2C_PORT0_0 = 0, //disable port 0
155     E_HAL_HWI2C_PORT0_1,
156     E_HAL_HWI2C_PORT0_2,
157     E_HAL_HWI2C_PORT0_3,
158     E_HAL_HWI2C_PORT0_4,
159     E_HAL_HWI2C_PORT0_5,
160     E_HAL_HWI2C_PORT0_6,
161     E_HAL_HWI2C_PORT0_7,
162 
163     E_HAL_HWI2C_PORT1_0,  //disable port 1
164     E_HAL_HWI2C_PORT1_1,
165     E_HAL_HWI2C_PORT1_2,
166     E_HAL_HWI2C_PORT1_3,
167     E_HAL_HWI2C_PORT1_4,
168     E_HAL_HWI2C_PORT1_5,
169     E_HAL_HWI2C_PORT1_6,
170     E_HAL_HWI2C_PORT1_7,
171 
172     E_HAL_HWI2C_PORT2_0,  //disable port 2
173     E_HAL_HWI2C_PORT2_1,
174     E_HAL_HWI2C_PORT2_2,
175     E_HAL_HWI2C_PORT2_3,
176     E_HAL_HWI2C_PORT2_4,
177     E_HAL_HWI2C_PORT2_5,
178     E_HAL_HWI2C_PORT2_6,
179     E_HAL_HWI2C_PORT2_7,
180 
181     E_HAL_HWI2C_PORT3_0, //disable port 3
182     E_HAL_HWI2C_PORT3_1,
183     E_HAL_HWI2C_PORT3_2,
184     E_HAL_HWI2C_PORT3_3,
185     E_HAL_HWI2C_PORT3_4,
186     E_HAL_HWI2C_PORT3_5,
187     E_HAL_HWI2C_PORT3_6,
188     E_HAL_HWI2C_PORT3_7,
189 
190 	E_HAL_HWI2C_PORT4_0, //disable port 4
191     E_HAL_HWI2C_PORT4_1,
192     E_HAL_HWI2C_PORT4_2,
193     E_HAL_HWI2C_PORT4_3,
194     E_HAL_HWI2C_PORT4_4,
195     E_HAL_HWI2C_PORT4_5,
196     E_HAL_HWI2C_PORT4_6,
197     E_HAL_HWI2C_PORT4_7,
198 
199 	E_HAL_HWI2C_PORT5_0, //disable port 5
200     E_HAL_HWI2C_PORT5_1,
201     E_HAL_HWI2C_PORT5_2,
202     E_HAL_HWI2C_PORT5_3,
203     E_HAL_HWI2C_PORT5_4,
204     E_HAL_HWI2C_PORT5_5,
205     E_HAL_HWI2C_PORT5_6,
206     E_HAL_HWI2C_PORT5_7,
207 
208     E_HAL_HWI2C_PORT_NOSUP
209 }HAL_HWI2C_PORT;
210 
211 typedef enum _HAL_HWI2C_CLKSEL
212 {
213     E_HAL_HWI2C_CLKSEL_HIGH = 0,
214     E_HAL_HWI2C_CLKSEL_NORMAL,
215     E_HAL_HWI2C_CLKSEL_SLOW,
216     E_HAL_HWI2C_CLKSEL_VSLOW,
217     E_HAL_HWI2C_CLKSEL_USLOW,
218     E_HAL_HWI2C_CLKSEL_UVSLOW,
219     E_HAL_HWI2C_CLKSEL_NOSUP
220 }HAL_HWI2C_CLKSEL;
221 
222 typedef enum _HAL_HWI2C_CLK
223 {
224     E_HAL_HWI2C_CLK_DIV4 = 1, //750K@12MHz
225     E_HAL_HWI2C_CLK_DIV8,     //375K@12MHz
226     E_HAL_HWI2C_CLK_DIV16,    //187.5K@12MHz
227     E_HAL_HWI2C_CLK_DIV32,    //93.75K@12MHz
228     E_HAL_HWI2C_CLK_DIV64,    //46.875K@12MHz
229     E_HAL_HWI2C_CLK_DIV128,   //23.4375K@12MHz
230     E_HAL_HWI2C_CLK_DIV256,   //11.71875K@12MHz
231     E_HAL_HWI2C_CLK_DIV512,   //5.859375K@12MHz
232     E_HAL_HWI2C_CLK_DIV1024,  //2.9296875K@12MHz
233     E_HAL_HWI2C_CLK_NOSUP
234 }HAL_HWI2C_CLK;
235 
236 typedef enum {
237     E_HAL_HWI2C_READ_MODE_DIRECT,                       ///< first transmit slave address + reg address and then start receive the data */
238     E_HAL_HWI2C_READ_MODE_DIRECTION_CHANGE,             ///< slave address + reg address in write mode, direction change to read mode, repeat start slave address in read mode, data from device
239     E_HAL_HWI2C_READ_MODE_DIRECTION_CHANGE_STOP_START,  ///< slave address + reg address in write mode + stop, direction change to read mode, repeat start slave address in read mode, data from device
240     E_HAL_HWI2C_READ_MODE_MAX
241 } HAL_HWI2C_ReadMode;
242 
243 typedef enum _HAL_HWI2C_DMA_ADDRMODE
244 {
245     E_HAL_HWI2C_DMA_ADDR_NORMAL = 0,
246     E_HAL_HWI2C_DMA_ADDR_10BIT,
247     E_HAL_HWI2C_DMA_ADDR_MAX,
248 }HAL_HWI2C_DMA_ADDRMODE;
249 
250 typedef enum _HAL_HWI2C_DMA_MIUPRI
251 {
252     E_HAL_HWI2C_DMA_PRI_LOW = 0,
253     E_HAL_HWI2C_DMA_PRI_HIGH,
254     E_HAL_HWI2C_DMA_PRI_MAX,
255 }HAL_HWI2C_DMA_MIUPRI;
256 
257 typedef enum _HAL_HWI2C_DMA_MIUCH
258 {
259     E_HAL_HWI2C_DMA_MIU_CH0 = 0,
260     E_HAL_HWI2C_DMA_MIU_CH1,
261     E_HAL_HWI2C_DMA_MIU_MAX,
262 }HAL_HWI2C_DMA_MIUCH;
263 
264 typedef struct _HAL_HWI2C_PinCfg
265 {
266     MS_U32  u32Reg;    /// register
267     MS_U8   u8BitPos;  /// bit position
268     MS_BOOL bEnable;   /// enable or disable
269 }HAL_HWI2C_PinCfg;
270 
271 typedef struct _HAL_HWI2C_PortCfg //Synchronize with drvHWI2C.h
272 {
273     MS_U32                  u32DmaPhyAddr;  /// DMA physical address
274     HAL_HWI2C_DMA_ADDRMODE  eDmaAddrMode;   /// DMA address mode
275     HAL_HWI2C_DMA_MIUPRI    eDmaMiuPri;     /// DMA miu priroity
276     HAL_HWI2C_DMA_MIUCH     eDmaMiuCh;      /// DMA miu channel
277     MS_BOOL                 bDmaEnable;     /// DMA enable
278 
279     HAL_HWI2C_PORT          ePort;          /// number
280     HAL_HWI2C_CLKSEL        eSpeed;         /// clock speed
281     HAL_HWI2C_ReadMode      eReadMode;      /// read mode
282     MS_BOOL                 bEnable;        /// enable
283 
284 }HAL_HWI2C_PortCfg;
285 
286 /// I2C Configuration for initialization
287 typedef struct _HAL_HWI2C_CfgInit //Synchronize with drvHWI2C.h
288 {
289     HAL_HWI2C_PortCfg   sCfgPort[8];    /// port cfg info
290     HAL_HWI2C_PinCfg    sI2CPin;        /// pin info
291     HAL_HWI2C_CLKSEL    eSpeed;         /// speed
292     HAL_HWI2C_PORT      ePort;          /// port
293     HAL_HWI2C_ReadMode  eReadMode;      /// read mode
294 
295 }HAL_HWI2C_CfgInit;
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 // Extern function
299 ////////////////////////////////////////////////////////////////////////////////
300 void HAL_HWI2C_ExtraDelay(MS_U32 u32Us);
301 void HAL_HWI2C_SetIOMapBase(MS_VIRT u32Base);
302 MS_U8 HAL_HWI2C_ReadByte(MS_VIRT u32RegAddr);
303 MS_U16 HAL_HWI2C_Read2Byte(MS_U32 u32RegAddr);
304 MS_U32 HAL_HWI2C_Read4Byte(MS_U32 u32RegAddr);
305 MS_BOOL HAL_HWI2C_WriteByte(MS_U32 u32RegAddr, MS_U8 u8Val);
306 MS_BOOL HAL_HWI2C_Write2Byte(MS_U32 u32RegAddr, MS_U16 u16Val);
307 MS_BOOL HAL_HWI2C_Write4Byte(MS_U32 u32RegAddr, MS_U32 u32Val);
308 MS_BOOL HAL_HWI2C_WriteRegBit(MS_U32 u32RegAddr, MS_U8 u8Mask, MS_BOOL bEnable);
309 MS_BOOL HAL_HWI2C_WriteByteMask(MS_U32 u32RegAddr, MS_U8 u8Val, MS_U8 u8Mask);
310 
311 MS_BOOL HAL_HWI2C_Init_Chip(void);
312 MS_BOOL HAL_HWI2C_IsMaster(void);
313 MS_BOOL HAL_HWI2C_Master_Enable(MS_U32 u32PortOffset);
314 MS_BOOL HAL_HWI2C_SetPortRegOffset(HAL_HWI2C_PORT ePort, MS_U32* pu32PortOffset);
315 MS_BOOL HAL_HWI2C_GetPortIdxByOffset(MS_U32 u32PortOffset, MS_U8* pu8Port);
316 MS_BOOL HAL_HWI2C_GetPortIdxByPort(HAL_HWI2C_PORT ePort, MS_U8* pu8Port);
317 MS_BOOL HAL_HWI2C_SelectPort(HAL_HWI2C_PORT ePort);
318 MS_BOOL HAL_HWI2C_SetClk(MS_U32 u32PortOffset, HAL_HWI2C_CLKSEL eClkSel);
319 
320 MS_BOOL HAL_HWI2C_Start(MS_U32 u32PortOffset);
321 MS_BOOL HAL_HWI2C_Stop(MS_U32 u32PortOffset);
322 MS_BOOL HAL_HWI2C_ReadRdy(MS_U32 u32PortOffset);
323 MS_BOOL HAL_HWI2C_SendData(MS_U32 u32PortOffset, MS_U8 u8Data);
324 MS_U8 HAL_HWI2C_RecvData(MS_U32 u32PortOffset);
325 MS_BOOL HAL_HWI2C_Get_SendAck(MS_U32 u32PortOffset);
326 MS_BOOL HAL_HWI2C_NoAck(MS_U32 u32PortOffset);
327 MS_BOOL HAL_HWI2C_Ack(MS_U32 u32PortOffset);
328 MS_U8 HAL_HWI2C_GetState(MS_U32 u32PortOffset);
329 MS_BOOL HAL_HWI2C_Is_Idle(MS_U32 u32PortOffset);
330 MS_BOOL HAL_HWI2C_Is_INT(MS_U32 u32PortOffset);
331 MS_BOOL HAL_HWI2C_Clear_INT(MS_U32 u32PortOffset);
332 MS_BOOL HAL_HWI2C_Reset(MS_U32 u32PortOffset, MS_BOOL bReset);
333 MS_BOOL HAL_HWI2C_Send_Byte(MS_U32 u32PortOffset, MS_U8 u8Data);
334 MS_BOOL HAL_HWI2C_Recv_Byte(MS_U32 u32PortOffset, MS_U8 *pData);
335 
336 MS_BOOL HAL_HWI2C_DMA_Init(MS_U32 u32PortOffset, HAL_HWI2C_PortCfg* pstPortCfg);
337 MS_BOOL HAL_HWI2C_DMA_Enable(MS_U32 u32PortOffset, MS_BOOL bEnable);
338 MS_BOOL HAL_HWI2C_DMA_ReadBytes(MS_U32 u32PortOffset, MS_U16 u16SlaveCfg, MS_U32 uAddrCnt, MS_U8 *pRegAddr, MS_U32 uSize, MS_U8 *pData);
339 MS_BOOL HAL_HWI2C_DMA_WriteBytes(MS_U32 u32PortOffset, MS_U16 u16SlaveCfg, MS_U32 uAddrCnt, MS_U8 *pRegAddr, MS_U32 uSize, MS_U8 *pData);
340 #ifdef CONFIG_HWIIC_INTERRUPT_MODE_ENABLE
341 void HAL_HWI2C_Resume_Flag(MS_U8 u8Resume);
342 #endif
343 void HAL_HWI2C_Init_ExtraProc(void);
344 
345 #if 0
346 MS_BOOL HAL_HWI2C_EnINT(MS_BOOL bEnable);
347 MS_BOOL HAL_HWI2C_EnDMA(MS_BOOL bEnable);
348 MS_BOOL HAL_HWI2C_EnClkStretch(MS_BOOL bEnable);
349 MS_BOOL HAL_HWI2C_EnTimeoutINT(MS_BOOL bEnable);
350 MS_BOOL HAL_HWI2C_EnFilter(MS_BOOL bEnable);
351 MS_BOOL HAL_HWI2C_EnPushSda(MS_BOOL bEnable);
352 
353 MS_BOOL HAL_HWI2C_DMA_SetINT(MS_BOOL bEnable);
354 MS_BOOL HAL_HWI2C_DMA_Reset(MS_BOOL bReset);
355 MS_BOOL HAL_HWI2C_DMA_MiuReset(MS_BOOL bReset);
356 MS_BOOL HAL_HWI2C_DMA_SetMiuPri(HAL_HWI2C_DMA_MIUPRI eMiuPri);
357 MS_BOOL HAL_HWI2C_DMA_SetMiuAddr(MS_U32 u32MiuAddr);
358 MS_BOOL HAL_HWI2C_DMA_Trigger(void);
359 MS_BOOL HAL_HWI2C_DMA_ReTrigger(void);
360 MS_BOOL HAL_HWI2C_DMA_SetReadMode(HAL_HWI2C_ReadMode eReadMode);
361 MS_BOOL HAL_HWI2C_DMA_SetRdWrt(MS_BOOL bRdWrt);
362 MS_BOOL HAL_HWI2C_DMA_SetMiuChannel(HAL_HWI2C_DMA_MIUCH eMiuCh);
363 MS_BOOL HAL_HWI2C_DMA_TxfrDone(void);
364 MS_BOOL HAL_HWI2C_DMA_IsTxfrDone(void);
365 MS_BOOL HAL_HWI2C_DMA_SetTxfrCmd(MS_U8 u8CmdLen, MS_U8* pu8CmdBuf);
366 MS_BOOL HAL_HWI2C_DMA_SetCmdLen(MS_U8 u8CmdLen);
367 MS_BOOL HAL_HWI2C_DMA_SetDataLen(MS_U32 u32DataLen);
368 MS_U32 HAL_HWI2C_DMA_GetTxfrCnt(void);
369 MS_BOOL HAL_HWI2C_DMA_SetSlaveAddr(MS_U16 u16SlaveAddr);
370 HAL_HWI2C_DMA_ADDRMODE HAL_HWI2C_DMA_GetAddrMode(void);
371 MS_BOOL HAL_HWI2C_DMA_SetAddrMode(HAL_HWI2C_DMA_ADDRMODE eAddrMode);
372 #endif
373 
374 #endif  //_MHAL_HWI2C_H_
375 
376