blob: b7ed69413fec3d9bc1ca2b25e80b2fc9ecd70710 [file] [log] [blame]
Thomas Chou7bb1b9b2010-04-20 12:49:52 +08001/*
2 * Altera 10/100/1000 triple speed ethernet mac driver
3 *
4 * Copyright (C) 2008 Altera Corporation.
5 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080011#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Thomas Chouec06dd82015-10-22 15:29:11 +080013#include <dm.h>
14#include <errno.h>
15#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Thomas Chouec06dd82015-10-22 15:29:11 +080017#include <memalign.h>
18#include <miiphy.h>
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080019#include <net.h>
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080020#include <asm/cache.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090021#include <linux/dma-mapping.h>
Thomas Chouec06dd82015-10-22 15:29:11 +080022#include <asm/io.h>
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080023#include "altera_tse.h"
24
Thomas Chouec06dd82015-10-22 15:29:11 +080025DECLARE_GLOBAL_DATA_PTR;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080026
Thomas Chouec06dd82015-10-22 15:29:11 +080027static inline void alt_sgdma_construct_descriptor(
28 struct alt_sgdma_descriptor *desc,
29 struct alt_sgdma_descriptor *next,
30 void *read_addr,
31 void *write_addr,
Thomas Chou455e9532015-11-06 09:36:26 +080032 u16 length_or_eop,
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080033 int generate_eop,
34 int read_fixed,
Thomas Chouec06dd82015-10-22 15:29:11 +080035 int write_fixed_or_sop)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080036{
Thomas Chou455e9532015-11-06 09:36:26 +080037 u8 val;
Thomas Chouec06dd82015-10-22 15:29:11 +080038
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080039 /*
40 * Mark the "next" descriptor as "not" owned by hardware. This prevents
Thomas Chouec06dd82015-10-22 15:29:11 +080041 * The SGDMA controller from continuing to process the chain.
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080042 */
Thomas Chouec06dd82015-10-22 15:29:11 +080043 next->descriptor_control = next->descriptor_control &
44 ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080045
Thomas Chouec06dd82015-10-22 15:29:11 +080046 memset(desc, 0, sizeof(struct alt_sgdma_descriptor));
47 desc->source = virt_to_phys(read_addr);
48 desc->destination = virt_to_phys(write_addr);
49 desc->next = virt_to_phys(next);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080050 desc->bytes_to_transfer = length_or_eop;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080051
52 /*
53 * Set the descriptor control block as follows:
54 * - Set "owned by hardware" bit
55 * - Optionally set "generate EOP" bit
56 * - Optionally set the "read from fixed address" bit
57 * - Optionally set the "write to fixed address bit (which serves
58 * serves as a "generate SOP" control bit in memory-to-stream mode).
59 * - Set the 4-bit atlantic channel, if specified
60 *
61 * Note this step is performed after all other descriptor information
62 * has been filled out so that, if the controller already happens to be
63 * pointing at this descriptor, it will not run (via the "owned by
64 * hardware" bit) until all other descriptor has been set up.
65 */
Thomas Chouec06dd82015-10-22 15:29:11 +080066 val = ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK;
67 if (generate_eop)
68 val |= ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK;
69 if (read_fixed)
70 val |= ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK;
71 if (write_fixed_or_sop)
72 val |= ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK;
73 desc->descriptor_control = val;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080074}
75
Thomas Chouec06dd82015-10-22 15:29:11 +080076static int alt_sgdma_wait_transfer(struct alt_sgdma_registers *regs)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080077{
Thomas Chouec06dd82015-10-22 15:29:11 +080078 int status;
79 ulong ctime;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080080
Thomas Chouec06dd82015-10-22 15:29:11 +080081 /* Wait for the descriptor (chain) to complete */
82 ctime = get_timer(0);
83 while (1) {
84 status = readl(&regs->status);
85 if (!(status & ALT_SGDMA_STATUS_BUSY_MSK))
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080086 break;
Thomas Chouec06dd82015-10-22 15:29:11 +080087 if (get_timer(ctime) > ALT_TSE_SGDMA_BUSY_TIMEOUT) {
88 status = -ETIMEDOUT;
89 debug("sgdma timeout\n");
90 break;
91 }
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080092 }
93
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080094 /* Clear Run */
Thomas Chouec06dd82015-10-22 15:29:11 +080095 writel(0, &regs->control);
96 /* Clear status */
97 writel(0xff, &regs->status);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +080098
Thomas Chouec06dd82015-10-22 15:29:11 +080099 return status;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800100}
101
Thomas Chouec06dd82015-10-22 15:29:11 +0800102static int alt_sgdma_start_transfer(struct alt_sgdma_registers *regs,
103 struct alt_sgdma_descriptor *desc)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800104{
Thomas Chou455e9532015-11-06 09:36:26 +0800105 u32 val;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800106
107 /* Point the controller at the descriptor */
Thomas Chouec06dd82015-10-22 15:29:11 +0800108 writel(virt_to_phys(desc), &regs->next_descriptor_pointer);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800109
110 /*
111 * Set up SGDMA controller to:
112 * - Disable interrupt generation
113 * - Run once a valid descriptor is written to controller
114 * - Stop on an error with any particular descriptor
115 */
Thomas Chouec06dd82015-10-22 15:29:11 +0800116 val = ALT_SGDMA_CONTROL_RUN_MSK | ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK;
117 writel(val, &regs->control);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800118
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800119 return 0;
120}
121
Thomas Chouec06dd82015-10-22 15:29:11 +0800122static void tse_adjust_link(struct altera_tse_priv *priv,
123 struct phy_device *phydev)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800124{
Thomas Chouec06dd82015-10-22 15:29:11 +0800125 struct alt_tse_mac *mac_dev = priv->mac_dev;
Thomas Chou455e9532015-11-06 09:36:26 +0800126 u32 refvar;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800127
Thomas Chouec06dd82015-10-22 15:29:11 +0800128 if (!phydev->link) {
129 debug("%s: No link.\n", phydev->dev->name);
130 return;
131 }
132
133 refvar = readl(&mac_dev->command_config);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800134
Thomas Chouec06dd82015-10-22 15:29:11 +0800135 if (phydev->duplex)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800136 refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
137 else
138 refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
139
Thomas Chouec06dd82015-10-22 15:29:11 +0800140 switch (phydev->speed) {
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800141 case 1000:
142 refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
143 refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
144 break;
145 case 100:
146 refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
147 refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
148 break;
149 case 10:
150 refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
151 refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
152 break;
153 }
Thomas Chouec06dd82015-10-22 15:29:11 +0800154 writel(refvar, &mac_dev->command_config);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800155}
156
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800157static int altera_tse_send_sgdma(struct udevice *dev, void *packet, int length)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800158{
Thomas Chouec06dd82015-10-22 15:29:11 +0800159 struct altera_tse_priv *priv = dev_get_priv(dev);
160 struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800161
Thomas Chouec06dd82015-10-22 15:29:11 +0800162 alt_sgdma_construct_descriptor(
163 tx_desc,
164 tx_desc + 1,
165 packet, /* read addr */
166 NULL, /* write addr */
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800167 length, /* length or EOP ,will change for each tx */
Thomas Chouec06dd82015-10-22 15:29:11 +0800168 1, /* gen eop */
169 0, /* read fixed */
170 1 /* write fixed or sop */
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800171 );
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800172
173 /* send the packet */
Thomas Chouec06dd82015-10-22 15:29:11 +0800174 alt_sgdma_start_transfer(priv->sgdma_tx, tx_desc);
175 alt_sgdma_wait_transfer(priv->sgdma_tx);
176 debug("sent %d bytes\n", tx_desc->actual_bytes_transferred);
177
178 return tx_desc->actual_bytes_transferred;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800179}
180
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800181static int altera_tse_recv_sgdma(struct udevice *dev, int flags,
182 uchar **packetp)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800183{
Thomas Chouec06dd82015-10-22 15:29:11 +0800184 struct altera_tse_priv *priv = dev_get_priv(dev);
185 struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
186 int packet_length;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800187
Thomas Chouec06dd82015-10-22 15:29:11 +0800188 if (rx_desc->descriptor_status &
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800189 ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
Thomas Choub917dee2015-11-09 08:00:00 +0800190 alt_sgdma_wait_transfer(priv->sgdma_rx);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800191 packet_length = rx_desc->actual_bytes_transferred;
Thomas Chouec06dd82015-10-22 15:29:11 +0800192 debug("recv %d bytes\n", packet_length);
193 *packetp = priv->rx_buf;
Joachim Foerster384a8192011-10-17 05:24:46 +0000194
195 return packet_length;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800196 }
197
Thomas Chouec06dd82015-10-22 15:29:11 +0800198 return -EAGAIN;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800199}
200
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800201static int altera_tse_free_pkt_sgdma(struct udevice *dev, uchar *packet,
202 int length)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800203{
Thomas Chouec06dd82015-10-22 15:29:11 +0800204 struct altera_tse_priv *priv = dev_get_priv(dev);
205 struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
Thomas Chouec06dd82015-10-22 15:29:11 +0800206
Thomas Chouec06dd82015-10-22 15:29:11 +0800207 alt_sgdma_construct_descriptor(
208 rx_desc,
209 rx_desc + 1,
210 NULL, /* read addr */
211 priv->rx_buf, /* write addr */
212 0, /* length or EOP */
213 0, /* gen eop */
214 0, /* read fixed */
215 0 /* write fixed or sop */
216 );
217
218 /* setup the sgdma */
219 alt_sgdma_start_transfer(priv->sgdma_rx, rx_desc);
220 debug("recv setup\n");
221
222 return 0;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800223}
224
Thomas Chou67bd52f2015-11-08 10:57:05 +0800225static void altera_tse_stop_mac(struct altera_tse_priv *priv)
226{
227 struct alt_tse_mac *mac_dev = priv->mac_dev;
228 u32 status;
229 ulong ctime;
230
231 /* reset the mac */
232 writel(ALTERA_TSE_CMD_SW_RESET_MSK, &mac_dev->command_config);
233 ctime = get_timer(0);
234 while (1) {
235 status = readl(&mac_dev->command_config);
236 if (!(status & ALTERA_TSE_CMD_SW_RESET_MSK))
237 break;
238 if (get_timer(ctime) > ALT_TSE_SW_RESET_TIMEOUT) {
239 debug("Reset mac timeout\n");
240 break;
241 }
242 }
243}
244
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800245static void altera_tse_stop_sgdma(struct udevice *dev)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800246{
Thomas Chouec06dd82015-10-22 15:29:11 +0800247 struct altera_tse_priv *priv = dev_get_priv(dev);
Thomas Chouec06dd82015-10-22 15:29:11 +0800248 struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
249 struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
250 struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
Thomas Chouec06dd82015-10-22 15:29:11 +0800251 int ret;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800252
253 /* clear rx desc & wait for sgdma to complete */
254 rx_desc->descriptor_control = 0;
Thomas Chouec06dd82015-10-22 15:29:11 +0800255 writel(0, &rx_sgdma->control);
256 ret = alt_sgdma_wait_transfer(rx_sgdma);
257 if (ret == -ETIMEDOUT)
258 writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
259 &rx_sgdma->control);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800260
Thomas Chouec06dd82015-10-22 15:29:11 +0800261 writel(0, &tx_sgdma->control);
262 ret = alt_sgdma_wait_transfer(tx_sgdma);
263 if (ret == -ETIMEDOUT)
264 writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
265 &tx_sgdma->control);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800266}
267
Thomas Chou9c0a2012015-11-09 14:36:29 +0800268static void msgdma_reset(struct msgdma_csr *csr)
269{
270 u32 status;
271 ulong ctime;
272
273 /* Reset mSGDMA */
274 writel(MSGDMA_CSR_STAT_MASK, &csr->status);
275 writel(MSGDMA_CSR_CTL_RESET, &csr->control);
276 ctime = get_timer(0);
277 while (1) {
278 status = readl(&csr->status);
279 if (!(status & MSGDMA_CSR_STAT_RESETTING))
280 break;
281 if (get_timer(ctime) > ALT_TSE_SW_RESET_TIMEOUT) {
282 debug("Reset msgdma timeout\n");
283 break;
284 }
285 }
286 /* Clear status */
287 writel(MSGDMA_CSR_STAT_MASK, &csr->status);
288}
289
290static u32 msgdma_wait(struct msgdma_csr *csr)
291{
292 u32 status;
293 ulong ctime;
294
295 /* Wait for the descriptor to complete */
296 ctime = get_timer(0);
297 while (1) {
298 status = readl(&csr->status);
299 if (!(status & MSGDMA_CSR_STAT_BUSY))
300 break;
301 if (get_timer(ctime) > ALT_TSE_SGDMA_BUSY_TIMEOUT) {
302 debug("sgdma timeout\n");
303 break;
304 }
305 }
306 /* Clear status */
307 writel(MSGDMA_CSR_STAT_MASK, &csr->status);
308
309 return status;
310}
311
312static int altera_tse_send_msgdma(struct udevice *dev, void *packet,
313 int length)
314{
315 struct altera_tse_priv *priv = dev_get_priv(dev);
316 struct msgdma_extended_desc *desc = priv->tx_desc;
317 u32 tx_buf = virt_to_phys(packet);
318 u32 status;
319
320 writel(tx_buf, &desc->read_addr_lo);
321 writel(0, &desc->read_addr_hi);
322 writel(0, &desc->write_addr_lo);
323 writel(0, &desc->write_addr_hi);
324 writel(length, &desc->len);
325 writel(0, &desc->burst_seq_num);
326 writel(MSGDMA_DESC_TX_STRIDE, &desc->stride);
327 writel(MSGDMA_DESC_CTL_TX_SINGLE, &desc->control);
328 status = msgdma_wait(priv->sgdma_tx);
329 debug("sent %d bytes, status %08x\n", length, status);
330
331 return 0;
332}
333
334static int altera_tse_recv_msgdma(struct udevice *dev, int flags,
335 uchar **packetp)
336{
337 struct altera_tse_priv *priv = dev_get_priv(dev);
338 struct msgdma_csr *csr = priv->sgdma_rx;
339 struct msgdma_response *resp = priv->rx_resp;
340 u32 level, length, status;
341
342 level = readl(&csr->resp_fill_level);
343 if (level & 0xffff) {
344 length = readl(&resp->bytes_transferred);
345 status = readl(&resp->status);
346 debug("recv %d bytes, status %08x\n", length, status);
347 *packetp = priv->rx_buf;
348
349 return length;
350 }
351
352 return -EAGAIN;
353}
354
355static int altera_tse_free_pkt_msgdma(struct udevice *dev, uchar *packet,
356 int length)
357{
358 struct altera_tse_priv *priv = dev_get_priv(dev);
359 struct msgdma_extended_desc *desc = priv->rx_desc;
360 u32 rx_buf = virt_to_phys(priv->rx_buf);
361
362 writel(0, &desc->read_addr_lo);
363 writel(0, &desc->read_addr_hi);
364 writel(rx_buf, &desc->write_addr_lo);
365 writel(0, &desc->write_addr_hi);
366 writel(PKTSIZE_ALIGN, &desc->len);
367 writel(0, &desc->burst_seq_num);
368 writel(MSGDMA_DESC_RX_STRIDE, &desc->stride);
369 writel(MSGDMA_DESC_CTL_RX_SINGLE, &desc->control);
370 debug("recv setup\n");
371
372 return 0;
373}
374
375static void altera_tse_stop_msgdma(struct udevice *dev)
376{
377 struct altera_tse_priv *priv = dev_get_priv(dev);
378
379 msgdma_reset(priv->sgdma_rx);
380 msgdma_reset(priv->sgdma_tx);
381}
382
Thomas Chouec06dd82015-10-22 15:29:11 +0800383static int tse_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800384{
Thomas Chouec06dd82015-10-22 15:29:11 +0800385 struct altera_tse_priv *priv = bus->priv;
386 struct alt_tse_mac *mac_dev = priv->mac_dev;
Thomas Chou455e9532015-11-06 09:36:26 +0800387 u32 value;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800388
389 /* set mdio address */
Thomas Chouec06dd82015-10-22 15:29:11 +0800390 writel(addr, &mac_dev->mdio_phy1_addr);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800391 /* get the data */
Thomas Chouec06dd82015-10-22 15:29:11 +0800392 value = readl(&mac_dev->mdio_phy1[reg]);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800393
Thomas Chouec06dd82015-10-22 15:29:11 +0800394 return value & 0xffff;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800395}
396
Thomas Chouec06dd82015-10-22 15:29:11 +0800397static int tse_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
398 u16 val)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800399{
Thomas Chouec06dd82015-10-22 15:29:11 +0800400 struct altera_tse_priv *priv = bus->priv;
401 struct alt_tse_mac *mac_dev = priv->mac_dev;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800402
403 /* set mdio address */
Thomas Chouec06dd82015-10-22 15:29:11 +0800404 writel(addr, &mac_dev->mdio_phy1_addr);
405 /* set the data */
406 writel(val, &mac_dev->mdio_phy1[reg]);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800407
408 return 0;
409}
410
Thomas Chouec06dd82015-10-22 15:29:11 +0800411static int tse_mdio_init(const char *name, struct altera_tse_priv *priv)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800412{
Thomas Chouec06dd82015-10-22 15:29:11 +0800413 struct mii_dev *bus = mdio_alloc();
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800414
Thomas Chouec06dd82015-10-22 15:29:11 +0800415 if (!bus) {
416 printf("Failed to allocate MDIO bus\n");
417 return -ENOMEM;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800418 }
419
Thomas Chouec06dd82015-10-22 15:29:11 +0800420 bus->read = tse_mdio_read;
421 bus->write = tse_mdio_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000422 snprintf(bus->name, sizeof(bus->name), "%s", name);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800423
Thomas Chouec06dd82015-10-22 15:29:11 +0800424 bus->priv = (void *)priv;
Joachim Foerster7ce99292011-10-25 22:39:57 +0000425
Thomas Chouec06dd82015-10-22 15:29:11 +0800426 return mdio_register(bus);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800427}
428
Thomas Chouec06dd82015-10-22 15:29:11 +0800429static int tse_phy_init(struct altera_tse_priv *priv, void *dev)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800430{
Thomas Chouec06dd82015-10-22 15:29:11 +0800431 struct phy_device *phydev;
432 unsigned int mask = 0xffffffff;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800433
Thomas Chouec06dd82015-10-22 15:29:11 +0800434 if (priv->phyaddr)
435 mask = 1 << priv->phyaddr;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800436
Thomas Chouec06dd82015-10-22 15:29:11 +0800437 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
438 if (!phydev)
439 return -ENODEV;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800440
Thomas Chouec06dd82015-10-22 15:29:11 +0800441 phy_connect_dev(phydev, dev);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800442
Thomas Chouec06dd82015-10-22 15:29:11 +0800443 phydev->supported &= PHY_GBIT_FEATURES;
444 phydev->advertising = phydev->supported;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800445
Thomas Chouec06dd82015-10-22 15:29:11 +0800446 priv->phydev = phydev;
447 phy_config(phydev);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800448
Thomas Chouec06dd82015-10-22 15:29:11 +0800449 return 0;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800450}
451
Thomas Chouec06dd82015-10-22 15:29:11 +0800452static int altera_tse_write_hwaddr(struct udevice *dev)
Thomas Chou53d01502010-04-27 20:15:10 +0800453{
Thomas Chouec06dd82015-10-22 15:29:11 +0800454 struct altera_tse_priv *priv = dev_get_priv(dev);
455 struct alt_tse_mac *mac_dev = priv->mac_dev;
Simon Glassfa20e932020-12-03 16:55:20 -0700456 struct eth_pdata *pdata = dev_get_plat(dev);
Thomas Chouec06dd82015-10-22 15:29:11 +0800457 u8 *hwaddr = pdata->enetaddr;
Thomas Chou455e9532015-11-06 09:36:26 +0800458 u32 mac_lo, mac_hi;
Thomas Chou53d01502010-04-27 20:15:10 +0800459
Thomas Chouec06dd82015-10-22 15:29:11 +0800460 mac_lo = (hwaddr[3] << 24) | (hwaddr[2] << 16) |
461 (hwaddr[1] << 8) | hwaddr[0];
462 mac_hi = (hwaddr[5] << 8) | hwaddr[4];
463 debug("Set MAC address to 0x%04x%08x\n", mac_hi, mac_lo);
Thomas Chou53d01502010-04-27 20:15:10 +0800464
Thomas Chouec06dd82015-10-22 15:29:11 +0800465 writel(mac_lo, &mac_dev->mac_addr_0);
466 writel(mac_hi, &mac_dev->mac_addr_1);
467 writel(mac_lo, &mac_dev->supp_mac_addr_0_0);
468 writel(mac_hi, &mac_dev->supp_mac_addr_0_1);
469 writel(mac_lo, &mac_dev->supp_mac_addr_1_0);
470 writel(mac_hi, &mac_dev->supp_mac_addr_1_1);
471 writel(mac_lo, &mac_dev->supp_mac_addr_2_0);
472 writel(mac_hi, &mac_dev->supp_mac_addr_2_1);
473 writel(mac_lo, &mac_dev->supp_mac_addr_3_0);
474 writel(mac_hi, &mac_dev->supp_mac_addr_3_1);
Thomas Chou53d01502010-04-27 20:15:10 +0800475
Thomas Chou53d01502010-04-27 20:15:10 +0800476 return 0;
477}
478
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800479static int altera_tse_send(struct udevice *dev, void *packet, int length)
480{
481 struct altera_tse_priv *priv = dev_get_priv(dev);
482 unsigned long tx_buf = (unsigned long)packet;
483
484 flush_dcache_range(tx_buf, tx_buf + length);
485
486 return priv->ops->send(dev, packet, length);
487}
488
489static int altera_tse_recv(struct udevice *dev, int flags, uchar **packetp)
490{
491 struct altera_tse_priv *priv = dev_get_priv(dev);
492
493 return priv->ops->recv(dev, flags, packetp);
494}
495
496static int altera_tse_free_pkt(struct udevice *dev, uchar *packet,
497 int length)
498{
499 struct altera_tse_priv *priv = dev_get_priv(dev);
500 unsigned long rx_buf = (unsigned long)priv->rx_buf;
501
502 invalidate_dcache_range(rx_buf, rx_buf + PKTSIZE_ALIGN);
503
504 return priv->ops->free_pkt(dev, packet, length);
505}
506
507static void altera_tse_stop(struct udevice *dev)
508{
509 struct altera_tse_priv *priv = dev_get_priv(dev);
510
511 priv->ops->stop(dev);
512 altera_tse_stop_mac(priv);
513}
514
Thomas Chouec06dd82015-10-22 15:29:11 +0800515static int altera_tse_start(struct udevice *dev)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800516{
Thomas Chouec06dd82015-10-22 15:29:11 +0800517 struct altera_tse_priv *priv = dev_get_priv(dev);
518 struct alt_tse_mac *mac_dev = priv->mac_dev;
Thomas Chou455e9532015-11-06 09:36:26 +0800519 u32 val;
Thomas Chouec06dd82015-10-22 15:29:11 +0800520 int ret;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800521
522 /* need to create sgdma */
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800523 debug("Configuring rx desc\n");
Thomas Chouec06dd82015-10-22 15:29:11 +0800524 altera_tse_free_pkt(dev, priv->rx_buf, PKTSIZE_ALIGN);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800525 /* start TSE */
526 debug("Configuring TSE Mac\n");
527 /* Initialize MAC registers */
Thomas Chouec06dd82015-10-22 15:29:11 +0800528 writel(PKTSIZE_ALIGN, &mac_dev->max_frame_length);
529 writel(priv->rx_fifo_depth - 16, &mac_dev->rx_sel_empty_threshold);
530 writel(0, &mac_dev->rx_sel_full_threshold);
531 writel(priv->tx_fifo_depth - 16, &mac_dev->tx_sel_empty_threshold);
532 writel(0, &mac_dev->tx_sel_full_threshold);
533 writel(8, &mac_dev->rx_almost_empty_threshold);
534 writel(8, &mac_dev->rx_almost_full_threshold);
535 writel(8, &mac_dev->tx_almost_empty_threshold);
536 writel(3, &mac_dev->tx_almost_full_threshold);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800537
538 /* NO Shift */
Thomas Chouec06dd82015-10-22 15:29:11 +0800539 writel(0, &mac_dev->rx_cmd_stat);
540 writel(0, &mac_dev->tx_cmd_stat);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800541
542 /* enable MAC */
Thomas Chouec06dd82015-10-22 15:29:11 +0800543 val = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
544 writel(val, &mac_dev->command_config);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800545
Thomas Chouec06dd82015-10-22 15:29:11 +0800546 /* Start up the PHY */
547 ret = phy_startup(priv->phydev);
548 if (ret) {
549 debug("Could not initialize PHY %s\n",
550 priv->phydev->dev->name);
551 return ret;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800552 }
553
Thomas Chouec06dd82015-10-22 15:29:11 +0800554 tse_adjust_link(priv, priv->phydev);
555
556 if (!priv->phydev->link)
557 return -EIO;
558
559 return 0;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800560}
561
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800562static const struct tse_ops tse_sgdma_ops = {
563 .send = altera_tse_send_sgdma,
564 .recv = altera_tse_recv_sgdma,
565 .free_pkt = altera_tse_free_pkt_sgdma,
566 .stop = altera_tse_stop_sgdma,
567};
568
Thomas Chou9c0a2012015-11-09 14:36:29 +0800569static const struct tse_ops tse_msgdma_ops = {
570 .send = altera_tse_send_msgdma,
571 .recv = altera_tse_recv_msgdma,
572 .free_pkt = altera_tse_free_pkt_msgdma,
573 .stop = altera_tse_stop_msgdma,
574};
575
Thomas Chouec06dd82015-10-22 15:29:11 +0800576static int altera_tse_probe(struct udevice *dev)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800577{
Simon Glassfa20e932020-12-03 16:55:20 -0700578 struct eth_pdata *pdata = dev_get_plat(dev);
Thomas Chouec06dd82015-10-22 15:29:11 +0800579 struct altera_tse_priv *priv = dev_get_priv(dev);
Thomas Choudde1ffb2015-11-06 09:37:17 +0800580 void *blob = (void *)gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700581 int node = dev_of_offset(dev);
Thomas Chouec06dd82015-10-22 15:29:11 +0800582 const char *list, *end;
583 const fdt32_t *cell;
584 void *base, *desc_mem = NULL;
585 unsigned long addr, size;
Thomas Choudde1ffb2015-11-06 09:37:17 +0800586 int parent, addrc, sizec;
Thomas Chouec06dd82015-10-22 15:29:11 +0800587 int len, idx;
588 int ret;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800589
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800590 priv->dma_type = dev_get_driver_data(dev);
591 if (priv->dma_type == ALT_SGDMA)
592 priv->ops = &tse_sgdma_ops;
Thomas Chou9c0a2012015-11-09 14:36:29 +0800593 else
594 priv->ops = &tse_msgdma_ops;
Thomas Chouec06dd82015-10-22 15:29:11 +0800595 /*
Thomas Choudde1ffb2015-11-06 09:37:17 +0800596 * decode regs. there are multiple reg tuples, and they need to
597 * match with reg-names.
Thomas Chouec06dd82015-10-22 15:29:11 +0800598 */
Thomas Choudde1ffb2015-11-06 09:37:17 +0800599 parent = fdt_parent_offset(blob, node);
Simon Glassbb7c01e2017-05-18 20:09:26 -0600600 fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
Thomas Chouec06dd82015-10-22 15:29:11 +0800601 list = fdt_getprop(blob, node, "reg-names", &len);
602 if (!list)
603 return -ENOENT;
604 end = list + len;
605 cell = fdt_getprop(blob, node, "reg", &len);
606 if (!cell)
607 return -ENOENT;
608 idx = 0;
609 while (list < end) {
610 addr = fdt_translate_address((void *)blob,
611 node, cell + idx);
Thomas Choudde1ffb2015-11-06 09:37:17 +0800612 size = fdt_addr_to_cpu(cell[idx + addrc]);
Thomas Choufb468332015-11-14 11:21:16 +0800613 base = map_physmem(addr, size, MAP_NOCACHE);
Thomas Chouec06dd82015-10-22 15:29:11 +0800614 len = strlen(list);
615 if (strcmp(list, "control_port") == 0)
616 priv->mac_dev = base;
617 else if (strcmp(list, "rx_csr") == 0)
618 priv->sgdma_rx = base;
Thomas Chou9c0a2012015-11-09 14:36:29 +0800619 else if (strcmp(list, "rx_desc") == 0)
620 priv->rx_desc = base;
621 else if (strcmp(list, "rx_resp") == 0)
622 priv->rx_resp = base;
Thomas Chouec06dd82015-10-22 15:29:11 +0800623 else if (strcmp(list, "tx_csr") == 0)
624 priv->sgdma_tx = base;
Thomas Chou9c0a2012015-11-09 14:36:29 +0800625 else if (strcmp(list, "tx_desc") == 0)
626 priv->tx_desc = base;
Thomas Chouec06dd82015-10-22 15:29:11 +0800627 else if (strcmp(list, "s1") == 0)
628 desc_mem = base;
Thomas Choudde1ffb2015-11-06 09:37:17 +0800629 idx += addrc + sizec;
Thomas Chouec06dd82015-10-22 15:29:11 +0800630 list += (len + 1);
631 }
632 /* decode fifo depth */
633 priv->rx_fifo_depth = fdtdec_get_int(blob, node,
634 "rx-fifo-depth", 0);
635 priv->tx_fifo_depth = fdtdec_get_int(blob, node,
636 "tx-fifo-depth", 0);
637 /* decode phy */
638 addr = fdtdec_get_int(blob, node,
639 "phy-handle", 0);
640 addr = fdt_node_offset_by_phandle(blob, addr);
641 priv->phyaddr = fdtdec_get_int(blob, addr,
642 "reg", 0);
643 /* init desc */
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800644 if (priv->dma_type == ALT_SGDMA) {
645 len = sizeof(struct alt_sgdma_descriptor) * 4;
646 if (!desc_mem) {
647 desc_mem = dma_alloc_coherent(len, &addr);
648 if (!desc_mem)
649 return -ENOMEM;
650 }
651 memset(desc_mem, 0, len);
652 priv->tx_desc = desc_mem;
653 priv->rx_desc = priv->tx_desc +
654 2 * sizeof(struct alt_sgdma_descriptor);
Thomas Chouec06dd82015-10-22 15:29:11 +0800655 }
Thomas Chouec06dd82015-10-22 15:29:11 +0800656 /* allocate recv packet buffer */
657 priv->rx_buf = malloc_cache_aligned(PKTSIZE_ALIGN);
658 if (!priv->rx_buf)
659 return -ENOMEM;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800660
Thomas Chouec06dd82015-10-22 15:29:11 +0800661 /* stop controller */
662 debug("Reset TSE & SGDMAs\n");
663 altera_tse_stop(dev);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800664
Thomas Chouec06dd82015-10-22 15:29:11 +0800665 /* start the phy */
666 priv->interface = pdata->phy_interface;
667 tse_mdio_init(dev->name, priv);
668 priv->bus = miiphy_get_dev_by_name(dev->name);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800669
Thomas Chouec06dd82015-10-22 15:29:11 +0800670 ret = tse_phy_init(priv, dev);
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800671
Thomas Chouec06dd82015-10-22 15:29:11 +0800672 return ret;
673}
Joachim Foerstercb0ddaf2011-10-17 05:24:44 +0000674
Thomas Chouec06dd82015-10-22 15:29:11 +0800675static int altera_tse_ofdata_to_platdata(struct udevice *dev)
676{
Simon Glassfa20e932020-12-03 16:55:20 -0700677 struct eth_pdata *pdata = dev_get_plat(dev);
Thomas Chouec06dd82015-10-22 15:29:11 +0800678 const char *phy_mode;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800679
Thomas Chouec06dd82015-10-22 15:29:11 +0800680 pdata->phy_interface = -1;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700681 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
682 NULL);
Thomas Chouec06dd82015-10-22 15:29:11 +0800683 if (phy_mode)
684 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
685 if (pdata->phy_interface == -1) {
686 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
687 return -EINVAL;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800688 }
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800689
Thomas Chouec06dd82015-10-22 15:29:11 +0800690 return 0;
691}
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800692
Thomas Chouec06dd82015-10-22 15:29:11 +0800693static const struct eth_ops altera_tse_ops = {
694 .start = altera_tse_start,
695 .send = altera_tse_send,
696 .recv = altera_tse_recv,
697 .free_pkt = altera_tse_free_pkt,
698 .stop = altera_tse_stop,
699 .write_hwaddr = altera_tse_write_hwaddr,
700};
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800701
Thomas Chouec06dd82015-10-22 15:29:11 +0800702static const struct udevice_id altera_tse_ids[] = {
Thomas Chou9c0a2012015-11-09 14:36:29 +0800703 { .compatible = "altr,tse-msgdma-1.0", .data = ALT_MSGDMA },
Thomas Chou9f9d66a2015-11-09 11:02:15 +0800704 { .compatible = "altr,tse-1.0", .data = ALT_SGDMA },
705 {}
Thomas Chouec06dd82015-10-22 15:29:11 +0800706};
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800707
Thomas Chouec06dd82015-10-22 15:29:11 +0800708U_BOOT_DRIVER(altera_tse) = {
709 .name = "altera_tse",
710 .id = UCLASS_ETH,
711 .of_match = altera_tse_ids,
712 .ops = &altera_tse_ops,
713 .ofdata_to_platdata = altera_tse_ofdata_to_platdata,
Simon Glass71fa5b42020-12-03 16:55:18 -0700714 .plat_auto = sizeof(struct eth_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700715 .priv_auto = sizeof(struct altera_tse_priv),
Thomas Chouec06dd82015-10-22 15:29:11 +0800716 .probe = altera_tse_probe,
717};