ll_temac_main.c 29 KB

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