mfd.c 36 KB

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