sh7757lcr.c 9.7 KB

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