netx-eth.c 14 KB

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