blob: 6328abd51ffad46cd5112acc47dba8708aa0c085 [file] [log] [blame]
Thomas Gleixner873e65b2019-05-27 08:55:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Geoff Levand74e95d5d2006-12-08 18:27:47 -08002/*
3 * PS3 virtual uart
4 *
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006 Sony Corp.
Geoff Levand74e95d5d2006-12-08 18:27:47 -08007 */
8
9#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080011#include <linux/module.h>
12#include <linux/interrupt.h>
Geoff Levandea1547d2007-02-06 14:23:47 -080013#include <linux/workqueue.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070014#include <linux/bitops.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080015#include <asm/ps3.h>
16
Geoff Levand75c86e72007-02-13 17:37:28 -080017#include <asm/firmware.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080018#include <asm/lv1call.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080019
20#include "vuart.h"
21
22MODULE_AUTHOR("Sony Corporation");
23MODULE_LICENSE("GPL v2");
Geoff Levand75c86e72007-02-13 17:37:28 -080024MODULE_DESCRIPTION("PS3 vuart");
Geoff Levand74e95d5d2006-12-08 18:27:47 -080025
26/**
27 * vuart - An inter-partition data link service.
28 * port 0: PS3 AV Settings.
29 * port 2: PS3 System Manager.
30 *
31 * The vuart provides a bi-directional byte stream data link between logical
32 * partitions. Its primary role is as a communications link between the guest
33 * OS and the system policy module. The current HV does not support any
34 * connections other than those listed.
35 */
36
37enum {PORT_COUNT = 3,};
38
39enum vuart_param {
40 PARAM_TX_TRIGGER = 0,
41 PARAM_RX_TRIGGER = 1,
42 PARAM_INTERRUPT_MASK = 2,
43 PARAM_RX_BUF_SIZE = 3, /* read only */
44 PARAM_RX_BYTES = 4, /* read only */
45 PARAM_TX_BUF_SIZE = 5, /* read only */
46 PARAM_TX_BYTES = 6, /* read only */
47 PARAM_INTERRUPT_STATUS = 7, /* read only */
48};
49
50enum vuart_interrupt_bit {
51 INTERRUPT_BIT_TX = 0,
52 INTERRUPT_BIT_RX = 1,
53 INTERRUPT_BIT_DISCONNECT = 2,
54};
55
56enum vuart_interrupt_mask {
57 INTERRUPT_MASK_TX = 1,
58 INTERRUPT_MASK_RX = 2,
59 INTERRUPT_MASK_DISCONNECT = 4,
60};
61
62/**
Geoff Levand7626e782007-06-16 08:01:06 +100063 * struct ps3_vuart_port_priv - private vuart device data.
64 */
65
66struct ps3_vuart_port_priv {
67 u64 interrupt_mask;
68
69 struct {
70 spinlock_t lock;
71 struct list_head head;
72 } tx_list;
73 struct {
74 struct ps3_vuart_work work;
75 unsigned long bytes_held;
76 spinlock_t lock;
77 struct list_head head;
78 } rx_list;
79 struct ps3_vuart_stats stats;
80};
81
82static struct ps3_vuart_port_priv *to_port_priv(
83 struct ps3_system_bus_device *dev)
84{
85 BUG_ON(!dev);
86 BUG_ON(!dev->driver_priv);
87 return (struct ps3_vuart_port_priv *)dev->driver_priv;
88}
89
90/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -080091 * struct ports_bmp - bitmap indicating ports needing service.
92 *
93 * A 256 bit read only bitmap indicating ports needing service. Do not write
94 * to these bits. Must not cross a page boundary.
95 */
96
97struct ports_bmp {
98 u64 status;
99 u64 unused[3];
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100100} __attribute__((aligned(32)));
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800101
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800102#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000103static void __maybe_unused _dump_ports_bmp(
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100104 const struct ports_bmp *bmp, const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800105{
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000106 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800107}
108
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800109#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000110static void __maybe_unused _dump_port_params(unsigned int port_number,
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100111 const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800112{
113#if defined(DEBUG)
114 static const char *strings[] = {
115 "tx_trigger ",
116 "rx_trigger ",
117 "interrupt_mask ",
118 "rx_buf_size ",
119 "rx_bytes ",
120 "tx_buf_size ",
121 "tx_bytes ",
122 "interrupt_status",
123 };
124 int result;
125 unsigned int i;
126 u64 value;
127
128 for (i = 0; i < ARRAY_SIZE(strings); i++) {
129 result = lv1_get_virtual_uart_param(port_number, i, &value);
130
131 if (result) {
132 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
133 port_number, strings[i], ps3_result(result));
134 continue;
135 }
136 pr_debug("%s:%d: port_%u: %s = %lxh\n",
137 func, line, port_number, strings[i], value);
138 }
139#endif
140}
141
Geoff Levand7626e782007-06-16 08:01:06 +1000142int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800143 struct vuart_triggers *trig)
144{
145 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000146 u64 size;
147 u64 val;
148 u64 tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800149
Geoff Levand7626e782007-06-16 08:01:06 +1000150 result = lv1_get_virtual_uart_param(dev->port_number,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000151 PARAM_TX_TRIGGER, &tx);
152 trig->tx = tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800153
154 if (result) {
155 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
156 __func__, __LINE__, ps3_result(result));
157 return result;
158 }
159
Geoff Levand7626e782007-06-16 08:01:06 +1000160 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800161 PARAM_RX_BUF_SIZE, &size);
162
163 if (result) {
164 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
165 __func__, __LINE__, ps3_result(result));
166 return result;
167 }
168
Geoff Levand7626e782007-06-16 08:01:06 +1000169 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800170 PARAM_RX_TRIGGER, &val);
171
172 if (result) {
173 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
174 __func__, __LINE__, ps3_result(result));
175 return result;
176 }
177
178 trig->rx = size - val;
179
180 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
181 trig->tx, trig->rx);
182
183 return result;
184}
185
Geoff Levand7626e782007-06-16 08:01:06 +1000186int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800187 unsigned int rx)
188{
189 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000190 u64 size;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800191
Geoff Levand7626e782007-06-16 08:01:06 +1000192 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800193 PARAM_TX_TRIGGER, tx);
194
195 if (result) {
196 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
197 __func__, __LINE__, ps3_result(result));
198 return result;
199 }
200
Geoff Levand7626e782007-06-16 08:01:06 +1000201 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800202 PARAM_RX_BUF_SIZE, &size);
203
204 if (result) {
205 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
206 __func__, __LINE__, ps3_result(result));
207 return result;
208 }
209
Geoff Levand7626e782007-06-16 08:01:06 +1000210 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800211 PARAM_RX_TRIGGER, size - rx);
212
213 if (result) {
214 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
215 __func__, __LINE__, ps3_result(result));
216 return result;
217 }
218
219 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
220 tx, rx);
221
222 return result;
223}
224
Geoff Levand7626e782007-06-16 08:01:06 +1000225static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800226 u64 *bytes_waiting)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800227{
Geoff Levand7626e782007-06-16 08:01:06 +1000228 int result;
229
230 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800231 PARAM_RX_BYTES, bytes_waiting);
232
233 if (result)
234 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
235 __func__, __LINE__, ps3_result(result));
236
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000237 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800238 *bytes_waiting);
239 return result;
240}
241
Geoff Levand7626e782007-06-16 08:01:06 +1000242/**
243 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
244 * @dev: The struct ps3_system_bus_device instance.
245 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
246 */
247
248static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800249 unsigned long mask)
250{
251 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000252 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800253
254 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
255
Geoff Levand7626e782007-06-16 08:01:06 +1000256 priv->interrupt_mask = mask;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800257
Geoff Levand7626e782007-06-16 08:01:06 +1000258 result = lv1_set_virtual_uart_param(dev->port_number,
259 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800260
261 if (result)
262 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
263 __func__, __LINE__, ps3_result(result));
264
265 return result;
266}
267
Geoff Levand7626e782007-06-16 08:01:06 +1000268static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800269 unsigned long *status)
270{
Geoff Levand7626e782007-06-16 08:01:06 +1000271 int result;
272 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800273 u64 tmp;
Geoff Levand7626e782007-06-16 08:01:06 +1000274
275 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand75c86e72007-02-13 17:37:28 -0800276 PARAM_INTERRUPT_STATUS, &tmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800277
278 if (result)
279 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
280 __func__, __LINE__, ps3_result(result));
281
Geoff Levand7626e782007-06-16 08:01:06 +1000282 *status = tmp & priv->interrupt_mask;
Geoff Levand75c86e72007-02-13 17:37:28 -0800283
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000284 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000285 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800286
287 return result;
288}
289
Geoff Levand7626e782007-06-16 08:01:06 +1000290int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800291{
Geoff Levand7626e782007-06-16 08:01:06 +1000292 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
293
294 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
295 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800296 | INTERRUPT_MASK_TX);
297}
298
Geoff Levand7626e782007-06-16 08:01:06 +1000299int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800300{
Geoff Levand7626e782007-06-16 08:01:06 +1000301 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
302
303 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
304 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800305 | INTERRUPT_MASK_RX);
306}
307
Geoff Levand7626e782007-06-16 08:01:06 +1000308int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800309{
Geoff Levand7626e782007-06-16 08:01:06 +1000310 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
311
312 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
313 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800314 | INTERRUPT_MASK_DISCONNECT);
315}
316
Geoff Levand7626e782007-06-16 08:01:06 +1000317int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800318{
Geoff Levand7626e782007-06-16 08:01:06 +1000319 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
320
321 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
322 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800323 & ~INTERRUPT_MASK_TX) : 0;
324}
325
Geoff Levand7626e782007-06-16 08:01:06 +1000326int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800327{
Geoff Levand7626e782007-06-16 08:01:06 +1000328 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
329
330 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
331 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800332 & ~INTERRUPT_MASK_RX) : 0;
333}
334
Geoff Levand7626e782007-06-16 08:01:06 +1000335int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800336{
Geoff Levand7626e782007-06-16 08:01:06 +1000337 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
338
339 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
340 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800341 & ~INTERRUPT_MASK_DISCONNECT) : 0;
342}
343
344/**
345 * ps3_vuart_raw_write - Low level write helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000346 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800347 *
348 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
349 */
350
Geoff Levand7626e782007-06-16 08:01:06 +1000351static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000352 const void *buf, unsigned int bytes, u64 *bytes_written)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800353{
354 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000355 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800356
Geoff Levand7626e782007-06-16 08:01:06 +1000357 result = lv1_write_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800358 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
359
360 if (result) {
Geoff Levand472b440f2021-06-03 19:17:01 +0000361 dev_warn(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800362 "%s\n", __func__, __LINE__, ps3_result(result));
363 return result;
364 }
365
Geoff Levand7626e782007-06-16 08:01:06 +1000366 priv->stats.bytes_written += *bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800367
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000368 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000369 *bytes_written, bytes, priv->stats.bytes_written);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800370
371 return result;
372}
373
374/**
375 * ps3_vuart_raw_read - Low level read helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000376 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800377 *
378 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
379 */
380
Geoff Levand7626e782007-06-16 08:01:06 +1000381static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000382 unsigned int bytes, u64 *bytes_read)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800383{
384 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000385 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800386
387 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
388
Geoff Levand7626e782007-06-16 08:01:06 +1000389 result = lv1_read_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800390 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
391
392 if (result) {
393 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
394 __func__, __LINE__, ps3_result(result));
395 return result;
396 }
397
Geoff Levand7626e782007-06-16 08:01:06 +1000398 priv->stats.bytes_read += *bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800399
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000400 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000401 *bytes_read, bytes, priv->stats.bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800402
403 return result;
404}
405
406/**
Geoff Levand75c86e72007-02-13 17:37:28 -0800407 * ps3_vuart_clear_rx_bytes - Discard bytes received.
Geoff Levand7626e782007-06-16 08:01:06 +1000408 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand75c86e72007-02-13 17:37:28 -0800409 * @bytes: Max byte count to discard, zero = all pending.
410 *
411 * Used to clear pending rx interrupt source. Will not block.
412 */
413
Geoff Levand7626e782007-06-16 08:01:06 +1000414void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800415 unsigned int bytes)
416{
417 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000418 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800419 u64 bytes_waiting;
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100420 void *tmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800421
422 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
423
424 BUG_ON(result);
425
426 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
427
428 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
429
430 if (!bytes)
431 return;
432
433 /* Add some extra space for recently arrived data. */
434
435 bytes += 128;
436
437 tmp = kmalloc(bytes, GFP_KERNEL);
438
439 if (!tmp)
440 return;
441
442 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
443
444 kfree(tmp);
445
446 /* Don't include these bytes in the stats. */
447
Geoff Levand7626e782007-06-16 08:01:06 +1000448 priv->stats.bytes_read -= bytes_waiting;
Geoff Levand75c86e72007-02-13 17:37:28 -0800449}
Geoff Levand7626e782007-06-16 08:01:06 +1000450EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
Geoff Levand75c86e72007-02-13 17:37:28 -0800451
452/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800453 * struct list_buffer - An element for a port device fifo buffer list.
454 */
455
456struct list_buffer {
457 struct list_head link;
458 const unsigned char *head;
459 const unsigned char *tail;
460 unsigned long dbg_number;
461 unsigned char data[];
462};
463
464/**
465 * ps3_vuart_write - the entry point for writing data to a port
Geoff Levand7626e782007-06-16 08:01:06 +1000466 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800467 *
468 * If the port is idle on entry as much of the incoming data is written to
469 * the port as the port will accept. Otherwise a list buffer is created
470 * and any remaning incoming data is copied to that buffer. The buffer is
471 * then enqueued for transmision via the transmit interrupt.
472 */
473
Geoff Levand7626e782007-06-16 08:01:06 +1000474int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800475 unsigned int bytes)
476{
477 static unsigned long dbg_number;
478 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000479 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800480 unsigned long flags;
481 struct list_buffer *lb;
482
483 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
484 bytes, bytes);
485
Geoff Levand7626e782007-06-16 08:01:06 +1000486 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800487
Geoff Levand7626e782007-06-16 08:01:06 +1000488 if (list_empty(&priv->tx_list.head)) {
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000489 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800490
491 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
492
Geoff Levand7626e782007-06-16 08:01:06 +1000493 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800494
495 if (result) {
496 dev_dbg(&dev->core,
497 "%s:%d: ps3_vuart_raw_write failed\n",
498 __func__, __LINE__);
499 return result;
500 }
501
502 if (bytes_written == bytes) {
503 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
504 __func__, __LINE__, bytes);
505 return 0;
506 }
507
508 bytes -= bytes_written;
509 buf += bytes_written;
510 } else
Geoff Levand7626e782007-06-16 08:01:06 +1000511 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800512
513 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
514
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100515 if (!lb)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800516 return -ENOMEM;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800517
518 memcpy(lb->data, buf, bytes);
519 lb->head = lb->data;
520 lb->tail = lb->data + bytes;
521 lb->dbg_number = ++dbg_number;
522
Geoff Levand7626e782007-06-16 08:01:06 +1000523 spin_lock_irqsave(&priv->tx_list.lock, flags);
524 list_add_tail(&lb->link, &priv->tx_list.head);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800525 ps3_vuart_enable_interrupt_tx(dev);
Geoff Levand7626e782007-06-16 08:01:06 +1000526 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800527
528 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
529 __func__, __LINE__, lb->dbg_number, bytes);
530
531 return 0;
532}
Geoff Levand7626e782007-06-16 08:01:06 +1000533EXPORT_SYMBOL_GPL(ps3_vuart_write);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800534
535/**
Geoff Levand7626e782007-06-16 08:01:06 +1000536 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
537 * @dev: The struct ps3_system_bus_device instance.
538 * @bytes_queued: Number of bytes queued to the buffer list.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800539 *
Geoff Levand7626e782007-06-16 08:01:06 +1000540 * Must be called with priv->rx_list.lock held.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800541 */
542
Geoff Levand7626e782007-06-16 08:01:06 +1000543static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
544 u64 *bytes_queued)
545{
546 static unsigned long dbg_number;
547 int result;
548 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
549 struct list_buffer *lb;
550 u64 bytes;
551
552 *bytes_queued = 0;
553
554 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
555 BUG_ON(result);
556
557 if (result)
558 return -EIO;
559
560 if (!bytes)
561 return 0;
562
563 /* Add some extra space for recently arrived data. */
564
565 bytes += 128;
566
567 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
568
569 if (!lb)
570 return -ENOMEM;
571
572 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
573
574 lb->head = lb->data;
575 lb->tail = lb->data + bytes;
576 lb->dbg_number = ++dbg_number;
577
578 list_add_tail(&lb->link, &priv->rx_list.head);
579 priv->rx_list.bytes_held += bytes;
580
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000581 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000582 __func__, __LINE__, lb->dbg_number, bytes);
583
584 *bytes_queued = bytes;
585
586 return 0;
587}
588
589/**
590 * ps3_vuart_read - The entry point for reading data from a port.
591 *
592 * Queue data waiting at the port, and if enough bytes to satisfy the request
593 * are held in the buffer list those bytes are dequeued and copied to the
594 * caller's buffer. Emptied list buffers are retiered. If the request cannot
595 * be statified by bytes held in the list buffers -EAGAIN is returned.
596 */
597
598int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800599 unsigned int bytes)
600{
Geoff Levand7626e782007-06-16 08:01:06 +1000601 int result;
602 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800603 unsigned long flags;
604 struct list_buffer *lb, *n;
605 unsigned long bytes_read;
606
607 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
608 bytes, bytes);
609
Geoff Levand7626e782007-06-16 08:01:06 +1000610 spin_lock_irqsave(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800611
Geoff Levand7626e782007-06-16 08:01:06 +1000612 /* Queue rx bytes here for polled reads. */
613
614 while (priv->rx_list.bytes_held < bytes) {
615 u64 tmp;
616
617 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
618 if (result || !tmp) {
619 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
620 __func__, __LINE__,
621 bytes - priv->rx_list.bytes_held);
622 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
623 return -EAGAIN;
624 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800625 }
626
Geoff Levand7626e782007-06-16 08:01:06 +1000627 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800628 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
629
630 memcpy(buf, lb->head, bytes_read);
631 buf += bytes_read;
632 bytes -= bytes_read;
Geoff Levand7626e782007-06-16 08:01:06 +1000633 priv->rx_list.bytes_held -= bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800634
635 if (bytes_read < lb->tail - lb->head) {
636 lb->head += bytes_read;
Geoff Levand75c86e72007-02-13 17:37:28 -0800637 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
638 "bytes\n", __func__, __LINE__, lb->dbg_number,
639 bytes_read);
Geoff Levand7626e782007-06-16 08:01:06 +1000640 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800641 return 0;
642 }
643
Geoff Levand75c86e72007-02-13 17:37:28 -0800644 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
645 "bytes\n", __func__, __LINE__, lb->dbg_number,
646 bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800647
648 list_del(&lb->link);
649 kfree(lb);
650 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800651
Geoff Levand7626e782007-06-16 08:01:06 +1000652 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800653 return 0;
654}
Geoff Levand7626e782007-06-16 08:01:06 +1000655EXPORT_SYMBOL_GPL(ps3_vuart_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800656
Geoff Levand7626e782007-06-16 08:01:06 +1000657/**
658 * ps3_vuart_work - Asynchronous read handler.
659 */
660
661static void ps3_vuart_work(struct work_struct *work)
Geoff Levandea1547d2007-02-06 14:23:47 -0800662{
Geoff Levand7626e782007-06-16 08:01:06 +1000663 struct ps3_system_bus_device *dev =
664 ps3_vuart_work_to_system_bus_dev(work);
665 struct ps3_vuart_port_driver *drv =
666 ps3_system_bus_dev_to_vuart_drv(dev);
667
668 BUG_ON(!drv);
669 drv->work(dev);
670}
671
672int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
673{
674 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levandea1547d2007-02-06 14:23:47 -0800675 unsigned long flags;
676
Geoff Levand7626e782007-06-16 08:01:06 +1000677 if (priv->rx_list.work.trigger) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800678 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
679 __func__, __LINE__);
680 return -EAGAIN;
681 }
682
683 BUG_ON(!bytes);
684
Geoff Levand7626e782007-06-16 08:01:06 +1000685 spin_lock_irqsave(&priv->rx_list.lock, flags);
686 if (priv->rx_list.bytes_held >= bytes) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800687 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
688 __func__, __LINE__, bytes);
Geoff Levand7626e782007-06-16 08:01:06 +1000689 schedule_work(&priv->rx_list.work.work);
690 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800691 return 0;
692 }
693
Geoff Levand7626e782007-06-16 08:01:06 +1000694 priv->rx_list.work.trigger = bytes;
695 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800696
697 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
698 __LINE__, bytes, bytes);
699
700 return 0;
701}
Geoff Levand7626e782007-06-16 08:01:06 +1000702EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800703
Geoff Levand7626e782007-06-16 08:01:06 +1000704void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
Geoff Levandea1547d2007-02-06 14:23:47 -0800705{
Geoff Levand7626e782007-06-16 08:01:06 +1000706 to_port_priv(dev)->rx_list.work.trigger = 0;
Geoff Levandea1547d2007-02-06 14:23:47 -0800707}
Geoff Levand7626e782007-06-16 08:01:06 +1000708EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800709
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800710/**
711 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
712 *
713 * Services the transmit interrupt for the port. Writes as much data from the
714 * buffer list as the port will accept. Retires any emptied list buffers and
715 * adjusts the final list buffer state for a partial write.
716 */
717
Geoff Levand7626e782007-06-16 08:01:06 +1000718static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800719{
720 int result = 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000721 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800722 unsigned long flags;
723 struct list_buffer *lb, *n;
724 unsigned long bytes_total = 0;
725
726 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
727
Geoff Levand7626e782007-06-16 08:01:06 +1000728 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800729
Geoff Levand7626e782007-06-16 08:01:06 +1000730 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800731
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000732 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800733
734 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
735 &bytes_written);
736
737 if (result) {
738 dev_dbg(&dev->core,
739 "%s:%d: ps3_vuart_raw_write failed\n",
740 __func__, __LINE__);
741 break;
742 }
743
744 bytes_total += bytes_written;
745
746 if (bytes_written < lb->tail - lb->head) {
747 lb->head += bytes_written;
748 dev_dbg(&dev->core,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000749 "%s:%d cleared buf_%lu, %llxh bytes\n",
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800750 __func__, __LINE__, lb->dbg_number,
751 bytes_written);
752 goto port_full;
753 }
754
755 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
756 lb->dbg_number);
757
758 list_del(&lb->link);
759 kfree(lb);
760 }
761
762 ps3_vuart_disable_interrupt_tx(dev);
763port_full:
Geoff Levand7626e782007-06-16 08:01:06 +1000764 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800765 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
766 __func__, __LINE__, bytes_total);
767 return result;
768}
769
770/**
771 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
772 *
773 * Services the receive interrupt for the port. Creates a list buffer and
774 * copies all waiting port data to that buffer and enqueues the buffer in the
775 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
776 */
777
Geoff Levand7626e782007-06-16 08:01:06 +1000778static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800779{
Geoff Levand7626e782007-06-16 08:01:06 +1000780 int result;
781 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800782 unsigned long flags;
Geoff Levand7626e782007-06-16 08:01:06 +1000783 u64 bytes;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800784
785 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
786
Geoff Levand7626e782007-06-16 08:01:06 +1000787 spin_lock_irqsave(&priv->rx_list.lock, flags);
788 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800789
Geoff Levand7626e782007-06-16 08:01:06 +1000790 if (result) {
791 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
792 return result;
Geoff Levandea1547d2007-02-06 14:23:47 -0800793 }
Geoff Levand7626e782007-06-16 08:01:06 +1000794
795 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
796 >= priv->rx_list.work.trigger) {
797 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
798 __func__, __LINE__, priv->rx_list.work.trigger);
799 priv->rx_list.work.trigger = 0;
800 schedule_work(&priv->rx_list.work.work);
801 }
802
803 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
804 return result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800805}
806
807static int ps3_vuart_handle_interrupt_disconnect(
Geoff Levand7626e782007-06-16 08:01:06 +1000808 struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800809{
810 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
811 BUG_ON("no support");
812 return -1;
813}
814
815/**
816 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
817 *
818 * Services any pending interrupt types for the port. Passes control to the
819 * third stage type specific interrupt handler. Returns control to the first
820 * stage handler after one iteration.
821 */
822
Geoff Levand7626e782007-06-16 08:01:06 +1000823static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800824{
825 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000826 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800827 unsigned long status;
828
Geoff Levand75c86e72007-02-13 17:37:28 -0800829 result = ps3_vuart_get_interrupt_status(dev, &status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800830
831 if (result)
832 return result;
833
834 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
835 status);
836
837 if (status & INTERRUPT_MASK_DISCONNECT) {
Geoff Levand7626e782007-06-16 08:01:06 +1000838 priv->stats.disconnect_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800839 result = ps3_vuart_handle_interrupt_disconnect(dev);
840 if (result)
841 ps3_vuart_disable_interrupt_disconnect(dev);
842 }
843
844 if (status & INTERRUPT_MASK_TX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000845 priv->stats.tx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800846 result = ps3_vuart_handle_interrupt_tx(dev);
847 if (result)
848 ps3_vuart_disable_interrupt_tx(dev);
849 }
850
851 if (status & INTERRUPT_MASK_RX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000852 priv->stats.rx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800853 result = ps3_vuart_handle_interrupt_rx(dev);
854 if (result)
855 ps3_vuart_disable_interrupt_rx(dev);
856 }
857
858 return 0;
859}
860
Xiongfeng Wang43c8a492020-04-29 18:00:48 +0800861static struct vuart_bus_priv {
Geoff Levand7626e782007-06-16 08:01:06 +1000862 struct ports_bmp *bmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800863 unsigned int virq;
Geoff Levandc4e67522008-01-19 07:32:53 +1100864 struct mutex probe_mutex;
Geoff Levand75c86e72007-02-13 17:37:28 -0800865 int use_count;
Geoff Levand7626e782007-06-16 08:01:06 +1000866 struct ps3_system_bus_device *devices[PORT_COUNT];
Xiongfeng Wang43c8a492020-04-29 18:00:48 +0800867} vuart_bus_priv;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800868
869/**
870 * ps3_vuart_irq_handler - first stage interrupt handler
871 *
872 * Loops finding any interrupting port and its associated instance data.
873 * Passes control to the second stage port specific interrupt handler. Loops
874 * until all outstanding interrupts are serviced.
875 */
876
877static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
878{
Geoff Levand7626e782007-06-16 08:01:06 +1000879 struct vuart_bus_priv *bus_priv = _private;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800880
Geoff Levand7626e782007-06-16 08:01:06 +1000881 BUG_ON(!bus_priv);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800882
883 while (1) {
884 unsigned int port;
885
Geoff Levand7626e782007-06-16 08:01:06 +1000886 dump_ports_bmp(bus_priv->bmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800887
Geoff Levand7626e782007-06-16 08:01:06 +1000888 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800889
890 if (port == BITS_PER_LONG)
891 break;
892
893 BUG_ON(port >= PORT_COUNT);
Geoff Levand75c86e72007-02-13 17:37:28 -0800894 BUG_ON(!bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800895
Geoff Levand75c86e72007-02-13 17:37:28 -0800896 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800897 }
898
899 return IRQ_HANDLED;
900}
901
Geoff Levand7626e782007-06-16 08:01:06 +1000902static int ps3_vuart_bus_interrupt_get(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800903{
904 int result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800905
Geoff Levand7626e782007-06-16 08:01:06 +1000906 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800907
Geoff Levand7626e782007-06-16 08:01:06 +1000908 vuart_bus_priv.use_count++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800909
Geoff Levand7626e782007-06-16 08:01:06 +1000910 BUG_ON(vuart_bus_priv.use_count > 2);
911
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100912 if (vuart_bus_priv.use_count != 1)
Geoff Levand7626e782007-06-16 08:01:06 +1000913 return 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000914
915 BUG_ON(vuart_bus_priv.bmp);
916
917 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
918
919 if (!vuart_bus_priv.bmp) {
Geoff Levand7626e782007-06-16 08:01:06 +1000920 result = -ENOMEM;
921 goto fail_bmp_malloc;
922 }
923
924 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
925 &vuart_bus_priv.virq);
926
927 if (result) {
928 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
929 __func__, __LINE__, result);
930 result = -EPERM;
931 goto fail_alloc_irq;
932 }
933
934 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
Yong Zhang6ea741a2011-10-21 23:56:53 +0000935 0, "vuart", &vuart_bus_priv);
Geoff Levand7626e782007-06-16 08:01:06 +1000936
937 if (result) {
938 pr_debug("%s:%d: request_irq failed (%d)\n",
939 __func__, __LINE__, result);
940 goto fail_request_irq;
941 }
942
943 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
944 return result;
945
946fail_request_irq:
947 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000948 vuart_bus_priv.virq = 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000949fail_alloc_irq:
950 kfree(vuart_bus_priv.bmp);
951 vuart_bus_priv.bmp = NULL;
952fail_bmp_malloc:
953 vuart_bus_priv.use_count--;
954 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800955 return result;
956}
957
Geoff Levand7626e782007-06-16 08:01:06 +1000958static int ps3_vuart_bus_interrupt_put(void)
959{
960 pr_debug(" -> %s:%d\n", __func__, __LINE__);
961
962 vuart_bus_priv.use_count--;
963
964 BUG_ON(vuart_bus_priv.use_count < 0);
965
966 if (vuart_bus_priv.use_count != 0)
967 return 0;
968
969 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
970
971 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000972 vuart_bus_priv.virq = 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000973
974 kfree(vuart_bus_priv.bmp);
975 vuart_bus_priv.bmp = NULL;
976
977 pr_debug(" <- %s:%d\n", __func__, __LINE__);
978 return 0;
979}
980
981static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800982{
983 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000984 struct ps3_vuart_port_driver *drv;
985 struct ps3_vuart_port_priv *priv = NULL;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800986
987 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
988
Geoff Levand7626e782007-06-16 08:01:06 +1000989 drv = ps3_system_bus_dev_to_vuart_drv(dev);
Colin King6a6120b2015-09-14 19:35:04 +0000990 BUG_ON(!drv);
Geoff Levand7626e782007-06-16 08:01:06 +1000991
992 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
993 drv->core.core.name);
994
Geoff Levand7626e782007-06-16 08:01:06 +1000995 if (dev->port_number >= PORT_COUNT) {
996 BUG();
997 return -EINVAL;
998 }
999
Geoff Levandc4e67522008-01-19 07:32:53 +11001000 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001001
Geoff Levand7626e782007-06-16 08:01:06 +10001002 result = ps3_vuart_bus_interrupt_get();
Geoff Levand75c86e72007-02-13 17:37:28 -08001003
Geoff Levand7626e782007-06-16 08:01:06 +10001004 if (result)
1005 goto fail_setup_interrupt;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001006
Geoff Levand7626e782007-06-16 08:01:06 +10001007 if (vuart_bus_priv.devices[dev->port_number]) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001008 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
Geoff Levand7626e782007-06-16 08:01:06 +10001009 __LINE__, dev->port_number);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001010 result = -EBUSY;
Geoff Levand7626e782007-06-16 08:01:06 +10001011 goto fail_busy;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001012 }
1013
Geoff Levand7626e782007-06-16 08:01:06 +10001014 vuart_bus_priv.devices[dev->port_number] = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001015
Geoff Levand7626e782007-06-16 08:01:06 +10001016 /* Setup dev->driver_priv. */
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001017
Geoff Levand7626e782007-06-16 08:01:06 +10001018 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1019 GFP_KERNEL);
Geoff Levand75c86e72007-02-13 17:37:28 -08001020
Geoff Levand7626e782007-06-16 08:01:06 +10001021 if (!dev->driver_priv) {
Geoff Levand75c86e72007-02-13 17:37:28 -08001022 result = -ENOMEM;
Geoff Levand7626e782007-06-16 08:01:06 +10001023 goto fail_dev_malloc;
Geoff Levand75c86e72007-02-13 17:37:28 -08001024 }
1025
Geoff Levand7626e782007-06-16 08:01:06 +10001026 priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -08001027
Geoff Levand7626e782007-06-16 08:01:06 +10001028 INIT_LIST_HEAD(&priv->tx_list.head);
1029 spin_lock_init(&priv->tx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001030
Geoff Levand7626e782007-06-16 08:01:06 +10001031 INIT_LIST_HEAD(&priv->rx_list.head);
1032 spin_lock_init(&priv->rx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001033
Tejun Heo4a8bb7f2014-03-07 10:24:48 -05001034 INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work);
Geoff Levand7626e782007-06-16 08:01:06 +10001035 priv->rx_list.work.trigger = 0;
1036 priv->rx_list.work.dev = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001037
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001038 /* clear stale pending interrupts */
Geoff Levand75c86e72007-02-13 17:37:28 -08001039
1040 ps3_vuart_clear_rx_bytes(dev, 0);
1041
1042 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001043
1044 ps3_vuart_set_triggers(dev, 1, 1);
1045
1046 if (drv->probe)
1047 result = drv->probe(dev);
1048 else {
1049 result = 0;
1050 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1051 __LINE__);
1052 }
1053
1054 if (result) {
1055 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1056 __func__, __LINE__);
1057 goto fail_probe;
1058 }
1059
Geoff Levandc4e67522008-01-19 07:32:53 +11001060 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001061
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001062 return result;
1063
1064fail_probe:
Geoff Levand75c86e72007-02-13 17:37:28 -08001065 ps3_vuart_set_interrupt_mask(dev, 0);
Geoff Levand7626e782007-06-16 08:01:06 +10001066 kfree(dev->driver_priv);
1067 dev->driver_priv = NULL;
1068fail_dev_malloc:
1069 vuart_bus_priv.devices[dev->port_number] = NULL;
1070fail_busy:
1071 ps3_vuart_bus_interrupt_put();
1072fail_setup_interrupt:
Geoff Levandc4e67522008-01-19 07:32:53 +11001073 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001074 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001075 return result;
1076}
1077
Geoff Levand7626e782007-06-16 08:01:06 +10001078/**
1079 * ps3_vuart_cleanup - common cleanup helper.
1080 * @dev: The struct ps3_system_bus_device instance.
1081 *
1082 * Cleans interrupts and HV resources. Must be called with
1083 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1084 * ps3_vuart_shutdown. After this call, polled reading will still work.
1085 */
1086
1087static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001088{
Geoff Levand7626e782007-06-16 08:01:06 +10001089 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1090
1091 ps3_vuart_cancel_async(dev);
1092 ps3_vuart_set_interrupt_mask(dev, 0);
1093 ps3_vuart_bus_interrupt_put();
1094 return 0;
1095}
1096
1097/**
1098 * ps3_vuart_remove - Completely clean the device instance.
1099 * @dev: The struct ps3_system_bus_device instance.
1100 *
1101 * Cleans all memory, interrupts and HV resources. After this call the
1102 * device can no longer be used.
1103 */
1104
Uwe Kleine-König6d247e42020-11-26 17:59:50 +01001105static void ps3_vuart_remove(struct ps3_system_bus_device *dev)
Geoff Levand7626e782007-06-16 08:01:06 +10001106{
1107 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1108 struct ps3_vuart_port_driver *drv;
1109
1110 BUG_ON(!dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001111
Geoff Levandc4e67522008-01-19 07:32:53 +11001112 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001113
Geoff Levand7626e782007-06-16 08:01:06 +10001114 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1115 dev->match_id);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001116
Geoff Levand7626e782007-06-16 08:01:06 +10001117 if (!dev->core.driver) {
1118 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1119 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001120 mutex_unlock(&vuart_bus_priv.probe_mutex);
Uwe Kleine-König6d247e42020-11-26 17:59:50 +01001121 return;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001122 }
Geoff Levand75c86e72007-02-13 17:37:28 -08001123
Geoff Levand7626e782007-06-16 08:01:06 +10001124 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1125
1126 BUG_ON(!drv);
1127
1128 if (drv->remove) {
1129 drv->remove(dev);
1130 } else {
1131 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1132 __LINE__);
1133 BUG();
1134 }
1135
1136 ps3_vuart_cleanup(dev);
1137
1138 vuart_bus_priv.devices[dev->port_number] = NULL;
1139 kfree(priv);
1140 priv = NULL;
1141
1142 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001143 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001144}
1145
1146/**
1147 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1148 * @dev: The struct ps3_system_bus_device instance.
1149 *
1150 * Cleans interrupts and HV resources. After this call the
1151 * device can still be used in polling mode. This behavior required
1152 * by sys-manager to be able to complete the device power operation
1153 * sequence.
1154 */
1155
Uwe Kleine-König6d247e42020-11-26 17:59:50 +01001156static void ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
Geoff Levand7626e782007-06-16 08:01:06 +10001157{
1158 struct ps3_vuart_port_driver *drv;
1159
1160 BUG_ON(!dev);
1161
Geoff Levandc4e67522008-01-19 07:32:53 +11001162 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001163
1164 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1165 dev->match_id);
1166
1167 if (!dev->core.driver) {
1168 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1169 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001170 mutex_unlock(&vuart_bus_priv.probe_mutex);
Uwe Kleine-König6d247e42020-11-26 17:59:50 +01001171 return;
Geoff Levand7626e782007-06-16 08:01:06 +10001172 }
1173
1174 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1175
1176 BUG_ON(!drv);
1177
1178 if (drv->shutdown)
1179 drv->shutdown(dev);
1180 else if (drv->remove) {
1181 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1182 __func__, __LINE__);
1183 drv->remove(dev);
1184 } else {
1185 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1186 __LINE__);
1187 BUG();
1188 }
1189
1190 ps3_vuart_cleanup(dev);
1191
1192 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001193
Geoff Levandc4e67522008-01-19 07:32:53 +11001194 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001195}
1196
Geoff Levand7626e782007-06-16 08:01:06 +10001197static int __init ps3_vuart_bus_init(void)
Geert Uytterhoeven5b8e8ee2007-02-12 00:55:15 -08001198{
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001199 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001200
1201 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
Geert Uytterhoevenef596c62007-03-10 00:05:38 +01001202 return -ENODEV;
Geoff Levand75c86e72007-02-13 17:37:28 -08001203
Geoff Levandc4e67522008-01-19 07:32:53 +11001204 mutex_init(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001205
Geoff Levand7626e782007-06-16 08:01:06 +10001206 return 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001207}
1208
Geoff Levand7626e782007-06-16 08:01:06 +10001209static void __exit ps3_vuart_bus_exit(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001210{
1211 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001212}
1213
Geoff Levand75c86e72007-02-13 17:37:28 -08001214core_initcall(ps3_vuart_bus_init);
1215module_exit(ps3_vuart_bus_exit);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001216
1217/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001218 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1219 */
1220
1221int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1222{
1223 int result;
1224
Geoff Levand7626e782007-06-16 08:01:06 +10001225 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1226
1227 BUG_ON(!drv->core.match_id);
1228 BUG_ON(!drv->core.core.name);
1229
1230 drv->core.probe = ps3_vuart_probe;
1231 drv->core.remove = ps3_vuart_remove;
1232 drv->core.shutdown = ps3_vuart_shutdown;
1233
1234 result = ps3_system_bus_driver_register(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001235 return result;
1236}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001237EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1238
1239/**
1240 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1241 */
1242
1243void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1244{
Geoff Levand7626e782007-06-16 08:01:06 +10001245 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1246 ps3_system_bus_driver_unregister(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001247}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001248EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);