programmer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "flash.h"
  21. #include "programmer.h"
  22. /* No-op shutdown() for programmers which don't need special handling */
  23. int noop_shutdown(void)
  24. {
  25. return 0;
  26. }
  27. /* Fallback map() for programmers which don't need special handling */
  28. void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
  29. {
  30. /* FIXME: Should return phys_addr. */
  31. return NULL;
  32. }
  33. /* No-op/fallback unmap() for programmers which don't need special handling */
  34. void fallback_unmap(void *virt_addr, size_t len)
  35. {
  36. }
  37. /* No-op chip_writeb() for parallel style drivers not supporting writes */
  38. void noop_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
  39. {
  40. }
  41. /* Little-endian fallback for drivers not supporting 16 bit accesses */
  42. void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
  43. chipaddr addr)
  44. {
  45. chip_writeb(flash, val & 0xff, addr);
  46. chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
  47. }
  48. /* Little-endian fallback for drivers not supporting 16 bit accesses */
  49. uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
  50. {
  51. uint16_t val;
  52. val = chip_readb(flash, addr);
  53. val |= chip_readb(flash, addr + 1) << 8;
  54. return val;
  55. }
  56. /* Little-endian fallback for drivers not supporting 32 bit accesses */
  57. void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
  58. chipaddr addr)
  59. {
  60. chip_writew(flash, val & 0xffff, addr);
  61. chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
  62. }
  63. /* Little-endian fallback for drivers not supporting 32 bit accesses */
  64. uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
  65. {
  66. uint32_t val;
  67. val = chip_readw(flash, addr);
  68. val |= chip_readw(flash, addr + 2) << 16;
  69. return val;
  70. }
  71. void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
  72. {
  73. size_t i;
  74. for (i = 0; i < len; i++)
  75. chip_writeb(flash, buf[i], addr + i);
  76. return;
  77. }
  78. void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
  79. chipaddr addr, size_t len)
  80. {
  81. size_t i;
  82. for (i = 0; i < len; i++)
  83. buf[i] = chip_readb(flash, addr + i);
  84. return;
  85. }
  86. int register_par_master(const struct par_master *mst,
  87. const enum chipbustype buses)
  88. {
  89. struct registered_master rmst;
  90. if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
  91. !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
  92. !mst->chip_readl || !mst->chip_readn) {
  93. msg_perr("%s called with incomplete master definition. "
  94. "Please report a bug at flashrom@flashrom.org\n",
  95. __func__);
  96. return ERROR_FLASHROM_BUG;
  97. }
  98. rmst.buses_supported = buses;
  99. rmst.par = *mst;
  100. return register_master(&rmst);
  101. }
  102. /* The limit of 4 is totally arbitrary. */
  103. #define MASTERS_MAX 4
  104. struct registered_master registered_masters[MASTERS_MAX];
  105. int registered_master_count = 0;
  106. /* This function copies the struct registered_master parameter. */
  107. int register_master(const struct registered_master *mst)
  108. {
  109. if (registered_master_count >= MASTERS_MAX) {
  110. msg_perr("Tried to register more than %i master "
  111. "interfaces.\n", MASTERS_MAX);
  112. return ERROR_FLASHROM_LIMIT;
  113. }
  114. registered_masters[registered_master_count] = *mst;
  115. registered_master_count++;
  116. return 0;
  117. }
  118. enum chipbustype get_buses_supported(void)
  119. {
  120. int i;
  121. enum chipbustype ret = BUS_NONE;
  122. for (i = 0; i < registered_master_count; i++)
  123. ret |= registered_masters[i].buses_supported;
  124. return ret;
  125. }