]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/cpsw.c
Merge remote branch 'remotes/kc/karo-tx6q' into karo-tx48
[karo-tx-uboot.git] / drivers / net / cpsw.c
1 /*
2  * CPSW Ethernet Switch Driver
3  *
4  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  */
15
16 #include <common.h>
17 #include <command.h>
18 #include <net.h>
19 #include <miiphy.h>
20 #include <malloc.h>
21 #include <net.h>
22 #include <netdev.h>
23 #include <asm/errno.h>
24 #include <asm/io.h>
25 #include <phy.h>
26 #include <asm/arch/cpu.h>
27
28 #define BITMASK(bits)           (BIT(bits) - 1)
29 #define PHY_REG_MASK            0x1f
30 #define PHY_ID_MASK             0x1f
31 #define NUM_DESCS               (PKTBUFSRX * 2)
32 #define PKT_MIN                 60
33 #define PKT_MAX                 (1500 + 14 + 4 + 4)
34 #define CLEAR_BIT               1
35 #define GIGABITEN               BIT(7)
36 #define FULLDUPLEXEN            BIT(0)
37 #define MIIEN                   BIT(15)
38
39 /* DMA Registers */
40 #define CPDMA_TXCONTROL         0x004
41 #define CPDMA_RXCONTROL         0x014
42 #define CPDMA_SOFTRESET         0x01c
43 #define CPDMA_RXFREE            0x0e0
44 #define CPDMA_TXHDP_VER1        0x100
45 #define CPDMA_TXHDP_VER2        0x200
46 #define CPDMA_RXHDP_VER1        0x120
47 #define CPDMA_RXHDP_VER2        0x220
48 #define CPDMA_TXCP_VER1         0x140
49 #define CPDMA_TXCP_VER2         0x240
50 #define CPDMA_RXCP_VER1         0x160
51 #define CPDMA_RXCP_VER2         0x260
52
53 #define CPDMA_RAM_ADDR          0x4a102000
54
55 /* Descriptor mode bits */
56 #define CPDMA_DESC_SOP          BIT(31)
57 #define CPDMA_DESC_EOP          BIT(30)
58 #define CPDMA_DESC_OWNER        BIT(29)
59 #define CPDMA_DESC_EOQ          BIT(28)
60
61 /*
62  * This timeout definition is a worst-case ultra defensive measure against
63  * unexpected controller lock ups.  Ideally, we should never ever hit this
64  * scenario in practice.
65  */
66 #define MDIO_TIMEOUT            100 /* msecs */
67 #define CPDMA_TIMEOUT           100 /* msecs */
68
69 struct cpsw_mdio_regs {
70         u32     version;
71         u32     control;
72 #define CONTROL_IDLE            BIT(31)
73 #define CONTROL_ENABLE          BIT(30)
74
75         u32     alive;
76         u32     link;
77         u32     linkintraw;
78         u32     linkintmasked;
79         u32     __reserved_0[2];
80         u32     userintraw;
81         u32     userintmasked;
82         u32     userintmaskset;
83         u32     userintmaskclr;
84         u32     __reserved_1[20];
85
86         struct {
87                 u32             access;
88                 u32             physel;
89 #define USERACCESS_GO           BIT(31)
90 #define USERACCESS_WRITE        BIT(30)
91 #define USERACCESS_ACK          BIT(29)
92 #define USERACCESS_READ         0
93 #define USERACCESS_DATA         0xffff
94         } user[0];
95 };
96
97 struct cpsw_regs {
98         u32     id_ver;
99         u32     control;
100         u32     soft_reset;
101         u32     stat_port_en;
102         u32     ptype;
103 };
104
105 struct cpsw_slave_regs {
106         u32     max_blks;
107         u32     blk_cnt;
108         u32     flow_thresh;
109         u32     port_vlan;
110         u32     tx_pri_map;
111         u32     gap_thresh;
112         u32     sa_lo;
113         u32     sa_hi;
114 };
115
116 struct cpsw_host_regs {
117         u32     max_blks;
118         u32     blk_cnt;
119         u32     flow_thresh;
120         u32     port_vlan;
121         u32     tx_pri_map;
122         u32     cpdma_tx_pri_map;
123         u32     cpdma_rx_chan_map;
124 };
125
126 struct cpsw_sliver_regs {
127         u32     id_ver;
128         u32     mac_control;
129         u32     mac_status;
130         u32     soft_reset;
131         u32     rx_maxlen;
132         u32     __reserved_0;
133         u32     rx_pause;
134         u32     tx_pause;
135         u32     __reserved_1;
136         u32     rx_pri_map;
137 };
138
139 #define ALE_ENTRY_BITS          68
140 #define ALE_ENTRY_WORDS         DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
141
142 /* ALE Registers */
143 #define ALE_CONTROL             0x08
144 #define ALE_UNKNOWNVLAN         0x18
145 #define ALE_TABLE_CONTROL       0x20
146 #define ALE_TABLE               0x34
147 #define ALE_PORTCTL             0x40
148
149 #define ALE_TABLE_WRITE         BIT(31)
150
151 #define ALE_TYPE_FREE                   0
152 #define ALE_TYPE_ADDR                   1
153 #define ALE_TYPE_VLAN                   2
154 #define ALE_TYPE_VLAN_ADDR              3
155
156 #define ALE_UCAST_PERSISTANT            0
157 #define ALE_UCAST_UNTOUCHED             1
158 #define ALE_UCAST_OUI                   2
159 #define ALE_UCAST_TOUCHED               3
160
161 #define ALE_MCAST_FWD                   0
162 #define ALE_MCAST_BLOCK_LEARN_FWD       1
163 #define ALE_MCAST_FWD_LEARN             2
164 #define ALE_MCAST_FWD_2                 3
165
166 enum cpsw_ale_port_state {
167         ALE_PORT_STATE_DISABLE  = 0x00,
168         ALE_PORT_STATE_BLOCK    = 0x01,
169         ALE_PORT_STATE_LEARN    = 0x02,
170         ALE_PORT_STATE_FORWARD  = 0x03,
171 };
172
173 /* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
174 #define ALE_SECURE      1
175 #define ALE_BLOCKED     2
176
177 struct cpsw_slave {
178         struct cpsw_slave_regs          *regs;
179         struct cpsw_sliver_regs         *sliver;
180         int                             slave_num;
181         u32                             mac_control;
182         struct cpsw_slave_data          *data;
183 };
184
185 struct cpdma_desc {
186         /* hardware fields */
187         u32                     hw_next;
188         u32                     hw_buffer;
189         u32                     hw_len;
190         u32                     hw_mode;
191 } __attribute__((aligned(CONFIG_SYS_CACHELINE_SIZE)));
192
193 struct cpsw_desc {
194         void *sw_buffer;
195         struct cpsw_desc *next;
196         struct cpdma_desc *dma_desc;
197 };
198
199 struct cpdma_chan {
200         struct cpsw_desc        *head, *tail;
201         void                    *hdp, *cp, *rxfree;
202 };
203
204 #define desc_write(desc, fld, val)      __raw_writel((u32)(val), &(desc)->dma_desc->fld)
205 #define desc_read(desc, fld)            __raw_readl(&(desc)->dma_desc->fld)
206 #define desc_read_ptr(desc, fld)        ((void *)__raw_readl(&(desc)->dma_desc->fld))
207
208 #define chan_write(chan, fld, val)      __raw_writel((u32)(val), (chan)->fld)
209 #define chan_read(chan, fld)            __raw_readl((chan)->fld)
210 #define chan_read_ptr(chan, fld)        ((void *)__raw_readl((chan)->fld))
211
212 #define for_each_slave(slave, priv) \
213         for (slave = (priv)->slaves; slave != (priv)->slaves + \
214                                 (priv)->data->slaves; slave++)
215
216 struct cpsw_priv {
217         struct eth_device               *dev;
218         struct cpsw_platform_data       *data;
219         int                             host_port;
220
221         struct cpsw_regs                *regs;
222         void                            *dma_regs;
223         struct cpsw_host_regs           *host_port_regs;
224         void                            *ale_regs;
225
226         struct cpsw_desc                descs[NUM_DESCS];
227         struct cpsw_desc                *desc_free;
228         struct cpdma_chan               rx_chan, tx_chan;
229
230         struct cpsw_slave               *slaves;
231         struct phy_device               *phydev;
232         struct mii_dev                  *bus;
233 };
234
235 static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
236 {
237         int idx;
238
239         idx    = start / 32;
240         start -= idx * 32;
241         idx    = 2 - idx; /* flip */
242         return (ale_entry[idx] >> start) & BITMASK(bits);
243 }
244
245 static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
246                                       u32 value)
247 {
248         int idx;
249
250         value &= BITMASK(bits);
251         idx    = start / 32;
252         start -= idx * 32;
253         idx    = 2 - idx; /* flip */
254         ale_entry[idx] &= ~(BITMASK(bits) << start);
255         ale_entry[idx] |=  (value << start);
256 }
257
258 #define DEFINE_ALE_FIELD(name, start, bits)                             \
259 static inline int cpsw_ale_get_##name(u32 *ale_entry)                   \
260 {                                                                       \
261         return cpsw_ale_get_field(ale_entry, start, bits);              \
262 }                                                                       \
263 static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value)       \
264 {                                                                       \
265         cpsw_ale_set_field(ale_entry, start, bits, value);              \
266 }
267
268 DEFINE_ALE_FIELD(entry_type,            60,     2)
269 DEFINE_ALE_FIELD(mcast_state,           62,     2)
270 DEFINE_ALE_FIELD(port_mask,             66,     3)
271 DEFINE_ALE_FIELD(ucast_type,            62,     2)
272 DEFINE_ALE_FIELD(port_num,              66,     2)
273 DEFINE_ALE_FIELD(blocked,               65,     1)
274 DEFINE_ALE_FIELD(secure,                64,     1)
275 DEFINE_ALE_FIELD(mcast,                 40,     1)
276
277 /* The MAC address field in the ALE entry cannot be macroized as above */
278 static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
279 {
280         int i;
281
282         for (i = 0; i < 6; i++)
283                 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
284 }
285
286 static inline void cpsw_ale_set_addr(u32 *ale_entry, u8 *addr)
287 {
288         int i;
289
290         for (i = 0; i < 6; i++)
291                 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
292 }
293
294 static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
295 {
296         int i;
297
298         __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
299
300         for (i = 0; i < ALE_ENTRY_WORDS; i++)
301                 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
302
303         return idx;
304 }
305
306 static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
307 {
308         int i;
309
310         for (i = 0; i < ALE_ENTRY_WORDS; i++)
311                 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
312
313         __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
314
315         return idx;
316 }
317
318 static int cpsw_ale_match_addr(struct cpsw_priv *priv, u8* addr)
319 {
320         u32 ale_entry[ALE_ENTRY_WORDS];
321         int type, idx;
322
323         for (idx = 0; idx < priv->data->ale_entries; idx++) {
324                 u8 entry_addr[6];
325
326                 cpsw_ale_read(priv, idx, ale_entry);
327                 type = cpsw_ale_get_entry_type(ale_entry);
328                 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
329                         continue;
330                 cpsw_ale_get_addr(ale_entry, entry_addr);
331                 if (memcmp(entry_addr, addr, 6) == 0)
332                         return idx;
333         }
334         return -ENOENT;
335 }
336
337 static int cpsw_ale_match_free(struct cpsw_priv *priv)
338 {
339         u32 ale_entry[ALE_ENTRY_WORDS];
340         int type, idx;
341
342         for (idx = 0; idx < priv->data->ale_entries; idx++) {
343                 cpsw_ale_read(priv, idx, ale_entry);
344                 type = cpsw_ale_get_entry_type(ale_entry);
345                 if (type == ALE_TYPE_FREE)
346                         return idx;
347         }
348         return -ENOENT;
349 }
350
351 static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
352 {
353         u32 ale_entry[ALE_ENTRY_WORDS];
354         int type, idx;
355
356         for (idx = 0; idx < priv->data->ale_entries; idx++) {
357                 cpsw_ale_read(priv, idx, ale_entry);
358                 type = cpsw_ale_get_entry_type(ale_entry);
359                 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
360                         continue;
361                 if (cpsw_ale_get_mcast(ale_entry))
362                         continue;
363                 type = cpsw_ale_get_ucast_type(ale_entry);
364                 if (type != ALE_UCAST_PERSISTANT &&
365                     type != ALE_UCAST_OUI)
366                         return idx;
367         }
368         return -ENOENT;
369 }
370
371 static int cpsw_ale_add_ucast(struct cpsw_priv *priv, u8 *addr,
372                               int port, int flags)
373 {
374         u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
375         int idx;
376
377         cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
378         cpsw_ale_set_addr(ale_entry, addr);
379         cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
380         cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
381         cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
382         cpsw_ale_set_port_num(ale_entry, port);
383
384         idx = cpsw_ale_match_addr(priv, addr);
385         if (idx < 0)
386                 idx = cpsw_ale_match_free(priv);
387         if (idx < 0)
388                 idx = cpsw_ale_find_ageable(priv);
389         if (idx < 0)
390                 return -ENOMEM;
391
392         cpsw_ale_write(priv, idx, ale_entry);
393         return 0;
394 }
395
396 static int cpsw_ale_add_mcast(struct cpsw_priv *priv, u8 *addr, int port_mask)
397 {
398         u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
399         int idx, mask;
400
401         idx = cpsw_ale_match_addr(priv, addr);
402         if (idx >= 0)
403                 cpsw_ale_read(priv, idx, ale_entry);
404
405         cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
406         cpsw_ale_set_addr(ale_entry, addr);
407         cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
408
409         mask = cpsw_ale_get_port_mask(ale_entry);
410         port_mask |= mask;
411         cpsw_ale_set_port_mask(ale_entry, port_mask);
412
413         if (idx < 0)
414                 idx = cpsw_ale_match_free(priv);
415         if (idx < 0)
416                 idx = cpsw_ale_find_ageable(priv);
417         if (idx < 0)
418                 return -ENOMEM;
419
420         cpsw_ale_write(priv, idx, ale_entry);
421         return 0;
422 }
423
424 static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
425 {
426         u32 tmp, mask = BIT(bit);
427
428         tmp  = __raw_readl(priv->ale_regs + ALE_CONTROL);
429         tmp &= ~mask;
430         tmp |= val ? mask : 0;
431         __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
432 }
433
434 #define cpsw_ale_enable(priv, val)      cpsw_ale_control(priv, 31, val)
435 #define cpsw_ale_clear(priv, val)       cpsw_ale_control(priv, 30, val)
436 #define cpsw_ale_vlan_aware(priv, val)  cpsw_ale_control(priv,  2, val)
437
438 static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
439                                        int val)
440 {
441         int offset = ALE_PORTCTL + 4 * port;
442         u32 tmp, mask = 0x3;
443
444         tmp  = __raw_readl(priv->ale_regs + offset);
445         tmp &= ~mask;
446         tmp |= val & mask;
447         __raw_writel(tmp, priv->ale_regs + offset);
448 }
449
450 static struct cpsw_mdio_regs *mdio_regs;
451
452 /* wait until hardware is ready for another user access */
453 static inline u32 wait_for_user_access(void)
454 {
455         int timeout = MDIO_TIMEOUT;
456         u32 reg;
457
458         while ((reg = __raw_readl(&mdio_regs->user[0].access)) & USERACCESS_GO) {
459                 udelay(1000);
460                 if (--timeout <= 0) {
461                         printf("TIMEOUT waiting for USERACCESS_GO\n");
462                         break;
463                 }
464         }
465
466         return reg;
467 }
468
469 /* wait until hardware state machine is idle */
470 static inline void wait_for_idle(void)
471 {
472         int timeout = MDIO_TIMEOUT;
473
474         while ((__raw_readl(&mdio_regs->control) & CONTROL_IDLE) == 0) {
475                 if (--timeout <= 0) {
476                         printf("TIMEOUT waiting for state machine idle\n");
477                         break;
478                 }
479                 udelay(1000);
480         }
481 }
482
483 static int cpsw_mdio_read(struct mii_dev *bus, int phy_id,
484                                 int dev_addr, int phy_reg)
485 {
486         unsigned short data;
487         u32 reg;
488
489         if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
490                 return -EINVAL;
491
492         if (wait_for_user_access() & USERACCESS_GO)
493                 /* promote error from previous access */
494                 return -ETIME;
495
496         reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
497                (phy_id << 16));
498         __raw_writel(reg, &mdio_regs->user[0].access);
499         reg = wait_for_user_access();
500         if (reg & USERACCESS_GO)
501                 return -ETIME;
502
503         data = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -1;
504         return data;
505 }
506
507 static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
508                                 int phy_reg, u16 data)
509 {
510         u32 reg;
511
512         if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
513                 return -EINVAL;
514
515         if (wait_for_user_access() & USERACCESS_GO)
516                 /* promote error from previous access */
517                 return -ETIME;
518
519         reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
520                    (phy_id << 16) | (data & USERACCESS_DATA));
521         __raw_writel(reg, &mdio_regs->user[0].access);
522         if (wait_for_user_access() & USERACCESS_GO)
523                 return -ETIME;
524
525         return 0;
526 }
527
528 static void cpsw_mdio_init(char *name, u32 mdio_base, u32 div)
529 {
530         struct mii_dev *bus = mdio_alloc();
531
532         mdio_regs = (struct cpsw_mdio_regs *)mdio_base;
533
534         /* set enable and clock divider */
535         __raw_writel(div | CONTROL_ENABLE, &mdio_regs->control);
536
537         /*
538          * wait for scan logic to settle:
539          * the scan time consists of (a) a large fixed component, and (b) a
540          * small component that varies with the mii bus frequency.  These
541          * were estimated using measurements at 1.1 and 2.2 MHz on tnetv107x
542          * silicon.  Since the effect of (b) was found to be largely
543          * negligible, we keep things simple here.
544          */
545         udelay(1000);
546
547         bus->read = cpsw_mdio_read;
548         bus->write = cpsw_mdio_write;
549         sprintf(bus->name, name);
550
551         mdio_register(bus);
552 }
553
554 /* Set a self-clearing bit in a register, and wait for it to clear */
555 static inline void setbit_and_wait_for_clear32(void *addr)
556 {
557         int loops = 0;
558
559         __raw_writel(CLEAR_BIT, addr);
560         while (__raw_readl(addr) & CLEAR_BIT)
561                 loops++;
562         debug("%s: reset finished after %u loops\n", __func__, loops);
563 }
564
565 #define mac_hi(mac)     (((mac)[0] << 0) | ((mac)[1] << 8) |    \
566                          ((mac)[2] << 16) | ((mac)[3] << 24))
567 #define mac_lo(mac)     (((mac)[4] << 0) | ((mac)[5] << 8))
568
569 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
570                                struct cpsw_priv *priv)
571 {
572         __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
573         __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
574 }
575
576 #define NUM_TRIES 50
577 static void cpsw_slave_update_link(struct cpsw_slave *slave,
578                                    struct cpsw_priv *priv, int *link)
579 {
580         struct phy_device *phy = priv->phydev;
581         u32 mac_control = 0;
582         int retries = NUM_TRIES;
583
584         do {
585                 phy_startup(phy);
586                 *link = phy->link;
587
588                 if (*link) { /* link up */
589                         mac_control = priv->data->mac_control;
590                         if (phy->speed == 1000)
591                                 mac_control |= GIGABITEN;
592                         if (phy->duplex == DUPLEX_FULL)
593                                 mac_control |= FULLDUPLEXEN;
594                         if (phy->speed == 100)
595                                 mac_control |= MIIEN;
596                 } else {
597                         udelay(10000);
598                 }
599         } while (!*link && retries-- > 0);
600         debug("%s: mac_control: %08x -> %08x after %u loops\n", __func__,
601                 slave->mac_control, mac_control, NUM_TRIES - retries);
602
603         if (mac_control == slave->mac_control)
604                 return;
605
606         if (mac_control) {
607                 printf("link up on port %d, speed %d, %s duplex\n",
608                                 slave->slave_num, phy->speed,
609                                 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
610         } else {
611                 printf("link down on port %d\n", slave->slave_num);
612         }
613
614         __raw_writel(mac_control, &slave->sliver->mac_control);
615         slave->mac_control = mac_control;
616 }
617
618 static int cpsw_update_link(struct cpsw_priv *priv)
619 {
620         int link = 0;
621         struct cpsw_slave *slave;
622
623         for_each_slave(slave, priv)
624                 cpsw_slave_update_link(slave, priv, &link);
625
626         return link;
627 }
628
629 static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
630 {
631         if (priv->host_port == 0)
632                 return slave_num + 1;
633         else
634                 return slave_num;
635 }
636
637 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
638 {
639         u32     slave_port;
640
641         debug("%s\n", __func__);
642         setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
643
644         /* setup priority mapping */
645         __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
646         __raw_writel(0x33221100, &slave->regs->tx_pri_map);
647
648         /* setup max packet size, and mac address */
649         __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
650         cpsw_set_slave_mac(slave, priv);
651
652         slave->mac_control = 0; /* no link yet */
653
654         /* enable forwarding */
655         slave_port = cpsw_get_slave_port(priv, slave->slave_num);
656         cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
657
658         cpsw_ale_add_mcast(priv, NetBcastAddr, 1 << slave_port);
659 }
660
661 static void cpdma_desc_get(struct cpsw_desc *desc)
662 {
663         invalidate_dcache_range((u32)desc->dma_desc, (u32)(&desc->dma_desc[1]));
664 }
665
666 static void cpdma_desc_put(struct cpsw_desc *desc)
667 {
668         flush_dcache_range((u32)desc->dma_desc, (u32)(&desc->dma_desc[1]));
669 }
670
671 static struct cpsw_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
672 {
673         struct cpsw_desc *desc = priv->desc_free;
674
675         if (desc) {
676                 cpdma_desc_get(desc);
677                 priv->desc_free = desc->next;
678         }
679         return desc;
680 }
681
682 static void cpdma_desc_free(struct cpsw_priv *priv, struct cpsw_desc *desc)
683 {
684         if (desc) {
685                 desc_write(desc, hw_next, priv->desc_free->dma_desc);
686                 cpdma_desc_put(desc);
687                 desc->next = priv->desc_free;
688                 priv->desc_free = desc;
689         }
690 }
691
692 static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
693                         void *buffer, int len)
694 {
695         struct cpsw_desc *desc, *prev;
696         u32 mode;
697
698         if (!buffer) {
699                 printf("ERROR: %s() NULL buffer\n", __func__);
700                 return -EINVAL;
701         }
702
703         flush_dcache_range((u32)buffer, (u32)buffer + len);
704
705         desc = cpdma_desc_alloc(priv);
706         if (!desc)
707                 return -ENOMEM;
708
709         debug("%s@%d: %cX desc %p DMA %p\n", __func__, __LINE__,
710                 chan == &priv->rx_chan ? 'R' : 'T', desc, desc->dma_desc);
711         if (len < PKT_MIN)
712                 len = PKT_MIN;
713
714         mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
715
716         desc->next = NULL;
717         desc_write(desc, hw_next,   0);
718         desc_write(desc, hw_buffer, buffer);
719         desc_write(desc, hw_len,    len);
720         desc_write(desc, hw_mode,   mode | len);
721
722         desc->sw_buffer = buffer;
723
724         cpdma_desc_put(desc);
725         if (!chan->head) {
726                 /* simple case - first packet enqueued */
727                 chan->head = desc;
728                 chan->tail = desc;
729                 chan_write(chan, hdp, desc->dma_desc);
730                 goto done;
731         }
732
733         /* not the first packet - enqueue at the tail */
734         prev = chan->tail;
735
736         prev->next = desc;
737         cpdma_desc_get(prev);
738         desc_write(prev, hw_next, desc->dma_desc);
739         cpdma_desc_put(prev);
740
741         chan->tail = desc;
742
743         /* next check if EOQ has been triggered already */
744         if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
745                 chan_write(chan, hdp, desc->dma_desc);
746
747 done:
748         if (chan->rxfree)
749                 chan_write(chan, rxfree, 1);
750         debug("%s@%d\n", __func__, __LINE__);
751         return 0;
752 }
753
754 static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
755                          void **buffer, int *len)
756 {
757         struct cpsw_desc *desc = chan->head;
758         u32 status;
759
760         if (!desc)
761                 return -ENOENT;
762
763         cpdma_desc_get(desc);
764
765         status = desc_read(desc, hw_mode);
766         if (status & CPDMA_DESC_OWNER)
767                 return -EBUSY;
768
769         if (len)
770                 *len = status & 0x7ff;
771
772         if (buffer)
773                 *buffer = desc->sw_buffer;
774         debug("%s@%d: buffer=%p\n", __func__, __LINE__, desc->sw_buffer);
775
776         chan->head = desc->next;
777         chan_write(chan, cp, desc->dma_desc);
778
779         cpdma_desc_free(priv, desc);
780         return 0;
781 }
782
783 static int cpsw_init(struct eth_device *dev, bd_t *bis)
784 {
785         struct cpsw_priv        *priv = dev->priv;
786         struct cpsw_slave       *slave;
787         int i, ret;
788
789         debug("%s\n", __func__);
790         /* soft reset the controller and initialize priv */
791         setbit_and_wait_for_clear32(&priv->regs->soft_reset);
792
793         /* initialize and reset the address lookup engine */
794         cpsw_ale_enable(priv, 1);
795         cpsw_ale_clear(priv, 1);
796         cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
797
798         /* setup host port priority mapping */
799         __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
800         __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
801
802         /* disable priority elevation and enable statistics on all ports */
803         __raw_writel(0, &priv->regs->ptype);
804
805         /* enable statistics collection only on the host port */
806         __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
807
808         cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
809
810         cpsw_ale_add_ucast(priv, priv->dev->enetaddr, priv->host_port,
811                            ALE_SECURE);
812         cpsw_ale_add_mcast(priv, NetBcastAddr, 1 << priv->host_port);
813
814         for_each_slave(slave, priv)
815                 cpsw_slave_init(slave, priv);
816
817         cpsw_update_link(priv);
818
819         /* init descriptor pool */
820         for (i = 0; i < NUM_DESCS; i++) {
821                 struct cpsw_desc *next_desc = (i < (NUM_DESCS - 1)) ?
822                         &priv->descs[i + 1] : NULL;
823
824                 priv->descs[i].next = next_desc;
825                 desc_write(&priv->descs[i], hw_next,
826                         next_desc ? next_desc->dma_desc : 0);
827                 cpdma_desc_put(&priv->descs[i]);
828         }
829         priv->desc_free = &priv->descs[0];
830
831         /* initialize channels */
832         if (priv->data->version == CPSW_CTRL_VERSION_2) {
833                 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
834                 priv->rx_chan.hdp       = priv->dma_regs + CPDMA_RXHDP_VER2;
835                 priv->rx_chan.cp        = priv->dma_regs + CPDMA_RXCP_VER2;
836                 priv->rx_chan.rxfree    = priv->dma_regs + CPDMA_RXFREE;
837
838                 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
839                 priv->tx_chan.hdp       = priv->dma_regs + CPDMA_TXHDP_VER2;
840                 priv->tx_chan.cp        = priv->dma_regs + CPDMA_TXCP_VER2;
841         } else {
842                 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
843                 priv->rx_chan.hdp       = priv->dma_regs + CPDMA_RXHDP_VER1;
844                 priv->rx_chan.cp        = priv->dma_regs + CPDMA_RXCP_VER1;
845                 priv->rx_chan.rxfree    = priv->dma_regs + CPDMA_RXFREE;
846
847                 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
848                 priv->tx_chan.hdp       = priv->dma_regs + CPDMA_TXHDP_VER1;
849                 priv->tx_chan.cp        = priv->dma_regs + CPDMA_TXCP_VER1;
850         }
851
852         /* clear dma state */
853         setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
854
855         if (priv->data->version == CPSW_CTRL_VERSION_2) {
856                 for (i = 0; i < priv->data->channels; i++) {
857                         __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4 * i);
858                         __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4 * i);
859                         __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4 * i);
860                         __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4 * i);
861                         __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4 * i);
862                 }
863         } else {
864                 for (i = 0; i < priv->data->channels; i++) {
865                         __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4 * i);
866                         __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4 * i);
867                         __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4 * i);
868                         __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4 * i);
869                         __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4 * i);
870
871                 }
872         }
873
874         __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
875         __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
876
877         /* submit rx descs */
878         for (i = 0; i < PKTBUFSRX; i++) {
879                 ret = cpdma_submit(priv, &priv->rx_chan, NetRxPackets[i],
880                                    PKTSIZE);
881                 if (ret < 0) {
882                         printf("error %d submitting rx desc\n", ret);
883                         break;
884                 }
885         }
886
887         return ret;
888 }
889
890 static void cpsw_halt(struct eth_device *dev)
891 {
892         struct cpsw_priv        *priv = dev->priv;
893
894         writel(0, priv->dma_regs + CPDMA_TXCONTROL);
895         writel(0, priv->dma_regs + CPDMA_RXCONTROL);
896
897         /* soft reset the controller and initialize priv */
898         setbit_and_wait_for_clear32(&priv->regs->soft_reset);
899
900         /* clear dma state */
901         setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
902
903         debug("%s\n", __func__);
904         priv->data->control(0);
905 }
906
907 static int cpsw_send(struct eth_device *dev, void *packet, int length)
908 {
909         struct cpsw_priv *priv = dev->priv;
910         void *buffer;
911         int len;
912
913         debug("%s@%d: sending packet %p..%p\n", __func__, __LINE__,
914                 packet, packet + length - 1);
915
916         if (!priv->data->mac_control && !cpsw_update_link(priv)) {
917                 printf("%s: Cannot send packet; link is down\n", __func__);
918                 return -EIO;
919         }
920
921         /* first reap completed packets */
922         while (cpdma_process(priv, &priv->tx_chan, &buffer, &len) == 0)
923                 /* NOP */;
924
925         return cpdma_submit(priv, &priv->tx_chan, packet, length);
926 }
927
928 static int cpsw_recv(struct eth_device *dev)
929 {
930         struct cpsw_priv        *priv = dev->priv;
931         void *buffer;
932         int len;
933
934         while (cpdma_process(priv, &priv->rx_chan, &buffer, &len) == 0) {
935                 if (buffer) {
936                         NetReceive(buffer, len);
937                         cpdma_submit(priv, &priv->rx_chan, buffer, PKTSIZE);
938                 } else {
939                         printf("NULL buffer returned from cpdma_process\n");
940                         return -EIO;
941                 }
942         }
943
944         return 0;
945 }
946
947 static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
948                             struct cpsw_priv *priv)
949 {
950         void                    *regs = priv->regs;
951         struct cpsw_slave_data  *data = priv->data->slave_data + slave_num;
952
953         debug("%s@%d: slave[%d] %p\n", __func__, __LINE__,
954                 slave_num, slave);
955         slave->slave_num = slave_num;
956         slave->data     = data;
957         slave->regs     = regs + data->slave_reg_ofs;
958         slave->sliver   = regs + data->sliver_reg_ofs;
959 }
960
961 static int cpsw_phy_init(struct eth_device *dev, struct cpsw_slave *slave)
962 {
963         struct cpsw_priv *priv = (struct cpsw_priv *)dev->priv;
964         struct phy_device *phydev;
965         u32 supported = (SUPPORTED_10baseT_Half |
966                         SUPPORTED_10baseT_Full |
967                         SUPPORTED_100baseT_Half |
968                         SUPPORTED_100baseT_Full |
969                         SUPPORTED_1000baseT_Full);
970
971         if (slave->data->phy_id < 0) {
972                 u32 phy_addr;
973
974                 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
975                         debug("Trying to connect to PHY @ addr %02x\n",
976                                 phy_addr);
977                         phydev = phy_connect(priv->bus, phy_addr,
978                                         dev, slave->data->phy_if);
979                         if (phydev)
980                                 break;
981                 }
982         } else {
983                 phydev = phy_connect(priv->bus,
984                                 slave->data->phy_id,
985                                 dev,
986                                 slave->data->phy_if);
987         }
988         if (!phydev) {
989                 printf("Failed to connect to PHY\n");
990                 return -EINVAL;
991         }
992
993         phydev->supported &= supported;
994         phydev->advertising = phydev->supported;
995
996         priv->phydev = phydev;
997         phy_config(phydev);
998
999         return 0;
1000 }
1001
1002 int cpsw_register(struct cpsw_platform_data *data)
1003 {
1004         int ret = 1;
1005         struct cpsw_priv        *priv;
1006         struct cpsw_slave       *slave;
1007         void                    *regs = (void *)data->cpsw_base;
1008         struct eth_device       *dev;
1009         int i;
1010         int idx = 0;
1011
1012         debug("%s@%d\n", __func__, __LINE__);
1013
1014         dev = calloc(sizeof(*dev), 1);
1015         if (!dev)
1016                 return -ENOMEM;
1017
1018         priv = calloc(sizeof(*priv), 1);
1019         if (!priv) {
1020                 free(dev);
1021                 return -ENOMEM;
1022         }
1023
1024         priv->data = data;
1025         priv->dev = dev;
1026
1027         priv->slaves = calloc(sizeof(struct cpsw_slave), data->slaves);
1028         if (!priv->slaves) {
1029                 free(dev);
1030                 free(priv);
1031                 return -ENOMEM;
1032         }
1033
1034         for (i = 0; i < NUM_DESCS; i++) {
1035                 priv->descs[i].dma_desc = memalign(CONFIG_SYS_CACHELINE_SIZE,
1036                                 sizeof(struct cpsw_desc) * NUM_DESCS);
1037                 if (!priv->descs[i].dma_desc) {
1038                         while (--i >= 0) {
1039                                 free(priv->descs[i].dma_desc);
1040                         }
1041                         free(priv->slaves);
1042                         free(priv);
1043                         free(dev);
1044                         return -ENOMEM;
1045                 }
1046                 debug("DMA desc[%d] allocated @ %p desc_size %u\n",
1047                         i, priv->descs[i].dma_desc,
1048                         sizeof(*priv->descs[i].dma_desc));
1049         }
1050
1051         priv->host_port         = data->host_port_num;
1052         priv->regs              = regs;
1053         priv->host_port_regs    = regs + data->host_port_reg_ofs;
1054         priv->dma_regs          = regs + data->cpdma_reg_ofs;
1055         priv->ale_regs          = regs + data->ale_reg_ofs;
1056
1057         for_each_slave(slave, priv) {
1058                 cpsw_slave_setup(slave, idx, priv);
1059                 idx = idx + 1;
1060         }
1061
1062         strcpy(dev->name, "cpsw");
1063         dev->iobase     = 0;
1064         dev->init       = cpsw_init;
1065         dev->halt       = cpsw_halt;
1066         dev->send       = cpsw_send;
1067         dev->recv       = cpsw_recv;
1068         dev->priv       = priv;
1069
1070         eth_register(dev);
1071
1072         cpsw_mdio_init(dev->name, data->mdio_base, data->mdio_div);
1073         priv->bus = miiphy_get_dev_by_name(dev->name);
1074         for_each_slave(slave, priv) {
1075                 ret = cpsw_phy_init(dev, slave);
1076                 if (ret < 0)
1077                         break;
1078         }
1079         return ret;
1080 }