Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 1 | /* |
| 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. |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <unistd.h> |
| 17 | #include "em100.h" |
| 18 | |
| 19 | /* FPGA related operations */ |
| 20 | |
| 21 | /** |
| 22 | * reconfig_fpga: Reconfigures FPGA after a change(?) |
| 23 | * @param em100: initialized em100 device structure |
| 24 | * |
| 25 | * out(16 bytes): 0x20 0 .. 0 |
| 26 | */ |
| 27 | int reconfig_fpga(struct em100 *em100) |
| 28 | { |
| 29 | unsigned char cmd[16]; |
| 30 | memset(cmd, 0, 16); |
| 31 | cmd[0] = 0x20; /* reconfig FPGA */ |
| 32 | if (!send_cmd(em100->dev, cmd)) { |
| 33 | return 0; |
| 34 | } |
| 35 | /* Specification says to wait 2s before |
| 36 | * issuing another USB command |
| 37 | */ |
| 38 | sleep(2); |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * check_fpga_status: Checkl FPGA configuration status |
| 44 | * @param em100: initialized em100 device structure |
| 45 | * |
| 46 | * out(16 bytes): 0x21 0 .. 0 |
| 47 | * in(result): pass: 1, fail: 0 |
| 48 | */ |
| 49 | int check_fpga_status(struct em100 *em100) |
| 50 | { |
| 51 | unsigned char cmd[16]; |
| 52 | unsigned char data[512]; |
| 53 | printf("FPGA configuration status: "); |
| 54 | memset(cmd, 0, 16); |
| 55 | cmd[0] = 0x21; /* Check FPGA status */ |
| 56 | if (!send_cmd(em100->dev, cmd)) { |
| 57 | printf("Unknown\n"); |
| 58 | return 0; |
| 59 | } |
| 60 | int len = get_response(em100->dev, data, 512); |
| 61 | if (len == 1) { |
| 62 | printf("%s\n", data[0] == 1 ? "PASS" : "FAIL"); |
| 63 | return 1; |
| 64 | } |
| 65 | printf("Unknown\n"); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * read_fpga_register: Read FPGA registers |
| 71 | * @param em100: initialized em100 device structure |
| 72 | * @param reg: FPGA register to write |
| 73 | * @param val: pointer to value |
| 74 | * |
| 75 | * out(2 bytes): 0x22 RegAddr .. 0 |
| 76 | * in(len + 2 bytes): 0x02 val val |
| 77 | */ |
| 78 | int read_fpga_register(struct em100 *em100, int reg, uint16_t *val) |
| 79 | { |
| 80 | unsigned char cmd[16]; |
| 81 | unsigned char data[256]; |
| 82 | |
| 83 | memset(cmd, 0, 16); |
| 84 | cmd[0] = 0x22; /* Read FPGA register */ |
| 85 | cmd[1] = reg; |
| 86 | if (!send_cmd(em100->dev, cmd)) { |
| 87 | return 0; |
| 88 | } |
| 89 | int len = get_response(em100->dev, data, 3); |
| 90 | if ((len == 3) && (data[0] == 2)) { |
| 91 | *val = (data[1] << 8) + data[2]; |
| 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * write_fpga_register: Write FPGA registers |
| 99 | * @param em100: initialized em100 device structure |
| 100 | * |
| 101 | * out(4 bytes): 0x23 RegAddr Val Val .. 0 |
| 102 | */ |
| 103 | int write_fpga_register(struct em100 *em100, int reg, int val) |
| 104 | { |
| 105 | unsigned char cmd[16]; |
| 106 | memset(cmd, 0, 16); |
| 107 | cmd[0] = 0x23; /* Write FPGA register */ |
| 108 | cmd[1] = reg; |
| 109 | cmd[2] = val >> 8; |
| 110 | cmd[3] = val & 0xff; |
| 111 | if (!send_cmd(em100->dev, cmd)) { |
| 112 | return 0; |
| 113 | } |
| 114 | return 1; |
| 115 | } |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 116 | |
| 117 | int fpga_set_voltage(struct em100 *em100, int voltage_code) |
| 118 | { |
| 119 | unsigned char cmd[16]; |
| 120 | |
| 121 | memset(cmd, '\0', 16); |
| 122 | cmd[0] = 0x24; /* Switch FPGA */ |
| 123 | if (voltage_code == 18) { |
| 124 | cmd[2] = 7; |
| 125 | cmd[3] = 0x80; |
| 126 | } |
| 127 | if (!send_cmd(em100->dev, cmd)) |
| 128 | return 0; |
| 129 | |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | int fpga_get_voltage(struct em100 *em100, int *voltage_codep) |
| 134 | { |
| 135 | if (!get_version(em100)) |
| 136 | return 0; |
| 137 | |
| 138 | *voltage_codep = em100->fpga & 0x8000 ? 18 : 33; |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | int fpga_reconfigure(struct em100 *em100) |
| 144 | { |
| 145 | unsigned char cmd[16]; |
| 146 | |
| 147 | memset(cmd, '\0', 16); |
| 148 | cmd[0] = 0x20; /* Switch FPGA */ |
| 149 | if (!send_cmd(em100->dev, cmd)) |
| 150 | return 0; |
| 151 | |
| 152 | return 1; |
| 153 | } |