mac-scc.c 12 KB

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