cs89x0.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  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/of.h>
  51. #include <linux/of_device.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/kernel.h>
  54. #include <linux/types.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/ioport.h>
  58. #include <linux/in.h>
  59. #include <linux/jiffies.h>
  60. #include <linux/skbuff.h>
  61. #include <linux/spinlock.h>
  62. #include <linux/string.h>
  63. #include <linux/init.h>
  64. #include <linux/bitops.h>
  65. #include <linux/delay.h>
  66. #include <linux/gfp.h>
  67. #include <linux/io.h>
  68. #include <asm/irq.h>
  69. #include <linux/atomic.h>
  70. #if ALLOW_DMA
  71. #include <asm/dma.h>
  72. #endif
  73. #include "cs89x0.h"
  74. #define cs89_dbg(val, level, fmt, ...) \
  75. do { \
  76. if (val <= net_debug) \
  77. pr_##level(fmt, ##__VA_ARGS__); \
  78. } while (0)
  79. static char version[] __initdata =
  80. "v2.4.3-pre1 Russell Nelson <nelson@crynwr.com>, Andrew Morton";
  81. #define DRV_NAME "cs89x0"
  82. /* First, a few definitions that the brave might change.
  83. * A zero-terminated list of I/O addresses to be probed. Some special flags..
  84. * Addr & 1 = Read back the address port, look for signature and reset
  85. * the page window before probing
  86. * Addr & 3 = Reset the page window and probe
  87. * The CLPS eval board has the Cirrus chip at 0x80090300, in ARM IO space,
  88. * but it is possible that a Cirrus board could be plugged into the ISA
  89. * slots.
  90. */
  91. /* The cs8900 has 4 IRQ pins, software selectable. cs8900_irq_map maps
  92. * them to system IRQ numbers. This mapping is card specific and is set to
  93. * the configuration of the Cirrus Eval board for this chip.
  94. */
  95. #ifndef CONFIG_CS89x0_PLATFORM
  96. static unsigned int netcard_portlist[] __used __initdata = {
  97. 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240,
  98. 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0
  99. };
  100. static unsigned int cs8900_irq_map[] = {
  101. 10, 11, 12, 5
  102. };
  103. #endif
  104. #if DEBUGGING
  105. static unsigned int net_debug = DEBUGGING;
  106. #else
  107. #define net_debug 0 /* gcc will remove all the debug code for us */
  108. #endif
  109. /* The number of low I/O ports used by the ethercard. */
  110. #define NETCARD_IO_EXTENT 16
  111. /* we allow the user to override various values normally set in the EEPROM */
  112. #define FORCE_RJ45 0x0001 /* pick one of these three */
  113. #define FORCE_AUI 0x0002
  114. #define FORCE_BNC 0x0004
  115. #define FORCE_AUTO 0x0010 /* pick one of these three */
  116. #define FORCE_HALF 0x0020
  117. #define FORCE_FULL 0x0030
  118. /* Information that need to be kept for each board. */
  119. struct net_local {
  120. int chip_type; /* one of: CS8900, CS8920, CS8920M */
  121. char chip_revision; /* revision letter of the chip ('A'...) */
  122. int send_cmd; /* the proper send command: TX_NOW, TX_AFTER_381, or TX_AFTER_ALL */
  123. int auto_neg_cnf; /* auto-negotiation word from EEPROM */
  124. int adapter_cnf; /* adapter configuration from EEPROM */
  125. int isa_config; /* ISA configuration from EEPROM */
  126. int irq_map; /* IRQ map from EEPROM */
  127. int rx_mode; /* what mode are we in? 0, RX_MULTCAST_ACCEPT, or RX_ALL_ACCEPT */
  128. int curr_rx_cfg; /* a copy of PP_RxCFG */
  129. int linectl; /* either 0 or LOW_RX_SQUELCH, depending on configuration. */
  130. int send_underrun; /* keep track of how many underruns in a row we get */
  131. int force; /* force various values; see FORCE* above. */
  132. spinlock_t lock;
  133. void __iomem *virt_addr;/* CS89x0 virtual address. */
  134. #if ALLOW_DMA
  135. int use_dma; /* Flag: we're using dma */
  136. int dma; /* DMA channel */
  137. int dmasize; /* 16 or 64 */
  138. unsigned char *dma_buff; /* points to the beginning of the buffer */
  139. unsigned char *end_dma_buff; /* points to the end of the buffer */
  140. unsigned char *rx_dma_ptr; /* points to the next packet */
  141. #endif
  142. };
  143. /* Example routines you must write ;->. */
  144. #define tx_done(dev) 1
  145. /*
  146. * Permit 'cs89x0_dma=N' in the kernel boot environment
  147. */
  148. #if !defined(MODULE)
  149. #if ALLOW_DMA
  150. static int g_cs89x0_dma;
  151. static int __init dma_fn(char *str)
  152. {
  153. g_cs89x0_dma = simple_strtol(str, NULL, 0);
  154. return 1;
  155. }
  156. __setup("cs89x0_dma=", dma_fn);
  157. #endif /* ALLOW_DMA */
  158. static int g_cs89x0_media__force;
  159. static int __init media_fn(char *str)
  160. {
  161. if (!strcmp(str, "rj45"))
  162. g_cs89x0_media__force = FORCE_RJ45;
  163. else if (!strcmp(str, "aui"))
  164. g_cs89x0_media__force = FORCE_AUI;
  165. else if (!strcmp(str, "bnc"))
  166. g_cs89x0_media__force = FORCE_BNC;
  167. return 1;
  168. }
  169. __setup("cs89x0_media=", media_fn);
  170. #endif
  171. static void readwords(struct net_local *lp, int portno, void *buf, int length)
  172. {
  173. u8 *buf8 = (u8 *)buf;
  174. do {
  175. u16 tmp16;
  176. tmp16 = ioread16(lp->virt_addr + portno);
  177. *buf8++ = (u8)tmp16;
  178. *buf8++ = (u8)(tmp16 >> 8);
  179. } while (--length);
  180. }
  181. static void writewords(struct net_local *lp, int portno, void *buf, int length)
  182. {
  183. u8 *buf8 = (u8 *)buf;
  184. do {
  185. u16 tmp16;
  186. tmp16 = *buf8++;
  187. tmp16 |= (*buf8++) << 8;
  188. iowrite16(tmp16, lp->virt_addr + portno);
  189. } while (--length);
  190. }
  191. static u16
  192. readreg(struct net_device *dev, u16 regno)
  193. {
  194. struct net_local *lp = netdev_priv(dev);
  195. iowrite16(regno, lp->virt_addr + ADD_PORT);
  196. return ioread16(lp->virt_addr + DATA_PORT);
  197. }
  198. static void
  199. writereg(struct net_device *dev, u16 regno, u16 value)
  200. {
  201. struct net_local *lp = netdev_priv(dev);
  202. iowrite16(regno, lp->virt_addr + ADD_PORT);
  203. iowrite16(value, lp->virt_addr + DATA_PORT);
  204. }
  205. static int __init
  206. wait_eeprom_ready(struct net_device *dev)
  207. {
  208. unsigned long timeout = jiffies;
  209. /* check to see if the EEPROM is ready,
  210. * a timeout is used just in case EEPROM is ready when
  211. * SI_BUSY in the PP_SelfST is clear
  212. */
  213. while (readreg(dev, PP_SelfST) & SI_BUSY)
  214. if (time_after_eq(jiffies, timeout + 40))
  215. return -1;
  216. return 0;
  217. }
  218. static int __init
  219. get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
  220. {
  221. int i;
  222. cs89_dbg(3, info, "EEPROM data from %x for %x:", off, len);
  223. for (i = 0; i < len; i++) {
  224. if (wait_eeprom_ready(dev) < 0)
  225. return -1;
  226. /* Now send the EEPROM read command and EEPROM location to read */
  227. writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
  228. if (wait_eeprom_ready(dev) < 0)
  229. return -1;
  230. buffer[i] = readreg(dev, PP_EEData);
  231. cs89_dbg(3, cont, " %04x", buffer[i]);
  232. }
  233. cs89_dbg(3, cont, "\n");
  234. return 0;
  235. }
  236. static int __init
  237. get_eeprom_cksum(int off, int len, int *buffer)
  238. {
  239. int i, cksum;
  240. cksum = 0;
  241. for (i = 0; i < len; i++)
  242. cksum += buffer[i];
  243. cksum &= 0xffff;
  244. if (cksum == 0)
  245. return 0;
  246. return -1;
  247. }
  248. static void
  249. write_irq(struct net_device *dev, int chip_type, int irq)
  250. {
  251. int i;
  252. if (chip_type == CS8900) {
  253. #ifndef CONFIG_CS89x0_PLATFORM
  254. /* Search the mapping table for the corresponding IRQ pin. */
  255. for (i = 0; i != ARRAY_SIZE(cs8900_irq_map); i++)
  256. if (cs8900_irq_map[i] == irq)
  257. break;
  258. /* Not found */
  259. if (i == ARRAY_SIZE(cs8900_irq_map))
  260. i = 3;
  261. #else
  262. /* INTRQ0 pin is used for interrupt generation. */
  263. i = 0;
  264. #endif
  265. writereg(dev, PP_CS8900_ISAINT, i);
  266. } else {
  267. writereg(dev, PP_CS8920_ISAINT, irq);
  268. }
  269. }
  270. static void
  271. count_rx_errors(int status, struct net_device *dev)
  272. {
  273. dev->stats.rx_errors++;
  274. if (status & RX_RUNT)
  275. dev->stats.rx_length_errors++;
  276. if (status & RX_EXTRA_DATA)
  277. dev->stats.rx_length_errors++;
  278. if ((status & RX_CRC_ERROR) && !(status & (RX_EXTRA_DATA | RX_RUNT)))
  279. /* per str 172 */
  280. dev->stats.rx_crc_errors++;
  281. if (status & RX_DRIBBLE)
  282. dev->stats.rx_frame_errors++;
  283. }
  284. /*********************************
  285. * This page contains DMA routines
  286. *********************************/
  287. #if ALLOW_DMA
  288. #define dma_page_eq(ptr1, ptr2) ((long)(ptr1) >> 17 == (long)(ptr2) >> 17)
  289. static void
  290. get_dma_channel(struct net_device *dev)
  291. {
  292. struct net_local *lp = netdev_priv(dev);
  293. if (lp->dma) {
  294. dev->dma = lp->dma;
  295. lp->isa_config |= ISA_RxDMA;
  296. } else {
  297. if ((lp->isa_config & ANY_ISA_DMA) == 0)
  298. return;
  299. dev->dma = lp->isa_config & DMA_NO_MASK;
  300. if (lp->chip_type == CS8900)
  301. dev->dma += 5;
  302. if (dev->dma < 5 || dev->dma > 7) {
  303. lp->isa_config &= ~ANY_ISA_DMA;
  304. return;
  305. }
  306. }
  307. }
  308. static void
  309. write_dma(struct net_device *dev, int chip_type, int dma)
  310. {
  311. struct net_local *lp = netdev_priv(dev);
  312. if ((lp->isa_config & ANY_ISA_DMA) == 0)
  313. return;
  314. if (chip_type == CS8900)
  315. writereg(dev, PP_CS8900_ISADMA, dma - 5);
  316. else
  317. writereg(dev, PP_CS8920_ISADMA, dma);
  318. }
  319. static void
  320. set_dma_cfg(struct net_device *dev)
  321. {
  322. struct net_local *lp = netdev_priv(dev);
  323. if (lp->use_dma) {
  324. if ((lp->isa_config & ANY_ISA_DMA) == 0) {
  325. cs89_dbg(3, err, "set_dma_cfg(): no DMA\n");
  326. return;
  327. }
  328. if (lp->isa_config & ISA_RxDMA) {
  329. lp->curr_rx_cfg |= RX_DMA_ONLY;
  330. cs89_dbg(3, info, "set_dma_cfg(): RX_DMA_ONLY\n");
  331. } else {
  332. lp->curr_rx_cfg |= AUTO_RX_DMA; /* not that we support it... */
  333. cs89_dbg(3, info, "set_dma_cfg(): AUTO_RX_DMA\n");
  334. }
  335. }
  336. }
  337. static int
  338. dma_bufcfg(struct net_device *dev)
  339. {
  340. struct net_local *lp = netdev_priv(dev);
  341. if (lp->use_dma)
  342. return (lp->isa_config & ANY_ISA_DMA) ? RX_DMA_ENBL : 0;
  343. else
  344. return 0;
  345. }
  346. static int
  347. dma_busctl(struct net_device *dev)
  348. {
  349. int retval = 0;
  350. struct net_local *lp = netdev_priv(dev);
  351. if (lp->use_dma) {
  352. if (lp->isa_config & ANY_ISA_DMA)
  353. retval |= RESET_RX_DMA; /* Reset the DMA pointer */
  354. if (lp->isa_config & DMA_BURST)
  355. retval |= DMA_BURST_MODE; /* Does ISA config specify DMA burst ? */
  356. if (lp->dmasize == 64)
  357. retval |= RX_DMA_SIZE_64K; /* did they ask for 64K? */
  358. retval |= MEMORY_ON; /* we need memory enabled to use DMA. */
  359. }
  360. return retval;
  361. }
  362. static void
  363. dma_rx(struct net_device *dev)
  364. {
  365. struct net_local *lp = netdev_priv(dev);
  366. struct sk_buff *skb;
  367. int status, length;
  368. unsigned char *bp = lp->rx_dma_ptr;
  369. status = bp[0] + (bp[1] << 8);
  370. length = bp[2] + (bp[3] << 8);
  371. bp += 4;
  372. cs89_dbg(5, debug, "%s: receiving DMA packet at %lx, status %x, length %x\n",
  373. dev->name, (unsigned long)bp, status, length);
  374. if ((status & RX_OK) == 0) {
  375. count_rx_errors(status, dev);
  376. goto skip_this_frame;
  377. }
  378. /* Malloc up new buffer. */
  379. skb = netdev_alloc_skb(dev, length + 2);
  380. if (skb == NULL) {
  381. dev->stats.rx_dropped++;
  382. /* AKPM: advance bp to the next frame */
  383. skip_this_frame:
  384. bp += (length + 3) & ~3;
  385. if (bp >= lp->end_dma_buff)
  386. bp -= lp->dmasize * 1024;
  387. lp->rx_dma_ptr = bp;
  388. return;
  389. }
  390. skb_reserve(skb, 2); /* longword align L3 header */
  391. if (bp + length > lp->end_dma_buff) {
  392. int semi_cnt = lp->end_dma_buff - bp;
  393. skb_put_data(skb, bp, semi_cnt);
  394. skb_put_data(skb, lp->dma_buff, length - semi_cnt);
  395. } else {
  396. skb_put_data(skb, bp, length);
  397. }
  398. bp += (length + 3) & ~3;
  399. if (bp >= lp->end_dma_buff)
  400. bp -= lp->dmasize*1024;
  401. lp->rx_dma_ptr = bp;
  402. cs89_dbg(3, info, "%s: received %d byte DMA packet of type %x\n",
  403. dev->name, length,
  404. ((skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  405. skb->data[ETH_ALEN + ETH_ALEN + 1]));
  406. skb->protocol = eth_type_trans(skb, dev);
  407. netif_rx(skb);
  408. dev->stats.rx_packets++;
  409. dev->stats.rx_bytes += length;
  410. }
  411. static void release_dma_buff(struct net_local *lp)
  412. {
  413. if (lp->dma_buff) {
  414. free_pages((unsigned long)(lp->dma_buff),
  415. get_order(lp->dmasize * 1024));
  416. lp->dma_buff = NULL;
  417. }
  418. }
  419. #endif /* ALLOW_DMA */
  420. static void
  421. control_dc_dc(struct net_device *dev, int on_not_off)
  422. {
  423. struct net_local *lp = netdev_priv(dev);
  424. unsigned int selfcontrol;
  425. unsigned long timenow = jiffies;
  426. /* control the DC to DC convertor in the SelfControl register.
  427. * Note: This is hooked up to a general purpose pin, might not
  428. * always be a DC to DC convertor.
  429. */
  430. selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
  431. if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
  432. selfcontrol |= HCB1;
  433. else
  434. selfcontrol &= ~HCB1;
  435. writereg(dev, PP_SelfCTL, selfcontrol);
  436. /* Wait for the DC/DC converter to power up - 500ms */
  437. while (time_before(jiffies, timenow + HZ))
  438. ;
  439. }
  440. /* send a test packet - return true if carrier bits are ok */
  441. static int
  442. send_test_pkt(struct net_device *dev)
  443. {
  444. struct net_local *lp = netdev_priv(dev);
  445. char test_packet[] = {
  446. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  447. 0, 46, /* A 46 in network order */
  448. 0, 0, /* DSAP=0 & SSAP=0 fields */
  449. 0xf3, 0 /* Control (Test Req + P bit set) */
  450. };
  451. unsigned long timenow = jiffies;
  452. writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
  453. memcpy(test_packet, dev->dev_addr, ETH_ALEN);
  454. memcpy(test_packet + ETH_ALEN, dev->dev_addr, ETH_ALEN);
  455. iowrite16(TX_AFTER_ALL, lp->virt_addr + TX_CMD_PORT);
  456. iowrite16(ETH_ZLEN, lp->virt_addr + TX_LEN_PORT);
  457. /* Test to see if the chip has allocated memory for the packet */
  458. while (time_before(jiffies, timenow + 5))
  459. if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
  460. break;
  461. if (time_after_eq(jiffies, timenow + 5))
  462. return 0; /* this shouldn't happen */
  463. /* Write the contents of the packet */
  464. writewords(lp, TX_FRAME_PORT, test_packet, (ETH_ZLEN + 1) >> 1);
  465. cs89_dbg(1, debug, "Sending test packet ");
  466. /* wait a couple of jiffies for packet to be received */
  467. for (timenow = jiffies; time_before(jiffies, timenow + 3);)
  468. ;
  469. if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
  470. cs89_dbg(1, cont, "succeeded\n");
  471. return 1;
  472. }
  473. cs89_dbg(1, cont, "failed\n");
  474. return 0;
  475. }
  476. #define DETECTED_NONE 0
  477. #define DETECTED_RJ45H 1
  478. #define DETECTED_RJ45F 2
  479. #define DETECTED_AUI 3
  480. #define DETECTED_BNC 4
  481. static int
  482. detect_tp(struct net_device *dev)
  483. {
  484. struct net_local *lp = netdev_priv(dev);
  485. unsigned long timenow = jiffies;
  486. int fdx;
  487. cs89_dbg(1, debug, "%s: Attempting TP\n", dev->name);
  488. /* If connected to another full duplex capable 10-Base-T card
  489. * the link pulses seem to be lost when the auto detect bit in
  490. * the LineCTL is set. To overcome this the auto detect bit will
  491. * be cleared whilst testing the 10-Base-T interface. This would
  492. * not be necessary for the sparrow chip but is simpler to do it
  493. * anyway.
  494. */
  495. writereg(dev, PP_LineCTL, lp->linectl & ~AUI_ONLY);
  496. control_dc_dc(dev, 0);
  497. /* Delay for the hardware to work out if the TP cable is present
  498. * - 150ms
  499. */
  500. for (timenow = jiffies; time_before(jiffies, timenow + 15);)
  501. ;
  502. if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
  503. return DETECTED_NONE;
  504. if (lp->chip_type == CS8900) {
  505. switch (lp->force & 0xf0) {
  506. #if 0
  507. case FORCE_AUTO:
  508. pr_info("%s: cs8900 doesn't autonegotiate\n",
  509. dev->name);
  510. return DETECTED_NONE;
  511. #endif
  512. /* CS8900 doesn't support AUTO, change to HALF*/
  513. case FORCE_AUTO:
  514. lp->force &= ~FORCE_AUTO;
  515. lp->force |= FORCE_HALF;
  516. break;
  517. case FORCE_HALF:
  518. break;
  519. case FORCE_FULL:
  520. writereg(dev, PP_TestCTL,
  521. readreg(dev, PP_TestCTL) | FDX_8900);
  522. break;
  523. }
  524. fdx = readreg(dev, PP_TestCTL) & FDX_8900;
  525. } else {
  526. switch (lp->force & 0xf0) {
  527. case FORCE_AUTO:
  528. lp->auto_neg_cnf = AUTO_NEG_ENABLE;
  529. break;
  530. case FORCE_HALF:
  531. lp->auto_neg_cnf = 0;
  532. break;
  533. case FORCE_FULL:
  534. lp->auto_neg_cnf = RE_NEG_NOW | ALLOW_FDX;
  535. break;
  536. }
  537. writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
  538. if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
  539. pr_info("%s: negotiating duplex...\n", dev->name);
  540. while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
  541. if (time_after(jiffies, timenow + 4000)) {
  542. pr_err("**** Full / half duplex auto-negotiation timed out ****\n");
  543. break;
  544. }
  545. }
  546. }
  547. fdx = readreg(dev, PP_AutoNegST) & FDX_ACTIVE;
  548. }
  549. if (fdx)
  550. return DETECTED_RJ45F;
  551. else
  552. return DETECTED_RJ45H;
  553. }
  554. static int
  555. detect_bnc(struct net_device *dev)
  556. {
  557. struct net_local *lp = netdev_priv(dev);
  558. cs89_dbg(1, debug, "%s: Attempting BNC\n", dev->name);
  559. control_dc_dc(dev, 1);
  560. writereg(dev, PP_LineCTL, (lp->linectl & ~AUTO_AUI_10BASET) | AUI_ONLY);
  561. if (send_test_pkt(dev))
  562. return DETECTED_BNC;
  563. else
  564. return DETECTED_NONE;
  565. }
  566. static int
  567. detect_aui(struct net_device *dev)
  568. {
  569. struct net_local *lp = netdev_priv(dev);
  570. cs89_dbg(1, debug, "%s: Attempting AUI\n", dev->name);
  571. control_dc_dc(dev, 0);
  572. writereg(dev, PP_LineCTL, (lp->linectl & ~AUTO_AUI_10BASET) | AUI_ONLY);
  573. if (send_test_pkt(dev))
  574. return DETECTED_AUI;
  575. else
  576. return DETECTED_NONE;
  577. }
  578. /* We have a good packet(s), get it/them out of the buffers. */
  579. static void
  580. net_rx(struct net_device *dev)
  581. {
  582. struct net_local *lp = netdev_priv(dev);
  583. struct sk_buff *skb;
  584. int status, length;
  585. status = ioread16(lp->virt_addr + RX_FRAME_PORT);
  586. length = ioread16(lp->virt_addr + RX_FRAME_PORT);
  587. if ((status & RX_OK) == 0) {
  588. count_rx_errors(status, dev);
  589. return;
  590. }
  591. /* Malloc up new buffer. */
  592. skb = netdev_alloc_skb(dev, length + 2);
  593. if (skb == NULL) {
  594. dev->stats.rx_dropped++;
  595. return;
  596. }
  597. skb_reserve(skb, 2); /* longword align L3 header */
  598. readwords(lp, RX_FRAME_PORT, skb_put(skb, length), length >> 1);
  599. if (length & 1)
  600. skb->data[length-1] = ioread16(lp->virt_addr + RX_FRAME_PORT);
  601. cs89_dbg(3, debug, "%s: received %d byte packet of type %x\n",
  602. dev->name, length,
  603. (skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  604. skb->data[ETH_ALEN + ETH_ALEN + 1]);
  605. skb->protocol = eth_type_trans(skb, dev);
  606. netif_rx(skb);
  607. dev->stats.rx_packets++;
  608. dev->stats.rx_bytes += length;
  609. }
  610. /* The typical workload of the driver:
  611. * Handle the network interface interrupts.
  612. */
  613. static irqreturn_t net_interrupt(int irq, void *dev_id)
  614. {
  615. struct net_device *dev = dev_id;
  616. struct net_local *lp;
  617. int status;
  618. int handled = 0;
  619. lp = netdev_priv(dev);
  620. /* we MUST read all the events out of the ISQ, otherwise we'll never
  621. * get interrupted again. As a consequence, we can't have any limit
  622. * on the number of times we loop in the interrupt handler. The
  623. * hardware guarantees that eventually we'll run out of events. Of
  624. * course, if you're on a slow machine, and packets are arriving
  625. * faster than you can read them off, you're screwed. Hasta la
  626. * vista, baby!
  627. */
  628. while ((status = ioread16(lp->virt_addr + ISQ_PORT))) {
  629. cs89_dbg(4, debug, "%s: event=%04x\n", dev->name, status);
  630. handled = 1;
  631. switch (status & ISQ_EVENT_MASK) {
  632. case ISQ_RECEIVER_EVENT:
  633. /* Got a packet(s). */
  634. net_rx(dev);
  635. break;
  636. case ISQ_TRANSMITTER_EVENT:
  637. dev->stats.tx_packets++;
  638. netif_wake_queue(dev); /* Inform upper layers. */
  639. if ((status & (TX_OK |
  640. TX_LOST_CRS |
  641. TX_SQE_ERROR |
  642. TX_LATE_COL |
  643. TX_16_COL)) != TX_OK) {
  644. if ((status & TX_OK) == 0)
  645. dev->stats.tx_errors++;
  646. if (status & TX_LOST_CRS)
  647. dev->stats.tx_carrier_errors++;
  648. if (status & TX_SQE_ERROR)
  649. dev->stats.tx_heartbeat_errors++;
  650. if (status & TX_LATE_COL)
  651. dev->stats.tx_window_errors++;
  652. if (status & TX_16_COL)
  653. dev->stats.tx_aborted_errors++;
  654. }
  655. break;
  656. case ISQ_BUFFER_EVENT:
  657. if (status & READY_FOR_TX) {
  658. /* we tried to transmit a packet earlier,
  659. * but inexplicably ran out of buffers.
  660. * That shouldn't happen since we only ever
  661. * load one packet. Shrug. Do the right
  662. * thing anyway.
  663. */
  664. netif_wake_queue(dev); /* Inform upper layers. */
  665. }
  666. if (status & TX_UNDERRUN) {
  667. cs89_dbg(0, err, "%s: transmit underrun\n",
  668. dev->name);
  669. lp->send_underrun++;
  670. if (lp->send_underrun == 3)
  671. lp->send_cmd = TX_AFTER_381;
  672. else if (lp->send_underrun == 6)
  673. lp->send_cmd = TX_AFTER_ALL;
  674. /* transmit cycle is done, although
  675. * frame wasn't transmitted - this
  676. * avoids having to wait for the upper
  677. * layers to timeout on us, in the
  678. * event of a tx underrun
  679. */
  680. netif_wake_queue(dev); /* Inform upper layers. */
  681. }
  682. #if ALLOW_DMA
  683. if (lp->use_dma && (status & RX_DMA)) {
  684. int count = readreg(dev, PP_DmaFrameCnt);
  685. while (count) {
  686. cs89_dbg(5, debug,
  687. "%s: receiving %d DMA frames\n",
  688. dev->name, count);
  689. if (count > 1)
  690. cs89_dbg(2, debug,
  691. "%s: receiving %d DMA frames\n",
  692. dev->name, count);
  693. dma_rx(dev);
  694. if (--count == 0)
  695. count = readreg(dev, PP_DmaFrameCnt);
  696. if (count > 0)
  697. cs89_dbg(2, debug,
  698. "%s: continuing with %d DMA frames\n",
  699. dev->name, count);
  700. }
  701. }
  702. #endif
  703. break;
  704. case ISQ_RX_MISS_EVENT:
  705. dev->stats.rx_missed_errors += (status >> 6);
  706. break;
  707. case ISQ_TX_COL_EVENT:
  708. dev->stats.collisions += (status >> 6);
  709. break;
  710. }
  711. }
  712. return IRQ_RETVAL(handled);
  713. }
  714. /* Open/initialize the board. This is called (in the current kernel)
  715. sometime after booting when the 'ifconfig' program is run.
  716. This routine should set everything up anew at each open, even
  717. registers that "should" only need to be set once at boot, so that
  718. there is non-reboot way to recover if something goes wrong.
  719. */
  720. /* AKPM: do we need to do any locking here? */
  721. static int
  722. net_open(struct net_device *dev)
  723. {
  724. struct net_local *lp = netdev_priv(dev);
  725. int result = 0;
  726. int i;
  727. int ret;
  728. if (dev->irq < 2) {
  729. /* Allow interrupts to be generated by the chip */
  730. /* Cirrus' release had this: */
  731. #if 0
  732. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
  733. #endif
  734. /* And 2.3.47 had this: */
  735. writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
  736. for (i = 2; i < CS8920_NO_INTS; i++) {
  737. if ((1 << i) & lp->irq_map) {
  738. if (request_irq(i, net_interrupt, 0, dev->name,
  739. dev) == 0) {
  740. dev->irq = i;
  741. write_irq(dev, lp->chip_type, i);
  742. /* writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT); */
  743. break;
  744. }
  745. }
  746. }
  747. if (i >= CS8920_NO_INTS) {
  748. writereg(dev, PP_BusCTL, 0); /* disable interrupts. */
  749. pr_err("can't get an interrupt\n");
  750. ret = -EAGAIN;
  751. goto bad_out;
  752. }
  753. } else {
  754. #if !defined(CONFIG_CS89x0_PLATFORM)
  755. if (((1 << dev->irq) & lp->irq_map) == 0) {
  756. pr_err("%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
  757. dev->name, dev->irq, lp->irq_map);
  758. ret = -EAGAIN;
  759. goto bad_out;
  760. }
  761. #endif
  762. /* FIXME: Cirrus' release had this: */
  763. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL)|ENABLE_IRQ);
  764. /* And 2.3.47 had this: */
  765. #if 0
  766. writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
  767. #endif
  768. write_irq(dev, lp->chip_type, dev->irq);
  769. ret = request_irq(dev->irq, net_interrupt, 0, dev->name, dev);
  770. if (ret) {
  771. pr_err("request_irq(%d) failed\n", dev->irq);
  772. goto bad_out;
  773. }
  774. }
  775. #if ALLOW_DMA
  776. if (lp->use_dma && (lp->isa_config & ANY_ISA_DMA)) {
  777. unsigned long flags;
  778. lp->dma_buff = (unsigned char *)__get_dma_pages(GFP_KERNEL,
  779. get_order(lp->dmasize * 1024));
  780. if (!lp->dma_buff) {
  781. pr_err("%s: cannot get %dK memory for DMA\n",
  782. dev->name, lp->dmasize);
  783. goto release_irq;
  784. }
  785. cs89_dbg(1, debug, "%s: dma %lx %lx\n",
  786. dev->name,
  787. (unsigned long)lp->dma_buff,
  788. (unsigned long)isa_virt_to_bus(lp->dma_buff));
  789. if ((unsigned long)lp->dma_buff >= MAX_DMA_ADDRESS ||
  790. !dma_page_eq(lp->dma_buff,
  791. lp->dma_buff + lp->dmasize * 1024 - 1)) {
  792. pr_err("%s: not usable as DMA buffer\n", dev->name);
  793. goto release_irq;
  794. }
  795. memset(lp->dma_buff, 0, lp->dmasize * 1024); /* Why? */
  796. if (request_dma(dev->dma, dev->name)) {
  797. pr_err("%s: cannot get dma channel %d\n",
  798. dev->name, dev->dma);
  799. goto release_irq;
  800. }
  801. write_dma(dev, lp->chip_type, dev->dma);
  802. lp->rx_dma_ptr = lp->dma_buff;
  803. lp->end_dma_buff = lp->dma_buff + lp->dmasize * 1024;
  804. spin_lock_irqsave(&lp->lock, flags);
  805. disable_dma(dev->dma);
  806. clear_dma_ff(dev->dma);
  807. set_dma_mode(dev->dma, DMA_RX_MODE); /* auto_init as well */
  808. set_dma_addr(dev->dma, isa_virt_to_bus(lp->dma_buff));
  809. set_dma_count(dev->dma, lp->dmasize * 1024);
  810. enable_dma(dev->dma);
  811. spin_unlock_irqrestore(&lp->lock, flags);
  812. }
  813. #endif /* ALLOW_DMA */
  814. /* set the Ethernet address */
  815. for (i = 0; i < ETH_ALEN / 2; i++)
  816. writereg(dev, PP_IA + i * 2,
  817. (dev->dev_addr[i * 2] |
  818. (dev->dev_addr[i * 2 + 1] << 8)));
  819. /* while we're testing the interface, leave interrupts disabled */
  820. writereg(dev, PP_BusCTL, MEMORY_ON);
  821. /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
  822. if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) &&
  823. (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
  824. lp->linectl = LOW_RX_SQUELCH;
  825. else
  826. lp->linectl = 0;
  827. /* check to make sure that they have the "right" hardware available */
  828. switch (lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
  829. case A_CNF_MEDIA_10B_T:
  830. result = lp->adapter_cnf & A_CNF_10B_T;
  831. break;
  832. case A_CNF_MEDIA_AUI:
  833. result = lp->adapter_cnf & A_CNF_AUI;
  834. break;
  835. case A_CNF_MEDIA_10B_2:
  836. result = lp->adapter_cnf & A_CNF_10B_2;
  837. break;
  838. default:
  839. result = lp->adapter_cnf & (A_CNF_10B_T |
  840. A_CNF_AUI |
  841. A_CNF_10B_2);
  842. }
  843. if (!result) {
  844. pr_err("%s: EEPROM is configured for unavailable media\n",
  845. dev->name);
  846. release_dma:
  847. #if ALLOW_DMA
  848. free_dma(dev->dma);
  849. release_irq:
  850. release_dma_buff(lp);
  851. #endif
  852. writereg(dev, PP_LineCTL,
  853. readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
  854. free_irq(dev->irq, dev);
  855. ret = -EAGAIN;
  856. goto bad_out;
  857. }
  858. /* set the hardware to the configured choice */
  859. switch (lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
  860. case A_CNF_MEDIA_10B_T:
  861. result = detect_tp(dev);
  862. if (result == DETECTED_NONE) {
  863. pr_warn("%s: 10Base-T (RJ-45) has no cable\n",
  864. dev->name);
  865. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  866. result = DETECTED_RJ45H; /* Yes! I don't care if I see a link pulse */
  867. }
  868. break;
  869. case A_CNF_MEDIA_AUI:
  870. result = detect_aui(dev);
  871. if (result == DETECTED_NONE) {
  872. pr_warn("%s: 10Base-5 (AUI) has no cable\n", dev->name);
  873. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  874. result = DETECTED_AUI; /* Yes! I don't care if I see a carrrier */
  875. }
  876. break;
  877. case A_CNF_MEDIA_10B_2:
  878. result = detect_bnc(dev);
  879. if (result == DETECTED_NONE) {
  880. pr_warn("%s: 10Base-2 (BNC) has no cable\n", dev->name);
  881. if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
  882. result = DETECTED_BNC; /* Yes! I don't care if I can xmit a packet */
  883. }
  884. break;
  885. case A_CNF_MEDIA_AUTO:
  886. writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
  887. if (lp->adapter_cnf & A_CNF_10B_T) {
  888. result = detect_tp(dev);
  889. if (result != DETECTED_NONE)
  890. break;
  891. }
  892. if (lp->adapter_cnf & A_CNF_AUI) {
  893. result = detect_aui(dev);
  894. if (result != DETECTED_NONE)
  895. break;
  896. }
  897. if (lp->adapter_cnf & A_CNF_10B_2) {
  898. result = detect_bnc(dev);
  899. if (result != DETECTED_NONE)
  900. break;
  901. }
  902. pr_err("%s: no media detected\n", dev->name);
  903. goto release_dma;
  904. }
  905. switch (result) {
  906. case DETECTED_NONE:
  907. pr_err("%s: no network cable attached to configured media\n",
  908. dev->name);
  909. goto release_dma;
  910. case DETECTED_RJ45H:
  911. pr_info("%s: using half-duplex 10Base-T (RJ-45)\n", dev->name);
  912. break;
  913. case DETECTED_RJ45F:
  914. pr_info("%s: using full-duplex 10Base-T (RJ-45)\n", dev->name);
  915. break;
  916. case DETECTED_AUI:
  917. pr_info("%s: using 10Base-5 (AUI)\n", dev->name);
  918. break;
  919. case DETECTED_BNC:
  920. pr_info("%s: using 10Base-2 (BNC)\n", dev->name);
  921. break;
  922. }
  923. /* Turn on both receive and transmit operations */
  924. writereg(dev, PP_LineCTL,
  925. readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
  926. /* Receive only error free packets addressed to this card */
  927. lp->rx_mode = 0;
  928. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
  929. lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
  930. if (lp->isa_config & STREAM_TRANSFER)
  931. lp->curr_rx_cfg |= RX_STREAM_ENBL;
  932. #if ALLOW_DMA
  933. set_dma_cfg(dev);
  934. #endif
  935. writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
  936. writereg(dev, PP_TxCFG, (TX_LOST_CRS_ENBL |
  937. TX_SQE_ERROR_ENBL |
  938. TX_OK_ENBL |
  939. TX_LATE_COL_ENBL |
  940. TX_JBR_ENBL |
  941. TX_ANY_COL_ENBL |
  942. TX_16_COL_ENBL));
  943. writereg(dev, PP_BufCFG, (READY_FOR_TX_ENBL |
  944. RX_MISS_COUNT_OVRFLOW_ENBL |
  945. #if ALLOW_DMA
  946. dma_bufcfg(dev) |
  947. #endif
  948. TX_COL_COUNT_OVRFLOW_ENBL |
  949. TX_UNDERRUN_ENBL));
  950. /* now that we've got our act together, enable everything */
  951. writereg(dev, PP_BusCTL, (ENABLE_IRQ
  952. | (dev->mem_start ? MEMORY_ON : 0) /* turn memory on */
  953. #if ALLOW_DMA
  954. | dma_busctl(dev)
  955. #endif
  956. ));
  957. netif_start_queue(dev);
  958. cs89_dbg(1, debug, "net_open() succeeded\n");
  959. return 0;
  960. bad_out:
  961. return ret;
  962. }
  963. /* The inverse routine to net_open(). */
  964. static int
  965. net_close(struct net_device *dev)
  966. {
  967. #if ALLOW_DMA
  968. struct net_local *lp = netdev_priv(dev);
  969. #endif
  970. netif_stop_queue(dev);
  971. writereg(dev, PP_RxCFG, 0);
  972. writereg(dev, PP_TxCFG, 0);
  973. writereg(dev, PP_BufCFG, 0);
  974. writereg(dev, PP_BusCTL, 0);
  975. free_irq(dev->irq, dev);
  976. #if ALLOW_DMA
  977. if (lp->use_dma && lp->dma) {
  978. free_dma(dev->dma);
  979. release_dma_buff(lp);
  980. }
  981. #endif
  982. /* Update the statistics here. */
  983. return 0;
  984. }
  985. /* Get the current statistics.
  986. * This may be called with the card open or closed.
  987. */
  988. static struct net_device_stats *
  989. net_get_stats(struct net_device *dev)
  990. {
  991. struct net_local *lp = netdev_priv(dev);
  992. unsigned long flags;
  993. spin_lock_irqsave(&lp->lock, flags);
  994. /* Update the statistics from the device registers. */
  995. dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
  996. dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
  997. spin_unlock_irqrestore(&lp->lock, flags);
  998. return &dev->stats;
  999. }
  1000. static void net_timeout(struct net_device *dev)
  1001. {
  1002. /* If we get here, some higher level has decided we are broken.
  1003. There should really be a "kick me" function call instead. */
  1004. cs89_dbg(0, err, "%s: transmit timed out, %s?\n",
  1005. dev->name,
  1006. tx_done(dev) ? "IRQ conflict" : "network cable problem");
  1007. /* Try to restart the adaptor. */
  1008. netif_wake_queue(dev);
  1009. }
  1010. static netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev)
  1011. {
  1012. struct net_local *lp = netdev_priv(dev);
  1013. unsigned long flags;
  1014. cs89_dbg(3, debug, "%s: sent %d byte packet of type %x\n",
  1015. dev->name, skb->len,
  1016. ((skb->data[ETH_ALEN + ETH_ALEN] << 8) |
  1017. skb->data[ETH_ALEN + ETH_ALEN + 1]));
  1018. /* keep the upload from being interrupted, since we
  1019. * ask the chip to start transmitting before the
  1020. * whole packet has been completely uploaded.
  1021. */
  1022. spin_lock_irqsave(&lp->lock, flags);
  1023. netif_stop_queue(dev);
  1024. /* initiate a transmit sequence */
  1025. iowrite16(lp->send_cmd, lp->virt_addr + TX_CMD_PORT);
  1026. iowrite16(skb->len, lp->virt_addr + TX_LEN_PORT);
  1027. /* Test to see if the chip has allocated memory for the packet */
  1028. if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
  1029. /* Gasp! It hasn't. But that shouldn't happen since
  1030. * we're waiting for TxOk, so return 1 and requeue this packet.
  1031. */
  1032. spin_unlock_irqrestore(&lp->lock, flags);
  1033. cs89_dbg(0, err, "Tx buffer not free!\n");
  1034. return NETDEV_TX_BUSY;
  1035. }
  1036. /* Write the contents of the packet */
  1037. writewords(lp, TX_FRAME_PORT, skb->data, (skb->len + 1) >> 1);
  1038. spin_unlock_irqrestore(&lp->lock, flags);
  1039. dev->stats.tx_bytes += skb->len;
  1040. dev_consume_skb_any(skb);
  1041. /* We DO NOT call netif_wake_queue() here.
  1042. * We also DO NOT call netif_start_queue().
  1043. *
  1044. * Either of these would cause another bottom half run through
  1045. * net_send_packet() before this packet has fully gone out.
  1046. * That causes us to hit the "Gasp!" above and the send is rescheduled.
  1047. * it runs like a dog. We just return and wait for the Tx completion
  1048. * interrupt handler to restart the netdevice layer
  1049. */
  1050. return NETDEV_TX_OK;
  1051. }
  1052. static void set_multicast_list(struct net_device *dev)
  1053. {
  1054. struct net_local *lp = netdev_priv(dev);
  1055. unsigned long flags;
  1056. u16 cfg;
  1057. spin_lock_irqsave(&lp->lock, flags);
  1058. if (dev->flags & IFF_PROMISC)
  1059. lp->rx_mode = RX_ALL_ACCEPT;
  1060. else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
  1061. /* The multicast-accept list is initialized to accept-all,
  1062. * and we rely on higher-level filtering for now.
  1063. */
  1064. lp->rx_mode = RX_MULTCAST_ACCEPT;
  1065. else
  1066. lp->rx_mode = 0;
  1067. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
  1068. /* in promiscuous mode, we accept errored packets,
  1069. * so we have to enable interrupts on them also
  1070. */
  1071. cfg = lp->curr_rx_cfg;
  1072. if (lp->rx_mode == RX_ALL_ACCEPT)
  1073. cfg |= RX_CRC_ERROR_ENBL | RX_RUNT_ENBL | RX_EXTRA_DATA_ENBL;
  1074. writereg(dev, PP_RxCFG, cfg);
  1075. spin_unlock_irqrestore(&lp->lock, flags);
  1076. }
  1077. static int set_mac_address(struct net_device *dev, void *p)
  1078. {
  1079. int i;
  1080. struct sockaddr *addr = p;
  1081. if (netif_running(dev))
  1082. return -EBUSY;
  1083. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1084. cs89_dbg(0, debug, "%s: Setting MAC address to %pM\n",
  1085. dev->name, dev->dev_addr);
  1086. /* set the Ethernet address */
  1087. for (i = 0; i < ETH_ALEN / 2; i++)
  1088. writereg(dev, PP_IA + i * 2,
  1089. (dev->dev_addr[i * 2] |
  1090. (dev->dev_addr[i * 2 + 1] << 8)));
  1091. return 0;
  1092. }
  1093. #ifdef CONFIG_NET_POLL_CONTROLLER
  1094. /*
  1095. * Polling receive - used by netconsole and other diagnostic tools
  1096. * to allow network i/o with interrupts disabled.
  1097. */
  1098. static void net_poll_controller(struct net_device *dev)
  1099. {
  1100. disable_irq(dev->irq);
  1101. net_interrupt(dev->irq, dev);
  1102. enable_irq(dev->irq);
  1103. }
  1104. #endif
  1105. static const struct net_device_ops net_ops = {
  1106. .ndo_open = net_open,
  1107. .ndo_stop = net_close,
  1108. .ndo_tx_timeout = net_timeout,
  1109. .ndo_start_xmit = net_send_packet,
  1110. .ndo_get_stats = net_get_stats,
  1111. .ndo_set_rx_mode = set_multicast_list,
  1112. .ndo_set_mac_address = set_mac_address,
  1113. #ifdef CONFIG_NET_POLL_CONTROLLER
  1114. .ndo_poll_controller = net_poll_controller,
  1115. #endif
  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_hw(io, int, ioport, 0);
  1491. module_param_hw(irq, int, irq, 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_hw(dma , int, dma, 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 const struct of_device_id __maybe_unused cs89x0_match[] = {
  1658. { .compatible = "cirrus,cs8900", },
  1659. { .compatible = "cirrus,cs8920", },
  1660. { },
  1661. };
  1662. MODULE_DEVICE_TABLE(of, cs89x0_match);
  1663. static struct platform_driver cs89x0_driver = {
  1664. .driver = {
  1665. .name = DRV_NAME,
  1666. .of_match_table = of_match_ptr(cs89x0_match),
  1667. },
  1668. .remove = cs89x0_platform_remove,
  1669. };
  1670. module_platform_driver_probe(cs89x0_driver, cs89x0_platform_probe);
  1671. #endif /* CONFIG_CS89x0_PLATFORM */
  1672. MODULE_LICENSE("GPL");
  1673. MODULE_DESCRIPTION("Crystal Semiconductor (Now Cirrus Logic) CS89[02]0 network driver");
  1674. MODULE_AUTHOR("Russell Nelson <nelson@crynwr.com>");