z85230.c 39 KB

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