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