ll_temac_main.c 28 KB

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