forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqspi_flash.c
590 lines (498 loc) · 17.4 KB
/
qspi_flash.c
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/****************************************************************************
* drivers/spi/qspi_flash.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <string.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spi/qspi_flash.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Define the FLASH SIZE in bytes */
#ifdef CONFIG_QSPI_FLASH_1M
# define CONFIG_QSPI_FLASH_SIZE (128 * 1024)
# define CONFIG_QSPI_FLASH_CAPACITY 0x11
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE
# define CONFIG_QSPI_FLASH_SECTORSIZE 2048
#endif
#endif
#ifdef CONFIG_QSPI_FLASH_8M
# define CONFIG_QSPI_FLASH_SIZE (1024 * 1024)
# define CONFIG_QSPI_FLASH_CAPACITY 0x14
#endif
#ifdef CONFIG_QSPI_FLASH_32M
# define CONFIG_QSPI_FLASH_SIZE (4 * 1024 * 1024)
# define CONFIG_QSPI_FLASH_CAPACITY 0x16
#endif
#ifdef CONFIG_QSPI_FLASH_64M
# define CONFIG_QSPI_FLASH_SIZE (8 * 1024 * 1024)
# define CONFIG_QSPI_FLASH_CAPACITY 0x17
#endif
#ifdef CONFIG_QSPI_FLASH_128M
# define CONFIG_QSPI_FLASH_SIZE (16 * 1024 * 1024)
# define CONFIG_QSPI_FLASH_CAPACITY 0x18
#endif
#ifndef CONFIG_QSPI_FLASH_MANUFACTURER
# define CONFIG_QSPI_FLASH_MANUFACTURER 0x20
#endif
#ifndef CONFIG_QSPI_FLASH_MEMORY_TYPE
# define CONFIG_QSPI_FLASH_MEMORY_TYPE 0xba
#endif
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE
# define CONFIG_QSPI_FLASH_SECTORSIZE 65536
#endif
#ifndef CONFIG_QSPI_FLASH_SUBSECTORSIZE
# define CONFIG_QSPI_FLASH_SUBSECTORSIZE 4096
#endif
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE_MASK
# define CONFIG_QSPI_FLASH_SECTORSIZE_MASK (~(CONFIG_QSPI_FLASH_SECTORSIZE-1))
#endif
#ifndef CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK
# define CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK (~(CONFIG_QSPI_FLASH_SUBSECTORSIZE-1))
#endif
#ifndef CONFIG_QSPI_FLASH_PAGESIZE
# define CONFIG_QSPI_FLASH_PAGESIZE 256
#endif
#ifndef CONFIG_QSPI_FLASH_PAGESIZE_MASK
# define CONFIG_QSPI_FLASH_PAGESIZE_MASK (CONFIG_QSPI_FLASH_PAGESIZE-1)
#endif
/* Define FLASH States */
#define QSPI_FLASH_STATE_IDLE 0
#define QSPI_FLASH_STATE_RDID1 1
#define QSPI_FLASH_STATE_RDID2 2
#define QSPI_FLASH_STATE_RDID3 3
#define QSPI_FLASH_STATE_WREN 4
#define QSPI_FLASH_STATE_RDSR 5
#define QSPI_FLASH_STATE_SE1 6
#define QSPI_FLASH_STATE_SE2 7
#define QSPI_FLASH_STATE_SE3 8
#define QSPI_FLASH_STATE_PP1 9
#define QSPI_FLASH_STATE_PP2 10
#define QSPI_FLASH_STATE_PP3 11
#define QSPI_FLASH_STATE_PP4 12
#define QSPI_FLASH_STATE_READ1 13
#define QSPI_FLASH_STATE_READ2 14
#define QSPI_FLASH_STATE_READ3 15
#define QSPI_FLASH_STATE_READ4 16
#define QSPI_FLASH_STATE_FREAD_WAIT 17
/* Instructions */
/* Command Value N Description Addr Dummy Data */
#define QSPI_FLASH_WREN 0x06 /* 1 Write Enable 0 0 0 */
#define QSPI_FLASH_WRDI 0x04 /* 1 Write Disable 0 0 0 */
#define QSPI_FLASH_RDID 0x9f /* 1 Read Identification 0 0 1-3 */
#define QSPI_FLASH_RDSR 0x05 /* 1 Read Status Register 0 0 >=1 */
#define QSPI_FLASH_WRSR 0x01 /* 1 Write Status Register 0 0 1 */
#define QSPI_FLASH_READ 0x03 /* 1 Read Data Bytes 3 0 >=1 */
#define QSPI_FLASH_FAST_READ 0x0b /* 1 Higher speed read 3 1 >=1 */
#define QSPI_FLASH_PP 0x02 /* 1 Page Program 3 0 1-256 */
#define QSPI_FLASH_SE 0xd8 /* 1 Sector Erase 3 0 0 */
#define QSPI_FLASH_BE 0xc7 /* 1 Bulk Erase 0 0 0 */
#define QSPI_FLASH_DP 0xb9 /* 2 Deep power down 0 0 0 */
#define QSPI_FLASH_RES 0xab /* 2 Read Electronic
* Signature 0 3 >=1 */
#define QSPI_FLASH_SSE 0x20 /* 3 Sub-Sector Erase 0 0 0 */
#define QSPI_FLASH_ID 0x9f /* JEDEC ID */
#define QSPI_FLASH_READ_QUAD 0xeb
#define QSPI_FLASH_DUMMY 0xa5
#define QSPI_FLASH_WREN_SET 0x02
/****************************************************************************
* Private Types
****************************************************************************/
struct qspi_flashdev_s
{
struct qspi_dev_s spidev; /* Externally visible part of the SPI interface */
uint32_t selected; /* SPIn base address */
int wren;
int state;
uint16_t read_data;
uint8_t last_cmd;
unsigned long address;
unsigned char data[CONFIG_QSPI_FLASH_SIZE];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* QSPI methods */
static int qspi_flash_lock(FAR struct qspi_dev_s *dev, bool lock);
static uint32_t qspi_flash_setfrequency(FAR struct qspi_dev_s *dev,
uint32_t frequency);
static void qspi_flash_setmode(FAR struct qspi_dev_s *dev,
enum qspi_mode_e mode);
static void qspi_flash_setbits(FAR struct qspi_dev_s *dev, int nbits);
static int qspi_flash_command(FAR struct qspi_dev_s *dev,
FAR struct qspi_cmdinfo_s *cmd);
static int qspi_flash_memory(FAR struct qspi_dev_s *dev,
FAR struct qspi_meminfo_s *mem);
static FAR void *qspi_flash_alloc(FAR struct qspi_dev_s *dev,
size_t buflen);
static void qspi_flash_free(FAR struct qspi_dev_s *dev,
FAR void *buffer);
static void qspi_flash_writeword(FAR struct qspi_flashdev_s *priv,
uint16_t data,
FAR struct qspi_cmdinfo_s *cmdinfo);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct qspi_ops_s g_qspiops =
{
qspi_flash_lock, /* lock */
qspi_flash_setfrequency, /* setfrequency */
qspi_flash_setmode, /* setmode */
qspi_flash_setbits, /* setbits */
#ifdef CONFIG_QSPI_HWFEATURES
NULL, /* hwfeatures */
#endif
qspi_flash_command, /* command */
qspi_flash_memory, /* memory */
qspi_flash_alloc, /* alloc */
qspi_flash_free /* free */
};
struct qspi_flashdev_s g_qspidev =
{
{
&g_qspiops
}
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: qspi_flash_lock
*
* Description:
* On SPI buses where there are multiple devices, it will be necessary to
* lock SPI to have exclusive access to the buses for a sequence of
* transfers. The bus should be locked before the chip is selected. After
* locking the SPI bus, the caller should then also call the setfrequency,
* setbits, and setmode methods to make sure that the SPI is properly
* configured for the device. If the SPI bus is being shared, then it
* may have been left in an incompatible state.
*
* Input Parameters:
* dev - Device-specific state data
* lock - true: Lock spi bus, false: unlock SPI bus
*
* Returned Value:
* None
*
****************************************************************************/
static int qspi_flash_lock(FAR struct qspi_dev_s *dev, bool lock)
{
return OK;
}
/****************************************************************************
* Name: qspi_flash_memory
*
* Description:
* Perform QSPI Memory transaction operations
*
* Returned Value:
* Always returns zero
*
****************************************************************************/
int qspi_flash_memory(FAR struct qspi_dev_s *dev,
FAR struct qspi_meminfo_s *mem)
{
FAR struct qspi_flashdev_s *priv = (FAR struct qspi_flashdev_s *)dev;
switch (mem->cmd)
{
case QSPI_FLASH_READ_QUAD:
priv->wren = 0;
memcpy(mem->buffer, &priv->data[mem->addr], mem->buflen);
priv->address += mem->addr + mem->buflen;
priv->state = QSPI_FLASH_STATE_IDLE;
break;
case QSPI_FLASH_PP:
if (priv->wren)
{
memcpy(&priv->data[mem->addr], mem->buffer, mem->buflen);
}
break;
default:
return -EINVAL;
}
return 0;
}
/****************************************************************************
* Name: qspi_flash_setfrequency
*
* Description:
* Set the SPI frequency.
*
* Input Parameters:
* dev - Device-specific state data
* frequency - The SPI frequency requested
*
* Returned Value:
* Returns the actual frequency selected
*
****************************************************************************/
static uint32_t qspi_flash_setfrequency(FAR struct qspi_dev_s *dev,
uint32_t frequency)
{
return frequency;
}
/****************************************************************************
* Name: qspi_flash_setmode
*
* Description:
* Set the SPI mode. see enum spi_mode_e for mode definitions
*
* Input Parameters:
* dev - Device-specific state data
* mode - The SPI mode requested
*
* Returned Value:
* Returns the actual frequency selected
*
****************************************************************************/
static void qspi_flash_setmode(FAR struct qspi_dev_s *dev,
enum qspi_mode_e mode)
{
}
/****************************************************************************
* Name: qspi_flash_setbits
*
* Description:
* Set the number of bits per word.
*
* Input Parameters:
* dev - Device-specific state data
* nbits - The number of bits requested
*
* Returned Value:
* None
*
****************************************************************************/
static void qspi_flash_setbits(FAR struct qspi_dev_s *dev, int nbits)
{
}
/****************************************************************************
* Name: qspi_flash_alloc
*
* Description:
* Allocate a buffer and associate it with the QSPI device
*
* Input Parameters:
* dev - Device-specific state data
* buflen - Length of buffer to allocate
*
* Returned Value:
* None
*
****************************************************************************/
static FAR void *qspi_flash_alloc(FAR struct qspi_dev_s *dev, size_t buflen)
{
return kmm_malloc(buflen);
}
/****************************************************************************
* Name: qspi_flash_free
*
* Description:
* Allocate a buffer and associate it with the QSPI device
*
* Input Parameters:
* dev - Device-specific state data
* buflen - Length of buffer to allocate
*
* Returned Value:
* None
*
****************************************************************************/
static void qspi_flash_free(FAR struct qspi_dev_s *dev, FAR void *buffer)
{
kmm_free(buffer);
}
/****************************************************************************
* Name: qspi_flash_sectorerase
*
* Description:
* Erase one sector
*
* Input Parameters:
* priv - Device-specific state data
*
* Returned Value:
* None
*
****************************************************************************/
static void qspi_flash_sectorerase(FAR struct qspi_flashdev_s *priv)
{
uint32_t address;
uint32_t len = 0;
/* Ensure the WREN bit is set before any erase operation */
if (priv->wren)
{
address = priv->address;
if (priv->last_cmd == QSPI_FLASH_SE)
{
address &= CONFIG_QSPI_FLASH_SECTORSIZE_MASK;
len = CONFIG_QSPI_FLASH_SECTORSIZE;
}
else if (priv->last_cmd == QSPI_FLASH_SSE)
{
address &= CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK;
len = CONFIG_QSPI_FLASH_SUBSECTORSIZE;
}
/* Now perform the erase */
memset(&priv->data[address], 0xff, len);
}
}
/****************************************************************************
* Name: qspi_flash_writeword
*
* Description:
* Write a word (byte in our case) to the FLASH state machine.
*
* Input Parameters:
* dev - Device-specific state data
* data - the data to send to the simulated FLASH
*
* Returned Value:
* None
*
****************************************************************************/
static void qspi_flash_writeword(FAR struct qspi_flashdev_s *priv,
uint16_t data,
FAR struct qspi_cmdinfo_s *cmdinfo)
{
switch (priv->state)
{
case QSPI_FLASH_STATE_IDLE:
priv->last_cmd = data;
priv->read_data = 0xff;
switch (data)
{
case QSPI_FLASH_WREN:
priv->wren = 1;
break;
case QSPI_FLASH_WRDI:
priv->wren = 0;
break;
/* Sector / Subsector erase */
case QSPI_FLASH_SE:
case QSPI_FLASH_SSE:
priv->address = cmdinfo->addr;
/* Now perform the sector or sub-sector erase.
* Really this should be done during the deselect,
* but this is just a simulation .
*/
qspi_flash_sectorerase(priv);
break;
/* Bulk Erase */
case QSPI_FLASH_BE:
priv->state = QSPI_FLASH_STATE_IDLE;
if (priv->wren)
{
memset(priv->data, 0xff, CONFIG_QSPI_FLASH_SIZE);
}
break;
default:
break;
}
break;
default:
priv->state = QSPI_FLASH_STATE_IDLE;
priv->read_data = 0xff;
break;
}
}
/****************************************************************************
* Name: qspi_flash_command
*
* Description:
* Perform QSPI Command operations
*
* Returned Value:
* Always returns zero
*
****************************************************************************/
static int qspi_flash_command(FAR struct qspi_dev_s *dev,
FAR struct qspi_cmdinfo_s *cmdinfo)
{
FAR uint8_t *p_buf;
FAR struct qspi_flashdev_s *priv = (FAR struct qspi_flashdev_s *)dev;
DEBUGASSERT(cmdinfo->cmd < 256);
/* Does data accompany the command? */
if (QSPICMD_ISDATA(cmdinfo->flags))
{
DEBUGASSERT(cmdinfo->buffer != NULL && cmdinfo->buflen > 0);
p_buf = (FAR uint8_t *)cmdinfo->buffer;
/* Read or write operation? */
if (QSPICMD_ISWRITE(cmdinfo->flags))
{
/* Write data operation */
qspi_flash_writeword(priv, cmdinfo->cmd, cmdinfo);
}
else
{
/* Read data operation */
switch (cmdinfo->cmd)
{
case QSPI_FLASH_ID:
p_buf[0] = CONFIG_QSPI_FLASH_MANUFACTURER;
p_buf[1] = CONFIG_QSPI_FLASH_MEMORY_TYPE;
p_buf[2] = CONFIG_QSPI_FLASH_CAPACITY;
break;
case QSPI_FLASH_RDSR:
p_buf[0] = priv->wren == 1 ? QSPI_FLASH_WREN_SET : 0;
break;
}
}
}
else
{
/* Write data operation */
qspi_flash_writeword(priv, cmdinfo->cmd, cmdinfo);
}
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: qspi_flash_initialize
*
* Description:
* Initialize the selected SPI port
*
* Returned Value:
* Valid SPI device structure reference on success; a NULL on failure
*
****************************************************************************/
FAR struct qspi_dev_s *qspi_flash_initialize(void)
{
FAR struct qspi_flashdev_s *priv = NULL;
priv = &g_qspidev;
priv->selected = 0;
priv->wren = 0;
priv->address = 0;
priv->state = QSPI_FLASH_STATE_IDLE;
priv->read_data = 0xff;
priv->last_cmd = 0xff;
memset(&priv->data[0], 0xff, sizeof(priv->data));
return (FAR struct qspi_dev_s *)priv;
}