pcs-rzn1-miic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022 Schneider Electric
  4. *
  5. * Clément Léger <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/device.h>
  9. #include <linux/mdio.h>
  10. #include <linux/of.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/pcs-rzn1-miic.h>
  13. #include <linux/phylink.h>
  14. #include <linux/pm_runtime.h>
  15. #include <dt-bindings/net/pcs-rzn1-miic.h>
  16. #define MIIC_PRCMD 0x0
  17. #define MIIC_ESID_CODE 0x4
  18. #define MIIC_MODCTRL 0x20
  19. #define MIIC_MODCTRL_SW_MODE GENMASK(4, 0)
  20. #define MIIC_CONVCTRL(port) (0x100 + (port) * 4)
  21. #define MIIC_CONVCTRL_CONV_SPEED GENMASK(1, 0)
  22. #define CONV_MODE_10MBPS 0
  23. #define CONV_MODE_100MBPS 1
  24. #define CONV_MODE_1000MBPS 2
  25. #define MIIC_CONVCTRL_CONV_MODE GENMASK(3, 2)
  26. #define CONV_MODE_MII 0
  27. #define CONV_MODE_RMII 1
  28. #define CONV_MODE_RGMII 2
  29. #define MIIC_CONVCTRL_FULLD BIT(8)
  30. #define MIIC_CONVCTRL_RGMII_LINK BIT(12)
  31. #define MIIC_CONVCTRL_RGMII_DUPLEX BIT(13)
  32. #define MIIC_CONVCTRL_RGMII_SPEED GENMASK(15, 14)
  33. #define MIIC_CONVRST 0x114
  34. #define MIIC_CONVRST_PHYIF_RST(port) BIT(port)
  35. #define MIIC_CONVRST_PHYIF_RST_MASK GENMASK(4, 0)
  36. #define MIIC_SWCTRL 0x304
  37. #define MIIC_SWDUPC 0x308
  38. #define MIIC_MAX_NR_PORTS 5
  39. #define MIIC_MODCTRL_CONF_CONV_NUM 6
  40. #define MIIC_MODCTRL_CONF_NONE -1
  41. /**
  42. * struct modctrl_match - Matching table entry for convctrl configuration
  43. * See section 8.2.1 of manual.
  44. * @mode_cfg: Configuration value for convctrl
  45. * @conv: Configuration of ethernet port muxes. First index is SWITCH_PORTIN,
  46. * then index 1 - 5 are CONV1 - CONV5.
  47. */
  48. struct modctrl_match {
  49. u32 mode_cfg;
  50. u8 conv[MIIC_MODCTRL_CONF_CONV_NUM];
  51. };
  52. static struct modctrl_match modctrl_match_table[] = {
  53. {0x0, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  54. MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
  55. {0x1, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  56. MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  57. {0x2, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  58. MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  59. {0x3, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  60. MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}},
  61. {0x8, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  62. MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
  63. {0x9, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  64. MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  65. {0xA, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  66. MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  67. {0xB, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  68. MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}},
  69. {0x10, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  70. MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
  71. {0x11, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  72. MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  73. {0x12, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  74. MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
  75. {0x13, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
  76. MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}}
  77. };
  78. static const char * const conf_to_string[] = {
  79. [MIIC_GMAC1_PORT] = "GMAC1_PORT",
  80. [MIIC_GMAC2_PORT] = "GMAC2_PORT",
  81. [MIIC_RTOS_PORT] = "RTOS_PORT",
  82. [MIIC_SERCOS_PORTA] = "SERCOS_PORTA",
  83. [MIIC_SERCOS_PORTB] = "SERCOS_PORTB",
  84. [MIIC_ETHERCAT_PORTA] = "ETHERCAT_PORTA",
  85. [MIIC_ETHERCAT_PORTB] = "ETHERCAT_PORTB",
  86. [MIIC_ETHERCAT_PORTC] = "ETHERCAT_PORTC",
  87. [MIIC_SWITCH_PORTA] = "SWITCH_PORTA",
  88. [MIIC_SWITCH_PORTB] = "SWITCH_PORTB",
  89. [MIIC_SWITCH_PORTC] = "SWITCH_PORTC",
  90. [MIIC_SWITCH_PORTD] = "SWITCH_PORTD",
  91. [MIIC_HSR_PORTA] = "HSR_PORTA",
  92. [MIIC_HSR_PORTB] = "HSR_PORTB",
  93. };
  94. static const char *index_to_string[MIIC_MODCTRL_CONF_CONV_NUM] = {
  95. "SWITCH_PORTIN",
  96. "CONV1",
  97. "CONV2",
  98. "CONV3",
  99. "CONV4",
  100. "CONV5",
  101. };
  102. /**
  103. * struct miic - MII converter structure
  104. * @base: base address of the MII converter
  105. * @dev: Device associated to the MII converter
  106. * @clks: Clocks used for this device
  107. * @nclk: Number of clocks
  108. * @lock: Lock used for read-modify-write access
  109. */
  110. struct miic {
  111. void __iomem *base;
  112. struct device *dev;
  113. struct clk_bulk_data *clks;
  114. int nclk;
  115. spinlock_t lock;
  116. };
  117. /**
  118. * struct miic_port - Per port MII converter struct
  119. * @miic: backiling to MII converter structure
  120. * @pcs: PCS structure associated to the port
  121. * @port: port number
  122. * @interface: interface mode of the port
  123. */
  124. struct miic_port {
  125. struct miic *miic;
  126. struct phylink_pcs pcs;
  127. int port;
  128. phy_interface_t interface;
  129. };
  130. static struct miic_port *phylink_pcs_to_miic_port(struct phylink_pcs *pcs)
  131. {
  132. return container_of(pcs, struct miic_port, pcs);
  133. }
  134. static void miic_reg_writel(struct miic *miic, int offset, u32 value)
  135. {
  136. writel(value, miic->base + offset);
  137. }
  138. static u32 miic_reg_readl(struct miic *miic, int offset)
  139. {
  140. return readl(miic->base + offset);
  141. }
  142. static void miic_reg_rmw(struct miic *miic, int offset, u32 mask, u32 val)
  143. {
  144. u32 reg;
  145. spin_lock(&miic->lock);
  146. reg = miic_reg_readl(miic, offset);
  147. reg &= ~mask;
  148. reg |= val;
  149. miic_reg_writel(miic, offset, reg);
  150. spin_unlock(&miic->lock);
  151. }
  152. static void miic_converter_enable(struct miic *miic, int port, int enable)
  153. {
  154. u32 val = 0;
  155. if (enable)
  156. val = MIIC_CONVRST_PHYIF_RST(port);
  157. miic_reg_rmw(miic, MIIC_CONVRST, MIIC_CONVRST_PHYIF_RST(port), val);
  158. }
  159. static int miic_config(struct phylink_pcs *pcs, unsigned int mode,
  160. phy_interface_t interface,
  161. const unsigned long *advertising, bool permit)
  162. {
  163. struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
  164. struct miic *miic = miic_port->miic;
  165. u32 speed, conv_mode, val, mask;
  166. int port = miic_port->port;
  167. switch (interface) {
  168. case PHY_INTERFACE_MODE_RMII:
  169. conv_mode = CONV_MODE_RMII;
  170. speed = CONV_MODE_100MBPS;
  171. break;
  172. case PHY_INTERFACE_MODE_RGMII:
  173. case PHY_INTERFACE_MODE_RGMII_ID:
  174. case PHY_INTERFACE_MODE_RGMII_TXID:
  175. case PHY_INTERFACE_MODE_RGMII_RXID:
  176. conv_mode = CONV_MODE_RGMII;
  177. speed = CONV_MODE_1000MBPS;
  178. break;
  179. case PHY_INTERFACE_MODE_MII:
  180. conv_mode = CONV_MODE_MII;
  181. /* When in MII mode, speed should be set to 0 (which is actually
  182. * CONV_MODE_10MBPS)
  183. */
  184. speed = CONV_MODE_10MBPS;
  185. break;
  186. default:
  187. return -EOPNOTSUPP;
  188. }
  189. val = FIELD_PREP(MIIC_CONVCTRL_CONV_MODE, conv_mode);
  190. mask = MIIC_CONVCTRL_CONV_MODE;
  191. /* Update speed only if we are going to change the interface because
  192. * the link might already be up and it would break it if the speed is
  193. * changed.
  194. */
  195. if (interface != miic_port->interface) {
  196. val |= FIELD_PREP(MIIC_CONVCTRL_CONV_SPEED, speed);
  197. mask |= MIIC_CONVCTRL_CONV_SPEED;
  198. miic_port->interface = interface;
  199. }
  200. miic_reg_rmw(miic, MIIC_CONVCTRL(port), mask, val);
  201. miic_converter_enable(miic_port->miic, miic_port->port, 1);
  202. return 0;
  203. }
  204. static void miic_link_up(struct phylink_pcs *pcs, unsigned int mode,
  205. phy_interface_t interface, int speed, int duplex)
  206. {
  207. struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
  208. struct miic *miic = miic_port->miic;
  209. u32 conv_speed = 0, val = 0;
  210. int port = miic_port->port;
  211. if (duplex == DUPLEX_FULL)
  212. val |= MIIC_CONVCTRL_FULLD;
  213. /* No speed in MII through-mode */
  214. if (interface != PHY_INTERFACE_MODE_MII) {
  215. switch (speed) {
  216. case SPEED_1000:
  217. conv_speed = CONV_MODE_1000MBPS;
  218. break;
  219. case SPEED_100:
  220. conv_speed = CONV_MODE_100MBPS;
  221. break;
  222. case SPEED_10:
  223. conv_speed = CONV_MODE_10MBPS;
  224. break;
  225. default:
  226. return;
  227. }
  228. }
  229. val |= FIELD_PREP(MIIC_CONVCTRL_CONV_SPEED, conv_speed);
  230. miic_reg_rmw(miic, MIIC_CONVCTRL(port),
  231. (MIIC_CONVCTRL_CONV_SPEED | MIIC_CONVCTRL_FULLD), val);
  232. }
  233. static int miic_validate(struct phylink_pcs *pcs, unsigned long *supported,
  234. const struct phylink_link_state *state)
  235. {
  236. if (phy_interface_mode_is_rgmii(state->interface) ||
  237. state->interface == PHY_INTERFACE_MODE_RMII ||
  238. state->interface == PHY_INTERFACE_MODE_MII)
  239. return 1;
  240. return -EINVAL;
  241. }
  242. static const struct phylink_pcs_ops miic_phylink_ops = {
  243. .pcs_validate = miic_validate,
  244. .pcs_config = miic_config,
  245. .pcs_link_up = miic_link_up,
  246. };
  247. struct phylink_pcs *miic_create(struct device *dev, struct device_node *np)
  248. {
  249. struct platform_device *pdev;
  250. struct miic_port *miic_port;
  251. struct device_node *pcs_np;
  252. struct miic *miic;
  253. u32 port;
  254. if (!of_device_is_available(np))
  255. return ERR_PTR(-ENODEV);
  256. if (of_property_read_u32(np, "reg", &port))
  257. return ERR_PTR(-EINVAL);
  258. if (port > MIIC_MAX_NR_PORTS || port < 1)
  259. return ERR_PTR(-EINVAL);
  260. /* The PCS pdev is attached to the parent node */
  261. pcs_np = of_get_parent(np);
  262. if (!pcs_np)
  263. return ERR_PTR(-ENODEV);
  264. if (!of_device_is_available(pcs_np)) {
  265. of_node_put(pcs_np);
  266. return ERR_PTR(-ENODEV);
  267. }
  268. pdev = of_find_device_by_node(pcs_np);
  269. of_node_put(pcs_np);
  270. if (!pdev || !platform_get_drvdata(pdev)) {
  271. if (pdev)
  272. put_device(&pdev->dev);
  273. return ERR_PTR(-EPROBE_DEFER);
  274. }
  275. miic_port = kzalloc(sizeof(*miic_port), GFP_KERNEL);
  276. if (!miic_port) {
  277. put_device(&pdev->dev);
  278. return ERR_PTR(-ENOMEM);
  279. }
  280. miic = platform_get_drvdata(pdev);
  281. device_link_add(dev, miic->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
  282. put_device(&pdev->dev);
  283. miic_port->miic = miic;
  284. miic_port->port = port - 1;
  285. miic_port->pcs.ops = &miic_phylink_ops;
  286. return &miic_port->pcs;
  287. }
  288. EXPORT_SYMBOL(miic_create);
  289. void miic_destroy(struct phylink_pcs *pcs)
  290. {
  291. struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
  292. miic_converter_enable(miic_port->miic, miic_port->port, 0);
  293. kfree(miic_port);
  294. }
  295. EXPORT_SYMBOL(miic_destroy);
  296. static int miic_init_hw(struct miic *miic, u32 cfg_mode)
  297. {
  298. int port;
  299. /* Unlock write access to accessory registers (cf datasheet). If this
  300. * is going to be used in conjunction with the Cortex-M3, this sequence
  301. * will have to be moved in register write
  302. */
  303. miic_reg_writel(miic, MIIC_PRCMD, 0x00A5);
  304. miic_reg_writel(miic, MIIC_PRCMD, 0x0001);
  305. miic_reg_writel(miic, MIIC_PRCMD, 0xFFFE);
  306. miic_reg_writel(miic, MIIC_PRCMD, 0x0001);
  307. miic_reg_writel(miic, MIIC_MODCTRL,
  308. FIELD_PREP(MIIC_MODCTRL_SW_MODE, cfg_mode));
  309. for (port = 0; port < MIIC_MAX_NR_PORTS; port++) {
  310. miic_converter_enable(miic, port, 0);
  311. /* Disable speed/duplex control from these registers, datasheet
  312. * says switch registers should be used to setup switch port
  313. * speed and duplex.
  314. */
  315. miic_reg_writel(miic, MIIC_SWCTRL, 0x0);
  316. miic_reg_writel(miic, MIIC_SWDUPC, 0x0);
  317. }
  318. return 0;
  319. }
  320. static bool miic_modctrl_match(s8 table_val[MIIC_MODCTRL_CONF_CONV_NUM],
  321. s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM])
  322. {
  323. int i;
  324. for (i = 0; i < MIIC_MODCTRL_CONF_CONV_NUM; i++) {
  325. if (dt_val[i] == MIIC_MODCTRL_CONF_NONE)
  326. continue;
  327. if (dt_val[i] != table_val[i])
  328. return false;
  329. }
  330. return true;
  331. }
  332. static void miic_dump_conf(struct device *dev,
  333. s8 conf[MIIC_MODCTRL_CONF_CONV_NUM])
  334. {
  335. const char *conf_name;
  336. int i;
  337. for (i = 0; i < MIIC_MODCTRL_CONF_CONV_NUM; i++) {
  338. if (conf[i] != MIIC_MODCTRL_CONF_NONE)
  339. conf_name = conf_to_string[conf[i]];
  340. else
  341. conf_name = "NONE";
  342. dev_err(dev, "%s: %s\n", index_to_string[i], conf_name);
  343. }
  344. }
  345. static int miic_match_dt_conf(struct device *dev,
  346. s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM],
  347. u32 *mode_cfg)
  348. {
  349. struct modctrl_match *table_entry;
  350. int i;
  351. for (i = 0; i < ARRAY_SIZE(modctrl_match_table); i++) {
  352. table_entry = &modctrl_match_table[i];
  353. if (miic_modctrl_match(table_entry->conv, dt_val)) {
  354. *mode_cfg = table_entry->mode_cfg;
  355. return 0;
  356. }
  357. }
  358. dev_err(dev, "Failed to apply requested configuration\n");
  359. miic_dump_conf(dev, dt_val);
  360. return -EINVAL;
  361. }
  362. static int miic_parse_dt(struct device *dev, u32 *mode_cfg)
  363. {
  364. s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM];
  365. struct device_node *np = dev->of_node;
  366. struct device_node *conv;
  367. u32 conf;
  368. int port;
  369. memset(dt_val, MIIC_MODCTRL_CONF_NONE, sizeof(dt_val));
  370. if (of_property_read_u32(np, "renesas,miic-switch-portin", &conf) == 0)
  371. dt_val[0] = conf;
  372. for_each_child_of_node(np, conv) {
  373. if (of_property_read_u32(conv, "reg", &port))
  374. continue;
  375. if (!of_device_is_available(conv))
  376. continue;
  377. if (of_property_read_u32(conv, "renesas,miic-input", &conf) == 0)
  378. dt_val[port] = conf;
  379. }
  380. return miic_match_dt_conf(dev, dt_val, mode_cfg);
  381. }
  382. static int miic_probe(struct platform_device *pdev)
  383. {
  384. struct device *dev = &pdev->dev;
  385. struct miic *miic;
  386. u32 mode_cfg;
  387. int ret;
  388. ret = miic_parse_dt(dev, &mode_cfg);
  389. if (ret < 0)
  390. return ret;
  391. miic = devm_kzalloc(dev, sizeof(*miic), GFP_KERNEL);
  392. if (!miic)
  393. return -ENOMEM;
  394. spin_lock_init(&miic->lock);
  395. miic->dev = dev;
  396. miic->base = devm_platform_ioremap_resource(pdev, 0);
  397. if (IS_ERR(miic->base))
  398. return PTR_ERR(miic->base);
  399. ret = devm_pm_runtime_enable(dev);
  400. if (ret < 0)
  401. return ret;
  402. ret = pm_runtime_resume_and_get(dev);
  403. if (ret < 0)
  404. return ret;
  405. ret = miic_init_hw(miic, mode_cfg);
  406. if (ret)
  407. goto disable_runtime_pm;
  408. /* miic_create() relies on that fact that data are attached to the
  409. * platform device to determine if the driver is ready so this needs to
  410. * be the last thing to be done after everything is initialized
  411. * properly.
  412. */
  413. platform_set_drvdata(pdev, miic);
  414. return 0;
  415. disable_runtime_pm:
  416. pm_runtime_put(dev);
  417. return ret;
  418. }
  419. static int miic_remove(struct platform_device *pdev)
  420. {
  421. pm_runtime_put(&pdev->dev);
  422. return 0;
  423. }
  424. static const struct of_device_id miic_of_mtable[] = {
  425. { .compatible = "renesas,rzn1-miic" },
  426. { /* sentinel */ },
  427. };
  428. MODULE_DEVICE_TABLE(of, miic_of_mtable);
  429. static struct platform_driver miic_driver = {
  430. .driver = {
  431. .name = "rzn1_miic",
  432. .suppress_bind_attrs = true,
  433. .of_match_table = miic_of_mtable,
  434. },
  435. .probe = miic_probe,
  436. .remove = miic_remove,
  437. };
  438. module_platform_driver(miic_driver);
  439. MODULE_LICENSE("GPL");
  440. MODULE_DESCRIPTION("Renesas MII converter PCS driver");
  441. MODULE_AUTHOR("Clément Léger <[email protected]>");