z85230.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version
  5. * 2 of the License, or (at your option) any later version.
  6. *
  7. * (c) Copyright 1998 Alan Cox <alan@lxorguk.ukuu.org.uk>
  8. * (c) Copyright 2000, 2001 Red Hat Inc
  9. *
  10. * Development of this driver was funded by Equiinet Ltd
  11. * http://www.equiinet.com
  12. *
  13. * ChangeLog:
  14. *
  15. * Asynchronous mode dropped for 2.2. For 2.5 we will attempt the
  16. * unification of all the Z85x30 asynchronous drivers for real.
  17. *
  18. * DMA now uses get_free_page as kmalloc buffers may span a 64K
  19. * boundary.
  20. *
  21. * Modified for SMP safety and SMP locking by Alan Cox
  22. * <alan@lxorguk.ukuu.org.uk>
  23. *
  24. * Performance
  25. *
  26. * Z85230:
  27. * Non DMA you want a 486DX50 or better to do 64Kbits. 9600 baud
  28. * X.25 is not unrealistic on all machines. DMA mode can in theory
  29. * handle T1/E1 quite nicely. In practice the limit seems to be about
  30. * 512Kbit->1Mbit depending on motherboard.
  31. *
  32. * Z85C30:
  33. * 64K will take DMA, 9600 baud X.25 should be ok.
  34. *
  35. * Z8530:
  36. * Synchronous mode without DMA is unlikely to pass about 2400 baud.
  37. */
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/mm.h>
  41. #include <linux/net.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/if_arp.h>
  45. #include <linux/delay.h>
  46. #include <linux/hdlc.h>
  47. #include <linux/ioport.h>
  48. #include <linux/init.h>
  49. #include <asm/dma.h>
  50. #include <asm/io.h>
  51. #define RT_LOCK
  52. #define RT_UNLOCK
  53. #include <linux/spinlock.h>
  54. #include "z85230.h"
  55. /**
  56. * z8530_read_port - Architecture specific interface function
  57. * @p: port to read
  58. *
  59. * Provided port access methods. The Comtrol SV11 requires no delays
  60. * between accesses and uses PC I/O. Some drivers may need a 5uS delay
  61. *
  62. * In the longer term this should become an architecture specific
  63. * section so that this can become a generic driver interface for all
  64. * platforms. For now we only handle PC I/O ports with or without the
  65. * dread 5uS sanity delay.
  66. *
  67. * The caller must hold sufficient locks to avoid violating the horrible
  68. * 5uS delay rule.
  69. */
  70. static inline int z8530_read_port(unsigned long p)
  71. {
  72. u8 r=inb(Z8530_PORT_OF(p));
  73. if(p&Z8530_PORT_SLEEP) /* gcc should figure this out efficiently ! */
  74. udelay(5);
  75. return r;
  76. }
  77. /**
  78. * z8530_write_port - Architecture specific interface function
  79. * @p: port to write
  80. * @d: value to write
  81. *
  82. * Write a value to a port with delays if need be. Note that the
  83. * caller must hold locks to avoid read/writes from other contexts
  84. * violating the 5uS rule
  85. *
  86. * In the longer term this should become an architecture specific
  87. * section so that this can become a generic driver interface for all
  88. * platforms. For now we only handle PC I/O ports with or without the
  89. * dread 5uS sanity delay.
  90. */
  91. static inline void z8530_write_port(unsigned long p, u8 d)
  92. {
  93. outb(d,Z8530_PORT_OF(p));
  94. if(p&Z8530_PORT_SLEEP)
  95. udelay(5);
  96. }
  97. static void z8530_rx_done(struct z8530_channel *c);
  98. static void z8530_tx_done(struct z8530_channel *c);
  99. /**
  100. * read_zsreg - Read a register from a Z85230
  101. * @c: Z8530 channel to read from (2 per chip)
  102. * @reg: Register to read
  103. * FIXME: Use a spinlock.
  104. *
  105. * Most of the Z8530 registers are indexed off the control registers.
  106. * A read is done by writing to the control register and reading the
  107. * register back. The caller must hold the lock
  108. */
  109. static inline u8 read_zsreg(struct z8530_channel *c, u8 reg)
  110. {
  111. if(reg)
  112. z8530_write_port(c->ctrlio, reg);
  113. return z8530_read_port(c->ctrlio);
  114. }
  115. /**
  116. * read_zsdata - Read the data port of a Z8530 channel
  117. * @c: The Z8530 channel to read the data port from
  118. *
  119. * The data port provides fast access to some things. We still
  120. * have all the 5uS delays to worry about.
  121. */
  122. static inline u8 read_zsdata(struct z8530_channel *c)
  123. {
  124. u8 r;
  125. r=z8530_read_port(c->dataio);
  126. return r;
  127. }
  128. /**
  129. * write_zsreg - Write to a Z8530 channel register
  130. * @c: The Z8530 channel
  131. * @reg: Register number
  132. * @val: Value to write
  133. *
  134. * Write a value to an indexed register. The caller must hold the lock
  135. * to honour the irritating delay rules. We know about register 0
  136. * being fast to access.
  137. *
  138. * Assumes c->lock is held.
  139. */
  140. static inline void write_zsreg(struct z8530_channel *c, u8 reg, u8 val)
  141. {
  142. if(reg)
  143. z8530_write_port(c->ctrlio, reg);
  144. z8530_write_port(c->ctrlio, val);
  145. }
  146. /**
  147. * write_zsctrl - Write to a Z8530 control register
  148. * @c: The Z8530 channel
  149. * @val: Value to write
  150. *
  151. * Write directly to the control register on the Z8530
  152. */
  153. static inline void write_zsctrl(struct z8530_channel *c, u8 val)
  154. {
  155. z8530_write_port(c->ctrlio, val);
  156. }
  157. /**
  158. * write_zsdata - Write to a Z8530 control register
  159. * @c: The Z8530 channel
  160. * @val: Value to write
  161. *
  162. * Write directly to the data register on the Z8530
  163. */
  164. static inline void write_zsdata(struct z8530_channel *c, u8 val)
  165. {
  166. z8530_write_port(c->dataio, val);
  167. }
  168. /*
  169. * Register loading parameters for a dead port
  170. */
  171. u8 z8530_dead_port[]=
  172. {
  173. 255
  174. };
  175. EXPORT_SYMBOL(z8530_dead_port);
  176. /*
  177. * Register loading parameters for currently supported circuit types
  178. */
  179. /*
  180. * Data clocked by telco end. This is the correct data for the UK
  181. * "kilostream" service, and most other similar services.
  182. */
  183. u8 z8530_hdlc_kilostream[]=
  184. {
  185. 4, SYNC_ENAB|SDLC|X1CLK,
  186. 2, 0, /* No vector */
  187. 1, 0,
  188. 3, ENT_HM|RxCRC_ENAB|Rx8,
  189. 5, TxCRC_ENAB|RTS|TxENAB|Tx8|DTR,
  190. 9, 0, /* Disable interrupts */
  191. 6, 0xFF,
  192. 7, FLAG,
  193. 10, ABUNDER|NRZ|CRCPS,/*MARKIDLE ??*/
  194. 11, TCTRxCP,
  195. 14, DISDPLL,
  196. 15, DCDIE|SYNCIE|CTSIE|TxUIE|BRKIE,
  197. 1, EXT_INT_ENAB|TxINT_ENAB|INT_ALL_Rx,
  198. 9, NV|MIE|NORESET,
  199. 255
  200. };
  201. EXPORT_SYMBOL(z8530_hdlc_kilostream);
  202. /*
  203. * As above but for enhanced chips.
  204. */
  205. u8 z8530_hdlc_kilostream_85230[]=
  206. {
  207. 4, SYNC_ENAB|SDLC|X1CLK,
  208. 2, 0, /* No vector */
  209. 1, 0,
  210. 3, ENT_HM|RxCRC_ENAB|Rx8,
  211. 5, TxCRC_ENAB|RTS|TxENAB|Tx8|DTR,
  212. 9, 0, /* Disable interrupts */
  213. 6, 0xFF,
  214. 7, FLAG,
  215. 10, ABUNDER|NRZ|CRCPS, /* MARKIDLE?? */
  216. 11, TCTRxCP,
  217. 14, DISDPLL,
  218. 15, DCDIE|SYNCIE|CTSIE|TxUIE|BRKIE,
  219. 1, EXT_INT_ENAB|TxINT_ENAB|INT_ALL_Rx,
  220. 9, NV|MIE|NORESET,
  221. 23, 3, /* Extended mode AUTO TX and EOM*/
  222. 255
  223. };
  224. EXPORT_SYMBOL(z8530_hdlc_kilostream_85230);
  225. /**
  226. * z8530_flush_fifo - Flush on chip RX FIFO
  227. * @c: Channel to flush
  228. *
  229. * Flush the receive FIFO. There is no specific option for this, we
  230. * blindly read bytes and discard them. Reading when there is no data
  231. * is harmless. The 8530 has a 4 byte FIFO, the 85230 has 8 bytes.
  232. *
  233. * All locking is handled for the caller. On return data may still be
  234. * present if it arrived during the flush.
  235. */
  236. static void z8530_flush_fifo(struct z8530_channel *c)
  237. {
  238. read_zsreg(c, R1);
  239. read_zsreg(c, R1);
  240. read_zsreg(c, R1);
  241. read_zsreg(c, R1);
  242. if(c->dev->type==Z85230)
  243. {
  244. read_zsreg(c, R1);
  245. read_zsreg(c, R1);
  246. read_zsreg(c, R1);
  247. read_zsreg(c, R1);
  248. }
  249. }
  250. /**
  251. * z8530_rtsdtr - Control the outgoing DTS/RTS line
  252. * @c: The Z8530 channel to control;
  253. * @set: 1 to set, 0 to clear
  254. *
  255. * Sets or clears DTR/RTS on the requested line. All locking is handled
  256. * by the caller. For now we assume all boards use the actual RTS/DTR
  257. * on the chip. Apparently one or two don't. We'll scream about them
  258. * later.
  259. */
  260. static void z8530_rtsdtr(struct z8530_channel *c, int set)
  261. {
  262. if (set)
  263. c->regs[5] |= (RTS | DTR);
  264. else
  265. c->regs[5] &= ~(RTS | DTR);
  266. write_zsreg(c, R5, c->regs[5]);
  267. }
  268. /**
  269. * z8530_rx - Handle a PIO receive event
  270. * @c: Z8530 channel to process
  271. *
  272. * Receive handler for receiving in PIO mode. This is much like the
  273. * async one but not quite the same or as complex
  274. *
  275. * Note: Its intended that this handler can easily be separated from
  276. * the main code to run realtime. That'll be needed for some machines
  277. * (eg to ever clock 64kbits on a sparc ;)).
  278. *
  279. * The RT_LOCK macros don't do anything now. Keep the code covered
  280. * by them as short as possible in all circumstances - clocks cost
  281. * baud. The interrupt handler is assumed to be atomic w.r.t. to
  282. * other code - this is true in the RT case too.
  283. *
  284. * We only cover the sync cases for this. If you want 2Mbit async
  285. * do it yourself but consider medical assistance first. This non DMA
  286. * synchronous mode is portable code. The DMA mode assumes PCI like
  287. * ISA DMA
  288. *
  289. * Called with the device lock held
  290. */
  291. static void z8530_rx(struct z8530_channel *c)
  292. {
  293. u8 ch,stat;
  294. while(1)
  295. {
  296. /* FIFO empty ? */
  297. if(!(read_zsreg(c, R0)&1))
  298. break;
  299. ch=read_zsdata(c);
  300. stat=read_zsreg(c, R1);
  301. /*
  302. * Overrun ?
  303. */
  304. if(c->count < c->max)
  305. {
  306. *c->dptr++=ch;
  307. c->count++;
  308. }
  309. if(stat&END_FR)
  310. {
  311. /*
  312. * Error ?
  313. */
  314. if(stat&(Rx_OVR|CRC_ERR))
  315. {
  316. /* Rewind the buffer and return */
  317. if(c->skb)
  318. c->dptr=c->skb->data;
  319. c->count=0;
  320. if(stat&Rx_OVR)
  321. {
  322. printk(KERN_WARNING "%s: overrun\n", c->dev->name);
  323. c->rx_overrun++;
  324. }
  325. if(stat&CRC_ERR)
  326. {
  327. c->rx_crc_err++;
  328. /* printk("crc error\n"); */
  329. }
  330. /* Shove the frame upstream */
  331. }
  332. else
  333. {
  334. /*
  335. * Drop the lock for RX processing, or
  336. * there are deadlocks
  337. */
  338. z8530_rx_done(c);
  339. write_zsctrl(c, RES_Rx_CRC);
  340. }
  341. }
  342. }
  343. /*
  344. * Clear irq
  345. */
  346. write_zsctrl(c, ERR_RES);
  347. write_zsctrl(c, RES_H_IUS);
  348. }
  349. /**
  350. * z8530_tx - Handle a PIO transmit event
  351. * @c: Z8530 channel to process
  352. *
  353. * Z8530 transmit interrupt handler for the PIO mode. The basic
  354. * idea is to attempt to keep the FIFO fed. We fill as many bytes
  355. * in as possible, its quite possible that we won't keep up with the
  356. * data rate otherwise.
  357. */
  358. static void z8530_tx(struct z8530_channel *c)
  359. {
  360. while(c->txcount) {
  361. /* FIFO full ? */
  362. if(!(read_zsreg(c, R0)&4))
  363. return;
  364. c->txcount--;
  365. /*
  366. * Shovel out the byte
  367. */
  368. write_zsreg(c, R8, *c->tx_ptr++);
  369. write_zsctrl(c, RES_H_IUS);
  370. /* We are about to underflow */
  371. if(c->txcount==0)
  372. {
  373. write_zsctrl(c, RES_EOM_L);
  374. write_zsreg(c, R10, c->regs[10]&~ABUNDER);
  375. }
  376. }
  377. /*
  378. * End of frame TX - fire another one
  379. */
  380. write_zsctrl(c, RES_Tx_P);
  381. z8530_tx_done(c);
  382. write_zsctrl(c, RES_H_IUS);
  383. }
  384. /**
  385. * z8530_status - Handle a PIO status exception
  386. * @chan: Z8530 channel to process
  387. *
  388. * A status event occurred in PIO synchronous mode. There are several
  389. * reasons the chip will bother us here. A transmit underrun means we
  390. * failed to feed the chip fast enough and just broke a packet. A DCD
  391. * change is a line up or down.
  392. */
  393. static void z8530_status(struct z8530_channel *chan)
  394. {
  395. u8 status, altered;
  396. status = read_zsreg(chan, R0);
  397. altered = chan->status ^ status;
  398. chan->status = status;
  399. if (status & TxEOM) {
  400. /* printk("%s: Tx underrun.\n", chan->dev->name); */
  401. chan->netdevice->stats.tx_fifo_errors++;
  402. write_zsctrl(chan, ERR_RES);
  403. z8530_tx_done(chan);
  404. }
  405. if (altered & chan->dcdcheck)
  406. {
  407. if (status & chan->dcdcheck) {
  408. printk(KERN_INFO "%s: DCD raised\n", chan->dev->name);
  409. write_zsreg(chan, R3, chan->regs[3] | RxENABLE);
  410. if (chan->netdevice)
  411. netif_carrier_on(chan->netdevice);
  412. } else {
  413. printk(KERN_INFO "%s: DCD lost\n", chan->dev->name);
  414. write_zsreg(chan, R3, chan->regs[3] & ~RxENABLE);
  415. z8530_flush_fifo(chan);
  416. if (chan->netdevice)
  417. netif_carrier_off(chan->netdevice);
  418. }
  419. }
  420. write_zsctrl(chan, RES_EXT_INT);
  421. write_zsctrl(chan, RES_H_IUS);
  422. }
  423. struct z8530_irqhandler z8530_sync =
  424. {
  425. z8530_rx,
  426. z8530_tx,
  427. z8530_status
  428. };
  429. EXPORT_SYMBOL(z8530_sync);
  430. /**
  431. * z8530_dma_rx - Handle a DMA RX event
  432. * @chan: Channel to handle
  433. *
  434. * Non bus mastering DMA interfaces for the Z8x30 devices. This
  435. * is really pretty PC specific. The DMA mode means that most receive
  436. * events are handled by the DMA hardware. We get a kick here only if
  437. * a frame ended.
  438. */
  439. static void z8530_dma_rx(struct z8530_channel *chan)
  440. {
  441. if(chan->rxdma_on)
  442. {
  443. /* Special condition check only */
  444. u8 status;
  445. read_zsreg(chan, R7);
  446. read_zsreg(chan, R6);
  447. status=read_zsreg(chan, R1);
  448. if(status&END_FR)
  449. {
  450. z8530_rx_done(chan); /* Fire up the next one */
  451. }
  452. write_zsctrl(chan, ERR_RES);
  453. write_zsctrl(chan, RES_H_IUS);
  454. }
  455. else
  456. {
  457. /* DMA is off right now, drain the slow way */
  458. z8530_rx(chan);
  459. }
  460. }
  461. /**
  462. * z8530_dma_tx - Handle a DMA TX event
  463. * @chan: The Z8530 channel to handle
  464. *
  465. * We have received an interrupt while doing DMA transmissions. It
  466. * shouldn't happen. Scream loudly if it does.
  467. */
  468. static void z8530_dma_tx(struct z8530_channel *chan)
  469. {
  470. if(!chan->dma_tx)
  471. {
  472. printk(KERN_WARNING "Hey who turned the DMA off?\n");
  473. z8530_tx(chan);
  474. return;
  475. }
  476. /* This shouldnt occur in DMA mode */
  477. printk(KERN_ERR "DMA tx - bogus event!\n");
  478. z8530_tx(chan);
  479. }
  480. /**
  481. * z8530_dma_status - Handle a DMA status exception
  482. * @chan: Z8530 channel to process
  483. *
  484. * A status event occurred on the Z8530. We receive these for two reasons
  485. * when in DMA mode. Firstly if we finished a packet transfer we get one
  486. * and kick the next packet out. Secondly we may see a DCD change.
  487. *
  488. */
  489. static void z8530_dma_status(struct z8530_channel *chan)
  490. {
  491. u8 status, altered;
  492. status=read_zsreg(chan, R0);
  493. altered=chan->status^status;
  494. chan->status=status;
  495. if(chan->dma_tx)
  496. {
  497. if(status&TxEOM)
  498. {
  499. unsigned long flags;
  500. flags=claim_dma_lock();
  501. disable_dma(chan->txdma);
  502. clear_dma_ff(chan->txdma);
  503. chan->txdma_on=0;
  504. release_dma_lock(flags);
  505. z8530_tx_done(chan);
  506. }
  507. }
  508. if (altered & chan->dcdcheck)
  509. {
  510. if (status & chan->dcdcheck) {
  511. printk(KERN_INFO "%s: DCD raised\n", chan->dev->name);
  512. write_zsreg(chan, R3, chan->regs[3] | RxENABLE);
  513. if (chan->netdevice)
  514. netif_carrier_on(chan->netdevice);
  515. } else {
  516. printk(KERN_INFO "%s:DCD lost\n", chan->dev->name);
  517. write_zsreg(chan, R3, chan->regs[3] & ~RxENABLE);
  518. z8530_flush_fifo(chan);
  519. if (chan->netdevice)
  520. netif_carrier_off(chan->netdevice);
  521. }
  522. }
  523. write_zsctrl(chan, RES_EXT_INT);
  524. write_zsctrl(chan, RES_H_IUS);
  525. }
  526. struct z8530_irqhandler z8530_dma_sync=
  527. {
  528. z8530_dma_rx,
  529. z8530_dma_tx,
  530. z8530_dma_status
  531. };
  532. EXPORT_SYMBOL(z8530_dma_sync);
  533. struct z8530_irqhandler z8530_txdma_sync=
  534. {
  535. z8530_rx,
  536. z8530_dma_tx,
  537. z8530_dma_status
  538. };
  539. EXPORT_SYMBOL(z8530_txdma_sync);
  540. /**
  541. * z8530_rx_clear - Handle RX events from a stopped chip
  542. * @c: Z8530 channel to shut up
  543. *
  544. * Receive interrupt vectors for a Z8530 that is in 'parked' mode.
  545. * For machines with PCI Z85x30 cards, or level triggered interrupts
  546. * (eg the MacII) we must clear the interrupt cause or die.
  547. */
  548. static void z8530_rx_clear(struct z8530_channel *c)
  549. {
  550. /*
  551. * Data and status bytes
  552. */
  553. u8 stat;
  554. read_zsdata(c);
  555. stat=read_zsreg(c, R1);
  556. if(stat&END_FR)
  557. write_zsctrl(c, RES_Rx_CRC);
  558. /*
  559. * Clear irq
  560. */
  561. write_zsctrl(c, ERR_RES);
  562. write_zsctrl(c, RES_H_IUS);
  563. }
  564. /**
  565. * z8530_tx_clear - Handle TX events from a stopped chip
  566. * @c: Z8530 channel to shut up
  567. *
  568. * Transmit interrupt vectors for a Z8530 that is in 'parked' mode.
  569. * For machines with PCI Z85x30 cards, or level triggered interrupts
  570. * (eg the MacII) we must clear the interrupt cause or die.
  571. */
  572. static void z8530_tx_clear(struct z8530_channel *c)
  573. {
  574. write_zsctrl(c, RES_Tx_P);
  575. write_zsctrl(c, RES_H_IUS);
  576. }
  577. /**
  578. * z8530_status_clear - Handle status events from a stopped chip
  579. * @chan: Z8530 channel to shut up
  580. *
  581. * Status interrupt vectors for a Z8530 that is in 'parked' mode.
  582. * For machines with PCI Z85x30 cards, or level triggered interrupts
  583. * (eg the MacII) we must clear the interrupt cause or die.
  584. */
  585. static void z8530_status_clear(struct z8530_channel *chan)
  586. {
  587. u8 status=read_zsreg(chan, R0);
  588. if(status&TxEOM)
  589. write_zsctrl(chan, ERR_RES);
  590. write_zsctrl(chan, RES_EXT_INT);
  591. write_zsctrl(chan, RES_H_IUS);
  592. }
  593. struct z8530_irqhandler z8530_nop=
  594. {
  595. z8530_rx_clear,
  596. z8530_tx_clear,
  597. z8530_status_clear
  598. };
  599. EXPORT_SYMBOL(z8530_nop);
  600. /**
  601. * z8530_interrupt - Handle an interrupt from a Z8530
  602. * @irq: Interrupt number
  603. * @dev_id: The Z8530 device that is interrupting.
  604. *
  605. * A Z85[2]30 device has stuck its hand in the air for attention.
  606. * We scan both the channels on the chip for events and then call
  607. * the channel specific call backs for each channel that has events.
  608. * We have to use callback functions because the two channels can be
  609. * in different modes.
  610. *
  611. * Locking is done for the handlers. Note that locking is done
  612. * at the chip level (the 5uS delay issue is per chip not per
  613. * channel). c->lock for both channels points to dev->lock
  614. */
  615. irqreturn_t z8530_interrupt(int irq, void *dev_id)
  616. {
  617. struct z8530_dev *dev=dev_id;
  618. u8 intr;
  619. static volatile int locker=0;
  620. int work=0;
  621. struct z8530_irqhandler *irqs;
  622. if(locker)
  623. {
  624. printk(KERN_ERR "IRQ re-enter\n");
  625. return IRQ_NONE;
  626. }
  627. locker=1;
  628. spin_lock(&dev->lock);
  629. while(++work<5000)
  630. {
  631. intr = read_zsreg(&dev->chanA, R3);
  632. if(!(intr & (CHARxIP|CHATxIP|CHAEXT|CHBRxIP|CHBTxIP|CHBEXT)))
  633. break;
  634. /* This holds the IRQ status. On the 8530 you must read it from chan
  635. A even though it applies to the whole chip */
  636. /* Now walk the chip and see what it is wanting - it may be
  637. an IRQ for someone else remember */
  638. irqs=dev->chanA.irqs;
  639. if(intr & (CHARxIP|CHATxIP|CHAEXT))
  640. {
  641. if(intr&CHARxIP)
  642. irqs->rx(&dev->chanA);
  643. if(intr&CHATxIP)
  644. irqs->tx(&dev->chanA);
  645. if(intr&CHAEXT)
  646. irqs->status(&dev->chanA);
  647. }
  648. irqs=dev->chanB.irqs;
  649. if(intr & (CHBRxIP|CHBTxIP|CHBEXT))
  650. {
  651. if(intr&CHBRxIP)
  652. irqs->rx(&dev->chanB);
  653. if(intr&CHBTxIP)
  654. irqs->tx(&dev->chanB);
  655. if(intr&CHBEXT)
  656. irqs->status(&dev->chanB);
  657. }
  658. }
  659. spin_unlock(&dev->lock);
  660. if(work==5000)
  661. printk(KERN_ERR "%s: interrupt jammed - abort(0x%X)!\n", dev->name, intr);
  662. /* Ok all done */
  663. locker=0;
  664. return IRQ_HANDLED;
  665. }
  666. EXPORT_SYMBOL(z8530_interrupt);
  667. static char reg_init[16]=
  668. {
  669. 0,0,0,0,
  670. 0,0,0,0,
  671. 0,0,0,0,
  672. 0x55,0,0,0
  673. };
  674. /**
  675. * z8530_sync_open - Open a Z8530 channel for PIO
  676. * @dev: The network interface we are using
  677. * @c: The Z8530 channel to open in synchronous PIO mode
  678. *
  679. * Switch a Z8530 into synchronous mode without DMA assist. We
  680. * raise the RTS/DTR and commence network operation.
  681. */
  682. int z8530_sync_open(struct net_device *dev, struct z8530_channel *c)
  683. {
  684. unsigned long flags;
  685. spin_lock_irqsave(c->lock, flags);
  686. c->sync = 1;
  687. c->mtu = dev->mtu+64;
  688. c->count = 0;
  689. c->skb = NULL;
  690. c->skb2 = NULL;
  691. c->irqs = &z8530_sync;
  692. /* This loads the double buffer up */
  693. z8530_rx_done(c); /* Load the frame ring */
  694. z8530_rx_done(c); /* Load the backup frame */
  695. z8530_rtsdtr(c,1);
  696. c->dma_tx = 0;
  697. c->regs[R1]|=TxINT_ENAB;
  698. write_zsreg(c, R1, c->regs[R1]);
  699. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  700. spin_unlock_irqrestore(c->lock, flags);
  701. return 0;
  702. }
  703. EXPORT_SYMBOL(z8530_sync_open);
  704. /**
  705. * z8530_sync_close - Close a PIO Z8530 channel
  706. * @dev: Network device to close
  707. * @c: Z8530 channel to disassociate and move to idle
  708. *
  709. * Close down a Z8530 interface and switch its interrupt handlers
  710. * to discard future events.
  711. */
  712. int z8530_sync_close(struct net_device *dev, struct z8530_channel *c)
  713. {
  714. u8 chk;
  715. unsigned long flags;
  716. spin_lock_irqsave(c->lock, flags);
  717. c->irqs = &z8530_nop;
  718. c->max = 0;
  719. c->sync = 0;
  720. chk=read_zsreg(c,R0);
  721. write_zsreg(c, R3, c->regs[R3]);
  722. z8530_rtsdtr(c,0);
  723. spin_unlock_irqrestore(c->lock, flags);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(z8530_sync_close);
  727. /**
  728. * z8530_sync_dma_open - Open a Z8530 for DMA I/O
  729. * @dev: The network device to attach
  730. * @c: The Z8530 channel to configure in sync DMA mode.
  731. *
  732. * Set up a Z85x30 device for synchronous DMA in both directions. Two
  733. * ISA DMA channels must be available for this to work. We assume ISA
  734. * DMA driven I/O and PC limits on access.
  735. */
  736. int z8530_sync_dma_open(struct net_device *dev, struct z8530_channel *c)
  737. {
  738. unsigned long cflags, dflags;
  739. c->sync = 1;
  740. c->mtu = dev->mtu+64;
  741. c->count = 0;
  742. c->skb = NULL;
  743. c->skb2 = NULL;
  744. /*
  745. * Load the DMA interfaces up
  746. */
  747. c->rxdma_on = 0;
  748. c->txdma_on = 0;
  749. /*
  750. * Allocate the DMA flip buffers. Limit by page size.
  751. * Everyone runs 1500 mtu or less on wan links so this
  752. * should be fine.
  753. */
  754. if(c->mtu > PAGE_SIZE/2)
  755. return -EMSGSIZE;
  756. c->rx_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  757. if(c->rx_buf[0]==NULL)
  758. return -ENOBUFS;
  759. c->rx_buf[1]=c->rx_buf[0]+PAGE_SIZE/2;
  760. c->tx_dma_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  761. if(c->tx_dma_buf[0]==NULL)
  762. {
  763. free_page((unsigned long)c->rx_buf[0]);
  764. c->rx_buf[0]=NULL;
  765. return -ENOBUFS;
  766. }
  767. c->tx_dma_buf[1]=c->tx_dma_buf[0]+PAGE_SIZE/2;
  768. c->tx_dma_used=0;
  769. c->dma_tx = 1;
  770. c->dma_num=0;
  771. c->dma_ready=1;
  772. /*
  773. * Enable DMA control mode
  774. */
  775. spin_lock_irqsave(c->lock, cflags);
  776. /*
  777. * TX DMA via DIR/REQ
  778. */
  779. c->regs[R14]|= DTRREQ;
  780. write_zsreg(c, R14, c->regs[R14]);
  781. c->regs[R1]&= ~TxINT_ENAB;
  782. write_zsreg(c, R1, c->regs[R1]);
  783. /*
  784. * RX DMA via W/Req
  785. */
  786. c->regs[R1]|= WT_FN_RDYFN;
  787. c->regs[R1]|= WT_RDY_RT;
  788. c->regs[R1]|= INT_ERR_Rx;
  789. c->regs[R1]&= ~TxINT_ENAB;
  790. write_zsreg(c, R1, c->regs[R1]);
  791. c->regs[R1]|= WT_RDY_ENAB;
  792. write_zsreg(c, R1, c->regs[R1]);
  793. /*
  794. * DMA interrupts
  795. */
  796. /*
  797. * Set up the DMA configuration
  798. */
  799. dflags=claim_dma_lock();
  800. disable_dma(c->rxdma);
  801. clear_dma_ff(c->rxdma);
  802. set_dma_mode(c->rxdma, DMA_MODE_READ|0x10);
  803. set_dma_addr(c->rxdma, virt_to_bus(c->rx_buf[0]));
  804. set_dma_count(c->rxdma, c->mtu);
  805. enable_dma(c->rxdma);
  806. disable_dma(c->txdma);
  807. clear_dma_ff(c->txdma);
  808. set_dma_mode(c->txdma, DMA_MODE_WRITE);
  809. disable_dma(c->txdma);
  810. release_dma_lock(dflags);
  811. /*
  812. * Select the DMA interrupt handlers
  813. */
  814. c->rxdma_on = 1;
  815. c->txdma_on = 1;
  816. c->tx_dma_used = 1;
  817. c->irqs = &z8530_dma_sync;
  818. z8530_rtsdtr(c,1);
  819. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  820. spin_unlock_irqrestore(c->lock, cflags);
  821. return 0;
  822. }
  823. EXPORT_SYMBOL(z8530_sync_dma_open);
  824. /**
  825. * z8530_sync_dma_close - Close down DMA I/O
  826. * @dev: Network device to detach
  827. * @c: Z8530 channel to move into discard mode
  828. *
  829. * Shut down a DMA mode synchronous interface. Halt the DMA, and
  830. * free the buffers.
  831. */
  832. int z8530_sync_dma_close(struct net_device *dev, struct z8530_channel *c)
  833. {
  834. u8 chk;
  835. unsigned long flags;
  836. c->irqs = &z8530_nop;
  837. c->max = 0;
  838. c->sync = 0;
  839. /*
  840. * Disable the PC DMA channels
  841. */
  842. flags=claim_dma_lock();
  843. disable_dma(c->rxdma);
  844. clear_dma_ff(c->rxdma);
  845. c->rxdma_on = 0;
  846. disable_dma(c->txdma);
  847. clear_dma_ff(c->txdma);
  848. release_dma_lock(flags);
  849. c->txdma_on = 0;
  850. c->tx_dma_used = 0;
  851. spin_lock_irqsave(c->lock, flags);
  852. /*
  853. * Disable DMA control mode
  854. */
  855. c->regs[R1]&= ~WT_RDY_ENAB;
  856. write_zsreg(c, R1, c->regs[R1]);
  857. c->regs[R1]&= ~(WT_RDY_RT|WT_FN_RDYFN|INT_ERR_Rx);
  858. c->regs[R1]|= INT_ALL_Rx;
  859. write_zsreg(c, R1, c->regs[R1]);
  860. c->regs[R14]&= ~DTRREQ;
  861. write_zsreg(c, R14, c->regs[R14]);
  862. if(c->rx_buf[0])
  863. {
  864. free_page((unsigned long)c->rx_buf[0]);
  865. c->rx_buf[0]=NULL;
  866. }
  867. if(c->tx_dma_buf[0])
  868. {
  869. free_page((unsigned long)c->tx_dma_buf[0]);
  870. c->tx_dma_buf[0]=NULL;
  871. }
  872. chk=read_zsreg(c,R0);
  873. write_zsreg(c, R3, c->regs[R3]);
  874. z8530_rtsdtr(c,0);
  875. spin_unlock_irqrestore(c->lock, flags);
  876. return 0;
  877. }
  878. EXPORT_SYMBOL(z8530_sync_dma_close);
  879. /**
  880. * z8530_sync_txdma_open - Open a Z8530 for TX driven DMA
  881. * @dev: The network device to attach
  882. * @c: The Z8530 channel to configure in sync DMA mode.
  883. *
  884. * Set up a Z85x30 device for synchronous DMA tranmission. One
  885. * ISA DMA channel must be available for this to work. The receive
  886. * side is run in PIO mode, but then it has the bigger FIFO.
  887. */
  888. int z8530_sync_txdma_open(struct net_device *dev, struct z8530_channel *c)
  889. {
  890. unsigned long cflags, dflags;
  891. printk("Opening sync interface for TX-DMA\n");
  892. c->sync = 1;
  893. c->mtu = dev->mtu+64;
  894. c->count = 0;
  895. c->skb = NULL;
  896. c->skb2 = NULL;
  897. /*
  898. * Allocate the DMA flip buffers. Limit by page size.
  899. * Everyone runs 1500 mtu or less on wan links so this
  900. * should be fine.
  901. */
  902. if(c->mtu > PAGE_SIZE/2)
  903. return -EMSGSIZE;
  904. c->tx_dma_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  905. if(c->tx_dma_buf[0]==NULL)
  906. return -ENOBUFS;
  907. c->tx_dma_buf[1] = c->tx_dma_buf[0] + PAGE_SIZE/2;
  908. spin_lock_irqsave(c->lock, cflags);
  909. /*
  910. * Load the PIO receive ring
  911. */
  912. z8530_rx_done(c);
  913. z8530_rx_done(c);
  914. /*
  915. * Load the DMA interfaces up
  916. */
  917. c->rxdma_on = 0;
  918. c->txdma_on = 0;
  919. c->tx_dma_used=0;
  920. c->dma_num=0;
  921. c->dma_ready=1;
  922. c->dma_tx = 1;
  923. /*
  924. * Enable DMA control mode
  925. */
  926. /*
  927. * TX DMA via DIR/REQ
  928. */
  929. c->regs[R14]|= DTRREQ;
  930. write_zsreg(c, R14, c->regs[R14]);
  931. c->regs[R1]&= ~TxINT_ENAB;
  932. write_zsreg(c, R1, c->regs[R1]);
  933. /*
  934. * Set up the DMA configuration
  935. */
  936. dflags = claim_dma_lock();
  937. disable_dma(c->txdma);
  938. clear_dma_ff(c->txdma);
  939. set_dma_mode(c->txdma, DMA_MODE_WRITE);
  940. disable_dma(c->txdma);
  941. release_dma_lock(dflags);
  942. /*
  943. * Select the DMA interrupt handlers
  944. */
  945. c->rxdma_on = 0;
  946. c->txdma_on = 1;
  947. c->tx_dma_used = 1;
  948. c->irqs = &z8530_txdma_sync;
  949. z8530_rtsdtr(c,1);
  950. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  951. spin_unlock_irqrestore(c->lock, cflags);
  952. return 0;
  953. }
  954. EXPORT_SYMBOL(z8530_sync_txdma_open);
  955. /**
  956. * z8530_sync_txdma_close - Close down a TX driven DMA channel
  957. * @dev: Network device to detach
  958. * @c: Z8530 channel to move into discard mode
  959. *
  960. * Shut down a DMA/PIO split mode synchronous interface. Halt the DMA,
  961. * and free the buffers.
  962. */
  963. int z8530_sync_txdma_close(struct net_device *dev, struct z8530_channel *c)
  964. {
  965. unsigned long dflags, cflags;
  966. u8 chk;
  967. spin_lock_irqsave(c->lock, cflags);
  968. c->irqs = &z8530_nop;
  969. c->max = 0;
  970. c->sync = 0;
  971. /*
  972. * Disable the PC DMA channels
  973. */
  974. dflags = claim_dma_lock();
  975. disable_dma(c->txdma);
  976. clear_dma_ff(c->txdma);
  977. c->txdma_on = 0;
  978. c->tx_dma_used = 0;
  979. release_dma_lock(dflags);
  980. /*
  981. * Disable DMA control mode
  982. */
  983. c->regs[R1]&= ~WT_RDY_ENAB;
  984. write_zsreg(c, R1, c->regs[R1]);
  985. c->regs[R1]&= ~(WT_RDY_RT|WT_FN_RDYFN|INT_ERR_Rx);
  986. c->regs[R1]|= INT_ALL_Rx;
  987. write_zsreg(c, R1, c->regs[R1]);
  988. c->regs[R14]&= ~DTRREQ;
  989. write_zsreg(c, R14, c->regs[R14]);
  990. if(c->tx_dma_buf[0])
  991. {
  992. free_page((unsigned long)c->tx_dma_buf[0]);
  993. c->tx_dma_buf[0]=NULL;
  994. }
  995. chk=read_zsreg(c,R0);
  996. write_zsreg(c, R3, c->regs[R3]);
  997. z8530_rtsdtr(c,0);
  998. spin_unlock_irqrestore(c->lock, cflags);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL(z8530_sync_txdma_close);
  1002. /*
  1003. * Name strings for Z8530 chips. SGI claim to have a 130, Zilog deny
  1004. * it exists...
  1005. */
  1006. static char *z8530_type_name[]={
  1007. "Z8530",
  1008. "Z85C30",
  1009. "Z85230"
  1010. };
  1011. /**
  1012. * z8530_describe - Uniformly describe a Z8530 port
  1013. * @dev: Z8530 device to describe
  1014. * @mapping: string holding mapping type (eg "I/O" or "Mem")
  1015. * @io: the port value in question
  1016. *
  1017. * Describe a Z8530 in a standard format. We must pass the I/O as
  1018. * the port offset isnt predictable. The main reason for this function
  1019. * is to try and get a common format of report.
  1020. */
  1021. void z8530_describe(struct z8530_dev *dev, char *mapping, unsigned long io)
  1022. {
  1023. printk(KERN_INFO "%s: %s found at %s 0x%lX, IRQ %d.\n",
  1024. dev->name,
  1025. z8530_type_name[dev->type],
  1026. mapping,
  1027. Z8530_PORT_OF(io),
  1028. dev->irq);
  1029. }
  1030. EXPORT_SYMBOL(z8530_describe);
  1031. /*
  1032. * Locked operation part of the z8530 init code
  1033. */
  1034. static inline int do_z8530_init(struct z8530_dev *dev)
  1035. {
  1036. /* NOP the interrupt handlers first - we might get a
  1037. floating IRQ transition when we reset the chip */
  1038. dev->chanA.irqs=&z8530_nop;
  1039. dev->chanB.irqs=&z8530_nop;
  1040. dev->chanA.dcdcheck=DCD;
  1041. dev->chanB.dcdcheck=DCD;
  1042. /* Reset the chip */
  1043. write_zsreg(&dev->chanA, R9, 0xC0);
  1044. udelay(200);
  1045. /* Now check its valid */
  1046. write_zsreg(&dev->chanA, R12, 0xAA);
  1047. if(read_zsreg(&dev->chanA, R12)!=0xAA)
  1048. return -ENODEV;
  1049. write_zsreg(&dev->chanA, R12, 0x55);
  1050. if(read_zsreg(&dev->chanA, R12)!=0x55)
  1051. return -ENODEV;
  1052. dev->type=Z8530;
  1053. /*
  1054. * See the application note.
  1055. */
  1056. write_zsreg(&dev->chanA, R15, 0x01);
  1057. /*
  1058. * If we can set the low bit of R15 then
  1059. * the chip is enhanced.
  1060. */
  1061. if(read_zsreg(&dev->chanA, R15)==0x01)
  1062. {
  1063. /* This C30 versus 230 detect is from Klaus Kudielka's dmascc */
  1064. /* Put a char in the fifo */
  1065. write_zsreg(&dev->chanA, R8, 0);
  1066. if(read_zsreg(&dev->chanA, R0)&Tx_BUF_EMP)
  1067. dev->type = Z85230; /* Has a FIFO */
  1068. else
  1069. dev->type = Z85C30; /* Z85C30, 1 byte FIFO */
  1070. }
  1071. /*
  1072. * The code assumes R7' and friends are
  1073. * off. Use write_zsext() for these and keep
  1074. * this bit clear.
  1075. */
  1076. write_zsreg(&dev->chanA, R15, 0);
  1077. /*
  1078. * At this point it looks like the chip is behaving
  1079. */
  1080. memcpy(dev->chanA.regs, reg_init, 16);
  1081. memcpy(dev->chanB.regs, reg_init ,16);
  1082. return 0;
  1083. }
  1084. /**
  1085. * z8530_init - Initialise a Z8530 device
  1086. * @dev: Z8530 device to initialise.
  1087. *
  1088. * Configure up a Z8530/Z85C30 or Z85230 chip. We check the device
  1089. * is present, identify the type and then program it to hopefully
  1090. * keep quite and behave. This matters a lot, a Z8530 in the wrong
  1091. * state will sometimes get into stupid modes generating 10Khz
  1092. * interrupt streams and the like.
  1093. *
  1094. * We set the interrupt handler up to discard any events, in case
  1095. * we get them during reset or setp.
  1096. *
  1097. * Return 0 for success, or a negative value indicating the problem
  1098. * in errno form.
  1099. */
  1100. int z8530_init(struct z8530_dev *dev)
  1101. {
  1102. unsigned long flags;
  1103. int ret;
  1104. /* Set up the chip level lock */
  1105. spin_lock_init(&dev->lock);
  1106. dev->chanA.lock = &dev->lock;
  1107. dev->chanB.lock = &dev->lock;
  1108. spin_lock_irqsave(&dev->lock, flags);
  1109. ret = do_z8530_init(dev);
  1110. spin_unlock_irqrestore(&dev->lock, flags);
  1111. return ret;
  1112. }
  1113. EXPORT_SYMBOL(z8530_init);
  1114. /**
  1115. * z8530_shutdown - Shutdown a Z8530 device
  1116. * @dev: The Z8530 chip to shutdown
  1117. *
  1118. * We set the interrupt handlers to silence any interrupts. We then
  1119. * reset the chip and wait 100uS to be sure the reset completed. Just
  1120. * in case the caller then tries to do stuff.
  1121. *
  1122. * This is called without the lock held
  1123. */
  1124. int z8530_shutdown(struct z8530_dev *dev)
  1125. {
  1126. unsigned long flags;
  1127. /* Reset the chip */
  1128. spin_lock_irqsave(&dev->lock, flags);
  1129. dev->chanA.irqs=&z8530_nop;
  1130. dev->chanB.irqs=&z8530_nop;
  1131. write_zsreg(&dev->chanA, R9, 0xC0);
  1132. /* We must lock the udelay, the chip is offlimits here */
  1133. udelay(100);
  1134. spin_unlock_irqrestore(&dev->lock, flags);
  1135. return 0;
  1136. }
  1137. EXPORT_SYMBOL(z8530_shutdown);
  1138. /**
  1139. * z8530_channel_load - Load channel data
  1140. * @c: Z8530 channel to configure
  1141. * @rtable: table of register, value pairs
  1142. * FIXME: ioctl to allow user uploaded tables
  1143. *
  1144. * Load a Z8530 channel up from the system data. We use +16 to
  1145. * indicate the "prime" registers. The value 255 terminates the
  1146. * table.
  1147. */
  1148. int z8530_channel_load(struct z8530_channel *c, u8 *rtable)
  1149. {
  1150. unsigned long flags;
  1151. spin_lock_irqsave(c->lock, flags);
  1152. while(*rtable!=255)
  1153. {
  1154. int reg=*rtable++;
  1155. if(reg>0x0F)
  1156. write_zsreg(c, R15, c->regs[15]|1);
  1157. write_zsreg(c, reg&0x0F, *rtable);
  1158. if(reg>0x0F)
  1159. write_zsreg(c, R15, c->regs[15]&~1);
  1160. c->regs[reg]=*rtable++;
  1161. }
  1162. c->rx_function=z8530_null_rx;
  1163. c->skb=NULL;
  1164. c->tx_skb=NULL;
  1165. c->tx_next_skb=NULL;
  1166. c->mtu=1500;
  1167. c->max=0;
  1168. c->count=0;
  1169. c->status=read_zsreg(c, R0);
  1170. c->sync=1;
  1171. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  1172. spin_unlock_irqrestore(c->lock, flags);
  1173. return 0;
  1174. }
  1175. EXPORT_SYMBOL(z8530_channel_load);
  1176. /**
  1177. * z8530_tx_begin - Begin packet transmission
  1178. * @c: The Z8530 channel to kick
  1179. *
  1180. * This is the speed sensitive side of transmission. If we are called
  1181. * and no buffer is being transmitted we commence the next buffer. If
  1182. * nothing is queued we idle the sync.
  1183. *
  1184. * Note: We are handling this code path in the interrupt path, keep it
  1185. * fast or bad things will happen.
  1186. *
  1187. * Called with the lock held.
  1188. */
  1189. static void z8530_tx_begin(struct z8530_channel *c)
  1190. {
  1191. unsigned long flags;
  1192. if(c->tx_skb)
  1193. return;
  1194. c->tx_skb=c->tx_next_skb;
  1195. c->tx_next_skb=NULL;
  1196. c->tx_ptr=c->tx_next_ptr;
  1197. if(c->tx_skb==NULL)
  1198. {
  1199. /* Idle on */
  1200. if(c->dma_tx)
  1201. {
  1202. flags=claim_dma_lock();
  1203. disable_dma(c->txdma);
  1204. /*
  1205. * Check if we crapped out.
  1206. */
  1207. if (get_dma_residue(c->txdma))
  1208. {
  1209. c->netdevice->stats.tx_dropped++;
  1210. c->netdevice->stats.tx_fifo_errors++;
  1211. }
  1212. release_dma_lock(flags);
  1213. }
  1214. c->txcount=0;
  1215. }
  1216. else
  1217. {
  1218. c->txcount=c->tx_skb->len;
  1219. if(c->dma_tx)
  1220. {
  1221. /*
  1222. * FIXME. DMA is broken for the original 8530,
  1223. * on the older parts we need to set a flag and
  1224. * wait for a further TX interrupt to fire this
  1225. * stage off
  1226. */
  1227. flags=claim_dma_lock();
  1228. disable_dma(c->txdma);
  1229. /*
  1230. * These two are needed by the 8530/85C30
  1231. * and must be issued when idling.
  1232. */
  1233. if(c->dev->type!=Z85230)
  1234. {
  1235. write_zsctrl(c, RES_Tx_CRC);
  1236. write_zsctrl(c, RES_EOM_L);
  1237. }
  1238. write_zsreg(c, R10, c->regs[10]&~ABUNDER);
  1239. clear_dma_ff(c->txdma);
  1240. set_dma_addr(c->txdma, virt_to_bus(c->tx_ptr));
  1241. set_dma_count(c->txdma, c->txcount);
  1242. enable_dma(c->txdma);
  1243. release_dma_lock(flags);
  1244. write_zsctrl(c, RES_EOM_L);
  1245. write_zsreg(c, R5, c->regs[R5]|TxENAB);
  1246. }
  1247. else
  1248. {
  1249. /* ABUNDER off */
  1250. write_zsreg(c, R10, c->regs[10]);
  1251. write_zsctrl(c, RES_Tx_CRC);
  1252. while(c->txcount && (read_zsreg(c,R0)&Tx_BUF_EMP))
  1253. {
  1254. write_zsreg(c, R8, *c->tx_ptr++);
  1255. c->txcount--;
  1256. }
  1257. }
  1258. }
  1259. /*
  1260. * Since we emptied tx_skb we can ask for more
  1261. */
  1262. netif_wake_queue(c->netdevice);
  1263. }
  1264. /**
  1265. * z8530_tx_done - TX complete callback
  1266. * @c: The channel that completed a transmit.
  1267. *
  1268. * This is called when we complete a packet send. We wake the queue,
  1269. * start the next packet going and then free the buffer of the existing
  1270. * packet. This code is fairly timing sensitive.
  1271. *
  1272. * Called with the register lock held.
  1273. */
  1274. static void z8530_tx_done(struct z8530_channel *c)
  1275. {
  1276. struct sk_buff *skb;
  1277. /* Actually this can happen.*/
  1278. if (c->tx_skb == NULL)
  1279. return;
  1280. skb = c->tx_skb;
  1281. c->tx_skb = NULL;
  1282. z8530_tx_begin(c);
  1283. c->netdevice->stats.tx_packets++;
  1284. c->netdevice->stats.tx_bytes += skb->len;
  1285. dev_kfree_skb_irq(skb);
  1286. }
  1287. /**
  1288. * z8530_null_rx - Discard a packet
  1289. * @c: The channel the packet arrived on
  1290. * @skb: The buffer
  1291. *
  1292. * We point the receive handler at this function when idle. Instead
  1293. * of processing the frames we get to throw them away.
  1294. */
  1295. void z8530_null_rx(struct z8530_channel *c, struct sk_buff *skb)
  1296. {
  1297. dev_kfree_skb_any(skb);
  1298. }
  1299. EXPORT_SYMBOL(z8530_null_rx);
  1300. /**
  1301. * z8530_rx_done - Receive completion callback
  1302. * @c: The channel that completed a receive
  1303. *
  1304. * A new packet is complete. Our goal here is to get back into receive
  1305. * mode as fast as possible. On the Z85230 we could change to using
  1306. * ESCC mode, but on the older chips we have no choice. We flip to the
  1307. * new buffer immediately in DMA mode so that the DMA of the next
  1308. * frame can occur while we are copying the previous buffer to an sk_buff
  1309. *
  1310. * Called with the lock held
  1311. */
  1312. static void z8530_rx_done(struct z8530_channel *c)
  1313. {
  1314. struct sk_buff *skb;
  1315. int ct;
  1316. /*
  1317. * Is our receive engine in DMA mode
  1318. */
  1319. if(c->rxdma_on)
  1320. {
  1321. /*
  1322. * Save the ready state and the buffer currently
  1323. * being used as the DMA target
  1324. */
  1325. int ready=c->dma_ready;
  1326. unsigned char *rxb=c->rx_buf[c->dma_num];
  1327. unsigned long flags;
  1328. /*
  1329. * Complete this DMA. Neccessary to find the length
  1330. */
  1331. flags=claim_dma_lock();
  1332. disable_dma(c->rxdma);
  1333. clear_dma_ff(c->rxdma);
  1334. c->rxdma_on=0;
  1335. ct=c->mtu-get_dma_residue(c->rxdma);
  1336. if(ct<0)
  1337. ct=2; /* Shit happens.. */
  1338. c->dma_ready=0;
  1339. /*
  1340. * Normal case: the other slot is free, start the next DMA
  1341. * into it immediately.
  1342. */
  1343. if(ready)
  1344. {
  1345. c->dma_num^=1;
  1346. set_dma_mode(c->rxdma, DMA_MODE_READ|0x10);
  1347. set_dma_addr(c->rxdma, virt_to_bus(c->rx_buf[c->dma_num]));
  1348. set_dma_count(c->rxdma, c->mtu);
  1349. c->rxdma_on = 1;
  1350. enable_dma(c->rxdma);
  1351. /* Stop any frames that we missed the head of
  1352. from passing */
  1353. write_zsreg(c, R0, RES_Rx_CRC);
  1354. }
  1355. else
  1356. /* Can't occur as we dont reenable the DMA irq until
  1357. after the flip is done */
  1358. printk(KERN_WARNING "%s: DMA flip overrun!\n",
  1359. c->netdevice->name);
  1360. release_dma_lock(flags);
  1361. /*
  1362. * Shove the old buffer into an sk_buff. We can't DMA
  1363. * directly into one on a PC - it might be above the 16Mb
  1364. * boundary. Optimisation - we could check to see if we
  1365. * can avoid the copy. Optimisation 2 - make the memcpy
  1366. * a copychecksum.
  1367. */
  1368. skb = dev_alloc_skb(ct);
  1369. if (skb == NULL) {
  1370. c->netdevice->stats.rx_dropped++;
  1371. printk(KERN_WARNING "%s: Memory squeeze.\n",
  1372. c->netdevice->name);
  1373. } else {
  1374. skb_put(skb, ct);
  1375. skb_copy_to_linear_data(skb, rxb, ct);
  1376. c->netdevice->stats.rx_packets++;
  1377. c->netdevice->stats.rx_bytes += ct;
  1378. }
  1379. c->dma_ready = 1;
  1380. } else {
  1381. RT_LOCK;
  1382. skb = c->skb;
  1383. /*
  1384. * The game we play for non DMA is similar. We want to
  1385. * get the controller set up for the next packet as fast
  1386. * as possible. We potentially only have one byte + the
  1387. * fifo length for this. Thus we want to flip to the new
  1388. * buffer and then mess around copying and allocating
  1389. * things. For the current case it doesn't matter but
  1390. * if you build a system where the sync irq isnt blocked
  1391. * by the kernel IRQ disable then you need only block the
  1392. * sync IRQ for the RT_LOCK area.
  1393. *
  1394. */
  1395. ct=c->count;
  1396. c->skb = c->skb2;
  1397. c->count = 0;
  1398. c->max = c->mtu;
  1399. if (c->skb) {
  1400. c->dptr = c->skb->data;
  1401. c->max = c->mtu;
  1402. } else {
  1403. c->count = 0;
  1404. c->max = 0;
  1405. }
  1406. RT_UNLOCK;
  1407. c->skb2 = dev_alloc_skb(c->mtu);
  1408. if (c->skb2 == NULL)
  1409. printk(KERN_WARNING "%s: memory squeeze.\n",
  1410. c->netdevice->name);
  1411. else
  1412. skb_put(c->skb2, c->mtu);
  1413. c->netdevice->stats.rx_packets++;
  1414. c->netdevice->stats.rx_bytes += ct;
  1415. }
  1416. /*
  1417. * If we received a frame we must now process it.
  1418. */
  1419. if (skb) {
  1420. skb_trim(skb, ct);
  1421. c->rx_function(c, skb);
  1422. } else {
  1423. c->netdevice->stats.rx_dropped++;
  1424. printk(KERN_ERR "%s: Lost a frame\n", c->netdevice->name);
  1425. }
  1426. }
  1427. /**
  1428. * spans_boundary - Check a packet can be ISA DMA'd
  1429. * @skb: The buffer to check
  1430. *
  1431. * Returns true if the buffer cross a DMA boundary on a PC. The poor
  1432. * thing can only DMA within a 64K block not across the edges of it.
  1433. */
  1434. static inline int spans_boundary(struct sk_buff *skb)
  1435. {
  1436. unsigned long a=(unsigned long)skb->data;
  1437. a^=(a+skb->len);
  1438. if(a&0x00010000) /* If the 64K bit is different.. */
  1439. return 1;
  1440. return 0;
  1441. }
  1442. /**
  1443. * z8530_queue_xmit - Queue a packet
  1444. * @c: The channel to use
  1445. * @skb: The packet to kick down the channel
  1446. *
  1447. * Queue a packet for transmission. Because we have rather
  1448. * hard to hit interrupt latencies for the Z85230 per packet
  1449. * even in DMA mode we do the flip to DMA buffer if needed here
  1450. * not in the IRQ.
  1451. *
  1452. * Called from the network code. The lock is not held at this
  1453. * point.
  1454. */
  1455. int z8530_queue_xmit(struct z8530_channel *c, struct sk_buff *skb)
  1456. {
  1457. unsigned long flags;
  1458. netif_stop_queue(c->netdevice);
  1459. if(c->tx_next_skb)
  1460. {
  1461. return 1;
  1462. }
  1463. /* PC SPECIFIC - DMA limits */
  1464. /*
  1465. * If we will DMA the transmit and its gone over the ISA bus
  1466. * limit, then copy to the flip buffer
  1467. */
  1468. if(c->dma_tx && ((unsigned long)(virt_to_bus(skb->data+skb->len))>=16*1024*1024 || spans_boundary(skb)))
  1469. {
  1470. /*
  1471. * Send the flip buffer, and flip the flippy bit.
  1472. * We don't care which is used when just so long as
  1473. * we never use the same buffer twice in a row. Since
  1474. * only one buffer can be going out at a time the other
  1475. * has to be safe.
  1476. */
  1477. c->tx_next_ptr=c->tx_dma_buf[c->tx_dma_used];
  1478. c->tx_dma_used^=1; /* Flip temp buffer */
  1479. skb_copy_from_linear_data(skb, c->tx_next_ptr, skb->len);
  1480. }
  1481. else
  1482. c->tx_next_ptr=skb->data;
  1483. RT_LOCK;
  1484. c->tx_next_skb=skb;
  1485. RT_UNLOCK;
  1486. spin_lock_irqsave(c->lock, flags);
  1487. z8530_tx_begin(c);
  1488. spin_unlock_irqrestore(c->lock, flags);
  1489. return 0;
  1490. }
  1491. EXPORT_SYMBOL(z8530_queue_xmit);
  1492. /*
  1493. * Module support
  1494. */
  1495. static char banner[] __initdata = KERN_INFO "Generic Z85C30/Z85230 interface driver v0.02\n";
  1496. static int __init z85230_init_driver(void)
  1497. {
  1498. printk(banner);
  1499. return 0;
  1500. }
  1501. module_init(z85230_init_driver);
  1502. static void __exit z85230_cleanup_driver(void)
  1503. {
  1504. }
  1505. module_exit(z85230_cleanup_driver);
  1506. MODULE_AUTHOR("Red Hat Inc.");
  1507. MODULE_DESCRIPTION("Z85x30 synchronous driver core");
  1508. MODULE_LICENSE("GPL");