pcnet.c 13 KB

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