mfd.c 36 KB

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