1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <zircon/types.h>
8 
9 #define NAND_CE0 (0xe << 10)
10 #define NAND_CE1 (0xd << 10)
11 
12 #define NAND_NCE 0x01
13 #define NAND_CLE 0x02
14 #define NAND_ALE 0x04
15 
16 #define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
17 #define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
18 #define NAND_CTRL_CHANGE 0x80
19 
20 #define NAND_CMD_READ0 0
21 #define NAND_CMD_READ1 1
22 #define NAND_CMD_PAGEPROG 0x10
23 #define NAND_CMD_READOOB 0x50
24 #define NAND_CMD_ERASE1 0x60
25 #define NAND_CMD_STATUS 0x70
26 #define NAND_CMD_SEQIN 0x80
27 #define NAND_CMD_READID 0x90
28 #define NAND_CMD_ERASE2 0xd0
29 #define NAND_CMD_RESET 0xff
30 #define NAND_CMD_NONE -1
31 
32 /* Extended commands for large page devices */
33 #define NAND_CMD_READSTART 0x30
34 
35 /* Status */
36 #define NAND_STATUS_FAIL 0x01
37 #define NAND_STATUS_FAIL_N1 0x02
38 #define NAND_STATUS_TRUE_READY 0x20
39 #define NAND_STATUS_READY 0x40
40 #define NAND_STATUS_WP 0x80
41 
42 struct nand_timings {
43     uint32_t tRC_min;
44     uint32_t tREA_max;
45     uint32_t RHOH_min;
46 };
47 
48 struct nand_chip_table {
49     uint8_t manufacturer_id;
50     uint8_t device_id;
51     const char* manufacturer_name;
52     const char* device_name;
53     struct nand_timings timings;
54     uint32_t chip_delay_us;     /* delay us after enqueuing command */
55     /*
56      * extended_id_nand -> pagesize, erase blocksize, OOB size
57      * could vary given the same device id.
58      */
59     bool extended_id_nand;
60     uint64_t chipsize; /* MiB */
61     /* Valid only if extended_id_nand is false */
62     uint32_t page_size;        /* bytes */
63     uint32_t oobsize;          /* bytes */
64     uint32_t erase_block_size; /* bytes */
65     uint32_t bus_width;        /* 8 vs 16 bit */
66 };
67 
68 typedef struct onfi_callback {
69     void* ctx;
70     void (*cmd_ctrl)(void* ctx, zx_status_t cmd, uint32_t ctrl);
71     uint8_t (*read_byte)(void* ctx);
72 } onfi_callback_t;
73 
74 struct nand_chip_table* find_nand_chip_table(uint8_t manuf_id,
75                                              uint8_t device_id);
76 void onfi_command(onfi_callback_t* cb, uint32_t command,
77                   int32_t column, int32_t page_addr,
78                   uint32_t capacity_mb, uint32_t chip_delay_us,
79                   int buswidth_16);
80 zx_status_t onfi_wait(onfi_callback_t* cb, uint32_t timeout_ms);
81 
82