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
|
/*++
Copyright (c) 2008 WonderMedia Technologies, Inc. All Rights Reserved.
This PROPRIETARY SOFTWARE is the property of WonderMedia Technologies, Inc.
and may contain trade secrets and/or other confidential information of
WonderMedia Technologies, Inc. This file shall not be disclosed to any third
party, in whole or in part, without prior written consent of WonderMedia.
THIS PROPRIETARY SOFTWARE AND ANY RELATED DOCUMENTATION ARE PROVIDED AS IS,
WITH ALL FAULTS, AND WITHOUT WARRANTY OF ANY KIND EITHER EXPRESS OR IMPLIED,
AND WonderMedia TECHNOLOGIES, INC. DISCLAIMS ALL EXPRESS OR IMPLIED WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
NON-INFRINGEMENT.
--*/
#ifndef __SPI_H__
#define __SPI_H__
#define SPI_REG_BASE 0xD8240000 /* SPI Base Address */
#define SPI_REG_BASE1 0xD8250000 /* SPI1 Base Address */
#define SPI_PORT_MAX 2
#define SPI_PORT_NUM 2
#define AHB_FREQ 100000
#define SPI_PORT_REG_BASE_OFFSET 0x10000
#define SPICR 0x00
#define SPISR 0x04
#define SPIDFCR 0x08
#define SPICRE 0x0C
#define SPITXFIFO 0x10
#define SPIRXFIFO 0x30
#define SPISRCV_CNT 0x50
#define SPISRCV_Add_CNT 0x54
#define GPIO_SPI0_SS BIT3
#define GPIO_SPI0_MOSI BIT2
#define GPIO_SPI0_MISO BIT1
#define GPIO_SPI0_CLK BIT0
#define GPIO_SPI0_SS_PULL_EN BIT3
#define GPIO_SPI0_MOSI_PULL_EN BIT2
#define GPIO_SPI0_MISO_PULL_EN BIT1
#define GPIO_SPI0_CLK_PULL_EN BIT0
/**/
/* SPI CFG setting*/
/**/
/************ Control Register ************/
/* Transmit Clock Driver*/
#define SPI_CR_TCD_SHIFT 21
#define SPI_CR_TCD_MASK (BIT31|BIT30|BIT29|BIT28|BIT27|BIT26|BIT25|BIT24|BIT23|BIT22|BIT21)
/* Slave Selection*/
#define SPI_CR_SS_SHIFT 19
#define SPI_CR_SS_MASK (BIT20|BIT19)
/* Transmit FIFO Byte Write Method*/
#define SPI_CR_WM_SHIFT 18
#define SPI_CR_WM_MASK (BIT18)
/* Receive FIFO Reset*/
#define SPI_CR_RFR_SHIFT 17
#define SPI_CR_RFR_MASK (BIT17)
/* Transmit FIFO Reset*/
#define SPI_CR_TFR_SHIFT 16
#define SPI_CR_TFR_MASK (BIT16)
/* DMA Request Control*/
#define SPI_CR_DRC_SHIFT 15
#define SPI_CR_DRC_MASK (BIT15)
/* Receive FIFO Threshold Selection*/
#define SPI_CR_RFTS_SHIFT 14
#define SPI_CR_RFTS_MASK (BIT14)
/* Transmit FIFO Threshold Selection*/
#define SPI_CR_TFTS_SHIFT 13
#define SPI_CR_TFTS_MASK (BIT13)
/* Transmit FIFO Under-run Interrupt*/
#define SPI_CR_TFUI_SHIFT 12
#define SPI_CR_TFUI_MASK (BIT12)
/* Transmit FIFO Empty Interrupt*/
#define SPI_CR_TFEI_SHIFT 11
#define SPI_CR_TFEI_MASK (BIT11)
/* Receive FIFO Over-run Interrupt*/
#define SPI_CR_RFOI_SHIFT 10
#define SPI_CR_RFOI_MASK (BIT10)
/* Receive FIFO Full Interrupt*/
#define SPI_CR_RFFI_SHIFT 9
#define SPI_CR_RFFI_MASK (BIT9)
/* Receive FIFO Empty Interrupt*/
#define SPI_CR_RFEI_SHIFT 8
#define SPI_CR_RFEI_MASK (BIT8)
/* Threshold IRQ/DMA Selection*/
#define SPI_CR_TIDS_SHIFT 7
#define SPI_CR_TIDS_MASK (BIT7)
/* Interrupt Enable*/
#define SPI_CR_IE_SHIFT 6
#define SPI_CR_IE_MASK (BIT6)
/* Module Enable*/
#define SPI_CR_ME_SHIFT 5
#define SPI_CR_ME_MASK (BIT5)
/* Module Fault Error Interrupt*/
#define SPI_CR_MFEI_SHIFT 4
#define SPI_CR_MFEI_MASK (BIT4)
/* Master/Slave Mode Select*/
#define SPI_CR_MSMS_SHIFT 3
#define SPI_CR_MSMS_MASK (BIT3)
/* Clock Polarity Select*/
#define SPI_CR_CPS_SHIFT 2
#define SPI_CR_CPS_MASK (BIT2)
/* Clock Phase Select*/
#define SPI_CR_CPHS_SHIFT 1
#define SPI_CR_CPHS_MASK (BIT1)
/* Module Fault Error Feature*/
#define SPI_CR_MFEF_SHIFT 0
#define SPI_CR_MFEF_MASK (BIT0)
/* SPI Control Register Reset Value*/
#define SPI_CR_RESET_MASK SPI_CR_MSMS_MASK
/************ Status Register *************/
/* RX FIFO Count*/
#define SPI_SR_RFCNT_SHIFT 24
#define SPI_SR_RFCNT_MASK (BIT31|BIT30|BIT29|BIT28|BIT27|BIT26|BIT25|BIT24)
/* TX FIFO Count*/
#define SPI_SR_TFCNT_SHIFT 16
#define SPI_SR_TFCNT_MASK (BIT23|BIT22|BIT21|BIT20|BIT19|BIT18|BIT17|BIT16)
/* TX FIFO Empty Status*/
#define SPI_SR_TFES_SHIFT 15
#define SPI_SR_TFES_MASK (BIT15)
/* Receive FIFO Threshold Passed Interrupt*/
#define SPI_SR_RFTPI_SHIFT 14
#define SPI_SR_RFTPI_MASK (BIT14)
/* Transmit FIFO Threshold Passed Interrupt*/
#define SPI_SR_TFTPI_SHIFT 13
#define SPI_SR_TFTPI_MASK (BIT13)
/* Transmit FIFO Under-run Interrupt*/
#define SPI_SR_TFUI_SHIFT 12
#define SPI_SR_TFUI_MASK (BIT12)
/* Transmit FIFO Empty Interrupt*/
#define SPI_SR_TFEI_SHIFT 11
#define SPI_SR_TFEI_MASK (BIT11)
/* Receive FIFO Over-run Interrupt*/
#define SPI_SR_RFOI_SHIFT 10
#define SPI_SR_RFOI_MASK (BIT10)
/* Receive FIFO Full Interrupt*/
#define SPI_SR_RFFI_SHIFT 9
#define SPI_SR_RFFI_MASK (BIT9)
/* Receive FIFO Empty Interrupt*/
#define SPI_SR_RFEI_SHIFT 8
#define SPI_SR_RFEI_MASK (BIT8)
/* SPI Busy*/
#define SPI_SR_BUSY_SHIFT 7
#define SPI_SR_BUSY_MASK (BIT7)
/* Mode Fault Error Interrupt*/
#define SPI_SR_MFEI_SHIFT 4
#define SPI_SR_MFEI_MASK (BIT4)
/****** Data Format Control Register ******/
/*Preset Counter*/
#define SPI_SSN_PRE_COUNTER_SHIFT 28
#define SPI_SSN_PRE_COUNTER_MASK (BIT31|BIT30|BIT29|BIT28)
/*HOLD EN*/
#define SPI_SSN_HOLD_EN BIT26
/*Microwire EN*/
#define SPI_MICROWIRE_EN BIT25
/*RX theshold Pass Interrupt Enable*/
#define SPI_RX_THESHOLD_INT_EN BIT24
/* Mode Fault Delay Count*/
#define SPI_DFCR_MFDCNT_SHIFT 16
#define SPI_DFCR_MFDCNT_MASK (BIT23|BIT22|BIT21|BIT20|BIT19|BIT18|BIT17|BIT16)
/* TX Drive Count*/
#define SPI_DFCR_TDCNT_SHIFT 8
#define SPI_DFCR_TDCNT_MASK (BIT15|BIT14|BIT13|BIT12|BIT11|BIT10|BIT9|BIT8)
/* TX Drive Enable*/
#define SPI_DFCR_TDE_SHIFT 7
#define SPI_DFCR_TDE_MASK (BIT7)
/* TX No Data Value*/
#define SPI_DFCR_TNDV_SHIFT 6
#define SPI_DFCR_TNDV_MASK (BIT6)
/* Direct SSN Enable*/
#define SPI_DFCR_DSE_SHIFT 5
#define SPI_DFCR_DSE_MASK (BIT5)
/* Direct SSN Value*/
#define SPI_DFCR_DSV_SHIFT 4
#define SPI_DFCR_DSV_MASK (BIT4)
/* SSN Control*/
#define SPI_DFCR_SC_SHIFT 3
#define SPI_DFCR_SC_MASK (BIT3)
/* SSN Port Mode*/
#define SPI_DFCR_SPM_SHIFT 2
#define SPI_DFCR_SPM_MASK (BIT2)
/* Receive Significant Bit Order*/
#define SPI_DFCR_RSBO_SHIFT 1
#define SPI_DFCR_RSBO_MASK (BIT1)
/* Transmit Significant Bit Order*/
#define SPI_DFCR_TSBO_SHIFT 0
#define SPI_DFCR_TSBO_MASK (BIT0)
/* SPI Data Format Control Register Reset Value*/
#define SPI_DFCR_RESET_MASK (SPI_DFCR_DSV_MASK|SPI_DFCR_DSE_MASK)
/* Clock Mode Define*/
#define SPI_CLK_MODE_LIST \
T(SPI_MODE_0), /* 0, CLK Idles Low + SS activation */ \
T(SPI_MODE_1), /* 1, CLK Idles Low + CLK activation */ \
T(SPI_MODE_2), /* 2, CLK Idles High + SS activation */ \
T(SPI_MODE_3) /* 3, CLK Idles High + CLK activation */
/* Bus Master Define*/
#define SPI_ARBITER_LIST \
T(SPI_BUS_MASTER), /* 0 */ \
T(SPI_BUS_SLAVE) /* 1 */
/* Bus Operate Mode Define*/
#define SPI_OP_MODE_LIST \
T(SPI_POLLING_MODE), /* 0 */ \
T(SPI_IRQ_MODE), /* 1 */ \
T(SPI_DMA_MODE) /* 2 */
#define T(x) x
enum {
SPI_CLK_MODE_LIST
};
enum {
SPI_ARBITER_LIST
};
enum {
SPI_OP_MODE_LIST
};
#undef T
struct spi_reg_s {
unsigned int volatile *cr; /* Control Register*/
unsigned int volatile *sr; /* Status Register*/
unsigned int volatile *dfcr; /* Data Format Control Register*/
unsigned int volatile *cre; /*Extended Control Register*/
unsigned char volatile *rfifo; /* Read FIFO, i.e. Receive FIFO*/
unsigned char volatile *wfifo; /* Write FIFO, i.e. Transfer FIFO*/
unsigned int volatile *srcv_cnt;
unsigned int volatile *srcv_add_cnt;
} ;
struct spi_info_s {
/* Register Data*/
struct spi_reg_s regs;
/* Private Data*/
unsigned int freq; /* Bus Frequence, Unit Khz*/
unsigned int clk_mode; /* Bus Clock Mode*/
unsigned int op_mode; /* Operation Mode, polling/irq/dma*/
/* Use for output setting value when FIFO goes empty*/
unsigned char tx_drive_count; /* Set the number of bytes to send out*/
unsigned char tx_drive_enable; /* Enable/Disable TX drive*/
unsigned char tx_nodata_value; /* Set output value, o => 0x0, 1 =>0xff*/
/* Use for SSN Control correlated with slave_select*/
unsigned char ssn_control; /* Set ssn control by 1 => program or 0 => hardware*/
/* user for master and slave control*/
unsigned int arbiter;
unsigned char ssn_port_mode; /* set ssn port mode. 0 => multi-master, 1 => P2P*/
unsigned char slave_select; /* Set output ssn pin {a,b,c,d}*/
unsigned int rx_cnt;
unsigned int tx_cnt;
/* interrup and dma control*/
int irq_num;
void (*isr)(void); /* interrupt service routine*/
void (*dsr)(void); /* dma service routine*/
} ;
//struct spi_info_s SPI_INFO[SPI_PORT_NUM];
//struct spi_info_s *SPI_PORT[SPI_PORT_NUM];
/*---------------------------------------------------------------------------------------*/
/* GPIO Public functions declaration*/
/*---------------------------------------------------------------------------------------*/
void spi_init(void);
void spi_info_reset(int port);
void spi_set_reg(int port);
void spi_enable(int port);
void spi_disable(int port);
struct spi_info_s *spi_get_info(int port);
void spi_write_then_read_data(unsigned char *wbuf, unsigned char *rbuf, int num, int clk_mode, int chip_sel);
int spi_init_clock(void);
#endif /*__SPI_H__*/
|