lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright © 2015-2016, Shuge
 *        Author: Sugar <shugeLinux@gmail.com>
 *
 * This file is provided under a dual BSD/GPL license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
 * GNU General Public License for more details.
 *
 */
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
 
#include <asm/io.h>
#include <asm/irq.h>
 
#include <linux/mfd/acx00-mfd.h>
#include <linux/sunxi-sid.h>
 
#define EXTEPHY_CTRL0 0x0014
#define EXTEPHY_CTRL1 0x0016
 
#define EPHY_CTRL 0x6000
#define EPHY_SID 0x8004
 
#define WAIT_MAX_COUNT 200
 
atomic_t ephy_en;
 
struct ephy_res {
   struct device_driver *plat_drv;
   struct device_driver *phy_drv;
   struct phy_device *phydev;
   struct acx00 *acx;
};
 
static struct ephy_res ephy_priv;
 
int ephy_is_enable(void)
{
   return atomic_read(&ephy_en);
}
EXPORT_SYMBOL_GPL(ephy_is_enable);
 
/**
 * @name    ephy_read_sid
 * @brief        read ephy sid from efuse
 * @param[IN]    none
 * @param[OUT]    p_ephy_cali: ephy calibration value
 * @return    return 0 if success,-value if fail
 */
static __attribute__((unused)) s32 ephy_read_sid(u16 *p_ephy_cali)
{
   s32 ret = 0;
   u8 buf[6];
 
   if (!p_ephy_cali) {
       pr_info("%s's pointer type args are NULL!\n", __func__);
       return -1;
   }
   ret = sunxi_efuse_readn(EFUSE_OEM_NAME, buf, 6);
   if (ret != 0) {
       pr_info("sunxi_efuse_readn failed:%d\n", ret);
       return ret;
   }
   *p_ephy_cali = buf[0] + (buf[1] << 8);
 
   return ret;
}
 
#if 0
static int ephy_reset(struct phy_device *phydev)
{
   int bmcr;
 
   /* Software Reset PHY */
   bmcr = phy_read(phydev, MII_BMCR);
   if (bmcr < 0)
       return bmcr;
 
   bmcr |= BMCR_RESET;
   bmcr = phy_write(phydev, MII_BMCR, bmcr);
   if (bmcr < 0)
       return bmcr;
 
   do {
       bmcr = phy_read(phydev, MII_BMCR);
       if (bmcr < 0)
           return bmcr;
   } while (bmcr & BMCR_RESET);
 
   return 0;
}
#endif
 
static void disable_intelligent_ieee(struct phy_device *phydev)
{
   unsigned int value;
 
   phy_write(phydev, 0x1f, 0x0100);    /* switch to page 1 */
   value = phy_read(phydev, 0x17);        /* read address 0 0x17 register */
   value &= ~(1 << 3);            /* reg 0x17 bit 3, set 0 to disable IEEE */
   phy_write(phydev, 0x17, value);
   phy_write(phydev, 0x1f, 0x0000);    /* switch to page 0 */
}
 
static void disable_802_3az_ieee(struct phy_device *phydev)
{
   unsigned int value;
 
   phy_write(phydev, 0xd, 0x7);
   phy_write(phydev, 0xe, 0x3c);
   phy_write(phydev, 0xd, 0x1 << 14 | 0x7);
   value = phy_read(phydev, 0xe);
   value &= ~(0x1 << 1);
   phy_write(phydev, 0xd, 0x7);
   phy_write(phydev, 0xe, 0x3c);
   phy_write(phydev, 0xd, 0x1 << 14 | 0x7);
   phy_write(phydev, 0xe, value);
 
   phy_write(phydev, 0x1f, 0x0200);    /* switch to page 2 */
   phy_write(phydev, 0x18, 0x0000);
}
 
