spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * spi.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include "config.h"
  26. #include "spi.h"
  27. struct spi {
  28. uint32_t _rsvd0[6];
  29. uint32_t irq; /* 0110 - SPIIRQ - Interrupt Status Register */
  30. uint32_t irqen; /* 0111 - SPIIRQEN - Interrupt Control Register */
  31. uint32_t cr0; /* 1000 - CR0 - Control Register 0 */
  32. uint32_t cr1; /* 1001 - CR1 - Control Register 1 */
  33. uint32_t cr2; /* 1010 - CR2 - Control Register 2 */
  34. uint32_t br; /* 1011 - BR - Baud Rate Register */
  35. uint32_t sr; /* 1100 - SR - Status Register */
  36. uint32_t txdr; /* 1101 - TXDR - Transmit Data Register */
  37. uint32_t rxdr; /* 1110 - RXDR - Receive Data Register */
  38. uint32_t csr; /* 1111 - CSR - Chip Select Register */
  39. } __attribute__((packed,aligned(4)));
  40. #define SPI_CR0_TIDLE(xcnt) (((xcnt) & 3) << 6)
  41. #define SPI_CR0_TTRAIL(xcnt) (((xcnt) & 7) << 3)
  42. #define SPI_CR0_TLEAD(xcnt) (((xcnt) & 7) << 0)
  43. #define SPI_CR1_ENABLE (1 << 7)
  44. #define SPI_CR1_WKUPEN_USER (1 << 6)
  45. #define SPI_CR1_TXEDGE (1 << 4)
  46. #define SPI_CR2_MASTER (1 << 7)
  47. #define SPI_CR2_MCSH (1 << 6)
  48. #define SPI_CR2_SDBRE (1 << 5)
  49. #define SPI_CR2_CPOL (1 << 2)
  50. #define SPI_CR2_CPHA (1 << 1)
  51. #define SPI_CR2_LSBF (1 << 0)
  52. #define SPI_SR_TIP (1 << 7)
  53. #define SPI_SR_BUSY (1 << 6)
  54. #define SPI_SR_TRDY (1 << 4)
  55. #define SPI_SR_RRDY (1 << 3)
  56. #define SPI_SR_TOE (1 << 2)
  57. #define SPI_SR_ROE (1 << 1)
  58. #define SPI_SR_MDF (1 << 0)
  59. static volatile struct spi * const spi_regs = (void*)(SPI_BASE);
  60. void
  61. spi_init(void)
  62. {
  63. spi_regs->cr0 = SPI_CR0_TIDLE(3) |
  64. SPI_CR0_TTRAIL(7) |
  65. SPI_CR0_TLEAD(7);
  66. spi_regs->cr1 = SPI_CR1_ENABLE;
  67. spi_regs->cr2 = SPI_CR2_MASTER | SPI_CR2_MCSH;
  68. spi_regs->br = 3;
  69. spi_regs->csr = 0xf;
  70. }
  71. void
  72. spi_xfer(unsigned cs, struct spi_xfer_chunk *xfer, unsigned n)
  73. {
  74. /* Setup CS */
  75. spi_regs->csr = 0xf ^ (1 << cs);
  76. /* Run the chunks */
  77. while (n--) {
  78. for (int i=0; i<xfer->len; i++)
  79. {
  80. spi_regs->txdr = xfer->write ? xfer->data[i] : 0x00;
  81. while (!(spi_regs->sr & SPI_SR_RRDY));
  82. if (xfer->read)
  83. xfer->data[i] = spi_regs->rxdr;
  84. }
  85. xfer++;
  86. }
  87. /* Clear CS */
  88. spi_regs->csr = 0xf ^ (1 << cs);
  89. }
  90. void flash_cmd(uint8_t cmd)
  91. {
  92. struct spi_xfer_chunk xfer[1] = {
  93. { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
  94. };
  95. spi_xfer(SPI_CS_FLASH, xfer, 1);
  96. }
  97. uint32_t flash_id(void)
  98. {
  99. uint32_t buf = 0x9f;
  100. struct spi_xfer_chunk xfer[2] = {
  101. { .data = (void*)&buf, .len = 1, .read = false, .write = true, },
  102. { .data = (void*)&buf, .len = 3, .read = true, .write = false, },
  103. };
  104. spi_xfer(SPI_CS_FLASH, xfer, 2);
  105. return buf;
  106. }
  107. void flash_read(void *dst, uint32_t addr, unsigned len)
  108. {
  109. uint8_t cmd[4] = { 0x03, ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff) };
  110. struct spi_xfer_chunk xfer[2] = {
  111. { .data = (void*)cmd, .len = 4, .read = false, .write = true, },
  112. { .data = (void*)dst, .len = len, .read = true, .write = false, },
  113. };
  114. spi_xfer(SPI_CS_FLASH, xfer, 2);
  115. }