enet.c 24 KB

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