linux/drivers/net/ethernet/nxp/lpc_eth.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * drivers/net/ethernet/nxp/lpc_eth.c
   4 *
   5 * Author: Kevin Wells <kevin.wells@nxp.com>
   6 *
   7 * Copyright (C) 2010 NXP Semiconductors
   8 * Copyright (C) 2012 Roland Stigge <stigge@antcom.de>
   9 */
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/clk.h>
  14#include <linux/crc32.h>
  15#include <linux/etherdevice.h>
  16#include <linux/module.h>
  17#include <linux/of_net.h>
  18#include <linux/phy.h>
  19#include <linux/platform_device.h>
  20#include <linux/spinlock.h>
  21
  22#include <mach/board.h>
  23#include <mach/hardware.h>
  24#include <mach/platform.h>
  25
  26#define MODNAME "lpc-eth"
  27#define DRV_VERSION "1.00"
  28
  29#define ENET_MAXF_SIZE 1536
  30#define ENET_RX_DESC 48
  31#define ENET_TX_DESC 16
  32
  33#define NAPI_WEIGHT 16
  34
  35/*
  36 * Ethernet MAC controller Register offsets
  37 */
  38#define LPC_ENET_MAC1(x)                        (x + 0x000)
  39#define LPC_ENET_MAC2(x)                        (x + 0x004)
  40#define LPC_ENET_IPGT(x)                        (x + 0x008)
  41#define LPC_ENET_IPGR(x)                        (x + 0x00C)
  42#define LPC_ENET_CLRT(x)                        (x + 0x010)
  43#define LPC_ENET_MAXF(x)                        (x + 0x014)
  44#define LPC_ENET_SUPP(x)                        (x + 0x018)
  45#define LPC_ENET_TEST(x)                        (x + 0x01C)
  46#define LPC_ENET_MCFG(x)                        (x + 0x020)
  47#define LPC_ENET_MCMD(x)                        (x + 0x024)
  48#define LPC_ENET_MADR(x)                        (x + 0x028)
  49#define LPC_ENET_MWTD(x)                        (x + 0x02C)
  50#define LPC_ENET_MRDD(x)                        (x + 0x030)
  51#define LPC_ENET_MIND(x)                        (x + 0x034)
  52#define LPC_ENET_SA0(x)                         (x + 0x040)
  53#define LPC_ENET_SA1(x)                         (x + 0x044)
  54#define LPC_ENET_SA2(x)                         (x + 0x048)
  55#define LPC_ENET_COMMAND(x)                     (x + 0x100)
  56#define LPC_ENET_STATUS(x)                      (x + 0x104)
  57#define LPC_ENET_RXDESCRIPTOR(x)                (x + 0x108)
  58#define LPC_ENET_RXSTATUS(x)                    (x + 0x10C)
  59#define LPC_ENET_RXDESCRIPTORNUMBER(x)          (x + 0x110)
  60#define LPC_ENET_RXPRODUCEINDEX(x)              (x + 0x114)
  61#define LPC_ENET_RXCONSUMEINDEX(x)              (x + 0x118)
  62#define LPC_ENET_TXDESCRIPTOR(x)                (x + 0x11C)
  63#define LPC_ENET_TXSTATUS(x)                    (x + 0x120)
  64#define LPC_ENET_TXDESCRIPTORNUMBER(x)          (x + 0x124)
  65#define LPC_ENET_TXPRODUCEINDEX(x)              (x + 0x128)
  66#define LPC_ENET_TXCONSUMEINDEX(x)              (x + 0x12C)
  67#define LPC_ENET_TSV0(x)                        (x + 0x158)
  68#define LPC_ENET_TSV1(x)                        (x + 0x15C)
  69#define LPC_ENET_RSV(x)                         (x + 0x160)
  70#define LPC_ENET_FLOWCONTROLCOUNTER(x)          (x + 0x170)
  71#define LPC_ENET_FLOWCONTROLSTATUS(x)           (x + 0x174)
  72#define LPC_ENET_RXFILTER_CTRL(x)               (x + 0x200)
  73#define LPC_ENET_RXFILTERWOLSTATUS(x)           (x + 0x204)
  74#define LPC_ENET_RXFILTERWOLCLEAR(x)            (x + 0x208)
  75#define LPC_ENET_HASHFILTERL(x)                 (x + 0x210)
  76#define LPC_ENET_HASHFILTERH(x)                 (x + 0x214)
  77#define LPC_ENET_INTSTATUS(x)                   (x + 0xFE0)
  78#define LPC_ENET_INTENABLE(x)                   (x + 0xFE4)
  79#define LPC_ENET_INTCLEAR(x)                    (x + 0xFE8)
  80#define LPC_ENET_INTSET(x)                      (x + 0xFEC)
  81#define LPC_ENET_POWERDOWN(x)                   (x + 0xFF4)
  82
  83/*
  84 * mac1 register definitions
  85 */
  86#define LPC_MAC1_RECV_ENABLE                    (1 << 0)
  87#define LPC_MAC1_PASS_ALL_RX_FRAMES             (1 << 1)
  88#define LPC_MAC1_RX_FLOW_CONTROL                (1 << 2)
  89#define LPC_MAC1_TX_FLOW_CONTROL                (1 << 3)
  90#define LPC_MAC1_LOOPBACK                       (1 << 4)
  91#define LPC_MAC1_RESET_TX                       (1 << 8)
  92#define LPC_MAC1_RESET_MCS_TX                   (1 << 9)
  93#define LPC_MAC1_RESET_RX                       (1 << 10)
  94#define LPC_MAC1_RESET_MCS_RX                   (1 << 11)
  95#define LPC_MAC1_SIMULATION_RESET               (1 << 14)
  96#define LPC_MAC1_SOFT_RESET                     (1 << 15)
  97
  98/*
  99 * mac2 register definitions
 100 */
 101#define LPC_MAC2_FULL_DUPLEX                    (1 << 0)
 102#define LPC_MAC2_FRAME_LENGTH_CHECKING          (1 << 1)
 103#define LPC_MAC2_HUGH_LENGTH_CHECKING           (1 << 2)
 104#define LPC_MAC2_DELAYED_CRC                    (1 << 3)
 105#define LPC_MAC2_CRC_ENABLE                     (1 << 4)
 106#define LPC_MAC2_PAD_CRC_ENABLE                 (1 << 5)
 107#define LPC_MAC2_VLAN_PAD_ENABLE                (1 << 6)
 108#define LPC_MAC2_AUTO_DETECT_PAD_ENABLE         (1 << 7)
 109#define LPC_MAC2_PURE_PREAMBLE_ENFORCEMENT      (1 << 8)
 110#define LPC_MAC2_LONG_PREAMBLE_ENFORCEMENT      (1 << 9)
 111#define LPC_MAC2_NO_BACKOFF                     (1 << 12)
 112#define LPC_MAC2_BACK_PRESSURE                  (1 << 13)
 113#define LPC_MAC2_EXCESS_DEFER                   (1 << 14)
 114
 115/*
 116 * ipgt register definitions
 117 */
 118#define LPC_IPGT_LOAD(n)                        ((n) & 0x7F)
 119
 120/*
 121 * ipgr register definitions
 122 */
 123#define LPC_IPGR_LOAD_PART2(n)                  ((n) & 0x7F)
 124#define LPC_IPGR_LOAD_PART1(n)                  (((n) & 0x7F) << 8)
 125
 126/*
 127 * clrt register definitions
 128 */
 129#define LPC_CLRT_LOAD_RETRY_MAX(n)              ((n) & 0xF)
 130#define LPC_CLRT_LOAD_COLLISION_WINDOW(n)       (((n) & 0x3F) << 8)
 131
 132/*
 133 * maxf register definitions
 134 */
 135#define LPC_MAXF_LOAD_MAX_FRAME_LEN(n)          ((n) & 0xFFFF)
 136
 137/*
 138 * supp register definitions
 139 */
 140#define LPC_SUPP_SPEED                          (1 << 8)
 141#define LPC_SUPP_RESET_RMII                     (1 << 11)
 142
 143/*
 144 * test register definitions
 145 */
 146#define LPC_TEST_SHORTCUT_PAUSE_QUANTA          (1 << 0)
 147#define LPC_TEST_PAUSE                          (1 << 1)
 148#define LPC_TEST_BACKPRESSURE                   (1 << 2)
 149
 150/*
 151 * mcfg register definitions
 152 */
 153#define LPC_MCFG_SCAN_INCREMENT                 (1 << 0)
 154#define LPC_MCFG_SUPPRESS_PREAMBLE              (1 << 1)
 155#define LPC_MCFG_CLOCK_SELECT(n)                (((n) & 0x7) << 2)
 156#define LPC_MCFG_CLOCK_HOST_DIV_4               0
 157#define LPC_MCFG_CLOCK_HOST_DIV_6               2
 158#define LPC_MCFG_CLOCK_HOST_DIV_8               3
 159#define LPC_MCFG_CLOCK_HOST_DIV_10              4
 160#define LPC_MCFG_CLOCK_HOST_DIV_14              5
 161#define LPC_MCFG_CLOCK_HOST_DIV_20              6
 162#define LPC_MCFG_CLOCK_HOST_DIV_28              7
 163#define LPC_MCFG_RESET_MII_MGMT                 (1 << 15)
 164
 165/*
 166 * mcmd register definitions
 167 */
 168#define LPC_MCMD_READ                           (1 << 0)
 169#define LPC_MCMD_SCAN                           (1 << 1)
 170
 171/*
 172 * madr register definitions
 173 */
 174#define LPC_MADR_REGISTER_ADDRESS(n)            ((n) & 0x1F)
 175#define LPC_MADR_PHY_0ADDRESS(n)                (((n) & 0x1F) << 8)
 176
 177/*
 178 * mwtd register definitions
 179 */
 180#define LPC_MWDT_WRITE(n)                       ((n) & 0xFFFF)
 181
 182/*
 183 * mrdd register definitions
 184 */
 185#define LPC_MRDD_READ_MASK                      0xFFFF
 186
 187/*
 188 * mind register definitions
 189 */
 190#define LPC_MIND_BUSY                           (1 << 0)
 191#define LPC_MIND_SCANNING                       (1 << 1)
 192#define LPC_MIND_NOT_VALID                      (1 << 2)
 193#define LPC_MIND_MII_LINK_FAIL                  (1 << 3)
 194
 195/*
 196 * command register definitions
 197 */
 198#define LPC_COMMAND_RXENABLE                    (1 << 0)
 199#define LPC_COMMAND_TXENABLE                    (1 << 1)
 200#define LPC_COMMAND_REG_RESET                   (1 << 3)
 201#define LPC_COMMAND_TXRESET                     (1 << 4)
 202#define LPC_COMMAND_RXRESET                     (1 << 5)
 203#define LPC_COMMAND_PASSRUNTFRAME               (1 << 6)
 204#define LPC_COMMAND_PASSRXFILTER                (1 << 7)
 205#define LPC_COMMAND_TXFLOWCONTROL               (1 << 8)
 206#define LPC_COMMAND_RMII                        (1 << 9)
 207#define LPC_COMMAND_FULLDUPLEX                  (1 << 10)
 208
 209/*
 210 * status register definitions
 211 */
 212#define LPC_STATUS_RXACTIVE                     (1 << 0)
 213#define LPC_STATUS_TXACTIVE                     (1 << 1)
 214
 215/*
 216 * tsv0 register definitions
 217 */
 218#define LPC_TSV0_CRC_ERROR                      (1 << 0)
 219#define LPC_TSV0_LENGTH_CHECK_ERROR             (1 << 1)
 220#define LPC_TSV0_LENGTH_OUT_OF_RANGE            (1 << 2)
 221#define LPC_TSV0_DONE                           (1 << 3)
 222#define LPC_TSV0_MULTICAST                      (1 << 4)
 223#define LPC_TSV0_BROADCAST                      (1 << 5)
 224#define LPC_TSV0_PACKET_DEFER                   (1 << 6)
 225#define LPC_TSV0_ESCESSIVE_DEFER                (1 << 7)
 226#define LPC_TSV0_ESCESSIVE_COLLISION            (1 << 8)
 227#define LPC_TSV0_LATE_COLLISION                 (1 << 9)
 228#define LPC_TSV0_GIANT                          (1 << 10)
 229#define LPC_TSV0_UNDERRUN                       (1 << 11)
 230#define LPC_TSV0_TOTAL_BYTES(n)                 (((n) >> 12) & 0xFFFF)
 231#define LPC_TSV0_CONTROL_FRAME                  (1 << 28)
 232#define LPC_TSV0_PAUSE                          (1 << 29)
 233#define LPC_TSV0_BACKPRESSURE                   (1 << 30)
 234#define LPC_TSV0_VLAN                           (1 << 31)
 235
 236/*
 237 * tsv1 register definitions
 238 */
 239#define LPC_TSV1_TRANSMIT_BYTE_COUNT(n)         ((n) & 0xFFFF)
 240#define LPC_TSV1_COLLISION_COUNT(n)             (((n) >> 16) & 0xF)
 241
 242/*
 243 * rsv register definitions
 244 */
 245#define LPC_RSV_RECEIVED_BYTE_COUNT(n)          ((n) & 0xFFFF)
 246#define LPC_RSV_RXDV_EVENT_IGNORED              (1 << 16)
 247#define LPC_RSV_RXDV_EVENT_PREVIOUSLY_SEEN      (1 << 17)
 248#define LPC_RSV_CARRIER_EVNT_PREVIOUS_SEEN      (1 << 18)
 249#define LPC_RSV_RECEIVE_CODE_VIOLATION          (1 << 19)
 250#define LPC_RSV_CRC_ERROR                       (1 << 20)
 251#define LPC_RSV_LENGTH_CHECK_ERROR              (1 << 21)
 252#define LPC_RSV_LENGTH_OUT_OF_RANGE             (1 << 22)
 253#define LPC_RSV_RECEIVE_OK                      (1 << 23)
 254#define LPC_RSV_MULTICAST                       (1 << 24)
 255#define LPC_RSV_BROADCAST                       (1 << 25)
 256#define LPC_RSV_DRIBBLE_NIBBLE                  (1 << 26)
 257#define LPC_RSV_CONTROL_FRAME                   (1 << 27)
 258#define LPC_RSV_PAUSE                           (1 << 28)
 259#define LPC_RSV_UNSUPPORTED_OPCODE              (1 << 29)
 260#define LPC_RSV_VLAN                            (1 << 30)
 261
 262/*
 263 * flowcontrolcounter register definitions
 264 */
 265#define LPC_FCCR_MIRRORCOUNTER(n)               ((n) & 0xFFFF)
 266#define LPC_FCCR_PAUSETIMER(n)                  (((n) >> 16) & 0xFFFF)
 267
 268/*
 269 * flowcontrolstatus register definitions
 270 */
 271#define LPC_FCCR_MIRRORCOUNTERCURRENT(n)        ((n) & 0xFFFF)
 272
 273/*
 274 * rxfilterctrl, rxfilterwolstatus, and rxfilterwolclear shared
 275 * register definitions
 276 */
 277#define LPC_RXFLTRW_ACCEPTUNICAST               (1 << 0)
 278#define LPC_RXFLTRW_ACCEPTUBROADCAST            (1 << 1)
 279#define LPC_RXFLTRW_ACCEPTUMULTICAST            (1 << 2)
 280#define LPC_RXFLTRW_ACCEPTUNICASTHASH           (1 << 3)
 281#define LPC_RXFLTRW_ACCEPTUMULTICASTHASH        (1 << 4)
 282#define LPC_RXFLTRW_ACCEPTPERFECT               (1 << 5)
 283
 284/*
 285 * rxfilterctrl register definitions
 286 */
 287#define LPC_RXFLTRWSTS_MAGICPACKETENWOL         (1 << 12)
 288#define LPC_RXFLTRWSTS_RXFILTERENWOL            (1 << 13)
 289
 290/*
 291 * rxfilterwolstatus/rxfilterwolclear register definitions
 292 */
 293#define LPC_RXFLTRWSTS_RXFILTERWOL              (1 << 7)
 294#define LPC_RXFLTRWSTS_MAGICPACKETWOL           (1 << 8)
 295
 296/*
 297 * intstatus, intenable, intclear, and Intset shared register
 298 * definitions
 299 */
 300#define LPC_MACINT_RXOVERRUNINTEN               (1 << 0)
 301#define LPC_MACINT_RXERRORONINT                 (1 << 1)
 302#define LPC_MACINT_RXFINISHEDINTEN              (1 << 2)
 303#define LPC_MACINT_RXDONEINTEN                  (1 << 3)
 304#define LPC_MACINT_TXUNDERRUNINTEN              (1 << 4)
 305#define LPC_MACINT_TXERRORINTEN                 (1 << 5)
 306#define LPC_MACINT_TXFINISHEDINTEN              (1 << 6)
 307#define LPC_MACINT_TXDONEINTEN                  (1 << 7)
 308#define LPC_MACINT_SOFTINTEN                    (1 << 12)
 309#define LPC_MACINT_WAKEUPINTEN                  (1 << 13)
 310
 311/*
 312 * powerdown register definitions
 313 */
 314#define LPC_POWERDOWN_MACAHB                    (1 << 31)
 315
 316static phy_interface_t lpc_phy_interface_mode(struct device *dev)
 317{
 318        if (dev && dev->of_node) {
 319                const char *mode = of_get_property(dev->of_node,
 320                                                   "phy-mode", NULL);
 321                if (mode && !strcmp(mode, "mii"))
 322                        return PHY_INTERFACE_MODE_MII;
 323        }
 324        return PHY_INTERFACE_MODE_RMII;
 325}
 326
 327static bool use_iram_for_net(struct device *dev)
 328{
 329        if (dev && dev->of_node)
 330                return of_property_read_bool(dev->of_node, "use-iram");
 331        return false;
 332}
 333
 334/* Receive Status information word */
 335#define RXSTATUS_SIZE                   0x000007FF
 336#define RXSTATUS_CONTROL                (1 << 18)
 337#define RXSTATUS_VLAN                   (1 << 19)
 338#define RXSTATUS_FILTER                 (1 << 20)
 339#define RXSTATUS_MULTICAST              (1 << 21)
 340#define RXSTATUS_BROADCAST              (1 << 22)
 341#define RXSTATUS_CRC                    (1 << 23)
 342#define RXSTATUS_SYMBOL                 (1 << 24)
 343#define RXSTATUS_LENGTH                 (1 << 25)
 344#define RXSTATUS_RANGE                  (1 << 26)
 345#define RXSTATUS_ALIGN                  (1 << 27)
 346#define RXSTATUS_OVERRUN                (1 << 28)
 347#define RXSTATUS_NODESC                 (1 << 29)
 348#define RXSTATUS_LAST                   (1 << 30)
 349#define RXSTATUS_ERROR                  (1 << 31)
 350
 351#define RXSTATUS_STATUS_ERROR \
 352        (RXSTATUS_NODESC | RXSTATUS_OVERRUN | RXSTATUS_ALIGN | \
 353         RXSTATUS_RANGE | RXSTATUS_LENGTH | RXSTATUS_SYMBOL | RXSTATUS_CRC)
 354
 355/* Receive Descriptor control word */
 356#define RXDESC_CONTROL_SIZE             0x000007FF
 357#define RXDESC_CONTROL_INT              (1 << 31)
 358
 359/* Transmit Status information word */
 360#define TXSTATUS_COLLISIONS_GET(x)      (((x) >> 21) & 0xF)
 361#define TXSTATUS_DEFER                  (1 << 25)
 362#define TXSTATUS_EXCESSDEFER            (1 << 26)
 363#define TXSTATUS_EXCESSCOLL             (1 << 27)
 364#define TXSTATUS_LATECOLL               (1 << 28)
 365#define TXSTATUS_UNDERRUN               (1 << 29)
 366#define TXSTATUS_NODESC                 (1 << 30)
 367#define TXSTATUS_ERROR                  (1 << 31)
 368
 369/* Transmit Descriptor control word */
 370#define TXDESC_CONTROL_SIZE             0x000007FF
 371#define TXDESC_CONTROL_OVERRIDE         (1 << 26)
 372#define TXDESC_CONTROL_HUGE             (1 << 27)
 373#define TXDESC_CONTROL_PAD              (1 << 28)
 374#define TXDESC_CONTROL_CRC              (1 << 29)
 375#define TXDESC_CONTROL_LAST             (1 << 30)
 376#define TXDESC_CONTROL_INT              (1 << 31)
 377
 378/*
 379 * Structure of a TX/RX descriptors and RX status
 380 */
 381struct txrx_desc_t {
 382        __le32 packet;
 383        __le32 control;
 384};
 385struct rx_status_t {
 386        __le32 statusinfo;
 387        __le32 statushashcrc;
 388};
 389
 390/*
 391 * Device driver data structure
 392 */
 393struct netdata_local {
 394        struct platform_device  *pdev;
 395        struct net_device       *ndev;
 396        spinlock_t              lock;
 397        void __iomem            *net_base;
 398        u32                     msg_enable;
 399        unsigned int            skblen[ENET_TX_DESC];
 400        unsigned int            last_tx_idx;
 401        unsigned int            num_used_tx_buffs;
 402        struct mii_bus          *mii_bus;
 403        struct clk              *clk;
 404        dma_addr_t              dma_buff_base_p;
 405        void                    *dma_buff_base_v;
 406        size_t                  dma_buff_size;
 407        struct txrx_desc_t      *tx_desc_v;
 408        u32                     *tx_stat_v;
 409        void                    *tx_buff_v;
 410        struct txrx_desc_t      *rx_desc_v;
 411        struct rx_status_t      *rx_stat_v;
 412        void                    *rx_buff_v;
 413        int                     link;
 414        int                     speed;
 415        int                     duplex;
 416        struct napi_struct      napi;
 417};
 418
 419/*
 420 * MAC support functions
 421 */
 422static void __lpc_set_mac(struct netdata_local *pldat, u8 *mac)
 423{
 424        u32 tmp;
 425
 426        /* Set station address */
 427        tmp = mac[0] | ((u32)mac[1] << 8);
 428        writel(tmp, LPC_ENET_SA2(pldat->net_base));
 429        tmp = mac[2] | ((u32)mac[3] << 8);
 430        writel(tmp, LPC_ENET_SA1(pldat->net_base));
 431        tmp = mac[4] | ((u32)mac[5] << 8);
 432        writel(tmp, LPC_ENET_SA0(pldat->net_base));
 433
 434        netdev_dbg(pldat->ndev, "Ethernet MAC address %pM\n", mac);
 435}
 436
 437static void __lpc_get_mac(struct netdata_local *pldat, u8 *mac)
 438{
 439        u32 tmp;
 440
 441        /* Get station address */
 442        tmp = readl(LPC_ENET_SA2(pldat->net_base));
 443        mac[0] = tmp & 0xFF;
 444        mac[1] = tmp >> 8;
 445        tmp = readl(LPC_ENET_SA1(pldat->net_base));
 446        mac[2] = tmp & 0xFF;
 447        mac[3] = tmp >> 8;
 448        tmp = readl(LPC_ENET_SA0(pldat->net_base));
 449        mac[4] = tmp & 0xFF;
 450        mac[5] = tmp >> 8;
 451}
 452
 453static void __lpc_params_setup(struct netdata_local *pldat)
 454{
 455        u32 tmp;
 456
 457        if (pldat->duplex == DUPLEX_FULL) {
 458                tmp = readl(LPC_ENET_MAC2(pldat->net_base));
 459                tmp |= LPC_MAC2_FULL_DUPLEX;
 460                writel(tmp, LPC_ENET_MAC2(pldat->net_base));
 461                tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
 462                tmp |= LPC_COMMAND_FULLDUPLEX;
 463                writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
 464                writel(LPC_IPGT_LOAD(0x15), LPC_ENET_IPGT(pldat->net_base));
 465        } else {
 466                tmp = readl(LPC_ENET_MAC2(pldat->net_base));
 467                tmp &= ~LPC_MAC2_FULL_DUPLEX;
 468                writel(tmp, LPC_ENET_MAC2(pldat->net_base));
 469                tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
 470                tmp &= ~LPC_COMMAND_FULLDUPLEX;
 471                writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
 472                writel(LPC_IPGT_LOAD(0x12), LPC_ENET_IPGT(pldat->net_base));
 473        }
 474
 475        if (pldat->speed == SPEED_100)
 476                writel(LPC_SUPP_SPEED, LPC_ENET_SUPP(pldat->net_base));
 477        else
 478                writel(0, LPC_ENET_SUPP(pldat->net_base));
 479}
 480
 481static void __lpc_eth_reset(struct netdata_local *pldat)
 482{
 483        /* Reset all MAC logic */
 484        writel((LPC_MAC1_RESET_TX | LPC_MAC1_RESET_MCS_TX | LPC_MAC1_RESET_RX |
 485                LPC_MAC1_RESET_MCS_RX | LPC_MAC1_SIMULATION_RESET |
 486                LPC_MAC1_SOFT_RESET), LPC_ENET_MAC1(pldat->net_base));
 487        writel((LPC_COMMAND_REG_RESET | LPC_COMMAND_TXRESET |
 488                LPC_COMMAND_RXRESET), LPC_ENET_COMMAND(pldat->net_base));
 489}
 490
 491static int __lpc_mii_mngt_reset(struct netdata_local *pldat)
 492{
 493        /* Reset MII management hardware */
 494        writel(LPC_MCFG_RESET_MII_MGMT, LPC_ENET_MCFG(pldat->net_base));
 495
 496        /* Setup MII clock to slowest rate with a /28 divider */
 497        writel(LPC_MCFG_CLOCK_SELECT(LPC_MCFG_CLOCK_HOST_DIV_28),
 498               LPC_ENET_MCFG(pldat->net_base));
 499
 500        return 0;
 501}
 502
 503static inline phys_addr_t __va_to_pa(void *addr, struct netdata_local *pldat)
 504{
 505        phys_addr_t phaddr;
 506
 507        phaddr = addr - pldat->dma_buff_base_v;
 508        phaddr += pldat->dma_buff_base_p;
 509
 510        return phaddr;
 511}
 512
 513static void lpc_eth_enable_int(void __iomem *regbase)
 514{
 515        writel((LPC_MACINT_RXDONEINTEN | LPC_MACINT_TXDONEINTEN),
 516               LPC_ENET_INTENABLE(regbase));
 517}
 518
 519static void lpc_eth_disable_int(void __iomem *regbase)
 520{
 521        writel(0, LPC_ENET_INTENABLE(regbase));
 522}
 523
 524/* Setup TX/RX descriptors */
 525static void __lpc_txrx_desc_setup(struct netdata_local *pldat)
 526{
 527        u32 *ptxstat;
 528        void *tbuff;
 529        int i;
 530        struct txrx_desc_t *ptxrxdesc;
 531        struct rx_status_t *prxstat;
 532
 533        tbuff = PTR_ALIGN(pldat->dma_buff_base_v, 16);
 534
 535        /* Setup TX descriptors, status, and buffers */
 536        pldat->tx_desc_v = tbuff;
 537        tbuff += sizeof(struct txrx_desc_t) * ENET_TX_DESC;
 538
 539        pldat->tx_stat_v = tbuff;
 540        tbuff += sizeof(u32) * ENET_TX_DESC;
 541
 542        tbuff = PTR_ALIGN(tbuff, 16);
 543        pldat->tx_buff_v = tbuff;
 544        tbuff += ENET_MAXF_SIZE * ENET_TX_DESC;
 545
 546        /* Setup RX descriptors, status, and buffers */
 547        pldat->rx_desc_v = tbuff;
 548        tbuff += sizeof(struct txrx_desc_t) * ENET_RX_DESC;
 549
 550        tbuff = PTR_ALIGN(tbuff, 16);
 551        pldat->rx_stat_v = tbuff;
 552        tbuff += sizeof(struct rx_status_t) * ENET_RX_DESC;
 553
 554        tbuff = PTR_ALIGN(tbuff, 16);
 555        pldat->rx_buff_v = tbuff;
 556        tbuff += ENET_MAXF_SIZE * ENET_RX_DESC;
 557
 558        /* Map the TX descriptors to the TX buffers in hardware */
 559        for (i = 0; i < ENET_TX_DESC; i++) {
 560                ptxstat = &pldat->tx_stat_v[i];
 561                ptxrxdesc = &pldat->tx_desc_v[i];
 562
 563                ptxrxdesc->packet = __va_to_pa(
 564                                pldat->tx_buff_v + i * ENET_MAXF_SIZE, pldat);
 565                ptxrxdesc->control = 0;
 566                *ptxstat = 0;
 567        }
 568
 569        /* Map the RX descriptors to the RX buffers in hardware */
 570        for (i = 0; i < ENET_RX_DESC; i++) {
 571                prxstat = &pldat->rx_stat_v[i];
 572                ptxrxdesc = &pldat->rx_desc_v[i];
 573
 574                ptxrxdesc->packet = __va_to_pa(
 575                                pldat->rx_buff_v + i * ENET_MAXF_SIZE, pldat);
 576                ptxrxdesc->control = RXDESC_CONTROL_INT | (ENET_MAXF_SIZE - 1);
 577                prxstat->statusinfo = 0;
 578                prxstat->statushashcrc = 0;
 579        }
 580
 581        /* Setup base addresses in hardware to point to buffers and
 582         * descriptors
 583         */
 584        writel((ENET_TX_DESC - 1),
 585               LPC_ENET_TXDESCRIPTORNUMBER(pldat->net_base));
 586        writel(__va_to_pa(pldat->tx_desc_v, pldat),
 587               LPC_ENET_TXDESCRIPTOR(pldat->net_base));
 588        writel(__va_to_pa(pldat->tx_stat_v, pldat),
 589               LPC_ENET_TXSTATUS(pldat->net_base));
 590        writel((ENET_RX_DESC - 1),
 591               LPC_ENET_RXDESCRIPTORNUMBER(pldat->net_base));
 592        writel(__va_to_pa(pldat->rx_desc_v, pldat),
 593               LPC_ENET_RXDESCRIPTOR(pldat->net_base));
 594        writel(__va_to_pa(pldat->rx_stat_v, pldat),
 595               LPC_ENET_RXSTATUS(pldat->net_base));
 596}
 597
 598static void __lpc_eth_init(struct netdata_local *pldat)
 599{
 600        u32 tmp;
 601
 602        /* Disable controller and reset */
 603        tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
 604        tmp &= ~LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
 605        writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
 606        tmp = readl(LPC_ENET_MAC1(pldat->net_base));
 607        tmp &= ~LPC_MAC1_RECV_ENABLE;
 608        writel(tmp, LPC_ENET_MAC1(pldat->net_base));
 609
 610        /* Initial MAC setup */
 611        writel(LPC_MAC1_PASS_ALL_RX_FRAMES, LPC_ENET_MAC1(pldat->net_base));
 612        writel((LPC_MAC2_PAD_CRC_ENABLE | LPC_MAC2_CRC_ENABLE),
 613               LPC_ENET_MAC2(pldat->net_base));
 614        writel(ENET_MAXF_SIZE, LPC_ENET_MAXF(pldat->net_base));
 615
 616        /* Collision window, gap */
 617        writel((LPC_CLRT_LOAD_RETRY_MAX(0xF) |
 618                LPC_CLRT_LOAD_COLLISION_WINDOW(0x37)),
 619               LPC_ENET_CLRT(pldat->net_base));
 620        writel(LPC_IPGR_LOAD_PART2(0x12), LPC_ENET_IPGR(pldat->net_base));
 621
 622        if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
 623                writel(LPC_COMMAND_PASSRUNTFRAME,
 624                       LPC_ENET_COMMAND(pldat->net_base));
 625        else {
 626                writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
 627                       LPC_ENET_COMMAND(pldat->net_base));
 628                writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
 629        }
 630
 631        __lpc_params_setup(pldat);
 632
 633        /* Setup TX and RX descriptors */
 634        __lpc_txrx_desc_setup(pldat);
 635
 636        /* Setup packet filtering */
 637        writel((LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT),
 638               LPC_ENET_RXFILTER_CTRL(pldat->net_base));
 639
 640        /* Get the next TX buffer output index */
 641        pldat->num_used_tx_buffs = 0;
 642        pldat->last_tx_idx =
 643                readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
 644
 645        /* Clear and enable interrupts */
 646        writel(0xFFFF, LPC_ENET_INTCLEAR(pldat->net_base));
 647        smp_wmb();
 648        lpc_eth_enable_int(pldat->net_base);
 649
 650        /* Enable controller */
 651        tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
 652        tmp |= LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
 653        writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
 654        tmp = readl(LPC_ENET_MAC1(pldat->net_base));
 655        tmp |= LPC_MAC1_RECV_ENABLE;
 656        writel(tmp, LPC_ENET_MAC1(pldat->net_base));
 657}
 658
 659static void __lpc_eth_shutdown(struct netdata_local *pldat)
 660{
 661        /* Reset ethernet and power down PHY */
 662        __lpc_eth_reset(pldat);
 663        writel(0, LPC_ENET_MAC1(pldat->net_base));
 664        writel(0, LPC_ENET_MAC2(pldat->net_base));
 665}
 666
 667/*
 668 * MAC<--->PHY support functions
 669 */
 670static int lpc_mdio_read(struct mii_bus *bus, int phy_id, int phyreg)
 671{
 672        struct netdata_local *pldat = bus->priv;
 673        unsigned long timeout = jiffies + msecs_to_jiffies(100);
 674        int lps;
 675
 676        writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
 677        writel(LPC_MCMD_READ, LPC_ENET_MCMD(pldat->net_base));
 678
 679        /* Wait for unbusy status */
 680        while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
 681                if (time_after(jiffies, timeout))
 682                        return -EIO;
 683                cpu_relax();
 684        }
 685
 686        lps = readl(LPC_ENET_MRDD(pldat->net_base));
 687        writel(0, LPC_ENET_MCMD(pldat->net_base));
 688
 689        return lps;
 690}
 691
 692static int lpc_mdio_write(struct mii_bus *bus, int phy_id, int phyreg,
 693                        u16 phydata)
 694{
 695        struct netdata_local *pldat = bus->priv;
 696        unsigned long timeout = jiffies + msecs_to_jiffies(100);
 697
 698        writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
 699        writel(phydata, LPC_ENET_MWTD(pldat->net_base));
 700
 701        /* Wait for completion */
 702        while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
 703                if (time_after(jiffies, timeout))
 704                        return -EIO;
 705                cpu_relax();
 706        }
 707
 708        return 0;
 709}
 710
 711static int lpc_mdio_reset(struct mii_bus *bus)
 712{
 713        return __lpc_mii_mngt_reset((struct netdata_local *)bus->priv);
 714}
 715
 716static void lpc_handle_link_change(struct net_device *ndev)
 717{
 718        struct netdata_local *pldat = netdev_priv(ndev);
 719        struct phy_device *phydev = ndev->phydev;
 720        unsigned long flags;
 721
 722        bool status_change = false;
 723
 724        spin_lock_irqsave(&pldat->lock, flags);
 725
 726        if (phydev->link) {
 727                if ((pldat->speed != phydev->speed) ||
 728                    (pldat->duplex != phydev->duplex)) {
 729                        pldat->speed = phydev->speed;
 730                        pldat->duplex = phydev->duplex;
 731                        status_change = true;
 732                }
 733        }
 734
 735        if (phydev->link != pldat->link) {
 736                if (!phydev->link) {
 737                        pldat->speed = 0;
 738                        pldat->duplex = -1;
 739                }
 740                pldat->link = phydev->link;
 741
 742                status_change = true;
 743        }
 744
 745        spin_unlock_irqrestore(&pldat->lock, flags);
 746
 747        if (status_change)
 748                __lpc_params_setup(pldat);
 749}
 750
 751static int lpc_mii_probe(struct net_device *ndev)
 752{
 753        struct netdata_local *pldat = netdev_priv(ndev);
 754        struct phy_device *phydev = phy_find_first(pldat->mii_bus);
 755
 756        if (!phydev) {
 757                netdev_err(ndev, "no PHY found\n");
 758                return -ENODEV;
 759        }
 760
 761        /* Attach to the PHY */
 762        if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
 763                netdev_info(ndev, "using MII interface\n");
 764        else
 765                netdev_info(ndev, "using RMII interface\n");
 766        phydev = phy_connect(ndev, phydev_name(phydev),
 767                             &lpc_handle_link_change,
 768                             lpc_phy_interface_mode(&pldat->pdev->dev));
 769
 770        if (IS_ERR(phydev)) {
 771                netdev_err(ndev, "Could not attach to PHY\n");
 772                return PTR_ERR(phydev);
 773        }
 774
 775        phy_set_max_speed(phydev, SPEED_100);
 776
 777        pldat->link = 0;
 778        pldat->speed = 0;
 779        pldat->duplex = -1;
 780
 781        phy_attached_info(phydev);
 782
 783        return 0;
 784}
 785
 786static int lpc_mii_init(struct netdata_local *pldat)
 787{
 788        int err = -ENXIO;
 789
 790        pldat->mii_bus = mdiobus_alloc();
 791        if (!pldat->mii_bus) {
 792                err = -ENOMEM;
 793                goto err_out;
 794        }
 795
 796        /* Setup MII mode */
 797        if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
 798                writel(LPC_COMMAND_PASSRUNTFRAME,
 799                       LPC_ENET_COMMAND(pldat->net_base));
 800        else {
 801                writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
 802                       LPC_ENET_COMMAND(pldat->net_base));
 803                writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
 804        }
 805
 806        pldat->mii_bus->name = "lpc_mii_bus";
 807        pldat->mii_bus->read = &lpc_mdio_read;
 808        pldat->mii_bus->write = &lpc_mdio_write;
 809        pldat->mii_bus->reset = &lpc_mdio_reset;
 810        snprintf(pldat->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
 811                 pldat->pdev->name, pldat->pdev->id);
 812        pldat->mii_bus->priv = pldat;
 813        pldat->mii_bus->parent = &pldat->pdev->dev;
 814
 815        platform_set_drvdata(pldat->pdev, pldat->mii_bus);
 816
 817        if (mdiobus_register(pldat->mii_bus))
 818                goto err_out_unregister_bus;
 819
 820        if (lpc_mii_probe(pldat->ndev) != 0)
 821                goto err_out_unregister_bus;
 822
 823        return 0;
 824
 825err_out_unregister_bus:
 826        mdiobus_unregister(pldat->mii_bus);
 827        mdiobus_free(pldat->mii_bus);
 828err_out:
 829        return err;
 830}
 831
 832static void __lpc_handle_xmit(struct net_device *ndev)
 833{
 834        struct netdata_local *pldat = netdev_priv(ndev);
 835        u32 txcidx, *ptxstat, txstat;
 836
 837        txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
 838        while (pldat->last_tx_idx != txcidx) {
 839                unsigned int skblen = pldat->skblen[pldat->last_tx_idx];
 840
 841                /* A buffer is available, get buffer status */
 842                ptxstat = &pldat->tx_stat_v[pldat->last_tx_idx];
 843                txstat = *ptxstat;
 844
 845                /* Next buffer and decrement used buffer counter */
 846                pldat->num_used_tx_buffs--;
 847                pldat->last_tx_idx++;
 848                if (pldat->last_tx_idx >= ENET_TX_DESC)
 849                        pldat->last_tx_idx = 0;
 850
 851                /* Update collision counter */
 852                ndev->stats.collisions += TXSTATUS_COLLISIONS_GET(txstat);
 853
 854                /* Any errors occurred? */
 855                if (txstat & TXSTATUS_ERROR) {
 856                        if (txstat & TXSTATUS_UNDERRUN) {
 857                                /* FIFO underrun */
 858                                ndev->stats.tx_fifo_errors++;
 859                        }
 860                        if (txstat & TXSTATUS_LATECOLL) {
 861                                /* Late collision */
 862                                ndev->stats.tx_aborted_errors++;
 863                        }
 864                        if (txstat & TXSTATUS_EXCESSCOLL) {
 865                                /* Excessive collision */
 866                                ndev->stats.tx_aborted_errors++;
 867                        }
 868                        if (txstat & TXSTATUS_EXCESSDEFER) {
 869                                /* Defer limit */
 870                                ndev->stats.tx_aborted_errors++;
 871                        }
 872                        ndev->stats.tx_errors++;
 873                } else {
 874                        /* Update stats */
 875                        ndev->stats.tx_packets++;
 876                        ndev->stats.tx_bytes += skblen;
 877                }
 878
 879                txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
 880        }
 881
 882        if (pldat->num_used_tx_buffs <= ENET_TX_DESC/2) {
 883                if (netif_queue_stopped(ndev))
 884                        netif_wake_queue(ndev);
 885        }
 886}
 887
 888static int __lpc_handle_recv(struct net_device *ndev, int budget)
 889{
 890        struct netdata_local *pldat = netdev_priv(ndev);
 891        struct sk_buff *skb;
 892        u32 rxconsidx, len, ethst;
 893        struct rx_status_t *prxstat;
 894        int rx_done = 0;
 895
 896        /* Get the current RX buffer indexes */
 897        rxconsidx = readl(LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
 898        while (rx_done < budget && rxconsidx !=
 899                        readl(LPC_ENET_RXPRODUCEINDEX(pldat->net_base))) {
 900                /* Get pointer to receive status */
 901                prxstat = &pldat->rx_stat_v[rxconsidx];
 902                len = (prxstat->statusinfo & RXSTATUS_SIZE) + 1;
 903
 904                /* Status error? */
 905                ethst = prxstat->statusinfo;
 906                if ((ethst & (RXSTATUS_ERROR | RXSTATUS_STATUS_ERROR)) ==
 907                    (RXSTATUS_ERROR | RXSTATUS_RANGE))
 908                        ethst &= ~RXSTATUS_ERROR;
 909
 910                if (ethst & RXSTATUS_ERROR) {
 911                        int si = prxstat->statusinfo;
 912                        /* Check statuses */
 913                        if (si & RXSTATUS_OVERRUN) {
 914                                /* Overrun error */
 915                                ndev->stats.rx_fifo_errors++;
 916                        } else if (si & RXSTATUS_CRC) {
 917                                /* CRC error */
 918                                ndev->stats.rx_crc_errors++;
 919                        } else if (si & RXSTATUS_LENGTH) {
 920                                /* Length error */
 921                                ndev->stats.rx_length_errors++;
 922                        } else if (si & RXSTATUS_ERROR) {
 923                                /* Other error */
 924                                ndev->stats.rx_length_errors++;
 925                        }
 926                        ndev->stats.rx_errors++;
 927                } else {
 928                        /* Packet is good */
 929                        skb = dev_alloc_skb(len);
 930                        if (!skb) {
 931                                ndev->stats.rx_dropped++;
 932                        } else {
 933                                /* Copy packet from buffer */
 934                                skb_put_data(skb,
 935                                             pldat->rx_buff_v + rxconsidx * ENET_MAXF_SIZE,
 936                                             len);
 937
 938                                /* Pass to upper layer */
 939                                skb->protocol = eth_type_trans(skb, ndev);
 940                                netif_receive_skb(skb);
 941                                ndev->stats.rx_packets++;
 942                                ndev->stats.rx_bytes += len;
 943                        }
 944                }
 945
 946                /* Increment consume index */
 947                rxconsidx = rxconsidx + 1;
 948                if (rxconsidx >= ENET_RX_DESC)
 949                        rxconsidx = 0;
 950                writel(rxconsidx,
 951                       LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
 952                rx_done++;
 953        }
 954
 955        return rx_done;
 956}
 957
 958static int lpc_eth_poll(struct napi_struct *napi, int budget)
 959{
 960        struct netdata_local *pldat = container_of(napi,
 961                        struct netdata_local, napi);
 962        struct net_device *ndev = pldat->ndev;
 963        int rx_done = 0;
 964        struct netdev_queue *txq = netdev_get_tx_queue(ndev, 0);
 965
 966        __netif_tx_lock(txq, smp_processor_id());
 967        __lpc_handle_xmit(ndev);
 968        __netif_tx_unlock(txq);
 969        rx_done = __lpc_handle_recv(ndev, budget);
 970
 971        if (rx_done < budget) {
 972                napi_complete_done(napi, rx_done);
 973                lpc_eth_enable_int(pldat->net_base);
 974        }
 975
 976        return rx_done;
 977}
 978
 979static irqreturn_t __lpc_eth_interrupt(int irq, void *dev_id)
 980{
 981        struct net_device *ndev = dev_id;
 982        struct netdata_local *pldat = netdev_priv(ndev);
 983        u32 tmp;
 984
 985        spin_lock(&pldat->lock);
 986
 987        tmp = readl(LPC_ENET_INTSTATUS(pldat->net_base));
 988        /* Clear interrupts */
 989        writel(tmp, LPC_ENET_INTCLEAR(pldat->net_base));
 990
 991        lpc_eth_disable_int(pldat->net_base);
 992        if (likely(napi_schedule_prep(&pldat->napi)))
 993                __napi_schedule(&pldat->napi);
 994
 995        spin_unlock(&pldat->lock);
 996
 997        return IRQ_HANDLED;
 998}
 999
1000static int lpc_eth_close(struct net_device *ndev)
1001{
1002        unsigned long flags;
1003        struct netdata_local *pldat = netdev_priv(ndev);
1004
1005        if (netif_msg_ifdown(pldat))
1006                dev_dbg(&pldat->pdev->dev, "shutting down %s\n", ndev->name);
1007
1008        napi_disable(&pldat->napi);
1009        netif_stop_queue(ndev);
1010
1011        if (ndev->phydev)
1012                phy_stop(ndev->phydev);
1013
1014        spin_lock_irqsave(&pldat->lock, flags);
1015        __lpc_eth_reset(pldat);
1016        netif_carrier_off(ndev);
1017        writel(0, LPC_ENET_MAC1(pldat->net_base));
1018        writel(0, LPC_ENET_MAC2(pldat->net_base));
1019        spin_unlock_irqrestore(&pldat->lock, flags);
1020
1021        clk_disable_unprepare(pldat->clk);
1022
1023        return 0;
1024}
1025
1026static int lpc_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1027{
1028        struct netdata_local *pldat = netdev_priv(ndev);
1029        u32 len, txidx;
1030        u32 *ptxstat;
1031        struct txrx_desc_t *ptxrxdesc;
1032
1033        len = skb->len;
1034
1035        spin_lock_irq(&pldat->lock);
1036
1037        if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1)) {
1038                /* This function should never be called when there are no
1039                   buffers */
1040                netif_stop_queue(ndev);
1041                spin_unlock_irq(&pldat->lock);
1042                WARN(1, "BUG! TX request when no free TX buffers!\n");
1043                return NETDEV_TX_BUSY;
1044        }
1045
1046        /* Get the next TX descriptor index */
1047        txidx = readl(LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
1048
1049        /* Setup control for the transfer */
1050        ptxstat = &pldat->tx_stat_v[txidx];
1051        *ptxstat = 0;
1052        ptxrxdesc = &pldat->tx_desc_v[txidx];
1053        ptxrxdesc->control =
1054                (len - 1) | TXDESC_CONTROL_LAST | TXDESC_CONTROL_INT;
1055
1056        /* Copy data to the DMA buffer */
1057        memcpy(pldat->tx_buff_v + txidx * ENET_MAXF_SIZE, skb->data, len);
1058
1059        /* Save the buffer and increment the buffer counter */
1060        pldat->skblen[txidx] = len;
1061        pldat->num_used_tx_buffs++;
1062
1063        /* Start transmit */
1064        txidx++;
1065        if (txidx >= ENET_TX_DESC)
1066                txidx = 0;
1067        writel(txidx, LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
1068
1069        /* Stop queue if no more TX buffers */
1070        if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1))
1071                netif_stop_queue(ndev);
1072
1073        spin_unlock_irq(&pldat->lock);
1074
1075        dev_kfree_skb(skb);
1076        return NETDEV_TX_OK;
1077}
1078
1079static int lpc_set_mac_address(struct net_device *ndev, void *p)
1080{
1081        struct sockaddr *addr = p;
1082        struct netdata_local *pldat = netdev_priv(ndev);
1083        unsigned long flags;
1084
1085        if (!is_valid_ether_addr(addr->sa_data))
1086                return -EADDRNOTAVAIL;
1087        memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
1088
1089        spin_lock_irqsave(&pldat->lock, flags);
1090
1091        /* Set station address */
1092        __lpc_set_mac(pldat, ndev->dev_addr);
1093
1094        spin_unlock_irqrestore(&pldat->lock, flags);
1095
1096        return 0;
1097}
1098
1099static void lpc_eth_set_multicast_list(struct net_device *ndev)
1100{
1101        struct netdata_local *pldat = netdev_priv(ndev);
1102        struct netdev_hw_addr_list *mcptr = &ndev->mc;
1103        struct netdev_hw_addr *ha;
1104        u32 tmp32, hash_val, hashlo, hashhi;
1105        unsigned long flags;
1106
1107        spin_lock_irqsave(&pldat->lock, flags);
1108
1109        /* Set station address */
1110        __lpc_set_mac(pldat, ndev->dev_addr);
1111
1112        tmp32 =  LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT;
1113
1114        if (ndev->flags & IFF_PROMISC)
1115                tmp32 |= LPC_RXFLTRW_ACCEPTUNICAST |
1116                        LPC_RXFLTRW_ACCEPTUMULTICAST;
1117        if (ndev->flags & IFF_ALLMULTI)
1118                tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICAST;
1119
1120        if (netdev_hw_addr_list_count(mcptr))
1121                tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICASTHASH;
1122
1123        writel(tmp32, LPC_ENET_RXFILTER_CTRL(pldat->net_base));
1124
1125
1126        /* Set initial hash table */
1127        hashlo = 0x0;
1128        hashhi = 0x0;
1129
1130        /* 64 bits : multicast address in hash table */
1131        netdev_hw_addr_list_for_each(ha, mcptr) {
1132                hash_val = (ether_crc(6, ha->addr) >> 23) & 0x3F;
1133
1134                if (hash_val >= 32)
1135                        hashhi |= 1 << (hash_val - 32);
1136                else
1137                        hashlo |= 1 << hash_val;
1138        }
1139
1140        writel(hashlo, LPC_ENET_HASHFILTERL(pldat->net_base));
1141        writel(hashhi, LPC_ENET_HASHFILTERH(pldat->net_base));
1142
1143        spin_unlock_irqrestore(&pldat->lock, flags);
1144}
1145
1146static int lpc_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1147{
1148        struct phy_device *phydev = ndev->phydev;
1149
1150        if (!netif_running(ndev))
1151                return -EINVAL;
1152
1153        if (!phydev)
1154                return -ENODEV;
1155
1156        return phy_mii_ioctl(phydev, req, cmd);
1157}
1158
1159static int lpc_eth_open(struct net_device *ndev)
1160{
1161        struct netdata_local *pldat = netdev_priv(ndev);
1162        int ret;
1163
1164        if (netif_msg_ifup(pldat))
1165                dev_dbg(&pldat->pdev->dev, "enabling %s\n", ndev->name);
1166
1167        ret = clk_prepare_enable(pldat->clk);
1168        if (ret)
1169                return ret;
1170
1171        /* Suspended PHY makes LPC ethernet core block, so resume now */
1172        phy_resume(ndev->phydev);
1173
1174        /* Reset and initialize */
1175        __lpc_eth_reset(pldat);
1176        __lpc_eth_init(pldat);
1177
1178        /* schedule a link state check */
1179        phy_start(ndev->phydev);
1180        netif_start_queue(ndev);
1181        napi_enable(&pldat->napi);
1182
1183        return 0;
1184}
1185
1186/*
1187 * Ethtool ops
1188 */
1189static void lpc_eth_ethtool_getdrvinfo(struct net_device *ndev,
1190        struct ethtool_drvinfo *info)
1191{
1192        strlcpy(info->driver, MODNAME, sizeof(info->driver));
1193        strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1194        strlcpy(info->bus_info, dev_name(ndev->dev.parent),
1195                sizeof(info->bus_info));
1196}
1197
1198static u32 lpc_eth_ethtool_getmsglevel(struct net_device *ndev)
1199{
1200        struct netdata_local *pldat = netdev_priv(ndev);
1201
1202        return pldat->msg_enable;
1203}
1204
1205static void lpc_eth_ethtool_setmsglevel(struct net_device *ndev, u32 level)
1206{
1207        struct netdata_local *pldat = netdev_priv(ndev);
1208
1209        pldat->msg_enable = level;
1210}
1211
1212static const struct ethtool_ops lpc_eth_ethtool_ops = {
1213        .get_drvinfo    = lpc_eth_ethtool_getdrvinfo,
1214        .get_msglevel   = lpc_eth_ethtool_getmsglevel,
1215        .set_msglevel   = lpc_eth_ethtool_setmsglevel,
1216        .get_link       = ethtool_op_get_link,
1217        .get_link_ksettings = phy_ethtool_get_link_ksettings,
1218        .set_link_ksettings = phy_ethtool_set_link_ksettings,
1219};
1220
1221static const struct net_device_ops lpc_netdev_ops = {
1222        .ndo_open               = lpc_eth_open,
1223        .ndo_stop               = lpc_eth_close,
1224        .ndo_start_xmit         = lpc_eth_hard_start_xmit,
1225        .ndo_set_rx_mode        = lpc_eth_set_multicast_list,
1226        .ndo_do_ioctl           = lpc_eth_ioctl,
1227        .ndo_set_mac_address    = lpc_set_mac_address,
1228        .ndo_validate_addr      = eth_validate_addr,
1229};
1230
1231static int lpc_eth_drv_probe(struct platform_device *pdev)
1232{
1233        struct device *dev = &pdev->dev;
1234        struct device_node *np = dev->of_node;
1235        struct netdata_local *pldat;
1236        struct net_device *ndev;
1237        dma_addr_t dma_handle;
1238        struct resource *res;
1239        int irq, ret;
1240        u32 tmp;
1241
1242        /* Setup network interface for RMII or MII mode */
1243        tmp = __raw_readl(LPC32XX_CLKPWR_MACCLK_CTRL);
1244        tmp &= ~LPC32XX_CLKPWR_MACCTRL_PINS_MSK;
1245        if (lpc_phy_interface_mode(dev) == PHY_INTERFACE_MODE_MII)
1246                tmp |= LPC32XX_CLKPWR_MACCTRL_USE_MII_PINS;
1247        else
1248                tmp |= LPC32XX_CLKPWR_MACCTRL_USE_RMII_PINS;
1249        __raw_writel(tmp, LPC32XX_CLKPWR_MACCLK_CTRL);
1250
1251        /* Get platform resources */
1252        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1253        irq = platform_get_irq(pdev, 0);
1254        if (!res || irq < 0) {
1255                dev_err(dev, "error getting resources.\n");
1256                ret = -ENXIO;
1257                goto err_exit;
1258        }
1259
1260        /* Allocate net driver data structure */
1261        ndev = alloc_etherdev(sizeof(struct netdata_local));
1262        if (!ndev) {
1263                dev_err(dev, "could not allocate device.\n");
1264                ret = -ENOMEM;
1265                goto err_exit;
1266        }
1267
1268        SET_NETDEV_DEV(ndev, dev);
1269
1270        pldat = netdev_priv(ndev);
1271        pldat->pdev = pdev;
1272        pldat->ndev = ndev;
1273
1274        spin_lock_init(&pldat->lock);
1275
1276        /* Save resources */
1277        ndev->irq = irq;
1278
1279        /* Get clock for the device */
1280        pldat->clk = clk_get(dev, NULL);
1281        if (IS_ERR(pldat->clk)) {
1282                dev_err(dev, "error getting clock.\n");
1283                ret = PTR_ERR(pldat->clk);
1284                goto err_out_free_dev;
1285        }
1286
1287        /* Enable network clock */
1288        ret = clk_prepare_enable(pldat->clk);
1289        if (ret)
1290                goto err_out_clk_put;
1291
1292        /* Map IO space */
1293        pldat->net_base = ioremap(res->start, resource_size(res));
1294        if (!pldat->net_base) {
1295                dev_err(dev, "failed to map registers\n");
1296                ret = -ENOMEM;
1297                goto err_out_disable_clocks;
1298        }
1299        ret = request_irq(ndev->irq, __lpc_eth_interrupt, 0,
1300                          ndev->name, ndev);
1301        if (ret) {
1302                dev_err(dev, "error requesting interrupt.\n");
1303                goto err_out_iounmap;
1304        }
1305
1306        /* Setup driver functions */
1307        ndev->netdev_ops = &lpc_netdev_ops;
1308        ndev->ethtool_ops = &lpc_eth_ethtool_ops;
1309        ndev->watchdog_timeo = msecs_to_jiffies(2500);
1310
1311        /* Get size of DMA buffers/descriptors region */
1312        pldat->dma_buff_size = (ENET_TX_DESC + ENET_RX_DESC) * (ENET_MAXF_SIZE +
1313                sizeof(struct txrx_desc_t) + sizeof(struct rx_status_t));
1314        pldat->dma_buff_base_v = 0;
1315
1316        if (use_iram_for_net(dev)) {
1317                dma_handle = LPC32XX_IRAM_BASE;
1318                if (pldat->dma_buff_size <= lpc32xx_return_iram_size())
1319                        pldat->dma_buff_base_v =
1320                                io_p2v(LPC32XX_IRAM_BASE);
1321                else
1322                        netdev_err(ndev,
1323                                "IRAM not big enough for net buffers, using SDRAM instead.\n");
1324        }
1325
1326        if (pldat->dma_buff_base_v == 0) {
1327                ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1328                if (ret)
1329                        goto err_out_free_irq;
1330
1331                pldat->dma_buff_size = PAGE_ALIGN(pldat->dma_buff_size);
1332
1333                /* Allocate a chunk of memory for the DMA ethernet buffers
1334                   and descriptors */
1335                pldat->dma_buff_base_v =
1336                        dma_alloc_coherent(dev,
1337                                           pldat->dma_buff_size, &dma_handle,
1338                                           GFP_KERNEL);
1339                if (pldat->dma_buff_base_v == NULL) {
1340                        ret = -ENOMEM;
1341                        goto err_out_free_irq;
1342                }
1343        }
1344        pldat->dma_buff_base_p = dma_handle;
1345
1346        netdev_dbg(ndev, "IO address space     :%pR\n", res);
1347        netdev_dbg(ndev, "IO address size      :%d\n", resource_size(res));
1348        netdev_dbg(ndev, "IO address (mapped)  :0x%p\n",
1349                        pldat->net_base);
1350        netdev_dbg(ndev, "IRQ number           :%d\n", ndev->irq);
1351        netdev_dbg(ndev, "DMA buffer size      :%d\n", pldat->dma_buff_size);
1352        netdev_dbg(ndev, "DMA buffer P address :0x%08x\n",
1353                        pldat->dma_buff_base_p);
1354        netdev_dbg(ndev, "DMA buffer V address :0x%p\n",
1355                        pldat->dma_buff_base_v);
1356
1357        /* Get MAC address from current HW setting (POR state is all zeros) */
1358        __lpc_get_mac(pldat, ndev->dev_addr);
1359
1360        if (!is_valid_ether_addr(ndev->dev_addr)) {
1361                const char *macaddr = of_get_mac_address(np);
1362                if (!IS_ERR(macaddr))
1363                        ether_addr_copy(ndev->dev_addr, macaddr);
1364        }
1365        if (!is_valid_ether_addr(ndev->dev_addr))
1366                eth_hw_addr_random(ndev);
1367
1368        /* Reset the ethernet controller */
1369        __lpc_eth_reset(pldat);
1370
1371        /* then shut everything down to save power */
1372        __lpc_eth_shutdown(pldat);
1373
1374        /* Set default parameters */
1375        pldat->msg_enable = NETIF_MSG_LINK;
1376
1377        /* Force an MII interface reset and clock setup */
1378        __lpc_mii_mngt_reset(pldat);
1379
1380        /* Force default PHY interface setup in chip, this will probably be
1381           changed by the PHY driver */
1382        pldat->link = 0;
1383        pldat->speed = 100;
1384        pldat->duplex = DUPLEX_FULL;
1385        __lpc_params_setup(pldat);
1386
1387        netif_napi_add(ndev, &pldat->napi, lpc_eth_poll, NAPI_WEIGHT);
1388
1389        ret = register_netdev(ndev);
1390        if (ret) {
1391                dev_err(dev, "Cannot register net device, aborting.\n");
1392                goto err_out_dma_unmap;
1393        }
1394        platform_set_drvdata(pdev, ndev);
1395
1396        ret = lpc_mii_init(pldat);
1397        if (ret)
1398                goto err_out_unregister_netdev;
1399
1400        netdev_info(ndev, "LPC mac at 0x%08x irq %d\n",
1401               res->start, ndev->irq);
1402
1403        device_init_wakeup(dev, 1);
1404        device_set_wakeup_enable(dev, 0);
1405
1406        return 0;
1407
1408err_out_unregister_netdev:
1409        unregister_netdev(ndev);
1410err_out_dma_unmap:
1411        if (!use_iram_for_net(dev) ||
1412            pldat->dma_buff_size > lpc32xx_return_iram_size())
1413                dma_free_coherent(dev, pldat->dma_buff_size,
1414                                  pldat->dma_buff_base_v,
1415                                  pldat->dma_buff_base_p);
1416err_out_free_irq:
1417        free_irq(ndev->irq, ndev);
1418err_out_iounmap:
1419        iounmap(pldat->net_base);
1420err_out_disable_clocks:
1421        clk_disable_unprepare(pldat->clk);
1422err_out_clk_put:
1423        clk_put(pldat->clk);
1424err_out_free_dev:
1425        free_netdev(ndev);
1426err_exit:
1427        pr_err("%s: not found (%d).\n", MODNAME, ret);
1428        return ret;
1429}
1430
1431static int lpc_eth_drv_remove(struct platform_device *pdev)
1432{
1433        struct net_device *ndev = platform_get_drvdata(pdev);
1434        struct netdata_local *pldat = netdev_priv(ndev);
1435
1436        unregister_netdev(ndev);
1437
1438        if (!use_iram_for_net(&pldat->pdev->dev) ||
1439            pldat->dma_buff_size > lpc32xx_return_iram_size())
1440                dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
1441                                  pldat->dma_buff_base_v,
1442                                  pldat->dma_buff_base_p);
1443        free_irq(ndev->irq, ndev);
1444        iounmap(pldat->net_base);
1445        mdiobus_unregister(pldat->mii_bus);
1446        mdiobus_free(pldat->mii_bus);
1447        clk_disable_unprepare(pldat->clk);
1448        clk_put(pldat->clk);
1449        free_netdev(ndev);
1450
1451        return 0;
1452}
1453
1454#ifdef CONFIG_PM
1455static int lpc_eth_drv_suspend(struct platform_device *pdev,
1456        pm_message_t state)
1457{
1458        struct net_device *ndev = platform_get_drvdata(pdev);
1459        struct netdata_local *pldat = netdev_priv(ndev);
1460
1461        if (device_may_wakeup(&pdev->dev))
1462                enable_irq_wake(ndev->irq);
1463
1464        if (ndev) {
1465                if (netif_running(ndev)) {
1466                        netif_device_detach(ndev);
1467                        __lpc_eth_shutdown(pldat);
1468                        clk_disable_unprepare(pldat->clk);
1469
1470                        /*
1471                         * Reset again now clock is disable to be sure
1472                         * EMC_MDC is down
1473                         */
1474                        __lpc_eth_reset(pldat);
1475                }
1476        }
1477
1478        return 0;
1479}
1480
1481static int lpc_eth_drv_resume(struct platform_device *pdev)
1482{
1483        struct net_device *ndev = platform_get_drvdata(pdev);
1484        struct netdata_local *pldat;
1485
1486        if (device_may_wakeup(&pdev->dev))
1487                disable_irq_wake(ndev->irq);
1488
1489        if (ndev) {
1490                if (netif_running(ndev)) {
1491                        pldat = netdev_priv(ndev);
1492
1493                        /* Enable interface clock */
1494                        clk_enable(pldat->clk);
1495
1496                        /* Reset and initialize */
1497                        __lpc_eth_reset(pldat);
1498                        __lpc_eth_init(pldat);
1499
1500                        netif_device_attach(ndev);
1501                }
1502        }
1503
1504        return 0;
1505}
1506#endif
1507
1508static const struct of_device_id lpc_eth_match[] = {
1509        { .compatible = "nxp,lpc-eth" },
1510        { }
1511};
1512MODULE_DEVICE_TABLE(of, lpc_eth_match);
1513
1514static struct platform_driver lpc_eth_driver = {
1515        .probe          = lpc_eth_drv_probe,
1516        .remove         = lpc_eth_drv_remove,
1517#ifdef CONFIG_PM
1518        .suspend        = lpc_eth_drv_suspend,
1519        .resume         = lpc_eth_drv_resume,
1520#endif
1521        .driver         = {
1522                .name   = MODNAME,
1523                .of_match_table = lpc_eth_match,
1524        },
1525};
1526
1527module_platform_driver(lpc_eth_driver);
1528
1529MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
1530MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
1531MODULE_DESCRIPTION("LPC Ethernet Driver");
1532MODULE_LICENSE("GPL");
1533