ethernut5.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * (C) Copyright 2011
  3. * egnite GmbH <info@egnite.de>
  4. *
  5. * (C) Copyright 2010
  6. * Ole Reinhardt <ole.reinhardt@thermotemp.de>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * Ethernut 5 general board support
  28. *
  29. * Ethernut is an open source hardware and software project for
  30. * embedded Ethernet devices. Hardware layouts and CAD files are
  31. * freely available under BSD-like license.
  32. *
  33. * Ethernut 5 is the first member of the Ethernut board family
  34. * with U-Boot and Linux support. This implementation is based
  35. * on the original work done by Ole Reinhardt, but heavily modified
  36. * to support additional features and the latest board revision 5.0F.
  37. *
  38. * Main board components are by default:
  39. *
  40. * Atmel AT91SAM9XE512 CPU with 512 kBytes NOR Flash
  41. * 2 x 64 MBytes Micron MT48LC32M16A2P SDRAM
  42. * 512 MBytes Micron MT29F4G08ABADA NAND Flash
  43. * 4 MBytes Atmel AT45DB321D DataFlash
  44. * SMSC LAN8710 Ethernet PHY
  45. * Atmel ATmega168 MCU used for power management
  46. * Linear Technology LTC4411 PoE controller
  47. *
  48. * U-Boot relevant board interfaces are:
  49. *
  50. * 100 Mbit Ethernet with IEEE 802.3af PoE
  51. * RS-232 serial port
  52. * USB host and device
  53. * MMC/SD-Card slot
  54. * Expansion port with I2C, SPI and more...
  55. *
  56. * Typically the U-Boot image is loaded from serial DataFlash into
  57. * SDRAM by the samboot boot loader, which is located in internal
  58. * NOR Flash and provides all essential initializations like CPU
  59. * and peripheral clocks and, of course, the SDRAM configuration.
  60. *
  61. * For testing purposes it is also possibly to directly transfer
  62. * the image into SDRAM via JTAG. A tested configuration exists
  63. * for the Turtelizer 2 hardware dongle and the OpenOCD software.
  64. * In this case the latter will do the basic hardware configuration
  65. * via its reset-init script.
  66. *
  67. * For additional information visit the project home page at
  68. * http://www.ethernut.de/
  69. */
  70. #include <common.h>
  71. #include <net.h>
  72. #include <netdev.h>
  73. #include <miiphy.h>
  74. #include <i2c.h>
  75. #include <spi.h>
  76. #include <dataflash.h>
  77. #include <mmc.h>
  78. #include <atmel_mci.h>
  79. #include <asm/arch/at91sam9260.h>
  80. #include <asm/arch/at91sam9260_matrix.h>
  81. #include <asm/arch/at91sam9_smc.h>
  82. #include <asm/arch/at91_common.h>
  83. #include <asm/arch/at91_pmc.h>
  84. #include <asm/arch/at91_spi.h>
  85. #include <asm/arch/gpio.h>
  86. #include <asm/io.h>
  87. #include "ethernut5_pwrman.h"
  88. DECLARE_GLOBAL_DATA_PTR;
  89. AT91S_DATAFLASH_INFO dataflash_info[CONFIG_SYS_MAX_DATAFLASH_BANKS];
  90. struct dataflash_addr cs[CONFIG_SYS_MAX_DATAFLASH_BANKS] = {
  91. {CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS0, 0}
  92. };
  93. /*
  94. * In fact we have 7 partitions, but u-boot supports 5 only. This is
  95. * no big deal, because the first partition is reserved for applications
  96. * and the last one is used by Nut/OS. Both need not to be visible here.
  97. */
  98. dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
  99. { 0x00021000, 0x00041FFF, FLAG_PROTECT_SET, 0, "setup" },
  100. { 0x00042000, 0x000C5FFF, FLAG_PROTECT_SET, 0, "uboot" },
  101. { 0x000C6000, 0x00359FFF, FLAG_PROTECT_SET, 0, "kernel" },
  102. { 0x0035A000, 0x003DDFFF, FLAG_PROTECT_SET, 0, "nutos" },
  103. { 0x003DE000, 0x003FEFFF, FLAG_PROTECT_CLEAR, 0, "env" }
  104. };
  105. /*
  106. * This is called last during early initialization. Most of the basic
  107. * hardware interfaces are up and running.
  108. *
  109. * The SDRAM hardware has been configured by the first stage boot loader.
  110. * We only need to announce its size, using u-boot's memory check.
  111. */
  112. int dram_init(void)
  113. {
  114. gd->ram_size = get_ram_size(
  115. (void *)CONFIG_SYS_SDRAM_BASE,
  116. CONFIG_SYS_SDRAM_SIZE);
  117. return 0;
  118. }
  119. #ifdef CONFIG_CMD_NAND
  120. static void ethernut5_nand_hw_init(void)
  121. {
  122. struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
  123. struct at91_matrix *matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
  124. unsigned long csa;
  125. /* Assign CS3 to NAND/SmartMedia Interface */
  126. csa = readl(&matrix->ebicsa);
  127. csa |= AT91_MATRIX_CS3A_SMC_SMARTMEDIA;
  128. writel(csa, &matrix->ebicsa);
  129. /* Configure SMC CS3 for NAND/SmartMedia */
  130. writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
  131. AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
  132. &smc->cs[3].setup);
  133. writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
  134. AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
  135. &smc->cs[3].pulse);
  136. writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
  137. &smc->cs[3].cycle);
  138. writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
  139. AT91_SMC_MODE_EXNW_DISABLE |
  140. AT91_SMC_MODE_DBW_8 |
  141. AT91_SMC_MODE_TDF_CYCLE(2),
  142. &smc->cs[3].mode);
  143. #ifdef CONFIG_SYS_NAND_READY_PIN
  144. /* Ready pin is optional. */
  145. at91_set_pio_input(CONFIG_SYS_NAND_READY_PIN, 1);
  146. #endif
  147. at91_set_pio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
  148. }
  149. #endif
  150. /*
  151. * This is called first during late initialization.
  152. */
  153. int board_init(void)
  154. {
  155. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  156. /* Enable clocks for all PIOs */
  157. writel((1 << ATMEL_ID_PIOA) | (1 << ATMEL_ID_PIOB) |
  158. (1 << ATMEL_ID_PIOC),
  159. &pmc->pcer);
  160. /* Set adress of boot parameters. */
  161. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  162. /* Initialize UARTs and power management. */
  163. at91_seriald_hw_init();
  164. ethernut5_power_init();
  165. #ifdef CONFIG_CMD_NAND
  166. ethernut5_nand_hw_init();
  167. #endif
  168. #ifdef CONFIG_HAS_DATAFLASH
  169. at91_spi0_hw_init(1 << 0);
  170. #endif
  171. return 0;
  172. }
  173. #ifdef CONFIG_MACB
  174. /*
  175. * This is optionally called last during late initialization.
  176. */
  177. int board_eth_init(bd_t *bis)
  178. {
  179. const char *devname;
  180. unsigned short mode;
  181. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  182. /* Enable on-chip EMAC clock. */
  183. writel(1 << ATMEL_ID_EMAC0, &pmc->pcer);
  184. /* Need to reset PHY via power management. */
  185. ethernut5_phy_reset();
  186. /* Set peripheral pins. */
  187. at91_macb_hw_init();
  188. /* Basic EMAC initialization. */
  189. if (macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC0, CONFIG_PHY_ID))
  190. return -1;
  191. /*
  192. * Early board revisions have a pull-down at the PHY's MODE0
  193. * strap pin, which forces the PHY into power down. Here we
  194. * switch to all-capable mode.
  195. */
  196. devname = miiphy_get_current_dev();
  197. if (miiphy_read(devname, 0, 18, &mode) == 0) {
  198. /* Set mode[2:0] to 0b111. */
  199. mode |= 0x00E0;
  200. miiphy_write(devname, 0, 18, mode);
  201. /* Soft reset overrides strap pins. */
  202. miiphy_write(devname, 0, MII_BMCR, BMCR_RESET);
  203. }
  204. /* Sync environment with network devices, needed for nfsroot. */
  205. return eth_init(gd->bd);
  206. }
  207. #endif
  208. #ifdef CONFIG_GENERIC_ATMEL_MCI
  209. int board_mmc_init(bd_t *bd)
  210. {
  211. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  212. /* Enable MCI clock. */
  213. writel(1 << ATMEL_ID_MCI, &pmc->pcer);
  214. /* Initialize MCI hardware. */
  215. at91_mci_hw_init();
  216. /* Register the device. */
  217. return atmel_mci_init((void *)ATMEL_BASE_MCI);
  218. }
  219. int board_mmc_getcd(struct mmc *mmc)
  220. {
  221. return !at91_get_pio_value(CONFIG_SYS_MMC_CD_PIN);
  222. }
  223. #endif
  224. #ifdef CONFIG_ATMEL_SPI
  225. /*
  226. * Note, that u-boot uses different code for SPI bus access. While
  227. * memory routines use automatic chip select control, the serial
  228. * flash support requires 'manual' GPIO control. Thus, we switch
  229. * modes.
  230. */
  231. void spi_cs_activate(struct spi_slave *slave)
  232. {
  233. /* Enable NPCS0 in GPIO mode. This disables peripheral control. */
  234. at91_set_pio_output(AT91_PIO_PORTA, 3, 0);
  235. }
  236. void spi_cs_deactivate(struct spi_slave *slave)
  237. {
  238. /* Disable NPCS0 in GPIO mode. */
  239. at91_set_pio_output(AT91_PIO_PORTA, 3, 1);
  240. /* Switch back to peripheral chip select control. */
  241. at91_set_a_periph(AT91_PIO_PORTA, 3, 1);
  242. }
  243. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  244. {
  245. return bus == 0 && cs == 0;
  246. }
  247. #endif