miiphybb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * (C) Copyright 2009 Industrie Dial Face S.p.A.
  3. * Luigi 'Comio' Mantellini <luigi.mantellini@idf-hit.com>
  4. *
  5. * (C) Copyright 2001
  6. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * This provides a bit-banged interface to the ethernet MII management
  12. * channel.
  13. */
  14. #include <common.h>
  15. #include <ioports.h>
  16. #include <ppc_asm.tmpl>
  17. #include <miiphy.h>
  18. #define BB_MII_RELOCATE(v,off) (v += (v?off:0))
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #ifndef CONFIG_BITBANGMII_MULTI
  21. /*
  22. * If CONFIG_BITBANGMII_MULTI is not defined we use a
  23. * compatibility layer with the previous miiphybb implementation
  24. * based on macros usage.
  25. *
  26. */
  27. static int bb_mii_init_wrap(struct bb_miiphy_bus *bus)
  28. {
  29. #ifdef MII_INIT
  30. MII_INIT;
  31. #endif
  32. return 0;
  33. }
  34. static int bb_mdio_active_wrap(struct bb_miiphy_bus *bus)
  35. {
  36. #ifdef MDIO_DECLARE
  37. MDIO_DECLARE;
  38. #endif
  39. MDIO_ACTIVE;
  40. return 0;
  41. }
  42. static int bb_mdio_tristate_wrap(struct bb_miiphy_bus *bus)
  43. {
  44. #ifdef MDIO_DECLARE
  45. MDIO_DECLARE;
  46. #endif
  47. MDIO_TRISTATE;
  48. return 0;
  49. }
  50. static int bb_set_mdio_wrap(struct bb_miiphy_bus *bus, int v)
  51. {
  52. #ifdef MDIO_DECLARE
  53. MDIO_DECLARE;
  54. #endif
  55. MDIO(v);
  56. return 0;
  57. }
  58. static int bb_get_mdio_wrap(struct bb_miiphy_bus *bus, int *v)
  59. {
  60. #ifdef MDIO_DECLARE
  61. MDIO_DECLARE;
  62. #endif
  63. *v = MDIO_READ;
  64. return 0;
  65. }
  66. static int bb_set_mdc_wrap(struct bb_miiphy_bus *bus, int v)
  67. {
  68. #ifdef MDC_DECLARE
  69. MDC_DECLARE;
  70. #endif
  71. MDC(v);
  72. return 0;
  73. }
  74. static int bb_delay_wrap(struct bb_miiphy_bus *bus)
  75. {
  76. MIIDELAY;
  77. return 0;
  78. }
  79. struct bb_miiphy_bus bb_miiphy_buses[] = {
  80. {
  81. .name = BB_MII_DEVNAME,
  82. .init = bb_mii_init_wrap,
  83. .mdio_active = bb_mdio_active_wrap,
  84. .mdio_tristate = bb_mdio_tristate_wrap,
  85. .set_mdio = bb_set_mdio_wrap,
  86. .get_mdio = bb_get_mdio_wrap,
  87. .set_mdc = bb_set_mdc_wrap,
  88. .delay = bb_delay_wrap,
  89. }
  90. };
  91. int bb_miiphy_buses_num = sizeof(bb_miiphy_buses) /
  92. sizeof(bb_miiphy_buses[0]);
  93. #endif
  94. void bb_miiphy_init(void)
  95. {
  96. int i;
  97. for (i = 0; i < bb_miiphy_buses_num; i++) {
  98. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  99. /* Relocate the hook pointers*/
  100. BB_MII_RELOCATE(bb_miiphy_buses[i].init, gd->reloc_off);
  101. BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_active, gd->reloc_off);
  102. BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_tristate, gd->reloc_off);
  103. BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdio, gd->reloc_off);
  104. BB_MII_RELOCATE(bb_miiphy_buses[i].get_mdio, gd->reloc_off);
  105. BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdc, gd->reloc_off);
  106. BB_MII_RELOCATE(bb_miiphy_buses[i].delay, gd->reloc_off);
  107. #endif
  108. if (bb_miiphy_buses[i].init != NULL) {
  109. bb_miiphy_buses[i].init(&bb_miiphy_buses[i]);
  110. }
  111. }
  112. }
  113. static inline struct bb_miiphy_bus *bb_miiphy_getbus(const char *devname)
  114. {
  115. #ifdef CONFIG_BITBANGMII_MULTI
  116. int i;
  117. /* Search the correct bus */
  118. for (i = 0; i < bb_miiphy_buses_num; i++) {
  119. if (!strcmp(bb_miiphy_buses[i].name, devname)) {
  120. return &bb_miiphy_buses[i];
  121. }
  122. }
  123. return NULL;
  124. #else
  125. /* We have just one bitbanging bus */
  126. return &bb_miiphy_buses[0];
  127. #endif
  128. }
  129. /*****************************************************************************
  130. *
  131. * Utility to send the preamble, address, and register (common to read
  132. * and write).
  133. */
  134. static void miiphy_pre(struct bb_miiphy_bus *bus, char read,
  135. unsigned char addr, unsigned char reg)
  136. {
  137. int j;
  138. /*
  139. * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
  140. * The IEEE spec says this is a PHY optional requirement. The AMD
  141. * 79C874 requires one after power up and one after a MII communications
  142. * error. This means that we are doing more preambles than we need,
  143. * but it is safer and will be much more robust.
  144. */
  145. bus->mdio_active(bus);
  146. bus->set_mdio(bus, 1);
  147. for (j = 0; j < 32; j++) {
  148. bus->set_mdc(bus, 0);
  149. bus->delay(bus);
  150. bus->set_mdc(bus, 1);
  151. bus->delay(bus);
  152. }
  153. /* send the start bit (01) and the read opcode (10) or write (10) */
  154. bus->set_mdc(bus, 0);
  155. bus->set_mdio(bus, 0);
  156. bus->delay(bus);
  157. bus->set_mdc(bus, 1);
  158. bus->delay(bus);
  159. bus->set_mdc(bus, 0);
  160. bus->set_mdio(bus, 1);
  161. bus->delay(bus);
  162. bus->set_mdc(bus, 1);
  163. bus->delay(bus);
  164. bus->set_mdc(bus, 0);
  165. bus->set_mdio(bus, read);
  166. bus->delay(bus);
  167. bus->set_mdc(bus, 1);
  168. bus->delay(bus);
  169. bus->set_mdc(bus, 0);
  170. bus->set_mdio(bus, !read);
  171. bus->delay(bus);
  172. bus->set_mdc(bus, 1);
  173. bus->delay(bus);
  174. /* send the PHY address */
  175. for (j = 0; j < 5; j++) {
  176. bus->set_mdc(bus, 0);
  177. if ((addr & 0x10) == 0) {
  178. bus->set_mdio(bus, 0);
  179. } else {
  180. bus->set_mdio(bus, 1);
  181. }
  182. bus->delay(bus);
  183. bus->set_mdc(bus, 1);
  184. bus->delay(bus);
  185. addr <<= 1;
  186. }
  187. /* send the register address */
  188. for (j = 0; j < 5; j++) {
  189. bus->set_mdc(bus, 0);
  190. if ((reg & 0x10) == 0) {
  191. bus->set_mdio(bus, 0);
  192. } else {
  193. bus->set_mdio(bus, 1);
  194. }
  195. bus->delay(bus);
  196. bus->set_mdc(bus, 1);
  197. bus->delay(bus);
  198. reg <<= 1;
  199. }
  200. }
  201. /*****************************************************************************
  202. *
  203. * Read a MII PHY register.
  204. *
  205. * Returns:
  206. * 0 on success
  207. */
  208. int bb_miiphy_read(const char *devname, unsigned char addr,
  209. unsigned char reg, unsigned short *value)
  210. {
  211. short rdreg; /* register working value */
  212. int v;
  213. int j; /* counter */
  214. struct bb_miiphy_bus *bus;
  215. bus = bb_miiphy_getbus(devname);
  216. if (bus == NULL) {
  217. return -1;
  218. }
  219. if (value == NULL) {
  220. puts("NULL value pointer\n");
  221. return -1;
  222. }
  223. miiphy_pre (bus, 1, addr, reg);
  224. /* tri-state our MDIO I/O pin so we can read */
  225. bus->set_mdc(bus, 0);
  226. bus->mdio_tristate(bus);
  227. bus->delay(bus);
  228. bus->set_mdc(bus, 1);
  229. bus->delay(bus);
  230. /* check the turnaround bit: the PHY should be driving it to zero */
  231. bus->get_mdio(bus, &v);
  232. if (v != 0) {
  233. /* puts ("PHY didn't drive TA low\n"); */
  234. for (j = 0; j < 32; j++) {
  235. bus->set_mdc(bus, 0);
  236. bus->delay(bus);
  237. bus->set_mdc(bus, 1);
  238. bus->delay(bus);
  239. }
  240. /* There is no PHY, set value to 0xFFFF and return */
  241. *value = 0xFFFF;
  242. return -1;
  243. }
  244. bus->set_mdc(bus, 0);
  245. bus->delay(bus);
  246. /* read 16 bits of register data, MSB first */
  247. rdreg = 0;
  248. for (j = 0; j < 16; j++) {
  249. bus->set_mdc(bus, 1);
  250. bus->delay(bus);
  251. rdreg <<= 1;
  252. bus->get_mdio(bus, &v);
  253. rdreg |= (v & 0x1);
  254. bus->set_mdc(bus, 0);
  255. bus->delay(bus);
  256. }
  257. bus->set_mdc(bus, 1);
  258. bus->delay(bus);
  259. bus->set_mdc(bus, 0);
  260. bus->delay(bus);
  261. bus->set_mdc(bus, 1);
  262. bus->delay(bus);
  263. *value = rdreg;
  264. #ifdef DEBUG
  265. printf ("miiphy_read(0x%x) @ 0x%x = 0x%04x\n", reg, addr, *value);
  266. #endif
  267. return 0;
  268. }
  269. /*****************************************************************************
  270. *
  271. * Write a MII PHY register.
  272. *
  273. * Returns:
  274. * 0 on success
  275. */
  276. int bb_miiphy_write (const char *devname, unsigned char addr,
  277. unsigned char reg, unsigned short value)
  278. {
  279. struct bb_miiphy_bus *bus;
  280. int j; /* counter */
  281. bus = bb_miiphy_getbus(devname);
  282. if (bus == NULL) {
  283. /* Bus not found! */
  284. return -1;
  285. }
  286. miiphy_pre (bus, 0, addr, reg);
  287. /* send the turnaround (10) */
  288. bus->set_mdc(bus, 0);
  289. bus->set_mdio(bus, 1);
  290. bus->delay(bus);
  291. bus->set_mdc(bus, 1);
  292. bus->delay(bus);
  293. bus->set_mdc(bus, 0);
  294. bus->set_mdio(bus, 0);
  295. bus->delay(bus);
  296. bus->set_mdc(bus, 1);
  297. bus->delay(bus);
  298. /* write 16 bits of register data, MSB first */
  299. for (j = 0; j < 16; j++) {
  300. bus->set_mdc(bus, 0);
  301. if ((value & 0x00008000) == 0) {
  302. bus->set_mdio(bus, 0);
  303. } else {
  304. bus->set_mdio(bus, 1);
  305. }
  306. bus->delay(bus);
  307. bus->set_mdc(bus, 1);
  308. bus->delay(bus);
  309. value <<= 1;
  310. }
  311. /*
  312. * Tri-state the MDIO line.
  313. */
  314. bus->mdio_tristate(bus);
  315. bus->set_mdc(bus, 0);
  316. bus->delay(bus);
  317. bus->set_mdc(bus, 1);
  318. bus->delay(bus);
  319. return 0;
  320. }