mac.c 23 KB

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