lowl-spi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Luke Dashjr
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #ifdef HAVE_LINUX_SPI_SPIDEV_H
  25. #define HAVE_LINUX_SPI
  26. #endif
  27. #include <stdbool.h>
  28. #include <stdint.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <getopt.h>
  34. #ifdef HAVE_LINUX_SPI
  35. #include <sys/mman.h>
  36. #include <fcntl.h>
  37. #include <sys/ioctl.h>
  38. #include <linux/types.h>
  39. #include <signal.h>
  40. #include <sys/types.h>
  41. #include <linux/spi/spidev.h>
  42. #include <time.h>
  43. #include <unistd.h>
  44. #include <sys/stat.h>
  45. #endif
  46. #include "logging.h"
  47. #include "lowl-spi.h"
  48. #ifdef HAVE_LINUX_SPI
  49. bool sys_spi_txrx(struct spi_port *port);
  50. static volatile unsigned *gpio;
  51. #endif
  52. struct spi_port *sys_spi;
  53. void spi_init(void)
  54. {
  55. #ifdef HAVE_LINUX_SPI
  56. int fd;
  57. fd = open("/dev/mem",O_RDWR|O_SYNC);
  58. if (fd < 0)
  59. {
  60. perror("/dev/mem trouble");
  61. return;
  62. }
  63. gpio = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  64. if (gpio == MAP_FAILED)
  65. {
  66. perror("gpio mmap trouble");
  67. return;
  68. }
  69. close(fd);
  70. sys_spi = malloc(sizeof(*sys_spi));
  71. *sys_spi = (struct spi_port){
  72. .txrx = sys_spi_txrx,
  73. };
  74. #endif
  75. }
  76. #ifdef HAVE_LINUX_SPI
  77. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  78. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  79. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  80. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  81. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  82. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  83. static
  84. int spi_reset(int a)
  85. {
  86. int i,j;
  87. int len = 8;
  88. INP_GPIO(10); OUT_GPIO(10);
  89. INP_GPIO(11); OUT_GPIO(11);
  90. GPIO_SET = 1 << 11; // Set SCK
  91. for (i = 0; i < 32; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  92. GPIO_SET = 1 << 10;
  93. for (j = 0; j < len; j++) {
  94. a *= a;
  95. }
  96. GPIO_CLR = 1 << 10;
  97. for (j = 0; j < len; j++) {
  98. a *= a;
  99. }
  100. }
  101. GPIO_CLR = 1 << 10;
  102. GPIO_CLR = 1 << 11;
  103. INP_GPIO(10);
  104. SET_GPIO_ALT(10,0);
  105. INP_GPIO(11);
  106. SET_GPIO_ALT(11,0);
  107. INP_GPIO(9);
  108. SET_GPIO_ALT(9,0);
  109. return a;
  110. }
  111. #define BAILOUT(s) do{ \
  112. perror(s); \
  113. close(fd); \
  114. return false; \
  115. }while(0)
  116. bool sys_spi_txrx(struct spi_port *port)
  117. {
  118. const void *wrbuf = spi_gettxbuf(port);
  119. void *rdbuf = spi_getrxbuf(port);
  120. size_t bufsz = spi_getbufsz(port);
  121. int fd;
  122. int mode, bits, speed, rv, i, j;
  123. struct spi_ioc_transfer tr[16];
  124. memset(&tr,0,sizeof(tr));
  125. mode = 0; bits = 8; speed = 4000000;
  126. if (port->speed)
  127. speed = port->speed;
  128. spi_reset(1234);
  129. fd = open("/dev/spidev0.0", O_RDWR);
  130. if (fd < 0) {
  131. perror("Unable to open SPI device");
  132. return false;
  133. }
  134. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0)
  135. BAILOUT("Unable to set WR MODE");
  136. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
  137. BAILOUT("Unable to set RD MODE");
  138. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
  139. BAILOUT("Unable to set WR_BITS_PER_WORD");
  140. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  141. BAILOUT("Unable to set RD_BITS_PER_WORD");
  142. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  143. BAILOUT("Unable to set WR_MAX_SPEED_HZ");
  144. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  145. BAILOUT("Unable to set RD_MAX_SPEED_HZ");
  146. rv = 0;
  147. while (bufsz >= 4096) {
  148. tr[rv].tx_buf = (uintptr_t) wrbuf;
  149. tr[rv].rx_buf = (uintptr_t) rdbuf;
  150. tr[rv].len = 4096;
  151. tr[rv].delay_usecs = 1;
  152. tr[rv].speed_hz = speed;
  153. tr[rv].bits_per_word = bits;
  154. bufsz -= 4096;
  155. wrbuf += 4096; rdbuf += 4096; rv ++;
  156. }
  157. if (bufsz > 0) {
  158. tr[rv].tx_buf = (uintptr_t) wrbuf;
  159. tr[rv].rx_buf = (uintptr_t) rdbuf;
  160. tr[rv].len = (unsigned)bufsz;
  161. tr[rv].delay_usecs = 1;
  162. tr[rv].speed_hz = speed;
  163. tr[rv].bits_per_word = bits;
  164. rv ++;
  165. }
  166. i = rv;
  167. for (j = 0; j < i; j++) {
  168. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  169. if (rv < 0)
  170. BAILOUT("WTF!");
  171. }
  172. close(fd);
  173. spi_reset(4321);
  174. return true;
  175. }
  176. #endif
  177. static
  178. void *spi_emit_buf_reverse(struct spi_port *port, const void *p, size_t sz)
  179. {
  180. const unsigned char *str = p;
  181. void * const rv = &port->spibuf_rx[port->spibufsz];
  182. if (port->spibufsz + sz >= SPIMAXSZ)
  183. return NULL;
  184. for (size_t i = 0; i < sz; ++i)
  185. {
  186. // Reverse bit order in each byte!
  187. unsigned char p = str[i];
  188. p = ((p & 0xaa)>>1) | ((p & 0x55) << 1);
  189. p = ((p & 0xcc)>>2) | ((p & 0x33) << 2);
  190. p = ((p & 0xf0)>>4) | ((p & 0x0f) << 4);
  191. port->spibuf[port->spibufsz++] = p;
  192. }
  193. return rv;
  194. }
  195. void spi_emit_buf(struct spi_port * const port, const void * const str, const size_t sz)
  196. {
  197. if (port->spibufsz + sz >= SPIMAXSZ)
  198. return;
  199. memcpy(&port->spibuf[port->spibufsz], str, sz);
  200. port->spibufsz += sz;
  201. }
  202. /* TODO: in production, emit just bit-sequences! Eliminate padding to byte! */
  203. void spi_emit_break(struct spi_port *port)
  204. {
  205. spi_emit_buf(port, "\x4", 1);
  206. }
  207. void spi_emit_fsync(struct spi_port *port)
  208. {
  209. spi_emit_buf(port, "\x6", 1);
  210. }
  211. void spi_emit_fasync(struct spi_port *port, int n)
  212. {
  213. int i;
  214. for (i = 0; i < n; i++) {
  215. spi_emit_buf(port, "\x5", 1);
  216. }
  217. }
  218. void spi_emit_nop(struct spi_port *port, int n) {
  219. int i;
  220. for (i = 0; i < n; ++i) {
  221. spi_emit_buf(port, "\x0", 1);
  222. }
  223. }
  224. void *spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len)
  225. {
  226. unsigned char otmp[3];
  227. if (len < 4 || len > 128)
  228. return NULL; /* This cannot be programmed in single frame! */
  229. len /= 4; /* Strip */
  230. otmp[0] = (len - 1) | 0xE0;
  231. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  232. spi_emit_buf(port, otmp, 3);
  233. return spi_emit_buf_reverse(port, buf, len*4);
  234. }
  235. #ifdef USE_BFSB
  236. void spi_bfsb_select_bank(int bank)
  237. {
  238. static int last_bank = -2;
  239. if (bank == last_bank)
  240. return;
  241. const int banks[4]={18,23,24,25}; // GPIO connected to OE of level shifters
  242. int i;
  243. for(i=0;i<4;i++)
  244. {
  245. if (i == bank)
  246. continue;
  247. INP_GPIO(banks[i]);
  248. OUT_GPIO(banks[i]);
  249. GPIO_CLR = 1 << banks[i];
  250. }
  251. if (bank != -1)
  252. {
  253. OUT_GPIO(banks[bank]);
  254. GPIO_SET = 1 << banks[bank];
  255. }
  256. last_bank = bank;
  257. }
  258. #endif