xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/winbond.c (revision 01b8c4d110abb0dcbe36dc5b6b10d93b2b8e2667)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017 exceet electronics GmbH
4  *
5  * Authors:
6  *	Frieder Schrempf <frieder.schrempf@exceet.de>
7  *	Boris Brezillon <boris.brezillon@bootlin.com>
8  */
9 
10 #ifndef __UBOOT__
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #endif
14 #include <linux/mtd/spinand.h>
15 
16 #define SPINAND_MFR_WINBOND		0xEF
17 
18 #define WINBOND_CFG_BUF_READ		BIT(3)
19 
20 static SPINAND_OP_VARIANTS(read_cache_variants,
21 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
22 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
23 		SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
24 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
25 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
26 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
27 
28 static SPINAND_OP_VARIANTS(write_cache_variants,
29 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
30 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
31 
32 static SPINAND_OP_VARIANTS(update_cache_variants,
33 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
34 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
35 
36 static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
37 				  struct mtd_oob_region *region)
38 {
39 	if (section > 3)
40 		return -ERANGE;
41 
42 	region->offset = (16 * section) + 8;
43 	region->length = 8;
44 
45 	return 0;
46 }
47 
48 static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
49 				   struct mtd_oob_region *region)
50 {
51 	if (section > 3)
52 		return -ERANGE;
53 
54 	region->offset = (16 * section) + 2;
55 	region->length = 6;
56 
57 	return 0;
58 }
59 
60 static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
61 	.ecc = w25m02gv_ooblayout_ecc,
62 	.free = w25m02gv_ooblayout_free,
63 };
64 
65 static int w25m02gv_select_target(struct spinand_device *spinand,
66 				  unsigned int target)
67 {
68 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
69 					  SPI_MEM_OP_NO_ADDR,
70 					  SPI_MEM_OP_NO_DUMMY,
71 					  SPI_MEM_OP_DATA_OUT(1,
72 							spinand->scratchbuf,
73 							1));
74 
75 	*spinand->scratchbuf = target;
76 	return spi_mem_exec_op(spinand->slave, &op);
77 }
78 
79 static const struct spinand_info winbond_spinand_table[] = {
80 	SPINAND_INFO("W25M02GV", 0xAB,
81 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 2),
82 		     NAND_ECCREQ(1, 512),
83 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
84 					      &write_cache_variants,
85 					      &update_cache_variants),
86 		     0,
87 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
88 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
89 	SPINAND_INFO("W25N01GV", 0xAA,
90 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
91 		     NAND_ECCREQ(1, 512),
92 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
93 					      &write_cache_variants,
94 					      &update_cache_variants),
95 		     0,
96 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
97 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
98 };
99 
100 /**
101  * winbond_spinand_detect - initialize device related part in spinand_device
102  * struct if it is a Winbond device.
103  * @spinand: SPI NAND device structure
104  */
105 static int winbond_spinand_detect(struct spinand_device *spinand)
106 {
107 	u8 *id = spinand->id.data;
108 	int ret;
109 
110 	/*
111 	 * Winbond SPI NAND read ID need a dummy byte,
112 	 * so the first byte in raw_id is dummy.
113 	 */
114 	if (id[1] != SPINAND_MFR_WINBOND)
115 		return 0;
116 
117 	ret = spinand_match_and_init(spinand, winbond_spinand_table,
118 				     ARRAY_SIZE(winbond_spinand_table), id[2]);
119 	if (ret)
120 		return ret;
121 
122 	return 1;
123 }
124 
125 static int winbond_spinand_init(struct spinand_device *spinand)
126 {
127 	struct nand_device *nand = spinand_to_nand(spinand);
128 	unsigned int i;
129 
130 	/*
131 	 * Make sure all dies are in buffer read mode and not continuous read
132 	 * mode.
133 	 */
134 	for (i = 0; i < nand->memorg.ntargets; i++) {
135 		spinand_select_target(spinand, i);
136 		spinand_upd_cfg(spinand, WINBOND_CFG_BUF_READ,
137 				WINBOND_CFG_BUF_READ);
138 	}
139 
140 	return 0;
141 }
142 
143 static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
144 	.detect = winbond_spinand_detect,
145 	.init = winbond_spinand_init,
146 };
147 
148 const struct spinand_manufacturer winbond_spinand_manufacturer = {
149 	.id = SPINAND_MFR_WINBOND,
150 	.name = "Winbond",
151 	.ops = &winbond_spinand_manuf_ops,
152 };
153