rayer_spi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 the SPIPGM hardware by "RayeR" Martin Rehak.
  20. * See http://rayer.ic.cz/elektro/spipgm.htm for schematics and instructions.
  21. */
  22. /* This driver uses non-portable direct I/O port accesses which won't work on
  23. * any non-x86 platform, and even on x86 there is a high chance there will be
  24. * collisions with any loaded parallel port drivers.
  25. * The big advantage of direct port I/O is OS independence and speed because
  26. * most OS parport drivers will perform many unnecessary accesses although
  27. * this driver just treats the parallel port as a GPIO set.
  28. */
  29. #if defined(__i386__) || defined(__x86_64__)
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "flash.h"
  33. #include "programmer.h"
  34. enum rayer_type {
  35. TYPE_RAYER,
  36. TYPE_XILINX_DLC5,
  37. };
  38. /* We have two sets of pins, out and in. The numbers for both sets are
  39. * independent and are bitshift values, not real pin numbers.
  40. * Default settings are for the RayeR hardware.
  41. */
  42. /* Pins for master->slave direction */
  43. static int rayer_cs_bit = 5;
  44. static int rayer_sck_bit = 6;
  45. static int rayer_mosi_bit = 7;
  46. /* Pins for slave->master direction */
  47. static int rayer_miso_bit = 6;
  48. static uint16_t lpt_iobase;
  49. /* Cached value of last byte sent. */
  50. static uint8_t lpt_outbyte;
  51. static void rayer_bitbang_set_cs(int val)
  52. {
  53. lpt_outbyte &= ~(1 << rayer_cs_bit);
  54. lpt_outbyte |= (val << rayer_cs_bit);
  55. OUTB(lpt_outbyte, lpt_iobase);
  56. }
  57. static void rayer_bitbang_set_sck(int val)
  58. {
  59. lpt_outbyte &= ~(1 << rayer_sck_bit);
  60. lpt_outbyte |= (val << rayer_sck_bit);
  61. OUTB(lpt_outbyte, lpt_iobase);
  62. }
  63. static void rayer_bitbang_set_mosi(int val)
  64. {
  65. lpt_outbyte &= ~(1 << rayer_mosi_bit);
  66. lpt_outbyte |= (val << rayer_mosi_bit);
  67. OUTB(lpt_outbyte, lpt_iobase);
  68. }
  69. static int rayer_bitbang_get_miso(void)
  70. {
  71. uint8_t tmp;
  72. tmp = INB(lpt_iobase + 1);
  73. tmp = (tmp >> rayer_miso_bit) & 0x1;
  74. return tmp;
  75. }
  76. static const struct bitbang_spi_master bitbang_spi_master_rayer = {
  77. .type = BITBANG_SPI_MASTER_RAYER,
  78. .set_cs = rayer_bitbang_set_cs,
  79. .set_sck = rayer_bitbang_set_sck,
  80. .set_mosi = rayer_bitbang_set_mosi,
  81. .get_miso = rayer_bitbang_get_miso,
  82. };
  83. int rayer_spi_init(void)
  84. {
  85. char *arg = NULL;
  86. enum rayer_type rayer_type = TYPE_RAYER;
  87. /* Non-default port requested? */
  88. arg = extract_programmer_param("iobase");
  89. if (arg) {
  90. char *endptr = NULL;
  91. unsigned long tmp;
  92. tmp = strtoul(arg, &endptr, 0);
  93. /* Port 0, port >0x10000, unaligned ports and garbage strings
  94. * are rejected.
  95. */
  96. if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
  97. (*endptr != '\0')) {
  98. /* Using ports below 0x100 is a really bad idea, and
  99. * should only be done if no port between 0x100 and
  100. * 0xfffc works due to routing issues.
  101. */
  102. msg_perr("Error: iobase= specified, but the I/O base "
  103. "given was invalid.\nIt must be a multiple of "
  104. "0x4 and lie between 0x100 and 0xfffc.\n");
  105. free(arg);
  106. return 1;
  107. } else {
  108. lpt_iobase = (uint16_t)tmp;
  109. msg_pinfo("Non-default I/O base requested. This will "
  110. "not change the hardware settings.\n");
  111. }
  112. } else {
  113. /* Pick a default value for the I/O base. */
  114. lpt_iobase = 0x378;
  115. }
  116. free(arg);
  117. msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
  118. lpt_iobase);
  119. arg = extract_programmer_param("type");
  120. if (arg) {
  121. if (!strcasecmp(arg, "rayer")) {
  122. rayer_type = TYPE_RAYER;
  123. } else if (!strcasecmp(arg, "xilinx")) {
  124. rayer_type = TYPE_XILINX_DLC5;
  125. } else {
  126. msg_perr("Error: Invalid device type specified.\n");
  127. free(arg);
  128. return 1;
  129. }
  130. }
  131. free(arg);
  132. switch (rayer_type) {
  133. case TYPE_RAYER:
  134. msg_pdbg("Using RayeR SPIPGM pinout.\n");
  135. /* Bits for master->slave direction */
  136. rayer_cs_bit = 5;
  137. rayer_sck_bit = 6;
  138. rayer_mosi_bit = 7;
  139. /* Bits for slave->master direction */
  140. rayer_miso_bit = 6;
  141. break;
  142. case TYPE_XILINX_DLC5:
  143. msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n");
  144. /* Bits for master->slave direction */
  145. rayer_cs_bit = 2;
  146. rayer_sck_bit = 1;
  147. rayer_mosi_bit = 0;
  148. /* Bits for slave->master direction */
  149. rayer_miso_bit = 4;
  150. }
  151. get_io_perms();
  152. /* Get the initial value before writing to any line. */
  153. lpt_outbyte = INB(lpt_iobase);
  154. /* Zero halfperiod delay. */
  155. if (bitbang_spi_init(&bitbang_spi_master_rayer, 0))
  156. return 1;
  157. return 0;
  158. }
  159. #else
  160. #error PCI port I/O access is not supported on this architecture yet.
  161. #endif