Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pcnet32.c 28KB

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