mac-scc.c 12 KB

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