smc_eeprom.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
  3. *
  4. * Copyright 2005, Seagate Technology LLC
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. #undef DEBUG
  25. #include <common.h>
  26. #include <command.h>
  27. #include <config.h>
  28. #include <net.h>
  29. #include "vct.h"
  30. #define SMSC9118_BASE CONFIG_DRIVER_SMC911X_BASE
  31. #define BYTE_TEST (SMSC9118_BASE + 0x64)
  32. #define GPIO_CFG (SMSC9118_BASE + 0x88)
  33. #define MAC_CSR_CMD (SMSC9118_BASE + 0xA4)
  34. #define MAC_CSR_CMD_CSR_BUSY (0x80000000)
  35. #define MAC_CSR_CMD_RNW (0x40000000)
  36. #define MAC_RD_CMD(reg) ((reg & 0x000000FF) | \
  37. (MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_RNW))
  38. #define MAC_WR_CMD(reg) ((reg & 0x000000FF) | \
  39. (MAC_CSR_CMD_CSR_BUSY))
  40. #define MAC_CSR_DATA (SMSC9118_BASE + 0xA8)
  41. #define E2P_CMD (SMSC9118_BASE + 0xB0)
  42. #define E2P_CMD_EPC_BUSY_ (0x80000000UL) /* Self Clearing */
  43. #define E2P_CMD_EPC_CMD_ (0x70000000UL) /* R/W */
  44. #define E2P_CMD_EPC_CMD_READ_ (0x00000000UL) /* R/W */
  45. #define E2P_CMD_EPC_CMD_EWDS_ (0x10000000UL) /* R/W */
  46. #define E2P_CMD_EPC_CMD_EWEN_ (0x20000000UL) /* R/W */
  47. #define E2P_CMD_EPC_CMD_WRITE_ (0x30000000UL) /* R/W */
  48. #define E2P_CMD_EPC_CMD_WRAL_ (0x40000000UL) /* R/W */
  49. #define E2P_CMD_EPC_CMD_ERASE_ (0x50000000UL) /* R/W */
  50. #define E2P_CMD_EPC_CMD_ERAL_ (0x60000000UL) /* R/W */
  51. #define E2P_CMD_EPC_CMD_RELOAD_ (0x70000000UL) /* R/W */
  52. #define E2P_CMD_EPC_TIMEOUT_ (0x00000200UL) /* R */
  53. #define E2P_CMD_MAC_ADDR_LOADED_ (0x00000100UL) /* RO */
  54. #define E2P_CMD_EPC_ADDR_ (0x000000FFUL) /* R/W */
  55. #define E2P_DATA (SMSC9118_BASE + 0xB4)
  56. #define MAC_ADDRH (0x2)
  57. #define MAC_ADDRL (0x3)
  58. #define MAC_TIMEOUT 200
  59. #define HIBYTE(word) ((u8)(((u16)(word)) >> 8))
  60. #define LOBYTE(word) ((u8)(((u16)(word)) & 0x00FFU))
  61. #define HIWORD(dword) ((u16)(((u32)(dword)) >> 16))
  62. #define LOWORD(dword) ((u16)(((u32)(dword)) & 0x0000FFFFUL))
  63. static int mac_busy(int req_to)
  64. {
  65. int timeout = req_to;
  66. while (timeout--) {
  67. if (!(smc911x_reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY))
  68. goto done;
  69. }
  70. return 1; /* Timeout */
  71. done:
  72. return 0; /* No timeout */
  73. }
  74. static ulong get_mac_reg(int reg)
  75. {
  76. ulong reg_val = 0xffffffff;
  77. if (smc911x_reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) {
  78. printf("get_mac_reg: previous command not complete\n");
  79. goto done;
  80. }
  81. smc911x_reg_write(MAC_CSR_CMD, MAC_RD_CMD(reg));
  82. udelay(10000);
  83. if (mac_busy(MAC_TIMEOUT) == 1) {
  84. printf("get_mac_reg: timeout waiting for response from MAC\n");
  85. goto done;
  86. }
  87. reg_val = smc911x_reg_read(MAC_CSR_DATA);
  88. done:
  89. return (reg_val);
  90. }
  91. static ulong eeprom_enable_access(void)
  92. {
  93. ulong gpio;
  94. gpio = smc911x_reg_read(GPIO_CFG);
  95. debug("%s: gpio= 0x%08lx ---> 0x%08lx\n", __func__, gpio,
  96. (gpio & 0xFF0FFFFFUL));
  97. smc911x_reg_write(GPIO_CFG, (gpio & 0xFF0FFFFFUL));
  98. return gpio;
  99. }
  100. static void eeprom_disable_access(ulong gpio)
  101. {
  102. debug("%s: gpio= 0x%08lx\n", __func__, gpio);
  103. smc911x_reg_write(GPIO_CFG, gpio);
  104. }
  105. static int eeprom_is_mac_address_loaded(void)
  106. {
  107. int ret;
  108. ret = smc911x_reg_read(MAC_CSR_CMD) & E2P_CMD_MAC_ADDR_LOADED_;
  109. debug("%s: ret = %x\n", __func__, ret);
  110. return ret;
  111. }
  112. static int eeprom_read_location(unchar address, u8 *data)
  113. {
  114. ulong timeout = 100000;
  115. ulong temp = 0;
  116. if ((temp = smc911x_reg_read(E2P_CMD)) & E2P_CMD_EPC_BUSY_) {
  117. printf("%s: Busy at start, E2P_CMD=0x%08lX\n", __func__, temp);
  118. return 0;
  119. }
  120. smc911x_reg_write(E2P_CMD,
  121. (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_READ_ |
  122. ((ulong) address)));
  123. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  124. udelay(10);
  125. timeout--;
  126. }
  127. if (timeout == 0) {
  128. printf("Timeout\n");
  129. return 0;
  130. }
  131. (*data) = (unchar) (smc911x_reg_read(E2P_DATA));
  132. debug("%s: ret = %x\n", __func__, (*data));
  133. return 1;
  134. }
  135. static int eeprom_enable_erase_and_write(void)
  136. {
  137. ulong timeout = 100000;
  138. if (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  139. printf("%s: Busy at start\n", __func__);
  140. return 0;
  141. }
  142. smc911x_reg_write(E2P_CMD, (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_EWEN_));
  143. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  144. udelay(10);
  145. timeout--;
  146. }
  147. if (timeout == 0) {
  148. printf("Timeout[1]\n");
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static int eeprom_disable_erase_and_write(void)
  154. {
  155. ulong timeout = 100000;
  156. if (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  157. printf("%s: Busy at start\n", __func__);
  158. return 0;
  159. }
  160. smc911x_reg_write(E2P_CMD, (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_EWDS_));
  161. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  162. udelay(10);
  163. timeout--;
  164. }
  165. if (timeout == 0) {
  166. printf("Timeout[2]\n");
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. static int eeprom_write_location(unchar address, unchar data)
  172. {
  173. ulong timeout = 100000;
  174. debug("%s: address: %x data = %x\n", __func__, address, data);
  175. if (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  176. printf("%s: Busy at start\n", __func__);
  177. return 0;
  178. }
  179. smc911x_reg_write(E2P_DATA, ((ulong) data));
  180. smc911x_reg_write(E2P_CMD,
  181. (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_WRITE_ |
  182. ((ulong) address)));
  183. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  184. udelay(10);
  185. timeout--;
  186. }
  187. if (timeout == 0) {
  188. printf("Timeout[3]\n");
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. static int eeprom_erase_all(void)
  194. {
  195. ulong timeout = 100000;
  196. if (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  197. printf("%s: Busy at start\n", __func__);
  198. return 0;
  199. }
  200. smc911x_reg_write(E2P_CMD, (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_ERAL_));
  201. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  202. udelay(10);
  203. timeout--;
  204. }
  205. if (timeout == 0) {
  206. printf("Timeout[4]\n");
  207. return 0;
  208. }
  209. return 1;
  210. }
  211. static int eeprom_reload(void)
  212. {
  213. ulong timeout = 100000;
  214. if (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  215. printf("%s: Busy at start\n", __func__);
  216. return -1;
  217. }
  218. smc911x_reg_write(E2P_CMD,
  219. (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_RELOAD_));
  220. while ((timeout > 0) && (smc911x_reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY_)) {
  221. udelay(10);
  222. timeout--;
  223. }
  224. if (timeout == 0)
  225. return 0;
  226. return 1;
  227. }
  228. static int eeprom_save_mac_address(ulong dwHi16, ulong dwLo32)
  229. {
  230. int result = 0;
  231. debug("%s: dwHI: 0x%08lx dwLO: %08lx, \n", __func__, dwHi16, dwLo32);
  232. if (!eeprom_enable_erase_and_write())
  233. goto DONE;
  234. if (!eeprom_erase_all())
  235. goto DONE;
  236. if (!eeprom_write_location(0, 0xA5))
  237. goto DONE;
  238. if (!eeprom_write_location(1, LOBYTE(LOWORD(dwLo32))))
  239. goto DONE;
  240. if (!eeprom_write_location(2, HIBYTE(LOWORD(dwLo32))))
  241. goto DONE;
  242. if (!eeprom_write_location(3, LOBYTE(HIWORD(dwLo32))))
  243. goto DONE;
  244. if (!eeprom_write_location(4, HIBYTE(HIWORD(dwLo32))))
  245. goto DONE;
  246. if (!eeprom_write_location(5, LOBYTE(LOWORD(dwHi16))))
  247. goto DONE;
  248. if (!eeprom_write_location(6, HIBYTE(LOWORD(dwHi16))))
  249. goto DONE;
  250. if (!eeprom_disable_erase_and_write())
  251. goto DONE;
  252. result = 1;
  253. DONE:
  254. return result;
  255. }
  256. static int do_eeprom_dump(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  257. {
  258. unchar data = 0, index = 0;
  259. ulong gpio_old_val;
  260. gpio_old_val = eeprom_enable_access();
  261. printf("EEPROM content: \n");
  262. for (index = 0; index < 8; index++) {
  263. if (eeprom_read_location(index, &data))
  264. printf("%02x ", data);
  265. else
  266. printf("FAILED");
  267. }
  268. eeprom_disable_access(gpio_old_val);
  269. printf("\n");
  270. return 0;
  271. }
  272. static int do_eeprom_erase_all(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  273. {
  274. eeprom_erase_all();
  275. return 0;
  276. }
  277. static int do_eeprom_save_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  278. {
  279. ulong hi16, lo32;
  280. unchar ethaddr[6], i;
  281. ulong gpio;
  282. char *tmp, *end;
  283. tmp = argv[1];
  284. for (i = 0; i < 6; i++) {
  285. ethaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  286. if (tmp)
  287. tmp = (*end) ? end + 1 : end;
  288. }
  289. hi16 = (ethaddr[5] << 8) | (ethaddr[4]);
  290. lo32 = (ethaddr[3] << 24) | (ethaddr[2] << 16) |
  291. (ethaddr[1] << 8) | (ethaddr[0]);
  292. gpio = eeprom_enable_access();
  293. eeprom_save_mac_address(hi16, lo32);
  294. eeprom_reload();
  295. /* Check new values */
  296. if (eeprom_is_mac_address_loaded()) {
  297. ulong mac_hi16, mac_lo32;
  298. mac_hi16 = get_mac_reg(MAC_ADDRH);
  299. mac_lo32 = get_mac_reg(MAC_ADDRL);
  300. printf("New MAC address: %lx, %lx\n", mac_hi16, mac_lo32);
  301. } else {
  302. printf("Address is not reloaded \n");
  303. }
  304. eeprom_disable_access(gpio);
  305. return 0;
  306. }
  307. U_BOOT_CMD(smcee, 1, 0, do_eeprom_erase_all,
  308. "smcee - Erase content of SMC EEPROM\n",);
  309. U_BOOT_CMD(smced, 1, 0, do_eeprom_dump,
  310. "smced - Dump content of SMC EEPROM\n",);
  311. U_BOOT_CMD(smcew, 2, 0, do_eeprom_save_mac,
  312. "smcew - Write MAC address to SMC EEPROM\n",
  313. "aa:bb:cc:dd:ee:ff new mac address\n");