Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 2 | /* |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 3 | * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc. |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 4 | * Jun-jie Zhang <b18070@freescale.com> |
| 5 | * Mingkai Hu <Mingkai.hu@freescale.com> |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 6 | */ |
Bin Meng | 79cd33a | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 7 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <miiphy.h> |
| 10 | #include <phy.h> |
| 11 | #include <fsl_mdio.h> |
| 12 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 14 | |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 15 | void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr, |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 16 | int dev_addr, int regnum, int value) |
| 17 | { |
| 18 | int timeout = 1000000; |
| 19 | |
| 20 | out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f)); |
| 21 | out_be32(&phyregs->miimcon, value); |
Alison Wang | 884f1c1 | 2014-09-05 13:52:37 +0800 | [diff] [blame] | 22 | /* Memory barrier */ |
| 23 | mb(); |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 24 | |
| 25 | while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--) |
| 26 | ; |
| 27 | } |
| 28 | |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 29 | int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr, |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 30 | int dev_addr, int regnum) |
| 31 | { |
| 32 | int value; |
| 33 | int timeout = 1000000; |
| 34 | |
Bin Meng | 79cd33a | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 35 | /* Put the address of the phy, and the register number into MIIMADD */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 36 | out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f)); |
| 37 | |
| 38 | /* Clear the command register, and wait */ |
| 39 | out_be32(&phyregs->miimcom, 0); |
Alison Wang | 884f1c1 | 2014-09-05 13:52:37 +0800 | [diff] [blame] | 40 | /* Memory barrier */ |
| 41 | mb(); |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 42 | |
| 43 | /* Initiate a read command, and wait */ |
| 44 | out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE); |
Alison Wang | 884f1c1 | 2014-09-05 13:52:37 +0800 | [diff] [blame] | 45 | /* Memory barrier */ |
| 46 | mb(); |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 47 | |
| 48 | /* Wait for the the indication that the read is done */ |
| 49 | while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY)) |
| 50 | && timeout--) |
| 51 | ; |
| 52 | |
| 53 | /* Grab the value read from the PHY */ |
| 54 | value = in_be32(&phyregs->miimstat); |
| 55 | |
| 56 | return value; |
| 57 | } |
| 58 | |
| 59 | static int fsl_pq_mdio_reset(struct mii_dev *bus) |
| 60 | { |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 61 | struct tsec_mii_mng __iomem *regs = |
| 62 | (struct tsec_mii_mng __iomem *)bus->priv; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 63 | |
| 64 | /* Reset MII (due to new addresses) */ |
| 65 | out_be32(®s->miimcfg, MIIMCFG_RESET_MGMT); |
| 66 | |
| 67 | out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE); |
| 68 | |
| 69 | while (in_be32(®s->miimind) & MIIMIND_BUSY) |
| 70 | ; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum) |
| 76 | { |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 77 | struct tsec_mii_mng __iomem *phyregs = |
| 78 | (struct tsec_mii_mng __iomem *)bus->priv; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 79 | |
| 80 | return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum); |
| 81 | } |
| 82 | |
| 83 | int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum, |
| 84 | u16 value) |
| 85 | { |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 86 | struct tsec_mii_mng __iomem *phyregs = |
| 87 | (struct tsec_mii_mng __iomem *)bus->priv; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 88 | |
| 89 | tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info) |
| 95 | { |
| 96 | struct mii_dev *bus = mdio_alloc(); |
| 97 | |
| 98 | if (!bus) { |
| 99 | printf("Failed to allocate FSL MDIO bus\n"); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | bus->read = tsec_phy_read; |
| 104 | bus->write = tsec_phy_write; |
| 105 | bus->reset = fsl_pq_mdio_reset; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 106 | strcpy(bus->name, info->name); |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 107 | |
Claudiu Manoil | 401b94f | 2013-09-30 12:44:43 +0300 | [diff] [blame] | 108 | bus->priv = (void *)info->regs; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 109 | |
| 110 | return mdio_register(bus); |
| 111 | } |