0721a067af468e56c1f6cdf35e5f8f7ec93a8c26
[openwrt/openwrt.git] / target / linux / layerscape / patches-4.4 / 1093-mtd-spi-nor-check-return-value-from-read-write.patch
1 From 8527843351169d999995d331bbdad75560ccafb2 Mon Sep 17 00:00:00 2001
2 From: Michal Suchanek <hramrach@gmail.com>
3 Date: Wed, 2 Dec 2015 10:38:20 +0000
4 Subject: [PATCH 093/113] mtd: spi-nor: check return value from read/write
5
6 SPI NOR hardware drivers now return useful value from their read/write
7 functions so check them.
8
9 Signed-off-by: Michal Suchanek <hramrach@gmail.com>
10 Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@freescale.com>
11 ---
12 drivers/mtd/spi-nor/spi-nor.c | 50 +++++++++++++++++++++++++++++------------
13 1 file changed, 36 insertions(+), 14 deletions(-)
14
15 --- a/drivers/mtd/spi-nor/spi-nor.c
16 +++ b/drivers/mtd/spi-nor/spi-nor.c
17 @@ -922,7 +922,10 @@ static int spi_nor_read(struct mtd_info
18 ret = nor->read(nor, from, len, retlen, buf);
19
20 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
21 - return ret;
22 + if (ret < 0)
23 + return ret;
24 +
25 + return 0;
26 }
27
28 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
29 @@ -948,10 +951,14 @@ static int sst_write(struct mtd_info *mt
30 nor->program_opcode = SPINOR_OP_BP;
31
32 /* write one byte. */
33 - nor->write(nor, to, 1, retlen, buf);
34 + ret = nor->write(nor, to, 1, retlen, buf);
35 + if (ret < 0)
36 + goto sst_write_err;
37 + WARN(ret != 1, "While writing 1 byte written %i bytes\n",
38 + (int)ret);
39 ret = spi_nor_wait_till_ready(nor);
40 if (ret)
41 - goto time_out;
42 + goto sst_write_err;
43 }
44 to += actual;
45
46 @@ -960,10 +967,14 @@ static int sst_write(struct mtd_info *mt
47 nor->program_opcode = SPINOR_OP_AAI_WP;
48
49 /* write two bytes. */
50 - nor->write(nor, to, 2, retlen, buf + actual);
51 + ret = nor->write(nor, to, 2, retlen, buf + actual);
52 + if (ret < 0)
53 + goto sst_write_err;
54 + WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
55 + (int)ret);
56 ret = spi_nor_wait_till_ready(nor);
57 if (ret)
58 - goto time_out;
59 + goto sst_write_err;
60 to += 2;
61 nor->sst_write_second = true;
62 }
63 @@ -972,21 +983,24 @@ static int sst_write(struct mtd_info *mt
64 write_disable(nor);
65 ret = spi_nor_wait_till_ready(nor);
66 if (ret)
67 - goto time_out;
68 + goto sst_write_err;
69
70 /* Write out trailing byte if it exists. */
71 if (actual != len) {
72 write_enable(nor);
73
74 nor->program_opcode = SPINOR_OP_BP;
75 - nor->write(nor, to, 1, retlen, buf + actual);
76 -
77 + ret = nor->write(nor, to, 1, retlen, buf + actual);
78 + if (ret < 0)
79 + goto sst_write_err;
80 + WARN(ret != 1, "While writing 1 byte written %i bytes\n",
81 + (int)ret);
82 ret = spi_nor_wait_till_ready(nor);
83 if (ret)
84 - goto time_out;
85 + goto sst_write_err;
86 write_disable(nor);
87 }
88 -time_out:
89 +sst_write_err:
90 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
91 return ret;
92 }
93 @@ -1015,14 +1029,18 @@ static int spi_nor_write(struct mtd_info
94
95 /* do all the bytes fit onto one page? */
96 if (page_offset + len <= nor->page_size) {
97 - nor->write(nor, to, len, retlen, buf);
98 + ret = nor->write(nor, to, len, retlen, buf);
99 + if (ret < 0)
100 + goto write_err;
101 } else {
102 /* the size of data remaining on the first page */
103 page_size = nor->page_size - page_offset;
104 - nor->write(nor, to, page_size, retlen, buf);
105 + ret = nor->write(nor, to, page_size, retlen, buf);
106 + if (ret < 0)
107 + goto write_err;
108
109 /* write everything in nor->page_size chunks */
110 - for (i = page_size; i < len; i += page_size) {
111 + for (i = ret; i < len; ) {
112 page_size = len - i;
113 if (page_size > nor->page_size)
114 page_size = nor->page_size;
115 @@ -1033,7 +1051,11 @@ static int spi_nor_write(struct mtd_info
116
117 write_enable(nor);
118
119 - nor->write(nor, to + i, page_size, retlen, buf + i);
120 + ret = nor->write(nor, to + i, page_size, retlen,
121 + buf + i);
122 + if (ret < 0)
123 + goto write_err;
124 + i += ret;
125 }
126 }
127