1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
200db8189SAndy Fleming /*
300db8189SAndy Fleming * drivers/net/phy/qsemi.c
400db8189SAndy Fleming *
500db8189SAndy Fleming * Driver for Quality Semiconductor PHYs
600db8189SAndy Fleming *
700db8189SAndy Fleming * Author: Andy Fleming
800db8189SAndy Fleming *
900db8189SAndy Fleming * Copyright (c) 2004 Freescale Semiconductor, Inc.
1000db8189SAndy Fleming */
1100db8189SAndy Fleming #include <linux/kernel.h>
1200db8189SAndy Fleming #include <linux/string.h>
1300db8189SAndy Fleming #include <linux/errno.h>
1400db8189SAndy Fleming #include <linux/unistd.h>
1500db8189SAndy Fleming #include <linux/interrupt.h>
1600db8189SAndy Fleming #include <linux/init.h>
1700db8189SAndy Fleming #include <linux/delay.h>
1800db8189SAndy Fleming #include <linux/netdevice.h>
1900db8189SAndy Fleming #include <linux/etherdevice.h>
2000db8189SAndy Fleming #include <linux/skbuff.h>
2100db8189SAndy Fleming #include <linux/spinlock.h>
2200db8189SAndy Fleming #include <linux/mm.h>
2300db8189SAndy Fleming #include <linux/module.h>
2400db8189SAndy Fleming #include <linux/mii.h>
2500db8189SAndy Fleming #include <linux/ethtool.h>
2600db8189SAndy Fleming #include <linux/phy.h>
2700db8189SAndy Fleming
2800db8189SAndy Fleming #include <asm/io.h>
2900db8189SAndy Fleming #include <asm/irq.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3100db8189SAndy Fleming
3200db8189SAndy Fleming /* ------------------------------------------------------------------------- */
3300db8189SAndy Fleming /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
3400db8189SAndy Fleming
3500db8189SAndy Fleming /* register definitions */
3600db8189SAndy Fleming
3700db8189SAndy Fleming #define MII_QS6612_MCR 17 /* Mode Control Register */
3800db8189SAndy Fleming #define MII_QS6612_FTR 27 /* Factory Test Register */
3900db8189SAndy Fleming #define MII_QS6612_MCO 28 /* Misc. Control Register */
4000db8189SAndy Fleming #define MII_QS6612_ISR 29 /* Interrupt Source Register */
4100db8189SAndy Fleming #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
4200db8189SAndy Fleming #define MII_QS6612_IMR_INIT 0x003a
4300db8189SAndy Fleming #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
4400db8189SAndy Fleming
4500db8189SAndy Fleming #define QS6612_PCR_AN_COMPLETE 0x1000
4600db8189SAndy Fleming #define QS6612_PCR_RLBEN 0x0200
4700db8189SAndy Fleming #define QS6612_PCR_DCREN 0x0100
4800db8189SAndy Fleming #define QS6612_PCR_4B5BEN 0x0040
4900db8189SAndy Fleming #define QS6612_PCR_TX_ISOLATE 0x0020
5000db8189SAndy Fleming #define QS6612_PCR_MLT3_DIS 0x0002
5100db8189SAndy Fleming #define QS6612_PCR_SCRM_DESCRM 0x0001
5200db8189SAndy Fleming
5300db8189SAndy Fleming MODULE_DESCRIPTION("Quality Semiconductor PHY driver");
5400db8189SAndy Fleming MODULE_AUTHOR("Andy Fleming");
5500db8189SAndy Fleming MODULE_LICENSE("GPL");
5600db8189SAndy Fleming
5700db8189SAndy Fleming /* Returns 0, unless there's a write error */
qs6612_config_init(struct phy_device * phydev)5800db8189SAndy Fleming static int qs6612_config_init(struct phy_device *phydev)
5900db8189SAndy Fleming {
6000db8189SAndy Fleming /* The PHY powers up isolated on the RPX,
6100db8189SAndy Fleming * so send a command to allow operation.
6200db8189SAndy Fleming * XXX - My docs indicate this should be 0x0940
6300db8189SAndy Fleming * ...or something. The current value sets three
6400db8189SAndy Fleming * reserved bits, bit 11, which specifies it should be
6500db8189SAndy Fleming * set to one, bit 10, which specifies it should be set
6600db8189SAndy Fleming * to 0, and bit 7, which doesn't specify. However, my
6700db8189SAndy Fleming * docs are preliminary, and I will leave it like this
6800db8189SAndy Fleming * until someone more knowledgable corrects me or it.
6900db8189SAndy Fleming * -- Andy Fleming
7000db8189SAndy Fleming */
7100db8189SAndy Fleming return phy_write(phydev, MII_QS6612_PCR, 0x0dc0);
7200db8189SAndy Fleming }
7300db8189SAndy Fleming
qs6612_ack_interrupt(struct phy_device * phydev)7400db8189SAndy Fleming static int qs6612_ack_interrupt(struct phy_device *phydev)
7500db8189SAndy Fleming {
7600db8189SAndy Fleming int err;
7700db8189SAndy Fleming
78a1a44174SIoana Ciornei /* The Interrupt Source register is not self-clearing, bits 4 and 5 are
79a1a44174SIoana Ciornei * cleared when MII_BMSR is read and bits 1 and 3 are cleared when
80a1a44174SIoana Ciornei * MII_EXPANSION is read
81a1a44174SIoana Ciornei */
8200db8189SAndy Fleming err = phy_read(phydev, MII_QS6612_ISR);
8300db8189SAndy Fleming
8400db8189SAndy Fleming if (err < 0)
8500db8189SAndy Fleming return err;
8600db8189SAndy Fleming
8700db8189SAndy Fleming err = phy_read(phydev, MII_BMSR);
8800db8189SAndy Fleming
8900db8189SAndy Fleming if (err < 0)
9000db8189SAndy Fleming return err;
9100db8189SAndy Fleming
9200db8189SAndy Fleming err = phy_read(phydev, MII_EXPANSION);
9300db8189SAndy Fleming
9400db8189SAndy Fleming if (err < 0)
9500db8189SAndy Fleming return err;
9600db8189SAndy Fleming
9700db8189SAndy Fleming return 0;
9800db8189SAndy Fleming }
9900db8189SAndy Fleming
qs6612_config_intr(struct phy_device * phydev)10000db8189SAndy Fleming static int qs6612_config_intr(struct phy_device *phydev)
10100db8189SAndy Fleming {
10200db8189SAndy Fleming int err;
103*775f2547SWenpeng Liang
104a1a44174SIoana Ciornei if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
105a1a44174SIoana Ciornei /* clear any interrupts before enabling them */
106a1a44174SIoana Ciornei err = qs6612_ack_interrupt(phydev);
107a1a44174SIoana Ciornei if (err)
108a1a44174SIoana Ciornei return err;
109a1a44174SIoana Ciornei
11000db8189SAndy Fleming err = phy_write(phydev, MII_QS6612_IMR,
11100db8189SAndy Fleming MII_QS6612_IMR_INIT);
112a1a44174SIoana Ciornei } else {
11300db8189SAndy Fleming err = phy_write(phydev, MII_QS6612_IMR, 0);
114a1a44174SIoana Ciornei if (err)
115a1a44174SIoana Ciornei return err;
116a1a44174SIoana Ciornei
117a1a44174SIoana Ciornei /* clear any leftover interrupts */
118a1a44174SIoana Ciornei err = qs6612_ack_interrupt(phydev);
119a1a44174SIoana Ciornei }
12000db8189SAndy Fleming
12100db8189SAndy Fleming return err;
12200db8189SAndy Fleming
12300db8189SAndy Fleming }
12400db8189SAndy Fleming
qs6612_handle_interrupt(struct phy_device * phydev)125efc3d9deSIoana Ciornei static irqreturn_t qs6612_handle_interrupt(struct phy_device *phydev)
126efc3d9deSIoana Ciornei {
127efc3d9deSIoana Ciornei int irq_status;
128efc3d9deSIoana Ciornei
129efc3d9deSIoana Ciornei irq_status = phy_read(phydev, MII_QS6612_ISR);
130efc3d9deSIoana Ciornei if (irq_status < 0) {
131efc3d9deSIoana Ciornei phy_error(phydev);
132efc3d9deSIoana Ciornei return IRQ_NONE;
133efc3d9deSIoana Ciornei }
134efc3d9deSIoana Ciornei
135efc3d9deSIoana Ciornei if (!(irq_status & MII_QS6612_IMR_INIT))
136efc3d9deSIoana Ciornei return IRQ_NONE;
137efc3d9deSIoana Ciornei
138efc3d9deSIoana Ciornei /* the interrupt source register is not self-clearing */
139efc3d9deSIoana Ciornei qs6612_ack_interrupt(phydev);
140efc3d9deSIoana Ciornei
141efc3d9deSIoana Ciornei phy_trigger_machine(phydev);
142efc3d9deSIoana Ciornei
143efc3d9deSIoana Ciornei return IRQ_HANDLED;
144efc3d9deSIoana Ciornei }
145efc3d9deSIoana Ciornei
146116dffa0SJohan Hovold static struct phy_driver qs6612_driver[] = { {
14700db8189SAndy Fleming .phy_id = 0x00181440,
14800db8189SAndy Fleming .name = "QS6612",
14900db8189SAndy Fleming .phy_id_mask = 0xfffffff0,
150dcdecdcfSHeiner Kallweit /* PHY_BASIC_FEATURES */
15100db8189SAndy Fleming .config_init = qs6612_config_init,
15200db8189SAndy Fleming .config_intr = qs6612_config_intr,
153efc3d9deSIoana Ciornei .handle_interrupt = qs6612_handle_interrupt,
154116dffa0SJohan Hovold } };
15500db8189SAndy Fleming
156116dffa0SJohan Hovold module_phy_driver(qs6612_driver);
1574e4f10f6SDavid Woodhouse
158cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused qs6612_tbl[] = {
1594e4f10f6SDavid Woodhouse { 0x00181440, 0xfffffff0 },
1604e4f10f6SDavid Woodhouse { }
1614e4f10f6SDavid Woodhouse };
1624e4f10f6SDavid Woodhouse
1634e4f10f6SDavid Woodhouse MODULE_DEVICE_TABLE(mdio, qs6612_tbl);
164