mv88e6xxx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 = ds_to_priv(ds);
  65. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  66. int ret;
  67. if (bus == NULL)
  68. return -EINVAL;
  69. mutex_lock(&ps->smi_mutex);
  70. ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
  71. mutex_unlock(&ps->smi_mutex);
  72. return ret;
  73. }
  74. int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
  75. int reg, u16 val)
  76. {
  77. int ret;
  78. if (sw_addr == 0)
  79. return mdiobus_write(bus, addr, reg, val);
  80. /* Wait for the bus to become free. */
  81. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  82. if (ret < 0)
  83. return ret;
  84. /* Transmit the data to write. */
  85. ret = mdiobus_write(bus, sw_addr, 1, val);
  86. if (ret < 0)
  87. return ret;
  88. /* Transmit the write command. */
  89. ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
  90. if (ret < 0)
  91. return ret;
  92. /* Wait for the write command to complete. */
  93. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  94. if (ret < 0)
  95. return ret;
  96. return 0;
  97. }
  98. int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  99. {
  100. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  101. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  102. int ret;
  103. if (bus == NULL)
  104. return -EINVAL;
  105. mutex_lock(&ps->smi_mutex);
  106. ret = __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
  107. mutex_unlock(&ps->smi_mutex);
  108. return ret;
  109. }
  110. int mv88e6xxx_config_prio(struct dsa_switch *ds)
  111. {
  112. /* Configure the IP ToS mapping registers. */
  113. REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
  114. REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
  115. REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
  116. REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
  117. REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
  118. REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
  119. REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
  120. REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
  121. /* Configure the IEEE 802.1p priority mapping register. */
  122. REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
  123. return 0;
  124. }
  125. int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
  126. {
  127. REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
  128. REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
  129. REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
  130. return 0;
  131. }
  132. int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
  133. {
  134. int i;
  135. int ret;
  136. for (i = 0; i < 6; i++) {
  137. int j;
  138. /* Write the MAC address byte. */
  139. REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
  140. /* Wait for the write to complete. */
  141. for (j = 0; j < 16; j++) {
  142. ret = REG_READ(REG_GLOBAL2, 0x0d);
  143. if ((ret & 0x8000) == 0)
  144. break;
  145. }
  146. if (j == 16)
  147. return -ETIMEDOUT;
  148. }
  149. return 0;
  150. }
  151. int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
  152. {
  153. if (addr >= 0)
  154. return mv88e6xxx_reg_read(ds, addr, regnum);
  155. return 0xffff;
  156. }
  157. int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
  158. {
  159. if (addr >= 0)
  160. return mv88e6xxx_reg_write(ds, addr, regnum, val);
  161. return 0;
  162. }
  163. #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
  164. static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
  165. {
  166. int ret;
  167. unsigned long timeout;
  168. ret = REG_READ(REG_GLOBAL, 0x04);
  169. REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
  170. timeout = jiffies + 1 * HZ;
  171. while (time_before(jiffies, timeout)) {
  172. ret = REG_READ(REG_GLOBAL, 0x00);
  173. usleep_range(1000, 2000);
  174. if ((ret & 0xc000) != 0xc000)
  175. return 0;
  176. }
  177. return -ETIMEDOUT;
  178. }
  179. static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
  180. {
  181. int ret;
  182. unsigned long timeout;
  183. ret = REG_READ(REG_GLOBAL, 0x04);
  184. REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
  185. timeout = jiffies + 1 * HZ;
  186. while (time_before(jiffies, timeout)) {
  187. ret = REG_READ(REG_GLOBAL, 0x00);
  188. usleep_range(1000, 2000);
  189. if ((ret & 0xc000) == 0xc000)
  190. return 0;
  191. }
  192. return -ETIMEDOUT;
  193. }
  194. static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
  195. {
  196. struct mv88e6xxx_priv_state *ps;
  197. ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
  198. if (mutex_trylock(&ps->ppu_mutex)) {
  199. struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
  200. if (mv88e6xxx_ppu_enable(ds) == 0)
  201. ps->ppu_disabled = 0;
  202. mutex_unlock(&ps->ppu_mutex);
  203. }
  204. }
  205. static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
  206. {
  207. struct mv88e6xxx_priv_state *ps = (void *)_ps;
  208. schedule_work(&ps->ppu_work);
  209. }
  210. static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
  211. {
  212. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  213. int ret;
  214. mutex_lock(&ps->ppu_mutex);
  215. /* If the PHY polling unit is enabled, disable it so that
  216. * we can access the PHY registers. If it was already
  217. * disabled, cancel the timer that is going to re-enable
  218. * it.
  219. */
  220. if (!ps->ppu_disabled) {
  221. ret = mv88e6xxx_ppu_disable(ds);
  222. if (ret < 0) {
  223. mutex_unlock(&ps->ppu_mutex);
  224. return ret;
  225. }
  226. ps->ppu_disabled = 1;
  227. } else {
  228. del_timer(&ps->ppu_timer);
  229. ret = 0;
  230. }
  231. return ret;
  232. }
  233. static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
  234. {
  235. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  236. /* Schedule a timer to re-enable the PHY polling unit. */
  237. mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
  238. mutex_unlock(&ps->ppu_mutex);
  239. }
  240. void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
  241. {
  242. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  243. mutex_init(&ps->ppu_mutex);
  244. INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
  245. init_timer(&ps->ppu_timer);
  246. ps->ppu_timer.data = (unsigned long)ps;
  247. ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
  248. }
  249. int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
  250. {
  251. int ret;
  252. ret = mv88e6xxx_ppu_access_get(ds);
  253. if (ret >= 0) {
  254. ret = mv88e6xxx_reg_read(ds, addr, regnum);
  255. mv88e6xxx_ppu_access_put(ds);
  256. }
  257. return ret;
  258. }
  259. int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
  260. int regnum, u16 val)
  261. {
  262. int ret;
  263. ret = mv88e6xxx_ppu_access_get(ds);
  264. if (ret >= 0) {
  265. ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
  266. mv88e6xxx_ppu_access_put(ds);
  267. }
  268. return ret;
  269. }
  270. #endif
  271. void mv88e6xxx_poll_link(struct dsa_switch *ds)
  272. {
  273. int i;
  274. for (i = 0; i < DSA_MAX_PORTS; i++) {
  275. struct net_device *dev;
  276. int uninitialized_var(port_status);
  277. int link;
  278. int speed;
  279. int duplex;
  280. int fc;
  281. dev = ds->ports[i];
  282. if (dev == NULL)
  283. continue;
  284. link = 0;
  285. if (dev->flags & IFF_UP) {
  286. port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
  287. if (port_status < 0)
  288. continue;
  289. link = !!(port_status & 0x0800);
  290. }
  291. if (!link) {
  292. if (netif_carrier_ok(dev)) {
  293. netdev_info(dev, "link down\n");
  294. netif_carrier_off(dev);
  295. }
  296. continue;
  297. }
  298. switch (port_status & 0x0300) {
  299. case 0x0000:
  300. speed = 10;
  301. break;
  302. case 0x0100:
  303. speed = 100;
  304. break;
  305. case 0x0200:
  306. speed = 1000;
  307. break;
  308. default:
  309. speed = -1;
  310. break;
  311. }
  312. duplex = (port_status & 0x0400) ? 1 : 0;
  313. fc = (port_status & 0x8000) ? 1 : 0;
  314. if (!netif_carrier_ok(dev)) {
  315. netdev_info(dev,
  316. "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  317. speed,
  318. duplex ? "full" : "half",
  319. fc ? "en" : "dis");
  320. netif_carrier_on(dev);
  321. }
  322. }
  323. }
  324. static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
  325. {
  326. int ret;
  327. int i;
  328. for (i = 0; i < 10; i++) {
  329. ret = REG_READ(REG_GLOBAL, 0x1d);
  330. if ((ret & 0x8000) == 0)
  331. return 0;
  332. }
  333. return -ETIMEDOUT;
  334. }
  335. static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
  336. {
  337. int ret;
  338. /* Snapshot the hardware statistics counters for this port. */
  339. REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
  340. /* Wait for the snapshotting to complete. */
  341. ret = mv88e6xxx_stats_wait(ds);
  342. if (ret < 0)
  343. return ret;
  344. return 0;
  345. }
  346. static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
  347. {
  348. u32 _val;
  349. int ret;
  350. *val = 0;
  351. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
  352. if (ret < 0)
  353. return;
  354. ret = mv88e6xxx_stats_wait(ds);
  355. if (ret < 0)
  356. return;
  357. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
  358. if (ret < 0)
  359. return;
  360. _val = ret << 16;
  361. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
  362. if (ret < 0)
  363. return;
  364. *val = _val | ret;
  365. }
  366. void mv88e6xxx_get_strings(struct dsa_switch *ds,
  367. int nr_stats, struct mv88e6xxx_hw_stat *stats,
  368. int port, uint8_t *data)
  369. {
  370. int i;
  371. for (i = 0; i < nr_stats; i++) {
  372. memcpy(data + i * ETH_GSTRING_LEN,
  373. stats[i].string, ETH_GSTRING_LEN);
  374. }
  375. }
  376. void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
  377. int nr_stats, struct mv88e6xxx_hw_stat *stats,
  378. int port, uint64_t *data)
  379. {
  380. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  381. int ret;
  382. int i;
  383. mutex_lock(&ps->stats_mutex);
  384. ret = mv88e6xxx_stats_snapshot(ds, port);
  385. if (ret < 0) {
  386. mutex_unlock(&ps->stats_mutex);
  387. return;
  388. }
  389. /* Read each of the counters. */
  390. for (i = 0; i < nr_stats; i++) {
  391. struct mv88e6xxx_hw_stat *s = stats + i;
  392. u32 low;
  393. u32 high = 0;
  394. if (s->reg >= 0x100) {
  395. int ret;
  396. ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
  397. s->reg - 0x100);
  398. if (ret < 0)
  399. goto error;
  400. low = ret;
  401. if (s->sizeof_stat == 4) {
  402. ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
  403. s->reg - 0x100 + 1);
  404. if (ret < 0)
  405. goto error;
  406. high = ret;
  407. }
  408. data[i] = (((u64)high) << 16) | low;
  409. continue;
  410. }
  411. mv88e6xxx_stats_read(ds, s->reg, &low);
  412. if (s->sizeof_stat == 8)
  413. mv88e6xxx_stats_read(ds, s->reg + 1, &high);
  414. data[i] = (((u64)high) << 32) | low;
  415. }
  416. error:
  417. mutex_unlock(&ps->stats_mutex);
  418. }
  419. int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
  420. {
  421. return 32 * sizeof(u16);
  422. }
  423. void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
  424. struct ethtool_regs *regs, void *_p)
  425. {
  426. u16 *p = _p;
  427. int i;
  428. regs->version = 0;
  429. memset(p, 0xff, 32 * sizeof(u16));
  430. for (i = 0; i < 32; i++) {
  431. int ret;
  432. ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
  433. if (ret >= 0)
  434. p[i] = ret;
  435. }
  436. }
  437. #ifdef CONFIG_NET_DSA_HWMON
  438. int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
  439. {
  440. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  441. int ret;
  442. int val;
  443. *temp = 0;
  444. mutex_lock(&ps->phy_mutex);
  445. ret = mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
  446. if (ret < 0)
  447. goto error;
  448. /* Enable temperature sensor */
  449. ret = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
  450. if (ret < 0)
  451. goto error;
  452. ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
  453. if (ret < 0)
  454. goto error;
  455. /* Wait for temperature to stabilize */
  456. usleep_range(10000, 12000);
  457. val = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
  458. if (val < 0) {
  459. ret = val;
  460. goto error;
  461. }
  462. /* Disable temperature sensor */
  463. ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
  464. if (ret < 0)
  465. goto error;
  466. *temp = ((val & 0x1f) - 5) * 5;
  467. error:
  468. mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
  469. mutex_unlock(&ps->phy_mutex);
  470. return ret;
  471. }
  472. #endif /* CONFIG_NET_DSA_HWMON */
  473. static int __init mv88e6xxx_init(void)
  474. {
  475. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  476. register_switch_driver(&mv88e6131_switch_driver);
  477. #endif
  478. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  479. register_switch_driver(&mv88e6123_61_65_switch_driver);
  480. #endif
  481. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  482. register_switch_driver(&mv88e6352_switch_driver);
  483. #endif
  484. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
  485. register_switch_driver(&mv88e6171_switch_driver);
  486. #endif
  487. return 0;
  488. }
  489. module_init(mv88e6xxx_init);
  490. static void __exit mv88e6xxx_cleanup(void)
  491. {
  492. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
  493. unregister_switch_driver(&mv88e6171_switch_driver);
  494. #endif
  495. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  496. unregister_switch_driver(&mv88e6123_61_65_switch_driver);
  497. #endif
  498. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  499. unregister_switch_driver(&mv88e6131_switch_driver);
  500. #endif
  501. }
  502. module_exit(mv88e6xxx_cleanup);
  503. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  504. MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
  505. MODULE_LICENSE("GPL");