2011-11-29

[DM368][IPNC][UBL] GPIO and Pinmux Initialization

[Prolog]
In order to adapt my new hardware board, I have to change many of GPIO settings in Appro's IPNC SDK. I thought the major GPIO setting code would reside at linux arch, but it was wrong. Surprisely,  most of the setting including pinmux and GPIO direction is done at UBL!


[Main]
Restore your memory to school life...yeah, the good old time. All lectures taught you that the settings should be made where it needed during boot process. i.e. GPIO should be only configured  at the stage it will be used during boot straps.


UBL apparently is not a good place to perform GPIO configuration. TI even doesn't provide UBL source code with dvsdk! 


1. the code to set GPIO@ubl is at flash_utils/DM36x/Common/src/device.c, DEVICE_init()
==========

  DEVICE_pinmuxControl(0,0xFFFFFFFF,0x00FD0000);  // All Video Inputs  DEVICE_pinmuxControl(1,0xFFFFFFFF,0x00145555);  // All Video Outputs  DEVICE_pinmuxControl(2,0xFFFFFFFF,0x000000DA);  // EMIFA  DEVICE_pinmuxControl(3,0xFFFFFFFF,0x60000000);  // SPI0, SPI1, UART1, I2C, SD0, SD1, McBSP0, CLKOUTs 00180000  DEVICE_pinmuxControl(4,0xFFFFFFFF,0x5555D555);  // MMC/SD0 instead of MS, SPI0 55555555    GPIO->DIR23 &= 0xfeffffff;  GPIO->CLRDATA23 = 0x01000000;

==========


Moreover, you can find the system and peripheral clocks are set in  DEVICE_PLL1Init() and DEVICE_PLL2Init(), which I think can be move some to u-boot or even linux.


2. I decide to follow what textbook said - minimize the configure in ubl and u-boot and leave board specific changes to linux boot code. the benefit is obvious - I don't have to change code and recompile ubl (and maybe even better: u-boot as well) if hardware is changed. 


to be compatible with most DM368 hardware, I set most pin to GPIO, except MMC/SD0, VIN, UART0, UART1, I2C, CLKOUT0. The changes is listed below: 
==========

 //MMC/SD0, GIO49-43, YIN7-0, VD, HD, C_WE_FIELD  DEVICE_pinmuxControl(0,0xFFFFFFFF,0x00000000);   //GIO79-92 DEVICE_pinmuxControl(1,0xFFFFFFFF,0x00430000); 
//CMNT: GIO57-78 and EM_A[1] is configured by AECFG setting. leave it as-is and change in u-boot or os// CE0(GIO56) set to GPIO Interfacing to a Non-CE Don't Care NAND Flash  DEVICE_pinmuxControl(2,0x00000080,0x00000080);
//consider to enable emac for emac-boot  DEVICE_pinmuxControl(3,0xFFFFFFFF,0x61580000);  //UART1_TX, UART0, I2C
  DEVICE_pinmuxControl(4,0xFFFFFFFF,0x0030c000);  //CLKOUT0, UART1_RX


==========

There is also patch of NAND CE for Non-CE Don't card NAND flash. The CE is configured to GPIO and driven low manually.



GPIO->DIR23 &= 0xFEFFFFFF; //DIR56(bit24) set to output (L)
GPIO->CLRDATA23 = 0x01000000; //nand_ce low


The binary file can be downloaded at dm36x ubl gpio bin (uart0 and uart1 message). please be noted that you may want to revise GPIOs which are used by UART0 or UART1 in either u-boot or linux because they are all configured as UART in this binary.


[Option]
1. Turn on PINMUX to Ethernet in U-boot 
board_eth_init@dm365evm.c

//CMNT: manipulate eth pinmux only, PINMUX3[18:0]
// writel((readl(PINMUX3) | 0x1affff), PINMUX3); //RR CMNT: [18:0]-eth, [20:19]-UART0, [31:21] GIO26-20
writel(((readl(PINMUX3) & 0xfff80000) | 0x2ffff), PINMUX3); //CMNT: [18:0]-eth, [20:19]-UART0, [31:21] GIO26-20
2. Extend PHY reset time at board_eth_init@dm365evm.c from 1ms to 10ms, according DAVICOM PHY specification
udelay(10000);



[Epilog]

Unfortunately, GPIO settings in Linux vary from versions. The worse is that the setting scatters in different files. If you want to do it all at one place, you can put them in board_init()@board-dm368-ipnc.c. 
The Pinmux definition files have to be changed as well - check 
arch/arm/plat-davinci/mux.c
arch/arm/mach-davinci/mux_cfg.c
include/arch/arm/mach-davinci/mux.h