lowl-spi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013-2014 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. int spi_open(struct spi_port * const spi, const char * const devpath)
  78. {
  79. const int fd = open(devpath, O_RDWR);
  80. if (fd < 0)
  81. return fd;
  82. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi->speed) < 0
  83. || ioctl(fd, SPI_IOC_WR_MODE, &spi->mode) < 0
  84. || ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits) < 0)
  85. {
  86. close(fd);
  87. return -1;
  88. }
  89. spi->fd = fd;
  90. return fd;
  91. }
  92. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  93. #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
  94. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  95. #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
  96. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  97. #define GPIO_LEV *(gpio+13)
  98. void bfg_gpio_setpin_output(const unsigned pin)
  99. {
  100. INP_GPIO(pin);
  101. OUT_GPIO(pin);
  102. }
  103. void bfg_gpio_set_high(const unsigned mask)
  104. {
  105. GPIO_SET = mask;
  106. }
  107. void bfg_gpio_set_low(const unsigned mask)
  108. {
  109. GPIO_CLR = mask;
  110. }
  111. unsigned bfg_gpio_get()
  112. {
  113. return GPIO_LEV;
  114. }
  115. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  116. static
  117. int spi_reset(int a)
  118. {
  119. int i,j;
  120. int len = 8;
  121. INP_GPIO(10); OUT_GPIO(10);
  122. INP_GPIO(11); OUT_GPIO(11);
  123. GPIO_SET = 1 << 11; // Set SCK
  124. for (i = 0; i < 32; i++) { // On standard settings this unoptimized code produces 1 Mhz freq.
  125. GPIO_SET = 1 << 10;
  126. for (j = 0; j < len; j++) {
  127. a *= a;
  128. }
  129. GPIO_CLR = 1 << 10;
  130. for (j = 0; j < len; j++) {
  131. a *= a;
  132. }
  133. }
  134. GPIO_CLR = 1 << 10;
  135. GPIO_CLR = 1 << 11;
  136. INP_GPIO(10);
  137. SET_GPIO_ALT(10,0);
  138. INP_GPIO(11);
  139. SET_GPIO_ALT(11,0);
  140. INP_GPIO(9);
  141. SET_GPIO_ALT(9,0);
  142. return a;
  143. }
  144. #define BAILOUT(s) do{ \
  145. perror(s); \
  146. close(fd); \
  147. return false; \
  148. }while(0)
  149. bool sys_spi_txrx(struct spi_port *port)
  150. {
  151. const void *wrbuf = spi_gettxbuf(port);
  152. void *rdbuf = spi_getrxbuf(port);
  153. size_t bufsz = spi_getbufsz(port);
  154. int fd;
  155. int mode, bits, speed, rv, i, j;
  156. struct spi_ioc_transfer tr[16];
  157. memset(&tr,0,sizeof(tr));
  158. mode = 0; bits = 8; speed = 4000000;
  159. if (port->speed)
  160. speed = port->speed;
  161. spi_reset(1234);
  162. fd = open("/dev/spidev0.0", O_RDWR);
  163. if (fd < 0) {
  164. perror("Unable to open SPI device");
  165. return false;
  166. }
  167. if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0)
  168. BAILOUT("Unable to set WR MODE");
  169. if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
  170. BAILOUT("Unable to set RD MODE");
  171. if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
  172. BAILOUT("Unable to set WR_BITS_PER_WORD");
  173. if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  174. BAILOUT("Unable to set RD_BITS_PER_WORD");
  175. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  176. BAILOUT("Unable to set WR_MAX_SPEED_HZ");
  177. if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  178. BAILOUT("Unable to set RD_MAX_SPEED_HZ");
  179. rv = 0;
  180. while (bufsz >= 4096) {
  181. tr[rv].tx_buf = (uintptr_t) wrbuf;
  182. tr[rv].rx_buf = (uintptr_t) rdbuf;
  183. tr[rv].len = 4096;
  184. tr[rv].delay_usecs = 1;
  185. tr[rv].speed_hz = speed;
  186. tr[rv].bits_per_word = bits;
  187. bufsz -= 4096;
  188. wrbuf += 4096; rdbuf += 4096; rv ++;
  189. }
  190. if (bufsz > 0) {
  191. tr[rv].tx_buf = (uintptr_t) wrbuf;
  192. tr[rv].rx_buf = (uintptr_t) rdbuf;
  193. tr[rv].len = (unsigned)bufsz;
  194. tr[rv].delay_usecs = 1;
  195. tr[rv].speed_hz = speed;
  196. tr[rv].bits_per_word = bits;
  197. rv ++;
  198. }
  199. i = rv;
  200. for (j = 0; j < i; j++) {
  201. rv = (int)ioctl(fd, SPI_IOC_MESSAGE(1), (intptr_t)&tr[j]);
  202. if (rv < 0)
  203. BAILOUT("WTF!");
  204. }
  205. close(fd);
  206. spi_reset(4321);
  207. return true;
  208. }
  209. bool linux_spi_txrx(struct spi_port * const spi)
  210. {
  211. const void * const wrbuf = spi_gettxbuf(spi);
  212. void * const rdbuf = spi_getrxbuf(spi);
  213. const size_t bufsz = spi_getbufsz(spi);
  214. const int fd = spi->fd;
  215. struct spi_ioc_transfer xf = {
  216. .tx_buf = (uintptr_t) wrbuf,
  217. .rx_buf = (uintptr_t) rdbuf,
  218. .len = bufsz,
  219. .delay_usecs = spi->delay,
  220. .speed_hz = spi->speed,
  221. .bits_per_word = spi->bits,
  222. };
  223. return (ioctl(fd, SPI_IOC_MESSAGE(1), &xf) > 0);
  224. }
  225. #endif
  226. static
  227. void *spi_emit_buf_reverse(struct spi_port *port, const void *p, size_t sz)
  228. {
  229. const unsigned char *str = p;
  230. void * const rv = &port->spibuf_rx[port->spibufsz];
  231. if (port->spibufsz + sz >= SPIMAXSZ)
  232. return NULL;
  233. for (size_t i = 0; i < sz; ++i)
  234. {
  235. // Reverse bit order in each byte!
  236. port->spibuf[port->spibufsz++] = bitflip8(str[i]);
  237. }
  238. return rv;
  239. }
  240. void spi_emit_buf(struct spi_port * const port, const void * const str, const size_t sz)
  241. {
  242. if (port->spibufsz + sz >= SPIMAXSZ)
  243. return;
  244. memcpy(&port->spibuf[port->spibufsz], str, sz);
  245. port->spibufsz += sz;
  246. }
  247. /* TODO: in production, emit just bit-sequences! Eliminate padding to byte! */
  248. void spi_emit_break(struct spi_port *port)
  249. {
  250. spi_emit_buf(port, "\x4", 1);
  251. }
  252. void spi_emit_fsync(struct spi_port *port)
  253. {
  254. spi_emit_buf(port, "\x6", 1);
  255. }
  256. void spi_emit_fasync(struct spi_port *port, int n)
  257. {
  258. int i;
  259. for (i = 0; i < n; i++) {
  260. spi_emit_buf(port, "\x5", 1);
  261. }
  262. }
  263. void spi_emit_nop(struct spi_port *port, int n) {
  264. int i;
  265. for (i = 0; i < n; ++i) {
  266. spi_emit_buf(port, "\x0", 1);
  267. }
  268. }
  269. void *spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len)
  270. {
  271. unsigned char otmp[3];
  272. if (len < 4 || len > 128)
  273. return NULL; /* This cannot be programmed in single frame! */
  274. len /= 4; /* Strip */
  275. otmp[0] = (len - 1) | 0xE0;
  276. otmp[1] = (addr >> 8)&0xFF; otmp[2] = addr & 0xFF;
  277. spi_emit_buf(port, otmp, 3);
  278. return spi_emit_buf_reverse(port, buf, len*4);
  279. }
  280. #ifdef USE_BFSB
  281. void spi_bfsb_select_bank(int bank)
  282. {
  283. static int last_bank = -2;
  284. if (bank == last_bank)
  285. return;
  286. const int banks[4]={18,23,24,25}; // GPIO connected to OE of level shifters
  287. int i;
  288. for(i=0;i<4;i++)
  289. {
  290. if (i == bank)
  291. continue;
  292. INP_GPIO(banks[i]);
  293. OUT_GPIO(banks[i]);
  294. GPIO_CLR = 1 << banks[i];
  295. }
  296. if (bank != -1)
  297. {
  298. OUT_GPIO(banks[bank]);
  299. GPIO_SET = 1 << banks[bank];
  300. }
  301. last_bank = bank;
  302. }
  303. #endif