blob: 09e088c32de7e70db4c5bc458a4d813e197a7484 [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001/*
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
47void dump_page(uint32_t *vaddr, uint32_t *vbase, uint32_t *pbase)
48{
49 int i =0;
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
60void reg_mod_bits(uint32_t *virt_addr, int data, int start_bit, int data_len)
61{
62 int mask=0;
63 int value;
64 int i;
65
66 if ((start_bit < 0) || (start_bit > 31) ||
67 (data_len < 1) || (data_len > 32) ||
68 (start_bit + data_len > 32)) {
69 fprintf(stderr, "Startbit range[0~31], and DataLen range[1~32], and Startbit + DataLen <= 32\n");
70 return;
71 }
72
developer15305a32021-12-21 19:34:15 +080073 for (i = 0; i < data_len; i++) {
74 if (start_bit + i > 31)
75 break;
76
developerfd40db22021-04-29 10:08:25 +080077 mask |= 1 << (start_bit + i);
developer15305a32021-12-21 19:34:15 +080078 }
developerfd40db22021-04-29 10:08:25 +080079
80 value = *((volatile uint32_t *) virt_addr);
81 value &= ~mask;
82 value |= (data << start_bit) & mask;;
83
84 *((uint32_t *) virt_addr) = value;
85
86 printf("Modify 0x%X[%d:%d]; ", data, start_bit + data_len - 1, start_bit);
87}
88
89void usage(void)
90{
91 fprintf(stderr, "\nUsage:\tregs [Type] [ Offset:Hex ] [ Data:Hex ] [StartBit:Dec] [DataLen:Dec]\n"
92 "\tType : access operation type : [m]odify, [w]wite, [d]ump\n"
93 "\tOffset : offset into memory region to act upon\n"
94 "\tData : data to be written\n"
95 "\tStartbit: Startbit of Addr that want to be modified. Range[0~31]\n"
96 "\tDataLen : Data length of Data. Range[1~32], and Startbit + DataLen <= 32\n\n"
97 "Example:\tRead/Write/Modify register \n"
98 "\tRead : regs d 0x1b100000 //dump 0x1b100000~0x1b1000f0 \n"
99 "\tWrite : regs w 0x1b100000 0x1234 //write 0x1b100000=0x1234\n"
100 "\tModify : regs m 0x1b100000 0x0 29 3 //modify 0x1b100000[29:31]=0\n");
101}
102
103int main(int argc, char **argv) {
104 int fd;
105 void *map_base = NULL;
106 void *virt_addr = NULL;
107 uint32_t read_result =0;
108 uint32_t writeval = 0;
109 uint32_t startbit = 0;
110 uint32_t datalen = 0;
111 char *filename = NULL;
112 off_t offset = 0;
113 int access_type = 0;
114
115 if(argc < 3) {
116 usage();
117 exit(1);
118 }
119
120 access_type = tolower(argv[1][0]);
121 if ((access_type == 'w' && argc < 4) || (access_type == 'm' && argc < 6)) {
122 usage();
123 exit(1);
124 }
125
126 filename = "/dev/mem";
127 if((fd = open(filename, O_RDWR | O_SYNC)) == -1)
128 PRINT_ERROR;
129
130 /* Map one page */
131 offset = strtoul(argv[2], NULL, 16);
132 map_base = mmap(0, 2*MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset & ~MAP_MASK);
133 if(map_base == (void *) -1)
134 PRINT_ERROR;
135
136 virt_addr = map_base + (offset & MAP_MASK);
137 read_result = *((volatile uint32_t *) virt_addr);
138 printf("Value at 0x%llX (%p): 0x%X\n",
139 (unsigned long long)offset, virt_addr, read_result);
140
141 switch(access_type) {
142 case 'm':
143 writeval = strtoul(argv[3], 0, 16);
144 startbit = strtoul(argv[4], 0, 10);
145 datalen = strtoul(argv[5], 0, 10);
146 reg_mod_bits((uint32_t *)virt_addr, writeval, startbit, datalen);
147 break;
148 case 'w':
149 writeval = strtoul(argv[3], 0, 16);
150 *((uint32_t *) virt_addr) = writeval;
151 printf("Written 0x%X; ", writeval);
152 break;
153 case 'd':
154 dump_page(virt_addr, map_base, (uint32_t *)(offset & ~MAP_MASK));
155 goto out;
156 default:
157 fprintf(stderr, "Illegal data type '%c'.\n", access_type);
158 goto out;
159 }
160
161 read_result = *((volatile uint32_t *) virt_addr);
162 printf("Readback 0x%X\n", read_result);
163
164out:
165 if(munmap(map_base, MAP_SIZE) == -1)
166 PRINT_ERROR;
167
168 close(fd);
169 return 0;
170}