z85230.c 39 KB

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