static int ephy_config_init(struct phy_device *phydev)
{
   int value;
 
   /* Iint ephy */
   phy_write(phydev, 0x1f, 0x0100);    /* Switch to Page 1 */
   phy_write(phydev, 0x12, 0x4824);    /* Disable APS */
 
   phy_write(phydev, 0x1f, 0x0200);    /* Switch to Page 2 */
   phy_write(phydev, 0x18, 0x0000);    /* PHYAFE TRX optimization */
 
   phy_write(phydev, 0x1f, 0x0600);    /* Switch to Page 6 */
   phy_write(phydev, 0x14, 0x708b);    /* PHYAFE TX optimization */
   phy_write(phydev, 0x13, 0xF000);    /* PHYAFE RX optimization */
   phy_write(phydev, 0x15, 0x1530);
 
   phy_write(phydev, 0x1f, 0x0800);    /* Switch to Page 6 */
   phy_write(phydev, 0x18, 0x00bc);    /* PHYAFE TRX optimization */
#if 0
   /* Disable Auto Power Saving mode */
   phy_write(phydev, 0x1f, 0x0100);    /* Switch to Page 1 */
   value = phy_read(phydev, 0x17);
   value &= ~BIT(13);
   phy_write(phydev, 0x17, value);
#endif
   disable_intelligent_ieee(phydev);    /* Disable Intelligent IEEE */
   disable_802_3az_ieee(phydev);        /* Disable 802.3az IEEE */
   phy_write(phydev, 0x1f, 0x0000);    /* Switch to Page 0 */
 
#ifdef CONFIG_MFD_ACX00
   value = acx00_reg_read(ephy_priv.acx, EPHY_CTRL);
   if (phydev->interface == PHY_INTERFACE_MODE_RMII)
       value |= (1 << 11);
   else
       value &= (~(1 << 11));
   acx00_reg_write(ephy_priv.acx, EPHY_CTRL, value | (1 << 11));
#endif
 
#if defined(CONFIG_ARCH_SUN50IW6)
   value = phy_read(phydev, 0x13);
   value |= 1 << 12;
   phy_write(phydev, 0x13, value);
#endif
 
   return 0;
}
 
static int ephy_probe(struct phy_device *phydev)
{
   struct phy_driver *drv;
 
   if (!phydev)
       return -ENODEV;
 
   drv = phydev->drv;
   ephy_priv.phy_drv = &drv->mdiodrv.driver;
   ephy_priv.phydev = phydev;
   return 0;
}
 
#if 0
static int ephy_ack_interrupt(struct phy_device *phydev)
{
   int err = phy_read(phydev, IP101A_G_IRQ_CONF_STATUS);
 
   if (err < 0)
       return err;
 
   return 0;
}
#endif
 
static void sunxi_ephy_enable(struct ephy_res *priv)
{
#ifdef CONFIG_MFD_ACX00
   int value;
   unsigned char i = 0;
#if defined(CONFIG_ARCH_SUN50IW6) || defined(CONFIG_ARCH_SUN50IW9)
   u16 ephy_cali = 0;
#endif
 
   if (!acx00_enable()) {
       for (i = 0; i < WAIT_MAX_COUNT; i++) {
           msleep(10);
           if (acx00_enable())
               break;
       }
       if (i == WAIT_MAX_COUNT) {
           pr_err("acx00 is no enable, and sunxi_ephy_enable is fail\n");
           return;
       }
   }
 
   value = acx00_reg_read(priv->acx, EXTEPHY_CTRL0);
   value |= 0x03;
   acx00_reg_write(priv->acx, EXTEPHY_CTRL0, value);
   value = acx00_reg_read(priv->acx, EXTEPHY_CTRL1);
#if defined(CONFIG_ARCH_SUN50IW9)
   /* disable link led to avoid conflict with twi2 */
   value |= 0x09;
#else
   value |= 0x0f;
#endif
   acx00_reg_write(priv->acx, EXTEPHY_CTRL1, value);
   acx00_reg_write(priv->acx, EPHY_CTRL, 0x06);
 
   /*for ephy */
   value = acx00_reg_read(priv->acx, EPHY_CTRL);
   value &= ~(0xf << 12);
 
#if defined(CONFIG_ARCH_SUN50IW6) || defined(CONFIG_ARCH_SUN50IW9)
   ephy_read_sid(&ephy_cali);
   value |= (0x0F & (0x03 + ephy_cali)) << 12;
#else
   value |= (0x0F & (0x03 + acx00_reg_read(priv->acx, EPHY_SID))) << 12;
#endif
 
   acx00_reg_write(priv->acx, EPHY_CTRL, value);
 
   atomic_set(&ephy_en, 1);
#endif
}
 
