pcibx_device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. Catalyst PCIBX32 PCI Extender control utility
  3. Copyright (c) 2006-2009 Michael Buesch <mb@bu3sch.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "pcibx_device.h"
  18. #include "pcibx.h"
  19. #include "utils.h"
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/ioctl.h>
  26. #ifdef __linux__
  27. # include <linux/ppdev.h>
  28. #endif
  29. /* Parport control register bits */
  30. #define PPCTL_IRQEN (1 << 4)
  31. #define PPCTL_READ (1 << 5)
  32. #define PPCTL_DATAMASK 0xF
  33. static uint8_t parport_read_data(struct pcibx_device *dev)
  34. {
  35. uint8_t res = 0;
  36. #if defined(__linux__)
  37. if (ioctl(dev->fd, PPRDATA, &res))
  38. prerror("Failed to read the parallel port data register\n");
  39. #else
  40. # error "Operating system not supported"
  41. #endif
  42. return res;
  43. }
  44. static void parport_write_data(struct pcibx_device *dev, uint8_t value)
  45. {
  46. #if defined(__linux__)
  47. if (ioctl(dev->fd, PPWDATA, &value))
  48. prerror("Failed to write the parallel port data register\n");
  49. #else
  50. # error "Operating system not supported"
  51. #endif
  52. }
  53. static void parport_write_control(struct pcibx_device *dev,
  54. uint8_t mask, uint8_t value)
  55. {
  56. #if defined(__linux__)
  57. struct ppdev_frob_struct frob = {
  58. .mask = mask,
  59. .val = value,
  60. };
  61. int direction;
  62. if (mask & PPCTL_READ) {
  63. direction = !!(value & PPCTL_READ);
  64. if (ioctl(dev->fd, PPDATADIR, &direction))
  65. prerror("Failed to set parallel port data direction\n");
  66. }
  67. frob.mask &= ~PPCTL_READ;
  68. frob.val &= frob.mask;
  69. if (ioctl(dev->fd, PPFCONTROL, &frob))
  70. prerror("Failed to write the parallel port control register\n");
  71. #else
  72. # error "Operating system not supported"
  73. #endif
  74. }
  75. static int parport_open(struct pcibx_device *dev, const char *port)
  76. {
  77. #if defined(__linux__)
  78. int err;
  79. dev->fd = open(port, O_RDWR);
  80. if (dev->fd < 0) {
  81. prerror("Could not open parallel port %s: %s\n",
  82. port, strerror(errno));
  83. return -1;
  84. }
  85. //FIXME
  86. #if 0
  87. err = ioctl(dev->fd, PPEXCL);
  88. if (err) {
  89. prerror("Failed to gain exclusive access to the parallel port %s: %s\n",
  90. port, strerror(err < 0 ? -err : err));
  91. close(dev->fd);
  92. return -1;
  93. }
  94. #endif
  95. err = ioctl(dev->fd, PPCLAIM);
  96. if (err) {
  97. prerror("Failed to claim the parallel port %s: %s\n",
  98. port, strerror(err < 0 ? -err : err));
  99. close(dev->fd);
  100. return -1;
  101. }
  102. #else
  103. # error "Operating system not supported"
  104. #endif
  105. parport_write_control(dev, PPCTL_DATAMASK | PPCTL_READ | PPCTL_IRQEN, 0xE);
  106. return 0;
  107. }
  108. static void parport_close(struct pcibx_device *dev)
  109. {
  110. #if defined(__linux__)
  111. ioctl(dev->fd, PPRELEASE);
  112. close(dev->fd);
  113. #else
  114. # error "Operating system not supported"
  115. #endif
  116. }
  117. static void pcibx_set_address(struct pcibx_device *dev,
  118. uint8_t address)
  119. {
  120. parport_write_control(dev, PPCTL_DATAMASK, 0xE);
  121. parport_write_data(dev, address + dev->regoffset);
  122. parport_write_control(dev, PPCTL_DATAMASK, 0x6);
  123. udelay(100);
  124. parport_write_control(dev, PPCTL_DATAMASK, 0xE);
  125. }
  126. static void pcibx_write_data(struct pcibx_device *dev,
  127. uint8_t data)
  128. {
  129. parport_write_data(dev, data);
  130. parport_write_control(dev, PPCTL_DATAMASK, 0xC);
  131. udelay(100);
  132. parport_write_control(dev, PPCTL_DATAMASK, 0xE);
  133. }
  134. static void pcibx_write_data_ext(struct pcibx_device *dev,
  135. uint8_t data)
  136. {
  137. parport_write_control(dev, PPCTL_DATAMASK, 0xE);
  138. parport_write_data(dev, data);
  139. parport_write_control(dev, PPCTL_DATAMASK, 0xC);
  140. msleep(2);
  141. parport_write_control(dev, PPCTL_DATAMASK, 0xE);
  142. }
  143. static uint8_t pcibx_read_data(struct pcibx_device *dev)
  144. {
  145. uint8_t v;
  146. parport_write_control(dev, PPCTL_DATAMASK | PPCTL_READ,
  147. PPCTL_READ | 0xF);
  148. v = parport_read_data(dev);
  149. parport_write_control(dev, PPCTL_DATAMASK | PPCTL_READ, 0xE);
  150. return v;
  151. }
  152. static void pcibx_write(struct pcibx_device *dev,
  153. uint8_t reg,
  154. uint8_t value)
  155. {
  156. pcibx_set_address(dev, reg);
  157. pcibx_write_data(dev, value);
  158. }
  159. static void pcibx_write_ext(struct pcibx_device *dev,
  160. uint8_t reg,
  161. uint8_t value)
  162. {
  163. pcibx_set_address(dev, reg);
  164. pcibx_write_data_ext(dev, value);
  165. }
  166. static uint8_t pcibx_read(struct pcibx_device *dev,
  167. uint8_t reg)
  168. {
  169. pcibx_set_address(dev, reg);
  170. return pcibx_read_data(dev);
  171. }
  172. int pcibx_device_init(struct pcibx_device *dev,
  173. const char *port,
  174. int is_pci1)
  175. {
  176. memset(dev, 0, sizeof(*dev));
  177. if (is_pci1)
  178. dev->regoffset = PCIBX_REGOFFSET_PCI1;
  179. else
  180. dev->regoffset = PCIBX_REGOFFSET_PCI2;
  181. return parport_open(dev, port);
  182. }
  183. void pcibx_device_exit(struct pcibx_device *dev)
  184. {
  185. parport_close(dev);
  186. memset(dev, 0, sizeof(*dev));
  187. }
  188. static void prsendinfo(const char *command)
  189. {
  190. if (cmdargs.verbose >= 2)
  191. prinfo("Sending command: %s\n", command);
  192. }
  193. void pcibx_cmd_global_pwr(struct pcibx_device *dev, int on)
  194. {
  195. if (on) {
  196. prsendinfo("Global Power ON");
  197. pcibx_write(dev, PCIBX_REG_GLOBALPWR, 1);
  198. } else {
  199. prsendinfo("Global Power OFF");
  200. pcibx_write(dev, PCIBX_REG_GLOBALPWR, 0);
  201. }
  202. }
  203. void pcibx_cmd_uut_pwr(struct pcibx_device *dev, int on)
  204. {
  205. if (on) {
  206. pcibx_cmd_global_pwr(dev, 1);
  207. prsendinfo("UUT Voltages ON");
  208. pcibx_write(dev, PCIBX_REG_UUTVOLT, 0);
  209. /* Wait for the RST# to become de-asserted. */
  210. do {
  211. msleep(200);
  212. } while (!(pcibx_read(dev, PCIBX_REG_STATUS) & PCIBX_STATUS_RSTDEASS));
  213. } else {
  214. prsendinfo("UUT Voltages OFF");
  215. pcibx_write(dev, PCIBX_REG_UUTVOLT, 1);
  216. }
  217. }
  218. uint8_t pcibx_cmd_getboardid(struct pcibx_device *dev)
  219. {
  220. prsendinfo("Get board ID");
  221. return pcibx_read(dev, PCIBX_REG_BOARDID);
  222. }
  223. uint8_t pcibx_cmd_getfirmrev(struct pcibx_device *dev)
  224. {
  225. prsendinfo("Get firmware rev");
  226. return pcibx_read(dev, PCIBX_REG_FIRMREV);
  227. }
  228. uint8_t pcibx_cmd_getstatus(struct pcibx_device *dev)
  229. {
  230. prsendinfo("Get status bits");
  231. return pcibx_read(dev, PCIBX_REG_STATUS);
  232. }
  233. void pcibx_cmd_clearbitstat(struct pcibx_device *dev)
  234. {
  235. prsendinfo("Clear 32/64 bit status");
  236. pcibx_write(dev, PCIBX_REG_CLEARBITSTAT, 0);
  237. }
  238. void pcibx_cmd_aux5(struct pcibx_device *dev, int on)
  239. {
  240. if (on) {
  241. prsendinfo("Aux 5V ON");
  242. pcibx_write(dev, PCIBX_REG_AUX5V, 0);
  243. } else {
  244. prsendinfo("Aux 5V OFF");
  245. pcibx_write(dev, PCIBX_REG_AUX5V, 1);
  246. }
  247. }
  248. void pcibx_cmd_aux33(struct pcibx_device *dev, int on)
  249. {
  250. if (on) {
  251. prsendinfo("Aux 3.3V ON");
  252. pcibx_write(dev, PCIBX_REG_AUX33V, 0);
  253. } else {
  254. prsendinfo("Aux 3.3V OFF");
  255. pcibx_write(dev, PCIBX_REG_AUX33V, 1);
  256. }
  257. }
  258. float pcibx_cmd_sysfreq(struct pcibx_device *dev)
  259. {
  260. float mhz;
  261. uint32_t tmp;
  262. uint32_t v;
  263. prsendinfo("Measure system frequency");
  264. pcibx_write(dev, PCIBX_REG_FREQMEASURE_CTL, 1);
  265. msleep(15);
  266. v = pcibx_read(dev, PCIBX_REG_FREQMEASURE_0);
  267. tmp = v;
  268. v = pcibx_read(dev, PCIBX_REG_FREQMEASURE_1);
  269. tmp |= (v << 8);
  270. v = pcibx_read(dev, PCIBX_REG_FREQMEASURE_2);
  271. tmp |= (v << 16);
  272. mhz = (float)tmp * 100.0 / 1048575.0;
  273. return mhz;
  274. }
  275. float pcibx_cmd_measure(struct pcibx_device *dev, enum measure_id id)
  276. {
  277. float ret;
  278. int i;
  279. uint16_t d0, d1;
  280. uint16_t tmp;
  281. prsendinfo("Measuring V/A");
  282. pcibx_write(dev, PCIBX_REG_MEASURE_CTL, id);
  283. msleep(10);
  284. pcibx_write_ext(dev, PCIBX_REG_MEASURE_CONV, 0);
  285. msleep(2);
  286. pcibx_set_address(dev, PCIBX_REG_MEASURE_STROBE);
  287. for (i = 0; i < 13; i++)
  288. pcibx_write_data(dev, 0);
  289. d0 = pcibx_read(dev, PCIBX_REG_MEASURE_DATA0);
  290. d1 = pcibx_read(dev, PCIBX_REG_MEASURE_DATA1);
  291. tmp = d0;
  292. tmp |= (d1 << 8);
  293. if (id == MEASURE_V12UUT)
  294. ret = (float)tmp * 5.75 * 2.5 / 4096.0;
  295. else
  296. ret = (float)tmp * 2.26 * 2.5 / 4096.0;
  297. return ret;
  298. }
  299. void pcibx_cmd_ramp(struct pcibx_device *dev, int fast)
  300. {
  301. prsendinfo("+5V RAMP");
  302. pcibx_write(dev, PCIBX_REG_RAMP, fast ? 1 : 0);
  303. }
  304. void pcibx_cmd_rst(struct pcibx_device *dev, double sec)
  305. {
  306. uint32_t tmp;
  307. prsendinfo("RST#");
  308. sec /= 2.56;
  309. sec *= 1000000.0;
  310. tmp = sec;
  311. tmp &= 0x1FFFFF;
  312. tmp |= (1 << 23);
  313. pcibx_write(dev, PCIBX_REG_RST_0, (tmp & 0x000000FF));
  314. pcibx_write(dev, PCIBX_REG_RST_1, (tmp & 0x0000FF00) >> 8);
  315. pcibx_write(dev, PCIBX_REG_RST_2, (tmp & 0x00FF0000) >> 16);
  316. }
  317. void pcibx_cmd_rstdefault(struct pcibx_device *dev)
  318. {
  319. prsendinfo("default RST#");
  320. pcibx_write(dev, PCIBX_REG_RST_0, 0);
  321. pcibx_write(dev, PCIBX_REG_RST_1, 0);
  322. pcibx_write(dev, PCIBX_REG_RST_2, 0);
  323. }
  324. uint8_t pcibx_cmd_getpme(struct pcibx_device *dev)
  325. {
  326. prsendinfo("PME# status");
  327. return pcibx_read(dev, PCIBX_REG_PME);
  328. }