blob: 6ede98c85d217b2b9d61218b0c073c9344dac8d0 [file] [log] [blame]
Frieder Schrempfea4d7c82018-08-16 17:30:14 +02001// 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__
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Frieder Schrempfea4d7c82018-08-16 17:30:14 +020012#include <linux/device.h>
13#include <linux/kernel.h>
14#endif
15#include <linux/mtd/spinand.h>
16
17#define SPINAND_MFR_WINBOND 0xEF
18
19#define WINBOND_CFG_BUF_READ BIT(3)
20
21static SPINAND_OP_VARIANTS(read_cache_variants,
22 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
23 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
24 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
25 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
26 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
27 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
28
29static SPINAND_OP_VARIANTS(write_cache_variants,
30 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
31 SPINAND_PROG_LOAD(true, 0, NULL, 0));
32
33static SPINAND_OP_VARIANTS(update_cache_variants,
34 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
35 SPINAND_PROG_LOAD(false, 0, NULL, 0));
36
37static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
38 struct mtd_oob_region *region)
39{
40 if (section > 3)
41 return -ERANGE;
42
43 region->offset = (16 * section) + 8;
44 region->length = 8;
45
46 return 0;
47}
48
49static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
50 struct mtd_oob_region *region)
51{
52 if (section > 3)
53 return -ERANGE;
54
55 region->offset = (16 * section) + 2;
56 region->length = 6;
57
58 return 0;
59}
60
61static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
62 .ecc = w25m02gv_ooblayout_ecc,
Simon Glass62fd1a42020-02-03 07:35:56 -070063 .rfree = w25m02gv_ooblayout_free,
Frieder Schrempfea4d7c82018-08-16 17:30:14 +020064};
65
66static int w25m02gv_select_target(struct spinand_device *spinand,
67 unsigned int target)
68{
69 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
70 SPI_MEM_OP_NO_ADDR,
71 SPI_MEM_OP_NO_DUMMY,
72 SPI_MEM_OP_DATA_OUT(1,
73 spinand->scratchbuf,
74 1));
75
76 *spinand->scratchbuf = target;
77 return spi_mem_exec_op(spinand->slave, &op);
78}
79
80static const struct spinand_info winbond_spinand_table[] = {
81 SPINAND_INFO("W25M02GV", 0xAB,
82 NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 2),
83 NAND_ECCREQ(1, 512),
84 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
85 &write_cache_variants,
86 &update_cache_variants),
87 0,
88 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
89 SPINAND_SELECT_TARGET(w25m02gv_select_target)),
Robert Markoa2ee9162020-01-16 14:03:35 +010090 SPINAND_INFO("W25N01GV", 0xAA,
91 NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
92 NAND_ECCREQ(1, 512),
93 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
94 &write_cache_variants,
95 &update_cache_variants),
96 0,
97 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
Frieder Schrempfea4d7c82018-08-16 17:30:14 +020098};
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 */
105static 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
125static 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
143static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
144 .detect = winbond_spinand_detect,
145 .init = winbond_spinand_init,
146};
147
148const struct spinand_manufacturer winbond_spinand_manufacturer = {
149 .id = SPINAND_MFR_WINBOND,
150 .name = "Winbond",
151 .ops = &winbond_spinand_manuf_ops,
152};