spr_misc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * (C) Copyright 2009
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  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. #include <common.h>
  24. #include <command.h>
  25. #include <i2c.h>
  26. #include <net.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/spr_xloader_table.h>
  30. #include <asm/arch/spr_defs.h>
  31. #define CPU 0
  32. #define DDR 1
  33. #define SRAM_REL 0xD2801000
  34. DECLARE_GLOBAL_DATA_PTR;
  35. static struct chip_data chip_data;
  36. int dram_init(void)
  37. {
  38. struct xloader_table *xloader_tb =
  39. (struct xloader_table *)XLOADER_TABLE_ADDRESS;
  40. struct xloader_table_1_1 *table_1_1;
  41. struct xloader_table_1_2 *table_1_2;
  42. struct chip_data *chip = &chip_data;
  43. gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
  44. gd->bd->bi_dram[0].size = get_ram_size(PHYS_SDRAM_1,
  45. PHYS_SDRAM_1_MAXSIZE);
  46. if (XLOADER_TABLE_VERSION_1_1 == xloader_tb->table_version) {
  47. table_1_1 = &xloader_tb->table.table_1_1;
  48. chip->dramfreq = table_1_1->ddrfreq;
  49. chip->dramtype = table_1_1->ddrtype;
  50. } else if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
  51. table_1_2 = &xloader_tb->table.table_1_2;
  52. chip->dramfreq = table_1_2->ddrfreq;
  53. chip->dramtype = table_1_2->ddrtype;
  54. } else {
  55. chip->dramfreq = -1;
  56. }
  57. return 0;
  58. }
  59. int misc_init_r(void)
  60. {
  61. #if defined(CONFIG_CMD_NET)
  62. uchar mac_id[6];
  63. if (!eth_getenv_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
  64. eth_setenv_enetaddr("ethaddr", mac_id);
  65. #endif
  66. setenv("verify", "n");
  67. #if defined(CONFIG_SPEAR_USBTTY)
  68. setenv("stdin", "usbtty");
  69. setenv("stdout", "usbtty");
  70. setenv("stderr", "usbtty");
  71. #endif
  72. return 0;
  73. }
  74. int spear_board_init(ulong mach_type)
  75. {
  76. struct xloader_table *xloader_tb =
  77. (struct xloader_table *)XLOADER_TABLE_ADDRESS;
  78. struct xloader_table_1_2 *table_1_2;
  79. struct chip_data *chip = &chip_data;
  80. gd->bd->bi_arch_number = mach_type;
  81. /* adress of boot parameters */
  82. gd->bd->bi_boot_params = CONFIG_BOOT_PARAMS_ADDR;
  83. /* CPU is initialized to work at 333MHz in Xloader */
  84. chip->cpufreq = 333;
  85. if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
  86. table_1_2 = &xloader_tb->table.table_1_2;
  87. memcpy(chip->version, table_1_2->version,
  88. sizeof(chip->version));
  89. }
  90. return 0;
  91. }
  92. static int i2c_read_mac(uchar *buffer)
  93. {
  94. u8 buf[2];
  95. i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  96. /* Check if mac in i2c memory is valid */
  97. if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  98. /* Valid mac address is saved in i2c eeprom */
  99. i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
  100. return 0;
  101. }
  102. return -1;
  103. }
  104. static int write_mac(uchar *mac)
  105. {
  106. u8 buf[2];
  107. buf[0] = (u8)MAGIC_BYTE0;
  108. buf[1] = (u8)MAGIC_BYTE1;
  109. i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  110. buf[0] = (u8)~MAGIC_BYTE0;
  111. buf[1] = (u8)~MAGIC_BYTE1;
  112. i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  113. /* check if valid MAC address is saved in I2C EEPROM or not? */
  114. if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  115. i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
  116. puts("I2C EEPROM written with mac address \n");
  117. return 0;
  118. }
  119. puts("I2C EEPROM writing failed \n");
  120. return -1;
  121. }
  122. int do_chip_config(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  123. {
  124. void (*sram_setfreq) (unsigned int, unsigned int);
  125. struct chip_data *chip = &chip_data;
  126. unsigned char mac[6];
  127. unsigned int reg, frequency;
  128. char *s, *e;
  129. char i2c_mac[20];
  130. if ((argc > 3) || (argc < 2)) {
  131. cmd_usage(cmdtp);
  132. return 1;
  133. }
  134. if ((!strcmp(argv[1], "cpufreq")) || (!strcmp(argv[1], "ddrfreq"))) {
  135. frequency = simple_strtoul(argv[2], NULL, 0);
  136. if (frequency > 333) {
  137. printf("Frequency is limited to 333MHz\n");
  138. return 1;
  139. }
  140. sram_setfreq = memcpy((void *)SRAM_REL, setfreq, setfreq_sz);
  141. if (!strcmp(argv[1], "cpufreq")) {
  142. sram_setfreq(CPU, frequency);
  143. printf("CPU frequency changed to %u\n", frequency);
  144. chip->cpufreq = frequency;
  145. } else {
  146. sram_setfreq(DDR, frequency);
  147. printf("DDR frequency changed to %u\n", frequency);
  148. chip->dramfreq = frequency;
  149. }
  150. return 0;
  151. } else if (!strcmp(argv[1], "ethaddr")) {
  152. s = argv[2];
  153. for (reg = 0; reg < 6; ++reg) {
  154. mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
  155. if (s)
  156. s = (*e) ? e + 1 : e;
  157. }
  158. write_mac(mac);
  159. return 0;
  160. } else if (!strcmp(argv[1], "print")) {
  161. if (chip->cpufreq == -1)
  162. printf("CPU Freq = Not Known\n");
  163. else
  164. printf("CPU Freq = %d MHz\n", chip->cpufreq);
  165. if (chip->dramfreq == -1)
  166. printf("DDR Freq = Not Known\n");
  167. else
  168. printf("DDR Freq = %d MHz\n", chip->dramfreq);
  169. if (chip->dramtype == DDRMOBILE)
  170. printf("DDR Type = MOBILE\n");
  171. else if (chip->dramtype == DDR2)
  172. printf("DDR Type = DDR2\n");
  173. else
  174. printf("DDR Type = Not Known\n");
  175. if (!i2c_read_mac(mac)) {
  176. sprintf(i2c_mac, "%pM", mac);
  177. printf("Ethaddr (from i2c mem) = %s\n", i2c_mac);
  178. } else {
  179. printf("Ethaddr (from i2c mem) = Not set\n");
  180. }
  181. printf("Xloader Rev = %s\n", chip->version);
  182. return 0;
  183. }
  184. cmd_usage(cmdtp);
  185. return 1;
  186. }
  187. U_BOOT_CMD(chip_config, 3, 1, do_chip_config,
  188. "configure chip",
  189. "chip_config cpufreq/ddrfreq frequency\n"
  190. "chip_config print");