mfd.c 37 KB

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