enet.c 29 KB

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