blob: 027225e1ade26d3fe8b43ca4ebe69b34fea05da4 [file] [log] [blame]
Thomas Gleixner4fa9c49f2019-05-29 07:18:05 -07001// SPDX-License-Identifier: GPL-2.0-only
Noam Camus0dd07702015-06-23 11:43:53 +03002/*
3 * Copyright(c) 2015 EZchip Technologies.
Noam Camus0dd07702015-06-23 11:43:53 +03004 */
5
6#include <linux/module.h>
7#include <linux/etherdevice.h>
Florian Westphal282ccf62017-03-29 17:17:31 +02008#include <linux/interrupt.h>
Noam Camus0dd07702015-06-23 11:43:53 +03009#include <linux/of_address.h>
10#include <linux/of_irq.h>
11#include <linux/of_net.h>
12#include <linux/of_platform.h>
13#include "nps_enet.h"
14
15#define DRV_NAME "nps_mgt_enet"
16
Elad Kanfi094f57a2016-07-13 16:58:07 +030017static inline bool nps_enet_is_tx_pending(struct nps_enet_priv *priv)
18{
19 u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL);
20 u32 tx_ctrl_ct = (tx_ctrl_value & TX_CTL_CT_MASK) >> TX_CTL_CT_SHIFT;
21
22 return (!tx_ctrl_ct && priv->tx_skb);
23}
24
Noam Camus0dd07702015-06-23 11:43:53 +030025static void nps_enet_clean_rx_fifo(struct net_device *ndev, u32 frame_len)
26{
27 struct nps_enet_priv *priv = netdev_priv(ndev);
28 u32 i, len = DIV_ROUND_UP(frame_len, sizeof(u32));
29
30 /* Empty Rx FIFO buffer by reading all words */
31 for (i = 0; i < len; i++)
32 nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
33}
34
35static void nps_enet_read_rx_fifo(struct net_device *ndev,
36 unsigned char *dst, u32 length)
37{
38 struct nps_enet_priv *priv = netdev_priv(ndev);
39 s32 i, last = length & (sizeof(u32) - 1);
40 u32 *reg = (u32 *)dst, len = length / sizeof(u32);
41 bool dst_is_aligned = IS_ALIGNED((unsigned long)dst, sizeof(u32));
42
43 /* In case dst is not aligned we need an intermediate buffer */
Lada Trimasovab54b8c22016-03-03 17:07:46 +030044 if (dst_is_aligned) {
45 ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, reg, len);
46 reg += len;
Elad Kanfiddbff3e2016-07-13 16:58:06 +030047 } else { /* !dst_is_aligned */
Noam Camus0dd07702015-06-23 11:43:53 +030048 for (i = 0; i < len; i++, reg++) {
Arnd Bergmannb0a8d1a2015-12-08 16:28:59 +010049 u32 buf = nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
Elad Kanfiddbff3e2016-07-13 16:58:06 +030050
Lada Trimasovab54b8c22016-03-03 17:07:46 +030051 put_unaligned_be32(buf, reg);
Noam Camus0dd07702015-06-23 11:43:53 +030052 }
53 }
Noam Camus0dd07702015-06-23 11:43:53 +030054 /* copy last bytes (if any) */
55 if (last) {
Lada Trimasovab54b8c22016-03-03 17:07:46 +030056 u32 buf;
Elad Kanfiddbff3e2016-07-13 16:58:06 +030057
Lada Trimasovab54b8c22016-03-03 17:07:46 +030058 ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, &buf, 1);
59 memcpy((u8 *)reg, &buf, last);
Noam Camus0dd07702015-06-23 11:43:53 +030060 }
61}
62
63static u32 nps_enet_rx_handler(struct net_device *ndev)
64{
65 u32 frame_len, err = 0;
66 u32 work_done = 0;
67 struct nps_enet_priv *priv = netdev_priv(ndev);
68 struct sk_buff *skb;
Lada Trimasovab54b8c22016-03-03 17:07:46 +030069 u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL);
70 u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT;
71 u32 rx_ctrl_er = (rx_ctrl_value & RX_CTL_ER_MASK) >> RX_CTL_ER_SHIFT;
72 u32 rx_ctrl_crc = (rx_ctrl_value & RX_CTL_CRC_MASK) >> RX_CTL_CRC_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +030073
Lada Trimasovab54b8c22016-03-03 17:07:46 +030074 frame_len = (rx_ctrl_value & RX_CTL_NR_MASK) >> RX_CTL_NR_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +030075
76 /* Check if we got RX */
Lada Trimasovab54b8c22016-03-03 17:07:46 +030077 if (!rx_ctrl_cr)
Noam Camus0dd07702015-06-23 11:43:53 +030078 return work_done;
79
80 /* If we got here there is a work for us */
81 work_done++;
82
83 /* Check Rx error */
Lada Trimasovab54b8c22016-03-03 17:07:46 +030084 if (rx_ctrl_er) {
Noam Camus0dd07702015-06-23 11:43:53 +030085 ndev->stats.rx_errors++;
86 err = 1;
87 }
88
89 /* Check Rx CRC error */
Lada Trimasovab54b8c22016-03-03 17:07:46 +030090 if (rx_ctrl_crc) {
Noam Camus0dd07702015-06-23 11:43:53 +030091 ndev->stats.rx_crc_errors++;
92 ndev->stats.rx_dropped++;
93 err = 1;
94 }
95
96 /* Check Frame length Min 64b */
97 if (unlikely(frame_len < ETH_ZLEN)) {
98 ndev->stats.rx_length_errors++;
99 ndev->stats.rx_dropped++;
100 err = 1;
101 }
102
103 if (err)
104 goto rx_irq_clean;
105
106 /* Skb allocation */
107 skb = netdev_alloc_skb_ip_align(ndev, frame_len);
108 if (unlikely(!skb)) {
109 ndev->stats.rx_errors++;
110 ndev->stats.rx_dropped++;
111 goto rx_irq_clean;
112 }
113
114 /* Copy frame from Rx fifo into the skb */
115 nps_enet_read_rx_fifo(ndev, skb->data, frame_len);
116
117 skb_put(skb, frame_len);
118 skb->protocol = eth_type_trans(skb, ndev);
119 skb->ip_summed = CHECKSUM_UNNECESSARY;
120
121 ndev->stats.rx_packets++;
122 ndev->stats.rx_bytes += frame_len;
123 netif_receive_skb(skb);
124
125 goto rx_irq_frame_done;
126
127rx_irq_clean:
128 /* Clean Rx fifo */
129 nps_enet_clean_rx_fifo(ndev, frame_len);
130
131rx_irq_frame_done:
132 /* Ack Rx ctrl register */
133 nps_enet_reg_set(priv, NPS_ENET_REG_RX_CTL, 0);
134
135 return work_done;
136}
137
138static void nps_enet_tx_handler(struct net_device *ndev)
139{
140 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300141 u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300142 u32 tx_ctrl_et = (tx_ctrl_value & TX_CTL_ET_MASK) >> TX_CTL_ET_SHIFT;
143 u32 tx_ctrl_nt = (tx_ctrl_value & TX_CTL_NT_MASK) >> TX_CTL_NT_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300144
145 /* Check if we got TX */
Elad Kanfi094f57a2016-07-13 16:58:07 +0300146 if (!nps_enet_is_tx_pending(priv))
Noam Camus0dd07702015-06-23 11:43:53 +0300147 return;
148
Noam Camus3d99b742015-08-20 08:00:04 +0300149 /* Ack Tx ctrl register */
150 nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, 0);
151
Noam Camus0dd07702015-06-23 11:43:53 +0300152 /* Check Tx transmit error */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300153 if (unlikely(tx_ctrl_et)) {
Noam Camus0dd07702015-06-23 11:43:53 +0300154 ndev->stats.tx_errors++;
155 } else {
156 ndev->stats.tx_packets++;
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300157 ndev->stats.tx_bytes += tx_ctrl_nt;
Noam Camus0dd07702015-06-23 11:43:53 +0300158 }
159
Noam Camus93fcf832015-08-20 08:00:02 +0300160 dev_kfree_skb(priv->tx_skb);
Elad Kanfie5df49d2016-05-09 20:13:19 +0300161 priv->tx_skb = NULL;
Noam Camus0dd07702015-06-23 11:43:53 +0300162
163 if (netif_queue_stopped(ndev))
164 netif_wake_queue(ndev);
165}
166
167/**
168 * nps_enet_poll - NAPI poll handler.
169 * @napi: Pointer to napi_struct structure.
170 * @budget: How many frames to process on one call.
171 *
172 * returns: Number of processed frames
173 */
174static int nps_enet_poll(struct napi_struct *napi, int budget)
175{
176 struct net_device *ndev = napi->dev;
177 struct nps_enet_priv *priv = netdev_priv(ndev);
Noam Camus0dd07702015-06-23 11:43:53 +0300178 u32 work_done;
179
Noam Camus0dd07702015-06-23 11:43:53 +0300180 nps_enet_tx_handler(ndev);
181 work_done = nps_enet_rx_handler(ndev);
Zakharov Vlad358e78b2017-03-29 13:41:46 +0300182 if ((work_done < budget) && napi_complete_done(napi, work_done)) {
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300183 u32 buf_int_enable_value = 0;
Noam Camus41493792015-08-20 08:00:05 +0300184
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300185 /* set tx_done and rx_rdy bits */
186 buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT;
187 buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT;
188
Noam Camus0dd07702015-06-23 11:43:53 +0300189 nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300190 buf_int_enable_value);
Elad Kanfi05c00d82016-05-09 20:13:20 +0300191
192 /* in case we will get a tx interrupt while interrupts
193 * are masked, we will lose it since the tx is edge interrupt.
194 * specifically, while executing the code section above,
195 * between nps_enet_tx_handler and the interrupts enable, all
196 * tx requests will be stuck until we will get an rx interrupt.
197 * the two code lines below will solve this situation by
198 * re-adding ourselves to the poll list.
199 */
Elad Kanfi094f57a2016-07-13 16:58:07 +0300200 if (nps_enet_is_tx_pending(priv)) {
Elad Kanfi86651652016-05-26 15:00:06 +0300201 nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0);
Elad Kanfi05c00d82016-05-09 20:13:20 +0300202 napi_reschedule(napi);
Elad Kanfi86651652016-05-26 15:00:06 +0300203 }
Noam Camus0dd07702015-06-23 11:43:53 +0300204 }
205
206 return work_done;
207}
208
209/**
210 * nps_enet_irq_handler - Global interrupt handler for ENET.
211 * @irq: irq number.
212 * @dev_instance: device instance.
213 *
214 * returns: IRQ_HANDLED for all cases.
215 *
216 * EZchip ENET has 2 interrupt causes, and depending on bits raised in
217 * CTRL registers we may tell what is a reason for interrupt to fire up.
218 * We got one for RX and the other for TX (completion).
219 */
220static irqreturn_t nps_enet_irq_handler(s32 irq, void *dev_instance)
221{
222 struct net_device *ndev = dev_instance;
223 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300224 u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300225 u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300226
Elad Kanfi094f57a2016-07-13 16:58:07 +0300227 if (nps_enet_is_tx_pending(priv) || rx_ctrl_cr)
Noam Camus0dd07702015-06-23 11:43:53 +0300228 if (likely(napi_schedule_prep(&priv->napi))) {
229 nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0);
230 __napi_schedule(&priv->napi);
231 }
232
233 return IRQ_HANDLED;
234}
235
236static void nps_enet_set_hw_mac_address(struct net_device *ndev)
237{
238 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300239 u32 ge_mac_cfg_1_value = 0;
240 u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value;
Noam Camus0dd07702015-06-23 11:43:53 +0300241
242 /* set MAC address in HW */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300243 ge_mac_cfg_1_value |= ndev->dev_addr[0] << CFG_1_OCTET_0_SHIFT;
244 ge_mac_cfg_1_value |= ndev->dev_addr[1] << CFG_1_OCTET_1_SHIFT;
245 ge_mac_cfg_1_value |= ndev->dev_addr[2] << CFG_1_OCTET_2_SHIFT;
246 ge_mac_cfg_1_value |= ndev->dev_addr[3] << CFG_1_OCTET_3_SHIFT;
247 *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_4_MASK)
248 | ndev->dev_addr[4] << CFG_2_OCTET_4_SHIFT;
249 *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_5_MASK)
250 | ndev->dev_addr[5] << CFG_2_OCTET_5_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300251
252 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_1,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300253 ge_mac_cfg_1_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300254
255 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300256 *ge_mac_cfg_2_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300257}
258
259/**
260 * nps_enet_hw_reset - Reset the network device.
261 * @ndev: Pointer to the network device.
262 *
263 * This function reset the PCS and TX fifo.
264 * The programming model is to set the relevant reset bits
265 * wait for some time for this to propagate and then unset
266 * the reset bits. This way we ensure that reset procedure
267 * is done successfully by device.
268 */
269static void nps_enet_hw_reset(struct net_device *ndev)
270{
271 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300272 u32 ge_rst_value = 0, phase_fifo_ctl_value = 0;
Noam Camus0dd07702015-06-23 11:43:53 +0300273
Noam Camus0dd07702015-06-23 11:43:53 +0300274 /* Pcs reset sequence*/
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300275 ge_rst_value |= NPS_ENET_ENABLE << RST_GMAC_0_SHIFT;
276 nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300277 usleep_range(10, 20);
Noam Camus136ab0d2016-07-12 16:01:11 +0300278 ge_rst_value = 0;
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300279 nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300280
281 /* Tx fifo reset sequence */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300282 phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_RST_SHIFT;
283 phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_INIT_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300284 nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300285 phase_fifo_ctl_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300286 usleep_range(10, 20);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300287 phase_fifo_ctl_value = 0;
Noam Camus0dd07702015-06-23 11:43:53 +0300288 nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300289 phase_fifo_ctl_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300290}
291
292static void nps_enet_hw_enable_control(struct net_device *ndev)
293{
294 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300295 u32 ge_mac_cfg_0_value = 0, buf_int_enable_value = 0;
296 u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value;
297 u32 *ge_mac_cfg_3_value = &priv->ge_mac_cfg_3_value;
Noam Camus0dd07702015-06-23 11:43:53 +0300298 s32 max_frame_length;
299
Noam Camus0dd07702015-06-23 11:43:53 +0300300 /* Enable Rx and Tx statistics */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300301 *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_STAT_EN_MASK)
302 | NPS_ENET_GE_MAC_CFG_2_STAT_EN << CFG_2_STAT_EN_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300303
304 /* Discard packets with different MAC address */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300305 *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK)
306 | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300307
308 /* Discard multicast packets */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300309 *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK)
310 | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300311
312 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300313 *ge_mac_cfg_2_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300314
315 /* Discard Packets bigger than max frame length */
316 max_frame_length = ETH_HLEN + ndev->mtu + ETH_FCS_LEN;
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300317 if (max_frame_length <= NPS_ENET_MAX_FRAME_LENGTH) {
318 *ge_mac_cfg_3_value =
319 (*ge_mac_cfg_3_value & ~CFG_3_MAX_LEN_MASK)
320 | max_frame_length << CFG_3_MAX_LEN_SHIFT;
321 }
Noam Camus0dd07702015-06-23 11:43:53 +0300322
323 /* Enable interrupts */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300324 buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT;
325 buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300326 nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300327 buf_int_enable_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300328
329 /* Write device MAC address to HW */
330 nps_enet_set_hw_mac_address(ndev);
331
332 /* Rx and Tx HW features */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300333 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_PAD_EN_SHIFT;
334 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_CRC_EN_SHIFT;
335 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_CRC_STRIP_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300336
337 /* IFG configuration */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300338 ge_mac_cfg_0_value |=
339 NPS_ENET_GE_MAC_CFG_0_RX_IFG << CFG_0_RX_IFG_SHIFT;
340 ge_mac_cfg_0_value |=
341 NPS_ENET_GE_MAC_CFG_0_TX_IFG << CFG_0_TX_IFG_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300342
343 /* preamble configuration */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300344 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_PR_CHECK_EN_SHIFT;
345 ge_mac_cfg_0_value |=
346 NPS_ENET_GE_MAC_CFG_0_TX_PR_LEN << CFG_0_TX_PR_LEN_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300347
348 /* enable flow control frames */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300349 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_FC_EN_SHIFT;
350 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_FC_EN_SHIFT;
351 ge_mac_cfg_0_value |=
352 NPS_ENET_GE_MAC_CFG_0_TX_FC_RETR << CFG_0_TX_FC_RETR_SHIFT;
353 *ge_mac_cfg_3_value = (*ge_mac_cfg_3_value & ~CFG_3_CF_DROP_MASK)
354 | NPS_ENET_ENABLE << CFG_3_CF_DROP_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300355
356 /* Enable Rx and Tx */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300357 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_EN_SHIFT;
358 ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_EN_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300359
Noam Camusde671562015-08-20 08:00:03 +0300360 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_3,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300361 *ge_mac_cfg_3_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300362 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300363 ge_mac_cfg_0_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300364}
365
366static void nps_enet_hw_disable_control(struct net_device *ndev)
367{
368 struct nps_enet_priv *priv = netdev_priv(ndev);
369
370 /* Disable interrupts */
371 nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0);
372
373 /* Disable Rx and Tx */
374 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 0);
375}
376
377static void nps_enet_send_frame(struct net_device *ndev,
378 struct sk_buff *skb)
379{
380 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300381 u32 tx_ctrl_value = 0;
Noam Camus0dd07702015-06-23 11:43:53 +0300382 short length = skb->len;
383 u32 i, len = DIV_ROUND_UP(length, sizeof(u32));
Arnd Bergmannb0a8d1a2015-12-08 16:28:59 +0100384 u32 *src = (void *)skb->data;
Noam Camus0dd07702015-06-23 11:43:53 +0300385 bool src_is_aligned = IS_ALIGNED((unsigned long)src, sizeof(u32));
386
Noam Camus0dd07702015-06-23 11:43:53 +0300387 /* In case src is not aligned we need an intermediate buffer */
388 if (src_is_aligned)
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300389 iowrite32_rep(priv->regs_base + NPS_ENET_REG_TX_BUF, src, len);
Arnd Bergmannb0a8d1a2015-12-08 16:28:59 +0100390 else /* !src_is_aligned */
391 for (i = 0; i < len; i++, src++)
392 nps_enet_reg_set(priv, NPS_ENET_REG_TX_BUF,
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300393 get_unaligned_be32(src));
Noam Camus0dd07702015-06-23 11:43:53 +0300394
Noam Camus0dd07702015-06-23 11:43:53 +0300395 /* Write the length of the Frame */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300396 tx_ctrl_value |= length << TX_CTL_NT_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300397
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300398 tx_ctrl_value |= NPS_ENET_ENABLE << TX_CTL_CT_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300399 /* Send Frame */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300400 nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, tx_ctrl_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300401}
402
403/**
404 * nps_enet_set_mac_address - Set the MAC address for this device.
405 * @ndev: Pointer to net_device structure.
406 * @p: 6 byte Address to be written as MAC address.
407 *
408 * This function copies the HW address from the sockaddr structure to the
409 * net_device structure and updates the address in HW.
410 *
411 * returns: -EBUSY if the net device is busy or 0 if the address is set
412 * successfully.
413 */
414static s32 nps_enet_set_mac_address(struct net_device *ndev, void *p)
415{
416 struct sockaddr *addr = p;
417 s32 res;
418
419 if (netif_running(ndev))
420 return -EBUSY;
421
422 res = eth_mac_addr(ndev, p);
423 if (!res) {
424 ether_addr_copy(ndev->dev_addr, addr->sa_data);
425 nps_enet_set_hw_mac_address(ndev);
426 }
427
428 return res;
429}
430
431/**
432 * nps_enet_set_rx_mode - Change the receive filtering mode.
433 * @ndev: Pointer to the network device.
434 *
435 * This function enables/disables promiscuous mode
436 */
437static void nps_enet_set_rx_mode(struct net_device *ndev)
438{
439 struct nps_enet_priv *priv = netdev_priv(ndev);
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300440 u32 ge_mac_cfg_2_value = priv->ge_mac_cfg_2_value;
Noam Camus0dd07702015-06-23 11:43:53 +0300441
442 if (ndev->flags & IFF_PROMISC) {
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300443 ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK)
444 | NPS_ENET_DISABLE << CFG_2_DISK_DA_SHIFT;
445 ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK)
446 | NPS_ENET_DISABLE << CFG_2_DISK_MC_SHIFT;
447
Noam Camus0dd07702015-06-23 11:43:53 +0300448 } else {
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300449 ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK)
450 | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT;
451 ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK)
452 | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300453 }
454
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300455 nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, ge_mac_cfg_2_value);
Noam Camus0dd07702015-06-23 11:43:53 +0300456}
457
458/**
459 * nps_enet_open - Open the network device.
460 * @ndev: Pointer to the network device.
461 *
462 * returns: 0, on success or non-zero error value on failure.
463 *
464 * This function sets the MAC address, requests and enables an IRQ
465 * for the ENET device and starts the Tx queue.
466 */
467static s32 nps_enet_open(struct net_device *ndev)
468{
469 struct nps_enet_priv *priv = netdev_priv(ndev);
470 s32 err;
471
472 /* Reset private variables */
Elad Kanfie5df49d2016-05-09 20:13:19 +0300473 priv->tx_skb = NULL;
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300474 priv->ge_mac_cfg_2_value = 0;
475 priv->ge_mac_cfg_3_value = 0;
Noam Camus0dd07702015-06-23 11:43:53 +0300476
477 /* ge_mac_cfg_3 default values */
Lada Trimasovab54b8c22016-03-03 17:07:46 +0300478 priv->ge_mac_cfg_3_value |=
479 NPS_ENET_GE_MAC_CFG_3_RX_IFG_TH << CFG_3_RX_IFG_TH_SHIFT;
480
481 priv->ge_mac_cfg_3_value |=
482 NPS_ENET_GE_MAC_CFG_3_MAX_LEN << CFG_3_MAX_LEN_SHIFT;
Noam Camus0dd07702015-06-23 11:43:53 +0300483
484 /* Disable HW device */
485 nps_enet_hw_disable_control(ndev);
486
487 /* irq Rx allocation */
488 err = request_irq(priv->irq, nps_enet_irq_handler,
489 0, "enet-rx-tx", ndev);
490 if (err)
491 return err;
492
493 napi_enable(&priv->napi);
494
495 /* Enable HW device */
496 nps_enet_hw_reset(ndev);
497 nps_enet_hw_enable_control(ndev);
498
499 netif_start_queue(ndev);
500
501 return 0;
502}
503
504/**
505 * nps_enet_stop - Close the network device.
506 * @ndev: Pointer to the network device.
507 *
508 * This function stops the Tx queue, disables interrupts for the ENET device.
509 */
510static s32 nps_enet_stop(struct net_device *ndev)
511{
512 struct nps_enet_priv *priv = netdev_priv(ndev);
513
514 napi_disable(&priv->napi);
515 netif_stop_queue(ndev);
516 nps_enet_hw_disable_control(ndev);
517 free_irq(priv->irq, ndev);
518
519 return 0;
520}
521
522/**
523 * nps_enet_start_xmit - Starts the data transmission.
524 * @skb: sk_buff pointer that contains data to be Transmitted.
525 * @ndev: Pointer to net_device structure.
526 *
527 * returns: NETDEV_TX_OK, on success
528 * NETDEV_TX_BUSY, if any of the descriptors are not free.
529 *
530 * This function is invoked from upper layers to initiate transmission.
531 */
532static netdev_tx_t nps_enet_start_xmit(struct sk_buff *skb,
533 struct net_device *ndev)
534{
535 struct nps_enet_priv *priv = netdev_priv(ndev);
536
537 /* This driver handles one frame at a time */
538 netif_stop_queue(ndev);
539
Noam Camus0dd07702015-06-23 11:43:53 +0300540 priv->tx_skb = skb;
541
Elad Kanfie5df49d2016-05-09 20:13:19 +0300542 /* make sure tx_skb is actually written to the memory
543 * before the HW is informed and the IRQ is fired.
544 */
545 wmb();
546
Noam Camus93fcf832015-08-20 08:00:02 +0300547 nps_enet_send_frame(ndev, skb);
548
Noam Camus0dd07702015-06-23 11:43:53 +0300549 return NETDEV_TX_OK;
550}
551
552#ifdef CONFIG_NET_POLL_CONTROLLER
553static void nps_enet_poll_controller(struct net_device *ndev)
554{
555 disable_irq(ndev->irq);
556 nps_enet_irq_handler(ndev->irq, ndev);
557 enable_irq(ndev->irq);
558}
559#endif
560
561static const struct net_device_ops nps_netdev_ops = {
562 .ndo_open = nps_enet_open,
563 .ndo_stop = nps_enet_stop,
564 .ndo_start_xmit = nps_enet_start_xmit,
565 .ndo_set_mac_address = nps_enet_set_mac_address,
566 .ndo_set_rx_mode = nps_enet_set_rx_mode,
567#ifdef CONFIG_NET_POLL_CONTROLLER
568 .ndo_poll_controller = nps_enet_poll_controller,
569#endif
570};
571
572static s32 nps_enet_probe(struct platform_device *pdev)
573{
574 struct device *dev = &pdev->dev;
575 struct net_device *ndev;
576 struct nps_enet_priv *priv;
577 s32 err = 0;
578 const char *mac_addr;
579 struct resource *res_regs;
580
581 if (!dev->of_node)
582 return -ENODEV;
583
584 ndev = alloc_etherdev(sizeof(struct nps_enet_priv));
585 if (!ndev)
586 return -ENOMEM;
587
588 platform_set_drvdata(pdev, ndev);
589 SET_NETDEV_DEV(ndev, dev);
590 priv = netdev_priv(ndev);
591
592 /* The EZ NET specific entries in the device structure. */
593 ndev->netdev_ops = &nps_netdev_ops;
594 ndev->watchdog_timeo = (400 * HZ / 1000);
595 /* FIXME :: no multicast support yet */
596 ndev->flags &= ~IFF_MULTICAST;
597
598 res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
599 priv->regs_base = devm_ioremap_resource(dev, res_regs);
600 if (IS_ERR(priv->regs_base)) {
601 err = PTR_ERR(priv->regs_base);
602 goto out_netdev;
603 }
604 dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs_base);
605
606 /* set kernel MAC address to dev */
607 mac_addr = of_get_mac_address(dev->of_node);
Petr Štetiara51645f2019-05-06 23:27:04 +0200608 if (!IS_ERR(mac_addr))
Noam Camus0dd07702015-06-23 11:43:53 +0300609 ether_addr_copy(ndev->dev_addr, mac_addr);
610 else
611 eth_hw_addr_random(ndev);
612
613 /* Get IRQ number */
614 priv->irq = platform_get_irq(pdev, 0);
615 if (!priv->irq) {
616 dev_err(dev, "failed to retrieve <irq Rx-Tx> value from device tree\n");
617 err = -ENODEV;
618 goto out_netdev;
619 }
620
621 netif_napi_add(ndev, &priv->napi, nps_enet_poll,
622 NPS_ENET_NAPI_POLL_WEIGHT);
623
624 /* Register the driver. Should be the last thing in probe */
625 err = register_netdev(ndev);
626 if (err) {
627 dev_err(dev, "Failed to register ndev for %s, err = 0x%08x\n",
628 ndev->name, (s32)err);
629 goto out_netif_api;
630 }
631
632 dev_info(dev, "(rx/tx=%d)\n", priv->irq);
633 return 0;
634
635out_netif_api:
636 netif_napi_del(&priv->napi);
637out_netdev:
638 if (err)
639 free_netdev(ndev);
640
641 return err;
642}
643
644static s32 nps_enet_remove(struct platform_device *pdev)
645{
646 struct net_device *ndev = platform_get_drvdata(pdev);
647 struct nps_enet_priv *priv = netdev_priv(ndev);
648
649 unregister_netdev(ndev);
650 free_netdev(ndev);
651 netif_napi_del(&priv->napi);
652
653 return 0;
654}
655
656static const struct of_device_id nps_enet_dt_ids[] = {
657 { .compatible = "ezchip,nps-mgt-enet" },
658 { /* Sentinel */ }
659};
Javier Martinez Canillasfc971a22016-10-17 11:05:40 -0300660MODULE_DEVICE_TABLE(of, nps_enet_dt_ids);
Noam Camus0dd07702015-06-23 11:43:53 +0300661
662static struct platform_driver nps_enet_driver = {
663 .probe = nps_enet_probe,
664 .remove = nps_enet_remove,
665 .driver = {
666 .name = DRV_NAME,
667 .of_match_table = nps_enet_dt_ids,
668 },
669};
670
671module_platform_driver(nps_enet_driver);
672
673MODULE_AUTHOR("EZchip Semiconductor");
674MODULE_LICENSE("GPL v2");