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
15 #define DEBUG
16 #include <linux/clk.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/gpio.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/mii.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_mdio.h>
28 #include <linux/of_net.h>
29 #include <linux/of_platform.h>
30 #include <linux/platform_device.h>
31 #include <linux/phy.h>
32 #include <linux/of_gpio.h>
33 #include <linux/io.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/soc/sunxi/sunxi_sram.h>
36 #include <linux/reset.h>
37
38 #define DRV_NAME "sun4i-emac"
39 #define DRV_VERSION "1.02"
40
41 #define EMAC_MAX_FRAME_LEN 0x0600
42
43 #define EMAC_POWER_CHAN_NUM 3
44 #define EMAC_POWER_CHAR_LENGTH 20
45
46 #define EMAC_CTL_REG (0x00)
47 #define EMAC_CTL_RESET (1 << 0)
48 #define EMAC_CTL_TX_EN (1 << 1)
49 #define EMAC_CTL_RX_EN (1 << 2)
50 #define EMAC_TX_MODE_REG (0x04)
51 #define EMAC_TX_MODE_ABORTED_FRAME_EN (1 << 0)
52 #define EMAC_TX_MODE_DMA_EN (1 << 1)
53 #define EMAC_TX_FLOW_REG (0x08)
54 #define EMAC_TX_CTL0_REG (0x0c)
55 #define EMAC_TX_CTL1_REG (0x10)
56 #define EMAC_TX_INS_REG (0x14)
57 #define EMAC_TX_PL0_REG (0x18)
58 #define EMAC_TX_PL1_REG (0x1c)
59 #define EMAC_TX_STA_REG (0x20)
60 #define EMAC_TX_IO_DATA_REG (0x24)
61 #define EMAC_TX_IO_DATA1_REG (0x28)
62 #define EMAC_TX_TSVL0_REG (0x2c)
63 #define EMAC_TX_TSVH0_REG (0x30)
64 #define EMAC_TX_TSVL1_REG (0x34)
65 #define EMAC_TX_TSVH1_REG (0x38)
66 #define EMAC_RX_CTL_REG (0x3c)
67 #define EMAC_RX_CTL_AUTO_DRQ_EN (1 << 1)
68 #define EMAC_RX_CTL_DMA_EN (1 << 2)
69 #define EMAC_RX_CTL_PASS_ALL_EN (1 << 4)
70 #define EMAC_RX_CTL_PASS_CTL_EN (1 << 5)
71 #define EMAC_RX_CTL_PASS_CRC_ERR_EN (1 << 6)
72 #define EMAC_RX_CTL_PASS_LEN_ERR_EN (1 << 7)
73 #define EMAC_RX_CTL_PASS_LEN_OOR_EN (1 << 8)
74 #define EMAC_RX_CTL_ACCEPT_UNICAST_EN (1 << 16)
75 #define EMAC_RX_CTL_DA_FILTER_EN (1 << 17)
76 #define EMAC_RX_CTL_ACCEPT_MULTICAST_EN (1 << 20)
77 #define EMAC_RX_CTL_HASH_FILTER_EN (1 << 21)
78 #define EMAC_RX_CTL_ACCEPT_BROADCAST_EN (1 << 22)
79 #define EMAC_RX_CTL_SA_FILTER_EN (1 << 24)
80 #define EMAC_RX_CTL_SA_FILTER_INVERT_EN (1 << 25)
81 #define EMAC_RX_HASH0_REG (0x40)
82 #define EMAC_RX_HASH1_REG (0x44)
83 #define EMAC_RX_STA_REG (0x48)
84 #define EMAC_RX_IO_DATA_REG (0x4c)
85 #define EMAC_RX_IO_DATA_LEN(x) (x & 0xffff)
86 #define EMAC_RX_IO_DATA_STATUS(x) ((x >> 16) & 0xffff)
87 #define EMAC_RX_IO_DATA_STATUS_CRC_ERR (1 << 4)
88 #define EMAC_RX_IO_DATA_STATUS_LEN_ERR (3 << 5)
89 #define EMAC_RX_IO_DATA_STATUS_OK (1 << 7)
90 #define EMAC_RX_FBC_REG (0x50)
91 #define EMAC_INT_CTL_REG (0x54)
92 #define EMAC_INT_STA_REG (0x58)
93 #define EMAC_MAC_CTL0_REG (0x5c)
94 #define EMAC_MAC_CTL0_RX_FLOW_CTL_EN (1 << 2)
95 #define EMAC_MAC_CTL0_TX_FLOW_CTL_EN (1 << 3)
96 #define EMAC_MAC_CTL0_SOFT_RESET (1 << 15)
97 #define EMAC_MAC_CTL1_REG (0x60)
98 #define EMAC_MAC_CTL1_DUPLEX_EN (1 << 0)
99 #define EMAC_MAC_CTL1_LEN_CHECK_EN (1 << 1)
100 #define EMAC_MAC_CTL1_HUGE_FRAME_EN (1 << 2)
101 #define EMAC_MAC_CTL1_DELAYED_CRC_EN (1 << 3)
102 #define EMAC_MAC_CTL1_CRC_EN (1 << 4)
103 #define EMAC_MAC_CTL1_PAD_EN (1 << 5)
104 #define EMAC_MAC_CTL1_PAD_CRC_EN (1 << 6)
105 #define EMAC_MAC_CTL1_AD_SHORT_FRAME_EN (1 << 7)
106 #define EMAC_MAC_CTL1_BACKOFF_DIS (1 << 12)
107 #define EMAC_MAC_IPGT_REG (0x64)
108 #define EMAC_MAC_IPGT_HALF_DUPLEX (0x12)
109 #define EMAC_MAC_IPGT_FULL_DUPLEX (0x15)
110 #define EMAC_MAC_IPGR_REG (0x68)
111 #define EMAC_MAC_IPGR_IPG1 (0x0c)
112 #define EMAC_MAC_IPGR_IPG2 (0x12)
113 #define EMAC_MAC_CLRT_REG (0x6c)
114 #define EMAC_MAC_CLRT_COLLISION_WINDOW (0x37)
115 #define EMAC_MAC_CLRT_RM (0x0f)
116 #define EMAC_MAC_MAXF_REG (0x70)
117 #define EMAC_MAC_SUPP_REG (0x74)
118 #define EMAC_MAC_TEST_REG (0x78)
119 #define EMAC_MAC_MCFG_REG (0x7c)
120 #define EMAC_MAC_A0_REG (0x98)
121 #define EMAC_MAC_A1_REG (0x9c)
122 #define EMAC_MAC_A2_REG (0xa0)
123 #define EMAC_SAFX_L_REG0 (0xa4)
124 #define EMAC_SAFX_H_REG0 (0xa8)
125 #define EMAC_SAFX_L_REG1 (0xac)
126 #define EMAC_SAFX_H_REG1 (0xb0)
127 #define EMAC_SAFX_L_REG2 (0xb4)
128 #define EMAC_SAFX_H_REG2 (0xb8)
129 #define EMAC_SAFX_L_REG3 (0xbc)
130 #define EMAC_SAFX_H_REG3 (0xc0)
131
132 #define EMAC_PHY_DUPLEX (1 << 8)
133
134 #define EMAC_EEPROM_MAGIC (0x444d394b)
135 #define EMAC_UNDOCUMENTED_MAGIC (0x0143414d)
136
137 /********************************************************/
138
139 #define SRAM_MAP_EMAC_LOCAL
140
141 #ifdef SRAM_MAP_EMAC_LOCAL
142 #define EMAC_SRAM_MAP_BASE (0x01c00004)
143 #define EMAC_SRAM_MAP_LEN (4)
144 void __iomem *sram_map_emac_base;
145 #endif /* end of SRAM_MAP_EMAC_LOCAL*/
146 /********************************************************/
147
148 #define EMAC_DEFAULT_MSG_ENABLE 0x0000
149 static int debug = -1; /* defaults above */
150 module_param(debug, int, 0);
151 MODULE_PARM_DESC(debug, "debug message flags");
152
153 /* Transmit timeout, default 5 seconds. */
154 static int watchdog = 5000;
155 module_param(watchdog, int, 0400);
156 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
157
158 /* EMAC register address locking.
159 *
160 * The EMAC uses an address register to control where data written
161 * to the data register goes. This means that the address register
162 * must be preserved over interrupts or similar calls.
163 *
164 * During interrupt and other critical calls, a spinlock is used to
165 * protect the system, but the calls themselves save the address
166 * in the address register in case they are interrupting another
167 * access to the device.
168 *
169 * For general accesses a lock is provided so that calls which are
170 * allowed to sleep are serialised so that the address register does
171 * not need to be saved. This lock also serves to serialise access
172 * to the EEPROM and PHY access registers which are shared between
173 * these two devices.
174 */
175
176 /* The driver supports the original EMACE, and now the two newer
177 * devices, EMACA and EMACB.
178 */
179
180 struct emac_board_info {
181 struct clk *clk;
182 struct device *dev;
183 struct platform_device *pdev;
184 raw_spinlock_t lock;
185 void __iomem *membase;
186 u32 msg_enable;
187 struct net_device *ndev;
188 struct sk_buff *skb_last;
189 u16 tx_fifo_stat;
190
191 int emacrx_completed_flag;
192
193 struct phy_device *phy_dev;
194 struct device_node *phy_node;
195 unsigned int link;
196 unsigned int speed;
197 unsigned int duplex;
198
199 phy_interface_t phy_interface;
200 struct reset_control *reset;
201
202 struct regulator *emac_supply[EMAC_POWER_CHAN_NUM];
203 u32 emac_supply_vol[EMAC_POWER_CHAN_NUM];
204 int phyrst;
205 u8 rst_active_low;
206 };
207
208 struct emac_power {
209 unsigned int vol;
210 const char *name;
211 };
212
emac_power_on(struct emac_board_info * priv)213 static int emac_power_on(struct emac_board_info *priv)
214 {
215 int i;
216
217 if (gpio_is_valid(priv->phyrst))
218 gpio_direction_output(priv->phyrst, priv->rst_active_low);
219
220 /* Set the voltage */
221 for (i = 0; i < EMAC_POWER_CHAN_NUM; i++) {
222 if (IS_ERR_OR_NULL(priv->emac_supply[i]))
223 continue;
224
225 if (regulator_set_voltage(priv->emac_supply[i],
226 priv->emac_supply_vol[i],
227 priv->emac_supply_vol[i])) {
228 pr_err("emac-power%d set voltage error\n", i);
229 return -EINVAL;
230 }
231
232 if (regulator_enable(priv->emac_supply[i])) {
233 pr_err("emac-power%d enable error\n", i);
234 return -EINVAL;
235 }
236 mdelay(3);
237 }
238
239 msleep(300);
240
241 /* If configure gpio to reset the phy device, we should reset it. */
242 if (gpio_is_valid(priv->phyrst)) {
243 msleep(20);
244 gpio_direction_output(priv->phyrst, !priv->rst_active_low);
245 msleep(20);
246 }
247
248 return 0;
249 }
250
emac_power_off(struct emac_board_info * priv)251 static void emac_power_off(struct emac_board_info *priv)
252 {
253 int i;
254
255 for (i = 0; i < EMAC_POWER_CHAN_NUM; i++) {
256 regulator_disable(priv->emac_supply[i]);
257 regulator_put(priv->emac_supply[i]);
258 }
259 }
260
emac_update_speed(struct net_device * dev)261 static void emac_update_speed(struct net_device *dev)
262 {
263 struct emac_board_info *db = netdev_priv(dev);
264 unsigned int reg_val;
265
266 /* set EMAC SPEED, depend on PHY */
267 reg_val = readl(db->membase + EMAC_MAC_SUPP_REG);
268 reg_val &= ~(0x1 << 8);
269 if (db->speed == SPEED_100)
270 reg_val |= 1 << 8;
271 writel(reg_val, db->membase + EMAC_MAC_SUPP_REG);
272 }
273
emac_update_duplex(struct net_device * dev)274 static void emac_update_duplex(struct net_device *dev)
275 {
276 struct emac_board_info *db = netdev_priv(dev);
277 unsigned int reg_val;
278
279 /* set duplex depend on phy */
280 reg_val = readl(db->membase + EMAC_MAC_CTL1_REG);
281 reg_val &= ~EMAC_MAC_CTL1_DUPLEX_EN;
282 if (db->duplex)
283 reg_val |= EMAC_MAC_CTL1_DUPLEX_EN;
284 writel(reg_val, db->membase + EMAC_MAC_CTL1_REG);
285 }
286
emac_handle_link_change(struct net_device * dev)287 static void emac_handle_link_change(struct net_device *dev)
288 {
289 struct emac_board_info *db = netdev_priv(dev);
290 struct phy_device *phydev = db->phy_dev;
291 unsigned long flags;
292 int status_change = 0;
293
294 if (phydev->link) {
295 if (db->speed != phydev->speed) {
296 raw_spin_lock_irqsave(&db->lock, flags);
297 db->speed = phydev->speed;
298 emac_update_speed(dev);
299 raw_spin_unlock_irqrestore(&db->lock, flags);
300 status_change = 1;
301 }
302
303 if (db->duplex != phydev->duplex) {
304 raw_spin_lock_irqsave(&db->lock, flags);
305 db->duplex = phydev->duplex;
306 emac_update_duplex(dev);
307 raw_spin_unlock_irqrestore(&db->lock, flags);
308 status_change = 1;
309 }
310 }
311
312 if (phydev->link != db->link) {
313 if (!phydev->link) {
314 db->speed = 0;
315 db->duplex = -1;
316 }
317 db->link = phydev->link;
318
319 status_change = 1;
320 }
321
322 if (status_change)
323 phy_print_status(phydev);
324 }
325
emac_mdio_probe(struct net_device * dev)326 static int emac_mdio_probe(struct net_device *dev)
327 {
328 struct emac_board_info *db = netdev_priv(dev);
329
330 /* to-do: PHY interrupts are currently not supported */
331
332 /* attach the mac to the phy */
333 db->phy_dev = of_phy_connect(db->ndev, db->phy_node,
334 &emac_handle_link_change, 0,
335 db->phy_interface);
336 if (!db->phy_dev) {
337 netdev_err(db->ndev, "could not find the PHY\n");
338 return -ENODEV;
339 }
340
341 /* mask with MAC supported features */
342 phy_set_max_speed(db->phy_dev, SPEED_100);
343
344 db->link = 0;
345 db->speed = 0;
346 db->duplex = -1;
347
348 return 0;
349 }
350
emac_mdio_remove(struct net_device * dev)351 static void emac_mdio_remove(struct net_device *dev)
352 {
353 struct emac_board_info *db = netdev_priv(dev);
354
355 phy_disconnect(db->phy_dev);
356 db->phy_dev = NULL;
357 }
358
emac_reset(struct emac_board_info * db)359 static void emac_reset(struct emac_board_info *db)
360 {
361 dev_dbg(db->dev, "resetting device\n");
362
363 /* RESET device */
364 writel(0, db->membase + EMAC_CTL_REG);
365 udelay(200);
366 writel(EMAC_CTL_RESET, db->membase + EMAC_CTL_REG);
367 udelay(200);
368 }
369
emac_outblk_32bit(void __iomem * reg,void * data,int count)370 static void emac_outblk_32bit(void __iomem *reg, void *data, int count)
371 {
372 writesl(reg, data, round_up(count, 4) / 4);
373 }
374
emac_inblk_32bit(void __iomem * reg,void * data,int count)375 static void emac_inblk_32bit(void __iomem *reg, void *data, int count)
376 {
377 readsl(reg, data, round_up(count, 4) / 4);
378 }
379
380 /* ethtool ops */
emac_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)381 static void emac_get_drvinfo(struct net_device *dev,
382 struct ethtool_drvinfo *info)
383 {
384 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
385 strlcpy(info->bus_info, dev_name(&dev->dev), sizeof(info->bus_info));
386 }
387
emac_get_msglevel(struct net_device * dev)388 static u32 emac_get_msglevel(struct net_device *dev)
389 {
390 struct emac_board_info *db = netdev_priv(dev);
391
392 return db->msg_enable;
393 }
394
emac_set_msglevel(struct net_device * dev,u32 value)395 static void emac_set_msglevel(struct net_device *dev, u32 value)
396 {
397 struct emac_board_info *db = netdev_priv(dev);
398
399 db->msg_enable = value;
400 }
401
402 static const struct ethtool_ops emac_ethtool_ops = {
403 .get_drvinfo = emac_get_drvinfo,
404 .get_link = ethtool_op_get_link,
405 .get_link_ksettings = phy_ethtool_get_link_ksettings,
406 .set_link_ksettings = phy_ethtool_set_link_ksettings,
407 .get_msglevel = emac_get_msglevel,
408 .set_msglevel = emac_set_msglevel,
409 };
410
emac_setup(struct net_device * ndev)411 static unsigned int emac_setup(struct net_device *ndev)
412 {
413 struct emac_board_info *db = netdev_priv(ndev);
414 unsigned int reg_val;
415
416 /* set up TX */
417 reg_val = readl(db->membase + EMAC_TX_MODE_REG);
418
419 writel(reg_val | EMAC_TX_MODE_ABORTED_FRAME_EN,
420 db->membase + EMAC_TX_MODE_REG);
421
422 /* set MAC */
423 /* set MAC CTL0 */
424 reg_val = readl(db->membase + EMAC_MAC_CTL0_REG);
425 writel(reg_val | EMAC_MAC_CTL0_RX_FLOW_CTL_EN |
426 EMAC_MAC_CTL0_TX_FLOW_CTL_EN,
427 db->membase + EMAC_MAC_CTL0_REG);
428
429 /* set MAC CTL1 */
430 reg_val = readl(db->membase + EMAC_MAC_CTL1_REG);
431 reg_val |= EMAC_MAC_CTL1_LEN_CHECK_EN;
432 reg_val |= EMAC_MAC_CTL1_CRC_EN;
433 reg_val |= EMAC_MAC_CTL1_PAD_EN;
434 writel(reg_val, db->membase + EMAC_MAC_CTL1_REG);
435
436 /* set up IPGT */
437 writel(EMAC_MAC_IPGT_FULL_DUPLEX, db->membase + EMAC_MAC_IPGT_REG);
438
439 /* set up IPGR */
440 writel((EMAC_MAC_IPGR_IPG1 << 8) | EMAC_MAC_IPGR_IPG2,
441 db->membase + EMAC_MAC_IPGR_REG);
442
443 /* set up Collison window */
444 writel((EMAC_MAC_CLRT_COLLISION_WINDOW << 8) | EMAC_MAC_CLRT_RM,
445 db->membase + EMAC_MAC_CLRT_REG);
446
447 /* set up Max Frame Length */
448 writel(EMAC_MAX_FRAME_LEN,
449 db->membase + EMAC_MAC_MAXF_REG);
450
451 return 0;
452 }
453
emac_set_rx_mode(struct net_device * ndev)454 static void emac_set_rx_mode(struct net_device *ndev)
455 {
456 struct emac_board_info *db = netdev_priv(ndev);
457 unsigned int reg_val;
458
459 /* set up RX */
460 reg_val = readl(db->membase + EMAC_RX_CTL_REG);
461
462 if (ndev->flags & IFF_PROMISC)
463 reg_val |= EMAC_RX_CTL_PASS_ALL_EN;
464 else
465 reg_val &= ~EMAC_RX_CTL_PASS_ALL_EN;
466
467 writel(reg_val | EMAC_RX_CTL_PASS_LEN_OOR_EN |
468 EMAC_RX_CTL_ACCEPT_UNICAST_EN | EMAC_RX_CTL_DA_FILTER_EN |
469 EMAC_RX_CTL_ACCEPT_MULTICAST_EN |
470 EMAC_RX_CTL_ACCEPT_BROADCAST_EN,
471 db->membase + EMAC_RX_CTL_REG);
472 }
473
emac_powerup(struct net_device * ndev)474 static unsigned int emac_powerup(struct net_device *ndev)
475 {
476 struct emac_board_info *db = netdev_priv(ndev);
477 unsigned int reg_val;
478
479 /* initial EMAC */
480 /* flush RX FIFO */
481 reg_val = readl(db->membase + EMAC_RX_CTL_REG);
482 reg_val |= 0x8;
483 writel(reg_val, db->membase + EMAC_RX_CTL_REG);
484 udelay(1);
485
486 /* initial MAC */
487 /* soft reset MAC */
488 reg_val = readl(db->membase + EMAC_MAC_CTL0_REG);
489 reg_val &= ~EMAC_MAC_CTL0_SOFT_RESET;
490 writel(reg_val, db->membase + EMAC_MAC_CTL0_REG);
491
492 /* set MII clock */
493 reg_val = readl(db->membase + EMAC_MAC_MCFG_REG);
494 reg_val &= (~(0xf << 2));
495 reg_val |= (0xD << 2);
496 writel(reg_val, db->membase + EMAC_MAC_MCFG_REG);
497
498 /* clear RX counter */
499 writel(0x0, db->membase + EMAC_RX_FBC_REG);
500
501 /* disable all interrupt and clear interrupt status */
502 writel(0, db->membase + EMAC_INT_CTL_REG);
503 reg_val = readl(db->membase + EMAC_INT_STA_REG);
504 writel(reg_val, db->membase + EMAC_INT_STA_REG);
505
506 udelay(1);
507
508 /* set up EMAC */
509 emac_setup(ndev);
510
511 /* set mac_address to chip */
512 writel(ndev->dev_addr[0] << 16 | ndev->dev_addr[1] << 8 | ndev->
513 dev_addr[2], db->membase + EMAC_MAC_A1_REG);
514 writel(ndev->dev_addr[3] << 16 | ndev->dev_addr[4] << 8 | ndev->
515 dev_addr[5], db->membase + EMAC_MAC_A0_REG);
516
517 mdelay(1);
518
519 return 0;
520 }
521
emac_set_mac_address(struct net_device * dev,void * p)522 static int emac_set_mac_address(struct net_device *dev, void *p)
523 {
524 struct sockaddr *addr = p;
525 struct emac_board_info *db = netdev_priv(dev);
526
527 if (netif_running(dev))
528 return -EBUSY;
529
530 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
531
532 writel(dev->dev_addr[0] << 16 | dev->dev_addr[1] << 8 | dev->
533 dev_addr[2], db->membase + EMAC_MAC_A1_REG);
534 writel(dev->dev_addr[3] << 16 | dev->dev_addr[4] << 8 | dev->
535 dev_addr[5], db->membase + EMAC_MAC_A0_REG);
536
537 return 0;
538 }
539
540 /* Initialize emac board */
emac_init_device(struct net_device * dev)541 static void emac_init_device(struct net_device *dev)
542 {
543 struct emac_board_info *db = netdev_priv(dev);
544 unsigned long flags;
545 unsigned int reg_val;
546
547 raw_spin_lock_irqsave(&db->lock, flags);
548
549 emac_update_speed(dev);
550 emac_update_duplex(dev);
551
552 /* enable RX/TX */
553 reg_val = readl(db->membase + EMAC_CTL_REG);
554 writel(reg_val | EMAC_CTL_RESET | EMAC_CTL_TX_EN | EMAC_CTL_RX_EN,
555 db->membase + EMAC_CTL_REG);
556
557 /* enable RX/TX0/RX Hlevel interrup */
558 reg_val = readl(db->membase + EMAC_INT_CTL_REG);
559 reg_val |= (0xf << 0) | (0x01 << 8);
560 writel(reg_val, db->membase + EMAC_INT_CTL_REG);
561
562 raw_spin_unlock_irqrestore(&db->lock, flags);
563 }
564
565 /* Our watchdog timed out. Called by the networking layer */
emac_timeout(struct net_device * dev,unsigned int txqueue)566 static void emac_timeout(struct net_device *dev, unsigned int txqueue)
567 {
568 struct emac_board_info *db = netdev_priv(dev);
569 unsigned long flags;
570
571 if (netif_msg_timer(db))
572 dev_err(db->dev, "tx time out.\n");
573
574 /* Save previous register address */
575 raw_spin_lock_irqsave(&db->lock, flags);
576
577 netif_stop_queue(dev);
578 emac_reset(db);
579 emac_init_device(dev);
580 /* We can accept TX packets again */
581 netif_trans_update(dev);
582 netif_wake_queue(dev);
583
584 /* Restore previous register address */
585 raw_spin_unlock_irqrestore(&db->lock, flags);
586 }
587
588 /* Hardware start transmission.
589 * Send a packet to media from the upper layer.
590 */
emac_start_xmit(struct sk_buff * skb,struct net_device * dev)591 static netdev_tx_t emac_start_xmit(struct sk_buff *skb, struct net_device *dev)
592 {
593 struct emac_board_info *db = netdev_priv(dev);
594 unsigned long channel;
595 unsigned long flags;
596
597 channel = db->tx_fifo_stat & 3;
598 if (channel == 3)
599 return 1;
600
601 channel = (channel == 1 ? 1 : 0);
602
603 raw_spin_lock_irqsave(&db->lock, flags);
604
605 writel(channel, db->membase + EMAC_TX_INS_REG);
606
607 emac_outblk_32bit(db->membase + EMAC_TX_IO_DATA_REG,
608 skb->data, skb->len);
609 dev->stats.tx_bytes += skb->len;
610
611 db->tx_fifo_stat |= 1 << channel;
612 /* TX control: First packet immediately send, second packet queue */
613 if (channel == 0) {
614 /* set TX len */
615 writel(skb->len, db->membase + EMAC_TX_PL0_REG);
616 /* start translate from fifo to phy */
617 writel(readl(db->membase + EMAC_TX_CTL0_REG) | 1,
618 db->membase + EMAC_TX_CTL0_REG);
619
620 /* save the time stamp */
621 netif_trans_update(dev);
622 } else if (channel == 1) {
623 /* set TX len */
624 writel(skb->len, db->membase + EMAC_TX_PL1_REG);
625 /* start translate from fifo to phy */
626 writel(readl(db->membase + EMAC_TX_CTL1_REG) | 1,
627 db->membase + EMAC_TX_CTL1_REG);
628
629 /* save the time stamp */
630 netif_trans_update(dev);
631 }
632
633 if ((db->tx_fifo_stat & 3) == 3) {
634 /* Second packet */
635 netif_stop_queue(dev);
636 }
637
638 raw_spin_unlock_irqrestore(&db->lock, flags);
639
640 /* free this SKB */
641 dev_consume_skb_any(skb);
642
643 return NETDEV_TX_OK;
644 }
645
646 /* EMAC interrupt handler
647 * receive the packet to upper layer, free the transmitted packet
648 */
emac_tx_done(struct net_device * dev,struct emac_board_info * db,unsigned int tx_status)649 static void emac_tx_done(struct net_device *dev, struct emac_board_info *db,
650 unsigned int tx_status)
651 {
652 /* One packet sent complete */
653 db->tx_fifo_stat &= ~(tx_status & 3);
654 if (3 == (tx_status & 3))
655 dev->stats.tx_packets += 2;
656 else
657 dev->stats.tx_packets++;
658
659 if (netif_msg_tx_done(db))
660 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
661
662 netif_wake_queue(dev);
663 }
664
665 /* Received a packet and pass to upper layer
666 */
emac_rx(struct net_device * dev)667 static void emac_rx(struct net_device *dev)
668 {
669 struct emac_board_info *db = netdev_priv(dev);
670 struct sk_buff *skb;
671 u8 *rdptr;
672 bool good_packet;
673 static int rxlen_last;
674 unsigned int reg_val;
675 u32 rxhdr, rxstatus, rxcount, rxlen;
676
677 /* Check packet ready or not */
678 while (1) {
679 /* race warning: the first packet might arrive with
680 * the interrupts disabled, but the second will fix
681 * it
682 */
683 rxcount = readl(db->membase + EMAC_RX_FBC_REG);
684
685 if (netif_msg_rx_status(db))
686 dev_dbg(db->dev, "RXCount: %x\n", rxcount);
687
688 if ((db->skb_last != NULL) && (rxlen_last > 0)) {
689 dev->stats.rx_bytes += rxlen_last;
690
691 /* Pass to upper layer */
692 db->skb_last->protocol = eth_type_trans(db->skb_last,
693 dev);
694 netif_rx(db->skb_last);
695 dev->stats.rx_packets++;
696 db->skb_last = NULL;
697 rxlen_last = 0;
698
699 reg_val = readl(db->membase + EMAC_RX_CTL_REG);
700 reg_val &= ~EMAC_RX_CTL_DMA_EN;
701 writel(reg_val, db->membase + EMAC_RX_CTL_REG);
702 }
703
704 if (!rxcount) {
705 db->emacrx_completed_flag = 1;
706 reg_val = readl(db->membase + EMAC_INT_CTL_REG);
707 reg_val |= (0xf << 0) | (0x01 << 8);
708 writel(reg_val, db->membase + EMAC_INT_CTL_REG);
709
710 /* had one stuck? */
711 rxcount = readl(db->membase + EMAC_RX_FBC_REG);
712 if (!rxcount)
713 return;
714 }
715
716 reg_val = readl(db->membase + EMAC_RX_IO_DATA_REG);
717 if (netif_msg_rx_status(db))
718 dev_dbg(db->dev, "receive header: %x\n", reg_val);
719 if (reg_val != EMAC_UNDOCUMENTED_MAGIC) {
720 /* disable RX */
721 reg_val = readl(db->membase + EMAC_CTL_REG);
722 writel(reg_val & ~EMAC_CTL_RX_EN,
723 db->membase + EMAC_CTL_REG);
724
725 /* Flush RX FIFO */
726 reg_val = readl(db->membase + EMAC_RX_CTL_REG);
727 writel(reg_val | (1 << 3),
728 db->membase + EMAC_RX_CTL_REG);
729
730 do {
731 reg_val = readl(db->membase + EMAC_RX_CTL_REG);
732 } while (reg_val & (1 << 3));
733
734 /* enable RX */
735 reg_val = readl(db->membase + EMAC_CTL_REG);
736 writel(reg_val | EMAC_CTL_RX_EN,
737 db->membase + EMAC_CTL_REG);
738 reg_val = readl(db->membase + EMAC_INT_CTL_REG);
739 reg_val |= (0xf << 0) | (0x01 << 8);
740 writel(reg_val, db->membase + EMAC_INT_CTL_REG);
741
742 db->emacrx_completed_flag = 1;
743
744 return;
745 }
746
747 /* A packet ready now & Get status/length */
748 good_packet = true;
749
750 emac_inblk_32bit(db->membase + EMAC_RX_IO_DATA_REG,
751 &rxhdr, sizeof(rxhdr));
752
753 if (netif_msg_rx_status(db))
754 dev_dbg(db->dev, "rxhdr: %x\n", *((int *)(&rxhdr)));
755
756 rxlen = EMAC_RX_IO_DATA_LEN(rxhdr);
757 rxstatus = EMAC_RX_IO_DATA_STATUS(rxhdr);
758
759 if (netif_msg_rx_status(db))
760 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
761 rxstatus, rxlen);
762
763 /* Packet Status check */
764 if (rxlen < 0x40) {
765 good_packet = false;
766 if (netif_msg_rx_err(db))
767 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
768 }
769
770 if (unlikely(!(rxstatus & EMAC_RX_IO_DATA_STATUS_OK))) {
771 good_packet = false;
772
773 if (rxstatus & EMAC_RX_IO_DATA_STATUS_CRC_ERR) {
774 if (netif_msg_rx_err(db))
775 dev_dbg(db->dev, "crc error\n");
776 dev->stats.rx_crc_errors++;
777 }
778
779 if (rxstatus & EMAC_RX_IO_DATA_STATUS_LEN_ERR) {
780 if (netif_msg_rx_err(db))
781 dev_dbg(db->dev, "length error\n");
782 dev->stats.rx_length_errors++;
783 }
784 }
785
786 /* Move data from EMAC */
787 if (good_packet) {
788 skb = netdev_alloc_skb(dev, rxlen + 4);
789 if (!skb)
790 continue;
791 skb_reserve(skb, 2);
792 rdptr = (u8 *) skb_put(skb, rxlen - 4);
793
794 /* Read received packet from RX SRAM */
795 if (netif_msg_rx_status(db))
796 dev_dbg(db->dev, "RxLen %x\n", rxlen);
797
798 emac_inblk_32bit(db->membase + EMAC_RX_IO_DATA_REG,
799 rdptr, rxlen);
800 dev->stats.rx_bytes += rxlen;
801
802 /* Pass to upper layer */
803 skb->protocol = eth_type_trans(skb, dev);
804 netif_rx(skb);
805 dev->stats.rx_packets++;
806 }
807 }
808 }
809
emac_interrupt(int irq,void * dev_id)810 static irqreturn_t emac_interrupt(int irq, void *dev_id)
811 {
812 struct net_device *dev = dev_id;
813 struct emac_board_info *db = netdev_priv(dev);
814 int int_status;
815 unsigned long flags;
816 unsigned int reg_val;
817
818 /* A real interrupt coming */
819
820 /* holders of db->lock must always block IRQs */
821 raw_spin_lock_irqsave(&db->lock, flags);
822
823 /* Disable all interrupts */
824 writel(0, db->membase + EMAC_INT_CTL_REG);
825
826 /* Got EMAC interrupt status */
827 /* Got ISR */
828 int_status = readl(db->membase + EMAC_INT_STA_REG);
829 /* Clear ISR status */
830 writel(int_status, db->membase + EMAC_INT_STA_REG);
831
832 if (netif_msg_intr(db))
833 dev_dbg(db->dev, "emac interrupt %02x\n", int_status);
834
835 /* Received the coming packet */
836 if ((int_status & 0x100) && (db->emacrx_completed_flag == 1)) {
837 /* carrier lost */
838 db->emacrx_completed_flag = 0;
839 emac_rx(dev);
840 }
841
842 /* Transmit Interrupt check */
843 if (int_status & (0x01 | 0x02))
844 emac_tx_done(dev, db, int_status);
845
846 if (int_status & (0x04 | 0x08))
847 netdev_info(dev, " ab : %x\n", int_status);
848
849 /* Re-enable interrupt mask */
850 if (db->emacrx_completed_flag == 1) {
851 reg_val = readl(db->membase + EMAC_INT_CTL_REG);
852 reg_val |= (0xf << 0) | (0x01 << 8);
853 writel(reg_val, db->membase + EMAC_INT_CTL_REG);
854 }
855 raw_spin_unlock_irqrestore(&db->lock, flags);
856
857 return IRQ_HANDLED;
858 }
859
860 #ifdef CONFIG_NET_POLL_CONTROLLER
861 /*
862 * Used by netconsole
863 */
emac_poll_controller(struct net_device * dev)864 static void emac_poll_controller(struct net_device *dev)
865 {
866 disable_irq(dev->irq);
867 emac_interrupt(dev->irq, dev);
868 enable_irq(dev->irq);
869 }
870 #endif
871
872 /* Open the interface.
873 * The interface is opened whenever "ifconfig" actives it.
874 */
emac_open(struct net_device * dev)875 static int emac_open(struct net_device *dev)
876 {
877 struct emac_board_info *db = netdev_priv(dev);
878 int ret;
879
880 if (netif_msg_ifup(db))
881 dev_dbg(db->dev, "enabling %s\n", dev->name);
882
883 if (request_irq(dev->irq, &emac_interrupt, 0, dev->name, dev))
884 return -EAGAIN;
885
886 ret = emac_power_on(db);
887 if (ret)
888 goto out;
889
890 ret = clk_prepare_enable(db->clk);
891 if (ret) {
892 dev_err(db->dev, "Error couldn't enable clock (%d)\n", ret);
893 goto power_out;
894 }
895
896 /* Initialize EMAC board */
897 emac_reset(db);
898 emac_init_device(dev);
899
900 ret = emac_mdio_probe(dev);
901 if (ret < 0) {
902 free_irq(dev->irq, dev);
903 netdev_err(dev, "cannot probe MDIO bus\n");
904 goto clk_out;
905 }
906
907 phy_start(db->phy_dev);
908 netif_start_queue(dev);
909
910 return 0;
911
912 clk_out:
913 clk_disable_unprepare(db->clk);
914 power_out:
915 emac_power_off(db);
916 out:
917 free_irq(dev->irq, dev);
918
919 return ret;
920 }
921
emac_shutdown(struct net_device * dev)922 static void emac_shutdown(struct net_device *dev)
923 {
924 unsigned int reg_val;
925 struct emac_board_info *db = netdev_priv(dev);
926
927 /* Disable all interrupt */
928 writel(0, db->membase + EMAC_INT_CTL_REG);
929
930 /* clear interupt status */
931 reg_val = readl(db->membase + EMAC_INT_STA_REG);
932 writel(reg_val, db->membase + EMAC_INT_STA_REG);
933
934 /* Disable RX/TX */
935 reg_val = readl(db->membase + EMAC_CTL_REG);
936 reg_val &= ~(EMAC_CTL_TX_EN | EMAC_CTL_RX_EN | EMAC_CTL_RESET);
937 writel(reg_val, db->membase + EMAC_CTL_REG);
938 }
939
940 /* Stop the interface.
941 * The interface is stopped when it is brought.
942 */
emac_stop(struct net_device * ndev)943 static int emac_stop(struct net_device *ndev)
944 {
945 struct emac_board_info *db = netdev_priv(ndev);
946
947 if (netif_msg_ifdown(db))
948 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
949
950 netif_stop_queue(ndev);
951 netif_carrier_off(ndev);
952
953 phy_stop(db->phy_dev);
954
955 emac_mdio_remove(ndev);
956
957 emac_shutdown(ndev);
958
959 free_irq(ndev->irq, ndev);
960
961 clk_disable_unprepare(db->clk);
962
963 emac_power_off(db);
964
965 return 0;
966 }
967
968 static const struct net_device_ops emac_netdev_ops = {
969 .ndo_open = emac_open,
970 .ndo_stop = emac_stop,
971 .ndo_start_xmit = emac_start_xmit,
972 .ndo_tx_timeout = emac_timeout,
973 .ndo_set_rx_mode = emac_set_rx_mode,
974 .ndo_do_ioctl = phy_do_ioctl_running,
975 .ndo_validate_addr = eth_validate_addr,
976 .ndo_set_mac_address = emac_set_mac_address,
977 #ifdef CONFIG_NET_POLL_CONTROLLER
978 .ndo_poll_controller = emac_poll_controller,
979 #endif
980 };
981
982 /* Search EMAC board, allocate space and register it
983 */
emac_probe(struct platform_device * pdev)984 static int emac_probe(struct platform_device *pdev)
985 {
986 struct device_node *np = pdev->dev.of_node;
987 struct emac_board_info *db;
988 struct net_device *ndev;
989 int ret = 0;
990 const char *mac_addr;
991 enum of_gpio_flags flag;
992 char power_vol[EMAC_POWER_CHAR_LENGTH];
993 char power[EMAC_POWER_CHAR_LENGTH];
994 int val, i;
995 u32 value;
996
997 ndev = alloc_etherdev(sizeof(struct emac_board_info));
998 if (!ndev) {
999 dev_err(&pdev->dev, "could not allocate device.\n");
1000 return -ENOMEM;
1001 }
1002
1003 SET_NETDEV_DEV(ndev, &pdev->dev);
1004
1005 db = netdev_priv(ndev);
1006 memset(db, 0, sizeof(*db));
1007
1008 db->dev = &pdev->dev;
1009 db->ndev = ndev;
1010 db->pdev = pdev;
1011 db->msg_enable = netif_msg_init(debug, EMAC_DEFAULT_MSG_ENABLE);
1012
1013 raw_spin_lock_init(&db->lock);
1014
1015 db->reset = devm_reset_control_get(&pdev->dev, NULL);
1016 if (IS_ERR(db->reset)) {
1017 dev_err(&pdev->dev, "Error couldn't get reset\n");
1018 goto out_reset;
1019 }
1020
1021 ret = reset_control_reset(db->reset);
1022 if (ret) {
1023 dev_err(&pdev->dev, "Error reset control error\n");
1024 goto out_reset;
1025 }
1026
1027 db->membase = of_iomap(np, 0);
1028 if (!db->membase) {
1029 dev_err(&pdev->dev, "failed to remap registers\n");
1030 ret = -ENOMEM;
1031 goto out;
1032 }
1033
1034 /* fill in parameters for net-dev structure */
1035 ndev->base_addr = (unsigned long)db->membase;
1036 ndev->irq = irq_of_parse_and_map(np, 0);
1037 if (ndev->irq == -ENXIO) {
1038 netdev_err(ndev, "No irq resource\n");
1039 ret = ndev->irq;
1040 goto out;
1041 }
1042
1043 #ifdef SRAM_MAP_EMAC_LOCAL
1044 sram_map_emac_base = ioremap(EMAC_SRAM_MAP_BASE, EMAC_SRAM_MAP_LEN);
1045 if (!sram_map_emac_base) {
1046 dev_err(&pdev->dev, "Error couldn't map SRAM to device\n");
1047 goto out_iounmap;
1048 }
1049
1050 val = readl(sram_map_emac_base);
1051
1052 if (((val >> 4) & 0x1) != 0x1) {
1053 dev_info(&pdev->dev, "Set SRAM_A3_A4_MAP map to EMAC \n");
1054 writel(val | 0x1 << 4, sram_map_emac_base);
1055 }
1056
1057 dev_info(&pdev->dev, "the data of sram map to EMAC is 0x%x \n", readl(sram_map_emac_base));
1058 #else
1059 ret = sunxi_sram_claim(&pdev->dev);
1060 if (ret) {
1061 dev_err(&pdev->dev, "Error couldn't map SRAM to device\n");
1062 goto out_iounmap;
1063 }
1064 #endif
1065
1066 db->clk = devm_clk_get(&pdev->dev, NULL);
1067 if (IS_ERR(db->clk))
1068 goto out_release_sram;
1069
1070
1071 db->phy_node = of_parse_phandle(np, "phy-handle", 0);
1072 if (!db->phy_node) {
1073 dev_err(&pdev->dev, "no associated PHY\n");
1074 ret = -ENODEV;
1075 goto out_clkput;
1076 }
1077
1078 db->phyrst = of_get_named_gpio_flags(np, "phy-rst", 0, &flag);
1079 db->rst_active_low = (flag == OF_GPIO_ACTIVE_LOW) ? 1 : 0;
1080
1081 if (gpio_is_valid(db->phyrst)) {
1082 if (gpio_request(db->phyrst, "phy-rst") < 0) {
1083 pr_err("gmac gpio request fail!\n");
1084 ret = -EINVAL;
1085 goto out_clkput;
1086 }
1087 }
1088
1089 /* config power regulator */
1090 for (i = 0; i < EMAC_POWER_CHAN_NUM; i++) {
1091 snprintf(power, 15, "emac-power%d", i);
1092 /* get gmac_supplyX voltage */
1093 sprintf(power_vol, "emac-power%d-vol", i);
1094 if (!of_property_read_u32(np, power_vol, &value)) {
1095 db->emac_supply_vol[i] = value;
1096 netdev_dbg(ndev, "Info: Emac_power_vol[%d] = %d\n", i, value);
1097 } else {
1098 db->emac_supply_vol[i] = 0;
1099 }
1100 db->emac_supply[i] = regulator_get(&pdev->dev, power);
1101
1102 if (IS_ERR(db->emac_supply[i]))
1103 netdev_err(ndev, "Error: Emac-power%d get error\n", i);
1104 else
1105 netdev_dbg(ndev, "Emac-power%d get success\n", i);
1106 }
1107
1108 /* Read MAC-address from DT */
1109 mac_addr = of_get_mac_address(np);
1110 if (!IS_ERR(mac_addr))
1111 ether_addr_copy(ndev->dev_addr, mac_addr);
1112
1113 /* Check if the MAC address is valid, if not get a random one */
1114 if (!is_valid_ether_addr(ndev->dev_addr)) {
1115 eth_hw_addr_random(ndev);
1116 dev_warn(&pdev->dev, "using random MAC address %pM\n",
1117 ndev->dev_addr);
1118 }
1119
1120 db->emacrx_completed_flag = 1;
1121 emac_powerup(ndev);
1122 emac_reset(db);
1123
1124 ndev->netdev_ops = &emac_netdev_ops;
1125 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1126 ndev->ethtool_ops = &emac_ethtool_ops;
1127
1128 platform_set_drvdata(pdev, ndev);
1129
1130 /* Carrier starts down, phylib will bring it up */
1131 netif_carrier_off(ndev);
1132
1133 ret = register_netdev(ndev);
1134 if (ret) {
1135 dev_err(&pdev->dev, "Registering netdev failed!\n");
1136 ret = -ENODEV;
1137
1138 if (gpio_is_valid(db->phyrst))
1139 gpio_free(db->phyrst);
1140
1141 goto out_clkput;
1142 }
1143
1144 dev_info(&pdev->dev, "%s: at %p, IRQ %d MAC: %pM\n",
1145 ndev->name, db->membase, ndev->irq, ndev->dev_addr);
1146
1147 return 0;
1148
1149 out_clkput:
1150 clk_put(db->clk);
1151 out_release_sram:
1152 #ifdef SRAM_MAP_EMAC_LOCAL
1153 iounmap(sram_map_emac_base);
1154 #else
1155 sunxi_sram_release(&pdev->dev);
1156 #endif
1157 out_iounmap:
1158 iounmap(db->membase);
1159 out:
1160 dev_err(db->dev, "not found (%d).\n", ret);
1161 reset_control_assert(db->reset);
1162 out_reset:
1163 free_netdev(ndev);
1164
1165 return ret;
1166 }
1167
emac_remove(struct platform_device * pdev)1168 static int emac_remove(struct platform_device *pdev)
1169 {
1170 struct net_device *ndev = platform_get_drvdata(pdev);
1171 struct emac_board_info *db = netdev_priv(ndev);
1172
1173 unregister_netdev(ndev);
1174 #ifdef SRAM_MAP_EMAC_LOCAL
1175 iounmap(sram_map_emac_base);
1176 #else
1177 sunxi_sram_release(&pdev->dev);
1178 #endif
1179 iounmap(db->membase);
1180 if (gpio_is_valid(db->phyrst))
1181 gpio_free(db->phyrst);
1182 clk_put(db->clk);
1183 free_netdev(ndev);
1184
1185 dev_dbg(&pdev->dev, "released and freed device\n");
1186 return 0;
1187 }
1188
emac_suspend(struct platform_device * dev,pm_message_t state)1189 static int emac_suspend(struct platform_device *dev, pm_message_t state)
1190 {
1191 struct net_device *ndev = platform_get_drvdata(dev);
1192
1193 netif_carrier_off(ndev);
1194 netif_device_detach(ndev);
1195 emac_shutdown(ndev);
1196
1197 return 0;
1198 }
1199
emac_resume(struct platform_device * dev)1200 static int emac_resume(struct platform_device *dev)
1201 {
1202 struct net_device *ndev = platform_get_drvdata(dev);
1203 struct emac_board_info *db = netdev_priv(ndev);
1204
1205 emac_reset(db);
1206 emac_init_device(ndev);
1207 netif_device_attach(ndev);
1208
1209 return 0;
1210 }
1211
1212 static const struct of_device_id emac_of_match[] = {
1213 {.compatible = "allwinner,sunxi-emac",},
1214 {},
1215 };
1216
1217 MODULE_DEVICE_TABLE(of, emac_of_match);
1218
1219 static struct platform_driver emac_driver = {
1220 .driver = {
1221 .name = "sun4i-emac",
1222 .of_match_table = emac_of_match,
1223 },
1224 .probe = emac_probe,
1225 .remove = emac_remove,
1226 .suspend = emac_suspend,
1227 .resume = emac_resume,
1228 };
1229
1230 module_platform_driver(emac_driver);
1231
1232 MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
1233 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1234 MODULE_AUTHOR("xuminghui <xuminghui@allwinnertech.com");
1235 MODULE_DESCRIPTION("Allwinner emac network driver");
1236 MODULE_LICENSE("GPL");
1237