ll_temac_main.c 29 KB

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