ll_temac_main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  592. {
  593. struct temac_local *lp = netdev_priv(ndev);
  594. struct cdmac_bd *cur_p;
  595. dma_addr_t start_p, tail_p;
  596. int ii;
  597. unsigned long num_frag;
  598. skb_frag_t *frag;
  599. num_frag = skb_shinfo(skb)->nr_frags;
  600. frag = &skb_shinfo(skb)->frags[0];
  601. start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  602. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  603. if (temac_check_tx_bd_space(lp, num_frag)) {
  604. if (!netif_queue_stopped(ndev))
  605. netif_stop_queue(ndev);
  606. return NETDEV_TX_BUSY;
  607. }
  608. cur_p->app0 = 0;
  609. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  610. unsigned int csum_start_off = skb_checksum_start_offset(skb);
  611. unsigned int csum_index_off = csum_start_off + skb->csum_offset;
  612. cur_p->app0 |= 1; /* TX Checksum Enabled */
  613. cur_p->app1 = (csum_start_off << 16) | csum_index_off;
  614. cur_p->app2 = 0; /* initial checksum seed */
  615. }
  616. cur_p->app0 |= STS_CTRL_APP0_SOP;
  617. cur_p->len = skb_headlen(skb);
  618. cur_p->phys = dma_map_single(ndev->dev.parent, skb->data,
  619. skb_headlen(skb), DMA_TO_DEVICE);
  620. cur_p->app4 = (unsigned long)skb;
  621. for (ii = 0; ii < num_frag; ii++) {
  622. lp->tx_bd_tail++;
  623. if (lp->tx_bd_tail >= TX_BD_NUM)
  624. lp->tx_bd_tail = 0;
  625. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  626. cur_p->phys = dma_map_single(ndev->dev.parent,
  627. skb_frag_address(frag),
  628. skb_frag_size(frag), DMA_TO_DEVICE);
  629. cur_p->len = skb_frag_size(frag);
  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->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 == htons(ETH_P_IP)) &&
  667. (skb->len > 64)) {
  668. skb->csum = cur_p->app3 & 0xFFFF;
  669. skb->ip_summed = CHECKSUM_COMPLETE;
  670. }
  671. if (!skb_defer_rx_timestamp(skb))
  672. netif_rx(skb);
  673. ndev->stats.rx_packets++;
  674. ndev->stats.rx_bytes += length;
  675. new_skb = netdev_alloc_skb_ip_align(ndev,
  676. XTE_MAX_JUMBO_FRAME_SIZE);
  677. if (!new_skb) {
  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. struct phy_device *phydev = NULL;
  725. int rc;
  726. dev_dbg(&ndev->dev, "temac_open()\n");
  727. if (lp->phy_node) {
  728. phydev = of_phy_connect(lp->ndev, lp->phy_node,
  729. temac_adjust_link, 0, 0);
  730. if (!phydev) {
  731. dev_err(lp->dev, "of_phy_connect() failed\n");
  732. return -ENODEV;
  733. }
  734. phy_start(phydev);
  735. }
  736. temac_device_reset(ndev);
  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. return 0;
  744. err_rx_irq:
  745. free_irq(lp->tx_irq, ndev);
  746. err_tx_irq:
  747. if (phydev)
  748. phy_disconnect(phydev);
  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. struct phy_device *phydev = ndev->phydev;
  756. dev_dbg(&ndev->dev, "temac_close()\n");
  757. free_irq(lp->tx_irq, ndev);
  758. free_irq(lp->rx_irq, ndev);
  759. if (phydev)
  760. phy_disconnect(phydev);
  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 int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  778. {
  779. if (!netif_running(ndev))
  780. return -EINVAL;
  781. if (!ndev->phydev)
  782. return -EINVAL;
  783. return phy_mii_ioctl(ndev->phydev, rq, cmd);
  784. }
  785. static const struct net_device_ops temac_netdev_ops = {
  786. .ndo_open = temac_open,
  787. .ndo_stop = temac_stop,
  788. .ndo_start_xmit = temac_start_xmit,
  789. .ndo_set_mac_address = temac_set_mac_address,
  790. .ndo_validate_addr = eth_validate_addr,
  791. .ndo_do_ioctl = temac_ioctl,
  792. #ifdef CONFIG_NET_POLL_CONTROLLER
  793. .ndo_poll_controller = temac_poll_controller,
  794. #endif
  795. };
  796. /* ---------------------------------------------------------------------
  797. * SYSFS device attributes
  798. */
  799. static ssize_t temac_show_llink_regs(struct device *dev,
  800. struct device_attribute *attr, char *buf)
  801. {
  802. struct net_device *ndev = dev_get_drvdata(dev);
  803. struct temac_local *lp = netdev_priv(ndev);
  804. int i, len = 0;
  805. for (i = 0; i < 0x11; i++)
  806. len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
  807. (i % 8) == 7 ? "\n" : " ");
  808. len += sprintf(buf + len, "\n");
  809. return len;
  810. }
  811. static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
  812. static struct attribute *temac_device_attrs[] = {
  813. &dev_attr_llink_regs.attr,
  814. NULL,
  815. };
  816. static const struct attribute_group temac_attr_group = {
  817. .attrs = temac_device_attrs,
  818. };
  819. /* ethtool support */
  820. static const struct ethtool_ops temac_ethtool_ops = {
  821. .nway_reset = phy_ethtool_nway_reset,
  822. .get_link = ethtool_op_get_link,
  823. .get_ts_info = ethtool_op_get_ts_info,
  824. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  825. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  826. };
  827. static int temac_of_probe(struct platform_device *op)
  828. {
  829. struct device_node *np;
  830. struct temac_local *lp;
  831. struct net_device *ndev;
  832. const void *addr;
  833. __be32 *p;
  834. int rc = 0;
  835. /* Init network device structure */
  836. ndev = alloc_etherdev(sizeof(*lp));
  837. if (!ndev)
  838. return -ENOMEM;
  839. platform_set_drvdata(op, ndev);
  840. SET_NETDEV_DEV(ndev, &op->dev);
  841. ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
  842. ndev->features = NETIF_F_SG;
  843. ndev->netdev_ops = &temac_netdev_ops;
  844. ndev->ethtool_ops = &temac_ethtool_ops;
  845. #if 0
  846. ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
  847. ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
  848. ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
  849. ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
  850. ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
  851. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
  852. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
  853. ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
  854. ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
  855. ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
  856. ndev->features |= NETIF_F_LRO; /* large receive offload */
  857. #endif
  858. /* setup temac private info structure */
  859. lp = netdev_priv(ndev);
  860. lp->ndev = ndev;
  861. lp->dev = &op->dev;
  862. lp->options = XTE_OPTION_DEFAULTS;
  863. spin_lock_init(&lp->rx_lock);
  864. mutex_init(&lp->indirect_mutex);
  865. /* map device registers */
  866. lp->regs = of_iomap(op->dev.of_node, 0);
  867. if (!lp->regs) {
  868. dev_err(&op->dev, "could not map temac regs.\n");
  869. rc = -ENOMEM;
  870. goto nodev;
  871. }
  872. /* Setup checksum offload, but default to off if not specified */
  873. lp->temac_features = 0;
  874. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
  875. if (p && be32_to_cpu(*p)) {
  876. lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
  877. /* Can checksum TCP/UDP over IPv4. */
  878. ndev->features |= NETIF_F_IP_CSUM;
  879. }
  880. p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
  881. if (p && be32_to_cpu(*p))
  882. lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
  883. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  884. np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
  885. if (!np) {
  886. dev_err(&op->dev, "could not find DMA node\n");
  887. rc = -ENODEV;
  888. goto err_iounmap;
  889. }
  890. /* Setup the DMA register accesses, could be DCR or memory mapped */
  891. if (temac_dcr_setup(lp, op, np)) {
  892. /* no DCR in the device tree, try non-DCR */
  893. lp->sdma_regs = of_iomap(np, 0);
  894. if (lp->sdma_regs) {
  895. lp->dma_in = temac_dma_in32;
  896. lp->dma_out = temac_dma_out32;
  897. dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
  898. } else {
  899. dev_err(&op->dev, "unable to map DMA registers\n");
  900. of_node_put(np);
  901. goto err_iounmap;
  902. }
  903. }
  904. lp->rx_irq = irq_of_parse_and_map(np, 0);
  905. lp->tx_irq = irq_of_parse_and_map(np, 1);
  906. of_node_put(np); /* Finished with the DMA node; drop the reference */
  907. if (!lp->rx_irq || !lp->tx_irq) {
  908. dev_err(&op->dev, "could not determine irqs\n");
  909. rc = -ENOMEM;
  910. goto err_iounmap_2;
  911. }
  912. /* Retrieve the MAC address */
  913. addr = of_get_mac_address(op->dev.of_node);
  914. if (!addr) {
  915. dev_err(&op->dev, "could not find MAC address\n");
  916. rc = -ENODEV;
  917. goto err_iounmap_2;
  918. }
  919. temac_init_mac_address(ndev, addr);
  920. rc = temac_mdio_setup(lp, op->dev.of_node);
  921. if (rc)
  922. dev_warn(&op->dev, "error registering MDIO bus\n");
  923. lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
  924. if (lp->phy_node)
  925. dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
  926. /* Add the device attributes */
  927. rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
  928. if (rc) {
  929. dev_err(lp->dev, "Error creating sysfs files\n");
  930. goto err_iounmap_2;
  931. }
  932. rc = register_netdev(lp->ndev);
  933. if (rc) {
  934. dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
  935. goto err_register_ndev;
  936. }
  937. return 0;
  938. err_register_ndev:
  939. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  940. err_iounmap_2:
  941. if (lp->sdma_regs)
  942. iounmap(lp->sdma_regs);
  943. err_iounmap:
  944. iounmap(lp->regs);
  945. nodev:
  946. free_netdev(ndev);
  947. ndev = NULL;
  948. return rc;
  949. }
  950. static int temac_of_remove(struct platform_device *op)
  951. {
  952. struct net_device *ndev = platform_get_drvdata(op);
  953. struct temac_local *lp = netdev_priv(ndev);
  954. temac_mdio_teardown(lp);
  955. unregister_netdev(ndev);
  956. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  957. of_node_put(lp->phy_node);
  958. lp->phy_node = NULL;
  959. iounmap(lp->regs);
  960. if (lp->sdma_regs)
  961. iounmap(lp->sdma_regs);
  962. free_netdev(ndev);
  963. return 0;
  964. }
  965. static const struct of_device_id temac_of_match[] = {
  966. { .compatible = "xlnx,xps-ll-temac-1.01.b", },
  967. { .compatible = "xlnx,xps-ll-temac-2.00.a", },
  968. { .compatible = "xlnx,xps-ll-temac-2.02.a", },
  969. { .compatible = "xlnx,xps-ll-temac-2.03.a", },
  970. {},
  971. };
  972. MODULE_DEVICE_TABLE(of, temac_of_match);
  973. static struct platform_driver temac_of_driver = {
  974. .probe = temac_of_probe,
  975. .remove = temac_of_remove,
  976. .driver = {
  977. .name = "xilinx_temac",
  978. .of_match_table = temac_of_match,
  979. },
  980. };
  981. module_platform_driver(temac_of_driver);
  982. MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
  983. MODULE_AUTHOR("Yoshio Kashiwagi");
  984. MODULE_LICENSE("GPL");