km_arm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * (C) Copyright 2009
  7. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  8. *
  9. * (C) Copyright 2010
  10. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  28. * MA 02110-1301 USA
  29. */
  30. #include <common.h>
  31. #include <i2c.h>
  32. #include <nand.h>
  33. #include <netdev.h>
  34. #include <miiphy.h>
  35. #include <asm/io.h>
  36. #include <asm/arch/kirkwood.h>
  37. #include <asm/arch/mpp.h>
  38. #include "../common/common.h"
  39. DECLARE_GLOBAL_DATA_PTR;
  40. /*
  41. * BOCO FPGA definitions
  42. */
  43. #define BOCO 0x10
  44. #define REG_CTRL_H 0x02
  45. #define MASK_WRL_UNITRUN 0x01
  46. #define MASK_RBX_PGY_PRESENT 0x40
  47. #define REG_IRQ_CIRQ2 0x2d
  48. #define MASK_RBI_DEFECT_16 0x01
  49. /* Multi-Purpose Pins Functionality configuration */
  50. u32 kwmpp_config[] = {
  51. MPP0_NF_IO2,
  52. MPP1_NF_IO3,
  53. MPP2_NF_IO4,
  54. MPP3_NF_IO5,
  55. MPP4_NF_IO6,
  56. MPP5_NF_IO7,
  57. MPP6_SYSRST_OUTn,
  58. MPP7_PEX_RST_OUTn,
  59. #if defined(CONFIG_SOFT_I2C)
  60. MPP8_GPIO, /* SDA */
  61. MPP9_GPIO, /* SCL */
  62. #endif
  63. #if defined(CONFIG_HARD_I2C)
  64. MPP8_TW_SDA,
  65. MPP9_TW_SCK,
  66. #endif
  67. MPP10_UART0_TXD,
  68. MPP11_UART0_RXD,
  69. MPP12_GPO, /* Reserved */
  70. MPP13_UART1_TXD,
  71. MPP14_UART1_RXD,
  72. MPP15_GPIO, /* Not used */
  73. MPP16_GPIO, /* Not used */
  74. MPP17_GPIO, /* Reserved */
  75. MPP18_NF_IO0,
  76. MPP19_NF_IO1,
  77. MPP20_GPIO,
  78. MPP21_GPIO,
  79. MPP22_GPIO,
  80. MPP23_GPIO,
  81. MPP24_GPIO,
  82. MPP25_GPIO,
  83. MPP26_GPIO,
  84. MPP27_GPIO,
  85. MPP28_GPIO,
  86. MPP29_GPIO,
  87. MPP30_GPIO,
  88. MPP31_GPIO,
  89. MPP32_GPIO,
  90. MPP33_GPIO,
  91. MPP34_GPIO, /* CDL1 (input) */
  92. MPP35_GPIO, /* CDL2 (input) */
  93. MPP36_GPIO, /* MAIN_IRQ (input) */
  94. MPP37_GPIO, /* BOARD_LED */
  95. MPP38_GPIO, /* Piggy3 LED[1] */
  96. MPP39_GPIO, /* Piggy3 LED[2] */
  97. MPP40_GPIO, /* Piggy3 LED[3] */
  98. MPP41_GPIO, /* Piggy3 LED[4] */
  99. MPP42_GPIO, /* Piggy3 LED[5] */
  100. MPP43_GPIO, /* Piggy3 LED[6] */
  101. MPP44_GPIO, /* Piggy3 LED[7], BIST_EN_L */
  102. MPP45_GPIO, /* Piggy3 LED[8] */
  103. MPP46_GPIO, /* Reserved */
  104. MPP47_GPIO, /* Reserved */
  105. MPP48_GPIO, /* Reserved */
  106. MPP49_GPIO, /* SW_INTOUTn */
  107. 0
  108. };
  109. #if defined(CONFIG_MGCOGE3UN)
  110. /*
  111. * Wait for startup OK from mgcoge3ne
  112. */
  113. int startup_allowed(void)
  114. {
  115. unsigned char buf;
  116. /*
  117. * Read CIRQ16 bit (bit 0)
  118. */
  119. if (i2c_read(BOCO, REG_IRQ_CIRQ2, 1, &buf, 1) != 0)
  120. printf("%s: Error reading Boco\n", __func__);
  121. else
  122. if ((buf & MASK_RBI_DEFECT_16) == MASK_RBI_DEFECT_16)
  123. return 1;
  124. return 0;
  125. }
  126. #endif
  127. #if (defined(CONFIG_MGCOGE3UN)|defined(CONFIG_PORTL2))
  128. /*
  129. * These two boards have always ethernet present. Its connected to the mv
  130. * switch.
  131. */
  132. int ethernet_present(void)
  133. {
  134. return 1;
  135. }
  136. #else
  137. int ethernet_present(void)
  138. {
  139. uchar buf;
  140. int ret = 0;
  141. if (i2c_read(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
  142. printf("%s: Error reading Boco\n", __func__);
  143. return -1;
  144. }
  145. if ((buf & MASK_RBX_PGY_PRESENT) == MASK_RBX_PGY_PRESENT)
  146. ret = 1;
  147. return ret;
  148. }
  149. #endif
  150. int initialize_unit_leds(void)
  151. {
  152. /*
  153. * Init the unit LEDs per default they all are
  154. * ok apart from bootstat
  155. */
  156. uchar buf;
  157. if (i2c_read(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
  158. printf("%s: Error reading Boco\n", __func__);
  159. return -1;
  160. }
  161. buf |= MASK_WRL_UNITRUN;
  162. if (i2c_write(BOCO, REG_CTRL_H, 1, &buf, 1) != 0) {
  163. printf("%s: Error writing Boco\n", __func__);
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. #if defined(CONFIG_BOOTCOUNT_LIMIT)
  169. void set_bootcount_addr(void)
  170. {
  171. uchar buf[32];
  172. unsigned int bootcountaddr;
  173. bootcountaddr = gd->ram_size - BOOTCOUNT_ADDR;
  174. sprintf((char *)buf, "0x%x", bootcountaddr);
  175. setenv("bootcountaddr", (char *)buf);
  176. }
  177. #endif
  178. int misc_init_r(void)
  179. {
  180. char *str;
  181. int mach_type;
  182. str = getenv("mach_type");
  183. if (str != NULL) {
  184. mach_type = simple_strtoul(str, NULL, 10);
  185. printf("Overwriting MACH_TYPE with %d!!!\n", mach_type);
  186. gd->bd->bi_arch_number = mach_type;
  187. }
  188. #if defined(CONFIG_MGCOGE3UN)
  189. char *wait_for_ne;
  190. wait_for_ne = getenv("waitforne");
  191. if (wait_for_ne != NULL) {
  192. if (strcmp(wait_for_ne, "true") == 0) {
  193. int cnt = 0;
  194. puts("NE go: ");
  195. while (startup_allowed() == 0) {
  196. udelay(200000);
  197. cnt++;
  198. if (cnt == 5)
  199. puts("wait\b\b\b\b");
  200. if (cnt == 10) {
  201. cnt = 0;
  202. puts(" \b\b\b\b");
  203. }
  204. }
  205. puts("OK\n");
  206. }
  207. }
  208. #endif
  209. initialize_unit_leds();
  210. set_km_env();
  211. #if defined(CONFIG_BOOTCOUNT_LIMIT)
  212. set_bootcount_addr();
  213. #endif
  214. return 0;
  215. }
  216. int board_early_init_f(void)
  217. {
  218. u32 tmp;
  219. kirkwood_mpp_conf(kwmpp_config);
  220. /*
  221. * The FLASH_GPIO_PIN switches between using a
  222. * NAND or a SPI FLASH. Set this pin on start
  223. * to NAND mode.
  224. */
  225. tmp = readl(KW_GPIO0_BASE);
  226. writel(tmp | FLASH_GPIO_PIN , KW_GPIO0_BASE);
  227. tmp = readl(KW_GPIO0_BASE + 4);
  228. writel(tmp & (~FLASH_GPIO_PIN) , KW_GPIO0_BASE + 4);
  229. #if defined(CONFIG_SOFT_I2C)
  230. /* init the GPIO for I2C Bitbang driver */
  231. kw_gpio_set_valid(KM_KIRKWOOD_SDA_PIN, 1);
  232. kw_gpio_set_valid(KM_KIRKWOOD_SCL_PIN, 1);
  233. kw_gpio_direction_output(KM_KIRKWOOD_SDA_PIN, 0);
  234. kw_gpio_direction_output(KM_KIRKWOOD_SCL_PIN, 0);
  235. #endif
  236. #if defined(CONFIG_SYS_EEPROM_WREN)
  237. kw_gpio_set_valid(KM_KIRKWOOD_ENV_WP, 38);
  238. kw_gpio_direction_output(KM_KIRKWOOD_ENV_WP, 1);
  239. #endif
  240. return 0;
  241. }
  242. int board_init(void)
  243. {
  244. /*
  245. * arch number of board
  246. */
  247. gd->bd->bi_arch_number = MACH_TYPE_KM_KIRKWOOD;
  248. /* address of boot parameters */
  249. gd->bd->bi_boot_params = kw_sdram_bar(0) + 0x100;
  250. return 0;
  251. }
  252. #if defined(CONFIG_CMD_SF)
  253. int do_spi_toggle(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  254. {
  255. u32 tmp;
  256. if (argc < 2)
  257. return cmd_usage(cmdtp);
  258. if ((strcmp(argv[1], "off") == 0)) {
  259. printf("SPI FLASH disabled, NAND enabled\n");
  260. /* Multi-Purpose Pins Functionality configuration */
  261. kwmpp_config[0] = MPP0_NF_IO2;
  262. kwmpp_config[1] = MPP1_NF_IO3;
  263. kwmpp_config[2] = MPP2_NF_IO4;
  264. kwmpp_config[3] = MPP3_NF_IO5;
  265. kirkwood_mpp_conf(kwmpp_config);
  266. tmp = readl(KW_GPIO0_BASE);
  267. writel(tmp | FLASH_GPIO_PIN , KW_GPIO0_BASE);
  268. } else if ((strcmp(argv[1], "on") == 0)) {
  269. printf("SPI FLASH enabled, NAND disabled\n");
  270. /* Multi-Purpose Pins Functionality configuration */
  271. kwmpp_config[0] = MPP0_SPI_SCn;
  272. kwmpp_config[1] = MPP1_SPI_MOSI;
  273. kwmpp_config[2] = MPP2_SPI_SCK;
  274. kwmpp_config[3] = MPP3_SPI_MISO;
  275. kirkwood_mpp_conf(kwmpp_config);
  276. tmp = readl(KW_GPIO0_BASE);
  277. writel(tmp & (~FLASH_GPIO_PIN) , KW_GPIO0_BASE);
  278. } else {
  279. return cmd_usage(cmdtp);
  280. }
  281. return 0;
  282. }
  283. U_BOOT_CMD(
  284. spitoggle, 2, 0, do_spi_toggle,
  285. "En-/disable SPI FLASH access",
  286. "<on|off> - Enable (on) or disable (off) SPI FLASH access\n"
  287. );
  288. #endif
  289. int dram_init(void)
  290. {
  291. /* dram_init must store complete ramsize in gd->ram_size */
  292. /* Fix this */
  293. gd->ram_size = get_ram_size((volatile void *)kw_sdram_bar(0),
  294. kw_sdram_bs(0));
  295. return 0;
  296. }
  297. void dram_init_banksize(void)
  298. {
  299. int i;
  300. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  301. gd->bd->bi_dram[i].start = kw_sdram_bar(i);
  302. gd->bd->bi_dram[i].size = get_ram_size((long *)kw_sdram_bar(i),
  303. kw_sdram_bs(i));
  304. }
  305. }
  306. #if (defined(CONFIG_MGCOGE3UN)|defined(CONFIG_PORTL2))
  307. #define PHY_LED_SEL 0x18
  308. #define PHY_LED0_LINK (0x5)
  309. #define PHY_LED1_ACT (0x8<<4)
  310. #define PHY_LED2_INT (0xe<<8)
  311. #define PHY_SPEC_CTRL 0x1c
  312. #define PHY_RGMII_CLK_STABLE (0x1<<10)
  313. #define PHY_CLSA (0x1<<1)
  314. /* Configure and enable MV88E3018 PHY */
  315. void reset_phy(void)
  316. {
  317. char *name = "egiga0";
  318. unsigned short reg;
  319. if (miiphy_set_current_dev(name))
  320. return;
  321. /* RGMII clk transition on data stable */
  322. if (miiphy_read(name, CONFIG_PHY_BASE_ADR, PHY_SPEC_CTRL, &reg) != 0)
  323. printf("Error reading PHY spec ctrl reg\n");
  324. if (miiphy_write(name, CONFIG_PHY_BASE_ADR, PHY_SPEC_CTRL,
  325. reg | PHY_RGMII_CLK_STABLE | PHY_CLSA) != 0)
  326. printf("Error writing PHY spec ctrl reg\n");
  327. /* leds setup */
  328. if (miiphy_write(name, CONFIG_PHY_BASE_ADR, PHY_LED_SEL,
  329. PHY_LED0_LINK | PHY_LED1_ACT | PHY_LED2_INT) != 0)
  330. printf("Error writing PHY LED reg\n");
  331. /* reset the phy */
  332. miiphy_reset(name, CONFIG_PHY_BASE_ADR);
  333. }
  334. #else
  335. /* Configure and enable MV88E1118 PHY on the piggy*/
  336. void reset_phy(void)
  337. {
  338. char *name = "egiga0";
  339. if (miiphy_set_current_dev(name))
  340. return;
  341. /* reset the phy */
  342. miiphy_reset(name, CONFIG_PHY_BASE_ADR);
  343. }
  344. #endif
  345. #if defined(CONFIG_HUSH_INIT_VAR)
  346. int hush_init_var(void)
  347. {
  348. ivm_read_eeprom();
  349. return 0;
  350. }
  351. #endif
  352. #if defined(CONFIG_BOOTCOUNT_LIMIT)
  353. void bootcount_store(ulong a)
  354. {
  355. volatile ulong *save_addr;
  356. volatile ulong size = 0;
  357. int i;
  358. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  359. size += gd->bd->bi_dram[i].size;
  360. }
  361. save_addr = (ulong*)(size - BOOTCOUNT_ADDR);
  362. writel(a, save_addr);
  363. writel(BOOTCOUNT_MAGIC, &save_addr[1]);
  364. }
  365. ulong bootcount_load(void)
  366. {
  367. volatile ulong *save_addr;
  368. volatile ulong size = 0;
  369. int i;
  370. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  371. size += gd->bd->bi_dram[i].size;
  372. }
  373. save_addr = (ulong*)(size - BOOTCOUNT_ADDR);
  374. if (readl(&save_addr[1]) != BOOTCOUNT_MAGIC)
  375. return 0;
  376. else
  377. return readl(save_addr);
  378. }
  379. #endif
  380. #if defined(CONFIG_SOFT_I2C)
  381. void set_sda(int state)
  382. {
  383. I2C_ACTIVE;
  384. I2C_SDA(state);
  385. }
  386. void set_scl(int state)
  387. {
  388. I2C_SCL(state);
  389. }
  390. int get_sda(void)
  391. {
  392. I2C_TRISTATE;
  393. return I2C_READ;
  394. }
  395. int get_scl(void)
  396. {
  397. return kw_gpio_get_value(KM_KIRKWOOD_SCL_PIN) ? 1 : 0;
  398. }
  399. #endif
  400. #if defined(CONFIG_SYS_EEPROM_WREN)
  401. int eeprom_write_enable(unsigned dev_addr, int state)
  402. {
  403. kw_gpio_set_value(KM_KIRKWOOD_ENV_WP, !state);
  404. return !kw_gpio_get_value(KM_KIRKWOOD_ENV_WP);
  405. }
  406. #endif