mac-fec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Freescale Ethernet controllers
  3. *
  4. * Copyright (c) 2005 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * 2005 (c) MontaVista Software, Inc.
  8. * Vitaly Bordug <vbordug@ru.mvista.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. #include <linux/config.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/bitops.h>
  35. #include <linux/fs.h>
  36. #include <linux/platform_device.h>
  37. #include <asm/irq.h>
  38. #include <asm/uaccess.h>
  39. #ifdef CONFIG_8xx
  40. #include <asm/8xx_immap.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/mpc8xx.h>
  43. #include <asm/commproc.h>
  44. #endif
  45. #include "fs_enet.h"
  46. /*************************************************/
  47. #if defined(CONFIG_CPM1)
  48. /* for a CPM1 __raw_xxx's are sufficient */
  49. #define __fs_out32(addr, x) __raw_writel(x, addr)
  50. #define __fs_out16(addr, x) __raw_writew(x, addr)
  51. #define __fs_in32(addr) __raw_readl(addr)
  52. #define __fs_in16(addr) __raw_readw(addr)
  53. #else
  54. /* for others play it safe */
  55. #define __fs_out32(addr, x) out_be32(addr, x)
  56. #define __fs_out16(addr, x) out_be16(addr, x)
  57. #define __fs_in32(addr) in_be32(addr)
  58. #define __fs_in16(addr) in_be16(addr)
  59. #endif
  60. /* write */
  61. #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
  62. /* read */
  63. #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
  64. /* set bits */
  65. #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
  66. /* clear bits */
  67. #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
  68. /* CRC polynomium used by the FEC for the multicast group filtering */
  69. #define FEC_CRC_POLY 0x04C11DB7
  70. #define FEC_MAX_MULTICAST_ADDRS 64
  71. /* Interrupt events/masks.
  72. */
  73. #define FEC_ENET_HBERR 0x80000000U /* Heartbeat error */
  74. #define FEC_ENET_BABR 0x40000000U /* Babbling receiver */
  75. #define FEC_ENET_BABT 0x20000000U /* Babbling transmitter */
  76. #define FEC_ENET_GRA 0x10000000U /* Graceful stop complete */
  77. #define FEC_ENET_TXF 0x08000000U /* Full frame transmitted */
  78. #define FEC_ENET_TXB 0x04000000U /* A buffer was transmitted */
  79. #define FEC_ENET_RXF 0x02000000U /* Full frame received */
  80. #define FEC_ENET_RXB 0x01000000U /* A buffer was received */
  81. #define FEC_ENET_MII 0x00800000U /* MII interrupt */
  82. #define FEC_ENET_EBERR 0x00400000U /* SDMA bus error */
  83. #define FEC_ECNTRL_PINMUX 0x00000004
  84. #define FEC_ECNTRL_ETHER_EN 0x00000002
  85. #define FEC_ECNTRL_RESET 0x00000001
  86. #define FEC_RCNTRL_BC_REJ 0x00000010
  87. #define FEC_RCNTRL_PROM 0x00000008
  88. #define FEC_RCNTRL_MII_MODE 0x00000004
  89. #define FEC_RCNTRL_DRT 0x00000002
  90. #define FEC_RCNTRL_LOOP 0x00000001
  91. #define FEC_TCNTRL_FDEN 0x00000004
  92. #define FEC_TCNTRL_HBC 0x00000002
  93. #define FEC_TCNTRL_GTS 0x00000001
  94. /* Make MII read/write commands for the FEC.
  95. */
  96. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  97. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
  98. #define mk_mii_end 0
  99. #define FEC_MII_LOOPS 10000
  100. /*
  101. * Delay to wait for FEC reset command to complete (in us)
  102. */
  103. #define FEC_RESET_DELAY 50
  104. static int whack_reset(fec_t * fecp)
  105. {
  106. int i;
  107. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
  108. for (i = 0; i < FEC_RESET_DELAY; i++) {
  109. if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
  110. return 0; /* OK */
  111. udelay(1);
  112. }
  113. return -1;
  114. }
  115. static int do_pd_setup(struct fs_enet_private *fep)
  116. {
  117. struct platform_device *pdev = to_platform_device(fep->dev);
  118. struct resource *r;
  119. /* Fill out IRQ field */
  120. fep->interrupt = platform_get_irq_byname(pdev,"interrupt");
  121. if (fep->interrupt < 0)
  122. return -EINVAL;
  123. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  124. fep->fec.fecp =(void*)r->start;
  125. if(fep->fec.fecp == NULL)
  126. return -EINVAL;
  127. return 0;
  128. }
  129. #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
  130. #define FEC_RX_EVENT (FEC_ENET_RXF)
  131. #define FEC_TX_EVENT (FEC_ENET_TXF)
  132. #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
  133. FEC_ENET_BABT | FEC_ENET_EBERR)
  134. static int setup_data(struct net_device *dev)
  135. {
  136. struct fs_enet_private *fep = netdev_priv(dev);
  137. if (do_pd_setup(fep) != 0)
  138. return -EINVAL;
  139. fep->fec.hthi = 0;
  140. fep->fec.htlo = 0;
  141. fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
  142. fep->ev_rx = FEC_RX_EVENT;
  143. fep->ev_tx = FEC_TX_EVENT;
  144. fep->ev_err = FEC_ERR_EVENT_MSK;
  145. return 0;
  146. }
  147. static int allocate_bd(struct net_device *dev)
  148. {
  149. struct fs_enet_private *fep = netdev_priv(dev);
  150. const struct fs_platform_info *fpi = fep->fpi;
  151. fep->ring_base = dma_alloc_coherent(fep->dev,
  152. (fpi->tx_ring + fpi->rx_ring) *
  153. sizeof(cbd_t), &fep->ring_mem_addr,
  154. GFP_KERNEL);
  155. if (fep->ring_base == NULL)
  156. return -ENOMEM;
  157. return 0;
  158. }
  159. static void free_bd(struct net_device *dev)
  160. {
  161. struct fs_enet_private *fep = netdev_priv(dev);
  162. const struct fs_platform_info *fpi = fep->fpi;
  163. if(fep->ring_base)
  164. dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
  165. * sizeof(cbd_t),
  166. fep->ring_base,
  167. fep->ring_mem_addr);
  168. }
  169. static void cleanup_data(struct net_device *dev)
  170. {
  171. /* nothing */
  172. }
  173. static void set_promiscuous_mode(struct net_device *dev)
  174. {
  175. struct fs_enet_private *fep = netdev_priv(dev);
  176. fec_t *fecp = fep->fec.fecp;
  177. FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
  178. }
  179. static void set_multicast_start(struct net_device *dev)
  180. {
  181. struct fs_enet_private *fep = netdev_priv(dev);
  182. fep->fec.hthi = 0;
  183. fep->fec.htlo = 0;
  184. }
  185. static void set_multicast_one(struct net_device *dev, const u8 *mac)
  186. {
  187. struct fs_enet_private *fep = netdev_priv(dev);
  188. int temp, hash_index, i, j;
  189. u32 crc, csrVal;
  190. u8 byte, msb;
  191. crc = 0xffffffff;
  192. for (i = 0; i < 6; i++) {
  193. byte = mac[i];
  194. for (j = 0; j < 8; j++) {
  195. msb = crc >> 31;
  196. crc <<= 1;
  197. if (msb ^ (byte & 0x1))
  198. crc ^= FEC_CRC_POLY;
  199. byte >>= 1;
  200. }
  201. }
  202. temp = (crc & 0x3f) >> 1;
  203. hash_index = ((temp & 0x01) << 4) |
  204. ((temp & 0x02) << 2) |
  205. ((temp & 0x04)) |
  206. ((temp & 0x08) >> 2) |
  207. ((temp & 0x10) >> 4);
  208. csrVal = 1 << hash_index;
  209. if (crc & 1)
  210. fep->fec.hthi |= csrVal;
  211. else
  212. fep->fec.htlo |= csrVal;
  213. }
  214. static void set_multicast_finish(struct net_device *dev)
  215. {
  216. struct fs_enet_private *fep = netdev_priv(dev);
  217. fec_t *fecp = fep->fec.fecp;
  218. /* if all multi or too many multicasts; just enable all */
  219. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  220. dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
  221. fep->fec.hthi = 0xffffffffU;
  222. fep->fec.htlo = 0xffffffffU;
  223. }
  224. FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
  225. FW(fecp, hash_table_high, fep->fec.hthi);
  226. FW(fecp, hash_table_low, fep->fec.htlo);
  227. }
  228. static void set_multicast_list(struct net_device *dev)
  229. {
  230. struct dev_mc_list *pmc;
  231. if ((dev->flags & IFF_PROMISC) == 0) {
  232. set_multicast_start(dev);
  233. for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
  234. set_multicast_one(dev, pmc->dmi_addr);
  235. set_multicast_finish(dev);
  236. } else
  237. set_promiscuous_mode(dev);
  238. }
  239. static void restart(struct net_device *dev)
  240. {
  241. #ifdef CONFIG_DUET
  242. immap_t *immap = fs_enet_immap;
  243. u32 cptr;
  244. #endif
  245. struct fs_enet_private *fep = netdev_priv(dev);
  246. fec_t *fecp = fep->fec.fecp;
  247. const struct fs_platform_info *fpi = fep->fpi;
  248. dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
  249. int r;
  250. u32 addrhi, addrlo;
  251. r = whack_reset(fep->fec.fecp);
  252. if (r != 0)
  253. printk(KERN_ERR DRV_MODULE_NAME
  254. ": %s FEC Reset FAILED!\n", dev->name);
  255. /*
  256. * Set station address.
  257. */
  258. addrhi = ((u32) dev->dev_addr[0] << 24) |
  259. ((u32) dev->dev_addr[1] << 16) |
  260. ((u32) dev->dev_addr[2] << 8) |
  261. (u32) dev->dev_addr[3];
  262. addrlo = ((u32) dev->dev_addr[4] << 24) |
  263. ((u32) dev->dev_addr[5] << 16);
  264. FW(fecp, addr_low, addrhi);
  265. FW(fecp, addr_high, addrlo);
  266. /*
  267. * Reset all multicast.
  268. */
  269. FW(fecp, hash_table_high, fep->fec.hthi);
  270. FW(fecp, hash_table_low, fep->fec.htlo);
  271. /*
  272. * Set maximum receive buffer size.
  273. */
  274. FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
  275. FW(fecp, r_hash, PKT_MAXBUF_SIZE);
  276. /* get physical address */
  277. rx_bd_base_phys = fep->ring_mem_addr;
  278. tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
  279. /*
  280. * Set receive and transmit descriptor base.
  281. */
  282. FW(fecp, r_des_start, rx_bd_base_phys);
  283. FW(fecp, x_des_start, tx_bd_base_phys);
  284. fs_init_bds(dev);
  285. /*
  286. * Enable big endian and don't care about SDMA FC.
  287. */
  288. FW(fecp, fun_code, 0x78000000);
  289. /*
  290. * Set MII speed.
  291. */
  292. FW(fecp, mii_speed, fep->mii_bus->fec.mii_speed);
  293. /*
  294. * Clear any outstanding interrupt.
  295. */
  296. FW(fecp, ievent, 0xffc0);
  297. FW(fecp, ivec, (fep->interrupt / 2) << 29);
  298. /*
  299. * adjust to speed (only for DUET & RMII)
  300. */
  301. #ifdef CONFIG_DUET
  302. if (fpi->use_rmii) {
  303. cptr = in_be32(&immap->im_cpm.cp_cptr);
  304. switch (fs_get_fec_index(fpi->fs_no)) {
  305. case 0:
  306. cptr |= 0x100;
  307. if (fep->speed == 10)
  308. cptr |= 0x0000010;
  309. else if (fep->speed == 100)
  310. cptr &= ~0x0000010;
  311. break;
  312. case 1:
  313. cptr |= 0x80;
  314. if (fep->speed == 10)
  315. cptr |= 0x0000008;
  316. else if (fep->speed == 100)
  317. cptr &= ~0x0000008;
  318. break;
  319. default:
  320. BUG(); /* should never happen */
  321. break;
  322. }
  323. out_be32(&immap->im_cpm.cp_cptr, cptr);
  324. }
  325. #endif
  326. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  327. /*
  328. * adjust to duplex mode
  329. */
  330. if (fep->duplex) {
  331. FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
  332. FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  333. } else {
  334. FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
  335. FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  336. }
  337. /*
  338. * Enable interrupts we wish to service.
  339. */
  340. FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
  341. FEC_ENET_RXF | FEC_ENET_RXB);
  342. /*
  343. * And last, enable the transmit and receive processing.
  344. */
  345. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  346. FW(fecp, r_des_active, 0x01000000);
  347. }
  348. static void stop(struct net_device *dev)
  349. {
  350. struct fs_enet_private *fep = netdev_priv(dev);
  351. fec_t *fecp = fep->fec.fecp;
  352. struct fs_enet_mii_bus *bus = fep->mii_bus;
  353. const struct fs_mii_bus_info *bi = bus->bus_info;
  354. int i;
  355. if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
  356. return; /* already down */
  357. FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
  358. for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
  359. i < FEC_RESET_DELAY; i++)
  360. udelay(1);
  361. if (i == FEC_RESET_DELAY)
  362. printk(KERN_WARNING DRV_MODULE_NAME
  363. ": %s FEC timeout on graceful transmit stop\n",
  364. dev->name);
  365. /*
  366. * Disable FEC. Let only MII interrupts.
  367. */
  368. FW(fecp, imask, 0);
  369. FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
  370. fs_cleanup_bds(dev);
  371. /* shut down FEC1? that's where the mii bus is */
  372. if (fep->fec.idx == 0 && bus->refs > 1 && bi->method == fsmii_fec) {
  373. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  374. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  375. FW(fecp, ievent, FEC_ENET_MII);
  376. FW(fecp, mii_speed, bus->fec.mii_speed);
  377. }
  378. }
  379. static void pre_request_irq(struct net_device *dev, int irq)
  380. {
  381. immap_t *immap = fs_enet_immap;
  382. u32 siel;
  383. /* SIU interrupt */
  384. if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
  385. siel = in_be32(&immap->im_siu_conf.sc_siel);
  386. if ((irq & 1) == 0)
  387. siel |= (0x80000000 >> irq);
  388. else
  389. siel &= ~(0x80000000 >> (irq & ~1));
  390. out_be32(&immap->im_siu_conf.sc_siel, siel);
  391. }
  392. }
  393. static void post_free_irq(struct net_device *dev, int irq)
  394. {
  395. /* nothing */
  396. }
  397. static void napi_clear_rx_event(struct net_device *dev)
  398. {
  399. struct fs_enet_private *fep = netdev_priv(dev);
  400. fec_t *fecp = fep->fec.fecp;
  401. FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
  402. }
  403. static void napi_enable_rx(struct net_device *dev)
  404. {
  405. struct fs_enet_private *fep = netdev_priv(dev);
  406. fec_t *fecp = fep->fec.fecp;
  407. FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  408. }
  409. static void napi_disable_rx(struct net_device *dev)
  410. {
  411. struct fs_enet_private *fep = netdev_priv(dev);
  412. fec_t *fecp = fep->fec.fecp;
  413. FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  414. }
  415. static void rx_bd_done(struct net_device *dev)
  416. {
  417. struct fs_enet_private *fep = netdev_priv(dev);
  418. fec_t *fecp = fep->fec.fecp;
  419. FW(fecp, r_des_active, 0x01000000);
  420. }
  421. static void tx_kickstart(struct net_device *dev)
  422. {
  423. struct fs_enet_private *fep = netdev_priv(dev);
  424. fec_t *fecp = fep->fec.fecp;
  425. FW(fecp, x_des_active, 0x01000000);
  426. }
  427. static u32 get_int_events(struct net_device *dev)
  428. {
  429. struct fs_enet_private *fep = netdev_priv(dev);
  430. fec_t *fecp = fep->fec.fecp;
  431. return FR(fecp, ievent) & FR(fecp, imask);
  432. }
  433. static void clear_int_events(struct net_device *dev, u32 int_events)
  434. {
  435. struct fs_enet_private *fep = netdev_priv(dev);
  436. fec_t *fecp = fep->fec.fecp;
  437. FW(fecp, ievent, int_events);
  438. }
  439. static void ev_error(struct net_device *dev, u32 int_events)
  440. {
  441. printk(KERN_WARNING DRV_MODULE_NAME
  442. ": %s FEC ERROR(s) 0x%x\n", dev->name, int_events);
  443. }
  444. int get_regs(struct net_device *dev, void *p, int *sizep)
  445. {
  446. struct fs_enet_private *fep = netdev_priv(dev);
  447. if (*sizep < sizeof(fec_t))
  448. return -EINVAL;
  449. memcpy_fromio(p, fep->fec.fecp, sizeof(fec_t));
  450. return 0;
  451. }
  452. int get_regs_len(struct net_device *dev)
  453. {
  454. return sizeof(fec_t);
  455. }
  456. void tx_restart(struct net_device *dev)
  457. {
  458. /* nothing */
  459. }
  460. /*************************************************************************/
  461. const struct fs_ops fs_fec_ops = {
  462. .setup_data = setup_data,
  463. .cleanup_data = cleanup_data,
  464. .set_multicast_list = set_multicast_list,
  465. .restart = restart,
  466. .stop = stop,
  467. .pre_request_irq = pre_request_irq,
  468. .post_free_irq = post_free_irq,
  469. .napi_clear_rx_event = napi_clear_rx_event,
  470. .napi_enable_rx = napi_enable_rx,
  471. .napi_disable_rx = napi_disable_rx,
  472. .rx_bd_done = rx_bd_done,
  473. .tx_kickstart = tx_kickstart,
  474. .get_int_events = get_int_events,
  475. .clear_int_events = clear_int_events,
  476. .ev_error = ev_error,
  477. .get_regs = get_regs,
  478. .get_regs_len = get_regs_len,
  479. .tx_restart = tx_restart,
  480. .allocate_bd = allocate_bd,
  481. .free_bd = free_bd,
  482. };
  483. /***********************************************************************/
  484. static int mii_read(struct fs_enet_mii_bus *bus, int phy_id, int location)
  485. {
  486. fec_t *fecp = bus->fec.fecp;
  487. int i, ret = -1;
  488. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  489. BUG();
  490. /* Add PHY address to register command. */
  491. FW(fecp, mii_data, (phy_id << 23) | mk_mii_read(location));
  492. for (i = 0; i < FEC_MII_LOOPS; i++)
  493. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  494. break;
  495. if (i < FEC_MII_LOOPS) {
  496. FW(fecp, ievent, FEC_ENET_MII);
  497. ret = FR(fecp, mii_data) & 0xffff;
  498. }
  499. return ret;
  500. }
  501. static void mii_write(struct fs_enet_mii_bus *bus, int phy_id, int location, int value)
  502. {
  503. fec_t *fecp = bus->fec.fecp;
  504. int i;
  505. /* this must never happen */
  506. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  507. BUG();
  508. /* Add PHY address to register command. */
  509. FW(fecp, mii_data, (phy_id << 23) | mk_mii_write(location, value));
  510. for (i = 0; i < FEC_MII_LOOPS; i++)
  511. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  512. break;
  513. if (i < FEC_MII_LOOPS)
  514. FW(fecp, ievent, FEC_ENET_MII);
  515. }
  516. int fs_mii_fec_init(struct fs_enet_mii_bus *bus)
  517. {
  518. bd_t *bd = (bd_t *)__res;
  519. const struct fs_mii_bus_info *bi = bus->bus_info;
  520. fec_t *fecp;
  521. if (bi->id != 0)
  522. return -1;
  523. bus->fec.fecp = &((immap_t *)fs_enet_immap)->im_cpm.cp_fec;
  524. bus->fec.mii_speed = ((((bd->bi_intfreq + 4999999) / 2500000) / 2)
  525. & 0x3F) << 1;
  526. fecp = bus->fec.fecp;
  527. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  528. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  529. FW(fecp, ievent, FEC_ENET_MII);
  530. FW(fecp, mii_speed, bus->fec.mii_speed);
  531. bus->mii_read = mii_read;
  532. bus->mii_write = mii_write;
  533. return 0;
  534. }