bmac.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /*
  2. * Network device driver for the BMAC ethernet controller on
  3. * Apple Powermacs. Assumes it's under a DBDMA controller.
  4. *
  5. * Copyright (C) 1998 Randy Gobbel.
  6. *
  7. * May 1999, Al Viro: proper release of /proc/net/bmac entry, switched to
  8. * dynamic procfs inode.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/delay.h>
  15. #include <linux/string.h>
  16. #include <linux/timer.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/init.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/crc32.h>
  21. #include <asm/prom.h>
  22. #include <asm/dbdma.h>
  23. #include <asm/io.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/machdep.h>
  27. #include <asm/pmac_feature.h>
  28. #include <asm/macio.h>
  29. #include <asm/irq.h>
  30. #include "bmac.h"
  31. #define trunc_page(x) ((void *)(((unsigned long)(x)) & ~((unsigned long)(PAGE_SIZE - 1))))
  32. #define round_page(x) trunc_page(((unsigned long)(x)) + ((unsigned long)(PAGE_SIZE - 1)))
  33. /*
  34. * CRC polynomial - used in working out multicast filter bits.
  35. */
  36. #define ENET_CRCPOLY 0x04c11db7
  37. /* switch to use multicast code lifted from sunhme driver */
  38. #define SUNHME_MULTICAST
  39. #define N_RX_RING 64
  40. #define N_TX_RING 32
  41. #define MAX_TX_ACTIVE 1
  42. #define ETHERCRC 4
  43. #define ETHERMINPACKET 64
  44. #define ETHERMTU 1500
  45. #define RX_BUFLEN (ETHERMTU + 14 + ETHERCRC + 2)
  46. #define TX_TIMEOUT HZ /* 1 second */
  47. /* Bits in transmit DMA status */
  48. #define TX_DMA_ERR 0x80
  49. #define XXDEBUG(args)
  50. struct bmac_data {
  51. /* volatile struct bmac *bmac; */
  52. struct sk_buff_head *queue;
  53. volatile struct dbdma_regs __iomem *tx_dma;
  54. int tx_dma_intr;
  55. volatile struct dbdma_regs __iomem *rx_dma;
  56. int rx_dma_intr;
  57. volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
  58. volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
  59. struct macio_dev *mdev;
  60. int is_bmac_plus;
  61. struct sk_buff *rx_bufs[N_RX_RING];
  62. int rx_fill;
  63. int rx_empty;
  64. struct sk_buff *tx_bufs[N_TX_RING];
  65. int tx_fill;
  66. int tx_empty;
  67. unsigned char tx_fullup;
  68. struct net_device_stats stats;
  69. struct timer_list tx_timeout;
  70. int timeout_active;
  71. int sleeping;
  72. int opened;
  73. unsigned short hash_use_count[64];
  74. unsigned short hash_table_mask[4];
  75. spinlock_t lock;
  76. };
  77. #if 0 /* Move that to ethtool */
  78. typedef struct bmac_reg_entry {
  79. char *name;
  80. unsigned short reg_offset;
  81. } bmac_reg_entry_t;
  82. #define N_REG_ENTRIES 31
  83. static bmac_reg_entry_t reg_entries[N_REG_ENTRIES] = {
  84. {"MEMADD", MEMADD},
  85. {"MEMDATAHI", MEMDATAHI},
  86. {"MEMDATALO", MEMDATALO},
  87. {"TXPNTR", TXPNTR},
  88. {"RXPNTR", RXPNTR},
  89. {"IPG1", IPG1},
  90. {"IPG2", IPG2},
  91. {"ALIMIT", ALIMIT},
  92. {"SLOT", SLOT},
  93. {"PALEN", PALEN},
  94. {"PAPAT", PAPAT},
  95. {"TXSFD", TXSFD},
  96. {"JAM", JAM},
  97. {"TXCFG", TXCFG},
  98. {"TXMAX", TXMAX},
  99. {"TXMIN", TXMIN},
  100. {"PAREG", PAREG},
  101. {"DCNT", DCNT},
  102. {"NCCNT", NCCNT},
  103. {"NTCNT", NTCNT},
  104. {"EXCNT", EXCNT},
  105. {"LTCNT", LTCNT},
  106. {"TXSM", TXSM},
  107. {"RXCFG", RXCFG},
  108. {"RXMAX", RXMAX},
  109. {"RXMIN", RXMIN},
  110. {"FRCNT", FRCNT},
  111. {"AECNT", AECNT},
  112. {"FECNT", FECNT},
  113. {"RXSM", RXSM},
  114. {"RXCV", RXCV}
  115. };
  116. #endif
  117. static unsigned char *bmac_emergency_rxbuf;
  118. /*
  119. * Number of bytes of private data per BMAC: allow enough for
  120. * the rx and tx dma commands plus a branch dma command each,
  121. * and another 16 bytes to allow us to align the dma command
  122. * buffers on a 16 byte boundary.
  123. */
  124. #define PRIV_BYTES (sizeof(struct bmac_data) \
  125. + (N_RX_RING + N_TX_RING + 4) * sizeof(struct dbdma_cmd) \
  126. + sizeof(struct sk_buff_head))
  127. static unsigned char bitrev(unsigned char b);
  128. static int bmac_open(struct net_device *dev);
  129. static int bmac_close(struct net_device *dev);
  130. static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev);
  131. static struct net_device_stats *bmac_stats(struct net_device *dev);
  132. static void bmac_set_multicast(struct net_device *dev);
  133. static void bmac_reset_and_enable(struct net_device *dev);
  134. static void bmac_start_chip(struct net_device *dev);
  135. static void bmac_init_chip(struct net_device *dev);
  136. static void bmac_init_registers(struct net_device *dev);
  137. static void bmac_enable_and_reset_chip(struct net_device *dev);
  138. static int bmac_set_address(struct net_device *dev, void *addr);
  139. static irqreturn_t bmac_misc_intr(int irq, void *dev_id);
  140. static irqreturn_t bmac_txdma_intr(int irq, void *dev_id);
  141. static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id);
  142. static void bmac_set_timeout(struct net_device *dev);
  143. static void bmac_tx_timeout(unsigned long data);
  144. static int bmac_output(struct sk_buff *skb, struct net_device *dev);
  145. static void bmac_start(struct net_device *dev);
  146. #define DBDMA_SET(x) ( ((x) | (x) << 16) )
  147. #define DBDMA_CLEAR(x) ( (x) << 16)
  148. static inline void
  149. dbdma_st32(volatile __u32 __iomem *a, unsigned long x)
  150. {
  151. __asm__ volatile( "stwbrx %0,0,%1" : : "r" (x), "r" (a) : "memory");
  152. return;
  153. }
  154. static inline unsigned long
  155. dbdma_ld32(volatile __u32 __iomem *a)
  156. {
  157. __u32 swap;
  158. __asm__ volatile ("lwbrx %0,0,%1" : "=r" (swap) : "r" (a));
  159. return swap;
  160. }
  161. static void
  162. dbdma_continue(volatile struct dbdma_regs __iomem *dmap)
  163. {
  164. dbdma_st32(&dmap->control,
  165. DBDMA_SET(RUN|WAKE) | DBDMA_CLEAR(PAUSE|DEAD));
  166. eieio();
  167. }
  168. static void
  169. dbdma_reset(volatile struct dbdma_regs __iomem *dmap)
  170. {
  171. dbdma_st32(&dmap->control,
  172. DBDMA_CLEAR(ACTIVE|DEAD|WAKE|FLUSH|PAUSE|RUN));
  173. eieio();
  174. while (dbdma_ld32(&dmap->status) & RUN)
  175. eieio();
  176. }
  177. static void
  178. dbdma_setcmd(volatile struct dbdma_cmd *cp,
  179. unsigned short cmd, unsigned count, unsigned long addr,
  180. unsigned long cmd_dep)
  181. {
  182. out_le16(&cp->command, cmd);
  183. out_le16(&cp->req_count, count);
  184. out_le32(&cp->phy_addr, addr);
  185. out_le32(&cp->cmd_dep, cmd_dep);
  186. out_le16(&cp->xfer_status, 0);
  187. out_le16(&cp->res_count, 0);
  188. }
  189. static inline
  190. void bmwrite(struct net_device *dev, unsigned long reg_offset, unsigned data )
  191. {
  192. out_le16((void __iomem *)dev->base_addr + reg_offset, data);
  193. }
  194. static inline
  195. unsigned short bmread(struct net_device *dev, unsigned long reg_offset )
  196. {
  197. return in_le16((void __iomem *)dev->base_addr + reg_offset);
  198. }
  199. static void
  200. bmac_enable_and_reset_chip(struct net_device *dev)
  201. {
  202. struct bmac_data *bp = netdev_priv(dev);
  203. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  204. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  205. if (rd)
  206. dbdma_reset(rd);
  207. if (td)
  208. dbdma_reset(td);
  209. pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 1);
  210. }
  211. #define MIFDELAY udelay(10)
  212. static unsigned int
  213. bmac_mif_readbits(struct net_device *dev, int nb)
  214. {
  215. unsigned int val = 0;
  216. while (--nb >= 0) {
  217. bmwrite(dev, MIFCSR, 0);
  218. MIFDELAY;
  219. if (bmread(dev, MIFCSR) & 8)
  220. val |= 1 << nb;
  221. bmwrite(dev, MIFCSR, 1);
  222. MIFDELAY;
  223. }
  224. bmwrite(dev, MIFCSR, 0);
  225. MIFDELAY;
  226. bmwrite(dev, MIFCSR, 1);
  227. MIFDELAY;
  228. return val;
  229. }
  230. static void
  231. bmac_mif_writebits(struct net_device *dev, unsigned int val, int nb)
  232. {
  233. int b;
  234. while (--nb >= 0) {
  235. b = (val & (1 << nb))? 6: 4;
  236. bmwrite(dev, MIFCSR, b);
  237. MIFDELAY;
  238. bmwrite(dev, MIFCSR, b|1);
  239. MIFDELAY;
  240. }
  241. }
  242. static unsigned int
  243. bmac_mif_read(struct net_device *dev, unsigned int addr)
  244. {
  245. unsigned int val;
  246. bmwrite(dev, MIFCSR, 4);
  247. MIFDELAY;
  248. bmac_mif_writebits(dev, ~0U, 32);
  249. bmac_mif_writebits(dev, 6, 4);
  250. bmac_mif_writebits(dev, addr, 10);
  251. bmwrite(dev, MIFCSR, 2);
  252. MIFDELAY;
  253. bmwrite(dev, MIFCSR, 1);
  254. MIFDELAY;
  255. val = bmac_mif_readbits(dev, 17);
  256. bmwrite(dev, MIFCSR, 4);
  257. MIFDELAY;
  258. return val;
  259. }
  260. static void
  261. bmac_mif_write(struct net_device *dev, unsigned int addr, unsigned int val)
  262. {
  263. bmwrite(dev, MIFCSR, 4);
  264. MIFDELAY;
  265. bmac_mif_writebits(dev, ~0U, 32);
  266. bmac_mif_writebits(dev, 5, 4);
  267. bmac_mif_writebits(dev, addr, 10);
  268. bmac_mif_writebits(dev, 2, 2);
  269. bmac_mif_writebits(dev, val, 16);
  270. bmac_mif_writebits(dev, 3, 2);
  271. }
  272. static void
  273. bmac_init_registers(struct net_device *dev)
  274. {
  275. struct bmac_data *bp = netdev_priv(dev);
  276. volatile unsigned short regValue;
  277. unsigned short *pWord16;
  278. int i;
  279. /* XXDEBUG(("bmac: enter init_registers\n")); */
  280. bmwrite(dev, RXRST, RxResetValue);
  281. bmwrite(dev, TXRST, TxResetBit);
  282. i = 100;
  283. do {
  284. --i;
  285. udelay(10000);
  286. regValue = bmread(dev, TXRST); /* wait for reset to clear..acknowledge */
  287. } while ((regValue & TxResetBit) && i > 0);
  288. if (!bp->is_bmac_plus) {
  289. regValue = bmread(dev, XCVRIF);
  290. regValue |= ClkBit | SerialMode | COLActiveLow;
  291. bmwrite(dev, XCVRIF, regValue);
  292. udelay(10000);
  293. }
  294. bmwrite(dev, RSEED, (unsigned short)0x1968);
  295. regValue = bmread(dev, XIFC);
  296. regValue |= TxOutputEnable;
  297. bmwrite(dev, XIFC, regValue);
  298. bmread(dev, PAREG);
  299. /* set collision counters to 0 */
  300. bmwrite(dev, NCCNT, 0);
  301. bmwrite(dev, NTCNT, 0);
  302. bmwrite(dev, EXCNT, 0);
  303. bmwrite(dev, LTCNT, 0);
  304. /* set rx counters to 0 */
  305. bmwrite(dev, FRCNT, 0);
  306. bmwrite(dev, LECNT, 0);
  307. bmwrite(dev, AECNT, 0);
  308. bmwrite(dev, FECNT, 0);
  309. bmwrite(dev, RXCV, 0);
  310. /* set tx fifo information */
  311. bmwrite(dev, TXTH, 4); /* 4 octets before tx starts */
  312. bmwrite(dev, TXFIFOCSR, 0); /* first disable txFIFO */
  313. bmwrite(dev, TXFIFOCSR, TxFIFOEnable );
  314. /* set rx fifo information */
  315. bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
  316. bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
  317. //bmwrite(dev, TXCFG, TxMACEnable); /* TxNeverGiveUp maybe later */
  318. bmread(dev, STATUS); /* read it just to clear it */
  319. /* zero out the chip Hash Filter registers */
  320. for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
  321. bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
  322. bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
  323. bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
  324. bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
  325. pWord16 = (unsigned short *)dev->dev_addr;
  326. bmwrite(dev, MADD0, *pWord16++);
  327. bmwrite(dev, MADD1, *pWord16++);
  328. bmwrite(dev, MADD2, *pWord16);
  329. bmwrite(dev, RXCFG, RxCRCNoStrip | RxHashFilterEnable | RxRejectOwnPackets);
  330. bmwrite(dev, INTDISABLE, EnableNormal);
  331. return;
  332. }
  333. #if 0
  334. static void
  335. bmac_disable_interrupts(struct net_device *dev)
  336. {
  337. bmwrite(dev, INTDISABLE, DisableAll);
  338. }
  339. static void
  340. bmac_enable_interrupts(struct net_device *dev)
  341. {
  342. bmwrite(dev, INTDISABLE, EnableNormal);
  343. }
  344. #endif
  345. static void
  346. bmac_start_chip(struct net_device *dev)
  347. {
  348. struct bmac_data *bp = netdev_priv(dev);
  349. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  350. unsigned short oldConfig;
  351. /* enable rx dma channel */
  352. dbdma_continue(rd);
  353. oldConfig = bmread(dev, TXCFG);
  354. bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
  355. /* turn on rx plus any other bits already on (promiscuous possibly) */
  356. oldConfig = bmread(dev, RXCFG);
  357. bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
  358. udelay(20000);
  359. }
  360. static void
  361. bmac_init_phy(struct net_device *dev)
  362. {
  363. unsigned int addr;
  364. struct bmac_data *bp = netdev_priv(dev);
  365. printk(KERN_DEBUG "phy registers:");
  366. for (addr = 0; addr < 32; ++addr) {
  367. if ((addr & 7) == 0)
  368. printk("\n" KERN_DEBUG);
  369. printk(" %.4x", bmac_mif_read(dev, addr));
  370. }
  371. printk("\n");
  372. if (bp->is_bmac_plus) {
  373. unsigned int capable, ctrl;
  374. ctrl = bmac_mif_read(dev, 0);
  375. capable = ((bmac_mif_read(dev, 1) & 0xf800) >> 6) | 1;
  376. if (bmac_mif_read(dev, 4) != capable
  377. || (ctrl & 0x1000) == 0) {
  378. bmac_mif_write(dev, 4, capable);
  379. bmac_mif_write(dev, 0, 0x1200);
  380. } else
  381. bmac_mif_write(dev, 0, 0x1000);
  382. }
  383. }
  384. static void bmac_init_chip(struct net_device *dev)
  385. {
  386. bmac_init_phy(dev);
  387. bmac_init_registers(dev);
  388. }
  389. #ifdef CONFIG_PM
  390. static int bmac_suspend(struct macio_dev *mdev, pm_message_t state)
  391. {
  392. struct net_device* dev = macio_get_drvdata(mdev);
  393. struct bmac_data *bp = netdev_priv(dev);
  394. unsigned long flags;
  395. unsigned short config;
  396. int i;
  397. netif_device_detach(dev);
  398. /* prolly should wait for dma to finish & turn off the chip */
  399. spin_lock_irqsave(&bp->lock, flags);
  400. if (bp->timeout_active) {
  401. del_timer(&bp->tx_timeout);
  402. bp->timeout_active = 0;
  403. }
  404. disable_irq(dev->irq);
  405. disable_irq(bp->tx_dma_intr);
  406. disable_irq(bp->rx_dma_intr);
  407. bp->sleeping = 1;
  408. spin_unlock_irqrestore(&bp->lock, flags);
  409. if (bp->opened) {
  410. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  411. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  412. config = bmread(dev, RXCFG);
  413. bmwrite(dev, RXCFG, (config & ~RxMACEnable));
  414. config = bmread(dev, TXCFG);
  415. bmwrite(dev, TXCFG, (config & ~TxMACEnable));
  416. bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
  417. /* disable rx and tx dma */
  418. st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
  419. st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
  420. /* free some skb's */
  421. for (i=0; i<N_RX_RING; i++) {
  422. if (bp->rx_bufs[i] != NULL) {
  423. dev_kfree_skb(bp->rx_bufs[i]);
  424. bp->rx_bufs[i] = NULL;
  425. }
  426. }
  427. for (i = 0; i<N_TX_RING; i++) {
  428. if (bp->tx_bufs[i] != NULL) {
  429. dev_kfree_skb(bp->tx_bufs[i]);
  430. bp->tx_bufs[i] = NULL;
  431. }
  432. }
  433. }
  434. pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
  435. return 0;
  436. }
  437. static int bmac_resume(struct macio_dev *mdev)
  438. {
  439. struct net_device* dev = macio_get_drvdata(mdev);
  440. struct bmac_data *bp = netdev_priv(dev);
  441. /* see if this is enough */
  442. if (bp->opened)
  443. bmac_reset_and_enable(dev);
  444. enable_irq(dev->irq);
  445. enable_irq(bp->tx_dma_intr);
  446. enable_irq(bp->rx_dma_intr);
  447. netif_device_attach(dev);
  448. return 0;
  449. }
  450. #endif /* CONFIG_PM */
  451. static int bmac_set_address(struct net_device *dev, void *addr)
  452. {
  453. struct bmac_data *bp = netdev_priv(dev);
  454. unsigned char *p = addr;
  455. unsigned short *pWord16;
  456. unsigned long flags;
  457. int i;
  458. XXDEBUG(("bmac: enter set_address\n"));
  459. spin_lock_irqsave(&bp->lock, flags);
  460. for (i = 0; i < 6; ++i) {
  461. dev->dev_addr[i] = p[i];
  462. }
  463. /* load up the hardware address */
  464. pWord16 = (unsigned short *)dev->dev_addr;
  465. bmwrite(dev, MADD0, *pWord16++);
  466. bmwrite(dev, MADD1, *pWord16++);
  467. bmwrite(dev, MADD2, *pWord16);
  468. spin_unlock_irqrestore(&bp->lock, flags);
  469. XXDEBUG(("bmac: exit set_address\n"));
  470. return 0;
  471. }
  472. static inline void bmac_set_timeout(struct net_device *dev)
  473. {
  474. struct bmac_data *bp = netdev_priv(dev);
  475. unsigned long flags;
  476. spin_lock_irqsave(&bp->lock, flags);
  477. if (bp->timeout_active)
  478. del_timer(&bp->tx_timeout);
  479. bp->tx_timeout.expires = jiffies + TX_TIMEOUT;
  480. bp->tx_timeout.function = bmac_tx_timeout;
  481. bp->tx_timeout.data = (unsigned long) dev;
  482. add_timer(&bp->tx_timeout);
  483. bp->timeout_active = 1;
  484. spin_unlock_irqrestore(&bp->lock, flags);
  485. }
  486. static void
  487. bmac_construct_xmt(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
  488. {
  489. void *vaddr;
  490. unsigned long baddr;
  491. unsigned long len;
  492. len = skb->len;
  493. vaddr = skb->data;
  494. baddr = virt_to_bus(vaddr);
  495. dbdma_setcmd(cp, (OUTPUT_LAST | INTR_ALWAYS | WAIT_IFCLR), len, baddr, 0);
  496. }
  497. static void
  498. bmac_construct_rxbuff(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
  499. {
  500. unsigned char *addr = skb? skb->data: bmac_emergency_rxbuf;
  501. dbdma_setcmd(cp, (INPUT_LAST | INTR_ALWAYS), RX_BUFLEN,
  502. virt_to_bus(addr), 0);
  503. }
  504. /* Bit-reverse one byte of an ethernet hardware address. */
  505. static unsigned char
  506. bitrev(unsigned char b)
  507. {
  508. int d = 0, i;
  509. for (i = 0; i < 8; ++i, b >>= 1)
  510. d = (d << 1) | (b & 1);
  511. return d;
  512. }
  513. static void
  514. bmac_init_tx_ring(struct bmac_data *bp)
  515. {
  516. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  517. memset((char *)bp->tx_cmds, 0, (N_TX_RING+1) * sizeof(struct dbdma_cmd));
  518. bp->tx_empty = 0;
  519. bp->tx_fill = 0;
  520. bp->tx_fullup = 0;
  521. /* put a branch at the end of the tx command list */
  522. dbdma_setcmd(&bp->tx_cmds[N_TX_RING],
  523. (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->tx_cmds));
  524. /* reset tx dma */
  525. dbdma_reset(td);
  526. out_le32(&td->wait_sel, 0x00200020);
  527. out_le32(&td->cmdptr, virt_to_bus(bp->tx_cmds));
  528. }
  529. static int
  530. bmac_init_rx_ring(struct bmac_data *bp)
  531. {
  532. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  533. int i;
  534. struct sk_buff *skb;
  535. /* initialize list of sk_buffs for receiving and set up recv dma */
  536. memset((char *)bp->rx_cmds, 0,
  537. (N_RX_RING + 1) * sizeof(struct dbdma_cmd));
  538. for (i = 0; i < N_RX_RING; i++) {
  539. if ((skb = bp->rx_bufs[i]) == NULL) {
  540. bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
  541. if (skb != NULL)
  542. skb_reserve(skb, 2);
  543. }
  544. bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
  545. }
  546. bp->rx_empty = 0;
  547. bp->rx_fill = i;
  548. /* Put a branch back to the beginning of the receive command list */
  549. dbdma_setcmd(&bp->rx_cmds[N_RX_RING],
  550. (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->rx_cmds));
  551. /* start rx dma */
  552. dbdma_reset(rd);
  553. out_le32(&rd->cmdptr, virt_to_bus(bp->rx_cmds));
  554. return 1;
  555. }
  556. static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev)
  557. {
  558. struct bmac_data *bp = netdev_priv(dev);
  559. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  560. int i;
  561. /* see if there's a free slot in the tx ring */
  562. /* XXDEBUG(("bmac_xmit_start: empty=%d fill=%d\n", */
  563. /* bp->tx_empty, bp->tx_fill)); */
  564. i = bp->tx_fill + 1;
  565. if (i >= N_TX_RING)
  566. i = 0;
  567. if (i == bp->tx_empty) {
  568. netif_stop_queue(dev);
  569. bp->tx_fullup = 1;
  570. XXDEBUG(("bmac_transmit_packet: tx ring full\n"));
  571. return -1; /* can't take it at the moment */
  572. }
  573. dbdma_setcmd(&bp->tx_cmds[i], DBDMA_STOP, 0, 0, 0);
  574. bmac_construct_xmt(skb, &bp->tx_cmds[bp->tx_fill]);
  575. bp->tx_bufs[bp->tx_fill] = skb;
  576. bp->tx_fill = i;
  577. bp->stats.tx_bytes += skb->len;
  578. dbdma_continue(td);
  579. return 0;
  580. }
  581. static int rxintcount;
  582. static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id)
  583. {
  584. struct net_device *dev = (struct net_device *) dev_id;
  585. struct bmac_data *bp = netdev_priv(dev);
  586. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  587. volatile struct dbdma_cmd *cp;
  588. int i, nb, stat;
  589. struct sk_buff *skb;
  590. unsigned int residual;
  591. int last;
  592. unsigned long flags;
  593. spin_lock_irqsave(&bp->lock, flags);
  594. if (++rxintcount < 10) {
  595. XXDEBUG(("bmac_rxdma_intr\n"));
  596. }
  597. last = -1;
  598. i = bp->rx_empty;
  599. while (1) {
  600. cp = &bp->rx_cmds[i];
  601. stat = ld_le16(&cp->xfer_status);
  602. residual = ld_le16(&cp->res_count);
  603. if ((stat & ACTIVE) == 0)
  604. break;
  605. nb = RX_BUFLEN - residual - 2;
  606. if (nb < (ETHERMINPACKET - ETHERCRC)) {
  607. skb = NULL;
  608. bp->stats.rx_length_errors++;
  609. bp->stats.rx_errors++;
  610. } else {
  611. skb = bp->rx_bufs[i];
  612. bp->rx_bufs[i] = NULL;
  613. }
  614. if (skb != NULL) {
  615. nb -= ETHERCRC;
  616. skb_put(skb, nb);
  617. skb->dev = dev;
  618. skb->protocol = eth_type_trans(skb, dev);
  619. netif_rx(skb);
  620. dev->last_rx = jiffies;
  621. ++bp->stats.rx_packets;
  622. bp->stats.rx_bytes += nb;
  623. } else {
  624. ++bp->stats.rx_dropped;
  625. }
  626. dev->last_rx = jiffies;
  627. if ((skb = bp->rx_bufs[i]) == NULL) {
  628. bp->rx_bufs[i] = skb = dev_alloc_skb(RX_BUFLEN+2);
  629. if (skb != NULL)
  630. skb_reserve(bp->rx_bufs[i], 2);
  631. }
  632. bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
  633. st_le16(&cp->res_count, 0);
  634. st_le16(&cp->xfer_status, 0);
  635. last = i;
  636. if (++i >= N_RX_RING) i = 0;
  637. }
  638. if (last != -1) {
  639. bp->rx_fill = last;
  640. bp->rx_empty = i;
  641. }
  642. dbdma_continue(rd);
  643. spin_unlock_irqrestore(&bp->lock, flags);
  644. if (rxintcount < 10) {
  645. XXDEBUG(("bmac_rxdma_intr done\n"));
  646. }
  647. return IRQ_HANDLED;
  648. }
  649. static int txintcount;
  650. static irqreturn_t bmac_txdma_intr(int irq, void *dev_id)
  651. {
  652. struct net_device *dev = (struct net_device *) dev_id;
  653. struct bmac_data *bp = netdev_priv(dev);
  654. volatile struct dbdma_cmd *cp;
  655. int stat;
  656. unsigned long flags;
  657. spin_lock_irqsave(&bp->lock, flags);
  658. if (txintcount++ < 10) {
  659. XXDEBUG(("bmac_txdma_intr\n"));
  660. }
  661. /* del_timer(&bp->tx_timeout); */
  662. /* bp->timeout_active = 0; */
  663. while (1) {
  664. cp = &bp->tx_cmds[bp->tx_empty];
  665. stat = ld_le16(&cp->xfer_status);
  666. if (txintcount < 10) {
  667. XXDEBUG(("bmac_txdma_xfer_stat=%#0x\n", stat));
  668. }
  669. if (!(stat & ACTIVE)) {
  670. /*
  671. * status field might not have been filled by DBDMA
  672. */
  673. if (cp == bus_to_virt(in_le32(&bp->tx_dma->cmdptr)))
  674. break;
  675. }
  676. if (bp->tx_bufs[bp->tx_empty]) {
  677. ++bp->stats.tx_packets;
  678. dev_kfree_skb_irq(bp->tx_bufs[bp->tx_empty]);
  679. }
  680. bp->tx_bufs[bp->tx_empty] = NULL;
  681. bp->tx_fullup = 0;
  682. netif_wake_queue(dev);
  683. if (++bp->tx_empty >= N_TX_RING)
  684. bp->tx_empty = 0;
  685. if (bp->tx_empty == bp->tx_fill)
  686. break;
  687. }
  688. spin_unlock_irqrestore(&bp->lock, flags);
  689. if (txintcount < 10) {
  690. XXDEBUG(("bmac_txdma_intr done->bmac_start\n"));
  691. }
  692. bmac_start(dev);
  693. return IRQ_HANDLED;
  694. }
  695. static struct net_device_stats *bmac_stats(struct net_device *dev)
  696. {
  697. struct bmac_data *p = netdev_priv(dev);
  698. return &p->stats;
  699. }
  700. #ifndef SUNHME_MULTICAST
  701. /* Real fast bit-reversal algorithm, 6-bit values */
  702. static int reverse6[64] = {
  703. 0x0,0x20,0x10,0x30,0x8,0x28,0x18,0x38,
  704. 0x4,0x24,0x14,0x34,0xc,0x2c,0x1c,0x3c,
  705. 0x2,0x22,0x12,0x32,0xa,0x2a,0x1a,0x3a,
  706. 0x6,0x26,0x16,0x36,0xe,0x2e,0x1e,0x3e,
  707. 0x1,0x21,0x11,0x31,0x9,0x29,0x19,0x39,
  708. 0x5,0x25,0x15,0x35,0xd,0x2d,0x1d,0x3d,
  709. 0x3,0x23,0x13,0x33,0xb,0x2b,0x1b,0x3b,
  710. 0x7,0x27,0x17,0x37,0xf,0x2f,0x1f,0x3f
  711. };
  712. static unsigned int
  713. crc416(unsigned int curval, unsigned short nxtval)
  714. {
  715. register unsigned int counter, cur = curval, next = nxtval;
  716. register int high_crc_set, low_data_set;
  717. /* Swap bytes */
  718. next = ((next & 0x00FF) << 8) | (next >> 8);
  719. /* Compute bit-by-bit */
  720. for (counter = 0; counter < 16; ++counter) {
  721. /* is high CRC bit set? */
  722. if ((cur & 0x80000000) == 0) high_crc_set = 0;
  723. else high_crc_set = 1;
  724. cur = cur << 1;
  725. if ((next & 0x0001) == 0) low_data_set = 0;
  726. else low_data_set = 1;
  727. next = next >> 1;
  728. /* do the XOR */
  729. if (high_crc_set ^ low_data_set) cur = cur ^ ENET_CRCPOLY;
  730. }
  731. return cur;
  732. }
  733. static unsigned int
  734. bmac_crc(unsigned short *address)
  735. {
  736. unsigned int newcrc;
  737. XXDEBUG(("bmac_crc: addr=%#04x, %#04x, %#04x\n", *address, address[1], address[2]));
  738. newcrc = crc416(0xffffffff, *address); /* address bits 47 - 32 */
  739. newcrc = crc416(newcrc, address[1]); /* address bits 31 - 16 */
  740. newcrc = crc416(newcrc, address[2]); /* address bits 15 - 0 */
  741. return(newcrc);
  742. }
  743. /*
  744. * Add requested mcast addr to BMac's hash table filter.
  745. *
  746. */
  747. static void
  748. bmac_addhash(struct bmac_data *bp, unsigned char *addr)
  749. {
  750. unsigned int crc;
  751. unsigned short mask;
  752. if (!(*addr)) return;
  753. crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
  754. crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
  755. if (bp->hash_use_count[crc]++) return; /* This bit is already set */
  756. mask = crc % 16;
  757. mask = (unsigned char)1 << mask;
  758. bp->hash_use_count[crc/16] |= mask;
  759. }
  760. static void
  761. bmac_removehash(struct bmac_data *bp, unsigned char *addr)
  762. {
  763. unsigned int crc;
  764. unsigned char mask;
  765. /* Now, delete the address from the filter copy, as indicated */
  766. crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
  767. crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
  768. if (bp->hash_use_count[crc] == 0) return; /* That bit wasn't in use! */
  769. if (--bp->hash_use_count[crc]) return; /* That bit is still in use */
  770. mask = crc % 16;
  771. mask = ((unsigned char)1 << mask) ^ 0xffff; /* To turn off bit */
  772. bp->hash_table_mask[crc/16] &= mask;
  773. }
  774. /*
  775. * Sync the adapter with the software copy of the multicast mask
  776. * (logical address filter).
  777. */
  778. static void
  779. bmac_rx_off(struct net_device *dev)
  780. {
  781. unsigned short rx_cfg;
  782. rx_cfg = bmread(dev, RXCFG);
  783. rx_cfg &= ~RxMACEnable;
  784. bmwrite(dev, RXCFG, rx_cfg);
  785. do {
  786. rx_cfg = bmread(dev, RXCFG);
  787. } while (rx_cfg & RxMACEnable);
  788. }
  789. unsigned short
  790. bmac_rx_on(struct net_device *dev, int hash_enable, int promisc_enable)
  791. {
  792. unsigned short rx_cfg;
  793. rx_cfg = bmread(dev, RXCFG);
  794. rx_cfg |= RxMACEnable;
  795. if (hash_enable) rx_cfg |= RxHashFilterEnable;
  796. else rx_cfg &= ~RxHashFilterEnable;
  797. if (promisc_enable) rx_cfg |= RxPromiscEnable;
  798. else rx_cfg &= ~RxPromiscEnable;
  799. bmwrite(dev, RXRST, RxResetValue);
  800. bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
  801. bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
  802. bmwrite(dev, RXCFG, rx_cfg );
  803. return rx_cfg;
  804. }
  805. static void
  806. bmac_update_hash_table_mask(struct net_device *dev, struct bmac_data *bp)
  807. {
  808. bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
  809. bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
  810. bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
  811. bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
  812. }
  813. #if 0
  814. static void
  815. bmac_add_multi(struct net_device *dev,
  816. struct bmac_data *bp, unsigned char *addr)
  817. {
  818. /* XXDEBUG(("bmac: enter bmac_add_multi\n")); */
  819. bmac_addhash(bp, addr);
  820. bmac_rx_off(dev);
  821. bmac_update_hash_table_mask(dev, bp);
  822. bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
  823. /* XXDEBUG(("bmac: exit bmac_add_multi\n")); */
  824. }
  825. static void
  826. bmac_remove_multi(struct net_device *dev,
  827. struct bmac_data *bp, unsigned char *addr)
  828. {
  829. bmac_removehash(bp, addr);
  830. bmac_rx_off(dev);
  831. bmac_update_hash_table_mask(dev, bp);
  832. bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
  833. }
  834. #endif
  835. /* Set or clear the multicast filter for this adaptor.
  836. num_addrs == -1 Promiscuous mode, receive all packets
  837. num_addrs == 0 Normal mode, clear multicast list
  838. num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  839. best-effort filtering.
  840. */
  841. static void bmac_set_multicast(struct net_device *dev)
  842. {
  843. struct dev_mc_list *dmi;
  844. struct bmac_data *bp = netdev_priv(dev);
  845. int num_addrs = dev->mc_count;
  846. unsigned short rx_cfg;
  847. int i;
  848. if (bp->sleeping)
  849. return;
  850. XXDEBUG(("bmac: enter bmac_set_multicast, n_addrs=%d\n", num_addrs));
  851. if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  852. for (i=0; i<4; i++) bp->hash_table_mask[i] = 0xffff;
  853. bmac_update_hash_table_mask(dev, bp);
  854. rx_cfg = bmac_rx_on(dev, 1, 0);
  855. XXDEBUG(("bmac: all multi, rx_cfg=%#08x\n"));
  856. } else if ((dev->flags & IFF_PROMISC) || (num_addrs < 0)) {
  857. rx_cfg = bmread(dev, RXCFG);
  858. rx_cfg |= RxPromiscEnable;
  859. bmwrite(dev, RXCFG, rx_cfg);
  860. rx_cfg = bmac_rx_on(dev, 0, 1);
  861. XXDEBUG(("bmac: promisc mode enabled, rx_cfg=%#08x\n", rx_cfg));
  862. } else {
  863. for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
  864. for (i=0; i<64; i++) bp->hash_use_count[i] = 0;
  865. if (num_addrs == 0) {
  866. rx_cfg = bmac_rx_on(dev, 0, 0);
  867. XXDEBUG(("bmac: multi disabled, rx_cfg=%#08x\n", rx_cfg));
  868. } else {
  869. for (dmi=dev->mc_list; dmi!=NULL; dmi=dmi->next)
  870. bmac_addhash(bp, dmi->dmi_addr);
  871. bmac_update_hash_table_mask(dev, bp);
  872. rx_cfg = bmac_rx_on(dev, 1, 0);
  873. XXDEBUG(("bmac: multi enabled, rx_cfg=%#08x\n", rx_cfg));
  874. }
  875. }
  876. /* XXDEBUG(("bmac: exit bmac_set_multicast\n")); */
  877. }
  878. #else /* ifdef SUNHME_MULTICAST */
  879. /* The version of set_multicast below was lifted from sunhme.c */
  880. static void bmac_set_multicast(struct net_device *dev)
  881. {
  882. struct dev_mc_list *dmi = dev->mc_list;
  883. char *addrs;
  884. int i;
  885. unsigned short rx_cfg;
  886. u32 crc;
  887. if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  888. bmwrite(dev, BHASH0, 0xffff);
  889. bmwrite(dev, BHASH1, 0xffff);
  890. bmwrite(dev, BHASH2, 0xffff);
  891. bmwrite(dev, BHASH3, 0xffff);
  892. } else if(dev->flags & IFF_PROMISC) {
  893. rx_cfg = bmread(dev, RXCFG);
  894. rx_cfg |= RxPromiscEnable;
  895. bmwrite(dev, RXCFG, rx_cfg);
  896. } else {
  897. u16 hash_table[4];
  898. rx_cfg = bmread(dev, RXCFG);
  899. rx_cfg &= ~RxPromiscEnable;
  900. bmwrite(dev, RXCFG, rx_cfg);
  901. for(i = 0; i < 4; i++) hash_table[i] = 0;
  902. for(i = 0; i < dev->mc_count; i++) {
  903. addrs = dmi->dmi_addr;
  904. dmi = dmi->next;
  905. if(!(*addrs & 1))
  906. continue;
  907. crc = ether_crc_le(6, addrs);
  908. crc >>= 26;
  909. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  910. }
  911. bmwrite(dev, BHASH0, hash_table[0]);
  912. bmwrite(dev, BHASH1, hash_table[1]);
  913. bmwrite(dev, BHASH2, hash_table[2]);
  914. bmwrite(dev, BHASH3, hash_table[3]);
  915. }
  916. }
  917. #endif /* SUNHME_MULTICAST */
  918. static int miscintcount;
  919. static irqreturn_t bmac_misc_intr(int irq, void *dev_id)
  920. {
  921. struct net_device *dev = (struct net_device *) dev_id;
  922. struct bmac_data *bp = netdev_priv(dev);
  923. unsigned int status = bmread(dev, STATUS);
  924. if (miscintcount++ < 10) {
  925. XXDEBUG(("bmac_misc_intr\n"));
  926. }
  927. /* XXDEBUG(("bmac_misc_intr, status=%#08x\n", status)); */
  928. /* bmac_txdma_intr_inner(irq, dev_id); */
  929. /* if (status & FrameReceived) bp->stats.rx_dropped++; */
  930. if (status & RxErrorMask) bp->stats.rx_errors++;
  931. if (status & RxCRCCntExp) bp->stats.rx_crc_errors++;
  932. if (status & RxLenCntExp) bp->stats.rx_length_errors++;
  933. if (status & RxOverFlow) bp->stats.rx_over_errors++;
  934. if (status & RxAlignCntExp) bp->stats.rx_frame_errors++;
  935. /* if (status & FrameSent) bp->stats.tx_dropped++; */
  936. if (status & TxErrorMask) bp->stats.tx_errors++;
  937. if (status & TxUnderrun) bp->stats.tx_fifo_errors++;
  938. if (status & TxNormalCollExp) bp->stats.collisions++;
  939. return IRQ_HANDLED;
  940. }
  941. /*
  942. * Procedure for reading EEPROM
  943. */
  944. #define SROMAddressLength 5
  945. #define DataInOn 0x0008
  946. #define DataInOff 0x0000
  947. #define Clk 0x0002
  948. #define ChipSelect 0x0001
  949. #define SDIShiftCount 3
  950. #define SD0ShiftCount 2
  951. #define DelayValue 1000 /* number of microseconds */
  952. #define SROMStartOffset 10 /* this is in words */
  953. #define SROMReadCount 3 /* number of words to read from SROM */
  954. #define SROMAddressBits 6
  955. #define EnetAddressOffset 20
  956. static unsigned char
  957. bmac_clock_out_bit(struct net_device *dev)
  958. {
  959. unsigned short data;
  960. unsigned short val;
  961. bmwrite(dev, SROMCSR, ChipSelect | Clk);
  962. udelay(DelayValue);
  963. data = bmread(dev, SROMCSR);
  964. udelay(DelayValue);
  965. val = (data >> SD0ShiftCount) & 1;
  966. bmwrite(dev, SROMCSR, ChipSelect);
  967. udelay(DelayValue);
  968. return val;
  969. }
  970. static void
  971. bmac_clock_in_bit(struct net_device *dev, unsigned int val)
  972. {
  973. unsigned short data;
  974. if (val != 0 && val != 1) return;
  975. data = (val << SDIShiftCount);
  976. bmwrite(dev, SROMCSR, data | ChipSelect );
  977. udelay(DelayValue);
  978. bmwrite(dev, SROMCSR, data | ChipSelect | Clk );
  979. udelay(DelayValue);
  980. bmwrite(dev, SROMCSR, data | ChipSelect);
  981. udelay(DelayValue);
  982. }
  983. static void
  984. reset_and_select_srom(struct net_device *dev)
  985. {
  986. /* first reset */
  987. bmwrite(dev, SROMCSR, 0);
  988. udelay(DelayValue);
  989. /* send it the read command (110) */
  990. bmac_clock_in_bit(dev, 1);
  991. bmac_clock_in_bit(dev, 1);
  992. bmac_clock_in_bit(dev, 0);
  993. }
  994. static unsigned short
  995. read_srom(struct net_device *dev, unsigned int addr, unsigned int addr_len)
  996. {
  997. unsigned short data, val;
  998. int i;
  999. /* send out the address we want to read from */
  1000. for (i = 0; i < addr_len; i++) {
  1001. val = addr >> (addr_len-i-1);
  1002. bmac_clock_in_bit(dev, val & 1);
  1003. }
  1004. /* Now read in the 16-bit data */
  1005. data = 0;
  1006. for (i = 0; i < 16; i++) {
  1007. val = bmac_clock_out_bit(dev);
  1008. data <<= 1;
  1009. data |= val;
  1010. }
  1011. bmwrite(dev, SROMCSR, 0);
  1012. return data;
  1013. }
  1014. /*
  1015. * It looks like Cogent and SMC use different methods for calculating
  1016. * checksums. What a pain..
  1017. */
  1018. static int
  1019. bmac_verify_checksum(struct net_device *dev)
  1020. {
  1021. unsigned short data, storedCS;
  1022. reset_and_select_srom(dev);
  1023. data = read_srom(dev, 3, SROMAddressBits);
  1024. storedCS = ((data >> 8) & 0x0ff) | ((data << 8) & 0xff00);
  1025. return 0;
  1026. }
  1027. static void
  1028. bmac_get_station_address(struct net_device *dev, unsigned char *ea)
  1029. {
  1030. int i;
  1031. unsigned short data;
  1032. for (i = 0; i < 6; i++)
  1033. {
  1034. reset_and_select_srom(dev);
  1035. data = read_srom(dev, i + EnetAddressOffset/2, SROMAddressBits);
  1036. ea[2*i] = bitrev(data & 0x0ff);
  1037. ea[2*i+1] = bitrev((data >> 8) & 0x0ff);
  1038. }
  1039. }
  1040. static void bmac_reset_and_enable(struct net_device *dev)
  1041. {
  1042. struct bmac_data *bp = netdev_priv(dev);
  1043. unsigned long flags;
  1044. struct sk_buff *skb;
  1045. unsigned char *data;
  1046. spin_lock_irqsave(&bp->lock, flags);
  1047. bmac_enable_and_reset_chip(dev);
  1048. bmac_init_tx_ring(bp);
  1049. bmac_init_rx_ring(bp);
  1050. bmac_init_chip(dev);
  1051. bmac_start_chip(dev);
  1052. bmwrite(dev, INTDISABLE, EnableNormal);
  1053. bp->sleeping = 0;
  1054. /*
  1055. * It seems that the bmac can't receive until it's transmitted
  1056. * a packet. So we give it a dummy packet to transmit.
  1057. */
  1058. skb = dev_alloc_skb(ETHERMINPACKET);
  1059. if (skb != NULL) {
  1060. data = skb_put(skb, ETHERMINPACKET);
  1061. memset(data, 0, ETHERMINPACKET);
  1062. memcpy(data, dev->dev_addr, 6);
  1063. memcpy(data+6, dev->dev_addr, 6);
  1064. bmac_transmit_packet(skb, dev);
  1065. }
  1066. spin_unlock_irqrestore(&bp->lock, flags);
  1067. }
  1068. static int __devinit bmac_probe(struct macio_dev *mdev, const struct of_device_id *match)
  1069. {
  1070. int j, rev, ret;
  1071. struct bmac_data *bp;
  1072. const unsigned char *prop_addr;
  1073. unsigned char addr[6];
  1074. struct net_device *dev;
  1075. int is_bmac_plus = ((int)match->data) != 0;
  1076. if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
  1077. printk(KERN_ERR "BMAC: can't use, need 3 addrs and 3 intrs\n");
  1078. return -ENODEV;
  1079. }
  1080. prop_addr = get_property(macio_get_of_node(mdev), "mac-address", NULL);
  1081. if (prop_addr == NULL) {
  1082. prop_addr = get_property(macio_get_of_node(mdev),
  1083. "local-mac-address", NULL);
  1084. if (prop_addr == NULL) {
  1085. printk(KERN_ERR "BMAC: Can't get mac-address\n");
  1086. return -ENODEV;
  1087. }
  1088. }
  1089. memcpy(addr, prop_addr, sizeof(addr));
  1090. dev = alloc_etherdev(PRIV_BYTES);
  1091. if (!dev) {
  1092. printk(KERN_ERR "BMAC: alloc_etherdev failed, out of memory\n");
  1093. return -ENOMEM;
  1094. }
  1095. bp = netdev_priv(dev);
  1096. SET_MODULE_OWNER(dev);
  1097. SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
  1098. macio_set_drvdata(mdev, dev);
  1099. bp->mdev = mdev;
  1100. spin_lock_init(&bp->lock);
  1101. if (macio_request_resources(mdev, "bmac")) {
  1102. printk(KERN_ERR "BMAC: can't request IO resource !\n");
  1103. goto out_free;
  1104. }
  1105. dev->base_addr = (unsigned long)
  1106. ioremap(macio_resource_start(mdev, 0), macio_resource_len(mdev, 0));
  1107. if (dev->base_addr == 0)
  1108. goto out_release;
  1109. dev->irq = macio_irq(mdev, 0);
  1110. bmac_enable_and_reset_chip(dev);
  1111. bmwrite(dev, INTDISABLE, DisableAll);
  1112. rev = addr[0] == 0 && addr[1] == 0xA0;
  1113. for (j = 0; j < 6; ++j)
  1114. dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
  1115. /* Enable chip without interrupts for now */
  1116. bmac_enable_and_reset_chip(dev);
  1117. bmwrite(dev, INTDISABLE, DisableAll);
  1118. dev->open = bmac_open;
  1119. dev->stop = bmac_close;
  1120. dev->hard_start_xmit = bmac_output;
  1121. dev->get_stats = bmac_stats;
  1122. dev->set_multicast_list = bmac_set_multicast;
  1123. dev->set_mac_address = bmac_set_address;
  1124. bmac_get_station_address(dev, addr);
  1125. if (bmac_verify_checksum(dev) != 0)
  1126. goto err_out_iounmap;
  1127. bp->is_bmac_plus = is_bmac_plus;
  1128. bp->tx_dma = ioremap(macio_resource_start(mdev, 1), macio_resource_len(mdev, 1));
  1129. if (!bp->tx_dma)
  1130. goto err_out_iounmap;
  1131. bp->tx_dma_intr = macio_irq(mdev, 1);
  1132. bp->rx_dma = ioremap(macio_resource_start(mdev, 2), macio_resource_len(mdev, 2));
  1133. if (!bp->rx_dma)
  1134. goto err_out_iounmap_tx;
  1135. bp->rx_dma_intr = macio_irq(mdev, 2);
  1136. bp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(bp + 1);
  1137. bp->rx_cmds = bp->tx_cmds + N_TX_RING + 1;
  1138. bp->queue = (struct sk_buff_head *)(bp->rx_cmds + N_RX_RING + 1);
  1139. skb_queue_head_init(bp->queue);
  1140. init_timer(&bp->tx_timeout);
  1141. ret = request_irq(dev->irq, bmac_misc_intr, 0, "BMAC-misc", dev);
  1142. if (ret) {
  1143. printk(KERN_ERR "BMAC: can't get irq %d\n", dev->irq);
  1144. goto err_out_iounmap_rx;
  1145. }
  1146. ret = request_irq(bp->tx_dma_intr, bmac_txdma_intr, 0, "BMAC-txdma", dev);
  1147. if (ret) {
  1148. printk(KERN_ERR "BMAC: can't get irq %d\n", bp->tx_dma_intr);
  1149. goto err_out_irq0;
  1150. }
  1151. ret = request_irq(bp->rx_dma_intr, bmac_rxdma_intr, 0, "BMAC-rxdma", dev);
  1152. if (ret) {
  1153. printk(KERN_ERR "BMAC: can't get irq %d\n", bp->rx_dma_intr);
  1154. goto err_out_irq1;
  1155. }
  1156. /* Mask chip interrupts and disable chip, will be
  1157. * re-enabled on open()
  1158. */
  1159. disable_irq(dev->irq);
  1160. pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
  1161. if (register_netdev(dev) != 0) {
  1162. printk(KERN_ERR "BMAC: Ethernet registration failed\n");
  1163. goto err_out_irq2;
  1164. }
  1165. printk(KERN_INFO "%s: BMAC%s at", dev->name, (is_bmac_plus? "+": ""));
  1166. for (j = 0; j < 6; ++j)
  1167. printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
  1168. XXDEBUG((", base_addr=%#0lx", dev->base_addr));
  1169. printk("\n");
  1170. return 0;
  1171. err_out_irq2:
  1172. free_irq(bp->rx_dma_intr, dev);
  1173. err_out_irq1:
  1174. free_irq(bp->tx_dma_intr, dev);
  1175. err_out_irq0:
  1176. free_irq(dev->irq, dev);
  1177. err_out_iounmap_rx:
  1178. iounmap(bp->rx_dma);
  1179. err_out_iounmap_tx:
  1180. iounmap(bp->tx_dma);
  1181. err_out_iounmap:
  1182. iounmap((void __iomem *)dev->base_addr);
  1183. out_release:
  1184. macio_release_resources(mdev);
  1185. out_free:
  1186. pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
  1187. free_netdev(dev);
  1188. return -ENODEV;
  1189. }
  1190. static int bmac_open(struct net_device *dev)
  1191. {
  1192. struct bmac_data *bp = netdev_priv(dev);
  1193. /* XXDEBUG(("bmac: enter open\n")); */
  1194. /* reset the chip */
  1195. bp->opened = 1;
  1196. bmac_reset_and_enable(dev);
  1197. enable_irq(dev->irq);
  1198. return 0;
  1199. }
  1200. static int bmac_close(struct net_device *dev)
  1201. {
  1202. struct bmac_data *bp = netdev_priv(dev);
  1203. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  1204. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  1205. unsigned short config;
  1206. int i;
  1207. bp->sleeping = 1;
  1208. /* disable rx and tx */
  1209. config = bmread(dev, RXCFG);
  1210. bmwrite(dev, RXCFG, (config & ~RxMACEnable));
  1211. config = bmread(dev, TXCFG);
  1212. bmwrite(dev, TXCFG, (config & ~TxMACEnable));
  1213. bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
  1214. /* disable rx and tx dma */
  1215. st_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
  1216. st_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
  1217. /* free some skb's */
  1218. XXDEBUG(("bmac: free rx bufs\n"));
  1219. for (i=0; i<N_RX_RING; i++) {
  1220. if (bp->rx_bufs[i] != NULL) {
  1221. dev_kfree_skb(bp->rx_bufs[i]);
  1222. bp->rx_bufs[i] = NULL;
  1223. }
  1224. }
  1225. XXDEBUG(("bmac: free tx bufs\n"));
  1226. for (i = 0; i<N_TX_RING; i++) {
  1227. if (bp->tx_bufs[i] != NULL) {
  1228. dev_kfree_skb(bp->tx_bufs[i]);
  1229. bp->tx_bufs[i] = NULL;
  1230. }
  1231. }
  1232. XXDEBUG(("bmac: all bufs freed\n"));
  1233. bp->opened = 0;
  1234. disable_irq(dev->irq);
  1235. pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
  1236. return 0;
  1237. }
  1238. static void
  1239. bmac_start(struct net_device *dev)
  1240. {
  1241. struct bmac_data *bp = netdev_priv(dev);
  1242. int i;
  1243. struct sk_buff *skb;
  1244. unsigned long flags;
  1245. if (bp->sleeping)
  1246. return;
  1247. spin_lock_irqsave(&bp->lock, flags);
  1248. while (1) {
  1249. i = bp->tx_fill + 1;
  1250. if (i >= N_TX_RING)
  1251. i = 0;
  1252. if (i == bp->tx_empty)
  1253. break;
  1254. skb = skb_dequeue(bp->queue);
  1255. if (skb == NULL)
  1256. break;
  1257. bmac_transmit_packet(skb, dev);
  1258. }
  1259. spin_unlock_irqrestore(&bp->lock, flags);
  1260. }
  1261. static int
  1262. bmac_output(struct sk_buff *skb, struct net_device *dev)
  1263. {
  1264. struct bmac_data *bp = netdev_priv(dev);
  1265. skb_queue_tail(bp->queue, skb);
  1266. bmac_start(dev);
  1267. return 0;
  1268. }
  1269. static void bmac_tx_timeout(unsigned long data)
  1270. {
  1271. struct net_device *dev = (struct net_device *) data;
  1272. struct bmac_data *bp = netdev_priv(dev);
  1273. volatile struct dbdma_regs __iomem *td = bp->tx_dma;
  1274. volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
  1275. volatile struct dbdma_cmd *cp;
  1276. unsigned long flags;
  1277. unsigned short config, oldConfig;
  1278. int i;
  1279. XXDEBUG(("bmac: tx_timeout called\n"));
  1280. spin_lock_irqsave(&bp->lock, flags);
  1281. bp->timeout_active = 0;
  1282. /* update various counters */
  1283. /* bmac_handle_misc_intrs(bp, 0); */
  1284. cp = &bp->tx_cmds[bp->tx_empty];
  1285. /* XXDEBUG((KERN_DEBUG "bmac: tx dmastat=%x %x runt=%d pr=%x fs=%x fc=%x\n", */
  1286. /* ld_le32(&td->status), ld_le16(&cp->xfer_status), bp->tx_bad_runt, */
  1287. /* mb->pr, mb->xmtfs, mb->fifofc)); */
  1288. /* turn off both tx and rx and reset the chip */
  1289. config = bmread(dev, RXCFG);
  1290. bmwrite(dev, RXCFG, (config & ~RxMACEnable));
  1291. config = bmread(dev, TXCFG);
  1292. bmwrite(dev, TXCFG, (config & ~TxMACEnable));
  1293. out_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
  1294. printk(KERN_ERR "bmac: transmit timeout - resetting\n");
  1295. bmac_enable_and_reset_chip(dev);
  1296. /* restart rx dma */
  1297. cp = bus_to_virt(ld_le32(&rd->cmdptr));
  1298. out_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
  1299. out_le16(&cp->xfer_status, 0);
  1300. out_le32(&rd->cmdptr, virt_to_bus(cp));
  1301. out_le32(&rd->control, DBDMA_SET(RUN|WAKE));
  1302. /* fix up the transmit side */
  1303. XXDEBUG((KERN_DEBUG "bmac: tx empty=%d fill=%d fullup=%d\n",
  1304. bp->tx_empty, bp->tx_fill, bp->tx_fullup));
  1305. i = bp->tx_empty;
  1306. ++bp->stats.tx_errors;
  1307. if (i != bp->tx_fill) {
  1308. dev_kfree_skb(bp->tx_bufs[i]);
  1309. bp->tx_bufs[i] = NULL;
  1310. if (++i >= N_TX_RING) i = 0;
  1311. bp->tx_empty = i;
  1312. }
  1313. bp->tx_fullup = 0;
  1314. netif_wake_queue(dev);
  1315. if (i != bp->tx_fill) {
  1316. cp = &bp->tx_cmds[i];
  1317. out_le16(&cp->xfer_status, 0);
  1318. out_le16(&cp->command, OUTPUT_LAST);
  1319. out_le32(&td->cmdptr, virt_to_bus(cp));
  1320. out_le32(&td->control, DBDMA_SET(RUN));
  1321. /* bmac_set_timeout(dev); */
  1322. XXDEBUG((KERN_DEBUG "bmac: starting %d\n", i));
  1323. }
  1324. /* turn it back on */
  1325. oldConfig = bmread(dev, RXCFG);
  1326. bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
  1327. oldConfig = bmread(dev, TXCFG);
  1328. bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
  1329. spin_unlock_irqrestore(&bp->lock, flags);
  1330. }
  1331. #if 0
  1332. static void dump_dbdma(volatile struct dbdma_cmd *cp,int count)
  1333. {
  1334. int i,*ip;
  1335. for (i=0;i< count;i++) {
  1336. ip = (int*)(cp+i);
  1337. printk("dbdma req 0x%x addr 0x%x baddr 0x%x xfer/res 0x%x\n",
  1338. ld_le32(ip+0),
  1339. ld_le32(ip+1),
  1340. ld_le32(ip+2),
  1341. ld_le32(ip+3));
  1342. }
  1343. }
  1344. #endif
  1345. #if 0
  1346. static int
  1347. bmac_proc_info(char *buffer, char **start, off_t offset, int length)
  1348. {
  1349. int len = 0;
  1350. off_t pos = 0;
  1351. off_t begin = 0;
  1352. int i;
  1353. if (bmac_devs == NULL)
  1354. return (-ENOSYS);
  1355. len += sprintf(buffer, "BMAC counters & registers\n");
  1356. for (i = 0; i<N_REG_ENTRIES; i++) {
  1357. len += sprintf(buffer + len, "%s: %#08x\n",
  1358. reg_entries[i].name,
  1359. bmread(bmac_devs, reg_entries[i].reg_offset));
  1360. pos = begin + len;
  1361. if (pos < offset) {
  1362. len = 0;
  1363. begin = pos;
  1364. }
  1365. if (pos > offset+length) break;
  1366. }
  1367. *start = buffer + (offset - begin);
  1368. len -= (offset - begin);
  1369. if (len > length) len = length;
  1370. return len;
  1371. }
  1372. #endif
  1373. static int __devexit bmac_remove(struct macio_dev *mdev)
  1374. {
  1375. struct net_device *dev = macio_get_drvdata(mdev);
  1376. struct bmac_data *bp = netdev_priv(dev);
  1377. unregister_netdev(dev);
  1378. free_irq(dev->irq, dev);
  1379. free_irq(bp->tx_dma_intr, dev);
  1380. free_irq(bp->rx_dma_intr, dev);
  1381. iounmap((void __iomem *)dev->base_addr);
  1382. iounmap(bp->tx_dma);
  1383. iounmap(bp->rx_dma);
  1384. macio_release_resources(mdev);
  1385. free_netdev(dev);
  1386. return 0;
  1387. }
  1388. static struct of_device_id bmac_match[] =
  1389. {
  1390. {
  1391. .name = "bmac",
  1392. .data = (void *)0,
  1393. },
  1394. {
  1395. .type = "network",
  1396. .compatible = "bmac+",
  1397. .data = (void *)1,
  1398. },
  1399. {},
  1400. };
  1401. MODULE_DEVICE_TABLE (of, bmac_match);
  1402. static struct macio_driver bmac_driver =
  1403. {
  1404. .name = "bmac",
  1405. .match_table = bmac_match,
  1406. .probe = bmac_probe,
  1407. .remove = bmac_remove,
  1408. #ifdef CONFIG_PM
  1409. .suspend = bmac_suspend,
  1410. .resume = bmac_resume,
  1411. #endif
  1412. };
  1413. static int __init bmac_init(void)
  1414. {
  1415. if (bmac_emergency_rxbuf == NULL) {
  1416. bmac_emergency_rxbuf = kmalloc(RX_BUFLEN, GFP_KERNEL);
  1417. if (bmac_emergency_rxbuf == NULL) {
  1418. printk(KERN_ERR "BMAC: can't allocate emergency RX buffer\n");
  1419. return -ENOMEM;
  1420. }
  1421. }
  1422. return macio_register_driver(&bmac_driver);
  1423. }
  1424. static void __exit bmac_exit(void)
  1425. {
  1426. macio_unregister_driver(&bmac_driver);
  1427. kfree(bmac_emergency_rxbuf);
  1428. bmac_emergency_rxbuf = NULL;
  1429. }
  1430. MODULE_AUTHOR("Randy Gobbel/Paul Mackerras");
  1431. MODULE_DESCRIPTION("PowerMac BMAC ethernet driver.");
  1432. MODULE_LICENSE("GPL");
  1433. module_init(bmac_init);
  1434. module_exit(bmac_exit);