enet.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Ethernet driver for Motorola MPC8xx.
  3. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  4. *
  5. * I copied the basic skeleton from the lance driver, because I did not
  6. * know how to write the Linux driver, but I did know how the LANCE worked.
  7. *
  8. * This version of the driver is somewhat selectable for the different
  9. * processor/board combinations. It works for the boards I know about
  10. * now, and should be easily modified to include others. Some of the
  11. * configuration information is contained in <asm/commproc.h> and the
  12. * remainder is here.
  13. *
  14. * Buffer descriptors are kept in the CPM dual port RAM, and the frame
  15. * buffers are in the host memory.
  16. *
  17. * Right now, I am very watseful with the buffers. I allocate memory
  18. * pages and then divide them into 2K frame buffers. This way I know I
  19. * have buffers large enough to hold one frame within one buffer descriptor.
  20. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  21. * will be much more memory efficient and will easily handle lots of
  22. * small packets.
  23. *
  24. */
  25. #include <linux/config.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/string.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/errno.h>
  31. #include <linux/ioport.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pci.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/dma-mapping.h>
  42. #include <linux/bitops.h>
  43. #include <asm/8xx_immap.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/mpc8xx.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/commproc.h>
  48. /*
  49. * Theory of Operation
  50. *
  51. * The MPC8xx CPM performs the Ethernet processing on SCC1. 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 send packet thread has partial control over the Tx ring and the
  70. * 'cep->tx_busy' flag. It sets the tx_busy flag whenever it's queuing a Tx
  71. * packet. If the next queue slot is empty, it clears the tx_busy flag when
  72. * finished otherwise it sets the 'lp->tx_full' flag.
  73. *
  74. * The MBX has a control register external to the MPC8xx that has some
  75. * control of the Ethernet interface. Information is in the manual for
  76. * your board.
  77. *
  78. * The RPX boards have an external control/status register. Consult the
  79. * programming documents for details unique to your board.
  80. *
  81. * For the TQM8xx(L) modules, there is no control register interface.
  82. * All functions are directly controlled using I/O pins. See <asm/commproc.h>.
  83. */
  84. /* The transmitter timeout
  85. */
  86. #define TX_TIMEOUT (2*HZ)
  87. /* The number of Tx and Rx buffers. These are allocated from the page
  88. * pool. The code may assume these are power of two, so it is best
  89. * to keep them that size.
  90. * We don't need to allocate pages for the transmitter. We just use
  91. * the skbuffer directly.
  92. */
  93. #ifdef CONFIG_ENET_BIG_BUFFERS
  94. #define CPM_ENET_RX_PAGES 32
  95. #define CPM_ENET_RX_FRSIZE 2048
  96. #define CPM_ENET_RX_FRPPG (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
  97. #define RX_RING_SIZE (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
  98. #define TX_RING_SIZE 64 /* Must be power of two */
  99. #define TX_RING_MOD_MASK 63 /* for this to work */
  100. #else
  101. #define CPM_ENET_RX_PAGES 4
  102. #define CPM_ENET_RX_FRSIZE 2048
  103. #define CPM_ENET_RX_FRPPG (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
  104. #define RX_RING_SIZE (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
  105. #define TX_RING_SIZE 8 /* Must be power of two */
  106. #define TX_RING_MOD_MASK 7 /* for this to work */
  107. #endif
  108. /* The CPM stores dest/src/type, data, and checksum for receive packets.
  109. */
  110. #define PKT_MAXBUF_SIZE 1518
  111. #define PKT_MINBUF_SIZE 64
  112. #define PKT_MAXBLR_SIZE 1520
  113. /* The CPM buffer descriptors track the ring buffers. The rx_bd_base and
  114. * tx_bd_base always point to the base of the buffer descriptors. The
  115. * cur_rx and cur_tx point to the currently available buffer.
  116. * The dirty_tx tracks the current buffer that is being sent by the
  117. * controller. The cur_tx and dirty_tx are equal under both completely
  118. * empty and completely full conditions. The empty/ready indicator in
  119. * the buffer descriptor determines the actual condition.
  120. */
  121. struct scc_enet_private {
  122. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  123. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  124. ushort skb_cur;
  125. ushort skb_dirty;
  126. /* CPM dual port RAM relative addresses.
  127. */
  128. cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
  129. cbd_t *tx_bd_base;
  130. cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
  131. cbd_t *dirty_tx; /* The ring entries to be free()ed. */
  132. scc_t *sccp;
  133. /* Virtual addresses for the receive buffers because we can't
  134. * do a __va() on them anymore.
  135. */
  136. unsigned char *rx_vaddr[RX_RING_SIZE];
  137. struct net_device_stats stats;
  138. uint tx_full;
  139. spinlock_t lock;
  140. };
  141. static int scc_enet_open(struct net_device *dev);
  142. static int scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
  143. static int scc_enet_rx(struct net_device *dev);
  144. static void scc_enet_interrupt(void *dev_id, struct pt_regs *regs);
  145. static int scc_enet_close(struct net_device *dev);
  146. static struct net_device_stats *scc_enet_get_stats(struct net_device *dev);
  147. static void set_multicast_list(struct net_device *dev);
  148. /* Get this from various configuration locations (depends on board).
  149. */
  150. /*static ushort my_enet_addr[] = { 0x0800, 0x3e26, 0x1559 };*/
  151. /* Typically, 860(T) boards use SCC1 for Ethernet, and other 8xx boards
  152. * use SCC2. Some even may use SCC3.
  153. * This is easily extended if necessary.
  154. */
  155. #if defined(CONFIG_SCC3_ENET)
  156. #define CPM_CR_ENET CPM_CR_CH_SCC3
  157. #define PROFF_ENET PROFF_SCC3
  158. #define SCC_ENET 2 /* Index, not number! */
  159. #define CPMVEC_ENET CPMVEC_SCC3
  160. #elif defined(CONFIG_SCC2_ENET)
  161. #define CPM_CR_ENET CPM_CR_CH_SCC2
  162. #define PROFF_ENET PROFF_SCC2
  163. #define SCC_ENET 1 /* Index, not number! */
  164. #define CPMVEC_ENET CPMVEC_SCC2
  165. #elif defined(CONFIG_SCC1_ENET)
  166. #define CPM_CR_ENET CPM_CR_CH_SCC1
  167. #define PROFF_ENET PROFF_SCC1
  168. #define SCC_ENET 0 /* Index, not number! */
  169. #define CPMVEC_ENET CPMVEC_SCC1
  170. #else
  171. #error CONFIG_SCCx_ENET not defined
  172. #endif
  173. static int
  174. scc_enet_open(struct net_device *dev)
  175. {
  176. /* I should reset the ring buffers here, but I don't yet know
  177. * a simple way to do that.
  178. */
  179. netif_start_queue(dev);
  180. return 0; /* Always succeed */
  181. }
  182. static int
  183. scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  184. {
  185. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  186. volatile cbd_t *bdp;
  187. /* Fill in a Tx ring entry */
  188. bdp = cep->cur_tx;
  189. #ifndef final_version
  190. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  191. /* Ooops. All transmit buffers are full. Bail out.
  192. * This should not happen, since cep->tx_busy should be set.
  193. */
  194. printk("%s: tx queue full!.\n", dev->name);
  195. return 1;
  196. }
  197. #endif
  198. /* Clear all of the status flags.
  199. */
  200. bdp->cbd_sc &= ~BD_ENET_TX_STATS;
  201. /* If the frame is short, tell CPM to pad it.
  202. */
  203. if (skb->len <= ETH_ZLEN)
  204. bdp->cbd_sc |= BD_ENET_TX_PAD;
  205. else
  206. bdp->cbd_sc &= ~BD_ENET_TX_PAD;
  207. /* Set buffer length and buffer pointer.
  208. */
  209. bdp->cbd_datlen = skb->len;
  210. bdp->cbd_bufaddr = __pa(skb->data);
  211. /* Save skb pointer.
  212. */
  213. cep->tx_skbuff[cep->skb_cur] = skb;
  214. cep->stats.tx_bytes += skb->len;
  215. cep->skb_cur = (cep->skb_cur+1) & TX_RING_MOD_MASK;
  216. /* Push the data cache so the CPM does not get stale memory
  217. * data.
  218. */
  219. flush_dcache_range((unsigned long)(skb->data),
  220. (unsigned long)(skb->data + skb->len));
  221. spin_lock_irq(&cep->lock);
  222. /* Send it on its way. Tell CPM its ready, interrupt when done,
  223. * its the last BD of the frame, and to put the CRC on the end.
  224. */
  225. bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  226. dev->trans_start = jiffies;
  227. /* If this was the last BD in the ring, start at the beginning again.
  228. */
  229. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  230. bdp = cep->tx_bd_base;
  231. else
  232. bdp++;
  233. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  234. netif_stop_queue(dev);
  235. cep->tx_full = 1;
  236. }
  237. cep->cur_tx = (cbd_t *)bdp;
  238. spin_unlock_irq(&cep->lock);
  239. return 0;
  240. }
  241. static void
  242. scc_enet_timeout(struct net_device *dev)
  243. {
  244. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  245. printk("%s: transmit timed out.\n", dev->name);
  246. cep->stats.tx_errors++;
  247. #ifndef final_version
  248. {
  249. int i;
  250. cbd_t *bdp;
  251. printk(" Ring data dump: cur_tx %p%s cur_rx %p.\n",
  252. cep->cur_tx, cep->tx_full ? " (full)" : "",
  253. cep->cur_rx);
  254. bdp = cep->tx_bd_base;
  255. for (i = 0 ; i < TX_RING_SIZE; i++, bdp++)
  256. printk("%04x %04x %08x\n",
  257. bdp->cbd_sc,
  258. bdp->cbd_datlen,
  259. bdp->cbd_bufaddr);
  260. bdp = cep->rx_bd_base;
  261. for (i = 0 ; i < RX_RING_SIZE; i++, bdp++)
  262. printk("%04x %04x %08x\n",
  263. bdp->cbd_sc,
  264. bdp->cbd_datlen,
  265. bdp->cbd_bufaddr);
  266. }
  267. #endif
  268. if (!cep->tx_full)
  269. netif_wake_queue(dev);
  270. }
  271. /* The interrupt handler.
  272. * This is called from the CPM handler, not the MPC core interrupt.
  273. */
  274. static void
  275. scc_enet_interrupt(void *dev_id, struct pt_regs *regs)
  276. {
  277. struct net_device *dev = dev_id;
  278. volatile struct scc_enet_private *cep;
  279. volatile cbd_t *bdp;
  280. ushort int_events;
  281. int must_restart;
  282. cep = (struct scc_enet_private *)dev->priv;
  283. /* Get the interrupt events that caused us to be here.
  284. */
  285. int_events = cep->sccp->scc_scce;
  286. cep->sccp->scc_scce = int_events;
  287. must_restart = 0;
  288. /* Handle receive event in its own function.
  289. */
  290. if (int_events & SCCE_ENET_RXF)
  291. scc_enet_rx(dev_id);
  292. /* Check for a transmit error. The manual is a little unclear
  293. * about this, so the debug code until I get it figured out. It
  294. * appears that if TXE is set, then TXB is not set. However,
  295. * if carrier sense is lost during frame transmission, the TXE
  296. * bit is set, "and continues the buffer transmission normally."
  297. * I don't know if "normally" implies TXB is set when the buffer
  298. * descriptor is closed.....trial and error :-).
  299. */
  300. /* Transmit OK, or non-fatal error. Update the buffer descriptors.
  301. */
  302. if (int_events & (SCCE_ENET_TXE | SCCE_ENET_TXB)) {
  303. spin_lock(&cep->lock);
  304. bdp = cep->dirty_tx;
  305. while ((bdp->cbd_sc&BD_ENET_TX_READY)==0) {
  306. if ((bdp==cep->cur_tx) && (cep->tx_full == 0))
  307. break;
  308. if (bdp->cbd_sc & BD_ENET_TX_HB) /* No heartbeat */
  309. cep->stats.tx_heartbeat_errors++;
  310. if (bdp->cbd_sc & BD_ENET_TX_LC) /* Late collision */
  311. cep->stats.tx_window_errors++;
  312. if (bdp->cbd_sc & BD_ENET_TX_RL) /* Retrans limit */
  313. cep->stats.tx_aborted_errors++;
  314. if (bdp->cbd_sc & BD_ENET_TX_UN) /* Underrun */
  315. cep->stats.tx_fifo_errors++;
  316. if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
  317. cep->stats.tx_carrier_errors++;
  318. /* No heartbeat or Lost carrier are not really bad errors.
  319. * The others require a restart transmit command.
  320. */
  321. if (bdp->cbd_sc &
  322. (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  323. must_restart = 1;
  324. cep->stats.tx_errors++;
  325. }
  326. cep->stats.tx_packets++;
  327. /* Deferred means some collisions occurred during transmit,
  328. * but we eventually sent the packet OK.
  329. */
  330. if (bdp->cbd_sc & BD_ENET_TX_DEF)
  331. cep->stats.collisions++;
  332. /* Free the sk buffer associated with this last transmit.
  333. */
  334. dev_kfree_skb_irq(cep->tx_skbuff[cep->skb_dirty]);
  335. cep->skb_dirty = (cep->skb_dirty + 1) & TX_RING_MOD_MASK;
  336. /* Update pointer to next buffer descriptor to be transmitted.
  337. */
  338. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  339. bdp = cep->tx_bd_base;
  340. else
  341. bdp++;
  342. /* I don't know if we can be held off from processing these
  343. * interrupts for more than one frame time. I really hope
  344. * not. In such a case, we would now want to check the
  345. * currently available BD (cur_tx) and determine if any
  346. * buffers between the dirty_tx and cur_tx have also been
  347. * sent. We would want to process anything in between that
  348. * does not have BD_ENET_TX_READY set.
  349. */
  350. /* Since we have freed up a buffer, the ring is no longer
  351. * full.
  352. */
  353. if (cep->tx_full) {
  354. cep->tx_full = 0;
  355. if (netif_queue_stopped(dev))
  356. netif_wake_queue(dev);
  357. }
  358. cep->dirty_tx = (cbd_t *)bdp;
  359. }
  360. if (must_restart) {
  361. volatile cpm8xx_t *cp;
  362. /* Some transmit errors cause the transmitter to shut
  363. * down. We now issue a restart transmit. Since the
  364. * errors close the BD and update the pointers, the restart
  365. * _should_ pick up without having to reset any of our
  366. * pointers either.
  367. */
  368. cp = cpmp;
  369. cp->cp_cpcr =
  370. mk_cr_cmd(CPM_CR_ENET, CPM_CR_RESTART_TX) | CPM_CR_FLG;
  371. while (cp->cp_cpcr & CPM_CR_FLG);
  372. }
  373. spin_unlock(&cep->lock);
  374. }
  375. /* Check for receive busy, i.e. packets coming but no place to
  376. * put them. This "can't happen" because the receive interrupt
  377. * is tossing previous frames.
  378. */
  379. if (int_events & SCCE_ENET_BSY) {
  380. cep->stats.rx_dropped++;
  381. printk("CPM ENET: BSY can't happen.\n");
  382. }
  383. return;
  384. }
  385. /* During a receive, the cur_rx points to the current incoming buffer.
  386. * When we update through the ring, if the next incoming buffer has
  387. * not been given to the system, we just set the empty indicator,
  388. * effectively tossing the packet.
  389. */
  390. static int
  391. scc_enet_rx(struct net_device *dev)
  392. {
  393. struct scc_enet_private *cep;
  394. volatile cbd_t *bdp;
  395. struct sk_buff *skb;
  396. ushort pkt_len;
  397. cep = (struct scc_enet_private *)dev->priv;
  398. /* First, grab all of the stats for the incoming packet.
  399. * These get messed up if we get called due to a busy condition.
  400. */
  401. bdp = cep->cur_rx;
  402. for (;;) {
  403. if (bdp->cbd_sc & BD_ENET_RX_EMPTY)
  404. break;
  405. #ifndef final_version
  406. /* Since we have allocated space to hold a complete frame, both
  407. * the first and last indicators should be set.
  408. */
  409. if ((bdp->cbd_sc & (BD_ENET_RX_FIRST | BD_ENET_RX_LAST)) !=
  410. (BD_ENET_RX_FIRST | BD_ENET_RX_LAST))
  411. printk("CPM ENET: rcv is not first+last\n");
  412. #endif
  413. /* Frame too long or too short.
  414. */
  415. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  416. cep->stats.rx_length_errors++;
  417. if (bdp->cbd_sc & BD_ENET_RX_NO) /* Frame alignment */
  418. cep->stats.rx_frame_errors++;
  419. if (bdp->cbd_sc & BD_ENET_RX_CR) /* CRC Error */
  420. cep->stats.rx_crc_errors++;
  421. if (bdp->cbd_sc & BD_ENET_RX_OV) /* FIFO overrun */
  422. cep->stats.rx_crc_errors++;
  423. /* Report late collisions as a frame error.
  424. * On this error, the BD is closed, but we don't know what we
  425. * have in the buffer. So, just drop this frame on the floor.
  426. */
  427. if (bdp->cbd_sc & BD_ENET_RX_CL) {
  428. cep->stats.rx_frame_errors++;
  429. }
  430. else {
  431. /* Process the incoming frame.
  432. */
  433. cep->stats.rx_packets++;
  434. pkt_len = bdp->cbd_datlen;
  435. cep->stats.rx_bytes += pkt_len;
  436. /* This does 16 byte alignment, much more than we need.
  437. * The packet length includes FCS, but we don't want to
  438. * include that when passing upstream as it messes up
  439. * bridging applications.
  440. */
  441. skb = dev_alloc_skb(pkt_len-4);
  442. if (skb == NULL) {
  443. printk("%s: Memory squeeze, dropping packet.\n", dev->name);
  444. cep->stats.rx_dropped++;
  445. }
  446. else {
  447. skb->dev = dev;
  448. skb_put(skb,pkt_len-4); /* Make room */
  449. eth_copy_and_sum(skb,
  450. cep->rx_vaddr[bdp - cep->rx_bd_base],
  451. pkt_len-4, 0);
  452. skb->protocol=eth_type_trans(skb,dev);
  453. netif_rx(skb);
  454. }
  455. }
  456. /* Clear the status flags for this buffer.
  457. */
  458. bdp->cbd_sc &= ~BD_ENET_RX_STATS;
  459. /* Mark the buffer empty.
  460. */
  461. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  462. /* Update BD pointer to next entry.
  463. */
  464. if (bdp->cbd_sc & BD_ENET_RX_WRAP)
  465. bdp = cep->rx_bd_base;
  466. else
  467. bdp++;
  468. }
  469. cep->cur_rx = (cbd_t *)bdp;
  470. return 0;
  471. }
  472. static int
  473. scc_enet_close(struct net_device *dev)
  474. {
  475. /* Don't know what to do yet.
  476. */
  477. netif_stop_queue(dev);
  478. return 0;
  479. }
  480. static struct net_device_stats *scc_enet_get_stats(struct net_device *dev)
  481. {
  482. struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
  483. return &cep->stats;
  484. }
  485. /* Set or clear the multicast filter for this adaptor.
  486. * Skeleton taken from sunlance driver.
  487. * The CPM Ethernet implementation allows Multicast as well as individual
  488. * MAC address filtering. Some of the drivers check to make sure it is
  489. * a group multicast address, and discard those that are not. I guess I
  490. * will do the same for now, but just remove the test if you want
  491. * individual filtering as well (do the upper net layers want or support
  492. * this kind of feature?).
  493. */
  494. static void set_multicast_list(struct net_device *dev)
  495. {
  496. struct scc_enet_private *cep;
  497. struct dev_mc_list *dmi;
  498. u_char *mcptr, *tdptr;
  499. volatile scc_enet_t *ep;
  500. int i, j;
  501. cep = (struct scc_enet_private *)dev->priv;
  502. /* Get pointer to SCC area in parameter RAM.
  503. */
  504. ep = (scc_enet_t *)dev->base_addr;
  505. if (dev->flags&IFF_PROMISC) {
  506. /* Log any net taps. */
  507. printk("%s: Promiscuous mode enabled.\n", dev->name);
  508. cep->sccp->scc_psmr |= SCC_PSMR_PRO;
  509. } else {
  510. cep->sccp->scc_psmr &= ~SCC_PSMR_PRO;
  511. if (dev->flags & IFF_ALLMULTI) {
  512. /* Catch all multicast addresses, so set the
  513. * filter to all 1's.
  514. */
  515. ep->sen_gaddr1 = 0xffff;
  516. ep->sen_gaddr2 = 0xffff;
  517. ep->sen_gaddr3 = 0xffff;
  518. ep->sen_gaddr4 = 0xffff;
  519. }
  520. else {
  521. /* Clear filter and add the addresses in the list.
  522. */
  523. ep->sen_gaddr1 = 0;
  524. ep->sen_gaddr2 = 0;
  525. ep->sen_gaddr3 = 0;
  526. ep->sen_gaddr4 = 0;
  527. dmi = dev->mc_list;
  528. for (i=0; i<dev->mc_count; i++) {
  529. /* Only support group multicast for now.
  530. */
  531. if (!(dmi->dmi_addr[0] & 1))
  532. continue;
  533. /* The address in dmi_addr is LSB first,
  534. * and taddr is MSB first. We have to
  535. * copy bytes MSB first from dmi_addr.
  536. */
  537. mcptr = (u_char *)dmi->dmi_addr + 5;
  538. tdptr = (u_char *)&ep->sen_taddrh;
  539. for (j=0; j<6; j++)
  540. *tdptr++ = *mcptr--;
  541. /* Ask CPM to run CRC and set bit in
  542. * filter mask.
  543. */
  544. cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_SET_GADDR) | CPM_CR_FLG;
  545. /* this delay is necessary here -- Cort */
  546. udelay(10);
  547. while (cpmp->cp_cpcr & CPM_CR_FLG);
  548. }
  549. }
  550. }
  551. }
  552. /* Initialize the CPM Ethernet on SCC. If EPPC-Bug loaded us, or performed
  553. * some other network I/O, a whole bunch of this has already been set up.
  554. * It is no big deal if we do it again, we just have to disable the
  555. * transmit and receive to make sure we don't catch the CPM with some
  556. * inconsistent control information.
  557. */
  558. static int __init scc_enet_init(void)
  559. {
  560. struct net_device *dev;
  561. struct scc_enet_private *cep;
  562. int i, j, k, err;
  563. uint dp_offset;
  564. unsigned char *eap, *ba;
  565. dma_addr_t mem_addr;
  566. bd_t *bd;
  567. volatile cbd_t *bdp;
  568. volatile cpm8xx_t *cp;
  569. volatile scc_t *sccp;
  570. volatile scc_enet_t *ep;
  571. volatile immap_t *immap;
  572. cp = cpmp; /* Get pointer to Communication Processor */
  573. immap = (immap_t *)(mfspr(SPRN_IMMR) & 0xFFFF0000); /* and to internal registers */
  574. bd = (bd_t *)__res;
  575. dev = alloc_etherdev(sizeof(*cep));
  576. if (!dev)
  577. return -ENOMEM;
  578. cep = dev->priv;
  579. spin_lock_init(&cep->lock);
  580. /* Get pointer to SCC area in parameter RAM.
  581. */
  582. ep = (scc_enet_t *)(&cp->cp_dparam[PROFF_ENET]);
  583. /* And another to the SCC register area.
  584. */
  585. sccp = (volatile scc_t *)(&cp->cp_scc[SCC_ENET]);
  586. cep->sccp = (scc_t *)sccp; /* Keep the pointer handy */
  587. /* Disable receive and transmit in case EPPC-Bug started it.
  588. */
  589. sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  590. /* Cookbook style from the MPC860 manual.....
  591. * Not all of this is necessary if EPPC-Bug has initialized
  592. * the network.
  593. * So far we are lucky, all board configurations use the same
  594. * pins, or at least the same I/O Port for these functions.....
  595. * It can't last though......
  596. */
  597. #if (defined(PA_ENET_RXD) && defined(PA_ENET_TXD))
  598. /* Configure port A pins for Txd and Rxd.
  599. */
  600. immap->im_ioport.iop_papar |= (PA_ENET_RXD | PA_ENET_TXD);
  601. immap->im_ioport.iop_padir &= ~(PA_ENET_RXD | PA_ENET_TXD);
  602. immap->im_ioport.iop_paodr &= ~PA_ENET_TXD;
  603. #elif (defined(PB_ENET_RXD) && defined(PB_ENET_TXD))
  604. /* Configure port B pins for Txd and Rxd.
  605. */
  606. immap->im_cpm.cp_pbpar |= (PB_ENET_RXD | PB_ENET_TXD);
  607. immap->im_cpm.cp_pbdir &= ~(PB_ENET_RXD | PB_ENET_TXD);
  608. immap->im_cpm.cp_pbodr &= ~PB_ENET_TXD;
  609. #else
  610. #error Exactly ONE pair of PA_ENET_[RT]XD, PB_ENET_[RT]XD must be defined
  611. #endif
  612. #if defined(PC_ENET_LBK)
  613. /* Configure port C pins to disable External Loopback
  614. */
  615. immap->im_ioport.iop_pcpar &= ~PC_ENET_LBK;
  616. immap->im_ioport.iop_pcdir |= PC_ENET_LBK;
  617. immap->im_ioport.iop_pcso &= ~PC_ENET_LBK;
  618. immap->im_ioport.iop_pcdat &= ~PC_ENET_LBK; /* Disable Loopback */
  619. #endif /* PC_ENET_LBK */
  620. /* Configure port C pins to enable CLSN and RENA.
  621. */
  622. immap->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  623. immap->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  624. immap->im_ioport.iop_pcso |= (PC_ENET_CLSN | PC_ENET_RENA);
  625. /* Configure port A for TCLK and RCLK.
  626. */
  627. immap->im_ioport.iop_papar |= (PA_ENET_TCLK | PA_ENET_RCLK);
  628. immap->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
  629. /* Configure Serial Interface clock routing.
  630. * First, clear all SCC bits to zero, then set the ones we want.
  631. */
  632. cp->cp_sicr &= ~SICR_ENET_MASK;
  633. cp->cp_sicr |= SICR_ENET_CLKRT;
  634. /* Manual says set SDDR, but I can't find anything with that
  635. * name. I think it is a misprint, and should be SDCR. This
  636. * has already been set by the communication processor initialization.
  637. */
  638. /* Allocate space for the buffer descriptors in the DP ram.
  639. * These are relative offsets in the DP ram address space.
  640. * Initialize base addresses for the buffer descriptors.
  641. */
  642. dp_offset = cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
  643. ep->sen_genscc.scc_rbase = dp_offset;
  644. cep->rx_bd_base = cpm_dpram_addr(dp_offset);
  645. dp_offset = cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
  646. ep->sen_genscc.scc_tbase = dp_offset;
  647. cep->tx_bd_base = cpm_dpram_addr(dp_offset);
  648. cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
  649. cep->cur_rx = cep->rx_bd_base;
  650. /* Issue init Rx BD command for SCC.
  651. * Manual says to perform an Init Rx parameters here. We have
  652. * to perform both Rx and Tx because the SCC may have been
  653. * already running.
  654. * In addition, we have to do it later because we don't yet have
  655. * all of the BD control/status set properly.
  656. cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_RX) | CPM_CR_FLG;
  657. while (cp->cp_cpcr & CPM_CR_FLG);
  658. */
  659. /* Initialize function code registers for big-endian.
  660. */
  661. ep->sen_genscc.scc_rfcr = SCC_EB;
  662. ep->sen_genscc.scc_tfcr = SCC_EB;
  663. /* Set maximum bytes per receive buffer.
  664. * This appears to be an Ethernet frame size, not the buffer
  665. * fragment size. It must be a multiple of four.
  666. */
  667. ep->sen_genscc.scc_mrblr = PKT_MAXBLR_SIZE;
  668. /* Set CRC preset and mask.
  669. */
  670. ep->sen_cpres = 0xffffffff;
  671. ep->sen_cmask = 0xdebb20e3;
  672. ep->sen_crcec = 0; /* CRC Error counter */
  673. ep->sen_alec = 0; /* alignment error counter */
  674. ep->sen_disfc = 0; /* discard frame counter */
  675. ep->sen_pads = 0x8888; /* Tx short frame pad character */
  676. ep->sen_retlim = 15; /* Retry limit threshold */
  677. ep->sen_maxflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  678. ep->sen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  679. ep->sen_maxd1 = PKT_MAXBLR_SIZE; /* maximum DMA1 length */
  680. ep->sen_maxd2 = PKT_MAXBLR_SIZE; /* maximum DMA2 length */
  681. /* Clear hash tables.
  682. */
  683. ep->sen_gaddr1 = 0;
  684. ep->sen_gaddr2 = 0;
  685. ep->sen_gaddr3 = 0;
  686. ep->sen_gaddr4 = 0;
  687. ep->sen_iaddr1 = 0;
  688. ep->sen_iaddr2 = 0;
  689. ep->sen_iaddr3 = 0;
  690. ep->sen_iaddr4 = 0;
  691. /* Set Ethernet station address.
  692. */
  693. eap = (unsigned char *)&(ep->sen_paddrh);
  694. for (i=5; i>=0; i--)
  695. *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
  696. ep->sen_pper = 0; /* 'cause the book says so */
  697. ep->sen_taddrl = 0; /* temp address (LSB) */
  698. ep->sen_taddrm = 0;
  699. ep->sen_taddrh = 0; /* temp address (MSB) */
  700. /* Now allocate the host memory pages and initialize the
  701. * buffer descriptors.
  702. */
  703. bdp = cep->tx_bd_base;
  704. for (i=0; i<TX_RING_SIZE; i++) {
  705. /* Initialize the BD for every fragment in the page.
  706. */
  707. bdp->cbd_sc = 0;
  708. bdp->cbd_bufaddr = 0;
  709. bdp++;
  710. }
  711. /* Set the last buffer to wrap.
  712. */
  713. bdp--;
  714. bdp->cbd_sc |= BD_SC_WRAP;
  715. bdp = cep->rx_bd_base;
  716. k = 0;
  717. for (i=0; i<CPM_ENET_RX_PAGES; i++) {
  718. /* Allocate a page.
  719. */
  720. ba = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE,
  721. &mem_addr, GFP_KERNEL);
  722. /* BUG: no check for failure */
  723. /* Initialize the BD for every fragment in the page.
  724. */
  725. for (j=0; j<CPM_ENET_RX_FRPPG; j++) {
  726. bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
  727. bdp->cbd_bufaddr = mem_addr;
  728. cep->rx_vaddr[k++] = ba;
  729. mem_addr += CPM_ENET_RX_FRSIZE;
  730. ba += CPM_ENET_RX_FRSIZE;
  731. bdp++;
  732. }
  733. }
  734. /* Set the last buffer to wrap.
  735. */
  736. bdp--;
  737. bdp->cbd_sc |= BD_SC_WRAP;
  738. /* Let's re-initialize the channel now. We have to do it later
  739. * than the manual describes because we have just now finished
  740. * the BD initialization.
  741. */
  742. cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  743. while (cp->cp_cpcr & CPM_CR_FLG);
  744. cep->skb_cur = cep->skb_dirty = 0;
  745. sccp->scc_scce = 0xffff; /* Clear any pending events */
  746. /* Enable interrupts for transmit error, complete frame
  747. * received, and any transmit buffer we have also set the
  748. * interrupt flag.
  749. */
  750. sccp->scc_sccm = (SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
  751. /* Install our interrupt handler.
  752. */
  753. cpm_install_handler(CPMVEC_ENET, scc_enet_interrupt, dev);
  754. /* Set GSMR_H to enable all normal operating modes.
  755. * Set GSMR_L to enable Ethernet to MC68160.
  756. */
  757. sccp->scc_gsmrh = 0;
  758. sccp->scc_gsmrl = (SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 | SCC_GSMRL_MODE_ENET);
  759. /* Set sync/delimiters.
  760. */
  761. sccp->scc_dsr = 0xd555;
  762. /* Set processing mode. Use Ethernet CRC, catch broadcast, and
  763. * start frame search 22 bit times after RENA.
  764. */
  765. sccp->scc_psmr = (SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
  766. /* It is now OK to enable the Ethernet transmitter.
  767. * Unfortunately, there are board implementation differences here.
  768. */
  769. #if (!defined (PB_ENET_TENA) && defined (PC_ENET_TENA))
  770. immap->im_ioport.iop_pcpar |= PC_ENET_TENA;
  771. immap->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
  772. #elif ( defined (PB_ENET_TENA) && !defined (PC_ENET_TENA))
  773. cp->cp_pbpar |= PB_ENET_TENA;
  774. cp->cp_pbdir |= PB_ENET_TENA;
  775. #else
  776. #error Configuration Error: define exactly ONE of PB_ENET_TENA, PC_ENET_TENA
  777. #endif
  778. #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
  779. /* And while we are here, set the configuration to enable ethernet.
  780. */
  781. *((volatile uint *)RPX_CSR_ADDR) &= ~BCSR0_ETHLPBK;
  782. *((volatile uint *)RPX_CSR_ADDR) |=
  783. (BCSR0_ETHEN | BCSR0_COLTESTDIS | BCSR0_FULLDPLXDIS);
  784. #endif
  785. #ifdef CONFIG_BSEIP
  786. /* BSE uses port B and C for PHY control.
  787. */
  788. cp->cp_pbpar &= ~(PB_BSE_POWERUP | PB_BSE_FDXDIS);
  789. cp->cp_pbdir |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
  790. cp->cp_pbdat |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
  791. immap->im_ioport.iop_pcpar &= ~PC_BSE_LOOPBACK;
  792. immap->im_ioport.iop_pcdir |= PC_BSE_LOOPBACK;
  793. immap->im_ioport.iop_pcso &= ~PC_BSE_LOOPBACK;
  794. immap->im_ioport.iop_pcdat &= ~PC_BSE_LOOPBACK;
  795. #endif
  796. #ifdef CONFIG_FADS
  797. cp->cp_pbpar |= PB_ENET_TENA;
  798. cp->cp_pbdir |= PB_ENET_TENA;
  799. /* Enable the EEST PHY.
  800. */
  801. *((volatile uint *)BCSR1) &= ~BCSR1_ETHEN;
  802. #endif
  803. dev->base_addr = (unsigned long)ep;
  804. #if 0
  805. dev->name = "CPM_ENET";
  806. #endif
  807. /* The CPM Ethernet specific entries in the device structure. */
  808. dev->open = scc_enet_open;
  809. dev->hard_start_xmit = scc_enet_start_xmit;
  810. dev->tx_timeout = scc_enet_timeout;
  811. dev->watchdog_timeo = TX_TIMEOUT;
  812. dev->stop = scc_enet_close;
  813. dev->get_stats = scc_enet_get_stats;
  814. dev->set_multicast_list = set_multicast_list;
  815. err = register_netdev(dev);
  816. if (err) {
  817. free_netdev(dev);
  818. return err;
  819. }
  820. /* And last, enable the transmit and receive processing.
  821. */
  822. sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  823. printk("%s: CPM ENET Version 0.2 on SCC%d, ", dev->name, SCC_ENET+1);
  824. for (i=0; i<5; i++)
  825. printk("%02x:", dev->dev_addr[i]);
  826. printk("%02x\n", dev->dev_addr[5]);
  827. return 0;
  828. }
  829. module_init(scc_enet_init);