mv88e61xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #include <common.h>
  25. #include <netdev.h>
  26. #include "mv88e61xx.h"
  27. #ifdef CONFIG_MV88E61XX_MULTICHIP_ADRMODE
  28. /* Chip Address mode
  29. * The Switch support two modes of operation
  30. * 1. single chip mode and
  31. * 2. Multi-chip mode
  32. * Refer section 9.2 &9.3 in chip datasheet-02 for more details
  33. *
  34. * By default single chip mode is configured
  35. * multichip mode operation can be configured in board header
  36. */
  37. static int mv88e61xx_busychk_multic(char *name, u32 devaddr)
  38. {
  39. u16 reg = 0;
  40. u32 timeout = MV88E61XX_PHY_TIMEOUT;
  41. /* Poll till SMIBusy bit is clear */
  42. do {
  43. miiphy_read(name, devaddr, 0x0, &reg);
  44. if (timeout-- == 0) {
  45. printf("SMI busy timeout\n");
  46. return -1;
  47. }
  48. } while (reg & (1 << 15));
  49. return 0;
  50. }
  51. static void mv88e61xx_wr_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 data)
  52. {
  53. u16 mii_dev_addr;
  54. /* command to read PHY dev address */
  55. if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
  56. printf("Error..could not read PHY dev address\n");
  57. return;
  58. }
  59. mv88e61xx_busychk_multic(name, mii_dev_addr);
  60. /* Write data to Switch indirect data register */
  61. miiphy_write(name, mii_dev_addr, 0x1, data);
  62. /* Write command to Switch indirect command register (write) */
  63. miiphy_write(name, mii_dev_addr, 0x0,
  64. reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
  65. 15));
  66. }
  67. static void mv88e61xx_rd_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 * data)
  68. {
  69. u16 mii_dev_addr;
  70. /* command to read PHY dev address */
  71. if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
  72. printf("Error..could not read PHY dev address\n");
  73. return;
  74. }
  75. mv88e61xx_busychk_multic(name, mii_dev_addr);
  76. /* Write command to Switch indirect command register (read) */
  77. miiphy_write(name, mii_dev_addr, 0x0,
  78. reg_ofs | (phy_adr << 5) | (1 << 11) | (1 << 12) | (1 <<
  79. 15));
  80. mv88e61xx_busychk_multic(name, mii_dev_addr);
  81. /* Read data from Switch indirect data register */
  82. miiphy_read(name, mii_dev_addr, 0x1, data);
  83. }
  84. #endif /* CONFIG_MV88E61XX_MULTICHIP_ADRMODE */
  85. static void mv88e61xx_port_vlan_config(struct mv88e61xx_config *swconfig,
  86. u32 max_prtnum, u32 ports_ofs)
  87. {
  88. u32 prt;
  89. u16 reg;
  90. char *name = swconfig->name;
  91. u32 cpu_port = swconfig->cpuport;
  92. u32 port_mask = swconfig->ports_enabled;
  93. enum mv88e61xx_cfg_vlan vlancfg = swconfig->vlancfg;
  94. /* be sure all ports are disabled */
  95. for (prt = 0; prt < max_prtnum; prt++) {
  96. RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, &reg);
  97. reg &= ~0x3;
  98. WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, reg);
  99. if (!(cpu_port & (1 << prt)))
  100. continue;
  101. /* Set CPU port VID to 0x1 */
  102. RD_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, &reg);
  103. reg &= ~0xfff;
  104. reg |= 0x1;
  105. WR_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, reg);
  106. }
  107. /* Setting Port default priority for all ports to zero */
  108. for (prt = 0; prt < max_prtnum; prt++) {
  109. RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, &reg);
  110. reg &= ~0xc000;
  111. WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, reg);
  112. }
  113. /* Setting VID and VID map for all ports except CPU port */
  114. for (prt = 0; prt < max_prtnum; prt++) {
  115. /* only for enabled ports */
  116. if ((1 << prt) & port_mask) {
  117. /* skip CPU port */
  118. if ((1 << prt) & cpu_port) {
  119. /*
  120. * Set Vlan map table for cpu_port to see
  121. * all ports
  122. */
  123. RD_PHY(name, (ports_ofs + prt),
  124. MV88E61XX_PRT_VMAP_REG, &reg);
  125. reg &= ~((1 << max_prtnum) - 1);
  126. reg |= port_mask & ~(1 << prt);
  127. WR_PHY(name, (ports_ofs + prt),
  128. MV88E61XX_PRT_VMAP_REG, reg);
  129. } else {
  130. /*
  131. * set Ports VLAN Mapping.
  132. * port prt <--> cpu_port VLAN #prt+1.
  133. */
  134. RD_PHY(name, ports_ofs + prt,
  135. MV88E61XX_PRT_VID_REG, &reg);
  136. reg &= ~0x0fff;
  137. reg |= (prt + 1);
  138. WR_PHY(name, ports_ofs + prt,
  139. MV88E61XX_PRT_VID_REG, reg);
  140. RD_PHY(name, ports_ofs + prt,
  141. MV88E61XX_PRT_VMAP_REG, &reg);
  142. if (vlancfg == MV88E61XX_VLANCFG_DEFAULT) {
  143. /*
  144. * all any port can send frames to all other ports
  145. * ref: sec 3.2.1.1 of datasheet
  146. */
  147. reg |= 0x03f;
  148. reg &= ~(1 << prt);
  149. } else if (vlancfg == MV88E61XX_VLANCFG_ROUTER) {
  150. /*
  151. * all other ports can send frames to CPU port only
  152. * ref: sec 3.2.1.2 of datasheet
  153. */
  154. reg &= ~((1 << max_prtnum) - 1);
  155. reg |= cpu_port;
  156. }
  157. WR_PHY(name, ports_ofs + prt,
  158. MV88E61XX_PRT_VMAP_REG, reg);
  159. }
  160. }
  161. }
  162. /*
  163. * enable only appropriate ports to forwarding mode
  164. * and disable the others
  165. */
  166. for (prt = 0; prt < max_prtnum; prt++) {
  167. if ((1 << prt) & port_mask) {
  168. RD_PHY(name, ports_ofs + prt,
  169. MV88E61XX_PRT_CTRL_REG, &reg);
  170. reg |= 0x3;
  171. WR_PHY(name, ports_ofs + prt,
  172. MV88E61XX_PRT_CTRL_REG, reg);
  173. } else {
  174. /* Disable port */
  175. RD_PHY(name, ports_ofs + prt,
  176. MV88E61XX_PRT_CTRL_REG, &reg);
  177. reg &= ~0x3;
  178. WR_PHY(name, ports_ofs + prt,
  179. MV88E61XX_PRT_CTRL_REG, reg);
  180. }
  181. }
  182. }
  183. /*
  184. * Make sure SMIBusy bit cleared before another
  185. * SMI operation can take place
  186. */
  187. static int mv88e61xx_busychk(char *name)
  188. {
  189. u16 reg = 0;
  190. u32 timeout = MV88E61XX_PHY_TIMEOUT;
  191. do {
  192. RD_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  193. MV88E61XX_PHY_CMD, &reg);
  194. if (timeout-- == 0) {
  195. printf("SMI busy timeout\n");
  196. return -1;
  197. }
  198. } while (reg & 1 << 15); /* busy mask */
  199. return 0;
  200. }
  201. /*
  202. * Power up the specified port and reset PHY
  203. */
  204. static int mv88361xx_powerup(struct mv88e61xx_config *swconfig, u32 prt)
  205. {
  206. char *name = swconfig->name;
  207. /* Write Copper Specific control reg1 (0x14) for-
  208. * Enable Phy power up
  209. * Energy Detect on (sense&Xmit NLP Periodically
  210. * reset other settings default
  211. */
  212. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x3360);
  213. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  214. MV88E61XX_PHY_CMD, (0x9410 | (prt << 5)));
  215. if (mv88e61xx_busychk(name))
  216. return -1;
  217. /* Write PHY ctrl reg (0x0) to apply
  218. * Phy reset (set bit 15 low)
  219. * reset other default values
  220. */
  221. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x1140);
  222. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  223. MV88E61XX_PHY_CMD, (0x9400 | (prt << 5)));
  224. if (mv88e61xx_busychk(name))
  225. return -1;
  226. return 0;
  227. }
  228. /*
  229. * Default Setup for LED[0]_Control (ref: Table 46 Datasheet-3)
  230. * is set to "On-1000Mb/s Link, Off Else"
  231. * This function sets it to "On-Link, Blink-Activity, Off-NoLink"
  232. *
  233. * This is optional settings may be needed on some boards
  234. * to setup PHY LEDs default configuration to detect 10/100/1000Mb/s
  235. * Link status
  236. */
  237. static int mv88361xx_led_init(struct mv88e61xx_config *swconfig, u32 prt)
  238. {
  239. char *name = swconfig->name;
  240. u16 reg;
  241. if (swconfig->led_init != MV88E61XX_LED_INIT_EN)
  242. return 0;
  243. /* set page address to 3 */
  244. reg = 3;
  245. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  246. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  247. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  248. 1 << MV88E61XX_MODE_OFST |
  249. 1 << MV88E61XX_OP_OFST |
  250. prt << MV88E61XX_ADDR_OFST | 22));
  251. if (mv88e61xx_busychk(name))
  252. return -1;
  253. /* set LED Func Ctrl reg */
  254. reg = 1; /* LED[0] On-Link, Blink-Activity, Off-NoLink */
  255. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  256. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  257. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  258. 1 << MV88E61XX_MODE_OFST |
  259. 1 << MV88E61XX_OP_OFST |
  260. prt << MV88E61XX_ADDR_OFST | 16));
  261. if (mv88e61xx_busychk(name))
  262. return -1;
  263. /* set page address to 0 */
  264. reg = 0;
  265. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  266. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  267. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  268. 1 << MV88E61XX_MODE_OFST |
  269. 1 << MV88E61XX_OP_OFST |
  270. prt << MV88E61XX_ADDR_OFST | 22));
  271. if (mv88e61xx_busychk(name))
  272. return -1;
  273. return 0;
  274. }
  275. /*
  276. * Reverse Transmit polarity for Media Dependent Interface
  277. * Pins (MDIP) bits in Copper Specific Control Register 3
  278. * (Page 0, Reg 20 for each phy (except cpu port)
  279. * Reference: Section 1.1 Switch datasheet-3
  280. *
  281. * This is optional settings may be needed on some boards
  282. * for PHY<->magnetics h/w tuning
  283. */
  284. static int mv88361xx_reverse_mdipn(struct mv88e61xx_config *swconfig, u32 prt)
  285. {
  286. char *name = swconfig->name;
  287. u16 reg;
  288. if (swconfig->mdip != MV88E61XX_MDIP_REVERSE)
  289. return 0;
  290. reg = 0x0f; /*Reverse MDIP/N[3:0] bits */
  291. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  292. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  293. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  294. 1 << MV88E61XX_MODE_OFST |
  295. 1 << MV88E61XX_OP_OFST |
  296. prt << MV88E61XX_ADDR_OFST | 20));
  297. if (mv88e61xx_busychk(name))
  298. return -1;
  299. return 0;
  300. }
  301. /*
  302. * Marvell 88E61XX Switch initialization
  303. */
  304. int mv88e61xx_switch_initialize(struct mv88e61xx_config *swconfig)
  305. {
  306. u32 prt;
  307. u16 reg;
  308. char *idstr;
  309. char *name = swconfig->name;
  310. if (miiphy_set_current_dev(name)) {
  311. printf("%s failed\n", __FUNCTION__);
  312. return -1;
  313. }
  314. if (!(swconfig->cpuport & ((1 << 4) | (1 << 5)))) {
  315. swconfig->cpuport = (1 << 5);
  316. printf("Invalid cpu port config, using default port5\n");
  317. }
  318. RD_PHY(name, MV88E61XX_PRT_OFST, PHY_PHYIDR2, &reg);
  319. switch (reg &= 0xfff0) {
  320. case 0x1610:
  321. idstr = "88E6161";
  322. break;
  323. case 0x1650:
  324. idstr = "88E6165";
  325. break;
  326. case 0x1210:
  327. idstr = "88E6123";
  328. /* ports 2,3,4 not available */
  329. swconfig->ports_enabled &= 0x023;
  330. break;
  331. default:
  332. /* Could not detect switch id */
  333. idstr = "88E61??";
  334. break;
  335. }
  336. /* Port based VLANs configuration */
  337. if ((swconfig->vlancfg == MV88E61XX_VLANCFG_DEFAULT)
  338. || (swconfig->vlancfg == MV88E61XX_VLANCFG_ROUTER))
  339. mv88e61xx_port_vlan_config(swconfig, MV88E61XX_MAX_PORTS_NUM,
  340. MV88E61XX_PRT_OFST);
  341. else {
  342. printf("Unsupported mode %s failed\n", __FUNCTION__);
  343. return -1;
  344. }
  345. if (swconfig->rgmii_delay == MV88E61XX_RGMII_DELAY_EN) {
  346. /*
  347. * Enable RGMII delay on Tx and Rx for CPU port
  348. * Ref: sec 9.5 of chip datasheet-02
  349. */
  350. WR_PHY(name, MV88E61XX_PRT_OFST + 5,
  351. MV88E61XX_RGMII_TIMECTRL_REG, 0x18);
  352. WR_PHY(name, MV88E61XX_PRT_OFST + 4,
  353. MV88E61XX_RGMII_TIMECTRL_REG, 0xc1e7);
  354. }
  355. for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
  356. if (!((1 << prt) & swconfig->cpuport)) {
  357. if (mv88361xx_led_init(swconfig, prt))
  358. return -1;
  359. if (mv88361xx_reverse_mdipn(swconfig, prt))
  360. return -1;
  361. if (mv88361xx_powerup(swconfig, prt))
  362. return -1;
  363. }
  364. /*Program port state */
  365. RD_PHY(name, MV88E61XX_PRT_OFST + prt,
  366. MV88E61XX_PRT_CTRL_REG, &reg);
  367. WR_PHY(name, MV88E61XX_PRT_OFST + prt,
  368. MV88E61XX_PRT_CTRL_REG,
  369. reg | (swconfig->portstate & 0x03));
  370. }
  371. printf("%s Initialized on %s\n", idstr, name);
  372. return 0;
  373. }