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

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