mfd.c 36 KB

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