hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
  Copyright (c) 2021, Rockchip Inc. All rights reserved.<BR>
 
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php
 
  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
**/
#include "Soc.h"
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/PcdLib.h>
#include <Library/SpiLib.h>
 
/** @defgroup How_To_Use How To Use
 *
 
 The SPI HAL driver can be used as follows:
 
 - Declare a SPI_Handle handle structure, for example:
   ```
   SPI_Handle instance;
   ```
 
 - Invoke SPI_Init() API to configure default config:
     - opMode: slave or master
     - apbTransform
     - endianMode
     - ssd
     - Clock rate
 
 - Invoke SPI_Configure() API to program other mode:
     - Data size
     - Clock polarity and phase
     - FirstBit
     - Clock div
     - Number of data frames received at RX only mode
     - IT FIFO Level and DMA FIFO Level
     - Transfer Mode
 
 - Blocking transfer:
     - The communication is performed in polling mode by calling SPI_PioTransfer().
     - after transfer done, invoke SPI_Stop to release the chip select.
 
 */
 
#define HAL_SPI_FIFO_LENGTH 64
/* Bit fields in SR */
#define HAL_SPI_SR_BUSY     (0x1 << SPI_SR_BSF_SHIFT)
#define HAL_SPI_SR_STB_BUSY (0x1 << SPI_SR_STB_SHIFT)
 
/* Bit fields in ISR, IMR, RISR, 7 bits */
#define SPI_INT_TXEI  (1 << SPI_IMR_TFEIM_SHIFT)
#define SPI_INT_TXOI  (1 << SPI_IMR_TFOIM_SHIFT)
#define SPI_INT_RXUI  (1 << SPI_IMR_RFUIM_SHIFT)
#define SPI_INT_RXOI  (1 << SPI_IMR_RFOIM_SHIFT)
#define SPI_INT_RXFI  (1 << SPI_IMR_RFFIM_SHIFT)
#define SPI_INT_TOI   (1 << SPI_IMR_TOIM_SHIFT)
#define SPI_INT_SSPI  (1 << SPI_IMR_SSPIM_SHIFT)
#define SPI_INT_TXFIM (1 << SPI_IMR_TXFIM_SHIFT)
 
/* Bit fields in ICR */
#define SPI_CLEAR_INT_ALL  (1 << SPI_ICR_CCI_SHIFT)
#define SPI_CLEAR_INT_RXUI (1 << SPI_ICR_CRFUI_SHIFT)
#define SPI_CLEAR_INT_RXOI (1 << SPI_ICR_CRFOI_SHIFT)
#define SPI_CLEAR_INT_TXOI (1 << SPI_ICR_CTFOI_SHIFT)
#define SPI_CLEAR_INT_TOI  (1 << SPI_ICR_CTOI_SHIFT)
#define SPI_ICR_SSPI_SHIFT (1 << SPI_ICR_CSSPI_SHIFT)
#define SPI_CLEAR_INT_TXFI (1 << SPI_ICR_CTXFI_SHIFT)
 
/* Bit fields in DMACR */
#define SPI_DMACR_TX_ENABLE (1 << SPI_DMACR_TDE_SHIFT)
#define SPI_DMACR_RX_ENABLE (1 << SPI_DMACR_RDE_SHIFT)
 
/* Bit fields in SPI TIMEOUT */
#define SPI_TIMEOUT_ENABLE  (1 << SPI_TIMEOUT_TOE_SHIFT)
#define SPI_TIMEOUT_DISABLE 0
 
#define IS_SPI_MODE(__MODE__) (((__MODE__) == CR0_OPM_SLAVE) || \
                               ((__MODE__) == CR0_OPM_MASTER))
 
#define IS_SPI_DIRECTION(__MODE__) (((__MODE__) == CR0_XFM_TR)        || \
                                    ((__MODE__) == CR0_XFM_TO) ||        \
                                    ((__MODE__) == CR0_XFM_RO))
 
