0005-sf-make-calculatiom-of-address-bytes-completely-conf.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  5. Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  6. --- a/drivers/mtd/spi/sf_ops.c
  7. +++ b/drivers/mtd/spi/sf_ops.c
  8. @@ -15,12 +15,17 @@
  9. #include "sf_internal.h"
  10. -static void spi_flash_addr(u32 addr, u8 *cmd)
  11. +static void spi_flash_addr(const struct spi_flash *flash, u32 addr, u8 *cmd)
  12. {
  13. /* cmd[0] is actual command */
  14. - cmd[1] = addr >> 16;
  15. - cmd[2] = addr >> 8;
  16. - cmd[3] = addr >> 0;
  17. + cmd[1] = addr >> (flash->addr_width * 8 - 8);
  18. + cmd[2] = addr >> (flash->addr_width * 8 - 16);
  19. + cmd[3] = addr >> (flash->addr_width * 8 - 24);
  20. +}
  21. +
  22. +static int spi_flash_cmdsz(const struct spi_flash *flash)
  23. +{
  24. + return 1 + flash->addr_width;
  25. }
  26. int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
  27. @@ -158,7 +163,7 @@ int spi_flash_write_common(struct spi_fl
  28. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
  29. {
  30. u32 erase_size;
  31. - u8 cmd[4];
  32. + u8 cmd[4], cmd_len;
  33. int ret = -1;
  34. erase_size = flash->erase_size;
  35. @@ -180,12 +185,13 @@ int spi_flash_cmd_erase_ops(struct spi_f
  36. if (ret < 0)
  37. goto done;
  38. #endif
  39. - spi_flash_addr(offset, cmd);
  40. + spi_flash_addr(flash, offset, cmd);
  41. + cmd_len = spi_flash_cmdsz(flash);
  42. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  43. cmd[2], cmd[3], offset);
  44. - ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
  45. + ret = spi_flash_write_common(flash, cmd, cmd_len, NULL, 0);
  46. if (ret < 0) {
  47. debug("SF: erase failed\n");
  48. goto done;
  49. @@ -206,7 +212,7 @@ int spi_flash_cmd_write_ops(struct spi_f
  50. {
  51. unsigned long byte_addr, page_size;
  52. size_t chunk_len, actual;
  53. - u8 cmd[4];
  54. + u8 cmd[4], cmd_len;
  55. int ret = -1;
  56. ret = spi_claim_bus(flash->spi);
  57. @@ -230,12 +236,13 @@ int spi_flash_cmd_write_ops(struct spi_f
  58. if (flash->spi->max_write_size)
  59. chunk_len = min(chunk_len, flash->spi->max_write_size);
  60. - spi_flash_addr(offset, cmd);
  61. + spi_flash_addr(flash, offset, cmd);
  62. + cmd_len = spi_flash_cmdsz(flash);
  63. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  64. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  65. - ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
  66. + ret = spi_flash_write_common(flash, cmd, cmd_len,
  67. buf + actual, chunk_len);
  68. if (ret < 0) {
  69. debug("SF: write failed\n");
  70. @@ -269,7 +276,7 @@ int spi_flash_read_common(struct spi_fla
  71. int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
  72. size_t len, void *data)
  73. {
  74. - u8 cmd[5], bank_sel = 0;
  75. + u8 cmd[5], cmd_len, bank_sel = 0;
  76. u32 remain_len, read_len;
  77. int ret = -1;
  78. @@ -288,7 +295,6 @@ int spi_flash_cmd_read_ops(struct spi_fl
  79. }
  80. cmd[0] = CMD_READ_ARRAY_FAST;
  81. - cmd[4] = 0x00;
  82. while (len) {
  83. #ifdef CONFIG_SPI_FLASH_BAR
  84. @@ -306,9 +312,11 @@ int spi_flash_cmd_read_ops(struct spi_fl
  85. else
  86. read_len = remain_len;
  87. - spi_flash_addr(offset, cmd);
  88. + spi_flash_addr(flash, offset, cmd);
  89. + cmd_len = spi_flash_cmdsz(flash);
  90. + cmd[cmd_len] = 0x00;
  91. - ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
  92. + ret = spi_flash_read_common(flash, cmd, cmd_len + 1,
  93. data, read_len);
  94. if (ret < 0) {
  95. debug("SF: read failed\n");
  96. --- a/drivers/mtd/spi/sf_probe.c
  97. +++ b/drivers/mtd/spi/sf_probe.c
  98. @@ -218,6 +218,9 @@ static int spi_flash_validate_params(str
  99. flash->poll_cmd = CMD_FLAG_STATUS;
  100. #endif
  101. + /* Configure default 3-byte addressing */
  102. + flash->addr_width = 3;
  103. +
  104. /* Configure the BAR - discover bank cmds and read current bank */
  105. #ifdef CONFIG_SPI_FLASH_BAR
  106. u8 curr_bank = 0;
  107. --- a/include/spi_flash.h
  108. +++ b/include/spi_flash.h
  109. @@ -57,6 +57,7 @@ struct spi_flash {
  110. #endif
  111. u8 poll_cmd;
  112. u8 erase_cmd;
  113. + u8 addr_width;
  114. void *memory_map;
  115. int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);