dnet.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Dave Ethernet Controller driver
  3. *
  4. * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <common.h>
  11. #ifndef CONFIG_DNET_AUTONEG_TIMEOUT
  12. #define CONFIG_DNET_AUTONEG_TIMEOUT 5000000 /* default value */
  13. #endif
  14. #include <net.h>
  15. #include <malloc.h>
  16. #include <linux/mii.h>
  17. #include <miiphy.h>
  18. #include <asm/io.h>
  19. #include <asm/unaligned.h>
  20. #include "dnet.h"
  21. struct dnet_device {
  22. struct dnet_registers *regs;
  23. const struct device *dev;
  24. struct eth_device netdev;
  25. unsigned short phy_addr;
  26. };
  27. /* get struct dnet_device from given struct netdev */
  28. #define to_dnet(_nd) container_of(_nd, struct dnet_device, netdev)
  29. /* function for reading internal MAC register */
  30. u16 dnet_readw_mac(struct dnet_device *dnet, u16 reg)
  31. {
  32. u16 data_read;
  33. /* issue a read */
  34. writel(reg, &dnet->regs->MACREG_ADDR);
  35. /* since a read/write op to the MAC is very slow,
  36. * we must wait before reading the data */
  37. udelay(1);
  38. /* read data read from the MAC register */
  39. data_read = readl(&dnet->regs->MACREG_DATA);
  40. /* all done */
  41. return data_read;
  42. }
  43. /* function for writing internal MAC register */
  44. void dnet_writew_mac(struct dnet_device *dnet, u16 reg, u16 val)
  45. {
  46. /* load data to write */
  47. writel(val, &dnet->regs->MACREG_DATA);
  48. /* issue a write */
  49. writel(reg | DNET_INTERNAL_WRITE, &dnet->regs->MACREG_ADDR);
  50. /* since a read/write op to the MAC is very slow,
  51. * we must wait before exiting */
  52. udelay(1);
  53. }
  54. static void dnet_mdio_write(struct dnet_device *dnet, u8 reg, u16 value)
  55. {
  56. u16 tmp;
  57. debug(DRIVERNAME "dnet_mdio_write %02x:%02x <- %04x\n",
  58. dnet->phy_addr, reg, value);
  59. while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
  60. DNET_INTERNAL_GMII_MNG_CMD_FIN))
  61. ;
  62. /* prepare for a write operation */
  63. tmp = (1 << 13);
  64. /* only 5 bits allowed for register offset */
  65. reg &= 0x1f;
  66. /* prepare reg_value for a write */
  67. tmp |= (dnet->phy_addr << 8);
  68. tmp |= reg;
  69. /* write data to write first */
  70. dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
  71. /* write control word */
  72. dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
  73. while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
  74. DNET_INTERNAL_GMII_MNG_CMD_FIN))
  75. ;
  76. }
  77. static u16 dnet_mdio_read(struct dnet_device *dnet, u8 reg)
  78. {
  79. u16 value;
  80. while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
  81. DNET_INTERNAL_GMII_MNG_CMD_FIN))
  82. ;
  83. /* only 5 bits allowed for register offset*/
  84. reg &= 0x1f;
  85. /* prepare reg_value for a read */
  86. value = (dnet->phy_addr << 8);
  87. value |= reg;
  88. /* write control word */
  89. dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
  90. /* wait for end of transfer */
  91. while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
  92. DNET_INTERNAL_GMII_MNG_CMD_FIN))
  93. ;
  94. value = dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG);
  95. debug(DRIVERNAME "dnet_mdio_read %02x:%02x <- %04x\n",
  96. dnet->phy_addr, reg, value);
  97. return value;
  98. }
  99. static int dnet_send(struct eth_device *netdev, void *packet, int length)
  100. {
  101. struct dnet_device *dnet = to_dnet(netdev);
  102. int i, wrsz;
  103. unsigned int *bufp;
  104. unsigned int tx_cmd;
  105. debug(DRIVERNAME "[%s] Sending %u bytes\n", __func__, length);
  106. bufp = (unsigned int *) (((u32)packet) & 0xFFFFFFFC);
  107. wrsz = (u32)length + 3;
  108. wrsz += ((u32)packet) & 0x3;
  109. wrsz >>= 2;
  110. tx_cmd = ((((unsigned int)(packet)) & 0x03) << 16) | (u32)length;
  111. /* check if there is enough room for the current frame */
  112. if (wrsz < (DNET_FIFO_SIZE - readl(&dnet->regs->TX_FIFO_WCNT))) {
  113. for (i = 0; i < wrsz; i++)
  114. writel(*bufp++, &dnet->regs->TX_DATA_FIFO);
  115. /*
  116. * inform MAC that a packet's written and ready
  117. * to be shipped out
  118. */
  119. writel(tx_cmd, &dnet->regs->TX_LEN_FIFO);
  120. } else {
  121. printf(DRIVERNAME "No free space (actual %d, required %d "
  122. "(words))\n", DNET_FIFO_SIZE -
  123. readl(&dnet->regs->TX_FIFO_WCNT), wrsz);
  124. }
  125. /* No one cares anyway */
  126. return 0;
  127. }
  128. static int dnet_recv(struct eth_device *netdev)
  129. {
  130. struct dnet_device *dnet = to_dnet(netdev);
  131. unsigned int *data_ptr;
  132. int pkt_len, poll, i;
  133. u32 cmd_word;
  134. debug("Waiting for pkt (polling)\n");
  135. poll = 50;
  136. while ((readl(&dnet->regs->RX_FIFO_WCNT) >> 16) == 0) {
  137. udelay(10); /* wait 10 usec */
  138. if (--poll == 0)
  139. return 0; /* no pkt available */
  140. }
  141. cmd_word = readl(&dnet->regs->RX_LEN_FIFO);
  142. pkt_len = cmd_word & 0xFFFF;
  143. debug("Got pkt with size %d bytes\n", pkt_len);
  144. if (cmd_word & 0xDF180000)
  145. printf("%s packet receive error %x\n", __func__, cmd_word);
  146. data_ptr = (unsigned int *)net_rx_packets[0];
  147. for (i = 0; i < (pkt_len + 3) >> 2; i++)
  148. *data_ptr++ = readl(&dnet->regs->RX_DATA_FIFO);
  149. /* ok + 5 ?? */
  150. net_process_received_packet(net_rx_packets[0], pkt_len + 5);
  151. return 0;
  152. }
  153. static void dnet_set_hwaddr(struct eth_device *netdev)
  154. {
  155. struct dnet_device *dnet = to_dnet(netdev);
  156. u16 tmp;
  157. tmp = get_unaligned_be16(netdev->enetaddr);
  158. dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
  159. tmp = get_unaligned_be16(&netdev->enetaddr[2]);
  160. dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
  161. tmp = get_unaligned_be16(&netdev->enetaddr[4]);
  162. dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
  163. }
  164. static void dnet_phy_reset(struct dnet_device *dnet)
  165. {
  166. struct eth_device *netdev = &dnet->netdev;
  167. int i;
  168. u16 status, adv;
  169. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  170. dnet_mdio_write(dnet, MII_ADVERTISE, adv);
  171. printf("%s: Starting autonegotiation...\n", netdev->name);
  172. dnet_mdio_write(dnet, MII_BMCR, (BMCR_ANENABLE
  173. | BMCR_ANRESTART));
  174. for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
  175. status = dnet_mdio_read(dnet, MII_BMSR);
  176. if (status & BMSR_ANEGCOMPLETE)
  177. break;
  178. udelay(100);
  179. }
  180. if (status & BMSR_ANEGCOMPLETE)
  181. printf("%s: Autonegotiation complete\n", netdev->name);
  182. else
  183. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  184. netdev->name, status);
  185. }
  186. static int dnet_phy_init(struct dnet_device *dnet)
  187. {
  188. struct eth_device *netdev = &dnet->netdev;
  189. u16 phy_id, status, adv, lpa;
  190. int media, speed, duplex;
  191. int i;
  192. u32 ctl_reg;
  193. /* Find a PHY */
  194. for (i = 0; i < 32; i++) {
  195. dnet->phy_addr = i;
  196. phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
  197. if (phy_id != 0xffff) {
  198. /* ok we found it */
  199. printf("Found PHY at address %d PHYID (%04x:%04x)\n",
  200. i, phy_id,
  201. dnet_mdio_read(dnet, MII_PHYSID2));
  202. break;
  203. }
  204. }
  205. /* Check if the PHY is up to snuff... */
  206. phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
  207. if (phy_id == 0xffff) {
  208. printf("%s: No PHY present\n", netdev->name);
  209. return -1;
  210. }
  211. status = dnet_mdio_read(dnet, MII_BMSR);
  212. if (!(status & BMSR_LSTATUS)) {
  213. /* Try to re-negotiate if we don't have link already. */
  214. dnet_phy_reset(dnet);
  215. for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
  216. status = dnet_mdio_read(dnet, MII_BMSR);
  217. if (status & BMSR_LSTATUS)
  218. break;
  219. udelay(100);
  220. }
  221. }
  222. if (!(status & BMSR_LSTATUS)) {
  223. printf("%s: link down (status: 0x%04x)\n",
  224. netdev->name, status);
  225. return -1;
  226. } else {
  227. adv = dnet_mdio_read(dnet, MII_ADVERTISE);
  228. lpa = dnet_mdio_read(dnet, MII_LPA);
  229. media = mii_nway_result(lpa & adv);
  230. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  231. ? 1 : 0);
  232. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  233. /* 1000BaseT ethernet is not supported */
  234. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  235. netdev->name,
  236. speed ? "100" : "10",
  237. duplex ? "full" : "half",
  238. lpa);
  239. ctl_reg = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
  240. if (duplex)
  241. ctl_reg &= ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
  242. else
  243. ctl_reg |= DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
  244. dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
  245. return 0;
  246. }
  247. }
  248. static int dnet_init(struct eth_device *netdev, bd_t *bd)
  249. {
  250. struct dnet_device *dnet = to_dnet(netdev);
  251. u32 config;
  252. /*
  253. * dnet_halt should have been called at some point before now,
  254. * so we'll assume the controller is idle.
  255. */
  256. /* set hardware address */
  257. dnet_set_hwaddr(netdev);
  258. if (dnet_phy_init(dnet) < 0)
  259. return -1;
  260. /* flush rx/tx fifos */
  261. writel(DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
  262. &dnet->regs->SYS_CTL);
  263. udelay(1000);
  264. writel(0, &dnet->regs->SYS_CTL);
  265. config = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
  266. config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
  267. DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
  268. DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
  269. DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
  270. dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, config);
  271. /* Enable TX and RX */
  272. dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG,
  273. DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
  274. return 0;
  275. }
  276. static void dnet_halt(struct eth_device *netdev)
  277. {
  278. struct dnet_device *dnet = to_dnet(netdev);
  279. /* Disable TX and RX */
  280. dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG, 0);
  281. }
  282. int dnet_eth_initialize(int id, void *regs, unsigned int phy_addr)
  283. {
  284. struct dnet_device *dnet;
  285. struct eth_device *netdev;
  286. unsigned int dev_capa;
  287. dnet = malloc(sizeof(struct dnet_device));
  288. if (!dnet) {
  289. printf("Error: Failed to allocate memory for DNET%d\n", id);
  290. return -1;
  291. }
  292. memset(dnet, 0, sizeof(struct dnet_device));
  293. netdev = &dnet->netdev;
  294. dnet->regs = (struct dnet_registers *)regs;
  295. dnet->phy_addr = phy_addr;
  296. sprintf(netdev->name, "dnet%d", id);
  297. netdev->init = dnet_init;
  298. netdev->halt = dnet_halt;
  299. netdev->send = dnet_send;
  300. netdev->recv = dnet_recv;
  301. dev_capa = readl(&dnet->regs->VERCAPS) & 0xFFFF;
  302. debug("%s: has %smdio, %sirq, %sgigabit, %sdma \n", netdev->name,
  303. (dev_capa & DNET_HAS_MDIO) ? "" : "no ",
  304. (dev_capa & DNET_HAS_IRQ) ? "" : "no ",
  305. (dev_capa & DNET_HAS_GIGABIT) ? "" : "no ",
  306. (dev_capa & DNET_HAS_DMA) ? "" : "no ");
  307. eth_register(netdev);
  308. return 0;
  309. }