spr_misc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_emi.h>
  30. #include <asm/arch/spr_xloader_table.h>
  31. #include <asm/arch/spr_defs.h>
  32. #define CPU 0
  33. #define DDR 1
  34. #define SRAM_REL 0xD2801000
  35. DECLARE_GLOBAL_DATA_PTR;
  36. static struct chip_data chip_data;
  37. int dram_init(void)
  38. {
  39. struct xloader_table *xloader_tb =
  40. (struct xloader_table *)XLOADER_TABLE_ADDRESS;
  41. struct xloader_table_1_1 *table_1_1;
  42. struct xloader_table_1_2 *table_1_2;
  43. struct chip_data *chip = &chip_data;
  44. gd->ram_size = get_ram_size(PHYS_SDRAM_1, PHYS_SDRAM_1_MAXSIZE);
  45. if (XLOADER_TABLE_VERSION_1_1 == xloader_tb->table_version) {
  46. table_1_1 = &xloader_tb->table.table_1_1;
  47. chip->dramfreq = table_1_1->ddrfreq;
  48. chip->dramtype = table_1_1->ddrtype;
  49. } else if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
  50. table_1_2 = &xloader_tb->table.table_1_2;
  51. chip->dramfreq = table_1_2->ddrfreq;
  52. chip->dramtype = table_1_2->ddrtype;
  53. } else {
  54. chip->dramfreq = -1;
  55. }
  56. return 0;
  57. }
  58. void dram_init_banksize(void)
  59. {
  60. gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
  61. gd->bd->bi_dram[0].size = gd->ram_size;
  62. }
  63. int misc_init_r(void)
  64. {
  65. #if defined(CONFIG_CMD_NET)
  66. uchar mac_id[6];
  67. if (!eth_getenv_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
  68. eth_setenv_enetaddr("ethaddr", mac_id);
  69. #endif
  70. setenv("verify", "n");
  71. #if defined(CONFIG_SPEAR_USBTTY)
  72. setenv("stdin", "usbtty");
  73. setenv("stdout", "usbtty");
  74. setenv("stderr", "usbtty");
  75. #endif
  76. return 0;
  77. }
  78. #ifdef CONFIG_SPEAR_EMI
  79. struct cust_emi_para {
  80. unsigned int tap;
  81. unsigned int tsdp;
  82. unsigned int tdpw;
  83. unsigned int tdpr;
  84. unsigned int tdcs;
  85. };
  86. /* EMI timing setting of m28w640hc of linux kernel */
  87. const struct cust_emi_para emi_timing_m28w640hc = {
  88. .tap = 0x10,
  89. .tsdp = 0x05,
  90. .tdpw = 0x0a,
  91. .tdpr = 0x0a,
  92. .tdcs = 0x05,
  93. };
  94. /* EMI timing setting of bootrom */
  95. const struct cust_emi_para emi_timing_bootrom = {
  96. .tap = 0xf,
  97. .tsdp = 0x0,
  98. .tdpw = 0xff,
  99. .tdpr = 0x111,
  100. .tdcs = 0x02,
  101. };
  102. void spear_emi_init(void)
  103. {
  104. const struct cust_emi_para *p = &emi_timing_m28w640hc;
  105. struct emi_regs *emi_regs_p = (struct emi_regs *)CONFIG_SPEAR_EMIBASE;
  106. unsigned int cs;
  107. unsigned int val, tmp;
  108. val = readl(CONFIG_SPEAR_RASBASE);
  109. if (val & EMI_ACKMSK)
  110. tmp = 0x3f;
  111. else
  112. tmp = 0x0;
  113. writel(tmp, &emi_regs_p->ack);
  114. for (cs = 0; cs < CONFIG_SYS_MAX_FLASH_BANKS; cs++) {
  115. writel(p->tap, &emi_regs_p->bank_regs[cs].tap);
  116. writel(p->tsdp, &emi_regs_p->bank_regs[cs].tsdp);
  117. writel(p->tdpw, &emi_regs_p->bank_regs[cs].tdpw);
  118. writel(p->tdpr, &emi_regs_p->bank_regs[cs].tdpr);
  119. writel(p->tdcs, &emi_regs_p->bank_regs[cs].tdcs);
  120. writel(EMI_CNTL_ENBBYTERW | ((val & 0x18) >> 3),
  121. &emi_regs_p->bank_regs[cs].control);
  122. }
  123. }
  124. #endif
  125. int spear_board_init(ulong mach_type)
  126. {
  127. struct xloader_table *xloader_tb =
  128. (struct xloader_table *)XLOADER_TABLE_ADDRESS;
  129. struct xloader_table_1_2 *table_1_2;
  130. struct chip_data *chip = &chip_data;
  131. gd->bd->bi_arch_number = mach_type;
  132. /* adress of boot parameters */
  133. gd->bd->bi_boot_params = CONFIG_BOOT_PARAMS_ADDR;
  134. /* CPU is initialized to work at 333MHz in Xloader */
  135. chip->cpufreq = 333;
  136. if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
  137. table_1_2 = &xloader_tb->table.table_1_2;
  138. memcpy(chip->version, table_1_2->version,
  139. sizeof(chip->version));
  140. }
  141. #ifdef CONFIG_SPEAR_EMI
  142. spear_emi_init();
  143. #endif
  144. return 0;
  145. }
  146. static int i2c_read_mac(uchar *buffer)
  147. {
  148. u8 buf[2];
  149. i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  150. /* Check if mac in i2c memory is valid */
  151. if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  152. /* Valid mac address is saved in i2c eeprom */
  153. i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
  154. return 0;
  155. }
  156. return -1;
  157. }
  158. static int write_mac(uchar *mac)
  159. {
  160. u8 buf[2];
  161. buf[0] = (u8)MAGIC_BYTE0;
  162. buf[1] = (u8)MAGIC_BYTE1;
  163. i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  164. buf[0] = (u8)~MAGIC_BYTE0;
  165. buf[1] = (u8)~MAGIC_BYTE1;
  166. i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  167. /* check if valid MAC address is saved in I2C EEPROM or not? */
  168. if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  169. i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
  170. puts("I2C EEPROM written with mac address \n");
  171. return 0;
  172. }
  173. puts("I2C EEPROM writing failed \n");
  174. return -1;
  175. }
  176. int do_chip_config(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  177. {
  178. void (*sram_setfreq) (unsigned int, unsigned int);
  179. struct chip_data *chip = &chip_data;
  180. unsigned char mac[6];
  181. unsigned int reg, frequency;
  182. char *s, *e;
  183. char i2c_mac[20];
  184. if ((argc > 3) || (argc < 2))
  185. return cmd_usage(cmdtp);
  186. if ((!strcmp(argv[1], "cpufreq")) || (!strcmp(argv[1], "ddrfreq"))) {
  187. frequency = simple_strtoul(argv[2], NULL, 0);
  188. if (frequency > 333) {
  189. printf("Frequency is limited to 333MHz\n");
  190. return 1;
  191. }
  192. sram_setfreq = memcpy((void *)SRAM_REL, setfreq, setfreq_sz);
  193. if (!strcmp(argv[1], "cpufreq")) {
  194. sram_setfreq(CPU, frequency);
  195. printf("CPU frequency changed to %u\n", frequency);
  196. chip->cpufreq = frequency;
  197. } else {
  198. sram_setfreq(DDR, frequency);
  199. printf("DDR frequency changed to %u\n", frequency);
  200. chip->dramfreq = frequency;
  201. }
  202. return 0;
  203. } else if (!strcmp(argv[1], "ethaddr")) {
  204. s = argv[2];
  205. for (reg = 0; reg < 6; ++reg) {
  206. mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
  207. if (s)
  208. s = (*e) ? e + 1 : e;
  209. }
  210. write_mac(mac);
  211. return 0;
  212. } else if (!strcmp(argv[1], "print")) {
  213. if (chip->cpufreq == -1)
  214. printf("CPU Freq = Not Known\n");
  215. else
  216. printf("CPU Freq = %d MHz\n", chip->cpufreq);
  217. if (chip->dramfreq == -1)
  218. printf("DDR Freq = Not Known\n");
  219. else
  220. printf("DDR Freq = %d MHz\n", chip->dramfreq);
  221. if (chip->dramtype == DDRMOBILE)
  222. printf("DDR Type = MOBILE\n");
  223. else if (chip->dramtype == DDR2)
  224. printf("DDR Type = DDR2\n");
  225. else
  226. printf("DDR Type = Not Known\n");
  227. if (!i2c_read_mac(mac)) {
  228. sprintf(i2c_mac, "%pM", mac);
  229. printf("Ethaddr (from i2c mem) = %s\n", i2c_mac);
  230. } else {
  231. printf("Ethaddr (from i2c mem) = Not set\n");
  232. }
  233. printf("Xloader Rev = %s\n", chip->version);
  234. return 0;
  235. }
  236. return cmd_usage(cmdtp);
  237. }
  238. U_BOOT_CMD(chip_config, 3, 1, do_chip_config,
  239. "configure chip",
  240. "chip_config cpufreq/ddrfreq frequency\n"
  241. "chip_config print");