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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. #include "etherboot.h"
  42. #include "nic.h"
  43. #include <gpxe/pci.h>
  44. #include <gpxe/ethernet.h>
  45. #include "timer.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[PKT_BUF_SZ * TX_RING_SIZE];
  190. unsigned char rxb[RX_RING_SIZE * PKT_BUF_SZ];
  191. } pcnet32_bufs __shared;
  192. /* May need to be moved to mii.h */
  193. struct mii_if_info {
  194. int phy_id;
  195. int advertising;
  196. unsigned int full_duplex:1; /* is full duplex? */
  197. };
  198. /*
  199. * The first three fields of pcnet32_private are read by the ethernet device
  200. * so we allocate the structure should be allocated by pci_alloc_consistent().
  201. */
  202. #define MII_CNT 4
  203. struct pcnet32_private {
  204. struct pcnet32_init_block init_block;
  205. struct pci_dev *pci_dev; /* Pointer to the associated pci device structure */
  206. const char *name;
  207. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  208. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  209. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  210. struct pcnet32_access a;
  211. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  212. char tx_full;
  213. int options;
  214. int shared_irq:1, /* shared irq possible */
  215. ltint:1, /* enable TxDone-intr inhibitor */
  216. dxsuflo:1, /* disable transmit stop on uflo */
  217. mii:1; /* mii port available */
  218. struct mii_if_info mii_if;
  219. unsigned char phys[MII_CNT];
  220. struct net_device *next;
  221. int full_duplex:1;
  222. } lpx;
  223. static struct pcnet32_private *lp;
  224. static int mdio_read(struct nic *nic __unused, int phy_id, int reg_num);
  225. #if 0
  226. static void mdio_write(struct nic *nic __unused, int phy_id, int reg_num,
  227. int val);
  228. #endif
  229. enum pci_flags_bit {
  230. PCI_USES_IO = 1, PCI_USES_MEM = 2, PCI_USES_MASTER = 4,
  231. PCI_ADDR0 = 0x10 << 0, PCI_ADDR1 = 0x10 << 1, PCI_ADDR2 =
  232. 0x10 << 2, PCI_ADDR3 = 0x10 << 3,
  233. };
  234. static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
  235. {
  236. outw(index, addr + PCNET32_WIO_RAP);
  237. return inw(addr + PCNET32_WIO_RDP);
  238. }
  239. static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
  240. {
  241. outw(index, addr + PCNET32_WIO_RAP);
  242. outw(val, addr + PCNET32_WIO_RDP);
  243. }
  244. static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
  245. {
  246. outw(index, addr + PCNET32_WIO_RAP);
  247. return inw(addr + PCNET32_WIO_BDP);
  248. }
  249. static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
  250. {
  251. outw(index, addr + PCNET32_WIO_RAP);
  252. outw(val, addr + PCNET32_WIO_BDP);
  253. }
  254. static u16 pcnet32_wio_read_rap(unsigned long addr)
  255. {
  256. return inw(addr + PCNET32_WIO_RAP);
  257. }
  258. static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
  259. {
  260. outw(val, addr + PCNET32_WIO_RAP);
  261. }
  262. static void pcnet32_wio_reset(unsigned long addr)
  263. {
  264. inw(addr + PCNET32_WIO_RESET);
  265. }
  266. static int pcnet32_wio_check(unsigned long addr)
  267. {
  268. outw(88, addr + PCNET32_WIO_RAP);
  269. return (inw(addr + PCNET32_WIO_RAP) == 88);
  270. }
  271. static struct pcnet32_access pcnet32_wio = {
  272. read_csr:pcnet32_wio_read_csr,
  273. write_csr:pcnet32_wio_write_csr,
  274. read_bcr:pcnet32_wio_read_bcr,
  275. write_bcr:pcnet32_wio_write_bcr,
  276. read_rap:pcnet32_wio_read_rap,
  277. write_rap:pcnet32_wio_write_rap,
  278. reset:pcnet32_wio_reset
  279. };
  280. static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
  281. {
  282. outl(index, addr + PCNET32_DWIO_RAP);
  283. return (inl(addr + PCNET32_DWIO_RDP) & 0xffff);
  284. }
  285. static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
  286. {
  287. outl(index, addr + PCNET32_DWIO_RAP);
  288. outl(val, addr + PCNET32_DWIO_RDP);
  289. }
  290. static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
  291. {
  292. outl(index, addr + PCNET32_DWIO_RAP);
  293. return (inl(addr + PCNET32_DWIO_BDP) & 0xffff);
  294. }
  295. static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
  296. {
  297. outl(index, addr + PCNET32_DWIO_RAP);
  298. outl(val, addr + PCNET32_DWIO_BDP);
  299. }
  300. static u16 pcnet32_dwio_read_rap(unsigned long addr)
  301. {
  302. return (inl(addr + PCNET32_DWIO_RAP) & 0xffff);
  303. }
  304. static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
  305. {
  306. outl(val, addr + PCNET32_DWIO_RAP);
  307. }
  308. static void pcnet32_dwio_reset(unsigned long addr)
  309. {
  310. inl(addr + PCNET32_DWIO_RESET);
  311. }
  312. static int pcnet32_dwio_check(unsigned long addr)
  313. {
  314. outl(88, addr + PCNET32_DWIO_RAP);
  315. return ((inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88);
  316. }
  317. static struct pcnet32_access pcnet32_dwio = {
  318. read_csr:pcnet32_dwio_read_csr,
  319. write_csr:pcnet32_dwio_write_csr,
  320. read_bcr:pcnet32_dwio_read_bcr,
  321. write_bcr:pcnet32_dwio_write_bcr,
  322. read_rap:pcnet32_dwio_read_rap,
  323. write_rap:pcnet32_dwio_write_rap,
  324. reset:pcnet32_dwio_reset
  325. };
  326. /* Initialize the PCNET32 Rx and Tx rings. */
  327. static int pcnet32_init_ring(struct nic *nic)
  328. {
  329. int i;
  330. lp->tx_full = 0;
  331. lp->cur_rx = lp->cur_tx = 0;
  332. for (i = 0; i < RX_RING_SIZE; i++) {
  333. pcnet32_bufs.rx_ring[i].base =
  334. virt_to_le32desc(&pcnet32_bufs.rxb[i]);
  335. pcnet32_bufs.rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
  336. pcnet32_bufs.rx_ring[i].status = le16_to_cpu(0x8000);
  337. }
  338. /* The Tx buffer address is filled in as needed, but we do need to clear
  339. the upper ownership bit. */
  340. for (i = 0; i < TX_RING_SIZE; i++) {
  341. pcnet32_bufs.tx_ring[i].base = 0;
  342. pcnet32_bufs.tx_ring[i].status = 0;
  343. }
  344. lp->init_block.tlen_rlen =
  345. le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
  346. for (i = 0; i < 6; i++)
  347. lp->init_block.phys_addr[i] = nic->node_addr[i];
  348. lp->init_block.rx_ring = virt_to_le32desc(&pcnet32_bufs.rx_ring[0]);
  349. lp->init_block.tx_ring = virt_to_le32desc(&pcnet32_bufs.tx_ring[0]);
  350. return 0;
  351. }
  352. /**************************************************************************
  353. RESET - Reset adapter
  354. ***************************************************************************/
  355. static void pcnet32_reset(struct nic *nic)
  356. {
  357. /* put the card in its initial state */
  358. u16 val;
  359. int i;
  360. /* Reset the PCNET32 */
  361. lp->a.reset(ioaddr);
  362. /* switch pcnet32 to 32bit mode */
  363. lp->a.write_bcr(ioaddr, 20, 2);
  364. /* set/reset autoselect bit */
  365. val = lp->a.read_bcr(ioaddr, 2) & ~2;
  366. if (lp->options & PCNET32_PORT_ASEL)
  367. val |= 2;
  368. lp->a.write_bcr(ioaddr, 2, val);
  369. /* handle full duplex setting */
  370. if (lp->full_duplex) {
  371. val = lp->a.read_bcr(ioaddr, 9) & ~3;
  372. if (lp->options & PCNET32_PORT_FD) {
  373. val |= 1;
  374. if (lp->options ==
  375. (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  376. val |= 2;
  377. } else if (lp->options & PCNET32_PORT_ASEL) {
  378. /* workaround of xSeries250, turn on for 79C975 only */
  379. i = ((lp->a.
  380. read_csr(ioaddr,
  381. 88) | (lp->a.read_csr(ioaddr,
  382. 89) << 16)) >>
  383. 12) & 0xffff;
  384. if (i == 0x2627)
  385. val |= 3;
  386. }
  387. lp->a.write_bcr(ioaddr, 9, val);
  388. }
  389. /* set/reset GPSI bit in test register */
  390. val = lp->a.read_csr(ioaddr, 124) & ~0x10;
  391. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  392. val |= 0x10;
  393. lp->a.write_csr(ioaddr, 124, val);
  394. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  395. val = lp->a.read_bcr(ioaddr, 32) & ~0x38; /* disable Auto Negotiation, set 10Mpbs, HD */
  396. if (lp->options & PCNET32_PORT_FD)
  397. val |= 0x10;
  398. if (lp->options & PCNET32_PORT_100)
  399. val |= 0x08;
  400. lp->a.write_bcr(ioaddr, 32, val);
  401. } else {
  402. if (lp->options & PCNET32_PORT_ASEL) { /* enable auto negotiate, setup, disable fd */
  403. val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
  404. val |= 0x20;
  405. lp->a.write_bcr(ioaddr, 32, val);
  406. }
  407. }
  408. #ifdef DO_DXSUFLO
  409. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  410. val = lp->a.read_csr(ioaddr, 3);
  411. val |= 0x40;
  412. lp->a.write_csr(ioaddr, 3, val);
  413. }
  414. #endif
  415. if (1)
  416. {
  417. //disable interrupts
  418. val = lp->a.read_csr(ioaddr, 3);
  419. val = val
  420. | (1 << 14) //BABLM intr disabled
  421. | (1 << 12) //MISSM missed frame mask intr disabled
  422. | (1 << 10) //RINTM receive intr disabled
  423. | (1 << 9) //TINTM transmit intr disabled
  424. | (1 << 8) //IDONM init done intr disabled
  425. ;
  426. lp->a.write_csr(ioaddr, 3, val);
  427. }
  428. if (lp->ltint) { /* Enable TxDone-intr inhibitor */
  429. val = lp->a.read_csr(ioaddr, 5);
  430. val |= (1 << 14);
  431. lp->a.write_csr(ioaddr, 5, val);
  432. }
  433. lp->init_block.mode =
  434. le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  435. lp->init_block.filter[0] = 0xffffffff;
  436. lp->init_block.filter[1] = 0xffffffff;
  437. pcnet32_init_ring(nic);
  438. /* Re-initialize the PCNET32, and start it when done. */
  439. lp->a.write_csr(ioaddr, 1,
  440. (virt_to_bus(&lp->init_block)) & 0xffff);
  441. lp->a.write_csr(ioaddr, 2, (virt_to_bus(&lp->init_block)) >> 16);
  442. lp->a.write_csr(ioaddr, 4, 0x0915);
  443. lp->a.write_csr(ioaddr, 0, 0x0001);
  444. i = 0;
  445. while (i++ < 100)
  446. if (lp->a.read_csr(ioaddr, 0) & 0x0100)
  447. break;
  448. /*
  449. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  450. * reports that doing so triggers a bug in the '974.
  451. */
  452. lp->a.write_csr(ioaddr, 0, 0x0042);
  453. dprintf(("pcnet32 open, csr0 %hX.\n", lp->a.read_csr(ioaddr, 0)));
  454. }
  455. /**************************************************************************
  456. POLL - Wait for a frame
  457. ***************************************************************************/
  458. static int pcnet32_poll(struct nic *nic __unused, int retrieve)
  459. {
  460. /* return true if there's an ethernet packet ready to read */
  461. /* nic->packet should contain data on return */
  462. /* nic->packetlen should contain length of data */
  463. signed char status;
  464. int entry;
  465. entry = lp->cur_rx & RX_RING_MOD_MASK;
  466. status = (le16_to_cpu(pcnet32_bufs.rx_ring[entry].status) >> 8);
  467. if (status < 0)
  468. return 0;
  469. if ( ! retrieve ) return 1;
  470. if (status == 0x03) {
  471. nic->packetlen =
  472. (le32_to_cpu(pcnet32_bufs.rx_ring[entry].msg_length)
  473. & 0xfff) - 4;
  474. memcpy(nic->packet, &pcnet32_bufs.rxb[entry], nic->packetlen);
  475. /* Andrew Boyd of QNX reports that some revs of the 79C765
  476. * clear the buffer length */
  477. pcnet32_bufs.rx_ring[entry].buf_length
  478. = le16_to_cpu(-PKT_BUF_SZ);
  479. /* prime for next receive */
  480. pcnet32_bufs.rx_ring[entry].status |= le16_to_cpu(0x8000);
  481. /* Switch to the next Rx ring buffer */
  482. lp->cur_rx++;
  483. } else {
  484. return 0;
  485. }
  486. return 1;
  487. }
  488. /**************************************************************************
  489. TRANSMIT - Transmit a frame
  490. ***************************************************************************/
  491. static void pcnet32_transmit(struct nic *nic __unused, const char *d, /* Destination */
  492. unsigned int t, /* Type */
  493. unsigned int s, /* size */
  494. const char *p)
  495. { /* Packet */
  496. /* send the packet to destination */
  497. unsigned long time;
  498. u8 *ptxb;
  499. u16 nstype;
  500. u16 status;
  501. int entry = 0; /*lp->cur_tx & TX_RING_MOD_MASK; */
  502. status = 0x8300;
  503. /* point to the current txb incase multiple tx_rings are used */
  504. ptxb = pcnet32_bufs.txb + (lp->cur_tx * PKT_BUF_SZ);
  505. /* copy the packet to ring buffer */
  506. memcpy(ptxb, d, ETH_ALEN); /* dst */
  507. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN); /* src */
  508. nstype = htons((u16) t); /* type */
  509. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2); /* type */
  510. memcpy(ptxb + ETH_HLEN, p, s);
  511. s += ETH_HLEN;
  512. while (s < ETH_ZLEN) /* pad to min length */
  513. ptxb[s++] = '\0';
  514. pcnet32_bufs.tx_ring[entry].length = le16_to_cpu(-s);
  515. pcnet32_bufs.tx_ring[entry].misc = 0x00000000;
  516. pcnet32_bufs.tx_ring[entry].base = (u32) virt_to_le32desc(ptxb);
  517. /* we set the top byte as the very last thing */
  518. pcnet32_bufs.tx_ring[entry].status = le16_to_cpu(status);
  519. /* Trigger an immediate send poll */
  520. lp->a.write_csr(ioaddr, 0, 0x0048);
  521. /* wait for transmit complete */
  522. lp->cur_tx = 0; /* (lp->cur_tx + 1); */
  523. time = currticks() + TICKS_PER_SEC; /* wait one second */
  524. while (currticks() < time &&
  525. ((short) le16_to_cpu(pcnet32_bufs.tx_ring[entry].status) < 0));
  526. if ((short) le16_to_cpu(pcnet32_bufs.tx_ring[entry].status) < 0)
  527. printf("PCNET32 timed out on transmit\n");
  528. /* Stop pointing at the current txb
  529. * otherwise the card continues to send the packet */
  530. pcnet32_bufs.tx_ring[entry].base = 0;
  531. }
  532. /**************************************************************************
  533. DISABLE - Turn off ethernet interface
  534. ***************************************************************************/
  535. static void pcnet32_disable ( struct nic *nic __unused ) {
  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 problems with dumb
  540. * DOS packet driver after a warm reboot
  541. */
  542. lp->a.write_bcr(ioaddr, 20, 0);
  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 nic *nic, struct pci_device *pci ) {
  563. int i, media;
  564. int fdx, mii, fset, dxsuflo, ltint;
  565. int chip_version;
  566. struct pcnet32_access *a = NULL;
  567. char *chipname;
  568. u8 promaddr[6];
  569. int shared = 1;
  570. if (pci->ioaddr == 0)
  571. return 0;
  572. /* BASE is used throughout to address the card */
  573. ioaddr = pci->ioaddr;
  574. printf("pcnet32.c: Found %s, Vendor=0x%hX Device=0x%hX\n",
  575. pci->driver_name, pci->vendor, pci->device);
  576. nic->irqno = 0;
  577. nic->ioaddr = pci->ioaddr & ~3;
  578. /* reset the chip */
  579. pcnet32_wio_reset(ioaddr);
  580. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  581. if (pcnet32_wio_read_csr(ioaddr, 0) == 4
  582. && pcnet32_wio_check(ioaddr)) {
  583. a = &pcnet32_wio;
  584. } else {
  585. pcnet32_dwio_reset(ioaddr);
  586. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4
  587. && pcnet32_dwio_check(ioaddr)) {
  588. a = &pcnet32_dwio;
  589. } else
  590. return 0;
  591. }
  592. chip_version =
  593. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  594. dprintf(("PCnet chip version is 0x%X\n", chip_version));
  595. if ((chip_version & 0xfff) != 0x003)
  596. return 0;
  597. /* initialize variables */
  598. fdx = mii = fset = dxsuflo = ltint = 0;
  599. chip_version = (chip_version >> 12) & 0xffff;
  600. switch (chip_version) {
  601. case 0x2420:
  602. chipname = "PCnet/PCI 79C970"; /* PCI */
  603. break;
  604. case 0x2430:
  605. if (shared)
  606. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  607. else
  608. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  609. break;
  610. case 0x2621:
  611. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  612. fdx = 1;
  613. break;
  614. case 0x2623:
  615. chipname = "PCnet/FAST 79C971"; /* PCI */
  616. fdx = 1;
  617. mii = 1;
  618. fset = 1;
  619. ltint = 1;
  620. break;
  621. case 0x2624:
  622. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  623. fdx = 1;
  624. mii = 1;
  625. fset = 1;
  626. break;
  627. case 0x2625:
  628. chipname = "PCnet/FAST III 79C973"; /* PCI */
  629. fdx = 1;
  630. mii = 1;
  631. break;
  632. case 0x2626:
  633. chipname = "PCnet/Home 79C978"; /* PCI */
  634. fdx = 1;
  635. /*
  636. * This is based on specs published at www.amd.com. This section
  637. * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
  638. * mode. The 79C978 can also go into standard ethernet, and there
  639. * probably should be some sort of module option to select the
  640. * mode by which the card should operate
  641. */
  642. /* switch to home wiring mode */
  643. media = a->read_bcr(ioaddr, 49);
  644. printf("media reset to %#x.\n", media);
  645. a->write_bcr(ioaddr, 49, media);
  646. break;
  647. case 0x2627:
  648. chipname = "PCnet/FAST III 79C975"; /* PCI */
  649. fdx = 1;
  650. mii = 1;
  651. break;
  652. default:
  653. chipname = "UNKNOWN";
  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. DBG ( "%s at %hX,", chipname, (unsigned int) 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. DBG ( "%s: IO Addr 0x%hX, MAC Addr %s\n ", chipname, (unsigned int) ioaddr,
  682. eth_ntoa ( nic->node_addr ) );
  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. // Causes a loader:
  745. // bin/blib.a(pcnet32.o)(.text+0x6b6): In function `pcnet32_probe':
  746. // drivers/net/pcnet32.c:871: undefined reference to `memcpy'
  747. // make: *** [bin/pcnet32.dsk.tmp] Error 1
  748. // So we do:
  749. memcpy ( &lp->a, a, sizeof ( lp->a ) );
  750. // To explicity call memcpy.
  751. /* detect special T1/E1 WAN card by checking for MAC address */
  752. if (nic->node_addr[0] == 0x00 && nic->node_addr[1] == 0xe0
  753. && nic->node_addr[2] == 0x75)
  754. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  755. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  756. lp->init_block.tlen_rlen =
  757. le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
  758. for (i = 0; i < 6; i++)
  759. lp->init_block.phys_addr[i] = nic->node_addr[i];
  760. lp->init_block.filter[0] = 0xffffffff;
  761. lp->init_block.filter[1] = 0xffffffff;
  762. lp->init_block.rx_ring = virt_to_bus(&pcnet32_bufs.rx_ring);
  763. lp->init_block.tx_ring = virt_to_bus(&pcnet32_bufs.tx_ring);
  764. /* switch pcnet32 to 32bit mode */
  765. a->write_bcr(ioaddr, 20, 2);
  766. a->write_csr(ioaddr, 1, (virt_to_bus(&lp->init_block)) & 0xffff);
  767. a->write_csr(ioaddr, 2, (virt_to_bus(&lp->init_block)) >> 16);
  768. /*
  769. * To auto-IRQ we enable the initialization-done and DMA error
  770. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  771. * boards will work.
  772. */
  773. /* Trigger an initialization just for the interrupt. */
  774. // a->write_csr(ioaddr, 0, 0x41);
  775. // mdelay(1);
  776. cards_found++;
  777. /* point to NIC specific routines */
  778. pcnet32_reset(nic);
  779. if (mii) {
  780. int tmp;
  781. int phy, phy_idx = 0;
  782. u16 mii_lpa;
  783. lp->phys[0] = 1; /* Default Setting */
  784. for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
  785. int mii_status = mdio_read(nic, phy, MII_BMSR);
  786. if (mii_status != 0xffff && mii_status != 0x0000) {
  787. lp->phys[phy_idx++] = phy;
  788. lp->mii_if.advertising =
  789. mdio_read(nic, phy, MII_ADVERTISE);
  790. if ((mii_status & 0x0040) == 0) {
  791. tmp = phy;
  792. dprintf (("MII PHY found at address %d, status "
  793. "%hX advertising %hX\n", phy, mii_status,
  794. lp->mii_if.advertising));
  795. }
  796. }
  797. }
  798. if (phy_idx == 0)
  799. printf("No MII transceiver found!\n");
  800. lp->mii_if.phy_id = lp->phys[0];
  801. lp->mii_if.advertising =
  802. mdio_read(nic, lp->phys[0], MII_ADVERTISE);
  803. mii_lpa = mdio_read(nic, lp->phys[0], MII_LPA);
  804. lp->mii_if.advertising &= mii_lpa;
  805. if (lp->mii_if.advertising & ADVERTISE_100FULL)
  806. printf("100Mbps Full-Duplex\n");
  807. else if (lp->mii_if.advertising & ADVERTISE_100HALF)
  808. printf("100Mbps Half-Duplex\n");
  809. else if (lp->mii_if.advertising & ADVERTISE_10FULL)
  810. printf("10Mbps Full-Duplex\n");
  811. else if (lp->mii_if.advertising & ADVERTISE_10HALF)
  812. printf("10Mbps Half-Duplex\n");
  813. else
  814. printf("\n");
  815. } else {
  816. /* The older chips are fixed 10Mbps, and some support full duplex,
  817. * although not via autonegotiation, but only via configuration. */
  818. if (fdx)
  819. printf("10Mbps Full-Duplex\n");
  820. else
  821. printf("10Mbps Half-Duplex\n");
  822. }
  823. nic->nic_op = &pcnet32_operations;
  824. return 1;
  825. }
  826. static int mdio_read(struct nic *nic __unused, int phy_id, int reg_num)
  827. {
  828. u16 val_out;
  829. int phyaddr;
  830. if (!lp->mii)
  831. return 0;
  832. phyaddr = lp->a.read_bcr(ioaddr, 33);
  833. lp->a.write_bcr(ioaddr, 33,
  834. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  835. val_out = lp->a.read_bcr(ioaddr, 34);
  836. lp->a.write_bcr(ioaddr, 33, phyaddr);
  837. return val_out;
  838. }
  839. #if 0
  840. static void mdio_write(struct nic *nic __unused, int phy_id, int reg_num,
  841. int val)
  842. {
  843. int phyaddr;
  844. if (!lp->mii)
  845. return;
  846. phyaddr = lp->a.read_bcr(ioaddr, 33);
  847. lp->a.write_bcr(ioaddr, 33,
  848. ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  849. lp->a.write_bcr(ioaddr, 34, val);
  850. lp->a.write_bcr(ioaddr, 33, phyaddr);
  851. }
  852. #endif
  853. static struct nic_operations pcnet32_operations = {
  854. .connect = dummy_connect,
  855. .poll = pcnet32_poll,
  856. .transmit = pcnet32_transmit,
  857. .irq = pcnet32_irq,
  858. };
  859. static struct pci_device_id pcnet32_nics[] = {
  860. PCI_ROM(0x1022, 0x2000, "pcnet32", "AMD PCnet/PCI"),
  861. PCI_ROM(0x1022, 0x2625, "pcnetfastiii", "AMD PCNet FAST III"),
  862. PCI_ROM(0x1022, 0x2001, "amdhomepna", "AMD PCnet/HomePNA"),
  863. };
  864. PCI_DRIVER ( pcnet32_driver, pcnet32_nics, PCI_NO_CLASS );
  865. DRIVER ( "PCNET32/PCI", nic_driver, pci_driver, pcnet32_driver,
  866. pcnet32_probe, pcnet32_disable );
  867. /*
  868. * Local variables:
  869. * c-basic-offset: 8
  870. * c-indent-level: 8
  871. * tab-width: 8
  872. * End:
  873. */