ll_temac_main.c 28 KB

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