bmac.c 41 KB

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