bf16-gpiodevice.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <fcntl.h>
  2. #include <sys/mman.h>
  3. #include <unistd.h>
  4. #include "bf16-gpiodevice.h"
  5. #include "driver-bitfury16.h"
  6. #include "miner.h"
  7. typedef struct {
  8. unsigned int* oe_addr; // R/W, bit==1 -> input, bit==0 -> output,
  9. unsigned int* data_out; // R/W
  10. unsigned int* data_set; // W/O
  11. unsigned int* data_clr; // W/O
  12. unsigned int* data_in; // R/O
  13. } gpio_attr_t;
  14. typedef struct {
  15. unsigned int* data_control;
  16. } ctrl_attr_t;
  17. typedef struct {
  18. gpio_attr_t gpio0;
  19. gpio_attr_t gpio1;
  20. gpio_attr_t gpio2;
  21. gpio_attr_t gpio3;
  22. ctrl_attr_t* ctrl;
  23. } mmap_attr_t;
  24. static mmap_attr_t mmap_base;
  25. char *gpio_device_name = "/dev/mem";
  26. int8_t gpio_write_ctrl(gpio_rq_t* rq)
  27. {
  28. if (rq->gpioIndex < MAX_GPIO_INDEX && rq->regIndex < MAX_REGISTER_INDEX) {
  29. gpio_attr_t* gpio_addr = (void*) &mmap_base;
  30. gpio_attr_t gpio = gpio_addr[rq->gpioIndex];
  31. unsigned int** addr = (void*) &gpio;
  32. *addr[rq->regIndex] = rq->data;
  33. return 0;
  34. }
  35. return -1;
  36. }
  37. int gpio_read_ctrl(gpio_rq_t* rq)
  38. {
  39. if (rq->gpioIndex < MAX_GPIO_INDEX && rq->regIndex < MAX_REGISTER_INDEX) {
  40. gpio_attr_t* gpio_addr = (void*) &mmap_base;
  41. gpio_attr_t gpio = gpio_addr[rq->gpioIndex];
  42. unsigned int** addr = (void*) &gpio;
  43. rq->data = *addr[rq->regIndex];
  44. return 0;
  45. }
  46. return -1;
  47. }
  48. void* map_gpio(gpio_attr_t* gpioAttr, void* gpio)
  49. {
  50. gpioAttr->oe_addr = gpio + GPIO_OE;
  51. gpioAttr->data_out = gpio + GPIO_DATAOUT;
  52. gpioAttr->data_set = gpio + GPIO_SETDATAOUT;
  53. gpioAttr->data_clr = gpio + GPIO_CLEARDATAOUT;
  54. gpioAttr->data_in = gpio + GPIO_DATAIN;
  55. return gpio;
  56. }
  57. void* mmap_open(const char *device, uint32_t base, uint32_t size)
  58. {
  59. int fd = open(device, O_RDWR | O_SYNC);
  60. if (fd < 0)
  61. quit(1, "Failed to open /dev/mem: %s", strerror(errno));
  62. void* mmap_addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
  63. if (mmap_addr == MAP_FAILED)
  64. quit(1, "Failed to mmap GPIO [%08x]: %s", base, strerror(errno));
  65. close(fd);
  66. return mmap_addr;
  67. }
  68. int8_t gpio_init(device_t* attr, char *device, uint16_t size)
  69. {
  70. attr->device = device;
  71. attr->mode = 0;
  72. attr->speed = 0;
  73. attr->bits = 0;
  74. attr->size = size;
  75. attr->rx = malloc(size);
  76. attr->tx = malloc(size);
  77. map_gpio(&mmap_base.gpio0, mmap_open(device, GPIO0_START_ADDR, GPIO_SIZE));
  78. map_gpio(&mmap_base.gpio1, mmap_open(device, GPIO1_START_ADDR, GPIO_SIZE));
  79. map_gpio(&mmap_base.gpio2, mmap_open(device, GPIO2_START_ADDR, GPIO_SIZE));
  80. map_gpio(&mmap_base.gpio3, mmap_open(device, GPIO3_START_ADDR, GPIO_SIZE));
  81. return 0;
  82. }
  83. void gpio_release(device_t *attr)
  84. {
  85. free(attr->rx);
  86. free(attr->tx);
  87. }