Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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 dev *dev __unused)
  535. {
  536. /* Stop the PCNET32 here -- it ocassionally polls memory if we don't */
  537. lp->a.write_csr(ioaddr, 0, 0x0004);
  538. /*
  539. * Switch back to 16-bit mode to avoid problesm with dumb
  540. * DOS packet driver after a warm reboot
  541. */
  542. lp->a.write_bcr(ioaddr, 20, 4);
  543. }
  544. /**************************************************************************
  545. IRQ - Enable, Disable, or Force interrupts
  546. ***************************************************************************/
  547. static void pcnet32_irq(struct nic *nic __unused, irq_action_t action __unused)
  548. {
  549. switch ( action ) {
  550. case DISABLE :
  551. break;
  552. case ENABLE :
  553. break;
  554. case FORCE :
  555. break;
  556. }
  557. }
  558. /**************************************************************************
  559. PROBE - Look for an adapter, this routine's visible to the outside
  560. You should omit the last argument struct pci_device * for a non-PCI NIC
  561. ***************************************************************************/
  562. static int pcnet32_probe(struct dev *dev, struct pci_device *pci)
  563. {
  564. struct nic *nic = (struct nic *) dev;
  565. int i, media;
  566. int fdx, mii, fset, dxsuflo, ltint;
  567. int chip_version;
  568. char *chipname;
  569. struct pcnet32_access *a = NULL;
  570. u8 promaddr[6];
  571. int shared = 1;
  572. if (pci->ioaddr == 0)
  573. return 0;
  574. /* BASE is used throughout to address the card */
  575. ioaddr = pci->ioaddr;
  576. printf("pcnet32.c: Found %s, Vendor=0x%hX Device=0x%hX\n",
  577. pci->name, pci->vendor, pci->dev_id);
  578. nic->irqno = 0;
  579. nic->ioaddr = pci->ioaddr & ~3;
  580. /* reset the chip */
  581. pcnet32_wio_reset(ioaddr);
  582. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  583. if (pcnet32_wio_read_csr(ioaddr, 0) == 4
  584. && pcnet32_wio_check(ioaddr)) {
  585. a = &pcnet32_wio;
  586. } else {
  587. pcnet32_dwio_reset(ioaddr);
  588. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
  589. && pcnet32_dwio_check(ioaddr)) {
  590. a = &pcnet32_dwio;
  591. } else
  592. return 0;
  593. }
  594. chip_version =
  595. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  596. dprintf(("PCnet chip version is %0xhX\n", chip_version));
  597. if ((chip_version & 0xfff) != 0x003)
  598. return 0;
  599. /* initialize variables */
  600. fdx = mii = fset = dxsuflo = ltint = 0;
  601. chip_version = (chip_version >> 12) & 0xffff;
  602. switch (chip_version) {
  603. case 0x2420:
  604. chipname = "PCnet/PCI 79C970"; /* PCI */
  605. break;
  606. case 0x2430:
  607. if (shared)
  608. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  609. else
  610. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  611. break;
  612. case 0x2621:
  613. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  614. fdx = 1;
  615. break;
  616. case 0x2623:
  617. chipname = "PCnet/FAST 79C971"; /* PCI */
  618. fdx = 1;
  619. mii = 1;
  620. fset = 1;
  621. ltint = 1;
  622. break;
  623. case 0x2624:
  624. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  625. fdx = 1;
  626. mii = 1;
  627. fset = 1;
  628. break;
  629. case 0x2625:
  630. chipname = "PCnet/FAST III 79C973"; /* PCI */
  631. fdx = 1;
  632. mii = 1;
  633. break;
  634. case 0x2626:
  635. chipname = "PCnet/Home 79C978"; /* PCI */
  636. fdx = 1;
  637. /*
  638. * This is based on specs published at www.amd.com. This section
  639. * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
  640. * mode. The 79C978 can also go into standard ethernet, and there
  641. * probably should be some sort of module option to select the
  642. * mode by which the card should operate
  643. */
  644. /* switch to home wiring mode */
  645. media = a->read_bcr(ioaddr, 49);
  646. printf("media reset to %#x.\n", media);
  647. a->write_bcr(ioaddr, 49, media);
  648. break;
  649. case 0x2627:
  650. chipname = "PCnet/FAST III 79C975"; /* PCI */
  651. fdx = 1;
  652. mii = 1;
  653. break;
  654. default:
  655. printf("PCnet version %#x, no PCnet32 chip.\n",
  656. chip_version);
  657. return 0;
  658. }
  659. /*
  660. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  661. * starting until the packet is loaded. Strike one for reliability, lose
  662. * one for latency - although on PCI this isnt a big loss. Older chips
  663. * have FIFO's smaller than a packet, so you can't do this.
  664. */
  665. if (fset) {
  666. a->write_bcr(ioaddr, 18,
  667. (a->read_bcr(ioaddr, 18) | 0x0800));
  668. a->write_csr(ioaddr, 80,
  669. (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  670. dxsuflo = 1;
  671. ltint = 1;
  672. }
  673. dprintf(("%s at %hX,", chipname, ioaddr));
  674. /* read PROM address */
  675. for (i = 0; i < 6; i++)
  676. promaddr[i] = inb(ioaddr + i);
  677. /* Update the nic structure with the MAC Address */
  678. for (i = 0; i < ETH_ALEN; i++) {
  679. nic->node_addr[i] = promaddr[i];
  680. }
  681. /* Print out some hardware info */
  682. printf("%s: %! at ioaddr %hX, ", pci->name, nic->node_addr,
  683. ioaddr);
  684. /* Set to pci bus master */
  685. adjust_pci_device(pci);
  686. /* point to private storage */
  687. lp = &lpx;
  688. #if EBDEBUG
  689. if (((chip_version + 1) & 0xfffe) == 0x2624) { /* Version 0x2623 or 0x2624 */
  690. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  691. dprintf((" tx_start_pt(0x%hX):", i));
  692. switch (i >> 10) {
  693. case 0:
  694. dprintf((" 20 bytes,"));
  695. break;
  696. case 1:
  697. dprintf((" 64 bytes,"));
  698. break;
  699. case 2:
  700. dprintf((" 128 bytes,"));
  701. break;
  702. case 3:
  703. dprintf(("~220 bytes,"));
  704. break;
  705. }
  706. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  707. dprintf((" BCR18(%hX):", i & 0xffff));
  708. if (i & (1 << 5))
  709. dprintf(("BurstWrEn "));
  710. if (i & (1 << 6))
  711. dprintf(("BurstRdEn "));
  712. if (i & (1 << 7))
  713. dprintf(("DWordIO "));
  714. if (i & (1 << 11))
  715. dprintf(("NoUFlow "));
  716. i = a->read_bcr(ioaddr, 25);
  717. dprintf((" SRAMSIZE=0x%hX,", i << 8));
  718. i = a->read_bcr(ioaddr, 26);
  719. dprintf((" SRAM_BND=0x%hX,", i << 8));
  720. i = a->read_bcr(ioaddr, 27);
  721. if (i & (1 << 14))
  722. dprintf(("LowLatRx"));
  723. }
  724. #endif
  725. lp->name = chipname;
  726. lp->shared_irq = shared;
  727. lp->full_duplex = fdx;
  728. lp->dxsuflo = dxsuflo;
  729. lp->ltint = ltint;
  730. lp->mii = mii;
  731. /* FIXME: Fix Options for only one card */
  732. if ((cards_found >= MAX_UNITS)
  733. || ((unsigned int) options[cards_found] > sizeof(options_mapping)))
  734. lp->options = PCNET32_PORT_ASEL;
  735. else
  736. lp->options = options_mapping[options[cards_found]];
  737. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  738. ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
  739. lp->options |= PCNET32_PORT_FD;
  740. if (!a) {
  741. printf("No access methods\n");
  742. return 0;
  743. }
  744. lp->a = *a;
  745. /* detect special T1/E1 WAN card by checking for MAC address */
  746. if (nic->node_addr[0] == 0x00 && nic->node_addr[1] == 0xe0
  747. && nic->node_addr[2] == 0x75)
  748. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  749. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  750. lp->init_block.tlen_rlen =
  751. le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
  752. for (i = 0; i < 6; i++)
  753. lp->init_block.phys_addr[i] = nic->node_addr[i];
  754. lp->init_block.filter[0] = 0xffffffff;
  755. lp->init_block.filter[1] = 0xffffffff;
  756. lp->init_block.rx_ring = virt_to_bus(&rx_ring);
  757. lp->init_block.tx_ring = virt_to_bus(&tx_ring);
  758. /* switch pcnet32 to 32bit mode */
  759. a->write_bcr(ioaddr, 20, 2);
  760. a->write_csr(ioaddr, 1, (virt_to_bus(&lp->init_block)) & 0xffff);
  761. a->write_csr(ioaddr, 2, (virt_to_bus(&lp->init_block)) >> 16);
  762. /*
  763. * To auto-IRQ we enable the initialization-done and DMA error
  764. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  765. * boards will work.
  766. */
  767. /* Trigger an initialization just for the interrupt. */
  768. a->write_csr(ioaddr, 0, 0x41);
  769. mdelay(1);
  770. cards_found++;
  771. /* point to NIC specific routines */
  772. pcnet32_reset(nic);
  773. if (1) {
  774. int tmp;
  775. int phy, phy_idx = 0;
  776. u16 mii_lpa;
  777. lp->phys[0] = 1; /* Default Setting */
  778. for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
  779. int mii_status = mdio_read(nic, phy, MII_BMSR);
  780. if (mii_status != 0xffff && mii_status != 0x0000) {
  781. lp->phys[phy_idx++] = phy;
  782. lp->mii_if.advertising =
  783. mdio_read(nic, phy, MII_ADVERTISE);
  784. if ((mii_status & 0x0040) == 0) {
  785. tmp = phy;
  786. dprintf (("MII PHY found at address %d, status "
  787. "%hX advertising %hX\n", phy, mii_status,
  788. lp->mii_if.advertising));
  789. }
  790. }
  791. }
  792. if (phy_idx == 0)
  793. printf("No MII transceiver found!\n");
  794. lp->mii_if.phy_id = lp->phys[0];
  795. lp->mii_if.advertising =
  796. mdio_read(nic, lp->phys[0], MII_ADVERTISE);
  797. mii_lpa = mdio_read(nic, lp->phys[0], MII_LPA);
  798. lp->mii_if.advertising &= mii_lpa;
  799. if (lp->mii_if.advertising & ADVERTISE_100FULL)
  800. printf("100Mbps Full-Duplex\n");
  801. else if (lp->mii_if.advertising & ADVERTISE_100HALF)
  802. printf("100Mbps Half-Duplex\n");
  803. else if (lp->mii_if.advertising & ADVERTISE_10FULL)
  804. printf("10Mbps Full-Duplex\n");
  805. else if (lp->mii_if.advertising & ADVERTISE_10HALF)
  806. printf("10Mbps Half-Duplex\n");
  807. else
  808. printf("\n");
  809. }
  810. nic->poll = pcnet32_poll;
  811. nic->transmit = pcnet32_transmit;
  812. dev->disable = pcnet32_disable;
  813. nic->irq = pcnet32_irq;
  814. return 1;
  815. }
  816. static int mdio_read(struct nic *nic __unused, int phy_id, int reg_num)
  817. {
  818. u16 val_out;
  819. int phyaddr;
  820. if (!lp->mii)
  821. return 0;
  822. phyaddr = lp->a.read_bcr(ioaddr, 33);
  823. lp->a.write_bcr(ioaddr, 33,
  824. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  825. val_out = lp->a.read_bcr(ioaddr, 34);
  826. lp->a.write_bcr(ioaddr, 33, phyaddr);
  827. return val_out;
  828. }
  829. #if 0
  830. static void mdio_write(struct nic *nic __unused, int phy_id, int reg_num,
  831. int val)
  832. {
  833. int phyaddr;
  834. if (!lp->mii)
  835. return;
  836. phyaddr = lp->a.read_bcr(ioaddr, 33);
  837. lp->a.write_bcr(ioaddr, 33,
  838. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  839. lp->a.write_bcr(ioaddr, 34, val);
  840. lp->a.write_bcr(ioaddr, 33, phyaddr);
  841. }
  842. #endif
  843. static struct pci_id pcnet32_nics[] = {
  844. PCI_ROM(0x1022, 0x2000, "lancepci", "AMD Lance/PCI"),
  845. PCI_ROM(0x1022, 0x2625, "pcnetfastiii", "AMD Lance/PCI PCNet/32"),
  846. PCI_ROM(0x1022, 0x2001, "amdhomepna", "AMD Lance/HomePNA"),
  847. };
  848. static struct pci_driver pcnet32_driver =
  849. PCI_DRIVER ( "PCNET32/PCI", pcnet32_nics, PCI_NO_CLASS );
  850. BOOT_DRIVER ( "PCNET32/PCI", pcnet32_probe );