z85230.c 40 KB

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