sis900.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* -*- Mode:C; c-basic-offset:4; -*- */
  2. /*
  3. sis900.c: An SiS 900/7016 PCI Fast Ethernet driver for Etherboot
  4. Copyright (C) 2001 Entity Cyber, Inc.
  5. Revision: 1.0 March 1, 2001
  6. Author: Marty Connor (mdc@thinguin.org)
  7. Adapted from a Linux driver which was written by Donald Becker
  8. and modified by Ollie Lho and Chin-Shan Li of SiS Corporation.
  9. Rewritten for Etherboot by Marty Connor.
  10. This software may be used and distributed according to the terms
  11. of the GNU Public License (GPL), incorporated herein by reference.
  12. References:
  13. SiS 7016 Fast Ethernet PCI Bus 10/100 Mbps LAN Controller with OnNow Support,
  14. preliminary Rev. 1.0 Jan. 14, 1998
  15. SiS 900 Fast Ethernet PCI Bus 10/100 Mbps LAN Single Chip with OnNow Support,
  16. preliminary Rev. 1.0 Nov. 10, 1998
  17. SiS 7014 Single Chip 100BASE-TX/10BASE-T Physical Layer Solution,
  18. preliminary Rev. 1.0 Jan. 18, 1998
  19. http://www.sis.com.tw/support/databook.htm */
  20. /* Revision History */
  21. /*
  22. 01 March 2001 mdc 1.0
  23. Initial Release. Tested with PCI based sis900 card and ThinkNIC
  24. computer.
  25. 20 March 2001 P.Koegel
  26. added support for sis630e and PHY ICS1893 and RTL8201
  27. Testet with SIS730S chipset + ICS1893
  28. */
  29. /* Includes */
  30. #include "etherboot.h"
  31. #include "nic.h"
  32. #include "pci.h"
  33. #include "cards.h"
  34. #include "sis900.h"
  35. /* Globals */
  36. static int sis900_debug = 0;
  37. static unsigned short vendor, dev_id;
  38. static unsigned long ioaddr;
  39. static unsigned int cur_phy;
  40. static unsigned int cur_rx;
  41. static BufferDesc txd;
  42. static BufferDesc rxd[NUM_RX_DESC];
  43. #ifdef USE_LOWMEM_BUFFER
  44. #define txb ((char *)0x10000 - TX_BUF_SIZE)
  45. #define rxb ((char *)0x10000 - NUM_RX_DESC*RX_BUF_SIZE - TX_BUF_SIZE)
  46. #else
  47. static unsigned char txb[TX_BUF_SIZE];
  48. static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
  49. #endif
  50. static struct mac_chip_info {
  51. const char *name;
  52. u16 vendor_id, device_id, flags;
  53. int io_size;
  54. } mac_chip_table[] = {
  55. { "SiS 900 PCI Fast Ethernet", PCI_VENDOR_ID_SIS, PCI_DEVICE_ID_SIS900,
  56. PCI_COMMAND_IO|PCI_COMMAND_MASTER, SIS900_TOTAL_SIZE},
  57. { "SiS 7016 PCI Fast Ethernet",PCI_VENDOR_ID_SIS, PCI_DEVICE_ID_SIS7016,
  58. PCI_COMMAND_IO|PCI_COMMAND_MASTER, SIS900_TOTAL_SIZE},
  59. {0,0,0,0,0} /* 0 terminated list. */
  60. };
  61. static void sis900_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex);
  62. static void amd79c901_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex);
  63. static void ics1893_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex);
  64. static void rtl8201_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex);
  65. static struct mii_chip_info {
  66. const char * name;
  67. u16 phy_id0;
  68. u16 phy_id1;
  69. void (*read_mode) (struct nic *nic, int phy_addr, int *speed, int *duplex);
  70. } mii_chip_table[] = {
  71. {"SiS 900 Internal MII PHY", 0x001d, 0x8000, sis900_read_mode},
  72. {"SiS 7014 Physical Layer Solution", 0x0016, 0xf830,sis900_read_mode},
  73. {"AMD 79C901 10BASE-T PHY", 0x0000, 0x35b9, amd79c901_read_mode},
  74. {"AMD 79C901 HomePNA PHY", 0x0000, 0x35c8, amd79c901_read_mode},
  75. {"ICS 1893 Integrated PHYceiver" , 0x0015, 0xf441,ics1893_read_mode},
  76. {"RTL 8201 10/100Mbps Phyceiver" , 0x0000, 0x8201,rtl8201_read_mode},
  77. {0,0,0,0}
  78. };
  79. static struct mii_phy {
  80. struct mii_phy * next;
  81. struct mii_chip_info * chip_info;
  82. int phy_addr;
  83. u16 status;
  84. } mii;
  85. // PCI to ISA bridge for SIS640E access
  86. static struct pci_device pci_isa_bridge_list[] = {
  87. { 0x1039, 0x0008,
  88. "SIS 85C503/5513 PCI to ISA bridge", 0, 0, 0, 0},
  89. {0, 0, NULL, 0, 0, 0, 0}
  90. };
  91. /* Function Prototypes */
  92. struct nic *sis900_probe(struct nic *nic, unsigned short *io_addrs, struct pci_device *pci);
  93. static u16 sis900_read_eeprom(int location);
  94. static void sis900_mdio_reset(long mdio_addr);
  95. static void sis900_mdio_idle(long mdio_addr);
  96. static u16 sis900_mdio_read(int phy_id, int location);
  97. static void sis900_mdio_write(int phy_id, int location, int val);
  98. static void sis900_init(struct nic *nic);
  99. static void sis900_reset(struct nic *nic);
  100. static void sis900_init_rxfilter(struct nic *nic);
  101. static void sis900_init_txd(struct nic *nic);
  102. static void sis900_init_rxd(struct nic *nic);
  103. static void sis900_set_rx_mode(struct nic *nic);
  104. static void sis900_check_mode(struct nic *nic);
  105. static void sis900_transmit(struct nic *nic, const char *d,
  106. unsigned int t, unsigned int s, const char *p);
  107. static int sis900_poll(struct nic *nic);
  108. static void sis900_disable(struct nic *nic);
  109. /**
  110. * sis900_get_mac_addr: - Get MAC address for stand alone SiS900 model
  111. * @pci_dev: the sis900 pci device
  112. * @net_dev: the net device to get address for
  113. *
  114. * Older SiS900 and friends, use EEPROM to store MAC address.
  115. * MAC address is read from read_eeprom() into @net_dev->dev_addr.
  116. */
  117. static int sis900_get_mac_addr(struct pci_device * pci_dev , struct nic *nic)
  118. {
  119. u16 signature;
  120. int i;
  121. /* check to see if we have sane EEPROM */
  122. signature = (u16) sis900_read_eeprom( EEPROMSignature);
  123. if (signature == 0xffff || signature == 0x0000) {
  124. printf ("sis900_probe: Error EERPOM read %hX\n", signature);
  125. return 0;
  126. }
  127. /* get MAC address from EEPROM */
  128. for (i = 0; i < 3; i++)
  129. ((u16 *)(nic->node_addr))[i] = sis900_read_eeprom(i+EEPROMMACAddr);
  130. return 1;
  131. }
  132. /**
  133. * sis630e_get_mac_addr: - Get MAC address for SiS630E model
  134. * @pci_dev: the sis900 pci device
  135. * @net_dev: the net device to get address for
  136. *
  137. * SiS630E model, use APC CMOS RAM to store MAC address.
  138. * APC CMOS RAM is accessed through ISA bridge.
  139. * MAC address is read into @net_dev->dev_addr.
  140. */
  141. static int sis630e_get_mac_addr(struct pci_device * pci_dev, struct nic *nic)
  142. {
  143. u8 reg;
  144. int i;
  145. struct pci_device *p;
  146. // find PCI to ISA bridge
  147. eth_pci_init(pci_isa_bridge_list);
  148. /* the firts entry in this list should contain bus/devfn */
  149. p = pci_isa_bridge_list;
  150. pcibios_read_config_byte(p->bus,p->devfn, 0x48, &reg);
  151. pcibios_write_config_byte(p->bus,p->devfn, 0x48, reg | 0x40);
  152. for (i = 0; i < ETH_ALEN; i++)
  153. {
  154. outb(0x09 + i, 0x70);
  155. ((u8 *)(nic->node_addr))[i] = inb(0x71);
  156. }
  157. pcibios_write_config_byte(p->bus,p->devfn, 0x48, reg & ~0x40);
  158. return 1;
  159. }
  160. /*
  161. * Function: sis900_probe
  162. *
  163. * Description: initializes initializes the NIC, retrieves the
  164. * MAC address of the card, and sets up some globals required by
  165. * other routines.
  166. *
  167. * Side effects:
  168. * leaves the ioaddress of the sis900 chip in the variable ioaddr.
  169. * leaves the sis900 initialized, and ready to recieve packets.
  170. *
  171. * Returns: struct nic *: pointer to NIC data structure
  172. */
  173. struct nic *sis900_probe(struct nic *nic, unsigned short *io_addrs, struct pci_device *pci)
  174. {
  175. int i;
  176. int found=0;
  177. int phy_addr;
  178. u16 signature;
  179. u8 revision;
  180. int ret;
  181. if (io_addrs == 0 || *io_addrs == 0)
  182. return NULL;
  183. ioaddr = *io_addrs & ~3;
  184. vendor = pci->vendor;
  185. dev_id = pci->dev_id;
  186. /* wakeup chip */
  187. pcibios_write_config_dword(pci->bus, pci->devfn, 0x40, 0x00000000);
  188. adjust_pci_device(pci);
  189. /* get MAC address */
  190. ret = 0;
  191. pcibios_read_config_byte(pci->bus,pci->devfn, PCI_REVISION, &revision);
  192. if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV)
  193. ret = sis630e_get_mac_addr(pci, nic);
  194. else if (revision == SIS630S_900_REV)
  195. ret = sis630e_get_mac_addr(pci, nic);
  196. else
  197. ret = sis900_get_mac_addr(pci, nic);
  198. if (ret == 0)
  199. {
  200. printf ("sis900_probe: Error MAC address not found\n");
  201. return NULL;
  202. }
  203. printf("\nsis900_probe: MAC addr %! at ioaddr %#hX\n",
  204. nic->node_addr, ioaddr);
  205. printf("sis900_probe: Vendor:%#hX Device:%#hX\n", vendor, dev_id);
  206. /* probe for mii transceiver */
  207. /* search for total of 32 possible mii phy addresses */
  208. found = 0;
  209. for (phy_addr = 0; phy_addr < 32; phy_addr++) {
  210. u16 mii_status;
  211. u16 phy_id0, phy_id1;
  212. mii_status = sis900_mdio_read(phy_addr, MII_STATUS);
  213. if (mii_status == 0xffff || mii_status == 0x0000)
  214. /* the mii is not accessable, try next one */
  215. continue;
  216. phy_id0 = sis900_mdio_read(phy_addr, MII_PHY_ID0);
  217. phy_id1 = sis900_mdio_read(phy_addr, MII_PHY_ID1);
  218. /* search our mii table for the current mii */
  219. for (i = 0; mii_chip_table[i].phy_id1; i++) {
  220. if (phy_id0 == mii_chip_table[i].phy_id0) {
  221. printf("sis900_probe: %s transceiver found at address %d.\n",
  222. mii_chip_table[i].name, phy_addr);
  223. mii.chip_info = &mii_chip_table[i];
  224. mii.phy_addr = phy_addr;
  225. mii.status = sis900_mdio_read(phy_addr, MII_STATUS);
  226. mii.next = NULL;
  227. found=1;
  228. break;
  229. }
  230. }
  231. }
  232. if (found == 0) {
  233. printf("sis900_probe: No MII transceivers found!\n");
  234. return NULL;
  235. }
  236. /* Arbitrarily select the last PHY found as current PHY */
  237. cur_phy = mii.phy_addr;
  238. printf("sis900_probe: Using %s as default\n", mii.chip_info->name);
  239. /* initialize device */
  240. sis900_init(nic);
  241. nic->reset = sis900_init;
  242. nic->poll = sis900_poll;
  243. nic->transmit = sis900_transmit;
  244. nic->disable = sis900_disable;
  245. return nic;
  246. }
  247. /*
  248. * EEPROM Routines: These functions read and write to EEPROM for
  249. * retrieving the MAC address and other configuration information about
  250. * the card.
  251. */
  252. /* Delay between EEPROM clock transitions. */
  253. #define eeprom_delay() inl(ee_addr)
  254. /* Function: sis900_read_eeprom
  255. *
  256. * Description: reads and returns a given location from EEPROM
  257. *
  258. * Arguments: int location: requested EEPROM location
  259. *
  260. * Returns: u16: contents of requested EEPROM location
  261. *
  262. */
  263. /* Read Serial EEPROM through EEPROM Access Register, Note that location is
  264. in word (16 bits) unit */
  265. static u16 sis900_read_eeprom(int location)
  266. {
  267. int i;
  268. u16 retval = 0;
  269. long ee_addr = ioaddr + mear;
  270. u32 read_cmd = location | EEread;
  271. outl(0, ee_addr);
  272. eeprom_delay();
  273. outl(EECLK, ee_addr);
  274. eeprom_delay();
  275. /* Shift the read command (9) bits out. */
  276. for (i = 8; i >= 0; i--) {
  277. u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
  278. outl(dataval, ee_addr);
  279. eeprom_delay();
  280. outl(dataval | EECLK, ee_addr);
  281. eeprom_delay();
  282. }
  283. outb(EECS, ee_addr);
  284. eeprom_delay();
  285. /* read the 16-bits data in */
  286. for (i = 16; i > 0; i--) {
  287. outl(EECS, ee_addr);
  288. eeprom_delay();
  289. outl(EECS | EECLK, ee_addr);
  290. eeprom_delay();
  291. retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
  292. eeprom_delay();
  293. }
  294. /* Terminate the EEPROM access. */
  295. outl(0, ee_addr);
  296. eeprom_delay();
  297. outl(EECLK, ee_addr);
  298. return (retval);
  299. }
  300. #define sis900_mdio_delay() inl(mdio_addr)
  301. /*
  302. Read and write the MII management registers using software-generated
  303. serial MDIO protocol. Note that the command bits and data bits are
  304. send out seperately
  305. */
  306. static void sis900_mdio_idle(long mdio_addr)
  307. {
  308. outl(MDIO | MDDIR, mdio_addr);
  309. sis900_mdio_delay();
  310. outl(MDIO | MDDIR | MDC, mdio_addr);
  311. }
  312. /* Syncronize the MII management interface by shifting 32 one bits out. */
  313. static void sis900_mdio_reset(long mdio_addr)
  314. {
  315. int i;
  316. for (i = 31; i >= 0; i--) {
  317. outl(MDDIR | MDIO, mdio_addr);
  318. sis900_mdio_delay();
  319. outl(MDDIR | MDIO | MDC, mdio_addr);
  320. sis900_mdio_delay();
  321. }
  322. return;
  323. }
  324. static u16 sis900_mdio_read(int phy_id, int location)
  325. {
  326. long mdio_addr = ioaddr + mear;
  327. int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
  328. u16 retval = 0;
  329. int i;
  330. sis900_mdio_reset(mdio_addr);
  331. sis900_mdio_idle(mdio_addr);
  332. for (i = 15; i >= 0; i--) {
  333. int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
  334. outl(dataval, mdio_addr);
  335. sis900_mdio_delay();
  336. outl(dataval | MDC, mdio_addr);
  337. sis900_mdio_delay();
  338. }
  339. /* Read the 16 data bits. */
  340. for (i = 16; i > 0; i--) {
  341. outl(0, mdio_addr);
  342. sis900_mdio_delay();
  343. retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
  344. outl(MDC, mdio_addr);
  345. sis900_mdio_delay();
  346. }
  347. return retval;
  348. }
  349. static void sis900_mdio_write(int phy_id, int location, int value)
  350. {
  351. long mdio_addr = ioaddr + mear;
  352. int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
  353. int i;
  354. sis900_mdio_reset(mdio_addr);
  355. sis900_mdio_idle(mdio_addr);
  356. /* Shift the command bits out. */
  357. for (i = 15; i >= 0; i--) {
  358. int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
  359. outb(dataval, mdio_addr);
  360. sis900_mdio_delay();
  361. outb(dataval | MDC, mdio_addr);
  362. sis900_mdio_delay();
  363. }
  364. sis900_mdio_delay();
  365. /* Shift the value bits out. */
  366. for (i = 15; i >= 0; i--) {
  367. int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
  368. outl(dataval, mdio_addr);
  369. sis900_mdio_delay();
  370. outl(dataval | MDC, mdio_addr);
  371. sis900_mdio_delay();
  372. }
  373. sis900_mdio_delay();
  374. /* Clear out extra bits. */
  375. for (i = 2; i > 0; i--) {
  376. outb(0, mdio_addr);
  377. sis900_mdio_delay();
  378. outb(MDC, mdio_addr);
  379. sis900_mdio_delay();
  380. }
  381. return;
  382. }
  383. /* Function: sis900_init
  384. *
  385. * Description: resets the ethernet controller chip and various
  386. * data structures required for sending and receiving packets.
  387. *
  388. * Arguments: struct nic *nic: NIC data structure
  389. *
  390. * returns: void.
  391. */
  392. static void
  393. sis900_init(struct nic *nic)
  394. {
  395. /* Soft reset the chip. */
  396. sis900_reset(nic);
  397. sis900_init_rxfilter(nic);
  398. sis900_init_txd(nic);
  399. sis900_init_rxd(nic);
  400. sis900_set_rx_mode(nic);
  401. sis900_check_mode(nic);
  402. outl(RxENA, ioaddr + cr);
  403. }
  404. /*
  405. * Function: sis900_reset
  406. *
  407. * Description: disables interrupts and soft resets the controller chip
  408. *
  409. * Arguments: struct nic *nic: NIC data structure
  410. *
  411. * Returns: void.
  412. */
  413. static void
  414. sis900_reset(struct nic *nic)
  415. {
  416. int i = 0;
  417. u32 status = TxRCMP | RxRCMP;
  418. outl(0, ioaddr + ier);
  419. outl(0, ioaddr + imr);
  420. outl(0, ioaddr + rfcr);
  421. outl(RxRESET | TxRESET | RESET, ioaddr + cr);
  422. /* Check that the chip has finished the reset. */
  423. while (status && (i++ < 1000)) {
  424. status ^= (inl(isr + ioaddr) & status);
  425. }
  426. outl(PESEL, ioaddr + cfg);
  427. }
  428. /* Function: sis_init_rxfilter
  429. *
  430. * Description: sets receive filter address to our MAC address
  431. *
  432. * Arguments: struct nic *nic: NIC data structure
  433. *
  434. * returns: void.
  435. */
  436. static void
  437. sis900_init_rxfilter(struct nic *nic)
  438. {
  439. u32 rfcrSave;
  440. int i;
  441. rfcrSave = inl(rfcr + ioaddr);
  442. /* disable packet filtering before setting filter */
  443. outl(rfcrSave & ~RFEN, rfcr);
  444. /* load MAC addr to filter data register */
  445. for (i = 0 ; i < 3 ; i++) {
  446. u32 w;
  447. w = (u32) *((u16 *)(nic->node_addr)+i);
  448. outl((i << RFADDR_shift), ioaddr + rfcr);
  449. outl(w, ioaddr + rfdr);
  450. if (sis900_debug > 0)
  451. printf("sis900_init_rxfilter: Receive Filter Addrss[%d]=%X\n",
  452. i, inl(ioaddr + rfdr));
  453. }
  454. /* enable packet filitering */
  455. outl(rfcrSave | RFEN, rfcr + ioaddr);
  456. }
  457. /*
  458. * Function: sis_init_txd
  459. *
  460. * Description: initializes the Tx descriptor
  461. *
  462. * Arguments: struct nic *nic: NIC data structure
  463. *
  464. * returns: void.
  465. */
  466. static void
  467. sis900_init_txd(struct nic *nic)
  468. {
  469. txd.link = (u32) 0;
  470. txd.cmdsts = (u32) 0;
  471. txd.bufptr = (u32) &txb[0];
  472. /* load Transmit Descriptor Register */
  473. outl((u32) &txd, ioaddr + txdp);
  474. if (sis900_debug > 0)
  475. printf("sis900_init_txd: TX descriptor register loaded with: %X\n",
  476. inl(ioaddr + txdp));
  477. }
  478. /* Function: sis_init_rxd
  479. *
  480. * Description: initializes the Rx descriptor ring
  481. *
  482. * Arguments: struct nic *nic: NIC data structure
  483. *
  484. * Returns: void.
  485. */
  486. static void
  487. sis900_init_rxd(struct nic *nic)
  488. {
  489. int i;
  490. cur_rx = 0;
  491. /* init RX descriptor */
  492. for (i = 0; i < NUM_RX_DESC; i++) {
  493. rxd[i].link = (i+1 < NUM_RX_DESC) ? (u32) &rxd[i+1] : (u32) &rxd[0];
  494. rxd[i].cmdsts = (u32) RX_BUF_SIZE;
  495. rxd[i].bufptr = (u32) &rxb[i*RX_BUF_SIZE];
  496. if (sis900_debug > 0)
  497. printf("sis900_init_rxd: rxd[%d]=%X link=%X cmdsts=%X bufptr=%X\n",
  498. i, &rxd[i], rxd[i].link, rxd[i].cmdsts, rxd[i].bufptr);
  499. }
  500. /* load Receive Descriptor Register */
  501. outl((u32) &rxd[0], ioaddr + rxdp);
  502. if (sis900_debug > 0)
  503. printf("sis900_init_rxd: RX descriptor register loaded with: %X\n",
  504. inl(ioaddr + rxdp));
  505. }
  506. /* Function: sis_init_rxd
  507. *
  508. * Description:
  509. * sets the receive mode to accept all broadcast packets and packets
  510. * with our MAC address, and reject all multicast packets.
  511. *
  512. * Arguments: struct nic *nic: NIC data structure
  513. *
  514. * Returns: void.
  515. */
  516. static void sis900_set_rx_mode(struct nic *nic)
  517. {
  518. int i;
  519. /* Configure Multicast Hash Table in Receive Filter
  520. to reject all MCAST packets */
  521. for (i = 0; i < 8; i++) {
  522. /* why plus 0x04? That makes the correct value for hash table. */
  523. outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
  524. outl((u32)(0x0), ioaddr + rfdr);
  525. }
  526. /* Accept Broadcast packets, destination addresses that match
  527. our MAC address */
  528. outl(RFEN | RFAAB, ioaddr + rfcr);
  529. return;
  530. }
  531. /* Function: sis900_check_mode
  532. *
  533. * Description: checks the state of transmit and receive
  534. * parameters on the NIC, and updates NIC registers to match
  535. *
  536. * Arguments: struct nic *nic: NIC data structure
  537. *
  538. * Returns: void.
  539. */
  540. static void
  541. sis900_check_mode (struct nic *nic)
  542. {
  543. int speed, duplex;
  544. u32 tx_flags = 0, rx_flags = 0;
  545. mii.chip_info->read_mode(nic, cur_phy, &speed, &duplex);
  546. tx_flags = TxATP | (TX_DMA_BURST << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
  547. rx_flags = RX_DMA_BURST << RxMXDMA_shift;
  548. if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS) {
  549. rx_flags |= (RxDRNT_10 << RxDRNT_shift);
  550. tx_flags |= (TxDRNT_10 << TxDRNT_shift);
  551. }
  552. else {
  553. rx_flags |= (RxDRNT_100 << RxDRNT_shift);
  554. tx_flags |= (TxDRNT_100 << TxDRNT_shift);
  555. }
  556. if (duplex == FDX_CAPABLE_FULL_SELECTED) {
  557. tx_flags |= (TxCSI | TxHBI);
  558. rx_flags |= RxATX;
  559. }
  560. outl (tx_flags, ioaddr + txcfg);
  561. outl (rx_flags, ioaddr + rxcfg);
  562. }
  563. /* Function: sis900_read_mode
  564. *
  565. * Description: retrieves and displays speed and duplex
  566. * parameters from the NIC
  567. *
  568. * Arguments: struct nic *nic: NIC data structure
  569. *
  570. * Returns: void.
  571. */
  572. static void
  573. sis900_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex)
  574. {
  575. int i = 0;
  576. u32 status;
  577. /* STSOUT register is Latched on Transition, read operation updates it */
  578. while (i++ < 2)
  579. status = sis900_mdio_read(phy_addr, MII_STSOUT);
  580. if (status & MII_STSOUT_SPD)
  581. *speed = HW_SPEED_100_MBPS;
  582. else
  583. *speed = HW_SPEED_10_MBPS;
  584. if (status & MII_STSOUT_DPLX)
  585. *duplex = FDX_CAPABLE_FULL_SELECTED;
  586. else
  587. *duplex = FDX_CAPABLE_HALF_SELECTED;
  588. if (status & MII_STSOUT_LINK_FAIL)
  589. printf("sis900_read_mode: Media Link Off\n");
  590. else
  591. printf("sis900_read_mode: Media Link On %s %s-duplex \n",
  592. *speed == HW_SPEED_100_MBPS ?
  593. "100mbps" : "10mbps",
  594. *duplex == FDX_CAPABLE_FULL_SELECTED ?
  595. "full" : "half");
  596. }
  597. /* Function: amd79c901_read_mode
  598. *
  599. * Description: retrieves and displays speed and duplex
  600. * parameters from the NIC
  601. *
  602. * Arguments: struct nic *nic: NIC data structure
  603. *
  604. * Returns: void.
  605. */
  606. static void
  607. amd79c901_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex)
  608. {
  609. int i;
  610. u16 status;
  611. for (i = 0; i < 2; i++)
  612. status = sis900_mdio_read(phy_addr, MII_STATUS);
  613. if (status & MII_STAT_CAN_AUTO) {
  614. /* 10BASE-T PHY */
  615. for (i = 0; i < 2; i++)
  616. status = sis900_mdio_read(phy_addr, MII_STATUS_SUMMARY);
  617. if (status & MII_STSSUM_SPD)
  618. *speed = HW_SPEED_100_MBPS;
  619. else
  620. *speed = HW_SPEED_10_MBPS;
  621. if (status & MII_STSSUM_DPLX)
  622. *duplex = FDX_CAPABLE_FULL_SELECTED;
  623. else
  624. *duplex = FDX_CAPABLE_HALF_SELECTED;
  625. if (status & MII_STSSUM_LINK)
  626. printf("amd79c901_read_mode: Media Link On %s %s-duplex \n",
  627. *speed == HW_SPEED_100_MBPS ?
  628. "100mbps" : "10mbps",
  629. *duplex == FDX_CAPABLE_FULL_SELECTED ?
  630. "full" : "half");
  631. else
  632. printf("amd79c901_read_mode: Media Link Off\n");
  633. }
  634. else {
  635. /* HomePNA */
  636. *speed = HW_SPEED_HOME;
  637. *duplex = FDX_CAPABLE_HALF_SELECTED;
  638. if (status & MII_STAT_LINK)
  639. printf("amd79c901_read_mode:Media Link On 1mbps half-duplex \n");
  640. else
  641. printf("amd79c901_read_mode: Media Link Off\n");
  642. }
  643. }
  644. /**
  645. * ics1893_read_mode: - read media mode for ICS1893 PHY
  646. * @net_dev: the net device to read mode for
  647. * @phy_addr: mii phy address
  648. * @speed: the transmit speed to be determined
  649. * @duplex: the duplex mode to be determined
  650. *
  651. * ICS1893 PHY use Quick Poll Detailed Status register
  652. * to determine the speed and duplex mode for sis900
  653. */
  654. static void ics1893_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex)
  655. {
  656. int i = 0;
  657. u32 status;
  658. /* MII_QPDSTS is Latched, read twice in succession will reflect the current state */
  659. for (i = 0; i < 2; i++)
  660. status = sis900_mdio_read(phy_addr, MII_QPDSTS);
  661. if (status & MII_STSICS_SPD)
  662. *speed = HW_SPEED_100_MBPS;
  663. else
  664. *speed = HW_SPEED_10_MBPS;
  665. if (status & MII_STSICS_DPLX)
  666. *duplex = FDX_CAPABLE_FULL_SELECTED;
  667. else
  668. *duplex = FDX_CAPABLE_HALF_SELECTED;
  669. if (status & MII_STSICS_LINKSTS)
  670. printf("ics1893_read_mode: Media Link On %s %s-duplex \n",
  671. *speed == HW_SPEED_100_MBPS ?
  672. "100mbps" : "10mbps",
  673. *duplex == FDX_CAPABLE_FULL_SELECTED ?
  674. "full" : "half");
  675. else
  676. printf("ics1893_read_mode: Media Link Off\n");
  677. }
  678. /**
  679. * rtl8201_read_mode: - read media mode for rtl8201 phy
  680. * @nic: the net device to read mode for
  681. * @phy_addr: mii phy address
  682. * @speed: the transmit speed to be determined
  683. * @duplex: the duplex mode to be determined
  684. *
  685. * read MII_STATUS register from rtl8201 phy
  686. * to determine the speed and duplex mode for sis900
  687. */
  688. static void rtl8201_read_mode(struct nic *nic, int phy_addr, int *speed, int *duplex)
  689. {
  690. u32 status;
  691. status = sis900_mdio_read(phy_addr, MII_STATUS);
  692. if (status & MII_STAT_CAN_TX_FDX) {
  693. *speed = HW_SPEED_100_MBPS;
  694. *duplex = FDX_CAPABLE_FULL_SELECTED;
  695. }
  696. else if (status & MII_STAT_CAN_TX) {
  697. *speed = HW_SPEED_100_MBPS;
  698. *duplex = FDX_CAPABLE_HALF_SELECTED;
  699. }
  700. else if (status & MII_STAT_CAN_T_FDX) {
  701. *speed = HW_SPEED_10_MBPS;
  702. *duplex = FDX_CAPABLE_FULL_SELECTED;
  703. }
  704. else if (status & MII_STAT_CAN_T) {
  705. *speed = HW_SPEED_10_MBPS;
  706. *duplex = FDX_CAPABLE_HALF_SELECTED;
  707. }
  708. if (status & MII_STAT_LINK)
  709. printf("rtl8201_read_mode: Media Link On %s %s-duplex \n",
  710. *speed == HW_SPEED_100_MBPS ?
  711. "100mbps" : "10mbps",
  712. *duplex == FDX_CAPABLE_FULL_SELECTED ?
  713. "full" : "half");
  714. else
  715. printf("rtl9201_read_config_mode: Media Link Off\n");
  716. }
  717. /* Function: sis900_transmit
  718. *
  719. * Description: transmits a packet and waits for completion or timeout.
  720. *
  721. * Arguments: char d[6]: destination ethernet address.
  722. * unsigned short t: ethernet protocol type.
  723. * unsigned short s: size of the data-part of the packet.
  724. * char *p: the data for the packet.
  725. *
  726. * Returns: void.
  727. */
  728. static void
  729. sis900_transmit(struct nic *nic,
  730. const char *d, /* Destination */
  731. unsigned int t, /* Type */
  732. unsigned int s, /* size */
  733. const char *p) /* Packet */
  734. {
  735. u32 status, to, nstype;
  736. volatile u32 tx_status;
  737. /* Stop the transmitter */
  738. outl(TxDIS, ioaddr + cr);
  739. /* load Transmit Descriptor Register */
  740. outl((u32) &txd, ioaddr + txdp);
  741. if (sis900_debug > 1)
  742. printf("sis900_transmit: TX descriptor register loaded with: %X\n",
  743. inl(ioaddr + txdp));
  744. memcpy(txb, d, ETH_ALEN);
  745. memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  746. nstype = htons(t);
  747. memcpy(txb + 2 * ETH_ALEN, (char*)&nstype, 2);
  748. memcpy(txb + ETH_HLEN, p, s);
  749. s += ETH_HLEN;
  750. s &= DSIZE;
  751. if (sis900_debug > 1)
  752. printf("sis900_transmit: sending %d bytes ethtype %hX\n", (int) s, t);
  753. /* pad to minimum packet size */
  754. while (s < ETH_ZLEN)
  755. txb[s++] = '\0';
  756. /* set the transmit buffer descriptor and enable Transmit State Machine */
  757. txd.bufptr = (u32) &txb[0];
  758. txd.cmdsts = (u32) OWN | s;
  759. /* restart the transmitter */
  760. outl(TxENA, ioaddr + cr);
  761. if (sis900_debug > 1)
  762. printf("sis900_transmit: Queued Tx packet size %d.\n", (int) s);
  763. to = currticks() + TX_TIMEOUT;
  764. while (((tx_status=txd.cmdsts) & OWN) && (currticks() < to))
  765. /* wait */ ;
  766. if (currticks() >= to) {
  767. printf("sis900_transmit: TX Timeout! Tx status %X.\n", tx_status);
  768. }
  769. if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
  770. /* packet unsuccessfully transmited */
  771. printf("sis900_transmit: Transmit error, Tx status %X.\n", tx_status);
  772. }
  773. /* Disable interrupts by clearing the interrupt mask. */
  774. outl(0, ioaddr + imr);
  775. }
  776. /* Function: sis900_poll
  777. *
  778. * Description: checks for a received packet and returns it if found.
  779. *
  780. * Arguments: struct nic *nic: NIC data structure
  781. *
  782. * Returns: 1 if a packet was recieved.
  783. * 0 if no pacet was recieved.
  784. *
  785. * Side effects:
  786. * Returns (copies) the packet to the array nic->packet.
  787. * Returns the length of the packet in nic->packetlen.
  788. */
  789. static int
  790. sis900_poll(struct nic *nic)
  791. {
  792. u32 rx_status = rxd[cur_rx].cmdsts;
  793. int retstat = 0;
  794. if (sis900_debug > 2)
  795. printf("sis900_poll: cur_rx:%d, status:%X\n", cur_rx, rx_status);
  796. if (!(rx_status & OWN))
  797. return retstat;
  798. if (sis900_debug > 1)
  799. printf("sis900_poll: got a packet: cur_rx:%d, status:%X\n",
  800. cur_rx, rx_status);
  801. nic->packetlen = (rx_status & DSIZE) - CRC_SIZE;
  802. if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
  803. /* corrupted packet received */
  804. printf("sis900_poll: Corrupted packet received, buffer status = %X\n",
  805. rx_status);
  806. retstat = 0;
  807. } else {
  808. /* give packet to higher level routine */
  809. memcpy(nic->packet, (rxb + cur_rx*RX_BUF_SIZE), nic->packetlen);
  810. retstat = 1;
  811. }
  812. /* return the descriptor and buffer to receive ring */
  813. rxd[cur_rx].cmdsts = RX_BUF_SIZE;
  814. rxd[cur_rx].bufptr = (u32) &rxb[cur_rx*RX_BUF_SIZE];
  815. if (++cur_rx == NUM_RX_DESC)
  816. cur_rx = 0;
  817. /* re-enable the potentially idle receive state machine */
  818. outl(RxENA , ioaddr + cr);
  819. return retstat;
  820. }
  821. /* Function: sis900_disable
  822. *
  823. * Description: Turns off interrupts and stops Tx and Rx engines
  824. *
  825. * Arguments: struct nic *nic: NIC data structure
  826. *
  827. * Returns: void.
  828. */
  829. static void
  830. sis900_disable(struct nic *nic)
  831. {
  832. /* Disable interrupts by clearing the interrupt mask. */
  833. outl(0, ioaddr + imr);
  834. outl(0, ioaddr + ier);
  835. /* Stop the chip's Tx and Rx Status Machine */
  836. outl(RxDIS | TxDIS, ioaddr + cr);
  837. }