bmac.c 41 KB

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