Linux Audio

Check our new training course

Loading...
v6.8
  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(&eth_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(&eth_lock);
134
135	return pdev;
136
137err:
138	platform_device_put(pdev);
139no_mem:
140	mutex_unlock(&eth_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, &params);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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);
v5.4
  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	struct fman			*fman;
 61	struct device_node		*internal_phy_node;
 62	/* List of multicast addresses */
 63	struct list_head		mc_addr_list;
 64	struct platform_device		*eth_dev;
 65	struct fixed_phy_status		*fixed_link;
 66	u16				speed;
 67	u16				max_speed;
 68
 69	int (*enable)(struct fman_mac *mac_dev, enum comm_mode mode);
 70	int (*disable)(struct fman_mac *mac_dev, enum comm_mode mode);
 71};
 72
 73struct mac_address {
 74	u8 addr[ETH_ALEN];
 75	struct list_head list;
 76};
 77
 78static void mac_exception(void *handle, enum fman_mac_exceptions ex)
 
 79{
 80	struct mac_device	*mac_dev;
 81	struct mac_priv_s	*priv;
 82
 83	mac_dev = handle;
 84	priv = mac_dev->priv;
 85
 86	if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
 87		/* don't flag RX FIFO after the first */
 88		mac_dev->set_exception(mac_dev->fman_mac,
 89				       FM_MAC_EX_10G_RX_FIFO_OVFL, false);
 90		dev_err(priv->dev, "10G MAC got RX FIFO Error = %x\n", ex);
 91	}
 92
 93	dev_dbg(priv->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
 94		__func__, ex);
 95}
 96
 97static void set_fman_mac_params(struct mac_device *mac_dev,
 98				struct fman_mac_params *params)
 99{
100	struct mac_priv_s *priv = mac_dev->priv;
101
102	params->base_addr = (typeof(params->base_addr))
103		devm_ioremap(priv->dev, mac_dev->res->start,
104			     resource_size(mac_dev->res));
105	memcpy(&params->addr, mac_dev->addr, sizeof(mac_dev->addr));
106	params->max_speed	= priv->max_speed;
107	params->phy_if		= mac_dev->phy_if;
108	params->basex_if	= false;
109	params->mac_id		= priv->cell_index;
110	params->fm		= (void *)priv->fman;
111	params->exception_cb	= mac_exception;
112	params->event_cb	= mac_exception;
113	params->dev_id		= mac_dev;
114	params->internal_phy_node = priv->internal_phy_node;
115}
116
117static int tgec_initialization(struct mac_device *mac_dev)
118{
119	int err;
120	struct mac_priv_s	*priv;
121	struct fman_mac_params	params;
122	u32			version;
123
124	priv = mac_dev->priv;
125
126	set_fman_mac_params(mac_dev, &params);
127
128	mac_dev->fman_mac = tgec_config(&params);
129	if (!mac_dev->fman_mac) {
130		err = -EINVAL;
131		goto _return;
132	}
133
134	err = tgec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
135	if (err < 0)
136		goto _return_fm_mac_free;
137
138	err = tgec_init(mac_dev->fman_mac);
139	if (err < 0)
140		goto _return_fm_mac_free;
141
142	/* For 10G MAC, disable Tx ECC exception */
143	err = mac_dev->set_exception(mac_dev->fman_mac,
144				     FM_MAC_EX_10G_TX_ECC_ER, false);
145	if (err < 0)
146		goto _return_fm_mac_free;
147
148	err = tgec_get_version(mac_dev->fman_mac, &version);
149	if (err < 0)
150		goto _return_fm_mac_free;
151
152	dev_info(priv->dev, "FMan XGEC version: 0x%08x\n", version);
153
154	goto _return;
155
156_return_fm_mac_free:
157	tgec_free(mac_dev->fman_mac);
158
159_return:
160	return err;
161}
162
163static int dtsec_initialization(struct mac_device *mac_dev)
164{
165	int			err;
166	struct mac_priv_s	*priv;
167	struct fman_mac_params	params;
168	u32			version;
169
170	priv = mac_dev->priv;
171
172	set_fman_mac_params(mac_dev, &params);
173
174	mac_dev->fman_mac = dtsec_config(&params);
175	if (!mac_dev->fman_mac) {
176		err = -EINVAL;
177		goto _return;
178	}
179
180	err = dtsec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
181	if (err < 0)
182		goto _return_fm_mac_free;
183
184	err = dtsec_cfg_pad_and_crc(mac_dev->fman_mac, true);
185	if (err < 0)
186		goto _return_fm_mac_free;
187
188	err = dtsec_init(mac_dev->fman_mac);
189	if (err < 0)
190		goto _return_fm_mac_free;
191
192	/* For 1G MAC, disable by default the MIB counters overflow interrupt */
193	err = mac_dev->set_exception(mac_dev->fman_mac,
194				     FM_MAC_EX_1G_RX_MIB_CNT_OVFL, false);
195	if (err < 0)
196		goto _return_fm_mac_free;
197
198	err = dtsec_get_version(mac_dev->fman_mac, &version);
199	if (err < 0)
200		goto _return_fm_mac_free;
201
202	dev_info(priv->dev, "FMan dTSEC version: 0x%08x\n", version);
203
204	goto _return;
205
206_return_fm_mac_free:
207	dtsec_free(mac_dev->fman_mac);
208
209_return:
210	return err;
211}
212
213static int memac_initialization(struct mac_device *mac_dev)
214{
215	int			 err;
216	struct mac_priv_s	*priv;
217	struct fman_mac_params	 params;
218
219	priv = mac_dev->priv;
220
221	set_fman_mac_params(mac_dev, &params);
222
223	if (priv->max_speed == SPEED_10000)
224		params.phy_if = PHY_INTERFACE_MODE_XGMII;
225
226	mac_dev->fman_mac = memac_config(&params);
227	if (!mac_dev->fman_mac) {
228		err = -EINVAL;
229		goto _return;
230	}
231
232	err = memac_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
233	if (err < 0)
234		goto _return_fm_mac_free;
235
236	err = memac_cfg_reset_on_init(mac_dev->fman_mac, true);
237	if (err < 0)
238		goto _return_fm_mac_free;
239
240	err = memac_cfg_fixed_link(mac_dev->fman_mac, priv->fixed_link);
241	if (err < 0)
242		goto _return_fm_mac_free;
243
244	err = memac_init(mac_dev->fman_mac);
245	if (err < 0)
246		goto _return_fm_mac_free;
247
248	dev_info(priv->dev, "FMan MEMAC\n");
249
250	goto _return;
251
252_return_fm_mac_free:
253	memac_free(mac_dev->fman_mac);
254
255_return:
256	return err;
257}
258
259static int start(struct mac_device *mac_dev)
260{
261	int	 err;
262	struct phy_device *phy_dev = mac_dev->phy_dev;
263	struct mac_priv_s *priv = mac_dev->priv;
264
265	err = priv->enable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
266	if (!err && phy_dev)
267		phy_start(phy_dev);
268
269	return err;
270}
271
272static int stop(struct mac_device *mac_dev)
273{
274	struct mac_priv_s *priv = mac_dev->priv;
275
276	if (mac_dev->phy_dev)
277		phy_stop(mac_dev->phy_dev);
278
279	return priv->disable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
280}
281
282static int set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
283{
284	struct mac_priv_s	*priv;
285	struct mac_address	*old_addr, *tmp;
286	struct netdev_hw_addr	*ha;
287	int			err;
288	enet_addr_t		*addr;
289
290	priv = mac_dev->priv;
291
292	/* Clear previous address list */
293	list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
294		addr = (enet_addr_t *)old_addr->addr;
295		err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
296		if (err < 0)
297			return err;
298
299		list_del(&old_addr->list);
300		kfree(old_addr);
301	}
302
303	/* Add all the addresses from the new list */
304	netdev_for_each_mc_addr(ha, net_dev) {
305		addr = (enet_addr_t *)ha->addr;
306		err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
307		if (err < 0)
308			return err;
309
310		tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
311		if (!tmp)
312			return -ENOMEM;
313
314		ether_addr_copy(tmp->addr, ha->addr);
315		list_add(&tmp->list, &priv->mc_addr_list);
316	}
317	return 0;
318}
319
320/**
321 * fman_set_mac_active_pause
322 * @mac_dev:	A pointer to the MAC device
323 * @rx:		Pause frame setting for RX
324 * @tx:		Pause frame setting for TX
325 *
326 * Set the MAC RX/TX PAUSE frames settings
327 *
328 * Avoid redundant calls to FMD, if the MAC driver already contains the desired
329 * active PAUSE settings. Otherwise, the new active settings should be reflected
330 * in FMan.
331 *
332 * Return: 0 on success; Error code otherwise.
333 */
334int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
335{
336	struct fman_mac *fman_mac = mac_dev->fman_mac;
337	int err = 0;
338
339	if (rx != mac_dev->rx_pause_active) {
340		err = mac_dev->set_rx_pause(fman_mac, rx);
341		if (likely(err == 0))
342			mac_dev->rx_pause_active = rx;
343	}
344
345	if (tx != mac_dev->tx_pause_active) {
346		u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
347					 FSL_FM_PAUSE_TIME_DISABLE);
348
349		err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
350
351		if (likely(err == 0))
352			mac_dev->tx_pause_active = tx;
353	}
354
355	return err;
356}
357EXPORT_SYMBOL(fman_set_mac_active_pause);
358
359/**
360 * fman_get_pause_cfg
361 * @mac_dev:	A pointer to the MAC device
362 * @rx:		Return value for RX setting
363 * @tx:		Return value for TX setting
364 *
365 * Determine the MAC RX/TX PAUSE frames settings based on PHY
366 * autonegotiation or values set by eththool.
367 *
368 * Return: Pointer to FMan device.
369 */
370void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
371			bool *tx_pause)
372{
373	struct phy_device *phy_dev = mac_dev->phy_dev;
374	u16 lcl_adv, rmt_adv;
375	u8 flowctrl;
376
377	*rx_pause = *tx_pause = false;
378
379	if (!phy_dev->duplex)
380		return;
381
382	/* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
383	 * are those set by ethtool.
384	 */
385	if (!mac_dev->autoneg_pause) {
386		*rx_pause = mac_dev->rx_pause_req;
387		*tx_pause = mac_dev->tx_pause_req;
388		return;
389	}
390
391	/* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
392	 * settings depend on the result of the link negotiation.
393	 */
394
395	/* get local capabilities */
396	lcl_adv = linkmode_adv_to_lcl_adv_t(phy_dev->advertising);
397
398	/* get link partner capabilities */
399	rmt_adv = 0;
400	if (phy_dev->pause)
401		rmt_adv |= LPA_PAUSE_CAP;
402	if (phy_dev->asym_pause)
403		rmt_adv |= LPA_PAUSE_ASYM;
404
405	/* Calculate TX/RX settings based on local and peer advertised
406	 * symmetric/asymmetric PAUSE capabilities.
407	 */
408	flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
409	if (flowctrl & FLOW_CTRL_RX)
410		*rx_pause = true;
411	if (flowctrl & FLOW_CTRL_TX)
412		*tx_pause = true;
413}
414EXPORT_SYMBOL(fman_get_pause_cfg);
415
416static void adjust_link_void(struct mac_device *mac_dev)
417{
418}
419
420static void adjust_link_dtsec(struct mac_device *mac_dev)
421{
422	struct phy_device *phy_dev = mac_dev->phy_dev;
423	struct fman_mac *fman_mac;
424	bool rx_pause, tx_pause;
425	int err;
426
427	fman_mac = mac_dev->fman_mac;
428	if (!phy_dev->link) {
429		dtsec_restart_autoneg(fman_mac);
430
431		return;
432	}
433
434	dtsec_adjust_link(fman_mac, phy_dev->speed);
435	fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
436	err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
437	if (err < 0)
438		dev_err(mac_dev->priv->dev, "fman_set_mac_active_pause() = %d\n",
439			err);
440}
441
442static void adjust_link_memac(struct mac_device *mac_dev)
443{
444	struct phy_device *phy_dev = mac_dev->phy_dev;
445	struct fman_mac *fman_mac;
446	bool rx_pause, tx_pause;
447	int err;
448
449	fman_mac = mac_dev->fman_mac;
450	memac_adjust_link(fman_mac, phy_dev->speed);
451
452	fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
453	err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
454	if (err < 0)
455		dev_err(mac_dev->priv->dev, "fman_set_mac_active_pause() = %d\n",
456			err);
457}
458
459static void setup_dtsec(struct mac_device *mac_dev)
460{
461	mac_dev->init			= dtsec_initialization;
462	mac_dev->set_promisc		= dtsec_set_promiscuous;
463	mac_dev->change_addr		= dtsec_modify_mac_address;
464	mac_dev->add_hash_mac_addr	= dtsec_add_hash_mac_address;
465	mac_dev->remove_hash_mac_addr	= dtsec_del_hash_mac_address;
466	mac_dev->set_tx_pause		= dtsec_set_tx_pause_frames;
467	mac_dev->set_rx_pause		= dtsec_accept_rx_pause_frames;
468	mac_dev->set_exception		= dtsec_set_exception;
469	mac_dev->set_allmulti		= dtsec_set_allmulti;
470	mac_dev->set_tstamp		= dtsec_set_tstamp;
471	mac_dev->set_multi		= set_multi;
472	mac_dev->start			= start;
473	mac_dev->stop			= stop;
474	mac_dev->adjust_link            = adjust_link_dtsec;
475	mac_dev->priv->enable		= dtsec_enable;
476	mac_dev->priv->disable		= dtsec_disable;
477}
478
479static void setup_tgec(struct mac_device *mac_dev)
480{
481	mac_dev->init			= tgec_initialization;
482	mac_dev->set_promisc		= tgec_set_promiscuous;
483	mac_dev->change_addr		= tgec_modify_mac_address;
484	mac_dev->add_hash_mac_addr	= tgec_add_hash_mac_address;
485	mac_dev->remove_hash_mac_addr	= tgec_del_hash_mac_address;
486	mac_dev->set_tx_pause		= tgec_set_tx_pause_frames;
487	mac_dev->set_rx_pause		= tgec_accept_rx_pause_frames;
488	mac_dev->set_exception		= tgec_set_exception;
489	mac_dev->set_allmulti		= tgec_set_allmulti;
490	mac_dev->set_tstamp		= tgec_set_tstamp;
491	mac_dev->set_multi		= set_multi;
492	mac_dev->start			= start;
493	mac_dev->stop			= stop;
494	mac_dev->adjust_link            = adjust_link_void;
495	mac_dev->priv->enable		= tgec_enable;
496	mac_dev->priv->disable		= tgec_disable;
497}
498
499static void setup_memac(struct mac_device *mac_dev)
500{
501	mac_dev->init			= memac_initialization;
502	mac_dev->set_promisc		= memac_set_promiscuous;
503	mac_dev->change_addr		= memac_modify_mac_address;
504	mac_dev->add_hash_mac_addr	= memac_add_hash_mac_address;
505	mac_dev->remove_hash_mac_addr	= memac_del_hash_mac_address;
506	mac_dev->set_tx_pause		= memac_set_tx_pause_frames;
507	mac_dev->set_rx_pause		= memac_accept_rx_pause_frames;
508	mac_dev->set_exception		= memac_set_exception;
509	mac_dev->set_allmulti		= memac_set_allmulti;
510	mac_dev->set_tstamp		= memac_set_tstamp;
511	mac_dev->set_multi		= set_multi;
512	mac_dev->start			= start;
513	mac_dev->stop			= stop;
514	mac_dev->adjust_link            = adjust_link_memac;
515	mac_dev->priv->enable		= memac_enable;
516	mac_dev->priv->disable		= memac_disable;
517}
518
519#define DTSEC_SUPPORTED \
520	(SUPPORTED_10baseT_Half \
521	| SUPPORTED_10baseT_Full \
522	| SUPPORTED_100baseT_Half \
523	| SUPPORTED_100baseT_Full \
524	| SUPPORTED_Autoneg \
525	| SUPPORTED_Pause \
526	| SUPPORTED_Asym_Pause \
527	| SUPPORTED_MII)
528
529static DEFINE_MUTEX(eth_lock);
530
531static const u16 phy2speed[] = {
532	[PHY_INTERFACE_MODE_MII]		= SPEED_100,
533	[PHY_INTERFACE_MODE_GMII]		= SPEED_1000,
534	[PHY_INTERFACE_MODE_SGMII]		= SPEED_1000,
535	[PHY_INTERFACE_MODE_TBI]		= SPEED_1000,
536	[PHY_INTERFACE_MODE_RMII]		= SPEED_100,
537	[PHY_INTERFACE_MODE_RGMII]		= SPEED_1000,
538	[PHY_INTERFACE_MODE_RGMII_ID]		= SPEED_1000,
539	[PHY_INTERFACE_MODE_RGMII_RXID]	= SPEED_1000,
540	[PHY_INTERFACE_MODE_RGMII_TXID]	= SPEED_1000,
541	[PHY_INTERFACE_MODE_RTBI]		= SPEED_1000,
542	[PHY_INTERFACE_MODE_QSGMII]		= SPEED_1000,
543	[PHY_INTERFACE_MODE_XGMII]		= SPEED_10000
544};
545
546static struct platform_device *dpaa_eth_add_device(int fman_id,
547						   struct mac_device *mac_dev)
548{
549	struct platform_device *pdev;
550	struct dpaa_eth_data data;
551	struct mac_priv_s	*priv;
552	static int dpaa_eth_dev_cnt;
553	int ret;
554
555	priv = mac_dev->priv;
556
557	data.mac_dev = mac_dev;
558	data.mac_hw_id = priv->cell_index;
559	data.fman_hw_id = fman_id;
560
561	mutex_lock(&eth_lock);
562	pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
563	if (!pdev) {
564		ret = -ENOMEM;
565		goto no_mem;
566	}
567
568	pdev->dev.parent = priv->dev;
569
570	ret = platform_device_add_data(pdev, &data, sizeof(data));
571	if (ret)
572		goto err;
573
574	ret = platform_device_add(pdev);
575	if (ret)
576		goto err;
577
578	dpaa_eth_dev_cnt++;
579	mutex_unlock(&eth_lock);
580
581	return pdev;
582
583err:
584	platform_device_put(pdev);
585no_mem:
586	mutex_unlock(&eth_lock);
587
588	return ERR_PTR(ret);
589}
590
591static const struct of_device_id mac_match[] = {
592	{ .compatible	= "fsl,fman-dtsec" },
593	{ .compatible	= "fsl,fman-xgec" },
594	{ .compatible	= "fsl,fman-memac" },
595	{}
596};
597MODULE_DEVICE_TABLE(of, mac_match);
598
599static int mac_probe(struct platform_device *_of_dev)
600{
601	int			 err, i, nph;
 
 
602	struct device		*dev;
603	struct device_node	*mac_node, *dev_node;
604	struct mac_device	*mac_dev;
605	struct platform_device	*of_dev;
606	struct resource		 res;
607	struct mac_priv_s	*priv;
608	const u8		*mac_addr;
609	u32			 val;
610	u8			fman_id;
611	int			phy_if;
612
613	dev = &_of_dev->dev;
614	mac_node = dev->of_node;
 
615
616	mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
617	if (!mac_dev) {
618		err = -ENOMEM;
619		goto _return;
620	}
621	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
622	if (!priv) {
623		err = -ENOMEM;
624		goto _return;
625	}
626
627	/* Save private information */
628	mac_dev->priv = priv;
629	priv->dev = dev;
630
631	if (of_device_is_compatible(mac_node, "fsl,fman-dtsec")) {
632		setup_dtsec(mac_dev);
633		priv->internal_phy_node = of_parse_phandle(mac_node,
634							  "tbi-handle", 0);
635	} else if (of_device_is_compatible(mac_node, "fsl,fman-xgec")) {
636		setup_tgec(mac_dev);
637	} else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
638		setup_memac(mac_dev);
639		priv->internal_phy_node = of_parse_phandle(mac_node,
640							  "pcsphy-handle", 0);
641	} else {
642		dev_err(dev, "MAC node (%pOF) contains unsupported MAC\n",
643			mac_node);
644		err = -EINVAL;
645		goto _return;
646	}
647
648	INIT_LIST_HEAD(&priv->mc_addr_list);
649
650	/* Get the FM node */
651	dev_node = of_get_parent(mac_node);
652	if (!dev_node) {
653		dev_err(dev, "of_get_parent(%pOF) failed\n",
654			mac_node);
655		err = -EINVAL;
656		goto _return_of_get_parent;
657	}
658
659	of_dev = of_find_device_by_node(dev_node);
660	if (!of_dev) {
661		dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
662		err = -EINVAL;
663		goto _return_of_node_put;
664	}
665
666	/* Get the FMan cell-index */
667	err = of_property_read_u32(dev_node, "cell-index", &val);
668	if (err) {
669		dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
670		err = -EINVAL;
671		goto _return_of_node_put;
672	}
673	/* cell-index 0 => FMan id 1 */
674	fman_id = (u8)(val + 1);
675
676	priv->fman = fman_bind(&of_dev->dev);
677	if (!priv->fman) {
678		dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
679		err = -ENODEV;
680		goto _return_of_node_put;
681	}
682
683	of_node_put(dev_node);
684
685	/* Get the address of the memory mapped registers */
686	err = of_address_to_resource(mac_node, 0, &res);
687	if (err < 0) {
688		dev_err(dev, "of_address_to_resource(%pOF) = %d\n",
689			mac_node, err);
690		goto _return_of_get_parent;
691	}
692
693	mac_dev->res = __devm_request_region(dev,
694					     fman_get_mem_region(priv->fman),
695					     res.start, res.end + 1 - res.start,
696					     "mac");
697	if (!mac_dev->res) {
698		dev_err(dev, "__devm_request_mem_region(mac) failed\n");
699		err = -EBUSY;
700		goto _return_of_get_parent;
701	}
702
703	priv->vaddr = devm_ioremap(dev, mac_dev->res->start,
704				   mac_dev->res->end + 1 - mac_dev->res->start);
705	if (!priv->vaddr) {
706		dev_err(dev, "devm_ioremap() failed\n");
707		err = -EIO;
708		goto _return_of_get_parent;
709	}
710
711	if (!of_device_is_available(mac_node)) {
712		err = -ENODEV;
713		goto _return_of_get_parent;
714	}
715
716	/* Get the cell-index */
717	err = of_property_read_u32(mac_node, "cell-index", &val);
718	if (err) {
719		dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
720		err = -EINVAL;
721		goto _return_of_get_parent;
722	}
723	priv->cell_index = (u8)val;
724
725	/* Get the MAC address */
726	mac_addr = of_get_mac_address(mac_node);
727	if (IS_ERR(mac_addr)) {
728		dev_err(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
729		err = -EINVAL;
730		goto _return_of_get_parent;
731	}
732	ether_addr_copy(mac_dev->addr, mac_addr);
733
734	/* Get the port handles */
735	nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
736	if (unlikely(nph < 0)) {
737		dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
738			mac_node);
739		err = nph;
740		goto _return_of_get_parent;
741	}
742
743	if (nph != ARRAY_SIZE(mac_dev->port)) {
744		dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
745			mac_node);
746		err = -EINVAL;
747		goto _return_of_get_parent;
748	}
749
750	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
751		/* Find the port node */
752		dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
753		if (!dev_node) {
754			dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
755				mac_node);
756			err = -EINVAL;
757			goto _return_of_node_put;
758		}
759
760		of_dev = of_find_device_by_node(dev_node);
761		if (!of_dev) {
762			dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
763				dev_node);
764			err = -EINVAL;
765			goto _return_of_node_put;
766		}
767
768		mac_dev->port[i] = fman_port_bind(&of_dev->dev);
769		if (!mac_dev->port[i]) {
770			dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
771				dev_node);
772			err = -EINVAL;
773			goto _return_of_node_put;
774		}
775		of_node_put(dev_node);
776	}
777
778	/* Get the PHY connection type */
779	phy_if = of_get_phy_mode(mac_node);
780	if (phy_if < 0) {
781		dev_warn(dev,
782			 "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
783			 mac_node);
784		phy_if = PHY_INTERFACE_MODE_SGMII;
785	}
786	mac_dev->phy_if = phy_if;
787
788	priv->speed		= phy2speed[mac_dev->phy_if];
789	priv->max_speed		= priv->speed;
790	mac_dev->if_support	= DTSEC_SUPPORTED;
791	/* We don't support half-duplex in SGMII mode */
792	if (mac_dev->phy_if == PHY_INTERFACE_MODE_SGMII)
793		mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
794					SUPPORTED_100baseT_Half);
795
796	/* Gigabit support (no half-duplex) */
797	if (priv->max_speed == 1000)
798		mac_dev->if_support |= SUPPORTED_1000baseT_Full;
799
800	/* The 10G interface only supports one mode */
801	if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
802		mac_dev->if_support = SUPPORTED_10000baseT_Full;
803
804	/* Get the rest of the PHY information */
805	mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
806	if (!mac_dev->phy_node && of_phy_is_fixed_link(mac_node)) {
807		struct phy_device *phy;
808
809		err = of_phy_register_fixed_link(mac_node);
810		if (err)
811			goto _return_of_get_parent;
812
813		priv->fixed_link = kzalloc(sizeof(*priv->fixed_link),
814					   GFP_KERNEL);
815		if (!priv->fixed_link) {
816			err = -ENOMEM;
817			goto _return_of_get_parent;
818		}
819
820		mac_dev->phy_node = of_node_get(mac_node);
821		phy = of_phy_find_device(mac_dev->phy_node);
822		if (!phy) {
823			err = -EINVAL;
824			of_node_put(mac_dev->phy_node);
825			goto _return_of_get_parent;
826		}
827
828		priv->fixed_link->link = phy->link;
829		priv->fixed_link->speed = phy->speed;
830		priv->fixed_link->duplex = phy->duplex;
831		priv->fixed_link->pause = phy->pause;
832		priv->fixed_link->asym_pause = phy->asym_pause;
833
834		put_device(&phy->mdio.dev);
835	}
836
837	err = mac_dev->init(mac_dev);
838	if (err < 0) {
839		dev_err(dev, "mac_dev->init() = %d\n", err);
840		of_node_put(mac_dev->phy_node);
841		goto _return_of_get_parent;
842	}
843
844	/* pause frame autonegotiation enabled */
845	mac_dev->autoneg_pause = true;
846
847	/* By intializing the values to false, force FMD to enable PAUSE frames
848	 * on RX and TX
849	 */
850	mac_dev->rx_pause_req = true;
851	mac_dev->tx_pause_req = true;
852	mac_dev->rx_pause_active = false;
853	mac_dev->tx_pause_active = false;
854	err = fman_set_mac_active_pause(mac_dev, true, true);
855	if (err < 0)
856		dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
857
858	dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr);
 
859
860	priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
861	if (IS_ERR(priv->eth_dev)) {
 
862		dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
863			priv->cell_index);
864		priv->eth_dev = NULL;
865	}
866
867	goto _return;
868
869_return_of_node_put:
870	of_node_put(dev_node);
871_return_of_get_parent:
872	kfree(priv->fixed_link);
873_return:
874	return err;
875}
876
 
 
 
 
 
 
 
877static struct platform_driver mac_driver = {
878	.driver = {
879		.name		= KBUILD_MODNAME,
880		.of_match_table	= mac_match,
881	},
882	.probe		= mac_probe,
 
883};
884
885builtin_platform_driver(mac_driver);