taurus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Board functions for Siemens TAURUS (AT91SAM9G20) based boards
  3. * (C) Copyright Siemens AG
  4. *
  5. * Based on:
  6. * U-Boot file: board/atmel/at91sam9260ek/at91sam9260ek.c
  7. *
  8. * (C) Copyright 2007-2008
  9. * Stelian Pop <stelian@popies.net>
  10. * Lead Tech Design <www.leadtechdesign.com>
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <command.h>
  15. #include <common.h>
  16. #include <asm/io.h>
  17. #include <asm/arch/at91sam9260_matrix.h>
  18. #include <asm/arch/at91sam9_smc.h>
  19. #include <asm/arch/at91_common.h>
  20. #include <asm/arch/at91_pmc.h>
  21. #include <asm/arch/at91_rstc.h>
  22. #include <asm/arch/gpio.h>
  23. #include <asm/arch/at91sam9_sdramc.h>
  24. #include <asm/arch/clk.h>
  25. #include <linux/mtd/nand.h>
  26. #include <atmel_mci.h>
  27. #include <asm/arch/at91_spi.h>
  28. #include <spi.h>
  29. #include <net.h>
  30. #include <netdev.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. static void taurus_nand_hw_init(void)
  33. {
  34. struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
  35. struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
  36. unsigned long csa;
  37. /* Assign CS3 to NAND/SmartMedia Interface */
  38. csa = readl(&matrix->ebicsa);
  39. csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
  40. writel(csa, &matrix->ebicsa);
  41. /* Configure SMC CS3 for NAND/SmartMedia */
  42. writel(AT91_SMC_SETUP_NWE(2) | AT91_SMC_SETUP_NCS_WR(0) |
  43. AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(0),
  44. &smc->cs[3].setup);
  45. writel(AT91_SMC_PULSE_NWE(4) | AT91_SMC_PULSE_NCS_WR(3) |
  46. AT91_SMC_PULSE_NRD(4) | AT91_SMC_PULSE_NCS_RD(3),
  47. &smc->cs[3].pulse);
  48. writel(AT91_SMC_CYCLE_NWE(7) | AT91_SMC_CYCLE_NRD(7),
  49. &smc->cs[3].cycle);
  50. writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
  51. AT91_SMC_MODE_EXNW_DISABLE |
  52. AT91_SMC_MODE_DBW_8 |
  53. AT91_SMC_MODE_TDF_CYCLE(3),
  54. &smc->cs[3].mode);
  55. /* Configure RDY/BSY */
  56. at91_set_gpio_input(CONFIG_SYS_NAND_READY_PIN, 1);
  57. /* Enable NandFlash */
  58. at91_set_gpio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
  59. }
  60. #if defined(CONFIG_SPL_BUILD)
  61. #include <spl.h>
  62. #include <nand.h>
  63. #include <spi_flash.h>
  64. void matrix_init(void)
  65. {
  66. struct at91_matrix *mat = (struct at91_matrix *)ATMEL_BASE_MATRIX;
  67. writel((readl(&mat->scfg[3]) & (~AT91_MATRIX_SLOT_CYCLE))
  68. | AT91_MATRIX_SLOT_CYCLE_(0x40),
  69. &mat->scfg[3]);
  70. }
  71. #if defined(CONFIG_BOARD_AXM)
  72. static int at91_is_recovery(void)
  73. {
  74. if ((at91_get_gpio_value(AT91_PIN_PA26) == 0) &&
  75. (at91_get_gpio_value(AT91_PIN_PA27) == 0))
  76. return 1;
  77. return 0;
  78. }
  79. #elif defined(CONFIG_BOARD_TAURUS)
  80. static int at91_is_recovery(void)
  81. {
  82. if (at91_get_gpio_value(AT91_PIN_PA31) == 0)
  83. return 1;
  84. return 0;
  85. }
  86. #endif
  87. void spl_board_init(void)
  88. {
  89. taurus_nand_hw_init();
  90. at91_spi0_hw_init(TAURUS_SPI_MASK);
  91. #if defined(CONFIG_BOARD_AXM)
  92. /* Configure LED PINs */
  93. at91_set_gpio_output(AT91_PIN_PA6, 0);
  94. at91_set_gpio_output(AT91_PIN_PA8, 0);
  95. at91_set_gpio_output(AT91_PIN_PA9, 0);
  96. at91_set_gpio_output(AT91_PIN_PA10, 0);
  97. at91_set_gpio_output(AT91_PIN_PA11, 0);
  98. at91_set_gpio_output(AT91_PIN_PA12, 0);
  99. /* Configure recovery button PINs */
  100. at91_set_gpio_input(AT91_PIN_PA26, 1);
  101. at91_set_gpio_input(AT91_PIN_PA27, 1);
  102. #elif defined(CONFIG_BOARD_TAURUS)
  103. at91_set_gpio_input(AT91_PIN_PA31, 1);
  104. #endif
  105. /* check for recovery mode */
  106. if (at91_is_recovery() == 1) {
  107. struct spi_flash *flash;
  108. puts("Recovery button pressed\n");
  109. nand_init();
  110. spl_nand_erase_one(0, 0);
  111. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  112. 0,
  113. CONFIG_SF_DEFAULT_SPEED,
  114. CONFIG_SF_DEFAULT_MODE);
  115. if (!flash) {
  116. puts("no flash\n");
  117. } else {
  118. puts("erase spi flash sector 0\n");
  119. spi_flash_erase(flash, 0,
  120. CONFIG_SYS_NAND_U_BOOT_SIZE);
  121. }
  122. }
  123. }
  124. #define SDRAM_BASE_CONF (AT91_SDRAMC_NR_13 | AT91_SDRAMC_CAS_3 \
  125. |AT91_SDRAMC_NB_4 | AT91_SDRAMC_DBW_32 \
  126. | AT91_SDRAMC_TWR_VAL(3) | AT91_SDRAMC_TRC_VAL(9) \
  127. | AT91_SDRAMC_TRP_VAL(3) | AT91_SDRAMC_TRCD_VAL(3) \
  128. | AT91_SDRAMC_TRAS_VAL(6) | AT91_SDRAMC_TXSR_VAL(10))
  129. void sdramc_configure(unsigned int mask)
  130. {
  131. struct at91_matrix *ma = (struct at91_matrix *)ATMEL_BASE_MATRIX;
  132. struct sdramc_reg setting;
  133. at91_sdram_hw_init();
  134. setting.cr = SDRAM_BASE_CONF | mask;
  135. setting.mdr = AT91_SDRAMC_MD_SDRAM;
  136. setting.tr = (CONFIG_SYS_MASTER_CLOCK * 7) / 1000000;
  137. writel(readl(&ma->ebicsa) | AT91_MATRIX_CS1A_SDRAMC |
  138. AT91_MATRIX_VDDIOMSEL_3_3V | AT91_MATRIX_EBI_IOSR_SEL,
  139. &ma->ebicsa);
  140. sdramc_initialize(ATMEL_BASE_CS1, &setting);
  141. }
  142. void mem_init(void)
  143. {
  144. unsigned int ram_size = 0;
  145. /* Configure SDRAM for 128MB */
  146. sdramc_configure(AT91_SDRAMC_NC_10);
  147. /* Do memtest for 128MB */
  148. ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  149. CONFIG_SYS_SDRAM_SIZE);
  150. /*
  151. * If 32MB or 16MB should be supported check also for
  152. * expected mirroring at A16 and A17
  153. * To find mirror addresses depends how the collumns are connected
  154. * at RAM (internaly or externaly)
  155. * If the collumns are not in inverted order the mirror size effect
  156. * behaves like normal SRAM with A0,A1,A2,etc. connected incremantal
  157. */
  158. /* Mirrors at A15 on ATMEL G20 SDRAM Controller with 64MB*/
  159. if (ram_size == 0x800) {
  160. printf("\n\r 64MB");
  161. sdramc_configure(AT91_SDRAMC_NC_9);
  162. } else {
  163. /* Size already initialized */
  164. printf("\n\r 128MB");
  165. }
  166. }
  167. #endif
  168. #ifdef CONFIG_MACB
  169. static void siemens_phy_reset(void)
  170. {
  171. /*
  172. * we need to reset PHY for 200us
  173. * because of bug in ATMEL G20 CPU (undefined initial state of GPIO)
  174. */
  175. if ((readl(AT91_ASM_RSTC_SR) & AT91_RSTC_RSTTYP) ==
  176. AT91_RSTC_RSTTYP_GENERAL)
  177. at91_set_gpio_value(AT91_PIN_PA25, 0); /* reset eth switch */
  178. }
  179. static void taurus_macb_hw_init(void)
  180. {
  181. /* Enable EMAC clock */
  182. at91_periph_clk_enable(ATMEL_ID_EMAC0);
  183. /*
  184. * Disable pull-up on:
  185. * RXDV (PA17) => PHY normal mode (not Test mode)
  186. * ERX0 (PA14) => PHY ADDR0
  187. * ERX1 (PA15) => PHY ADDR1
  188. * ERX2 (PA25) => PHY ADDR2
  189. * ERX3 (PA26) => PHY ADDR3
  190. * ECRS (PA28) => PHY ADDR4 => PHYADDR = 0x0
  191. *
  192. * PHY has internal pull-down
  193. */
  194. at91_set_pio_pullup(AT91_PIO_PORTA, 14, 0);
  195. at91_set_pio_pullup(AT91_PIO_PORTA, 15, 0);
  196. at91_set_pio_pullup(AT91_PIO_PORTA, 17, 0);
  197. at91_set_pio_pullup(AT91_PIO_PORTA, 25, 0);
  198. at91_set_pio_pullup(AT91_PIO_PORTA, 26, 0);
  199. at91_set_pio_pullup(AT91_PIO_PORTA, 28, 0);
  200. siemens_phy_reset();
  201. at91_phy_reset();
  202. at91_set_gpio_input(AT91_PIN_PA25, 1); /* ERST tri-state */
  203. /* Re-enable pull-up */
  204. at91_set_pio_pullup(AT91_PIO_PORTA, 14, 1);
  205. at91_set_pio_pullup(AT91_PIO_PORTA, 15, 1);
  206. at91_set_pio_pullup(AT91_PIO_PORTA, 17, 1);
  207. at91_set_pio_pullup(AT91_PIO_PORTA, 25, 1);
  208. at91_set_pio_pullup(AT91_PIO_PORTA, 26, 1);
  209. at91_set_pio_pullup(AT91_PIO_PORTA, 28, 1);
  210. /* Initialize EMAC=MACB hardware */
  211. at91_macb_hw_init();
  212. }
  213. #endif
  214. #ifdef CONFIG_GENERIC_ATMEL_MCI
  215. int board_mmc_init(bd_t *bd)
  216. {
  217. at91_mci_hw_init();
  218. return atmel_mci_init((void *)ATMEL_BASE_MCI);
  219. }
  220. #endif
  221. int board_early_init_f(void)
  222. {
  223. /* Enable clocks for all PIOs */
  224. at91_periph_clk_enable(ATMEL_ID_PIOA);
  225. at91_periph_clk_enable(ATMEL_ID_PIOB);
  226. at91_periph_clk_enable(ATMEL_ID_PIOC);
  227. at91_seriald_hw_init();
  228. return 0;
  229. }
  230. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  231. {
  232. return bus == 0 && cs == 0;
  233. }
  234. void spi_cs_activate(struct spi_slave *slave)
  235. {
  236. at91_set_gpio_value(TAURUS_SPI_CS_PIN, 0);
  237. }
  238. void spi_cs_deactivate(struct spi_slave *slave)
  239. {
  240. at91_set_gpio_value(TAURUS_SPI_CS_PIN, 1);
  241. }
  242. #ifdef CONFIG_USB_GADGET_AT91
  243. #include <linux/usb/at91_udc.h>
  244. void at91_udp_hw_init(void)
  245. {
  246. at91_pmc_t *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
  247. /* Enable PLLB */
  248. writel(get_pllb_init(), &pmc->pllbr);
  249. while ((readl(&pmc->sr) & AT91_PMC_LOCKB) != AT91_PMC_LOCKB)
  250. ;
  251. /* Enable UDPCK clock, MCK is enabled in at91_clock_init() */
  252. at91_periph_clk_enable(ATMEL_ID_UDP);
  253. writel(AT91SAM926x_PMC_UDP, &pmc->scer);
  254. }
  255. struct at91_udc_data board_udc_data = {
  256. .baseaddr = ATMEL_BASE_UDP0,
  257. };
  258. #endif
  259. int board_init(void)
  260. {
  261. /* adress of boot parameters */
  262. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  263. #ifdef CONFIG_CMD_NAND
  264. taurus_nand_hw_init();
  265. #endif
  266. #ifdef CONFIG_MACB
  267. taurus_macb_hw_init();
  268. #endif
  269. at91_spi0_hw_init(TAURUS_SPI_MASK);
  270. #ifdef CONFIG_USB_GADGET_AT91
  271. at91_udp_hw_init();
  272. at91_udc_probe(&board_udc_data);
  273. #endif
  274. return 0;
  275. }
  276. int dram_init(void)
  277. {
  278. gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  279. CONFIG_SYS_SDRAM_SIZE);
  280. return 0;
  281. }
  282. int board_eth_init(bd_t *bis)
  283. {
  284. int rc = 0;
  285. #ifdef CONFIG_MACB
  286. rc = macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, 0x00);
  287. #endif
  288. return rc;
  289. }
  290. #if !defined(CONFIG_SPL_BUILD)
  291. #if defined(CONFIG_BOARD_AXM)
  292. /*
  293. * Booting the Fallback Image.
  294. *
  295. * The function is used to provide and
  296. * boot the image with the fallback
  297. * parameters, incase if the faulty image
  298. * in upgraded over the base firmware.
  299. *
  300. */
  301. static int upgrade_failure_fallback(void)
  302. {
  303. char *partitionset_active = NULL;
  304. char *rootfs = NULL;
  305. char *rootfs_fallback = NULL;
  306. char *kern_off;
  307. char *kern_off_fb;
  308. char *kern_size;
  309. char *kern_size_fb;
  310. partitionset_active = getenv("partitionset_active");
  311. if (partitionset_active) {
  312. if (partitionset_active[0] == 'A')
  313. setenv("partitionset_active", "B");
  314. else
  315. setenv("partitionset_active", "A");
  316. } else {
  317. printf("partitionset_active missing.\n");
  318. return -ENOENT;
  319. }
  320. rootfs = getenv("rootfs");
  321. rootfs_fallback = getenv("rootfs_fallback");
  322. setenv("rootfs", rootfs_fallback);
  323. setenv("rootfs_fallback", rootfs);
  324. kern_size = getenv("kernel_size");
  325. kern_size_fb = getenv("kernel_size_fallback");
  326. setenv("kernel_size", kern_size_fb);
  327. setenv("kernel_size_fallback", kern_size);
  328. kern_off = getenv("kernel_Off");
  329. kern_off_fb = getenv("kernel_Off_fallback");
  330. setenv("kernel_Off", kern_off_fb);
  331. setenv("kernel_Off_fallback", kern_off);
  332. setenv("bootargs", '\0');
  333. setenv("upgrade_available", '\0');
  334. setenv("boot_retries", '\0');
  335. saveenv();
  336. return 0;
  337. }
  338. static int do_upgrade_available(cmd_tbl_t *cmdtp, int flag, int argc,
  339. char * const argv[])
  340. {
  341. unsigned long upgrade_available = 0;
  342. unsigned long boot_retry = 0;
  343. char boot_buf[10];
  344. upgrade_available = simple_strtoul(getenv("upgrade_available"), NULL,
  345. 10);
  346. if (upgrade_available) {
  347. boot_retry = simple_strtoul(getenv("boot_retries"), NULL, 10);
  348. boot_retry++;
  349. sprintf(boot_buf, "%lx", boot_retry);
  350. setenv("boot_retries", boot_buf);
  351. saveenv();
  352. /*
  353. * Here the boot_retries count is checked, and if the
  354. * count becomes greater than 2 switch back to the
  355. * fallback, and reset the board.
  356. */
  357. if (boot_retry > 2) {
  358. if (upgrade_failure_fallback() == 0)
  359. do_reset(NULL, 0, 0, NULL);
  360. return -1;
  361. }
  362. }
  363. return 0;
  364. }
  365. U_BOOT_CMD(
  366. upgrade_available, 1, 1, do_upgrade_available,
  367. "check Siemens update",
  368. "no parameters"
  369. );
  370. #endif
  371. #endif