cs89x0.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /* cs89x0.c: A Crystal Semiconductor (Now Cirrus Logic) CS89[02]0
  2. * driver for linux.
  3. * Written 1996 by Russell Nelson, with reference to skeleton.c
  4. * written 1993-1994 by Donald Becker.
  5. *
  6. * This software may be used and distributed according to the terms
  7. * of the GNU General Public License, incorporated herein by reference.
  8. *
  9. * The author may be reached at nelson@crynwr.com, Crynwr
  10. * Software, 521 Pleasant Valley Rd., Potsdam, NY 13676
  11. *
  12. * Other contributors:
  13. * Mike Cruse : mcruse@cti-ltd.com
  14. * Russ Nelson
  15. * Melody Lee : ethernet@crystal.cirrus.com
  16. * Alan Cox
  17. * Andrew Morton
  18. * Oskar Schirmer : oskar@scara.com
  19. * Deepak Saxena : dsaxena@plexity.net
  20. * Dmitry Pervushin : dpervushin@ru.mvista.com
  21. * Deepak Saxena : dsaxena@plexity.net
  22. * Domenico Andreoli : cavokz@gmail.com
  23. */
  24. /*
  25. * Set this to zero to disable DMA code
  26. *
  27. * Note that even if DMA is turned off we still support the 'dma' and 'use_dma'
  28. * module options so we don't break any startup scripts.
  29. */
  30. #ifndef CONFIG_ISA_DMA_API
  31. #define ALLOW_DMA 0
  32. #else
  33. #define ALLOW_DMA 1
  34. #endif
  35. /*
  36. * Set this to zero to remove all the debug statements via
  37. * dead code elimination
  38. */
  39. #define DEBUGGING 1
  40. /* Sources:
  41. * Crynwr packet driver epktisa.
  42. * Crystal Semiconductor data sheets.
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/module.h>
  46. #include <linux/printk.h>
  47. #include <linux/errno.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/etherdevice.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/kernel.h>
  52. #include <linux/types.h>
  53. #include <linux/fcntl.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/ioport.h>
  56. #include <linux/in.h>
  57. #include <linux/skbuff.h>
  58. #include <linux/spinlock.h>
  59. #include <linux/string.h>
  60. #include <linux/init.h>
  61. #include <linux/bitops.h>
  62. #include <linux/delay.h>
  63. #include <linux/gfp.h>
  64. #include <linux/io.h>
  65. #include <asm/irq.h>
  66. #include <linux/atomic.h>
  67. #if ALLOW_DMA
  68. #include <asm/dma.h>
  69. #endif
  70. #include "cs89x0.h"
  71. #define cs89_dbg(val, level, fmt, ...) \
  72. do { \
  73. if (val <= net_debug) \
  74. pr_##level(fmt, ##__VA_ARGS__); \
  75. } while (0)
  76. static char version[] __initdata =
  77. "v2.4.3-pre1 Russell Nelson <nelson@crynwr.com>, Andrew Morton";
  78. #define DRV_NAME "cs89x0"
  79. /* First, a few definitions that the brave might change.
  80. * A zero-terminated list of I/O addresses to be probed. Some special flags..
  81. * Addr & 1 = Read back the address port, look for signature and reset
  82. * the page window before probing
  83. * Addr & 3 = Reset the page window and probe
  84. * The CLPS eval board has the Cirrus chip at 0x80090300, in ARM IO space,
  85. * but it is possible that a Cirrus board could be plugged into the ISA
  86. * slots.
  87. */
  88. /* The cs8900 has 4 IRQ pins, software selectable. cs8900_irq_map maps
  89. * them to system IRQ numbers. This mapping is card specific and is set to
  90. * the configuration of the Cirrus Eval board for this chip.
  91. */
  92. #ifndef CONFIG_CS89x0_PLATFORM
  93. static unsigned int netcard_portlist[] __used __initdata = {
  94. 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240,
  95. 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0
  96. };
  97. static unsigned int cs8900_irq_map[] = {
  98. 10, 11, 12, 5
  99. };
  100. #endif
  101. #if DEBUGGING
  102. static unsigned int net_debug = DEBUGGING;
  103. #else
  104. #define net_debug 0 /* gcc will remove all the debug code for us */
  105. #endif
  106. /* The number of low I/O ports used by the ethercard. */
  107. #define NETCARD_IO_EXTENT 16
  108. /* we allow the user to override various values normally set in the EEPROM */
  109. #define FORCE_RJ45 0x0001 /* pick one of these three */
  110. #define FORCE_AUI 0x0002
  111. #define FORCE_BNC 0x0004
  112. #define FORCE_AUTO 0x0010 /* pick one of these three */
  113. #define FORCE_HALF 0x0020
  114. #define FORCE_FULL 0x0030
  115. /* Information that need to be kept for each board. */
  116. struct net_local {
  117. int chip_type; /* one of: CS8900, CS8920, CS8920M */
  118. char chip_revision; /* revision letter of the chip ('A'...) */
  119. int send_cmd; /* the proper send command: TX_NOW, TX_AFTER_381, or TX_AFTER_ALL */
  120. int auto_neg_cnf; /* auto-negotiation word from EEPROM */
  121. int adapter_cnf; /* adapter configuration from EEPROM */
  122. int isa_config; /* ISA configuration from EEPROM */
  123. int irq_map; /* IRQ map from EEPROM */
  124. int rx_mode; /* what mode are we in? 0, RX_MULTCAST_ACCEPT, or RX_ALL_ACCEPT */
  125. int curr_rx_cfg; /* a copy of PP_RxCFG */
  126. int linectl; /* either 0 or LOW_RX_SQUELCH, depending on configuration. */
  127. int send_underrun; /* keep track of how many underruns in a row we get */
  128. int force; /* force various values; see FORCE* above. */
  129. spinlock_t lock;
  130. void __iomem *virt_addr;/* CS89x0 virtual address. */
  131. #if ALLOW_DMA
  132. int use_dma; /* Flag: we're using dma */
  133. int dma; /* DMA channel */
  134. int dmasize; /* 16 or 64 */
  135. unsigned char *dma_buff; /* points to the beginning of the buffer */
  136. unsigned char *end_dma_buff; /* points to the end of the buffer */
  137. unsigned char *rx_dma_ptr; /* points to the next packet */
  138. #endif
  139. };
  140. /* Example routines you must write ;->. */
  141. #define tx_done(dev) 1
  142. /*
  143. * Permit 'cs89x0_dma=N' in the kernel boot environment
  144. */
  145. #if !defined(MODULE)
  146. #if ALLOW_DMA
  147. static int g_cs89x0_dma;
  148. static int __init dma_fn(char *str)
  149. {
  150. g_cs89x0_dma = simple_strtol(str, NULL, 0);
  151. return 1;
  152. }
  153. __setup("cs89x0_dma=", dma_fn);
  154. #endif /* ALLOW_DMA */
  155. static int g_cs89x0_media__force;
  156. static int __init media_fn(char *str)
  157. {
  158. if (!strcmp(str, "rj45"))
  159. g_cs89x0_media__force = FORCE_RJ45;
  160. else if (!strcmp(str, "aui"))
  161. g_cs89x0_media__force = FORCE_AUI;
  162. else if (!strcmp(str, "bnc"))
  163. g_cs89x0_media__force = FORCE_BNC;
  164. return 1;
  165. }
  166. __setup("cs89x0_media=", media_fn);
  167. #endif
  168. static void readwords(struct net_local *lp, int portno, void *buf, int length)
  169. {
  170. u8 *buf8 = (u8 *)buf;
  171. do {
  172. u16 tmp16;
  173. tmp16 = ioread16(lp->virt_addr + portno);
  174. *buf8++ = (u8)tmp16;
  175. *buf8++ = (u8)(tmp16 >> 8);
  176. } while (--length);
  177. }
  178. static void writewords(struct net_local *lp, int portno, void *buf, int length)
  179. {
  180. u8 *buf8 = (u8 *)buf;
  181. do {
  182. u16 tmp16;
  183. tmp16 = *buf8++;
  184. tmp16 |= (*buf8++) << 8;
  185. iowrite16(tmp16, lp->virt_addr + portno);
  186. } while (--length);
  187. }
  188. static u16
  189. readreg(struct net_device *dev, u16 regno)
  190. {
  191. struct net_local *lp = netdev_priv(dev);
  192. iowrite16(regno, lp->virt_addr + ADD_PORT);
  193. return ioread16(lp->virt_addr + DATA_PORT);
  194. }
  195. static void
  196. writereg(struct net_device *dev, u16 regno, u16 value)
  197. {
  198. struct net_local *lp = netdev_priv(dev);
  199. iowrite16(regno, lp->virt_addr + ADD_PORT);
  200. iowrite16(value, lp->virt_addr + DATA_PORT);
  201. }
  202. static int __init
  203. wait_eeprom_ready(struct net_device *dev)
  204. {
  205. int timeout = jiffies;
  206. /* check to see if the EEPROM is ready,
  207. * a timeout is used just in case EEPROM is ready when
  208. * SI_BUSY in the PP_SelfST is clear
  209. */
  210. while (readreg(dev, PP_SelfST) & SI_BUSY)
  211. if (jiffies - timeout >= 40)
  212. return -1;
  213. return 0;
  214. }
  215. static int __init
  216. get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
  217. {
  218. int i;
  219. cs89_dbg(3, info, "EEPROM data from %x for %x:", off, len);
  220. for (i = 0; i < len; i++) {
  221. if (wait_eeprom_ready(dev) < 0)
  222. return -1;
  223. /* Now send the EEPROM read command and EEPROM location to read */
  224. writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
  225. if (wait_eeprom_ready(dev) < 0)
  226. return -1;
  227. buffer[i] = readreg(dev, PP_EEData);
  228. cs89_dbg(3, cont, " %04x", buffer[i]);
  229. }
  230. cs89_dbg(3, cont, "\n");
  231. return 0;
  232. }
  233. static int __init
  234. get_eeprom_cksum(int off, int len, int *buffer)
  235. {
  236. int i, cksum;
  237. cksum = 0;
  238. for (i = 0; i < len; i++)
  239. cksum += buffer[i];
  240. cksum &= 0xffff;
  241. if (cksum == 0)
  242. return 0;
  243. return -1;
  244. }
  245. static void
  246. write_irq(struct net_device *dev, int chip_type, int irq)
  247. {
  248. int i;
  249. if (chip_type == CS8900) {
  250. #ifndef CONFIG_CS89x0_PLATFORM
  251. /* Search the mapping table for the corresponding IRQ pin. */
  252. for (i = 0; i != ARRAY_SIZE(cs8900_irq_map); i++)
  253. if (cs8900_irq_map[i] == irq)
  254. break;
  255. /* Not found */
  256. if (i == ARRAY_SIZE(cs8900_irq_map))
  257. i = 3;
  258. #else
  259. /* INTRQ0 pin is used for interrupt generation. */
  260. i = 0;
  261. #endif
  262. writereg(dev, PP_CS8900_ISAINT, i);
  263. } else {
  264. writereg(dev, PP_CS8920_ISAINT, irq);
  265. }
  266. }
  267. static void
  268. count_rx_errors(int status, struct net_device *dev)
  269. {
  270. dev->stats.rx_errors++;
  271. if (status & RX_RUNT)
  272. dev->stats.rx_length_errors++;
  273. if (status & RX_EXTRA_DATA)
  274. dev->stats.rx_length_errors++;
  275. if ((status & RX_CRC_ERROR) && !(status & (RX_EXTRA_DATA | RX_RUNT)))
  276. /* per str 172 */
  277. dev->stats.rx_crc_errors++;
  278. if (status & RX_DRIBBLE)
  279. dev->stats.rx_frame_errors++;
  280. }
  281. /*********************************
  282. * This page contains DMA routines
  283. *********************************/
  284. #if ALLOW_DMA
  285. #define dma_page_eq(ptr1, ptr2) ((long)(ptr1) >> 17 == (long)(ptr2) >> 17)
  286. static void
  287. get_dma_channel(struct net_device *dev)
  288. {
  289. struct net_local *lp = netdev_priv(dev);
  290. if (lp->dma) {
  291. dev->dma = lp->dma;
  292. lp->isa_config |= ISA_RxDMA;
  293. } else {
  294. if ((lp->isa_config & ANY_ISA_DMA) == 0)
  295. return;
  296. dev->dma = lp->isa_config & DMA_NO_MASK;
  297. if (lp->chip_type == CS8900)
  298. dev->dma += 5;
  299. if (dev->dma < 5 || dev->dma > 7) {
  300. lp->isa_config &= ~ANY_ISA_DMA;
  301. return;
  302. }
  303. }
  304. }
  305. static void
  306. write_dma(struct net_device *dev, int chip_type, int dma)
  307. {
  308. struct net_local *lp = netdev_priv(dev);
  309. if ((lp->isa_config & ANY_ISA_DMA) == 0)
  310. return;
  311. if (chip_type == CS8900)
  312. writereg(dev, PP_CS8900_ISADMA, dma - 5);
  313. else
  314. writereg(dev, PP_CS8920_ISADMA, dma);
  315. }
  316. static void
  317. set_dma_cfg(struct net_device *dev)
  318. {
  319. struct net_local *lp = netdev_priv(dev);
  320. if (lp->use_dma) {
  321. if ((lp->isa_config & ANY_ISA_DMA) == 0) {
  322. cs89_dbg(3, err, "set_dma_cfg(): no DMA\n");
  323. return;
  324. }
  325. if (lp->isa_config & ISA_RxDMA) {
  326. lp->curr_rx_cfg |= RX_DMA_ONLY;
  327. cs89_dbg(3, info, "set_dma_cfg(): RX_DMA_ONLY\n");
  328. } else {
  329. lp->curr_rx_cfg |= AUTO_RX_DMA; /* not that we support it... */
  330. cs89_dbg(3, info, "set_dma_cfg(): AUTO_RX_DMA\n");
  331. }
  332. }
  333. }
  334. static int
  335. dma_bufcfg(struct net_device *dev)
  336. {
  337. struct net_local *lp = netdev_priv(dev);
  338. if (lp->use_dma)
  339. return (lp->isa_config & ANY_ISA_DMA) ? RX_DMA_ENBL : 0;
  340. else
  341. return 0;
  342. }
  343. static int
  344. dma_busctl(struct net_device *dev)
  345. {
  346. int retval = 0;
  347. struct net_local *lp = netdev_priv(dev);
  348. if (lp->use_dma) {
  349. if (lp->isa_config & ANY_ISA_DMA)
  350. retval |= RESET_RX_DMA; /* Reset the DMA pointer */
  351. if (lp->isa_config & DMA_BURST)
  352. retval |= DMA_BURST_MODE; /* Does ISA config specify DMA burst ? */
  353. if (lp->dmasize == 64)
  354. retval |= RX_DMA_SIZE_64K; /* did they ask for 64K? */
  355. retval |= MEMORY_ON; /* we need memory enabled to use DMA. */
  356. }
  357. return retval;
  358. }
  359. static void
  360. dma_rx(struct net_device *dev)
  361. {
  362. struct net_local *lp = netdev_priv(dev);
  363. struct sk_buff *skb;
  364. int status, length;
  365. unsigned char *bp = lp->rx_dma_ptr;
  366. status = bp[0] + (bp[1] << 8);
  367. length = bp[2] + (bp[3] << 8);
  368. bp += 4;
  369. cs89_dbg(5, debug, "%s: receiving DMA packet at %lx, status %x, length %x\n",
  370. dev->name, (unsigned long)bp, status, length);
  371. if ((status & RX_OK) == 0) {
  372. count_rx_errors(status, dev);
  373. goto skip_this_frame;
  374. }
  375. /* Malloc up new buffer. */
  376. skb = netdev_alloc_skb(dev, length + 2);
  377. if (skb == NULL) {
  378. dev->stats.rx_dropped++;
  379. /* AKPM: advance bp to the next frame */
  380. skip_this_frame:
  381. bp += (length + 3) & ~3;
  382. if (bp >= lp->end_dma_buff)
  383. bp -= lp->dmasize * 1024;
  384. lp->rx_dma_ptr = bp;
  385. return;
  386. }
  387. skb_reserve(skb, 2); /* longword align L3 header */
  388. if (bp + length > lp->end_dma_buff) {
  389. int semi_cnt = lp->end_dma_buff - bp;
  390. memcpy(skb_put(skb, semi_cnt), bp, semi_cnt);
  391. memcpy(skb_put(skb, length - semi_cnt), lp->dma_buff,
  392. length - semi_cnt);
  393. } else {
  394. memcpy(skb_put(skb, length), bp, length);
  395. }
  396. bp += (length + 3) & ~3;
  397. if (bp >= lp->end_dma_buff)
  398. bp -= lp->dmasize*1024;
  399. lp->rx_dma_ptr = bp;
  400. cs89_dbg(3, info, "%s: received %d byte DMA packet of type %x\n",
  401. dev->name, length,
  402. ((skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  403. skb->data[ETH_ALEN + ETH_ALEN + 1]));
  404. skb->protocol = eth_type_trans(skb, dev);
  405. netif_rx(skb);
  406. dev->stats.rx_packets++;
  407. dev->stats.rx_bytes += length;
  408. }
  409. static void release_dma_buff(struct net_local *lp)
  410. {
  411. if (lp->dma_buff) {
  412. free_pages((unsigned long)(lp->dma_buff),
  413. get_order(lp->dmasize * 1024));
  414. lp->dma_buff = NULL;
  415. }
  416. }
  417. #endif /* ALLOW_DMA */
  418. static void
  419. control_dc_dc(struct net_device *dev, int on_not_off)
  420. {
  421. struct net_local *lp = netdev_priv(dev);
  422. unsigned int selfcontrol;
  423. int timenow = jiffies;
  424. /* control the DC to DC convertor in the SelfControl register.
  425. * Note: This is hooked up to a general purpose pin, might not
  426. * always be a DC to DC convertor.
  427. */
  428. selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
  429. if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
  430. selfcontrol |= HCB1;
  431. else
  432. selfcontrol &= ~HCB1;
  433. writereg(dev, PP_SelfCTL, selfcontrol);
  434. /* Wait for the DC/DC converter to power up - 500ms */
  435. while (jiffies - timenow < HZ)
  436. ;
  437. }
  438. /* send a test packet - return true if carrier bits are ok */
  439. static int
  440. send_test_pkt(struct net_device *dev)
  441. {
  442. struct net_local *lp = netdev_priv(dev);
  443. char test_packet[] = {
  444. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  445. 0, 46, /* A 46 in network order */
  446. 0, 0, /* DSAP=0 & SSAP=0 fields */
  447. 0xf3, 0 /* Control (Test Req + P bit set) */
  448. };
  449. long timenow = jiffies;
  450. writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
  451. memcpy(test_packet, dev->dev_addr, ETH_ALEN);
  452. memcpy(test_packet + ETH_ALEN, dev->dev_addr, ETH_ALEN);
  453. iowrite16(TX_AFTER_ALL, lp->virt_addr + TX_CMD_PORT);
  454. iowrite16(ETH_ZLEN, lp->virt_addr + TX_LEN_PORT);
  455. /* Test to see if the chip has allocated memory for the packet */
  456. while (jiffies - timenow < 5)
  457. if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
  458. break;
  459. if (jiffies - timenow >= 5)
  460. return 0; /* this shouldn't happen */
  461. /* Write the contents of the packet */
  462. writewords(lp, TX_FRAME_PORT, test_packet, (ETH_ZLEN + 1) >> 1);
  463. cs89_dbg(1, debug, "Sending test packet ");
  464. /* wait a couple of jiffies for packet to be received */
  465. for (timenow = jiffies; jiffies - timenow < 3;)
  466. ;
  467. if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
  468. cs89_dbg(1, cont, "succeeded\n");
  469. return 1;
  470. }
  471. cs89_dbg(1, cont, "failed\n");
  472. return 0;
  473. }
  474. #define DETECTED_NONE 0
  475. #define DETECTED_RJ45H 1
  476. #define DETECTED_RJ45F 2
  477. #define DETECTED_AUI 3
  478. #define DETECTED_BNC 4
  479. static int
  480. detect_tp(struct net_device *dev)
  481. {
  482. struct net_local *lp = netdev_priv(dev);
  483. int timenow = jiffies;
  484. int fdx;
  485. cs89_dbg(1, debug, "%s: Attempting TP\n", dev->name);
  486. /* If connected to another full duplex capable 10-Base-T card
  487. * the link pulses seem to be lost when the auto detect bit in
  488. * the LineCTL is set. To overcome this the auto detect bit will
  489. * be cleared whilst testing the 10-Base-T interface. This would
  490. * not be necessary for the sparrow chip but is simpler to do it
  491. * anyway.
  492. */
  493. writereg(dev, PP_LineCTL, lp->linectl & ~AUI_ONLY);
  494. control_dc_dc(dev, 0);
  495. /* Delay for the hardware to work out if the TP cable is present
  496. * - 150ms
  497. */
  498. for (timenow = jiffies; jiffies - timenow < 15;)
  499. ;
  500. if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
  501. return DETECTED_NONE;
  502. if (lp->chip_type == CS8900) {
  503. switch (lp->force & 0xf0) {
  504. #if 0
  505. case FORCE_AUTO:
  506. pr_info("%s: cs8900 doesn't autonegotiate\n",
  507. dev->name);
  508. return DETECTED_NONE;
  509. #endif
  510. /* CS8900 doesn't support AUTO, change to HALF*/
  511. case FORCE_AUTO:
  512. lp->force &= ~FORCE_AUTO;
  513. lp->force |= FORCE_HALF;
  514. break;
  515. case FORCE_HALF:
  516. break;
  517. case FORCE_FULL:
  518. writereg(dev, PP_TestCTL,
  519. readreg(dev, PP_TestCTL) | FDX_8900);
  520. break;
  521. }
  522. fdx = readreg(dev, PP_TestCTL) & FDX_8900;
  523. } else {
  524. switch (lp->force & 0xf0) {
  525. case FORCE_AUTO:
  526. lp->auto_neg_cnf = AUTO_NEG_ENABLE;
  527. break;
  528. case FORCE_HALF:
  529. lp->auto_neg_cnf = 0;
  530. break;
  531. case FORCE_FULL:
  532. lp->auto_neg_cnf = RE_NEG_NOW | ALLOW_FDX;
  533. break;
  534. }
  535. writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
  536. if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
  537. pr_info("%s: negotiating duplex...\n", dev->name);
  538. while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
  539. if (jiffies - timenow > 4000) {
  540. pr_err("**** Full / half duplex auto-negotiation timed out ****\n");
  541. break;
  542. }
  543. }
  544. }
  545. fdx = readreg(dev, PP_AutoNegST) & FDX_ACTIVE;
  546. }
  547. if (fdx)
  548. return DETECTED_RJ45F;
  549. else
  550. return DETECTED_RJ45H;
  551. }
  552. static int
  553. detect_bnc(struct net_device *dev)
  554. {
  555. struct net_local *lp = netdev_priv(dev);
  556. cs89_dbg(1, debug, "%s: Attempting BNC\n", dev->name);
  557. control_dc_dc(dev, 1);
  558. writereg(dev, PP_LineCTL, (lp->linectl & ~AUTO_AUI_10BASET) | AUI_ONLY);
  559. if (send_test_pkt(dev))
  560. return DETECTED_BNC;
  561. else
  562. return DETECTED_NONE;
  563. }
  564. static int
  565. detect_aui(struct net_device *dev)
  566. {
  567. struct net_local *lp = netdev_priv(dev);
  568. cs89_dbg(1, debug, "%s: Attempting AUI\n", dev->name);
  569. control_dc_dc(dev, 0);
  570. writereg(dev, PP_LineCTL, (lp->linectl & ~AUTO_AUI_10BASET) | AUI_ONLY);
  571. if (send_test_pkt(dev))
  572. return DETECTED_AUI;
  573. else
  574. return DETECTED_NONE;
  575. }
  576. /* We have a good packet(s), get it/them out of the buffers. */
  577. static void
  578. net_rx(struct net_device *dev)
  579. {
  580. struct net_local *lp = netdev_priv(dev);
  581. struct sk_buff *skb;
  582. int status, length;
  583. status = ioread16(lp->virt_addr + RX_FRAME_PORT);
  584. length = ioread16(lp->virt_addr + RX_FRAME_PORT);
  585. if ((status & RX_OK) == 0) {
  586. count_rx_errors(status, dev);
  587. return;
  588. }
  589. /* Malloc up new buffer. */
  590. skb = netdev_alloc_skb(dev, length + 2);
  591. if (skb == NULL) {
  592. dev->stats.rx_dropped++;
  593. return;
  594. }
  595. skb_reserve(skb, 2); /* longword align L3 header */
  596. readwords(lp, RX_FRAME_PORT, skb_put(skb, length), length >> 1);
  597. if (length & 1)
  598. skb->data[length-1] = ioread16(lp->virt_addr + RX_FRAME_PORT);
  599. cs89_dbg(3, debug, "%s: received %d byte packet of type %x\n",
  600. dev->name, length,
  601. (skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  602. skb->data[ETH_ALEN + ETH_ALEN + 1]);
  603. skb->protocol = eth_type_trans(skb, dev);
  604. netif_rx(skb);
  605. dev->stats.rx_packets++;
  606. dev->stats.rx_bytes += length;
  607. }
  608. /* The typical workload of the driver:
  609. * Handle the network interface interrupts.
  610. */
  611. static irqreturn_t net_interrupt(int irq, void *dev_id)
  612. {
  613. struct net_device *dev = dev_id;
  614. struct net_local *lp;
  615. int status;
  616. int handled = 0;
  617. lp = netdev_priv(dev);
  618. /* we MUST read all the events out of the ISQ, otherwise we'll never
  619. * get interrupted again. As a consequence, we can't have any limit
  620. * on the number of times we loop in the interrupt handler. The
  621. * hardware guarantees that eventually we'll run out of events. Of
  622. * course, if you're on a slow machine, and packets are arriving
  623. * faster than you can read them off, you're screwed. Hasta la
  624. * vista, baby!
  625. */
  626. while ((status = ioread16(lp->virt_addr + ISQ_PORT))) {
  627. cs89_dbg(4, debug, "%s: event=%04x\n", dev->name, status);
  628. handled = 1;
  629. switch (status & ISQ_EVENT_MASK) {
  630. case ISQ_RECEIVER_EVENT:
  631. /* Got a packet(s). */
  632. net_rx(dev);
  633. break;
  634. case ISQ_TRANSMITTER_EVENT:
  635. dev->stats.tx_packets++;
  636. netif_wake_queue(dev); /* Inform upper layers. */
  637. if ((status & (TX_OK |
  638. TX_LOST_CRS |
  639. TX_SQE_ERROR |
  640. TX_LATE_COL |
  641. TX_16_COL)) != TX_OK) {
  642. if ((status & TX_OK) == 0)
  643. dev->stats.tx_errors++;
  644. if (status & TX_LOST_CRS)
  645. dev->stats.tx_carrier_errors++;
  646. if (status & TX_SQE_ERROR)
  647. dev->stats.tx_heartbeat_errors++;
  648. if (status & TX_LATE_COL)
  649. dev->stats.tx_window_errors++;
  650. if (status & TX_16_COL)
  651. dev->stats.tx_aborted_errors++;
  652. }
  653. break;
  654. case ISQ_BUFFER_EVENT:
  655. if (status & READY_FOR_TX) {
  656. /* we tried to transmit a packet earlier,
  657. * but inexplicably ran out of buffers.
  658. * That shouldn't happen since we only ever
  659. * load one packet. Shrug. Do the right
  660. * thing anyway.
  661. */
  662. netif_wake_queue(dev); /* Inform upper layers. */
  663. }
  664. if (status & TX_UNDERRUN) {
  665. cs89_dbg(0, err, "%s: transmit underrun\n",
  666. dev->name);
  667. lp->send_underrun++;
  668. if (lp->send_underrun == 3)
  669. lp->send_cmd = TX_AFTER_381;
  670. else if (lp->send_underrun == 6)
  671. lp->send_cmd = TX_AFTER_ALL;
  672. /* transmit cycle is done, although
  673. * frame wasn't transmitted - this
  674. * avoids having to wait for the upper
  675. * layers to timeout on us, in the
  676. * event of a tx underrun
  677. */
  678. netif_wake_queue(dev); /* Inform upper layers. */
  679. }
  680. #if ALLOW_DMA
  681. if (lp->use_dma && (status & RX_DMA)) {
  682. int count = readreg(dev, PP_DmaFrameCnt);
  683. while (count) {
  684. cs89_dbg(5, debug,
  685. "%s: receiving %d DMA frames\n",
  686. dev->name, count);
  687. if (count > 1)
  688. cs89_dbg(2, debug,
  689. "%s: receiving %d DMA frames\n",
  690. dev->name, count);
  691. dma_rx(dev);
  692. if (--count == 0)
  693. count = readreg(dev, PP_DmaFrameCnt);
  694. if (count > 0)
  695. cs89_dbg(2, debug,
  696. "%s: continuing with %d DMA frames\n",
  697. dev->name, count);
  698. }
  699. }
  700. #endif
  701. break;
  702. case ISQ_RX_MISS_EVENT:
  703. dev->stats.rx_missed_errors += (status >> 6);
  704. break;
  705. case ISQ_TX_COL_EVENT:
  706. dev->stats.collisions += (status >> 6);
  707. break;
  708. }
  709. }
  710. return IRQ_RETVAL(handled);
  711. }
  712. /* Open/initialize the board. This is called (in the current kernel)
  713. sometime after booting when the 'ifconfig' program is run.
  714. This routine should set everything up anew at each open, even
  715. registers that "should" only need to be set once at boot, so that
  716. there is non-reboot way to recover if something goes wrong.
  717. */
  718. /* AKPM: do we need to do any locking here? */
  719. static int
  720. net_open(struct net_device *dev)
  721. {
  722. struct net_local *lp = netdev_priv(dev);
  723. int result = 0;
  724. int i;
  725. int ret;
  726. if (dev->irq < 2) {
  727. /* Allow interrupts to be generated by the chip */
  728. /* Cirrus' release had this: */
  729. #if 0
  730. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
  731. #endif
  732. /* And 2.3.47 had this: */
  733. writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
  734. for (i = 2; i < CS8920_NO_INTS; i++) {
  735. if ((1 << i) & lp->irq_map) {
  736. if (request_irq(i, net_interrupt, 0, dev->name,
  737. dev) == 0) {
  738. dev->irq = i;
  739. write_irq(dev, lp->chip_type, i);
  740. /* writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT); */
  741. break;
  742. }
  743. }
  744. }
  745. if (i >= CS8920_NO_INTS) {
  746. writereg(dev, PP_BusCTL, 0); /* disable interrupts. */
  747. pr_err("can't get an interrupt\n");
  748. ret = -EAGAIN;
  749. goto bad_out;
  750. }
  751. } else {
  752. #if !defined(CONFIG_CS89x0_PLATFORM)
  753. if (((1 << dev->irq) & lp->irq_map) == 0) {
  754. pr_err("%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
  755. dev->name, dev->irq, lp->irq_map);
  756. ret = -EAGAIN;
  757. goto bad_out;
  758. }
  759. #endif
  760. /* FIXME: Cirrus' release had this: */
  761. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ);
  762. /* And 2.3.47 had this: */
  763. #if 0
  764. writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
  765. #endif
  766. write_irq(dev, lp->chip_type, dev->irq);
  767. ret = request_irq(dev->irq, net_interrupt, 0, dev->name, dev);
  768. if (ret) {
  769. pr_err("request_irq(%d) failed\n", dev->irq);
  770. goto bad_out;
  771. }
  772. }
  773. #if ALLOW_DMA
  774. if (lp->use_dma && (lp->isa_config & ANY_ISA_DMA)) {
  775. unsigned long flags;
  776. lp->dma_buff = (unsigned char *)__get_dma_pages(GFP_KERNEL,
  777. get_order(lp->dmasize * 1024));
  778. if (!lp->dma_buff) {
  779. pr_err("%s: cannot get %dK memory for DMA\n",
  780. dev->name, lp->dmasize);
  781. goto release_irq;
  782. }
  783. cs89_dbg(1, debug, "%s: dma %lx %lx\n",
  784. dev->name,
  785. (unsigned long)lp->dma_buff,
  786. (unsigned long)isa_virt_to_bus(lp->dma_buff));
  787. if ((unsigned long)lp->dma_buff >= MAX_DMA_ADDRESS ||
  788. !dma_page_eq(lp->dma_buff,
  789. lp->dma_buff + lp->dmasize * 1024 - 1)) {
  790. pr_err("%s: not usable as DMA buffer\n", dev->name);
  791. goto release_irq;
  792. }
  793. memset(lp->dma_buff, 0, lp->dmasize * 1024); /* Why? */
  794. if (request_dma(dev->dma, dev->name)) {
  795. pr_err("%s: cannot get dma channel %d\n",
  796. dev->name, dev->dma);
  797. goto release_irq;
  798. }
  799. write_dma(dev, lp->chip_type, dev->dma);
  800. lp->rx_dma_ptr = lp->dma_buff;
  801. lp->end_dma_buff = lp->dma_buff + lp->dmasize * 1024;
  802. spin_lock_irqsave(&lp->lock, flags);
  803. disable_dma(dev->dma);
  804. clear_dma_ff(dev->dma);
  805. set_dma_mode(dev->dma, DMA_RX_MODE); /* auto_init as well */
  806. set_dma_addr(dev->dma, isa_virt_to_bus(lp->dma_buff));
  807. set_dma_count(dev->dma, lp->dmasize * 1024);
  808. enable_dma(dev->dma);
  809. spin_unlock_irqrestore(&lp->lock, flags);
  810. }
  811. #endif /* ALLOW_DMA */
  812. /* set the Ethernet address */
  813. for (i = 0; i < ETH_ALEN / 2; i++)
  814. writereg(dev, PP_IA + i * 2,
  815. (dev->dev_addr[i * 2] |
  816. (dev->dev_addr[i * 2 + 1] << 8)));
  817. /* while we're testing the interface, leave interrupts disabled */
  818. writereg(dev, PP_BusCTL, MEMORY_ON);
  819. /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
  820. if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) &&
  821. (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
  822. lp->linectl = LOW_RX_SQUELCH;
  823. else
  824. lp->linectl = 0;
  825. /* check to make sure that they have the "right" hardware available */
  826. switch (lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
  827. case A_CNF_MEDIA_10B_T:
  828. result = lp->adapter_cnf & A_CNF_10B_T;
  829. break;
  830. case A_CNF_MEDIA_AUI:
  831. result = lp->adapter_cnf & A_CNF_AUI;
  832. break;
  833. case A_CNF_MEDIA_10B_2:
  834. result = lp->adapter_cnf & A_CNF_10B_2;
  835. break;
  836. default:
  837. result = lp->adapter_cnf & (A_CNF_10B_T |
  838. A_CNF_AUI |
  839. A_CNF_10B_2);
  840. }
  841. if (!result) {
  842. pr_err("%s: EEPROM is configured for unavailable media\n",
  843. dev->name);
  844. release_dma:
  845. #if ALLOW_DMA
  846. free_dma(dev->dma);
  847. release_irq:
  848. release_dma_buff(lp);
  849. #endif
  850. writereg(dev, PP_LineCTL,
  851. readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
  852. free_irq(dev->irq, dev);
  853. ret = -EAGAIN;
  854. goto bad_out;
  855. }
  856. /* set the hardware to the configured choice */
  857. switch (lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
  858. case A_CNF_MEDIA_10B_T:
  859. result = detect_tp(dev);
  860. if (result == DETECTED_NONE) {
  861. pr_warn("%s: 10Base-T (RJ-45) has no cable\n",
  862. dev->name);
  863. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  864. result = DETECTED_RJ45H; /* Yes! I don't care if I see a link pulse */
  865. }
  866. break;
  867. case A_CNF_MEDIA_AUI:
  868. result = detect_aui(dev);
  869. if (result == DETECTED_NONE) {
  870. pr_warn("%s: 10Base-5 (AUI) has no cable\n", dev->name);
  871. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  872. result = DETECTED_AUI; /* Yes! I don't care if I see a carrrier */
  873. }
  874. break;
  875. case A_CNF_MEDIA_10B_2:
  876. result = detect_bnc(dev);
  877. if (result == DETECTED_NONE) {
  878. pr_warn("%s: 10Base-2 (BNC) has no cable\n", dev->name);
  879. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  880. result = DETECTED_BNC; /* Yes! I don't care if I can xmit a packet */
  881. }
  882. break;
  883. case A_CNF_MEDIA_AUTO:
  884. writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
  885. if (lp->adapter_cnf & A_CNF_10B_T) {
  886. result = detect_tp(dev);
  887. if (result != DETECTED_NONE)
  888. break;
  889. }
  890. if (lp->adapter_cnf & A_CNF_AUI) {
  891. result = detect_aui(dev);
  892. if (result != DETECTED_NONE)
  893. break;
  894. }
  895. if (lp->adapter_cnf & A_CNF_10B_2) {
  896. result = detect_bnc(dev);
  897. if (result != DETECTED_NONE)
  898. break;
  899. }
  900. pr_err("%s: no media detected\n", dev->name);
  901. goto release_dma;
  902. }
  903. switch (result) {
  904. case DETECTED_NONE:
  905. pr_err("%s: no network cable attached to configured media\n",
  906. dev->name);
  907. goto release_dma;
  908. case DETECTED_RJ45H:
  909. pr_info("%s: using half-duplex 10Base-T (RJ-45)\n", dev->name);
  910. break;
  911. case DETECTED_RJ45F:
  912. pr_info("%s: using full-duplex 10Base-T (RJ-45)\n", dev->name);
  913. break;
  914. case DETECTED_AUI:
  915. pr_info("%s: using 10Base-5 (AUI)\n", dev->name);
  916. break;
  917. case DETECTED_BNC:
  918. pr_info("%s: using 10Base-2 (BNC)\n", dev->name);
  919. break;
  920. }
  921. /* Turn on both receive and transmit operations */
  922. writereg(dev, PP_LineCTL,
  923. readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
  924. /* Receive only error free packets addressed to this card */
  925. lp->rx_mode = 0;
  926. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
  927. lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
  928. if (lp->isa_config & STREAM_TRANSFER)
  929. lp->curr_rx_cfg |= RX_STREAM_ENBL;
  930. #if ALLOW_DMA
  931. set_dma_cfg(dev);
  932. #endif
  933. writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
  934. writereg(dev, PP_TxCFG, (TX_LOST_CRS_ENBL |
  935. TX_SQE_ERROR_ENBL |
  936. TX_OK_ENBL |
  937. TX_LATE_COL_ENBL |
  938. TX_JBR_ENBL |
  939. TX_ANY_COL_ENBL |
  940. TX_16_COL_ENBL));
  941. writereg(dev, PP_BufCFG, (READY_FOR_TX_ENBL |
  942. RX_MISS_COUNT_OVRFLOW_ENBL |
  943. #if ALLOW_DMA
  944. dma_bufcfg(dev) |
  945. #endif
  946. TX_COL_COUNT_OVRFLOW_ENBL |
  947. TX_UNDERRUN_ENBL));
  948. /* now that we've got our act together, enable everything */
  949. writereg(dev, PP_BusCTL, (ENABLE_IRQ
  950. | (dev->mem_start ? MEMORY_ON : 0) /* turn memory on */
  951. #if ALLOW_DMA
  952. | dma_busctl(dev)
  953. #endif
  954. ));
  955. netif_start_queue(dev);
  956. cs89_dbg(1, debug, "net_open() succeeded\n");
  957. return 0;
  958. bad_out:
  959. return ret;
  960. }
  961. /* The inverse routine to net_open(). */
  962. static int
  963. net_close(struct net_device *dev)
  964. {
  965. #if ALLOW_DMA
  966. struct net_local *lp = netdev_priv(dev);
  967. #endif
  968. netif_stop_queue(dev);
  969. writereg(dev, PP_RxCFG, 0);
  970. writereg(dev, PP_TxCFG, 0);
  971. writereg(dev, PP_BufCFG, 0);
  972. writereg(dev, PP_BusCTL, 0);
  973. free_irq(dev->irq, dev);
  974. #if ALLOW_DMA
  975. if (lp->use_dma && lp->dma) {
  976. free_dma(dev->dma);
  977. release_dma_buff(lp);
  978. }
  979. #endif
  980. /* Update the statistics here. */
  981. return 0;
  982. }
  983. /* Get the current statistics.
  984. * This may be called with the card open or closed.
  985. */
  986. static struct net_device_stats *
  987. net_get_stats(struct net_device *dev)
  988. {
  989. struct net_local *lp = netdev_priv(dev);
  990. unsigned long flags;
  991. spin_lock_irqsave(&lp->lock, flags);
  992. /* Update the statistics from the device registers. */
  993. dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
  994. dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
  995. spin_unlock_irqrestore(&lp->lock, flags);
  996. return &dev->stats;
  997. }
  998. static void net_timeout(struct net_device *dev)
  999. {
  1000. /* If we get here, some higher level has decided we are broken.
  1001. There should really be a "kick me" function call instead. */
  1002. cs89_dbg(0, err, "%s: transmit timed out, %s?\n",
  1003. dev->name,
  1004. tx_done(dev) ? "IRQ conflict" : "network cable problem");
  1005. /* Try to restart the adaptor. */
  1006. netif_wake_queue(dev);
  1007. }
  1008. static netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev)
  1009. {
  1010. struct net_local *lp = netdev_priv(dev);
  1011. unsigned long flags;
  1012. cs89_dbg(3, debug, "%s: sent %d byte packet of type %x\n",
  1013. dev->name, skb->len,
  1014. ((skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  1015. skb->data[ETH_ALEN + ETH_ALEN + 1]));
  1016. /* keep the upload from being interrupted, since we
  1017. * ask the chip to start transmitting before the
  1018. * whole packet has been completely uploaded.
  1019. */
  1020. spin_lock_irqsave(&lp->lock, flags);
  1021. netif_stop_queue(dev);
  1022. /* initiate a transmit sequence */
  1023. iowrite16(lp->send_cmd, lp->virt_addr + TX_CMD_PORT);
  1024. iowrite16(skb->len, lp->virt_addr + TX_LEN_PORT);
  1025. /* Test to see if the chip has allocated memory for the packet */
  1026. if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
  1027. /* Gasp! It hasn't. But that shouldn't happen since
  1028. * we're waiting for TxOk, so return 1 and requeue this packet.
  1029. */
  1030. spin_unlock_irqrestore(&lp->lock, flags);
  1031. cs89_dbg(0, err, "Tx buffer not free!\n");
  1032. return NETDEV_TX_BUSY;
  1033. }
  1034. /* Write the contents of the packet */
  1035. writewords(lp, TX_FRAME_PORT, skb->data, (skb->len + 1) >> 1);
  1036. spin_unlock_irqrestore(&lp->lock, flags);
  1037. dev->stats.tx_bytes += skb->len;
  1038. dev_consume_skb_any(skb);
  1039. /* We DO NOT call netif_wake_queue() here.
  1040. * We also DO NOT call netif_start_queue().
  1041. *
  1042. * Either of these would cause another bottom half run through
  1043. * net_send_packet() before this packet has fully gone out.
  1044. * That causes us to hit the "Gasp!" above and the send is rescheduled.
  1045. * it runs like a dog. We just return and wait for the Tx completion
  1046. * interrupt handler to restart the netdevice layer
  1047. */
  1048. return NETDEV_TX_OK;
  1049. }
  1050. static void set_multicast_list(struct net_device *dev)
  1051. {
  1052. struct net_local *lp = netdev_priv(dev);
  1053. unsigned long flags;
  1054. u16 cfg;
  1055. spin_lock_irqsave(&lp->lock, flags);
  1056. if (dev->flags & IFF_PROMISC)
  1057. lp->rx_mode = RX_ALL_ACCEPT;
  1058. else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
  1059. /* The multicast-accept list is initialized to accept-all,
  1060. * and we rely on higher-level filtering for now.
  1061. */
  1062. lp->rx_mode = RX_MULTCAST_ACCEPT;
  1063. else
  1064. lp->rx_mode = 0;
  1065. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
  1066. /* in promiscuous mode, we accept errored packets,
  1067. * so we have to enable interrupts on them also
  1068. */
  1069. cfg = lp->curr_rx_cfg;
  1070. if (lp->rx_mode == RX_ALL_ACCEPT)
  1071. cfg |= RX_CRC_ERROR_ENBL | RX_RUNT_ENBL | RX_EXTRA_DATA_ENBL;
  1072. writereg(dev, PP_RxCFG, cfg);
  1073. spin_unlock_irqrestore(&lp->lock, flags);
  1074. }
  1075. static int set_mac_address(struct net_device *dev, void *p)
  1076. {
  1077. int i;
  1078. struct sockaddr *addr = p;
  1079. if (netif_running(dev))
  1080. return -EBUSY;
  1081. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1082. cs89_dbg(0, debug, "%s: Setting MAC address to %pM\n",
  1083. dev->name, dev->dev_addr);
  1084. /* set the Ethernet address */
  1085. for (i = 0; i < ETH_ALEN / 2; i++)
  1086. writereg(dev, PP_IA + i * 2,
  1087. (dev->dev_addr[i * 2] |
  1088. (dev->dev_addr[i * 2 + 1] << 8)));
  1089. return 0;
  1090. }
  1091. #ifdef CONFIG_NET_POLL_CONTROLLER
  1092. /*
  1093. * Polling receive - used by netconsole and other diagnostic tools
  1094. * to allow network i/o with interrupts disabled.
  1095. */
  1096. static void net_poll_controller(struct net_device *dev)
  1097. {
  1098. disable_irq(dev->irq);
  1099. net_interrupt(dev->irq, dev);
  1100. enable_irq(dev->irq);
  1101. }
  1102. #endif
  1103. static const struct net_device_ops net_ops = {
  1104. .ndo_open = net_open,
  1105. .ndo_stop = net_close,
  1106. .ndo_tx_timeout = net_timeout,
  1107. .ndo_start_xmit = net_send_packet,
  1108. .ndo_get_stats = net_get_stats,
  1109. .ndo_set_rx_mode = set_multicast_list,
  1110. .ndo_set_mac_address = set_mac_address,
  1111. #ifdef CONFIG_NET_POLL_CONTROLLER
  1112. .ndo_poll_controller = net_poll_controller,
  1113. #endif
  1114. .ndo_change_mtu = eth_change_mtu,
  1115. .ndo_validate_addr = eth_validate_addr,
  1116. };
  1117. static void __init reset_chip(struct net_device *dev)
  1118. {
  1119. #if !defined(CONFIG_MACH_MX31ADS)
  1120. struct net_local *lp = netdev_priv(dev);
  1121. int reset_start_time;
  1122. writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
  1123. /* wait 30 ms */
  1124. msleep(30);
  1125. if (lp->chip_type != CS8900) {
  1126. /* Hardware problem requires PNP registers to be reconfigured after a reset */
  1127. iowrite16(PP_CS8920_ISAINT, lp->virt_addr + ADD_PORT);
  1128. iowrite8(dev->irq, lp->virt_addr + DATA_PORT);
  1129. iowrite8(0, lp->virt_addr + DATA_PORT + 1);
  1130. iowrite16(PP_CS8920_ISAMemB, lp->virt_addr + ADD_PORT);
  1131. iowrite8((dev->mem_start >> 16) & 0xff,
  1132. lp->virt_addr + DATA_PORT);
  1133. iowrite8((dev->mem_start >> 8) & 0xff,
  1134. lp->virt_addr + DATA_PORT + 1);
  1135. }
  1136. /* Wait until the chip is reset */
  1137. reset_start_time = jiffies;
  1138. while ((readreg(dev, PP_SelfST) & INIT_DONE) == 0 &&
  1139. jiffies - reset_start_time < 2)
  1140. ;
  1141. #endif /* !CONFIG_MACH_MX31ADS */
  1142. }
  1143. /* This is the real probe routine.
  1144. * Linux has a history of friendly device probes on the ISA bus.
  1145. * A good device probes avoids doing writes, and
  1146. * verifies that the correct device exists and functions.
  1147. * Return 0 on success.
  1148. */
  1149. static int __init
  1150. cs89x0_probe1(struct net_device *dev, void __iomem *ioaddr, int modular)
  1151. {
  1152. struct net_local *lp = netdev_priv(dev);
  1153. int i;
  1154. int tmp;
  1155. unsigned rev_type = 0;
  1156. int eeprom_buff[CHKSUM_LEN];
  1157. int retval;
  1158. /* Initialize the device structure. */
  1159. if (!modular) {
  1160. memset(lp, 0, sizeof(*lp));
  1161. spin_lock_init(&lp->lock);
  1162. #ifndef MODULE
  1163. #if ALLOW_DMA
  1164. if (g_cs89x0_dma) {
  1165. lp->use_dma = 1;
  1166. lp->dma = g_cs89x0_dma;
  1167. lp->dmasize = 16; /* Could make this an option... */
  1168. }
  1169. #endif
  1170. lp->force = g_cs89x0_media__force;
  1171. #endif
  1172. }
  1173. pr_debug("PP_addr at %p[%x]: 0x%x\n",
  1174. ioaddr, ADD_PORT, ioread16(ioaddr + ADD_PORT));
  1175. iowrite16(PP_ChipID, ioaddr + ADD_PORT);
  1176. tmp = ioread16(ioaddr + DATA_PORT);
  1177. if (tmp != CHIP_EISA_ID_SIG) {
  1178. pr_debug("%s: incorrect signature at %p[%x]: 0x%x!="
  1179. CHIP_EISA_ID_SIG_STR "\n",
  1180. dev->name, ioaddr, DATA_PORT, tmp);
  1181. retval = -ENODEV;
  1182. goto out1;
  1183. }
  1184. lp->virt_addr = ioaddr;
  1185. /* get the chip type */
  1186. rev_type = readreg(dev, PRODUCT_ID_ADD);
  1187. lp->chip_type = rev_type & ~REVISON_BITS;
  1188. lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
  1189. /* Check the chip type and revision in order to set the correct
  1190. * send command. CS8920 revision C and CS8900 revision F can use
  1191. * the faster send.
  1192. */
  1193. lp->send_cmd = TX_AFTER_381;
  1194. if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
  1195. lp->send_cmd = TX_NOW;
  1196. if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
  1197. lp->send_cmd = TX_NOW;
  1198. pr_info_once("%s\n", version);
  1199. pr_info("%s: cs89%c0%s rev %c found at %p ",
  1200. dev->name,
  1201. lp->chip_type == CS8900 ? '0' : '2',
  1202. lp->chip_type == CS8920M ? "M" : "",
  1203. lp->chip_revision,
  1204. lp->virt_addr);
  1205. reset_chip(dev);
  1206. /* Here we read the current configuration of the chip.
  1207. * If there is no Extended EEPROM then the idea is to not disturb
  1208. * the chip configuration, it should have been correctly setup by
  1209. * automatic EEPROM read on reset. So, if the chip says it read
  1210. * the EEPROM the driver will always do *something* instead of
  1211. * complain that adapter_cnf is 0.
  1212. */
  1213. if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) ==
  1214. (EEPROM_OK | EEPROM_PRESENT)) {
  1215. /* Load the MAC. */
  1216. for (i = 0; i < ETH_ALEN / 2; i++) {
  1217. unsigned int Addr;
  1218. Addr = readreg(dev, PP_IA + i * 2);
  1219. dev->dev_addr[i * 2] = Addr & 0xFF;
  1220. dev->dev_addr[i * 2 + 1] = Addr >> 8;
  1221. }
  1222. /* Load the Adapter Configuration.
  1223. * Note: Barring any more specific information from some
  1224. * other source (ie EEPROM+Schematics), we would not know
  1225. * how to operate a 10Base2 interface on the AUI port.
  1226. * However, since we do read the status of HCB1 and use
  1227. * settings that always result in calls to control_dc_dc(dev,0)
  1228. * a BNC interface should work if the enable pin
  1229. * (dc/dc converter) is on HCB1.
  1230. * It will be called AUI however.
  1231. */
  1232. lp->adapter_cnf = 0;
  1233. i = readreg(dev, PP_LineCTL);
  1234. /* Preserve the setting of the HCB1 pin. */
  1235. if ((i & (HCB1 | HCB1_ENBL)) == (HCB1 | HCB1_ENBL))
  1236. lp->adapter_cnf |= A_CNF_DC_DC_POLARITY;
  1237. /* Save the sqelch bit */
  1238. if ((i & LOW_RX_SQUELCH) == LOW_RX_SQUELCH)
  1239. lp->adapter_cnf |= A_CNF_EXTND_10B_2 | A_CNF_LOW_RX_SQUELCH;
  1240. /* Check if the card is in 10Base-t only mode */
  1241. if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == 0)
  1242. lp->adapter_cnf |= A_CNF_10B_T | A_CNF_MEDIA_10B_T;
  1243. /* Check if the card is in AUI only mode */
  1244. if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUI_ONLY)
  1245. lp->adapter_cnf |= A_CNF_AUI | A_CNF_MEDIA_AUI;
  1246. /* Check if the card is in Auto mode. */
  1247. if ((i & (AUI_ONLY | AUTO_AUI_10BASET)) == AUTO_AUI_10BASET)
  1248. lp->adapter_cnf |= A_CNF_AUI | A_CNF_10B_T |
  1249. A_CNF_MEDIA_AUI | A_CNF_MEDIA_10B_T | A_CNF_MEDIA_AUTO;
  1250. cs89_dbg(1, info, "%s: PP_LineCTL=0x%x, adapter_cnf=0x%x\n",
  1251. dev->name, i, lp->adapter_cnf);
  1252. /* IRQ. Other chips already probe, see below. */
  1253. if (lp->chip_type == CS8900)
  1254. lp->isa_config = readreg(dev, PP_CS8900_ISAINT) & INT_NO_MASK;
  1255. pr_cont("[Cirrus EEPROM] ");
  1256. }
  1257. pr_cont("\n");
  1258. /* First check to see if an EEPROM is attached. */
  1259. if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
  1260. pr_warn("No EEPROM, relying on command line....\n");
  1261. else if (get_eeprom_data(dev, START_EEPROM_DATA, CHKSUM_LEN, eeprom_buff) < 0) {
  1262. pr_warn("EEPROM read failed, relying on command line\n");
  1263. } else if (get_eeprom_cksum(START_EEPROM_DATA, CHKSUM_LEN, eeprom_buff) < 0) {
  1264. /* Check if the chip was able to read its own configuration starting
  1265. at 0 in the EEPROM*/
  1266. if ((readreg(dev, PP_SelfST) & (EEPROM_OK | EEPROM_PRESENT)) !=
  1267. (EEPROM_OK | EEPROM_PRESENT))
  1268. pr_warn("Extended EEPROM checksum bad and no Cirrus EEPROM, relying on command line\n");
  1269. } else {
  1270. /* This reads an extended EEPROM that is not documented
  1271. * in the CS8900 datasheet.
  1272. */
  1273. /* get transmission control word but keep the autonegotiation bits */
  1274. if (!lp->auto_neg_cnf)
  1275. lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET / 2];
  1276. /* Store adapter configuration */
  1277. if (!lp->adapter_cnf)
  1278. lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET / 2];
  1279. /* Store ISA configuration */
  1280. lp->isa_config = eeprom_buff[ISA_CNF_OFFSET / 2];
  1281. dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET / 2] << 8;
  1282. /* eeprom_buff has 32-bit ints, so we can't just memcpy it */
  1283. /* store the initial memory base address */
  1284. for (i = 0; i < ETH_ALEN / 2; i++) {
  1285. dev->dev_addr[i * 2] = eeprom_buff[i];
  1286. dev->dev_addr[i * 2 + 1] = eeprom_buff[i] >> 8;
  1287. }
  1288. cs89_dbg(1, debug, "%s: new adapter_cnf: 0x%x\n",
  1289. dev->name, lp->adapter_cnf);
  1290. }
  1291. /* allow them to force multiple transceivers. If they force multiple, autosense */
  1292. {
  1293. int count = 0;
  1294. if (lp->force & FORCE_RJ45) {
  1295. lp->adapter_cnf |= A_CNF_10B_T;
  1296. count++;
  1297. }
  1298. if (lp->force & FORCE_AUI) {
  1299. lp->adapter_cnf |= A_CNF_AUI;
  1300. count++;
  1301. }
  1302. if (lp->force & FORCE_BNC) {
  1303. lp->adapter_cnf |= A_CNF_10B_2;
  1304. count++;
  1305. }
  1306. if (count > 1)
  1307. lp->adapter_cnf |= A_CNF_MEDIA_AUTO;
  1308. else if (lp->force & FORCE_RJ45)
  1309. lp->adapter_cnf |= A_CNF_MEDIA_10B_T;
  1310. else if (lp->force & FORCE_AUI)
  1311. lp->adapter_cnf |= A_CNF_MEDIA_AUI;
  1312. else if (lp->force & FORCE_BNC)
  1313. lp->adapter_cnf |= A_CNF_MEDIA_10B_2;
  1314. }
  1315. cs89_dbg(1, debug, "%s: after force 0x%x, adapter_cnf=0x%x\n",
  1316. dev->name, lp->force, lp->adapter_cnf);
  1317. /* FIXME: We don't let you set dc-dc polarity or low RX squelch from the command line: add it here */
  1318. /* FIXME: We don't let you set the IMM bit from the command line: add it to lp->auto_neg_cnf here */
  1319. /* FIXME: we don't set the Ethernet address on the command line. Use
  1320. * ifconfig IFACE hw ether AABBCCDDEEFF
  1321. */
  1322. pr_info("media %s%s%s",
  1323. (lp->adapter_cnf & A_CNF_10B_T) ? "RJ-45," : "",
  1324. (lp->adapter_cnf & A_CNF_AUI) ? "AUI," : "",
  1325. (lp->adapter_cnf & A_CNF_10B_2) ? "BNC," : "");
  1326. lp->irq_map = 0xffff;
  1327. /* If this is a CS8900 then no pnp soft */
  1328. if (lp->chip_type != CS8900 &&
  1329. /* Check if the ISA IRQ has been set */
  1330. (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
  1331. (i != 0 && i < CS8920_NO_INTS))) {
  1332. if (!dev->irq)
  1333. dev->irq = i;
  1334. } else {
  1335. i = lp->isa_config & INT_NO_MASK;
  1336. #ifndef CONFIG_CS89x0_PLATFORM
  1337. if (lp->chip_type == CS8900) {
  1338. /* Translate the IRQ using the IRQ mapping table. */
  1339. if (i >= ARRAY_SIZE(cs8900_irq_map))
  1340. pr_err("invalid ISA interrupt number %d\n", i);
  1341. else
  1342. i = cs8900_irq_map[i];
  1343. lp->irq_map = CS8900_IRQ_MAP; /* fixed IRQ map for CS8900 */
  1344. } else {
  1345. int irq_map_buff[IRQ_MAP_LEN/2];
  1346. if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
  1347. IRQ_MAP_LEN / 2,
  1348. irq_map_buff) >= 0) {
  1349. if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
  1350. lp->irq_map = ((irq_map_buff[0] >> 8) |
  1351. (irq_map_buff[1] << 8));
  1352. }
  1353. }
  1354. #endif
  1355. if (!dev->irq)
  1356. dev->irq = i;
  1357. }
  1358. pr_cont(" IRQ %d", dev->irq);
  1359. #if ALLOW_DMA
  1360. if (lp->use_dma) {
  1361. get_dma_channel(dev);
  1362. pr_cont(", DMA %d", dev->dma);
  1363. } else
  1364. #endif
  1365. pr_cont(", programmed I/O");
  1366. /* print the ethernet address. */
  1367. pr_cont(", MAC %pM\n", dev->dev_addr);
  1368. dev->netdev_ops = &net_ops;
  1369. dev->watchdog_timeo = HZ;
  1370. cs89_dbg(0, info, "cs89x0_probe1() successful\n");
  1371. retval = register_netdev(dev);
  1372. if (retval)
  1373. goto out2;
  1374. return 0;
  1375. out2:
  1376. iowrite16(PP_ChipID, lp->virt_addr + ADD_PORT);
  1377. out1:
  1378. return retval;
  1379. }
  1380. #ifndef CONFIG_CS89x0_PLATFORM
  1381. /*
  1382. * This function converts the I/O port addres used by the cs89x0_probe() and
  1383. * init_module() functions to the I/O memory address used by the
  1384. * cs89x0_probe1() function.
  1385. */
  1386. static int __init
  1387. cs89x0_ioport_probe(struct net_device *dev, unsigned long ioport, int modular)
  1388. {
  1389. struct net_local *lp = netdev_priv(dev);
  1390. int ret;
  1391. void __iomem *io_mem;
  1392. if (!lp)
  1393. return -ENOMEM;
  1394. dev->base_addr = ioport;
  1395. if (!request_region(ioport, NETCARD_IO_EXTENT, DRV_NAME)) {
  1396. ret = -EBUSY;
  1397. goto out;
  1398. }
  1399. io_mem = ioport_map(ioport & ~3, NETCARD_IO_EXTENT);
  1400. if (!io_mem) {
  1401. ret = -ENOMEM;
  1402. goto release;
  1403. }
  1404. /* if they give us an odd I/O address, then do ONE write to
  1405. * the address port, to get it back to address zero, where we
  1406. * expect to find the EISA signature word. An IO with a base of 0x3
  1407. * will skip the test for the ADD_PORT.
  1408. */
  1409. if (ioport & 1) {
  1410. cs89_dbg(1, info, "%s: odd ioaddr 0x%lx\n", dev->name, ioport);
  1411. if ((ioport & 2) != 2) {
  1412. if ((ioread16(io_mem + ADD_PORT) & ADD_MASK) !=
  1413. ADD_SIG) {
  1414. pr_err("%s: bad signature 0x%x\n",
  1415. dev->name, ioread16(io_mem + ADD_PORT));
  1416. ret = -ENODEV;
  1417. goto unmap;
  1418. }
  1419. }
  1420. }
  1421. ret = cs89x0_probe1(dev, io_mem, modular);
  1422. if (!ret)
  1423. goto out;
  1424. unmap:
  1425. ioport_unmap(io_mem);
  1426. release:
  1427. release_region(ioport, NETCARD_IO_EXTENT);
  1428. out:
  1429. return ret;
  1430. }
  1431. #ifndef MODULE
  1432. /* Check for a network adaptor of this type, and return '0' iff one exists.
  1433. * If dev->base_addr == 0, probe all likely locations.
  1434. * If dev->base_addr == 1, always return failure.
  1435. * If dev->base_addr == 2, allocate space for the device and return success
  1436. * (detachable devices only).
  1437. * Return 0 on success.
  1438. */
  1439. struct net_device * __init cs89x0_probe(int unit)
  1440. {
  1441. struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
  1442. unsigned *port;
  1443. int err = 0;
  1444. int irq;
  1445. int io;
  1446. if (!dev)
  1447. return ERR_PTR(-ENODEV);
  1448. sprintf(dev->name, "eth%d", unit);
  1449. netdev_boot_setup_check(dev);
  1450. io = dev->base_addr;
  1451. irq = dev->irq;
  1452. cs89_dbg(0, info, "cs89x0_probe(0x%x)\n", io);
  1453. if (io > 0x1ff) { /* Check a single specified location. */
  1454. err = cs89x0_ioport_probe(dev, io, 0);
  1455. } else if (io != 0) { /* Don't probe at all. */
  1456. err = -ENXIO;
  1457. } else {
  1458. for (port = netcard_portlist; *port; port++) {
  1459. if (cs89x0_ioport_probe(dev, *port, 0) == 0)
  1460. break;
  1461. dev->irq = irq;
  1462. }
  1463. if (!*port)
  1464. err = -ENODEV;
  1465. }
  1466. if (err)
  1467. goto out;
  1468. return dev;
  1469. out:
  1470. free_netdev(dev);
  1471. pr_warn("no cs8900 or cs8920 detected. Be sure to disable PnP with SETUP\n");
  1472. return ERR_PTR(err);
  1473. }
  1474. #endif
  1475. #endif
  1476. #if defined(MODULE) && !defined(CONFIG_CS89x0_PLATFORM)
  1477. static struct net_device *dev_cs89x0;
  1478. /* Support the 'debug' module parm even if we're compiled for non-debug to
  1479. * avoid breaking someone's startup scripts
  1480. */
  1481. static int io;
  1482. static int irq;
  1483. static int debug;
  1484. static char media[8];
  1485. static int duplex = -1;
  1486. static int use_dma; /* These generate unused var warnings if ALLOW_DMA = 0 */
  1487. static int dma;
  1488. static int dmasize = 16; /* or 64 */
  1489. module_param(io, int, 0);
  1490. module_param(irq, int, 0);
  1491. module_param(debug, int, 0);
  1492. module_param_string(media, media, sizeof(media), 0);
  1493. module_param(duplex, int, 0);
  1494. module_param(dma , int, 0);
  1495. module_param(dmasize , int, 0);
  1496. module_param(use_dma , int, 0);
  1497. MODULE_PARM_DESC(io, "cs89x0 I/O base address");
  1498. MODULE_PARM_DESC(irq, "cs89x0 IRQ number");
  1499. #if DEBUGGING
  1500. MODULE_PARM_DESC(debug, "cs89x0 debug level (0-6)");
  1501. #else
  1502. MODULE_PARM_DESC(debug, "(ignored)");
  1503. #endif
  1504. MODULE_PARM_DESC(media, "Set cs89x0 adapter(s) media type(s) (rj45,bnc,aui)");
  1505. /* No other value than -1 for duplex seems to be currently interpreted */
  1506. MODULE_PARM_DESC(duplex, "(ignored)");
  1507. #if ALLOW_DMA
  1508. MODULE_PARM_DESC(dma , "cs89x0 ISA DMA channel; ignored if use_dma=0");
  1509. MODULE_PARM_DESC(dmasize , "cs89x0 DMA size in kB (16,64); ignored if use_dma=0");
  1510. MODULE_PARM_DESC(use_dma , "cs89x0 using DMA (0-1)");
  1511. #else
  1512. MODULE_PARM_DESC(dma , "(ignored)");
  1513. MODULE_PARM_DESC(dmasize , "(ignored)");
  1514. MODULE_PARM_DESC(use_dma , "(ignored)");
  1515. #endif
  1516. MODULE_AUTHOR("Mike Cruse, Russwll Nelson <nelson@crynwr.com>, Andrew Morton");
  1517. MODULE_LICENSE("GPL");
  1518. /*
  1519. * media=t - specify media type
  1520. * or media=2
  1521. * or media=aui
  1522. * or medai=auto
  1523. * duplex=0 - specify forced half/full/autonegotiate duplex
  1524. * debug=# - debug level
  1525. *
  1526. * Default Chip Configuration:
  1527. * DMA Burst = enabled
  1528. * IOCHRDY Enabled = enabled
  1529. * UseSA = enabled
  1530. * CS8900 defaults to half-duplex if not specified on command-line
  1531. * CS8920 defaults to autoneg if not specified on command-line
  1532. * Use reset defaults for other config parameters
  1533. *
  1534. * Assumptions:
  1535. * media type specified is supported (circuitry is present)
  1536. * if memory address is > 1MB, then required mem decode hw is present
  1537. * if 10B-2, then agent other than driver will enable DC/DC converter
  1538. * (hw or software util)
  1539. */
  1540. int __init init_module(void)
  1541. {
  1542. struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
  1543. struct net_local *lp;
  1544. int ret = 0;
  1545. #if DEBUGGING
  1546. net_debug = debug;
  1547. #else
  1548. debug = 0;
  1549. #endif
  1550. if (!dev)
  1551. return -ENOMEM;
  1552. dev->irq = irq;
  1553. dev->base_addr = io;
  1554. lp = netdev_priv(dev);
  1555. #if ALLOW_DMA
  1556. if (use_dma) {
  1557. lp->use_dma = use_dma;
  1558. lp->dma = dma;
  1559. lp->dmasize = dmasize;
  1560. }
  1561. #endif
  1562. spin_lock_init(&lp->lock);
  1563. /* boy, they'd better get these right */
  1564. if (!strcmp(media, "rj45"))
  1565. lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
  1566. else if (!strcmp(media, "aui"))
  1567. lp->adapter_cnf = A_CNF_MEDIA_AUI | A_CNF_AUI;
  1568. else if (!strcmp(media, "bnc"))
  1569. lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
  1570. else
  1571. lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
  1572. if (duplex == -1)
  1573. lp->auto_neg_cnf = AUTO_NEG_ENABLE;
  1574. if (io == 0) {
  1575. pr_err("Module autoprobing not allowed\n");
  1576. pr_err("Append io=0xNNN\n");
  1577. ret = -EPERM;
  1578. goto out;
  1579. } else if (io <= 0x1ff) {
  1580. ret = -ENXIO;
  1581. goto out;
  1582. }
  1583. #if ALLOW_DMA
  1584. if (use_dma && dmasize != 16 && dmasize != 64) {
  1585. pr_err("dma size must be either 16K or 64K, not %dK\n",
  1586. dmasize);
  1587. ret = -EPERM;
  1588. goto out;
  1589. }
  1590. #endif
  1591. ret = cs89x0_ioport_probe(dev, io, 1);
  1592. if (ret)
  1593. goto out;
  1594. dev_cs89x0 = dev;
  1595. return 0;
  1596. out:
  1597. free_netdev(dev);
  1598. return ret;
  1599. }
  1600. void __exit
  1601. cleanup_module(void)
  1602. {
  1603. struct net_local *lp = netdev_priv(dev_cs89x0);
  1604. unregister_netdev(dev_cs89x0);
  1605. iowrite16(PP_ChipID, lp->virt_addr + ADD_PORT);
  1606. ioport_unmap(lp->virt_addr);
  1607. release_region(dev_cs89x0->base_addr, NETCARD_IO_EXTENT);
  1608. free_netdev(dev_cs89x0);
  1609. }
  1610. #endif /* MODULE && !CONFIG_CS89x0_PLATFORM */
  1611. #ifdef CONFIG_CS89x0_PLATFORM
  1612. static int __init cs89x0_platform_probe(struct platform_device *pdev)
  1613. {
  1614. struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
  1615. struct net_local *lp;
  1616. struct resource *mem_res;
  1617. void __iomem *virt_addr;
  1618. int err;
  1619. if (!dev)
  1620. return -ENOMEM;
  1621. lp = netdev_priv(dev);
  1622. dev->irq = platform_get_irq(pdev, 0);
  1623. if (dev->irq <= 0) {
  1624. dev_warn(&dev->dev, "interrupt resource missing\n");
  1625. err = -ENXIO;
  1626. goto free;
  1627. }
  1628. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1629. virt_addr = devm_ioremap_resource(&pdev->dev, mem_res);
  1630. if (IS_ERR(virt_addr)) {
  1631. err = PTR_ERR(virt_addr);
  1632. goto free;
  1633. }
  1634. err = cs89x0_probe1(dev, virt_addr, 0);
  1635. if (err) {
  1636. dev_warn(&dev->dev, "no cs8900 or cs8920 detected\n");
  1637. goto free;
  1638. }
  1639. platform_set_drvdata(pdev, dev);
  1640. return 0;
  1641. free:
  1642. free_netdev(dev);
  1643. return err;
  1644. }
  1645. static int cs89x0_platform_remove(struct platform_device *pdev)
  1646. {
  1647. struct net_device *dev = platform_get_drvdata(pdev);
  1648. /* This platform_get_resource() call will not return NULL, because
  1649. * the same call in cs89x0_platform_probe() has returned a non NULL
  1650. * value.
  1651. */
  1652. unregister_netdev(dev);
  1653. free_netdev(dev);
  1654. return 0;
  1655. }
  1656. static struct platform_driver cs89x0_driver = {
  1657. .driver = {
  1658. .name = DRV_NAME,
  1659. .owner = THIS_MODULE,
  1660. },
  1661. .remove = cs89x0_platform_remove,
  1662. };
  1663. module_platform_driver_probe(cs89x0_driver, cs89x0_platform_probe);
  1664. #endif /* CONFIG_CS89x0_PLATFORM */