spi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 (unsigned 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. #define FLASH_CMD_DEEP_POWER_DOWN 0xb9
  91. #define FLASH_CMD_WAKE_UP 0xab
  92. #define FLASH_CMD_WRITE_ENABLE 0x06
  93. #define FLASH_CMD_WRITE_ENABLE_VOLATILE 0x50
  94. #define FLASH_CMD_WRITE_DISABLE 0x04
  95. #define FLASH_CMD_READ_MANUF_ID 0x9f
  96. #define FLASH_CMD_READ_UNIQUE_ID 0x4b
  97. #define FLASH_CMD_READ_SR1 0x05
  98. #define FLASH_CMD_WRITE_SR1 0x01
  99. #define FLASH_CMD_READ_DATA 0x03
  100. #define FLASH_CMD_PAGE_PROGRAM 0x02
  101. #define FLASH_CMD_CHIP_ERASE 0x60
  102. #define FLASH_CMD_SECTOR_ERASE 0x20
  103. void
  104. flash_cmd(uint8_t cmd)
  105. {
  106. struct spi_xfer_chunk xfer[1] = {
  107. { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
  108. };
  109. spi_xfer(SPI_CS_FLASH, xfer, 1);
  110. }
  111. void
  112. flash_deep_power_down(void)
  113. {
  114. flash_cmd(FLASH_CMD_DEEP_POWER_DOWN);
  115. }
  116. void
  117. flash_wake_up(void)
  118. {
  119. flash_cmd(FLASH_CMD_WAKE_UP);
  120. }
  121. void
  122. flash_write_enable(void)
  123. {
  124. flash_cmd(FLASH_CMD_WRITE_ENABLE);
  125. }
  126. void
  127. flash_write_enable_volatile(void)
  128. {
  129. flash_cmd(FLASH_CMD_WRITE_ENABLE_VOLATILE);
  130. }
  131. void
  132. flash_write_disable(void)
  133. {
  134. flash_cmd(FLASH_CMD_WRITE_DISABLE);
  135. }
  136. void
  137. flash_manuf_id(void *manuf)
  138. {
  139. uint8_t cmd = FLASH_CMD_READ_MANUF_ID;
  140. struct spi_xfer_chunk xfer[2] = {
  141. { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
  142. { .data = (void*)manuf, .len = 3, .read = true, .write = false, },
  143. };
  144. spi_xfer(SPI_CS_FLASH, xfer, 2);
  145. }
  146. void
  147. flash_unique_id(void *id)
  148. {
  149. uint8_t cmd = FLASH_CMD_READ_UNIQUE_ID;
  150. struct spi_xfer_chunk xfer[3] = {
  151. { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
  152. { .data = (void*)0, .len = 4, .read = false, .write = false, },
  153. { .data = (void*)id, .len = 8, .read = true, .write = false, },
  154. };
  155. spi_xfer(SPI_CS_FLASH, xfer, 3);
  156. }
  157. uint8_t
  158. flash_read_sr(void)
  159. {
  160. uint8_t cmd = FLASH_CMD_READ_SR1;
  161. uint8_t rv;
  162. struct spi_xfer_chunk xfer[2] = {
  163. { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
  164. { .data = (void*)&rv, .len = 1, .read = true, .write = false, },
  165. };
  166. spi_xfer(SPI_CS_FLASH, xfer, 2);
  167. return rv;
  168. }
  169. void
  170. flash_write_sr(uint8_t sr)
  171. {
  172. uint8_t cmd[2] = { FLASH_CMD_WRITE_SR1, sr };
  173. struct spi_xfer_chunk xfer[1] = {
  174. { .data = (void*)cmd, .len = 2, .read = false, .write = true, },
  175. };
  176. spi_xfer(SPI_CS_FLASH, xfer, 1);
  177. }
  178. void
  179. flash_read(void *dst, uint32_t addr, unsigned len)
  180. {
  181. uint8_t cmd[4] = { FLASH_CMD_READ_DATA, ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff) };
  182. struct spi_xfer_chunk xfer[2] = {
  183. { .data = (void*)cmd, .len = 4, .read = false, .write = true, },
  184. { .data = (void*)dst, .len = len, .read = true, .write = false, },
  185. };
  186. spi_xfer(SPI_CS_FLASH, xfer, 2);
  187. }
  188. void
  189. flash_page_program(void *src, uint32_t addr, unsigned len)
  190. {
  191. uint8_t cmd[4] = { FLASH_CMD_PAGE_PROGRAM, ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff) };
  192. struct spi_xfer_chunk xfer[2] = {
  193. { .data = (void*)cmd, .len = 4, .read = false, .write = true, },
  194. { .data = (void*)src, .len = len, .read = false, .write = true, },
  195. };
  196. spi_xfer(SPI_CS_FLASH, xfer, 2);
  197. }
  198. void
  199. flash_sector_erase(uint32_t addr)
  200. {
  201. uint8_t cmd[4] = { FLASH_CMD_SECTOR_ERASE, ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff) };
  202. struct spi_xfer_chunk xfer[1] = {
  203. { .data = (void*)cmd, .len = 4, .read = false, .write = true, },
  204. };
  205. spi_xfer(SPI_CS_FLASH, xfer, 1);
  206. }