sh7757lcr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (C) 2011 Renesas Solutions Corp.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (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., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <asm/processor.h>
  25. #include <asm/io.h>
  26. #include <spi_flash.h>
  27. int checkboard(void)
  28. {
  29. puts("BOARD: R0P7757LC0030RL board\n");
  30. return 0;
  31. }
  32. static void init_gctrl(void)
  33. {
  34. struct gctrl_regs *gctrl = GCTRL_BASE;
  35. unsigned long graofst;
  36. graofst = (SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_GRA_OFFSET) >> 24;
  37. writel(graofst | 0x20000f00, &gctrl->gracr3);
  38. }
  39. static int init_pcie_bridge_from_spi(void *buf, size_t size)
  40. {
  41. struct spi_flash *spi;
  42. int ret;
  43. unsigned long pcie_addr;
  44. spi = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
  45. if (!spi) {
  46. printf("%s: spi_flash probe error.\n", __func__);
  47. return 1;
  48. }
  49. if (is_sh7757_b0())
  50. pcie_addr = SH7757LCR_PCIEBRG_ADDR_B0;
  51. else
  52. pcie_addr = SH7757LCR_PCIEBRG_ADDR;
  53. ret = spi_flash_read(spi, pcie_addr, size, buf);
  54. if (ret) {
  55. printf("%s: spi_flash read error.\n", __func__);
  56. spi_flash_free(spi);
  57. return 1;
  58. }
  59. spi_flash_free(spi);
  60. return 0;
  61. }
  62. static void init_pcie_bridge(void)
  63. {
  64. struct pciebrg_regs *pciebrg = PCIEBRG_BASE;
  65. struct pcie_setup_regs *pcie_setup = PCIE_SETUP_BASE;
  66. int i;
  67. unsigned char *data;
  68. unsigned short tmp;
  69. unsigned long pcie_size;
  70. if (!(readw(&pciebrg->ctrl_h8s) & 0x0001))
  71. return;
  72. if (is_sh7757_b0())
  73. pcie_size = SH7757LCR_PCIEBRG_SIZE_B0;
  74. else
  75. pcie_size = SH7757LCR_PCIEBRG_SIZE;
  76. data = malloc(pcie_size);
  77. if (!data) {
  78. printf("%s: malloc error.\n", __func__);
  79. return;
  80. }
  81. if (init_pcie_bridge_from_spi(data, pcie_size)) {
  82. free(data);
  83. return;
  84. }
  85. if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff &&
  86. data[3] == 0xff) {
  87. free(data);
  88. printf("%s: skipped initialization\n", __func__);
  89. return;
  90. }
  91. writew(0xa501, &pciebrg->ctrl_h8s); /* reset */
  92. writew(0x0000, &pciebrg->cp_ctrl);
  93. writew(0x0000, &pciebrg->cp_addr);
  94. for (i = 0; i < pcie_size; i += 2) {
  95. tmp = (data[i] << 8) | data[i + 1];
  96. writew(tmp, &pciebrg->cp_data);
  97. }
  98. writew(0xa500, &pciebrg->ctrl_h8s); /* start */
  99. if (!is_sh7757_b0())
  100. writel(0x00000001, &pcie_setup->pbictl3);
  101. free(data);
  102. }
  103. static void init_usb_phy(void)
  104. {
  105. struct usb_common_regs *common0 = USB0_COMMON_BASE;
  106. struct usb_common_regs *common1 = USB1_COMMON_BASE;
  107. struct usb0_phy_regs *phy = USB0_PHY_BASE;
  108. struct usb1_port_regs *port = USB1_PORT_BASE;
  109. struct usb1_alignment_regs *align = USB1_ALIGNMENT_BASE;
  110. writew(0x0100, &phy->reset); /* set reset */
  111. /* port0 = USB0, port1 = USB1 */
  112. writew(0x0002, &phy->portsel);
  113. writel(0x0001, &port->port1sel); /* port1 = Host */
  114. writew(0x0111, &phy->reset); /* clear reset */
  115. writew(0x4000, &common0->suspmode);
  116. writew(0x4000, &common1->suspmode);
  117. #if defined(__LITTLE_ENDIAN)
  118. writel(0x00000000, &align->ehcidatac);
  119. writel(0x00000000, &align->ohcidatac);
  120. #endif
  121. }
  122. static void set_mac_to_sh_eth_register(int channel, char *mac_string)
  123. {
  124. struct ether_mac_regs *ether;
  125. unsigned char mac[6];
  126. unsigned long val;
  127. eth_parse_enetaddr(mac_string, mac);
  128. if (!channel)
  129. ether = ETHER0_MAC_BASE;
  130. else
  131. ether = ETHER1_MAC_BASE;
  132. val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
  133. writel(val, &ether->mahr);
  134. val = (mac[4] << 8) | mac[5];
  135. writel(val, &ether->malr);
  136. }
  137. static void set_mac_to_sh_giga_eth_register(int channel, char *mac_string)
  138. {
  139. struct ether_mac_regs *ether;
  140. unsigned char mac[6];
  141. unsigned long val;
  142. eth_parse_enetaddr(mac_string, mac);
  143. if (!channel)
  144. ether = GETHER0_MAC_BASE;
  145. else
  146. ether = GETHER1_MAC_BASE;
  147. val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
  148. writel(val, &ether->mahr);
  149. val = (mac[4] << 8) | mac[5];
  150. writel(val, &ether->malr);
  151. }
  152. /*****************************************************************
  153. * This PMB must be set on this timing. The lowlevel_init is run on
  154. * Area 0(phys 0x00000000), so we have to map it.
  155. *
  156. * The new PMB table is following:
  157. * ent virt phys v sz c wt
  158. * 0 0xa0000000 0x40000000 1 128M 0 1
  159. * 1 0xa8000000 0x48000000 1 128M 0 1
  160. * 2 0xb0000000 0x50000000 1 128M 0 1
  161. * 3 0xb8000000 0x58000000 1 128M 0 1
  162. * 4 0x80000000 0x40000000 1 128M 1 1
  163. * 5 0x88000000 0x48000000 1 128M 1 1
  164. * 6 0x90000000 0x50000000 1 128M 1 1
  165. * 7 0x98000000 0x58000000 1 128M 1 1
  166. */
  167. static void set_pmb_on_board_init(void)
  168. {
  169. struct mmu_regs *mmu = MMU_BASE;
  170. /* clear ITLB */
  171. writel(0x00000004, &mmu->mmucr);
  172. /* delete PMB for SPIBOOT */
  173. writel(0, PMB_ADDR_BASE(0));
  174. writel(0, PMB_DATA_BASE(0));
  175. /* add PMB for SDRAM(0x40000000 - 0x47ffffff) */
  176. /* ppn ub v s1 s0 c wt */
  177. writel(mk_pmb_addr_val(0xa0), PMB_ADDR_BASE(0));
  178. writel(mk_pmb_data_val(0x40, 1, 1, 1, 0, 0, 1), PMB_DATA_BASE(0));
  179. writel(mk_pmb_addr_val(0xb0), PMB_ADDR_BASE(2));
  180. writel(mk_pmb_data_val(0x50, 1, 1, 1, 0, 0, 1), PMB_DATA_BASE(2));
  181. writel(mk_pmb_addr_val(0xb8), PMB_ADDR_BASE(3));
  182. writel(mk_pmb_data_val(0x58, 1, 1, 1, 0, 0, 1), PMB_DATA_BASE(3));
  183. writel(mk_pmb_addr_val(0x80), PMB_ADDR_BASE(4));
  184. writel(mk_pmb_data_val(0x40, 0, 1, 1, 0, 1, 1), PMB_DATA_BASE(4));
  185. writel(mk_pmb_addr_val(0x90), PMB_ADDR_BASE(6));
  186. writel(mk_pmb_data_val(0x50, 0, 1, 1, 0, 1, 1), PMB_DATA_BASE(6));
  187. writel(mk_pmb_addr_val(0x98), PMB_ADDR_BASE(7));
  188. writel(mk_pmb_data_val(0x58, 0, 1, 1, 0, 1, 1), PMB_DATA_BASE(7));
  189. }
  190. int board_init(void)
  191. {
  192. struct gether_control_regs *gether = GETHER_CONTROL_BASE;
  193. set_pmb_on_board_init();
  194. /* enable RMII's MDIO (disable GRMII's MDIO) */
  195. writel(0x00030000, &gether->gbecont);
  196. init_gctrl();
  197. init_usb_phy();
  198. return 0;
  199. }
  200. int dram_init(void)
  201. {
  202. DECLARE_GLOBAL_DATA_PTR;
  203. gd->bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
  204. gd->bd->bi_memsize = CONFIG_SYS_SDRAM_SIZE;
  205. printf("DRAM: %dMB\n", CONFIG_SYS_SDRAM_SIZE / (1024 * 1024));
  206. printf(" Physical address\n");
  207. printf(" 0x%08x - 0x%08x : Accessible Space as ECC Area\n",
  208. SH7757LCR_SDRAM_PHYS_TOP,
  209. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_SDRAM_SIZE - 1);
  210. printf(" 0x%08x - 0x%08x : No Access Area\n",
  211. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_SDRAM_SIZE,
  212. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_SDRAM_SIZE * 2 - 1);
  213. printf(" 0x%08x - 0x%08x : Non-ECC Area for DVC/AVC\n",
  214. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_SDRAM_ECC_SETTING * 2,
  215. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_SDRAM_ECC_SETTING * 2 +
  216. SH7757LCR_SDRAM_DVC_SIZE - 1);
  217. printf(" 0x%08x - 0x%08x : Non-ECC Area for G200eR2\n",
  218. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_GRA_OFFSET,
  219. SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_GRA_OFFSET + 0x00ffffff);
  220. return 0;
  221. }
  222. static int get_sh_eth_mac_raw(unsigned char *buf, int size)
  223. {
  224. struct spi_flash *spi;
  225. int ret;
  226. spi = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
  227. if (spi == NULL) {
  228. printf("%s: spi_flash probe error.\n", __func__);
  229. return 1;
  230. }
  231. ret = spi_flash_read(spi, SH7757LCR_ETHERNET_MAC_BASE, size, buf);
  232. if (ret) {
  233. printf("%s: spi_flash read error.\n", __func__);
  234. spi_flash_free(spi);
  235. return 1;
  236. }
  237. spi_flash_free(spi);
  238. return 0;
  239. }
  240. static int get_sh_eth_mac(int channel, char *mac_string, unsigned char *buf)
  241. {
  242. memcpy(mac_string, &buf[channel * (SH7757LCR_ETHERNET_MAC_SIZE + 1)],
  243. SH7757LCR_ETHERNET_MAC_SIZE);
  244. mac_string[SH7757LCR_ETHERNET_MAC_SIZE] = 0x00; /* terminate */
  245. return 0;
  246. }
  247. static void init_ethernet_mac(void)
  248. {
  249. char mac_string[64];
  250. char env_string[64];
  251. int i;
  252. unsigned char *buf;
  253. buf = malloc(256);
  254. if (!buf) {
  255. printf("%s: malloc error.\n", __func__);
  256. return;
  257. }
  258. get_sh_eth_mac_raw(buf, 256);
  259. /* Fast Ethernet */
  260. for (i = 0; i < SH7757LCR_ETHERNET_NUM_CH; i++) {
  261. get_sh_eth_mac(i, mac_string, buf);
  262. if (i == 0)
  263. setenv("ethaddr", mac_string);
  264. else {
  265. sprintf(env_string, "eth%daddr", i);
  266. setenv(env_string, mac_string);
  267. }
  268. set_mac_to_sh_eth_register(i, mac_string);
  269. }
  270. /* Gigabit Ethernet */
  271. for (i = 0; i < SH7757LCR_GIGA_ETHERNET_NUM_CH; i++) {
  272. get_sh_eth_mac(i + SH7757LCR_ETHERNET_NUM_CH, mac_string, buf);
  273. sprintf(env_string, "eth%daddr", i + SH7757LCR_ETHERNET_NUM_CH);
  274. setenv(env_string, mac_string);
  275. set_mac_to_sh_giga_eth_register(i, mac_string);
  276. }
  277. free(buf);
  278. }
  279. static void init_pcie(void)
  280. {
  281. struct pcie_setup_regs *pcie_setup = PCIE_SETUP_BASE;
  282. struct pcie_system_bus_regs *pcie_sysbus = PCIE_SYSTEM_BUS_BASE;
  283. writel(0x00000ff2, &pcie_setup->ladmsk0);
  284. writel(0x00000001, &pcie_setup->barmap);
  285. writel(0xffcaa000, &pcie_setup->lad0);
  286. writel(0x00030000, &pcie_sysbus->endictl0);
  287. writel(0x00000003, &pcie_sysbus->endictl1);
  288. writel(0x00000004, &pcie_setup->pbictl2);
  289. }
  290. static void finish_spiboot(void)
  291. {
  292. struct gctrl_regs *gctrl = GCTRL_BASE;
  293. /*
  294. * SH7757 B0 does not use LBSC.
  295. * So if we set SPIBOOTCAN to 1, SH7757 can not access Area0.
  296. * This setting is not cleared by manual reset, So we have to set it
  297. * to 0.
  298. */
  299. writel(0x00000000, &gctrl->spibootcan);
  300. }
  301. int board_late_init(void)
  302. {
  303. init_ethernet_mac();
  304. init_pcie_bridge();
  305. init_pcie();
  306. finish_spiboot();
  307. return 0;
  308. }
  309. int do_sh_g200(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  310. {
  311. struct gctrl_regs *gctrl = GCTRL_BASE;
  312. unsigned long graofst;
  313. writel(0xfedcba98, &gctrl->wprotect);
  314. graofst = (SH7757LCR_SDRAM_PHYS_TOP + SH7757LCR_GRA_OFFSET) >> 24;
  315. writel(graofst | 0xa0000f00, &gctrl->gracr3);
  316. return 0;
  317. }
  318. U_BOOT_CMD(
  319. sh_g200, 1, 1, do_sh_g200,
  320. "enable sh-g200",
  321. "enable SH-G200 bus (disable PCIe-G200)"
  322. );
  323. int do_write_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  324. {
  325. int i, ret;
  326. char mac_string[256];
  327. struct spi_flash *spi;
  328. unsigned char *buf;
  329. if (argc != 5) {
  330. buf = malloc(256);
  331. if (!buf) {
  332. printf("%s: malloc error.\n", __func__);
  333. return 1;
  334. }
  335. get_sh_eth_mac_raw(buf, 256);
  336. /* print current MAC address */
  337. for (i = 0; i < 4; i++) {
  338. get_sh_eth_mac(i, mac_string, buf);
  339. if (i < 2)
  340. printf(" ETHERC ch%d = %s\n", i, mac_string);
  341. else
  342. printf("GETHERC ch%d = %s\n", i-2, mac_string);
  343. }
  344. free(buf);
  345. return 0;
  346. }
  347. /* new setting */
  348. memset(mac_string, 0xff, sizeof(mac_string));
  349. sprintf(mac_string, "%s\t%s\t%s\t%s",
  350. argv[1], argv[2], argv[3], argv[4]);
  351. /* write MAC data to SPI rom */
  352. spi = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
  353. if (!spi) {
  354. printf("%s: spi_flash probe error.\n", __func__);
  355. return 1;
  356. }
  357. ret = spi_flash_erase(spi, SH7757LCR_ETHERNET_MAC_BASE_SPI,
  358. SH7757LCR_SPI_SECTOR_SIZE);
  359. if (ret) {
  360. printf("%s: spi_flash erase error.\n", __func__);
  361. return 1;
  362. }
  363. ret = spi_flash_write(spi, SH7757LCR_ETHERNET_MAC_BASE_SPI,
  364. sizeof(mac_string), mac_string);
  365. if (ret) {
  366. printf("%s: spi_flash write error.\n", __func__);
  367. spi_flash_free(spi);
  368. return 1;
  369. }
  370. spi_flash_free(spi);
  371. puts("The writing of the MAC address to SPI ROM was completed.\n");
  372. return 0;
  373. }
  374. U_BOOT_CMD(
  375. write_mac, 5, 1, do_write_mac,
  376. "write MAC address for ETHERC/GETHERC",
  377. "[ETHERC ch0] [ETHERC ch1] [GETHERC ch0] [GETHERC ch1]\n"
  378. );