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/skbuff.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/tcp.h> /* needed for sizeof(tcphdr) */
  43. #include <linux/udp.h> /* needed for sizeof(udphdr) */
  44. #include <linux/phy.h>
  45. #include <linux/in.h>
  46. #include <linux/io.h>
  47. #include <linux/ip.h>
  48. #include <linux/slab.h>
  49. #include "ll_temac.h"
  50. #define TX_BD_NUM 64
  51. #define RX_BD_NUM 128
  52. /* ---------------------------------------------------------------------
  53. * Low level register access functions
  54. */
  55. u32 temac_ior(struct temac_local *lp, int offset)
  56. {
  57. return in_be32((u32 *)(lp->regs + offset));
  58. }
  59. void temac_iow(struct temac_local *lp, int offset, u32 value)
  60. {
  61. out_be32((u32 *) (lp->regs + offset), value);
  62. }
  63. int temac_indirect_busywait(struct temac_local *lp)
  64. {
  65. long end = jiffies + 2;
  66. while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
  67. if (end - jiffies <= 0) {
  68. WARN_ON(1);
  69. return -ETIMEDOUT;
  70. }
  71. msleep(1);
  72. }
  73. return 0;
  74. }
  75. /**
  76. * temac_indirect_in32
  77. *
  78. * lp->indirect_mutex must be held when calling this function
  79. */
  80. u32 temac_indirect_in32(struct temac_local *lp, int reg)
  81. {
  82. u32 val;
  83. if (temac_indirect_busywait(lp))
  84. return -ETIMEDOUT;
  85. temac_iow(lp, XTE_CTL0_OFFSET, reg);
  86. if (temac_indirect_busywait(lp))
  87. return -ETIMEDOUT;
  88. val = temac_ior(lp, XTE_LSW0_OFFSET);
  89. return val;
  90. }
  91. /**
  92. * temac_indirect_out32
  93. *
  94. * lp->indirect_mutex must be held when calling this function
  95. */
  96. void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
  97. {
  98. if (temac_indirect_busywait(lp))
  99. return;
  100. temac_iow(lp, XTE_LSW0_OFFSET, value);
  101. temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
  102. }
  103. /**
  104. * temac_dma_in32 - Memory mapped DMA read, this function expects a
  105. * register input that is based on DCR word addresses which
  106. * are then converted to memory mapped byte addresses
  107. */
  108. static u32 temac_dma_in32(struct temac_local *lp, int reg)
  109. {
  110. return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
  111. }
  112. /**
  113. * temac_dma_out32 - Memory mapped DMA read, this function expects a
  114. * register input that is based on DCR word addresses which
  115. * are then converted to memory mapped byte addresses
  116. */
  117. static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
  118. {
  119. out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
  120. }
  121. /* DMA register access functions can be DCR based or memory mapped.
  122. * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
  123. * memory mapped.
  124. */
  125. #ifdef CONFIG_PPC_DCR
  126. /**
  127. * temac_dma_dcr_in32 - DCR based DMA read
  128. */
  129. static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
  130. {
  131. return dcr_read(lp->sdma_dcrs, reg);
  132. }
  133. /**
  134. * temac_dma_dcr_out32 - DCR based DMA write
  135. */
  136. static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
  137. {
  138. dcr_write(lp->sdma_dcrs, reg, value);
  139. }
  140. /**
  141. * temac_dcr_setup - If the DMA is DCR based, then setup the address and
  142. * I/O functions
  143. */
  144. static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
  145. struct device_node *np)
  146. {
  147. unsigned int dcrs;
  148. /* setup the dcr address mapping if it's in the device tree */
  149. dcrs = dcr_resource_start(np, 0);
  150. if (dcrs != 0) {
  151. lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  152. lp->dma_in = temac_dma_dcr_in;
  153. lp->dma_out = temac_dma_dcr_out;
  154. dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
  155. return 0;
  156. }
  157. /* no DCR in the device tree, indicate a failure */
  158. return -1;
  159. }
  160. #else
  161. /*
  162. * temac_dcr_setup - This is a stub for when DCR is not supported,
  163. * such as with MicroBlaze
  164. */
  165. static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
  166. struct device_node *np)
  167. {
  168. return -1;
  169. }
  170. #endif
  171. /**
  172. * * temac_dma_bd_release - Release buffer descriptor rings
  173. */
  174. static void temac_dma_bd_release(struct net_device *ndev)
  175. {
  176. struct temac_local *lp = netdev_priv(ndev);
  177. int i;
  178. for (i = 0; i < RX_BD_NUM; i++) {
  179. if (!lp->rx_skb[i])
  180. break;
  181. else {
  182. dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
  183. XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
  184. dev_kfree_skb(lp->rx_skb[i]);
  185. }
  186. }
  187. if (lp->rx_bd_v)
  188. dma_free_coherent(ndev->dev.parent,
  189. sizeof(*lp->rx_bd_v) * RX_BD_NUM,
  190. lp->rx_bd_v, lp->rx_bd_p);
  191. if (lp->tx_bd_v)
  192. dma_free_coherent(ndev->dev.parent,
  193. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  194. lp->tx_bd_v, lp->tx_bd_p);
  195. if (lp->rx_skb)
  196. kfree(lp->rx_skb);
  197. }
  198. /**
  199. * temac_dma_bd_init - Setup buffer descriptor rings
  200. */
  201. static int temac_dma_bd_init(struct net_device *ndev)
  202. {
  203. struct temac_local *lp = netdev_priv(ndev);
  204. struct sk_buff *skb;
  205. int i;
  206. lp->rx_skb = kzalloc(sizeof(*lp->rx_skb) * RX_BD_NUM, GFP_KERNEL);
  207. if (!lp->rx_skb) {
  208. dev_err(&ndev->dev,
  209. "can't allocate memory for DMA RX buffer\n");
  210. goto out;
  211. }
  212. /* allocate the tx and rx ring buffer descriptors. */
  213. /* returns a virtual addres and a physical address. */
  214. lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  215. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  216. &lp->tx_bd_p, GFP_KERNEL);
  217. if (!lp->tx_bd_v) {
  218. dev_err(&ndev->dev,
  219. "unable to allocate DMA TX buffer descriptors");
  220. goto out;
  221. }
  222. lp->rx_bd_v = dma_alloc_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. dev_err(&ndev->dev,
  227. "unable to allocate DMA RX buffer descriptors");
  228. goto out;
  229. }
  230. memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
  231. for (i = 0; i < TX_BD_NUM; i++) {
  232. lp->tx_bd_v[i].next = lp->tx_bd_p +
  233. sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
  234. }
  235. memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
  236. for (i = 0; i < RX_BD_NUM; i++) {
  237. lp->rx_bd_v[i].next = lp->rx_bd_p +
  238. sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
  239. skb = netdev_alloc_skb_ip_align(ndev,
  240. XTE_MAX_JUMBO_FRAME_SIZE);
  241. if (skb == 0) {
  242. dev_err(&ndev->dev, "alloc_skb error %d\n", i);
  243. goto out;
  244. }
  245. lp->rx_skb[i] = skb;
  246. /* returns physical address of skb->data */
  247. lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
  248. skb->data,
  249. XTE_MAX_JUMBO_FRAME_SIZE,
  250. DMA_FROM_DEVICE);
  251. lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
  252. lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
  253. }
  254. lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
  255. CHNL_CTRL_IRQ_EN |
  256. CHNL_CTRL_IRQ_DLY_EN |
  257. CHNL_CTRL_IRQ_COAL_EN);
  258. /* 0x10220483 */
  259. /* 0x00100483 */
  260. lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
  261. CHNL_CTRL_IRQ_EN |
  262. CHNL_CTRL_IRQ_DLY_EN |
  263. CHNL_CTRL_IRQ_COAL_EN |
  264. CHNL_CTRL_IRQ_IOE);
  265. /* 0xff010283 */
  266. lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
  267. lp->dma_out(lp, RX_TAILDESC_PTR,
  268. lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  269. lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
  270. return 0;
  271. out:
  272. temac_dma_bd_release(ndev);
  273. return -ENOMEM;
  274. }
  275. /* ---------------------------------------------------------------------
  276. * net_device_ops
  277. */
  278. static int temac_set_mac_address(struct net_device *ndev, void *address)
  279. {
  280. struct temac_local *lp = netdev_priv(ndev);
  281. if (address)
  282. memcpy(ndev->dev_addr, address, ETH_ALEN);
  283. if (!is_valid_ether_addr(ndev->dev_addr))
  284. random_ether_addr(ndev->dev_addr);
  285. /* set up unicast MAC address filter set its mac address */
  286. mutex_lock(&lp->indirect_mutex);
  287. temac_indirect_out32(lp, XTE_UAW0_OFFSET,
  288. (ndev->dev_addr[0]) |
  289. (ndev->dev_addr[1] << 8) |
  290. (ndev->dev_addr[2] << 16) |
  291. (ndev->dev_addr[3] << 24));
  292. /* There are reserved bits in EUAW1
  293. * so don't affect them Set MAC bits [47:32] in EUAW1 */
  294. temac_indirect_out32(lp, XTE_UAW1_OFFSET,
  295. (ndev->dev_addr[4] & 0x000000ff) |
  296. (ndev->dev_addr[5] << 8));
  297. mutex_unlock(&lp->indirect_mutex);
  298. return 0;
  299. }
  300. static int netdev_set_mac_address(struct net_device *ndev, void *p)
  301. {
  302. struct sockaddr *addr = p;
  303. return temac_set_mac_address(ndev, addr->sa_data);
  304. }
  305. static void temac_set_multicast_list(struct net_device *ndev)
  306. {
  307. struct temac_local *lp = netdev_priv(ndev);
  308. u32 multi_addr_msw, multi_addr_lsw, val;
  309. int i;
  310. mutex_lock(&lp->indirect_mutex);
  311. if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
  312. netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
  313. /*
  314. * We must make the kernel realise we had to move
  315. * into promisc mode or we start all out war on
  316. * the cable. If it was a promisc request the
  317. * flag is already set. If not we assert it.
  318. */
  319. ndev->flags |= IFF_PROMISC;
  320. temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
  321. dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
  322. } else if (!netdev_mc_empty(ndev)) {
  323. struct netdev_hw_addr *ha;
  324. i = 0;
  325. netdev_for_each_mc_addr(ha, ndev) {
  326. if (i >= MULTICAST_CAM_TABLE_NUM)
  327. break;
  328. multi_addr_msw = ((ha->addr[3] << 24) |
  329. (ha->addr[2] << 16) |
  330. (ha->addr[1] << 8) |
  331. (ha->addr[0]));
  332. temac_indirect_out32(lp, XTE_MAW0_OFFSET,
  333. multi_addr_msw);
  334. multi_addr_lsw = ((ha->addr[5] << 8) |
  335. (ha->addr[4]) | (i << 16));
  336. temac_indirect_out32(lp, XTE_MAW1_OFFSET,
  337. multi_addr_lsw);
  338. i++;
  339. }
  340. } else {
  341. val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
  342. temac_indirect_out32(lp, XTE_AFM_OFFSET,
  343. val & ~XTE_AFM_EPPRM_MASK);
  344. temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
  345. temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
  346. dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
  347. }
  348. mutex_unlock(&lp->indirect_mutex);
  349. }
  350. struct temac_option {
  351. int flg;
  352. u32 opt;
  353. u32 reg;
  354. u32 m_or;
  355. u32 m_and;
  356. } temac_options[] = {
  357. /* Turn on jumbo packet support for both Rx and Tx */
  358. {
  359. .opt = XTE_OPTION_JUMBO,
  360. .reg = XTE_TXC_OFFSET,
  361. .m_or = XTE_TXC_TXJMBO_MASK,
  362. },
  363. {
  364. .opt = XTE_OPTION_JUMBO,
  365. .reg = XTE_RXC1_OFFSET,
  366. .m_or =XTE_RXC1_RXJMBO_MASK,
  367. },
  368. /* Turn on VLAN packet support for both Rx and Tx */
  369. {
  370. .opt = XTE_OPTION_VLAN,
  371. .reg = XTE_TXC_OFFSET,
  372. .m_or =XTE_TXC_TXVLAN_MASK,
  373. },
  374. {
  375. .opt = XTE_OPTION_VLAN,
  376. .reg = XTE_RXC1_OFFSET,
  377. .m_or =XTE_RXC1_RXVLAN_MASK,
  378. },
  379. /* Turn on FCS stripping on receive packets */
  380. {
  381. .opt = XTE_OPTION_FCS_STRIP,
  382. .reg = XTE_RXC1_OFFSET,
  383. .m_or =XTE_RXC1_RXFCS_MASK,
  384. },
  385. /* Turn on FCS insertion on transmit packets */
  386. {
  387. .opt = XTE_OPTION_FCS_INSERT,
  388. .reg = XTE_TXC_OFFSET,
  389. .m_or =XTE_TXC_TXFCS_MASK,
  390. },
  391. /* Turn on length/type field checking on receive packets */
  392. {
  393. .opt = XTE_OPTION_LENTYPE_ERR,
  394. .reg = XTE_RXC1_OFFSET,
  395. .m_or =XTE_RXC1_RXLT_MASK,
  396. },
  397. /* Turn on flow control */
  398. {
  399. .opt = XTE_OPTION_FLOW_CONTROL,
  400. .reg = XTE_FCC_OFFSET,
  401. .m_or =XTE_FCC_RXFLO_MASK,
  402. },
  403. /* Turn on flow control */
  404. {
  405. .opt = XTE_OPTION_FLOW_CONTROL,
  406. .reg = XTE_FCC_OFFSET,
  407. .m_or =XTE_FCC_TXFLO_MASK,
  408. },
  409. /* Turn on promiscuous frame filtering (all frames are received ) */
  410. {
  411. .opt = XTE_OPTION_PROMISC,
  412. .reg = XTE_AFM_OFFSET,
  413. .m_or =XTE_AFM_EPPRM_MASK,
  414. },
  415. /* Enable transmitter if not already enabled */
  416. {
  417. .opt = XTE_OPTION_TXEN,
  418. .reg = XTE_TXC_OFFSET,
  419. .m_or =XTE_TXC_TXEN_MASK,
  420. },
  421. /* Enable receiver? */
  422. {
  423. .opt = XTE_OPTION_RXEN,
  424. .reg = XTE_RXC1_OFFSET,
  425. .m_or =XTE_RXC1_RXEN_MASK,
  426. },
  427. {}
  428. };
  429. /**
  430. * temac_setoptions
  431. */
  432. static u32 temac_setoptions(struct net_device *ndev, u32 options)
  433. {
  434. struct temac_local *lp = netdev_priv(ndev);
  435. struct temac_option *tp = &temac_options[0];
  436. int reg;
  437. mutex_lock(&lp->indirect_mutex);
  438. while (tp->opt) {
  439. reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
  440. if (options & tp->opt)
  441. reg |= tp->m_or;
  442. temac_indirect_out32(lp, tp->reg, reg);
  443. tp++;
  444. }
  445. lp->options |= options;
  446. mutex_unlock(&lp->indirect_mutex);
  447. return (0);
  448. }
  449. /* Initilize temac */
  450. static void temac_device_reset(struct net_device *ndev)
  451. {
  452. struct temac_local *lp = netdev_priv(ndev);
  453. u32 timeout;
  454. u32 val;
  455. /* Perform a software reset */
  456. /* 0x300 host enable bit ? */
  457. /* reset PHY through control register ?:1 */
  458. dev_dbg(&ndev->dev, "%s()\n", __func__);
  459. mutex_lock(&lp->indirect_mutex);
  460. /* Reset the receiver and wait for it to finish reset */
  461. temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
  462. timeout = 1000;
  463. while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
  464. udelay(1);
  465. if (--timeout == 0) {
  466. dev_err(&ndev->dev,
  467. "temac_device_reset RX reset timeout!!\n");
  468. break;
  469. }
  470. }
  471. /* Reset the transmitter and wait for it to finish reset */
  472. temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
  473. timeout = 1000;
  474. while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
  475. udelay(1);
  476. if (--timeout == 0) {
  477. dev_err(&ndev->dev,
  478. "temac_device_reset TX reset timeout!!\n");
  479. break;
  480. }
  481. }
  482. /* Disable the receiver */
  483. val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
  484. temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
  485. /* Reset Local Link (DMA) */
  486. lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
  487. timeout = 1000;
  488. while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
  489. udelay(1);
  490. if (--timeout == 0) {
  491. dev_err(&ndev->dev,
  492. "temac_device_reset DMA reset timeout!!\n");
  493. break;
  494. }
  495. }
  496. lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
  497. if (temac_dma_bd_init(ndev)) {
  498. dev_err(&ndev->dev,
  499. "temac_device_reset descriptor allocation failed\n");
  500. }
  501. temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
  502. temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
  503. temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
  504. temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
  505. mutex_unlock(&lp->indirect_mutex);
  506. /* Sync default options with HW
  507. * but leave receiver and transmitter disabled. */
  508. temac_setoptions(ndev,
  509. lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
  510. temac_set_mac_address(ndev, NULL);
  511. /* Set address filter table */
  512. temac_set_multicast_list(ndev);
  513. if (temac_setoptions(ndev, lp->options))
  514. dev_err(&ndev->dev, "Error setting TEMAC options\n");
  515. /* Init Driver variable */
  516. ndev->trans_start = jiffies; /* prevent tx timeout */
  517. }
  518. void temac_adjust_link(struct net_device *ndev)
  519. {
  520. struct temac_local *lp = netdev_priv(ndev);
  521. struct phy_device *phy = lp->phy_dev;
  522. u32 mii_speed;
  523. int link_state;
  524. /* hash together the state values to decide if something has changed */
  525. link_state = phy->speed | (phy->duplex << 1) | phy->link;
  526. mutex_lock(&lp->indirect_mutex);
  527. if (lp->last_link != link_state) {
  528. mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
  529. mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
  530. switch (phy->speed) {
  531. case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
  532. case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
  533. case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
  534. }
  535. /* Write new speed setting out to TEMAC */
  536. temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
  537. lp->last_link = link_state;
  538. phy_print_status(phy);
  539. }
  540. mutex_unlock(&lp->indirect_mutex);
  541. }
  542. static void temac_start_xmit_done(struct net_device *ndev)
  543. {
  544. struct temac_local *lp = netdev_priv(ndev);
  545. struct cdmac_bd *cur_p;
  546. unsigned int stat = 0;
  547. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  548. stat = cur_p->app0;
  549. while (stat & STS_CTRL_APP0_CMPLT) {
  550. dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
  551. DMA_TO_DEVICE);
  552. if (cur_p->app4)
  553. dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
  554. cur_p->app0 = 0;
  555. cur_p->app1 = 0;
  556. cur_p->app2 = 0;
  557. cur_p->app3 = 0;
  558. cur_p->app4 = 0;
  559. ndev->stats.tx_packets++;
  560. ndev->stats.tx_bytes += cur_p->len;
  561. lp->tx_bd_ci++;
  562. if (lp->tx_bd_ci >= TX_BD_NUM)
  563. lp->tx_bd_ci = 0;
  564. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  565. stat = cur_p->app0;
  566. }
  567. netif_wake_queue(ndev);
  568. }
  569. static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
  570. {
  571. struct cdmac_bd *cur_p;
  572. int tail;
  573. tail = lp->tx_bd_tail;
  574. cur_p = &lp->tx_bd_v[tail];
  575. do {
  576. if (cur_p->app0)
  577. return NETDEV_TX_BUSY;
  578. tail++;
  579. if (tail >= TX_BD_NUM)
  580. tail = 0;
  581. cur_p = &lp->tx_bd_v[tail];
  582. num_frag--;
  583. } while (num_frag >= 0);
  584. return 0;
  585. }
  586. static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  587. {
  588. struct temac_local *lp = netdev_priv(ndev);
  589. struct cdmac_bd *cur_p;
  590. dma_addr_t start_p, tail_p;
  591. int ii;
  592. unsigned long num_frag;
  593. skb_frag_t *frag;
  594. num_frag = skb_shinfo(skb)->nr_frags;
  595. frag = &skb_shinfo(skb)->frags[0];
  596. start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  597. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  598. if (temac_check_tx_bd_space(lp, num_frag)) {
  599. if (!netif_queue_stopped(ndev)) {
  600. netif_stop_queue(ndev);
  601. return NETDEV_TX_BUSY;
  602. }
  603. return NETDEV_TX_BUSY;
  604. }
  605. cur_p->app0 = 0;
  606. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  607. unsigned int csum_start_off = skb_transport_offset(skb);
  608. unsigned int csum_index_off = csum_start_off + skb->csum_offset;
  609. cur_p->app0 |= 1; /* TX Checksum Enabled */
  610. cur_p->app1 = (csum_start_off << 16) | csum_index_off;
  611. cur_p->app2 = 0; /* initial checksum seed */
  612. }
  613. cur_p->app0 |= STS_CTRL_APP0_SOP;
  614. cur_p->len = skb_headlen(skb);
  615. cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
  616. DMA_TO_DEVICE);
  617. cur_p->app4 = (unsigned long)skb;
  618. for (ii = 0; ii < num_frag; ii++) {
  619. lp->tx_bd_tail++;
  620. if (lp->tx_bd_tail >= TX_BD_NUM)
  621. lp->tx_bd_tail = 0;
  622. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  623. cur_p->phys = dma_map_single(ndev->dev.parent,
  624. (void *)page_address(frag->page) +
  625. frag->page_offset,
  626. frag->size, DMA_TO_DEVICE);
  627. cur_p->len = frag->size;
  628. cur_p->app0 = 0;
  629. frag++;
  630. }
  631. cur_p->app0 |= STS_CTRL_APP0_EOP;
  632. tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  633. lp->tx_bd_tail++;
  634. if (lp->tx_bd_tail >= TX_BD_NUM)
  635. lp->tx_bd_tail = 0;
  636. /* Kick off the transfer */
  637. lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
  638. return NETDEV_TX_OK;
  639. }
  640. static void ll_temac_recv(struct net_device *ndev)
  641. {
  642. struct temac_local *lp = netdev_priv(ndev);
  643. struct sk_buff *skb, *new_skb;
  644. unsigned int bdstat;
  645. struct cdmac_bd *cur_p;
  646. dma_addr_t tail_p;
  647. int length;
  648. unsigned long flags;
  649. spin_lock_irqsave(&lp->rx_lock, flags);
  650. tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
  651. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  652. bdstat = cur_p->app0;
  653. while ((bdstat & STS_CTRL_APP0_CMPLT)) {
  654. skb = lp->rx_skb[lp->rx_bd_ci];
  655. length = cur_p->app4 & 0x3FFF;
  656. dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
  657. DMA_FROM_DEVICE);
  658. skb_put(skb, length);
  659. skb->dev = ndev;
  660. skb->protocol = eth_type_trans(skb, ndev);
  661. skb->ip_summed = CHECKSUM_NONE;
  662. /* if we're doing rx csum offload, set it up */
  663. if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
  664. (skb->protocol == __constant_htons(ETH_P_IP)) &&
  665. (skb->len > 64)) {
  666. skb->csum = cur_p->app3 & 0xFFFF;
  667. skb->ip_summed = CHECKSUM_COMPLETE;
  668. }
  669. netif_rx(skb);
  670. ndev->stats.rx_packets++;
  671. ndev->stats.rx_bytes += length;
  672. new_skb = netdev_alloc_skb_ip_align(ndev,
  673. XTE_MAX_JUMBO_FRAME_SIZE);
  674. if (new_skb == 0) {
  675. dev_err(&ndev->dev, "no memory for new sk_buff\n");
  676. spin_unlock_irqrestore(&lp->rx_lock, flags);
  677. return;
  678. }
  679. cur_p->app0 = STS_CTRL_APP0_IRQONEND;
  680. cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
  681. XTE_MAX_JUMBO_FRAME_SIZE,
  682. DMA_FROM_DEVICE);
  683. cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
  684. lp->rx_skb[lp->rx_bd_ci] = new_skb;
  685. lp->rx_bd_ci++;
  686. if (lp->rx_bd_ci >= RX_BD_NUM)
  687. lp->rx_bd_ci = 0;
  688. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  689. bdstat = cur_p->app0;
  690. }
  691. lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
  692. spin_unlock_irqrestore(&lp->rx_lock, flags);
  693. }
  694. static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
  695. {
  696. struct net_device *ndev = _ndev;
  697. struct temac_local *lp = netdev_priv(ndev);
  698. unsigned int status;
  699. status = lp->dma_in(lp, TX_IRQ_REG);
  700. lp->dma_out(lp, TX_IRQ_REG, status);
  701. if (status & (IRQ_COAL | IRQ_DLY))
  702. temac_start_xmit_done(lp->ndev);
  703. if (status & 0x080)
  704. dev_err(&ndev->dev, "DMA error 0x%x\n", status);
  705. return IRQ_HANDLED;
  706. }
  707. static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
  708. {
  709. struct net_device *ndev = _ndev;
  710. struct temac_local *lp = netdev_priv(ndev);
  711. unsigned int status;
  712. /* Read and clear the status registers */
  713. status = lp->dma_in(lp, RX_IRQ_REG);
  714. lp->dma_out(lp, RX_IRQ_REG, status);
  715. if (status & (IRQ_COAL | IRQ_DLY))
  716. ll_temac_recv(lp->ndev);
  717. return IRQ_HANDLED;
  718. }
  719. static int temac_open(struct net_device *ndev)
  720. {
  721. struct temac_local *lp = netdev_priv(ndev);
  722. int rc;
  723. dev_dbg(&ndev->dev, "temac_open()\n");
  724. if (lp->phy_node) {
  725. lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
  726. temac_adjust_link, 0, 0);
  727. if (!lp->phy_dev) {
  728. dev_err(lp->dev, "of_phy_connect() failed\n");
  729. return -ENODEV;
  730. }
  731. phy_start(lp->phy_dev);
  732. }
  733. rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
  734. if (rc)
  735. goto err_tx_irq;
  736. rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
  737. if (rc)
  738. goto err_rx_irq;
  739. temac_device_reset(ndev);
  740. return 0;
  741. err_rx_irq:
  742. free_irq(lp->tx_irq, ndev);
  743. err_tx_irq:
  744. if (lp->phy_dev)
  745. phy_disconnect(lp->phy_dev);
  746. lp->phy_dev = NULL;
  747. dev_err(lp->dev, "request_irq() failed\n");
  748. return rc;
  749. }
  750. static int temac_stop(struct net_device *ndev)
  751. {
  752. struct temac_local *lp = netdev_priv(ndev);
  753. dev_dbg(&ndev->dev, "temac_close()\n");
  754. free_irq(lp->tx_irq, ndev);
  755. free_irq(lp->rx_irq, ndev);
  756. if (lp->phy_dev)
  757. phy_disconnect(lp->phy_dev);
  758. lp->phy_dev = NULL;
  759. temac_dma_bd_release(ndev);
  760. return 0;
  761. }
  762. #ifdef CONFIG_NET_POLL_CONTROLLER
  763. static void
  764. temac_poll_controller(struct net_device *ndev)
  765. {
  766. struct temac_local *lp = netdev_priv(ndev);
  767. disable_irq(lp->tx_irq);
  768. disable_irq(lp->rx_irq);
  769. ll_temac_rx_irq(lp->tx_irq, lp);
  770. ll_temac_tx_irq(lp->rx_irq, lp);
  771. enable_irq(lp->tx_irq);
  772. enable_irq(lp->rx_irq);
  773. }
  774. #endif
  775. static const struct net_device_ops temac_netdev_ops = {
  776. .ndo_open = temac_open,
  777. .ndo_stop = temac_stop,
  778. .ndo_start_xmit = temac_start_xmit,
  779. .ndo_set_mac_address = netdev_set_mac_address,
  780. .ndo_validate_addr = eth_validate_addr,
  781. //.ndo_set_multicast_list = temac_set_multicast_list,
  782. #ifdef CONFIG_NET_POLL_CONTROLLER
  783. .ndo_poll_controller = temac_poll_controller,
  784. #endif
  785. };
  786. /* ---------------------------------------------------------------------
  787. * SYSFS device attributes
  788. */
  789. static ssize_t temac_show_llink_regs(struct device *dev,
  790. struct device_attribute *attr, char *buf)
  791. {
  792. struct net_device *ndev = dev_get_drvdata(dev);
  793. struct temac_local *lp = netdev_priv(ndev);
  794. int i, len = 0;
  795. for (i = 0; i < 0x11; i++)
  796. len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
  797. (i % 8) == 7 ? "\n" : " ");
  798. len += sprintf(buf + len, "\n");
  799. return len;
  800. }
  801. static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
  802. static struct attribute *temac_device_attrs[] = {
  803. &dev_attr_llink_regs.attr,
  804. NULL,
  805. };
  806. static const struct attribute_group temac_attr_group = {
  807. .attrs = temac_device_attrs,
  808. };
  809. static int __init
  810. temac_of_probe(struct of_device *op, const struct of_device_id *match)
  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 of_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 of_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 of_register_platform_driver(&temac_of_driver);
  970. }
  971. module_init(temac_init);
  972. static void __exit temac_exit(void)
  973. {
  974. of_unregister_platform_driver(&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");