mac-fec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioport.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/bitops.h>
  31. #include <linux/fs.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_device.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/gfp.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/cpm1.h>
  44. #endif
  45. #include "fs_enet.h"
  46. #include "fec.h"
  47. /*************************************************/
  48. #if defined(CONFIG_CPM1)
  49. /* for a CPM1 __raw_xxx's are sufficient */
  50. #define __fs_out32(addr, x) __raw_writel(x, addr)
  51. #define __fs_out16(addr, x) __raw_writew(x, addr)
  52. #define __fs_in32(addr) __raw_readl(addr)
  53. #define __fs_in16(addr) __raw_readw(addr)
  54. #else
  55. /* for others play it safe */
  56. #define __fs_out32(addr, x) out_be32(addr, x)
  57. #define __fs_out16(addr, x) out_be16(addr, x)
  58. #define __fs_in32(addr) in_be32(addr)
  59. #define __fs_in16(addr) in_be16(addr)
  60. #endif
  61. /* write */
  62. #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
  63. /* read */
  64. #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
  65. /* set bits */
  66. #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
  67. /* clear bits */
  68. #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
  69. /*
  70. * Delay to wait for FEC reset command to complete (in us)
  71. */
  72. #define FEC_RESET_DELAY 50
  73. static int whack_reset(struct fec __iomem *fecp)
  74. {
  75. int i;
  76. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
  77. for (i = 0; i < FEC_RESET_DELAY; i++) {
  78. if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
  79. return 0; /* OK */
  80. udelay(1);
  81. }
  82. return -1;
  83. }
  84. static int do_pd_setup(struct fs_enet_private *fep)
  85. {
  86. struct platform_device *ofdev = to_platform_device(fep->dev);
  87. fep->interrupt = of_irq_to_resource(ofdev->dev.of_node, 0, NULL);
  88. if (fep->interrupt == NO_IRQ)
  89. return -EINVAL;
  90. fep->fec.fecp = of_iomap(ofdev->dev.of_node, 0);
  91. if (!fep->fcc.fccp)
  92. return -EINVAL;
  93. return 0;
  94. }
  95. #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
  96. #define FEC_RX_EVENT (FEC_ENET_RXF)
  97. #define FEC_TX_EVENT (FEC_ENET_TXF)
  98. #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
  99. FEC_ENET_BABT | FEC_ENET_EBERR)
  100. static int setup_data(struct net_device *dev)
  101. {
  102. struct fs_enet_private *fep = netdev_priv(dev);
  103. if (do_pd_setup(fep) != 0)
  104. return -EINVAL;
  105. fep->fec.hthi = 0;
  106. fep->fec.htlo = 0;
  107. fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
  108. fep->ev_rx = FEC_RX_EVENT;
  109. fep->ev_tx = FEC_TX_EVENT;
  110. fep->ev_err = FEC_ERR_EVENT_MSK;
  111. return 0;
  112. }
  113. static int allocate_bd(struct net_device *dev)
  114. {
  115. struct fs_enet_private *fep = netdev_priv(dev);
  116. const struct fs_platform_info *fpi = fep->fpi;
  117. fep->ring_base = (void __force __iomem *)dma_alloc_coherent(fep->dev,
  118. (fpi->tx_ring + fpi->rx_ring) *
  119. sizeof(cbd_t), &fep->ring_mem_addr,
  120. GFP_KERNEL);
  121. if (fep->ring_base == NULL)
  122. return -ENOMEM;
  123. return 0;
  124. }
  125. static void free_bd(struct net_device *dev)
  126. {
  127. struct fs_enet_private *fep = netdev_priv(dev);
  128. const struct fs_platform_info *fpi = fep->fpi;
  129. if(fep->ring_base)
  130. dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
  131. * sizeof(cbd_t),
  132. (void __force *)fep->ring_base,
  133. fep->ring_mem_addr);
  134. }
  135. static void cleanup_data(struct net_device *dev)
  136. {
  137. /* nothing */
  138. }
  139. static void set_promiscuous_mode(struct net_device *dev)
  140. {
  141. struct fs_enet_private *fep = netdev_priv(dev);
  142. struct fec __iomem *fecp = fep->fec.fecp;
  143. FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
  144. }
  145. static void set_multicast_start(struct net_device *dev)
  146. {
  147. struct fs_enet_private *fep = netdev_priv(dev);
  148. fep->fec.hthi = 0;
  149. fep->fec.htlo = 0;
  150. }
  151. static void set_multicast_one(struct net_device *dev, const u8 *mac)
  152. {
  153. struct fs_enet_private *fep = netdev_priv(dev);
  154. int temp, hash_index, i, j;
  155. u32 crc, csrVal;
  156. u8 byte, msb;
  157. crc = 0xffffffff;
  158. for (i = 0; i < 6; i++) {
  159. byte = mac[i];
  160. for (j = 0; j < 8; j++) {
  161. msb = crc >> 31;
  162. crc <<= 1;
  163. if (msb ^ (byte & 0x1))
  164. crc ^= FEC_CRC_POLY;
  165. byte >>= 1;
  166. }
  167. }
  168. temp = (crc & 0x3f) >> 1;
  169. hash_index = ((temp & 0x01) << 4) |
  170. ((temp & 0x02) << 2) |
  171. ((temp & 0x04)) |
  172. ((temp & 0x08) >> 2) |
  173. ((temp & 0x10) >> 4);
  174. csrVal = 1 << hash_index;
  175. if (crc & 1)
  176. fep->fec.hthi |= csrVal;
  177. else
  178. fep->fec.htlo |= csrVal;
  179. }
  180. static void set_multicast_finish(struct net_device *dev)
  181. {
  182. struct fs_enet_private *fep = netdev_priv(dev);
  183. struct fec __iomem *fecp = fep->fec.fecp;
  184. /* if all multi or too many multicasts; just enable all */
  185. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  186. netdev_mc_count(dev) > FEC_MAX_MULTICAST_ADDRS) {
  187. fep->fec.hthi = 0xffffffffU;
  188. fep->fec.htlo = 0xffffffffU;
  189. }
  190. FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
  191. FW(fecp, grp_hash_table_high, fep->fec.hthi);
  192. FW(fecp, grp_hash_table_low, fep->fec.htlo);
  193. }
  194. static void set_multicast_list(struct net_device *dev)
  195. {
  196. struct netdev_hw_addr *ha;
  197. if ((dev->flags & IFF_PROMISC) == 0) {
  198. set_multicast_start(dev);
  199. netdev_for_each_mc_addr(ha, dev)
  200. set_multicast_one(dev, ha->addr);
  201. set_multicast_finish(dev);
  202. } else
  203. set_promiscuous_mode(dev);
  204. }
  205. static void restart(struct net_device *dev)
  206. {
  207. struct fs_enet_private *fep = netdev_priv(dev);
  208. struct fec __iomem *fecp = fep->fec.fecp;
  209. const struct fs_platform_info *fpi = fep->fpi;
  210. dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
  211. int r;
  212. u32 addrhi, addrlo;
  213. struct mii_bus* mii = fep->phydev->bus;
  214. struct fec_info* fec_inf = mii->priv;
  215. r = whack_reset(fep->fec.fecp);
  216. if (r != 0)
  217. dev_err(fep->dev, "FEC Reset FAILED!\n");
  218. /*
  219. * Set station address.
  220. */
  221. addrhi = ((u32) dev->dev_addr[0] << 24) |
  222. ((u32) dev->dev_addr[1] << 16) |
  223. ((u32) dev->dev_addr[2] << 8) |
  224. (u32) dev->dev_addr[3];
  225. addrlo = ((u32) dev->dev_addr[4] << 24) |
  226. ((u32) dev->dev_addr[5] << 16);
  227. FW(fecp, addr_low, addrhi);
  228. FW(fecp, addr_high, addrlo);
  229. /*
  230. * Reset all multicast.
  231. */
  232. FW(fecp, grp_hash_table_high, fep->fec.hthi);
  233. FW(fecp, grp_hash_table_low, fep->fec.htlo);
  234. /*
  235. * Set maximum receive buffer size.
  236. */
  237. FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
  238. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  239. FW(fecp, r_cntrl, PKT_MAXBUF_SIZE << 16);
  240. #else
  241. FW(fecp, r_hash, PKT_MAXBUF_SIZE);
  242. #endif
  243. /* get physical address */
  244. rx_bd_base_phys = fep->ring_mem_addr;
  245. tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
  246. /*
  247. * Set receive and transmit descriptor base.
  248. */
  249. FW(fecp, r_des_start, rx_bd_base_phys);
  250. FW(fecp, x_des_start, tx_bd_base_phys);
  251. fs_init_bds(dev);
  252. /*
  253. * Enable big endian and don't care about SDMA FC.
  254. */
  255. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  256. FS(fecp, dma_control, 0xC0000000);
  257. #else
  258. FW(fecp, fun_code, 0x78000000);
  259. #endif
  260. /*
  261. * Set MII speed.
  262. */
  263. FW(fecp, mii_speed, fec_inf->mii_speed);
  264. /*
  265. * Clear any outstanding interrupt.
  266. */
  267. FW(fecp, ievent, 0xffc0);
  268. #ifndef CONFIG_FS_ENET_MPC5121_FEC
  269. FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
  270. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  271. #else
  272. /*
  273. * Only set MII/RMII mode - do not touch maximum frame length
  274. * configured before.
  275. */
  276. FS(fecp, r_cntrl, fpi->use_rmii ?
  277. FEC_RCNTRL_RMII_MODE : FEC_RCNTRL_MII_MODE);
  278. #endif
  279. /*
  280. * adjust to duplex mode
  281. */
  282. if (fep->phydev->duplex) {
  283. FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
  284. FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  285. } else {
  286. FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
  287. FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  288. }
  289. /*
  290. * Enable interrupts we wish to service.
  291. */
  292. FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
  293. FEC_ENET_RXF | FEC_ENET_RXB);
  294. /*
  295. * And last, enable the transmit and receive processing.
  296. */
  297. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  298. FW(fecp, r_des_active, 0x01000000);
  299. }
  300. static void stop(struct net_device *dev)
  301. {
  302. struct fs_enet_private *fep = netdev_priv(dev);
  303. const struct fs_platform_info *fpi = fep->fpi;
  304. struct fec __iomem *fecp = fep->fec.fecp;
  305. struct fec_info* feci= fep->phydev->bus->priv;
  306. int i;
  307. if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
  308. return; /* already down */
  309. FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
  310. for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
  311. i < FEC_RESET_DELAY; i++)
  312. udelay(1);
  313. if (i == FEC_RESET_DELAY)
  314. dev_warn(fep->dev, "FEC timeout on graceful transmit stop\n");
  315. /*
  316. * Disable FEC. Let only MII interrupts.
  317. */
  318. FW(fecp, imask, 0);
  319. FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
  320. fs_cleanup_bds(dev);
  321. /* shut down FEC1? that's where the mii bus is */
  322. if (fpi->has_phy) {
  323. FS(fecp, r_cntrl, fpi->use_rmii ?
  324. FEC_RCNTRL_RMII_MODE :
  325. FEC_RCNTRL_MII_MODE); /* MII/RMII enable */
  326. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  327. FW(fecp, ievent, FEC_ENET_MII);
  328. FW(fecp, mii_speed, feci->mii_speed);
  329. }
  330. }
  331. static void napi_clear_rx_event(struct net_device *dev)
  332. {
  333. struct fs_enet_private *fep = netdev_priv(dev);
  334. struct fec __iomem *fecp = fep->fec.fecp;
  335. FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
  336. }
  337. static void napi_enable_rx(struct net_device *dev)
  338. {
  339. struct fs_enet_private *fep = netdev_priv(dev);
  340. struct fec __iomem *fecp = fep->fec.fecp;
  341. FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  342. }
  343. static void napi_disable_rx(struct net_device *dev)
  344. {
  345. struct fs_enet_private *fep = netdev_priv(dev);
  346. struct fec __iomem *fecp = fep->fec.fecp;
  347. FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  348. }
  349. static void rx_bd_done(struct net_device *dev)
  350. {
  351. struct fs_enet_private *fep = netdev_priv(dev);
  352. struct fec __iomem *fecp = fep->fec.fecp;
  353. FW(fecp, r_des_active, 0x01000000);
  354. }
  355. static void tx_kickstart(struct net_device *dev)
  356. {
  357. struct fs_enet_private *fep = netdev_priv(dev);
  358. struct fec __iomem *fecp = fep->fec.fecp;
  359. FW(fecp, x_des_active, 0x01000000);
  360. }
  361. static u32 get_int_events(struct net_device *dev)
  362. {
  363. struct fs_enet_private *fep = netdev_priv(dev);
  364. struct fec __iomem *fecp = fep->fec.fecp;
  365. return FR(fecp, ievent) & FR(fecp, imask);
  366. }
  367. static void clear_int_events(struct net_device *dev, u32 int_events)
  368. {
  369. struct fs_enet_private *fep = netdev_priv(dev);
  370. struct fec __iomem *fecp = fep->fec.fecp;
  371. FW(fecp, ievent, int_events);
  372. }
  373. static void ev_error(struct net_device *dev, u32 int_events)
  374. {
  375. struct fs_enet_private *fep = netdev_priv(dev);
  376. dev_warn(fep->dev, "FEC ERROR(s) 0x%x\n", int_events);
  377. }
  378. static int get_regs(struct net_device *dev, void *p, int *sizep)
  379. {
  380. struct fs_enet_private *fep = netdev_priv(dev);
  381. if (*sizep < sizeof(struct fec))
  382. return -EINVAL;
  383. memcpy_fromio(p, fep->fec.fecp, sizeof(struct fec));
  384. return 0;
  385. }
  386. static int get_regs_len(struct net_device *dev)
  387. {
  388. return sizeof(struct fec);
  389. }
  390. static void tx_restart(struct net_device *dev)
  391. {
  392. /* nothing */
  393. }
  394. /*************************************************************************/
  395. const struct fs_ops fs_fec_ops = {
  396. .setup_data = setup_data,
  397. .cleanup_data = cleanup_data,
  398. .set_multicast_list = set_multicast_list,
  399. .restart = restart,
  400. .stop = stop,
  401. .napi_clear_rx_event = napi_clear_rx_event,
  402. .napi_enable_rx = napi_enable_rx,
  403. .napi_disable_rx = napi_disable_rx,
  404. .rx_bd_done = rx_bd_done,
  405. .tx_kickstart = tx_kickstart,
  406. .get_int_events = get_int_events,
  407. .clear_int_events = clear_int_events,
  408. .ev_error = ev_error,
  409. .get_regs = get_regs,
  410. .get_regs_len = get_regs_len,
  411. .tx_restart = tx_restart,
  412. .allocate_bd = allocate_bd,
  413. .free_bd = free_bd,
  414. };