blob: e7d2496a4738b025f0d5c1cd63bba57a71b4ebd0 [file] [log] [blame]
stigge@antcom.deb7370112012-03-08 11:49:17 +00001/*
2 * drivers/net/ethernet/nxp/lpc_eth.c
3 *
4 * Author: Kevin Wells <kevin.wells@nxp.com>
5 *
6 * Copyright (C) 2010 NXP Semiconductors
7 * Copyright (C) 2012 Roland Stigge <stigge@antcom.de>
8 *
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.
13 *
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.
18 */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
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>
Roland Stigge4de02e42012-04-22 12:01:19 +020043#include <linux/of.h>
stigge@antcom.deb7370112012-03-08 11:49:17 +000044#include <linux/of_net.h>
45#include <linux/types.h>
46
47#include <linux/delay.h>
48#include <linux/io.h>
49#include <mach/board.h>
50#include <mach/platform.h>
51#include <mach/hardware.h>
52
53#define MODNAME "lpc-eth"
54#define DRV_VERSION "1.00"
stigge@antcom.deb7370112012-03-08 11:49:17 +000055
56#define ENET_MAXF_SIZE 1536
57#define ENET_RX_DESC 48
58#define ENET_TX_DESC 16
59
60#define NAPI_WEIGHT 16
61
62/*
63 * Ethernet MAC controller Register offsets
64 */
65#define LPC_ENET_MAC1(x) (x + 0x000)
66#define LPC_ENET_MAC2(x) (x + 0x004)
67#define LPC_ENET_IPGT(x) (x + 0x008)
68#define LPC_ENET_IPGR(x) (x + 0x00C)
69#define LPC_ENET_CLRT(x) (x + 0x010)
70#define LPC_ENET_MAXF(x) (x + 0x014)
71#define LPC_ENET_SUPP(x) (x + 0x018)
72#define LPC_ENET_TEST(x) (x + 0x01C)
73#define LPC_ENET_MCFG(x) (x + 0x020)
74#define LPC_ENET_MCMD(x) (x + 0x024)
75#define LPC_ENET_MADR(x) (x + 0x028)
76#define LPC_ENET_MWTD(x) (x + 0x02C)
77#define LPC_ENET_MRDD(x) (x + 0x030)
78#define LPC_ENET_MIND(x) (x + 0x034)
79#define LPC_ENET_SA0(x) (x + 0x040)
80#define LPC_ENET_SA1(x) (x + 0x044)
81#define LPC_ENET_SA2(x) (x + 0x048)
82#define LPC_ENET_COMMAND(x) (x + 0x100)
83#define LPC_ENET_STATUS(x) (x + 0x104)
84#define LPC_ENET_RXDESCRIPTOR(x) (x + 0x108)
85#define LPC_ENET_RXSTATUS(x) (x + 0x10C)
86#define LPC_ENET_RXDESCRIPTORNUMBER(x) (x + 0x110)
87#define LPC_ENET_RXPRODUCEINDEX(x) (x + 0x114)
88#define LPC_ENET_RXCONSUMEINDEX(x) (x + 0x118)
89#define LPC_ENET_TXDESCRIPTOR(x) (x + 0x11C)
90#define LPC_ENET_TXSTATUS(x) (x + 0x120)
91#define LPC_ENET_TXDESCRIPTORNUMBER(x) (x + 0x124)
92#define LPC_ENET_TXPRODUCEINDEX(x) (x + 0x128)
93#define LPC_ENET_TXCONSUMEINDEX(x) (x + 0x12C)
94#define LPC_ENET_TSV0(x) (x + 0x158)
95#define LPC_ENET_TSV1(x) (x + 0x15C)
96#define LPC_ENET_RSV(x) (x + 0x160)
97#define LPC_ENET_FLOWCONTROLCOUNTER(x) (x + 0x170)
98#define LPC_ENET_FLOWCONTROLSTATUS(x) (x + 0x174)
99#define LPC_ENET_RXFILTER_CTRL(x) (x + 0x200)
100#define LPC_ENET_RXFILTERWOLSTATUS(x) (x + 0x204)
101#define LPC_ENET_RXFILTERWOLCLEAR(x) (x + 0x208)
102#define LPC_ENET_HASHFILTERL(x) (x + 0x210)
103#define LPC_ENET_HASHFILTERH(x) (x + 0x214)
104#define LPC_ENET_INTSTATUS(x) (x + 0xFE0)
105#define LPC_ENET_INTENABLE(x) (x + 0xFE4)
106#define LPC_ENET_INTCLEAR(x) (x + 0xFE8)
107#define LPC_ENET_INTSET(x) (x + 0xFEC)
108#define LPC_ENET_POWERDOWN(x) (x + 0xFF4)
109
110/*
111 * mac1 register definitions
112 */
113#define LPC_MAC1_RECV_ENABLE (1 << 0)
114#define LPC_MAC1_PASS_ALL_RX_FRAMES (1 << 1)
115#define LPC_MAC1_RX_FLOW_CONTROL (1 << 2)
116#define LPC_MAC1_TX_FLOW_CONTROL (1 << 3)
117#define LPC_MAC1_LOOPBACK (1 << 4)
118#define LPC_MAC1_RESET_TX (1 << 8)
119#define LPC_MAC1_RESET_MCS_TX (1 << 9)
120#define LPC_MAC1_RESET_RX (1 << 10)
121#define LPC_MAC1_RESET_MCS_RX (1 << 11)
122#define LPC_MAC1_SIMULATION_RESET (1 << 14)
123#define LPC_MAC1_SOFT_RESET (1 << 15)
124
125/*
126 * mac2 register definitions
127 */
128#define LPC_MAC2_FULL_DUPLEX (1 << 0)
129#define LPC_MAC2_FRAME_LENGTH_CHECKING (1 << 1)
130#define LPC_MAC2_HUGH_LENGTH_CHECKING (1 << 2)
131#define LPC_MAC2_DELAYED_CRC (1 << 3)
132#define LPC_MAC2_CRC_ENABLE (1 << 4)
133#define LPC_MAC2_PAD_CRC_ENABLE (1 << 5)
134#define LPC_MAC2_VLAN_PAD_ENABLE (1 << 6)
135#define LPC_MAC2_AUTO_DETECT_PAD_ENABLE (1 << 7)
136#define LPC_MAC2_PURE_PREAMBLE_ENFORCEMENT (1 << 8)
137#define LPC_MAC2_LONG_PREAMBLE_ENFORCEMENT (1 << 9)
138#define LPC_MAC2_NO_BACKOFF (1 << 12)
139#define LPC_MAC2_BACK_PRESSURE (1 << 13)
140#define LPC_MAC2_EXCESS_DEFER (1 << 14)
141
142/*
143 * ipgt register definitions
144 */
145#define LPC_IPGT_LOAD(n) ((n) & 0x7F)
146
147/*
148 * ipgr register definitions
149 */
150#define LPC_IPGR_LOAD_PART2(n) ((n) & 0x7F)
151#define LPC_IPGR_LOAD_PART1(n) (((n) & 0x7F) << 8)
152
153/*
154 * clrt register definitions
155 */
156#define LPC_CLRT_LOAD_RETRY_MAX(n) ((n) & 0xF)
157#define LPC_CLRT_LOAD_COLLISION_WINDOW(n) (((n) & 0x3F) << 8)
158
159/*
160 * maxf register definitions
161 */
162#define LPC_MAXF_LOAD_MAX_FRAME_LEN(n) ((n) & 0xFFFF)
163
164/*
165 * supp register definitions
166 */
167#define LPC_SUPP_SPEED (1 << 8)
168#define LPC_SUPP_RESET_RMII (1 << 11)
169
170/*
171 * test register definitions
172 */
173#define LPC_TEST_SHORTCUT_PAUSE_QUANTA (1 << 0)
174#define LPC_TEST_PAUSE (1 << 1)
175#define LPC_TEST_BACKPRESSURE (1 << 2)
176
177/*
178 * mcfg register definitions
179 */
180#define LPC_MCFG_SCAN_INCREMENT (1 << 0)
181#define LPC_MCFG_SUPPRESS_PREAMBLE (1 << 1)
182#define LPC_MCFG_CLOCK_SELECT(n) (((n) & 0x7) << 2)
183#define LPC_MCFG_CLOCK_HOST_DIV_4 0
184#define LPC_MCFG_CLOCK_HOST_DIV_6 2
185#define LPC_MCFG_CLOCK_HOST_DIV_8 3
186#define LPC_MCFG_CLOCK_HOST_DIV_10 4
187#define LPC_MCFG_CLOCK_HOST_DIV_14 5
188#define LPC_MCFG_CLOCK_HOST_DIV_20 6
189#define LPC_MCFG_CLOCK_HOST_DIV_28 7
190#define LPC_MCFG_RESET_MII_MGMT (1 << 15)
191
192/*
193 * mcmd register definitions
194 */
195#define LPC_MCMD_READ (1 << 0)
196#define LPC_MCMD_SCAN (1 << 1)
197
198/*
199 * madr register definitions
200 */
201#define LPC_MADR_REGISTER_ADDRESS(n) ((n) & 0x1F)
202#define LPC_MADR_PHY_0ADDRESS(n) (((n) & 0x1F) << 8)
203
204/*
205 * mwtd register definitions
206 */
207#define LPC_MWDT_WRITE(n) ((n) & 0xFFFF)
208
209/*
210 * mrdd register definitions
211 */
212#define LPC_MRDD_READ_MASK 0xFFFF
213
214/*
215 * mind register definitions
216 */
217#define LPC_MIND_BUSY (1 << 0)
218#define LPC_MIND_SCANNING (1 << 1)
219#define LPC_MIND_NOT_VALID (1 << 2)
220#define LPC_MIND_MII_LINK_FAIL (1 << 3)
221
222/*
223 * command register definitions
224 */
225#define LPC_COMMAND_RXENABLE (1 << 0)
226#define LPC_COMMAND_TXENABLE (1 << 1)
227#define LPC_COMMAND_REG_RESET (1 << 3)
228#define LPC_COMMAND_TXRESET (1 << 4)
229#define LPC_COMMAND_RXRESET (1 << 5)
230#define LPC_COMMAND_PASSRUNTFRAME (1 << 6)
231#define LPC_COMMAND_PASSRXFILTER (1 << 7)
232#define LPC_COMMAND_TXFLOWCONTROL (1 << 8)
233#define LPC_COMMAND_RMII (1 << 9)
234#define LPC_COMMAND_FULLDUPLEX (1 << 10)
235
236/*
237 * status register definitions
238 */
239#define LPC_STATUS_RXACTIVE (1 << 0)
240#define LPC_STATUS_TXACTIVE (1 << 1)
241
242/*
243 * tsv0 register definitions
244 */
245#define LPC_TSV0_CRC_ERROR (1 << 0)
246#define LPC_TSV0_LENGTH_CHECK_ERROR (1 << 1)
247#define LPC_TSV0_LENGTH_OUT_OF_RANGE (1 << 2)
248#define LPC_TSV0_DONE (1 << 3)
249#define LPC_TSV0_MULTICAST (1 << 4)
250#define LPC_TSV0_BROADCAST (1 << 5)
251#define LPC_TSV0_PACKET_DEFER (1 << 6)
252#define LPC_TSV0_ESCESSIVE_DEFER (1 << 7)
253#define LPC_TSV0_ESCESSIVE_COLLISION (1 << 8)
254#define LPC_TSV0_LATE_COLLISION (1 << 9)
255#define LPC_TSV0_GIANT (1 << 10)
256#define LPC_TSV0_UNDERRUN (1 << 11)
257#define LPC_TSV0_TOTAL_BYTES(n) (((n) >> 12) & 0xFFFF)
258#define LPC_TSV0_CONTROL_FRAME (1 << 28)
259#define LPC_TSV0_PAUSE (1 << 29)
260#define LPC_TSV0_BACKPRESSURE (1 << 30)
261#define LPC_TSV0_VLAN (1 << 31)
262
263/*
264 * tsv1 register definitions
265 */
266#define LPC_TSV1_TRANSMIT_BYTE_COUNT(n) ((n) & 0xFFFF)
267#define LPC_TSV1_COLLISION_COUNT(n) (((n) >> 16) & 0xF)
268
269/*
270 * rsv register definitions
271 */
272#define LPC_RSV_RECEIVED_BYTE_COUNT(n) ((n) & 0xFFFF)
273#define LPC_RSV_RXDV_EVENT_IGNORED (1 << 16)
274#define LPC_RSV_RXDV_EVENT_PREVIOUSLY_SEEN (1 << 17)
275#define LPC_RSV_CARRIER_EVNT_PREVIOUS_SEEN (1 << 18)
276#define LPC_RSV_RECEIVE_CODE_VIOLATION (1 << 19)
277#define LPC_RSV_CRC_ERROR (1 << 20)
278#define LPC_RSV_LENGTH_CHECK_ERROR (1 << 21)
279#define LPC_RSV_LENGTH_OUT_OF_RANGE (1 << 22)
280#define LPC_RSV_RECEIVE_OK (1 << 23)
281#define LPC_RSV_MULTICAST (1 << 24)
282#define LPC_RSV_BROADCAST (1 << 25)
283#define LPC_RSV_DRIBBLE_NIBBLE (1 << 26)
284#define LPC_RSV_CONTROL_FRAME (1 << 27)
285#define LPC_RSV_PAUSE (1 << 28)
286#define LPC_RSV_UNSUPPORTED_OPCODE (1 << 29)
287#define LPC_RSV_VLAN (1 << 30)
288
289/*
290 * flowcontrolcounter register definitions
291 */
292#define LPC_FCCR_MIRRORCOUNTER(n) ((n) & 0xFFFF)
293#define LPC_FCCR_PAUSETIMER(n) (((n) >> 16) & 0xFFFF)
294
295/*
296 * flowcontrolstatus register definitions
297 */
298#define LPC_FCCR_MIRRORCOUNTERCURRENT(n) ((n) & 0xFFFF)
299
300/*
301 * rxfliterctrl, rxfilterwolstatus, and rxfilterwolclear shared
302 * register definitions
303 */
304#define LPC_RXFLTRW_ACCEPTUNICAST (1 << 0)
305#define LPC_RXFLTRW_ACCEPTUBROADCAST (1 << 1)
306#define LPC_RXFLTRW_ACCEPTUMULTICAST (1 << 2)
307#define LPC_RXFLTRW_ACCEPTUNICASTHASH (1 << 3)
308#define LPC_RXFLTRW_ACCEPTUMULTICASTHASH (1 << 4)
309#define LPC_RXFLTRW_ACCEPTPERFECT (1 << 5)
310
311/*
312 * rxfliterctrl register definitions
313 */
314#define LPC_RXFLTRWSTS_MAGICPACKETENWOL (1 << 12)
315#define LPC_RXFLTRWSTS_RXFILTERENWOL (1 << 13)
316
317/*
318 * rxfilterwolstatus/rxfilterwolclear register definitions
319 */
320#define LPC_RXFLTRWSTS_RXFILTERWOL (1 << 7)
321#define LPC_RXFLTRWSTS_MAGICPACKETWOL (1 << 8)
322
323/*
324 * intstatus, intenable, intclear, and Intset shared register
325 * definitions
326 */
327#define LPC_MACINT_RXOVERRUNINTEN (1 << 0)
328#define LPC_MACINT_RXERRORONINT (1 << 1)
329#define LPC_MACINT_RXFINISHEDINTEN (1 << 2)
330#define LPC_MACINT_RXDONEINTEN (1 << 3)
331#define LPC_MACINT_TXUNDERRUNINTEN (1 << 4)
332#define LPC_MACINT_TXERRORINTEN (1 << 5)
333#define LPC_MACINT_TXFINISHEDINTEN (1 << 6)
334#define LPC_MACINT_TXDONEINTEN (1 << 7)
335#define LPC_MACINT_SOFTINTEN (1 << 12)
336#define LPC_MACINT_WAKEUPINTEN (1 << 13)
337
338/*
339 * powerdown register definitions
340 */
341#define LPC_POWERDOWN_MACAHB (1 << 31)
342
Roland Stigge4de02e42012-04-22 12:01:19 +0200343static phy_interface_t lpc_phy_interface_mode(struct device *dev)
stigge@antcom.deb7370112012-03-08 11:49:17 +0000344{
Roland Stigge4de02e42012-04-22 12:01:19 +0200345 if (dev && dev->of_node) {
346 const char *mode = of_get_property(dev->of_node,
347 "phy-mode", NULL);
348 if (mode && !strcmp(mode, "mii"))
349 return PHY_INTERFACE_MODE_MII;
350 return PHY_INTERFACE_MODE_RMII;
351 }
352
353 /* non-DT */
stigge@antcom.deb7370112012-03-08 11:49:17 +0000354#ifdef CONFIG_ARCH_LPC32XX_MII_SUPPORT
355 return PHY_INTERFACE_MODE_MII;
356#else
357 return PHY_INTERFACE_MODE_RMII;
358#endif
359}
360
Roland Stigge4de02e42012-04-22 12:01:19 +0200361static bool use_iram_for_net(struct device *dev)
stigge@antcom.deb7370112012-03-08 11:49:17 +0000362{
Roland Stigge4de02e42012-04-22 12:01:19 +0200363 if (dev && dev->of_node)
364 return of_property_read_bool(dev->of_node, "use-iram");
365
366 /* non-DT */
stigge@antcom.deb7370112012-03-08 11:49:17 +0000367#ifdef CONFIG_ARCH_LPC32XX_IRAM_FOR_NET
Roland Stigge4de02e42012-04-22 12:01:19 +0200368 return true;
stigge@antcom.deb7370112012-03-08 11:49:17 +0000369#else
Roland Stigge4de02e42012-04-22 12:01:19 +0200370 return false;
stigge@antcom.deb7370112012-03-08 11:49:17 +0000371#endif
372}
373
374/* Receive Status information word */
375#define RXSTATUS_SIZE 0x000007FF
376#define RXSTATUS_CONTROL (1 << 18)
377#define RXSTATUS_VLAN (1 << 19)
378#define RXSTATUS_FILTER (1 << 20)
379#define RXSTATUS_MULTICAST (1 << 21)
380#define RXSTATUS_BROADCAST (1 << 22)
381#define RXSTATUS_CRC (1 << 23)
382#define RXSTATUS_SYMBOL (1 << 24)
383#define RXSTATUS_LENGTH (1 << 25)
384#define RXSTATUS_RANGE (1 << 26)
385#define RXSTATUS_ALIGN (1 << 27)
386#define RXSTATUS_OVERRUN (1 << 28)
387#define RXSTATUS_NODESC (1 << 29)
388#define RXSTATUS_LAST (1 << 30)
389#define RXSTATUS_ERROR (1 << 31)
390
391#define RXSTATUS_STATUS_ERROR \
392 (RXSTATUS_NODESC | RXSTATUS_OVERRUN | RXSTATUS_ALIGN | \
393 RXSTATUS_RANGE | RXSTATUS_LENGTH | RXSTATUS_SYMBOL | RXSTATUS_CRC)
394
395/* Receive Descriptor control word */
396#define RXDESC_CONTROL_SIZE 0x000007FF
397#define RXDESC_CONTROL_INT (1 << 31)
398
399/* Transmit Status information word */
400#define TXSTATUS_COLLISIONS_GET(x) (((x) >> 21) & 0xF)
401#define TXSTATUS_DEFER (1 << 25)
402#define TXSTATUS_EXCESSDEFER (1 << 26)
403#define TXSTATUS_EXCESSCOLL (1 << 27)
404#define TXSTATUS_LATECOLL (1 << 28)
405#define TXSTATUS_UNDERRUN (1 << 29)
406#define TXSTATUS_NODESC (1 << 30)
407#define TXSTATUS_ERROR (1 << 31)
408
409/* Transmit Descriptor control word */
410#define TXDESC_CONTROL_SIZE 0x000007FF
411#define TXDESC_CONTROL_OVERRIDE (1 << 26)
412#define TXDESC_CONTROL_HUGE (1 << 27)
413#define TXDESC_CONTROL_PAD (1 << 28)
414#define TXDESC_CONTROL_CRC (1 << 29)
415#define TXDESC_CONTROL_LAST (1 << 30)
416#define TXDESC_CONTROL_INT (1 << 31)
417
stigge@antcom.deb7370112012-03-08 11:49:17 +0000418/*
419 * Structure of a TX/RX descriptors and RX status
420 */
421struct txrx_desc_t {
422 __le32 packet;
423 __le32 control;
424};
425struct rx_status_t {
426 __le32 statusinfo;
427 __le32 statushashcrc;
428};
429
430/*
431 * Device driver data structure
432 */
433struct netdata_local {
434 struct platform_device *pdev;
435 struct net_device *ndev;
436 spinlock_t lock;
437 void __iomem *net_base;
438 u32 msg_enable;
Eric Dumazeta7e2eaa2012-06-12 23:58:16 +0000439 unsigned int skblen[ENET_TX_DESC];
stigge@antcom.deb7370112012-03-08 11:49:17 +0000440 unsigned int last_tx_idx;
441 unsigned int num_used_tx_buffs;
442 struct mii_bus *mii_bus;
443 struct phy_device *phy_dev;
444 struct clk *clk;
445 dma_addr_t dma_buff_base_p;
446 void *dma_buff_base_v;
447 size_t dma_buff_size;
448 struct txrx_desc_t *tx_desc_v;
449 u32 *tx_stat_v;
450 void *tx_buff_v;
451 struct txrx_desc_t *rx_desc_v;
452 struct rx_status_t *rx_stat_v;
453 void *rx_buff_v;
454 int link;
455 int speed;
456 int duplex;
457 struct napi_struct napi;
458};
459
460/*
461 * MAC support functions
462 */
463static void __lpc_set_mac(struct netdata_local *pldat, u8 *mac)
464{
465 u32 tmp;
466
467 /* Set station address */
468 tmp = mac[0] | ((u32)mac[1] << 8);
469 writel(tmp, LPC_ENET_SA2(pldat->net_base));
470 tmp = mac[2] | ((u32)mac[3] << 8);
471 writel(tmp, LPC_ENET_SA1(pldat->net_base));
472 tmp = mac[4] | ((u32)mac[5] << 8);
473 writel(tmp, LPC_ENET_SA0(pldat->net_base));
474
475 netdev_dbg(pldat->ndev, "Ethernet MAC address %pM\n", mac);
476}
477
478static void __lpc_get_mac(struct netdata_local *pldat, u8 *mac)
479{
480 u32 tmp;
481
482 /* Get station address */
483 tmp = readl(LPC_ENET_SA2(pldat->net_base));
484 mac[0] = tmp & 0xFF;
485 mac[1] = tmp >> 8;
486 tmp = readl(LPC_ENET_SA1(pldat->net_base));
487 mac[2] = tmp & 0xFF;
488 mac[3] = tmp >> 8;
489 tmp = readl(LPC_ENET_SA0(pldat->net_base));
490 mac[4] = tmp & 0xFF;
491 mac[5] = tmp >> 8;
492}
493
494static void __lpc_eth_clock_enable(struct netdata_local *pldat,
495 bool enable)
496{
497 if (enable)
498 clk_enable(pldat->clk);
499 else
500 clk_disable(pldat->clk);
501}
502
503static void __lpc_params_setup(struct netdata_local *pldat)
504{
505 u32 tmp;
506
507 if (pldat->duplex == DUPLEX_FULL) {
508 tmp = readl(LPC_ENET_MAC2(pldat->net_base));
509 tmp |= LPC_MAC2_FULL_DUPLEX;
510 writel(tmp, LPC_ENET_MAC2(pldat->net_base));
511 tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
512 tmp |= LPC_COMMAND_FULLDUPLEX;
513 writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
514 writel(LPC_IPGT_LOAD(0x15), LPC_ENET_IPGT(pldat->net_base));
515 } else {
516 tmp = readl(LPC_ENET_MAC2(pldat->net_base));
517 tmp &= ~LPC_MAC2_FULL_DUPLEX;
518 writel(tmp, LPC_ENET_MAC2(pldat->net_base));
519 tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
520 tmp &= ~LPC_COMMAND_FULLDUPLEX;
521 writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
522 writel(LPC_IPGT_LOAD(0x12), LPC_ENET_IPGT(pldat->net_base));
523 }
524
525 if (pldat->speed == SPEED_100)
526 writel(LPC_SUPP_SPEED, LPC_ENET_SUPP(pldat->net_base));
527 else
528 writel(0, LPC_ENET_SUPP(pldat->net_base));
529}
530
531static void __lpc_eth_reset(struct netdata_local *pldat)
532{
533 /* Reset all MAC logic */
534 writel((LPC_MAC1_RESET_TX | LPC_MAC1_RESET_MCS_TX | LPC_MAC1_RESET_RX |
535 LPC_MAC1_RESET_MCS_RX | LPC_MAC1_SIMULATION_RESET |
536 LPC_MAC1_SOFT_RESET), LPC_ENET_MAC1(pldat->net_base));
537 writel((LPC_COMMAND_REG_RESET | LPC_COMMAND_TXRESET |
538 LPC_COMMAND_RXRESET), LPC_ENET_COMMAND(pldat->net_base));
539}
540
541static int __lpc_mii_mngt_reset(struct netdata_local *pldat)
542{
543 /* Reset MII management hardware */
544 writel(LPC_MCFG_RESET_MII_MGMT, LPC_ENET_MCFG(pldat->net_base));
545
546 /* Setup MII clock to slowest rate with a /28 divider */
547 writel(LPC_MCFG_CLOCK_SELECT(LPC_MCFG_CLOCK_HOST_DIV_28),
548 LPC_ENET_MCFG(pldat->net_base));
549
550 return 0;
551}
552
553static inline phys_addr_t __va_to_pa(void *addr, struct netdata_local *pldat)
554{
555 phys_addr_t phaddr;
556
557 phaddr = addr - pldat->dma_buff_base_v;
558 phaddr += pldat->dma_buff_base_p;
559
560 return phaddr;
561}
562
563static void lpc_eth_enable_int(void __iomem *regbase)
564{
565 writel((LPC_MACINT_RXDONEINTEN | LPC_MACINT_TXDONEINTEN),
566 LPC_ENET_INTENABLE(regbase));
567}
568
569static void lpc_eth_disable_int(void __iomem *regbase)
570{
571 writel(0, LPC_ENET_INTENABLE(regbase));
572}
573
574/* Setup TX/RX descriptors */
575static void __lpc_txrx_desc_setup(struct netdata_local *pldat)
576{
577 u32 *ptxstat;
578 void *tbuff;
579 int i;
580 struct txrx_desc_t *ptxrxdesc;
581 struct rx_status_t *prxstat;
582
583 tbuff = PTR_ALIGN(pldat->dma_buff_base_v, 16);
584
585 /* Setup TX descriptors, status, and buffers */
586 pldat->tx_desc_v = tbuff;
587 tbuff += sizeof(struct txrx_desc_t) * ENET_TX_DESC;
588
589 pldat->tx_stat_v = tbuff;
590 tbuff += sizeof(u32) * ENET_TX_DESC;
591
592 tbuff = PTR_ALIGN(tbuff, 16);
593 pldat->tx_buff_v = tbuff;
594 tbuff += ENET_MAXF_SIZE * ENET_TX_DESC;
595
596 /* Setup RX descriptors, status, and buffers */
597 pldat->rx_desc_v = tbuff;
598 tbuff += sizeof(struct txrx_desc_t) * ENET_RX_DESC;
599
600 tbuff = PTR_ALIGN(tbuff, 16);
601 pldat->rx_stat_v = tbuff;
602 tbuff += sizeof(struct rx_status_t) * ENET_RX_DESC;
603
604 tbuff = PTR_ALIGN(tbuff, 16);
605 pldat->rx_buff_v = tbuff;
606 tbuff += ENET_MAXF_SIZE * ENET_RX_DESC;
607
608 /* Map the TX descriptors to the TX buffers in hardware */
609 for (i = 0; i < ENET_TX_DESC; i++) {
610 ptxstat = &pldat->tx_stat_v[i];
611 ptxrxdesc = &pldat->tx_desc_v[i];
612
613 ptxrxdesc->packet = __va_to_pa(
614 pldat->tx_buff_v + i * ENET_MAXF_SIZE, pldat);
615 ptxrxdesc->control = 0;
616 *ptxstat = 0;
617 }
618
619 /* Map the RX descriptors to the RX buffers in hardware */
620 for (i = 0; i < ENET_RX_DESC; i++) {
621 prxstat = &pldat->rx_stat_v[i];
622 ptxrxdesc = &pldat->rx_desc_v[i];
623
624 ptxrxdesc->packet = __va_to_pa(
625 pldat->rx_buff_v + i * ENET_MAXF_SIZE, pldat);
626 ptxrxdesc->control = RXDESC_CONTROL_INT | (ENET_MAXF_SIZE - 1);
627 prxstat->statusinfo = 0;
628 prxstat->statushashcrc = 0;
629 }
630
631 /* Setup base addresses in hardware to point to buffers and
632 * descriptors
633 */
634 writel((ENET_TX_DESC - 1),
635 LPC_ENET_TXDESCRIPTORNUMBER(pldat->net_base));
636 writel(__va_to_pa(pldat->tx_desc_v, pldat),
637 LPC_ENET_TXDESCRIPTOR(pldat->net_base));
638 writel(__va_to_pa(pldat->tx_stat_v, pldat),
639 LPC_ENET_TXSTATUS(pldat->net_base));
640 writel((ENET_RX_DESC - 1),
641 LPC_ENET_RXDESCRIPTORNUMBER(pldat->net_base));
642 writel(__va_to_pa(pldat->rx_desc_v, pldat),
643 LPC_ENET_RXDESCRIPTOR(pldat->net_base));
644 writel(__va_to_pa(pldat->rx_stat_v, pldat),
645 LPC_ENET_RXSTATUS(pldat->net_base));
646}
647
648static void __lpc_eth_init(struct netdata_local *pldat)
649{
650 u32 tmp;
651
652 /* Disable controller and reset */
653 tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
654 tmp &= ~LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
655 writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
656 tmp = readl(LPC_ENET_MAC1(pldat->net_base));
657 tmp &= ~LPC_MAC1_RECV_ENABLE;
658 writel(tmp, LPC_ENET_MAC1(pldat->net_base));
659
660 /* Initial MAC setup */
661 writel(LPC_MAC1_PASS_ALL_RX_FRAMES, LPC_ENET_MAC1(pldat->net_base));
662 writel((LPC_MAC2_PAD_CRC_ENABLE | LPC_MAC2_CRC_ENABLE),
663 LPC_ENET_MAC2(pldat->net_base));
664 writel(ENET_MAXF_SIZE, LPC_ENET_MAXF(pldat->net_base));
665
666 /* Collision window, gap */
667 writel((LPC_CLRT_LOAD_RETRY_MAX(0xF) |
668 LPC_CLRT_LOAD_COLLISION_WINDOW(0x37)),
669 LPC_ENET_CLRT(pldat->net_base));
670 writel(LPC_IPGR_LOAD_PART2(0x12), LPC_ENET_IPGR(pldat->net_base));
671
Roland Stigge4de02e42012-04-22 12:01:19 +0200672 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
stigge@antcom.deb7370112012-03-08 11:49:17 +0000673 writel(LPC_COMMAND_PASSRUNTFRAME,
674 LPC_ENET_COMMAND(pldat->net_base));
675 else {
676 writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
677 LPC_ENET_COMMAND(pldat->net_base));
678 writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
679 }
680
681 __lpc_params_setup(pldat);
682
683 /* Setup TX and RX descriptors */
684 __lpc_txrx_desc_setup(pldat);
685
686 /* Setup packet filtering */
687 writel((LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT),
688 LPC_ENET_RXFILTER_CTRL(pldat->net_base));
689
690 /* Get the next TX buffer output index */
691 pldat->num_used_tx_buffs = 0;
692 pldat->last_tx_idx =
693 readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
694
695 /* Clear and enable interrupts */
696 writel(0xFFFF, LPC_ENET_INTCLEAR(pldat->net_base));
697 smp_wmb();
698 lpc_eth_enable_int(pldat->net_base);
699
700 /* Enable controller */
701 tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
702 tmp |= LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
703 writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
704 tmp = readl(LPC_ENET_MAC1(pldat->net_base));
705 tmp |= LPC_MAC1_RECV_ENABLE;
706 writel(tmp, LPC_ENET_MAC1(pldat->net_base));
707}
708
709static void __lpc_eth_shutdown(struct netdata_local *pldat)
710{
711 /* Reset ethernet and power down PHY */
712 __lpc_eth_reset(pldat);
713 writel(0, LPC_ENET_MAC1(pldat->net_base));
714 writel(0, LPC_ENET_MAC2(pldat->net_base));
715}
716
717/*
718 * MAC<--->PHY support functions
719 */
720static int lpc_mdio_read(struct mii_bus *bus, int phy_id, int phyreg)
721{
722 struct netdata_local *pldat = bus->priv;
723 unsigned long timeout = jiffies + msecs_to_jiffies(100);
724 int lps;
725
726 writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
727 writel(LPC_MCMD_READ, LPC_ENET_MCMD(pldat->net_base));
728
729 /* Wait for unbusy status */
730 while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
731 if (time_after(jiffies, timeout))
732 return -EIO;
733 cpu_relax();
734 }
735
736 lps = readl(LPC_ENET_MRDD(pldat->net_base));
737 writel(0, LPC_ENET_MCMD(pldat->net_base));
738
739 return lps;
740}
741
742static int lpc_mdio_write(struct mii_bus *bus, int phy_id, int phyreg,
743 u16 phydata)
744{
745 struct netdata_local *pldat = bus->priv;
746 unsigned long timeout = jiffies + msecs_to_jiffies(100);
747
748 writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
749 writel(phydata, LPC_ENET_MWTD(pldat->net_base));
750
751 /* Wait for completion */
752 while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
753 if (time_after(jiffies, timeout))
754 return -EIO;
755 cpu_relax();
756 }
757
758 return 0;
759}
760
761static int lpc_mdio_reset(struct mii_bus *bus)
762{
763 return __lpc_mii_mngt_reset((struct netdata_local *)bus->priv);
764}
765
766static void lpc_handle_link_change(struct net_device *ndev)
767{
768 struct netdata_local *pldat = netdev_priv(ndev);
769 struct phy_device *phydev = pldat->phy_dev;
770 unsigned long flags;
771
772 bool status_change = false;
773
774 spin_lock_irqsave(&pldat->lock, flags);
775
776 if (phydev->link) {
777 if ((pldat->speed != phydev->speed) ||
778 (pldat->duplex != phydev->duplex)) {
779 pldat->speed = phydev->speed;
780 pldat->duplex = phydev->duplex;
781 status_change = true;
782 }
783 }
784
785 if (phydev->link != pldat->link) {
786 if (!phydev->link) {
787 pldat->speed = 0;
788 pldat->duplex = -1;
789 }
790 pldat->link = phydev->link;
791
792 status_change = true;
793 }
794
795 spin_unlock_irqrestore(&pldat->lock, flags);
796
797 if (status_change)
798 __lpc_params_setup(pldat);
799}
800
801static int lpc_mii_probe(struct net_device *ndev)
802{
803 struct netdata_local *pldat = netdev_priv(ndev);
804 struct phy_device *phydev = phy_find_first(pldat->mii_bus);
805
806 if (!phydev) {
807 netdev_err(ndev, "no PHY found\n");
808 return -ENODEV;
809 }
810
811 /* Attach to the PHY */
Roland Stigge4de02e42012-04-22 12:01:19 +0200812 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
stigge@antcom.deb7370112012-03-08 11:49:17 +0000813 netdev_info(ndev, "using MII interface\n");
814 else
815 netdev_info(ndev, "using RMII interface\n");
816 phydev = phy_connect(ndev, dev_name(&phydev->dev),
Roland Stigge4de02e42012-04-22 12:01:19 +0200817 &lpc_handle_link_change, 0,
818 lpc_phy_interface_mode(&pldat->pdev->dev));
stigge@antcom.deb7370112012-03-08 11:49:17 +0000819
820 if (IS_ERR(phydev)) {
821 netdev_err(ndev, "Could not attach to PHY\n");
822 return PTR_ERR(phydev);
823 }
824
825 /* mask with MAC supported features */
826 phydev->supported &= PHY_BASIC_FEATURES;
827
828 phydev->advertising = phydev->supported;
829
830 pldat->link = 0;
831 pldat->speed = 0;
832 pldat->duplex = -1;
833 pldat->phy_dev = phydev;
834
835 netdev_info(ndev,
836 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
837 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
838 return 0;
839}
840
841static int lpc_mii_init(struct netdata_local *pldat)
842{
843 int err = -ENXIO, i;
844
845 pldat->mii_bus = mdiobus_alloc();
846 if (!pldat->mii_bus) {
847 err = -ENOMEM;
848 goto err_out;
849 }
850
851 /* Setup MII mode */
Roland Stigge4de02e42012-04-22 12:01:19 +0200852 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
stigge@antcom.deb7370112012-03-08 11:49:17 +0000853 writel(LPC_COMMAND_PASSRUNTFRAME,
854 LPC_ENET_COMMAND(pldat->net_base));
855 else {
856 writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
857 LPC_ENET_COMMAND(pldat->net_base));
858 writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
859 }
860
861 pldat->mii_bus->name = "lpc_mii_bus";
862 pldat->mii_bus->read = &lpc_mdio_read;
863 pldat->mii_bus->write = &lpc_mdio_write;
864 pldat->mii_bus->reset = &lpc_mdio_reset;
865 snprintf(pldat->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
866 pldat->pdev->name, pldat->pdev->id);
867 pldat->mii_bus->priv = pldat;
868 pldat->mii_bus->parent = &pldat->pdev->dev;
869
870 pldat->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
871 if (!pldat->mii_bus->irq) {
872 err = -ENOMEM;
873 goto err_out_1;
874 }
875
876 for (i = 0; i < PHY_MAX_ADDR; i++)
877 pldat->mii_bus->irq[i] = PHY_POLL;
878
879 platform_set_drvdata(pldat->pdev, pldat->mii_bus);
880
881 if (mdiobus_register(pldat->mii_bus))
882 goto err_out_free_mdio_irq;
883
884 if (lpc_mii_probe(pldat->ndev) != 0)
885 goto err_out_unregister_bus;
886
887 return 0;
888
889err_out_unregister_bus:
890 mdiobus_unregister(pldat->mii_bus);
891err_out_free_mdio_irq:
892 kfree(pldat->mii_bus->irq);
893err_out_1:
894 mdiobus_free(pldat->mii_bus);
895err_out:
896 return err;
897}
898
899static void __lpc_handle_xmit(struct net_device *ndev)
900{
901 struct netdata_local *pldat = netdev_priv(ndev);
stigge@antcom.deb7370112012-03-08 11:49:17 +0000902 u32 txcidx, *ptxstat, txstat;
903
904 txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
905 while (pldat->last_tx_idx != txcidx) {
Eric Dumazeta7e2eaa2012-06-12 23:58:16 +0000906 unsigned int skblen = pldat->skblen[pldat->last_tx_idx];
stigge@antcom.deb7370112012-03-08 11:49:17 +0000907
908 /* A buffer is available, get buffer status */
909 ptxstat = &pldat->tx_stat_v[pldat->last_tx_idx];
910 txstat = *ptxstat;
911
912 /* Next buffer and decrement used buffer counter */
913 pldat->num_used_tx_buffs--;
914 pldat->last_tx_idx++;
915 if (pldat->last_tx_idx >= ENET_TX_DESC)
916 pldat->last_tx_idx = 0;
917
918 /* Update collision counter */
919 ndev->stats.collisions += TXSTATUS_COLLISIONS_GET(txstat);
920
921 /* Any errors occurred? */
922 if (txstat & TXSTATUS_ERROR) {
923 if (txstat & TXSTATUS_UNDERRUN) {
924 /* FIFO underrun */
925 ndev->stats.tx_fifo_errors++;
926 }
927 if (txstat & TXSTATUS_LATECOLL) {
928 /* Late collision */
929 ndev->stats.tx_aborted_errors++;
930 }
931 if (txstat & TXSTATUS_EXCESSCOLL) {
932 /* Excessive collision */
933 ndev->stats.tx_aborted_errors++;
934 }
935 if (txstat & TXSTATUS_EXCESSDEFER) {
936 /* Defer limit */
937 ndev->stats.tx_aborted_errors++;
938 }
939 ndev->stats.tx_errors++;
940 } else {
941 /* Update stats */
942 ndev->stats.tx_packets++;
Eric Dumazeta7e2eaa2012-06-12 23:58:16 +0000943 ndev->stats.tx_bytes += skblen;
stigge@antcom.deb7370112012-03-08 11:49:17 +0000944 }
945
946 txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
947 }
948
Eric Dumazet3f16da52012-06-11 07:21:36 +0000949 if (pldat->num_used_tx_buffs <= ENET_TX_DESC/2) {
950 if (netif_queue_stopped(ndev))
951 netif_wake_queue(ndev);
952 }
stigge@antcom.deb7370112012-03-08 11:49:17 +0000953}
954
955static int __lpc_handle_recv(struct net_device *ndev, int budget)
956{
957 struct netdata_local *pldat = netdev_priv(ndev);
958 struct sk_buff *skb;
959 u32 rxconsidx, len, ethst;
960 struct rx_status_t *prxstat;
961 u8 *prdbuf;
962 int rx_done = 0;
963
964 /* Get the current RX buffer indexes */
965 rxconsidx = readl(LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
966 while (rx_done < budget && rxconsidx !=
967 readl(LPC_ENET_RXPRODUCEINDEX(pldat->net_base))) {
968 /* Get pointer to receive status */
969 prxstat = &pldat->rx_stat_v[rxconsidx];
970 len = (prxstat->statusinfo & RXSTATUS_SIZE) + 1;
971
972 /* Status error? */
973 ethst = prxstat->statusinfo;
974 if ((ethst & (RXSTATUS_ERROR | RXSTATUS_STATUS_ERROR)) ==
975 (RXSTATUS_ERROR | RXSTATUS_RANGE))
976 ethst &= ~RXSTATUS_ERROR;
977
978 if (ethst & RXSTATUS_ERROR) {
979 int si = prxstat->statusinfo;
980 /* Check statuses */
981 if (si & RXSTATUS_OVERRUN) {
982 /* Overrun error */
983 ndev->stats.rx_fifo_errors++;
984 } else if (si & RXSTATUS_CRC) {
985 /* CRC error */
986 ndev->stats.rx_crc_errors++;
987 } else if (si & RXSTATUS_LENGTH) {
988 /* Length error */
989 ndev->stats.rx_length_errors++;
990 } else if (si & RXSTATUS_ERROR) {
991 /* Other error */
992 ndev->stats.rx_length_errors++;
993 }
994 ndev->stats.rx_errors++;
995 } else {
996 /* Packet is good */
Eric Dumazete7f8c1f2012-04-03 12:02:11 +0000997 skb = dev_alloc_skb(len);
998 if (!skb) {
stigge@antcom.deb7370112012-03-08 11:49:17 +0000999 ndev->stats.rx_dropped++;
Eric Dumazete7f8c1f2012-04-03 12:02:11 +00001000 } else {
stigge@antcom.deb7370112012-03-08 11:49:17 +00001001 prdbuf = skb_put(skb, len);
1002
1003 /* Copy packet from buffer */
1004 memcpy(prdbuf, pldat->rx_buff_v +
1005 rxconsidx * ENET_MAXF_SIZE, len);
1006
1007 /* Pass to upper layer */
1008 skb->protocol = eth_type_trans(skb, ndev);
1009 netif_receive_skb(skb);
1010 ndev->stats.rx_packets++;
1011 ndev->stats.rx_bytes += len;
1012 }
1013 }
1014
1015 /* Increment consume index */
1016 rxconsidx = rxconsidx + 1;
1017 if (rxconsidx >= ENET_RX_DESC)
1018 rxconsidx = 0;
1019 writel(rxconsidx,
1020 LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
1021 rx_done++;
1022 }
1023
1024 return rx_done;
1025}
1026
1027static int lpc_eth_poll(struct napi_struct *napi, int budget)
1028{
1029 struct netdata_local *pldat = container_of(napi,
1030 struct netdata_local, napi);
1031 struct net_device *ndev = pldat->ndev;
1032 int rx_done = 0;
1033 struct netdev_queue *txq = netdev_get_tx_queue(ndev, 0);
1034
1035 __netif_tx_lock(txq, smp_processor_id());
1036 __lpc_handle_xmit(ndev);
1037 __netif_tx_unlock(txq);
1038 rx_done = __lpc_handle_recv(ndev, budget);
1039
1040 if (rx_done < budget) {
1041 napi_complete(napi);
1042 lpc_eth_enable_int(pldat->net_base);
1043 }
1044
1045 return rx_done;
1046}
1047
1048static irqreturn_t __lpc_eth_interrupt(int irq, void *dev_id)
1049{
1050 struct net_device *ndev = dev_id;
1051 struct netdata_local *pldat = netdev_priv(ndev);
1052 u32 tmp;
1053
1054 spin_lock(&pldat->lock);
1055
1056 tmp = readl(LPC_ENET_INTSTATUS(pldat->net_base));
1057 /* Clear interrupts */
1058 writel(tmp, LPC_ENET_INTCLEAR(pldat->net_base));
1059
1060 lpc_eth_disable_int(pldat->net_base);
1061 if (likely(napi_schedule_prep(&pldat->napi)))
1062 __napi_schedule(&pldat->napi);
1063
1064 spin_unlock(&pldat->lock);
1065
1066 return IRQ_HANDLED;
1067}
1068
1069static int lpc_eth_close(struct net_device *ndev)
1070{
1071 unsigned long flags;
1072 struct netdata_local *pldat = netdev_priv(ndev);
1073
1074 if (netif_msg_ifdown(pldat))
1075 dev_dbg(&pldat->pdev->dev, "shutting down %s\n", ndev->name);
1076
1077 napi_disable(&pldat->napi);
1078 netif_stop_queue(ndev);
1079
1080 if (pldat->phy_dev)
1081 phy_stop(pldat->phy_dev);
1082
1083 spin_lock_irqsave(&pldat->lock, flags);
1084 __lpc_eth_reset(pldat);
1085 netif_carrier_off(ndev);
1086 writel(0, LPC_ENET_MAC1(pldat->net_base));
1087 writel(0, LPC_ENET_MAC2(pldat->net_base));
1088 spin_unlock_irqrestore(&pldat->lock, flags);
1089
1090 __lpc_eth_clock_enable(pldat, false);
1091
1092 return 0;
1093}
1094
1095static int lpc_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1096{
1097 struct netdata_local *pldat = netdev_priv(ndev);
1098 u32 len, txidx;
1099 u32 *ptxstat;
1100 struct txrx_desc_t *ptxrxdesc;
1101
1102 len = skb->len;
1103
1104 spin_lock_irq(&pldat->lock);
1105
1106 if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1)) {
1107 /* This function should never be called when there are no
1108 buffers */
1109 netif_stop_queue(ndev);
1110 spin_unlock_irq(&pldat->lock);
1111 WARN(1, "BUG! TX request when no free TX buffers!\n");
1112 return NETDEV_TX_BUSY;
1113 }
1114
1115 /* Get the next TX descriptor index */
1116 txidx = readl(LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
1117
1118 /* Setup control for the transfer */
1119 ptxstat = &pldat->tx_stat_v[txidx];
1120 *ptxstat = 0;
1121 ptxrxdesc = &pldat->tx_desc_v[txidx];
1122 ptxrxdesc->control =
1123 (len - 1) | TXDESC_CONTROL_LAST | TXDESC_CONTROL_INT;
1124
1125 /* Copy data to the DMA buffer */
1126 memcpy(pldat->tx_buff_v + txidx * ENET_MAXF_SIZE, skb->data, len);
1127
1128 /* Save the buffer and increment the buffer counter */
Eric Dumazeta7e2eaa2012-06-12 23:58:16 +00001129 pldat->skblen[txidx] = len;
stigge@antcom.deb7370112012-03-08 11:49:17 +00001130 pldat->num_used_tx_buffs++;
1131
1132 /* Start transmit */
1133 txidx++;
1134 if (txidx >= ENET_TX_DESC)
1135 txidx = 0;
1136 writel(txidx, LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
1137
1138 /* Stop queue if no more TX buffers */
1139 if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1))
1140 netif_stop_queue(ndev);
1141
1142 spin_unlock_irq(&pldat->lock);
1143
Eric Dumazeta7e2eaa2012-06-12 23:58:16 +00001144 dev_kfree_skb(skb);
stigge@antcom.deb7370112012-03-08 11:49:17 +00001145 return NETDEV_TX_OK;
1146}
1147
1148static int lpc_set_mac_address(struct net_device *ndev, void *p)
1149{
1150 struct sockaddr *addr = p;
1151 struct netdata_local *pldat = netdev_priv(ndev);
1152 unsigned long flags;
1153
1154 if (!is_valid_ether_addr(addr->sa_data))
1155 return -EADDRNOTAVAIL;
1156 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
1157
1158 spin_lock_irqsave(&pldat->lock, flags);
1159
1160 /* Set station address */
1161 __lpc_set_mac(pldat, ndev->dev_addr);
1162
1163 spin_unlock_irqrestore(&pldat->lock, flags);
1164
1165 return 0;
1166}
1167
1168static void lpc_eth_set_multicast_list(struct net_device *ndev)
1169{
1170 struct netdata_local *pldat = netdev_priv(ndev);
1171 struct netdev_hw_addr_list *mcptr = &ndev->mc;
1172 struct netdev_hw_addr *ha;
1173 u32 tmp32, hash_val, hashlo, hashhi;
1174 unsigned long flags;
1175
1176 spin_lock_irqsave(&pldat->lock, flags);
1177
1178 /* Set station address */
1179 __lpc_set_mac(pldat, ndev->dev_addr);
1180
1181 tmp32 = LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT;
1182
1183 if (ndev->flags & IFF_PROMISC)
1184 tmp32 |= LPC_RXFLTRW_ACCEPTUNICAST |
1185 LPC_RXFLTRW_ACCEPTUMULTICAST;
1186 if (ndev->flags & IFF_ALLMULTI)
1187 tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICAST;
1188
1189 if (netdev_hw_addr_list_count(mcptr))
1190 tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICASTHASH;
1191
1192 writel(tmp32, LPC_ENET_RXFILTER_CTRL(pldat->net_base));
1193
1194
1195 /* Set initial hash table */
1196 hashlo = 0x0;
1197 hashhi = 0x0;
1198
1199 /* 64 bits : multicast address in hash table */
1200 netdev_hw_addr_list_for_each(ha, mcptr) {
1201 hash_val = (ether_crc(6, ha->addr) >> 23) & 0x3F;
1202
1203 if (hash_val >= 32)
1204 hashhi |= 1 << (hash_val - 32);
1205 else
1206 hashlo |= 1 << hash_val;
1207 }
1208
1209 writel(hashlo, LPC_ENET_HASHFILTERL(pldat->net_base));
1210 writel(hashhi, LPC_ENET_HASHFILTERH(pldat->net_base));
1211
1212 spin_unlock_irqrestore(&pldat->lock, flags);
1213}
1214
1215static int lpc_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1216{
1217 struct netdata_local *pldat = netdev_priv(ndev);
1218 struct phy_device *phydev = pldat->phy_dev;
1219
1220 if (!netif_running(ndev))
1221 return -EINVAL;
1222
1223 if (!phydev)
1224 return -ENODEV;
1225
1226 return phy_mii_ioctl(phydev, req, cmd);
1227}
1228
1229static int lpc_eth_open(struct net_device *ndev)
1230{
1231 struct netdata_local *pldat = netdev_priv(ndev);
1232
1233 if (netif_msg_ifup(pldat))
1234 dev_dbg(&pldat->pdev->dev, "enabling %s\n", ndev->name);
1235
1236 if (!is_valid_ether_addr(ndev->dev_addr))
1237 return -EADDRNOTAVAIL;
1238
1239 __lpc_eth_clock_enable(pldat, true);
1240
1241 /* Reset and initialize */
1242 __lpc_eth_reset(pldat);
1243 __lpc_eth_init(pldat);
1244
1245 /* schedule a link state check */
1246 phy_start(pldat->phy_dev);
1247 netif_start_queue(ndev);
1248 napi_enable(&pldat->napi);
1249
1250 return 0;
1251}
1252
1253/*
1254 * Ethtool ops
1255 */
1256static void lpc_eth_ethtool_getdrvinfo(struct net_device *ndev,
1257 struct ethtool_drvinfo *info)
1258{
1259 strcpy(info->driver, MODNAME);
1260 strcpy(info->version, DRV_VERSION);
1261 strcpy(info->bus_info, dev_name(ndev->dev.parent));
1262}
1263
1264static u32 lpc_eth_ethtool_getmsglevel(struct net_device *ndev)
1265{
1266 struct netdata_local *pldat = netdev_priv(ndev);
1267
1268 return pldat->msg_enable;
1269}
1270
1271static void lpc_eth_ethtool_setmsglevel(struct net_device *ndev, u32 level)
1272{
1273 struct netdata_local *pldat = netdev_priv(ndev);
1274
1275 pldat->msg_enable = level;
1276}
1277
1278static int lpc_eth_ethtool_getsettings(struct net_device *ndev,
1279 struct ethtool_cmd *cmd)
1280{
1281 struct netdata_local *pldat = netdev_priv(ndev);
1282 struct phy_device *phydev = pldat->phy_dev;
1283
1284 if (!phydev)
1285 return -EOPNOTSUPP;
1286
1287 return phy_ethtool_gset(phydev, cmd);
1288}
1289
1290static int lpc_eth_ethtool_setsettings(struct net_device *ndev,
1291 struct ethtool_cmd *cmd)
1292{
1293 struct netdata_local *pldat = netdev_priv(ndev);
1294 struct phy_device *phydev = pldat->phy_dev;
1295
1296 if (!phydev)
1297 return -EOPNOTSUPP;
1298
1299 return phy_ethtool_sset(phydev, cmd);
1300}
1301
1302static const struct ethtool_ops lpc_eth_ethtool_ops = {
1303 .get_drvinfo = lpc_eth_ethtool_getdrvinfo,
1304 .get_settings = lpc_eth_ethtool_getsettings,
1305 .set_settings = lpc_eth_ethtool_setsettings,
1306 .get_msglevel = lpc_eth_ethtool_getmsglevel,
1307 .set_msglevel = lpc_eth_ethtool_setmsglevel,
1308 .get_link = ethtool_op_get_link,
1309};
1310
1311static const struct net_device_ops lpc_netdev_ops = {
1312 .ndo_open = lpc_eth_open,
1313 .ndo_stop = lpc_eth_close,
1314 .ndo_start_xmit = lpc_eth_hard_start_xmit,
1315 .ndo_set_rx_mode = lpc_eth_set_multicast_list,
1316 .ndo_do_ioctl = lpc_eth_ioctl,
1317 .ndo_set_mac_address = lpc_set_mac_address,
Eric Dumazete3047852012-06-10 23:24:00 +00001318 .ndo_change_mtu = eth_change_mtu,
stigge@antcom.deb7370112012-03-08 11:49:17 +00001319};
1320
1321static int lpc_eth_drv_probe(struct platform_device *pdev)
1322{
1323 struct resource *res;
stigge@antcom.deb7370112012-03-08 11:49:17 +00001324 struct net_device *ndev;
1325 struct netdata_local *pldat;
1326 struct phy_device *phydev;
1327 dma_addr_t dma_handle;
1328 int irq, ret;
Roland Stigge4de02e42012-04-22 12:01:19 +02001329 u32 tmp;
1330
1331 /* Setup network interface for RMII or MII mode */
1332 tmp = __raw_readl(LPC32XX_CLKPWR_MACCLK_CTRL);
1333 tmp &= ~LPC32XX_CLKPWR_MACCTRL_PINS_MSK;
1334 if (lpc_phy_interface_mode(&pdev->dev) == PHY_INTERFACE_MODE_MII)
1335 tmp |= LPC32XX_CLKPWR_MACCTRL_USE_MII_PINS;
1336 else
1337 tmp |= LPC32XX_CLKPWR_MACCTRL_USE_RMII_PINS;
1338 __raw_writel(tmp, LPC32XX_CLKPWR_MACCLK_CTRL);
stigge@antcom.deb7370112012-03-08 11:49:17 +00001339
1340 /* Get platform resources */
1341 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
stigge@antcom.deb7370112012-03-08 11:49:17 +00001342 irq = platform_get_irq(pdev, 0);
Roland Stigge4de02e42012-04-22 12:01:19 +02001343 if ((!res) || (irq < 0) || (irq >= NR_IRQS)) {
stigge@antcom.deb7370112012-03-08 11:49:17 +00001344 dev_err(&pdev->dev, "error getting resources.\n");
1345 ret = -ENXIO;
1346 goto err_exit;
1347 }
1348
1349 /* Allocate net driver data structure */
1350 ndev = alloc_etherdev(sizeof(struct netdata_local));
1351 if (!ndev) {
1352 dev_err(&pdev->dev, "could not allocate device.\n");
1353 ret = -ENOMEM;
1354 goto err_exit;
1355 }
1356
1357 SET_NETDEV_DEV(ndev, &pdev->dev);
1358
1359 pldat = netdev_priv(ndev);
1360 pldat->pdev = pdev;
1361 pldat->ndev = ndev;
1362
1363 spin_lock_init(&pldat->lock);
1364
1365 /* Save resources */
1366 ndev->irq = irq;
1367
1368 /* Get clock for the device */
1369 pldat->clk = clk_get(&pdev->dev, NULL);
1370 if (IS_ERR(pldat->clk)) {
1371 dev_err(&pdev->dev, "error getting clock.\n");
1372 ret = PTR_ERR(pldat->clk);
1373 goto err_out_free_dev;
1374 }
1375
1376 /* Enable network clock */
1377 __lpc_eth_clock_enable(pldat, true);
1378
1379 /* Map IO space */
1380 pldat->net_base = ioremap(res->start, res->end - res->start + 1);
1381 if (!pldat->net_base) {
1382 dev_err(&pdev->dev, "failed to map registers\n");
1383 ret = -ENOMEM;
1384 goto err_out_disable_clocks;
1385 }
1386 ret = request_irq(ndev->irq, __lpc_eth_interrupt, 0,
1387 ndev->name, ndev);
1388 if (ret) {
1389 dev_err(&pdev->dev, "error requesting interrupt.\n");
1390 goto err_out_iounmap;
1391 }
1392
1393 /* Fill in the fields of the device structure with ethernet values. */
1394 ether_setup(ndev);
1395
1396 /* Setup driver functions */
1397 ndev->netdev_ops = &lpc_netdev_ops;
1398 ndev->ethtool_ops = &lpc_eth_ethtool_ops;
1399 ndev->watchdog_timeo = msecs_to_jiffies(2500);
1400
1401 /* Get size of DMA buffers/descriptors region */
1402 pldat->dma_buff_size = (ENET_TX_DESC + ENET_RX_DESC) * (ENET_MAXF_SIZE +
1403 sizeof(struct txrx_desc_t) + sizeof(struct rx_status_t));
1404 pldat->dma_buff_base_v = 0;
1405
Roland Stigge4de02e42012-04-22 12:01:19 +02001406 if (use_iram_for_net(&pldat->pdev->dev)) {
1407 dma_handle = LPC32XX_IRAM_BASE;
stigge@antcom.deb7370112012-03-08 11:49:17 +00001408 if (pldat->dma_buff_size <= lpc32xx_return_iram_size())
1409 pldat->dma_buff_base_v =
Roland Stigge4de02e42012-04-22 12:01:19 +02001410 io_p2v(LPC32XX_IRAM_BASE);
stigge@antcom.deb7370112012-03-08 11:49:17 +00001411 else
1412 netdev_err(ndev,
1413 "IRAM not big enough for net buffers, using SDRAM instead.\n");
1414 }
1415
1416 if (pldat->dma_buff_base_v == 0) {
Roland Stigge4de02e42012-04-22 12:01:19 +02001417 pldat->pdev->dev.coherent_dma_mask = 0xFFFFFFFF;
1418 pldat->pdev->dev.dma_mask = &pldat->pdev->dev.coherent_dma_mask;
stigge@antcom.deb7370112012-03-08 11:49:17 +00001419 pldat->dma_buff_size = PAGE_ALIGN(pldat->dma_buff_size);
1420
1421 /* Allocate a chunk of memory for the DMA ethernet buffers
1422 and descriptors */
1423 pldat->dma_buff_base_v =
1424 dma_alloc_coherent(&pldat->pdev->dev,
1425 pldat->dma_buff_size, &dma_handle,
1426 GFP_KERNEL);
1427
1428 if (pldat->dma_buff_base_v == NULL) {
1429 dev_err(&pdev->dev, "error getting DMA region.\n");
1430 ret = -ENOMEM;
1431 goto err_out_free_irq;
1432 }
1433 }
1434 pldat->dma_buff_base_p = dma_handle;
1435
1436 netdev_dbg(ndev, "IO address start :0x%08x\n",
1437 res->start);
1438 netdev_dbg(ndev, "IO address size :%d\n",
1439 res->end - res->start + 1);
stigge@antcom.deb31525d2012-06-18 10:14:42 +00001440 netdev_dbg(ndev, "IO address (mapped) :0x%p\n",
stigge@antcom.deb7370112012-03-08 11:49:17 +00001441 pldat->net_base);
1442 netdev_dbg(ndev, "IRQ number :%d\n", ndev->irq);
1443 netdev_dbg(ndev, "DMA buffer size :%d\n", pldat->dma_buff_size);
1444 netdev_dbg(ndev, "DMA buffer P address :0x%08x\n",
1445 pldat->dma_buff_base_p);
1446 netdev_dbg(ndev, "DMA buffer V address :0x%p\n",
1447 pldat->dma_buff_base_v);
1448
1449 /* Get MAC address from current HW setting (POR state is all zeros) */
1450 __lpc_get_mac(pldat, ndev->dev_addr);
1451
1452#ifdef CONFIG_OF_NET
1453 if (!is_valid_ether_addr(ndev->dev_addr)) {
1454 const char *macaddr = of_get_mac_address(pdev->dev.of_node);
1455 if (macaddr)
1456 memcpy(ndev->dev_addr, macaddr, ETH_ALEN);
1457 }
1458#endif
1459 if (!is_valid_ether_addr(ndev->dev_addr))
stigge@antcom.decdaf0b82012-03-28 12:36:26 +00001460 eth_hw_addr_random(ndev);
stigge@antcom.deb7370112012-03-08 11:49:17 +00001461
1462 /* Reset the ethernet controller */
1463 __lpc_eth_reset(pldat);
1464
1465 /* then shut everything down to save power */
1466 __lpc_eth_shutdown(pldat);
1467
1468 /* Set default parameters */
1469 pldat->msg_enable = NETIF_MSG_LINK;
1470
1471 /* Force an MII interface reset and clock setup */
1472 __lpc_mii_mngt_reset(pldat);
1473
1474 /* Force default PHY interface setup in chip, this will probably be
1475 changed by the PHY driver */
1476 pldat->link = 0;
1477 pldat->speed = 100;
1478 pldat->duplex = DUPLEX_FULL;
1479 __lpc_params_setup(pldat);
1480
1481 netif_napi_add(ndev, &pldat->napi, lpc_eth_poll, NAPI_WEIGHT);
1482
1483 ret = register_netdev(ndev);
1484 if (ret) {
1485 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1486 goto err_out_dma_unmap;
1487 }
1488 platform_set_drvdata(pdev, ndev);
1489
1490 if (lpc_mii_init(pldat) != 0)
1491 goto err_out_unregister_netdev;
1492
1493 netdev_info(ndev, "LPC mac at 0x%08x irq %d\n",
1494 res->start, ndev->irq);
1495
1496 phydev = pldat->phy_dev;
1497
1498 device_init_wakeup(&pdev->dev, 1);
1499 device_set_wakeup_enable(&pdev->dev, 0);
1500
1501 return 0;
1502
1503err_out_unregister_netdev:
1504 platform_set_drvdata(pdev, NULL);
1505 unregister_netdev(ndev);
1506err_out_dma_unmap:
Roland Stigge4de02e42012-04-22 12:01:19 +02001507 if (!use_iram_for_net(&pldat->pdev->dev) ||
stigge@antcom.deb7370112012-03-08 11:49:17 +00001508 pldat->dma_buff_size > lpc32xx_return_iram_size())
1509 dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
1510 pldat->dma_buff_base_v,
1511 pldat->dma_buff_base_p);
1512err_out_free_irq:
1513 free_irq(ndev->irq, ndev);
1514err_out_iounmap:
1515 iounmap(pldat->net_base);
1516err_out_disable_clocks:
1517 clk_disable(pldat->clk);
1518 clk_put(pldat->clk);
1519err_out_free_dev:
1520 free_netdev(ndev);
1521err_exit:
1522 pr_err("%s: not found (%d).\n", MODNAME, ret);
1523 return ret;
1524}
1525
1526static int lpc_eth_drv_remove(struct platform_device *pdev)
1527{
1528 struct net_device *ndev = platform_get_drvdata(pdev);
1529 struct netdata_local *pldat = netdev_priv(ndev);
1530
1531 unregister_netdev(ndev);
1532 platform_set_drvdata(pdev, NULL);
1533
Roland Stigge4de02e42012-04-22 12:01:19 +02001534 if (!use_iram_for_net(&pldat->pdev->dev) ||
stigge@antcom.deb7370112012-03-08 11:49:17 +00001535 pldat->dma_buff_size > lpc32xx_return_iram_size())
1536 dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
1537 pldat->dma_buff_base_v,
1538 pldat->dma_buff_base_p);
1539 free_irq(ndev->irq, ndev);
1540 iounmap(pldat->net_base);
1541 mdiobus_free(pldat->mii_bus);
1542 clk_disable(pldat->clk);
1543 clk_put(pldat->clk);
1544 free_netdev(ndev);
1545
1546 return 0;
1547}
1548
1549#ifdef CONFIG_PM
1550static int lpc_eth_drv_suspend(struct platform_device *pdev,
1551 pm_message_t state)
1552{
1553 struct net_device *ndev = platform_get_drvdata(pdev);
1554 struct netdata_local *pldat = netdev_priv(ndev);
1555
1556 if (device_may_wakeup(&pdev->dev))
1557 enable_irq_wake(ndev->irq);
1558
1559 if (ndev) {
1560 if (netif_running(ndev)) {
1561 netif_device_detach(ndev);
1562 __lpc_eth_shutdown(pldat);
1563 clk_disable(pldat->clk);
1564
1565 /*
1566 * Reset again now clock is disable to be sure
1567 * EMC_MDC is down
1568 */
1569 __lpc_eth_reset(pldat);
1570 }
1571 }
1572
1573 return 0;
1574}
1575
1576static int lpc_eth_drv_resume(struct platform_device *pdev)
1577{
1578 struct net_device *ndev = platform_get_drvdata(pdev);
1579 struct netdata_local *pldat;
1580
1581 if (device_may_wakeup(&pdev->dev))
1582 disable_irq_wake(ndev->irq);
1583
1584 if (ndev) {
1585 if (netif_running(ndev)) {
1586 pldat = netdev_priv(ndev);
1587
1588 /* Enable interface clock */
1589 clk_enable(pldat->clk);
1590
1591 /* Reset and initialize */
1592 __lpc_eth_reset(pldat);
1593 __lpc_eth_init(pldat);
1594
1595 netif_device_attach(ndev);
1596 }
1597 }
1598
1599 return 0;
1600}
1601#endif
1602
Roland Stigge4de02e42012-04-22 12:01:19 +02001603#ifdef CONFIG_OF
1604static const struct of_device_id lpc_eth_match[] = {
1605 { .compatible = "nxp,lpc-eth" },
1606 { }
1607};
1608MODULE_DEVICE_TABLE(of, lpc_eth_match);
1609#endif
1610
stigge@antcom.deb7370112012-03-08 11:49:17 +00001611static struct platform_driver lpc_eth_driver = {
1612 .probe = lpc_eth_drv_probe,
1613 .remove = __devexit_p(lpc_eth_drv_remove),
1614#ifdef CONFIG_PM
1615 .suspend = lpc_eth_drv_suspend,
1616 .resume = lpc_eth_drv_resume,
1617#endif
1618 .driver = {
1619 .name = MODNAME,
Roland Stigge4de02e42012-04-22 12:01:19 +02001620 .of_match_table = of_match_ptr(lpc_eth_match),
stigge@antcom.deb7370112012-03-08 11:49:17 +00001621 },
1622};
1623
1624module_platform_driver(lpc_eth_driver);
1625
1626MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
1627MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
1628MODULE_DESCRIPTION("LPC Ethernet Driver");
1629MODULE_LICENSE("GPL");