sdram.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) Freescale Semiconductor, Inc. 2006-2007
  3. * Copyright (C) Sheldon Instruments, Inc. 2008
  4. *
  5. * Author: Ron Madrid <info@sheldoninst.com>
  6. *
  7. * (C) Copyright 2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <mpc83xx.h>
  30. #include <spd_sdram.h>
  31. #include <asm/bitops.h>
  32. #include <asm/io.h>
  33. #include <asm/processor.h>
  34. #include <asm/mmu.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. static long fixed_sdram(void);
  37. #if defined(CONFIG_NAND_SPL)
  38. void si_wait_i2c(void)
  39. {
  40. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  41. while (!(__raw_readb(&im->i2c[0].sr) & 0x02))
  42. ;
  43. __raw_writeb(0x00, &im->i2c[0].sr);
  44. sync();
  45. return;
  46. }
  47. void si_read_i2c(u32 lbyte, int count, u8 *buffer)
  48. {
  49. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  50. u32 i;
  51. u8 chip = 0x50 << 1; /* boot sequencer I2C */
  52. u32 ubyte = (lbyte & 0xff00) >> 8;
  53. lbyte &= 0xff;
  54. /*
  55. * Set up controller
  56. */
  57. __raw_writeb(0x3f, &im->i2c[0].fdr);
  58. __raw_writeb(0x00, &im->i2c[0].adr);
  59. __raw_writeb(0x00, &im->i2c[0].sr);
  60. __raw_writeb(0x00, &im->i2c[0].dr);
  61. while (__raw_readb(&im->i2c[0].sr) & 0x20)
  62. ;
  63. /*
  64. * Writing address to device
  65. */
  66. __raw_writeb(0xb0, &im->i2c[0].cr);
  67. sync();
  68. __raw_writeb(chip, &im->i2c[0].dr);
  69. si_wait_i2c();
  70. __raw_writeb(0xb0, &im->i2c[0].cr);
  71. sync();
  72. __raw_writeb(ubyte, &im->i2c[0].dr);
  73. si_wait_i2c();
  74. __raw_writeb(lbyte, &im->i2c[0].dr);
  75. si_wait_i2c();
  76. __raw_writeb(0xb4, &im->i2c[0].cr);
  77. sync();
  78. __raw_writeb(chip + 1, &im->i2c[0].dr);
  79. si_wait_i2c();
  80. __raw_writeb(0xa0, &im->i2c[0].cr);
  81. sync();
  82. /*
  83. * Dummy read
  84. */
  85. __raw_readb(&im->i2c[0].dr);
  86. si_wait_i2c();
  87. /*
  88. * Read actual data
  89. */
  90. for (i = 0; i < count; i++)
  91. {
  92. if (i == (count - 2)) /* Reached next to last byte, No ACK */
  93. __raw_writeb(0xa8, &im->i2c[0].cr);
  94. if (i == (count - 1)) /* Reached last byte, STOP */
  95. __raw_writeb(0x88, &im->i2c[0].cr);
  96. /* Read byte of data */
  97. buffer[i] = __raw_readb(&im->i2c[0].dr);
  98. if (i == (count - 1))
  99. break;
  100. si_wait_i2c();
  101. }
  102. return;
  103. }
  104. #endif /* CONFIG_NAND_SPL */
  105. phys_size_t initdram(int board_type)
  106. {
  107. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  108. volatile fsl_lbus_t *lbc= &im->lbus;
  109. u32 msize;
  110. if ((__raw_readl(&im->sysconf.immrbar) & IMMRBAR_BASE_ADDR) != (u32) im)
  111. return -1;
  112. /* DDR SDRAM - Main SODIMM */
  113. __raw_writel(CONFIG_SYS_DDR_BASE & LAWBAR_BAR, &im->sysconf.ddrlaw[0].bar);
  114. msize = fixed_sdram();
  115. /* Local Bus setup lbcr and mrtpr */
  116. __raw_writel(CONFIG_SYS_LBC_LBCR, &lbc->lbcr);
  117. __raw_writel(CONFIG_SYS_LBC_MRTPR, &lbc->mrtpr);
  118. sync();
  119. /* return total bus SDRAM size(bytes) -- DDR */
  120. return (msize * 1024 * 1024);
  121. }
  122. /*************************************************************************
  123. * fixed sdram init -- reads values from boot sequencer I2C
  124. ************************************************************************/
  125. static long fixed_sdram(void)
  126. {
  127. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  128. u32 msizelog2, msize = 1;
  129. #if defined(CONFIG_NAND_SPL)
  130. u32 i;
  131. const u8 bytecount = 135;
  132. u8 buffer[bytecount];
  133. u32 addr, data;
  134. si_read_i2c(0, bytecount, buffer);
  135. for (i = 18; i < bytecount; i += 7){
  136. addr = (u32)buffer[i];
  137. addr <<= 8;
  138. addr |= (u32)buffer[i + 1];
  139. addr <<= 2;
  140. data = (u32)buffer[i + 2];
  141. data <<= 8;
  142. data |= (u32)buffer[i + 3];
  143. data <<= 8;
  144. data |= (u32)buffer[i + 4];
  145. data <<= 8;
  146. data |= (u32)buffer[i + 5];
  147. __raw_writel(data, (u32 *)(CONFIG_SYS_IMMR + addr));
  148. }
  149. sync();
  150. /* enable DDR controller */
  151. __raw_writel((__raw_readl(&im->ddr.sdram_cfg) | SDRAM_CFG_MEM_EN), &im->ddr.sdram_cfg);
  152. #endif /* (CONFIG_NAND_SPL) */
  153. msizelog2 = ((__raw_readl(&im->sysconf.ddrlaw[0].ar) & LAWAR_SIZE) + 1);
  154. msize <<= (msizelog2 - 20);
  155. return msize;
  156. }