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
|
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/clk.h>
#ifdef CONFIG_OF
#include <linux/fb.h>
#endif
#include <soc/sprd/hardware.h>
#include <soc/sprd/sci.h>
#include <soc/sprd/sci_glb_regs.h>
#include "spi_simple_drv.h"
#ifdef CONFIG_OF
#define FB_SPI_CLOCK ("fb_spi_clock")
#define FB_SPI_CLOCK_PARENT ("fb_spi_clock_parent")
#endif
static u32 used_spi_reg_base;
#ifdef CONFIG_OF
static void SPI_Enable(struct device *device,bool is_en)
#else
static void SPI_Enable( uint32_t spi_id, bool is_en)
#endif
{
struct clk *spi_clk;
#ifndef CONFIG_OF
switch (spi_id) {
case 0:
spi_clk = clk_get(NULL, "clk_spi0");
break;
case 1:
spi_clk = clk_get(NULL, "clk_spi1");
break;
case 2:
spi_clk = clk_get(NULL, "clk_spi2");
break;
default:
BUG_ON(1);
}
#else
spi_clk = of_clk_get_by_name(device->of_node, FB_SPI_CLOCK);
if (IS_ERR(spi_clk)) {
printk(KERN_WARNING "sprdfb: get spi clock fail!\n");
return ;
} else {
pr_debug(KERN_INFO "sprdfb: get spi clock ok!\n");
}
#endif
if(is_en)
{
clk_prepare_enable(spi_clk);
}
else
{
clk_disable_unprepare(spi_clk);
}
}
static void SPI_Reset( uint32_t spi_id)
{
u32 rst_reg, rst_bit;
switch (spi_id) {
case 0:
rst_reg = REG_AP_APB_APB_RST;
rst_bit = BIT_SPI0_SOFT_RST;
break;
case 1:
rst_reg = REG_AP_APB_APB_RST;
rst_bit = BIT_SPI1_SOFT_RST;
break;
case 2:
rst_reg = REG_AP_APB_APB_RST;
rst_bit = BIT_SPI2_SOFT_RST;
break;
default:
BUG_ON(1);
}
sci_glb_set(rst_reg, rst_bit);
msleep(50);
sci_glb_clr(rst_reg, rst_bit);
}
/*just set the spi src clk = 26Mhz*/
#ifdef CONFIG_OF
static void SPI_ClkSetting(struct device *device)
#else
static void SPI_ClkSetting(uint32_t spi_id)
#endif
{
struct clk *spi_clk, *spi_src_clk;
#ifndef CONFIG_OF
switch (spi_id) {
case 0:
spi_clk = clk_get(NULL, "clk_spi0");
break;
case 1:
spi_clk = clk_get(NULL, "clk_spi1");
break;
case 2:
spi_clk = clk_get(NULL, "clk_spi2");
break;
default:
BUG_ON(1);
}
spi_src_clk = clk_get(NULL, "ext_26m");
#else
spi_clk = of_clk_get_by_name(device->of_node, FB_SPI_CLOCK);
if (IS_ERR(spi_clk)) {
printk(KERN_WARNING "sprdfb: get spi clock fail!\n");
return ;
} else {
pr_debug(KERN_INFO "sprdfb: get spi clock ok!\n");
}
spi_src_clk = of_clk_get_by_name(device->of_node, FB_SPI_CLOCK_PARENT);
if (IS_ERR(spi_src_clk)) {
printk(KERN_WARNING "sprdfb: get spi clock parent fail!\n");
return ;
} else {
pr_debug(KERN_INFO "sprdfb: get spi clock parent ok!\n");
}
#endif
clk_set_parent(spi_clk, spi_src_clk);
clk_set_rate(spi_clk, SPI_DEF_SRC_CLK);
}
void SPI_SetCsLow( uint32_t spi_sel_csx , bool is_low)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T*)(used_spi_reg_base);
if(is_low) {
spi_ctr_ptr->ctl0 &= ~((1<<spi_sel_csx)<<SPI_SEL_CS_SHIFT);
}
else
{
spi_ctr_ptr->ctl0 |= ((1<<spi_sel_csx)<<SPI_SEL_CS_SHIFT);
}
}
void SPI_SetCd( uint32_t cd)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T*)(used_spi_reg_base);
if(cd == 0) {
spi_ctr_ptr->ctl8 &= ~(SPI_CD_MASK);
} else {
spi_ctr_ptr->ctl8 |= (SPI_CD_MASK);
}
}
#if 0
static void SPI_SetSpiMode(uint32_t spi_mode)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
uint32_t temp = spi_ctr_ptr->ctl7;
temp &= ~SPI_MODE_MASK;
temp |= (spi_mode<<SPI_MODE_SHIFT);
spi_ctr_ptr->ctl7 = temp;
}
#endif
void SPI_SetDatawidth(uint32_t datawidth)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
uint32_t temp = spi_ctr_ptr->ctl0;
if( 32 == datawidth )
{
spi_ctr_ptr->ctl0 &= ~0x7C;
return;
}
temp &= ~0x0000007C;
temp |= (datawidth<<2);
spi_ctr_ptr->ctl0 = temp;
}
static void SPI_SetTxLen(uint32_t data_len, uint32_t dummy_bitlen)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
uint32_t ctl8 = spi_ctr_ptr->ctl8;
uint32_t ctl9 = spi_ctr_ptr->ctl9;
data_len &= TX_MAX_LEN_MASK;
ctl8 &= ~((TX_DUMY_LEN_MASK<<4) | TX_DATA_LEN_H_MASK);
ctl9 &= ~( TX_DATA_LEN_L_MASK );
spi_ctr_ptr->ctl8 = (ctl8 | (dummy_bitlen<<4) | (data_len>>16));
spi_ctr_ptr->ctl9 = (ctl9 | (data_len&0xFFFF));
}
static void SPI_SetRxLen(uint32_t data_len, uint32_t dummy_bitlen)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
uint32_t ctl10 = spi_ctr_ptr->ctl10;
uint32_t ctl11 = spi_ctr_ptr->ctl11;
data_len &= RX_MAX_LEN_MASK;
ctl10 &= ~((RX_DUMY_LEN_MASK<<4) | RX_DATA_LEN_H_MASK);
ctl11 &= ~( RX_DATA_LEN_L_MASK );
spi_ctr_ptr->ctl10 = (ctl10 | (dummy_bitlen<<4) | (data_len>>16));
spi_ctr_ptr->ctl11 = (ctl11 | (data_len&0xFFFF));
}
static void SPI_TxReq( void )
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
spi_ctr_ptr->ctl12 |= SW_TX_REQ_MASK;
}
static void SPI_RxReq( void )
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
spi_ctr_ptr->ctl12 |= SW_RX_REQ_MASK;
}
static unsigned long SPRD_SPI0_BASE = 0xffffffff;
static unsigned long SPRD_SPI1_BASE = 0xffffffff;
static unsigned long SPRD_SPI2_BASE = 0xffffffff;
#ifdef CONFIG_OF
void SPI_Init(struct device *device, u32 spi_id, SPI_INIT_PARM *spi_parm)
#else
void SPI_Init(u32 spi_id, SPI_INIT_PARM *spi_parm)
#endif
{
volatile SPI_CTL_REG_T *spi_ctr_ptr;
u32 temp;
#ifdef CONFIG_OF
SPI_ClkSetting(device);
SPI_Enable(device, true);
#else
SPI_ClkSetting(spi_id);
SPI_Enable(spi_id, true);
#endif
SPI_Reset(spi_id);
switch (spi_id) {
case 0:
used_spi_reg_base = SPRD_SPI0_BASE;
break;
case 1:
used_spi_reg_base = SPRD_SPI1_BASE;
break;
case 2:
used_spi_reg_base = SPRD_SPI2_BASE;
break;
default:
BUG_ON(1);
}
spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
/*fixme, the spi_clk is 1Mhz*/
spi_ctr_ptr->clkd = (SPI_DEF_SRC_CLK / (SPI_DEF_CLK << 1)) - 1;
temp = 0;
temp |= (spi_parm->tx_edge << 1) |
(spi_parm->rx_edge << 0) |
(0x1 << 13) |
(spi_parm->msb_lsb_sel<< 7) ;
spi_ctr_ptr->ctl0 = temp;
spi_ctr_ptr->ctl1 |= BIT(12) | BIT(13);
/*rx fifo full watermark is 16*/
spi_ctr_ptr->ctl3 = 0x10;
/*set SPIMODE_3WIRE_9BIT_SDIO mode*/
spi_ctr_ptr->ctl7 &= ~(0x7 << 3);
spi_ctr_ptr->ctl7 |= SPIMODE_3WIRE_9BIT_SDIO << 3;
}
static void SPI_WaitTxFinish(void)
{
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
while( !((spi_ctr_ptr->iraw)&BIT(8)) ) // IS tx finish
{
}
spi_ctr_ptr->iclr |= BIT(8);
// Wait for spi bus idle
while((spi_ctr_ptr->sts2)&BIT(8))
{
}
// Wait for tx real empty
while( !((spi_ctr_ptr->sts2)&BIT(7)) )
{
}
}
void SPI_WriteData(uint32_t data, uint32_t data_len, uint32_t dummy_bitlen)
{
// uint32_t command;
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
// The unit of data_len is identical with buswidth
SPI_SetTxLen(data_len, dummy_bitlen);
SPI_TxReq( );
spi_ctr_ptr->data = data;
SPI_WaitTxFinish();
}
uint32_t SPI_ReadData( uint32_t data_len, uint32_t dummy_bitlen )
{
uint32_t read_data=0, rxt_cnt=0;
volatile SPI_CTL_REG_T *spi_ctr_ptr = (volatile SPI_CTL_REG_T *)(used_spi_reg_base);
// The unit of data_len is identical with buswidth
SPI_SetRxLen(data_len, dummy_bitlen);
SPI_RxReq( );
//Wait for spi receive finish
while( !((spi_ctr_ptr->iraw)&BIT(9)) )
{
//wait rxt fifo full
if((spi_ctr_ptr->iraw)&BIT(6))
{
rxt_cnt = (spi_ctr_ptr->ctl3)&0x1F;
printk("---FIFOFULL:rxt_cnt=0x%x", rxt_cnt);
while(rxt_cnt--)
{
read_data = spi_ctr_ptr->data;
printk("---FIFOFULL: SPI_ReadData =0x%x", read_data);
}
}
}
while((spi_ctr_ptr->sts2)&BIT(8))
{
}
while(data_len--)
{
read_data = spi_ctr_ptr->data;
printk("---Finish: SPI_ReadData =0x%x", read_data);
}
return (read_data);
}
|