mac-fec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  122. fep->fec.fecp =(void*)r->start;
  123. if(fep->fec.fecp == NULL)
  124. return -EINVAL;
  125. return 0;
  126. }
  127. #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
  128. #define FEC_RX_EVENT (FEC_ENET_RXF)
  129. #define FEC_TX_EVENT (FEC_ENET_TXF)
  130. #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
  131. FEC_ENET_BABT | FEC_ENET_EBERR)
  132. static int setup_data(struct net_device *dev)
  133. {
  134. struct fs_enet_private *fep = netdev_priv(dev);
  135. if (do_pd_setup(fep) != 0)
  136. return -EINVAL;
  137. fep->fec.hthi = 0;
  138. fep->fec.htlo = 0;
  139. fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
  140. fep->ev_rx = FEC_RX_EVENT;
  141. fep->ev_tx = FEC_TX_EVENT;
  142. fep->ev_err = FEC_ERR_EVENT_MSK;
  143. return 0;
  144. }
  145. static int allocate_bd(struct net_device *dev)
  146. {
  147. struct fs_enet_private *fep = netdev_priv(dev);
  148. const struct fs_platform_info *fpi = fep->fpi;
  149. fep->ring_base = dma_alloc_coherent(fep->dev,
  150. (fpi->tx_ring + fpi->rx_ring) *
  151. sizeof(cbd_t), &fep->ring_mem_addr,
  152. GFP_KERNEL);
  153. if (fep->ring_base == NULL)
  154. return -ENOMEM;
  155. return 0;
  156. }
  157. static void free_bd(struct net_device *dev)
  158. {
  159. struct fs_enet_private *fep = netdev_priv(dev);
  160. const struct fs_platform_info *fpi = fep->fpi;
  161. if(fep->ring_base)
  162. dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
  163. * sizeof(cbd_t),
  164. fep->ring_base,
  165. fep->ring_mem_addr);
  166. }
  167. static void cleanup_data(struct net_device *dev)
  168. {
  169. /* nothing */
  170. }
  171. static void set_promiscuous_mode(struct net_device *dev)
  172. {
  173. struct fs_enet_private *fep = netdev_priv(dev);
  174. fec_t *fecp = fep->fec.fecp;
  175. FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
  176. }
  177. static void set_multicast_start(struct net_device *dev)
  178. {
  179. struct fs_enet_private *fep = netdev_priv(dev);
  180. fep->fec.hthi = 0;
  181. fep->fec.htlo = 0;
  182. }
  183. static void set_multicast_one(struct net_device *dev, const u8 *mac)
  184. {
  185. struct fs_enet_private *fep = netdev_priv(dev);
  186. int temp, hash_index, i, j;
  187. u32 crc, csrVal;
  188. u8 byte, msb;
  189. crc = 0xffffffff;
  190. for (i = 0; i < 6; i++) {
  191. byte = mac[i];
  192. for (j = 0; j < 8; j++) {
  193. msb = crc >> 31;
  194. crc <<= 1;
  195. if (msb ^ (byte & 0x1))
  196. crc ^= FEC_CRC_POLY;
  197. byte >>= 1;
  198. }
  199. }
  200. temp = (crc & 0x3f) >> 1;
  201. hash_index = ((temp & 0x01) << 4) |
  202. ((temp & 0x02) << 2) |
  203. ((temp & 0x04)) |
  204. ((temp & 0x08) >> 2) |
  205. ((temp & 0x10) >> 4);
  206. csrVal = 1 << hash_index;
  207. if (crc & 1)
  208. fep->fec.hthi |= csrVal;
  209. else
  210. fep->fec.htlo |= csrVal;
  211. }
  212. static void set_multicast_finish(struct net_device *dev)
  213. {
  214. struct fs_enet_private *fep = netdev_priv(dev);
  215. fec_t *fecp = fep->fec.fecp;
  216. /* if all multi or too many multicasts; just enable all */
  217. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  218. dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
  219. fep->fec.hthi = 0xffffffffU;
  220. fep->fec.htlo = 0xffffffffU;
  221. }
  222. FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
  223. FW(fecp, hash_table_high, fep->fec.hthi);
  224. FW(fecp, hash_table_low, fep->fec.htlo);
  225. }
  226. static void set_multicast_list(struct net_device *dev)
  227. {
  228. struct dev_mc_list *pmc;
  229. if ((dev->flags & IFF_PROMISC) == 0) {
  230. set_multicast_start(dev);
  231. for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
  232. set_multicast_one(dev, pmc->dmi_addr);
  233. set_multicast_finish(dev);
  234. } else
  235. set_promiscuous_mode(dev);
  236. }
  237. static void restart(struct net_device *dev)
  238. {
  239. #ifdef CONFIG_DUET
  240. immap_t *immap = fs_enet_immap;
  241. u32 cptr;
  242. #endif
  243. struct fs_enet_private *fep = netdev_priv(dev);
  244. fec_t *fecp = fep->fec.fecp;
  245. const struct fs_platform_info *fpi = fep->fpi;
  246. dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
  247. int r;
  248. u32 addrhi, addrlo;
  249. r = whack_reset(fep->fec.fecp);
  250. if (r != 0)
  251. printk(KERN_ERR DRV_MODULE_NAME
  252. ": %s FEC Reset FAILED!\n", dev->name);
  253. /*
  254. * Set station address.
  255. */
  256. addrhi = ((u32) dev->dev_addr[0] << 24) |
  257. ((u32) dev->dev_addr[1] << 16) |
  258. ((u32) dev->dev_addr[2] << 8) |
  259. (u32) dev->dev_addr[3];
  260. addrlo = ((u32) dev->dev_addr[4] << 24) |
  261. ((u32) dev->dev_addr[5] << 16);
  262. FW(fecp, addr_low, addrhi);
  263. FW(fecp, addr_high, addrlo);
  264. /*
  265. * Reset all multicast.
  266. */
  267. FW(fecp, hash_table_high, fep->fec.hthi);
  268. FW(fecp, hash_table_low, fep->fec.htlo);
  269. /*
  270. * Set maximum receive buffer size.
  271. */
  272. FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
  273. FW(fecp, r_hash, PKT_MAXBUF_SIZE);
  274. /* get physical address */
  275. rx_bd_base_phys = fep->ring_mem_addr;
  276. tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
  277. /*
  278. * Set receive and transmit descriptor base.
  279. */
  280. FW(fecp, r_des_start, rx_bd_base_phys);
  281. FW(fecp, x_des_start, tx_bd_base_phys);
  282. fs_init_bds(dev);
  283. /*
  284. * Enable big endian and don't care about SDMA FC.
  285. */
  286. FW(fecp, fun_code, 0x78000000);
  287. /*
  288. * Set MII speed.
  289. */
  290. FW(fecp, mii_speed, fep->mii_bus->fec.mii_speed);
  291. /*
  292. * Clear any outstanding interrupt.
  293. */
  294. FW(fecp, ievent, 0xffc0);
  295. FW(fecp, ivec, (fep->interrupt / 2) << 29);
  296. /*
  297. * adjust to speed (only for DUET & RMII)
  298. */
  299. #ifdef CONFIG_DUET
  300. if (fpi->use_rmii) {
  301. cptr = in_be32(&immap->im_cpm.cp_cptr);
  302. switch (fs_get_fec_index(fpi->fs_no)) {
  303. case 0:
  304. cptr |= 0x100;
  305. if (fep->speed == 10)
  306. cptr |= 0x0000010;
  307. else if (fep->speed == 100)
  308. cptr &= ~0x0000010;
  309. break;
  310. case 1:
  311. cptr |= 0x80;
  312. if (fep->speed == 10)
  313. cptr |= 0x0000008;
  314. else if (fep->speed == 100)
  315. cptr &= ~0x0000008;
  316. break;
  317. default:
  318. BUG(); /* should never happen */
  319. break;
  320. }
  321. out_be32(&immap->im_cpm.cp_cptr, cptr);
  322. }
  323. #endif
  324. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  325. /*
  326. * adjust to duplex mode
  327. */
  328. if (fep->duplex) {
  329. FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
  330. FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  331. } else {
  332. FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
  333. FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  334. }
  335. /*
  336. * Enable interrupts we wish to service.
  337. */
  338. FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
  339. FEC_ENET_RXF | FEC_ENET_RXB);
  340. /*
  341. * And last, enable the transmit and receive processing.
  342. */
  343. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  344. FW(fecp, r_des_active, 0x01000000);
  345. }
  346. static void stop(struct net_device *dev)
  347. {
  348. struct fs_enet_private *fep = netdev_priv(dev);
  349. fec_t *fecp = fep->fec.fecp;
  350. struct fs_enet_mii_bus *bus = fep->mii_bus;
  351. const struct fs_mii_bus_info *bi = bus->bus_info;
  352. int i;
  353. if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
  354. return; /* already down */
  355. FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
  356. for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
  357. i < FEC_RESET_DELAY; i++)
  358. udelay(1);
  359. if (i == FEC_RESET_DELAY)
  360. printk(KERN_WARNING DRV_MODULE_NAME
  361. ": %s FEC timeout on graceful transmit stop\n",
  362. dev->name);
  363. /*
  364. * Disable FEC. Let only MII interrupts.
  365. */
  366. FW(fecp, imask, 0);
  367. FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
  368. fs_cleanup_bds(dev);
  369. /* shut down FEC1? that's where the mii bus is */
  370. if (fep->fec.idx == 0 && bus->refs > 1 && bi->method == fsmii_fec) {
  371. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  372. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  373. FW(fecp, ievent, FEC_ENET_MII);
  374. FW(fecp, mii_speed, bus->fec.mii_speed);
  375. }
  376. }
  377. static void pre_request_irq(struct net_device *dev, int irq)
  378. {
  379. immap_t *immap = fs_enet_immap;
  380. u32 siel;
  381. /* SIU interrupt */
  382. if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
  383. siel = in_be32(&immap->im_siu_conf.sc_siel);
  384. if ((irq & 1) == 0)
  385. siel |= (0x80000000 >> irq);
  386. else
  387. siel &= ~(0x80000000 >> (irq & ~1));
  388. out_be32(&immap->im_siu_conf.sc_siel, siel);
  389. }
  390. }
  391. static void post_free_irq(struct net_device *dev, int irq)
  392. {
  393. /* nothing */
  394. }
  395. static void napi_clear_rx_event(struct net_device *dev)
  396. {
  397. struct fs_enet_private *fep = netdev_priv(dev);
  398. fec_t *fecp = fep->fec.fecp;
  399. FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
  400. }
  401. static void napi_enable_rx(struct net_device *dev)
  402. {
  403. struct fs_enet_private *fep = netdev_priv(dev);
  404. fec_t *fecp = fep->fec.fecp;
  405. FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  406. }
  407. static void napi_disable_rx(struct net_device *dev)
  408. {
  409. struct fs_enet_private *fep = netdev_priv(dev);
  410. fec_t *fecp = fep->fec.fecp;
  411. FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  412. }
  413. static void rx_bd_done(struct net_device *dev)
  414. {
  415. struct fs_enet_private *fep = netdev_priv(dev);
  416. fec_t *fecp = fep->fec.fecp;
  417. FW(fecp, r_des_active, 0x01000000);
  418. }
  419. static void tx_kickstart(struct net_device *dev)
  420. {
  421. struct fs_enet_private *fep = netdev_priv(dev);
  422. fec_t *fecp = fep->fec.fecp;
  423. FW(fecp, x_des_active, 0x01000000);
  424. }
  425. static u32 get_int_events(struct net_device *dev)
  426. {
  427. struct fs_enet_private *fep = netdev_priv(dev);
  428. fec_t *fecp = fep->fec.fecp;
  429. return FR(fecp, ievent) & FR(fecp, imask);
  430. }
  431. static void clear_int_events(struct net_device *dev, u32 int_events)
  432. {
  433. struct fs_enet_private *fep = netdev_priv(dev);
  434. fec_t *fecp = fep->fec.fecp;
  435. FW(fecp, ievent, int_events);
  436. }
  437. static void ev_error(struct net_device *dev, u32 int_events)
  438. {
  439. printk(KERN_WARNING DRV_MODULE_NAME
  440. ": %s FEC ERROR(s) 0x%x\n", dev->name, int_events);
  441. }
  442. int get_regs(struct net_device *dev, void *p, int *sizep)
  443. {
  444. struct fs_enet_private *fep = netdev_priv(dev);
  445. if (*sizep < sizeof(fec_t))
  446. return -EINVAL;
  447. memcpy_fromio(p, fep->fec.fecp, sizeof(fec_t));
  448. return 0;
  449. }
  450. int get_regs_len(struct net_device *dev)
  451. {
  452. return sizeof(fec_t);
  453. }
  454. void tx_restart(struct net_device *dev)
  455. {
  456. /* nothing */
  457. }
  458. /*************************************************************************/
  459. const struct fs_ops fs_fec_ops = {
  460. .setup_data = setup_data,
  461. .cleanup_data = cleanup_data,
  462. .set_multicast_list = set_multicast_list,
  463. .restart = restart,
  464. .stop = stop,
  465. .pre_request_irq = pre_request_irq,
  466. .post_free_irq = post_free_irq,
  467. .napi_clear_rx_event = napi_clear_rx_event,
  468. .napi_enable_rx = napi_enable_rx,
  469. .napi_disable_rx = napi_disable_rx,
  470. .rx_bd_done = rx_bd_done,
  471. .tx_kickstart = tx_kickstart,
  472. .get_int_events = get_int_events,
  473. .clear_int_events = clear_int_events,
  474. .ev_error = ev_error,
  475. .get_regs = get_regs,
  476. .get_regs_len = get_regs_len,
  477. .tx_restart = tx_restart,
  478. .allocate_bd = allocate_bd,
  479. .free_bd = free_bd,
  480. };
  481. /***********************************************************************/
  482. static int mii_read(struct fs_enet_mii_bus *bus, int phy_id, int location)
  483. {
  484. fec_t *fecp = bus->fec.fecp;
  485. int i, ret = -1;
  486. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  487. BUG();
  488. /* Add PHY address to register command. */
  489. FW(fecp, mii_data, (phy_id << 23) | mk_mii_read(location));
  490. for (i = 0; i < FEC_MII_LOOPS; i++)
  491. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  492. break;
  493. if (i < FEC_MII_LOOPS) {
  494. FW(fecp, ievent, FEC_ENET_MII);
  495. ret = FR(fecp, mii_data) & 0xffff;
  496. }
  497. return ret;
  498. }
  499. static void mii_write(struct fs_enet_mii_bus *bus, int phy_id, int location, int value)
  500. {
  501. fec_t *fecp = bus->fec.fecp;
  502. int i;
  503. /* this must never happen */
  504. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  505. BUG();
  506. /* Add PHY address to register command. */
  507. FW(fecp, mii_data, (phy_id << 23) | mk_mii_write(location, value));
  508. for (i = 0; i < FEC_MII_LOOPS; i++)
  509. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  510. break;
  511. if (i < FEC_MII_LOOPS)
  512. FW(fecp, ievent, FEC_ENET_MII);
  513. }
  514. int fs_mii_fec_init(struct fs_enet_mii_bus *bus)
  515. {
  516. bd_t *bd = (bd_t *)__res;
  517. const struct fs_mii_bus_info *bi = bus->bus_info;
  518. fec_t *fecp;
  519. if (bi->id != 0)
  520. return -1;
  521. bus->fec.fecp = &((immap_t *)fs_enet_immap)->im_cpm.cp_fec;
  522. bus->fec.mii_speed = ((((bd->bi_intfreq + 4999999) / 2500000) / 2)
  523. & 0x3F) << 1;
  524. fecp = bus->fec.fecp;
  525. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  526. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  527. FW(fecp, ievent, FEC_ENET_MII);
  528. FW(fecp, mii_speed, bus->fec.mii_speed);
  529. bus->mii_read = mii_read;
  530. bus->mii_write = mii_write;
  531. return 0;
  532. }