Advertisement
dhirajjadhav

gpmc_sync_nor_read_write

Jun 20th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.96 KB | None | 0 0
  1. /*
  2. Test file for talking to external memory using GPMC pins in GPMC mode.
  3. */
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <sys/mman.h>
  7. #include "hw_gpmc.h" // SYS-BIOS
  8. #include "hw_cm_per.h" // SYS-BIOS
  9. #include "soc_AM335x.h" // SYS-BIOS
  10.  
  11. #define MMAP_OFFSET 0x44c00000
  12. #define Clock_Div 0x2
  13. #define RD_CYCLETIME 0xa*(Clock_Div+1)
  14. #define WR_CYCLETIME 0x3*(Clock_Div+1)
  15. //#define RD_ACCESSTIME 0x8*(Clock_Div+1)
  16. #define RD_ACCESSTIME 0x8*(Clock_Div+1)
  17.  
  18. #define CSRD_OFFTIME 0xa*(Clock_Div+1)
  19. #define CSWR_OFFTIME 0x3*(Clock_Div+1)
  20.  
  21. #define ADV_ONTIME 0x1*(Clock_Div+1)
  22. #define ADVWR_OFFTIME 0x1*(Clock_Div+1)
  23. #define ADVRD_OFFTIME 0x2*(Clock_Div+1)
  24.  
  25. #define OE_ONTIME 0x2*(Clock_Div+1)
  26. #define OE_OFFTIME 0x9*(Clock_Div+1)
  27.  
  28.  
  29. #define WE_ONTIME 0x1*(Clock_Div+1)
  30. #define WE_OFFTIME 0x3*(Clock_Div+1)
  31. // redefine HWREG macro to use MMAP result                                    
  32. #define HWREG(x) __mmapl[(x-MMAP_OFFSET)/4]                                    
  33. #define CM_DEVICE 0x44E00700                                                  
  34.                                                                                
  35. volatile ulong *__mmapl;                                                      
  36.                                                                                
  37. uint gpmc_init()                                                              
  38. {                                                                              
  39.     ushort csNum = 1;                                                          
  40.     uint temp = 0;                                                            
  41.                                                                                
  42.                                                                                
  43.     //enable clock to GPMC module                                              
  44.     HWREG(SOC_PRCM_REGS + CM_PER_GPMC_CLKCTRL ) |=                            
  45.                 CM_PER_GPMC_CLKCTRL_MODULEMODE_ENABLE;                        
  46.     //check to see if enabled                                                  
  47.     while((HWREG(SOC_PRCM_REGS + CM_PER_GPMC_CLKCTRL) & CM_PER_GPMC_CLKCTRL_IDLEST) !=
  48.                 (CM_PER_GPMC_CLKCTRL_IDLEST_FUNC << CM_PER_GPMC_CLKCTRL_IDLEST_SHIFT));
  49.                                                                                
  50.     //reset the GPMC module                                                    
  51.     HWREG(SOC_GPMC_0_REGS + GPMC_SYSCONFIG ) |= GPMC_SYSCONFIG_SOFTRESET;      
  52.     while((HWREG(SOC_GPMC_0_REGS + GPMC_SYSSTATUS) & GPMC_SYSSTATUS_RESETDONE) ==
  53.                 GPMC_SYSSTATUS_RESETDONE_RSTONGOING);                          
  54.                                                                                
  55.     //Configure to no idle                                                    
  56.     temp = HWREG(SOC_GPMC_0_REGS + GPMC_SYSCONFIG);                            
  57.     temp &= ~GPMC_SYSCONFIG_IDLEMODE;                                          
  58.     temp |= GPMC_SYSCONFIG_IDLEMODE_NOIDLE << GPMC_SYSCONFIG_IDLEMODE_SHIFT;  
  59.     HWREG(SOC_GPMC_0_REGS + GPMC_SYSCONFIG) = temp;
  60.                                                                              
  61.     HWREG(SOC_GPMC_0_REGS + GPMC_IRQENABLE) = 0x0;                            
  62.     HWREG(SOC_GPMC_0_REGS + GPMC_TIMEOUT_CONTROL) = 0x0;                      
  63.                                                                                
  64.     //configure for NOR and granularity x2                                    
  65.     // CONFIG 1 .. 16 bits multiplexed                                        
  66.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG1(csNum)) = (0x0 |                      
  67.         (GPMC_CONFIG1_0_DEVICESIZE_SIXTEENBITS <<                              
  68.                 GPMC_CONFIG1_0_DEVICESIZE_SHIFT ) |                            
  69.         (GPMC_CONFIG1_0_ATTACHEDDEVICEPAGELENGTH_FOUR <<                      
  70.                 GPMC_CONFIG1_0_ATTACHEDDEVICEPAGELENGTH_SHIFT ) |              
  71.         (0x2 << 8 )|(0x28000000)|(Clock_Div));  //Address/Data Multiplex enable
  72.     // config 2 .. chip select assert/deassert times                          
  73.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG2(csNum)) = (0x0 |                      
  74.         (0x00) |        // CS_ON_TIME  -- bigger than adv deassert..          
  75.         (CSRD_OFFTIME << GPMC_CONFIG2_0_CSRDOFFTIME_SHIFT) |    // CS_DEASSERT_RD
  76.         (CSWR_OFFTIME << GPMC_CONFIG2_0_CSWROFFTIME_SHIFT));    //CS_DEASSERT_WR
  77.                                                                                
  78.     // config 3 .. latch enable assert and de-assert                          
  79.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG3(csNum)) = (0x0 |                      
  80.         (ADV_ONTIME << GPMC_CONFIG3_0_ADVONTIME_SHIFT) | //ADV_ASSERT          
  81.         (ADVRD_OFFTIME << GPMC_CONFIG3_0_ADVRDOFFTIME_SHIFT) | //ADV_DEASSERT_RD
  82.         (ADVWR_OFFTIME << GPMC_CONFIG3_0_ADVWROFFTIME_SHIFT)); //ADV_DEASSERT_WR
  83.                                                                                
  84.     // config 4 .. output enable / read write enable assert and de-assert      
  85.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG4(csNum)) = (0x0 |                      
  86.         (OE_ONTIME << GPMC_CONFIG4_0_OEONTIME_SHIFT) |  //OE_ASSERT            
  87.         (OE_OFFTIME << GPMC_CONFIG4_0_OEOFFTIME_SHIFT) |        //OE_DEASSERT  
  88.         (WE_ONTIME << GPMC_CONFIG4_0_WEONTIME_SHIFT)| //WE_ASSERT              
  89.         (WE_OFFTIME << GPMC_CONFIG4_0_WEOFFTIME_SHIFT)); //WE_DEASSERT
  90.       (WE_OFFTIME << GPMC_CONFIG4_0_WEOFFTIME_SHIFT)); //WE_DEASSERT        
  91.     // Config 5 - read and write cycle time                                    
  92.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG5(csNum)) = (0x0 |                      
  93.         (RD_CYCLETIME << GPMC_CONFIG5_0_RDCYCLETIME_SHIFT)|     //CFG_5_RD_CYCLE_TIM XXX
  94.         (WR_CYCLETIME << GPMC_CONFIG5_0_WRCYCLETIME_SHIFT)|     //CFG_5_WR_CYCLE_TIM XXX
  95.         //(OE_ONTIME  << GPMC_CONFIG5_0_RDACCESSTIME_SHIFT));  // CFG_5_RD_ACCESS_TIM XXX
  96.         (RD_ACCESSTIME  << GPMC_CONFIG5_0_RDACCESSTIME_SHIFT));  // CFG_5_RD_ACCESS_TIM XXX
  97.                                                                                
  98.     // Config 6 .. bus turnaround delay, etc                                  
  99.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG6(csNum)) = (0x0 |                      
  100.         (0 << //GPMC_CONFIG6_0_CYCLE2CYCLESAMECSEN_C2CDELAY                    
  101.                 GPMC_CONFIG6_0_CYCLE2CYCLESAMECSEN_SHIFT) |                    
  102.         (0 << GPMC_CONFIG6_0_CYCLE2CYCLEDELAY_SHIFT) | //CYC2CYC_DELAY        
  103.         (OE_ONTIME << GPMC_CONFIG6_0_WRDATAONADMUXBUS_SHIFT)| //WR_DATA_ON_ADMUX XXX
  104.         (0 << GPMC_CONFIG6_0_WRACCESSTIME_SHIFT));  //CFG_6_WR_ACCESS_TIM      
  105.                                                                                
  106.     // config 7 .. base address of the chip select and address space. (16 MB is smallest)
  107.     HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG7(csNum)) =                            
  108.         ( 0x09 << GPMC_CONFIG7_0_BASEADDRESS_SHIFT) | //CFG_7_BASE_ADDR        
  109.         (0x1 << GPMC_CONFIG7_0_CSVALID_SHIFT) |                                
  110.         (0x0f << GPMC_CONFIG7_0_MASKADDRESS_SHIFT);  //CFG_7_MASK              
  111.                                                                                
  112.  printf("\n CONFIG1=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG1(csNum)));      
  113.  printf("\n CONFIG2=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG2(csNum)));      
  114.  printf("\n CONFIG3=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG3(csNum)));      
  115.  printf("\n CONFIG4=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG4(csNum)));      
  116.  printf("\n CONFIG50=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG5(csNum)));    
  117.  printf("\n CONFIG6=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG6(csNum)));      
  118.  printf("\n CONFIG7=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG7(csNum)));      
  119.                                                                                
  120.                                                                                
  121.     return 0;                                                                  
  122. }                                                                              
  123.                                                                                
  124. ushort mymem[0x10000];                                                        
  125.                                                                                
  126. int main(int argc, char** argv)                                                
  127. {                                                                              
  128.     int i, k,temp;                                                            
  129.     ushort testread_temp = 1;                                                    
  130.     int j=0,addr=0x35a5;                                                      
  131.           printf("\n testread_Temp Before: %x",testread_temp);                      
  132.                                                                                
  133.     int fd = open("/dev/mem", O_RDWR|O_SYNC); //O_SYNC makes the memory uncacheable
  134.     __mmapl = (ulong*) mmap(NULL, 0x20000000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, MMAP_OFFSET);
  135.                                                                                
  136.     gpmc_init();                                                              
  137.                                                                                
  138.    volatile ushort *extmem;                                                    
  139.  
  140.     extmem =(ushort *)mmap(NULL, 0x40000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x09000000);
  141.     HWREG(CM_DEVICE) = 0x9b; //0xb1;// 0xA1; // Send L3 clock/5 = 40 Mhz to CLKOUT2
  142.                                                                                
  143.             printf("\n GPMC_SYSCONFIG=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG7(1)-0x58 ));
  144.             printf("\n GPMC_STATUS=0x%x", HWREG(SOC_GPMC_0_REGS + GPMC_CONFIG7(1)-0x54 ));  
  145.  
  146.            i = 0x1a52;                                                        
  147.           extmem[i] = 0x5a5a;                                                  
  148.           extmem[i+1] = 0x5a5a;                                                
  149.           printf("\n testread_Temp After Write Before Read: %x",testread_temp);      
  150.            test_temp = extmem[0x1a52];                                        
  151.           temp=msync((void *)extmem,0x40000,MS_ASYNC);                        
  152.                                                                                        
  153.           printf("\n sync result=%d testread_Temp: %x\n",temp,testread_temp);        
  154.     }                                                                          
  155.     return(0);                                                                
  156. }
  157.  
  158. output:
  159.  
  160.  testread_Temp Before: 1
  161.  CONFIG1=0x28001202
  162.  CONFIG2=0x91e00
  163.  CONFIG3=0x30603
  164.  CONFIG4=0x9031b06
  165.  CONFIG50=0x18091e
  166.  CONFIG6=0x60000
  167.  CONFIG7=0xf49
  168.  GPMC_SYSCONFIG=0xa00
  169.  GPMC_STATUS=0x1
  170.  testread_Temp After Write Before Read: 1
  171.  sync result=0 testread_Temp: 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement