mac-scc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Ethernet on Serial Communications Controller (SCC) driver for Motorola MPC8xx and MPC82xx.
  3. *
  4. * Copyright (c) 2003 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_platform.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. /*************************************************/
  45. #if defined(CONFIG_CPM1)
  46. /* for a 8xx __raw_xxx's are sufficient */
  47. #define __fs_out32(addr, x) __raw_writel(x, addr)
  48. #define __fs_out16(addr, x) __raw_writew(x, addr)
  49. #define __fs_out8(addr, x) __raw_writeb(x, addr)
  50. #define __fs_in32(addr) __raw_readl(addr)
  51. #define __fs_in16(addr) __raw_readw(addr)
  52. #define __fs_in8(addr) __raw_readb(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, read, set bits, clear bits */
  61. #define W32(_p, _m, _v) __fs_out32(&(_p)->_m, (_v))
  62. #define R32(_p, _m) __fs_in32(&(_p)->_m)
  63. #define S32(_p, _m, _v) W32(_p, _m, R32(_p, _m) | (_v))
  64. #define C32(_p, _m, _v) W32(_p, _m, R32(_p, _m) & ~(_v))
  65. #define W16(_p, _m, _v) __fs_out16(&(_p)->_m, (_v))
  66. #define R16(_p, _m) __fs_in16(&(_p)->_m)
  67. #define S16(_p, _m, _v) W16(_p, _m, R16(_p, _m) | (_v))
  68. #define C16(_p, _m, _v) W16(_p, _m, R16(_p, _m) & ~(_v))
  69. #define W8(_p, _m, _v) __fs_out8(&(_p)->_m, (_v))
  70. #define R8(_p, _m) __fs_in8(&(_p)->_m)
  71. #define S8(_p, _m, _v) W8(_p, _m, R8(_p, _m) | (_v))
  72. #define C8(_p, _m, _v) W8(_p, _m, R8(_p, _m) & ~(_v))
  73. #define SCC_MAX_MULTICAST_ADDRS 64
  74. /*
  75. * Delay to wait for SCC reset command to complete (in us)
  76. */
  77. #define SCC_RESET_DELAY 50
  78. static inline int scc_cr_cmd(struct fs_enet_private *fep, u32 op)
  79. {
  80. const struct fs_platform_info *fpi = fep->fpi;
  81. return cpm_command(fpi->cp_command, op);
  82. }
  83. static int do_pd_setup(struct fs_enet_private *fep)
  84. {
  85. struct of_device *ofdev = to_of_device(fep->dev);
  86. fep->interrupt = of_irq_to_resource(ofdev->node, 0, NULL);
  87. if (fep->interrupt == NO_IRQ)
  88. return -EINVAL;
  89. fep->scc.sccp = of_iomap(ofdev->node, 0);
  90. if (!fep->scc.sccp)
  91. return -EINVAL;
  92. fep->scc.ep = of_iomap(ofdev->node, 1);
  93. if (!fep->scc.ep) {
  94. iounmap(fep->scc.sccp);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. #define SCC_NAPI_RX_EVENT_MSK (SCCE_ENET_RXF | SCCE_ENET_RXB)
  100. #define SCC_RX_EVENT (SCCE_ENET_RXF)
  101. #define SCC_TX_EVENT (SCCE_ENET_TXB)
  102. #define SCC_ERR_EVENT_MSK (SCCE_ENET_TXE | SCCE_ENET_BSY)
  103. static int setup_data(struct net_device *dev)
  104. {
  105. struct fs_enet_private *fep = netdev_priv(dev);
  106. do_pd_setup(fep);
  107. fep->scc.hthi = 0;
  108. fep->scc.htlo = 0;
  109. fep->ev_napi_rx = SCC_NAPI_RX_EVENT_MSK;
  110. fep->ev_rx = SCC_RX_EVENT;
  111. fep->ev_tx = SCC_TX_EVENT | SCCE_ENET_TXE;
  112. fep->ev_err = SCC_ERR_EVENT_MSK;
  113. return 0;
  114. }
  115. static int allocate_bd(struct net_device *dev)
  116. {
  117. struct fs_enet_private *fep = netdev_priv(dev);
  118. const struct fs_platform_info *fpi = fep->fpi;
  119. fep->ring_mem_addr = cpm_dpalloc((fpi->tx_ring + fpi->rx_ring) *
  120. sizeof(cbd_t), 8);
  121. if (IS_ERR_VALUE(fep->ring_mem_addr))
  122. return -ENOMEM;
  123. fep->ring_base = (void __iomem __force*)
  124. cpm_dpram_addr(fep->ring_mem_addr);
  125. return 0;
  126. }
  127. static void free_bd(struct net_device *dev)
  128. {
  129. struct fs_enet_private *fep = netdev_priv(dev);
  130. if (fep->ring_base)
  131. cpm_dpfree(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. scc_t __iomem *sccp = fep->scc.sccp;
  141. S16(sccp, scc_psmr, SCC_PSMR_PRO);
  142. }
  143. static void set_multicast_start(struct net_device *dev)
  144. {
  145. struct fs_enet_private *fep = netdev_priv(dev);
  146. scc_enet_t __iomem *ep = fep->scc.ep;
  147. W16(ep, sen_gaddr1, 0);
  148. W16(ep, sen_gaddr2, 0);
  149. W16(ep, sen_gaddr3, 0);
  150. W16(ep, sen_gaddr4, 0);
  151. }
  152. static void set_multicast_one(struct net_device *dev, const u8 * mac)
  153. {
  154. struct fs_enet_private *fep = netdev_priv(dev);
  155. scc_enet_t __iomem *ep = fep->scc.ep;
  156. u16 taddrh, taddrm, taddrl;
  157. taddrh = ((u16) mac[5] << 8) | mac[4];
  158. taddrm = ((u16) mac[3] << 8) | mac[2];
  159. taddrl = ((u16) mac[1] << 8) | mac[0];
  160. W16(ep, sen_taddrh, taddrh);
  161. W16(ep, sen_taddrm, taddrm);
  162. W16(ep, sen_taddrl, taddrl);
  163. scc_cr_cmd(fep, CPM_CR_SET_GADDR);
  164. }
  165. static void set_multicast_finish(struct net_device *dev)
  166. {
  167. struct fs_enet_private *fep = netdev_priv(dev);
  168. scc_t __iomem *sccp = fep->scc.sccp;
  169. scc_enet_t __iomem *ep = fep->scc.ep;
  170. /* clear promiscuous always */
  171. C16(sccp, scc_psmr, SCC_PSMR_PRO);
  172. /* if all multi or too many multicasts; just enable all */
  173. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  174. dev->mc_count > SCC_MAX_MULTICAST_ADDRS) {
  175. W16(ep, sen_gaddr1, 0xffff);
  176. W16(ep, sen_gaddr2, 0xffff);
  177. W16(ep, sen_gaddr3, 0xffff);
  178. W16(ep, sen_gaddr4, 0xffff);
  179. }
  180. }
  181. static void set_multicast_list(struct net_device *dev)
  182. {
  183. struct dev_mc_list *pmc;
  184. if ((dev->flags & IFF_PROMISC) == 0) {
  185. set_multicast_start(dev);
  186. for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
  187. set_multicast_one(dev, pmc->dmi_addr);
  188. set_multicast_finish(dev);
  189. } else
  190. set_promiscuous_mode(dev);
  191. }
  192. /*
  193. * This function is called to start or restart the FEC during a link
  194. * change. This only happens when switching between half and full
  195. * duplex.
  196. */
  197. static void restart(struct net_device *dev)
  198. {
  199. struct fs_enet_private *fep = netdev_priv(dev);
  200. scc_t __iomem *sccp = fep->scc.sccp;
  201. scc_enet_t __iomem *ep = fep->scc.ep;
  202. const struct fs_platform_info *fpi = fep->fpi;
  203. u16 paddrh, paddrm, paddrl;
  204. const unsigned char *mac;
  205. int i;
  206. C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  207. /* clear everything (slow & steady does it) */
  208. for (i = 0; i < sizeof(*ep); i++)
  209. __fs_out8((u8 __iomem *)ep + i, 0);
  210. /* point to bds */
  211. W16(ep, sen_genscc.scc_rbase, fep->ring_mem_addr);
  212. W16(ep, sen_genscc.scc_tbase,
  213. fep->ring_mem_addr + sizeof(cbd_t) * fpi->rx_ring);
  214. /* Initialize function code registers for big-endian.
  215. */
  216. W8(ep, sen_genscc.scc_rfcr, SCC_EB);
  217. W8(ep, sen_genscc.scc_tfcr, SCC_EB);
  218. /* Set maximum bytes per receive buffer.
  219. * This appears to be an Ethernet frame size, not the buffer
  220. * fragment size. It must be a multiple of four.
  221. */
  222. W16(ep, sen_genscc.scc_mrblr, 0x5f0);
  223. /* Set CRC preset and mask.
  224. */
  225. W32(ep, sen_cpres, 0xffffffff);
  226. W32(ep, sen_cmask, 0xdebb20e3);
  227. W32(ep, sen_crcec, 0); /* CRC Error counter */
  228. W32(ep, sen_alec, 0); /* alignment error counter */
  229. W32(ep, sen_disfc, 0); /* discard frame counter */
  230. W16(ep, sen_pads, 0x8888); /* Tx short frame pad character */
  231. W16(ep, sen_retlim, 15); /* Retry limit threshold */
  232. W16(ep, sen_maxflr, 0x5ee); /* maximum frame length register */
  233. W16(ep, sen_minflr, PKT_MINBUF_SIZE); /* minimum frame length register */
  234. W16(ep, sen_maxd1, 0x000005f0); /* maximum DMA1 length */
  235. W16(ep, sen_maxd2, 0x000005f0); /* maximum DMA2 length */
  236. /* Clear hash tables.
  237. */
  238. W16(ep, sen_gaddr1, 0);
  239. W16(ep, sen_gaddr2, 0);
  240. W16(ep, sen_gaddr3, 0);
  241. W16(ep, sen_gaddr4, 0);
  242. W16(ep, sen_iaddr1, 0);
  243. W16(ep, sen_iaddr2, 0);
  244. W16(ep, sen_iaddr3, 0);
  245. W16(ep, sen_iaddr4, 0);
  246. /* set address
  247. */
  248. mac = dev->dev_addr;
  249. paddrh = ((u16) mac[5] << 8) | mac[4];
  250. paddrm = ((u16) mac[3] << 8) | mac[2];
  251. paddrl = ((u16) mac[1] << 8) | mac[0];
  252. W16(ep, sen_paddrh, paddrh);
  253. W16(ep, sen_paddrm, paddrm);
  254. W16(ep, sen_paddrl, paddrl);
  255. W16(ep, sen_pper, 0);
  256. W16(ep, sen_taddrl, 0);
  257. W16(ep, sen_taddrm, 0);
  258. W16(ep, sen_taddrh, 0);
  259. fs_init_bds(dev);
  260. scc_cr_cmd(fep, CPM_CR_INIT_TRX);
  261. W16(sccp, scc_scce, 0xffff);
  262. /* Enable interrupts we wish to service.
  263. */
  264. W16(sccp, scc_sccm, SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
  265. /* Set GSMR_H to enable all normal operating modes.
  266. * Set GSMR_L to enable Ethernet to MC68160.
  267. */
  268. W32(sccp, scc_gsmrh, 0);
  269. W32(sccp, scc_gsmrl,
  270. SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 |
  271. SCC_GSMRL_MODE_ENET);
  272. /* Set sync/delimiters.
  273. */
  274. W16(sccp, scc_dsr, 0xd555);
  275. /* Set processing mode. Use Ethernet CRC, catch broadcast, and
  276. * start frame search 22 bit times after RENA.
  277. */
  278. W16(sccp, scc_psmr, SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
  279. /* Set full duplex mode if needed */
  280. if (fep->phydev->duplex)
  281. S16(sccp, scc_psmr, SCC_PSMR_LPB | SCC_PSMR_FDE);
  282. S32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  283. }
  284. static void stop(struct net_device *dev)
  285. {
  286. struct fs_enet_private *fep = netdev_priv(dev);
  287. scc_t __iomem *sccp = fep->scc.sccp;
  288. int i;
  289. for (i = 0; (R16(sccp, scc_sccm) == 0) && i < SCC_RESET_DELAY; i++)
  290. udelay(1);
  291. if (i == SCC_RESET_DELAY)
  292. printk(KERN_WARNING DRV_MODULE_NAME
  293. ": %s SCC timeout on graceful transmit stop\n",
  294. dev->name);
  295. W16(sccp, scc_sccm, 0);
  296. C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  297. fs_cleanup_bds(dev);
  298. }
  299. static void pre_request_irq(struct net_device *dev, int irq)
  300. {
  301. #ifndef CONFIG_PPC_MERGE
  302. immap_t *immap = fs_enet_immap;
  303. u32 siel;
  304. /* SIU interrupt */
  305. if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
  306. siel = in_be32(&immap->im_siu_conf.sc_siel);
  307. if ((irq & 1) == 0)
  308. siel |= (0x80000000 >> irq);
  309. else
  310. siel &= ~(0x80000000 >> (irq & ~1));
  311. out_be32(&immap->im_siu_conf.sc_siel, siel);
  312. }
  313. #endif
  314. }
  315. static void post_free_irq(struct net_device *dev, int irq)
  316. {
  317. /* nothing */
  318. }
  319. static void napi_clear_rx_event(struct net_device *dev)
  320. {
  321. struct fs_enet_private *fep = netdev_priv(dev);
  322. scc_t __iomem *sccp = fep->scc.sccp;
  323. W16(sccp, scc_scce, SCC_NAPI_RX_EVENT_MSK);
  324. }
  325. static void napi_enable_rx(struct net_device *dev)
  326. {
  327. struct fs_enet_private *fep = netdev_priv(dev);
  328. scc_t __iomem *sccp = fep->scc.sccp;
  329. S16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
  330. }
  331. static void napi_disable_rx(struct net_device *dev)
  332. {
  333. struct fs_enet_private *fep = netdev_priv(dev);
  334. scc_t __iomem *sccp = fep->scc.sccp;
  335. C16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
  336. }
  337. static void rx_bd_done(struct net_device *dev)
  338. {
  339. /* nothing */
  340. }
  341. static void tx_kickstart(struct net_device *dev)
  342. {
  343. /* nothing */
  344. }
  345. static u32 get_int_events(struct net_device *dev)
  346. {
  347. struct fs_enet_private *fep = netdev_priv(dev);
  348. scc_t __iomem *sccp = fep->scc.sccp;
  349. return (u32) R16(sccp, scc_scce);
  350. }
  351. static void clear_int_events(struct net_device *dev, u32 int_events)
  352. {
  353. struct fs_enet_private *fep = netdev_priv(dev);
  354. scc_t __iomem *sccp = fep->scc.sccp;
  355. W16(sccp, scc_scce, int_events & 0xffff);
  356. }
  357. static void ev_error(struct net_device *dev, u32 int_events)
  358. {
  359. printk(KERN_WARNING DRV_MODULE_NAME
  360. ": %s SCC ERROR(s) 0x%x\n", dev->name, int_events);
  361. }
  362. static int get_regs(struct net_device *dev, void *p, int *sizep)
  363. {
  364. struct fs_enet_private *fep = netdev_priv(dev);
  365. if (*sizep < sizeof(scc_t) + sizeof(scc_enet_t __iomem *))
  366. return -EINVAL;
  367. memcpy_fromio(p, fep->scc.sccp, sizeof(scc_t));
  368. p = (char *)p + sizeof(scc_t);
  369. memcpy_fromio(p, fep->scc.ep, sizeof(scc_enet_t __iomem *));
  370. return 0;
  371. }
  372. static int get_regs_len(struct net_device *dev)
  373. {
  374. return sizeof(scc_t) + sizeof(scc_enet_t __iomem *);
  375. }
  376. static void tx_restart(struct net_device *dev)
  377. {
  378. struct fs_enet_private *fep = netdev_priv(dev);
  379. scc_cr_cmd(fep, CPM_CR_RESTART_TX);
  380. }
  381. /*************************************************************************/
  382. const struct fs_ops fs_scc_ops = {
  383. .setup_data = setup_data,
  384. .cleanup_data = cleanup_data,
  385. .set_multicast_list = set_multicast_list,
  386. .restart = restart,
  387. .stop = stop,
  388. .pre_request_irq = pre_request_irq,
  389. .post_free_irq = post_free_irq,
  390. .napi_clear_rx_event = napi_clear_rx_event,
  391. .napi_enable_rx = napi_enable_rx,
  392. .napi_disable_rx = napi_disable_rx,
  393. .rx_bd_done = rx_bd_done,
  394. .tx_kickstart = tx_kickstart,
  395. .get_int_events = get_int_events,
  396. .clear_int_events = clear_int_events,
  397. .ev_error = ev_error,
  398. .get_regs = get_regs,
  399. .get_regs_len = get_regs_len,
  400. .tx_restart = tx_restart,
  401. .allocate_bd = allocate_bd,
  402. .free_bd = free_bd,
  403. };