pcnet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. #ifdef CONFIG_PN62
  244. /* Setup LED registers */
  245. val = pcnet_read_bcr(dev, 2) | 0x1000;
  246. pcnet_write_bcr(dev, 2, val); /* enable LEDPE */
  247. pcnet_write_bcr(dev, 4, 0x5080); /* 100MBit */
  248. pcnet_write_bcr(dev, 5, 0x40c0); /* LNKSE */
  249. pcnet_write_bcr(dev, 6, 0x4090); /* TX Activity */
  250. pcnet_write_bcr(dev, 7, 0x4084); /* RX Activity */
  251. #endif
  252. /* Set/reset autoselect bit */
  253. val = pcnet_read_bcr(dev, 2) & ~2;
  254. val |= 2;
  255. pcnet_write_bcr(dev, 2, val);
  256. /* Enable auto negotiate, setup, disable fd */
  257. val = pcnet_read_bcr(dev, 32) & ~0x98;
  258. val |= 0x20;
  259. pcnet_write_bcr(dev, 32, val);
  260. /*
  261. * We only maintain one structure because the drivers will never
  262. * be used concurrently. In 32bit mode the RX and TX ring entries
  263. * must be aligned on 16-byte boundaries.
  264. */
  265. if (lp == NULL) {
  266. addr = (u32)malloc(sizeof(pcnet_priv_t) + 0x10);
  267. addr = (addr + 0xf) & ~0xf;
  268. lp = (pcnet_priv_t *)addr;
  269. }
  270. lp->init_block.mode = cpu_to_le16(0x0000);
  271. lp->init_block.filter[0] = 0x00000000;
  272. lp->init_block.filter[1] = 0x00000000;
  273. /*
  274. * Initialize the Rx ring.
  275. */
  276. lp->cur_rx = 0;
  277. for (i = 0; i < RX_RING_SIZE; i++) {
  278. lp->rx_ring[i].base = PCI_TO_MEM_LE(dev, lp->rx_buf[i]);
  279. lp->rx_ring[i].buf_length = cpu_to_le16(-PKT_BUF_SZ);
  280. lp->rx_ring[i].status = cpu_to_le16(0x8000);
  281. PCNET_DEBUG1
  282. ("Rx%d: base=0x%x buf_length=0x%hx status=0x%hx\n", i,
  283. lp->rx_ring[i].base, lp->rx_ring[i].buf_length,
  284. lp->rx_ring[i].status);
  285. }
  286. /*
  287. * Initialize the Tx ring. The Tx buffer address is filled in as
  288. * needed, but we do need to clear the upper ownership bit.
  289. */
  290. lp->cur_tx = 0;
  291. for (i = 0; i < TX_RING_SIZE; i++) {
  292. lp->tx_ring[i].base = 0;
  293. lp->tx_ring[i].status = 0;
  294. }
  295. /*
  296. * Setup Init Block.
  297. */
  298. PCNET_DEBUG1("Init block at 0x%p: MAC", &lp->init_block);
  299. for (i = 0; i < 6; i++) {
  300. lp->init_block.phys_addr[i] = dev->enetaddr[i];
  301. PCNET_DEBUG1(" %02x", lp->init_block.phys_addr[i]);
  302. }
  303. lp->init_block.tlen_rlen = cpu_to_le16(TX_RING_LEN_BITS |
  304. RX_RING_LEN_BITS);
  305. lp->init_block.rx_ring = PCI_TO_MEM_LE(dev, lp->rx_ring);
  306. lp->init_block.tx_ring = PCI_TO_MEM_LE(dev, lp->tx_ring);
  307. PCNET_DEBUG1("\ntlen_rlen=0x%x rx_ring=0x%x tx_ring=0x%x\n",
  308. lp->init_block.tlen_rlen,
  309. lp->init_block.rx_ring, lp->init_block.tx_ring);
  310. /*
  311. * Tell the controller where the Init Block is located.
  312. */
  313. addr = PCI_TO_MEM(dev, &lp->init_block);
  314. pcnet_write_csr(dev, 1, addr & 0xffff);
  315. pcnet_write_csr(dev, 2, (addr >> 16) & 0xffff);
  316. pcnet_write_csr(dev, 4, 0x0915);
  317. pcnet_write_csr(dev, 0, 0x0001); /* start */
  318. /* Wait for Init Done bit */
  319. for (i = 10000; i > 0; i--) {
  320. if (pcnet_read_csr(dev, 0) & 0x0100)
  321. break;
  322. udelay(10);
  323. }
  324. if (i <= 0) {
  325. printf("%s: TIMEOUT: controller init failed\n", dev->name);
  326. pcnet_reset(dev);
  327. return -1;
  328. }
  329. /*
  330. * Finally start network controller operation.
  331. */
  332. pcnet_write_csr(dev, 0, 0x0002);
  333. return 0;
  334. }
  335. static int pcnet_send(struct eth_device *dev, void *packet, int pkt_len)
  336. {
  337. int i, status;
  338. struct pcnet_tx_head *entry = &lp->tx_ring[lp->cur_tx];
  339. PCNET_DEBUG2("Tx%d: %d bytes from 0x%p ", lp->cur_tx, pkt_len,
  340. packet);
  341. /* Wait for completion by testing the OWN bit */
  342. for (i = 1000; i > 0; i--) {
  343. status = le16_to_cpu(entry->status);
  344. if ((status & 0x8000) == 0)
  345. break;
  346. udelay(100);
  347. PCNET_DEBUG2(".");
  348. }
  349. if (i <= 0) {
  350. printf("%s: TIMEOUT: Tx%d failed (status = 0x%x)\n",
  351. dev->name, lp->cur_tx, status);
  352. pkt_len = 0;
  353. goto failure;
  354. }
  355. /*
  356. * Setup Tx ring. Caution: the write order is important here,
  357. * set the status with the "ownership" bits last.
  358. */
  359. status = 0x8300;
  360. entry->length = le16_to_cpu(-pkt_len);
  361. entry->misc = 0x00000000;
  362. entry->base = PCI_TO_MEM_LE(dev, packet);
  363. entry->status = le16_to_cpu(status);
  364. /* Trigger an immediate send poll. */
  365. pcnet_write_csr(dev, 0, 0x0008);
  366. failure:
  367. if (++lp->cur_tx >= TX_RING_SIZE)
  368. lp->cur_tx = 0;
  369. PCNET_DEBUG2("done\n");
  370. return pkt_len;
  371. }
  372. static int pcnet_recv (struct eth_device *dev)
  373. {
  374. struct pcnet_rx_head *entry;
  375. int pkt_len = 0;
  376. u16 status;
  377. while (1) {
  378. entry = &lp->rx_ring[lp->cur_rx];
  379. /*
  380. * If we own the next entry, it's a new packet. Send it up.
  381. */
  382. status = le16_to_cpu(entry->status);
  383. if ((status & 0x8000) != 0)
  384. break;
  385. status >>= 8;
  386. if (status != 0x03) { /* There was an error. */
  387. printf("%s: Rx%d", dev->name, lp->cur_rx);
  388. PCNET_DEBUG1(" (status=0x%x)", status);
  389. if (status & 0x20)
  390. printf(" Frame");
  391. if (status & 0x10)
  392. printf(" Overflow");
  393. if (status & 0x08)
  394. printf(" CRC");
  395. if (status & 0x04)
  396. printf(" Fifo");
  397. printf(" Error\n");
  398. entry->status &= le16_to_cpu(0x03ff);
  399. } else {
  400. pkt_len = (le32_to_cpu(entry->msg_length) & 0xfff) - 4;
  401. if (pkt_len < 60) {
  402. printf("%s: Rx%d: invalid packet length %d\n",
  403. dev->name, lp->cur_rx, pkt_len);
  404. } else {
  405. NetReceive(lp->rx_buf[lp->cur_rx], pkt_len);
  406. PCNET_DEBUG2("Rx%d: %d bytes from 0x%p\n",
  407. lp->cur_rx, pkt_len,
  408. lp->rx_buf[lp->cur_rx]);
  409. }
  410. }
  411. entry->status |= cpu_to_le16(0x8000);
  412. if (++lp->cur_rx >= RX_RING_SIZE)
  413. lp->cur_rx = 0;
  414. }
  415. return pkt_len;
  416. }
  417. static void pcnet_halt(struct eth_device *dev)
  418. {
  419. int i;
  420. PCNET_DEBUG1("%s: pcnet_halt...\n", dev->name);
  421. /* Reset the PCnet controller */
  422. pcnet_reset(dev);
  423. /* Wait for Stop bit */
  424. for (i = 1000; i > 0; i--) {
  425. if (pcnet_read_csr(dev, 0) & 0x4)
  426. break;
  427. udelay(10);
  428. }
  429. if (i <= 0)
  430. printf("%s: TIMEOUT: controller reset failed\n", dev->name);
  431. }