#define IS_SPI_DATASIZE(__DATASIZE__) (((__DATASIZE__) == CR0_DATA_FRAME_SIZE_4BIT) || \
                                       ((__DATASIZE__) == CR0_DATA_FRAME_SIZE_8BIT) || \
                                       ((__DATASIZE__) == CR0_DATA_FRAME_SIZE_16BIT))
 
#define IS_SPI_CPOL(__CPOL__) (((__CPOL__) == CR0_POLARITY_LOW) || \
                               ((__CPOL__) == CR0_POLARITY_HIGH))
 
#define IS_SPI_CPHA(__CPHA__) (((__CPHA__) == CR0_PHASE_1EDGE) || \
                               ((__CPHA__) == CR0_PHASE_2EDGE))
 
#define IS_SPI_FIRST_BIT(__BIT__) (((__BIT__) == CR0_FIRSTBIT_MSB) || \
                                   ((__BIT__) == CR0_FIRSTBIT_LSB))
 
#define IS_SPI_APBTRANSFORM(__MODE__) (((__MODE__) == CR0_BHT_16BIT) || \
                                      ((__MODE__) == CR0_BHT_8BIT))
 
#define IS_SPI_ENDIAN_MODE(__MODE__) (((__MODE__) == CR0_EM_BIG) || \
                                      ((__MODE__) == CR0_EM_LITTLE))
 
#define IS_SPI_SSD_BIT(__MODE__) (((__MODE__) == CR0_SSD_HALF) || \
                                  ((__MODE__) == CR0_SSD_ONE))
 
#define IS_SPI_CSM(__NCYCLES__) (((__NCYCLES__) == CR0_CSM_0CYCLE) ||  \
                                 ((__NCYCLES__) == CR0_CSM_1CYCLE) ||  \
                                 ((__NCYCLES__) == CR0_CSM_2CYCLES) || \
                                 ((__NCYCLES__) == CR0_CSM_3CYCLES))
 
/**
  * @brief  Initialize the SPI according to the specified parameters.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @param  base: SPI controller register base address.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_Init(struct SPI_HANDLE *pSPI, UINT32 base)
{
  pSPI->pReg = (struct SPI_REG *)(long)base;
 
  pSPI->config.opMode = CR0_OPM_MASTER;
 
  /* Default config */
  pSPI->config.apbTransform = CR0_BHT_8BIT;
  pSPI->config.endianMode = CR0_EM_BIG;
  pSPI->config.ssd = CR0_SSD_ONE;
  pSPI->config.csm = CR0_CSM_0CYCLE;
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Start or stop the spi module.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @param  enable: start or stop the spi module.
  * @return status
  */
