mac.c 25 KB

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