pcnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * (C) Copyright 2002 Wolfgang Grandegger, wg@denx.de.
  3. *
  4. * This driver for AMD PCnet network controllers is derived from the
  5. * Linux driver pcnet32.c written 1996-1999 by Thomas Bogendoerfer.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <netdev.h>
  13. #include <asm/io.h>
  14. #include <pci.h>
  15. #define PCNET_DEBUG_LEVEL 0 /* 0=off, 1=init, 2=rx/tx */
  16. #define PCNET_DEBUG1(fmt,args...) \
  17. debug_cond(PCNET_DEBUG_LEVEL > 0, fmt ,##args)
  18. #define PCNET_DEBUG2(fmt,args...) \
  19. debug_cond(PCNET_DEBUG_LEVEL > 1, fmt ,##args)
  20. #if !defined(CONF_PCNET_79C973) && defined(CONF_PCNET_79C975)
  21. #error "Macro for PCnet chip version is not defined!"
  22. #endif
  23. /*
  24. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  25. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  26. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  27. */
  28. #define PCNET_LOG_TX_BUFFERS 0
  29. #define PCNET_LOG_RX_BUFFERS 2
  30. #define TX_RING_SIZE (1 << (PCNET_LOG_TX_BUFFERS))
  31. #define TX_RING_LEN_BITS ((PCNET_LOG_TX_BUFFERS) << 12)
  32. #define RX_RING_SIZE (1 << (PCNET_LOG_RX_BUFFERS))
  33. #define RX_RING_LEN_BITS ((PCNET_LOG_RX_BUFFERS) << 4)
  34. #define PKT_BUF_SZ 1544
  35. /* The PCNET Rx and Tx ring descriptors. */
  36. struct pcnet_rx_head {
  37. u32 base;
  38. s16 buf_length;
  39. s16 status;
  40. u32 msg_length;
  41. u32 reserved;
  42. };
  43. struct pcnet_tx_head {
  44. u32 base;
  45. s16 length;
  46. s16 status;
  47. u32 misc;
  48. u32 reserved;
  49. };
  50. /* The PCNET 32-Bit initialization block, described in databook. */
  51. struct pcnet_init_block {
  52. u16 mode;
  53. u16 tlen_rlen;
  54. u8 phys_addr[6];
  55. u16 reserved;
  56. u32 filter[2];
  57. /* Receive and transmit ring base, along with extra bits. */
  58. u32 rx_ring;
  59. u32 tx_ring;
  60. u32 reserved2;
  61. };
  62. typedef struct pcnet_priv {
  63. struct pcnet_rx_head rx_ring[RX_RING_SIZE];
  64. struct pcnet_tx_head tx_ring[TX_RING_SIZE];
  65. struct pcnet_init_block init_block;
  66. /* Receive Buffer space */
  67. unsigned char rx_buf[RX_RING_SIZE][PKT_BUF_SZ + 4];
  68. int cur_rx;
  69. int cur_tx;
  70. } pcnet_priv_t;
  71. static pcnet_priv_t *lp;
  72. /* Offsets from base I/O address for WIO mode */
  73. #define PCNET_RDP 0x10
  74. #define PCNET_RAP 0x12
  75. #define PCNET_RESET 0x14
  76. #define PCNET_BDP 0x16
  77. static u16 pcnet_read_csr(struct eth_device *dev, int index)
  78. {
  79. outw(index, dev->iobase + PCNET_RAP);
  80. return inw(dev->iobase + PCNET_RDP);
  81. }
  82. static void pcnet_write_csr(struct eth_device *dev, int index, u16 val)
  83. {
  84. outw(index, dev->iobase + PCNET_RAP);
  85. outw(val, dev->iobase + PCNET_RDP);
  86. }
  87. static u16 pcnet_read_bcr(struct eth_device *dev, int index)
  88. {
  89. outw(index, dev->iobase + PCNET_RAP);
  90. return inw(dev->iobase + PCNET_BDP);
  91. }
  92. static void pcnet_write_bcr(struct eth_device *dev, int index, u16 val)
  93. {
  94. outw(index, dev->iobase + PCNET_RAP);
  95. outw(val, dev->iobase + PCNET_BDP);
  96. }
  97. static void pcnet_reset(struct eth_device *dev)
  98. {
  99. inw(dev->iobase + PCNET_RESET);
  100. }
  101. static int pcnet_check(struct eth_device *dev)
  102. {
  103. outw(88, dev->iobase + PCNET_RAP);
  104. return inw(dev->iobase + PCNET_RAP) == 88;
  105. }
  106. static int pcnet_init (struct eth_device *dev, bd_t * bis);
  107. static int pcnet_send(struct eth_device *dev, void *packet, int length);
  108. static int pcnet_recv (struct eth_device *dev);
  109. static void pcnet_halt (struct eth_device *dev);
  110. static int pcnet_probe (struct eth_device *dev, bd_t * bis, int dev_num);
  111. #define PCI_TO_MEM(d, a) pci_virt_to_mem((pci_dev_t)d->priv, (a))
  112. #define PCI_TO_MEM_LE(d,a) (u32)(cpu_to_le32(PCI_TO_MEM(d,a)))
  113. static struct pci_device_id supported[] = {
  114. {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE},
  115. {}
  116. };
  117. int pcnet_initialize(bd_t *bis)
  118. {
  119. pci_dev_t devbusfn;
  120. struct eth_device *dev;
  121. u16 command, status;
  122. int dev_nr = 0;
  123. PCNET_DEBUG1("\npcnet_initialize...\n");
  124. for (dev_nr = 0;; dev_nr++) {
  125. /*
  126. * Find the PCnet PCI device(s).
  127. */
  128. devbusfn = pci_find_devices(supported, dev_nr);
  129. if (devbusfn < 0)
  130. break;
  131. /*
  132. * Allocate and pre-fill the device structure.
  133. */
  134. dev = (struct eth_device *)malloc(sizeof(*dev));
  135. if (!dev) {
  136. printf("pcnet: Can not allocate memory\n");
  137. break;
  138. }
  139. memset(dev, 0, sizeof(*dev));
  140. dev->priv = (void *)devbusfn;
  141. sprintf(dev->name, "pcnet#%d", dev_nr);
  142. /*
  143. * Setup the PCI device.
  144. */
  145. pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_0,
  146. (unsigned int *)&dev->iobase);
  147. dev->iobase = pci_io_to_phys(devbusfn, dev->iobase);
  148. dev->iobase &= ~0xf;
  149. PCNET_DEBUG1("%s: devbusfn=0x%x iobase=0x%x: ",
  150. dev->name, devbusfn, dev->iobase);
  151. command = PCI_COMMAND_IO | PCI_COMMAND_MASTER;
  152. pci_write_config_word(devbusfn, PCI_COMMAND, command);
  153. pci_read_config_word(devbusfn, PCI_COMMAND, &status);
  154. if ((status & command) != command) {
  155. printf("%s: Couldn't enable IO access or Bus Mastering\n",
  156. dev->name);
  157. free(dev);
  158. continue;
  159. }
  160. pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x40);
  161. /*
  162. * Probe the PCnet chip.
  163. */
  164. if (pcnet_probe(dev, bis, dev_nr) < 0) {
  165. free(dev);
  166. continue;
  167. }
  168. /*
  169. * Setup device structure and register the driver.
  170. */
  171. dev->init = pcnet_init;
  172. dev->halt = pcnet_halt;
  173. dev->send = pcnet_send;
  174. dev->recv = pcnet_recv;
  175. eth_register(dev);
  176. }
  177. udelay(10 * 1000);
  178. return dev_nr;
  179. }
  180. static int pcnet_probe(struct eth_device *dev, bd_t *bis, int dev_nr)
  181. {
  182. int chip_version;
  183. char *chipname;
  184. #ifdef PCNET_HAS_PROM
  185. int i;
  186. #endif
  187. /* Reset the PCnet controller */
  188. pcnet_reset(dev);
  189. /* Check if register access is working */
  190. if (pcnet_read_csr(dev, 0) != 4 || !pcnet_check(dev)) {
  191. printf("%s: CSR register access check failed\n", dev->name);
  192. return -1;
  193. }
  194. /* Identify the chip */
  195. chip_version =
  196. pcnet_read_csr(dev, 88) | (pcnet_read_csr(dev, 89) << 16);
  197. if ((chip_version & 0xfff) != 0x003)
  198. return -1;
  199. chip_version = (chip_version >> 12) & 0xffff;
  200. switch (chip_version) {
  201. case 0x2621:
  202. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  203. break;
  204. #ifdef CONFIG_PCNET_79C973
  205. case 0x2625:
  206. chipname = "PCnet/FAST III 79C973"; /* PCI */
  207. break;
  208. #endif
  209. #ifdef CONFIG_PCNET_79C975
  210. case 0x2627:
  211. chipname = "PCnet/FAST III 79C975"; /* PCI */
  212. break;
  213. #endif
  214. default:
  215. printf("%s: PCnet version %#x not supported\n",
  216. dev->name, chip_version);
  217. return -1;
  218. }
  219. PCNET_DEBUG1("AMD %s\n", chipname);
  220. #ifdef PCNET_HAS_PROM
  221. /*
  222. * In most chips, after a chip reset, the ethernet address is read from
  223. * the station address PROM at the base address and programmed into the
  224. * "Physical Address Registers" CSR12-14.
  225. */
  226. for (i = 0; i < 3; i++) {
  227. unsigned int val;
  228. val = pcnet_read_csr(dev, i + 12) & 0x0ffff;
  229. /* There may be endianness issues here. */
  230. dev->enetaddr[2 * i] = val & 0x0ff;
  231. dev->enetaddr[2 * i + 1] = (val >> 8) & 0x0ff;
  232. }
  233. #endif /* PCNET_HAS_PROM */
  234. return 0;
  235. }
  236. static int pcnet_init(struct eth_device *dev, bd_t *bis)
  237. {
  238. int i, val;
  239. u32 addr;
  240. PCNET_DEBUG1("%s: pcnet_init...\n", dev->name);
  241. /* Switch pcnet to 32bit mode */
  242. pcnet_write_bcr(dev, 20, 2);
  243. /* Set/reset autoselect bit */
  244. val = pcnet_read_bcr(dev, 2) & ~2;
  245. val |= 2;
  246. pcnet_write_bcr(dev, 2, val);
  247. /* Enable auto negotiate, setup, disable fd */
  248. val = pcnet_read_bcr(dev, 32) & ~0x98;
  249. val |= 0x20;
  250. pcnet_write_bcr(dev, 32, val);
  251. /*
  252. * Enable NOUFLO on supported controllers, with the transmit
  253. * start point set to the full packet. This will cause entire
  254. * packets to be buffered by the ethernet controller before
  255. * transmission, eliminating underflows which are common on
  256. * slower devices. Controllers which do not support NOUFLO will
  257. * simply be left with a larger transmit FIFO threshold.
  258. */
  259. val = pcnet_read_bcr(dev, 18);
  260. val |= 1 << 11;
  261. pcnet_write_bcr(dev, 18, val);
  262. val = pcnet_read_csr(dev, 80);
  263. val |= 0x3 << 10;
  264. pcnet_write_csr(dev, 80, val);
  265. /*
  266. * We only maintain one structure because the drivers will never
  267. * be used concurrently. In 32bit mode the RX and TX ring entries
  268. * must be aligned on 16-byte boundaries.
  269. */
  270. if (lp == NULL) {
  271. addr = (u32)malloc(sizeof(pcnet_priv_t) + 0x10);
  272. addr = (addr + 0xf) & ~0xf;
  273. lp = (pcnet_priv_t *)addr;
  274. }
  275. lp->init_block.mode = cpu_to_le16(0x0000);
  276. lp->init_block.filter[0] = 0x00000000;
  277. lp->init_block.filter[1] = 0x00000000;
  278. /*
  279. * Initialize the Rx ring.
  280. */
  281. lp->cur_rx = 0;
  282. for (i = 0; i < RX_RING_SIZE; i++) {
  283. lp->rx_ring[i].base = PCI_TO_MEM_LE(dev, lp->rx_buf[i]);
  284. lp->rx_ring[i].buf_length = cpu_to_le16(-PKT_BUF_SZ);
  285. lp->rx_ring[i].status = cpu_to_le16(0x8000);
  286. PCNET_DEBUG1
  287. ("Rx%d: base=0x%x buf_length=0x%hx status=0x%hx\n", i,
  288. lp->rx_ring[i].base, lp->rx_ring[i].buf_length,
  289. lp->rx_ring[i].status);
  290. }
  291. /*
  292. * Initialize the Tx ring. The Tx buffer address is filled in as
  293. * needed, but we do need to clear the upper ownership bit.
  294. */
  295. lp->cur_tx = 0;
  296. for (i = 0; i < TX_RING_SIZE; i++) {
  297. lp->tx_ring[i].base = 0;
  298. lp->tx_ring[i].status = 0;
  299. }
  300. /*
  301. * Setup Init Block.
  302. */
  303. PCNET_DEBUG1("Init block at 0x%p: MAC", &lp->init_block);
  304. for (i = 0; i < 6; i++) {
  305. lp->init_block.phys_addr[i] = dev->enetaddr[i];
  306. PCNET_DEBUG1(" %02x", lp->init_block.phys_addr[i]);
  307. }
  308. lp->init_block.tlen_rlen = cpu_to_le16(TX_RING_LEN_BITS |
  309. RX_RING_LEN_BITS);
  310. lp->init_block.rx_ring = PCI_TO_MEM_LE(dev, lp->rx_ring);
  311. lp->init_block.tx_ring = PCI_TO_MEM_LE(dev, lp->tx_ring);
  312. flush_dcache_range((unsigned long)lp, (unsigned long)&lp->rx_buf);
  313. PCNET_DEBUG1("\ntlen_rlen=0x%x rx_ring=0x%x tx_ring=0x%x\n",
  314. lp->init_block.tlen_rlen,
  315. lp->init_block.rx_ring, lp->init_block.tx_ring);
  316. /*
  317. * Tell the controller where the Init Block is located.
  318. */
  319. addr = PCI_TO_MEM(dev, &lp->init_block);
  320. pcnet_write_csr(dev, 1, addr & 0xffff);
  321. pcnet_write_csr(dev, 2, (addr >> 16) & 0xffff);
  322. pcnet_write_csr(dev, 4, 0x0915);
  323. pcnet_write_csr(dev, 0, 0x0001); /* start */
  324. /* Wait for Init Done bit */
  325. for (i = 10000; i > 0; i--) {
  326. if (pcnet_read_csr(dev, 0) & 0x0100)
  327. break;
  328. udelay(10);
  329. }
  330. if (i <= 0) {
  331. printf("%s: TIMEOUT: controller init failed\n", dev->name);
  332. pcnet_reset(dev);
  333. return -1;
  334. }
  335. /*
  336. * Finally start network controller operation.
  337. */
  338. pcnet_write_csr(dev, 0, 0x0002);
  339. return 0;
  340. }
  341. static int pcnet_send(struct eth_device *dev, void *packet, int pkt_len)
  342. {
  343. int i, status;
  344. struct pcnet_tx_head *entry = &lp->tx_ring[lp->cur_tx];
  345. PCNET_DEBUG2("Tx%d: %d bytes from 0x%p ", lp->cur_tx, pkt_len,
  346. packet);
  347. flush_dcache_range((unsigned long)packet,
  348. (unsigned long)packet + pkt_len);
  349. /* Wait for completion by testing the OWN bit */
  350. for (i = 1000; i > 0; i--) {
  351. invalidate_dcache_range((unsigned long)entry,
  352. (unsigned long)entry + sizeof(*entry));
  353. status = le16_to_cpu(entry->status);
  354. if ((status & 0x8000) == 0)
  355. break;
  356. udelay(100);
  357. PCNET_DEBUG2(".");
  358. }
  359. if (i <= 0) {
  360. printf("%s: TIMEOUT: Tx%d failed (status = 0x%x)\n",
  361. dev->name, lp->cur_tx, status);
  362. pkt_len = 0;
  363. goto failure;
  364. }
  365. /*
  366. * Setup Tx ring. Caution: the write order is important here,
  367. * set the status with the "ownership" bits last.
  368. */
  369. status = 0x8300;
  370. entry->length = cpu_to_le16(-pkt_len);
  371. entry->misc = 0x00000000;
  372. entry->base = PCI_TO_MEM_LE(dev, packet);
  373. entry->status = cpu_to_le16(status);
  374. flush_dcache_range((unsigned long)entry,
  375. (unsigned long)entry + sizeof(*entry));
  376. /* Trigger an immediate send poll. */
  377. pcnet_write_csr(dev, 0, 0x0008);
  378. failure:
  379. if (++lp->cur_tx >= TX_RING_SIZE)
  380. lp->cur_tx = 0;
  381. PCNET_DEBUG2("done\n");
  382. return pkt_len;
  383. }
  384. static int pcnet_recv (struct eth_device *dev)
  385. {
  386. struct pcnet_rx_head *entry;
  387. int pkt_len = 0;
  388. u16 status;
  389. while (1) {
  390. entry = &lp->rx_ring[lp->cur_rx];
  391. invalidate_dcache_range((unsigned long)entry,
  392. (unsigned long)entry + sizeof(*entry));
  393. /*
  394. * If we own the next entry, it's a new packet. Send it up.
  395. */
  396. status = le16_to_cpu(entry->status);
  397. if ((status & 0x8000) != 0)
  398. break;
  399. status >>= 8;
  400. if (status != 0x03) { /* There was an error. */
  401. printf("%s: Rx%d", dev->name, lp->cur_rx);
  402. PCNET_DEBUG1(" (status=0x%x)", status);
  403. if (status & 0x20)
  404. printf(" Frame");
  405. if (status & 0x10)
  406. printf(" Overflow");
  407. if (status & 0x08)
  408. printf(" CRC");
  409. if (status & 0x04)
  410. printf(" Fifo");
  411. printf(" Error\n");
  412. entry->status &= le16_to_cpu(0x03ff);
  413. } else {
  414. pkt_len = (le32_to_cpu(entry->msg_length) & 0xfff) - 4;
  415. if (pkt_len < 60) {
  416. printf("%s: Rx%d: invalid packet length %d\n",
  417. dev->name, lp->cur_rx, pkt_len);
  418. } else {
  419. invalidate_dcache_range(
  420. (unsigned long)lp->rx_buf[lp->cur_rx],
  421. (unsigned long)lp->rx_buf[lp->cur_rx] +
  422. pkt_len);
  423. NetReceive(lp->rx_buf[lp->cur_rx], pkt_len);
  424. PCNET_DEBUG2("Rx%d: %d bytes from 0x%p\n",
  425. lp->cur_rx, pkt_len,
  426. lp->rx_buf[lp->cur_rx]);
  427. }
  428. }
  429. entry->status |= cpu_to_le16(0x8000);
  430. flush_dcache_range((unsigned long)entry,
  431. (unsigned long)entry + sizeof(*entry));
  432. if (++lp->cur_rx >= RX_RING_SIZE)
  433. lp->cur_rx = 0;
  434. }
  435. return pkt_len;
  436. }
  437. static void pcnet_halt(struct eth_device *dev)
  438. {
  439. int i;
  440. PCNET_DEBUG1("%s: pcnet_halt...\n", dev->name);
  441. /* Reset the PCnet controller */
  442. pcnet_reset(dev);
  443. /* Wait for Stop bit */
  444. for (i = 1000; i > 0; i--) {
  445. if (pcnet_read_csr(dev, 0) & 0x4)
  446. break;
  447. udelay(10);
  448. }
  449. if (i <= 0)
  450. printf("%s: TIMEOUT: controller reset failed\n", dev->name);
  451. }