spi-boot.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2012 Renesas Solutions Corp.
  3. *
  4. * This file is subject to the terms and conditions of the GNU Lesser
  5. * General Public License. See the file "COPYING.LIB" in the main
  6. * directory of this archive for more details.
  7. */
  8. #include <common.h>
  9. #define CONFIG_RAM_BOOT_PHYS CONFIG_SYS_TEXT_BASE
  10. #define CONFIG_SPI_ADDR 0x00000000
  11. #define CONFIG_SPI_LENGTH CONFIG_SYS_MONITOR_LEN
  12. #define CONFIG_RAM_BOOT CONFIG_SYS_TEXT_BASE
  13. #define SPIWDMADR 0xFE001018
  14. #define SPIWDMCNTR 0xFE001020
  15. #define SPIDMCOR 0xFE001028
  16. #define SPIDMINTSR 0xFE001188
  17. #define SPIDMINTMR 0xFE001190
  18. #define SPIDMINTSR_DMEND 0x00000004
  19. #define TBR 0xFE002000
  20. #define RBR 0xFE002000
  21. #define CR1 0xFE002008
  22. #define CR2 0xFE002010
  23. #define CR3 0xFE002018
  24. #define CR4 0xFE002020
  25. /* CR1 */
  26. #define SPI_TBE 0x80
  27. #define SPI_TBF 0x40
  28. #define SPI_RBE 0x20
  29. #define SPI_RBF 0x10
  30. #define SPI_PFONRD 0x08
  31. #define SPI_SSDB 0x04
  32. #define SPI_SSD 0x02
  33. #define SPI_SSA 0x01
  34. /* CR2 */
  35. #define SPI_RSTF 0x80
  36. #define SPI_LOOPBK 0x40
  37. #define SPI_CPOL 0x20
  38. #define SPI_CPHA 0x10
  39. #define SPI_L1M0 0x08
  40. /* CR4 */
  41. #define SPI_TBEI 0x80
  42. #define SPI_TBFI 0x40
  43. #define SPI_RBEI 0x20
  44. #define SPI_RBFI 0x10
  45. #define SPI_SpiS0 0x02
  46. #define SPI_SSS 0x01
  47. #define spi_write(val, addr) (*(volatile unsigned long *)(addr)) = val
  48. #define spi_read(addr) (*(volatile unsigned long *)(addr))
  49. /* M25P80 */
  50. #define M25_READ 0x03
  51. #define __uses_spiboot2 __attribute__((section(".spiboot2.text")))
  52. static void __uses_spiboot2 spi_reset(void)
  53. {
  54. int timeout = 0x00100000;
  55. /* Make sure the last transaction is finalized */
  56. spi_write(0x00, CR3);
  57. spi_write(0x02, CR1);
  58. while (!(spi_read(CR4) & SPI_SpiS0)) {
  59. if (timeout-- < 0)
  60. break;
  61. }
  62. spi_write(0x00, CR1);
  63. spi_write(spi_read(CR2) | SPI_RSTF, CR2); /* fifo reset */
  64. spi_write(spi_read(CR2) & ~SPI_RSTF, CR2);
  65. spi_write(0, SPIDMCOR);
  66. }
  67. static void __uses_spiboot2 spi_read_flash(void *buf, unsigned long addr,
  68. unsigned long len)
  69. {
  70. spi_write(M25_READ, TBR);
  71. spi_write((addr >> 16) & 0xFF, TBR);
  72. spi_write((addr >> 8) & 0xFF, TBR);
  73. spi_write(addr & 0xFF, TBR);
  74. spi_write(SPIDMINTSR_DMEND, SPIDMINTSR);
  75. spi_write((unsigned long)buf, SPIWDMADR);
  76. spi_write(len & 0xFFFFFFE0, SPIWDMCNTR);
  77. spi_write(1, SPIDMCOR);
  78. spi_write(0xff, CR3);
  79. spi_write(spi_read(CR1) | SPI_SSDB, CR1);
  80. spi_write(spi_read(CR1) | SPI_SSA, CR1);
  81. while (!(spi_read(SPIDMINTSR) & SPIDMINTSR_DMEND))
  82. ;
  83. /* Nagate SP0-SS0 */
  84. spi_write(0, CR1);
  85. }
  86. void __uses_spiboot2 spiboot_main(void)
  87. {
  88. void (*_start)(void) = (void *)CONFIG_SYS_TEXT_BASE;
  89. spi_reset();
  90. spi_read_flash((void *)CONFIG_RAM_BOOT_PHYS, CONFIG_SPI_ADDR,
  91. CONFIG_SPI_LENGTH);
  92. _start();
  93. }