blob: 0fe140cf6e608c37e129ff462daf97901a4df38b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasute40095f2016-05-24 23:29:09 +02002/*
3 * Atheros AR71xx / AR9xxx GMAC driver
4 *
5 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
Rosy Song84265232019-03-16 09:24:50 +08006 * Copyright (C) 2019 Rosy Song <rosysong@rosinson.com>
Marek Vasute40095f2016-05-24 23:29:09 +02007 */
8
9#include <common.h>
Simon Glassd96c2602019-12-28 10:44:58 -070010#include <clock_legacy.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070011#include <cpu_func.h>
Marek Vasute40095f2016-05-24 23:29:09 +020012#include <dm.h>
13#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Marek Vasute40095f2016-05-24 23:29:09 +020015#include <miiphy.h>
16#include <malloc.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <net.h>
18#include <asm/cache.h>
Marek Vasute40095f2016-05-24 23:29:09 +020019#include <linux/compiler.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Marek Vasute40095f2016-05-24 23:29:09 +020021#include <linux/err.h>
22#include <linux/mii.h>
23#include <wait_bit.h>
24#include <asm/io.h>
25
26#include <mach/ath79.h>
27
28DECLARE_GLOBAL_DATA_PTR;
29
30enum ag7xxx_model {
31 AG7XXX_MODEL_AG933X,
32 AG7XXX_MODEL_AG934X,
Rosy Song84265232019-03-16 09:24:50 +080033 AG7XXX_MODEL_AG953X,
34 AG7XXX_MODEL_AG956X
Marek Vasute40095f2016-05-24 23:29:09 +020035};
36
Joe Hershberger9240a2f2017-06-26 14:40:08 -050037/* MAC Configuration 1 */
Marek Vasute40095f2016-05-24 23:29:09 +020038#define AG7XXX_ETH_CFG1 0x00
39#define AG7XXX_ETH_CFG1_SOFT_RST BIT(31)
40#define AG7XXX_ETH_CFG1_RX_RST BIT(19)
41#define AG7XXX_ETH_CFG1_TX_RST BIT(18)
42#define AG7XXX_ETH_CFG1_LOOPBACK BIT(8)
43#define AG7XXX_ETH_CFG1_RX_EN BIT(2)
44#define AG7XXX_ETH_CFG1_TX_EN BIT(0)
45
Joe Hershberger9240a2f2017-06-26 14:40:08 -050046/* MAC Configuration 2 */
Marek Vasute40095f2016-05-24 23:29:09 +020047#define AG7XXX_ETH_CFG2 0x04
48#define AG7XXX_ETH_CFG2_IF_1000 BIT(9)
49#define AG7XXX_ETH_CFG2_IF_10_100 BIT(8)
50#define AG7XXX_ETH_CFG2_IF_SPEED_MASK (3 << 8)
51#define AG7XXX_ETH_CFG2_HUGE_FRAME_EN BIT(5)
52#define AG7XXX_ETH_CFG2_LEN_CHECK BIT(4)
53#define AG7XXX_ETH_CFG2_PAD_CRC_EN BIT(2)
54#define AG7XXX_ETH_CFG2_FDX BIT(0)
55
Joe Hershberger9240a2f2017-06-26 14:40:08 -050056/* MII Configuration */
Marek Vasute40095f2016-05-24 23:29:09 +020057#define AG7XXX_ETH_MII_MGMT_CFG 0x20
58#define AG7XXX_ETH_MII_MGMT_CFG_RESET BIT(31)
59
Joe Hershberger9240a2f2017-06-26 14:40:08 -050060/* MII Command */
Marek Vasute40095f2016-05-24 23:29:09 +020061#define AG7XXX_ETH_MII_MGMT_CMD 0x24
62#define AG7XXX_ETH_MII_MGMT_CMD_READ 0x1
63
Joe Hershberger9240a2f2017-06-26 14:40:08 -050064/* MII Address */
Marek Vasute40095f2016-05-24 23:29:09 +020065#define AG7XXX_ETH_MII_MGMT_ADDRESS 0x28
66#define AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT 8
67
Joe Hershberger9240a2f2017-06-26 14:40:08 -050068/* MII Control */
Marek Vasute40095f2016-05-24 23:29:09 +020069#define AG7XXX_ETH_MII_MGMT_CTRL 0x2c
70
Joe Hershberger9240a2f2017-06-26 14:40:08 -050071/* MII Status */
Marek Vasute40095f2016-05-24 23:29:09 +020072#define AG7XXX_ETH_MII_MGMT_STATUS 0x30
73
Joe Hershberger9240a2f2017-06-26 14:40:08 -050074/* MII Indicators */
Marek Vasute40095f2016-05-24 23:29:09 +020075#define AG7XXX_ETH_MII_MGMT_IND 0x34
76#define AG7XXX_ETH_MII_MGMT_IND_INVALID BIT(2)
77#define AG7XXX_ETH_MII_MGMT_IND_BUSY BIT(0)
78
Joe Hershberger9240a2f2017-06-26 14:40:08 -050079/* STA Address 1 & 2 */
Marek Vasute40095f2016-05-24 23:29:09 +020080#define AG7XXX_ETH_ADDR1 0x40
81#define AG7XXX_ETH_ADDR2 0x44
82
Joe Hershberger9240a2f2017-06-26 14:40:08 -050083/* ETH Configuration 0 - 5 */
Marek Vasute40095f2016-05-24 23:29:09 +020084#define AG7XXX_ETH_FIFO_CFG_0 0x48
85#define AG7XXX_ETH_FIFO_CFG_1 0x4c
86#define AG7XXX_ETH_FIFO_CFG_2 0x50
87#define AG7XXX_ETH_FIFO_CFG_3 0x54
88#define AG7XXX_ETH_FIFO_CFG_4 0x58
89#define AG7XXX_ETH_FIFO_CFG_5 0x5c
90
Joe Hershberger9240a2f2017-06-26 14:40:08 -050091/* DMA Transfer Control for Queue 0 */
Marek Vasute40095f2016-05-24 23:29:09 +020092#define AG7XXX_ETH_DMA_TX_CTRL 0x180
93#define AG7XXX_ETH_DMA_TX_CTRL_TXE BIT(0)
94
Joe Hershberger9240a2f2017-06-26 14:40:08 -050095/* Descriptor Address for Queue 0 Tx */
Marek Vasute40095f2016-05-24 23:29:09 +020096#define AG7XXX_ETH_DMA_TX_DESC 0x184
97
Joe Hershberger9240a2f2017-06-26 14:40:08 -050098/* DMA Tx Status */
Marek Vasute40095f2016-05-24 23:29:09 +020099#define AG7XXX_ETH_DMA_TX_STATUS 0x188
100
Joe Hershberger9240a2f2017-06-26 14:40:08 -0500101/* Rx Control */
Marek Vasute40095f2016-05-24 23:29:09 +0200102#define AG7XXX_ETH_DMA_RX_CTRL 0x18c
103#define AG7XXX_ETH_DMA_RX_CTRL_RXE BIT(0)
104
Joe Hershberger9240a2f2017-06-26 14:40:08 -0500105/* Pointer to Rx Descriptor */
Marek Vasute40095f2016-05-24 23:29:09 +0200106#define AG7XXX_ETH_DMA_RX_DESC 0x190
107
Joe Hershberger9240a2f2017-06-26 14:40:08 -0500108/* Rx Status */
Marek Vasute40095f2016-05-24 23:29:09 +0200109#define AG7XXX_ETH_DMA_RX_STATUS 0x194
110
Rosy Songf1f943e2019-02-05 17:50:44 +0800111/* Custom register at 0x1805002C */
112#define AG7XXX_ETH_XMII 0x2C
113#define AG7XXX_ETH_XMII_TX_INVERT BIT(31)
114#define AG7XXX_ETH_XMII_RX_DELAY_LSB 28
115#define AG7XXX_ETH_XMII_RX_DELAY_MASK 0x30000000
116#define AG7XXX_ETH_XMII_RX_DELAY_SET(x) \
117 (((x) << AG7XXX_ETH_XMII_RX_DELAY_LSB) & AG7XXX_ETH_XMII_RX_DELAY_MASK)
118#define AG7XXX_ETH_XMII_TX_DELAY_LSB 26
119#define AG7XXX_ETH_XMII_TX_DELAY_MASK 0x0c000000
120#define AG7XXX_ETH_XMII_TX_DELAY_SET(x) \
121 (((x) << AG7XXX_ETH_XMII_TX_DELAY_LSB) & AG7XXX_ETH_XMII_TX_DELAY_MASK)
122#define AG7XXX_ETH_XMII_GIGE BIT(25)
123
Marek Vasute40095f2016-05-24 23:29:09 +0200124/* Custom register at 0x18070000 */
125#define AG7XXX_GMAC_ETH_CFG 0x00
Rosy Songf1f943e2019-02-05 17:50:44 +0800126#define AG7XXX_ETH_CFG_RXDV_DELAY_LSB 16
127#define AG7XXX_ETH_CFG_RXDV_DELAY_MASK 0x00030000
128#define AG7XXX_ETH_CFG_RXDV_DELAY_SET(x) \
129 (((x) << AG7XXX_ETH_CFG_RXDV_DELAY_LSB) & AG7XXX_ETH_CFG_RXDV_DELAY_MASK)
130#define AG7XXX_ETH_CFG_RXD_DELAY_LSB 14
131#define AG7XXX_ETH_CFG_RXD_DELAY_MASK 0x0000c000
132#define AG7XXX_ETH_CFG_RXD_DELAY_SET(x) \
133 (((x) << AG7XXX_ETH_CFG_RXD_DELAY_LSB) & AG7XXX_ETH_CFG_RXD_DELAY_MASK)
Marek Vasute40095f2016-05-24 23:29:09 +0200134#define AG7XXX_ETH_CFG_SW_PHY_ADDR_SWAP BIT(8)
135#define AG7XXX_ETH_CFG_SW_PHY_SWAP BIT(7)
136#define AG7XXX_ETH_CFG_SW_ONLY_MODE BIT(6)
137#define AG7XXX_ETH_CFG_GE0_ERR_EN BIT(5)
138#define AG7XXX_ETH_CFG_MII_GE0_SLAVE BIT(4)
139#define AG7XXX_ETH_CFG_MII_GE0_MASTER BIT(3)
140#define AG7XXX_ETH_CFG_GMII_GE0 BIT(2)
141#define AG7XXX_ETH_CFG_MII_GE0 BIT(1)
142#define AG7XXX_ETH_CFG_RGMII_GE0 BIT(0)
143
144#define CONFIG_TX_DESCR_NUM 8
145#define CONFIG_RX_DESCR_NUM 8
146#define CONFIG_ETH_BUFSIZE 2048
147#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
148#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
149
150/* DMA descriptor. */
151struct ag7xxx_dma_desc {
152 u32 data_addr;
153#define AG7XXX_DMADESC_IS_EMPTY BIT(31)
154#define AG7XXX_DMADESC_FTPP_OVERRIDE_OFFSET 16
155#define AG7XXX_DMADESC_PKT_SIZE_OFFSET 0
156#define AG7XXX_DMADESC_PKT_SIZE_MASK 0xfff
157 u32 config;
158 u32 next_desc;
159 u32 _pad[5];
160};
161
162struct ar7xxx_eth_priv {
163 struct ag7xxx_dma_desc tx_mac_descrtable[CONFIG_TX_DESCR_NUM];
164 struct ag7xxx_dma_desc rx_mac_descrtable[CONFIG_RX_DESCR_NUM];
165 char txbuffs[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
166 char rxbuffs[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
167
168 void __iomem *regs;
169 void __iomem *phyregs;
170
171 struct eth_device *dev;
172 struct phy_device *phydev;
173 struct mii_dev *bus;
174
175 u32 interface;
176 u32 tx_currdescnum;
177 u32 rx_currdescnum;
178 enum ag7xxx_model model;
179};
180
181/*
182 * Switch and MDIO access
183 */
184static int ag7xxx_switch_read(struct mii_dev *bus, int addr, int reg, u16 *val)
185{
186 struct ar7xxx_eth_priv *priv = bus->priv;
187 void __iomem *regs = priv->phyregs;
188 int ret;
189
190 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
191 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
192 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
193 writel(AG7XXX_ETH_MII_MGMT_CMD_READ,
194 regs + AG7XXX_ETH_MII_MGMT_CMD);
195
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100196 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
197 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
Marek Vasute40095f2016-05-24 23:29:09 +0200198 if (ret)
199 return ret;
200
201 *val = readl(regs + AG7XXX_ETH_MII_MGMT_STATUS) & 0xffff;
202 writel(0x0, regs + AG7XXX_ETH_MII_MGMT_CMD);
203
204 return 0;
205}
206
207static int ag7xxx_switch_write(struct mii_dev *bus, int addr, int reg, u16 val)
208{
209 struct ar7xxx_eth_priv *priv = bus->priv;
210 void __iomem *regs = priv->phyregs;
211 int ret;
212
213 writel((addr << AG7XXX_ETH_MII_MGMT_ADDRESS_SHIFT) | reg,
214 regs + AG7XXX_ETH_MII_MGMT_ADDRESS);
215 writel(val, regs + AG7XXX_ETH_MII_MGMT_CTRL);
216
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100217 ret = wait_for_bit_le32(regs + AG7XXX_ETH_MII_MGMT_IND,
218 AG7XXX_ETH_MII_MGMT_IND_BUSY, 0, 1000, 0);
Marek Vasute40095f2016-05-24 23:29:09 +0200219
220 return ret;
221}
222
223static int ag7xxx_switch_reg_read(struct mii_dev *bus, int reg, u32 *val)
224{
225 struct ar7xxx_eth_priv *priv = bus->priv;
226 u32 phy_addr;
227 u32 reg_addr;
228 u32 phy_temp;
229 u32 reg_temp;
Rosy Song84265232019-03-16 09:24:50 +0800230 u32 reg_temp_w = (reg & 0xfffffffc) >> 1;
Marek Vasute40095f2016-05-24 23:29:09 +0200231 u16 rv = 0;
232 int ret;
233
Rosy Songf1f943e2019-02-05 17:50:44 +0800234 if (priv->model == AG7XXX_MODEL_AG933X ||
235 priv->model == AG7XXX_MODEL_AG953X) {
Marek Vasute40095f2016-05-24 23:29:09 +0200236 phy_addr = 0x1f;
237 reg_addr = 0x10;
Rosy Song84265232019-03-16 09:24:50 +0800238 } else if (priv->model == AG7XXX_MODEL_AG934X ||
239 priv->model == AG7XXX_MODEL_AG956X) {
Marek Vasute40095f2016-05-24 23:29:09 +0200240 phy_addr = 0x18;
241 reg_addr = 0x00;
242 } else
243 return -EINVAL;
244
Rosy Song84265232019-03-16 09:24:50 +0800245 if (priv->model == AG7XXX_MODEL_AG956X)
246 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, (reg >> 9) & 0x1ff);
247 else
248 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
Marek Vasute40095f2016-05-24 23:29:09 +0200249 if (ret)
250 return ret;
251
252 phy_temp = ((reg >> 6) & 0x7) | 0x10;
Rosy Song84265232019-03-16 09:24:50 +0800253 if (priv->model == AG7XXX_MODEL_AG956X)
254 reg_temp = reg_temp_w & 0x1f;
255 else
256 reg_temp = (reg >> 1) & 0x1e;
Marek Vasute40095f2016-05-24 23:29:09 +0200257 *val = 0;
258
259 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 0, &rv);
260 if (ret < 0)
261 return ret;
262 *val |= rv;
263
Rosy Song84265232019-03-16 09:24:50 +0800264 if (priv->model == AG7XXX_MODEL_AG956X) {
265 phy_temp = (((reg_temp_w + 1) >> 5) & 0x7) | 0x10;
266 reg_temp = (reg_temp_w + 1) & 0x1f;
267 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp, &rv);
268 } else {
269 ret = ag7xxx_switch_read(bus, phy_temp, reg_temp | 1, &rv);
270 }
Marek Vasute40095f2016-05-24 23:29:09 +0200271 if (ret < 0)
272 return ret;
273 *val |= (rv << 16);
274
275 return 0;
276}
277
278static int ag7xxx_switch_reg_write(struct mii_dev *bus, int reg, u32 val)
279{
280 struct ar7xxx_eth_priv *priv = bus->priv;
281 u32 phy_addr;
282 u32 reg_addr;
283 u32 phy_temp;
284 u32 reg_temp;
Rosy Song84265232019-03-16 09:24:50 +0800285 u32 reg_temp_w = (reg & 0xfffffffc) >> 1;
Marek Vasute40095f2016-05-24 23:29:09 +0200286 int ret;
287
Rosy Songf1f943e2019-02-05 17:50:44 +0800288 if (priv->model == AG7XXX_MODEL_AG933X ||
289 priv->model == AG7XXX_MODEL_AG953X) {
Marek Vasute40095f2016-05-24 23:29:09 +0200290 phy_addr = 0x1f;
291 reg_addr = 0x10;
Rosy Song84265232019-03-16 09:24:50 +0800292 } else if (priv->model == AG7XXX_MODEL_AG934X ||
293 priv->model == AG7XXX_MODEL_AG956X) {
Marek Vasute40095f2016-05-24 23:29:09 +0200294 phy_addr = 0x18;
295 reg_addr = 0x00;
296 } else
297 return -EINVAL;
298
Rosy Song84265232019-03-16 09:24:50 +0800299 if (priv->model == AG7XXX_MODEL_AG956X)
300 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, (reg >> 9) & 0x1ff);
301 else
302 ret = ag7xxx_switch_write(bus, phy_addr, reg_addr, reg >> 9);
Marek Vasute40095f2016-05-24 23:29:09 +0200303 if (ret)
304 return ret;
305
Rosy Song84265232019-03-16 09:24:50 +0800306 if (priv->model == AG7XXX_MODEL_AG956X) {
307 reg_temp = (reg_temp_w + 1) & 0x1f;
308 phy_temp = (((reg_temp_w + 1) >> 5) & 0x7) | 0x10;
309 } else {
310 phy_temp = ((reg >> 6) & 0x7) | 0x10;
311 reg_temp = (reg >> 1) & 0x1e;
312 }
Marek Vasute40095f2016-05-24 23:29:09 +0200313
314 /*
315 * The switch on AR933x has some special register behavior, which
316 * expects particular write order of their nibbles:
317 * 0x40 ..... MSB first, LSB second
318 * 0x50 ..... MSB first, LSB second
319 * 0x98 ..... LSB first, MSB second
320 * others ... don't care
321 */
322 if ((priv->model == AG7XXX_MODEL_AG933X) && (reg == 0x98)) {
323 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
324 if (ret < 0)
325 return ret;
326
327 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
328 if (ret < 0)
329 return ret;
330 } else {
Rosy Song84265232019-03-16 09:24:50 +0800331 if (priv->model == AG7XXX_MODEL_AG956X)
332 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp, val >> 16);
333 else
334 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 1, val >> 16);
Marek Vasute40095f2016-05-24 23:29:09 +0200335 if (ret < 0)
336 return ret;
337
Rosy Song84265232019-03-16 09:24:50 +0800338 if (priv->model == AG7XXX_MODEL_AG956X) {
339 phy_temp = ((reg_temp_w >> 5) & 0x7) | 0x10;
340 reg_temp = reg_temp_w & 0x1f;
341 }
342
Marek Vasute40095f2016-05-24 23:29:09 +0200343 ret = ag7xxx_switch_write(bus, phy_temp, reg_temp | 0, val & 0xffff);
344 if (ret < 0)
345 return ret;
346 }
347
348 return 0;
349}
350
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500351static int ag7xxx_mdio_rw(struct mii_dev *bus, int addr, int reg, u32 val)
Marek Vasute40095f2016-05-24 23:29:09 +0200352{
353 u32 data;
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500354 unsigned long start;
355 int ret;
356 /* No idea if this is long enough or too long */
357 int timeout_ms = 1000;
Marek Vasute40095f2016-05-24 23:29:09 +0200358
359 /* Dummy read followed by PHY read/write command. */
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500360 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
361 if (ret < 0)
362 return ret;
Marek Vasute40095f2016-05-24 23:29:09 +0200363 data = val | (reg << 16) | (addr << 21) | BIT(30) | BIT(31);
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500364 ret = ag7xxx_switch_reg_write(bus, 0x98, data);
365 if (ret < 0)
366 return ret;
367
368 start = get_timer(0);
Marek Vasute40095f2016-05-24 23:29:09 +0200369
370 /* Wait for operation to finish */
371 do {
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500372 ret = ag7xxx_switch_reg_read(bus, 0x98, &data);
373 if (ret < 0)
374 return ret;
375
376 if (get_timer(start) > timeout_ms)
377 return -ETIMEDOUT;
Marek Vasute40095f2016-05-24 23:29:09 +0200378 } while (data & BIT(31));
379
380 return data & 0xffff;
381}
382
383static int ag7xxx_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
384{
385 return ag7xxx_mdio_rw(bus, addr, reg, BIT(27));
386}
387
388static int ag7xxx_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
389 u16 val)
390{
Joe Hershberger2fd519f2017-06-26 14:40:09 -0500391 int ret;
392
393 ret = ag7xxx_mdio_rw(bus, addr, reg, val);
394 if (ret < 0)
395 return ret;
Marek Vasute40095f2016-05-24 23:29:09 +0200396 return 0;
397}
398
399/*
400 * DMA ring handlers
401 */
402static void ag7xxx_dma_clean_tx(struct udevice *dev)
403{
404 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
405 struct ag7xxx_dma_desc *curr, *next;
406 u32 start, end;
407 int i;
408
409 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
410 curr = &priv->tx_mac_descrtable[i];
411 next = &priv->tx_mac_descrtable[(i + 1) % CONFIG_TX_DESCR_NUM];
412
413 curr->data_addr = virt_to_phys(&priv->txbuffs[i * CONFIG_ETH_BUFSIZE]);
414 curr->config = AG7XXX_DMADESC_IS_EMPTY;
415 curr->next_desc = virt_to_phys(next);
416 }
417
418 priv->tx_currdescnum = 0;
419
420 /* Cache: Flush descriptors, don't care about buffers. */
421 start = (u32)(&priv->tx_mac_descrtable[0]);
422 end = start + sizeof(priv->tx_mac_descrtable);
423 flush_dcache_range(start, end);
424}
425
426static void ag7xxx_dma_clean_rx(struct udevice *dev)
427{
428 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
429 struct ag7xxx_dma_desc *curr, *next;
430 u32 start, end;
431 int i;
432
433 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
434 curr = &priv->rx_mac_descrtable[i];
435 next = &priv->rx_mac_descrtable[(i + 1) % CONFIG_RX_DESCR_NUM];
436
437 curr->data_addr = virt_to_phys(&priv->rxbuffs[i * CONFIG_ETH_BUFSIZE]);
438 curr->config = AG7XXX_DMADESC_IS_EMPTY;
439 curr->next_desc = virt_to_phys(next);
440 }
441
442 priv->rx_currdescnum = 0;
443
444 /* Cache: Flush+Invalidate descriptors, Invalidate buffers. */
445 start = (u32)(&priv->rx_mac_descrtable[0]);
446 end = start + sizeof(priv->rx_mac_descrtable);
447 flush_dcache_range(start, end);
448 invalidate_dcache_range(start, end);
449
450 start = (u32)&priv->rxbuffs;
451 end = start + sizeof(priv->rxbuffs);
452 invalidate_dcache_range(start, end);
453}
454
455/*
456 * Ethernet I/O
457 */
458static int ag7xxx_eth_send(struct udevice *dev, void *packet, int length)
459{
460 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
461 struct ag7xxx_dma_desc *curr;
462 u32 start, end;
463
464 curr = &priv->tx_mac_descrtable[priv->tx_currdescnum];
465
466 /* Cache: Invalidate descriptor. */
467 start = (u32)curr;
468 end = start + sizeof(*curr);
469 invalidate_dcache_range(start, end);
470
471 if (!(curr->config & AG7XXX_DMADESC_IS_EMPTY)) {
472 printf("ag7xxx: Out of TX DMA descriptors!\n");
473 return -EPERM;
474 }
475
476 /* Copy the packet into the data buffer. */
477 memcpy(phys_to_virt(curr->data_addr), packet, length);
478 curr->config = length & AG7XXX_DMADESC_PKT_SIZE_MASK;
479
480 /* Cache: Flush descriptor, Flush buffer. */
481 start = (u32)curr;
482 end = start + sizeof(*curr);
483 flush_dcache_range(start, end);
484 start = (u32)phys_to_virt(curr->data_addr);
485 end = start + length;
486 flush_dcache_range(start, end);
487
488 /* Load the DMA descriptor and start TX DMA. */
489 writel(AG7XXX_ETH_DMA_TX_CTRL_TXE,
490 priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
491
492 /* Switch to next TX descriptor. */
493 priv->tx_currdescnum = (priv->tx_currdescnum + 1) % CONFIG_TX_DESCR_NUM;
494
495 return 0;
496}
497
498static int ag7xxx_eth_recv(struct udevice *dev, int flags, uchar **packetp)
499{
500 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
501 struct ag7xxx_dma_desc *curr;
502 u32 start, end, length;
503
504 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
505
506 /* Cache: Invalidate descriptor. */
507 start = (u32)curr;
508 end = start + sizeof(*curr);
509 invalidate_dcache_range(start, end);
510
511 /* No packets received. */
512 if (curr->config & AG7XXX_DMADESC_IS_EMPTY)
513 return -EAGAIN;
514
515 length = curr->config & AG7XXX_DMADESC_PKT_SIZE_MASK;
516
517 /* Cache: Invalidate buffer. */
518 start = (u32)phys_to_virt(curr->data_addr);
519 end = start + length;
520 invalidate_dcache_range(start, end);
521
522 /* Receive one packet and return length. */
523 *packetp = phys_to_virt(curr->data_addr);
524 return length;
525}
526
527static int ag7xxx_eth_free_pkt(struct udevice *dev, uchar *packet,
528 int length)
529{
530 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
531 struct ag7xxx_dma_desc *curr;
532 u32 start, end;
533
534 curr = &priv->rx_mac_descrtable[priv->rx_currdescnum];
535
536 curr->config = AG7XXX_DMADESC_IS_EMPTY;
537
538 /* Cache: Flush descriptor. */
539 start = (u32)curr;
540 end = start + sizeof(*curr);
541 flush_dcache_range(start, end);
542
543 /* Switch to next RX descriptor. */
544 priv->rx_currdescnum = (priv->rx_currdescnum + 1) % CONFIG_RX_DESCR_NUM;
545
546 return 0;
547}
548
549static int ag7xxx_eth_start(struct udevice *dev)
550{
551 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
552
553 /* FIXME: Check if link up */
554
555 /* Clear the DMA rings. */
556 ag7xxx_dma_clean_tx(dev);
557 ag7xxx_dma_clean_rx(dev);
558
559 /* Load DMA descriptors and start the RX DMA. */
560 writel(virt_to_phys(&priv->tx_mac_descrtable[priv->tx_currdescnum]),
561 priv->regs + AG7XXX_ETH_DMA_TX_DESC);
562 writel(virt_to_phys(&priv->rx_mac_descrtable[priv->rx_currdescnum]),
563 priv->regs + AG7XXX_ETH_DMA_RX_DESC);
564 writel(AG7XXX_ETH_DMA_RX_CTRL_RXE,
565 priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
566
567 return 0;
568}
569
570static void ag7xxx_eth_stop(struct udevice *dev)
571{
572 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
573
574 /* Stop the TX DMA. */
575 writel(0, priv->regs + AG7XXX_ETH_DMA_TX_CTRL);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100576 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_TX_CTRL, ~0, 0,
577 1000, 0);
Marek Vasute40095f2016-05-24 23:29:09 +0200578
579 /* Stop the RX DMA. */
580 writel(0, priv->regs + AG7XXX_ETH_DMA_RX_CTRL);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100581 wait_for_bit_le32(priv->regs + AG7XXX_ETH_DMA_RX_CTRL, ~0, 0,
582 1000, 0);
Marek Vasute40095f2016-05-24 23:29:09 +0200583}
584
585/*
586 * Hardware setup
587 */
588static int ag7xxx_eth_write_hwaddr(struct udevice *dev)
589{
590 struct eth_pdata *pdata = dev_get_platdata(dev);
591 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
592 unsigned char *mac = pdata->enetaddr;
593 u32 macid_lo, macid_hi;
594
595 macid_hi = mac[3] | (mac[2] << 8) | (mac[1] << 16) | (mac[0] << 24);
596 macid_lo = (mac[5] << 16) | (mac[4] << 24);
597
598 writel(macid_lo, priv->regs + AG7XXX_ETH_ADDR1);
599 writel(macid_hi, priv->regs + AG7XXX_ETH_ADDR2);
600
601 return 0;
602}
603
604static void ag7xxx_hw_setup(struct udevice *dev)
605{
606 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
607 u32 speed;
608
609 setbits_be32(priv->regs + AG7XXX_ETH_CFG1,
610 AG7XXX_ETH_CFG1_RX_RST | AG7XXX_ETH_CFG1_TX_RST |
611 AG7XXX_ETH_CFG1_SOFT_RST);
612
613 mdelay(10);
614
615 writel(AG7XXX_ETH_CFG1_RX_EN | AG7XXX_ETH_CFG1_TX_EN,
616 priv->regs + AG7XXX_ETH_CFG1);
617
618 if (priv->interface == PHY_INTERFACE_MODE_RMII)
619 speed = AG7XXX_ETH_CFG2_IF_10_100;
620 else
621 speed = AG7XXX_ETH_CFG2_IF_1000;
622
623 clrsetbits_be32(priv->regs + AG7XXX_ETH_CFG2,
624 AG7XXX_ETH_CFG2_IF_SPEED_MASK,
625 speed | AG7XXX_ETH_CFG2_PAD_CRC_EN |
626 AG7XXX_ETH_CFG2_LEN_CHECK);
627
628 writel(0xfff0000, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
629 writel(0x1fff, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
630
631 writel(0x1f00, priv->regs + AG7XXX_ETH_FIFO_CFG_0);
632 setbits_be32(priv->regs + AG7XXX_ETH_FIFO_CFG_4, 0x3ffff);
633 writel(0x10ffff, priv->regs + AG7XXX_ETH_FIFO_CFG_1);
634 writel(0xaaa0555, priv->regs + AG7XXX_ETH_FIFO_CFG_2);
635 writel(0x7eccf, priv->regs + AG7XXX_ETH_FIFO_CFG_5);
636 writel(0x1f00140, priv->regs + AG7XXX_ETH_FIFO_CFG_3);
637}
638
639static int ag7xxx_mii_get_div(void)
640{
641 ulong freq = get_bus_freq(0);
642
643 switch (freq / 1000000) {
644 case 150: return 0x7;
645 case 175: return 0x5;
646 case 200: return 0x4;
647 case 210: return 0x9;
648 case 220: return 0x9;
649 default: return 0x7;
650 }
651}
652
653static int ag7xxx_mii_setup(struct udevice *dev)
654{
655 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
656 int i, ret, div = ag7xxx_mii_get_div();
657 u32 reg;
658
659 if (priv->model == AG7XXX_MODEL_AG933X) {
660 /* Unit 0 is PHY-less on AR9331, see datasheet Figure 2-3 */
661 if (priv->interface == PHY_INTERFACE_MODE_RMII)
662 return 0;
663 }
664
Rosy Songf1f943e2019-02-05 17:50:44 +0800665 if (priv->model == AG7XXX_MODEL_AG934X)
666 reg = 0x4;
667 else if (priv->model == AG7XXX_MODEL_AG953X)
668 reg = 0x2;
Rosy Song84265232019-03-16 09:24:50 +0800669 else if (priv->model == AG7XXX_MODEL_AG956X)
670 reg = 0x7;
Rosy Songf1f943e2019-02-05 17:50:44 +0800671
672 if (priv->model == AG7XXX_MODEL_AG934X ||
Rosy Song84265232019-03-16 09:24:50 +0800673 priv->model == AG7XXX_MODEL_AG953X ||
674 priv->model == AG7XXX_MODEL_AG956X) {
Rosy Songf1f943e2019-02-05 17:50:44 +0800675 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | reg,
Marek Vasute40095f2016-05-24 23:29:09 +0200676 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
Rosy Songf1f943e2019-02-05 17:50:44 +0800677 writel(reg, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
Marek Vasute40095f2016-05-24 23:29:09 +0200678 return 0;
679 }
680
681 for (i = 0; i < 10; i++) {
682 writel(AG7XXX_ETH_MII_MGMT_CFG_RESET | div,
683 priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
684 writel(div, priv->regs + AG7XXX_ETH_MII_MGMT_CFG);
685
686 /* Check the switch */
687 ret = ag7xxx_switch_reg_read(priv->bus, 0x10c, &reg);
688 if (ret)
689 continue;
690
691 if (reg != 0x18007fff)
692 continue;
693
694 return 0;
695 }
696
697 return -EINVAL;
698}
699
700static int ag933x_phy_setup_wan(struct udevice *dev)
701{
702 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
703
704 /* Configure switch port 4 (GMAC0) */
705 return ag7xxx_mdio_write(priv->bus, 4, 0, MII_BMCR, 0x9000);
706}
707
708static int ag933x_phy_setup_lan(struct udevice *dev)
709{
710 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
711 int i, ret;
712 u32 reg;
713
714 /* Reset the switch */
715 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
716 if (ret)
717 return ret;
718 reg |= BIT(31);
719 ret = ag7xxx_switch_reg_write(priv->bus, 0, reg);
720 if (ret)
721 return ret;
722
723 do {
724 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
725 if (ret)
726 return ret;
727 } while (reg & BIT(31));
728
729 /* Configure switch ports 0...3 (GMAC1) */
730 for (i = 0; i < 4; i++) {
731 ret = ag7xxx_mdio_write(priv->bus, 0x4, 0, MII_BMCR, 0x9000);
732 if (ret)
733 return ret;
734 }
735
736 /* Enable CPU port */
737 ret = ag7xxx_switch_reg_write(priv->bus, 0x78, BIT(8));
738 if (ret)
739 return ret;
740
741 for (i = 0; i < 4; i++) {
742 ret = ag7xxx_switch_reg_write(priv->bus, i * 0x100, BIT(9));
743 if (ret)
744 return ret;
745 }
746
747 /* QM Control */
748 ret = ag7xxx_switch_reg_write(priv->bus, 0x38, 0xc000050e);
749 if (ret)
750 return ret;
751
752 /* Disable Atheros header */
753 ret = ag7xxx_switch_reg_write(priv->bus, 0x104, 0x4004);
754 if (ret)
755 return ret;
756
757 /* Tag priority mapping */
758 ret = ag7xxx_switch_reg_write(priv->bus, 0x70, 0xfa50);
759 if (ret)
760 return ret;
761
762 /* Enable ARP packets to the CPU */
763 ret = ag7xxx_switch_reg_read(priv->bus, 0x5c, &reg);
764 if (ret)
765 return ret;
766 reg |= 0x100000;
767 ret = ag7xxx_switch_reg_write(priv->bus, 0x5c, reg);
768 if (ret)
769 return ret;
770
771 return 0;
772}
773
Rosy Songf1f943e2019-02-05 17:50:44 +0800774static int ag953x_phy_setup_wan(struct udevice *dev)
775{
776 int ret;
777 u32 reg = 0;
778 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
779
780 /* Set wan port connect to GE0 */
781 ret = ag7xxx_switch_reg_read(priv->bus, 0x8, &reg);
782 if (ret)
783 return ret;
784
785 ret = ag7xxx_switch_reg_write(priv->bus, 0x8, reg | BIT(28));
786 if (ret)
787 return ret;
788
789 /* Configure switch port 4 (GMAC0) */
790 ret = ag7xxx_switch_write(priv->bus, 4, MII_BMCR, 0x9000);
791 if (ret)
792 return ret;
793
794 return 0;
795}
796
797static int ag953x_phy_setup_lan(struct udevice *dev)
798{
799 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
800 int i, ret;
801 u32 reg = 0;
802
803 /* Reset the switch */
804 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
805 if (ret)
806 return ret;
807
808 ret = ag7xxx_switch_reg_write(priv->bus, 0, reg | BIT(31));
809 if (ret)
810 return ret;
811
812 do {
813 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
814 if (ret)
815 return ret;
816 } while (reg & BIT(31));
817
818 ret = ag7xxx_switch_reg_write(priv->bus, 0x100, 0x4e);
819 if (ret)
820 return ret;
821
822 /* Set GMII mode */
823 ret = ag7xxx_switch_reg_read(priv->bus, 0x4, &reg);
824 if (ret)
825 return ret;
826
827 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, reg | BIT(6));
828 if (ret)
829 return ret;
830
831 /* Configure switch ports 0...4 (GMAC1) */
832 for (i = 0; i < 5; i++) {
833 ret = ag7xxx_switch_write(priv->bus, i, MII_BMCR, 0x9000);
834 if (ret)
835 return ret;
836 }
837
838 for (i = 0; i < 5; i++) {
839 ret = ag7xxx_switch_reg_write(priv->bus, (i + 2) * 0x100, BIT(9));
840 if (ret)
841 return ret;
842 }
843
844 /* QM Control */
845 ret = ag7xxx_switch_reg_write(priv->bus, 0x38, 0xc000050e);
846 if (ret)
847 return ret;
848
849 /* Disable Atheros header */
850 ret = ag7xxx_switch_reg_write(priv->bus, 0x104, 0x4004);
851 if (ret)
852 return ret;
853
854 /* Tag priority mapping */
855 ret = ag7xxx_switch_reg_write(priv->bus, 0x70, 0xfa50);
856 if (ret)
857 return ret;
858
859 /* Enable ARP packets to the CPU */
860 ret = ag7xxx_switch_reg_read(priv->bus, 0x5c, &reg);
861 if (ret)
862 return ret;
863
864 ret = ag7xxx_switch_reg_write(priv->bus, 0x5c, reg | 0x100000);
865 if (ret)
866 return ret;
867
868 /* Enable broadcast packets to the CPU */
869 ret = ag7xxx_switch_reg_read(priv->bus, 0x2c, &reg);
870 if (ret)
871 return ret;
872
873 ret = ag7xxx_switch_reg_write(priv->bus, 0x2c, reg | BIT(25) | BIT(26));
874 if (ret)
875 return ret;
876
877 return 0;
878}
879
Marek Vasute40095f2016-05-24 23:29:09 +0200880static int ag933x_phy_setup_reset_set(struct udevice *dev, int port)
881{
882 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
883 int ret;
884
Rosy Song84265232019-03-16 09:24:50 +0800885 if (priv->model == AG7XXX_MODEL_AG953X ||
886 priv->model == AG7XXX_MODEL_AG956X) {
Rosy Songf1f943e2019-02-05 17:50:44 +0800887 ret = ag7xxx_switch_write(priv->bus, port, MII_ADVERTISE,
888 ADVERTISE_ALL);
889 } else {
890 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_ADVERTISE,
891 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
892 ADVERTISE_PAUSE_ASYM);
893 }
Marek Vasute40095f2016-05-24 23:29:09 +0200894 if (ret)
895 return ret;
896
897 if (priv->model == AG7XXX_MODEL_AG934X) {
898 ret = ag7xxx_mdio_write(priv->bus, port, 0, MII_CTRL1000,
899 ADVERTISE_1000FULL);
900 if (ret)
901 return ret;
Rosy Song84265232019-03-16 09:24:50 +0800902 } else if (priv->model == AG7XXX_MODEL_AG956X) {
903 ret = ag7xxx_switch_write(priv->bus, port, MII_CTRL1000,
904 ADVERTISE_1000FULL);
905 if (ret)
906 return ret;
Marek Vasute40095f2016-05-24 23:29:09 +0200907 }
908
Rosy Song84265232019-03-16 09:24:50 +0800909 if (priv->model == AG7XXX_MODEL_AG953X ||
910 priv->model == AG7XXX_MODEL_AG956X)
Rosy Songf1f943e2019-02-05 17:50:44 +0800911 return ag7xxx_switch_write(priv->bus, port, MII_BMCR,
912 BMCR_ANENABLE | BMCR_RESET);
913
Marek Vasute40095f2016-05-24 23:29:09 +0200914 return ag7xxx_mdio_write(priv->bus, port, 0, MII_BMCR,
915 BMCR_ANENABLE | BMCR_RESET);
916}
917
918static int ag933x_phy_setup_reset_fin(struct udevice *dev, int port)
919{
920 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
921 int ret;
Rosy Songf1f943e2019-02-05 17:50:44 +0800922 u16 reg;
Marek Vasute40095f2016-05-24 23:29:09 +0200923
Rosy Song84265232019-03-16 09:24:50 +0800924 if (priv->model == AG7XXX_MODEL_AG953X ||
925 priv->model == AG7XXX_MODEL_AG956X) {
Rosy Songf1f943e2019-02-05 17:50:44 +0800926 do {
927 ret = ag7xxx_switch_read(priv->bus, port, MII_BMCR, &reg);
928 if (ret < 0)
929 return ret;
930 mdelay(10);
931 } while (reg & BMCR_RESET);
932 } else {
933 do {
934 ret = ag7xxx_mdio_read(priv->bus, port, 0, MII_BMCR);
935 if (ret < 0)
936 return ret;
937 mdelay(10);
938 } while (ret & BMCR_RESET);
939 }
Marek Vasute40095f2016-05-24 23:29:09 +0200940
941 return 0;
942}
943
944static int ag933x_phy_setup_common(struct udevice *dev)
945{
946 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
947 int i, ret, phymax;
Rosy Songf1f943e2019-02-05 17:50:44 +0800948 u16 reg;
Marek Vasute40095f2016-05-24 23:29:09 +0200949
950 if (priv->model == AG7XXX_MODEL_AG933X)
951 phymax = 4;
Rosy Songf1f943e2019-02-05 17:50:44 +0800952 else if (priv->model == AG7XXX_MODEL_AG934X ||
Rosy Song84265232019-03-16 09:24:50 +0800953 priv->model == AG7XXX_MODEL_AG953X ||
954 priv->model == AG7XXX_MODEL_AG956X)
Marek Vasute40095f2016-05-24 23:29:09 +0200955 phymax = 5;
956 else
957 return -EINVAL;
958
959 if (priv->interface == PHY_INTERFACE_MODE_RMII) {
960 ret = ag933x_phy_setup_reset_set(dev, phymax);
961 if (ret)
962 return ret;
963
964 ret = ag933x_phy_setup_reset_fin(dev, phymax);
965 if (ret)
966 return ret;
967
968 /* Read out link status */
Rosy Songf1f943e2019-02-05 17:50:44 +0800969 if (priv->model == AG7XXX_MODEL_AG953X)
970 ret = ag7xxx_switch_read(priv->bus, phymax, MII_MIPSCR, &reg);
971 else
972 ret = ag7xxx_mdio_read(priv->bus, phymax, 0, MII_MIPSCR);
Marek Vasute40095f2016-05-24 23:29:09 +0200973 if (ret < 0)
974 return ret;
975
976 return 0;
977 }
978
979 /* Switch ports */
980 for (i = 0; i < phymax; i++) {
981 ret = ag933x_phy_setup_reset_set(dev, i);
982 if (ret)
983 return ret;
984 }
985
986 for (i = 0; i < phymax; i++) {
987 ret = ag933x_phy_setup_reset_fin(dev, i);
988 if (ret)
989 return ret;
990 }
991
992 for (i = 0; i < phymax; i++) {
993 /* Read out link status */
Rosy Song84265232019-03-16 09:24:50 +0800994 if (priv->model == AG7XXX_MODEL_AG953X ||
995 priv->model == AG7XXX_MODEL_AG956X)
Rosy Songf1f943e2019-02-05 17:50:44 +0800996 ret = ag7xxx_switch_read(priv->bus, i, MII_MIPSCR, &reg);
997 else
998 ret = ag7xxx_mdio_read(priv->bus, i, 0, MII_MIPSCR);
Marek Vasute40095f2016-05-24 23:29:09 +0200999 if (ret < 0)
1000 return ret;
1001 }
1002
1003 return 0;
1004}
1005
1006static int ag934x_phy_setup(struct udevice *dev)
1007{
1008 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1009 int i, ret;
1010 u32 reg;
1011
1012 ret = ag7xxx_switch_reg_write(priv->bus, 0x624, 0x7f7f7f7f);
1013 if (ret)
1014 return ret;
1015 ret = ag7xxx_switch_reg_write(priv->bus, 0x10, 0x40000000);
1016 if (ret)
1017 return ret;
1018 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, 0x07600000);
1019 if (ret)
1020 return ret;
1021 ret = ag7xxx_switch_reg_write(priv->bus, 0xc, 0x01000000);
1022 if (ret)
1023 return ret;
1024 ret = ag7xxx_switch_reg_write(priv->bus, 0x7c, 0x0000007e);
1025 if (ret)
1026 return ret;
1027
1028 /* AR8327/AR8328 v1.0 fixup */
1029 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
1030 if (ret)
1031 return ret;
1032 if ((reg & 0xffff) == 0x1201) {
1033 for (i = 0; i < 5; i++) {
1034 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x0);
1035 if (ret)
1036 return ret;
1037 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x02ea);
1038 if (ret)
1039 return ret;
1040 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1d, 0x3d);
1041 if (ret)
1042 return ret;
1043 ret = ag7xxx_mdio_write(priv->bus, i, 0, 0x1e, 0x68a0);
1044 if (ret)
1045 return ret;
1046 }
1047 }
1048
1049 ret = ag7xxx_switch_reg_read(priv->bus, 0x66c, &reg);
1050 if (ret)
1051 return ret;
1052 reg &= ~0x70000;
1053 ret = ag7xxx_switch_reg_write(priv->bus, 0x66c, reg);
1054 if (ret)
1055 return ret;
1056
1057 return 0;
1058}
1059
Rosy Song84265232019-03-16 09:24:50 +08001060static int ag956x_phy_setup(struct udevice *dev)
1061{
1062 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1063 int i, ret;
1064 u32 reg, ctrl;
1065
1066 ret = ag7xxx_switch_reg_read(priv->bus, 0x0, &reg);
1067 if (ret)
1068 return ret;
1069 if ((reg & 0xffff) >= 0x1301)
1070 ctrl = 0xc74164de;
1071 else
1072 ctrl = 0xc74164d0;
1073
1074 ret = ag7xxx_switch_reg_write(priv->bus, 0x4, BIT(7));
1075 if (ret)
1076 return ret;
1077
1078 ret = ag7xxx_switch_reg_write(priv->bus, 0xe0, ctrl);
1079 if (ret)
1080 return ret;
1081
1082 ret = ag7xxx_switch_reg_write(priv->bus, 0x624, 0x7f7f7f7f);
1083 if (ret)
1084 return ret;
1085
1086 /*
1087 * Values suggested by the switch team when s17 in sgmii
1088 * configuration. 0x10(S17_PWS_REG) = 0x602613a0
1089 */
1090 ret = ag7xxx_switch_reg_write(priv->bus, 0x10, 0x602613a0);
1091 if (ret)
1092 return ret;
1093
1094 ret = ag7xxx_switch_reg_write(priv->bus, 0x7c, 0x0000007e);
1095 if (ret)
1096 return ret;
1097
1098 /* AR8337/AR8334 v1.0 fixup */
1099 ret = ag7xxx_switch_reg_read(priv->bus, 0, &reg);
1100 if (ret)
1101 return ret;
1102 if ((reg & 0xffff) == 0x1301) {
1103 for (i = 0; i < 5; i++) {
1104 /* Turn on Gigabit clock */
1105 ret = ag7xxx_switch_write(priv->bus, i, 0x1d, 0x3d);
1106 if (ret)
1107 return ret;
1108 ret = ag7xxx_switch_write(priv->bus, i, 0x1e, 0x6820);
1109 if (ret)
1110 return ret;
1111 }
1112 }
1113
1114 return 0;
1115}
1116
Marek Vasute40095f2016-05-24 23:29:09 +02001117static int ag7xxx_mac_probe(struct udevice *dev)
1118{
1119 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1120 int ret;
1121
1122 ag7xxx_hw_setup(dev);
1123 ret = ag7xxx_mii_setup(dev);
1124 if (ret)
1125 return ret;
1126
1127 ag7xxx_eth_write_hwaddr(dev);
1128
1129 if (priv->model == AG7XXX_MODEL_AG933X) {
1130 if (priv->interface == PHY_INTERFACE_MODE_RMII)
1131 ret = ag933x_phy_setup_wan(dev);
1132 else
1133 ret = ag933x_phy_setup_lan(dev);
Rosy Songf1f943e2019-02-05 17:50:44 +08001134 } else if (priv->model == AG7XXX_MODEL_AG953X) {
1135 if (priv->interface == PHY_INTERFACE_MODE_RMII)
1136 ret = ag953x_phy_setup_wan(dev);
1137 else
1138 ret = ag953x_phy_setup_lan(dev);
Marek Vasute40095f2016-05-24 23:29:09 +02001139 } else if (priv->model == AG7XXX_MODEL_AG934X) {
1140 ret = ag934x_phy_setup(dev);
Rosy Song84265232019-03-16 09:24:50 +08001141 } else if (priv->model == AG7XXX_MODEL_AG956X) {
1142 ret = ag956x_phy_setup(dev);
Marek Vasute40095f2016-05-24 23:29:09 +02001143 } else {
1144 return -EINVAL;
1145 }
1146
1147 if (ret)
1148 return ret;
1149
1150 return ag933x_phy_setup_common(dev);
1151}
1152
1153static int ag7xxx_mdio_probe(struct udevice *dev)
1154{
1155 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1156 struct mii_dev *bus = mdio_alloc();
1157
1158 if (!bus)
1159 return -ENOMEM;
1160
1161 bus->read = ag7xxx_mdio_read;
1162 bus->write = ag7xxx_mdio_write;
1163 snprintf(bus->name, sizeof(bus->name), dev->name);
1164
1165 bus->priv = (void *)priv;
1166
1167 return mdio_register(bus);
1168}
1169
1170static int ag7xxx_get_phy_iface_offset(struct udevice *dev)
1171{
1172 int offset;
1173
Simon Glasse160f7d2017-01-17 16:52:55 -07001174 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), "phy");
Marek Vasute40095f2016-05-24 23:29:09 +02001175 if (offset <= 0) {
1176 debug("%s: PHY OF node not found (ret=%i)\n", __func__, offset);
1177 return -EINVAL;
1178 }
1179
1180 offset = fdt_parent_offset(gd->fdt_blob, offset);
1181 if (offset <= 0) {
1182 debug("%s: PHY OF node parent MDIO bus not found (ret=%i)\n",
1183 __func__, offset);
1184 return -EINVAL;
1185 }
1186
1187 offset = fdt_parent_offset(gd->fdt_blob, offset);
1188 if (offset <= 0) {
1189 debug("%s: PHY MDIO OF node parent MAC not found (ret=%i)\n",
1190 __func__, offset);
1191 return -EINVAL;
1192 }
1193
1194 return offset;
1195}
1196
1197static int ag7xxx_eth_probe(struct udevice *dev)
1198{
1199 struct eth_pdata *pdata = dev_get_platdata(dev);
1200 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1201 void __iomem *iobase, *phyiobase;
1202 int ret, phyreg;
1203
1204 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
1205 ret = ag7xxx_get_phy_iface_offset(dev);
1206 if (ret <= 0)
1207 return ret;
1208 phyreg = fdtdec_get_int(gd->fdt_blob, ret, "reg", -1);
1209
1210 iobase = map_physmem(pdata->iobase, 0x200, MAP_NOCACHE);
1211 phyiobase = map_physmem(phyreg, 0x200, MAP_NOCACHE);
1212
1213 debug("%s, iobase=%p, phyiobase=%p, priv=%p\n",
1214 __func__, iobase, phyiobase, priv);
1215 priv->regs = iobase;
1216 priv->phyregs = phyiobase;
1217 priv->interface = pdata->phy_interface;
1218 priv->model = dev_get_driver_data(dev);
1219
1220 ret = ag7xxx_mdio_probe(dev);
1221 if (ret)
1222 return ret;
1223
1224 priv->bus = miiphy_get_dev_by_name(dev->name);
1225
1226 ret = ag7xxx_mac_probe(dev);
1227 debug("%s, ret=%d\n", __func__, ret);
1228
1229 return ret;
1230}
1231
1232static int ag7xxx_eth_remove(struct udevice *dev)
1233{
1234 struct ar7xxx_eth_priv *priv = dev_get_priv(dev);
1235
1236 free(priv->phydev);
1237 mdio_unregister(priv->bus);
1238 mdio_free(priv->bus);
1239
1240 return 0;
1241}
1242
1243static const struct eth_ops ag7xxx_eth_ops = {
1244 .start = ag7xxx_eth_start,
1245 .send = ag7xxx_eth_send,
1246 .recv = ag7xxx_eth_recv,
1247 .free_pkt = ag7xxx_eth_free_pkt,
1248 .stop = ag7xxx_eth_stop,
1249 .write_hwaddr = ag7xxx_eth_write_hwaddr,
1250};
1251
1252static int ag7xxx_eth_ofdata_to_platdata(struct udevice *dev)
1253{
1254 struct eth_pdata *pdata = dev_get_platdata(dev);
1255 const char *phy_mode;
1256 int ret;
1257
Simon Glassa821c4a2017-05-17 17:18:05 -06001258 pdata->iobase = devfdt_get_addr(dev);
Marek Vasute40095f2016-05-24 23:29:09 +02001259 pdata->phy_interface = -1;
1260
1261 /* Decoding of convoluted PHY wiring on Atheros MIPS. */
1262 ret = ag7xxx_get_phy_iface_offset(dev);
1263 if (ret <= 0)
1264 return ret;
1265
1266 phy_mode = fdt_getprop(gd->fdt_blob, ret, "phy-mode", NULL);
1267 if (phy_mode)
1268 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1269 if (pdata->phy_interface == -1) {
1270 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1271 return -EINVAL;
1272 }
1273
1274 return 0;
1275}
1276
1277static const struct udevice_id ag7xxx_eth_ids[] = {
1278 { .compatible = "qca,ag933x-mac", .data = AG7XXX_MODEL_AG933X },
1279 { .compatible = "qca,ag934x-mac", .data = AG7XXX_MODEL_AG934X },
Rosy Songf1f943e2019-02-05 17:50:44 +08001280 { .compatible = "qca,ag953x-mac", .data = AG7XXX_MODEL_AG953X },
Rosy Song84265232019-03-16 09:24:50 +08001281 { .compatible = "qca,ag956x-mac", .data = AG7XXX_MODEL_AG956X },
Marek Vasute40095f2016-05-24 23:29:09 +02001282 { }
1283};
1284
1285U_BOOT_DRIVER(eth_ag7xxx) = {
1286 .name = "eth_ag7xxx",
1287 .id = UCLASS_ETH,
1288 .of_match = ag7xxx_eth_ids,
1289 .ofdata_to_platdata = ag7xxx_eth_ofdata_to_platdata,
1290 .probe = ag7xxx_eth_probe,
1291 .remove = ag7xxx_eth_remove,
1292 .ops = &ag7xxx_eth_ops,
1293 .priv_auto_alloc_size = sizeof(struct ar7xxx_eth_priv),
1294 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1295 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1296};