Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
Andy Fleming | b36a4d4 | 2014-07-25 17:39:08 -0500 | [diff] [blame] | 3 | * Andy Fleming <afleming@gmail.com> |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 4 | * Roy Zang <tie-fei.zang@freescale.com> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 7 | * Some part is taken from tsec.c |
| 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <miiphy.h> |
| 11 | #include <phy.h> |
| 12 | #include <asm/io.h> |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 13 | #include <fsl_memac.h> |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 14 | #include <fm_eth.h> |
| 15 | |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 16 | #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN |
| 17 | #define memac_out_32(a, v) out_le32(a, v) |
| 18 | #define memac_clrbits_32(a, v) clrbits_le32(a, v) |
| 19 | #define memac_setbits_32(a, v) setbits_le32(a, v) |
| 20 | #else |
| 21 | #define memac_out_32(a, v) out_be32(a, v) |
| 22 | #define memac_clrbits_32(a, v) clrbits_be32(a, v) |
| 23 | #define memac_setbits_32(a, v) setbits_be32(a, v) |
| 24 | #endif |
| 25 | |
| 26 | static u32 memac_in_32(u32 *reg) |
| 27 | { |
| 28 | #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN |
| 29 | return in_le32(reg); |
| 30 | #else |
| 31 | return in_be32(reg); |
| 32 | #endif |
| 33 | } |
| 34 | |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Write value to the PHY for this device to the register at regnum, waiting |
| 37 | * until the write is done before it returns. All PHY configuration has to be |
| 38 | * done through the TSEC1 MIIM regs |
| 39 | */ |
| 40 | int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr, |
| 41 | int regnum, u16 value) |
| 42 | { |
| 43 | u32 mdio_ctl; |
| 44 | struct memac_mdio_controller *regs = bus->priv; |
| 45 | u32 c45 = 1; /* Default to 10G interface */ |
| 46 | |
| 47 | if (dev_addr == MDIO_DEVAD_NONE) { |
| 48 | c45 = 0; /* clause 22 */ |
| 49 | dev_addr = regnum & 0x1f; |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 50 | memac_clrbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Shaohui Xie | adbcb9c | 2014-04-22 18:21:37 +0800 | [diff] [blame] | 51 | } else |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 52 | memac_setbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 53 | |
| 54 | /* Wait till the bus is free */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 55 | while ((memac_in_32(®s->mdio_stat)) & MDIO_STAT_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 56 | ; |
| 57 | |
| 58 | /* Set the port and dev addr */ |
| 59 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 60 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 61 | |
| 62 | /* Set the register address */ |
| 63 | if (c45) |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 64 | memac_out_32(®s->mdio_addr, regnum & 0xffff); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 65 | |
| 66 | /* Wait till the bus is free */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 67 | while ((memac_in_32(®s->mdio_stat)) & MDIO_STAT_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 68 | ; |
| 69 | |
| 70 | /* Write the value to the register */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 71 | memac_out_32(®s->mdio_data, MDIO_DATA(value)); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 72 | |
| 73 | /* Wait till the MDIO write is complete */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 74 | while ((memac_in_32(®s->mdio_data)) & MDIO_DATA_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 75 | ; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Reads from register regnum in the PHY for device dev, returning the value. |
| 82 | * Clears miimcom first. All PHY configuration has to be done through the |
| 83 | * TSEC1 MIIM regs |
| 84 | */ |
| 85 | int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr, |
| 86 | int regnum) |
| 87 | { |
| 88 | u32 mdio_ctl; |
| 89 | struct memac_mdio_controller *regs = bus->priv; |
| 90 | u32 c45 = 1; |
| 91 | |
| 92 | if (dev_addr == MDIO_DEVAD_NONE) { |
Shaohui Xie | 182fea2 | 2014-08-13 18:32:19 +0800 | [diff] [blame] | 93 | if (!strcmp(bus->name, DEFAULT_FM_TGEC_MDIO_NAME)) |
| 94 | return 0xffff; |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 95 | c45 = 0; /* clause 22 */ |
| 96 | dev_addr = regnum & 0x1f; |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 97 | memac_clrbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Shaohui Xie | adbcb9c | 2014-04-22 18:21:37 +0800 | [diff] [blame] | 98 | } else |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 99 | memac_setbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 100 | |
| 101 | /* Wait till the bus is free */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 102 | while ((memac_in_32(®s->mdio_stat)) & MDIO_STAT_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 103 | ; |
| 104 | |
| 105 | /* Set the Port and Device Addrs */ |
| 106 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 107 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 108 | |
| 109 | /* Set the register address */ |
| 110 | if (c45) |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 111 | memac_out_32(®s->mdio_addr, regnum & 0xffff); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 112 | |
| 113 | /* Wait till the bus is free */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 114 | while ((memac_in_32(®s->mdio_stat)) & MDIO_STAT_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 115 | ; |
| 116 | |
| 117 | /* Initiate the read */ |
| 118 | mdio_ctl |= MDIO_CTL_READ; |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 119 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 120 | |
| 121 | /* Wait till the MDIO write is complete */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 122 | while ((memac_in_32(®s->mdio_data)) & MDIO_DATA_BSY) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 123 | ; |
| 124 | |
| 125 | /* Return all Fs if nothing was there */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 126 | if (memac_in_32(®s->mdio_stat) & MDIO_STAT_RD_ER) |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 127 | return 0xffff; |
| 128 | |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 129 | return memac_in_32(®s->mdio_data) & 0xffff; |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int memac_mdio_reset(struct mii_dev *bus) |
| 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info) |
| 138 | { |
| 139 | struct mii_dev *bus = mdio_alloc(); |
| 140 | |
| 141 | if (!bus) { |
| 142 | printf("Failed to allocate FM TGEC MDIO bus\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | bus->read = memac_mdio_read; |
| 147 | bus->write = memac_mdio_write; |
| 148 | bus->reset = memac_mdio_reset; |
| 149 | sprintf(bus->name, info->name); |
| 150 | |
| 151 | bus->priv = info->regs; |
| 152 | |
Priyanka Jain | 845226e | 2014-04-08 10:55:49 +0530 | [diff] [blame] | 153 | /* |
| 154 | * On some platforms like B4860, default value of MDIO_CLK_DIV bits |
| 155 | * in mdio_stat(mdio_cfg) register generates MDIO clock too high |
| 156 | * (much higher than 2.5MHz), violating the IEEE specs. |
| 157 | * On other platforms like T1040, default value of MDIO_CLK_DIV bits |
| 158 | * is zero, so MDIO clock is disabled. |
| 159 | * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to |
| 160 | * be properly initialized. |
Shaohui Xie | b583b4c | 2014-08-13 18:38:09 +0800 | [diff] [blame] | 161 | * NEG bit default should be '1' as per FMAN-v3 RM, but on platform |
| 162 | * like T2080QDS, this bit default is '0', which leads to MDIO failure |
| 163 | * on XAUI PHY, so set this bit definitely. |
Priyanka Jain | 845226e | 2014-04-08 10:55:49 +0530 | [diff] [blame] | 164 | */ |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame^] | 165 | memac_setbits_32( |
| 166 | &((struct memac_mdio_controller *)info->regs)->mdio_stat, |
| 167 | MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG); |
Priyanka Jain | 845226e | 2014-04-08 10:55:49 +0530 | [diff] [blame] | 168 | |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 169 | return mdio_register(bus); |
| 170 | } |