blob: e4ad5ba5d0a3da7b4014c003f7582d9146323a44 [file] [log] [blame]
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001/*
2 * PS3 virtual uart
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
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 as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/interrupt.h>
Geoff Levandea1547d2007-02-06 14:23:47 -080024#include <linux/workqueue.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070025#include <linux/bitops.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080026#include <asm/ps3.h>
27
Geoff Levand75c86e72007-02-13 17:37:28 -080028#include <asm/firmware.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080029#include <asm/lv1call.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080030
31#include "vuart.h"
32
33MODULE_AUTHOR("Sony Corporation");
34MODULE_LICENSE("GPL v2");
Geoff Levand75c86e72007-02-13 17:37:28 -080035MODULE_DESCRIPTION("PS3 vuart");
Geoff Levand74e95d5d2006-12-08 18:27:47 -080036
37/**
38 * vuart - An inter-partition data link service.
39 * port 0: PS3 AV Settings.
40 * port 2: PS3 System Manager.
41 *
42 * The vuart provides a bi-directional byte stream data link between logical
43 * partitions. Its primary role is as a communications link between the guest
44 * OS and the system policy module. The current HV does not support any
45 * connections other than those listed.
46 */
47
48enum {PORT_COUNT = 3,};
49
50enum vuart_param {
51 PARAM_TX_TRIGGER = 0,
52 PARAM_RX_TRIGGER = 1,
53 PARAM_INTERRUPT_MASK = 2,
54 PARAM_RX_BUF_SIZE = 3, /* read only */
55 PARAM_RX_BYTES = 4, /* read only */
56 PARAM_TX_BUF_SIZE = 5, /* read only */
57 PARAM_TX_BYTES = 6, /* read only */
58 PARAM_INTERRUPT_STATUS = 7, /* read only */
59};
60
61enum vuart_interrupt_bit {
62 INTERRUPT_BIT_TX = 0,
63 INTERRUPT_BIT_RX = 1,
64 INTERRUPT_BIT_DISCONNECT = 2,
65};
66
67enum vuart_interrupt_mask {
68 INTERRUPT_MASK_TX = 1,
69 INTERRUPT_MASK_RX = 2,
70 INTERRUPT_MASK_DISCONNECT = 4,
71};
72
73/**
Geoff Levand7626e782007-06-16 08:01:06 +100074 * struct ps3_vuart_port_priv - private vuart device data.
75 */
76
77struct ps3_vuart_port_priv {
78 u64 interrupt_mask;
79
80 struct {
81 spinlock_t lock;
82 struct list_head head;
83 } tx_list;
84 struct {
85 struct ps3_vuart_work work;
86 unsigned long bytes_held;
87 spinlock_t lock;
88 struct list_head head;
89 } rx_list;
90 struct ps3_vuart_stats stats;
91};
92
93static struct ps3_vuart_port_priv *to_port_priv(
94 struct ps3_system_bus_device *dev)
95{
96 BUG_ON(!dev);
97 BUG_ON(!dev->driver_priv);
98 return (struct ps3_vuart_port_priv *)dev->driver_priv;
99}
100
101/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800102 * struct ports_bmp - bitmap indicating ports needing service.
103 *
104 * A 256 bit read only bitmap indicating ports needing service. Do not write
105 * to these bits. Must not cross a page boundary.
106 */
107
108struct ports_bmp {
109 u64 status;
110 u64 unused[3];
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100111} __attribute__((aligned(32)));
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800112
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800113#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000114static void __maybe_unused _dump_ports_bmp(
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100115 const struct ports_bmp *bmp, const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800116{
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000117 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800118}
119
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800120#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000121static void __maybe_unused _dump_port_params(unsigned int port_number,
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100122 const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800123{
124#if defined(DEBUG)
125 static const char *strings[] = {
126 "tx_trigger ",
127 "rx_trigger ",
128 "interrupt_mask ",
129 "rx_buf_size ",
130 "rx_bytes ",
131 "tx_buf_size ",
132 "tx_bytes ",
133 "interrupt_status",
134 };
135 int result;
136 unsigned int i;
137 u64 value;
138
139 for (i = 0; i < ARRAY_SIZE(strings); i++) {
140 result = lv1_get_virtual_uart_param(port_number, i, &value);
141
142 if (result) {
143 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
144 port_number, strings[i], ps3_result(result));
145 continue;
146 }
147 pr_debug("%s:%d: port_%u: %s = %lxh\n",
148 func, line, port_number, strings[i], value);
149 }
150#endif
151}
152
153struct vuart_triggers {
154 unsigned long rx;
155 unsigned long tx;
156};
157
Geoff Levand7626e782007-06-16 08:01:06 +1000158int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800159 struct vuart_triggers *trig)
160{
161 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000162 u64 size;
163 u64 val;
164 u64 tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800165
Geoff Levand7626e782007-06-16 08:01:06 +1000166 result = lv1_get_virtual_uart_param(dev->port_number,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000167 PARAM_TX_TRIGGER, &tx);
168 trig->tx = tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800169
170 if (result) {
171 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
172 __func__, __LINE__, ps3_result(result));
173 return result;
174 }
175
Geoff Levand7626e782007-06-16 08:01:06 +1000176 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800177 PARAM_RX_BUF_SIZE, &size);
178
179 if (result) {
180 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
181 __func__, __LINE__, ps3_result(result));
182 return result;
183 }
184
Geoff Levand7626e782007-06-16 08:01:06 +1000185 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800186 PARAM_RX_TRIGGER, &val);
187
188 if (result) {
189 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
190 __func__, __LINE__, ps3_result(result));
191 return result;
192 }
193
194 trig->rx = size - val;
195
196 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
197 trig->tx, trig->rx);
198
199 return result;
200}
201
Geoff Levand7626e782007-06-16 08:01:06 +1000202int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800203 unsigned int rx)
204{
205 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000206 u64 size;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800207
Geoff Levand7626e782007-06-16 08:01:06 +1000208 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800209 PARAM_TX_TRIGGER, tx);
210
211 if (result) {
212 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
213 __func__, __LINE__, ps3_result(result));
214 return result;
215 }
216
Geoff Levand7626e782007-06-16 08:01:06 +1000217 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800218 PARAM_RX_BUF_SIZE, &size);
219
220 if (result) {
221 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
222 __func__, __LINE__, ps3_result(result));
223 return result;
224 }
225
Geoff Levand7626e782007-06-16 08:01:06 +1000226 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800227 PARAM_RX_TRIGGER, size - rx);
228
229 if (result) {
230 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
231 __func__, __LINE__, ps3_result(result));
232 return result;
233 }
234
235 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
236 tx, rx);
237
238 return result;
239}
240
Geoff Levand7626e782007-06-16 08:01:06 +1000241static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800242 u64 *bytes_waiting)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800243{
Geoff Levand7626e782007-06-16 08:01:06 +1000244 int result;
245
246 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800247 PARAM_RX_BYTES, bytes_waiting);
248
249 if (result)
250 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
251 __func__, __LINE__, ps3_result(result));
252
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000253 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800254 *bytes_waiting);
255 return result;
256}
257
Geoff Levand7626e782007-06-16 08:01:06 +1000258/**
259 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
260 * @dev: The struct ps3_system_bus_device instance.
261 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
262 */
263
264static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800265 unsigned long mask)
266{
267 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000268 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800269
270 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
271
Geoff Levand7626e782007-06-16 08:01:06 +1000272 priv->interrupt_mask = mask;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800273
Geoff Levand7626e782007-06-16 08:01:06 +1000274 result = lv1_set_virtual_uart_param(dev->port_number,
275 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800276
277 if (result)
278 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
279 __func__, __LINE__, ps3_result(result));
280
281 return result;
282}
283
Geoff Levand7626e782007-06-16 08:01:06 +1000284static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800285 unsigned long *status)
286{
Geoff Levand7626e782007-06-16 08:01:06 +1000287 int result;
288 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800289 u64 tmp;
Geoff Levand7626e782007-06-16 08:01:06 +1000290
291 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand75c86e72007-02-13 17:37:28 -0800292 PARAM_INTERRUPT_STATUS, &tmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800293
294 if (result)
295 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
296 __func__, __LINE__, ps3_result(result));
297
Geoff Levand7626e782007-06-16 08:01:06 +1000298 *status = tmp & priv->interrupt_mask;
Geoff Levand75c86e72007-02-13 17:37:28 -0800299
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000300 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000301 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800302
303 return result;
304}
305
Geoff Levand7626e782007-06-16 08:01:06 +1000306int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800307{
Geoff Levand7626e782007-06-16 08:01:06 +1000308 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
309
310 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
311 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800312 | INTERRUPT_MASK_TX);
313}
314
Geoff Levand7626e782007-06-16 08:01:06 +1000315int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800316{
Geoff Levand7626e782007-06-16 08:01:06 +1000317 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
318
319 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
320 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800321 | INTERRUPT_MASK_RX);
322}
323
Geoff Levand7626e782007-06-16 08:01:06 +1000324int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800325{
Geoff Levand7626e782007-06-16 08:01:06 +1000326 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
327
328 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
329 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800330 | INTERRUPT_MASK_DISCONNECT);
331}
332
Geoff Levand7626e782007-06-16 08:01:06 +1000333int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800334{
Geoff Levand7626e782007-06-16 08:01:06 +1000335 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
336
337 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
338 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800339 & ~INTERRUPT_MASK_TX) : 0;
340}
341
Geoff Levand7626e782007-06-16 08:01:06 +1000342int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800343{
Geoff Levand7626e782007-06-16 08:01:06 +1000344 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
345
346 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
347 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800348 & ~INTERRUPT_MASK_RX) : 0;
349}
350
Geoff Levand7626e782007-06-16 08:01:06 +1000351int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800352{
Geoff Levand7626e782007-06-16 08:01:06 +1000353 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
354
355 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
356 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800357 & ~INTERRUPT_MASK_DISCONNECT) : 0;
358}
359
360/**
361 * ps3_vuart_raw_write - Low level write helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000362 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800363 *
364 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
365 */
366
Geoff Levand7626e782007-06-16 08:01:06 +1000367static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000368 const void *buf, unsigned int bytes, u64 *bytes_written)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800369{
370 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000371 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800372
Geoff Levand7626e782007-06-16 08:01:06 +1000373 result = lv1_write_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800374 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
375
376 if (result) {
377 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
378 "%s\n", __func__, __LINE__, ps3_result(result));
379 return result;
380 }
381
Geoff Levand7626e782007-06-16 08:01:06 +1000382 priv->stats.bytes_written += *bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800383
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000384 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000385 *bytes_written, bytes, priv->stats.bytes_written);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800386
387 return result;
388}
389
390/**
391 * ps3_vuart_raw_read - Low level read helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000392 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800393 *
394 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
395 */
396
Geoff Levand7626e782007-06-16 08:01:06 +1000397static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000398 unsigned int bytes, u64 *bytes_read)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800399{
400 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000401 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800402
403 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
404
Geoff Levand7626e782007-06-16 08:01:06 +1000405 result = lv1_read_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800406 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
407
408 if (result) {
409 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
410 __func__, __LINE__, ps3_result(result));
411 return result;
412 }
413
Geoff Levand7626e782007-06-16 08:01:06 +1000414 priv->stats.bytes_read += *bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800415
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000416 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000417 *bytes_read, bytes, priv->stats.bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800418
419 return result;
420}
421
422/**
Geoff Levand75c86e72007-02-13 17:37:28 -0800423 * ps3_vuart_clear_rx_bytes - Discard bytes received.
Geoff Levand7626e782007-06-16 08:01:06 +1000424 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand75c86e72007-02-13 17:37:28 -0800425 * @bytes: Max byte count to discard, zero = all pending.
426 *
427 * Used to clear pending rx interrupt source. Will not block.
428 */
429
Geoff Levand7626e782007-06-16 08:01:06 +1000430void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800431 unsigned int bytes)
432{
433 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000434 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800435 u64 bytes_waiting;
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100436 void *tmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800437
438 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
439
440 BUG_ON(result);
441
442 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
443
444 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
445
446 if (!bytes)
447 return;
448
449 /* Add some extra space for recently arrived data. */
450
451 bytes += 128;
452
453 tmp = kmalloc(bytes, GFP_KERNEL);
454
455 if (!tmp)
456 return;
457
458 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
459
460 kfree(tmp);
461
462 /* Don't include these bytes in the stats. */
463
Geoff Levand7626e782007-06-16 08:01:06 +1000464 priv->stats.bytes_read -= bytes_waiting;
Geoff Levand75c86e72007-02-13 17:37:28 -0800465}
Geoff Levand7626e782007-06-16 08:01:06 +1000466EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
Geoff Levand75c86e72007-02-13 17:37:28 -0800467
468/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800469 * struct list_buffer - An element for a port device fifo buffer list.
470 */
471
472struct list_buffer {
473 struct list_head link;
474 const unsigned char *head;
475 const unsigned char *tail;
476 unsigned long dbg_number;
477 unsigned char data[];
478};
479
480/**
481 * ps3_vuart_write - the entry point for writing data to a port
Geoff Levand7626e782007-06-16 08:01:06 +1000482 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800483 *
484 * If the port is idle on entry as much of the incoming data is written to
485 * the port as the port will accept. Otherwise a list buffer is created
486 * and any remaning incoming data is copied to that buffer. The buffer is
487 * then enqueued for transmision via the transmit interrupt.
488 */
489
Geoff Levand7626e782007-06-16 08:01:06 +1000490int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800491 unsigned int bytes)
492{
493 static unsigned long dbg_number;
494 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000495 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800496 unsigned long flags;
497 struct list_buffer *lb;
498
499 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
500 bytes, bytes);
501
Geoff Levand7626e782007-06-16 08:01:06 +1000502 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800503
Geoff Levand7626e782007-06-16 08:01:06 +1000504 if (list_empty(&priv->tx_list.head)) {
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000505 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800506
507 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
508
Geoff Levand7626e782007-06-16 08:01:06 +1000509 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800510
511 if (result) {
512 dev_dbg(&dev->core,
513 "%s:%d: ps3_vuart_raw_write failed\n",
514 __func__, __LINE__);
515 return result;
516 }
517
518 if (bytes_written == bytes) {
519 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
520 __func__, __LINE__, bytes);
521 return 0;
522 }
523
524 bytes -= bytes_written;
525 buf += bytes_written;
526 } else
Geoff Levand7626e782007-06-16 08:01:06 +1000527 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800528
529 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
530
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100531 if (!lb)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800532 return -ENOMEM;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800533
534 memcpy(lb->data, buf, bytes);
535 lb->head = lb->data;
536 lb->tail = lb->data + bytes;
537 lb->dbg_number = ++dbg_number;
538
Geoff Levand7626e782007-06-16 08:01:06 +1000539 spin_lock_irqsave(&priv->tx_list.lock, flags);
540 list_add_tail(&lb->link, &priv->tx_list.head);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800541 ps3_vuart_enable_interrupt_tx(dev);
Geoff Levand7626e782007-06-16 08:01:06 +1000542 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800543
544 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
545 __func__, __LINE__, lb->dbg_number, bytes);
546
547 return 0;
548}
Geoff Levand7626e782007-06-16 08:01:06 +1000549EXPORT_SYMBOL_GPL(ps3_vuart_write);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800550
551/**
Geoff Levand7626e782007-06-16 08:01:06 +1000552 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
553 * @dev: The struct ps3_system_bus_device instance.
554 * @bytes_queued: Number of bytes queued to the buffer list.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800555 *
Geoff Levand7626e782007-06-16 08:01:06 +1000556 * Must be called with priv->rx_list.lock held.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800557 */
558
Geoff Levand7626e782007-06-16 08:01:06 +1000559static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
560 u64 *bytes_queued)
561{
562 static unsigned long dbg_number;
563 int result;
564 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
565 struct list_buffer *lb;
566 u64 bytes;
567
568 *bytes_queued = 0;
569
570 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
571 BUG_ON(result);
572
573 if (result)
574 return -EIO;
575
576 if (!bytes)
577 return 0;
578
579 /* Add some extra space for recently arrived data. */
580
581 bytes += 128;
582
583 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
584
585 if (!lb)
586 return -ENOMEM;
587
588 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
589
590 lb->head = lb->data;
591 lb->tail = lb->data + bytes;
592 lb->dbg_number = ++dbg_number;
593
594 list_add_tail(&lb->link, &priv->rx_list.head);
595 priv->rx_list.bytes_held += bytes;
596
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000597 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000598 __func__, __LINE__, lb->dbg_number, bytes);
599
600 *bytes_queued = bytes;
601
602 return 0;
603}
604
605/**
606 * ps3_vuart_read - The entry point for reading data from a port.
607 *
608 * Queue data waiting at the port, and if enough bytes to satisfy the request
609 * are held in the buffer list those bytes are dequeued and copied to the
610 * caller's buffer. Emptied list buffers are retiered. If the request cannot
611 * be statified by bytes held in the list buffers -EAGAIN is returned.
612 */
613
614int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800615 unsigned int bytes)
616{
Geoff Levand7626e782007-06-16 08:01:06 +1000617 int result;
618 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800619 unsigned long flags;
620 struct list_buffer *lb, *n;
621 unsigned long bytes_read;
622
623 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
624 bytes, bytes);
625
Geoff Levand7626e782007-06-16 08:01:06 +1000626 spin_lock_irqsave(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800627
Geoff Levand7626e782007-06-16 08:01:06 +1000628 /* Queue rx bytes here for polled reads. */
629
630 while (priv->rx_list.bytes_held < bytes) {
631 u64 tmp;
632
633 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
634 if (result || !tmp) {
635 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
636 __func__, __LINE__,
637 bytes - priv->rx_list.bytes_held);
638 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
639 return -EAGAIN;
640 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800641 }
642
Geoff Levand7626e782007-06-16 08:01:06 +1000643 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800644 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
645
646 memcpy(buf, lb->head, bytes_read);
647 buf += bytes_read;
648 bytes -= bytes_read;
Geoff Levand7626e782007-06-16 08:01:06 +1000649 priv->rx_list.bytes_held -= bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800650
651 if (bytes_read < lb->tail - lb->head) {
652 lb->head += bytes_read;
Geoff Levand75c86e72007-02-13 17:37:28 -0800653 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
654 "bytes\n", __func__, __LINE__, lb->dbg_number,
655 bytes_read);
Geoff Levand7626e782007-06-16 08:01:06 +1000656 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800657 return 0;
658 }
659
Geoff Levand75c86e72007-02-13 17:37:28 -0800660 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
661 "bytes\n", __func__, __LINE__, lb->dbg_number,
662 bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800663
664 list_del(&lb->link);
665 kfree(lb);
666 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800667
Geoff Levand7626e782007-06-16 08:01:06 +1000668 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800669 return 0;
670}
Geoff Levand7626e782007-06-16 08:01:06 +1000671EXPORT_SYMBOL_GPL(ps3_vuart_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800672
Geoff Levand7626e782007-06-16 08:01:06 +1000673/**
674 * ps3_vuart_work - Asynchronous read handler.
675 */
676
677static void ps3_vuart_work(struct work_struct *work)
Geoff Levandea1547d2007-02-06 14:23:47 -0800678{
Geoff Levand7626e782007-06-16 08:01:06 +1000679 struct ps3_system_bus_device *dev =
680 ps3_vuart_work_to_system_bus_dev(work);
681 struct ps3_vuart_port_driver *drv =
682 ps3_system_bus_dev_to_vuart_drv(dev);
683
684 BUG_ON(!drv);
685 drv->work(dev);
686}
687
688int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
689{
690 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levandea1547d2007-02-06 14:23:47 -0800691 unsigned long flags;
692
Geoff Levand7626e782007-06-16 08:01:06 +1000693 if (priv->rx_list.work.trigger) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800694 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
695 __func__, __LINE__);
696 return -EAGAIN;
697 }
698
699 BUG_ON(!bytes);
700
Geoff Levand7626e782007-06-16 08:01:06 +1000701 PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
Geoff Levandea1547d2007-02-06 14:23:47 -0800702
Geoff Levand7626e782007-06-16 08:01:06 +1000703 spin_lock_irqsave(&priv->rx_list.lock, flags);
704 if (priv->rx_list.bytes_held >= bytes) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800705 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
706 __func__, __LINE__, bytes);
Geoff Levand7626e782007-06-16 08:01:06 +1000707 schedule_work(&priv->rx_list.work.work);
708 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800709 return 0;
710 }
711
Geoff Levand7626e782007-06-16 08:01:06 +1000712 priv->rx_list.work.trigger = bytes;
713 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800714
715 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
716 __LINE__, bytes, bytes);
717
718 return 0;
719}
Geoff Levand7626e782007-06-16 08:01:06 +1000720EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800721
Geoff Levand7626e782007-06-16 08:01:06 +1000722void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
Geoff Levandea1547d2007-02-06 14:23:47 -0800723{
Geoff Levand7626e782007-06-16 08:01:06 +1000724 to_port_priv(dev)->rx_list.work.trigger = 0;
Geoff Levandea1547d2007-02-06 14:23:47 -0800725}
Geoff Levand7626e782007-06-16 08:01:06 +1000726EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800727
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800728/**
729 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
730 *
731 * Services the transmit interrupt for the port. Writes as much data from the
732 * buffer list as the port will accept. Retires any emptied list buffers and
733 * adjusts the final list buffer state for a partial write.
734 */
735
Geoff Levand7626e782007-06-16 08:01:06 +1000736static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800737{
738 int result = 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000739 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800740 unsigned long flags;
741 struct list_buffer *lb, *n;
742 unsigned long bytes_total = 0;
743
744 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
745
Geoff Levand7626e782007-06-16 08:01:06 +1000746 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800747
Geoff Levand7626e782007-06-16 08:01:06 +1000748 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800749
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000750 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800751
752 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
753 &bytes_written);
754
755 if (result) {
756 dev_dbg(&dev->core,
757 "%s:%d: ps3_vuart_raw_write failed\n",
758 __func__, __LINE__);
759 break;
760 }
761
762 bytes_total += bytes_written;
763
764 if (bytes_written < lb->tail - lb->head) {
765 lb->head += bytes_written;
766 dev_dbg(&dev->core,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000767 "%s:%d cleared buf_%lu, %llxh bytes\n",
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800768 __func__, __LINE__, lb->dbg_number,
769 bytes_written);
770 goto port_full;
771 }
772
773 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
774 lb->dbg_number);
775
776 list_del(&lb->link);
777 kfree(lb);
778 }
779
780 ps3_vuart_disable_interrupt_tx(dev);
781port_full:
Geoff Levand7626e782007-06-16 08:01:06 +1000782 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800783 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
784 __func__, __LINE__, bytes_total);
785 return result;
786}
787
788/**
789 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
790 *
791 * Services the receive interrupt for the port. Creates a list buffer and
792 * copies all waiting port data to that buffer and enqueues the buffer in the
793 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
794 */
795
Geoff Levand7626e782007-06-16 08:01:06 +1000796static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800797{
Geoff Levand7626e782007-06-16 08:01:06 +1000798 int result;
799 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800800 unsigned long flags;
Geoff Levand7626e782007-06-16 08:01:06 +1000801 u64 bytes;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800802
803 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
804
Geoff Levand7626e782007-06-16 08:01:06 +1000805 spin_lock_irqsave(&priv->rx_list.lock, flags);
806 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800807
Geoff Levand7626e782007-06-16 08:01:06 +1000808 if (result) {
809 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
810 return result;
Geoff Levandea1547d2007-02-06 14:23:47 -0800811 }
Geoff Levand7626e782007-06-16 08:01:06 +1000812
813 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
814 >= priv->rx_list.work.trigger) {
815 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
816 __func__, __LINE__, priv->rx_list.work.trigger);
817 priv->rx_list.work.trigger = 0;
818 schedule_work(&priv->rx_list.work.work);
819 }
820
821 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
822 return result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800823}
824
825static int ps3_vuart_handle_interrupt_disconnect(
Geoff Levand7626e782007-06-16 08:01:06 +1000826 struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800827{
828 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
829 BUG_ON("no support");
830 return -1;
831}
832
833/**
834 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
835 *
836 * Services any pending interrupt types for the port. Passes control to the
837 * third stage type specific interrupt handler. Returns control to the first
838 * stage handler after one iteration.
839 */
840
Geoff Levand7626e782007-06-16 08:01:06 +1000841static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800842{
843 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000844 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800845 unsigned long status;
846
Geoff Levand75c86e72007-02-13 17:37:28 -0800847 result = ps3_vuart_get_interrupt_status(dev, &status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800848
849 if (result)
850 return result;
851
852 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
853 status);
854
855 if (status & INTERRUPT_MASK_DISCONNECT) {
Geoff Levand7626e782007-06-16 08:01:06 +1000856 priv->stats.disconnect_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800857 result = ps3_vuart_handle_interrupt_disconnect(dev);
858 if (result)
859 ps3_vuart_disable_interrupt_disconnect(dev);
860 }
861
862 if (status & INTERRUPT_MASK_TX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000863 priv->stats.tx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800864 result = ps3_vuart_handle_interrupt_tx(dev);
865 if (result)
866 ps3_vuart_disable_interrupt_tx(dev);
867 }
868
869 if (status & INTERRUPT_MASK_RX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000870 priv->stats.rx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800871 result = ps3_vuart_handle_interrupt_rx(dev);
872 if (result)
873 ps3_vuart_disable_interrupt_rx(dev);
874 }
875
876 return 0;
877}
878
Geoff Levand75c86e72007-02-13 17:37:28 -0800879struct vuart_bus_priv {
Geoff Levand7626e782007-06-16 08:01:06 +1000880 struct ports_bmp *bmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800881 unsigned int virq;
Geoff Levandc4e67522008-01-19 07:32:53 +1100882 struct mutex probe_mutex;
Geoff Levand75c86e72007-02-13 17:37:28 -0800883 int use_count;
Geoff Levand7626e782007-06-16 08:01:06 +1000884 struct ps3_system_bus_device *devices[PORT_COUNT];
Geoff Levand75c86e72007-02-13 17:37:28 -0800885} static vuart_bus_priv;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800886
887/**
888 * ps3_vuart_irq_handler - first stage interrupt handler
889 *
890 * Loops finding any interrupting port and its associated instance data.
891 * Passes control to the second stage port specific interrupt handler. Loops
892 * until all outstanding interrupts are serviced.
893 */
894
895static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
896{
Geoff Levand7626e782007-06-16 08:01:06 +1000897 struct vuart_bus_priv *bus_priv = _private;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800898
Geoff Levand7626e782007-06-16 08:01:06 +1000899 BUG_ON(!bus_priv);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800900
901 while (1) {
902 unsigned int port;
903
Geoff Levand7626e782007-06-16 08:01:06 +1000904 dump_ports_bmp(bus_priv->bmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800905
Geoff Levand7626e782007-06-16 08:01:06 +1000906 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800907
908 if (port == BITS_PER_LONG)
909 break;
910
911 BUG_ON(port >= PORT_COUNT);
Geoff Levand75c86e72007-02-13 17:37:28 -0800912 BUG_ON(!bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800913
Geoff Levand75c86e72007-02-13 17:37:28 -0800914 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800915 }
916
917 return IRQ_HANDLED;
918}
919
Geoff Levand7626e782007-06-16 08:01:06 +1000920static int ps3_vuart_bus_interrupt_get(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800921{
922 int result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800923
Geoff Levand7626e782007-06-16 08:01:06 +1000924 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800925
Geoff Levand7626e782007-06-16 08:01:06 +1000926 vuart_bus_priv.use_count++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800927
Geoff Levand7626e782007-06-16 08:01:06 +1000928 BUG_ON(vuart_bus_priv.use_count > 2);
929
Geert Uytterhoevend0e5c212008-01-19 07:32:19 +1100930 if (vuart_bus_priv.use_count != 1)
Geoff Levand7626e782007-06-16 08:01:06 +1000931 return 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000932
933 BUG_ON(vuart_bus_priv.bmp);
934
935 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
936
937 if (!vuart_bus_priv.bmp) {
938 pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
939 result = -ENOMEM;
940 goto fail_bmp_malloc;
941 }
942
943 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
944 &vuart_bus_priv.virq);
945
946 if (result) {
947 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
948 __func__, __LINE__, result);
949 result = -EPERM;
950 goto fail_alloc_irq;
951 }
952
953 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
954 IRQF_DISABLED, "vuart", &vuart_bus_priv);
955
956 if (result) {
957 pr_debug("%s:%d: request_irq failed (%d)\n",
958 __func__, __LINE__, result);
959 goto fail_request_irq;
960 }
961
962 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
963 return result;
964
965fail_request_irq:
966 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
967 vuart_bus_priv.virq = NO_IRQ;
968fail_alloc_irq:
969 kfree(vuart_bus_priv.bmp);
970 vuart_bus_priv.bmp = NULL;
971fail_bmp_malloc:
972 vuart_bus_priv.use_count--;
973 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800974 return result;
975}
976
Geoff Levand7626e782007-06-16 08:01:06 +1000977static int ps3_vuart_bus_interrupt_put(void)
978{
979 pr_debug(" -> %s:%d\n", __func__, __LINE__);
980
981 vuart_bus_priv.use_count--;
982
983 BUG_ON(vuart_bus_priv.use_count < 0);
984
985 if (vuart_bus_priv.use_count != 0)
986 return 0;
987
988 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
989
990 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
991 vuart_bus_priv.virq = NO_IRQ;
992
993 kfree(vuart_bus_priv.bmp);
994 vuart_bus_priv.bmp = NULL;
995
996 pr_debug(" <- %s:%d\n", __func__, __LINE__);
997 return 0;
998}
999
1000static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001001{
1002 int result;
Geoff Levand7626e782007-06-16 08:01:06 +10001003 struct ps3_vuart_port_driver *drv;
1004 struct ps3_vuart_port_priv *priv = NULL;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001005
1006 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1007
Geoff Levand7626e782007-06-16 08:01:06 +10001008 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1009
1010 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1011 drv->core.core.name);
1012
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001013 BUG_ON(!drv);
1014
Geoff Levand7626e782007-06-16 08:01:06 +10001015 if (dev->port_number >= PORT_COUNT) {
1016 BUG();
1017 return -EINVAL;
1018 }
1019
Geoff Levandc4e67522008-01-19 07:32:53 +11001020 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001021
Geoff Levand7626e782007-06-16 08:01:06 +10001022 result = ps3_vuart_bus_interrupt_get();
Geoff Levand75c86e72007-02-13 17:37:28 -08001023
Geoff Levand7626e782007-06-16 08:01:06 +10001024 if (result)
1025 goto fail_setup_interrupt;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001026
Geoff Levand7626e782007-06-16 08:01:06 +10001027 if (vuart_bus_priv.devices[dev->port_number]) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001028 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
Geoff Levand7626e782007-06-16 08:01:06 +10001029 __LINE__, dev->port_number);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001030 result = -EBUSY;
Geoff Levand7626e782007-06-16 08:01:06 +10001031 goto fail_busy;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001032 }
1033
Geoff Levand7626e782007-06-16 08:01:06 +10001034 vuart_bus_priv.devices[dev->port_number] = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001035
Geoff Levand7626e782007-06-16 08:01:06 +10001036 /* Setup dev->driver_priv. */
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001037
Geoff Levand7626e782007-06-16 08:01:06 +10001038 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1039 GFP_KERNEL);
Geoff Levand75c86e72007-02-13 17:37:28 -08001040
Geoff Levand7626e782007-06-16 08:01:06 +10001041 if (!dev->driver_priv) {
Geoff Levand75c86e72007-02-13 17:37:28 -08001042 result = -ENOMEM;
Geoff Levand7626e782007-06-16 08:01:06 +10001043 goto fail_dev_malloc;
Geoff Levand75c86e72007-02-13 17:37:28 -08001044 }
1045
Geoff Levand7626e782007-06-16 08:01:06 +10001046 priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -08001047
Geoff Levand7626e782007-06-16 08:01:06 +10001048 INIT_LIST_HEAD(&priv->tx_list.head);
1049 spin_lock_init(&priv->tx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001050
Geoff Levand7626e782007-06-16 08:01:06 +10001051 INIT_LIST_HEAD(&priv->rx_list.head);
1052 spin_lock_init(&priv->rx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001053
Geoff Levand7626e782007-06-16 08:01:06 +10001054 INIT_WORK(&priv->rx_list.work.work, NULL);
1055 priv->rx_list.work.trigger = 0;
1056 priv->rx_list.work.dev = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001057
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001058 /* clear stale pending interrupts */
Geoff Levand75c86e72007-02-13 17:37:28 -08001059
1060 ps3_vuart_clear_rx_bytes(dev, 0);
1061
1062 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001063
1064 ps3_vuart_set_triggers(dev, 1, 1);
1065
1066 if (drv->probe)
1067 result = drv->probe(dev);
1068 else {
1069 result = 0;
1070 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1071 __LINE__);
1072 }
1073
1074 if (result) {
1075 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1076 __func__, __LINE__);
1077 goto fail_probe;
1078 }
1079
Geoff Levandc4e67522008-01-19 07:32:53 +11001080 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001081
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001082 return result;
1083
1084fail_probe:
Geoff Levand75c86e72007-02-13 17:37:28 -08001085 ps3_vuart_set_interrupt_mask(dev, 0);
Geoff Levand7626e782007-06-16 08:01:06 +10001086 kfree(dev->driver_priv);
1087 dev->driver_priv = NULL;
1088fail_dev_malloc:
1089 vuart_bus_priv.devices[dev->port_number] = NULL;
1090fail_busy:
1091 ps3_vuart_bus_interrupt_put();
1092fail_setup_interrupt:
Geoff Levandc4e67522008-01-19 07:32:53 +11001093 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001094 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001095 return result;
1096}
1097
Geoff Levand7626e782007-06-16 08:01:06 +10001098/**
1099 * ps3_vuart_cleanup - common cleanup helper.
1100 * @dev: The struct ps3_system_bus_device instance.
1101 *
1102 * Cleans interrupts and HV resources. Must be called with
1103 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1104 * ps3_vuart_shutdown. After this call, polled reading will still work.
1105 */
1106
1107static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001108{
Geoff Levand7626e782007-06-16 08:01:06 +10001109 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1110
1111 ps3_vuart_cancel_async(dev);
1112 ps3_vuart_set_interrupt_mask(dev, 0);
1113 ps3_vuart_bus_interrupt_put();
1114 return 0;
1115}
1116
1117/**
1118 * ps3_vuart_remove - Completely clean the device instance.
1119 * @dev: The struct ps3_system_bus_device instance.
1120 *
1121 * Cleans all memory, interrupts and HV resources. After this call the
1122 * device can no longer be used.
1123 */
1124
1125static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1126{
1127 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1128 struct ps3_vuart_port_driver *drv;
1129
1130 BUG_ON(!dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001131
Geoff Levandc4e67522008-01-19 07:32:53 +11001132 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001133
Geoff Levand7626e782007-06-16 08:01:06 +10001134 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1135 dev->match_id);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001136
Geoff Levand7626e782007-06-16 08:01:06 +10001137 if (!dev->core.driver) {
1138 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1139 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001140 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001141 return 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001142 }
Geoff Levand75c86e72007-02-13 17:37:28 -08001143
Geoff Levand7626e782007-06-16 08:01:06 +10001144 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1145
1146 BUG_ON(!drv);
1147
1148 if (drv->remove) {
1149 drv->remove(dev);
1150 } else {
1151 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1152 __LINE__);
1153 BUG();
1154 }
1155
1156 ps3_vuart_cleanup(dev);
1157
1158 vuart_bus_priv.devices[dev->port_number] = NULL;
1159 kfree(priv);
1160 priv = NULL;
1161
1162 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001163 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001164 return 0;
1165}
1166
1167/**
1168 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1169 * @dev: The struct ps3_system_bus_device instance.
1170 *
1171 * Cleans interrupts and HV resources. After this call the
1172 * device can still be used in polling mode. This behavior required
1173 * by sys-manager to be able to complete the device power operation
1174 * sequence.
1175 */
1176
1177static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1178{
1179 struct ps3_vuart_port_driver *drv;
1180
1181 BUG_ON(!dev);
1182
Geoff Levandc4e67522008-01-19 07:32:53 +11001183 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001184
1185 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1186 dev->match_id);
1187
1188 if (!dev->core.driver) {
1189 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1190 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001191 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001192 return 0;
1193 }
1194
1195 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1196
1197 BUG_ON(!drv);
1198
1199 if (drv->shutdown)
1200 drv->shutdown(dev);
1201 else if (drv->remove) {
1202 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1203 __func__, __LINE__);
1204 drv->remove(dev);
1205 } else {
1206 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1207 __LINE__);
1208 BUG();
1209 }
1210
1211 ps3_vuart_cleanup(dev);
1212
1213 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001214
Geoff Levandc4e67522008-01-19 07:32:53 +11001215 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001216 return 0;
1217}
1218
Geoff Levand7626e782007-06-16 08:01:06 +10001219static int __init ps3_vuart_bus_init(void)
Geert Uytterhoeven5b8e8ee2007-02-12 00:55:15 -08001220{
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001221 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001222
1223 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
Geert Uytterhoevenef596c62007-03-10 00:05:38 +01001224 return -ENODEV;
Geoff Levand75c86e72007-02-13 17:37:28 -08001225
Geoff Levandc4e67522008-01-19 07:32:53 +11001226 mutex_init(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001227
Geoff Levand7626e782007-06-16 08:01:06 +10001228 return 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001229}
1230
Geoff Levand7626e782007-06-16 08:01:06 +10001231static void __exit ps3_vuart_bus_exit(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001232{
1233 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001234}
1235
Geoff Levand75c86e72007-02-13 17:37:28 -08001236core_initcall(ps3_vuart_bus_init);
1237module_exit(ps3_vuart_bus_exit);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001238
1239/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001240 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1241 */
1242
1243int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1244{
1245 int result;
1246
Geoff Levand7626e782007-06-16 08:01:06 +10001247 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1248
1249 BUG_ON(!drv->core.match_id);
1250 BUG_ON(!drv->core.core.name);
1251
1252 drv->core.probe = ps3_vuart_probe;
1253 drv->core.remove = ps3_vuart_remove;
1254 drv->core.shutdown = ps3_vuart_shutdown;
1255
1256 result = ps3_system_bus_driver_register(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001257 return result;
1258}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001259EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1260
1261/**
1262 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1263 */
1264
1265void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1266{
Geoff Levand7626e782007-06-16 08:01:06 +10001267 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1268 ps3_system_bus_driver_unregister(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001269}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001270EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);