ehv_bytechan.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* ePAPR hypervisor byte channel device driver
  2. *
  3. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  4. *
  5. * Author: Timur Tabi <timur@freescale.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. *
  11. * This driver support three distinct interfaces, all of which are related to
  12. * ePAPR hypervisor byte channels.
  13. *
  14. * 1) An early-console (udbg) driver. This provides early console output
  15. * through a byte channel. The byte channel handle must be specified in a
  16. * Kconfig option.
  17. *
  18. * 2) A normal console driver. Output is sent to the byte channel designated
  19. * for stdout in the device tree. The console driver is for handling kernel
  20. * printk calls.
  21. *
  22. * 3) A tty driver, which is used to handle user-space input and output. The
  23. * byte channel used for the console is designated as the default tty.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/err.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/fs.h>
  31. #include <linux/poll.h>
  32. #include <asm/epapr_hcalls.h>
  33. #include <linux/of.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/cdev.h>
  37. #include <linux/console.h>
  38. #include <linux/tty.h>
  39. #include <linux/tty_flip.h>
  40. #include <linux/circ_buf.h>
  41. #include <asm/udbg.h>
  42. /* The size of the transmit circular buffer. This must be a power of two. */
  43. #define BUF_SIZE 2048
  44. /* Per-byte channel private data */
  45. struct ehv_bc_data {
  46. struct device *dev;
  47. struct tty_port port;
  48. uint32_t handle;
  49. unsigned int rx_irq;
  50. unsigned int tx_irq;
  51. spinlock_t lock; /* lock for transmit buffer */
  52. unsigned char buf[BUF_SIZE]; /* transmit circular buffer */
  53. unsigned int head; /* circular buffer head */
  54. unsigned int tail; /* circular buffer tail */
  55. int tx_irq_enabled; /* true == TX interrupt is enabled */
  56. };
  57. /* Array of byte channel objects */
  58. static struct ehv_bc_data *bcs;
  59. /* Byte channel handle for stdout (and stdin), taken from device tree */
  60. static unsigned int stdout_bc;
  61. /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
  62. static unsigned int stdout_irq;
  63. /**************************** SUPPORT FUNCTIONS ****************************/
  64. /*
  65. * Enable the transmit interrupt
  66. *
  67. * Unlike a serial device, byte channels have no mechanism for disabling their
  68. * own receive or transmit interrupts. To emulate that feature, we toggle
  69. * the IRQ in the kernel.
  70. *
  71. * We cannot just blindly call enable_irq() or disable_irq(), because these
  72. * calls are reference counted. This means that we cannot call enable_irq()
  73. * if interrupts are already enabled. This can happen in two situations:
  74. *
  75. * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
  76. * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
  77. *
  78. * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
  79. */
  80. static void enable_tx_interrupt(struct ehv_bc_data *bc)
  81. {
  82. if (!bc->tx_irq_enabled) {
  83. enable_irq(bc->tx_irq);
  84. bc->tx_irq_enabled = 1;
  85. }
  86. }
  87. static void disable_tx_interrupt(struct ehv_bc_data *bc)
  88. {
  89. if (bc->tx_irq_enabled) {
  90. disable_irq_nosync(bc->tx_irq);
  91. bc->tx_irq_enabled = 0;
  92. }
  93. }
  94. /*
  95. * find the byte channel handle to use for the console
  96. *
  97. * The byte channel to be used for the console is specified via a "stdout"
  98. * property in the /chosen node.
  99. *
  100. * For compatible with legacy device trees, we also look for a "stdout" alias.
  101. */
  102. static int find_console_handle(void)
  103. {
  104. struct device_node *np, *np2;
  105. const char *sprop = NULL;
  106. const uint32_t *iprop;
  107. np = of_find_node_by_path("/chosen");
  108. if (np)
  109. sprop = of_get_property(np, "stdout-path", NULL);
  110. if (!np || !sprop) {
  111. of_node_put(np);
  112. np = of_find_node_by_name(NULL, "aliases");
  113. if (np)
  114. sprop = of_get_property(np, "stdout", NULL);
  115. }
  116. if (!sprop) {
  117. of_node_put(np);
  118. return 0;
  119. }
  120. /* We don't care what the aliased node is actually called. We only
  121. * care if it's compatible with "epapr,hv-byte-channel", because that
  122. * indicates that it's a byte channel node. We use a temporary
  123. * variable, 'np2', because we can't release 'np' until we're done with
  124. * 'sprop'.
  125. */
  126. np2 = of_find_node_by_path(sprop);
  127. of_node_put(np);
  128. np = np2;
  129. if (!np) {
  130. pr_warning("ehv-bc: stdout node '%s' does not exist\n", sprop);
  131. return 0;
  132. }
  133. /* Is it a byte channel? */
  134. if (!of_device_is_compatible(np, "epapr,hv-byte-channel")) {
  135. of_node_put(np);
  136. return 0;
  137. }
  138. stdout_irq = irq_of_parse_and_map(np, 0);
  139. if (stdout_irq == NO_IRQ) {
  140. pr_err("ehv-bc: no 'interrupts' property in %s node\n", sprop);
  141. of_node_put(np);
  142. return 0;
  143. }
  144. /*
  145. * The 'hv-handle' property contains the handle for this byte channel.
  146. */
  147. iprop = of_get_property(np, "hv-handle", NULL);
  148. if (!iprop) {
  149. pr_err("ehv-bc: no 'hv-handle' property in %s node\n",
  150. np->name);
  151. of_node_put(np);
  152. return 0;
  153. }
  154. stdout_bc = be32_to_cpu(*iprop);
  155. of_node_put(np);
  156. return 1;
  157. }
  158. /*************************** EARLY CONSOLE DRIVER ***************************/
  159. #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
  160. /*
  161. * send a byte to a byte channel, wait if necessary
  162. *
  163. * This function sends a byte to a byte channel, and it waits and
  164. * retries if the byte channel is full. It returns if the character
  165. * has been sent, or if some error has occurred.
  166. *
  167. */
  168. static void byte_channel_spin_send(const char data)
  169. {
  170. int ret, count;
  171. do {
  172. count = 1;
  173. ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
  174. &count, &data);
  175. } while (ret == EV_EAGAIN);
  176. }
  177. /*
  178. * The udbg subsystem calls this function to display a single character.
  179. * We convert CR to a CR/LF.
  180. */
  181. static void ehv_bc_udbg_putc(char c)
  182. {
  183. if (c == '\n')
  184. byte_channel_spin_send('\r');
  185. byte_channel_spin_send(c);
  186. }
  187. /*
  188. * early console initialization
  189. *
  190. * PowerPC kernels support an early printk console, also known as udbg.
  191. * This function must be called via the ppc_md.init_early function pointer.
  192. * At this point, the device tree has been unflattened, so we can obtain the
  193. * byte channel handle for stdout.
  194. *
  195. * We only support displaying of characters (putc). We do not support
  196. * keyboard input.
  197. */
  198. void __init udbg_init_ehv_bc(void)
  199. {
  200. unsigned int rx_count, tx_count;
  201. unsigned int ret;
  202. /* Verify the byte channel handle */
  203. ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
  204. &rx_count, &tx_count);
  205. if (ret)
  206. return;
  207. udbg_putc = ehv_bc_udbg_putc;
  208. register_early_udbg_console();
  209. udbg_printf("ehv-bc: early console using byte channel handle %u\n",
  210. CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
  211. }
  212. #endif
  213. /****************************** CONSOLE DRIVER ******************************/
  214. static struct tty_driver *ehv_bc_driver;
  215. /*
  216. * Byte channel console sending worker function.
  217. *
  218. * For consoles, if the output buffer is full, we should just spin until it
  219. * clears.
  220. */
  221. static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
  222. unsigned int count)
  223. {
  224. unsigned int len;
  225. int ret = 0;
  226. while (count) {
  227. len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
  228. do {
  229. ret = ev_byte_channel_send(handle, &len, s);
  230. } while (ret == EV_EAGAIN);
  231. count -= len;
  232. s += len;
  233. }
  234. return ret;
  235. }
  236. /*
  237. * write a string to the console
  238. *
  239. * This function gets called to write a string from the kernel, typically from
  240. * a printk(). This function spins until all data is written.
  241. *
  242. * We copy the data to a temporary buffer because we need to insert a \r in
  243. * front of every \n. It's more efficient to copy the data to the buffer than
  244. * it is to make multiple hcalls for each character or each newline.
  245. */
  246. static void ehv_bc_console_write(struct console *co, const char *s,
  247. unsigned int count)
  248. {
  249. char s2[EV_BYTE_CHANNEL_MAX_BYTES];
  250. unsigned int i, j = 0;
  251. char c;
  252. for (i = 0; i < count; i++) {
  253. c = *s++;
  254. if (c == '\n')
  255. s2[j++] = '\r';
  256. s2[j++] = c;
  257. if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
  258. if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
  259. return;
  260. j = 0;
  261. }
  262. }
  263. if (j)
  264. ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
  265. }
  266. /*
  267. * When /dev/console is opened, the kernel iterates the console list looking
  268. * for one with ->device and then calls that method. On success, it expects
  269. * the passed-in int* to contain the minor number to use.
  270. */
  271. static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
  272. {
  273. *index = co->index;
  274. return ehv_bc_driver;
  275. }
  276. static struct console ehv_bc_console = {
  277. .name = "ttyEHV",
  278. .write = ehv_bc_console_write,
  279. .device = ehv_bc_console_device,
  280. .flags = CON_PRINTBUFFER | CON_ENABLED,
  281. };
  282. /*
  283. * Console initialization
  284. *
  285. * This is the first function that is called after the device tree is
  286. * available, so here is where we determine the byte channel handle and IRQ for
  287. * stdout/stdin, even though that information is used by the tty and character
  288. * drivers.
  289. */
  290. static int __init ehv_bc_console_init(void)
  291. {
  292. if (!find_console_handle()) {
  293. pr_debug("ehv-bc: stdout is not a byte channel\n");
  294. return -ENODEV;
  295. }
  296. #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
  297. /* Print a friendly warning if the user chose the wrong byte channel
  298. * handle for udbg.
  299. */
  300. if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
  301. pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n",
  302. CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
  303. #endif
  304. /* add_preferred_console() must be called before register_console(),
  305. otherwise it won't work. However, we don't want to enumerate all the
  306. byte channels here, either, since we only care about one. */
  307. add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
  308. register_console(&ehv_bc_console);
  309. pr_info("ehv-bc: registered console driver for byte channel %u\n",
  310. stdout_bc);
  311. return 0;
  312. }
  313. console_initcall(ehv_bc_console_init);
  314. /******************************** TTY DRIVER ********************************/
  315. /*
  316. * byte channel receive interupt handler
  317. *
  318. * This ISR is called whenever data is available on a byte channel.
  319. */
  320. static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
  321. {
  322. struct ehv_bc_data *bc = data;
  323. unsigned int rx_count, tx_count, len;
  324. int count;
  325. char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
  326. int ret;
  327. /* Find out how much data needs to be read, and then ask the TTY layer
  328. * if it can handle that much. We want to ensure that every byte we
  329. * read from the byte channel will be accepted by the TTY layer.
  330. */
  331. ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
  332. count = tty_buffer_request_room(&bc->port, rx_count);
  333. /* 'count' is the maximum amount of data the TTY layer can accept at
  334. * this time. However, during testing, I was never able to get 'count'
  335. * to be less than 'rx_count'. I'm not sure whether I'm calling it
  336. * correctly.
  337. */
  338. while (count > 0) {
  339. len = min_t(unsigned int, count, sizeof(buffer));
  340. /* Read some data from the byte channel. This function will
  341. * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
  342. */
  343. ev_byte_channel_receive(bc->handle, &len, buffer);
  344. /* 'len' is now the amount of data that's been received. 'len'
  345. * can't be zero, and most likely it's equal to one.
  346. */
  347. /* Pass the received data to the tty layer. */
  348. ret = tty_insert_flip_string(&bc->port, buffer, len);
  349. /* 'ret' is the number of bytes that the TTY layer accepted.
  350. * If it's not equal to 'len', then it means the buffer is
  351. * full, which should never happen. If it does happen, we can
  352. * exit gracefully, but we drop the last 'len - ret' characters
  353. * that we read from the byte channel.
  354. */
  355. if (ret != len)
  356. break;
  357. count -= len;
  358. }
  359. /* Tell the tty layer that we're done. */
  360. tty_flip_buffer_push(&bc->port);
  361. return IRQ_HANDLED;
  362. }
  363. /*
  364. * dequeue the transmit buffer to the hypervisor
  365. *
  366. * This function, which can be called in interrupt context, dequeues as much
  367. * data as possible from the transmit buffer to the byte channel.
  368. */
  369. static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
  370. {
  371. unsigned int count;
  372. unsigned int len, ret;
  373. unsigned long flags;
  374. do {
  375. spin_lock_irqsave(&bc->lock, flags);
  376. len = min_t(unsigned int,
  377. CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
  378. EV_BYTE_CHANNEL_MAX_BYTES);
  379. ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
  380. /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
  381. if (!ret || (ret == EV_EAGAIN))
  382. bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
  383. count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
  384. spin_unlock_irqrestore(&bc->lock, flags);
  385. } while (count && !ret);
  386. spin_lock_irqsave(&bc->lock, flags);
  387. if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
  388. /*
  389. * If we haven't emptied the buffer, then enable the TX IRQ.
  390. * We'll get an interrupt when there's more room in the
  391. * hypervisor's output buffer.
  392. */
  393. enable_tx_interrupt(bc);
  394. else
  395. disable_tx_interrupt(bc);
  396. spin_unlock_irqrestore(&bc->lock, flags);
  397. }
  398. /*
  399. * byte channel transmit interupt handler
  400. *
  401. * This ISR is called whenever space becomes available for transmitting
  402. * characters on a byte channel.
  403. */
  404. static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
  405. {
  406. struct ehv_bc_data *bc = data;
  407. ehv_bc_tx_dequeue(bc);
  408. tty_port_tty_wakeup(&bc->port);
  409. return IRQ_HANDLED;
  410. }
  411. /*
  412. * This function is called when the tty layer has data for us send. We store
  413. * the data first in a circular buffer, and then dequeue as much of that data
  414. * as possible.
  415. *
  416. * We don't need to worry about whether there is enough room in the buffer for
  417. * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
  418. * layer how much data it can safely send to us. We guarantee that
  419. * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
  420. * too much data.
  421. */
  422. static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
  423. int count)
  424. {
  425. struct ehv_bc_data *bc = ttys->driver_data;
  426. unsigned long flags;
  427. unsigned int len;
  428. unsigned int written = 0;
  429. while (1) {
  430. spin_lock_irqsave(&bc->lock, flags);
  431. len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
  432. if (count < len)
  433. len = count;
  434. if (len) {
  435. memcpy(bc->buf + bc->head, s, len);
  436. bc->head = (bc->head + len) & (BUF_SIZE - 1);
  437. }
  438. spin_unlock_irqrestore(&bc->lock, flags);
  439. if (!len)
  440. break;
  441. s += len;
  442. count -= len;
  443. written += len;
  444. }
  445. ehv_bc_tx_dequeue(bc);
  446. return written;
  447. }
  448. /*
  449. * This function can be called multiple times for a given tty_struct, which is
  450. * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
  451. *
  452. * The tty layer will still call this function even if the device was not
  453. * registered (i.e. tty_register_device() was not called). This happens
  454. * because tty_register_device() is optional and some legacy drivers don't
  455. * use it. So we need to check for that.
  456. */
  457. static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
  458. {
  459. struct ehv_bc_data *bc = &bcs[ttys->index];
  460. if (!bc->dev)
  461. return -ENODEV;
  462. return tty_port_open(&bc->port, ttys, filp);
  463. }
  464. /*
  465. * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
  466. * still call this function to close the tty device. So we can't assume that
  467. * the tty port has been initialized.
  468. */
  469. static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
  470. {
  471. struct ehv_bc_data *bc = &bcs[ttys->index];
  472. if (bc->dev)
  473. tty_port_close(&bc->port, ttys, filp);
  474. }
  475. /*
  476. * Return the amount of space in the output buffer
  477. *
  478. * This is actually a contract between the driver and the tty layer outlining
  479. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  480. * driver MUST honor the return value.
  481. */
  482. static int ehv_bc_tty_write_room(struct tty_struct *ttys)
  483. {
  484. struct ehv_bc_data *bc = ttys->driver_data;
  485. unsigned long flags;
  486. int count;
  487. spin_lock_irqsave(&bc->lock, flags);
  488. count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
  489. spin_unlock_irqrestore(&bc->lock, flags);
  490. return count;
  491. }
  492. /*
  493. * Stop sending data to the tty layer
  494. *
  495. * This function is called when the tty layer's input buffers are getting full,
  496. * so the driver should stop sending it data. The easiest way to do this is to
  497. * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
  498. * called.
  499. *
  500. * The hypervisor will continue to queue up any incoming data. If there is any
  501. * data in the queue when the RX interrupt is enabled, we'll immediately get an
  502. * RX interrupt.
  503. */
  504. static void ehv_bc_tty_throttle(struct tty_struct *ttys)
  505. {
  506. struct ehv_bc_data *bc = ttys->driver_data;
  507. disable_irq(bc->rx_irq);
  508. }
  509. /*
  510. * Resume sending data to the tty layer
  511. *
  512. * This function is called after previously calling ehv_bc_tty_throttle(). The
  513. * tty layer's input buffers now have more room, so the driver can resume
  514. * sending it data.
  515. */
  516. static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
  517. {
  518. struct ehv_bc_data *bc = ttys->driver_data;
  519. /* If there is any data in the queue when the RX interrupt is enabled,
  520. * we'll immediately get an RX interrupt.
  521. */
  522. enable_irq(bc->rx_irq);
  523. }
  524. static void ehv_bc_tty_hangup(struct tty_struct *ttys)
  525. {
  526. struct ehv_bc_data *bc = ttys->driver_data;
  527. ehv_bc_tx_dequeue(bc);
  528. tty_port_hangup(&bc->port);
  529. }
  530. /*
  531. * TTY driver operations
  532. *
  533. * If we could ask the hypervisor how much data is still in the TX buffer, or
  534. * at least how big the TX buffers are, then we could implement the
  535. * .wait_until_sent and .chars_in_buffer functions.
  536. */
  537. static const struct tty_operations ehv_bc_ops = {
  538. .open = ehv_bc_tty_open,
  539. .close = ehv_bc_tty_close,
  540. .write = ehv_bc_tty_write,
  541. .write_room = ehv_bc_tty_write_room,
  542. .throttle = ehv_bc_tty_throttle,
  543. .unthrottle = ehv_bc_tty_unthrottle,
  544. .hangup = ehv_bc_tty_hangup,
  545. };
  546. /*
  547. * initialize the TTY port
  548. *
  549. * This function will only be called once, no matter how many times
  550. * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
  551. * why we initialize tty_struct-related variables here.
  552. */
  553. static int ehv_bc_tty_port_activate(struct tty_port *port,
  554. struct tty_struct *ttys)
  555. {
  556. struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
  557. int ret;
  558. ttys->driver_data = bc;
  559. ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
  560. if (ret < 0) {
  561. dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
  562. bc->rx_irq, ret);
  563. return ret;
  564. }
  565. /* request_irq also enables the IRQ */
  566. bc->tx_irq_enabled = 1;
  567. ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
  568. if (ret < 0) {
  569. dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
  570. bc->tx_irq, ret);
  571. free_irq(bc->rx_irq, bc);
  572. return ret;
  573. }
  574. /* The TX IRQ is enabled only when we can't write all the data to the
  575. * byte channel at once, so by default it's disabled.
  576. */
  577. disable_tx_interrupt(bc);
  578. return 0;
  579. }
  580. static void ehv_bc_tty_port_shutdown(struct tty_port *port)
  581. {
  582. struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
  583. free_irq(bc->tx_irq, bc);
  584. free_irq(bc->rx_irq, bc);
  585. }
  586. static const struct tty_port_operations ehv_bc_tty_port_ops = {
  587. .activate = ehv_bc_tty_port_activate,
  588. .shutdown = ehv_bc_tty_port_shutdown,
  589. };
  590. static int ehv_bc_tty_probe(struct platform_device *pdev)
  591. {
  592. struct device_node *np = pdev->dev.of_node;
  593. struct ehv_bc_data *bc;
  594. const uint32_t *iprop;
  595. unsigned int handle;
  596. int ret;
  597. static unsigned int index = 1;
  598. unsigned int i;
  599. iprop = of_get_property(np, "hv-handle", NULL);
  600. if (!iprop) {
  601. dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n",
  602. np->name);
  603. return -ENODEV;
  604. }
  605. /* We already told the console layer that the index for the console
  606. * device is zero, so we need to make sure that we use that index when
  607. * we probe the console byte channel node.
  608. */
  609. handle = be32_to_cpu(*iprop);
  610. i = (handle == stdout_bc) ? 0 : index++;
  611. bc = &bcs[i];
  612. bc->handle = handle;
  613. bc->head = 0;
  614. bc->tail = 0;
  615. spin_lock_init(&bc->lock);
  616. bc->rx_irq = irq_of_parse_and_map(np, 0);
  617. bc->tx_irq = irq_of_parse_and_map(np, 1);
  618. if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
  619. dev_err(&pdev->dev, "no 'interrupts' property in %s node\n",
  620. np->name);
  621. ret = -ENODEV;
  622. goto error;
  623. }
  624. tty_port_init(&bc->port);
  625. bc->port.ops = &ehv_bc_tty_port_ops;
  626. bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
  627. &pdev->dev);
  628. if (IS_ERR(bc->dev)) {
  629. ret = PTR_ERR(bc->dev);
  630. dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
  631. goto error;
  632. }
  633. dev_set_drvdata(&pdev->dev, bc);
  634. dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
  635. ehv_bc_driver->name, i, bc->handle);
  636. return 0;
  637. error:
  638. tty_port_destroy(&bc->port);
  639. irq_dispose_mapping(bc->tx_irq);
  640. irq_dispose_mapping(bc->rx_irq);
  641. memset(bc, 0, sizeof(struct ehv_bc_data));
  642. return ret;
  643. }
  644. static int ehv_bc_tty_remove(struct platform_device *pdev)
  645. {
  646. struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev);
  647. tty_unregister_device(ehv_bc_driver, bc - bcs);
  648. tty_port_destroy(&bc->port);
  649. irq_dispose_mapping(bc->tx_irq);
  650. irq_dispose_mapping(bc->rx_irq);
  651. return 0;
  652. }
  653. static const struct of_device_id ehv_bc_tty_of_ids[] = {
  654. { .compatible = "epapr,hv-byte-channel" },
  655. {}
  656. };
  657. static struct platform_driver ehv_bc_tty_driver = {
  658. .driver = {
  659. .owner = THIS_MODULE,
  660. .name = "ehv-bc",
  661. .of_match_table = ehv_bc_tty_of_ids,
  662. },
  663. .probe = ehv_bc_tty_probe,
  664. .remove = ehv_bc_tty_remove,
  665. };
  666. /**
  667. * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
  668. *
  669. * This function is called when this module is loaded.
  670. */
  671. static int __init ehv_bc_init(void)
  672. {
  673. struct device_node *np;
  674. unsigned int count = 0; /* Number of elements in bcs[] */
  675. int ret;
  676. pr_info("ePAPR hypervisor byte channel driver\n");
  677. /* Count the number of byte channels */
  678. for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
  679. count++;
  680. if (!count)
  681. return -ENODEV;
  682. /* The array index of an element in bcs[] is the same as the tty index
  683. * for that element. If you know the address of an element in the
  684. * array, then you can use pointer math (e.g. "bc - bcs") to get its
  685. * tty index.
  686. */
  687. bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL);
  688. if (!bcs)
  689. return -ENOMEM;
  690. ehv_bc_driver = alloc_tty_driver(count);
  691. if (!ehv_bc_driver) {
  692. ret = -ENOMEM;
  693. goto error;
  694. }
  695. ehv_bc_driver->driver_name = "ehv-bc";
  696. ehv_bc_driver->name = ehv_bc_console.name;
  697. ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  698. ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
  699. ehv_bc_driver->init_termios = tty_std_termios;
  700. ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  701. tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
  702. ret = tty_register_driver(ehv_bc_driver);
  703. if (ret) {
  704. pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
  705. goto error;
  706. }
  707. ret = platform_driver_register(&ehv_bc_tty_driver);
  708. if (ret) {
  709. pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
  710. ret);
  711. goto error;
  712. }
  713. return 0;
  714. error:
  715. if (ehv_bc_driver) {
  716. tty_unregister_driver(ehv_bc_driver);
  717. put_tty_driver(ehv_bc_driver);
  718. }
  719. kfree(bcs);
  720. return ret;
  721. }
  722. /**
  723. * ehv_bc_exit - ePAPR hypervisor byte channel driver termination
  724. *
  725. * This function is called when this driver is unloaded.
  726. */
  727. static void __exit ehv_bc_exit(void)
  728. {
  729. platform_driver_unregister(&ehv_bc_tty_driver);
  730. tty_unregister_driver(ehv_bc_driver);
  731. put_tty_driver(ehv_bc_driver);
  732. kfree(bcs);
  733. }
  734. module_init(ehv_bc_init);
  735. module_exit(ehv_bc_exit);
  736. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  737. MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver");
  738. MODULE_LICENSE("GPL v2");