Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2009-2011 Freescale Semiconductor, Inc. |
Andy Fleming | b36a4d4 | 2014-07-25 17:39:08 -0500 | [diff] [blame] | 4 | * Andy Fleming <afleming@gmail.com> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 5 | * Some part is taken from tsec.c |
| 6 | */ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 7 | #include <miiphy.h> |
| 8 | #include <phy.h> |
| 9 | #include <asm/io.h> |
Shaohui Xie | 513eaf2 | 2015-10-26 19:47:47 +0800 | [diff] [blame] | 10 | #include <fsl_tgec.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 11 | #include <fm_eth.h> |
| 12 | |
| 13 | /* |
| 14 | * Write value to the PHY for this device to the register at regnum, waiting |
| 15 | * until the write is done before it returns. All PHY configuration has to be |
| 16 | * done through the TSEC1 MIIM regs |
| 17 | */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 18 | static int tgec_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr, |
| 19 | int regnum, u16 value) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 20 | { |
| 21 | u32 mdio_ctl; |
| 22 | u32 stat_val; |
| 23 | struct tgec_mdio_controller *regs = bus->priv; |
| 24 | |
| 25 | if (dev_addr == MDIO_DEVAD_NONE) |
| 26 | return 0; |
| 27 | |
| 28 | /* Wait till the bus is free */ |
| 29 | stat_val = MDIO_STAT_CLKDIV(100); |
| 30 | out_be32(®s->mdio_stat, stat_val); |
| 31 | while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY) |
| 32 | ; |
| 33 | |
| 34 | /* Set the port and dev addr */ |
| 35 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
| 36 | out_be32(®s->mdio_ctl, mdio_ctl); |
| 37 | |
| 38 | /* Set the register address */ |
| 39 | out_be32(®s->mdio_addr, regnum & 0xffff); |
| 40 | |
| 41 | /* Wait till the bus is free */ |
| 42 | while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY) |
| 43 | ; |
| 44 | |
| 45 | /* Write the value to the register */ |
| 46 | out_be32(®s->mdio_data, MDIO_DATA(value)); |
| 47 | |
| 48 | /* Wait till the MDIO write is complete */ |
| 49 | while ((in_be32(®s->mdio_data)) & MDIO_DATA_BSY) |
| 50 | ; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Reads from register regnum in the PHY for device dev, returning the value. |
| 57 | * Clears miimcom first. All PHY configuration has to be done through the |
| 58 | * TSEC1 MIIM regs |
| 59 | */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 60 | static int tgec_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr, |
| 61 | int regnum) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 62 | { |
| 63 | u32 mdio_ctl; |
| 64 | u32 stat_val; |
| 65 | struct tgec_mdio_controller *regs = bus->priv; |
| 66 | |
| 67 | if (dev_addr == MDIO_DEVAD_NONE) |
| 68 | return 0xffff; |
| 69 | |
| 70 | stat_val = MDIO_STAT_CLKDIV(100); |
| 71 | out_be32(®s->mdio_stat, stat_val); |
| 72 | /* Wait till the bus is free */ |
| 73 | while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY) |
| 74 | ; |
| 75 | |
| 76 | /* Set the Port and Device Addrs */ |
| 77 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
| 78 | out_be32(®s->mdio_ctl, mdio_ctl); |
| 79 | |
| 80 | /* Set the register address */ |
| 81 | out_be32(®s->mdio_addr, regnum & 0xffff); |
| 82 | |
| 83 | /* Wait till the bus is free */ |
| 84 | while ((in_be32(®s->mdio_stat)) & MDIO_STAT_BSY) |
| 85 | ; |
| 86 | |
| 87 | /* Initiate the read */ |
| 88 | mdio_ctl |= MDIO_CTL_READ; |
| 89 | out_be32(®s->mdio_ctl, mdio_ctl); |
| 90 | |
| 91 | /* Wait till the MDIO write is complete */ |
| 92 | while ((in_be32(®s->mdio_data)) & MDIO_DATA_BSY) |
| 93 | ; |
| 94 | |
| 95 | /* Return all Fs if nothing was there */ |
| 96 | if (in_be32(®s->mdio_stat) & MDIO_STAT_RD_ER) |
| 97 | return 0xffff; |
| 98 | |
| 99 | return in_be32(®s->mdio_data) & 0xffff; |
| 100 | } |
| 101 | |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 102 | static int tgec_mdio_reset(struct mii_dev *bus) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 103 | { |
| 104 | return 0; |
| 105 | } |
| 106 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 107 | int fm_tgec_mdio_init(struct bd_info *bis, struct tgec_mdio_info *info) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 108 | { |
| 109 | struct mii_dev *bus = mdio_alloc(); |
| 110 | |
| 111 | if (!bus) { |
| 112 | printf("Failed to allocate FM TGEC MDIO bus\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | bus->read = tgec_mdio_read; |
| 117 | bus->write = tgec_mdio_write; |
| 118 | bus->reset = tgec_mdio_reset; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 119 | strcpy(bus->name, info->name); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 120 | |
| 121 | bus->priv = info->regs; |
| 122 | |
| 123 | return mdio_register(bus); |
| 124 | } |