ll_temac_main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. /* Kick off the transfer */
  639. lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
  640. return NETDEV_TX_OK;
  641. }
  642. static void ll_temac_recv(struct net_device *ndev)
  643. {
  644. struct temac_local *lp = netdev_priv(ndev);
  645. struct sk_buff *skb, *new_skb;
  646. unsigned int bdstat;
  647. struct cdmac_bd *cur_p;
  648. dma_addr_t tail_p;
  649. int length;
  650. unsigned long flags;
  651. spin_lock_irqsave(&lp->rx_lock, flags);
  652. tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
  653. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  654. bdstat = cur_p->app0;
  655. while ((bdstat & STS_CTRL_APP0_CMPLT)) {
  656. skb = lp->rx_skb[lp->rx_bd_ci];
  657. length = cur_p->app4 & 0x3FFF;
  658. dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
  659. DMA_FROM_DEVICE);
  660. skb_put(skb, length);
  661. skb->dev = ndev;
  662. skb->protocol = eth_type_trans(skb, ndev);
  663. skb_checksum_none_assert(skb);
  664. /* if we're doing rx csum offload, set it up */
  665. if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
  666. (skb->protocol == __constant_htons(ETH_P_IP)) &&
  667. (skb->len > 64)) {
  668. skb->csum = cur_p->app3 & 0xFFFF;
  669. skb->ip_summed = CHECKSUM_COMPLETE;
  670. }
  671. netif_rx(skb);
  672. ndev->stats.rx_packets++;
  673. ndev->stats.rx_bytes += length;
  674. new_skb = netdev_alloc_skb_ip_align(ndev,
  675. XTE_MAX_JUMBO_FRAME_SIZE);
  676. if (new_skb == 0) {
  677. dev_err(&ndev->dev, "no memory for new sk_buff\n");
  678. spin_unlock_irqrestore(&lp->rx_lock, flags);
  679. return;
  680. }
  681. cur_p->app0 = STS_CTRL_APP0_IRQONEND;
  682. cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
  683. XTE_MAX_JUMBO_FRAME_SIZE,
  684. DMA_FROM_DEVICE);
  685. cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
  686. lp->rx_skb[lp->rx_bd_ci] = new_skb;
  687. lp->rx_bd_ci++;
  688. if (lp->rx_bd_ci >= RX_BD_NUM)
  689. lp->rx_bd_ci = 0;
  690. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  691. bdstat = cur_p->app0;
  692. }
  693. lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
  694. spin_unlock_irqrestore(&lp->rx_lock, flags);
  695. }
  696. static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
  697. {
  698. struct net_device *ndev = _ndev;
  699. struct temac_local *lp = netdev_priv(ndev);
  700. unsigned int status;
  701. status = lp->dma_in(lp, TX_IRQ_REG);
  702. lp->dma_out(lp, TX_IRQ_REG, status);
  703. if (status & (IRQ_COAL | IRQ_DLY))
  704. temac_start_xmit_done(lp->ndev);
  705. if (status & 0x080)
  706. dev_err(&ndev->dev, "DMA error 0x%x\n", status);
  707. return IRQ_HANDLED;
  708. }
  709. static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
  710. {
  711. struct net_device *ndev = _ndev;
  712. struct temac_local *lp = netdev_priv(ndev);
  713. unsigned int status;
  714. /* Read and clear the status registers */
  715. status = lp->dma_in(lp, RX_IRQ_REG);
  716. lp->dma_out(lp, RX_IRQ_REG, status);
  717. if (status & (IRQ_COAL | IRQ_DLY))
  718. ll_temac_recv(lp->ndev);
  719. return IRQ_HANDLED;
  720. }
  721. static int temac_open(struct net_device *ndev)
  722. {
  723. struct temac_local *lp = netdev_priv(ndev);
  724. int rc;
  725. dev_dbg(&ndev->dev, "temac_open()\n");
  726. if (lp->phy_node) {
  727. lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
  728. temac_adjust_link, 0, 0);
  729. if (!lp->phy_dev) {
  730. dev_err(lp->dev, "of_phy_connect() failed\n");
  731. return -ENODEV;
  732. }
  733. phy_start(lp->phy_dev);
  734. }
  735. rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
  736. if (rc)
  737. goto err_tx_irq;
  738. rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
  739. if (rc)
  740. goto err_rx_irq;
  741. temac_device_reset(ndev);
  742. return 0;
  743. err_rx_irq:
  744. free_irq(lp->tx_irq, ndev);
  745. err_tx_irq:
  746. if (lp->phy_dev)
  747. phy_disconnect(lp->phy_dev);
  748. lp->phy_dev = NULL;
  749. dev_err(lp->dev, "request_irq() failed\n");
  750. return rc;
  751. }
  752. static int temac_stop(struct net_device *ndev)
  753. {
  754. struct temac_local *lp = netdev_priv(ndev);
  755. dev_dbg(&ndev->dev, "temac_close()\n");
  756. free_irq(lp->tx_irq, ndev);
  757. free_irq(lp->rx_irq, ndev);
  758. if (lp->phy_dev)
  759. phy_disconnect(lp->phy_dev);
  760. lp->phy_dev = NULL;
  761. temac_dma_bd_release(ndev);
  762. return 0;
  763. }
  764. #ifdef CONFIG_NET_POLL_CONTROLLER
  765. static void
  766. temac_poll_controller(struct net_device *ndev)
  767. {
  768. struct temac_local *lp = netdev_priv(ndev);
  769. disable_irq(lp->tx_irq);
  770. disable_irq(lp->rx_irq);
  771. ll_temac_rx_irq(lp->tx_irq, ndev);
  772. ll_temac_tx_irq(lp->rx_irq, ndev);
  773. enable_irq(lp->tx_irq);
  774. enable_irq(lp->rx_irq);
  775. }
  776. #endif
  777. static const struct net_device_ops temac_netdev_ops = {
  778. .ndo_open = temac_open,
  779. .ndo_stop = temac_stop,
  780. .ndo_start_xmit = temac_start_xmit,
  781. .ndo_set_mac_address = netdev_set_mac_address,
  782. .ndo_validate_addr = eth_validate_addr,
  783. //.ndo_set_multicast_list = temac_set_multicast_list,
  784. #ifdef CONFIG_NET_POLL_CONTROLLER
  785. .ndo_poll_controller = temac_poll_controller,
  786. #endif
  787. };
  788. /* ---------------------------------------------------------------------
  789. * SYSFS device attributes
  790. */
  791. static ssize_t temac_show_llink_regs(struct device *dev,
  792. struct device_attribute *attr, char *buf)
  793. {
  794. struct net_device *ndev = dev_get_drvdata(dev);
  795. struct temac_local *lp = netdev_priv(ndev);
  796. int i, len = 0;
  797. for (i = 0; i < 0x11; i++)
  798. len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
  799. (i % 8) == 7 ? "\n" : " ");
  800. len += sprintf(buf + len, "\n");
  801. return len;
  802. }
  803. static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
  804. static struct attribute *temac_device_attrs[] = {
  805. &dev_attr_llink_regs.attr,
  806. NULL,
  807. };
  808. static const struct attribute_group temac_attr_group = {
  809. .attrs = temac_device_attrs,
  810. };
  811. static int __devinit temac_of_probe(struct platform_device *op)
  812. {
  813. struct device_node *np;
  814. struct temac_local *lp;
  815. struct net_device *ndev;
  816. const void *addr;
  817. __be32 *p;
  818. int size, rc = 0;
  819. /* Init network device structure */
  820. ndev = alloc_etherdev(sizeof(*lp));
  821. if (!ndev) {
  822. dev_err(&op->dev, "could not allocate device.\n");
  823. return -ENOMEM;
  824. }
  825. ether_setup(ndev);
  826. dev_set_drvdata(&op->dev, ndev);
  827. SET_NETDEV_DEV(ndev, &op->dev);
  828. ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
  829. ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
  830. ndev->netdev_ops = &temac_netdev_ops;
  831. #if 0
  832. ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
  833. ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
  834. ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
  835. ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
  836. ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
  837. ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
  838. ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
  839. ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
  840. ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
  841. ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
  842. ndev->features |= NETIF_F_LRO; /* large receive offload */
  843. #endif
  844. /* setup temac private info structure */
  845. lp = netdev_priv(ndev);
  846. lp->ndev = ndev;
  847. lp->dev = &op->dev;
  848. lp->options = XTE_OPTION_DEFAULTS;
  849. spin_lock_init(&lp->rx_lock);
  850. mutex_init(&lp->indirect_mutex);
  851. /* map device registers */
  852. lp->regs = of_iomap(op->dev.of_node, 0);
  853. if (!lp->regs) {
  854. dev_err(&op->dev, "could not map temac regs.\n");
  855. goto nodev;
  856. }
  857. /* Setup checksum offload, but default to off if not specified */
  858. lp->temac_features = 0;
  859. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
  860. if (p && be32_to_cpu(*p)) {
  861. lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
  862. /* Can checksum TCP/UDP over IPv4. */
  863. ndev->features |= NETIF_F_IP_CSUM;
  864. }
  865. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
  866. if (p && be32_to_cpu(*p))
  867. lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
  868. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  869. np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
  870. if (!np) {
  871. dev_err(&op->dev, "could not find DMA node\n");
  872. goto err_iounmap;
  873. }
  874. /* Setup the DMA register accesses, could be DCR or memory mapped */
  875. if (temac_dcr_setup(lp, op, np)) {
  876. /* no DCR in the device tree, try non-DCR */
  877. lp->sdma_regs = of_iomap(np, 0);
  878. if (lp->sdma_regs) {
  879. lp->dma_in = temac_dma_in32;
  880. lp->dma_out = temac_dma_out32;
  881. dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
  882. } else {
  883. dev_err(&op->dev, "unable to map DMA registers\n");
  884. of_node_put(np);
  885. goto err_iounmap;
  886. }
  887. }
  888. lp->rx_irq = irq_of_parse_and_map(np, 0);
  889. lp->tx_irq = irq_of_parse_and_map(np, 1);
  890. of_node_put(np); /* Finished with the DMA node; drop the reference */
  891. if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) {
  892. dev_err(&op->dev, "could not determine irqs\n");
  893. rc = -ENOMEM;
  894. goto err_iounmap_2;
  895. }
  896. /* Retrieve the MAC address */
  897. addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
  898. if ((!addr) || (size != 6)) {
  899. dev_err(&op->dev, "could not find MAC address\n");
  900. rc = -ENODEV;
  901. goto err_iounmap_2;
  902. }
  903. temac_set_mac_address(ndev, (void *)addr);
  904. rc = temac_mdio_setup(lp, op->dev.of_node);
  905. if (rc)
  906. dev_warn(&op->dev, "error registering MDIO bus\n");
  907. lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
  908. if (lp->phy_node)
  909. dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
  910. /* Add the device attributes */
  911. rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
  912. if (rc) {
  913. dev_err(lp->dev, "Error creating sysfs files\n");
  914. goto err_iounmap_2;
  915. }
  916. rc = register_netdev(lp->ndev);
  917. if (rc) {
  918. dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
  919. goto err_register_ndev;
  920. }
  921. return 0;
  922. err_register_ndev:
  923. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  924. err_iounmap_2:
  925. if (lp->sdma_regs)
  926. iounmap(lp->sdma_regs);
  927. err_iounmap:
  928. iounmap(lp->regs);
  929. nodev:
  930. free_netdev(ndev);
  931. ndev = NULL;
  932. return rc;
  933. }
  934. static int __devexit temac_of_remove(struct platform_device *op)
  935. {
  936. struct net_device *ndev = dev_get_drvdata(&op->dev);
  937. struct temac_local *lp = netdev_priv(ndev);
  938. temac_mdio_teardown(lp);
  939. unregister_netdev(ndev);
  940. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  941. if (lp->phy_node)
  942. of_node_put(lp->phy_node);
  943. lp->phy_node = NULL;
  944. dev_set_drvdata(&op->dev, NULL);
  945. iounmap(lp->regs);
  946. if (lp->sdma_regs)
  947. iounmap(lp->sdma_regs);
  948. free_netdev(ndev);
  949. return 0;
  950. }
  951. static struct of_device_id temac_of_match[] __devinitdata = {
  952. { .compatible = "xlnx,xps-ll-temac-1.01.b", },
  953. { .compatible = "xlnx,xps-ll-temac-2.00.a", },
  954. { .compatible = "xlnx,xps-ll-temac-2.02.a", },
  955. { .compatible = "xlnx,xps-ll-temac-2.03.a", },
  956. {},
  957. };
  958. MODULE_DEVICE_TABLE(of, temac_of_match);
  959. static struct platform_driver temac_of_driver = {
  960. .probe = temac_of_probe,
  961. .remove = __devexit_p(temac_of_remove),
  962. .driver = {
  963. .owner = THIS_MODULE,
  964. .name = "xilinx_temac",
  965. .of_match_table = temac_of_match,
  966. },
  967. };
  968. static int __init temac_init(void)
  969. {
  970. return platform_driver_register(&temac_of_driver);
  971. }
  972. module_init(temac_init);
  973. static void __exit temac_exit(void)
  974. {
  975. platform_driver_unregister(&temac_of_driver);
  976. }
  977. module_exit(temac_exit);
  978. MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
  979. MODULE_AUTHOR("Yoshio Kashiwagi");
  980. MODULE_LICENSE("GPL");