mv88e61xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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(u32 devaddr)
  38. {
  39. u32 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 reg;
  54. u32 mii_dev_addr;
  55. /* command to read PHY dev address */
  56. if (!miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
  57. printf("Error..could not read PHY dev address\n");
  58. return;
  59. }
  60. mv88e61xx_busychk_multic(mii_dev_addr);
  61. /* Write data to Switch indirect data register */
  62. miiphy_write(name, mii_dev_addr, 0x1, data);
  63. /* Write command to Switch indirect command register (write) */
  64. miiphy_write(name, mii_dev_addr, 0x0,
  65. reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
  66. 15));
  67. }
  68. static void mv88e61xx_rd_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 * data)
  69. {
  70. u16 reg;
  71. u32 mii_dev_addr;
  72. /* command to read PHY dev address */
  73. if (!miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
  74. printf("Error..could not read PHY dev address\n");
  75. return;
  76. }
  77. mv88e61xx_busychk_multic(mii_dev_addr);
  78. /* Write command to Switch indirect command register (read) */
  79. miiphy_write(name, mii_dev_addr, 0x0,
  80. reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
  81. 15));
  82. mv88e61xx_busychk_multic(mii_dev_addr);
  83. /* Read data from Switch indirect data register */
  84. miiphy_read(name, mii_dev_addr, 0x1, (u16 *) & data);
  85. }
  86. #endif /* CONFIG_MV88E61XX_MULTICHIP_ADRMODE */
  87. static void mv88e61xx_port_vlan_config(struct mv88e61xx_config *swconfig,
  88. u32 max_prtnum, u32 ports_ofs)
  89. {
  90. u32 prt;
  91. u16 reg;
  92. char *name = swconfig->name;
  93. u32 cpu_port = swconfig->cpuport;
  94. u32 port_mask = swconfig->ports_enabled;
  95. enum mv88e61xx_cfg_vlan vlancfg = swconfig->vlancfg;
  96. /* be sure all ports are disabled */
  97. for (prt = 0; prt < max_prtnum; prt++) {
  98. RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, &reg);
  99. reg &= ~0x3;
  100. WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, reg);
  101. if (!(cpu_port & (1 << prt)))
  102. continue;
  103. /* Set CPU port VID to 0x1 */
  104. RD_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, &reg);
  105. reg &= ~0xfff;
  106. reg |= 0x1;
  107. WR_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, reg);
  108. }
  109. /* Setting Port default priority for all ports to zero */
  110. for (prt = 0; prt < max_prtnum; prt++) {
  111. RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, &reg);
  112. reg &= ~0xc000;
  113. WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, reg);
  114. }
  115. /* Setting VID and VID map for all ports except CPU port */
  116. for (prt = 0; prt < max_prtnum; prt++) {
  117. /* only for enabled ports */
  118. if ((1 << prt) & port_mask) {
  119. /* skip CPU port */
  120. if ((1 << prt) & cpu_port) {
  121. /*
  122. * Set Vlan map table for cpu_port to see
  123. * all ports
  124. */
  125. RD_PHY(name, (ports_ofs + prt),
  126. MV88E61XX_PRT_VMAP_REG, &reg);
  127. reg &= ~((1 << max_prtnum) - 1);
  128. reg |= port_mask & ~(1 << prt);
  129. WR_PHY(name, (ports_ofs + prt),
  130. MV88E61XX_PRT_VMAP_REG, reg);
  131. } else {
  132. /*
  133. * set Ports VLAN Mapping.
  134. * port prt <--> cpu_port VLAN #prt+1.
  135. */
  136. RD_PHY(name, ports_ofs + prt,
  137. MV88E61XX_PRT_VID_REG, &reg);
  138. reg &= ~0x0fff;
  139. reg |= (prt + 1);
  140. WR_PHY(name, ports_ofs + prt,
  141. MV88E61XX_PRT_VID_REG, reg);
  142. RD_PHY(name, ports_ofs + prt,
  143. MV88E61XX_PRT_VMAP_REG, &reg);
  144. if (vlancfg == MV88E61XX_VLANCFG_DEFAULT) {
  145. /*
  146. * all any port can send frames to all other ports
  147. * ref: sec 3.2.1.1 of datasheet
  148. */
  149. reg |= 0x03f;
  150. reg &= ~(1 << prt);
  151. } else if (vlancfg == MV88E61XX_VLANCFG_ROUTER) {
  152. /*
  153. * all other ports can send frames to CPU port only
  154. * ref: sec 3.2.1.2 of datasheet
  155. */
  156. reg &= ~((1 << max_prtnum) - 1);
  157. reg |= cpu_port;
  158. }
  159. WR_PHY(name, ports_ofs + prt,
  160. MV88E61XX_PRT_VMAP_REG, reg);
  161. }
  162. }
  163. }
  164. /*
  165. * enable only appropriate ports to forwarding mode
  166. * and disable the others
  167. */
  168. for (prt = 0; prt < max_prtnum; prt++) {
  169. if ((1 << prt) & port_mask) {
  170. RD_PHY(name, ports_ofs + prt,
  171. MV88E61XX_PRT_CTRL_REG, &reg);
  172. reg |= 0x3;
  173. WR_PHY(name, ports_ofs + prt,
  174. MV88E61XX_PRT_CTRL_REG, reg);
  175. } else {
  176. /* Disable port */
  177. RD_PHY(name, ports_ofs + prt,
  178. MV88E61XX_PRT_CTRL_REG, &reg);
  179. reg &= ~0x3;
  180. WR_PHY(name, ports_ofs + prt,
  181. MV88E61XX_PRT_CTRL_REG, reg);
  182. }
  183. }
  184. }
  185. /*
  186. * Make sure SMIBusy bit cleared before another
  187. * SMI operation can take place
  188. */
  189. static int mv88e61xx_busychk(char *name)
  190. {
  191. u32 reg = 0;
  192. u32 timeout = MV88E61XX_PHY_TIMEOUT;
  193. do {
  194. RD_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  195. MV88E61XX_PHY_CMD, (u16 *) & reg);
  196. if (timeout-- == 0) {
  197. printf("SMI busy timeout\n");
  198. return -1;
  199. }
  200. } while (reg & 1 << 28); /* busy mask */
  201. return 0;
  202. }
  203. /*
  204. * Power up the specified port and reset PHY
  205. */
  206. static int mv88361xx_powerup(struct mv88e61xx_config *swconfig, u32 prt)
  207. {
  208. char *name = swconfig->name;
  209. /* Write Copper Specific control reg1 (0x14) for-
  210. * Enable Phy power up
  211. * Energy Detect on (sense&Xmit NLP Periodically
  212. * reset other settings default
  213. */
  214. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x3360);
  215. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  216. MV88E61XX_PHY_CMD, (0x9410 | (prt << 5)));
  217. if (mv88e61xx_busychk(name))
  218. return -1;
  219. /* Write PHY ctrl reg (0x0) to apply
  220. * Phy reset (set bit 15 low)
  221. * reset other default values
  222. */
  223. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x1140);
  224. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  225. MV88E61XX_PHY_CMD, (0x9400 | (prt << 5)));
  226. if (mv88e61xx_busychk(name))
  227. return -1;
  228. return 0;
  229. }
  230. /*
  231. * Default Setup for LED[0]_Control (ref: Table 46 Datasheet-3)
  232. * is set to "On-1000Mb/s Link, Off Else"
  233. * This function sets it to "On-Link, Blink-Activity, Off-NoLink"
  234. *
  235. * This is optional settings may be needed on some boards
  236. * to setup PHY LEDs default configuration to detect 10/100/1000Mb/s
  237. * Link status
  238. */
  239. static int mv88361xx_led_init(struct mv88e61xx_config *swconfig, u32 prt)
  240. {
  241. char *name = swconfig->name;
  242. u16 reg;
  243. if (swconfig->led_init != MV88E61XX_LED_INIT_EN)
  244. return 0;
  245. /* set page address to 3 */
  246. reg = 3;
  247. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  248. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  249. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  250. 1 << MV88E61XX_MODE_OFST |
  251. 1 << MV88E61XX_OP_OFST |
  252. prt << MV88E61XX_ADDR_OFST | 22));
  253. if (mv88e61xx_busychk(name))
  254. return -1;
  255. /* set LED Func Ctrl reg */
  256. reg = 1; /* LED[0] On-Link, Blink-Activity, Off-NoLink */
  257. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  258. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  259. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  260. 1 << MV88E61XX_MODE_OFST |
  261. 1 << MV88E61XX_OP_OFST |
  262. prt << MV88E61XX_ADDR_OFST | 16));
  263. if (mv88e61xx_busychk(name))
  264. return -1;
  265. /* set page address to 0 */
  266. reg = 0;
  267. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  268. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  269. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  270. 1 << MV88E61XX_MODE_OFST |
  271. 1 << MV88E61XX_OP_OFST |
  272. prt << MV88E61XX_ADDR_OFST | 22));
  273. if (mv88e61xx_busychk(name))
  274. return -1;
  275. return 0;
  276. }
  277. /*
  278. * Reverse Transmit polarity for Media Dependent Interface
  279. * Pins (MDIP) bits in Copper Specific Control Register 3
  280. * (Page 0, Reg 20 for each phy (except cpu port)
  281. * Reference: Section 1.1 Switch datasheet-3
  282. *
  283. * This is optional settings may be needed on some boards
  284. * for PHY<->magnetics h/w tuning
  285. */
  286. static int mv88361xx_reverse_mdipn(struct mv88e61xx_config *swconfig, u32 prt)
  287. {
  288. char *name = swconfig->name;
  289. u16 reg;
  290. if (swconfig->mdip != MV88E61XX_MDIP_REVERSE)
  291. return 0;
  292. reg = 0x0f; /*Reverse MDIP/N[3:0] bits */
  293. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
  294. WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
  295. MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
  296. 1 << MV88E61XX_MODE_OFST |
  297. 1 << MV88E61XX_OP_OFST |
  298. prt << MV88E61XX_ADDR_OFST | 20));
  299. if (mv88e61xx_busychk(name))
  300. return -1;
  301. return 0;
  302. }
  303. /*
  304. * Marvell 88E61XX Switch initialization
  305. */
  306. int mv88e61xx_switch_initialize(struct mv88e61xx_config *swconfig)
  307. {
  308. u32 prt;
  309. u16 reg;
  310. char *idstr;
  311. char *name = swconfig->name;
  312. if (miiphy_set_current_dev(name)) {
  313. printf("%s failed\n", __FUNCTION__);
  314. return -1;
  315. }
  316. if (!(swconfig->cpuport & ((1 << 4) | (1 << 5)))) {
  317. swconfig->cpuport = (1 << 5);
  318. printf("Invalid cpu port config, using default port5\n");
  319. }
  320. RD_PHY(name, MV88E61XX_PRT_OFST, PHY_PHYIDR2, &reg);
  321. reg &= 0xfff0;
  322. if (reg == 0x1610)
  323. idstr = "88E6161";
  324. if (reg == 0x1650)
  325. idstr = "88E6165";
  326. if (reg == 0x1210) {
  327. idstr = "88E6123";
  328. /* ports 2,3,4 not available */
  329. swconfig->ports_enabled &= 0x023;
  330. }
  331. /* Port based VLANs configuration */
  332. if ((swconfig->vlancfg == MV88E61XX_VLANCFG_DEFAULT)
  333. || (swconfig->vlancfg == MV88E61XX_VLANCFG_ROUTER))
  334. mv88e61xx_port_vlan_config(swconfig, MV88E61XX_MAX_PORTS_NUM,
  335. MV88E61XX_PRT_OFST);
  336. else {
  337. printf("Unsupported mode %s failed\n", __FUNCTION__);
  338. return -1;
  339. }
  340. if (swconfig->rgmii_delay == MV88E61XX_RGMII_DELAY_EN) {
  341. /*
  342. * Enable RGMII delay on Tx and Rx for CPU port
  343. * Ref: sec 9.5 of chip datasheet-02
  344. */
  345. WR_PHY(name, MV88E61XX_PRT_OFST + 5,
  346. MV88E61XX_RGMII_TIMECTRL_REG, 0x18);
  347. WR_PHY(name, MV88E61XX_PRT_OFST + 4,
  348. MV88E61XX_RGMII_TIMECTRL_REG, 0xc1e7);
  349. }
  350. for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
  351. if (!((1 << prt) & swconfig->cpuport)) {
  352. if (mv88361xx_led_init(swconfig, prt))
  353. return -1;
  354. if (mv88361xx_reverse_mdipn(swconfig, prt))
  355. return -1;
  356. if (mv88361xx_powerup(swconfig, prt))
  357. return -1;
  358. }
  359. /*Program port state */
  360. RD_PHY(name, MV88E61XX_PRT_OFST + prt,
  361. MV88E61XX_PRT_CTRL_REG, &reg);
  362. WR_PHY(name, MV88E61XX_PRT_OFST + prt,
  363. MV88E61XX_PRT_CTRL_REG,
  364. reg | (swconfig->portstate & 0x03));
  365. }
  366. printf("%s Initialized on %s\n", idstr, name);
  367. return 0;
  368. }