1 // SPDX-License-Identifier: GPL-2.0-only
2 /* r8169_firmware.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
14 #include "r8169_firmware.h"
22 PHY_CLEAR_READCOUNT
= 0x7,
24 PHY_READCOUNT_EQ_SKIP
= 0x9,
25 PHY_COMP_EQ_SKIPN
= 0xa,
26 PHY_COMP_NEQ_SKIPN
= 0xb,
27 PHY_WRITE_PREVIOUS
= 0xc,
34 char version
[RTL_VER_SIZE
];
40 #define FW_OPCODE_SIZE sizeof_field(struct rtl_fw_phy_action, code[0])
42 static bool rtl_fw_format_ok(struct rtl_fw
*rtl_fw
)
44 const struct firmware
*fw
= rtl_fw
->fw
;
45 struct fw_info
*fw_info
= (struct fw_info
*)fw
->data
;
46 struct rtl_fw_phy_action
*pa
= &rtl_fw
->phy_action
;
48 if (fw
->size
< FW_OPCODE_SIZE
)
51 if (!fw_info
->magic
) {
52 size_t i
, size
, start
;
55 if (fw
->size
< sizeof(*fw_info
))
58 for (i
= 0; i
< fw
->size
; i
++)
59 checksum
+= fw
->data
[i
];
63 start
= le32_to_cpu(fw_info
->fw_start
);
67 size
= le32_to_cpu(fw_info
->fw_len
);
68 if (size
> (fw
->size
- start
) / FW_OPCODE_SIZE
)
71 strscpy(rtl_fw
->version
, fw_info
->version
, RTL_VER_SIZE
);
73 pa
->code
= (__le32
*)(fw
->data
+ start
);
76 if (fw
->size
% FW_OPCODE_SIZE
)
79 strscpy(rtl_fw
->version
, rtl_fw
->fw_name
, RTL_VER_SIZE
);
81 pa
->code
= (__le32
*)fw
->data
;
82 pa
->size
= fw
->size
/ FW_OPCODE_SIZE
;
88 static bool rtl_fw_data_ok(struct rtl_fw
*rtl_fw
)
90 struct rtl_fw_phy_action
*pa
= &rtl_fw
->phy_action
;
93 for (index
= 0; index
< pa
->size
; index
++) {
94 u32 action
= le32_to_cpu(pa
->code
[index
]);
95 u32 val
= action
& 0x0000ffff;
96 u32 regno
= (action
& 0x0fff0000) >> 16;
98 switch (action
>> 28) {
102 case PHY_CLEAR_READCOUNT
:
104 case PHY_WRITE_PREVIOUS
:
117 case PHY_READCOUNT_EQ_SKIP
:
118 if (index
+ 2 >= pa
->size
)
121 case PHY_COMP_EQ_SKIPN
:
122 case PHY_COMP_NEQ_SKIPN
:
124 if (index
+ 1 + regno
>= pa
->size
)
129 dev_err(rtl_fw
->dev
, "Invalid action 0x%08x\n", action
);
136 dev_err(rtl_fw
->dev
, "Out of range of firmware\n");
140 void rtl_fw_write_firmware(struct rtl8169_private
*tp
, struct rtl_fw
*rtl_fw
)
142 struct rtl_fw_phy_action
*pa
= &rtl_fw
->phy_action
;
143 rtl_fw_write_t fw_write
= rtl_fw
->phy_write
;
144 rtl_fw_read_t fw_read
= rtl_fw
->phy_read
;
145 int predata
= 0, count
= 0;
148 for (index
= 0; index
< pa
->size
; index
++) {
149 u32 action
= le32_to_cpu(pa
->code
[index
]);
150 u32 data
= action
& 0x0000ffff;
151 u32 regno
= (action
& 0x0fff0000) >> 16;
152 enum rtl_fw_opcode opcode
= action
>> 28;
156 predata
= fw_read(tp
, regno
);
166 index
-= (regno
+ 1);
170 fw_write
= rtl_fw
->mac_mcu_write
;
171 fw_read
= rtl_fw
->mac_mcu_read
;
173 fw_write
= rtl_fw
->phy_write
;
174 fw_read
= rtl_fw
->phy_read
;
178 case PHY_CLEAR_READCOUNT
:
182 fw_write(tp
, regno
, data
);
184 case PHY_READCOUNT_EQ_SKIP
:
188 case PHY_COMP_EQ_SKIPN
:
192 case PHY_COMP_NEQ_SKIPN
:
196 case PHY_WRITE_PREVIOUS
:
197 fw_write(tp
, regno
, predata
);
209 void rtl_fw_release_firmware(struct rtl_fw
*rtl_fw
)
211 release_firmware(rtl_fw
->fw
);
214 int rtl_fw_request_firmware(struct rtl_fw
*rtl_fw
)
218 rc
= firmware_request_nowarn(&rtl_fw
->fw
, rtl_fw
->fw_name
, rtl_fw
->dev
);
222 if (!rtl_fw_format_ok(rtl_fw
) || !rtl_fw_data_ok(rtl_fw
)) {
223 release_firmware(rtl_fw
->fw
);
230 dev_warn(rtl_fw
->dev
, "Unable to load firmware %s (%d)\n",
231 rtl_fw
->fw_name
, rc
);