mv88e6xxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
  3. * Copyright (c) 2008 Marvell Semiconductor
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/phy.h>
  16. #include <net/dsa.h>
  17. #include "mv88e6xxx.h"
  18. /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
  19. * use all 32 SMI bus addresses on its SMI bus, and all switch registers
  20. * will be directly accessible on some {device address,register address}
  21. * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
  22. * will only respond to SMI transactions to that specific address, and
  23. * an indirect addressing mechanism needs to be used to access its
  24. * registers.
  25. */
  26. static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
  27. {
  28. int ret;
  29. int i;
  30. for (i = 0; i < 16; i++) {
  31. ret = mdiobus_read(bus, sw_addr, 0);
  32. if (ret < 0)
  33. return ret;
  34. if ((ret & 0x8000) == 0)
  35. return 0;
  36. }
  37. return -ETIMEDOUT;
  38. }
  39. int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
  40. {
  41. int ret;
  42. if (sw_addr == 0)
  43. return mdiobus_read(bus, addr, reg);
  44. /* Wait for the bus to become free. */
  45. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  46. if (ret < 0)
  47. return ret;
  48. /* Transmit the read command. */
  49. ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg);
  50. if (ret < 0)
  51. return ret;
  52. /* Wait for the read command to complete. */
  53. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  54. if (ret < 0)
  55. return ret;
  56. /* Read the data. */
  57. ret = mdiobus_read(bus, sw_addr, 1);
  58. if (ret < 0)
  59. return ret;
  60. return ret & 0xffff;
  61. }
  62. int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
  63. {
  64. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  65. int ret;
  66. mutex_lock(&ps->smi_mutex);
  67. ret = __mv88e6xxx_reg_read(ds->master_mii_bus,
  68. ds->pd->sw_addr, addr, reg);
  69. mutex_unlock(&ps->smi_mutex);
  70. return ret;
  71. }
  72. int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
  73. int reg, u16 val)
  74. {
  75. int ret;
  76. if (sw_addr == 0)
  77. return mdiobus_write(bus, addr, reg, val);
  78. /* Wait for the bus to become free. */
  79. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  80. if (ret < 0)
  81. return ret;
  82. /* Transmit the data to write. */
  83. ret = mdiobus_write(bus, sw_addr, 1, val);
  84. if (ret < 0)
  85. return ret;
  86. /* Transmit the write command. */
  87. ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
  88. if (ret < 0)
  89. return ret;
  90. /* Wait for the write command to complete. */
  91. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  92. if (ret < 0)
  93. return ret;
  94. return 0;
  95. }
  96. int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  97. {
  98. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  99. int ret;
  100. mutex_lock(&ps->smi_mutex);
  101. ret = __mv88e6xxx_reg_write(ds->master_mii_bus,
  102. ds->pd->sw_addr, addr, reg, val);
  103. mutex_unlock(&ps->smi_mutex);
  104. return ret;
  105. }
  106. int mv88e6xxx_config_prio(struct dsa_switch *ds)
  107. {
  108. /* Configure the IP ToS mapping registers. */
  109. REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
  110. REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
  111. REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
  112. REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
  113. REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
  114. REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
  115. REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
  116. REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
  117. /* Configure the IEEE 802.1p priority mapping register. */
  118. REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
  119. return 0;
  120. }
  121. int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
  122. {
  123. REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
  124. REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
  125. REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
  126. return 0;
  127. }
  128. int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
  129. {
  130. int i;
  131. int ret;
  132. for (i = 0; i < 6; i++) {
  133. int j;
  134. /* Write the MAC address byte. */
  135. REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
  136. /* Wait for the write to complete. */
  137. for (j = 0; j < 16; j++) {
  138. ret = REG_READ(REG_GLOBAL2, 0x0d);
  139. if ((ret & 0x8000) == 0)
  140. break;
  141. }
  142. if (j == 16)
  143. return -ETIMEDOUT;
  144. }
  145. return 0;
  146. }
  147. int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
  148. {
  149. if (addr >= 0)
  150. return mv88e6xxx_reg_read(ds, addr, regnum);
  151. return 0xffff;
  152. }
  153. int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
  154. {
  155. if (addr >= 0)
  156. return mv88e6xxx_reg_write(ds, addr, regnum, val);
  157. return 0;
  158. }
  159. #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
  160. static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
  161. {
  162. int ret;
  163. unsigned long timeout;
  164. ret = REG_READ(REG_GLOBAL, 0x04);
  165. REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
  166. timeout = jiffies + 1 * HZ;
  167. while (time_before(jiffies, timeout)) {
  168. ret = REG_READ(REG_GLOBAL, 0x00);
  169. usleep_range(1000, 2000);
  170. if ((ret & 0xc000) != 0xc000)
  171. return 0;
  172. }
  173. return -ETIMEDOUT;
  174. }
  175. static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
  176. {
  177. int ret;
  178. unsigned long timeout;
  179. ret = REG_READ(REG_GLOBAL, 0x04);
  180. REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
  181. timeout = jiffies + 1 * HZ;
  182. while (time_before(jiffies, timeout)) {
  183. ret = REG_READ(REG_GLOBAL, 0x00);
  184. usleep_range(1000, 2000);
  185. if ((ret & 0xc000) == 0xc000)
  186. return 0;
  187. }
  188. return -ETIMEDOUT;
  189. }
  190. static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
  191. {
  192. struct mv88e6xxx_priv_state *ps;
  193. ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
  194. if (mutex_trylock(&ps->ppu_mutex)) {
  195. struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
  196. if (mv88e6xxx_ppu_enable(ds) == 0)
  197. ps->ppu_disabled = 0;
  198. mutex_unlock(&ps->ppu_mutex);
  199. }
  200. }
  201. static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
  202. {
  203. struct mv88e6xxx_priv_state *ps = (void *)_ps;
  204. schedule_work(&ps->ppu_work);
  205. }
  206. static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
  207. {
  208. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  209. int ret;
  210. mutex_lock(&ps->ppu_mutex);
  211. /* If the PHY polling unit is enabled, disable it so that
  212. * we can access the PHY registers. If it was already
  213. * disabled, cancel the timer that is going to re-enable
  214. * it.
  215. */
  216. if (!ps->ppu_disabled) {
  217. ret = mv88e6xxx_ppu_disable(ds);
  218. if (ret < 0) {
  219. mutex_unlock(&ps->ppu_mutex);
  220. return ret;
  221. }
  222. ps->ppu_disabled = 1;
  223. } else {
  224. del_timer(&ps->ppu_timer);
  225. ret = 0;
  226. }
  227. return ret;
  228. }
  229. static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
  230. {
  231. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  232. /* Schedule a timer to re-enable the PHY polling unit. */
  233. mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
  234. mutex_unlock(&ps->ppu_mutex);
  235. }
  236. void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
  237. {
  238. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  239. mutex_init(&ps->ppu_mutex);
  240. INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
  241. init_timer(&ps->ppu_timer);
  242. ps->ppu_timer.data = (unsigned long)ps;
  243. ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
  244. }
  245. int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
  246. {
  247. int ret;
  248. ret = mv88e6xxx_ppu_access_get(ds);
  249. if (ret >= 0) {
  250. ret = mv88e6xxx_reg_read(ds, addr, regnum);
  251. mv88e6xxx_ppu_access_put(ds);
  252. }
  253. return ret;
  254. }
  255. int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
  256. int regnum, u16 val)
  257. {
  258. int ret;
  259. ret = mv88e6xxx_ppu_access_get(ds);
  260. if (ret >= 0) {
  261. ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
  262. mv88e6xxx_ppu_access_put(ds);
  263. }
  264. return ret;
  265. }
  266. #endif
  267. void mv88e6xxx_poll_link(struct dsa_switch *ds)
  268. {
  269. int i;
  270. for (i = 0; i < DSA_MAX_PORTS; i++) {
  271. struct net_device *dev;
  272. int uninitialized_var(port_status);
  273. int link;
  274. int speed;
  275. int duplex;
  276. int fc;
  277. dev = ds->ports[i];
  278. if (dev == NULL)
  279. continue;
  280. link = 0;
  281. if (dev->flags & IFF_UP) {
  282. port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
  283. if (port_status < 0)
  284. continue;
  285. link = !!(port_status & 0x0800);
  286. }
  287. if (!link) {
  288. if (netif_carrier_ok(dev)) {
  289. netdev_info(dev, "link down\n");
  290. netif_carrier_off(dev);
  291. }
  292. continue;
  293. }
  294. switch (port_status & 0x0300) {
  295. case 0x0000:
  296. speed = 10;
  297. break;
  298. case 0x0100:
  299. speed = 100;
  300. break;
  301. case 0x0200:
  302. speed = 1000;
  303. break;
  304. default:
  305. speed = -1;
  306. break;
  307. }
  308. duplex = (port_status & 0x0400) ? 1 : 0;
  309. fc = (port_status & 0x8000) ? 1 : 0;
  310. if (!netif_carrier_ok(dev)) {
  311. netdev_info(dev,
  312. "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  313. speed,
  314. duplex ? "full" : "half",
  315. fc ? "en" : "dis");
  316. netif_carrier_on(dev);
  317. }
  318. }
  319. }
  320. static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
  321. {
  322. int ret;
  323. int i;
  324. for (i = 0; i < 10; i++) {
  325. ret = REG_READ(REG_GLOBAL, 0x1d);
  326. if ((ret & 0x8000) == 0)
  327. return 0;
  328. }
  329. return -ETIMEDOUT;
  330. }
  331. static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
  332. {
  333. int ret;
  334. /* Snapshot the hardware statistics counters for this port. */
  335. REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
  336. /* Wait for the snapshotting to complete. */
  337. ret = mv88e6xxx_stats_wait(ds);
  338. if (ret < 0)
  339. return ret;
  340. return 0;
  341. }
  342. static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
  343. {
  344. u32 _val;
  345. int ret;
  346. *val = 0;
  347. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
  348. if (ret < 0)
  349. return;
  350. ret = mv88e6xxx_stats_wait(ds);
  351. if (ret < 0)
  352. return;
  353. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
  354. if (ret < 0)
  355. return;
  356. _val = ret << 16;
  357. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
  358. if (ret < 0)
  359. return;
  360. *val = _val | ret;
  361. }
  362. void mv88e6xxx_get_strings(struct dsa_switch *ds,
  363. int nr_stats, struct mv88e6xxx_hw_stat *stats,
  364. int port, uint8_t *data)
  365. {
  366. int i;
  367. for (i = 0; i < nr_stats; i++) {
  368. memcpy(data + i * ETH_GSTRING_LEN,
  369. stats[i].string, ETH_GSTRING_LEN);
  370. }
  371. }
  372. void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
  373. int nr_stats, struct mv88e6xxx_hw_stat *stats,
  374. int port, uint64_t *data)
  375. {
  376. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  377. int ret;
  378. int i;
  379. mutex_lock(&ps->stats_mutex);
  380. ret = mv88e6xxx_stats_snapshot(ds, port);
  381. if (ret < 0) {
  382. mutex_unlock(&ps->stats_mutex);
  383. return;
  384. }
  385. /* Read each of the counters. */
  386. for (i = 0; i < nr_stats; i++) {
  387. struct mv88e6xxx_hw_stat *s = stats + i;
  388. u32 low;
  389. u32 high;
  390. mv88e6xxx_stats_read(ds, s->reg, &low);
  391. if (s->sizeof_stat == 8)
  392. mv88e6xxx_stats_read(ds, s->reg + 1, &high);
  393. else
  394. high = 0;
  395. data[i] = (((u64)high) << 32) | low;
  396. }
  397. mutex_unlock(&ps->stats_mutex);
  398. }
  399. static int __init mv88e6xxx_init(void)
  400. {
  401. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  402. register_switch_driver(&mv88e6131_switch_driver);
  403. #endif
  404. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  405. register_switch_driver(&mv88e6123_61_65_switch_driver);
  406. #endif
  407. return 0;
  408. }
  409. module_init(mv88e6xxx_init);
  410. static void __exit mv88e6xxx_cleanup(void)
  411. {
  412. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  413. unregister_switch_driver(&mv88e6123_61_65_switch_driver);
  414. #endif
  415. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  416. unregister_switch_driver(&mv88e6131_switch_driver);
  417. #endif
  418. }
  419. module_exit(mv88e6xxx_cleanup);
  420. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  421. MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
  422. MODULE_LICENSE("GPL");