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