Loading...
1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
2/*
3 * Copyright 2008 - 2015 Freescale Semiconductor Inc.
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/of_address.h>
11#include <linux/of_platform.h>
12#include <linux/of_net.h>
13#include <linux/of_mdio.h>
14#include <linux/device.h>
15#include <linux/phy.h>
16#include <linux/netdevice.h>
17#include <linux/phy_fixed.h>
18#include <linux/phylink.h>
19#include <linux/etherdevice.h>
20#include <linux/libfdt_env.h>
21#include <linux/platform_device.h>
22
23#include "mac.h"
24#include "fman_mac.h"
25#include "fman_dtsec.h"
26#include "fman_tgec.h"
27#include "fman_memac.h"
28
29MODULE_LICENSE("Dual BSD/GPL");
30MODULE_DESCRIPTION("FSL FMan MAC API based driver");
31
32struct mac_priv_s {
33 u8 cell_index;
34 struct fman *fman;
35 /* List of multicast addresses */
36 struct list_head mc_addr_list;
37 struct platform_device *eth_dev;
38 u16 speed;
39};
40
41struct mac_address {
42 u8 addr[ETH_ALEN];
43 struct list_head list;
44};
45
46static void mac_exception(struct mac_device *mac_dev,
47 enum fman_mac_exceptions ex)
48{
49 if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
50 /* don't flag RX FIFO after the first */
51 mac_dev->set_exception(mac_dev->fman_mac,
52 FM_MAC_EX_10G_RX_FIFO_OVFL, false);
53 dev_err(mac_dev->dev, "10G MAC got RX FIFO Error = %x\n", ex);
54 }
55
56 dev_dbg(mac_dev->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
57 __func__, ex);
58}
59
60int fman_set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
61{
62 struct mac_priv_s *priv;
63 struct mac_address *old_addr, *tmp;
64 struct netdev_hw_addr *ha;
65 int err;
66 enet_addr_t *addr;
67
68 priv = mac_dev->priv;
69
70 /* Clear previous address list */
71 list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
72 addr = (enet_addr_t *)old_addr->addr;
73 err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
74 if (err < 0)
75 return err;
76
77 list_del(&old_addr->list);
78 kfree(old_addr);
79 }
80
81 /* Add all the addresses from the new list */
82 netdev_for_each_mc_addr(ha, net_dev) {
83 addr = (enet_addr_t *)ha->addr;
84 err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
85 if (err < 0)
86 return err;
87
88 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
89 if (!tmp)
90 return -ENOMEM;
91
92 ether_addr_copy(tmp->addr, ha->addr);
93 list_add(&tmp->list, &priv->mc_addr_list);
94 }
95 return 0;
96}
97
98static DEFINE_MUTEX(eth_lock);
99
100static struct platform_device *dpaa_eth_add_device(int fman_id,
101 struct mac_device *mac_dev)
102{
103 struct platform_device *pdev;
104 struct dpaa_eth_data data;
105 struct mac_priv_s *priv;
106 static int dpaa_eth_dev_cnt;
107 int ret;
108
109 priv = mac_dev->priv;
110
111 data.mac_dev = mac_dev;
112 data.mac_hw_id = priv->cell_index;
113 data.fman_hw_id = fman_id;
114
115 mutex_lock(ð_lock);
116 pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
117 if (!pdev) {
118 ret = -ENOMEM;
119 goto no_mem;
120 }
121
122 pdev->dev.parent = mac_dev->dev;
123
124 ret = platform_device_add_data(pdev, &data, sizeof(data));
125 if (ret)
126 goto err;
127
128 ret = platform_device_add(pdev);
129 if (ret)
130 goto err;
131
132 dpaa_eth_dev_cnt++;
133 mutex_unlock(ð_lock);
134
135 return pdev;
136
137err:
138 platform_device_put(pdev);
139no_mem:
140 mutex_unlock(ð_lock);
141
142 return ERR_PTR(ret);
143}
144
145static const struct of_device_id mac_match[] = {
146 { .compatible = "fsl,fman-dtsec", .data = dtsec_initialization },
147 { .compatible = "fsl,fman-xgec", .data = tgec_initialization },
148 { .compatible = "fsl,fman-memac", .data = memac_initialization },
149 {}
150};
151MODULE_DEVICE_TABLE(of, mac_match);
152
153static int mac_probe(struct platform_device *_of_dev)
154{
155 int err, i, nph;
156 int (*init)(struct mac_device *mac_dev, struct device_node *mac_node,
157 struct fman_mac_params *params);
158 struct device *dev;
159 struct device_node *mac_node, *dev_node;
160 struct mac_device *mac_dev;
161 struct platform_device *of_dev;
162 struct mac_priv_s *priv;
163 struct fman_mac_params params;
164 u32 val;
165 u8 fman_id;
166 phy_interface_t phy_if;
167
168 dev = &_of_dev->dev;
169 mac_node = dev->of_node;
170 init = of_device_get_match_data(dev);
171
172 mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
173 if (!mac_dev)
174 return -ENOMEM;
175 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
176 if (!priv)
177 return -ENOMEM;
178 platform_set_drvdata(_of_dev, mac_dev);
179
180 /* Save private information */
181 mac_dev->priv = priv;
182 mac_dev->dev = dev;
183
184 INIT_LIST_HEAD(&priv->mc_addr_list);
185
186 /* Get the FM node */
187 dev_node = of_get_parent(mac_node);
188 if (!dev_node) {
189 dev_err(dev, "of_get_parent(%pOF) failed\n",
190 mac_node);
191 return -EINVAL;
192 }
193
194 of_dev = of_find_device_by_node(dev_node);
195 if (!of_dev) {
196 dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
197 err = -EINVAL;
198 goto _return_of_node_put;
199 }
200
201 /* Get the FMan cell-index */
202 err = of_property_read_u32(dev_node, "cell-index", &val);
203 if (err) {
204 dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
205 err = -EINVAL;
206 goto _return_of_node_put;
207 }
208 /* cell-index 0 => FMan id 1 */
209 fman_id = (u8)(val + 1);
210
211 priv->fman = fman_bind(&of_dev->dev);
212 if (!priv->fman) {
213 dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
214 err = -ENODEV;
215 goto _return_of_node_put;
216 }
217
218 of_node_put(dev_node);
219
220 /* Get the address of the memory mapped registers */
221 mac_dev->res = platform_get_mem_or_io(_of_dev, 0);
222 if (!mac_dev->res) {
223 dev_err(dev, "could not get registers\n");
224 return -EINVAL;
225 }
226
227 err = devm_request_resource(dev, fman_get_mem_region(priv->fman),
228 mac_dev->res);
229 if (err) {
230 dev_err_probe(dev, err, "could not request resource\n");
231 return err;
232 }
233
234 mac_dev->vaddr = devm_ioremap(dev, mac_dev->res->start,
235 resource_size(mac_dev->res));
236 if (!mac_dev->vaddr) {
237 dev_err(dev, "devm_ioremap() failed\n");
238 return -EIO;
239 }
240
241 if (!of_device_is_available(mac_node))
242 return -ENODEV;
243
244 /* Get the cell-index */
245 err = of_property_read_u32(mac_node, "cell-index", &val);
246 if (err) {
247 dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
248 return -EINVAL;
249 }
250 priv->cell_index = (u8)val;
251
252 /* Get the MAC address */
253 err = of_get_mac_address(mac_node, mac_dev->addr);
254 if (err)
255 dev_warn(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
256
257 /* Get the port handles */
258 nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
259 if (unlikely(nph < 0)) {
260 dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
261 mac_node);
262 return nph;
263 }
264
265 if (nph != ARRAY_SIZE(mac_dev->port)) {
266 dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
267 mac_node);
268 return -EINVAL;
269 }
270
271 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
272 /* Find the port node */
273 dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
274 if (!dev_node) {
275 dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
276 mac_node);
277 return -EINVAL;
278 }
279
280 of_dev = of_find_device_by_node(dev_node);
281 if (!of_dev) {
282 dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
283 dev_node);
284 err = -EINVAL;
285 goto _return_of_node_put;
286 }
287
288 mac_dev->port[i] = fman_port_bind(&of_dev->dev);
289 if (!mac_dev->port[i]) {
290 dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
291 dev_node);
292 err = -EINVAL;
293 goto _return_of_node_put;
294 }
295 of_node_put(dev_node);
296 }
297
298 /* Get the PHY connection type */
299 err = of_get_phy_mode(mac_node, &phy_if);
300 if (err) {
301 dev_warn(dev,
302 "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
303 mac_node);
304 phy_if = PHY_INTERFACE_MODE_SGMII;
305 }
306 mac_dev->phy_if = phy_if;
307
308 params.mac_id = priv->cell_index;
309 params.fm = (void *)priv->fman;
310 params.exception_cb = mac_exception;
311 params.event_cb = mac_exception;
312
313 err = init(mac_dev, mac_node, ¶ms);
314 if (err < 0)
315 return err;
316
317 if (!is_zero_ether_addr(mac_dev->addr))
318 dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr);
319
320 priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
321 if (IS_ERR(priv->eth_dev)) {
322 err = PTR_ERR(priv->eth_dev);
323 dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
324 priv->cell_index);
325 priv->eth_dev = NULL;
326 }
327
328 return err;
329
330_return_of_node_put:
331 of_node_put(dev_node);
332 return err;
333}
334
335static void mac_remove(struct platform_device *pdev)
336{
337 struct mac_device *mac_dev = platform_get_drvdata(pdev);
338
339 platform_device_unregister(mac_dev->priv->eth_dev);
340}
341
342static struct platform_driver mac_driver = {
343 .driver = {
344 .name = KBUILD_MODNAME,
345 .of_match_table = mac_match,
346 },
347 .probe = mac_probe,
348 .remove_new = mac_remove,
349};
350
351builtin_platform_driver(mac_driver);
1/* Copyright 2008-2015 Freescale Semiconductor, Inc.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * * Neither the name of Freescale Semiconductor nor the
11 * names of its contributors may be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 *
15 * ALTERNATIVELY, this software may be distributed under the terms of the
16 * GNU General Public License ("GPL") as published by the Free Software
17 * Foundation, either version 2 of that License or (at your option) any
18 * later version.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34#include <linux/init.h>
35#include <linux/module.h>
36#include <linux/of_address.h>
37#include <linux/of_platform.h>
38#include <linux/of_net.h>
39#include <linux/of_mdio.h>
40#include <linux/device.h>
41#include <linux/phy.h>
42#include <linux/netdevice.h>
43#include <linux/phy_fixed.h>
44#include <linux/etherdevice.h>
45#include <linux/libfdt_env.h>
46
47#include "mac.h"
48#include "fman_mac.h"
49#include "fman_dtsec.h"
50#include "fman_tgec.h"
51#include "fman_memac.h"
52
53MODULE_LICENSE("Dual BSD/GPL");
54MODULE_DESCRIPTION("FSL FMan MAC API based driver");
55
56struct mac_priv_s {
57 struct device *dev;
58 void __iomem *vaddr;
59 u8 cell_index;
60 phy_interface_t phy_if;
61 struct fman *fman;
62 struct device_node *phy_node;
63 struct device_node *internal_phy_node;
64 /* List of multicast addresses */
65 struct list_head mc_addr_list;
66 struct platform_device *eth_dev;
67 struct fixed_phy_status *fixed_link;
68 u16 speed;
69 u16 max_speed;
70
71 int (*enable)(struct fman_mac *mac_dev, enum comm_mode mode);
72 int (*disable)(struct fman_mac *mac_dev, enum comm_mode mode);
73};
74
75struct mac_address {
76 u8 addr[ETH_ALEN];
77 struct list_head list;
78};
79
80static void mac_exception(void *handle, enum fman_mac_exceptions ex)
81{
82 struct mac_device *mac_dev;
83 struct mac_priv_s *priv;
84
85 mac_dev = handle;
86 priv = mac_dev->priv;
87
88 if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
89 /* don't flag RX FIFO after the first */
90 mac_dev->set_exception(mac_dev->fman_mac,
91 FM_MAC_EX_10G_RX_FIFO_OVFL, false);
92 dev_err(priv->dev, "10G MAC got RX FIFO Error = %x\n", ex);
93 }
94
95 dev_dbg(priv->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
96 __func__, ex);
97}
98
99static void set_fman_mac_params(struct mac_device *mac_dev,
100 struct fman_mac_params *params)
101{
102 struct mac_priv_s *priv = mac_dev->priv;
103
104 params->base_addr = (typeof(params->base_addr))
105 devm_ioremap(priv->dev, mac_dev->res->start,
106 resource_size(mac_dev->res));
107 memcpy(¶ms->addr, mac_dev->addr, sizeof(mac_dev->addr));
108 params->max_speed = priv->max_speed;
109 params->phy_if = priv->phy_if;
110 params->basex_if = false;
111 params->mac_id = priv->cell_index;
112 params->fm = (void *)priv->fman;
113 params->exception_cb = mac_exception;
114 params->event_cb = mac_exception;
115 params->dev_id = mac_dev;
116 params->internal_phy_node = priv->internal_phy_node;
117}
118
119static int tgec_initialization(struct mac_device *mac_dev)
120{
121 int err;
122 struct mac_priv_s *priv;
123 struct fman_mac_params params;
124 u32 version;
125
126 priv = mac_dev->priv;
127
128 set_fman_mac_params(mac_dev, ¶ms);
129
130 mac_dev->fman_mac = tgec_config(¶ms);
131 if (!mac_dev->fman_mac) {
132 err = -EINVAL;
133 goto _return;
134 }
135
136 err = tgec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
137 if (err < 0)
138 goto _return_fm_mac_free;
139
140 err = tgec_init(mac_dev->fman_mac);
141 if (err < 0)
142 goto _return_fm_mac_free;
143
144 /* For 10G MAC, disable Tx ECC exception */
145 err = mac_dev->set_exception(mac_dev->fman_mac,
146 FM_MAC_EX_10G_TX_ECC_ER, false);
147 if (err < 0)
148 goto _return_fm_mac_free;
149
150 err = tgec_get_version(mac_dev->fman_mac, &version);
151 if (err < 0)
152 goto _return_fm_mac_free;
153
154 dev_info(priv->dev, "FMan XGEC version: 0x%08x\n", version);
155
156 goto _return;
157
158_return_fm_mac_free:
159 tgec_free(mac_dev->fman_mac);
160
161_return:
162 return err;
163}
164
165static int dtsec_initialization(struct mac_device *mac_dev)
166{
167 int err;
168 struct mac_priv_s *priv;
169 struct fman_mac_params params;
170 u32 version;
171
172 priv = mac_dev->priv;
173
174 set_fman_mac_params(mac_dev, ¶ms);
175
176 mac_dev->fman_mac = dtsec_config(¶ms);
177 if (!mac_dev->fman_mac) {
178 err = -EINVAL;
179 goto _return;
180 }
181
182 err = dtsec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
183 if (err < 0)
184 goto _return_fm_mac_free;
185
186 err = dtsec_cfg_pad_and_crc(mac_dev->fman_mac, true);
187 if (err < 0)
188 goto _return_fm_mac_free;
189
190 err = dtsec_init(mac_dev->fman_mac);
191 if (err < 0)
192 goto _return_fm_mac_free;
193
194 /* For 1G MAC, disable by default the MIB counters overflow interrupt */
195 err = mac_dev->set_exception(mac_dev->fman_mac,
196 FM_MAC_EX_1G_RX_MIB_CNT_OVFL, false);
197 if (err < 0)
198 goto _return_fm_mac_free;
199
200 err = dtsec_get_version(mac_dev->fman_mac, &version);
201 if (err < 0)
202 goto _return_fm_mac_free;
203
204 dev_info(priv->dev, "FMan dTSEC version: 0x%08x\n", version);
205
206 goto _return;
207
208_return_fm_mac_free:
209 dtsec_free(mac_dev->fman_mac);
210
211_return:
212 return err;
213}
214
215static int memac_initialization(struct mac_device *mac_dev)
216{
217 int err;
218 struct mac_priv_s *priv;
219 struct fman_mac_params params;
220
221 priv = mac_dev->priv;
222
223 set_fman_mac_params(mac_dev, ¶ms);
224
225 if (priv->max_speed == SPEED_10000)
226 params.phy_if = PHY_INTERFACE_MODE_XGMII;
227
228 mac_dev->fman_mac = memac_config(¶ms);
229 if (!mac_dev->fman_mac) {
230 err = -EINVAL;
231 goto _return;
232 }
233
234 err = memac_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
235 if (err < 0)
236 goto _return_fm_mac_free;
237
238 err = memac_cfg_reset_on_init(mac_dev->fman_mac, true);
239 if (err < 0)
240 goto _return_fm_mac_free;
241
242 err = memac_cfg_fixed_link(mac_dev->fman_mac, priv->fixed_link);
243 if (err < 0)
244 goto _return_fm_mac_free;
245
246 err = memac_init(mac_dev->fman_mac);
247 if (err < 0)
248 goto _return_fm_mac_free;
249
250 dev_info(priv->dev, "FMan MEMAC\n");
251
252 goto _return;
253
254_return_fm_mac_free:
255 memac_free(mac_dev->fman_mac);
256
257_return:
258 return err;
259}
260
261static int start(struct mac_device *mac_dev)
262{
263 int err;
264 struct phy_device *phy_dev = mac_dev->phy_dev;
265 struct mac_priv_s *priv = mac_dev->priv;
266
267 err = priv->enable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
268 if (!err && phy_dev)
269 phy_start(phy_dev);
270
271 return err;
272}
273
274static int stop(struct mac_device *mac_dev)
275{
276 struct mac_priv_s *priv = mac_dev->priv;
277
278 if (mac_dev->phy_dev)
279 phy_stop(mac_dev->phy_dev);
280
281 return priv->disable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
282}
283
284static int set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
285{
286 struct mac_priv_s *priv;
287 struct mac_address *old_addr, *tmp;
288 struct netdev_hw_addr *ha;
289 int err;
290 enet_addr_t *addr;
291
292 priv = mac_dev->priv;
293
294 /* Clear previous address list */
295 list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
296 addr = (enet_addr_t *)old_addr->addr;
297 err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
298 if (err < 0)
299 return err;
300
301 list_del(&old_addr->list);
302 kfree(old_addr);
303 }
304
305 /* Add all the addresses from the new list */
306 netdev_for_each_mc_addr(ha, net_dev) {
307 addr = (enet_addr_t *)ha->addr;
308 err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
309 if (err < 0)
310 return err;
311
312 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
313 if (!tmp)
314 return -ENOMEM;
315
316 ether_addr_copy(tmp->addr, ha->addr);
317 list_add(&tmp->list, &priv->mc_addr_list);
318 }
319 return 0;
320}
321
322/**
323 * fman_set_mac_active_pause
324 * @mac_dev: A pointer to the MAC device
325 * @rx: Pause frame setting for RX
326 * @tx: Pause frame setting for TX
327 *
328 * Set the MAC RX/TX PAUSE frames settings
329 *
330 * Avoid redundant calls to FMD, if the MAC driver already contains the desired
331 * active PAUSE settings. Otherwise, the new active settings should be reflected
332 * in FMan.
333 *
334 * Return: 0 on success; Error code otherwise.
335 */
336int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
337{
338 struct fman_mac *fman_mac = mac_dev->fman_mac;
339 int err = 0;
340
341 if (rx != mac_dev->rx_pause_active) {
342 err = mac_dev->set_rx_pause(fman_mac, rx);
343 if (likely(err == 0))
344 mac_dev->rx_pause_active = rx;
345 }
346
347 if (tx != mac_dev->tx_pause_active) {
348 u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
349 FSL_FM_PAUSE_TIME_DISABLE);
350
351 err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
352
353 if (likely(err == 0))
354 mac_dev->tx_pause_active = tx;
355 }
356
357 return err;
358}
359EXPORT_SYMBOL(fman_set_mac_active_pause);
360
361/**
362 * fman_get_pause_cfg
363 * @mac_dev: A pointer to the MAC device
364 * @rx: Return value for RX setting
365 * @tx: Return value for TX setting
366 *
367 * Determine the MAC RX/TX PAUSE frames settings based on PHY
368 * autonegotiation or values set by eththool.
369 *
370 * Return: Pointer to FMan device.
371 */
372void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
373 bool *tx_pause)
374{
375 struct phy_device *phy_dev = mac_dev->phy_dev;
376 u16 lcl_adv, rmt_adv;
377 u8 flowctrl;
378
379 *rx_pause = *tx_pause = false;
380
381 if (!phy_dev->duplex)
382 return;
383
384 /* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
385 * are those set by ethtool.
386 */
387 if (!mac_dev->autoneg_pause) {
388 *rx_pause = mac_dev->rx_pause_req;
389 *tx_pause = mac_dev->tx_pause_req;
390 return;
391 }
392
393 /* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
394 * settings depend on the result of the link negotiation.
395 */
396
397 /* get local capabilities */
398 lcl_adv = 0;
399 if (phy_dev->advertising & ADVERTISED_Pause)
400 lcl_adv |= ADVERTISE_PAUSE_CAP;
401 if (phy_dev->advertising & ADVERTISED_Asym_Pause)
402 lcl_adv |= ADVERTISE_PAUSE_ASYM;
403
404 /* get link partner capabilities */
405 rmt_adv = 0;
406 if (phy_dev->pause)
407 rmt_adv |= LPA_PAUSE_CAP;
408 if (phy_dev->asym_pause)
409 rmt_adv |= LPA_PAUSE_ASYM;
410
411 /* Calculate TX/RX settings based on local and peer advertised
412 * symmetric/asymmetric PAUSE capabilities.
413 */
414 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
415 if (flowctrl & FLOW_CTRL_RX)
416 *rx_pause = true;
417 if (flowctrl & FLOW_CTRL_TX)
418 *tx_pause = true;
419}
420EXPORT_SYMBOL(fman_get_pause_cfg);
421
422static void adjust_link_void(struct net_device *net_dev)
423{
424}
425
426static void adjust_link_dtsec(struct net_device *net_dev)
427{
428 struct device *dev = net_dev->dev.parent;
429 struct dpaa_eth_data *eth_data = dev->platform_data;
430 struct mac_device *mac_dev = eth_data->mac_dev;
431 struct phy_device *phy_dev = mac_dev->phy_dev;
432 struct fman_mac *fman_mac;
433 bool rx_pause, tx_pause;
434 int err;
435
436 fman_mac = mac_dev->fman_mac;
437 if (!phy_dev->link) {
438 dtsec_restart_autoneg(fman_mac);
439
440 return;
441 }
442
443 dtsec_adjust_link(fman_mac, phy_dev->speed);
444 fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
445 err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
446 if (err < 0)
447 netdev_err(net_dev, "fman_set_mac_active_pause() = %d\n", err);
448}
449
450static void adjust_link_memac(struct net_device *net_dev)
451{
452 struct device *dev = net_dev->dev.parent;
453 struct dpaa_eth_data *eth_data = dev->platform_data;
454 struct mac_device *mac_dev = eth_data->mac_dev;
455 struct phy_device *phy_dev = mac_dev->phy_dev;
456 struct fman_mac *fman_mac;
457 bool rx_pause, tx_pause;
458 int err;
459
460 fman_mac = mac_dev->fman_mac;
461 memac_adjust_link(fman_mac, phy_dev->speed);
462
463 fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
464 err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
465 if (err < 0)
466 netdev_err(net_dev, "fman_set_mac_active_pause() = %d\n", err);
467}
468
469/* Initializes driver's PHY state, and attaches to the PHY.
470 * Returns 0 on success.
471 */
472static struct phy_device *init_phy(struct net_device *net_dev,
473 struct mac_device *mac_dev,
474 void (*adj_lnk)(struct net_device *))
475{
476 struct phy_device *phy_dev;
477 struct mac_priv_s *priv = mac_dev->priv;
478
479 phy_dev = of_phy_connect(net_dev, priv->phy_node, adj_lnk, 0,
480 priv->phy_if);
481 if (!phy_dev) {
482 netdev_err(net_dev, "Could not connect to PHY\n");
483 return NULL;
484 }
485
486 /* Remove any features not supported by the controller */
487 phy_dev->supported &= mac_dev->if_support;
488 /* Enable the symmetric and asymmetric PAUSE frame advertisements,
489 * as most of the PHY drivers do not enable them by default.
490 */
491 phy_dev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
492 phy_dev->advertising = phy_dev->supported;
493
494 mac_dev->phy_dev = phy_dev;
495
496 return phy_dev;
497}
498
499static struct phy_device *dtsec_init_phy(struct net_device *net_dev,
500 struct mac_device *mac_dev)
501{
502 return init_phy(net_dev, mac_dev, &adjust_link_dtsec);
503}
504
505static struct phy_device *tgec_init_phy(struct net_device *net_dev,
506 struct mac_device *mac_dev)
507{
508 return init_phy(net_dev, mac_dev, adjust_link_void);
509}
510
511static struct phy_device *memac_init_phy(struct net_device *net_dev,
512 struct mac_device *mac_dev)
513{
514 return init_phy(net_dev, mac_dev, &adjust_link_memac);
515}
516
517static void setup_dtsec(struct mac_device *mac_dev)
518{
519 mac_dev->init_phy = dtsec_init_phy;
520 mac_dev->init = dtsec_initialization;
521 mac_dev->set_promisc = dtsec_set_promiscuous;
522 mac_dev->change_addr = dtsec_modify_mac_address;
523 mac_dev->add_hash_mac_addr = dtsec_add_hash_mac_address;
524 mac_dev->remove_hash_mac_addr = dtsec_del_hash_mac_address;
525 mac_dev->set_tx_pause = dtsec_set_tx_pause_frames;
526 mac_dev->set_rx_pause = dtsec_accept_rx_pause_frames;
527 mac_dev->set_exception = dtsec_set_exception;
528 mac_dev->set_multi = set_multi;
529 mac_dev->start = start;
530 mac_dev->stop = stop;
531
532 mac_dev->priv->enable = dtsec_enable;
533 mac_dev->priv->disable = dtsec_disable;
534}
535
536static void setup_tgec(struct mac_device *mac_dev)
537{
538 mac_dev->init_phy = tgec_init_phy;
539 mac_dev->init = tgec_initialization;
540 mac_dev->set_promisc = tgec_set_promiscuous;
541 mac_dev->change_addr = tgec_modify_mac_address;
542 mac_dev->add_hash_mac_addr = tgec_add_hash_mac_address;
543 mac_dev->remove_hash_mac_addr = tgec_del_hash_mac_address;
544 mac_dev->set_tx_pause = tgec_set_tx_pause_frames;
545 mac_dev->set_rx_pause = tgec_accept_rx_pause_frames;
546 mac_dev->set_exception = tgec_set_exception;
547 mac_dev->set_multi = set_multi;
548 mac_dev->start = start;
549 mac_dev->stop = stop;
550
551 mac_dev->priv->enable = tgec_enable;
552 mac_dev->priv->disable = tgec_disable;
553}
554
555static void setup_memac(struct mac_device *mac_dev)
556{
557 mac_dev->init_phy = memac_init_phy;
558 mac_dev->init = memac_initialization;
559 mac_dev->set_promisc = memac_set_promiscuous;
560 mac_dev->change_addr = memac_modify_mac_address;
561 mac_dev->add_hash_mac_addr = memac_add_hash_mac_address;
562 mac_dev->remove_hash_mac_addr = memac_del_hash_mac_address;
563 mac_dev->set_tx_pause = memac_set_tx_pause_frames;
564 mac_dev->set_rx_pause = memac_accept_rx_pause_frames;
565 mac_dev->set_exception = memac_set_exception;
566 mac_dev->set_multi = set_multi;
567 mac_dev->start = start;
568 mac_dev->stop = stop;
569
570 mac_dev->priv->enable = memac_enable;
571 mac_dev->priv->disable = memac_disable;
572}
573
574#define DTSEC_SUPPORTED \
575 (SUPPORTED_10baseT_Half \
576 | SUPPORTED_10baseT_Full \
577 | SUPPORTED_100baseT_Half \
578 | SUPPORTED_100baseT_Full \
579 | SUPPORTED_Autoneg \
580 | SUPPORTED_Pause \
581 | SUPPORTED_Asym_Pause \
582 | SUPPORTED_MII)
583
584static DEFINE_MUTEX(eth_lock);
585
586static const u16 phy2speed[] = {
587 [PHY_INTERFACE_MODE_MII] = SPEED_100,
588 [PHY_INTERFACE_MODE_GMII] = SPEED_1000,
589 [PHY_INTERFACE_MODE_SGMII] = SPEED_1000,
590 [PHY_INTERFACE_MODE_TBI] = SPEED_1000,
591 [PHY_INTERFACE_MODE_RMII] = SPEED_100,
592 [PHY_INTERFACE_MODE_RGMII] = SPEED_1000,
593 [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000,
594 [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000,
595 [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000,
596 [PHY_INTERFACE_MODE_RTBI] = SPEED_1000,
597 [PHY_INTERFACE_MODE_QSGMII] = SPEED_1000,
598 [PHY_INTERFACE_MODE_XGMII] = SPEED_10000
599};
600
601static struct platform_device *dpaa_eth_add_device(int fman_id,
602 struct mac_device *mac_dev,
603 struct device_node *node)
604{
605 struct platform_device *pdev;
606 struct dpaa_eth_data data;
607 struct mac_priv_s *priv;
608 static int dpaa_eth_dev_cnt;
609 int ret;
610
611 priv = mac_dev->priv;
612
613 data.mac_dev = mac_dev;
614 data.mac_hw_id = priv->cell_index;
615 data.fman_hw_id = fman_id;
616 data.mac_node = node;
617
618 mutex_lock(ð_lock);
619
620 pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
621 if (!pdev) {
622 ret = -ENOMEM;
623 goto no_mem;
624 }
625
626 ret = platform_device_add_data(pdev, &data, sizeof(data));
627 if (ret)
628 goto err;
629
630 ret = platform_device_add(pdev);
631 if (ret)
632 goto err;
633
634 dpaa_eth_dev_cnt++;
635 mutex_unlock(ð_lock);
636
637 return pdev;
638
639err:
640 platform_device_put(pdev);
641no_mem:
642 mutex_unlock(ð_lock);
643
644 return ERR_PTR(ret);
645}
646
647static const struct of_device_id mac_match[] = {
648 { .compatible = "fsl,fman-dtsec" },
649 { .compatible = "fsl,fman-xgec" },
650 { .compatible = "fsl,fman-memac" },
651 {}
652};
653MODULE_DEVICE_TABLE(of, mac_match);
654
655static int mac_probe(struct platform_device *_of_dev)
656{
657 int err, i, nph;
658 struct device *dev;
659 struct device_node *mac_node, *dev_node;
660 struct mac_device *mac_dev;
661 struct platform_device *of_dev;
662 struct resource res;
663 struct mac_priv_s *priv;
664 const u8 *mac_addr;
665 u32 val;
666 u8 fman_id;
667 int phy_if;
668
669 dev = &_of_dev->dev;
670 mac_node = dev->of_node;
671
672 mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
673 if (!mac_dev) {
674 err = -ENOMEM;
675 dev_err(dev, "devm_kzalloc() = %d\n", err);
676 goto _return;
677 }
678 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
679 if (!priv) {
680 err = -ENOMEM;
681 goto _return;
682 }
683
684 /* Save private information */
685 mac_dev->priv = priv;
686 priv->dev = dev;
687
688 if (of_device_is_compatible(mac_node, "fsl,fman-dtsec")) {
689 setup_dtsec(mac_dev);
690 priv->internal_phy_node = of_parse_phandle(mac_node,
691 "tbi-handle", 0);
692 } else if (of_device_is_compatible(mac_node, "fsl,fman-xgec")) {
693 setup_tgec(mac_dev);
694 } else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
695 setup_memac(mac_dev);
696 priv->internal_phy_node = of_parse_phandle(mac_node,
697 "pcsphy-handle", 0);
698 } else {
699 dev_err(dev, "MAC node (%s) contains unsupported MAC\n",
700 mac_node->full_name);
701 err = -EINVAL;
702 goto _return;
703 }
704
705 /* Register mac_dev */
706 dev_set_drvdata(dev, mac_dev);
707
708 INIT_LIST_HEAD(&priv->mc_addr_list);
709
710 /* Get the FM node */
711 dev_node = of_get_parent(mac_node);
712 if (!dev_node) {
713 dev_err(dev, "of_get_parent(%s) failed\n",
714 mac_node->full_name);
715 err = -EINVAL;
716 goto _return_dev_set_drvdata;
717 }
718
719 of_dev = of_find_device_by_node(dev_node);
720 if (!of_dev) {
721 dev_err(dev, "of_find_device_by_node(%s) failed\n",
722 dev_node->full_name);
723 err = -EINVAL;
724 goto _return_of_node_put;
725 }
726
727 /* Get the FMan cell-index */
728 err = of_property_read_u32(dev_node, "cell-index", &val);
729 if (err) {
730 dev_err(dev, "failed to read cell-index for %s\n",
731 dev_node->full_name);
732 err = -EINVAL;
733 goto _return_of_node_put;
734 }
735 /* cell-index 0 => FMan id 1 */
736 fman_id = (u8)(val + 1);
737
738 priv->fman = fman_bind(&of_dev->dev);
739 if (!priv->fman) {
740 dev_err(dev, "fman_bind(%s) failed\n", dev_node->full_name);
741 err = -ENODEV;
742 goto _return_of_node_put;
743 }
744
745 of_node_put(dev_node);
746
747 /* Get the address of the memory mapped registers */
748 err = of_address_to_resource(mac_node, 0, &res);
749 if (err < 0) {
750 dev_err(dev, "of_address_to_resource(%s) = %d\n",
751 mac_node->full_name, err);
752 goto _return_dev_set_drvdata;
753 }
754
755 mac_dev->res = __devm_request_region(dev,
756 fman_get_mem_region(priv->fman),
757 res.start, res.end + 1 - res.start,
758 "mac");
759 if (!mac_dev->res) {
760 dev_err(dev, "__devm_request_mem_region(mac) failed\n");
761 err = -EBUSY;
762 goto _return_dev_set_drvdata;
763 }
764
765 priv->vaddr = devm_ioremap(dev, mac_dev->res->start,
766 mac_dev->res->end + 1 - mac_dev->res->start);
767 if (!priv->vaddr) {
768 dev_err(dev, "devm_ioremap() failed\n");
769 err = -EIO;
770 goto _return_dev_set_drvdata;
771 }
772
773 if (!of_device_is_available(mac_node)) {
774 devm_iounmap(dev, priv->vaddr);
775 __devm_release_region(dev, fman_get_mem_region(priv->fman),
776 res.start, res.end + 1 - res.start);
777 devm_kfree(dev, mac_dev);
778 dev_set_drvdata(dev, NULL);
779 return -ENODEV;
780 }
781
782 /* Get the cell-index */
783 err = of_property_read_u32(mac_node, "cell-index", &val);
784 if (err) {
785 dev_err(dev, "failed to read cell-index for %s\n",
786 mac_node->full_name);
787 err = -EINVAL;
788 goto _return_dev_set_drvdata;
789 }
790 priv->cell_index = (u8)val;
791
792 /* Get the MAC address */
793 mac_addr = of_get_mac_address(mac_node);
794 if (!mac_addr) {
795 dev_err(dev, "of_get_mac_address(%s) failed\n",
796 mac_node->full_name);
797 err = -EINVAL;
798 goto _return_dev_set_drvdata;
799 }
800 memcpy(mac_dev->addr, mac_addr, sizeof(mac_dev->addr));
801
802 /* Get the port handles */
803 nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
804 if (unlikely(nph < 0)) {
805 dev_err(dev, "of_count_phandle_with_args(%s, fsl,fman-ports) failed\n",
806 mac_node->full_name);
807 err = nph;
808 goto _return_dev_set_drvdata;
809 }
810
811 if (nph != ARRAY_SIZE(mac_dev->port)) {
812 dev_err(dev, "Not supported number of fman-ports handles of mac node %s from device tree\n",
813 mac_node->full_name);
814 err = -EINVAL;
815 goto _return_dev_set_drvdata;
816 }
817
818 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
819 /* Find the port node */
820 dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
821 if (!dev_node) {
822 dev_err(dev, "of_parse_phandle(%s, fsl,fman-ports) failed\n",
823 mac_node->full_name);
824 err = -EINVAL;
825 goto _return_of_node_put;
826 }
827
828 of_dev = of_find_device_by_node(dev_node);
829 if (!of_dev) {
830 dev_err(dev, "of_find_device_by_node(%s) failed\n",
831 dev_node->full_name);
832 err = -EINVAL;
833 goto _return_of_node_put;
834 }
835
836 mac_dev->port[i] = fman_port_bind(&of_dev->dev);
837 if (!mac_dev->port[i]) {
838 dev_err(dev, "dev_get_drvdata(%s) failed\n",
839 dev_node->full_name);
840 err = -EINVAL;
841 goto _return_of_node_put;
842 }
843 of_node_put(dev_node);
844 }
845
846 /* Get the PHY connection type */
847 phy_if = of_get_phy_mode(mac_node);
848 if (phy_if < 0) {
849 dev_warn(dev,
850 "of_get_phy_mode() for %s failed. Defaulting to SGMII\n",
851 mac_node->full_name);
852 phy_if = PHY_INTERFACE_MODE_SGMII;
853 }
854 priv->phy_if = phy_if;
855
856 priv->speed = phy2speed[priv->phy_if];
857 priv->max_speed = priv->speed;
858 mac_dev->if_support = DTSEC_SUPPORTED;
859 /* We don't support half-duplex in SGMII mode */
860 if (priv->phy_if == PHY_INTERFACE_MODE_SGMII)
861 mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
862 SUPPORTED_100baseT_Half);
863
864 /* Gigabit support (no half-duplex) */
865 if (priv->max_speed == 1000)
866 mac_dev->if_support |= SUPPORTED_1000baseT_Full;
867
868 /* The 10G interface only supports one mode */
869 if (priv->phy_if == PHY_INTERFACE_MODE_XGMII)
870 mac_dev->if_support = SUPPORTED_10000baseT_Full;
871
872 /* Get the rest of the PHY information */
873 priv->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
874 if (!priv->phy_node && of_phy_is_fixed_link(mac_node)) {
875 struct phy_device *phy;
876
877 err = of_phy_register_fixed_link(mac_node);
878 if (err)
879 goto _return_dev_set_drvdata;
880
881 priv->fixed_link = kzalloc(sizeof(*priv->fixed_link),
882 GFP_KERNEL);
883 if (!priv->fixed_link) {
884 err = -ENOMEM;
885 goto _return_dev_set_drvdata;
886 }
887
888 priv->phy_node = of_node_get(mac_node);
889 phy = of_phy_find_device(priv->phy_node);
890 if (!phy) {
891 err = -EINVAL;
892 goto _return_dev_set_drvdata;
893 }
894
895 priv->fixed_link->link = phy->link;
896 priv->fixed_link->speed = phy->speed;
897 priv->fixed_link->duplex = phy->duplex;
898 priv->fixed_link->pause = phy->pause;
899 priv->fixed_link->asym_pause = phy->asym_pause;
900
901 put_device(&phy->mdio.dev);
902 }
903
904 err = mac_dev->init(mac_dev);
905 if (err < 0) {
906 dev_err(dev, "mac_dev->init() = %d\n", err);
907 of_node_put(priv->phy_node);
908 goto _return_dev_set_drvdata;
909 }
910
911 /* pause frame autonegotiation enabled */
912 mac_dev->autoneg_pause = true;
913
914 /* By intializing the values to false, force FMD to enable PAUSE frames
915 * on RX and TX
916 */
917 mac_dev->rx_pause_req = true;
918 mac_dev->tx_pause_req = true;
919 mac_dev->rx_pause_active = false;
920 mac_dev->tx_pause_active = false;
921 err = fman_set_mac_active_pause(mac_dev, true, true);
922 if (err < 0)
923 dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
924
925 dev_info(dev, "FMan MAC address: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
926 mac_dev->addr[0], mac_dev->addr[1], mac_dev->addr[2],
927 mac_dev->addr[3], mac_dev->addr[4], mac_dev->addr[5]);
928
929 priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev, mac_node);
930 if (IS_ERR(priv->eth_dev)) {
931 dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
932 priv->cell_index);
933 priv->eth_dev = NULL;
934 }
935
936 goto _return;
937
938_return_of_node_put:
939 of_node_put(dev_node);
940_return_dev_set_drvdata:
941 kfree(priv->fixed_link);
942 dev_set_drvdata(dev, NULL);
943_return:
944 return err;
945}
946
947static struct platform_driver mac_driver = {
948 .driver = {
949 .name = KBUILD_MODNAME,
950 .of_match_table = mac_match,
951 },
952 .probe = mac_probe,
953};
954
955builtin_platform_driver(mac_driver);