mdio.c (2035B)
1 /* Copyright 2014 Brian Swetland <swetland@frotz.net> 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <unistd.h> 19 #include <string.h> 20 21 #include "jtag.h" 22 #include "zynq.h" 23 24 static JTAG *jtag; 25 26 void jconnect(void) { 27 if (!(jtag = jtag_open())) { 28 fprintf(stderr, "jconnect: cannot open usb jtag ifc\n"); 29 exit(-1); 30 } 31 if (jtag_enumerate(jtag) <= 0) { 32 fprintf(stderr, "jconnect: cannot enumerate jtag devices\n"); 33 exit(-1); 34 } 35 if (jtag_select(jtag, 0x13722093) && jtag_select(jtag, 0x13631093)) { 36 fprintf(stderr, "jconnect: cannot connect to ZYNQ\n"); 37 exit(-1); 38 } 39 40 jtag_ir_wr(jtag, XIL_USER4); 41 } 42 43 u32 jrd(u32 addr) { 44 u64 u; 45 jtag_dr_io(jtag, 4, addr & 7, &u); 46 jtag_dr_io(jtag, 36, 0, &u); 47 return (u32) u; 48 } 49 50 void jwr(u32 addr, u32 val) { 51 u64 u = ((u64)val) | (((u64) (addr & 7)) << 32) | 0x800000000ULL; 52 jtag_dr_io(jtag, 36, u, &u); 53 } 54 55 int main(int argc, char **argv) { 56 57 if (argc == 3) { 58 u32 addr = strtoul(argv[1], 0, 0); 59 u32 val = strtoul(argv[2], 0, 0); 60 jconnect(); 61 jwr(3, 0xFFFFFFFF); 62 usleep(10000); 63 jwr(3, 0x50820000 | ((addr & 31) << 18) | (val & 0xFFFF)); 64 return 0; 65 } else if (argc == 2) { 66 u32 addr = strtoul(argv[1], 0, 0); 67 u32 val; 68 jconnect(); 69 jwr(3, 0xFFFFFFFF); 70 usleep(10000); 71 jwr(3, 0x60800000 | ((addr & 31) << 18)); 72 usleep(10000); 73 val = jrd(3); 74 printf("%08x\n", val); 75 return 0; 76 } else { 77 fprintf(stderr, 78 "usage:\n" 79 " write: debug <addr> <val>\n" 80 " read: debug <addr>\n"); 81 return -1; 82 } 83 }