bmac.c 41 KB

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