772-v5.14-net-dsa-mt7530-add-interrupt-support.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. From ba751e28d44255744a30190faad0ca09b455c44d Mon Sep 17 00:00:00 2001
  2. From: DENG Qingfang <[email protected]>
  3. Date: Wed, 19 May 2021 11:32:00 +0800
  4. Subject: [PATCH] net: dsa: mt7530: add interrupt support
  5. Add support for MT7530 interrupt controller to handle internal PHYs.
  6. In order to assign an IRQ number to each PHY, the registration of MDIO bus
  7. is also done in this driver.
  8. Signed-off-by: DENG Qingfang <[email protected]>
  9. Reviewed-by: Andrew Lunn <[email protected]>
  10. Reviewed-by: Florian Fainelli <[email protected]>
  11. Reviewed-by: Vladimir Oltean <[email protected]>
  12. Signed-off-by: David S. Miller <[email protected]>
  13. ---
  14. drivers/net/dsa/mt7530.c | 264 +++++++++++++++++++++++++++++++++++----
  15. drivers/net/dsa/mt7530.h | 20 ++-
  16. 2 files changed, 256 insertions(+), 28 deletions(-)
  17. --- a/drivers/net/dsa/mt7530.c
  18. +++ b/drivers/net/dsa/mt7530.c
  19. @@ -10,6 +10,7 @@
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. +#include <linux/of_irq.h>
  24. #include <linux/of_mdio.h>
  25. #include <linux/of_net.h>
  26. #include <linux/of_platform.h>
  27. @@ -603,18 +604,14 @@ mt7530_mib_reset(struct dsa_switch *ds)
  28. mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE);
  29. }
  30. -static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum)
  31. +static int mt7530_phy_read(struct mt7530_priv *priv, int port, int regnum)
  32. {
  33. - struct mt7530_priv *priv = ds->priv;
  34. -
  35. return mdiobus_read_nested(priv->bus, port, regnum);
  36. }
  37. -static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum,
  38. +static int mt7530_phy_write(struct mt7530_priv *priv, int port, int regnum,
  39. u16 val)
  40. {
  41. - struct mt7530_priv *priv = ds->priv;
  42. -
  43. return mdiobus_write_nested(priv->bus, port, regnum, val);
  44. }
  45. @@ -792,9 +789,8 @@ out:
  46. }
  47. static int
  48. -mt7531_ind_phy_read(struct dsa_switch *ds, int port, int regnum)
  49. +mt7531_ind_phy_read(struct mt7530_priv *priv, int port, int regnum)
  50. {
  51. - struct mt7530_priv *priv = ds->priv;
  52. int devad;
  53. int ret;
  54. @@ -810,10 +806,9 @@ mt7531_ind_phy_read(struct dsa_switch *d
  55. }
  56. static int
  57. -mt7531_ind_phy_write(struct dsa_switch *ds, int port, int regnum,
  58. +mt7531_ind_phy_write(struct mt7530_priv *priv, int port, int regnum,
  59. u16 data)
  60. {
  61. - struct mt7530_priv *priv = ds->priv;
  62. int devad;
  63. int ret;
  64. @@ -829,6 +824,22 @@ mt7531_ind_phy_write(struct dsa_switch *
  65. return ret;
  66. }
  67. +static int
  68. +mt753x_phy_read(struct mii_bus *bus, int port, int regnum)
  69. +{
  70. + struct mt7530_priv *priv = bus->priv;
  71. +
  72. + return priv->info->phy_read(priv, port, regnum);
  73. +}
  74. +
  75. +static int
  76. +mt753x_phy_write(struct mii_bus *bus, int port, int regnum, u16 val)
  77. +{
  78. + struct mt7530_priv *priv = bus->priv;
  79. +
  80. + return priv->info->phy_write(priv, port, regnum, val);
  81. +}
  82. +
  83. static void
  84. mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  85. uint8_t *data)
  86. @@ -1796,6 +1807,210 @@ mt7530_setup_gpio(struct mt7530_priv *pr
  87. return devm_gpiochip_add_data(dev, gc, priv);
  88. }
  89. +static irqreturn_t
  90. +mt7530_irq_thread_fn(int irq, void *dev_id)
  91. +{
  92. + struct mt7530_priv *priv = dev_id;
  93. + bool handled = false;
  94. + u32 val;
  95. + int p;
  96. +
  97. + mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  98. + val = mt7530_mii_read(priv, MT7530_SYS_INT_STS);
  99. + mt7530_mii_write(priv, MT7530_SYS_INT_STS, val);
  100. + mutex_unlock(&priv->bus->mdio_lock);
  101. +
  102. + for (p = 0; p < MT7530_NUM_PHYS; p++) {
  103. + if (BIT(p) & val) {
  104. + unsigned int irq;
  105. +
  106. + irq = irq_find_mapping(priv->irq_domain, p);
  107. + handle_nested_irq(irq);
  108. + handled = true;
  109. + }
  110. + }
  111. +
  112. + return IRQ_RETVAL(handled);
  113. +}
  114. +
  115. +static void
  116. +mt7530_irq_mask(struct irq_data *d)
  117. +{
  118. + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  119. +
  120. + priv->irq_enable &= ~BIT(d->hwirq);
  121. +}
  122. +
  123. +static void
  124. +mt7530_irq_unmask(struct irq_data *d)
  125. +{
  126. + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  127. +
  128. + priv->irq_enable |= BIT(d->hwirq);
  129. +}
  130. +
  131. +static void
  132. +mt7530_irq_bus_lock(struct irq_data *d)
  133. +{
  134. + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  135. +
  136. + mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  137. +}
  138. +
  139. +static void
  140. +mt7530_irq_bus_sync_unlock(struct irq_data *d)
  141. +{
  142. + struct mt7530_priv *priv = irq_data_get_irq_chip_data(d);
  143. +
  144. + mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable);
  145. + mutex_unlock(&priv->bus->mdio_lock);
  146. +}
  147. +
  148. +static struct irq_chip mt7530_irq_chip = {
  149. + .name = KBUILD_MODNAME,
  150. + .irq_mask = mt7530_irq_mask,
  151. + .irq_unmask = mt7530_irq_unmask,
  152. + .irq_bus_lock = mt7530_irq_bus_lock,
  153. + .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock,
  154. +};
  155. +
  156. +static int
  157. +mt7530_irq_map(struct irq_domain *domain, unsigned int irq,
  158. + irq_hw_number_t hwirq)
  159. +{
  160. + irq_set_chip_data(irq, domain->host_data);
  161. + irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq);
  162. + irq_set_nested_thread(irq, true);
  163. + irq_set_noprobe(irq);
  164. +
  165. + return 0;
  166. +}
  167. +
  168. +static const struct irq_domain_ops mt7530_irq_domain_ops = {
  169. + .map = mt7530_irq_map,
  170. + .xlate = irq_domain_xlate_onecell,
  171. +};
  172. +
  173. +static void
  174. +mt7530_setup_mdio_irq(struct mt7530_priv *priv)
  175. +{
  176. + struct dsa_switch *ds = priv->ds;
  177. + int p;
  178. +
  179. + for (p = 0; p < MT7530_NUM_PHYS; p++) {
  180. + if (BIT(p) & ds->phys_mii_mask) {
  181. + unsigned int irq;
  182. +
  183. + irq = irq_create_mapping(priv->irq_domain, p);
  184. + ds->slave_mii_bus->irq[p] = irq;
  185. + }
  186. + }
  187. +}
  188. +
  189. +static int
  190. +mt7530_setup_irq(struct mt7530_priv *priv)
  191. +{
  192. + struct device *dev = priv->dev;
  193. + struct device_node *np = dev->of_node;
  194. + int ret;
  195. +
  196. + if (!of_property_read_bool(np, "interrupt-controller")) {
  197. + dev_info(dev, "no interrupt support\n");
  198. + return 0;
  199. + }
  200. +
  201. + priv->irq = of_irq_get(np, 0);
  202. + if (priv->irq <= 0) {
  203. + dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq);
  204. + return priv->irq ? : -EINVAL;
  205. + }
  206. +
  207. + priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS,
  208. + &mt7530_irq_domain_ops, priv);
  209. + if (!priv->irq_domain) {
  210. + dev_err(dev, "failed to create IRQ domain\n");
  211. + return -ENOMEM;
  212. + }
  213. +
  214. + /* This register must be set for MT7530 to properly fire interrupts */
  215. + if (priv->id != ID_MT7531)
  216. + mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL);
  217. +
  218. + ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn,
  219. + IRQF_ONESHOT, KBUILD_MODNAME, priv);
  220. + if (ret) {
  221. + irq_domain_remove(priv->irq_domain);
  222. + dev_err(dev, "failed to request IRQ: %d\n", ret);
  223. + return ret;
  224. + }
  225. +
  226. + return 0;
  227. +}
  228. +
  229. +static void
  230. +mt7530_free_mdio_irq(struct mt7530_priv *priv)
  231. +{
  232. + int p;
  233. +
  234. + for (p = 0; p < MT7530_NUM_PHYS; p++) {
  235. + if (BIT(p) & priv->ds->phys_mii_mask) {
  236. + unsigned int irq;
  237. +
  238. + irq = irq_find_mapping(priv->irq_domain, p);
  239. + irq_dispose_mapping(irq);
  240. + }
  241. + }
  242. +}
  243. +
  244. +static void
  245. +mt7530_free_irq_common(struct mt7530_priv *priv)
  246. +{
  247. + free_irq(priv->irq, priv);
  248. + irq_domain_remove(priv->irq_domain);
  249. +}
  250. +
  251. +static void
  252. +mt7530_free_irq(struct mt7530_priv *priv)
  253. +{
  254. + mt7530_free_mdio_irq(priv);
  255. + mt7530_free_irq_common(priv);
  256. +}
  257. +
  258. +static int
  259. +mt7530_setup_mdio(struct mt7530_priv *priv)
  260. +{
  261. + struct dsa_switch *ds = priv->ds;
  262. + struct device *dev = priv->dev;
  263. + struct mii_bus *bus;
  264. + static int idx;
  265. + int ret;
  266. +
  267. + bus = devm_mdiobus_alloc(dev);
  268. + if (!bus)
  269. + return -ENOMEM;
  270. +
  271. + ds->slave_mii_bus = bus;
  272. + bus->priv = priv;
  273. + bus->name = KBUILD_MODNAME "-mii";
  274. + snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++);
  275. + bus->read = mt753x_phy_read;
  276. + bus->write = mt753x_phy_write;
  277. + bus->parent = dev;
  278. + bus->phy_mask = ~ds->phys_mii_mask;
  279. +
  280. + if (priv->irq)
  281. + mt7530_setup_mdio_irq(priv);
  282. +
  283. + ret = devm_mdiobus_register(dev, bus);
  284. + if (ret) {
  285. + dev_err(dev, "failed to register MDIO bus: %d\n", ret);
  286. + if (priv->irq)
  287. + mt7530_free_mdio_irq(priv);
  288. + }
  289. +
  290. + return ret;
  291. +}
  292. +
  293. static int
  294. mt7530_setup(struct dsa_switch *ds)
  295. {
  296. @@ -2747,24 +2962,20 @@ static int
  297. mt753x_setup(struct dsa_switch *ds)
  298. {
  299. struct mt7530_priv *priv = ds->priv;
  300. + int ret = priv->info->sw_setup(ds);
  301. - return priv->info->sw_setup(ds);
  302. -}
  303. -
  304. -static int
  305. -mt753x_phy_read(struct dsa_switch *ds, int port, int regnum)
  306. -{
  307. - struct mt7530_priv *priv = ds->priv;
  308. + if (ret)
  309. + return ret;
  310. - return priv->info->phy_read(ds, port, regnum);
  311. -}
  312. + ret = mt7530_setup_irq(priv);
  313. + if (ret)
  314. + return ret;
  315. -static int
  316. -mt753x_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  317. -{
  318. - struct mt7530_priv *priv = ds->priv;
  319. + ret = mt7530_setup_mdio(priv);
  320. + if (ret && priv->irq)
  321. + mt7530_free_irq_common(priv);
  322. - return priv->info->phy_write(ds, port, regnum, val);
  323. + return ret;
  324. }
  325. static int mt753x_get_mac_eee(struct dsa_switch *ds, int port,
  326. @@ -2801,8 +3012,6 @@ static const struct dsa_switch_ops mt753
  327. .get_tag_protocol = mtk_get_tag_protocol,
  328. .setup = mt753x_setup,
  329. .get_strings = mt7530_get_strings,
  330. - .phy_read = mt753x_phy_read,
  331. - .phy_write = mt753x_phy_write,
  332. .get_ethtool_stats = mt7530_get_ethtool_stats,
  333. .get_sset_count = mt7530_get_sset_count,
  334. .set_ageing_time = mt7530_set_ageing_time,
  335. @@ -2985,6 +3194,9 @@ mt7530_remove(struct mdio_device *mdiode
  336. dev_err(priv->dev, "Failed to disable io pwr: %d\n",
  337. ret);
  338. + if (priv->irq)
  339. + mt7530_free_irq(priv);
  340. +
  341. dsa_unregister_switch(priv->ds);
  342. mutex_destroy(&priv->reg_mutex);
  343. }
  344. --- a/drivers/net/dsa/mt7530.h
  345. +++ b/drivers/net/dsa/mt7530.h
  346. @@ -7,6 +7,7 @@
  347. #define __MT7530_H
  348. #define MT7530_NUM_PORTS 7
  349. +#define MT7530_NUM_PHYS 5
  350. #define MT7530_CPU_PORT 6
  351. #define MT7530_NUM_FDB_RECORDS 2048
  352. #define MT7530_ALL_MEMBERS 0xff
  353. @@ -392,6 +393,12 @@ enum mt7531_sgmii_force_duplex {
  354. #define SYS_CTRL_SW_RST BIT(1)
  355. #define SYS_CTRL_REG_RST BIT(0)
  356. +/* Register for system interrupt */
  357. +#define MT7530_SYS_INT_EN 0x7008
  358. +
  359. +/* Register for system interrupt status */
  360. +#define MT7530_SYS_INT_STS 0x700c
  361. +
  362. /* Register for PHY Indirect Access Control */
  363. #define MT7531_PHY_IAC 0x701C
  364. #define MT7531_PHY_ACS_ST BIT(31)
  365. @@ -713,6 +720,8 @@ static const char *p5_intf_modes(unsigne
  366. }
  367. }
  368. +struct mt7530_priv;
  369. +
  370. /* struct mt753x_info - This is the main data structure for holding the specific
  371. * part for each supported device
  372. * @sw_setup: Holding the handler to a device initialization
  373. @@ -737,8 +746,8 @@ struct mt753x_info {
  374. enum mt753x_id id;
  375. int (*sw_setup)(struct dsa_switch *ds);
  376. - int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  377. - int (*phy_write)(struct dsa_switch *ds, int port, int regnum, u16 val);
  378. + int (*phy_read)(struct mt7530_priv *priv, int port, int regnum);
  379. + int (*phy_write)(struct mt7530_priv *priv, int port, int regnum, u16 val);
  380. int (*pad_setup)(struct dsa_switch *ds, phy_interface_t interface);
  381. int (*cpu_port_config)(struct dsa_switch *ds, int port);
  382. bool (*phy_mode_supported)(struct dsa_switch *ds, int port,
  383. @@ -772,6 +781,10 @@ struct mt753x_info {
  384. * registers
  385. * @p6_interface Holding the current port 6 interface
  386. * @p5_intf_sel: Holding the current port 5 interface select
  387. + *
  388. + * @irq: IRQ number of the switch
  389. + * @irq_domain: IRQ domain of the switch irq_chip
  390. + * @irq_enable: IRQ enable bits, synced to SYS_INT_EN
  391. */
  392. struct mt7530_priv {
  393. struct device *dev;
  394. @@ -793,6 +806,9 @@ struct mt7530_priv {
  395. struct mt7530_port ports[MT7530_NUM_PORTS];
  396. /* protect among processes for registers access*/
  397. struct mutex reg_mutex;
  398. + int irq;
  399. + struct irq_domain *irq_domain;
  400. + u32 irq_enable;
  401. };
  402. struct mt7530_hw_vlan_entry {