z85230.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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. spin_lock(c->lock);
  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. spin_unlock(c->lock);
  349. }
  350. /**
  351. * z8530_tx - Handle a PIO transmit event
  352. * @c: Z8530 channel to process
  353. *
  354. * Z8530 transmit interrupt handler for the PIO mode. The basic
  355. * idea is to attempt to keep the FIFO fed. We fill as many bytes
  356. * in as possible, its quite possible that we won't keep up with the
  357. * data rate otherwise.
  358. */
  359. static void z8530_tx(struct z8530_channel *c)
  360. {
  361. spin_lock(c->lock);
  362. while(c->txcount) {
  363. /* FIFO full ? */
  364. if(!(read_zsreg(c, R0)&4))
  365. break;
  366. c->txcount--;
  367. /*
  368. * Shovel out the byte
  369. */
  370. write_zsreg(c, R8, *c->tx_ptr++);
  371. write_zsctrl(c, RES_H_IUS);
  372. /* We are about to underflow */
  373. if(c->txcount==0)
  374. {
  375. write_zsctrl(c, RES_EOM_L);
  376. write_zsreg(c, R10, c->regs[10]&~ABUNDER);
  377. }
  378. }
  379. /*
  380. * End of frame TX - fire another one
  381. */
  382. write_zsctrl(c, RES_Tx_P);
  383. z8530_tx_done(c);
  384. write_zsctrl(c, RES_H_IUS);
  385. spin_unlock(c->lock);
  386. }
  387. /**
  388. * z8530_status - Handle a PIO status exception
  389. * @chan: Z8530 channel to process
  390. *
  391. * A status event occurred in PIO synchronous mode. There are several
  392. * reasons the chip will bother us here. A transmit underrun means we
  393. * failed to feed the chip fast enough and just broke a packet. A DCD
  394. * change is a line up or down. We communicate that back to the protocol
  395. * layer for synchronous PPP to renegotiate.
  396. */
  397. static void z8530_status(struct z8530_channel *chan)
  398. {
  399. u8 status, altered;
  400. spin_lock(chan->lock);
  401. status=read_zsreg(chan, R0);
  402. altered=chan->status^status;
  403. chan->status=status;
  404. if(status&TxEOM)
  405. {
  406. /* printk("%s: Tx underrun.\n", chan->dev->name); */
  407. chan->stats.tx_fifo_errors++;
  408. write_zsctrl(chan, ERR_RES);
  409. z8530_tx_done(chan);
  410. }
  411. if(altered&chan->dcdcheck)
  412. {
  413. if(status&chan->dcdcheck)
  414. {
  415. printk(KERN_INFO "%s: DCD raised\n", chan->dev->name);
  416. write_zsreg(chan, R3, chan->regs[3]|RxENABLE);
  417. if(chan->netdevice &&
  418. ((chan->netdevice->type == ARPHRD_HDLC) ||
  419. (chan->netdevice->type == ARPHRD_PPP)))
  420. sppp_reopen(chan->netdevice);
  421. }
  422. else
  423. {
  424. printk(KERN_INFO "%s: DCD lost\n", chan->dev->name);
  425. write_zsreg(chan, R3, chan->regs[3]&~RxENABLE);
  426. z8530_flush_fifo(chan);
  427. }
  428. }
  429. write_zsctrl(chan, RES_EXT_INT);
  430. write_zsctrl(chan, RES_H_IUS);
  431. spin_unlock(chan->lock);
  432. }
  433. struct z8530_irqhandler z8530_sync=
  434. {
  435. z8530_rx,
  436. z8530_tx,
  437. z8530_status
  438. };
  439. EXPORT_SYMBOL(z8530_sync);
  440. /**
  441. * z8530_dma_rx - Handle a DMA RX event
  442. * @chan: Channel to handle
  443. *
  444. * Non bus mastering DMA interfaces for the Z8x30 devices. This
  445. * is really pretty PC specific. The DMA mode means that most receive
  446. * events are handled by the DMA hardware. We get a kick here only if
  447. * a frame ended.
  448. */
  449. static void z8530_dma_rx(struct z8530_channel *chan)
  450. {
  451. spin_lock(chan->lock);
  452. if(chan->rxdma_on)
  453. {
  454. /* Special condition check only */
  455. u8 status;
  456. read_zsreg(chan, R7);
  457. read_zsreg(chan, R6);
  458. status=read_zsreg(chan, R1);
  459. if(status&END_FR)
  460. {
  461. z8530_rx_done(chan); /* Fire up the next one */
  462. }
  463. write_zsctrl(chan, ERR_RES);
  464. write_zsctrl(chan, RES_H_IUS);
  465. }
  466. else
  467. {
  468. /* DMA is off right now, drain the slow way */
  469. z8530_rx(chan);
  470. }
  471. spin_unlock(chan->lock);
  472. }
  473. /**
  474. * z8530_dma_tx - Handle a DMA TX event
  475. * @chan: The Z8530 channel to handle
  476. *
  477. * We have received an interrupt while doing DMA transmissions. It
  478. * shouldn't happen. Scream loudly if it does.
  479. */
  480. static void z8530_dma_tx(struct z8530_channel *chan)
  481. {
  482. spin_lock(chan->lock);
  483. if(!chan->dma_tx)
  484. {
  485. printk(KERN_WARNING "Hey who turned the DMA off?\n");
  486. z8530_tx(chan);
  487. return;
  488. }
  489. /* This shouldnt occur in DMA mode */
  490. printk(KERN_ERR "DMA tx - bogus event!\n");
  491. z8530_tx(chan);
  492. spin_unlock(chan->lock);
  493. }
  494. /**
  495. * z8530_dma_status - Handle a DMA status exception
  496. * @chan: Z8530 channel to process
  497. *
  498. * A status event occurred on the Z8530. We receive these for two reasons
  499. * when in DMA mode. Firstly if we finished a packet transfer we get one
  500. * and kick the next packet out. Secondly we may see a DCD change and
  501. * have to poke the protocol layer.
  502. *
  503. */
  504. static void z8530_dma_status(struct z8530_channel *chan)
  505. {
  506. u8 status, altered;
  507. status=read_zsreg(chan, R0);
  508. altered=chan->status^status;
  509. chan->status=status;
  510. if(chan->dma_tx)
  511. {
  512. if(status&TxEOM)
  513. {
  514. unsigned long flags;
  515. flags=claim_dma_lock();
  516. disable_dma(chan->txdma);
  517. clear_dma_ff(chan->txdma);
  518. chan->txdma_on=0;
  519. release_dma_lock(flags);
  520. z8530_tx_done(chan);
  521. }
  522. }
  523. spin_lock(chan->lock);
  524. if(altered&chan->dcdcheck)
  525. {
  526. if(status&chan->dcdcheck)
  527. {
  528. printk(KERN_INFO "%s: DCD raised\n", chan->dev->name);
  529. write_zsreg(chan, R3, chan->regs[3]|RxENABLE);
  530. if(chan->netdevice &&
  531. ((chan->netdevice->type == ARPHRD_HDLC) ||
  532. (chan->netdevice->type == ARPHRD_PPP)))
  533. sppp_reopen(chan->netdevice);
  534. }
  535. else
  536. {
  537. printk(KERN_INFO "%s:DCD lost\n", chan->dev->name);
  538. write_zsreg(chan, R3, chan->regs[3]&~RxENABLE);
  539. z8530_flush_fifo(chan);
  540. }
  541. }
  542. write_zsctrl(chan, RES_EXT_INT);
  543. write_zsctrl(chan, RES_H_IUS);
  544. spin_unlock(chan->lock);
  545. }
  546. struct z8530_irqhandler z8530_dma_sync=
  547. {
  548. z8530_dma_rx,
  549. z8530_dma_tx,
  550. z8530_dma_status
  551. };
  552. EXPORT_SYMBOL(z8530_dma_sync);
  553. struct z8530_irqhandler z8530_txdma_sync=
  554. {
  555. z8530_rx,
  556. z8530_dma_tx,
  557. z8530_dma_status
  558. };
  559. EXPORT_SYMBOL(z8530_txdma_sync);
  560. /**
  561. * z8530_rx_clear - Handle RX events from a stopped chip
  562. * @c: Z8530 channel to shut up
  563. *
  564. * Receive interrupt vectors for a Z8530 that is in 'parked' mode.
  565. * For machines with PCI Z85x30 cards, or level triggered interrupts
  566. * (eg the MacII) we must clear the interrupt cause or die.
  567. */
  568. static void z8530_rx_clear(struct z8530_channel *c)
  569. {
  570. /*
  571. * Data and status bytes
  572. */
  573. u8 stat;
  574. read_zsdata(c);
  575. stat=read_zsreg(c, R1);
  576. if(stat&END_FR)
  577. write_zsctrl(c, RES_Rx_CRC);
  578. /*
  579. * Clear irq
  580. */
  581. write_zsctrl(c, ERR_RES);
  582. write_zsctrl(c, RES_H_IUS);
  583. }
  584. /**
  585. * z8530_tx_clear - Handle TX events from a stopped chip
  586. * @c: Z8530 channel to shut up
  587. *
  588. * Transmit interrupt vectors for a Z8530 that is in 'parked' mode.
  589. * For machines with PCI Z85x30 cards, or level triggered interrupts
  590. * (eg the MacII) we must clear the interrupt cause or die.
  591. */
  592. static void z8530_tx_clear(struct z8530_channel *c)
  593. {
  594. write_zsctrl(c, RES_Tx_P);
  595. write_zsctrl(c, RES_H_IUS);
  596. }
  597. /**
  598. * z8530_status_clear - Handle status events from a stopped chip
  599. * @chan: Z8530 channel to shut up
  600. *
  601. * Status interrupt vectors for a Z8530 that is in 'parked' mode.
  602. * For machines with PCI Z85x30 cards, or level triggered interrupts
  603. * (eg the MacII) we must clear the interrupt cause or die.
  604. */
  605. static void z8530_status_clear(struct z8530_channel *chan)
  606. {
  607. u8 status=read_zsreg(chan, R0);
  608. if(status&TxEOM)
  609. write_zsctrl(chan, ERR_RES);
  610. write_zsctrl(chan, RES_EXT_INT);
  611. write_zsctrl(chan, RES_H_IUS);
  612. }
  613. struct z8530_irqhandler z8530_nop=
  614. {
  615. z8530_rx_clear,
  616. z8530_tx_clear,
  617. z8530_status_clear
  618. };
  619. EXPORT_SYMBOL(z8530_nop);
  620. /**
  621. * z8530_interrupt - Handle an interrupt from a Z8530
  622. * @irq: Interrupt number
  623. * @dev_id: The Z8530 device that is interrupting.
  624. * @regs: unused
  625. *
  626. * A Z85[2]30 device has stuck its hand in the air for attention.
  627. * We scan both the channels on the chip for events and then call
  628. * the channel specific call backs for each channel that has events.
  629. * We have to use callback functions because the two channels can be
  630. * in different modes.
  631. *
  632. * Locking is done for the handlers. Note that locking is done
  633. * at the chip level (the 5uS delay issue is per chip not per
  634. * channel). c->lock for both channels points to dev->lock
  635. */
  636. irqreturn_t z8530_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  637. {
  638. struct z8530_dev *dev=dev_id;
  639. u8 intr;
  640. static volatile int locker=0;
  641. int work=0;
  642. struct z8530_irqhandler *irqs;
  643. if(locker)
  644. {
  645. printk(KERN_ERR "IRQ re-enter\n");
  646. return IRQ_NONE;
  647. }
  648. locker=1;
  649. spin_lock(&dev->lock);
  650. while(++work<5000)
  651. {
  652. intr = read_zsreg(&dev->chanA, R3);
  653. if(!(intr & (CHARxIP|CHATxIP|CHAEXT|CHBRxIP|CHBTxIP|CHBEXT)))
  654. break;
  655. /* This holds the IRQ status. On the 8530 you must read it from chan
  656. A even though it applies to the whole chip */
  657. /* Now walk the chip and see what it is wanting - it may be
  658. an IRQ for someone else remember */
  659. irqs=dev->chanA.irqs;
  660. if(intr & (CHARxIP|CHATxIP|CHAEXT))
  661. {
  662. if(intr&CHARxIP)
  663. irqs->rx(&dev->chanA);
  664. if(intr&CHATxIP)
  665. irqs->tx(&dev->chanA);
  666. if(intr&CHAEXT)
  667. irqs->status(&dev->chanA);
  668. }
  669. irqs=dev->chanB.irqs;
  670. if(intr & (CHBRxIP|CHBTxIP|CHBEXT))
  671. {
  672. if(intr&CHBRxIP)
  673. irqs->rx(&dev->chanB);
  674. if(intr&CHBTxIP)
  675. irqs->tx(&dev->chanB);
  676. if(intr&CHBEXT)
  677. irqs->status(&dev->chanB);
  678. }
  679. }
  680. spin_unlock(&dev->lock);
  681. if(work==5000)
  682. printk(KERN_ERR "%s: interrupt jammed - abort(0x%X)!\n", dev->name, intr);
  683. /* Ok all done */
  684. locker=0;
  685. return IRQ_HANDLED;
  686. }
  687. EXPORT_SYMBOL(z8530_interrupt);
  688. static char reg_init[16]=
  689. {
  690. 0,0,0,0,
  691. 0,0,0,0,
  692. 0,0,0,0,
  693. 0x55,0,0,0
  694. };
  695. /**
  696. * z8530_sync_open - Open a Z8530 channel for PIO
  697. * @dev: The network interface we are using
  698. * @c: The Z8530 channel to open in synchronous PIO mode
  699. *
  700. * Switch a Z8530 into synchronous mode without DMA assist. We
  701. * raise the RTS/DTR and commence network operation.
  702. */
  703. int z8530_sync_open(struct net_device *dev, struct z8530_channel *c)
  704. {
  705. unsigned long flags;
  706. spin_lock_irqsave(c->lock, flags);
  707. c->sync = 1;
  708. c->mtu = dev->mtu+64;
  709. c->count = 0;
  710. c->skb = NULL;
  711. c->skb2 = NULL;
  712. c->irqs = &z8530_sync;
  713. /* This loads the double buffer up */
  714. z8530_rx_done(c); /* Load the frame ring */
  715. z8530_rx_done(c); /* Load the backup frame */
  716. z8530_rtsdtr(c,1);
  717. c->dma_tx = 0;
  718. c->regs[R1]|=TxINT_ENAB;
  719. write_zsreg(c, R1, c->regs[R1]);
  720. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  721. spin_unlock_irqrestore(c->lock, flags);
  722. return 0;
  723. }
  724. EXPORT_SYMBOL(z8530_sync_open);
  725. /**
  726. * z8530_sync_close - Close a PIO Z8530 channel
  727. * @dev: Network device to close
  728. * @c: Z8530 channel to disassociate and move to idle
  729. *
  730. * Close down a Z8530 interface and switch its interrupt handlers
  731. * to discard future events.
  732. */
  733. int z8530_sync_close(struct net_device *dev, struct z8530_channel *c)
  734. {
  735. u8 chk;
  736. unsigned long flags;
  737. spin_lock_irqsave(c->lock, flags);
  738. c->irqs = &z8530_nop;
  739. c->max = 0;
  740. c->sync = 0;
  741. chk=read_zsreg(c,R0);
  742. write_zsreg(c, R3, c->regs[R3]);
  743. z8530_rtsdtr(c,0);
  744. spin_unlock_irqrestore(c->lock, flags);
  745. return 0;
  746. }
  747. EXPORT_SYMBOL(z8530_sync_close);
  748. /**
  749. * z8530_sync_dma_open - Open a Z8530 for DMA I/O
  750. * @dev: The network device to attach
  751. * @c: The Z8530 channel to configure in sync DMA mode.
  752. *
  753. * Set up a Z85x30 device for synchronous DMA in both directions. Two
  754. * ISA DMA channels must be available for this to work. We assume ISA
  755. * DMA driven I/O and PC limits on access.
  756. */
  757. int z8530_sync_dma_open(struct net_device *dev, struct z8530_channel *c)
  758. {
  759. unsigned long cflags, dflags;
  760. c->sync = 1;
  761. c->mtu = dev->mtu+64;
  762. c->count = 0;
  763. c->skb = NULL;
  764. c->skb2 = NULL;
  765. /*
  766. * Load the DMA interfaces up
  767. */
  768. c->rxdma_on = 0;
  769. c->txdma_on = 0;
  770. /*
  771. * Allocate the DMA flip buffers. Limit by page size.
  772. * Everyone runs 1500 mtu or less on wan links so this
  773. * should be fine.
  774. */
  775. if(c->mtu > PAGE_SIZE/2)
  776. return -EMSGSIZE;
  777. c->rx_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  778. if(c->rx_buf[0]==NULL)
  779. return -ENOBUFS;
  780. c->rx_buf[1]=c->rx_buf[0]+PAGE_SIZE/2;
  781. c->tx_dma_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  782. if(c->tx_dma_buf[0]==NULL)
  783. {
  784. free_page((unsigned long)c->rx_buf[0]);
  785. c->rx_buf[0]=NULL;
  786. return -ENOBUFS;
  787. }
  788. c->tx_dma_buf[1]=c->tx_dma_buf[0]+PAGE_SIZE/2;
  789. c->tx_dma_used=0;
  790. c->dma_tx = 1;
  791. c->dma_num=0;
  792. c->dma_ready=1;
  793. /*
  794. * Enable DMA control mode
  795. */
  796. spin_lock_irqsave(c->lock, cflags);
  797. /*
  798. * TX DMA via DIR/REQ
  799. */
  800. c->regs[R14]|= DTRREQ;
  801. write_zsreg(c, R14, c->regs[R14]);
  802. c->regs[R1]&= ~TxINT_ENAB;
  803. write_zsreg(c, R1, c->regs[R1]);
  804. /*
  805. * RX DMA via W/Req
  806. */
  807. c->regs[R1]|= WT_FN_RDYFN;
  808. c->regs[R1]|= WT_RDY_RT;
  809. c->regs[R1]|= INT_ERR_Rx;
  810. c->regs[R1]&= ~TxINT_ENAB;
  811. write_zsreg(c, R1, c->regs[R1]);
  812. c->regs[R1]|= WT_RDY_ENAB;
  813. write_zsreg(c, R1, c->regs[R1]);
  814. /*
  815. * DMA interrupts
  816. */
  817. /*
  818. * Set up the DMA configuration
  819. */
  820. dflags=claim_dma_lock();
  821. disable_dma(c->rxdma);
  822. clear_dma_ff(c->rxdma);
  823. set_dma_mode(c->rxdma, DMA_MODE_READ|0x10);
  824. set_dma_addr(c->rxdma, virt_to_bus(c->rx_buf[0]));
  825. set_dma_count(c->rxdma, c->mtu);
  826. enable_dma(c->rxdma);
  827. disable_dma(c->txdma);
  828. clear_dma_ff(c->txdma);
  829. set_dma_mode(c->txdma, DMA_MODE_WRITE);
  830. disable_dma(c->txdma);
  831. release_dma_lock(dflags);
  832. /*
  833. * Select the DMA interrupt handlers
  834. */
  835. c->rxdma_on = 1;
  836. c->txdma_on = 1;
  837. c->tx_dma_used = 1;
  838. c->irqs = &z8530_dma_sync;
  839. z8530_rtsdtr(c,1);
  840. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  841. spin_unlock_irqrestore(c->lock, cflags);
  842. return 0;
  843. }
  844. EXPORT_SYMBOL(z8530_sync_dma_open);
  845. /**
  846. * z8530_sync_dma_close - Close down DMA I/O
  847. * @dev: Network device to detach
  848. * @c: Z8530 channel to move into discard mode
  849. *
  850. * Shut down a DMA mode synchronous interface. Halt the DMA, and
  851. * free the buffers.
  852. */
  853. int z8530_sync_dma_close(struct net_device *dev, struct z8530_channel *c)
  854. {
  855. u8 chk;
  856. unsigned long flags;
  857. c->irqs = &z8530_nop;
  858. c->max = 0;
  859. c->sync = 0;
  860. /*
  861. * Disable the PC DMA channels
  862. */
  863. flags=claim_dma_lock();
  864. disable_dma(c->rxdma);
  865. clear_dma_ff(c->rxdma);
  866. c->rxdma_on = 0;
  867. disable_dma(c->txdma);
  868. clear_dma_ff(c->txdma);
  869. release_dma_lock(flags);
  870. c->txdma_on = 0;
  871. c->tx_dma_used = 0;
  872. spin_lock_irqsave(c->lock, flags);
  873. /*
  874. * Disable DMA control mode
  875. */
  876. c->regs[R1]&= ~WT_RDY_ENAB;
  877. write_zsreg(c, R1, c->regs[R1]);
  878. c->regs[R1]&= ~(WT_RDY_RT|WT_FN_RDYFN|INT_ERR_Rx);
  879. c->regs[R1]|= INT_ALL_Rx;
  880. write_zsreg(c, R1, c->regs[R1]);
  881. c->regs[R14]&= ~DTRREQ;
  882. write_zsreg(c, R14, c->regs[R14]);
  883. if(c->rx_buf[0])
  884. {
  885. free_page((unsigned long)c->rx_buf[0]);
  886. c->rx_buf[0]=NULL;
  887. }
  888. if(c->tx_dma_buf[0])
  889. {
  890. free_page((unsigned long)c->tx_dma_buf[0]);
  891. c->tx_dma_buf[0]=NULL;
  892. }
  893. chk=read_zsreg(c,R0);
  894. write_zsreg(c, R3, c->regs[R3]);
  895. z8530_rtsdtr(c,0);
  896. spin_unlock_irqrestore(c->lock, flags);
  897. return 0;
  898. }
  899. EXPORT_SYMBOL(z8530_sync_dma_close);
  900. /**
  901. * z8530_sync_txdma_open - Open a Z8530 for TX driven DMA
  902. * @dev: The network device to attach
  903. * @c: The Z8530 channel to configure in sync DMA mode.
  904. *
  905. * Set up a Z85x30 device for synchronous DMA tranmission. One
  906. * ISA DMA channel must be available for this to work. The receive
  907. * side is run in PIO mode, but then it has the bigger FIFO.
  908. */
  909. int z8530_sync_txdma_open(struct net_device *dev, struct z8530_channel *c)
  910. {
  911. unsigned long cflags, dflags;
  912. printk("Opening sync interface for TX-DMA\n");
  913. c->sync = 1;
  914. c->mtu = dev->mtu+64;
  915. c->count = 0;
  916. c->skb = NULL;
  917. c->skb2 = NULL;
  918. /*
  919. * Allocate the DMA flip buffers. Limit by page size.
  920. * Everyone runs 1500 mtu or less on wan links so this
  921. * should be fine.
  922. */
  923. if(c->mtu > PAGE_SIZE/2)
  924. return -EMSGSIZE;
  925. c->tx_dma_buf[0]=(void *)get_zeroed_page(GFP_KERNEL|GFP_DMA);
  926. if(c->tx_dma_buf[0]==NULL)
  927. return -ENOBUFS;
  928. c->tx_dma_buf[1] = c->tx_dma_buf[0] + PAGE_SIZE/2;
  929. spin_lock_irqsave(c->lock, cflags);
  930. /*
  931. * Load the PIO receive ring
  932. */
  933. z8530_rx_done(c);
  934. z8530_rx_done(c);
  935. /*
  936. * Load the DMA interfaces up
  937. */
  938. c->rxdma_on = 0;
  939. c->txdma_on = 0;
  940. c->tx_dma_used=0;
  941. c->dma_num=0;
  942. c->dma_ready=1;
  943. c->dma_tx = 1;
  944. /*
  945. * Enable DMA control mode
  946. */
  947. /*
  948. * TX DMA via DIR/REQ
  949. */
  950. c->regs[R14]|= DTRREQ;
  951. write_zsreg(c, R14, c->regs[R14]);
  952. c->regs[R1]&= ~TxINT_ENAB;
  953. write_zsreg(c, R1, c->regs[R1]);
  954. /*
  955. * Set up the DMA configuration
  956. */
  957. dflags = claim_dma_lock();
  958. disable_dma(c->txdma);
  959. clear_dma_ff(c->txdma);
  960. set_dma_mode(c->txdma, DMA_MODE_WRITE);
  961. disable_dma(c->txdma);
  962. release_dma_lock(dflags);
  963. /*
  964. * Select the DMA interrupt handlers
  965. */
  966. c->rxdma_on = 0;
  967. c->txdma_on = 1;
  968. c->tx_dma_used = 1;
  969. c->irqs = &z8530_txdma_sync;
  970. z8530_rtsdtr(c,1);
  971. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  972. spin_unlock_irqrestore(c->lock, cflags);
  973. return 0;
  974. }
  975. EXPORT_SYMBOL(z8530_sync_txdma_open);
  976. /**
  977. * z8530_sync_txdma_close - Close down a TX driven DMA channel
  978. * @dev: Network device to detach
  979. * @c: Z8530 channel to move into discard mode
  980. *
  981. * Shut down a DMA/PIO split mode synchronous interface. Halt the DMA,
  982. * and free the buffers.
  983. */
  984. int z8530_sync_txdma_close(struct net_device *dev, struct z8530_channel *c)
  985. {
  986. unsigned long dflags, cflags;
  987. u8 chk;
  988. spin_lock_irqsave(c->lock, cflags);
  989. c->irqs = &z8530_nop;
  990. c->max = 0;
  991. c->sync = 0;
  992. /*
  993. * Disable the PC DMA channels
  994. */
  995. dflags = claim_dma_lock();
  996. disable_dma(c->txdma);
  997. clear_dma_ff(c->txdma);
  998. c->txdma_on = 0;
  999. c->tx_dma_used = 0;
  1000. release_dma_lock(dflags);
  1001. /*
  1002. * Disable DMA control mode
  1003. */
  1004. c->regs[R1]&= ~WT_RDY_ENAB;
  1005. write_zsreg(c, R1, c->regs[R1]);
  1006. c->regs[R1]&= ~(WT_RDY_RT|WT_FN_RDYFN|INT_ERR_Rx);
  1007. c->regs[R1]|= INT_ALL_Rx;
  1008. write_zsreg(c, R1, c->regs[R1]);
  1009. c->regs[R14]&= ~DTRREQ;
  1010. write_zsreg(c, R14, c->regs[R14]);
  1011. if(c->tx_dma_buf[0])
  1012. {
  1013. free_page((unsigned long)c->tx_dma_buf[0]);
  1014. c->tx_dma_buf[0]=NULL;
  1015. }
  1016. chk=read_zsreg(c,R0);
  1017. write_zsreg(c, R3, c->regs[R3]);
  1018. z8530_rtsdtr(c,0);
  1019. spin_unlock_irqrestore(c->lock, cflags);
  1020. return 0;
  1021. }
  1022. EXPORT_SYMBOL(z8530_sync_txdma_close);
  1023. /*
  1024. * Name strings for Z8530 chips. SGI claim to have a 130, Zilog deny
  1025. * it exists...
  1026. */
  1027. static char *z8530_type_name[]={
  1028. "Z8530",
  1029. "Z85C30",
  1030. "Z85230"
  1031. };
  1032. /**
  1033. * z8530_describe - Uniformly describe a Z8530 port
  1034. * @dev: Z8530 device to describe
  1035. * @mapping: string holding mapping type (eg "I/O" or "Mem")
  1036. * @io: the port value in question
  1037. *
  1038. * Describe a Z8530 in a standard format. We must pass the I/O as
  1039. * the port offset isnt predictable. The main reason for this function
  1040. * is to try and get a common format of report.
  1041. */
  1042. void z8530_describe(struct z8530_dev *dev, char *mapping, unsigned long io)
  1043. {
  1044. printk(KERN_INFO "%s: %s found at %s 0x%lX, IRQ %d.\n",
  1045. dev->name,
  1046. z8530_type_name[dev->type],
  1047. mapping,
  1048. Z8530_PORT_OF(io),
  1049. dev->irq);
  1050. }
  1051. EXPORT_SYMBOL(z8530_describe);
  1052. /*
  1053. * Locked operation part of the z8530 init code
  1054. */
  1055. static inline int do_z8530_init(struct z8530_dev *dev)
  1056. {
  1057. /* NOP the interrupt handlers first - we might get a
  1058. floating IRQ transition when we reset the chip */
  1059. dev->chanA.irqs=&z8530_nop;
  1060. dev->chanB.irqs=&z8530_nop;
  1061. dev->chanA.dcdcheck=DCD;
  1062. dev->chanB.dcdcheck=DCD;
  1063. /* Reset the chip */
  1064. write_zsreg(&dev->chanA, R9, 0xC0);
  1065. udelay(200);
  1066. /* Now check its valid */
  1067. write_zsreg(&dev->chanA, R12, 0xAA);
  1068. if(read_zsreg(&dev->chanA, R12)!=0xAA)
  1069. return -ENODEV;
  1070. write_zsreg(&dev->chanA, R12, 0x55);
  1071. if(read_zsreg(&dev->chanA, R12)!=0x55)
  1072. return -ENODEV;
  1073. dev->type=Z8530;
  1074. /*
  1075. * See the application note.
  1076. */
  1077. write_zsreg(&dev->chanA, R15, 0x01);
  1078. /*
  1079. * If we can set the low bit of R15 then
  1080. * the chip is enhanced.
  1081. */
  1082. if(read_zsreg(&dev->chanA, R15)==0x01)
  1083. {
  1084. /* This C30 versus 230 detect is from Klaus Kudielka's dmascc */
  1085. /* Put a char in the fifo */
  1086. write_zsreg(&dev->chanA, R8, 0);
  1087. if(read_zsreg(&dev->chanA, R0)&Tx_BUF_EMP)
  1088. dev->type = Z85230; /* Has a FIFO */
  1089. else
  1090. dev->type = Z85C30; /* Z85C30, 1 byte FIFO */
  1091. }
  1092. /*
  1093. * The code assumes R7' and friends are
  1094. * off. Use write_zsext() for these and keep
  1095. * this bit clear.
  1096. */
  1097. write_zsreg(&dev->chanA, R15, 0);
  1098. /*
  1099. * At this point it looks like the chip is behaving
  1100. */
  1101. memcpy(dev->chanA.regs, reg_init, 16);
  1102. memcpy(dev->chanB.regs, reg_init ,16);
  1103. return 0;
  1104. }
  1105. /**
  1106. * z8530_init - Initialise a Z8530 device
  1107. * @dev: Z8530 device to initialise.
  1108. *
  1109. * Configure up a Z8530/Z85C30 or Z85230 chip. We check the device
  1110. * is present, identify the type and then program it to hopefully
  1111. * keep quite and behave. This matters a lot, a Z8530 in the wrong
  1112. * state will sometimes get into stupid modes generating 10Khz
  1113. * interrupt streams and the like.
  1114. *
  1115. * We set the interrupt handler up to discard any events, in case
  1116. * we get them during reset or setp.
  1117. *
  1118. * Return 0 for success, or a negative value indicating the problem
  1119. * in errno form.
  1120. */
  1121. int z8530_init(struct z8530_dev *dev)
  1122. {
  1123. unsigned long flags;
  1124. int ret;
  1125. /* Set up the chip level lock */
  1126. spin_lock_init(&dev->lock);
  1127. dev->chanA.lock = &dev->lock;
  1128. dev->chanB.lock = &dev->lock;
  1129. spin_lock_irqsave(&dev->lock, flags);
  1130. ret = do_z8530_init(dev);
  1131. spin_unlock_irqrestore(&dev->lock, flags);
  1132. return ret;
  1133. }
  1134. EXPORT_SYMBOL(z8530_init);
  1135. /**
  1136. * z8530_shutdown - Shutdown a Z8530 device
  1137. * @dev: The Z8530 chip to shutdown
  1138. *
  1139. * We set the interrupt handlers to silence any interrupts. We then
  1140. * reset the chip and wait 100uS to be sure the reset completed. Just
  1141. * in case the caller then tries to do stuff.
  1142. *
  1143. * This is called without the lock held
  1144. */
  1145. int z8530_shutdown(struct z8530_dev *dev)
  1146. {
  1147. unsigned long flags;
  1148. /* Reset the chip */
  1149. spin_lock_irqsave(&dev->lock, flags);
  1150. dev->chanA.irqs=&z8530_nop;
  1151. dev->chanB.irqs=&z8530_nop;
  1152. write_zsreg(&dev->chanA, R9, 0xC0);
  1153. /* We must lock the udelay, the chip is offlimits here */
  1154. udelay(100);
  1155. spin_unlock_irqrestore(&dev->lock, flags);
  1156. return 0;
  1157. }
  1158. EXPORT_SYMBOL(z8530_shutdown);
  1159. /**
  1160. * z8530_channel_load - Load channel data
  1161. * @c: Z8530 channel to configure
  1162. * @rtable: table of register, value pairs
  1163. * FIXME: ioctl to allow user uploaded tables
  1164. *
  1165. * Load a Z8530 channel up from the system data. We use +16 to
  1166. * indicate the "prime" registers. The value 255 terminates the
  1167. * table.
  1168. */
  1169. int z8530_channel_load(struct z8530_channel *c, u8 *rtable)
  1170. {
  1171. unsigned long flags;
  1172. spin_lock_irqsave(c->lock, flags);
  1173. while(*rtable!=255)
  1174. {
  1175. int reg=*rtable++;
  1176. if(reg>0x0F)
  1177. write_zsreg(c, R15, c->regs[15]|1);
  1178. write_zsreg(c, reg&0x0F, *rtable);
  1179. if(reg>0x0F)
  1180. write_zsreg(c, R15, c->regs[15]&~1);
  1181. c->regs[reg]=*rtable++;
  1182. }
  1183. c->rx_function=z8530_null_rx;
  1184. c->skb=NULL;
  1185. c->tx_skb=NULL;
  1186. c->tx_next_skb=NULL;
  1187. c->mtu=1500;
  1188. c->max=0;
  1189. c->count=0;
  1190. c->status=read_zsreg(c, R0);
  1191. c->sync=1;
  1192. write_zsreg(c, R3, c->regs[R3]|RxENABLE);
  1193. spin_unlock_irqrestore(c->lock, flags);
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL(z8530_channel_load);
  1197. /**
  1198. * z8530_tx_begin - Begin packet transmission
  1199. * @c: The Z8530 channel to kick
  1200. *
  1201. * This is the speed sensitive side of transmission. If we are called
  1202. * and no buffer is being transmitted we commence the next buffer. If
  1203. * nothing is queued we idle the sync.
  1204. *
  1205. * Note: We are handling this code path in the interrupt path, keep it
  1206. * fast or bad things will happen.
  1207. *
  1208. * Called with the lock held.
  1209. */
  1210. static void z8530_tx_begin(struct z8530_channel *c)
  1211. {
  1212. unsigned long flags;
  1213. if(c->tx_skb)
  1214. return;
  1215. c->tx_skb=c->tx_next_skb;
  1216. c->tx_next_skb=NULL;
  1217. c->tx_ptr=c->tx_next_ptr;
  1218. if(c->tx_skb==NULL)
  1219. {
  1220. /* Idle on */
  1221. if(c->dma_tx)
  1222. {
  1223. flags=claim_dma_lock();
  1224. disable_dma(c->txdma);
  1225. /*
  1226. * Check if we crapped out.
  1227. */
  1228. if(get_dma_residue(c->txdma))
  1229. {
  1230. c->stats.tx_dropped++;
  1231. c->stats.tx_fifo_errors++;
  1232. }
  1233. release_dma_lock(flags);
  1234. }
  1235. c->txcount=0;
  1236. }
  1237. else
  1238. {
  1239. c->txcount=c->tx_skb->len;
  1240. if(c->dma_tx)
  1241. {
  1242. /*
  1243. * FIXME. DMA is broken for the original 8530,
  1244. * on the older parts we need to set a flag and
  1245. * wait for a further TX interrupt to fire this
  1246. * stage off
  1247. */
  1248. flags=claim_dma_lock();
  1249. disable_dma(c->txdma);
  1250. /*
  1251. * These two are needed by the 8530/85C30
  1252. * and must be issued when idling.
  1253. */
  1254. if(c->dev->type!=Z85230)
  1255. {
  1256. write_zsctrl(c, RES_Tx_CRC);
  1257. write_zsctrl(c, RES_EOM_L);
  1258. }
  1259. write_zsreg(c, R10, c->regs[10]&~ABUNDER);
  1260. clear_dma_ff(c->txdma);
  1261. set_dma_addr(c->txdma, virt_to_bus(c->tx_ptr));
  1262. set_dma_count(c->txdma, c->txcount);
  1263. enable_dma(c->txdma);
  1264. release_dma_lock(flags);
  1265. write_zsctrl(c, RES_EOM_L);
  1266. write_zsreg(c, R5, c->regs[R5]|TxENAB);
  1267. }
  1268. else
  1269. {
  1270. /* ABUNDER off */
  1271. write_zsreg(c, R10, c->regs[10]);
  1272. write_zsctrl(c, RES_Tx_CRC);
  1273. while(c->txcount && (read_zsreg(c,R0)&Tx_BUF_EMP))
  1274. {
  1275. write_zsreg(c, R8, *c->tx_ptr++);
  1276. c->txcount--;
  1277. }
  1278. }
  1279. }
  1280. /*
  1281. * Since we emptied tx_skb we can ask for more
  1282. */
  1283. netif_wake_queue(c->netdevice);
  1284. }
  1285. /**
  1286. * z8530_tx_done - TX complete callback
  1287. * @c: The channel that completed a transmit.
  1288. *
  1289. * This is called when we complete a packet send. We wake the queue,
  1290. * start the next packet going and then free the buffer of the existing
  1291. * packet. This code is fairly timing sensitive.
  1292. *
  1293. * Called with the register lock held.
  1294. */
  1295. static void z8530_tx_done(struct z8530_channel *c)
  1296. {
  1297. struct sk_buff *skb;
  1298. /* Actually this can happen.*/
  1299. if(c->tx_skb==NULL)
  1300. return;
  1301. skb=c->tx_skb;
  1302. c->tx_skb=NULL;
  1303. z8530_tx_begin(c);
  1304. c->stats.tx_packets++;
  1305. c->stats.tx_bytes+=skb->len;
  1306. dev_kfree_skb_irq(skb);
  1307. }
  1308. /**
  1309. * z8530_null_rx - Discard a packet
  1310. * @c: The channel the packet arrived on
  1311. * @skb: The buffer
  1312. *
  1313. * We point the receive handler at this function when idle. Instead
  1314. * of syncppp processing the frames we get to throw them away.
  1315. */
  1316. void z8530_null_rx(struct z8530_channel *c, struct sk_buff *skb)
  1317. {
  1318. dev_kfree_skb_any(skb);
  1319. }
  1320. EXPORT_SYMBOL(z8530_null_rx);
  1321. /**
  1322. * z8530_rx_done - Receive completion callback
  1323. * @c: The channel that completed a receive
  1324. *
  1325. * A new packet is complete. Our goal here is to get back into receive
  1326. * mode as fast as possible. On the Z85230 we could change to using
  1327. * ESCC mode, but on the older chips we have no choice. We flip to the
  1328. * new buffer immediately in DMA mode so that the DMA of the next
  1329. * frame can occur while we are copying the previous buffer to an sk_buff
  1330. *
  1331. * Called with the lock held
  1332. */
  1333. static void z8530_rx_done(struct z8530_channel *c)
  1334. {
  1335. struct sk_buff *skb;
  1336. int ct;
  1337. /*
  1338. * Is our receive engine in DMA mode
  1339. */
  1340. if(c->rxdma_on)
  1341. {
  1342. /*
  1343. * Save the ready state and the buffer currently
  1344. * being used as the DMA target
  1345. */
  1346. int ready=c->dma_ready;
  1347. unsigned char *rxb=c->rx_buf[c->dma_num];
  1348. unsigned long flags;
  1349. /*
  1350. * Complete this DMA. Neccessary to find the length
  1351. */
  1352. flags=claim_dma_lock();
  1353. disable_dma(c->rxdma);
  1354. clear_dma_ff(c->rxdma);
  1355. c->rxdma_on=0;
  1356. ct=c->mtu-get_dma_residue(c->rxdma);
  1357. if(ct<0)
  1358. ct=2; /* Shit happens.. */
  1359. c->dma_ready=0;
  1360. /*
  1361. * Normal case: the other slot is free, start the next DMA
  1362. * into it immediately.
  1363. */
  1364. if(ready)
  1365. {
  1366. c->dma_num^=1;
  1367. set_dma_mode(c->rxdma, DMA_MODE_READ|0x10);
  1368. set_dma_addr(c->rxdma, virt_to_bus(c->rx_buf[c->dma_num]));
  1369. set_dma_count(c->rxdma, c->mtu);
  1370. c->rxdma_on = 1;
  1371. enable_dma(c->rxdma);
  1372. /* Stop any frames that we missed the head of
  1373. from passing */
  1374. write_zsreg(c, R0, RES_Rx_CRC);
  1375. }
  1376. else
  1377. /* Can't occur as we dont reenable the DMA irq until
  1378. after the flip is done */
  1379. printk(KERN_WARNING "%s: DMA flip overrun!\n", c->netdevice->name);
  1380. release_dma_lock(flags);
  1381. /*
  1382. * Shove the old buffer into an sk_buff. We can't DMA
  1383. * directly into one on a PC - it might be above the 16Mb
  1384. * boundary. Optimisation - we could check to see if we
  1385. * can avoid the copy. Optimisation 2 - make the memcpy
  1386. * a copychecksum.
  1387. */
  1388. skb=dev_alloc_skb(ct);
  1389. if(skb==NULL)
  1390. {
  1391. c->stats.rx_dropped++;
  1392. printk(KERN_WARNING "%s: Memory squeeze.\n", c->netdevice->name);
  1393. }
  1394. else
  1395. {
  1396. skb_put(skb, ct);
  1397. memcpy(skb->data, rxb, ct);
  1398. c->stats.rx_packets++;
  1399. c->stats.rx_bytes+=ct;
  1400. }
  1401. c->dma_ready=1;
  1402. }
  1403. else
  1404. {
  1405. RT_LOCK;
  1406. skb=c->skb;
  1407. /*
  1408. * The game we play for non DMA is similar. We want to
  1409. * get the controller set up for the next packet as fast
  1410. * as possible. We potentially only have one byte + the
  1411. * fifo length for this. Thus we want to flip to the new
  1412. * buffer and then mess around copying and allocating
  1413. * things. For the current case it doesn't matter but
  1414. * if you build a system where the sync irq isnt blocked
  1415. * by the kernel IRQ disable then you need only block the
  1416. * sync IRQ for the RT_LOCK area.
  1417. *
  1418. */
  1419. ct=c->count;
  1420. c->skb = c->skb2;
  1421. c->count = 0;
  1422. c->max = c->mtu;
  1423. if(c->skb)
  1424. {
  1425. c->dptr = c->skb->data;
  1426. c->max = c->mtu;
  1427. }
  1428. else
  1429. {
  1430. c->count= 0;
  1431. c->max = 0;
  1432. }
  1433. RT_UNLOCK;
  1434. c->skb2 = dev_alloc_skb(c->mtu);
  1435. if(c->skb2==NULL)
  1436. printk(KERN_WARNING "%s: memory squeeze.\n",
  1437. c->netdevice->name);
  1438. else
  1439. {
  1440. skb_put(c->skb2,c->mtu);
  1441. }
  1442. c->stats.rx_packets++;
  1443. c->stats.rx_bytes+=ct;
  1444. }
  1445. /*
  1446. * If we received a frame we must now process it.
  1447. */
  1448. if(skb)
  1449. {
  1450. skb_trim(skb, ct);
  1451. c->rx_function(c,skb);
  1452. }
  1453. else
  1454. {
  1455. c->stats.rx_dropped++;
  1456. printk(KERN_ERR "%s: Lost a frame\n", c->netdevice->name);
  1457. }
  1458. }
  1459. /**
  1460. * spans_boundary - Check a packet can be ISA DMA'd
  1461. * @skb: The buffer to check
  1462. *
  1463. * Returns true if the buffer cross a DMA boundary on a PC. The poor
  1464. * thing can only DMA within a 64K block not across the edges of it.
  1465. */
  1466. static inline int spans_boundary(struct sk_buff *skb)
  1467. {
  1468. unsigned long a=(unsigned long)skb->data;
  1469. a^=(a+skb->len);
  1470. if(a&0x00010000) /* If the 64K bit is different.. */
  1471. return 1;
  1472. return 0;
  1473. }
  1474. /**
  1475. * z8530_queue_xmit - Queue a packet
  1476. * @c: The channel to use
  1477. * @skb: The packet to kick down the channel
  1478. *
  1479. * Queue a packet for transmission. Because we have rather
  1480. * hard to hit interrupt latencies for the Z85230 per packet
  1481. * even in DMA mode we do the flip to DMA buffer if needed here
  1482. * not in the IRQ.
  1483. *
  1484. * Called from the network code. The lock is not held at this
  1485. * point.
  1486. */
  1487. int z8530_queue_xmit(struct z8530_channel *c, struct sk_buff *skb)
  1488. {
  1489. unsigned long flags;
  1490. netif_stop_queue(c->netdevice);
  1491. if(c->tx_next_skb)
  1492. {
  1493. return 1;
  1494. }
  1495. /* PC SPECIFIC - DMA limits */
  1496. /*
  1497. * If we will DMA the transmit and its gone over the ISA bus
  1498. * limit, then copy to the flip buffer
  1499. */
  1500. if(c->dma_tx && ((unsigned long)(virt_to_bus(skb->data+skb->len))>=16*1024*1024 || spans_boundary(skb)))
  1501. {
  1502. /*
  1503. * Send the flip buffer, and flip the flippy bit.
  1504. * We don't care which is used when just so long as
  1505. * we never use the same buffer twice in a row. Since
  1506. * only one buffer can be going out at a time the other
  1507. * has to be safe.
  1508. */
  1509. c->tx_next_ptr=c->tx_dma_buf[c->tx_dma_used];
  1510. c->tx_dma_used^=1; /* Flip temp buffer */
  1511. memcpy(c->tx_next_ptr, skb->data, skb->len);
  1512. }
  1513. else
  1514. c->tx_next_ptr=skb->data;
  1515. RT_LOCK;
  1516. c->tx_next_skb=skb;
  1517. RT_UNLOCK;
  1518. spin_lock_irqsave(c->lock, flags);
  1519. z8530_tx_begin(c);
  1520. spin_unlock_irqrestore(c->lock, flags);
  1521. return 0;
  1522. }
  1523. EXPORT_SYMBOL(z8530_queue_xmit);
  1524. /**
  1525. * z8530_get_stats - Get network statistics
  1526. * @c: The channel to use
  1527. *
  1528. * Get the statistics block. We keep the statistics in software as
  1529. * the chip doesn't do it for us.
  1530. *
  1531. * Locking is ignored here - we could lock for a copy but its
  1532. * not likely to be that big an issue
  1533. */
  1534. struct net_device_stats *z8530_get_stats(struct z8530_channel *c)
  1535. {
  1536. return &c->stats;
  1537. }
  1538. EXPORT_SYMBOL(z8530_get_stats);
  1539. /*
  1540. * Module support
  1541. */
  1542. static char banner[] __initdata = KERN_INFO "Generic Z85C30/Z85230 interface driver v0.02\n";
  1543. static int __init z85230_init_driver(void)
  1544. {
  1545. printk(banner);
  1546. return 0;
  1547. }
  1548. module_init(z85230_init_driver);
  1549. static void __exit z85230_cleanup_driver(void)
  1550. {
  1551. }
  1552. module_exit(z85230_cleanup_driver);
  1553. MODULE_AUTHOR("Red Hat Inc.");
  1554. MODULE_DESCRIPTION("Z85x30 synchronous driver core");
  1555. MODULE_LICENSE("GPL");