static inline RETURN_STATUS SPI_EnableChip(struct SPI_HANDLE *pSPI, int enable)
{
  WRITE_REG(pSPI->pReg->ENR, (enable ? 1 : 0));
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Configure the spi clock division.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @param  div: clock division.
  * @return status
  */
static inline RETURN_STATUS SPI_SetClock(struct SPI_HANDLE *pSPI, UINT16 div)
{
  WRITE_REG(pSPI->pReg->BAUDR, div);
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Configure the cs signal.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @param select: cs number select.
  * @param  enable: active or inactive the cs signal.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_SetCS(struct SPI_HANDLE *pSPI, UINT8 select, UINT8 enable)
{
  UINT32 ser;
 
  ASSERT(pSPI != NULL);
 
  ser = READ_REG(pSPI->pReg->SER) & SPI_SER_SER_MASK;
 
  if (enable) {
    ser |= 1 << select;
  } else {
    ser &= ~(1 << select);
  }
 
  WRITE_REG(pSPI->pReg->SER, ser);
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Wait for the transfer finished.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_FlushFifo(struct SPI_HANDLE *pSPI)
{
  ASSERT(pSPI != NULL);
 
  while (READ_REG(pSPI->pReg->RXFLR)) {
    READ_REG(pSPI->pReg->RXDR);
  }
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  The max amount of data can be written in blocking mode.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return Max bytes can xfer.
  */
static inline UINT32 SPI_TxMax(struct SPI_HANDLE *pSPI)
{
  UINT32 txLeft, txRoom;
 
  txLeft = (pSPI->pTxBufferEnd - pSPI->pTxBuffer) / pSPI->config.nBytes;
  txRoom = HAL_SPI_FIFO_LENGTH - READ_REG(pSPI->pReg->TXFLR);
 
  return MIN(txLeft, txRoom);
}
 
/**
  * @brief  Send an amount of data in blocking mode.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
static RETURN_STATUS SPI_PioWrite(struct SPI_HANDLE *pSPI)
{
  UINT32 max = SPI_TxMax(pSPI);
  UINT32 txw = 0;
 
  while (max--) {
    if (pSPI->config.nBytes == 1) {
      txw = *(const UINT8 *)(pSPI->pTxBuffer);
    } else {
      txw = *(const UINT16 *)(pSPI->pTxBuffer);
    }
 
    WRITE_REG(pSPI->pReg->TXDR, txw);
    pSPI->pTxBuffer += pSPI->config.nBytes;
  }
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Read an amount of data(byte) in blocking mode.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
static RETURN_STATUS SPI_PioReadByte(struct SPI_HANDLE *pSPI)
{
  UINT32 rxLeft = pSPI->pRxBufferEnd - pSPI->pRxBuffer;
  UINT32 rxRoom = READ_REG(pSPI->pReg->RXFLR);
  UINT32 max = MIN(rxLeft, rxRoom);
 
  while (max > 7) {
    *(pSPI->pRxBuffer + 0) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 1) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 2) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 3) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 4) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 5) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 6) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    *(pSPI->pRxBuffer + 7) = (UINT8)READ_REG(pSPI->pReg->RXDR);
    pSPI->pRxBuffer += 8;
    max -= 8;
  }
 
  while (max--) {
    *pSPI->pRxBuffer = (UINT8)READ_REG(pSPI->pReg->RXDR);
    pSPI->pRxBuffer++;
  }
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Read an amount of data(short) in blocking mode.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
static RETURN_STATUS SPI_PioReadShort(struct SPI_HANDLE *pSPI)
{
  UINT32 rxLeft = (pSPI->pRxBufferEnd - pSPI->pRxBuffer) >> 1;
  UINT32 rxRoom = READ_REG(pSPI->pReg->RXFLR);
  UINT32 max = MIN(rxLeft, rxRoom);
 
  while (max > 7) {
    *((UINT16 *)pSPI->pRxBuffer + 0) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 1) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 2) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 3) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 4) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 5) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 6) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    *((UINT16 *)pSPI->pRxBuffer + 7) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    pSPI->pRxBuffer += 16;
    max -= 8;
  }
 
  while (max--) {
    *((UINT16 *)pSPI->pRxBuffer) = (UINT16)READ_REG(pSPI->pReg->RXDR);
    pSPI->pRxBuffer += 2;
  }
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Transmit an amount of data in blocking mode.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_PioTransfer(struct SPI_HANDLE *pSPI)
{
  UINT32 remain = 0;
 
  ASSERT(pSPI != NULL);
 
  pSPI->type = SPI_POLL;
  SPI_EnableChip(pSPI, 1);
 
  do {
    if (pSPI->pTxBuffer) {
      remain = pSPI->pTxBufferEnd - pSPI->pTxBuffer;
      SPI_PioWrite(pSPI);
    }
 
    if (pSPI->pRxBuffer) {
      remain = pSPI->pRxBufferEnd - pSPI->pRxBuffer;
 
      if (pSPI->config.nBytes == 1) {
        SPI_PioReadByte(pSPI);
      } else {
        SPI_PioReadShort(pSPI);
      }
    }
  } while (remain);
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Query the SPI bus state is idle or busy.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return HAL status
  */
RETURN_STATUS SPI_QueryBusState(struct SPI_HANDLE *pSPI)
{
  HAL_ASSERT(pSPI != NULL);
  if (!(READ_REG(pSPI->pReg->SR) & HAL_SPI_SR_BUSY)) {
    return RETURN_SUCCESS;
  }
 
  return -2;
}
 
/**
  * @brief  Stop the transmit.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_Stop(struct SPI_HANDLE *pSPI)
{
  UINT32 ret;
  ASSERT(pSPI != NULL);
 
  do
  {
    ret = SPI_QueryBusState(pSPI);
    if (ret == RETURN_SUCCESS)
      break;
  } while(1);
 
  SPI_EnableChip(pSPI, 0);
 
  return RETURN_SUCCESS;
}
 
/**
  * @brief  Configure the SPI transfer mode depend on the tx/rx buffer.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @return status
  */
static RETURN_STATUS HAL_SPI_ConfigureTransferMode(struct SPI_HANDLE *pSPI)
{
    UINT32 cr0;
 
    if (pSPI->pTxBuffer && pSPI->pRxBuffer) {
        pSPI->config.xfmMode = CR0_XFM_TR;
    } else if (pSPI->pTxBuffer) {
        pSPI->config.xfmMode = CR0_XFM_TO;
    } else if (pSPI->pRxBuffer) {
        pSPI->config.xfmMode = CR0_XFM_RO;
    }
 
    cr0 = READ_REG(pSPI->pReg->CTRLR[0]);
    cr0 &= ~SPI_CTRLR0_XFM_MASK;
    cr0 |= pSPI->config.xfmMode;
 
    WRITE_REG(pSPI->pReg->DMARDLR, pSPI->dmaBurstSize - 1);
    WRITE_REG(pSPI->pReg->CTRLR[0], cr0);
 
    return RETURN_SUCCESS;
}
 
/**
  * @brief  Program the SPI config via this api.
  * @param  pSPI: pointer to a SPI_Handle structure that contains
  *               the configuration information for SPI module.
  * @param  pTxData: pointer to TX buffer.
  * @param  pRxData: pointer to RX buffer.
  * @param  size: amount of data to be sent.
  * @return status
  */
RETURN_STATUS
EFIAPI
SPI_Configure(struct SPI_HANDLE *pSPI, const UINT8 *pTxData, UINT8 *pRxData, UINT32 size)
{
  UINT32 cr0 = 0;
  UINT32 div = 0;
 
  cr0 |= pSPI->config.opMode;
 
  cr0 |= pSPI->config.apbTransform | pSPI->config.endianMode | pSPI->config.ssd;
  /* Data width */
  cr0 |= pSPI->config.nBytes;
 
  /* Mode for polarity, phase, first bit and endian */
  cr0 |= pSPI->config.clkPolarity | pSPI->config.clkPhase | pSPI->config.firstBit;
 
  /* Config CSM cycles */
  cr0 |= pSPI->config.csm;
 
  /* div doesn't support odd number */
  div = DIV_ROUND_UP(pSPI->maxFreq, pSPI->config.speed);
  div = (div + 1) & 0xfffe;
 
  WRITE_REG(pSPI->pReg->CTRLR[0], cr0);
 
  WRITE_REG(pSPI->pReg->TXFTLR, HAL_SPI_FIFO_LENGTH / 2 - 1);
  WRITE_REG(pSPI->pReg->RXFTLR, HAL_SPI_FIFO_LENGTH / 2 - 1);
 
  WRITE_REG(pSPI->pReg->DMATDLR, HAL_SPI_FIFO_LENGTH / 2 - 1);
  WRITE_REG(pSPI->pReg->DMARDLR, 0);
 
  SPI_SetClock(pSPI, div);
 
  pSPI->pTxBuffer = pTxData;
  pSPI->pTxBufferEnd = pTxData + size;
  pSPI->pRxBuffer = pRxData;
  pSPI->pRxBufferEnd = pRxData + size;
  pSPI->len = size;
 
  HAL_SPI_ConfigureTransferMode(pSPI);
 
  if (pSPI->config.xfmMode == CR0_XFM_RO) {
    if (pSPI->config.nBytes == 1) {
      WRITE_REG(pSPI->pReg->CTRLR[1], pSPI->len - 1);
    } else if (pSPI->config.nBytes == 2) {
      WRITE_REG(pSPI->pReg->CTRLR[1], (pSPI->len / 2) - 1);
    } else {
      WRITE_REG(pSPI->pReg->CTRLR[1], (pSPI->len * 2) - 1);
    }
  }
 
  return RETURN_SUCCESS;
}