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