1aa58bec0SParthiban Veerasooran // SPDX-License-Identifier: GPL-2.0+
2aa58bec0SParthiban Veerasooran /*
3aa58bec0SParthiban Veerasooran * OPEN Alliance 10BASE‑T1x MAC‑PHY Serial Interface framework
4aa58bec0SParthiban Veerasooran *
5aa58bec0SParthiban Veerasooran * Author: Parthiban Veerasooran <parthiban.veerasooran@microchip.com>
6aa58bec0SParthiban Veerasooran */
7aa58bec0SParthiban Veerasooran
8aa58bec0SParthiban Veerasooran #include <linux/bitfield.h>
91f9c4eedSParthiban Veerasooran #include <linux/iopoll.h>
108f9bf857SParthiban Veerasooran #include <linux/mdio.h>
118f9bf857SParthiban Veerasooran #include <linux/phy.h>
12aa58bec0SParthiban Veerasooran #include <linux/oa_tc6.h>
13aa58bec0SParthiban Veerasooran
141f9c4eedSParthiban Veerasooran /* OPEN Alliance TC6 registers */
158f9bf857SParthiban Veerasooran /* Standard Capabilities Register */
168f9bf857SParthiban Veerasooran #define OA_TC6_REG_STDCAP 0x0002
178f9bf857SParthiban Veerasooran #define STDCAP_DIRECT_PHY_REG_ACCESS BIT(8)
188f9bf857SParthiban Veerasooran
191f9c4eedSParthiban Veerasooran /* Reset Control and Status Register */
201f9c4eedSParthiban Veerasooran #define OA_TC6_REG_RESET 0x0003
211f9c4eedSParthiban Veerasooran #define RESET_SWRESET BIT(0) /* Software Reset */
221f9c4eedSParthiban Veerasooran
23f845a027SParthiban Veerasooran /* Configuration Register #0 */
24f845a027SParthiban Veerasooran #define OA_TC6_REG_CONFIG0 0x0004
25f845a027SParthiban Veerasooran #define CONFIG0_SYNC BIT(15)
26*afd42170SParthiban Veerasooran #define CONFIG0_ZARFE_ENABLE BIT(12)
27f845a027SParthiban Veerasooran
281f9c4eedSParthiban Veerasooran /* Status Register #0 */
291f9c4eedSParthiban Veerasooran #define OA_TC6_REG_STATUS0 0x0008
301f9c4eedSParthiban Veerasooran #define STATUS0_RESETC BIT(6) /* Reset Complete */
3153fbde8aSParthiban Veerasooran #define STATUS0_HEADER_ERROR BIT(5)
3253fbde8aSParthiban Veerasooran #define STATUS0_LOSS_OF_FRAME_ERROR BIT(4)
33d70a0d8fSParthiban Veerasooran #define STATUS0_RX_BUFFER_OVERFLOW_ERROR BIT(3)
3453fbde8aSParthiban Veerasooran #define STATUS0_TX_PROTOCOL_ERROR BIT(0)
3553fbde8aSParthiban Veerasooran
3653fbde8aSParthiban Veerasooran /* Buffer Status Register */
3753fbde8aSParthiban Veerasooran #define OA_TC6_REG_BUFFER_STATUS 0x000B
3853fbde8aSParthiban Veerasooran #define BUFFER_STATUS_TX_CREDITS_AVAILABLE GENMASK(15, 8)
39d70a0d8fSParthiban Veerasooran #define BUFFER_STATUS_RX_CHUNKS_AVAILABLE GENMASK(7, 0)
401f9c4eedSParthiban Veerasooran
4186c03a0fSParthiban Veerasooran /* Interrupt Mask Register #0 */
4286c03a0fSParthiban Veerasooran #define OA_TC6_REG_INT_MASK0 0x000C
4386c03a0fSParthiban Veerasooran #define INT_MASK0_HEADER_ERR_MASK BIT(5)
4486c03a0fSParthiban Veerasooran #define INT_MASK0_LOSS_OF_FRAME_ERR_MASK BIT(4)
4586c03a0fSParthiban Veerasooran #define INT_MASK0_RX_BUFFER_OVERFLOW_ERR_MASK BIT(3)
4686c03a0fSParthiban Veerasooran #define INT_MASK0_TX_PROTOCOL_ERR_MASK BIT(0)
4786c03a0fSParthiban Veerasooran
488f9bf857SParthiban Veerasooran /* PHY Clause 22 registers base address and mask */
498f9bf857SParthiban Veerasooran #define OA_TC6_PHY_STD_REG_ADDR_BASE 0xFF00
508f9bf857SParthiban Veerasooran #define OA_TC6_PHY_STD_REG_ADDR_MASK 0x1F
518f9bf857SParthiban Veerasooran
52aa58bec0SParthiban Veerasooran /* Control command header */
53aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_DATA_NOT_CTRL BIT(31)
54aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_WRITE_NOT_READ BIT(29)
55aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_MEM_MAP_SELECTOR GENMASK(27, 24)
56aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_ADDR GENMASK(23, 8)
57aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_LENGTH GENMASK(7, 1)
58aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_PARITY BIT(0)
59aa58bec0SParthiban Veerasooran
6053fbde8aSParthiban Veerasooran /* Data header */
6153fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_DATA_NOT_CTRL BIT(31)
6253fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_DATA_VALID BIT(21)
6353fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_START_VALID BIT(20)
6453fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_START_WORD_OFFSET GENMASK(19, 16)
6553fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_END_VALID BIT(14)
6653fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_END_BYTE_OFFSET GENMASK(13, 8)
6753fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_PARITY BIT(0)
6853fbde8aSParthiban Veerasooran
6953fbde8aSParthiban Veerasooran /* Data footer */
7053fbde8aSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_EXTENDED_STS BIT(31)
7153fbde8aSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_RXD_HEADER_BAD BIT(30)
7253fbde8aSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_CONFIG_SYNC BIT(29)
73d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_RX_CHUNKS GENMASK(28, 24)
74d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_DATA_VALID BIT(21)
75d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_START_VALID BIT(20)
76d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_START_WORD_OFFSET GENMASK(19, 16)
77d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_END_VALID BIT(14)
78d70a0d8fSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_END_BYTE_OFFSET GENMASK(13, 8)
7953fbde8aSParthiban Veerasooran #define OA_TC6_DATA_FOOTER_TX_CREDITS GENMASK(5, 1)
8053fbde8aSParthiban Veerasooran
818f9bf857SParthiban Veerasooran /* PHY – Clause 45 registers memory map selector (MMS) as per table 6 in the
828f9bf857SParthiban Veerasooran * OPEN Alliance specification.
838f9bf857SParthiban Veerasooran */
848f9bf857SParthiban Veerasooran #define OA_TC6_PHY_C45_PCS_MMS2 2 /* MMD 3 */
858f9bf857SParthiban Veerasooran #define OA_TC6_PHY_C45_PMA_PMD_MMS3 3 /* MMD 1 */
868f9bf857SParthiban Veerasooran #define OA_TC6_PHY_C45_VS_PLCA_MMS4 4 /* MMD 31 */
878f9bf857SParthiban Veerasooran #define OA_TC6_PHY_C45_AUTO_NEG_MMS5 5 /* MMD 7 */
888f9bf857SParthiban Veerasooran #define OA_TC6_PHY_C45_POWER_UNIT_MMS6 6 /* MMD 13 */
898f9bf857SParthiban Veerasooran
90aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_HEADER_SIZE 4
91aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_REG_VALUE_SIZE 4
92aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_IGNORED_SIZE 4
93aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_MAX_REGISTERS 128
94aa58bec0SParthiban Veerasooran #define OA_TC6_CTRL_SPI_BUF_SIZE (OA_TC6_CTRL_HEADER_SIZE +\
95aa58bec0SParthiban Veerasooran (OA_TC6_CTRL_MAX_REGISTERS *\
96aa58bec0SParthiban Veerasooran OA_TC6_CTRL_REG_VALUE_SIZE) +\
97aa58bec0SParthiban Veerasooran OA_TC6_CTRL_IGNORED_SIZE)
9853fbde8aSParthiban Veerasooran #define OA_TC6_CHUNK_PAYLOAD_SIZE 64
9953fbde8aSParthiban Veerasooran #define OA_TC6_DATA_HEADER_SIZE 4
10053fbde8aSParthiban Veerasooran #define OA_TC6_CHUNK_SIZE (OA_TC6_DATA_HEADER_SIZE +\
10153fbde8aSParthiban Veerasooran OA_TC6_CHUNK_PAYLOAD_SIZE)
10253fbde8aSParthiban Veerasooran #define OA_TC6_MAX_TX_CHUNKS 48
10353fbde8aSParthiban Veerasooran #define OA_TC6_SPI_DATA_BUF_SIZE (OA_TC6_MAX_TX_CHUNKS *\
10453fbde8aSParthiban Veerasooran OA_TC6_CHUNK_SIZE)
1051f9c4eedSParthiban Veerasooran #define STATUS0_RESETC_POLL_DELAY 1000
1061f9c4eedSParthiban Veerasooran #define STATUS0_RESETC_POLL_TIMEOUT 1000000
107aa58bec0SParthiban Veerasooran
108aa58bec0SParthiban Veerasooran /* Internal structure for MAC-PHY drivers */
109aa58bec0SParthiban Veerasooran struct oa_tc6 {
1108f9bf857SParthiban Veerasooran struct device *dev;
1118f9bf857SParthiban Veerasooran struct net_device *netdev;
1128f9bf857SParthiban Veerasooran struct phy_device *phydev;
1138f9bf857SParthiban Veerasooran struct mii_bus *mdiobus;
114aa58bec0SParthiban Veerasooran struct spi_device *spi;
115aa58bec0SParthiban Veerasooran struct mutex spi_ctrl_lock; /* Protects spi control transfer */
116aa58bec0SParthiban Veerasooran void *spi_ctrl_tx_buf;
117aa58bec0SParthiban Veerasooran void *spi_ctrl_rx_buf;
11853fbde8aSParthiban Veerasooran void *spi_data_tx_buf;
11953fbde8aSParthiban Veerasooran void *spi_data_rx_buf;
12053fbde8aSParthiban Veerasooran struct sk_buff *ongoing_tx_skb;
12153fbde8aSParthiban Veerasooran struct sk_buff *waiting_tx_skb;
122d70a0d8fSParthiban Veerasooran struct sk_buff *rx_skb;
12353fbde8aSParthiban Veerasooran struct task_struct *spi_thread;
12453fbde8aSParthiban Veerasooran wait_queue_head_t spi_wq;
12553fbde8aSParthiban Veerasooran u16 tx_skb_offset;
12653fbde8aSParthiban Veerasooran u16 spi_data_tx_buf_offset;
12753fbde8aSParthiban Veerasooran u16 tx_credits;
128d70a0d8fSParthiban Veerasooran u8 rx_chunks_available;
129d70a0d8fSParthiban Veerasooran bool rx_buf_overflow;
1302c6ce535SParthiban Veerasooran bool int_flag;
131aa58bec0SParthiban Veerasooran };
132aa58bec0SParthiban Veerasooran
133aa58bec0SParthiban Veerasooran enum oa_tc6_header_type {
134aa58bec0SParthiban Veerasooran OA_TC6_CTRL_HEADER,
13553fbde8aSParthiban Veerasooran OA_TC6_DATA_HEADER,
136aa58bec0SParthiban Veerasooran };
137aa58bec0SParthiban Veerasooran
138aa58bec0SParthiban Veerasooran enum oa_tc6_register_op {
139375d1e02SParthiban Veerasooran OA_TC6_CTRL_REG_READ = 0,
140aa58bec0SParthiban Veerasooran OA_TC6_CTRL_REG_WRITE = 1,
141aa58bec0SParthiban Veerasooran };
142aa58bec0SParthiban Veerasooran
14353fbde8aSParthiban Veerasooran enum oa_tc6_data_valid_info {
14453fbde8aSParthiban Veerasooran OA_TC6_DATA_INVALID,
14553fbde8aSParthiban Veerasooran OA_TC6_DATA_VALID,
14653fbde8aSParthiban Veerasooran };
14753fbde8aSParthiban Veerasooran
14853fbde8aSParthiban Veerasooran enum oa_tc6_data_start_valid_info {
14953fbde8aSParthiban Veerasooran OA_TC6_DATA_START_INVALID,
15053fbde8aSParthiban Veerasooran OA_TC6_DATA_START_VALID,
15153fbde8aSParthiban Veerasooran };
15253fbde8aSParthiban Veerasooran
15353fbde8aSParthiban Veerasooran enum oa_tc6_data_end_valid_info {
15453fbde8aSParthiban Veerasooran OA_TC6_DATA_END_INVALID,
15553fbde8aSParthiban Veerasooran OA_TC6_DATA_END_VALID,
15653fbde8aSParthiban Veerasooran };
15753fbde8aSParthiban Veerasooran
oa_tc6_spi_transfer(struct oa_tc6 * tc6,enum oa_tc6_header_type header_type,u16 length)158aa58bec0SParthiban Veerasooran static int oa_tc6_spi_transfer(struct oa_tc6 *tc6,
159aa58bec0SParthiban Veerasooran enum oa_tc6_header_type header_type, u16 length)
160aa58bec0SParthiban Veerasooran {
161aa58bec0SParthiban Veerasooran struct spi_transfer xfer = { 0 };
162aa58bec0SParthiban Veerasooran struct spi_message msg;
163aa58bec0SParthiban Veerasooran
16453fbde8aSParthiban Veerasooran if (header_type == OA_TC6_DATA_HEADER) {
16553fbde8aSParthiban Veerasooran xfer.tx_buf = tc6->spi_data_tx_buf;
16653fbde8aSParthiban Veerasooran xfer.rx_buf = tc6->spi_data_rx_buf;
16753fbde8aSParthiban Veerasooran } else {
168aa58bec0SParthiban Veerasooran xfer.tx_buf = tc6->spi_ctrl_tx_buf;
169aa58bec0SParthiban Veerasooran xfer.rx_buf = tc6->spi_ctrl_rx_buf;
17053fbde8aSParthiban Veerasooran }
171aa58bec0SParthiban Veerasooran xfer.len = length;
172aa58bec0SParthiban Veerasooran
173aa58bec0SParthiban Veerasooran spi_message_init(&msg);
174aa58bec0SParthiban Veerasooran spi_message_add_tail(&xfer, &msg);
175aa58bec0SParthiban Veerasooran
176aa58bec0SParthiban Veerasooran return spi_sync(tc6->spi, &msg);
177aa58bec0SParthiban Veerasooran }
178aa58bec0SParthiban Veerasooran
oa_tc6_get_parity(u32 p)179aa58bec0SParthiban Veerasooran static int oa_tc6_get_parity(u32 p)
180aa58bec0SParthiban Veerasooran {
181aa58bec0SParthiban Veerasooran /* Public domain code snippet, lifted from
182aa58bec0SParthiban Veerasooran * http://www-graphics.stanford.edu/~seander/bithacks.html
183aa58bec0SParthiban Veerasooran */
184aa58bec0SParthiban Veerasooran p ^= p >> 1;
185aa58bec0SParthiban Veerasooran p ^= p >> 2;
186aa58bec0SParthiban Veerasooran p = (p & 0x11111111U) * 0x11111111U;
187aa58bec0SParthiban Veerasooran
188aa58bec0SParthiban Veerasooran /* Odd parity is used here */
189aa58bec0SParthiban Veerasooran return !((p >> 28) & 1);
190aa58bec0SParthiban Veerasooran }
191aa58bec0SParthiban Veerasooran
oa_tc6_prepare_ctrl_header(u32 addr,u8 length,enum oa_tc6_register_op reg_op)192aa58bec0SParthiban Veerasooran static __be32 oa_tc6_prepare_ctrl_header(u32 addr, u8 length,
193aa58bec0SParthiban Veerasooran enum oa_tc6_register_op reg_op)
194aa58bec0SParthiban Veerasooran {
195aa58bec0SParthiban Veerasooran u32 header;
196aa58bec0SParthiban Veerasooran
197aa58bec0SParthiban Veerasooran header = FIELD_PREP(OA_TC6_CTRL_HEADER_DATA_NOT_CTRL,
198aa58bec0SParthiban Veerasooran OA_TC6_CTRL_HEADER) |
199aa58bec0SParthiban Veerasooran FIELD_PREP(OA_TC6_CTRL_HEADER_WRITE_NOT_READ, reg_op) |
200aa58bec0SParthiban Veerasooran FIELD_PREP(OA_TC6_CTRL_HEADER_MEM_MAP_SELECTOR, addr >> 16) |
201aa58bec0SParthiban Veerasooran FIELD_PREP(OA_TC6_CTRL_HEADER_ADDR, addr) |
202aa58bec0SParthiban Veerasooran FIELD_PREP(OA_TC6_CTRL_HEADER_LENGTH, length - 1);
203aa58bec0SParthiban Veerasooran header |= FIELD_PREP(OA_TC6_CTRL_HEADER_PARITY,
204aa58bec0SParthiban Veerasooran oa_tc6_get_parity(header));
205aa58bec0SParthiban Veerasooran
206aa58bec0SParthiban Veerasooran return cpu_to_be32(header);
207aa58bec0SParthiban Veerasooran }
208aa58bec0SParthiban Veerasooran
oa_tc6_update_ctrl_write_data(struct oa_tc6 * tc6,u32 value[],u8 length)209aa58bec0SParthiban Veerasooran static void oa_tc6_update_ctrl_write_data(struct oa_tc6 *tc6, u32 value[],
210aa58bec0SParthiban Veerasooran u8 length)
211aa58bec0SParthiban Veerasooran {
212aa58bec0SParthiban Veerasooran __be32 *tx_buf = tc6->spi_ctrl_tx_buf + OA_TC6_CTRL_HEADER_SIZE;
213aa58bec0SParthiban Veerasooran
214aa58bec0SParthiban Veerasooran for (int i = 0; i < length; i++)
215aa58bec0SParthiban Veerasooran *tx_buf++ = cpu_to_be32(value[i]);
216aa58bec0SParthiban Veerasooran }
217aa58bec0SParthiban Veerasooran
oa_tc6_calculate_ctrl_buf_size(u8 length)218aa58bec0SParthiban Veerasooran static u16 oa_tc6_calculate_ctrl_buf_size(u8 length)
219aa58bec0SParthiban Veerasooran {
220aa58bec0SParthiban Veerasooran /* Control command consists 4 bytes header + 4 bytes register value for
221aa58bec0SParthiban Veerasooran * each register + 4 bytes ignored value.
222aa58bec0SParthiban Veerasooran */
223aa58bec0SParthiban Veerasooran return OA_TC6_CTRL_HEADER_SIZE + OA_TC6_CTRL_REG_VALUE_SIZE * length +
224aa58bec0SParthiban Veerasooran OA_TC6_CTRL_IGNORED_SIZE;
225aa58bec0SParthiban Veerasooran }
226aa58bec0SParthiban Veerasooran
oa_tc6_prepare_ctrl_spi_buf(struct oa_tc6 * tc6,u32 address,u32 value[],u8 length,enum oa_tc6_register_op reg_op)227aa58bec0SParthiban Veerasooran static void oa_tc6_prepare_ctrl_spi_buf(struct oa_tc6 *tc6, u32 address,
228aa58bec0SParthiban Veerasooran u32 value[], u8 length,
229aa58bec0SParthiban Veerasooran enum oa_tc6_register_op reg_op)
230aa58bec0SParthiban Veerasooran {
231aa58bec0SParthiban Veerasooran __be32 *tx_buf = tc6->spi_ctrl_tx_buf;
232aa58bec0SParthiban Veerasooran
233aa58bec0SParthiban Veerasooran *tx_buf = oa_tc6_prepare_ctrl_header(address, length, reg_op);
234aa58bec0SParthiban Veerasooran
235375d1e02SParthiban Veerasooran if (reg_op == OA_TC6_CTRL_REG_WRITE)
236aa58bec0SParthiban Veerasooran oa_tc6_update_ctrl_write_data(tc6, value, length);
237aa58bec0SParthiban Veerasooran }
238aa58bec0SParthiban Veerasooran
oa_tc6_check_ctrl_write_reply(struct oa_tc6 * tc6,u8 size)239aa58bec0SParthiban Veerasooran static int oa_tc6_check_ctrl_write_reply(struct oa_tc6 *tc6, u8 size)
240aa58bec0SParthiban Veerasooran {
241aa58bec0SParthiban Veerasooran u8 *tx_buf = tc6->spi_ctrl_tx_buf;
242aa58bec0SParthiban Veerasooran u8 *rx_buf = tc6->spi_ctrl_rx_buf;
243aa58bec0SParthiban Veerasooran
244aa58bec0SParthiban Veerasooran rx_buf += OA_TC6_CTRL_IGNORED_SIZE;
245aa58bec0SParthiban Veerasooran
246aa58bec0SParthiban Veerasooran /* The echoed control write must match with the one that was
247aa58bec0SParthiban Veerasooran * transmitted.
248aa58bec0SParthiban Veerasooran */
249aa58bec0SParthiban Veerasooran if (memcmp(tx_buf, rx_buf, size - OA_TC6_CTRL_IGNORED_SIZE))
250aa58bec0SParthiban Veerasooran return -EPROTO;
251aa58bec0SParthiban Veerasooran
252aa58bec0SParthiban Veerasooran return 0;
253aa58bec0SParthiban Veerasooran }
254aa58bec0SParthiban Veerasooran
oa_tc6_check_ctrl_read_reply(struct oa_tc6 * tc6,u8 size)255375d1e02SParthiban Veerasooran static int oa_tc6_check_ctrl_read_reply(struct oa_tc6 *tc6, u8 size)
256375d1e02SParthiban Veerasooran {
257375d1e02SParthiban Veerasooran u32 *rx_buf = tc6->spi_ctrl_rx_buf + OA_TC6_CTRL_IGNORED_SIZE;
258375d1e02SParthiban Veerasooran u32 *tx_buf = tc6->spi_ctrl_tx_buf;
259375d1e02SParthiban Veerasooran
260375d1e02SParthiban Veerasooran /* The echoed control read header must match with the one that was
261375d1e02SParthiban Veerasooran * transmitted.
262375d1e02SParthiban Veerasooran */
263375d1e02SParthiban Veerasooran if (*tx_buf != *rx_buf)
264375d1e02SParthiban Veerasooran return -EPROTO;
265375d1e02SParthiban Veerasooran
266375d1e02SParthiban Veerasooran return 0;
267375d1e02SParthiban Veerasooran }
268375d1e02SParthiban Veerasooran
oa_tc6_copy_ctrl_read_data(struct oa_tc6 * tc6,u32 value[],u8 length)269375d1e02SParthiban Veerasooran static void oa_tc6_copy_ctrl_read_data(struct oa_tc6 *tc6, u32 value[],
270375d1e02SParthiban Veerasooran u8 length)
271375d1e02SParthiban Veerasooran {
272375d1e02SParthiban Veerasooran __be32 *rx_buf = tc6->spi_ctrl_rx_buf + OA_TC6_CTRL_IGNORED_SIZE +
273375d1e02SParthiban Veerasooran OA_TC6_CTRL_HEADER_SIZE;
274375d1e02SParthiban Veerasooran
275375d1e02SParthiban Veerasooran for (int i = 0; i < length; i++)
276375d1e02SParthiban Veerasooran value[i] = be32_to_cpu(*rx_buf++);
277375d1e02SParthiban Veerasooran }
278375d1e02SParthiban Veerasooran
oa_tc6_perform_ctrl(struct oa_tc6 * tc6,u32 address,u32 value[],u8 length,enum oa_tc6_register_op reg_op)279aa58bec0SParthiban Veerasooran static int oa_tc6_perform_ctrl(struct oa_tc6 *tc6, u32 address, u32 value[],
280aa58bec0SParthiban Veerasooran u8 length, enum oa_tc6_register_op reg_op)
281aa58bec0SParthiban Veerasooran {
282aa58bec0SParthiban Veerasooran u16 size;
283aa58bec0SParthiban Veerasooran int ret;
284aa58bec0SParthiban Veerasooran
285aa58bec0SParthiban Veerasooran /* Prepare control command and copy to SPI control buffer */
286aa58bec0SParthiban Veerasooran oa_tc6_prepare_ctrl_spi_buf(tc6, address, value, length, reg_op);
287aa58bec0SParthiban Veerasooran
288aa58bec0SParthiban Veerasooran size = oa_tc6_calculate_ctrl_buf_size(length);
289aa58bec0SParthiban Veerasooran
290aa58bec0SParthiban Veerasooran /* Perform SPI transfer */
291aa58bec0SParthiban Veerasooran ret = oa_tc6_spi_transfer(tc6, OA_TC6_CTRL_HEADER, size);
292aa58bec0SParthiban Veerasooran if (ret) {
293aa58bec0SParthiban Veerasooran dev_err(&tc6->spi->dev, "SPI transfer failed for control: %d\n",
294aa58bec0SParthiban Veerasooran ret);
295aa58bec0SParthiban Veerasooran return ret;
296aa58bec0SParthiban Veerasooran }
297aa58bec0SParthiban Veerasooran
298aa58bec0SParthiban Veerasooran /* Check echoed/received control write command reply for errors */
299375d1e02SParthiban Veerasooran if (reg_op == OA_TC6_CTRL_REG_WRITE)
300aa58bec0SParthiban Veerasooran return oa_tc6_check_ctrl_write_reply(tc6, size);
301375d1e02SParthiban Veerasooran
302375d1e02SParthiban Veerasooran /* Check echoed/received control read command reply for errors */
303375d1e02SParthiban Veerasooran ret = oa_tc6_check_ctrl_read_reply(tc6, size);
304375d1e02SParthiban Veerasooran if (ret)
305375d1e02SParthiban Veerasooran return ret;
306375d1e02SParthiban Veerasooran
307375d1e02SParthiban Veerasooran oa_tc6_copy_ctrl_read_data(tc6, value, length);
308375d1e02SParthiban Veerasooran
309375d1e02SParthiban Veerasooran return 0;
310aa58bec0SParthiban Veerasooran }
311aa58bec0SParthiban Veerasooran
312aa58bec0SParthiban Veerasooran /**
313375d1e02SParthiban Veerasooran * oa_tc6_read_registers - function for reading multiple consecutive registers.
314375d1e02SParthiban Veerasooran * @tc6: oa_tc6 struct.
315375d1e02SParthiban Veerasooran * @address: address of the first register to be read in the MAC-PHY.
316375d1e02SParthiban Veerasooran * @value: values to be read from the starting register address @address.
317375d1e02SParthiban Veerasooran * @length: number of consecutive registers to be read from @address.
318375d1e02SParthiban Veerasooran *
319375d1e02SParthiban Veerasooran * Maximum of 128 consecutive registers can be read starting at @address.
320375d1e02SParthiban Veerasooran *
321375d1e02SParthiban Veerasooran * Return: 0 on success otherwise failed.
322375d1e02SParthiban Veerasooran */
oa_tc6_read_registers(struct oa_tc6 * tc6,u32 address,u32 value[],u8 length)323375d1e02SParthiban Veerasooran int oa_tc6_read_registers(struct oa_tc6 *tc6, u32 address, u32 value[],
324375d1e02SParthiban Veerasooran u8 length)
325375d1e02SParthiban Veerasooran {
326375d1e02SParthiban Veerasooran int ret;
327375d1e02SParthiban Veerasooran
328375d1e02SParthiban Veerasooran if (!length || length > OA_TC6_CTRL_MAX_REGISTERS) {
329375d1e02SParthiban Veerasooran dev_err(&tc6->spi->dev, "Invalid register length parameter\n");
330375d1e02SParthiban Veerasooran return -EINVAL;
331375d1e02SParthiban Veerasooran }
332375d1e02SParthiban Veerasooran
333375d1e02SParthiban Veerasooran mutex_lock(&tc6->spi_ctrl_lock);
334375d1e02SParthiban Veerasooran ret = oa_tc6_perform_ctrl(tc6, address, value, length,
335375d1e02SParthiban Veerasooran OA_TC6_CTRL_REG_READ);
336375d1e02SParthiban Veerasooran mutex_unlock(&tc6->spi_ctrl_lock);
337375d1e02SParthiban Veerasooran
338375d1e02SParthiban Veerasooran return ret;
339375d1e02SParthiban Veerasooran }
340375d1e02SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_read_registers);
341375d1e02SParthiban Veerasooran
342375d1e02SParthiban Veerasooran /**
343375d1e02SParthiban Veerasooran * oa_tc6_read_register - function for reading a MAC-PHY register.
344375d1e02SParthiban Veerasooran * @tc6: oa_tc6 struct.
345375d1e02SParthiban Veerasooran * @address: register address of the MAC-PHY to be read.
346375d1e02SParthiban Veerasooran * @value: value read from the @address register address of the MAC-PHY.
347375d1e02SParthiban Veerasooran *
348375d1e02SParthiban Veerasooran * Return: 0 on success otherwise failed.
349375d1e02SParthiban Veerasooran */
oa_tc6_read_register(struct oa_tc6 * tc6,u32 address,u32 * value)350375d1e02SParthiban Veerasooran int oa_tc6_read_register(struct oa_tc6 *tc6, u32 address, u32 *value)
351375d1e02SParthiban Veerasooran {
352375d1e02SParthiban Veerasooran return oa_tc6_read_registers(tc6, address, value, 1);
353375d1e02SParthiban Veerasooran }
354375d1e02SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_read_register);
355375d1e02SParthiban Veerasooran
356375d1e02SParthiban Veerasooran /**
357aa58bec0SParthiban Veerasooran * oa_tc6_write_registers - function for writing multiple consecutive registers.
358aa58bec0SParthiban Veerasooran * @tc6: oa_tc6 struct.
359aa58bec0SParthiban Veerasooran * @address: address of the first register to be written in the MAC-PHY.
360aa58bec0SParthiban Veerasooran * @value: values to be written from the starting register address @address.
361aa58bec0SParthiban Veerasooran * @length: number of consecutive registers to be written from @address.
362aa58bec0SParthiban Veerasooran *
363aa58bec0SParthiban Veerasooran * Maximum of 128 consecutive registers can be written starting at @address.
364aa58bec0SParthiban Veerasooran *
365aa58bec0SParthiban Veerasooran * Return: 0 on success otherwise failed.
366aa58bec0SParthiban Veerasooran */
oa_tc6_write_registers(struct oa_tc6 * tc6,u32 address,u32 value[],u8 length)367aa58bec0SParthiban Veerasooran int oa_tc6_write_registers(struct oa_tc6 *tc6, u32 address, u32 value[],
368aa58bec0SParthiban Veerasooran u8 length)
369aa58bec0SParthiban Veerasooran {
370aa58bec0SParthiban Veerasooran int ret;
371aa58bec0SParthiban Veerasooran
372aa58bec0SParthiban Veerasooran if (!length || length > OA_TC6_CTRL_MAX_REGISTERS) {
373aa58bec0SParthiban Veerasooran dev_err(&tc6->spi->dev, "Invalid register length parameter\n");
374aa58bec0SParthiban Veerasooran return -EINVAL;
375aa58bec0SParthiban Veerasooran }
376aa58bec0SParthiban Veerasooran
377aa58bec0SParthiban Veerasooran mutex_lock(&tc6->spi_ctrl_lock);
378aa58bec0SParthiban Veerasooran ret = oa_tc6_perform_ctrl(tc6, address, value, length,
379aa58bec0SParthiban Veerasooran OA_TC6_CTRL_REG_WRITE);
380aa58bec0SParthiban Veerasooran mutex_unlock(&tc6->spi_ctrl_lock);
381aa58bec0SParthiban Veerasooran
382aa58bec0SParthiban Veerasooran return ret;
383aa58bec0SParthiban Veerasooran }
384aa58bec0SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_write_registers);
385aa58bec0SParthiban Veerasooran
386aa58bec0SParthiban Veerasooran /**
387aa58bec0SParthiban Veerasooran * oa_tc6_write_register - function for writing a MAC-PHY register.
388aa58bec0SParthiban Veerasooran * @tc6: oa_tc6 struct.
389aa58bec0SParthiban Veerasooran * @address: register address of the MAC-PHY to be written.
390aa58bec0SParthiban Veerasooran * @value: value to be written in the @address register address of the MAC-PHY.
391aa58bec0SParthiban Veerasooran *
392aa58bec0SParthiban Veerasooran * Return: 0 on success otherwise failed.
393aa58bec0SParthiban Veerasooran */
oa_tc6_write_register(struct oa_tc6 * tc6,u32 address,u32 value)394aa58bec0SParthiban Veerasooran int oa_tc6_write_register(struct oa_tc6 *tc6, u32 address, u32 value)
395aa58bec0SParthiban Veerasooran {
396aa58bec0SParthiban Veerasooran return oa_tc6_write_registers(tc6, address, &value, 1);
397aa58bec0SParthiban Veerasooran }
398aa58bec0SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_write_register);
399aa58bec0SParthiban Veerasooran
oa_tc6_check_phy_reg_direct_access_capability(struct oa_tc6 * tc6)4008f9bf857SParthiban Veerasooran static int oa_tc6_check_phy_reg_direct_access_capability(struct oa_tc6 *tc6)
4018f9bf857SParthiban Veerasooran {
4028f9bf857SParthiban Veerasooran u32 regval;
4038f9bf857SParthiban Veerasooran int ret;
4048f9bf857SParthiban Veerasooran
4058f9bf857SParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_STDCAP, ®val);
4068f9bf857SParthiban Veerasooran if (ret)
4078f9bf857SParthiban Veerasooran return ret;
4088f9bf857SParthiban Veerasooran
4098f9bf857SParthiban Veerasooran if (!(regval & STDCAP_DIRECT_PHY_REG_ACCESS))
4108f9bf857SParthiban Veerasooran return -ENODEV;
4118f9bf857SParthiban Veerasooran
4128f9bf857SParthiban Veerasooran return 0;
4138f9bf857SParthiban Veerasooran }
4148f9bf857SParthiban Veerasooran
oa_tc6_handle_link_change(struct net_device * netdev)4158f9bf857SParthiban Veerasooran static void oa_tc6_handle_link_change(struct net_device *netdev)
4168f9bf857SParthiban Veerasooran {
4178f9bf857SParthiban Veerasooran phy_print_status(netdev->phydev);
4188f9bf857SParthiban Veerasooran }
4198f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_read(struct mii_bus * bus,int addr,int regnum)4208f9bf857SParthiban Veerasooran static int oa_tc6_mdiobus_read(struct mii_bus *bus, int addr, int regnum)
4218f9bf857SParthiban Veerasooran {
4228f9bf857SParthiban Veerasooran struct oa_tc6 *tc6 = bus->priv;
4238f9bf857SParthiban Veerasooran u32 regval;
4248f9bf857SParthiban Veerasooran bool ret;
4258f9bf857SParthiban Veerasooran
4268f9bf857SParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_PHY_STD_REG_ADDR_BASE |
4278f9bf857SParthiban Veerasooran (regnum & OA_TC6_PHY_STD_REG_ADDR_MASK),
4288f9bf857SParthiban Veerasooran ®val);
4298f9bf857SParthiban Veerasooran if (ret)
4308f9bf857SParthiban Veerasooran return ret;
4318f9bf857SParthiban Veerasooran
4328f9bf857SParthiban Veerasooran return regval;
4338f9bf857SParthiban Veerasooran }
4348f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_write(struct mii_bus * bus,int addr,int regnum,u16 val)4358f9bf857SParthiban Veerasooran static int oa_tc6_mdiobus_write(struct mii_bus *bus, int addr, int regnum,
4368f9bf857SParthiban Veerasooran u16 val)
4378f9bf857SParthiban Veerasooran {
4388f9bf857SParthiban Veerasooran struct oa_tc6 *tc6 = bus->priv;
4398f9bf857SParthiban Veerasooran
4408f9bf857SParthiban Veerasooran return oa_tc6_write_register(tc6, OA_TC6_PHY_STD_REG_ADDR_BASE |
4418f9bf857SParthiban Veerasooran (regnum & OA_TC6_PHY_STD_REG_ADDR_MASK),
4428f9bf857SParthiban Veerasooran val);
4438f9bf857SParthiban Veerasooran }
4448f9bf857SParthiban Veerasooran
oa_tc6_get_phy_c45_mms(int devnum)4458f9bf857SParthiban Veerasooran static int oa_tc6_get_phy_c45_mms(int devnum)
4468f9bf857SParthiban Veerasooran {
4478f9bf857SParthiban Veerasooran switch (devnum) {
4488f9bf857SParthiban Veerasooran case MDIO_MMD_PCS:
4498f9bf857SParthiban Veerasooran return OA_TC6_PHY_C45_PCS_MMS2;
4508f9bf857SParthiban Veerasooran case MDIO_MMD_PMAPMD:
4518f9bf857SParthiban Veerasooran return OA_TC6_PHY_C45_PMA_PMD_MMS3;
4528f9bf857SParthiban Veerasooran case MDIO_MMD_VEND2:
4538f9bf857SParthiban Veerasooran return OA_TC6_PHY_C45_VS_PLCA_MMS4;
4548f9bf857SParthiban Veerasooran case MDIO_MMD_AN:
4558f9bf857SParthiban Veerasooran return OA_TC6_PHY_C45_AUTO_NEG_MMS5;
4568f9bf857SParthiban Veerasooran case MDIO_MMD_POWER_UNIT:
4578f9bf857SParthiban Veerasooran return OA_TC6_PHY_C45_POWER_UNIT_MMS6;
4588f9bf857SParthiban Veerasooran default:
4598f9bf857SParthiban Veerasooran return -EOPNOTSUPP;
4608f9bf857SParthiban Veerasooran }
4618f9bf857SParthiban Veerasooran }
4628f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_read_c45(struct mii_bus * bus,int addr,int devnum,int regnum)4638f9bf857SParthiban Veerasooran static int oa_tc6_mdiobus_read_c45(struct mii_bus *bus, int addr, int devnum,
4648f9bf857SParthiban Veerasooran int regnum)
4658f9bf857SParthiban Veerasooran {
4668f9bf857SParthiban Veerasooran struct oa_tc6 *tc6 = bus->priv;
4678f9bf857SParthiban Veerasooran u32 regval;
4688f9bf857SParthiban Veerasooran int ret;
4698f9bf857SParthiban Veerasooran
4708f9bf857SParthiban Veerasooran ret = oa_tc6_get_phy_c45_mms(devnum);
4718f9bf857SParthiban Veerasooran if (ret < 0)
4728f9bf857SParthiban Veerasooran return ret;
4738f9bf857SParthiban Veerasooran
4748f9bf857SParthiban Veerasooran ret = oa_tc6_read_register(tc6, (ret << 16) | regnum, ®val);
4758f9bf857SParthiban Veerasooran if (ret)
4768f9bf857SParthiban Veerasooran return ret;
4778f9bf857SParthiban Veerasooran
4788f9bf857SParthiban Veerasooran return regval;
4798f9bf857SParthiban Veerasooran }
4808f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_write_c45(struct mii_bus * bus,int addr,int devnum,int regnum,u16 val)4818f9bf857SParthiban Veerasooran static int oa_tc6_mdiobus_write_c45(struct mii_bus *bus, int addr, int devnum,
4828f9bf857SParthiban Veerasooran int regnum, u16 val)
4838f9bf857SParthiban Veerasooran {
4848f9bf857SParthiban Veerasooran struct oa_tc6 *tc6 = bus->priv;
4858f9bf857SParthiban Veerasooran int ret;
4868f9bf857SParthiban Veerasooran
4878f9bf857SParthiban Veerasooran ret = oa_tc6_get_phy_c45_mms(devnum);
4888f9bf857SParthiban Veerasooran if (ret < 0)
4898f9bf857SParthiban Veerasooran return ret;
4908f9bf857SParthiban Veerasooran
4918f9bf857SParthiban Veerasooran return oa_tc6_write_register(tc6, (ret << 16) | regnum, val);
4928f9bf857SParthiban Veerasooran }
4938f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_register(struct oa_tc6 * tc6)4948f9bf857SParthiban Veerasooran static int oa_tc6_mdiobus_register(struct oa_tc6 *tc6)
4958f9bf857SParthiban Veerasooran {
4968f9bf857SParthiban Veerasooran int ret;
4978f9bf857SParthiban Veerasooran
4988f9bf857SParthiban Veerasooran tc6->mdiobus = mdiobus_alloc();
4998f9bf857SParthiban Veerasooran if (!tc6->mdiobus) {
5008f9bf857SParthiban Veerasooran netdev_err(tc6->netdev, "MDIO bus alloc failed\n");
5018f9bf857SParthiban Veerasooran return -ENOMEM;
5028f9bf857SParthiban Veerasooran }
5038f9bf857SParthiban Veerasooran
5048f9bf857SParthiban Veerasooran tc6->mdiobus->priv = tc6;
5058f9bf857SParthiban Veerasooran tc6->mdiobus->read = oa_tc6_mdiobus_read;
5068f9bf857SParthiban Veerasooran tc6->mdiobus->write = oa_tc6_mdiobus_write;
5078f9bf857SParthiban Veerasooran /* OPEN Alliance 10BASE-T1x compliance MAC-PHYs will have both C22 and
5088f9bf857SParthiban Veerasooran * C45 registers space. If the PHY is discovered via C22 bus protocol it
5098f9bf857SParthiban Veerasooran * assumes it uses C22 protocol and always uses C22 registers indirect
5108f9bf857SParthiban Veerasooran * access to access C45 registers. This is because, we don't have a
5118f9bf857SParthiban Veerasooran * clean separation between C22/C45 register space and C22/C45 MDIO bus
5128f9bf857SParthiban Veerasooran * protocols. Resulting, PHY C45 registers direct access can't be used
5138f9bf857SParthiban Veerasooran * which can save multiple SPI bus access. To support this feature, PHY
5148f9bf857SParthiban Veerasooran * drivers can set .read_mmd/.write_mmd in the PHY driver to call
5158f9bf857SParthiban Veerasooran * .read_c45/.write_c45. Ex: drivers/net/phy/microchip_t1s.c
5168f9bf857SParthiban Veerasooran */
5178f9bf857SParthiban Veerasooran tc6->mdiobus->read_c45 = oa_tc6_mdiobus_read_c45;
5188f9bf857SParthiban Veerasooran tc6->mdiobus->write_c45 = oa_tc6_mdiobus_write_c45;
5198f9bf857SParthiban Veerasooran tc6->mdiobus->name = "oa-tc6-mdiobus";
5208f9bf857SParthiban Veerasooran tc6->mdiobus->parent = tc6->dev;
5218f9bf857SParthiban Veerasooran
5228f9bf857SParthiban Veerasooran snprintf(tc6->mdiobus->id, ARRAY_SIZE(tc6->mdiobus->id), "%s",
5238f9bf857SParthiban Veerasooran dev_name(&tc6->spi->dev));
5248f9bf857SParthiban Veerasooran
5258f9bf857SParthiban Veerasooran ret = mdiobus_register(tc6->mdiobus);
5268f9bf857SParthiban Veerasooran if (ret) {
5278f9bf857SParthiban Veerasooran netdev_err(tc6->netdev, "Could not register MDIO bus\n");
5288f9bf857SParthiban Veerasooran mdiobus_free(tc6->mdiobus);
5298f9bf857SParthiban Veerasooran return ret;
5308f9bf857SParthiban Veerasooran }
5318f9bf857SParthiban Veerasooran
5328f9bf857SParthiban Veerasooran return 0;
5338f9bf857SParthiban Veerasooran }
5348f9bf857SParthiban Veerasooran
oa_tc6_mdiobus_unregister(struct oa_tc6 * tc6)5358f9bf857SParthiban Veerasooran static void oa_tc6_mdiobus_unregister(struct oa_tc6 *tc6)
5368f9bf857SParthiban Veerasooran {
5378f9bf857SParthiban Veerasooran mdiobus_unregister(tc6->mdiobus);
5388f9bf857SParthiban Veerasooran mdiobus_free(tc6->mdiobus);
5398f9bf857SParthiban Veerasooran }
5408f9bf857SParthiban Veerasooran
oa_tc6_phy_init(struct oa_tc6 * tc6)5418f9bf857SParthiban Veerasooran static int oa_tc6_phy_init(struct oa_tc6 *tc6)
5428f9bf857SParthiban Veerasooran {
5438f9bf857SParthiban Veerasooran int ret;
5448f9bf857SParthiban Veerasooran
5458f9bf857SParthiban Veerasooran ret = oa_tc6_check_phy_reg_direct_access_capability(tc6);
5468f9bf857SParthiban Veerasooran if (ret) {
5478f9bf857SParthiban Veerasooran netdev_err(tc6->netdev,
5488f9bf857SParthiban Veerasooran "Direct PHY register access is not supported by the MAC-PHY\n");
5498f9bf857SParthiban Veerasooran return ret;
5508f9bf857SParthiban Veerasooran }
5518f9bf857SParthiban Veerasooran
5528f9bf857SParthiban Veerasooran ret = oa_tc6_mdiobus_register(tc6);
5538f9bf857SParthiban Veerasooran if (ret)
5548f9bf857SParthiban Veerasooran return ret;
5558f9bf857SParthiban Veerasooran
5568f9bf857SParthiban Veerasooran tc6->phydev = phy_find_first(tc6->mdiobus);
5578f9bf857SParthiban Veerasooran if (!tc6->phydev) {
5588f9bf857SParthiban Veerasooran netdev_err(tc6->netdev, "No PHY found\n");
5598f9bf857SParthiban Veerasooran oa_tc6_mdiobus_unregister(tc6);
5608f9bf857SParthiban Veerasooran return -ENODEV;
5618f9bf857SParthiban Veerasooran }
5628f9bf857SParthiban Veerasooran
5638f9bf857SParthiban Veerasooran tc6->phydev->is_internal = true;
5648f9bf857SParthiban Veerasooran ret = phy_connect_direct(tc6->netdev, tc6->phydev,
5658f9bf857SParthiban Veerasooran &oa_tc6_handle_link_change,
5668f9bf857SParthiban Veerasooran PHY_INTERFACE_MODE_INTERNAL);
5678f9bf857SParthiban Veerasooran if (ret) {
5688f9bf857SParthiban Veerasooran netdev_err(tc6->netdev, "Can't attach PHY to %s\n",
5698f9bf857SParthiban Veerasooran tc6->mdiobus->id);
5708f9bf857SParthiban Veerasooran oa_tc6_mdiobus_unregister(tc6);
5718f9bf857SParthiban Veerasooran return ret;
5728f9bf857SParthiban Veerasooran }
5738f9bf857SParthiban Veerasooran
5748f9bf857SParthiban Veerasooran phy_attached_info(tc6->netdev->phydev);
5758f9bf857SParthiban Veerasooran
5768f9bf857SParthiban Veerasooran return 0;
5778f9bf857SParthiban Veerasooran }
5788f9bf857SParthiban Veerasooran
oa_tc6_phy_exit(struct oa_tc6 * tc6)5798f9bf857SParthiban Veerasooran static void oa_tc6_phy_exit(struct oa_tc6 *tc6)
5808f9bf857SParthiban Veerasooran {
5818f9bf857SParthiban Veerasooran phy_disconnect(tc6->phydev);
5828f9bf857SParthiban Veerasooran oa_tc6_mdiobus_unregister(tc6);
5838f9bf857SParthiban Veerasooran }
5848f9bf857SParthiban Veerasooran
oa_tc6_read_status0(struct oa_tc6 * tc6)5851f9c4eedSParthiban Veerasooran static int oa_tc6_read_status0(struct oa_tc6 *tc6)
5861f9c4eedSParthiban Veerasooran {
5871f9c4eedSParthiban Veerasooran u32 regval;
5881f9c4eedSParthiban Veerasooran int ret;
5891f9c4eedSParthiban Veerasooran
5901f9c4eedSParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_STATUS0, ®val);
5911f9c4eedSParthiban Veerasooran if (ret) {
5921f9c4eedSParthiban Veerasooran dev_err(&tc6->spi->dev, "STATUS0 register read failed: %d\n",
5931f9c4eedSParthiban Veerasooran ret);
5941f9c4eedSParthiban Veerasooran return 0;
5951f9c4eedSParthiban Veerasooran }
5961f9c4eedSParthiban Veerasooran
5971f9c4eedSParthiban Veerasooran return regval;
5981f9c4eedSParthiban Veerasooran }
5991f9c4eedSParthiban Veerasooran
oa_tc6_sw_reset_macphy(struct oa_tc6 * tc6)6001f9c4eedSParthiban Veerasooran static int oa_tc6_sw_reset_macphy(struct oa_tc6 *tc6)
6011f9c4eedSParthiban Veerasooran {
6021f9c4eedSParthiban Veerasooran u32 regval = RESET_SWRESET;
6031f9c4eedSParthiban Veerasooran int ret;
6041f9c4eedSParthiban Veerasooran
6051f9c4eedSParthiban Veerasooran ret = oa_tc6_write_register(tc6, OA_TC6_REG_RESET, regval);
6061f9c4eedSParthiban Veerasooran if (ret)
6071f9c4eedSParthiban Veerasooran return ret;
6081f9c4eedSParthiban Veerasooran
6091f9c4eedSParthiban Veerasooran /* Poll for soft reset complete for every 1ms until 1s timeout */
6101f9c4eedSParthiban Veerasooran ret = readx_poll_timeout(oa_tc6_read_status0, tc6, regval,
6111f9c4eedSParthiban Veerasooran regval & STATUS0_RESETC,
6121f9c4eedSParthiban Veerasooran STATUS0_RESETC_POLL_DELAY,
6131f9c4eedSParthiban Veerasooran STATUS0_RESETC_POLL_TIMEOUT);
6141f9c4eedSParthiban Veerasooran if (ret)
6151f9c4eedSParthiban Veerasooran return -ENODEV;
6161f9c4eedSParthiban Veerasooran
6171f9c4eedSParthiban Veerasooran /* Clear the reset complete status */
6181f9c4eedSParthiban Veerasooran return oa_tc6_write_register(tc6, OA_TC6_REG_STATUS0, regval);
6191f9c4eedSParthiban Veerasooran }
6201f9c4eedSParthiban Veerasooran
oa_tc6_unmask_macphy_error_interrupts(struct oa_tc6 * tc6)62186c03a0fSParthiban Veerasooran static int oa_tc6_unmask_macphy_error_interrupts(struct oa_tc6 *tc6)
62286c03a0fSParthiban Veerasooran {
62386c03a0fSParthiban Veerasooran u32 regval;
62486c03a0fSParthiban Veerasooran int ret;
62586c03a0fSParthiban Veerasooran
62686c03a0fSParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_INT_MASK0, ®val);
62786c03a0fSParthiban Veerasooran if (ret)
62886c03a0fSParthiban Veerasooran return ret;
62986c03a0fSParthiban Veerasooran
63086c03a0fSParthiban Veerasooran regval &= ~(INT_MASK0_TX_PROTOCOL_ERR_MASK |
63186c03a0fSParthiban Veerasooran INT_MASK0_RX_BUFFER_OVERFLOW_ERR_MASK |
63286c03a0fSParthiban Veerasooran INT_MASK0_LOSS_OF_FRAME_ERR_MASK |
63386c03a0fSParthiban Veerasooran INT_MASK0_HEADER_ERR_MASK);
63486c03a0fSParthiban Veerasooran
63586c03a0fSParthiban Veerasooran return oa_tc6_write_register(tc6, OA_TC6_REG_INT_MASK0, regval);
63686c03a0fSParthiban Veerasooran }
63786c03a0fSParthiban Veerasooran
oa_tc6_enable_data_transfer(struct oa_tc6 * tc6)638f845a027SParthiban Veerasooran static int oa_tc6_enable_data_transfer(struct oa_tc6 *tc6)
639f845a027SParthiban Veerasooran {
640f845a027SParthiban Veerasooran u32 value;
641f845a027SParthiban Veerasooran int ret;
642f845a027SParthiban Veerasooran
643f845a027SParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_CONFIG0, &value);
644f845a027SParthiban Veerasooran if (ret)
645f845a027SParthiban Veerasooran return ret;
646f845a027SParthiban Veerasooran
647f845a027SParthiban Veerasooran /* Enable configuration synchronization for data transfer */
648f845a027SParthiban Veerasooran value |= CONFIG0_SYNC;
649f845a027SParthiban Veerasooran
650f845a027SParthiban Veerasooran return oa_tc6_write_register(tc6, OA_TC6_REG_CONFIG0, value);
651f845a027SParthiban Veerasooran }
652f845a027SParthiban Veerasooran
oa_tc6_cleanup_ongoing_rx_skb(struct oa_tc6 * tc6)653d70a0d8fSParthiban Veerasooran static void oa_tc6_cleanup_ongoing_rx_skb(struct oa_tc6 *tc6)
654d70a0d8fSParthiban Veerasooran {
655d70a0d8fSParthiban Veerasooran if (tc6->rx_skb) {
656d70a0d8fSParthiban Veerasooran tc6->netdev->stats.rx_dropped++;
657d70a0d8fSParthiban Veerasooran kfree_skb(tc6->rx_skb);
658d70a0d8fSParthiban Veerasooran tc6->rx_skb = NULL;
659d70a0d8fSParthiban Veerasooran }
660d70a0d8fSParthiban Veerasooran }
661d70a0d8fSParthiban Veerasooran
oa_tc6_cleanup_ongoing_tx_skb(struct oa_tc6 * tc6)66253fbde8aSParthiban Veerasooran static void oa_tc6_cleanup_ongoing_tx_skb(struct oa_tc6 *tc6)
66353fbde8aSParthiban Veerasooran {
66453fbde8aSParthiban Veerasooran if (tc6->ongoing_tx_skb) {
66553fbde8aSParthiban Veerasooran tc6->netdev->stats.tx_dropped++;
66653fbde8aSParthiban Veerasooran kfree_skb(tc6->ongoing_tx_skb);
66753fbde8aSParthiban Veerasooran tc6->ongoing_tx_skb = NULL;
66853fbde8aSParthiban Veerasooran }
66953fbde8aSParthiban Veerasooran }
67053fbde8aSParthiban Veerasooran
oa_tc6_process_extended_status(struct oa_tc6 * tc6)67153fbde8aSParthiban Veerasooran static int oa_tc6_process_extended_status(struct oa_tc6 *tc6)
67253fbde8aSParthiban Veerasooran {
67353fbde8aSParthiban Veerasooran u32 value;
67453fbde8aSParthiban Veerasooran int ret;
67553fbde8aSParthiban Veerasooran
67653fbde8aSParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_STATUS0, &value);
67753fbde8aSParthiban Veerasooran if (ret) {
67853fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "STATUS0 register read failed: %d\n",
67953fbde8aSParthiban Veerasooran ret);
68053fbde8aSParthiban Veerasooran return ret;
68153fbde8aSParthiban Veerasooran }
68253fbde8aSParthiban Veerasooran
68353fbde8aSParthiban Veerasooran /* Clear the error interrupts status */
68453fbde8aSParthiban Veerasooran ret = oa_tc6_write_register(tc6, OA_TC6_REG_STATUS0, value);
68553fbde8aSParthiban Veerasooran if (ret) {
68653fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "STATUS0 register write failed: %d\n",
68753fbde8aSParthiban Veerasooran ret);
68853fbde8aSParthiban Veerasooran return ret;
68953fbde8aSParthiban Veerasooran }
69053fbde8aSParthiban Veerasooran
691d70a0d8fSParthiban Veerasooran if (FIELD_GET(STATUS0_RX_BUFFER_OVERFLOW_ERROR, value)) {
692d70a0d8fSParthiban Veerasooran tc6->rx_buf_overflow = true;
693d70a0d8fSParthiban Veerasooran oa_tc6_cleanup_ongoing_rx_skb(tc6);
694d70a0d8fSParthiban Veerasooran net_err_ratelimited("%s: Receive buffer overflow error\n",
695d70a0d8fSParthiban Veerasooran tc6->netdev->name);
696d70a0d8fSParthiban Veerasooran return -EAGAIN;
697d70a0d8fSParthiban Veerasooran }
69853fbde8aSParthiban Veerasooran if (FIELD_GET(STATUS0_TX_PROTOCOL_ERROR, value)) {
69953fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Transmit protocol error\n");
70053fbde8aSParthiban Veerasooran return -ENODEV;
70153fbde8aSParthiban Veerasooran }
70253fbde8aSParthiban Veerasooran /* TODO: Currently loss of frame and header errors are treated as
70353fbde8aSParthiban Veerasooran * non-recoverable errors. They will be handled in the next version.
70453fbde8aSParthiban Veerasooran */
70553fbde8aSParthiban Veerasooran if (FIELD_GET(STATUS0_LOSS_OF_FRAME_ERROR, value)) {
70653fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Loss of frame error\n");
70753fbde8aSParthiban Veerasooran return -ENODEV;
70853fbde8aSParthiban Veerasooran }
70953fbde8aSParthiban Veerasooran if (FIELD_GET(STATUS0_HEADER_ERROR, value)) {
71053fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Header error\n");
71153fbde8aSParthiban Veerasooran return -ENODEV;
71253fbde8aSParthiban Veerasooran }
71353fbde8aSParthiban Veerasooran
71453fbde8aSParthiban Veerasooran return 0;
71553fbde8aSParthiban Veerasooran }
71653fbde8aSParthiban Veerasooran
oa_tc6_process_rx_chunk_footer(struct oa_tc6 * tc6,u32 footer)71753fbde8aSParthiban Veerasooran static int oa_tc6_process_rx_chunk_footer(struct oa_tc6 *tc6, u32 footer)
71853fbde8aSParthiban Veerasooran {
71953fbde8aSParthiban Veerasooran /* Process rx chunk footer for the following,
72053fbde8aSParthiban Veerasooran * 1. tx credits
72153fbde8aSParthiban Veerasooran * 2. errors if any from MAC-PHY
722d70a0d8fSParthiban Veerasooran * 3. receive chunks available
72353fbde8aSParthiban Veerasooran */
72453fbde8aSParthiban Veerasooran tc6->tx_credits = FIELD_GET(OA_TC6_DATA_FOOTER_TX_CREDITS, footer);
725d70a0d8fSParthiban Veerasooran tc6->rx_chunks_available = FIELD_GET(OA_TC6_DATA_FOOTER_RX_CHUNKS,
726d70a0d8fSParthiban Veerasooran footer);
72753fbde8aSParthiban Veerasooran
72853fbde8aSParthiban Veerasooran if (FIELD_GET(OA_TC6_DATA_FOOTER_EXTENDED_STS, footer)) {
72953fbde8aSParthiban Veerasooran int ret = oa_tc6_process_extended_status(tc6);
73053fbde8aSParthiban Veerasooran
73153fbde8aSParthiban Veerasooran if (ret)
73253fbde8aSParthiban Veerasooran return ret;
73353fbde8aSParthiban Veerasooran }
73453fbde8aSParthiban Veerasooran
73553fbde8aSParthiban Veerasooran /* TODO: Currently received header bad and configuration unsync errors
73653fbde8aSParthiban Veerasooran * are treated as non-recoverable errors. They will be handled in the
73753fbde8aSParthiban Veerasooran * next version.
73853fbde8aSParthiban Veerasooran */
73953fbde8aSParthiban Veerasooran if (FIELD_GET(OA_TC6_DATA_FOOTER_RXD_HEADER_BAD, footer)) {
74053fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Rxd header bad error\n");
74153fbde8aSParthiban Veerasooran return -ENODEV;
74253fbde8aSParthiban Veerasooran }
74353fbde8aSParthiban Veerasooran
74453fbde8aSParthiban Veerasooran if (!FIELD_GET(OA_TC6_DATA_FOOTER_CONFIG_SYNC, footer)) {
74553fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Config unsync error\n");
74653fbde8aSParthiban Veerasooran return -ENODEV;
74753fbde8aSParthiban Veerasooran }
74853fbde8aSParthiban Veerasooran
74953fbde8aSParthiban Veerasooran return 0;
75053fbde8aSParthiban Veerasooran }
75153fbde8aSParthiban Veerasooran
oa_tc6_submit_rx_skb(struct oa_tc6 * tc6)752d70a0d8fSParthiban Veerasooran static void oa_tc6_submit_rx_skb(struct oa_tc6 *tc6)
753d70a0d8fSParthiban Veerasooran {
754d70a0d8fSParthiban Veerasooran tc6->rx_skb->protocol = eth_type_trans(tc6->rx_skb, tc6->netdev);
755d70a0d8fSParthiban Veerasooran tc6->netdev->stats.rx_packets++;
756d70a0d8fSParthiban Veerasooran tc6->netdev->stats.rx_bytes += tc6->rx_skb->len;
757d70a0d8fSParthiban Veerasooran
758d70a0d8fSParthiban Veerasooran netif_rx(tc6->rx_skb);
759d70a0d8fSParthiban Veerasooran
760d70a0d8fSParthiban Veerasooran tc6->rx_skb = NULL;
761d70a0d8fSParthiban Veerasooran }
762d70a0d8fSParthiban Veerasooran
oa_tc6_update_rx_skb(struct oa_tc6 * tc6,u8 * payload,u8 length)763d70a0d8fSParthiban Veerasooran static void oa_tc6_update_rx_skb(struct oa_tc6 *tc6, u8 *payload, u8 length)
764d70a0d8fSParthiban Veerasooran {
765d70a0d8fSParthiban Veerasooran memcpy(skb_put(tc6->rx_skb, length), payload, length);
766d70a0d8fSParthiban Veerasooran }
767d70a0d8fSParthiban Veerasooran
oa_tc6_allocate_rx_skb(struct oa_tc6 * tc6)768d70a0d8fSParthiban Veerasooran static int oa_tc6_allocate_rx_skb(struct oa_tc6 *tc6)
769d70a0d8fSParthiban Veerasooran {
770d70a0d8fSParthiban Veerasooran tc6->rx_skb = netdev_alloc_skb_ip_align(tc6->netdev, tc6->netdev->mtu +
771d70a0d8fSParthiban Veerasooran ETH_HLEN + ETH_FCS_LEN);
772d70a0d8fSParthiban Veerasooran if (!tc6->rx_skb) {
773d70a0d8fSParthiban Veerasooran tc6->netdev->stats.rx_dropped++;
774d70a0d8fSParthiban Veerasooran return -ENOMEM;
775d70a0d8fSParthiban Veerasooran }
776d70a0d8fSParthiban Veerasooran
777d70a0d8fSParthiban Veerasooran return 0;
778d70a0d8fSParthiban Veerasooran }
779d70a0d8fSParthiban Veerasooran
oa_tc6_prcs_complete_rx_frame(struct oa_tc6 * tc6,u8 * payload,u16 size)780d70a0d8fSParthiban Veerasooran static int oa_tc6_prcs_complete_rx_frame(struct oa_tc6 *tc6, u8 *payload,
781d70a0d8fSParthiban Veerasooran u16 size)
782d70a0d8fSParthiban Veerasooran {
783d70a0d8fSParthiban Veerasooran int ret;
784d70a0d8fSParthiban Veerasooran
785d70a0d8fSParthiban Veerasooran ret = oa_tc6_allocate_rx_skb(tc6);
786d70a0d8fSParthiban Veerasooran if (ret)
787d70a0d8fSParthiban Veerasooran return ret;
788d70a0d8fSParthiban Veerasooran
789d70a0d8fSParthiban Veerasooran oa_tc6_update_rx_skb(tc6, payload, size);
790d70a0d8fSParthiban Veerasooran
791d70a0d8fSParthiban Veerasooran oa_tc6_submit_rx_skb(tc6);
792d70a0d8fSParthiban Veerasooran
793d70a0d8fSParthiban Veerasooran return 0;
794d70a0d8fSParthiban Veerasooran }
795d70a0d8fSParthiban Veerasooran
oa_tc6_prcs_rx_frame_start(struct oa_tc6 * tc6,u8 * payload,u16 size)796d70a0d8fSParthiban Veerasooran static int oa_tc6_prcs_rx_frame_start(struct oa_tc6 *tc6, u8 *payload, u16 size)
797d70a0d8fSParthiban Veerasooran {
798d70a0d8fSParthiban Veerasooran int ret;
799d70a0d8fSParthiban Veerasooran
800d70a0d8fSParthiban Veerasooran ret = oa_tc6_allocate_rx_skb(tc6);
801d70a0d8fSParthiban Veerasooran if (ret)
802d70a0d8fSParthiban Veerasooran return ret;
803d70a0d8fSParthiban Veerasooran
804d70a0d8fSParthiban Veerasooran oa_tc6_update_rx_skb(tc6, payload, size);
805d70a0d8fSParthiban Veerasooran
806d70a0d8fSParthiban Veerasooran return 0;
807d70a0d8fSParthiban Veerasooran }
808d70a0d8fSParthiban Veerasooran
oa_tc6_prcs_rx_frame_end(struct oa_tc6 * tc6,u8 * payload,u16 size)809d70a0d8fSParthiban Veerasooran static void oa_tc6_prcs_rx_frame_end(struct oa_tc6 *tc6, u8 *payload, u16 size)
810d70a0d8fSParthiban Veerasooran {
811d70a0d8fSParthiban Veerasooran oa_tc6_update_rx_skb(tc6, payload, size);
812d70a0d8fSParthiban Veerasooran
813d70a0d8fSParthiban Veerasooran oa_tc6_submit_rx_skb(tc6);
814d70a0d8fSParthiban Veerasooran }
815d70a0d8fSParthiban Veerasooran
oa_tc6_prcs_ongoing_rx_frame(struct oa_tc6 * tc6,u8 * payload,u32 footer)816d70a0d8fSParthiban Veerasooran static void oa_tc6_prcs_ongoing_rx_frame(struct oa_tc6 *tc6, u8 *payload,
817d70a0d8fSParthiban Veerasooran u32 footer)
818d70a0d8fSParthiban Veerasooran {
819d70a0d8fSParthiban Veerasooran oa_tc6_update_rx_skb(tc6, payload, OA_TC6_CHUNK_PAYLOAD_SIZE);
820d70a0d8fSParthiban Veerasooran }
821d70a0d8fSParthiban Veerasooran
oa_tc6_prcs_rx_chunk_payload(struct oa_tc6 * tc6,u8 * data,u32 footer)822d70a0d8fSParthiban Veerasooran static int oa_tc6_prcs_rx_chunk_payload(struct oa_tc6 *tc6, u8 *data,
823d70a0d8fSParthiban Veerasooran u32 footer)
824d70a0d8fSParthiban Veerasooran {
825d70a0d8fSParthiban Veerasooran u8 start_byte_offset = FIELD_GET(OA_TC6_DATA_FOOTER_START_WORD_OFFSET,
826d70a0d8fSParthiban Veerasooran footer) * sizeof(u32);
827d70a0d8fSParthiban Veerasooran u8 end_byte_offset = FIELD_GET(OA_TC6_DATA_FOOTER_END_BYTE_OFFSET,
828d70a0d8fSParthiban Veerasooran footer);
829d70a0d8fSParthiban Veerasooran bool start_valid = FIELD_GET(OA_TC6_DATA_FOOTER_START_VALID, footer);
830d70a0d8fSParthiban Veerasooran bool end_valid = FIELD_GET(OA_TC6_DATA_FOOTER_END_VALID, footer);
831d70a0d8fSParthiban Veerasooran u16 size;
832d70a0d8fSParthiban Veerasooran
833d70a0d8fSParthiban Veerasooran /* Restart the new rx frame after receiving rx buffer overflow error */
834d70a0d8fSParthiban Veerasooran if (start_valid && tc6->rx_buf_overflow)
835d70a0d8fSParthiban Veerasooran tc6->rx_buf_overflow = false;
836d70a0d8fSParthiban Veerasooran
837d70a0d8fSParthiban Veerasooran if (tc6->rx_buf_overflow)
838d70a0d8fSParthiban Veerasooran return 0;
839d70a0d8fSParthiban Veerasooran
840d70a0d8fSParthiban Veerasooran /* Process the chunk with complete rx frame */
841d70a0d8fSParthiban Veerasooran if (start_valid && end_valid && start_byte_offset < end_byte_offset) {
842d70a0d8fSParthiban Veerasooran size = end_byte_offset + 1 - start_byte_offset;
843d70a0d8fSParthiban Veerasooran return oa_tc6_prcs_complete_rx_frame(tc6,
844d70a0d8fSParthiban Veerasooran &data[start_byte_offset],
845d70a0d8fSParthiban Veerasooran size);
846d70a0d8fSParthiban Veerasooran }
847d70a0d8fSParthiban Veerasooran
848d70a0d8fSParthiban Veerasooran /* Process the chunk with only rx frame start */
849d70a0d8fSParthiban Veerasooran if (start_valid && !end_valid) {
850d70a0d8fSParthiban Veerasooran size = OA_TC6_CHUNK_PAYLOAD_SIZE - start_byte_offset;
851d70a0d8fSParthiban Veerasooran return oa_tc6_prcs_rx_frame_start(tc6,
852d70a0d8fSParthiban Veerasooran &data[start_byte_offset],
853d70a0d8fSParthiban Veerasooran size);
854d70a0d8fSParthiban Veerasooran }
855d70a0d8fSParthiban Veerasooran
856d70a0d8fSParthiban Veerasooran /* Process the chunk with only rx frame end */
857d70a0d8fSParthiban Veerasooran if (end_valid && !start_valid) {
858d70a0d8fSParthiban Veerasooran size = end_byte_offset + 1;
859d70a0d8fSParthiban Veerasooran oa_tc6_prcs_rx_frame_end(tc6, data, size);
860d70a0d8fSParthiban Veerasooran return 0;
861d70a0d8fSParthiban Veerasooran }
862d70a0d8fSParthiban Veerasooran
863d70a0d8fSParthiban Veerasooran /* Process the chunk with previous rx frame end and next rx frame
864d70a0d8fSParthiban Veerasooran * start.
865d70a0d8fSParthiban Veerasooran */
866d70a0d8fSParthiban Veerasooran if (start_valid && end_valid && start_byte_offset > end_byte_offset) {
867d70a0d8fSParthiban Veerasooran /* After rx buffer overflow error received, there might be a
868d70a0d8fSParthiban Veerasooran * possibility of getting an end valid of a previously
869d70a0d8fSParthiban Veerasooran * incomplete rx frame along with the new rx frame start valid.
870d70a0d8fSParthiban Veerasooran */
871d70a0d8fSParthiban Veerasooran if (tc6->rx_skb) {
872d70a0d8fSParthiban Veerasooran size = end_byte_offset + 1;
873d70a0d8fSParthiban Veerasooran oa_tc6_prcs_rx_frame_end(tc6, data, size);
874d70a0d8fSParthiban Veerasooran }
875d70a0d8fSParthiban Veerasooran size = OA_TC6_CHUNK_PAYLOAD_SIZE - start_byte_offset;
876d70a0d8fSParthiban Veerasooran return oa_tc6_prcs_rx_frame_start(tc6,
877d70a0d8fSParthiban Veerasooran &data[start_byte_offset],
878d70a0d8fSParthiban Veerasooran size);
879d70a0d8fSParthiban Veerasooran }
880d70a0d8fSParthiban Veerasooran
881d70a0d8fSParthiban Veerasooran /* Process the chunk with ongoing rx frame data */
882d70a0d8fSParthiban Veerasooran oa_tc6_prcs_ongoing_rx_frame(tc6, data, footer);
883d70a0d8fSParthiban Veerasooran
884d70a0d8fSParthiban Veerasooran return 0;
885d70a0d8fSParthiban Veerasooran }
886d70a0d8fSParthiban Veerasooran
oa_tc6_get_rx_chunk_footer(struct oa_tc6 * tc6,u16 footer_offset)88753fbde8aSParthiban Veerasooran static u32 oa_tc6_get_rx_chunk_footer(struct oa_tc6 *tc6, u16 footer_offset)
88853fbde8aSParthiban Veerasooran {
88953fbde8aSParthiban Veerasooran u8 *rx_buf = tc6->spi_data_rx_buf;
89053fbde8aSParthiban Veerasooran __be32 footer;
89153fbde8aSParthiban Veerasooran
89253fbde8aSParthiban Veerasooran footer = *((__be32 *)&rx_buf[footer_offset]);
89353fbde8aSParthiban Veerasooran
89453fbde8aSParthiban Veerasooran return be32_to_cpu(footer);
89553fbde8aSParthiban Veerasooran }
89653fbde8aSParthiban Veerasooran
oa_tc6_process_spi_data_rx_buf(struct oa_tc6 * tc6,u16 length)89753fbde8aSParthiban Veerasooran static int oa_tc6_process_spi_data_rx_buf(struct oa_tc6 *tc6, u16 length)
89853fbde8aSParthiban Veerasooran {
89953fbde8aSParthiban Veerasooran u16 no_of_rx_chunks = length / OA_TC6_CHUNK_SIZE;
90053fbde8aSParthiban Veerasooran u32 footer;
90153fbde8aSParthiban Veerasooran int ret;
90253fbde8aSParthiban Veerasooran
90353fbde8aSParthiban Veerasooran /* All the rx chunks in the receive SPI data buffer are examined here */
90453fbde8aSParthiban Veerasooran for (int i = 0; i < no_of_rx_chunks; i++) {
90553fbde8aSParthiban Veerasooran /* Last 4 bytes in each received chunk consist footer info */
90653fbde8aSParthiban Veerasooran footer = oa_tc6_get_rx_chunk_footer(tc6, i * OA_TC6_CHUNK_SIZE +
90753fbde8aSParthiban Veerasooran OA_TC6_CHUNK_PAYLOAD_SIZE);
90853fbde8aSParthiban Veerasooran
90953fbde8aSParthiban Veerasooran ret = oa_tc6_process_rx_chunk_footer(tc6, footer);
91053fbde8aSParthiban Veerasooran if (ret)
91153fbde8aSParthiban Veerasooran return ret;
912d70a0d8fSParthiban Veerasooran
913d70a0d8fSParthiban Veerasooran /* If there is a data valid chunks then process it for the
914d70a0d8fSParthiban Veerasooran * information needed to determine the validity and the location
915d70a0d8fSParthiban Veerasooran * of the receive frame data.
916d70a0d8fSParthiban Veerasooran */
917d70a0d8fSParthiban Veerasooran if (FIELD_GET(OA_TC6_DATA_FOOTER_DATA_VALID, footer)) {
918d70a0d8fSParthiban Veerasooran u8 *payload = tc6->spi_data_rx_buf + i *
919d70a0d8fSParthiban Veerasooran OA_TC6_CHUNK_SIZE;
920d70a0d8fSParthiban Veerasooran
921d70a0d8fSParthiban Veerasooran ret = oa_tc6_prcs_rx_chunk_payload(tc6, payload,
922d70a0d8fSParthiban Veerasooran footer);
923d70a0d8fSParthiban Veerasooran if (ret)
924d70a0d8fSParthiban Veerasooran return ret;
925d70a0d8fSParthiban Veerasooran }
92653fbde8aSParthiban Veerasooran }
92753fbde8aSParthiban Veerasooran
92853fbde8aSParthiban Veerasooran return 0;
92953fbde8aSParthiban Veerasooran }
93053fbde8aSParthiban Veerasooran
oa_tc6_prepare_data_header(bool data_valid,bool start_valid,bool end_valid,u8 end_byte_offset)93153fbde8aSParthiban Veerasooran static __be32 oa_tc6_prepare_data_header(bool data_valid, bool start_valid,
93253fbde8aSParthiban Veerasooran bool end_valid, u8 end_byte_offset)
93353fbde8aSParthiban Veerasooran {
93453fbde8aSParthiban Veerasooran u32 header = FIELD_PREP(OA_TC6_DATA_HEADER_DATA_NOT_CTRL,
93553fbde8aSParthiban Veerasooran OA_TC6_DATA_HEADER) |
93653fbde8aSParthiban Veerasooran FIELD_PREP(OA_TC6_DATA_HEADER_DATA_VALID, data_valid) |
93753fbde8aSParthiban Veerasooran FIELD_PREP(OA_TC6_DATA_HEADER_START_VALID, start_valid) |
93853fbde8aSParthiban Veerasooran FIELD_PREP(OA_TC6_DATA_HEADER_END_VALID, end_valid) |
93953fbde8aSParthiban Veerasooran FIELD_PREP(OA_TC6_DATA_HEADER_END_BYTE_OFFSET,
94053fbde8aSParthiban Veerasooran end_byte_offset);
94153fbde8aSParthiban Veerasooran
94253fbde8aSParthiban Veerasooran header |= FIELD_PREP(OA_TC6_DATA_HEADER_PARITY,
94353fbde8aSParthiban Veerasooran oa_tc6_get_parity(header));
94453fbde8aSParthiban Veerasooran
94553fbde8aSParthiban Veerasooran return cpu_to_be32(header);
94653fbde8aSParthiban Veerasooran }
94753fbde8aSParthiban Veerasooran
oa_tc6_add_tx_skb_to_spi_buf(struct oa_tc6 * tc6)94853fbde8aSParthiban Veerasooran static void oa_tc6_add_tx_skb_to_spi_buf(struct oa_tc6 *tc6)
94953fbde8aSParthiban Veerasooran {
95053fbde8aSParthiban Veerasooran enum oa_tc6_data_end_valid_info end_valid = OA_TC6_DATA_END_INVALID;
95153fbde8aSParthiban Veerasooran __be32 *tx_buf = tc6->spi_data_tx_buf + tc6->spi_data_tx_buf_offset;
95253fbde8aSParthiban Veerasooran u16 remaining_len = tc6->ongoing_tx_skb->len - tc6->tx_skb_offset;
95353fbde8aSParthiban Veerasooran u8 *tx_skb_data = tc6->ongoing_tx_skb->data + tc6->tx_skb_offset;
95453fbde8aSParthiban Veerasooran enum oa_tc6_data_start_valid_info start_valid;
95553fbde8aSParthiban Veerasooran u8 end_byte_offset = 0;
95653fbde8aSParthiban Veerasooran u16 length_to_copy;
95753fbde8aSParthiban Veerasooran
95853fbde8aSParthiban Veerasooran /* Initial value is assigned here to avoid more than 80 characters in
95953fbde8aSParthiban Veerasooran * the declaration place.
96053fbde8aSParthiban Veerasooran */
96153fbde8aSParthiban Veerasooran start_valid = OA_TC6_DATA_START_INVALID;
96253fbde8aSParthiban Veerasooran
96353fbde8aSParthiban Veerasooran /* Set start valid if the current tx chunk contains the start of the tx
96453fbde8aSParthiban Veerasooran * ethernet frame.
96553fbde8aSParthiban Veerasooran */
96653fbde8aSParthiban Veerasooran if (!tc6->tx_skb_offset)
96753fbde8aSParthiban Veerasooran start_valid = OA_TC6_DATA_START_VALID;
96853fbde8aSParthiban Veerasooran
96953fbde8aSParthiban Veerasooran /* If the remaining tx skb length is more than the chunk payload size of
97053fbde8aSParthiban Veerasooran * 64 bytes then copy only 64 bytes and leave the ongoing tx skb for
97153fbde8aSParthiban Veerasooran * next tx chunk.
97253fbde8aSParthiban Veerasooran */
97353fbde8aSParthiban Veerasooran length_to_copy = min_t(u16, remaining_len, OA_TC6_CHUNK_PAYLOAD_SIZE);
97453fbde8aSParthiban Veerasooran
97553fbde8aSParthiban Veerasooran /* Copy the tx skb data to the tx chunk payload buffer */
97653fbde8aSParthiban Veerasooran memcpy(tx_buf + 1, tx_skb_data, length_to_copy);
97753fbde8aSParthiban Veerasooran tc6->tx_skb_offset += length_to_copy;
97853fbde8aSParthiban Veerasooran
97953fbde8aSParthiban Veerasooran /* Set end valid if the current tx chunk contains the end of the tx
98053fbde8aSParthiban Veerasooran * ethernet frame.
98153fbde8aSParthiban Veerasooran */
98253fbde8aSParthiban Veerasooran if (tc6->ongoing_tx_skb->len == tc6->tx_skb_offset) {
98353fbde8aSParthiban Veerasooran end_valid = OA_TC6_DATA_END_VALID;
98453fbde8aSParthiban Veerasooran end_byte_offset = length_to_copy - 1;
98553fbde8aSParthiban Veerasooran tc6->tx_skb_offset = 0;
98653fbde8aSParthiban Veerasooran tc6->netdev->stats.tx_bytes += tc6->ongoing_tx_skb->len;
98753fbde8aSParthiban Veerasooran tc6->netdev->stats.tx_packets++;
98853fbde8aSParthiban Veerasooran kfree_skb(tc6->ongoing_tx_skb);
98953fbde8aSParthiban Veerasooran tc6->ongoing_tx_skb = NULL;
99053fbde8aSParthiban Veerasooran }
99153fbde8aSParthiban Veerasooran
99253fbde8aSParthiban Veerasooran *tx_buf = oa_tc6_prepare_data_header(OA_TC6_DATA_VALID, start_valid,
99353fbde8aSParthiban Veerasooran end_valid, end_byte_offset);
99453fbde8aSParthiban Veerasooran tc6->spi_data_tx_buf_offset += OA_TC6_CHUNK_SIZE;
99553fbde8aSParthiban Veerasooran }
99653fbde8aSParthiban Veerasooran
oa_tc6_prepare_spi_tx_buf_for_tx_skbs(struct oa_tc6 * tc6)99753fbde8aSParthiban Veerasooran static u16 oa_tc6_prepare_spi_tx_buf_for_tx_skbs(struct oa_tc6 *tc6)
99853fbde8aSParthiban Veerasooran {
99953fbde8aSParthiban Veerasooran u16 used_tx_credits;
100053fbde8aSParthiban Veerasooran
100153fbde8aSParthiban Veerasooran /* Get tx skbs and convert them into tx chunks based on the tx credits
100253fbde8aSParthiban Veerasooran * available.
100353fbde8aSParthiban Veerasooran */
100453fbde8aSParthiban Veerasooran for (used_tx_credits = 0; used_tx_credits < tc6->tx_credits;
100553fbde8aSParthiban Veerasooran used_tx_credits++) {
100653fbde8aSParthiban Veerasooran if (!tc6->ongoing_tx_skb) {
100753fbde8aSParthiban Veerasooran tc6->ongoing_tx_skb = tc6->waiting_tx_skb;
100853fbde8aSParthiban Veerasooran tc6->waiting_tx_skb = NULL;
100953fbde8aSParthiban Veerasooran }
101053fbde8aSParthiban Veerasooran if (!tc6->ongoing_tx_skb)
101153fbde8aSParthiban Veerasooran break;
101253fbde8aSParthiban Veerasooran oa_tc6_add_tx_skb_to_spi_buf(tc6);
101353fbde8aSParthiban Veerasooran }
101453fbde8aSParthiban Veerasooran
101553fbde8aSParthiban Veerasooran return used_tx_credits * OA_TC6_CHUNK_SIZE;
101653fbde8aSParthiban Veerasooran }
101753fbde8aSParthiban Veerasooran
oa_tc6_add_empty_chunks_to_spi_buf(struct oa_tc6 * tc6,u16 needed_empty_chunks)1018d70a0d8fSParthiban Veerasooran static void oa_tc6_add_empty_chunks_to_spi_buf(struct oa_tc6 *tc6,
1019d70a0d8fSParthiban Veerasooran u16 needed_empty_chunks)
1020d70a0d8fSParthiban Veerasooran {
1021d70a0d8fSParthiban Veerasooran __be32 header;
1022d70a0d8fSParthiban Veerasooran
1023d70a0d8fSParthiban Veerasooran header = oa_tc6_prepare_data_header(OA_TC6_DATA_INVALID,
1024d70a0d8fSParthiban Veerasooran OA_TC6_DATA_START_INVALID,
1025d70a0d8fSParthiban Veerasooran OA_TC6_DATA_END_INVALID, 0);
1026d70a0d8fSParthiban Veerasooran
1027d70a0d8fSParthiban Veerasooran while (needed_empty_chunks--) {
1028d70a0d8fSParthiban Veerasooran __be32 *tx_buf = tc6->spi_data_tx_buf +
1029d70a0d8fSParthiban Veerasooran tc6->spi_data_tx_buf_offset;
1030d70a0d8fSParthiban Veerasooran
1031d70a0d8fSParthiban Veerasooran *tx_buf = header;
1032d70a0d8fSParthiban Veerasooran tc6->spi_data_tx_buf_offset += OA_TC6_CHUNK_SIZE;
1033d70a0d8fSParthiban Veerasooran }
1034d70a0d8fSParthiban Veerasooran }
1035d70a0d8fSParthiban Veerasooran
oa_tc6_prepare_spi_tx_buf_for_rx_chunks(struct oa_tc6 * tc6,u16 len)1036d70a0d8fSParthiban Veerasooran static u16 oa_tc6_prepare_spi_tx_buf_for_rx_chunks(struct oa_tc6 *tc6, u16 len)
1037d70a0d8fSParthiban Veerasooran {
1038d70a0d8fSParthiban Veerasooran u16 tx_chunks = len / OA_TC6_CHUNK_SIZE;
1039d70a0d8fSParthiban Veerasooran u16 needed_empty_chunks;
1040d70a0d8fSParthiban Veerasooran
1041d70a0d8fSParthiban Veerasooran /* If there are more chunks to receive than to transmit, we need to add
1042d70a0d8fSParthiban Veerasooran * enough empty tx chunks to allow the reception of the excess rx
1043d70a0d8fSParthiban Veerasooran * chunks.
1044d70a0d8fSParthiban Veerasooran */
1045d70a0d8fSParthiban Veerasooran if (tx_chunks >= tc6->rx_chunks_available)
1046d70a0d8fSParthiban Veerasooran return len;
1047d70a0d8fSParthiban Veerasooran
1048d70a0d8fSParthiban Veerasooran needed_empty_chunks = tc6->rx_chunks_available - tx_chunks;
1049d70a0d8fSParthiban Veerasooran
1050d70a0d8fSParthiban Veerasooran oa_tc6_add_empty_chunks_to_spi_buf(tc6, needed_empty_chunks);
1051d70a0d8fSParthiban Veerasooran
1052d70a0d8fSParthiban Veerasooran return needed_empty_chunks * OA_TC6_CHUNK_SIZE + len;
1053d70a0d8fSParthiban Veerasooran }
1054d70a0d8fSParthiban Veerasooran
oa_tc6_try_spi_transfer(struct oa_tc6 * tc6)105553fbde8aSParthiban Veerasooran static int oa_tc6_try_spi_transfer(struct oa_tc6 *tc6)
105653fbde8aSParthiban Veerasooran {
105753fbde8aSParthiban Veerasooran int ret;
105853fbde8aSParthiban Veerasooran
105953fbde8aSParthiban Veerasooran while (true) {
1060d70a0d8fSParthiban Veerasooran u16 spi_len = 0;
106153fbde8aSParthiban Veerasooran
106253fbde8aSParthiban Veerasooran tc6->spi_data_tx_buf_offset = 0;
106353fbde8aSParthiban Veerasooran
106453fbde8aSParthiban Veerasooran if (tc6->ongoing_tx_skb || tc6->waiting_tx_skb)
1065d70a0d8fSParthiban Veerasooran spi_len = oa_tc6_prepare_spi_tx_buf_for_tx_skbs(tc6);
106653fbde8aSParthiban Veerasooran
1067d70a0d8fSParthiban Veerasooran spi_len = oa_tc6_prepare_spi_tx_buf_for_rx_chunks(tc6, spi_len);
1068d70a0d8fSParthiban Veerasooran
10692c6ce535SParthiban Veerasooran if (tc6->int_flag) {
10702c6ce535SParthiban Veerasooran tc6->int_flag = false;
10712c6ce535SParthiban Veerasooran if (spi_len == 0) {
10722c6ce535SParthiban Veerasooran oa_tc6_add_empty_chunks_to_spi_buf(tc6, 1);
10732c6ce535SParthiban Veerasooran spi_len = OA_TC6_CHUNK_SIZE;
10742c6ce535SParthiban Veerasooran }
10752c6ce535SParthiban Veerasooran }
10762c6ce535SParthiban Veerasooran
1077d70a0d8fSParthiban Veerasooran if (spi_len == 0)
107853fbde8aSParthiban Veerasooran break;
107953fbde8aSParthiban Veerasooran
1080d70a0d8fSParthiban Veerasooran ret = oa_tc6_spi_transfer(tc6, OA_TC6_DATA_HEADER, spi_len);
108153fbde8aSParthiban Veerasooran if (ret) {
108253fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "SPI data transfer failed: %d\n",
108353fbde8aSParthiban Veerasooran ret);
108453fbde8aSParthiban Veerasooran return ret;
108553fbde8aSParthiban Veerasooran }
108653fbde8aSParthiban Veerasooran
1087d70a0d8fSParthiban Veerasooran ret = oa_tc6_process_spi_data_rx_buf(tc6, spi_len);
108853fbde8aSParthiban Veerasooran if (ret) {
1089d70a0d8fSParthiban Veerasooran if (ret == -EAGAIN)
1090d70a0d8fSParthiban Veerasooran continue;
1091d70a0d8fSParthiban Veerasooran
109253fbde8aSParthiban Veerasooran oa_tc6_cleanup_ongoing_tx_skb(tc6);
1093d70a0d8fSParthiban Veerasooran oa_tc6_cleanup_ongoing_rx_skb(tc6);
109453fbde8aSParthiban Veerasooran netdev_err(tc6->netdev, "Device error: %d\n", ret);
109553fbde8aSParthiban Veerasooran return ret;
109653fbde8aSParthiban Veerasooran }
109753fbde8aSParthiban Veerasooran
109853fbde8aSParthiban Veerasooran if (!tc6->waiting_tx_skb && netif_queue_stopped(tc6->netdev))
109953fbde8aSParthiban Veerasooran netif_wake_queue(tc6->netdev);
110053fbde8aSParthiban Veerasooran }
110153fbde8aSParthiban Veerasooran
110253fbde8aSParthiban Veerasooran return 0;
110353fbde8aSParthiban Veerasooran }
110453fbde8aSParthiban Veerasooran
oa_tc6_spi_thread_handler(void * data)110553fbde8aSParthiban Veerasooran static int oa_tc6_spi_thread_handler(void *data)
110653fbde8aSParthiban Veerasooran {
110753fbde8aSParthiban Veerasooran struct oa_tc6 *tc6 = data;
110853fbde8aSParthiban Veerasooran int ret;
110953fbde8aSParthiban Veerasooran
111053fbde8aSParthiban Veerasooran while (likely(!kthread_should_stop())) {
11112c6ce535SParthiban Veerasooran /* This kthread will be waken up if there is a tx skb or mac-phy
11122c6ce535SParthiban Veerasooran * interrupt to perform spi transfer with tx chunks.
11132c6ce535SParthiban Veerasooran */
111453fbde8aSParthiban Veerasooran wait_event_interruptible(tc6->spi_wq, tc6->waiting_tx_skb ||
11152c6ce535SParthiban Veerasooran tc6->int_flag ||
111653fbde8aSParthiban Veerasooran kthread_should_stop());
111753fbde8aSParthiban Veerasooran
111853fbde8aSParthiban Veerasooran if (kthread_should_stop())
111953fbde8aSParthiban Veerasooran break;
112053fbde8aSParthiban Veerasooran
112153fbde8aSParthiban Veerasooran ret = oa_tc6_try_spi_transfer(tc6);
112253fbde8aSParthiban Veerasooran if (ret)
112353fbde8aSParthiban Veerasooran return ret;
112453fbde8aSParthiban Veerasooran }
112553fbde8aSParthiban Veerasooran
112653fbde8aSParthiban Veerasooran return 0;
112753fbde8aSParthiban Veerasooran }
112853fbde8aSParthiban Veerasooran
oa_tc6_update_buffer_status_from_register(struct oa_tc6 * tc6)112953fbde8aSParthiban Veerasooran static int oa_tc6_update_buffer_status_from_register(struct oa_tc6 *tc6)
113053fbde8aSParthiban Veerasooran {
113153fbde8aSParthiban Veerasooran u32 value;
113253fbde8aSParthiban Veerasooran int ret;
113353fbde8aSParthiban Veerasooran
1134d70a0d8fSParthiban Veerasooran /* Initially tx credits and rx chunks available to be updated from the
1135d70a0d8fSParthiban Veerasooran * register as there is no data transfer performed yet. Later they will
1136d70a0d8fSParthiban Veerasooran * be updated from the rx footer.
113753fbde8aSParthiban Veerasooran */
113853fbde8aSParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_BUFFER_STATUS, &value);
113953fbde8aSParthiban Veerasooran if (ret)
114053fbde8aSParthiban Veerasooran return ret;
114153fbde8aSParthiban Veerasooran
114253fbde8aSParthiban Veerasooran tc6->tx_credits = FIELD_GET(BUFFER_STATUS_TX_CREDITS_AVAILABLE, value);
1143d70a0d8fSParthiban Veerasooran tc6->rx_chunks_available = FIELD_GET(BUFFER_STATUS_RX_CHUNKS_AVAILABLE,
1144d70a0d8fSParthiban Veerasooran value);
114553fbde8aSParthiban Veerasooran
114653fbde8aSParthiban Veerasooran return 0;
114753fbde8aSParthiban Veerasooran }
114853fbde8aSParthiban Veerasooran
oa_tc6_macphy_isr(int irq,void * data)11492c6ce535SParthiban Veerasooran static irqreturn_t oa_tc6_macphy_isr(int irq, void *data)
11502c6ce535SParthiban Veerasooran {
11512c6ce535SParthiban Veerasooran struct oa_tc6 *tc6 = data;
11522c6ce535SParthiban Veerasooran
11532c6ce535SParthiban Veerasooran /* MAC-PHY interrupt can occur for the following reasons.
11542c6ce535SParthiban Veerasooran * - availability of tx credits if it was 0 before and not reported in
11552c6ce535SParthiban Veerasooran * the previous rx footer.
11562c6ce535SParthiban Veerasooran * - availability of rx chunks if it was 0 before and not reported in
11572c6ce535SParthiban Veerasooran * the previous rx footer.
11582c6ce535SParthiban Veerasooran * - extended status event not reported in the previous rx footer.
11592c6ce535SParthiban Veerasooran */
11602c6ce535SParthiban Veerasooran tc6->int_flag = true;
11612c6ce535SParthiban Veerasooran /* Wake spi kthread to perform spi transfer */
11622c6ce535SParthiban Veerasooran wake_up_interruptible(&tc6->spi_wq);
11632c6ce535SParthiban Veerasooran
11642c6ce535SParthiban Veerasooran return IRQ_HANDLED;
11652c6ce535SParthiban Veerasooran }
11662c6ce535SParthiban Veerasooran
116753fbde8aSParthiban Veerasooran /**
1168*afd42170SParthiban Veerasooran * oa_tc6_zero_align_receive_frame_enable - function to enable zero align
1169*afd42170SParthiban Veerasooran * receive frame feature.
1170*afd42170SParthiban Veerasooran * @tc6: oa_tc6 struct.
1171*afd42170SParthiban Veerasooran *
1172*afd42170SParthiban Veerasooran * Return: 0 on success otherwise failed.
1173*afd42170SParthiban Veerasooran */
oa_tc6_zero_align_receive_frame_enable(struct oa_tc6 * tc6)1174*afd42170SParthiban Veerasooran int oa_tc6_zero_align_receive_frame_enable(struct oa_tc6 *tc6)
1175*afd42170SParthiban Veerasooran {
1176*afd42170SParthiban Veerasooran u32 regval;
1177*afd42170SParthiban Veerasooran int ret;
1178*afd42170SParthiban Veerasooran
1179*afd42170SParthiban Veerasooran ret = oa_tc6_read_register(tc6, OA_TC6_REG_CONFIG0, ®val);
1180*afd42170SParthiban Veerasooran if (ret)
1181*afd42170SParthiban Veerasooran return ret;
1182*afd42170SParthiban Veerasooran
1183*afd42170SParthiban Veerasooran /* Set Zero-Align Receive Frame Enable */
1184*afd42170SParthiban Veerasooran regval |= CONFIG0_ZARFE_ENABLE;
1185*afd42170SParthiban Veerasooran
1186*afd42170SParthiban Veerasooran return oa_tc6_write_register(tc6, OA_TC6_REG_CONFIG0, regval);
1187*afd42170SParthiban Veerasooran }
1188*afd42170SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_zero_align_receive_frame_enable);
1189*afd42170SParthiban Veerasooran
1190*afd42170SParthiban Veerasooran /**
119153fbde8aSParthiban Veerasooran * oa_tc6_start_xmit - function for sending the tx skb which consists ethernet
119253fbde8aSParthiban Veerasooran * frame.
119353fbde8aSParthiban Veerasooran * @tc6: oa_tc6 struct.
119453fbde8aSParthiban Veerasooran * @skb: socket buffer in which the ethernet frame is stored.
119553fbde8aSParthiban Veerasooran *
119653fbde8aSParthiban Veerasooran * Return: NETDEV_TX_OK if the transmit ethernet frame skb added in the tx_skb_q
119753fbde8aSParthiban Veerasooran * otherwise returns NETDEV_TX_BUSY.
119853fbde8aSParthiban Veerasooran */
oa_tc6_start_xmit(struct oa_tc6 * tc6,struct sk_buff * skb)119953fbde8aSParthiban Veerasooran netdev_tx_t oa_tc6_start_xmit(struct oa_tc6 *tc6, struct sk_buff *skb)
120053fbde8aSParthiban Veerasooran {
120153fbde8aSParthiban Veerasooran if (tc6->waiting_tx_skb) {
120253fbde8aSParthiban Veerasooran netif_stop_queue(tc6->netdev);
120353fbde8aSParthiban Veerasooran return NETDEV_TX_BUSY;
120453fbde8aSParthiban Veerasooran }
120553fbde8aSParthiban Veerasooran
120653fbde8aSParthiban Veerasooran if (skb_linearize(skb)) {
120753fbde8aSParthiban Veerasooran dev_kfree_skb_any(skb);
120853fbde8aSParthiban Veerasooran tc6->netdev->stats.tx_dropped++;
120953fbde8aSParthiban Veerasooran return NETDEV_TX_OK;
121053fbde8aSParthiban Veerasooran }
121153fbde8aSParthiban Veerasooran
121253fbde8aSParthiban Veerasooran tc6->waiting_tx_skb = skb;
121353fbde8aSParthiban Veerasooran
121453fbde8aSParthiban Veerasooran /* Wake spi kthread to perform spi transfer */
121553fbde8aSParthiban Veerasooran wake_up_interruptible(&tc6->spi_wq);
121653fbde8aSParthiban Veerasooran
121753fbde8aSParthiban Veerasooran return NETDEV_TX_OK;
121853fbde8aSParthiban Veerasooran }
121953fbde8aSParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_start_xmit);
122053fbde8aSParthiban Veerasooran
1221aa58bec0SParthiban Veerasooran /**
1222aa58bec0SParthiban Veerasooran * oa_tc6_init - allocates and initializes oa_tc6 structure.
1223aa58bec0SParthiban Veerasooran * @spi: device with which data will be exchanged.
12248f9bf857SParthiban Veerasooran * @netdev: network device interface structure.
1225aa58bec0SParthiban Veerasooran *
1226aa58bec0SParthiban Veerasooran * Return: pointer reference to the oa_tc6 structure if the MAC-PHY
1227aa58bec0SParthiban Veerasooran * initialization is successful otherwise NULL.
1228aa58bec0SParthiban Veerasooran */
oa_tc6_init(struct spi_device * spi,struct net_device * netdev)12298f9bf857SParthiban Veerasooran struct oa_tc6 *oa_tc6_init(struct spi_device *spi, struct net_device *netdev)
1230aa58bec0SParthiban Veerasooran {
1231aa58bec0SParthiban Veerasooran struct oa_tc6 *tc6;
12321f9c4eedSParthiban Veerasooran int ret;
1233aa58bec0SParthiban Veerasooran
1234aa58bec0SParthiban Veerasooran tc6 = devm_kzalloc(&spi->dev, sizeof(*tc6), GFP_KERNEL);
1235aa58bec0SParthiban Veerasooran if (!tc6)
1236aa58bec0SParthiban Veerasooran return NULL;
1237aa58bec0SParthiban Veerasooran
1238aa58bec0SParthiban Veerasooran tc6->spi = spi;
12398f9bf857SParthiban Veerasooran tc6->netdev = netdev;
12408f9bf857SParthiban Veerasooran SET_NETDEV_DEV(netdev, &spi->dev);
1241aa58bec0SParthiban Veerasooran mutex_init(&tc6->spi_ctrl_lock);
1242aa58bec0SParthiban Veerasooran
1243aa58bec0SParthiban Veerasooran /* Set the SPI controller to pump at realtime priority */
1244aa58bec0SParthiban Veerasooran tc6->spi->rt = true;
1245aa58bec0SParthiban Veerasooran spi_setup(tc6->spi);
1246aa58bec0SParthiban Veerasooran
1247aa58bec0SParthiban Veerasooran tc6->spi_ctrl_tx_buf = devm_kzalloc(&tc6->spi->dev,
1248aa58bec0SParthiban Veerasooran OA_TC6_CTRL_SPI_BUF_SIZE,
1249aa58bec0SParthiban Veerasooran GFP_KERNEL);
1250aa58bec0SParthiban Veerasooran if (!tc6->spi_ctrl_tx_buf)
1251aa58bec0SParthiban Veerasooran return NULL;
1252aa58bec0SParthiban Veerasooran
1253aa58bec0SParthiban Veerasooran tc6->spi_ctrl_rx_buf = devm_kzalloc(&tc6->spi->dev,
1254aa58bec0SParthiban Veerasooran OA_TC6_CTRL_SPI_BUF_SIZE,
1255aa58bec0SParthiban Veerasooran GFP_KERNEL);
1256aa58bec0SParthiban Veerasooran if (!tc6->spi_ctrl_rx_buf)
1257aa58bec0SParthiban Veerasooran return NULL;
1258aa58bec0SParthiban Veerasooran
125953fbde8aSParthiban Veerasooran tc6->spi_data_tx_buf = devm_kzalloc(&tc6->spi->dev,
126053fbde8aSParthiban Veerasooran OA_TC6_SPI_DATA_BUF_SIZE,
126153fbde8aSParthiban Veerasooran GFP_KERNEL);
126253fbde8aSParthiban Veerasooran if (!tc6->spi_data_tx_buf)
126353fbde8aSParthiban Veerasooran return NULL;
126453fbde8aSParthiban Veerasooran
126553fbde8aSParthiban Veerasooran tc6->spi_data_rx_buf = devm_kzalloc(&tc6->spi->dev,
126653fbde8aSParthiban Veerasooran OA_TC6_SPI_DATA_BUF_SIZE,
126753fbde8aSParthiban Veerasooran GFP_KERNEL);
126853fbde8aSParthiban Veerasooran if (!tc6->spi_data_rx_buf)
126953fbde8aSParthiban Veerasooran return NULL;
127053fbde8aSParthiban Veerasooran
12711f9c4eedSParthiban Veerasooran ret = oa_tc6_sw_reset_macphy(tc6);
12721f9c4eedSParthiban Veerasooran if (ret) {
12731f9c4eedSParthiban Veerasooran dev_err(&tc6->spi->dev,
12741f9c4eedSParthiban Veerasooran "MAC-PHY software reset failed: %d\n", ret);
12751f9c4eedSParthiban Veerasooran return NULL;
12761f9c4eedSParthiban Veerasooran }
12771f9c4eedSParthiban Veerasooran
127886c03a0fSParthiban Veerasooran ret = oa_tc6_unmask_macphy_error_interrupts(tc6);
127986c03a0fSParthiban Veerasooran if (ret) {
128086c03a0fSParthiban Veerasooran dev_err(&tc6->spi->dev,
128186c03a0fSParthiban Veerasooran "MAC-PHY error interrupts unmask failed: %d\n", ret);
128286c03a0fSParthiban Veerasooran return NULL;
128386c03a0fSParthiban Veerasooran }
128486c03a0fSParthiban Veerasooran
12858f9bf857SParthiban Veerasooran ret = oa_tc6_phy_init(tc6);
12868f9bf857SParthiban Veerasooran if (ret) {
12878f9bf857SParthiban Veerasooran dev_err(&tc6->spi->dev,
12888f9bf857SParthiban Veerasooran "MAC internal PHY initialization failed: %d\n", ret);
12898f9bf857SParthiban Veerasooran return NULL;
12908f9bf857SParthiban Veerasooran }
12918f9bf857SParthiban Veerasooran
1292f845a027SParthiban Veerasooran ret = oa_tc6_enable_data_transfer(tc6);
1293f845a027SParthiban Veerasooran if (ret) {
1294f845a027SParthiban Veerasooran dev_err(&tc6->spi->dev, "Failed to enable data transfer: %d\n",
1295f845a027SParthiban Veerasooran ret);
1296f845a027SParthiban Veerasooran goto phy_exit;
1297f845a027SParthiban Veerasooran }
1298f845a027SParthiban Veerasooran
129953fbde8aSParthiban Veerasooran ret = oa_tc6_update_buffer_status_from_register(tc6);
130053fbde8aSParthiban Veerasooran if (ret) {
130153fbde8aSParthiban Veerasooran dev_err(&tc6->spi->dev,
130253fbde8aSParthiban Veerasooran "Failed to update buffer status: %d\n", ret);
130353fbde8aSParthiban Veerasooran goto phy_exit;
130453fbde8aSParthiban Veerasooran }
130553fbde8aSParthiban Veerasooran
130653fbde8aSParthiban Veerasooran init_waitqueue_head(&tc6->spi_wq);
130753fbde8aSParthiban Veerasooran
130853fbde8aSParthiban Veerasooran tc6->spi_thread = kthread_run(oa_tc6_spi_thread_handler, tc6,
130953fbde8aSParthiban Veerasooran "oa-tc6-spi-thread");
131053fbde8aSParthiban Veerasooran if (IS_ERR(tc6->spi_thread)) {
131153fbde8aSParthiban Veerasooran dev_err(&tc6->spi->dev, "Failed to create SPI thread\n");
131253fbde8aSParthiban Veerasooran goto phy_exit;
131353fbde8aSParthiban Veerasooran }
131453fbde8aSParthiban Veerasooran
131553fbde8aSParthiban Veerasooran sched_set_fifo(tc6->spi_thread);
131653fbde8aSParthiban Veerasooran
13172c6ce535SParthiban Veerasooran ret = devm_request_irq(&tc6->spi->dev, tc6->spi->irq, oa_tc6_macphy_isr,
13182c6ce535SParthiban Veerasooran IRQF_TRIGGER_FALLING, dev_name(&tc6->spi->dev),
13192c6ce535SParthiban Veerasooran tc6);
13202c6ce535SParthiban Veerasooran if (ret) {
13212c6ce535SParthiban Veerasooran dev_err(&tc6->spi->dev, "Failed to request macphy isr %d\n",
13222c6ce535SParthiban Veerasooran ret);
13232c6ce535SParthiban Veerasooran goto kthread_stop;
13242c6ce535SParthiban Veerasooran }
13252c6ce535SParthiban Veerasooran
13262c6ce535SParthiban Veerasooran /* oa_tc6_sw_reset_macphy() function resets and clears the MAC-PHY reset
13272c6ce535SParthiban Veerasooran * complete status. IRQ is also asserted on reset completion and it is
13282c6ce535SParthiban Veerasooran * remain asserted until MAC-PHY receives a data chunk. So performing an
13292c6ce535SParthiban Veerasooran * empty data chunk transmission will deassert the IRQ. Refer section
13302c6ce535SParthiban Veerasooran * 7.7 and 9.2.8.8 in the OPEN Alliance specification for more details.
13312c6ce535SParthiban Veerasooran */
13322c6ce535SParthiban Veerasooran tc6->int_flag = true;
13332c6ce535SParthiban Veerasooran wake_up_interruptible(&tc6->spi_wq);
13342c6ce535SParthiban Veerasooran
1335aa58bec0SParthiban Veerasooran return tc6;
1336f845a027SParthiban Veerasooran
13372c6ce535SParthiban Veerasooran kthread_stop:
13382c6ce535SParthiban Veerasooran kthread_stop(tc6->spi_thread);
1339f845a027SParthiban Veerasooran phy_exit:
1340f845a027SParthiban Veerasooran oa_tc6_phy_exit(tc6);
1341f845a027SParthiban Veerasooran return NULL;
1342aa58bec0SParthiban Veerasooran }
1343aa58bec0SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_init);
1344aa58bec0SParthiban Veerasooran
13458f9bf857SParthiban Veerasooran /**
13468f9bf857SParthiban Veerasooran * oa_tc6_exit - exit function.
13478f9bf857SParthiban Veerasooran * @tc6: oa_tc6 struct.
13488f9bf857SParthiban Veerasooran */
oa_tc6_exit(struct oa_tc6 * tc6)13498f9bf857SParthiban Veerasooran void oa_tc6_exit(struct oa_tc6 *tc6)
13508f9bf857SParthiban Veerasooran {
13518f9bf857SParthiban Veerasooran oa_tc6_phy_exit(tc6);
135253fbde8aSParthiban Veerasooran kthread_stop(tc6->spi_thread);
135353fbde8aSParthiban Veerasooran dev_kfree_skb_any(tc6->ongoing_tx_skb);
135453fbde8aSParthiban Veerasooran dev_kfree_skb_any(tc6->waiting_tx_skb);
1355d70a0d8fSParthiban Veerasooran dev_kfree_skb_any(tc6->rx_skb);
13568f9bf857SParthiban Veerasooran }
13578f9bf857SParthiban Veerasooran EXPORT_SYMBOL_GPL(oa_tc6_exit);
13588f9bf857SParthiban Veerasooran
1359aa58bec0SParthiban Veerasooran MODULE_DESCRIPTION("OPEN Alliance 10BASE‑T1x MAC‑PHY Serial Interface Lib");
1360aa58bec0SParthiban Veerasooran MODULE_AUTHOR("Parthiban Veerasooran <parthiban.veerasooran@microchip.com>");
1361aa58bec0SParthiban Veerasooran MODULE_LICENSE("GPL");
1362