1623b1527bc1e417633a1bcea8ac26d9fba1f1a1
[openwrt.git] / package / boot / uboot-lantiq / patches / 0006-sf-add-generic-support-for-4-byte-address-mode.patch
1 From fb9ed0ef6f0ba6b6535c64dcfcf45c161723e56f Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Tue, 6 Nov 2012 19:31:38 +0100
4 Subject: sf: add generic support for 4-byte address mode
5
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
7
8 --- a/drivers/mtd/spi/spi_flash.c
9 +++ b/drivers/mtd/spi/spi_flash.c
10 @@ -18,19 +18,35 @@
11  static void spi_flash_addr(struct spi_flash *flash, u32 addr, u8 *cmd, u8 *cmd_len)
12  {
13         /* cmd[0] is actual command */
14 -       cmd[1] = addr >> 16;
15 -       cmd[2] = addr >> 8;
16 -       cmd[3] = addr >> 0;
17 -       *cmd_len = 4;
18 +       if (spi_flash_use_4byte_mode(flash)) {
19 +               cmd[1] = addr >> 24;
20 +               cmd[2] = addr >> 16;
21 +               cmd[3] = addr >> 8;
22 +               cmd[4] = addr >> 0;
23 +               *cmd_len = 5;
24 +       } else {
25 +               cmd[1] = addr >> 16;
26 +               cmd[2] = addr >> 8;
27 +               cmd[3] = addr >> 0;
28 +               *cmd_len = 4;
29 +       }
30  }
31  
32  static void spi_flash_page_addr(struct spi_flash *flash, u32 page_addr, u32 byte_addr, u8 *cmd, u8 *cmd_len)
33  {
34         /* cmd[0] is actual command */
35 -       cmd[1] = page_addr >> 8;
36 -       cmd[2] = page_addr >> 0;
37 -       cmd[3] = byte_addr;
38 -       *cmd_len = 4;
39 +       if (spi_flash_use_4byte_mode(flash)) {
40 +               cmd[1] = page_addr >> 16;
41 +               cmd[2] = page_addr >> 8;
42 +               cmd[3] = page_addr >> 0;
43 +               cmd[4] = byte_addr;
44 +               *cmd_len = 5;
45 +       } else {
46 +               cmd[1] = page_addr >> 8;
47 +               cmd[2] = page_addr >> 0;
48 +               cmd[3] = byte_addr;
49 +               *cmd_len = 4;
50 +       }
51  }
52  
53  static int spi_flash_read_write(struct spi_slave *spi,
54 @@ -81,7 +97,7 @@ int spi_flash_cmd_write_multi(struct spi
55         unsigned long page_addr, byte_addr, page_size;
56         size_t chunk_len, actual;
57         int ret;
58 -       u8 cmd[4], cmd_len;
59 +       u8 cmd[5], cmd_len;
60  
61         page_size = flash->page_size;
62         page_addr = offset / page_size;
63 @@ -99,8 +115,8 @@ int spi_flash_cmd_write_multi(struct spi
64  
65                 spi_flash_page_addr(flash, page_addr, byte_addr, cmd, &cmd_len);
66  
67 -               debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
68 -                     buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
69 +               debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x%02x } chunk_len = %zu\n",
70 +                     buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], chunk_len);
71  
72                 ret = spi_flash_cmd_write_enable(flash);
73                 if (ret < 0) {
74 @@ -146,7 +162,7 @@ int spi_flash_read_common(struct spi_fla
75  int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
76                 size_t len, void *data)
77  {
78 -       u8 cmd[5], cmd_len;
79 +       u8 cmd[6], cmd_len;
80  
81         cmd[0] = CMD_READ_ARRAY_FAST;
82         spi_flash_addr(flash, offset, cmd, &cmd_len);
83 @@ -202,7 +218,7 @@ int spi_flash_cmd_erase(struct spi_flash
84  {
85         u32 start, end, erase_size;
86         int ret;
87 -       u8 cmd[4], cmd_len;
88 +       u8 cmd[5], cmd_len;
89  
90         erase_size = flash->sector_size;
91         if (offset % erase_size || len % erase_size) {
92 @@ -227,8 +243,8 @@ int spi_flash_cmd_erase(struct spi_flash
93                 spi_flash_addr(flash, offset, cmd, &cmd_len);
94                 offset += erase_size;
95  
96 -               debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
97 -                     cmd[2], cmd[3], offset);
98 +               debug("SF: erase %2x %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
99 +                     cmd[2], cmd[3], cmd[4], offset);
100  
101                 ret = spi_flash_cmd_write_enable(flash);
102                 if (ret)
103 @@ -409,6 +425,12 @@ int spi_flash_probe_spl(struct spi_flash
104                 goto err_manufacturer_probe;
105         }
106  
107 +       ret = spi_flash_set_4byte_mode(flash);
108 +       if (ret) {
109 +               debug("SF: Failed to enable 4 byte mode: %d\n", ret);
110 +               goto err_manufacturer_probe;
111 +       }
112 +
113         spi_release_bus(spi);
114  
115         return 0;
116 --- a/drivers/mtd/spi/spi_flash_internal.h
117 +++ b/drivers/mtd/spi/spi_flash_internal.h
118 @@ -97,6 +97,31 @@ int spi_flash_cmd_wait_ready(struct spi_
119  /* Erase sectors. */
120  int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len);
121  
122 +#ifdef CONFIG_SPI_FLASH_4BYTE_MODE
123 +static inline int spi_flash_use_4byte_mode(struct spi_flash *flash)
124 +{
125 +       return NULL != flash->set_4byte_mode;
126 +}
127 +
128 +static inline int spi_flash_set_4byte_mode(struct spi_flash *flash)
129 +{
130 +       if (spi_flash_use_4byte_mode(flash))
131 +               return flash->set_4byte_mode(flash);
132 +
133 +       return 0;
134 +}
135 +#else
136 +static inline int spi_flash_use_4byte_mode(struct spi_flash *flash)
137 +{
138 +       return 0;
139 +}
140 +
141 +static inline int spi_flash_set_4byte_mode(struct spi_flash *flash)
142 +{
143 +       return 0;
144 +}
145 +#endif
146 +
147  /* Manufacturer-specific probe functions */
148  int spi_flash_probe_spansion(struct spi_flash *flash, u8 *idcode);
149  int spi_flash_probe_atmel(struct spi_flash *flash, u8 *idcode);
150 --- a/include/spi_flash.h
151 +++ b/include/spi_flash.h
152 @@ -46,6 +46,9 @@ struct spi_flash {
153                                 size_t len, const void *buf);
154         int             (*erase)(struct spi_flash *flash, u32 offset,
155                                 size_t len);
156 +#ifdef CONFIG_SPI_FLASH_4BYTE_MODE
157 +       int             (*set_4byte_mode)(struct spi_flash *flash);
158 +#endif
159  };
160  
161  struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,