mac-fec.c 12 KB

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