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

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