keystone_net.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Ethernet driver for TI K2HK EVM.
  3. *
  4. * (C) Copyright 2012-2014
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <console.h>
  12. #include <dm.h>
  13. #include <dm/lists.h>
  14. #include <net.h>
  15. #include <phy.h>
  16. #include <errno.h>
  17. #include <miiphy.h>
  18. #include <malloc.h>
  19. #include <asm/ti-common/keystone_nav.h>
  20. #include <asm/ti-common/keystone_net.h>
  21. #include <asm/ti-common/keystone_serdes.h>
  22. #include <asm/arch/psc_defs.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifndef CONFIG_DM_ETH
  25. unsigned int emac_open;
  26. static struct mii_dev *mdio_bus;
  27. static unsigned int sys_has_mdio = 1;
  28. #endif
  29. #ifdef KEYSTONE2_EMAC_GIG_ENABLE
  30. #define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x)
  31. #else
  32. #define emac_gigabit_enable(x) /* no gigabit to enable */
  33. #endif
  34. #define RX_BUFF_NUMS 24
  35. #define RX_BUFF_LEN 1520
  36. #define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN
  37. #define SGMII_ANEG_TIMEOUT 4000
  38. static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16);
  39. #ifndef CONFIG_DM_ETH
  40. struct rx_buff_desc net_rx_buffs = {
  41. .buff_ptr = rx_buffs,
  42. .num_buffs = RX_BUFF_NUMS,
  43. .buff_len = RX_BUFF_LEN,
  44. .rx_flow = 22,
  45. };
  46. #endif
  47. #ifdef CONFIG_DM_ETH
  48. enum link_type {
  49. LINK_TYPE_MAC_TO_MAC_AUTO = 0,
  50. LINK_TYPE_MAC_TO_PHY_MODE = 1,
  51. LINK_TYPE_MAC_TO_MAC_FORCED_MODE = 2,
  52. LINK_TYPE_MAC_TO_FIBRE_MODE = 3,
  53. LINK_TYPE_MAC_TO_PHY_NO_MDIO_MODE = 4,
  54. LINK_TYPE_10G_MAC_TO_PHY_MODE = 10,
  55. LINK_TYPE_10G_MAC_TO_MAC_FORCED_MODE = 11,
  56. };
  57. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  58. ((mac)[2] << 16) | ((mac)[3] << 24))
  59. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  60. #ifdef CONFIG_KSNET_NETCP_V1_0
  61. #define EMAC_EMACSW_BASE_OFS 0x90800
  62. #define EMAC_EMACSW_PORT_BASE_OFS (EMAC_EMACSW_BASE_OFS + 0x60)
  63. /* CPSW Switch slave registers */
  64. #define CPGMACSL_REG_SA_LO 0x10
  65. #define CPGMACSL_REG_SA_HI 0x14
  66. #define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
  67. (x) * 0x30)
  68. #elif defined CONFIG_KSNET_NETCP_V1_5
  69. #define EMAC_EMACSW_PORT_BASE_OFS 0x222000
  70. /* CPSW Switch slave registers */
  71. #define CPGMACSL_REG_SA_LO 0x308
  72. #define CPGMACSL_REG_SA_HI 0x30c
  73. #define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
  74. (x) * 0x1000)
  75. #endif
  76. struct ks2_eth_priv {
  77. struct udevice *dev;
  78. struct phy_device *phydev;
  79. struct mii_dev *mdio_bus;
  80. int phy_addr;
  81. phy_interface_t phy_if;
  82. int sgmii_link_type;
  83. void *mdio_base;
  84. struct rx_buff_desc net_rx_buffs;
  85. struct pktdma_cfg *netcp_pktdma;
  86. void *hd;
  87. int slave_port;
  88. enum link_type link_type;
  89. bool emac_open;
  90. bool has_mdio;
  91. };
  92. #endif
  93. /* MDIO */
  94. static int keystone2_mdio_reset(struct mii_dev *bus)
  95. {
  96. u_int32_t clkdiv;
  97. struct mdio_regs *adap_mdio = bus->priv;
  98. clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
  99. writel((clkdiv & 0xffff) | MDIO_CONTROL_ENABLE |
  100. MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE,
  101. &adap_mdio->control);
  102. while (readl(&adap_mdio->control) & MDIO_CONTROL_IDLE)
  103. ;
  104. return 0;
  105. }
  106. /**
  107. * keystone2_mdio_read - read a PHY register via MDIO interface.
  108. * Blocks until operation is complete.
  109. */
  110. static int keystone2_mdio_read(struct mii_dev *bus,
  111. int addr, int devad, int reg)
  112. {
  113. int tmp;
  114. struct mdio_regs *adap_mdio = bus->priv;
  115. while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
  116. ;
  117. writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ |
  118. ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16),
  119. &adap_mdio->useraccess0);
  120. /* Wait for command to complete */
  121. while ((tmp = readl(&adap_mdio->useraccess0)) & MDIO_USERACCESS0_GO)
  122. ;
  123. if (tmp & MDIO_USERACCESS0_ACK)
  124. return tmp & 0xffff;
  125. return -1;
  126. }
  127. /**
  128. * keystone2_mdio_write - write to a PHY register via MDIO interface.
  129. * Blocks until operation is complete.
  130. */
  131. static int keystone2_mdio_write(struct mii_dev *bus,
  132. int addr, int devad, int reg, u16 val)
  133. {
  134. struct mdio_regs *adap_mdio = bus->priv;
  135. while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
  136. ;
  137. writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE |
  138. ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16) |
  139. (val & 0xffff), &adap_mdio->useraccess0);
  140. /* Wait for command to complete */
  141. while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
  142. ;
  143. return 0;
  144. }
  145. #ifndef CONFIG_DM_ETH
  146. static void __attribute__((unused))
  147. keystone2_eth_gigabit_enable(struct eth_device *dev)
  148. {
  149. u_int16_t data;
  150. struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
  151. if (sys_has_mdio) {
  152. data = keystone2_mdio_read(mdio_bus, eth_priv->phy_addr,
  153. MDIO_DEVAD_NONE, 0);
  154. /* speed selection MSB */
  155. if (!(data & (1 << 6)))
  156. return;
  157. }
  158. /*
  159. * Check if link detected is giga-bit
  160. * If Gigabit mode detected, enable gigbit in MAC
  161. */
  162. writel(readl(DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) +
  163. CPGMACSL_REG_CTL) |
  164. EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
  165. DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + CPGMACSL_REG_CTL);
  166. }
  167. #else
  168. static void __attribute__((unused))
  169. keystone2_eth_gigabit_enable(struct udevice *dev)
  170. {
  171. struct ks2_eth_priv *priv = dev_get_priv(dev);
  172. u_int16_t data;
  173. if (priv->has_mdio) {
  174. data = keystone2_mdio_read(priv->mdio_bus, priv->phy_addr,
  175. MDIO_DEVAD_NONE, 0);
  176. /* speed selection MSB */
  177. if (!(data & (1 << 6)))
  178. return;
  179. }
  180. /*
  181. * Check if link detected is giga-bit
  182. * If Gigabit mode detected, enable gigbit in MAC
  183. */
  184. writel(readl(DEVICE_EMACSL_BASE(priv->slave_port - 1) +
  185. CPGMACSL_REG_CTL) |
  186. EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
  187. DEVICE_EMACSL_BASE(priv->slave_port - 1) + CPGMACSL_REG_CTL);
  188. }
  189. #endif
  190. #ifdef CONFIG_SOC_K2G
  191. int keystone_rgmii_config(struct phy_device *phy_dev)
  192. {
  193. unsigned int i, status;
  194. i = 0;
  195. do {
  196. if (i > SGMII_ANEG_TIMEOUT) {
  197. puts(" TIMEOUT !\n");
  198. phy_dev->link = 0;
  199. return 0;
  200. }
  201. if (ctrlc()) {
  202. puts("user interrupt!\n");
  203. phy_dev->link = 0;
  204. return -EINTR;
  205. }
  206. if ((i++ % 500) == 0)
  207. printf(".");
  208. udelay(1000); /* 1 ms */
  209. status = readl(RGMII_STATUS_REG);
  210. } while (!(status & RGMII_REG_STATUS_LINK));
  211. puts(" done\n");
  212. return 0;
  213. }
  214. #else
  215. int keystone_sgmii_config(struct phy_device *phy_dev, int port, int interface)
  216. {
  217. unsigned int i, status, mask;
  218. unsigned int mr_adv_ability, control;
  219. switch (interface) {
  220. case SGMII_LINK_MAC_MAC_AUTONEG:
  221. mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
  222. SGMII_REG_MR_ADV_LINK |
  223. SGMII_REG_MR_ADV_FULL_DUPLEX |
  224. SGMII_REG_MR_ADV_GIG_MODE);
  225. control = (SGMII_REG_CONTROL_MASTER |
  226. SGMII_REG_CONTROL_AUTONEG);
  227. break;
  228. case SGMII_LINK_MAC_PHY:
  229. case SGMII_LINK_MAC_PHY_FORCED:
  230. mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
  231. control = SGMII_REG_CONTROL_AUTONEG;
  232. break;
  233. case SGMII_LINK_MAC_MAC_FORCED:
  234. mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
  235. SGMII_REG_MR_ADV_LINK |
  236. SGMII_REG_MR_ADV_FULL_DUPLEX |
  237. SGMII_REG_MR_ADV_GIG_MODE);
  238. control = SGMII_REG_CONTROL_MASTER;
  239. break;
  240. case SGMII_LINK_MAC_FIBER:
  241. mr_adv_ability = 0x20;
  242. control = SGMII_REG_CONTROL_AUTONEG;
  243. break;
  244. default:
  245. mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
  246. control = SGMII_REG_CONTROL_AUTONEG;
  247. }
  248. __raw_writel(0, SGMII_CTL_REG(port));
  249. /*
  250. * Wait for the SerDes pll to lock,
  251. * but don't trap if lock is never read
  252. */
  253. for (i = 0; i < 1000; i++) {
  254. udelay(2000);
  255. status = __raw_readl(SGMII_STATUS_REG(port));
  256. if ((status & SGMII_REG_STATUS_LOCK) != 0)
  257. break;
  258. }
  259. __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
  260. __raw_writel(control, SGMII_CTL_REG(port));
  261. mask = SGMII_REG_STATUS_LINK;
  262. if (control & SGMII_REG_CONTROL_AUTONEG)
  263. mask |= SGMII_REG_STATUS_AUTONEG;
  264. status = __raw_readl(SGMII_STATUS_REG(port));
  265. if ((status & mask) == mask)
  266. return 0;
  267. printf("\n%s Waiting for SGMII auto negotiation to complete",
  268. phy_dev->dev->name);
  269. while ((status & mask) != mask) {
  270. /*
  271. * Timeout reached ?
  272. */
  273. if (i > SGMII_ANEG_TIMEOUT) {
  274. puts(" TIMEOUT !\n");
  275. phy_dev->link = 0;
  276. return 0;
  277. }
  278. if (ctrlc()) {
  279. puts("user interrupt!\n");
  280. phy_dev->link = 0;
  281. return -EINTR;
  282. }
  283. if ((i++ % 500) == 0)
  284. printf(".");
  285. udelay(1000); /* 1 ms */
  286. status = __raw_readl(SGMII_STATUS_REG(port));
  287. }
  288. puts(" done\n");
  289. return 0;
  290. }
  291. #endif
  292. int mac_sl_reset(u32 port)
  293. {
  294. u32 i, v;
  295. if (port >= DEVICE_N_GMACSL_PORTS)
  296. return GMACSL_RET_INVALID_PORT;
  297. /* Set the soft reset bit */
  298. writel(CPGMAC_REG_RESET_VAL_RESET,
  299. DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
  300. /* Wait for the bit to clear */
  301. for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
  302. v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
  303. if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
  304. CPGMAC_REG_RESET_VAL_RESET)
  305. return GMACSL_RET_OK;
  306. }
  307. /* Timeout on the reset */
  308. return GMACSL_RET_WARN_RESET_INCOMPLETE;
  309. }
  310. int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
  311. {
  312. u32 v, i;
  313. int ret = GMACSL_RET_OK;
  314. if (port >= DEVICE_N_GMACSL_PORTS)
  315. return GMACSL_RET_INVALID_PORT;
  316. if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
  317. cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
  318. ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
  319. }
  320. /* Must wait if the device is undergoing reset */
  321. for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
  322. v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
  323. if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
  324. CPGMAC_REG_RESET_VAL_RESET)
  325. break;
  326. }
  327. if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
  328. return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
  329. writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
  330. writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
  331. #ifndef CONFIG_SOC_K2HK
  332. /* Map RX packet flow priority to 0 */
  333. writel(0, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RX_PRI_MAP);
  334. #endif
  335. return ret;
  336. }
  337. int ethss_config(u32 ctl, u32 max_pkt_size)
  338. {
  339. u32 i;
  340. /* Max length register */
  341. writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
  342. /* Control register */
  343. writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
  344. /* All statistics enabled by default */
  345. writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
  346. DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
  347. /* Reset and enable the ALE */
  348. writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
  349. CPSW_REG_VAL_ALE_CTL_BYPASS,
  350. DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
  351. /* All ports put into forward mode */
  352. for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
  353. writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
  354. DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
  355. return 0;
  356. }
  357. int ethss_start(void)
  358. {
  359. int i;
  360. struct mac_sl_cfg cfg;
  361. cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
  362. cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
  363. for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
  364. mac_sl_reset(i);
  365. mac_sl_config(i, &cfg);
  366. }
  367. return 0;
  368. }
  369. int ethss_stop(void)
  370. {
  371. int i;
  372. for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
  373. mac_sl_reset(i);
  374. return 0;
  375. }
  376. struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
  377. .clk = SERDES_CLOCK_156P25M,
  378. .rate = SERDES_RATE_5G,
  379. .rate_mode = SERDES_QUARTER_RATE,
  380. .intf = SERDES_PHY_SGMII,
  381. .loopback = 0,
  382. };
  383. #ifndef CONFIG_SOC_K2G
  384. static void keystone2_net_serdes_setup(void)
  385. {
  386. ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE,
  387. &ks2_serdes_sgmii_156p25mhz,
  388. CONFIG_KSNET_SERDES_LANES_PER_SGMII);
  389. #if defined(CONFIG_SOC_K2E) || defined(CONFIG_SOC_K2L)
  390. ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII2_BASE,
  391. &ks2_serdes_sgmii_156p25mhz,
  392. CONFIG_KSNET_SERDES_LANES_PER_SGMII);
  393. #endif
  394. /* wait till setup */
  395. udelay(5000);
  396. }
  397. #endif
  398. #ifndef CONFIG_DM_ETH
  399. int keystone2_eth_read_mac_addr(struct eth_device *dev)
  400. {
  401. struct eth_priv_t *eth_priv;
  402. u32 maca = 0;
  403. u32 macb = 0;
  404. eth_priv = (struct eth_priv_t *)dev->priv;
  405. /* Read the e-fuse mac address */
  406. if (eth_priv->slave_port == 1) {
  407. maca = __raw_readl(MAC_ID_BASE_ADDR);
  408. macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
  409. }
  410. dev->enetaddr[0] = (macb >> 8) & 0xff;
  411. dev->enetaddr[1] = (macb >> 0) & 0xff;
  412. dev->enetaddr[2] = (maca >> 24) & 0xff;
  413. dev->enetaddr[3] = (maca >> 16) & 0xff;
  414. dev->enetaddr[4] = (maca >> 8) & 0xff;
  415. dev->enetaddr[5] = (maca >> 0) & 0xff;
  416. return 0;
  417. }
  418. int32_t cpmac_drv_send(u32 *buffer, int num_bytes, int slave_port_num)
  419. {
  420. if (num_bytes < EMAC_MIN_ETHERNET_PKT_SIZE)
  421. num_bytes = EMAC_MIN_ETHERNET_PKT_SIZE;
  422. return ksnav_send(&netcp_pktdma, buffer,
  423. num_bytes, (slave_port_num) << 16);
  424. }
  425. /* Eth device open */
  426. static int keystone2_eth_open(struct eth_device *dev, bd_t *bis)
  427. {
  428. struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
  429. struct phy_device *phy_dev = eth_priv->phy_dev;
  430. debug("+ emac_open\n");
  431. net_rx_buffs.rx_flow = eth_priv->rx_flow;
  432. sys_has_mdio =
  433. (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY) ? 1 : 0;
  434. if (sys_has_mdio)
  435. keystone2_mdio_reset(mdio_bus);
  436. #ifdef CONFIG_SOC_K2G
  437. keystone_rgmii_config(phy_dev);
  438. #else
  439. keystone_sgmii_config(phy_dev, eth_priv->slave_port - 1,
  440. eth_priv->sgmii_link_type);
  441. #endif
  442. udelay(10000);
  443. /* On chip switch configuration */
  444. ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
  445. /* TODO: add error handling code */
  446. if (qm_init()) {
  447. printf("ERROR: qm_init()\n");
  448. return -1;
  449. }
  450. if (ksnav_init(&netcp_pktdma, &net_rx_buffs)) {
  451. qm_close();
  452. printf("ERROR: netcp_init()\n");
  453. return -1;
  454. }
  455. /*
  456. * Streaming switch configuration. If not present this
  457. * statement is defined to void in target.h.
  458. * If present this is usually defined to a series of register writes
  459. */
  460. hw_config_streaming_switch();
  461. if (sys_has_mdio) {
  462. keystone2_mdio_reset(mdio_bus);
  463. phy_startup(phy_dev);
  464. if (phy_dev->link == 0) {
  465. ksnav_close(&netcp_pktdma);
  466. qm_close();
  467. return -1;
  468. }
  469. }
  470. emac_gigabit_enable(dev);
  471. ethss_start();
  472. debug("- emac_open\n");
  473. emac_open = 1;
  474. return 0;
  475. }
  476. /* Eth device close */
  477. void keystone2_eth_close(struct eth_device *dev)
  478. {
  479. struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
  480. struct phy_device *phy_dev = eth_priv->phy_dev;
  481. debug("+ emac_close\n");
  482. if (!emac_open)
  483. return;
  484. ethss_stop();
  485. ksnav_close(&netcp_pktdma);
  486. qm_close();
  487. phy_shutdown(phy_dev);
  488. emac_open = 0;
  489. debug("- emac_close\n");
  490. }
  491. /*
  492. * This function sends a single packet on the network and returns
  493. * positive number (number of bytes transmitted) or negative for error
  494. */
  495. static int keystone2_eth_send_packet(struct eth_device *dev,
  496. void *packet, int length)
  497. {
  498. int ret_status = -1;
  499. struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
  500. struct phy_device *phy_dev = eth_priv->phy_dev;
  501. genphy_update_link(phy_dev);
  502. if (phy_dev->link == 0)
  503. return -1;
  504. if (cpmac_drv_send((u32 *)packet, length, eth_priv->slave_port) != 0)
  505. return ret_status;
  506. return length;
  507. }
  508. /*
  509. * This function handles receipt of a packet from the network
  510. */
  511. static int keystone2_eth_rcv_packet(struct eth_device *dev)
  512. {
  513. void *hd;
  514. int pkt_size;
  515. u32 *pkt;
  516. hd = ksnav_recv(&netcp_pktdma, &pkt, &pkt_size);
  517. if (hd == NULL)
  518. return 0;
  519. net_process_received_packet((uchar *)pkt, pkt_size);
  520. ksnav_release_rxhd(&netcp_pktdma, hd);
  521. return pkt_size;
  522. }
  523. #ifdef CONFIG_MCAST_TFTP
  524. static int keystone2_eth_bcast_addr(struct eth_device *dev, u32 ip, u8 set)
  525. {
  526. return 0;
  527. }
  528. #endif
  529. /*
  530. * This function initializes the EMAC hardware.
  531. */
  532. int keystone2_emac_initialize(struct eth_priv_t *eth_priv)
  533. {
  534. int res;
  535. struct eth_device *dev;
  536. struct phy_device *phy_dev;
  537. struct mdio_regs *adap_mdio = (struct mdio_regs *)EMAC_MDIO_BASE_ADDR;
  538. dev = malloc(sizeof(struct eth_device));
  539. if (dev == NULL)
  540. return -1;
  541. memset(dev, 0, sizeof(struct eth_device));
  542. strcpy(dev->name, eth_priv->int_name);
  543. dev->priv = eth_priv;
  544. keystone2_eth_read_mac_addr(dev);
  545. dev->iobase = 0;
  546. dev->init = keystone2_eth_open;
  547. dev->halt = keystone2_eth_close;
  548. dev->send = keystone2_eth_send_packet;
  549. dev->recv = keystone2_eth_rcv_packet;
  550. #ifdef CONFIG_MCAST_TFTP
  551. dev->mcast = keystone2_eth_bcast_addr;
  552. #endif
  553. eth_register(dev);
  554. /* Register MDIO bus if it's not registered yet */
  555. if (!mdio_bus) {
  556. mdio_bus = mdio_alloc();
  557. mdio_bus->read = keystone2_mdio_read;
  558. mdio_bus->write = keystone2_mdio_write;
  559. mdio_bus->reset = keystone2_mdio_reset;
  560. mdio_bus->priv = (void *)EMAC_MDIO_BASE_ADDR;
  561. strcpy(mdio_bus->name, "ethernet-mdio");
  562. res = mdio_register(mdio_bus);
  563. if (res)
  564. return res;
  565. }
  566. #ifndef CONFIG_SOC_K2G
  567. keystone2_net_serdes_setup();
  568. #endif
  569. /* Create phy device and bind it with driver */
  570. #ifdef CONFIG_KSNET_MDIO_PHY_CONFIG_ENABLE
  571. phy_dev = phy_connect(mdio_bus, eth_priv->phy_addr,
  572. dev, eth_priv->phy_if);
  573. phy_config(phy_dev);
  574. #else
  575. phy_dev = phy_find_by_mask(mdio_bus, 1 << eth_priv->phy_addr,
  576. eth_priv->phy_if);
  577. phy_dev->dev = dev;
  578. #endif
  579. eth_priv->phy_dev = phy_dev;
  580. return 0;
  581. }
  582. #else
  583. static int ks2_eth_start(struct udevice *dev)
  584. {
  585. struct ks2_eth_priv *priv = dev_get_priv(dev);
  586. #ifdef CONFIG_SOC_K2G
  587. keystone_rgmii_config(priv->phydev);
  588. #else
  589. keystone_sgmii_config(priv->phydev, priv->slave_port - 1,
  590. priv->sgmii_link_type);
  591. #endif
  592. udelay(10000);
  593. /* On chip switch configuration */
  594. ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
  595. qm_init();
  596. if (ksnav_init(priv->netcp_pktdma, &priv->net_rx_buffs)) {
  597. error("ksnav_init failed\n");
  598. goto err_knav_init;
  599. }
  600. /*
  601. * Streaming switch configuration. If not present this
  602. * statement is defined to void in target.h.
  603. * If present this is usually defined to a series of register writes
  604. */
  605. hw_config_streaming_switch();
  606. if (priv->has_mdio) {
  607. keystone2_mdio_reset(priv->mdio_bus);
  608. phy_startup(priv->phydev);
  609. if (priv->phydev->link == 0) {
  610. error("phy startup failed\n");
  611. goto err_phy_start;
  612. }
  613. }
  614. emac_gigabit_enable(dev);
  615. ethss_start();
  616. priv->emac_open = true;
  617. return 0;
  618. err_phy_start:
  619. ksnav_close(priv->netcp_pktdma);
  620. err_knav_init:
  621. qm_close();
  622. return -EFAULT;
  623. }
  624. static int ks2_eth_send(struct udevice *dev, void *packet, int length)
  625. {
  626. struct ks2_eth_priv *priv = dev_get_priv(dev);
  627. genphy_update_link(priv->phydev);
  628. if (priv->phydev->link == 0)
  629. return -1;
  630. if (length < EMAC_MIN_ETHERNET_PKT_SIZE)
  631. length = EMAC_MIN_ETHERNET_PKT_SIZE;
  632. return ksnav_send(priv->netcp_pktdma, (u32 *)packet,
  633. length, (priv->slave_port) << 16);
  634. }
  635. static int ks2_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  636. {
  637. struct ks2_eth_priv *priv = dev_get_priv(dev);
  638. int pkt_size;
  639. u32 *pkt = NULL;
  640. priv->hd = ksnav_recv(priv->netcp_pktdma, &pkt, &pkt_size);
  641. if (priv->hd == NULL)
  642. return -EAGAIN;
  643. *packetp = (uchar *)pkt;
  644. return pkt_size;
  645. }
  646. static int ks2_eth_free_pkt(struct udevice *dev, uchar *packet,
  647. int length)
  648. {
  649. struct ks2_eth_priv *priv = dev_get_priv(dev);
  650. ksnav_release_rxhd(priv->netcp_pktdma, priv->hd);
  651. return 0;
  652. }
  653. static void ks2_eth_stop(struct udevice *dev)
  654. {
  655. struct ks2_eth_priv *priv = dev_get_priv(dev);
  656. if (!priv->emac_open)
  657. return;
  658. ethss_stop();
  659. ksnav_close(priv->netcp_pktdma);
  660. qm_close();
  661. phy_shutdown(priv->phydev);
  662. priv->emac_open = false;
  663. }
  664. int ks2_eth_read_rom_hwaddr(struct udevice *dev)
  665. {
  666. struct ks2_eth_priv *priv = dev_get_priv(dev);
  667. struct eth_pdata *pdata = dev_get_platdata(dev);
  668. u32 maca = 0;
  669. u32 macb = 0;
  670. /* Read the e-fuse mac address */
  671. if (priv->slave_port == 1) {
  672. maca = __raw_readl(MAC_ID_BASE_ADDR);
  673. macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
  674. }
  675. pdata->enetaddr[0] = (macb >> 8) & 0xff;
  676. pdata->enetaddr[1] = (macb >> 0) & 0xff;
  677. pdata->enetaddr[2] = (maca >> 24) & 0xff;
  678. pdata->enetaddr[3] = (maca >> 16) & 0xff;
  679. pdata->enetaddr[4] = (maca >> 8) & 0xff;
  680. pdata->enetaddr[5] = (maca >> 0) & 0xff;
  681. return 0;
  682. }
  683. int ks2_eth_write_hwaddr(struct udevice *dev)
  684. {
  685. struct ks2_eth_priv *priv = dev_get_priv(dev);
  686. struct eth_pdata *pdata = dev_get_platdata(dev);
  687. writel(mac_hi(pdata->enetaddr),
  688. DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
  689. CPGMACSL_REG_SA_HI);
  690. writel(mac_lo(pdata->enetaddr),
  691. DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
  692. CPGMACSL_REG_SA_LO);
  693. return 0;
  694. }
  695. static int ks2_eth_probe(struct udevice *dev)
  696. {
  697. struct ks2_eth_priv *priv = dev_get_priv(dev);
  698. struct mii_dev *mdio_bus;
  699. int ret;
  700. priv->dev = dev;
  701. /* These clock enables has to be moved to common location */
  702. if (cpu_is_k2g())
  703. writel(KS2_ETHERNET_RGMII, KS2_ETHERNET_CFG);
  704. /* By default, select PA PLL clock as PA clock source */
  705. #ifndef CONFIG_SOC_K2G
  706. if (psc_enable_module(KS2_LPSC_PA))
  707. return -EACCES;
  708. #endif
  709. if (psc_enable_module(KS2_LPSC_CPGMAC))
  710. return -EACCES;
  711. if (psc_enable_module(KS2_LPSC_CRYPTO))
  712. return -EACCES;
  713. if (cpu_is_k2e() || cpu_is_k2l())
  714. pll_pa_clk_sel();
  715. priv->net_rx_buffs.buff_ptr = rx_buffs;
  716. priv->net_rx_buffs.num_buffs = RX_BUFF_NUMS;
  717. priv->net_rx_buffs.buff_len = RX_BUFF_LEN;
  718. if (priv->slave_port == 1) {
  719. /*
  720. * Register MDIO bus for slave 0 only, other slave have
  721. * to re-use the same
  722. */
  723. mdio_bus = mdio_alloc();
  724. if (!mdio_bus) {
  725. error("MDIO alloc failed\n");
  726. return -ENOMEM;
  727. }
  728. priv->mdio_bus = mdio_bus;
  729. mdio_bus->read = keystone2_mdio_read;
  730. mdio_bus->write = keystone2_mdio_write;
  731. mdio_bus->reset = keystone2_mdio_reset;
  732. mdio_bus->priv = priv->mdio_base;
  733. sprintf(mdio_bus->name, "ethernet-mdio");
  734. ret = mdio_register(mdio_bus);
  735. if (ret) {
  736. error("MDIO bus register failed\n");
  737. return ret;
  738. }
  739. } else {
  740. /* Get the MDIO bus from slave 0 device */
  741. struct ks2_eth_priv *parent_priv;
  742. parent_priv = dev_get_priv(dev->parent);
  743. priv->mdio_bus = parent_priv->mdio_bus;
  744. }
  745. #ifndef CONFIG_SOC_K2G
  746. keystone2_net_serdes_setup();
  747. #endif
  748. priv->netcp_pktdma = &netcp_pktdma;
  749. if (priv->has_mdio) {
  750. priv->phydev = phy_connect(priv->mdio_bus, priv->phy_addr,
  751. dev, priv->phy_if);
  752. phy_config(priv->phydev);
  753. }
  754. return 0;
  755. }
  756. int ks2_eth_remove(struct udevice *dev)
  757. {
  758. struct ks2_eth_priv *priv = dev_get_priv(dev);
  759. free(priv->phydev);
  760. mdio_unregister(priv->mdio_bus);
  761. mdio_free(priv->mdio_bus);
  762. return 0;
  763. }
  764. static const struct eth_ops ks2_eth_ops = {
  765. .start = ks2_eth_start,
  766. .send = ks2_eth_send,
  767. .recv = ks2_eth_recv,
  768. .free_pkt = ks2_eth_free_pkt,
  769. .stop = ks2_eth_stop,
  770. .read_rom_hwaddr = ks2_eth_read_rom_hwaddr,
  771. .write_hwaddr = ks2_eth_write_hwaddr,
  772. };
  773. static int ks2_eth_bind_slaves(struct udevice *dev, int gbe, int *gbe_0)
  774. {
  775. const void *fdt = gd->fdt_blob;
  776. struct udevice *sl_dev;
  777. int interfaces;
  778. int sec_slave;
  779. int slave;
  780. int ret;
  781. char *slave_name;
  782. interfaces = fdt_subnode_offset(fdt, gbe, "interfaces");
  783. fdt_for_each_subnode(fdt, slave, interfaces) {
  784. int slave_no;
  785. slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
  786. if (slave_no == -ENOENT)
  787. continue;
  788. if (slave_no == 0) {
  789. /* This is the current eth device */
  790. *gbe_0 = slave;
  791. } else {
  792. /* Slave devices to be registered */
  793. slave_name = malloc(20);
  794. snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
  795. ret = device_bind_driver_to_node(dev, "eth_ks2_sl",
  796. slave_name, slave,
  797. &sl_dev);
  798. if (ret) {
  799. error("ks2_net - not able to bind slave interfaces\n");
  800. return ret;
  801. }
  802. }
  803. }
  804. sec_slave = fdt_subnode_offset(fdt, gbe, "secondary-slave-ports");
  805. fdt_for_each_subnode(fdt, slave, sec_slave) {
  806. int slave_no;
  807. slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
  808. if (slave_no == -ENOENT)
  809. continue;
  810. /* Slave devices to be registered */
  811. slave_name = malloc(20);
  812. snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
  813. ret = device_bind_driver_to_node(dev, "eth_ks2_sl", slave_name,
  814. slave, &sl_dev);
  815. if (ret) {
  816. error("ks2_net - not able to bind slave interfaces\n");
  817. return ret;
  818. }
  819. }
  820. return 0;
  821. }
  822. static int ks2_eth_parse_slave_interface(int netcp, int slave,
  823. struct ks2_eth_priv *priv,
  824. struct eth_pdata *pdata)
  825. {
  826. const void *fdt = gd->fdt_blob;
  827. int mdio;
  828. int phy;
  829. int dma_count;
  830. u32 dma_channel[8];
  831. priv->slave_port = fdtdec_get_int(fdt, slave, "slave-port", -1);
  832. priv->net_rx_buffs.rx_flow = priv->slave_port * 8;
  833. /* U-Boot slave port number starts with 1 instead of 0 */
  834. priv->slave_port += 1;
  835. dma_count = fdtdec_get_int_array_count(fdt, netcp,
  836. "ti,navigator-dmas",
  837. dma_channel, 8);
  838. if (dma_count > (2 * priv->slave_port)) {
  839. int dma_idx;
  840. dma_idx = priv->slave_port * 2 - 1;
  841. priv->net_rx_buffs.rx_flow = dma_channel[dma_idx];
  842. }
  843. priv->link_type = fdtdec_get_int(fdt, slave, "link-interface", -1);
  844. phy = fdtdec_lookup_phandle(fdt, slave, "phy-handle");
  845. if (phy >= 0) {
  846. priv->phy_addr = fdtdec_get_int(fdt, phy, "reg", -1);
  847. mdio = fdt_parent_offset(fdt, phy);
  848. if (mdio < 0) {
  849. error("mdio dt not found\n");
  850. return -ENODEV;
  851. }
  852. priv->mdio_base = (void *)fdtdec_get_addr(fdt, mdio, "reg");
  853. }
  854. if (priv->link_type == LINK_TYPE_MAC_TO_PHY_MODE) {
  855. priv->phy_if = PHY_INTERFACE_MODE_SGMII;
  856. pdata->phy_interface = priv->phy_if;
  857. priv->sgmii_link_type = SGMII_LINK_MAC_PHY;
  858. priv->has_mdio = true;
  859. }
  860. return 0;
  861. }
  862. static int ks2_sl_eth_ofdata_to_platdata(struct udevice *dev)
  863. {
  864. struct ks2_eth_priv *priv = dev_get_priv(dev);
  865. struct eth_pdata *pdata = dev_get_platdata(dev);
  866. const void *fdt = gd->fdt_blob;
  867. int slave = dev->of_offset;
  868. int interfaces;
  869. int gbe;
  870. int netcp_devices;
  871. int netcp;
  872. interfaces = fdt_parent_offset(fdt, slave);
  873. gbe = fdt_parent_offset(fdt, interfaces);
  874. netcp_devices = fdt_parent_offset(fdt, gbe);
  875. netcp = fdt_parent_offset(fdt, netcp_devices);
  876. ks2_eth_parse_slave_interface(netcp, slave, priv, pdata);
  877. pdata->iobase = fdtdec_get_addr(fdt, netcp, "reg");
  878. return 0;
  879. }
  880. static int ks2_eth_ofdata_to_platdata(struct udevice *dev)
  881. {
  882. struct ks2_eth_priv *priv = dev_get_priv(dev);
  883. struct eth_pdata *pdata = dev_get_platdata(dev);
  884. const void *fdt = gd->fdt_blob;
  885. int gbe_0 = -ENODEV;
  886. int netcp_devices;
  887. int gbe;
  888. netcp_devices = fdt_subnode_offset(fdt, dev->of_offset,
  889. "netcp-devices");
  890. gbe = fdt_subnode_offset(fdt, netcp_devices, "gbe");
  891. ks2_eth_bind_slaves(dev, gbe, &gbe_0);
  892. ks2_eth_parse_slave_interface(dev->of_offset, gbe_0, priv, pdata);
  893. pdata->iobase = dev_get_addr(dev);
  894. return 0;
  895. }
  896. static const struct udevice_id ks2_eth_ids[] = {
  897. { .compatible = "ti,netcp-1.0" },
  898. { }
  899. };
  900. U_BOOT_DRIVER(eth_ks2_slave) = {
  901. .name = "eth_ks2_sl",
  902. .id = UCLASS_ETH,
  903. .ofdata_to_platdata = ks2_sl_eth_ofdata_to_platdata,
  904. .probe = ks2_eth_probe,
  905. .remove = ks2_eth_remove,
  906. .ops = &ks2_eth_ops,
  907. .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
  908. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  909. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  910. };
  911. U_BOOT_DRIVER(eth_ks2) = {
  912. .name = "eth_ks2",
  913. .id = UCLASS_ETH,
  914. .of_match = ks2_eth_ids,
  915. .ofdata_to_platdata = ks2_eth_ofdata_to_platdata,
  916. .probe = ks2_eth_probe,
  917. .remove = ks2_eth_remove,
  918. .ops = &ks2_eth_ops,
  919. .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
  920. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  921. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  922. };
  923. #endif