eon.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * (C) Copyright 2010, ucRobotics Inc.
  3. * Author: Chong Huang <chuang@ucrobotics.com>
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <spi_flash.h>
  9. #include "spi_flash_internal.h"
  10. /* EN25Q128-specific commands */
  11. #define CMD_EN25Q128_WREN 0x06 /* Write Enable */
  12. #define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
  13. #define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
  14. #define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
  15. #define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
  16. #define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  17. #define CMD_EN25Q128_PP 0x02 /* Page Program */
  18. #define CMD_EN25Q128_SE 0x20 /* Sector Erase */
  19. #define CMD_EN25Q128_BE 0xd8 /* Block Erase */
  20. #define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
  21. #define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
  22. #define EON_ID_EN25Q128 0x18
  23. struct eon_spi_flash_params {
  24. u8 idcode1;
  25. u16 page_size;
  26. u16 pages_per_sector;
  27. u16 sectors_per_block;
  28. u16 nr_sectors;
  29. const char *name;
  30. };
  31. /* spi_flash needs to be first so upper layers can free() it */
  32. struct eon_spi_flash {
  33. struct spi_flash flash;
  34. const struct eon_spi_flash_params *params;
  35. };
  36. static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
  37. {
  38. return container_of(flash, struct eon_spi_flash, flash);
  39. }
  40. static const struct eon_spi_flash_params eon_spi_flash_table[] = {
  41. {
  42. .idcode1 = EON_ID_EN25Q128,
  43. .page_size = 256,
  44. .pages_per_sector = 16,
  45. .sectors_per_block = 16,
  46. .nr_sectors = 4096,
  47. .name = "EN25Q128",
  48. },
  49. };
  50. static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
  51. {
  52. return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
  53. }
  54. struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
  55. {
  56. const struct eon_spi_flash_params *params;
  57. struct eon_spi_flash *eon;
  58. unsigned int i;
  59. for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
  60. params = &eon_spi_flash_table[i];
  61. if (params->idcode1 == idcode[2])
  62. break;
  63. }
  64. if (i == ARRAY_SIZE(eon_spi_flash_table)) {
  65. debug("SF: Unsupported EON ID %02x\n", idcode[1]);
  66. return NULL;
  67. }
  68. eon = malloc(sizeof(*eon));
  69. if (!eon) {
  70. debug("SF: Failed to allocate memory\n");
  71. return NULL;
  72. }
  73. eon->params = params;
  74. eon->flash.spi = spi;
  75. eon->flash.name = params->name;
  76. eon->flash.write = spi_flash_cmd_write_multi;
  77. eon->flash.erase = eon_erase;
  78. eon->flash.read = spi_flash_cmd_read_fast;
  79. eon->flash.page_size = params->page_size;
  80. eon->flash.sector_size = params->page_size * params->pages_per_sector
  81. * params->sectors_per_block;
  82. eon->flash.size = params->page_size * params->pages_per_sector
  83. * params->nr_sectors;
  84. return &eon->flash;
  85. }