2 * Atheros AR71xx built-in ethernet mac driver
4 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Based on Atheros' AG7100 driver
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/random.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/ethtool.h>
26 #include <linux/etherdevice.h>
27 #include <linux/phy.h>
28 #include <linux/skbuff.h>
29 #include <linux/dma-mapping.h>
31 #include <linux/bitops.h>
33 #include <asm/mach-ar71xx/ar71xx.h>
34 #include <asm/mach-ar71xx/platform.h>
38 #define AG71XX_DRV_NAME "ag71xx"
39 #define AG71XX_DRV_VERSION "0.3.10"
41 #define AG71XX_NAPI_TX 1
43 #define AG71XX_NAPI_WEIGHT 64
45 #define AG71XX_INT_ERR (AG71XX_INT_RX_BE | AG71XX_INT_TX_BE)
46 #define AG71XX_INT_TX (AG71XX_INT_TX_PS)
47 #define AG71XX_INT_RX (AG71XX_INT_RX_PR | AG71XX_INT_RX_OF)
50 #define AG71XX_INT_POLL (AG71XX_INT_RX | AG71XX_INT_TX)
51 #define AG71XX_INT_INIT (AG71XX_INT_ERR | AG71XX_INT_POLL)
53 #define AG71XX_INT_POLL (AG71XX_INT_RX)
54 #define AG71XX_INT_INIT (AG71XX_INT_ERR | AG71XX_INT_POLL | AG71XX_INT_TX)
57 #define AG71XX_TX_FIFO_LEN 2048
58 #define AG71XX_TX_MTU_LEN 1536
59 #define AG71XX_RX_PKT_RESERVE 64
60 #define AG71XX_RX_PKT_SIZE \
61 (AG71XX_RX_PKT_RESERVE + ETH_HLEN + ETH_FRAME_LEN + ETH_FCS_LEN)
63 #define AG71XX_TX_RING_SIZE 64
64 #define AG71XX_TX_THRES_STOP (AG71XX_TX_RING_SIZE - 4)
65 #define AG71XX_TX_THRES_WAKEUP \
66 (AG71XX_TX_RING_SIZE - (AG71XX_TX_RING_SIZE / 4))
68 #define AG71XX_RX_RING_SIZE 128
72 #define DBG(fmt, args...) printk(KERN_DEBUG fmt, ## args)
74 #define DBG(fmt, args...) do {} while (0)
77 #define ag71xx_assert(_cond) \
81 printk("%s,%d: assertion failed\n", __FILE__, __LINE__); \
88 #define DESC_EMPTY BIT(31)
89 #define DESC_MORE BIT(24)
90 #define DESC_PKTLEN_M 0x1fff
99 struct ag71xx_buf
*buf
;
100 struct ag71xx_desc
*descs
;
101 dma_addr_t descs_dma
;
108 void __iomem
*mac_base
;
109 void __iomem
*mii_ctrl
;
112 struct platform_device
*pdev
;
113 struct net_device
*dev
;
114 struct napi_struct napi
;
116 struct ag71xx_ring rx_ring
;
117 struct ag71xx_ring tx_ring
;
119 struct phy_device
*phy_dev
;
120 struct mii_bus mii_bus
;
127 extern struct ethtool_ops ag71xx_ethtool_ops
;
129 extern int ag71xx_mdio_init(struct ag71xx
*ag
, int id
);
130 extern void ag71xx_mdio_cleanup(struct ag71xx
*ag
);
131 extern int ag71xx_mii_peek(struct ag71xx
*ag
);
132 extern void ag71xx_mii_ctrl_set_if(struct ag71xx
*ag
, unsigned int mii_if
);
133 extern void ag71xx_mii_ctrl_set_speed(struct ag71xx
*ag
, unsigned int speed
);
134 extern void ag71xx_link_update(struct ag71xx
*ag
);
136 static inline struct ag71xx_platform_data
*ag71xx_get_pdata(struct ag71xx
*ag
)
138 return ag
->pdev
->dev
.platform_data
;
141 static inline void ag71xx_wr(struct ag71xx
*ag
, unsigned reg
, u32 value
)
143 __raw_writel(value
, ag
->mac_base
+ reg
);
146 static inline u32
ag71xx_rr(struct ag71xx
*ag
, unsigned reg
)
148 return __raw_readl(ag
->mac_base
+ reg
);
151 static inline void ag71xx_sb(struct ag71xx
*ag
, unsigned reg
, u32 mask
)
153 void __iomem
*r
= ag
->mac_base
+ reg
;
155 __raw_writel(__raw_readl(r
) | mask
, r
);
158 static inline void ag71xx_cb(struct ag71xx
*ag
, unsigned reg
, u32 mask
)
160 void __iomem
*r
= ag
->mac_base
+ reg
;
162 __raw_writel(__raw_readl(r
) & ~mask
, r
);
165 static inline int ag71xx_desc_empty(struct ag71xx_desc
*desc
)
167 return ((desc
->ctrl
& DESC_EMPTY
) != 0);
170 static inline int ag71xx_desc_pktlen(struct ag71xx_desc
*desc
)
172 return (desc
->ctrl
& DESC_PKTLEN_M
);
175 /* Register offsets */
176 #define AG71XX_REG_MAC_CFG1 0x0000
177 #define AG71XX_REG_MAC_CFG2 0x0004
178 #define AG71XX_REG_MAC_IPG 0x0008
179 #define AG71XX_REG_MAC_HDX 0x000c
180 #define AG71XX_REG_MAC_MFL 0x0010
181 #define AG71XX_REG_MII_CFG 0x0020
182 #define AG71XX_REG_MII_CMD 0x0024
183 #define AG71XX_REG_MII_ADDR 0x0028
184 #define AG71XX_REG_MII_CTRL 0x002c
185 #define AG71XX_REG_MII_STATUS 0x0030
186 #define AG71XX_REG_MII_IND 0x0034
187 #define AG71XX_REG_MAC_IFCTL 0x0038
188 #define AG71XX_REG_MAC_ADDR1 0x0040
189 #define AG71XX_REG_MAC_ADDR2 0x0044
190 #define AG71XX_REG_FIFO_CFG0 0x0048
191 #define AG71XX_REG_FIFO_CFG1 0x004c
192 #define AG71XX_REG_FIFO_CFG2 0x0050
193 #define AG71XX_REG_FIFO_CFG3 0x0054
194 #define AG71XX_REG_FIFO_CFG4 0x0058
195 #define AG71XX_REG_FIFO_CFG5 0x005c
196 #define AG71XX_REG_FIFO_RAM0 0x0060
197 #define AG71XX_REG_FIFO_RAM1 0x0064
198 #define AG71XX_REG_FIFO_RAM2 0x0068
199 #define AG71XX_REG_FIFO_RAM3 0x006c
200 #define AG71XX_REG_FIFO_RAM4 0x0070
201 #define AG71XX_REG_FIFO_RAM5 0x0074
202 #define AG71XX_REG_FIFO_RAM6 0x0078
203 #define AG71XX_REG_FIFO_RAM7 0x007c
205 #define AG71XX_REG_TX_CTRL 0x0180
206 #define AG71XX_REG_TX_DESC 0x0184
207 #define AG71XX_REG_TX_STATUS 0x0188
208 #define AG71XX_REG_RX_CTRL 0x018c
209 #define AG71XX_REG_RX_DESC 0x0190
210 #define AG71XX_REG_RX_STATUS 0x0194
211 #define AG71XX_REG_INT_ENABLE 0x0198
212 #define AG71XX_REG_INT_STATUS 0x019c
214 #define MAC_CFG1_TXE BIT(0)
215 #define MAC_CFG1_STX BIT(1)
216 #define MAC_CFG1_RXE BIT(2)
217 #define MAC_CFG1_SRX BIT(3)
218 #define MAC_CFG1_LB BIT(8)
219 #define MAC_CFG1_SR BIT(31)
221 #define MAC_CFG2_FDX BIT(0)
222 #define MAC_CFG2_CRC_EN BIT(1)
223 #define MAC_CFG2_PAD_CRC_EN BIT(2)
224 #define MAC_CFG2_LEN_CHECK BIT(4)
225 #define MAC_CFG2_HUGE_FRAME_EN BIT(5)
226 #define MAC_CFG2_IF_1000 BIT(9)
227 #define MAC_CFG2_IF_10_100 BIT(8)
229 #define AG71XX_INT_TX_PS BIT(0)
230 #define AG71XX_INT_TX_UR BIT(1)
231 #define AG71XX_INT_TX_BE BIT(3)
232 #define AG71XX_INT_RX_PR BIT(4)
233 #define AG71XX_INT_RX_OF BIT(6)
234 #define AG71XX_INT_RX_BE BIT(7)
236 #define MAC_IFCTL_SPEED BIT(16)
238 #define MII_CFG_CLK_DIV_4 0
239 #define MII_CFG_CLK_DIV_6 2
240 #define MII_CFG_CLK_DIV_8 3
241 #define MII_CFG_CLK_DIV_10 4
242 #define MII_CFG_CLK_DIV_14 5
243 #define MII_CFG_CLK_DIV_20 6
244 #define MII_CFG_CLK_DIV_28 7
246 #define MII_CMD_WRITE 0x0
247 #define MII_CMD_READ 0x1
249 #define MII_IND_BUSY BIT(0)
250 #define MII_IND_INVALID BIT(2)
252 #define TX_CTRL_TXE BIT(0)
254 #define TX_STATUS_PS BIT(0)
255 #define TX_STATUS_UR BIT(1)
256 #define TX_STATUS_BE BIT(3)
258 #define RX_CTRL_RXE BIT(0)
260 #define RX_STATUS_PR BIT(0)
261 #define RX_STATUS_OF BIT(1)
262 #define RX_STATUS_BE BIT(3)
264 #define FIFO_CFG5_BYTE_PER_CLK BIT(19)
266 #define MII_CTRL_SPEED_S 4
267 #define MII_CTRL_SPEED_M 3
268 #define MII_CTRL_SPEED_10 0
269 #define MII_CTRL_SPEED_100 1
270 #define MII_CTRL_SPEED_1000 2
272 static inline void ag71xx_int_enable(struct ag71xx
*ag
, u32 ints
)
274 ag71xx_sb(ag
, AG71XX_REG_INT_ENABLE
, ints
);
277 static inline void ag71xx_int_disable(struct ag71xx
*ag
, u32 ints
)
279 ag71xx_cb(ag
, AG71XX_REG_INT_ENABLE
, ints
);
282 #endif /* _AG71XX_H */