dc_srom.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * (C) Copyright 2002 ELTEC Elektronik AG
  3. * Frank Gottschling <fgottschling@eltec.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * SRom I/O routines.
  25. */
  26. #include <common.h>
  27. #include <pci.h>
  28. #include "srom.h"
  29. #define SROM_RD 0x00004000 /* Read from Boot ROM */
  30. #define SROM_WR 0x00002000 /* Write to Boot ROM */
  31. #define SROM_SR 0x00000800 /* Select Serial ROM when set */
  32. #define DT_IN 0x00000004 /* Serial Data In */
  33. #define DT_CLK 0x00000002 /* Serial ROM Clock */
  34. #define DT_CS 0x00000001 /* Serial ROM Chip Select */
  35. static u_int dc_srom_iobase;
  36. /*----------------------------------------------------------------------------*/
  37. static int inl(u_long addr)
  38. {
  39. return le32_to_cpu(*(volatile u_long *)(addr));
  40. }
  41. /*----------------------------------------------------------------------------*/
  42. static void outl (int command, u_long addr)
  43. {
  44. *(volatile u_long *)(addr) = cpu_to_le32(command);
  45. }
  46. /*----------------------------------------------------------------------------*/
  47. static void sendto_srom(u_int command, u_long addr)
  48. {
  49. outl(command, addr);
  50. udelay(1);
  51. return;
  52. }
  53. /*----------------------------------------------------------------------------*/
  54. static int getfrom_srom(u_long addr)
  55. {
  56. s32 tmp;
  57. tmp = inl(addr);
  58. udelay(1);
  59. return tmp;
  60. }
  61. /*----------------------------------------------------------------------------*/
  62. static void srom_latch (u_int command, u_long addr)
  63. {
  64. sendto_srom (command, addr);
  65. sendto_srom (command | DT_CLK, addr);
  66. sendto_srom (command, addr);
  67. return;
  68. }
  69. /*----------------------------------------------------------------------------*/
  70. static void srom_command_rd (u_int command, u_long addr)
  71. {
  72. srom_latch (command, addr);
  73. srom_latch (command, addr);
  74. srom_latch ((command & 0x0000ff00) | DT_CS, addr);
  75. return;
  76. }
  77. /*----------------------------------------------------------------------------*/
  78. static void srom_command_wr (u_int command, u_long addr)
  79. {
  80. srom_latch (command, addr);
  81. srom_latch ((command & 0x0000ff00) | DT_CS, addr);
  82. srom_latch (command, addr);
  83. return;
  84. }
  85. /*----------------------------------------------------------------------------*/
  86. static void srom_address(u_int command, u_long addr, u_char offset)
  87. {
  88. int i;
  89. signed char a;
  90. a = (char)(offset << 2);
  91. for (i=0; i<6; i++, a <<= 1)
  92. {
  93. srom_latch(command | ((a < 0) ? DT_IN : 0), addr);
  94. }
  95. udelay(1);
  96. i = (getfrom_srom(addr) >> 3) & 0x01;
  97. return;
  98. }
  99. /*----------------------------------------------------------------------------*/
  100. static short srom_data_rd (u_int command, u_long addr)
  101. {
  102. int i;
  103. short word = 0;
  104. s32 tmp;
  105. for (i=0; i<16; i++)
  106. {
  107. sendto_srom(command | DT_CLK, addr);
  108. tmp = getfrom_srom(addr);
  109. sendto_srom(command, addr);
  110. word = (word << 1) | ((tmp >> 3) & 0x01);
  111. }
  112. sendto_srom(command & 0x0000ff00, addr);
  113. return word;
  114. }
  115. /*----------------------------------------------------------------------------*/
  116. static int srom_data_wr (u_int command, u_long addr, short val)
  117. {
  118. int i;
  119. u_long longVal;
  120. s32 tmp;
  121. longVal = (u_long)(le16_to_cpu(val));
  122. for (i=0; i<16; i++)
  123. {
  124. tmp = (longVal & 0x8000)>>13;
  125. sendto_srom (tmp | command, addr);
  126. sendto_srom (tmp | command | DT_CLK, addr);
  127. sendto_srom (tmp | command, addr);
  128. longVal = longVal<<1;
  129. }
  130. sendto_srom(command & 0x0000ff00, addr);
  131. sendto_srom(command, addr);
  132. tmp = 100;
  133. do
  134. {
  135. if ((getfrom_srom(dc_srom_iobase) & 0x8) == 0x8)
  136. break;
  137. udelay(1000);
  138. } while (--tmp);
  139. if (tmp == 0)
  140. {
  141. printf("Write DEC21143 SRom timed out !\n");
  142. return (-1);
  143. }
  144. return 0;
  145. }
  146. /*----------------------------------------------------------------------------*/
  147. static short srom_rd (u_long addr, u_char offset)
  148. {
  149. sendto_srom (SROM_RD | SROM_SR, addr);
  150. srom_latch (SROM_RD | SROM_SR | DT_CS, addr);
  151. srom_command_rd (SROM_RD | SROM_SR | DT_IN | DT_CS, addr);
  152. srom_address (SROM_RD | SROM_SR | DT_CS, addr, offset);
  153. return srom_data_rd (SROM_RD | SROM_SR | DT_CS, addr);
  154. }
  155. /*----------------------------------------------------------------------------*/
  156. static void srom_wr_enable (u_long addr)
  157. {
  158. int i;
  159. sendto_srom (SROM_WR | SROM_SR, addr);
  160. srom_latch (SROM_WR | SROM_SR | DT_CS, addr);
  161. srom_latch (SROM_WR | SROM_SR | DT_IN | DT_CS, addr);
  162. srom_latch (SROM_WR | SROM_SR | DT_CS, addr);
  163. srom_latch (SROM_WR | SROM_SR | DT_CS, addr);
  164. for (i=0; i<6; i++)
  165. {
  166. srom_latch (SROM_WR | SROM_SR | DT_IN | DT_CS, addr);
  167. }
  168. }
  169. /*----------------------------------------------------------------------------*/
  170. static int srom_wr (u_long addr, u_char offset, short val)
  171. {
  172. srom_wr_enable (addr);
  173. sendto_srom (SROM_WR | SROM_SR, addr);
  174. srom_latch (SROM_WR | SROM_SR | DT_CS, addr);
  175. srom_command_wr (SROM_WR | SROM_SR | DT_IN | DT_CS, addr);
  176. srom_address (SROM_WR | SROM_SR | DT_CS, addr, offset);
  177. return srom_data_wr (SROM_WR | SROM_SR | DT_CS, addr, val);
  178. }
  179. /*----------------------------------------------------------------------------*/
  180. /*
  181. * load data from the srom
  182. */
  183. int dc_srom_load (u_short *dest)
  184. {
  185. int offset;
  186. short tmp;
  187. /* get srom iobase from local network controller */
  188. pci_read_config_dword(PCI_BDF(0,14,0), PCI_BASE_ADDRESS_1, &dc_srom_iobase);
  189. dc_srom_iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  190. dc_srom_iobase = pci_mem_to_phys(PCI_BDF(0,14,0), dc_srom_iobase);
  191. dc_srom_iobase += 0x48; /* io offset for srom access */
  192. memset (dest, 0, 128);
  193. for (offset=0; offset<64; offset++)
  194. {
  195. tmp = srom_rd (dc_srom_iobase, offset);
  196. *dest++ = le16_to_cpu(tmp);
  197. }
  198. return (0);
  199. }
  200. /*----------------------------------------------------------------------------*/
  201. /*
  202. * store data into the srom
  203. */
  204. int dc_srom_store (u_short *src)
  205. {
  206. int offset;
  207. /* get srom iobase from local network controller */
  208. pci_read_config_dword(PCI_BDF(0,14,0), PCI_BASE_ADDRESS_1, &dc_srom_iobase);
  209. dc_srom_iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  210. dc_srom_iobase = pci_mem_to_phys(PCI_BDF(0,14,0), dc_srom_iobase);
  211. dc_srom_iobase += 0x48; /* io offset for srom access */
  212. for (offset=0; offset<64; offset++)
  213. {
  214. if (srom_wr (dc_srom_iobase, offset, *src) == -1)
  215. return (-1);
  216. src++;
  217. }
  218. return (0);
  219. }
  220. /*----------------------------------------------------------------------------*/