8250_mid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * 8250_mid.c - Driver for UART on Intel Penwell and various other Intel SOCs
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  5. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/rational.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/dma/hsu.h>
  15. #include <linux/8250_pci.h>
  16. #include "8250.h"
  17. #define PCI_DEVICE_ID_INTEL_PNW_UART1 0x081b
  18. #define PCI_DEVICE_ID_INTEL_PNW_UART2 0x081c
  19. #define PCI_DEVICE_ID_INTEL_PNW_UART3 0x081d
  20. #define PCI_DEVICE_ID_INTEL_TNG_UART 0x1191
  21. #define PCI_DEVICE_ID_INTEL_DNV_UART 0x19d8
  22. /* Intel MID Specific registers */
  23. #define INTEL_MID_UART_DNV_FISR 0x08
  24. #define INTEL_MID_UART_PS 0x30
  25. #define INTEL_MID_UART_MUL 0x34
  26. #define INTEL_MID_UART_DIV 0x38
  27. struct mid8250;
  28. struct mid8250_board {
  29. unsigned int flags;
  30. unsigned long freq;
  31. unsigned int base_baud;
  32. int (*setup)(struct mid8250 *, struct uart_port *p);
  33. void (*exit)(struct mid8250 *);
  34. };
  35. struct mid8250 {
  36. int line;
  37. int dma_index;
  38. struct pci_dev *dma_dev;
  39. struct uart_8250_dma dma;
  40. struct mid8250_board *board;
  41. struct hsu_dma_chip dma_chip;
  42. };
  43. /*****************************************************************************/
  44. static int pnw_setup(struct mid8250 *mid, struct uart_port *p)
  45. {
  46. struct pci_dev *pdev = to_pci_dev(p->dev);
  47. switch (pdev->device) {
  48. case PCI_DEVICE_ID_INTEL_PNW_UART1:
  49. mid->dma_index = 0;
  50. break;
  51. case PCI_DEVICE_ID_INTEL_PNW_UART2:
  52. mid->dma_index = 1;
  53. break;
  54. case PCI_DEVICE_ID_INTEL_PNW_UART3:
  55. mid->dma_index = 2;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. mid->dma_dev = pci_get_slot(pdev->bus,
  61. PCI_DEVFN(PCI_SLOT(pdev->devfn), 3));
  62. return 0;
  63. }
  64. static int tng_setup(struct mid8250 *mid, struct uart_port *p)
  65. {
  66. struct pci_dev *pdev = to_pci_dev(p->dev);
  67. int index = PCI_FUNC(pdev->devfn);
  68. /* Currently no support for HSU port0 */
  69. if (index-- == 0)
  70. return -ENODEV;
  71. mid->dma_index = index;
  72. mid->dma_dev = pci_get_slot(pdev->bus, PCI_DEVFN(5, 0));
  73. return 0;
  74. }
  75. static int dnv_handle_irq(struct uart_port *p)
  76. {
  77. struct mid8250 *mid = p->private_data;
  78. unsigned int fisr = serial_port_in(p, INTEL_MID_UART_DNV_FISR);
  79. int ret = IRQ_NONE;
  80. if (fisr & BIT(2))
  81. ret |= hsu_dma_irq(&mid->dma_chip, 1);
  82. if (fisr & BIT(1))
  83. ret |= hsu_dma_irq(&mid->dma_chip, 0);
  84. if (fisr & BIT(0))
  85. ret |= serial8250_handle_irq(p, serial_port_in(p, UART_IIR));
  86. return ret;
  87. }
  88. #define DNV_DMA_CHAN_OFFSET 0x80
  89. static int dnv_setup(struct mid8250 *mid, struct uart_port *p)
  90. {
  91. struct hsu_dma_chip *chip = &mid->dma_chip;
  92. struct pci_dev *pdev = to_pci_dev(p->dev);
  93. unsigned int bar = FL_GET_BASE(mid->board->flags);
  94. int ret;
  95. chip->dev = &pdev->dev;
  96. chip->irq = pdev->irq;
  97. chip->regs = p->membase;
  98. chip->length = pci_resource_len(pdev, bar);
  99. chip->offset = DNV_DMA_CHAN_OFFSET;
  100. /* Falling back to PIO mode if DMA probing fails */
  101. ret = hsu_dma_probe(chip);
  102. if (ret)
  103. return 0;
  104. mid->dma_dev = pdev;
  105. p->handle_irq = dnv_handle_irq;
  106. return 0;
  107. }
  108. static void dnv_exit(struct mid8250 *mid)
  109. {
  110. if (!mid->dma_dev)
  111. return;
  112. hsu_dma_remove(&mid->dma_chip);
  113. }
  114. /*****************************************************************************/
  115. static void mid8250_set_termios(struct uart_port *p,
  116. struct ktermios *termios,
  117. struct ktermios *old)
  118. {
  119. unsigned int baud = tty_termios_baud_rate(termios);
  120. struct mid8250 *mid = p->private_data;
  121. unsigned short ps = 16;
  122. unsigned long fuart = baud * ps;
  123. unsigned long w = BIT(24) - 1;
  124. unsigned long mul, div;
  125. /* Gracefully handle the B0 case: fall back to B9600 */
  126. fuart = fuart ? fuart : 9600 * 16;
  127. if (mid->board->freq < fuart) {
  128. /* Find prescaler value that satisfies Fuart < Fref */
  129. if (mid->board->freq > baud)
  130. ps = mid->board->freq / baud; /* baud rate too high */
  131. else
  132. ps = 1; /* PLL case */
  133. fuart = baud * ps;
  134. } else {
  135. /* Get Fuart closer to Fref */
  136. fuart *= rounddown_pow_of_two(mid->board->freq / fuart);
  137. }
  138. rational_best_approximation(fuart, mid->board->freq, w, w, &mul, &div);
  139. p->uartclk = fuart * 16 / ps; /* core uses ps = 16 always */
  140. writel(ps, p->membase + INTEL_MID_UART_PS); /* set PS */
  141. writel(mul, p->membase + INTEL_MID_UART_MUL); /* set MUL */
  142. writel(div, p->membase + INTEL_MID_UART_DIV);
  143. serial8250_do_set_termios(p, termios, old);
  144. }
  145. static bool mid8250_dma_filter(struct dma_chan *chan, void *param)
  146. {
  147. struct hsu_dma_slave *s = param;
  148. if (s->dma_dev != chan->device->dev || s->chan_id != chan->chan_id)
  149. return false;
  150. chan->private = s;
  151. return true;
  152. }
  153. static int mid8250_dma_setup(struct mid8250 *mid, struct uart_8250_port *port)
  154. {
  155. struct uart_8250_dma *dma = &mid->dma;
  156. struct device *dev = port->port.dev;
  157. struct hsu_dma_slave *rx_param;
  158. struct hsu_dma_slave *tx_param;
  159. if (!mid->dma_dev)
  160. return 0;
  161. rx_param = devm_kzalloc(dev, sizeof(*rx_param), GFP_KERNEL);
  162. if (!rx_param)
  163. return -ENOMEM;
  164. tx_param = devm_kzalloc(dev, sizeof(*tx_param), GFP_KERNEL);
  165. if (!tx_param)
  166. return -ENOMEM;
  167. rx_param->chan_id = mid->dma_index * 2 + 1;
  168. tx_param->chan_id = mid->dma_index * 2;
  169. dma->rxconf.src_maxburst = 64;
  170. dma->txconf.dst_maxburst = 64;
  171. rx_param->dma_dev = &mid->dma_dev->dev;
  172. tx_param->dma_dev = &mid->dma_dev->dev;
  173. dma->fn = mid8250_dma_filter;
  174. dma->rx_param = rx_param;
  175. dma->tx_param = tx_param;
  176. port->dma = dma;
  177. return 0;
  178. }
  179. static int mid8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  180. {
  181. struct uart_8250_port uart;
  182. struct mid8250 *mid;
  183. unsigned int bar;
  184. int ret;
  185. ret = pcim_enable_device(pdev);
  186. if (ret)
  187. return ret;
  188. pci_set_master(pdev);
  189. mid = devm_kzalloc(&pdev->dev, sizeof(*mid), GFP_KERNEL);
  190. if (!mid)
  191. return -ENOMEM;
  192. mid->board = (struct mid8250_board *)id->driver_data;
  193. bar = FL_GET_BASE(mid->board->flags);
  194. memset(&uart, 0, sizeof(struct uart_8250_port));
  195. uart.port.dev = &pdev->dev;
  196. uart.port.irq = pdev->irq;
  197. uart.port.private_data = mid;
  198. uart.port.type = PORT_16750;
  199. uart.port.iotype = UPIO_MEM;
  200. uart.port.uartclk = mid->board->base_baud * 16;
  201. uart.port.flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE;
  202. uart.port.set_termios = mid8250_set_termios;
  203. uart.port.mapbase = pci_resource_start(pdev, bar);
  204. uart.port.membase = pcim_iomap(pdev, bar, 0);
  205. if (!uart.port.membase)
  206. return -ENOMEM;
  207. if (mid->board->setup) {
  208. ret = mid->board->setup(mid, &uart.port);
  209. if (ret)
  210. return ret;
  211. }
  212. ret = mid8250_dma_setup(mid, &uart);
  213. if (ret)
  214. goto err;
  215. ret = serial8250_register_8250_port(&uart);
  216. if (ret < 0)
  217. goto err;
  218. mid->line = ret;
  219. pci_set_drvdata(pdev, mid);
  220. return 0;
  221. err:
  222. if (mid->board->exit)
  223. mid->board->exit(mid);
  224. return ret;
  225. }
  226. static void mid8250_remove(struct pci_dev *pdev)
  227. {
  228. struct mid8250 *mid = pci_get_drvdata(pdev);
  229. if (mid->board->exit)
  230. mid->board->exit(mid);
  231. serial8250_unregister_port(mid->line);
  232. }
  233. static const struct mid8250_board pnw_board = {
  234. .flags = FL_BASE0,
  235. .freq = 50000000,
  236. .base_baud = 115200,
  237. .setup = pnw_setup,
  238. };
  239. static const struct mid8250_board tng_board = {
  240. .flags = FL_BASE0,
  241. .freq = 38400000,
  242. .base_baud = 1843200,
  243. .setup = tng_setup,
  244. };
  245. static const struct mid8250_board dnv_board = {
  246. .flags = FL_BASE1,
  247. .freq = 133333333,
  248. .base_baud = 115200,
  249. .setup = dnv_setup,
  250. .exit = dnv_exit,
  251. };
  252. #define MID_DEVICE(id, board) { PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&board }
  253. static const struct pci_device_id pci_ids[] = {
  254. MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART1, pnw_board),
  255. MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART2, pnw_board),
  256. MID_DEVICE(PCI_DEVICE_ID_INTEL_PNW_UART3, pnw_board),
  257. MID_DEVICE(PCI_DEVICE_ID_INTEL_TNG_UART, tng_board),
  258. MID_DEVICE(PCI_DEVICE_ID_INTEL_DNV_UART, dnv_board),
  259. { },
  260. };
  261. MODULE_DEVICE_TABLE(pci, pci_ids);
  262. static struct pci_driver mid8250_pci_driver = {
  263. .name = "8250_mid",
  264. .id_table = pci_ids,
  265. .probe = mid8250_probe,
  266. .remove = mid8250_remove,
  267. };
  268. module_pci_driver(mid8250_pci_driver);
  269. MODULE_AUTHOR("Intel Corporation");
  270. MODULE_LICENSE("GPL v2");
  271. MODULE_DESCRIPTION("Intel MID UART driver");