netx-eth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/interrupt.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 <linux/platform_data/eth-netx.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 = netdev_alloc_skb(ndev, len);
  131. if (unlikely(skb == NULL)) {
  132. ndev->stats.rx_dropped++;
  133. return;
  134. }
  135. data = skb_put(skb, len);
  136. memcpy_fromio(data, priv->sram_base + frameno * 1560, len);
  137. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  138. FIFO_PTR_SEGMENT(seg) | FIFO_PTR_FRAMENO(frameno));
  139. skb->protocol = eth_type_trans(skb, ndev);
  140. netif_rx(skb);
  141. ndev->stats.rx_packets++;
  142. ndev->stats.rx_bytes += len;
  143. }
  144. static irqreturn_t
  145. netx_eth_interrupt(int irq, void *dev_id)
  146. {
  147. struct net_device *ndev = dev_id;
  148. struct netx_eth_priv *priv = netdev_priv(ndev);
  149. int status;
  150. unsigned long flags;
  151. spin_lock_irqsave(&priv->lock, flags);
  152. status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
  153. while (status) {
  154. int fill_level;
  155. writel(status, NETX_PFIFO_XPEC_ISR(priv->id));
  156. if ((status & ISR_CON_HI) || (status & ISR_IND_HI))
  157. printk("%s: unexpected status: 0x%08x\n",
  158. __func__, status);
  159. fill_level =
  160. readl(NETX_PFIFO_FILL_LEVEL(IND_FIFO_PORT_LO(priv->id)));
  161. while (fill_level--)
  162. netx_eth_receive(ndev);
  163. if (status & ISR_CON_LO)
  164. netif_wake_queue(ndev);
  165. if (status & ISR_LINK_STATUS_CHANGE)
  166. mii_check_media(&priv->mii, netif_msg_link(priv), 1);
  167. status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
  168. }
  169. spin_unlock_irqrestore(&priv->lock, flags);
  170. return IRQ_HANDLED;
  171. }
  172. static int netx_eth_open(struct net_device *ndev)
  173. {
  174. struct netx_eth_priv *priv = netdev_priv(ndev);
  175. if (request_irq
  176. (ndev->irq, netx_eth_interrupt, IRQF_SHARED, ndev->name, ndev))
  177. return -EAGAIN;
  178. writel(ndev->dev_addr[0] |
  179. ndev->dev_addr[1]<<8 |
  180. ndev->dev_addr[2]<<16 |
  181. ndev->dev_addr[3]<<24,
  182. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
  183. writel(ndev->dev_addr[4] |
  184. ndev->dev_addr[5]<<8,
  185. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
  186. writel(LOCAL_CONFIG_LINK_STATUS_IRQ_EN |
  187. LOCAL_CONFIG_CON_LO_IRQ_EN |
  188. LOCAL_CONFIG_CON_HI_IRQ_EN |
  189. LOCAL_CONFIG_IND_LO_IRQ_EN |
  190. LOCAL_CONFIG_IND_HI_IRQ_EN,
  191. priv->xpec_base + NETX_XPEC_RAM_START_OFS +
  192. ETH_MAC_LOCAL_CONFIG);
  193. mii_check_media(&priv->mii, netif_msg_link(priv), 1);
  194. netif_start_queue(ndev);
  195. return 0;
  196. }
  197. static int netx_eth_close(struct net_device *ndev)
  198. {
  199. struct netx_eth_priv *priv = netdev_priv(ndev);
  200. netif_stop_queue(ndev);
  201. writel(0,
  202. priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_LOCAL_CONFIG);
  203. free_irq(ndev->irq, ndev);
  204. return 0;
  205. }
  206. static void netx_eth_timeout(struct net_device *ndev)
  207. {
  208. struct netx_eth_priv *priv = netdev_priv(ndev);
  209. int i;
  210. printk(KERN_ERR "%s: transmit timed out, resetting\n", ndev->name);
  211. spin_lock_irq(&priv->lock);
  212. xc_reset(priv->xc);
  213. xc_start(priv->xc);
  214. for (i=2; i<=18; i++)
  215. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  216. FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
  217. spin_unlock_irq(&priv->lock);
  218. netif_wake_queue(ndev);
  219. }
  220. static int
  221. netx_eth_phy_read(struct net_device *ndev, int phy_id, int reg)
  222. {
  223. unsigned int val;
  224. val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
  225. MIIMU_REGADDR(reg) | MIIMU_PHY_NRES;
  226. writel(val, NETX_MIIMU);
  227. while (readl(NETX_MIIMU) & MIIMU_SNRDY);
  228. return readl(NETX_MIIMU) >> 16;
  229. }
  230. static void
  231. netx_eth_phy_write(struct net_device *ndev, int phy_id, int reg, int value)
  232. {
  233. unsigned int val;
  234. val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
  235. MIIMU_REGADDR(reg) | MIIMU_PHY_NRES | MIIMU_OPMODE_WRITE |
  236. MIIMU_DATA(value);
  237. writel(val, NETX_MIIMU);
  238. while (readl(NETX_MIIMU) & MIIMU_SNRDY);
  239. }
  240. static const struct net_device_ops netx_eth_netdev_ops = {
  241. .ndo_open = netx_eth_open,
  242. .ndo_stop = netx_eth_close,
  243. .ndo_start_xmit = netx_eth_hard_start_xmit,
  244. .ndo_tx_timeout = netx_eth_timeout,
  245. .ndo_set_rx_mode = netx_eth_set_multicast_list,
  246. .ndo_change_mtu = eth_change_mtu,
  247. .ndo_validate_addr = eth_validate_addr,
  248. .ndo_set_mac_address = eth_mac_addr,
  249. };
  250. static int netx_eth_enable(struct net_device *ndev)
  251. {
  252. struct netx_eth_priv *priv = netdev_priv(ndev);
  253. unsigned int mac4321, mac65;
  254. int running, i;
  255. ndev->netdev_ops = &netx_eth_netdev_ops;
  256. ndev->watchdog_timeo = msecs_to_jiffies(5000);
  257. priv->msg_enable = NETIF_MSG_LINK;
  258. priv->mii.phy_id_mask = 0x1f;
  259. priv->mii.reg_num_mask = 0x1f;
  260. priv->mii.force_media = 0;
  261. priv->mii.full_duplex = 0;
  262. priv->mii.dev = ndev;
  263. priv->mii.mdio_read = netx_eth_phy_read;
  264. priv->mii.mdio_write = netx_eth_phy_write;
  265. priv->mii.phy_id = INTERNAL_PHY_ADR + priv->id;
  266. running = xc_running(priv->xc);
  267. xc_stop(priv->xc);
  268. /* if the xc engine is already running, assume the bootloader has
  269. * loaded the firmware for us
  270. */
  271. if (running) {
  272. /* get Node Address from hardware */
  273. mac4321 = readl(priv->xpec_base +
  274. NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
  275. mac65 = readl(priv->xpec_base +
  276. NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
  277. ndev->dev_addr[0] = mac4321 & 0xff;
  278. ndev->dev_addr[1] = (mac4321 >> 8) & 0xff;
  279. ndev->dev_addr[2] = (mac4321 >> 16) & 0xff;
  280. ndev->dev_addr[3] = (mac4321 >> 24) & 0xff;
  281. ndev->dev_addr[4] = mac65 & 0xff;
  282. ndev->dev_addr[5] = (mac65 >> 8) & 0xff;
  283. } else {
  284. if (xc_request_firmware(priv->xc)) {
  285. printk(CARDNAME ": requesting firmware failed\n");
  286. return -ENODEV;
  287. }
  288. }
  289. xc_reset(priv->xc);
  290. xc_start(priv->xc);
  291. if (!is_valid_ether_addr(ndev->dev_addr))
  292. printk("%s: Invalid ethernet MAC address. Please "
  293. "set using ifconfig\n", ndev->name);
  294. for (i=2; i<=18; i++)
  295. pfifo_push(EMPTY_PTR_FIFO(priv->id),
  296. FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
  297. return register_netdev(ndev);
  298. }
  299. static int netx_eth_drv_probe(struct platform_device *pdev)
  300. {
  301. struct netx_eth_priv *priv;
  302. struct net_device *ndev;
  303. struct netxeth_platform_data *pdata;
  304. int ret;
  305. ndev = alloc_etherdev(sizeof (struct netx_eth_priv));
  306. if (!ndev) {
  307. ret = -ENOMEM;
  308. goto exit;
  309. }
  310. SET_NETDEV_DEV(ndev, &pdev->dev);
  311. platform_set_drvdata(pdev, ndev);
  312. priv = netdev_priv(ndev);
  313. pdata = dev_get_platdata(&pdev->dev);
  314. priv->xc = request_xc(pdata->xcno, &pdev->dev);
  315. if (!priv->xc) {
  316. dev_err(&pdev->dev, "unable to request xc engine\n");
  317. ret = -ENODEV;
  318. goto exit_free_netdev;
  319. }
  320. ndev->irq = priv->xc->irq;
  321. priv->id = pdev->id;
  322. priv->xpec_base = priv->xc->xpec_base;
  323. priv->xmac_base = priv->xc->xmac_base;
  324. priv->sram_base = priv->xc->sram_base;
  325. spin_lock_init(&priv->lock);
  326. ret = pfifo_request(PFIFO_MASK(priv->id));
  327. if (ret) {
  328. printk("unable to request PFIFO\n");
  329. goto exit_free_xc;
  330. }
  331. ret = netx_eth_enable(ndev);
  332. if (ret)
  333. goto exit_free_pfifo;
  334. return 0;
  335. exit_free_pfifo:
  336. pfifo_free(PFIFO_MASK(priv->id));
  337. exit_free_xc:
  338. free_xc(priv->xc);
  339. exit_free_netdev:
  340. free_netdev(ndev);
  341. exit:
  342. return ret;
  343. }
  344. static int netx_eth_drv_remove(struct platform_device *pdev)
  345. {
  346. struct net_device *ndev = platform_get_drvdata(pdev);
  347. struct netx_eth_priv *priv = netdev_priv(ndev);
  348. unregister_netdev(ndev);
  349. xc_stop(priv->xc);
  350. free_xc(priv->xc);
  351. free_netdev(ndev);
  352. pfifo_free(PFIFO_MASK(priv->id));
  353. return 0;
  354. }
  355. static int netx_eth_drv_suspend(struct platform_device *pdev, pm_message_t state)
  356. {
  357. dev_err(&pdev->dev, "suspend not implemented\n");
  358. return 0;
  359. }
  360. static int netx_eth_drv_resume(struct platform_device *pdev)
  361. {
  362. dev_err(&pdev->dev, "resume not implemented\n");
  363. return 0;
  364. }
  365. static struct platform_driver netx_eth_driver = {
  366. .probe = netx_eth_drv_probe,
  367. .remove = netx_eth_drv_remove,
  368. .suspend = netx_eth_drv_suspend,
  369. .resume = netx_eth_drv_resume,
  370. .driver = {
  371. .name = CARDNAME,
  372. },
  373. };
  374. static int __init netx_eth_init(void)
  375. {
  376. unsigned int phy_control, val;
  377. printk("NetX Ethernet driver\n");
  378. phy_control = PHY_CONTROL_PHY_ADDRESS(INTERNAL_PHY_ADR>>1) |
  379. PHY_CONTROL_PHY1_MODE(PHY_MODE_ALL) |
  380. PHY_CONTROL_PHY1_AUTOMDIX |
  381. PHY_CONTROL_PHY1_EN |
  382. PHY_CONTROL_PHY0_MODE(PHY_MODE_ALL) |
  383. PHY_CONTROL_PHY0_AUTOMDIX |
  384. PHY_CONTROL_PHY0_EN |
  385. PHY_CONTROL_CLK_XLATIN;
  386. val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
  387. writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
  388. writel(phy_control | PHY_CONTROL_RESET, NETX_SYSTEM_PHY_CONTROL);
  389. udelay(100);
  390. val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
  391. writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
  392. writel(phy_control, NETX_SYSTEM_PHY_CONTROL);
  393. return platform_driver_register(&netx_eth_driver);
  394. }
  395. static void __exit netx_eth_cleanup(void)
  396. {
  397. platform_driver_unregister(&netx_eth_driver);
  398. }
  399. module_init(netx_eth_init);
  400. module_exit(netx_eth_cleanup);
  401. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  402. MODULE_LICENSE("GPL");
  403. MODULE_ALIAS("platform:" CARDNAME);
  404. MODULE_FIRMWARE("xc0.bin");
  405. MODULE_FIRMWARE("xc1.bin");
  406. MODULE_FIRMWARE("xc2.bin");