rayer_spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /* Driver for various LPT adapters.
  20. *
  21. * This driver uses non-portable direct I/O port accesses which won't work on
  22. * any non-x86 platform, and even on x86 there is a high chance there will be
  23. * collisions with any loaded parallel port drivers.
  24. * The big advantage of direct port I/O is OS independence and speed because
  25. * most OS parport drivers will perform many unnecessary accesses although
  26. * this driver just treats the parallel port as a GPIO set.
  27. */
  28. #if defined(__i386__) || defined(__x86_64__)
  29. #include <stdlib.h>
  30. #include <strings.h>
  31. #include <string.h>
  32. #include "flash.h"
  33. #include "programmer.h"
  34. #include "hwaccess.h"
  35. /* We have two sets of pins, out and in. The numbers for both sets are
  36. * independent and are bitshift values, not real pin numbers.
  37. * Default settings are for the RayeR hardware.
  38. */
  39. struct rayer_programmer {
  40. const char *type;
  41. const enum test_state status;
  42. const char *description;
  43. const void *dev_data;
  44. };
  45. struct rayer_pinout {
  46. uint8_t cs_bit;
  47. uint8_t sck_bit;
  48. uint8_t mosi_bit;
  49. uint8_t miso_bit;
  50. void (*preinit)(const void *);
  51. int (*shutdown)(void *);
  52. };
  53. static const struct rayer_pinout rayer_spipgm = {
  54. .cs_bit = 5,
  55. .sck_bit = 6,
  56. .mosi_bit = 7,
  57. .miso_bit = 6,
  58. };
  59. static void dlc5_preinit(const void *);
  60. static int dlc5_shutdown(void *);
  61. static const struct rayer_pinout xilinx_dlc5 = {
  62. .cs_bit = 2,
  63. .sck_bit = 1,
  64. .mosi_bit = 0,
  65. .miso_bit = 4,
  66. .preinit = dlc5_preinit,
  67. .shutdown = dlc5_shutdown,
  68. };
  69. static void byteblaster_preinit(const void *);
  70. static int byteblaster_shutdown(void *);
  71. static const struct rayer_pinout altera_byteblastermv = {
  72. .cs_bit = 1,
  73. .sck_bit = 0,
  74. .mosi_bit = 6,
  75. .miso_bit = 7,
  76. .preinit = byteblaster_preinit,
  77. .shutdown = byteblaster_shutdown,
  78. };
  79. static void stk200_preinit(const void *);
  80. static int stk200_shutdown(void *);
  81. static const struct rayer_pinout atmel_stk200 = {
  82. .cs_bit = 7,
  83. .sck_bit = 4,
  84. .mosi_bit = 5,
  85. .miso_bit = 6,
  86. .preinit = stk200_preinit,
  87. .shutdown = stk200_shutdown,
  88. };
  89. static const struct rayer_pinout wiggler_lpt = {
  90. .cs_bit = 1,
  91. .sck_bit = 2,
  92. .mosi_bit = 3,
  93. .miso_bit = 7,
  94. };
  95. static const struct rayer_pinout spi_tt = {
  96. .cs_bit = 2,
  97. .sck_bit = 0,
  98. .mosi_bit = 4,
  99. .miso_bit = 7,
  100. };
  101. static const struct rayer_programmer rayer_spi_types[] = {
  102. {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm},
  103. {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5},
  104. {"byteblastermv", OK, "Altera ByteBlasterMV", &altera_byteblastermv},
  105. {"stk200", NT, "Atmel STK200/300 adapter", &atmel_stk200},
  106. {"wiggler", OK, "Wiggler LPT", &wiggler_lpt},
  107. {"spi_tt", NT, "SPI Tiny Tools (SPI_TT LPT)", &spi_tt},
  108. {0},
  109. };
  110. static const struct rayer_pinout *pinout = NULL;
  111. static uint16_t lpt_iobase;
  112. /* Cached value of last byte sent. */
  113. static uint8_t lpt_outbyte;
  114. static void rayer_bitbang_set_cs(int val)
  115. {
  116. lpt_outbyte &= ~(1 << pinout->cs_bit);
  117. lpt_outbyte |= (val << pinout->cs_bit);
  118. OUTB(lpt_outbyte, lpt_iobase);
  119. }
  120. static void rayer_bitbang_set_sck(int val)
  121. {
  122. lpt_outbyte &= ~(1 << pinout->sck_bit);
  123. lpt_outbyte |= (val << pinout->sck_bit);
  124. OUTB(lpt_outbyte, lpt_iobase);
  125. }
  126. static void rayer_bitbang_set_mosi(int val)
  127. {
  128. lpt_outbyte &= ~(1 << pinout->mosi_bit);
  129. lpt_outbyte |= (val << pinout->mosi_bit);
  130. OUTB(lpt_outbyte, lpt_iobase);
  131. }
  132. static int rayer_bitbang_get_miso(void)
  133. {
  134. uint8_t tmp;
  135. tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted
  136. tmp = (tmp >> pinout->miso_bit) & 0x1;
  137. return tmp;
  138. }
  139. static const struct bitbang_spi_master bitbang_spi_master_rayer = {
  140. .type = BITBANG_SPI_MASTER_RAYER,
  141. .set_cs = rayer_bitbang_set_cs,
  142. .set_sck = rayer_bitbang_set_sck,
  143. .set_mosi = rayer_bitbang_set_mosi,
  144. .get_miso = rayer_bitbang_get_miso,
  145. .half_period = 0,
  146. };
  147. int rayer_spi_init(void)
  148. {
  149. const struct rayer_programmer *prog = rayer_spi_types;
  150. char *arg = NULL;
  151. /* Non-default port requested? */
  152. arg = extract_programmer_param("iobase");
  153. if (arg) {
  154. char *endptr = NULL;
  155. unsigned long tmp;
  156. tmp = strtoul(arg, &endptr, 0);
  157. /* Port 0, port >0x10000, unaligned ports and garbage strings
  158. * are rejected.
  159. */
  160. if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
  161. (*endptr != '\0')) {
  162. /* Using ports below 0x100 is a really bad idea, and
  163. * should only be done if no port between 0x100 and
  164. * 0xfffc works due to routing issues.
  165. */
  166. msg_perr("Error: iobase= specified, but the I/O base "
  167. "given was invalid.\nIt must be a multiple of "
  168. "0x4 and lie between 0x100 and 0xfffc.\n");
  169. free(arg);
  170. return 1;
  171. } else {
  172. lpt_iobase = (uint16_t)tmp;
  173. msg_pinfo("Non-default I/O base requested. This will "
  174. "not change the hardware settings.\n");
  175. }
  176. } else {
  177. /* Pick a default value for the I/O base. */
  178. lpt_iobase = 0x378;
  179. }
  180. free(arg);
  181. msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
  182. lpt_iobase);
  183. arg = extract_programmer_param("type");
  184. if (arg) {
  185. for (; prog->type != NULL; prog++) {
  186. if (strcasecmp(arg, prog->type) == 0) {
  187. break;
  188. }
  189. }
  190. if (prog->type == NULL) {
  191. msg_perr("Error: Invalid device type specified.\n");
  192. free(arg);
  193. return 1;
  194. }
  195. free(arg);
  196. }
  197. msg_pinfo("Using %s pinout.\n", prog->description);
  198. pinout = (struct rayer_pinout *)prog->dev_data;
  199. if (rget_io_perms())
  200. return 1;
  201. /* Get the initial value before writing to any line. */
  202. lpt_outbyte = INB(lpt_iobase);
  203. if (pinout->shutdown)
  204. register_shutdown(pinout->shutdown, (void*)pinout);
  205. if (pinout->preinit)
  206. pinout->preinit(pinout);
  207. if (register_spi_bitbang_master(&bitbang_spi_master_rayer))
  208. return 1;
  209. return 0;
  210. }
  211. static void byteblaster_preinit(const void *data){
  212. msg_pdbg("byteblaster_preinit\n");
  213. /* Assert #EN signal. */
  214. OUTB(2, lpt_iobase + 2 );
  215. }
  216. static int byteblaster_shutdown(void *data){
  217. msg_pdbg("byteblaster_shutdown\n");
  218. /* De-Assert #EN signal. */
  219. OUTB(0, lpt_iobase + 2 );
  220. return 0;
  221. }
  222. static void stk200_preinit(const void *data) {
  223. msg_pdbg("stk200_init\n");
  224. /* Assert #EN signals, set LED signal. */
  225. lpt_outbyte = (1 << 6) ;
  226. OUTB(lpt_outbyte, lpt_iobase);
  227. }
  228. static int stk200_shutdown(void *data) {
  229. msg_pdbg("stk200_shutdown\n");
  230. /* Assert #EN signals, clear LED signal. */
  231. lpt_outbyte = (1 << 2) | (1 << 3);
  232. OUTB(lpt_outbyte, lpt_iobase);
  233. return 0;
  234. }
  235. static void dlc5_preinit(const void *data) {
  236. msg_pdbg("dlc5_preinit\n");
  237. /* Assert pin 6 to receive MISO. */
  238. lpt_outbyte |= (1<<4);
  239. OUTB(lpt_outbyte, lpt_iobase);
  240. }
  241. static int dlc5_shutdown(void *data) {
  242. msg_pdbg("dlc5_shutdown\n");
  243. /* De-assert pin 6 to force MISO low. */
  244. lpt_outbyte &= ~(1<<4);
  245. OUTB(lpt_outbyte, lpt_iobase);
  246. return 0;
  247. }
  248. #else
  249. #error PCI port I/O access is not supported on this architecture yet.
  250. #endif