lpc32xx_ssp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * LPC32xx SSP interface (SPI mode)
  3. *
  4. * (C) Copyright 2014 DENX Software Engineering GmbH
  5. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <linux/compat.h>
  11. #include <asm/io.h>
  12. #include <malloc.h>
  13. #include <spi.h>
  14. #include <asm/arch/clk.h>
  15. /* SSP chip registers */
  16. struct ssp_regs {
  17. u32 cr0;
  18. u32 cr1;
  19. u32 data;
  20. u32 sr;
  21. u32 cpsr;
  22. u32 imsc;
  23. u32 ris;
  24. u32 mis;
  25. u32 icr;
  26. u32 dmacr;
  27. };
  28. /* CR1 register defines */
  29. #define SSP_CR1_SSP_ENABLE 0x0002
  30. /* SR register defines */
  31. #define SSP_SR_TNF 0x0002
  32. /* SSP status RX FIFO not empty bit */
  33. #define SSP_SR_RNE 0x0004
  34. /* lpc32xx spi slave */
  35. struct lpc32xx_spi_slave {
  36. struct spi_slave slave;
  37. struct ssp_regs *regs;
  38. };
  39. static inline struct lpc32xx_spi_slave *to_lpc32xx_spi_slave(
  40. struct spi_slave *slave)
  41. {
  42. return container_of(slave, struct lpc32xx_spi_slave, slave);
  43. }
  44. /* spi_init is called during boot when CONFIG_CMD_SPI is defined */
  45. void spi_init(void)
  46. {
  47. /*
  48. * nothing to do: clocking was enabled in lpc32xx_ssp_enable()
  49. * and configuration will be done in spi_setup_slave()
  50. */
  51. }
  52. /* the following is called in sequence by do_spi_xfer() */
  53. struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode)
  54. {
  55. struct lpc32xx_spi_slave *lslave;
  56. /* we only set up SSP0 for now, so ignore bus */
  57. if (mode & SPI_3WIRE) {
  58. error("3-wire mode not supported");
  59. return NULL;
  60. }
  61. if (mode & SPI_SLAVE) {
  62. error("slave mode not supported\n");
  63. return NULL;
  64. }
  65. if (mode & SPI_PREAMBLE) {
  66. error("preamble byte skipping not supported\n");
  67. return NULL;
  68. }
  69. lslave = spi_alloc_slave(struct lpc32xx_spi_slave, bus, cs);
  70. if (!lslave) {
  71. printf("SPI_error: Fail to allocate lpc32xx_spi_slave\n");
  72. return NULL;
  73. }
  74. lslave->regs = (struct ssp_regs *)SSP0_BASE;
  75. /*
  76. * 8 bit frame, SPI fmt, 500kbps -> clock divider is 26.
  77. * Set SCR to 0 and CPSDVSR to 26.
  78. */
  79. writel(0x7, &lslave->regs->cr0); /* 8-bit chunks, SPI, 1 clk/bit */
  80. writel(26, &lslave->regs->cpsr); /* SSP clock = HCLK/26 = 500kbps */
  81. writel(0, &lslave->regs->imsc); /* do not raise any interrupts */
  82. writel(0, &lslave->regs->icr); /* clear any pending interrupt */
  83. writel(0, &lslave->regs->dmacr); /* do not do DMAs */
  84. writel(SSP_CR1_SSP_ENABLE, &lslave->regs->cr1); /* enable SSP0 */
  85. return &lslave->slave;
  86. }
  87. void spi_free_slave(struct spi_slave *slave)
  88. {
  89. struct lpc32xx_spi_slave *lslave = to_lpc32xx_spi_slave(slave);
  90. debug("(lpc32xx) spi_free_slave: 0x%08x\n", (u32)lslave);
  91. free(lslave);
  92. }
  93. int spi_claim_bus(struct spi_slave *slave)
  94. {
  95. /* only one bus and slave so far, always available */
  96. return 0;
  97. }
  98. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  99. const void *dout, void *din, unsigned long flags)
  100. {
  101. struct lpc32xx_spi_slave *lslave = to_lpc32xx_spi_slave(slave);
  102. int bytelen = bitlen >> 3;
  103. int idx_out = 0;
  104. int idx_in = 0;
  105. int start_time;
  106. start_time = get_timer(0);
  107. while ((idx_out < bytelen) || (idx_in < bytelen)) {
  108. int status = readl(&lslave->regs->sr);
  109. if ((idx_out < bytelen) && (status & SSP_SR_TNF))
  110. writel(((u8 *)dout)[idx_out++], &lslave->regs->data);
  111. if ((idx_in < bytelen) && (status & status & SSP_SR_RNE))
  112. ((u8 *)din)[idx_in++] = readl(&lslave->regs->data);
  113. if (get_timer(start_time) >= CONFIG_LPC32XX_SSP_TIMEOUT)
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. void spi_release_bus(struct spi_slave *slave)
  119. {
  120. /* do nothing */
  121. }