blob: c824c3da3ddbf6c8369ee1f8b9e7f8a35c5e9893 [file] [log] [blame]
Robert Marko90dfaeb2020-10-08 22:05:11 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Qualcomm IPQ4019 MDIO driver
4 *
5 * Copyright (c) 2020 Sartura Ltd.
6 *
7 * Author: Luka Kovacic <luka.kovacic@sartura.hr>
8 * Author: Robert Marko <robert.marko@sartura.hr>
9 *
10 * Based on Linux driver
11 */
12
13#include <asm/io.h>
Robert Marko90dfaeb2020-10-08 22:05:11 +020014#include <dm.h>
15#include <errno.h>
16#include <linux/bitops.h>
17#include <linux/iopoll.h>
18#include <miiphy.h>
19#include <phy.h>
20
21#define MDIO_MODE_REG 0x40
22#define MDIO_ADDR_REG 0x44
23#define MDIO_DATA_WRITE_REG 0x48
24#define MDIO_DATA_READ_REG 0x4c
25#define MDIO_CMD_REG 0x50
26#define MDIO_CMD_ACCESS_BUSY BIT(16)
27#define MDIO_CMD_ACCESS_START BIT(8)
28#define MDIO_CMD_ACCESS_CODE_READ 0
29#define MDIO_CMD_ACCESS_CODE_WRITE 1
30
31/* 0 = Clause 22, 1 = Clause 45 */
32#define MDIO_MODE_BIT BIT(8)
33
34#define IPQ4019_MDIO_TIMEOUT 10000
35#define IPQ4019_MDIO_SLEEP 10
36
37struct ipq4019_mdio_priv {
38 phys_addr_t mdio_base;
39};
40
41static int ipq4019_mdio_wait_busy(struct ipq4019_mdio_priv *priv)
42{
43 unsigned int busy;
44
45 return readl_poll_sleep_timeout(priv->mdio_base + MDIO_CMD_REG, busy,
46 (busy & MDIO_CMD_ACCESS_BUSY) == 0, IPQ4019_MDIO_SLEEP,
47 IPQ4019_MDIO_TIMEOUT);
48}
49
50int ipq4019_mdio_read(struct udevice *dev, int addr, int devad, int reg)
51{
52 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
53 unsigned int cmd;
54
55 if (ipq4019_mdio_wait_busy(priv))
56 return -ETIMEDOUT;
57
58 /* Issue the phy address and reg */
59 writel((addr << 8) | reg, priv->mdio_base + MDIO_ADDR_REG);
60
61 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_READ;
62
63 /* Issue read command */
64 writel(cmd, priv->mdio_base + MDIO_CMD_REG);
65
66 /* Wait read complete */
67 if (ipq4019_mdio_wait_busy(priv))
68 return -ETIMEDOUT;
69
70 /* Read and return data */
71 return readl(priv->mdio_base + MDIO_DATA_READ_REG);
72}
73
74int ipq4019_mdio_write(struct udevice *dev, int addr, int devad,
75 int reg, u16 val)
76{
77 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
78 unsigned int cmd;
79
80 if (ipq4019_mdio_wait_busy(priv))
81 return -ETIMEDOUT;
82
83 /* Issue the phy addreass and reg */
84 writel((addr << 8) | reg, priv->mdio_base + MDIO_ADDR_REG);
85
86 /* Issue write data */
87 writel(val, priv->mdio_base + MDIO_DATA_WRITE_REG);
88
89 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_WRITE;
90
91 /* Issue write command */
92 writel(cmd, priv->mdio_base + MDIO_CMD_REG);
93
94 /* Wait for write complete */
95
96 if (ipq4019_mdio_wait_busy(priv))
97 return -ETIMEDOUT;
98
99 return 0;
100}
101
102static const struct mdio_ops ipq4019_mdio_ops = {
103 .read = ipq4019_mdio_read,
104 .write = ipq4019_mdio_write,
105};
106
107static int ipq4019_mdio_bind(struct udevice *dev)
108{
Simon Glassa7ece582020-12-19 10:40:14 -0700109 if (ofnode_valid(dev_ofnode(dev)))
110 device_set_name(dev, ofnode_get_name(dev_ofnode(dev)));
Robert Marko90dfaeb2020-10-08 22:05:11 +0200111
112 return 0;
113}
114
115static int ipq4019_mdio_probe(struct udevice *dev)
116{
117 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
118 unsigned int data;
119
120 priv->mdio_base = dev_read_addr(dev);
121 if (priv->mdio_base == FDT_ADDR_T_NONE)
122 return -EINVAL;
123
124 /* Enter Clause 22 mode */
125 data = readl(priv->mdio_base + MDIO_MODE_REG);
126 data &= ~MDIO_MODE_BIT;
127 writel(data, priv->mdio_base + MDIO_MODE_REG);
128
129 return 0;
130}
131
132static const struct udevice_id ipq4019_mdio_ids[] = {
133 { .compatible = "qcom,ipq4019-mdio", },
134 { }
135};
136
137U_BOOT_DRIVER(ipq4019_mdio) = {
138 .name = "ipq4019_mdio",
139 .id = UCLASS_MDIO,
140 .of_match = ipq4019_mdio_ids,
141 .bind = ipq4019_mdio_bind,
142 .probe = ipq4019_mdio_probe,
143 .ops = &ipq4019_mdio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700144 .priv_auto = sizeof(struct ipq4019_mdio_priv),
Robert Marko90dfaeb2020-10-08 22:05:11 +0200145};