1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This is a new flat driver which is based on the original emac_lite
6*4882a593Smuzhiyun * driver from John Williams <john.williams@xilinx.com>.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * 2007 - 2013 (c) Xilinx, Inc.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/uaccess.h>
13*4882a593Smuzhiyun #include <linux/netdevice.h>
14*4882a593Smuzhiyun #include <linux/etherdevice.h>
15*4882a593Smuzhiyun #include <linux/skbuff.h>
16*4882a593Smuzhiyun #include <linux/ethtool.h>
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/of_address.h>
20*4882a593Smuzhiyun #include <linux/of_device.h>
21*4882a593Smuzhiyun #include <linux/of_platform.h>
22*4882a593Smuzhiyun #include <linux/of_mdio.h>
23*4882a593Smuzhiyun #include <linux/of_net.h>
24*4882a593Smuzhiyun #include <linux/phy.h>
25*4882a593Smuzhiyun #include <linux/interrupt.h>
26*4882a593Smuzhiyun #include <linux/iopoll.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define DRIVER_NAME "xilinx_emaclite"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* Register offsets for the EmacLite Core */
31*4882a593Smuzhiyun #define XEL_TXBUFF_OFFSET 0x0 /* Transmit Buffer */
32*4882a593Smuzhiyun #define XEL_MDIOADDR_OFFSET 0x07E4 /* MDIO Address Register */
33*4882a593Smuzhiyun #define XEL_MDIOWR_OFFSET 0x07E8 /* MDIO Write Data Register */
34*4882a593Smuzhiyun #define XEL_MDIORD_OFFSET 0x07EC /* MDIO Read Data Register */
35*4882a593Smuzhiyun #define XEL_MDIOCTRL_OFFSET 0x07F0 /* MDIO Control Register */
36*4882a593Smuzhiyun #define XEL_GIER_OFFSET 0x07F8 /* GIE Register */
37*4882a593Smuzhiyun #define XEL_TSR_OFFSET 0x07FC /* Tx status */
38*4882a593Smuzhiyun #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
41*4882a593Smuzhiyun #define XEL_RPLR_OFFSET 0x100C /* Rx packet length */
42*4882a593Smuzhiyun #define XEL_RSR_OFFSET 0x17FC /* Rx status */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* MDIO Address Register Bit Masks */
47*4882a593Smuzhiyun #define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
48*4882a593Smuzhiyun #define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
49*4882a593Smuzhiyun #define XEL_MDIOADDR_PHYADR_SHIFT 5
50*4882a593Smuzhiyun #define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* MDIO Write Data Register Bit Masks */
53*4882a593Smuzhiyun #define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* MDIO Read Data Register Bit Masks */
56*4882a593Smuzhiyun #define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* MDIO Control Register Bit Masks */
59*4882a593Smuzhiyun #define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
60*4882a593Smuzhiyun #define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Global Interrupt Enable Register (GIER) Bit Masks */
63*4882a593Smuzhiyun #define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* Transmit Status Register (TSR) Bit Masks */
66*4882a593Smuzhiyun #define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
67*4882a593Smuzhiyun #define XEL_TSR_PROGRAM_MASK 0x00000002 /* Program the MAC address */
68*4882a593Smuzhiyun #define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
69*4882a593Smuzhiyun #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
70*4882a593Smuzhiyun * only. This is not documented
71*4882a593Smuzhiyun * in the HW spec
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Define for programming the MAC address into the EmacLite */
75*4882a593Smuzhiyun #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Receive Status Register (RSR) */
78*4882a593Smuzhiyun #define XEL_RSR_RECV_DONE_MASK 0x00000001 /* Rx complete */
79*4882a593Smuzhiyun #define XEL_RSR_RECV_IE_MASK 0x00000008 /* Rx interrupt enable bit */
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* Transmit Packet Length Register (TPLR) */
82*4882a593Smuzhiyun #define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Receive Packet Length Register (RPLR) */
85*4882a593Smuzhiyun #define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #define XEL_HEADER_OFFSET 12 /* Offset to length field */
88*4882a593Smuzhiyun #define XEL_HEADER_SHIFT 16 /* Shift value for length */
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* General Ethernet Definitions */
91*4882a593Smuzhiyun #define XEL_ARP_PACKET_SIZE 28 /* Max ARP packet size */
92*4882a593Smuzhiyun #define XEL_HEADER_IP_LENGTH_OFFSET 16 /* IP Length Offset */
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #define TX_TIMEOUT (60 * HZ) /* Tx timeout is 60 seconds. */
97*4882a593Smuzhiyun #define ALIGNMENT 4
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
100*4882a593Smuzhiyun #define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32)adr)) % ALIGNMENT)
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #ifdef __BIG_ENDIAN
103*4882a593Smuzhiyun #define xemaclite_readl ioread32be
104*4882a593Smuzhiyun #define xemaclite_writel iowrite32be
105*4882a593Smuzhiyun #else
106*4882a593Smuzhiyun #define xemaclite_readl ioread32
107*4882a593Smuzhiyun #define xemaclite_writel iowrite32
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * struct net_local - Our private per device data
112*4882a593Smuzhiyun * @ndev: instance of the network device
113*4882a593Smuzhiyun * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
114*4882a593Smuzhiyun * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
115*4882a593Smuzhiyun * @next_tx_buf_to_use: next Tx buffer to write to
116*4882a593Smuzhiyun * @next_rx_buf_to_use: next Rx buffer to read from
117*4882a593Smuzhiyun * @base_addr: base address of the Emaclite device
118*4882a593Smuzhiyun * @reset_lock: lock used for synchronization
119*4882a593Smuzhiyun * @deferred_skb: holds an skb (for transmission at a later time) when the
120*4882a593Smuzhiyun * Tx buffer is not free
121*4882a593Smuzhiyun * @phy_dev: pointer to the PHY device
122*4882a593Smuzhiyun * @phy_node: pointer to the PHY device node
123*4882a593Smuzhiyun * @mii_bus: pointer to the MII bus
124*4882a593Smuzhiyun * @last_link: last link status
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun struct net_local {
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun struct net_device *ndev;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun bool tx_ping_pong;
131*4882a593Smuzhiyun bool rx_ping_pong;
132*4882a593Smuzhiyun u32 next_tx_buf_to_use;
133*4882a593Smuzhiyun u32 next_rx_buf_to_use;
134*4882a593Smuzhiyun void __iomem *base_addr;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun spinlock_t reset_lock;
137*4882a593Smuzhiyun struct sk_buff *deferred_skb;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun struct phy_device *phy_dev;
140*4882a593Smuzhiyun struct device_node *phy_node;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun struct mii_bus *mii_bus;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun int last_link;
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*************************/
149*4882a593Smuzhiyun /* EmacLite driver calls */
150*4882a593Smuzhiyun /*************************/
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
154*4882a593Smuzhiyun * @drvdata: Pointer to the Emaclite device private data
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * This function enables the Tx and Rx interrupts for the Emaclite device along
157*4882a593Smuzhiyun * with the Global Interrupt Enable.
158*4882a593Smuzhiyun */
xemaclite_enable_interrupts(struct net_local * drvdata)159*4882a593Smuzhiyun static void xemaclite_enable_interrupts(struct net_local *drvdata)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun u32 reg_data;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* Enable the Tx interrupts for the first Buffer */
164*4882a593Smuzhiyun reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
165*4882a593Smuzhiyun xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
166*4882a593Smuzhiyun drvdata->base_addr + XEL_TSR_OFFSET);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* Enable the Rx interrupts for the first buffer */
169*4882a593Smuzhiyun xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* Enable the Global Interrupt Enable */
172*4882a593Smuzhiyun xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /**
176*4882a593Smuzhiyun * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
177*4882a593Smuzhiyun * @drvdata: Pointer to the Emaclite device private data
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * This function disables the Tx and Rx interrupts for the Emaclite device,
180*4882a593Smuzhiyun * along with the Global Interrupt Enable.
181*4882a593Smuzhiyun */
xemaclite_disable_interrupts(struct net_local * drvdata)182*4882a593Smuzhiyun static void xemaclite_disable_interrupts(struct net_local *drvdata)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun u32 reg_data;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* Disable the Global Interrupt Enable */
187*4882a593Smuzhiyun xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* Disable the Tx interrupts for the first buffer */
190*4882a593Smuzhiyun reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
191*4882a593Smuzhiyun xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
192*4882a593Smuzhiyun drvdata->base_addr + XEL_TSR_OFFSET);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Disable the Rx interrupts for the first buffer */
195*4882a593Smuzhiyun reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
196*4882a593Smuzhiyun xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
197*4882a593Smuzhiyun drvdata->base_addr + XEL_RSR_OFFSET);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
202*4882a593Smuzhiyun * @src_ptr: Void pointer to the 16-bit aligned source address
203*4882a593Smuzhiyun * @dest_ptr: Pointer to the 32-bit aligned destination address
204*4882a593Smuzhiyun * @length: Number bytes to write from source to destination
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
207*4882a593Smuzhiyun * address in the EmacLite device.
208*4882a593Smuzhiyun */
xemaclite_aligned_write(void * src_ptr,u32 * dest_ptr,unsigned length)209*4882a593Smuzhiyun static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
210*4882a593Smuzhiyun unsigned length)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun u32 align_buffer;
213*4882a593Smuzhiyun u32 *to_u32_ptr;
214*4882a593Smuzhiyun u16 *from_u16_ptr, *to_u16_ptr;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun to_u32_ptr = dest_ptr;
217*4882a593Smuzhiyun from_u16_ptr = src_ptr;
218*4882a593Smuzhiyun align_buffer = 0;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun for (; length > 3; length -= 4) {
221*4882a593Smuzhiyun to_u16_ptr = (u16 *)&align_buffer;
222*4882a593Smuzhiyun *to_u16_ptr++ = *from_u16_ptr++;
223*4882a593Smuzhiyun *to_u16_ptr++ = *from_u16_ptr++;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* This barrier resolves occasional issues seen around
226*4882a593Smuzhiyun * cases where the data is not properly flushed out
227*4882a593Smuzhiyun * from the processor store buffers to the destination
228*4882a593Smuzhiyun * memory locations.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun wmb();
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* Output a word */
233*4882a593Smuzhiyun *to_u32_ptr++ = align_buffer;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun if (length) {
236*4882a593Smuzhiyun u8 *from_u8_ptr, *to_u8_ptr;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* Set up to output the remaining data */
239*4882a593Smuzhiyun align_buffer = 0;
240*4882a593Smuzhiyun to_u8_ptr = (u8 *)&align_buffer;
241*4882a593Smuzhiyun from_u8_ptr = (u8 *)from_u16_ptr;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* Output the remaining data */
244*4882a593Smuzhiyun for (; length > 0; length--)
245*4882a593Smuzhiyun *to_u8_ptr++ = *from_u8_ptr++;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* This barrier resolves occasional issues seen around
248*4882a593Smuzhiyun * cases where the data is not properly flushed out
249*4882a593Smuzhiyun * from the processor store buffers to the destination
250*4882a593Smuzhiyun * memory locations.
251*4882a593Smuzhiyun */
252*4882a593Smuzhiyun wmb();
253*4882a593Smuzhiyun *to_u32_ptr = align_buffer;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /**
258*4882a593Smuzhiyun * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
259*4882a593Smuzhiyun * @src_ptr: Pointer to the 32-bit aligned source address
260*4882a593Smuzhiyun * @dest_ptr: Pointer to the 16-bit aligned destination address
261*4882a593Smuzhiyun * @length: Number bytes to read from source to destination
262*4882a593Smuzhiyun *
263*4882a593Smuzhiyun * This function reads data from a 32-bit aligned address in the EmacLite device
264*4882a593Smuzhiyun * to a 16-bit aligned buffer.
265*4882a593Smuzhiyun */
xemaclite_aligned_read(u32 * src_ptr,u8 * dest_ptr,unsigned length)266*4882a593Smuzhiyun static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
267*4882a593Smuzhiyun unsigned length)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun u16 *to_u16_ptr, *from_u16_ptr;
270*4882a593Smuzhiyun u32 *from_u32_ptr;
271*4882a593Smuzhiyun u32 align_buffer;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun from_u32_ptr = src_ptr;
274*4882a593Smuzhiyun to_u16_ptr = (u16 *)dest_ptr;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun for (; length > 3; length -= 4) {
277*4882a593Smuzhiyun /* Copy each word into the temporary buffer */
278*4882a593Smuzhiyun align_buffer = *from_u32_ptr++;
279*4882a593Smuzhiyun from_u16_ptr = (u16 *)&align_buffer;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Read data from source */
282*4882a593Smuzhiyun *to_u16_ptr++ = *from_u16_ptr++;
283*4882a593Smuzhiyun *to_u16_ptr++ = *from_u16_ptr++;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (length) {
287*4882a593Smuzhiyun u8 *to_u8_ptr, *from_u8_ptr;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* Set up to read the remaining data */
290*4882a593Smuzhiyun to_u8_ptr = (u8 *)to_u16_ptr;
291*4882a593Smuzhiyun align_buffer = *from_u32_ptr++;
292*4882a593Smuzhiyun from_u8_ptr = (u8 *)&align_buffer;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /* Read the remaining data */
295*4882a593Smuzhiyun for (; length > 0; length--)
296*4882a593Smuzhiyun *to_u8_ptr = *from_u8_ptr;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun * xemaclite_send_data - Send an Ethernet frame
302*4882a593Smuzhiyun * @drvdata: Pointer to the Emaclite device private data
303*4882a593Smuzhiyun * @data: Pointer to the data to be sent
304*4882a593Smuzhiyun * @byte_count: Total frame size, including header
305*4882a593Smuzhiyun *
306*4882a593Smuzhiyun * This function checks if the Tx buffer of the Emaclite device is free to send
307*4882a593Smuzhiyun * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
308*4882a593Smuzhiyun * returns an error.
309*4882a593Smuzhiyun *
310*4882a593Smuzhiyun * Return: 0 upon success or -1 if the buffer(s) are full.
311*4882a593Smuzhiyun *
312*4882a593Smuzhiyun * Note: The maximum Tx packet size can not be more than Ethernet header
313*4882a593Smuzhiyun * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
314*4882a593Smuzhiyun */
xemaclite_send_data(struct net_local * drvdata,u8 * data,unsigned int byte_count)315*4882a593Smuzhiyun static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
316*4882a593Smuzhiyun unsigned int byte_count)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun u32 reg_data;
319*4882a593Smuzhiyun void __iomem *addr;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /* Determine the expected Tx buffer address */
322*4882a593Smuzhiyun addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /* If the length is too large, truncate it */
325*4882a593Smuzhiyun if (byte_count > ETH_FRAME_LEN)
326*4882a593Smuzhiyun byte_count = ETH_FRAME_LEN;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /* Check if the expected buffer is available */
329*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
330*4882a593Smuzhiyun if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
331*4882a593Smuzhiyun XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* Switch to next buffer if configured */
334*4882a593Smuzhiyun if (drvdata->tx_ping_pong != 0)
335*4882a593Smuzhiyun drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
336*4882a593Smuzhiyun } else if (drvdata->tx_ping_pong != 0) {
337*4882a593Smuzhiyun /* If the expected buffer is full, try the other buffer,
338*4882a593Smuzhiyun * if it is configured in HW
339*4882a593Smuzhiyun */
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun addr = (void __iomem __force *)((u32 __force)addr ^
342*4882a593Smuzhiyun XEL_BUFFER_OFFSET);
343*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
346*4882a593Smuzhiyun XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
347*4882a593Smuzhiyun return -1; /* Buffers were full, return failure */
348*4882a593Smuzhiyun } else
349*4882a593Smuzhiyun return -1; /* Buffer was full, return failure */
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Write the frame to the buffer */
352*4882a593Smuzhiyun xemaclite_aligned_write(data, (u32 __force *)addr, byte_count);
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
355*4882a593Smuzhiyun addr + XEL_TPLR_OFFSET);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* Update the Tx Status Register to indicate that there is a
358*4882a593Smuzhiyun * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
359*4882a593Smuzhiyun * is used by the interrupt handler to check whether a frame
360*4882a593Smuzhiyun * has been transmitted
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
363*4882a593Smuzhiyun reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
364*4882a593Smuzhiyun xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun return 0;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun * xemaclite_recv_data - Receive a frame
371*4882a593Smuzhiyun * @drvdata: Pointer to the Emaclite device private data
372*4882a593Smuzhiyun * @data: Address where the data is to be received
373*4882a593Smuzhiyun * @maxlen: Maximum supported ethernet packet length
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * This function is intended to be called from the interrupt context or
376*4882a593Smuzhiyun * with a wrapper which waits for the receive frame to be available.
377*4882a593Smuzhiyun *
378*4882a593Smuzhiyun * Return: Total number of bytes received
379*4882a593Smuzhiyun */
xemaclite_recv_data(struct net_local * drvdata,u8 * data,int maxlen)380*4882a593Smuzhiyun static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun void __iomem *addr;
383*4882a593Smuzhiyun u16 length, proto_type;
384*4882a593Smuzhiyun u32 reg_data;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Determine the expected buffer address */
387*4882a593Smuzhiyun addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /* Verify which buffer has valid data */
390*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
393*4882a593Smuzhiyun if (drvdata->rx_ping_pong != 0)
394*4882a593Smuzhiyun drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
395*4882a593Smuzhiyun } else {
396*4882a593Smuzhiyun /* The instance is out of sync, try other buffer if other
397*4882a593Smuzhiyun * buffer is configured, return 0 otherwise. If the instance is
398*4882a593Smuzhiyun * out of sync, do not update the 'next_rx_buf_to_use' since it
399*4882a593Smuzhiyun * will correct on subsequent calls
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun if (drvdata->rx_ping_pong != 0)
402*4882a593Smuzhiyun addr = (void __iomem __force *)((u32 __force)addr ^
403*4882a593Smuzhiyun XEL_BUFFER_OFFSET);
404*4882a593Smuzhiyun else
405*4882a593Smuzhiyun return 0; /* No data was available */
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun /* Verify that buffer has valid data */
408*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
409*4882a593Smuzhiyun if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
410*4882a593Smuzhiyun XEL_RSR_RECV_DONE_MASK)
411*4882a593Smuzhiyun return 0; /* No data was available */
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /* Get the protocol type of the ethernet frame that arrived
415*4882a593Smuzhiyun */
416*4882a593Smuzhiyun proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
417*4882a593Smuzhiyun XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
418*4882a593Smuzhiyun XEL_RPLR_LENGTH_MASK);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /* Check if received ethernet frame is a raw ethernet frame
421*4882a593Smuzhiyun * or an IP packet or an ARP packet
422*4882a593Smuzhiyun */
423*4882a593Smuzhiyun if (proto_type > ETH_DATA_LEN) {
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (proto_type == ETH_P_IP) {
426*4882a593Smuzhiyun length = ((ntohl(xemaclite_readl(addr +
427*4882a593Smuzhiyun XEL_HEADER_IP_LENGTH_OFFSET +
428*4882a593Smuzhiyun XEL_RXBUFF_OFFSET)) >>
429*4882a593Smuzhiyun XEL_HEADER_SHIFT) &
430*4882a593Smuzhiyun XEL_RPLR_LENGTH_MASK);
431*4882a593Smuzhiyun length = min_t(u16, length, ETH_DATA_LEN);
432*4882a593Smuzhiyun length += ETH_HLEN + ETH_FCS_LEN;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun } else if (proto_type == ETH_P_ARP)
435*4882a593Smuzhiyun length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
436*4882a593Smuzhiyun else
437*4882a593Smuzhiyun /* Field contains type other than IP or ARP, use max
438*4882a593Smuzhiyun * frame size and let user parse it
439*4882a593Smuzhiyun */
440*4882a593Smuzhiyun length = ETH_FRAME_LEN + ETH_FCS_LEN;
441*4882a593Smuzhiyun } else
442*4882a593Smuzhiyun /* Use the length in the frame, plus the header and trailer */
443*4882a593Smuzhiyun length = proto_type + ETH_HLEN + ETH_FCS_LEN;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun if (WARN_ON(length > maxlen))
446*4882a593Smuzhiyun length = maxlen;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /* Read from the EmacLite device */
449*4882a593Smuzhiyun xemaclite_aligned_read((u32 __force *)(addr + XEL_RXBUFF_OFFSET),
450*4882a593Smuzhiyun data, length);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /* Acknowledge the frame */
453*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
454*4882a593Smuzhiyun reg_data &= ~XEL_RSR_RECV_DONE_MASK;
455*4882a593Smuzhiyun xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun return length;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /**
461*4882a593Smuzhiyun * xemaclite_update_address - Update the MAC address in the device
462*4882a593Smuzhiyun * @drvdata: Pointer to the Emaclite device private data
463*4882a593Smuzhiyun * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
464*4882a593Smuzhiyun *
465*4882a593Smuzhiyun * Tx must be idle and Rx should be idle for deterministic results.
466*4882a593Smuzhiyun * It is recommended that this function should be called after the
467*4882a593Smuzhiyun * initialization and before transmission of any packets from the device.
468*4882a593Smuzhiyun * The MAC address can be programmed using any of the two transmit
469*4882a593Smuzhiyun * buffers (if configured).
470*4882a593Smuzhiyun */
xemaclite_update_address(struct net_local * drvdata,u8 * address_ptr)471*4882a593Smuzhiyun static void xemaclite_update_address(struct net_local *drvdata,
472*4882a593Smuzhiyun u8 *address_ptr)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun void __iomem *addr;
475*4882a593Smuzhiyun u32 reg_data;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* Determine the expected Tx buffer address */
478*4882a593Smuzhiyun addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun xemaclite_aligned_write(address_ptr, (u32 __force *)addr, ETH_ALEN);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /* Update the MAC address in the EmacLite */
485*4882a593Smuzhiyun reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
486*4882a593Smuzhiyun xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /* Wait for EmacLite to finish with the MAC address update */
489*4882a593Smuzhiyun while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
490*4882a593Smuzhiyun XEL_TSR_PROG_MAC_ADDR) != 0)
491*4882a593Smuzhiyun ;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /**
495*4882a593Smuzhiyun * xemaclite_set_mac_address - Set the MAC address for this device
496*4882a593Smuzhiyun * @dev: Pointer to the network device instance
497*4882a593Smuzhiyun * @address: Void pointer to the sockaddr structure
498*4882a593Smuzhiyun *
499*4882a593Smuzhiyun * This function copies the HW address from the sockaddr strucutre to the
500*4882a593Smuzhiyun * net_device structure and updates the address in HW.
501*4882a593Smuzhiyun *
502*4882a593Smuzhiyun * Return: Error if the net device is busy or 0 if the addr is set
503*4882a593Smuzhiyun * successfully
504*4882a593Smuzhiyun */
xemaclite_set_mac_address(struct net_device * dev,void * address)505*4882a593Smuzhiyun static int xemaclite_set_mac_address(struct net_device *dev, void *address)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
508*4882a593Smuzhiyun struct sockaddr *addr = address;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun if (netif_running(dev))
511*4882a593Smuzhiyun return -EBUSY;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
514*4882a593Smuzhiyun xemaclite_update_address(lp, dev->dev_addr);
515*4882a593Smuzhiyun return 0;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /**
519*4882a593Smuzhiyun * xemaclite_tx_timeout - Callback for Tx Timeout
520*4882a593Smuzhiyun * @dev: Pointer to the network device
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * This function is called when Tx time out occurs for Emaclite device.
523*4882a593Smuzhiyun */
xemaclite_tx_timeout(struct net_device * dev,unsigned int txqueue)524*4882a593Smuzhiyun static void xemaclite_tx_timeout(struct net_device *dev, unsigned int txqueue)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
527*4882a593Smuzhiyun unsigned long flags;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
530*4882a593Smuzhiyun TX_TIMEOUT * 1000UL / HZ);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun dev->stats.tx_errors++;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /* Reset the device */
535*4882a593Smuzhiyun spin_lock_irqsave(&lp->reset_lock, flags);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /* Shouldn't really be necessary, but shouldn't hurt */
538*4882a593Smuzhiyun netif_stop_queue(dev);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun xemaclite_disable_interrupts(lp);
541*4882a593Smuzhiyun xemaclite_enable_interrupts(lp);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if (lp->deferred_skb) {
544*4882a593Smuzhiyun dev_kfree_skb(lp->deferred_skb);
545*4882a593Smuzhiyun lp->deferred_skb = NULL;
546*4882a593Smuzhiyun dev->stats.tx_errors++;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /* To exclude tx timeout */
550*4882a593Smuzhiyun netif_trans_update(dev); /* prevent tx timeout */
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /* We're all ready to go. Start the queue */
553*4882a593Smuzhiyun netif_wake_queue(dev);
554*4882a593Smuzhiyun spin_unlock_irqrestore(&lp->reset_lock, flags);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /**********************/
558*4882a593Smuzhiyun /* Interrupt Handlers */
559*4882a593Smuzhiyun /**********************/
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun * xemaclite_tx_handler - Interrupt handler for frames sent
563*4882a593Smuzhiyun * @dev: Pointer to the network device
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * This function updates the number of packets transmitted and handles the
566*4882a593Smuzhiyun * deferred skb, if there is one.
567*4882a593Smuzhiyun */
xemaclite_tx_handler(struct net_device * dev)568*4882a593Smuzhiyun static void xemaclite_tx_handler(struct net_device *dev)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun dev->stats.tx_packets++;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun if (!lp->deferred_skb)
575*4882a593Smuzhiyun return;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun if (xemaclite_send_data(lp, (u8 *)lp->deferred_skb->data,
578*4882a593Smuzhiyun lp->deferred_skb->len))
579*4882a593Smuzhiyun return;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun dev->stats.tx_bytes += lp->deferred_skb->len;
582*4882a593Smuzhiyun dev_consume_skb_irq(lp->deferred_skb);
583*4882a593Smuzhiyun lp->deferred_skb = NULL;
584*4882a593Smuzhiyun netif_trans_update(dev); /* prevent tx timeout */
585*4882a593Smuzhiyun netif_wake_queue(dev);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /**
589*4882a593Smuzhiyun * xemaclite_rx_handler- Interrupt handler for frames received
590*4882a593Smuzhiyun * @dev: Pointer to the network device
591*4882a593Smuzhiyun *
592*4882a593Smuzhiyun * This function allocates memory for a socket buffer, fills it with data
593*4882a593Smuzhiyun * received and hands it over to the TCP/IP stack.
594*4882a593Smuzhiyun */
xemaclite_rx_handler(struct net_device * dev)595*4882a593Smuzhiyun static void xemaclite_rx_handler(struct net_device *dev)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
598*4882a593Smuzhiyun struct sk_buff *skb;
599*4882a593Smuzhiyun unsigned int align;
600*4882a593Smuzhiyun u32 len;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun len = ETH_FRAME_LEN + ETH_FCS_LEN;
603*4882a593Smuzhiyun skb = netdev_alloc_skb(dev, len + ALIGNMENT);
604*4882a593Smuzhiyun if (!skb) {
605*4882a593Smuzhiyun /* Couldn't get memory. */
606*4882a593Smuzhiyun dev->stats.rx_dropped++;
607*4882a593Smuzhiyun dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
608*4882a593Smuzhiyun return;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /* A new skb should have the data halfword aligned, but this code is
612*4882a593Smuzhiyun * here just in case that isn't true. Calculate how many
613*4882a593Smuzhiyun * bytes we should reserve to get the data to start on a word
614*4882a593Smuzhiyun * boundary
615*4882a593Smuzhiyun */
616*4882a593Smuzhiyun align = BUFFER_ALIGN(skb->data);
617*4882a593Smuzhiyun if (align)
618*4882a593Smuzhiyun skb_reserve(skb, align);
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun skb_reserve(skb, 2);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun len = xemaclite_recv_data(lp, (u8 *)skb->data, len);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun if (!len) {
625*4882a593Smuzhiyun dev->stats.rx_errors++;
626*4882a593Smuzhiyun dev_kfree_skb_irq(skb);
627*4882a593Smuzhiyun return;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun skb_put(skb, len); /* Tell the skb how much data we got */
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun skb->protocol = eth_type_trans(skb, dev);
633*4882a593Smuzhiyun skb_checksum_none_assert(skb);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun dev->stats.rx_packets++;
636*4882a593Smuzhiyun dev->stats.rx_bytes += len;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun if (!skb_defer_rx_timestamp(skb))
639*4882a593Smuzhiyun netif_rx(skb); /* Send the packet upstream */
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun * xemaclite_interrupt - Interrupt handler for this driver
644*4882a593Smuzhiyun * @irq: Irq of the Emaclite device
645*4882a593Smuzhiyun * @dev_id: Void pointer to the network device instance used as callback
646*4882a593Smuzhiyun * reference
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * Return: IRQ_HANDLED
649*4882a593Smuzhiyun *
650*4882a593Smuzhiyun * This function handles the Tx and Rx interrupts of the EmacLite device.
651*4882a593Smuzhiyun */
xemaclite_interrupt(int irq,void * dev_id)652*4882a593Smuzhiyun static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun bool tx_complete = false;
655*4882a593Smuzhiyun struct net_device *dev = dev_id;
656*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
657*4882a593Smuzhiyun void __iomem *base_addr = lp->base_addr;
658*4882a593Smuzhiyun u32 tx_status;
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun /* Check if there is Rx Data available */
661*4882a593Smuzhiyun if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
662*4882a593Smuzhiyun XEL_RSR_RECV_DONE_MASK) ||
663*4882a593Smuzhiyun (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
664*4882a593Smuzhiyun & XEL_RSR_RECV_DONE_MASK))
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun xemaclite_rx_handler(dev);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun /* Check if the Transmission for the first buffer is completed */
669*4882a593Smuzhiyun tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
670*4882a593Smuzhiyun if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
671*4882a593Smuzhiyun (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
674*4882a593Smuzhiyun xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun tx_complete = true;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /* Check if the Transmission for the second buffer is completed */
680*4882a593Smuzhiyun tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
681*4882a593Smuzhiyun if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
682*4882a593Smuzhiyun (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
685*4882a593Smuzhiyun xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
686*4882a593Smuzhiyun XEL_TSR_OFFSET);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun tx_complete = true;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun /* If there was a Tx interrupt, call the Tx Handler */
692*4882a593Smuzhiyun if (tx_complete != 0)
693*4882a593Smuzhiyun xemaclite_tx_handler(dev);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun return IRQ_HANDLED;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /**********************/
699*4882a593Smuzhiyun /* MDIO Bus functions */
700*4882a593Smuzhiyun /**********************/
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /**
703*4882a593Smuzhiyun * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
704*4882a593Smuzhiyun * @lp: Pointer to the Emaclite device private data
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * This function waits till the device is ready to accept a new MDIO
707*4882a593Smuzhiyun * request.
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * Return: 0 for success or ETIMEDOUT for a timeout
710*4882a593Smuzhiyun */
711*4882a593Smuzhiyun
xemaclite_mdio_wait(struct net_local * lp)712*4882a593Smuzhiyun static int xemaclite_mdio_wait(struct net_local *lp)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun u32 val;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun /* wait for the MDIO interface to not be busy or timeout
717*4882a593Smuzhiyun * after some time.
718*4882a593Smuzhiyun */
719*4882a593Smuzhiyun return readx_poll_timeout(xemaclite_readl,
720*4882a593Smuzhiyun lp->base_addr + XEL_MDIOCTRL_OFFSET,
721*4882a593Smuzhiyun val, !(val & XEL_MDIOCTRL_MDIOSTS_MASK),
722*4882a593Smuzhiyun 1000, 20000);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun * xemaclite_mdio_read - Read from a given MII management register
727*4882a593Smuzhiyun * @bus: the mii_bus struct
728*4882a593Smuzhiyun * @phy_id: the phy address
729*4882a593Smuzhiyun * @reg: register number to read from
730*4882a593Smuzhiyun *
731*4882a593Smuzhiyun * This function waits till the device is ready to accept a new MDIO
732*4882a593Smuzhiyun * request and then writes the phy address to the MDIO Address register
733*4882a593Smuzhiyun * and reads data from MDIO Read Data register, when its available.
734*4882a593Smuzhiyun *
735*4882a593Smuzhiyun * Return: Value read from the MII management register
736*4882a593Smuzhiyun */
xemaclite_mdio_read(struct mii_bus * bus,int phy_id,int reg)737*4882a593Smuzhiyun static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun struct net_local *lp = bus->priv;
740*4882a593Smuzhiyun u32 ctrl_reg;
741*4882a593Smuzhiyun u32 rc;
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun if (xemaclite_mdio_wait(lp))
744*4882a593Smuzhiyun return -ETIMEDOUT;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /* Write the PHY address, register number and set the OP bit in the
747*4882a593Smuzhiyun * MDIO Address register. Set the Status bit in the MDIO Control
748*4882a593Smuzhiyun * register to start a MDIO read transaction.
749*4882a593Smuzhiyun */
750*4882a593Smuzhiyun ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
751*4882a593Smuzhiyun xemaclite_writel(XEL_MDIOADDR_OP_MASK |
752*4882a593Smuzhiyun ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
753*4882a593Smuzhiyun lp->base_addr + XEL_MDIOADDR_OFFSET);
754*4882a593Smuzhiyun xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
755*4882a593Smuzhiyun lp->base_addr + XEL_MDIOCTRL_OFFSET);
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun if (xemaclite_mdio_wait(lp))
758*4882a593Smuzhiyun return -ETIMEDOUT;
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun dev_dbg(&lp->ndev->dev,
763*4882a593Smuzhiyun "%s(phy_id=%i, reg=%x) == %x\n", __func__,
764*4882a593Smuzhiyun phy_id, reg, rc);
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun return rc;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /**
770*4882a593Smuzhiyun * xemaclite_mdio_write - Write to a given MII management register
771*4882a593Smuzhiyun * @bus: the mii_bus struct
772*4882a593Smuzhiyun * @phy_id: the phy address
773*4882a593Smuzhiyun * @reg: register number to write to
774*4882a593Smuzhiyun * @val: value to write to the register number specified by reg
775*4882a593Smuzhiyun *
776*4882a593Smuzhiyun * This function waits till the device is ready to accept a new MDIO
777*4882a593Smuzhiyun * request and then writes the val to the MDIO Write Data register.
778*4882a593Smuzhiyun *
779*4882a593Smuzhiyun * Return: 0 upon success or a negative error upon failure
780*4882a593Smuzhiyun */
xemaclite_mdio_write(struct mii_bus * bus,int phy_id,int reg,u16 val)781*4882a593Smuzhiyun static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
782*4882a593Smuzhiyun u16 val)
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun struct net_local *lp = bus->priv;
785*4882a593Smuzhiyun u32 ctrl_reg;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun dev_dbg(&lp->ndev->dev,
788*4882a593Smuzhiyun "%s(phy_id=%i, reg=%x, val=%x)\n", __func__,
789*4882a593Smuzhiyun phy_id, reg, val);
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (xemaclite_mdio_wait(lp))
792*4882a593Smuzhiyun return -ETIMEDOUT;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* Write the PHY address, register number and clear the OP bit in the
795*4882a593Smuzhiyun * MDIO Address register and then write the value into the MDIO Write
796*4882a593Smuzhiyun * Data register. Finally, set the Status bit in the MDIO Control
797*4882a593Smuzhiyun * register to start a MDIO write transaction.
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
800*4882a593Smuzhiyun xemaclite_writel(~XEL_MDIOADDR_OP_MASK &
801*4882a593Smuzhiyun ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
802*4882a593Smuzhiyun lp->base_addr + XEL_MDIOADDR_OFFSET);
803*4882a593Smuzhiyun xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
804*4882a593Smuzhiyun xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
805*4882a593Smuzhiyun lp->base_addr + XEL_MDIOCTRL_OFFSET);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun return 0;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun /**
811*4882a593Smuzhiyun * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
812*4882a593Smuzhiyun * @lp: Pointer to the Emaclite device private data
813*4882a593Smuzhiyun * @dev: Pointer to OF device structure
814*4882a593Smuzhiyun *
815*4882a593Smuzhiyun * This function enables MDIO bus in the Emaclite device and registers a
816*4882a593Smuzhiyun * mii_bus.
817*4882a593Smuzhiyun *
818*4882a593Smuzhiyun * Return: 0 upon success or a negative error upon failure
819*4882a593Smuzhiyun */
xemaclite_mdio_setup(struct net_local * lp,struct device * dev)820*4882a593Smuzhiyun static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun struct mii_bus *bus;
823*4882a593Smuzhiyun struct resource res;
824*4882a593Smuzhiyun struct device_node *np = of_get_parent(lp->phy_node);
825*4882a593Smuzhiyun struct device_node *npp;
826*4882a593Smuzhiyun int rc, ret;
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun /* Don't register the MDIO bus if the phy_node or its parent node
829*4882a593Smuzhiyun * can't be found.
830*4882a593Smuzhiyun */
831*4882a593Smuzhiyun if (!np) {
832*4882a593Smuzhiyun dev_err(dev, "Failed to register mdio bus.\n");
833*4882a593Smuzhiyun return -ENODEV;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun npp = of_get_parent(np);
836*4882a593Smuzhiyun ret = of_address_to_resource(npp, 0, &res);
837*4882a593Smuzhiyun of_node_put(npp);
838*4882a593Smuzhiyun if (ret) {
839*4882a593Smuzhiyun dev_err(dev, "%s resource error!\n",
840*4882a593Smuzhiyun dev->of_node->full_name);
841*4882a593Smuzhiyun of_node_put(np);
842*4882a593Smuzhiyun return ret;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun if (lp->ndev->mem_start != res.start) {
845*4882a593Smuzhiyun struct phy_device *phydev;
846*4882a593Smuzhiyun phydev = of_phy_find_device(lp->phy_node);
847*4882a593Smuzhiyun if (!phydev)
848*4882a593Smuzhiyun dev_info(dev,
849*4882a593Smuzhiyun "MDIO of the phy is not registered yet\n");
850*4882a593Smuzhiyun else
851*4882a593Smuzhiyun put_device(&phydev->mdio.dev);
852*4882a593Smuzhiyun of_node_put(np);
853*4882a593Smuzhiyun return 0;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /* Enable the MDIO bus by asserting the enable bit in MDIO Control
857*4882a593Smuzhiyun * register.
858*4882a593Smuzhiyun */
859*4882a593Smuzhiyun xemaclite_writel(XEL_MDIOCTRL_MDIOEN_MASK,
860*4882a593Smuzhiyun lp->base_addr + XEL_MDIOCTRL_OFFSET);
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun bus = mdiobus_alloc();
863*4882a593Smuzhiyun if (!bus) {
864*4882a593Smuzhiyun dev_err(dev, "Failed to allocate mdiobus\n");
865*4882a593Smuzhiyun of_node_put(np);
866*4882a593Smuzhiyun return -ENOMEM;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
870*4882a593Smuzhiyun (unsigned long long)res.start);
871*4882a593Smuzhiyun bus->priv = lp;
872*4882a593Smuzhiyun bus->name = "Xilinx Emaclite MDIO";
873*4882a593Smuzhiyun bus->read = xemaclite_mdio_read;
874*4882a593Smuzhiyun bus->write = xemaclite_mdio_write;
875*4882a593Smuzhiyun bus->parent = dev;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun rc = of_mdiobus_register(bus, np);
878*4882a593Smuzhiyun of_node_put(np);
879*4882a593Smuzhiyun if (rc) {
880*4882a593Smuzhiyun dev_err(dev, "Failed to register mdio bus.\n");
881*4882a593Smuzhiyun goto err_register;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun lp->mii_bus = bus;
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun return 0;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun err_register:
889*4882a593Smuzhiyun mdiobus_free(bus);
890*4882a593Smuzhiyun return rc;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun /**
894*4882a593Smuzhiyun * xemaclite_adjust_link - Link state callback for the Emaclite device
895*4882a593Smuzhiyun * @ndev: pointer to net_device struct
896*4882a593Smuzhiyun *
897*4882a593Smuzhiyun * There's nothing in the Emaclite device to be configured when the link
898*4882a593Smuzhiyun * state changes. We just print the status.
899*4882a593Smuzhiyun */
xemaclite_adjust_link(struct net_device * ndev)900*4882a593Smuzhiyun static void xemaclite_adjust_link(struct net_device *ndev)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun struct net_local *lp = netdev_priv(ndev);
903*4882a593Smuzhiyun struct phy_device *phy = lp->phy_dev;
904*4882a593Smuzhiyun int link_state;
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /* hash together the state values to decide if something has changed */
907*4882a593Smuzhiyun link_state = phy->speed | (phy->duplex << 1) | phy->link;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun if (lp->last_link != link_state) {
910*4882a593Smuzhiyun lp->last_link = link_state;
911*4882a593Smuzhiyun phy_print_status(phy);
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /**
916*4882a593Smuzhiyun * xemaclite_open - Open the network device
917*4882a593Smuzhiyun * @dev: Pointer to the network device
918*4882a593Smuzhiyun *
919*4882a593Smuzhiyun * This function sets the MAC address, requests an IRQ and enables interrupts
920*4882a593Smuzhiyun * for the Emaclite device and starts the Tx queue.
921*4882a593Smuzhiyun * It also connects to the phy device, if MDIO is included in Emaclite device.
922*4882a593Smuzhiyun *
923*4882a593Smuzhiyun * Return: 0 on success. -ENODEV, if PHY cannot be connected.
924*4882a593Smuzhiyun * Non-zero error value on failure.
925*4882a593Smuzhiyun */
xemaclite_open(struct net_device * dev)926*4882a593Smuzhiyun static int xemaclite_open(struct net_device *dev)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
929*4882a593Smuzhiyun int retval;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun /* Just to be safe, stop the device first */
932*4882a593Smuzhiyun xemaclite_disable_interrupts(lp);
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun if (lp->phy_node) {
935*4882a593Smuzhiyun lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
936*4882a593Smuzhiyun xemaclite_adjust_link, 0,
937*4882a593Smuzhiyun PHY_INTERFACE_MODE_MII);
938*4882a593Smuzhiyun if (!lp->phy_dev) {
939*4882a593Smuzhiyun dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
940*4882a593Smuzhiyun return -ENODEV;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /* EmacLite doesn't support giga-bit speeds */
944*4882a593Smuzhiyun phy_set_max_speed(lp->phy_dev, SPEED_100);
945*4882a593Smuzhiyun phy_start(lp->phy_dev);
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun /* Set the MAC address each time opened */
949*4882a593Smuzhiyun xemaclite_update_address(lp, dev->dev_addr);
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /* Grab the IRQ */
952*4882a593Smuzhiyun retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
953*4882a593Smuzhiyun if (retval) {
954*4882a593Smuzhiyun dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
955*4882a593Smuzhiyun dev->irq);
956*4882a593Smuzhiyun if (lp->phy_dev)
957*4882a593Smuzhiyun phy_disconnect(lp->phy_dev);
958*4882a593Smuzhiyun lp->phy_dev = NULL;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun return retval;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun /* Enable Interrupts */
964*4882a593Smuzhiyun xemaclite_enable_interrupts(lp);
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun /* We're ready to go */
967*4882a593Smuzhiyun netif_start_queue(dev);
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun return 0;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun /**
973*4882a593Smuzhiyun * xemaclite_close - Close the network device
974*4882a593Smuzhiyun * @dev: Pointer to the network device
975*4882a593Smuzhiyun *
976*4882a593Smuzhiyun * This function stops the Tx queue, disables interrupts and frees the IRQ for
977*4882a593Smuzhiyun * the Emaclite device.
978*4882a593Smuzhiyun * It also disconnects the phy device associated with the Emaclite device.
979*4882a593Smuzhiyun *
980*4882a593Smuzhiyun * Return: 0, always.
981*4882a593Smuzhiyun */
xemaclite_close(struct net_device * dev)982*4882a593Smuzhiyun static int xemaclite_close(struct net_device *dev)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun netif_stop_queue(dev);
987*4882a593Smuzhiyun xemaclite_disable_interrupts(lp);
988*4882a593Smuzhiyun free_irq(dev->irq, dev);
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (lp->phy_dev)
991*4882a593Smuzhiyun phy_disconnect(lp->phy_dev);
992*4882a593Smuzhiyun lp->phy_dev = NULL;
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun return 0;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun /**
998*4882a593Smuzhiyun * xemaclite_send - Transmit a frame
999*4882a593Smuzhiyun * @orig_skb: Pointer to the socket buffer to be transmitted
1000*4882a593Smuzhiyun * @dev: Pointer to the network device
1001*4882a593Smuzhiyun *
1002*4882a593Smuzhiyun * This function checks if the Tx buffer of the Emaclite device is free to send
1003*4882a593Smuzhiyun * data. If so, it fills the Tx buffer with data from socket buffer data,
1004*4882a593Smuzhiyun * updates the stats and frees the socket buffer. The Tx completion is signaled
1005*4882a593Smuzhiyun * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
1006*4882a593Smuzhiyun * deferred and the Tx queue is stopped so that the deferred socket buffer can
1007*4882a593Smuzhiyun * be transmitted when the Emaclite device is free to transmit data.
1008*4882a593Smuzhiyun *
1009*4882a593Smuzhiyun * Return: NETDEV_TX_OK, always.
1010*4882a593Smuzhiyun */
1011*4882a593Smuzhiyun static netdev_tx_t
xemaclite_send(struct sk_buff * orig_skb,struct net_device * dev)1012*4882a593Smuzhiyun xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun struct net_local *lp = netdev_priv(dev);
1015*4882a593Smuzhiyun struct sk_buff *new_skb;
1016*4882a593Smuzhiyun unsigned int len;
1017*4882a593Smuzhiyun unsigned long flags;
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun len = orig_skb->len;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun new_skb = orig_skb;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun spin_lock_irqsave(&lp->reset_lock, flags);
1024*4882a593Smuzhiyun if (xemaclite_send_data(lp, (u8 *)new_skb->data, len) != 0) {
1025*4882a593Smuzhiyun /* If the Emaclite Tx buffer is busy, stop the Tx queue and
1026*4882a593Smuzhiyun * defer the skb for transmission during the ISR, after the
1027*4882a593Smuzhiyun * current transmission is complete
1028*4882a593Smuzhiyun */
1029*4882a593Smuzhiyun netif_stop_queue(dev);
1030*4882a593Smuzhiyun lp->deferred_skb = new_skb;
1031*4882a593Smuzhiyun /* Take the time stamp now, since we can't do this in an ISR. */
1032*4882a593Smuzhiyun skb_tx_timestamp(new_skb);
1033*4882a593Smuzhiyun spin_unlock_irqrestore(&lp->reset_lock, flags);
1034*4882a593Smuzhiyun return NETDEV_TX_OK;
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun spin_unlock_irqrestore(&lp->reset_lock, flags);
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun skb_tx_timestamp(new_skb);
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun dev->stats.tx_bytes += len;
1041*4882a593Smuzhiyun dev_consume_skb_any(new_skb);
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun return NETDEV_TX_OK;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun /**
1047*4882a593Smuzhiyun * get_bool - Get a parameter from the OF device
1048*4882a593Smuzhiyun * @ofdev: Pointer to OF device structure
1049*4882a593Smuzhiyun * @s: Property to be retrieved
1050*4882a593Smuzhiyun *
1051*4882a593Smuzhiyun * This function looks for a property in the device node and returns the value
1052*4882a593Smuzhiyun * of the property if its found or 0 if the property is not found.
1053*4882a593Smuzhiyun *
1054*4882a593Smuzhiyun * Return: Value of the parameter if the parameter is found, or 0 otherwise
1055*4882a593Smuzhiyun */
get_bool(struct platform_device * ofdev,const char * s)1056*4882a593Smuzhiyun static bool get_bool(struct platform_device *ofdev, const char *s)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun if (!p) {
1061*4882a593Smuzhiyun dev_warn(&ofdev->dev, "Parameter %s not found, defaulting to false\n", s);
1062*4882a593Smuzhiyun return false;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun return (bool)*p;
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun /**
1069*4882a593Smuzhiyun * xemaclite_ethtools_get_drvinfo - Get various Axi Emac Lite driver info
1070*4882a593Smuzhiyun * @ndev: Pointer to net_device structure
1071*4882a593Smuzhiyun * @ed: Pointer to ethtool_drvinfo structure
1072*4882a593Smuzhiyun *
1073*4882a593Smuzhiyun * This implements ethtool command for getting the driver information.
1074*4882a593Smuzhiyun * Issue "ethtool -i ethX" under linux prompt to execute this function.
1075*4882a593Smuzhiyun */
xemaclite_ethtools_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * ed)1076*4882a593Smuzhiyun static void xemaclite_ethtools_get_drvinfo(struct net_device *ndev,
1077*4882a593Smuzhiyun struct ethtool_drvinfo *ed)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun static const struct ethtool_ops xemaclite_ethtool_ops = {
1083*4882a593Smuzhiyun .get_drvinfo = xemaclite_ethtools_get_drvinfo,
1084*4882a593Smuzhiyun .get_link = ethtool_op_get_link,
1085*4882a593Smuzhiyun .get_link_ksettings = phy_ethtool_get_link_ksettings,
1086*4882a593Smuzhiyun .set_link_ksettings = phy_ethtool_set_link_ksettings,
1087*4882a593Smuzhiyun };
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun static const struct net_device_ops xemaclite_netdev_ops;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun /**
1092*4882a593Smuzhiyun * xemaclite_of_probe - Probe method for the Emaclite device.
1093*4882a593Smuzhiyun * @ofdev: Pointer to OF device structure
1094*4882a593Smuzhiyun *
1095*4882a593Smuzhiyun * This function probes for the Emaclite device in the device tree.
1096*4882a593Smuzhiyun * It initializes the driver data structure and the hardware, sets the MAC
1097*4882a593Smuzhiyun * address and registers the network device.
1098*4882a593Smuzhiyun * It also registers a mii_bus for the Emaclite device, if MDIO is included
1099*4882a593Smuzhiyun * in the device.
1100*4882a593Smuzhiyun *
1101*4882a593Smuzhiyun * Return: 0, if the driver is bound to the Emaclite device, or
1102*4882a593Smuzhiyun * a negative error if there is failure.
1103*4882a593Smuzhiyun */
xemaclite_of_probe(struct platform_device * ofdev)1104*4882a593Smuzhiyun static int xemaclite_of_probe(struct platform_device *ofdev)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun struct resource *res;
1107*4882a593Smuzhiyun struct net_device *ndev = NULL;
1108*4882a593Smuzhiyun struct net_local *lp = NULL;
1109*4882a593Smuzhiyun struct device *dev = &ofdev->dev;
1110*4882a593Smuzhiyun const void *mac_address;
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun int rc = 0;
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun dev_info(dev, "Device Tree Probing\n");
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun /* Create an ethernet device instance */
1117*4882a593Smuzhiyun ndev = alloc_etherdev(sizeof(struct net_local));
1118*4882a593Smuzhiyun if (!ndev)
1119*4882a593Smuzhiyun return -ENOMEM;
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun dev_set_drvdata(dev, ndev);
1122*4882a593Smuzhiyun SET_NETDEV_DEV(ndev, &ofdev->dev);
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun lp = netdev_priv(ndev);
1125*4882a593Smuzhiyun lp->ndev = ndev;
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun /* Get IRQ for the device */
1128*4882a593Smuzhiyun res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
1129*4882a593Smuzhiyun if (!res) {
1130*4882a593Smuzhiyun dev_err(dev, "no IRQ found\n");
1131*4882a593Smuzhiyun rc = -ENXIO;
1132*4882a593Smuzhiyun goto error;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun ndev->irq = res->start;
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
1138*4882a593Smuzhiyun lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
1139*4882a593Smuzhiyun if (IS_ERR(lp->base_addr)) {
1140*4882a593Smuzhiyun rc = PTR_ERR(lp->base_addr);
1141*4882a593Smuzhiyun goto error;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun ndev->mem_start = res->start;
1145*4882a593Smuzhiyun ndev->mem_end = res->end;
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun spin_lock_init(&lp->reset_lock);
1148*4882a593Smuzhiyun lp->next_tx_buf_to_use = 0x0;
1149*4882a593Smuzhiyun lp->next_rx_buf_to_use = 0x0;
1150*4882a593Smuzhiyun lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
1151*4882a593Smuzhiyun lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
1152*4882a593Smuzhiyun mac_address = of_get_mac_address(ofdev->dev.of_node);
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun if (!IS_ERR(mac_address)) {
1155*4882a593Smuzhiyun /* Set the MAC address. */
1156*4882a593Smuzhiyun ether_addr_copy(ndev->dev_addr, mac_address);
1157*4882a593Smuzhiyun } else {
1158*4882a593Smuzhiyun dev_warn(dev, "No MAC address found, using random\n");
1159*4882a593Smuzhiyun eth_hw_addr_random(ndev);
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun /* Clear the Tx CSR's in case this is a restart */
1163*4882a593Smuzhiyun xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
1164*4882a593Smuzhiyun xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun /* Set the MAC address in the EmacLite device */
1167*4882a593Smuzhiyun xemaclite_update_address(lp, ndev->dev_addr);
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
1170*4882a593Smuzhiyun xemaclite_mdio_setup(lp, &ofdev->dev);
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun ndev->netdev_ops = &xemaclite_netdev_ops;
1175*4882a593Smuzhiyun ndev->ethtool_ops = &xemaclite_ethtool_ops;
1176*4882a593Smuzhiyun ndev->flags &= ~IFF_MULTICAST;
1177*4882a593Smuzhiyun ndev->watchdog_timeo = TX_TIMEOUT;
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun /* Finally, register the device */
1180*4882a593Smuzhiyun rc = register_netdev(ndev);
1181*4882a593Smuzhiyun if (rc) {
1182*4882a593Smuzhiyun dev_err(dev,
1183*4882a593Smuzhiyun "Cannot register network device, aborting\n");
1184*4882a593Smuzhiyun goto put_node;
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun dev_info(dev,
1188*4882a593Smuzhiyun "Xilinx EmacLite at 0x%08X mapped to 0x%p, irq=%d\n",
1189*4882a593Smuzhiyun (unsigned int __force)ndev->mem_start, lp->base_addr, ndev->irq);
1190*4882a593Smuzhiyun return 0;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun put_node:
1193*4882a593Smuzhiyun of_node_put(lp->phy_node);
1194*4882a593Smuzhiyun error:
1195*4882a593Smuzhiyun free_netdev(ndev);
1196*4882a593Smuzhiyun return rc;
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun /**
1200*4882a593Smuzhiyun * xemaclite_of_remove - Unbind the driver from the Emaclite device.
1201*4882a593Smuzhiyun * @of_dev: Pointer to OF device structure
1202*4882a593Smuzhiyun *
1203*4882a593Smuzhiyun * This function is called if a device is physically removed from the system or
1204*4882a593Smuzhiyun * if the driver module is being unloaded. It frees any resources allocated to
1205*4882a593Smuzhiyun * the device.
1206*4882a593Smuzhiyun *
1207*4882a593Smuzhiyun * Return: 0, always.
1208*4882a593Smuzhiyun */
xemaclite_of_remove(struct platform_device * of_dev)1209*4882a593Smuzhiyun static int xemaclite_of_remove(struct platform_device *of_dev)
1210*4882a593Smuzhiyun {
1211*4882a593Smuzhiyun struct net_device *ndev = platform_get_drvdata(of_dev);
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun struct net_local *lp = netdev_priv(ndev);
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun /* Un-register the mii_bus, if configured */
1216*4882a593Smuzhiyun if (lp->mii_bus) {
1217*4882a593Smuzhiyun mdiobus_unregister(lp->mii_bus);
1218*4882a593Smuzhiyun mdiobus_free(lp->mii_bus);
1219*4882a593Smuzhiyun lp->mii_bus = NULL;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun unregister_netdev(ndev);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun of_node_put(lp->phy_node);
1225*4882a593Smuzhiyun lp->phy_node = NULL;
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun free_netdev(ndev);
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun return 0;
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun #ifdef CONFIG_NET_POLL_CONTROLLER
1233*4882a593Smuzhiyun static void
xemaclite_poll_controller(struct net_device * ndev)1234*4882a593Smuzhiyun xemaclite_poll_controller(struct net_device *ndev)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun disable_irq(ndev->irq);
1237*4882a593Smuzhiyun xemaclite_interrupt(ndev->irq, ndev);
1238*4882a593Smuzhiyun enable_irq(ndev->irq);
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun #endif
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun /* Ioctl MII Interface */
xemaclite_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)1243*4882a593Smuzhiyun static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1244*4882a593Smuzhiyun {
1245*4882a593Smuzhiyun if (!dev->phydev || !netif_running(dev))
1246*4882a593Smuzhiyun return -EINVAL;
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun switch (cmd) {
1249*4882a593Smuzhiyun case SIOCGMIIPHY:
1250*4882a593Smuzhiyun case SIOCGMIIREG:
1251*4882a593Smuzhiyun case SIOCSMIIREG:
1252*4882a593Smuzhiyun return phy_mii_ioctl(dev->phydev, rq, cmd);
1253*4882a593Smuzhiyun default:
1254*4882a593Smuzhiyun return -EOPNOTSUPP;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun static const struct net_device_ops xemaclite_netdev_ops = {
1259*4882a593Smuzhiyun .ndo_open = xemaclite_open,
1260*4882a593Smuzhiyun .ndo_stop = xemaclite_close,
1261*4882a593Smuzhiyun .ndo_start_xmit = xemaclite_send,
1262*4882a593Smuzhiyun .ndo_set_mac_address = xemaclite_set_mac_address,
1263*4882a593Smuzhiyun .ndo_tx_timeout = xemaclite_tx_timeout,
1264*4882a593Smuzhiyun .ndo_do_ioctl = xemaclite_ioctl,
1265*4882a593Smuzhiyun #ifdef CONFIG_NET_POLL_CONTROLLER
1266*4882a593Smuzhiyun .ndo_poll_controller = xemaclite_poll_controller,
1267*4882a593Smuzhiyun #endif
1268*4882a593Smuzhiyun };
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun /* Match table for OF platform binding */
1271*4882a593Smuzhiyun static const struct of_device_id xemaclite_of_match[] = {
1272*4882a593Smuzhiyun { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1273*4882a593Smuzhiyun { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1274*4882a593Smuzhiyun { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1275*4882a593Smuzhiyun { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1276*4882a593Smuzhiyun { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1277*4882a593Smuzhiyun { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
1278*4882a593Smuzhiyun { /* end of list */ },
1279*4882a593Smuzhiyun };
1280*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun static struct platform_driver xemaclite_of_driver = {
1283*4882a593Smuzhiyun .driver = {
1284*4882a593Smuzhiyun .name = DRIVER_NAME,
1285*4882a593Smuzhiyun .of_match_table = xemaclite_of_match,
1286*4882a593Smuzhiyun },
1287*4882a593Smuzhiyun .probe = xemaclite_of_probe,
1288*4882a593Smuzhiyun .remove = xemaclite_of_remove,
1289*4882a593Smuzhiyun };
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun module_platform_driver(xemaclite_of_driver);
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun MODULE_AUTHOR("Xilinx, Inc.");
1294*4882a593Smuzhiyun MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1295*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1296