w39.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2008 coresystems GmbH
  5. * Copyright (C) 2010 Carl-Daniel Hailfinger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "flash.h"
  22. static int printlock_w39_fwh_block(struct flashctx *flash, unsigned int offset)
  23. {
  24. chipaddr wrprotect = flash->virtual_registers + offset + 2;
  25. uint8_t locking;
  26. locking = chip_readb(flash, wrprotect);
  27. msg_cdbg("Lock status of block at 0x%08x is ", offset);
  28. switch (locking & 0x7) {
  29. case 0:
  30. msg_cdbg("Full Access.\n");
  31. break;
  32. case 1:
  33. msg_cdbg("Write Lock (Default State).\n");
  34. break;
  35. case 2:
  36. msg_cdbg("Locked Open (Full Access, Lock Down).\n");
  37. break;
  38. case 3:
  39. msg_cerr("Error: Write Lock, Locked Down.\n");
  40. break;
  41. case 4:
  42. msg_cdbg("Read Lock.\n");
  43. break;
  44. case 5:
  45. msg_cdbg("Read/Write Lock.\n");
  46. break;
  47. case 6:
  48. msg_cerr("Error: Read Lock, Locked Down.\n");
  49. break;
  50. case 7:
  51. msg_cerr("Error: Read/Write Lock, Locked Down.\n");
  52. break;
  53. }
  54. /* Read or write lock present? */
  55. return (locking & ((1 << 2) | (1 << 0))) ? -1 : 0;
  56. }
  57. static int unlock_w39_fwh_block(struct flashctx *flash, unsigned int offset)
  58. {
  59. chipaddr wrprotect = flash->virtual_registers + offset + 2;
  60. uint8_t locking;
  61. locking = chip_readb(flash, wrprotect);
  62. /* Read or write lock present? */
  63. if (locking & ((1 << 2) | (1 << 0))) {
  64. /* Lockdown active? */
  65. if (locking & (1 << 1)) {
  66. msg_cerr("Can't unlock block at 0x%08x!\n", offset);
  67. return -1;
  68. } else {
  69. msg_cdbg("Unlocking block at 0x%08x\n", offset);
  70. chip_writeb(flash, 0, wrprotect);
  71. }
  72. }
  73. return 0;
  74. }
  75. static uint8_t w39_idmode_readb(struct flashctx *flash, unsigned int offset)
  76. {
  77. chipaddr bios = flash->virtual_memory;
  78. uint8_t val;
  79. /* Product Identification Entry */
  80. chip_writeb(flash, 0xAA, bios + 0x5555);
  81. chip_writeb(flash, 0x55, bios + 0x2AAA);
  82. chip_writeb(flash, 0x90, bios + 0x5555);
  83. programmer_delay(10);
  84. /* Read something, maybe hardware lock bits */
  85. val = chip_readb(flash, bios + offset);
  86. /* Product Identification Exit */
  87. chip_writeb(flash, 0xAA, bios + 0x5555);
  88. chip_writeb(flash, 0x55, bios + 0x2AAA);
  89. chip_writeb(flash, 0xF0, bios + 0x5555);
  90. programmer_delay(10);
  91. return val;
  92. }
  93. static int printlock_w39_tblwp(uint8_t lock)
  94. {
  95. msg_cdbg("Hardware bootblock locking (#TBL) is %sactive.\n",
  96. (lock & (1 << 2)) ? "" : "not ");
  97. msg_cdbg("Hardware remaining chip locking (#WP) is %sactive..\n",
  98. (lock & (1 << 3)) ? "" : "not ");
  99. if (lock & ((1 << 2) | (1 << 3)))
  100. return -1;
  101. return 0;
  102. }
  103. static int printlock_w39_bootblock_64k16k(uint8_t lock)
  104. {
  105. msg_cdbg("Software 64 kB bootblock locking is %sactive.\n",
  106. (lock & (1 << 0)) ? "" : "not ");
  107. msg_cdbg("Software 16 kB bootblock locking is %sactive.\n",
  108. (lock & (1 << 1)) ? "" : "not ");
  109. if (lock & ((1 << 1) | (1 << 0)))
  110. return -1;
  111. return 0;
  112. }
  113. static int printlock_w39_common(struct flashctx *flash, unsigned int offset)
  114. {
  115. uint8_t lock;
  116. lock = w39_idmode_readb(flash, offset);
  117. msg_cdbg("Lockout bits:\n");
  118. return printlock_w39_tblwp(lock);
  119. }
  120. static int printlock_w39_fwh(struct flashctx *flash)
  121. {
  122. unsigned int i, total_size = flash->total_size * 1024;
  123. int ret = 0;
  124. /* Print lock status of the complete chip */
  125. for (i = 0; i < total_size; i += flash->page_size)
  126. ret |= printlock_w39_fwh_block(flash, i);
  127. return ret;
  128. }
  129. static int unlock_w39_fwh(struct flashctx *flash)
  130. {
  131. unsigned int i, total_size = flash->total_size * 1024;
  132. /* Unlock the complete chip */
  133. for (i = 0; i < total_size; i += flash->page_size)
  134. if (unlock_w39_fwh_block(flash, i))
  135. return -1;
  136. return 0;
  137. }
  138. int printlock_w39l040(struct flashctx *flash)
  139. {
  140. uint8_t lock;
  141. int ret;
  142. lock = w39_idmode_readb(flash, 0x00002);
  143. msg_cdbg("Bottom boot block:\n");
  144. ret = printlock_w39_bootblock_64k16k(lock);
  145. lock = w39_idmode_readb(flash, 0x7fff2);
  146. msg_cdbg("Top boot block:\n");
  147. ret |= printlock_w39_bootblock_64k16k(lock);
  148. return ret;
  149. }
  150. int printlock_w39v040a(struct flashctx *flash)
  151. {
  152. uint8_t lock;
  153. int ret = 0;
  154. /* The W39V040A datasheet contradicts itself on the lock register
  155. * location: 0x00002 and 0x7fff2 are both mentioned. Pick the one
  156. * which is similar to the other chips of the same family.
  157. */
  158. lock = w39_idmode_readb(flash, 0x7fff2);
  159. msg_cdbg("Lockout bits:\n");
  160. ret = printlock_w39_tblwp(lock);
  161. ret |= printlock_w39_bootblock_64k16k(lock);
  162. return ret;
  163. }
  164. int printlock_w39v040b(struct flashctx *flash)
  165. {
  166. return printlock_w39_common(flash, 0x7fff2);
  167. }
  168. int printlock_w39v040c(struct flashctx *flash)
  169. {
  170. /* Typo in the datasheet? The other chips use 0x7fff2. */
  171. return printlock_w39_common(flash, 0xfff2);
  172. }
  173. int printlock_w39v040fa(struct flashctx *flash)
  174. {
  175. int ret = 0;
  176. ret = printlock_w39v040a(flash);
  177. ret |= printlock_w39_fwh(flash);
  178. return ret;
  179. }
  180. int printlock_w39v040fb(struct flashctx *flash)
  181. {
  182. int ret = 0;
  183. ret = printlock_w39v040b(flash);
  184. ret |= printlock_w39_fwh(flash);
  185. return ret;
  186. }
  187. int printlock_w39v040fc(struct flashctx *flash)
  188. {
  189. int ret = 0;
  190. /* W39V040C and W39V040FC use different WP/TBL offsets. */
  191. ret = printlock_w39_common(flash, 0x7fff2);
  192. ret |= printlock_w39_fwh(flash);
  193. return ret;
  194. }
  195. int printlock_w39v080a(struct flashctx *flash)
  196. {
  197. return printlock_w39_common(flash, 0xffff2);
  198. }
  199. int printlock_w39v080fa(struct flashctx *flash)
  200. {
  201. int ret = 0;
  202. ret = printlock_w39v080a(flash);
  203. ret |= printlock_w39_fwh(flash);
  204. return ret;
  205. }
  206. int printlock_w39v080fa_dual(struct flashctx *flash)
  207. {
  208. msg_cinfo("Block locking for W39V080FA in dual mode is "
  209. "undocumented.\n");
  210. /* Better safe than sorry. */
  211. return -1;
  212. }
  213. int unlock_w39v040fb(struct flashctx *flash)
  214. {
  215. if (unlock_w39_fwh(flash))
  216. return -1;
  217. if (printlock_w39_common(flash, 0x7fff2))
  218. return -1;
  219. return 0;
  220. }
  221. int unlock_w39v080fa(struct flashctx *flash)
  222. {
  223. if (unlock_w39_fwh(flash))
  224. return -1;
  225. if (printlock_w39_common(flash, 0xffff2))
  226. return -1;
  227. return 0;
  228. }