1 From 6fb5f86b094756d94de8abe7425e3d290ff22dd2 Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Sun, 13 Oct 2013 15:09:28 +0200
4 Subject: sf: make calculatiom of address bytes completely configurable
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
8 diff --git a/drivers/mtd/spi/sf_ops.c b/drivers/mtd/spi/sf_ops.c
9 index d34747b..207adf5 100644
10 --- a/drivers/mtd/spi/sf_ops.c
11 +++ b/drivers/mtd/spi/sf_ops.c
14 #include "sf_internal.h"
16 -static void spi_flash_addr(u32 addr, u8 *cmd)
17 +static void spi_flash_addr(const struct spi_flash *flash, u32 addr, u8 *cmd)
19 /* cmd[0] is actual command */
20 - cmd[1] = addr >> 16;
23 + cmd[1] = addr >> (flash->addr_width * 8 - 8);
24 + cmd[2] = addr >> (flash->addr_width * 8 - 16);
25 + cmd[3] = addr >> (flash->addr_width * 8 - 24);
28 +static int spi_flash_cmdsz(const struct spi_flash *flash)
30 + return 1 + flash->addr_width;
33 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
34 @@ -158,7 +163,7 @@ int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
35 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
42 erase_size = flash->erase_size;
43 @@ -180,12 +185,13 @@ int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
47 - spi_flash_addr(offset, cmd);
48 + spi_flash_addr(flash, offset, cmd);
49 + cmd_len = spi_flash_cmdsz(flash);
51 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
52 cmd[2], cmd[3], offset);
54 - ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
55 + ret = spi_flash_write_common(flash, cmd, cmd_len, NULL, 0);
57 debug("SF: erase failed\n");
59 @@ -206,7 +212,7 @@ int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
61 unsigned long byte_addr, page_size;
62 size_t chunk_len, actual;
67 ret = spi_claim_bus(flash->spi);
68 @@ -230,12 +236,13 @@ int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
69 if (flash->spi->max_write_size)
70 chunk_len = min(chunk_len, flash->spi->max_write_size);
72 - spi_flash_addr(offset, cmd);
73 + spi_flash_addr(flash, offset, cmd);
74 + cmd_len = spi_flash_cmdsz(flash);
76 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
77 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
79 - ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
80 + ret = spi_flash_write_common(flash, cmd, cmd_len,
81 buf + actual, chunk_len);
83 debug("SF: write failed\n");
84 @@ -269,7 +276,7 @@ int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
85 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
86 size_t len, void *data)
88 - u8 cmd[5], bank_sel = 0;
89 + u8 cmd[5], cmd_len, bank_sel = 0;
90 u32 remain_len, read_len;
93 @@ -288,7 +295,6 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
96 cmd[0] = CMD_READ_ARRAY_FAST;
100 #ifdef CONFIG_SPI_FLASH_BAR
101 @@ -306,9 +312,11 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
103 read_len = remain_len;
105 - spi_flash_addr(offset, cmd);
106 + spi_flash_addr(flash, offset, cmd);
107 + cmd_len = spi_flash_cmdsz(flash);
108 + cmd[cmd_len] = 0x00;
110 - ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
111 + ret = spi_flash_read_common(flash, cmd, cmd_len + 1,
114 debug("SF: read failed\n");
115 diff --git a/drivers/mtd/spi/sf_probe.c b/drivers/mtd/spi/sf_probe.c
116 index 380aa09..84289db 100644
117 --- a/drivers/mtd/spi/sf_probe.c
118 +++ b/drivers/mtd/spi/sf_probe.c
119 @@ -218,6 +218,9 @@ static int spi_flash_validate_params(struct spi_flash *flash,
120 flash->poll_cmd = CMD_FLAG_STATUS;
123 + /* Configure default 3-byte addressing */
124 + flash->addr_width = 3;
126 /* Configure the BAR - discover bank cmds and read current bank */
127 #ifdef CONFIG_SPI_FLASH_BAR
129 diff --git a/include/spi_flash.h b/include/spi_flash.h
130 index 411dd1b..cc9398b 100644
131 --- a/include/spi_flash.h
132 +++ b/include/spi_flash.h
133 @@ -57,6 +57,7 @@ struct spi_flash {
140 int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);