bmac.c 41 KB

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