developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1 | /* |
| 2 | * pcimem.c: Simple program to read/write from/to a pci device from userspace. |
| 3 | * |
| 4 | * Copyright (C) 2010, Bill Farrow (bfarrow@beyondelectronics.us) |
| 5 | * |
| 6 | * Based on the devmem2.c code |
| 7 | * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl) |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdint.h> |
| 28 | #include <unistd.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <signal.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <ctype.h> |
| 34 | #include <termios.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/mman.h> |
| 37 | |
| 38 | #define PRINT_ERROR \ |
| 39 | do { \ |
| 40 | fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ |
| 41 | __LINE__, __FILE__, errno, strerror(errno)); exit(1); \ |
| 42 | } while(0) |
| 43 | |
| 44 | #define MAP_SIZE 4096UL |
| 45 | #define MAP_MASK (MAP_SIZE - 1) |
| 46 | |
| 47 | void dump_page(uint32_t *vaddr, uint32_t *vbase, uint32_t *pbase) |
| 48 | { |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 49 | int i = 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 50 | uint32_t *end = vaddr + (MAP_SIZE >> 6); |
| 51 | uint32_t *start = vaddr; |
| 52 | |
| 53 | while(start < end) { |
| 54 | printf("%p:%08x %08x %08x %08x\n", |
| 55 | start - vbase + pbase, start[0], start[1] , start[2], start[3]); |
| 56 | start+=4; |
| 57 | } |
| 58 | } |
| 59 | |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 60 | void reg_mod_bits(uint32_t *virt_addr, int data, int start_bit, int data_len) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 61 | { |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 62 | int mask = 0; |
| 63 | int value; |
| 64 | int i; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 65 | |
| 66 | if ((start_bit < 0) || (start_bit > 31) || |
| 67 | (data_len < 1) || (data_len > 32) || |
| 68 | (start_bit + data_len > 32)) { |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 69 | fprintf(stderr, |
| 70 | "Startbit range[0~31], and DataLen range[1~32], and Startbit + DataLen <= 32\n"); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 71 | exit(1); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 72 | } |
| 73 | |
developer | 15305a3 | 2021-12-21 19:34:15 +0800 | [diff] [blame] | 74 | for (i = 0; i < data_len; i++) { |
| 75 | if (start_bit + i > 31) |
| 76 | break; |
| 77 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 78 | mask |= 1 << (start_bit + i); |
developer | 15305a3 | 2021-12-21 19:34:15 +0800 | [diff] [blame] | 79 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 80 | |
| 81 | value = *((volatile uint32_t *) virt_addr); |
| 82 | value &= ~mask; |
| 83 | value |= (data << start_bit) & mask;; |
| 84 | |
| 85 | *((uint32_t *) virt_addr) = value; |
| 86 | |
| 87 | printf("Modify 0x%X[%d:%d]; ", data, start_bit + data_len - 1, start_bit); |
| 88 | } |
| 89 | |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 90 | void print_binary(uint32_t data) |
| 91 | { |
| 92 | if (data > 1) |
| 93 | print_binary(data >> 1); |
| 94 | |
| 95 | printf("%x", data % 2); |
| 96 | } |
| 97 | |
| 98 | void reg_read_bits(uint32_t *virt_addr, uint32_t *virt_base, uint32_t *phy_base, |
| 99 | int start_bit, int data_len) |
| 100 | { |
| 101 | uint32_t mask = 0; |
| 102 | uint32_t value; |
| 103 | int i; |
| 104 | |
| 105 | if ((start_bit < 0) || (start_bit > 31) || |
| 106 | (data_len < 1) || (data_len > 32) || |
| 107 | (start_bit + data_len > 32)) { |
| 108 | fprintf(stderr, |
| 109 | "Startbit range[0~31], and DataLen range[1~32], and Startbit + DataLen <= 32\n"); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 110 | exit(1); |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | for (i = 0; i < data_len; i++) { |
| 114 | if (start_bit + i > 31) |
| 115 | break; |
| 116 | |
| 117 | mask |= 1 << (start_bit + i); |
| 118 | } |
| 119 | |
| 120 | value = *((uint32_t *)virt_addr); |
| 121 | value &= mask; |
| 122 | |
| 123 | if (start_bit == 0 && data_len == 32) |
| 124 | printf("%p: %08x\n", |
| 125 | virt_addr - virt_base + phy_base, value); |
| 126 | else { |
| 127 | printf("%p[%2d:%2d] = %x (hex)\n", |
| 128 | virt_addr - virt_base + phy_base, |
| 129 | start_bit + data_len - 1, start_bit, |
| 130 | value >> start_bit); |
| 131 | printf("%17s = %d (dec)\n", "", value >> start_bit); |
| 132 | printf("%17s = ", ""); |
| 133 | print_binary(value >> start_bit); |
| 134 | printf(" (bin)\n"); |
| 135 | } |
| 136 | } |
| 137 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 138 | void usage(void) |
| 139 | { |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 140 | fprintf(stderr, |
| 141 | "\nUsage:\tregs [Type] [ Offset:Hex ] [ Data:Hex ] [StartBit:Dec] [DataLen:Dec]\n" |
| 142 | "\tType : access operation type : [m]odify, [w]wite, [d]ump\n" |
| 143 | "\tOffset : offset into memory region to act upon\n" |
| 144 | "\tData : data to be written\n" |
| 145 | "\tStartbit: Startbit of Addr that want to be modified. Range[0~31]\n" |
| 146 | "\tDataLen : Data length of Data. Range[1~32], and Startbit + DataLen <= 32\n\n" |
| 147 | "Example:\tRead/Write/Modify register\n" |
| 148 | "\tDump : regs d 0x1b100000 //dump 0x1b100000~0x1b1000f0\n" |
| 149 | "\tRead : regs r 0x1b100000 //read 0x1b100000\n" |
| 150 | "\tRead : regs r 0x1b100000 29 3 //read 0x1b100000[29:31]\n" |
| 151 | "\tWrite : regs w 0x1b100000 0x1234 //write 0x1b100000=0x1234\n" |
| 152 | "\tModify : regs m 0x1b100000 0x0 29 3 //modify 0x1b100000[29:31]=0\n"); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 153 | exit(1); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 154 | } |
| 155 | |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 156 | int main(int argc, char **argv) |
| 157 | { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 158 | int fd; |
| 159 | void *map_base = NULL; |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 160 | void *virt_addr = NULL; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 161 | uint32_t read_result =0; |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 162 | uint32_t writeval = 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 163 | uint32_t startbit = 0; |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 164 | uint32_t datalen = 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 165 | char *filename = NULL; |
| 166 | off_t offset = 0; |
| 167 | int access_type = 0; |
| 168 | |
| 169 | if(argc < 3) { |
| 170 | usage(); |
| 171 | exit(1); |
| 172 | } |
| 173 | |
| 174 | access_type = tolower(argv[1][0]); |
| 175 | if ((access_type == 'w' && argc < 4) || (access_type == 'm' && argc < 6)) { |
| 176 | usage(); |
| 177 | exit(1); |
| 178 | } |
| 179 | |
| 180 | filename = "/dev/mem"; |
| 181 | if((fd = open(filename, O_RDWR | O_SYNC)) == -1) |
| 182 | PRINT_ERROR; |
| 183 | |
| 184 | /* Map one page */ |
| 185 | offset = strtoul(argv[2], NULL, 16); |
developer | 39f2116 | 2023-09-28 19:42:48 +0800 | [diff] [blame] | 186 | if (offset > 0xFFFFFFFFFFFFUL) |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 187 | PRINT_ERROR; |
| 188 | |
| 189 | map_base = mmap(0, 2 * MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 190 | fd, offset & ~MAP_MASK); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 191 | if(map_base == (void *) -1) |
| 192 | PRINT_ERROR; |
| 193 | |
| 194 | virt_addr = map_base + (offset & MAP_MASK); |
| 195 | read_result = *((volatile uint32_t *) virt_addr); |
| 196 | printf("Value at 0x%llX (%p): 0x%X\n", |
| 197 | (unsigned long long)offset, virt_addr, read_result); |
| 198 | |
| 199 | switch(access_type) { |
| 200 | case 'm': |
| 201 | writeval = strtoul(argv[3], 0, 16); |
developer | 39f2116 | 2023-09-28 19:42:48 +0800 | [diff] [blame] | 202 | if (writeval > 0xFFFFFFFFUL) |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 203 | PRINT_ERROR; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 204 | startbit = strtoul(argv[4], 0, 10); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 205 | if (startbit > 32) |
| 206 | PRINT_ERROR; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 207 | datalen = strtoul(argv[5], 0, 10); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 208 | if (datalen > 32) |
| 209 | PRINT_ERROR; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 210 | reg_mod_bits((uint32_t *)virt_addr, writeval, startbit, datalen); |
| 211 | break; |
| 212 | case 'w': |
| 213 | writeval = strtoul(argv[3], 0, 16); |
developer | 39f2116 | 2023-09-28 19:42:48 +0800 | [diff] [blame] | 214 | if (writeval > 0xFFFFFFFFUL) |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 215 | PRINT_ERROR; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 216 | *((uint32_t *) virt_addr) = writeval; |
| 217 | printf("Written 0x%X; ", writeval); |
| 218 | break; |
| 219 | case 'd': |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 220 | dump_page((uint32_t *)virt_addr, (uint32_t *)map_base, |
| 221 | (uint32_t *)(offset & ~MAP_MASK)); |
| 222 | goto out; |
| 223 | case 'r': |
| 224 | if (argc == 3) |
| 225 | reg_read_bits((uint32_t *)virt_addr, |
| 226 | (uint32_t *)map_base, |
| 227 | (uint32_t *)(offset & ~MAP_MASK), |
| 228 | 0, 32); |
| 229 | else { |
| 230 | startbit = strtoul(argv[3], 0, 10); |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 231 | if (startbit > 32) |
| 232 | PRINT_ERROR; |
| 233 | datalen = strtoul(argv[4], 0, 10); |
| 234 | if (datalen > 32) |
| 235 | PRINT_ERROR; |
developer | e11cc02 | 2023-05-08 22:16:53 +0800 | [diff] [blame] | 236 | reg_read_bits((uint32_t *)virt_addr, |
| 237 | (uint32_t *)map_base, |
| 238 | (uint32_t *)(offset & ~MAP_MASK), |
| 239 | startbit, datalen); |
| 240 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 241 | goto out; |
| 242 | default: |
developer | abeb33c | 2023-09-27 17:15:06 +0800 | [diff] [blame] | 243 | printf("Illegal data type '%c'.\n", access_type); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | read_result = *((volatile uint32_t *) virt_addr); |
| 248 | printf("Readback 0x%X\n", read_result); |
| 249 | |
| 250 | out: |
| 251 | if(munmap(map_base, MAP_SIZE) == -1) |
| 252 | PRINT_ERROR; |
| 253 | |
| 254 | close(fd); |
| 255 | return 0; |
| 256 | } |