You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pcnet32.c 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /**************************************************************************
  2. *
  3. * pcnet32.c -- Etherboot device driver for the AMD PCnet32
  4. * Written 2003-2003 by Timothy Legge <tlegge@rogers.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Portions of this code based on:
  21. * pcnet32.c: An AMD PCnet32 ethernet driver for linux:
  22. *
  23. * (C) 1996-1999 Thomas Bogendoerfer
  24. * See Linux Driver for full information
  25. *
  26. * The transmit and poll functions were written with reference to:
  27. * lance.c - LANCE NIC driver for Etherboot written by Ken Yap
  28. *
  29. * Linux Driver Version 1.27a, 10.02.2002
  30. *
  31. *
  32. * REVISION HISTORY:
  33. * ================
  34. * v1.0 08-06-2003 timlegge Initial port of Linux driver
  35. * v1.1 08-23-2003 timlegge Add multicast support
  36. * v1.2 01-17-2004 timlegge Initial driver output cleanup
  37. * v1.3 03-29-2004 timlegge More driver cleanup
  38. *
  39. * Indent Options: indent -kr -i8
  40. ***************************************************************************/
  41. /* to get some global routines like printf */
  42. #include "etherboot.h"
  43. /* to get the interface to the body of the program */
  44. #include "nic.h"
  45. /* to get the PCI support functions, if this is a PCI NIC */
  46. #include "pci.h"
  47. /* Include the time functions */
  48. #include "timer.h"
  49. #include "mii.h"
  50. /* void hex_dump(const char *data, const unsigned int len); */
  51. /* Etherboot Specific definations */
  52. #define drv_version "v1.3"
  53. #define drv_date "03-29-2004"
  54. typedef unsigned char u8;
  55. typedef signed char s8;
  56. typedef unsigned short u16;
  57. typedef signed short s16;
  58. typedef unsigned int u32;
  59. typedef signed int s32;
  60. static u32 ioaddr; /* Globally used for the card's io address */
  61. #ifdef EDEBUG
  62. #define dprintf(x) printf x
  63. #else
  64. #define dprintf(x)
  65. #endif
  66. /* Condensed operations for readability. */
  67. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  68. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  69. /* End Etherboot Specific */
  70. int cards_found /* __initdata */ ;
  71. #ifdef REMOVE
  72. /* FIXME: Remove these they are probably pointless */
  73. /*
  74. * VLB I/O addresses
  75. */
  76. static unsigned int pcnet32_portlist[] /*__initdata */ =
  77. { 0x300, 0x320, 0x340, 0x360, 0 };
  78. static int pcnet32_debug = 1;
  79. static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
  80. static int pcnet32vlb; /* check for VLB cards ? */
  81. static struct net_device *pcnet32_dev;
  82. static int max_interrupt_work = 80;
  83. static int rx_copybreak = 200;
  84. #endif
  85. #define PCNET32_PORT_AUI 0x00
  86. #define PCNET32_PORT_10BT 0x01
  87. #define PCNET32_PORT_GPSI 0x02
  88. #define PCNET32_PORT_MII 0x03
  89. #define PCNET32_PORT_PORTSEL 0x03
  90. #define PCNET32_PORT_ASEL 0x04
  91. #define PCNET32_PORT_100 0x40
  92. #define PCNET32_PORT_FD 0x80
  93. #define PCNET32_DMA_MASK 0xffffffff
  94. /*
  95. * table to translate option values from tulip
  96. * to internal options
  97. */
  98. static unsigned char options_mapping[] = {
  99. PCNET32_PORT_ASEL, /* 0 Auto-select */
  100. PCNET32_PORT_AUI, /* 1 BNC/AUI */
  101. PCNET32_PORT_AUI, /* 2 AUI/BNC */
  102. PCNET32_PORT_ASEL, /* 3 not supported */
  103. PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
  104. PCNET32_PORT_ASEL, /* 5 not supported */
  105. PCNET32_PORT_ASEL, /* 6 not supported */
  106. PCNET32_PORT_ASEL, /* 7 not supported */
  107. PCNET32_PORT_ASEL, /* 8 not supported */
  108. PCNET32_PORT_MII, /* 9 MII 10baseT */
  109. PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
  110. PCNET32_PORT_MII, /* 11 MII (autosel) */
  111. PCNET32_PORT_10BT, /* 12 10BaseT */
  112. PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
  113. PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD, /* 14 MII 100BaseTx-FD */
  114. PCNET32_PORT_ASEL /* 15 not supported */
  115. };
  116. #define MAX_UNITS 8 /* More are supported, limit only on options */
  117. static int options[MAX_UNITS];
  118. static int full_duplex[MAX_UNITS];
  119. /*
  120. * Theory of Operation
  121. *
  122. * This driver uses the same software structure as the normal lance
  123. * driver. So look for a verbose description in lance.c. The differences
  124. * to the normal lance driver is the use of the 32bit mode of PCnet32
  125. * and PCnetPCI chips. Because these chips are 32bit chips, there is no
  126. * 16MB limitation and we don't need bounce buffers.
  127. */
  128. /*
  129. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  130. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  131. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  132. */
  133. #ifndef PCNET32_LOG_TX_BUFFERS
  134. #define PCNET32_LOG_TX_BUFFERS 1
  135. #define PCNET32_LOG_RX_BUFFERS 2
  136. #endif
  137. #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
  138. #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
  139. /* FIXME: Fix this to allow multiple tx_ring descriptors */
  140. #define TX_RING_LEN_BITS 0x0000 /*PCNET32_LOG_TX_BUFFERS) << 12) */
  141. #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
  142. #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
  143. #define RX_RING_LEN_BITS ((PCNET32_LOG_RX_BUFFERS) << 4)
  144. #define PKT_BUF_SZ 1544
  145. /* Offsets from base I/O address. */
  146. #define PCNET32_WIO_RDP 0x10
  147. #define PCNET32_WIO_RAP 0x12
  148. #define PCNET32_WIO_RESET 0x14
  149. #define PCNET32_WIO_BDP 0x16
  150. #define PCNET32_DWIO_RDP 0x10
  151. #define PCNET32_DWIO_RAP 0x14
  152. #define PCNET32_DWIO_RESET 0x18
  153. #define PCNET32_DWIO_BDP 0x1C
  154. #define PCNET32_TOTAL_SIZE 0x20
  155. /* Buffers for the tx and Rx */
  156. /* Create a static buffer of size PKT_BUF_SZ for each
  157. TX Descriptor. All descriptors point to a
  158. part of this buffer */
  159. static unsigned char txb[PKT_BUF_SZ * TX_RING_SIZE];
  160. // __attribute__ ((aligned(16)));
  161. /* Create a static buffer of size PKT_BUF_SZ for each
  162. RX Descriptor All descriptors point to a
  163. part of this buffer */
  164. static unsigned char rxb[RX_RING_SIZE * PKT_BUF_SZ];
  165. // __attribute__ ((aligned(16)));
  166. /* The PCNET32 Rx and Tx ring descriptors. */
  167. struct pcnet32_rx_head {
  168. u32 base;
  169. s16 buf_length;
  170. s16 status;
  171. u32 msg_length;
  172. u32 reserved;
  173. };
  174. struct pcnet32_tx_head {
  175. u32 base;
  176. s16 length;
  177. s16 status;
  178. u32 misc;
  179. u32 reserved;
  180. };
  181. /* The PCNET32 32-Bit initialization block, described in databook. */
  182. struct pcnet32_init_block {
  183. u16 mode;
  184. u16 tlen_rlen;
  185. u8 phys_addr[6];
  186. u16 reserved;
  187. u32 filter[2];
  188. /* Receive and transmit ring base, along with extra bits. */
  189. u32 rx_ring;
  190. u32 tx_ring;
  191. };
  192. /* PCnet32 access functions */
  193. struct pcnet32_access {
  194. u16(*read_csr) (unsigned long, int);
  195. void (*write_csr) (unsigned long, int, u16);
  196. u16(*read_bcr) (unsigned long, int);
  197. void (*write_bcr) (unsigned long, int, u16);
  198. u16(*read_rap) (unsigned long);
  199. void (*write_rap) (unsigned long, u16);
  200. void (*reset) (unsigned long);
  201. };
  202. /* Define the TX Descriptor */
  203. static struct pcnet32_tx_head tx_ring[TX_RING_SIZE]
  204. __attribute__ ((aligned(16)));
  205. /* Define the RX Descriptor */
  206. static struct pcnet32_rx_head rx_ring[RX_RING_SIZE]
  207. __attribute__ ((aligned(16)));
  208. /* May need to be moved to mii.h */
  209. struct mii_if_info {
  210. int phy_id;
  211. int advertising;
  212. unsigned int full_duplex:1; /* is full duplex? */
  213. };
  214. /*
  215. * The first three fields of pcnet32_private are read by the ethernet device
  216. * so we allocate the structure should be allocated by pci_alloc_consistent().
  217. */
  218. #define MII_CNT 4
  219. struct pcnet32_private {
  220. struct pcnet32_init_block init_block;
  221. struct pci_dev *pci_dev; /* Pointer to the associated pci device structure */
  222. const char *name;
  223. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  224. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  225. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  226. struct pcnet32_access a;
  227. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  228. char tx_full;
  229. int options;
  230. int shared_irq:1, /* shared irq possible */
  231. ltint:1, /* enable TxDone-intr inhibitor */
  232. dxsuflo:1, /* disable transmit stop on uflo */
  233. mii:1; /* mii port available */
  234. struct mii_if_info mii_if;
  235. unsigned char phys[MII_CNT];
  236. struct net_device *next;
  237. int full_duplex:1;
  238. } lpx;
  239. static struct pcnet32_private *lp;
  240. static int mdio_read(struct nic *nic __unused, int phy_id, int reg_num);
  241. #if 0
  242. static void mdio_write(struct nic *nic __unused, int phy_id, int reg_num,
  243. int val);
  244. #endif
  245. enum pci_flags_bit {
  246. PCI_USES_IO = 1, PCI_USES_MEM = 2, PCI_USES_MASTER = 4,
  247. PCI_ADDR0 = 0x10 << 0, PCI_ADDR1 = 0x10 << 1, PCI_ADDR2 =
  248. 0x10 << 2, PCI_ADDR3 = 0x10 << 3,
  249. };
  250. static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
  251. {
  252. outw(index, addr + PCNET32_WIO_RAP);
  253. return inw(addr + PCNET32_WIO_RDP);
  254. }
  255. static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
  256. {
  257. outw(index, addr + PCNET32_WIO_RAP);
  258. outw(val, addr + PCNET32_WIO_RDP);
  259. }
  260. static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
  261. {
  262. outw(index, addr + PCNET32_WIO_RAP);
  263. return inw(addr + PCNET32_WIO_BDP);
  264. }
  265. static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
  266. {
  267. outw(index, addr + PCNET32_WIO_RAP);
  268. outw(val, addr + PCNET32_WIO_BDP);
  269. }
  270. static u16 pcnet32_wio_read_rap(unsigned long addr)
  271. {
  272. return inw(addr + PCNET32_WIO_RAP);
  273. }
  274. static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
  275. {
  276. outw(val, addr + PCNET32_WIO_RAP);
  277. }
  278. static void pcnet32_wio_reset(unsigned long addr)
  279. {
  280. inw(addr + PCNET32_WIO_RESET);
  281. }
  282. static int pcnet32_wio_check(unsigned long addr)
  283. {
  284. outw(88, addr + PCNET32_WIO_RAP);
  285. return (inw(addr + PCNET32_WIO_RAP) == 88);
  286. }
  287. static struct pcnet32_access pcnet32_wio = {
  288. read_csr:pcnet32_wio_read_csr,
  289. write_csr:pcnet32_wio_write_csr,
  290. read_bcr:pcnet32_wio_read_bcr,
  291. write_bcr:pcnet32_wio_write_bcr,
  292. read_rap:pcnet32_wio_read_rap,
  293. write_rap:pcnet32_wio_write_rap,
  294. reset:pcnet32_wio_reset
  295. };
  296. static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
  297. {
  298. outl(index, addr + PCNET32_DWIO_RAP);
  299. return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
  300. }
  301. static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
  302. {
  303. outl(index, addr + PCNET32_DWIO_RAP);
  304. outl(val, addr + PCNET32_DWIO_RDP);
  305. }
  306. static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
  307. {
  308. outl(index, addr + PCNET32_DWIO_RAP);
  309. return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
  310. }
  311. static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
  312. {
  313. outl(index, addr + PCNET32_DWIO_RAP);
  314. outl(val, addr + PCNET32_DWIO_BDP);
  315. }
  316. static u16 pcnet32_dwio_read_rap(unsigned long addr)
  317. {
  318. return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
  319. }
  320. static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
  321. {
  322. outl(val, addr + PCNET32_DWIO_RAP);
  323. }
  324. static void pcnet32_dwio_reset(unsigned long addr)
  325. {
  326. inl(addr + PCNET32_DWIO_RESET);
  327. }
  328. static int pcnet32_dwio_check(unsigned long addr)
  329. {
  330. outl(88, addr + PCNET32_DWIO_RAP);
  331. return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
  332. }
  333. static struct pcnet32_access pcnet32_dwio = {
  334. read_csr:pcnet32_dwio_read_csr,
  335. write_csr:pcnet32_dwio_write_csr,
  336. read_bcr:pcnet32_dwio_read_bcr,
  337. write_bcr:pcnet32_dwio_write_bcr,
  338. read_rap:pcnet32_dwio_read_rap,
  339. write_rap:pcnet32_dwio_write_rap,
  340. reset:pcnet32_dwio_reset
  341. };
  342. /* Initialize the PCNET32 Rx and Tx rings. */
  343. static int pcnet32_init_ring(struct nic *nic)
  344. {
  345. int i;
  346. lp->tx_full = 0;
  347. lp->cur_rx = lp->cur_tx = 0;
  348. for (i = 0; i < RX_RING_SIZE; i++) {
  349. rx_ring[i].base = (u32) virt_to_le32desc(&rxb[i]);
  350. rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
  351. rx_ring[i].status = le16_to_cpu(0x8000);
  352. }
  353. /* The Tx buffer address is filled in as needed, but we do need to clear
  354. the upper ownership bit. */
  355. for (i = 0; i < TX_RING_SIZE; i++) {
  356. tx_ring[i].base = 0;
  357. tx_ring[i].status = 0;
  358. }
  359. lp->init_block.tlen_rlen =
  360. le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
  361. for (i = 0; i < 6; i++)
  362. lp->init_block.phys_addr[i] = nic->node_addr[i];
  363. lp->init_block.rx_ring = (u32) virt_to_le32desc(&rx_ring[0]);
  364. lp->init_block.tx_ring = (u32) virt_to_le32desc(&tx_ring[0]);
  365. return 0;
  366. }
  367. /**************************************************************************
  368. RESET - Reset adapter
  369. ***************************************************************************/
  370. static void pcnet32_reset(struct nic *nic)
  371. {
  372. /* put the card in its initial state */
  373. u16 val;
  374. int i;
  375. /* Reset the PCNET32 */
  376. lp->a.reset(ioaddr);
  377. /* switch pcnet32 to 32bit mode */
  378. lp->a.write_bcr(ioaddr, 20, 2);
  379. /* set/reset autoselect bit */
  380. val = lp->a.read_bcr(ioaddr, 2) & ~2;
  381. if (lp->options & PCNET32_PORT_ASEL)
  382. val |= 2;
  383. lp->a.write_bcr(ioaddr, 2, val);
  384. /* handle full duplex setting */
  385. if (lp->full_duplex) {
  386. val = lp->a.read_bcr(ioaddr, 9) & ~3;
  387. if (lp->options & PCNET32_PORT_FD) {
  388. val |= 1;
  389. if (lp->options ==
  390. (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  391. val |= 2;
  392. } else if (lp->options & PCNET32_PORT_ASEL) {
  393. /* workaround of xSeries250, turn on for 79C975 only */
  394. i = ((lp->a.
  395. read_csr(ioaddr,
  396. 88) | (lp->a.read_csr(ioaddr,
  397. 89) << 16)) >>
  398. 12) & 0xffff;
  399. if (i == 0x2627)
  400. val |= 3;
  401. }
  402. lp->a.write_bcr(ioaddr, 9, val);
  403. }
  404. /* set/reset GPSI bit in test register */
  405. val = lp->a.read_csr(ioaddr, 124) & ~0x10;
  406. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  407. val |= 0x10;
  408. lp->a.write_csr(ioaddr, 124, val);
  409. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  410. val = lp->a.read_bcr(ioaddr, 32) & ~0x38; /* disable Auto Negotiation, set 10Mpbs, HD */
  411. if (lp->options & PCNET32_PORT_FD)
  412. val |= 0x10;
  413. if (lp->options & PCNET32_PORT_100)
  414. val |= 0x08;
  415. lp->a.write_bcr(ioaddr, 32, val);
  416. } else {
  417. if (lp->options & PCNET32_PORT_ASEL) { /* enable auto negotiate, setup, disable fd */
  418. val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
  419. val |= 0x20;
  420. lp->a.write_bcr(ioaddr, 32, val);
  421. }
  422. }
  423. #ifdef DO_DXSUFLO
  424. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  425. val = lp->a.read_csr(ioaddr, 3);
  426. val |= 0x40;
  427. lp->a.write_csr(ioaddr, 3, val);
  428. }
  429. #endif
  430. if (lp->ltint) { /* Enable TxDone-intr inhibitor */
  431. val = lp->a.read_csr(ioaddr, 5);
  432. val |= (1 << 14);
  433. lp->a.write_csr(ioaddr, 5, val);
  434. }
  435. lp->init_block.mode =
  436. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  437. lp->init_block.filter[0] = 0xffffffff;
  438. lp->init_block.filter[1] = 0xffffffff;
  439. pcnet32_init_ring(nic);
  440. /* Re-initialize the PCNET32, and start it when done. */
  441. lp->a.write_csr(ioaddr, 1,
  442. (virt_to_bus(&lp->init_block)) & 0xffff);
  443. lp->a.write_csr(ioaddr, 2, (virt_to_bus(&lp->init_block)) >> 16);
  444. lp->a.write_csr(ioaddr, 4, 0x0915);
  445. lp->a.write_csr(ioaddr, 0, 0x0001);
  446. i = 0;
  447. while (i++ < 100)
  448. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  449. break;
  450. /*
  451. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  452. * reports that doing so triggers a bug in the '974.
  453. */
  454. lp->a.write_csr(ioaddr, 0, 0x0042);
  455. dprintf(("pcnet32 open, csr0 %hX.\n", lp->a.read_csr(ioaddr, 0)));
  456. }
  457. /**************************************************************************
  458. POLL - Wait for a frame
  459. ***************************************************************************/
  460. static int pcnet32_poll(struct nic *nic __unused, int retrieve)
  461. {
  462. /* return true if there's an ethernet packet ready to read */
  463. /* nic->packet should contain data on return */
  464. /* nic->packetlen should contain length of data */
  465. int status;
  466. int entry;
  467. entry = lp->cur_rx & RX_RING_MOD_MASK;
  468. status = ((short) le16_to_cpu(rx_ring[entry].status) >> 8);
  469. if (status < 0)
  470. return 0;
  471. if ( ! retrieve ) return 1;
  472. if (status == 0x03) {
  473. nic->packetlen =
  474. (le32_to_cpu(rx_ring[entry].msg_length) & 0xfff) - 4;
  475. memcpy(nic->packet, &rxb[entry], nic->packetlen);
  476. /* Andrew Boyd of QNX reports that some revs of the 79C765
  477. * clear the buffer length */
  478. rx_ring[entry].buf_length = le16_to_cpu(-PKT_BUF_SZ);
  479. rx_ring[entry].status |= le16_to_cpu(0x8000); /* prime for next receive */
  480. /* Switch to the next Rx ring buffer */
  481. lp->cur_rx++;
  482. } else {
  483. return 0;
  484. }
  485. return 1;
  486. }
  487. /**************************************************************************
  488. TRANSMIT - Transmit a frame
  489. ***************************************************************************/
  490. static void pcnet32_transmit(struct nic *nic __unused, const char *d, /* Destination */
  491. unsigned int t, /* Type */
  492. unsigned int s, /* size */
  493. const char *p)
  494. { /* Packet */
  495. /* send the packet to destination */
  496. unsigned long time;
  497. u8 *ptxb;
  498. u16 nstype;
  499. u16 status;
  500. int entry = 0; /*lp->cur_tx & TX_RING_MOD_MASK; */
  501. status = 0x8300;
  502. /* point to the current txb incase multiple tx_rings are used */
  503. ptxb = txb + (lp->cur_tx * PKT_BUF_SZ);
  504. /* copy the packet to ring buffer */
  505. memcpy(ptxb, d, ETH_ALEN); /* dst */
  506. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN); /* src */
  507. nstype = htons((u16) t); /* type */
  508. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2); /* type */
  509. memcpy(ptxb + ETH_HLEN, p, s);
  510. s += ETH_HLEN;
  511. while (s < ETH_ZLEN) /* pad to min length */
  512. ptxb[s++] = '\0';
  513. tx_ring[entry].length = le16_to_cpu(-s);
  514. tx_ring[entry].misc = 0x00000000;
  515. tx_ring[entry].base = (u32) virt_to_le32desc(ptxb);
  516. /* we set the top byte as the very last thing */
  517. tx_ring[entry].status = le16_to_cpu(status);
  518. /* Trigger an immediate send poll */
  519. lp->a.write_csr(ioaddr, 0, 0x0048);
  520. /* wait for transmit complete */
  521. lp->cur_tx = 0; /* (lp->cur_tx + 1); */
  522. time = currticks() + TICKS_PER_SEC; /* wait one second */
  523. while (currticks() < time &&
  524. ((short) le16_to_cpu(tx_ring[entry].status) < 0));
  525. if ((short) le16_to_cpu(tx_ring[entry].status) < 0)
  526. printf("PCNET32 timed out on transmit\n");
  527. /* Stop pointing at the current txb
  528. * otherwise the card continues to send the packet */
  529. tx_ring[entry].base = 0;
  530. }
  531. /**************************************************************************
  532. DISABLE - Turn off ethernet interface
  533. ***************************************************************************/
  534. static void pcnet32_disable ( struct nic *nic __unused ) {
  535. /* Stop the PCNET32 here -- it ocassionally polls memory if we don't */
  536. lp->a.write_csr(ioaddr, 0, 0x0004);
  537. /*
  538. * Switch back to 16-bit mode to avoid problesm with dumb
  539. * DOS packet driver after a warm reboot
  540. */
  541. lp->a.write_bcr(ioaddr, 20, 4);
  542. }
  543. /**************************************************************************
  544. IRQ - Enable, Disable, or Force interrupts
  545. ***************************************************************************/
  546. static void pcnet32_irq(struct nic *nic __unused, irq_action_t action __unused)
  547. {
  548. switch ( action ) {
  549. case DISABLE :
  550. break;
  551. case ENABLE :
  552. break;
  553. case FORCE :
  554. break;
  555. }
  556. }
  557. /**************************************************************************
  558. PROBE - Look for an adapter, this routine's visible to the outside
  559. You should omit the last argument struct pci_device * for a non-PCI NIC
  560. ***************************************************************************/
  561. static int pcnet32_probe ( struct dev *dev ) {
  562. struct nic *nic = nic_device ( dev );
  563. struct pci_device *pci = pci_device ( dev );
  564. int i, media;
  565. int fdx, mii, fset, dxsuflo, ltint;
  566. int chip_version;
  567. char *chipname;
  568. struct pcnet32_access *a = NULL;
  569. u8 promaddr[6];
  570. int shared = 1;
  571. if (pci->ioaddr == 0)
  572. return 0;
  573. /* BASE is used throughout to address the card */
  574. ioaddr = pci->ioaddr;
  575. printf("pcnet32.c: Found %s, Vendor=0x%hX Device=0x%hX\n",
  576. pci->name, pci->vendor, pci->dev_id);
  577. nic->irqno = 0;
  578. nic->ioaddr = pci->ioaddr & ~3;
  579. /* reset the chip */
  580. pcnet32_wio_reset(ioaddr);
  581. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  582. if (pcnet32_wio_read_csr(ioaddr, 0) == 4
  583. && pcnet32_wio_check(ioaddr)) {
  584. a = &pcnet32_wio;
  585. } else {
  586. pcnet32_dwio_reset(ioaddr);
  587. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
  588. && pcnet32_dwio_check(ioaddr)) {
  589. a = &pcnet32_dwio;
  590. } else
  591. return 0;
  592. }
  593. chip_version =
  594. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  595. dprintf(("PCnet chip version is %0xhX\n", chip_version));
  596. if ((chip_version & 0xfff) != 0x003)
  597. return 0;
  598. /* initialize variables */
  599. fdx = mii = fset = dxsuflo = ltint = 0;
  600. chip_version = (chip_version >> 12) & 0xffff;
  601. switch (chip_version) {
  602. case 0x2420:
  603. chipname = "PCnet/PCI 79C970"; /* PCI */
  604. break;
  605. case 0x2430:
  606. if (shared)
  607. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  608. else
  609. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  610. break;
  611. case 0x2621:
  612. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  613. fdx = 1;
  614. break;
  615. case 0x2623:
  616. chipname = "PCnet/FAST 79C971"; /* PCI */
  617. fdx = 1;
  618. mii = 1;
  619. fset = 1;
  620. ltint = 1;
  621. break;
  622. case 0x2624:
  623. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  624. fdx = 1;
  625. mii = 1;
  626. fset = 1;
  627. break;
  628. case 0x2625:
  629. chipname = "PCnet/FAST III 79C973"; /* PCI */
  630. fdx = 1;
  631. mii = 1;
  632. break;
  633. case 0x2626:
  634. chipname = "PCnet/Home 79C978"; /* PCI */
  635. fdx = 1;
  636. /*
  637. * This is based on specs published at www.amd.com. This section
  638. * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
  639. * mode. The 79C978 can also go into standard ethernet, and there
  640. * probably should be some sort of module option to select the
  641. * mode by which the card should operate
  642. */
  643. /* switch to home wiring mode */
  644. media = a->read_bcr(ioaddr, 49);
  645. printf("media reset to %#x.\n", media);
  646. a->write_bcr(ioaddr, 49, media);
  647. break;
  648. case 0x2627:
  649. chipname = "PCnet/FAST III 79C975"; /* PCI */
  650. fdx = 1;
  651. mii = 1;
  652. break;
  653. default:
  654. printf("PCnet version %#x, no PCnet32 chip.\n",
  655. chip_version);
  656. return 0;
  657. }
  658. /*
  659. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  660. * starting until the packet is loaded. Strike one for reliability, lose
  661. * one for latency - although on PCI this isnt a big loss. Older chips
  662. * have FIFO's smaller than a packet, so you can't do this.
  663. */
  664. if (fset) {
  665. a->write_bcr(ioaddr, 18,
  666. (a->read_bcr(ioaddr, 18) | 0x0800));
  667. a->write_csr(ioaddr, 80,
  668. (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  669. dxsuflo = 1;
  670. ltint = 1;
  671. }
  672. dprintf(("%s at %hX,", chipname, ioaddr));
  673. /* read PROM address */
  674. for (i = 0; i < 6; i++)
  675. promaddr[i] = inb(ioaddr + i);
  676. /* Update the nic structure with the MAC Address */
  677. for (i = 0; i < ETH_ALEN; i++) {
  678. nic->node_addr[i] = promaddr[i];
  679. }
  680. /* Print out some hardware info */
  681. printf("%s: %! at ioaddr %hX, ", pci->name, nic->node_addr,
  682. ioaddr);
  683. /* Set to pci bus master */
  684. adjust_pci_device(pci);
  685. /* point to private storage */
  686. lp = &lpx;
  687. #if EBDEBUG
  688. if (((chip_version + 1) & 0xfffe) == 0x2624) { /* Version 0x2623 or 0x2624 */
  689. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  690. dprintf((" tx_start_pt(0x%hX):", i));
  691. switch (i >> 10) {
  692. case 0:
  693. dprintf((" 20 bytes,"));
  694. break;
  695. case 1:
  696. dprintf((" 64 bytes,"));
  697. break;
  698. case 2:
  699. dprintf((" 128 bytes,"));
  700. break;
  701. case 3:
  702. dprintf(("~220 bytes,"));
  703. break;
  704. }
  705. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  706. dprintf((" BCR18(%hX):", i & 0xffff));
  707. if (i & (1 << 5))
  708. dprintf(("BurstWrEn "));
  709. if (i & (1 << 6))
  710. dprintf(("BurstRdEn "));
  711. if (i & (1 << 7))
  712. dprintf(("DWordIO "));
  713. if (i & (1 << 11))
  714. dprintf(("NoUFlow "));
  715. i = a->read_bcr(ioaddr, 25);
  716. dprintf((" SRAMSIZE=0x%hX,", i << 8));
  717. i = a->read_bcr(ioaddr, 26);
  718. dprintf((" SRAM_BND=0x%hX,", i << 8));
  719. i = a->read_bcr(ioaddr, 27);
  720. if (i & (1 << 14))
  721. dprintf(("LowLatRx"));
  722. }
  723. #endif
  724. lp->name = chipname;
  725. lp->shared_irq = shared;
  726. lp->full_duplex = fdx;
  727. lp->dxsuflo = dxsuflo;
  728. lp->ltint = ltint;
  729. lp->mii = mii;
  730. /* FIXME: Fix Options for only one card */
  731. if ((cards_found >= MAX_UNITS)
  732. || ((unsigned int) options[cards_found] > sizeof(options_mapping)))
  733. lp->options = PCNET32_PORT_ASEL;
  734. else
  735. lp->options = options_mapping[options[cards_found]];
  736. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  737. ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
  738. lp->options |= PCNET32_PORT_FD;
  739. if (!a) {
  740. printf("No access methods\n");
  741. return 0;
  742. }
  743. lp->a = *a;
  744. /* detect special T1/E1 WAN card by checking for MAC address */
  745. if (nic->node_addr[0] == 0x00 && nic->node_addr[1] == 0xe0
  746. && nic->node_addr[2] == 0x75)
  747. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  748. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  749. lp->init_block.tlen_rlen =
  750. le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
  751. for (i = 0; i < 6; i++)
  752. lp->init_block.phys_addr[i] = nic->node_addr[i];
  753. lp->init_block.filter[0] = 0xffffffff;
  754. lp->init_block.filter[1] = 0xffffffff;
  755. lp->init_block.rx_ring = virt_to_bus(&rx_ring);
  756. lp->init_block.tx_ring = virt_to_bus(&tx_ring);
  757. /* switch pcnet32 to 32bit mode */
  758. a->write_bcr(ioaddr, 20, 2);
  759. a->write_csr(ioaddr, 1, (virt_to_bus(&lp->init_block)) & 0xffff);
  760. a->write_csr(ioaddr, 2, (virt_to_bus(&lp->init_block)) >> 16);
  761. /*
  762. * To auto-IRQ we enable the initialization-done and DMA error
  763. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  764. * boards will work.
  765. */
  766. /* Trigger an initialization just for the interrupt. */
  767. a->write_csr(ioaddr, 0, 0x41);
  768. mdelay(1);
  769. cards_found++;
  770. /* point to NIC specific routines */
  771. pcnet32_reset(nic);
  772. if (1) {
  773. int tmp;
  774. int phy, phy_idx = 0;
  775. u16 mii_lpa;
  776. lp->phys[0] = 1; /* Default Setting */
  777. for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
  778. int mii_status = mdio_read(nic, phy, MII_BMSR);
  779. if (mii_status != 0xffff && mii_status != 0x0000) {
  780. lp->phys[phy_idx++] = phy;
  781. lp->mii_if.advertising =
  782. mdio_read(nic, phy, MII_ADVERTISE);
  783. if ((mii_status & 0x0040) == 0) {
  784. tmp = phy;
  785. dprintf (("MII PHY found at address %d, status "
  786. "%hX advertising %hX\n", phy, mii_status,
  787. lp->mii_if.advertising));
  788. }
  789. }
  790. }
  791. if (phy_idx == 0)
  792. printf("No MII transceiver found!\n");
  793. lp->mii_if.phy_id = lp->phys[0];
  794. lp->mii_if.advertising =
  795. mdio_read(nic, lp->phys[0], MII_ADVERTISE);
  796. mii_lpa = mdio_read(nic, lp->phys[0], MII_LPA);
  797. lp->mii_if.advertising &= mii_lpa;
  798. if (lp->mii_if.advertising & ADVERTISE_100FULL)
  799. printf("100Mbps Full-Duplex\n");
  800. else if (lp->mii_if.advertising & ADVERTISE_100HALF)
  801. printf("100Mbps Half-Duplex\n");
  802. else if (lp->mii_if.advertising & ADVERTISE_10FULL)
  803. printf("10Mbps Full-Duplex\n");
  804. else if (lp->mii_if.advertising & ADVERTISE_10HALF)
  805. printf("10Mbps Half-Duplex\n");
  806. else
  807. printf("\n");
  808. }
  809. static struct nic_operations pcnet32_operations;
  810. static struct nic_operations pcnet32_operations = {
  811. .connect = dummy_connect,
  812. .poll = pcnet32_poll,
  813. .transmit = pcnet32_transmit,
  814. .irq = pcnet32_irq,
  815. .disable = pcnet32_disable,
  816. };
  817. nic->nic_op = &pcnet32_operations;
  818. return 1;
  819. }
  820. static int mdio_read(struct nic *nic __unused, int phy_id, int reg_num)
  821. {
  822. u16 val_out;
  823. int phyaddr;
  824. if (!lp->mii)
  825. return 0;
  826. phyaddr = lp->a.read_bcr(ioaddr, 33);
  827. lp->a.write_bcr(ioaddr, 33,
  828. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  829. val_out = lp->a.read_bcr(ioaddr, 34);
  830. lp->a.write_bcr(ioaddr, 33, phyaddr);
  831. return val_out;
  832. }
  833. #if 0
  834. static void mdio_write(struct nic *nic __unused, int phy_id, int reg_num,
  835. int val)
  836. {
  837. int phyaddr;
  838. if (!lp->mii)
  839. return;
  840. phyaddr = lp->a.read_bcr(ioaddr, 33);
  841. lp->a.write_bcr(ioaddr, 33,
  842. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  843. lp->a.write_bcr(ioaddr, 34, val);
  844. lp->a.write_bcr(ioaddr, 33, phyaddr);
  845. }
  846. #endif
  847. static struct pci_id pcnet32_nics[] = {
  848. PCI_ROM(0x1022, 0x2000, "lancepci", "AMD Lance/PCI"),
  849. PCI_ROM(0x1022, 0x2625, "pcnetfastiii", "AMD Lance/PCI PCNet/32"),
  850. PCI_ROM(0x1022, 0x2001, "amdhomepna", "AMD Lance/HomePNA"),
  851. };
  852. static struct pci_driver pcnet32_driver =
  853. PCI_DRIVER ( "PCNET32/PCI", pcnet32_nics, PCI_NO_CLASS );
  854. BOOT_DRIVER ( "PCNET32/PCI", pcnet32_probe );