mfd.c 36 KB

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