bmac.c 41 KB

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