static void sunxi_ephy_disable(struct ephy_res *priv)
{
   int value;
 
   /* reset ephy */
   value = acx00_reg_read(priv->acx, EXTEPHY_CTRL0);
   value &= ~0x01;
   acx00_reg_write(priv->acx, EXTEPHY_CTRL0, value);
 
   /* shutdown ephy */
   value = acx00_reg_read(priv->acx, EPHY_CTRL);
   value |= 0x01;
   acx00_reg_write(priv->acx, EPHY_CTRL, value);
}
 
static struct phy_driver ephy_driver = {
   .phy_id        = 0x00441400,
   .name        = "ephy",
   .phy_id_mask    = 0x0ffffff0,
   .features    = PHY_BASIC_FEATURES | SUPPORTED_Pause |
             SUPPORTED_Asym_Pause,
#if 0
   .flags        = PHY_HAS_INTERRUPT,
   .ack_interrupt    = ephy_ack_interrupt,
#endif
   .config_init    = &ephy_config_init,
   .config_aneg    = &genphy_config_aneg,
   .read_status    = &genphy_read_status,
   .suspend    = genphy_suspend,
   .resume        = genphy_resume,
   .probe        = ephy_probe,
};
 
static const struct platform_device_id sunxi_ephy_id[] = {
   { "acx-ephy", 0},
   { },
};
MODULE_DEVICE_TABLE(platform, sunxi_ephy_id);
 
static int ephy_plat_probe(struct platform_device *pdev)
{
   struct acx00 *ax = dev_get_drvdata(pdev->dev.parent);
 
   if (!ax)
       return -ENODEV;
 
   ephy_priv.acx = ax;
   platform_set_drvdata(pdev, &ephy_priv);
   ephy_priv.plat_drv = pdev->dev.driver;
 
   atomic_set(&ephy_en, 0);
 
   sunxi_ephy_enable(&ephy_priv);
 
   return 0;
}
 
static int ephy_plat_remove(struct platform_device *pdev)
{
   return 0;
}
 
static int sunxi_phy_suspend(struct device *dev)
{
   sunxi_ephy_disable(&ephy_priv);
   atomic_set(&ephy_en, 0);
 
   return 0;
}
 
static int sunxi_phy_resume(struct device *dev)
{
   sunxi_ephy_enable(&ephy_priv);
 
   return 0;
}
 
/* Suspend hook structures */
static const struct dev_pm_ops sunxi_phy_pm_ops = {
   .suspend = sunxi_phy_suspend,
   .resume = sunxi_phy_resume,
};
 
static struct platform_driver ephy_plat_driver = {
   .driver = {
       .name = "acx-ephy",
       .owner = THIS_MODULE,
       .pm = &sunxi_phy_pm_ops,
   },
   .probe = ephy_plat_probe,
   .remove = ephy_plat_remove,
   .id_table = sunxi_ephy_id,
};
 
static int ephy_init(void)
{
   int ret = 0;
 
   ret = platform_driver_register(&ephy_plat_driver);
   if (ret)
       return -EINVAL;
 
   ret = phy_driver_register(&ephy_driver, THIS_MODULE);
   if (ret)
       platform_driver_unregister(&ephy_plat_driver);
 
   return ret;
}
 
static void ephy_exit(void)
{
   if (ephy_priv.plat_drv)
       platform_driver_unregister(&ephy_plat_driver);
 
   phy_driver_unregister(&ephy_driver);
}
 
module_init(ephy_init);
module_exit(ephy_exit);
 
static struct mdio_device_id __maybe_unused ephy_tbl[] = {
   { 0x00441400, 0x0ffffff0 },
   { }
};
 
MODULE_DEVICE_TABLE(mdio, ephy_tbl);
 
MODULE_DESCRIPTION("Allwinner EPHY drivers");
MODULE_AUTHOR("Sugar <shugeLinux@gmail.com>");
MODULE_LICENSE("GPL");