mac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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_multi = set_multi;
  387. mac_dev->start = start;
  388. mac_dev->stop = stop;
  389. mac_dev->adjust_link = adjust_link_dtsec;
  390. mac_dev->priv->enable = dtsec_enable;
  391. mac_dev->priv->disable = dtsec_disable;
  392. }
  393. static void setup_tgec(struct mac_device *mac_dev)
  394. {
  395. mac_dev->init = tgec_initialization;
  396. mac_dev->set_promisc = tgec_set_promiscuous;
  397. mac_dev->change_addr = tgec_modify_mac_address;
  398. mac_dev->add_hash_mac_addr = tgec_add_hash_mac_address;
  399. mac_dev->remove_hash_mac_addr = tgec_del_hash_mac_address;
  400. mac_dev->set_tx_pause = tgec_set_tx_pause_frames;
  401. mac_dev->set_rx_pause = tgec_accept_rx_pause_frames;
  402. mac_dev->set_exception = tgec_set_exception;
  403. mac_dev->set_multi = set_multi;
  404. mac_dev->start = start;
  405. mac_dev->stop = stop;
  406. mac_dev->adjust_link = adjust_link_void;
  407. mac_dev->priv->enable = tgec_enable;
  408. mac_dev->priv->disable = tgec_disable;
  409. }
  410. static void setup_memac(struct mac_device *mac_dev)
  411. {
  412. mac_dev->init = memac_initialization;
  413. mac_dev->set_promisc = memac_set_promiscuous;
  414. mac_dev->change_addr = memac_modify_mac_address;
  415. mac_dev->add_hash_mac_addr = memac_add_hash_mac_address;
  416. mac_dev->remove_hash_mac_addr = memac_del_hash_mac_address;
  417. mac_dev->set_tx_pause = memac_set_tx_pause_frames;
  418. mac_dev->set_rx_pause = memac_accept_rx_pause_frames;
  419. mac_dev->set_exception = memac_set_exception;
  420. mac_dev->set_multi = set_multi;
  421. mac_dev->start = start;
  422. mac_dev->stop = stop;
  423. mac_dev->adjust_link = adjust_link_memac;
  424. mac_dev->priv->enable = memac_enable;
  425. mac_dev->priv->disable = memac_disable;
  426. }
  427. #define DTSEC_SUPPORTED \
  428. (SUPPORTED_10baseT_Half \
  429. | SUPPORTED_10baseT_Full \
  430. | SUPPORTED_100baseT_Half \
  431. | SUPPORTED_100baseT_Full \
  432. | SUPPORTED_Autoneg \
  433. | SUPPORTED_Pause \
  434. | SUPPORTED_Asym_Pause \
  435. | SUPPORTED_MII)
  436. static DEFINE_MUTEX(eth_lock);
  437. static const u16 phy2speed[] = {
  438. [PHY_INTERFACE_MODE_MII] = SPEED_100,
  439. [PHY_INTERFACE_MODE_GMII] = SPEED_1000,
  440. [PHY_INTERFACE_MODE_SGMII] = SPEED_1000,
  441. [PHY_INTERFACE_MODE_TBI] = SPEED_1000,
  442. [PHY_INTERFACE_MODE_RMII] = SPEED_100,
  443. [PHY_INTERFACE_MODE_RGMII] = SPEED_1000,
  444. [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000,
  445. [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000,
  446. [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000,
  447. [PHY_INTERFACE_MODE_RTBI] = SPEED_1000,
  448. [PHY_INTERFACE_MODE_QSGMII] = SPEED_1000,
  449. [PHY_INTERFACE_MODE_XGMII] = SPEED_10000
  450. };
  451. static struct platform_device *dpaa_eth_add_device(int fman_id,
  452. struct mac_device *mac_dev)
  453. {
  454. struct platform_device *pdev;
  455. struct dpaa_eth_data data;
  456. struct mac_priv_s *priv;
  457. static int dpaa_eth_dev_cnt;
  458. int ret;
  459. priv = mac_dev->priv;
  460. data.mac_dev = mac_dev;
  461. data.mac_hw_id = priv->cell_index;
  462. data.fman_hw_id = fman_id;
  463. mutex_lock(&eth_lock);
  464. pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
  465. if (!pdev) {
  466. ret = -ENOMEM;
  467. goto no_mem;
  468. }
  469. pdev->dev.parent = priv->dev;
  470. set_dma_ops(&pdev->dev, get_dma_ops(priv->dev));
  471. ret = platform_device_add_data(pdev, &data, sizeof(data));
  472. if (ret)
  473. goto err;
  474. ret = platform_device_add(pdev);
  475. if (ret)
  476. goto err;
  477. dpaa_eth_dev_cnt++;
  478. mutex_unlock(&eth_lock);
  479. return pdev;
  480. err:
  481. platform_device_put(pdev);
  482. no_mem:
  483. mutex_unlock(&eth_lock);
  484. return ERR_PTR(ret);
  485. }
  486. static const struct of_device_id mac_match[] = {
  487. { .compatible = "fsl,fman-dtsec" },
  488. { .compatible = "fsl,fman-xgec" },
  489. { .compatible = "fsl,fman-memac" },
  490. {}
  491. };
  492. MODULE_DEVICE_TABLE(of, mac_match);
  493. static int mac_probe(struct platform_device *_of_dev)
  494. {
  495. int err, i, nph;
  496. struct device *dev;
  497. struct device_node *mac_node, *dev_node;
  498. struct mac_device *mac_dev;
  499. struct platform_device *of_dev;
  500. struct resource res;
  501. struct mac_priv_s *priv;
  502. const u8 *mac_addr;
  503. u32 val;
  504. u8 fman_id;
  505. int phy_if;
  506. dev = &_of_dev->dev;
  507. mac_node = dev->of_node;
  508. mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
  509. if (!mac_dev) {
  510. err = -ENOMEM;
  511. dev_err(dev, "devm_kzalloc() = %d\n", err);
  512. goto _return;
  513. }
  514. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  515. if (!priv) {
  516. err = -ENOMEM;
  517. goto _return;
  518. }
  519. /* Save private information */
  520. mac_dev->priv = priv;
  521. priv->dev = dev;
  522. if (of_device_is_compatible(mac_node, "fsl,fman-dtsec")) {
  523. setup_dtsec(mac_dev);
  524. priv->internal_phy_node = of_parse_phandle(mac_node,
  525. "tbi-handle", 0);
  526. } else if (of_device_is_compatible(mac_node, "fsl,fman-xgec")) {
  527. setup_tgec(mac_dev);
  528. } else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
  529. setup_memac(mac_dev);
  530. priv->internal_phy_node = of_parse_phandle(mac_node,
  531. "pcsphy-handle", 0);
  532. } else {
  533. dev_err(dev, "MAC node (%pOF) contains unsupported MAC\n",
  534. mac_node);
  535. err = -EINVAL;
  536. goto _return;
  537. }
  538. INIT_LIST_HEAD(&priv->mc_addr_list);
  539. /* Get the FM node */
  540. dev_node = of_get_parent(mac_node);
  541. if (!dev_node) {
  542. dev_err(dev, "of_get_parent(%pOF) failed\n",
  543. mac_node);
  544. err = -EINVAL;
  545. goto _return_of_get_parent;
  546. }
  547. of_dev = of_find_device_by_node(dev_node);
  548. if (!of_dev) {
  549. dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
  550. err = -EINVAL;
  551. goto _return_of_node_put;
  552. }
  553. /* Get the FMan cell-index */
  554. err = of_property_read_u32(dev_node, "cell-index", &val);
  555. if (err) {
  556. dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
  557. err = -EINVAL;
  558. goto _return_of_node_put;
  559. }
  560. /* cell-index 0 => FMan id 1 */
  561. fman_id = (u8)(val + 1);
  562. priv->fman = fman_bind(&of_dev->dev);
  563. if (!priv->fman) {
  564. dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
  565. err = -ENODEV;
  566. goto _return_of_node_put;
  567. }
  568. of_node_put(dev_node);
  569. /* Get the address of the memory mapped registers */
  570. err = of_address_to_resource(mac_node, 0, &res);
  571. if (err < 0) {
  572. dev_err(dev, "of_address_to_resource(%pOF) = %d\n",
  573. mac_node, err);
  574. goto _return_of_get_parent;
  575. }
  576. mac_dev->res = __devm_request_region(dev,
  577. fman_get_mem_region(priv->fman),
  578. res.start, res.end + 1 - res.start,
  579. "mac");
  580. if (!mac_dev->res) {
  581. dev_err(dev, "__devm_request_mem_region(mac) failed\n");
  582. err = -EBUSY;
  583. goto _return_of_get_parent;
  584. }
  585. priv->vaddr = devm_ioremap(dev, mac_dev->res->start,
  586. mac_dev->res->end + 1 - mac_dev->res->start);
  587. if (!priv->vaddr) {
  588. dev_err(dev, "devm_ioremap() failed\n");
  589. err = -EIO;
  590. goto _return_of_get_parent;
  591. }
  592. if (!of_device_is_available(mac_node)) {
  593. devm_iounmap(dev, priv->vaddr);
  594. __devm_release_region(dev, fman_get_mem_region(priv->fman),
  595. res.start, res.end + 1 - res.start);
  596. devm_kfree(dev, mac_dev);
  597. dev_set_drvdata(dev, NULL);
  598. return -ENODEV;
  599. }
  600. /* Get the cell-index */
  601. err = of_property_read_u32(mac_node, "cell-index", &val);
  602. if (err) {
  603. dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
  604. err = -EINVAL;
  605. goto _return_of_get_parent;
  606. }
  607. priv->cell_index = (u8)val;
  608. /* Get the MAC address */
  609. mac_addr = of_get_mac_address(mac_node);
  610. if (!mac_addr) {
  611. dev_err(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
  612. err = -EINVAL;
  613. goto _return_of_get_parent;
  614. }
  615. memcpy(mac_dev->addr, mac_addr, sizeof(mac_dev->addr));
  616. /* Get the port handles */
  617. nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
  618. if (unlikely(nph < 0)) {
  619. dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
  620. mac_node);
  621. err = nph;
  622. goto _return_of_get_parent;
  623. }
  624. if (nph != ARRAY_SIZE(mac_dev->port)) {
  625. dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
  626. mac_node);
  627. err = -EINVAL;
  628. goto _return_of_get_parent;
  629. }
  630. for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
  631. /* Find the port node */
  632. dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
  633. if (!dev_node) {
  634. dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
  635. mac_node);
  636. err = -EINVAL;
  637. goto _return_of_node_put;
  638. }
  639. of_dev = of_find_device_by_node(dev_node);
  640. if (!of_dev) {
  641. dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
  642. dev_node);
  643. err = -EINVAL;
  644. goto _return_of_node_put;
  645. }
  646. mac_dev->port[i] = fman_port_bind(&of_dev->dev);
  647. if (!mac_dev->port[i]) {
  648. dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
  649. dev_node);
  650. err = -EINVAL;
  651. goto _return_of_node_put;
  652. }
  653. of_node_put(dev_node);
  654. }
  655. /* Get the PHY connection type */
  656. phy_if = of_get_phy_mode(mac_node);
  657. if (phy_if < 0) {
  658. dev_warn(dev,
  659. "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
  660. mac_node);
  661. phy_if = PHY_INTERFACE_MODE_SGMII;
  662. }
  663. mac_dev->phy_if = phy_if;
  664. priv->speed = phy2speed[mac_dev->phy_if];
  665. priv->max_speed = priv->speed;
  666. mac_dev->if_support = DTSEC_SUPPORTED;
  667. /* We don't support half-duplex in SGMII mode */
  668. if (mac_dev->phy_if == PHY_INTERFACE_MODE_SGMII)
  669. mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
  670. SUPPORTED_100baseT_Half);
  671. /* Gigabit support (no half-duplex) */
  672. if (priv->max_speed == 1000)
  673. mac_dev->if_support |= SUPPORTED_1000baseT_Full;
  674. /* The 10G interface only supports one mode */
  675. if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
  676. mac_dev->if_support = SUPPORTED_10000baseT_Full;
  677. /* Get the rest of the PHY information */
  678. mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
  679. if (!mac_dev->phy_node && of_phy_is_fixed_link(mac_node)) {
  680. struct phy_device *phy;
  681. err = of_phy_register_fixed_link(mac_node);
  682. if (err)
  683. goto _return_of_get_parent;
  684. priv->fixed_link = kzalloc(sizeof(*priv->fixed_link),
  685. GFP_KERNEL);
  686. if (!priv->fixed_link) {
  687. err = -ENOMEM;
  688. goto _return_of_get_parent;
  689. }
  690. mac_dev->phy_node = of_node_get(mac_node);
  691. phy = of_phy_find_device(mac_dev->phy_node);
  692. if (!phy) {
  693. err = -EINVAL;
  694. goto _return_of_get_parent;
  695. }
  696. priv->fixed_link->link = phy->link;
  697. priv->fixed_link->speed = phy->speed;
  698. priv->fixed_link->duplex = phy->duplex;
  699. priv->fixed_link->pause = phy->pause;
  700. priv->fixed_link->asym_pause = phy->asym_pause;
  701. put_device(&phy->mdio.dev);
  702. }
  703. err = mac_dev->init(mac_dev);
  704. if (err < 0) {
  705. dev_err(dev, "mac_dev->init() = %d\n", err);
  706. of_node_put(mac_dev->phy_node);
  707. goto _return_of_get_parent;
  708. }
  709. /* pause frame autonegotiation enabled */
  710. mac_dev->autoneg_pause = true;
  711. /* By intializing the values to false, force FMD to enable PAUSE frames
  712. * on RX and TX
  713. */
  714. mac_dev->rx_pause_req = true;
  715. mac_dev->tx_pause_req = true;
  716. mac_dev->rx_pause_active = false;
  717. mac_dev->tx_pause_active = false;
  718. err = fman_set_mac_active_pause(mac_dev, true, true);
  719. if (err < 0)
  720. dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
  721. dev_info(dev, "FMan MAC address: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
  722. mac_dev->addr[0], mac_dev->addr[1], mac_dev->addr[2],
  723. mac_dev->addr[3], mac_dev->addr[4], mac_dev->addr[5]);
  724. priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
  725. if (IS_ERR(priv->eth_dev)) {
  726. dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
  727. priv->cell_index);
  728. priv->eth_dev = NULL;
  729. }
  730. goto _return;
  731. _return_of_node_put:
  732. of_node_put(dev_node);
  733. _return_of_get_parent:
  734. kfree(priv->fixed_link);
  735. _return:
  736. return err;
  737. }
  738. static struct platform_driver mac_driver = {
  739. .driver = {
  740. .name = KBUILD_MODNAME,
  741. .of_match_table = mac_match,
  742. },
  743. .probe = mac_probe,
  744. };
  745. builtin_platform_driver(mac_driver);