mfd.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. 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. void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
  288. {
  289. dbuf->ofs = 0;
  290. chan_writel(rxc, HSU_CH_BSR, 32);
  291. chan_writel(rxc, HSU_CH_MOTSR, 4);
  292. chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
  293. chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
  294. chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
  295. | (0x1 << 16)
  296. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  297. );
  298. chan_writel(rxc, HSU_CH_CR, 0x3);
  299. }
  300. /* Protected by spin_lock_irqsave(port->lock) */
  301. static void serial_hsu_start_tx(struct uart_port *port)
  302. {
  303. struct uart_hsu_port *up =
  304. container_of(port, struct uart_hsu_port, port);
  305. if (up->use_dma) {
  306. hsu_dma_tx(up);
  307. } else if (!(up->ier & UART_IER_THRI)) {
  308. up->ier |= UART_IER_THRI;
  309. serial_out(up, UART_IER, up->ier);
  310. }
  311. }
  312. static void serial_hsu_stop_tx(struct uart_port *port)
  313. {
  314. struct uart_hsu_port *up =
  315. container_of(port, struct uart_hsu_port, port);
  316. struct hsu_dma_chan *txc = up->txc;
  317. if (up->use_dma)
  318. chan_writel(txc, HSU_CH_CR, 0x0);
  319. else if (up->ier & UART_IER_THRI) {
  320. up->ier &= ~UART_IER_THRI;
  321. serial_out(up, UART_IER, up->ier);
  322. }
  323. }
  324. /* This is always called in spinlock protected mode, so
  325. * modify timeout timer is safe here */
  326. void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
  327. {
  328. struct hsu_dma_buffer *dbuf = &up->rxbuf;
  329. struct hsu_dma_chan *chan = up->rxc;
  330. struct uart_port *port = &up->port;
  331. struct tty_port *tport = &port->state->port;
  332. int count;
  333. /*
  334. * First need to know how many is already transferred,
  335. * then check if its a timeout DMA irq, and return
  336. * the trail bytes out, push them up and reenable the
  337. * channel
  338. */
  339. /* Timeout IRQ, need wait some time, see Errata 2 */
  340. if (int_sts & 0xf00)
  341. udelay(2);
  342. /* Stop the channel */
  343. chan_writel(chan, HSU_CH_CR, 0x0);
  344. count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
  345. if (!count) {
  346. /* Restart the channel before we leave */
  347. chan_writel(chan, HSU_CH_CR, 0x3);
  348. return;
  349. }
  350. dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
  351. dbuf->dma_size, DMA_FROM_DEVICE);
  352. /*
  353. * Head will only wrap around when we recycle
  354. * the DMA buffer, and when that happens, we
  355. * explicitly set tail to 0. So head will
  356. * always be greater than tail.
  357. */
  358. tty_insert_flip_string(tport, dbuf->buf, count);
  359. port->icount.rx += count;
  360. dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
  361. dbuf->dma_size, DMA_FROM_DEVICE);
  362. /* Reprogram the channel */
  363. chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
  364. chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
  365. chan_writel(chan, HSU_CH_DCR, 0x1
  366. | (0x1 << 8)
  367. | (0x1 << 16)
  368. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  369. );
  370. tty_flip_buffer_push(tport);
  371. chan_writel(chan, HSU_CH_CR, 0x3);
  372. }
  373. static void serial_hsu_stop_rx(struct uart_port *port)
  374. {
  375. struct uart_hsu_port *up =
  376. container_of(port, struct uart_hsu_port, port);
  377. struct hsu_dma_chan *chan = up->rxc;
  378. if (up->use_dma)
  379. chan_writel(chan, HSU_CH_CR, 0x2);
  380. else {
  381. up->ier &= ~UART_IER_RLSI;
  382. up->port.read_status_mask &= ~UART_LSR_DR;
  383. serial_out(up, UART_IER, up->ier);
  384. }
  385. }
  386. static inline void receive_chars(struct uart_hsu_port *up, int *status)
  387. {
  388. unsigned int ch, flag;
  389. unsigned int max_count = 256;
  390. do {
  391. ch = serial_in(up, UART_RX);
  392. flag = TTY_NORMAL;
  393. up->port.icount.rx++;
  394. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  395. UART_LSR_FE | UART_LSR_OE))) {
  396. dev_warn(up->dev, "We really rush into ERR/BI case"
  397. "status = 0x%02x", *status);
  398. /* For statistics only */
  399. if (*status & UART_LSR_BI) {
  400. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  401. up->port.icount.brk++;
  402. /*
  403. * We do the SysRQ and SAK checking
  404. * here because otherwise the break
  405. * may get masked by ignore_status_mask
  406. * or read_status_mask.
  407. */
  408. if (uart_handle_break(&up->port))
  409. goto ignore_char;
  410. } else if (*status & UART_LSR_PE)
  411. up->port.icount.parity++;
  412. else if (*status & UART_LSR_FE)
  413. up->port.icount.frame++;
  414. if (*status & UART_LSR_OE)
  415. up->port.icount.overrun++;
  416. /* Mask off conditions which should be ignored. */
  417. *status &= up->port.read_status_mask;
  418. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  419. if (up->port.cons &&
  420. up->port.cons->index == up->port.line) {
  421. /* Recover the break flag from console xmit */
  422. *status |= up->lsr_break_flag;
  423. up->lsr_break_flag = 0;
  424. }
  425. #endif
  426. if (*status & UART_LSR_BI) {
  427. flag = TTY_BREAK;
  428. } else if (*status & UART_LSR_PE)
  429. flag = TTY_PARITY;
  430. else if (*status & UART_LSR_FE)
  431. flag = TTY_FRAME;
  432. }
  433. if (uart_handle_sysrq_char(&up->port, ch))
  434. goto ignore_char;
  435. uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
  436. ignore_char:
  437. *status = serial_in(up, UART_LSR);
  438. } while ((*status & UART_LSR_DR) && max_count--);
  439. tty_flip_buffer_push(&up->port.state->port);
  440. }
  441. static void transmit_chars(struct uart_hsu_port *up)
  442. {
  443. struct circ_buf *xmit = &up->port.state->xmit;
  444. int count;
  445. if (up->port.x_char) {
  446. serial_out(up, UART_TX, up->port.x_char);
  447. up->port.icount.tx++;
  448. up->port.x_char = 0;
  449. return;
  450. }
  451. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  452. serial_hsu_stop_tx(&up->port);
  453. return;
  454. }
  455. /* The IRQ is for TX FIFO half-empty */
  456. count = up->port.fifosize / 2;
  457. do {
  458. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  459. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  460. up->port.icount.tx++;
  461. if (uart_circ_empty(xmit))
  462. break;
  463. } while (--count > 0);
  464. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  465. uart_write_wakeup(&up->port);
  466. if (uart_circ_empty(xmit))
  467. serial_hsu_stop_tx(&up->port);
  468. }
  469. static inline void check_modem_status(struct uart_hsu_port *up)
  470. {
  471. int status;
  472. status = serial_in(up, UART_MSR);
  473. if ((status & UART_MSR_ANY_DELTA) == 0)
  474. return;
  475. if (status & UART_MSR_TERI)
  476. up->port.icount.rng++;
  477. if (status & UART_MSR_DDSR)
  478. up->port.icount.dsr++;
  479. /* We may only get DDCD when HW init and reset */
  480. if (status & UART_MSR_DDCD)
  481. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  482. /* Will start/stop_tx accordingly */
  483. if (status & UART_MSR_DCTS)
  484. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  485. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  486. }
  487. /*
  488. * This handles the interrupt from one port.
  489. */
  490. static irqreturn_t port_irq(int irq, void *dev_id)
  491. {
  492. struct uart_hsu_port *up = dev_id;
  493. unsigned int iir, lsr;
  494. unsigned long flags;
  495. if (unlikely(!up->running))
  496. return IRQ_NONE;
  497. spin_lock_irqsave(&up->port.lock, flags);
  498. if (up->use_dma) {
  499. lsr = serial_in(up, UART_LSR);
  500. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
  501. UART_LSR_FE | UART_LSR_OE)))
  502. dev_warn(up->dev,
  503. "Got lsr irq while using DMA, lsr = 0x%2x\n",
  504. lsr);
  505. check_modem_status(up);
  506. spin_unlock_irqrestore(&up->port.lock, flags);
  507. return IRQ_HANDLED;
  508. }
  509. iir = serial_in(up, UART_IIR);
  510. if (iir & UART_IIR_NO_INT) {
  511. spin_unlock_irqrestore(&up->port.lock, flags);
  512. return IRQ_NONE;
  513. }
  514. lsr = serial_in(up, UART_LSR);
  515. if (lsr & UART_LSR_DR)
  516. receive_chars(up, &lsr);
  517. check_modem_status(up);
  518. /* lsr will be renewed during the receive_chars */
  519. if (lsr & UART_LSR_THRE)
  520. transmit_chars(up);
  521. spin_unlock_irqrestore(&up->port.lock, flags);
  522. return IRQ_HANDLED;
  523. }
  524. static inline void dma_chan_irq(struct hsu_dma_chan *chan)
  525. {
  526. struct uart_hsu_port *up = chan->uport;
  527. unsigned long flags;
  528. u32 int_sts;
  529. spin_lock_irqsave(&up->port.lock, flags);
  530. if (!up->use_dma || !up->running)
  531. goto exit;
  532. /*
  533. * No matter what situation, need read clear the IRQ status
  534. * There is a bug, see Errata 5, HSD 2900918
  535. */
  536. int_sts = chan_readl(chan, HSU_CH_SR);
  537. /* Rx channel */
  538. if (chan->dirt == DMA_FROM_DEVICE)
  539. hsu_dma_rx(up, int_sts);
  540. /* Tx channel */
  541. if (chan->dirt == DMA_TO_DEVICE) {
  542. chan_writel(chan, HSU_CH_CR, 0x0);
  543. up->dma_tx_on = 0;
  544. hsu_dma_tx(up);
  545. }
  546. exit:
  547. spin_unlock_irqrestore(&up->port.lock, flags);
  548. return;
  549. }
  550. static irqreturn_t dma_irq(int irq, void *dev_id)
  551. {
  552. struct hsu_port *hsu = dev_id;
  553. u32 int_sts, i;
  554. int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
  555. /* Currently we only have 6 channels may be used */
  556. for (i = 0; i < 6; i++) {
  557. if (int_sts & 0x1)
  558. dma_chan_irq(&hsu->chans[i]);
  559. int_sts >>= 1;
  560. }
  561. return IRQ_HANDLED;
  562. }
  563. static unsigned int serial_hsu_tx_empty(struct uart_port *port)
  564. {
  565. struct uart_hsu_port *up =
  566. container_of(port, struct uart_hsu_port, port);
  567. unsigned long flags;
  568. unsigned int ret;
  569. spin_lock_irqsave(&up->port.lock, flags);
  570. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  571. spin_unlock_irqrestore(&up->port.lock, flags);
  572. return ret;
  573. }
  574. static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
  575. {
  576. struct uart_hsu_port *up =
  577. container_of(port, struct uart_hsu_port, port);
  578. unsigned char status;
  579. unsigned int ret;
  580. status = serial_in(up, UART_MSR);
  581. ret = 0;
  582. if (status & UART_MSR_DCD)
  583. ret |= TIOCM_CAR;
  584. if (status & UART_MSR_RI)
  585. ret |= TIOCM_RNG;
  586. if (status & UART_MSR_DSR)
  587. ret |= TIOCM_DSR;
  588. if (status & UART_MSR_CTS)
  589. ret |= TIOCM_CTS;
  590. return ret;
  591. }
  592. static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  593. {
  594. struct uart_hsu_port *up =
  595. container_of(port, struct uart_hsu_port, port);
  596. unsigned char mcr = 0;
  597. if (mctrl & TIOCM_RTS)
  598. mcr |= UART_MCR_RTS;
  599. if (mctrl & TIOCM_DTR)
  600. mcr |= UART_MCR_DTR;
  601. if (mctrl & TIOCM_OUT1)
  602. mcr |= UART_MCR_OUT1;
  603. if (mctrl & TIOCM_OUT2)
  604. mcr |= UART_MCR_OUT2;
  605. if (mctrl & TIOCM_LOOP)
  606. mcr |= UART_MCR_LOOP;
  607. mcr |= up->mcr;
  608. serial_out(up, UART_MCR, mcr);
  609. }
  610. static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
  611. {
  612. struct uart_hsu_port *up =
  613. container_of(port, struct uart_hsu_port, port);
  614. unsigned long flags;
  615. spin_lock_irqsave(&up->port.lock, flags);
  616. if (break_state == -1)
  617. up->lcr |= UART_LCR_SBC;
  618. else
  619. up->lcr &= ~UART_LCR_SBC;
  620. serial_out(up, UART_LCR, up->lcr);
  621. spin_unlock_irqrestore(&up->port.lock, flags);
  622. }
  623. /*
  624. * What special to do:
  625. * 1. chose the 64B fifo mode
  626. * 2. start dma or pio depends on configuration
  627. * 3. we only allocate dma memory when needed
  628. */
  629. static int serial_hsu_startup(struct uart_port *port)
  630. {
  631. struct uart_hsu_port *up =
  632. container_of(port, struct uart_hsu_port, port);
  633. unsigned long flags;
  634. pm_runtime_get_sync(up->dev);
  635. /*
  636. * Clear the FIFO buffers and disable them.
  637. * (they will be reenabled in set_termios())
  638. */
  639. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  640. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  641. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  642. serial_out(up, UART_FCR, 0);
  643. /* Clear the interrupt registers. */
  644. (void) serial_in(up, UART_LSR);
  645. (void) serial_in(up, UART_RX);
  646. (void) serial_in(up, UART_IIR);
  647. (void) serial_in(up, UART_MSR);
  648. /* Now, initialize the UART, default is 8n1 */
  649. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  650. spin_lock_irqsave(&up->port.lock, flags);
  651. up->port.mctrl |= TIOCM_OUT2;
  652. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  653. /*
  654. * Finally, enable interrupts. Note: Modem status interrupts
  655. * are set via set_termios(), which will be occurring imminently
  656. * anyway, so we don't enable them here.
  657. */
  658. if (!up->use_dma)
  659. up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
  660. else
  661. up->ier = 0;
  662. serial_out(up, UART_IER, up->ier);
  663. spin_unlock_irqrestore(&up->port.lock, flags);
  664. /* DMA init */
  665. if (up->use_dma) {
  666. struct hsu_dma_buffer *dbuf;
  667. struct circ_buf *xmit = &port->state->xmit;
  668. up->dma_tx_on = 0;
  669. /* First allocate the RX buffer */
  670. dbuf = &up->rxbuf;
  671. dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
  672. if (!dbuf->buf) {
  673. up->use_dma = 0;
  674. goto exit;
  675. }
  676. dbuf->dma_addr = dma_map_single(port->dev,
  677. dbuf->buf,
  678. HSU_DMA_BUF_SIZE,
  679. DMA_FROM_DEVICE);
  680. dbuf->dma_size = HSU_DMA_BUF_SIZE;
  681. /* Start the RX channel right now */
  682. hsu_dma_start_rx_chan(up->rxc, dbuf);
  683. /* Next init the TX DMA */
  684. dbuf = &up->txbuf;
  685. dbuf->buf = xmit->buf;
  686. dbuf->dma_addr = dma_map_single(port->dev,
  687. dbuf->buf,
  688. UART_XMIT_SIZE,
  689. DMA_TO_DEVICE);
  690. dbuf->dma_size = UART_XMIT_SIZE;
  691. /* This should not be changed all around */
  692. chan_writel(up->txc, HSU_CH_BSR, 32);
  693. chan_writel(up->txc, HSU_CH_MOTSR, 4);
  694. dbuf->ofs = 0;
  695. }
  696. exit:
  697. /* And clear the interrupt registers again for luck. */
  698. (void) serial_in(up, UART_LSR);
  699. (void) serial_in(up, UART_RX);
  700. (void) serial_in(up, UART_IIR);
  701. (void) serial_in(up, UART_MSR);
  702. up->running = 1;
  703. return 0;
  704. }
  705. static void serial_hsu_shutdown(struct uart_port *port)
  706. {
  707. struct uart_hsu_port *up =
  708. container_of(port, struct uart_hsu_port, port);
  709. unsigned long flags;
  710. /* Disable interrupts from this port */
  711. up->ier = 0;
  712. serial_out(up, UART_IER, 0);
  713. up->running = 0;
  714. spin_lock_irqsave(&up->port.lock, flags);
  715. up->port.mctrl &= ~TIOCM_OUT2;
  716. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  717. spin_unlock_irqrestore(&up->port.lock, flags);
  718. /* Disable break condition and FIFOs */
  719. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  720. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  721. UART_FCR_CLEAR_RCVR |
  722. UART_FCR_CLEAR_XMIT);
  723. serial_out(up, UART_FCR, 0);
  724. pm_runtime_put(up->dev);
  725. }
  726. static void
  727. serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
  728. struct ktermios *old)
  729. {
  730. struct uart_hsu_port *up =
  731. container_of(port, struct uart_hsu_port, port);
  732. unsigned char cval, fcr = 0;
  733. unsigned long flags;
  734. unsigned int baud, quot;
  735. u32 ps, mul;
  736. switch (termios->c_cflag & CSIZE) {
  737. case CS5:
  738. cval = UART_LCR_WLEN5;
  739. break;
  740. case CS6:
  741. cval = UART_LCR_WLEN6;
  742. break;
  743. case CS7:
  744. cval = UART_LCR_WLEN7;
  745. break;
  746. default:
  747. case CS8:
  748. cval = UART_LCR_WLEN8;
  749. break;
  750. }
  751. /* CMSPAR isn't supported by this driver */
  752. termios->c_cflag &= ~CMSPAR;
  753. if (termios->c_cflag & CSTOPB)
  754. cval |= UART_LCR_STOP;
  755. if (termios->c_cflag & PARENB)
  756. cval |= UART_LCR_PARITY;
  757. if (!(termios->c_cflag & PARODD))
  758. cval |= UART_LCR_EPAR;
  759. /*
  760. * The base clk is 50Mhz, and the baud rate come from:
  761. * baud = 50M * MUL / (DIV * PS * DLAB)
  762. *
  763. * For those basic low baud rate we can get the direct
  764. * scalar from 2746800, like 115200 = 2746800/24. For those
  765. * higher baud rate, we handle them case by case, mainly by
  766. * adjusting the MUL/PS registers, and DIV register is kept
  767. * as default value 0x3d09 to make things simple
  768. */
  769. baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  770. quot = 1;
  771. ps = 0x10;
  772. mul = 0x3600;
  773. switch (baud) {
  774. case 3500000:
  775. mul = 0x3345;
  776. ps = 0xC;
  777. break;
  778. case 1843200:
  779. mul = 0x2400;
  780. break;
  781. case 3000000:
  782. case 2500000:
  783. case 2000000:
  784. case 1500000:
  785. case 1000000:
  786. case 500000:
  787. /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
  788. mul = baud / 500000 * 0x9C4;
  789. break;
  790. default:
  791. /* Use uart_get_divisor to get quot for other baud rates */
  792. quot = 0;
  793. }
  794. if (!quot)
  795. quot = uart_get_divisor(port, baud);
  796. if ((up->port.uartclk / quot) < (2400 * 16))
  797. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
  798. else if ((up->port.uartclk / quot) < (230400 * 16))
  799. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
  800. else
  801. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
  802. fcr |= UART_FCR_HSU_64B_FIFO;
  803. /*
  804. * Ok, we're now changing the port state. Do it with
  805. * interrupts disabled.
  806. */
  807. spin_lock_irqsave(&up->port.lock, flags);
  808. /* Update the per-port timeout */
  809. uart_update_timeout(port, termios->c_cflag, baud);
  810. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  811. if (termios->c_iflag & INPCK)
  812. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  813. if (termios->c_iflag & (BRKINT | PARMRK))
  814. up->port.read_status_mask |= UART_LSR_BI;
  815. /* Characters to ignore */
  816. up->port.ignore_status_mask = 0;
  817. if (termios->c_iflag & IGNPAR)
  818. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  819. if (termios->c_iflag & IGNBRK) {
  820. up->port.ignore_status_mask |= UART_LSR_BI;
  821. /*
  822. * If we're ignoring parity and break indicators,
  823. * ignore overruns too (for real raw support).
  824. */
  825. if (termios->c_iflag & IGNPAR)
  826. up->port.ignore_status_mask |= UART_LSR_OE;
  827. }
  828. /* Ignore all characters if CREAD is not set */
  829. if ((termios->c_cflag & CREAD) == 0)
  830. up->port.ignore_status_mask |= UART_LSR_DR;
  831. /*
  832. * CTS flow control flag and modem status interrupts, disable
  833. * MSI by default
  834. */
  835. up->ier &= ~UART_IER_MSI;
  836. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  837. up->ier |= UART_IER_MSI;
  838. serial_out(up, UART_IER, up->ier);
  839. if (termios->c_cflag & CRTSCTS)
  840. up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
  841. else
  842. up->mcr &= ~UART_MCR_AFE;
  843. serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
  844. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  845. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  846. serial_out(up, UART_LCR, cval); /* reset DLAB */
  847. serial_out(up, UART_MUL, mul); /* set MUL */
  848. serial_out(up, UART_PS, ps); /* set PS */
  849. up->lcr = cval; /* Save LCR */
  850. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  851. serial_out(up, UART_FCR, fcr);
  852. spin_unlock_irqrestore(&up->port.lock, flags);
  853. }
  854. static void
  855. serial_hsu_pm(struct uart_port *port, unsigned int state,
  856. unsigned int oldstate)
  857. {
  858. }
  859. static void serial_hsu_release_port(struct uart_port *port)
  860. {
  861. }
  862. static int serial_hsu_request_port(struct uart_port *port)
  863. {
  864. return 0;
  865. }
  866. static void serial_hsu_config_port(struct uart_port *port, int flags)
  867. {
  868. struct uart_hsu_port *up =
  869. container_of(port, struct uart_hsu_port, port);
  870. up->port.type = PORT_MFD;
  871. }
  872. static int
  873. serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
  874. {
  875. /* We don't want the core code to modify any port params */
  876. return -EINVAL;
  877. }
  878. static const char *
  879. serial_hsu_type(struct uart_port *port)
  880. {
  881. struct uart_hsu_port *up =
  882. container_of(port, struct uart_hsu_port, port);
  883. return up->name;
  884. }
  885. /* Mainly for uart console use */
  886. static struct uart_hsu_port *serial_hsu_ports[3];
  887. static struct uart_driver serial_hsu_reg;
  888. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  889. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  890. /* Wait for transmitter & holding register to empty */
  891. static inline void wait_for_xmitr(struct uart_hsu_port *up)
  892. {
  893. unsigned int status, tmout = 1000;
  894. /* Wait up to 1ms for the character to be sent. */
  895. do {
  896. status = serial_in(up, UART_LSR);
  897. if (status & UART_LSR_BI)
  898. up->lsr_break_flag = UART_LSR_BI;
  899. if (--tmout == 0)
  900. break;
  901. udelay(1);
  902. } while (!(status & BOTH_EMPTY));
  903. /* Wait up to 1s for flow control if necessary */
  904. if (up->port.flags & UPF_CONS_FLOW) {
  905. tmout = 1000000;
  906. while (--tmout &&
  907. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  908. udelay(1);
  909. }
  910. }
  911. static void serial_hsu_console_putchar(struct uart_port *port, int ch)
  912. {
  913. struct uart_hsu_port *up =
  914. container_of(port, struct uart_hsu_port, port);
  915. wait_for_xmitr(up);
  916. serial_out(up, UART_TX, ch);
  917. }
  918. /*
  919. * Print a string to the serial port trying not to disturb
  920. * any possible real use of the port...
  921. *
  922. * The console_lock must be held when we get here.
  923. */
  924. static void
  925. serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
  926. {
  927. struct uart_hsu_port *up = serial_hsu_ports[co->index];
  928. unsigned long flags;
  929. unsigned int ier;
  930. int locked = 1;
  931. touch_nmi_watchdog();
  932. local_irq_save(flags);
  933. if (up->port.sysrq)
  934. locked = 0;
  935. else if (oops_in_progress) {
  936. locked = spin_trylock(&up->port.lock);
  937. } else
  938. spin_lock(&up->port.lock);
  939. /* First save the IER then disable the interrupts */
  940. ier = serial_in(up, UART_IER);
  941. serial_out(up, UART_IER, 0);
  942. uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
  943. /*
  944. * Finally, wait for transmitter to become empty
  945. * and restore the IER
  946. */
  947. wait_for_xmitr(up);
  948. serial_out(up, UART_IER, ier);
  949. if (locked)
  950. spin_unlock(&up->port.lock);
  951. local_irq_restore(flags);
  952. }
  953. static struct console serial_hsu_console;
  954. static int __init
  955. serial_hsu_console_setup(struct console *co, char *options)
  956. {
  957. struct uart_hsu_port *up;
  958. int baud = 115200;
  959. int bits = 8;
  960. int parity = 'n';
  961. int flow = 'n';
  962. if (co->index == -1 || co->index >= serial_hsu_reg.nr)
  963. co->index = 0;
  964. up = serial_hsu_ports[co->index];
  965. if (!up)
  966. return -ENODEV;
  967. if (options)
  968. uart_parse_options(options, &baud, &parity, &bits, &flow);
  969. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  970. }
  971. static struct console serial_hsu_console = {
  972. .name = "ttyMFD",
  973. .write = serial_hsu_console_write,
  974. .device = uart_console_device,
  975. .setup = serial_hsu_console_setup,
  976. .flags = CON_PRINTBUFFER,
  977. .index = -1,
  978. .data = &serial_hsu_reg,
  979. };
  980. #define SERIAL_HSU_CONSOLE (&serial_hsu_console)
  981. #else
  982. #define SERIAL_HSU_CONSOLE NULL
  983. #endif
  984. struct uart_ops serial_hsu_pops = {
  985. .tx_empty = serial_hsu_tx_empty,
  986. .set_mctrl = serial_hsu_set_mctrl,
  987. .get_mctrl = serial_hsu_get_mctrl,
  988. .stop_tx = serial_hsu_stop_tx,
  989. .start_tx = serial_hsu_start_tx,
  990. .stop_rx = serial_hsu_stop_rx,
  991. .enable_ms = serial_hsu_enable_ms,
  992. .break_ctl = serial_hsu_break_ctl,
  993. .startup = serial_hsu_startup,
  994. .shutdown = serial_hsu_shutdown,
  995. .set_termios = serial_hsu_set_termios,
  996. .pm = serial_hsu_pm,
  997. .type = serial_hsu_type,
  998. .release_port = serial_hsu_release_port,
  999. .request_port = serial_hsu_request_port,
  1000. .config_port = serial_hsu_config_port,
  1001. .verify_port = serial_hsu_verify_port,
  1002. };
  1003. static struct uart_driver serial_hsu_reg = {
  1004. .owner = THIS_MODULE,
  1005. .driver_name = "MFD serial",
  1006. .dev_name = "ttyMFD",
  1007. .major = TTY_MAJOR,
  1008. .minor = 128,
  1009. .nr = 3,
  1010. .cons = SERIAL_HSU_CONSOLE,
  1011. };
  1012. #ifdef CONFIG_PM
  1013. static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
  1014. {
  1015. void *priv = pci_get_drvdata(pdev);
  1016. struct uart_hsu_port *up;
  1017. /* Make sure this is not the internal dma controller */
  1018. if (priv && (pdev->device != 0x081E)) {
  1019. up = priv;
  1020. uart_suspend_port(&serial_hsu_reg, &up->port);
  1021. }
  1022. pci_save_state(pdev);
  1023. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1024. return 0;
  1025. }
  1026. static int serial_hsu_resume(struct pci_dev *pdev)
  1027. {
  1028. void *priv = pci_get_drvdata(pdev);
  1029. struct uart_hsu_port *up;
  1030. int ret;
  1031. pci_set_power_state(pdev, PCI_D0);
  1032. pci_restore_state(pdev);
  1033. ret = pci_enable_device(pdev);
  1034. if (ret)
  1035. dev_warn(&pdev->dev,
  1036. "HSU: can't re-enable device, try to continue\n");
  1037. if (priv && (pdev->device != 0x081E)) {
  1038. up = priv;
  1039. uart_resume_port(&serial_hsu_reg, &up->port);
  1040. }
  1041. return 0;
  1042. }
  1043. #else
  1044. #define serial_hsu_suspend NULL
  1045. #define serial_hsu_resume NULL
  1046. #endif
  1047. #ifdef CONFIG_PM_RUNTIME
  1048. static int serial_hsu_runtime_idle(struct device *dev)
  1049. {
  1050. pm_schedule_suspend(dev, 500);
  1051. return -EBUSY;
  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");