sync_serial.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * Simple synchronous serial port driver for ETRAX FS.
  3. *
  4. * Copyright (c) 2005 Axis Communications AB
  5. *
  6. * Author: Mikael Starvik
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/config.h>
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/major.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/poll.h>
  19. #include <linux/init.h>
  20. #include <linux/timer.h>
  21. #include <linux/spinlock.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/dma.h>
  24. #include <asm/arch/pinmux.h>
  25. #include <asm/arch/hwregs/reg_rdwr.h>
  26. #include <asm/arch/hwregs/sser_defs.h>
  27. #include <asm/arch/hwregs/dma_defs.h>
  28. #include <asm/arch/hwregs/dma.h>
  29. #include <asm/arch/hwregs/intr_vect_defs.h>
  30. #include <asm/arch/hwregs/intr_vect.h>
  31. #include <asm/arch/hwregs/reg_map.h>
  32. #include <asm/sync_serial.h>
  33. /* The receiver is a bit tricky beacuse of the continuous stream of data.*/
  34. /* */
  35. /* Three DMA descriptors are linked together. Each DMA descriptor is */
  36. /* responsible for port->bufchunk of a common buffer. */
  37. /* */
  38. /* +---------------------------------------------+ */
  39. /* | +----------+ +----------+ +----------+ | */
  40. /* +-> | Descr[0] |-->| Descr[1] |-->| Descr[2] |-+ */
  41. /* +----------+ +----------+ +----------+ */
  42. /* | | | */
  43. /* v v v */
  44. /* +-------------------------------------+ */
  45. /* | BUFFER | */
  46. /* +-------------------------------------+ */
  47. /* |<- data_avail ->| */
  48. /* readp writep */
  49. /* */
  50. /* If the application keeps up the pace readp will be right after writep.*/
  51. /* If the application can't keep the pace we have to throw away data. */
  52. /* The idea is that readp should be ready with the data pointed out by */
  53. /* Descr[i] when the DMA has filled in Descr[i+1]. */
  54. /* Otherwise we will discard */
  55. /* the rest of the data pointed out by Descr1 and set readp to the start */
  56. /* of Descr2 */
  57. #define SYNC_SERIAL_MAJOR 125
  58. /* IN_BUFFER_SIZE should be a multiple of 6 to make sure that 24 bit */
  59. /* words can be handled */
  60. #define IN_BUFFER_SIZE 12288
  61. #define IN_DESCR_SIZE 256
  62. #define NUM_IN_DESCR (IN_BUFFER_SIZE/IN_DESCR_SIZE)
  63. #define OUT_BUFFER_SIZE 4096
  64. #define DEFAULT_FRAME_RATE 0
  65. #define DEFAULT_WORD_RATE 7
  66. /* NOTE: Enabling some debug will likely cause overrun or underrun,
  67. * especially if manual mode is use.
  68. */
  69. #define DEBUG(x)
  70. #define DEBUGREAD(x)
  71. #define DEBUGWRITE(x)
  72. #define DEBUGPOLL(x)
  73. #define DEBUGRXINT(x)
  74. #define DEBUGTXINT(x)
  75. typedef struct sync_port
  76. {
  77. reg_scope_instances regi_sser;
  78. reg_scope_instances regi_dmain;
  79. reg_scope_instances regi_dmaout;
  80. char started; /* 1 if port has been started */
  81. char port_nbr; /* Port 0 or 1 */
  82. char busy; /* 1 if port is busy */
  83. char enabled; /* 1 if port is enabled */
  84. char use_dma; /* 1 if port uses dma */
  85. char tr_running;
  86. char init_irqs;
  87. int output;
  88. int input;
  89. volatile unsigned int out_count; /* Remaining bytes for current transfer */
  90. unsigned char* outp; /* Current position in out_buffer */
  91. volatile unsigned char* volatile readp; /* Next byte to be read by application */
  92. volatile unsigned char* volatile writep; /* Next byte to be written by etrax */
  93. unsigned int in_buffer_size;
  94. unsigned int inbufchunk;
  95. unsigned char out_buffer[OUT_BUFFER_SIZE] __attribute__ ((aligned(32)));
  96. unsigned char in_buffer[IN_BUFFER_SIZE]__attribute__ ((aligned(32)));
  97. unsigned char flip[IN_BUFFER_SIZE] __attribute__ ((aligned(32)));
  98. struct dma_descr_data* next_rx_desc;
  99. struct dma_descr_data* prev_rx_desc;
  100. int full;
  101. dma_descr_data in_descr[NUM_IN_DESCR] __attribute__ ((__aligned__(16)));
  102. dma_descr_context in_context __attribute__ ((__aligned__(32)));
  103. dma_descr_data out_descr __attribute__ ((__aligned__(16)));
  104. dma_descr_context out_context __attribute__ ((__aligned__(32)));
  105. wait_queue_head_t out_wait_q;
  106. wait_queue_head_t in_wait_q;
  107. spinlock_t lock;
  108. } sync_port;
  109. static int etrax_sync_serial_init(void);
  110. static void initialize_port(int portnbr);
  111. static inline int sync_data_avail(struct sync_port *port);
  112. static int sync_serial_open(struct inode *, struct file*);
  113. static int sync_serial_release(struct inode*, struct file*);
  114. static unsigned int sync_serial_poll(struct file *filp, poll_table *wait);
  115. static int sync_serial_ioctl(struct inode*, struct file*,
  116. unsigned int cmd, unsigned long arg);
  117. static ssize_t sync_serial_write(struct file * file, const char * buf,
  118. size_t count, loff_t *ppos);
  119. static ssize_t sync_serial_read(struct file *file, char *buf,
  120. size_t count, loff_t *ppos);
  121. #if (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
  122. defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
  123. (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
  124. defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA))
  125. #define SYNC_SER_DMA
  126. #endif
  127. static void send_word(sync_port* port);
  128. static void start_dma(struct sync_port *port, const char* data, int count);
  129. static void start_dma_in(sync_port* port);
  130. #ifdef SYNC_SER_DMA
  131. static irqreturn_t tr_interrupt(int irq, void *dev_id, struct pt_regs * regs);
  132. static irqreturn_t rx_interrupt(int irq, void *dev_id, struct pt_regs * regs);
  133. #endif
  134. #if (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
  135. !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
  136. (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
  137. !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA))
  138. #define SYNC_SER_MANUAL
  139. #endif
  140. #ifdef SYNC_SER_MANUAL
  141. static irqreturn_t manual_interrupt(int irq, void *dev_id, struct pt_regs * regs);
  142. #endif
  143. /* The ports */
  144. static struct sync_port ports[]=
  145. {
  146. {
  147. .regi_sser = regi_sser0,
  148. .regi_dmaout = regi_dma4,
  149. .regi_dmain = regi_dma5,
  150. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
  151. .use_dma = 1,
  152. #else
  153. .use_dma = 0,
  154. #endif
  155. },
  156. {
  157. .regi_sser = regi_sser1,
  158. .regi_dmaout = regi_dma6,
  159. .regi_dmain = regi_dma7,
  160. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
  161. .use_dma = 1,
  162. #else
  163. .use_dma = 0,
  164. #endif
  165. }
  166. };
  167. #define NUMBER_OF_PORTS (sizeof(ports)/sizeof(sync_port))
  168. static struct file_operations sync_serial_fops = {
  169. .owner = THIS_MODULE,
  170. .write = sync_serial_write,
  171. .read = sync_serial_read,
  172. .poll = sync_serial_poll,
  173. .ioctl = sync_serial_ioctl,
  174. .open = sync_serial_open,
  175. .release = sync_serial_release
  176. };
  177. static int __init etrax_sync_serial_init(void)
  178. {
  179. ports[0].enabled = 0;
  180. ports[1].enabled = 0;
  181. if (register_chrdev(SYNC_SERIAL_MAJOR,"sync serial", &sync_serial_fops) <0 )
  182. {
  183. printk("unable to get major for synchronous serial port\n");
  184. return -EBUSY;
  185. }
  186. /* Initialize Ports */
  187. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  188. if (crisv32_pinmux_alloc_fixed(pinmux_sser0))
  189. {
  190. printk("Unable to allocate pins for syncrhronous serial port 0\n");
  191. return -EIO;
  192. }
  193. ports[0].enabled = 1;
  194. initialize_port(0);
  195. #endif
  196. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
  197. if (crisv32_pinmux_alloc_fixed(pinmux_sser1))
  198. {
  199. printk("Unable to allocate pins for syncrhronous serial port 0\n");
  200. return -EIO;
  201. }
  202. ports[1].enabled = 1;
  203. initialize_port(1);
  204. #endif
  205. printk("ETRAX FS synchronous serial port driver\n");
  206. return 0;
  207. }
  208. static void __init initialize_port(int portnbr)
  209. {
  210. struct sync_port* port = &ports[portnbr];
  211. reg_sser_rw_cfg cfg = {0};
  212. reg_sser_rw_frm_cfg frm_cfg = {0};
  213. reg_sser_rw_tr_cfg tr_cfg = {0};
  214. reg_sser_rw_rec_cfg rec_cfg = {0};
  215. DEBUG(printk("Init sync serial port %d\n", portnbr));
  216. port->port_nbr = portnbr;
  217. port->init_irqs = 1;
  218. port->outp = port->out_buffer;
  219. port->output = 1;
  220. port->input = 0;
  221. port->readp = port->flip;
  222. port->writep = port->flip;
  223. port->in_buffer_size = IN_BUFFER_SIZE;
  224. port->inbufchunk = IN_DESCR_SIZE;
  225. port->next_rx_desc = &port->in_descr[0];
  226. port->prev_rx_desc = &port->in_descr[NUM_IN_DESCR-1];
  227. port->prev_rx_desc->eol = 1;
  228. init_waitqueue_head(&port->out_wait_q);
  229. init_waitqueue_head(&port->in_wait_q);
  230. spin_lock_init(&port->lock);
  231. cfg.out_clk_src = regk_sser_intern_clk;
  232. cfg.out_clk_pol = regk_sser_pos;
  233. cfg.clk_od_mode = regk_sser_no;
  234. cfg.clk_dir = regk_sser_out;
  235. cfg.gate_clk = regk_sser_no;
  236. cfg.base_freq = regk_sser_f29_493;
  237. cfg.clk_div = 256;
  238. REG_WR(sser, port->regi_sser, rw_cfg, cfg);
  239. frm_cfg.wordrate = DEFAULT_WORD_RATE;
  240. frm_cfg.type = regk_sser_edge;
  241. frm_cfg.frame_pin_dir = regk_sser_out;
  242. frm_cfg.frame_pin_use = regk_sser_frm;
  243. frm_cfg.status_pin_dir = regk_sser_in;
  244. frm_cfg.status_pin_use = regk_sser_hold;
  245. frm_cfg.out_on = regk_sser_tr;
  246. frm_cfg.tr_delay = 1;
  247. REG_WR(sser, port->regi_sser, rw_frm_cfg, frm_cfg);
  248. tr_cfg.urun_stop = regk_sser_no;
  249. tr_cfg.sample_size = 7;
  250. tr_cfg.sh_dir = regk_sser_msbfirst;
  251. tr_cfg.use_dma = port->use_dma ? regk_sser_yes : regk_sser_no;
  252. tr_cfg.rate_ctrl = regk_sser_bulk;
  253. tr_cfg.data_pin_use = regk_sser_dout;
  254. tr_cfg.bulk_wspace = 1;
  255. REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  256. rec_cfg.sample_size = 7;
  257. rec_cfg.sh_dir = regk_sser_msbfirst;
  258. rec_cfg.use_dma = port->use_dma ? regk_sser_yes : regk_sser_no;
  259. rec_cfg.fifo_thr = regk_sser_inf;
  260. REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  261. }
  262. static inline int sync_data_avail(struct sync_port *port)
  263. {
  264. int avail;
  265. unsigned char *start;
  266. unsigned char *end;
  267. start = (unsigned char*)port->readp; /* cast away volatile */
  268. end = (unsigned char*)port->writep; /* cast away volatile */
  269. /* 0123456789 0123456789
  270. * ----- - -----
  271. * ^rp ^wp ^wp ^rp
  272. */
  273. if (end >= start)
  274. avail = end - start;
  275. else
  276. avail = port->in_buffer_size - (start - end);
  277. return avail;
  278. }
  279. static inline int sync_data_avail_to_end(struct sync_port *port)
  280. {
  281. int avail;
  282. unsigned char *start;
  283. unsigned char *end;
  284. start = (unsigned char*)port->readp; /* cast away volatile */
  285. end = (unsigned char*)port->writep; /* cast away volatile */
  286. /* 0123456789 0123456789
  287. * ----- -----
  288. * ^rp ^wp ^wp ^rp
  289. */
  290. if (end >= start)
  291. avail = end - start;
  292. else
  293. avail = port->flip + port->in_buffer_size - start;
  294. return avail;
  295. }
  296. static int sync_serial_open(struct inode *inode, struct file *file)
  297. {
  298. int dev = MINOR(inode->i_rdev);
  299. sync_port* port;
  300. reg_dma_rw_cfg cfg = {.en = regk_dma_yes};
  301. reg_dma_rw_intr_mask intr_mask = {.data = regk_dma_yes};
  302. DEBUG(printk("Open sync serial port %d\n", dev));
  303. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled)
  304. {
  305. DEBUG(printk("Invalid minor %d\n", dev));
  306. return -ENODEV;
  307. }
  308. port = &ports[dev];
  309. /* Allow open this device twice (assuming one reader and one writer) */
  310. if (port->busy == 2)
  311. {
  312. DEBUG(printk("Device is busy.. \n"));
  313. return -EBUSY;
  314. }
  315. if (port->init_irqs) {
  316. if (port->use_dma) {
  317. if (port == &ports[0]){
  318. #ifdef SYNC_SER_DMA
  319. if(request_irq(DMA4_INTR_VECT,
  320. tr_interrupt,
  321. 0,
  322. "synchronous serial 0 dma tr",
  323. &ports[0])) {
  324. printk(KERN_CRIT "Can't allocate sync serial port 0 IRQ");
  325. return -EBUSY;
  326. } else if(request_irq(DMA5_INTR_VECT,
  327. rx_interrupt,
  328. 0,
  329. "synchronous serial 1 dma rx",
  330. &ports[0])) {
  331. free_irq(DMA4_INTR_VECT, &port[0]);
  332. printk(KERN_CRIT "Can't allocate sync serial port 0 IRQ");
  333. return -EBUSY;
  334. } else if (crisv32_request_dma(SYNC_SER0_TX_DMA_NBR,
  335. "synchronous serial 0 dma tr",
  336. DMA_VERBOSE_ON_ERROR,
  337. 0,
  338. dma_sser0)) {
  339. free_irq(DMA4_INTR_VECT, &port[0]);
  340. free_irq(DMA5_INTR_VECT, &port[0]);
  341. printk(KERN_CRIT "Can't allocate sync serial port 0 TX DMA channel");
  342. return -EBUSY;
  343. } else if (crisv32_request_dma(SYNC_SER0_RX_DMA_NBR,
  344. "synchronous serial 0 dma rec",
  345. DMA_VERBOSE_ON_ERROR,
  346. 0,
  347. dma_sser0)) {
  348. crisv32_free_dma(SYNC_SER0_TX_DMA_NBR);
  349. free_irq(DMA4_INTR_VECT, &port[0]);
  350. free_irq(DMA5_INTR_VECT, &port[0]);
  351. printk(KERN_CRIT "Can't allocate sync serial port 1 RX DMA channel");
  352. return -EBUSY;
  353. }
  354. #endif
  355. }
  356. else if (port == &ports[1]){
  357. #ifdef SYNC_SER_DMA
  358. if (request_irq(DMA6_INTR_VECT,
  359. tr_interrupt,
  360. 0,
  361. "synchronous serial 1 dma tr",
  362. &ports[1])) {
  363. printk(KERN_CRIT "Can't allocate sync serial port 1 IRQ");
  364. return -EBUSY;
  365. } else if (request_irq(DMA7_INTR_VECT,
  366. rx_interrupt,
  367. 0,
  368. "synchronous serial 1 dma rx",
  369. &ports[1])) {
  370. free_irq(DMA6_INTR_VECT, &ports[1]);
  371. printk(KERN_CRIT "Can't allocate sync serial port 3 IRQ");
  372. return -EBUSY;
  373. } else if (crisv32_request_dma(SYNC_SER1_TX_DMA_NBR,
  374. "synchronous serial 1 dma tr",
  375. DMA_VERBOSE_ON_ERROR,
  376. 0,
  377. dma_sser1)) {
  378. free_irq(21, &ports[1]);
  379. free_irq(20, &ports[1]);
  380. printk(KERN_CRIT "Can't allocate sync serial port 3 TX DMA channel");
  381. return -EBUSY;
  382. } else if (crisv32_request_dma(SYNC_SER1_RX_DMA_NBR,
  383. "synchronous serial 3 dma rec",
  384. DMA_VERBOSE_ON_ERROR,
  385. 0,
  386. dma_sser1)) {
  387. crisv32_free_dma(SYNC_SER1_TX_DMA_NBR);
  388. free_irq(DMA6_INTR_VECT, &ports[1]);
  389. free_irq(DMA7_INTR_VECT, &ports[1]);
  390. printk(KERN_CRIT "Can't allocate sync serial port 3 RX DMA channel");
  391. return -EBUSY;
  392. }
  393. #endif
  394. }
  395. /* Enable DMAs */
  396. REG_WR(dma, port->regi_dmain, rw_cfg, cfg);
  397. REG_WR(dma, port->regi_dmaout, rw_cfg, cfg);
  398. /* Enable DMA IRQs */
  399. REG_WR(dma, port->regi_dmain, rw_intr_mask, intr_mask);
  400. REG_WR(dma, port->regi_dmaout, rw_intr_mask, intr_mask);
  401. /* Set up wordsize = 2 for DMAs. */
  402. DMA_WR_CMD (port->regi_dmain, regk_dma_set_w_size1);
  403. DMA_WR_CMD (port->regi_dmaout, regk_dma_set_w_size1);
  404. start_dma_in(port);
  405. port->init_irqs = 0;
  406. } else { /* !port->use_dma */
  407. #ifdef SYNC_SER_MANUAL
  408. if (port == &ports[0]) {
  409. if (request_irq(SSER0_INTR_VECT,
  410. manual_interrupt,
  411. 0,
  412. "synchronous serial manual irq",
  413. &ports[0])) {
  414. printk("Can't allocate sync serial manual irq");
  415. return -EBUSY;
  416. }
  417. } else if (port == &ports[1]) {
  418. if (request_irq(SSER1_INTR_VECT,
  419. manual_interrupt,
  420. 0,
  421. "synchronous serial manual irq",
  422. &ports[1])) {
  423. printk(KERN_CRIT "Can't allocate sync serial manual irq");
  424. return -EBUSY;
  425. }
  426. }
  427. port->init_irqs = 0;
  428. #else
  429. panic("sync_serial: Manual mode not supported.\n");
  430. #endif /* SYNC_SER_MANUAL */
  431. }
  432. } /* port->init_irqs */
  433. port->busy++;
  434. return 0;
  435. }
  436. static int sync_serial_release(struct inode *inode, struct file *file)
  437. {
  438. int dev = MINOR(inode->i_rdev);
  439. sync_port* port;
  440. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled)
  441. {
  442. DEBUG(printk("Invalid minor %d\n", dev));
  443. return -ENODEV;
  444. }
  445. port = &ports[dev];
  446. if (port->busy)
  447. port->busy--;
  448. if (!port->busy)
  449. /* XXX */ ;
  450. return 0;
  451. }
  452. static unsigned int sync_serial_poll(struct file *file, poll_table *wait)
  453. {
  454. int dev = MINOR(file->f_dentry->d_inode->i_rdev);
  455. unsigned int mask = 0;
  456. sync_port* port;
  457. DEBUGPOLL( static unsigned int prev_mask = 0; );
  458. port = &ports[dev];
  459. poll_wait(file, &port->out_wait_q, wait);
  460. poll_wait(file, &port->in_wait_q, wait);
  461. /* Some room to write */
  462. if (port->out_count < OUT_BUFFER_SIZE)
  463. mask |= POLLOUT | POLLWRNORM;
  464. /* At least an inbufchunk of data */
  465. if (sync_data_avail(port) >= port->inbufchunk)
  466. mask |= POLLIN | POLLRDNORM;
  467. DEBUGPOLL(if (mask != prev_mask)
  468. printk("sync_serial_poll: mask 0x%08X %s %s\n", mask,
  469. mask&POLLOUT?"POLLOUT":"", mask&POLLIN?"POLLIN":"");
  470. prev_mask = mask;
  471. );
  472. return mask;
  473. }
  474. static int sync_serial_ioctl(struct inode *inode, struct file *file,
  475. unsigned int cmd, unsigned long arg)
  476. {
  477. int return_val = 0;
  478. int dev = MINOR(file->f_dentry->d_inode->i_rdev);
  479. sync_port* port;
  480. reg_sser_rw_tr_cfg tr_cfg;
  481. reg_sser_rw_rec_cfg rec_cfg;
  482. reg_sser_rw_frm_cfg frm_cfg;
  483. reg_sser_rw_cfg gen_cfg;
  484. reg_sser_rw_intr_mask intr_mask;
  485. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled)
  486. {
  487. DEBUG(printk("Invalid minor %d\n", dev));
  488. return -1;
  489. }
  490. port = &ports[dev];
  491. spin_lock_irq(&port->lock);
  492. tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  493. rec_cfg = REG_RD(sser, port->regi_sser, rw_rec_cfg);
  494. frm_cfg = REG_RD(sser, port->regi_sser, rw_frm_cfg);
  495. gen_cfg = REG_RD(sser, port->regi_sser, rw_cfg);
  496. intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
  497. switch(cmd)
  498. {
  499. case SSP_SPEED:
  500. if (GET_SPEED(arg) == CODEC)
  501. {
  502. gen_cfg.base_freq = regk_sser_f32;
  503. /* FREQ = 0 => 4 MHz => clk_div = 7*/
  504. gen_cfg.clk_div = 6 + (1 << GET_FREQ(arg));
  505. }
  506. else
  507. {
  508. gen_cfg.base_freq = regk_sser_f29_493;
  509. switch (GET_SPEED(arg))
  510. {
  511. case SSP150:
  512. gen_cfg.clk_div = 29493000 / (150 * 8) - 1;
  513. break;
  514. case SSP300:
  515. gen_cfg.clk_div = 29493000 / (300 * 8) - 1;
  516. break;
  517. case SSP600:
  518. gen_cfg.clk_div = 29493000 / (600 * 8) - 1;
  519. break;
  520. case SSP1200:
  521. gen_cfg.clk_div = 29493000 / (1200 * 8) - 1;
  522. break;
  523. case SSP2400:
  524. gen_cfg.clk_div = 29493000 / (2400 * 8) - 1;
  525. break;
  526. case SSP4800:
  527. gen_cfg.clk_div = 29493000 / (4800 * 8) - 1;
  528. break;
  529. case SSP9600:
  530. gen_cfg.clk_div = 29493000 / (9600 * 8) - 1;
  531. break;
  532. case SSP19200:
  533. gen_cfg.clk_div = 29493000 / (19200 * 8) - 1;
  534. break;
  535. case SSP28800:
  536. gen_cfg.clk_div = 29493000 / (28800 * 8) - 1;
  537. break;
  538. case SSP57600:
  539. gen_cfg.clk_div = 29493000 / (57600 * 8) - 1;
  540. break;
  541. case SSP115200:
  542. gen_cfg.clk_div = 29493000 / (115200 * 8) - 1;
  543. break;
  544. case SSP230400:
  545. gen_cfg.clk_div = 29493000 / (230400 * 8) - 1;
  546. break;
  547. case SSP460800:
  548. gen_cfg.clk_div = 29493000 / (460800 * 8) - 1;
  549. break;
  550. case SSP921600:
  551. gen_cfg.clk_div = 29493000 / (921600 * 8) - 1;
  552. break;
  553. case SSP3125000:
  554. gen_cfg.base_freq = regk_sser_f100;
  555. gen_cfg.clk_div = 100000000 / (3125000 * 8) - 1;
  556. break;
  557. }
  558. }
  559. frm_cfg.wordrate = GET_WORD_RATE(arg);
  560. break;
  561. case SSP_MODE:
  562. switch(arg)
  563. {
  564. case MASTER_OUTPUT:
  565. port->output = 1;
  566. port->input = 0;
  567. gen_cfg.clk_dir = regk_sser_out;
  568. break;
  569. case SLAVE_OUTPUT:
  570. port->output = 1;
  571. port->input = 0;
  572. gen_cfg.clk_dir = regk_sser_in;
  573. break;
  574. case MASTER_INPUT:
  575. port->output = 0;
  576. port->input = 1;
  577. gen_cfg.clk_dir = regk_sser_out;
  578. break;
  579. case SLAVE_INPUT:
  580. port->output = 0;
  581. port->input = 1;
  582. gen_cfg.clk_dir = regk_sser_in;
  583. break;
  584. case MASTER_BIDIR:
  585. port->output = 1;
  586. port->input = 1;
  587. gen_cfg.clk_dir = regk_sser_out;
  588. break;
  589. case SLAVE_BIDIR:
  590. port->output = 1;
  591. port->input = 1;
  592. gen_cfg.clk_dir = regk_sser_in;
  593. break;
  594. default:
  595. spin_unlock_irq(&port->lock);
  596. return -EINVAL;
  597. }
  598. if (!port->use_dma || (arg == MASTER_OUTPUT || arg == SLAVE_OUTPUT))
  599. intr_mask.rdav = regk_sser_yes;
  600. break;
  601. case SSP_FRAME_SYNC:
  602. if (arg & NORMAL_SYNC)
  603. frm_cfg.tr_delay = 1;
  604. else if (arg & EARLY_SYNC)
  605. frm_cfg.tr_delay = 0;
  606. tr_cfg.bulk_wspace = frm_cfg.tr_delay;
  607. frm_cfg.early_wend = regk_sser_yes;
  608. if (arg & BIT_SYNC)
  609. frm_cfg.type = regk_sser_edge;
  610. else if (arg & WORD_SYNC)
  611. frm_cfg.type = regk_sser_level;
  612. else if (arg & EXTENDED_SYNC)
  613. frm_cfg.early_wend = regk_sser_no;
  614. if (arg & SYNC_ON)
  615. frm_cfg.frame_pin_use = regk_sser_frm;
  616. else if (arg & SYNC_OFF)
  617. frm_cfg.frame_pin_use = regk_sser_gio0;
  618. if (arg & WORD_SIZE_8)
  619. rec_cfg.sample_size = tr_cfg.sample_size = 7;
  620. else if (arg & WORD_SIZE_12)
  621. rec_cfg.sample_size = tr_cfg.sample_size = 11;
  622. else if (arg & WORD_SIZE_16)
  623. rec_cfg.sample_size = tr_cfg.sample_size = 15;
  624. else if (arg & WORD_SIZE_24)
  625. rec_cfg.sample_size = tr_cfg.sample_size = 23;
  626. else if (arg & WORD_SIZE_32)
  627. rec_cfg.sample_size = tr_cfg.sample_size = 31;
  628. if (arg & BIT_ORDER_MSB)
  629. rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_msbfirst;
  630. else if (arg & BIT_ORDER_LSB)
  631. rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_lsbfirst;
  632. if (arg & FLOW_CONTROL_ENABLE)
  633. rec_cfg.fifo_thr = regk_sser_thr16;
  634. else if (arg & FLOW_CONTROL_DISABLE)
  635. rec_cfg.fifo_thr = regk_sser_inf;
  636. if (arg & CLOCK_NOT_GATED)
  637. gen_cfg.gate_clk = regk_sser_no;
  638. else if (arg & CLOCK_GATED)
  639. gen_cfg.gate_clk = regk_sser_yes;
  640. break;
  641. case SSP_IPOLARITY:
  642. /* NOTE!! negedge is considered NORMAL */
  643. if (arg & CLOCK_NORMAL)
  644. rec_cfg.clk_pol = regk_sser_neg;
  645. else if (arg & CLOCK_INVERT)
  646. rec_cfg.clk_pol = regk_sser_pos;
  647. if (arg & FRAME_NORMAL)
  648. frm_cfg.level = regk_sser_pos_hi;
  649. else if (arg & FRAME_INVERT)
  650. frm_cfg.level = regk_sser_neg_lo;
  651. if (arg & STATUS_NORMAL)
  652. gen_cfg.hold_pol = regk_sser_pos;
  653. else if (arg & STATUS_INVERT)
  654. gen_cfg.hold_pol = regk_sser_neg;
  655. break;
  656. case SSP_OPOLARITY:
  657. if (arg & CLOCK_NORMAL)
  658. gen_cfg.out_clk_pol = regk_sser_neg;
  659. else if (arg & CLOCK_INVERT)
  660. gen_cfg.out_clk_pol = regk_sser_pos;
  661. if (arg & FRAME_NORMAL)
  662. frm_cfg.level = regk_sser_pos_hi;
  663. else if (arg & FRAME_INVERT)
  664. frm_cfg.level = regk_sser_neg_lo;
  665. if (arg & STATUS_NORMAL)
  666. gen_cfg.hold_pol = regk_sser_pos;
  667. else if (arg & STATUS_INVERT)
  668. gen_cfg.hold_pol = regk_sser_neg;
  669. break;
  670. case SSP_SPI:
  671. rec_cfg.fifo_thr = regk_sser_inf;
  672. rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_msbfirst;
  673. rec_cfg.sample_size = tr_cfg.sample_size = 7;
  674. frm_cfg.frame_pin_use = regk_sser_frm;
  675. frm_cfg.type = regk_sser_level;
  676. frm_cfg.tr_delay = 1;
  677. frm_cfg.level = regk_sser_neg_lo;
  678. if (arg & SPI_SLAVE)
  679. {
  680. rec_cfg.clk_pol = regk_sser_neg;
  681. gen_cfg.clk_dir = regk_sser_in;
  682. port->input = 1;
  683. port->output = 0;
  684. }
  685. else
  686. {
  687. gen_cfg.out_clk_pol = regk_sser_pos;
  688. port->input = 0;
  689. port->output = 1;
  690. gen_cfg.clk_dir = regk_sser_out;
  691. }
  692. break;
  693. case SSP_INBUFCHUNK:
  694. break;
  695. default:
  696. return_val = -1;
  697. }
  698. if (port->started)
  699. {
  700. tr_cfg.tr_en = port->output;
  701. rec_cfg.rec_en = port->input;
  702. }
  703. REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  704. REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  705. REG_WR(sser, port->regi_sser, rw_frm_cfg, frm_cfg);
  706. REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
  707. REG_WR(sser, port->regi_sser, rw_cfg, gen_cfg);
  708. spin_unlock_irq(&port->lock);
  709. return return_val;
  710. }
  711. static ssize_t sync_serial_write(struct file * file, const char * buf,
  712. size_t count, loff_t *ppos)
  713. {
  714. int dev = MINOR(file->f_dentry->d_inode->i_rdev);
  715. DECLARE_WAITQUEUE(wait, current);
  716. sync_port *port;
  717. unsigned long c, c1;
  718. unsigned long free_outp;
  719. unsigned long outp;
  720. unsigned long out_buffer;
  721. unsigned long flags;
  722. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled)
  723. {
  724. DEBUG(printk("Invalid minor %d\n", dev));
  725. return -ENODEV;
  726. }
  727. port = &ports[dev];
  728. DEBUGWRITE(printk("W d%d c %lu (%d/%d)\n", port->port_nbr, count, port->out_count, OUT_BUFFER_SIZE));
  729. /* Space to end of buffer */
  730. /*
  731. * out_buffer <c1>012345<- c ->OUT_BUFFER_SIZE
  732. * outp^ +out_count
  733. ^free_outp
  734. * out_buffer 45<- c ->0123OUT_BUFFER_SIZE
  735. * +out_count outp^
  736. * free_outp
  737. *
  738. */
  739. /* Read variables that may be updated by interrupts */
  740. spin_lock_irqsave(&port->lock, flags);
  741. count = count > OUT_BUFFER_SIZE - port->out_count ? OUT_BUFFER_SIZE - port->out_count : count;
  742. outp = (unsigned long)port->outp;
  743. free_outp = outp + port->out_count;
  744. spin_unlock_irqrestore(&port->lock, flags);
  745. out_buffer = (unsigned long)port->out_buffer;
  746. /* Find out where and how much to write */
  747. if (free_outp >= out_buffer + OUT_BUFFER_SIZE)
  748. free_outp -= OUT_BUFFER_SIZE;
  749. if (free_outp >= outp)
  750. c = out_buffer + OUT_BUFFER_SIZE - free_outp;
  751. else
  752. c = outp - free_outp;
  753. if (c > count)
  754. c = count;
  755. // DEBUGWRITE(printk("w op %08lX fop %08lX c %lu\n", outp, free_outp, c));
  756. if (copy_from_user((void*)free_outp, buf, c))
  757. return -EFAULT;
  758. if (c != count) {
  759. buf += c;
  760. c1 = count - c;
  761. DEBUGWRITE(printk("w2 fi %lu c %lu c1 %lu\n", free_outp-out_buffer, c, c1));
  762. if (copy_from_user((void*)out_buffer, buf, c1))
  763. return -EFAULT;
  764. }
  765. spin_lock_irqsave(&port->lock, flags);
  766. port->out_count += count;
  767. spin_unlock_irqrestore(&port->lock, flags);
  768. /* Make sure transmitter/receiver is running */
  769. if (!port->started)
  770. {
  771. reg_sser_rw_cfg cfg = REG_RD(sser, port->regi_sser, rw_cfg);
  772. reg_sser_rw_tr_cfg tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  773. reg_sser_rw_rec_cfg rec_cfg = REG_RD(sser, port->regi_sser, rw_rec_cfg);
  774. cfg.en = regk_sser_yes;
  775. tr_cfg.tr_en = port->output;
  776. rec_cfg.rec_en = port->input;
  777. REG_WR(sser, port->regi_sser, rw_cfg, cfg);
  778. REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  779. REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  780. port->started = 1;
  781. }
  782. if (file->f_flags & O_NONBLOCK) {
  783. spin_lock_irqsave(&port->lock, flags);
  784. if (!port->tr_running) {
  785. if (!port->use_dma) {
  786. reg_sser_rw_intr_mask intr_mask;
  787. intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
  788. /* Start sender by writing data */
  789. send_word(port);
  790. /* and enable transmitter ready IRQ */
  791. intr_mask.trdy = 1;
  792. REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
  793. } else {
  794. start_dma(port, (unsigned char* volatile )port->outp, c);
  795. }
  796. }
  797. spin_unlock_irqrestore(&port->lock, flags);
  798. DEBUGWRITE(printk("w d%d c %lu NB\n",
  799. port->port_nbr, count));
  800. return count;
  801. }
  802. /* Sleep until all sent */
  803. add_wait_queue(&port->out_wait_q, &wait);
  804. set_current_state(TASK_INTERRUPTIBLE);
  805. spin_lock_irqsave(&port->lock, flags);
  806. if (!port->tr_running) {
  807. if (!port->use_dma) {
  808. reg_sser_rw_intr_mask intr_mask;
  809. intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
  810. /* Start sender by writing data */
  811. send_word(port);
  812. /* and enable transmitter ready IRQ */
  813. intr_mask.trdy = 1;
  814. REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
  815. } else {
  816. start_dma(port, port->outp, c);
  817. }
  818. }
  819. spin_unlock_irqrestore(&port->lock, flags);
  820. schedule();
  821. set_current_state(TASK_RUNNING);
  822. remove_wait_queue(&port->out_wait_q, &wait);
  823. if (signal_pending(current))
  824. {
  825. return -EINTR;
  826. }
  827. DEBUGWRITE(printk("w d%d c %lu\n", port->port_nbr, count));
  828. return count;
  829. }
  830. static ssize_t sync_serial_read(struct file * file, char * buf,
  831. size_t count, loff_t *ppos)
  832. {
  833. int dev = MINOR(file->f_dentry->d_inode->i_rdev);
  834. int avail;
  835. sync_port *port;
  836. unsigned char* start;
  837. unsigned char* end;
  838. unsigned long flags;
  839. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled)
  840. {
  841. DEBUG(printk("Invalid minor %d\n", dev));
  842. return -ENODEV;
  843. }
  844. port = &ports[dev];
  845. DEBUGREAD(printk("R%d c %d ri %lu wi %lu /%lu\n", dev, count, port->readp - port->flip, port->writep - port->flip, port->in_buffer_size));
  846. if (!port->started)
  847. {
  848. reg_sser_rw_cfg cfg = REG_RD(sser, port->regi_sser, rw_cfg);
  849. reg_sser_rw_tr_cfg tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  850. reg_sser_rw_rec_cfg rec_cfg = REG_RD(sser, port->regi_sser, rw_rec_cfg);
  851. cfg.en = regk_sser_yes;
  852. tr_cfg.tr_en = regk_sser_yes;
  853. rec_cfg.rec_en = regk_sser_yes;
  854. REG_WR(sser, port->regi_sser, rw_cfg, cfg);
  855. REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  856. REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  857. port->started = 1;
  858. }
  859. /* Calculate number of available bytes */
  860. /* Save pointers to avoid that they are modified by interrupt */
  861. spin_lock_irqsave(&port->lock, flags);
  862. start = (unsigned char*)port->readp; /* cast away volatile */
  863. end = (unsigned char*)port->writep; /* cast away volatile */
  864. spin_unlock_irqrestore(&port->lock, flags);
  865. while ((start == end) && !port->full) /* No data */
  866. {
  867. if (file->f_flags & O_NONBLOCK)
  868. {
  869. return -EAGAIN;
  870. }
  871. interruptible_sleep_on(&port->in_wait_q);
  872. if (signal_pending(current))
  873. {
  874. return -EINTR;
  875. }
  876. spin_lock_irqsave(&port->lock, flags);
  877. start = (unsigned char*)port->readp; /* cast away volatile */
  878. end = (unsigned char*)port->writep; /* cast away volatile */
  879. spin_unlock_irqrestore(&port->lock, flags);
  880. }
  881. /* Lazy read, never return wrapped data. */
  882. if (port->full)
  883. avail = port->in_buffer_size;
  884. else if (end > start)
  885. avail = end - start;
  886. else
  887. avail = port->flip + port->in_buffer_size - start;
  888. count = count > avail ? avail : count;
  889. if (copy_to_user(buf, start, count))
  890. return -EFAULT;
  891. /* Disable interrupts while updating readp */
  892. spin_lock_irqsave(&port->lock, flags);
  893. port->readp += count;
  894. if (port->readp >= port->flip + port->in_buffer_size) /* Wrap? */
  895. port->readp = port->flip;
  896. port->full = 0;
  897. spin_unlock_irqrestore(&port->lock, flags);
  898. DEBUGREAD(printk("r %d\n", count));
  899. return count;
  900. }
  901. static void send_word(sync_port* port)
  902. {
  903. reg_sser_rw_tr_cfg tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  904. reg_sser_rw_tr_data tr_data = {0};
  905. switch(tr_cfg.sample_size)
  906. {
  907. case 8:
  908. port->out_count--;
  909. tr_data.data = *port->outp++;
  910. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  911. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  912. port->outp = port->out_buffer;
  913. break;
  914. case 12:
  915. {
  916. int data = (*port->outp++) << 8;
  917. data |= *port->outp++;
  918. port->out_count-=2;
  919. tr_data.data = data;
  920. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  921. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  922. port->outp = port->out_buffer;
  923. }
  924. break;
  925. case 16:
  926. port->out_count-=2;
  927. tr_data.data = *(unsigned short *)port->outp;
  928. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  929. port->outp+=2;
  930. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  931. port->outp = port->out_buffer;
  932. break;
  933. case 24:
  934. port->out_count-=3;
  935. tr_data.data = *(unsigned short *)port->outp;
  936. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  937. port->outp+=2;
  938. tr_data.data = *port->outp++;
  939. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  940. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  941. port->outp = port->out_buffer;
  942. break;
  943. case 32:
  944. port->out_count-=4;
  945. tr_data.data = *(unsigned short *)port->outp;
  946. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  947. port->outp+=2;
  948. tr_data.data = *(unsigned short *)port->outp;
  949. REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  950. port->outp+=2;
  951. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  952. port->outp = port->out_buffer;
  953. break;
  954. }
  955. }
  956. static void start_dma(struct sync_port* port, const char* data, int count)
  957. {
  958. port->tr_running = 1;
  959. port->out_descr.buf = (char*)virt_to_phys((char*)data);
  960. port->out_descr.after = port->out_descr.buf + count;
  961. port->out_descr.eol = port->out_descr.intr = 1;
  962. port->out_context.saved_data = (dma_descr_data*)virt_to_phys(&port->out_descr);
  963. port->out_context.saved_data_buf = port->out_descr.buf;
  964. DMA_START_CONTEXT(port->regi_dmaout, virt_to_phys((char*)&port->out_context));
  965. DEBUGTXINT(printk("dma %08lX c %d\n", (unsigned long)data, count));
  966. }
  967. static void start_dma_in(sync_port* port)
  968. {
  969. int i;
  970. char* buf;
  971. port->writep = port->flip;
  972. if (port->writep > port->flip + port->in_buffer_size)
  973. {
  974. panic("Offset too large in sync serial driver\n");
  975. return;
  976. }
  977. buf = (char*)virt_to_phys(port->in_buffer);
  978. for (i = 0; i < NUM_IN_DESCR; i++) {
  979. port->in_descr[i].buf = buf;
  980. port->in_descr[i].after = buf + port->inbufchunk;
  981. port->in_descr[i].intr = 1;
  982. port->in_descr[i].next = (dma_descr_data*)virt_to_phys(&port->in_descr[i+1]);
  983. port->in_descr[i].buf = buf;
  984. buf += port->inbufchunk;
  985. }
  986. /* Link the last descriptor to the first */
  987. port->in_descr[i-1].next = (dma_descr_data*)virt_to_phys(&port->in_descr[0]);
  988. port->in_descr[i-1].eol = regk_sser_yes;
  989. port->next_rx_desc = &port->in_descr[0];
  990. port->prev_rx_desc = &port->in_descr[NUM_IN_DESCR - 1];
  991. port->in_context.saved_data = (dma_descr_data*)virt_to_phys(&port->in_descr[0]);
  992. port->in_context.saved_data_buf = port->in_descr[0].buf;
  993. DMA_START_CONTEXT(port->regi_dmain, virt_to_phys(&port->in_context));
  994. }
  995. #ifdef SYNC_SER_DMA
  996. static irqreturn_t tr_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  997. {
  998. reg_dma_r_masked_intr masked;
  999. reg_dma_rw_ack_intr ack_intr = {.data = regk_dma_yes};
  1000. int i;
  1001. struct dma_descr_data *descr;
  1002. unsigned int sentl;
  1003. int found = 0;
  1004. for (i = 0; i < NUMBER_OF_PORTS; i++)
  1005. {
  1006. sync_port *port = &ports[i];
  1007. if (!port->enabled || !port->use_dma )
  1008. continue;
  1009. masked = REG_RD(dma, port->regi_dmaout, r_masked_intr);
  1010. if (masked.data) /* IRQ active for the port? */
  1011. {
  1012. found = 1;
  1013. /* Clear IRQ */
  1014. REG_WR(dma, port->regi_dmaout, rw_ack_intr, ack_intr);
  1015. descr = &port->out_descr;
  1016. sentl = descr->after - descr->buf;
  1017. port->out_count -= sentl;
  1018. port->outp += sentl;
  1019. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1020. port->outp = port->out_buffer;
  1021. if (port->out_count) {
  1022. int c;
  1023. c = port->out_buffer + OUT_BUFFER_SIZE - port->outp;
  1024. if (c > port->out_count)
  1025. c = port->out_count;
  1026. DEBUGTXINT(printk("tx_int DMAWRITE %i %i\n", sentl, c));
  1027. start_dma(port, port->outp, c);
  1028. } else {
  1029. DEBUGTXINT(printk("tx_int DMA stop %i\n", sentl));
  1030. port->tr_running = 0;
  1031. }
  1032. wake_up_interruptible(&port->out_wait_q); /* wake up the waiting process */
  1033. }
  1034. }
  1035. return IRQ_RETVAL(found);
  1036. } /* tr_interrupt */
  1037. static irqreturn_t rx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1038. {
  1039. reg_dma_r_masked_intr masked;
  1040. reg_dma_rw_ack_intr ack_intr = {.data = regk_dma_yes};
  1041. int i;
  1042. int found = 0;
  1043. for (i = 0; i < NUMBER_OF_PORTS; i++)
  1044. {
  1045. sync_port *port = &ports[i];
  1046. if (!port->enabled || !port->use_dma )
  1047. continue;
  1048. masked = REG_RD(dma, port->regi_dmain, r_masked_intr);
  1049. if (masked.data) /* Descriptor interrupt */
  1050. {
  1051. found = 1;
  1052. while (REG_RD(dma, port->regi_dmain, rw_data) !=
  1053. virt_to_phys(port->next_rx_desc)) {
  1054. if (port->writep + port->inbufchunk > port->flip + port->in_buffer_size) {
  1055. int first_size = port->flip + port->in_buffer_size - port->writep;
  1056. memcpy((char*)port->writep, phys_to_virt((unsigned)port->next_rx_desc->buf), first_size);
  1057. memcpy(port->flip, phys_to_virt((unsigned)port->next_rx_desc->buf+first_size), port->inbufchunk - first_size);
  1058. port->writep = port->flip + port->inbufchunk - first_size;
  1059. } else {
  1060. memcpy((char*)port->writep,
  1061. phys_to_virt((unsigned)port->next_rx_desc->buf),
  1062. port->inbufchunk);
  1063. port->writep += port->inbufchunk;
  1064. if (port->writep >= port->flip + port->in_buffer_size)
  1065. port->writep = port->flip;
  1066. }
  1067. if (port->writep == port->readp)
  1068. {
  1069. port->full = 1;
  1070. }
  1071. port->next_rx_desc->eol = 0;
  1072. port->prev_rx_desc->eol = 1;
  1073. port->prev_rx_desc = phys_to_virt((unsigned)port->next_rx_desc);
  1074. port->next_rx_desc = phys_to_virt((unsigned)port->next_rx_desc->next);
  1075. wake_up_interruptible(&port->in_wait_q); /* wake up the waiting process */
  1076. DMA_CONTINUE(port->regi_dmain);
  1077. REG_WR(dma, port->regi_dmain, rw_ack_intr, ack_intr);
  1078. }
  1079. }
  1080. }
  1081. return IRQ_RETVAL(found);
  1082. } /* rx_interrupt */
  1083. #endif /* SYNC_SER_DMA */
  1084. #ifdef SYNC_SER_MANUAL
  1085. static irqreturn_t manual_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1086. {
  1087. int i;
  1088. int found = 0;
  1089. reg_sser_r_masked_intr masked;
  1090. for (i = 0; i < NUMBER_OF_PORTS; i++)
  1091. {
  1092. sync_port* port = &ports[i];
  1093. if (!port->enabled || port->use_dma)
  1094. {
  1095. continue;
  1096. }
  1097. masked = REG_RD(sser, port->regi_sser, r_masked_intr);
  1098. if (masked.rdav) /* Data received? */
  1099. {
  1100. reg_sser_rw_rec_cfg rec_cfg = REG_RD(sser, port->regi_sser, rw_rec_cfg);
  1101. reg_sser_r_rec_data data = REG_RD(sser, port->regi_sser, r_rec_data);
  1102. found = 1;
  1103. /* Read data */
  1104. switch(rec_cfg.sample_size)
  1105. {
  1106. case 8:
  1107. *port->writep++ = data.data & 0xff;
  1108. break;
  1109. case 12:
  1110. *port->writep = (data.data & 0x0ff0) >> 4;
  1111. *(port->writep + 1) = data.data & 0x0f;
  1112. port->writep+=2;
  1113. break;
  1114. case 16:
  1115. *(unsigned short*)port->writep = data.data;
  1116. port->writep+=2;
  1117. break;
  1118. case 24:
  1119. *(unsigned int*)port->writep = data.data;
  1120. port->writep+=3;
  1121. break;
  1122. case 32:
  1123. *(unsigned int*)port->writep = data.data;
  1124. port->writep+=4;
  1125. break;
  1126. }
  1127. if (port->writep >= port->flip + port->in_buffer_size) /* Wrap? */
  1128. port->writep = port->flip;
  1129. if (port->writep == port->readp) {
  1130. /* receive buffer overrun, discard oldest data
  1131. */
  1132. port->readp++;
  1133. if (port->readp >= port->flip + port->in_buffer_size) /* Wrap? */
  1134. port->readp = port->flip;
  1135. }
  1136. if (sync_data_avail(port) >= port->inbufchunk)
  1137. wake_up_interruptible(&port->in_wait_q); /* Wake up application */
  1138. }
  1139. if (masked.trdy) /* Transmitter ready? */
  1140. {
  1141. found = 1;
  1142. if (port->out_count > 0) /* More data to send */
  1143. send_word(port);
  1144. else /* transmission finished */
  1145. {
  1146. reg_sser_rw_intr_mask intr_mask;
  1147. intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
  1148. intr_mask.trdy = 0;
  1149. REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
  1150. wake_up_interruptible(&port->out_wait_q); /* Wake up application */
  1151. }
  1152. }
  1153. }
  1154. return IRQ_RETVAL(found);
  1155. }
  1156. #endif
  1157. module_init(etrax_sync_serial_init);