blob: 6ae5c40fa36af741b184a77b326968cbcdcc1984 [file] [log] [blame]
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +05301/*
2 * pic32_mdio.c: PIC32 MDIO/MII driver, part of pic32_eth.c.
3 *
4 * Copyright 2015 Microchip Inc.
5 * Purna Chandra Mandal <purna.mandal@microchip.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9#include <common.h>
10#include <phy.h>
11#include <miiphy.h>
12#include <errno.h>
13#include <wait_bit.h>
14#include <asm/io.h>
15#include "pic32_eth.h"
16
17static int pic32_mdio_write(struct mii_dev *bus,
18 int addr, int dev_addr,
19 int reg, u16 value)
20{
21 u32 v;
22 struct pic32_mii_regs *mii_regs = bus->priv;
23
24 /* Wait for the previous operation to finish */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010025 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
26 false, CONFIG_SYS_HZ, true);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053027
28 /* Put phyaddr and regaddr into MIIMADD */
29 v = (addr << MIIMADD_PHYADDR_SHIFT) | (reg & MIIMADD_REGADDR);
30 writel(v, &mii_regs->madr.raw);
31
32 /* Initiate a write command */
33 writel(value, &mii_regs->mwtd.raw);
34
35 /* Wait 30 clock cycles for busy flag to be set */
36 udelay(12);
37
38 /* Wait for write to complete */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010039 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
40 false, CONFIG_SYS_HZ, true);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053041
42 return 0;
43}
44
45static int pic32_mdio_read(struct mii_dev *bus, int addr, int devaddr, int reg)
46{
47 u32 v;
48 struct pic32_mii_regs *mii_regs = bus->priv;
49
50 /* Wait for the previous operation to finish */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010051 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
52 false, CONFIG_SYS_HZ, true);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053053
54 /* Put phyaddr and regaddr into MIIMADD */
55 v = (addr << MIIMADD_PHYADDR_SHIFT) | (reg & MIIMADD_REGADDR);
56 writel(v, &mii_regs->madr.raw);
57
58 /* Initiate a read command */
59 writel(MIIMCMD_READ, &mii_regs->mcmd.raw);
60
61 /* Wait 30 clock cycles for busy flag to be set */
62 udelay(12);
63
64 /* Wait for read to complete */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010065 wait_for_bit_le32(&mii_regs->mind.raw,
66 MIIMIND_NOTVALID | MIIMIND_BUSY,
67 false, CONFIG_SYS_HZ, false);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053068
69 /* Clear the command register */
70 writel(0, &mii_regs->mcmd.raw);
71
72 /* Grab the value read from the PHY */
73 v = readl(&mii_regs->mrdd.raw);
74 return v;
75}
76
77static int pic32_mdio_reset(struct mii_dev *bus)
78{
79 struct pic32_mii_regs *mii_regs = bus->priv;
80
81 /* Reset MII (due to new addresses) */
82 writel(MIIMCFG_RSTMGMT, &mii_regs->mcfg.raw);
83
84 /* Wait for the operation to finish */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010085 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053086 false, CONFIG_SYS_HZ, true);
87
88 /* Clear reset bit */
89 writel(0, &mii_regs->mcfg);
90
91 /* Wait for the operation to finish */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010092 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
93 false, CONFIG_SYS_HZ, true);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +053094
95 /* Set the MII Management Clock (MDC) - no faster than 2.5 MHz */
96 writel(MIIMCFG_CLKSEL_DIV40, &mii_regs->mcfg.raw);
97
98 /* Wait for the operation to finish */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010099 wait_for_bit_le32(&mii_regs->mind.raw, MIIMIND_BUSY,
100 false, CONFIG_SYS_HZ, true);
Purna Chandra Mandal6b731c92016-01-28 15:30:21 +0530101 return 0;
102}
103
104int pic32_mdio_init(const char *name, ulong ioaddr)
105{
106 struct mii_dev *bus;
107
108 bus = mdio_alloc();
109 if (!bus) {
110 printf("Failed to allocate PIC32-MDIO bus\n");
111 return -ENOMEM;
112 }
113
114 bus->read = pic32_mdio_read;
115 bus->write = pic32_mdio_write;
116 bus->reset = pic32_mdio_reset;
117 strncpy(bus->name, name, sizeof(bus->name));
118 bus->priv = (void *)ioaddr;
119
120 return mdio_register(bus);
121}