enet.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. #ifdef PE_ENET_TCLK
  621. /* Configure port E for TCLK and RCLK.
  622. */
  623. cp->cp_pepar |= (PE_ENET_TCLK | PE_ENET_RCLK);
  624. cp->cp_pedir &= ~(PE_ENET_TCLK | PE_ENET_RCLK);
  625. cp->cp_peso &= ~(PE_ENET_TCLK | PE_ENET_RCLK);
  626. #else
  627. /* Configure port A for TCLK and RCLK.
  628. */
  629. immap->im_ioport.iop_papar |= (PA_ENET_TCLK | PA_ENET_RCLK);
  630. immap->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
  631. #endif
  632. /* Configure port C pins to enable CLSN and RENA.
  633. */
  634. immap->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  635. immap->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  636. immap->im_ioport.iop_pcso |= (PC_ENET_CLSN | PC_ENET_RENA);
  637. /* Configure Serial Interface clock routing.
  638. * First, clear all SCC bits to zero, then set the ones we want.
  639. */
  640. cp->cp_sicr &= ~SICR_ENET_MASK;
  641. cp->cp_sicr |= SICR_ENET_CLKRT;
  642. /* Manual says set SDDR, but I can't find anything with that
  643. * name. I think it is a misprint, and should be SDCR. This
  644. * has already been set by the communication processor initialization.
  645. */
  646. /* Allocate space for the buffer descriptors in the DP ram.
  647. * These are relative offsets in the DP ram address space.
  648. * Initialize base addresses for the buffer descriptors.
  649. */
  650. dp_offset = cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE, 8);
  651. ep->sen_genscc.scc_rbase = dp_offset;
  652. cep->rx_bd_base = cpm_dpram_addr(dp_offset);
  653. dp_offset = cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE, 8);
  654. ep->sen_genscc.scc_tbase = dp_offset;
  655. cep->tx_bd_base = cpm_dpram_addr(dp_offset);
  656. cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
  657. cep->cur_rx = cep->rx_bd_base;
  658. /* Issue init Rx BD command for SCC.
  659. * Manual says to perform an Init Rx parameters here. We have
  660. * to perform both Rx and Tx because the SCC may have been
  661. * already running.
  662. * In addition, we have to do it later because we don't yet have
  663. * all of the BD control/status set properly.
  664. cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_RX) | CPM_CR_FLG;
  665. while (cp->cp_cpcr & CPM_CR_FLG);
  666. */
  667. /* Initialize function code registers for big-endian.
  668. */
  669. ep->sen_genscc.scc_rfcr = SCC_EB;
  670. ep->sen_genscc.scc_tfcr = SCC_EB;
  671. /* Set maximum bytes per receive buffer.
  672. * This appears to be an Ethernet frame size, not the buffer
  673. * fragment size. It must be a multiple of four.
  674. */
  675. ep->sen_genscc.scc_mrblr = PKT_MAXBLR_SIZE;
  676. /* Set CRC preset and mask.
  677. */
  678. ep->sen_cpres = 0xffffffff;
  679. ep->sen_cmask = 0xdebb20e3;
  680. ep->sen_crcec = 0; /* CRC Error counter */
  681. ep->sen_alec = 0; /* alignment error counter */
  682. ep->sen_disfc = 0; /* discard frame counter */
  683. ep->sen_pads = 0x8888; /* Tx short frame pad character */
  684. ep->sen_retlim = 15; /* Retry limit threshold */
  685. ep->sen_maxflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  686. ep->sen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  687. ep->sen_maxd1 = PKT_MAXBLR_SIZE; /* maximum DMA1 length */
  688. ep->sen_maxd2 = PKT_MAXBLR_SIZE; /* maximum DMA2 length */
  689. /* Clear hash tables.
  690. */
  691. ep->sen_gaddr1 = 0;
  692. ep->sen_gaddr2 = 0;
  693. ep->sen_gaddr3 = 0;
  694. ep->sen_gaddr4 = 0;
  695. ep->sen_iaddr1 = 0;
  696. ep->sen_iaddr2 = 0;
  697. ep->sen_iaddr3 = 0;
  698. ep->sen_iaddr4 = 0;
  699. /* Set Ethernet station address.
  700. */
  701. eap = (unsigned char *)&(ep->sen_paddrh);
  702. for (i=5; i>=0; i--)
  703. *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
  704. ep->sen_pper = 0; /* 'cause the book says so */
  705. ep->sen_taddrl = 0; /* temp address (LSB) */
  706. ep->sen_taddrm = 0;
  707. ep->sen_taddrh = 0; /* temp address (MSB) */
  708. /* Now allocate the host memory pages and initialize the
  709. * buffer descriptors.
  710. */
  711. bdp = cep->tx_bd_base;
  712. for (i=0; i<TX_RING_SIZE; i++) {
  713. /* Initialize the BD for every fragment in the page.
  714. */
  715. bdp->cbd_sc = 0;
  716. bdp->cbd_bufaddr = 0;
  717. bdp++;
  718. }
  719. /* Set the last buffer to wrap.
  720. */
  721. bdp--;
  722. bdp->cbd_sc |= BD_SC_WRAP;
  723. bdp = cep->rx_bd_base;
  724. k = 0;
  725. for (i=0; i<CPM_ENET_RX_PAGES; i++) {
  726. /* Allocate a page.
  727. */
  728. ba = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE,
  729. &mem_addr, GFP_KERNEL);
  730. /* BUG: no check for failure */
  731. /* Initialize the BD for every fragment in the page.
  732. */
  733. for (j=0; j<CPM_ENET_RX_FRPPG; j++) {
  734. bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
  735. bdp->cbd_bufaddr = mem_addr;
  736. cep->rx_vaddr[k++] = ba;
  737. mem_addr += CPM_ENET_RX_FRSIZE;
  738. ba += CPM_ENET_RX_FRSIZE;
  739. bdp++;
  740. }
  741. }
  742. /* Set the last buffer to wrap.
  743. */
  744. bdp--;
  745. bdp->cbd_sc |= BD_SC_WRAP;
  746. /* Let's re-initialize the channel now. We have to do it later
  747. * than the manual describes because we have just now finished
  748. * the BD initialization.
  749. */
  750. cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  751. while (cp->cp_cpcr & CPM_CR_FLG);
  752. cep->skb_cur = cep->skb_dirty = 0;
  753. sccp->scc_scce = 0xffff; /* Clear any pending events */
  754. /* Enable interrupts for transmit error, complete frame
  755. * received, and any transmit buffer we have also set the
  756. * interrupt flag.
  757. */
  758. sccp->scc_sccm = (SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
  759. /* Install our interrupt handler.
  760. */
  761. cpm_install_handler(CPMVEC_ENET, scc_enet_interrupt, dev);
  762. /* Set GSMR_H to enable all normal operating modes.
  763. * Set GSMR_L to enable Ethernet to MC68160.
  764. */
  765. sccp->scc_gsmrh = 0;
  766. sccp->scc_gsmrl = (SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 | SCC_GSMRL_MODE_ENET);
  767. /* Set sync/delimiters.
  768. */
  769. sccp->scc_dsr = 0xd555;
  770. /* Set processing mode. Use Ethernet CRC, catch broadcast, and
  771. * start frame search 22 bit times after RENA.
  772. */
  773. sccp->scc_psmr = (SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
  774. /* It is now OK to enable the Ethernet transmitter.
  775. * Unfortunately, there are board implementation differences here.
  776. */
  777. #if (!defined (PB_ENET_TENA) && defined (PC_ENET_TENA) && !defined (PE_ENET_TENA))
  778. immap->im_ioport.iop_pcpar |= PC_ENET_TENA;
  779. immap->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
  780. #elif ( defined (PB_ENET_TENA) && !defined (PC_ENET_TENA) && !defined (PE_ENET_TENA))
  781. cp->cp_pbpar |= PB_ENET_TENA;
  782. cp->cp_pbdir |= PB_ENET_TENA;
  783. #elif ( !defined (PB_ENET_TENA) && !defined (PC_ENET_TENA) && defined (PE_ENET_TENA))
  784. cp->cp_pepar |= PE_ENET_TENA;
  785. cp->cp_pedir &= ~PE_ENET_TENA;
  786. cp->cp_peso |= PE_ENET_TENA;
  787. #else
  788. #error Configuration Error: define exactly ONE of PB_ENET_TENA, PC_ENET_TENA, PE_ENET_TENA
  789. #endif
  790. #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
  791. /* And while we are here, set the configuration to enable ethernet.
  792. */
  793. *((volatile uint *)RPX_CSR_ADDR) &= ~BCSR0_ETHLPBK;
  794. *((volatile uint *)RPX_CSR_ADDR) |=
  795. (BCSR0_ETHEN | BCSR0_COLTESTDIS | BCSR0_FULLDPLXDIS);
  796. #endif
  797. #ifdef CONFIG_BSEIP
  798. /* BSE uses port B and C for PHY control.
  799. */
  800. cp->cp_pbpar &= ~(PB_BSE_POWERUP | PB_BSE_FDXDIS);
  801. cp->cp_pbdir |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
  802. cp->cp_pbdat |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
  803. immap->im_ioport.iop_pcpar &= ~PC_BSE_LOOPBACK;
  804. immap->im_ioport.iop_pcdir |= PC_BSE_LOOPBACK;
  805. immap->im_ioport.iop_pcso &= ~PC_BSE_LOOPBACK;
  806. immap->im_ioport.iop_pcdat &= ~PC_BSE_LOOPBACK;
  807. #endif
  808. #ifdef CONFIG_FADS
  809. cp->cp_pbpar |= PB_ENET_TENA;
  810. cp->cp_pbdir |= PB_ENET_TENA;
  811. /* Enable the EEST PHY.
  812. */
  813. *((volatile uint *)BCSR1) &= ~BCSR1_ETHEN;
  814. #endif
  815. #ifdef CONFIG_MPC885ADS
  816. /* Deassert PHY reset and enable the PHY.
  817. */
  818. {
  819. volatile uint __iomem *bcsr = ioremap(BCSR_ADDR, BCSR_SIZE);
  820. uint tmp;
  821. tmp = in_be32(bcsr + 1 /* BCSR1 */);
  822. tmp |= BCSR1_ETHEN;
  823. out_be32(bcsr + 1, tmp);
  824. tmp = in_be32(bcsr + 4 /* BCSR4 */);
  825. tmp |= BCSR4_ETH10_RST;
  826. out_be32(bcsr + 4, tmp);
  827. iounmap(bcsr);
  828. }
  829. /* On MPC885ADS SCC ethernet PHY defaults to the full duplex mode
  830. * upon reset. SCC is set to half duplex by default. So this
  831. * inconsistency should be better fixed by the software.
  832. */
  833. #endif
  834. dev->base_addr = (unsigned long)ep;
  835. #if 0
  836. dev->name = "CPM_ENET";
  837. #endif
  838. /* The CPM Ethernet specific entries in the device structure. */
  839. dev->open = scc_enet_open;
  840. dev->hard_start_xmit = scc_enet_start_xmit;
  841. dev->tx_timeout = scc_enet_timeout;
  842. dev->watchdog_timeo = TX_TIMEOUT;
  843. dev->stop = scc_enet_close;
  844. dev->get_stats = scc_enet_get_stats;
  845. dev->set_multicast_list = set_multicast_list;
  846. err = register_netdev(dev);
  847. if (err) {
  848. free_netdev(dev);
  849. return err;
  850. }
  851. /* And last, enable the transmit and receive processing.
  852. */
  853. sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  854. printk("%s: CPM ENET Version 0.2 on SCC%d, ", dev->name, SCC_ENET+1);
  855. for (i=0; i<5; i++)
  856. printk("%02x:", dev->dev_addr[i]);
  857. printk("%02x\n", dev->dev_addr[5]);
  858. return 0;
  859. }
  860. module_init(scc_enet_init);