enet.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Ethernet driver for Motorola MPC8260.
  3. * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
  4. * Copyright (c) 2000 MontaVista Software Inc. (source@mvista.com)
  5. * 2.3.99 Updates
  6. *
  7. * I copied this from the 8xx CPM Ethernet driver, so follow the
  8. * credits back through that.
  9. *
  10. * This version of the driver is somewhat selectable for the different
  11. * processor/board combinations. It works for the boards I know about
  12. * now, and should be easily modified to include others. Some of the
  13. * configuration information is contained in <asm/commproc.h> and the
  14. * remainder is here.
  15. *
  16. * Buffer descriptors are kept in the CPM dual port RAM, and the frame
  17. * buffers are in the host memory.
  18. *
  19. * Right now, I am very watseful with the buffers. I allocate memory
  20. * pages and then divide them into 2K frame buffers. This way I know I
  21. * have buffers large enough to hold one frame within one buffer descriptor.
  22. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  23. * will be much more memory efficient and will easily handle lots of
  24. * small packets.
  25. *
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/sched.h>
  29. #include <linux/string.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/errno.h>
  32. #include <linux/ioport.h>
  33. #include <linux/slab.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/pci.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/bitops.h>
  43. #include <asm/immap_cpm2.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/mpc8260.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/cpm2.h>
  48. #include <asm/irq.h>
  49. /*
  50. * Theory of Operation
  51. *
  52. * The MPC8260 CPM performs the Ethernet processing on an SCC. It can use
  53. * an aribtrary number of buffers on byte boundaries, but must have at
  54. * least two receive buffers to prevent constant overrun conditions.
  55. *
  56. * The buffer descriptors are allocated from the CPM dual port memory
  57. * with the data buffers allocated from host memory, just like all other
  58. * serial communication protocols. The host memory buffers are allocated
  59. * from the free page pool, and then divided into smaller receive and
  60. * transmit buffers. The size of the buffers should be a power of two,
  61. * since that nicely divides the page. This creates a ring buffer
  62. * structure similar to the LANCE and other controllers.
  63. *
  64. * Like the LANCE driver:
  65. * The driver runs as two independent, single-threaded flows of control. One
  66. * is the send-packet routine, which enforces single-threaded use by the
  67. * cep->tx_busy flag. The other thread is the interrupt handler, which is
  68. * single threaded by the hardware and other software.
  69. */
  70. /* The transmitter timeout
  71. */
  72. #define TX_TIMEOUT (2*HZ)
  73. /* The number of Tx and Rx buffers. These are allocated from the page
  74. * pool. The code may assume these are power of two, so it is best
  75. * to keep them that size.
  76. * We don't need to allocate pages for the transmitter. We just use
  77. * the skbuffer directly.
  78. */
  79. #define CPM_ENET_RX_PAGES 4
  80. #define CPM_ENET_RX_FRSIZE 2048
  81. #define CPM_ENET_RX_FRPPG (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
  82. #define RX_RING_SIZE (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
  83. #define TX_RING_SIZE 8 /* Must be power of two */
  84. #define TX_RING_MOD_MASK 7 /* for this to work */
  85. /* The CPM stores dest/src/type, data, and checksum for receive packets.
  86. */
  87. #define PKT_MAXBUF_SIZE 1518
  88. #define PKT_MINBUF_SIZE 64
  89. #define PKT_MAXBLR_SIZE 1520
  90. /* The CPM buffer descriptors track the ring buffers. The rx_bd_base and
  91. * tx_bd_base always point to the base of the buffer descriptors. The
  92. * cur_rx and cur_tx point to the currently available buffer.
  93. * The dirty_tx tracks the current buffer that is being sent by the
  94. * controller. The cur_tx and dirty_tx are equal under both completely
  95. * empty and completely full conditions. The empty/ready indicator in
  96. * the buffer descriptor determines the actual condition.
  97. */
  98. struct scc_enet_private {
  99. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  100. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  101. ushort skb_cur;
  102. ushort skb_dirty;
  103. /* CPM dual port RAM relative addresses.
  104. */
  105. cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
  106. cbd_t *tx_bd_base;
  107. cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
  108. cbd_t *dirty_tx; /* The ring entries to be free()ed. */
  109. scc_t *sccp;
  110. struct net_device_stats stats;
  111. uint tx_full;
  112. spinlock_t lock;
  113. };
  114. static int scc_enet_open(struct net_device *dev);
  115. static int scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
  116. static int scc_enet_rx(struct net_device *dev);
  117. static irqreturn_t scc_enet_interrupt(int irq, void *dev_id);
  118. static int scc_enet_close(struct net_device *dev);
  119. static struct net_device_stats *scc_enet_get_stats(struct net_device *dev);
  120. static void set_multicast_list(struct net_device *dev);
  121. /* These will be configurable for the SCC choice.
  122. */
  123. #define CPM_ENET_BLOCK CPM_CR_SCC1_SBLOCK
  124. #define CPM_ENET_PAGE CPM_CR_SCC1_PAGE
  125. #define PROFF_ENET PROFF_SCC1
  126. #define SCC_ENET 0
  127. #define SIU_INT_ENET SIU_INT_SCC1
  128. /* These are both board and SCC dependent....
  129. */
  130. #define PD_ENET_RXD ((uint)0x00000001)
  131. #define PD_ENET_TXD ((uint)0x00000002)
  132. #define PD_ENET_TENA ((uint)0x00000004)
  133. #define PC_ENET_RENA ((uint)0x00020000)
  134. #define PC_ENET_CLSN ((uint)0x00000004)
  135. #define PC_ENET_TXCLK ((uint)0x00000800)
  136. #define PC_ENET_RXCLK ((uint)0x00000400)
  137. #define CMX_CLK_ROUTE ((uint)0x25000000)
  138. #define CMX_CLK_MASK ((uint)0xff000000)
  139. /* Specific to a board.
  140. */
  141. #define PC_EST8260_ENET_LOOPBACK ((uint)0x80000000)
  142. #define PC_EST8260_ENET_SQE ((uint)0x40000000)
  143. #define PC_EST8260_ENET_NOTFD ((uint)0x20000000)
  144. static int
  145. scc_enet_open(struct net_device *dev)
  146. {
  147. /* I should reset the ring buffers here, but I don't yet know
  148. * a simple way to do that.
  149. */
  150. netif_start_queue(dev);
  151. return 0; /* Always succeed */
  152. }
  153. static int
  154. scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  155. {
  156. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  157. volatile cbd_t *bdp;
  158. /* Fill in a Tx ring entry */
  159. bdp = cep->cur_tx;
  160. #ifndef final_version
  161. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  162. /* Ooops. All transmit buffers are full. Bail out.
  163. * This should not happen, since cep->tx_full should be set.
  164. */
  165. printk("%s: tx queue full!.\n", dev->name);
  166. return 1;
  167. }
  168. #endif
  169. /* Clear all of the status flags.
  170. */
  171. bdp->cbd_sc &= ~BD_ENET_TX_STATS;
  172. /* If the frame is short, tell CPM to pad it.
  173. */
  174. if (skb->len <= ETH_ZLEN)
  175. bdp->cbd_sc |= BD_ENET_TX_PAD;
  176. else
  177. bdp->cbd_sc &= ~BD_ENET_TX_PAD;
  178. /* Set buffer length and buffer pointer.
  179. */
  180. bdp->cbd_datlen = skb->len;
  181. bdp->cbd_bufaddr = __pa(skb->data);
  182. /* Save skb pointer.
  183. */
  184. cep->tx_skbuff[cep->skb_cur] = skb;
  185. cep->stats.tx_bytes += skb->len;
  186. cep->skb_cur = (cep->skb_cur+1) & TX_RING_MOD_MASK;
  187. spin_lock_irq(&cep->lock);
  188. /* Send it on its way. Tell CPM its ready, interrupt when done,
  189. * its the last BD of the frame, and to put the CRC on the end.
  190. */
  191. bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  192. dev->trans_start = jiffies;
  193. /* If this was the last BD in the ring, start at the beginning again.
  194. */
  195. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  196. bdp = cep->tx_bd_base;
  197. else
  198. bdp++;
  199. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  200. netif_stop_queue(dev);
  201. cep->tx_full = 1;
  202. }
  203. cep->cur_tx = (cbd_t *)bdp;
  204. spin_unlock_irq(&cep->lock);
  205. return 0;
  206. }
  207. static void
  208. scc_enet_timeout(struct net_device *dev)
  209. {
  210. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  211. printk("%s: transmit timed out.\n", dev->name);
  212. cep->stats.tx_errors++;
  213. #ifndef final_version
  214. {
  215. int i;
  216. cbd_t *bdp;
  217. printk(" Ring data dump: cur_tx %p%s cur_rx %p.\n",
  218. cep->cur_tx, cep->tx_full ? " (full)" : "",
  219. cep->cur_rx);
  220. bdp = cep->tx_bd_base;
  221. printk(" Tx @base %p :\n", bdp);
  222. for (i = 0 ; i < TX_RING_SIZE; i++, bdp++)
  223. printk("%04x %04x %08x\n",
  224. bdp->cbd_sc,
  225. bdp->cbd_datlen,
  226. bdp->cbd_bufaddr);
  227. bdp = cep->rx_bd_base;
  228. printk(" Rx @base %p :\n", bdp);
  229. for (i = 0 ; i < RX_RING_SIZE; i++, bdp++)
  230. printk("%04x %04x %08x\n",
  231. bdp->cbd_sc,
  232. bdp->cbd_datlen,
  233. bdp->cbd_bufaddr);
  234. }
  235. #endif
  236. if (!cep->tx_full)
  237. netif_wake_queue(dev);
  238. }
  239. /* The interrupt handler.
  240. * This is called from the CPM handler, not the MPC core interrupt.
  241. */
  242. static irqreturn_t
  243. scc_enet_interrupt(int irq, void * dev_id)
  244. {
  245. struct net_device *dev = dev_id;
  246. volatile struct scc_enet_private *cep;
  247. volatile cbd_t *bdp;
  248. ushort int_events;
  249. int must_restart;
  250. cep = (struct scc_enet_private *)dev->priv;
  251. /* Get the interrupt events that caused us to be here.
  252. */
  253. int_events = cep->sccp->scc_scce;
  254. cep->sccp->scc_scce = int_events;
  255. must_restart = 0;
  256. /* Handle receive event in its own function.
  257. */
  258. if (int_events & SCCE_ENET_RXF)
  259. scc_enet_rx(dev_id);
  260. /* Check for a transmit error. The manual is a little unclear
  261. * about this, so the debug code until I get it figured out. It
  262. * appears that if TXE is set, then TXB is not set. However,
  263. * if carrier sense is lost during frame transmission, the TXE
  264. * bit is set, "and continues the buffer transmission normally."
  265. * I don't know if "normally" implies TXB is set when the buffer
  266. * descriptor is closed.....trial and error :-).
  267. */
  268. /* Transmit OK, or non-fatal error. Update the buffer descriptors.
  269. */
  270. if (int_events & (SCCE_ENET_TXE | SCCE_ENET_TXB)) {
  271. spin_lock(&cep->lock);
  272. bdp = cep->dirty_tx;
  273. while ((bdp->cbd_sc&BD_ENET_TX_READY)==0) {
  274. if ((bdp==cep->cur_tx) && (cep->tx_full == 0))
  275. break;
  276. if (bdp->cbd_sc & BD_ENET_TX_HB) /* No heartbeat */
  277. cep->stats.tx_heartbeat_errors++;
  278. if (bdp->cbd_sc & BD_ENET_TX_LC) /* Late collision */
  279. cep->stats.tx_window_errors++;
  280. if (bdp->cbd_sc & BD_ENET_TX_RL) /* Retrans limit */
  281. cep->stats.tx_aborted_errors++;
  282. if (bdp->cbd_sc & BD_ENET_TX_UN) /* Underrun */
  283. cep->stats.tx_fifo_errors++;
  284. if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
  285. cep->stats.tx_carrier_errors++;
  286. /* No heartbeat or Lost carrier are not really bad errors.
  287. * The others require a restart transmit command.
  288. */
  289. if (bdp->cbd_sc &
  290. (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  291. must_restart = 1;
  292. cep->stats.tx_errors++;
  293. }
  294. cep->stats.tx_packets++;
  295. /* Deferred means some collisions occurred during transmit,
  296. * but we eventually sent the packet OK.
  297. */
  298. if (bdp->cbd_sc & BD_ENET_TX_DEF)
  299. cep->stats.collisions++;
  300. /* Free the sk buffer associated with this last transmit.
  301. */
  302. dev_kfree_skb_irq(cep->tx_skbuff[cep->skb_dirty]);
  303. cep->skb_dirty = (cep->skb_dirty + 1) & TX_RING_MOD_MASK;
  304. /* Update pointer to next buffer descriptor to be transmitted.
  305. */
  306. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  307. bdp = cep->tx_bd_base;
  308. else
  309. bdp++;
  310. /* I don't know if we can be held off from processing these
  311. * interrupts for more than one frame time. I really hope
  312. * not. In such a case, we would now want to check the
  313. * currently available BD (cur_tx) and determine if any
  314. * buffers between the dirty_tx and cur_tx have also been
  315. * sent. We would want to process anything in between that
  316. * does not have BD_ENET_TX_READY set.
  317. */
  318. /* Since we have freed up a buffer, the ring is no longer
  319. * full.
  320. */
  321. if (cep->tx_full) {
  322. cep->tx_full = 0;
  323. if (netif_queue_stopped(dev)) {
  324. netif_wake_queue(dev);
  325. }
  326. }
  327. cep->dirty_tx = (cbd_t *)bdp;
  328. }
  329. if (must_restart) {
  330. volatile cpm_cpm2_t *cp;
  331. /* Some transmit errors cause the transmitter to shut
  332. * down. We now issue a restart transmit. Since the
  333. * errors close the BD and update the pointers, the restart
  334. * _should_ pick up without having to reset any of our
  335. * pointers either.
  336. */
  337. cp = cpmp;
  338. cp->cp_cpcr =
  339. mk_cr_cmd(CPM_ENET_PAGE, CPM_ENET_BLOCK, 0,
  340. CPM_CR_RESTART_TX) | CPM_CR_FLG;
  341. while (cp->cp_cpcr & CPM_CR_FLG);
  342. }
  343. spin_unlock(&cep->lock);
  344. }
  345. /* Check for receive busy, i.e. packets coming but no place to
  346. * put them. This "can't happen" because the receive interrupt
  347. * is tossing previous frames.
  348. */
  349. if (int_events & SCCE_ENET_BSY) {
  350. cep->stats.rx_dropped++;
  351. printk("SCC ENET: BSY can't happen.\n");
  352. }
  353. return IRQ_HANDLED;
  354. }
  355. /* During a receive, the cur_rx points to the current incoming buffer.
  356. * When we update through the ring, if the next incoming buffer has
  357. * not been given to the system, we just set the empty indicator,
  358. * effectively tossing the packet.
  359. */
  360. static int
  361. scc_enet_rx(struct net_device *dev)
  362. {
  363. struct scc_enet_private *cep;
  364. volatile cbd_t *bdp;
  365. struct sk_buff *skb;
  366. ushort pkt_len;
  367. cep = (struct scc_enet_private *)dev->priv;
  368. /* First, grab all of the stats for the incoming packet.
  369. * These get messed up if we get called due to a busy condition.
  370. */
  371. bdp = cep->cur_rx;
  372. for (;;) {
  373. if (bdp->cbd_sc & BD_ENET_RX_EMPTY)
  374. break;
  375. #ifndef final_version
  376. /* Since we have allocated space to hold a complete frame, both
  377. * the first and last indicators should be set.
  378. */
  379. if ((bdp->cbd_sc & (BD_ENET_RX_FIRST | BD_ENET_RX_LAST)) !=
  380. (BD_ENET_RX_FIRST | BD_ENET_RX_LAST))
  381. printk("CPM ENET: rcv is not first+last\n");
  382. #endif
  383. /* Frame too long or too short.
  384. */
  385. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  386. cep->stats.rx_length_errors++;
  387. if (bdp->cbd_sc & BD_ENET_RX_NO) /* Frame alignment */
  388. cep->stats.rx_frame_errors++;
  389. if (bdp->cbd_sc & BD_ENET_RX_CR) /* CRC Error */
  390. cep->stats.rx_crc_errors++;
  391. if (bdp->cbd_sc & BD_ENET_RX_OV) /* FIFO overrun */
  392. cep->stats.rx_crc_errors++;
  393. /* Report late collisions as a frame error.
  394. * On this error, the BD is closed, but we don't know what we
  395. * have in the buffer. So, just drop this frame on the floor.
  396. */
  397. if (bdp->cbd_sc & BD_ENET_RX_CL) {
  398. cep->stats.rx_frame_errors++;
  399. }
  400. else {
  401. /* Process the incoming frame.
  402. */
  403. cep->stats.rx_packets++;
  404. pkt_len = bdp->cbd_datlen;
  405. cep->stats.rx_bytes += pkt_len;
  406. /* This does 16 byte alignment, much more than we need.
  407. * The packet length includes FCS, but we don't want to
  408. * include that when passing upstream as it messes up
  409. * bridging applications.
  410. */
  411. skb = dev_alloc_skb(pkt_len-4);
  412. if (skb == NULL) {
  413. printk("%s: Memory squeeze, dropping packet.\n", dev->name);
  414. cep->stats.rx_dropped++;
  415. }
  416. else {
  417. skb->dev = dev;
  418. skb_put(skb,pkt_len-4); /* Make room */
  419. eth_copy_and_sum(skb,
  420. (unsigned char *)__va(bdp->cbd_bufaddr),
  421. pkt_len-4, 0);
  422. skb->protocol=eth_type_trans(skb,dev);
  423. netif_rx(skb);
  424. }
  425. }
  426. /* Clear the status flags for this buffer.
  427. */
  428. bdp->cbd_sc &= ~BD_ENET_RX_STATS;
  429. /* Mark the buffer empty.
  430. */
  431. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  432. /* Update BD pointer to next entry.
  433. */
  434. if (bdp->cbd_sc & BD_ENET_RX_WRAP)
  435. bdp = cep->rx_bd_base;
  436. else
  437. bdp++;
  438. }
  439. cep->cur_rx = (cbd_t *)bdp;
  440. return 0;
  441. }
  442. static int
  443. scc_enet_close(struct net_device *dev)
  444. {
  445. /* Don't know what to do yet.
  446. */
  447. netif_stop_queue(dev);
  448. return 0;
  449. }
  450. static struct net_device_stats *scc_enet_get_stats(struct net_device *dev)
  451. {
  452. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  453. return &cep->stats;
  454. }
  455. /* Set or clear the multicast filter for this adaptor.
  456. * Skeleton taken from sunlance driver.
  457. * The CPM Ethernet implementation allows Multicast as well as individual
  458. * MAC address filtering. Some of the drivers check to make sure it is
  459. * a group multicast address, and discard those that are not. I guess I
  460. * will do the same for now, but just remove the test if you want
  461. * individual filtering as well (do the upper net layers want or support
  462. * this kind of feature?).
  463. */
  464. static void set_multicast_list(struct net_device *dev)
  465. {
  466. struct scc_enet_private *cep;
  467. struct dev_mc_list *dmi;
  468. u_char *mcptr, *tdptr;
  469. volatile scc_enet_t *ep;
  470. int i, j;
  471. cep = (struct scc_enet_private *)dev->priv;
  472. /* Get pointer to SCC area in parameter RAM.
  473. */
  474. ep = (scc_enet_t *)dev->base_addr;
  475. if (dev->flags&IFF_PROMISC) {
  476. /* Log any net taps. */
  477. printk("%s: Promiscuous mode enabled.\n", dev->name);
  478. cep->sccp->scc_psmr |= SCC_PSMR_PRO;
  479. } else {
  480. cep->sccp->scc_psmr &= ~SCC_PSMR_PRO;
  481. if (dev->flags & IFF_ALLMULTI) {
  482. /* Catch all multicast addresses, so set the
  483. * filter to all 1's.
  484. */
  485. ep->sen_gaddr1 = 0xffff;
  486. ep->sen_gaddr2 = 0xffff;
  487. ep->sen_gaddr3 = 0xffff;
  488. ep->sen_gaddr4 = 0xffff;
  489. }
  490. else {
  491. /* Clear filter and add the addresses in the list.
  492. */
  493. ep->sen_gaddr1 = 0;
  494. ep->sen_gaddr2 = 0;
  495. ep->sen_gaddr3 = 0;
  496. ep->sen_gaddr4 = 0;
  497. dmi = dev->mc_list;
  498. for (i=0; i<dev->mc_count; i++) {
  499. /* Only support group multicast for now.
  500. */
  501. if (!(dmi->dmi_addr[0] & 1))
  502. continue;
  503. /* The address in dmi_addr is LSB first,
  504. * and taddr is MSB first. We have to
  505. * copy bytes MSB first from dmi_addr.
  506. */
  507. mcptr = (u_char *)dmi->dmi_addr + 5;
  508. tdptr = (u_char *)&ep->sen_taddrh;
  509. for (j=0; j<6; j++)
  510. *tdptr++ = *mcptr--;
  511. /* Ask CPM to run CRC and set bit in
  512. * filter mask.
  513. */
  514. cpmp->cp_cpcr = mk_cr_cmd(CPM_ENET_PAGE,
  515. CPM_ENET_BLOCK, 0,
  516. CPM_CR_SET_GADDR) | CPM_CR_FLG;
  517. /* this delay is necessary here -- Cort */
  518. udelay(10);
  519. while (cpmp->cp_cpcr & CPM_CR_FLG);
  520. }
  521. }
  522. }
  523. }
  524. /* Initialize the CPM Ethernet on SCC.
  525. */
  526. static int __init scc_enet_init(void)
  527. {
  528. struct net_device *dev;
  529. struct scc_enet_private *cep;
  530. int i, j, err;
  531. uint dp_offset;
  532. unsigned char *eap;
  533. unsigned long mem_addr;
  534. bd_t *bd;
  535. volatile cbd_t *bdp;
  536. volatile cpm_cpm2_t *cp;
  537. volatile scc_t *sccp;
  538. volatile scc_enet_t *ep;
  539. volatile cpm2_map_t *immap;
  540. volatile iop_cpm2_t *io;
  541. cp = cpmp; /* Get pointer to Communication Processor */
  542. immap = (cpm2_map_t *)CPM_MAP_ADDR; /* and to internal registers */
  543. io = &immap->im_ioport;
  544. bd = (bd_t *)__res;
  545. /* Create an Ethernet device instance.
  546. */
  547. dev = alloc_etherdev(sizeof(*cep));
  548. if (!dev)
  549. return -ENOMEM;
  550. cep = dev->priv;
  551. spin_lock_init(&cep->lock);
  552. /* Get pointer to SCC area in parameter RAM.
  553. */
  554. ep = (scc_enet_t *)(&immap->im_dprambase[PROFF_ENET]);
  555. /* And another to the SCC register area.
  556. */
  557. sccp = (volatile scc_t *)(&immap->im_scc[SCC_ENET]);
  558. cep->sccp = (scc_t *)sccp; /* Keep the pointer handy */
  559. /* Disable receive and transmit in case someone left it running.
  560. */
  561. sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  562. /* Configure port C and D pins for SCC Ethernet. This
  563. * won't work for all SCC possibilities....it will be
  564. * board/port specific.
  565. */
  566. io->iop_pparc |=
  567. (PC_ENET_RENA | PC_ENET_CLSN | PC_ENET_TXCLK | PC_ENET_RXCLK);
  568. io->iop_pdirc &=
  569. ~(PC_ENET_RENA | PC_ENET_CLSN | PC_ENET_TXCLK | PC_ENET_RXCLK);
  570. io->iop_psorc &=
  571. ~(PC_ENET_RENA | PC_ENET_TXCLK | PC_ENET_RXCLK);
  572. io->iop_psorc |= PC_ENET_CLSN;
  573. io->iop_ppard |= (PD_ENET_RXD | PD_ENET_TXD | PD_ENET_TENA);
  574. io->iop_pdird |= (PD_ENET_TXD | PD_ENET_TENA);
  575. io->iop_pdird &= ~PD_ENET_RXD;
  576. io->iop_psord |= PD_ENET_TXD;
  577. io->iop_psord &= ~(PD_ENET_RXD | PD_ENET_TENA);
  578. /* Configure Serial Interface clock routing.
  579. * First, clear all SCC bits to zero, then set the ones we want.
  580. */
  581. immap->im_cpmux.cmx_scr &= ~CMX_CLK_MASK;
  582. immap->im_cpmux.cmx_scr |= CMX_CLK_ROUTE;
  583. /* Allocate space for the buffer descriptors in the DP ram.
  584. * These are relative offsets in the DP ram address space.
  585. * Initialize base addresses for the buffer descriptors.
  586. */
  587. dp_offset = cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
  588. ep->sen_genscc.scc_rbase = dp_offset;
  589. cep->rx_bd_base = (cbd_t *)cpm_dpram_addr(dp_offset);
  590. dp_offset = cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
  591. ep->sen_genscc.scc_tbase = dp_offset;
  592. cep->tx_bd_base = (cbd_t *)cpm_dpram_addr(dp_offset);
  593. cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
  594. cep->cur_rx = cep->rx_bd_base;
  595. ep->sen_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
  596. ep->sen_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
  597. /* Set maximum bytes per receive buffer.
  598. * This appears to be an Ethernet frame size, not the buffer
  599. * fragment size. It must be a multiple of four.
  600. */
  601. ep->sen_genscc.scc_mrblr = PKT_MAXBLR_SIZE;
  602. /* Set CRC preset and mask.
  603. */
  604. ep->sen_cpres = 0xffffffff;
  605. ep->sen_cmask = 0xdebb20e3;
  606. ep->sen_crcec = 0; /* CRC Error counter */
  607. ep->sen_alec = 0; /* alignment error counter */
  608. ep->sen_disfc = 0; /* discard frame counter */
  609. ep->sen_pads = 0x8888; /* Tx short frame pad character */
  610. ep->sen_retlim = 15; /* Retry limit threshold */
  611. ep->sen_maxflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  612. ep->sen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  613. ep->sen_maxd1 = PKT_MAXBLR_SIZE; /* maximum DMA1 length */
  614. ep->sen_maxd2 = PKT_MAXBLR_SIZE; /* maximum DMA2 length */
  615. /* Clear hash tables.
  616. */
  617. ep->sen_gaddr1 = 0;
  618. ep->sen_gaddr2 = 0;
  619. ep->sen_gaddr3 = 0;
  620. ep->sen_gaddr4 = 0;
  621. ep->sen_iaddr1 = 0;
  622. ep->sen_iaddr2 = 0;
  623. ep->sen_iaddr3 = 0;
  624. ep->sen_iaddr4 = 0;
  625. /* Set Ethernet station address.
  626. *
  627. * This is supplied in the board information structure, so we
  628. * copy that into the controller.
  629. */
  630. eap = (unsigned char *)&(ep->sen_paddrh);
  631. for (i=5; i>=0; i--)
  632. *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
  633. ep->sen_pper = 0; /* 'cause the book says so */
  634. ep->sen_taddrl = 0; /* temp address (LSB) */
  635. ep->sen_taddrm = 0;
  636. ep->sen_taddrh = 0; /* temp address (MSB) */
  637. /* Now allocate the host memory pages and initialize the
  638. * buffer descriptors.
  639. */
  640. bdp = cep->tx_bd_base;
  641. for (i=0; i<TX_RING_SIZE; i++) {
  642. /* Initialize the BD for every fragment in the page.
  643. */
  644. bdp->cbd_sc = 0;
  645. bdp->cbd_bufaddr = 0;
  646. bdp++;
  647. }
  648. /* Set the last buffer to wrap.
  649. */
  650. bdp--;
  651. bdp->cbd_sc |= BD_SC_WRAP;
  652. bdp = cep->rx_bd_base;
  653. for (i=0; i<CPM_ENET_RX_PAGES; i++) {
  654. /* Allocate a page.
  655. */
  656. mem_addr = __get_free_page(GFP_KERNEL);
  657. /* BUG: no check for failure */
  658. /* Initialize the BD for every fragment in the page.
  659. */
  660. for (j=0; j<CPM_ENET_RX_FRPPG; j++) {
  661. bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
  662. bdp->cbd_bufaddr = __pa(mem_addr);
  663. mem_addr += CPM_ENET_RX_FRSIZE;
  664. bdp++;
  665. }
  666. }
  667. /* Set the last buffer to wrap.
  668. */
  669. bdp--;
  670. bdp->cbd_sc |= BD_SC_WRAP;
  671. /* Let's re-initialize the channel now. We have to do it later
  672. * than the manual describes because we have just now finished
  673. * the BD initialization.
  674. */
  675. cpmp->cp_cpcr = mk_cr_cmd(CPM_ENET_PAGE, CPM_ENET_BLOCK, 0,
  676. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  677. while (cp->cp_cpcr & CPM_CR_FLG);
  678. cep->skb_cur = cep->skb_dirty = 0;
  679. sccp->scc_scce = 0xffff; /* Clear any pending events */
  680. /* Enable interrupts for transmit error, complete frame
  681. * received, and any transmit buffer we have also set the
  682. * interrupt flag.
  683. */
  684. sccp->scc_sccm = (SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
  685. /* Install our interrupt handler.
  686. */
  687. request_irq(SIU_INT_ENET, scc_enet_interrupt, 0, "enet", dev);
  688. /* BUG: no check for failure */
  689. /* Set GSMR_H to enable all normal operating modes.
  690. * Set GSMR_L to enable Ethernet to MC68160.
  691. */
  692. sccp->scc_gsmrh = 0;
  693. sccp->scc_gsmrl = (SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 | SCC_GSMRL_MODE_ENET);
  694. /* Set sync/delimiters.
  695. */
  696. sccp->scc_dsr = 0xd555;
  697. /* Set processing mode. Use Ethernet CRC, catch broadcast, and
  698. * start frame search 22 bit times after RENA.
  699. */
  700. sccp->scc_psmr = (SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
  701. /* It is now OK to enable the Ethernet transmitter.
  702. * Unfortunately, there are board implementation differences here.
  703. */
  704. io->iop_pparc &= ~(PC_EST8260_ENET_LOOPBACK |
  705. PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
  706. io->iop_psorc &= ~(PC_EST8260_ENET_LOOPBACK |
  707. PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
  708. io->iop_pdirc |= (PC_EST8260_ENET_LOOPBACK |
  709. PC_EST8260_ENET_SQE | PC_EST8260_ENET_NOTFD);
  710. io->iop_pdatc &= ~(PC_EST8260_ENET_LOOPBACK | PC_EST8260_ENET_SQE);
  711. io->iop_pdatc |= PC_EST8260_ENET_NOTFD;
  712. dev->base_addr = (unsigned long)ep;
  713. /* The CPM Ethernet specific entries in the device structure. */
  714. dev->open = scc_enet_open;
  715. dev->hard_start_xmit = scc_enet_start_xmit;
  716. dev->tx_timeout = scc_enet_timeout;
  717. dev->watchdog_timeo = TX_TIMEOUT;
  718. dev->stop = scc_enet_close;
  719. dev->get_stats = scc_enet_get_stats;
  720. dev->set_multicast_list = set_multicast_list;
  721. /* And last, enable the transmit and receive processing.
  722. */
  723. sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  724. err = register_netdev(dev);
  725. if (err) {
  726. free_netdev(dev);
  727. return err;
  728. }
  729. printk("%s: SCC ENET Version 0.1, ", dev->name);
  730. for (i=0; i<5; i++)
  731. printk("%02x:", dev->dev_addr[i]);
  732. printk("%02x\n", dev->dev_addr[5]);
  733. return 0;
  734. }
  735. module_init(scc_enet_init);