altera_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Altera SPI driver
  3. *
  4. * based on bfin_spi.c
  5. * Copyright (c) 2005-2008 Analog Devices Inc.
  6. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <malloc.h>
  13. #include <spi.h>
  14. #define ALTERA_SPI_RXDATA 0
  15. #define ALTERA_SPI_TXDATA 4
  16. #define ALTERA_SPI_STATUS 8
  17. #define ALTERA_SPI_CONTROL 12
  18. #define ALTERA_SPI_SLAVE_SEL 20
  19. #define ALTERA_SPI_STATUS_ROE_MSK (0x8)
  20. #define ALTERA_SPI_STATUS_TOE_MSK (0x10)
  21. #define ALTERA_SPI_STATUS_TMT_MSK (0x20)
  22. #define ALTERA_SPI_STATUS_TRDY_MSK (0x40)
  23. #define ALTERA_SPI_STATUS_RRDY_MSK (0x80)
  24. #define ALTERA_SPI_STATUS_E_MSK (0x100)
  25. #define ALTERA_SPI_CONTROL_IROE_MSK (0x8)
  26. #define ALTERA_SPI_CONTROL_ITOE_MSK (0x10)
  27. #define ALTERA_SPI_CONTROL_ITRDY_MSK (0x40)
  28. #define ALTERA_SPI_CONTROL_IRRDY_MSK (0x80)
  29. #define ALTERA_SPI_CONTROL_IE_MSK (0x100)
  30. #define ALTERA_SPI_CONTROL_SSO_MSK (0x400)
  31. #ifndef CONFIG_SYS_ALTERA_SPI_LIST
  32. #define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE }
  33. #endif
  34. static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST;
  35. struct altera_spi_slave {
  36. struct spi_slave slave;
  37. ulong base;
  38. };
  39. #define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave)
  40. __attribute__((weak))
  41. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  42. {
  43. return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32;
  44. }
  45. __attribute__((weak))
  46. void spi_cs_activate(struct spi_slave *slave)
  47. {
  48. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  49. writel(1 << slave->cs, altspi->base + ALTERA_SPI_SLAVE_SEL);
  50. writel(ALTERA_SPI_CONTROL_SSO_MSK, altspi->base + ALTERA_SPI_CONTROL);
  51. }
  52. __attribute__((weak))
  53. void spi_cs_deactivate(struct spi_slave *slave)
  54. {
  55. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  56. writel(0, altspi->base + ALTERA_SPI_CONTROL);
  57. writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
  58. }
  59. void spi_init(void)
  60. {
  61. }
  62. void spi_set_speed(struct spi_slave *slave, uint hz)
  63. {
  64. /* altera spi core does not support programmable speed */
  65. }
  66. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  67. unsigned int max_hz, unsigned int mode)
  68. {
  69. struct altera_spi_slave *altspi;
  70. if (!spi_cs_is_valid(bus, cs))
  71. return NULL;
  72. altspi = spi_alloc_slave(struct altera_spi_slave, bus, cs);
  73. if (!altspi)
  74. return NULL;
  75. altspi->base = altera_spi_base_list[bus];
  76. debug("%s: bus:%i cs:%i base:%lx\n", __func__,
  77. bus, cs, altspi->base);
  78. return &altspi->slave;
  79. }
  80. void spi_free_slave(struct spi_slave *slave)
  81. {
  82. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  83. free(altspi);
  84. }
  85. int spi_claim_bus(struct spi_slave *slave)
  86. {
  87. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  88. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  89. writel(0, altspi->base + ALTERA_SPI_CONTROL);
  90. writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
  91. return 0;
  92. }
  93. void spi_release_bus(struct spi_slave *slave)
  94. {
  95. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  96. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  97. writel(0, altspi->base + ALTERA_SPI_SLAVE_SEL);
  98. }
  99. #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
  100. # define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
  101. #endif
  102. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  103. void *din, unsigned long flags)
  104. {
  105. struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
  106. /* assume spi core configured to do 8 bit transfers */
  107. uint bytes = bitlen / 8;
  108. const uchar *txp = dout;
  109. uchar *rxp = din;
  110. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  111. slave->bus, slave->cs, bitlen, bytes, flags);
  112. if (bitlen == 0)
  113. goto done;
  114. if (bitlen % 8) {
  115. flags |= SPI_XFER_END;
  116. goto done;
  117. }
  118. /* empty read buffer */
  119. if (readl(altspi->base + ALTERA_SPI_STATUS) &
  120. ALTERA_SPI_STATUS_RRDY_MSK)
  121. readl(altspi->base + ALTERA_SPI_RXDATA);
  122. if (flags & SPI_XFER_BEGIN)
  123. spi_cs_activate(slave);
  124. while (bytes--) {
  125. uchar d = txp ? *txp++ : CONFIG_ALTERA_SPI_IDLE_VAL;
  126. debug("%s: tx:%x ", __func__, d);
  127. writel(d, altspi->base + ALTERA_SPI_TXDATA);
  128. while (!(readl(altspi->base + ALTERA_SPI_STATUS) &
  129. ALTERA_SPI_STATUS_RRDY_MSK))
  130. ;
  131. d = readl(altspi->base + ALTERA_SPI_RXDATA);
  132. if (rxp)
  133. *rxp++ = d;
  134. debug("rx:%x\n", d);
  135. }
  136. done:
  137. if (flags & SPI_XFER_END)
  138. spi_cs_deactivate(slave);
  139. return 0;
  140. }