mv88e6xxx.c 11 KB

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