ll_temac_main.c 29 KB

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