blob: 2fecc3bc02f4f5023ddb4bb6e542610a8b295870 [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
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauerc566d202015-08-25 09:52:42 -070016 */
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}
Simon Glass98cd62c2018-12-26 14:31:20 -0700120
121int fpga_set_voltage(struct em100 *em100, int voltage_code)
122{
123 unsigned char cmd[16];
124
125 memset(cmd, '\0', 16);
126 cmd[0] = 0x24; /* Switch FPGA */
127 if (voltage_code == 18) {
128 cmd[2] = 7;
129 cmd[3] = 0x80;
130 }
131 if (!send_cmd(em100->dev, cmd))
132 return 0;
133
134 return 1;
135}
136
137int fpga_get_voltage(struct em100 *em100, int *voltage_codep)
138{
139 if (!get_version(em100))
140 return 0;
141
142 *voltage_codep = em100->fpga & 0x8000 ? 18 : 33;
143
144 return 1;
145}
146
147int fpga_reconfigure(struct em100 *em100)
148{
149 unsigned char cmd[16];
150
151 memset(cmd, '\0', 16);
152 cmd[0] = 0x20; /* Switch FPGA */
153 if (!send_cmd(em100->dev, cmd))
154 return 0;
155
156 return 1;
157}