ar9331.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2019 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  3. /*
  4. * +----------------------+
  5. * GMAC1----RGMII----|--MAC0 |
  6. * \---MDIO1----|--REGs |----MDIO3----\
  7. * | | | +------+
  8. * | | +--| |
  9. * | MAC1-|----RMII--M-----| PHY0 |-o P0
  10. * | | | | +------+
  11. * | | | +--| |
  12. * | MAC2-|----RMII--------| PHY1 |-o P1
  13. * | | | | +------+
  14. * | | | +--| |
  15. * | MAC3-|----RMII--------| PHY2 |-o P2
  16. * | | | | +------+
  17. * | | | +--| |
  18. * | MAC4-|----RMII--------| PHY3 |-o P3
  19. * | | | | +------+
  20. * | | | +--| |
  21. * | MAC5-|--+-RMII--M-----|-PHY4-|-o P4
  22. * | | | | +------+
  23. * +----------------------+ | \--CFG_SW_PHY_SWAP
  24. * GMAC0---------------RMII--------------------/ \-CFG_SW_PHY_ADDR_SWAP
  25. * \---MDIO0--NC
  26. *
  27. * GMAC0 and MAC5 are connected together and use same PHY. Depending on
  28. * configuration it can be PHY4 (default) or PHY0. Only GMAC0 or MAC5 can be
  29. * used at same time. If GMAC0 is used (default) then MAC5 should be disabled.
  30. *
  31. * CFG_SW_PHY_SWAP - swap connections of PHY0 and PHY4. If this bit is not set
  32. * PHY4 is connected to GMAC0/MAC5 bundle and PHY0 is connected to MAC1. If this
  33. * bit is set, PHY4 is connected to MAC1 and PHY0 is connected to GMAC0/MAC5
  34. * bundle.
  35. *
  36. * CFG_SW_PHY_ADDR_SWAP - swap addresses of PHY0 and PHY4
  37. *
  38. * CFG_SW_PHY_SWAP and CFG_SW_PHY_ADDR_SWAP are part of SoC specific register
  39. * set and not related to switch internal registers.
  40. */
  41. #include <linux/bitfield.h>
  42. #include <linux/module.h>
  43. #include <linux/of_irq.h>
  44. #include <linux/of_mdio.h>
  45. #include <linux/regmap.h>
  46. #include <linux/reset.h>
  47. #include <net/dsa.h>
  48. #define AR9331_SW_NAME "ar9331_switch"
  49. #define AR9331_SW_PORTS 6
  50. /* dummy reg to change page */
  51. #define AR9331_SW_REG_PAGE 0x40000
  52. /* Global Interrupt */
  53. #define AR9331_SW_REG_GINT 0x10
  54. #define AR9331_SW_REG_GINT_MASK 0x14
  55. #define AR9331_SW_GINT_PHY_INT BIT(2)
  56. #define AR9331_SW_REG_FLOOD_MASK 0x2c
  57. #define AR9331_SW_FLOOD_MASK_BROAD_TO_CPU BIT(26)
  58. #define AR9331_SW_REG_GLOBAL_CTRL 0x30
  59. #define AR9331_SW_GLOBAL_CTRL_MFS_M GENMASK(13, 0)
  60. #define AR9331_SW_REG_MDIO_CTRL 0x98
  61. #define AR9331_SW_MDIO_CTRL_BUSY BIT(31)
  62. #define AR9331_SW_MDIO_CTRL_MASTER_EN BIT(30)
  63. #define AR9331_SW_MDIO_CTRL_CMD_READ BIT(27)
  64. #define AR9331_SW_MDIO_CTRL_PHY_ADDR_M GENMASK(25, 21)
  65. #define AR9331_SW_MDIO_CTRL_REG_ADDR_M GENMASK(20, 16)
  66. #define AR9331_SW_MDIO_CTRL_DATA_M GENMASK(16, 0)
  67. #define AR9331_SW_REG_PORT_STATUS(_port) (0x100 + (_port) * 0x100)
  68. /* FLOW_LINK_EN - enable mac flow control config auto-neg with phy.
  69. * If not set, mac can be config by software.
  70. */
  71. #define AR9331_SW_PORT_STATUS_FLOW_LINK_EN BIT(12)
  72. /* LINK_EN - If set, MAC is configured from PHY link status.
  73. * If not set, MAC should be configured by software.
  74. */
  75. #define AR9331_SW_PORT_STATUS_LINK_EN BIT(9)
  76. #define AR9331_SW_PORT_STATUS_DUPLEX_MODE BIT(6)
  77. #define AR9331_SW_PORT_STATUS_RX_FLOW_EN BIT(5)
  78. #define AR9331_SW_PORT_STATUS_TX_FLOW_EN BIT(4)
  79. #define AR9331_SW_PORT_STATUS_RXMAC BIT(3)
  80. #define AR9331_SW_PORT_STATUS_TXMAC BIT(2)
  81. #define AR9331_SW_PORT_STATUS_SPEED_M GENMASK(1, 0)
  82. #define AR9331_SW_PORT_STATUS_SPEED_1000 2
  83. #define AR9331_SW_PORT_STATUS_SPEED_100 1
  84. #define AR9331_SW_PORT_STATUS_SPEED_10 0
  85. #define AR9331_SW_PORT_STATUS_MAC_MASK \
  86. (AR9331_SW_PORT_STATUS_TXMAC | AR9331_SW_PORT_STATUS_RXMAC)
  87. #define AR9331_SW_PORT_STATUS_LINK_MASK \
  88. (AR9331_SW_PORT_STATUS_DUPLEX_MODE | \
  89. AR9331_SW_PORT_STATUS_RX_FLOW_EN | AR9331_SW_PORT_STATUS_TX_FLOW_EN | \
  90. AR9331_SW_PORT_STATUS_SPEED_M)
  91. #define AR9331_SW_REG_PORT_CTRL(_port) (0x104 + (_port) * 0x100)
  92. #define AR9331_SW_PORT_CTRL_HEAD_EN BIT(11)
  93. #define AR9331_SW_PORT_CTRL_PORT_STATE GENMASK(2, 0)
  94. #define AR9331_SW_PORT_CTRL_PORT_STATE_DISABLED 0
  95. #define AR9331_SW_PORT_CTRL_PORT_STATE_BLOCKING 1
  96. #define AR9331_SW_PORT_CTRL_PORT_STATE_LISTENING 2
  97. #define AR9331_SW_PORT_CTRL_PORT_STATE_LEARNING 3
  98. #define AR9331_SW_PORT_CTRL_PORT_STATE_FORWARD 4
  99. #define AR9331_SW_REG_PORT_VLAN(_port) (0x108 + (_port) * 0x100)
  100. #define AR9331_SW_PORT_VLAN_8021Q_MODE GENMASK(31, 30)
  101. #define AR9331_SW_8021Q_MODE_SECURE 3
  102. #define AR9331_SW_8021Q_MODE_CHECK 2
  103. #define AR9331_SW_8021Q_MODE_FALLBACK 1
  104. #define AR9331_SW_8021Q_MODE_NONE 0
  105. #define AR9331_SW_PORT_VLAN_PORT_VID_MEMBER GENMASK(25, 16)
  106. /* MIB registers */
  107. #define AR9331_MIB_COUNTER(x) (0x20000 + ((x) * 0x100))
  108. /* Phy bypass mode
  109. * ------------------------------------------------------------------------
  110. * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
  111. *
  112. * real | start | OP | PhyAddr | Reg Addr | TA |
  113. * atheros| start | OP | 2'b00 |PhyAdd[2:0]| Reg Addr[4:0] | TA |
  114. *
  115. *
  116. * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
  117. * real | Data |
  118. * atheros| Data |
  119. *
  120. * ------------------------------------------------------------------------
  121. * Page address mode
  122. * ------------------------------------------------------------------------
  123. * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
  124. * real | start | OP | PhyAddr | Reg Addr | TA |
  125. * atheros| start | OP | 2'b11 | 8'b0 | TA |
  126. *
  127. * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
  128. * real | Data |
  129. * atheros| | Page [9:0] |
  130. */
  131. /* In case of Page Address mode, Bit[18:9] of 32 bit register address should be
  132. * written to bits[9:0] of mdio data register.
  133. */
  134. #define AR9331_SW_ADDR_PAGE GENMASK(18, 9)
  135. /* ------------------------------------------------------------------------
  136. * Normal register access mode
  137. * ------------------------------------------------------------------------
  138. * Bit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |
  139. * real | start | OP | PhyAddr | Reg Addr | TA |
  140. * atheros| start | OP | 2'b10 | low_addr[7:0] | TA |
  141. *
  142. * Bit: |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
  143. * real | Data |
  144. * atheros| Data |
  145. * ------------------------------------------------------------------------
  146. */
  147. #define AR9331_SW_LOW_ADDR_PHY GENMASK(8, 6)
  148. #define AR9331_SW_LOW_ADDR_REG GENMASK(5, 1)
  149. #define AR9331_SW_MDIO_PHY_MODE_M GENMASK(4, 3)
  150. #define AR9331_SW_MDIO_PHY_MODE_PAGE 3
  151. #define AR9331_SW_MDIO_PHY_MODE_REG 2
  152. #define AR9331_SW_MDIO_PHY_MODE_BYPASS 0
  153. #define AR9331_SW_MDIO_PHY_ADDR_M GENMASK(2, 0)
  154. /* Empirical determined values */
  155. #define AR9331_SW_MDIO_POLL_SLEEP_US 1
  156. #define AR9331_SW_MDIO_POLL_TIMEOUT_US 20
  157. /* The interval should be small enough to avoid overflow of 32bit MIBs */
  158. /*
  159. * FIXME: until we can read MIBs from stats64 call directly (i.e. sleep
  160. * there), we have to poll stats more frequently then it is actually needed.
  161. * For overflow protection, normally, 100 sec interval should have been OK.
  162. */
  163. #define STATS_INTERVAL_JIFFIES (3 * HZ)
  164. struct ar9331_sw_stats_raw {
  165. u32 rxbroad; /* 0x00 */
  166. u32 rxpause; /* 0x04 */
  167. u32 rxmulti; /* 0x08 */
  168. u32 rxfcserr; /* 0x0c */
  169. u32 rxalignerr; /* 0x10 */
  170. u32 rxrunt; /* 0x14 */
  171. u32 rxfragment; /* 0x18 */
  172. u32 rx64byte; /* 0x1c */
  173. u32 rx128byte; /* 0x20 */
  174. u32 rx256byte; /* 0x24 */
  175. u32 rx512byte; /* 0x28 */
  176. u32 rx1024byte; /* 0x2c */
  177. u32 rx1518byte; /* 0x30 */
  178. u32 rxmaxbyte; /* 0x34 */
  179. u32 rxtoolong; /* 0x38 */
  180. u32 rxgoodbyte; /* 0x3c */
  181. u32 rxgoodbyte_hi;
  182. u32 rxbadbyte; /* 0x44 */
  183. u32 rxbadbyte_hi;
  184. u32 rxoverflow; /* 0x4c */
  185. u32 filtered; /* 0x50 */
  186. u32 txbroad; /* 0x54 */
  187. u32 txpause; /* 0x58 */
  188. u32 txmulti; /* 0x5c */
  189. u32 txunderrun; /* 0x60 */
  190. u32 tx64byte; /* 0x64 */
  191. u32 tx128byte; /* 0x68 */
  192. u32 tx256byte; /* 0x6c */
  193. u32 tx512byte; /* 0x70 */
  194. u32 tx1024byte; /* 0x74 */
  195. u32 tx1518byte; /* 0x78 */
  196. u32 txmaxbyte; /* 0x7c */
  197. u32 txoversize; /* 0x80 */
  198. u32 txbyte; /* 0x84 */
  199. u32 txbyte_hi;
  200. u32 txcollision; /* 0x8c */
  201. u32 txabortcol; /* 0x90 */
  202. u32 txmulticol; /* 0x94 */
  203. u32 txsinglecol; /* 0x98 */
  204. u32 txexcdefer; /* 0x9c */
  205. u32 txdefer; /* 0xa0 */
  206. u32 txlatecol; /* 0xa4 */
  207. };
  208. struct ar9331_sw_port {
  209. int idx;
  210. struct delayed_work mib_read;
  211. struct rtnl_link_stats64 stats;
  212. struct ethtool_pause_stats pause_stats;
  213. struct spinlock stats_lock;
  214. };
  215. struct ar9331_sw_priv {
  216. struct device *dev;
  217. struct dsa_switch ds;
  218. struct dsa_switch_ops ops;
  219. struct irq_domain *irqdomain;
  220. u32 irq_mask;
  221. struct mutex lock_irq;
  222. struct mii_bus *mbus; /* mdio master */
  223. struct mii_bus *sbus; /* mdio slave */
  224. struct regmap *regmap;
  225. struct reset_control *sw_reset;
  226. struct ar9331_sw_port port[AR9331_SW_PORTS];
  227. };
  228. static struct ar9331_sw_priv *ar9331_sw_port_to_priv(struct ar9331_sw_port *port)
  229. {
  230. struct ar9331_sw_port *p = port - port->idx;
  231. return (struct ar9331_sw_priv *)((void *)p -
  232. offsetof(struct ar9331_sw_priv, port));
  233. }
  234. /* Warning: switch reset will reset last AR9331_SW_MDIO_PHY_MODE_PAGE request
  235. * If some kind of optimization is used, the request should be repeated.
  236. */
  237. static int ar9331_sw_reset(struct ar9331_sw_priv *priv)
  238. {
  239. int ret;
  240. ret = reset_control_assert(priv->sw_reset);
  241. if (ret)
  242. goto error;
  243. /* AR9331 doc do not provide any information about proper reset
  244. * sequence. The AR8136 (the closes switch to the AR9331) doc says:
  245. * reset duration should be greater than 10ms. So, let's use this value
  246. * for now.
  247. */
  248. usleep_range(10000, 15000);
  249. ret = reset_control_deassert(priv->sw_reset);
  250. if (ret)
  251. goto error;
  252. /* There is no information on how long should we wait after reset.
  253. * AR8136 has an EEPROM and there is an Interrupt for EEPROM load
  254. * status. AR9331 has no EEPROM support.
  255. * For now, do not wait. In case AR8136 will be needed, the after
  256. * reset delay can be added as well.
  257. */
  258. return 0;
  259. error:
  260. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  261. return ret;
  262. }
  263. static int ar9331_sw_mbus_write(struct mii_bus *mbus, int port, int regnum,
  264. u16 data)
  265. {
  266. struct ar9331_sw_priv *priv = mbus->priv;
  267. struct regmap *regmap = priv->regmap;
  268. u32 val;
  269. int ret;
  270. ret = regmap_write(regmap, AR9331_SW_REG_MDIO_CTRL,
  271. AR9331_SW_MDIO_CTRL_BUSY |
  272. AR9331_SW_MDIO_CTRL_MASTER_EN |
  273. FIELD_PREP(AR9331_SW_MDIO_CTRL_PHY_ADDR_M, port) |
  274. FIELD_PREP(AR9331_SW_MDIO_CTRL_REG_ADDR_M, regnum) |
  275. FIELD_PREP(AR9331_SW_MDIO_CTRL_DATA_M, data));
  276. if (ret)
  277. goto error;
  278. ret = regmap_read_poll_timeout(regmap, AR9331_SW_REG_MDIO_CTRL, val,
  279. !(val & AR9331_SW_MDIO_CTRL_BUSY),
  280. AR9331_SW_MDIO_POLL_SLEEP_US,
  281. AR9331_SW_MDIO_POLL_TIMEOUT_US);
  282. if (ret)
  283. goto error;
  284. return 0;
  285. error:
  286. dev_err_ratelimited(priv->dev, "PHY write error: %i\n", ret);
  287. return ret;
  288. }
  289. static int ar9331_sw_mbus_read(struct mii_bus *mbus, int port, int regnum)
  290. {
  291. struct ar9331_sw_priv *priv = mbus->priv;
  292. struct regmap *regmap = priv->regmap;
  293. u32 val;
  294. int ret;
  295. ret = regmap_write(regmap, AR9331_SW_REG_MDIO_CTRL,
  296. AR9331_SW_MDIO_CTRL_BUSY |
  297. AR9331_SW_MDIO_CTRL_MASTER_EN |
  298. AR9331_SW_MDIO_CTRL_CMD_READ |
  299. FIELD_PREP(AR9331_SW_MDIO_CTRL_PHY_ADDR_M, port) |
  300. FIELD_PREP(AR9331_SW_MDIO_CTRL_REG_ADDR_M, regnum));
  301. if (ret)
  302. goto error;
  303. ret = regmap_read_poll_timeout(regmap, AR9331_SW_REG_MDIO_CTRL, val,
  304. !(val & AR9331_SW_MDIO_CTRL_BUSY),
  305. AR9331_SW_MDIO_POLL_SLEEP_US,
  306. AR9331_SW_MDIO_POLL_TIMEOUT_US);
  307. if (ret)
  308. goto error;
  309. ret = regmap_read(regmap, AR9331_SW_REG_MDIO_CTRL, &val);
  310. if (ret)
  311. goto error;
  312. return FIELD_GET(AR9331_SW_MDIO_CTRL_DATA_M, val);
  313. error:
  314. dev_err_ratelimited(priv->dev, "PHY read error: %i\n", ret);
  315. return ret;
  316. }
  317. static int ar9331_sw_mbus_init(struct ar9331_sw_priv *priv)
  318. {
  319. struct device *dev = priv->dev;
  320. struct mii_bus *mbus;
  321. struct device_node *np, *mnp;
  322. int ret;
  323. np = dev->of_node;
  324. mbus = devm_mdiobus_alloc(dev);
  325. if (!mbus)
  326. return -ENOMEM;
  327. mbus->name = np->full_name;
  328. snprintf(mbus->id, MII_BUS_ID_SIZE, "%pOF", np);
  329. mbus->read = ar9331_sw_mbus_read;
  330. mbus->write = ar9331_sw_mbus_write;
  331. mbus->priv = priv;
  332. mbus->parent = dev;
  333. mnp = of_get_child_by_name(np, "mdio");
  334. if (!mnp)
  335. return -ENODEV;
  336. ret = devm_of_mdiobus_register(dev, mbus, mnp);
  337. of_node_put(mnp);
  338. if (ret)
  339. return ret;
  340. priv->mbus = mbus;
  341. return 0;
  342. }
  343. static int ar9331_sw_setup_port(struct dsa_switch *ds, int port)
  344. {
  345. struct ar9331_sw_priv *priv = ds->priv;
  346. struct regmap *regmap = priv->regmap;
  347. u32 port_mask, port_ctrl, val;
  348. int ret;
  349. /* Generate default port settings */
  350. port_ctrl = FIELD_PREP(AR9331_SW_PORT_CTRL_PORT_STATE,
  351. AR9331_SW_PORT_CTRL_PORT_STATE_FORWARD);
  352. if (dsa_is_cpu_port(ds, port)) {
  353. /* CPU port should be allowed to communicate with all user
  354. * ports.
  355. */
  356. port_mask = dsa_user_ports(ds);
  357. /* Enable Atheros header on CPU port. This will allow us
  358. * communicate with each port separately
  359. */
  360. port_ctrl |= AR9331_SW_PORT_CTRL_HEAD_EN;
  361. } else if (dsa_is_user_port(ds, port)) {
  362. /* User ports should communicate only with the CPU port.
  363. */
  364. port_mask = BIT(dsa_upstream_port(ds, port));
  365. } else {
  366. /* Other ports do not need to communicate at all */
  367. port_mask = 0;
  368. }
  369. val = FIELD_PREP(AR9331_SW_PORT_VLAN_8021Q_MODE,
  370. AR9331_SW_8021Q_MODE_NONE) |
  371. FIELD_PREP(AR9331_SW_PORT_VLAN_PORT_VID_MEMBER, port_mask);
  372. ret = regmap_write(regmap, AR9331_SW_REG_PORT_VLAN(port), val);
  373. if (ret)
  374. goto error;
  375. ret = regmap_write(regmap, AR9331_SW_REG_PORT_CTRL(port), port_ctrl);
  376. if (ret)
  377. goto error;
  378. return 0;
  379. error:
  380. dev_err(priv->dev, "%s: error: %i\n", __func__, ret);
  381. return ret;
  382. }
  383. static int ar9331_sw_setup(struct dsa_switch *ds)
  384. {
  385. struct ar9331_sw_priv *priv = ds->priv;
  386. struct regmap *regmap = priv->regmap;
  387. int ret, i;
  388. ret = ar9331_sw_reset(priv);
  389. if (ret)
  390. return ret;
  391. /* Reset will set proper defaults. CPU - Port0 will be enabled and
  392. * configured. All other ports (ports 1 - 5) are disabled
  393. */
  394. ret = ar9331_sw_mbus_init(priv);
  395. if (ret)
  396. return ret;
  397. /* Do not drop broadcast frames */
  398. ret = regmap_write_bits(regmap, AR9331_SW_REG_FLOOD_MASK,
  399. AR9331_SW_FLOOD_MASK_BROAD_TO_CPU,
  400. AR9331_SW_FLOOD_MASK_BROAD_TO_CPU);
  401. if (ret)
  402. goto error;
  403. /* Set max frame size to the maximum supported value */
  404. ret = regmap_write_bits(regmap, AR9331_SW_REG_GLOBAL_CTRL,
  405. AR9331_SW_GLOBAL_CTRL_MFS_M,
  406. AR9331_SW_GLOBAL_CTRL_MFS_M);
  407. if (ret)
  408. goto error;
  409. for (i = 0; i < ds->num_ports; i++) {
  410. ret = ar9331_sw_setup_port(ds, i);
  411. if (ret)
  412. goto error;
  413. }
  414. ds->configure_vlan_while_not_filtering = false;
  415. return 0;
  416. error:
  417. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  418. return ret;
  419. }
  420. static void ar9331_sw_port_disable(struct dsa_switch *ds, int port)
  421. {
  422. struct ar9331_sw_priv *priv = ds->priv;
  423. struct regmap *regmap = priv->regmap;
  424. int ret;
  425. ret = regmap_write(regmap, AR9331_SW_REG_PORT_STATUS(port), 0);
  426. if (ret)
  427. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  428. }
  429. static enum dsa_tag_protocol ar9331_sw_get_tag_protocol(struct dsa_switch *ds,
  430. int port,
  431. enum dsa_tag_protocol m)
  432. {
  433. return DSA_TAG_PROTO_AR9331;
  434. }
  435. static void ar9331_sw_phylink_get_caps(struct dsa_switch *ds, int port,
  436. struct phylink_config *config)
  437. {
  438. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  439. MAC_10 | MAC_100;
  440. switch (port) {
  441. case 0:
  442. __set_bit(PHY_INTERFACE_MODE_GMII,
  443. config->supported_interfaces);
  444. config->mac_capabilities |= MAC_1000;
  445. break;
  446. case 1:
  447. case 2:
  448. case 3:
  449. case 4:
  450. case 5:
  451. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  452. config->supported_interfaces);
  453. break;
  454. }
  455. }
  456. static void ar9331_sw_phylink_mac_config(struct phylink_config *config,
  457. unsigned int mode,
  458. const struct phylink_link_state *state)
  459. {
  460. struct dsa_port *dp = dsa_phylink_to_port(config);
  461. struct ar9331_sw_priv *priv = dp->ds->priv;
  462. struct regmap *regmap = priv->regmap;
  463. int ret;
  464. ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(dp->index),
  465. AR9331_SW_PORT_STATUS_LINK_EN |
  466. AR9331_SW_PORT_STATUS_FLOW_LINK_EN, 0);
  467. if (ret)
  468. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  469. }
  470. static void ar9331_sw_phylink_mac_link_down(struct phylink_config *config,
  471. unsigned int mode,
  472. phy_interface_t interface)
  473. {
  474. struct dsa_port *dp = dsa_phylink_to_port(config);
  475. struct ar9331_sw_priv *priv = dp->ds->priv;
  476. struct regmap *regmap = priv->regmap;
  477. int port = dp->index;
  478. int ret;
  479. ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(port),
  480. AR9331_SW_PORT_STATUS_MAC_MASK, 0);
  481. if (ret)
  482. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  483. cancel_delayed_work_sync(&priv->port[port].mib_read);
  484. }
  485. static void ar9331_sw_phylink_mac_link_up(struct phylink_config *config,
  486. struct phy_device *phydev,
  487. unsigned int mode,
  488. phy_interface_t interface,
  489. int speed, int duplex,
  490. bool tx_pause, bool rx_pause)
  491. {
  492. struct dsa_port *dp = dsa_phylink_to_port(config);
  493. struct ar9331_sw_priv *priv = dp->ds->priv;
  494. struct regmap *regmap = priv->regmap;
  495. int port = dp->index;
  496. u32 val;
  497. int ret;
  498. schedule_delayed_work(&priv->port[port].mib_read, 0);
  499. val = AR9331_SW_PORT_STATUS_MAC_MASK;
  500. switch (speed) {
  501. case SPEED_1000:
  502. val |= AR9331_SW_PORT_STATUS_SPEED_1000;
  503. break;
  504. case SPEED_100:
  505. val |= AR9331_SW_PORT_STATUS_SPEED_100;
  506. break;
  507. case SPEED_10:
  508. val |= AR9331_SW_PORT_STATUS_SPEED_10;
  509. break;
  510. default:
  511. return;
  512. }
  513. if (duplex)
  514. val |= AR9331_SW_PORT_STATUS_DUPLEX_MODE;
  515. if (tx_pause)
  516. val |= AR9331_SW_PORT_STATUS_TX_FLOW_EN;
  517. if (rx_pause)
  518. val |= AR9331_SW_PORT_STATUS_RX_FLOW_EN;
  519. ret = regmap_update_bits(regmap, AR9331_SW_REG_PORT_STATUS(port),
  520. AR9331_SW_PORT_STATUS_MAC_MASK |
  521. AR9331_SW_PORT_STATUS_LINK_MASK,
  522. val);
  523. if (ret)
  524. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  525. }
  526. static void ar9331_read_stats(struct ar9331_sw_port *port)
  527. {
  528. struct ar9331_sw_priv *priv = ar9331_sw_port_to_priv(port);
  529. struct ethtool_pause_stats *pstats = &port->pause_stats;
  530. struct rtnl_link_stats64 *stats = &port->stats;
  531. struct ar9331_sw_stats_raw raw;
  532. int ret;
  533. /* Do the slowest part first, to avoid needless locking for long time */
  534. ret = regmap_bulk_read(priv->regmap, AR9331_MIB_COUNTER(port->idx),
  535. &raw, sizeof(raw) / sizeof(u32));
  536. if (ret) {
  537. dev_err_ratelimited(priv->dev, "%s: %i\n", __func__, ret);
  538. return;
  539. }
  540. /* All MIB counters are cleared automatically on read */
  541. spin_lock(&port->stats_lock);
  542. stats->rx_bytes += raw.rxgoodbyte;
  543. stats->tx_bytes += raw.txbyte;
  544. stats->rx_packets += raw.rx64byte + raw.rx128byte + raw.rx256byte +
  545. raw.rx512byte + raw.rx1024byte + raw.rx1518byte + raw.rxmaxbyte;
  546. stats->tx_packets += raw.tx64byte + raw.tx128byte + raw.tx256byte +
  547. raw.tx512byte + raw.tx1024byte + raw.tx1518byte + raw.txmaxbyte;
  548. stats->rx_length_errors += raw.rxrunt + raw.rxfragment + raw.rxtoolong;
  549. stats->rx_crc_errors += raw.rxfcserr;
  550. stats->rx_frame_errors += raw.rxalignerr;
  551. stats->rx_missed_errors += raw.rxoverflow;
  552. stats->rx_dropped += raw.filtered;
  553. stats->rx_errors += raw.rxfcserr + raw.rxalignerr + raw.rxrunt +
  554. raw.rxfragment + raw.rxoverflow + raw.rxtoolong;
  555. stats->tx_window_errors += raw.txlatecol;
  556. stats->tx_fifo_errors += raw.txunderrun;
  557. stats->tx_aborted_errors += raw.txabortcol;
  558. stats->tx_errors += raw.txoversize + raw.txabortcol + raw.txunderrun +
  559. raw.txlatecol;
  560. stats->multicast += raw.rxmulti;
  561. stats->collisions += raw.txcollision;
  562. pstats->tx_pause_frames += raw.txpause;
  563. pstats->rx_pause_frames += raw.rxpause;
  564. spin_unlock(&port->stats_lock);
  565. }
  566. static void ar9331_do_stats_poll(struct work_struct *work)
  567. {
  568. struct ar9331_sw_port *port = container_of(work, struct ar9331_sw_port,
  569. mib_read.work);
  570. ar9331_read_stats(port);
  571. schedule_delayed_work(&port->mib_read, STATS_INTERVAL_JIFFIES);
  572. }
  573. static void ar9331_get_stats64(struct dsa_switch *ds, int port,
  574. struct rtnl_link_stats64 *s)
  575. {
  576. struct ar9331_sw_priv *priv = ds->priv;
  577. struct ar9331_sw_port *p = &priv->port[port];
  578. spin_lock(&p->stats_lock);
  579. memcpy(s, &p->stats, sizeof(*s));
  580. spin_unlock(&p->stats_lock);
  581. }
  582. static void ar9331_get_pause_stats(struct dsa_switch *ds, int port,
  583. struct ethtool_pause_stats *pause_stats)
  584. {
  585. struct ar9331_sw_priv *priv = ds->priv;
  586. struct ar9331_sw_port *p = &priv->port[port];
  587. spin_lock(&p->stats_lock);
  588. memcpy(pause_stats, &p->pause_stats, sizeof(*pause_stats));
  589. spin_unlock(&p->stats_lock);
  590. }
  591. static const struct phylink_mac_ops ar9331_phylink_mac_ops = {
  592. .mac_config = ar9331_sw_phylink_mac_config,
  593. .mac_link_down = ar9331_sw_phylink_mac_link_down,
  594. .mac_link_up = ar9331_sw_phylink_mac_link_up,
  595. };
  596. static const struct dsa_switch_ops ar9331_sw_ops = {
  597. .get_tag_protocol = ar9331_sw_get_tag_protocol,
  598. .setup = ar9331_sw_setup,
  599. .port_disable = ar9331_sw_port_disable,
  600. .phylink_get_caps = ar9331_sw_phylink_get_caps,
  601. .get_stats64 = ar9331_get_stats64,
  602. .get_pause_stats = ar9331_get_pause_stats,
  603. };
  604. static irqreturn_t ar9331_sw_irq(int irq, void *data)
  605. {
  606. struct ar9331_sw_priv *priv = data;
  607. struct regmap *regmap = priv->regmap;
  608. u32 stat;
  609. int ret;
  610. ret = regmap_read(regmap, AR9331_SW_REG_GINT, &stat);
  611. if (ret) {
  612. dev_err(priv->dev, "can't read interrupt status\n");
  613. return IRQ_NONE;
  614. }
  615. if (!stat)
  616. return IRQ_NONE;
  617. if (stat & AR9331_SW_GINT_PHY_INT) {
  618. int child_irq;
  619. child_irq = irq_find_mapping(priv->irqdomain, 0);
  620. handle_nested_irq(child_irq);
  621. }
  622. ret = regmap_write(regmap, AR9331_SW_REG_GINT, stat);
  623. if (ret) {
  624. dev_err(priv->dev, "can't write interrupt status\n");
  625. return IRQ_NONE;
  626. }
  627. return IRQ_HANDLED;
  628. }
  629. static void ar9331_sw_mask_irq(struct irq_data *d)
  630. {
  631. struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
  632. priv->irq_mask = 0;
  633. }
  634. static void ar9331_sw_unmask_irq(struct irq_data *d)
  635. {
  636. struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
  637. priv->irq_mask = AR9331_SW_GINT_PHY_INT;
  638. }
  639. static void ar9331_sw_irq_bus_lock(struct irq_data *d)
  640. {
  641. struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
  642. mutex_lock(&priv->lock_irq);
  643. }
  644. static void ar9331_sw_irq_bus_sync_unlock(struct irq_data *d)
  645. {
  646. struct ar9331_sw_priv *priv = irq_data_get_irq_chip_data(d);
  647. struct regmap *regmap = priv->regmap;
  648. int ret;
  649. ret = regmap_update_bits(regmap, AR9331_SW_REG_GINT_MASK,
  650. AR9331_SW_GINT_PHY_INT, priv->irq_mask);
  651. if (ret)
  652. dev_err(priv->dev, "failed to change IRQ mask\n");
  653. mutex_unlock(&priv->lock_irq);
  654. }
  655. static struct irq_chip ar9331_sw_irq_chip = {
  656. .name = AR9331_SW_NAME,
  657. .irq_mask = ar9331_sw_mask_irq,
  658. .irq_unmask = ar9331_sw_unmask_irq,
  659. .irq_bus_lock = ar9331_sw_irq_bus_lock,
  660. .irq_bus_sync_unlock = ar9331_sw_irq_bus_sync_unlock,
  661. };
  662. static int ar9331_sw_irq_map(struct irq_domain *domain, unsigned int irq,
  663. irq_hw_number_t hwirq)
  664. {
  665. irq_set_chip_data(irq, domain->host_data);
  666. irq_set_chip_and_handler(irq, &ar9331_sw_irq_chip, handle_simple_irq);
  667. irq_set_nested_thread(irq, 1);
  668. irq_set_noprobe(irq);
  669. return 0;
  670. }
  671. static void ar9331_sw_irq_unmap(struct irq_domain *d, unsigned int irq)
  672. {
  673. irq_set_nested_thread(irq, 0);
  674. irq_set_chip_and_handler(irq, NULL, NULL);
  675. irq_set_chip_data(irq, NULL);
  676. }
  677. static const struct irq_domain_ops ar9331_sw_irqdomain_ops = {
  678. .map = ar9331_sw_irq_map,
  679. .unmap = ar9331_sw_irq_unmap,
  680. .xlate = irq_domain_xlate_onecell,
  681. };
  682. static int ar9331_sw_irq_init(struct ar9331_sw_priv *priv)
  683. {
  684. struct device_node *np = priv->dev->of_node;
  685. struct device *dev = priv->dev;
  686. int ret, irq;
  687. irq = of_irq_get(np, 0);
  688. if (irq <= 0) {
  689. dev_err(dev, "failed to get parent IRQ\n");
  690. return irq ? irq : -EINVAL;
  691. }
  692. mutex_init(&priv->lock_irq);
  693. ret = devm_request_threaded_irq(dev, irq, NULL, ar9331_sw_irq,
  694. IRQF_ONESHOT, AR9331_SW_NAME, priv);
  695. if (ret) {
  696. dev_err(dev, "unable to request irq: %d\n", ret);
  697. return ret;
  698. }
  699. priv->irqdomain = irq_domain_add_linear(np, 1, &ar9331_sw_irqdomain_ops,
  700. priv);
  701. if (!priv->irqdomain) {
  702. dev_err(dev, "failed to create IRQ domain\n");
  703. return -EINVAL;
  704. }
  705. irq_set_parent(irq_create_mapping(priv->irqdomain, 0), irq);
  706. return 0;
  707. }
  708. static int __ar9331_mdio_write(struct mii_bus *sbus, u8 mode, u16 reg, u16 val)
  709. {
  710. u8 r, p;
  711. p = FIELD_PREP(AR9331_SW_MDIO_PHY_MODE_M, mode) |
  712. FIELD_GET(AR9331_SW_LOW_ADDR_PHY, reg);
  713. r = FIELD_GET(AR9331_SW_LOW_ADDR_REG, reg);
  714. return __mdiobus_write(sbus, p, r, val);
  715. }
  716. static int __ar9331_mdio_read(struct mii_bus *sbus, u16 reg)
  717. {
  718. u8 r, p;
  719. p = FIELD_PREP(AR9331_SW_MDIO_PHY_MODE_M, AR9331_SW_MDIO_PHY_MODE_REG) |
  720. FIELD_GET(AR9331_SW_LOW_ADDR_PHY, reg);
  721. r = FIELD_GET(AR9331_SW_LOW_ADDR_REG, reg);
  722. return __mdiobus_read(sbus, p, r);
  723. }
  724. static int ar9331_mdio_read(void *ctx, const void *reg_buf, size_t reg_len,
  725. void *val_buf, size_t val_len)
  726. {
  727. struct ar9331_sw_priv *priv = ctx;
  728. struct mii_bus *sbus = priv->sbus;
  729. u32 reg = *(u32 *)reg_buf;
  730. int ret;
  731. if (reg == AR9331_SW_REG_PAGE) {
  732. /* We cannot read the page selector register from hardware and
  733. * we cache its value in regmap. Return all bits set here,
  734. * that regmap will always write the page on first use.
  735. */
  736. *(u32 *)val_buf = GENMASK(9, 0);
  737. return 0;
  738. }
  739. mutex_lock_nested(&sbus->mdio_lock, MDIO_MUTEX_NESTED);
  740. ret = __ar9331_mdio_read(sbus, reg);
  741. if (ret < 0)
  742. goto error;
  743. *(u32 *)val_buf = ret;
  744. ret = __ar9331_mdio_read(sbus, reg + 2);
  745. if (ret < 0)
  746. goto error;
  747. *(u32 *)val_buf |= ret << 16;
  748. mutex_unlock(&sbus->mdio_lock);
  749. return 0;
  750. error:
  751. mutex_unlock(&sbus->mdio_lock);
  752. dev_err_ratelimited(&sbus->dev, "Bus error. Failed to read register.\n");
  753. return ret;
  754. }
  755. static int ar9331_mdio_write(void *ctx, u32 reg, u32 val)
  756. {
  757. struct ar9331_sw_priv *priv = (struct ar9331_sw_priv *)ctx;
  758. struct mii_bus *sbus = priv->sbus;
  759. int ret;
  760. mutex_lock_nested(&sbus->mdio_lock, MDIO_MUTEX_NESTED);
  761. if (reg == AR9331_SW_REG_PAGE) {
  762. ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_PAGE,
  763. 0, val);
  764. if (ret < 0)
  765. goto error;
  766. mutex_unlock(&sbus->mdio_lock);
  767. return 0;
  768. }
  769. /* In case of this switch we work with 32bit registers on top of 16bit
  770. * bus. Some registers (for example access to forwarding database) have
  771. * trigger bit on the first 16bit half of request, the result and
  772. * configuration of request in the second half.
  773. * To make it work properly, we should do the second part of transfer
  774. * before the first one is done.
  775. */
  776. ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_REG, reg + 2,
  777. val >> 16);
  778. if (ret < 0)
  779. goto error;
  780. ret = __ar9331_mdio_write(sbus, AR9331_SW_MDIO_PHY_MODE_REG, reg, val);
  781. if (ret < 0)
  782. goto error;
  783. mutex_unlock(&sbus->mdio_lock);
  784. return 0;
  785. error:
  786. mutex_unlock(&sbus->mdio_lock);
  787. dev_err_ratelimited(&sbus->dev, "Bus error. Failed to write register.\n");
  788. return ret;
  789. }
  790. static int ar9331_sw_bus_write(void *context, const void *data, size_t count)
  791. {
  792. u32 reg = *(u32 *)data;
  793. u32 val = *((u32 *)data + 1);
  794. return ar9331_mdio_write(context, reg, val);
  795. }
  796. static const struct regmap_range ar9331_valid_regs[] = {
  797. regmap_reg_range(0x0, 0x0),
  798. regmap_reg_range(0x10, 0x14),
  799. regmap_reg_range(0x20, 0x24),
  800. regmap_reg_range(0x2c, 0x30),
  801. regmap_reg_range(0x40, 0x44),
  802. regmap_reg_range(0x50, 0x78),
  803. regmap_reg_range(0x80, 0x98),
  804. regmap_reg_range(0x100, 0x120),
  805. regmap_reg_range(0x200, 0x220),
  806. regmap_reg_range(0x300, 0x320),
  807. regmap_reg_range(0x400, 0x420),
  808. regmap_reg_range(0x500, 0x520),
  809. regmap_reg_range(0x600, 0x620),
  810. regmap_reg_range(0x20000, 0x200a4),
  811. regmap_reg_range(0x20100, 0x201a4),
  812. regmap_reg_range(0x20200, 0x202a4),
  813. regmap_reg_range(0x20300, 0x203a4),
  814. regmap_reg_range(0x20400, 0x204a4),
  815. regmap_reg_range(0x20500, 0x205a4),
  816. /* dummy page selector reg */
  817. regmap_reg_range(AR9331_SW_REG_PAGE, AR9331_SW_REG_PAGE),
  818. };
  819. static const struct regmap_range ar9331_nonvolatile_regs[] = {
  820. regmap_reg_range(AR9331_SW_REG_PAGE, AR9331_SW_REG_PAGE),
  821. };
  822. static const struct regmap_range_cfg ar9331_regmap_range[] = {
  823. {
  824. .selector_reg = AR9331_SW_REG_PAGE,
  825. .selector_mask = GENMASK(9, 0),
  826. .selector_shift = 0,
  827. .window_start = 0,
  828. .window_len = 512,
  829. .range_min = 0,
  830. .range_max = AR9331_SW_REG_PAGE - 4,
  831. },
  832. };
  833. static const struct regmap_access_table ar9331_register_set = {
  834. .yes_ranges = ar9331_valid_regs,
  835. .n_yes_ranges = ARRAY_SIZE(ar9331_valid_regs),
  836. };
  837. static const struct regmap_access_table ar9331_volatile_set = {
  838. .no_ranges = ar9331_nonvolatile_regs,
  839. .n_no_ranges = ARRAY_SIZE(ar9331_nonvolatile_regs),
  840. };
  841. static const struct regmap_config ar9331_mdio_regmap_config = {
  842. .reg_bits = 32,
  843. .val_bits = 32,
  844. .reg_stride = 4,
  845. .max_register = AR9331_SW_REG_PAGE,
  846. .use_single_read = true,
  847. .use_single_write = true,
  848. .ranges = ar9331_regmap_range,
  849. .num_ranges = ARRAY_SIZE(ar9331_regmap_range),
  850. .volatile_table = &ar9331_volatile_set,
  851. .wr_table = &ar9331_register_set,
  852. .rd_table = &ar9331_register_set,
  853. .cache_type = REGCACHE_MAPLE,
  854. };
  855. static const struct regmap_bus ar9331_sw_bus = {
  856. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  857. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  858. .read = ar9331_mdio_read,
  859. .write = ar9331_sw_bus_write,
  860. };
  861. static int ar9331_sw_probe(struct mdio_device *mdiodev)
  862. {
  863. struct ar9331_sw_priv *priv;
  864. struct dsa_switch *ds;
  865. int ret, i;
  866. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  867. if (!priv)
  868. return -ENOMEM;
  869. priv->regmap = devm_regmap_init(&mdiodev->dev, &ar9331_sw_bus, priv,
  870. &ar9331_mdio_regmap_config);
  871. if (IS_ERR(priv->regmap)) {
  872. ret = PTR_ERR(priv->regmap);
  873. dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
  874. return ret;
  875. }
  876. priv->sw_reset = devm_reset_control_get(&mdiodev->dev, "switch");
  877. if (IS_ERR(priv->sw_reset)) {
  878. dev_err(&mdiodev->dev, "missing switch reset\n");
  879. return PTR_ERR(priv->sw_reset);
  880. }
  881. priv->sbus = mdiodev->bus;
  882. priv->dev = &mdiodev->dev;
  883. ret = ar9331_sw_irq_init(priv);
  884. if (ret)
  885. return ret;
  886. ds = &priv->ds;
  887. ds->dev = &mdiodev->dev;
  888. ds->num_ports = AR9331_SW_PORTS;
  889. ds->priv = priv;
  890. priv->ops = ar9331_sw_ops;
  891. ds->ops = &priv->ops;
  892. ds->phylink_mac_ops = &ar9331_phylink_mac_ops;
  893. dev_set_drvdata(&mdiodev->dev, priv);
  894. for (i = 0; i < ARRAY_SIZE(priv->port); i++) {
  895. struct ar9331_sw_port *port = &priv->port[i];
  896. port->idx = i;
  897. spin_lock_init(&port->stats_lock);
  898. INIT_DELAYED_WORK(&port->mib_read, ar9331_do_stats_poll);
  899. }
  900. ret = dsa_register_switch(ds);
  901. if (ret)
  902. goto err_remove_irq;
  903. return 0;
  904. err_remove_irq:
  905. irq_domain_remove(priv->irqdomain);
  906. return ret;
  907. }
  908. static void ar9331_sw_remove(struct mdio_device *mdiodev)
  909. {
  910. struct ar9331_sw_priv *priv = dev_get_drvdata(&mdiodev->dev);
  911. unsigned int i;
  912. if (!priv)
  913. return;
  914. for (i = 0; i < ARRAY_SIZE(priv->port); i++) {
  915. struct ar9331_sw_port *port = &priv->port[i];
  916. cancel_delayed_work_sync(&port->mib_read);
  917. }
  918. irq_domain_remove(priv->irqdomain);
  919. dsa_unregister_switch(&priv->ds);
  920. reset_control_assert(priv->sw_reset);
  921. }
  922. static void ar9331_sw_shutdown(struct mdio_device *mdiodev)
  923. {
  924. struct ar9331_sw_priv *priv = dev_get_drvdata(&mdiodev->dev);
  925. if (!priv)
  926. return;
  927. dsa_switch_shutdown(&priv->ds);
  928. dev_set_drvdata(&mdiodev->dev, NULL);
  929. }
  930. static const struct of_device_id ar9331_sw_of_match[] = {
  931. { .compatible = "qca,ar9331-switch" },
  932. { },
  933. };
  934. static struct mdio_driver ar9331_sw_mdio_driver = {
  935. .probe = ar9331_sw_probe,
  936. .remove = ar9331_sw_remove,
  937. .shutdown = ar9331_sw_shutdown,
  938. .mdiodrv.driver = {
  939. .name = AR9331_SW_NAME,
  940. .of_match_table = ar9331_sw_of_match,
  941. },
  942. };
  943. mdio_module_driver(ar9331_sw_mdio_driver);
  944. MODULE_AUTHOR("Oleksij Rempel <kernel@pengutronix.de>");
  945. MODULE_DESCRIPTION("Driver for Atheros AR9331 switch");
  946. MODULE_LICENSE("GPL v2");