blob: cceafc8007c54708f8cca9e425b5f9e817510efc [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2 *
3 * (C)Copyright 2014 Marvell Hefei Branch. All Rights Reserved.
4 *
5 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF MARVELL.
6 * The copyright notice above does not evidence any actual or intended
7 * publication of such source code.
8 * This Module contains Proprietary Information of Marvell and should be
9 * treated as Confidential.
10 * The information in this file is provided for the exclusive use of the
11 * licensees of Marvell.
12 * Such users have the right to use, modify, and incorporate this code into
13 * products for purposes authorized by the license agreement provided they
14 * include this notice and the associated copyright notice with any such
15 * product.
16 * The information in this file is provided "AS IS" without warranty.
17 *
18 ******************************************************************************/
19
20#include "spi.h"
21#include "PlatformConfig.h"
22#include "xllp_dmac.h"
23
24UINT gpio_cs_bit = 12; /* SPI_CS: NezhaS using GPIO76, 76%64=12 */
25UINT gpio_base = GPIO2_BASE;
26
27#define GPIO_CS_BIT gpio_cs_bit
28#define pGPIO_LR (volatile int *)(gpio_base + GPIO_PLR) //Pin level. set 0
29#define pGPIO_DR (volatile int *)(gpio_base + GPIO_PDR) //Direction. set 0
30#define pGPIO_SR (volatile int *)(gpio_base + GPIO_PSR) //Set. set 0
31#define pGPIO_CR (volatile int *)(gpio_base + GPIO_PCR) //Clear. set 0
32#define pGPIO_SDR (volatile int *)(gpio_base + GPIO_SDR) //Bit set. set 0
33
34#define GPIO_CS_SET (1<< GPIO_CS_BIT)
35
36void SPI_ConfigCS(void)
37{
38#ifdef SSP_CS_USE_GPIO
39 gpio_cs_bit = (PlatformIsNezhac() || PlatformIsFalconA0())?12:13;
40 gpio_base = (PlatformIsNezhac() || PlatformIsFalconA0())?GPIO2_BASE:GPIO0_BASE;
41 obm_printf("SPI %d 0x%x\n\r", gpio_cs_bit, gpio_base);
42 *pGPIO_DR |= (0x1<<gpio_cs_bit);
43#endif
44}
45
46void Assert_CS(void)
47{
48#ifdef SSP_CS_USE_GPIO
49 *pGPIO_CR |= GPIO_CS_SET;
50 while (*pGPIO_LR & GPIO_CS_SET);
51#else
52 UINT_T top_ctrl = BU_REG_READ(SSP_TCR);
53 if(top_ctrl & SSP_TCR_SSE)
54 reg_bit_clr(SSP_TCR, SSP_TCR_SSE);
55 reg_bit_set(SSP_TCR, SSP_TCR_HFL);
56 if(top_ctrl & SSP_TCR_SSE)
57 reg_bit_set(SSP_TCR, SSP_TCR_SSE);
58#endif
59}
60
61void Deassert_CS(void)
62{
63#ifdef SSP_CS_USE_GPIO
64 *pGPIO_SR |= GPIO_CS_SET;
65 while (!(*pGPIO_LR & GPIO_CS_SET));
66#else
67 UINT_T top_ctrl = BU_REG_READ(SSP_TCR);
68 if(top_ctrl & SSP_TCR_SSE)
69 reg_bit_clr(SSP_TCR, SSP_TCR_SSE);
70 reg_bit_clr(SSP_TCR, SSP_TCR_HFL);
71 if(top_ctrl & SSP_TCR_SSE)
72 reg_bit_set(SSP_TCR, SSP_TCR_SSE);
73#endif
74}
75
76void SPI_DisableSSP(void)
77{
78 //make sure SSP is disabled
79 reg_bit_clr(SSP_TCR, SSP_TCR_SSE);
80
81 //reset SSP CR's
82 reg_write(SSP_TCR, SSP_TCR_INITIAL);
83 reg_write(SSP_FCR, SSP_FCR_INITIAL);
84 reg_write(SSP_IER, SSP_IER_INITIAL);
85}
86
87void SPI_WaitSSPComplete(void)
88{
89 volatile int timeout = 0xFFFF;
90
91 while ((*SSP_SR & (SSP_SSSR_TFL | SSP_SSSR_TF_NF | SSP_SSSR_BSY)) != SSP_SSSR_TF_NF)
92 {
93 ROW_DELAY(DEFAULT_TIMEOUT);
94
95 if((timeout--) <= 0)
96 {
97 obm_printf("SPI_WaitSSPComplete timeout\n\r");
98 break;
99 }
100 }
101}
102
103void ROW_DELAY(UINT_T x)
104{
105 while (x > 0)
106 {
107 x--;
108 }
109}
110
111
112/***********************************************************
113* SPI_Write_Read
114* PIO mode to write and then read out the data
115* Returns:
116* None
117*************************************************************/
118void SPI_Write_Read(unsigned char *cmd, unsigned char *data, unsigned char len)
119{
120 unsigned char i;
121
122 for (i = 0; i < len; i++)
123 {
124 BU_REG_WRITE8(SSP_DR, cmd[i]);
125 SPI_WaitSSPComplete();
126
127 data[i] = BU_REG_READ8(SSP_DR);
128 }
129}
130
131void SPI_FireUp(void)
132{
133 reg_bit_set(SSP_TCR, SSP_TCR_SSE);
134}
135
136void SPI_ConfigDSS(int dss)
137{
138 UINT_T top_ctrl;
139 top_ctrl = BU_REG_READ(SSP_TCR);
140 if(top_ctrl & SSP_TCR_SSE)
141 reg_bit_clr(SSP_TCR, SSP_TCR_SSE);
142 reg_bit_clr(SSP_TCR, SSP_TCR_DSS_MASK);
143 reg_bit_set(SSP_TCR, SHIFT5(dss-1));
144 if(top_ctrl & SSP_TCR_SSE)
145 reg_bit_set(SSP_TCR, SSP_TCR_SSE);
146}
147
148UINT_T SPI_ReadData(void)
149{
150 return BU_REG_READ(SSP_DR);
151}
152
153void SPI_WriteData(UINT_T data)
154{
155 BU_REG_WRITE(SSP_DR, data);
156}
157
158
159void SPI_ConfigInt(int setting)
160{
161 reg_bit_set(SSP_IER, SSP_IER_TIE | SSP_IER_RIE | SSP_IER_RTOIE);
162}
163
164void SPI_ConfigDMA(int rft, int tft, int rre, int twe, int rafc)
165{
166 UINT_T fcr;
167
168 reg_write(SSP_TOR, SSP_TOR_TIMEOUT);
169 reg_bit_set(SSP_TCR, SSP_TCR_TRIAL);
170
171 fcr = SSP_FCR_TWE(twe) | SSP_FCR_RRE(rre) | SSP_FCR_RFT(rft) | SSP_FCR_TFT(tft);
172 fcr |= SSP_FCR_TSRE;
173 fcr |= SSP_FCR_RSRE;
174 if(rafc)
175 fcr |= SSP_FCR_RAFC;
176
177 reg_write(SSP_FCR, fcr);
178
179 return;
180}