I am currently trying to evaluate the /dev/mem device. I have written the following program
#include <stdio.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>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
/*********************************************************************/
/* Define masks */
/* for read and write */
/********************************************************************/
//#define ADDR_RANGE_MASK 0xE2000FFF // "00111111111111" Allows only the the
// 12 first bit's to be set since we use the
// first 12 bits for addressing in the FPGA
//#define READ_MASK 0xE2001FFF // "01111111111111" read bit 1 & write bit 0
//#define WRITE_MASK 0xE2002FFF // "10111111111111" read bit 0 & write bit 1
//#define BASE_ADDR 0xE2000000 //CS_2 range starts @ 0xE2000000
/********************************************************************/
/* Define size to allocate */
/********************************************************************/
#define MAP_SIZE 4096UL // needs to be looked at....
#define MAP_MASK (MAP_SIZE - 4)
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result, writeval;
off_t target;
if(argc < 2){ //error message in case of wrong input
fprintf (stderr, "\nUsage:\t%s { address } [ data ]\n"
"\taddress : memory address to act upon\n"
"\tdata : data to be written (32bit)\n\n"
"To [i]nitialize type only i leaving out the addresses \n\n",
argv[0] );
exit(1);
}
/********************************************************************/
/* Init */
/********************************************************************/
if(strcmp(argv[1],"i" ) == 0 ) //strcmp() returns 0 if strings are the same (case sensitive)
{
printf ("init started.\n");
target = 0x31080240;
// Open /dev/mem file & error handling
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
printf ("/dev/mem opened.\n");
fflush (stdout);
// Map one page
map_base = mmap (0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if (map_base == (void *) -1) FATAL;
printf ("Memory (0x31080240) mapped at address %p.\n", map_base);
fflush (stdout);
// start WRITE
virt_addr = map_base + (target & MAP_MASK);
writeval = 0x2; //32Bit data bus setting for static memory of CS_2
*((unsigned long *) virt_addr) = writeval;
printf ("Written 0x%X\n", writeval);
fflush (stdout);
printf ("init successful\nRestart program to write or read data\n\n");
// stop WRITE
}
else
/********************************************************************/
/* Read/Write */
/********************************************************************/
{
target = strtoul (argv[1], 0, 0); // Parsing the input & leaving out leading 0 & 0x's
// Open /dev/mem file & error handling
if( (fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
printf ("/dev/mem opened.\n");
fflush (stdout);
// Map one page
map_base = mmap (0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if (map_base == (void *) -1) FATAL;
printf ("Memory mapped at address %p.\n", map_base);
fflush (stdout);
virt_addr = map_base + (target & MAP_MASK);
// start READ
if (argc <= 2) {
read_result = *((unsigned long *) virt_addr); //unsigned long fits the 32 bit bus data
printf("read_result: %d \n",read_result);
// stop READ
printf ("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
fflush (stdout);
}
// start WRITE if there is data to write given in the arguments
if (argc > 2) {
writeval = strtoul(argv[2], 0, 0);
*((unsigned long *) virt_addr) = writeval;
printf ("Written 0x%X\n", writeval);
fflush (stdout);
}
// stop WRITE
}
/********************************************************************/
/* UnMap one page */
/********************************************************************/
if (munmap(map_base, MAP_SIZE) == -1) FATAL;
close (fd);
return 0;
}
I can see from the schematics that the EMMC and GPMC shares the same memory device. How will i be able to toggle between the emmc and gpmc in Kernel level.
Is there any reference document for this operation.
Regards
-Ash