bmac.c 42 KB

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