z85230.c 39 KB

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