mfd.c 36 KB

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