mv88e6xxx.c 11 KB

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