mac-fec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mii.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/bitops.h>
  32. #include <linux/fs.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/of_device.h>
  35. #include <asm/irq.h>
  36. #include <asm/uaccess.h>
  37. #ifdef CONFIG_8xx
  38. #include <asm/8xx_immap.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/mpc8xx.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(fec_t __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 of_device *ofdev = to_of_device(fep->dev);
  85. fep->interrupt = of_irq_to_resource(ofdev->node, 0, NULL);
  86. if (fep->interrupt == NO_IRQ)
  87. return -EINVAL;
  88. fep->fec.fecp = of_iomap(ofdev->node, 0);
  89. if (!fep->fcc.fccp)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
  94. #define FEC_RX_EVENT (FEC_ENET_RXF)
  95. #define FEC_TX_EVENT (FEC_ENET_TXF)
  96. #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
  97. FEC_ENET_BABT | FEC_ENET_EBERR)
  98. static int setup_data(struct net_device *dev)
  99. {
  100. struct fs_enet_private *fep = netdev_priv(dev);
  101. if (do_pd_setup(fep) != 0)
  102. return -EINVAL;
  103. fep->fec.hthi = 0;
  104. fep->fec.htlo = 0;
  105. fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
  106. fep->ev_rx = FEC_RX_EVENT;
  107. fep->ev_tx = FEC_TX_EVENT;
  108. fep->ev_err = FEC_ERR_EVENT_MSK;
  109. return 0;
  110. }
  111. static int allocate_bd(struct net_device *dev)
  112. {
  113. struct fs_enet_private *fep = netdev_priv(dev);
  114. const struct fs_platform_info *fpi = fep->fpi;
  115. fep->ring_base = (void __force __iomem *)dma_alloc_coherent(fep->dev,
  116. (fpi->tx_ring + fpi->rx_ring) *
  117. sizeof(cbd_t), &fep->ring_mem_addr,
  118. GFP_KERNEL);
  119. if (fep->ring_base == NULL)
  120. return -ENOMEM;
  121. return 0;
  122. }
  123. static void free_bd(struct net_device *dev)
  124. {
  125. struct fs_enet_private *fep = netdev_priv(dev);
  126. const struct fs_platform_info *fpi = fep->fpi;
  127. if(fep->ring_base)
  128. dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
  129. * sizeof(cbd_t),
  130. (void __force *)fep->ring_base,
  131. fep->ring_mem_addr);
  132. }
  133. static void cleanup_data(struct net_device *dev)
  134. {
  135. /* nothing */
  136. }
  137. static void set_promiscuous_mode(struct net_device *dev)
  138. {
  139. struct fs_enet_private *fep = netdev_priv(dev);
  140. fec_t __iomem *fecp = fep->fec.fecp;
  141. FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
  142. }
  143. static void set_multicast_start(struct net_device *dev)
  144. {
  145. struct fs_enet_private *fep = netdev_priv(dev);
  146. fep->fec.hthi = 0;
  147. fep->fec.htlo = 0;
  148. }
  149. static void set_multicast_one(struct net_device *dev, const u8 *mac)
  150. {
  151. struct fs_enet_private *fep = netdev_priv(dev);
  152. int temp, hash_index, i, j;
  153. u32 crc, csrVal;
  154. u8 byte, msb;
  155. crc = 0xffffffff;
  156. for (i = 0; i < 6; i++) {
  157. byte = mac[i];
  158. for (j = 0; j < 8; j++) {
  159. msb = crc >> 31;
  160. crc <<= 1;
  161. if (msb ^ (byte & 0x1))
  162. crc ^= FEC_CRC_POLY;
  163. byte >>= 1;
  164. }
  165. }
  166. temp = (crc & 0x3f) >> 1;
  167. hash_index = ((temp & 0x01) << 4) |
  168. ((temp & 0x02) << 2) |
  169. ((temp & 0x04)) |
  170. ((temp & 0x08) >> 2) |
  171. ((temp & 0x10) >> 4);
  172. csrVal = 1 << hash_index;
  173. if (crc & 1)
  174. fep->fec.hthi |= csrVal;
  175. else
  176. fep->fec.htlo |= csrVal;
  177. }
  178. static void set_multicast_finish(struct net_device *dev)
  179. {
  180. struct fs_enet_private *fep = netdev_priv(dev);
  181. fec_t __iomem *fecp = fep->fec.fecp;
  182. /* if all multi or too many multicasts; just enable all */
  183. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  184. dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
  185. fep->fec.hthi = 0xffffffffU;
  186. fep->fec.htlo = 0xffffffffU;
  187. }
  188. FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
  189. FW(fecp, hash_table_high, fep->fec.hthi);
  190. FW(fecp, hash_table_low, fep->fec.htlo);
  191. }
  192. static void set_multicast_list(struct net_device *dev)
  193. {
  194. struct dev_mc_list *pmc;
  195. if ((dev->flags & IFF_PROMISC) == 0) {
  196. set_multicast_start(dev);
  197. for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
  198. set_multicast_one(dev, pmc->dmi_addr);
  199. set_multicast_finish(dev);
  200. } else
  201. set_promiscuous_mode(dev);
  202. }
  203. static void restart(struct net_device *dev)
  204. {
  205. struct fs_enet_private *fep = netdev_priv(dev);
  206. fec_t __iomem *fecp = fep->fec.fecp;
  207. const struct fs_platform_info *fpi = fep->fpi;
  208. dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
  209. int r;
  210. u32 addrhi, addrlo;
  211. struct mii_bus* mii = fep->phydev->bus;
  212. struct fec_info* fec_inf = mii->priv;
  213. r = whack_reset(fep->fec.fecp);
  214. if (r != 0)
  215. printk(KERN_ERR DRV_MODULE_NAME
  216. ": %s FEC Reset FAILED!\n", dev->name);
  217. /*
  218. * Set station address.
  219. */
  220. addrhi = ((u32) dev->dev_addr[0] << 24) |
  221. ((u32) dev->dev_addr[1] << 16) |
  222. ((u32) dev->dev_addr[2] << 8) |
  223. (u32) dev->dev_addr[3];
  224. addrlo = ((u32) dev->dev_addr[4] << 24) |
  225. ((u32) dev->dev_addr[5] << 16);
  226. FW(fecp, addr_low, addrhi);
  227. FW(fecp, addr_high, addrlo);
  228. /*
  229. * Reset all multicast.
  230. */
  231. FW(fecp, hash_table_high, fep->fec.hthi);
  232. FW(fecp, hash_table_low, fep->fec.htlo);
  233. /*
  234. * Set maximum receive buffer size.
  235. */
  236. FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
  237. FW(fecp, r_hash, PKT_MAXBUF_SIZE);
  238. /* get physical address */
  239. rx_bd_base_phys = fep->ring_mem_addr;
  240. tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
  241. /*
  242. * Set receive and transmit descriptor base.
  243. */
  244. FW(fecp, r_des_start, rx_bd_base_phys);
  245. FW(fecp, x_des_start, tx_bd_base_phys);
  246. fs_init_bds(dev);
  247. /*
  248. * Enable big endian and don't care about SDMA FC.
  249. */
  250. FW(fecp, fun_code, 0x78000000);
  251. /*
  252. * Set MII speed.
  253. */
  254. FW(fecp, mii_speed, fec_inf->mii_speed);
  255. /*
  256. * Clear any outstanding interrupt.
  257. */
  258. FW(fecp, ievent, 0xffc0);
  259. FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
  260. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  261. /*
  262. * adjust to duplex mode
  263. */
  264. if (fep->phydev->duplex) {
  265. FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
  266. FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  267. } else {
  268. FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
  269. FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  270. }
  271. /*
  272. * Enable interrupts we wish to service.
  273. */
  274. FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
  275. FEC_ENET_RXF | FEC_ENET_RXB);
  276. /*
  277. * And last, enable the transmit and receive processing.
  278. */
  279. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  280. FW(fecp, r_des_active, 0x01000000);
  281. }
  282. static void stop(struct net_device *dev)
  283. {
  284. struct fs_enet_private *fep = netdev_priv(dev);
  285. const struct fs_platform_info *fpi = fep->fpi;
  286. fec_t __iomem *fecp = fep->fec.fecp;
  287. struct fec_info* feci= fep->phydev->bus->priv;
  288. int i;
  289. if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
  290. return; /* already down */
  291. FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
  292. for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
  293. i < FEC_RESET_DELAY; i++)
  294. udelay(1);
  295. if (i == FEC_RESET_DELAY)
  296. printk(KERN_WARNING DRV_MODULE_NAME
  297. ": %s FEC timeout on graceful transmit stop\n",
  298. dev->name);
  299. /*
  300. * Disable FEC. Let only MII interrupts.
  301. */
  302. FW(fecp, imask, 0);
  303. FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
  304. fs_cleanup_bds(dev);
  305. /* shut down FEC1? that's where the mii bus is */
  306. if (fpi->has_phy) {
  307. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  308. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  309. FW(fecp, ievent, FEC_ENET_MII);
  310. FW(fecp, mii_speed, feci->mii_speed);
  311. }
  312. }
  313. static void napi_clear_rx_event(struct net_device *dev)
  314. {
  315. struct fs_enet_private *fep = netdev_priv(dev);
  316. fec_t __iomem *fecp = fep->fec.fecp;
  317. FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
  318. }
  319. static void napi_enable_rx(struct net_device *dev)
  320. {
  321. struct fs_enet_private *fep = netdev_priv(dev);
  322. fec_t __iomem *fecp = fep->fec.fecp;
  323. FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  324. }
  325. static void napi_disable_rx(struct net_device *dev)
  326. {
  327. struct fs_enet_private *fep = netdev_priv(dev);
  328. fec_t __iomem *fecp = fep->fec.fecp;
  329. FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
  330. }
  331. static void rx_bd_done(struct net_device *dev)
  332. {
  333. struct fs_enet_private *fep = netdev_priv(dev);
  334. fec_t __iomem *fecp = fep->fec.fecp;
  335. FW(fecp, r_des_active, 0x01000000);
  336. }
  337. static void tx_kickstart(struct net_device *dev)
  338. {
  339. struct fs_enet_private *fep = netdev_priv(dev);
  340. fec_t __iomem *fecp = fep->fec.fecp;
  341. FW(fecp, x_des_active, 0x01000000);
  342. }
  343. static u32 get_int_events(struct net_device *dev)
  344. {
  345. struct fs_enet_private *fep = netdev_priv(dev);
  346. fec_t __iomem *fecp = fep->fec.fecp;
  347. return FR(fecp, ievent) & FR(fecp, imask);
  348. }
  349. static void clear_int_events(struct net_device *dev, u32 int_events)
  350. {
  351. struct fs_enet_private *fep = netdev_priv(dev);
  352. fec_t __iomem *fecp = fep->fec.fecp;
  353. FW(fecp, ievent, int_events);
  354. }
  355. static void ev_error(struct net_device *dev, u32 int_events)
  356. {
  357. printk(KERN_WARNING DRV_MODULE_NAME
  358. ": %s FEC ERROR(s) 0x%x\n", dev->name, int_events);
  359. }
  360. static int get_regs(struct net_device *dev, void *p, int *sizep)
  361. {
  362. struct fs_enet_private *fep = netdev_priv(dev);
  363. if (*sizep < sizeof(fec_t))
  364. return -EINVAL;
  365. memcpy_fromio(p, fep->fec.fecp, sizeof(fec_t));
  366. return 0;
  367. }
  368. static int get_regs_len(struct net_device *dev)
  369. {
  370. return sizeof(fec_t);
  371. }
  372. static void tx_restart(struct net_device *dev)
  373. {
  374. /* nothing */
  375. }
  376. /*************************************************************************/
  377. const struct fs_ops fs_fec_ops = {
  378. .setup_data = setup_data,
  379. .cleanup_data = cleanup_data,
  380. .set_multicast_list = set_multicast_list,
  381. .restart = restart,
  382. .stop = stop,
  383. .napi_clear_rx_event = napi_clear_rx_event,
  384. .napi_enable_rx = napi_enable_rx,
  385. .napi_disable_rx = napi_disable_rx,
  386. .rx_bd_done = rx_bd_done,
  387. .tx_kickstart = tx_kickstart,
  388. .get_int_events = get_int_events,
  389. .clear_int_events = clear_int_events,
  390. .ev_error = ev_error,
  391. .get_regs = get_regs,
  392. .get_regs_len = get_regs_len,
  393. .tx_restart = tx_restart,
  394. .allocate_bd = allocate_bd,
  395. .free_bd = free_bd,
  396. };