z85230.c 39 KB

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