mfd.c 37 KB

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