sf_mtd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
  4. */
  5. #include <common.h>
  6. #include <flash.h>
  7. #include <malloc.h>
  8. #include <linux/errno.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <spi_flash.h>
  11. static struct mtd_info sf_mtd_info;
  12. static bool sf_mtd_registered;
  13. static char sf_mtd_name[8];
  14. static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  15. {
  16. struct spi_flash *flash = mtd->priv;
  17. int err;
  18. if (!flash)
  19. return -ENODEV;
  20. instr->state = MTD_ERASING;
  21. err = spi_flash_erase(flash, instr->addr, instr->len);
  22. if (err) {
  23. instr->state = MTD_ERASE_FAILED;
  24. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  25. return -EIO;
  26. }
  27. instr->state = MTD_ERASE_DONE;
  28. mtd_erase_callback(instr);
  29. return 0;
  30. }
  31. static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  32. size_t *retlen, u_char *buf)
  33. {
  34. struct spi_flash *flash = mtd->priv;
  35. int err;
  36. if (!flash)
  37. return -ENODEV;
  38. err = spi_flash_read(flash, from, len, buf);
  39. if (!err)
  40. *retlen = len;
  41. return err;
  42. }
  43. static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  44. size_t *retlen, const u_char *buf)
  45. {
  46. struct spi_flash *flash = mtd->priv;
  47. int err;
  48. if (!flash)
  49. return -ENODEV;
  50. err = spi_flash_write(flash, to, len, buf);
  51. if (!err)
  52. *retlen = len;
  53. return err;
  54. }
  55. static void spi_flash_mtd_sync(struct mtd_info *mtd)
  56. {
  57. }
  58. static int spi_flash_mtd_number(void)
  59. {
  60. #ifdef CONFIG_SYS_MAX_FLASH_BANKS
  61. return CONFIG_SYS_MAX_FLASH_BANKS;
  62. #else
  63. return 0;
  64. #endif
  65. }
  66. int spi_flash_mtd_register(struct spi_flash *flash)
  67. {
  68. int ret;
  69. if (sf_mtd_registered) {
  70. ret = del_mtd_device(&sf_mtd_info);
  71. if (ret)
  72. return ret;
  73. sf_mtd_registered = false;
  74. }
  75. sf_mtd_registered = false;
  76. memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
  77. sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
  78. sf_mtd_info.name = sf_mtd_name;
  79. sf_mtd_info.type = MTD_NORFLASH;
  80. sf_mtd_info.flags = MTD_CAP_NORFLASH;
  81. sf_mtd_info.writesize = 1;
  82. sf_mtd_info.writebufsize = flash->page_size;
  83. sf_mtd_info._erase = spi_flash_mtd_erase;
  84. sf_mtd_info._read = spi_flash_mtd_read;
  85. sf_mtd_info._write = spi_flash_mtd_write;
  86. sf_mtd_info._sync = spi_flash_mtd_sync;
  87. sf_mtd_info.size = flash->size;
  88. sf_mtd_info.priv = flash;
  89. /* Only uniform flash devices for now */
  90. sf_mtd_info.numeraseregions = 0;
  91. sf_mtd_info.erasesize = flash->sector_size;
  92. ret = add_mtd_device(&sf_mtd_info);
  93. if (!ret)
  94. sf_mtd_registered = true;
  95. return ret;
  96. }
  97. void spi_flash_mtd_unregister(void)
  98. {
  99. int ret;
  100. if (!sf_mtd_registered)
  101. return;
  102. ret = del_mtd_device(&sf_mtd_info);
  103. if (!ret) {
  104. sf_mtd_registered = false;
  105. return;
  106. }
  107. /*
  108. * Setting mtd->priv to NULL is the best we can do. Thanks to that,
  109. * the MTD layer can still call mtd hooks without risking a
  110. * use-after-free bug. Still, things should be fixed to prevent the
  111. * spi_flash object from being destroyed when del_mtd_device() fails.
  112. */
  113. sf_mtd_info.priv = NULL;
  114. printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
  115. sf_mtd_info.name);
  116. }