sun4i-emac.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * Allwinner EMAC Fast Ethernet driver for Linux.
  3. *
  4. * Copyright 2012-2013 Stefan Roese <sr@denx.de>
  5. * Copyright 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * Based on the Linux driver provided by Allwinner:
  8. * Copyright (C) 1997 Sten Wang
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/gpio.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/mii.h>
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/of_net.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/phy.h>
  30. #include <linux/soc/sunxi/sunxi_sram.h>
  31. #include "sun4i-emac.h"
  32. #define DRV_NAME "sun4i-emac"
  33. #define DRV_VERSION "1.02"
  34. #define EMAC_MAX_FRAME_LEN 0x0600
  35. #define EMAC_DEFAULT_MSG_ENABLE 0x0000
  36. static int debug = -1; /* defaults above */;
  37. module_param(debug, int, 0);
  38. MODULE_PARM_DESC(debug, "debug message flags");
  39. /* Transmit timeout, default 5 seconds. */
  40. static int watchdog = 5000;
  41. module_param(watchdog, int, 0400);
  42. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  43. /* EMAC register address locking.
  44. *
  45. * The EMAC uses an address register to control where data written
  46. * to the data register goes. This means that the address register
  47. * must be preserved over interrupts or similar calls.
  48. *
  49. * During interrupt and other critical calls, a spinlock is used to
  50. * protect the system, but the calls themselves save the address
  51. * in the address register in case they are interrupting another
  52. * access to the device.
  53. *
  54. * For general accesses a lock is provided so that calls which are
  55. * allowed to sleep are serialised so that the address register does
  56. * not need to be saved. This lock also serves to serialise access
  57. * to the EEPROM and PHY access registers which are shared between
  58. * these two devices.
  59. */
  60. /* The driver supports the original EMACE, and now the two newer
  61. * devices, EMACA and EMACB.
  62. */
  63. struct emac_board_info {
  64. struct clk *clk;
  65. struct device *dev;
  66. struct platform_device *pdev;
  67. spinlock_t lock;
  68. void __iomem *membase;
  69. u32 msg_enable;
  70. struct net_device *ndev;
  71. struct sk_buff *skb_last;
  72. u16 tx_fifo_stat;
  73. int emacrx_completed_flag;
  74. struct device_node *phy_node;
  75. unsigned int link;
  76. unsigned int speed;
  77. unsigned int duplex;
  78. phy_interface_t phy_interface;
  79. };
  80. static void emac_update_speed(struct net_device *dev)
  81. {
  82. struct emac_board_info *db = netdev_priv(dev);
  83. unsigned int reg_val;
  84. /* set EMAC SPEED, depend on PHY */
  85. reg_val = readl(db->membase + EMAC_MAC_SUPP_REG);
  86. reg_val &= ~(0x1 << 8);
  87. if (db->speed == SPEED_100)
  88. reg_val |= 1 << 8;
  89. writel(reg_val, db->membase + EMAC_MAC_SUPP_REG);
  90. }
  91. static void emac_update_duplex(struct net_device *dev)
  92. {
  93. struct emac_board_info *db = netdev_priv(dev);
  94. unsigned int reg_val;
  95. /* set duplex depend on phy */
  96. reg_val = readl(db->membase + EMAC_MAC_CTL1_REG);
  97. reg_val &= ~EMAC_MAC_CTL1_DUPLEX_EN;
  98. if (db->duplex)
  99. reg_val |= EMAC_MAC_CTL1_DUPLEX_EN;
  100. writel(reg_val, db->membase + EMAC_MAC_CTL1_REG);
  101. }
  102. static void emac_handle_link_change(struct net_device *dev)
  103. {
  104. struct emac_board_info *db = netdev_priv(dev);
  105. struct phy_device *phydev = dev->phydev;
  106. unsigned long flags;
  107. int status_change = 0;
  108. if (phydev->link) {
  109. if (db->speed != phydev->speed) {
  110. spin_lock_irqsave(&db->lock, flags);
  111. db->speed = phydev->speed;
  112. emac_update_speed(dev);
  113. spin_unlock_irqrestore(&db->lock, flags);
  114. status_change = 1;
  115. }
  116. if (db->duplex != phydev->duplex) {
  117. spin_lock_irqsave(&db->lock, flags);
  118. db->duplex = phydev->duplex;
  119. emac_update_duplex(dev);
  120. spin_unlock_irqrestore(&db->lock, flags);
  121. status_change = 1;
  122. }
  123. }
  124. if (phydev->link != db->link) {
  125. if (!phydev->link) {
  126. db->speed = 0;
  127. db->duplex = -1;
  128. }
  129. db->link = phydev->link;
  130. status_change = 1;
  131. }
  132. if (status_change)
  133. phy_print_status(phydev);
  134. }
  135. static int emac_mdio_probe(struct net_device *dev)
  136. {
  137. struct emac_board_info *db = netdev_priv(dev);
  138. struct phy_device *phydev;
  139. /* to-do: PHY interrupts are currently not supported */
  140. /* attach the mac to the phy */
  141. phydev = of_phy_connect(db->ndev, db->phy_node,
  142. &emac_handle_link_change, 0,
  143. db->phy_interface);
  144. if (!phydev) {
  145. netdev_err(db->ndev, "could not find the PHY\n");
  146. return -ENODEV;
  147. }
  148. /* mask with MAC supported features */
  149. phy_set_max_speed(phydev, SPEED_100);
  150. db->link = 0;
  151. db->speed = 0;
  152. db->duplex = -1;
  153. return 0;
  154. }
  155. static void emac_mdio_remove(struct net_device *dev)
  156. {
  157. phy_disconnect(dev->phydev);
  158. }
  159. static void emac_reset(struct emac_board_info *db)
  160. {
  161. dev_dbg(db->dev, "resetting device\n");
  162. /* RESET device */
  163. writel(0, db->membase + EMAC_CTL_REG);
  164. udelay(200);
  165. writel(EMAC_CTL_RESET, db->membase + EMAC_CTL_REG);
  166. udelay(200);
  167. }
  168. static void emac_outblk_32bit(void __iomem *reg, void *data, int count)
  169. {
  170. writesl(reg, data, round_up(count, 4) / 4);
  171. }
  172. static void emac_inblk_32bit(void __iomem *reg, void *data, int count)
  173. {
  174. readsl(reg, data, round_up(count, 4) / 4);
  175. }
  176. static int emac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  177. {
  178. struct phy_device *phydev = dev->phydev;
  179. if (!netif_running(dev))
  180. return -EINVAL;
  181. if (!phydev)
  182. return -ENODEV;
  183. return phy_mii_ioctl(phydev, rq, cmd);
  184. }
  185. /* ethtool ops */
  186. static void emac_get_drvinfo(struct net_device *dev,
  187. struct ethtool_drvinfo *info)
  188. {
  189. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  190. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  191. strlcpy(info->bus_info, dev_name(&dev->dev), sizeof(info->bus_info));
  192. }
  193. static u32 emac_get_msglevel(struct net_device *dev)
  194. {
  195. struct emac_board_info *db = netdev_priv(dev);
  196. return db->msg_enable;
  197. }
  198. static void emac_set_msglevel(struct net_device *dev, u32 value)
  199. {
  200. struct emac_board_info *db = netdev_priv(dev);
  201. db->msg_enable = value;
  202. }
  203. static const struct ethtool_ops emac_ethtool_ops = {
  204. .get_drvinfo = emac_get_drvinfo,
  205. .get_link = ethtool_op_get_link,
  206. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  207. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  208. .get_msglevel = emac_get_msglevel,
  209. .set_msglevel = emac_set_msglevel,
  210. };
  211. static unsigned int emac_setup(struct net_device *ndev)
  212. {
  213. struct emac_board_info *db = netdev_priv(ndev);
  214. unsigned int reg_val;
  215. /* set up TX */
  216. reg_val = readl(db->membase + EMAC_TX_MODE_REG);
  217. writel(reg_val | EMAC_TX_MODE_ABORTED_FRAME_EN,
  218. db->membase + EMAC_TX_MODE_REG);
  219. /* set MAC */
  220. /* set MAC CTL0 */
  221. reg_val = readl(db->membase + EMAC_MAC_CTL0_REG);
  222. writel(reg_val | EMAC_MAC_CTL0_RX_FLOW_CTL_EN |
  223. EMAC_MAC_CTL0_TX_FLOW_CTL_EN,
  224. db->membase + EMAC_MAC_CTL0_REG);
  225. /* set MAC CTL1 */
  226. reg_val = readl(db->membase + EMAC_MAC_CTL1_REG);
  227. reg_val |= EMAC_MAC_CTL1_LEN_CHECK_EN;
  228. reg_val |= EMAC_MAC_CTL1_CRC_EN;
  229. reg_val |= EMAC_MAC_CTL1_PAD_EN;
  230. writel(reg_val, db->membase + EMAC_MAC_CTL1_REG);
  231. /* set up IPGT */
  232. writel(EMAC_MAC_IPGT_FULL_DUPLEX, db->membase + EMAC_MAC_IPGT_REG);
  233. /* set up IPGR */
  234. writel((EMAC_MAC_IPGR_IPG1 << 8) | EMAC_MAC_IPGR_IPG2,
  235. db->membase + EMAC_MAC_IPGR_REG);
  236. /* set up Collison window */
  237. writel((EMAC_MAC_CLRT_COLLISION_WINDOW << 8) | EMAC_MAC_CLRT_RM,
  238. db->membase + EMAC_MAC_CLRT_REG);
  239. /* set up Max Frame Length */
  240. writel(EMAC_MAX_FRAME_LEN,
  241. db->membase + EMAC_MAC_MAXF_REG);
  242. return 0;
  243. }
  244. static void emac_set_rx_mode(struct net_device *ndev)
  245. {
  246. struct emac_board_info *db = netdev_priv(ndev);
  247. unsigned int reg_val;
  248. /* set up RX */
  249. reg_val = readl(db->membase + EMAC_RX_CTL_REG);
  250. if (ndev->flags & IFF_PROMISC)
  251. reg_val |= EMAC_RX_CTL_PASS_ALL_EN;
  252. else
  253. reg_val &= ~EMAC_RX_CTL_PASS_ALL_EN;
  254. writel(reg_val | EMAC_RX_CTL_PASS_LEN_OOR_EN |
  255. EMAC_RX_CTL_ACCEPT_UNICAST_EN | EMAC_RX_CTL_DA_FILTER_EN |
  256. EMAC_RX_CTL_ACCEPT_MULTICAST_EN |
  257. EMAC_RX_CTL_ACCEPT_BROADCAST_EN,
  258. db->membase + EMAC_RX_CTL_REG);
  259. }
  260. static unsigned int emac_powerup(struct net_device *ndev)
  261. {
  262. struct emac_board_info *db = netdev_priv(ndev);
  263. unsigned int reg_val;
  264. /* initial EMAC */
  265. /* flush RX FIFO */
  266. reg_val = readl(db->membase + EMAC_RX_CTL_REG);
  267. reg_val |= 0x8;
  268. writel(reg_val, db->membase + EMAC_RX_CTL_REG);
  269. udelay(1);
  270. /* initial MAC */
  271. /* soft reset MAC */
  272. reg_val = readl(db->membase + EMAC_MAC_CTL0_REG);
  273. reg_val &= ~EMAC_MAC_CTL0_SOFT_RESET;
  274. writel(reg_val, db->membase + EMAC_MAC_CTL0_REG);
  275. /* set MII clock */
  276. reg_val = readl(db->membase + EMAC_MAC_MCFG_REG);
  277. reg_val &= (~(0xf << 2));
  278. reg_val |= (0xD << 2);
  279. writel(reg_val, db->membase + EMAC_MAC_MCFG_REG);
  280. /* clear RX counter */
  281. writel(0x0, db->membase + EMAC_RX_FBC_REG);
  282. /* disable all interrupt and clear interrupt status */
  283. writel(0, db->membase + EMAC_INT_CTL_REG);
  284. reg_val = readl(db->membase + EMAC_INT_STA_REG);
  285. writel(reg_val, db->membase + EMAC_INT_STA_REG);
  286. udelay(1);
  287. /* set up EMAC */
  288. emac_setup(ndev);
  289. /* set mac_address to chip */
  290. writel(ndev->dev_addr[0] << 16 | ndev->dev_addr[1] << 8 | ndev->
  291. dev_addr[2], db->membase + EMAC_MAC_A1_REG);
  292. writel(ndev->dev_addr[3] << 16 | ndev->dev_addr[4] << 8 | ndev->
  293. dev_addr[5], db->membase + EMAC_MAC_A0_REG);
  294. mdelay(1);
  295. return 0;
  296. }
  297. static int emac_set_mac_address(struct net_device *dev, void *p)
  298. {
  299. struct sockaddr *addr = p;
  300. struct emac_board_info *db = netdev_priv(dev);
  301. if (netif_running(dev))
  302. return -EBUSY;
  303. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  304. writel(dev->dev_addr[0] << 16 | dev->dev_addr[1] << 8 | dev->
  305. dev_addr[2], db->membase + EMAC_MAC_A1_REG);
  306. writel(dev->dev_addr[3] << 16 | dev->dev_addr[4] << 8 | dev->
  307. dev_addr[5], db->membase + EMAC_MAC_A0_REG);
  308. return 0;
  309. }
  310. /* Initialize emac board */
  311. static void emac_init_device(struct net_device *dev)
  312. {
  313. struct emac_board_info *db = netdev_priv(dev);
  314. unsigned long flags;
  315. unsigned int reg_val;
  316. spin_lock_irqsave(&db->lock, flags);
  317. emac_update_speed(dev);
  318. emac_update_duplex(dev);
  319. /* enable RX/TX */
  320. reg_val = readl(db->membase + EMAC_CTL_REG);
  321. writel(reg_val | EMAC_CTL_RESET | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN,
  322. db->membase + EMAC_CTL_REG);
  323. /* enable RX/TX0/RX Hlevel interrup */
  324. reg_val = readl(db->membase + EMAC_INT_CTL_REG);
  325. reg_val |= (0xf << 0) | (0x01 << 8);
  326. writel(reg_val, db->membase + EMAC_INT_CTL_REG);
  327. spin_unlock_irqrestore(&db->lock, flags);
  328. }
  329. /* Our watchdog timed out. Called by the networking layer */
  330. static void emac_timeout(struct net_device *dev)
  331. {
  332. struct emac_board_info *db = netdev_priv(dev);
  333. unsigned long flags;
  334. if (netif_msg_timer(db))
  335. dev_err(db->dev, "tx time out.\n");
  336. /* Save previous register address */
  337. spin_lock_irqsave(&db->lock, flags);
  338. netif_stop_queue(dev);
  339. emac_reset(db);
  340. emac_init_device(dev);
  341. /* We can accept TX packets again */
  342. netif_trans_update(dev);
  343. netif_wake_queue(dev);
  344. /* Restore previous register address */
  345. spin_unlock_irqrestore(&db->lock, flags);
  346. }
  347. /* Hardware start transmission.
  348. * Send a packet to media from the upper layer.
  349. */
  350. static netdev_tx_t emac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  351. {
  352. struct emac_board_info *db = netdev_priv(dev);
  353. unsigned long channel;
  354. unsigned long flags;
  355. channel = db->tx_fifo_stat & 3;
  356. if (channel == 3)
  357. return NETDEV_TX_BUSY;
  358. channel = (channel == 1 ? 1 : 0);
  359. spin_lock_irqsave(&db->lock, flags);
  360. writel(channel, db->membase + EMAC_TX_INS_REG);
  361. emac_outblk_32bit(db->membase + EMAC_TX_IO_DATA_REG,
  362. skb->data, skb->len);
  363. dev->stats.tx_bytes += skb->len;
  364. db->tx_fifo_stat |= 1 << channel;
  365. /* TX control: First packet immediately send, second packet queue */
  366. if (channel == 0) {
  367. /* set TX len */
  368. writel(skb->len, db->membase + EMAC_TX_PL0_REG);
  369. /* start translate from fifo to phy */
  370. writel(readl(db->membase + EMAC_TX_CTL0_REG) | 1,
  371. db->membase + EMAC_TX_CTL0_REG);
  372. /* save the time stamp */
  373. netif_trans_update(dev);
  374. } else if (channel == 1) {
  375. /* set TX len */
  376. writel(skb->len, db->membase + EMAC_TX_PL1_REG);
  377. /* start translate from fifo to phy */
  378. writel(readl(db->membase + EMAC_TX_CTL1_REG) | 1,
  379. db->membase + EMAC_TX_CTL1_REG);
  380. /* save the time stamp */
  381. netif_trans_update(dev);
  382. }
  383. if ((db->tx_fifo_stat & 3) == 3) {
  384. /* Second packet */
  385. netif_stop_queue(dev);
  386. }
  387. spin_unlock_irqrestore(&db->lock, flags);
  388. /* free this SKB */
  389. dev_consume_skb_any(skb);
  390. return NETDEV_TX_OK;
  391. }
  392. /* EMAC interrupt handler
  393. * receive the packet to upper layer, free the transmitted packet
  394. */
  395. static void emac_tx_done(struct net_device *dev, struct emac_board_info *db,
  396. unsigned int tx_status)
  397. {
  398. /* One packet sent complete */
  399. db->tx_fifo_stat &= ~(tx_status & 3);
  400. if (3 == (tx_status & 3))
  401. dev->stats.tx_packets += 2;
  402. else
  403. dev->stats.tx_packets++;
  404. if (netif_msg_tx_done(db))
  405. dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
  406. netif_wake_queue(dev);
  407. }
  408. /* Received a packet and pass to upper layer
  409. */
  410. static void emac_rx(struct net_device *dev)
  411. {
  412. struct emac_board_info *db = netdev_priv(dev);
  413. struct sk_buff *skb;
  414. u8 *rdptr;
  415. bool good_packet;
  416. static int rxlen_last;
  417. unsigned int reg_val;
  418. u32 rxhdr, rxstatus, rxcount, rxlen;
  419. /* Check packet ready or not */
  420. while (1) {
  421. /* race warning: the first packet might arrive with
  422. * the interrupts disabled, but the second will fix
  423. * it
  424. */
  425. rxcount = readl(db->membase + EMAC_RX_FBC_REG);
  426. if (netif_msg_rx_status(db))
  427. dev_dbg(db->dev, "RXCount: %x\n", rxcount);
  428. if ((db->skb_last != NULL) && (rxlen_last > 0)) {
  429. dev->stats.rx_bytes += rxlen_last;
  430. /* Pass to upper layer */
  431. db->skb_last->protocol = eth_type_trans(db->skb_last,
  432. dev);
  433. netif_rx(db->skb_last);
  434. dev->stats.rx_packets++;
  435. db->skb_last = NULL;
  436. rxlen_last = 0;
  437. reg_val = readl(db->membase + EMAC_RX_CTL_REG);
  438. reg_val &= ~EMAC_RX_CTL_DMA_EN;
  439. writel(reg_val, db->membase + EMAC_RX_CTL_REG);
  440. }
  441. if (!rxcount) {
  442. db->emacrx_completed_flag = 1;
  443. reg_val = readl(db->membase + EMAC_INT_CTL_REG);
  444. reg_val |= (0xf << 0) | (0x01 << 8);
  445. writel(reg_val, db->membase + EMAC_INT_CTL_REG);
  446. /* had one stuck? */
  447. rxcount = readl(db->membase + EMAC_RX_FBC_REG);
  448. if (!rxcount)
  449. return;
  450. }
  451. reg_val = readl(db->membase + EMAC_RX_IO_DATA_REG);
  452. if (netif_msg_rx_status(db))
  453. dev_dbg(db->dev, "receive header: %x\n", reg_val);
  454. if (reg_val != EMAC_UNDOCUMENTED_MAGIC) {
  455. /* disable RX */
  456. reg_val = readl(db->membase + EMAC_CTL_REG);
  457. writel(reg_val & ~EMAC_CTL_RX_EN,
  458. db->membase + EMAC_CTL_REG);
  459. /* Flush RX FIFO */
  460. reg_val = readl(db->membase + EMAC_RX_CTL_REG);
  461. writel(reg_val | (1 << 3),
  462. db->membase + EMAC_RX_CTL_REG);
  463. do {
  464. reg_val = readl(db->membase + EMAC_RX_CTL_REG);
  465. } while (reg_val & (1 << 3));
  466. /* enable RX */
  467. reg_val = readl(db->membase + EMAC_CTL_REG);
  468. writel(reg_val | EMAC_CTL_RX_EN,
  469. db->membase + EMAC_CTL_REG);
  470. reg_val = readl(db->membase + EMAC_INT_CTL_REG);
  471. reg_val |= (0xf << 0) | (0x01 << 8);
  472. writel(reg_val, db->membase + EMAC_INT_CTL_REG);
  473. db->emacrx_completed_flag = 1;
  474. return;
  475. }
  476. /* A packet ready now & Get status/length */
  477. good_packet = true;
  478. rxhdr = readl(db->membase + EMAC_RX_IO_DATA_REG);
  479. if (netif_msg_rx_status(db))
  480. dev_dbg(db->dev, "rxhdr: %x\n", *((int *)(&rxhdr)));
  481. rxlen = EMAC_RX_IO_DATA_LEN(rxhdr);
  482. rxstatus = EMAC_RX_IO_DATA_STATUS(rxhdr);
  483. if (netif_msg_rx_status(db))
  484. dev_dbg(db->dev, "RX: status %02x, length %04x\n",
  485. rxstatus, rxlen);
  486. /* Packet Status check */
  487. if (rxlen < 0x40) {
  488. good_packet = false;
  489. if (netif_msg_rx_err(db))
  490. dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
  491. }
  492. if (unlikely(!(rxstatus & EMAC_RX_IO_DATA_STATUS_OK))) {
  493. good_packet = false;
  494. if (rxstatus & EMAC_RX_IO_DATA_STATUS_CRC_ERR) {
  495. if (netif_msg_rx_err(db))
  496. dev_dbg(db->dev, "crc error\n");
  497. dev->stats.rx_crc_errors++;
  498. }
  499. if (rxstatus & EMAC_RX_IO_DATA_STATUS_LEN_ERR) {
  500. if (netif_msg_rx_err(db))
  501. dev_dbg(db->dev, "length error\n");
  502. dev->stats.rx_length_errors++;
  503. }
  504. }
  505. /* Move data from EMAC */
  506. if (good_packet) {
  507. skb = netdev_alloc_skb(dev, rxlen + 4);
  508. if (!skb)
  509. continue;
  510. skb_reserve(skb, 2);
  511. rdptr = skb_put(skb, rxlen - 4);
  512. /* Read received packet from RX SRAM */
  513. if (netif_msg_rx_status(db))
  514. dev_dbg(db->dev, "RxLen %x\n", rxlen);
  515. emac_inblk_32bit(db->membase + EMAC_RX_IO_DATA_REG,
  516. rdptr, rxlen);
  517. dev->stats.rx_bytes += rxlen;
  518. /* Pass to upper layer */
  519. skb->protocol = eth_type_trans(skb, dev);
  520. netif_rx(skb);
  521. dev->stats.rx_packets++;
  522. }
  523. }
  524. }
  525. static irqreturn_t emac_interrupt(int irq, void *dev_id)
  526. {
  527. struct net_device *dev = dev_id;
  528. struct emac_board_info *db = netdev_priv(dev);
  529. int int_status;
  530. unsigned long flags;
  531. unsigned int reg_val;
  532. /* A real interrupt coming */
  533. /* holders of db->lock must always block IRQs */
  534. spin_lock_irqsave(&db->lock, flags);
  535. /* Disable all interrupts */
  536. writel(0, db->membase + EMAC_INT_CTL_REG);
  537. /* Got EMAC interrupt status */
  538. /* Got ISR */
  539. int_status = readl(db->membase + EMAC_INT_STA_REG);
  540. /* Clear ISR status */
  541. writel(int_status, db->membase + EMAC_INT_STA_REG);
  542. if (netif_msg_intr(db))
  543. dev_dbg(db->dev, "emac interrupt %02x\n", int_status);
  544. /* Received the coming packet */
  545. if ((int_status & 0x100) && (db->emacrx_completed_flag == 1)) {
  546. /* carrier lost */
  547. db->emacrx_completed_flag = 0;
  548. emac_rx(dev);
  549. }
  550. /* Transmit Interrupt check */
  551. if (int_status & (0x01 | 0x02))
  552. emac_tx_done(dev, db, int_status);
  553. if (int_status & (0x04 | 0x08))
  554. netdev_info(dev, " ab : %x\n", int_status);
  555. /* Re-enable interrupt mask */
  556. if (db->emacrx_completed_flag == 1) {
  557. reg_val = readl(db->membase + EMAC_INT_CTL_REG);
  558. reg_val |= (0xf << 0) | (0x01 << 8);
  559. writel(reg_val, db->membase + EMAC_INT_CTL_REG);
  560. }
  561. spin_unlock_irqrestore(&db->lock, flags);
  562. return IRQ_HANDLED;
  563. }
  564. #ifdef CONFIG_NET_POLL_CONTROLLER
  565. /*
  566. * Used by netconsole
  567. */
  568. static void emac_poll_controller(struct net_device *dev)
  569. {
  570. disable_irq(dev->irq);
  571. emac_interrupt(dev->irq, dev);
  572. enable_irq(dev->irq);
  573. }
  574. #endif
  575. /* Open the interface.
  576. * The interface is opened whenever "ifconfig" actives it.
  577. */
  578. static int emac_open(struct net_device *dev)
  579. {
  580. struct emac_board_info *db = netdev_priv(dev);
  581. int ret;
  582. if (netif_msg_ifup(db))
  583. dev_dbg(db->dev, "enabling %s\n", dev->name);
  584. if (request_irq(dev->irq, &emac_interrupt, 0, dev->name, dev))
  585. return -EAGAIN;
  586. /* Initialize EMAC board */
  587. emac_reset(db);
  588. emac_init_device(dev);
  589. ret = emac_mdio_probe(dev);
  590. if (ret < 0) {
  591. free_irq(dev->irq, dev);
  592. netdev_err(dev, "cannot probe MDIO bus\n");
  593. return ret;
  594. }
  595. phy_start(dev->phydev);
  596. netif_start_queue(dev);
  597. return 0;
  598. }
  599. static void emac_shutdown(struct net_device *dev)
  600. {
  601. unsigned int reg_val;
  602. struct emac_board_info *db = netdev_priv(dev);
  603. /* Disable all interrupt */
  604. writel(0, db->membase + EMAC_INT_CTL_REG);
  605. /* clear interrupt status */
  606. reg_val = readl(db->membase + EMAC_INT_STA_REG);
  607. writel(reg_val, db->membase + EMAC_INT_STA_REG);
  608. /* Disable RX/TX */
  609. reg_val = readl(db->membase + EMAC_CTL_REG);
  610. reg_val &= ~(EMAC_CTL_TX_EN | EMAC_CTL_RX_EN | EMAC_CTL_RESET);
  611. writel(reg_val, db->membase + EMAC_CTL_REG);
  612. }
  613. /* Stop the interface.
  614. * The interface is stopped when it is brought.
  615. */
  616. static int emac_stop(struct net_device *ndev)
  617. {
  618. struct emac_board_info *db = netdev_priv(ndev);
  619. if (netif_msg_ifdown(db))
  620. dev_dbg(db->dev, "shutting down %s\n", ndev->name);
  621. netif_stop_queue(ndev);
  622. netif_carrier_off(ndev);
  623. phy_stop(ndev->phydev);
  624. emac_mdio_remove(ndev);
  625. emac_shutdown(ndev);
  626. free_irq(ndev->irq, ndev);
  627. return 0;
  628. }
  629. static const struct net_device_ops emac_netdev_ops = {
  630. .ndo_open = emac_open,
  631. .ndo_stop = emac_stop,
  632. .ndo_start_xmit = emac_start_xmit,
  633. .ndo_tx_timeout = emac_timeout,
  634. .ndo_set_rx_mode = emac_set_rx_mode,
  635. .ndo_do_ioctl = emac_ioctl,
  636. .ndo_validate_addr = eth_validate_addr,
  637. .ndo_set_mac_address = emac_set_mac_address,
  638. #ifdef CONFIG_NET_POLL_CONTROLLER
  639. .ndo_poll_controller = emac_poll_controller,
  640. #endif
  641. };
  642. /* Search EMAC board, allocate space and register it
  643. */
  644. static int emac_probe(struct platform_device *pdev)
  645. {
  646. struct device_node *np = pdev->dev.of_node;
  647. struct emac_board_info *db;
  648. struct net_device *ndev;
  649. int ret = 0;
  650. const char *mac_addr;
  651. ndev = alloc_etherdev(sizeof(struct emac_board_info));
  652. if (!ndev) {
  653. dev_err(&pdev->dev, "could not allocate device.\n");
  654. return -ENOMEM;
  655. }
  656. SET_NETDEV_DEV(ndev, &pdev->dev);
  657. db = netdev_priv(ndev);
  658. db->dev = &pdev->dev;
  659. db->ndev = ndev;
  660. db->pdev = pdev;
  661. db->msg_enable = netif_msg_init(debug, EMAC_DEFAULT_MSG_ENABLE);
  662. spin_lock_init(&db->lock);
  663. db->membase = of_iomap(np, 0);
  664. if (!db->membase) {
  665. dev_err(&pdev->dev, "failed to remap registers\n");
  666. ret = -ENOMEM;
  667. goto out;
  668. }
  669. /* fill in parameters for net-dev structure */
  670. ndev->base_addr = (unsigned long)db->membase;
  671. ndev->irq = irq_of_parse_and_map(np, 0);
  672. if (ndev->irq == -ENXIO) {
  673. netdev_err(ndev, "No irq resource\n");
  674. ret = ndev->irq;
  675. goto out_iounmap;
  676. }
  677. db->clk = devm_clk_get(&pdev->dev, NULL);
  678. if (IS_ERR(db->clk)) {
  679. ret = PTR_ERR(db->clk);
  680. goto out_dispose_mapping;
  681. }
  682. ret = clk_prepare_enable(db->clk);
  683. if (ret) {
  684. dev_err(&pdev->dev, "Error couldn't enable clock (%d)\n", ret);
  685. goto out_dispose_mapping;
  686. }
  687. ret = sunxi_sram_claim(&pdev->dev);
  688. if (ret) {
  689. dev_err(&pdev->dev, "Error couldn't map SRAM to device\n");
  690. goto out_clk_disable_unprepare;
  691. }
  692. db->phy_node = of_parse_phandle(np, "phy-handle", 0);
  693. if (!db->phy_node)
  694. db->phy_node = of_parse_phandle(np, "phy", 0);
  695. if (!db->phy_node) {
  696. dev_err(&pdev->dev, "no associated PHY\n");
  697. ret = -ENODEV;
  698. goto out_release_sram;
  699. }
  700. /* Read MAC-address from DT */
  701. mac_addr = of_get_mac_address(np);
  702. if (!IS_ERR(mac_addr))
  703. ether_addr_copy(ndev->dev_addr, mac_addr);
  704. /* Check if the MAC address is valid, if not get a random one */
  705. if (!is_valid_ether_addr(ndev->dev_addr)) {
  706. eth_hw_addr_random(ndev);
  707. dev_warn(&pdev->dev, "using random MAC address %pM\n",
  708. ndev->dev_addr);
  709. }
  710. db->emacrx_completed_flag = 1;
  711. emac_powerup(ndev);
  712. emac_reset(db);
  713. ndev->netdev_ops = &emac_netdev_ops;
  714. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  715. ndev->ethtool_ops = &emac_ethtool_ops;
  716. platform_set_drvdata(pdev, ndev);
  717. /* Carrier starts down, phylib will bring it up */
  718. netif_carrier_off(ndev);
  719. ret = register_netdev(ndev);
  720. if (ret) {
  721. dev_err(&pdev->dev, "Registering netdev failed!\n");
  722. ret = -ENODEV;
  723. goto out_release_sram;
  724. }
  725. dev_info(&pdev->dev, "%s: at %p, IRQ %d MAC: %pM\n",
  726. ndev->name, db->membase, ndev->irq, ndev->dev_addr);
  727. return 0;
  728. out_release_sram:
  729. sunxi_sram_release(&pdev->dev);
  730. out_clk_disable_unprepare:
  731. clk_disable_unprepare(db->clk);
  732. out_dispose_mapping:
  733. irq_dispose_mapping(ndev->irq);
  734. out_iounmap:
  735. iounmap(db->membase);
  736. out:
  737. dev_err(db->dev, "not found (%d).\n", ret);
  738. free_netdev(ndev);
  739. return ret;
  740. }
  741. static int emac_remove(struct platform_device *pdev)
  742. {
  743. struct net_device *ndev = platform_get_drvdata(pdev);
  744. struct emac_board_info *db = netdev_priv(ndev);
  745. unregister_netdev(ndev);
  746. sunxi_sram_release(&pdev->dev);
  747. clk_disable_unprepare(db->clk);
  748. irq_dispose_mapping(ndev->irq);
  749. iounmap(db->membase);
  750. free_netdev(ndev);
  751. dev_dbg(&pdev->dev, "released and freed device\n");
  752. return 0;
  753. }
  754. static int emac_suspend(struct platform_device *dev, pm_message_t state)
  755. {
  756. struct net_device *ndev = platform_get_drvdata(dev);
  757. netif_carrier_off(ndev);
  758. netif_device_detach(ndev);
  759. emac_shutdown(ndev);
  760. return 0;
  761. }
  762. static int emac_resume(struct platform_device *dev)
  763. {
  764. struct net_device *ndev = platform_get_drvdata(dev);
  765. struct emac_board_info *db = netdev_priv(ndev);
  766. emac_reset(db);
  767. emac_init_device(ndev);
  768. netif_device_attach(ndev);
  769. return 0;
  770. }
  771. static const struct of_device_id emac_of_match[] = {
  772. {.compatible = "allwinner,sun4i-a10-emac",},
  773. /* Deprecated */
  774. {.compatible = "allwinner,sun4i-emac",},
  775. {},
  776. };
  777. MODULE_DEVICE_TABLE(of, emac_of_match);
  778. static struct platform_driver emac_driver = {
  779. .driver = {
  780. .name = "sun4i-emac",
  781. .of_match_table = emac_of_match,
  782. },
  783. .probe = emac_probe,
  784. .remove = emac_remove,
  785. .suspend = emac_suspend,
  786. .resume = emac_resume,
  787. };
  788. module_platform_driver(emac_driver);
  789. MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
  790. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  791. MODULE_DESCRIPTION("Allwinner A10 emac network driver");
  792. MODULE_LICENSE("GPL");