2 * drivers/net/ethernet/nxp/lpc_eth.c
4 * Author: Kevin Wells <kevin.wells@nxp.com>
6 * Copyright (C) 2010 NXP Semiconductors
7 * Copyright (C) 2012 Roland Stigge <stigge@antcom.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/crc32.h>
32 #include <linux/platform_device.h>
33 #include <linux/spinlock.h>
34 #include <linux/ethtool.h>
35 #include <linux/mii.h>
36 #include <linux/clk.h>
37 #include <linux/workqueue.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/skbuff.h>
41 #include <linux/phy.h>
42 #include <linux/dma-mapping.h>
44 #include <linux/of_net.h>
45 #include <linux/types.h>
48 #include <mach/board.h>
49 #include <mach/platform.h>
50 #include <mach/hardware.h>
52 #define MODNAME "lpc-eth"
53 #define DRV_VERSION "1.00"
55 #define ENET_MAXF_SIZE 1536
56 #define ENET_RX_DESC 48
57 #define ENET_TX_DESC 16
59 #define NAPI_WEIGHT 16
62 * Ethernet MAC controller Register offsets
64 #define LPC_ENET_MAC1(x) (x + 0x000)
65 #define LPC_ENET_MAC2(x) (x + 0x004)
66 #define LPC_ENET_IPGT(x) (x + 0x008)
67 #define LPC_ENET_IPGR(x) (x + 0x00C)
68 #define LPC_ENET_CLRT(x) (x + 0x010)
69 #define LPC_ENET_MAXF(x) (x + 0x014)
70 #define LPC_ENET_SUPP(x) (x + 0x018)
71 #define LPC_ENET_TEST(x) (x + 0x01C)
72 #define LPC_ENET_MCFG(x) (x + 0x020)
73 #define LPC_ENET_MCMD(x) (x + 0x024)
74 #define LPC_ENET_MADR(x) (x + 0x028)
75 #define LPC_ENET_MWTD(x) (x + 0x02C)
76 #define LPC_ENET_MRDD(x) (x + 0x030)
77 #define LPC_ENET_MIND(x) (x + 0x034)
78 #define LPC_ENET_SA0(x) (x + 0x040)
79 #define LPC_ENET_SA1(x) (x + 0x044)
80 #define LPC_ENET_SA2(x) (x + 0x048)
81 #define LPC_ENET_COMMAND(x) (x + 0x100)
82 #define LPC_ENET_STATUS(x) (x + 0x104)
83 #define LPC_ENET_RXDESCRIPTOR(x) (x + 0x108)
84 #define LPC_ENET_RXSTATUS(x) (x + 0x10C)
85 #define LPC_ENET_RXDESCRIPTORNUMBER(x) (x + 0x110)
86 #define LPC_ENET_RXPRODUCEINDEX(x) (x + 0x114)
87 #define LPC_ENET_RXCONSUMEINDEX(x) (x + 0x118)
88 #define LPC_ENET_TXDESCRIPTOR(x) (x + 0x11C)
89 #define LPC_ENET_TXSTATUS(x) (x + 0x120)
90 #define LPC_ENET_TXDESCRIPTORNUMBER(x) (x + 0x124)
91 #define LPC_ENET_TXPRODUCEINDEX(x) (x + 0x128)
92 #define LPC_ENET_TXCONSUMEINDEX(x) (x + 0x12C)
93 #define LPC_ENET_TSV0(x) (x + 0x158)
94 #define LPC_ENET_TSV1(x) (x + 0x15C)
95 #define LPC_ENET_RSV(x) (x + 0x160)
96 #define LPC_ENET_FLOWCONTROLCOUNTER(x) (x + 0x170)
97 #define LPC_ENET_FLOWCONTROLSTATUS(x) (x + 0x174)
98 #define LPC_ENET_RXFILTER_CTRL(x) (x + 0x200)
99 #define LPC_ENET_RXFILTERWOLSTATUS(x) (x + 0x204)
100 #define LPC_ENET_RXFILTERWOLCLEAR(x) (x + 0x208)
101 #define LPC_ENET_HASHFILTERL(x) (x + 0x210)
102 #define LPC_ENET_HASHFILTERH(x) (x + 0x214)
103 #define LPC_ENET_INTSTATUS(x) (x + 0xFE0)
104 #define LPC_ENET_INTENABLE(x) (x + 0xFE4)
105 #define LPC_ENET_INTCLEAR(x) (x + 0xFE8)
106 #define LPC_ENET_INTSET(x) (x + 0xFEC)
107 #define LPC_ENET_POWERDOWN(x) (x + 0xFF4)
110 * mac1 register definitions
112 #define LPC_MAC1_RECV_ENABLE (1 << 0)
113 #define LPC_MAC1_PASS_ALL_RX_FRAMES (1 << 1)
114 #define LPC_MAC1_RX_FLOW_CONTROL (1 << 2)
115 #define LPC_MAC1_TX_FLOW_CONTROL (1 << 3)
116 #define LPC_MAC1_LOOPBACK (1 << 4)
117 #define LPC_MAC1_RESET_TX (1 << 8)
118 #define LPC_MAC1_RESET_MCS_TX (1 << 9)
119 #define LPC_MAC1_RESET_RX (1 << 10)
120 #define LPC_MAC1_RESET_MCS_RX (1 << 11)
121 #define LPC_MAC1_SIMULATION_RESET (1 << 14)
122 #define LPC_MAC1_SOFT_RESET (1 << 15)
125 * mac2 register definitions
127 #define LPC_MAC2_FULL_DUPLEX (1 << 0)
128 #define LPC_MAC2_FRAME_LENGTH_CHECKING (1 << 1)
129 #define LPC_MAC2_HUGH_LENGTH_CHECKING (1 << 2)
130 #define LPC_MAC2_DELAYED_CRC (1 << 3)
131 #define LPC_MAC2_CRC_ENABLE (1 << 4)
132 #define LPC_MAC2_PAD_CRC_ENABLE (1 << 5)
133 #define LPC_MAC2_VLAN_PAD_ENABLE (1 << 6)
134 #define LPC_MAC2_AUTO_DETECT_PAD_ENABLE (1 << 7)
135 #define LPC_MAC2_PURE_PREAMBLE_ENFORCEMENT (1 << 8)
136 #define LPC_MAC2_LONG_PREAMBLE_ENFORCEMENT (1 << 9)
137 #define LPC_MAC2_NO_BACKOFF (1 << 12)
138 #define LPC_MAC2_BACK_PRESSURE (1 << 13)
139 #define LPC_MAC2_EXCESS_DEFER (1 << 14)
142 * ipgt register definitions
144 #define LPC_IPGT_LOAD(n) ((n) & 0x7F)
147 * ipgr register definitions
149 #define LPC_IPGR_LOAD_PART2(n) ((n) & 0x7F)
150 #define LPC_IPGR_LOAD_PART1(n) (((n) & 0x7F) << 8)
153 * clrt register definitions
155 #define LPC_CLRT_LOAD_RETRY_MAX(n) ((n) & 0xF)
156 #define LPC_CLRT_LOAD_COLLISION_WINDOW(n) (((n) & 0x3F) << 8)
159 * maxf register definitions
161 #define LPC_MAXF_LOAD_MAX_FRAME_LEN(n) ((n) & 0xFFFF)
164 * supp register definitions
166 #define LPC_SUPP_SPEED (1 << 8)
167 #define LPC_SUPP_RESET_RMII (1 << 11)
170 * test register definitions
172 #define LPC_TEST_SHORTCUT_PAUSE_QUANTA (1 << 0)
173 #define LPC_TEST_PAUSE (1 << 1)
174 #define LPC_TEST_BACKPRESSURE (1 << 2)
177 * mcfg register definitions
179 #define LPC_MCFG_SCAN_INCREMENT (1 << 0)
180 #define LPC_MCFG_SUPPRESS_PREAMBLE (1 << 1)
181 #define LPC_MCFG_CLOCK_SELECT(n) (((n) & 0x7) << 2)
182 #define LPC_MCFG_CLOCK_HOST_DIV_4 0
183 #define LPC_MCFG_CLOCK_HOST_DIV_6 2
184 #define LPC_MCFG_CLOCK_HOST_DIV_8 3
185 #define LPC_MCFG_CLOCK_HOST_DIV_10 4
186 #define LPC_MCFG_CLOCK_HOST_DIV_14 5
187 #define LPC_MCFG_CLOCK_HOST_DIV_20 6
188 #define LPC_MCFG_CLOCK_HOST_DIV_28 7
189 #define LPC_MCFG_RESET_MII_MGMT (1 << 15)
192 * mcmd register definitions
194 #define LPC_MCMD_READ (1 << 0)
195 #define LPC_MCMD_SCAN (1 << 1)
198 * madr register definitions
200 #define LPC_MADR_REGISTER_ADDRESS(n) ((n) & 0x1F)
201 #define LPC_MADR_PHY_0ADDRESS(n) (((n) & 0x1F) << 8)
204 * mwtd register definitions
206 #define LPC_MWDT_WRITE(n) ((n) & 0xFFFF)
209 * mrdd register definitions
211 #define LPC_MRDD_READ_MASK 0xFFFF
214 * mind register definitions
216 #define LPC_MIND_BUSY (1 << 0)
217 #define LPC_MIND_SCANNING (1 << 1)
218 #define LPC_MIND_NOT_VALID (1 << 2)
219 #define LPC_MIND_MII_LINK_FAIL (1 << 3)
222 * command register definitions
224 #define LPC_COMMAND_RXENABLE (1 << 0)
225 #define LPC_COMMAND_TXENABLE (1 << 1)
226 #define LPC_COMMAND_REG_RESET (1 << 3)
227 #define LPC_COMMAND_TXRESET (1 << 4)
228 #define LPC_COMMAND_RXRESET (1 << 5)
229 #define LPC_COMMAND_PASSRUNTFRAME (1 << 6)
230 #define LPC_COMMAND_PASSRXFILTER (1 << 7)
231 #define LPC_COMMAND_TXFLOWCONTROL (1 << 8)
232 #define LPC_COMMAND_RMII (1 << 9)
233 #define LPC_COMMAND_FULLDUPLEX (1 << 10)
236 * status register definitions
238 #define LPC_STATUS_RXACTIVE (1 << 0)
239 #define LPC_STATUS_TXACTIVE (1 << 1)
242 * tsv0 register definitions
244 #define LPC_TSV0_CRC_ERROR (1 << 0)
245 #define LPC_TSV0_LENGTH_CHECK_ERROR (1 << 1)
246 #define LPC_TSV0_LENGTH_OUT_OF_RANGE (1 << 2)
247 #define LPC_TSV0_DONE (1 << 3)
248 #define LPC_TSV0_MULTICAST (1 << 4)
249 #define LPC_TSV0_BROADCAST (1 << 5)
250 #define LPC_TSV0_PACKET_DEFER (1 << 6)
251 #define LPC_TSV0_ESCESSIVE_DEFER (1 << 7)
252 #define LPC_TSV0_ESCESSIVE_COLLISION (1 << 8)
253 #define LPC_TSV0_LATE_COLLISION (1 << 9)
254 #define LPC_TSV0_GIANT (1 << 10)
255 #define LPC_TSV0_UNDERRUN (1 << 11)
256 #define LPC_TSV0_TOTAL_BYTES(n) (((n) >> 12) & 0xFFFF)
257 #define LPC_TSV0_CONTROL_FRAME (1 << 28)
258 #define LPC_TSV0_PAUSE (1 << 29)
259 #define LPC_TSV0_BACKPRESSURE (1 << 30)
260 #define LPC_TSV0_VLAN (1 << 31)
263 * tsv1 register definitions
265 #define LPC_TSV1_TRANSMIT_BYTE_COUNT(n) ((n) & 0xFFFF)
266 #define LPC_TSV1_COLLISION_COUNT(n) (((n) >> 16) & 0xF)
269 * rsv register definitions
271 #define LPC_RSV_RECEIVED_BYTE_COUNT(n) ((n) & 0xFFFF)
272 #define LPC_RSV_RXDV_EVENT_IGNORED (1 << 16)
273 #define LPC_RSV_RXDV_EVENT_PREVIOUSLY_SEEN (1 << 17)
274 #define LPC_RSV_CARRIER_EVNT_PREVIOUS_SEEN (1 << 18)
275 #define LPC_RSV_RECEIVE_CODE_VIOLATION (1 << 19)
276 #define LPC_RSV_CRC_ERROR (1 << 20)
277 #define LPC_RSV_LENGTH_CHECK_ERROR (1 << 21)
278 #define LPC_RSV_LENGTH_OUT_OF_RANGE (1 << 22)
279 #define LPC_RSV_RECEIVE_OK (1 << 23)
280 #define LPC_RSV_MULTICAST (1 << 24)
281 #define LPC_RSV_BROADCAST (1 << 25)
282 #define LPC_RSV_DRIBBLE_NIBBLE (1 << 26)
283 #define LPC_RSV_CONTROL_FRAME (1 << 27)
284 #define LPC_RSV_PAUSE (1 << 28)
285 #define LPC_RSV_UNSUPPORTED_OPCODE (1 << 29)
286 #define LPC_RSV_VLAN (1 << 30)
289 * flowcontrolcounter register definitions
291 #define LPC_FCCR_MIRRORCOUNTER(n) ((n) & 0xFFFF)
292 #define LPC_FCCR_PAUSETIMER(n) (((n) >> 16) & 0xFFFF)
295 * flowcontrolstatus register definitions
297 #define LPC_FCCR_MIRRORCOUNTERCURRENT(n) ((n) & 0xFFFF)
300 * rxfliterctrl, rxfilterwolstatus, and rxfilterwolclear shared
301 * register definitions
303 #define LPC_RXFLTRW_ACCEPTUNICAST (1 << 0)
304 #define LPC_RXFLTRW_ACCEPTUBROADCAST (1 << 1)
305 #define LPC_RXFLTRW_ACCEPTUMULTICAST (1 << 2)
306 #define LPC_RXFLTRW_ACCEPTUNICASTHASH (1 << 3)
307 #define LPC_RXFLTRW_ACCEPTUMULTICASTHASH (1 << 4)
308 #define LPC_RXFLTRW_ACCEPTPERFECT (1 << 5)
311 * rxfliterctrl register definitions
313 #define LPC_RXFLTRWSTS_MAGICPACKETENWOL (1 << 12)
314 #define LPC_RXFLTRWSTS_RXFILTERENWOL (1 << 13)
317 * rxfilterwolstatus/rxfilterwolclear register definitions
319 #define LPC_RXFLTRWSTS_RXFILTERWOL (1 << 7)
320 #define LPC_RXFLTRWSTS_MAGICPACKETWOL (1 << 8)
323 * intstatus, intenable, intclear, and Intset shared register
326 #define LPC_MACINT_RXOVERRUNINTEN (1 << 0)
327 #define LPC_MACINT_RXERRORONINT (1 << 1)
328 #define LPC_MACINT_RXFINISHEDINTEN (1 << 2)
329 #define LPC_MACINT_RXDONEINTEN (1 << 3)
330 #define LPC_MACINT_TXUNDERRUNINTEN (1 << 4)
331 #define LPC_MACINT_TXERRORINTEN (1 << 5)
332 #define LPC_MACINT_TXFINISHEDINTEN (1 << 6)
333 #define LPC_MACINT_TXDONEINTEN (1 << 7)
334 #define LPC_MACINT_SOFTINTEN (1 << 12)
335 #define LPC_MACINT_WAKEUPINTEN (1 << 13)
338 * powerdown register definitions
340 #define LPC_POWERDOWN_MACAHB (1 << 31)
342 static phy_interface_t
lpc_phy_interface_mode(struct device
*dev
)
344 if (dev
&& dev
->of_node
) {
345 const char *mode
= of_get_property(dev
->of_node
,
347 if (mode
&& !strcmp(mode
, "mii"))
348 return PHY_INTERFACE_MODE_MII
;
350 return PHY_INTERFACE_MODE_RMII
;
353 static bool use_iram_for_net(struct device
*dev
)
355 if (dev
&& dev
->of_node
)
356 return of_property_read_bool(dev
->of_node
, "use-iram");
360 /* Receive Status information word */
361 #define RXSTATUS_SIZE 0x000007FF
362 #define RXSTATUS_CONTROL (1 << 18)
363 #define RXSTATUS_VLAN (1 << 19)
364 #define RXSTATUS_FILTER (1 << 20)
365 #define RXSTATUS_MULTICAST (1 << 21)
366 #define RXSTATUS_BROADCAST (1 << 22)
367 #define RXSTATUS_CRC (1 << 23)
368 #define RXSTATUS_SYMBOL (1 << 24)
369 #define RXSTATUS_LENGTH (1 << 25)
370 #define RXSTATUS_RANGE (1 << 26)
371 #define RXSTATUS_ALIGN (1 << 27)
372 #define RXSTATUS_OVERRUN (1 << 28)
373 #define RXSTATUS_NODESC (1 << 29)
374 #define RXSTATUS_LAST (1 << 30)
375 #define RXSTATUS_ERROR (1 << 31)
377 #define RXSTATUS_STATUS_ERROR \
378 (RXSTATUS_NODESC | RXSTATUS_OVERRUN | RXSTATUS_ALIGN | \
379 RXSTATUS_RANGE | RXSTATUS_LENGTH | RXSTATUS_SYMBOL | RXSTATUS_CRC)
381 /* Receive Descriptor control word */
382 #define RXDESC_CONTROL_SIZE 0x000007FF
383 #define RXDESC_CONTROL_INT (1 << 31)
385 /* Transmit Status information word */
386 #define TXSTATUS_COLLISIONS_GET(x) (((x) >> 21) & 0xF)
387 #define TXSTATUS_DEFER (1 << 25)
388 #define TXSTATUS_EXCESSDEFER (1 << 26)
389 #define TXSTATUS_EXCESSCOLL (1 << 27)
390 #define TXSTATUS_LATECOLL (1 << 28)
391 #define TXSTATUS_UNDERRUN (1 << 29)
392 #define TXSTATUS_NODESC (1 << 30)
393 #define TXSTATUS_ERROR (1 << 31)
395 /* Transmit Descriptor control word */
396 #define TXDESC_CONTROL_SIZE 0x000007FF
397 #define TXDESC_CONTROL_OVERRIDE (1 << 26)
398 #define TXDESC_CONTROL_HUGE (1 << 27)
399 #define TXDESC_CONTROL_PAD (1 << 28)
400 #define TXDESC_CONTROL_CRC (1 << 29)
401 #define TXDESC_CONTROL_LAST (1 << 30)
402 #define TXDESC_CONTROL_INT (1 << 31)
405 * Structure of a TX/RX descriptors and RX status
413 __le32 statushashcrc
;
417 * Device driver data structure
419 struct netdata_local
{
420 struct platform_device
*pdev
;
421 struct net_device
*ndev
;
423 void __iomem
*net_base
;
425 unsigned int skblen
[ENET_TX_DESC
];
426 unsigned int last_tx_idx
;
427 unsigned int num_used_tx_buffs
;
428 struct mii_bus
*mii_bus
;
429 struct phy_device
*phy_dev
;
431 dma_addr_t dma_buff_base_p
;
432 void *dma_buff_base_v
;
433 size_t dma_buff_size
;
434 struct txrx_desc_t
*tx_desc_v
;
437 struct txrx_desc_t
*rx_desc_v
;
438 struct rx_status_t
*rx_stat_v
;
443 struct napi_struct napi
;
447 * MAC support functions
449 static void __lpc_set_mac(struct netdata_local
*pldat
, u8
*mac
)
453 /* Set station address */
454 tmp
= mac
[0] | ((u32
)mac
[1] << 8);
455 writel(tmp
, LPC_ENET_SA2(pldat
->net_base
));
456 tmp
= mac
[2] | ((u32
)mac
[3] << 8);
457 writel(tmp
, LPC_ENET_SA1(pldat
->net_base
));
458 tmp
= mac
[4] | ((u32
)mac
[5] << 8);
459 writel(tmp
, LPC_ENET_SA0(pldat
->net_base
));
461 netdev_dbg(pldat
->ndev
, "Ethernet MAC address %pM\n", mac
);
464 static void __lpc_get_mac(struct netdata_local
*pldat
, u8
*mac
)
468 /* Get station address */
469 tmp
= readl(LPC_ENET_SA2(pldat
->net_base
));
472 tmp
= readl(LPC_ENET_SA1(pldat
->net_base
));
475 tmp
= readl(LPC_ENET_SA0(pldat
->net_base
));
480 static void __lpc_eth_clock_enable(struct netdata_local
*pldat
,
484 clk_enable(pldat
->clk
);
486 clk_disable(pldat
->clk
);
489 static void __lpc_params_setup(struct netdata_local
*pldat
)
493 if (pldat
->duplex
== DUPLEX_FULL
) {
494 tmp
= readl(LPC_ENET_MAC2(pldat
->net_base
));
495 tmp
|= LPC_MAC2_FULL_DUPLEX
;
496 writel(tmp
, LPC_ENET_MAC2(pldat
->net_base
));
497 tmp
= readl(LPC_ENET_COMMAND(pldat
->net_base
));
498 tmp
|= LPC_COMMAND_FULLDUPLEX
;
499 writel(tmp
, LPC_ENET_COMMAND(pldat
->net_base
));
500 writel(LPC_IPGT_LOAD(0x15), LPC_ENET_IPGT(pldat
->net_base
));
502 tmp
= readl(LPC_ENET_MAC2(pldat
->net_base
));
503 tmp
&= ~LPC_MAC2_FULL_DUPLEX
;
504 writel(tmp
, LPC_ENET_MAC2(pldat
->net_base
));
505 tmp
= readl(LPC_ENET_COMMAND(pldat
->net_base
));
506 tmp
&= ~LPC_COMMAND_FULLDUPLEX
;
507 writel(tmp
, LPC_ENET_COMMAND(pldat
->net_base
));
508 writel(LPC_IPGT_LOAD(0x12), LPC_ENET_IPGT(pldat
->net_base
));
511 if (pldat
->speed
== SPEED_100
)
512 writel(LPC_SUPP_SPEED
, LPC_ENET_SUPP(pldat
->net_base
));
514 writel(0, LPC_ENET_SUPP(pldat
->net_base
));
517 static void __lpc_eth_reset(struct netdata_local
*pldat
)
519 /* Reset all MAC logic */
520 writel((LPC_MAC1_RESET_TX
| LPC_MAC1_RESET_MCS_TX
| LPC_MAC1_RESET_RX
|
521 LPC_MAC1_RESET_MCS_RX
| LPC_MAC1_SIMULATION_RESET
|
522 LPC_MAC1_SOFT_RESET
), LPC_ENET_MAC1(pldat
->net_base
));
523 writel((LPC_COMMAND_REG_RESET
| LPC_COMMAND_TXRESET
|
524 LPC_COMMAND_RXRESET
), LPC_ENET_COMMAND(pldat
->net_base
));
527 static int __lpc_mii_mngt_reset(struct netdata_local
*pldat
)
529 /* Reset MII management hardware */
530 writel(LPC_MCFG_RESET_MII_MGMT
, LPC_ENET_MCFG(pldat
->net_base
));
532 /* Setup MII clock to slowest rate with a /28 divider */
533 writel(LPC_MCFG_CLOCK_SELECT(LPC_MCFG_CLOCK_HOST_DIV_28
),
534 LPC_ENET_MCFG(pldat
->net_base
));
539 static inline phys_addr_t
__va_to_pa(void *addr
, struct netdata_local
*pldat
)
543 phaddr
= addr
- pldat
->dma_buff_base_v
;
544 phaddr
+= pldat
->dma_buff_base_p
;
549 static void lpc_eth_enable_int(void __iomem
*regbase
)
551 writel((LPC_MACINT_RXDONEINTEN
| LPC_MACINT_TXDONEINTEN
),
552 LPC_ENET_INTENABLE(regbase
));
555 static void lpc_eth_disable_int(void __iomem
*regbase
)
557 writel(0, LPC_ENET_INTENABLE(regbase
));
560 /* Setup TX/RX descriptors */
561 static void __lpc_txrx_desc_setup(struct netdata_local
*pldat
)
566 struct txrx_desc_t
*ptxrxdesc
;
567 struct rx_status_t
*prxstat
;
569 tbuff
= PTR_ALIGN(pldat
->dma_buff_base_v
, 16);
571 /* Setup TX descriptors, status, and buffers */
572 pldat
->tx_desc_v
= tbuff
;
573 tbuff
+= sizeof(struct txrx_desc_t
) * ENET_TX_DESC
;
575 pldat
->tx_stat_v
= tbuff
;
576 tbuff
+= sizeof(u32
) * ENET_TX_DESC
;
578 tbuff
= PTR_ALIGN(tbuff
, 16);
579 pldat
->tx_buff_v
= tbuff
;
580 tbuff
+= ENET_MAXF_SIZE
* ENET_TX_DESC
;
582 /* Setup RX descriptors, status, and buffers */
583 pldat
->rx_desc_v
= tbuff
;
584 tbuff
+= sizeof(struct txrx_desc_t
) * ENET_RX_DESC
;
586 tbuff
= PTR_ALIGN(tbuff
, 16);
587 pldat
->rx_stat_v
= tbuff
;
588 tbuff
+= sizeof(struct rx_status_t
) * ENET_RX_DESC
;
590 tbuff
= PTR_ALIGN(tbuff
, 16);
591 pldat
->rx_buff_v
= tbuff
;
592 tbuff
+= ENET_MAXF_SIZE
* ENET_RX_DESC
;
594 /* Map the TX descriptors to the TX buffers in hardware */
595 for (i
= 0; i
< ENET_TX_DESC
; i
++) {
596 ptxstat
= &pldat
->tx_stat_v
[i
];
597 ptxrxdesc
= &pldat
->tx_desc_v
[i
];
599 ptxrxdesc
->packet
= __va_to_pa(
600 pldat
->tx_buff_v
+ i
* ENET_MAXF_SIZE
, pldat
);
601 ptxrxdesc
->control
= 0;
605 /* Map the RX descriptors to the RX buffers in hardware */
606 for (i
= 0; i
< ENET_RX_DESC
; i
++) {
607 prxstat
= &pldat
->rx_stat_v
[i
];
608 ptxrxdesc
= &pldat
->rx_desc_v
[i
];
610 ptxrxdesc
->packet
= __va_to_pa(
611 pldat
->rx_buff_v
+ i
* ENET_MAXF_SIZE
, pldat
);
612 ptxrxdesc
->control
= RXDESC_CONTROL_INT
| (ENET_MAXF_SIZE
- 1);
613 prxstat
->statusinfo
= 0;
614 prxstat
->statushashcrc
= 0;
617 /* Setup base addresses in hardware to point to buffers and
620 writel((ENET_TX_DESC
- 1),
621 LPC_ENET_TXDESCRIPTORNUMBER(pldat
->net_base
));
622 writel(__va_to_pa(pldat
->tx_desc_v
, pldat
),
623 LPC_ENET_TXDESCRIPTOR(pldat
->net_base
));
624 writel(__va_to_pa(pldat
->tx_stat_v
, pldat
),
625 LPC_ENET_TXSTATUS(pldat
->net_base
));
626 writel((ENET_RX_DESC
- 1),
627 LPC_ENET_RXDESCRIPTORNUMBER(pldat
->net_base
));
628 writel(__va_to_pa(pldat
->rx_desc_v
, pldat
),
629 LPC_ENET_RXDESCRIPTOR(pldat
->net_base
));
630 writel(__va_to_pa(pldat
->rx_stat_v
, pldat
),
631 LPC_ENET_RXSTATUS(pldat
->net_base
));
634 static void __lpc_eth_init(struct netdata_local
*pldat
)
638 /* Disable controller and reset */
639 tmp
= readl(LPC_ENET_COMMAND(pldat
->net_base
));
640 tmp
&= ~LPC_COMMAND_RXENABLE
| LPC_COMMAND_TXENABLE
;
641 writel(tmp
, LPC_ENET_COMMAND(pldat
->net_base
));
642 tmp
= readl(LPC_ENET_MAC1(pldat
->net_base
));
643 tmp
&= ~LPC_MAC1_RECV_ENABLE
;
644 writel(tmp
, LPC_ENET_MAC1(pldat
->net_base
));
646 /* Initial MAC setup */
647 writel(LPC_MAC1_PASS_ALL_RX_FRAMES
, LPC_ENET_MAC1(pldat
->net_base
));
648 writel((LPC_MAC2_PAD_CRC_ENABLE
| LPC_MAC2_CRC_ENABLE
),
649 LPC_ENET_MAC2(pldat
->net_base
));
650 writel(ENET_MAXF_SIZE
, LPC_ENET_MAXF(pldat
->net_base
));
652 /* Collision window, gap */
653 writel((LPC_CLRT_LOAD_RETRY_MAX(0xF) |
654 LPC_CLRT_LOAD_COLLISION_WINDOW(0x37)),
655 LPC_ENET_CLRT(pldat
->net_base
));
656 writel(LPC_IPGR_LOAD_PART2(0x12), LPC_ENET_IPGR(pldat
->net_base
));
658 if (lpc_phy_interface_mode(&pldat
->pdev
->dev
) == PHY_INTERFACE_MODE_MII
)
659 writel(LPC_COMMAND_PASSRUNTFRAME
,
660 LPC_ENET_COMMAND(pldat
->net_base
));
662 writel((LPC_COMMAND_PASSRUNTFRAME
| LPC_COMMAND_RMII
),
663 LPC_ENET_COMMAND(pldat
->net_base
));
664 writel(LPC_SUPP_RESET_RMII
, LPC_ENET_SUPP(pldat
->net_base
));
667 __lpc_params_setup(pldat
);
669 /* Setup TX and RX descriptors */
670 __lpc_txrx_desc_setup(pldat
);
672 /* Setup packet filtering */
673 writel((LPC_RXFLTRW_ACCEPTUBROADCAST
| LPC_RXFLTRW_ACCEPTPERFECT
),
674 LPC_ENET_RXFILTER_CTRL(pldat
->net_base
));
676 /* Get the next TX buffer output index */
677 pldat
->num_used_tx_buffs
= 0;
679 readl(LPC_ENET_TXCONSUMEINDEX(pldat
->net_base
));
681 /* Clear and enable interrupts */
682 writel(0xFFFF, LPC_ENET_INTCLEAR(pldat
->net_base
));
684 lpc_eth_enable_int(pldat
->net_base
);
686 /* Enable controller */
687 tmp
= readl(LPC_ENET_COMMAND(pldat
->net_base
));
688 tmp
|= LPC_COMMAND_RXENABLE
| LPC_COMMAND_TXENABLE
;
689 writel(tmp
, LPC_ENET_COMMAND(pldat
->net_base
));
690 tmp
= readl(LPC_ENET_MAC1(pldat
->net_base
));
691 tmp
|= LPC_MAC1_RECV_ENABLE
;
692 writel(tmp
, LPC_ENET_MAC1(pldat
->net_base
));
695 static void __lpc_eth_shutdown(struct netdata_local
*pldat
)
697 /* Reset ethernet and power down PHY */
698 __lpc_eth_reset(pldat
);
699 writel(0, LPC_ENET_MAC1(pldat
->net_base
));
700 writel(0, LPC_ENET_MAC2(pldat
->net_base
));
704 * MAC<--->PHY support functions
706 static int lpc_mdio_read(struct mii_bus
*bus
, int phy_id
, int phyreg
)
708 struct netdata_local
*pldat
= bus
->priv
;
709 unsigned long timeout
= jiffies
+ msecs_to_jiffies(100);
712 writel(((phy_id
<< 8) | phyreg
), LPC_ENET_MADR(pldat
->net_base
));
713 writel(LPC_MCMD_READ
, LPC_ENET_MCMD(pldat
->net_base
));
715 /* Wait for unbusy status */
716 while (readl(LPC_ENET_MIND(pldat
->net_base
)) & LPC_MIND_BUSY
) {
717 if (time_after(jiffies
, timeout
))
722 lps
= readl(LPC_ENET_MRDD(pldat
->net_base
));
723 writel(0, LPC_ENET_MCMD(pldat
->net_base
));
728 static int lpc_mdio_write(struct mii_bus
*bus
, int phy_id
, int phyreg
,
731 struct netdata_local
*pldat
= bus
->priv
;
732 unsigned long timeout
= jiffies
+ msecs_to_jiffies(100);
734 writel(((phy_id
<< 8) | phyreg
), LPC_ENET_MADR(pldat
->net_base
));
735 writel(phydata
, LPC_ENET_MWTD(pldat
->net_base
));
737 /* Wait for completion */
738 while (readl(LPC_ENET_MIND(pldat
->net_base
)) & LPC_MIND_BUSY
) {
739 if (time_after(jiffies
, timeout
))
747 static int lpc_mdio_reset(struct mii_bus
*bus
)
749 return __lpc_mii_mngt_reset((struct netdata_local
*)bus
->priv
);
752 static void lpc_handle_link_change(struct net_device
*ndev
)
754 struct netdata_local
*pldat
= netdev_priv(ndev
);
755 struct phy_device
*phydev
= pldat
->phy_dev
;
758 bool status_change
= false;
760 spin_lock_irqsave(&pldat
->lock
, flags
);
763 if ((pldat
->speed
!= phydev
->speed
) ||
764 (pldat
->duplex
!= phydev
->duplex
)) {
765 pldat
->speed
= phydev
->speed
;
766 pldat
->duplex
= phydev
->duplex
;
767 status_change
= true;
771 if (phydev
->link
!= pldat
->link
) {
776 pldat
->link
= phydev
->link
;
778 status_change
= true;
781 spin_unlock_irqrestore(&pldat
->lock
, flags
);
784 __lpc_params_setup(pldat
);
787 static int lpc_mii_probe(struct net_device
*ndev
)
789 struct netdata_local
*pldat
= netdev_priv(ndev
);
790 struct phy_device
*phydev
= phy_find_first(pldat
->mii_bus
);
793 netdev_err(ndev
, "no PHY found\n");
797 /* Attach to the PHY */
798 if (lpc_phy_interface_mode(&pldat
->pdev
->dev
) == PHY_INTERFACE_MODE_MII
)
799 netdev_info(ndev
, "using MII interface\n");
801 netdev_info(ndev
, "using RMII interface\n");
802 phydev
= phy_connect(ndev
, dev_name(&phydev
->dev
),
803 &lpc_handle_link_change
, 0,
804 lpc_phy_interface_mode(&pldat
->pdev
->dev
));
806 if (IS_ERR(phydev
)) {
807 netdev_err(ndev
, "Could not attach to PHY\n");
808 return PTR_ERR(phydev
);
811 /* mask with MAC supported features */
812 phydev
->supported
&= PHY_BASIC_FEATURES
;
814 phydev
->advertising
= phydev
->supported
;
819 pldat
->phy_dev
= phydev
;
822 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
823 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
827 static int lpc_mii_init(struct netdata_local
*pldat
)
831 pldat
->mii_bus
= mdiobus_alloc();
832 if (!pldat
->mii_bus
) {
838 if (lpc_phy_interface_mode(&pldat
->pdev
->dev
) == PHY_INTERFACE_MODE_MII
)
839 writel(LPC_COMMAND_PASSRUNTFRAME
,
840 LPC_ENET_COMMAND(pldat
->net_base
));
842 writel((LPC_COMMAND_PASSRUNTFRAME
| LPC_COMMAND_RMII
),
843 LPC_ENET_COMMAND(pldat
->net_base
));
844 writel(LPC_SUPP_RESET_RMII
, LPC_ENET_SUPP(pldat
->net_base
));
847 pldat
->mii_bus
->name
= "lpc_mii_bus";
848 pldat
->mii_bus
->read
= &lpc_mdio_read
;
849 pldat
->mii_bus
->write
= &lpc_mdio_write
;
850 pldat
->mii_bus
->reset
= &lpc_mdio_reset
;
851 snprintf(pldat
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
852 pldat
->pdev
->name
, pldat
->pdev
->id
);
853 pldat
->mii_bus
->priv
= pldat
;
854 pldat
->mii_bus
->parent
= &pldat
->pdev
->dev
;
856 pldat
->mii_bus
->irq
= kmalloc(sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
857 if (!pldat
->mii_bus
->irq
) {
862 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
863 pldat
->mii_bus
->irq
[i
] = PHY_POLL
;
865 platform_set_drvdata(pldat
->pdev
, pldat
->mii_bus
);
867 if (mdiobus_register(pldat
->mii_bus
))
868 goto err_out_free_mdio_irq
;
870 if (lpc_mii_probe(pldat
->ndev
) != 0)
871 goto err_out_unregister_bus
;
875 err_out_unregister_bus
:
876 mdiobus_unregister(pldat
->mii_bus
);
877 err_out_free_mdio_irq
:
878 kfree(pldat
->mii_bus
->irq
);
880 mdiobus_free(pldat
->mii_bus
);
885 static void __lpc_handle_xmit(struct net_device
*ndev
)
887 struct netdata_local
*pldat
= netdev_priv(ndev
);
888 u32 txcidx
, *ptxstat
, txstat
;
890 txcidx
= readl(LPC_ENET_TXCONSUMEINDEX(pldat
->net_base
));
891 while (pldat
->last_tx_idx
!= txcidx
) {
892 unsigned int skblen
= pldat
->skblen
[pldat
->last_tx_idx
];
894 /* A buffer is available, get buffer status */
895 ptxstat
= &pldat
->tx_stat_v
[pldat
->last_tx_idx
];
898 /* Next buffer and decrement used buffer counter */
899 pldat
->num_used_tx_buffs
--;
900 pldat
->last_tx_idx
++;
901 if (pldat
->last_tx_idx
>= ENET_TX_DESC
)
902 pldat
->last_tx_idx
= 0;
904 /* Update collision counter */
905 ndev
->stats
.collisions
+= TXSTATUS_COLLISIONS_GET(txstat
);
907 /* Any errors occurred? */
908 if (txstat
& TXSTATUS_ERROR
) {
909 if (txstat
& TXSTATUS_UNDERRUN
) {
911 ndev
->stats
.tx_fifo_errors
++;
913 if (txstat
& TXSTATUS_LATECOLL
) {
915 ndev
->stats
.tx_aborted_errors
++;
917 if (txstat
& TXSTATUS_EXCESSCOLL
) {
918 /* Excessive collision */
919 ndev
->stats
.tx_aborted_errors
++;
921 if (txstat
& TXSTATUS_EXCESSDEFER
) {
923 ndev
->stats
.tx_aborted_errors
++;
925 ndev
->stats
.tx_errors
++;
928 ndev
->stats
.tx_packets
++;
929 ndev
->stats
.tx_bytes
+= skblen
;
932 txcidx
= readl(LPC_ENET_TXCONSUMEINDEX(pldat
->net_base
));
935 if (pldat
->num_used_tx_buffs
<= ENET_TX_DESC
/2) {
936 if (netif_queue_stopped(ndev
))
937 netif_wake_queue(ndev
);
941 static int __lpc_handle_recv(struct net_device
*ndev
, int budget
)
943 struct netdata_local
*pldat
= netdev_priv(ndev
);
945 u32 rxconsidx
, len
, ethst
;
946 struct rx_status_t
*prxstat
;
950 /* Get the current RX buffer indexes */
951 rxconsidx
= readl(LPC_ENET_RXCONSUMEINDEX(pldat
->net_base
));
952 while (rx_done
< budget
&& rxconsidx
!=
953 readl(LPC_ENET_RXPRODUCEINDEX(pldat
->net_base
))) {
954 /* Get pointer to receive status */
955 prxstat
= &pldat
->rx_stat_v
[rxconsidx
];
956 len
= (prxstat
->statusinfo
& RXSTATUS_SIZE
) + 1;
959 ethst
= prxstat
->statusinfo
;
960 if ((ethst
& (RXSTATUS_ERROR
| RXSTATUS_STATUS_ERROR
)) ==
961 (RXSTATUS_ERROR
| RXSTATUS_RANGE
))
962 ethst
&= ~RXSTATUS_ERROR
;
964 if (ethst
& RXSTATUS_ERROR
) {
965 int si
= prxstat
->statusinfo
;
967 if (si
& RXSTATUS_OVERRUN
) {
969 ndev
->stats
.rx_fifo_errors
++;
970 } else if (si
& RXSTATUS_CRC
) {
972 ndev
->stats
.rx_crc_errors
++;
973 } else if (si
& RXSTATUS_LENGTH
) {
975 ndev
->stats
.rx_length_errors
++;
976 } else if (si
& RXSTATUS_ERROR
) {
978 ndev
->stats
.rx_length_errors
++;
980 ndev
->stats
.rx_errors
++;
983 skb
= dev_alloc_skb(len
);
985 ndev
->stats
.rx_dropped
++;
987 prdbuf
= skb_put(skb
, len
);
989 /* Copy packet from buffer */
990 memcpy(prdbuf
, pldat
->rx_buff_v
+
991 rxconsidx
* ENET_MAXF_SIZE
, len
);
993 /* Pass to upper layer */
994 skb
->protocol
= eth_type_trans(skb
, ndev
);
995 netif_receive_skb(skb
);
996 ndev
->stats
.rx_packets
++;
997 ndev
->stats
.rx_bytes
+= len
;
1001 /* Increment consume index */
1002 rxconsidx
= rxconsidx
+ 1;
1003 if (rxconsidx
>= ENET_RX_DESC
)
1006 LPC_ENET_RXCONSUMEINDEX(pldat
->net_base
));
1013 static int lpc_eth_poll(struct napi_struct
*napi
, int budget
)
1015 struct netdata_local
*pldat
= container_of(napi
,
1016 struct netdata_local
, napi
);
1017 struct net_device
*ndev
= pldat
->ndev
;
1019 struct netdev_queue
*txq
= netdev_get_tx_queue(ndev
, 0);
1021 __netif_tx_lock(txq
, smp_processor_id());
1022 __lpc_handle_xmit(ndev
);
1023 __netif_tx_unlock(txq
);
1024 rx_done
= __lpc_handle_recv(ndev
, budget
);
1026 if (rx_done
< budget
) {
1027 napi_complete(napi
);
1028 lpc_eth_enable_int(pldat
->net_base
);
1034 static irqreturn_t
__lpc_eth_interrupt(int irq
, void *dev_id
)
1036 struct net_device
*ndev
= dev_id
;
1037 struct netdata_local
*pldat
= netdev_priv(ndev
);
1040 spin_lock(&pldat
->lock
);
1042 tmp
= readl(LPC_ENET_INTSTATUS(pldat
->net_base
));
1043 /* Clear interrupts */
1044 writel(tmp
, LPC_ENET_INTCLEAR(pldat
->net_base
));
1046 lpc_eth_disable_int(pldat
->net_base
);
1047 if (likely(napi_schedule_prep(&pldat
->napi
)))
1048 __napi_schedule(&pldat
->napi
);
1050 spin_unlock(&pldat
->lock
);
1055 static int lpc_eth_close(struct net_device
*ndev
)
1057 unsigned long flags
;
1058 struct netdata_local
*pldat
= netdev_priv(ndev
);
1060 if (netif_msg_ifdown(pldat
))
1061 dev_dbg(&pldat
->pdev
->dev
, "shutting down %s\n", ndev
->name
);
1063 napi_disable(&pldat
->napi
);
1064 netif_stop_queue(ndev
);
1067 phy_stop(pldat
->phy_dev
);
1069 spin_lock_irqsave(&pldat
->lock
, flags
);
1070 __lpc_eth_reset(pldat
);
1071 netif_carrier_off(ndev
);
1072 writel(0, LPC_ENET_MAC1(pldat
->net_base
));
1073 writel(0, LPC_ENET_MAC2(pldat
->net_base
));
1074 spin_unlock_irqrestore(&pldat
->lock
, flags
);
1076 __lpc_eth_clock_enable(pldat
, false);
1081 static int lpc_eth_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1083 struct netdata_local
*pldat
= netdev_priv(ndev
);
1086 struct txrx_desc_t
*ptxrxdesc
;
1090 spin_lock_irq(&pldat
->lock
);
1092 if (pldat
->num_used_tx_buffs
>= (ENET_TX_DESC
- 1)) {
1093 /* This function should never be called when there are no
1095 netif_stop_queue(ndev
);
1096 spin_unlock_irq(&pldat
->lock
);
1097 WARN(1, "BUG! TX request when no free TX buffers!\n");
1098 return NETDEV_TX_BUSY
;
1101 /* Get the next TX descriptor index */
1102 txidx
= readl(LPC_ENET_TXPRODUCEINDEX(pldat
->net_base
));
1104 /* Setup control for the transfer */
1105 ptxstat
= &pldat
->tx_stat_v
[txidx
];
1107 ptxrxdesc
= &pldat
->tx_desc_v
[txidx
];
1108 ptxrxdesc
->control
=
1109 (len
- 1) | TXDESC_CONTROL_LAST
| TXDESC_CONTROL_INT
;
1111 /* Copy data to the DMA buffer */
1112 memcpy(pldat
->tx_buff_v
+ txidx
* ENET_MAXF_SIZE
, skb
->data
, len
);
1114 /* Save the buffer and increment the buffer counter */
1115 pldat
->skblen
[txidx
] = len
;
1116 pldat
->num_used_tx_buffs
++;
1118 /* Start transmit */
1120 if (txidx
>= ENET_TX_DESC
)
1122 writel(txidx
, LPC_ENET_TXPRODUCEINDEX(pldat
->net_base
));
1124 /* Stop queue if no more TX buffers */
1125 if (pldat
->num_used_tx_buffs
>= (ENET_TX_DESC
- 1))
1126 netif_stop_queue(ndev
);
1128 spin_unlock_irq(&pldat
->lock
);
1131 return NETDEV_TX_OK
;
1134 static int lpc_set_mac_address(struct net_device
*ndev
, void *p
)
1136 struct sockaddr
*addr
= p
;
1137 struct netdata_local
*pldat
= netdev_priv(ndev
);
1138 unsigned long flags
;
1140 if (!is_valid_ether_addr(addr
->sa_data
))
1141 return -EADDRNOTAVAIL
;
1142 memcpy(ndev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
1144 spin_lock_irqsave(&pldat
->lock
, flags
);
1146 /* Set station address */
1147 __lpc_set_mac(pldat
, ndev
->dev_addr
);
1149 spin_unlock_irqrestore(&pldat
->lock
, flags
);
1154 static void lpc_eth_set_multicast_list(struct net_device
*ndev
)
1156 struct netdata_local
*pldat
= netdev_priv(ndev
);
1157 struct netdev_hw_addr_list
*mcptr
= &ndev
->mc
;
1158 struct netdev_hw_addr
*ha
;
1159 u32 tmp32
, hash_val
, hashlo
, hashhi
;
1160 unsigned long flags
;
1162 spin_lock_irqsave(&pldat
->lock
, flags
);
1164 /* Set station address */
1165 __lpc_set_mac(pldat
, ndev
->dev_addr
);
1167 tmp32
= LPC_RXFLTRW_ACCEPTUBROADCAST
| LPC_RXFLTRW_ACCEPTPERFECT
;
1169 if (ndev
->flags
& IFF_PROMISC
)
1170 tmp32
|= LPC_RXFLTRW_ACCEPTUNICAST
|
1171 LPC_RXFLTRW_ACCEPTUMULTICAST
;
1172 if (ndev
->flags
& IFF_ALLMULTI
)
1173 tmp32
|= LPC_RXFLTRW_ACCEPTUMULTICAST
;
1175 if (netdev_hw_addr_list_count(mcptr
))
1176 tmp32
|= LPC_RXFLTRW_ACCEPTUMULTICASTHASH
;
1178 writel(tmp32
, LPC_ENET_RXFILTER_CTRL(pldat
->net_base
));
1181 /* Set initial hash table */
1185 /* 64 bits : multicast address in hash table */
1186 netdev_hw_addr_list_for_each(ha
, mcptr
) {
1187 hash_val
= (ether_crc(6, ha
->addr
) >> 23) & 0x3F;
1190 hashhi
|= 1 << (hash_val
- 32);
1192 hashlo
|= 1 << hash_val
;
1195 writel(hashlo
, LPC_ENET_HASHFILTERL(pldat
->net_base
));
1196 writel(hashhi
, LPC_ENET_HASHFILTERH(pldat
->net_base
));
1198 spin_unlock_irqrestore(&pldat
->lock
, flags
);
1201 static int lpc_eth_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1203 struct netdata_local
*pldat
= netdev_priv(ndev
);
1204 struct phy_device
*phydev
= pldat
->phy_dev
;
1206 if (!netif_running(ndev
))
1212 return phy_mii_ioctl(phydev
, req
, cmd
);
1215 static int lpc_eth_open(struct net_device
*ndev
)
1217 struct netdata_local
*pldat
= netdev_priv(ndev
);
1219 if (netif_msg_ifup(pldat
))
1220 dev_dbg(&pldat
->pdev
->dev
, "enabling %s\n", ndev
->name
);
1222 if (!is_valid_ether_addr(ndev
->dev_addr
))
1223 return -EADDRNOTAVAIL
;
1225 __lpc_eth_clock_enable(pldat
, true);
1227 /* Reset and initialize */
1228 __lpc_eth_reset(pldat
);
1229 __lpc_eth_init(pldat
);
1231 /* schedule a link state check */
1232 phy_start(pldat
->phy_dev
);
1233 netif_start_queue(ndev
);
1234 napi_enable(&pldat
->napi
);
1242 static void lpc_eth_ethtool_getdrvinfo(struct net_device
*ndev
,
1243 struct ethtool_drvinfo
*info
)
1245 strcpy(info
->driver
, MODNAME
);
1246 strcpy(info
->version
, DRV_VERSION
);
1247 strcpy(info
->bus_info
, dev_name(ndev
->dev
.parent
));
1250 static u32
lpc_eth_ethtool_getmsglevel(struct net_device
*ndev
)
1252 struct netdata_local
*pldat
= netdev_priv(ndev
);
1254 return pldat
->msg_enable
;
1257 static void lpc_eth_ethtool_setmsglevel(struct net_device
*ndev
, u32 level
)
1259 struct netdata_local
*pldat
= netdev_priv(ndev
);
1261 pldat
->msg_enable
= level
;
1264 static int lpc_eth_ethtool_getsettings(struct net_device
*ndev
,
1265 struct ethtool_cmd
*cmd
)
1267 struct netdata_local
*pldat
= netdev_priv(ndev
);
1268 struct phy_device
*phydev
= pldat
->phy_dev
;
1273 return phy_ethtool_gset(phydev
, cmd
);
1276 static int lpc_eth_ethtool_setsettings(struct net_device
*ndev
,
1277 struct ethtool_cmd
*cmd
)
1279 struct netdata_local
*pldat
= netdev_priv(ndev
);
1280 struct phy_device
*phydev
= pldat
->phy_dev
;
1285 return phy_ethtool_sset(phydev
, cmd
);
1288 static const struct ethtool_ops lpc_eth_ethtool_ops
= {
1289 .get_drvinfo
= lpc_eth_ethtool_getdrvinfo
,
1290 .get_settings
= lpc_eth_ethtool_getsettings
,
1291 .set_settings
= lpc_eth_ethtool_setsettings
,
1292 .get_msglevel
= lpc_eth_ethtool_getmsglevel
,
1293 .set_msglevel
= lpc_eth_ethtool_setmsglevel
,
1294 .get_link
= ethtool_op_get_link
,
1297 static const struct net_device_ops lpc_netdev_ops
= {
1298 .ndo_open
= lpc_eth_open
,
1299 .ndo_stop
= lpc_eth_close
,
1300 .ndo_start_xmit
= lpc_eth_hard_start_xmit
,
1301 .ndo_set_rx_mode
= lpc_eth_set_multicast_list
,
1302 .ndo_do_ioctl
= lpc_eth_ioctl
,
1303 .ndo_set_mac_address
= lpc_set_mac_address
,
1304 .ndo_change_mtu
= eth_change_mtu
,
1307 static int lpc_eth_drv_probe(struct platform_device
*pdev
)
1309 struct resource
*res
;
1310 struct net_device
*ndev
;
1311 struct netdata_local
*pldat
;
1312 struct phy_device
*phydev
;
1313 dma_addr_t dma_handle
;
1317 /* Setup network interface for RMII or MII mode */
1318 tmp
= __raw_readl(LPC32XX_CLKPWR_MACCLK_CTRL
);
1319 tmp
&= ~LPC32XX_CLKPWR_MACCTRL_PINS_MSK
;
1320 if (lpc_phy_interface_mode(&pdev
->dev
) == PHY_INTERFACE_MODE_MII
)
1321 tmp
|= LPC32XX_CLKPWR_MACCTRL_USE_MII_PINS
;
1323 tmp
|= LPC32XX_CLKPWR_MACCTRL_USE_RMII_PINS
;
1324 __raw_writel(tmp
, LPC32XX_CLKPWR_MACCLK_CTRL
);
1326 /* Get platform resources */
1327 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1328 irq
= platform_get_irq(pdev
, 0);
1329 if ((!res
) || (irq
< 0) || (irq
>= NR_IRQS
)) {
1330 dev_err(&pdev
->dev
, "error getting resources.\n");
1335 /* Allocate net driver data structure */
1336 ndev
= alloc_etherdev(sizeof(struct netdata_local
));
1338 dev_err(&pdev
->dev
, "could not allocate device.\n");
1343 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1345 pldat
= netdev_priv(ndev
);
1349 spin_lock_init(&pldat
->lock
);
1351 /* Save resources */
1354 /* Get clock for the device */
1355 pldat
->clk
= clk_get(&pdev
->dev
, NULL
);
1356 if (IS_ERR(pldat
->clk
)) {
1357 dev_err(&pdev
->dev
, "error getting clock.\n");
1358 ret
= PTR_ERR(pldat
->clk
);
1359 goto err_out_free_dev
;
1362 /* Enable network clock */
1363 __lpc_eth_clock_enable(pldat
, true);
1366 pldat
->net_base
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
1367 if (!pldat
->net_base
) {
1368 dev_err(&pdev
->dev
, "failed to map registers\n");
1370 goto err_out_disable_clocks
;
1372 ret
= request_irq(ndev
->irq
, __lpc_eth_interrupt
, 0,
1375 dev_err(&pdev
->dev
, "error requesting interrupt.\n");
1376 goto err_out_iounmap
;
1379 /* Fill in the fields of the device structure with ethernet values. */
1382 /* Setup driver functions */
1383 ndev
->netdev_ops
= &lpc_netdev_ops
;
1384 ndev
->ethtool_ops
= &lpc_eth_ethtool_ops
;
1385 ndev
->watchdog_timeo
= msecs_to_jiffies(2500);
1387 /* Get size of DMA buffers/descriptors region */
1388 pldat
->dma_buff_size
= (ENET_TX_DESC
+ ENET_RX_DESC
) * (ENET_MAXF_SIZE
+
1389 sizeof(struct txrx_desc_t
) + sizeof(struct rx_status_t
));
1390 pldat
->dma_buff_base_v
= 0;
1392 if (use_iram_for_net(&pldat
->pdev
->dev
)) {
1393 dma_handle
= LPC32XX_IRAM_BASE
;
1394 if (pldat
->dma_buff_size
<= lpc32xx_return_iram_size())
1395 pldat
->dma_buff_base_v
=
1396 io_p2v(LPC32XX_IRAM_BASE
);
1399 "IRAM not big enough for net buffers, using SDRAM instead.\n");
1402 if (pldat
->dma_buff_base_v
== 0) {
1403 pldat
->pdev
->dev
.coherent_dma_mask
= 0xFFFFFFFF;
1404 pldat
->pdev
->dev
.dma_mask
= &pldat
->pdev
->dev
.coherent_dma_mask
;
1405 pldat
->dma_buff_size
= PAGE_ALIGN(pldat
->dma_buff_size
);
1407 /* Allocate a chunk of memory for the DMA ethernet buffers
1409 pldat
->dma_buff_base_v
=
1410 dma_alloc_coherent(&pldat
->pdev
->dev
,
1411 pldat
->dma_buff_size
, &dma_handle
,
1414 if (pldat
->dma_buff_base_v
== NULL
) {
1415 dev_err(&pdev
->dev
, "error getting DMA region.\n");
1417 goto err_out_free_irq
;
1420 pldat
->dma_buff_base_p
= dma_handle
;
1422 netdev_dbg(ndev
, "IO address start :0x%08x\n",
1424 netdev_dbg(ndev
, "IO address size :%d\n",
1425 res
->end
- res
->start
+ 1);
1426 netdev_dbg(ndev
, "IO address (mapped) :0x%p\n",
1428 netdev_dbg(ndev
, "IRQ number :%d\n", ndev
->irq
);
1429 netdev_dbg(ndev
, "DMA buffer size :%d\n", pldat
->dma_buff_size
);
1430 netdev_dbg(ndev
, "DMA buffer P address :0x%08x\n",
1431 pldat
->dma_buff_base_p
);
1432 netdev_dbg(ndev
, "DMA buffer V address :0x%p\n",
1433 pldat
->dma_buff_base_v
);
1435 /* Get MAC address from current HW setting (POR state is all zeros) */
1436 __lpc_get_mac(pldat
, ndev
->dev_addr
);
1438 #ifdef CONFIG_OF_NET
1439 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
1440 const char *macaddr
= of_get_mac_address(pdev
->dev
.of_node
);
1442 memcpy(ndev
->dev_addr
, macaddr
, ETH_ALEN
);
1445 if (!is_valid_ether_addr(ndev
->dev_addr
))
1446 eth_hw_addr_random(ndev
);
1448 /* Reset the ethernet controller */
1449 __lpc_eth_reset(pldat
);
1451 /* then shut everything down to save power */
1452 __lpc_eth_shutdown(pldat
);
1454 /* Set default parameters */
1455 pldat
->msg_enable
= NETIF_MSG_LINK
;
1457 /* Force an MII interface reset and clock setup */
1458 __lpc_mii_mngt_reset(pldat
);
1460 /* Force default PHY interface setup in chip, this will probably be
1461 changed by the PHY driver */
1464 pldat
->duplex
= DUPLEX_FULL
;
1465 __lpc_params_setup(pldat
);
1467 netif_napi_add(ndev
, &pldat
->napi
, lpc_eth_poll
, NAPI_WEIGHT
);
1469 ret
= register_netdev(ndev
);
1471 dev_err(&pdev
->dev
, "Cannot register net device, aborting.\n");
1472 goto err_out_dma_unmap
;
1474 platform_set_drvdata(pdev
, ndev
);
1476 if (lpc_mii_init(pldat
) != 0)
1477 goto err_out_unregister_netdev
;
1479 netdev_info(ndev
, "LPC mac at 0x%08x irq %d\n",
1480 res
->start
, ndev
->irq
);
1482 phydev
= pldat
->phy_dev
;
1484 device_init_wakeup(&pdev
->dev
, 1);
1485 device_set_wakeup_enable(&pdev
->dev
, 0);
1489 err_out_unregister_netdev
:
1490 platform_set_drvdata(pdev
, NULL
);
1491 unregister_netdev(ndev
);
1493 if (!use_iram_for_net(&pldat
->pdev
->dev
) ||
1494 pldat
->dma_buff_size
> lpc32xx_return_iram_size())
1495 dma_free_coherent(&pldat
->pdev
->dev
, pldat
->dma_buff_size
,
1496 pldat
->dma_buff_base_v
,
1497 pldat
->dma_buff_base_p
);
1499 free_irq(ndev
->irq
, ndev
);
1501 iounmap(pldat
->net_base
);
1502 err_out_disable_clocks
:
1503 clk_disable(pldat
->clk
);
1504 clk_put(pldat
->clk
);
1508 pr_err("%s: not found (%d).\n", MODNAME
, ret
);
1512 static int lpc_eth_drv_remove(struct platform_device
*pdev
)
1514 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1515 struct netdata_local
*pldat
= netdev_priv(ndev
);
1517 unregister_netdev(ndev
);
1518 platform_set_drvdata(pdev
, NULL
);
1520 if (!use_iram_for_net(&pldat
->pdev
->dev
) ||
1521 pldat
->dma_buff_size
> lpc32xx_return_iram_size())
1522 dma_free_coherent(&pldat
->pdev
->dev
, pldat
->dma_buff_size
,
1523 pldat
->dma_buff_base_v
,
1524 pldat
->dma_buff_base_p
);
1525 free_irq(ndev
->irq
, ndev
);
1526 iounmap(pldat
->net_base
);
1527 mdiobus_unregister(pldat
->mii_bus
);
1528 mdiobus_free(pldat
->mii_bus
);
1529 clk_disable(pldat
->clk
);
1530 clk_put(pldat
->clk
);
1537 static int lpc_eth_drv_suspend(struct platform_device
*pdev
,
1540 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1541 struct netdata_local
*pldat
= netdev_priv(ndev
);
1543 if (device_may_wakeup(&pdev
->dev
))
1544 enable_irq_wake(ndev
->irq
);
1547 if (netif_running(ndev
)) {
1548 netif_device_detach(ndev
);
1549 __lpc_eth_shutdown(pldat
);
1550 clk_disable(pldat
->clk
);
1553 * Reset again now clock is disable to be sure
1556 __lpc_eth_reset(pldat
);
1563 static int lpc_eth_drv_resume(struct platform_device
*pdev
)
1565 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1566 struct netdata_local
*pldat
;
1568 if (device_may_wakeup(&pdev
->dev
))
1569 disable_irq_wake(ndev
->irq
);
1572 if (netif_running(ndev
)) {
1573 pldat
= netdev_priv(ndev
);
1575 /* Enable interface clock */
1576 clk_enable(pldat
->clk
);
1578 /* Reset and initialize */
1579 __lpc_eth_reset(pldat
);
1580 __lpc_eth_init(pldat
);
1582 netif_device_attach(ndev
);
1591 static const struct of_device_id lpc_eth_match
[] = {
1592 { .compatible
= "nxp,lpc-eth" },
1595 MODULE_DEVICE_TABLE(of
, lpc_eth_match
);
1598 static struct platform_driver lpc_eth_driver
= {
1599 .probe
= lpc_eth_drv_probe
,
1600 .remove
= __devexit_p(lpc_eth_drv_remove
),
1602 .suspend
= lpc_eth_drv_suspend
,
1603 .resume
= lpc_eth_drv_resume
,
1607 .of_match_table
= of_match_ptr(lpc_eth_match
),
1611 module_platform_driver(lpc_eth_driver
);
1613 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
1614 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
1615 MODULE_DESCRIPTION("LPC Ethernet Driver");
1616 MODULE_LICENSE("GPL");