summaryrefslogtreecommitdiffstats
path: root/devmem-aspeed.c
blob: da125e228b919e0c97c447328529d9ee9491b926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * devmem2.c: Simple program to read/write from/to any location in memory.
 *
 * Copyright (C) 2000 Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
 * Copyright (C) 2009 Carl-Daniel Hailfinger
 * Copyright (C) 2016-2018 Raptor Engineering, LLC
 *
 *
 * This software has been developed for the LART computing board
 * (http://www.lart.tudelft.nl/). The development has been sponsored by
 * the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
 * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
 * projects.
 *
 * The author can be reached at:
 *
 *  Jan-Derk Bakker
 *  Information and Communication Theory Group
 *  Faculty of Information Technology and Systems
 *  Delft University of Technology
 *  P.O. Box 5031
 *  2600 GA Delft
 *  The Netherlands
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <pci/pci.h>

#define FATAL do { pci_cleanup(pacc);; fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)

#define MAP_SIZE			(128 * 1024)
#define MAP_MASK			(MAP_SIZE - 1)
#define ASPEED_P2A_OFFSET               0x10000

struct pci_access *pacc;
uintptr_t aspeed_bar_offset = 0;
void *aspeed_control_bar = NULL;
void *aspeed_bar = NULL;

struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
{
	struct pci_dev *temp;
	struct pci_filter filter;

	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;

	return NULL;
}

void sync_primitive(void)
{
#if defined(__PPC__) || defined(__PPC64__)
	asm("eieio" : : : "memory");
#endif
}

static int ast2400_set_a2b_bridge_dest_address(uintptr_t address)
{
	aspeed_bar_offset = address & 0xffff;

	*((volatile uint32_t *)(aspeed_control_bar + 0xf000)) = 0x0;
	sync_primitive();
	*((volatile uint32_t *)(aspeed_control_bar + 0xf004)) = address & 0xffff0000;
	sync_primitive();
	*((volatile uint32_t *)(aspeed_control_bar + 0xf000)) = 0x1;
	sync_primitive();

        return 0;
}

static int ast2400_disable_a2b_bridge(void)
{
	*((volatile uint32_t *)(aspeed_control_bar + 0xf000)) = 0x0;
	sync_primitive();

        return 0;
}

int main(int argc, char **argv) {
	int fd;
	void *bar_addr = NULL;
	void *virt_addr;
	uint32_t read_result, writeval;
	uintptr_t target;
	struct pci_dev* aspeed_dev = NULL;
	int access_type = 'w';

	if(argc < 2) {
		fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
			"\taddress : memory address to act upon\n"
			"\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
			"\tdata    : data to be written\n\n",
			argv[0]);
		exit(1);
	}
	target = strtoul(argv[1], 0, 0);

	if(argc > 2) {
		access_type = tolower(argv[2][0]);
	}

        pacc = pci_alloc();
        pci_init(pacc);
        pci_scan_bus(pacc);

	aspeed_dev = pci_dev_find(0x1a03, 0x2000);
	if (!aspeed_dev) FATAL;

	bar_addr = (void *)(aspeed_dev->base_addr[1] & PCI_BASE_ADDRESS_MEM_MASK);

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;

	/* Map control page */
	aspeed_control_bar = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (uintptr_t)bar_addr);
	if(aspeed_control_bar == (void *) -1) FATAL;

        bar_addr += ASPEED_P2A_OFFSET;

	/* Map data page */
	aspeed_bar = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (uintptr_t)bar_addr);
	if(aspeed_bar == (void *) -1) FATAL;

	ast2400_set_a2b_bridge_dest_address(target);

	virt_addr = aspeed_bar + (aspeed_bar_offset & MAP_MASK);
	switch(access_type) {
		case 'b':
			read_result = *((volatile uint8_t *) virt_addr);
			break;
		case 'h':
			read_result = *((volatile uint16_t *) virt_addr);
			break;
		case 'w':
			read_result = *((volatile uint32_t *) virt_addr);
			break;
		default:
			ast2400_disable_a2b_bridge();
			fprintf(stderr, "Illegal data type '%c'.\n", access_type);
			exit(2);
	}
	printf("0x%08X\n", read_result);
	fflush(stdout);

	if(argc > 3) {
		writeval = strtoul(argv[3], 0, 0);
		switch(access_type) {
			case 'b':
				*((volatile uint8_t *) virt_addr) = writeval;
				sync_primitive();
				read_result = *((volatile uint8_t *) virt_addr);
				break;
			case 'h':
				*((volatile uint16_t *) virt_addr) = writeval;
				sync_primitive();
				read_result = *((volatile uint16_t *) virt_addr);
				break;
			case 'w':
				*((volatile uint32_t *) virt_addr) = writeval;
				sync_primitive();
				read_result = *((volatile uint32_t *) virt_addr);
				break;
		}
	}

	ast2400_disable_a2b_bridge();
	if(munmap(aspeed_bar, MAP_SIZE) == -1) FATAL;
	if(munmap(aspeed_control_bar, MAP_SIZE) == -1) FATAL;

	close(fd);
	return 0;
}

OpenPOWER on IntegriCloud