bmac.c 41 KB

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