netx-eth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * drivers/net/netx-eth.c
  3. *
  4. * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/mii.h>
  28. #include <asm/io.h>
  29. #include <mach/hardware.h>
  30. #include <mach/netx-regs.h>
  31. #include <mach/pfifo.h>
  32. #include <mach/xc.h>
  33. #include <mach/eth.h>
  34. /* XC Fifo Offsets */
  35. #define EMPTY_PTR_FIFO(xcno) (0 + ((xcno) << 3)) /* Index of the empty pointer FIFO */
  36. #define IND_FIFO_PORT_HI(xcno) (1 + ((xcno) << 3)) /* Index of the FIFO where received */
  37. /* Data packages are indicated by XC */
  38. #define IND_FIFO_PORT_LO(xcno) (2 + ((xcno) << 3)) /* Index of the FIFO where received */
  39. /* Data packages are indicated by XC */
  40. #define REQ_FIFO_PORT_HI(xcno) (3 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
  41. /* have to be indicated by ARM which */
  42. /* shall be sent */
  43. #define REQ_FIFO_PORT_LO(xcno) (4 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
  44. /* have to be indicated by ARM which shall */
  45. /* be sent */
  46. #define CON_FIFO_PORT_HI(xcno) (5 + ((xcno) << 3)) /* Index of the FIFO where sent Data packages */
  47. /* are confirmed */
  48. #define CON_FIFO_PORT_LO(xcno) (6 + ((xcno) << 3)) /* Index of the FIFO where sent Data */
  49. /* packages are confirmed */
  50. #define PFIFO_MASK(xcno) (0x7f << (xcno*8))
  51. #define FIFO_PTR_FRAMELEN_SHIFT 0
  52. #define FIFO_PTR_FRAMELEN_MASK (0x7ff << 0)
  53. #define FIFO_PTR_FRAMELEN(len) (((len) << 0) & FIFO_PTR_FRAMELEN_MASK)
  54. #define FIFO_PTR_TIMETRIG (1<<11)
  55. #define FIFO_PTR_MULTI_REQ
  56. #define FIFO_PTR_ORIGIN (1<<14)
  57. #define FIFO_PTR_VLAN (1<<15)
  58. #define FIFO_PTR_FRAMENO_SHIFT 16
  59. #define FIFO_PTR_FRAMENO_MASK (0x3f << 16)
  60. #define FIFO_PTR_FRAMENO(no) (((no) << 16) & FIFO_PTR_FRAMENO_MASK)
  61. #define FIFO_PTR_SEGMENT_SHIFT 22
  62. #define FIFO_PTR_SEGMENT_MASK (0xf << 22)
  63. #define FIFO_PTR_SEGMENT(seg) (((seg) & 0xf) << 22)
  64. #define FIFO_PTR_ERROR_SHIFT 28
  65. #define FIFO_PTR_ERROR_MASK (0xf << 28)
  66. #define ISR_LINK_STATUS_CHANGE (1<<4)
  67. #define ISR_IND_LO (1<<3)
  68. #define ISR_CON_LO (1<<2)
  69. #define ISR_IND_HI (1<<1)
  70. #define ISR_CON_HI (1<<0)
  71. #define ETH_MAC_LOCAL_CONFIG 0x1560
  72. #define ETH_MAC_4321 0x1564
  73. #define ETH_MAC_65 0x1568
  74. #define MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT 16
  75. #define MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK (0xf<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT)
  76. #define MAC_TRAFFIC_CLASS_ARRANGEMENT(x) (((x)<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT) & MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK)
  77. #define LOCAL_CONFIG_LINK_STATUS_IRQ_EN (1<<24)
  78. #define LOCAL_CONFIG_CON_LO_IRQ_EN (1<<23)
  79. #define LOCAL_CONFIG_CON_HI_IRQ_EN (1<<22)
  80. #define LOCAL_CONFIG_IND_LO_IRQ_EN (1<<21)
  81. #define LOCAL_CONFIG_IND_HI_IRQ_EN (1<<20)
  82. #define CARDNAME "netx-eth"
  83. /* LSB must be zero */
  84. #define INTERNAL_PHY_ADR 0x1c
  85. struct netx_eth_priv {
  86. void __iomem *sram_base, *xpec_base, *xmac_base;
  87. int id;
  88. struct mii_if_info mii;
  89. u32 msg_enable;
  90. struct xc *xc;
  91. spinlock_t lock;
  92. };
  93. static void netx_eth_set_multicast_list(struct net_device *ndev)
  94. {
  95. /* implement me */
  96. }
  97. static int
  98. netx_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  99. {
  100. struct netx_eth_priv *priv = netdev_priv(ndev);
  101. unsigned char *buf = skb->data;
  102. unsigned int len = skb->len;
  103. spin_lock_irq(&priv->lock);
  104. memcpy_toio(priv->sram_base + 1560, (void *)buf, len);
  105. if (len < 60) {
  106. memset_io(priv->sram_base + 1560 + len, 0, 60 - len);
  107. len = 60;
  108. }
  109. pfifo_push(REQ_FIFO_PORT_LO(priv->id),
  110. FIFO_PTR_SEGMENT(priv->id) |
  111. FIFO_PTR_FRAMENO(1) |
  112. FIFO_PTR_FRAMELEN(len));
  113. ndev->stats.tx_packets++;
  114. ndev->stats.tx_bytes += skb->len;
  115. netif_stop_queue(ndev);
  116. spin_unlock_irq(&priv->lock);
  117. dev_kfree_skb(skb);
  118. return NETDEV_TX_OK;
  119. }
  120. static void netx_eth_receive(struct net_device *ndev)
  121. {
  122. struct netx_eth_priv *priv = netdev_priv(ndev);
  123. unsigned int val, frameno, seg, len;
  124. unsigned char *data;
  125. struct sk_buff *skb;
  126. val = pfifo_pop(IND_FIFO_PORT_LO(priv->id));
  127. frameno = (val & FIFO_PTR_FRAMENO_MASK) >> FIFO_PTR_FRAMENO_SHIFT;
  128. seg = (val & FIFO_PTR_SEGMENT_MASK) >> FIFO_PTR_SEGMENT_SHIFT;
  129. len = (val & FIFO_PTR_FRAMELEN_MASK) >> FIFO_PTR_FRAMELEN_SHIFT;
  130. skb = dev_alloc_skb(len);
  131. if (unlikely(skb == NULL)) {
  132. printk(KERN_NOTICE "%s: Low memory, packet dropped.\n",
  133. ndev->name);
  134. ndev->stats.rx_dropped++;
  135. return;
  136. }
  137. data = skb_put(skb, len);
  138. memcpy_fromio(data, priv->sram_base + frameno * 1560, len);
  139. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  140. FIFO_PTR_SEGMENT(seg) | FIFO_PTR_FRAMENO(frameno));
  141. skb->protocol = eth_type_trans(skb, ndev);
  142. netif_rx(skb);
  143. ndev->stats.rx_packets++;
  144. ndev->stats.rx_bytes += len;
  145. }
  146. static irqreturn_t
  147. netx_eth_interrupt(int irq, void *dev_id)
  148. {
  149. struct net_device *ndev = dev_id;
  150. struct netx_eth_priv *priv = netdev_priv(ndev);
  151. int status;
  152. unsigned long flags;
  153. spin_lock_irqsave(&priv->lock, flags);
  154. status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
  155. while (status) {
  156. int fill_level;
  157. writel(status, NETX_PFIFO_XPEC_ISR(priv->id));
  158. if ((status & ISR_CON_HI) || (status & ISR_IND_HI))
  159. printk("%s: unexpected status: 0x%08x\n",
  160. __func__, status);
  161. fill_level =
  162. readl(NETX_PFIFO_FILL_LEVEL(IND_FIFO_PORT_LO(priv->id)));
  163. while (fill_level--)
  164. netx_eth_receive(ndev);
  165. if (status & ISR_CON_LO)
  166. netif_wake_queue(ndev);
  167. if (status & ISR_LINK_STATUS_CHANGE)
  168. mii_check_media(&priv->mii, netif_msg_link(priv), 1);
  169. status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
  170. }
  171. spin_unlock_irqrestore(&priv->lock, flags);
  172. return IRQ_HANDLED;
  173. }
  174. static int netx_eth_open(struct net_device *ndev)
  175. {
  176. struct netx_eth_priv *priv = netdev_priv(ndev);
  177. if (request_irq
  178. (ndev->irq, netx_eth_interrupt, IRQF_SHARED, ndev->name, ndev))
  179. return -EAGAIN;
  180. writel(ndev->dev_addr[0] |
  181. ndev->dev_addr[1]<<8 |
  182. ndev->dev_addr[2]<<16 |
  183. ndev->dev_addr[3]<<24,
  184. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
  185. writel(ndev->dev_addr[4] |
  186. ndev->dev_addr[5]<<8,
  187. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
  188. writel(LOCAL_CONFIG_LINK_STATUS_IRQ_EN |
  189. LOCAL_CONFIG_CON_LO_IRQ_EN |
  190. LOCAL_CONFIG_CON_HI_IRQ_EN |
  191. LOCAL_CONFIG_IND_LO_IRQ_EN |
  192. LOCAL_CONFIG_IND_HI_IRQ_EN,
  193. priv->xpec_base + NETX_XPEC_RAM_START_OFS +
  194. ETH_MAC_LOCAL_CONFIG);
  195. mii_check_media(&priv->mii, netif_msg_link(priv), 1);
  196. netif_start_queue(ndev);
  197. return 0;
  198. }
  199. static int netx_eth_close(struct net_device *ndev)
  200. {
  201. struct netx_eth_priv *priv = netdev_priv(ndev);
  202. netif_stop_queue(ndev);
  203. writel(0,
  204. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_LOCAL_CONFIG);
  205. free_irq(ndev->irq, ndev);
  206. return 0;
  207. }
  208. static void netx_eth_timeout(struct net_device *ndev)
  209. {
  210. struct netx_eth_priv *priv = netdev_priv(ndev);
  211. int i;
  212. printk(KERN_ERR "%s: transmit timed out, resetting\n", ndev->name);
  213. spin_lock_irq(&priv->lock);
  214. xc_reset(priv->xc);
  215. xc_start(priv->xc);
  216. for (i=2; i<=18; i++)
  217. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  218. FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
  219. spin_unlock_irq(&priv->lock);
  220. netif_wake_queue(ndev);
  221. }
  222. static int
  223. netx_eth_phy_read(struct net_device *ndev, int phy_id, int reg)
  224. {
  225. unsigned int val;
  226. val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
  227. MIIMU_REGADDR(reg) | MIIMU_PHY_NRES;
  228. writel(val, NETX_MIIMU);
  229. while (readl(NETX_MIIMU) & MIIMU_SNRDY);
  230. return readl(NETX_MIIMU) >> 16;
  231. }
  232. static void
  233. netx_eth_phy_write(struct net_device *ndev, int phy_id, int reg, int value)
  234. {
  235. unsigned int val;
  236. val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
  237. MIIMU_REGADDR(reg) | MIIMU_PHY_NRES | MIIMU_OPMODE_WRITE |
  238. MIIMU_DATA(value);
  239. writel(val, NETX_MIIMU);
  240. while (readl(NETX_MIIMU) & MIIMU_SNRDY);
  241. }
  242. static const struct net_device_ops netx_eth_netdev_ops = {
  243. .ndo_open = netx_eth_open,
  244. .ndo_stop = netx_eth_close,
  245. .ndo_start_xmit = netx_eth_hard_start_xmit,
  246. .ndo_tx_timeout = netx_eth_timeout,
  247. .ndo_set_multicast_list = netx_eth_set_multicast_list,
  248. .ndo_change_mtu = eth_change_mtu,
  249. .ndo_validate_addr = eth_validate_addr,
  250. .ndo_set_mac_address = eth_mac_addr,
  251. };
  252. static int netx_eth_enable(struct net_device *ndev)
  253. {
  254. struct netx_eth_priv *priv = netdev_priv(ndev);
  255. unsigned int mac4321, mac65;
  256. int running, i;
  257. ether_setup(ndev);
  258. ndev->netdev_ops = &netx_eth_netdev_ops;
  259. ndev->watchdog_timeo = msecs_to_jiffies(5000);
  260. priv->msg_enable = NETIF_MSG_LINK;
  261. priv->mii.phy_id_mask = 0x1f;
  262. priv->mii.reg_num_mask = 0x1f;
  263. priv->mii.force_media = 0;
  264. priv->mii.full_duplex = 0;
  265. priv->mii.dev = ndev;
  266. priv->mii.mdio_read = netx_eth_phy_read;
  267. priv->mii.mdio_write = netx_eth_phy_write;
  268. priv->mii.phy_id = INTERNAL_PHY_ADR + priv->id;
  269. running = xc_running(priv->xc);
  270. xc_stop(priv->xc);
  271. /* if the xc engine is already running, assume the bootloader has
  272. * loaded the firmware for us
  273. */
  274. if (running) {
  275. /* get Node Address from hardware */
  276. mac4321 = readl(priv->xpec_base +
  277. NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
  278. mac65 = readl(priv->xpec_base +
  279. NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
  280. ndev->dev_addr[0] = mac4321 & 0xff;
  281. ndev->dev_addr[1] = (mac4321 >> 8) & 0xff;
  282. ndev->dev_addr[2] = (mac4321 >> 16) & 0xff;
  283. ndev->dev_addr[3] = (mac4321 >> 24) & 0xff;
  284. ndev->dev_addr[4] = mac65 & 0xff;
  285. ndev->dev_addr[5] = (mac65 >> 8) & 0xff;
  286. } else {
  287. if (xc_request_firmware(priv->xc)) {
  288. printk(CARDNAME ": requesting firmware failed\n");
  289. return -ENODEV;
  290. }
  291. }
  292. xc_reset(priv->xc);
  293. xc_start(priv->xc);
  294. if (!is_valid_ether_addr(ndev->dev_addr))
  295. printk("%s: Invalid ethernet MAC address. Please "
  296. "set using ifconfig\n", ndev->name);
  297. for (i=2; i<=18; i++)
  298. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  299. FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
  300. return register_netdev(ndev);
  301. }
  302. static int netx_eth_drv_probe(struct platform_device *pdev)
  303. {
  304. struct netx_eth_priv *priv;
  305. struct net_device *ndev;
  306. struct netxeth_platform_data *pdata;
  307. int ret;
  308. ndev = alloc_etherdev(sizeof (struct netx_eth_priv));
  309. if (!ndev) {
  310. printk("%s: could not allocate device.\n", CARDNAME);
  311. ret = -ENOMEM;
  312. goto exit;
  313. }
  314. SET_NETDEV_DEV(ndev, &pdev->dev);
  315. platform_set_drvdata(pdev, ndev);
  316. priv = netdev_priv(ndev);
  317. pdata = (struct netxeth_platform_data *)pdev->dev.platform_data;
  318. priv->xc = request_xc(pdata->xcno, &pdev->dev);
  319. if (!priv->xc) {
  320. dev_err(&pdev->dev, "unable to request xc engine\n");
  321. ret = -ENODEV;
  322. goto exit_free_netdev;
  323. }
  324. ndev->irq = priv->xc->irq;
  325. priv->id = pdev->id;
  326. priv->xpec_base = priv->xc->xpec_base;
  327. priv->xmac_base = priv->xc->xmac_base;
  328. priv->sram_base = priv->xc->sram_base;
  329. spin_lock_init(&priv->lock);
  330. ret = pfifo_request(PFIFO_MASK(priv->id));
  331. if (ret) {
  332. printk("unable to request PFIFO\n");
  333. goto exit_free_xc;
  334. }
  335. ret = netx_eth_enable(ndev);
  336. if (ret)
  337. goto exit_free_pfifo;
  338. return 0;
  339. exit_free_pfifo:
  340. pfifo_free(PFIFO_MASK(priv->id));
  341. exit_free_xc:
  342. free_xc(priv->xc);
  343. exit_free_netdev:
  344. platform_set_drvdata(pdev, NULL);
  345. free_netdev(ndev);
  346. exit:
  347. return ret;
  348. }
  349. static int netx_eth_drv_remove(struct platform_device *pdev)
  350. {
  351. struct net_device *ndev = dev_get_drvdata(&pdev->dev);
  352. struct netx_eth_priv *priv = netdev_priv(ndev);
  353. platform_set_drvdata(pdev, NULL);
  354. unregister_netdev(ndev);
  355. xc_stop(priv->xc);
  356. free_xc(priv->xc);
  357. free_netdev(ndev);
  358. pfifo_free(PFIFO_MASK(priv->id));
  359. return 0;
  360. }
  361. static int netx_eth_drv_suspend(struct platform_device *pdev, pm_message_t state)
  362. {
  363. dev_err(&pdev->dev, "suspend not implemented\n");
  364. return 0;
  365. }
  366. static int netx_eth_drv_resume(struct platform_device *pdev)
  367. {
  368. dev_err(&pdev->dev, "resume not implemented\n");
  369. return 0;
  370. }
  371. static struct platform_driver netx_eth_driver = {
  372. .probe = netx_eth_drv_probe,
  373. .remove = netx_eth_drv_remove,
  374. .suspend = netx_eth_drv_suspend,
  375. .resume = netx_eth_drv_resume,
  376. .driver = {
  377. .name = CARDNAME,
  378. .owner = THIS_MODULE,
  379. },
  380. };
  381. static int __init netx_eth_init(void)
  382. {
  383. unsigned int phy_control, val;
  384. printk("NetX Ethernet driver\n");
  385. phy_control = PHY_CONTROL_PHY_ADDRESS(INTERNAL_PHY_ADR>>1) |
  386. PHY_CONTROL_PHY1_MODE(PHY_MODE_ALL) |
  387. PHY_CONTROL_PHY1_AUTOMDIX |
  388. PHY_CONTROL_PHY1_EN |
  389. PHY_CONTROL_PHY0_MODE(PHY_MODE_ALL) |
  390. PHY_CONTROL_PHY0_AUTOMDIX |
  391. PHY_CONTROL_PHY0_EN |
  392. PHY_CONTROL_CLK_XLATIN;
  393. val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
  394. writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
  395. writel(phy_control | PHY_CONTROL_RESET, NETX_SYSTEM_PHY_CONTROL);
  396. udelay(100);
  397. val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
  398. writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
  399. writel(phy_control, NETX_SYSTEM_PHY_CONTROL);
  400. return platform_driver_register(&netx_eth_driver);
  401. }
  402. static void __exit netx_eth_cleanup(void)
  403. {
  404. platform_driver_unregister(&netx_eth_driver);
  405. }
  406. module_init(netx_eth_init);
  407. module_exit(netx_eth_cleanup);
  408. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  409. MODULE_LICENSE("GPL");
  410. MODULE_ALIAS("platform:" CARDNAME);
  411. MODULE_FIRMWARE("xc0.bin");
  412. MODULE_FIRMWARE("xc1.bin");
  413. MODULE_FIRMWARE("xc2.bin");