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