sync_serial.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. * Simple synchronous serial port driver for ETRAX 100LX.
  3. *
  4. * Synchronous serial ports are used for continuous streamed data like audio.
  5. * The default setting for this driver is compatible with the STA 013 MP3
  6. * decoder. The driver can easily be tuned to fit other audio encoder/decoders
  7. * and SPI
  8. *
  9. * Copyright (c) 2001-2008 Axis Communications AB
  10. *
  11. * Author: Mikael Starvik, Johan Adolfsson
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/major.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/poll.h>
  22. #include <linux/init.h>
  23. #include <linux/mutex.h>
  24. #include <linux/timer.h>
  25. #include <linux/wait.h>
  26. #include <asm/irq.h>
  27. #include <asm/dma.h>
  28. #include <asm/io.h>
  29. #include <arch/svinto.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/sync_serial.h>
  32. #include <arch/io_interface_mux.h>
  33. /* The receiver is a bit tricky because 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. /* Define some macros to access ETRAX 100 registers */
  76. #define SETF(var, reg, field, val) \
  77. do { \
  78. var = (var & ~IO_MASK_(reg##_, field##_)) | \
  79. IO_FIELD_(reg##_, field##_, val); \
  80. } while (0)
  81. #define SETS(var, reg, field, val) \
  82. do { \
  83. var = (var & ~IO_MASK_(reg##_, field##_)) | \
  84. IO_STATE_(reg##_, field##_, _##val); \
  85. } while (0)
  86. struct sync_port {
  87. /* Etrax registers and bits*/
  88. const volatile unsigned *const status;
  89. volatile unsigned *const ctrl_data;
  90. volatile unsigned *const output_dma_first;
  91. volatile unsigned char *const output_dma_cmd;
  92. volatile unsigned char *const output_dma_clr_irq;
  93. volatile unsigned *const input_dma_first;
  94. volatile unsigned char *const input_dma_cmd;
  95. volatile unsigned *const input_dma_descr;
  96. /* 8*4 */
  97. volatile unsigned char *const input_dma_clr_irq;
  98. volatile unsigned *const data_out;
  99. const volatile unsigned *const data_in;
  100. char data_avail_bit; /* In R_IRQ_MASK1_RD/SET/CLR */
  101. char transmitter_ready_bit; /* In R_IRQ_MASK1_RD/SET/CLR */
  102. char input_dma_descr_bit; /* In R_IRQ_MASK2_RD */
  103. char output_dma_bit; /* In R_IRQ_MASK2_RD */
  104. /* End of fields initialised in array */
  105. char started; /* 1 if port has been started */
  106. char port_nbr; /* Port 0 or 1 */
  107. char busy; /* 1 if port is busy */
  108. char enabled; /* 1 if port is enabled */
  109. char use_dma; /* 1 if port uses dma */
  110. char tr_running;
  111. char init_irqs;
  112. /* Register shadow */
  113. unsigned int ctrl_data_shadow;
  114. /* Remaining bytes for current transfer */
  115. volatile unsigned int out_count;
  116. /* Current position in out_buffer */
  117. unsigned char *outp;
  118. /* 16*4 */
  119. /* Next byte to be read by application */
  120. volatile unsigned char *volatile readp;
  121. /* Next byte to be written by etrax */
  122. volatile unsigned char *volatile writep;
  123. unsigned int in_buffer_size;
  124. unsigned int inbufchunk;
  125. struct etrax_dma_descr out_descr __attribute__ ((aligned(32)));
  126. struct etrax_dma_descr in_descr[NUM_IN_DESCR] __attribute__ ((aligned(32)));
  127. unsigned char out_buffer[OUT_BUFFER_SIZE] __attribute__ ((aligned(32)));
  128. unsigned char in_buffer[IN_BUFFER_SIZE]__attribute__ ((aligned(32)));
  129. unsigned char flip[IN_BUFFER_SIZE] __attribute__ ((aligned(32)));
  130. struct etrax_dma_descr *next_rx_desc;
  131. struct etrax_dma_descr *prev_rx_desc;
  132. int full;
  133. wait_queue_head_t out_wait_q;
  134. wait_queue_head_t in_wait_q;
  135. };
  136. static DEFINE_MUTEX(sync_serial_mutex);
  137. static int etrax_sync_serial_init(void);
  138. static void initialize_port(int portnbr);
  139. static inline int sync_data_avail(struct sync_port *port);
  140. static int sync_serial_open(struct inode *inode, struct file *file);
  141. static int sync_serial_release(struct inode *inode, struct file *file);
  142. static unsigned int sync_serial_poll(struct file *filp, poll_table *wait);
  143. static long sync_serial_ioctl(struct file *file,
  144. unsigned int cmd, unsigned long arg);
  145. static ssize_t sync_serial_write(struct file *file, const char *buf,
  146. size_t count, loff_t *ppos);
  147. static ssize_t sync_serial_read(struct file *file, char *buf,
  148. size_t count, loff_t *ppos);
  149. #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
  150. defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
  151. (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
  152. defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
  153. #define SYNC_SER_DMA
  154. #endif
  155. static void send_word(struct sync_port *port);
  156. static void start_dma(struct sync_port *port, const char *data, int count);
  157. static void start_dma_in(struct sync_port *port);
  158. #ifdef SYNC_SER_DMA
  159. static irqreturn_t tr_interrupt(int irq, void *dev_id);
  160. static irqreturn_t rx_interrupt(int irq, void *dev_id);
  161. #endif
  162. #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
  163. !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
  164. (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
  165. !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
  166. #define SYNC_SER_MANUAL
  167. #endif
  168. #ifdef SYNC_SER_MANUAL
  169. static irqreturn_t manual_interrupt(int irq, void *dev_id);
  170. #endif
  171. /* The ports */
  172. static struct sync_port ports[] = {
  173. {
  174. .status = R_SYNC_SERIAL1_STATUS,
  175. .ctrl_data = R_SYNC_SERIAL1_CTRL,
  176. .output_dma_first = R_DMA_CH8_FIRST,
  177. .output_dma_cmd = R_DMA_CH8_CMD,
  178. .output_dma_clr_irq = R_DMA_CH8_CLR_INTR,
  179. .input_dma_first = R_DMA_CH9_FIRST,
  180. .input_dma_cmd = R_DMA_CH9_CMD,
  181. .input_dma_descr = R_DMA_CH9_DESCR,
  182. .input_dma_clr_irq = R_DMA_CH9_CLR_INTR,
  183. .data_out = R_SYNC_SERIAL1_TR_DATA,
  184. .data_in = R_SYNC_SERIAL1_REC_DATA,
  185. .data_avail_bit = IO_BITNR(R_IRQ_MASK1_RD, ser1_data),
  186. .transmitter_ready_bit = IO_BITNR(R_IRQ_MASK1_RD, ser1_ready),
  187. .input_dma_descr_bit = IO_BITNR(R_IRQ_MASK2_RD, dma9_descr),
  188. .output_dma_bit = IO_BITNR(R_IRQ_MASK2_RD, dma8_eop),
  189. .init_irqs = 1,
  190. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
  191. .use_dma = 1,
  192. #else
  193. .use_dma = 0,
  194. #endif
  195. },
  196. {
  197. .status = R_SYNC_SERIAL3_STATUS,
  198. .ctrl_data = R_SYNC_SERIAL3_CTRL,
  199. .output_dma_first = R_DMA_CH4_FIRST,
  200. .output_dma_cmd = R_DMA_CH4_CMD,
  201. .output_dma_clr_irq = R_DMA_CH4_CLR_INTR,
  202. .input_dma_first = R_DMA_CH5_FIRST,
  203. .input_dma_cmd = R_DMA_CH5_CMD,
  204. .input_dma_descr = R_DMA_CH5_DESCR,
  205. .input_dma_clr_irq = R_DMA_CH5_CLR_INTR,
  206. .data_out = R_SYNC_SERIAL3_TR_DATA,
  207. .data_in = R_SYNC_SERIAL3_REC_DATA,
  208. .data_avail_bit = IO_BITNR(R_IRQ_MASK1_RD, ser3_data),
  209. .transmitter_ready_bit = IO_BITNR(R_IRQ_MASK1_RD, ser3_ready),
  210. .input_dma_descr_bit = IO_BITNR(R_IRQ_MASK2_RD, dma5_descr),
  211. .output_dma_bit = IO_BITNR(R_IRQ_MASK2_RD, dma4_eop),
  212. .init_irqs = 1,
  213. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
  214. .use_dma = 1,
  215. #else
  216. .use_dma = 0,
  217. #endif
  218. }
  219. };
  220. /* Register shadows */
  221. static unsigned sync_serial_prescale_shadow;
  222. #define NUMBER_OF_PORTS 2
  223. static const struct file_operations sync_serial_fops = {
  224. .owner = THIS_MODULE,
  225. .write = sync_serial_write,
  226. .read = sync_serial_read,
  227. .poll = sync_serial_poll,
  228. .unlocked_ioctl = sync_serial_ioctl,
  229. .open = sync_serial_open,
  230. .release = sync_serial_release,
  231. .llseek = noop_llseek,
  232. };
  233. static int __init etrax_sync_serial_init(void)
  234. {
  235. ports[0].enabled = 0;
  236. ports[1].enabled = 0;
  237. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  238. if (cris_request_io_interface(if_sync_serial_1, "sync_ser1")) {
  239. printk(KERN_CRIT "ETRAX100LX sync_serial: "
  240. "Could not allocate IO group for port %d\n", 0);
  241. return -EBUSY;
  242. }
  243. #endif
  244. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
  245. if (cris_request_io_interface(if_sync_serial_3, "sync_ser3")) {
  246. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  247. cris_free_io_interface(if_sync_serial_1);
  248. #endif
  249. printk(KERN_CRIT "ETRAX100LX sync_serial: "
  250. "Could not allocate IO group for port %d\n", 1);
  251. return -EBUSY;
  252. }
  253. #endif
  254. if (register_chrdev(SYNC_SERIAL_MAJOR, "sync serial",
  255. &sync_serial_fops) < 0) {
  256. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
  257. cris_free_io_interface(if_sync_serial_3);
  258. #endif
  259. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  260. cris_free_io_interface(if_sync_serial_1);
  261. #endif
  262. printk("unable to get major for synchronous serial port\n");
  263. return -EBUSY;
  264. }
  265. /* Deselect synchronous serial ports while configuring. */
  266. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode1, async);
  267. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode3, async);
  268. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  269. /* Initialize Ports */
  270. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  271. ports[0].enabled = 1;
  272. SETS(port_pb_i2c_shadow, R_PORT_PB_I2C, syncser1, ss1extra);
  273. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode1, sync);
  274. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
  275. ports[0].use_dma = 1;
  276. #else
  277. ports[0].use_dma = 0;
  278. #endif
  279. initialize_port(0);
  280. #endif
  281. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
  282. ports[1].enabled = 1;
  283. SETS(port_pb_i2c_shadow, R_PORT_PB_I2C, syncser3, ss3extra);
  284. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode3, sync);
  285. #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
  286. ports[1].use_dma = 1;
  287. #else
  288. ports[1].use_dma = 0;
  289. #endif
  290. initialize_port(1);
  291. #endif
  292. *R_PORT_PB_I2C = port_pb_i2c_shadow; /* Use PB4/PB7 */
  293. /* Set up timing */
  294. *R_SYNC_SERIAL_PRESCALE = sync_serial_prescale_shadow = (
  295. IO_STATE(R_SYNC_SERIAL_PRESCALE, clk_sel_u1, codec) |
  296. IO_STATE(R_SYNC_SERIAL_PRESCALE, word_stb_sel_u1, external) |
  297. IO_STATE(R_SYNC_SERIAL_PRESCALE, clk_sel_u3, codec) |
  298. IO_STATE(R_SYNC_SERIAL_PRESCALE, word_stb_sel_u3, external) |
  299. IO_STATE(R_SYNC_SERIAL_PRESCALE, prescaler, div4) |
  300. IO_FIELD(R_SYNC_SERIAL_PRESCALE, frame_rate,
  301. DEFAULT_FRAME_RATE) |
  302. IO_FIELD(R_SYNC_SERIAL_PRESCALE, word_rate, DEFAULT_WORD_RATE) |
  303. IO_STATE(R_SYNC_SERIAL_PRESCALE, warp_mode, normal));
  304. /* Select synchronous ports */
  305. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  306. printk(KERN_INFO "ETRAX 100LX synchronous serial port driver\n");
  307. return 0;
  308. }
  309. static void __init initialize_port(int portnbr)
  310. {
  311. struct sync_port *port = &ports[portnbr];
  312. DEBUG(printk(KERN_DEBUG "Init sync serial port %d\n", portnbr));
  313. port->started = 0;
  314. port->port_nbr = portnbr;
  315. port->busy = 0;
  316. port->tr_running = 0;
  317. port->out_count = 0;
  318. port->outp = port->out_buffer;
  319. port->readp = port->flip;
  320. port->writep = port->flip;
  321. port->in_buffer_size = IN_BUFFER_SIZE;
  322. port->inbufchunk = IN_DESCR_SIZE;
  323. port->next_rx_desc = &port->in_descr[0];
  324. port->prev_rx_desc = &port->in_descr[NUM_IN_DESCR-1];
  325. port->prev_rx_desc->ctrl = d_eol;
  326. init_waitqueue_head(&port->out_wait_q);
  327. init_waitqueue_head(&port->in_wait_q);
  328. port->ctrl_data_shadow =
  329. IO_STATE(R_SYNC_SERIAL1_CTRL, tr_baud, c115k2Hz) |
  330. IO_STATE(R_SYNC_SERIAL1_CTRL, mode, master_output) |
  331. IO_STATE(R_SYNC_SERIAL1_CTRL, error, ignore) |
  332. IO_STATE(R_SYNC_SERIAL1_CTRL, rec_enable, disable) |
  333. IO_STATE(R_SYNC_SERIAL1_CTRL, f_synctype, normal) |
  334. IO_STATE(R_SYNC_SERIAL1_CTRL, f_syncsize, word) |
  335. IO_STATE(R_SYNC_SERIAL1_CTRL, f_sync, on) |
  336. IO_STATE(R_SYNC_SERIAL1_CTRL, clk_mode, normal) |
  337. IO_STATE(R_SYNC_SERIAL1_CTRL, clk_halt, stopped) |
  338. IO_STATE(R_SYNC_SERIAL1_CTRL, bitorder, msb) |
  339. IO_STATE(R_SYNC_SERIAL1_CTRL, tr_enable, disable) |
  340. IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size8bit) |
  341. IO_STATE(R_SYNC_SERIAL1_CTRL, buf_empty, lmt_8) |
  342. IO_STATE(R_SYNC_SERIAL1_CTRL, buf_full, lmt_8) |
  343. IO_STATE(R_SYNC_SERIAL1_CTRL, flow_ctrl, enabled) |
  344. IO_STATE(R_SYNC_SERIAL1_CTRL, clk_polarity, neg) |
  345. IO_STATE(R_SYNC_SERIAL1_CTRL, frame_polarity, normal)|
  346. IO_STATE(R_SYNC_SERIAL1_CTRL, status_polarity, inverted)|
  347. IO_STATE(R_SYNC_SERIAL1_CTRL, clk_driver, normal) |
  348. IO_STATE(R_SYNC_SERIAL1_CTRL, frame_driver, normal) |
  349. IO_STATE(R_SYNC_SERIAL1_CTRL, status_driver, normal)|
  350. IO_STATE(R_SYNC_SERIAL1_CTRL, def_out0, high);
  351. if (port->use_dma)
  352. port->ctrl_data_shadow |= IO_STATE(R_SYNC_SERIAL1_CTRL,
  353. dma_enable, on);
  354. else
  355. port->ctrl_data_shadow |= IO_STATE(R_SYNC_SERIAL1_CTRL,
  356. dma_enable, off);
  357. *port->ctrl_data = port->ctrl_data_shadow;
  358. }
  359. static inline int sync_data_avail(struct sync_port *port)
  360. {
  361. int avail;
  362. unsigned char *start;
  363. unsigned char *end;
  364. start = (unsigned char *)port->readp; /* cast away volatile */
  365. end = (unsigned char *)port->writep; /* cast away volatile */
  366. /* 0123456789 0123456789
  367. * ----- - -----
  368. * ^rp ^wp ^wp ^rp
  369. */
  370. if (end >= start)
  371. avail = end - start;
  372. else
  373. avail = port->in_buffer_size - (start - end);
  374. return avail;
  375. }
  376. static inline int sync_data_avail_to_end(struct sync_port *port)
  377. {
  378. int avail;
  379. unsigned char *start;
  380. unsigned char *end;
  381. start = (unsigned char *)port->readp; /* cast away volatile */
  382. end = (unsigned char *)port->writep; /* cast away volatile */
  383. /* 0123456789 0123456789
  384. * ----- -----
  385. * ^rp ^wp ^wp ^rp
  386. */
  387. if (end >= start)
  388. avail = end - start;
  389. else
  390. avail = port->flip + port->in_buffer_size - start;
  391. return avail;
  392. }
  393. static int sync_serial_open(struct inode *inode, struct file *file)
  394. {
  395. int dev = MINOR(inode->i_rdev);
  396. struct sync_port *port;
  397. int mode;
  398. int err = -EBUSY;
  399. mutex_lock(&sync_serial_mutex);
  400. DEBUG(printk(KERN_DEBUG "Open sync serial port %d\n", dev));
  401. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled) {
  402. DEBUG(printk(KERN_DEBUG "Invalid minor %d\n", dev));
  403. err = -ENODEV;
  404. goto out;
  405. }
  406. port = &ports[dev];
  407. /* Allow open this device twice (assuming one reader and one writer) */
  408. if (port->busy == 2) {
  409. DEBUG(printk(KERN_DEBUG "Device is busy.. \n"));
  410. goto out;
  411. }
  412. if (port->init_irqs) {
  413. if (port->use_dma) {
  414. if (port == &ports[0]) {
  415. #ifdef SYNC_SER_DMA
  416. if (request_irq(24, tr_interrupt, 0,
  417. "synchronous serial 1 dma tr",
  418. &ports[0])) {
  419. printk(KERN_CRIT "Can't alloc "
  420. "sync serial port 1 IRQ");
  421. goto out;
  422. } else if (request_irq(25, rx_interrupt, 0,
  423. "synchronous serial 1 dma rx",
  424. &ports[0])) {
  425. free_irq(24, &port[0]);
  426. printk(KERN_CRIT "Can't alloc "
  427. "sync serial port 1 IRQ");
  428. goto out;
  429. } else if (cris_request_dma(8,
  430. "synchronous serial 1 dma tr",
  431. DMA_VERBOSE_ON_ERROR,
  432. dma_ser1)) {
  433. free_irq(24, &port[0]);
  434. free_irq(25, &port[0]);
  435. printk(KERN_CRIT "Can't alloc "
  436. "sync serial port 1 "
  437. "TX DMA channel");
  438. goto out;
  439. } else if (cris_request_dma(9,
  440. "synchronous serial 1 dma rec",
  441. DMA_VERBOSE_ON_ERROR,
  442. dma_ser1)) {
  443. cris_free_dma(8, NULL);
  444. free_irq(24, &port[0]);
  445. free_irq(25, &port[0]);
  446. printk(KERN_CRIT "Can't alloc "
  447. "sync serial port 1 "
  448. "RX DMA channel");
  449. goto out;
  450. }
  451. #endif
  452. RESET_DMA(8); WAIT_DMA(8);
  453. RESET_DMA(9); WAIT_DMA(9);
  454. *R_DMA_CH8_CLR_INTR =
  455. IO_STATE(R_DMA_CH8_CLR_INTR, clr_eop,
  456. do) |
  457. IO_STATE(R_DMA_CH8_CLR_INTR, clr_descr,
  458. do);
  459. *R_DMA_CH9_CLR_INTR =
  460. IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop,
  461. do) |
  462. IO_STATE(R_DMA_CH9_CLR_INTR, clr_descr,
  463. do);
  464. *R_IRQ_MASK2_SET =
  465. IO_STATE(R_IRQ_MASK2_SET, dma8_eop,
  466. set) |
  467. IO_STATE(R_IRQ_MASK2_SET, dma9_descr,
  468. set);
  469. } else if (port == &ports[1]) {
  470. #ifdef SYNC_SER_DMA
  471. if (request_irq(20, tr_interrupt, 0,
  472. "synchronous serial 3 dma tr",
  473. &ports[1])) {
  474. printk(KERN_CRIT "Can't alloc "
  475. "sync serial port 3 IRQ");
  476. goto out;
  477. } else if (request_irq(21, rx_interrupt, 0,
  478. "synchronous serial 3 dma rx",
  479. &ports[1])) {
  480. free_irq(20, &ports[1]);
  481. printk(KERN_CRIT "Can't alloc "
  482. "sync serial port 3 IRQ");
  483. goto out;
  484. } else if (cris_request_dma(4,
  485. "synchronous serial 3 dma tr",
  486. DMA_VERBOSE_ON_ERROR,
  487. dma_ser3)) {
  488. free_irq(21, &ports[1]);
  489. free_irq(20, &ports[1]);
  490. printk(KERN_CRIT "Can't alloc "
  491. "sync serial port 3 "
  492. "TX DMA channel");
  493. goto out;
  494. } else if (cris_request_dma(5,
  495. "synchronous serial 3 dma rec",
  496. DMA_VERBOSE_ON_ERROR,
  497. dma_ser3)) {
  498. cris_free_dma(4, NULL);
  499. free_irq(21, &ports[1]);
  500. free_irq(20, &ports[1]);
  501. printk(KERN_CRIT "Can't alloc "
  502. "sync serial port 3 "
  503. "RX DMA channel");
  504. goto out;
  505. }
  506. #endif
  507. RESET_DMA(4); WAIT_DMA(4);
  508. RESET_DMA(5); WAIT_DMA(5);
  509. *R_DMA_CH4_CLR_INTR =
  510. IO_STATE(R_DMA_CH4_CLR_INTR, clr_eop,
  511. do) |
  512. IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr,
  513. do);
  514. *R_DMA_CH5_CLR_INTR =
  515. IO_STATE(R_DMA_CH5_CLR_INTR, clr_eop,
  516. do) |
  517. IO_STATE(R_DMA_CH5_CLR_INTR, clr_descr,
  518. do);
  519. *R_IRQ_MASK2_SET =
  520. IO_STATE(R_IRQ_MASK2_SET, dma4_eop,
  521. set) |
  522. IO_STATE(R_IRQ_MASK2_SET, dma5_descr,
  523. set);
  524. }
  525. start_dma_in(port);
  526. port->init_irqs = 0;
  527. } else { /* !port->use_dma */
  528. #ifdef SYNC_SER_MANUAL
  529. if (port == &ports[0]) {
  530. if (request_irq(8,
  531. manual_interrupt,
  532. IRQF_SHARED,
  533. "synchronous serial manual irq",
  534. &ports[0])) {
  535. printk(KERN_CRIT "Can't alloc "
  536. "sync serial manual irq");
  537. goto out;
  538. }
  539. } else if (port == &ports[1]) {
  540. if (request_irq(8,
  541. manual_interrupt,
  542. IRQF_SHARED,
  543. "synchronous serial manual irq",
  544. &ports[1])) {
  545. printk(KERN_CRIT "Can't alloc "
  546. "sync serial manual irq");
  547. goto out;
  548. }
  549. }
  550. port->init_irqs = 0;
  551. #else
  552. panic("sync_serial: Manual mode not supported.\n");
  553. #endif /* SYNC_SER_MANUAL */
  554. }
  555. } /* port->init_irqs */
  556. port->busy++;
  557. /* Start port if we use it as input */
  558. mode = IO_EXTRACT(R_SYNC_SERIAL1_CTRL, mode, port->ctrl_data_shadow);
  559. if (mode == IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, master_input) ||
  560. mode == IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, slave_input) ||
  561. mode == IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, master_bidir) ||
  562. mode == IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, slave_bidir)) {
  563. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, clk_halt,
  564. running);
  565. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, tr_enable,
  566. enable);
  567. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, rec_enable,
  568. enable);
  569. port->started = 1;
  570. *port->ctrl_data = port->ctrl_data_shadow;
  571. if (!port->use_dma)
  572. *R_IRQ_MASK1_SET = 1 << port->data_avail_bit;
  573. DEBUG(printk(KERN_DEBUG "sser%d rec started\n", dev));
  574. }
  575. err = 0;
  576. out:
  577. mutex_unlock(&sync_serial_mutex);
  578. return err;
  579. }
  580. static int sync_serial_release(struct inode *inode, struct file *file)
  581. {
  582. int dev = MINOR(inode->i_rdev);
  583. struct sync_port *port;
  584. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled) {
  585. DEBUG(printk(KERN_DEBUG "Invalid minor %d\n", dev));
  586. return -ENODEV;
  587. }
  588. port = &ports[dev];
  589. if (port->busy)
  590. port->busy--;
  591. if (!port->busy)
  592. *R_IRQ_MASK1_CLR = ((1 << port->data_avail_bit) |
  593. (1 << port->transmitter_ready_bit));
  594. return 0;
  595. }
  596. static unsigned int sync_serial_poll(struct file *file, poll_table *wait)
  597. {
  598. int dev = MINOR(file_inode(file)->i_rdev);
  599. unsigned int mask = 0;
  600. struct sync_port *port;
  601. DEBUGPOLL(static unsigned int prev_mask = 0);
  602. port = &ports[dev];
  603. poll_wait(file, &port->out_wait_q, wait);
  604. poll_wait(file, &port->in_wait_q, wait);
  605. /* Some room to write */
  606. if (port->out_count < OUT_BUFFER_SIZE)
  607. mask |= POLLOUT | POLLWRNORM;
  608. /* At least an inbufchunk of data */
  609. if (sync_data_avail(port) >= port->inbufchunk)
  610. mask |= POLLIN | POLLRDNORM;
  611. DEBUGPOLL(if (mask != prev_mask)
  612. printk(KERN_DEBUG "sync_serial_poll: mask 0x%08X %s %s\n",
  613. mask,
  614. mask & POLLOUT ? "POLLOUT" : "",
  615. mask & POLLIN ? "POLLIN" : "");
  616. prev_mask = mask;
  617. );
  618. return mask;
  619. }
  620. static int sync_serial_ioctl_unlocked(struct file *file,
  621. unsigned int cmd, unsigned long arg)
  622. {
  623. int return_val = 0;
  624. unsigned long flags;
  625. int dev = MINOR(file_inode(file)->i_rdev);
  626. struct sync_port *port;
  627. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled) {
  628. DEBUG(printk(KERN_DEBUG "Invalid minor %d\n", dev));
  629. return -1;
  630. }
  631. port = &ports[dev];
  632. local_irq_save(flags);
  633. /* Disable port while changing config */
  634. if (dev) {
  635. if (port->use_dma) {
  636. RESET_DMA(4); WAIT_DMA(4);
  637. port->tr_running = 0;
  638. port->out_count = 0;
  639. port->outp = port->out_buffer;
  640. *R_DMA_CH4_CLR_INTR =
  641. IO_STATE(R_DMA_CH4_CLR_INTR, clr_eop, do) |
  642. IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr, do);
  643. }
  644. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode3, async);
  645. } else {
  646. if (port->use_dma) {
  647. RESET_DMA(8); WAIT_DMA(8);
  648. port->tr_running = 0;
  649. port->out_count = 0;
  650. port->outp = port->out_buffer;
  651. *R_DMA_CH8_CLR_INTR =
  652. IO_STATE(R_DMA_CH8_CLR_INTR, clr_eop, do) |
  653. IO_STATE(R_DMA_CH8_CLR_INTR, clr_descr, do);
  654. }
  655. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode1, async);
  656. }
  657. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  658. local_irq_restore(flags);
  659. switch (cmd) {
  660. case SSP_SPEED:
  661. if (GET_SPEED(arg) == CODEC) {
  662. if (dev)
  663. SETS(sync_serial_prescale_shadow,
  664. R_SYNC_SERIAL_PRESCALE, clk_sel_u3,
  665. codec);
  666. else
  667. SETS(sync_serial_prescale_shadow,
  668. R_SYNC_SERIAL_PRESCALE, clk_sel_u1,
  669. codec);
  670. SETF(sync_serial_prescale_shadow,
  671. R_SYNC_SERIAL_PRESCALE, prescaler,
  672. GET_FREQ(arg));
  673. SETF(sync_serial_prescale_shadow,
  674. R_SYNC_SERIAL_PRESCALE, frame_rate,
  675. GET_FRAME_RATE(arg));
  676. SETF(sync_serial_prescale_shadow,
  677. R_SYNC_SERIAL_PRESCALE, word_rate,
  678. GET_WORD_RATE(arg));
  679. } else {
  680. SETF(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  681. tr_baud, GET_SPEED(arg));
  682. if (dev)
  683. SETS(sync_serial_prescale_shadow,
  684. R_SYNC_SERIAL_PRESCALE, clk_sel_u3,
  685. baudrate);
  686. else
  687. SETS(sync_serial_prescale_shadow,
  688. R_SYNC_SERIAL_PRESCALE, clk_sel_u1,
  689. baudrate);
  690. }
  691. break;
  692. case SSP_MODE:
  693. if (arg > 5)
  694. return -EINVAL;
  695. if (arg == MASTER_OUTPUT || arg == SLAVE_OUTPUT)
  696. *R_IRQ_MASK1_CLR = 1 << port->data_avail_bit;
  697. else if (!port->use_dma)
  698. *R_IRQ_MASK1_SET = 1 << port->data_avail_bit;
  699. SETF(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, mode, arg);
  700. break;
  701. case SSP_FRAME_SYNC:
  702. if (arg & NORMAL_SYNC)
  703. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  704. f_synctype, normal);
  705. else if (arg & EARLY_SYNC)
  706. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  707. f_synctype, early);
  708. if (arg & BIT_SYNC)
  709. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  710. f_syncsize, bit);
  711. else if (arg & WORD_SYNC)
  712. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  713. f_syncsize, word);
  714. else if (arg & EXTENDED_SYNC)
  715. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  716. f_syncsize, extended);
  717. if (arg & SYNC_ON)
  718. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  719. f_sync, on);
  720. else if (arg & SYNC_OFF)
  721. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  722. f_sync, off);
  723. if (arg & WORD_SIZE_8)
  724. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  725. wordsize, size8bit);
  726. else if (arg & WORD_SIZE_12)
  727. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  728. wordsize, size12bit);
  729. else if (arg & WORD_SIZE_16)
  730. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  731. wordsize, size16bit);
  732. else if (arg & WORD_SIZE_24)
  733. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  734. wordsize, size24bit);
  735. else if (arg & WORD_SIZE_32)
  736. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  737. wordsize, size32bit);
  738. if (arg & BIT_ORDER_MSB)
  739. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  740. bitorder, msb);
  741. else if (arg & BIT_ORDER_LSB)
  742. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  743. bitorder, lsb);
  744. if (arg & FLOW_CONTROL_ENABLE)
  745. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  746. flow_ctrl, enabled);
  747. else if (arg & FLOW_CONTROL_DISABLE)
  748. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  749. flow_ctrl, disabled);
  750. if (arg & CLOCK_NOT_GATED)
  751. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  752. clk_mode, normal);
  753. else if (arg & CLOCK_GATED)
  754. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  755. clk_mode, gated);
  756. break;
  757. case SSP_IPOLARITY:
  758. /* NOTE!! negedge is considered NORMAL */
  759. if (arg & CLOCK_NORMAL)
  760. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  761. clk_polarity, neg);
  762. else if (arg & CLOCK_INVERT)
  763. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  764. clk_polarity, pos);
  765. if (arg & FRAME_NORMAL)
  766. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  767. frame_polarity, normal);
  768. else if (arg & FRAME_INVERT)
  769. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  770. frame_polarity, inverted);
  771. if (arg & STATUS_NORMAL)
  772. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  773. status_polarity, normal);
  774. else if (arg & STATUS_INVERT)
  775. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  776. status_polarity, inverted);
  777. break;
  778. case SSP_OPOLARITY:
  779. if (arg & CLOCK_NORMAL)
  780. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  781. clk_driver, normal);
  782. else if (arg & CLOCK_INVERT)
  783. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  784. clk_driver, inverted);
  785. if (arg & FRAME_NORMAL)
  786. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  787. frame_driver, normal);
  788. else if (arg & FRAME_INVERT)
  789. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  790. frame_driver, inverted);
  791. if (arg & STATUS_NORMAL)
  792. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  793. status_driver, normal);
  794. else if (arg & STATUS_INVERT)
  795. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  796. status_driver, inverted);
  797. break;
  798. case SSP_SPI:
  799. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, flow_ctrl,
  800. disabled);
  801. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, bitorder,
  802. msb);
  803. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, wordsize,
  804. size8bit);
  805. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, f_sync, on);
  806. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, f_syncsize,
  807. word);
  808. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, f_synctype,
  809. normal);
  810. if (arg & SPI_SLAVE) {
  811. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  812. frame_polarity, inverted);
  813. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  814. clk_polarity, neg);
  815. SETF(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  816. mode, SLAVE_INPUT);
  817. } else {
  818. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  819. frame_driver, inverted);
  820. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  821. clk_driver, inverted);
  822. SETF(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL,
  823. mode, MASTER_OUTPUT);
  824. }
  825. break;
  826. case SSP_INBUFCHUNK:
  827. #if 0
  828. if (arg > port->in_buffer_size/NUM_IN_DESCR)
  829. return -EINVAL;
  830. port->inbufchunk = arg;
  831. /* Make sure in_buffer_size is a multiple of inbufchunk */
  832. port->in_buffer_size =
  833. (port->in_buffer_size/port->inbufchunk) *
  834. port->inbufchunk;
  835. DEBUG(printk(KERN_DEBUG "inbufchunk %i in_buffer_size: %i\n",
  836. port->inbufchunk, port->in_buffer_size));
  837. if (port->use_dma) {
  838. if (port->port_nbr == 0) {
  839. RESET_DMA(9);
  840. WAIT_DMA(9);
  841. } else {
  842. RESET_DMA(5);
  843. WAIT_DMA(5);
  844. }
  845. start_dma_in(port);
  846. }
  847. #endif
  848. break;
  849. default:
  850. return_val = -1;
  851. }
  852. /* Make sure we write the config without interruption */
  853. local_irq_save(flags);
  854. /* Set config and enable port */
  855. *port->ctrl_data = port->ctrl_data_shadow;
  856. nop(); nop(); nop(); nop();
  857. *R_SYNC_SERIAL_PRESCALE = sync_serial_prescale_shadow;
  858. nop(); nop(); nop(); nop();
  859. if (dev)
  860. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode3, sync);
  861. else
  862. SETS(gen_config_ii_shadow, R_GEN_CONFIG_II, sermode1, sync);
  863. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  864. /* Reset DMA. At readout from serial port the data could be shifted
  865. * one byte if not resetting DMA.
  866. */
  867. if (port->use_dma) {
  868. if (port->port_nbr == 0) {
  869. RESET_DMA(9);
  870. WAIT_DMA(9);
  871. } else {
  872. RESET_DMA(5);
  873. WAIT_DMA(5);
  874. }
  875. start_dma_in(port);
  876. }
  877. local_irq_restore(flags);
  878. return return_val;
  879. }
  880. static long sync_serial_ioctl(struct file *file,
  881. unsigned int cmd, unsigned long arg)
  882. {
  883. long ret;
  884. mutex_lock(&sync_serial_mutex);
  885. ret = sync_serial_ioctl_unlocked(file, cmd, arg);
  886. mutex_unlock(&sync_serial_mutex);
  887. return ret;
  888. }
  889. static ssize_t sync_serial_write(struct file *file, const char *buf,
  890. size_t count, loff_t *ppos)
  891. {
  892. int dev = MINOR(file_inode(file)->i_rdev);
  893. DECLARE_WAITQUEUE(wait, current);
  894. struct sync_port *port;
  895. unsigned long flags;
  896. unsigned long c, c1;
  897. unsigned long free_outp;
  898. unsigned long outp;
  899. unsigned long out_buffer;
  900. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled) {
  901. DEBUG(printk(KERN_DEBUG "Invalid minor %d\n", dev));
  902. return -ENODEV;
  903. }
  904. port = &ports[dev];
  905. DEBUGWRITE(printk(KERN_DEBUG "W d%d c %lu (%d/%d)\n",
  906. port->port_nbr, count, port->out_count, OUT_BUFFER_SIZE));
  907. /* Space to end of buffer */
  908. /*
  909. * out_buffer <c1>012345<- c ->OUT_BUFFER_SIZE
  910. * outp^ +out_count
  911. * ^free_outp
  912. * out_buffer 45<- c ->0123OUT_BUFFER_SIZE
  913. * +out_count outp^
  914. * free_outp
  915. *
  916. */
  917. /* Read variables that may be updated by interrupts */
  918. local_irq_save(flags);
  919. if (count > OUT_BUFFER_SIZE - port->out_count)
  920. count = OUT_BUFFER_SIZE - port->out_count;
  921. outp = (unsigned long)port->outp;
  922. free_outp = outp + port->out_count;
  923. local_irq_restore(flags);
  924. out_buffer = (unsigned long)port->out_buffer;
  925. /* Find out where and how much to write */
  926. if (free_outp >= out_buffer + OUT_BUFFER_SIZE)
  927. free_outp -= OUT_BUFFER_SIZE;
  928. if (free_outp >= outp)
  929. c = out_buffer + OUT_BUFFER_SIZE - free_outp;
  930. else
  931. c = outp - free_outp;
  932. if (c > count)
  933. c = count;
  934. DEBUGWRITE(printk(KERN_DEBUG "w op %08lX fop %08lX c %lu\n",
  935. outp, free_outp, c));
  936. if (copy_from_user((void *)free_outp, buf, c))
  937. return -EFAULT;
  938. if (c != count) {
  939. buf += c;
  940. c1 = count - c;
  941. DEBUGWRITE(printk(KERN_DEBUG "w2 fi %lu c %lu c1 %lu\n",
  942. free_outp-out_buffer, c, c1));
  943. if (copy_from_user((void *)out_buffer, buf, c1))
  944. return -EFAULT;
  945. }
  946. local_irq_save(flags);
  947. port->out_count += count;
  948. local_irq_restore(flags);
  949. /* Make sure transmitter/receiver is running */
  950. if (!port->started) {
  951. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, clk_halt,
  952. running);
  953. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, tr_enable,
  954. enable);
  955. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, rec_enable,
  956. enable);
  957. port->started = 1;
  958. }
  959. *port->ctrl_data = port->ctrl_data_shadow;
  960. if (file->f_flags & O_NONBLOCK) {
  961. local_irq_save(flags);
  962. if (!port->tr_running) {
  963. if (!port->use_dma) {
  964. /* Start sender by writing data */
  965. send_word(port);
  966. /* and enable transmitter ready IRQ */
  967. *R_IRQ_MASK1_SET = 1 <<
  968. port->transmitter_ready_bit;
  969. } else
  970. start_dma(port,
  971. (unsigned char *volatile)port->outp, c);
  972. }
  973. local_irq_restore(flags);
  974. DEBUGWRITE(printk(KERN_DEBUG "w d%d c %lu NB\n",
  975. port->port_nbr, count));
  976. return count;
  977. }
  978. /* Sleep until all sent */
  979. add_wait_queue(&port->out_wait_q, &wait);
  980. set_current_state(TASK_INTERRUPTIBLE);
  981. local_irq_save(flags);
  982. if (!port->tr_running) {
  983. if (!port->use_dma) {
  984. /* Start sender by writing data */
  985. send_word(port);
  986. /* and enable transmitter ready IRQ */
  987. *R_IRQ_MASK1_SET = 1 << port->transmitter_ready_bit;
  988. } else
  989. start_dma(port, port->outp, c);
  990. }
  991. local_irq_restore(flags);
  992. schedule();
  993. remove_wait_queue(&port->out_wait_q, &wait);
  994. if (signal_pending(current))
  995. return -EINTR;
  996. DEBUGWRITE(printk(KERN_DEBUG "w d%d c %lu\n", port->port_nbr, count));
  997. return count;
  998. }
  999. static ssize_t sync_serial_read(struct file *file, char *buf,
  1000. size_t count, loff_t *ppos)
  1001. {
  1002. int dev = MINOR(file_inode(file)->i_rdev);
  1003. int avail;
  1004. struct sync_port *port;
  1005. unsigned char *start;
  1006. unsigned char *end;
  1007. unsigned long flags;
  1008. if (dev < 0 || dev >= NUMBER_OF_PORTS || !ports[dev].enabled) {
  1009. DEBUG(printk(KERN_DEBUG "Invalid minor %d\n", dev));
  1010. return -ENODEV;
  1011. }
  1012. port = &ports[dev];
  1013. DEBUGREAD(printk(KERN_DEBUG "R%d c %d ri %lu wi %lu /%lu\n",
  1014. dev, count, port->readp - port->flip,
  1015. port->writep - port->flip, port->in_buffer_size));
  1016. if (!port->started) {
  1017. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, clk_halt,
  1018. running);
  1019. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, tr_enable,
  1020. enable);
  1021. SETS(port->ctrl_data_shadow, R_SYNC_SERIAL1_CTRL, rec_enable,
  1022. enable);
  1023. port->started = 1;
  1024. }
  1025. *port->ctrl_data = port->ctrl_data_shadow;
  1026. /* Calculate number of available bytes */
  1027. /* Save pointers to avoid that they are modified by interrupt */
  1028. local_irq_save(flags);
  1029. start = (unsigned char *)port->readp; /* cast away volatile */
  1030. end = (unsigned char *)port->writep; /* cast away volatile */
  1031. local_irq_restore(flags);
  1032. while (start == end && !port->full) {
  1033. /* No data */
  1034. if (file->f_flags & O_NONBLOCK)
  1035. return -EAGAIN;
  1036. wait_event_interruptible(port->in_wait_q,
  1037. !(start == end && !port->full));
  1038. if (signal_pending(current))
  1039. return -EINTR;
  1040. local_irq_save(flags);
  1041. start = (unsigned char *)port->readp; /* cast away volatile */
  1042. end = (unsigned char *)port->writep; /* cast away volatile */
  1043. local_irq_restore(flags);
  1044. }
  1045. /* Lazy read, never return wrapped data. */
  1046. if (port->full)
  1047. avail = port->in_buffer_size;
  1048. else if (end > start)
  1049. avail = end - start;
  1050. else
  1051. avail = port->flip + port->in_buffer_size - start;
  1052. count = count > avail ? avail : count;
  1053. if (copy_to_user(buf, start, count))
  1054. return -EFAULT;
  1055. /* Disable interrupts while updating readp */
  1056. local_irq_save(flags);
  1057. port->readp += count;
  1058. if (port->readp >= port->flip + port->in_buffer_size) /* Wrap? */
  1059. port->readp = port->flip;
  1060. port->full = 0;
  1061. local_irq_restore(flags);
  1062. DEBUGREAD(printk(KERN_DEBUG "r %d\n", count));
  1063. return count;
  1064. }
  1065. static void send_word(struct sync_port *port)
  1066. {
  1067. switch (IO_EXTRACT(R_SYNC_SERIAL1_CTRL, wordsize,
  1068. port->ctrl_data_shadow)) {
  1069. case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, wordsize, size8bit):
  1070. port->out_count--;
  1071. *port->data_out = *port->outp++;
  1072. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1073. port->outp = port->out_buffer;
  1074. break;
  1075. case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, wordsize, size12bit):
  1076. {
  1077. int data = (*port->outp++) << 8;
  1078. data |= *port->outp++;
  1079. port->out_count -= 2;
  1080. *port->data_out = data;
  1081. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1082. port->outp = port->out_buffer;
  1083. break;
  1084. }
  1085. case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, wordsize, size16bit):
  1086. port->out_count -= 2;
  1087. *port->data_out = *(unsigned short *)port->outp;
  1088. port->outp += 2;
  1089. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1090. port->outp = port->out_buffer;
  1091. break;
  1092. case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, wordsize, size24bit):
  1093. port->out_count -= 3;
  1094. *port->data_out = *(unsigned int *)port->outp;
  1095. port->outp += 3;
  1096. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1097. port->outp = port->out_buffer;
  1098. break;
  1099. case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, wordsize, size32bit):
  1100. port->out_count -= 4;
  1101. *port->data_out = *(unsigned int *)port->outp;
  1102. port->outp += 4;
  1103. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1104. port->outp = port->out_buffer;
  1105. break;
  1106. }
  1107. }
  1108. static void start_dma(struct sync_port *port, const char *data, int count)
  1109. {
  1110. port->tr_running = 1;
  1111. port->out_descr.hw_len = 0;
  1112. port->out_descr.next = 0;
  1113. port->out_descr.ctrl = d_eol | d_eop; /* No d_wait to avoid glitches */
  1114. port->out_descr.sw_len = count;
  1115. port->out_descr.buf = virt_to_phys(data);
  1116. port->out_descr.status = 0;
  1117. *port->output_dma_first = virt_to_phys(&port->out_descr);
  1118. *port->output_dma_cmd = IO_STATE(R_DMA_CH0_CMD, cmd, start);
  1119. DEBUGTXINT(printk(KERN_DEBUG "dma %08lX c %d\n",
  1120. (unsigned long)data, count));
  1121. }
  1122. static void start_dma_in(struct sync_port *port)
  1123. {
  1124. int i;
  1125. unsigned long buf;
  1126. port->writep = port->flip;
  1127. if (port->writep > port->flip + port->in_buffer_size) {
  1128. panic("Offset too large in sync serial driver\n");
  1129. return;
  1130. }
  1131. buf = virt_to_phys(port->in_buffer);
  1132. for (i = 0; i < NUM_IN_DESCR; i++) {
  1133. port->in_descr[i].sw_len = port->inbufchunk;
  1134. port->in_descr[i].ctrl = d_int;
  1135. port->in_descr[i].next = virt_to_phys(&port->in_descr[i+1]);
  1136. port->in_descr[i].buf = buf;
  1137. port->in_descr[i].hw_len = 0;
  1138. port->in_descr[i].status = 0;
  1139. port->in_descr[i].fifo_len = 0;
  1140. buf += port->inbufchunk;
  1141. prepare_rx_descriptor(&port->in_descr[i]);
  1142. }
  1143. /* Link the last descriptor to the first */
  1144. port->in_descr[i-1].next = virt_to_phys(&port->in_descr[0]);
  1145. port->in_descr[i-1].ctrl |= d_eol;
  1146. port->next_rx_desc = &port->in_descr[0];
  1147. port->prev_rx_desc = &port->in_descr[NUM_IN_DESCR - 1];
  1148. *port->input_dma_first = virt_to_phys(port->next_rx_desc);
  1149. *port->input_dma_cmd = IO_STATE(R_DMA_CH0_CMD, cmd, start);
  1150. }
  1151. #ifdef SYNC_SER_DMA
  1152. static irqreturn_t tr_interrupt(int irq, void *dev_id)
  1153. {
  1154. unsigned long ireg = *R_IRQ_MASK2_RD;
  1155. struct etrax_dma_descr *descr;
  1156. unsigned int sentl;
  1157. int handled = 0;
  1158. int i;
  1159. for (i = 0; i < NUMBER_OF_PORTS; i++) {
  1160. struct sync_port *port = &ports[i];
  1161. if (!port->enabled || !port->use_dma)
  1162. continue;
  1163. /* IRQ active for the port? */
  1164. if (!(ireg & (1 << port->output_dma_bit)))
  1165. continue;
  1166. handled = 1;
  1167. /* Clear IRQ */
  1168. *port->output_dma_clr_irq =
  1169. IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do) |
  1170. IO_STATE(R_DMA_CH0_CLR_INTR, clr_descr, do);
  1171. descr = &port->out_descr;
  1172. if (!(descr->status & d_stop))
  1173. sentl = descr->sw_len;
  1174. else
  1175. /* Otherwise find amount of data sent here */
  1176. sentl = descr->hw_len;
  1177. port->out_count -= sentl;
  1178. port->outp += sentl;
  1179. if (port->outp >= port->out_buffer + OUT_BUFFER_SIZE)
  1180. port->outp = port->out_buffer;
  1181. if (port->out_count) {
  1182. int c = port->out_buffer + OUT_BUFFER_SIZE - port->outp;
  1183. if (c > port->out_count)
  1184. c = port->out_count;
  1185. DEBUGTXINT(printk(KERN_DEBUG
  1186. "tx_int DMAWRITE %i %i\n", sentl, c));
  1187. start_dma(port, port->outp, c);
  1188. } else {
  1189. DEBUGTXINT(printk(KERN_DEBUG
  1190. "tx_int DMA stop %i\n", sentl));
  1191. port->tr_running = 0;
  1192. }
  1193. /* wake up the waiting process */
  1194. wake_up_interruptible(&port->out_wait_q);
  1195. }
  1196. return IRQ_RETVAL(handled);
  1197. } /* tr_interrupt */
  1198. static irqreturn_t rx_interrupt(int irq, void *dev_id)
  1199. {
  1200. unsigned long ireg = *R_IRQ_MASK2_RD;
  1201. int i;
  1202. int handled = 0;
  1203. for (i = 0; i < NUMBER_OF_PORTS; i++) {
  1204. struct sync_port *port = &ports[i];
  1205. if (!port->enabled || !port->use_dma)
  1206. continue;
  1207. if (!(ireg & (1 << port->input_dma_descr_bit)))
  1208. continue;
  1209. /* Descriptor interrupt */
  1210. handled = 1;
  1211. while (*port->input_dma_descr !=
  1212. virt_to_phys(port->next_rx_desc)) {
  1213. if (port->writep + port->inbufchunk > port->flip +
  1214. port->in_buffer_size) {
  1215. int first_size = port->flip +
  1216. port->in_buffer_size - port->writep;
  1217. memcpy(port->writep,
  1218. phys_to_virt(port->next_rx_desc->buf),
  1219. first_size);
  1220. memcpy(port->flip,
  1221. phys_to_virt(port->next_rx_desc->buf +
  1222. first_size),
  1223. port->inbufchunk - first_size);
  1224. port->writep = port->flip +
  1225. port->inbufchunk - first_size;
  1226. } else {
  1227. memcpy(port->writep,
  1228. phys_to_virt(port->next_rx_desc->buf),
  1229. port->inbufchunk);
  1230. port->writep += port->inbufchunk;
  1231. if (port->writep >= port->flip
  1232. + port->in_buffer_size)
  1233. port->writep = port->flip;
  1234. }
  1235. if (port->writep == port->readp)
  1236. port->full = 1;
  1237. prepare_rx_descriptor(port->next_rx_desc);
  1238. port->next_rx_desc->ctrl |= d_eol;
  1239. port->prev_rx_desc->ctrl &= ~d_eol;
  1240. port->prev_rx_desc = phys_to_virt((unsigned)
  1241. port->next_rx_desc);
  1242. port->next_rx_desc = phys_to_virt((unsigned)
  1243. port->next_rx_desc->next);
  1244. /* Wake up the waiting process */
  1245. wake_up_interruptible(&port->in_wait_q);
  1246. *port->input_dma_cmd = IO_STATE(R_DMA_CH1_CMD,
  1247. cmd, restart);
  1248. /* DMA has reached end of descriptor */
  1249. *port->input_dma_clr_irq = IO_STATE(R_DMA_CH0_CLR_INTR,
  1250. clr_descr, do);
  1251. }
  1252. }
  1253. return IRQ_RETVAL(handled);
  1254. } /* rx_interrupt */
  1255. #endif /* SYNC_SER_DMA */
  1256. #ifdef SYNC_SER_MANUAL
  1257. static irqreturn_t manual_interrupt(int irq, void *dev_id)
  1258. {
  1259. int i;
  1260. int handled = 0;
  1261. for (i = 0; i < NUMBER_OF_PORTS; i++) {
  1262. struct sync_port *port = &ports[i];
  1263. if (!port->enabled || port->use_dma)
  1264. continue;
  1265. /* Data received? */
  1266. if (*R_IRQ_MASK1_RD & (1 << port->data_avail_bit)) {
  1267. handled = 1;
  1268. /* Read data */
  1269. switch (port->ctrl_data_shadow &
  1270. IO_MASK(R_SYNC_SERIAL1_CTRL, wordsize)) {
  1271. case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size8bit):
  1272. *port->writep++ =
  1273. *(volatile char *)port->data_in;
  1274. break;
  1275. case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size12bit):
  1276. {
  1277. int data = *(unsigned short *)port->data_in;
  1278. *port->writep = (data & 0x0ff0) >> 4;
  1279. *(port->writep + 1) = data & 0x0f;
  1280. port->writep += 2;
  1281. break;
  1282. }
  1283. case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size16bit):
  1284. *(unsigned short *)port->writep =
  1285. *(volatile unsigned short *)port->data_in;
  1286. port->writep += 2;
  1287. break;
  1288. case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size24bit):
  1289. *(unsigned int *)port->writep = *port->data_in;
  1290. port->writep += 3;
  1291. break;
  1292. case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size32bit):
  1293. *(unsigned int *)port->writep = *port->data_in;
  1294. port->writep += 4;
  1295. break;
  1296. }
  1297. /* Wrap? */
  1298. if (port->writep >= port->flip + port->in_buffer_size)
  1299. port->writep = port->flip;
  1300. if (port->writep == port->readp) {
  1301. /* Receive buffer overrun, discard oldest */
  1302. port->readp++;
  1303. /* Wrap? */
  1304. if (port->readp >= port->flip +
  1305. port->in_buffer_size)
  1306. port->readp = port->flip;
  1307. }
  1308. if (sync_data_avail(port) >= port->inbufchunk) {
  1309. /* Wake up application */
  1310. wake_up_interruptible(&port->in_wait_q);
  1311. }
  1312. }
  1313. /* Transmitter ready? */
  1314. if (*R_IRQ_MASK1_RD & (1 << port->transmitter_ready_bit)) {
  1315. if (port->out_count > 0) {
  1316. /* More data to send */
  1317. send_word(port);
  1318. } else {
  1319. /* Transmission finished */
  1320. /* Turn off IRQ */
  1321. *R_IRQ_MASK1_CLR = 1 <<
  1322. port->transmitter_ready_bit;
  1323. /* Wake up application */
  1324. wake_up_interruptible(&port->out_wait_q);
  1325. }
  1326. }
  1327. }
  1328. return IRQ_RETVAL(handled);
  1329. }
  1330. #endif
  1331. module_init(etrax_sync_serial_init);