xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/fmsh.c (revision 9c5e2f1dbb99eaf369ad4db5eb753d1bd4ae53d8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 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_FMSH		0xA1
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 fm25s01a_ooblayout_ecc(struct mtd_info *mtd, int section,
35 				  struct mtd_oob_region *region)
36 {
37 	return -ERANGE;
38 }
39 
40 static int fm25s01a_ooblayout_free(struct mtd_info *mtd, int section,
41 				   struct mtd_oob_region *region)
42 {
43 	if (section)
44 		return -ERANGE;
45 
46 	region->offset = 2;
47 	region->length = 62;
48 
49 	return 0;
50 }
51 
52 static const struct mtd_ooblayout_ops fm25s01a_ooblayout = {
53 	.ecc = fm25s01a_ooblayout_ecc,
54 	.rfree = fm25s01a_ooblayout_free,
55 };
56 
57 static int fm25s01_ooblayout_ecc(struct mtd_info *mtd, int section,
58 				 struct mtd_oob_region *region)
59 {
60 	if (section)
61 		return -ERANGE;
62 
63 	region->offset = 64;
64 	region->length = 64;
65 
66 	return 0;
67 }
68 
69 static int fm25s01_ooblayout_free(struct mtd_info *mtd, int section,
70 				  struct mtd_oob_region *region)
71 {
72 	if (section)
73 		return -ERANGE;
74 
75 	region->offset = 2;
76 	region->length = 62;
77 
78 	return 0;
79 }
80 
81 static const struct mtd_ooblayout_ops fm25s01_ooblayout = {
82 	.ecc = fm25s01_ooblayout_ecc,
83 	.rfree = fm25s01_ooblayout_free,
84 };
85 
86 static const struct spinand_info fmsh_spinand_table[] = {
87 	SPINAND_INFO("FM25S01A", 0xE4,
88 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 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(&fm25s01a_ooblayout, NULL)),
95 	SPINAND_INFO("FM25S02A", 0xE5,
96 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
97 		     NAND_ECCREQ(1, 512),
98 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
99 					      &write_cache_variants,
100 					      &update_cache_variants),
101 		     1,
102 		     SPINAND_ECCINFO(&fm25s01a_ooblayout, NULL)),
103 	SPINAND_INFO("FM25S01", 0xA1,
104 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
105 		     NAND_ECCREQ(1, 512),
106 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
107 					      &write_cache_variants,
108 					      &update_cache_variants),
109 		     0,
110 		     SPINAND_ECCINFO(&fm25s01_ooblayout, NULL)),
111 };
112 
113 /**
114  * fmsh_spinand_detect - initialize device related part in spinand_device
115  * struct if it is a FMSH device.
116  * @spinand: SPI NAND device structure
117  */
118 static int fmsh_spinand_detect(struct spinand_device *spinand)
119 {
120 	u8 *id = spinand->id.data;
121 	int ret;
122 
123 	/*
124 	 * FMSH SPI NAND read ID need a dummy byte,
125 	 * so the first byte in raw_id is dummy.
126 	 */
127 	if (id[1] != SPINAND_MFR_FMSH)
128 		return 0;
129 
130 	ret = spinand_match_and_init(spinand, fmsh_spinand_table,
131 				     ARRAY_SIZE(fmsh_spinand_table), id[2]);
132 	if (ret)
133 		return ret;
134 
135 	return 1;
136 }
137 
138 static const struct spinand_manufacturer_ops fmsh_spinand_manuf_ops = {
139 	.detect = fmsh_spinand_detect,
140 };
141 
142 const struct spinand_manufacturer fmsh_spinand_manufacturer = {
143 	.id = SPINAND_MFR_FMSH,
144 	.name = "FMSH",
145 	.ops = &fmsh_spinand_manuf_ops,
146 };
147