1 /*
2  * Copyright 2017-2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_common.h"
9 #include "fsl_debug_console.h"
10 #include "board.h"
11 #include "fsl_gpio.h"
12 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
13 #include "fsl_lpi2c.h"
14 #endif /* SDK_I2C_BASED_COMPONENT_USED */
15 /*******************************************************************************
16  * Variables
17  ******************************************************************************/
18 static sc_ipc_t ipcHandle; /* ipc handle */
19 /*******************************************************************************
20  * Code
21  ******************************************************************************/
BOARD_InitRpc(void)22 sc_ipc_t BOARD_InitRpc(void)
23 {
24     /* Initialize the IPC channel to communicate with SCFW */
25     SystemInitScfwIpc();
26 
27     ipcHandle = SystemGetScfwIpcHandle();
28     if (ipcHandle)
29     {
30         CLOCK_Init(ipcHandle);
31     }
32 
33     /*
34      * Current core reports it is done to SCFW when early boot mode is enabled.
35      * This mode is used to minimize the time from POR to M4 execution for some specific fast-boot use-cases.
36      * Please refer to Boot Flow chapter of System Controller Firmware Porting Guide document for more information.
37      */
38     if (sc_misc_boot_done(ipcHandle, BOARD_M4_CPU_RSRC) != SC_ERR_NONE)
39     {
40         assert(0);
41     }
42     return ipcHandle;
43 }
44 
45 /* Initialize debug console. */
BOARD_InitDebugConsole(void)46 void BOARD_InitDebugConsole(void)
47 {
48     uint32_t freq = SC_24MHZ;
49 
50     /* Power on Local LPUART for M4. */
51     sc_pm_set_resource_power_mode(ipcHandle, BOARD_DEBUG_UART_SC_RSRC, SC_PM_PW_MODE_ON);
52     /* Enable clock of Local LPUART for M4. */
53     CLOCK_EnableClockExt(BOARD_DEBUG_UART_CLKSRC, 0);
54     /* Set clock Frequncy of Local LPUART for M4. */
55     freq = CLOCK_SetIpFreq(BOARD_DEBUG_UART_CLKSRC, freq);
56 
57     /* Initialize Debug Console using local LPUART for M4. */
58     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, freq);
59 }
60 
61 /* Initialize MPU, configure non-cacheable memory */
BOARD_InitMemory(void)62 void BOARD_InitMemory(void)
63 {
64 #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
65     extern uint32_t Load$$LR$$LR_cache_region$$Base[];
66     extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
67     uint32_t cacheStart = (uint32_t)Load$$LR$$LR_cache_region$$Base;
68     uint32_t size       = (cacheStart < 0x20000000U) ? (0) : ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit - cacheStart);
69 #else
70     extern uint32_t __CACHE_REGION_START[];
71     extern uint32_t __CACHE_REGION_SIZE[];
72     uint32_t cacheStart = (uint32_t)__CACHE_REGION_START;
73     uint32_t size       = (uint32_t)__CACHE_REGION_SIZE;
74 #endif
75     uint32_t i = 0;
76     /* Make sure outstanding transfers are done. */
77     __DMB();
78     /* Disable the MPU. */
79     MPU->CTRL = 0;
80 
81     /*
82        The ARMv7-M default address map define the address space 0x20000000 to 0x3FFFFFFF as SRAM with Normal type, but
83        there the address
84        space 0x28000000 ~ 0x3FFFFFFF has been physically mapped to smart subsystems, so there need change the default
85        memory attributes.
86        Since the base address of MPU region should be multiples of region size, to make it simple, the MPU region 0 set
87        the all 512M of SRAM space
88        with device attributes, then disable subregion 0 and 1 (address space 0x20000000 ~ 0x27FFFFFF) to use the
89        background memory attributes.
90     */
91 
92     /* Select Region 0 and set its base address to the M4 code bus start address. */
93     MPU->RBAR = (0x20000000U & MPU_RBAR_ADDR_Msk) | MPU_RBAR_VALID_Msk | (0 << MPU_RBAR_REGION_Pos);
94 
95     /* Region 0 setting:
96      * 1) Disable Instruction Access;
97      * 2) AP = 011b, full access;
98      * 3) Non-shared device;
99      * 4) Region Not Shared;
100      * 5) Sub-Region 0,1 Disabled;
101      * 6) MPU Protection Region size = 512M byte;
102      * 7) Enable Region 0.
103      */
104     MPU->RASR = (0x1 << MPU_RASR_XN_Pos) | (0x3 << MPU_RASR_AP_Pos) | (0x2 << MPU_RASR_TEX_Pos) |
105                 (0x3 << MPU_RASR_SRD_Pos) | (28 << MPU_RASR_SIZE_Pos) | MPU_RASR_ENABLE_Msk;
106 
107     /*
108        Non-cacheable area is provided in DDR memory, the DDR region 2MB - 128MB totally 126MB is revserved for CM4
109        cores.
110        You can put global or static uninitialized variables in NonCacheable section(initialized variables in
111        NonCacheable.init section)
112        to make them uncacheable. Since the base address of MPU region should be multiples of region size, to make it
113        simple,
114        the MPU region 1 set the address space 0x80000000 ~ 0xFFFFFFFF to be non-cacheable(disable sub-region 6,7 to use
115        the background memory
116        attributes for address space 0xE0000000 ~ 0xFFFFFFFF). Then MPU region 2 set the text and data section to be
117        cacheable if the program running
118        on DDR. The cacheable area base address should be multiples of its size in linker file, they can be modified per
119        your needs.
120     */
121 
122     /* Select Region 1 and set its base address to the DDR start address. */
123     MPU->RBAR = (0x80000000U & MPU_RBAR_ADDR_Msk) | MPU_RBAR_VALID_Msk | (1 << MPU_RBAR_REGION_Pos);
124 
125     /* Region 1 setting:
126      * 1) Enable Instruction Access;
127      * 2) AP = 011b, full access;
128      * 3) Shared Device;
129      * 4) Sub-Region 6,7 Disabled;
130      * 5) MPU Protection Region size = 2048M byte;
131      * 6) Enable Region 1.
132      */
133     MPU->RASR = (0x3 << MPU_RASR_AP_Pos) | (0x1 << MPU_RASR_B_Pos) | (0xC0 << MPU_RASR_SRD_Pos) |
134                 (30 << MPU_RASR_SIZE_Pos) | MPU_RASR_ENABLE_Msk;
135 
136     while ((size >> i) > 0x1U)
137     {
138         i++;
139     }
140 
141     /* If run on DDR, configure text and data section to be cacheable */
142     if (i != 0)
143     {
144         /* The MPU region size should be 2^N, 5<=N<=32, region base should be multiples of size. */
145         assert((size & (size - 1)) == 0);
146         assert(!(cacheStart % size));
147         assert(size == (uint32_t)(1 << i));
148         assert(i >= 5);
149 
150         /* Select Region 2 and set its base address to the cache able region start address. */
151         MPU->RBAR = (cacheStart & MPU_RBAR_ADDR_Msk) | MPU_RBAR_VALID_Msk | (2 << MPU_RBAR_REGION_Pos);
152 
153         /* Region 2 setting:
154          * 1) Enable Instruction Access;
155          * 2) AP = 011b, full access;
156          * 3) Outer and inner Cacheable, write and read allocate;
157          * 4) Region Not Shared;
158          * 5) All Sub-Region Enabled;
159          * 6) MPU Protection Region size get from linker file;
160          * 7) Enable Region 2.
161          */
162         MPU->RASR = (0x3 << MPU_RASR_AP_Pos) | (0x1 << MPU_RASR_TEX_Pos) | (0x1 << MPU_RASR_C_Pos) |
163                     (0x1 << MPU_RASR_B_Pos) | ((i - 1) << MPU_RASR_SIZE_Pos) | MPU_RASR_ENABLE_Msk;
164     }
165 
166     /* Enable Privileged default memory map and the MPU. */
167     MPU->CTRL = MPU_CTRL_ENABLE_Msk | MPU_CTRL_PRIVDEFENA_Msk;
168     /* Memory barriers to ensure subsequence data & instruction
169      * transfers using updated MPU settings.
170      */
171     __DSB();
172     __ISB();
173 }
174 
175 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_LPI2C_Init(LPI2C_Type * base,uint32_t clkSrc_Hz)176 void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
177 {
178     lpi2c_master_config_t lpi2cConfig = {0};
179 
180     /*
181      * lpi2cConfig.debugEnable = false;
182      * lpi2cConfig.ignoreAck = false;
183      * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
184      * lpi2cConfig.baudRate_Hz = 100000U;
185      * lpi2cConfig.busIdleTimeout_ns = 0;
186      * lpi2cConfig.pinLowTimeout_ns = 0;
187      * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
188      * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
189      */
190     LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
191     LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
192 }
193 
BOARD_LPI2C_Deinit(LPI2C_Type * base)194 void BOARD_LPI2C_Deinit(LPI2C_Type *base)
195 {
196     LPI2C_MasterDeinit(base);
197 }
198 
BOARD_LPI2C_Send(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * txBuff,uint8_t txBuffSize)199 status_t BOARD_LPI2C_Send(LPI2C_Type *base,
200                           uint8_t deviceAddress,
201                           uint32_t subAddress,
202                           uint8_t subAddressSize,
203                           uint8_t *txBuff,
204                           uint8_t txBuffSize)
205 {
206     lpi2c_master_transfer_t xfer;
207 
208     xfer.flags          = kLPI2C_TransferDefaultFlag;
209     xfer.slaveAddress   = deviceAddress;
210     xfer.direction      = kLPI2C_Write;
211     xfer.subaddress     = subAddress;
212     xfer.subaddressSize = subAddressSize;
213     xfer.data           = txBuff;
214     xfer.dataSize       = txBuffSize;
215 
216     return LPI2C_MasterTransferBlocking(base, &xfer);
217 }
218 
BOARD_LPI2C_SendWithoutSubAddr(LPI2C_Type * base,uint32_t baudRate_Hz,uint8_t deviceAddress,uint8_t * txBuff,uint8_t txBuffSize,uint8_t needStop)219 status_t BOARD_LPI2C_SendWithoutSubAddr(LPI2C_Type *base,
220                                         uint32_t baudRate_Hz,
221                                         uint8_t deviceAddress,
222                                         uint8_t *txBuff,
223                                         uint8_t txBuffSize,
224                                         uint8_t needStop)
225 {
226     status_t reVal;
227     size_t txCount = 0xFFU;
228     size_t txSize  = 0;
229     /* 9 I2C SCLK cycles in us, get the next larger integer if can not be divided with no remainder to retain enough
230      * time*/
231     uint32_t delay = (9000000U + baudRate_Hz - 1U) / baudRate_Hz;
232 
233     /* Send master blocking data to slave. */
234     reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
235     if (kStatus_Success == reVal)
236     {
237         /* Wait I2C master tx FIFO empty. */
238         while (txCount)
239         {
240             LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
241         }
242         /* Wait for 9 cycle to ensure the ack/nack cycle ends. */
243         SDK_DelayAtLeastUs(delay, SystemCoreClock);
244         /* Check communicate with slave successful or not */
245         if (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
246         {
247             return kStatus_LPI2C_Nak;
248         }
249 
250         /* Check each response from slave. */
251         for (txSize = 0; txSize < txBuffSize; txSize++)
252         {
253             reVal = LPI2C_MasterSend(base, &txBuff[txSize], 1);
254             /* Wait tx FIFO empty. */
255             LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
256             while (txCount)
257             {
258                 LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
259             }
260             /* Wait for 9 cycle to ensure the ack/nack cycle ends. */
261             SDK_DelayAtLeastUs(delay, SystemCoreClock);
262             /* Check communicate with slave successful or not */
263             if (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
264             {
265                 return kStatus_LPI2C_Nak;
266             }
267         }
268 
269         if (reVal != kStatus_Success)
270         {
271             return reVal;
272         }
273 
274         if (needStop)
275         {
276             reVal = LPI2C_MasterStop(base);
277             if (reVal != kStatus_Success)
278             {
279                 return reVal;
280             }
281         }
282     }
283 
284     return reVal;
285 }
286 
BOARD_LPI2C_Receive(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)287 status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
288                              uint8_t deviceAddress,
289                              uint32_t subAddress,
290                              uint8_t subAddressSize,
291                              uint8_t *rxBuff,
292                              uint8_t rxBuffSize)
293 {
294     lpi2c_master_transfer_t xfer;
295 
296     xfer.flags          = kLPI2C_TransferDefaultFlag;
297     xfer.slaveAddress   = deviceAddress;
298     xfer.direction      = kLPI2C_Read;
299     xfer.subaddress     = subAddress;
300     xfer.subaddressSize = subAddressSize;
301     xfer.data           = rxBuff;
302     xfer.dataSize       = rxBuffSize;
303 
304     return LPI2C_MasterTransferBlocking(base, &xfer);
305 }
306 
BOARD_LPI2C_ReceiveWithoutSubAddr(LPI2C_Type * base,uint32_t baudRate_Hz,uint8_t deviceAddress,uint8_t * rxBuff,uint8_t rxBuffSize,uint8_t flags)307 status_t BOARD_LPI2C_ReceiveWithoutSubAddr(
308     LPI2C_Type *base, uint32_t baudRate_Hz, uint8_t deviceAddress, uint8_t *rxBuff, uint8_t rxBuffSize, uint8_t flags)
309 {
310     status_t reVal;
311     size_t txCount = 0xFFU;
312     /* 9 I2C SCLK cycles in us, get the next larger integer if can not be divided with no remainder to retain enough
313      * time*/
314     uint32_t delay = (9000000U + baudRate_Hz - 1U) / baudRate_Hz;
315 
316     reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Read);
317     if (kStatus_Success == reVal)
318     {
319         /* Wait I2C master tx FIFO empty. */
320         while (txCount)
321         {
322             LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
323         }
324         /* Wait for 9 cycle to ensure the ack/nack cycle ends. */
325         SDK_DelayAtLeastUs(delay, SystemCoreClock);
326         /* Check communicate with slave successful or not. */
327         if (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
328         {
329             return kStatus_LPI2C_Nak;
330         }
331 
332         reVal = LPI2C_MasterReceive(base, rxBuff, rxBuffSize);
333         if (reVal != kStatus_Success)
334         {
335             return reVal;
336         }
337 
338         reVal = LPI2C_MasterStop(base);
339         if (reVal != kStatus_Success)
340         {
341             return reVal;
342         }
343     }
344     return reVal;
345 }
346 
BOARD_LPI2C_SendSCCB(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * txBuff,uint8_t txBuffSize)347 status_t BOARD_LPI2C_SendSCCB(LPI2C_Type *base,
348                               uint8_t deviceAddress,
349                               uint32_t subAddress,
350                               uint8_t subAddressSize,
351                               uint8_t *txBuff,
352                               uint8_t txBuffSize)
353 {
354     lpi2c_master_transfer_t xfer;
355 
356     xfer.flags          = kLPI2C_TransferDefaultFlag;
357     xfer.slaveAddress   = deviceAddress;
358     xfer.direction      = kLPI2C_Write;
359     xfer.subaddress     = subAddress;
360     xfer.subaddressSize = subAddressSize;
361     xfer.data           = txBuff;
362     xfer.dataSize       = txBuffSize;
363 
364     return LPI2C_MasterTransferBlocking(base, &xfer);
365 }
366 
BOARD_LPI2C_ReceiveSCCB(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)367 status_t BOARD_LPI2C_ReceiveSCCB(LPI2C_Type *base,
368                                  uint8_t deviceAddress,
369                                  uint32_t subAddress,
370                                  uint8_t subAddressSize,
371                                  uint8_t *rxBuff,
372                                  uint8_t rxBuffSize)
373 {
374     status_t status;
375     lpi2c_master_transfer_t xfer;
376 
377     xfer.flags          = kLPI2C_TransferDefaultFlag;
378     xfer.slaveAddress   = deviceAddress;
379     xfer.direction      = kLPI2C_Write;
380     xfer.subaddress     = subAddress;
381     xfer.subaddressSize = subAddressSize;
382     xfer.data           = NULL;
383     xfer.dataSize       = 0;
384 
385     status = LPI2C_MasterTransferBlocking(base, &xfer);
386 
387     if (kStatus_Success == status)
388     {
389         xfer.subaddressSize = 0;
390         xfer.direction      = kLPI2C_Read;
391         xfer.data           = rxBuff;
392         xfer.dataSize       = rxBuffSize;
393 
394         status = LPI2C_MasterTransferBlocking(base, &xfer);
395     }
396 
397     return status;
398 }
399 
BOARD_Display0_I2C_Init(void)400 void BOARD_Display0_I2C_Init(void)
401 {
402     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
403     sc_err_t err             = SC_ERR_NONE;
404     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
405 
406     err = sc_pm_set_resource_power_mode(ipc, SC_R_MIPI_0_I2C_0, SC_PM_PW_MODE_ON);
407     if (SC_ERR_NONE != err)
408     {
409         assert(false);
410     }
411     err = sc_pm_set_clock_rate(ipc, SC_R_MIPI_0_I2C_0, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
412     if (SC_ERR_NONE != err)
413     {
414         assert(false);
415     }
416     BOARD_LPI2C_Init(BOARD_DISPLAY0_I2C_BASEADDR, lpi2cClkFreq_Hz);
417 }
418 
BOARD_Display0_I2C_Deinit(void)419 void BOARD_Display0_I2C_Deinit(void)
420 {
421     sc_err_t err = SC_ERR_NONE;
422     sc_ipc_t ipc = SystemGetScfwIpcHandle();
423 
424     BOARD_LPI2C_Deinit(BOARD_DISPLAY0_I2C_BASEADDR);
425 
426     err = sc_pm_set_resource_power_mode(ipc, SC_R_MIPI_0_I2C_0, SC_PM_PW_MODE_OFF);
427     if (SC_ERR_NONE != err)
428     {
429         assert(false);
430     }
431 }
432 
BOARD_Display1_I2C_Init(void)433 void BOARD_Display1_I2C_Init(void)
434 {
435     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
436     sc_err_t err             = SC_ERR_NONE;
437     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
438 
439     err = sc_pm_set_resource_power_mode(ipc, SC_R_MIPI_1_I2C_0, SC_PM_PW_MODE_ON);
440     if (SC_ERR_NONE != err)
441     {
442         assert(false);
443     }
444     err = sc_pm_set_clock_rate(ipc, SC_R_MIPI_1_I2C_0, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
445     if (SC_ERR_NONE != err)
446     {
447         assert(false);
448     }
449     BOARD_LPI2C_Init(BOARD_DISPLAY1_I2C_BASEADDR, lpi2cClkFreq_Hz);
450 }
451 
BOARD_Display1_I2C_Deinit(void)452 void BOARD_Display1_I2C_Deinit(void)
453 {
454     sc_err_t err = SC_ERR_NONE;
455     sc_ipc_t ipc = SystemGetScfwIpcHandle();
456 
457     BOARD_LPI2C_Deinit(BOARD_DISPLAY1_I2C_BASEADDR);
458 
459     err = sc_pm_set_resource_power_mode(ipc, SC_R_MIPI_1_I2C_0, SC_PM_PW_MODE_OFF);
460     if (SC_ERR_NONE != err)
461     {
462         assert(false);
463     }
464 }
465 
BOARD_Display2_I2C_Init(void)466 void BOARD_Display2_I2C_Init(void)
467 {
468     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
469     sc_err_t err             = SC_ERR_NONE;
470     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
471 
472     err = sc_pm_set_resource_power_mode(ipc, SC_R_LVDS_0_I2C_0, SC_PM_PW_MODE_ON);
473     if (SC_ERR_NONE != err)
474     {
475         assert(false);
476     }
477     err = sc_pm_set_clock_rate(ipc, SC_R_LVDS_0_I2C_0, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
478     if (SC_ERR_NONE != err)
479     {
480         assert(false);
481     }
482 
483     BOARD_LPI2C_Init(BOARD_DISPLAY2_I2C_BASEADDR, lpi2cClkFreq_Hz);
484 }
485 
BOARD_Display2_I2C_Deinit(void)486 void BOARD_Display2_I2C_Deinit(void)
487 {
488     sc_err_t err = SC_ERR_NONE;
489     sc_ipc_t ipc = SystemGetScfwIpcHandle();
490 
491     BOARD_LPI2C_Deinit(BOARD_DISPLAY2_I2C_BASEADDR);
492 
493     err = sc_pm_set_resource_power_mode(ipc, SC_R_LVDS_0_I2C_0, SC_PM_PW_MODE_OFF);
494     if (SC_ERR_NONE != err)
495     {
496         assert(false);
497     }
498 }
499 
BOARD_Display3_I2C_Init(void)500 void BOARD_Display3_I2C_Init(void)
501 {
502     /* Display 2 & 3 uses the same I2C. */
503     BOARD_Display2_I2C_Init();
504 }
505 
BOARD_Display3_I2C_Deinit(void)506 void BOARD_Display3_I2C_Deinit(void)
507 {
508     /* Display 2 & 3 uses the same I2C. */
509     BOARD_Display2_I2C_Deinit();
510 }
511 
BOARD_Display4_I2C_Init(void)512 void BOARD_Display4_I2C_Init(void)
513 {
514     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
515     sc_err_t err             = SC_ERR_NONE;
516     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
517 
518     err = sc_pm_set_resource_power_mode(ipc, SC_R_LVDS_1_I2C_0, SC_PM_PW_MODE_ON);
519     if (SC_ERR_NONE != err)
520     {
521         assert(false);
522     }
523     err = sc_pm_set_clock_rate(ipc, SC_R_LVDS_1_I2C_0, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
524     if (SC_ERR_NONE != err)
525     {
526         assert(false);
527     }
528     BOARD_LPI2C_Init(BOARD_DISPLAY4_I2C_BASEADDR, lpi2cClkFreq_Hz);
529 }
530 
BOARD_Display4_I2C_Deinit(void)531 void BOARD_Display4_I2C_Deinit(void)
532 {
533     sc_err_t err = SC_ERR_NONE;
534     sc_ipc_t ipc = SystemGetScfwIpcHandle();
535 
536     BOARD_LPI2C_Deinit(BOARD_DISPLAY4_I2C_BASEADDR);
537 
538     err = sc_pm_set_resource_power_mode(ipc, SC_R_LVDS_1_I2C_0, SC_PM_PW_MODE_OFF);
539     if (SC_ERR_NONE != err)
540     {
541         assert(false);
542     }
543 }
544 
BOARD_Display5_I2C_Init(void)545 void BOARD_Display5_I2C_Init(void)
546 {
547     /* Display 4 & 5 uses the same I2C. */
548     BOARD_Display4_I2C_Init();
549 }
550 
BOARD_Display5_I2C_Deinit(void)551 void BOARD_Display5_I2C_Deinit(void)
552 {
553     /* Display 4 & 5 uses the same I2C. */
554     BOARD_Display4_I2C_Deinit();
555 }
556 
BOARD_Display0_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)557 status_t BOARD_Display0_I2C_Send(
558     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
559 {
560     return BOARD_LPI2C_Send(BOARD_DISPLAY0_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
561                             txBuffSize);
562 }
563 
BOARD_Display0_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)564 status_t BOARD_Display0_I2C_Receive(
565     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
566 {
567     return BOARD_LPI2C_Receive(BOARD_DISPLAY0_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
568                                rxBuffSize);
569 }
570 
BOARD_Display1_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)571 status_t BOARD_Display1_I2C_Send(
572     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
573 {
574     return BOARD_LPI2C_Send(BOARD_DISPLAY1_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
575                             txBuffSize);
576 }
577 
BOARD_Display1_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)578 status_t BOARD_Display1_I2C_Receive(
579     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
580 {
581     return BOARD_LPI2C_Receive(BOARD_DISPLAY1_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
582                                rxBuffSize);
583 }
584 
BOARD_Display2_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)585 status_t BOARD_Display2_I2C_Send(
586     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
587 {
588     return BOARD_LPI2C_Send(BOARD_DISPLAY2_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
589                             txBuffSize);
590 }
591 
BOARD_Display2_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)592 status_t BOARD_Display2_I2C_Receive(
593     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
594 {
595     return BOARD_LPI2C_Receive(BOARD_DISPLAY2_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
596                                rxBuffSize);
597 }
598 
BOARD_Display3_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)599 status_t BOARD_Display3_I2C_Send(
600     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
601 {
602     return BOARD_LPI2C_Send(BOARD_DISPLAY3_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
603                             txBuffSize);
604 }
605 
BOARD_Display3_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)606 status_t BOARD_Display3_I2C_Receive(
607     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
608 {
609     return BOARD_LPI2C_Receive(BOARD_DISPLAY3_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
610                                rxBuffSize);
611 }
612 
BOARD_Display4_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)613 status_t BOARD_Display4_I2C_Send(
614     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
615 {
616     return BOARD_LPI2C_Send(BOARD_DISPLAY4_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
617                             txBuffSize);
618 }
619 
BOARD_Display4_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)620 status_t BOARD_Display4_I2C_Receive(
621     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
622 {
623     return BOARD_LPI2C_Receive(BOARD_DISPLAY4_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
624                                rxBuffSize);
625 }
626 
BOARD_Display5_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)627 status_t BOARD_Display5_I2C_Send(
628     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
629 {
630     return BOARD_LPI2C_Send(BOARD_DISPLAY5_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
631                             txBuffSize);
632 }
633 
BOARD_Display5_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)634 status_t BOARD_Display5_I2C_Receive(
635     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
636 {
637     return BOARD_LPI2C_Receive(BOARD_DISPLAY5_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
638                                rxBuffSize);
639 }
640 
BOARD_Camera0_I2C_Init(void)641 void BOARD_Camera0_I2C_Init(void)
642 {
643     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
644     sc_err_t err             = SC_ERR_NONE;
645     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
646 
647     err = sc_pm_set_resource_power_mode(ipc, BOARD_CAMERA0_I2C_RSRC, SC_PM_PW_MODE_ON);
648     if (SC_ERR_NONE != err)
649     {
650         assert(false);
651     }
652 
653     err = sc_pm_set_clock_rate(ipc, BOARD_CAMERA0_I2C_RSRC, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
654     if (SC_ERR_NONE != err)
655     {
656         assert(false);
657     }
658 
659     lpi2c_master_config_t lpi2cConfig = {0};
660     /*
661      * lpi2cConfig.debugEnable = false;
662      * lpi2cConfig.ignoreAck = false;
663      * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
664      * lpi2cConfig.baudRate_Hz = 100000U;
665      * lpi2cConfig.busIdleTimeout_ns = 0;
666      * lpi2cConfig.pinLowTimeout_ns = 0;
667      * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
668      * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
669      */
670     LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
671     lpi2cConfig.baudRate_Hz = 400000U;
672     LPI2C_MasterInit(BOARD_CAMERA0_I2C_BASEADDR, &lpi2cConfig, lpi2cClkFreq_Hz);
673 }
674 
BOARD_Camera0_I2C_Deinit(void)675 void BOARD_Camera0_I2C_Deinit(void)
676 {
677     sc_err_t err = SC_ERR_NONE;
678     sc_ipc_t ipc = SystemGetScfwIpcHandle();
679 
680     BOARD_LPI2C_Deinit(BOARD_CAMERA0_I2C_BASEADDR);
681 
682     err = sc_pm_set_resource_power_mode(ipc, BOARD_CAMERA0_I2C_RSRC, SC_PM_PW_MODE_OFF);
683     if (SC_ERR_NONE != err)
684     {
685         assert(false);
686     }
687 }
688 
BOARD_Camera1_I2C_Init(void)689 void BOARD_Camera1_I2C_Init(void)
690 {
691     uint32_t lpi2cClkFreq_Hz = SC_24MHZ;
692     sc_err_t err             = SC_ERR_NONE;
693     sc_ipc_t ipc             = SystemGetScfwIpcHandle();
694 
695     err = sc_pm_set_resource_power_mode(ipc, BOARD_CAMERA1_I2C_RSRC, SC_PM_PW_MODE_ON);
696     if (SC_ERR_NONE != err)
697     {
698         assert(false);
699     }
700 
701     err = sc_pm_set_clock_rate(ipc, BOARD_CAMERA1_I2C_RSRC, SC_PM_CLK_PER, &lpi2cClkFreq_Hz);
702     if (SC_ERR_NONE != err)
703     {
704         assert(false);
705     }
706 
707     lpi2c_master_config_t lpi2cConfig = {0};
708     /*
709      * lpi2cConfig.debugEnable = false;
710      * lpi2cConfig.ignoreAck = false;
711      * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
712      * lpi2cConfig.baudRate_Hz = 100000U;
713      * lpi2cConfig.busIdleTimeout_ns = 0;
714      * lpi2cConfig.pinLowTimeout_ns = 0;
715      * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
716      * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
717      */
718     LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
719     lpi2cConfig.baudRate_Hz = 400000U;
720     LPI2C_MasterInit(BOARD_CAMERA1_I2C_BASEADDR, &lpi2cConfig, lpi2cClkFreq_Hz);
721 }
722 
BOARD_Camera1_I2C_Deinit(void)723 void BOARD_Camera1_I2C_Deinit(void)
724 {
725     sc_err_t err = SC_ERR_NONE;
726     sc_ipc_t ipc = SystemGetScfwIpcHandle();
727 
728     BOARD_LPI2C_Deinit(BOARD_CAMERA1_I2C_BASEADDR);
729 
730     err = sc_pm_set_resource_power_mode(ipc, BOARD_CAMERA1_I2C_RSRC, SC_PM_PW_MODE_OFF);
731     if (SC_ERR_NONE != err)
732     {
733         assert(false);
734     }
735 }
736 
BOARD_Camera0_I2C_SendSCCB(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)737 status_t BOARD_Camera0_I2C_SendSCCB(
738     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
739 {
740     return BOARD_LPI2C_SendSCCB(BOARD_CAMERA0_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize,
741                                 (uint8_t *)txBuff, txBuffSize);
742 }
743 
BOARD_Camera0_I2C_ReceiveSCCB(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)744 status_t BOARD_Camera0_I2C_ReceiveSCCB(
745     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
746 {
747     return BOARD_LPI2C_ReceiveSCCB(BOARD_CAMERA0_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
748                                    rxBuffSize);
749 }
750 
BOARD_Camera1_I2C_SendSCCB(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)751 status_t BOARD_Camera1_I2C_SendSCCB(
752     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
753 {
754     return BOARD_LPI2C_SendSCCB(BOARD_CAMERA1_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize,
755                                 (uint8_t *)txBuff, txBuffSize);
756 }
757 
BOARD_Camera1_I2C_ReceiveSCCB(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)758 status_t BOARD_Camera1_I2C_ReceiveSCCB(
759     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
760 {
761     return BOARD_LPI2C_ReceiveSCCB(BOARD_CAMERA1_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
762                                    rxBuffSize);
763 }
764 
BOARD_Codec_I2C_Init(void)765 void BOARD_Codec_I2C_Init(void)
766 {
767     BOARD_LPI2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
768 }
769 
BOARD_Codec_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)770 status_t BOARD_Codec_I2C_Send(
771     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
772 {
773     return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
774                             txBuffSize);
775 }
776 
BOARD_Codec_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)777 status_t BOARD_Codec_I2C_Receive(
778     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
779 {
780     return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
781 }
782 #endif
783