blob: 676f2fe1d173fb738ca838baac3832aaaa09d1d2 [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
2 * Copyright 2012-2015 Google Inc.
3 *
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; version 2 of the License.
7 *
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 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
21#include "em100.h"
22
23/* FPGA related operations */
24
25/**
26 * reconfig_fpga: Reconfigures FPGA after a change(?)
27 * @param em100: initialized em100 device structure
28 *
29 * out(16 bytes): 0x20 0 .. 0
30 */
31int reconfig_fpga(struct em100 *em100)
32{
33 unsigned char cmd[16];
34 memset(cmd, 0, 16);
35 cmd[0] = 0x20; /* reconfig FPGA */
36 if (!send_cmd(em100->dev, cmd)) {
37 return 0;
38 }
39 /* Specification says to wait 2s before
40 * issuing another USB command
41 */
42 sleep(2);
43 return 1;
44}
45
46/**
47 * check_fpga_status: Checkl FPGA configuration status
48 * @param em100: initialized em100 device structure
49 *
50 * out(16 bytes): 0x21 0 .. 0
51 * in(result): pass: 1, fail: 0
52 */
53int check_fpga_status(struct em100 *em100)
54{
55 unsigned char cmd[16];
56 unsigned char data[512];
57 printf("FPGA configuration status: ");
58 memset(cmd, 0, 16);
59 cmd[0] = 0x21; /* Check FPGA status */
60 if (!send_cmd(em100->dev, cmd)) {
61 printf("Unknown\n");
62 return 0;
63 }
64 int len = get_response(em100->dev, data, 512);
65 if (len == 1) {
66 printf("%s\n", data[0] == 1 ? "PASS" : "FAIL");
67 return 1;
68 }
69 printf("Unknown\n");
70 return 0;
71}
72
73/**
74 * read_fpga_register: Read FPGA registers
75 * @param em100: initialized em100 device structure
76 * @param reg: FPGA register to write
77 * @param val: pointer to value
78 *
79 * out(2 bytes): 0x22 RegAddr .. 0
80 * in(len + 2 bytes): 0x02 val val
81 */
82int read_fpga_register(struct em100 *em100, int reg, uint16_t *val)
83{
84 unsigned char cmd[16];
85 unsigned char data[256];
86
87 memset(cmd, 0, 16);
88 cmd[0] = 0x22; /* Read FPGA register */
89 cmd[1] = reg;
90 if (!send_cmd(em100->dev, cmd)) {
91 return 0;
92 }
93 int len = get_response(em100->dev, data, 3);
94 if ((len == 3) && (data[0] == 2)) {
95 *val = (data[1] << 8) + data[2];
96 return 1;
97 }
98 return 0;
99}
100
101/**
102 * write_fpga_register: Write FPGA registers
103 * @param em100: initialized em100 device structure
104 *
105 * out(4 bytes): 0x23 RegAddr Val Val .. 0
106 */
107int write_fpga_register(struct em100 *em100, int reg, int val)
108{
109 unsigned char cmd[16];
110 memset(cmd, 0, 16);
111 cmd[0] = 0x23; /* Write FPGA register */
112 cmd[1] = reg;
113 cmd[2] = val >> 8;
114 cmd[3] = val & 0xff;
115 if (!send_cmd(em100->dev, cmd)) {
116 return 0;
117 }
118 return 1;
119}