bmac.c 41 KB

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