ll_temac_main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * Driver for Xilinx TEMAC Ethernet device
  3. *
  4. * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi
  5. * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net>
  6. * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
  7. *
  8. * This is a driver for the Xilinx ll_temac ipcore which is often used
  9. * in the Virtex and Spartan series of chips.
  10. *
  11. * Notes:
  12. * - The ll_temac hardware uses indirect access for many of the TEMAC
  13. * registers, include the MDIO bus. However, indirect access to MDIO
  14. * registers take considerably more clock cycles than to TEMAC registers.
  15. * MDIO accesses are long, so threads doing them should probably sleep
  16. * rather than busywait. However, since only one indirect access can be
  17. * in progress at any given time, that means that *all* indirect accesses
  18. * could end up sleeping (to wait for an MDIO access to complete).
  19. * Fortunately none of the indirect accesses are on the 'hot' path for tx
  20. * or rx, so this should be okay.
  21. *
  22. * TODO:
  23. * - Factor out locallink DMA code into separate driver
  24. * - Fix multicast assignment.
  25. * - Fix support for hardware checksumming.
  26. * - Testing. Lots and lots of testing.
  27. *
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/init.h>
  32. #include <linux/mii.h>
  33. #include <linux/module.h>
  34. #include <linux/mutex.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_mdio.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/of_address.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/tcp.h> /* needed for sizeof(tcphdr) */
  44. #include <linux/udp.h> /* needed for sizeof(udphdr) */
  45. #include <linux/phy.h>
  46. #include <linux/in.h>
  47. #include <linux/io.h>
  48. #include <linux/ip.h>
  49. #include <linux/slab.h>
  50. #include <linux/interrupt.h>
  51. #include "ll_temac.h"
  52. #define TX_BD_NUM 64
  53. #define RX_BD_NUM 128
  54. /* ---------------------------------------------------------------------
  55. * Low level register access functions
  56. */
  57. u32 temac_ior(struct temac_local *lp, int offset)
  58. {
  59. return in_be32((u32 *)(lp->regs + offset));
  60. }
  61. void temac_iow(struct temac_local *lp, int offset, u32 value)
  62. {
  63. out_be32((u32 *) (lp->regs + offset), value);
  64. }
  65. int temac_indirect_busywait(struct temac_local *lp)
  66. {
  67. long end = jiffies + 2;
  68. while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
  69. if (end - jiffies <= 0) {
  70. WARN_ON(1);
  71. return -ETIMEDOUT;
  72. }
  73. msleep(1);
  74. }
  75. return 0;
  76. }
  77. /**
  78. * temac_indirect_in32
  79. *
  80. * lp->indirect_mutex must be held when calling this function
  81. */
  82. u32 temac_indirect_in32(struct temac_local *lp, int reg)
  83. {
  84. u32 val;
  85. if (temac_indirect_busywait(lp))
  86. return -ETIMEDOUT;
  87. temac_iow(lp, XTE_CTL0_OFFSET, reg);
  88. if (temac_indirect_busywait(lp))
  89. return -ETIMEDOUT;
  90. val = temac_ior(lp, XTE_LSW0_OFFSET);
  91. return val;
  92. }
  93. /**
  94. * temac_indirect_out32
  95. *
  96. * lp->indirect_mutex must be held when calling this function
  97. */
  98. void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
  99. {
  100. if (temac_indirect_busywait(lp))
  101. return;
  102. temac_iow(lp, XTE_LSW0_OFFSET, value);
  103. temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
  104. }
  105. /**
  106. * temac_dma_in32 - Memory mapped DMA read, this function expects a
  107. * register input that is based on DCR word addresses which
  108. * are then converted to memory mapped byte addresses
  109. */
  110. static u32 temac_dma_in32(struct temac_local *lp, int reg)
  111. {
  112. return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
  113. }
  114. /**
  115. * temac_dma_out32 - Memory mapped DMA read, this function expects a
  116. * register input that is based on DCR word addresses which
  117. * are then converted to memory mapped byte addresses
  118. */
  119. static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
  120. {
  121. out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
  122. }
  123. /* DMA register access functions can be DCR based or memory mapped.
  124. * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
  125. * memory mapped.
  126. */
  127. #ifdef CONFIG_PPC_DCR
  128. /**
  129. * temac_dma_dcr_in32 - DCR based DMA read
  130. */
  131. static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
  132. {
  133. return dcr_read(lp->sdma_dcrs, reg);
  134. }
  135. /**
  136. * temac_dma_dcr_out32 - DCR based DMA write
  137. */
  138. static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
  139. {
  140. dcr_write(lp->sdma_dcrs, reg, value);
  141. }
  142. /**
  143. * temac_dcr_setup - If the DMA is DCR based, then setup the address and
  144. * I/O functions
  145. */
  146. static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
  147. struct device_node *np)
  148. {
  149. unsigned int dcrs;
  150. /* setup the dcr address mapping if it's in the device tree */
  151. dcrs = dcr_resource_start(np, 0);
  152. if (dcrs != 0) {
  153. lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  154. lp->dma_in = temac_dma_dcr_in;
  155. lp->dma_out = temac_dma_dcr_out;
  156. dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
  157. return 0;
  158. }
  159. /* no DCR in the device tree, indicate a failure */
  160. return -1;
  161. }
  162. #else
  163. /*
  164. * temac_dcr_setup - This is a stub for when DCR is not supported,
  165. * such as with MicroBlaze
  166. */
  167. static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
  168. struct device_node *np)
  169. {
  170. return -1;
  171. }
  172. #endif
  173. /**
  174. * * temac_dma_bd_release - Release buffer descriptor rings
  175. */
  176. static void temac_dma_bd_release(struct net_device *ndev)
  177. {
  178. struct temac_local *lp = netdev_priv(ndev);
  179. int i;
  180. for (i = 0; i < RX_BD_NUM; i++) {
  181. if (!lp->rx_skb[i])
  182. break;
  183. else {
  184. dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
  185. XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
  186. dev_kfree_skb(lp->rx_skb[i]);
  187. }
  188. }
  189. if (lp->rx_bd_v)
  190. dma_free_coherent(ndev->dev.parent,
  191. sizeof(*lp->rx_bd_v) * RX_BD_NUM,
  192. lp->rx_bd_v, lp->rx_bd_p);
  193. if (lp->tx_bd_v)
  194. dma_free_coherent(ndev->dev.parent,
  195. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  196. lp->tx_bd_v, lp->tx_bd_p);
  197. if (lp->rx_skb)
  198. kfree(lp->rx_skb);
  199. }
  200. /**
  201. * temac_dma_bd_init - Setup buffer descriptor rings
  202. */
  203. static int temac_dma_bd_init(struct net_device *ndev)
  204. {
  205. struct temac_local *lp = netdev_priv(ndev);
  206. struct sk_buff *skb;
  207. int i;
  208. lp->rx_skb = kzalloc(sizeof(*lp->rx_skb) * RX_BD_NUM, GFP_KERNEL);
  209. if (!lp->rx_skb) {
  210. dev_err(&ndev->dev,
  211. "can't allocate memory for DMA RX buffer\n");
  212. goto out;
  213. }
  214. /* allocate the tx and rx ring buffer descriptors. */
  215. /* returns a virtual address and a physical address. */
  216. lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  217. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  218. &lp->tx_bd_p, GFP_KERNEL);
  219. if (!lp->tx_bd_v) {
  220. dev_err(&ndev->dev,
  221. "unable to allocate DMA TX buffer descriptors");
  222. goto out;
  223. }
  224. lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  225. sizeof(*lp->rx_bd_v) * RX_BD_NUM,
  226. &lp->rx_bd_p, GFP_KERNEL);
  227. if (!lp->rx_bd_v) {
  228. dev_err(&ndev->dev,
  229. "unable to allocate DMA RX buffer descriptors");
  230. goto out;
  231. }
  232. memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
  233. for (i = 0; i < TX_BD_NUM; i++) {
  234. lp->tx_bd_v[i].next = lp->tx_bd_p +
  235. sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
  236. }
  237. memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
  238. for (i = 0; i < RX_BD_NUM; i++) {
  239. lp->rx_bd_v[i].next = lp->rx_bd_p +
  240. sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
  241. skb = netdev_alloc_skb_ip_align(ndev,
  242. XTE_MAX_JUMBO_FRAME_SIZE);
  243. if (skb == 0) {
  244. dev_err(&ndev->dev, "alloc_skb error %d\n", i);
  245. goto out;
  246. }
  247. lp->rx_skb[i] = skb;
  248. /* returns physical address of skb->data */
  249. lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
  250. skb->data,
  251. XTE_MAX_JUMBO_FRAME_SIZE,
  252. DMA_FROM_DEVICE);
  253. lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
  254. lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
  255. }
  256. lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
  257. CHNL_CTRL_IRQ_EN |
  258. CHNL_CTRL_IRQ_DLY_EN |
  259. CHNL_CTRL_IRQ_COAL_EN);
  260. /* 0x10220483 */
  261. /* 0x00100483 */
  262. lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
  263. CHNL_CTRL_IRQ_EN |
  264. CHNL_CTRL_IRQ_DLY_EN |
  265. CHNL_CTRL_IRQ_COAL_EN |
  266. CHNL_CTRL_IRQ_IOE);
  267. /* 0xff010283 */
  268. lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
  269. lp->dma_out(lp, RX_TAILDESC_PTR,
  270. lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  271. lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
  272. return 0;
  273. out:
  274. temac_dma_bd_release(ndev);
  275. return -ENOMEM;
  276. }
  277. /* ---------------------------------------------------------------------
  278. * net_device_ops
  279. */
  280. static int temac_set_mac_address(struct net_device *ndev, void *address)
  281. {
  282. struct temac_local *lp = netdev_priv(ndev);
  283. if (address)
  284. memcpy(ndev->dev_addr, address, ETH_ALEN);
  285. if (!is_valid_ether_addr(ndev->dev_addr))
  286. random_ether_addr(ndev->dev_addr);
  287. /* set up unicast MAC address filter set its mac address */
  288. mutex_lock(&lp->indirect_mutex);
  289. temac_indirect_out32(lp, XTE_UAW0_OFFSET,
  290. (ndev->dev_addr[0]) |
  291. (ndev->dev_addr[1] << 8) |
  292. (ndev->dev_addr[2] << 16) |
  293. (ndev->dev_addr[3] << 24));
  294. /* There are reserved bits in EUAW1
  295. * so don't affect them Set MAC bits [47:32] in EUAW1 */
  296. temac_indirect_out32(lp, XTE_UAW1_OFFSET,
  297. (ndev->dev_addr[4] & 0x000000ff) |
  298. (ndev->dev_addr[5] << 8));
  299. mutex_unlock(&lp->indirect_mutex);
  300. return 0;
  301. }
  302. static int netdev_set_mac_address(struct net_device *ndev, void *p)
  303. {
  304. struct sockaddr *addr = p;
  305. return temac_set_mac_address(ndev, addr->sa_data);
  306. }
  307. static void temac_set_multicast_list(struct net_device *ndev)
  308. {
  309. struct temac_local *lp = netdev_priv(ndev);
  310. u32 multi_addr_msw, multi_addr_lsw, val;
  311. int i;
  312. mutex_lock(&lp->indirect_mutex);
  313. if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
  314. netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
  315. /*
  316. * We must make the kernel realise we had to move
  317. * into promisc mode or we start all out war on
  318. * the cable. If it was a promisc request the
  319. * flag is already set. If not we assert it.
  320. */
  321. ndev->flags |= IFF_PROMISC;
  322. temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
  323. dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
  324. } else if (!netdev_mc_empty(ndev)) {
  325. struct netdev_hw_addr *ha;
  326. i = 0;
  327. netdev_for_each_mc_addr(ha, ndev) {
  328. if (i >= MULTICAST_CAM_TABLE_NUM)
  329. break;
  330. multi_addr_msw = ((ha->addr[3] << 24) |
  331. (ha->addr[2] << 16) |
  332. (ha->addr[1] << 8) |
  333. (ha->addr[0]));
  334. temac_indirect_out32(lp, XTE_MAW0_OFFSET,
  335. multi_addr_msw);
  336. multi_addr_lsw = ((ha->addr[5] << 8) |
  337. (ha->addr[4]) | (i << 16));
  338. temac_indirect_out32(lp, XTE_MAW1_OFFSET,
  339. multi_addr_lsw);
  340. i++;
  341. }
  342. } else {
  343. val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
  344. temac_indirect_out32(lp, XTE_AFM_OFFSET,
  345. val & ~XTE_AFM_EPPRM_MASK);
  346. temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
  347. temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
  348. dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
  349. }
  350. mutex_unlock(&lp->indirect_mutex);
  351. }
  352. struct temac_option {
  353. int flg;
  354. u32 opt;
  355. u32 reg;
  356. u32 m_or;
  357. u32 m_and;
  358. } temac_options[] = {
  359. /* Turn on jumbo packet support for both Rx and Tx */
  360. {
  361. .opt = XTE_OPTION_JUMBO,
  362. .reg = XTE_TXC_OFFSET,
  363. .m_or = XTE_TXC_TXJMBO_MASK,
  364. },
  365. {
  366. .opt = XTE_OPTION_JUMBO,
  367. .reg = XTE_RXC1_OFFSET,
  368. .m_or =XTE_RXC1_RXJMBO_MASK,
  369. },
  370. /* Turn on VLAN packet support for both Rx and Tx */
  371. {
  372. .opt = XTE_OPTION_VLAN,
  373. .reg = XTE_TXC_OFFSET,
  374. .m_or =XTE_TXC_TXVLAN_MASK,
  375. },
  376. {
  377. .opt = XTE_OPTION_VLAN,
  378. .reg = XTE_RXC1_OFFSET,
  379. .m_or =XTE_RXC1_RXVLAN_MASK,
  380. },
  381. /* Turn on FCS stripping on receive packets */
  382. {
  383. .opt = XTE_OPTION_FCS_STRIP,
  384. .reg = XTE_RXC1_OFFSET,
  385. .m_or =XTE_RXC1_RXFCS_MASK,
  386. },
  387. /* Turn on FCS insertion on transmit packets */
  388. {
  389. .opt = XTE_OPTION_FCS_INSERT,
  390. .reg = XTE_TXC_OFFSET,
  391. .m_or =XTE_TXC_TXFCS_MASK,
  392. },
  393. /* Turn on length/type field checking on receive packets */
  394. {
  395. .opt = XTE_OPTION_LENTYPE_ERR,
  396. .reg = XTE_RXC1_OFFSET,
  397. .m_or =XTE_RXC1_RXLT_MASK,
  398. },
  399. /* Turn on flow control */
  400. {
  401. .opt = XTE_OPTION_FLOW_CONTROL,
  402. .reg = XTE_FCC_OFFSET,
  403. .m_or =XTE_FCC_RXFLO_MASK,
  404. },
  405. /* Turn on flow control */
  406. {
  407. .opt = XTE_OPTION_FLOW_CONTROL,
  408. .reg = XTE_FCC_OFFSET,
  409. .m_or =XTE_FCC_TXFLO_MASK,
  410. },
  411. /* Turn on promiscuous frame filtering (all frames are received ) */
  412. {
  413. .opt = XTE_OPTION_PROMISC,
  414. .reg = XTE_AFM_OFFSET,
  415. .m_or =XTE_AFM_EPPRM_MASK,
  416. },
  417. /* Enable transmitter if not already enabled */
  418. {
  419. .opt = XTE_OPTION_TXEN,
  420. .reg = XTE_TXC_OFFSET,
  421. .m_or =XTE_TXC_TXEN_MASK,
  422. },
  423. /* Enable receiver? */
  424. {
  425. .opt = XTE_OPTION_RXEN,
  426. .reg = XTE_RXC1_OFFSET,
  427. .m_or =XTE_RXC1_RXEN_MASK,
  428. },
  429. {}
  430. };
  431. /**
  432. * temac_setoptions
  433. */
  434. static u32 temac_setoptions(struct net_device *ndev, u32 options)
  435. {
  436. struct temac_local *lp = netdev_priv(ndev);
  437. struct temac_option *tp = &temac_options[0];
  438. int reg;
  439. mutex_lock(&lp->indirect_mutex);
  440. while (tp->opt) {
  441. reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
  442. if (options & tp->opt)
  443. reg |= tp->m_or;
  444. temac_indirect_out32(lp, tp->reg, reg);
  445. tp++;
  446. }
  447. lp->options |= options;
  448. mutex_unlock(&lp->indirect_mutex);
  449. return 0;
  450. }
  451. /* Initialize temac */
  452. static void temac_device_reset(struct net_device *ndev)
  453. {
  454. struct temac_local *lp = netdev_priv(ndev);
  455. u32 timeout;
  456. u32 val;
  457. /* Perform a software reset */
  458. /* 0x300 host enable bit ? */
  459. /* reset PHY through control register ?:1 */
  460. dev_dbg(&ndev->dev, "%s()\n", __func__);
  461. mutex_lock(&lp->indirect_mutex);
  462. /* Reset the receiver and wait for it to finish reset */
  463. temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
  464. timeout = 1000;
  465. while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
  466. udelay(1);
  467. if (--timeout == 0) {
  468. dev_err(&ndev->dev,
  469. "temac_device_reset RX reset timeout!!\n");
  470. break;
  471. }
  472. }
  473. /* Reset the transmitter and wait for it to finish reset */
  474. temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
  475. timeout = 1000;
  476. while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
  477. udelay(1);
  478. if (--timeout == 0) {
  479. dev_err(&ndev->dev,
  480. "temac_device_reset TX reset timeout!!\n");
  481. break;
  482. }
  483. }
  484. /* Disable the receiver */
  485. val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
  486. temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
  487. /* Reset Local Link (DMA) */
  488. lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
  489. timeout = 1000;
  490. while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
  491. udelay(1);
  492. if (--timeout == 0) {
  493. dev_err(&ndev->dev,
  494. "temac_device_reset DMA reset timeout!!\n");
  495. break;
  496. }
  497. }
  498. lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
  499. if (temac_dma_bd_init(ndev)) {
  500. dev_err(&ndev->dev,
  501. "temac_device_reset descriptor allocation failed\n");
  502. }
  503. temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
  504. temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
  505. temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
  506. temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
  507. mutex_unlock(&lp->indirect_mutex);
  508. /* Sync default options with HW
  509. * but leave receiver and transmitter disabled. */
  510. temac_setoptions(ndev,
  511. lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
  512. temac_set_mac_address(ndev, NULL);
  513. /* Set address filter table */
  514. temac_set_multicast_list(ndev);
  515. if (temac_setoptions(ndev, lp->options))
  516. dev_err(&ndev->dev, "Error setting TEMAC options\n");
  517. /* Init Driver variable */
  518. ndev->trans_start = jiffies; /* prevent tx timeout */
  519. }
  520. void temac_adjust_link(struct net_device *ndev)
  521. {
  522. struct temac_local *lp = netdev_priv(ndev);
  523. struct phy_device *phy = lp->phy_dev;
  524. u32 mii_speed;
  525. int link_state;
  526. /* hash together the state values to decide if something has changed */
  527. link_state = phy->speed | (phy->duplex << 1) | phy->link;
  528. mutex_lock(&lp->indirect_mutex);
  529. if (lp->last_link != link_state) {
  530. mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
  531. mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
  532. switch (phy->speed) {
  533. case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
  534. case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
  535. case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
  536. }
  537. /* Write new speed setting out to TEMAC */
  538. temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
  539. lp->last_link = link_state;
  540. phy_print_status(phy);
  541. }
  542. mutex_unlock(&lp->indirect_mutex);
  543. }
  544. static void temac_start_xmit_done(struct net_device *ndev)
  545. {
  546. struct temac_local *lp = netdev_priv(ndev);
  547. struct cdmac_bd *cur_p;
  548. unsigned int stat = 0;
  549. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  550. stat = cur_p->app0;
  551. while (stat & STS_CTRL_APP0_CMPLT) {
  552. dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
  553. DMA_TO_DEVICE);
  554. if (cur_p->app4)
  555. dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
  556. cur_p->app0 = 0;
  557. cur_p->app1 = 0;
  558. cur_p->app2 = 0;
  559. cur_p->app3 = 0;
  560. cur_p->app4 = 0;
  561. ndev->stats.tx_packets++;
  562. ndev->stats.tx_bytes += cur_p->len;
  563. lp->tx_bd_ci++;
  564. if (lp->tx_bd_ci >= TX_BD_NUM)
  565. lp->tx_bd_ci = 0;
  566. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  567. stat = cur_p->app0;
  568. }
  569. netif_wake_queue(ndev);
  570. }
  571. static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
  572. {
  573. struct cdmac_bd *cur_p;
  574. int tail;
  575. tail = lp->tx_bd_tail;
  576. cur_p = &lp->tx_bd_v[tail];
  577. do {
  578. if (cur_p->app0)
  579. return NETDEV_TX_BUSY;
  580. tail++;
  581. if (tail >= TX_BD_NUM)
  582. tail = 0;
  583. cur_p = &lp->tx_bd_v[tail];
  584. num_frag--;
  585. } while (num_frag >= 0);
  586. return 0;
  587. }
  588. static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  589. {
  590. struct temac_local *lp = netdev_priv(ndev);
  591. struct cdmac_bd *cur_p;
  592. dma_addr_t start_p, tail_p;
  593. int ii;
  594. unsigned long num_frag;
  595. skb_frag_t *frag;
  596. num_frag = skb_shinfo(skb)->nr_frags;
  597. frag = &skb_shinfo(skb)->frags[0];
  598. start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  599. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  600. if (temac_check_tx_bd_space(lp, num_frag)) {
  601. if (!netif_queue_stopped(ndev)) {
  602. netif_stop_queue(ndev);
  603. return NETDEV_TX_BUSY;
  604. }
  605. return NETDEV_TX_BUSY;
  606. }
  607. cur_p->app0 = 0;
  608. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  609. unsigned int csum_start_off = skb_checksum_start_offset(skb);
  610. unsigned int csum_index_off = csum_start_off + skb->csum_offset;
  611. cur_p->app0 |= 1; /* TX Checksum Enabled */
  612. cur_p->app1 = (csum_start_off << 16) | csum_index_off;
  613. cur_p->app2 = 0; /* initial checksum seed */
  614. }
  615. cur_p->app0 |= STS_CTRL_APP0_SOP;
  616. cur_p->len = skb_headlen(skb);
  617. cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
  618. DMA_TO_DEVICE);
  619. cur_p->app4 = (unsigned long)skb;
  620. for (ii = 0; ii < num_frag; ii++) {
  621. lp->tx_bd_tail++;
  622. if (lp->tx_bd_tail >= TX_BD_NUM)
  623. lp->tx_bd_tail = 0;
  624. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  625. cur_p->phys = dma_map_single(ndev->dev.parent,
  626. (void *)page_address(frag->page) +
  627. frag->page_offset,
  628. frag->size, DMA_TO_DEVICE);
  629. cur_p->len = frag->size;
  630. cur_p->app0 = 0;
  631. frag++;
  632. }
  633. cur_p->app0 |= STS_CTRL_APP0_EOP;
  634. tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  635. lp->tx_bd_tail++;
  636. if (lp->tx_bd_tail >= TX_BD_NUM)
  637. lp->tx_bd_tail = 0;
  638. skb_tx_timestamp(skb);
  639. /* Kick off the transfer */
  640. lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
  641. return NETDEV_TX_OK;
  642. }
  643. static void ll_temac_recv(struct net_device *ndev)
  644. {
  645. struct temac_local *lp = netdev_priv(ndev);
  646. struct sk_buff *skb, *new_skb;
  647. unsigned int bdstat;
  648. struct cdmac_bd *cur_p;
  649. dma_addr_t tail_p;
  650. int length;
  651. unsigned long flags;
  652. spin_lock_irqsave(&lp->rx_lock, flags);
  653. tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
  654. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  655. bdstat = cur_p->app0;
  656. while ((bdstat & STS_CTRL_APP0_CMPLT)) {
  657. skb = lp->rx_skb[lp->rx_bd_ci];
  658. length = cur_p->app4 & 0x3FFF;
  659. dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
  660. DMA_FROM_DEVICE);
  661. skb_put(skb, length);
  662. skb->dev = ndev;
  663. skb->protocol = eth_type_trans(skb, ndev);
  664. skb_checksum_none_assert(skb);
  665. /* if we're doing rx csum offload, set it up */
  666. if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
  667. (skb->protocol == __constant_htons(ETH_P_IP)) &&
  668. (skb->len > 64)) {
  669. skb->csum = cur_p->app3 & 0xFFFF;
  670. skb->ip_summed = CHECKSUM_COMPLETE;
  671. }
  672. if (!skb_defer_rx_timestamp(skb))
  673. netif_rx(skb);
  674. ndev->stats.rx_packets++;
  675. ndev->stats.rx_bytes += length;
  676. new_skb = netdev_alloc_skb_ip_align(ndev,
  677. XTE_MAX_JUMBO_FRAME_SIZE);
  678. if (new_skb == 0) {
  679. dev_err(&ndev->dev, "no memory for new sk_buff\n");
  680. spin_unlock_irqrestore(&lp->rx_lock, flags);
  681. return;
  682. }
  683. cur_p->app0 = STS_CTRL_APP0_IRQONEND;
  684. cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
  685. XTE_MAX_JUMBO_FRAME_SIZE,
  686. DMA_FROM_DEVICE);
  687. cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
  688. lp->rx_skb[lp->rx_bd_ci] = new_skb;
  689. lp->rx_bd_ci++;
  690. if (lp->rx_bd_ci >= RX_BD_NUM)
  691. lp->rx_bd_ci = 0;
  692. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  693. bdstat = cur_p->app0;
  694. }
  695. lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
  696. spin_unlock_irqrestore(&lp->rx_lock, flags);
  697. }
  698. static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
  699. {
  700. struct net_device *ndev = _ndev;
  701. struct temac_local *lp = netdev_priv(ndev);
  702. unsigned int status;
  703. status = lp->dma_in(lp, TX_IRQ_REG);
  704. lp->dma_out(lp, TX_IRQ_REG, status);
  705. if (status & (IRQ_COAL | IRQ_DLY))
  706. temac_start_xmit_done(lp->ndev);
  707. if (status & 0x080)
  708. dev_err(&ndev->dev, "DMA error 0x%x\n", status);
  709. return IRQ_HANDLED;
  710. }
  711. static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
  712. {
  713. struct net_device *ndev = _ndev;
  714. struct temac_local *lp = netdev_priv(ndev);
  715. unsigned int status;
  716. /* Read and clear the status registers */
  717. status = lp->dma_in(lp, RX_IRQ_REG);
  718. lp->dma_out(lp, RX_IRQ_REG, status);
  719. if (status & (IRQ_COAL | IRQ_DLY))
  720. ll_temac_recv(lp->ndev);
  721. return IRQ_HANDLED;
  722. }
  723. static int temac_open(struct net_device *ndev)
  724. {
  725. struct temac_local *lp = netdev_priv(ndev);
  726. int rc;
  727. dev_dbg(&ndev->dev, "temac_open()\n");
  728. if (lp->phy_node) {
  729. lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
  730. temac_adjust_link, 0, 0);
  731. if (!lp->phy_dev) {
  732. dev_err(lp->dev, "of_phy_connect() failed\n");
  733. return -ENODEV;
  734. }
  735. phy_start(lp->phy_dev);
  736. }
  737. rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
  738. if (rc)
  739. goto err_tx_irq;
  740. rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
  741. if (rc)
  742. goto err_rx_irq;
  743. temac_device_reset(ndev);
  744. return 0;
  745. err_rx_irq:
  746. free_irq(lp->tx_irq, ndev);
  747. err_tx_irq:
  748. if (lp->phy_dev)
  749. phy_disconnect(lp->phy_dev);
  750. lp->phy_dev = NULL;
  751. dev_err(lp->dev, "request_irq() failed\n");
  752. return rc;
  753. }
  754. static int temac_stop(struct net_device *ndev)
  755. {
  756. struct temac_local *lp = netdev_priv(ndev);
  757. dev_dbg(&ndev->dev, "temac_close()\n");
  758. free_irq(lp->tx_irq, ndev);
  759. free_irq(lp->rx_irq, ndev);
  760. if (lp->phy_dev)
  761. phy_disconnect(lp->phy_dev);
  762. lp->phy_dev = NULL;
  763. temac_dma_bd_release(ndev);
  764. return 0;
  765. }
  766. #ifdef CONFIG_NET_POLL_CONTROLLER
  767. static void
  768. temac_poll_controller(struct net_device *ndev)
  769. {
  770. struct temac_local *lp = netdev_priv(ndev);
  771. disable_irq(lp->tx_irq);
  772. disable_irq(lp->rx_irq);
  773. ll_temac_rx_irq(lp->tx_irq, ndev);
  774. ll_temac_tx_irq(lp->rx_irq, ndev);
  775. enable_irq(lp->tx_irq);
  776. enable_irq(lp->rx_irq);
  777. }
  778. #endif
  779. static const struct net_device_ops temac_netdev_ops = {
  780. .ndo_open = temac_open,
  781. .ndo_stop = temac_stop,
  782. .ndo_start_xmit = temac_start_xmit,
  783. .ndo_set_mac_address = netdev_set_mac_address,
  784. .ndo_validate_addr = eth_validate_addr,
  785. //.ndo_set_multicast_list = temac_set_multicast_list,
  786. #ifdef CONFIG_NET_POLL_CONTROLLER
  787. .ndo_poll_controller = temac_poll_controller,
  788. #endif
  789. };
  790. /* ---------------------------------------------------------------------
  791. * SYSFS device attributes
  792. */
  793. static ssize_t temac_show_llink_regs(struct device *dev,
  794. struct device_attribute *attr, char *buf)
  795. {
  796. struct net_device *ndev = dev_get_drvdata(dev);
  797. struct temac_local *lp = netdev_priv(ndev);
  798. int i, len = 0;
  799. for (i = 0; i < 0x11; i++)
  800. len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
  801. (i % 8) == 7 ? "\n" : " ");
  802. len += sprintf(buf + len, "\n");
  803. return len;
  804. }
  805. static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
  806. static struct attribute *temac_device_attrs[] = {
  807. &dev_attr_llink_regs.attr,
  808. NULL,
  809. };
  810. static const struct attribute_group temac_attr_group = {
  811. .attrs = temac_device_attrs,
  812. };
  813. static int __devinit temac_of_probe(struct platform_device *op)
  814. {
  815. struct device_node *np;
  816. struct temac_local *lp;
  817. struct net_device *ndev;
  818. const void *addr;
  819. __be32 *p;
  820. int size, rc = 0;
  821. /* Init network device structure */
  822. ndev = alloc_etherdev(sizeof(*lp));
  823. if (!ndev) {
  824. dev_err(&op->dev, "could not allocate device.\n");
  825. return -ENOMEM;
  826. }
  827. ether_setup(ndev);
  828. dev_set_drvdata(&op->dev, ndev);
  829. SET_NETDEV_DEV(ndev, &op->dev);
  830. ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
  831. ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
  832. ndev->netdev_ops = &temac_netdev_ops;
  833. #if 0
  834. ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
  835. ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
  836. ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
  837. ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
  838. ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
  839. ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
  840. ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
  841. ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
  842. ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
  843. ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
  844. ndev->features |= NETIF_F_LRO; /* large receive offload */
  845. #endif
  846. /* setup temac private info structure */
  847. lp = netdev_priv(ndev);
  848. lp->ndev = ndev;
  849. lp->dev = &op->dev;
  850. lp->options = XTE_OPTION_DEFAULTS;
  851. spin_lock_init(&lp->rx_lock);
  852. mutex_init(&lp->indirect_mutex);
  853. /* map device registers */
  854. lp->regs = of_iomap(op->dev.of_node, 0);
  855. if (!lp->regs) {
  856. dev_err(&op->dev, "could not map temac regs.\n");
  857. goto nodev;
  858. }
  859. /* Setup checksum offload, but default to off if not specified */
  860. lp->temac_features = 0;
  861. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
  862. if (p && be32_to_cpu(*p)) {
  863. lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
  864. /* Can checksum TCP/UDP over IPv4. */
  865. ndev->features |= NETIF_F_IP_CSUM;
  866. }
  867. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
  868. if (p && be32_to_cpu(*p))
  869. lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
  870. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  871. np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
  872. if (!np) {
  873. dev_err(&op->dev, "could not find DMA node\n");
  874. goto err_iounmap;
  875. }
  876. /* Setup the DMA register accesses, could be DCR or memory mapped */
  877. if (temac_dcr_setup(lp, op, np)) {
  878. /* no DCR in the device tree, try non-DCR */
  879. lp->sdma_regs = of_iomap(np, 0);
  880. if (lp->sdma_regs) {
  881. lp->dma_in = temac_dma_in32;
  882. lp->dma_out = temac_dma_out32;
  883. dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
  884. } else {
  885. dev_err(&op->dev, "unable to map DMA registers\n");
  886. of_node_put(np);
  887. goto err_iounmap;
  888. }
  889. }
  890. lp->rx_irq = irq_of_parse_and_map(np, 0);
  891. lp->tx_irq = irq_of_parse_and_map(np, 1);
  892. of_node_put(np); /* Finished with the DMA node; drop the reference */
  893. if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) {
  894. dev_err(&op->dev, "could not determine irqs\n");
  895. rc = -ENOMEM;
  896. goto err_iounmap_2;
  897. }
  898. /* Retrieve the MAC address */
  899. addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
  900. if ((!addr) || (size != 6)) {
  901. dev_err(&op->dev, "could not find MAC address\n");
  902. rc = -ENODEV;
  903. goto err_iounmap_2;
  904. }
  905. temac_set_mac_address(ndev, (void *)addr);
  906. rc = temac_mdio_setup(lp, op->dev.of_node);
  907. if (rc)
  908. dev_warn(&op->dev, "error registering MDIO bus\n");
  909. lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
  910. if (lp->phy_node)
  911. dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
  912. /* Add the device attributes */
  913. rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
  914. if (rc) {
  915. dev_err(lp->dev, "Error creating sysfs files\n");
  916. goto err_iounmap_2;
  917. }
  918. rc = register_netdev(lp->ndev);
  919. if (rc) {
  920. dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
  921. goto err_register_ndev;
  922. }
  923. return 0;
  924. err_register_ndev:
  925. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  926. err_iounmap_2:
  927. if (lp->sdma_regs)
  928. iounmap(lp->sdma_regs);
  929. err_iounmap:
  930. iounmap(lp->regs);
  931. nodev:
  932. free_netdev(ndev);
  933. ndev = NULL;
  934. return rc;
  935. }
  936. static int __devexit temac_of_remove(struct platform_device *op)
  937. {
  938. struct net_device *ndev = dev_get_drvdata(&op->dev);
  939. struct temac_local *lp = netdev_priv(ndev);
  940. temac_mdio_teardown(lp);
  941. unregister_netdev(ndev);
  942. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  943. if (lp->phy_node)
  944. of_node_put(lp->phy_node);
  945. lp->phy_node = NULL;
  946. dev_set_drvdata(&op->dev, NULL);
  947. iounmap(lp->regs);
  948. if (lp->sdma_regs)
  949. iounmap(lp->sdma_regs);
  950. free_netdev(ndev);
  951. return 0;
  952. }
  953. static struct of_device_id temac_of_match[] __devinitdata = {
  954. { .compatible = "xlnx,xps-ll-temac-1.01.b", },
  955. { .compatible = "xlnx,xps-ll-temac-2.00.a", },
  956. { .compatible = "xlnx,xps-ll-temac-2.02.a", },
  957. { .compatible = "xlnx,xps-ll-temac-2.03.a", },
  958. {},
  959. };
  960. MODULE_DEVICE_TABLE(of, temac_of_match);
  961. static struct platform_driver temac_of_driver = {
  962. .probe = temac_of_probe,
  963. .remove = __devexit_p(temac_of_remove),
  964. .driver = {
  965. .owner = THIS_MODULE,
  966. .name = "xilinx_temac",
  967. .of_match_table = temac_of_match,
  968. },
  969. };
  970. static int __init temac_init(void)
  971. {
  972. return platform_driver_register(&temac_of_driver);
  973. }
  974. module_init(temac_init);
  975. static void __exit temac_exit(void)
  976. {
  977. platform_driver_unregister(&temac_of_driver);
  978. }
  979. module_exit(temac_exit);
  980. MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
  981. MODULE_AUTHOR("Yoshio Kashiwagi");
  982. MODULE_LICENSE("GPL");