z85230.c 39 KB

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