mfd.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * mfd.c: driver for High Speed UART device of Intel Medfield platform
  3. *
  4. * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
  5. *
  6. * (C) Copyright 2009 Intel Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. */
  13. /* Notes:
  14. * 1. there should be 2 types of register access method, one for
  15. * UART ports, the other for the general purpose registers
  16. *
  17. * 2. It used to have a Irda port, but was defeatured recently
  18. *
  19. * 3. Based on the info from HSU MAS, 0/1 channel are assigned to
  20. * port0, 2/3 chan to port 1, 4/5 chan to port 3. Even number
  21. * chan will be read, odd chan for write
  22. *
  23. * 4. HUS supports both the 64B and 16B FIFO version, but this driver
  24. * will only use 64B version
  25. *
  26. * 5. In A0 stepping, UART will not support TX half empty flag, thus
  27. * need add a #ifdef judgement
  28. *
  29. * 6. One more bug for A0, the loopback mode won't support AFC
  30. * auto-flow control
  31. *
  32. * 7. HSU has some special FCR control bits, we add it to serial_reg.h
  33. *
  34. * 8. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always asserted,
  35. * only when the HW is reset the DDCD and DDSR will be triggered
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/console.h>
  40. #include <linux/sysrq.h>
  41. #include <linux/serial_reg.h>
  42. #include <linux/circ_buf.h>
  43. #include <linux/delay.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/tty.h>
  46. #include <linux/tty_flip.h>
  47. #include <linux/serial_core.h>
  48. #include <linux/serial_mfd.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/pci.h>
  51. #include <linux/io.h>
  52. #include <linux/debugfs.h>
  53. #define MFD_HSU_A0_STEPPING 1
  54. #define HSU_DMA_BUF_SIZE 2048
  55. #define chan_readl(chan, offset) readl(chan->reg + offset)
  56. #define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
  57. #define mfd_readl(obj, offset) readl(obj->reg + offset)
  58. #define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
  59. #define HSU_DMA_TIMEOUT_CHECK_FREQ (HZ/10)
  60. struct hsu_dma_buffer {
  61. u8 *buf;
  62. dma_addr_t dma_addr;
  63. u32 dma_size;
  64. u32 ofs;
  65. };
  66. struct hsu_dma_chan {
  67. u32 id;
  68. u32 dirt; /* to or from device */
  69. struct uart_hsu_port *uport;
  70. void __iomem *reg;
  71. struct timer_list rx_timer; /* only needed by RX channel */
  72. };
  73. struct uart_hsu_port {
  74. struct uart_port port;
  75. unsigned char ier;
  76. unsigned char lcr;
  77. unsigned char mcr;
  78. unsigned int lsr_break_flag;
  79. char name[12];
  80. int index;
  81. struct device *dev;
  82. struct hsu_dma_chan *txc;
  83. struct hsu_dma_chan *rxc;
  84. struct hsu_dma_buffer txbuf;
  85. struct hsu_dma_buffer rxbuf;
  86. int use_dma; /* flag for DMA/PIO */
  87. int running;
  88. int dma_tx_on;
  89. };
  90. /* Top level data structure of HSU */
  91. struct hsu_port {
  92. struct pci_device *pdev;
  93. void __iomem *reg;
  94. unsigned long paddr;
  95. unsigned long iolen;
  96. u32 irq;
  97. struct uart_hsu_port port[3];
  98. struct hsu_dma_chan chans[10];
  99. #ifdef CONFIG_DEBUG_FS
  100. struct dentry *debugfs;
  101. #endif
  102. };
  103. static inline void hexdump(char *str, u8 *addr, int cnt)
  104. {
  105. int i;
  106. for (i = 0; i < cnt; i += 8) {
  107. printk("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
  108. addr[i], addr[i+1], addr[i+2], addr[i+3],
  109. addr[i+4], addr[i+5], addr[i+6], addr[i+7]);
  110. printk("\n");
  111. }
  112. }
  113. static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
  114. {
  115. unsigned int val;
  116. if (offset > UART_MSR) {
  117. offset <<= 2;
  118. val = readl(up->port.membase + offset);
  119. } else
  120. val = (unsigned int)readb(up->port.membase + offset);
  121. return val;
  122. }
  123. static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
  124. {
  125. if (offset > UART_MSR) {
  126. offset <<= 2;
  127. writel(value, up->port.membase + offset);
  128. } else {
  129. unsigned char val = value & 0xff;
  130. writeb(val, up->port.membase + offset);
  131. }
  132. }
  133. #ifdef CONFIG_DEBUG_FS
  134. #define HSU_REGS_BUFSIZE 1024
  135. static int hsu_show_regs_open(struct inode *inode, struct file *file)
  136. {
  137. file->private_data = inode->i_private;
  138. return 0;
  139. }
  140. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  141. size_t count, loff_t *ppos)
  142. {
  143. struct uart_hsu_port *up = file->private_data;
  144. char *buf;
  145. u32 len = 0;
  146. ssize_t ret;
  147. buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
  148. if (!buf)
  149. return 0;
  150. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  151. "MFD HSU port[%d] regs:\n", up->index);
  152. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  153. "=================================\n");
  154. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  155. "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
  156. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  157. "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
  158. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  159. "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
  160. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  161. "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
  162. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  163. "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
  164. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  165. "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
  166. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  167. "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
  168. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  169. "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
  170. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  171. "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
  172. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  173. "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
  174. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  175. kfree(buf);
  176. return ret;
  177. }
  178. static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
  179. size_t count, loff_t *ppos)
  180. {
  181. struct hsu_dma_chan *chan = file->private_data;
  182. char *buf;
  183. u32 len = 0;
  184. ssize_t ret;
  185. buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
  186. if (!buf)
  187. return 0;
  188. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  189. "MFD HSU DMA channel [%d] regs:\n", chan->id);
  190. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  191. "=================================\n");
  192. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  193. "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
  194. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  195. "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
  196. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  197. "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
  198. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  199. "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
  200. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  201. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
  202. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  203. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
  204. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  205. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
  206. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  207. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
  208. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  209. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
  210. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  211. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
  212. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  213. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
  214. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  215. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
  216. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  217. kfree(buf);
  218. return ret;
  219. }
  220. static const struct file_operations port_regs_ops = {
  221. .owner = THIS_MODULE,
  222. .open = hsu_show_regs_open,
  223. .read = port_show_regs,
  224. };
  225. static const struct file_operations dma_regs_ops = {
  226. .owner = THIS_MODULE,
  227. .open = hsu_show_regs_open,
  228. .read = dma_show_regs,
  229. };
  230. static int hsu_debugfs_init(struct hsu_port *hsu)
  231. {
  232. int i;
  233. char name[32];
  234. hsu->debugfs = debugfs_create_dir("hsu", NULL);
  235. if (!hsu->debugfs)
  236. return -ENOMEM;
  237. for (i = 0; i < 3; i++) {
  238. snprintf(name, sizeof(name), "port_%d_regs", i);
  239. debugfs_create_file(name, S_IFREG | S_IRUGO,
  240. hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
  241. }
  242. for (i = 0; i < 6; i++) {
  243. snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
  244. debugfs_create_file(name, S_IFREG | S_IRUGO,
  245. hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
  246. }
  247. return 0;
  248. }
  249. static void hsu_debugfs_remove(struct hsu_port *hsu)
  250. {
  251. if (hsu->debugfs)
  252. debugfs_remove_recursive(hsu->debugfs);
  253. }
  254. #else
  255. static inline int hsu_debugfs_init(struct hsu_port *hsu)
  256. {
  257. return 0;
  258. }
  259. static inline void hsu_debugfs_remove(struct hsu_port *hsu)
  260. {
  261. }
  262. #endif /* CONFIG_DEBUG_FS */
  263. static void serial_hsu_enable_ms(struct uart_port *port)
  264. {
  265. struct uart_hsu_port *up =
  266. container_of(port, struct uart_hsu_port, port);
  267. up->ier |= UART_IER_MSI;
  268. serial_out(up, UART_IER, up->ier);
  269. }
  270. void hsu_dma_tx(struct uart_hsu_port *up)
  271. {
  272. struct circ_buf *xmit = &up->port.state->xmit;
  273. struct hsu_dma_buffer *dbuf = &up->txbuf;
  274. int count;
  275. /* test_and_set_bit may be better, but anyway it's in lock protected mode */
  276. if (up->dma_tx_on)
  277. return;
  278. /* Update the circ buf info */
  279. xmit->tail += dbuf->ofs;
  280. xmit->tail &= UART_XMIT_SIZE - 1;
  281. up->port.icount.tx += dbuf->ofs;
  282. dbuf->ofs = 0;
  283. /* Disable the channel */
  284. chan_writel(up->txc, HSU_CH_CR, 0x0);
  285. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
  286. dma_sync_single_for_device(up->port.dev,
  287. dbuf->dma_addr,
  288. dbuf->dma_size,
  289. DMA_TO_DEVICE);
  290. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  291. dbuf->ofs = count;
  292. /* Reprogram the channel */
  293. chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
  294. chan_writel(up->txc, HSU_CH_D0TSR, count);
  295. /* Reenable the channel */
  296. chan_writel(up->txc, HSU_CH_DCR, 0x1
  297. | (0x1 << 8)
  298. | (0x1 << 16)
  299. | (0x1 << 24));
  300. WARN(chan_readl(up->txc, HSU_CH_CR) & 0x1,
  301. "TX channel has already be started!!\n");
  302. up->dma_tx_on = 1;
  303. chan_writel(up->txc, HSU_CH_CR, 0x1);
  304. }
  305. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  306. uart_write_wakeup(&up->port);
  307. }
  308. /* The buffer is already cache coherent */
  309. void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
  310. {
  311. /* Need start RX dma channel here */
  312. dbuf->ofs = 0;
  313. chan_writel(rxc, HSU_CH_BSR, 32);
  314. chan_writel(rxc, HSU_CH_MOTSR, 4);
  315. chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
  316. chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
  317. chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
  318. | (0x1 << 16)
  319. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  320. );
  321. chan_writel(rxc, HSU_CH_CR, 0x3);
  322. mod_timer(&rxc->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
  323. }
  324. /* Protected by spin_lock_irqsave(port->lock) */
  325. static void serial_hsu_start_tx(struct uart_port *port)
  326. {
  327. struct uart_hsu_port *up =
  328. container_of(port, struct uart_hsu_port, port);
  329. if (up->use_dma) {
  330. hsu_dma_tx(up);
  331. } else if (!(up->ier & UART_IER_THRI)) {
  332. up->ier |= UART_IER_THRI;
  333. serial_out(up, UART_IER, up->ier);
  334. }
  335. }
  336. static void serial_hsu_stop_tx(struct uart_port *port)
  337. {
  338. struct uart_hsu_port *up =
  339. container_of(port, struct uart_hsu_port, port);
  340. struct hsu_dma_chan *txc = up->txc;
  341. if (up->use_dma)
  342. chan_writel(txc, HSU_CH_CR, 0x0);
  343. else if (up->ier & UART_IER_THRI) {
  344. up->ier &= ~UART_IER_THRI;
  345. serial_out(up, UART_IER, up->ier);
  346. }
  347. }
  348. /* This is always called in spinlock protected mode, so
  349. * modify timeout timer is safe here */
  350. void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
  351. {
  352. struct hsu_dma_buffer *dbuf = &up->rxbuf;
  353. struct hsu_dma_chan *chan = up->rxc;
  354. struct uart_port *port = &up->port;
  355. struct tty_struct *tty = port->state->port.tty;
  356. int count;
  357. if (!tty)
  358. return;
  359. /*
  360. * first need to know how many is already transferred,
  361. * then check if its a timeout DMA irq, and return
  362. * the trail bytes out, push them up and reenable the
  363. * channel, better to use 2 descriptors at the same time
  364. */
  365. /* timeout IRQ, need wait some time, see Errata 2 */
  366. if (int_sts & 0xf00)
  367. udelay(2);
  368. /* Stop the channel */
  369. chan_writel(chan, HSU_CH_CR, 0x0);
  370. /* We can use 2 ways to calc the actual transfer len */
  371. count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
  372. if (!count) {
  373. /* restart the channel before we leave */
  374. chan_writel(chan, HSU_CH_CR, 0x3);
  375. return;
  376. }
  377. del_timer(&chan->rx_timer);
  378. dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
  379. dbuf->dma_size, DMA_FROM_DEVICE);
  380. /*
  381. * head will only wrap around when we recycle
  382. * the DMA buffer, and when that happens, we
  383. * explicitly set tail to 0. So head will
  384. * always be greater than tail.
  385. */
  386. tty_insert_flip_string(tty, dbuf->buf, count);
  387. port->icount.rx += count;
  388. dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
  389. dbuf->dma_size, DMA_FROM_DEVICE);
  390. /* Reprogram the channel */
  391. chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
  392. chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
  393. chan_writel(chan, HSU_CH_DCR, 0x1
  394. | (0x1 << 8)
  395. | (0x1 << 16)
  396. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  397. );
  398. tty_flip_buffer_push(tty);
  399. chan_writel(chan, HSU_CH_CR, 0x3);
  400. chan->rx_timer.expires = jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ;
  401. add_timer(&chan->rx_timer);
  402. }
  403. static void serial_hsu_stop_rx(struct uart_port *port)
  404. {
  405. struct uart_hsu_port *up =
  406. container_of(port, struct uart_hsu_port, port);
  407. struct hsu_dma_chan *chan = up->rxc;
  408. if (up->use_dma)
  409. chan_writel(chan, HSU_CH_CR, 0x2);
  410. else {
  411. up->ier &= ~UART_IER_RLSI;
  412. up->port.read_status_mask &= ~UART_LSR_DR;
  413. serial_out(up, UART_IER, up->ier);
  414. }
  415. }
  416. /*
  417. * if there is error flag, should we just reset the FIFO or keeps
  418. * working on it
  419. */
  420. static inline void receive_chars(struct uart_hsu_port *up, int *status)
  421. {
  422. struct tty_struct *tty = up->port.state->port.tty;
  423. unsigned int ch, flag;
  424. unsigned int max_count = 256;
  425. if (!tty)
  426. return;
  427. do {
  428. ch = serial_in(up, UART_RX);
  429. flag = TTY_NORMAL;
  430. up->port.icount.rx++;
  431. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  432. UART_LSR_FE | UART_LSR_OE))) {
  433. dev_warn(up->dev, "We really rush into ERR/BI case"
  434. "status = 0x%02x", *status);
  435. /* For statistics only */
  436. if (*status & UART_LSR_BI) {
  437. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  438. up->port.icount.brk++;
  439. /*
  440. * We do the SysRQ and SAK checking
  441. * here because otherwise the break
  442. * may get masked by ignore_status_mask
  443. * or read_status_mask.
  444. */
  445. if (uart_handle_break(&up->port))
  446. goto ignore_char;
  447. } else if (*status & UART_LSR_PE)
  448. up->port.icount.parity++;
  449. else if (*status & UART_LSR_FE)
  450. up->port.icount.frame++;
  451. if (*status & UART_LSR_OE)
  452. up->port.icount.overrun++;
  453. /* Mask off conditions which should be ignored. */
  454. *status &= up->port.read_status_mask;
  455. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  456. if (up->port.cons &&
  457. up->port.cons->index == up->port.line) {
  458. /* Recover the break flag from console xmit */
  459. *status |= up->lsr_break_flag;
  460. up->lsr_break_flag = 0;
  461. }
  462. #endif
  463. if (*status & UART_LSR_BI) {
  464. flag = TTY_BREAK;
  465. } else if (*status & UART_LSR_PE)
  466. flag = TTY_PARITY;
  467. else if (*status & UART_LSR_FE)
  468. flag = TTY_FRAME;
  469. }
  470. if (uart_handle_sysrq_char(&up->port, ch))
  471. goto ignore_char;
  472. uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
  473. ignore_char:
  474. *status = serial_in(up, UART_LSR);
  475. } while ((*status & UART_LSR_DR) && max_count--);
  476. tty_flip_buffer_push(tty);
  477. }
  478. static void transmit_chars(struct uart_hsu_port *up)
  479. {
  480. struct circ_buf *xmit = &up->port.state->xmit;
  481. int count;
  482. int i = 0; /* for debug use */
  483. if (up->port.x_char) {
  484. serial_out(up, UART_TX, up->port.x_char);
  485. up->port.icount.tx++;
  486. up->port.x_char = 0;
  487. return;
  488. }
  489. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  490. serial_hsu_stop_tx(&up->port);
  491. return;
  492. }
  493. #ifndef MFD_HSU_A0_STEPPING
  494. count = up->port.fifosize / 2;
  495. #else
  496. /*
  497. * A0 only supports fully empty IRQ, and the first char written
  498. * into it won't clear the EMPT bit, so we may need be cautious
  499. * by useing a shorter buffer
  500. */
  501. /* count = up->port.fifosize; */
  502. count = up->port.fifosize - 4;
  503. #endif
  504. do {
  505. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  506. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  507. i++;
  508. up->port.icount.tx++;
  509. if (uart_circ_empty(xmit))
  510. break;
  511. } while (--count > 0);
  512. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  513. uart_write_wakeup(&up->port);
  514. if (uart_circ_empty(xmit))
  515. serial_hsu_stop_tx(&up->port);
  516. }
  517. static inline void check_modem_status(struct uart_hsu_port *up)
  518. {
  519. int status;
  520. status = serial_in(up, UART_MSR);
  521. if ((status & UART_MSR_ANY_DELTA) == 0)
  522. return;
  523. if (status & UART_MSR_TERI)
  524. up->port.icount.rng++;
  525. if (status & UART_MSR_DDSR)
  526. up->port.icount.dsr++;
  527. /* We may only get DDCD when HW init and reset */
  528. if (status & UART_MSR_DDCD)
  529. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  530. /* will start/stop_tx accordingly */
  531. if (status & UART_MSR_DCTS)
  532. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  533. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  534. }
  535. /*
  536. * This handles the interrupt from one port.
  537. */
  538. static irqreturn_t port_irq(int irq, void *dev_id)
  539. {
  540. struct uart_hsu_port *up = dev_id;
  541. unsigned int iir, lsr;
  542. unsigned long flags;
  543. if (unlikely(!up->running))
  544. return IRQ_NONE;
  545. if (up->use_dma) {
  546. lsr = serial_in(up, UART_LSR);
  547. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
  548. UART_LSR_FE | UART_LSR_OE)))
  549. dev_warn(up->dev,
  550. "Got lsr irq while using DMA, lsr = 0x%2x\n",
  551. lsr);
  552. check_modem_status(up);
  553. return IRQ_HANDLED;
  554. }
  555. spin_lock_irqsave(&up->port.lock, flags);
  556. iir = serial_in(up, UART_IIR);
  557. if (iir & UART_IIR_NO_INT) {
  558. spin_unlock_irqrestore(&up->port.lock, flags);
  559. return IRQ_NONE;
  560. }
  561. lsr = serial_in(up, UART_LSR);
  562. if (lsr & UART_LSR_DR)
  563. receive_chars(up, &lsr);
  564. /* lsr will be renewed during the receive_chars */
  565. if (lsr & UART_LSR_THRE)
  566. transmit_chars(up);
  567. spin_unlock_irqrestore(&up->port.lock, flags);
  568. return IRQ_HANDLED;
  569. }
  570. static inline void dma_chan_irq(struct hsu_dma_chan *chan)
  571. {
  572. struct uart_hsu_port *up = chan->uport;
  573. unsigned long flags;
  574. u32 int_sts;
  575. spin_lock_irqsave(&up->port.lock, flags);
  576. if (!up->use_dma || !up->running)
  577. goto exit;
  578. /*
  579. * No matter what situation, need read clear the IRQ status
  580. * There is a bug, see Errata 5, HSD 2900918
  581. */
  582. int_sts = chan_readl(chan, HSU_CH_SR);
  583. /* Rx channel */
  584. if (chan->dirt == DMA_FROM_DEVICE)
  585. hsu_dma_rx(up, int_sts);
  586. /* Tx channel */
  587. if (chan->dirt == DMA_TO_DEVICE) {
  588. /* dma for irq should be done */
  589. chan_writel(chan, HSU_CH_CR, 0x0);
  590. up->dma_tx_on = 0;
  591. hsu_dma_tx(up);
  592. }
  593. exit:
  594. spin_unlock_irqrestore(&up->port.lock, flags);
  595. return;
  596. }
  597. static irqreturn_t dma_irq(int irq, void *dev_id)
  598. {
  599. struct hsu_port *hsu = dev_id;
  600. u32 int_sts, i;
  601. int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
  602. /* Currently we only have 6 channels may be used */
  603. for (i = 0; i < 6; i++) {
  604. if (int_sts & 0x1)
  605. dma_chan_irq(&hsu->chans[i]);
  606. int_sts >>= 1;
  607. }
  608. return IRQ_HANDLED;
  609. }
  610. static unsigned int serial_hsu_tx_empty(struct uart_port *port)
  611. {
  612. struct uart_hsu_port *up =
  613. container_of(port, struct uart_hsu_port, port);
  614. unsigned long flags;
  615. unsigned int ret;
  616. spin_lock_irqsave(&up->port.lock, flags);
  617. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  618. spin_unlock_irqrestore(&up->port.lock, flags);
  619. return ret;
  620. }
  621. static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
  622. {
  623. struct uart_hsu_port *up =
  624. container_of(port, struct uart_hsu_port, port);
  625. unsigned char status;
  626. unsigned int ret;
  627. status = serial_in(up, UART_MSR);
  628. ret = 0;
  629. if (status & UART_MSR_DCD)
  630. ret |= TIOCM_CAR;
  631. if (status & UART_MSR_RI)
  632. ret |= TIOCM_RNG;
  633. if (status & UART_MSR_DSR)
  634. ret |= TIOCM_DSR;
  635. if (status & UART_MSR_CTS)
  636. ret |= TIOCM_CTS;
  637. return ret;
  638. }
  639. static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  640. {
  641. struct uart_hsu_port *up =
  642. container_of(port, struct uart_hsu_port, port);
  643. unsigned char mcr = 0;
  644. if (mctrl & TIOCM_RTS)
  645. mcr |= UART_MCR_RTS;
  646. if (mctrl & TIOCM_DTR)
  647. mcr |= UART_MCR_DTR;
  648. if (mctrl & TIOCM_OUT1)
  649. mcr |= UART_MCR_OUT1;
  650. if (mctrl & TIOCM_OUT2)
  651. mcr |= UART_MCR_OUT2;
  652. if (mctrl & TIOCM_LOOP)
  653. mcr |= UART_MCR_LOOP;
  654. mcr |= up->mcr;
  655. serial_out(up, UART_MCR, mcr);
  656. }
  657. static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
  658. {
  659. struct uart_hsu_port *up =
  660. container_of(port, struct uart_hsu_port, port);
  661. unsigned long flags;
  662. spin_lock_irqsave(&up->port.lock, flags);
  663. if (break_state == -1)
  664. up->lcr |= UART_LCR_SBC;
  665. else
  666. up->lcr &= ~UART_LCR_SBC;
  667. serial_out(up, UART_LCR, up->lcr);
  668. spin_unlock_irqrestore(&up->port.lock, flags);
  669. }
  670. /*
  671. * What special to do:
  672. * 1. chose the 64B fifo mode
  673. * 2. make sure not to select half empty mode for A0 stepping
  674. * 3. start dma or pio depends on configuration
  675. * 4. we only allocate dma memory when needed
  676. */
  677. static int serial_hsu_startup(struct uart_port *port)
  678. {
  679. struct uart_hsu_port *up =
  680. container_of(port, struct uart_hsu_port, port);
  681. unsigned long flags;
  682. /*
  683. * Clear the FIFO buffers and disable them.
  684. * (they will be reenabled in set_termios())
  685. */
  686. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  687. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  688. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  689. serial_out(up, UART_FCR, 0);
  690. /* Clear the interrupt registers. */
  691. (void) serial_in(up, UART_LSR);
  692. (void) serial_in(up, UART_RX);
  693. (void) serial_in(up, UART_IIR);
  694. (void) serial_in(up, UART_MSR);
  695. /* Now, initialize the UART, default is 8n1 */
  696. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  697. spin_lock_irqsave(&up->port.lock, flags);
  698. up->port.mctrl |= TIOCM_OUT2;
  699. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  700. /*
  701. * Finally, enable interrupts. Note: Modem status interrupts
  702. * are set via set_termios(), which will be occurring imminently
  703. * anyway, so we don't enable them here.
  704. */
  705. if (!up->use_dma)
  706. up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
  707. else
  708. up->ier = 0;
  709. serial_out(up, UART_IER, up->ier);
  710. spin_unlock_irqrestore(&up->port.lock, flags);
  711. /* DMA init */
  712. /* When use DMA, TX/RX's FIFO and IRQ should be disabled */
  713. if (up->use_dma) {
  714. struct hsu_dma_buffer *dbuf;
  715. struct circ_buf *xmit = &port->state->xmit;
  716. up->dma_tx_on = 0;
  717. /* First allocate the RX buffer */
  718. dbuf = &up->rxbuf;
  719. dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
  720. if (!dbuf->buf) {
  721. up->use_dma = 0;
  722. goto exit;
  723. }
  724. dbuf->dma_addr = dma_map_single(port->dev,
  725. dbuf->buf,
  726. HSU_DMA_BUF_SIZE,
  727. DMA_FROM_DEVICE);
  728. dbuf->dma_size = HSU_DMA_BUF_SIZE;
  729. /* Start the RX channel right now */
  730. hsu_dma_start_rx_chan(up->rxc, dbuf);
  731. /* Next init the TX DMA */
  732. dbuf = &up->txbuf;
  733. dbuf->buf = xmit->buf;
  734. dbuf->dma_addr = dma_map_single(port->dev,
  735. dbuf->buf,
  736. UART_XMIT_SIZE,
  737. DMA_TO_DEVICE);
  738. dbuf->dma_size = UART_XMIT_SIZE;
  739. /* This should not be changed all around */
  740. chan_writel(up->txc, HSU_CH_BSR, 32);
  741. chan_writel(up->txc, HSU_CH_MOTSR, 4);
  742. dbuf->ofs = 0;
  743. }
  744. exit:
  745. /* And clear the interrupt registers again for luck. */
  746. (void) serial_in(up, UART_LSR);
  747. (void) serial_in(up, UART_RX);
  748. (void) serial_in(up, UART_IIR);
  749. (void) serial_in(up, UART_MSR);
  750. up->running = 1;
  751. return 0;
  752. }
  753. static void serial_hsu_shutdown(struct uart_port *port)
  754. {
  755. struct uart_hsu_port *up =
  756. container_of(port, struct uart_hsu_port, port);
  757. unsigned long flags;
  758. del_timer_sync(&up->rxc->rx_timer);
  759. /* Disable interrupts from this port */
  760. up->ier = 0;
  761. serial_out(up, UART_IER, 0);
  762. up->running = 0;
  763. spin_lock_irqsave(&up->port.lock, flags);
  764. up->port.mctrl &= ~TIOCM_OUT2;
  765. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  766. spin_unlock_irqrestore(&up->port.lock, flags);
  767. /* Disable break condition and FIFOs */
  768. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  769. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  770. UART_FCR_CLEAR_RCVR |
  771. UART_FCR_CLEAR_XMIT);
  772. serial_out(up, UART_FCR, 0);
  773. }
  774. static void
  775. serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
  776. struct ktermios *old)
  777. {
  778. struct uart_hsu_port *up =
  779. container_of(port, struct uart_hsu_port, port);
  780. struct tty_struct *tty = port->state->port.tty;
  781. unsigned char cval, fcr = 0;
  782. unsigned long flags;
  783. unsigned int baud, quot;
  784. u32 mul = 0x3600;
  785. u32 ps = 0x10;
  786. switch (termios->c_cflag & CSIZE) {
  787. case CS5:
  788. cval = UART_LCR_WLEN5;
  789. break;
  790. case CS6:
  791. cval = UART_LCR_WLEN6;
  792. break;
  793. case CS7:
  794. cval = UART_LCR_WLEN7;
  795. break;
  796. default:
  797. case CS8:
  798. cval = UART_LCR_WLEN8;
  799. break;
  800. }
  801. /* CMSPAR isn't supported by this driver */
  802. if (tty)
  803. tty->termios->c_cflag &= ~CMSPAR;
  804. if (termios->c_cflag & CSTOPB)
  805. cval |= UART_LCR_STOP;
  806. if (termios->c_cflag & PARENB)
  807. cval |= UART_LCR_PARITY;
  808. if (!(termios->c_cflag & PARODD))
  809. cval |= UART_LCR_EPAR;
  810. /*
  811. * For those basic low baud rate we can get the direct
  812. * scalar from 2746800, like 115200 = 2746800/24, for those
  813. * higher baud rate, we have to handle them case by case,
  814. * but DIV reg is never touched as its default value 0x3d09
  815. */
  816. baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  817. quot = uart_get_divisor(port, baud);
  818. switch (baud) {
  819. case 3500000:
  820. mul = 0x3345;
  821. ps = 0xC;
  822. quot = 1;
  823. break;
  824. case 2500000:
  825. mul = 0x2710;
  826. ps = 0x10;
  827. quot = 1;
  828. break;
  829. case 18432000:
  830. mul = 0x2400;
  831. ps = 0x10;
  832. quot = 1;
  833. break;
  834. case 1500000:
  835. mul = 0x1D4C;
  836. ps = 0xc;
  837. quot = 1;
  838. break;
  839. default:
  840. ;
  841. }
  842. if ((up->port.uartclk / quot) < (2400 * 16))
  843. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
  844. else if ((up->port.uartclk / quot) < (230400 * 16))
  845. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
  846. else
  847. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
  848. fcr |= UART_FCR_HSU_64B_FIFO;
  849. #ifdef MFD_HSU_A0_STEPPING
  850. /* A0 doesn't support half empty IRQ */
  851. fcr |= UART_FCR_FULL_EMPT_TXI;
  852. #endif
  853. /*
  854. * Ok, we're now changing the port state. Do it with
  855. * interrupts disabled.
  856. */
  857. spin_lock_irqsave(&up->port.lock, flags);
  858. /* Update the per-port timeout */
  859. uart_update_timeout(port, termios->c_cflag, baud);
  860. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  861. if (termios->c_iflag & INPCK)
  862. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  863. if (termios->c_iflag & (BRKINT | PARMRK))
  864. up->port.read_status_mask |= UART_LSR_BI;
  865. /* Characters to ignore */
  866. up->port.ignore_status_mask = 0;
  867. if (termios->c_iflag & IGNPAR)
  868. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  869. if (termios->c_iflag & IGNBRK) {
  870. up->port.ignore_status_mask |= UART_LSR_BI;
  871. /*
  872. * If we're ignoring parity and break indicators,
  873. * ignore overruns too (for real raw support).
  874. */
  875. if (termios->c_iflag & IGNPAR)
  876. up->port.ignore_status_mask |= UART_LSR_OE;
  877. }
  878. /* Ignore all characters if CREAD is not set */
  879. if ((termios->c_cflag & CREAD) == 0)
  880. up->port.ignore_status_mask |= UART_LSR_DR;
  881. /*
  882. * CTS flow control flag and modem status interrupts, disable
  883. * MSI by default
  884. */
  885. up->ier &= ~UART_IER_MSI;
  886. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  887. up->ier |= UART_IER_MSI;
  888. serial_out(up, UART_IER, up->ier);
  889. if (termios->c_cflag & CRTSCTS)
  890. up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
  891. else
  892. up->mcr &= ~UART_MCR_AFE;
  893. serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
  894. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  895. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  896. serial_out(up, UART_LCR, cval); /* reset DLAB */
  897. serial_out(up, UART_MUL, mul); /* set MUL */
  898. serial_out(up, UART_PS, ps); /* set PS */
  899. up->lcr = cval; /* Save LCR */
  900. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  901. serial_out(up, UART_FCR, fcr);
  902. spin_unlock_irqrestore(&up->port.lock, flags);
  903. }
  904. static void
  905. serial_hsu_pm(struct uart_port *port, unsigned int state,
  906. unsigned int oldstate)
  907. {
  908. }
  909. static void serial_hsu_release_port(struct uart_port *port)
  910. {
  911. }
  912. static int serial_hsu_request_port(struct uart_port *port)
  913. {
  914. return 0;
  915. }
  916. static void serial_hsu_config_port(struct uart_port *port, int flags)
  917. {
  918. #if 0
  919. struct uart_hsu_port *up =
  920. container_of(port, struct uart_hsu_port, port);
  921. up->port.type = PORT_MFD;
  922. #endif
  923. }
  924. static int
  925. serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
  926. {
  927. /* We don't want the core code to modify any port params */
  928. return -EINVAL;
  929. }
  930. static const char *
  931. serial_hsu_type(struct uart_port *port)
  932. {
  933. struct uart_hsu_port *up =
  934. container_of(port, struct uart_hsu_port, port);
  935. return up->name;
  936. }
  937. /* Mainly for uart console use */
  938. static struct uart_hsu_port *serial_hsu_ports[3];
  939. static struct uart_driver serial_hsu_reg;
  940. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  941. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  942. /* Wait for transmitter & holding register to empty */
  943. static inline void wait_for_xmitr(struct uart_hsu_port *up)
  944. {
  945. unsigned int status, tmout = 1000;
  946. /* Wait up to 1ms for the character to be sent. */
  947. do {
  948. status = serial_in(up, UART_LSR);
  949. if (status & UART_LSR_BI)
  950. up->lsr_break_flag = UART_LSR_BI;
  951. if (--tmout == 0)
  952. break;
  953. udelay(1);
  954. } while (!(status & BOTH_EMPTY));
  955. /* Wait up to 1s for flow control if necessary */
  956. if (up->port.flags & UPF_CONS_FLOW) {
  957. tmout = 1000000;
  958. while (--tmout &&
  959. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  960. udelay(1);
  961. }
  962. }
  963. static void serial_hsu_console_putchar(struct uart_port *port, int ch)
  964. {
  965. struct uart_hsu_port *up =
  966. container_of(port, struct uart_hsu_port, port);
  967. wait_for_xmitr(up);
  968. serial_out(up, UART_TX, ch);
  969. }
  970. /*
  971. * Print a string to the serial port trying not to disturb
  972. * any possible real use of the port...
  973. *
  974. * The console_lock must be held when we get here.
  975. */
  976. static void
  977. serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
  978. {
  979. struct uart_hsu_port *up = serial_hsu_ports[co->index];
  980. unsigned long flags;
  981. unsigned int ier;
  982. int locked = 1;
  983. local_irq_save(flags);
  984. if (up->port.sysrq)
  985. locked = 0;
  986. else if (oops_in_progress) {
  987. locked = spin_trylock(&up->port.lock);
  988. } else
  989. spin_lock(&up->port.lock);
  990. /* First save the IER then disable the interrupts */
  991. ier = serial_in(up, UART_IER);
  992. serial_out(up, UART_IER, 0);
  993. uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
  994. /*
  995. * Finally, wait for transmitter to become empty
  996. * and restore the IER
  997. */
  998. wait_for_xmitr(up);
  999. serial_out(up, UART_IER, ier);
  1000. if (locked)
  1001. spin_unlock(&up->port.lock);
  1002. local_irq_restore(flags);
  1003. }
  1004. static struct console serial_hsu_console;
  1005. static int __init
  1006. serial_hsu_console_setup(struct console *co, char *options)
  1007. {
  1008. struct uart_hsu_port *up;
  1009. int baud = 115200;
  1010. int bits = 8;
  1011. int parity = 'n';
  1012. int flow = 'n';
  1013. int ret;
  1014. if (co->index == -1 || co->index >= serial_hsu_reg.nr)
  1015. co->index = 0;
  1016. up = serial_hsu_ports[co->index];
  1017. if (!up)
  1018. return -ENODEV;
  1019. if (options)
  1020. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1021. ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
  1022. return ret;
  1023. }
  1024. static struct console serial_hsu_console = {
  1025. .name = "ttyMFD",
  1026. .write = serial_hsu_console_write,
  1027. .device = uart_console_device,
  1028. .setup = serial_hsu_console_setup,
  1029. .flags = CON_PRINTBUFFER,
  1030. .index = 2,
  1031. .data = &serial_hsu_reg,
  1032. };
  1033. #endif
  1034. struct uart_ops serial_hsu_pops = {
  1035. .tx_empty = serial_hsu_tx_empty,
  1036. .set_mctrl = serial_hsu_set_mctrl,
  1037. .get_mctrl = serial_hsu_get_mctrl,
  1038. .stop_tx = serial_hsu_stop_tx,
  1039. .start_tx = serial_hsu_start_tx,
  1040. .stop_rx = serial_hsu_stop_rx,
  1041. .enable_ms = serial_hsu_enable_ms,
  1042. .break_ctl = serial_hsu_break_ctl,
  1043. .startup = serial_hsu_startup,
  1044. .shutdown = serial_hsu_shutdown,
  1045. .set_termios = serial_hsu_set_termios,
  1046. .pm = serial_hsu_pm,
  1047. .type = serial_hsu_type,
  1048. .release_port = serial_hsu_release_port,
  1049. .request_port = serial_hsu_request_port,
  1050. .config_port = serial_hsu_config_port,
  1051. .verify_port = serial_hsu_verify_port,
  1052. };
  1053. static struct uart_driver serial_hsu_reg = {
  1054. .owner = THIS_MODULE,
  1055. .driver_name = "MFD serial",
  1056. .dev_name = "ttyMFD",
  1057. .major = TTY_MAJOR,
  1058. .minor = 128,
  1059. .nr = 3,
  1060. };
  1061. #ifdef CONFIG_PM
  1062. static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
  1063. {
  1064. struct uart_hsu_port *up;
  1065. up = pci_get_drvdata(pdev);
  1066. if (!up)
  1067. return 0;
  1068. uart_suspend_port(&serial_hsu_reg, &up->port);
  1069. return 0;
  1070. }
  1071. static int serial_hsu_resume(struct pci_dev *pdev)
  1072. {
  1073. struct uart_hsu_port *up;
  1074. up = pci_get_drvdata(pdev);
  1075. if (!up)
  1076. return 0;
  1077. uart_resume_port(&serial_hsu_reg, &up->port);
  1078. return 0;
  1079. }
  1080. #else
  1081. #define serial_hsu_suspend NULL
  1082. #define serial_hsu_resume NULL
  1083. #endif
  1084. /* temp global pointer before we settle down on using one or four PCI dev */
  1085. static struct hsu_port *phsu;
  1086. static int serial_hsu_probe(struct pci_dev *pdev,
  1087. const struct pci_device_id *ent)
  1088. {
  1089. struct uart_hsu_port *uport;
  1090. int index, ret;
  1091. printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
  1092. pdev->vendor, pdev->device);
  1093. switch (pdev->device) {
  1094. case 0x081B:
  1095. index = 0;
  1096. break;
  1097. case 0x081C:
  1098. index = 1;
  1099. break;
  1100. case 0x081D:
  1101. index = 2;
  1102. break;
  1103. case 0x081E:
  1104. /* internal DMA controller */
  1105. index = 3;
  1106. break;
  1107. default:
  1108. dev_err(&pdev->dev, "HSU: out of index!");
  1109. return -ENODEV;
  1110. }
  1111. ret = pci_enable_device(pdev);
  1112. if (ret)
  1113. return ret;
  1114. if (index == 3) {
  1115. /* DMA controller */
  1116. ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
  1117. if (ret) {
  1118. dev_err(&pdev->dev, "can not get IRQ\n");
  1119. goto err_disable;
  1120. }
  1121. pci_set_drvdata(pdev, phsu);
  1122. } else {
  1123. /* UART port 0~2 */
  1124. uport = &phsu->port[index];
  1125. uport->port.irq = pdev->irq;
  1126. uport->port.dev = &pdev->dev;
  1127. uport->dev = &pdev->dev;
  1128. ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
  1129. if (ret) {
  1130. dev_err(&pdev->dev, "can not get IRQ\n");
  1131. goto err_disable;
  1132. }
  1133. uart_add_one_port(&serial_hsu_reg, &uport->port);
  1134. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  1135. if (index == 2) {
  1136. register_console(&serial_hsu_console);
  1137. uport->port.cons = &serial_hsu_console;
  1138. }
  1139. #endif
  1140. pci_set_drvdata(pdev, uport);
  1141. }
  1142. return 0;
  1143. err_disable:
  1144. pci_disable_device(pdev);
  1145. return ret;
  1146. }
  1147. static void hsu_dma_rx_timeout(unsigned long data)
  1148. {
  1149. struct hsu_dma_chan *chan = (void *)data;
  1150. struct uart_hsu_port *up = chan->uport;
  1151. struct hsu_dma_buffer *dbuf = &up->rxbuf;
  1152. int count = 0;
  1153. unsigned long flags;
  1154. spin_lock_irqsave(&up->port.lock, flags);
  1155. count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
  1156. if (!count) {
  1157. mod_timer(&chan->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
  1158. goto exit;
  1159. }
  1160. hsu_dma_rx(up, 0);
  1161. exit:
  1162. spin_unlock_irqrestore(&up->port.lock, flags);
  1163. }
  1164. static void hsu_global_init(void)
  1165. {
  1166. struct hsu_port *hsu;
  1167. struct uart_hsu_port *uport;
  1168. struct hsu_dma_chan *dchan;
  1169. int i, ret;
  1170. hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
  1171. if (!hsu)
  1172. return;
  1173. /* Get basic io resource and map it */
  1174. hsu->paddr = 0xffa28000;
  1175. hsu->iolen = 0x1000;
  1176. if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
  1177. pr_warning("HSU: error in request mem region\n");
  1178. hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
  1179. if (!hsu->reg) {
  1180. pr_err("HSU: error in ioremap\n");
  1181. ret = -ENOMEM;
  1182. goto err_free_region;
  1183. }
  1184. /* Initialise the 3 UART ports */
  1185. uport = hsu->port;
  1186. for (i = 0; i < 3; i++) {
  1187. uport->port.type = PORT_MFD;
  1188. uport->port.iotype = UPIO_MEM;
  1189. uport->port.mapbase = (resource_size_t)hsu->paddr
  1190. + HSU_PORT_REG_OFFSET
  1191. + i * HSU_PORT_REG_LENGTH;
  1192. uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
  1193. + i * HSU_PORT_REG_LENGTH;
  1194. sprintf(uport->name, "hsu_port%d", i);
  1195. uport->port.fifosize = 64;
  1196. uport->port.ops = &serial_hsu_pops;
  1197. uport->port.line = i;
  1198. uport->port.flags = UPF_IOREMAP;
  1199. /* make the maxim support rate to 2746800 bps */
  1200. uport->port.uartclk = 115200 * 24 * 16;
  1201. uport->running = 0;
  1202. uport->txc = &hsu->chans[i * 2];
  1203. uport->rxc = &hsu->chans[i * 2 + 1];
  1204. serial_hsu_ports[i] = uport;
  1205. uport->index = i;
  1206. uport++;
  1207. }
  1208. /* Initialise 6 dma channels */
  1209. dchan = hsu->chans;
  1210. for (i = 0; i < 6; i++) {
  1211. dchan->id = i;
  1212. dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1213. dchan->uport = &hsu->port[i/2];
  1214. dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
  1215. i * HSU_DMA_CHANS_REG_LENGTH;
  1216. /* Work around for RX */
  1217. if (dchan->dirt == DMA_FROM_DEVICE) {
  1218. init_timer(&dchan->rx_timer);
  1219. dchan->rx_timer.function = hsu_dma_rx_timeout;
  1220. dchan->rx_timer.data = (unsigned long)dchan;
  1221. }
  1222. dchan++;
  1223. }
  1224. phsu = hsu;
  1225. hsu_debugfs_init(hsu);
  1226. return;
  1227. err_free_region:
  1228. release_mem_region(hsu->paddr, hsu->iolen);
  1229. kfree(hsu);
  1230. return;
  1231. }
  1232. static void serial_hsu_remove(struct pci_dev *pdev)
  1233. {
  1234. struct hsu_port *hsu;
  1235. int i;
  1236. hsu = pci_get_drvdata(pdev);
  1237. if (!hsu)
  1238. return;
  1239. for (i = 0; i < 3; i++)
  1240. uart_remove_one_port(&serial_hsu_reg, &hsu->port[i].port);
  1241. pci_set_drvdata(pdev, NULL);
  1242. free_irq(hsu->irq, hsu);
  1243. pci_disable_device(pdev);
  1244. }
  1245. /* First 3 are UART ports, and the 4th is the DMA */
  1246. static const struct pci_device_id pci_ids[] __devinitdata = {
  1247. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
  1248. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
  1249. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
  1250. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
  1251. {},
  1252. };
  1253. static struct pci_driver hsu_pci_driver = {
  1254. .name = "HSU serial",
  1255. .id_table = pci_ids,
  1256. .probe = serial_hsu_probe,
  1257. .remove = __devexit_p(serial_hsu_remove),
  1258. .suspend = serial_hsu_suspend,
  1259. .resume = serial_hsu_resume,
  1260. };
  1261. static int __init hsu_pci_init(void)
  1262. {
  1263. int ret;
  1264. hsu_global_init();
  1265. ret = uart_register_driver(&serial_hsu_reg);
  1266. if (ret)
  1267. return ret;
  1268. return pci_register_driver(&hsu_pci_driver);
  1269. }
  1270. static void __exit hsu_pci_exit(void)
  1271. {
  1272. pci_unregister_driver(&hsu_pci_driver);
  1273. uart_unregister_driver(&serial_hsu_reg);
  1274. hsu_debugfs_remove(phsu);
  1275. kfree(phsu);
  1276. }
  1277. module_init(hsu_pci_init);
  1278. module_exit(hsu_pci_exit);
  1279. MODULE_LICENSE("GPL v2");
  1280. MODULE_ALIAS("platform:medfield-hsu");