mcp2210.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2014 Con Kolivas
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "miner.h"
  10. #include "usbutils.h"
  11. #include "mcp2210.h"
  12. static bool mcp2210_send(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
  13. {
  14. int amount, err;
  15. if (unlikely(cgpu->usbinfo.nodev))
  16. return false;
  17. err = usb_write(cgpu, buf, MCP2210_BUFFER_LENGTH, &amount, cmd);
  18. if (err || amount != MCP2210_BUFFER_LENGTH) {
  19. applog(LOG_WARNING, "%s %d: Error %d sending %s sent %d of %d",
  20. cgpu->drv->name, cgpu->device_id, err, usb_cmdname(cmd),
  21. amount, MCP2210_BUFFER_LENGTH);
  22. return false;
  23. }
  24. return true;
  25. }
  26. static bool mcp2210_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
  27. {
  28. int amount, err;
  29. if (unlikely(cgpu->usbinfo.nodev))
  30. return false;
  31. err = usb_read(cgpu, buf, MCP2210_BUFFER_LENGTH, &amount, cmd);
  32. if (err || amount != MCP2210_BUFFER_LENGTH) {
  33. applog(LOG_WARNING, "%s %d: Error %d receiving %s received %d of %d",
  34. cgpu->drv->name, cgpu->device_id, err, usb_cmdname(cmd),
  35. amount, MCP2210_BUFFER_LENGTH);
  36. return false;
  37. }
  38. return true;
  39. }
  40. bool mcp2210_send_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
  41. {
  42. uint8_t mcp_cmd = buf[0];
  43. if (!mcp2210_send(cgpu, buf, cmd))
  44. return false;
  45. if (!mcp2210_recv(cgpu, buf, cmd))
  46. return false;
  47. /* Return code should always echo original command */
  48. if (buf[0] != mcp_cmd) {
  49. applog(LOG_WARNING, "%s %d: Response code mismatch, asked for %u got %u",
  50. cgpu->drv->name, cgpu->device_id, mcp_cmd, buf[0]);
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool mcp2210_get_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp)
  56. {
  57. char buf[MCP2210_BUFFER_LENGTH];
  58. int i;
  59. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  60. buf[0] = MCP2210_GET_GPIO_SETTING;
  61. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
  62. return false;
  63. for (i = 0; i < 8; i++) {
  64. mcp->designation.pin[i] = buf[4 + i];
  65. mcp->value.pin[i] = !!(buf[13] & (0x01u << i));
  66. mcp->direction.pin[i] = !!(buf[15] & (0x01u << i));
  67. }
  68. mcp->designation.pin[8] = buf[12];
  69. mcp->value.pin[8] = buf[14] & 0x01u;
  70. mcp->direction.pin[8] = buf[16] & 0x01u;
  71. return true;
  72. }
  73. bool mcp2210_set_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp)
  74. {
  75. char buf[MCP2210_BUFFER_LENGTH];
  76. uint8_t buf17;
  77. int i;
  78. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  79. buf[0] = MCP2210_GET_GPIO_SETTING;
  80. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
  81. return false;
  82. buf17 = buf[17];
  83. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  84. buf[0] = MCP2210_SET_GPIO_SETTING;
  85. buf[17] = buf17;
  86. for (i = 0; i < 8; i++) {
  87. buf[4 + i] = mcp->designation.pin[i];
  88. buf[13] |= mcp->value.pin[i] << i;
  89. buf[15] |= mcp->direction.pin[i] << i;
  90. }
  91. buf[12] = mcp->designation.pin[8];
  92. buf[14] = mcp->value.pin[8];
  93. buf[16] = mcp->direction.pin[8];
  94. return mcp2210_send_recv(cgpu, buf, C_MCP_SETGPIOSETTING);
  95. }
  96. /* Get all the pin designations and store them in a gpio_pin struct */
  97. bool mcp2210_get_gpio_pindes(struct cgpu_info *cgpu, struct gpio_pin *gp)
  98. {
  99. char buf[MCP2210_BUFFER_LENGTH];
  100. int i;
  101. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  102. buf[0] = MCP2210_GET_GPIO_SETTING;
  103. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
  104. return false;
  105. for (i = 0; i < 9; i++)
  106. gp->pin[i] = buf[4 + i];
  107. return true;
  108. }
  109. /* Get all the pin vals and store them in a gpio_pin struct */
  110. bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp)
  111. {
  112. char buf[MCP2210_BUFFER_LENGTH];
  113. int i;
  114. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  115. buf[0] = MCP2210_GET_GPIO_PIN_VAL;
  116. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
  117. return false;
  118. for (i = 0; i < 8; i++)
  119. gp->pin[i] = !!(buf[4] & (0x01u << i));
  120. gp->pin[8] = buf[5] & 0x01u;
  121. return true;
  122. }
  123. /* Get all the pindirs */
  124. bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp)
  125. {
  126. char buf[MCP2210_BUFFER_LENGTH];
  127. int i;
  128. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  129. buf[0] = MCP2210_GET_GPIO_PIN_DIR;
  130. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
  131. return false;
  132. for (i = 0; i < 8; i++)
  133. gp->pin[i] = !!(buf[4] & (0x01u << i));
  134. gp->pin[8] = buf[5] & 0x01u;
  135. return true;
  136. }
  137. /* Get the designation of one pin */
  138. bool mcp2210_get_gpio_pin(struct cgpu_info *cgpu, int pin, int *des)
  139. {
  140. struct gpio_pin gp;
  141. if (!mcp2210_get_gpio_pindes(cgpu, &gp))
  142. return false;
  143. *des = gp.pin[pin];
  144. return true;
  145. }
  146. /* Get one pinval */
  147. bool mcp2210_get_gpio_pinval(struct cgpu_info *cgpu, int pin, int *val)
  148. {
  149. char buf[MCP2210_BUFFER_LENGTH];
  150. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  151. buf[0] = MCP2210_GET_GPIO_PIN_VAL;
  152. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
  153. return false;
  154. buf[0] = MCP2210_GET_GPIO_PIN_VAL;
  155. if (pin < 8)
  156. *val = !!(buf[4] & (0x01u << pin));
  157. else
  158. *val = !!(buf[5] & 0x01u);
  159. return true;
  160. }
  161. /* Get one pindir */
  162. bool mcp2210_get_gpio_pindir(struct cgpu_info *cgpu, int pin, int *dir)
  163. {
  164. char buf[MCP2210_BUFFER_LENGTH];
  165. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  166. buf[0] = MCP2210_GET_GPIO_PIN_DIR;
  167. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
  168. return false;
  169. buf[0] = MCP2210_GET_GPIO_PIN_DIR;
  170. if (pin < 8)
  171. *dir = !!(buf[4] & (0x01u << pin));
  172. else
  173. *dir = !!(buf[5] & 0x01u);
  174. return true;
  175. }
  176. bool mcp2210_spi_cancel(struct cgpu_info *cgpu)
  177. {
  178. char buf[MCP2210_BUFFER_LENGTH];
  179. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  180. buf[0] = MCP2210_SPI_CANCEL;
  181. return mcp2210_send_recv(cgpu, buf, C_MCP_SPICANCEL);
  182. }
  183. /* Abbreviations correspond to:
  184. * IdleChipSelectValue, ActiveChipSelectValue, CSToDataDelay, LastDataByteToCSDelay,
  185. * SubsequentDataByteDelay, BytesPerSPITransfer
  186. */
  187. bool
  188. mcp2210_get_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int *bitrate, unsigned int *icsv,
  189. unsigned int *acsv, unsigned int *cstdd, unsigned int *ldbtcsd,
  190. unsigned int *sdbd, unsigned int *bpst, unsigned int *spimode)
  191. {
  192. char buf[MCP2210_BUFFER_LENGTH];
  193. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  194. buf[0] = MCP2210_GET_SPI_SETTING;
  195. if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETSPISETTING))
  196. return false;
  197. *bitrate = buf[7] << 24 | buf[6] << 16 | buf[5] << 8 | buf[4];
  198. *icsv = (buf[9] & 0x1) << 8 | buf[8];
  199. *acsv = (buf[11] & 0x1) << 8 | buf[10];
  200. *cstdd = buf[13] << 8 | buf[12];
  201. *ldbtcsd = buf[15] << 8 | buf[14];
  202. *sdbd = buf[17] << 8 | buf[16];
  203. *bpst = buf[19] << 8 | buf[18];
  204. *spimode = buf[20];
  205. return true;
  206. }
  207. bool
  208. mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, unsigned int icsv,
  209. unsigned int acsv, unsigned int cstdd, unsigned int ldbtcsd,
  210. unsigned int sdbd, unsigned int bpst, unsigned int spimode)
  211. {
  212. char buf[MCP2210_BUFFER_LENGTH];
  213. bool ret;
  214. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  215. buf[0] = MCP2210_SET_SPI_SETTING;
  216. buf[4] = bitrate & 0xfful;
  217. buf[5] = (bitrate & 0xff00ul) >> 8;
  218. buf[6] = (bitrate & 0xff0000ul) >> 16;
  219. buf[7] = (bitrate & 0xff000000ul) >> 24;
  220. buf[8] = icsv & 0xff;
  221. buf[9] = (icsv & 0x100) >> 8;
  222. buf[10] = acsv & 0xff;
  223. buf[11] = (acsv & 0x100) >> 8;
  224. buf[12] = cstdd & 0xff;
  225. buf[13] = (cstdd & 0xff00) >> 8;
  226. buf[14] = ldbtcsd & 0xff;
  227. buf[15] = (ldbtcsd & 0xff00) >> 8;
  228. buf[16] = sdbd & 0xff;
  229. buf[17] = (sdbd & 0xff00) >> 8;
  230. buf[18] = bpst & 0xff;
  231. buf[19] = (bpst & 0xff00) >> 8;
  232. buf[20] = spimode;
  233. ret = mcp2210_send_recv(cgpu, buf, C_MCP_SETSPISETTING);
  234. if (!ret)
  235. return ret;
  236. if (buf[1] != 0) {
  237. applog(LOG_DEBUG, "Failed to set spi settings");
  238. return false;
  239. }
  240. return true;
  241. }
  242. /* Perform an spi transfer of *length bytes and return the amount of data
  243. * returned in the same buffer in *length */
  244. bool mcp2210_spi_transfer(struct cgpu_info *cgpu, struct mcp_settings *mcp,
  245. char *data, unsigned int *length)
  246. {
  247. uint8_t res, status, orig_len, offset = 0;
  248. char buf[MCP2210_BUFFER_LENGTH];
  249. if (unlikely(*length > MCP2210_TRANSFER_MAX || !*length)) {
  250. applog(LOG_ERR, "%s %d: Unable to spi transfer %u bytes", cgpu->drv->name,
  251. cgpu->device_id, *length);
  252. return false;
  253. }
  254. if (mcp->bpst != *length) {
  255. /* Set the transfer setting only when it changes. */
  256. mcp->bpst = *length;
  257. if (!mcp2210_set_spi_transfer_settings(cgpu, mcp->bitrate, mcp->icsv,
  258. mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode))
  259. return false;
  260. }
  261. orig_len = *length;
  262. retry:
  263. applog(LOG_DEBUG, "%s %d: SPI sending %u bytes", cgpu->drv->name, cgpu->device_id,
  264. *length);
  265. memset(buf, 0, MCP2210_BUFFER_LENGTH);
  266. buf[0] = MCP2210_SPI_TRANSFER;
  267. buf[1] = *length;
  268. if (*length)
  269. memcpy(buf + 4, data + offset, *length);
  270. if (!mcp2210_send_recv(cgpu, buf, C_MCP_SPITRANSFER))
  271. return false;
  272. res = (uint8_t)buf[1];
  273. switch(res) {
  274. case MCP2210_SPI_TRANSFER_SUCCESS:
  275. *length = buf[2];
  276. status = buf[3];
  277. applog(LOG_DEBUG, "%s %d: SPI transfer success, received %u bytes status 0x%x",
  278. cgpu->drv->name, cgpu->device_id, *length, status);
  279. if (*length) {
  280. memcpy(data + offset, buf + 4, *length);
  281. offset += *length;
  282. }
  283. if (status == 0x30) {
  284. /* This shouldn't happen */
  285. applog(LOG_DEBUG, "%s %d: SPI expecting more data inappropriately",
  286. cgpu->drv->name, cgpu->device_id);
  287. return false;
  288. }
  289. if (offset < orig_len) {
  290. *length = 0;
  291. goto retry;
  292. }
  293. *length = orig_len;
  294. return true;
  295. case MCP2210_SPI_TRANSFER_ERROR_IP:
  296. applog(LOG_DEBUG, "%s %d: SPI transfer error in progress",
  297. cgpu->drv->name, cgpu->device_id);
  298. goto retry;
  299. case MCP2210_SPI_TRANSFER_ERROR_NA:
  300. applog(LOG_WARNING, "%s %d: External owner error on mcp2210 spi transfer",
  301. cgpu->drv->name, cgpu->device_id);
  302. default:
  303. return false;
  304. }
  305. }