srom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * File: srom.c
  20. *
  21. * Purpose:Implement functions to access eeprom
  22. *
  23. * Author: Jerry Chen
  24. *
  25. * Date: Jan 29, 2003
  26. *
  27. * Functions:
  28. * SROMbyReadEmbedded - Embedded read eeprom via MAC
  29. * SROMbWriteEmbedded - Embedded write eeprom via MAC
  30. * SROMvRegBitsOn - Set Bits On in eeprom
  31. * SROMvRegBitsOff - Clear Bits Off in eeprom
  32. * SROMbIsRegBitsOn - Test if Bits On in eeprom
  33. * SROMbIsRegBitsOff - Test if Bits Off in eeprom
  34. * SROMvReadAllContents - Read all contents in eeprom
  35. * SROMvWriteAllContents - Write all contents in eeprom
  36. * SROMvReadEtherAddress - Read Ethernet Address in eeprom
  37. * SROMvWriteEtherAddress - Write Ethernet Address in eeprom
  38. * SROMvReadSubSysVenId - Read Sub_VID and Sub_SysId in eeprom
  39. * SROMbAutoLoad - Auto Load eeprom to MAC register
  40. *
  41. * Revision History:
  42. *
  43. */
  44. #include "upc.h"
  45. #include "tmacro.h"
  46. #include "tether.h"
  47. #include "mac.h"
  48. #include "srom.h"
  49. /*--------------------- Static Definitions -------------------------*/
  50. /*--------------------- Static Classes ----------------------------*/
  51. /*--------------------- Static Variables --------------------------*/
  52. /*--------------------- Static Functions --------------------------*/
  53. /*--------------------- Export Variables --------------------------*/
  54. /*--------------------- Export Functions --------------------------*/
  55. /*
  56. * Description: Read a byte from EEPROM, by MAC I2C
  57. *
  58. * Parameters:
  59. * In:
  60. * dwIoBase - I/O base address
  61. * byContntOffset - address of EEPROM
  62. * Out:
  63. * none
  64. *
  65. * Return Value: data read
  66. *
  67. */
  68. unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
  69. {
  70. unsigned short wDelay, wNoACK;
  71. unsigned char byWait;
  72. unsigned char byData;
  73. unsigned char byOrg;
  74. byData = 0xFF;
  75. VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
  76. /* turn off hardware retry for getting NACK */
  77. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
  78. for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
  79. VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
  80. VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
  81. /* issue read command */
  82. VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMR);
  83. /* wait DONE be set */
  84. for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
  85. VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
  86. if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
  87. break;
  88. PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
  89. }
  90. if ((wDelay < W_MAX_TIMEOUT) &&
  91. ( !(byWait & I2MCSR_NACK))) {
  92. break;
  93. }
  94. }
  95. VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
  96. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
  97. return byData;
  98. }
  99. /*
  100. * Description: Write a byte to EEPROM, by MAC I2C
  101. *
  102. * Parameters:
  103. * In:
  104. * dwIoBase - I/O base address
  105. * byContntOffset - address of EEPROM
  106. * wData - data to write
  107. * Out:
  108. * none
  109. *
  110. * Return Value: true if succeeded; false if failed.
  111. *
  112. */
  113. bool SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData)
  114. {
  115. unsigned short wDelay, wNoACK;
  116. unsigned char byWait;
  117. unsigned char byOrg;
  118. VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
  119. /* turn off hardware retry for getting NACK */
  120. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
  121. for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
  122. VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
  123. VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
  124. VNSvOutPortB(dwIoBase + MAC_REG_I2MDOPT, byData);
  125. /* issue write command */
  126. VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMW);
  127. /* wait DONE be set */
  128. for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
  129. VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
  130. if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
  131. break;
  132. PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
  133. }
  134. if ((wDelay < W_MAX_TIMEOUT) &&
  135. ( !(byWait & I2MCSR_NACK))) {
  136. break;
  137. }
  138. }
  139. if (wNoACK == W_MAX_I2CRETRY) {
  140. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
  141. return false;
  142. }
  143. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
  144. return true;
  145. }
  146. /*
  147. * Description: Turn bits on in eeprom
  148. *
  149. * Parameters:
  150. * In:
  151. * dwIoBase - I/O base address
  152. * byContntOffset - address of EEPROM
  153. * byBits - bits to turn on
  154. * Out:
  155. * none
  156. *
  157. * Return Value: none
  158. *
  159. */
  160. void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
  161. {
  162. unsigned char byOrgData;
  163. byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
  164. SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData | byBits));
  165. }
  166. /*
  167. * Description: Turn bits off in eeprom
  168. *
  169. * Parameters:
  170. * In:
  171. * dwIoBase - I/O base address
  172. * byContntOffset - address of EEPROM
  173. * byBits - bits to turn off
  174. * Out:
  175. * none
  176. *
  177. */
  178. void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
  179. {
  180. unsigned char byOrgData;
  181. byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
  182. SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData & (~byBits)));
  183. }
  184. /*
  185. * Description: Test if bits on in eeprom
  186. *
  187. * Parameters:
  188. * In:
  189. * dwIoBase - I/O base address
  190. * byContntOffset - address of EEPROM
  191. * byTestBits - bits to test
  192. * Out:
  193. * none
  194. *
  195. * Return Value: true if all test bits on; otherwise false
  196. *
  197. */
  198. bool SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
  199. {
  200. unsigned char byOrgData;
  201. byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
  202. return (byOrgData & byTestBits) == byTestBits;
  203. }
  204. /*
  205. * Description: Test if bits off in eeprom
  206. *
  207. * Parameters:
  208. * In:
  209. * dwIoBase - I/O base address
  210. * byContntOffset - address of EEPROM
  211. * byTestBits - bits to test
  212. * Out:
  213. * none
  214. *
  215. * Return Value: true if all test bits off; otherwise false
  216. *
  217. */
  218. bool SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
  219. {
  220. unsigned char byOrgData;
  221. byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
  222. return !(byOrgData & byTestBits);
  223. }
  224. /*
  225. * Description: Read all contents of eeprom to buffer
  226. *
  227. * Parameters:
  228. * In:
  229. * dwIoBase - I/O base address
  230. * Out:
  231. * pbyEepromRegs - EEPROM content Buffer
  232. *
  233. * Return Value: none
  234. *
  235. */
  236. void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
  237. {
  238. int ii;
  239. /* ii = Rom Address */
  240. for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
  241. *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase,(unsigned char) ii);
  242. pbyEepromRegs++;
  243. }
  244. }
  245. /*
  246. * Description: Write all contents of buffer to eeprom
  247. *
  248. * Parameters:
  249. * In:
  250. * dwIoBase - I/O base address
  251. * pbyEepromRegs - EEPROM content Buffer
  252. * Out:
  253. * none
  254. *
  255. * Return Value: none
  256. *
  257. */
  258. void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
  259. {
  260. int ii;
  261. /* ii = Rom Address */
  262. for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
  263. SROMbWriteEmbedded(dwIoBase,(unsigned char) ii, *pbyEepromRegs);
  264. pbyEepromRegs++;
  265. }
  266. }
  267. /*
  268. * Description: Read Ethernet Address from eeprom to buffer
  269. *
  270. * Parameters:
  271. * In:
  272. * dwIoBase - I/O base address
  273. * Out:
  274. * pbyEtherAddress - Ethernet Address buffer
  275. *
  276. * Return Value: none
  277. *
  278. */
  279. void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
  280. {
  281. unsigned char ii;
  282. /* ii = Rom Address */
  283. for (ii = 0; ii < ETH_ALEN; ii++) {
  284. *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
  285. pbyEtherAddress++;
  286. }
  287. }
  288. /*
  289. * Description: Write Ethernet Address from buffer to eeprom
  290. *
  291. * Parameters:
  292. * In:
  293. * dwIoBase - I/O base address
  294. * pbyEtherAddress - Ethernet Address buffer
  295. * Out:
  296. * none
  297. *
  298. * Return Value: none
  299. *
  300. */
  301. void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
  302. {
  303. unsigned char ii;
  304. /* ii = Rom Address */
  305. for (ii = 0; ii < ETH_ALEN; ii++) {
  306. SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
  307. pbyEtherAddress++;
  308. }
  309. }
  310. /*
  311. * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
  312. *
  313. * Parameters:
  314. * In:
  315. * dwIoBase - I/O base address
  316. * Out:
  317. * pdwSubSysVenId - Sub_VID and Sub_SysId read
  318. *
  319. * Return Value: none
  320. *
  321. */
  322. void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
  323. {
  324. unsigned char *pbyData;
  325. pbyData = (unsigned char *)pdwSubSysVenId;
  326. /* sub vendor */
  327. *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
  328. *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
  329. /* sub system */
  330. *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
  331. *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
  332. }
  333. /*
  334. * Description: Auto Load EEPROM to MAC register
  335. *
  336. * Parameters:
  337. * In:
  338. * dwIoBase - I/O base address
  339. * Out:
  340. * none
  341. *
  342. * Return Value: true if success; otherwise false
  343. *
  344. */
  345. bool SROMbAutoLoad(unsigned long dwIoBase)
  346. {
  347. unsigned char byWait;
  348. int ii;
  349. unsigned char byOrg;
  350. VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
  351. /* turn on hardware retry */
  352. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
  353. MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
  354. /* ii = Rom Address */
  355. for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
  356. MACvTimer0MicroSDelay(dwIoBase, CB_EEPROM_READBYTE_WAIT);
  357. VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
  358. if ( !(byWait & I2MCSR_AUTOLD))
  359. break;
  360. }
  361. VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
  362. if (ii == EEP_MAX_CONTEXT_SIZE)
  363. return false;
  364. return true;
  365. }